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
) {
97 EVP_CIPHER_CTX_free(frag
->msg_header
.
98 saved_retransmit_state
.enc_write_ctx
);
99 EVP_MD_CTX_free(frag
->msg_header
.saved_retransmit_state
.write_hash
);
101 OPENSSL_free(frag
->fragment
);
102 OPENSSL_free(frag
->reassembly
);
107 * send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
108 * SSL3_RT_CHANGE_CIPHER_SPEC)
110 int dtls1_do_write(SSL_CONNECTION
*s
, int type
)
116 size_t len
, frag_off
, mac_size
, blocksize
, used_len
;
117 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
119 if (!dtls1_query_mtu(s
))
122 if (s
->d1
->mtu
< dtls1_min_mtu(s
))
123 /* should have something reasonable now */
126 if (s
->init_off
== 0 && type
== SSL3_RT_HANDSHAKE
) {
127 if (!ossl_assert(s
->init_num
==
128 s
->d1
->w_msg_hdr
.msg_len
+ DTLS1_HM_HEADER_LENGTH
))
134 && (EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(s
->enc_write_ctx
)) &
135 EVP_CIPH_FLAG_AEAD_CIPHER
) != 0)
138 mac_size
= EVP_MD_CTX_get_size(s
->write_hash
);
142 if (s
->enc_write_ctx
&&
143 (EVP_CIPHER_CTX_get_mode(s
->enc_write_ctx
) == EVP_CIPH_CBC_MODE
))
144 blocksize
= 2 * EVP_CIPHER_CTX_get_block_size(s
->enc_write_ctx
);
149 s
->rwstate
= SSL_NOTHING
;
151 /* s->init_num shouldn't ever be < 0...but just in case */
152 while (s
->init_num
> 0) {
153 if (type
== SSL3_RT_HANDSHAKE
&& s
->init_off
!= 0) {
154 /* We must be writing a fragment other than the first one */
157 /* This is the first attempt at writing out this fragment */
159 if (s
->init_off
<= DTLS1_HM_HEADER_LENGTH
) {
161 * Each fragment that was already sent must at least have
162 * contained the message header plus one other byte.
163 * Therefore |init_off| must have progressed by at least
164 * |DTLS1_HM_HEADER_LENGTH + 1| bytes. If not something went
171 * Adjust |init_off| and |init_num| to allow room for a new
172 * message header for this fragment.
174 s
->init_off
-= DTLS1_HM_HEADER_LENGTH
;
175 s
->init_num
+= DTLS1_HM_HEADER_LENGTH
;
178 * We must have been called again after a retry so use the
179 * fragment offset from our last attempt. We do not need
180 * to adjust |init_off| and |init_num| as above, because
181 * that should already have been done before the retry.
183 frag_off
= s
->d1
->w_msg_hdr
.frag_off
;
187 used_len
= BIO_wpending(s
->wbio
) + DTLS1_RT_HEADER_LENGTH
188 + mac_size
+ blocksize
;
189 if (s
->d1
->mtu
> used_len
)
190 curr_mtu
= s
->d1
->mtu
- used_len
;
194 if (curr_mtu
<= DTLS1_HM_HEADER_LENGTH
) {
196 * grr.. we could get an error if MTU picked was wrong
198 ret
= BIO_flush(s
->wbio
);
200 s
->rwstate
= SSL_WRITING
;
203 used_len
= DTLS1_RT_HEADER_LENGTH
+ mac_size
+ blocksize
;
204 if (s
->d1
->mtu
> used_len
+ DTLS1_HM_HEADER_LENGTH
) {
205 curr_mtu
= s
->d1
->mtu
- used_len
;
207 /* Shouldn't happen */
213 * We just checked that s->init_num > 0 so this cast should be safe
215 if (((unsigned int)s
->init_num
) > curr_mtu
)
220 if (len
> ssl_get_max_send_fragment(s
))
221 len
= ssl_get_max_send_fragment(s
);
224 * XDTLS: this function is too long. split out the CCS part
226 if (type
== SSL3_RT_HANDSHAKE
) {
227 if (len
< DTLS1_HM_HEADER_LENGTH
) {
229 * len is so small that we really can't do anything sensible
234 dtls1_fix_message_header(s
, frag_off
, len
- DTLS1_HM_HEADER_LENGTH
);
236 dtls1_write_message_header(s
,
237 (unsigned char *)&s
->init_buf
->
241 ret
= dtls1_write_bytes(s
, type
, &s
->init_buf
->data
[s
->init_off
], len
,
245 * might need to update MTU here, but we don't know which
246 * previous packet caused the failure -- so can't really
247 * retransmit anything. continue as if everything is fine and
248 * wait for an alert to handle the retransmit
250 if (retry
&& BIO_ctrl(SSL_get_wbio(ssl
),
251 BIO_CTRL_DGRAM_MTU_EXCEEDED
, 0, NULL
) > 0) {
252 if (!(SSL_get_options(ssl
) & SSL_OP_NO_QUERY_MTU
)) {
253 if (!dtls1_query_mtu(s
))
255 /* Have one more go */
265 * bad if this assert fails, only part of the handshake message
266 * got sent. but why would this happen?
268 if (!ossl_assert(len
== written
))
271 if (type
== SSL3_RT_HANDSHAKE
&& !s
->d1
->retransmitting
) {
273 * should not be done for 'Hello Request's, but in that case
274 * we'll ignore the result anyway
277 (unsigned char *)&s
->init_buf
->data
[s
->init_off
];
278 const struct hm_header_st
*msg_hdr
= &s
->d1
->w_msg_hdr
;
281 if (frag_off
== 0 && s
->version
!= DTLS1_BAD_VER
) {
283 * reconstruct message header is if it is being sent in
286 *p
++ = msg_hdr
->type
;
287 l2n3(msg_hdr
->msg_len
, p
);
288 s2n(msg_hdr
->seq
, p
);
290 l2n3(msg_hdr
->msg_len
, p
);
291 p
-= DTLS1_HM_HEADER_LENGTH
;
294 p
+= DTLS1_HM_HEADER_LENGTH
;
295 xlen
= written
- DTLS1_HM_HEADER_LENGTH
;
298 if (!ssl3_finish_mac(s
, p
, xlen
))
302 if (written
== s
->init_num
) {
304 s
->msg_callback(1, s
->version
, type
, s
->init_buf
->data
,
305 (size_t)(s
->init_off
+ s
->init_num
), ssl
,
306 s
->msg_callback_arg
);
308 s
->init_off
= 0; /* done writing this message */
313 s
->init_off
+= written
;
314 s
->init_num
-= written
;
315 written
-= DTLS1_HM_HEADER_LENGTH
;
319 * We save the fragment offset for the next fragment so we have it
320 * available in case of an IO retry. We don't know the length of the
321 * next fragment yet so just set that to 0 for now. It will be
322 * updated again later.
324 dtls1_fix_message_header(s
, frag_off
, 0);
330 int dtls_get_message(SSL_CONNECTION
*s
, int *mt
)
332 struct hm_header_st
*msg_hdr
;
338 msg_hdr
= &s
->d1
->r_msg_hdr
;
339 memset(msg_hdr
, 0, sizeof(*msg_hdr
));
342 if (!dtls_get_reassembled_message(s
, &errtype
, &tmplen
)) {
343 if (errtype
== DTLS1_HM_BAD_FRAGMENT
344 || errtype
== DTLS1_HM_FRAGMENT_RETRY
) {
345 /* bad fragment received */
351 *mt
= s
->s3
.tmp
.message_type
;
353 p
= (unsigned char *)s
->init_buf
->data
;
355 if (*mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
356 if (s
->msg_callback
) {
357 s
->msg_callback(0, s
->version
, SSL3_RT_CHANGE_CIPHER_SPEC
,
358 p
, 1, SSL_CONNECTION_GET_SSL(s
),
359 s
->msg_callback_arg
);
362 * This isn't a real handshake message so skip the processing below.
367 msg_len
= msg_hdr
->msg_len
;
369 /* reconstruct message header */
370 *(p
++) = msg_hdr
->type
;
372 s2n(msg_hdr
->seq
, p
);
376 memset(msg_hdr
, 0, sizeof(*msg_hdr
));
378 s
->d1
->handshake_read_seq
++;
380 s
->init_msg
= s
->init_buf
->data
+ DTLS1_HM_HEADER_LENGTH
;
386 * Actually we already have the message body - but this is an opportunity for
387 * DTLS to do any further processing it wants at the same point that TLS would
388 * be asked for the message body.
390 int dtls_get_message_body(SSL_CONNECTION
*s
, size_t *len
)
392 unsigned char *msg
= (unsigned char *)s
->init_buf
->data
;
393 size_t msg_len
= s
->init_num
+ DTLS1_HM_HEADER_LENGTH
;
395 if (s
->s3
.tmp
.message_type
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
396 /* Nothing to be done */
400 * If receiving Finished, record MAC of prior handshake messages for
401 * Finished verification.
403 if (*(s
->init_buf
->data
) == SSL3_MT_FINISHED
&& !ssl3_take_mac(s
)) {
404 /* SSLfatal() already called */
408 if (s
->version
== DTLS1_BAD_VER
) {
409 msg
+= DTLS1_HM_HEADER_LENGTH
;
410 msg_len
-= DTLS1_HM_HEADER_LENGTH
;
413 if (!ssl3_finish_mac(s
, msg
, msg_len
))
417 s
->msg_callback(0, s
->version
, SSL3_RT_HANDSHAKE
,
418 s
->init_buf
->data
, s
->init_num
+ DTLS1_HM_HEADER_LENGTH
,
419 SSL_CONNECTION_GET_SSL(s
), s
->msg_callback_arg
);
427 * dtls1_max_handshake_message_len returns the maximum number of bytes
428 * permitted in a DTLS handshake message for |s|. The minimum is 16KB, but
429 * may be greater if the maximum certificate list size requires it.
431 static size_t dtls1_max_handshake_message_len(const SSL_CONNECTION
*s
)
433 size_t max_len
= DTLS1_HM_HEADER_LENGTH
+ SSL3_RT_MAX_ENCRYPTED_LENGTH
;
434 if (max_len
< s
->max_cert_list
)
435 return s
->max_cert_list
;
439 static int dtls1_preprocess_fragment(SSL_CONNECTION
*s
,
440 struct hm_header_st
*msg_hdr
)
442 size_t frag_off
, frag_len
, msg_len
;
444 msg_len
= msg_hdr
->msg_len
;
445 frag_off
= msg_hdr
->frag_off
;
446 frag_len
= msg_hdr
->frag_len
;
448 /* sanity checking */
449 if ((frag_off
+ frag_len
) > msg_len
450 || msg_len
> dtls1_max_handshake_message_len(s
)) {
451 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_EXCESSIVE_MESSAGE_SIZE
);
455 if (s
->d1
->r_msg_hdr
.frag_off
== 0) { /* first fragment */
457 * msg_len is limited to 2^24, but is effectively checked against
458 * dtls_max_handshake_message_len(s) above
460 if (!BUF_MEM_grow_clean(s
->init_buf
, msg_len
+ DTLS1_HM_HEADER_LENGTH
)) {
461 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_BUF_LIB
);
465 s
->s3
.tmp
.message_size
= msg_len
;
466 s
->d1
->r_msg_hdr
.msg_len
= msg_len
;
467 s
->s3
.tmp
.message_type
= msg_hdr
->type
;
468 s
->d1
->r_msg_hdr
.type
= msg_hdr
->type
;
469 s
->d1
->r_msg_hdr
.seq
= msg_hdr
->seq
;
470 } else if (msg_len
!= s
->d1
->r_msg_hdr
.msg_len
) {
472 * They must be playing with us! BTW, failure to enforce upper limit
473 * would open possibility for buffer overrun.
475 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_EXCESSIVE_MESSAGE_SIZE
);
483 * Returns 1 if there is a buffered fragment available, 0 if not, or -1 on a
486 static int dtls1_retrieve_buffered_fragment(SSL_CONNECTION
*s
, size_t *len
)
489 * (0) check whether the desired fragment is available
491 * (1) copy over the fragment to s->init_buf->data[]
492 * (2) update s->init_num
500 iter
= pqueue_iterator(s
->d1
->buffered_messages
);
502 item
= pqueue_next(&iter
);
506 frag
= (hm_fragment
*)item
->data
;
508 if (frag
->msg_header
.seq
< s
->d1
->handshake_read_seq
) {
510 hm_fragment
*nextfrag
;
513 || frag
->msg_header
.seq
!= 0
514 || s
->d1
->handshake_read_seq
!= 1
515 || s
->statem
.hand_state
!= DTLS_ST_SW_HELLO_VERIFY_REQUEST
) {
517 * This is a stale message that has been buffered so clear it.
518 * It is safe to pop this message from the queue even though
519 * we have an active iterator
521 pqueue_pop(s
->d1
->buffered_messages
);
522 dtls1_hm_fragment_free(frag
);
528 * We have fragments for a ClientHello without a cookie,
529 * even though we have sent a HelloVerifyRequest. It is possible
530 * that the HelloVerifyRequest got lost and this is a
531 * retransmission of the original ClientHello
533 next
= pqueue_next(&iter
);
535 nextfrag
= (hm_fragment
*)next
->data
;
536 if (nextfrag
->msg_header
.seq
== s
->d1
->handshake_read_seq
) {
538 * We have fragments for both a ClientHello without
539 * cookie and one with. Ditch the one without.
541 pqueue_pop(s
->d1
->buffered_messages
);
542 dtls1_hm_fragment_free(frag
);
554 } while (item
== NULL
);
556 /* Don't return if reassembly still in progress */
557 if (frag
->reassembly
!= NULL
)
560 if (s
->d1
->handshake_read_seq
== frag
->msg_header
.seq
|| chretran
) {
561 size_t frag_len
= frag
->msg_header
.frag_len
;
562 pqueue_pop(s
->d1
->buffered_messages
);
564 /* Calls SSLfatal() as required */
565 ret
= dtls1_preprocess_fragment(s
, &frag
->msg_header
);
567 if (ret
&& frag
->msg_header
.frag_len
> 0) {
569 (unsigned char *)s
->init_buf
->data
+ DTLS1_HM_HEADER_LENGTH
;
570 memcpy(&p
[frag
->msg_header
.frag_off
], frag
->fragment
,
571 frag
->msg_header
.frag_len
);
574 dtls1_hm_fragment_free(frag
);
580 * We got a new ClientHello with a message sequence of 0.
581 * Reset the read/write sequences back to the beginning.
582 * We process it like this is the first time we've seen a
583 * ClientHello from the client.
585 s
->d1
->handshake_read_seq
= 0;
586 s
->d1
->next_handshake_write_seq
= 0;
600 static int dtls1_reassemble_fragment(SSL_CONNECTION
*s
,
601 const struct hm_header_st
*msg_hdr
)
603 hm_fragment
*frag
= NULL
;
605 int i
= -1, is_complete
;
606 unsigned char seq64be
[8];
607 size_t frag_len
= msg_hdr
->frag_len
;
609 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
611 if ((msg_hdr
->frag_off
+ frag_len
) > msg_hdr
->msg_len
||
612 msg_hdr
->msg_len
> dtls1_max_handshake_message_len(s
))
616 return DTLS1_HM_FRAGMENT_RETRY
;
619 /* Try to find item in queue */
620 memset(seq64be
, 0, sizeof(seq64be
));
621 seq64be
[6] = (unsigned char)(msg_hdr
->seq
>> 8);
622 seq64be
[7] = (unsigned char)msg_hdr
->seq
;
623 item
= pqueue_find(s
->d1
->buffered_messages
, seq64be
);
626 frag
= dtls1_hm_fragment_new(msg_hdr
->msg_len
, 1);
629 memcpy(&(frag
->msg_header
), msg_hdr
, sizeof(*msg_hdr
));
630 frag
->msg_header
.frag_len
= frag
->msg_header
.msg_len
;
631 frag
->msg_header
.frag_off
= 0;
633 frag
= (hm_fragment
*)item
->data
;
634 if (frag
->msg_header
.msg_len
!= msg_hdr
->msg_len
) {
642 * If message is already reassembled, this must be a retransmit and can
643 * be dropped. In this case item != NULL and so frag does not need to be
646 if (frag
->reassembly
== NULL
) {
647 unsigned char devnull
[256];
650 i
= ssl
->method
->ssl_read_bytes(ssl
, SSL3_RT_HANDSHAKE
, NULL
,
653 sizeof(devnull
) ? sizeof(devnull
) :
654 frag_len
, 0, &readbytes
);
657 frag_len
-= readbytes
;
659 return DTLS1_HM_FRAGMENT_RETRY
;
662 /* read the body of the fragment (header has already been read */
663 i
= ssl
->method
->ssl_read_bytes(ssl
, SSL3_RT_HANDSHAKE
, NULL
,
664 frag
->fragment
+ msg_hdr
->frag_off
,
665 frag_len
, 0, &readbytes
);
666 if (i
<= 0 || readbytes
!= frag_len
)
671 RSMBLY_BITMASK_MARK(frag
->reassembly
, (long)msg_hdr
->frag_off
,
672 (long)(msg_hdr
->frag_off
+ frag_len
));
674 if (!ossl_assert(msg_hdr
->msg_len
> 0))
676 RSMBLY_BITMASK_IS_COMPLETE(frag
->reassembly
, (long)msg_hdr
->msg_len
,
680 OPENSSL_free(frag
->reassembly
);
681 frag
->reassembly
= NULL
;
685 item
= pitem_new(seq64be
, frag
);
691 item
= pqueue_insert(s
->d1
->buffered_messages
, item
);
693 * pqueue_insert fails iff a duplicate item is inserted. However,
694 * |item| cannot be a duplicate. If it were, |pqueue_find|, above,
695 * would have returned it and control would never have reached this
698 if (!ossl_assert(item
!= NULL
))
702 return DTLS1_HM_FRAGMENT_RETRY
;
706 dtls1_hm_fragment_free(frag
);
710 static int dtls1_process_out_of_seq_message(SSL_CONNECTION
*s
,
711 const struct hm_header_st
*msg_hdr
)
714 hm_fragment
*frag
= NULL
;
716 unsigned char seq64be
[8];
717 size_t frag_len
= msg_hdr
->frag_len
;
719 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
721 if ((msg_hdr
->frag_off
+ frag_len
) > msg_hdr
->msg_len
)
724 /* Try to find item in queue, to prevent duplicate entries */
725 memset(seq64be
, 0, sizeof(seq64be
));
726 seq64be
[6] = (unsigned char)(msg_hdr
->seq
>> 8);
727 seq64be
[7] = (unsigned char)msg_hdr
->seq
;
728 item
= pqueue_find(s
->d1
->buffered_messages
, seq64be
);
731 * If we already have an entry and this one is a fragment, don't discard
732 * it and rather try to reassemble it.
734 if (item
!= NULL
&& frag_len
!= msg_hdr
->msg_len
)
738 * Discard the message if sequence number was already there, is too far
739 * in the future, already in the queue or if we received a FINISHED
740 * before the SERVER_HELLO, which then must be a stale retransmit.
742 if (msg_hdr
->seq
<= s
->d1
->handshake_read_seq
||
743 msg_hdr
->seq
> s
->d1
->handshake_read_seq
+ 10 || item
!= NULL
||
744 (s
->d1
->handshake_read_seq
== 0 && msg_hdr
->type
== SSL3_MT_FINISHED
)) {
745 unsigned char devnull
[256];
748 i
= ssl
->method
->ssl_read_bytes(ssl
, SSL3_RT_HANDSHAKE
, NULL
,
751 sizeof(devnull
) ? sizeof(devnull
) :
752 frag_len
, 0, &readbytes
);
755 frag_len
-= readbytes
;
758 if (frag_len
!= msg_hdr
->msg_len
) {
759 return dtls1_reassemble_fragment(s
, msg_hdr
);
762 if (frag_len
> dtls1_max_handshake_message_len(s
))
765 frag
= dtls1_hm_fragment_new(frag_len
, 0);
769 memcpy(&(frag
->msg_header
), msg_hdr
, sizeof(*msg_hdr
));
773 * read the body of the fragment (header has already been read
775 i
= ssl
->method
->ssl_read_bytes(ssl
, SSL3_RT_HANDSHAKE
, NULL
,
776 frag
->fragment
, frag_len
, 0,
778 if (i
<=0 || readbytes
!= frag_len
)
784 item
= pitem_new(seq64be
, frag
);
788 item
= pqueue_insert(s
->d1
->buffered_messages
, item
);
790 * pqueue_insert fails iff a duplicate item is inserted. However,
791 * |item| cannot be a duplicate. If it were, |pqueue_find|, above,
792 * would have returned it. Then, either |frag_len| !=
793 * |msg_hdr->msg_len| in which case |item| is set to NULL and it will
794 * have been processed with |dtls1_reassemble_fragment|, above, or
795 * the record will have been discarded.
797 if (!ossl_assert(item
!= NULL
))
801 return DTLS1_HM_FRAGMENT_RETRY
;
805 dtls1_hm_fragment_free(frag
);
809 static int dtls_get_reassembled_message(SSL_CONNECTION
*s
, int *errtype
,
812 unsigned char wire
[DTLS1_HM_HEADER_LENGTH
];
813 size_t mlen
, frag_off
, frag_len
;
814 int i
, ret
, recvd_type
;
815 struct hm_header_st msg_hdr
;
817 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
823 /* see if we have the required fragment already */
824 ret
= dtls1_retrieve_buffered_fragment(s
, &frag_len
);
826 /* SSLfatal() already called */
830 s
->init_num
= frag_len
;
835 /* read handshake message header */
836 i
= ssl
->method
->ssl_read_bytes(ssl
, SSL3_RT_HANDSHAKE
, &recvd_type
, wire
,
837 DTLS1_HM_HEADER_LENGTH
, 0, &readbytes
);
838 if (i
<= 0) { /* nbio, or an error */
839 s
->rwstate
= SSL_READING
;
843 if (recvd_type
== SSL3_RT_CHANGE_CIPHER_SPEC
) {
844 if (wire
[0] != SSL3_MT_CCS
) {
845 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
846 SSL_R_BAD_CHANGE_CIPHER_SPEC
);
850 memcpy(s
->init_buf
->data
, wire
, readbytes
);
851 s
->init_num
= readbytes
- 1;
852 s
->init_msg
= s
->init_buf
->data
+ 1;
853 s
->s3
.tmp
.message_type
= SSL3_MT_CHANGE_CIPHER_SPEC
;
854 s
->s3
.tmp
.message_size
= readbytes
- 1;
855 *len
= readbytes
- 1;
859 /* Handshake fails if message header is incomplete */
860 if (readbytes
!= DTLS1_HM_HEADER_LENGTH
) {
861 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_UNEXPECTED_MESSAGE
);
865 /* parse the message fragment header */
866 dtls1_get_message_header(wire
, &msg_hdr
);
868 mlen
= msg_hdr
.msg_len
;
869 frag_off
= msg_hdr
.frag_off
;
870 frag_len
= msg_hdr
.frag_len
;
873 * We must have at least frag_len bytes left in the record to be read.
874 * Fragments must not span records.
876 if (frag_len
> s
->rlayer
.tlsrecs
[s
->rlayer
.curr_rec
].length
) {
877 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_BAD_LENGTH
);
882 * if this is a future (or stale) message it gets buffered
883 * (or dropped)--no further processing at this time
884 * While listening, we accept seq 1 (ClientHello with cookie)
885 * although we're still expecting seq 0 (ClientHello)
887 if (msg_hdr
.seq
!= s
->d1
->handshake_read_seq
) {
890 || s
->d1
->handshake_read_seq
!= 1
891 || wire
[0] != SSL3_MT_CLIENT_HELLO
892 || s
->statem
.hand_state
!= DTLS_ST_SW_HELLO_VERIFY_REQUEST
) {
893 *errtype
= dtls1_process_out_of_seq_message(s
, &msg_hdr
);
897 * We received a ClientHello and sent back a HelloVerifyRequest. We
898 * now seem to have received a retransmitted initial ClientHello. That
899 * is allowed (possibly our HelloVerifyRequest got lost).
904 if (frag_len
&& frag_len
< mlen
) {
905 *errtype
= dtls1_reassemble_fragment(s
, &msg_hdr
);
910 && s
->d1
->r_msg_hdr
.frag_off
== 0
911 && s
->statem
.hand_state
!= TLS_ST_OK
912 && wire
[0] == SSL3_MT_HELLO_REQUEST
) {
914 * The server may always send 'Hello Request' messages -- we are
915 * doing a handshake anyway now, so ignore them if their format is
916 * correct. Does not count for 'Finished' MAC.
918 if (wire
[1] == 0 && wire
[2] == 0 && wire
[3] == 0) {
920 s
->msg_callback(0, s
->version
, SSL3_RT_HANDSHAKE
,
921 wire
, DTLS1_HM_HEADER_LENGTH
, ssl
,
922 s
->msg_callback_arg
);
926 } else { /* Incorrectly formatted Hello request */
928 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_UNEXPECTED_MESSAGE
);
933 if (!dtls1_preprocess_fragment(s
, &msg_hdr
)) {
934 /* SSLfatal() already called */
940 (unsigned char *)s
->init_buf
->data
+ DTLS1_HM_HEADER_LENGTH
;
942 i
= ssl
->method
->ssl_read_bytes(ssl
, SSL3_RT_HANDSHAKE
, NULL
,
943 &p
[frag_off
], frag_len
, 0, &readbytes
);
946 * This shouldn't ever fail due to NBIO because we already checked
947 * that we have enough data in the record
950 s
->rwstate
= SSL_READING
;
959 * XDTLS: an incorrectly formatted fragment should cause the handshake
962 if (readbytes
!= frag_len
) {
963 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_BAD_LENGTH
);
969 * We got a new ClientHello with a message sequence of 0.
970 * Reset the read/write sequences back to the beginning.
971 * We process it like this is the first time we've seen a ClientHello
974 s
->d1
->handshake_read_seq
= 0;
975 s
->d1
->next_handshake_write_seq
= 0;
979 * Note that s->init_num is *not* used as current offset in
980 * s->init_buf->data, but as a counter summing up fragments' lengths: as
981 * soon as they sum up to handshake packet length, we assume we have got
984 *len
= s
->init_num
= frag_len
;
994 * for these 2 messages, we need to
995 * ssl->enc_read_ctx re-init
996 * ssl->s3.read_mac_secret re-init
997 * ssl->session->read_sym_enc assign
998 * ssl->session->read_compression assign
999 * ssl->session->read_hash assign
1001 CON_FUNC_RETURN
dtls_construct_change_cipher_spec(SSL_CONNECTION
*s
,
1004 if (s
->version
== DTLS1_BAD_VER
) {
1005 s
->d1
->next_handshake_write_seq
++;
1007 if (!WPACKET_put_bytes_u16(pkt
, s
->d1
->handshake_write_seq
)) {
1008 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1009 return CON_FUNC_ERROR
;
1013 return CON_FUNC_SUCCESS
;
1016 #ifndef OPENSSL_NO_SCTP
1018 * Wait for a dry event. Should only be called at a point in the handshake
1019 * where we are not expecting any data from the peer except an alert.
1021 WORK_STATE
dtls_wait_for_dry(SSL_CONNECTION
*s
)
1025 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
1027 /* read app data until dry event */
1028 ret
= BIO_dgram_sctp_wait_for_dry(SSL_get_wbio(ssl
));
1030 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1036 * We're not expecting any more messages from the peer at this point -
1037 * but we could get an alert. If an alert is waiting then we will never
1038 * return successfully. Therefore we attempt to read a message. This
1039 * should never succeed but will process any waiting alerts.
1041 if (dtls_get_reassembled_message(s
, &errtype
, &len
)) {
1042 /* The call succeeded! This should never happen */
1043 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_UNEXPECTED_MESSAGE
);
1047 s
->s3
.in_read_app_data
= 2;
1048 s
->rwstate
= SSL_READING
;
1049 BIO_clear_retry_flags(SSL_get_rbio(ssl
));
1050 BIO_set_retry_read(SSL_get_rbio(ssl
));
1053 return WORK_FINISHED_CONTINUE
;
1057 int dtls1_read_failed(SSL_CONNECTION
*s
, int code
)
1059 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
1062 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1066 if (!dtls1_is_timer_expired(s
) || ossl_statem_in_error(s
)) {
1068 * not a timeout, none of our business, let higher layers handle
1069 * this. in fact it's probably an error
1073 /* done, no need to send a retransmit */
1074 if (!SSL_in_init(ssl
))
1076 BIO_set_flags(SSL_get_rbio(ssl
), BIO_FLAGS_READ
);
1080 return dtls1_handle_timeout(s
);
1083 int dtls1_get_queue_priority(unsigned short seq
, int is_ccs
)
1086 * The index of the retransmission queue actually is the message sequence
1087 * number, since the queue only contains messages of a single handshake.
1088 * However, the ChangeCipherSpec has no message sequence number and so
1089 * using only the sequence will result in the CCS and Finished having the
1090 * same index. To prevent this, the sequence number is multiplied by 2.
1091 * In case of a CCS 1 is subtracted. This does not only differ CSS and
1092 * Finished, it also maintains the order of the index (important for
1093 * priority queues) and fits in the unsigned short variable.
1095 return seq
* 2 - is_ccs
;
1098 int dtls1_retransmit_buffered_messages(SSL_CONNECTION
*s
)
1100 pqueue
*sent
= s
->d1
->sent_messages
;
1106 iter
= pqueue_iterator(sent
);
1108 for (item
= pqueue_next(&iter
); item
!= NULL
; item
= pqueue_next(&iter
)) {
1109 frag
= (hm_fragment
*)item
->data
;
1110 if (dtls1_retransmit_message(s
, (unsigned short)
1111 dtls1_get_queue_priority
1112 (frag
->msg_header
.seq
,
1113 frag
->msg_header
.is_ccs
), &found
) <= 0)
1120 int dtls1_buffer_message(SSL_CONNECTION
*s
, int is_ccs
)
1124 unsigned char seq64be
[8];
1127 * this function is called immediately after a message has been
1130 if (!ossl_assert(s
->init_off
== 0))
1133 frag
= dtls1_hm_fragment_new(s
->init_num
, 0);
1137 memcpy(frag
->fragment
, s
->init_buf
->data
, s
->init_num
);
1140 /* For DTLS1_BAD_VER the header length is non-standard */
1141 if (!ossl_assert(s
->d1
->w_msg_hdr
.msg_len
+
1143 DTLS1_BAD_VER
) ? 3 : DTLS1_CCS_HEADER_LENGTH
)
1144 == (unsigned int)s
->init_num
)) {
1145 dtls1_hm_fragment_free(frag
);
1149 if (!ossl_assert(s
->d1
->w_msg_hdr
.msg_len
+
1150 DTLS1_HM_HEADER_LENGTH
== (unsigned int)s
->init_num
)) {
1151 dtls1_hm_fragment_free(frag
);
1156 frag
->msg_header
.msg_len
= s
->d1
->w_msg_hdr
.msg_len
;
1157 frag
->msg_header
.seq
= s
->d1
->w_msg_hdr
.seq
;
1158 frag
->msg_header
.type
= s
->d1
->w_msg_hdr
.type
;
1159 frag
->msg_header
.frag_off
= 0;
1160 frag
->msg_header
.frag_len
= s
->d1
->w_msg_hdr
.msg_len
;
1161 frag
->msg_header
.is_ccs
= is_ccs
;
1163 /* save current state */
1164 frag
->msg_header
.saved_retransmit_state
.enc_write_ctx
= s
->enc_write_ctx
;
1165 frag
->msg_header
.saved_retransmit_state
.write_hash
= s
->write_hash
;
1166 frag
->msg_header
.saved_retransmit_state
.compress
= s
->compress
;
1167 frag
->msg_header
.saved_retransmit_state
.session
= s
->session
;
1168 frag
->msg_header
.saved_retransmit_state
.epoch
=
1169 DTLS_RECORD_LAYER_get_w_epoch(&s
->rlayer
);
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
.enc_write_ctx
= s
->enc_write_ctx
;
1232 saved_state
.write_hash
= s
->write_hash
;
1233 saved_state
.compress
= s
->compress
;
1234 saved_state
.session
= s
->session
;
1235 saved_state
.epoch
= DTLS_RECORD_LAYER_get_w_epoch(&s
->rlayer
);
1237 s
->d1
->retransmitting
= 1;
1239 /* restore state in which the message was originally sent */
1240 s
->enc_write_ctx
= frag
->msg_header
.saved_retransmit_state
.enc_write_ctx
;
1241 s
->write_hash
= frag
->msg_header
.saved_retransmit_state
.write_hash
;
1242 s
->compress
= frag
->msg_header
.saved_retransmit_state
.compress
;
1243 s
->session
= frag
->msg_header
.saved_retransmit_state
.session
;
1244 DTLS_RECORD_LAYER_set_saved_w_epoch(&s
->rlayer
,
1246 saved_retransmit_state
.epoch
);
1248 ret
= dtls1_do_write(s
, frag
->msg_header
.is_ccs
?
1249 SSL3_RT_CHANGE_CIPHER_SPEC
: SSL3_RT_HANDSHAKE
);
1251 /* restore current state */
1252 s
->enc_write_ctx
= saved_state
.enc_write_ctx
;
1253 s
->write_hash
= saved_state
.write_hash
;
1254 s
->compress
= saved_state
.compress
;
1255 s
->session
= saved_state
.session
;
1256 DTLS_RECORD_LAYER_set_saved_w_epoch(&s
->rlayer
, saved_state
.epoch
);
1258 s
->d1
->retransmitting
= 0;
1260 (void)BIO_flush(s
->wbio
);
1264 void dtls1_set_message_header(SSL_CONNECTION
*s
,
1265 unsigned char mt
, size_t len
,
1266 size_t frag_off
, size_t frag_len
)
1268 if (frag_off
== 0) {
1269 s
->d1
->handshake_write_seq
= s
->d1
->next_handshake_write_seq
;
1270 s
->d1
->next_handshake_write_seq
++;
1273 dtls1_set_message_header_int(s
, mt
, len
, s
->d1
->handshake_write_seq
,
1274 frag_off
, frag_len
);
1277 /* don't actually do the writing, wait till the MTU has been retrieved */
1279 dtls1_set_message_header_int(SSL_CONNECTION
*s
, unsigned char mt
,
1280 size_t len
, unsigned short seq_num
,
1281 size_t frag_off
, size_t frag_len
)
1283 struct hm_header_st
*msg_hdr
= &s
->d1
->w_msg_hdr
;
1286 msg_hdr
->msg_len
= len
;
1287 msg_hdr
->seq
= seq_num
;
1288 msg_hdr
->frag_off
= frag_off
;
1289 msg_hdr
->frag_len
= frag_len
;
1293 dtls1_fix_message_header(SSL_CONNECTION
*s
, size_t frag_off
, size_t frag_len
)
1295 struct hm_header_st
*msg_hdr
= &s
->d1
->w_msg_hdr
;
1297 msg_hdr
->frag_off
= frag_off
;
1298 msg_hdr
->frag_len
= frag_len
;
1301 static unsigned char *dtls1_write_message_header(SSL_CONNECTION
*s
,
1304 struct hm_header_st
*msg_hdr
= &s
->d1
->w_msg_hdr
;
1306 *p
++ = msg_hdr
->type
;
1307 l2n3(msg_hdr
->msg_len
, p
);
1309 s2n(msg_hdr
->seq
, p
);
1310 l2n3(msg_hdr
->frag_off
, p
);
1311 l2n3(msg_hdr
->frag_len
, p
);
1316 void dtls1_get_message_header(unsigned char *data
, struct hm_header_st
*msg_hdr
)
1318 memset(msg_hdr
, 0, sizeof(*msg_hdr
));
1319 msg_hdr
->type
= *(data
++);
1320 n2l3(data
, msg_hdr
->msg_len
);
1322 n2s(data
, msg_hdr
->seq
);
1323 n2l3(data
, msg_hdr
->frag_off
);
1324 n2l3(data
, msg_hdr
->frag_len
);
1327 int dtls1_set_handshake_header(SSL_CONNECTION
*s
, WPACKET
*pkt
, int htype
)
1329 unsigned char *header
;
1331 if (htype
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
1332 s
->d1
->handshake_write_seq
= s
->d1
->next_handshake_write_seq
;
1333 dtls1_set_message_header_int(s
, SSL3_MT_CCS
, 0,
1334 s
->d1
->handshake_write_seq
, 0, 0);
1335 if (!WPACKET_put_bytes_u8(pkt
, SSL3_MT_CCS
))
1338 dtls1_set_message_header(s
, htype
, 0, 0, 0);
1340 * We allocate space at the start for the message header. This gets
1343 if (!WPACKET_allocate_bytes(pkt
, DTLS1_HM_HEADER_LENGTH
, &header
)
1344 || !WPACKET_start_sub_packet(pkt
))
1351 int dtls1_close_construct_packet(SSL_CONNECTION
*s
, WPACKET
*pkt
, int htype
)
1355 if ((htype
!= SSL3_MT_CHANGE_CIPHER_SPEC
&& !WPACKET_close(pkt
))
1356 || !WPACKET_get_length(pkt
, &msglen
)
1357 || msglen
> INT_MAX
)
1360 if (htype
!= SSL3_MT_CHANGE_CIPHER_SPEC
) {
1361 s
->d1
->w_msg_hdr
.msg_len
= msglen
- DTLS1_HM_HEADER_LENGTH
;
1362 s
->d1
->w_msg_hdr
.frag_len
= msglen
- DTLS1_HM_HEADER_LENGTH
;
1364 s
->init_num
= (int)msglen
;
1367 if (htype
!= DTLS1_MT_HELLO_VERIFY_REQUEST
) {
1368 /* Buffer the message to handle re-xmits */
1369 if (!dtls1_buffer_message(s
, htype
== SSL3_MT_CHANGE_CIPHER_SPEC