summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjsing <>2021-01-19 18:34:02 +0000
committerjsing <>2021-01-19 18:34:02 +0000
commit0e9595ce9da2c27470d495fbfc1b189eb4a3df24 (patch)
tree74a2816e9839360303307ee6f0a6fb18c1b2585b /src
parent11fb2101811061cab904382a8a9392a1451bc040 (diff)
downloadopenbsd-0e9595ce9da2c27470d495fbfc1b189eb4a3df24.tar.gz
openbsd-0e9595ce9da2c27470d495fbfc1b189eb4a3df24.tar.bz2
openbsd-0e9595ce9da2c27470d495fbfc1b189eb4a3df24.zip
Factor out code for explicit IV length, block size and MAC length.
Pull this code up into the record protection struct, which means we only need the length checks in one place. This code will soon be used for additional purposes. ok inoguchi@ tb@
Diffstat (limited to 'src')
-rw-r--r--src/lib/libssl/tls12_record_layer.c98
1 files changed, 77 insertions, 21 deletions
diff --git a/src/lib/libssl/tls12_record_layer.c b/src/lib/libssl/tls12_record_layer.c
index 50311a3d84..04699f9a83 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.9 2021/01/13 18:20:54 jsing Exp $ */ 1/* $OpenBSD: tls12_record_layer.c,v 1.10 2021/01/19 18:34:02 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2020 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2020 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -58,6 +58,68 @@ tls12_record_protection_free(struct tls12_record_protection *rp)
58 freezero(rp, sizeof(struct tls12_record_protection)); 58 freezero(rp, sizeof(struct tls12_record_protection));
59} 59}
60 60
61static int
62tls12_record_protection_eiv_len(struct tls12_record_protection *rp,
63 size_t *out_eiv_len)
64{
65 int eiv_len;
66
67 *out_eiv_len = 0;
68
69 if (rp->cipher_ctx == NULL)
70 return 0;
71
72 eiv_len = 0;
73 if (EVP_CIPHER_CTX_mode(rp->cipher_ctx) == EVP_CIPH_CBC_MODE)
74 eiv_len = EVP_CIPHER_CTX_iv_length(rp->cipher_ctx);
75 if (eiv_len < 0 || eiv_len > EVP_MAX_IV_LENGTH)
76 return 0;
77
78 *out_eiv_len = eiv_len;
79
80 return 1;
81}
82
83static int
84tls12_record_protection_block_size(struct tls12_record_protection *rp,
85 size_t *out_block_size)
86{
87 int block_size;
88
89 *out_block_size = 0;
90
91 if (rp->cipher_ctx == NULL)
92 return 0;
93
94 block_size = EVP_CIPHER_CTX_block_size(rp->cipher_ctx);
95 if (block_size < 0 || block_size > EVP_MAX_BLOCK_LENGTH)
96 return 0;
97
98 *out_block_size = block_size;
99
100 return 1;
101}
102
103static int
104tls12_record_protection_mac_len(struct tls12_record_protection *rp,
105 size_t *out_mac_len)
106{
107 int mac_len;
108
109 *out_mac_len = 0;
110
111 if (rp->hash_ctx == NULL)
112 return 0;
113
114 mac_len = EVP_MD_CTX_size(rp->hash_ctx);
115 if (mac_len <= 0 || mac_len > EVP_MAX_MD_SIZE)
116 return 0;
117
118 *out_mac_len = mac_len;
119
120 return 1;
121}
122
61struct tls12_record_layer { 123struct tls12_record_layer {
62 uint16_t version; 124 uint16_t version;
63 int dtls; 125 int dtls;
@@ -566,9 +628,9 @@ tls12_record_layer_open_record_protected_cipher(struct tls12_record_layer *rl,
566{ 628{
567 EVP_CIPHER_CTX *enc = rl->read->cipher_ctx; 629 EVP_CIPHER_CTX *enc = rl->read->cipher_ctx;
568 SSL3_RECORD_INTERNAL rrec; 630 SSL3_RECORD_INTERNAL rrec;
569 int block_size, eiv_len; 631 size_t block_size, eiv_len;
570 uint8_t *mac = NULL; 632 uint8_t *mac = NULL;
571 int mac_len = 0; 633 size_t mac_len = 0;
572 uint8_t *out_mac = NULL; 634 uint8_t *out_mac = NULL;
573 size_t out_mac_len = 0; 635 size_t out_mac_len = 0;
574 uint8_t *plain; 636 uint8_t *plain;
@@ -579,22 +641,19 @@ tls12_record_layer_open_record_protected_cipher(struct tls12_record_layer *rl,
579 641
580 memset(&cbb_mac, 0, sizeof(cbb_mac)); 642 memset(&cbb_mac, 0, sizeof(cbb_mac));
581 643
582 block_size = EVP_CIPHER_CTX_block_size(enc); 644 if (!tls12_record_protection_block_size(rl->read, &block_size))
583 if (block_size < 0 || block_size > EVP_MAX_BLOCK_LENGTH)
584 goto err; 645 goto err;
585 646
586 /* Determine explicit IV length. */ 647 /* Determine explicit IV length. */
587 eiv_len = 0; 648 eiv_len = 0;
588 if (rl->version != TLS1_VERSION && 649 if (rl->version != TLS1_VERSION) {
589 EVP_CIPHER_CTX_mode(enc) == EVP_CIPH_CBC_MODE) 650 if (!tls12_record_protection_eiv_len(rl->read, &eiv_len))
590 eiv_len = EVP_CIPHER_CTX_iv_length(enc); 651 goto err;
591 if (eiv_len < 0 || eiv_len > EVP_MAX_IV_LENGTH) 652 }
592 goto err;
593 653
594 mac_len = 0; 654 mac_len = 0;
595 if (rl->read->hash_ctx != NULL) { 655 if (rl->read->hash_ctx != NULL) {
596 mac_len = EVP_MD_CTX_size(rl->read->hash_ctx); 656 if (!tls12_record_protection_mac_len(rl->read, &mac_len))
597 if (mac_len <= 0 || mac_len > EVP_MAX_MD_SIZE)
598 goto err; 657 goto err;
599 } 658 }
600 659
@@ -808,8 +867,7 @@ tls12_record_layer_seal_record_protected_cipher(struct tls12_record_layer *rl,
808 size_t content_len, CBB *out) 867 size_t content_len, CBB *out)
809{ 868{
810 EVP_CIPHER_CTX *enc = rl->write->cipher_ctx; 869 EVP_CIPHER_CTX *enc = rl->write->cipher_ctx;
811 size_t mac_len, pad_len; 870 size_t block_size, eiv_len, mac_len, pad_len;
812 int block_size, eiv_len;
813 uint8_t *enc_data, *eiv, *pad, pad_val; 871 uint8_t *enc_data, *eiv, *pad, pad_val;
814 uint8_t *plain = NULL; 872 uint8_t *plain = NULL;
815 size_t plain_len = 0; 873 size_t plain_len = 0;
@@ -821,11 +879,10 @@ tls12_record_layer_seal_record_protected_cipher(struct tls12_record_layer *rl,
821 879
822 /* Add explicit IV if necessary. */ 880 /* Add explicit IV if necessary. */
823 eiv_len = 0; 881 eiv_len = 0;
824 if (rl->version != TLS1_VERSION && 882 if (rl->version != TLS1_VERSION) {
825 EVP_CIPHER_CTX_mode(enc) == EVP_CIPH_CBC_MODE) 883 if (!tls12_record_protection_eiv_len(rl->write, &eiv_len))
826 eiv_len = EVP_CIPHER_CTX_iv_length(enc); 884 goto err;
827 if (eiv_len < 0 || eiv_len > EVP_MAX_IV_LENGTH) 885 }
828 goto err;
829 if (eiv_len > 0) { 886 if (eiv_len > 0) {
830 if (!CBB_add_space(&cbb, &eiv, eiv_len)) 887 if (!CBB_add_space(&cbb, &eiv, eiv_len))
831 goto err; 888 goto err;
@@ -845,8 +902,7 @@ tls12_record_layer_seal_record_protected_cipher(struct tls12_record_layer *rl,
845 plain_len = (size_t)eiv_len + content_len + mac_len; 902 plain_len = (size_t)eiv_len + content_len + mac_len;
846 903
847 /* Add padding to block size, if necessary. */ 904 /* Add padding to block size, if necessary. */
848 block_size = EVP_CIPHER_CTX_block_size(enc); 905 if (!tls12_record_protection_block_size(rl->write, &block_size))
849 if (block_size < 0 || block_size > EVP_MAX_BLOCK_LENGTH)
850 goto err; 906 goto err;
851 if (block_size > 1) { 907 if (block_size > 1) {
852 pad_len = block_size - (plain_len % block_size); 908 pad_len = block_size - (plain_len % block_size);