-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Support SSLKEYLOGFILE in Release builds #100665
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 all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cca6238
Allow SSLKEYLOGFILE functionality in Release builds
rzikm 5e87384
Unify SSLKEYLOGFILE logging, gate behind AppContextSwitch
rzikm 6b70be8
Add more points where QUIC secrets are logged
rzikm 06c9429
Enable in Debug builds without appctx switch
rzikm ad97210
Update src/libraries/System.Net.Quic/src/System.Net.Quic.csproj
rzikm 6806a61
Code review feedback
rzikm 0256cf1
More code review changes
rzikm 97822da
iAdjust appctx switch name
rzikm 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
134 changes: 134 additions & 0 deletions
134
src/libraries/Common/src/System/Net/Security/SslKeyLogger.cs
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,134 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Net; | ||
|
||
internal static class SslKeyLogger | ||
{ | ||
private static readonly string? s_keyLogFile = Environment.GetEnvironmentVariable("SSLKEYLOGFILE"); | ||
private static readonly FileStream? s_fileStream; | ||
|
||
#pragma warning disable CA1810 // Initialize all static fields when declared and remove cctor | ||
static SslKeyLogger() | ||
{ | ||
s_fileStream = null; | ||
|
||
try | ||
{ | ||
#if DEBUG | ||
bool isEnabled = true; | ||
#else | ||
bool isEnabled = AppContext.TryGetSwitch("System.Net.EnableSslKeyLogging", out bool enabled) && enabled; | ||
#endif | ||
|
||
if (isEnabled && s_keyLogFile != null) | ||
{ | ||
s_fileStream = File.Open(s_keyLogFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
if (NetEventSource.Log.IsEnabled()) | ||
{ | ||
NetEventSource.Error(null, $"Failed to open SSL key log file '{s_keyLogFile}': {ex}"); | ||
} | ||
} | ||
} | ||
#pragma warning restore CA1810 | ||
|
||
public static bool IsEnabled => s_fileStream != null; | ||
|
||
public static void WriteLineRaw(ReadOnlySpan<byte> data) | ||
{ | ||
Debug.Assert(s_fileStream != null); | ||
if (s_fileStream == null) | ||
{ | ||
return; | ||
} | ||
|
||
if (data.Length > 0) | ||
{ | ||
lock (s_fileStream) | ||
{ | ||
s_fileStream.Write(data); | ||
s_fileStream.WriteByte((byte)'\n'); | ||
s_fileStream.Flush(); | ||
} | ||
} | ||
} | ||
|
||
public static void WriteSecrets( | ||
ReadOnlySpan<byte> clientRandom, | ||
ReadOnlySpan<byte> clientHandshakeTrafficSecret, | ||
ReadOnlySpan<byte> serverHandshakeTrafficSecret, | ||
ReadOnlySpan<byte> clientTrafficSecret0, | ||
ReadOnlySpan<byte> serverTrafficSecret0, | ||
ReadOnlySpan<byte> clientEarlyTrafficSecret) | ||
{ | ||
Debug.Assert(s_fileStream != null); | ||
Debug.Assert(!clientRandom.IsEmpty); | ||
|
||
if (s_fileStream == null || | ||
clientRandom.IsEmpty || | ||
|
||
// return early if there is nothing to log | ||
(clientHandshakeTrafficSecret.IsEmpty && | ||
serverHandshakeTrafficSecret.IsEmpty && | ||
clientTrafficSecret0.IsEmpty && | ||
serverTrafficSecret0.IsEmpty && | ||
clientEarlyTrafficSecret.IsEmpty)) | ||
{ | ||
return; | ||
} | ||
|
||
Span<byte> clientRandomUtf8 = clientRandom.Length <= 1024 ? stackalloc byte[clientRandom.Length * 2] : new byte[clientRandom.Length * 2]; | ||
HexEncode(clientRandom, clientRandomUtf8); | ||
|
||
lock (s_fileStream) | ||
{ | ||
WriteSecretCore("CLIENT_HANDSHAKE_TRAFFIC_SECRET"u8, clientRandomUtf8, clientHandshakeTrafficSecret); | ||
WriteSecretCore("SERVER_HANDSHAKE_TRAFFIC_SECRET"u8, clientRandomUtf8, serverHandshakeTrafficSecret); | ||
WriteSecretCore("CLIENT_TRAFFIC_SECRET_0"u8, clientRandomUtf8, clientTrafficSecret0); | ||
WriteSecretCore("SERVER_TRAFFIC_SECRET_0"u8, clientRandomUtf8, serverTrafficSecret0); | ||
WriteSecretCore("CLIENT_EARLY_TRAFFIC_SECRET"u8, clientRandomUtf8, clientEarlyTrafficSecret); | ||
|
||
s_fileStream.Flush(); | ||
} | ||
} | ||
|
||
private static void WriteSecretCore(ReadOnlySpan<byte> labelUtf8, ReadOnlySpan<byte> clientRandomUtf8, ReadOnlySpan<byte> secret) | ||
{ | ||
if (secret.Length == 0) | ||
{ | ||
return; | ||
} | ||
|
||
// write the secret line in the format {label} {client_random (hex)} {secret (hex)} e.g. | ||
// SERVER_HANDSHAKE_TRAFFIC_SECRET bae582227f0f46ca663cb8c3d62e68cec38c2b947e7c4a9ec6f4e262b5ed5354 48f6bd5b0c8447d97129c6dad080f34c7f9f11ade8eeabb011f33811543411d7ab1013b1374bcd81bfface6a2deef539 | ||
int totalLength = labelUtf8.Length + 1 + clientRandomUtf8.Length + 1 + 2 * secret.Length + 1; | ||
rzikm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Span<byte> line = totalLength <= 1024 ? stackalloc byte[totalLength] : new byte[totalLength]; | ||
|
||
labelUtf8.CopyTo(line); | ||
line[labelUtf8.Length] = (byte)' '; | ||
|
||
clientRandomUtf8.CopyTo(line.Slice(labelUtf8.Length + 1)); | ||
line[labelUtf8.Length + 1 + clientRandomUtf8.Length] = (byte)' '; | ||
|
||
HexEncode(secret, line.Slice(labelUtf8.Length + 1 + clientRandomUtf8.Length + 1)); | ||
line[^1] = (byte)'\n'; | ||
|
||
s_fileStream!.Write(line); | ||
} | ||
|
||
private static void HexEncode(ReadOnlySpan<byte> source, Span<byte> destination) | ||
{ | ||
for (int i = 0; i < source.Length; i++) | ||
{ | ||
HexConverter.ToBytesBuffer(source[i], destination.Slice(i * 2)); | ||
} | ||
} | ||
|
||
} |
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
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.