1
1
using System . Net ;
2
2
using System . Net . Security ;
3
+ using System . Security . Authentication ;
3
4
using System . Security . Cryptography . X509Certificates ;
4
5
using Microsoft . AspNetCore . Authentication . Certificate ;
5
6
using Microsoft . AspNetCore . Connections . Features ;
15
16
var mTlsEnabled = bool . TryParse ( builder . Configuration [ "mTLS" ] , out var mTlsEnabledConfig ) && mTlsEnabledConfig ;
16
17
var tlsRenegotiationEnabled = bool . TryParse ( builder . Configuration [ "tlsRenegotiation" ] , out var tlsRenegotiationEnabledConfig ) && tlsRenegotiationEnabledConfig ;
17
18
var listeningEndpoints = builder . Configuration [ "urls" ] ?? "https://localhost:5000/" ;
19
+ var supportedTlsVersions = ParseSslProtocols ( builder . Configuration [ "tlsProtocols" ] ) ;
18
20
19
21
// debug
20
22
var writeCertValidationEventsToConsole = bool . TryParse ( builder . Configuration [ "certValidationConsoleEnabled" ] , out var certValidationConsoleEnabled ) && certValidationConsoleEnabled ;
@@ -46,6 +48,11 @@ void ConfigureListen(KestrelServerOptions serverOptions, IConfigurationRoot conf
46
48
// [SuppressMessage("Microsoft.Security", "CSCAN0220.DefaultPasswordContexts", Justification="Benchmark code, not a secret")]
47
49
listenOptions . UseHttps ( "testCert.pfx" , "testPassword" , options =>
48
50
{
51
+ if ( supportedTlsVersions is not null )
52
+ {
53
+ options . SslProtocols = supportedTlsVersions . Value ;
54
+ }
55
+
49
56
if ( mTlsEnabled )
50
57
{
51
58
options . ClientCertificateMode = ClientCertificateMode . RequireCertificate ;
@@ -164,6 +171,7 @@ bool AllowAnyCertificateValidationWithLogging(X509Certificate2 certificate, X509
164
171
{
165
172
Console . WriteLine ( $ "\t enabled logging stats to console") ;
166
173
}
174
+ Console . WriteLine ( $ "\t supported TLS versions: { supportedTlsVersions } ") ;
167
175
Console . WriteLine ( $ "\t listening endpoints: { listeningEndpoints } ") ;
168
176
Console . WriteLine ( "--------------------------------" ) ;
169
177
@@ -184,4 +192,37 @@ static IPEndPoint CreateIPEndPoint(UrlPrefix urlPrefix)
184
192
}
185
193
186
194
return new IPEndPoint ( ip , urlPrefix . PortValue ) ;
195
+ }
196
+
197
+ static SslProtocols ? ParseSslProtocols ( string ? supportedTlsVersions )
198
+ {
199
+ var protocols = SslProtocols . None ;
200
+ if ( string . IsNullOrEmpty ( supportedTlsVersions ) )
201
+ {
202
+ return protocols ;
203
+ }
204
+
205
+ foreach ( var version in supportedTlsVersions . Split ( ',' ) )
206
+ {
207
+ switch ( version . Trim ( ) . ToLower ( ) )
208
+ {
209
+ #pragma warning disable SYSLIB0039 // Type or member is obsolete
210
+ case "tls11" :
211
+ protocols |= SslProtocols . Tls11 ;
212
+ break ;
213
+ #pragma warning restore SYSLIB0039 // Type or member is obsolete
214
+ case "tls12" :
215
+ protocols |= SslProtocols . Tls12 ;
216
+ break ;
217
+ case "tls13" :
218
+ protocols |= SslProtocols . Tls13 ;
219
+ break ;
220
+ case "any" :
221
+ return null ;
222
+ default :
223
+ throw new ArgumentException ( $ "Unsupported TLS version: { version } ") ;
224
+ }
225
+ }
226
+
227
+ return protocols ;
187
228
}
0 commit comments