File tree Expand file tree Collapse file tree 3 files changed +45
-7
lines changed
src/BenchmarksApps/TLS/HttpSys/NetSh Expand file tree Collapse file tree 3 files changed +45
-7
lines changed Original file line number Diff line number Diff line change @@ -48,12 +48,8 @@ public void DeleteBindingIfExists(string ipPort)
4848
4949 public void DeleteBinding ( string ipPort )
5050 {
51- Console . WriteLine ( "Disabling mTLS for http.sys" ) ;
52-
5351 var command = $ "http delete sslcert ipport={ ipPort } ";
5452 ExecuteNetShCommand ( command ) ;
55-
56- Console . WriteLine ( "Disabled http.sys settings for mTLS" ) ;
5753 }
5854
5955 public bool TryGetSslCertBinding ( string ipPort , out SslCertBinding result )
Original file line number Diff line number Diff line change @@ -13,11 +13,12 @@ public static SslCertBinding PreConfigureNetsh(
1313 NetShFlag enableSessionTicket = NetShFlag . Disabled )
1414 {
1515 // we will anyway reconfigure the netsh certificate binding, so we can delete it firstly
16- _netshWrapper . DeleteBindingIfExists ( httpsIpPort ) ;
17-
16+ // and also delete a certificate which is bound to the netsh
1817 if ( _netshWrapper . TryGetSslCertBinding ( httpsIpPort , out var sslCertBinding ) )
1918 {
20- throw new NetshException ( $ "Binding already exists ({ httpsIpPort } ). It was unable to be deleted, and run can not proceed without proper configuration. SslCertBinding: " + sslCertBinding ) ;
19+ Console . WriteLine ( $ "Deleting certificate (thumbprint='{ sslCertBinding . CertificateThumbprint } ') from the localmachine(my) store") ;
20+ SslCertificatesConfigurator . RemoveCertificate ( sslCertBinding . CertificateThumbprint ) ;
21+ _netshWrapper . DeleteBindingIfExists ( httpsIpPort ) ;
2122 }
2223
2324 if ( ! _netshWrapper . TrySelfSignCertificate ( httpsIpPort , certPublicKeyLength , out _certThumbprint ) )
@@ -47,6 +48,12 @@ public static void ResetNetshConfiguration(
4748 int certPublicKeyLength = 4096 )
4849 {
4950 _netshWrapper . DeleteBindingIfExists ( httpsIpPort ) ;
51+ if ( ! string . IsNullOrEmpty ( _certThumbprint ) )
52+ {
53+ Console . WriteLine ( $ "Deleting certificate (thumbprint='{ _certThumbprint } ') from the localmachine(my) store") ;
54+ SslCertificatesConfigurator . RemoveCertificate ( _certThumbprint ) ;
55+ }
56+
5057 _netshWrapper . AddCertBinding (
5158 httpsIpPort ,
5259 _certThumbprint ,
Original file line number Diff line number Diff line change 1+ using System . Security . Cryptography . X509Certificates ;
2+
3+ namespace HttpSys . NetSh
4+ {
5+ public static class SslCertificatesConfigurator
6+ {
7+ public static void RemoveCertificate ( string thumbprint )
8+ {
9+ try
10+ {
11+ using ( var store = new X509Store ( StoreName . My , StoreLocation . LocalMachine ) )
12+ {
13+ store . Open ( OpenFlags . ReadWrite ) ;
14+
15+ var certs = store . Certificates . Find ( X509FindType . FindByThumbprint , thumbprint , validOnly : false ) ;
16+ if ( certs . Count == 0 )
17+ {
18+ Console . WriteLine ( "Certificate not found." ) ;
19+ }
20+
21+ foreach ( var cert in certs )
22+ {
23+ store . Remove ( cert ) ;
24+ Console . WriteLine ( $ "Deleted certificate (store LocalMachine/My): { cert . Subject } ") ;
25+ }
26+ store . Close ( ) ;
27+ }
28+ }
29+ catch ( Exception ex )
30+ {
31+ Console . WriteLine ( $ "Remove certificate (thumbprint='{ thumbprint } ') error: { ex . Message } ") ;
32+ }
33+ }
34+ }
35+ }
You can’t perform that action at this time.
0 commit comments