Пример #1
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);
        }
 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;
 }