internal static IntPtr CertEnumCertificatesInStore([In] System.Security.Cryptography.SafeCertStoreHandle hCertStore, [In] IntPtr pPrevCertContext)
        {
            if (hCertStore == null)
            {
                throw new ArgumentNullException("hCertStore");
            }
            if (hCertStore.IsInvalid)
            {
                throw new CryptographicException(SR.GetString("Cryptography_InvalidHandle"), "hCertStore");
            }
            if (pPrevCertContext == IntPtr.Zero)
            {
                new StorePermission(StorePermissionFlags.EnumerateCertificates).Demand();
            }
            IntPtr pCertContext = CAPIUnsafe.CertEnumCertificatesInStore(hCertStore, pPrevCertContext);

            if (pCertContext == IntPtr.Zero)
            {
                int hr = Marshal.GetLastWin32Error();
                if (hr != -2146885628)
                {
                    CAPISafe.CertFreeCertificateContext(pCertContext);
                    throw new CryptographicException(hr);
                }
            }
            return(pCertContext);
        }
 internal static bool PFXExportCertStore([In] System.Security.Cryptography.SafeCertStoreHandle hCertStore, [In, Out] IntPtr pPFX, [In] string szPassword, [In] uint dwFlags)
 {
     if (hCertStore == null)
     {
         throw new ArgumentNullException("hCertStore");
     }
     if (hCertStore.IsInvalid)
     {
         throw new CryptographicException(SR.GetString("Cryptography_InvalidHandle"), "hCertStore");
     }
     new KeyContainerPermission(KeyContainerPermissionFlags.Export | KeyContainerPermissionFlags.Open).Demand();
     return(CAPIUnsafe.PFXExportCertStore(hCertStore, pPFX, szPassword, dwFlags));
 }
        internal static unsafe System.Security.Cryptography.SafeCertStoreHandle PFXImportCertStore([In] uint dwObjectType, [In] object pvObject, [In] string szPassword, [In] uint dwFlags, [In] bool persistKeyContainers)
        {
            if (pvObject == null)
            {
                throw new ArgumentNullException("pvObject");
            }
            byte[] buffer = null;
            if (dwObjectType == 1)
            {
                buffer = File.ReadAllBytes((string)pvObject);
            }
            else
            {
                buffer = (byte[])pvObject;
            }
            if (persistKeyContainers)
            {
                new KeyContainerPermission(KeyContainerPermissionFlags.Create).Demand();
            }
            System.Security.Cryptography.SafeCertStoreHandle invalidHandle = System.Security.Cryptography.SafeCertStoreHandle.InvalidHandle;
            GCHandle handle2 = GCHandle.Alloc(buffer, GCHandleType.Pinned);
            IntPtr   ptr     = handle2.AddrOfPinnedObject();

            try
            {
                CAPIBase.CRYPTOAPI_BLOB cryptoapi_blob;
                cryptoapi_blob.cbData = (uint)buffer.Length;
                cryptoapi_blob.pbData = ptr;
                invalidHandle         = CAPIUnsafe.PFXImportCertStore(new IntPtr((void *)&cryptoapi_blob), szPassword, dwFlags);
            }
            finally
            {
                if (handle2.IsAllocated)
                {
                    handle2.Free();
                }
            }
            if (!invalidHandle.IsInvalid && !persistKeyContainers)
            {
                for (IntPtr ptr2 = CertEnumCertificatesInStore(invalidHandle, IntPtr.Zero); ptr2 != IntPtr.Zero; ptr2 = CertEnumCertificatesInStore(invalidHandle, ptr2))
                {
                    CAPIBase.CRYPTOAPI_BLOB cryptoapi_blob2 = new CAPIBase.CRYPTOAPI_BLOB();
                    if (!CertSetCertificateContextProperty(ptr2, 0x65, 0x40000000, new IntPtr((void *)&cryptoapi_blob2)))
                    {
                        throw new CryptographicException(Marshal.GetLastWin32Error());
                    }
                }
            }
            return(invalidHandle);
        }
 internal static bool CertSaveStore([In] System.Security.Cryptography.SafeCertStoreHandle hCertStore, [In] uint dwMsgAndCertEncodingType, [In] uint dwSaveAs, [In] uint dwSaveTo, [In, Out] IntPtr pvSaveToPara, [In] uint dwFlags)
 {
     if (hCertStore == null)
     {
         throw new ArgumentNullException("hCertStore");
     }
     if (hCertStore.IsInvalid)
     {
         throw new CryptographicException(SR.GetString("Cryptography_InvalidHandle"), "hCertStore");
     }
     new StorePermission(StorePermissionFlags.EnumerateCertificates).Demand();
     if ((dwSaveTo == 3) || (dwSaveTo == 4))
     {
         throw new ArgumentException(SR.GetString("Security_InvalidValue"), "pvSaveToPara");
     }
     return(CAPIUnsafe.CertSaveStore(hCertStore, dwMsgAndCertEncodingType, dwSaveAs, dwSaveTo, pvSaveToPara, dwFlags));
 }
 internal static bool CertAddCertificateLinkToStore([In] System.Security.Cryptography.SafeCertStoreHandle hCertStore, [In] System.Security.Cryptography.SafeCertContextHandle pCertContext, [In] uint dwAddDisposition, [In, Out] System.Security.Cryptography.SafeCertContextHandle ppStoreContext)
 {
     if (hCertStore == null)
     {
         throw new ArgumentNullException("hCertStore");
     }
     if (hCertStore.IsInvalid)
     {
         throw new CryptographicException(SR.GetString("Cryptography_InvalidHandle"), "hCertStore");
     }
     if (pCertContext == null)
     {
         throw new ArgumentNullException("pCertContext");
     }
     if (pCertContext.IsInvalid)
     {
         throw new CryptographicException(SR.GetString("Cryptography_InvalidHandle"), "pCertContext");
     }
     new StorePermission(StorePermissionFlags.AddToStore).Demand();
     return(CAPIUnsafe.CertAddCertificateLinkToStore(hCertStore, pCertContext, dwAddDisposition, ppStoreContext));
 }
        internal static unsafe bool CryptQueryObject([In] uint dwObjectType, [In] object pvObject, [In] uint dwExpectedContentTypeFlags, [In] uint dwExpectedFormatTypeFlags, [In] uint dwFlags, [Out] IntPtr pdwMsgAndCertEncodingType, [Out] IntPtr pdwContentType, [Out] IntPtr pdwFormatType, [In, Out] ref System.Security.Cryptography.SafeCertStoreHandle phCertStore, [In, Out] IntPtr phMsg, [In, Out] IntPtr ppvContext)
        {
            bool     flag   = false;
            GCHandle handle = GCHandle.Alloc(pvObject, GCHandleType.Pinned);
            IntPtr   ptr    = handle.AddrOfPinnedObject();

            try
            {
                if (pvObject == null)
                {
                    throw new ArgumentNullException("pvObject");
                }
                if (dwObjectType == 1)
                {
                    string fullPath = Path.GetFullPath((string)pvObject);
                    new FileIOPermission(FileIOPermissionAccess.Read, fullPath).Demand();
                }
                else
                {
                    CAPIBase.CRYPTOAPI_BLOB cryptoapi_blob;
                    cryptoapi_blob.cbData = (uint)((byte[])pvObject).Length;
                    cryptoapi_blob.pbData = ptr;
                    ptr = new IntPtr((void *)&cryptoapi_blob);
                }
                flag = CAPIUnsafe.CryptQueryObject(dwObjectType, ptr, dwExpectedContentTypeFlags, dwExpectedFormatTypeFlags, dwFlags, pdwMsgAndCertEncodingType, pdwContentType, pdwFormatType, ref phCertStore, phMsg, ppvContext);
            }
            finally
            {
                if (handle.IsAllocated)
                {
                    handle.Free();
                }
            }
            return(flag);
        }
 internal static System.Security.Cryptography.SafeCertContextHandle CertFindCertificateInStore([In] System.Security.Cryptography.SafeCertStoreHandle hCertStore, [In] uint dwCertEncodingType, [In] uint dwFindFlags, [In] uint dwFindType, [In] IntPtr pvFindPara, [In] System.Security.Cryptography.SafeCertContextHandle pPrevCertContext)
 {
     if (hCertStore == null)
     {
         throw new ArgumentNullException("hCertStore");
     }
     if (hCertStore.IsInvalid)
     {
         throw new CryptographicException(SR.GetString("Cryptography_InvalidHandle"), "hCertStore");
     }
     return(CAPIUnsafe.CertFindCertificateInStore(hCertStore, dwCertEncodingType, dwFindFlags, dwFindType, pvFindPara, pPrevCertContext));
 }
 internal static System.Security.Cryptography.SafeCertContextHandle CertEnumCertificatesInStore([In] System.Security.Cryptography.SafeCertStoreHandle hCertStore, [In] System.Security.Cryptography.SafeCertContextHandle pPrevCertContext)
 {
     if (hCertStore == null)
     {
         throw new ArgumentNullException("hCertStore");
     }
     if (hCertStore.IsInvalid)
     {
         throw new CryptographicException(SR.GetString("Cryptography_InvalidHandle"), "hCertStore");
     }
     if (pPrevCertContext.IsInvalid)
     {
         new StorePermission(StorePermissionFlags.EnumerateCertificates).Demand();
     }
     System.Security.Cryptography.SafeCertContextHandle handle = CAPIUnsafe.CertEnumCertificatesInStore(hCertStore, pPrevCertContext);
     if (((handle == null) || handle.IsInvalid) && (Marshal.GetLastWin32Error() != -2146885628))
     {
         throw new CryptographicException(Marshal.GetLastWin32Error());
     }
     return(handle);
 }