I want to save all certificates from a URL to disk. So for example https://www.google.de
If I browse this page with Firefox, I can see three certificates.
With Firefox I can export them all, and save them to disk.
So I want to do that in C#. I started getting the certificates with the following code..
/// <summary>
/// Get and write certificate from URL into file in path
/// </summary>
/// <param name="_URL">URL of website with certficate</param>
/// <param name="_path">Path where you want to store certificate</param>
private static void SaveCertificate(String _URL, String _path)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_URL);
request.AllowAutoRedirect = false;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Close();
X509Certificate2 cert = new X509Certificate2(request.ServicePoint.Certificate);
File.WriteAllText(_path, ExportToPEM(cert));
}
catch (Exception)
{
}
}
/// <summary>
/// Export a certificate to a PEM format string
/// </summary>
/// <param name="_cert">The certificate to export</param>
/// <returns>A PEM encoded string</returns>
public static string ExportToPEM(X509Certificate2 _cert)
{
StringBuilder builder = new StringBuilder();
try
{
builder.AppendLine("-----BEGIN CERTIFICATE-----");
builder.AppendLine(Convert.ToBase64String(_cert.Export(X509ContentType.Cert), Base64FormattingOptions.InsertLineBreaks));
builder.AppendLine("-----END CERTIFICATE-----");
}
catch (Exception)
{
}
return builder.ToString();
}
The problem with this code is, I just get one certificate exept three. (the certificate I get is the marked certificate in the screenshot from firefox)
I also tryed the solution from here SO Question but it didnt work for me.
My question is, how can I get all certificates from a URL?