|
| 1 | +using System.Diagnostics; |
| 2 | +using System.Security.Cryptography.X509Certificates; |
| 3 | + |
| 4 | +namespace HttpSys |
| 5 | +{ |
| 6 | + public static class NetShWrapper |
| 7 | + { |
| 8 | + public static void DisableHttpSysMutualTlsIfExists(string ipPort) |
| 9 | + { |
| 10 | + try |
| 11 | + { |
| 12 | + DisableHttpSysMutualTls(ipPort); |
| 13 | + } |
| 14 | + catch |
| 15 | + { |
| 16 | + // ignore |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + public static void DisableHttpSysMutualTls(string ipPort) |
| 21 | + { |
| 22 | + Console.WriteLine("Disabling mTLS for http.sys"); |
| 23 | + |
| 24 | + string command = $"http delete sslcert ipport={ipPort}"; |
| 25 | + ExecuteNetShCommand(command); |
| 26 | + |
| 27 | + Console.WriteLine("Disabled http.sys settings for mTLS"); |
| 28 | + } |
| 29 | + |
| 30 | + public static void Show() |
| 31 | + { |
| 32 | + ExecuteNetShCommand("http show sslcert", alwaysLogOutput: true); |
| 33 | + } |
| 34 | + |
| 35 | + public static void EnableHttpSysMutualTls(string ipPort) |
| 36 | + { |
| 37 | + Console.WriteLine("Setting up mTLS for http.sys"); |
| 38 | + |
| 39 | + var certificate = LoadCertificate(); |
| 40 | + Console.WriteLine("Loaded `testCert.pfx` from local file system"); |
| 41 | + using (var store = new X509Store(StoreName.My, StoreLocation.LocalMachine)) |
| 42 | + { |
| 43 | + store.Open(OpenFlags.ReadWrite); |
| 44 | + store.Add(certificate); |
| 45 | + Console.WriteLine("Added `testCert.pfx` to localMachine cert store"); |
| 46 | + store.Close(); |
| 47 | + } |
| 48 | + |
| 49 | + string certThumbprint = certificate.Thumbprint; |
| 50 | + string appId = Guid.NewGuid().ToString(); |
| 51 | + |
| 52 | + string command = $"http add sslcert ipport={ipPort} certstorename=MY certhash={certThumbprint} appid={{{appId}}} clientcertnegotiation=enable"; |
| 53 | + ExecuteNetShCommand(command); |
| 54 | + |
| 55 | + Console.WriteLine("Configured http.sys settings for mTLS"); |
| 56 | + } |
| 57 | + |
| 58 | + private static void ExecuteNetShCommand(string command, bool alwaysLogOutput = false) |
| 59 | + { |
| 60 | + ProcessStartInfo processInfo = new ProcessStartInfo("netsh", command) |
| 61 | + { |
| 62 | + RedirectStandardOutput = true, |
| 63 | + RedirectStandardError = true, |
| 64 | + UseShellExecute = false, |
| 65 | + CreateNoWindow = true |
| 66 | + }; |
| 67 | + |
| 68 | + Console.WriteLine($"Executing command: `netsh {command}`"); |
| 69 | + using Process process = Process.Start(processInfo)!; |
| 70 | + string output = process.StandardOutput.ReadToEnd(); |
| 71 | + process.WaitForExit(); |
| 72 | + |
| 73 | + if (alwaysLogOutput) |
| 74 | + { |
| 75 | + Console.WriteLine(output); |
| 76 | + } |
| 77 | + |
| 78 | + if (process.ExitCode != 0) |
| 79 | + { |
| 80 | + throw new InvalidOperationException($"netsh command execution failure: {output}"); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private static X509Certificate2 LoadCertificate() |
| 85 | + => File.Exists("testCert.pfx") |
| 86 | + ? X509CertificateLoader.LoadPkcs12FromFile("testCert.pfx", "testPassword", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable) |
| 87 | + : X509CertificateLoader.LoadPkcs12FromFile("../testCert.pfx", "testPassword", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable); |
| 88 | + } |
| 89 | +} |
0 commit comments