summaryrefslogtreecommitdiff
path: root/src/lib/libssl/tls13_record_layer.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libssl/tls13_record_layer.c')
-rw-r--r--src/lib/libssl/tls13_record_layer.c108
1 files changed, 34 insertions, 74 deletions
diff --git a/src/lib/libssl/tls13_record_layer.c b/src/lib/libssl/tls13_record_layer.c
index 6556547353..2e32cb8a37 100644
--- a/src/lib/libssl/tls13_record_layer.c
+++ b/src/lib/libssl/tls13_record_layer.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: tls13_record_layer.c,v 1.62 2021/06/08 18:05:47 tb Exp $ */ 1/* $OpenBSD: tls13_record_layer.c,v 1.63 2021/09/04 16:26:12 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2018, 2019 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2018, 2019 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -17,6 +17,7 @@
17 17
18#include "tls13_internal.h" 18#include "tls13_internal.h"
19#include "tls13_record.h" 19#include "tls13_record.h"
20#include "tls_content.h"
20 21
21static ssize_t tls13_record_layer_write_chunk(struct tls13_record_layer *rl, 22static ssize_t tls13_record_layer_write_chunk(struct tls13_record_layer *rl,
22 uint8_t content_type, const uint8_t *buf, size_t n); 23 uint8_t content_type, const uint8_t *buf, size_t n);
@@ -99,11 +100,8 @@ struct tls13_record_layer {
99 uint8_t *phh_data; 100 uint8_t *phh_data;
100 size_t phh_len; 101 size_t phh_len;
101 102
102 /* Buffer containing plaintext from opened records. */ 103 /* Content from opened records. */
103 uint8_t rbuf_content_type; 104 struct tls_content *rcontent;
104 uint8_t *rbuf;
105 size_t rbuf_len;
106 CBS rbuf_cbs;
107 105
108 /* Record protection. */ 106 /* Record protection. */
109 const EVP_MD *hash; 107 const EVP_MD *hash;
@@ -117,16 +115,6 @@ struct tls13_record_layer {
117}; 115};
118 116
119static void 117static void
120tls13_record_layer_rbuf_free(struct tls13_record_layer *rl)
121{
122 CBS_init(&rl->rbuf_cbs, NULL, 0);
123 freezero(rl->rbuf, rl->rbuf_len);
124 rl->rbuf = NULL;
125 rl->rbuf_len = 0;
126 rl->rbuf_content_type = 0;
127}
128
129static void
130tls13_record_layer_rrec_free(struct tls13_record_layer *rl) 118tls13_record_layer_rrec_free(struct tls13_record_layer *rl)
131{ 119{
132 tls13_record_free(rl->rrec); 120 tls13_record_free(rl->rrec);
@@ -149,6 +137,9 @@ tls13_record_layer_new(const struct tls13_record_layer_callbacks *callbacks,
149 if ((rl = calloc(1, sizeof(struct tls13_record_layer))) == NULL) 137 if ((rl = calloc(1, sizeof(struct tls13_record_layer))) == NULL)
150 goto err; 138 goto err;
151 139
140 if ((rl->rcontent = tls_content_new()) == NULL)
141 goto err;
142
152 if ((rl->read = tls13_record_protection_new()) == NULL) 143 if ((rl->read = tls13_record_protection_new()) == NULL)
153 goto err; 144 goto err;
154 if ((rl->write = tls13_record_protection_new()) == NULL) 145 if ((rl->write = tls13_record_protection_new()) == NULL)
@@ -178,7 +169,7 @@ tls13_record_layer_free(struct tls13_record_layer *rl)
178 freezero(rl->alert_data, rl->alert_len); 169 freezero(rl->alert_data, rl->alert_len);
179 freezero(rl->phh_data, rl->phh_len); 170 freezero(rl->phh_data, rl->phh_len);
180 171
181 tls13_record_layer_rbuf_free(rl); 172 tls_content_free(rl->rcontent);
182 173
183 tls13_record_protection_free(rl->read); 174 tls13_record_protection_free(rl->read);
184 tls13_record_protection_free(rl->write); 175 tls13_record_protection_free(rl->write);
@@ -187,9 +178,9 @@ tls13_record_layer_free(struct tls13_record_layer *rl)
187} 178}
188 179
189void 180void
190tls13_record_layer_rbuf(struct tls13_record_layer *rl, CBS *cbs) 181tls13_record_layer_rcontent(struct tls13_record_layer *rl, CBS *cbs)
191{ 182{
192 CBS_dup(&rl->rbuf_cbs, cbs); 183 CBS_dup(tls_content_cbs(rl->rcontent), cbs);
193} 184}
194 185
195static const uint8_t tls13_max_seq_num[TLS13_RECORD_SEQ_NUM_LEN] = { 186static const uint8_t tls13_max_seq_num[TLS13_RECORD_SEQ_NUM_LEN] = {
@@ -292,22 +283,18 @@ tls13_record_layer_process_alert(struct tls13_record_layer *rl)
292 * will result in one of three things - continuation (user_cancelled), 283 * will result in one of three things - continuation (user_cancelled),
293 * read channel closure (close_notify) or termination (all others). 284 * read channel closure (close_notify) or termination (all others).
294 */ 285 */
295 if (rl->rbuf == NULL) 286 if (tls_content_type(rl->rcontent) != SSL3_RT_ALERT)
296 return TLS13_IO_FAILURE; 287 return TLS13_IO_FAILURE;
297 288
298 if (rl->rbuf_content_type != SSL3_RT_ALERT) 289 if (!CBS_get_u8(tls_content_cbs(rl->rcontent), &alert_level))
299 return TLS13_IO_FAILURE;
300
301 if (!CBS_get_u8(&rl->rbuf_cbs, &alert_level))
302 return tls13_send_alert(rl, TLS13_ALERT_DECODE_ERROR); 290 return tls13_send_alert(rl, TLS13_ALERT_DECODE_ERROR);
303 291 if (!CBS_get_u8(tls_content_cbs(rl->rcontent), &alert_desc))
304 if (!CBS_get_u8(&rl->rbuf_cbs, &alert_desc))
305 return tls13_send_alert(rl, TLS13_ALERT_DECODE_ERROR); 292 return tls13_send_alert(rl, TLS13_ALERT_DECODE_ERROR);
306 293
307 if (CBS_len(&rl->rbuf_cbs) != 0) 294 if (tls_content_remaining(rl->rcontent) != 0)
308 return tls13_send_alert(rl, TLS13_ALERT_DECODE_ERROR); 295 return tls13_send_alert(rl, TLS13_ALERT_DECODE_ERROR);
309 296
310 tls13_record_layer_rbuf_free(rl); 297 tls_content_clear(rl->rcontent);
311 298
312 /* 299 /*
313 * Alert level is ignored for closure alerts (RFC 8446 section 6.1), 300 * Alert level is ignored for closure alerts (RFC 8446 section 6.1),
@@ -531,15 +518,10 @@ tls13_record_layer_open_record_plaintext(struct tls13_record_layer *rl)
531 return 0; 518 return 0;
532 } 519 }
533 520
534 tls13_record_layer_rbuf_free(rl); 521 if (!tls_content_dup_data(rl->rcontent,
535 522 tls13_record_content_type(rl->rrec), CBS_data(&cbs), CBS_len(&cbs)))
536 if (!CBS_stow(&cbs, &rl->rbuf, &rl->rbuf_len))
537 return 0; 523 return 0;
538 524
539 rl->rbuf_content_type = tls13_record_content_type(rl->rrec);
540
541 CBS_init(&rl->rbuf_cbs, rl->rbuf, rl->rbuf_len);
542
543 return 1; 525 return 1;
544} 526}
545 527
@@ -604,13 +586,7 @@ tls13_record_layer_open_record_protected(struct tls13_record_layer *rl)
604 } 586 }
605 content_type = content[inner_len]; 587 content_type = content[inner_len];
606 588
607 tls13_record_layer_rbuf_free(rl); 589 tls_content_set_data(rl->rcontent, content_type, content, inner_len);
608
609 rl->rbuf_content_type = content_type;
610 rl->rbuf = content;
611 rl->rbuf_len = inner_len;
612
613 CBS_init(&rl->rbuf_cbs, rl->rbuf, rl->rbuf_len);
614 590
615 return 1; 591 return 1;
616 592
@@ -877,12 +853,12 @@ tls13_record_layer_read_record(struct tls13_record_layer *rl)
877 * we must terminate the connection with an unexpected_message alert. 853 * we must terminate the connection with an unexpected_message alert.
878 * See RFC 8446 section 5.4. 854 * See RFC 8446 section 5.4.
879 */ 855 */
880 if (CBS_len(&rl->rbuf_cbs) == 0 && 856 if (tls_content_remaining(rl->rcontent) == 0 &&
881 (rl->rbuf_content_type == SSL3_RT_ALERT || 857 (tls_content_type(rl->rcontent) == SSL3_RT_ALERT ||
882 rl->rbuf_content_type == SSL3_RT_HANDSHAKE)) 858 tls_content_type(rl->rcontent) == SSL3_RT_HANDSHAKE))
883 return tls13_send_alert(rl, TLS13_ALERT_UNEXPECTED_MESSAGE); 859 return tls13_send_alert(rl, TLS13_ALERT_UNEXPECTED_MESSAGE);
884 860
885 switch (rl->rbuf_content_type) { 861 switch (tls_content_type(rl->rcontent)) {
886 case SSL3_RT_ALERT: 862 case SSL3_RT_ALERT:
887 return tls13_record_layer_process_alert(rl); 863 return tls13_record_layer_process_alert(rl);
888 864
@@ -907,10 +883,10 @@ tls13_record_layer_read_record(struct tls13_record_layer *rl)
907static ssize_t 883static ssize_t
908tls13_record_layer_pending(struct tls13_record_layer *rl, uint8_t content_type) 884tls13_record_layer_pending(struct tls13_record_layer *rl, uint8_t content_type)
909{ 885{
910 if (rl->rbuf_content_type != content_type) 886 if (tls_content_type(rl->rcontent) != content_type)
911 return 0; 887 return 0;
912 888
913 return CBS_len(&rl->rbuf_cbs); 889 return tls_content_remaining(rl->rcontent);
914} 890}
915 891
916static ssize_t 892static ssize_t
@@ -929,9 +905,9 @@ tls13_record_layer_recv_phh(struct tls13_record_layer *rl)
929 * TLS13_IO_FAILURE something broke. 905 * TLS13_IO_FAILURE something broke.
930 */ 906 */
931 if (rl->cb.phh_recv != NULL) 907 if (rl->cb.phh_recv != NULL)
932 ret = rl->cb.phh_recv(rl->cb_arg, &rl->rbuf_cbs); 908 ret = rl->cb.phh_recv(rl->cb_arg, tls_content_cbs(rl->rcontent));
933 909
934 tls13_record_layer_rbuf_free(rl); 910 tls_content_clear(rl->rcontent);
935 911
936 /* Leave post handshake handshake mode unless we need more data. */ 912 /* Leave post handshake handshake mode unless we need more data. */
937 if (ret != TLS13_IO_WANT_POLLIN) 913 if (ret != TLS13_IO_WANT_POLLIN)
@@ -960,7 +936,7 @@ tls13_record_layer_read_internal(struct tls13_record_layer *rl,
960 return TLS13_IO_EOF; 936 return TLS13_IO_EOF;
961 937
962 /* If necessary, pull up the next record. */ 938 /* If necessary, pull up the next record. */
963 if (CBS_len(&rl->rbuf_cbs) == 0) { 939 if (tls_content_remaining(rl->rcontent) == 0) {
964 if ((ret = tls13_record_layer_read_record(rl)) <= 0) 940 if ((ret = tls13_record_layer_read_record(rl)) <= 0)
965 return ret; 941 return ret;
966 942
@@ -968,17 +944,15 @@ tls13_record_layer_read_internal(struct tls13_record_layer *rl,
968 * We may have read a valid 0-byte application data record, 944 * We may have read a valid 0-byte application data record,
969 * in which case we need to read the next record. 945 * in which case we need to read the next record.
970 */ 946 */
971 if (CBS_len(&rl->rbuf_cbs) == 0) { 947 if (tls_content_remaining(rl->rcontent) == 0)
972 tls13_record_layer_rbuf_free(rl);
973 return TLS13_IO_WANT_POLLIN; 948 return TLS13_IO_WANT_POLLIN;
974 }
975 } 949 }
976 950
977 /* 951 /*
978 * If we are in post handshake handshake mode, we must not see 952 * If we are in post handshake handshake mode, we must not see
979 * any record type that isn't a handshake until we are done. 953 * any record type that isn't a handshake until we are done.
980 */ 954 */
981 if (rl->phh && rl->rbuf_content_type != SSL3_RT_HANDSHAKE) 955 if (rl->phh && tls_content_type(rl->rcontent) != SSL3_RT_HANDSHAKE)
982 return tls13_send_alert(rl, TLS13_ALERT_UNEXPECTED_MESSAGE); 956 return tls13_send_alert(rl, TLS13_ALERT_UNEXPECTED_MESSAGE);
983 957
984 /* 958 /*
@@ -987,32 +961,18 @@ tls13_record_layer_read_internal(struct tls13_record_layer *rl,
987 * be trying to read application data and need to handle a 961 * be trying to read application data and need to handle a
988 * post-handshake handshake message instead... 962 * post-handshake handshake message instead...
989 */ 963 */
990 if (rl->rbuf_content_type != content_type) { 964 if (tls_content_type(rl->rcontent) != content_type) {
991 if (rl->rbuf_content_type == SSL3_RT_HANDSHAKE) { 965 if (tls_content_type(rl->rcontent) == SSL3_RT_HANDSHAKE) {
992 if (rl->handshake_completed) 966 if (rl->handshake_completed)
993 return tls13_record_layer_recv_phh(rl); 967 return tls13_record_layer_recv_phh(rl);
994 } 968 }
995 return tls13_send_alert(rl, TLS13_ALERT_UNEXPECTED_MESSAGE); 969 return tls13_send_alert(rl, TLS13_ALERT_UNEXPECTED_MESSAGE);
996 } 970 }
997 971
998 if (n > CBS_len(&rl->rbuf_cbs)) 972 if (peek)
999 n = CBS_len(&rl->rbuf_cbs); 973 return tls_content_peek(rl->rcontent, buf, n);
1000 974
1001 /* XXX - CBS_memcpy? CBS_copy_bytes? */ 975 return tls_content_read(rl->rcontent, buf, n);
1002 memcpy(buf, CBS_data(&rl->rbuf_cbs), n);
1003
1004 if (!peek) {
1005 if (!CBS_skip(&rl->rbuf_cbs, n))
1006 goto err;
1007 }
1008
1009 if (CBS_len(&rl->rbuf_cbs) == 0)
1010 tls13_record_layer_rbuf_free(rl);
1011
1012 return n;
1013
1014 err:
1015 return TLS13_IO_FAILURE;
1016} 976}
1017 977
1018static ssize_t 978static ssize_t