private static unsafe SafeCertStoreHandle SelectFromStore(SafeCertStoreHandle safeSourceStoreHandle, string title, string message, X509SelectionFlag selectionFlags, IntPtr hwndParent) { int dwErrorCode = CAPI.ERROR_SUCCESS; // First, create a memory store SafeCertStoreHandle safeCertStoreHandle = CAPI.CertOpenStore((IntPtr)CAPI.CERT_STORE_PROV_MEMORY, CAPI.X509_ASN_ENCODING | CAPI.PKCS_7_ASN_ENCODING, IntPtr.Zero, 0, null); if (safeCertStoreHandle == null || safeCertStoreHandle.IsInvalid) { throw new CryptographicException(Marshal.GetLastWin32Error()); } CAPI.CRYPTUI_SELECTCERTIFICATE_STRUCTW csc = new CAPI.CRYPTUI_SELECTCERTIFICATE_STRUCTW(); // Older versions of CRYPTUI do not check the size correctly, // so always force it to the oldest version of the structure. csc.dwSize = (uint)Marshal.OffsetOf(typeof(CAPI.CRYPTUI_SELECTCERTIFICATE_STRUCTW), "hSelectedCertStore"); csc.hwndParent = hwndParent; csc.dwFlags = (uint)selectionFlags; csc.szTitle = title; csc.dwDontUseColumn = 0; csc.szDisplayString = message; csc.pFilterCallback = IntPtr.Zero; csc.pDisplayCallback = IntPtr.Zero; csc.pvCallbackData = IntPtr.Zero; csc.cDisplayStores = 1; IntPtr hSourceCertStore = safeSourceStoreHandle.DangerousGetHandle(); csc.rghDisplayStores = new IntPtr(&hSourceCertStore); csc.cStores = 0; csc.rghStores = IntPtr.Zero; csc.cPropSheetPages = 0; csc.rgPropSheetPages = IntPtr.Zero; csc.hSelectedCertStore = safeCertStoreHandle.DangerousGetHandle(); SafeCertContextHandle safeCertContextHandle = CAPI.CryptUIDlgSelectCertificateW(csc); if (safeCertContextHandle != null && !safeCertContextHandle.IsInvalid) { // Single select, so add it to our hCertStore SafeCertContextHandle ppStoreContext = SafeCertContextHandle.InvalidHandle; if (!CAPI.CertAddCertificateContextToStore(safeCertStoreHandle, safeCertContextHandle, CAPI.CERT_STORE_ADD_NEWER_INHERIT_PROPERTIES, ppStoreContext)) { dwErrorCode = Marshal.GetLastWin32Error(); } } if (dwErrorCode != CAPI.ERROR_SUCCESS) { throw new CryptographicException(Marshal.GetLastWin32Error()); } return(safeCertStoreHandle); }
public void Add(X509Certificate2 certificate) { if (certificate == null) { throw new ArgumentNullException("certificate"); } if (((this.m_safeCertStoreHandle == null) || this.m_safeCertStoreHandle.IsInvalid) || this.m_safeCertStoreHandle.IsClosed) { throw new CryptographicException(SR.GetString("Cryptography_X509_StoreNotOpen")); } if (!CAPI.CertAddCertificateContextToStore(this.m_safeCertStoreHandle, certificate.CertContext, 5, System.Security.Cryptography.SafeCertContextHandle.InvalidHandle)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } }
public void Add(X509Certificate2 certificate) { if (certificate == null) { throw new ArgumentNullException("certificate"); } if (m_safeCertStoreHandle == null || m_safeCertStoreHandle.IsInvalid || m_safeCertStoreHandle.IsClosed) { throw new CryptographicException(SR.GetString(SR.Cryptography_X509_StoreNotOpen)); } if (!CAPI.CertAddCertificateContextToStore(m_safeCertStoreHandle, certificate.CertContext, CAPI.CERT_STORE_ADD_REPLACE_EXISTING_INHERIT_PROPERTIES, Cryptography.SafeCertContextHandle.InvalidHandle)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } }