1010// behavioral
1111var mTlsEnabled = bool . TryParse ( builder . Configuration [ "mTLS" ] , out var mTlsEnabledConfig ) && mTlsEnabledConfig ;
1212var 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 ;
1315var listeningEndpoints = builder . Configuration [ "urls" ] ?? "https://localhost:5000/" ;
1416var httpsIpPort = listeningEndpoints . Split ( ";" ) . First ( x => x . Contains ( "https" ) ) . Replace ( "https://" , "" ) ;
1517
1820var statsEnabled = bool . TryParse ( builder . Configuration [ "statsEnabled" ] , out var connectionStatsEnabledConfig ) && connectionStatsEnabledConfig ;
1921var logRequestDetails = bool . TryParse ( builder . Configuration [ "logRequestDetails" ] , out var logRequestDetailsConfig ) && logRequestDetailsConfig ;
2022
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 ) ;
6029
6130#pragma warning disable CA1416 // Can be launched only on Windows (HttpSys)
6231builder . WebHost . UseHttpSys ( options =>
143112
144113await app . StartAsync ( ) ;
145114
146- netshWrapper . LogSslCertBinding ( httpsIpPort ) ;
115+ NetshConfigurator . LogCurrentSslCertBinding ( httpsIpPort ) ;
147116
148117Console . WriteLine ( "Application Info:" ) ;
149118if ( mTlsEnabled )
165134await app . WaitForShutdownAsync ( ) ;
166135Console . WriteLine ( "Application stopped." ) ;
167136
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