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)
48
48
49
49
public void DeleteBinding ( string ipPort )
50
50
{
51
- Console . WriteLine ( "Disabling mTLS for http.sys" ) ;
52
-
53
51
var command = $ "http delete sslcert ipport={ ipPort } ";
54
52
ExecuteNetShCommand ( command ) ;
55
-
56
- Console . WriteLine ( "Disabled http.sys settings for mTLS" ) ;
57
53
}
58
54
59
55
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(
13
13
NetShFlag enableSessionTicket = NetShFlag . Disabled )
14
14
{
15
15
// 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
18
17
if ( _netshWrapper . TryGetSslCertBinding ( httpsIpPort , out var sslCertBinding ) )
19
18
{
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 ) ;
21
22
}
22
23
23
24
if ( ! _netshWrapper . TrySelfSignCertificate ( httpsIpPort , certPublicKeyLength , out _certThumbprint ) )
@@ -47,6 +48,12 @@ public static void ResetNetshConfiguration(
47
48
int certPublicKeyLength = 4096 )
48
49
{
49
50
_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
+
50
57
_netshWrapper . AddCertBinding (
51
58
httpsIpPort ,
52
59
_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