Skip to content

Commit e3f650c

Browse files
committed
use SslServerAuthenticationOptions as options directly
1 parent 22557cc commit e3f650c

File tree

10 files changed

+32
-55
lines changed

10 files changed

+32
-55
lines changed

samples/ConfigSample/appsettings.tls.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
{
66
"ip": "Any",
77
"port": 4040,
8-
"security": "Tls12",
9-
"certificateOptions" : {
8+
"authenticationOptions" : {
109
"filePath": "supersocket.pfx",
11-
"password": "supersocket"
10+
"password": "supersocket",
11+
"enabledSslProtocols": "Tls12"
1212
}
1313
}
1414
]

samples/LiveChat/appsettings.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
{
1919
"ip": "Any",
2020
"port": 4041,
21-
"security": "Tls12",
22-
"certificateOptions" : {
21+
"authenticationOptions": {
2322
"filePath": "supersocket.pfx",
24-
"password": "supersocket"
23+
"password": "supersocket",
24+
"enabledSslProtocols": "Tls12"
2525
}
2626
}
2727
]

samples/WebSocketServer/appsettings.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
{
1010
"ip": "Any",
1111
"port": 4041,
12-
"security": "Tls12",
13-
"certificateOptions" : {
12+
"authenticationOptions": {
1413
"filePath": "supersocket.pfx",
15-
"password": "supersocket"
14+
"password": "supersocket",
15+
"enabledSslProtocols": "Tls12"
1616
}
1717
}
1818
]

src/SuperSocket.Primitives/CertificateOptions.cs renamed to src/SuperSocket.Primitives/ServerAuthenticationOptions.cs

+9-18
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66

77
namespace SuperSocket
88
{
9-
public class CertificateOptions
9+
public class ServerAuthenticationOptions : SslServerAuthenticationOptions
1010
{
11-
public X509Certificate Certificate { get; set; }
12-
13-
1411
/// <summary>
1512
/// Gets the certificate file path (pfx).
1613
/// </summary>
@@ -44,26 +41,15 @@ public class CertificateOptions
4441
public StoreLocation StoreLocation { get; set; } = StoreLocation.CurrentUser;//The X.509 certificate store used by the current user.
4542

4643

47-
/// <summary>
48-
/// Gets a value indicating whether [client certificate required].
49-
/// </summary>
50-
/// <value>
51-
/// <c>true</c> if [client certificate required]; otherwise, <c>false</c>.
52-
/// </value>
53-
public bool ClientCertificateRequired { get; set; }
54-
5544
/// <summary>
5645
/// Gets a value that will be used to instantiate the X509Certificate2 object in the CertificateManager
5746
/// </summary>
5847
public X509KeyStorageFlags KeyStorageFlags { get; set; }
5948

60-
61-
public RemoteCertificateValidationCallback RemoteCertificateValidationCallback { get; set; }
62-
6349
public void EnsureCertificate()
6450
{
6551
// The certificate is there already
66-
if (Certificate != null)
52+
if (this.ServerCertificate != null)
6753
return;
6854

6955
// load certificate from pfx file
@@ -76,15 +62,15 @@ public void EnsureCertificate()
7662
filePath = Path.Combine(AppContext.BaseDirectory, filePath);
7763
}
7864

79-
Certificate = new X509Certificate2(filePath, Password, KeyStorageFlags);
65+
ServerCertificate = new X509Certificate2(filePath, Password, KeyStorageFlags);
8066
}
8167
else if (!string.IsNullOrEmpty(Thumbprint)) // load certificate from certificate store
8268
{
8369
var store = new X509Store((StoreName)Enum.Parse(typeof(StoreName), StoreName), StoreLocation);
8470

8571
store.Open(OpenFlags.ReadOnly);
8672

87-
Certificate = store.Certificates.OfType<X509Certificate2>()
73+
ServerCertificate = store.Certificates.OfType<X509Certificate2>()
8874
.FirstOrDefault(c => c.Thumbprint.Equals(Thumbprint, StringComparison.OrdinalIgnoreCase));
8975

9076
store.Close();
@@ -94,5 +80,10 @@ public void EnsureCertificate()
9480
throw new Exception($"Either {FilePath} or {Thumbprint} is required to load the certificate.");
9581
}
9682
}
83+
84+
public override string ToString()
85+
{
86+
return this.EnabledSslProtocols.ToString();
87+
}
9788
}
9889
}

src/SuperSocket.Server.Abstractions/ListenOptions.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ public class ListenOptions
1616

1717
public bool NoDelay { get; set; }
1818

19-
public SslProtocols Security { get; set; }
20-
21-
public CertificateOptions CertificateOptions { get; set; }
19+
public ServerAuthenticationOptions AuthenticationOptions { get; set; }
2220

2321
public TimeSpan ConnectionAcceptTimeOut { get; set; } = TimeSpan.FromSeconds(5);
2422

@@ -49,7 +47,7 @@ public IPEndPoint ToEndPoint()
4947

5048
public override string ToString()
5149
{
52-
return $"{nameof(Ip)}={Ip}, {nameof(Port)}={Port}, {nameof(Security)}={Security}, {nameof(Path)}={Path}, {nameof(BackLog)}={BackLog}, {nameof(NoDelay)}={NoDelay}";
50+
return $"{nameof(Ip)}={Ip}, {nameof(Port)}={Port}, {nameof(AuthenticationOptions)}={AuthenticationOptions}, {nameof(Path)}={Path}, {nameof(BackLog)}={BackLog}, {nameof(NoDelay)}={NoDelay}";
5351
}
5452
}
5553
}

src/SuperSocket.Server/Connection/DefaultConnectionStreamInitializersFactory.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public virtual IEnumerable<IConnectionStreamInitializer> Create(ListenOptions li
3030
{
3131
var connectionStreamInitializers = new List<IConnectionStreamInitializer>();
3232

33-
if (listenOptions.Security != SslProtocols.None)
33+
if (listenOptions.AuthenticationOptions != null && listenOptions.AuthenticationOptions.EnabledSslProtocols != SslProtocols.None)
3434
{
3535
connectionStreamInitializers.Add(new NetworkStreamInitializer());
3636
connectionStreamInitializers.Add(new SslStreamInitializer());

src/SuperSocket.Server/Connection/SslStreamInitializer.cs

+3-11
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,13 @@ public class SslStreamInitializer : IConnectionStreamInitializer
1414

1515
public void Setup(ListenOptions listenOptions)
1616
{
17-
var authOptions = new SslServerAuthenticationOptions();
17+
var authOptions = listenOptions.AuthenticationOptions;
1818

19-
authOptions.EnabledSslProtocols = listenOptions.Security;
20-
21-
if (listenOptions.CertificateOptions.Certificate == null)
19+
if (authOptions.ServerCertificate == null)
2220
{
23-
listenOptions.CertificateOptions.EnsureCertificate();
21+
authOptions.EnsureCertificate();
2422
}
2523

26-
authOptions.ServerCertificate = listenOptions.CertificateOptions.Certificate;
27-
authOptions.ClientCertificateRequired = listenOptions.CertificateOptions.ClientCertificateRequired;
28-
29-
if (listenOptions.CertificateOptions.RemoteCertificateValidationCallback != null)
30-
authOptions.RemoteCertificateValidationCallback = listenOptions.CertificateOptions.RemoteCertificateValidationCallback;
31-
3224
_authOptions = authOptions;
3325
}
3426

test/SuperSocket.Tests/GzipSecureHostConfigurator.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,11 @@ public override void Configure(ISuperSocketHostBuilder hostBuilder)
3636
{
3737
var listener = options.Listeners[0];
3838

39-
if (listener.Security == SslProtocols.None)
40-
listener.Security = GetServerEnabledSslProtocols();
41-
42-
listener.CertificateOptions = new CertificateOptions
39+
listener.AuthenticationOptions = new ServerAuthenticationOptions
4340
{
4441
FilePath = "supersocket.pfx",
45-
Password = "supersocket"
42+
Password = "supersocket",
43+
EnabledSslProtocols = GetServerEnabledSslProtocols()
4644
};
4745
});
4846
});

test/SuperSocket.Tests/MainTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public async Task TestSecurityOptions(string security, SslProtocols protocols, b
117117
{
118118
configBuilder.AddInMemoryCollection(new Dictionary<string, string>
119119
{
120-
{ "serverOptions:listeners:0:security", security }
120+
{ "serverOptions:listeners:0:authenticationOptions:enabledSslProtocols", security }
121121
});
122122
})
123123
.ConfigureSuperSocket(serverOptions =>
@@ -142,7 +142,7 @@ public async Task TestSecurityOptions(string security, SslProtocols protocols, b
142142
}
143143

144144
Assert.NotNull(listener);
145-
Assert.Equal(protocols, listener.Security);
145+
Assert.Equal(protocols, listener.AuthenticationOptions.EnabledSslProtocols);
146146

147147
using (server)
148148
{

test/SuperSocket.Tests/SecureHostConfigurator.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,11 @@ public override void Configure(ISuperSocketHostBuilder hostBuilder)
3434
{
3535
var listener = options.Listeners[0];
3636

37-
if (listener.Security == SslProtocols.None)
38-
listener.Security = GetServerEnabledSslProtocols();
39-
40-
listener.CertificateOptions = new CertificateOptions
37+
listener.AuthenticationOptions = new ServerAuthenticationOptions
4138
{
4239
FilePath = "supersocket.pfx",
43-
Password = "supersocket"
40+
Password = "supersocket",
41+
EnabledSslProtocols = GetServerEnabledSslProtocols()
4442
};
4543
});
4644
});

0 commit comments

Comments
 (0)