예제 #1
0
        /// <summary>
        /// Imports the PKC s7.
        /// </summary>
        /// <param name="storeName">Name of the store.</param>
        /// <param name="storeLoc">The store loc.</param>
        /// <param name="pkcsBlob">The PKCS BLOB.</param>
        /// <returns></returns>
        /// <exception cref="System.Security.Cryptography.CryptographicException"></exception>
        public static string ImportPKCS7(string storeName, StoreLocation storeLoc, byte[] pkcsBlob)
        {
            var pvPara    = IntPtr.Zero;
            var hMemStore = IntPtr.Zero;

            var cBlob = new Crypt.CRYPT_DATA_BLOB()
            {
                cbData = pkcsBlob.Length, pbData = Marshal.AllocHGlobal(pkcsBlob.Length)
            };

            Marshal.Copy(pkcsBlob, 0, cBlob.pbData, pkcsBlob.Length);
            try
            {
                pvPara = Marshal.AllocHGlobal(Marshal.SizeOf(cBlob));
                Marshal.StructureToPtr(cBlob, pvPara, false);

                var encodingType = Crypt.PKCS_7_ASN_ENCODING | Crypt.X509_ASN_ENCODING;
                hMemStore = Crypt.CertOpenStore(Crypt.CERT_STORE_PROV_PKCS7, encodingType, IntPtr.Zero, 0, pvPara);

                if (hMemStore == IntPtr.Zero)
                {
                    throw new CryptographicException(Kernel.GetFormatMessage(Marshal.GetLastWin32Error()));
                }
                var memStore = new X509Store(hMemStore);

                var certs = new X509Certificate2[memStore.Certificates.Count];
                memStore.Certificates.CopyTo(certs, 0);

                return(ImportCertificates(storeName, storeLoc, certs));
            }
            finally
            {
                if (hMemStore != IntPtr.Zero)
                {
                    Crypt.CertCloseStore(hMemStore, Crypt.CERT_CLOSE_STORE_CHECK_FLAG);
                }

                if (pvPara != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pvPara);
                }

                if (cBlob.pbData != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(cBlob.pbData);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Imports the PFX.
        /// </summary>
        /// <param name="storeName">Name of the store.</param>
        /// <param name="storeLoc">The store loc.</param>
        /// <param name="pfxBlob">The PFX BLOB.</param>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        /// <exception cref="System.Security.Cryptography.CryptographicException">
        /// Invalid PFX format
        /// or
        /// </exception>
        public static string ImportPFX(string storeName, StoreLocation storeLoc, byte[] pfxBlob, string password)
        {
            var cBlob     = new Crypt.CRYPT_DATA_BLOB();
            var hMemStore = IntPtr.Zero;

            try
            {
                cBlob.cbData = pfxBlob.Length;
                cBlob.pbData = Marshal.AllocHGlobal(pfxBlob.Length);
                Marshal.Copy(pfxBlob, 0, cBlob.pbData, pfxBlob.Length);

                if (!Crypt.PFXIsPFXBlob(ref cBlob))
                {
                    throw new CryptographicException("Invalid PFX format");
                }

                hMemStore = Crypt.PFXImportCertStore(ref cBlob, password, Crypt.CRYPT_USER_KEYSET);
                if (hMemStore == IntPtr.Zero)
                {
                    throw new CryptographicException(Kernel.GetFormatMessage(Marshal.GetLastWin32Error()));
                }

                var memStore = new X509Store(hMemStore);
                var certs    = new X509Certificate2[memStore.Certificates.Count];
                memStore.Certificates.CopyTo(certs, 0);

                return(ImportCertificates(storeName, storeLoc, certs));
            }
            finally
            {
                if (hMemStore != IntPtr.Zero)
                {
                    Crypt.CertCloseStore(hMemStore, Crypt.CERT_CLOSE_STORE_CHECK_FLAG);
                }

                if (cBlob.pbData != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(cBlob.pbData);
                }
            }
        }