|
1 | 1 | using System.Text;
|
2 | 2 | using Microsoft.Win32;
|
3 | 3 |
|
4 |
| -namespace HttpSys |
| 4 | +namespace HttpSys; |
| 5 | + |
| 6 | +public static class RegistryController |
5 | 7 | {
|
6 |
| - public static class RegistryController |
| 8 | + private const string TLS12Key = @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server"; |
| 9 | + private const string TLS13Key = @"SYSTEM\CurrentControlSet\Services\HTTP\Parameters"; |
| 10 | + |
| 11 | + public static void ShowRegistryKeys() |
7 | 12 | {
|
8 |
| - private const string TLS12Key = @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server"; |
9 |
| - private const string TLS13Key = @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\HTTP\Parameters"; |
| 13 | + var tls12Enabled = GetRegistryValue(TLS12Key, ""); |
| 14 | + var tls13Enabled = GetRegistryValue(TLS13Key, "EnableHTTP3"); |
10 | 15 |
|
11 |
| - [System.Diagnostics.CodeAnalysis.SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "benchmark only runs on windows")] |
12 |
| - public static void ShowRegistryKeys() |
13 |
| - { |
14 |
| - var strBuilder = new StringBuilder("Registry TLS settings: \n"); |
| 16 | + var strBuilder = new StringBuilder("Registry TLS settings: \n"); |
| 17 | + strBuilder.AppendLine("\tTLS 1.2: " + tls12Enabled?.ToString()); |
| 18 | + strBuilder.AppendLine("\tTLS 1.3: " + tls13Enabled?.ToString()); |
| 19 | + strBuilder.AppendLine("\t------"); |
| 20 | + |
| 21 | + Console.WriteLine(strBuilder.ToString()); |
| 22 | + } |
| 23 | + |
| 24 | + private static void EnableTls12() |
| 25 | + { |
| 26 | + // todo |
| 27 | + } |
15 | 28 |
|
16 |
| - var tls12Enabled = Registry.GetValue(TLS12Key, "Enabled", "not defined"); |
17 |
| - strBuilder.AppendLine("\tTLS 1.2: " + tls12Enabled?.ToString()); |
| 29 | + private static void EnableTls13() |
| 30 | + { |
| 31 | + var localKey = Environment.Is64BitOperatingSystem |
| 32 | + ? RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64) |
| 33 | + : RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32); |
18 | 34 |
|
19 |
| - var tls13Enabled = Registry.GetValue(TLS13Key, "EnableHTTP3", "not defined"); |
20 |
| - strBuilder.AppendLine("\tTLS 1.3: " + tls12Enabled?.ToString()); |
| 35 | + localKey.OpenSubKey(TLS13Key).SetValue("EnableHTTP3", 1); |
| 36 | + } |
21 | 37 |
|
22 |
| - Console.WriteLine(strBuilder.ToString()); |
| 38 | + [System.Diagnostics.CodeAnalysis.SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "benchmark only runs on windows")] |
| 39 | + private static string? GetRegistryValue(string path, string name) |
| 40 | + { |
| 41 | + var localKey = Environment.Is64BitOperatingSystem |
| 42 | + ? RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64) |
| 43 | + : RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32); |
| 44 | + |
| 45 | + var registrySubKey = localKey.OpenSubKey(path); |
| 46 | + if (registrySubKey is not null) |
| 47 | + { |
| 48 | + return registrySubKey.GetValue(name)?.ToString(); |
23 | 49 | }
|
| 50 | + |
| 51 | + return null; |
24 | 52 | }
|
25 | 53 | }
|
0 commit comments