/// <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); } } }