Skip to content

Commit c4aebe9

Browse files
committed
add set security protocol support
ability to set the security protocol along with certificate for wss://
1 parent 09dc176 commit c4aebe9

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

src/Fleck.Tests/WebSocketServerTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Net;
33
using System.Net.Sockets;
4+
using System.Security.Authentication;
45
using Moq;
56
using NUnit.Framework;
67
using System.Security.Cryptography.X509Certificates;
@@ -60,6 +61,14 @@ public void ShouldBeSecureWithWssAndCertificate()
6061
Assert.IsTrue(server.IsSecure);
6162
}
6263

64+
[Test]
65+
public void ShouldDefaultToNoneWithWssAndCertificate()
66+
{
67+
var server = new WebSocketServer("wss://0.0.0.0:8000");
68+
server.Certificate = new X509Certificate2();
69+
Assert.AreEqual(server.EnabledSslProtocols, SslProtocols.None);
70+
}
71+
6372
[Test]
6473
public void ShouldNotBeSecureWithWssAndNoCertificate()
6574
{

src/Fleck/Interfaces/ISocket.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Security.Cryptography.X509Certificates;
44
using System.Threading.Tasks;
55
using System.IO;
6+
using System.Security.Authentication;
67

78
namespace Fleck
89
{
@@ -17,7 +18,7 @@ public interface ISocket
1718
Task<ISocket> Accept(Action<ISocket> callback, Action<Exception> error);
1819
Task Send(byte[] buffer, Action callback, Action<Exception> error);
1920
Task<int> Receive(byte[] buffer, Action<int> callback, Action<Exception> error, int offset = 0);
20-
Task Authenticate(X509Certificate2 certificate, Action callback, Action<Exception> error);
21+
Task Authenticate(X509Certificate2 certificate, SslProtocols enabledSslProtocols, Action callback, Action<Exception> error);
2122

2223
void Dispose();
2324
void Close();

src/Fleck/SocketWrapper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ public SocketWrapper(Socket socket)
4545
_stream = new NetworkStream(_socket);
4646
}
4747

48-
public Task Authenticate(X509Certificate2 certificate, Action callback, Action<Exception> error)
48+
public Task Authenticate(X509Certificate2 certificate, SslProtocols enabledSslProtocols, Action callback, Action<Exception> error)
4949
{
5050
var ssl = new SslStream(_stream, false);
5151
_stream = new QueuedStream(ssl);
5252
Func<AsyncCallback, object, IAsyncResult> begin =
53-
(cb, s) => ssl.BeginAuthenticateAsServer(certificate, false, SslProtocols.Tls, false, cb, s);
53+
(cb, s) => ssl.BeginAuthenticateAsServer(certificate, false, enabledSslProtocols, false, cb, s);
5454

5555
Task task = Task.Factory.FromAsync(begin, ssl.EndAuthenticateAsServer, null);
5656
task.ContinueWith(t => callback(), TaskContinuationOptions.NotOnFaulted)

src/Fleck/WebSocketServer.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Net.Sockets;
44
using System.Security.Cryptography.X509Certificates;
55
using System.Collections.Generic;
6+
using System.Security.Authentication;
67
using Fleck.Helpers;
78

89
namespace Fleck
@@ -40,6 +41,7 @@ public WebSocketServer(int port, string location)
4041
public string Location { get; private set; }
4142
public int Port { get; private set; }
4243
public X509Certificate2 Certificate { get; set; }
44+
public SslProtocols EnabledSslProtocols { get; set; }
4345
public IEnumerable<string> SupportedSubProtocols { get; set; }
4446

4547
public bool IsSecure
@@ -81,6 +83,12 @@ public void Start(Action<IWebSocketConnection> config)
8183
FleckLog.Error("Scheme cannot be 'wss' without a Certificate");
8284
return;
8385
}
86+
87+
if (EnabledSslProtocols == SslProtocols.None)
88+
{
89+
EnabledSslProtocols = SslProtocols.Tls;
90+
FleckLog.Debug("Using default TLS 1.0 security protocol.");
91+
}
8492
}
8593
ListenForClients();
8694
_config = config;
@@ -117,6 +125,7 @@ private void OnClientConnect(ISocket clientSocket)
117125
FleckLog.Debug("Authenticating Secure Connection");
118126
clientSocket
119127
.Authenticate(Certificate,
128+
EnabledSslProtocols,
120129
connection.StartReceiving,
121130
e => FleckLog.Warn("Failed to Authenticate", e));
122131
}

0 commit comments

Comments
 (0)