Beispiel #1
0
    public static void ShowCertificateDialog(X509Chain chain, string title, IntPtr parent)
    {
        const int  certStoreProvMemory     = 2;     // CERT_STORE_PROV_MEMORY
        const int  certCloseStoreCheckFlag = 2;     // CERT_CLOSE_STORE_CHECK_FLAG
        const uint certStoreAddAlways      = 4;     // CERT_STORE_ADD_ALWAYS
        const uint x509AsnEncoding         = 1;     // X509_ASN_ENCODING
        var        storeHandle             = CertOpenStore(certStoreProvMemory, 0, 0, 0, null);

        if (storeHandle == IntPtr.Zero)
        {
            throw new Win32Exception();
        }
        try
        {
            foreach (var element in chain.ChainElements)
            {
                var certificate       = element.Certificate;
                var certificateBytes  = certificate.Export(X509ContentType.Cert);
                var certContextHandle = CertCreateCertificateContext(
                    x509AsnEncoding, certificateBytes, (uint)certificateBytes.Length);
                if (certContextHandle == IntPtr.Zero)
                {
                    throw new Win32Exception();
                }
                CertAddCertificateContextToStore(storeHandle, certContextHandle, certStoreAddAlways, IntPtr.Zero);
            }
            var extraStoreArray       = new[] { storeHandle };
            var extraStoreArrayHandle = GCHandle.Alloc(extraStoreArray, GCHandleType.Pinned);
            try
            {
                var extraStorePointer = extraStoreArrayHandle.AddrOfPinnedObject();
                var viewInfo          = new CRYPTUI_VIEWCERTIFICATE_STRUCT();
                viewInfo.hwndParent   = parent;
                viewInfo.dwSize       = Marshal.SizeOf(viewInfo);
                viewInfo.pCertContext = chain.ChainElements[0].Certificate.Handle;
                viewInfo.szTitle      = title;
                viewInfo.nStartPage   = 0;
                viewInfo.cStores      = 1;
                viewInfo.rghStores    = extraStorePointer;
                var fPropertiesChanged = false;
                CryptUIDlgViewCertificate(ref viewInfo, ref fPropertiesChanged);
            }
            finally
            {
                if (extraStoreArrayHandle.IsAllocated)
                {
                    extraStoreArrayHandle.Free();
                }
            }
        }
        finally
        {
            CertCloseStore(storeHandle, certCloseStoreCheckFlag);
        }
    }
Beispiel #2
0
        public void ViewCertificate()
        {
            var certViewInfo = new CRYPTUI_VIEWCERTIFICATE_STRUCT();

            certViewInfo.dwSize       = Marshal.SizeOf(certViewInfo);
            certViewInfo.pCertContext = _cert.Handle;
            certViewInfo.szTitle      = "Certificate Info";
            certViewInfo.dwFlags      = CRYPTUI_DISABLE_ADDTOSTORE;
            certViewInfo.nStartPage   = 0;
            bool fPropertiesChanged = false;

            if (!CryptUIDlgViewCertificate(ref certViewInfo, ref fPropertiesChanged))
            {
                int error = Marshal.GetLastWin32Error();
                if (error != 1223)
                {
                    MessageBox.Show("Showing the certificate errored with exit code " + error.ToString(),
                                    "ERROR!", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }
Beispiel #3
0
        private void buttonViewCertificate_Click(object sender, EventArgs e)
        {
            try
            {
                // Get the cert
                X509Certificate2 cert = new X509Certificate2(this.Certificate);

                // Show the cert
                CRYPTUI_VIEWCERTIFICATE_STRUCT certViewInfo = new CRYPTUI_VIEWCERTIFICATE_STRUCT();
                certViewInfo.dwSize       = Marshal.SizeOf(certViewInfo);
                certViewInfo.pCertContext = cert.Handle;
                certViewInfo.szTitle      = "Certificate";
                certViewInfo.dwFlags      = CRYPTUI_DISABLE_ADDTOSTORE;
                certViewInfo.nStartPage   = 0;
                certViewInfo.hwndParent   = this.Handle;
                bool fPropertiesChanged = false;
                CryptUIDlgViewCertificate(ref certViewInfo, ref fPropertiesChanged);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                ExceptionDialog.ShowDialog(this, ex);
            }
        }
Beispiel #4
0
 internal static extern bool CryptUIDlgViewCertificate(
     ref CRYPTUI_VIEWCERTIFICATE_STRUCT pCertViewInfo,
     ref bool pfPropertiesChanged
     );
Beispiel #5
0
        static void ShowCertificateChain(this IEnumerable <X509Certificate2> chain, HwndSource hwndSource)
        {
            const int  CERT_STORE_PROV_MEMORY      = 2;
            const int  CERT_CLOSE_STORE_CHECK_FLAG = 2;
            const uint CERT_STORE_ADD_ALWAYS       = 4;
            const uint X509_ASN_ENCODING           = 1;

            var storeHandle = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0, 0, null);

            if (storeHandle == IntPtr.Zero)
            {
                throw new Win32Exception();
            }

            try
            {
                foreach (var cert in chain)
                {
                    var certificate       = cert;
                    var certificateBytes  = certificate.Export(X509ContentType.Cert);
                    var certContextHandle = CertCreateCertificateContext(X509_ASN_ENCODING, certificateBytes, (uint)certificateBytes.Length);

                    if (certContextHandle == IntPtr.Zero)
                    {
                        throw new Win32Exception();
                    }

                    CertAddCertificateContextToStore(storeHandle, certContextHandle, CERT_STORE_ADD_ALWAYS, IntPtr.Zero);
                }

                var extraStoreArray       = new[] { storeHandle };
                var extraStoreArrayHandle = GCHandle.Alloc(extraStoreArray, GCHandleType.Pinned);
                try
                {
                    var extraStorePointer = extraStoreArrayHandle.AddrOfPinnedObject();

                    var viewInfo = new CRYPTUI_VIEWCERTIFICATE_STRUCT
                    {
                        hwndParent   = hwndSource?.Handle ?? IntPtr.Zero,
                        pCertContext = chain.First().Handle,
                        nStartPage   = 0,
                        cStores      = 1,
                        rghStores    = extraStorePointer
                    };
                    viewInfo.dwSize = Marshal.SizeOf(viewInfo);

                    var fPropertiesChanged = false;
                    CryptUIDlgViewCertificate(ref viewInfo, ref fPropertiesChanged);
                }
                finally
                {
                    if (extraStoreArrayHandle.IsAllocated)
                    {
                        extraStoreArrayHandle.Free();
                    }
                }
            }
            finally
            {
                CertCloseStore(storeHandle, CERT_CLOSE_STORE_CHECK_FLAG);
            }
        }