Debian Codegraph @

crt/s2n/tls/s2n_key_log.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 /**
17 * This module implements key logging as defined by the NSS Key Log Format
18 *
19 * See https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/Key_Log_Format
20 *
21 * This key log file is a series of lines. Comment lines begin with a sharp
22 * character ('#') and are ignored. Secrets follow the format
23 * <Label> <space> <ClientRandom> <space> <Secret> where:
24 *
25 * <Label> describes the following secret.
26 * <ClientRandom> is 32 bytes Random value from the Client Hello message, encoded as 64 hexadecimal characters.
27 * <Secret> depends on the Label (see below).
28 *
29 * The following labels are defined, followed by a description of the secret:
30 *
31 * RSA: 48 bytes for the premaster secret, encoded as 96 hexadecimal characters (removed in NSS 3.34)
32 * CLIENT_RANDOM: 48 bytes for the master secret, encoded as 96 hexadecimal characters (for SSL 3.0, TLS 1.0, 1.1 and 1.2)
33 * CLIENT_EARLY_TRAFFIC_SECRET: the hex-encoded early traffic secret for the client side (for TLS 1.3)
34 * CLIENT_HANDSHAKE_TRAFFIC_SECRET: the hex-encoded handshake traffic secret for the client side (for TLS 1.3)
35 * SERVER_HANDSHAKE_TRAFFIC_SECRET: the hex-encoded handshake traffic secret for the server side (for TLS 1.3)
36 * CLIENT_TRAFFIC_SECRET_0: the first hex-encoded application traffic secret for the client side (for TLS 1.3)
37 * SERVER_TRAFFIC_SECRET_0: the first hex-encoded application traffic secret for the server side (for TLS 1.3)
38 * EARLY_EXPORTER_SECRET: the hex-encoded early exporter secret (for TLS 1.3).
39 * EXPORTER_SECRET: the hex-encoded exporter secret (for TLS 1.3)
40 */
41
42 #include "api/s2n.h"
43 #include "tls/s2n_config.h"
44 #include "tls/s2n_connection.h"
45 #include "tls/s2n_crypto_constants.h"
46 #include "tls/s2n_quic_support.h" /* this currently holds the s2n_secret_type_t enum */
47 #include "utils/s2n_blob.h"
48 #include "utils/s2n_safety.h"
49
50 /* hex requires 2 chars per byte */
51 #define HEX_ENCODING_SIZE 2
52
53 S2N_RESULT s2n_key_log_tls13_secret(struct s2n_connection *conn, const struct s2n_blob *secret, s2n_secret_type_t secret_type)
54 {
55 RESULT_ENSURE_REF(conn);
56 RESULT_ENSURE_REF(conn->config);
57 RESULT_ENSURE_REF(secret);
58
59 /* only emit keys if the callback has been set */
60 if (!conn->config->key_log_cb) {
61 return S2N_RESULT_OK;
62 }
63
64 const uint8_t client_early_traffic_label[] = "CLIENT_EARLY_TRAFFIC_SECRET ";
65 const uint8_t client_handshake_label[] = "CLIENT_HANDSHAKE_TRAFFIC_SECRET ";
66 const uint8_t server_handshake_label[] = "SERVER_HANDSHAKE_TRAFFIC_SECRET ";
67 const uint8_t client_traffic_label[] = "CLIENT_TRAFFIC_SECRET_0 ";
68 const uint8_t server_traffic_label[] = "SERVER_TRAFFIC_SECRET_0 ";
69 const uint8_t exporter_secret_label[] = "EXPORTER_SECRET ";
70
71 const uint8_t *label = NULL;
72 uint8_t label_size = 0;
73
74 switch (secret_type) {
75 case S2N_CLIENT_EARLY_TRAFFIC_SECRET:
76 label = client_early_traffic_label;
77 label_size = sizeof(client_early_traffic_label) - 1;
78 break;
79 case S2N_CLIENT_HANDSHAKE_TRAFFIC_SECRET:
80 label = client_handshake_label;
81 label_size = sizeof(client_handshake_label) - 1;
82 break;
83 case S2N_SERVER_HANDSHAKE_TRAFFIC_SECRET:
84 label = server_handshake_label;
85 label_size = sizeof(server_handshake_label) - 1;
86 break;
87 case S2N_CLIENT_APPLICATION_TRAFFIC_SECRET:
88 label = client_traffic_label;
89 label_size = sizeof(client_traffic_label) - 1;
90 break;
91 case S2N_SERVER_APPLICATION_TRAFFIC_SECRET:
92 label = server_traffic_label;
93 label_size = sizeof(server_traffic_label) - 1;
94 break;
95 case S2N_EXPORTER_SECRET:
96 label = exporter_secret_label;
97 label_size = sizeof(exporter_secret_label) - 1;
98 break;
99 default:
100 /* Ignore the secret types we don't understand */
101 return S2N_RESULT_OK;
102 }
103
104 const uint8_t len = label_size
105 + S2N_TLS_RANDOM_DATA_LEN * HEX_ENCODING_SIZE
106 + 1 /* SPACE */
107 + secret->size * HEX_ENCODING_SIZE;
108
109 DEFER_CLEANUP(struct s2n_stuffer output, s2n_stuffer_free);
110 RESULT_GUARD_POSIX(s2n_stuffer_alloc(&output, len));
111
112 struct s2n_blob client_random = { 0 };
113 RESULT_GUARD_POSIX(s2n_blob_init(&client_random, conn->client_hello.random,
114 sizeof(conn->client_hello.random)));
115
116 RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(&output, label, label_size));
117 RESULT_GUARD(s2n_stuffer_write_hex(&output, &client_random));
118 RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(&output, ' '));
119 RESULT_GUARD(s2n_stuffer_write_hex(&output, secret));
120
121 uint8_t *data = s2n_stuffer_raw_read(&output, len);
122 RESULT_ENSURE_REF(data);
123
124 conn->config->key_log_cb(conn->config->key_log_ctx, conn, data, len);
125
126 return S2N_RESULT_OK;
127 }
128
129 S2N_RESULT s2n_key_log_tls12_secret(struct s2n_connection *conn)
130 {
131 RESULT_ENSURE_REF(conn);
132 RESULT_ENSURE_REF(conn->config);
133
134 /* only emit keys if the callback has been set */
135 if (!conn->config->key_log_cb) {
136 return S2N_RESULT_OK;
137 }
138
139 /* CLIENT_RANDOM: 48 bytes for the master secret, encoded as 96 hexadecimal characters (for SSL 3.0, TLS 1.0, 1.1 and 1.2) */
140 const uint8_t label[] = "CLIENT_RANDOM ";
141 const uint8_t label_size = sizeof(label) - 1;
142
143 const uint8_t len = label_size
144 + S2N_TLS_RANDOM_DATA_LEN * HEX_ENCODING_SIZE
145 + 1 /* SPACE */
146 + S2N_TLS_SECRET_LEN * HEX_ENCODING_SIZE;
147
148 DEFER_CLEANUP(struct s2n_stuffer output, s2n_stuffer_free);
149 RESULT_GUARD_POSIX(s2n_stuffer_alloc(&output, len));
150
151 struct s2n_blob client_random = { 0 };
152 RESULT_GUARD_POSIX(s2n_blob_init(&client_random, conn->client_hello.random,
153 sizeof(conn->client_hello.random)));
154
155 struct s2n_blob master_secret = { 0 };
156 RESULT_GUARD_POSIX(s2n_blob_init(&master_secret, conn->secrets.version.tls12.master_secret,
157 sizeof(conn->secrets.version.tls12.master_secret)));
158
159 RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(&output, label, label_size));
160 RESULT_GUARD(s2n_stuffer_write_hex(&output, &client_random));
161 RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(&output, ' '));
162 RESULT_GUARD(s2n_stuffer_write_hex(&output, &master_secret));
163
164 uint8_t *data = s2n_stuffer_raw_read(&output, len);
165 RESULT_ENSURE_REF(data);
166
167 conn->config->key_log_cb(conn->config->key_log_ctx, conn, data, len);
168
169 return S2N_RESULT_OK;
170 }