コード例 #1
0
        internal static unsafe X509Certificate2 CreateDummyCertificate(CspParameters parameters)
        {
            System.Security.Cryptography.SafeCertContextHandle invalidHandle = System.Security.Cryptography.SafeCertContextHandle.InvalidHandle;
            SafeCryptProvHandle hCryptProv = SafeCryptProvHandle.InvalidHandle;
            uint dwFlags = 0U;

            if ((parameters.Flags & CspProviderFlags.UseMachineKeyStore) != CspProviderFlags.NoFlags)
            {
                dwFlags |= 32U;
            }
            if ((parameters.Flags & CspProviderFlags.UseDefaultKeyContainer) != CspProviderFlags.NoFlags)
            {
                dwFlags |= 4026531840U;
            }
            if ((parameters.Flags & CspProviderFlags.NoPrompt) != CspProviderFlags.NoFlags)
            {
                dwFlags |= 64U;
            }
            if (!CAPI.CryptAcquireContext(ref hCryptProv, parameters.KeyContainerName, parameters.ProviderName, (uint)parameters.ProviderType, dwFlags))
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            CAPI.CRYPT_KEY_PROV_INFO cryptKeyProvInfo = new CAPI.CRYPT_KEY_PROV_INFO();
            cryptKeyProvInfo.pwszProvName      = parameters.ProviderName;
            cryptKeyProvInfo.pwszContainerName = parameters.KeyContainerName;
            cryptKeyProvInfo.dwProvType        = (uint)parameters.ProviderType;
            cryptKeyProvInfo.dwKeySpec         = (uint)parameters.KeyNumber;
            cryptKeyProvInfo.dwFlags           = (parameters.Flags & CspProviderFlags.UseMachineKeyStore) == CspProviderFlags.UseMachineKeyStore ? 32U : 0U;
            SafeLocalAllocHandle localAllocHandle1 = CAPI.LocalAlloc(64U, new IntPtr(Marshal.SizeOf(typeof(CAPI.CRYPT_KEY_PROV_INFO))));

            Marshal.StructureToPtr((object)cryptKeyProvInfo, localAllocHandle1.DangerousGetHandle(), false);
            CAPI.CRYPT_ALGORITHM_IDENTIFIER algorithmIdentifier = new CAPI.CRYPT_ALGORITHM_IDENTIFIER();
            algorithmIdentifier.pszObjId = "1.3.14.3.2.29";
            SafeLocalAllocHandle localAllocHandle2 = CAPI.LocalAlloc(64U, new IntPtr(Marshal.SizeOf(typeof(CAPI.CRYPT_ALGORITHM_IDENTIFIER))));

            Marshal.StructureToPtr((object)algorithmIdentifier, localAllocHandle2.DangerousGetHandle(), false);
            X500DistinguishedName distinguishedName = new X500DistinguishedName("cn=CMS Signer Dummy Certificate");

            System.Security.Cryptography.SafeCertContextHandle selfSignCertificate;

            fixed(byte *numPtr = distinguishedName.RawData)
            selfSignCertificate = CAPI.CAPIUnsafe.CertCreateSelfSignCertificate(hCryptProv, new IntPtr((void *)&new CAPI.CRYPTOAPI_BLOB()
            {
                cbData = (uint)distinguishedName.RawData.Length,
                pbData = new IntPtr((void *)numPtr)
            }), 1U, localAllocHandle1.DangerousGetHandle(), localAllocHandle2.DangerousGetHandle(), IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);

            Marshal.DestroyStructure(localAllocHandle1.DangerousGetHandle(), typeof(CAPI.CRYPT_KEY_PROV_INFO));
            localAllocHandle1.Dispose();
            Marshal.DestroyStructure(localAllocHandle2.DangerousGetHandle(), typeof(CAPI.CRYPT_ALGORITHM_IDENTIFIER));
            localAllocHandle2.Dispose();
            if (selfSignCertificate == null || selfSignCertificate.IsInvalid)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            X509Certificate2 x509Certificate2 = new X509Certificate2(selfSignCertificate.DangerousGetHandle());

            selfSignCertificate.Dispose();
            return(x509Certificate2);
        }
コード例 #2
0
        internal static unsafe CAPI.CMSG_SIGNER_ENCODE_INFO CreateSignerEncodeInfo(CmsSigner signer, bool silent)
        {
            CAPI.CMSG_SIGNER_ENCODE_INFO signerEncodeInfo = new CAPI.CMSG_SIGNER_ENCODE_INFO(Marshal.SizeOf(typeof(CAPI.CMSG_SIGNER_ENCODE_INFO)));
            SafeCryptProvHandle          invalidHandle1   = SafeCryptProvHandle.InvalidHandle;
            uint pdwKeySpec       = 0U;
            bool pfCallerFreeProv = false;

            signerEncodeInfo.HashAlgorithm.pszObjId = signer.DigestAlgorithm.Value;
            if (string.Compare(signer.Certificate.PublicKey.Oid.Value, "1.2.840.10040.4.1", StringComparison.Ordinal) == 0)
            {
                signerEncodeInfo.HashEncryptionAlgorithm.pszObjId = "1.2.840.10040.4.3";
            }
            signerEncodeInfo.cAuthAttr    = (uint)signer.SignedAttributes.Count;
            signerEncodeInfo.rgAuthAttr   = PkcsUtils.CreateCryptAttributes(signer.SignedAttributes);
            signerEncodeInfo.cUnauthAttr  = (uint)signer.UnsignedAttributes.Count;
            signerEncodeInfo.rgUnauthAttr = PkcsUtils.CreateCryptAttributes(signer.UnsignedAttributes);
            if (signer.SignerIdentifierType == SubjectIdentifierType.NoSignature)
            {
                signerEncodeInfo.HashEncryptionAlgorithm.pszObjId = "1.3.6.1.5.5.7.6.2";
                signerEncodeInfo.pCertInfo = IntPtr.Zero;
                signerEncodeInfo.dwKeySpec = pdwKeySpec;
                if (!CAPI.CryptAcquireContext(out invalidHandle1, (string)null, (string)null, 1U, 4026531840U))
                {
                    throw new CryptographicException(Marshal.GetLastWin32Error());
                }
                signerEncodeInfo.hCryptProv = invalidHandle1.DangerousGetHandle();
                GC.SuppressFinalize((object)invalidHandle1);
                signerEncodeInfo.SignerId.dwIdChoice = 1U;
                X500DistinguishedName distinguishedName = new X500DistinguishedName("CN=Dummy Signer");
                distinguishedName.Oid = new Oid("1.3.6.1.4.1.311.21.9");
                signerEncodeInfo.SignerId.Value.IssuerSerialNumber.Issuer.cbData = (uint)distinguishedName.RawData.Length;
                SafeLocalAllocHandle localAllocHandle1 = CAPI.LocalAlloc(64U, new IntPtr((long)signerEncodeInfo.SignerId.Value.IssuerSerialNumber.Issuer.cbData));
                Marshal.Copy(distinguishedName.RawData, 0, localAllocHandle1.DangerousGetHandle(), distinguishedName.RawData.Length);
                signerEncodeInfo.SignerId.Value.IssuerSerialNumber.Issuer.pbData = localAllocHandle1.DangerousGetHandle();
                GC.SuppressFinalize((object)localAllocHandle1);
                signerEncodeInfo.SignerId.Value.IssuerSerialNumber.SerialNumber.cbData = 1U;
                SafeLocalAllocHandle localAllocHandle2 = CAPI.LocalAlloc(64U, new IntPtr((long)signerEncodeInfo.SignerId.Value.IssuerSerialNumber.SerialNumber.cbData));
                *(sbyte *)(void *)localAllocHandle2.DangerousGetHandle() = (sbyte)0;
                signerEncodeInfo.SignerId.Value.IssuerSerialNumber.SerialNumber.pbData = localAllocHandle2.DangerousGetHandle();
                GC.SuppressFinalize((object)localAllocHandle2);
                return(signerEncodeInfo);
            }
            else
            {
                System.Security.Cryptography.SafeCertContextHandle certContext1 = X509Utils.GetCertContext(signer.Certificate);
                if (!CAPI.CAPISafe.CryptAcquireCertificatePrivateKey(certContext1, silent ? 70U : 6U, IntPtr.Zero, out invalidHandle1, out pdwKeySpec, out pfCallerFreeProv))
                {
                    throw new CryptographicException(Marshal.GetLastWin32Error());
                }
                signerEncodeInfo.dwKeySpec  = pdwKeySpec;
                signerEncodeInfo.hCryptProv = invalidHandle1.DangerousGetHandle();
                GC.SuppressFinalize((object)invalidHandle1);
                CAPI.CERT_CONTEXT certContext2 = *(CAPI.CERT_CONTEXT *)(void *) certContext1.DangerousGetHandle();
                signerEncodeInfo.pCertInfo = certContext2.pCertInfo;
                if (signer.SignerIdentifierType == SubjectIdentifierType.SubjectKeyIdentifier)
                {
                    uint pcbData = 0U;
                    SafeLocalAllocHandle invalidHandle2 = SafeLocalAllocHandle.InvalidHandle;
                    if (!CAPI.CAPISafe.CertGetCertificateContextProperty(certContext1, 20U, invalidHandle2, out pcbData))
                    {
                        throw new CryptographicException(Marshal.GetLastWin32Error());
                    }
                    if (pcbData > 0U)
                    {
                        SafeLocalAllocHandle pvData = CAPI.LocalAlloc(64U, new IntPtr((long)pcbData));
                        if (!CAPI.CAPISafe.CertGetCertificateContextProperty(certContext1, 20U, pvData, out pcbData))
                        {
                            throw new CryptographicException(Marshal.GetLastWin32Error());
                        }
                        signerEncodeInfo.SignerId.dwIdChoice         = 2U;
                        signerEncodeInfo.SignerId.Value.KeyId.cbData = pcbData;
                        signerEncodeInfo.SignerId.Value.KeyId.pbData = pvData.DangerousGetHandle();
                        GC.SuppressFinalize((object)pvData);
                    }
                }
                return(signerEncodeInfo);
            }
        }