From ab5ddd1b3fb2d9af1be6366202432c66048d8b28 Mon Sep 17 00:00:00 2001 From: jsing Date: Sat, 4 Sep 2021 14:24:28 +0000 Subject: [PATCH] Change dtls1_get_message_header() to take a CBS. The callers know the actual length and can initialise a CBS correctly. ok inoguchi@ tb@ --- lib/libssl/d1_both.c | 32 +++++++++++++++----------------- lib/libssl/d1_pkt.c | 6 ++++-- lib/libssl/dtls_locl.h | 5 ++--- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/lib/libssl/d1_both.c b/lib/libssl/d1_both.c index 61dc47b4b74..4c014be6a9f 100644 --- a/lib/libssl/d1_both.c +++ b/lib/libssl/d1_both.c @@ -1,4 +1,4 @@ -/* $OpenBSD: d1_both.c,v 1.77 2021/07/19 08:42:24 jsing Exp $ */ +/* $OpenBSD: d1_both.c,v 1.78 2021/09/04 14:24:28 jsing Exp $ */ /* * DTLS implementation written by Nagendra Modadugu * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. @@ -744,8 +744,9 @@ dtls1_get_message_fragment(SSL *s, int st1, int stn, long max, int *ok) { unsigned char wire[DTLS1_HM_HEADER_LENGTH]; unsigned long len, frag_off, frag_len; - int i, al; struct hm_header_st msg_hdr; + int i, al; + CBS cbs; again: /* see if we have the required fragment already */ @@ -758,16 +759,16 @@ dtls1_get_message_fragment(SSL *s, int st1, int stn, long max, int *ok) /* read handshake message header */ i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, wire, DTLS1_HM_HEADER_LENGTH, 0); - if (i <= 0) /* nbio, or an error */ - { + if (i <= 0) { + /* nbio, or an error */ s->internal->rwstate = SSL_READING; *ok = 0; return i; } - /* Handshake fails if message header is incomplete */ - if (i != DTLS1_HM_HEADER_LENGTH || - /* parse the message fragment header */ - dtls1_get_message_header(wire, &msg_hdr) == 0) { + + CBS_init(&cbs, wire, i); + if (!dtls1_get_message_header(&cbs, &msg_hdr)) { + /* Handshake fails if message header is incomplete. */ al = SSL_AD_UNEXPECTED_MESSAGE; SSLerror(s, SSL_R_UNEXPECTED_MESSAGE); goto fatal_err; @@ -1172,26 +1173,23 @@ dtls1_guess_mtu(unsigned int curr_mtu) } int -dtls1_get_message_header(unsigned char *data, struct hm_header_st *msg_hdr) +dtls1_get_message_header(CBS *header, struct hm_header_st *msg_hdr) { - CBS header; uint32_t msg_len, frag_off, frag_len; uint16_t seq; uint8_t type; - CBS_init(&header, data, sizeof(*msg_hdr)); - memset(msg_hdr, 0, sizeof(*msg_hdr)); - if (!CBS_get_u8(&header, &type)) + if (!CBS_get_u8(header, &type)) return 0; - if (!CBS_get_u24(&header, &msg_len)) + if (!CBS_get_u24(header, &msg_len)) return 0; - if (!CBS_get_u16(&header, &seq)) + if (!CBS_get_u16(header, &seq)) return 0; - if (!CBS_get_u24(&header, &frag_off)) + if (!CBS_get_u24(header, &frag_off)) return 0; - if (!CBS_get_u24(&header, &frag_len)) + if (!CBS_get_u24(header, &frag_len)) return 0; msg_hdr->type = type; diff --git a/lib/libssl/d1_pkt.c b/lib/libssl/d1_pkt.c index 11e6d7f8f86..0b952cf5f32 100644 --- a/lib/libssl/d1_pkt.c +++ b/lib/libssl/d1_pkt.c @@ -1,4 +1,4 @@ -/* $OpenBSD: d1_pkt.c,v 1.110 2021/09/04 14:15:52 jsing Exp $ */ +/* $OpenBSD: d1_pkt.c,v 1.111 2021/09/04 14:24:28 jsing Exp $ */ /* * DTLS implementation written by Nagendra Modadugu * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. @@ -807,9 +807,11 @@ dtls1_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek) rr->length >= DTLS1_HM_HEADER_LENGTH && rr->off == 0 && !s->internal->in_handshake) { struct hm_header_st msg_hdr; + CBS cbs; /* this may just be a stale retransmit */ - if (!dtls1_get_message_header(rr->data, &msg_hdr)) + CBS_init(&cbs, rr->data, rr->length); + if (!dtls1_get_message_header(&cbs, &msg_hdr)) return -1; if (rr->epoch != tls12_record_layer_read_epoch(s->internal->rl)) { rr->length = 0; diff --git a/lib/libssl/dtls_locl.h b/lib/libssl/dtls_locl.h index 502b42dcdd8..4cf8827ec31 100644 --- a/lib/libssl/dtls_locl.h +++ b/lib/libssl/dtls_locl.h @@ -1,4 +1,4 @@ -/* $OpenBSD: dtls_locl.h,v 1.6 2021/08/31 13:34:55 jsing Exp $ */ +/* $OpenBSD: dtls_locl.h,v 1.7 2021/09/04 14:24:28 jsing Exp $ */ /* * DTLS implementation written by Nagendra Modadugu * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. @@ -206,8 +206,7 @@ int dtls1_retransmit_message(SSL *s, unsigned short seq, int dtls1_get_queue_priority(unsigned short seq, int is_ccs); int dtls1_retransmit_buffered_messages(SSL *s); void dtls1_clear_record_buffer(SSL *s); -int dtls1_get_message_header(unsigned char *data, - struct hm_header_st *msg_hdr); +int dtls1_get_message_header(CBS *header, struct hm_header_st *msg_hdr); void dtls1_reset_read_seq_numbers(SSL *s); struct timeval* dtls1_get_timeout(SSL *s, struct timeval* timeleft); int dtls1_check_timeout_num(SSL *s); -- 2.20.1