summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjsing <>2021-01-19 18:51:08 +0000
committerjsing <>2021-01-19 18:51:08 +0000
commitac4995fa26f1a8ba3ff386c0caf843a423a4abc7 (patch)
tree5ff1746dcd8d564bdb0c60d5075f307fbaeac57d /src
parent0e9595ce9da2c27470d495fbfc1b189eb4a3df24 (diff)
downloadopenbsd-ac4995fa26f1a8ba3ff386c0caf843a423a4abc7.tar.gz
openbsd-ac4995fa26f1a8ba3ff386c0caf843a423a4abc7.tar.bz2
openbsd-ac4995fa26f1a8ba3ff386c0caf843a423a4abc7.zip
Provide record layer overhead for DTLS.
Rather than manually calculating the maximum record layer overhead in the DTLS code, have the record layer provide this information. This also makes it work correctly with AEAD ciphersuites. ok inoguchi@ tb@
Diffstat (limited to 'src')
-rw-r--r--src/lib/libssl/d1_both.c22
-rw-r--r--src/lib/libssl/ssl_locl.h4
-rw-r--r--src/lib/libssl/tls12_record_layer.c29
3 files changed, 38 insertions, 17 deletions
diff --git a/src/lib/libssl/d1_both.c b/src/lib/libssl/d1_both.c
index ae5ebfacb4..d6bf6dfd1b 100644
--- a/src/lib/libssl/d1_both.c
+++ b/src/lib/libssl/d1_both.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: d1_both.c,v 1.63 2020/12/05 19:34:57 tb Exp $ */ 1/* $OpenBSD: d1_both.c,v 1.64 2021/01/19 18:51:08 jsing Exp $ */
2/* 2/*
3 * DTLS implementation written by Nagendra Modadugu 3 * DTLS implementation written by Nagendra Modadugu
4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. 4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
@@ -218,7 +218,8 @@ dtls1_do_write(SSL *s, int type)
218{ 218{
219 int ret; 219 int ret;
220 int curr_mtu; 220 int curr_mtu;
221 unsigned int len, frag_off, mac_size, blocksize; 221 unsigned int len, frag_off;
222 size_t overhead;
222 223
223 /* AHA! Figure out the MTU, and stick to the right size */ 224 /* AHA! Figure out the MTU, and stick to the right size */
224 if (D1I(s)->mtu < dtls1_min_mtu() && 225 if (D1I(s)->mtu < dtls1_min_mtu() &&
@@ -246,21 +247,13 @@ dtls1_do_write(SSL *s, int type)
246 OPENSSL_assert(s->internal->init_num == 247 OPENSSL_assert(s->internal->init_num ==
247 (int)D1I(s)->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH); 248 (int)D1I(s)->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH);
248 249
249 if (s->internal->write_hash) 250 if (!tls12_record_layer_write_overhead(s->internal->rl, &overhead))
250 mac_size = EVP_MD_CTX_size(s->internal->write_hash); 251 return -1;
251 else
252 mac_size = 0;
253
254 if (s->internal->enc_write_ctx &&
255 (EVP_CIPHER_mode( s->internal->enc_write_ctx->cipher) & EVP_CIPH_CBC_MODE))
256 blocksize = 2 * EVP_CIPHER_block_size(s->internal->enc_write_ctx->cipher);
257 else
258 blocksize = 0;
259 252
260 frag_off = 0; 253 frag_off = 0;
261 while (s->internal->init_num) { 254 while (s->internal->init_num) {
262 curr_mtu = D1I(s)->mtu - BIO_wpending(SSL_get_wbio(s)) - 255 curr_mtu = D1I(s)->mtu - BIO_wpending(SSL_get_wbio(s)) -
263 DTLS1_RT_HEADER_LENGTH - mac_size - blocksize; 256 DTLS1_RT_HEADER_LENGTH - overhead;
264 257
265 if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) { 258 if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) {
266 /* grr.. we could get an error if MTU picked was wrong */ 259 /* grr.. we could get an error if MTU picked was wrong */
@@ -268,7 +261,7 @@ dtls1_do_write(SSL *s, int type)
268 if (ret <= 0) 261 if (ret <= 0)
269 return ret; 262 return ret;
270 curr_mtu = D1I(s)->mtu - DTLS1_RT_HEADER_LENGTH - 263 curr_mtu = D1I(s)->mtu - DTLS1_RT_HEADER_LENGTH -
271 mac_size - blocksize; 264 overhead;
272 } 265 }
273 266
274 if (s->internal->init_num > curr_mtu) 267 if (s->internal->init_num > curr_mtu)
@@ -276,7 +269,6 @@ dtls1_do_write(SSL *s, int type)
276 else 269 else
277 len = s->internal->init_num; 270 len = s->internal->init_num;
278 271
279
280 /* XDTLS: this function is too long. split out the CCS part */ 272 /* XDTLS: this function is too long. split out the CCS part */
281 if (type == SSL3_RT_HANDSHAKE) { 273 if (type == SSL3_RT_HANDSHAKE) {
282 if (s->internal->init_off != 0) { 274 if (s->internal->init_off != 0) {
diff --git a/src/lib/libssl/ssl_locl.h b/src/lib/libssl/ssl_locl.h
index 560fcdc1a4..e09f668121 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.312 2021/01/13 18:20:54 jsing Exp $ */ 1/* $OpenBSD: ssl_locl.h,v 1.313 2021/01/19 18:51:08 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 *
@@ -477,6 +477,8 @@ struct tls12_record_layer *tls12_record_layer_new(void);
477void tls12_record_layer_free(struct tls12_record_layer *rl); 477void tls12_record_layer_free(struct tls12_record_layer *rl);
478void tls12_record_layer_alert(struct tls12_record_layer *rl, 478void tls12_record_layer_alert(struct tls12_record_layer *rl,
479 uint8_t *alert_desc); 479 uint8_t *alert_desc);
480int tls12_record_layer_write_overhead(struct tls12_record_layer *rl,
481 size_t *overhead);
480void tls12_record_layer_set_version(struct tls12_record_layer *rl, 482void tls12_record_layer_set_version(struct tls12_record_layer *rl,
481 uint16_t version); 483 uint16_t version);
482void tls12_record_layer_set_write_epoch(struct tls12_record_layer *rl, 484void tls12_record_layer_set_write_epoch(struct tls12_record_layer *rl,
diff --git a/src/lib/libssl/tls12_record_layer.c b/src/lib/libssl/tls12_record_layer.c
index 04699f9a83..7fa31707d3 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.10 2021/01/19 18:34:02 jsing Exp $ */ 1/* $OpenBSD: tls12_record_layer.c,v 1.11 2021/01/19 18:51:08 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2020 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2020 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -168,6 +168,33 @@ tls12_record_layer_alert(struct tls12_record_layer *rl, uint8_t *alert_desc)
168 *alert_desc = rl->alert_desc; 168 *alert_desc = rl->alert_desc;
169} 169}
170 170
171int
172tls12_record_layer_write_overhead(struct tls12_record_layer *rl,
173 size_t *overhead)
174{
175 size_t block_size, eiv_len, mac_len;
176
177 *overhead = 0;
178
179 if (rl->write->aead_ctx != NULL) {
180 *overhead = rl->write->aead_ctx->tag_len;
181 } else if (rl->write->cipher_ctx != NULL) {
182 eiv_len = 0;
183 if (rl->version != TLS1_VERSION) {
184 if (!tls12_record_protection_eiv_len(rl->write, &eiv_len))
185 return 0;
186 }
187 if (!tls12_record_protection_block_size(rl->write, &block_size))
188 return 0;
189 if (!tls12_record_protection_mac_len(rl->write, &mac_len))
190 return 0;
191
192 *overhead = eiv_len + block_size + mac_len;
193 }
194
195 return 1;
196}
197
171void 198void
172tls12_record_layer_set_version(struct tls12_record_layer *rl, uint16_t version) 199tls12_record_layer_set_version(struct tls12_record_layer *rl, uint16_t version)
173{ 200{