diff options
-rw-r--r-- | src/lib/libssl/Makefile | 5 | ||||
-rw-r--r-- | src/lib/libssl/tls13_internal.h | 4 | ||||
-rw-r--r-- | src/lib/libssl/tls13_legacy.c | 4 | ||||
-rw-r--r-- | src/lib/libssl/tls13_record_layer.c | 108 | ||||
-rw-r--r-- | src/lib/libssl/tls_content.c | 149 | ||||
-rw-r--r-- | src/lib/libssl/tls_content.h | 48 |
6 files changed, 238 insertions, 80 deletions
diff --git a/src/lib/libssl/Makefile b/src/lib/libssl/Makefile index 6171194629..d468308c7e 100644 --- a/src/lib/libssl/Makefile +++ b/src/lib/libssl/Makefile | |||
@@ -1,4 +1,4 @@ | |||
1 | # $OpenBSD: Makefile,v 1.70 2021/05/05 10:05:27 jsing Exp $ | 1 | # $OpenBSD: Makefile,v 1.71 2021/09/04 16:26:12 jsing Exp $ |
2 | 2 | ||
3 | .include <bsd.own.mk> | 3 | .include <bsd.own.mk> |
4 | .ifndef NOMAN | 4 | .ifndef NOMAN |
@@ -81,7 +81,8 @@ SRCS= \ | |||
81 | tls13_lib.c \ | 81 | tls13_lib.c \ |
82 | tls13_record.c \ | 82 | tls13_record.c \ |
83 | tls13_record_layer.c \ | 83 | tls13_record_layer.c \ |
84 | tls13_server.c | 84 | tls13_server.c \ |
85 | tls_content.c | ||
85 | 86 | ||
86 | HDRS= dtls1.h srtp.h ssl.h ssl2.h ssl23.h ssl3.h tls1.h | 87 | HDRS= dtls1.h srtp.h ssl.h ssl2.h ssl23.h ssl3.h tls1.h |
87 | 88 | ||
diff --git a/src/lib/libssl/tls13_internal.h b/src/lib/libssl/tls13_internal.h index 30ef7dd931..12ed733f2b 100644 --- a/src/lib/libssl/tls13_internal.h +++ b/src/lib/libssl/tls13_internal.h | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: tls13_internal.h,v 1.90 2021/06/28 15:35:14 tb Exp $ */ | 1 | /* $OpenBSD: tls13_internal.h,v 1.91 2021/09/04 16:26:12 jsing Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2018 Bob Beck <beck@openbsd.org> | 3 | * Copyright (c) 2018 Bob Beck <beck@openbsd.org> |
4 | * Copyright (c) 2018 Theo Buehler <tb@openbsd.org> | 4 | * Copyright (c) 2018 Theo Buehler <tb@openbsd.org> |
@@ -209,7 +209,7 @@ struct tls13_record_layer *tls13_record_layer_new( | |||
209 | void tls13_record_layer_free(struct tls13_record_layer *rl); | 209 | void tls13_record_layer_free(struct tls13_record_layer *rl); |
210 | void tls13_record_layer_allow_ccs(struct tls13_record_layer *rl, int allow); | 210 | void tls13_record_layer_allow_ccs(struct tls13_record_layer *rl, int allow); |
211 | void tls13_record_layer_allow_legacy_alerts(struct tls13_record_layer *rl, int allow); | 211 | void tls13_record_layer_allow_legacy_alerts(struct tls13_record_layer *rl, int allow); |
212 | void tls13_record_layer_rbuf(struct tls13_record_layer *rl, CBS *cbs); | 212 | void tls13_record_layer_rcontent(struct tls13_record_layer *rl, CBS *cbs); |
213 | void tls13_record_layer_set_aead(struct tls13_record_layer *rl, | 213 | void tls13_record_layer_set_aead(struct tls13_record_layer *rl, |
214 | const EVP_AEAD *aead); | 214 | const EVP_AEAD *aead); |
215 | void tls13_record_layer_set_hash(struct tls13_record_layer *rl, | 215 | void tls13_record_layer_set_hash(struct tls13_record_layer *rl, |
diff --git a/src/lib/libssl/tls13_legacy.c b/src/lib/libssl/tls13_legacy.c index 477d09d63e..df4408d903 100644 --- a/src/lib/libssl/tls13_legacy.c +++ b/src/lib/libssl/tls13_legacy.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: tls13_legacy.c,v 1.28 2021/09/03 13:16:54 jsing Exp $ */ | 1 | /* $OpenBSD: tls13_legacy.c,v 1.29 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 | * |
@@ -304,7 +304,7 @@ tls13_use_legacy_stack(struct tls13_ctx *ctx) | |||
304 | goto err; | 304 | goto err; |
305 | 305 | ||
306 | /* Stash any unprocessed data from the last record. */ | 306 | /* Stash any unprocessed data from the last record. */ |
307 | tls13_record_layer_rbuf(ctx->rl, &cbs); | 307 | tls13_record_layer_rcontent(ctx->rl, &cbs); |
308 | if (CBS_len(&cbs) > 0) { | 308 | if (CBS_len(&cbs) > 0) { |
309 | if (!CBB_init_fixed(&cbb, S3I(s)->rbuf.buf, | 309 | if (!CBB_init_fixed(&cbb, S3I(s)->rbuf.buf, |
310 | S3I(s)->rbuf.len)) | 310 | S3I(s)->rbuf.len)) |
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 | ||
21 | static ssize_t tls13_record_layer_write_chunk(struct tls13_record_layer *rl, | 22 | static 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 | ||
119 | static void | 117 | static void |
120 | tls13_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 | |||
129 | static void | ||
130 | tls13_record_layer_rrec_free(struct tls13_record_layer *rl) | 118 | tls13_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 | ||
189 | void | 180 | void |
190 | tls13_record_layer_rbuf(struct tls13_record_layer *rl, CBS *cbs) | 181 | tls13_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 | ||
195 | static const uint8_t tls13_max_seq_num[TLS13_RECORD_SEQ_NUM_LEN] = { | 186 | static 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) | |||
907 | static ssize_t | 883 | static ssize_t |
908 | tls13_record_layer_pending(struct tls13_record_layer *rl, uint8_t content_type) | 884 | tls13_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 | ||
916 | static ssize_t | 892 | static 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 | ||
1018 | static ssize_t | 978 | static ssize_t |
diff --git a/src/lib/libssl/tls_content.c b/src/lib/libssl/tls_content.c new file mode 100644 index 0000000000..ede178f84c --- /dev/null +++ b/src/lib/libssl/tls_content.c | |||
@@ -0,0 +1,149 @@ | |||
1 | /* $OpenBSD: tls_content.c,v 1.1 2021/09/04 16:26:12 jsing Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2020 Joel Sing <jsing@openbsd.org> | ||
4 | * | ||
5 | * Permission to use, copy, modify, and distribute this software for any | ||
6 | * purpose with or without fee is hereby granted, provided that the above | ||
7 | * copyright notice and this permission notice appear in all copies. | ||
8 | * | ||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | */ | ||
17 | |||
18 | #include <stdlib.h> | ||
19 | #include <string.h> | ||
20 | |||
21 | #include "tls_content.h" | ||
22 | |||
23 | /* Content from a TLS record. */ | ||
24 | struct tls_content { | ||
25 | uint8_t type; | ||
26 | uint16_t epoch; | ||
27 | |||
28 | const uint8_t *data; | ||
29 | size_t len; | ||
30 | CBS cbs; | ||
31 | }; | ||
32 | |||
33 | struct tls_content * | ||
34 | tls_content_new(void) | ||
35 | { | ||
36 | return calloc(1, sizeof(struct tls_content)); | ||
37 | } | ||
38 | |||
39 | void | ||
40 | tls_content_clear(struct tls_content *content) | ||
41 | { | ||
42 | freezero((void *)content->data, content->len); | ||
43 | memset(content, 0, sizeof(*content)); | ||
44 | } | ||
45 | |||
46 | void | ||
47 | tls_content_free(struct tls_content *content) | ||
48 | { | ||
49 | if (content == NULL) | ||
50 | return; | ||
51 | |||
52 | tls_content_clear(content); | ||
53 | |||
54 | freezero(content, sizeof(struct tls_content)); | ||
55 | } | ||
56 | |||
57 | CBS * | ||
58 | tls_content_cbs(struct tls_content *content) | ||
59 | { | ||
60 | return &content->cbs; | ||
61 | } | ||
62 | |||
63 | int | ||
64 | tls_content_equal(struct tls_content *content, const uint8_t *buf, size_t n) | ||
65 | { | ||
66 | return CBS_mem_equal(&content->cbs, buf, n); | ||
67 | } | ||
68 | |||
69 | size_t | ||
70 | tls_content_remaining(struct tls_content *content) | ||
71 | { | ||
72 | return CBS_len(&content->cbs); | ||
73 | } | ||
74 | |||
75 | uint8_t | ||
76 | tls_content_type(struct tls_content *content) | ||
77 | { | ||
78 | return content->type; | ||
79 | } | ||
80 | |||
81 | int | ||
82 | tls_content_dup_data(struct tls_content *content, uint8_t type, | ||
83 | const uint8_t *data, size_t data_len) | ||
84 | { | ||
85 | uint8_t *dup; | ||
86 | |||
87 | if ((dup = calloc(1, data_len)) == NULL) | ||
88 | return 0; | ||
89 | memcpy(dup, data, data_len); | ||
90 | |||
91 | tls_content_set_data(content, type, dup, data_len); | ||
92 | |||
93 | return 1; | ||
94 | } | ||
95 | |||
96 | uint16_t | ||
97 | tls_content_epoch(struct tls_content *content) | ||
98 | { | ||
99 | return content->epoch; | ||
100 | } | ||
101 | |||
102 | void | ||
103 | tls_content_set_epoch(struct tls_content *content, uint16_t epoch) | ||
104 | { | ||
105 | content->epoch = epoch; | ||
106 | } | ||
107 | |||
108 | void | ||
109 | tls_content_set_data(struct tls_content *content, uint8_t type, | ||
110 | const uint8_t *data, size_t data_len) | ||
111 | { | ||
112 | tls_content_clear(content); | ||
113 | |||
114 | content->type = type; | ||
115 | content->data = data; | ||
116 | content->len = data_len; | ||
117 | |||
118 | CBS_init(&content->cbs, content->data, content->len); | ||
119 | } | ||
120 | |||
121 | static ssize_t | ||
122 | tls_content_read_internal(struct tls_content *content, uint8_t *buf, size_t n, | ||
123 | int peek) | ||
124 | { | ||
125 | if (n > CBS_len(&content->cbs)) | ||
126 | n = CBS_len(&content->cbs); | ||
127 | |||
128 | /* XXX - CBS_memcpy? CBS_copy_bytes? */ | ||
129 | memcpy(buf, CBS_data(&content->cbs), n); | ||
130 | |||
131 | if (!peek) { | ||
132 | if (!CBS_skip(&content->cbs, n)) | ||
133 | return -1; | ||
134 | } | ||
135 | |||
136 | return n; | ||
137 | } | ||
138 | |||
139 | ssize_t | ||
140 | tls_content_peek(struct tls_content *content, uint8_t *buf, size_t n) | ||
141 | { | ||
142 | return tls_content_read_internal(content, buf, n, 1); | ||
143 | } | ||
144 | |||
145 | ssize_t | ||
146 | tls_content_read(struct tls_content *content, uint8_t *buf, size_t n) | ||
147 | { | ||
148 | return tls_content_read_internal(content, buf, n, 0); | ||
149 | } | ||
diff --git a/src/lib/libssl/tls_content.h b/src/lib/libssl/tls_content.h new file mode 100644 index 0000000000..173af2a740 --- /dev/null +++ b/src/lib/libssl/tls_content.h | |||
@@ -0,0 +1,48 @@ | |||
1 | /* $OpenBSD: tls_content.h,v 1.1 2021/09/04 16:26:12 jsing Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2020 Joel Sing <jsing@openbsd.org> | ||
4 | * | ||
5 | * Permission to use, copy, modify, and distribute this software for any | ||
6 | * purpose with or without fee is hereby granted, provided that the above | ||
7 | * copyright notice and this permission notice appear in all copies. | ||
8 | * | ||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | */ | ||
17 | |||
18 | #ifndef HEADER_TLS_CONTENT_H | ||
19 | #define HEADER_TLS_CONTENT_H | ||
20 | |||
21 | #include "bytestring.h" | ||
22 | |||
23 | __BEGIN_HIDDEN_DECLS | ||
24 | |||
25 | struct tls_content; | ||
26 | |||
27 | struct tls_content *tls_content_new(void); | ||
28 | void tls_content_clear(struct tls_content *content); | ||
29 | void tls_content_free(struct tls_content *content); | ||
30 | |||
31 | CBS *tls_content_cbs(struct tls_content *content); | ||
32 | int tls_content_equal(struct tls_content *content, const uint8_t *buf, size_t n); | ||
33 | size_t tls_content_remaining(struct tls_content *content); | ||
34 | uint8_t tls_content_type(struct tls_content *content); | ||
35 | uint16_t tls_content_epoch(struct tls_content *content); | ||
36 | |||
37 | int tls_content_dup_data(struct tls_content *content, uint8_t type, | ||
38 | const uint8_t *data, size_t data_len); | ||
39 | void tls_content_set_data(struct tls_content *content, uint8_t type, | ||
40 | const uint8_t *data, size_t data_len); | ||
41 | void tls_content_set_epoch(struct tls_content *content, uint16_t epoch); | ||
42 | |||
43 | ssize_t tls_content_peek(struct tls_content *content, uint8_t *buf, size_t n); | ||
44 | ssize_t tls_content_read(struct tls_content *content, uint8_t *buf, size_t n); | ||
45 | |||
46 | __END_HIDDEN_DECLS | ||
47 | |||
48 | #endif | ||