Skip to content

Commit dc6b466

Browse files
StevenBonePghdarkl
authored andcommitted
Resolve Issue #158 TLS 1.2 Support in Fleck and WebSocket4Net (#170)
* Resolve Issue #158 "TLS 1.2 Support in Fleck and WebSocket4Net" by adding overloads to Fleck and WebSocket4Net. Also added convienence overloads for WAMP2 DefaultRouter (Fleck) and for WAMP1 Channel Factory extensions (WebSocket4Net). Added overloads where defaults would also be acceptable for backwards binary compatibility - these can be removed if this is undesired. I was unable to validate the Mono build. * Add missing space.
1 parent a5db89c commit dc6b466

File tree

9 files changed

+200
-24
lines changed

9 files changed

+200
-24
lines changed

src/net45/Default/WampSharp.Fleck/Fleck/FleckAuthenticatedWebSocketTransport.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Security.Cryptography.X509Certificates;
1+
using System;
2+
using System.Security.Authentication;
3+
using System.Security.Cryptography.X509Certificates;
24
using WampSharp.V2.Authentication;
35

46
namespace WampSharp.Fleck
@@ -9,7 +11,16 @@ public FleckAuthenticatedWebSocketTransport
911
(string location,
1012
ICookieAuthenticatorFactory cookieAuthenticatorFactory = null,
1113
X509Certificate2 certificate = null)
12-
: base(location, cookieAuthenticatorFactory, certificate)
14+
: this(location: location, cookieAuthenticatorFactory: cookieAuthenticatorFactory, certificate: certificate, getEnabledSslProtocols: null)
15+
{
16+
}
17+
18+
public FleckAuthenticatedWebSocketTransport
19+
(string location,
20+
ICookieAuthenticatorFactory cookieAuthenticatorFactory = null,
21+
X509Certificate2 certificate = null,
22+
Func<SslProtocols> getEnabledSslProtocols = null)
23+
: base(location, cookieAuthenticatorFactory, certificate, getEnabledSslProtocols)
1324
{
1425
}
1526
}

src/net45/Default/WampSharp.Fleck/Fleck/FleckWampConnectionListener.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Reactive.Disposables;
33
using System.Reactive.Subjects;
4+
using System.Security.Authentication;
45
using System.Security.Cryptography.X509Certificates;
56
using Fleck;
67
using WampSharp.Core.Listener;
@@ -17,15 +18,25 @@ public class FleckWampConnectionListener<TMessage> : IWampConnectionListener<TMe
1718
private readonly string mLocation;
1819
private readonly IWampTextBinding<TMessage> mBinding;
1920
private readonly X509Certificate2 mCertificate;
21+
private readonly Func<SslProtocols> mGetEnabledSslProtocols;
2022
private readonly object mLock = new object();
2123

2224
public FleckWampConnectionListener(string location,
2325
IWampTextBinding<TMessage> binding,
2426
X509Certificate2 certificate = null)
27+
:this(location: location, binding: binding, certificate: certificate, getEnabledSslProtocols: null)
28+
{
29+
}
30+
31+
public FleckWampConnectionListener(string location,
32+
IWampTextBinding<TMessage> binding,
33+
X509Certificate2 certificate,
34+
Func<SslProtocols> getEnabledSslProtocols)
2535
{
2636
mLocation = location;
2737
mBinding = binding;
2838
mCertificate = certificate;
39+
mGetEnabledSslProtocols = getEnabledSslProtocols;
2940
}
3041

3142
public IDisposable Subscribe(IObserver<IWampConnection<TMessage>> observer)
@@ -65,6 +76,11 @@ private void StartServer()
6576
server.Certificate = mCertificate;
6677
}
6778

79+
if (mGetEnabledSslProtocols != null)
80+
{
81+
server.EnabledSslProtocols = mGetEnabledSslProtocols();
82+
}
83+
6884
mServer.Start(connection =>
6985
{
7086
FleckWampTextConnection<TMessage> wampConnection =

src/net45/Default/WampSharp.Fleck/Fleck/FleckWebSocketTransport.cs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Security.Authentication;
23
using System.Security.Cryptography.X509Certificates;
34
using Fleck;
45
using WampSharp.Core.Listener;
@@ -22,9 +23,21 @@ public class FleckWebSocketTransport : WebSocketTransport<IWebSocketConnection>
2223
/// given the server address to run at.
2324
/// </summary>
2425
/// <param name="location">The given server address.</param>
25-
/// <param name="certificate"></param>
26+
/// <param name="certificate">The <see cref="X509Certificate2"/> certificate to use for secured websockets.</param>
2627
public FleckWebSocketTransport(string location, X509Certificate2 certificate = null)
27-
: this(location, null, certificate)
28+
: this(location: location, cookieAuthenticatorFactory: null, certificate: certificate, getEnabledSslProtocols: null)
29+
{
30+
}
31+
32+
/// <summary>
33+
/// Creates a new instance of <see cref="FleckWebSocketTransport"/>
34+
/// given the server address to run at.
35+
/// </summary>
36+
/// <param name="location">The given server address.</param>
37+
/// <param name="certificate">The <see cref="X509Certificate2"/> certificate to use for secured websockets.</param>
38+
/// <param name="getEnabledSslProtocols"> If non-null, used to set Fleck's EnabledSslProtocols. </param>
39+
public FleckWebSocketTransport(string location, X509Certificate2 certificate, Func<SslProtocols> getEnabledSslProtocols)
40+
: this(location: location, cookieAuthenticatorFactory: null, certificate: certificate, getEnabledSslProtocols: getEnabledSslProtocols)
2841
{
2942
}
3043

@@ -34,15 +47,36 @@ public FleckWebSocketTransport(string location, X509Certificate2 certificate = n
3447
/// </summary>
3548
/// <param name="location">The given server address.</param>
3649
/// <param name="cookieAuthenticatorFactory"></param>
37-
/// <param name="certificate"></param>
50+
/// <param name="certificate">The <see cref="X509Certificate2"/> certificate to use for secured websockets.</param>
3851
protected FleckWebSocketTransport(string location,
3952
ICookieAuthenticatorFactory cookieAuthenticatorFactory = null,
4053
X509Certificate2 certificate = null)
54+
: this(location: location, cookieAuthenticatorFactory: null, certificate: certificate, getEnabledSslProtocols: null)
55+
{
56+
}
57+
58+
/// <summary>
59+
/// Creates a new instance of <see cref="FleckWebSocketTransport"/>
60+
/// given the server address to run at.
61+
/// </summary>
62+
/// <param name="location">The given server address.</param>
63+
/// <param name="cookieAuthenticatorFactory"></param>
64+
/// <param name="certificate">The <see cref="X509Certificate2"/> certificate to use for secured websockets.</param>
65+
/// <param name="getEnabledSslProtocols"> If non-null, used to set Fleck's EnabledSslProtocols. </param>
66+
protected FleckWebSocketTransport(string location,
67+
ICookieAuthenticatorFactory cookieAuthenticatorFactory = null,
68+
X509Certificate2 certificate = null,
69+
Func<SslProtocols> getEnabledSslProtocols = null)
4170
: base(cookieAuthenticatorFactory)
4271
{
4372
mServer = new WebSocketServer(location);
4473
mServer.Certificate = certificate;
45-
74+
75+
if (getEnabledSslProtocols != null)
76+
{
77+
mServer.EnabledSslProtocols = getEnabledSslProtocols();
78+
}
79+
4680
RouteLogs();
4781
}
4882

src/net45/Default/WampSharp.WebSocket4Net/WebSocket4Net/WebSocket4NetBinaryConnection.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using WampSharp.Core.Message;
1+
using System;
2+
using SuperSocket.ClientEngine;
3+
using WampSharp.Core.Message;
24
using WampSharp.V2.Binding;
35
using WebSocket4Net;
46

@@ -32,7 +34,20 @@ public WebSocket4NetBinaryConnection(WebSocket webSocket, IWampBinaryBinding<TMe
3234
/// <param name="serverAddress">The server address to connect to.</param>
3335
/// <param name="binding">The <see cref="IWampBinaryBinding{TMessage}"/> to use.</param>
3436
public WebSocket4NetBinaryConnection(string serverAddress, IWampBinaryBinding<TMessage> binding)
35-
: base(serverAddress, binding)
37+
: this(serverAddress: serverAddress, binding: binding, configureSecurityOptions: null)
38+
{
39+
}
40+
41+
/// <summary>
42+
/// Creates a new instance of <see cref="WebSocket4NetBinaryConnection{TMessage}"/>
43+
/// given the server address to connect to, the binary binding to use, and an Action
44+
/// to configure WebSocket Security Options.
45+
/// </summary>
46+
/// <param name="serverAddress">The server address to connect to.</param>
47+
/// <param name="binding">The <see cref="IWampBinaryBinding{TMessage}"/> to use.</param>
48+
/// <param name="configureSecurityOptions">If non-null, called to allow custom setup of the WebSocket security options.</param>
49+
public WebSocket4NetBinaryConnection(string serverAddress, IWampBinaryBinding<TMessage> binding, Action<SecurityOption> configureSecurityOptions)
50+
: base(serverAddress, binding, configureSecurityOptions)
3651
{
3752
mBinding = binding;
3853
WebSocket.DataReceived += OnDataReceived;

src/net45/Default/WampSharp.WebSocket4Net/WebSocket4Net/WebSocket4NetConnection.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,19 @@ public WebSocket4NetConnection(WebSocket webSocket,
2929

3030
public WebSocket4NetConnection(string serverAddress,
3131
IWampBinding<TMessage> binding)
32+
: this(serverAddress: serverAddress, binding: binding, configureSecurityOptions: null)
33+
{
34+
}
35+
36+
public WebSocket4NetConnection(string serverAddress,
37+
IWampBinding<TMessage> binding,
38+
Action<SecurityOption> configureSecurityOptions)
3239
: this(new WebSocket(serverAddress, binding.Name, WebSocketVersion.None), binding)
3340
{
41+
if (configureSecurityOptions != null)
42+
{
43+
configureSecurityOptions(WebSocket.Security);
44+
}
3445
}
3546

3647
public IWampBinding<TMessage> Binding

src/net45/Default/WampSharp.WebSocket4Net/WebSocket4Net/WebSocket4NetTextConnection.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using WampSharp.Core.Message;
1+
using System;
2+
using SuperSocket.ClientEngine;
3+
using WampSharp.Core.Message;
24
using WampSharp.V2.Binding;
35
using WebSocket4Net;
46

@@ -32,7 +34,20 @@ public WebSocket4NetTextConnection(WebSocket webSocket, IWampTextBinding<TMessag
3234
/// <param name="serverAddress">The server address to connect to.</param>
3335
/// <param name="binding">The <see cref="IWampTextBinding{TMessage}"/> to use.</param>
3436
public WebSocket4NetTextConnection(string serverAddress, IWampTextBinding<TMessage> binding)
35-
: base(serverAddress, binding)
37+
: this(serverAddress: serverAddress, binding: binding, configureSecurityOptions: null)
38+
{
39+
}
40+
41+
/// <summary>
42+
/// Creates a new instance of <see cref="WebSocket4NetTextConnection{TMessage}"/>
43+
/// given the server address to connect to, the text binding to use, and an Action
44+
/// to configure WebSocket Security Options.
45+
/// </summary>
46+
/// <param name="serverAddress">The server address to connect to.</param>
47+
/// <param name="binding">The <see cref="IWampTextBinding{TMessage}"/> to use.</param>
48+
/// <param name="configureSecurityOptions">If non-null, called to allow custom setup of the WebSocket security options.</param>
49+
public WebSocket4NetTextConnection(string serverAddress, IWampTextBinding<TMessage> binding, Action<SecurityOption> configureSecurityOptions)
50+
: base(serverAddress, binding, configureSecurityOptions)
3651
{
3752
mBinding = binding;
3853
WebSocket.MessageReceived += OnMessageReceived;

src/net45/WampSharp.Default.Router/WAMP2/V2/DefaultWampAuthenticationHost.cs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
4+
using System.Security.Authentication;
35
using System.Security.Cryptography.X509Certificates;
46
using WampSharp.Binding;
57
using WampSharp.Fleck;
@@ -68,7 +70,7 @@ public DefaultWampAuthenticationHost
6870
/// <param name="sessionAuthenticationFactory"></param>
6971
/// <param name="bindings">The given bindings.</param>
7072
/// <param name="cookieAuthenticatorFactory"></param>
71-
/// <param name="certificate"></param>
73+
/// <param name="certificate">The <see cref="X509Certificate2"/> certificate to use for secured websockets.</param>
7274
public DefaultWampAuthenticationHost
7375
(string location,
7476
IWampSessionAuthenticatorFactory sessionAuthenticationFactory,
@@ -99,19 +101,54 @@ public DefaultWampAuthenticationHost
99101
/// <param name="cookieAuthenticatorFactory">The given <see cref="ICookieAuthenticatorFactory"/> used to authenticate
100102
/// users given their cookies.</param>
101103
/// <param name="certificate">The <see cref="X509Certificate2"/> certificate to use for secured websockets.</param>
104+
public DefaultWampAuthenticationHost(string location,
105+
IWampSessionAuthenticatorFactory sessionAuthenticationFactory,
106+
IWampRealmContainer realmContainer = null,
107+
IWampUriValidator uriValidator = null,
108+
IEnumerable<IWampBinding> bindings = null,
109+
ICookieAuthenticatorFactory cookieAuthenticatorFactory = null,
110+
X509Certificate2 certificate = null)
111+
: this(location: location,
112+
sessionAuthenticationFactory: sessionAuthenticationFactory,
113+
realmContainer: null,
114+
uriValidator: null,
115+
bindings: bindings,
116+
cookieAuthenticatorFactory: cookieAuthenticatorFactory,
117+
certificate: certificate,
118+
getEnabledSslProtocols: null)
119+
{
120+
}
121+
122+
/// <summary>
123+
/// Initializes a new instance of <see cref="DefaultWampHost"/> listening at
124+
/// the given location with the given bindings and the given
125+
/// <see cref="IWampRealmContainer"/>.
126+
/// </summary>
127+
/// <param name="location">The given location.</param>
128+
/// <param name="sessionAuthenticationFactory">The <see cref="IWampSessionAuthenticatorFactory"/>
129+
/// used to accept pending clients.</param>
130+
/// <param name="realmContainer">The <see cref="IWampRealmContainer"/> associated with this
131+
/// host.</param>
132+
/// <param name="uriValidator">The <see cref="IWampUriValidator"/> used to validate uris.</param>
133+
/// <param name="bindings">The given bindings.</param>
134+
/// <param name="cookieAuthenticatorFactory">The given <see cref="ICookieAuthenticatorFactory"/> used to authenticate
135+
/// users given their cookies.</param>
136+
/// <param name="certificate">The <see cref="X509Certificate2"/> certificate to use for secured websockets.</param>
137+
/// <param name="getEnabledSslProtocols"> If non-null, used to set Fleck's EnabledSslProtocols. </param>
102138
public DefaultWampAuthenticationHost(string location,
103139
IWampSessionAuthenticatorFactory sessionAuthenticationFactory,
104140
IWampRealmContainer realmContainer = null,
105141
IWampUriValidator uriValidator = null,
106142
IEnumerable<IWampBinding> bindings = null,
107143
ICookieAuthenticatorFactory cookieAuthenticatorFactory = null,
108-
X509Certificate2 certificate = null)
144+
X509Certificate2 certificate = null,
145+
Func<SslProtocols> getEnabledSslProtocols = null)
109146
: base(sessionAuthenticationFactory, realmContainer, uriValidator)
110147
{
111-
bindings = bindings ?? new IWampBinding[] {new JTokenJsonBinding(), new JTokenMsgpackBinding()};
148+
bindings = bindings ?? new IWampBinding[] { new JTokenJsonBinding(), new JTokenMsgpackBinding() };
112149

113150
this.RegisterTransport(
114-
new FleckAuthenticatedWebSocketTransport(location, cookieAuthenticatorFactory, certificate),
151+
new FleckAuthenticatedWebSocketTransport(location, cookieAuthenticatorFactory, certificate, getEnabledSslProtocols),
115152
bindings.ToArray());
116153
}
117154

src/net45/WampSharp.Default.Router/WAMP2/V2/DefaultWampHost.cs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
4+
using System.Security.Authentication;
35
using System.Security.Cryptography.X509Certificates;
46
using WampSharp.Binding;
57
using WampSharp.Fleck;
@@ -33,7 +35,7 @@ public DefaultWampHost(string location)
3335
/// <see cref="IWampRealmContainer"/>.
3436
/// </summary>
3537
/// <param name="location">The given location.</param>
36-
/// <param name="certificate"></param>
38+
/// <param name="certificate">The <see cref="X509Certificate2"/> certificate to use for secured websockets.</param>
3739
public DefaultWampHost(string location, X509Certificate2 certificate = null)
3840
: this(location: location, bindings: null, certificate: certificate)
3941
{
@@ -46,7 +48,7 @@ public DefaultWampHost(string location, X509Certificate2 certificate = null)
4648
/// </summary>
4749
/// <param name="location">The given location.</param>
4850
/// <param name="bindings">The given bindings.</param>
49-
/// <param name="certificate"></param>
51+
/// <param name="certificate">The <see cref="X509Certificate2"/> certificate to use for secured websockets.</param>
5052
public DefaultWampHost(string location, IEnumerable<IWampBinding> bindings, X509Certificate2 certificate = null)
5153
: this(location: location, realmContainer: null, uriValidator: null, bindings: bindings, certificate: certificate)
5254
{
@@ -61,17 +63,38 @@ public DefaultWampHost(string location, IEnumerable<IWampBinding> bindings, X509
6163
/// <param name="realmContainer">The given <see cref="IWampRealmContainer"/>.</param>
6264
/// <param name="uriValidator">The <see cref="IWampUriValidator"/> to use to validate uris.</param>
6365
/// <param name="bindings">The given bindings.</param>
64-
/// <param name="certificate"></param>
66+
/// <param name="certificate">The <see cref="X509Certificate2"/> certificate to use for secured websockets.</param>
6567
public DefaultWampHost(string location,
6668
IWampRealmContainer realmContainer = null,
6769
IWampUriValidator uriValidator = null,
6870
IEnumerable<IWampBinding> bindings = null,
6971
X509Certificate2 certificate = null)
72+
: this(location: location, realmContainer: null, uriValidator: null, bindings: bindings, certificate: certificate, getEnabledSslProtocols: null)
73+
{
74+
}
75+
76+
/// <summary>
77+
/// Initializes a new instance of <see cref="DefaultWampHost"/> listening at
78+
/// the given location with the given bindings and the given
79+
/// <see cref="IWampRealmContainer"/>.
80+
/// </summary>
81+
/// <param name="location">The given location.</param>
82+
/// <param name="realmContainer">The given <see cref="IWampRealmContainer"/>.</param>
83+
/// <param name="uriValidator">The <see cref="IWampUriValidator"/> to use to validate uris.</param>
84+
/// <param name="bindings">The given bindings.</param>
85+
/// <param name="certificate">The <see cref="X509Certificate2"/> certificate to use for secured websockets.</param>
86+
/// <param name="getEnabledSslProtocols"> If non-null, used to set Fleck's EnabledSslProtocols. </param>
87+
public DefaultWampHost(string location,
88+
IWampRealmContainer realmContainer = null,
89+
IWampUriValidator uriValidator = null,
90+
IEnumerable<IWampBinding> bindings = null,
91+
X509Certificate2 certificate = null,
92+
Func<SslProtocols> getEnabledSslProtocols = null)
7093
: base(realmContainer, uriValidator)
7194
{
7295
bindings = bindings ?? new IWampBinding[] {new JTokenJsonBinding(), new JTokenMsgpackBinding()};
7396

74-
this.RegisterTransport(new FleckWebSocketTransport(location, certificate),
97+
this.RegisterTransport(new FleckWebSocketTransport(location, certificate, getEnabledSslProtocols),
7598
bindings.ToArray());
7699
}
77100

0 commit comments

Comments
 (0)