summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjsing <>2021-01-28 17:00:39 +0000
committerjsing <>2021-01-28 17:00:39 +0000
commit282f50c32f1cbabc77296f995bf1a6408dca06df (patch)
tree7bef75b2e33f0547bd41212a8a2355d0bd970a54 /src
parent7f3297e4d9569e0e3bb11f4329fdd890572f07ff (diff)
downloadopenbsd-282f50c32f1cbabc77296f995bf1a6408dca06df.tar.gz
openbsd-282f50c32f1cbabc77296f995bf1a6408dca06df.tar.bz2
openbsd-282f50c32f1cbabc77296f995bf1a6408dca06df.zip
Move AEAD handling into the new TLSv1.2 record layer.
ok tb@
Diffstat (limited to 'src')
-rw-r--r--src/lib/libssl/ssl_lib.c14
-rw-r--r--src/lib/libssl/ssl_locl.h12
-rw-r--r--src/lib/libssl/t1_enc.c94
-rw-r--r--src/lib/libssl/tls12_record_layer.c116
4 files changed, 102 insertions, 134 deletions
diff --git a/src/lib/libssl/ssl_lib.c b/src/lib/libssl/ssl_lib.c
index 5cf4be74aa..b67f8569cc 100644
--- a/src/lib/libssl/ssl_lib.c
+++ b/src/lib/libssl/ssl_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ssl_lib.c,v 1.243 2021/01/26 18:45:32 tb Exp $ */ 1/* $OpenBSD: ssl_lib.c,v 1.244 2021/01/28 17:00:38 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -2618,12 +2618,6 @@ ssl_clear_cipher_read_state(SSL *s)
2618 s->read_hash = NULL; 2618 s->read_hash = NULL;
2619 2619
2620 tls12_record_layer_clear_read_state(s->internal->rl); 2620 tls12_record_layer_clear_read_state(s->internal->rl);
2621
2622 if (s->internal->aead_read_ctx != NULL) {
2623 EVP_AEAD_CTX_cleanup(&s->internal->aead_read_ctx->ctx);
2624 free(s->internal->aead_read_ctx);
2625 s->internal->aead_read_ctx = NULL;
2626 }
2627} 2621}
2628 2622
2629void 2623void
@@ -2635,12 +2629,6 @@ ssl_clear_cipher_write_state(SSL *s)
2635 s->internal->write_hash = NULL; 2629 s->internal->write_hash = NULL;
2636 2630
2637 tls12_record_layer_clear_write_state(s->internal->rl); 2631 tls12_record_layer_clear_write_state(s->internal->rl);
2638
2639 if (s->internal->aead_write_ctx != NULL) {
2640 EVP_AEAD_CTX_cleanup(&s->internal->aead_write_ctx->ctx);
2641 free(s->internal->aead_write_ctx);
2642 s->internal->aead_write_ctx = NULL;
2643 }
2644} 2632}
2645 2633
2646/* Fix this function so that it takes an optional type parameter */ 2634/* Fix this function so that it takes an optional type parameter */
diff --git a/src/lib/libssl/ssl_locl.h b/src/lib/libssl/ssl_locl.h
index 4390361c66..d5298d7af1 100644
--- a/src/lib/libssl/ssl_locl.h
+++ b/src/lib/libssl/ssl_locl.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: ssl_locl.h,v 1.317 2021/01/26 14:22:20 jsing Exp $ */ 1/* $OpenBSD: ssl_locl.h,v 1.318 2021/01/28 17:00:39 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -481,6 +481,8 @@ int tls12_record_layer_write_overhead(struct tls12_record_layer *rl,
481 size_t *overhead); 481 size_t *overhead);
482int tls12_record_layer_read_protected(struct tls12_record_layer *rl); 482int tls12_record_layer_read_protected(struct tls12_record_layer *rl);
483int tls12_record_layer_write_protected(struct tls12_record_layer *rl); 483int tls12_record_layer_write_protected(struct tls12_record_layer *rl);
484void tls12_record_layer_set_aead(struct tls12_record_layer *rl,
485 const EVP_AEAD *aead);
484void tls12_record_layer_set_version(struct tls12_record_layer *rl, 486void tls12_record_layer_set_version(struct tls12_record_layer *rl,
485 uint16_t version); 487 uint16_t version);
486void tls12_record_layer_set_write_epoch(struct tls12_record_layer *rl, 488void tls12_record_layer_set_write_epoch(struct tls12_record_layer *rl,
@@ -758,14 +760,6 @@ typedef struct ssl_internal_st {
758 760
759 STACK_OF(SSL_CIPHER) *cipher_list_tls13; 761 STACK_OF(SSL_CIPHER) *cipher_list_tls13;
760 762
761 SSL_AEAD_CTX *aead_read_ctx; /* AEAD context. If non-NULL, then
762 enc_read_ctx and read_hash are
763 ignored. */
764
765 SSL_AEAD_CTX *aead_write_ctx; /* AEAD context. If non-NULL, then
766 enc_write_ctx and write_hash are
767 ignored. */
768
769 EVP_CIPHER_CTX *enc_write_ctx; /* cryptographic state */ 763 EVP_CIPHER_CTX *enc_write_ctx; /* cryptographic state */
770 EVP_MD_CTX *write_hash; /* used for mac generation */ 764 EVP_MD_CTX *write_hash; /* used for mac generation */
771 765
diff --git a/src/lib/libssl/t1_enc.c b/src/lib/libssl/t1_enc.c
index a0b377389b..b84a5347f1 100644
--- a/src/lib/libssl/t1_enc.c
+++ b/src/lib/libssl/t1_enc.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: t1_enc.c,v 1.130 2021/01/26 14:22:20 jsing Exp $ */ 1/* $OpenBSD: t1_enc.c,v 1.131 2021/01/28 17:00:39 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -311,90 +311,6 @@ tls1_generate_key_block(SSL *s, uint8_t *key_block, size_t key_block_len)
311} 311}
312 312
313/* 313/*
314 * tls1_aead_ctx_init allocates aead_ctx, if needed. It returns 1 on success
315 * and 0 on failure.
316 */
317static int
318tls1_aead_ctx_init(SSL_AEAD_CTX **aead_ctx)
319{
320 if (*aead_ctx != NULL) {
321 EVP_AEAD_CTX_cleanup(&(*aead_ctx)->ctx);
322 return (1);
323 }
324
325 *aead_ctx = malloc(sizeof(SSL_AEAD_CTX));
326 if (*aead_ctx == NULL) {
327 SSLerrorx(ERR_R_MALLOC_FAILURE);
328 return (0);
329 }
330
331 return (1);
332}
333
334static int
335tls1_change_cipher_state_aead(SSL *s, char is_read, const unsigned char *key,
336 unsigned int key_len, const unsigned char *iv, unsigned int iv_len)
337{
338 const EVP_AEAD *aead = S3I(s)->tmp.new_aead;
339 SSL_AEAD_CTX *aead_ctx;
340
341 /* XXX - Need to avoid clearing write state for DTLS. */
342 if (SSL_is_dtls(s))
343 return 0;
344
345 if (is_read) {
346 ssl_clear_cipher_read_state(s);
347 if (!tls1_aead_ctx_init(&s->internal->aead_read_ctx))
348 return 0;
349 aead_ctx = s->internal->aead_read_ctx;
350
351 if (!tls12_record_layer_set_read_aead(s->internal->rl, aead_ctx))
352 return 0;
353 } else {
354 ssl_clear_cipher_write_state(s);
355 if (!tls1_aead_ctx_init(&s->internal->aead_write_ctx))
356 return 0;
357 aead_ctx = s->internal->aead_write_ctx;
358
359 if (!tls12_record_layer_set_write_aead(s->internal->rl, aead_ctx))
360 return 0;
361 }
362
363 if (!EVP_AEAD_CTX_init(&aead_ctx->ctx, aead, key, key_len,
364 EVP_AEAD_DEFAULT_TAG_LENGTH, NULL))
365 return (0);
366 if (iv_len > sizeof(aead_ctx->fixed_nonce)) {
367 SSLerrorx(ERR_R_INTERNAL_ERROR);
368 return (0);
369 }
370 memcpy(aead_ctx->fixed_nonce, iv, iv_len);
371 aead_ctx->fixed_nonce_len = iv_len;
372 aead_ctx->variable_nonce_len = 8; /* always the case, currently. */
373 aead_ctx->variable_nonce_in_record =
374 (S3I(s)->hs.new_cipher->algorithm2 &
375 SSL_CIPHER_ALGORITHM2_VARIABLE_NONCE_IN_RECORD) != 0;
376 aead_ctx->xor_fixed_nonce =
377 S3I(s)->hs.new_cipher->algorithm_enc == SSL_CHACHA20POLY1305;
378 aead_ctx->tag_len = EVP_AEAD_max_overhead(aead);
379
380 if (aead_ctx->xor_fixed_nonce) {
381 if (aead_ctx->fixed_nonce_len != EVP_AEAD_nonce_length(aead) ||
382 aead_ctx->variable_nonce_len > EVP_AEAD_nonce_length(aead)) {
383 SSLerrorx(ERR_R_INTERNAL_ERROR);
384 return (0);
385 }
386 } else {
387 if (aead_ctx->variable_nonce_len + aead_ctx->fixed_nonce_len !=
388 EVP_AEAD_nonce_length(aead)) {
389 SSLerrorx(ERR_R_INTERNAL_ERROR);
390 return (0);
391 }
392 }
393
394 return (1);
395}
396
397/*
398 * tls1_change_cipher_state_cipher performs the work needed to switch cipher 314 * tls1_change_cipher_state_cipher performs the work needed to switch cipher
399 * states when using EVP_CIPHER. The argument is_read is true iff this function 315 * states when using EVP_CIPHER. The argument is_read is true iff this function
400 * is being called due to reading, as opposed to writing, a ChangeCipherSpec 316 * is being called due to reading, as opposed to writing, a ChangeCipherSpec
@@ -566,10 +482,8 @@ tls1_change_cipher_state(SSL *s, int which)
566 goto err; 482 goto err;
567 } 483 }
568 484
569 if (aead != NULL) { 485 if (aead != NULL)
570 return tls1_change_cipher_state_aead(s, is_read, key, key_len, 486 return 1;
571 iv, iv_len);
572 }
573 487
574 return tls1_change_cipher_state_cipher(s, is_read, 488 return tls1_change_cipher_state_cipher(s, is_read,
575 mac_secret, mac_secret_size, key, key_len, iv, iv_len); 489 mac_secret, mac_secret_size, key, key_len, iv, iv_len);
@@ -617,6 +531,8 @@ tls1_setup_key_block(SSL *s)
617 S3I(s)->tmp.new_mac_pkey_type = mac_type; 531 S3I(s)->tmp.new_mac_pkey_type = mac_type;
618 S3I(s)->tmp.new_mac_secret_size = mac_secret_size; 532 S3I(s)->tmp.new_mac_secret_size = mac_secret_size;
619 533
534 tls12_record_layer_set_aead(s->internal->rl, aead);
535
620 tls1_cleanup_key_block(s); 536 tls1_cleanup_key_block(s);
621 537
622 if ((key_block = reallocarray(NULL, mac_secret_size + key_len + iv_len, 538 if ((key_block = reallocarray(NULL, mac_secret_size + key_len + iv_len,
diff --git a/src/lib/libssl/tls12_record_layer.c b/src/lib/libssl/tls12_record_layer.c
index 0104443286..b74a6588ef 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.15 2021/01/26 14:22:20 jsing Exp $ */ 1/* $OpenBSD: tls12_record_layer.c,v 1.16 2021/01/28 17:00:39 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2020 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2020 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -15,6 +15,7 @@
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */ 16 */
17 17
18#include <limits.h>
18#include <stdlib.h> 19#include <stdlib.h>
19 20
20#include <openssl/evp.h> 21#include <openssl/evp.h>
@@ -25,6 +26,8 @@ struct tls12_record_protection {
25 uint16_t epoch; 26 uint16_t epoch;
26 uint8_t seq_num[SSL3_SEQUENCE_SIZE]; 27 uint8_t seq_num[SSL3_SEQUENCE_SIZE];
27 28
29 SSL_AEAD_CTX *aead_ctx;
30
28 int stream_mac; 31 int stream_mac;
29 32
30 uint8_t *mac_key; 33 uint8_t *mac_key;
@@ -34,8 +37,6 @@ struct tls12_record_protection {
34 * XXX - for now these are just pointers to externally managed 37 * XXX - for now these are just pointers to externally managed
35 * structs/memory. These should eventually be owned by the record layer. 38 * structs/memory. These should eventually be owned by the record layer.
36 */ 39 */
37 SSL_AEAD_CTX *aead_ctx;
38
39 EVP_CIPHER_CTX *cipher_ctx; 40 EVP_CIPHER_CTX *cipher_ctx;
40 EVP_MD_CTX *hash_ctx; 41 EVP_MD_CTX *hash_ctx;
41}; 42};
@@ -51,6 +52,12 @@ tls12_record_protection_clear(struct tls12_record_protection *rp)
51{ 52{
52 memset(rp->seq_num, 0, sizeof(rp->seq_num)); 53 memset(rp->seq_num, 0, sizeof(rp->seq_num));
53 54
55 if (rp->aead_ctx != NULL) {
56 EVP_AEAD_CTX_cleanup(&rp->aead_ctx->ctx);
57 freezero(rp->aead_ctx, sizeof(*rp->aead_ctx));
58 rp->aead_ctx = NULL;
59 }
60
54 freezero(rp->mac_key, rp->mac_key_len); 61 freezero(rp->mac_key, rp->mac_key_len);
55 rp->mac_key = NULL; 62 rp->mac_key = NULL;
56 rp->mac_key_len = 0; 63 rp->mac_key_len = 0;
@@ -141,6 +148,8 @@ struct tls12_record_layer {
141 148
142 uint8_t alert_desc; 149 uint8_t alert_desc;
143 150
151 const EVP_AEAD *aead;
152
144 /* Pointers to active record protection (memory is not owned). */ 153 /* Pointers to active record protection (memory is not owned). */
145 struct tls12_record_protection *read; 154 struct tls12_record_protection *read;
146 struct tls12_record_protection *write; 155 struct tls12_record_protection *write;
@@ -232,6 +241,12 @@ tls12_record_layer_write_protected(struct tls12_record_layer *rl)
232} 241}
233 242
234void 243void
244tls12_record_layer_set_aead(struct tls12_record_layer *rl, const EVP_AEAD *aead)
245{
246 rl->aead = aead;
247}
248
249void
235tls12_record_layer_set_version(struct tls12_record_layer *rl, uint16_t version) 250tls12_record_layer_set_version(struct tls12_record_layer *rl, uint16_t version)
236{ 251{
237 rl->version = version; 252 rl->version = version;
@@ -324,24 +339,6 @@ tls12_record_layer_reflect_seq_num(struct tls12_record_layer *rl)
324} 339}
325 340
326int 341int
327tls12_record_layer_set_read_aead(struct tls12_record_layer *rl,
328 SSL_AEAD_CTX *aead_ctx)
329{
330 tls12_record_layer_set_read_state(rl, aead_ctx, NULL, NULL, 0);
331
332 return 1;
333}
334
335int
336tls12_record_layer_set_write_aead(struct tls12_record_layer *rl,
337 SSL_AEAD_CTX *aead_ctx)
338{
339 tls12_record_layer_set_write_state(rl, aead_ctx, NULL, NULL, 0);
340
341 return 1;
342}
343
344int
345tls12_record_layer_set_read_cipher_hash(struct tls12_record_layer *rl, 342tls12_record_layer_set_read_cipher_hash(struct tls12_record_layer *rl,
346 EVP_CIPHER_CTX *cipher_ctx, EVP_MD_CTX *hash_ctx, int stream_mac) 343 EVP_CIPHER_CTX *cipher_ctx, EVP_MD_CTX *hash_ctx, int stream_mac)
347{ 344{
@@ -381,6 +378,75 @@ tls12_record_layer_set_read_mac_key(struct tls12_record_layer *rl,
381 return 1; 378 return 1;
382} 379}
383 380
381static int
382tls12_record_layer_ccs_aead(struct tls12_record_layer *rl,
383 struct tls12_record_protection *rp, int is_write, const uint8_t *mac_key,
384 size_t mac_key_len, const uint8_t *key, size_t key_len, const uint8_t *iv,
385 size_t iv_len)
386{
387 size_t aead_nonce_len = EVP_AEAD_nonce_length(rl->aead);
388
389 if ((rp->aead_ctx = calloc(1, sizeof(*rp->aead_ctx))) == NULL)
390 return 0;
391
392 /* AES GCM cipher suites use variable nonce in record. */
393 if (rl->aead == EVP_aead_aes_128_gcm() ||
394 rl->aead == EVP_aead_aes_256_gcm())
395 rp->aead_ctx->variable_nonce_in_record = 1;
396
397 /* ChaCha20 Poly1305 XORs the fixed and variable nonces. */
398 if (rl->aead == EVP_aead_chacha20_poly1305())
399 rp->aead_ctx->xor_fixed_nonce = 1;
400
401 if (iv_len > sizeof(rp->aead_ctx->fixed_nonce))
402 return 0;
403
404 memcpy(rp->aead_ctx->fixed_nonce, iv, iv_len);
405 rp->aead_ctx->fixed_nonce_len = iv_len;
406 rp->aead_ctx->tag_len = EVP_AEAD_max_overhead(rl->aead);
407 rp->aead_ctx->variable_nonce_len = 8;
408
409 if (rp->aead_ctx->xor_fixed_nonce) {
410 /* Fixed nonce length must match, variable must not exceed. */
411 if (rp->aead_ctx->fixed_nonce_len != aead_nonce_len)
412 return 0;
413 if (rp->aead_ctx->variable_nonce_len > aead_nonce_len)
414 return 0;
415 } else {
416 /* Concatenated nonce length must equal AEAD nonce length. */
417 if (rp->aead_ctx->fixed_nonce_len +
418 rp->aead_ctx->variable_nonce_len != aead_nonce_len)
419 return 0;
420 }
421
422 if (!EVP_AEAD_CTX_init(&rp->aead_ctx->ctx, rl->aead, key, key_len,
423 EVP_AEAD_DEFAULT_TAG_LENGTH, NULL))
424 return 0;
425
426 return 1;
427}
428
429static int
430tls12_record_layer_change_cipher_state(struct tls12_record_layer *rl,
431 struct tls12_record_protection *rp, int is_write, const uint8_t *mac_key,
432 size_t mac_key_len, const uint8_t *key, size_t key_len, const uint8_t *iv,
433 size_t iv_len)
434{
435 /* Require unused record protection. */
436 if (rp->cipher_ctx != NULL || rp->aead_ctx != NULL)
437 return 0;
438
439 if (mac_key_len > INT_MAX || key_len > INT_MAX || iv_len > INT_MAX)
440 return 0;
441
442 /* XXX - only aead for now. */
443 if (rl->aead == NULL)
444 return 1;
445
446 return tls12_record_layer_ccs_aead(rl, rp, is_write, mac_key,
447 mac_key_len, key, key_len, iv, iv_len);
448}
449
384int 450int
385tls12_record_layer_change_read_cipher_state(struct tls12_record_layer *rl, 451tls12_record_layer_change_read_cipher_state(struct tls12_record_layer *rl,
386 const uint8_t *mac_key, size_t mac_key_len, const uint8_t *key, 452 const uint8_t *mac_key, size_t mac_key_len, const uint8_t *key,
@@ -394,7 +460,9 @@ tls12_record_layer_change_read_cipher_state(struct tls12_record_layer *rl,
394 460
395 /* Read sequence number gets reset to zero. */ 461 /* Read sequence number gets reset to zero. */
396 462
397 /* XXX - change cipher state. */ 463 if (!tls12_record_layer_change_cipher_state(rl, read_new, 0,
464 mac_key, mac_key_len, key, key_len, iv, iv_len))
465 goto err;
398 466
399 tls12_record_protection_free(rl->read_current); 467 tls12_record_protection_free(rl->read_current);
400 rl->read = rl->read_current = read_new; 468 rl->read = rl->read_current = read_new;
@@ -421,7 +489,9 @@ tls12_record_layer_change_write_cipher_state(struct tls12_record_layer *rl,
421 489
422 /* Write sequence number gets reset to zero. */ 490 /* Write sequence number gets reset to zero. */
423 491
424 /* XXX - change cipher state. */ 492 if (!tls12_record_layer_change_cipher_state(rl, write_new, 1,
493 mac_key, mac_key_len, key, key_len, iv, iv_len))
494 goto err;
425 495
426 if (rl->dtls) { 496 if (rl->dtls) {
427 tls12_record_protection_free(rl->write_previous); 497 tls12_record_protection_free(rl->write_previous);