summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjsing <>2020-10-07 07:46:18 +0000
committerjsing <>2020-10-07 07:46:18 +0000
commit42db3438e170653a0dea8617f6b9a9f5f25fd2be (patch)
tree798bca469eaa323240a2e9ea4adae4ba698cf94c /src
parent5da2fd84e6a7a1bc9c1fe0780bdeabf515e71884 (diff)
downloadopenbsd-42db3438e170653a0dea8617f6b9a9f5f25fd2be.tar.gz
openbsd-42db3438e170653a0dea8617f6b9a9f5f25fd2be.tar.bz2
openbsd-42db3438e170653a0dea8617f6b9a9f5f25fd2be.zip
Include a TLS record header when switching to the legacy stack.
When switching to the legacy TLS stack we previously copied any remaining handshake messages into the receive buffer, but do not include any TLS record header (largely due to the fact that we've already processed part of the TLS record that we actually received - that part is placed into the init_buf). This worked fine with the old record layer implementation, however the new record layer expects to find the TLS record header. This means that if we switch from the new stack to the legacy stack (i.e. the remote side does not support TLSv1.3) and there is more than one handshake message in the TLS plaintext record (which Microsoft's TLS stack is known to do), we now read a TLS record of zero bytes instead of getting the correct length. Fix this by generating a pseudo-TLS record header when switching from the new TLS stack to the legacy stack. Found the hard way by guenther@. Thanks to tb@ for coming up with a reproducible test case and doing much of the debugging. ok inoguchi@ tb@
Diffstat (limited to 'src')
-rw-r--r--src/lib/libssl/tls13_legacy.c36
1 files changed, 27 insertions, 9 deletions
diff --git a/src/lib/libssl/tls13_legacy.c b/src/lib/libssl/tls13_legacy.c
index 317a1cb0f5..a26afeeeb9 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.13 2020/09/13 15:04:35 jsing Exp $ */ 1/* $OpenBSD: tls13_legacy.c,v 1.14 2020/10/07 07:46:18 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 *
@@ -297,22 +297,35 @@ static int
297tls13_use_legacy_stack(struct tls13_ctx *ctx) 297tls13_use_legacy_stack(struct tls13_ctx *ctx)
298{ 298{
299 SSL *s = ctx->ssl; 299 SSL *s = ctx->ssl;
300 CBB cbb, fragment;
300 CBS cbs; 301 CBS cbs;
301 302
303 memset(&cbb, 0, sizeof(cbb));
304
302 if (!ssl3_setup_init_buffer(s)) 305 if (!ssl3_setup_init_buffer(s))
303 return 0; 306 goto err;
304 if (!ssl3_setup_buffers(s)) 307 if (!ssl3_setup_buffers(s))
305 return 0; 308 goto err;
306 if (!ssl_init_wbio_buffer(s, 1)) 309 if (!ssl_init_wbio_buffer(s, 1))
307 return 0; 310 goto err;
308 311
309 /* Stash any unprocessed data from the last record. */ 312 /* Stash any unprocessed data from the last record. */
310 tls13_record_layer_rbuf(ctx->rl, &cbs); 313 tls13_record_layer_rbuf(ctx->rl, &cbs);
311 if (CBS_len(&cbs) > 0) { 314 if (CBS_len(&cbs) > 0) {
312 if (!CBS_write_bytes(&cbs, 315 if (!CBB_init_fixed(&cbb, S3I(s)->rbuf.buf,
313 S3I(s)->rbuf.buf + SSL3_RT_HEADER_LENGTH, 316 S3I(s)->rbuf.len))
314 S3I(s)->rbuf.len - SSL3_RT_HEADER_LENGTH, NULL)) 317 goto err;
315 return 0; 318 if (!CBB_add_u8(&cbb, SSL3_RT_HANDSHAKE))
319 goto err;
320 if (!CBB_add_u16(&cbb, TLS1_2_VERSION))
321 goto err;
322 if (!CBB_add_u16_length_prefixed(&cbb, &fragment))
323 goto err;
324 if (!CBB_add_bytes(&fragment, CBS_data(&cbs),
325 CBS_len(&cbs)))
326 goto err;
327 if (!CBB_finish(&cbb, NULL, NULL))
328 goto err;
316 329
317 S3I(s)->rbuf.offset = SSL3_RT_HEADER_LENGTH; 330 S3I(s)->rbuf.offset = SSL3_RT_HEADER_LENGTH;
318 S3I(s)->rbuf.left = CBS_len(&cbs); 331 S3I(s)->rbuf.left = CBS_len(&cbs);
@@ -328,13 +341,18 @@ tls13_use_legacy_stack(struct tls13_ctx *ctx)
328 tls13_handshake_msg_data(ctx->hs_msg, &cbs); 341 tls13_handshake_msg_data(ctx->hs_msg, &cbs);
329 if (!CBS_write_bytes(&cbs, s->internal->init_buf->data, 342 if (!CBS_write_bytes(&cbs, s->internal->init_buf->data,
330 s->internal->init_buf->length, NULL)) 343 s->internal->init_buf->length, NULL))
331 return 0; 344 goto err;
332 345
333 S3I(s)->tmp.reuse_message = 1; 346 S3I(s)->tmp.reuse_message = 1;
334 S3I(s)->tmp.message_type = tls13_handshake_msg_type(ctx->hs_msg); 347 S3I(s)->tmp.message_type = tls13_handshake_msg_type(ctx->hs_msg);
335 S3I(s)->tmp.message_size = CBS_len(&cbs); 348 S3I(s)->tmp.message_size = CBS_len(&cbs);
336 349
337 return 1; 350 return 1;
351
352 err:
353 CBB_cleanup(&cbb);
354
355 return 0;
338} 356}
339 357
340int 358int