From f44cc766af490387fafc568a566a137e2418ceed Mon Sep 17 00:00:00 2001 From: meum Date: Fri, 5 Oct 2018 11:08:32 +0200 Subject: [PATCH 1/3] Make AuthenticateAsServer async --- uhttpsharp/Clients/ClientSslDecoerator.cs | 16 ++++++++++++++-- uhttpsharp/HttpClient.cs | 16 +++++++++++++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/uhttpsharp/Clients/ClientSslDecoerator.cs b/uhttpsharp/Clients/ClientSslDecoerator.cs index 116682b..7f3962a 100644 --- a/uhttpsharp/Clients/ClientSslDecoerator.cs +++ b/uhttpsharp/Clients/ClientSslDecoerator.cs @@ -1,21 +1,33 @@ -using System.IO; +using System; +using System.IO; using System.Net; using System.Net.Security; using System.Security.Authentication; using System.Security.Cryptography.X509Certificates; +using System.Threading.Tasks; namespace uhttpsharp.Clients { public class ClientSslDecorator : IClient { private readonly IClient _child; + private readonly X509Certificate _certificate; private readonly SslStream _sslStream; public ClientSslDecorator(IClient child, X509Certificate certificate) { _child = child; + _certificate = certificate; _sslStream = new SslStream(_child.Stream); - _sslStream.AuthenticateAsServer(certificate, false, SslProtocols.Tls, true); + } + + public async Task AuthenticateAsServer() + { + Task timeout = Task.Delay(TimeSpan.FromSeconds(10)); + if (timeout == await Task.WhenAny(_sslStream.AuthenticateAsServerAsync(_certificate, false, SslProtocols.Tls, true), timeout).ConfigureAwait(false)) + { + throw new TimeoutException("SSL Authentication Timeout"); + } } public Stream Stream diff --git a/uhttpsharp/HttpClient.cs b/uhttpsharp/HttpClient.cs index 120a436..8b9b358 100644 --- a/uhttpsharp/HttpClient.cs +++ b/uhttpsharp/HttpClient.cs @@ -44,7 +44,7 @@ internal sealed class HttpClientHandler private readonly IHttpRequestProvider _requestProvider; private readonly EndPoint _remoteEndPoint; private DateTime _lastOperationTime; - private readonly Stream _stream; + private Stream _stream; public HttpClientHandler(IClient client, Func requestHandler, IHttpRequestProvider requestProvider) { @@ -52,8 +52,6 @@ public HttpClientHandler(IClient client, Func requestHandler _client = client; _requestHandler = requestHandler; _requestProvider = requestProvider; - - _stream = new BufferedStream(_client.Stream, 8096); Logger.InfoFormat("Got Client {0}", _remoteEndPoint); @@ -62,10 +60,22 @@ public HttpClientHandler(IClient client, Func requestHandler UpdateLastOperationTime(); } + private async Task InitializeStream() + { + if (Client is ClientSslDecorator) + { + await ((ClientSslDecorator)Client).AuthenticateAsServer(); + } + + _stream = new BufferedStream(_client.Stream, 8096); + } + private async void Process() { try { + await InitializeStream(); + while (_client.Connected) { // TODO : Configuration. From 819b1b422fe1b8d5e6ba885a49c266fd0f5dfa4f Mon Sep 17 00:00:00 2001 From: meum Date: Fri, 5 Oct 2018 16:17:44 +0200 Subject: [PATCH 2/3] ConfigureAwait(false) --- uhttpsharp/HttpClient.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/uhttpsharp/HttpClient.cs b/uhttpsharp/HttpClient.cs index 8b9b358..9adadb0 100644 --- a/uhttpsharp/HttpClient.cs +++ b/uhttpsharp/HttpClient.cs @@ -64,7 +64,7 @@ private async Task InitializeStream() { if (Client is ClientSslDecorator) { - await ((ClientSslDecorator)Client).AuthenticateAsServer(); + await ((ClientSslDecorator)Client).AuthenticateAsServer().ConfigureAwait(false); } _stream = new BufferedStream(_client.Stream, 8096); @@ -290,4 +290,4 @@ private static Func Aggregate(this IList Date: Wed, 2 Dec 2020 02:44:08 +1300 Subject: [PATCH 3/3] Bumped TLS version to 1.2 --- uhttpsharp/Clients/ClientSslDecoerator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uhttpsharp/Clients/ClientSslDecoerator.cs b/uhttpsharp/Clients/ClientSslDecoerator.cs index 7f3962a..98eff9a 100644 --- a/uhttpsharp/Clients/ClientSslDecoerator.cs +++ b/uhttpsharp/Clients/ClientSslDecoerator.cs @@ -24,7 +24,7 @@ public ClientSslDecorator(IClient child, X509Certificate certificate) public async Task AuthenticateAsServer() { Task timeout = Task.Delay(TimeSpan.FromSeconds(10)); - if (timeout == await Task.WhenAny(_sslStream.AuthenticateAsServerAsync(_certificate, false, SslProtocols.Tls, true), timeout).ConfigureAwait(false)) + if (timeout == await Task.WhenAny(_sslStream.AuthenticateAsServerAsync(_certificate, false, SslProtocols.Tls12, true), timeout).ConfigureAwait(false)) { throw new TimeoutException("SSL Authentication Timeout"); }