Skip to content

Commit 76d18fa

Browse files
print tls key for debug
1 parent 82acf51 commit 76d18fa

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

source/s2n/s2n_tls_channel_handler.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <aws/io/tls_channel_handler.h>
66

77
#include <aws/common/clock.h>
8+
#include <aws/common/encoding.h>
9+
#include <aws/common/environment.h>
810
#include <aws/common/mutex.h>
911

1012
#include <aws/io/channel.h>
@@ -41,6 +43,8 @@ struct s2n_delayed_shutdown_task {
4143
int error;
4244
};
4345

46+
AWS_STATIC_STRING_FROM_LITERAL(s_tls_key_log_env_var, "AWS_CRT_EXPORT_TLS_KEYS");
47+
4448
struct s2n_handler {
4549
struct aws_channel_handler handler;
4650
struct aws_tls_channel_handler_shared shared_state;
@@ -98,6 +102,67 @@ struct aws_tls_key_operation {
98102
struct aws_atomic_var complete_count;
99103
};
100104

105+
static aws_thread_once s_tls_key_log_warning_once = AWS_THREAD_ONCE_STATIC_INIT;
106+
107+
static void s_emit_tls_key_log_warning(void *user_data) {
108+
(void)user_data;
109+
printf(
110+
"\n\n===================================================");
111+
printf(
112+
"TLS key logging enabled via AWS_CRT_EXPORT_TLS_KEYS. Secrets will be emitted in NSS key log format.");
113+
printf(
114+
"===================================================\n\n");
115+
}
116+
117+
static bool s_tls_ctx_should_log_keys(struct aws_allocator *allocator) {
118+
struct aws_string *env_value = NULL;
119+
bool enabled = false;
120+
121+
if (aws_get_environment_value(allocator, s_tls_key_log_env_var, &env_value) == AWS_OP_SUCCESS) {
122+
if (env_value != NULL) {
123+
struct aws_byte_cursor value_cursor = aws_byte_cursor_from_string(env_value);
124+
if (value_cursor.len > 0 &&
125+
!aws_byte_cursor_eq_c_str_ignore_case(&value_cursor, "0") &&
126+
!aws_byte_cursor_eq_c_str_ignore_case(&value_cursor, "false") &&
127+
!aws_byte_cursor_eq_c_str_ignore_case(&value_cursor, "off")) {
128+
enabled = true;
129+
}
130+
aws_string_destroy(env_value);
131+
}
132+
}
133+
134+
return enabled;
135+
}
136+
137+
static int s_s2n_key_log_callback(void *user_data, struct s2n_connection *conn, uint8_t *log_line, size_t len) {
138+
(void)conn;
139+
140+
if (log_line == NULL || len == 0) {
141+
return S2N_SUCCESS;
142+
}
143+
144+
struct s2n_ctx *s2n_ctx = user_data;
145+
if (s2n_ctx == NULL) {
146+
AWS_LOGF_WARN(AWS_LS_IO_TLS, "TLS key log: missing TLS context for emitted secret.");
147+
return S2N_SUCCESS;
148+
}
149+
150+
struct aws_allocator *allocator = s2n_ctx->ctx.alloc;
151+
152+
struct aws_byte_cursor cursor = aws_byte_cursor_from_array(log_line, len);
153+
struct aws_string *line_str = aws_string_new_from_cursor(allocator, &cursor);
154+
if (line_str == NULL) {
155+
AWS_LOGF_WARN(AWS_LS_IO_TLS, "TLS key log: failed to allocate buffer for emitted secret.");
156+
return S2N_SUCCESS;
157+
}
158+
159+
AWS_LOGF_INFO(AWS_LS_IO_TLS, "TLS key log: %s", (const char *)line_str->bytes);
160+
fprintf(stderr, "%s\n", (const char *)line_str->bytes);
161+
aws_string_destroy(line_str);
162+
163+
return S2N_SUCCESS;
164+
}
165+
101166
AWS_STATIC_STRING_FROM_LITERAL(s_debian_path, "/etc/ssl/certs");
102167
AWS_STATIC_STRING_FROM_LITERAL(s_rhel_path, "/etc/pki/tls/certs");
103168
AWS_STATIC_STRING_FROM_LITERAL(s_android_path, "/system/etc/security/cacerts");
@@ -1354,6 +1419,7 @@ static struct aws_tls_ctx *s_tls_ctx_new(
13541419
struct aws_allocator *alloc,
13551420
const struct aws_tls_ctx_options *options,
13561421
s2n_mode mode) {
1422+
s_log_and_raise_s2n_errno("ASDF");
13571423
struct s2n_ctx *s2n_ctx = aws_mem_calloc(alloc, 1, sizeof(struct s2n_ctx));
13581424

13591425
if (!s2n_ctx) {
@@ -1388,6 +1454,17 @@ static struct aws_tls_ctx *s_tls_ctx_new(
13881454
goto cleanup_s2n_config;
13891455
}
13901456

1457+
if (s_tls_ctx_should_log_keys(alloc)) {
1458+
if (s2n_config_set_key_log_cb(s2n_ctx->s2n_config, s_s2n_key_log_callback, s2n_ctx)) {
1459+
printf(
1460+
"\n\n\n\n\nctx: failed to enable TLS key logging: %s (%s)\n\n\n\n",
1461+
s2n_strerror(s2n_errno, "EN"),
1462+
s2n_strerror_debug(s2n_errno, "EN"));
1463+
} else {
1464+
aws_thread_call_once(&s_tls_key_log_warning_once, s_emit_tls_key_log_warning, NULL);
1465+
}
1466+
}
1467+
13911468
const char *security_policy = NULL;
13921469
if (options->custom_key_op_handler != NULL) {
13931470
/* When custom_key_op_handler is set, don't use security policy that allow TLS 1.3.

0 commit comments

Comments
 (0)