| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "tls/s2n_tls13_secrets.h" |
| 17 |
|
| 18 |
#include "tls/s2n_connection.h" |
| 19 |
#include "tls/s2n_key_log.h" |
| 20 |
#include "tls/s2n_tls13_handshake.h" |
| 21 |
#include "utils/s2n_bitmap.h" |
| 22 |
|
| 23 |
#define S2N_MAX_HASHLEN SHA384_DIGEST_LENGTH |
| 24 |
|
| 25 |
#define CONN_HMAC_ALG(conn) ((conn)->secure->cipher_suite->prf_alg) |
| 26 |
#define CONN_SECRETS(conn) ((conn)->secrets.version.tls13) |
| 27 |
#define CONN_HASHES(conn) ((conn)->handshake.hashes) |
| 28 |
|
| 29 |
#define CONN_SECRET(conn, secret) ( \ |
| 30 |
(struct s2n_blob){ .data = CONN_SECRETS(conn).secret, .size = s2n_get_hash_len(CONN_HMAC_ALG(conn)) }) |
| 31 |
#define CONN_HASH(conn, hash) ( \ |
| 32 |
(struct s2n_blob){ .data = CONN_HASHES(conn)->hash, .size = s2n_get_hash_len(CONN_HMAC_ALG(conn)) }) |
| 33 |
#define CONN_FINISHED(conn, mode) ( \ |
| 34 |
(struct s2n_blob){ .data = (conn)->handshake.mode##_finished, .size = s2n_get_hash_len(CONN_HMAC_ALG(conn)) }) |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
static uint8_t zero_value_bytes[S2N_MAX_HASHLEN] = { 0 }; |
| 42 |
#define ZERO_VALUE(hmac_alg) ( \ |
| 43 |
(const struct s2n_blob){ .data = zero_value_bytes, .size = s2n_get_hash_len(hmac_alg) }) |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
#define EMPTY_CONTEXT(hmac_alg) ( \ |
| 54 |
(const struct s2n_blob){ .data = s2n_get_empty_context(hmac_alg), .size = s2n_get_hash_len(hmac_alg) }) |
| 55 |
|
| 56 |
static uint8_t s2n_get_hash_len(s2n_hmac_algorithm hmac_alg) |
| 57 |
{ |
| 58 |
uint8_t hash_size = 0; |
| 59 |
if (s2n_hmac_digest_size(hmac_alg, &hash_size) != S2N_SUCCESS) { |
| 60 |
return 0; |
| 61 |
} |
| 62 |
return hash_size; |
| 63 |
} |
| 64 |
|
| 65 |
static uint8_t *s2n_get_empty_context(s2n_hmac_algorithm hmac_alg) |
| 66 |
{ |
| 67 |
static uint8_t sha256_empty_digest[S2N_MAX_HASHLEN] = { 0 }; |
| 68 |
static uint8_t sha384_empty_digest[S2N_MAX_HASHLEN] = { 0 }; |
| 69 |
|
| 70 |
switch (hmac_alg) { |
| 71 |
case S2N_HMAC_SHA256: |
| 72 |
return sha256_empty_digest; |
| 73 |
case S2N_HMAC_SHA384: |
| 74 |
return sha384_empty_digest; |
| 75 |
default: |
| 76 |
return NULL; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
static s2n_hmac_algorithm supported_hmacs[] = { |
| 81 |
S2N_HMAC_SHA256, |
| 82 |
S2N_HMAC_SHA384 |
| 83 |
}; |
| 84 |
|
| 85 |
S2N_RESULT s2n_tls13_empty_transcripts_init() |
| 86 |
{ |
| 87 |
DEFER_CLEANUP(struct s2n_hash_state hash = { 0 }, s2n_hash_free); |
| 88 |
RESULT_GUARD_POSIX(s2n_hash_new(&hash)); |
| 89 |
|
| 90 |
s2n_hash_algorithm hash_alg = S2N_HASH_NONE; |
| 91 |
for (size_t i = 0; i < s2n_array_len(supported_hmacs); i++) { |
| 92 |
s2n_hmac_algorithm hmac_alg = supported_hmacs[i]; |
| 93 |
struct s2n_blob digest = EMPTY_CONTEXT(hmac_alg); |
| 94 |
|
| 95 |
RESULT_GUARD_POSIX(s2n_hmac_hash_alg(hmac_alg, &hash_alg)); |
| 96 |
RESULT_GUARD_POSIX(s2n_hash_init(&hash, hash_alg)); |
| 97 |
RESULT_GUARD_POSIX(s2n_hash_digest(&hash, digest.data, digest.size)); |
| 98 |
} |
| 99 |
|
| 100 |
return S2N_RESULT_OK; |
| 101 |
} |
| 102 |
|
| 103 |
static S2N_RESULT s2n_calculate_transcript_digest(struct s2n_connection *conn) |
| 104 |
{ |
| 105 |
RESULT_ENSURE_REF(conn); |
| 106 |
RESULT_ENSURE_REF(conn->handshake.hashes); |
| 107 |
|
| 108 |
s2n_hash_algorithm hash_algorithm = S2N_HASH_NONE; |
| 109 |
RESULT_GUARD_POSIX(s2n_hmac_hash_alg(CONN_HMAC_ALG(conn), &hash_algorithm)); |
| 110 |
|
| 111 |
uint8_t digest_size = 0; |
| 112 |
RESULT_GUARD_POSIX(s2n_hash_digest_size(hash_algorithm, &digest_size)); |
| 113 |
|
| 114 |
struct s2n_blob digest = { 0 }; |
| 115 |
RESULT_GUARD_POSIX(s2n_blob_init(&digest, CONN_HASHES(conn)->transcript_hash_digest, digest_size)); |
| 116 |
|
| 117 |
struct s2n_hash_state *hash_state = &conn->handshake.hashes->hash_workspace; |
| 118 |
RESULT_GUARD(s2n_handshake_copy_hash_state(conn, hash_algorithm, hash_state)); |
| 119 |
RESULT_GUARD_POSIX(s2n_hash_digest(hash_state, digest.data, digest.size)); |
| 120 |
|
| 121 |
return S2N_RESULT_OK; |
| 122 |
} |
| 123 |
|
| 124 |
static S2N_RESULT s2n_extract_secret(s2n_hmac_algorithm hmac_alg, |
| 125 |
const struct s2n_blob *previous_secret_material, const struct s2n_blob *new_secret_material, |
| 126 |
struct s2n_blob *output) |
| 127 |
{ |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
DEFER_CLEANUP(struct s2n_hmac_state hmac_state = { 0 }, s2n_hmac_free); |
| 134 |
RESULT_GUARD_POSIX(s2n_hmac_new(&hmac_state)); |
| 135 |
|
| 136 |
RESULT_GUARD_POSIX(s2n_hkdf_extract(&hmac_state, hmac_alg, |
| 137 |
previous_secret_material, new_secret_material, output)); |
| 138 |
return S2N_RESULT_OK; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
static S2N_RESULT s2n_derive_secret(s2n_hmac_algorithm hmac_alg, |
| 148 |
const struct s2n_blob *previous_secret_material, const struct s2n_blob *label, const struct s2n_blob *context, |
| 149 |
struct s2n_blob *output) |
| 150 |
{ |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
DEFER_CLEANUP(struct s2n_hmac_state hmac_state = { 0 }, s2n_hmac_free); |
| 157 |
RESULT_GUARD_POSIX(s2n_hmac_new(&hmac_state)); |
| 158 |
|
| 159 |
output->size = s2n_get_hash_len(hmac_alg); |
| 160 |
RESULT_GUARD_POSIX(s2n_hkdf_expand_label(&hmac_state, hmac_alg, |
| 161 |
previous_secret_material, label, context, output)); |
| 162 |
return S2N_RESULT_OK; |
| 163 |
} |
| 164 |
|
| 165 |
static S2N_RESULT s2n_derive_secret_with_context(struct s2n_connection *conn, |
| 166 |
s2n_extract_secret_type_t input_secret_type, const struct s2n_blob *label, message_type_t transcript_end_msg, |
| 167 |
struct s2n_blob *output) |
| 168 |
{ |
| 169 |
RESULT_ENSURE_REF(conn); |
| 170 |
RESULT_ENSURE_REF(label); |
| 171 |
RESULT_ENSURE_REF(output); |
| 172 |
|
| 173 |
RESULT_ENSURE(conn->secrets.extract_secret_type == input_secret_type, S2N_ERR_SECRET_SCHEDULE_STATE); |
| 174 |
RESULT_ENSURE(s2n_conn_get_current_message_type(conn) == transcript_end_msg, S2N_ERR_SECRET_SCHEDULE_STATE); |
| 175 |
RESULT_GUARD(s2n_derive_secret(CONN_HMAC_ALG(conn), &CONN_SECRET(conn, extract_secret), |
| 176 |
label, &CONN_HASH(conn, transcript_hash_digest), output)); |
| 177 |
return S2N_RESULT_OK; |
| 178 |
} |
| 179 |
|
| 180 |
static S2N_RESULT s2n_derive_secret_without_context(struct s2n_connection *conn, |
| 181 |
s2n_extract_secret_type_t input_secret_type, struct s2n_blob *output) |
| 182 |
{ |
| 183 |
RESULT_ENSURE_REF(conn); |
| 184 |
RESULT_ENSURE_REF(output); |
| 185 |
|
| 186 |
RESULT_ENSURE(conn->secrets.extract_secret_type == input_secret_type, S2N_ERR_SECRET_SCHEDULE_STATE); |
| 187 |
RESULT_GUARD(s2n_derive_secret(CONN_HMAC_ALG(conn), &CONN_SECRET(conn, extract_secret), |
| 188 |
&s2n_tls13_label_derived_secret, &EMPTY_CONTEXT(CONN_HMAC_ALG(conn)), output)); |
| 189 |
return S2N_RESULT_OK; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
static S2N_RESULT s2n_tls13_compute_finished_key(struct s2n_connection *conn, |
| 202 |
const struct s2n_blob *base_key, struct s2n_blob *output) |
| 203 |
{ |
| 204 |
RESULT_ENSURE_REF(conn); |
| 205 |
RESULT_ENSURE_REF(base_key); |
| 206 |
RESULT_ENSURE_REF(output); |
| 207 |
|
| 208 |
RESULT_GUARD(s2n_handshake_set_finished_len(conn, output->size)); |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
DEFER_CLEANUP(struct s2n_hmac_state hmac_state = { 0 }, s2n_hmac_free); |
| 215 |
RESULT_GUARD_POSIX(s2n_hmac_new(&hmac_state)); |
| 216 |
|
| 217 |
RESULT_GUARD_POSIX(s2n_hkdf_expand_label(&hmac_state, CONN_HMAC_ALG(conn), |
| 218 |
base_key, &s2n_tls13_label_finished, &(struct s2n_blob){ 0 }, output)); |
| 219 |
return S2N_RESULT_OK; |
| 220 |
} |
| 221 |
|
| 222 |
static S2N_RESULT s2n_call_secret_callbacks(struct s2n_connection *conn, |
| 223 |
const struct s2n_blob *secret, s2n_secret_type_t secret_type) |
| 224 |
{ |
| 225 |
RESULT_ENSURE_REF(conn); |
| 226 |
RESULT_ENSURE_REF(secret); |
| 227 |
|
| 228 |
if (conn->secret_cb && (s2n_connection_is_quic_enabled(conn) || s2n_in_unit_test())) { |
| 229 |
RESULT_GUARD_POSIX(conn->secret_cb(conn->secret_cb_context, conn, secret_type, |
| 230 |
secret->data, secret->size)); |
| 231 |
} |
| 232 |
s2n_result_ignore(s2n_key_log_tls13_secret(conn, secret, secret_type)); |
| 233 |
return S2N_RESULT_OK; |
| 234 |
} |
| 235 |
|
| 236 |
static S2N_RESULT s2n_trigger_secret_callbacks(struct s2n_connection *conn, |
| 237 |
const struct s2n_blob *secret, s2n_extract_secret_type_t secret_type, s2n_mode mode) |
| 238 |
{ |
| 239 |
RESULT_ENSURE_REF(conn); |
| 240 |
RESULT_ENSURE_REF(secret); |
| 241 |
|
| 242 |
static const s2n_secret_type_t conversions[][2] = { |
| 243 |
[S2N_EARLY_SECRET] = { S2N_CLIENT_EARLY_TRAFFIC_SECRET, S2N_CLIENT_EARLY_TRAFFIC_SECRET }, |
| 244 |
[S2N_HANDSHAKE_SECRET] = { S2N_SERVER_HANDSHAKE_TRAFFIC_SECRET, S2N_CLIENT_HANDSHAKE_TRAFFIC_SECRET }, |
| 245 |
[S2N_MASTER_SECRET] = { S2N_SERVER_APPLICATION_TRAFFIC_SECRET, S2N_CLIENT_APPLICATION_TRAFFIC_SECRET }, |
| 246 |
}; |
| 247 |
s2n_secret_type_t callback_secret_type = conversions[secret_type][mode]; |
| 248 |
|
| 249 |
RESULT_GUARD(s2n_call_secret_callbacks(conn, secret, callback_secret_type)); |
| 250 |
return S2N_RESULT_OK; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
S2N_RESULT s2n_extract_early_secret(struct s2n_psk *psk) |
| 266 |
{ |
| 267 |
RESULT_ENSURE_REF(psk); |
| 268 |
RESULT_GUARD_POSIX(s2n_realloc(&psk->early_secret, s2n_get_hash_len(psk->hmac_alg))); |
| 269 |
RESULT_GUARD(s2n_extract_secret(psk->hmac_alg, |
| 270 |
&ZERO_VALUE(psk->hmac_alg), |
| 271 |
&psk->secret, |
| 272 |
&psk->early_secret)); |
| 273 |
return S2N_RESULT_OK; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
static S2N_RESULT s2n_extract_early_secret_for_schedule(struct s2n_connection *conn) |
| 282 |
{ |
| 283 |
RESULT_ENSURE_REF(conn); |
| 284 |
|
| 285 |
struct s2n_psk *psk = conn->psk_params.chosen_psk; |
| 286 |
s2n_hmac_algorithm hmac_alg = CONN_HMAC_ALG(conn); |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
if (conn->mode == S2N_CLIENT && conn->early_data_state == S2N_EARLY_DATA_REQUESTED) { |
| 293 |
RESULT_GUARD(s2n_array_get(&conn->psk_params.psk_list, 0, (void **) &psk)); |
| 294 |
RESULT_ENSURE_REF(psk); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
if (psk == NULL) { |
| 303 |
RESULT_GUARD(s2n_extract_secret(hmac_alg, |
| 304 |
&ZERO_VALUE(hmac_alg), |
| 305 |
&ZERO_VALUE(hmac_alg), |
| 306 |
&CONN_SECRET(conn, extract_secret))); |
| 307 |
return S2N_RESULT_OK; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
RESULT_ENSURE_EQ(hmac_alg, psk->hmac_alg); |
| 316 |
RESULT_CHECKED_MEMCPY(CONN_SECRETS(conn).extract_secret, psk->early_secret.data, psk->early_secret.size); |
| 317 |
return S2N_RESULT_OK; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
S2N_RESULT s2n_derive_binder_key(struct s2n_psk *psk, struct s2n_blob *output) |
| 327 |
{ |
| 328 |
RESULT_ENSURE_REF(psk); |
| 329 |
RESULT_ENSURE_REF(output); |
| 330 |
|
| 331 |
const struct s2n_blob *label = &s2n_tls13_label_resumption_psk_binder_key; |
| 332 |
if (psk->type == S2N_PSK_TYPE_EXTERNAL) { |
| 333 |
label = &s2n_tls13_label_external_psk_binder_key; |
| 334 |
} |
| 335 |
RESULT_GUARD(s2n_extract_early_secret(psk)); |
| 336 |
RESULT_GUARD(s2n_derive_secret(psk->hmac_alg, |
| 337 |
&psk->early_secret, |
| 338 |
label, |
| 339 |
&EMPTY_CONTEXT(psk->hmac_alg), |
| 340 |
output)); |
| 341 |
return S2N_RESULT_OK; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
static S2N_RESULT s2n_derive_client_early_traffic_secret(struct s2n_connection *conn, struct s2n_blob *output) |
| 351 |
{ |
| 352 |
RESULT_GUARD(s2n_derive_secret_with_context(conn, |
| 353 |
S2N_EARLY_SECRET, |
| 354 |
&s2n_tls13_label_client_early_traffic_secret, |
| 355 |
CLIENT_HELLO, |
| 356 |
output)); |
| 357 |
return S2N_RESULT_OK; |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
static S2N_RESULT s2n_extract_handshake_secret(struct s2n_connection *conn) |
| 370 |
{ |
| 371 |
RESULT_ENSURE_REF(conn); |
| 372 |
|
| 373 |
struct s2n_blob derived_secret = { 0 }; |
| 374 |
uint8_t derived_secret_bytes[S2N_TLS13_SECRET_MAX_LEN] = { 0 }; |
| 375 |
RESULT_GUARD_POSIX(s2n_blob_init(&derived_secret, derived_secret_bytes, S2N_TLS13_SECRET_MAX_LEN)); |
| 376 |
RESULT_GUARD(s2n_derive_secret_without_context(conn, S2N_EARLY_SECRET, &derived_secret)); |
| 377 |
|
| 378 |
DEFER_CLEANUP(struct s2n_blob shared_secret = { 0 }, s2n_free_or_wipe); |
| 379 |
RESULT_GUARD_POSIX(s2n_tls13_compute_shared_secret(conn, &shared_secret)); |
| 380 |
|
| 381 |
RESULT_GUARD(s2n_extract_secret(CONN_HMAC_ALG(conn), |
| 382 |
&derived_secret, |
| 383 |
&shared_secret, |
| 384 |
&CONN_SECRET(conn, extract_secret))); |
| 385 |
|
| 386 |
return S2N_RESULT_OK; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
static S2N_RESULT s2n_derive_client_handshake_traffic_secret(struct s2n_connection *conn, struct s2n_blob *output) |
| 397 |
{ |
| 398 |
RESULT_ENSURE_REF(conn); |
| 399 |
RESULT_ENSURE_REF(output); |
| 400 |
|
| 401 |
RESULT_GUARD(s2n_derive_secret_with_context(conn, |
| 402 |
S2N_HANDSHAKE_SECRET, |
| 403 |
&s2n_tls13_label_client_handshake_traffic_secret, |
| 404 |
SERVER_HELLO, |
| 405 |
output)); |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
RESULT_GUARD(s2n_tls13_compute_finished_key(conn, |
| 416 |
output, &CONN_FINISHED(conn, client))); |
| 417 |
|
| 418 |
return S2N_RESULT_OK; |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
static S2N_RESULT s2n_derive_server_handshake_traffic_secret(struct s2n_connection *conn, struct s2n_blob *output) |
| 429 |
{ |
| 430 |
RESULT_ENSURE_REF(conn); |
| 431 |
RESULT_ENSURE_REF(output); |
| 432 |
|
| 433 |
RESULT_GUARD(s2n_derive_secret_with_context(conn, |
| 434 |
S2N_HANDSHAKE_SECRET, |
| 435 |
&s2n_tls13_label_server_handshake_traffic_secret, |
| 436 |
SERVER_HELLO, |
| 437 |
output)); |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
RESULT_GUARD(s2n_tls13_compute_finished_key(conn, |
| 448 |
output, &CONN_FINISHED(conn, server))); |
| 449 |
|
| 450 |
return S2N_RESULT_OK; |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
static S2N_RESULT s2n_extract_master_secret(struct s2n_connection *conn) |
| 462 |
{ |
| 463 |
RESULT_ENSURE_REF(conn); |
| 464 |
|
| 465 |
struct s2n_blob derived_secret = { 0 }; |
| 466 |
uint8_t derived_secret_bytes[S2N_TLS13_SECRET_MAX_LEN] = { 0 }; |
| 467 |
RESULT_GUARD_POSIX(s2n_blob_init(&derived_secret, derived_secret_bytes, S2N_TLS13_SECRET_MAX_LEN)); |
| 468 |
RESULT_GUARD(s2n_derive_secret_without_context(conn, S2N_HANDSHAKE_SECRET, &derived_secret)); |
| 469 |
|
| 470 |
RESULT_GUARD(s2n_extract_secret(CONN_HMAC_ALG(conn), |
| 471 |
&derived_secret, |
| 472 |
&ZERO_VALUE(CONN_HMAC_ALG(conn)), |
| 473 |
&CONN_SECRET(conn, extract_secret))); |
| 474 |
return S2N_RESULT_OK; |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
static S2N_RESULT s2n_derive_client_application_traffic_secret(struct s2n_connection *conn, struct s2n_blob *output) |
| 485 |
{ |
| 486 |
RESULT_GUARD(s2n_derive_secret_with_context(conn, |
| 487 |
S2N_MASTER_SECRET, |
| 488 |
&s2n_tls13_label_client_application_traffic_secret, |
| 489 |
SERVER_FINISHED, |
| 490 |
output)); |
| 491 |
return S2N_RESULT_OK; |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
static S2N_RESULT s2n_derive_server_application_traffic_secret(struct s2n_connection *conn, struct s2n_blob *output) |
| 502 |
{ |
| 503 |
RESULT_GUARD(s2n_derive_secret_with_context(conn, |
| 504 |
S2N_MASTER_SECRET, |
| 505 |
&s2n_tls13_label_server_application_traffic_secret, |
| 506 |
SERVER_FINISHED, |
| 507 |
output)); |
| 508 |
return S2N_RESULT_OK; |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
S2N_RESULT s2n_derive_resumption_master_secret(struct s2n_connection *conn) |
| 519 |
{ |
| 520 |
RESULT_ENSURE_REF(conn); |
| 521 |
|
| 522 |
RESULT_ENSURE_REF(conn->secure); |
| 523 |
RESULT_ENSURE_REF(conn->secure->cipher_suite); |
| 524 |
|
| 525 |
RESULT_GUARD(s2n_derive_secret_with_context(conn, |
| 526 |
S2N_MASTER_SECRET, |
| 527 |
&s2n_tls13_label_resumption_master_secret, |
| 528 |
CLIENT_FINISHED, |
| 529 |
&CONN_SECRET(conn, resumption_master_secret))); |
| 530 |
return S2N_RESULT_OK; |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
S2N_RESULT s2n_derive_exporter_master_secret(struct s2n_connection *conn, struct s2n_blob *secret) |
| 541 |
{ |
| 542 |
RESULT_ENSURE_REF(conn); |
| 543 |
|
| 544 |
RESULT_ENSURE_REF(conn->secure); |
| 545 |
RESULT_ENSURE_REF(conn->secure->cipher_suite); |
| 546 |
|
| 547 |
RESULT_GUARD(s2n_derive_secret_with_context(conn, |
| 548 |
S2N_MASTER_SECRET, |
| 549 |
&s2n_tls13_label_exporter_master_secret, |
| 550 |
SERVER_FINISHED, |
| 551 |
secret)); |
| 552 |
|
| 553 |
RESULT_GUARD(s2n_call_secret_callbacks(conn, secret, S2N_EXPORTER_SECRET)); |
| 554 |
|
| 555 |
return S2N_RESULT_OK; |
| 556 |
} |
| 557 |
|
| 558 |
static s2n_result (*extract_methods[])(struct s2n_connection *conn) = { |
| 559 |
[S2N_EARLY_SECRET] = &s2n_extract_early_secret_for_schedule, |
| 560 |
[S2N_HANDSHAKE_SECRET] = &s2n_extract_handshake_secret, |
| 561 |
[S2N_MASTER_SECRET] = &s2n_extract_master_secret, |
| 562 |
}; |
| 563 |
|
| 564 |
S2N_RESULT s2n_tls13_extract_secret(struct s2n_connection *conn, s2n_extract_secret_type_t secret_type) |
| 565 |
{ |
| 566 |
RESULT_ENSURE_REF(conn); |
| 567 |
RESULT_ENSURE_REF(conn->secure); |
| 568 |
RESULT_ENSURE_REF(conn->secure->cipher_suite); |
| 569 |
RESULT_ENSURE_REF(conn->handshake.hashes); |
| 570 |
RESULT_ENSURE_NE(secret_type, S2N_NONE_SECRET); |
| 571 |
|
| 572 |
RESULT_ENSURE_GTE(secret_type, 0); |
| 573 |
RESULT_ENSURE_LT(secret_type, s2n_array_len(extract_methods)); |
| 574 |
|
| 575 |
s2n_extract_secret_type_t next_secret_type = conn->secrets.extract_secret_type + 1; |
| 576 |
for (s2n_extract_secret_type_t i = next_secret_type; i <= secret_type; i++) { |
| 577 |
RESULT_ENSURE_REF(extract_methods[i]); |
| 578 |
RESULT_GUARD(extract_methods[i](conn)); |
| 579 |
conn->secrets.extract_secret_type = i; |
| 580 |
} |
| 581 |
|
| 582 |
return S2N_RESULT_OK; |
| 583 |
} |
| 584 |
|
| 585 |
static s2n_result (*derive_methods[][2])(struct s2n_connection *conn, struct s2n_blob *secret) = { |
| 586 |
[S2N_EARLY_SECRET] = { &s2n_derive_client_early_traffic_secret, &s2n_derive_client_early_traffic_secret }, |
| 587 |
[S2N_HANDSHAKE_SECRET] = { &s2n_derive_server_handshake_traffic_secret, &s2n_derive_client_handshake_traffic_secret }, |
| 588 |
[S2N_MASTER_SECRET] = { &s2n_derive_server_application_traffic_secret, &s2n_derive_client_application_traffic_secret }, |
| 589 |
}; |
| 590 |
|
| 591 |
S2N_RESULT s2n_tls13_derive_secret(struct s2n_connection *conn, s2n_extract_secret_type_t secret_type, |
| 592 |
s2n_mode mode, struct s2n_blob *secret) |
| 593 |
{ |
| 594 |
RESULT_ENSURE_REF(conn); |
| 595 |
RESULT_ENSURE_REF(secret); |
| 596 |
RESULT_ENSURE_REF(conn->secure); |
| 597 |
RESULT_ENSURE_REF(conn->secure->cipher_suite); |
| 598 |
RESULT_ENSURE_REF(conn->handshake.hashes); |
| 599 |
RESULT_ENSURE_NE(secret_type, S2N_NONE_SECRET); |
| 600 |
|
| 601 |
RESULT_GUARD(s2n_tls13_extract_secret(conn, secret_type)); |
| 602 |
|
| 603 |
RESULT_ENSURE_GTE(secret_type, 0); |
| 604 |
RESULT_ENSURE_LT(secret_type, s2n_array_len(derive_methods)); |
| 605 |
RESULT_ENSURE_REF(derive_methods[secret_type][mode]); |
| 606 |
RESULT_GUARD(derive_methods[secret_type][mode](conn, secret)); |
| 607 |
|
| 608 |
RESULT_GUARD(s2n_trigger_secret_callbacks(conn, secret, secret_type, mode)); |
| 609 |
return S2N_RESULT_OK; |
| 610 |
} |
| 611 |
|
| 612 |
S2N_RESULT s2n_tls13_secrets_clean(struct s2n_connection *conn) |
| 613 |
{ |
| 614 |
RESULT_ENSURE_REF(conn); |
| 615 |
|
| 616 |
RESULT_ENSURE_REF(conn->secure); |
| 617 |
RESULT_ENSURE_REF(conn->secure->cipher_suite); |
| 618 |
|
| 619 |
if (conn->actual_protocol_version < S2N_TLS13) { |
| 620 |
return S2N_RESULT_OK; |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
RESULT_GUARD_POSIX(s2n_blob_zero(&CONN_SECRET(conn, extract_secret))); |
| 630 |
conn->secrets.extract_secret_type = S2N_NONE_SECRET; |
| 631 |
|
| 632 |
|
| 633 |
RESULT_GUARD_POSIX(s2n_blob_zero(&CONN_SECRET(conn, client_early_secret))); |
| 634 |
RESULT_GUARD_POSIX(s2n_blob_zero(&CONN_SECRET(conn, client_handshake_secret))); |
| 635 |
RESULT_GUARD_POSIX(s2n_blob_zero(&CONN_SECRET(conn, server_handshake_secret))); |
| 636 |
|
| 637 |
return S2N_RESULT_OK; |
| 638 |
} |
| 639 |
|
| 640 |
S2N_RESULT s2n_tls13_secrets_update(struct s2n_connection *conn) |
| 641 |
{ |
| 642 |
RESULT_ENSURE_REF(conn); |
| 643 |
if (s2n_connection_get_protocol_version(conn) < S2N_TLS13) { |
| 644 |
return S2N_RESULT_OK; |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
RESULT_ENSURE_REF(conn->secure); |
| 649 |
RESULT_ENSURE_REF(conn->secure->cipher_suite); |
| 650 |
|
| 651 |
message_type_t message_type = s2n_conn_get_current_message_type(conn); |
| 652 |
switch (message_type) { |
| 653 |
case CLIENT_HELLO: |
| 654 |
if (conn->early_data_state == S2N_EARLY_DATA_REQUESTED |
| 655 |
|| conn->early_data_state == S2N_EARLY_DATA_ACCEPTED) { |
| 656 |
RESULT_GUARD(s2n_calculate_transcript_digest(conn)); |
| 657 |
RESULT_GUARD(s2n_tls13_derive_secret(conn, S2N_EARLY_SECRET, |
| 658 |
S2N_CLIENT, &CONN_SECRET(conn, client_early_secret))); |
| 659 |
} |
| 660 |
break; |
| 661 |
case SERVER_HELLO: |
| 662 |
RESULT_GUARD(s2n_calculate_transcript_digest(conn)); |
| 663 |
RESULT_GUARD(s2n_tls13_derive_secret(conn, S2N_HANDSHAKE_SECRET, |
| 664 |
S2N_CLIENT, &CONN_SECRET(conn, client_handshake_secret))); |
| 665 |
RESULT_GUARD(s2n_tls13_derive_secret(conn, S2N_HANDSHAKE_SECRET, |
| 666 |
S2N_SERVER, &CONN_SECRET(conn, server_handshake_secret))); |
| 667 |
break; |
| 668 |
case SERVER_FINISHED: |
| 669 |
RESULT_GUARD(s2n_calculate_transcript_digest(conn)); |
| 670 |
RESULT_GUARD(s2n_tls13_derive_secret(conn, S2N_MASTER_SECRET, |
| 671 |
S2N_CLIENT, &CONN_SECRET(conn, client_app_secret))); |
| 672 |
RESULT_GUARD(s2n_tls13_derive_secret(conn, S2N_MASTER_SECRET, |
| 673 |
S2N_SERVER, &CONN_SECRET(conn, server_app_secret))); |
| 674 |
RESULT_GUARD(s2n_derive_exporter_master_secret(conn, |
| 675 |
&CONN_SECRET(conn, exporter_master_secret))); |
| 676 |
break; |
| 677 |
case CLIENT_FINISHED: |
| 678 |
RESULT_GUARD(s2n_calculate_transcript_digest(conn)); |
| 679 |
RESULT_GUARD(s2n_derive_resumption_master_secret(conn)); |
| 680 |
break; |
| 681 |
default: |
| 682 |
break; |
| 683 |
} |
| 684 |
return S2N_RESULT_OK; |
| 685 |
} |
| 686 |
|
| 687 |
S2N_RESULT s2n_tls13_secrets_get(struct s2n_connection *conn, s2n_extract_secret_type_t secret_type, |
| 688 |
s2n_mode mode, struct s2n_blob *secret) |
| 689 |
{ |
| 690 |
RESULT_ENSURE_REF(conn); |
| 691 |
RESULT_ENSURE_REF(secret); |
| 692 |
|
| 693 |
RESULT_ENSURE_REF(conn->secure); |
| 694 |
RESULT_ENSURE_REF(conn->secure->cipher_suite); |
| 695 |
|
| 696 |
bool is_available = (secret_type <= conn->secrets.extract_secret_type) |
| 697 |
|
| 698 |
|| (secret_type == S2N_MASTER_SECRET && s2n_handshake_is_complete(conn)); |
| 699 |
RESULT_ENSURE(is_available, S2N_ERR_SAFETY); |
| 700 |
|
| 701 |
uint8_t *secrets[][2] = { |
| 702 |
[S2N_EARLY_SECRET] = { NULL, CONN_SECRETS(conn).client_early_secret }, |
| 703 |
[S2N_HANDSHAKE_SECRET] = { CONN_SECRETS(conn).server_handshake_secret, CONN_SECRETS(conn).client_handshake_secret }, |
| 704 |
[S2N_MASTER_SECRET] = { CONN_SECRETS(conn).server_app_secret, CONN_SECRETS(conn).client_app_secret }, |
| 705 |
}; |
| 706 |
RESULT_ENSURE_GT(secret_type, S2N_NONE_SECRET); |
| 707 |
RESULT_ENSURE_LT(secret_type, s2n_array_len(secrets)); |
| 708 |
RESULT_ENSURE_REF(secrets[secret_type][mode]); |
| 709 |
|
| 710 |
secret->size = s2n_get_hash_len(CONN_HMAC_ALG(conn)); |
| 711 |
RESULT_CHECKED_MEMCPY(secret->data, secrets[secret_type][mode], secret->size); |
| 712 |
RESULT_ENSURE_GT(secret->size, 0); |
| 713 |
return S2N_RESULT_OK; |
| 714 |
} |
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
int s2n_connection_tls_exporter(struct s2n_connection *conn, |
| 725 |
const uint8_t *label_in, uint32_t label_length, |
| 726 |
const uint8_t *context, uint32_t context_length, |
| 727 |
uint8_t *output_in, uint32_t output_length) |
| 728 |
{ |
| 729 |
POSIX_ENSURE_REF(conn); |
| 730 |
POSIX_ENSURE_REF(output_in); |
| 731 |
POSIX_ENSURE_REF(label_in); |
| 732 |
POSIX_ENSURE_REF(context); |
| 733 |
POSIX_ENSURE(s2n_connection_get_protocol_version(conn) == S2N_TLS13, S2N_ERR_INVALID_STATE); |
| 734 |
|
| 735 |
POSIX_ENSURE(is_handshake_complete(conn), S2N_ERR_HANDSHAKE_NOT_COMPLETE); |
| 736 |
POSIX_ENSURE_REF(conn->secure); |
| 737 |
POSIX_ENSURE_REF(conn->secure->cipher_suite); |
| 738 |
s2n_hmac_algorithm hmac_alg = conn->secure->cipher_suite->prf_alg; |
| 739 |
|
| 740 |
uint8_t label_bytes[S2N_MAX_HKDF_EXPAND_LABEL_LENGTH] = { 0 }; |
| 741 |
struct s2n_blob label = { 0 }; |
| 742 |
POSIX_ENSURE_LTE(label_length, sizeof(label_bytes)); |
| 743 |
POSIX_CHECKED_MEMCPY(label_bytes, label_in, label_length); |
| 744 |
POSIX_GUARD(s2n_blob_init(&label, label_bytes, label_length)); |
| 745 |
|
| 746 |
uint8_t derived_secret_bytes[S2N_MAX_DIGEST_LEN] = { 0 }; |
| 747 |
struct s2n_blob derived_secret = { 0 }; |
| 748 |
POSIX_ENSURE_LTE(s2n_get_hash_len(CONN_HMAC_ALG(conn)), S2N_MAX_DIGEST_LEN); |
| 749 |
POSIX_GUARD(s2n_blob_init(&derived_secret, |
| 750 |
derived_secret_bytes, s2n_get_hash_len(CONN_HMAC_ALG(conn)))); |
| 751 |
POSIX_GUARD_RESULT(s2n_derive_secret(hmac_alg, &CONN_SECRET(conn, exporter_master_secret), |
| 752 |
&label, &EMPTY_CONTEXT(hmac_alg), &derived_secret)); |
| 753 |
|
| 754 |
DEFER_CLEANUP(struct s2n_hmac_state hmac_state = { 0 }, s2n_hmac_free); |
| 755 |
POSIX_GUARD(s2n_hmac_new(&hmac_state)); |
| 756 |
|
| 757 |
DEFER_CLEANUP(struct s2n_hash_state hash = { 0 }, s2n_hash_free); |
| 758 |
POSIX_GUARD(s2n_hash_new(&hash)); |
| 759 |
|
| 760 |
s2n_hash_algorithm hash_alg = { 0 }; |
| 761 |
POSIX_GUARD(s2n_hmac_hash_alg(hmac_alg, &hash_alg)); |
| 762 |
uint8_t digest_bytes[S2N_MAX_DIGEST_LEN] = { 0 }; |
| 763 |
struct s2n_blob digest = { 0 }; |
| 764 |
POSIX_ENSURE_LTE(s2n_get_hash_len(CONN_HMAC_ALG(conn)), S2N_MAX_DIGEST_LEN); |
| 765 |
POSIX_GUARD(s2n_blob_init(&digest, digest_bytes, s2n_get_hash_len(CONN_HMAC_ALG(conn)))); |
| 766 |
|
| 767 |
POSIX_GUARD(s2n_hash_init(&hash, hash_alg)); |
| 768 |
POSIX_GUARD(s2n_hash_update(&hash, context, context_length)); |
| 769 |
POSIX_GUARD(s2n_hash_digest(&hash, digest.data, digest.size)); |
| 770 |
|
| 771 |
struct s2n_blob output = { 0 }; |
| 772 |
POSIX_GUARD(s2n_blob_init(&output, output_in, output_length)); |
| 773 |
POSIX_GUARD(s2n_hkdf_expand_label(&hmac_state, hmac_alg, |
| 774 |
&derived_secret, &s2n_tls13_label_exporter, &digest, &output)); |
| 775 |
|
| 776 |
return S2N_SUCCESS; |
| 777 |
} |