diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/libssl/tls12_record_layer.c | 176 |
1 files changed, 101 insertions, 75 deletions
diff --git a/src/lib/libssl/tls12_record_layer.c b/src/lib/libssl/tls12_record_layer.c index 600b73987e..2b331355be 100644 --- a/src/lib/libssl/tls12_record_layer.c +++ b/src/lib/libssl/tls12_record_layer.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: tls12_record_layer.c,v 1.7 2021/01/07 15:37:19 jsing Exp $ */ | 1 | /* $OpenBSD: tls12_record_layer.c,v 1.8 2021/01/12 17:47:20 jsing Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2020 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2020 Joel Sing <jsing@openbsd.org> |
| 4 | * | 4 | * |
| @@ -21,35 +21,51 @@ | |||
| 21 | 21 | ||
| 22 | #include "ssl_locl.h" | 22 | #include "ssl_locl.h" |
| 23 | 23 | ||
| 24 | struct tls12_record_layer { | 24 | struct tls12_record_protection { |
| 25 | uint16_t version; | 25 | uint16_t epoch; |
| 26 | int dtls; | ||
| 27 | |||
| 28 | uint8_t alert_desc; | ||
| 29 | |||
| 30 | uint16_t read_epoch; | ||
| 31 | uint16_t write_epoch; | ||
| 32 | 26 | ||
| 33 | int read_stream_mac; | 27 | int stream_mac; |
| 34 | int write_stream_mac; | ||
| 35 | 28 | ||
| 36 | uint8_t *read_mac_key; | 29 | uint8_t *mac_key; |
| 37 | size_t read_mac_key_len; | 30 | size_t mac_key_len; |
| 38 | 31 | ||
| 39 | /* | 32 | /* |
| 40 | * XXX - for now these are just pointers to externally managed | 33 | * XXX - for now these are just pointers to externally managed |
| 41 | * structs/memory. These should eventually be owned by the record layer. | 34 | * structs/memory. These should eventually be owned by the record layer. |
| 42 | */ | 35 | */ |
| 43 | SSL_AEAD_CTX *read_aead_ctx; | 36 | SSL_AEAD_CTX *aead_ctx; |
| 44 | SSL_AEAD_CTX *write_aead_ctx; | 37 | |
| 38 | EVP_CIPHER_CTX *cipher_ctx; | ||
| 39 | EVP_MD_CTX *hash_ctx; | ||
| 40 | |||
| 41 | uint8_t *seq_num; | ||
| 42 | }; | ||
| 43 | |||
| 44 | static struct tls12_record_protection * | ||
| 45 | tls12_record_protection_new(void) | ||
| 46 | { | ||
| 47 | return calloc(1, sizeof(struct tls12_record_protection)); | ||
| 48 | } | ||
| 49 | |||
| 50 | static void | ||
| 51 | tls12_record_protection_free(struct tls12_record_protection *rp) | ||
| 52 | { | ||
| 53 | if (rp == NULL) | ||
| 54 | return; | ||
| 55 | |||
| 56 | freezero(rp->mac_key, rp->mac_key_len); | ||
| 57 | |||
| 58 | freezero(rp, sizeof(struct tls12_record_protection)); | ||
| 59 | } | ||
| 45 | 60 | ||
| 46 | EVP_CIPHER_CTX *read_cipher_ctx; | 61 | struct tls12_record_layer { |
| 47 | EVP_MD_CTX *read_hash_ctx; | 62 | uint16_t version; |
| 48 | EVP_CIPHER_CTX *write_cipher_ctx; | 63 | int dtls; |
| 49 | EVP_MD_CTX *write_hash_ctx; | ||
| 50 | 64 | ||
| 51 | uint8_t *read_seq_num; | 65 | uint8_t alert_desc; |
| 52 | uint8_t *write_seq_num; | 66 | |
| 67 | struct tls12_record_protection *read; | ||
| 68 | struct tls12_record_protection *write; | ||
| 53 | }; | 69 | }; |
| 54 | 70 | ||
| 55 | struct tls12_record_layer * | 71 | struct tls12_record_layer * |
| @@ -58,9 +74,18 @@ tls12_record_layer_new(void) | |||
| 58 | struct tls12_record_layer *rl; | 74 | struct tls12_record_layer *rl; |
| 59 | 75 | ||
| 60 | if ((rl = calloc(1, sizeof(struct tls12_record_layer))) == NULL) | 76 | if ((rl = calloc(1, sizeof(struct tls12_record_layer))) == NULL) |
| 61 | return NULL; | 77 | goto err; |
| 78 | if ((rl->read = tls12_record_protection_new()) == NULL) | ||
| 79 | goto err; | ||
| 80 | if ((rl->write = tls12_record_protection_new()) == NULL) | ||
| 81 | goto err; | ||
| 62 | 82 | ||
| 63 | return rl; | 83 | return rl; |
| 84 | |||
| 85 | err: | ||
| 86 | tls12_record_layer_free(rl); | ||
| 87 | |||
| 88 | return NULL; | ||
| 64 | } | 89 | } |
| 65 | 90 | ||
| 66 | void | 91 | void |
| @@ -69,7 +94,8 @@ tls12_record_layer_free(struct tls12_record_layer *rl) | |||
| 69 | if (rl == NULL) | 94 | if (rl == NULL) |
| 70 | return; | 95 | return; |
| 71 | 96 | ||
| 72 | freezero(rl->read_mac_key, rl->read_mac_key_len); | 97 | tls12_record_protection_free(rl->read); |
| 98 | tls12_record_protection_free(rl->write); | ||
| 73 | 99 | ||
| 74 | freezero(rl, sizeof(struct tls12_record_layer)); | 100 | freezero(rl, sizeof(struct tls12_record_layer)); |
| 75 | } | 101 | } |
| @@ -90,13 +116,13 @@ tls12_record_layer_set_version(struct tls12_record_layer *rl, uint16_t version) | |||
| 90 | void | 116 | void |
| 91 | tls12_record_layer_set_read_epoch(struct tls12_record_layer *rl, uint16_t epoch) | 117 | tls12_record_layer_set_read_epoch(struct tls12_record_layer *rl, uint16_t epoch) |
| 92 | { | 118 | { |
| 93 | rl->read_epoch = epoch; | 119 | rl->read->epoch = epoch; |
| 94 | } | 120 | } |
| 95 | 121 | ||
| 96 | void | 122 | void |
| 97 | tls12_record_layer_set_write_epoch(struct tls12_record_layer *rl, uint16_t epoch) | 123 | tls12_record_layer_set_write_epoch(struct tls12_record_layer *rl, uint16_t epoch) |
| 98 | { | 124 | { |
| 99 | rl->write_epoch = epoch; | 125 | rl->write->epoch = epoch; |
| 100 | } | 126 | } |
| 101 | 127 | ||
| 102 | static void | 128 | static void |
| @@ -104,11 +130,11 @@ tls12_record_layer_set_read_state(struct tls12_record_layer *rl, | |||
| 104 | SSL_AEAD_CTX *aead_ctx, EVP_CIPHER_CTX *cipher_ctx, EVP_MD_CTX *hash_ctx, | 130 | SSL_AEAD_CTX *aead_ctx, EVP_CIPHER_CTX *cipher_ctx, EVP_MD_CTX *hash_ctx, |
| 105 | int stream_mac) | 131 | int stream_mac) |
| 106 | { | 132 | { |
| 107 | rl->read_aead_ctx = aead_ctx; | 133 | rl->read->aead_ctx = aead_ctx; |
| 108 | 134 | ||
| 109 | rl->read_cipher_ctx = cipher_ctx; | 135 | rl->read->cipher_ctx = cipher_ctx; |
| 110 | rl->read_hash_ctx = hash_ctx; | 136 | rl->read->hash_ctx = hash_ctx; |
| 111 | rl->read_stream_mac = stream_mac; | 137 | rl->read->stream_mac = stream_mac; |
| 112 | } | 138 | } |
| 113 | 139 | ||
| 114 | static void | 140 | static void |
| @@ -116,11 +142,11 @@ tls12_record_layer_set_write_state(struct tls12_record_layer *rl, | |||
| 116 | SSL_AEAD_CTX *aead_ctx, EVP_CIPHER_CTX *cipher_ctx, EVP_MD_CTX *hash_ctx, | 142 | SSL_AEAD_CTX *aead_ctx, EVP_CIPHER_CTX *cipher_ctx, EVP_MD_CTX *hash_ctx, |
| 117 | int stream_mac) | 143 | int stream_mac) |
| 118 | { | 144 | { |
| 119 | rl->write_aead_ctx = aead_ctx; | 145 | rl->write->aead_ctx = aead_ctx; |
| 120 | 146 | ||
| 121 | rl->write_cipher_ctx = cipher_ctx; | 147 | rl->write->cipher_ctx = cipher_ctx; |
| 122 | rl->write_hash_ctx = hash_ctx; | 148 | rl->write->hash_ctx = hash_ctx; |
| 123 | rl->write_stream_mac = stream_mac; | 149 | rl->write->stream_mac = stream_mac; |
| 124 | } | 150 | } |
| 125 | 151 | ||
| 126 | void | 152 | void |
| @@ -128,28 +154,28 @@ tls12_record_layer_clear_read_state(struct tls12_record_layer *rl) | |||
| 128 | { | 154 | { |
| 129 | tls12_record_layer_set_read_state(rl, NULL, NULL, NULL, 0); | 155 | tls12_record_layer_set_read_state(rl, NULL, NULL, NULL, 0); |
| 130 | tls12_record_layer_set_read_mac_key(rl, NULL, 0); | 156 | tls12_record_layer_set_read_mac_key(rl, NULL, 0); |
| 131 | rl->read_seq_num = NULL; | 157 | rl->read->seq_num = NULL; |
| 132 | } | 158 | } |
| 133 | 159 | ||
| 134 | void | 160 | void |
| 135 | tls12_record_layer_clear_write_state(struct tls12_record_layer *rl) | 161 | tls12_record_layer_clear_write_state(struct tls12_record_layer *rl) |
| 136 | { | 162 | { |
| 137 | tls12_record_layer_set_write_state(rl, NULL, NULL, NULL, 0); | 163 | tls12_record_layer_set_write_state(rl, NULL, NULL, NULL, 0); |
| 138 | rl->write_seq_num = NULL; | 164 | rl->write->seq_num = NULL; |
| 139 | } | 165 | } |
| 140 | 166 | ||
| 141 | void | 167 | void |
| 142 | tls12_record_layer_set_read_seq_num(struct tls12_record_layer *rl, | 168 | tls12_record_layer_set_read_seq_num(struct tls12_record_layer *rl, |
| 143 | uint8_t *seq_num) | 169 | uint8_t *seq_num) |
| 144 | { | 170 | { |
| 145 | rl->read_seq_num = seq_num; | 171 | rl->read->seq_num = seq_num; |
| 146 | } | 172 | } |
| 147 | 173 | ||
| 148 | void | 174 | void |
| 149 | tls12_record_layer_set_write_seq_num(struct tls12_record_layer *rl, | 175 | tls12_record_layer_set_write_seq_num(struct tls12_record_layer *rl, |
| 150 | uint8_t *seq_num) | 176 | uint8_t *seq_num) |
| 151 | { | 177 | { |
| 152 | rl->write_seq_num = seq_num; | 178 | rl->write->seq_num = seq_num; |
| 153 | } | 179 | } |
| 154 | 180 | ||
| 155 | int | 181 | int |
| @@ -194,18 +220,18 @@ int | |||
| 194 | tls12_record_layer_set_read_mac_key(struct tls12_record_layer *rl, | 220 | tls12_record_layer_set_read_mac_key(struct tls12_record_layer *rl, |
| 195 | const uint8_t *mac_key, size_t mac_key_len) | 221 | const uint8_t *mac_key, size_t mac_key_len) |
| 196 | { | 222 | { |
| 197 | freezero(rl->read_mac_key, rl->read_mac_key_len); | 223 | freezero(rl->read->mac_key, rl->read->mac_key_len); |
| 198 | rl->read_mac_key = NULL; | 224 | rl->read->mac_key = NULL; |
| 199 | rl->read_mac_key_len = 0; | 225 | rl->read->mac_key_len = 0; |
| 200 | 226 | ||
| 201 | if (mac_key == NULL || mac_key_len == 0) | 227 | if (mac_key == NULL || mac_key_len == 0) |
| 202 | return 1; | 228 | return 1; |
| 203 | 229 | ||
| 204 | if ((rl->read_mac_key = calloc(1, mac_key_len)) == NULL) | 230 | if ((rl->read->mac_key = calloc(1, mac_key_len)) == NULL) |
| 205 | return 0; | 231 | return 0; |
| 206 | 232 | ||
| 207 | memcpy(rl->read_mac_key, mac_key, mac_key_len); | 233 | memcpy(rl->read->mac_key, mac_key, mac_key_len); |
| 208 | rl->read_mac_key_len = mac_key_len; | 234 | rl->read->mac_key_len = mac_key_len; |
| 209 | 235 | ||
| 210 | return 1; | 236 | return 1; |
| 211 | } | 237 | } |
| @@ -328,19 +354,19 @@ tls12_record_layer_read_mac_cbc(struct tls12_record_layer *rl, CBB *cbb, | |||
| 328 | * Must be constant time to avoid leaking details about CBC padding. | 354 | * Must be constant time to avoid leaking details about CBC padding. |
| 329 | */ | 355 | */ |
| 330 | 356 | ||
| 331 | if (!ssl3_cbc_record_digest_supported(rl->read_hash_ctx)) | 357 | if (!ssl3_cbc_record_digest_supported(rl->read->hash_ctx)) |
| 332 | goto err; | 358 | goto err; |
| 333 | 359 | ||
| 334 | if (!tls12_record_layer_pseudo_header(rl, content_type, content_len, | 360 | if (!tls12_record_layer_pseudo_header(rl, content_type, content_len, |
| 335 | rl->read_epoch, rl->read_seq_num, SSL3_SEQUENCE_SIZE, | 361 | rl->read->epoch, rl->read->seq_num, SSL3_SEQUENCE_SIZE, |
| 336 | &header, &header_len)) | 362 | &header, &header_len)) |
| 337 | goto err; | 363 | goto err; |
| 338 | 364 | ||
| 339 | if (!CBB_add_space(cbb, &mac, mac_len)) | 365 | if (!CBB_add_space(cbb, &mac, mac_len)) |
| 340 | goto err; | 366 | goto err; |
| 341 | if (!ssl3_cbc_digest_record(rl->read_hash_ctx, mac, &out_mac_len, header, | 367 | if (!ssl3_cbc_digest_record(rl->read->hash_ctx, mac, &out_mac_len, header, |
| 342 | content, content_len + mac_len, content_len + mac_len + padding_len, | 368 | content, content_len + mac_len, content_len + mac_len + padding_len, |
| 343 | rl->read_mac_key, rl->read_mac_key_len)) | 369 | rl->read->mac_key, rl->read->mac_key_len)) |
| 344 | goto err; | 370 | goto err; |
| 345 | if (mac_len != out_mac_len) | 371 | if (mac_len != out_mac_len) |
| 346 | goto err; | 372 | goto err; |
| @@ -357,14 +383,14 @@ static int | |||
| 357 | tls12_record_layer_read_mac(struct tls12_record_layer *rl, CBB *cbb, | 383 | tls12_record_layer_read_mac(struct tls12_record_layer *rl, CBB *cbb, |
| 358 | uint8_t content_type, const uint8_t *content, size_t content_len) | 384 | uint8_t content_type, const uint8_t *content, size_t content_len) |
| 359 | { | 385 | { |
| 360 | EVP_CIPHER_CTX *enc = rl->read_cipher_ctx; | 386 | EVP_CIPHER_CTX *enc = rl->read->cipher_ctx; |
| 361 | size_t out_len; | 387 | size_t out_len; |
| 362 | 388 | ||
| 363 | if (EVP_CIPHER_CTX_mode(enc) == EVP_CIPH_CBC_MODE) | 389 | if (EVP_CIPHER_CTX_mode(enc) == EVP_CIPH_CBC_MODE) |
| 364 | return 0; | 390 | return 0; |
| 365 | 391 | ||
| 366 | return tls12_record_layer_mac(rl, cbb, rl->read_hash_ctx, | 392 | return tls12_record_layer_mac(rl, cbb, rl->read->hash_ctx, |
| 367 | rl->read_stream_mac, rl->read_epoch, rl->read_seq_num, | 393 | rl->read->stream_mac, rl->read->epoch, rl->read->seq_num, |
| 368 | SSL3_SEQUENCE_SIZE, content_type, content, content_len, &out_len); | 394 | SSL3_SEQUENCE_SIZE, content_type, content, content_len, &out_len); |
| 369 | } | 395 | } |
| 370 | 396 | ||
| @@ -373,8 +399,8 @@ tls12_record_layer_write_mac(struct tls12_record_layer *rl, CBB *cbb, | |||
| 373 | uint8_t content_type, const uint8_t *content, size_t content_len, | 399 | uint8_t content_type, const uint8_t *content, size_t content_len, |
| 374 | size_t *out_len) | 400 | size_t *out_len) |
| 375 | { | 401 | { |
| 376 | return tls12_record_layer_mac(rl, cbb, rl->write_hash_ctx, | 402 | return tls12_record_layer_mac(rl, cbb, rl->write->hash_ctx, |
| 377 | rl->write_stream_mac, rl->write_epoch, rl->write_seq_num, | 403 | rl->write->stream_mac, rl->write->epoch, rl->write->seq_num, |
| 378 | SSL3_SEQUENCE_SIZE, content_type, content, content_len, out_len); | 404 | SSL3_SEQUENCE_SIZE, content_type, content, content_len, out_len); |
| 379 | } | 405 | } |
| 380 | 406 | ||
| @@ -456,7 +482,7 @@ static int | |||
| 456 | tls12_record_layer_open_record_plaintext(struct tls12_record_layer *rl, | 482 | tls12_record_layer_open_record_plaintext(struct tls12_record_layer *rl, |
| 457 | uint8_t content_type, CBS *fragment, uint8_t **out, size_t *out_len) | 483 | uint8_t content_type, CBS *fragment, uint8_t **out, size_t *out_len) |
| 458 | { | 484 | { |
| 459 | if (rl->read_aead_ctx != NULL || rl->read_cipher_ctx != NULL) | 485 | if (rl->read->aead_ctx != NULL || rl->read->cipher_ctx != NULL) |
| 460 | return 0; | 486 | return 0; |
| 461 | 487 | ||
| 462 | /* XXX - decrypt/process in place for now. */ | 488 | /* XXX - decrypt/process in place for now. */ |
| @@ -470,7 +496,7 @@ static int | |||
| 470 | tls12_record_layer_open_record_protected_aead(struct tls12_record_layer *rl, | 496 | tls12_record_layer_open_record_protected_aead(struct tls12_record_layer *rl, |
| 471 | uint8_t content_type, CBS *fragment, uint8_t **out, size_t *out_len) | 497 | uint8_t content_type, CBS *fragment, uint8_t **out, size_t *out_len) |
| 472 | { | 498 | { |
| 473 | const SSL_AEAD_CTX *aead = rl->read_aead_ctx; | 499 | const SSL_AEAD_CTX *aead = rl->read->aead_ctx; |
| 474 | uint8_t *header = NULL, *nonce = NULL; | 500 | uint8_t *header = NULL, *nonce = NULL; |
| 475 | size_t header_len = 0, nonce_len = 0; | 501 | size_t header_len = 0, nonce_len = 0; |
| 476 | uint8_t *plain; | 502 | uint8_t *plain; |
| @@ -482,7 +508,7 @@ tls12_record_layer_open_record_protected_aead(struct tls12_record_layer *rl, | |||
| 482 | /* XXX - move to nonce allocated in record layer, matching TLSv1.3 */ | 508 | /* XXX - move to nonce allocated in record layer, matching TLSv1.3 */ |
| 483 | if (aead->xor_fixed_nonce) { | 509 | if (aead->xor_fixed_nonce) { |
| 484 | if (!tls12_record_layer_aead_xored_nonce(rl, aead, | 510 | if (!tls12_record_layer_aead_xored_nonce(rl, aead, |
| 485 | rl->read_seq_num, &nonce, &nonce_len)) | 511 | rl->read->seq_num, &nonce, &nonce_len)) |
| 486 | goto err; | 512 | goto err; |
| 487 | } else if (aead->variable_nonce_in_record) { | 513 | } else if (aead->variable_nonce_in_record) { |
| 488 | if (!CBS_get_bytes(fragment, &var_nonce, | 514 | if (!CBS_get_bytes(fragment, &var_nonce, |
| @@ -493,7 +519,7 @@ tls12_record_layer_open_record_protected_aead(struct tls12_record_layer *rl, | |||
| 493 | goto err; | 519 | goto err; |
| 494 | } else { | 520 | } else { |
| 495 | if (!tls12_record_layer_aead_concat_nonce(rl, aead, | 521 | if (!tls12_record_layer_aead_concat_nonce(rl, aead, |
| 496 | rl->read_seq_num, &nonce, &nonce_len)) | 522 | rl->read->seq_num, &nonce, &nonce_len)) |
| 497 | goto err; | 523 | goto err; |
| 498 | } | 524 | } |
| 499 | 525 | ||
| @@ -512,7 +538,7 @@ tls12_record_layer_open_record_protected_aead(struct tls12_record_layer *rl, | |||
| 512 | plain_len = CBS_len(fragment) - aead->tag_len; | 538 | plain_len = CBS_len(fragment) - aead->tag_len; |
| 513 | 539 | ||
| 514 | if (!tls12_record_layer_pseudo_header(rl, content_type, plain_len, | 540 | if (!tls12_record_layer_pseudo_header(rl, content_type, plain_len, |
| 515 | epoch, rl->read_seq_num, SSL3_SEQUENCE_SIZE, &header, &header_len)) | 541 | epoch, rl->read->seq_num, SSL3_SEQUENCE_SIZE, &header, &header_len)) |
| 516 | goto err; | 542 | goto err; |
| 517 | 543 | ||
| 518 | if (!EVP_AEAD_CTX_open(&aead->ctx, plain, out_len, plain_len, | 544 | if (!EVP_AEAD_CTX_open(&aead->ctx, plain, out_len, plain_len, |
| @@ -545,7 +571,7 @@ static int | |||
| 545 | tls12_record_layer_open_record_protected_cipher(struct tls12_record_layer *rl, | 571 | tls12_record_layer_open_record_protected_cipher(struct tls12_record_layer *rl, |
| 546 | uint8_t content_type, CBS *fragment, uint8_t **out, size_t *out_len) | 572 | uint8_t content_type, CBS *fragment, uint8_t **out, size_t *out_len) |
| 547 | { | 573 | { |
| 548 | EVP_CIPHER_CTX *enc = rl->read_cipher_ctx; | 574 | EVP_CIPHER_CTX *enc = rl->read->cipher_ctx; |
| 549 | SSL3_RECORD_INTERNAL rrec; | 575 | SSL3_RECORD_INTERNAL rrec; |
| 550 | int block_size, eiv_len; | 576 | int block_size, eiv_len; |
| 551 | uint8_t *mac = NULL; | 577 | uint8_t *mac = NULL; |
| @@ -573,8 +599,8 @@ tls12_record_layer_open_record_protected_cipher(struct tls12_record_layer *rl, | |||
| 573 | goto err; | 599 | goto err; |
| 574 | 600 | ||
| 575 | mac_len = 0; | 601 | mac_len = 0; |
| 576 | if (rl->read_hash_ctx != NULL) { | 602 | if (rl->read->hash_ctx != NULL) { |
| 577 | mac_len = EVP_MD_CTX_size(rl->read_hash_ctx); | 603 | mac_len = EVP_MD_CTX_size(rl->read->hash_ctx); |
| 578 | if (mac_len <= 0 || mac_len > EVP_MAX_MD_SIZE) | 604 | if (mac_len <= 0 || mac_len > EVP_MAX_MD_SIZE) |
| 579 | goto err; | 605 | goto err; |
| 580 | } | 606 | } |
| @@ -689,11 +715,11 @@ tls12_record_layer_open_record(struct tls12_record_layer *rl, uint8_t *buf, | |||
| 689 | if (!CBS_get_u16_length_prefixed(&cbs, &fragment)) | 715 | if (!CBS_get_u16_length_prefixed(&cbs, &fragment)) |
| 690 | return 0; | 716 | return 0; |
| 691 | 717 | ||
| 692 | if (rl->read_aead_ctx != NULL) { | 718 | if (rl->read->aead_ctx != NULL) { |
| 693 | if (!tls12_record_layer_open_record_protected_aead(rl, | 719 | if (!tls12_record_layer_open_record_protected_aead(rl, |
| 694 | content_type, &fragment, out, out_len)) | 720 | content_type, &fragment, out, out_len)) |
| 695 | return 0; | 721 | return 0; |
| 696 | } else if (rl->read_cipher_ctx != NULL) { | 722 | } else if (rl->read->cipher_ctx != NULL) { |
| 697 | if (!tls12_record_layer_open_record_protected_cipher(rl, | 723 | if (!tls12_record_layer_open_record_protected_cipher(rl, |
| 698 | content_type, &fragment, out, out_len)) | 724 | content_type, &fragment, out, out_len)) |
| 699 | return 0; | 725 | return 0; |
| @@ -704,7 +730,7 @@ tls12_record_layer_open_record(struct tls12_record_layer *rl, uint8_t *buf, | |||
| 704 | } | 730 | } |
| 705 | 731 | ||
| 706 | if (!rl->dtls) | 732 | if (!rl->dtls) |
| 707 | tls1_record_sequence_increment(rl->read_seq_num); | 733 | tls1_record_sequence_increment(rl->read->seq_num); |
| 708 | 734 | ||
| 709 | return 1; | 735 | return 1; |
| 710 | } | 736 | } |
| @@ -713,7 +739,7 @@ static int | |||
| 713 | tls12_record_layer_seal_record_plaintext(struct tls12_record_layer *rl, | 739 | tls12_record_layer_seal_record_plaintext(struct tls12_record_layer *rl, |
| 714 | uint8_t content_type, const uint8_t *content, size_t content_len, CBB *out) | 740 | uint8_t content_type, const uint8_t *content, size_t content_len, CBB *out) |
| 715 | { | 741 | { |
| 716 | if (rl->write_aead_ctx != NULL || rl->write_cipher_ctx != NULL) | 742 | if (rl->write->aead_ctx != NULL || rl->write->cipher_ctx != NULL) |
| 717 | return 0; | 743 | return 0; |
| 718 | 744 | ||
| 719 | return CBB_add_bytes(out, content, content_len); | 745 | return CBB_add_bytes(out, content, content_len); |
| @@ -723,7 +749,7 @@ static int | |||
| 723 | tls12_record_layer_seal_record_protected_aead(struct tls12_record_layer *rl, | 749 | tls12_record_layer_seal_record_protected_aead(struct tls12_record_layer *rl, |
| 724 | uint8_t content_type, const uint8_t *content, size_t content_len, CBB *out) | 750 | uint8_t content_type, const uint8_t *content, size_t content_len, CBB *out) |
| 725 | { | 751 | { |
| 726 | const SSL_AEAD_CTX *aead = rl->write_aead_ctx; | 752 | const SSL_AEAD_CTX *aead = rl->write->aead_ctx; |
| 727 | uint8_t *header = NULL, *nonce = NULL; | 753 | uint8_t *header = NULL, *nonce = NULL; |
| 728 | size_t header_len = 0, nonce_len = 0; | 754 | size_t header_len = 0, nonce_len = 0; |
| 729 | size_t enc_record_len, out_len; | 755 | size_t enc_record_len, out_len; |
| @@ -734,22 +760,22 @@ tls12_record_layer_seal_record_protected_aead(struct tls12_record_layer *rl, | |||
| 734 | /* XXX - move to nonce allocated in record layer, matching TLSv1.3 */ | 760 | /* XXX - move to nonce allocated in record layer, matching TLSv1.3 */ |
| 735 | if (aead->xor_fixed_nonce) { | 761 | if (aead->xor_fixed_nonce) { |
| 736 | if (!tls12_record_layer_aead_xored_nonce(rl, aead, | 762 | if (!tls12_record_layer_aead_xored_nonce(rl, aead, |
| 737 | rl->write_seq_num, &nonce, &nonce_len)) | 763 | rl->write->seq_num, &nonce, &nonce_len)) |
| 738 | goto err; | 764 | goto err; |
| 739 | } else { | 765 | } else { |
| 740 | if (!tls12_record_layer_aead_concat_nonce(rl, aead, | 766 | if (!tls12_record_layer_aead_concat_nonce(rl, aead, |
| 741 | rl->write_seq_num, &nonce, &nonce_len)) | 767 | rl->write->seq_num, &nonce, &nonce_len)) |
| 742 | goto err; | 768 | goto err; |
| 743 | } | 769 | } |
| 744 | 770 | ||
| 745 | if (aead->variable_nonce_in_record) { | 771 | if (aead->variable_nonce_in_record) { |
| 746 | /* XXX - length check? */ | 772 | /* XXX - length check? */ |
| 747 | if (!CBB_add_bytes(out, rl->write_seq_num, aead->variable_nonce_len)) | 773 | if (!CBB_add_bytes(out, rl->write->seq_num, aead->variable_nonce_len)) |
| 748 | goto err; | 774 | goto err; |
| 749 | } | 775 | } |
| 750 | 776 | ||
| 751 | if (!tls12_record_layer_pseudo_header(rl, content_type, content_len, | 777 | if (!tls12_record_layer_pseudo_header(rl, content_type, content_len, |
| 752 | epoch, rl->write_seq_num, SSL3_SEQUENCE_SIZE, &header, &header_len)) | 778 | epoch, rl->write->seq_num, SSL3_SEQUENCE_SIZE, &header, &header_len)) |
| 753 | goto err; | 779 | goto err; |
| 754 | 780 | ||
| 755 | /* XXX EVP_AEAD_max_tag_len vs EVP_AEAD_CTX_tag_len. */ | 781 | /* XXX EVP_AEAD_max_tag_len vs EVP_AEAD_CTX_tag_len. */ |
| @@ -779,7 +805,7 @@ static int | |||
| 779 | tls12_record_layer_seal_record_protected_cipher(struct tls12_record_layer *rl, | 805 | tls12_record_layer_seal_record_protected_cipher(struct tls12_record_layer *rl, |
| 780 | uint8_t content_type, const uint8_t *content, size_t content_len, CBB *out) | 806 | uint8_t content_type, const uint8_t *content, size_t content_len, CBB *out) |
| 781 | { | 807 | { |
| 782 | EVP_CIPHER_CTX *enc = rl->write_cipher_ctx; | 808 | EVP_CIPHER_CTX *enc = rl->write->cipher_ctx; |
| 783 | size_t mac_len, pad_len; | 809 | size_t mac_len, pad_len; |
| 784 | int block_size, eiv_len; | 810 | int block_size, eiv_len; |
| 785 | uint8_t *enc_data, *eiv, *pad, pad_val; | 811 | uint8_t *enc_data, *eiv, *pad, pad_val; |
| @@ -808,7 +834,7 @@ tls12_record_layer_seal_record_protected_cipher(struct tls12_record_layer *rl, | |||
| 808 | goto err; | 834 | goto err; |
| 809 | 835 | ||
| 810 | mac_len = 0; | 836 | mac_len = 0; |
| 811 | if (rl->write_hash_ctx != NULL) { | 837 | if (rl->write->hash_ctx != NULL) { |
| 812 | if (!tls12_record_layer_write_mac(rl, &cbb, content_type, | 838 | if (!tls12_record_layer_write_mac(rl, &cbb, content_type, |
| 813 | content, content_len, &mac_len)) | 839 | content, content_len, &mac_len)) |
| 814 | goto err; | 840 | goto err; |
| @@ -865,18 +891,18 @@ tls12_record_layer_seal_record(struct tls12_record_layer *rl, | |||
| 865 | return 0; | 891 | return 0; |
| 866 | if (rl->dtls) { | 892 | if (rl->dtls) { |
| 867 | if (!tls12_record_layer_build_seq_num(rl, cbb, | 893 | if (!tls12_record_layer_build_seq_num(rl, cbb, |
| 868 | rl->write_epoch, rl->write_seq_num, | 894 | rl->write->epoch, rl->write->seq_num, |
| 869 | SSL3_SEQUENCE_SIZE)) | 895 | SSL3_SEQUENCE_SIZE)) |
| 870 | return 0; | 896 | return 0; |
| 871 | } | 897 | } |
| 872 | if (!CBB_add_u16_length_prefixed(cbb, &fragment)) | 898 | if (!CBB_add_u16_length_prefixed(cbb, &fragment)) |
| 873 | return 0; | 899 | return 0; |
| 874 | 900 | ||
| 875 | if (rl->write_aead_ctx != NULL) { | 901 | if (rl->write->aead_ctx != NULL) { |
| 876 | if (!tls12_record_layer_seal_record_protected_aead(rl, | 902 | if (!tls12_record_layer_seal_record_protected_aead(rl, |
| 877 | content_type, content, content_len, &fragment)) | 903 | content_type, content, content_len, &fragment)) |
| 878 | return 0; | 904 | return 0; |
| 879 | } else if (rl->write_cipher_ctx != NULL) { | 905 | } else if (rl->write->cipher_ctx != NULL) { |
| 880 | if (!tls12_record_layer_seal_record_protected_cipher(rl, | 906 | if (!tls12_record_layer_seal_record_protected_cipher(rl, |
| 881 | content_type, content, content_len, &fragment)) | 907 | content_type, content, content_len, &fragment)) |
| 882 | return 0; | 908 | return 0; |
| @@ -889,7 +915,7 @@ tls12_record_layer_seal_record(struct tls12_record_layer *rl, | |||
| 889 | if (!CBB_flush(cbb)) | 915 | if (!CBB_flush(cbb)) |
| 890 | return 0; | 916 | return 0; |
| 891 | 917 | ||
| 892 | tls1_record_sequence_increment(rl->write_seq_num); | 918 | tls1_record_sequence_increment(rl->write->seq_num); |
| 893 | 919 | ||
| 894 | return 1; | 920 | return 1; |
| 895 | } | 921 | } |
