SslStream.AuthenticateAsServer挂起

使用VS 10和.NET Framework 4.0,我编写了三个c#unit testing来检查不同服务器/客户端协议设置的SslStream行为。

private void InternalTestSsl(SslProtocols serverProtocols, SslProtocols clientProtocols) { X509Certificate2 certificate = RetrieveServerCertificate(); var server = new TcpListener(new IPEndPoint(IPAddress.Any, 20000)); server.Start(); try { // create and execute a task for server operations var serverTask = new Task(() => { TcpClient connectionToClient = server.AcceptTcpClient(); var sslStream = new SslStream(connectionToClient.GetStream(), false, (a, b, c, d) => true, (a, b, c, d, e) => certificate); sslStream.AuthenticateAsServer(certificate, false, serverProtocols, true); Assert.IsTrue(sslStream.IsAuthenticated); }); serverTask.Start(); // create and execute a task for client operations var clientTask = new Task(() => { var clientConnection = new TcpClient(); clientConnection.Connect(new IPEndPoint(IPAddress.Loopback, 20000)); var sslStream = new SslStream(clientConnection.GetStream(), false, (a, b, c, d) => true, (a, b, c, d, e) => null); sslStream.AuthenticateAsClient(Environment.MachineName, null, clientProtocols, true); Assert.IsTrue(sslStream.IsAuthenticated); }); clientTask.Start(); // wait for both server and client task to finish, check results if (!serverTask.Wait(TimeSpan.FromSeconds(1))) { throw new Exception("Server task did not end in time."); } if (!clientTask.Wait(TimeSpan.FromSeconds(1))) { throw new Exception("Client task did not end in time."); } } finally { server.Stop(); } } [TestMethod] public void TestTlsTls() { InternalTestSsl(SslProtocols.Tls, SslProtocols.Tls); } [TestMethod] public void TestTlsSsl3() { InternalTestSsl(SslProtocols.Tls, SslProtocols.Ssl3); } [TestMethod] public void TestSsl3Tls() { InternalTestSsl(SslProtocols.Ssl3, SslProtocols.Tls); } 

当服务器和客户端定义要使用的相同协议(仅TLS)时,TestTlsTls通过。 TestTlsSsl3的失败也是完全可以理解的(抛出AuthenticationException是因为服务器需要使用TLS,但客户端只想玩SSL3)。

我希望测试“TestSsl3Tls”失败并使用相同的AuthenticationException,而是我的自定义exception“服务器任务没有及时结束”。 被解雇。 在调试时,我看到只有客户端任务收到AuthenticationException,而serverTask仍然在AuthenticateAsServer的调用中。 交换我的Wait命令会导致相反的情况:“TestSsl3Tls”的AuthenticationException,“客户端没有及时结束”的“TestTlsSsl3”。

这是正常的行为,AuthenticationException只被抛出一边,另一边等待(可能是新的身份validation尝试还是断开连接)?

我不知道你的问题的答案是否这种行为是正常的,但在我看来这是一个错误,我最近有同样的问题,因为我无法控制客户端安全协议,我的服务器一直冻结。

我通过Disposing sslstream解决了它,它在AuthenticateAsServer中导致exception,从而允许我的服务器继续。

这是代码片段

  // wait for both server and client task to finish, check results if (!serverTask.Wait(TimeSpan.FromSeconds(1))) { sslStream.Dispose(); // this line will cause an exception in the server task (AuthenticateAsServer), which allows the server to continue ... } 

既然你的问题导致了我的冻结问题的根源,我会分享我的解决方案……