Skip to content

Commit 5ecd3cf

Browse files
committed
testing registry control
1 parent 34f579c commit 5ecd3cf

File tree

9 files changed

+108
-42
lines changed

9 files changed

+108
-42
lines changed

src/BenchmarksApps.sln

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpSys", "BenchmarksApps\T
7272
EndProject
7373
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kestrel", "BenchmarksApps\TLS\Kestrel\Kestrel.csproj", "{291DCDF7-4B7C-D687-A62B-9DF7DF50F2F2}"
7474
EndProject
75+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared", "BenchmarksApps\TLS\Shared\Shared.csproj", "{C7A7E484-F1DB-4A46-B8F8-4895C413F680}"
76+
EndProject
7577
Global
7678
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7779
Debug_Database|Any CPU = Debug_Database|Any CPU
@@ -280,6 +282,14 @@ Global
280282
{291DCDF7-4B7C-D687-A62B-9DF7DF50F2F2}.Release_Database|Any CPU.Build.0 = Release_Database|Any CPU
281283
{291DCDF7-4B7C-D687-A62B-9DF7DF50F2F2}.Release|Any CPU.ActiveCfg = Release|Any CPU
282284
{291DCDF7-4B7C-D687-A62B-9DF7DF50F2F2}.Release|Any CPU.Build.0 = Release|Any CPU
285+
{C7A7E484-F1DB-4A46-B8F8-4895C413F680}.Debug_Database|Any CPU.ActiveCfg = Debug_Database|Any CPU
286+
{C7A7E484-F1DB-4A46-B8F8-4895C413F680}.Debug_Database|Any CPU.Build.0 = Debug_Database|Any CPU
287+
{C7A7E484-F1DB-4A46-B8F8-4895C413F680}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
288+
{C7A7E484-F1DB-4A46-B8F8-4895C413F680}.Debug|Any CPU.Build.0 = Debug|Any CPU
289+
{C7A7E484-F1DB-4A46-B8F8-4895C413F680}.Release_Database|Any CPU.ActiveCfg = Release_Database|Any CPU
290+
{C7A7E484-F1DB-4A46-B8F8-4895C413F680}.Release_Database|Any CPU.Build.0 = Release_Database|Any CPU
291+
{C7A7E484-F1DB-4A46-B8F8-4895C413F680}.Release|Any CPU.ActiveCfg = Release|Any CPU
292+
{C7A7E484-F1DB-4A46-B8F8-4895C413F680}.Release|Any CPU.Build.0 = Release|Any CPU
283293
EndGlobalSection
284294
GlobalSection(SolutionProperties) = preSolution
285295
HideSolutionNode = FALSE
@@ -297,6 +307,7 @@ Global
297307
{D6616E03-A2DA-4929-AD28-595ECC4C004D} = {B6DB234C-8F80-4160-B95D-D70AFC444A3D}
298308
{455942DF-6C8E-4054-AF1D-41A10BE1466F} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
299309
{291DCDF7-4B7C-D687-A62B-9DF7DF50F2F2} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
310+
{C7A7E484-F1DB-4A46-B8F8-4895C413F680} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
300311
EndGlobalSection
301312
GlobalSection(ExtensibilityGlobals) = postSolution
302313
SolutionGuid = {117072DC-DE12-4F74-90CA-692FA2BE8DCB}

src/BenchmarksApps/TLS/HttpSys/HttpSys.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
44
<TargetFramework>net9.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
</PropertyGroup>
88

9+
<ItemGroup>
10+
<ProjectReference Include="..\Shared\Shared.csproj" />
11+
</ItemGroup>
12+
913
<ItemGroup>
1014
<None Update="testCert.pfx">
1115
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>

src/BenchmarksApps/TLS/HttpSys/Program.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using HttpSys;
22
using Microsoft.AspNetCore.Connections.Features;
3-
using Microsoft.AspNetCore.Http.Features;
43
using Microsoft.AspNetCore.Server.HttpSys;
4+
using Shared;
55

66
var builder = WebApplication.CreateBuilder(args);
77
builder.Logging.ClearProviders();
@@ -11,6 +11,7 @@
1111
var tlsRegistryLogsEnabled = bool.TryParse(builder.Configuration["tlsRegistryLogs"], out var tlsRegistryLogsConfig) && tlsRegistryLogsConfig;
1212
var logRequestInfo = bool.TryParse(builder.Configuration["logRequestInfo"], out var logRequestInfoConfig) && logRequestInfoConfig;
1313
var statsEnabled = bool.TryParse(builder.Configuration["statsEnabled"], out var connectionStatsEnabledConfig) && connectionStatsEnabledConfig;
14+
var supportedTlsVersions = ConfigurationHelpers.ParseSslProtocols(builder.Configuration["tlsProtocols"]);
1415

1516
var mTlsEnabled = bool.TryParse(builder.Configuration["mTLS"], out var mTlsEnabledConfig) && mTlsEnabledConfig;
1617
var tlsRenegotiationEnabled = bool.TryParse(builder.Configuration["tlsRenegotiation"], out var tlsRenegotiationEnabledConfig) && tlsRenegotiationEnabledConfig;
@@ -132,11 +133,9 @@ void OnShutdown()
132133
});
133134
}
134135

135-
await app.StartAsync();
136-
136+
RegistryController.EnableTls(supportedTlsVersions);
137137
if (tlsRegistryLogsEnabled)
138138
{
139-
Console.WriteLine("Fetching registry info");
140139
RegistryController.ShowRegistryKeys();
141140
}
142141
if (httpSysLogsEnabled)
@@ -160,5 +159,7 @@ void OnShutdown()
160159
Console.WriteLine($"\tlistening endpoints: {listeningEndpoints}");
161160
Console.WriteLine("--------------------------------");
162161

162+
await app.StartAsync();
163+
163164
Console.WriteLine("Application started.");
164165
await app.WaitForShutdownAsync();

src/BenchmarksApps/TLS/HttpSys/RegistryController.cs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
using System.Text;
1+
using System.Security.Authentication;
2+
using System.Text;
23
using Microsoft.Win32;
34

45
namespace HttpSys;
56

7+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "benchmark only runs on windows")]
68
public static class RegistryController
79
{
810
private const string TLS12Key = @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server";
@@ -21,6 +23,22 @@ public static void ShowRegistryKeys()
2123
Console.WriteLine(strBuilder.ToString());
2224
}
2325

26+
public static void EnableTls(SslProtocols sslProtocols)
27+
{
28+
if (sslProtocols.HasFlag(SslProtocols.Tls12))
29+
{
30+
EnableTls12();
31+
return;
32+
}
33+
if (sslProtocols.HasFlag(SslProtocols.Tls13))
34+
{
35+
EnableTls13();
36+
return;
37+
}
38+
39+
throw new ArgumentOutOfRangeException(nameof(sslProtocols), "Unsupported TLS protocol version. Only TLS1.2 and TLS1.3 are supported.");
40+
}
41+
2442
private static void EnableTls12()
2543
{
2644
// todo
@@ -32,10 +50,19 @@ private static void EnableTls13()
3250
? RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
3351
: RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
3452

35-
localKey.OpenSubKey(TLS13Key).SetValue("EnableHTTP3", 1);
53+
var registrySubKey = localKey.OpenSubKey(TLS13Key, writable: true);
54+
if (registrySubKey is null)
55+
{
56+
Console.WriteLine($"Registry subKey `{TLS13Key}` does not exist. Creating one...");
57+
registrySubKey = localKey.CreateSubKey(TLS13Key);
58+
Console.WriteLine($"Created Registry subKey `{TLS13Key}`");
59+
}
60+
61+
Console.WriteLine($"Enabling registry setting {TLS13Key}\\EnableHTTP3 for TLS1.3");
62+
registrySubKey.SetValue("EnableHTTP3", 1);
63+
Console.WriteLine($"Enabled registry setting {TLS13Key}\\EnableHTTP3 for TLS1.3");
3664
}
3765

38-
[System.Diagnostics.CodeAnalysis.SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "benchmark only runs on windows")]
3966
private static string? GetRegistryValue(string path, string name)
4067
{
4168
var localKey = Environment.Is64BitOperatingSystem

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@
99
"httpSysLogs": "true",
1010
"tlsRegistryLogs": "true",
1111
"tlsRenegotiation": "true",
12-
"certValidationConsoleEnabled": "true"
12+
"certValidationConsoleEnabled": "true",
13+
14+
"tlsProtocols": "tls13"
1315
}

src/BenchmarksApps/TLS/Kestrel/Kestrel.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
<PackageReference Include="Microsoft.AspNetCore.Authentication.Certificate" Version="9.0.0" />
1111
</ItemGroup>
1212

13+
<ItemGroup>
14+
<ProjectReference Include="..\Shared\Shared.csproj" />
15+
</ItemGroup>
16+
1317
<ItemGroup>
1418
<None Update="testCert.pfx">
1519
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>

src/BenchmarksApps/TLS/Kestrel/Program.cs

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.AspNetCore.Server.HttpSys;
66
using Microsoft.AspNetCore.Server.Kestrel.Core;
77
using Microsoft.AspNetCore.Server.Kestrel.Https;
8+
using Shared;
89

910
var builder = WebApplication.CreateBuilder(args);
1011
builder.Logging.ClearProviders();
@@ -14,7 +15,7 @@
1415
var tlsRenegotiationEnabled = bool.TryParse(builder.Configuration["tlsRenegotiation"], out var tlsRenegotiationEnabledConfig) && tlsRenegotiationEnabledConfig;
1516
var statsEnabled = bool.TryParse(builder.Configuration["statsEnabled"], out var connectionStatsEnabledConfig) && connectionStatsEnabledConfig;
1617
var listeningEndpoints = builder.Configuration["urls"] ?? "https://localhost:5000/";
17-
var supportedTlsVersions = ParseSslProtocols(builder.Configuration["tlsProtocols"]);
18+
var supportedTlsVersions = ConfigurationHelpers.ParseSslProtocols(builder.Configuration["tlsProtocols"]);
1819

1920
if (mTlsEnabled && tlsRenegotiationEnabled)
2021
{
@@ -161,36 +162,4 @@ static IPEndPoint CreateIPEndPoint(UrlPrefix urlPrefix)
161162
}
162163

163164
return new IPEndPoint(ip, urlPrefix.PortValue);
164-
}
165-
166-
static SslProtocols ParseSslProtocols(string? supportedTlsVersions)
167-
{
168-
var protocols = SslProtocols.Tls12; // default it TLS 1.2
169-
if (string.IsNullOrEmpty(supportedTlsVersions))
170-
{
171-
return protocols;
172-
}
173-
174-
protocols = SslProtocols.None;
175-
foreach (var version in supportedTlsVersions.Split(','))
176-
{
177-
switch (version.Trim().ToLower())
178-
{
179-
#pragma warning disable SYSLIB0039 // Type or member is obsolete
180-
case "tls11":
181-
protocols |= SslProtocols.Tls11;
182-
break;
183-
#pragma warning restore SYSLIB0039 // Type or member is obsolete
184-
case "tls12":
185-
protocols |= SslProtocols.Tls12;
186-
break;
187-
case "tls13":
188-
protocols |= SslProtocols.Tls13;
189-
break;
190-
default:
191-
throw new ArgumentException($"Unsupported TLS version: {version}");
192-
}
193-
}
194-
195-
return protocols;
196165
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.Security.Authentication;
2+
3+
namespace Shared
4+
{
5+
public static class ConfigurationHelpers
6+
{
7+
public static SslProtocols ParseSslProtocols(string? supportedTlsVersions)
8+
{
9+
var protocols = SslProtocols.Tls12; // default it TLS 1.2
10+
if (string.IsNullOrEmpty(supportedTlsVersions))
11+
{
12+
return protocols;
13+
}
14+
15+
protocols = SslProtocols.None;
16+
foreach (var version in supportedTlsVersions.Split(','))
17+
{
18+
switch (version.Trim().ToLower())
19+
{
20+
#pragma warning disable SYSLIB0039 // Type or member is obsolete
21+
case "tls11":
22+
protocols |= SslProtocols.Tls11;
23+
break;
24+
#pragma warning restore SYSLIB0039 // Type or member is obsolete
25+
case "tls12":
26+
protocols |= SslProtocols.Tls12;
27+
break;
28+
case "tls13":
29+
protocols |= SslProtocols.Tls13;
30+
break;
31+
default:
32+
throw new ArgumentException($"Unsupported TLS version: {version}");
33+
}
34+
}
35+
36+
return protocols;
37+
}
38+
}
39+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>

0 commit comments

Comments
 (0)