internal static unsafe X509Certificate2 CreateDummyCertificate(CspParameters parameters)
        {
            System.Security.Cryptography.SafeCertContextHandle invalidHandle = System.Security.Cryptography.SafeCertContextHandle.InvalidHandle;
            System.Security.Cryptography.SafeCryptProvHandle   hCryptProv    = System.Security.Cryptography.SafeCryptProvHandle.InvalidHandle;
            uint dwFlags = 0;

            if ((parameters.Flags & CspProviderFlags.UseMachineKeyStore) != CspProviderFlags.NoFlags)
            {
                dwFlags |= 0x20;
            }
            if ((parameters.Flags & CspProviderFlags.UseDefaultKeyContainer) != CspProviderFlags.NoFlags)
            {
                dwFlags |= 0xf0000000;
            }
            if ((parameters.Flags & CspProviderFlags.NoPrompt) != CspProviderFlags.NoFlags)
            {
                dwFlags |= 0x40;
            }
            if (!System.Security.Cryptography.CAPI.CryptAcquireContext(ref hCryptProv, parameters.KeyContainerName, parameters.ProviderName, (uint)parameters.ProviderType, dwFlags))
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            System.Security.Cryptography.CAPI.CRYPT_KEY_PROV_INFO structure = new System.Security.Cryptography.CAPI.CRYPT_KEY_PROV_INFO {
                pwszProvName      = parameters.ProviderName,
                pwszContainerName = parameters.KeyContainerName,
                dwProvType        = (uint)parameters.ProviderType,
                dwKeySpec         = (uint)parameters.KeyNumber,
                dwFlags           = ((parameters.Flags & CspProviderFlags.UseMachineKeyStore) == CspProviderFlags.UseMachineKeyStore) ? 0x20 : 0
            };
            System.Security.Cryptography.SafeLocalAllocHandle handle3 = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CRYPT_KEY_PROV_INFO))));
            Marshal.StructureToPtr(structure, handle3.DangerousGetHandle(), false);
            System.Security.Cryptography.CAPI.CRYPT_ALGORITHM_IDENTIFIER crypt_algorithm_identifier = new System.Security.Cryptography.CAPI.CRYPT_ALGORITHM_IDENTIFIER {
                pszObjId = "1.3.14.3.2.29"
            };
            System.Security.Cryptography.SafeLocalAllocHandle handle4 = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CRYPT_ALGORITHM_IDENTIFIER))));
            Marshal.StructureToPtr(crypt_algorithm_identifier, handle4.DangerousGetHandle(), false);
            X500DistinguishedName name = new X500DistinguishedName("cn=CMS Signer Dummy Certificate");

            fixed(byte *numRef = name.RawData)
            {
                System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB cryptoapi_blob = new System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB {
                    cbData = (uint)name.RawData.Length,
                    pbData = new IntPtr((void *)numRef)
                };
                invalidHandle = System.Security.Cryptography.CAPI.CAPIUnsafe.CertCreateSelfSignCertificate(hCryptProv, new IntPtr((void *)&cryptoapi_blob), 1, handle3.DangerousGetHandle(), handle4.DangerousGetHandle(), IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
            }

            Marshal.DestroyStructure(handle3.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CRYPT_KEY_PROV_INFO));
            handle3.Dispose();
            Marshal.DestroyStructure(handle4.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CRYPT_ALGORITHM_IDENTIFIER));
            handle4.Dispose();
            if ((invalidHandle == null) || invalidHandle.IsInvalid)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            X509Certificate2 certificate = new X509Certificate2(invalidHandle.DangerousGetHandle());

            invalidHandle.Dispose();
            return(certificate);
        }
        internal static bool CryptAcquireContext(ref SafeCryptProvHandle hCryptProv, IntPtr pwszContainer, IntPtr pwszProvider, uint dwProvType, uint dwFlags)
        {
            string str = null;

            if (pwszContainer != IntPtr.Zero)
            {
                str = Marshal.PtrToStringUni(pwszContainer);
            }
            string str2 = null;

            if (pwszProvider != IntPtr.Zero)
            {
                str2 = Marshal.PtrToStringUni(pwszProvider);
            }
            return(CryptAcquireContext(ref hCryptProv, str, str2, dwProvType, dwFlags));
        }
示例#3
0
        internal static unsafe int GetMaxKeyLength (SafeCryptProvHandle safeCryptProvHandle, uint algId) {
            uint enumFlag = CAPI.CRYPT_FIRST;
            uint cbPeex = (uint) Marshal.SizeOf(typeof(CAPI.PROV_ENUMALGS_EX));
            SafeLocalAllocHandle pPeex = CAPI.LocalAlloc(CAPI.LPTR, new IntPtr(Marshal.SizeOf(typeof(CAPI.PROV_ENUMALGS_EX))));

            using (pPeex) {
                while (CAPI.CAPISafe.CryptGetProvParam(safeCryptProvHandle, CAPI.PP_ENUMALGS_EX, pPeex.DangerousGetHandle(), new IntPtr(&cbPeex), enumFlag)) {
                    CAPI.PROV_ENUMALGS_EX peex = (CAPI.PROV_ENUMALGS_EX) Marshal.PtrToStructure(pPeex.DangerousGetHandle(), typeof(CAPI.PROV_ENUMALGS_EX));

                    if (peex.aiAlgid == algId)
                        return (int) peex.dwMaxLen;

                    enumFlag = 0;
                }
            }

            throw new CryptographicException(CAPI.CRYPT_E_UNKNOWN_ALGO);
        }
        internal static unsafe int GetMaxKeyLength(System.Security.Cryptography.SafeCryptProvHandle safeCryptProvHandle, uint algId)
        {
            uint dwFlags = 1;
            uint num2    = (uint)Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.PROV_ENUMALGS_EX));

            System.Security.Cryptography.SafeLocalAllocHandle handle = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.PROV_ENUMALGS_EX))));
            using (handle)
            {
                while (System.Security.Cryptography.CAPI.CAPISafe.CryptGetProvParam(safeCryptProvHandle, 0x16, handle.DangerousGetHandle(), new IntPtr((void *)&num2), dwFlags))
                {
                    System.Security.Cryptography.CAPI.PROV_ENUMALGS_EX prov_enumalgs_ex = (System.Security.Cryptography.CAPI.PROV_ENUMALGS_EX)Marshal.PtrToStructure(handle.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.PROV_ENUMALGS_EX));
                    if (prov_enumalgs_ex.aiAlgid == algId)
                    {
                        return((int)prov_enumalgs_ex.dwMaxLen);
                    }
                    dwFlags = 0;
                }
            }
            throw new CryptographicException(-2146889726);
        }
        internal static bool CryptAcquireContext([In, Out] ref SafeCryptProvHandle hCryptProv, [In, MarshalAs(UnmanagedType.LPStr)] string pwszContainer, [In, MarshalAs(UnmanagedType.LPStr)] string pwszProvider, [In] uint dwProvType, [In] uint dwFlags)
        {
            CspParameters parameters = new CspParameters {
                ProviderName     = pwszProvider,
                KeyContainerName = pwszContainer,
                ProviderType     = (int)dwProvType,
                KeyNumber        = -1,
                Flags            = ((dwFlags & 0x20) == 0x20) ? CspProviderFlags.UseMachineKeyStore : CspProviderFlags.NoFlags
            };
            KeyContainerPermission            permission  = new KeyContainerPermission(KeyContainerPermissionFlags.NoFlags);
            KeyContainerPermissionAccessEntry accessEntry = new KeyContainerPermissionAccessEntry(parameters, KeyContainerPermissionFlags.Open);

            permission.AccessEntries.Add(accessEntry);
            permission.Demand();
            bool flag = CAPIUnsafe.CryptAcquireContext(ref hCryptProv, pwszContainer, pwszProvider, dwProvType, dwFlags);

            if (!flag && (Marshal.GetLastWin32Error() == -2146893802))
            {
                flag = CAPIUnsafe.CryptAcquireContext(ref hCryptProv, pwszContainer, pwszProvider, dwProvType, dwFlags | 8);
            }
            return(flag);
        }
示例#6
0
        bool CryptAcquireContext(
            ref SafeCryptProvHandle  hCryptProv,
            IntPtr                   pwszContainer,
            IntPtr                   pwszProvider,
            uint                     dwProvType,
            uint                     dwFlags) {

            string container = null;
            if (pwszContainer != IntPtr.Zero) { 
                container = Marshal.PtrToStringUni(pwszContainer);
            }

            string provider = null;
            if (pwszProvider != IntPtr.Zero) { 
                provider = Marshal.PtrToStringUni(pwszProvider);
            }

            return CryptAcquireContext(ref hCryptProv,
                                       container,
                                       provider,
                                       dwProvType,
                                       dwFlags);
        }
示例#7
0
 internal static extern bool CryptAcquireCertificatePrivateKey([In] SafeCertContextHandle pCert, [In] uint dwFlags, [In] IntPtr pvReserved, [In, Out] ref SafeCryptProvHandle phCryptProv, [In, Out] ref uint pdwKeySpec, [In, Out] ref bool pfCallerFreeProv);
示例#8
0
 internal static extern bool CryptGetProvParam([In] SafeCryptProvHandle hProv, [In] uint dwParam, [In] IntPtr pbData, [In] IntPtr pdwDataLen, [In] uint dwFlags);
        internal static unsafe System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO CreateSignerEncodeInfo(CmsSigner signer, bool silent)
        {
            System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO cmsg_signer_encode_info = new System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO)));
            System.Security.Cryptography.SafeCryptProvHandle          invalidHandle           = System.Security.Cryptography.SafeCryptProvHandle.InvalidHandle;
            uint pdwKeySpec       = 0;
            bool pfCallerFreeProv = false;

            cmsg_signer_encode_info.HashAlgorithm.pszObjId = signer.DigestAlgorithm.Value;
            if (string.Compare(signer.Certificate.PublicKey.Oid.Value, "1.2.840.10040.4.1", StringComparison.Ordinal) == 0)
            {
                cmsg_signer_encode_info.HashEncryptionAlgorithm.pszObjId = "1.2.840.10040.4.3";
            }
            cmsg_signer_encode_info.cAuthAttr    = (uint)signer.SignedAttributes.Count;
            cmsg_signer_encode_info.rgAuthAttr   = CreateCryptAttributes(signer.SignedAttributes);
            cmsg_signer_encode_info.cUnauthAttr  = (uint)signer.UnsignedAttributes.Count;
            cmsg_signer_encode_info.rgUnauthAttr = CreateCryptAttributes(signer.UnsignedAttributes);
            if (signer.SignerIdentifierType == SubjectIdentifierType.NoSignature)
            {
                cmsg_signer_encode_info.HashEncryptionAlgorithm.pszObjId = "1.3.6.1.5.5.7.6.2";
                cmsg_signer_encode_info.pCertInfo = IntPtr.Zero;
                cmsg_signer_encode_info.dwKeySpec = pdwKeySpec;
                if (!System.Security.Cryptography.CAPI.CryptAcquireContext(ref invalidHandle, (string)null, (string)null, 1, 0xf0000000))
                {
                    throw new CryptographicException(Marshal.GetLastWin32Error());
                }
                cmsg_signer_encode_info.hCryptProv = invalidHandle.DangerousGetHandle();
                GC.SuppressFinalize(invalidHandle);
                cmsg_signer_encode_info.SignerId.dwIdChoice = 1;
                X500DistinguishedName name = new X500DistinguishedName("CN=Dummy Signer")
                {
                    Oid = new Oid("1.3.6.1.4.1.311.21.9")
                };
                cmsg_signer_encode_info.SignerId.Value.IssuerSerialNumber.Issuer.cbData = (uint)name.RawData.Length;
                System.Security.Cryptography.SafeLocalAllocHandle handle2 = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr((long)cmsg_signer_encode_info.SignerId.Value.IssuerSerialNumber.Issuer.cbData));
                Marshal.Copy(name.RawData, 0, handle2.DangerousGetHandle(), name.RawData.Length);
                cmsg_signer_encode_info.SignerId.Value.IssuerSerialNumber.Issuer.pbData = handle2.DangerousGetHandle();
                GC.SuppressFinalize(handle2);
                cmsg_signer_encode_info.SignerId.Value.IssuerSerialNumber.SerialNumber.cbData = 1;
                System.Security.Cryptography.SafeLocalAllocHandle handle3 = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr((long)cmsg_signer_encode_info.SignerId.Value.IssuerSerialNumber.SerialNumber.cbData));
                byte *handle = (byte *)handle3.DangerousGetHandle();
                handle[0] = 0;
                cmsg_signer_encode_info.SignerId.Value.IssuerSerialNumber.SerialNumber.pbData = handle3.DangerousGetHandle();
                GC.SuppressFinalize(handle3);
                return(cmsg_signer_encode_info);
            }
            System.Security.Cryptography.SafeCertContextHandle certContext = System.Security.Cryptography.X509Certificates.X509Utils.GetCertContext(signer.Certificate);
            if (!System.Security.Cryptography.CAPI.CAPISafe.CryptAcquireCertificatePrivateKey(certContext, silent ? 70 : 6, IntPtr.Zero, ref invalidHandle, ref pdwKeySpec, ref pfCallerFreeProv))
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            cmsg_signer_encode_info.dwKeySpec  = pdwKeySpec;
            cmsg_signer_encode_info.hCryptProv = invalidHandle.DangerousGetHandle();
            GC.SuppressFinalize(invalidHandle);
            System.Security.Cryptography.CAPI.CERT_CONTEXT cert_context = *((System.Security.Cryptography.CAPI.CERT_CONTEXT *)certContext.DangerousGetHandle());
            cmsg_signer_encode_info.pCertInfo = cert_context.pCertInfo;
            if (signer.SignerIdentifierType == SubjectIdentifierType.SubjectKeyIdentifier)
            {
                uint pcbData = 0;
                System.Security.Cryptography.SafeLocalAllocHandle pvData = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle;
                if (!System.Security.Cryptography.CAPI.CAPISafe.CertGetCertificateContextProperty(certContext, 20, pvData, ref pcbData))
                {
                    throw new CryptographicException(Marshal.GetLastWin32Error());
                }
                if (pcbData <= 0)
                {
                    return(cmsg_signer_encode_info);
                }
                pvData = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr((long)pcbData));
                if (!System.Security.Cryptography.CAPI.CAPISafe.CertGetCertificateContextProperty(certContext, 20, pvData, ref pcbData))
                {
                    throw new CryptographicException(Marshal.GetLastWin32Error());
                }
                cmsg_signer_encode_info.SignerId.dwIdChoice         = 2;
                cmsg_signer_encode_info.SignerId.Value.KeyId.cbData = pcbData;
                cmsg_signer_encode_info.SignerId.Value.KeyId.pbData = pvData.DangerousGetHandle();
                GC.SuppressFinalize(pvData);
            }
            return(cmsg_signer_encode_info);
        }
 internal static bool CryptAcquireContext(ref SafeCryptProvHandle hCryptProv, IntPtr pwszContainer, IntPtr pwszProvider, uint dwProvType, uint dwFlags)
 {
     string str = null;
     if (pwszContainer != IntPtr.Zero)
     {
         str = Marshal.PtrToStringUni(pwszContainer);
     }
     string str2 = null;
     if (pwszProvider != IntPtr.Zero)
     {
         str2 = Marshal.PtrToStringUni(pwszProvider);
     }
     return CryptAcquireContext(ref hCryptProv, str, str2, dwProvType, dwFlags);
 }
示例#11
0
 protected internal static extern bool CryptAcquireContext([In, Out] ref SafeCryptProvHandle hCryptProv, [In, MarshalAs(UnmanagedType.LPStr)] string pszContainer, [In, MarshalAs(UnmanagedType.LPStr)] string pszProvider, [In] uint dwProvType, [In] uint dwFlags);
示例#12
0
 protected internal static extern SafeCertContextHandle CertCreateSelfSignCertificate([In] SafeCryptProvHandle hProv, [In] IntPtr pSubjectIssuerBlob, [In] uint dwFlags, [In] IntPtr pKeyProvInfo, [In] IntPtr pSignatureAlgorithm, [In] IntPtr pStartTime, [In] IntPtr pEndTime, [In] IntPtr pExtensions);