diff --git a/uhttpsharp/Clients/ClientSslDecoerator.cs b/uhttpsharp/Clients/ClientSslDecoerator.cs index 116682b..98eff9a 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.Tls12, 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..9adadb0 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().ConfigureAwait(false); + } + + _stream = new BufferedStream(_client.Stream, 8096); + } + private async void Process() { try { + await InitializeStream(); + while (_client.Connected) { // TODO : Configuration. @@ -280,4 +290,4 @@ private static Func Aggregate(this IList