2 * Copyright 2005-2024 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
14 #include "../ssl_local.h"
15 #include "statem_local.h"
16 #include "internal/cryptlib.h"
17 #include <openssl/buffer.h>
18 #include <openssl/objects.h>
19 #include <openssl/evp.h>
20 #include <openssl/x509.h>
22 #define RSMBLY_BITMASK_SIZE(msg_len) (((msg_len) + 7) / 8)
24 #define RSMBLY_BITMASK_MARK(bitmask, start, end) { \
25 if ((end) - (start) <= 8) { \
27 for (ii = (start); ii < (end); ii++) bitmask[((ii) >> 3)] |= (1 << ((ii) & 7)); \
30 bitmask[((start) >> 3)] |= bitmask_start_values[((start) & 7)]; \
31 for (ii = (((start) >> 3) + 1); ii < ((((end) - 1)) >> 3); ii++) bitmask[ii] = 0xff; \
32 bitmask[(((end) - 1) >> 3)] |= bitmask_end_values[((end) & 7)]; \
35 #define RSMBLY_BITMASK_IS_COMPLETE(bitmask, msg_len, is_complete) { \
38 if (bitmask[(((msg_len) - 1) >> 3)] != bitmask_end_values[((msg_len) & 7)]) is_complete = 0; \
39 if (is_complete) for (ii = (((msg_len) - 1) >> 3) - 1; ii >= 0 ; ii--) \
40 if (bitmask[ii] != 0xff) { is_complete = 0; break; } }
42 static const unsigned char bitmask_start_values[] =
43 { 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80 };
44 static const unsigned char bitmask_end_values[] =
45 { 0xff, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f };
47 static void dtls1_fix_message_header(SSL_CONNECTION *s, size_t frag_off,
49 static unsigned char *dtls1_write_message_header(SSL_CONNECTION *s,
51 static void dtls1_set_message_header_int(SSL_CONNECTION *s, unsigned char mt,
53 unsigned short seq_num,
56 static int dtls_get_reassembled_message(SSL_CONNECTION *s, int *errtype,
59 static hm_fragment *dtls1_hm_fragment_new(size_t frag_len, int reassembly)
61 hm_fragment *frag = NULL;
62 unsigned char *buf = NULL;
63 unsigned char *bitmask = NULL;
65 if ((frag = OPENSSL_zalloc(sizeof(*frag))) == NULL)
69 if ((buf = OPENSSL_malloc(frag_len)) == NULL) {
75 /* zero length fragment gets zero frag->fragment */
78 /* Initialize reassembly bitmask if necessary */
80 bitmask = OPENSSL_zalloc(RSMBLY_BITMASK_SIZE(frag_len));
81 if (bitmask == NULL) {
88 frag->reassembly = bitmask;
93 void dtls1_hm_fragment_free(hm_fragment *frag)
98 OPENSSL_free(frag->fragment);
99 OPENSSL_free(frag->reassembly);
104 * send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
105 * SSL3_RT_CHANGE_CIPHER_SPEC)
107 int dtls1_do_write(SSL_CONNECTION *s, uint8_t type)
113 size_t len, frag_off, overhead, used_len;
114 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
116 if (!dtls1_query_mtu(s))
119 if (s->d1->mtu < dtls1_min_mtu(s))
120 /* should have something reasonable now */
123 if (s->init_off == 0 && type == SSL3_RT_HANDSHAKE) {
124 if (!ossl_assert(s->init_num ==
125 s->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH))
129 overhead = s->rlayer.wrlmethod->get_max_record_overhead(s->rlayer.wrl);
132 s->rwstate = SSL_NOTHING;
134 /* s->init_num shouldn't ever be < 0...but just in case */
135 while (s->init_num > 0) {
136 if (type == SSL3_RT_HANDSHAKE && s->init_off != 0) {
137 /* We must be writing a fragment other than the first one */
140 /* This is the first attempt at writing out this fragment */
142 if (s->init_off <= DTLS1_HM_HEADER_LENGTH) {
144 * Each fragment that was already sent must at least have
145 * contained the message header plus one other byte.
146 * Therefore |init_off| must have progressed by at least
147 * |DTLS1_HM_HEADER_LENGTH + 1| bytes. If not something went
154 * Adjust |init_off| and |init_num| to allow room for a new
155 * message header for this fragment.
157 s->init_off -= DTLS1_HM_HEADER_LENGTH;
158 s->init_num += DTLS1_HM_HEADER_LENGTH;
161 * We must have been called again after a retry so use the
162 * fragment offset from our last attempt. We do not need
163 * to adjust |init_off| and |init_num| as above, because
164 * that should already have been done before the retry.
166 frag_off = s->d1->w_msg_hdr.frag_off;
170 used_len = BIO_wpending(s->wbio) + overhead;
171 if (s->d1->mtu > used_len)
172 curr_mtu = s->d1->mtu - used_len;
176 if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) {
178 * grr.. we could get an error if MTU picked was wrong
180 ret = BIO_flush(s->wbio);
182 s->rwstate = SSL_WRITING;
185 if (s->d1->mtu > overhead + DTLS1_HM_HEADER_LENGTH) {
186 curr_mtu = s->d1->mtu - overhead;
188 /* Shouldn't happen */
194 * We just checked that s->init_num > 0 so this cast should be safe
196 if (((unsigned int)s->init_num) > curr_mtu)
201 if (len > ssl_get_max_send_fragment(s))
202 len = ssl_get_max_send_fragment(s);
205 * XDTLS: this function is too long. split out the CCS part
207 if (type == SSL3_RT_HANDSHAKE) {
208 if (len < DTLS1_HM_HEADER_LENGTH) {
210 * len is so small that we really can't do anything sensible
215 dtls1_fix_message_header(s, frag_off, len - DTLS1_HM_HEADER_LENGTH);
217 dtls1_write_message_header(s,
218 (unsigned char *)&s->init_buf->
222 ret = dtls1_write_bytes(s, type, &s->init_buf->data[s->init_off], len,
226 * might need to update MTU here, but we don't know which
227 * previous packet caused the failure -- so can't really
228 * retransmit anything. continue as if everything is fine and
229 * wait for an alert to handle the retransmit
231 if (retry && BIO_ctrl(SSL_get_wbio(ssl),
232 BIO_CTRL_DGRAM_MTU_EXCEEDED, 0, NULL) > 0) {
233 if (!(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
234 if (!dtls1_query_mtu(s))
236 /* Have one more go */
246 * bad if this assert fails, only part of the handshake message
247 * got sent. but why would this happen?
249 if (!ossl_assert(len == written))
253 * We should not exceed the MTU size. If compression is in use
254 * then the max record overhead calculation is unreliable so we do
255 * not check in that case. We use assert rather than ossl_assert
256 * because in a production build, if this assert were ever to fail,
257 * then the best thing to do is probably carry on regardless.
259 assert(s->s3.tmp.new_compression != NULL
260 || BIO_wpending(s->wbio) <= (int)s->d1->mtu);
262 if (type == SSL3_RT_HANDSHAKE && !s->d1->retransmitting) {
264 * should not be done for 'Hello Request's, but in that case
265 * we'll ignore the result anyway
268 (unsigned char *)&s->init_buf->data[s->init_off];
269 const struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
272 if (frag_off == 0 && s->version != DTLS1_BAD_VER) {
274 * reconstruct message header is if it is being sent in
277 *p++ = msg_hdr->type;
278 l2n3(msg_hdr->msg_len, p);
279 s2n(msg_hdr->seq, p);
281 l2n3(msg_hdr->msg_len, p);
282 p -= DTLS1_HM_HEADER_LENGTH;
285 p += DTLS1_HM_HEADER_LENGTH;
286 xlen = written - DTLS1_HM_HEADER_LENGTH;
289 if (!ssl3_finish_mac(s, p, xlen))
293 if (written == s->init_num) {
295 s->msg_callback(1, s->version, type, s->init_buf->data,
296 (size_t)(s->init_off + s->init_num), ssl,
297 s->msg_callback_arg);
299 s->init_off = 0; /* done writing this message */
304 s->init_off += written;
305 s->init_num -= written;
306 written -= DTLS1_HM_HEADER_LENGTH;
310 * We save the fragment offset for the next fragment so we have it
311 * available in case of an IO retry. We don't know the length of the
312 * next fragment yet so just set that to 0 for now. It will be
313 * updated again later.
315 dtls1_fix_message_header(s, frag_off, 0);
321 int dtls_get_message(SSL_CONNECTION *s, int *mt)
323 struct hm_header_st *msg_hdr;
329 msg_hdr = &s->d1->r_msg_hdr;
330 memset(msg_hdr, 0, sizeof(*msg_hdr));
333 if (!dtls_get_reassembled_message(s, &errtype, &tmplen)) {
334 if (errtype == DTLS1_HM_BAD_FRAGMENT
335 || errtype == DTLS1_HM_FRAGMENT_RETRY) {
336 /* bad fragment received */
342 *mt = s->s3.tmp.message_type;
344 p = (unsigned char *)s->init_buf->data;
346 if (*mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
347 if (s->msg_callback) {
348 s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC,
349 p, 1, SSL_CONNECTION_GET_SSL(s),
350 s->msg_callback_arg);
353 * This isn't a real handshake message so skip the processing below.
358 msg_len = msg_hdr->msg_len;
360 /* reconstruct message header */
361 *(p++) = msg_hdr->type;
363 s2n(msg_hdr->seq, p);
367 memset(msg_hdr, 0, sizeof(*msg_hdr));
369 s->d1->handshake_read_seq++;
371 s->init_msg = s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
377 * Actually we already have the message body - but this is an opportunity for
378 * DTLS to do any further processing it wants at the same point that TLS would
379 * be asked for the message body.
381 int dtls_get_message_body(SSL_CONNECTION *s, size_t *len)
383 unsigned char *msg = (unsigned char *)s->init_buf->data;
384 size_t msg_len = s->init_num + DTLS1_HM_HEADER_LENGTH;
386 if (s->s3.tmp.message_type == SSL3_MT_CHANGE_CIPHER_SPEC) {
387 /* Nothing to be done */
391 * If receiving Finished, record MAC of prior handshake messages for
392 * Finished verification.
394 if (*(s->init_buf->data) == SSL3_MT_FINISHED && !ssl3_take_mac(s)) {
395 /* SSLfatal() already called */
399 if (s->version == DTLS1_BAD_VER) {
400 msg += DTLS1_HM_HEADER_LENGTH;
401 msg_len -= DTLS1_HM_HEADER_LENGTH;
404 if (!ssl3_finish_mac(s, msg, msg_len))
408 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
409 s->init_buf->data, s->init_num + DTLS1_HM_HEADER_LENGTH,
410 SSL_CONNECTION_GET_SSL(s), s->msg_callback_arg);
418 * dtls1_max_handshake_message_len returns the maximum number of bytes
419 * permitted in a DTLS handshake message for |s|. The minimum is 16KB, but
420 * may be greater if the maximum certificate list size requires it.
422 static size_t dtls1_max_handshake_message_len(const SSL_CONNECTION *s)
424 size_t max_len = DTLS1_HM_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH;
425 if (max_len < s->max_cert_list)
426 return s->max_cert_list;
430 static int dtls1_preprocess_fragment(SSL_CONNECTION *s,
431 struct hm_header_st *msg_hdr)
433 size_t frag_off, frag_len, msg_len;
435 msg_len = msg_hdr->msg_len;
436 frag_off = msg_hdr->frag_off;
437 frag_len = msg_hdr->frag_len;
439 /* sanity checking */
440 if ((frag_off + frag_len) > msg_len
441 || msg_len > dtls1_max_handshake_message_len(s)) {
442 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_EXCESSIVE_MESSAGE_SIZE);
446 if (s->d1->r_msg_hdr.frag_off == 0) { /* first fragment */
448 * msg_len is limited to 2^24, but is effectively checked against
449 * dtls_max_handshake_message_len(s) above
451 if (!BUF_MEM_grow_clean(s->init_buf, msg_len + DTLS1_HM_HEADER_LENGTH)) {
452 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BUF_LIB);
456 s->s3.tmp.message_size = msg_len;
457 s->d1->r_msg_hdr.msg_len = msg_len;
458 s->s3.tmp.message_type = msg_hdr->type;
459 s->d1->r_msg_hdr.type = msg_hdr->type;
460 s->d1->r_msg_hdr.seq = msg_hdr->seq;
461 } else if (msg_len != s->d1->r_msg_hdr.msg_len) {
463 * They must be playing with us! BTW, failure to enforce upper limit
464 * would open possibility for buffer overrun.
466 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_EXCESSIVE_MESSAGE_SIZE);
474 * Returns 1 if there is a buffered fragment available, 0 if not, or -1 on a
477 static int dtls1_retrieve_buffered_fragment(SSL_CONNECTION *s, size_t *len)
480 * (0) check whether the desired fragment is available
482 * (1) copy over the fragment to s->init_buf->data[]
483 * (2) update s->init_num
491 iter = pqueue_iterator(s->d1->buffered_messages);
493 item = pqueue_next(&iter);
497 frag = (hm_fragment *)item->data;
499 if (frag->msg_header.seq < s->d1->handshake_read_seq) {
501 hm_fragment *nextfrag;
504 || frag->msg_header.seq != 0
505 || s->d1->handshake_read_seq != 1
506 || s->statem.hand_state != DTLS_ST_SW_HELLO_VERIFY_REQUEST) {
508 * This is a stale message that has been buffered so clear it.
509 * It is safe to pop this message from the queue even though
510 * we have an active iterator
512 pqueue_pop(s->d1->buffered_messages);
513 dtls1_hm_fragment_free(frag);
519 * We have fragments for a ClientHello without a cookie,
520 * even though we have sent a HelloVerifyRequest. It is possible
521 * that the HelloVerifyRequest got lost and this is a
522 * retransmission of the original ClientHello
524 next = pqueue_next(&iter);
526 nextfrag = (hm_fragment *)next->data;
527 if (nextfrag->msg_header.seq == s->d1->handshake_read_seq) {
529 * We have fragments for both a ClientHello without
530 * cookie and one with. Ditch the one without.
532 pqueue_pop(s->d1->buffered_messages);
533 dtls1_hm_fragment_free(frag);
545 } while (item == NULL);
547 /* Don't return if reassembly still in progress */
548 if (frag->reassembly != NULL)
551 if (s->d1->handshake_read_seq == frag->msg_header.seq || chretran) {
552 size_t frag_len = frag->msg_header.frag_len;
553 pqueue_pop(s->d1->buffered_messages);
555 /* Calls SSLfatal() as required */
556 ret = dtls1_preprocess_fragment(s, &frag->msg_header);
558 if (ret && frag->msg_header.frag_len > 0) {
560 (unsigned char *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
561 memcpy(&p[frag->msg_header.frag_off], frag->fragment,
562 frag->msg_header.frag_len);
565 dtls1_hm_fragment_free(frag);
571 * We got a new ClientHello with a message sequence of 0.
572 * Reset the read/write sequences back to the beginning.
573 * We process it like this is the first time we've seen a
574 * ClientHello from the client.
576 s->d1->handshake_read_seq = 0;
577 s->d1->next_handshake_write_seq = 0;
591 static int dtls1_reassemble_fragment(SSL_CONNECTION *s,
592 const struct hm_header_st *msg_hdr)
594 hm_fragment *frag = NULL;
596 int i = -1, is_complete;
597 unsigned char seq64be[8];
598 size_t frag_len = msg_hdr->frag_len;
600 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
602 if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len ||
603 msg_hdr->msg_len > dtls1_max_handshake_message_len(s))
607 return DTLS1_HM_FRAGMENT_RETRY;
610 /* Try to find item in queue */
611 memset(seq64be, 0, sizeof(seq64be));
612 seq64be[6] = (unsigned char)(msg_hdr->seq >> 8);
613 seq64be[7] = (unsigned char)msg_hdr->seq;
614 item = pqueue_find(s->d1->buffered_messages, seq64be);
617 frag = dtls1_hm_fragment_new(msg_hdr->msg_len, 1);
620 memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
621 frag->msg_header.frag_len = frag->msg_header.msg_len;
622 frag->msg_header.frag_off = 0;
624 frag = (hm_fragment *)item->data;
625 if (frag->msg_header.msg_len != msg_hdr->msg_len) {
633 * If message is already reassembled, this must be a retransmit and can
634 * be dropped. In this case item != NULL and so frag does not need to be
637 if (frag->reassembly == NULL) {
638 unsigned char devnull[256];
641 i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL,
644 sizeof(devnull) ? sizeof(devnull) :
645 frag_len, 0, &readbytes);
648 frag_len -= readbytes;
650 return DTLS1_HM_FRAGMENT_RETRY;
653 /* read the body of the fragment (header has already been read */
654 i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL,
655 frag->fragment + msg_hdr->frag_off,
656 frag_len, 0, &readbytes);
657 if (i <= 0 || readbytes != frag_len)
662 RSMBLY_BITMASK_MARK(frag->reassembly, (long)msg_hdr->frag_off,
663 (long)(msg_hdr->frag_off + frag_len));
665 if (!ossl_assert(msg_hdr->msg_len > 0))
667 RSMBLY_BITMASK_IS_COMPLETE(frag->reassembly, (long)msg_hdr->msg_len,
671 OPENSSL_free(frag->reassembly);
672 frag->reassembly = NULL;
676 item = pitem_new(seq64be, frag);
682 item = pqueue_insert(s->d1->buffered_messages, item);
684 * pqueue_insert fails iff a duplicate item is inserted. However,
685 * |item| cannot be a duplicate. If it were, |pqueue_find|, above,
686 * would have returned it and control would never have reached this
689 if (!ossl_assert(item != NULL))
693 return DTLS1_HM_FRAGMENT_RETRY;
697 dtls1_hm_fragment_free(frag);
701 static int dtls1_process_out_of_seq_message(SSL_CONNECTION *s,
702 const struct hm_header_st *msg_hdr)
705 hm_fragment *frag = NULL;
707 unsigned char seq64be[8];
708 size_t frag_len = msg_hdr->frag_len;
710 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
712 if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len)
715 /* Try to find item in queue, to prevent duplicate entries */
716 memset(seq64be, 0, sizeof(seq64be));
717 seq64be[6] = (unsigned char)(msg_hdr->seq >> 8);
718 seq64be[7] = (unsigned char)msg_hdr->seq;
719 item = pqueue_find(s->d1->buffered_messages, seq64be);
722 * If we already have an entry and this one is a fragment, don't discard
723 * it and rather try to reassemble it.
725 if (item != NULL && frag_len != msg_hdr->msg_len)
729 * Discard the message if sequence number was already there, is too far
730 * in the future, already in the queue or if we received a FINISHED
731 * before the SERVER_HELLO, which then must be a stale retransmit.
733 if (msg_hdr->seq <= s->d1->handshake_read_seq ||
734 msg_hdr->seq > s->d1->handshake_read_seq + 10 || item != NULL ||
735 (s->d1->handshake_read_seq == 0 && msg_hdr->type == SSL3_MT_FINISHED)) {
736 unsigned char devnull[256];
739 i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL,
742 sizeof(devnull) ? sizeof(devnull) :
743 frag_len, 0, &readbytes);
746 frag_len -= readbytes;
749 if (frag_len != msg_hdr->msg_len) {
750 return dtls1_reassemble_fragment(s, msg_hdr);
753 if (frag_len > dtls1_max_handshake_message_len(s))
756 frag = dtls1_hm_fragment_new(frag_len, 0);
760 memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
764 * read the body of the fragment (header has already been read
766 i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL,
767 frag->fragment, frag_len, 0,
769 if (i<=0 || readbytes != frag_len)
775 item = pitem_new(seq64be, frag);
779 item = pqueue_insert(s->d1->buffered_messages, item);
781 * pqueue_insert fails iff a duplicate item is inserted. However,
782 * |item| cannot be a duplicate. If it were, |pqueue_find|, above,
783 * would have returned it. Then, either |frag_len| !=
784 * |msg_hdr->msg_len| in which case |item| is set to NULL and it will
785 * have been processed with |dtls1_reassemble_fragment|, above, or
786 * the record will have been discarded.
788 if (!ossl_assert(item != NULL))
792 return DTLS1_HM_FRAGMENT_RETRY;
796 dtls1_hm_fragment_free(frag);
800 static int dtls_get_reassembled_message(SSL_CONNECTION *s, int *errtype,
803 size_t mlen, frag_off, frag_len;
806 struct hm_header_st msg_hdr;
808 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
814 p = (unsigned char *)s->init_buf->data;
817 /* see if we have the required fragment already */
818 ret = dtls1_retrieve_buffered_fragment(s, &frag_len);
820 /* SSLfatal() already called */
824 s->init_num = frag_len;
829 /* read handshake message header */
830 i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, &recvd_type, p,
831 DTLS1_HM_HEADER_LENGTH, 0, &readbytes);
832 if (i <= 0) { /* nbio, or an error */
833 s->rwstate = SSL_READING;
837 if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) {
838 if (p[0] != SSL3_MT_CCS) {
839 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
840 SSL_R_BAD_CHANGE_CIPHER_SPEC);
844 s->init_num = readbytes - 1;
845 s->init_msg = s->init_buf->data + 1;
846 s->s3.tmp.message_type = SSL3_MT_CHANGE_CIPHER_SPEC;
847 s->s3.tmp.message_size = readbytes - 1;
848 *len = readbytes - 1;
852 /* Handshake fails if message header is incomplete */
853 if (readbytes != DTLS1_HM_HEADER_LENGTH) {
854 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
858 /* parse the message fragment header */
859 dtls1_get_message_header(p, &msg_hdr);
861 mlen = msg_hdr.msg_len;
862 frag_off = msg_hdr.frag_off;
863 frag_len = msg_hdr.frag_len;
866 * We must have at least frag_len bytes left in the record to be read.
867 * Fragments must not span records.
869 if (frag_len > s->rlayer.tlsrecs[s->rlayer.curr_rec].length) {
870 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_LENGTH);
875 * if this is a future (or stale) message it gets buffered
876 * (or dropped)--no further processing at this time
877 * While listening, we accept seq 1 (ClientHello with cookie)
878 * although we're still expecting seq 0 (ClientHello)
880 if (msg_hdr.seq != s->d1->handshake_read_seq) {
883 || s->d1->handshake_read_seq != 1
884 || p[0] != SSL3_MT_CLIENT_HELLO
885 || s->statem.hand_state != DTLS_ST_SW_HELLO_VERIFY_REQUEST) {
886 *errtype = dtls1_process_out_of_seq_message(s, &msg_hdr);
890 * We received a ClientHello and sent back a HelloVerifyRequest. We
891 * now seem to have received a retransmitted initial ClientHello. That
892 * is allowed (possibly our HelloVerifyRequest got lost).
897 if (frag_len && frag_len < mlen) {
898 *errtype = dtls1_reassemble_fragment(s, &msg_hdr);
903 && s->d1->r_msg_hdr.frag_off == 0
904 && s->statem.hand_state != TLS_ST_OK
905 && p[0] == SSL3_MT_HELLO_REQUEST) {
907 * The server may always send 'Hello Request' messages -- we are
908 * doing a handshake anyway now, so ignore them if their format is
909 * correct. Does not count for 'Finished' MAC.
911 if (p[1] == 0 && p[2] == 0 && p[3] == 0) {
913 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
914 p, DTLS1_HM_HEADER_LENGTH, ssl,
915 s->msg_callback_arg);
919 } else { /* Incorrectly formatted Hello request */
921 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
926 if (!dtls1_preprocess_fragment(s, &msg_hdr)) {
927 /* SSLfatal() already called */
932 p += DTLS1_HM_HEADER_LENGTH;
934 i = ssl->method->ssl_read_bytes(ssl, SSL3_RT_HANDSHAKE, NULL,
935 &p[frag_off], frag_len, 0, &readbytes);
938 * This shouldn't ever fail due to NBIO because we already checked
939 * that we have enough data in the record
942 s->rwstate = SSL_READING;
951 * XDTLS: an incorrectly formatted fragment should cause the handshake
954 if (readbytes != frag_len) {
955 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_LENGTH);
961 * We got a new ClientHello with a message sequence of 0.
962 * Reset the read/write sequences back to the beginning.
963 * We process it like this is the first time we've seen a ClientHello
966 s->d1->handshake_read_seq = 0;
967 s->d1->next_handshake_write_seq = 0;
971 * Note that s->init_num is *not* used as current offset in
972 * s->init_buf->data, but as a counter summing up fragments' lengths: as
973 * soon as they sum up to handshake packet length, we assume we have got
976 *len = s->init_num = frag_len;
986 * for these 2 messages, we need to
987 * ssl->session->read_sym_enc assign
988 * ssl->session->read_compression assign
989 * ssl->session->read_hash assign
991 CON_FUNC_RETURN dtls_construct_change_cipher_spec(SSL_CONNECTION *s,
994 if (s->version == DTLS1_BAD_VER) {
995 s->d1->next_handshake_write_seq++;
997 if (!WPACKET_put_bytes_u16(pkt, s->d1->handshake_write_seq)) {
998 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
999 return CON_FUNC_ERROR;
1003 return CON_FUNC_SUCCESS;
1006 #ifndef OPENSSL_NO_SCTP
1008 * Wait for a dry event. Should only be called at a point in the handshake
1009 * where we are not expecting any data from the peer except an alert.
1011 WORK_STATE dtls_wait_for_dry(SSL_CONNECTION *s)
1015 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
1017 /* read app data until dry event */
1018 ret = BIO_dgram_sctp_wait_for_dry(SSL_get_wbio(ssl));
1020 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1026 * We're not expecting any more messages from the peer at this point -
1027 * but we could get an alert. If an alert is waiting then we will never
1028 * return successfully. Therefore we attempt to read a message. This
1029 * should never succeed but will process any waiting alerts.
1031 if (dtls_get_reassembled_message(s, &errtype, &len)) {
1032 /* The call succeeded! This should never happen */
1033 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
1037 s->s3.in_read_app_data = 2;
1038 s->rwstate = SSL_READING;
1039 BIO_clear_retry_flags(SSL_get_rbio(ssl));
1040 BIO_set_retry_read(SSL_get_rbio(ssl));
1043 return WORK_FINISHED_CONTINUE;
1047 int dtls1_read_failed(SSL_CONNECTION *s, int code)
1049 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
1052 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1056 if (!dtls1_is_timer_expired(s) || ossl_statem_in_error(s)) {
1058 * not a timeout, none of our business, let higher layers handle
1059 * this. in fact it's probably an error
1063 /* done, no need to send a retransmit */
1064 if (!SSL_in_init(ssl))
1066 BIO_set_flags(SSL_get_rbio(ssl), BIO_FLAGS_READ);
1070 return dtls1_handle_timeout(s);
1073 int dtls1_get_queue_priority(unsigned short seq, int is_ccs)
1076 * The index of the retransmission queue actually is the message sequence
1077 * number, since the queue only contains messages of a single handshake.
1078 * However, the ChangeCipherSpec has no message sequence number and so
1079 * using only the sequence will result in the CCS and Finished having the
1080 * same index. To prevent this, the sequence number is multiplied by 2.
1081 * In case of a CCS 1 is subtracted. This does not only differ CSS and
1082 * Finished, it also maintains the order of the index (important for
1083 * priority queues) and fits in the unsigned short variable.
1085 return seq * 2 - is_ccs;
1088 int dtls1_retransmit_buffered_messages(SSL_CONNECTION *s)
1090 pqueue *sent = s->d1->sent_messages;
1096 iter = pqueue_iterator(sent);
1098 for (item = pqueue_next(&iter); item != NULL; item = pqueue_next(&iter)) {
1099 frag = (hm_fragment *)item->data;
1100 if (dtls1_retransmit_message(s, (unsigned short)
1101 dtls1_get_queue_priority
1102 (frag->msg_header.seq,
1103 frag->msg_header.is_ccs), &found) <= 0)
1110 int dtls1_buffer_message(SSL_CONNECTION *s, int is_ccs)
1114 unsigned char seq64be[8];
1117 * this function is called immediately after a message has been
1120 if (!ossl_assert(s->init_off == 0))
1123 frag = dtls1_hm_fragment_new(s->init_num, 0);
1127 memcpy(frag->fragment, s->init_buf->data, s->init_num);
1130 /* For DTLS1_BAD_VER the header length is non-standard */
1131 if (!ossl_assert(s->d1->w_msg_hdr.msg_len +
1133 DTLS1_BAD_VER) ? 3 : DTLS1_CCS_HEADER_LENGTH)
1134 == (unsigned int)s->init_num)) {
1135 dtls1_hm_fragment_free(frag);
1139 if (!ossl_assert(s->d1->w_msg_hdr.msg_len +
1140 DTLS1_HM_HEADER_LENGTH == (unsigned int)s->init_num)) {
1141 dtls1_hm_fragment_free(frag);
1146 frag->msg_header.msg_len = s->d1->w_msg_hdr.msg_len;
1147 frag->msg_header.seq = s->d1->w_msg_hdr.seq;
1148 frag->msg_header.type = s->d1->w_msg_hdr.type;
1149 frag->msg_header.frag_off = 0;
1150 frag->msg_header.frag_len = s->d1->w_msg_hdr.msg_len;
1151 frag->msg_header.is_ccs = is_ccs;
1153 /* save current state */
1154 frag->msg_header.saved_retransmit_state.wrlmethod = s->rlayer.wrlmethod;
1155 frag->msg_header.saved_retransmit_state.wrl = s->rlayer.wrl;
1158 memset(seq64be, 0, sizeof(seq64be));
1161 char)(dtls1_get_queue_priority(frag->msg_header.seq,
1162 frag->msg_header.is_ccs) >> 8);
1165 char)(dtls1_get_queue_priority(frag->msg_header.seq,
1166 frag->msg_header.is_ccs));
1168 item = pitem_new(seq64be, frag);
1170 dtls1_hm_fragment_free(frag);
1174 pqueue_insert(s->d1->sent_messages, item);
1178 int dtls1_retransmit_message(SSL_CONNECTION *s, unsigned short seq, int *found)
1181 /* XDTLS: for now assuming that read/writes are blocking */
1184 unsigned long header_length;
1185 unsigned char seq64be[8];
1186 struct dtls1_retransmit_state saved_state;
1188 /* XDTLS: the requested message ought to be found, otherwise error */
1189 memset(seq64be, 0, sizeof(seq64be));
1190 seq64be[6] = (unsigned char)(seq >> 8);
1191 seq64be[7] = (unsigned char)seq;
1193 item = pqueue_find(s->d1->sent_messages, seq64be);
1195 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1201 frag = (hm_fragment *)item->data;
1203 if (frag->msg_header.is_ccs)
1204 header_length = DTLS1_CCS_HEADER_LENGTH;
1206 header_length = DTLS1_HM_HEADER_LENGTH;
1208 memcpy(s->init_buf->data, frag->fragment,
1209 frag->msg_header.msg_len + header_length);
1210 s->init_num = frag->msg_header.msg_len + header_length;
1212 dtls1_set_message_header_int(s, frag->msg_header.type,
1213 frag->msg_header.msg_len,
1214 frag->msg_header.seq, 0,
1215 frag->msg_header.frag_len);
1217 /* save current state */
1218 saved_state.wrlmethod = s->rlayer.wrlmethod;
1219 saved_state.wrl = s->rlayer.wrl;
1221 s->d1->retransmitting = 1;
1223 /* restore state in which the message was originally sent */
1224 s->rlayer.wrlmethod = frag->msg_header.saved_retransmit_state.wrlmethod;
1225 s->rlayer.wrl = frag->msg_header.saved_retransmit_state.wrl;
1228 * The old wrl may be still pointing at an old BIO. Update it to what we're
1231 s->rlayer.wrlmethod->set1_bio(s->rlayer.wrl, s->wbio);
1233 ret = dtls1_do_write(s, frag->msg_header.is_ccs ?
1234 SSL3_RT_CHANGE_CIPHER_SPEC : SSL3_RT_HANDSHAKE);
1236 /* restore current state */
1237 s->rlayer.wrlmethod = saved_state.wrlmethod;
1238 s->rlayer.wrl = saved_state.wrl;
1240 s->d1->retransmitting = 0;
1242 (void)BIO_flush(s->wbio);
1246 void dtls1_set_message_header(SSL_CONNECTION *s,
1247 unsigned char mt, size_t len,
1248 size_t frag_off, size_t frag_len)
1250 if (frag_off == 0) {
1251 s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
1252 s->d1->next_handshake_write_seq++;
1255 dtls1_set_message_header_int(s, mt, len, s->d1->handshake_write_seq,
1256 frag_off, frag_len);
1259 /* don't actually do the writing, wait till the MTU has been retrieved */
1261 dtls1_set_message_header_int(SSL_CONNECTION *s, unsigned char mt,
1262 size_t len, unsigned short seq_num,
1263 size_t frag_off, size_t frag_len)
1265 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
1268 msg_hdr->msg_len = len;
1269 msg_hdr->seq = seq_num;
1270 msg_hdr->frag_off = frag_off;
1271 msg_hdr->frag_len = frag_len;
1275 dtls1_fix_message_header(SSL_CONNECTION *s, size_t frag_off, size_t frag_len)
1277 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
1279 msg_hdr->frag_off = frag_off;
1280 msg_hdr->frag_len = frag_len;
1283 static unsigned char *dtls1_write_message_header(SSL_CONNECTION *s,
1286 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
1288 *p++ = msg_hdr->type;
1289 l2n3(msg_hdr->msg_len, p);
1291 s2n(msg_hdr->seq, p);
1292 l2n3(msg_hdr->frag_off, p);
1293 l2n3(msg_hdr->frag_len, p);
1298 void dtls1_get_message_header(const unsigned char *data, struct
1299 hm_header_st *msg_hdr)
1301 memset(msg_hdr, 0, sizeof(*msg_hdr));
1302 msg_hdr->type = *(data++);
1303 n2l3(data, msg_hdr->msg_len);
1305 n2s(data, msg_hdr->seq);
1306 n2l3(data, msg_hdr->frag_off);
1307 n2l3(data, msg_hdr->frag_len);
1310 int dtls1_set_handshake_header(SSL_CONNECTION *s, WPACKET *pkt, int htype)
1312 unsigned char *header;
1314 if (htype == SSL3_MT_CHANGE_CIPHER_SPEC) {
1315 s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
1316 dtls1_set_message_header_int(s, SSL3_MT_CCS, 0,
1317 s->d1->handshake_write_seq, 0, 0);
1318 if (!WPACKET_put_bytes_u8(pkt, SSL3_MT_CCS))
1321 dtls1_set_message_header(s, htype, 0, 0, 0);
1323 * We allocate space at the start for the message header. This gets
1326 if (!WPACKET_allocate_bytes(pkt, DTLS1_HM_HEADER_LENGTH, &header)
1327 || !WPACKET_start_sub_packet(pkt))
1334 int dtls1_close_construct_packet(SSL_CONNECTION *s, WPACKET *pkt, int htype)
1338 if ((htype != SSL3_MT_CHANGE_CIPHER_SPEC && !WPACKET_close(pkt))
1339 || !WPACKET_get_length(pkt, &msglen)
1340 || msglen > INT_MAX)
1343 if (htype != SSL3_MT_CHANGE_CIPHER_SPEC) {
1344 s->d1->w_msg_hdr.msg_len = msglen - DTLS1_HM_HEADER_LENGTH;
1345 s->d1->w_msg_hdr.frag_len = msglen - DTLS1_HM_HEADER_LENGTH;
1347 s->init_num = (int)msglen;
1350 if (htype != DTLS1_MT_HELLO_VERIFY_REQUEST) {
1351 /* Buffer the message to handle re-xmits */
1352 if (!dtls1_buffer_message(s, htype == SSL3_MT_CHANGE_CIPHER_SPEC