void FreeHandle() { if (_handle != IntPtr.Zero) { var closed = Crypt32.CertCloseStore(_handle, Crypt32.CertCloreStoreFlags.NONE); _handle = IntPtr.Zero; Debug.Assert(closed); } Marshal.FreeHGlobal(_blob.pbData); }
public static Pkcs7CertificateStore Create(byte[] data) { const string STORE_TYPE = "PKCS7"; var dataPtr = Marshal.AllocHGlobal(data.Length); Marshal.Copy(data, 0, dataPtr, data.Length); var blob = new Crypt32.CRYPTOAPI_BLOB { cbData = (uint)data.Length, pbData = dataPtr }; var handle = Crypt32.CertOpenStore(STORE_TYPE, Crypt32.CertEncodingType.NONE, IntPtr.Zero, Crypt32.CertOpenStoreFlags.NONE, ref blob); if (handle == IntPtr.Zero) { throw new InvalidOperationException("Failed to create a memory certificate store."); } return(new Pkcs7CertificateStore(handle, blob)); }