Skip to content

Conversation

@koarz
Copy link
Contributor

@koarz koarz commented Dec 16, 2025

What problem does this PR solve?

Issue Number: resolve

Problem Summary:

This commit adds support for the SSLKEYLOGFILE environment variable,
enabling the export of TLS master secrets. This allows developers to
decrypt HTTPS/TLS traffic using tools like Wireshark for debugging purposes.

What is changed and the side effects?

Changed:

Side effects:

  • Performance effects:

  • Breaking backward compatibility:


Check List:

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds support for the SSLKEYLOGFILE environment variable to enable TLS key logging for debugging purposes. When set, TLS master secrets are exported to a file, allowing developers to decrypt HTTPS/TLS traffic using tools like Wireshark.

Key changes:

  • Implements SSLKEYLOGFILE support using pthread_once for initialization and OpenSSL's keylog callback API
  • Adds compatibility guards for OpenSSL 1.1.1+ and BoringSSL
  • Integrates key logging into both client and server SSL context creation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 212 to 213
fprintf(g_ssl_keylog_file, "%s\n", line);
fflush(g_ssl_keylog_file);
Copy link

Copilot AI Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fprintf and fflush operations on g_ssl_keylog_file are not thread-safe. The SSLKeyLogCallback can be invoked concurrently from multiple SSL connections, and without synchronization, concurrent writes could interleave or corrupt the output. Consider adding a mutex to protect the file write operations or use flockfile/funlockfile for atomic writes.

Copilot uses AI. Check for mistakes.
Comment on lines 195 to 221
static void InitSSLKeyLogFile() {
const char* path = getenv("SSLKEYLOGFILE");
if (path == NULL || path[0] == '\0') {
return;
}
g_ssl_keylog_file = fopen(path, "a");
if (g_ssl_keylog_file == NULL) {
PLOG(WARNING) << "Fail to open SSLKEYLOGFILE=" << path;
}
}

static void SSLKeyLogCallback(const SSL* ssl, const char* line) {
(void)ssl;
if (line == NULL) {
return;
}
// Write the full key log line with newline in one call to keep output atomic.
fprintf(g_ssl_keylog_file, "%s\n", line);
fflush(g_ssl_keylog_file);
}

static void MaybeSetKeyLogCallback(SSL_CTX* ctx) {
pthread_once(&g_ssl_keylog_once, InitSSLKeyLogFile);
if (ctx != NULL && g_ssl_keylog_file != NULL) {
SSL_CTX_set_keylog_callback(ctx, SSLKeyLogCallback);
}
}
Copy link

Copilot AI Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The InitSSLKeyLogFile, SSLKeyLogCallback, and MaybeSetKeyLogCallback functions lack documentation explaining their purpose, the SSLKEYLOGFILE format expectations, and any security implications of enabling this feature. Adding comments would help other developers understand when and how this debugging feature should be used, especially since it exposes sensitive cryptographic material.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant