-
Notifications
You must be signed in to change notification settings - Fork 241
fix: integrate mutual TLS scenario with underlying http.sys server #2044
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
DeagleGross
merged 18 commits into
aspnet:main
from
DeagleGross:dmkorolev/tls-httpsys-fixes
Jan 15, 2025
Merged
Changes from 12 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
9d774bf
init scripts
DeagleGross 5bb7a65
upd
DeagleGross d7fdb1c
add http.sys setup
DeagleGross 86f2ec8
add debug info
DeagleGross 7adced1
path to cert?
DeagleGross deedf40
add tlsRenegotiation param
DeagleGross 546753b
dont use scripts
DeagleGross efa9d8f
pass tls renego param
DeagleGross 6332df9
httpsys logs
DeagleGross 12acd3e
netsh wrap
DeagleGross 5d64e38
adjust CI configs
DeagleGross adbba73
minor
DeagleGross 2f5437e
address PR comment
DeagleGross 754adee
address PR comments + change port to high-value (no intersection with…
DeagleGross fad2041
typo
DeagleGross 2b5c4d9
remove pragmas
DeagleGross 1503f74
sync client port and server port
DeagleGross c69e2ce
port 8080
DeagleGross File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| using System.Diagnostics; | ||
| using System.Security.Cryptography.X509Certificates; | ||
|
|
||
| namespace HttpSys | ||
| { | ||
| public static class NetShWrapper | ||
| { | ||
| public static void DisableHttpSysMutualTls(string ipPort) | ||
| { | ||
| Console.WriteLine("Disabling mTLS for http.sys"); | ||
|
|
||
| string command = $"http delete sslcert ipport={ipPort}"; | ||
| ExecuteNetShCommand(command); | ||
|
|
||
| Console.WriteLine("Disabled http.sys settings for mTLS"); | ||
| } | ||
|
|
||
| public static void Show() | ||
| { | ||
| ExecuteNetShCommand("http show sslcert", alwaysLogOutput: true); | ||
| } | ||
|
|
||
| public static void EnableHttpSysMutualTls(string ipPort) | ||
| { | ||
| Console.WriteLine("Setting up mTLS for http.sys"); | ||
|
|
||
| var certificate = LoadCertificate(); | ||
| Console.WriteLine("Loaded `testCert.pfx` from local file system"); | ||
| using (var store = new X509Store(StoreName.My, StoreLocation.LocalMachine)) | ||
| { | ||
| store.Open(OpenFlags.ReadWrite); | ||
| store.Add(certificate); | ||
| Console.WriteLine("Added `testCert.pfx` to localMachine cert store"); | ||
| store.Close(); | ||
| } | ||
|
|
||
| string certThumbprint = certificate.Thumbprint; | ||
| string appId = Guid.NewGuid().ToString(); | ||
|
|
||
| string command = $"http add sslcert ipport={ipPort} certstorename=MY certhash={certThumbprint} appid={{{appId}}} clientcertnegotiation=enable"; | ||
| ExecuteNetShCommand(command); | ||
|
|
||
| Console.WriteLine("Configured http.sys settings for mTLS"); | ||
| } | ||
|
|
||
| private static void ExecuteNetShCommand(string command, bool alwaysLogOutput = false) | ||
| { | ||
| ProcessStartInfo processInfo = new ProcessStartInfo("netsh", command) | ||
| { | ||
| RedirectStandardOutput = true, | ||
| RedirectStandardError = true, | ||
| UseShellExecute = false, | ||
| CreateNoWindow = true | ||
| }; | ||
|
|
||
| Console.WriteLine($"Executing command: `netsh {command}`"); | ||
| using Process process = Process.Start(processInfo)!; | ||
| string output = process.StandardOutput.ReadToEnd(); | ||
| process.WaitForExit(); | ||
|
|
||
| if (alwaysLogOutput) | ||
| { | ||
| Console.WriteLine(output); | ||
| } | ||
|
|
||
| if (process.ExitCode != 0) | ||
| { | ||
| throw new InvalidOperationException($"netsh command execution failure: {output}"); | ||
| } | ||
| } | ||
|
|
||
| #pragma warning disable SYSLIB0057 // Type or member is obsolete | ||
| private static X509Certificate2 LoadCertificate() => File.Exists("testCert.pfx") | ||
| ? new X509Certificate2("testCert.pfx", "testPassword", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable) | ||
| : new X509Certificate2("../testCert.pfx", "testPassword", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable); | ||
| #pragma warning restore SYSLIB0057 // Type or member is obsolete | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.