gpt4 book ai didi

C#从一个网站下载所有的https证书

转载 作者:太空宇宙 更新时间:2023-11-03 13:14:52 25 4
gpt4 key购买 nike

我想将所有证书从 URL 保存到磁盘。例如 https://www.google.de

如果我用 Firefox 浏览这个页面,我可以看到三个证书。 enter image description here

使用 Firefox,我可以将它们全部导出,并将它们保存到磁盘。

所以我想用 C# 来做。我开始使用以下代码获取证书..

    /// <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();
}

这段代码的问题是,我只得到一个证书,除了三个。 (我得到的证书是firefox截图中标记的证书)

我也尝试了这里的解决方案 SO Question但它对我不起作用。

我的问题是,如何从一个 URL 获取所有证书?

最佳答案

获取证书链的一种方法是在您的请求上实现ServerCertificateValidationCallback

这是一个接受参数的函数:

(object sender, X509Certificate 证书, X509Chain链, SslPolicyErrors sslPolicyErrors)

这通常用于验证证书和链,但在本例中我们只是使用它来访问证书链。它确实感觉很像 hack,但它应该可以工作。我已经根据您发布的代码实现了概念验证。代码将证书输出到控制台窗口。

   public static void Main(string[] args)
{
SaveCertificate("https://www.google.de", "");
}
/// <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)
{

var request = (HttpWebRequest)WebRequest.Create(url);
request.AllowAutoRedirect = false;
request.ServerCertificateValidationCallback = ServerCertificateValidationCallback;

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Close();
Console.ReadLine();

}

private static bool ServerCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
foreach (var cer in chain.ChainElements)
{
Console.WriteLine(cer.Certificate.FriendlyName);
Console.WriteLine(ExportToPem(cer.Certificate));
}

return true;
}

/// <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();
}

关于C#从一个网站下载所有的https证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46999494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com