Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Certificate Information from Web Browser Control

Does anyone know how to open up the "Certificate Information" screen based on the SSL from the WebBrowser control?

like image 499
Kyle Rosendo Avatar asked Apr 22 '10 10:04

Kyle Rosendo


2 Answers

This can be achieved by using a class called X509Certificate2UI.

To make this class avalable to you, you need to add a reference to System.Security.dll

In the X509Certificate2UI class you have a meyhod called DisplayCertificate() which takes an X509Certificate2 object as a parameter. When invoked, this method shows a dialog box displaying all cert information including chaining, exactly the same as the dialog box you will find in IE.

The webbrowser control can only return a X509Certificate which can then be passed into the constructor of the X509Certificate2 class.

So the code looks as such:

//includes on top
using System.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

//Do webrequest to get info on secure site
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://securesite.com");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Close();

//retrieve the ssl cert and assign it to an X509Certificate object
X509Certificate cert = request.ServicePoint.Certificate;

//convert the X509Certificate to an X509Certificate2 object by passing it into the constructor
X509Certificate2 cert2 = new X509Certificate2(cert);

//display the cert dialog box
X509Certificate2UI.DisplayCertificate(cert2);
like image 114
Yo Momma Avatar answered Nov 03 '22 03:11

Yo Momma


If I understand you correct you should search for this information not in WebBrowser but inside of CryptoAPI. Exist such function like CryptUIDlgSelectCertificateFromStore, CryptUIDlgViewContext from Cryptui.dll. There are some functions in WINTRUST.DLL like WinVerifyTrustEx which can also display some dialogs.

Could you exactly describe how I can display dialog what you want in Internet Explore? Do you works already with WebBrowser control, then you can trace, for example, inside of BeforeNavigate2 Event the url which Internet Explorer has. Having this URL you can download SSL certificate an display if with respect of CryptUIDlgViewContext. To download or get the certificate you can use InternetQueryOption with INTERNET_OPTION_SERVER_CERT_CHAIN_CONTEXT or INTERNET_OPTION_CLIENT_CERT_CONTEXT flag. It can be that information from INTERNET_OPTION_SECURITY_CERTIFICATE, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT, (see http://support.microsoft.com/kb/251347) will be enough for you.

like image 42
Oleg Avatar answered Nov 03 '22 04:11

Oleg