2 * Copyright 2005-2022 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
13 #include "../ssl_local.h"
14 #include "statem_local.h"
15 #include "internal/cryptlib.h"
16 #include <openssl/buffer.h>
17 #include <openssl/objects.h>
18 #include <openssl/evp.h>
19 #include <openssl/x509.h>
21 #define RSMBLY_BITMASK_SIZE(msg_len) (((msg_len) + 7) / 8)
23 #define RSMBLY_BITMASK_MARK(bitmask, start, end) { \
24 if ((end) - (start) <= 8) { \
26 for (ii = (start); ii < (end); ii++) bitmask[((ii) >> 3)] |= (1 << ((ii) & 7)); \
29 bitmask[((start) >> 3)] |= bitmask_start_values[((start) & 7)]; \
30 for (ii = (((start) >> 3) + 1); ii < ((((end) - 1)) >> 3); ii++) bitmask[ii] = 0xff; \
31 bitmask[(((end) - 1) >> 3)] |= bitmask_end_values[((end) & 7)]; \
34 #define RSMBLY_BITMASK_IS_COMPLETE(bitmask, msg_len, is_complete) { \
37 if (bitmask[(((msg_len) - 1) >> 3)] != bitmask_end_values[((msg_len) & 7)]) is_complete = 0; \
38 if (is_complete) for (ii = (((msg_len) - 1) >> 3) - 1; ii >= 0 ; ii--) \
39 if (bitmask[ii] != 0xff) { is_complete = 0; break; } }
41 static unsigned char bitmask_start_values
[] =
42 { 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80 };
43 static unsigned char bitmask_end_values
[] =
44 { 0xff, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f };
46 static void dtls1_fix_message_header(SSL_CONNECTION
*s
, size_t frag_off
,
48 static unsigned char *dtls1_write_message_header(SSL_CONNECTION
*s
,
50 static void dtls1_set_message_header_int(SSL_CONNECTION
*s
, unsigned char mt
,
52 unsigned short seq_num
,
55 static int dtls_get_reassembled_message(SSL_CONNECTION
*s
, int *errtype
,
58 static hm_fragment
*dtls1_hm_fragment_new(size_t frag_len
, int reassembly
)
60 hm_fragment
*frag
= NULL
;
61 unsigned char *buf
= NULL
;
62 unsigned char *bitmask
= NULL
;
64 if ((frag
= OPENSSL_malloc(sizeof(*frag
))) == NULL
)
68 if ((buf
= OPENSSL_malloc(frag_len
)) == NULL
) {
74 /* zero length fragment gets zero frag->fragment */
77 /* Initialize reassembly bitmask if necessary */
79 bitmask
= OPENSSL_zalloc(RSMBLY_BITMASK_SIZE(frag_len
));
80 if (bitmask
== NULL
) {
87 frag
->reassembly
= bitmask
;
92 void dtls1_hm_fragment_free(hm_fragment
*frag
)
96 if (frag
->msg_header
.is_ccs
) {
98 * If we're freeing the CCS then we're done with the old wrl and it
101 if (frag
->msg_header
.saved_retransmit_state
.wrlmethod
!= NULL
)
102 frag
->msg_header
.saved_retransmit_state
.wrlmethod
->free(frag
->msg_header
.saved_retransmit_state
.wrl
);
104 OPENSSL_free(frag
->fragment
);
105 OPENSSL_free(frag
->reassembly
);
110 * send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
111 * SSL3_RT_CHANGE_CIPHER_SPEC)
113 int dtls1_do_write(SSL_CONNECTION
*s
, int type
)
119 size_t len
, frag_off
, mac_size
, blocksize
, used_len
;
120 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
122 if (!dtls1_query_mtu(s
))
125 if (s
->d1
->mtu
< dtls1_min_mtu(s
))
126 /* should have something reasonable now */
129 if (s
->init_off
== 0 && type
== SSL3_RT_HANDSHAKE
) {
130 if (!ossl_assert(s
->init_num
==
131 s
->d1
->w_msg_hdr
.msg_len
+ DTLS1_HM_HEADER_LENGTH
))
137 && (EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(s
->enc_write_ctx
)) &
138 EVP_CIPH_FLAG_AEAD_CIPHER
) != 0)
141 mac_size
= EVP_MD_CTX_get_size(s
->write_hash
);
145 if (s
->enc_write_ctx
&&
146 (EVP_CIPHER_CTX_get_mode(s
->enc_write_ctx
) == EVP_CIPH_CBC_MODE
))
147 blocksize
= 2 * EVP_CIPHER_CTX_get_block_size(s
->enc_write_ctx
);
152 s
->rwstate
= SSL_NOTHING
;
154 /* s->init_num shouldn't ever be < 0...but just in case */
155 while (s
->init_num
> 0) {
156 if (type
== SSL3_RT_HANDSHAKE
&& s
->init_off
!= 0) {
157 /* We must be writing a fragment other than the first one */
160 /* This is the first attempt at writing out this fragment */
162 if (s
->init_off
<= DTLS1_HM_HEADER_LENGTH
) {
164 * Each fragment that was already sent must at least have
165 * contained the message header plus one other byte.
166 * Therefore |init_off| must have progressed by at least
167 * |DTLS1_HM_HEADER_LENGTH + 1| bytes. If not something went
174 * Adjust |init_off| and |init_num| to allow room for a new
175 * message header for this fragment.
177 s
->init_off
-= DTLS1_HM_HEADER_LENGTH
;
178 s
->init_num
+= DTLS1_HM_HEADER_LENGTH
;
181 * We must have been called again after a retry so use the
182 * fragment offset from our last attempt. We do not need
183 * to adjust |init_off| and |init_num| as above, because
184 * that should already have been done before the retry.
186 frag_off
= s
->d1
->w_msg_hdr
.frag_off
;
190 used_len
= BIO_wpending(s
->wbio
) + DTLS1_RT_HEADER_LENGTH
191 + mac_size
+ blocksize
;
192 if (s
->d1
->mtu
> used_len
)
193 curr_mtu
= s
->d1
->mtu
- used_len
;
197 if (curr_mtu
<= DTLS1_HM_HEADER_LENGTH
) {
199 * grr.. we could get an error if MTU picked was wrong
201 ret
= BIO_flush(s
->wbio
);
203 s
->rwstate
= SSL_WRITING
;
206 used_len
= DTLS1_RT_HEADER_LENGTH
+ mac_size
+ blocksize
;
207 if (s
->d1
->mtu
> used_len
+ DTLS1_HM_HEADER_LENGTH
) {
208 curr_mtu
= s
->d1
->mtu
- used_len
;
210 /* Shouldn't happen */
216 * We just checked that s->init_num > 0 so this cast should be safe
218 if (((unsigned int)s
->init_num
) > curr_mtu
)
223 if (len
> ssl_get_max_send_fragment(s
))
224 len
= ssl_get_max_send_fragment(s
);
227 * XDTLS: this function is too long. split out the CCS part
229 if (type
== SSL3_RT_HANDSHAKE
) {
230 if (len
< DTLS1_HM_HEADER_LENGTH
) {
232 * len is so small that we really can't do anything sensible
237 dtls1_fix_message_header(s
, frag_off
, len
- DTLS1_HM_HEADER_LENGTH
);
239 dtls1_write_message_header(s
,
240 (unsigned char *)&s
->init_buf
->
244 ret
= dtls1_write_bytes(s
, type
, &s
->init_buf
->data
[s
->init_off
], len
,
248 * might need to update MTU here, but we don't know which
249 * previous packet caused the failure -- so can't really
250 * retransmit anything. continue as if everything is fine and
251 * wait for an alert to handle the retransmit
253 if (retry
&& BIO_ctrl(SSL_get_wbio(ssl
),
254 BIO_CTRL_DGRAM_MTU_EXCEEDED
, 0, NULL
) > 0) {
255 if (!(SSL_get_options(ssl
) & SSL_OP_NO_QUERY_MTU
)) {
256 if (!dtls1_query_mtu(s
))
258 /* Have one more go */
268 * bad if this assert fails, only part of the handshake message
269 * got sent. but why would this happen?
271 if (!ossl_assert(len
== written
))
274 if (type
== SSL3_RT_HANDSHAKE
&& !s
->d1
->retransmitting
) {
276 * should not be done for 'Hello Request's, but in that case
277 * we'll ignore the result anyway
280 (unsigned char *)&s
->init_buf
->data
[s
->init_off
];
281 const struct hm_header_st
*msg_hdr
= &s
->d1
->w_msg_hdr
;
284 if (frag_off
== 0 && s
->version
!= DTLS1_BAD_VER
) {
286 * reconstruct message header is if it is being sent in
289 *p
++ = msg_hdr
->type
;
290 l2n3(msg_hdr
->msg_len
, p
);
291 s2n(msg_hdr
->seq
, p
);
293 l2n3(msg_hdr
->msg_len
, p
);
294 p
-= DTLS1_HM_HEADER_LENGTH
;
297 p
+= DTLS1_HM_HEADER_LENGTH
;
298 xlen
= written
- DTLS1_HM_HEADER_LENGTH
;
301 if (!ssl3_finish_mac(s
, p
, xlen
))
305 if (written
== s
->init_num
) {
307 s
->msg_callback(1, s
->version
, type
, s
->init_buf
->data
,
308 (size_t)(s
->init_off
+ s
->init_num
), ssl
,
309 s
->msg_callback_arg
);
311 s
->init_off
= 0; /* done writing this message */
316 s
->init_off
+= written
;
317 s
->init_num
-= written
;
318 written
-= DTLS1_HM_HEADER_LENGTH
;
322 * We save the fragment offset for the next fragment so we have it
323 * available in case of an IO retry. We don't know the length of the
324 * next fragment yet so just set that to 0 for now. It will be
325 * updated again later.
327 dtls1_fix_message_header(s
, frag_off
, 0);
333 int dtls_get_message(SSL_CONNECTION
*s
, int *mt
)
335 struct hm_header_st
*msg_hdr
;
341 msg_hdr
= &s
->d1
->r_msg_hdr
;
342 memset(msg_hdr
, 0, sizeof(*msg_hdr
));
345 if (!dtls_get_reassembled_message(s
, &errtype
, &tmplen
)) {
346 if (errtype
== DTLS1_HM_BAD_FRAGMENT
347 || errtype
== DTLS1_HM_FRAGMENT_RETRY
) {
348 /* bad fragment received */
354 *mt
= s
->s3
.tmp
.message_type
;
356 p
= (unsigned char *)s
->init_buf
->data
;
358 if (*mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
359 if (s
->msg_callback
) {
360 s
->msg_callback(0, s
->version
, SSL3_RT_CHANGE_CIPHER_SPEC
,
361 p
, 1, SSL_CONNECTION_GET_SSL(s
),
362 s
->msg_callback_arg
);
365 * This isn't a real handshake message so skip the processing below.
370 msg_len
= msg_hdr
->msg_len
;
372 /* reconstruct message header */
373 *(p
++) = msg_hdr
->type
;
375 s2n(msg_hdr
->seq
, p
);
379 memset(msg_hdr
, 0, sizeof(*msg_hdr
));
381 s
->d1
->handshake_read_seq
++;
383 s
->init_msg
= s
->init_buf
->data
+ DTLS1_HM_HEADER_LENGTH
;
389 * Actually we already have the message body - but this is an opportunity for
390 * DTLS to do any further processing it wants at the same point that TLS would
391 * be asked for the message body.
393 int dtls_get_message_body(SSL_CONNECTION
*s
, size_t *len
)
395 unsigned char *msg
= (unsigned char *)s
->init_buf
->data
;
396 size_t msg_len
= s
->init_num
+ DTLS1_HM_HEADER_LENGTH
;
398 if (s
->s3
.tmp
.message_type
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
399 /* Nothing to be done */
403 * If receiving Finished, record MAC of prior handshake messages for
404 * Finished verification.
406 if (*(s
->init_buf
->data
) == SSL3_MT_FINISHED
&& !ssl3_take_mac(s
)) {
407 /* SSLfatal() already called */
411 if (s
->version
== DTLS1_BAD_VER
) {
412 msg
+= DTLS1_HM_HEADER_LENGTH
;
413 msg_len
-= DTLS1_HM_HEADER_LENGTH
;
416 if (!ssl3_finish_mac(s
, msg
, msg_len
))
420 s
->msg_callback(0, s
->version
, SSL3_RT_HANDSHAKE
,
421 s
->init_buf
->data
, s
->init_num
+ DTLS1_HM_HEADER_LENGTH
,
422 SSL_CONNECTION_GET_SSL(s
), s
->msg_callback_arg
);
430 * dtls1_max_handshake_message_len returns the maximum number of bytes
431 * permitted in a DTLS handshake message for |s|. The minimum is 16KB, but
432 * may be greater if the maximum certificate list size requires it.
434 static size_t dtls1_max_handshake_message_len(const SSL_CONNECTION
*s
)
436 size_t max_len
= DTLS1_HM_HEADER_LENGTH
+ SSL3_RT_MAX_ENCRYPTED_LENGTH
;
437 if (max_len
< s
->max_cert_list
)
438 return s
->max_cert_list
;
442 static int dtls1_preprocess_fragment(SSL_CONNECTION
*s
,
443 struct hm_header_st
*msg_hdr
)
445 size_t frag_off
, frag_len
, msg_len
;
447 msg_len
= msg_hdr
->msg_len
;
448 frag_off
= msg_hdr
->frag_off
;
449 frag_len
= msg_hdr
->frag_len
;
451 /* sanity checking */
452 if ((frag_off
+ frag_len
) > msg_len
453 || msg_len
> dtls1_max_handshake_message_len(s
)) {
454 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_EXCESSIVE_MESSAGE_SIZE
);
458 if (s
->d1
->r_msg_hdr
.frag_off
== 0) { /* first fragment */
460 * msg_len is limited to 2^24, but is effectively checked against
461 * dtls_max_handshake_message_len(s) above
463 if (!BUF_MEM_grow_clean(s
->init_buf
, msg_len
+ DTLS1_HM_HEADER_LENGTH
)) {
464 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_BUF_LIB
);
468 s
->s3
.tmp
.message_size
= msg_len
;
469 s
->d1
->r_msg_hdr
.msg_len
= msg_len
;
470 s
->s3
.tmp
.message_type
= msg_hdr
->type
;
471 s
->d1
->r_msg_hdr
.type
= msg_hdr
->type
;
472 s
->d1
->r_msg_hdr
.seq
= msg_hdr
->seq
;
473 } else if (msg_len
!= s
->d1
->r_msg_hdr
.msg_len
) {
475 * They must be playing with us! BTW, failure to enforce upper limit
476 * would open possibility for buffer overrun.
478 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_EXCESSIVE_MESSAGE_SIZE
);
486 * Returns 1 if there is a buffered fragment available, 0 if not, or -1 on a
489 static int dtls1_retrieve_buffered_fragment(SSL_CONNECTION
*s
, size_t *len
)
492 * (0) check whether the desired fragment is available
494 * (1) copy over the fragment to s->init_buf->data[]
495 * (2) update s->init_num
503 iter
= pqueue_iterator(s
->d1
->buffered_messages
);
505 item
= pqueue_next(&iter
);
509 frag
= (hm_fragment
*)item
->data
;
511 if (frag
->msg_header
.seq
< s
->d1
->handshake_read_seq
) {
513 hm_fragment
*nextfrag
;
516 || frag
->msg_header
.seq
!= 0
517 || s
->d1
->handshake_read_seq
!= 1
518 || s
->statem
.hand_state
!= DTLS_ST_SW_HELLO_VERIFY_REQUEST
) {
520 * This is a stale message that has been buffered so clear it.
521 * It is safe to pop this message from the queue even though
522 * we have an active iterator
524 pqueue_pop(s
->d1
->buffered_messages
);
525 dtls1_hm_fragment_free(frag
);
531 * We have fragments for a ClientHello without a cookie,
532 * even though we have sent a HelloVerifyRequest. It is possible
533 * that the HelloVerifyRequest got lost and this is a
534 * retransmission of the original ClientHello
536 next
= pqueue_next(&iter
);
538 nextfrag
= (hm_fragment
*)next
->data
;
539 if (nextfrag
->msg_header
.seq
== s
->d1
->handshake_read_seq
) {
541 * We have fragments for both a ClientHello without
542 * cookie and one with. Ditch the one without.
544 pqueue_pop(s
->d1
->buffered_messages
);
545 dtls1_hm_fragment_free(frag
);
557 } while (item
== NULL
);
559 /* Don't return if reassembly still in progress */
560 if (frag
->reassembly
!= NULL
)
563 if (s
->d1
->handshake_read_seq
== frag
->msg_header
.seq
|| chretran
) {
564 size_t frag_len
= frag
->msg_header
.frag_len
;
565 pqueue_pop(s
->d1
->buffered_messages
);
567 /* Calls SSLfatal() as required */
568 ret
= dtls1_preprocess_fragment(s
, &frag
->msg_header
);
570 if (ret
&& frag
->msg_header
.frag_len
> 0) {
572 (unsigned char *)s
->init_buf
->data
+ DTLS1_HM_HEADER_LENGTH
;
573 memcpy(&p
[frag
->msg_header
.frag_off
], frag
->fragment
,
574 frag
->msg_header
.frag_len
);
577 dtls1_hm_fragment_free(frag
);
583 * We got a new ClientHello with a message sequence of 0.
584 * Reset the read/write sequences back to the beginning.
585 * We process it like this is the first time we've seen a
586 * ClientHello from the client.
588 s
->d1
->handshake_read_seq
= 0;
589 s
->d1
->next_handshake_write_seq
= 0;
603 static int dtls1_reassemble_fragment(SSL_CONNECTION
*s
,
604 const struct hm_header_st
*msg_hdr
)
606 hm_fragment
*frag
= NULL
;
608 int i
= -1, is_complete
;
609 unsigned char seq64be
[8];
610 size_t frag_len
= msg_hdr
->frag_len
;
612 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
614 if ((msg_hdr
->frag_off
+ frag_len
) > msg_hdr
->msg_len
||
615 msg_hdr
->msg_len
> dtls1_max_handshake_message_len(s
))
619 return DTLS1_HM_FRAGMENT_RETRY
;
622 /* Try to find item in queue */
623 memset(seq64be
, 0, sizeof(seq64be
));
624 seq64be
[6] = (unsigned char)(msg_hdr
->seq
>> 8);
625 seq64be
[7] = (unsigned char)msg_hdr
->seq
;
626 item
= pqueue_find(s
->d1
->buffered_messages
, seq64be
);
629 frag
= dtls1_hm_fragment_new(msg_hdr
->msg_len
, 1);
632 memcpy(&(frag
->msg_header
), msg_hdr
, sizeof(*msg_hdr
));
633 frag
->msg_header
.frag_len
= frag
->msg_header
.msg_len
;
634 frag
->msg_header
.frag_off
= 0;
636 frag
= (hm_fragment
*)item
->data
;
637 if (frag
->msg_header
.msg_len
!= msg_hdr
->msg_len
) {
645 * If message is already reassembled, this must be a retransmit and can
646 * be dropped. In this case item != NULL and so frag does not need to be
649 if (frag
->reassembly
== NULL
) {
650 unsigned char devnull
[256];
653 i
= ssl
->method
->ssl_read_bytes(ssl
, SSL3_RT_HANDSHAKE
, NULL
,
656 sizeof(devnull
) ? sizeof(devnull
) :
657 frag_len
, 0, &readbytes
);
660 frag_len
-= readbytes
;
662 return DTLS1_HM_FRAGMENT_RETRY
;
665 /* read the body of the fragment (header has already been read */
666 i
= ssl
->method
->ssl_read_bytes(ssl
, SSL3_RT_HANDSHAKE
, NULL
,
667 frag
->fragment
+ msg_hdr
->frag_off
,
668 frag_len
, 0, &readbytes
);
669 if (i
<= 0 || readbytes
!= frag_len
)
674 RSMBLY_BITMASK_MARK(frag
->reassembly
, (long)msg_hdr
->frag_off
,
675 (long)(msg_hdr
->frag_off
+ frag_len
));
677 if (!ossl_assert(msg_hdr
->msg_len
> 0))
679 RSMBLY_BITMASK_IS_COMPLETE(frag
->reassembly
, (long)msg_hdr
->msg_len
,
683 OPENSSL_free(frag
->reassembly
);
684 frag
->reassembly
= NULL
;
688 item
= pitem_new(seq64be
, frag
);
694 item
= pqueue_insert(s
->d1
->buffered_messages
, item
);
696 * pqueue_insert fails iff a duplicate item is inserted. However,
697 * |item| cannot be a duplicate. If it were, |pqueue_find|, above,
698 * would have returned it and control would never have reached this
701 if (!ossl_assert(item
!= NULL
))
705 return DTLS1_HM_FRAGMENT_RETRY
;
709 dtls1_hm_fragment_free(frag
);
713 static int dtls1_process_out_of_seq_message(SSL_CONNECTION
*s
,
714 const struct hm_header_st
*msg_hdr
)
717 hm_fragment
*frag
= NULL
;
719 unsigned char seq64be
[8];
720 size_t frag_len
= msg_hdr
->frag_len
;
722 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
724 if ((msg_hdr
->frag_off
+ frag_len
) > msg_hdr
->msg_len
)
727 /* Try to find item in queue, to prevent duplicate entries */
728 memset(seq64be
, 0, sizeof(seq64be
));
729 seq64be
[6] = (unsigned char)(msg_hdr
->seq
>> 8);
730 seq64be
[7] = (unsigned char)msg_hdr
->seq
;
731 item
= pqueue_find(s
->d1
->buffered_messages
, seq64be
);
734 * If we already have an entry and this one is a fragment, don't discard
735 * it and rather try to reassemble it.
737 if (item
!= NULL
&& frag_len
!= msg_hdr
->msg_len
)
741 * Discard the message if sequence number was already there, is too far
742 * in the future, already in the queue or if we received a FINISHED
743 * before the SERVER_HELLO, which then must be a stale retransmit.
745 if (msg_hdr
->seq
<= s
->d1
->handshake_read_seq
||
746 msg_hdr
->seq
> s
->d1
->handshake_read_seq
+ 10 || item
!= NULL
||
747 (s
->d1
->handshake_read_seq
== 0 && msg_hdr
->type
== SSL3_MT_FINISHED
)) {
748 unsigned char devnull
[256];
751 i
= ssl
->method
->ssl_read_bytes(ssl
, SSL3_RT_HANDSHAKE
, NULL
,
754 sizeof(devnull
) ? sizeof(devnull
) :
755 frag_len
, 0, &readbytes
);
758 frag_len
-= readbytes
;
761 if (frag_len
!= msg_hdr
->msg_len
) {
762 return dtls1_reassemble_fragment(s
, msg_hdr
);
765 if (frag_len
> dtls1_max_handshake_message_len(s
))
768 frag
= dtls1_hm_fragment_new(frag_len
, 0);
772 memcpy(&(frag
->msg_header
), msg_hdr
, sizeof(*msg_hdr
));
776 * read the body of the fragment (header has already been read
778 i
= ssl
->method
->ssl_read_bytes(ssl
, SSL3_RT_HANDSHAKE
, NULL
,
779 frag
->fragment
, frag_len
, 0,
781 if (i
<=0 || readbytes
!= frag_len
)
787 item
= pitem_new(seq64be
, frag
);
791 item
= pqueue_insert(s
->d1
->buffered_messages
, item
);
793 * pqueue_insert fails iff a duplicate item is inserted. However,
794 * |item| cannot be a duplicate. If it were, |pqueue_find|, above,
795 * would have returned it. Then, either |frag_len| !=
796 * |msg_hdr->msg_len| in which case |item| is set to NULL and it will
797 * have been processed with |dtls1_reassemble_fragment|, above, or
798 * the record will have been discarded.
800 if (!ossl_assert(item
!= NULL
))
804 return DTLS1_HM_FRAGMENT_RETRY
;
808 dtls1_hm_fragment_free(frag
);
812 static int dtls_get_reassembled_message(SSL_CONNECTION
*s
, int *errtype
,
815 unsigned char wire
[DTLS1_HM_HEADER_LENGTH
];
816 size_t mlen
, frag_off
, frag_len
;
817 int i
, ret
, recvd_type
;
818 struct hm_header_st msg_hdr
;
820 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
826 /* see if we have the required fragment already */
827 ret
= dtls1_retrieve_buffered_fragment(s
, &frag_len
);
829 /* SSLfatal() already called */
833 s
->init_num
= frag_len
;
838 /* read handshake message header */
839 i
= ssl
->method
->ssl_read_bytes(ssl
, SSL3_RT_HANDSHAKE
, &recvd_type
, wire
,
840 DTLS1_HM_HEADER_LENGTH
, 0, &readbytes
);
841 if (i
<= 0) { /* nbio, or an error */
842 s
->rwstate
= SSL_READING
;
846 if (recvd_type
== SSL3_RT_CHANGE_CIPHER_SPEC
) {
847 if (wire
[0] != SSL3_MT_CCS
) {
848 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
849 SSL_R_BAD_CHANGE_CIPHER_SPEC
);
853 memcpy(s
->init_buf
->data
, wire
, readbytes
);
854 s
->init_num
= readbytes
- 1;
855 s
->init_msg
= s
->init_buf
->data
+ 1;
856 s
->s3
.tmp
.message_type
= SSL3_MT_CHANGE_CIPHER_SPEC
;
857 s
->s3
.tmp
.message_size
= readbytes
- 1;
858 *len
= readbytes
- 1;
862 /* Handshake fails if message header is incomplete */
863 if (readbytes
!= DTLS1_HM_HEADER_LENGTH
) {
864 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_UNEXPECTED_MESSAGE
);
868 /* parse the message fragment header */
869 dtls1_get_message_header(wire
, &msg_hdr
);
871 mlen
= msg_hdr
.msg_len
;
872 frag_off
= msg_hdr
.frag_off
;
873 frag_len
= msg_hdr
.frag_len
;
876 * We must have at least frag_len bytes left in the record to be read.
877 * Fragments must not span records.
879 if (frag_len
> s
->rlayer
.tlsrecs
[s
->rlayer
.curr_rec
].length
) {
880 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_BAD_LENGTH
);
885 * if this is a future (or stale) message it gets buffered
886 * (or dropped)--no further processing at this time
887 * While listening, we accept seq 1 (ClientHello with cookie)
888 * although we're still expecting seq 0 (ClientHello)
890 if (msg_hdr
.seq
!= s
->d1
->handshake_read_seq
) {
893 || s
->d1
->handshake_read_seq
!= 1
894 || wire
[0] != SSL3_MT_CLIENT_HELLO
895 || s
->statem
.hand_state
!= DTLS_ST_SW_HELLO_VERIFY_REQUEST
) {
896 *errtype
= dtls1_process_out_of_seq_message(s
, &msg_hdr
);
900 * We received a ClientHello and sent back a HelloVerifyRequest. We
901 * now seem to have received a retransmitted initial ClientHello. That
902 * is allowed (possibly our HelloVerifyRequest got lost).
907 if (frag_len
&& frag_len
< mlen
) {
908 *errtype
= dtls1_reassemble_fragment(s
, &msg_hdr
);
913 && s
->d1
->r_msg_hdr
.frag_off
== 0
914 && s
->statem
.hand_state
!= TLS_ST_OK
915 && wire
[0] == SSL3_MT_HELLO_REQUEST
) {
917 * The server may always send 'Hello Request' messages -- we are
918 * doing a handshake anyway now, so ignore them if their format is
919 * correct. Does not count for 'Finished' MAC.
921 if (wire
[1] == 0 && wire
[2] == 0 && wire
[3] == 0) {
923 s
->msg_callback(0, s
->version
, SSL3_RT_HANDSHAKE
,
924 wire
, DTLS1_HM_HEADER_LENGTH
, ssl
,
925 s
->msg_callback_arg
);
929 } else { /* Incorrectly formatted Hello request */
931 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_UNEXPECTED_MESSAGE
);
936 if (!dtls1_preprocess_fragment(s
, &msg_hdr
)) {
937 /* SSLfatal() already called */
943 (unsigned char *)s
->init_buf
->data
+ DTLS1_HM_HEADER_LENGTH
;
945 i
= ssl
->method
->ssl_read_bytes(ssl
, SSL3_RT_HANDSHAKE
, NULL
,
946 &p
[frag_off
], frag_len
, 0, &readbytes
);
949 * This shouldn't ever fail due to NBIO because we already checked
950 * that we have enough data in the record
953 s
->rwstate
= SSL_READING
;
962 * XDTLS: an incorrectly formatted fragment should cause the handshake
965 if (readbytes
!= frag_len
) {
966 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_BAD_LENGTH
);
972 * We got a new ClientHello with a message sequence of 0.
973 * Reset the read/write sequences back to the beginning.
974 * We process it like this is the first time we've seen a ClientHello
977 s
->d1
->handshake_read_seq
= 0;
978 s
->d1
->next_handshake_write_seq
= 0;
982 * Note that s->init_num is *not* used as current offset in
983 * s->init_buf->data, but as a counter summing up fragments' lengths: as
984 * soon as they sum up to handshake packet length, we assume we have got
987 *len
= s
->init_num
= frag_len
;
997 * for these 2 messages, we need to
998 * ssl->enc_read_ctx re-init
999 * ssl->s3.read_mac_secret re-init
1000 * ssl->session->read_sym_enc assign
1001 * ssl->session->read_compression assign
1002 * ssl->session->read_hash assign
1004 CON_FUNC_RETURN
dtls_construct_change_cipher_spec(SSL_CONNECTION
*s
,
1007 if (s
->version
== DTLS1_BAD_VER
) {
1008 s
->d1
->next_handshake_write_seq
++;
1010 if (!WPACKET_put_bytes_u16(pkt
, s
->d1
->handshake_write_seq
)) {
1011 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1012 return CON_FUNC_ERROR
;
1016 return CON_FUNC_SUCCESS
;
1019 #ifndef OPENSSL_NO_SCTP
1021 * Wait for a dry event. Should only be called at a point in the handshake
1022 * where we are not expecting any data from the peer except an alert.
1024 WORK_STATE
dtls_wait_for_dry(SSL_CONNECTION
*s
)
1028 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
1030 /* read app data until dry event */
1031 ret
= BIO_dgram_sctp_wait_for_dry(SSL_get_wbio(ssl
));
1033 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1039 * We're not expecting any more messages from the peer at this point -
1040 * but we could get an alert. If an alert is waiting then we will never
1041 * return successfully. Therefore we attempt to read a message. This
1042 * should never succeed but will process any waiting alerts.
1044 if (dtls_get_reassembled_message(s
, &errtype
, &len
)) {
1045 /* The call succeeded! This should never happen */
1046 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_UNEXPECTED_MESSAGE
);
1050 s
->s3
.in_read_app_data
= 2;
1051 s
->rwstate
= SSL_READING
;
1052 BIO_clear_retry_flags(SSL_get_rbio(ssl
));
1053 BIO_set_retry_read(SSL_get_rbio(ssl
));
1056 return WORK_FINISHED_CONTINUE
;
1060 int dtls1_read_failed(SSL_CONNECTION
*s
, int code
)
1062 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
1065 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1069 if (!dtls1_is_timer_expired(s
) || ossl_statem_in_error(s
)) {
1071 * not a timeout, none of our business, let higher layers handle
1072 * this. in fact it's probably an error
1076 /* done, no need to send a retransmit */
1077 if (!SSL_in_init(ssl
))
1079 BIO_set_flags(SSL_get_rbio(ssl
), BIO_FLAGS_READ
);
1083 return dtls1_handle_timeout(s
);
1086 int dtls1_get_queue_priority(unsigned short seq
, int is_ccs
)
1089 * The index of the retransmission queue actually is the message sequence
1090 * number, since the queue only contains messages of a single handshake.
1091 * However, the ChangeCipherSpec has no message sequence number and so
1092 * using only the sequence will result in the CCS and Finished having the
1093 * same index. To prevent this, the sequence number is multiplied by 2.
1094 * In case of a CCS 1 is subtracted. This does not only differ CSS and
1095 * Finished, it also maintains the order of the index (important for
1096 * priority queues) and fits in the unsigned short variable.
1098 return seq
* 2 - is_ccs
;
1101 int dtls1_retransmit_buffered_messages(SSL_CONNECTION
*s
)
1103 pqueue
*sent
= s
->d1
->sent_messages
;
1109 iter
= pqueue_iterator(sent
);
1111 for (item
= pqueue_next(&iter
); item
!= NULL
; item
= pqueue_next(&iter
)) {
1112 frag
= (hm_fragment
*)item
->data
;
1113 if (dtls1_retransmit_message(s
, (unsigned short)
1114 dtls1_get_queue_priority
1115 (frag
->msg_header
.seq
,
1116 frag
->msg_header
.is_ccs
), &found
) <= 0)
1123 int dtls1_buffer_message(SSL_CONNECTION
*s
, int is_ccs
)
1127 unsigned char seq64be
[8];
1130 * this function is called immediately after a message has been
1133 if (!ossl_assert(s
->init_off
== 0))
1136 frag
= dtls1_hm_fragment_new(s
->init_num
, 0);
1140 memcpy(frag
->fragment
, s
->init_buf
->data
, s
->init_num
);
1143 /* For DTLS1_BAD_VER the header length is non-standard */
1144 if (!ossl_assert(s
->d1
->w_msg_hdr
.msg_len
+
1146 DTLS1_BAD_VER
) ? 3 : DTLS1_CCS_HEADER_LENGTH
)
1147 == (unsigned int)s
->init_num
)) {
1148 dtls1_hm_fragment_free(frag
);
1152 if (!ossl_assert(s
->d1
->w_msg_hdr
.msg_len
+
1153 DTLS1_HM_HEADER_LENGTH
== (unsigned int)s
->init_num
)) {
1154 dtls1_hm_fragment_free(frag
);
1159 frag
->msg_header
.msg_len
= s
->d1
->w_msg_hdr
.msg_len
;
1160 frag
->msg_header
.seq
= s
->d1
->w_msg_hdr
.seq
;
1161 frag
->msg_header
.type
= s
->d1
->w_msg_hdr
.type
;
1162 frag
->msg_header
.frag_off
= 0;
1163 frag
->msg_header
.frag_len
= s
->d1
->w_msg_hdr
.msg_len
;
1164 frag
->msg_header
.is_ccs
= is_ccs
;
1166 /* save current state */
1167 frag
->msg_header
.saved_retransmit_state
.wrlmethod
= s
->rlayer
.wrlmethod
;
1168 frag
->msg_header
.saved_retransmit_state
.wrl
= s
->rlayer
.wrl
;
1171 memset(seq64be
, 0, sizeof(seq64be
));
1174 char)(dtls1_get_queue_priority(frag
->msg_header
.seq
,
1175 frag
->msg_header
.is_ccs
) >> 8);
1178 char)(dtls1_get_queue_priority(frag
->msg_header
.seq
,
1179 frag
->msg_header
.is_ccs
));
1181 item
= pitem_new(seq64be
, frag
);
1183 dtls1_hm_fragment_free(frag
);
1187 pqueue_insert(s
->d1
->sent_messages
, item
);
1191 int dtls1_retransmit_message(SSL_CONNECTION
*s
, unsigned short seq
, int *found
)
1194 /* XDTLS: for now assuming that read/writes are blocking */
1197 unsigned long header_length
;
1198 unsigned char seq64be
[8];
1199 struct dtls1_retransmit_state saved_state
;
1201 /* XDTLS: the requested message ought to be found, otherwise error */
1202 memset(seq64be
, 0, sizeof(seq64be
));
1203 seq64be
[6] = (unsigned char)(seq
>> 8);
1204 seq64be
[7] = (unsigned char)seq
;
1206 item
= pqueue_find(s
->d1
->sent_messages
, seq64be
);
1208 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1214 frag
= (hm_fragment
*)item
->data
;
1216 if (frag
->msg_header
.is_ccs
)
1217 header_length
= DTLS1_CCS_HEADER_LENGTH
;
1219 header_length
= DTLS1_HM_HEADER_LENGTH
;
1221 memcpy(s
->init_buf
->data
, frag
->fragment
,
1222 frag
->msg_header
.msg_len
+ header_length
);
1223 s
->init_num
= frag
->msg_header
.msg_len
+ header_length
;
1225 dtls1_set_message_header_int(s
, frag
->msg_header
.type
,
1226 frag
->msg_header
.msg_len
,
1227 frag
->msg_header
.seq
, 0,
1228 frag
->msg_header
.frag_len
);
1230 /* save current state */
1231 saved_state
.wrlmethod
= s
->rlayer
.wrlmethod
;
1232 saved_state
.wrl
= s
->rlayer
.wrl
;
1234 s
->d1
->retransmitting
= 1;
1236 /* restore state in which the message was originally sent */
1237 s
->rlayer
.wrlmethod
= frag
->msg_header
.saved_retransmit_state
.wrlmethod
;
1238 s
->rlayer
.wrl
= frag
->msg_header
.saved_retransmit_state
.wrl
;
1241 * The old wrl may be still pointing at an old BIO. Update it to what we're
1244 s
->rlayer
.wrlmethod
->set1_bio(s
->rlayer
.wrl
, s
->wbio
);
1246 ret
= dtls1_do_write(s
, frag
->msg_header
.is_ccs
?
1247 SSL3_RT_CHANGE_CIPHER_SPEC
: SSL3_RT_HANDSHAKE
);
1249 /* restore current state */
1250 s
->rlayer
.wrlmethod
= saved_state
.wrlmethod
;
1251 s
->rlayer
.wrl
= saved_state
.wrl
;
1253 s
->d1
->retransmitting
= 0;
1255 (void)BIO_flush(s
->wbio
);
1259 void dtls1_set_message_header(SSL_CONNECTION
*s
,
1260 unsigned char mt
, size_t len
,
1261 size_t frag_off
, size_t frag_len
)
1263 if (frag_off
== 0) {
1264 s
->d1
->handshake_write_seq
= s
->d1
->next_handshake_write_seq
;
1265 s
->d1
->next_handshake_write_seq
++;
1268 dtls1_set_message_header_int(s
, mt
, len
, s
->d1
->handshake_write_seq
,
1269 frag_off
, frag_len
);
1272 /* don't actually do the writing, wait till the MTU has been retrieved */
1274 dtls1_set_message_header_int(SSL_CONNECTION
*s
, unsigned char mt
,
1275 size_t len
, unsigned short seq_num
,
1276 size_t frag_off
, size_t frag_len
)
1278 struct hm_header_st
*msg_hdr
= &s
->d1
->w_msg_hdr
;
1281 msg_hdr
->msg_len
= len
;
1282 msg_hdr
->seq
= seq_num
;
1283 msg_hdr
->frag_off
= frag_off
;
1284 msg_hdr
->frag_len
= frag_len
;
1288 dtls1_fix_message_header(SSL_CONNECTION
*s
, size_t frag_off
, size_t frag_len
)
1290 struct hm_header_st
*msg_hdr
= &s
->d1
->w_msg_hdr
;
1292 msg_hdr
->frag_off
= frag_off
;
1293 msg_hdr
->frag_len
= frag_len
;
1296 static unsigned char *dtls1_write_message_header(SSL_CONNECTION
*s
,
1299 struct hm_header_st
*msg_hdr
= &s
->d1
->w_msg_hdr
;
1301 *p
++ = msg_hdr
->type
;
1302 l2n3(msg_hdr
->msg_len
, p
);
1304 s2n(msg_hdr
->seq
, p
);
1305 l2n3(msg_hdr
->frag_off
, p
);
1306 l2n3(msg_hdr
->frag_len
, p
);
1311 void dtls1_get_message_header(unsigned char *data
, struct hm_header_st
*msg_hdr
)
1313 memset(msg_hdr
, 0, sizeof(*msg_hdr
));
1314 msg_hdr
->type
= *(data
++);
1315 n2l3(data
, msg_hdr
->msg_len
);
1317 n2s(data
, msg_hdr
->seq
);
1318 n2l3(data
, msg_hdr
->frag_off
);
1319 n2l3(data
, msg_hdr
->frag_len
);
1322 int dtls1_set_handshake_header(SSL_CONNECTION
*s
, WPACKET
*pkt
, int htype
)
1324 unsigned char *header
;
1326 if (htype
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
1327 s
->d1
->handshake_write_seq
= s
->d1
->next_handshake_write_seq
;
1328 dtls1_set_message_header_int(s
, SSL3_MT_CCS
, 0,
1329 s
->d1
->handshake_write_seq
, 0, 0);
1330 if (!WPACKET_put_bytes_u8(pkt
, SSL3_MT_CCS
))
1333 dtls1_set_message_header(s
, htype
, 0, 0, 0);
1335 * We allocate space at the start for the message header. This gets
1338 if (!WPACKET_allocate_bytes(pkt
, DTLS1_HM_HEADER_LENGTH
, &header
)
1339 || !WPACKET_start_sub_packet(pkt
))
1346 int dtls1_close_construct_packet(SSL_CONNECTION
*s
, WPACKET
*pkt
, int htype
)
1350 if ((htype
!= SSL3_MT_CHANGE_CIPHER_SPEC
&& !WPACKET_close(pkt
))
1351 || !WPACKET_get_length(pkt
, &msglen
)
1352 || msglen
> INT_MAX
)
1355 if (htype
!= SSL3_MT_CHANGE_CIPHER_SPEC
) {
1356 s
->d1
->w_msg_hdr
.msg_len
= msglen
- DTLS1_HM_HEADER_LENGTH
;
1357 s
->d1
->w_msg_hdr
.frag_len
= msglen
- DTLS1_HM_HEADER_LENGTH
;
1359 s
->init_num
= (int)msglen
;
1362 if (htype
!= DTLS1_MT_HELLO_VERIFY_REQUEST
) {
1363 /* Buffer the message to handle re-xmits */
1364 if (!dtls1_buffer_message(s
, htype
== SSL3_MT_CHANGE_CIPHER_SPEC