gpt4 book ai didi

c# - 我如何确定与网络服务器的连接是否使用完美前向保密?

转载 作者:太空狗 更新时间:2023-10-29 21:27:27 25 4
gpt4 key购买 nike

我有一个连接到网络服务器并显示 SSL 证书到期日期的 C# 程序。

我想知道的是如何确定连接是否使用完美前向保密 [PFS]?

using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ServicePointManager.ServerCertificateValidationCallback += ServerCertificateValidationCallback;
ServicePointManager.CheckCertificateRevocationList = true;

var request = WebRequest.Create("https://www.microsoft.com/");

var response = request.GetResponse();

Console.WriteLine("Done.");
Console.ReadLine();
}
private static bool ServerCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
Console.WriteLine("Certificate expires on " + certificate.GetExpirationDateString());

return true;
}
}
}

最佳答案

前言:我不是密码学家。

根据 this Information Security答,你要看约定的密码组,即组中的 key 交换片。据此,任何基于 Diffie-Hellman 的算法都可以提供完美的前向保密性。正如埃里克森在评论中指出的那样,这可能是不真实的,您需要了解假设存在完美前向保密而实际上并不存在的安全复杂性。

有了它,您正在寻找 SslStream .这将使您能够访问所需的 key 交换属性。

它不像使用 WebRequest 甚至 HttpRequest 那样容易。你将不得不自己写出连接。一个例子看起来像:

string host = "www.microsoft.com";

using (var client = new TcpClient(host, 443))
using (var stream = client.GetStream())
using (var sslStream = new SslStream(stream))
{
sslStream.AuthenticateAsClient(host);

// figure out if sslStream.KeyExchangeAlgorithm support PFS here
}

理论上,KeyExchangeAlgorithm 是一个枚举。您可以执行 if(sslStream.KeyExchangeAlgorithm == ExchangeAlgorithmType.DiffieHellman),您就会知道答案[1]。但是根据this Microsoft Forum post , ExchangeAlgorithmType 可能是 44550 等同于椭圆曲线 Diffie-Hellman。椭圆曲线 Diffie-Hellman 确实支持完美前向保密。

如果您想修改当前代码以在一个连接中实现所有这些,可在 sslStream.RemoteCertificate 中获取远程证书,这样您就可以获得证书到期日期。

[1] 可能并非所有 Diffie-Hellman 交换都支持完美前向保密。同样,请考虑这样做的安全问题。

关于c# - 我如何确定与网络服务器的连接是否使用完美前向保密?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27741449/

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