Skip to content

Commit b81e10b

Browse files
committed
also delete a cert from the store
1 parent 369ee94 commit b81e10b

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

src/BenchmarksApps/TLS/HttpSys/NetSh/NetShWrapper.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff 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)

src/BenchmarksApps/TLS/HttpSys/NetSh/NetshConfigurator.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff 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,
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
}

0 commit comments

Comments
 (0)