gpt4 book ai didi

c# - 为什么不支持在同一进程中使用相同的证书调用 SslStream.AuthenticateAsServer 和 SslStream.AuthenticateAsClient?

转载 作者:行者123 更新时间:2023-12-04 22:41:42 25 4
gpt4 key购买 nike

我最近在将套接字通信转换为使用 System.Net.Security.SslStream 而不是 NetworkStream 时遇到了一个问题。此转换针对 .Net Framework 4.8 项目的解决方案。此任务的要求决定了双向 TLS 身份验证。这项工作以平静的方式进行,直到我在一个包含服务器和客户端的项目中转换了代码。此时,行为是这样的:发生的第一个相互验证的连接(无论是客户端还是服务器)都会成功,而第二个总是失败。需要指出的是,每个流程都有自己的证书。在第二次连接尝试期间抛出的异常是:

System.Security.Authentication.AuthenticationException: A call to SSPI failed, see inner exception. ---> System.ComponentModel.Win32Exception: The client and server cannot communicate, because they do not possess a common algorithm
经过一些实验后,我发现如果我每个进程有两个唯一的证书,并将一个用于所有 SslStream.AuthenticateAsServer 调用,另一个用于所有 SslStream.AuthenticateAsClient 调用,我不会有任何问题。尽管找到了解决此问题的方法,但我仍然不明白为什么将单个证书用于服务器和客户端连接是一个问题。我在网上找不到任何解释。
我创建了一个解决方案,用很少的代码演示了这个问题。该解决方案包含一个带有 TlsSample 类的类库项目,以及两个尝试相互连接的控制台应用程序(AServer 和 BServer)。
Tls 示例:
using System;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;

namespace TlsSampleShared
{
public class TlsSample
{
private X509Certificate2Collection certs;
private int _listenerPort;
private int _clientPort;
private string _certificateName;
private TimeSpan _connectionDelay;

public TlsSample(int listenerPort, int clientPort, string certificateName, TimeSpan connectionDelay)
{
_listenerPort = listenerPort;
_clientPort = clientPort;
_certificateName = certificateName;
_connectionDelay = connectionDelay;
}

public async Task ConnectAsync()
{
using (var store = new X509Store(StoreName.My, StoreLocation.LocalMachine))
{
store.Open(OpenFlags.ReadOnly);
certs = store.Certificates.Find(X509FindType.FindBySubjectName, _certificateName, true);
Console.WriteLine($"Using certificate {certs[0].Subject} with thumbnail {certs[0].Thumbprint}.");
}

var listenTask = Task.Run(Listen);

await Task.Delay(_connectionDelay);
var aClient = new TcpClient();
aClient.Connect(IPAddress.Loopback, _clientPort);
var s = new SslStream(aClient.GetStream());
Authenticate(aClient, s, false);

await listenTask;
}

private async Task Listen()
{
var l = new TcpListener(new IPEndPoint(IPAddress.Any, _listenerPort));
l.Start();
var c = await l.AcceptTcpClientAsync();
var s = new SslStream(c.GetStream());
Authenticate(c, s, true);
}

private void Authenticate(TcpClient c, SslStream s, bool server)
{
try
{
if (server)
s.AuthenticateAsServer(certs[0], true, false);
else
s.AuthenticateAsClient(Dns.GetHostName(), certs, false);

Console.WriteLine($@"TLS {(server ? "server" : "client")} handshake succeeded between ({c.Client.LocalEndPoint}-{c.Client.RemoteEndPoint}):
Authenticated: {s.IsAuthenticated}
Mutually Authenticated: {s.IsMutuallyAuthenticated}
Server: {s.IsServer}
Encrypted: {s.IsEncrypted}
Signed: {s.IsSigned}
Key Exchange Algorithm: {s.KeyExchangeAlgorithm}
Key Exchange Strength: {s.KeyExchangeStrength}
Cipher Algorithm: {s.CipherAlgorithm}
Cipher Strength: {s.CipherStrength}
Hash Algorithm: {s.HashAlgorithm}
Hash Strength: {s.HashStrength}
Ssl Protocol: {s.SslProtocol}");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred while authenticating ({c.Client.LocalEndPoint}-{c.Client.RemoteEndPoint}) as a {(server ? "server" : "client")}{Environment.NewLine}{ex}");
}
}
}
}
AServer/Program.cs:
using System;
using System.Threading.Tasks;
using TlsSampleShared;

namespace AServer
{
class Program
{
static async Task Main() => await new TlsSample(7000, 7001, "serverA.VM-MIL-SM", TimeSpan.FromSeconds(1)).ConnectAsync();
}
}
BServer/Program.cs:
using System;
using System.Threading.Tasks;
using TlsSampleShared;

namespace BServer
{
class Program
{
static async Task Main() => await new TlsSample(7001, 7000, "serverB.VM-MIL-SM", TimeSpan.FromSeconds(2)).ConnectAsync();
}
}
要进行实验,您需要在证书存储中拥有两个证书。任何关于为什么使用单个证书的客户端和服务器身份验证在同一过程中失败的任何见解都将不胜感激。
在玩弄我的示例时,我注意到的另一条信息是,当 AServer 和 BServer 几乎同时尝试相互连接时,我得到了一个不同的异常。这可以通过为 AServer 和 BServer 设置相同的连接延迟来看到。在这种情况下,两个连接都不成功,抛出的异常是:
System.Security.Authentication.AuthenticationException: A call to SSPI failed, see inner exception. ---> System.ComponentModel.Win32Exception: The Local Security Authority cannot be contacted

最佳答案

Microsoft 确认此行为是一个错误(至少在 .Net Framework 4.8 中)。 .Net 5 中的 SslStream 类中不存在该错误。

关于c# - 为什么不支持在同一进程中使用相同的证书调用 SslStream.AuthenticateAsServer 和 SslStream.AuthenticateAsClient?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71373717/

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