-
Notifications
You must be signed in to change notification settings - Fork 15
Add tls-uses-pqc test to verify OpenSSL TLS does PQC key exchange. #411
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
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| using System.Net; | ||
| using System.Net.Security; | ||
| using System.Net.Sockets; | ||
| using System.Reflection; | ||
| using System.Runtime.InteropServices; | ||
| using System.Security.Cryptography; | ||
| using System.Security.Cryptography.X509Certificates; | ||
|
|
||
| // Create a self-signed certificate. | ||
| using var key = ECDsa.Create(); | ||
| var certReq = new CertificateRequest("CN=localhost", key, HashAlgorithmName.SHA256); | ||
| using var cert = certReq.CreateSelfSigned(DateTimeOffset.UtcNow.AddMinutes(-1), DateTimeOffset.UtcNow.AddYears(1)); | ||
|
|
||
| // Set up a loopback TCP connection. | ||
| var listener = new TcpListener(IPAddress.Loopback, 0); | ||
| listener.Start(); | ||
| int port = ((IPEndPoint)listener.LocalEndpoint).Port; | ||
|
|
||
| // Server side: accept and authenticate as TLS server. | ||
| var serverTask = Task.Run(async () => | ||
| { | ||
| using var serverClient = await listener.AcceptTcpClientAsync(); | ||
| await using var serverSsl = new SslStream(serverClient.GetStream(), false); | ||
| await serverSsl.AuthenticateAsServerAsync(cert); | ||
| return GetNegotiatedGroupName(serverSsl); | ||
| }); | ||
|
|
||
| // Client side: connect and authenticate as TLS client. | ||
| using var client = new TcpClient(); | ||
| await client.ConnectAsync(IPAddress.Loopback, port); | ||
| await using var clientSsl = new SslStream(client.GetStream(), false, (sender, certificate, chain, errors) => true /* accept cert */); | ||
| await clientSsl.AuthenticateAsClientAsync("localhost"); | ||
|
|
||
| string? clientGroup = GetNegotiatedGroupName(clientSsl); | ||
| string? serverGroup = await serverTask; | ||
| listener.Stop(); | ||
|
|
||
| Console.WriteLine($"Client negotiated group: {clientGroup}"); | ||
| Console.WriteLine($"Server negotiated group: {serverGroup}"); | ||
|
|
||
| // Verify PQC was used: the group name should contain "MLKEM". | ||
| bool clientPqc = clientGroup != null && clientGroup.Contains("MLKEM", StringComparison.OrdinalIgnoreCase); | ||
| bool serverPqc = serverGroup != null && serverGroup.Contains("MLKEM", StringComparison.OrdinalIgnoreCase); | ||
|
|
||
| if (clientPqc && serverPqc) | ||
| { | ||
| Console.WriteLine("PASS: PQC key exchange negotiated."); | ||
| return 0; | ||
| } | ||
|
|
||
| Console.WriteLine($"FAIL: Expected PQC key exchange group containing 'MLKEM', got client: '{clientGroup}', server: '{serverGroup}'"); | ||
| return 1; | ||
|
|
||
| // Use OpenSSL interop to get the negotiated key exchange group (SslStream doesn't expose this information). | ||
| static string? GetNegotiatedGroupName(SslStream sslStream) | ||
| { | ||
| var secCtxField = typeof(SslStream).GetField("_securityContext", BindingFlags.NonPublic | BindingFlags.Instance)!; | ||
| var safeSslHandle = (SafeHandle)secCtxField.GetValue(sslStream)!; | ||
| bool added = false; | ||
| safeSslHandle.DangerousAddRef(ref added); | ||
| try | ||
| { | ||
| IntPtr namePtr = SSL_get0_group_name(safeSslHandle.DangerousGetHandle()); | ||
| return namePtr == IntPtr.Zero ? null : Marshal.PtrToStringAnsi(namePtr); | ||
| } | ||
| finally | ||
| { | ||
| if (added) safeSslHandle.DangerousRelease(); | ||
| } | ||
| } | ||
|
|
||
| [DllImport("libssl.so.3")] | ||
| static extern IntPtr SSL_get0_group_name(IntPtr ssl); | ||
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,16 @@ | ||
| { | ||
| "name": "tls-uses-pqc", | ||
| "enabled": true, | ||
| "requiresSdk": true, | ||
| "version": "10.0", | ||
| "versionSpecific": false, | ||
| "type": "bash", | ||
| "cleanup": true, | ||
| "ignoredRIDs":[ | ||
| "rhel.8", // no PQC key exchange support | ||
| "rhel.9", // no PQC key exchange support | ||
| "centos.8", // no PQC key exchange support | ||
| "centos.9", // no PQC key exchange support | ||
| "fedora.42" // no PQC key exchange support | ||
| ] | ||
| } |
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,6 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| set -euo pipefail | ||
| IFS=$'\n\t' | ||
|
|
||
| dotnet run |
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,10 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>$(TestTargetFramework)</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
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.