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 <openssl/buffer.h>
16 #include <openssl/objects.h>
17 #include <openssl/evp.h>
18 #include <openssl/x509.h>
20 #define RSMBLY_BITMASK_SIZE(msg_len) (((msg_len) + 7) / 8)
22 #define RSMBLY_BITMASK_MARK(bitmask, start, end) { \
23 if ((end) - (start) <= 8) { \
25 for (ii = (start); ii < (end); ii++) bitmask[((ii) >> 3)] |= (1 << ((ii) & 7)); \
28 bitmask[((start) >> 3)] |= bitmask_start_values[((start) & 7)]; \
29 for (ii = (((start) >> 3) + 1); ii < ((((end) - 1)) >> 3); ii++) bitmask[ii] = 0xff; \
30 bitmask[(((end) - 1) >> 3)] |= bitmask_end_values[((end) & 7)]; \
33 #define RSMBLY_BITMASK_IS_COMPLETE(bitmask, msg_len, is_complete) { \
35 OPENSSL_assert((msg_len) > 0); \
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, unsigned long frag_off,
47 unsigned long frag_len);
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,
52 unsigned long frag_off,
53 unsigned long frag_len);
54 static int dtls_get_reassembled_message(SSL *s, long *len);
56 static hm_fragment *dtls1_hm_fragment_new(unsigned long frag_len,
59 hm_fragment *frag = NULL;
60 unsigned char *buf = NULL;
61 unsigned char *bitmask = NULL;
63 frag = OPENSSL_malloc(sizeof(*frag));
68 buf = OPENSSL_malloc(frag_len);
75 /* zero length fragment gets zero frag->fragment */
78 /* Initialize reassembly bitmask if necessary */
80 bitmask = OPENSSL_zalloc(RSMBLY_BITMASK_SIZE(frag_len));
81 if (bitmask == NULL) {
88 frag->reassembly = bitmask;
93 void dtls1_hm_fragment_free(hm_fragment *frag)
97 if (frag->msg_header.is_ccs) {
98 EVP_CIPHER_CTX_free(frag->msg_header.
99 saved_retransmit_state.enc_write_ctx);
100 EVP_MD_CTX_free(frag->msg_header.saved_retransmit_state.write_hash);
102 OPENSSL_free(frag->fragment);
103 OPENSSL_free(frag->reassembly);
108 * send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
109 * SSL3_RT_CHANGE_CIPHER_SPEC)
111 int dtls1_do_write(SSL *s, int type)
114 unsigned int curr_mtu;
116 unsigned int 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 OPENSSL_assert(s->init_num ==
127 s->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH);
131 && (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_write_ctx)) &
132 EVP_CIPH_FLAG_AEAD_CIPHER) != 0)
135 mac_size = EVP_MD_CTX_size(s->write_hash);
139 if (s->enc_write_ctx &&
140 (EVP_CIPHER_CTX_mode(s->enc_write_ctx) == EVP_CIPH_CBC_MODE))
141 blocksize = 2 * EVP_CIPHER_CTX_block_size(s->enc_write_ctx);
146 s->rwstate = SSL_NOTHING;
148 /* s->init_num shouldn't ever be < 0...but just in case */
149 while (s->init_num > 0) {
150 if (type == SSL3_RT_HANDSHAKE && s->init_off != 0) {
151 /* We must be writing a fragment other than the first one */
154 /* This is the first attempt at writing out this fragment */
156 if (s->init_off <= DTLS1_HM_HEADER_LENGTH) {
158 * Each fragment that was already sent must at least have
159 * contained the message header plus one other byte.
160 * Therefore |init_off| must have progressed by at least
161 * |DTLS1_HM_HEADER_LENGTH + 1| bytes. If not something went
168 * Adjust |init_off| and |init_num| to allow room for a new
169 * message header for this fragment.
171 s->init_off -= DTLS1_HM_HEADER_LENGTH;
172 s->init_num += DTLS1_HM_HEADER_LENGTH;
175 * We must have been called again after a retry so use the
176 * fragment offset from our last attempt. We do not need
177 * to adjust |init_off| and |init_num| as above, because
178 * that should already have been done before the retry.
180 frag_off = s->d1->w_msg_hdr.frag_off;
184 used_len = BIO_wpending(s->wbio) + DTLS1_RT_HEADER_LENGTH
185 + mac_size + blocksize;
186 if (s->d1->mtu > used_len)
187 curr_mtu = s->d1->mtu - used_len;
191 if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) {
193 * grr.. we could get an error if MTU picked was wrong
195 ret = BIO_flush(s->wbio);
197 s->rwstate = SSL_WRITING;
200 used_len = DTLS1_RT_HEADER_LENGTH + mac_size + blocksize;
201 if (s->d1->mtu > used_len + DTLS1_HM_HEADER_LENGTH) {
202 curr_mtu = s->d1->mtu - used_len;
204 /* Shouldn't happen */
210 * We just checked that s->init_num > 0 so this cast should be safe
212 if (((unsigned int)s->init_num) > curr_mtu)
217 /* Shouldn't ever happen */
222 * XDTLS: this function is too long. split out the CCS part
224 if (type == SSL3_RT_HANDSHAKE) {
225 if (len < DTLS1_HM_HEADER_LENGTH) {
227 * len is so small that we really can't do anything sensible
232 dtls1_fix_message_header(s, frag_off, len - DTLS1_HM_HEADER_LENGTH);
234 dtls1_write_message_header(s,
235 (unsigned char *)&s->init_buf->
239 ret = dtls1_write_bytes(s, type, &s->init_buf->data[s->init_off], len);
242 * might need to update MTU here, but we don't know which
243 * previous packet caused the failure -- so can't really
244 * retransmit anything. continue as if everything is fine and
245 * wait for an alert to handle the retransmit
247 if (retry && BIO_ctrl(SSL_get_wbio(s),
248 BIO_CTRL_DGRAM_MTU_EXCEEDED, 0, NULL) > 0) {
249 if (!(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
250 if (!dtls1_query_mtu(s))
252 /* Have one more go */
262 * bad if this assert fails, only part of the handshake message
263 * got sent. but why would this happen?
265 OPENSSL_assert(len == (unsigned int)ret);
267 if (type == SSL3_RT_HANDSHAKE && !s->d1->retransmitting) {
269 * should not be done for 'Hello Request's, but in that case
270 * we'll ignore the result anyway
273 (unsigned char *)&s->init_buf->data[s->init_off];
274 const struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
277 if (frag_off == 0 && s->version != DTLS1_BAD_VER) {
279 * reconstruct message header is if it is being sent in
282 *p++ = msg_hdr->type;
283 l2n3(msg_hdr->msg_len, p);
284 s2n(msg_hdr->seq, p);
286 l2n3(msg_hdr->msg_len, p);
287 p -= DTLS1_HM_HEADER_LENGTH;
290 p += DTLS1_HM_HEADER_LENGTH;
291 xlen = ret - DTLS1_HM_HEADER_LENGTH;
294 if (!ssl3_finish_mac(s, p, xlen))
298 if (ret == (int)s->init_num) {
300 s->msg_callback(1, s->version, type, s->init_buf->data,
301 (size_t)(s->init_off + s->init_num), s,
302 s->msg_callback_arg);
304 s->init_off = 0; /* done writing this message */
311 ret -= DTLS1_HM_HEADER_LENGTH;
315 * We save the fragment offset for the next fragment so we have it
316 * available in case of an IO retry. We don't know the length of the
317 * next fragment yet so just set that to 0 for now. It will be
318 * updated again later.
320 dtls1_fix_message_header(s, frag_off, 0);
326 int dtls_get_message(SSL *s, int *mt, size_t *len)
328 struct hm_header_st *msg_hdr;
330 unsigned long msg_len;
334 msg_hdr = &s->d1->r_msg_hdr;
335 memset(msg_hdr, 0, sizeof(*msg_hdr));
338 ok = dtls_get_reassembled_message(s, &tmplen);
339 if (tmplen == DTLS1_HM_BAD_FRAGMENT || tmplen == DTLS1_HM_FRAGMENT_RETRY) {
340 /* bad fragment received */
342 } else if (tmplen <= 0 && !ok) {
346 *mt = s->s3->tmp.message_type;
348 p = (unsigned char *)s->init_buf->data;
350 if (*mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
351 if (s->msg_callback) {
352 s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC,
353 p, 1, s, s->msg_callback_arg);
356 * This isn't a real handshake message so skip the processing below.
358 *len = (unsigned long)tmplen;
362 msg_len = msg_hdr->msg_len;
364 /* reconstruct message header */
365 *(p++) = msg_hdr->type;
367 s2n(msg_hdr->seq, p);
370 if (s->version != DTLS1_BAD_VER) {
371 p -= DTLS1_HM_HEADER_LENGTH;
372 msg_len += DTLS1_HM_HEADER_LENGTH;
375 if (!ssl3_finish_mac(s, p, msg_len))
378 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
379 p, msg_len, s, s->msg_callback_arg);
381 memset(msg_hdr, 0, sizeof(*msg_hdr));
383 s->d1->handshake_read_seq++;
385 s->init_msg = s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
392 * dtls1_max_handshake_message_len returns the maximum number of bytes
393 * permitted in a DTLS handshake message for |s|. The minimum is 16KB, but
394 * may be greater if the maximum certificate list size requires it.
396 static unsigned long dtls1_max_handshake_message_len(const SSL *s)
398 unsigned long max_len =
399 DTLS1_HM_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH;
400 if (max_len < (unsigned long)s->max_cert_list)
401 return s->max_cert_list;
405 static int dtls1_preprocess_fragment(SSL *s, struct hm_header_st *msg_hdr)
407 size_t frag_off, frag_len, msg_len;
409 msg_len = msg_hdr->msg_len;
410 frag_off = msg_hdr->frag_off;
411 frag_len = msg_hdr->frag_len;
413 /* sanity checking */
414 if ((frag_off + frag_len) > msg_len
415 || msg_len > dtls1_max_handshake_message_len(s)) {
416 SSLerr(SSL_F_DTLS1_PREPROCESS_FRAGMENT, SSL_R_EXCESSIVE_MESSAGE_SIZE);
417 return SSL_AD_ILLEGAL_PARAMETER;
420 if (s->d1->r_msg_hdr.frag_off == 0) { /* first fragment */
422 * msg_len is limited to 2^24, but is effectively checked against
423 * dtls_max_handshake_message_len(s) above
425 if (!BUF_MEM_grow_clean(s->init_buf, msg_len + DTLS1_HM_HEADER_LENGTH)) {
426 SSLerr(SSL_F_DTLS1_PREPROCESS_FRAGMENT, ERR_R_BUF_LIB);
427 return SSL_AD_INTERNAL_ERROR;
430 s->s3->tmp.message_size = msg_len;
431 s->d1->r_msg_hdr.msg_len = msg_len;
432 s->s3->tmp.message_type = msg_hdr->type;
433 s->d1->r_msg_hdr.type = msg_hdr->type;
434 s->d1->r_msg_hdr.seq = msg_hdr->seq;
435 } else if (msg_len != s->d1->r_msg_hdr.msg_len) {
437 * They must be playing with us! BTW, failure to enforce upper limit
438 * would open possibility for buffer overrun.
440 SSLerr(SSL_F_DTLS1_PREPROCESS_FRAGMENT, SSL_R_EXCESSIVE_MESSAGE_SIZE);
441 return SSL_AD_ILLEGAL_PARAMETER;
444 return 0; /* no error */
447 static int dtls1_retrieve_buffered_fragment(SSL *s, int *ok)
450 * (0) check whether the desired fragment is available
452 * (1) copy over the fragment to s->init_buf->data[]
453 * (2) update s->init_num
462 item = pqueue_peek(s->d1->buffered_messages);
466 frag = (hm_fragment *)item->data;
468 if (frag->msg_header.seq < s->d1->handshake_read_seq) {
469 /* This is a stale message that has been buffered so clear it */
470 pqueue_pop(s->d1->buffered_messages);
471 dtls1_hm_fragment_free(frag);
476 } while (item == NULL);
478 /* Don't return if reassembly still in progress */
479 if (frag->reassembly != NULL)
482 if (s->d1->handshake_read_seq == frag->msg_header.seq) {
483 unsigned long frag_len = frag->msg_header.frag_len;
484 pqueue_pop(s->d1->buffered_messages);
486 al = dtls1_preprocess_fragment(s, &frag->msg_header);
488 if (al == 0) { /* no alert */
490 (unsigned char *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
491 memcpy(&p[frag->msg_header.frag_off], frag->fragment,
492 frag->msg_header.frag_len);
495 dtls1_hm_fragment_free(frag);
503 ssl3_send_alert(s, SSL3_AL_FATAL, al);
512 dtls1_reassemble_fragment(SSL *s, const struct hm_header_st *msg_hdr, int *ok)
514 hm_fragment *frag = NULL;
516 int i = -1, is_complete;
517 unsigned char seq64be[8];
518 unsigned long frag_len = msg_hdr->frag_len;
521 if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len ||
522 msg_hdr->msg_len > dtls1_max_handshake_message_len(s))
526 return DTLS1_HM_FRAGMENT_RETRY;
528 /* Try to find item in queue */
529 memset(seq64be, 0, sizeof(seq64be));
530 seq64be[6] = (unsigned char)(msg_hdr->seq >> 8);
531 seq64be[7] = (unsigned char)msg_hdr->seq;
532 item = pqueue_find(s->d1->buffered_messages, seq64be);
535 frag = dtls1_hm_fragment_new(msg_hdr->msg_len, 1);
538 memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
539 frag->msg_header.frag_len = frag->msg_header.msg_len;
540 frag->msg_header.frag_off = 0;
542 frag = (hm_fragment *)item->data;
543 if (frag->msg_header.msg_len != msg_hdr->msg_len) {
551 * If message is already reassembled, this must be a retransmit and can
552 * be dropped. In this case item != NULL and so frag does not need to be
555 if (frag->reassembly == NULL) {
556 unsigned char devnull[256];
559 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
562 sizeof(devnull) ? sizeof(devnull) :
568 return DTLS1_HM_FRAGMENT_RETRY;
571 /* read the body of the fragment (header has already been read */
572 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
573 frag->fragment + msg_hdr->frag_off,
575 if (i <= 0 || read != frag_len)
580 RSMBLY_BITMASK_MARK(frag->reassembly, (long)msg_hdr->frag_off,
581 (long)(msg_hdr->frag_off + frag_len));
583 RSMBLY_BITMASK_IS_COMPLETE(frag->reassembly, (long)msg_hdr->msg_len,
587 OPENSSL_free(frag->reassembly);
588 frag->reassembly = NULL;
592 item = pitem_new(seq64be, frag);
598 item = pqueue_insert(s->d1->buffered_messages, item);
600 * pqueue_insert fails iff a duplicate item is inserted. However,
601 * |item| cannot be a duplicate. If it were, |pqueue_find|, above,
602 * would have returned it and control would never have reached this
605 OPENSSL_assert(item != NULL);
608 return DTLS1_HM_FRAGMENT_RETRY;
612 dtls1_hm_fragment_free(frag);
618 dtls1_process_out_of_seq_message(SSL *s, const struct hm_header_st *msg_hdr,
622 hm_fragment *frag = NULL;
624 unsigned char seq64be[8];
625 unsigned long frag_len = msg_hdr->frag_len;
628 if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len)
631 /* Try to find item in queue, to prevent duplicate entries */
632 memset(seq64be, 0, sizeof(seq64be));
633 seq64be[6] = (unsigned char)(msg_hdr->seq >> 8);
634 seq64be[7] = (unsigned char)msg_hdr->seq;
635 item = pqueue_find(s->d1->buffered_messages, seq64be);
638 * If we already have an entry and this one is a fragment, don't discard
639 * it and rather try to reassemble it.
641 if (item != NULL && frag_len != msg_hdr->msg_len)
645 * Discard the message if sequence number was already there, is too far
646 * in the future, already in the queue or if we received a FINISHED
647 * before the SERVER_HELLO, which then must be a stale retransmit.
649 if (msg_hdr->seq <= s->d1->handshake_read_seq ||
650 msg_hdr->seq > s->d1->handshake_read_seq + 10 || item != NULL ||
651 (s->d1->handshake_read_seq == 0 && msg_hdr->type == SSL3_MT_FINISHED)) {
652 unsigned char devnull[256];
655 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
658 sizeof(devnull) ? sizeof(devnull) :
665 if (frag_len != msg_hdr->msg_len)
666 return dtls1_reassemble_fragment(s, msg_hdr, ok);
668 if (frag_len > dtls1_max_handshake_message_len(s))
671 frag = dtls1_hm_fragment_new(frag_len, 0);
675 memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr));
679 * read the body of the fragment (header has already been read
681 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
682 frag->fragment, frag_len, 0, &read);
683 if (i<=0 || read != frag_len)
689 item = pitem_new(seq64be, frag);
693 item = pqueue_insert(s->d1->buffered_messages, item);
695 * pqueue_insert fails iff a duplicate item is inserted. However,
696 * |item| cannot be a duplicate. If it were, |pqueue_find|, above,
697 * would have returned it. Then, either |frag_len| !=
698 * |msg_hdr->msg_len| in which case |item| is set to NULL and it will
699 * have been processed with |dtls1_reassemble_fragment|, above, or
700 * the record will have been discarded.
702 OPENSSL_assert(item != NULL);
705 return DTLS1_HM_FRAGMENT_RETRY;
709 dtls1_hm_fragment_free(frag);
714 static int dtls_get_reassembled_message(SSL *s, long *len)
716 unsigned char wire[DTLS1_HM_HEADER_LENGTH];
717 unsigned long mlen, frag_off, frag_len;
718 int i, al, recvd_type;
719 struct hm_header_st msg_hdr;
724 /* see if we have the required fragment already */
725 if ((frag_len = dtls1_retrieve_buffered_fragment(s, &ok)) || ok) {
727 s->init_num = frag_len;
732 /* read handshake message header */
733 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &recvd_type, wire,
734 DTLS1_HM_HEADER_LENGTH, 0, &read);
735 if (i <= 0) { /* nbio, or an error */
736 s->rwstate = SSL_READING;
740 if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) {
741 if (wire[0] != SSL3_MT_CCS) {
742 al = SSL_AD_UNEXPECTED_MESSAGE;
743 SSLerr(SSL_F_DTLS_GET_REASSEMBLED_MESSAGE,
744 SSL_R_BAD_CHANGE_CIPHER_SPEC);
748 memcpy(s->init_buf->data, wire, read);
749 s->init_num = read - 1;
750 s->init_msg = s->init_buf->data + 1;
751 s->s3->tmp.message_type = SSL3_MT_CHANGE_CIPHER_SPEC;
752 s->s3->tmp.message_size = read - 1;
757 /* Handshake fails if message header is incomplete */
758 if (read != DTLS1_HM_HEADER_LENGTH) {
759 al = SSL_AD_UNEXPECTED_MESSAGE;
760 SSLerr(SSL_F_DTLS_GET_REASSEMBLED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
764 /* parse the message fragment header */
765 dtls1_get_message_header(wire, &msg_hdr);
767 mlen = msg_hdr.msg_len;
768 frag_off = msg_hdr.frag_off;
769 frag_len = msg_hdr.frag_len;
772 * We must have at least frag_len bytes left in the record to be read.
773 * Fragments must not span records.
775 if (frag_len > RECORD_LAYER_get_rrec_length(&s->rlayer)) {
776 al = SSL3_AD_ILLEGAL_PARAMETER;
777 SSLerr(SSL_F_DTLS_GET_REASSEMBLED_MESSAGE, SSL_R_BAD_LENGTH);
782 * if this is a future (or stale) message it gets buffered
783 * (or dropped)--no further processing at this time
784 * While listening, we accept seq 1 (ClientHello with cookie)
785 * although we're still expecting seq 0 (ClientHello)
787 if (msg_hdr.seq != s->d1->handshake_read_seq) {
788 *len = dtls1_process_out_of_seq_message(s, &msg_hdr, &ok);
792 if (frag_len && frag_len < mlen) {
793 *len = dtls1_reassemble_fragment(s, &msg_hdr, &ok);
797 if (!s->server && s->d1->r_msg_hdr.frag_off == 0 &&
798 wire[0] == SSL3_MT_HELLO_REQUEST) {
800 * The server may always send 'Hello Request' messages -- we are
801 * doing a handshake anyway now, so ignore them if their format is
802 * correct. Does not count for 'Finished' MAC.
804 if (wire[1] == 0 && wire[2] == 0 && wire[3] == 0) {
806 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
807 wire, DTLS1_HM_HEADER_LENGTH, s,
808 s->msg_callback_arg);
812 } else { /* Incorrectly formatted Hello request */
814 al = SSL_AD_UNEXPECTED_MESSAGE;
815 SSLerr(SSL_F_DTLS_GET_REASSEMBLED_MESSAGE,
816 SSL_R_UNEXPECTED_MESSAGE);
821 if ((al = dtls1_preprocess_fragment(s, &msg_hdr)))
826 (unsigned char *)s->init_buf->data + DTLS1_HM_HEADER_LENGTH;
828 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
829 &p[frag_off], frag_len, 0, &read);
832 * This shouldn't ever fail due to NBIO because we already checked
833 * that we have enough data in the record
836 s->rwstate = SSL_READING;
844 * XDTLS: an incorrectly formatted fragment should cause the handshake
847 if (read != frag_len) {
848 al = SSL3_AD_ILLEGAL_PARAMETER;
849 SSLerr(SSL_F_DTLS_GET_REASSEMBLED_MESSAGE, SSL3_AD_ILLEGAL_PARAMETER);
854 * Note that s->init_num is *not* used as current offset in
855 * s->init_buf->data, but as a counter summing up fragments' lengths: as
856 * soon as they sum up to handshake packet length, we assume we have got
859 *len = s->init_num = frag_len;
863 ssl3_send_alert(s, SSL3_AL_FATAL, al);
870 * for these 2 messages, we need to
871 * ssl->enc_read_ctx re-init
872 * ssl->rlayer.read_sequence zero
873 * ssl->s3->read_mac_secret re-init
874 * ssl->session->read_sym_enc assign
875 * ssl->session->read_compression assign
876 * ssl->session->read_hash assign
878 int dtls_construct_change_cipher_spec(SSL *s, WPACKET *pkt)
880 if (s->version == DTLS1_BAD_VER) {
881 s->d1->next_handshake_write_seq++;
883 if (!WPACKET_put_bytes_u16(pkt, s->d1->handshake_write_seq)) {
884 SSLerr(SSL_F_DTLS_CONSTRUCT_CHANGE_CIPHER_SPEC, ERR_R_INTERNAL_ERROR);
885 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
892 #ifndef OPENSSL_NO_SCTP
893 WORK_STATE dtls_wait_for_dry(SSL *s)
897 /* read app data until dry event */
898 ret = BIO_dgram_sctp_wait_for_dry(SSL_get_wbio(s));
903 s->s3->in_read_app_data = 2;
904 s->rwstate = SSL_READING;
905 BIO_clear_retry_flags(SSL_get_rbio(s));
906 BIO_set_retry_read(SSL_get_rbio(s));
909 return WORK_FINISHED_CONTINUE;
913 int dtls1_read_failed(SSL *s, int code)
916 SSLerr(SSL_F_DTLS1_READ_FAILED, ERR_R_INTERNAL_ERROR);
920 if (!dtls1_is_timer_expired(s)) {
922 * not a timeout, none of our business, let higher layers handle
923 * this. in fact it's probably an error
927 #ifndef OPENSSL_NO_HEARTBEATS
928 /* done, no need to send a retransmit */
929 if (!SSL_in_init(s) && !s->tlsext_hb_pending)
931 /* done, no need to send a retransmit */
935 BIO_set_flags(SSL_get_rbio(s), BIO_FLAGS_READ);
939 return dtls1_handle_timeout(s);
942 int dtls1_get_queue_priority(unsigned short seq, int is_ccs)
945 * The index of the retransmission queue actually is the message sequence
946 * number, since the queue only contains messages of a single handshake.
947 * However, the ChangeCipherSpec has no message sequence number and so
948 * using only the sequence will result in the CCS and Finished having the
949 * same index. To prevent this, the sequence number is multiplied by 2.
950 * In case of a CCS 1 is subtracted. This does not only differ CSS and
951 * Finished, it also maintains the order of the index (important for
952 * priority queues) and fits in the unsigned short variable.
954 return seq * 2 - is_ccs;
957 int dtls1_retransmit_buffered_messages(SSL *s)
959 pqueue *sent = s->d1->sent_messages;
965 iter = pqueue_iterator(sent);
967 for (item = pqueue_next(&iter); item != NULL; item = pqueue_next(&iter)) {
968 frag = (hm_fragment *)item->data;
969 if (dtls1_retransmit_message(s, (unsigned short)
970 dtls1_get_queue_priority
971 (frag->msg_header.seq,
972 frag->msg_header.is_ccs), &found) <= 0)
979 int dtls1_buffer_message(SSL *s, int is_ccs)
983 unsigned char seq64be[8];
986 * this function is called immediately after a message has been
989 OPENSSL_assert(s->init_off == 0);
991 frag = dtls1_hm_fragment_new(s->init_num, 0);
995 memcpy(frag->fragment, s->init_buf->data, s->init_num);
998 /* For DTLS1_BAD_VER the header length is non-standard */
999 OPENSSL_assert(s->d1->w_msg_hdr.msg_len +
1001 DTLS1_BAD_VER) ? 3 : DTLS1_CCS_HEADER_LENGTH)
1002 == (unsigned int)s->init_num);
1004 OPENSSL_assert(s->d1->w_msg_hdr.msg_len +
1005 DTLS1_HM_HEADER_LENGTH == (unsigned int)s->init_num);
1008 frag->msg_header.msg_len = s->d1->w_msg_hdr.msg_len;
1009 frag->msg_header.seq = s->d1->w_msg_hdr.seq;
1010 frag->msg_header.type = s->d1->w_msg_hdr.type;
1011 frag->msg_header.frag_off = 0;
1012 frag->msg_header.frag_len = s->d1->w_msg_hdr.msg_len;
1013 frag->msg_header.is_ccs = is_ccs;
1015 /* save current state */
1016 frag->msg_header.saved_retransmit_state.enc_write_ctx = s->enc_write_ctx;
1017 frag->msg_header.saved_retransmit_state.write_hash = s->write_hash;
1018 frag->msg_header.saved_retransmit_state.compress = s->compress;
1019 frag->msg_header.saved_retransmit_state.session = s->session;
1020 frag->msg_header.saved_retransmit_state.epoch =
1021 DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer);
1023 memset(seq64be, 0, sizeof(seq64be));
1026 char)(dtls1_get_queue_priority(frag->msg_header.seq,
1027 frag->msg_header.is_ccs) >> 8);
1030 char)(dtls1_get_queue_priority(frag->msg_header.seq,
1031 frag->msg_header.is_ccs));
1033 item = pitem_new(seq64be, frag);
1035 dtls1_hm_fragment_free(frag);
1039 pqueue_insert(s->d1->sent_messages, item);
1043 int dtls1_retransmit_message(SSL *s, unsigned short seq, int *found)
1046 /* XDTLS: for now assuming that read/writes are blocking */
1049 unsigned long header_length;
1050 unsigned char seq64be[8];
1051 struct dtls1_retransmit_state saved_state;
1054 OPENSSL_assert(s->init_num == 0);
1055 OPENSSL_assert(s->init_off == 0);
1058 /* XDTLS: the requested message ought to be found, otherwise error */
1059 memset(seq64be, 0, sizeof(seq64be));
1060 seq64be[6] = (unsigned char)(seq >> 8);
1061 seq64be[7] = (unsigned char)seq;
1063 item = pqueue_find(s->d1->sent_messages, seq64be);
1065 SSLerr(SSL_F_DTLS1_RETRANSMIT_MESSAGE, ERR_R_INTERNAL_ERROR);
1071 frag = (hm_fragment *)item->data;
1073 if (frag->msg_header.is_ccs)
1074 header_length = DTLS1_CCS_HEADER_LENGTH;
1076 header_length = DTLS1_HM_HEADER_LENGTH;
1078 memcpy(s->init_buf->data, frag->fragment,
1079 frag->msg_header.msg_len + header_length);
1080 s->init_num = frag->msg_header.msg_len + header_length;
1082 dtls1_set_message_header_int(s, frag->msg_header.type,
1083 frag->msg_header.msg_len,
1084 frag->msg_header.seq, 0,
1085 frag->msg_header.frag_len);
1087 /* save current state */
1088 saved_state.enc_write_ctx = s->enc_write_ctx;
1089 saved_state.write_hash = s->write_hash;
1090 saved_state.compress = s->compress;
1091 saved_state.session = s->session;
1092 saved_state.epoch = DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer);
1094 s->d1->retransmitting = 1;
1096 /* restore state in which the message was originally sent */
1097 s->enc_write_ctx = frag->msg_header.saved_retransmit_state.enc_write_ctx;
1098 s->write_hash = frag->msg_header.saved_retransmit_state.write_hash;
1099 s->compress = frag->msg_header.saved_retransmit_state.compress;
1100 s->session = frag->msg_header.saved_retransmit_state.session;
1101 DTLS_RECORD_LAYER_set_saved_w_epoch(&s->rlayer,
1103 saved_retransmit_state.epoch);
1105 ret = dtls1_do_write(s, frag->msg_header.is_ccs ?
1106 SSL3_RT_CHANGE_CIPHER_SPEC : SSL3_RT_HANDSHAKE);
1108 /* restore current state */
1109 s->enc_write_ctx = saved_state.enc_write_ctx;
1110 s->write_hash = saved_state.write_hash;
1111 s->compress = saved_state.compress;
1112 s->session = saved_state.session;
1113 DTLS_RECORD_LAYER_set_saved_w_epoch(&s->rlayer, saved_state.epoch);
1115 s->d1->retransmitting = 0;
1117 (void)BIO_flush(s->wbio);
1121 void dtls1_set_message_header(SSL *s,
1122 unsigned char mt, unsigned long len,
1123 unsigned long frag_off, unsigned long frag_len)
1125 if (frag_off == 0) {
1126 s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
1127 s->d1->next_handshake_write_seq++;
1130 dtls1_set_message_header_int(s, mt, len, s->d1->handshake_write_seq,
1131 frag_off, frag_len);
1134 /* don't actually do the writing, wait till the MTU has been retrieved */
1136 dtls1_set_message_header_int(SSL *s, unsigned char mt,
1137 unsigned long len, unsigned short seq_num,
1138 unsigned long frag_off, unsigned long frag_len)
1140 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
1143 msg_hdr->msg_len = len;
1144 msg_hdr->seq = seq_num;
1145 msg_hdr->frag_off = frag_off;
1146 msg_hdr->frag_len = frag_len;
1150 dtls1_fix_message_header(SSL *s, unsigned long frag_off, unsigned long frag_len)
1152 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
1154 msg_hdr->frag_off = frag_off;
1155 msg_hdr->frag_len = frag_len;
1158 static unsigned char *dtls1_write_message_header(SSL *s, unsigned char *p)
1160 struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr;
1162 *p++ = msg_hdr->type;
1163 l2n3(msg_hdr->msg_len, p);
1165 s2n(msg_hdr->seq, p);
1166 l2n3(msg_hdr->frag_off, p);
1167 l2n3(msg_hdr->frag_len, p);
1172 void dtls1_get_message_header(unsigned char *data, struct hm_header_st *msg_hdr)
1174 memset(msg_hdr, 0, sizeof(*msg_hdr));
1175 msg_hdr->type = *(data++);
1176 n2l3(data, msg_hdr->msg_len);
1178 n2s(data, msg_hdr->seq);
1179 n2l3(data, msg_hdr->frag_off);
1180 n2l3(data, msg_hdr->frag_len);
1183 int dtls1_set_handshake_header(SSL *s, WPACKET *pkt, int htype)
1185 unsigned char *header;
1187 if (htype == SSL3_MT_CHANGE_CIPHER_SPEC) {
1188 s->d1->handshake_write_seq = s->d1->next_handshake_write_seq;
1189 dtls1_set_message_header_int(s, SSL3_MT_CCS, 0,
1190 s->d1->handshake_write_seq, 0, 0);
1191 if (!WPACKET_put_bytes_u8(pkt, SSL3_MT_CCS))
1194 dtls1_set_message_header(s, htype, 0, 0, 0);
1196 * We allocate space at the start for the message header. This gets
1199 if (!WPACKET_allocate_bytes(pkt, DTLS1_HM_HEADER_LENGTH, &header)
1200 || !WPACKET_start_sub_packet(pkt))
1207 int dtls1_close_construct_packet(SSL *s, WPACKET *pkt, int htype)
1211 if ((htype != SSL3_MT_CHANGE_CIPHER_SPEC && !WPACKET_close(pkt))
1212 || !WPACKET_get_length(pkt, &msglen)
1213 || msglen > INT_MAX)
1216 if (htype != SSL3_MT_CHANGE_CIPHER_SPEC) {
1217 s->d1->w_msg_hdr.msg_len = msglen - DTLS1_HM_HEADER_LENGTH;
1218 s->d1->w_msg_hdr.frag_len = msglen - DTLS1_HM_HEADER_LENGTH;
1220 s->init_num = (int)msglen;
1223 if (htype != DTLS1_MT_HELLO_VERIFY_REQUEST) {
1224 /* Buffer the message to handle re-xmits */
1225 if (!dtls1_buffer_message(s, htype == SSL3_MT_CHANGE_CIPHER_SPEC