| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 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" |
| 47 |
#include "utils/s2n_blob.h" |
| 48 |
#include "utils/s2n_safety.h" |
| 49 |
|
| 50 |
|
| 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 |
|
| 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 |
|
| 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 |
| 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 |
|
| 135 |
if (!conn->config->key_log_cb) { |
| 136 |
return S2N_RESULT_OK; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 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 |
| 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 |
} |