Skip to content

Commit d7fdb1c

Browse files
committed
add http.sys setup
1 parent 5bb7a65 commit d7fdb1c

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/BenchmarksApps/TLS/HttpSys/Program.cs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.AspNetCore.Mvc;
1+
using System.Diagnostics;
2+
using System.Security.Cryptography.X509Certificates;
23
using Microsoft.AspNetCore.Server.HttpSys;
34

45
var builder = WebApplication.CreateBuilder(args);
@@ -7,6 +8,7 @@
78
var writeCertValidationEventsToConsole = bool.TryParse(builder.Configuration["certValidationConsoleEnabled"], out var certValidationConsoleEnabled) && certValidationConsoleEnabled;
89
var statsEnabled = bool.TryParse(builder.Configuration["statsEnabled"], out var connectionStatsEnabledConfig) && connectionStatsEnabledConfig;
910
var mTlsEnabled = bool.TryParse(builder.Configuration["mTLS"], out var mTlsEnabledConfig) && mTlsEnabledConfig;
11+
var tlsRenegotiationEnabled = bool.TryParse(builder.Configuration["tlsRenegotiation"], out var tlsRenegotiationEnabledConfig) && tlsRenegotiationEnabledConfig;
1012
var listeningEndpoints = builder.Configuration["urls"] ?? "https://localhost:5000/";
1113

1214
#pragma warning disable CA1416 // Can be launched only on Windows (HttpSys)
@@ -40,6 +42,11 @@
4042
}
4143

4244
if (mTlsEnabled)
45+
{
46+
ConfigureHttpSysForMutualTls();
47+
}
48+
49+
if (tlsRenegotiationEnabled)
4350
{
4451
// this is an http.sys middleware to get a cert
4552
Console.WriteLine("Registered client cert validation middleware");
@@ -89,4 +96,39 @@
8996
Console.WriteLine("--------------------------------");
9097

9198
Console.WriteLine("Application started.");
92-
await app.WaitForShutdownAsync();
99+
await app.WaitForShutdownAsync();
100+
101+
void ConfigureHttpSysForMutualTls()
102+
{
103+
var certificate = new X509Certificate2("testCert.pfx", "testPassword", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);
104+
using (var store = new X509Store(StoreName.My, StoreLocation.LocalMachine))
105+
{
106+
store.Open(OpenFlags.ReadWrite);
107+
store.Add(certificate);
108+
store.Close();
109+
}
110+
111+
string certThumbprint = certificate.Thumbprint;
112+
string appId = Guid.NewGuid().ToString();
113+
114+
string command = $"http add sslcert ipport=0.0.0.0:5000 certhash={certThumbprint} appid={{{appId}}} clientcertnegotiation=enable";
115+
ProcessStartInfo processInfo = new ProcessStartInfo("netsh", command)
116+
{
117+
RedirectStandardOutput = true,
118+
RedirectStandardError = true,
119+
UseShellExecute = false,
120+
CreateNoWindow = true
121+
};
122+
123+
using Process process = Process.Start(processInfo)!;
124+
string output = process.StandardOutput.ReadToEnd();
125+
string error = process.StandardError.ReadToEnd();
126+
process.WaitForExit();
127+
128+
if (process.ExitCode != 0)
129+
{
130+
throw new InvalidOperationException($"Failed to configure http.sys: {error}");
131+
}
132+
133+
Console.WriteLine("Configured http.sys settings for mTLS");
134+
}

src/BenchmarksApps/TLS/HttpSys/appsettings.Development.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
}
77
},
88
"mTLS": "true",
9+
"tlsRenegotiation": "true",
910
"certValidationConsoleEnabled": "true"
1011
}

0 commit comments

Comments
 (0)