internal static unsafe X509Certificate2Collection GetCertificates(System.Security.Cryptography.SafeCryptMsgHandle safeCryptMsgHandle)
        {
            uint num  = 0;
            uint num2 = (uint)Marshal.SizeOf(typeof(uint));
            X509Certificate2Collection certificates = new X509Certificate2Collection();

            if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, 11, 0, new IntPtr((void *)&num), new IntPtr((void *)&num2)))
            {
                checkErr(Marshal.GetLastWin32Error());
            }
            for (uint i = 0; i < num; i++)
            {
                uint cbData = 0;
                System.Security.Cryptography.SafeLocalAllocHandle invalidHandle = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle;
                GetParam(safeCryptMsgHandle, 12, i, out invalidHandle, out cbData);
                if (cbData > 0)
                {
                    System.Security.Cryptography.SafeCertContextHandle handle2 = System.Security.Cryptography.CAPI.CAPISafe.CertCreateCertificateContext(0x10001, invalidHandle, cbData);
                    if ((handle2 == null) || handle2.IsInvalid)
                    {
                        throw new CryptographicException(Marshal.GetLastWin32Error());
                    }
                    certificates.Add(new X509Certificate2(handle2.DangerousGetHandle()));
                    handle2.Dispose();
                }
            }
            return(certificates);
        }
        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);
        }
Beispiel #3
0
        internal static unsafe X509Certificate2Collection GetCertificates(SafeCryptMsgHandle safeCryptMsgHandle)
        {
            uint num1 = 0U;
            uint num2 = (uint)Marshal.SizeOf(typeof(uint));
            X509Certificate2Collection certificate2Collection = new X509Certificate2Collection();

            if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, 11U, 0U, new IntPtr((void *)&num1), new IntPtr((void *)&num2)))
            {
                PkcsUtils.checkErr(Marshal.GetLastWin32Error());
            }
            for (uint index = 0U; index < num1; ++index)
            {
                uint cbData = 0U;
                SafeLocalAllocHandle pvData = SafeLocalAllocHandle.InvalidHandle;
                PkcsUtils.GetParam(safeCryptMsgHandle, 12U, index, out pvData, out cbData);
                if (cbData > 0U)
                {
                    System.Security.Cryptography.SafeCertContextHandle certificateContext = CAPI.CAPISafe.CertCreateCertificateContext(65537U, pvData, cbData);
                    if (certificateContext == null || certificateContext.IsInvalid)
                    {
                        throw new CryptographicException(Marshal.GetLastWin32Error());
                    }
                    certificate2Collection.Add(new X509Certificate2(certificateContext.DangerousGetHandle()));
                    certificateContext.Dispose();
                }
            }
            return(certificate2Collection);
        }
Beispiel #4
0
        private static void FindByCert(System.Security.Cryptography.SafeCertStoreHandle safeSourceStoreHandle, uint dwFindType, IntPtr pvFindPara, bool validOnly, FindProcDelegate pfnCertCallback1, FindProcDelegate pfnCertCallback2, object pvCallbackData1, object pvCallbackData2, System.Security.Cryptography.SafeCertStoreHandle safeTargetStoreHandle)
        {
            int hr = 0;

            System.Security.Cryptography.SafeCertContextHandle invalidHandle = System.Security.Cryptography.SafeCertContextHandle.InvalidHandle;
            invalidHandle = CAPI.CertFindCertificateInStore(safeSourceStoreHandle, 0x10001, 0, dwFindType, pvFindPara, invalidHandle);
            while ((invalidHandle != null) && !invalidHandle.IsInvalid)
            {
                if (pfnCertCallback1 != null)
                {
                    hr = pfnCertCallback1(invalidHandle, pvCallbackData1);
                    if (hr == 1)
                    {
                        if (pfnCertCallback2 != null)
                        {
                            hr = pfnCertCallback2(invalidHandle, pvCallbackData2);
                        }
                        if (hr == 1)
                        {
                            goto Label_008D;
                        }
                    }
                    if (hr != 0)
                    {
                        break;
                    }
                }
                if (validOnly)
                {
                    hr = System.Security.Cryptography.X509Certificates.X509Utils.VerifyCertificate(invalidHandle, null, null, X509RevocationMode.NoCheck, X509RevocationFlag.ExcludeRoot, DateTime.Now, new TimeSpan(0, 0, 0), null, new IntPtr(1L), IntPtr.Zero);
                    if (hr == 1)
                    {
                        goto Label_008D;
                    }
                    if (hr != 0)
                    {
                        break;
                    }
                }
                if (!CAPI.CertAddCertificateLinkToStore(safeTargetStoreHandle, invalidHandle, 4, System.Security.Cryptography.SafeCertContextHandle.InvalidHandle))
                {
                    hr = Marshal.GetHRForLastWin32Error();
                    break;
                }
Label_008D:
                GC.SuppressFinalize(invalidHandle);
                invalidHandle = CAPI.CertFindCertificateInStore(safeSourceStoreHandle, 0x10001, 0, dwFindType, pvFindPara, invalidHandle);
            }
            if ((invalidHandle != null) && !invalidHandle.IsInvalid)
            {
                invalidHandle.Dispose();
            }
            if ((hr != 1) && (hr != 0))
            {
                throw new CryptographicException(hr);
            }
        }
Beispiel #5
0
        private static unsafe byte[] ExportCertificatesToBlob(System.Security.Cryptography.SafeCertStoreHandle safeCertStoreHandle, X509ContentType contentType, string password)
        {
            System.Security.Cryptography.SafeCertContextHandle invalidHandle = System.Security.Cryptography.SafeCertContextHandle.InvalidHandle;
            uint dwSaveAs = 2;

            byte[] destination = null;
            CAPIBase.CRYPTOAPI_BLOB cryptoapi_blob = new CAPIBase.CRYPTOAPI_BLOB();
            SafeLocalAllocHandle    pbElement      = SafeLocalAllocHandle.InvalidHandle;

            switch (contentType)
            {
            case X509ContentType.Cert:
                invalidHandle = CAPI.CertEnumCertificatesInStore(safeCertStoreHandle, invalidHandle);
                if ((invalidHandle != null) && !invalidHandle.IsInvalid)
                {
                    CAPIBase.CERT_CONTEXT cert_context = *((CAPIBase.CERT_CONTEXT *)invalidHandle.DangerousGetHandle());
                    destination = new byte[cert_context.cbCertEncoded];
                    Marshal.Copy(cert_context.pbCertEncoded, destination, 0, destination.Length);
                }
                break;

            case X509ContentType.SerializedCert:
            {
                invalidHandle = CAPI.CertEnumCertificatesInStore(safeCertStoreHandle, invalidHandle);
                uint num2 = 0;
                if ((invalidHandle != null) && !invalidHandle.IsInvalid)
                {
                    if (!CAPISafe.CertSerializeCertificateStoreElement(invalidHandle, 0, pbElement, new IntPtr((void *)&num2)))
                    {
                        throw new CryptographicException(Marshal.GetLastWin32Error());
                    }
                    pbElement = CAPI.LocalAlloc(0, new IntPtr((long)num2));
                    if (!CAPISafe.CertSerializeCertificateStoreElement(invalidHandle, 0, pbElement, new IntPtr((void *)&num2)))
                    {
                        throw new CryptographicException(Marshal.GetLastWin32Error());
                    }
                    destination = new byte[num2];
                    Marshal.Copy(pbElement.DangerousGetHandle(), destination, 0, destination.Length);
                    break;
                }
                break;
            }

            case X509ContentType.Pfx:
                if (!CAPI.PFXExportCertStore(safeCertStoreHandle, new IntPtr((void *)&cryptoapi_blob), password, 6))
                {
                    throw new CryptographicException(Marshal.GetLastWin32Error());
                }
                cryptoapi_blob.pbData = CAPI.LocalAlloc(0, new IntPtr((long)cryptoapi_blob.cbData)).DangerousGetHandle();
                if (!CAPI.PFXExportCertStore(safeCertStoreHandle, new IntPtr((void *)&cryptoapi_blob), password, 6))
                {
                    throw new CryptographicException(Marshal.GetLastWin32Error());
                }
                destination = new byte[cryptoapi_blob.cbData];
                Marshal.Copy(cryptoapi_blob.pbData, destination, 0, destination.Length);
                break;

            case X509ContentType.SerializedStore:
            case X509ContentType.Pkcs7:
                if (contentType == X509ContentType.SerializedStore)
                {
                    dwSaveAs = 1;
                }
                if (!CAPI.CertSaveStore(safeCertStoreHandle, 0x10001, dwSaveAs, 2, new IntPtr((void *)&cryptoapi_blob), 0))
                {
                    throw new CryptographicException(Marshal.GetLastWin32Error());
                }
                pbElement             = CAPI.LocalAlloc(0, new IntPtr((long)cryptoapi_blob.cbData));
                cryptoapi_blob.pbData = pbElement.DangerousGetHandle();
                if (!CAPI.CertSaveStore(safeCertStoreHandle, 0x10001, dwSaveAs, 2, new IntPtr((void *)&cryptoapi_blob), 0))
                {
                    throw new CryptographicException(Marshal.GetLastWin32Error());
                }
                destination = new byte[cryptoapi_blob.cbData];
                Marshal.Copy(cryptoapi_blob.pbData, destination, 0, destination.Length);
                break;

            default:
                throw new CryptographicException(SR.GetString("Cryptography_X509_InvalidContentType"));
            }
            pbElement.Dispose();
            invalidHandle.Dispose();
            return(destination);
        }