Skip to content

Commit 5d74143

Browse files
committed
adjusted certificate options
1 parent 20270ed commit 5d74143

File tree

8 files changed

+59
-29
lines changed

8 files changed

+59
-29
lines changed

samples/ConfigSample/appsettings.tls.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
"ip": "Any",
77
"port": 4040,
88
"authenticationOptions" : {
9-
"filePath": "supersocket.pfx",
10-
"password": "supersocket",
9+
"certificateOptions" : {
10+
"filePath": "supersocket.pfx",
11+
"password": "supersocket"
12+
},
1113
"enabledSslProtocols": "Tls12"
1214
}
1315
}

samples/LiveChat/appsettings.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
"ip": "Any",
2020
"port": 4041,
2121
"authenticationOptions": {
22-
"filePath": "supersocket.pfx",
23-
"password": "supersocket",
22+
"certificateOptions": {
23+
"filePath": "supersocket.pfx",
24+
"password": "supersocket"
25+
},
2426
"enabledSslProtocols": "Tls12"
2527
}
2628
}

samples/WebSocketServer/appsettings.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
"ip": "Any",
1111
"port": 4041,
1212
"authenticationOptions": {
13-
"filePath": "supersocket.pfx",
14-
"password": "supersocket",
13+
"certificateOptions": {
14+
"filePath": "supersocket.pfx",
15+
"password": "supersocket"
16+
},
1517
"enabledSslProtocols": "Tls12"
1618
}
1719
}

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

+6-18
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using System;
22
using System.IO;
33
using System.Linq;
4-
using System.Net.Security;
54
using System.Security.Cryptography.X509Certificates;
65

7-
namespace SuperSocket
6+
namespace SuperSocket.Server.Abstractions
87
{
9-
public class ServerAuthenticationOptions : SslServerAuthenticationOptions
8+
public class CertificateOptions
109
{
1110
/// <summary>
1211
/// Gets the certificate file path (pfx).
@@ -46,12 +45,8 @@ public class ServerAuthenticationOptions : SslServerAuthenticationOptions
4645
/// </summary>
4746
public X509KeyStorageFlags KeyStorageFlags { get; set; }
4847

49-
public void EnsureCertificate()
48+
public X509Certificate GetCertificate()
5049
{
51-
// The certificate is there already
52-
if (this.ServerCertificate != null)
53-
return;
54-
5550
// load certificate from pfx file
5651
if (!string.IsNullOrEmpty(FilePath))
5752
{
@@ -62,28 +57,21 @@ public void EnsureCertificate()
6257
filePath = Path.Combine(AppContext.BaseDirectory, filePath);
6358
}
6459

65-
ServerCertificate = new X509Certificate2(filePath, Password, KeyStorageFlags);
60+
return new X509Certificate2(filePath, Password, KeyStorageFlags);
6661
}
6762
else if (!string.IsNullOrEmpty(Thumbprint)) // load certificate from certificate store
6863
{
69-
var store = new X509Store((StoreName)Enum.Parse(typeof(StoreName), StoreName), StoreLocation);
64+
using var store = new X509Store((StoreName)Enum.Parse(typeof(StoreName), StoreName), StoreLocation);
7065

7166
store.Open(OpenFlags.ReadOnly);
7267

73-
ServerCertificate = store.Certificates.OfType<X509Certificate2>()
68+
return store.Certificates.OfType<X509Certificate2>()
7469
.FirstOrDefault(c => c.Thumbprint.Equals(Thumbprint, StringComparison.OrdinalIgnoreCase));
75-
76-
store.Close();
7770
}
7871
else
7972
{
8073
throw new Exception($"Either {FilePath} or {Thumbprint} is required to load the certificate.");
8174
}
8275
}
83-
84-
public override string ToString()
85-
{
86-
return this.EnabledSslProtocols.ToString();
87-
}
8876
}
8977
}

src/SuperSocket.Server.Abstractions/ListenOptions.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Net;
3-
using System.Security.Authentication;
43

54
namespace SuperSocket.Server.Abstractions
65
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using System.Net.Security;
5+
using System.Security.Cryptography.X509Certificates;
6+
7+
namespace SuperSocket.Server.Abstractions
8+
{
9+
public class ServerAuthenticationOptions : SslServerAuthenticationOptions
10+
{
11+
/// <summary>
12+
/// The certificate options.
13+
/// </summary>
14+
public CertificateOptions CertificateOptions { get; set; }
15+
16+
public void EnsureCertificate()
17+
{
18+
var certificateOptions = CertificateOptions;
19+
20+
if (certificateOptions != null)
21+
{
22+
ServerCertificate = certificateOptions.GetCertificate();
23+
}
24+
}
25+
26+
public override string ToString()
27+
{
28+
return EnabledSslProtocols.ToString();
29+
}
30+
}
31+
}

test/SuperSocket.Tests/GzipSecureHostConfigurator.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ public override void Configure(ISuperSocketHostBuilder hostBuilder)
3838

3939
listener.AuthenticationOptions = new ServerAuthenticationOptions
4040
{
41-
FilePath = "supersocket.pfx",
42-
Password = "supersocket",
41+
CertificateOptions = new CertificateOptions
42+
{
43+
FilePath = "supersocket.pfx",
44+
Password = "supersocket"
45+
},
4346
EnabledSslProtocols = GetServerEnabledSslProtocols()
4447
};
4548
});

test/SuperSocket.Tests/SecureHostConfigurator.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ public override void Configure(ISuperSocketHostBuilder hostBuilder)
4343
authenticationOptions = listener.AuthenticationOptions = new ServerAuthenticationOptions();
4444
}
4545

46-
authenticationOptions.FilePath = "supersocket.pfx";
47-
authenticationOptions.Password = "supersocket";
46+
authenticationOptions.CertificateOptions = new CertificateOptions
47+
{
48+
FilePath = "supersocket.pfx",
49+
Password = "supersocket"
50+
};
4851

4952
if (authenticationOptions.EnabledSslProtocols == SslProtocols.None)
5053
{

0 commit comments

Comments
 (0)