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 GetPrivateKeyInfo(System.Security.Cryptography.SafeCertContextHandle safeCertContext, ref CspParameters parameters) { System.Security.Cryptography.SafeLocalAllocHandle invalidHandle = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle; uint pcbData = 0; if (!System.Security.Cryptography.CAPI.CAPISafe.CertGetCertificateContextProperty(safeCertContext, 2, invalidHandle, ref pcbData)) { if (Marshal.GetLastWin32Error() != -2146885628) { throw new CryptographicException(Marshal.GetLastWin32Error()); } return(false); } invalidHandle = System.Security.Cryptography.CAPI.LocalAlloc(0, new IntPtr((long)pcbData)); if (!System.Security.Cryptography.CAPI.CAPISafe.CertGetCertificateContextProperty(safeCertContext, 2, invalidHandle, ref pcbData)) { if (Marshal.GetLastWin32Error() != -2146885628) { throw new CryptographicException(Marshal.GetLastWin32Error()); } return(false); } System.Security.Cryptography.CAPI.CRYPT_KEY_PROV_INFO crypt_key_prov_info = (System.Security.Cryptography.CAPI.CRYPT_KEY_PROV_INFO)Marshal.PtrToStructure(invalidHandle.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CRYPT_KEY_PROV_INFO)); parameters.ProviderName = crypt_key_prov_info.pwszProvName; parameters.KeyContainerName = crypt_key_prov_info.pwszContainerName; parameters.ProviderType = (int)crypt_key_prov_info.dwProvType; parameters.KeyNumber = (int)crypt_key_prov_info.dwKeySpec; parameters.Flags = ((crypt_key_prov_info.dwFlags & 0x20) == 0x20) ? CspProviderFlags.UseMachineKeyStore : CspProviderFlags.NoFlags; invalidHandle.Dispose(); return(true); }
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; }