forked from bonesoul/uhttpsharp
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathClientSslDecorator.cs
More file actions
53 lines (46 loc) · 1.39 KB
/
ClientSslDecorator.cs
File metadata and controls
53 lines (46 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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);
}
public async Task InitializeStream()
{
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
{
get { return _sslStream; }
}
public bool Connected
{
get { return _child.Connected; }
}
public void Close()
{
_child.Close();
}
public EndPoint RemoteEndPoint
{
get { return _child.RemoteEndPoint; }
}
}
}