1
1
using System ;
2
2
using System . IO ;
3
3
using System . Linq ;
4
- using System . Net . Security ;
5
4
using System . Security . Cryptography . X509Certificates ;
6
5
7
- namespace SuperSocket
6
+ namespace SuperSocket . Server . Abstractions
8
7
{
9
- public class ServerAuthenticationOptions : SslServerAuthenticationOptions
8
+ public class CertificateOptions
10
9
{
11
10
/// <summary>
12
11
/// Gets the certificate file path (pfx).
@@ -46,12 +45,8 @@ public class ServerAuthenticationOptions : SslServerAuthenticationOptions
46
45
/// </summary>
47
46
public X509KeyStorageFlags KeyStorageFlags { get ; set ; }
48
47
49
- public void EnsureCertificate ( )
48
+ public X509Certificate GetCertificate ( )
50
49
{
51
- // The certificate is there already
52
- if ( this . ServerCertificate != null )
53
- return ;
54
-
55
50
// load certificate from pfx file
56
51
if ( ! string . IsNullOrEmpty ( FilePath ) )
57
52
{
@@ -62,28 +57,21 @@ public void EnsureCertificate()
62
57
filePath = Path . Combine ( AppContext . BaseDirectory , filePath ) ;
63
58
}
64
59
65
- ServerCertificate = new X509Certificate2 ( filePath , Password , KeyStorageFlags ) ;
60
+ return new X509Certificate2 ( filePath , Password , KeyStorageFlags ) ;
66
61
}
67
62
else if ( ! string . IsNullOrEmpty ( Thumbprint ) ) // load certificate from certificate store
68
63
{
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 ) ;
70
65
71
66
store . Open ( OpenFlags . ReadOnly ) ;
72
67
73
- ServerCertificate = store . Certificates . OfType < X509Certificate2 > ( )
68
+ return store . Certificates . OfType < X509Certificate2 > ( )
74
69
. FirstOrDefault ( c => c . Thumbprint . Equals ( Thumbprint , StringComparison . OrdinalIgnoreCase ) ) ;
75
-
76
- store . Close ( ) ;
77
70
}
78
71
else
79
72
{
80
73
throw new Exception ( $ "Either { FilePath } or { Thumbprint } is required to load the certificate.") ;
81
74
}
82
75
}
83
-
84
- public override string ToString ( )
85
- {
86
- return this . EnabledSslProtocols . ToString ( ) ;
87
- }
88
76
}
89
77
}
0 commit comments