Skip to content

Commit fd052b8

Browse files
committed
properly reuse cert
1 parent b81e10b commit fd052b8

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ public static SslCertBinding PreConfigureNetsh(
1616
// and also delete a certificate which is bound to the netsh
1717
if (_netshWrapper.TryGetSslCertBinding(httpsIpPort, out var sslCertBinding))
1818
{
19-
Console.WriteLine($"Deleting certificate (thumbprint='{sslCertBinding.CertificateThumbprint}') from the localmachine(my) store");
20-
SslCertificatesConfigurator.RemoveCertificate(sslCertBinding.CertificateThumbprint);
19+
_certThumbprint = sslCertBinding.CertificateThumbprint;
2120
_netshWrapper.DeleteBindingIfExists(httpsIpPort);
2221
}
2322

24-
if (!_netshWrapper.TrySelfSignCertificate(httpsIpPort, certPublicKeyLength, out _certThumbprint))
23+
if (string.IsNullOrEmpty(_certThumbprint) // only need to self-sign if we dont have a cert in a store, otherwise just reuse it
24+
&& !_netshWrapper.TrySelfSignCertificate(httpsIpPort, certPublicKeyLength, out _certThumbprint))
2525
{
26-
throw new ApplicationException($"Failed to setup ssl binding for '{httpsIpPort}'. Please unblock the VM.");
26+
throw new ApplicationException($"Failed to setup ssl binding for '{httpsIpPort}'.");
2727
}
2828

2929
_netshWrapper.AddCertBinding(
@@ -48,10 +48,10 @@ public static void ResetNetshConfiguration(
4848
int certPublicKeyLength = 4096)
4949
{
5050
_netshWrapper.DeleteBindingIfExists(httpsIpPort);
51-
if (!string.IsNullOrEmpty(_certThumbprint))
51+
if (string.IsNullOrEmpty(_certThumbprint) // again - if cert already exists, we can just reuse it
52+
&& !_netshWrapper.TrySelfSignCertificate(httpsIpPort, certPublicKeyLength, out _certThumbprint))
5253
{
53-
Console.WriteLine($"Deleting certificate (thumbprint='{_certThumbprint}') from the localmachine(my) store");
54-
SslCertificatesConfigurator.RemoveCertificate(_certThumbprint);
54+
throw new ApplicationException($"Failed to self-sign a cert for '{httpsIpPort}'.");
5555
}
5656

5757
_netshWrapper.AddCertBinding(

0 commit comments

Comments
 (0)