10
10
// behavioral
11
11
var mTlsEnabled = bool . TryParse ( builder . Configuration [ "mTLS" ] , out var mTlsEnabledConfig ) && mTlsEnabledConfig ;
12
12
var tlsRenegotiationEnabled = bool . TryParse ( builder . Configuration [ "tlsRenegotiation" ] , out var tlsRenegotiationEnabledConfig ) && tlsRenegotiationEnabledConfig ;
13
+ var certPublicKeySpecified = int . TryParse ( builder . Configuration [ "certPublicKeyLength" ] , out var certPublicKeyConfig ) ;
14
+ var certPublicKeyLength = certPublicKeySpecified ? certPublicKeyConfig : 2048 ;
13
15
var listeningEndpoints = builder . Configuration [ "urls" ] ?? "https://localhost:5000/" ;
14
16
var httpsIpPort = listeningEndpoints . Split ( ";" ) . First ( x => x . Contains ( "https" ) ) . Replace ( "https://" , "" ) ;
15
17
18
20
var statsEnabled = bool . TryParse ( builder . Configuration [ "statsEnabled" ] , out var connectionStatsEnabledConfig ) && connectionStatsEnabledConfig ;
19
21
var logRequestDetails = bool . TryParse ( builder . Configuration [ "logRequestDetails" ] , out var logRequestDetailsConfig ) && logRequestDetailsConfig ;
20
22
21
- var mTLSNetShFlag = mTlsEnabled ? NetShFlag . Enable : NetShFlag . Disabled ;
22
-
23
- var netshWrapper = new NetShWrapper ( ) ;
24
-
25
- // verify there is an netsh http sslcert binding for specified ip:port
26
- if ( ! netshWrapper . TryGetSslCertBinding ( httpsIpPort , out var sslCertBinding ) )
27
- {
28
- Console . WriteLine ( $ "No binding existed. Need to self-sign it and bind to '{ httpsIpPort } '") ;
29
- if ( ! netshWrapper . TrySelfSignCertificate ( httpsIpPort , out var originalCertThumbprint ) )
30
- {
31
- throw new ApplicationException ( $ "Failed to setup ssl binding for '{ httpsIpPort } '. Please unblock the VM.") ;
32
- }
33
- netshWrapper . AddCertBinding (
34
- httpsIpPort ,
35
- originalCertThumbprint ,
36
- disablesessionid : NetShFlag . Enable ,
37
- enablesessionticket : NetShFlag . Disabled ,
38
- clientCertNegotiation : mTLSNetShFlag ) ;
39
- }
40
-
41
- Console . WriteLine ( "Current netsh ssl certificate binding: \n " + sslCertBinding ) ;
42
-
43
- if (
44
- // those flags can be set only on later versions of HTTP.SYS; so only considering mTLS here
45
- ( netshWrapper . SupportsDisableSessionId && sslCertBinding . DisableSessionIdTlsResumption != NetShFlag . Enable )
46
- || ( netshWrapper . SupportsEnableSessionTicket && ( sslCertBinding . EnableSessionTicketTlsResumption == NetShFlag . Enable ) )
47
- || sslCertBinding . NegotiateClientCertificate != mTLSNetShFlag )
48
- {
49
- Console . WriteLine ( $ "Need to prepare ssl-cert binding for the run.") ;
50
- Console . WriteLine ( $ "Expected configuration: mTLS={ mTLSNetShFlag } ; disableSessionId={ NetShFlag . Enable } ; enableSessionTicket={ NetShFlag . Disabled } ") ;
51
-
52
- netshWrapper . UpdateCertBinding (
53
- httpsIpPort ,
54
- sslCertBinding . CertificateThumbprint ,
55
- appId : sslCertBinding . ApplicationId ,
56
- disablesessionid : NetShFlag . Enable ,
57
- enablesessionticket : NetShFlag . Disabled ,
58
- clientCertNegotiation : mTLSNetShFlag ) ;
59
- }
23
+ var sslCertConfiguration = NetshConfigurator . PreConfigureNetsh (
24
+ httpsIpPort ,
25
+ certPublicKeyLength : certPublicKeyLength ,
26
+ clientCertNegotiation : mTlsEnabled ? NetShFlag . Enable : NetShFlag . Disabled ,
27
+ disablesessionid : NetShFlag . Enable ,
28
+ enableSessionTicket : NetShFlag . Disabled ) ;
60
29
61
30
#pragma warning disable CA1416 // Can be launched only on Windows (HttpSys)
62
31
builder . WebHost . UseHttpSys ( options =>
143
112
144
113
await app . StartAsync ( ) ;
145
114
146
- netshWrapper . LogSslCertBinding ( httpsIpPort ) ;
115
+ NetshConfigurator . LogCurrentSslCertBinding ( httpsIpPort ) ;
147
116
148
117
Console . WriteLine ( "Application Info:" ) ;
149
118
if ( mTlsEnabled )
165
134
await app . WaitForShutdownAsync ( ) ;
166
135
Console . WriteLine ( "Application stopped." ) ;
167
136
168
- if ( netshWrapper . TryGetSslCertBinding ( httpsIpPort , out sslCertBinding ) && mTLSNetShFlag == NetShFlag . Enable )
169
- {
170
- // update the sslCert binding to disable "negotiate client cert" (aka mTLS) to not break other tests.
171
- Console . WriteLine ( $ "Rolling back mTLS setting for sslCert binding at '{ httpsIpPort } '") ;
172
-
173
- sslCertBinding . NegotiateClientCertificate = NetShFlag . Disabled ;
174
- netshWrapper . UpdateCertBinding ( httpsIpPort , sslCertBinding ) ;
175
- }
137
+ Console . WriteLine ( "Starting netsh rollback configuration..." ) ;
138
+ NetshConfigurator . ResetNetshConfiguration ( httpsIpPort , certPublicKeyLength : 4096 ) ; // a default value
139
+ Console . WriteLine ( $ "Reset netsh (ipport={ httpsIpPort } ) completed.") ;
0 commit comments