1
1
using System . Net ;
2
2
using System . Net . Security ;
3
+ using System . Security . Authentication ;
3
4
using System . Security . Cryptography . X509Certificates ;
4
- using Microsoft . AspNetCore . Authentication . Certificate ;
5
5
using Microsoft . AspNetCore . Server . HttpSys ;
6
6
using Microsoft . AspNetCore . Server . Kestrel . Core ;
7
7
using Microsoft . AspNetCore . Server . Kestrel . Https ;
14
14
var tlsRenegotiationEnabled = bool . TryParse ( builder . Configuration [ "tlsRenegotiation" ] , out var tlsRenegotiationEnabledConfig ) && tlsRenegotiationEnabledConfig ;
15
15
var statsEnabled = bool . TryParse ( builder . Configuration [ "statsEnabled" ] , out var connectionStatsEnabledConfig ) && connectionStatsEnabledConfig ;
16
16
var listeningEndpoints = builder . Configuration [ "urls" ] ?? "https://localhost:5000/" ;
17
+ var supportedTlsVersions = ParseSslProtocols ( builder . Configuration [ "tlsProtocols" ] ) ;
17
18
18
19
if ( mTlsEnabled && tlsRenegotiationEnabled )
19
20
{
@@ -40,6 +41,8 @@ void ConfigureListen(KestrelServerOptions serverOptions, IConfigurationRoot conf
40
41
// [SuppressMessage("Microsoft.Security", "CSCAN0220.DefaultPasswordContexts", Justification="Benchmark code, not a secret")]
41
42
listenOptions . UseHttps ( "testCert.pfx" , "testPassword" , options =>
42
43
{
44
+ options . SslProtocols = supportedTlsVersions ;
45
+
43
46
if ( mTlsEnabled )
44
47
{
45
48
options . ClientCertificateMode = ClientCertificateMode . RequireCertificate ;
@@ -137,6 +140,7 @@ bool AllowAnyCertificateValidationWithLogging(X509Certificate2 certificate, X509
137
140
{
138
141
Console . WriteLine ( $ "\t enabled logging stats to console") ;
139
142
}
143
+ Console . WriteLine ( $ "\t supported TLS versions: { supportedTlsVersions } ") ;
140
144
Console . WriteLine ( $ "\t listening endpoints: { listeningEndpoints } ") ;
141
145
Console . WriteLine ( "--------------------------------" ) ;
142
146
@@ -157,4 +161,36 @@ static IPEndPoint CreateIPEndPoint(UrlPrefix urlPrefix)
157
161
}
158
162
159
163
return new IPEndPoint ( ip , urlPrefix . PortValue ) ;
164
+ }
165
+
166
+ static SslProtocols ParseSslProtocols ( string ? supportedTlsVersions )
167
+ {
168
+ var protocols = SslProtocols . Tls12 ; // default it TLS 1.2
169
+ if ( string . IsNullOrEmpty ( supportedTlsVersions ) )
170
+ {
171
+ return protocols ;
172
+ }
173
+
174
+ protocols = SslProtocols . None ;
175
+ foreach ( var version in supportedTlsVersions . Split ( ',' ) )
176
+ {
177
+ switch ( version . Trim ( ) . ToLower ( ) )
178
+ {
179
+ #pragma warning disable SYSLIB0039 // Type or member is obsolete
180
+ case "tls11" :
181
+ protocols |= SslProtocols . Tls11 ;
182
+ break ;
183
+ #pragma warning restore SYSLIB0039 // Type or member is obsolete
184
+ case "tls12" :
185
+ protocols |= SslProtocols . Tls12 ;
186
+ break ;
187
+ case "tls13" :
188
+ protocols |= SslProtocols . Tls13 ;
189
+ break ;
190
+ default :
191
+ throw new ArgumentException ( $ "Unsupported TLS version: { version } ") ;
192
+ }
193
+ }
194
+
195
+ return protocols ;
160
196
}
0 commit comments