2 * Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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_locl.h"
14 #include "statem_locl.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
*s
, size_t frag_off
,
48 static unsigned char *dtls1_write_message_header(SSL
*s
, unsigned char *p
);
49 static void dtls1_set_message_header_int(SSL
*s
, unsigned char mt
,
51 unsigned short seq_num
,
54 static int dtls_get_reassembled_message(SSL
*s
, int *errtype
, size_t *len
);
56 static hm_fragment
*dtls1_hm_fragment_new(size_t frag_len
, int reassembly
)
58 hm_fragment
*frag
= NULL
;
59 unsigned char *buf
= NULL
;
60 unsigned char *bitmask
= NULL
;
62 frag
= OPENSSL_malloc(sizeof(*frag
));
67 buf
= OPENSSL_malloc(frag_len
);
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
*s
, int type
)
116 size_t len
, frag_off
, mac_size
, blocksize
, used_len
;
118 if (!dtls1_query_mtu(s
))
121 if (s
->d1
->mtu
< dtls1_min_mtu(s
))
122 /* should have something reasonable now */
125 if (s
->init_off
== 0 && type
== SSL3_RT_HANDSHAKE
) {
126 if (!ossl_assert(s
->init_num
==
127 s
->d1
->w_msg_hdr
.msg_len
+ DTLS1_HM_HEADER_LENGTH
))
133 && (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s
->enc_write_ctx
)) &
134 EVP_CIPH_FLAG_AEAD_CIPHER
) != 0)
137 mac_size
= EVP_MD_CTX_size(s
->write_hash
);
141 if (s
->enc_write_ctx
&&
142 (EVP_CIPHER_CTX_mode(s
->enc_write_ctx
) == EVP_CIPH_CBC_MODE
))
143 blocksize
= 2 * EVP_CIPHER_CTX_block_size(s
->enc_write_ctx
);
148 s
->rwstate
= SSL_NOTHING
;
150 /* s->init_num shouldn't ever be < 0...but just in case */
151 while (s
->init_num
> 0) {
152 if (type
== SSL3_RT_HANDSHAKE
&& s
->init_off
!= 0) {
153 /* We must be writing a fragment other than the first one */
156 /* This is the first attempt at writing out this fragment */
158 if (s
->init_off
<= DTLS1_HM_HEADER_LENGTH
) {
160 * Each fragment that was already sent must at least have
161 * contained the message header plus one other byte.
162 * Therefore |init_off| must have progressed by at least
163 * |DTLS1_HM_HEADER_LENGTH + 1| bytes. If not something went
170 * Adjust |init_off| and |init_num| to allow room for a new
171 * message header for this fragment.
173 s
->init_off
-= DTLS1_HM_HEADER_LENGTH
;
174 s
->init_num
+= DTLS1_HM_HEADER_LENGTH
;
177 * We must have been called again after a retry so use the
178 * fragment offset from our last attempt. We do not need
179 * to adjust |init_off| and |init_num| as above, because
180 * that should already have been done before the retry.
182 frag_off
= s
->d1
->w_msg_hdr
.frag_off
;
186 used_len
= BIO_wpending(s
->wbio
) + DTLS1_RT_HEADER_LENGTH
187 + mac_size
+ blocksize
;
188 if (s
->d1
->mtu
> used_len
)
189 curr_mtu
= s
->d1
->mtu
- used_len
;
193 if (curr_mtu
<= DTLS1_HM_HEADER_LENGTH
) {
195 * grr.. we could get an error if MTU picked was wrong
197 ret
= BIO_flush(s
->wbio
);
199 s
->rwstate
= SSL_WRITING
;
202 used_len
= DTLS1_RT_HEADER_LENGTH
+ mac_size
+ blocksize
;
203 if (s
->d1
->mtu
> used_len
+ DTLS1_HM_HEADER_LENGTH
) {
204 curr_mtu
= s
->d1
->mtu
- used_len
;
206 /* Shouldn't happen */
212 * We just checked that s->init_num > 0 so this cast should be safe
214 if (((unsigned int)s
->init_num
) > curr_mtu
)
219 if (len
> s
->max_send_fragment
)
220 len
= s
->max_send_fragment
;
223 * XDTLS: this function is too long. split out the CCS part
225 if (type
== SSL3_RT_HANDSHAKE
) {
226 if (len
< DTLS1_HM_HEADER_LENGTH
) {
228 * len is so small that we really can't do anything sensible
233 dtls1_fix_message_header(s
, frag_off
, len
- DTLS1_HM_HEADER_LENGTH
);
235 dtls1_write_message_header(s
,
236 (unsigned char *)&s
->init_buf
->
240 ret
= dtls1_write_bytes(s
, type
, &s
->init_buf
->data
[s
->init_off
], len
,
244 * might need to update MTU here, but we don't know which
245 * previous packet caused the failure -- so can't really
246 * retransmit anything. continue as if everything is fine and
247 * wait for an alert to handle the retransmit
249 if (retry
&& BIO_ctrl(SSL_get_wbio(s
),
250 BIO_CTRL_DGRAM_MTU_EXCEEDED
, 0, NULL
) > 0) {
251 if (!(SSL_get_options(s
) & SSL_OP_NO_QUERY_MTU
)) {
252 if (!dtls1_query_mtu(s
))
254 /* Have one more go */
264 * bad if this assert fails, only part of the handshake message
265 * got sent. but why would this happen?
267 if (!ossl_assert(len
== written
))
270 if (type
== SSL3_RT_HANDSHAKE
&& !s
->d1
->retransmitting
) {
272 * should not be done for 'Hello Request's, but in that case
273 * we'll ignore the result anyway
276 (unsigned char *)&s
->init_buf
->data
[s
->init_off
];
277 const struct hm_header_st
*msg_hdr
= &s
->d1
->w_msg_hdr
;
280 if (frag_off
== 0 && s
->version
!= DTLS1_BAD_VER
) {
282 * reconstruct message header is if it is being sent in
285 *p
++ = msg_hdr
->type
;
286 l2n3(msg_hdr
->msg_len
, p
);
287 s2n(msg_hdr
->seq
, p
);
289 l2n3(msg_hdr
->msg_len
, p
);
290 p
-= DTLS1_HM_HEADER_LENGTH
;
293 p
+= DTLS1_HM_HEADER_LENGTH
;
294 xlen
= written
- DTLS1_HM_HEADER_LENGTH
;
297 if (!ssl3_finish_mac(s
, p
, xlen
))
301 if (written
== s
->init_num
) {
303 s
->msg_callback(1, s
->version
, type
, s
->init_buf
->data
,
304 (size_t)(s
->init_off
+ s
->init_num
), s
,
305 s
->msg_callback_arg
);
307 s
->init_off
= 0; /* done writing this message */
312 s
->init_off
+= written
;
313 s
->init_num
-= written
;
314 written
-= DTLS1_HM_HEADER_LENGTH
;
318 * We save the fragment offset for the next fragment so we have it
319 * available in case of an IO retry. We don't know the length of the
320 * next fragment yet so just set that to 0 for now. It will be
321 * updated again later.
323 dtls1_fix_message_header(s
, frag_off
, 0);
329 int dtls_get_message(SSL
*s
, int *mt
, size_t *len
)
331 struct hm_header_st
*msg_hdr
;
337 msg_hdr
= &s
->d1
->r_msg_hdr
;
338 memset(msg_hdr
, 0, sizeof(*msg_hdr
));
341 if (!dtls_get_reassembled_message(s
, &errtype
, &tmplen
)) {
342 if (errtype
== DTLS1_HM_BAD_FRAGMENT
343 || errtype
== DTLS1_HM_FRAGMENT_RETRY
) {
344 /* bad fragment received */
350 *mt
= s
->s3
->tmp
.message_type
;
352 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, s
, s
->msg_callback_arg
);
361 * This isn't a real handshake message so skip the processing below.
366 msg_len
= msg_hdr
->msg_len
;
368 /* reconstruct message header */
369 *(p
++) = msg_hdr
->type
;
371 s2n(msg_hdr
->seq
, p
);
374 if (s
->version
!= DTLS1_BAD_VER
) {
375 p
-= DTLS1_HM_HEADER_LENGTH
;
376 msg_len
+= DTLS1_HM_HEADER_LENGTH
;
379 if (!ssl3_finish_mac(s
, p
, msg_len
))
382 s
->msg_callback(0, s
->version
, SSL3_RT_HANDSHAKE
,
383 p
, msg_len
, s
, s
->msg_callback_arg
);
385 memset(msg_hdr
, 0, sizeof(*msg_hdr
));
387 s
->d1
->handshake_read_seq
++;
389 s
->init_msg
= s
->init_buf
->data
+ DTLS1_HM_HEADER_LENGTH
;
395 * dtls1_max_handshake_message_len returns the maximum number of bytes
396 * permitted in a DTLS handshake message for |s|. The minimum is 16KB, but
397 * may be greater if the maximum certificate list size requires it.
399 static size_t dtls1_max_handshake_message_len(const SSL
*s
)
401 size_t max_len
= DTLS1_HM_HEADER_LENGTH
+ SSL3_RT_MAX_ENCRYPTED_LENGTH
;
402 if (max_len
< s
->max_cert_list
)
403 return s
->max_cert_list
;
407 static int dtls1_preprocess_fragment(SSL
*s
, struct hm_header_st
*msg_hdr
)
409 size_t frag_off
, frag_len
, msg_len
;
411 msg_len
= msg_hdr
->msg_len
;
412 frag_off
= msg_hdr
->frag_off
;
413 frag_len
= msg_hdr
->frag_len
;
415 /* sanity checking */
416 if ((frag_off
+ frag_len
) > msg_len
417 || msg_len
> dtls1_max_handshake_message_len(s
)) {
418 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_F_DTLS1_PREPROCESS_FRAGMENT
,
419 SSL_R_EXCESSIVE_MESSAGE_SIZE
);
423 if (s
->d1
->r_msg_hdr
.frag_off
== 0) { /* first fragment */
425 * msg_len is limited to 2^24, but is effectively checked against
426 * dtls_max_handshake_message_len(s) above
428 if (!BUF_MEM_grow_clean(s
->init_buf
, msg_len
+ DTLS1_HM_HEADER_LENGTH
)) {
429 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_DTLS1_PREPROCESS_FRAGMENT
,
434 s
->s3
->tmp
.message_size
= msg_len
;
435 s
->d1
->r_msg_hdr
.msg_len
= msg_len
;
436 s
->s3
->tmp
.message_type
= msg_hdr
->type
;
437 s
->d1
->r_msg_hdr
.type
= msg_hdr
->type
;
438 s
->d1
->r_msg_hdr
.seq
= msg_hdr
->seq
;
439 } else if (msg_len
!= s
->d1
->r_msg_hdr
.msg_len
) {
441 * They must be playing with us! BTW, failure to enforce upper limit
442 * would open possibility for buffer overrun.
444 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_F_DTLS1_PREPROCESS_FRAGMENT
,
445 SSL_R_EXCESSIVE_MESSAGE_SIZE
);
453 * Returns 1 if there is a buffered fragment available, 0 if not, or -1 on a
456 static int dtls1_retrieve_buffered_fragment(SSL
*s
, size_t *len
)
459 * (0) check whether the desired fragment is available
461 * (1) copy over the fragment to s->init_buf->data[]
462 * (2) update s->init_num
469 item
= pqueue_peek(s
->d1
->buffered_messages
);
473 frag
= (hm_fragment
*)item
->data
;
475 if (frag
->msg_header
.seq
< s
->d1
->handshake_read_seq
) {
476 /* This is a stale message that has been buffered so clear it */
477 pqueue_pop(s
->d1
->buffered_messages
);
478 dtls1_hm_fragment_free(frag
);
483 } while (item
== NULL
);
485 /* Don't return if reassembly still in progress */
486 if (frag
->reassembly
!= NULL
)
489 if (s
->d1
->handshake_read_seq
== frag
->msg_header
.seq
) {
490 size_t frag_len
= frag
->msg_header
.frag_len
;
491 pqueue_pop(s
->d1
->buffered_messages
);
493 /* Calls SSLfatal() as required */
494 ret
= dtls1_preprocess_fragment(s
, &frag
->msg_header
);
498 (unsigned char *)s
->init_buf
->data
+ DTLS1_HM_HEADER_LENGTH
;
499 memcpy(&p
[frag
->msg_header
.frag_off
], frag
->fragment
,
500 frag
->msg_header
.frag_len
);
503 dtls1_hm_fragment_free(frag
);
520 dtls1_reassemble_fragment(SSL
*s
, const struct hm_header_st
*msg_hdr
)
522 hm_fragment
*frag
= NULL
;
524 int i
= -1, is_complete
;
525 unsigned char seq64be
[8];
526 size_t frag_len
= msg_hdr
->frag_len
;
529 if ((msg_hdr
->frag_off
+ frag_len
) > msg_hdr
->msg_len
||
530 msg_hdr
->msg_len
> dtls1_max_handshake_message_len(s
))
534 return DTLS1_HM_FRAGMENT_RETRY
;
537 /* Try to find item in queue */
538 memset(seq64be
, 0, sizeof(seq64be
));
539 seq64be
[6] = (unsigned char)(msg_hdr
->seq
>> 8);
540 seq64be
[7] = (unsigned char)msg_hdr
->seq
;
541 item
= pqueue_find(s
->d1
->buffered_messages
, seq64be
);
544 frag
= dtls1_hm_fragment_new(msg_hdr
->msg_len
, 1);
547 memcpy(&(frag
->msg_header
), msg_hdr
, sizeof(*msg_hdr
));
548 frag
->msg_header
.frag_len
= frag
->msg_header
.msg_len
;
549 frag
->msg_header
.frag_off
= 0;
551 frag
= (hm_fragment
*)item
->data
;
552 if (frag
->msg_header
.msg_len
!= msg_hdr
->msg_len
) {
560 * If message is already reassembled, this must be a retransmit and can
561 * be dropped. In this case item != NULL and so frag does not need to be
564 if (frag
->reassembly
== NULL
) {
565 unsigned char devnull
[256];
568 i
= s
->method
->ssl_read_bytes(s
, SSL3_RT_HANDSHAKE
, NULL
,
571 sizeof(devnull
) ? sizeof(devnull
) :
572 frag_len
, 0, &readbytes
);
575 frag_len
-= readbytes
;
577 return DTLS1_HM_FRAGMENT_RETRY
;
580 /* read the body of the fragment (header has already been read */
581 i
= s
->method
->ssl_read_bytes(s
, SSL3_RT_HANDSHAKE
, NULL
,
582 frag
->fragment
+ msg_hdr
->frag_off
,
583 frag_len
, 0, &readbytes
);
584 if (i
<= 0 || readbytes
!= frag_len
)
589 RSMBLY_BITMASK_MARK(frag
->reassembly
, (long)msg_hdr
->frag_off
,
590 (long)(msg_hdr
->frag_off
+ frag_len
));
592 if (!ossl_assert(msg_hdr
->msg_len
> 0))
594 RSMBLY_BITMASK_IS_COMPLETE(frag
->reassembly
, (long)msg_hdr
->msg_len
,
598 OPENSSL_free(frag
->reassembly
);
599 frag
->reassembly
= NULL
;
603 item
= pitem_new(seq64be
, frag
);
609 item
= pqueue_insert(s
->d1
->buffered_messages
, item
);
611 * pqueue_insert fails iff a duplicate item is inserted. However,
612 * |item| cannot be a duplicate. If it were, |pqueue_find|, above,
613 * would have returned it and control would never have reached this
616 if (!ossl_assert(item
!= NULL
))
620 return DTLS1_HM_FRAGMENT_RETRY
;
624 dtls1_hm_fragment_free(frag
);
629 dtls1_process_out_of_seq_message(SSL
*s
, const struct hm_header_st
*msg_hdr
)
632 hm_fragment
*frag
= NULL
;
634 unsigned char seq64be
[8];
635 size_t frag_len
= msg_hdr
->frag_len
;
638 if ((msg_hdr
->frag_off
+ frag_len
) > msg_hdr
->msg_len
)
641 /* Try to find item in queue, to prevent duplicate entries */
642 memset(seq64be
, 0, sizeof(seq64be
));
643 seq64be
[6] = (unsigned char)(msg_hdr
->seq
>> 8);
644 seq64be
[7] = (unsigned char)msg_hdr
->seq
;
645 item
= pqueue_find(s
->d1
->buffered_messages
, seq64be
);
648 * If we already have an entry and this one is a fragment, don't discard
649 * it and rather try to reassemble it.
651 if (item
!= NULL
&& frag_len
!= msg_hdr
->msg_len
)
655 * Discard the message if sequence number was already there, is too far
656 * in the future, already in the queue or if we received a FINISHED
657 * before the SERVER_HELLO, which then must be a stale retransmit.
659 if (msg_hdr
->seq
<= s
->d1
->handshake_read_seq
||
660 msg_hdr
->seq
> s
->d1
->handshake_read_seq
+ 10 || item
!= NULL
||
661 (s
->d1
->handshake_read_seq
== 0 && msg_hdr
->type
== SSL3_MT_FINISHED
)) {
662 unsigned char devnull
[256];
665 i
= s
->method
->ssl_read_bytes(s
, SSL3_RT_HANDSHAKE
, NULL
,
668 sizeof(devnull
) ? sizeof(devnull
) :
669 frag_len
, 0, &readbytes
);
672 frag_len
-= readbytes
;
675 if (frag_len
!= msg_hdr
->msg_len
) {
676 return dtls1_reassemble_fragment(s
, msg_hdr
);
679 if (frag_len
> dtls1_max_handshake_message_len(s
))
682 frag
= dtls1_hm_fragment_new(frag_len
, 0);
686 memcpy(&(frag
->msg_header
), msg_hdr
, sizeof(*msg_hdr
));
690 * read the body of the fragment (header has already been read
692 i
= s
->method
->ssl_read_bytes(s
, SSL3_RT_HANDSHAKE
, NULL
,
693 frag
->fragment
, frag_len
, 0,
695 if (i
<=0 || readbytes
!= frag_len
)
701 item
= pitem_new(seq64be
, frag
);
705 item
= pqueue_insert(s
->d1
->buffered_messages
, item
);
707 * pqueue_insert fails iff a duplicate item is inserted. However,
708 * |item| cannot be a duplicate. If it were, |pqueue_find|, above,
709 * would have returned it. Then, either |frag_len| !=
710 * |msg_hdr->msg_len| in which case |item| is set to NULL and it will
711 * have been processed with |dtls1_reassemble_fragment|, above, or
712 * the record will have been discarded.
714 if (!ossl_assert(item
!= NULL
))
718 return DTLS1_HM_FRAGMENT_RETRY
;
722 dtls1_hm_fragment_free(frag
);
726 static int dtls_get_reassembled_message(SSL
*s
, int *errtype
, size_t *len
)
728 unsigned char wire
[DTLS1_HM_HEADER_LENGTH
];
729 size_t mlen
, frag_off
, frag_len
;
730 int i
, ret
, recvd_type
;
731 struct hm_header_st msg_hdr
;
737 /* see if we have the required fragment already */
738 ret
= dtls1_retrieve_buffered_fragment(s
, &frag_len
);
740 /* SSLfatal() already called */
744 s
->init_num
= frag_len
;
749 /* read handshake message header */
750 i
= s
->method
->ssl_read_bytes(s
, SSL3_RT_HANDSHAKE
, &recvd_type
, wire
,
751 DTLS1_HM_HEADER_LENGTH
, 0, &readbytes
);
752 if (i
<= 0) { /* nbio, or an error */
753 s
->rwstate
= SSL_READING
;
757 if (recvd_type
== SSL3_RT_CHANGE_CIPHER_SPEC
) {
758 if (wire
[0] != SSL3_MT_CCS
) {
759 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
760 SSL_F_DTLS_GET_REASSEMBLED_MESSAGE
,
761 SSL_R_BAD_CHANGE_CIPHER_SPEC
);
765 memcpy(s
->init_buf
->data
, wire
, readbytes
);
766 s
->init_num
= readbytes
- 1;
767 s
->init_msg
= s
->init_buf
->data
+ 1;
768 s
->s3
->tmp
.message_type
= SSL3_MT_CHANGE_CIPHER_SPEC
;
769 s
->s3
->tmp
.message_size
= readbytes
- 1;
770 *len
= readbytes
- 1;
774 /* Handshake fails if message header is incomplete */
775 if (readbytes
!= DTLS1_HM_HEADER_LENGTH
) {
776 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
777 SSL_F_DTLS_GET_REASSEMBLED_MESSAGE
, SSL_R_UNEXPECTED_MESSAGE
);
781 /* parse the message fragment header */
782 dtls1_get_message_header(wire
, &msg_hdr
);
784 mlen
= msg_hdr
.msg_len
;
785 frag_off
= msg_hdr
.frag_off
;
786 frag_len
= msg_hdr
.frag_len
;
789 * We must have at least frag_len bytes left in the record to be read.
790 * Fragments must not span records.
792 if (frag_len
> RECORD_LAYER_get_rrec_length(&s
->rlayer
)) {
793 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
794 SSL_F_DTLS_GET_REASSEMBLED_MESSAGE
, SSL_R_BAD_LENGTH
);
799 * if this is a future (or stale) message it gets buffered
800 * (or dropped)--no further processing at this time
801 * While listening, we accept seq 1 (ClientHello with cookie)
802 * although we're still expecting seq 0 (ClientHello)
804 if (msg_hdr
.seq
!= s
->d1
->handshake_read_seq
) {
805 *errtype
= dtls1_process_out_of_seq_message(s
, &msg_hdr
);
809 if (frag_len
&& frag_len
< mlen
) {
810 *errtype
= dtls1_reassemble_fragment(s
, &msg_hdr
);
815 && s
->d1
->r_msg_hdr
.frag_off
== 0
816 && s
->statem
.hand_state
!= TLS_ST_OK
817 && wire
[0] == SSL3_MT_HELLO_REQUEST
) {
819 * The server may always send 'Hello Request' messages -- we are
820 * doing a handshake anyway now, so ignore them if their format is
821 * correct. Does not count for 'Finished' MAC.
823 if (wire
[1] == 0 && wire
[2] == 0 && wire
[3] == 0) {
825 s
->msg_callback(0, s
->version
, SSL3_RT_HANDSHAKE
,
826 wire
, DTLS1_HM_HEADER_LENGTH
, s
,
827 s
->msg_callback_arg
);
831 } else { /* Incorrectly formatted Hello request */
833 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
834 SSL_F_DTLS_GET_REASSEMBLED_MESSAGE
,
835 SSL_R_UNEXPECTED_MESSAGE
);
840 if (!dtls1_preprocess_fragment(s
, &msg_hdr
)) {
841 /* SSLfatal() already called */
847 (unsigned char *)s
->init_buf
->data
+ DTLS1_HM_HEADER_LENGTH
;
849 i
= s
->method
->ssl_read_bytes(s
, SSL3_RT_HANDSHAKE
, NULL
,
850 &p
[frag_off
], frag_len
, 0, &readbytes
);
853 * This shouldn't ever fail due to NBIO because we already checked
854 * that we have enough data in the record
857 s
->rwstate
= SSL_READING
;
866 * XDTLS: an incorrectly formatted fragment should cause the handshake
869 if (readbytes
!= frag_len
) {
870 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
871 SSL_F_DTLS_GET_REASSEMBLED_MESSAGE
, SSL_R_BAD_LENGTH
);
876 * Note that s->init_num is *not* used as current offset in
877 * s->init_buf->data, but as a counter summing up fragments' lengths: as
878 * soon as they sum up to handshake packet length, we assume we have got
881 *len
= s
->init_num
= frag_len
;
891 * for these 2 messages, we need to
892 * ssl->enc_read_ctx re-init
893 * ssl->rlayer.read_sequence zero
894 * ssl->s3->read_mac_secret re-init
895 * ssl->session->read_sym_enc assign
896 * ssl->session->read_compression assign
897 * ssl->session->read_hash assign
899 int dtls_construct_change_cipher_spec(SSL
*s
, WPACKET
*pkt
)
901 if (s
->version
== DTLS1_BAD_VER
) {
902 s
->d1
->next_handshake_write_seq
++;
904 if (!WPACKET_put_bytes_u16(pkt
, s
->d1
->handshake_write_seq
)) {
905 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
906 SSL_F_DTLS_CONSTRUCT_CHANGE_CIPHER_SPEC
,
907 ERR_R_INTERNAL_ERROR
);
915 #ifndef OPENSSL_NO_SCTP
916 WORK_STATE
dtls_wait_for_dry(SSL
*s
)
920 /* read app data until dry event */
921 ret
= BIO_dgram_sctp_wait_for_dry(SSL_get_wbio(s
));
923 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_DTLS_WAIT_FOR_DRY
,
924 ERR_R_INTERNAL_ERROR
);
929 s
->s3
->in_read_app_data
= 2;
930 s
->rwstate
= SSL_READING
;
931 BIO_clear_retry_flags(SSL_get_rbio(s
));
932 BIO_set_retry_read(SSL_get_rbio(s
));
935 return WORK_FINISHED_CONTINUE
;
939 int dtls1_read_failed(SSL
*s
, int code
)
942 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
943 SSL_F_DTLS1_READ_FAILED
, ERR_R_INTERNAL_ERROR
);
947 if (!dtls1_is_timer_expired(s
)) {
949 * not a timeout, none of our business, let higher layers handle
950 * this. in fact it's probably an error
954 /* done, no need to send a retransmit */
957 BIO_set_flags(SSL_get_rbio(s
), BIO_FLAGS_READ
);
961 return dtls1_handle_timeout(s
);
964 int dtls1_get_queue_priority(unsigned short seq
, int is_ccs
)
967 * The index of the retransmission queue actually is the message sequence
968 * number, since the queue only contains messages of a single handshake.
969 * However, the ChangeCipherSpec has no message sequence number and so
970 * using only the sequence will result in the CCS and Finished having the
971 * same index. To prevent this, the sequence number is multiplied by 2.
972 * In case of a CCS 1 is subtracted. This does not only differ CSS and
973 * Finished, it also maintains the order of the index (important for
974 * priority queues) and fits in the unsigned short variable.
976 return seq
* 2 - is_ccs
;
979 int dtls1_retransmit_buffered_messages(SSL
*s
)
981 pqueue
*sent
= s
->d1
->sent_messages
;
987 iter
= pqueue_iterator(sent
);
989 for (item
= pqueue_next(&iter
); item
!= NULL
; item
= pqueue_next(&iter
)) {
990 frag
= (hm_fragment
*)item
->data
;
991 if (dtls1_retransmit_message(s
, (unsigned short)
992 dtls1_get_queue_priority
993 (frag
->msg_header
.seq
,
994 frag
->msg_header
.is_ccs
), &found
) <= 0)
1001 int dtls1_buffer_message(SSL
*s
, int is_ccs
)
1005 unsigned char seq64be
[8];
1008 * this function is called immediately after a message has been
1011 if (!ossl_assert(s
->init_off
== 0))
1014 frag
= dtls1_hm_fragment_new(s
->init_num
, 0);
1018 memcpy(frag
->fragment
, s
->init_buf
->data
, s
->init_num
);
1021 /* For DTLS1_BAD_VER the header length is non-standard */
1022 if (!ossl_assert(s
->d1
->w_msg_hdr
.msg_len
+
1024 DTLS1_BAD_VER
) ? 3 : DTLS1_CCS_HEADER_LENGTH
)
1025 == (unsigned int)s
->init_num
))
1028 if (!ossl_assert(s
->d1
->w_msg_hdr
.msg_len
+
1029 DTLS1_HM_HEADER_LENGTH
== (unsigned int)s
->init_num
))
1033 frag
->msg_header
.msg_len
= s
->d1
->w_msg_hdr
.msg_len
;
1034 frag
->msg_header
.seq
= s
->d1
->w_msg_hdr
.seq
;
1035 frag
->msg_header
.type
= s
->d1
->w_msg_hdr
.type
;
1036 frag
->msg_header
.frag_off
= 0;
1037 frag
->msg_header
.frag_len
= s
->d1
->w_msg_hdr
.msg_len
;
1038 frag
->msg_header
.is_ccs
= is_ccs
;
1040 /* save current state */
1041 frag
->msg_header
.saved_retransmit_state
.enc_write_ctx
= s
->enc_write_ctx
;
1042 frag
->msg_header
.saved_retransmit_state
.write_hash
= s
->write_hash
;
1043 frag
->msg_header
.saved_retransmit_state
.compress
= s
->compress
;
1044 frag
->msg_header
.saved_retransmit_state
.session
= s
->session
;
1045 frag
->msg_header
.saved_retransmit_state
.epoch
=
1046 DTLS_RECORD_LAYER_get_w_epoch(&s
->rlayer
);
1048 memset(seq64be
, 0, sizeof(seq64be
));
1051 char)(dtls1_get_queue_priority(frag
->msg_header
.seq
,
1052 frag
->msg_header
.is_ccs
) >> 8);
1055 char)(dtls1_get_queue_priority(frag
->msg_header
.seq
,
1056 frag
->msg_header
.is_ccs
));
1058 item
= pitem_new(seq64be
, frag
);
1060 dtls1_hm_fragment_free(frag
);
1064 pqueue_insert(s
->d1
->sent_messages
, item
);
1068 int dtls1_retransmit_message(SSL
*s
, unsigned short seq
, int *found
)
1071 /* XDTLS: for now assuming that read/writes are blocking */
1074 unsigned long header_length
;
1075 unsigned char seq64be
[8];
1076 struct dtls1_retransmit_state saved_state
;
1078 /* XDTLS: the requested message ought to be found, otherwise error */
1079 memset(seq64be
, 0, sizeof(seq64be
));
1080 seq64be
[6] = (unsigned char)(seq
>> 8);
1081 seq64be
[7] = (unsigned char)seq
;
1083 item
= pqueue_find(s
->d1
->sent_messages
, seq64be
);
1085 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_DTLS1_RETRANSMIT_MESSAGE
,
1086 ERR_R_INTERNAL_ERROR
);
1092 frag
= (hm_fragment
*)item
->data
;
1094 if (frag
->msg_header
.is_ccs
)
1095 header_length
= DTLS1_CCS_HEADER_LENGTH
;
1097 header_length
= DTLS1_HM_HEADER_LENGTH
;
1099 memcpy(s
->init_buf
->data
, frag
->fragment
,
1100 frag
->msg_header
.msg_len
+ header_length
);
1101 s
->init_num
= frag
->msg_header
.msg_len
+ header_length
;
1103 dtls1_set_message_header_int(s
, frag
->msg_header
.type
,
1104 frag
->msg_header
.msg_len
,
1105 frag
->msg_header
.seq
, 0,
1106 frag
->msg_header
.frag_len
);
1108 /* save current state */
1109 saved_state
.enc_write_ctx
= s
->enc_write_ctx
;
1110 saved_state
.write_hash
= s
->write_hash
;
1111 saved_state
.compress
= s
->compress
;
1112 saved_state
.session
= s
->session
;
1113 saved_state
.epoch
= DTLS_RECORD_LAYER_get_w_epoch(&s
->rlayer
);
1115 s
->d1
->retransmitting
= 1;
1117 /* restore state in which the message was originally sent */
1118 s
->enc_write_ctx
= frag
->msg_header
.saved_retransmit_state
.enc_write_ctx
;
1119 s
->write_hash
= frag
->msg_header
.saved_retransmit_state
.write_hash
;
1120 s
->compress
= frag
->msg_header
.saved_retransmit_state
.compress
;
1121 s
->session
= frag
->msg_header
.saved_retransmit_state
.session
;
1122 DTLS_RECORD_LAYER_set_saved_w_epoch(&s
->rlayer
,
1124 saved_retransmit_state
.epoch
);
1126 ret
= dtls1_do_write(s
, frag
->msg_header
.is_ccs
?
1127 SSL3_RT_CHANGE_CIPHER_SPEC
: SSL3_RT_HANDSHAKE
);
1129 /* restore current state */
1130 s
->enc_write_ctx
= saved_state
.enc_write_ctx
;
1131 s
->write_hash
= saved_state
.write_hash
;
1132 s
->compress
= saved_state
.compress
;
1133 s
->session
= saved_state
.session
;
1134 DTLS_RECORD_LAYER_set_saved_w_epoch(&s
->rlayer
, saved_state
.epoch
);
1136 s
->d1
->retransmitting
= 0;
1138 (void)BIO_flush(s
->wbio
);
1142 void dtls1_set_message_header(SSL
*s
,
1143 unsigned char mt
, size_t len
,
1144 size_t frag_off
, size_t frag_len
)
1146 if (frag_off
== 0) {
1147 s
->d1
->handshake_write_seq
= s
->d1
->next_handshake_write_seq
;
1148 s
->d1
->next_handshake_write_seq
++;
1151 dtls1_set_message_header_int(s
, mt
, len
, s
->d1
->handshake_write_seq
,
1152 frag_off
, frag_len
);
1155 /* don't actually do the writing, wait till the MTU has been retrieved */
1157 dtls1_set_message_header_int(SSL
*s
, unsigned char mt
,
1158 size_t len
, unsigned short seq_num
,
1159 size_t frag_off
, size_t frag_len
)
1161 struct hm_header_st
*msg_hdr
= &s
->d1
->w_msg_hdr
;
1164 msg_hdr
->msg_len
= len
;
1165 msg_hdr
->seq
= seq_num
;
1166 msg_hdr
->frag_off
= frag_off
;
1167 msg_hdr
->frag_len
= frag_len
;
1171 dtls1_fix_message_header(SSL
*s
, size_t frag_off
, size_t frag_len
)
1173 struct hm_header_st
*msg_hdr
= &s
->d1
->w_msg_hdr
;
1175 msg_hdr
->frag_off
= frag_off
;
1176 msg_hdr
->frag_len
= frag_len
;
1179 static unsigned char *dtls1_write_message_header(SSL
*s
, unsigned char *p
)
1181 struct hm_header_st
*msg_hdr
= &s
->d1
->w_msg_hdr
;
1183 *p
++ = msg_hdr
->type
;
1184 l2n3(msg_hdr
->msg_len
, p
);
1186 s2n(msg_hdr
->seq
, p
);
1187 l2n3(msg_hdr
->frag_off
, p
);
1188 l2n3(msg_hdr
->frag_len
, p
);
1193 void dtls1_get_message_header(unsigned char *data
, struct hm_header_st
*msg_hdr
)
1195 memset(msg_hdr
, 0, sizeof(*msg_hdr
));
1196 msg_hdr
->type
= *(data
++);
1197 n2l3(data
, msg_hdr
->msg_len
);
1199 n2s(data
, msg_hdr
->seq
);
1200 n2l3(data
, msg_hdr
->frag_off
);
1201 n2l3(data
, msg_hdr
->frag_len
);
1204 int dtls1_set_handshake_header(SSL
*s
, WPACKET
*pkt
, int htype
)
1206 unsigned char *header
;
1208 if (htype
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
1209 s
->d1
->handshake_write_seq
= s
->d1
->next_handshake_write_seq
;
1210 dtls1_set_message_header_int(s
, SSL3_MT_CCS
, 0,
1211 s
->d1
->handshake_write_seq
, 0, 0);
1212 if (!WPACKET_put_bytes_u8(pkt
, SSL3_MT_CCS
))
1215 dtls1_set_message_header(s
, htype
, 0, 0, 0);
1217 * We allocate space at the start for the message header. This gets
1220 if (!WPACKET_allocate_bytes(pkt
, DTLS1_HM_HEADER_LENGTH
, &header
)
1221 || !WPACKET_start_sub_packet(pkt
))
1228 int dtls1_close_construct_packet(SSL
*s
, WPACKET
*pkt
, int htype
)
1232 if ((htype
!= SSL3_MT_CHANGE_CIPHER_SPEC
&& !WPACKET_close(pkt
))
1233 || !WPACKET_get_length(pkt
, &msglen
)
1234 || msglen
> INT_MAX
)
1237 if (htype
!= SSL3_MT_CHANGE_CIPHER_SPEC
) {
1238 s
->d1
->w_msg_hdr
.msg_len
= msglen
- DTLS1_HM_HEADER_LENGTH
;
1239 s
->d1
->w_msg_hdr
.frag_len
= msglen
- DTLS1_HM_HEADER_LENGTH
;
1241 s
->init_num
= (int)msglen
;
1244 if (htype
!= DTLS1_MT_HELLO_VERIFY_REQUEST
) {
1245 /* Buffer the message to handle re-xmits */
1246 if (!dtls1_buffer_message(s
, htype
== SSL3_MT_CHANGE_CIPHER_SPEC