1 /* $OpenBSD: d1_both.c,v 1.51 2017/05/07 04:22:24 beck Exp $ */
3 * DTLS implementation written by Nagendra Modadugu
4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
6 /* ====================================================================
7 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * openssl-core@openssl.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
59 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
60 * All rights reserved.
62 * This package is an SSL implementation written
63 * by Eric Young (eay@cryptsoft.com).
64 * The implementation was written so as to conform with Netscapes SSL.
66 * This library is free for commercial and non-commercial use as long as
67 * the following conditions are aheared to. The following conditions
68 * apply to all code found in this distribution, be it the RC4, RSA,
69 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
70 * included with this distribution is covered by the same copyright terms
71 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
73 * Copyright remains Eric Young's, and as such any Copyright notices in
74 * the code are not to be removed.
75 * If this package is used in a product, Eric Young should be given attribution
76 * as the author of the parts of the library used.
77 * This can be in the form of a textual message at program startup or
78 * in documentation (online or textual) provided with the package.
80 * Redistribution and use in source and binary forms, with or without
81 * modification, are permitted provided that the following conditions
83 * 1. Redistributions of source code must retain the copyright
84 * notice, this list of conditions and the following disclaimer.
85 * 2. Redistributions in binary form must reproduce the above copyright
86 * notice, this list of conditions and the following disclaimer in the
87 * documentation and/or other materials provided with the distribution.
88 * 3. All advertising materials mentioning features or use of this software
89 * must display the following acknowledgement:
90 * "This product includes cryptographic software written by
91 * Eric Young (eay@cryptsoft.com)"
92 * The word 'cryptographic' can be left out if the rouines from the library
93 * being used are not cryptographic related :-).
94 * 4. If you include any Windows specific code (or a derivative thereof) from
95 * the apps directory (application code) you must include an acknowledgement:
96 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
98 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
99 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
100 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
101 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
102 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
103 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
104 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
105 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
106 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
107 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
110 * The licence and distribution terms for any publically available version or
111 * derivative of this code cannot be changed. i.e. this code cannot simply be
112 * copied and put under another distribution licence
113 * [including the GNU Public Licence.]
120 #include "ssl_locl.h"
122 #include <openssl/buffer.h>
123 #include <openssl/evp.h>
124 #include <openssl/objects.h>
125 #include <openssl/x509.h>
128 #include "bytestring.h"
130 #define RSMBLY_BITMASK_SIZE(msg_len) (((msg_len) + 7) / 8)
132 #define RSMBLY_BITMASK_MARK(bitmask, start, end) { \
133 if ((end) - (start) <= 8) { \
135 for (ii = (start); ii < (end); ii++) bitmask[((ii) >> 3)] |= (1 << ((ii) & 7)); \
138 bitmask[((start) >> 3)] |= bitmask_start_values[((start) & 7)]; \
139 for (ii = (((start) >> 3) + 1); ii < ((((end) - 1)) >> 3); ii++) bitmask[ii] = 0xff; \
140 bitmask[(((end) - 1) >> 3)] |= bitmask_end_values[((end) & 7)]; \
143 #define RSMBLY_BITMASK_IS_COMPLETE(bitmask, msg_len, is_complete) { \
145 OPENSSL_assert((msg_len) > 0); \
147 if (bitmask[(((msg_len) - 1) >> 3)] != bitmask_end_values[((msg_len) & 7)]) is_complete = 0; \
148 if (is_complete) for (ii = (((msg_len) - 1) >> 3) - 1; ii >= 0 ; ii--) \
149 if (bitmask[ii] != 0xff) { is_complete = 0; break; } }
151 static unsigned char bitmask_start_values
[] = {
152 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80
154 static unsigned char bitmask_end_values
[] = {
155 0xff, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f
158 /* XDTLS: figure out the right values */
159 static unsigned int g_probable_mtu
[] = {1500 - 28, 512 - 28, 256 - 28};
161 static unsigned int dtls1_guess_mtu(unsigned int curr_mtu
);
162 static void dtls1_fix_message_header(SSL
*s
, unsigned long frag_off
,
163 unsigned long frag_len
);
164 static unsigned char *dtls1_write_message_header(SSL
*s
, unsigned char *p
);
165 static void dtls1_set_message_header_int(SSL
*s
, unsigned char mt
,
166 unsigned long len
, unsigned short seq_num
, unsigned long frag_off
,
167 unsigned long frag_len
);
168 static long dtls1_get_message_fragment(SSL
*s
, int st1
, int stn
, long max
,
172 dtls1_hm_fragment_new(unsigned long frag_len
, int reassembly
)
174 hm_fragment
*frag
= NULL
;
175 unsigned char *buf
= NULL
;
176 unsigned char *bitmask
= NULL
;
178 frag
= malloc(sizeof(hm_fragment
));
183 buf
= malloc(frag_len
);
190 /* zero length fragment gets zero frag->fragment */
191 frag
->fragment
= buf
;
193 /* Initialize reassembly bitmask if necessary */
195 bitmask
= malloc(RSMBLY_BITMASK_SIZE(frag_len
));
196 if (bitmask
== NULL
) {
201 memset(bitmask
, 0, RSMBLY_BITMASK_SIZE(frag_len
));
204 frag
->reassembly
= bitmask
;
210 dtls1_hm_fragment_free(hm_fragment
*frag
)
215 if (frag
->msg_header
.is_ccs
) {
217 frag
->msg_header
.saved_retransmit_state
.enc_write_ctx
);
219 frag
->msg_header
.saved_retransmit_state
.write_hash
);
221 free(frag
->fragment
);
222 free(frag
->reassembly
);
226 /* send s->internal->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or SSL3_RT_CHANGE_CIPHER_SPEC) */
228 dtls1_do_write(SSL
*s
, int type
)
232 unsigned int len
, frag_off
, mac_size
, blocksize
;
234 /* AHA! Figure out the MTU, and stick to the right size */
235 if (D1I(s
)->mtu
< dtls1_min_mtu() &&
236 !(SSL_get_options(s
) & SSL_OP_NO_QUERY_MTU
)) {
237 D1I(s
)->mtu
= BIO_ctrl(SSL_get_wbio(s
),
238 BIO_CTRL_DGRAM_QUERY_MTU
, 0, NULL
);
241 * I've seen the kernel return bogus numbers when it
242 * doesn't know the MTU (ie., the initial write), so just
243 * make sure we have a reasonable number
245 if (D1I(s
)->mtu
< dtls1_min_mtu()) {
247 D1I(s
)->mtu
= dtls1_guess_mtu(D1I(s
)->mtu
);
248 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SET_MTU
,
253 OPENSSL_assert(D1I(s
)->mtu
>= dtls1_min_mtu());
254 /* should have something reasonable now */
256 if (s
->internal
->init_off
== 0 && type
== SSL3_RT_HANDSHAKE
)
257 OPENSSL_assert(s
->internal
->init_num
==
258 (int)D1I(s
)->w_msg_hdr
.msg_len
+ DTLS1_HM_HEADER_LENGTH
);
260 if (s
->internal
->write_hash
)
261 mac_size
= EVP_MD_CTX_size(s
->internal
->write_hash
);
265 if (s
->internal
->enc_write_ctx
&&
266 (EVP_CIPHER_mode( s
->internal
->enc_write_ctx
->cipher
) & EVP_CIPH_CBC_MODE
))
267 blocksize
= 2 * EVP_CIPHER_block_size(s
->internal
->enc_write_ctx
->cipher
);
272 while (s
->internal
->init_num
) {
273 curr_mtu
= D1I(s
)->mtu
- BIO_wpending(SSL_get_wbio(s
)) -
274 DTLS1_RT_HEADER_LENGTH
- mac_size
- blocksize
;
276 if (curr_mtu
<= DTLS1_HM_HEADER_LENGTH
) {
277 /* grr.. we could get an error if MTU picked was wrong */
278 ret
= BIO_flush(SSL_get_wbio(s
));
281 curr_mtu
= D1I(s
)->mtu
- DTLS1_RT_HEADER_LENGTH
-
282 mac_size
- blocksize
;
285 if (s
->internal
->init_num
> curr_mtu
)
288 len
= s
->internal
->init_num
;
291 /* XDTLS: this function is too long. split out the CCS part */
292 if (type
== SSL3_RT_HANDSHAKE
) {
293 if (s
->internal
->init_off
!= 0) {
294 OPENSSL_assert(s
->internal
->init_off
> DTLS1_HM_HEADER_LENGTH
);
295 s
->internal
->init_off
-= DTLS1_HM_HEADER_LENGTH
;
296 s
->internal
->init_num
+= DTLS1_HM_HEADER_LENGTH
;
298 if (s
->internal
->init_num
> curr_mtu
)
301 len
= s
->internal
->init_num
;
304 dtls1_fix_message_header(s
, frag_off
,
305 len
- DTLS1_HM_HEADER_LENGTH
);
307 dtls1_write_message_header(s
,
308 (unsigned char *)&s
->internal
->init_buf
->data
[s
->internal
->init_off
]);
310 OPENSSL_assert(len
>= DTLS1_HM_HEADER_LENGTH
);
313 ret
= dtls1_write_bytes(s
, type
,
314 &s
->internal
->init_buf
->data
[s
->internal
->init_off
], len
);
317 * Might need to update MTU here, but we don't know
318 * which previous packet caused the failure -- so
319 * can't really retransmit anything. continue as
320 * if everything is fine and wait for an alert to
321 * handle the retransmit
323 if (BIO_ctrl(SSL_get_wbio(s
),
324 BIO_CTRL_DGRAM_MTU_EXCEEDED
, 0, NULL
) > 0)
325 D1I(s
)->mtu
= BIO_ctrl(SSL_get_wbio(s
),
326 BIO_CTRL_DGRAM_QUERY_MTU
, 0, NULL
);
332 * Bad if this assert fails, only part of the
333 * handshake message got sent. but why would
336 OPENSSL_assert(len
== (unsigned int)ret
);
338 if (type
== SSL3_RT_HANDSHAKE
&&
339 !D1I(s
)->retransmitting
) {
341 * Should not be done for 'Hello Request's,
342 * but in that case we'll ignore the result
345 unsigned char *p
= (unsigned char *)&s
->internal
->init_buf
->data
[s
->internal
->init_off
];
346 const struct hm_header_st
*msg_hdr
= &D1I(s
)->w_msg_hdr
;
351 * Reconstruct message header is if it
352 * is being sent in single fragment
354 *p
++ = msg_hdr
->type
;
355 l2n3(msg_hdr
->msg_len
, p
);
356 s2n (msg_hdr
->seq
, p
);
358 l2n3(msg_hdr
->msg_len
, p
);
359 p
-= DTLS1_HM_HEADER_LENGTH
;
362 p
+= DTLS1_HM_HEADER_LENGTH
;
363 xlen
= ret
- DTLS1_HM_HEADER_LENGTH
;
366 tls1_finish_mac(s
, p
, xlen
);
369 if (ret
== s
->internal
->init_num
) {
370 if (s
->internal
->msg_callback
)
371 s
->internal
->msg_callback(1, s
->version
, type
,
372 s
->internal
->init_buf
->data
,
373 (size_t)(s
->internal
->init_off
+ s
->internal
->init_num
),
374 s
, s
->internal
->msg_callback_arg
);
376 s
->internal
->init_off
= 0;
377 /* done writing this message */
378 s
->internal
->init_num
= 0;
382 s
->internal
->init_off
+= ret
;
383 s
->internal
->init_num
-= ret
;
384 frag_off
+= (ret
-= DTLS1_HM_HEADER_LENGTH
);
392 * Obtain handshake message of message type 'mt' (any if mt == -1),
393 * maximum acceptable body length 'max'.
394 * Read an entire handshake message. Handshake messages arrive in
398 dtls1_get_message(SSL
*s
, int st1
, int stn
, int mt
, long max
, int *ok
)
401 struct hm_header_st
*msg_hdr
;
403 unsigned long msg_len
;
406 * s3->internal->tmp is used to store messages that are unexpected, caused
407 * by the absence of an optional handshake message
409 if (S3I(s
)->tmp
.reuse_message
) {
410 S3I(s
)->tmp
.reuse_message
= 0;
411 if ((mt
>= 0) && (S3I(s
)->tmp
.message_type
!= mt
)) {
412 al
= SSL_AD_UNEXPECTED_MESSAGE
;
413 SSLerror(s
, SSL_R_UNEXPECTED_MESSAGE
);
417 s
->internal
->init_msg
= s
->internal
->init_buf
->data
+ DTLS1_HM_HEADER_LENGTH
;
418 s
->internal
->init_num
= (int)S3I(s
)->tmp
.message_size
;
419 return s
->internal
->init_num
;
422 msg_hdr
= &D1I(s
)->r_msg_hdr
;
423 memset(msg_hdr
, 0x00, sizeof(struct hm_header_st
));
426 i
= dtls1_get_message_fragment(s
, st1
, stn
, max
, ok
);
427 if (i
== DTLS1_HM_BAD_FRAGMENT
||
428 i
== DTLS1_HM_FRAGMENT_RETRY
) /* bad fragment received */
430 else if (i
<= 0 && !*ok
)
433 p
= (unsigned char *)s
->internal
->init_buf
->data
;
434 msg_len
= msg_hdr
->msg_len
;
436 /* reconstruct message header */
437 *(p
++) = msg_hdr
->type
;
439 s2n (msg_hdr
->seq
, p
);
443 p
-= DTLS1_HM_HEADER_LENGTH
;
444 msg_len
+= DTLS1_HM_HEADER_LENGTH
;
446 tls1_finish_mac(s
, p
, msg_len
);
447 if (s
->internal
->msg_callback
)
448 s
->internal
->msg_callback(0, s
->version
, SSL3_RT_HANDSHAKE
, p
, msg_len
,
449 s
, s
->internal
->msg_callback_arg
);
451 memset(msg_hdr
, 0x00, sizeof(struct hm_header_st
));
453 /* Don't change sequence numbers while listening */
455 D1I(s
)->handshake_read_seq
++;
457 s
->internal
->init_msg
= s
->internal
->init_buf
->data
+ DTLS1_HM_HEADER_LENGTH
;
458 return s
->internal
->init_num
;
461 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
468 dtls1_preprocess_fragment(SSL
*s
, struct hm_header_st
*msg_hdr
, int max
)
470 size_t frag_off
, frag_len
, msg_len
;
472 msg_len
= msg_hdr
->msg_len
;
473 frag_off
= msg_hdr
->frag_off
;
474 frag_len
= msg_hdr
->frag_len
;
476 /* sanity checking */
477 if ((frag_off
+ frag_len
) > msg_len
) {
478 SSLerror(s
, SSL_R_EXCESSIVE_MESSAGE_SIZE
);
479 return SSL_AD_ILLEGAL_PARAMETER
;
482 if ((frag_off
+ frag_len
) > (unsigned long)max
) {
483 SSLerror(s
, SSL_R_EXCESSIVE_MESSAGE_SIZE
);
484 return SSL_AD_ILLEGAL_PARAMETER
;
487 if ( D1I(s
)->r_msg_hdr
.frag_off
== 0) /* first fragment */
490 * msg_len is limited to 2^24, but is effectively checked
493 if (!BUF_MEM_grow_clean(s
->internal
->init_buf
,
494 msg_len
+ DTLS1_HM_HEADER_LENGTH
)) {
495 SSLerror(s
, ERR_R_BUF_LIB
);
496 return SSL_AD_INTERNAL_ERROR
;
499 S3I(s
)->tmp
.message_size
= msg_len
;
500 D1I(s
)->r_msg_hdr
.msg_len
= msg_len
;
501 S3I(s
)->tmp
.message_type
= msg_hdr
->type
;
502 D1I(s
)->r_msg_hdr
.type
= msg_hdr
->type
;
503 D1I(s
)->r_msg_hdr
.seq
= msg_hdr
->seq
;
504 } else if (msg_len
!= D1I(s
)->r_msg_hdr
.msg_len
) {
506 * They must be playing with us! BTW, failure to enforce
507 * upper limit would open possibility for buffer overrun.
509 SSLerror(s
, SSL_R_EXCESSIVE_MESSAGE_SIZE
);
510 return SSL_AD_ILLEGAL_PARAMETER
;
513 return 0; /* no error */
517 dtls1_retrieve_buffered_fragment(SSL
*s
, long max
, int *ok
)
520 * (0) check whether the desired fragment is available
522 * (1) copy over the fragment to s->internal->init_buf->data[]
523 * (2) update s->internal->init_num
530 item
= pqueue_peek(D1I(s
)->buffered_messages
);
534 frag
= (hm_fragment
*)item
->data
;
536 /* Don't return if reassembly still in progress */
537 if (frag
->reassembly
!= NULL
)
540 if (D1I(s
)->handshake_read_seq
== frag
->msg_header
.seq
) {
541 unsigned long frag_len
= frag
->msg_header
.frag_len
;
542 pqueue_pop(D1I(s
)->buffered_messages
);
544 al
= dtls1_preprocess_fragment(s
, &frag
->msg_header
, max
);
546 if (al
== 0) /* no alert */
548 unsigned char *p
= (unsigned char *)s
->internal
->init_buf
->data
+ DTLS1_HM_HEADER_LENGTH
;
549 memcpy(&p
[frag
->msg_header
.frag_off
],
550 frag
->fragment
, frag
->msg_header
.frag_len
);
553 dtls1_hm_fragment_free(frag
);
561 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
562 s
->internal
->init_num
= 0;
570 * dtls1_max_handshake_message_len returns the maximum number of bytes
571 * permitted in a DTLS handshake message for |s|. The minimum is 16KB,
572 * but may be greater if the maximum certificate list size requires it.
575 dtls1_max_handshake_message_len(const SSL
*s
)
577 unsigned long max_len
;
579 max_len
= DTLS1_HM_HEADER_LENGTH
+ SSL3_RT_MAX_ENCRYPTED_LENGTH
;
580 if (max_len
< (unsigned long)s
->internal
->max_cert_list
)
581 return s
->internal
->max_cert_list
;
586 dtls1_reassemble_fragment(SSL
*s
, struct hm_header_st
* msg_hdr
, int *ok
)
588 hm_fragment
*frag
= NULL
;
590 int i
= -1, is_complete
;
591 unsigned char seq64be
[8];
592 unsigned long frag_len
= msg_hdr
->frag_len
;
594 if ((msg_hdr
->frag_off
+ frag_len
) > msg_hdr
->msg_len
||
595 msg_hdr
->msg_len
> dtls1_max_handshake_message_len(s
))
599 i
= DTLS1_HM_FRAGMENT_RETRY
;
603 /* Try to find item in queue */
604 memset(seq64be
, 0, sizeof(seq64be
));
605 seq64be
[6] = (unsigned char)(msg_hdr
->seq
>> 8);
606 seq64be
[7] = (unsigned char)msg_hdr
->seq
;
607 item
= pqueue_find(D1I(s
)->buffered_messages
, seq64be
);
610 frag
= dtls1_hm_fragment_new(msg_hdr
->msg_len
, 1);
613 memcpy(&(frag
->msg_header
), msg_hdr
, sizeof(*msg_hdr
));
614 frag
->msg_header
.frag_len
= frag
->msg_header
.msg_len
;
615 frag
->msg_header
.frag_off
= 0;
617 frag
= (hm_fragment
*)item
->data
;
618 if (frag
->msg_header
.msg_len
!= msg_hdr
->msg_len
) {
626 * If message is already reassembled, this must be a
627 * retransmit and can be dropped.
629 if (frag
->reassembly
== NULL
) {
630 unsigned char devnull
[256];
633 i
= s
->method
->internal
->ssl_read_bytes(s
, SSL3_RT_HANDSHAKE
,
634 devnull
, frag_len
> sizeof(devnull
) ?
635 sizeof(devnull
) : frag_len
, 0);
640 i
= DTLS1_HM_FRAGMENT_RETRY
;
644 /* read the body of the fragment (header has already been read */
645 i
= s
->method
->internal
->ssl_read_bytes(s
, SSL3_RT_HANDSHAKE
,
646 frag
->fragment
+ msg_hdr
->frag_off
, frag_len
, 0);
647 if (i
<= 0 || (unsigned long)i
!= frag_len
)
650 RSMBLY_BITMASK_MARK(frag
->reassembly
, (long)msg_hdr
->frag_off
,
651 (long)(msg_hdr
->frag_off
+ frag_len
));
653 RSMBLY_BITMASK_IS_COMPLETE(frag
->reassembly
, (long)msg_hdr
->msg_len
,
657 free(frag
->reassembly
);
658 frag
->reassembly
= NULL
;
662 memset(seq64be
, 0, sizeof(seq64be
));
663 seq64be
[6] = (unsigned char)(msg_hdr
->seq
>> 8);
664 seq64be
[7] = (unsigned char)(msg_hdr
->seq
);
666 item
= pitem_new(seq64be
, frag
);
672 pqueue_insert(D1I(s
)->buffered_messages
, item
);
675 return DTLS1_HM_FRAGMENT_RETRY
;
678 if (item
== NULL
&& frag
!= NULL
)
679 dtls1_hm_fragment_free(frag
);
686 dtls1_process_out_of_seq_message(SSL
*s
, struct hm_header_st
* msg_hdr
, int *ok
)
689 hm_fragment
*frag
= NULL
;
691 unsigned char seq64be
[8];
692 unsigned long frag_len
= msg_hdr
->frag_len
;
694 if ((msg_hdr
->frag_off
+ frag_len
) > msg_hdr
->msg_len
)
697 /* Try to find item in queue, to prevent duplicate entries */
698 memset(seq64be
, 0, sizeof(seq64be
));
699 seq64be
[6] = (unsigned char) (msg_hdr
->seq
>> 8);
700 seq64be
[7] = (unsigned char) msg_hdr
->seq
;
701 item
= pqueue_find(D1I(s
)->buffered_messages
, seq64be
);
704 * If we already have an entry and this one is a fragment,
705 * don't discard it and rather try to reassemble it.
707 if (item
!= NULL
&& frag_len
< msg_hdr
->msg_len
)
711 * Discard the message if sequence number was already there, is
712 * too far in the future, already in the queue or if we received
713 * a FINISHED before the SERVER_HELLO, which then must be a stale
716 if (msg_hdr
->seq
<= D1I(s
)->handshake_read_seq
||
717 msg_hdr
->seq
> D1I(s
)->handshake_read_seq
+ 10 || item
!= NULL
||
718 (D1I(s
)->handshake_read_seq
== 0 &&
719 msg_hdr
->type
== SSL3_MT_FINISHED
)) {
720 unsigned char devnull
[256];
723 i
= s
->method
->internal
->ssl_read_bytes(s
, SSL3_RT_HANDSHAKE
,
724 devnull
, frag_len
> sizeof(devnull
) ?
725 sizeof(devnull
) : frag_len
, 0);
731 if (frag_len
< msg_hdr
->msg_len
)
732 return dtls1_reassemble_fragment(s
, msg_hdr
, ok
);
734 if (frag_len
> dtls1_max_handshake_message_len(s
))
737 frag
= dtls1_hm_fragment_new(frag_len
, 0);
741 memcpy(&(frag
->msg_header
), msg_hdr
, sizeof(*msg_hdr
));
744 /* read the body of the fragment (header has already been read */
745 i
= s
->method
->internal
->ssl_read_bytes(s
, SSL3_RT_HANDSHAKE
,
746 frag
->fragment
, frag_len
, 0);
747 if (i
<= 0 || (unsigned long)i
!= frag_len
)
751 memset(seq64be
, 0, sizeof(seq64be
));
752 seq64be
[6] = (unsigned char)(msg_hdr
->seq
>> 8);
753 seq64be
[7] = (unsigned char)(msg_hdr
->seq
);
755 item
= pitem_new(seq64be
, frag
);
759 pqueue_insert(D1I(s
)->buffered_messages
, item
);
762 return DTLS1_HM_FRAGMENT_RETRY
;
765 if (item
== NULL
&& frag
!= NULL
)
766 dtls1_hm_fragment_free(frag
);
773 dtls1_get_message_fragment(SSL
*s
, int st1
, int stn
, long max
, int *ok
)
775 unsigned char wire
[DTLS1_HM_HEADER_LENGTH
];
776 unsigned long len
, frag_off
, frag_len
;
778 struct hm_header_st msg_hdr
;
781 /* see if we have the required fragment already */
782 if ((frag_len
= dtls1_retrieve_buffered_fragment(s
, max
, ok
)) || *ok
) {
784 s
->internal
->init_num
= frag_len
;
788 /* read handshake message header */
789 i
= s
->method
->internal
->ssl_read_bytes(s
, SSL3_RT_HANDSHAKE
, wire
,
790 DTLS1_HM_HEADER_LENGTH
, 0);
791 if (i
<= 0) /* nbio, or an error */
793 s
->internal
->rwstate
= SSL_READING
;
797 /* Handshake fails if message header is incomplete */
798 if (i
!= DTLS1_HM_HEADER_LENGTH
||
799 /* parse the message fragment header */
800 dtls1_get_message_header(wire
, &msg_hdr
) == 0) {
801 al
= SSL_AD_UNEXPECTED_MESSAGE
;
802 SSLerror(s
, SSL_R_UNEXPECTED_MESSAGE
);
807 * if this is a future (or stale) message it gets buffered
808 * (or dropped)--no further processing at this time
809 * While listening, we accept seq 1 (ClientHello with cookie)
810 * although we're still expecting seq 0 (ClientHello)
812 if (msg_hdr
.seq
!= D1I(s
)->handshake_read_seq
&&
813 !(D1I(s
)->listen
&& msg_hdr
.seq
== 1))
814 return dtls1_process_out_of_seq_message(s
, &msg_hdr
, ok
);
816 len
= msg_hdr
.msg_len
;
817 frag_off
= msg_hdr
.frag_off
;
818 frag_len
= msg_hdr
.frag_len
;
820 if (frag_len
&& frag_len
< len
)
821 return dtls1_reassemble_fragment(s
, &msg_hdr
, ok
);
823 if (!s
->server
&& D1I(s
)->r_msg_hdr
.frag_off
== 0 &&
824 wire
[0] == SSL3_MT_HELLO_REQUEST
) {
826 * The server may always send 'Hello Request' messages --
827 * we are doing a handshake anyway now, so ignore them
828 * if their format is correct. Does not count for
831 if (wire
[1] == 0 && wire
[2] == 0 && wire
[3] == 0) {
832 if (s
->internal
->msg_callback
)
833 s
->internal
->msg_callback(0, s
->version
,
834 SSL3_RT_HANDSHAKE
, wire
,
835 DTLS1_HM_HEADER_LENGTH
, s
,
836 s
->internal
->msg_callback_arg
);
838 s
->internal
->init_num
= 0;
841 else /* Incorrectly formated Hello request */
843 al
= SSL_AD_UNEXPECTED_MESSAGE
;
844 SSLerror(s
, SSL_R_UNEXPECTED_MESSAGE
);
849 if ((al
= dtls1_preprocess_fragment(s
, &msg_hdr
, max
)))
852 /* XDTLS: ressurect this when restart is in place */
853 S3I(s
)->hs
.state
= stn
;
856 unsigned char *p
= (unsigned char *)s
->internal
->init_buf
->data
+ DTLS1_HM_HEADER_LENGTH
;
858 i
= s
->method
->internal
->ssl_read_bytes(s
, SSL3_RT_HANDSHAKE
,
859 &p
[frag_off
], frag_len
, 0);
860 /* XDTLS: fix this--message fragments cannot span multiple packets */
862 s
->internal
->rwstate
= SSL_READING
;
870 * XDTLS: an incorrectly formatted fragment should cause the
873 if (i
!= (int)frag_len
) {
874 al
= SSL3_AD_ILLEGAL_PARAMETER
;
875 SSLerror(s
, SSL3_AD_ILLEGAL_PARAMETER
);
882 * Note that s->internal->init_num is *not* used as current offset in
883 * s->internal->init_buf->data, but as a counter summing up fragments'
884 * lengths: as soon as they sum up to handshake packet
885 * length, we assume we have got all the fragments.
887 s
->internal
->init_num
= frag_len
;
891 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
892 s
->internal
->init_num
= 0;
899 * for these 2 messages, we need to
900 * ssl->enc_read_ctx re-init
901 * ssl->s3->internal->read_sequence zero
902 * ssl->s3->internal->read_mac_secret re-init
903 * ssl->session->read_sym_enc assign
904 * ssl->session->read_hash assign
907 dtls1_send_change_cipher_spec(SSL
*s
, int a
, int b
)
911 if (S3I(s
)->hs
.state
== a
) {
912 p
= (unsigned char *)s
->internal
->init_buf
->data
;
914 D1I(s
)->handshake_write_seq
= D1I(s
)->next_handshake_write_seq
;
915 s
->internal
->init_num
= DTLS1_CCS_HEADER_LENGTH
;
917 s
->internal
->init_off
= 0;
919 dtls1_set_message_header_int(s
, SSL3_MT_CCS
, 0,
920 D1I(s
)->handshake_write_seq
, 0, 0);
922 /* buffer the message to handle re-xmits */
923 dtls1_buffer_message(s
, 1);
925 S3I(s
)->hs
.state
= b
;
928 /* SSL3_ST_CW_CHANGE_B */
929 return (dtls1_do_write(s
, SSL3_RT_CHANGE_CIPHER_SPEC
));
933 dtls1_read_failed(SSL
*s
, int code
)
937 fprintf(stderr
, "invalid state reached %s:%d",
943 if (!dtls1_is_timer_expired(s
)) {
945 * not a timeout, none of our business, let higher layers
946 * handle this. in fact it's probably an error
951 if (!SSL_in_init(s
)) /* done, no need to send a retransmit */
953 BIO_set_flags(SSL_get_rbio(s
), BIO_FLAGS_READ
);
957 return dtls1_handle_timeout(s
);
961 dtls1_get_queue_priority(unsigned short seq
, int is_ccs
)
964 * The index of the retransmission queue actually is the message
965 * sequence number, since the queue only contains messages of a
966 * single handshake. However, the ChangeCipherSpec has no message
967 * sequence number and so using only the sequence will result in
968 * the CCS and Finished having the same index. To prevent this, the
969 * sequence number is multiplied by 2. In case of a CCS 1 is
970 * subtracted. This does not only differ CSS and Finished, it also
971 * maintains the order of the index (important for priority queues)
972 * and fits in the unsigned short variable.
974 return seq
* 2 - is_ccs
;
978 dtls1_retransmit_buffered_messages(SSL
*s
)
980 pqueue sent
= s
->d1
->sent_messages
;
986 iter
= pqueue_iterator(sent
);
988 for (item
= pqueue_next(&iter
); item
!= NULL
;
989 item
= pqueue_next(&iter
)) {
990 frag
= (hm_fragment
*)item
->data
;
991 if (dtls1_retransmit_message(s
,
992 (unsigned short)dtls1_get_queue_priority(
993 frag
->msg_header
.seq
, frag
->msg_header
.is_ccs
), 0,
994 &found
) <= 0 && found
) {
996 fprintf(stderr
, "dtls1_retransmit_message() failed\n");
1006 dtls1_buffer_message(SSL
*s
, int is_ccs
)
1010 unsigned char seq64be
[8];
1012 /* Buffer the messsage in order to handle DTLS retransmissions. */
1015 * This function is called immediately after a message has
1018 OPENSSL_assert(s
->internal
->init_off
== 0);
1020 frag
= dtls1_hm_fragment_new(s
->internal
->init_num
, 0);
1024 memcpy(frag
->fragment
, s
->internal
->init_buf
->data
, s
->internal
->init_num
);
1027 OPENSSL_assert(D1I(s
)->w_msg_hdr
.msg_len
+
1028 ((s
->version
== DTLS1_VERSION
) ?
1029 DTLS1_CCS_HEADER_LENGTH
: 3) == (unsigned int)s
->internal
->init_num
);
1031 OPENSSL_assert(D1I(s
)->w_msg_hdr
.msg_len
+
1032 DTLS1_HM_HEADER_LENGTH
== (unsigned int)s
->internal
->init_num
);
1035 frag
->msg_header
.msg_len
= D1I(s
)->w_msg_hdr
.msg_len
;
1036 frag
->msg_header
.seq
= D1I(s
)->w_msg_hdr
.seq
;
1037 frag
->msg_header
.type
= D1I(s
)->w_msg_hdr
.type
;
1038 frag
->msg_header
.frag_off
= 0;
1039 frag
->msg_header
.frag_len
= D1I(s
)->w_msg_hdr
.msg_len
;
1040 frag
->msg_header
.is_ccs
= is_ccs
;
1042 /* save current state*/
1043 frag
->msg_header
.saved_retransmit_state
.enc_write_ctx
= s
->internal
->enc_write_ctx
;
1044 frag
->msg_header
.saved_retransmit_state
.write_hash
= s
->internal
->write_hash
;
1045 frag
->msg_header
.saved_retransmit_state
.session
= s
->session
;
1046 frag
->msg_header
.saved_retransmit_state
.epoch
= D1I(s
)->w_epoch
;
1048 memset(seq64be
, 0, sizeof(seq64be
));
1049 seq64be
[6] = (unsigned char)(dtls1_get_queue_priority(
1050 frag
->msg_header
.seq
, frag
->msg_header
.is_ccs
) >> 8);
1051 seq64be
[7] = (unsigned char)(dtls1_get_queue_priority(
1052 frag
->msg_header
.seq
, frag
->msg_header
.is_ccs
));
1054 item
= pitem_new(seq64be
, frag
);
1056 dtls1_hm_fragment_free(frag
);
1060 pqueue_insert(s
->d1
->sent_messages
, item
);
1065 dtls1_retransmit_message(SSL
*s
, unsigned short seq
, unsigned long frag_off
,
1069 /* XDTLS: for now assuming that read/writes are blocking */
1072 unsigned long header_length
;
1073 unsigned char seq64be
[8];
1074 struct dtls1_retransmit_state saved_state
;
1075 unsigned char save_write_sequence
[8];
1078 OPENSSL_assert(s->internal->init_num == 0);
1079 OPENSSL_assert(s->internal->init_off == 0);
1082 /* XDTLS: the requested message ought to be found, otherwise error */
1083 memset(seq64be
, 0, sizeof(seq64be
));
1084 seq64be
[6] = (unsigned char)(seq
>> 8);
1085 seq64be
[7] = (unsigned char)seq
;
1087 item
= pqueue_find(s
->d1
->sent_messages
, seq64be
);
1090 fprintf(stderr
, "retransmit: message %d non-existant\n", seq
);
1097 frag
= (hm_fragment
*)item
->data
;
1099 if (frag
->msg_header
.is_ccs
)
1100 header_length
= DTLS1_CCS_HEADER_LENGTH
;
1102 header_length
= DTLS1_HM_HEADER_LENGTH
;
1104 memcpy(s
->internal
->init_buf
->data
, frag
->fragment
,
1105 frag
->msg_header
.msg_len
+ header_length
);
1106 s
->internal
->init_num
= frag
->msg_header
.msg_len
+ header_length
;
1108 dtls1_set_message_header_int(s
, frag
->msg_header
.type
,
1109 frag
->msg_header
.msg_len
, frag
->msg_header
.seq
, 0,
1110 frag
->msg_header
.frag_len
);
1112 /* save current state */
1113 saved_state
.enc_write_ctx
= s
->internal
->enc_write_ctx
;
1114 saved_state
.write_hash
= s
->internal
->write_hash
;
1115 saved_state
.session
= s
->session
;
1116 saved_state
.epoch
= D1I(s
)->w_epoch
;
1118 D1I(s
)->retransmitting
= 1;
1120 /* restore state in which the message was originally sent */
1121 s
->internal
->enc_write_ctx
= frag
->msg_header
.saved_retransmit_state
.enc_write_ctx
;
1122 s
->internal
->write_hash
= frag
->msg_header
.saved_retransmit_state
.write_hash
;
1123 s
->session
= frag
->msg_header
.saved_retransmit_state
.session
;
1124 D1I(s
)->w_epoch
= frag
->msg_header
.saved_retransmit_state
.epoch
;
1126 if (frag
->msg_header
.saved_retransmit_state
.epoch
==
1127 saved_state
.epoch
- 1) {
1128 memcpy(save_write_sequence
, S3I(s
)->write_sequence
,
1129 sizeof(S3I(s
)->write_sequence
));
1130 memcpy(S3I(s
)->write_sequence
, D1I(s
)->last_write_sequence
,
1131 sizeof(S3I(s
)->write_sequence
));
1134 ret
= dtls1_do_write(s
, frag
->msg_header
.is_ccs
?
1135 SSL3_RT_CHANGE_CIPHER_SPEC
: SSL3_RT_HANDSHAKE
);
1137 /* restore current state */
1138 s
->internal
->enc_write_ctx
= saved_state
.enc_write_ctx
;
1139 s
->internal
->write_hash
= saved_state
.write_hash
;
1140 s
->session
= saved_state
.session
;
1141 D1I(s
)->w_epoch
= saved_state
.epoch
;
1143 if (frag
->msg_header
.saved_retransmit_state
.epoch
==
1144 saved_state
.epoch
- 1) {
1145 memcpy(D1I(s
)->last_write_sequence
, S3I(s
)->write_sequence
,
1146 sizeof(S3I(s
)->write_sequence
));
1147 memcpy(S3I(s
)->write_sequence
, save_write_sequence
,
1148 sizeof(S3I(s
)->write_sequence
));
1151 D1I(s
)->retransmitting
= 0;
1153 (void)BIO_flush(SSL_get_wbio(s
));
1157 /* call this function when the buffered messages are no longer needed */
1159 dtls1_clear_record_buffer(SSL
*s
)
1163 for(item
= pqueue_pop(s
->d1
->sent_messages
); item
!= NULL
;
1164 item
= pqueue_pop(s
->d1
->sent_messages
)) {
1165 dtls1_hm_fragment_free((hm_fragment
*)item
->data
);
1171 dtls1_set_message_header(SSL
*s
, unsigned char mt
, unsigned long len
,
1172 unsigned long frag_off
, unsigned long frag_len
)
1174 /* Don't change sequence numbers while listening */
1175 if (frag_off
== 0 && !D1I(s
)->listen
) {
1176 D1I(s
)->handshake_write_seq
= D1I(s
)->next_handshake_write_seq
;
1177 D1I(s
)->next_handshake_write_seq
++;
1180 dtls1_set_message_header_int(s
, mt
, len
, D1I(s
)->handshake_write_seq
,
1181 frag_off
, frag_len
);
1184 /* don't actually do the writing, wait till the MTU has been retrieved */
1186 dtls1_set_message_header_int(SSL
*s
, unsigned char mt
, unsigned long len
,
1187 unsigned short seq_num
, unsigned long frag_off
, unsigned long frag_len
)
1189 struct hm_header_st
*msg_hdr
= &D1I(s
)->w_msg_hdr
;
1192 msg_hdr
->msg_len
= len
;
1193 msg_hdr
->seq
= seq_num
;
1194 msg_hdr
->frag_off
= frag_off
;
1195 msg_hdr
->frag_len
= frag_len
;
1199 dtls1_fix_message_header(SSL
*s
, unsigned long frag_off
, unsigned long frag_len
)
1201 struct hm_header_st
*msg_hdr
= &D1I(s
)->w_msg_hdr
;
1203 msg_hdr
->frag_off
= frag_off
;
1204 msg_hdr
->frag_len
= frag_len
;
1207 static unsigned char *
1208 dtls1_write_message_header(SSL
*s
, unsigned char *p
)
1210 struct hm_header_st
*msg_hdr
= &D1I(s
)->w_msg_hdr
;
1212 *p
++ = msg_hdr
->type
;
1213 l2n3(msg_hdr
->msg_len
, p
);
1215 s2n(msg_hdr
->seq
, p
);
1216 l2n3(msg_hdr
->frag_off
, p
);
1217 l2n3(msg_hdr
->frag_len
, p
);
1225 return (g_probable_mtu
[(sizeof(g_probable_mtu
) /
1226 sizeof(g_probable_mtu
[0])) - 1]);
1230 dtls1_guess_mtu(unsigned int curr_mtu
)
1235 return g_probable_mtu
[0];
1237 for (i
= 0; i
< sizeof(g_probable_mtu
) / sizeof(g_probable_mtu
[0]); i
++)
1238 if (curr_mtu
> g_probable_mtu
[i
])
1239 return g_probable_mtu
[i
];
1245 dtls1_get_message_header(unsigned char *data
, struct hm_header_st
*msg_hdr
)
1248 uint32_t msg_len
, frag_off
, frag_len
;
1252 CBS_init(&header
, data
, sizeof(*msg_hdr
));
1254 memset(msg_hdr
, 0, sizeof(*msg_hdr
));
1256 if (!CBS_get_u8(&header
, &type
))
1258 if (!CBS_get_u24(&header
, &msg_len
))
1260 if (!CBS_get_u16(&header
, &seq
))
1262 if (!CBS_get_u24(&header
, &frag_off
))
1264 if (!CBS_get_u24(&header
, &frag_len
))
1267 msg_hdr
->type
= type
;
1268 msg_hdr
->msg_len
= msg_len
;
1270 msg_hdr
->frag_off
= frag_off
;
1271 msg_hdr
->frag_len
= frag_len
;
1277 dtls1_get_ccs_header(unsigned char *data
, struct ccs_header_st
*ccs_hdr
)
1279 memset(ccs_hdr
, 0x00, sizeof(struct ccs_header_st
));
1281 ccs_hdr
->type
= *(data
++);
1285 dtls1_shutdown(SSL
*s
)
1289 ret
= ssl3_shutdown(s
);