Example #1
0
		public static X509Certificate2Collection SelectFromCollection (X509Certificate2Collection certificates, 
			string title, string message, X509SelectionFlag selectionFlag, IntPtr hwndParent)
		{
			if (certificates == null)
				throw new ArgumentNullException ("certificates");
			if ((selectionFlag < X509SelectionFlag.SingleSelection) || (selectionFlag > X509SelectionFlag.MultiSelection))
				throw new ArgumentException ("selectionFlag");

			throw new NotImplementedException ();
		}
 private static X509Certificate2Collection SelectFromCollectionHelper(X509Certificate2Collection certificates, string title, string message, X509SelectionFlag selectionFlag, IntPtr hwndParent)
 {
     X509Certificate2Collection certificates2;
     if (certificates == null)
     {
         throw new ArgumentNullException("certificates");
     }
     if ((selectionFlag < X509SelectionFlag.SingleSelection) || (selectionFlag > X509SelectionFlag.MultiSelection))
     {
         throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, SecurityResources.GetResourceString("Arg_EnumIllegalVal"), new object[] { "selectionFlag" }));
     }
     new StorePermission(StorePermissionFlags.AllFlags).Assert();
     using (System.Security.Cryptography.SafeCertStoreHandle handle = System.Security.Cryptography.X509Certificates.X509Utils.ExportToMemoryStore(certificates))
     {
         using (System.Security.Cryptography.SafeCertStoreHandle handle2 = SelectFromStore(handle, title, message, selectionFlag, hwndParent))
         {
             certificates2 = System.Security.Cryptography.X509Certificates.X509Utils.GetCertificates(handle2);
         }
     }
     return certificates2;
 }
Example #3
0
        private static unsafe SafeCertStoreHandle SelectFromStore(SafeCertStoreHandle safeSourceStoreHandle, string?title, string?message, X509SelectionFlag selectionFlags, IntPtr hwndParent)
        {
            int dwErrorCode = ERROR_SUCCESS;

            SafeCertStoreHandle safeCertStoreHandle = Interop.Crypt32.CertOpenStore(
                (IntPtr)Interop.Crypt32.CERT_STORE_PROV_MEMORY,
                Interop.Crypt32.X509_ASN_ENCODING | Interop.Crypt32.PKCS_7_ASN_ENCODING,
                IntPtr.Zero,
                0,
                IntPtr.Zero);

            if (safeCertStoreHandle == null || safeCertStoreHandle.IsInvalid)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }

            Interop.CryptUI.CRYPTUI_SELECTCERTIFICATE_STRUCTW csc = default;
            // Older versions of CRYPTUI do not check the size correctly,
            // so always force it to the oldest version of the structure.
#if NET7_0_OR_GREATER
            // Declare a local for Native to enable us to get the managed byte offset
            // without having a null check cause a failure.
            Interop.CryptUI.CRYPTUI_SELECTCERTIFICATE_STRUCTW.Marshaller.Native native;
            Unsafe.SkipInit(out native);
            csc.dwSize = (uint)Unsafe.ByteOffset(ref Unsafe.As <Interop.CryptUI.CRYPTUI_SELECTCERTIFICATE_STRUCTW.Marshaller.Native, byte>(ref native), ref Unsafe.As <IntPtr, byte>(ref native.hSelectedCertStore));
#else
            csc.dwSize = (uint)Marshal.OffsetOf(typeof(Interop.CryptUI.CRYPTUI_SELECTCERTIFICATE_STRUCTW), "hSelectedCertStore");
#endif
            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 = Interop.CryptUI.CryptUIDlgSelectCertificateW(ref csc);

            if (safeCertContextHandle != null && !safeCertContextHandle.IsInvalid)
            {
                // Single select, so add it to our hCertStore
                SafeCertContextHandle ppStoreContext = SafeCertContextHandle.InvalidHandle;
                if (!Interop.Crypt32.CertAddCertificateLinkToStore(safeCertStoreHandle,
                                                                   safeCertContextHandle,
                                                                   Interop.Crypt32.CERT_STORE_ADD_ALWAYS,
                                                                   ppStoreContext))
                {
                    dwErrorCode = Marshal.GetLastWin32Error();
                }
            }

            if (dwErrorCode != ERROR_SUCCESS)
            {
                throw new CryptographicException(dwErrorCode);
            }

            return(safeCertStoreHandle);
        }
Example #4
0
 public static X509Certificate2Collection SelectFromCollection(X509Certificate2Collection certificates, string title, string message, X509SelectionFlag selectionFlag)
 {
 }
Example #5
0
        private static X509Certificate2Collection SelectFromCollectionHelper (X509Certificate2Collection certificates, string title, string message, X509SelectionFlag selectionFlag, IntPtr hwndParent) {
            if (certificates == null)
                throw new ArgumentNullException("certificates");
            if (selectionFlag < X509SelectionFlag.SingleSelection || selectionFlag > X509SelectionFlag.MultiSelection)
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, SecurityResources.GetResourceString("Arg_EnumIllegalVal"), "selectionFlag"));

            //
            // We need to Assert all StorePermission flags since this is a memory store and we want 
            // semi-trusted code to be able to select certificates from a memory store.
            //

            StorePermission sp = new StorePermission(StorePermissionFlags.AllFlags);
            sp.Assert();

            using (SafeCertStoreHandle safeSourceStoreHandle = X509Utils.ExportToMemoryStore(certificates))
            using (SafeCertStoreHandle safeTargetStoreHandle = SelectFromStore(safeSourceStoreHandle, title, message, selectionFlag, hwndParent))
            {
                return X509Utils.GetCertificates(safeTargetStoreHandle);
            }
        }
 public static X509Certificate2Collection SelectFromCollection(X509Certificate2Collection certificates, string title, string message, X509SelectionFlag selectionFlag, System.IntPtr hwndParent)
 {
 }
                public static IEnumerable <X509Certificate2> ShowSelectionUIFromValidCertificates(X509SelectionFlag selectionModel = X509SelectionFlag.SingleSelection)
                {
                    var uiMessage = selectionModel == X509SelectionFlag.SingleSelection
                        ? DbMessages.Certificate_ShowSelectionUIFromValidCertificates_Selecione_o_certificado_que_deseja_utilizar
                        : DbMessages.Certificate_ShowSelectionUIFromValidCertificates_Selecione_os_certificados_que_deseja_utilizar;

                    var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);

                    store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);

                    try
                    {
                        var certificates = store.Certificates.Find(X509FindType.FindByTimeValid, DateTime.Now, true);
                        var selection    = X509Certificate2UI.SelectFromCollection(
                            certificates, DbMessages.Certificate_ShowSelectionUIFromValidCertificates_Certificados_vĂ¡lidos_, uiMessage, selectionModel);

                        return(selection.Cast <X509Certificate2>().ToArray());
                    }
                    finally
                    {
                        store.Close();
                    }
                }
Example #8
0
        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;
        }
Example #9
0
        private static X509Certificate2Collection SelectFromCollectionHelper(X509Certificate2Collection certificates, string title, string message, X509SelectionFlag selectionFlag, IntPtr hwndParent)
        {
            X509Certificate2Collection certificates2;

            if (certificates == null)
            {
                throw new ArgumentNullException("certificates");
            }
            if ((selectionFlag < X509SelectionFlag.SingleSelection) || (selectionFlag > X509SelectionFlag.MultiSelection))
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, SecurityResources.GetResourceString("Arg_EnumIllegalVal"), new object[] { "selectionFlag" }));
            }
            new StorePermission(StorePermissionFlags.AllFlags).Assert();
            using (System.Security.Cryptography.SafeCertStoreHandle handle = System.Security.Cryptography.X509Certificates.X509Utils.ExportToMemoryStore(certificates))
            {
                using (System.Security.Cryptography.SafeCertStoreHandle handle2 = SelectFromStore(handle, title, message, selectionFlag, hwndParent))
                {
                    certificates2 = System.Security.Cryptography.X509Certificates.X509Utils.GetCertificates(handle2);
                }
            }
            return(certificates2);
        }
Example #10
0
        public static ICollection <X509Certificate2> SelectCertificates(IEnumerable <X509Certificate2> certificates, string title = null, string message = null, X509SelectionFlag selectionFlag = X509SelectionFlag.SingleSelection, IntPtr?owner = null)
        {
            if (certificates == null)
            {
                return(new List <X509Certificate2>());
            }

            var certificateCollection = new X509Certificate2Collection(certificates.ToArray());

            // Fenster
            var selectedCertificates = X509Certificate2UI.SelectFromCollection(certificateCollection, title, message, selectionFlag, owner ?? IntPtr.Zero);

            var result = selectedCertificates.OfType <X509Certificate2>().ToList();

            if (selectionFlag == X509SelectionFlag.SingleSelection)
            {
                return(result.Count == 1 ? result : new List <X509Certificate2>());
            }

            return(result);
        }
Example #11
0
        private static unsafe System.Security.Cryptography.SafeCertStoreHandle SelectFromStore(System.Security.Cryptography.SafeCertStoreHandle safeSourceStoreHandle, string title, string message, X509SelectionFlag selectionFlags, IntPtr hwndParent)
        {
            int num = 0;

            System.Security.Cryptography.SafeCertStoreHandle hCertStore = System.Security.Cryptography.CAPI.CertOpenStore((IntPtr)2L, 0x10001, IntPtr.Zero, 0, null);
            if ((hCertStore == null) || hCertStore.IsInvalid)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            System.Security.Cryptography.CAPI.CRYPTUI_SELECTCERTIFICATE_STRUCTW csc = new System.Security.Cryptography.CAPI.CRYPTUI_SELECTCERTIFICATE_STRUCTW {
                dwSize           = (uint)((int)Marshal.OffsetOf(typeof(System.Security.Cryptography.CAPI.CRYPTUI_SELECTCERTIFICATE_STRUCTW), "hSelectedCertStore")),
                hwndParent       = hwndParent,
                dwFlags          = (uint)selectionFlags,
                szTitle          = title,
                dwDontUseColumn  = 0,
                szDisplayString  = message,
                pFilterCallback  = IntPtr.Zero,
                pDisplayCallback = IntPtr.Zero,
                pvCallbackData   = IntPtr.Zero,
                cDisplayStores   = 1
            };
            IntPtr handle = safeSourceStoreHandle.DangerousGetHandle();

            csc.rghDisplayStores   = new IntPtr((void *)&handle);
            csc.cStores            = 0;
            csc.rghStores          = IntPtr.Zero;
            csc.cPropSheetPages    = 0;
            csc.rgPropSheetPages   = IntPtr.Zero;
            csc.hSelectedCertStore = hCertStore.DangerousGetHandle();
            System.Security.Cryptography.SafeCertContextHandle pCertContext = System.Security.Cryptography.CAPI.CryptUIDlgSelectCertificateW(csc);
            if ((pCertContext != null) && !pCertContext.IsInvalid)
            {
                System.Security.Cryptography.SafeCertContextHandle invalidHandle = System.Security.Cryptography.SafeCertContextHandle.InvalidHandle;
                if (!System.Security.Cryptography.CAPI.CertAddCertificateContextToStore(hCertStore, pCertContext, 7, invalidHandle))
                {
                    num = Marshal.GetLastWin32Error();
                }
            }
            if (num != 0)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            return(hCertStore);
        }
		// note: UI
		public X509Certificate2Collection Select (string title, string message, X509SelectionFlag selectionFlag, IntPtr hwndParent) 
		{
			return null;
		}
		// note: UI
		public X509Certificate2Collection Select (string title, string message, X509SelectionFlag selectionFlag)
		{
			return null;
		}
Example #14
0
 public static X509Certificate2Collection SelectFromCollection(X509Certificate2Collection certificates, string?title, string?message, X509SelectionFlag selectionFlag, IntPtr hwndParent)
 {
     return(SelectFromCollectionHelper(certificates, title, message, selectionFlag, hwndParent));
 }
Example #15
0
        private static X509Certificate2Collection SelectFromCollectionHelper(X509Certificate2Collection certificates, string title, string message, X509SelectionFlag selectionFlag, IntPtr hwndParent)
        {
            if (certificates == null)
            {
                throw new ArgumentNullException("certificates");
            }
            if (selectionFlag < X509SelectionFlag.SingleSelection || selectionFlag > X509SelectionFlag.MultiSelection)
            {
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, SecurityResources.GetResourceString("Arg_EnumIllegalVal"), "selectionFlag"));
            }

            //
            // We need to Assert all StorePermission flags since this is a memory store and we want
            // semi-trusted code to be able to select certificates from a memory store.
            //

            StorePermission sp = new StorePermission(StorePermissionFlags.AllFlags);

            sp.Assert();

            using (SafeCertStoreHandle safeSourceStoreHandle = X509Utils.ExportToMemoryStore(certificates))
                using (SafeCertStoreHandle safeTargetStoreHandle = SelectFromStore(safeSourceStoreHandle, title, message, selectionFlag, hwndParent))
                {
                    return(X509Utils.GetCertificates(safeTargetStoreHandle));
                }
        }
Example #16
0
        private static X509Certificate2Collection SelectFromCollectionHelper(X509Certificate2Collection certificates, string?title, string?message, X509SelectionFlag selectionFlag, IntPtr hwndParent)
        {
            ArgumentNullException.ThrowIfNull(certificates);

            if (selectionFlag < X509SelectionFlag.SingleSelection || selectionFlag > X509SelectionFlag.MultiSelection)
            {
                throw new ArgumentException(SR.Format(SR.Enum_InvalidValue, nameof(selectionFlag)));
            }

            using (SafeCertStoreHandle safeSourceStoreHandle = X509Utils.ExportToMemoryStore(certificates))
                using (SafeCertStoreHandle safeTargetStoreHandle = SelectFromStore(safeSourceStoreHandle, title, message, selectionFlag, hwndParent))
                {
                    return(X509Utils.GetCertificates(safeTargetStoreHandle));
                }
        }
Example #17
0
 public static X509Certificate2Collection SelectFromCollection(X509Certificate2Collection certificates,
                                                               string title, string message, X509SelectionFlag selectionFlag)
 {
     // note: the LinkDemand won't interfere (by design) as this caller is trusted (correct behaviour)
     return(SelectFromCollection(certificates, title, message, selectionFlag, IntPtr.Zero));
 }
Example #18
0
 public static X509Certificate2Collection SelectFromCollection (X509Certificate2Collection certificates, string title, string message, X509SelectionFlag selectionFlag, IntPtr hwndParent) {
     return SelectFromCollectionHelper(certificates, title, message, selectionFlag, hwndParent);
 }
Example #19
0
        public static X509Certificate2Collection SelectFromCollection(X509Certificate2Collection certificates,
                                                                      string title, string message, X509SelectionFlag selectionFlag, IntPtr hwndParent)
        {
            if (certificates == null)
            {
                throw new ArgumentNullException("certificates");
            }
            if ((selectionFlag < X509SelectionFlag.SingleSelection) || (selectionFlag > X509SelectionFlag.MultiSelection))
            {
                throw new ArgumentException("selectionFlag");
            }

            throw new NotImplementedException();
        }
 public static X509Certificate2Collection SelectFromCollection(X509Certificate2Collection certificates, string title, string message, X509SelectionFlag selectionFlag)
 {
 }
Example #21
0
        private static unsafe SafeCertStoreHandle SelectFromStore(SafeCertStoreHandle safeSourceStoreHandle, string title, string message, X509SelectionFlag selectionFlags, IntPtr hwndParent)
        {
            int dwErrorCode = ERROR_SUCCESS;

            SafeCertStoreHandle safeCertStoreHandle = Interop.Crypt32.CertOpenStore(
                (IntPtr)Interop.Crypt32.CERT_STORE_PROV_MEMORY,
                Interop.Crypt32.X509_ASN_ENCODING | Interop.Crypt32.PKCS_7_ASN_ENCODING,
                IntPtr.Zero,
                0,
                null);

            if (safeCertStoreHandle == null || safeCertStoreHandle.IsInvalid)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }

            Interop.CryptUI.CRYPTUI_SELECTCERTIFICATE_STRUCTW csc = new Interop.CryptUI.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(Interop.CryptUI.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 = Interop.CryptUI.CryptUIDlgSelectCertificateW(csc);

            if (safeCertContextHandle != null && !safeCertContextHandle.IsInvalid)
            {
                // Single select, so add it to our hCertStore
                SafeCertContextHandle ppStoreContext = SafeCertContextHandle.InvalidHandle;
                if (!Interop.Crypt32.CertAddCertificateLinkToStore(safeCertStoreHandle,
                                                                   safeCertContextHandle,
                                                                   Interop.Crypt32.CERT_STORE_ADD_ALWAYS,
                                                                   ppStoreContext))
                {
                    dwErrorCode = Marshal.GetLastWin32Error();
                }
            }

            if (dwErrorCode != ERROR_SUCCESS)
            {
                throw new CryptographicException(dwErrorCode);
            }

            return(safeCertStoreHandle);
        }
Example #22
0
		public static X509Certificate2Collection SelectFromCollection (X509Certificate2Collection certificates, 
			string title, string message, X509SelectionFlag selectionFlag)
		{
			// note: the LinkDemand won't interfere (by design) as this caller is trusted (correct behaviour)
			return SelectFromCollection (certificates, title, message, selectionFlag, IntPtr.Zero);
		}
Example #23
0
 public static X509Certificate2Collection SelectFromCollection(X509Certificate2Collection certificates, string title, string message, X509SelectionFlag selectionFlag)
 {
     return(SelectFromCollectionHelper(certificates, title, message, selectionFlag, IntPtr.Zero));
 }
Example #24
0
        public static X509Certificate2Collection SelectStoreCertificate(StoreName storeName, StoreLocation storeLocation, string message, string title, X509SelectionFlag X509SelectionFlag = X509SelectionFlag.SingleSelection)
        {
            //Get certificate store
            var store = new X509Store(storeName, storeLocation);
            store.Open(OpenFlags.ReadOnly);

            var certCollection = store.Certificates.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
            var selection = X509Certificate2UI.SelectFromCollection(certCollection, title, message, X509SelectionFlag);
            
            store.Close();

            if (selection.Count > 0)
            {
                return selection;
            }

            return null;
        }
Example #25
0
 public static X509Certificates.X509Certificate2Collection SelectFromCollection(X509Certificates.X509Certificate2Collection certificates, string title, string message, X509SelectionFlag selectionFlag, IntPtr hwndParent)
 {
     throw null;
 }
Example #26
0
 public static X509Certificate2Collection SelectFromCollection(X509Certificate2Collection certificates, string title, string message, X509SelectionFlag selectionFlag, System.IntPtr hwndParent)
 {
 }
 private static unsafe System.Security.Cryptography.SafeCertStoreHandle SelectFromStore(System.Security.Cryptography.SafeCertStoreHandle safeSourceStoreHandle, string title, string message, X509SelectionFlag selectionFlags, IntPtr hwndParent)
 {
     int num = 0;
     System.Security.Cryptography.SafeCertStoreHandle hCertStore = System.Security.Cryptography.CAPI.CertOpenStore((IntPtr) 2L, 0x10001, IntPtr.Zero, 0, null);
     if ((hCertStore == null) || hCertStore.IsInvalid)
     {
         throw new CryptographicException(Marshal.GetLastWin32Error());
     }
     System.Security.Cryptography.CAPI.CRYPTUI_SELECTCERTIFICATE_STRUCTW csc = new System.Security.Cryptography.CAPI.CRYPTUI_SELECTCERTIFICATE_STRUCTW {
         dwSize = (uint) ((int) Marshal.OffsetOf(typeof(System.Security.Cryptography.CAPI.CRYPTUI_SELECTCERTIFICATE_STRUCTW), "hSelectedCertStore")),
         hwndParent = hwndParent,
         dwFlags = (uint) selectionFlags,
         szTitle = title,
         dwDontUseColumn = 0,
         szDisplayString = message,
         pFilterCallback = IntPtr.Zero,
         pDisplayCallback = IntPtr.Zero,
         pvCallbackData = IntPtr.Zero,
         cDisplayStores = 1
     };
     IntPtr handle = safeSourceStoreHandle.DangerousGetHandle();
     csc.rghDisplayStores = new IntPtr((void*) &handle);
     csc.cStores = 0;
     csc.rghStores = IntPtr.Zero;
     csc.cPropSheetPages = 0;
     csc.rgPropSheetPages = IntPtr.Zero;
     csc.hSelectedCertStore = hCertStore.DangerousGetHandle();
     System.Security.Cryptography.SafeCertContextHandle pCertContext = System.Security.Cryptography.CAPI.CryptUIDlgSelectCertificateW(csc);
     if ((pCertContext != null) && !pCertContext.IsInvalid)
     {
         System.Security.Cryptography.SafeCertContextHandle invalidHandle = System.Security.Cryptography.SafeCertContextHandle.InvalidHandle;
         if (!System.Security.Cryptography.CAPI.CertAddCertificateContextToStore(hCertStore, pCertContext, 7, invalidHandle))
         {
             num = Marshal.GetLastWin32Error();
         }
     }
     if (num != 0)
     {
         throw new CryptographicException(Marshal.GetLastWin32Error());
     }
     return hCertStore;
 }