1 /* $OpenBSD: d1_both.c,v 1.66 2021/01/26 14:22:19 jsing Exp $ */ 2 /* 3 * DTLS implementation written by Nagendra Modadugu 4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. 5 */ 6 /* ==================================================================== 7 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 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 19 * distribution. 20 * 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/)" 25 * 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. 30 * 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. 34 * 35 * 6. Redistributions of any form whatsoever must retain the following 36 * acknowledgment: 37 * "This product includes software developed by the OpenSSL Project 38 * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 39 * 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 * ==================================================================== 53 * 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). 57 * 58 */ 59 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 60 * All rights reserved. 61 * 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. 65 * 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). 72 * 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. 79 * 80 * Redistribution and use in source and binary forms, with or without 81 * modification, are permitted provided that the following conditions 82 * are met: 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)" 97 * 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 108 * SUCH DAMAGE. 109 * 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.] 114 */ 115 116 #include <limits.h> 117 #include <stdio.h> 118 #include <string.h> 119 120 #include "ssl_locl.h" 121 122 #include <openssl/buffer.h> 123 #include <openssl/evp.h> 124 #include <openssl/objects.h> 125 #include <openssl/x509.h> 126 127 #include "pqueue.h" 128 #include "bytestring.h" 129 130 #define RSMBLY_BITMASK_SIZE(msg_len) (((msg_len) + 7) / 8) 131 132 #define RSMBLY_BITMASK_MARK(bitmask, start, end) { \ 133 if ((end) - (start) <= 8) { \ 134 long ii; \ 135 for (ii = (start); ii < (end); ii++) bitmask[((ii) >> 3)] |= (1 << ((ii) & 7)); \ 136 } else { \ 137 long ii; \ 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)]; \ 141 } } 142 143 #define RSMBLY_BITMASK_IS_COMPLETE(bitmask, msg_len, is_complete) { \ 144 long ii; \ 145 OPENSSL_assert((msg_len) > 0); \ 146 is_complete = 1; \ 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; } } 150 151 static const unsigned char bitmask_start_values[] = { 152 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80 153 }; 154 static const unsigned char bitmask_end_values[] = { 155 0xff, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f 156 }; 157 158 /* XDTLS: figure out the right values */ 159 static const unsigned int g_probable_mtu[] = {1500 - 28, 512 - 28, 256 - 28}; 160 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 int dtls1_write_message_header(const struct hm_header_st *msg_hdr, 165 unsigned long frag_off, unsigned long frag_len, unsigned char *p); 166 static long dtls1_get_message_fragment(SSL *s, int st1, int stn, long max, 167 int *ok); 168 169 void dtls1_hm_fragment_free(hm_fragment *frag); 170 171 static hm_fragment * 172 dtls1_hm_fragment_new(unsigned long frag_len, int reassembly) 173 { 174 hm_fragment *frag; 175 176 if ((frag = calloc(1, sizeof(*frag))) == NULL) 177 goto err; 178 179 if (frag_len > 0) { 180 if ((frag->fragment = calloc(1, frag_len)) == NULL) 181 goto err; 182 } 183 184 /* Initialize reassembly bitmask if necessary. */ 185 if (reassembly) { 186 if ((frag->reassembly = calloc(1, 187 RSMBLY_BITMASK_SIZE(frag_len))) == NULL) 188 goto err; 189 } 190 191 return frag; 192 193 err: 194 dtls1_hm_fragment_free(frag); 195 return NULL; 196 } 197 198 void 199 dtls1_hm_fragment_free(hm_fragment *frag) 200 { 201 if (frag == NULL) 202 return; 203 204 if (frag->msg_header.is_ccs) { 205 EVP_CIPHER_CTX_free( 206 frag->msg_header.saved_retransmit_state.enc_write_ctx); 207 EVP_MD_CTX_free( 208 frag->msg_header.saved_retransmit_state.write_hash); 209 } 210 free(frag->fragment); 211 free(frag->reassembly); 212 free(frag); 213 } 214 215 /* send s->internal->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or SSL3_RT_CHANGE_CIPHER_SPEC) */ 216 int 217 dtls1_do_write(SSL *s, int type) 218 { 219 int ret; 220 int curr_mtu; 221 unsigned int len, frag_off; 222 size_t overhead; 223 224 /* AHA! Figure out the MTU, and stick to the right size */ 225 if (D1I(s)->mtu < dtls1_min_mtu() && 226 !(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) { 227 D1I(s)->mtu = BIO_ctrl(SSL_get_wbio(s), 228 BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL); 229 230 /* 231 * I've seen the kernel return bogus numbers when it 232 * doesn't know the MTU (ie., the initial write), so just 233 * make sure we have a reasonable number 234 */ 235 if (D1I(s)->mtu < dtls1_min_mtu()) { 236 D1I(s)->mtu = 0; 237 D1I(s)->mtu = dtls1_guess_mtu(D1I(s)->mtu); 238 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SET_MTU, 239 D1I(s)->mtu, NULL); 240 } 241 } 242 243 OPENSSL_assert(D1I(s)->mtu >= dtls1_min_mtu()); 244 /* should have something reasonable now */ 245 246 if (s->internal->init_off == 0 && type == SSL3_RT_HANDSHAKE) 247 OPENSSL_assert(s->internal->init_num == 248 (int)D1I(s)->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH); 249 250 if (!tls12_record_layer_write_overhead(s->internal->rl, &overhead)) 251 return -1; 252 253 frag_off = 0; 254 while (s->internal->init_num) { 255 curr_mtu = D1I(s)->mtu - BIO_wpending(SSL_get_wbio(s)) - 256 DTLS1_RT_HEADER_LENGTH - overhead; 257 258 if (curr_mtu <= DTLS1_HM_HEADER_LENGTH) { 259 /* grr.. we could get an error if MTU picked was wrong */ 260 ret = BIO_flush(SSL_get_wbio(s)); 261 if (ret <= 0) 262 return ret; 263 curr_mtu = D1I(s)->mtu - DTLS1_RT_HEADER_LENGTH - 264 overhead; 265 } 266 267 if (s->internal->init_num > curr_mtu) 268 len = curr_mtu; 269 else 270 len = s->internal->init_num; 271 272 /* XDTLS: this function is too long. split out the CCS part */ 273 if (type == SSL3_RT_HANDSHAKE) { 274 if (s->internal->init_off != 0) { 275 OPENSSL_assert(s->internal->init_off > DTLS1_HM_HEADER_LENGTH); 276 s->internal->init_off -= DTLS1_HM_HEADER_LENGTH; 277 s->internal->init_num += DTLS1_HM_HEADER_LENGTH; 278 279 if (s->internal->init_num > curr_mtu) 280 len = curr_mtu; 281 else 282 len = s->internal->init_num; 283 } 284 285 dtls1_fix_message_header(s, frag_off, 286 len - DTLS1_HM_HEADER_LENGTH); 287 288 if (!dtls1_write_message_header(&D1I(s)->w_msg_hdr, 289 D1I(s)->w_msg_hdr.frag_off, D1I(s)->w_msg_hdr.frag_len, 290 (unsigned char *)&s->internal->init_buf->data[s->internal->init_off])) 291 return -1; 292 293 OPENSSL_assert(len >= DTLS1_HM_HEADER_LENGTH); 294 } 295 296 ret = dtls1_write_bytes(s, type, 297 &s->internal->init_buf->data[s->internal->init_off], len); 298 if (ret < 0) { 299 /* 300 * Might need to update MTU here, but we don't know 301 * which previous packet caused the failure -- so 302 * can't really retransmit anything. continue as 303 * if everything is fine and wait for an alert to 304 * handle the retransmit 305 */ 306 if (BIO_ctrl(SSL_get_wbio(s), 307 BIO_CTRL_DGRAM_MTU_EXCEEDED, 0, NULL) > 0) 308 D1I(s)->mtu = BIO_ctrl(SSL_get_wbio(s), 309 BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL); 310 else 311 return (-1); 312 } else { 313 314 /* 315 * Bad if this assert fails, only part of the 316 * handshake message got sent. but why would 317 * this happen? 318 */ 319 OPENSSL_assert(len == (unsigned int)ret); 320 321 if (type == SSL3_RT_HANDSHAKE && 322 !D1I(s)->retransmitting) { 323 /* 324 * Should not be done for 'Hello Request's, 325 * but in that case we'll ignore the result 326 * anyway 327 */ 328 unsigned char *p = (unsigned char *)&s->internal->init_buf->data[s->internal->init_off]; 329 const struct hm_header_st *msg_hdr = &D1I(s)->w_msg_hdr; 330 int xlen; 331 332 if (frag_off == 0) { 333 /* 334 * Reconstruct message header is if it 335 * is being sent in single fragment 336 */ 337 if (!dtls1_write_message_header(msg_hdr, 338 0, msg_hdr->msg_len, p)) 339 return (-1); 340 xlen = ret; 341 } else { 342 p += DTLS1_HM_HEADER_LENGTH; 343 xlen = ret - DTLS1_HM_HEADER_LENGTH; 344 } 345 346 tls1_transcript_record(s, p, xlen); 347 } 348 349 if (ret == s->internal->init_num) { 350 if (s->internal->msg_callback) 351 s->internal->msg_callback(1, s->version, type, 352 s->internal->init_buf->data, 353 (size_t)(s->internal->init_off + s->internal->init_num), 354 s, s->internal->msg_callback_arg); 355 356 s->internal->init_off = 0; 357 /* done writing this message */ 358 s->internal->init_num = 0; 359 360 return (1); 361 } 362 s->internal->init_off += ret; 363 s->internal->init_num -= ret; 364 frag_off += (ret -= DTLS1_HM_HEADER_LENGTH); 365 } 366 } 367 return (0); 368 } 369 370 371 /* 372 * Obtain handshake message of message type 'mt' (any if mt == -1), 373 * maximum acceptable body length 'max'. 374 * Read an entire handshake message. Handshake messages arrive in 375 * fragments. 376 */ 377 long 378 dtls1_get_message(SSL *s, int st1, int stn, int mt, long max, int *ok) 379 { 380 int i, al; 381 struct hm_header_st *msg_hdr; 382 unsigned char *p; 383 unsigned long msg_len; 384 385 /* 386 * s3->internal->tmp is used to store messages that are unexpected, caused 387 * by the absence of an optional handshake message 388 */ 389 if (S3I(s)->tmp.reuse_message) { 390 S3I(s)->tmp.reuse_message = 0; 391 if ((mt >= 0) && (S3I(s)->tmp.message_type != mt)) { 392 al = SSL_AD_UNEXPECTED_MESSAGE; 393 SSLerror(s, SSL_R_UNEXPECTED_MESSAGE); 394 goto f_err; 395 } 396 *ok = 1; 397 s->internal->init_msg = s->internal->init_buf->data + DTLS1_HM_HEADER_LENGTH; 398 s->internal->init_num = (int)S3I(s)->tmp.message_size; 399 return s->internal->init_num; 400 } 401 402 msg_hdr = &D1I(s)->r_msg_hdr; 403 memset(msg_hdr, 0, sizeof(struct hm_header_st)); 404 405 again: 406 i = dtls1_get_message_fragment(s, st1, stn, max, ok); 407 if (i == DTLS1_HM_BAD_FRAGMENT || 408 i == DTLS1_HM_FRAGMENT_RETRY) /* bad fragment received */ 409 goto again; 410 else if (i <= 0 && !*ok) 411 return i; 412 413 p = (unsigned char *)s->internal->init_buf->data; 414 msg_len = msg_hdr->msg_len; 415 416 /* reconstruct message header */ 417 if (!dtls1_write_message_header(msg_hdr, 0, msg_len, p)) 418 return -1; 419 420 msg_len += DTLS1_HM_HEADER_LENGTH; 421 422 tls1_transcript_record(s, p, msg_len); 423 if (s->internal->msg_callback) 424 s->internal->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, p, msg_len, 425 s, s->internal->msg_callback_arg); 426 427 memset(msg_hdr, 0, sizeof(struct hm_header_st)); 428 429 /* Don't change sequence numbers while listening */ 430 if (!D1I(s)->listen) 431 D1I(s)->handshake_read_seq++; 432 433 s->internal->init_msg = s->internal->init_buf->data + DTLS1_HM_HEADER_LENGTH; 434 return s->internal->init_num; 435 436 f_err: 437 ssl3_send_alert(s, SSL3_AL_FATAL, al); 438 *ok = 0; 439 return -1; 440 } 441 442 443 static int 444 dtls1_preprocess_fragment(SSL *s, struct hm_header_st *msg_hdr, int max) 445 { 446 size_t frag_off, frag_len, msg_len; 447 448 msg_len = msg_hdr->msg_len; 449 frag_off = msg_hdr->frag_off; 450 frag_len = msg_hdr->frag_len; 451 452 /* sanity checking */ 453 if ((frag_off + frag_len) > msg_len) { 454 SSLerror(s, SSL_R_EXCESSIVE_MESSAGE_SIZE); 455 return SSL_AD_ILLEGAL_PARAMETER; 456 } 457 458 if ((frag_off + frag_len) > (unsigned long)max) { 459 SSLerror(s, SSL_R_EXCESSIVE_MESSAGE_SIZE); 460 return SSL_AD_ILLEGAL_PARAMETER; 461 } 462 463 if ( D1I(s)->r_msg_hdr.frag_off == 0) /* first fragment */ 464 { 465 /* 466 * msg_len is limited to 2^24, but is effectively checked 467 * against max above 468 */ 469 if (!BUF_MEM_grow_clean(s->internal->init_buf, 470 msg_len + DTLS1_HM_HEADER_LENGTH)) { 471 SSLerror(s, ERR_R_BUF_LIB); 472 return SSL_AD_INTERNAL_ERROR; 473 } 474 475 S3I(s)->tmp.message_size = msg_len; 476 D1I(s)->r_msg_hdr.msg_len = msg_len; 477 S3I(s)->tmp.message_type = msg_hdr->type; 478 D1I(s)->r_msg_hdr.type = msg_hdr->type; 479 D1I(s)->r_msg_hdr.seq = msg_hdr->seq; 480 } else if (msg_len != D1I(s)->r_msg_hdr.msg_len) { 481 /* 482 * They must be playing with us! BTW, failure to enforce 483 * upper limit would open possibility for buffer overrun. 484 */ 485 SSLerror(s, SSL_R_EXCESSIVE_MESSAGE_SIZE); 486 return SSL_AD_ILLEGAL_PARAMETER; 487 } 488 489 return 0; /* no error */ 490 } 491 492 static int 493 dtls1_retrieve_buffered_fragment(SSL *s, long max, int *ok) 494 { 495 /* 496 * (0) check whether the desired fragment is available 497 * if so: 498 * (1) copy over the fragment to s->internal->init_buf->data[] 499 * (2) update s->internal->init_num 500 */ 501 pitem *item; 502 hm_fragment *frag; 503 int al; 504 505 *ok = 0; 506 item = pqueue_peek(D1I(s)->buffered_messages); 507 if (item == NULL) 508 return 0; 509 510 frag = (hm_fragment *)item->data; 511 512 /* Don't return if reassembly still in progress */ 513 if (frag->reassembly != NULL) 514 return 0; 515 516 if (D1I(s)->handshake_read_seq == frag->msg_header.seq) { 517 unsigned long frag_len = frag->msg_header.frag_len; 518 pqueue_pop(D1I(s)->buffered_messages); 519 520 al = dtls1_preprocess_fragment(s, &frag->msg_header, max); 521 522 if (al == 0) /* no alert */ 523 { 524 unsigned char *p = (unsigned char *)s->internal->init_buf->data + DTLS1_HM_HEADER_LENGTH; 525 memcpy(&p[frag->msg_header.frag_off], 526 frag->fragment, frag->msg_header.frag_len); 527 } 528 529 dtls1_hm_fragment_free(frag); 530 pitem_free(item); 531 532 if (al == 0) { 533 *ok = 1; 534 return frag_len; 535 } 536 537 ssl3_send_alert(s, SSL3_AL_FATAL, al); 538 s->internal->init_num = 0; 539 *ok = 0; 540 return -1; 541 } else 542 return 0; 543 } 544 545 /* 546 * dtls1_max_handshake_message_len returns the maximum number of bytes 547 * permitted in a DTLS handshake message for |s|. The minimum is 16KB, 548 * but may be greater if the maximum certificate list size requires it. 549 */ 550 static unsigned long 551 dtls1_max_handshake_message_len(const SSL *s) 552 { 553 unsigned long max_len; 554 555 max_len = DTLS1_HM_HEADER_LENGTH + SSL3_RT_MAX_ENCRYPTED_LENGTH; 556 if (max_len < (unsigned long)s->internal->max_cert_list) 557 return s->internal->max_cert_list; 558 return max_len; 559 } 560 561 static int 562 dtls1_reassemble_fragment(SSL *s, struct hm_header_st* msg_hdr, int *ok) 563 { 564 hm_fragment *frag = NULL; 565 pitem *item = NULL; 566 int i = -1, is_complete; 567 unsigned char seq64be[8]; 568 unsigned long frag_len = msg_hdr->frag_len; 569 570 if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len || 571 msg_hdr->msg_len > dtls1_max_handshake_message_len(s)) 572 goto err; 573 574 if (frag_len == 0) { 575 i = DTLS1_HM_FRAGMENT_RETRY; 576 goto err; 577 } 578 579 /* Try to find item in queue */ 580 memset(seq64be, 0, sizeof(seq64be)); 581 seq64be[6] = (unsigned char)(msg_hdr->seq >> 8); 582 seq64be[7] = (unsigned char)msg_hdr->seq; 583 item = pqueue_find(D1I(s)->buffered_messages, seq64be); 584 585 if (item == NULL) { 586 frag = dtls1_hm_fragment_new(msg_hdr->msg_len, 1); 587 if (frag == NULL) 588 goto err; 589 memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr)); 590 frag->msg_header.frag_len = frag->msg_header.msg_len; 591 frag->msg_header.frag_off = 0; 592 } else { 593 frag = (hm_fragment*)item->data; 594 if (frag->msg_header.msg_len != msg_hdr->msg_len) { 595 item = NULL; 596 frag = NULL; 597 goto err; 598 } 599 } 600 601 /* 602 * If message is already reassembled, this must be a 603 * retransmit and can be dropped. 604 */ 605 if (frag->reassembly == NULL) { 606 unsigned char devnull [256]; 607 608 while (frag_len) { 609 i = s->method->internal->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, 610 devnull, frag_len > sizeof(devnull) ? 611 sizeof(devnull) : frag_len, 0); 612 if (i <= 0) 613 goto err; 614 frag_len -= i; 615 } 616 i = DTLS1_HM_FRAGMENT_RETRY; 617 goto err; 618 } 619 620 /* read the body of the fragment (header has already been read */ 621 i = s->method->internal->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, 622 frag->fragment + msg_hdr->frag_off, frag_len, 0); 623 if (i <= 0 || (unsigned long)i != frag_len) 624 goto err; 625 626 RSMBLY_BITMASK_MARK(frag->reassembly, (long)msg_hdr->frag_off, 627 (long)(msg_hdr->frag_off + frag_len)); 628 629 RSMBLY_BITMASK_IS_COMPLETE(frag->reassembly, (long)msg_hdr->msg_len, 630 is_complete); 631 632 if (is_complete) { 633 free(frag->reassembly); 634 frag->reassembly = NULL; 635 } 636 637 if (item == NULL) { 638 memset(seq64be, 0, sizeof(seq64be)); 639 seq64be[6] = (unsigned char)(msg_hdr->seq >> 8); 640 seq64be[7] = (unsigned char)(msg_hdr->seq); 641 642 item = pitem_new(seq64be, frag); 643 if (item == NULL) { 644 i = -1; 645 goto err; 646 } 647 648 pqueue_insert(D1I(s)->buffered_messages, item); 649 } 650 651 return DTLS1_HM_FRAGMENT_RETRY; 652 653 err: 654 if (item == NULL && frag != NULL) 655 dtls1_hm_fragment_free(frag); 656 *ok = 0; 657 return i; 658 } 659 660 661 static int 662 dtls1_process_out_of_seq_message(SSL *s, struct hm_header_st* msg_hdr, int *ok) 663 { 664 int i = -1; 665 hm_fragment *frag = NULL; 666 pitem *item = NULL; 667 unsigned char seq64be[8]; 668 unsigned long frag_len = msg_hdr->frag_len; 669 670 if ((msg_hdr->frag_off + frag_len) > msg_hdr->msg_len) 671 goto err; 672 673 /* Try to find item in queue, to prevent duplicate entries */ 674 memset(seq64be, 0, sizeof(seq64be)); 675 seq64be[6] = (unsigned char) (msg_hdr->seq >> 8); 676 seq64be[7] = (unsigned char) msg_hdr->seq; 677 item = pqueue_find(D1I(s)->buffered_messages, seq64be); 678 679 /* 680 * If we already have an entry and this one is a fragment, 681 * don't discard it and rather try to reassemble it. 682 */ 683 if (item != NULL && frag_len < msg_hdr->msg_len) 684 item = NULL; 685 686 /* 687 * Discard the message if sequence number was already there, is 688 * too far in the future, already in the queue or if we received 689 * a FINISHED before the SERVER_HELLO, which then must be a stale 690 * retransmit. 691 */ 692 if (msg_hdr->seq <= D1I(s)->handshake_read_seq || 693 msg_hdr->seq > D1I(s)->handshake_read_seq + 10 || item != NULL || 694 (D1I(s)->handshake_read_seq == 0 && 695 msg_hdr->type == SSL3_MT_FINISHED)) { 696 unsigned char devnull [256]; 697 698 while (frag_len) { 699 i = s->method->internal->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, 700 devnull, frag_len > sizeof(devnull) ? 701 sizeof(devnull) : frag_len, 0); 702 if (i <= 0) 703 goto err; 704 frag_len -= i; 705 } 706 } else { 707 if (frag_len < msg_hdr->msg_len) 708 return dtls1_reassemble_fragment(s, msg_hdr, ok); 709 710 if (frag_len > dtls1_max_handshake_message_len(s)) 711 goto err; 712 713 frag = dtls1_hm_fragment_new(frag_len, 0); 714 if (frag == NULL) 715 goto err; 716 717 memcpy(&(frag->msg_header), msg_hdr, sizeof(*msg_hdr)); 718 719 if (frag_len) { 720 /* read the body of the fragment (header has already been read */ 721 i = s->method->internal->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, 722 frag->fragment, frag_len, 0); 723 if (i <= 0 || (unsigned long)i != frag_len) 724 goto err; 725 } 726 727 memset(seq64be, 0, sizeof(seq64be)); 728 seq64be[6] = (unsigned char)(msg_hdr->seq >> 8); 729 seq64be[7] = (unsigned char)(msg_hdr->seq); 730 731 item = pitem_new(seq64be, frag); 732 if (item == NULL) 733 goto err; 734 735 pqueue_insert(D1I(s)->buffered_messages, item); 736 } 737 738 return DTLS1_HM_FRAGMENT_RETRY; 739 740 err: 741 if (item == NULL && frag != NULL) 742 dtls1_hm_fragment_free(frag); 743 *ok = 0; 744 return i; 745 } 746 747 748 static long 749 dtls1_get_message_fragment(SSL *s, int st1, int stn, long max, int *ok) 750 { 751 unsigned char wire[DTLS1_HM_HEADER_LENGTH]; 752 unsigned long len, frag_off, frag_len; 753 int i, al; 754 struct hm_header_st msg_hdr; 755 756 again: 757 /* see if we have the required fragment already */ 758 if ((frag_len = dtls1_retrieve_buffered_fragment(s, max, ok)) || *ok) { 759 if (*ok) 760 s->internal->init_num = frag_len; 761 return frag_len; 762 } 763 764 /* read handshake message header */ 765 i = s->method->internal->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, wire, 766 DTLS1_HM_HEADER_LENGTH, 0); 767 if (i <= 0) /* nbio, or an error */ 768 { 769 s->internal->rwstate = SSL_READING; 770 *ok = 0; 771 return i; 772 } 773 /* Handshake fails if message header is incomplete */ 774 if (i != DTLS1_HM_HEADER_LENGTH || 775 /* parse the message fragment header */ 776 dtls1_get_message_header(wire, &msg_hdr) == 0) { 777 al = SSL_AD_UNEXPECTED_MESSAGE; 778 SSLerror(s, SSL_R_UNEXPECTED_MESSAGE); 779 goto f_err; 780 } 781 782 /* 783 * if this is a future (or stale) message it gets buffered 784 * (or dropped)--no further processing at this time 785 * While listening, we accept seq 1 (ClientHello with cookie) 786 * although we're still expecting seq 0 (ClientHello) 787 */ 788 if (msg_hdr.seq != D1I(s)->handshake_read_seq && 789 !(D1I(s)->listen && msg_hdr.seq == 1)) 790 return dtls1_process_out_of_seq_message(s, &msg_hdr, ok); 791 792 len = msg_hdr.msg_len; 793 frag_off = msg_hdr.frag_off; 794 frag_len = msg_hdr.frag_len; 795 796 if (frag_len && frag_len < len) 797 return dtls1_reassemble_fragment(s, &msg_hdr, ok); 798 799 if (!s->server && D1I(s)->r_msg_hdr.frag_off == 0 && 800 wire[0] == SSL3_MT_HELLO_REQUEST) { 801 /* 802 * The server may always send 'Hello Request' messages -- 803 * we are doing a handshake anyway now, so ignore them 804 * if their format is correct. Does not count for 805 * 'Finished' MAC. 806 */ 807 if (wire[1] == 0 && wire[2] == 0 && wire[3] == 0) { 808 if (s->internal->msg_callback) 809 s->internal->msg_callback(0, s->version, 810 SSL3_RT_HANDSHAKE, wire, 811 DTLS1_HM_HEADER_LENGTH, s, 812 s->internal->msg_callback_arg); 813 814 s->internal->init_num = 0; 815 goto again; 816 } 817 else /* Incorrectly formated Hello request */ 818 { 819 al = SSL_AD_UNEXPECTED_MESSAGE; 820 SSLerror(s, SSL_R_UNEXPECTED_MESSAGE); 821 goto f_err; 822 } 823 } 824 825 if ((al = dtls1_preprocess_fragment(s, &msg_hdr, max))) 826 goto f_err; 827 828 /* XDTLS: ressurect this when restart is in place */ 829 S3I(s)->hs.state = stn; 830 831 if (frag_len > 0) { 832 unsigned char *p = (unsigned char *)s->internal->init_buf->data + DTLS1_HM_HEADER_LENGTH; 833 834 i = s->method->internal->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, 835 &p[frag_off], frag_len, 0); 836 /* XDTLS: fix this--message fragments cannot span multiple packets */ 837 if (i <= 0) { 838 s->internal->rwstate = SSL_READING; 839 *ok = 0; 840 return i; 841 } 842 } else 843 i = 0; 844 845 /* 846 * XDTLS: an incorrectly formatted fragment should cause the 847 * handshake to fail 848 */ 849 if (i != (int)frag_len) { 850 al = SSL3_AD_ILLEGAL_PARAMETER; 851 SSLerror(s, SSL3_AD_ILLEGAL_PARAMETER); 852 goto f_err; 853 } 854 855 *ok = 1; 856 857 /* 858 * Note that s->internal->init_num is *not* used as current offset in 859 * s->internal->init_buf->data, but as a counter summing up fragments' 860 * lengths: as soon as they sum up to handshake packet 861 * length, we assume we have got all the fragments. 862 */ 863 s->internal->init_num = frag_len; 864 return frag_len; 865 866 f_err: 867 ssl3_send_alert(s, SSL3_AL_FATAL, al); 868 s->internal->init_num = 0; 869 870 *ok = 0; 871 return (-1); 872 } 873 874 int 875 dtls1_read_failed(SSL *s, int code) 876 { 877 if (code > 0) { 878 #ifdef DEBUG 879 fprintf(stderr, "invalid state reached %s:%d", 880 __FILE__, __LINE__); 881 #endif 882 return 1; 883 } 884 885 if (!dtls1_is_timer_expired(s)) { 886 /* 887 * not a timeout, none of our business, let higher layers 888 * handle this. in fact it's probably an error 889 */ 890 return code; 891 } 892 893 if (!SSL_in_init(s)) /* done, no need to send a retransmit */ 894 { 895 BIO_set_flags(SSL_get_rbio(s), BIO_FLAGS_READ); 896 return code; 897 } 898 899 return dtls1_handle_timeout(s); 900 } 901 902 int 903 dtls1_get_queue_priority(unsigned short seq, int is_ccs) 904 { 905 /* 906 * The index of the retransmission queue actually is the message 907 * sequence number, since the queue only contains messages of a 908 * single handshake. However, the ChangeCipherSpec has no message 909 * sequence number and so using only the sequence will result in 910 * the CCS and Finished having the same index. To prevent this, the 911 * sequence number is multiplied by 2. In case of a CCS 1 is 912 * subtracted. This does not only differ CSS and Finished, it also 913 * maintains the order of the index (important for priority queues) 914 * and fits in the unsigned short variable. 915 */ 916 return seq * 2 - is_ccs; 917 } 918 919 int 920 dtls1_retransmit_buffered_messages(SSL *s) 921 { 922 pqueue sent = s->d1->sent_messages; 923 piterator iter; 924 pitem *item; 925 hm_fragment *frag; 926 int found = 0; 927 928 iter = pqueue_iterator(sent); 929 930 for (item = pqueue_next(&iter); item != NULL; 931 item = pqueue_next(&iter)) { 932 frag = (hm_fragment *)item->data; 933 if (dtls1_retransmit_message(s, 934 (unsigned short)dtls1_get_queue_priority( 935 frag->msg_header.seq, frag->msg_header.is_ccs), 0, 936 &found) <= 0 && found) { 937 #ifdef DEBUG 938 fprintf(stderr, "dtls1_retransmit_message() failed\n"); 939 #endif 940 return -1; 941 } 942 } 943 944 return 1; 945 } 946 947 int 948 dtls1_buffer_message(SSL *s, int is_ccs) 949 { 950 pitem *item; 951 hm_fragment *frag; 952 unsigned char seq64be[8]; 953 954 /* Buffer the messsage in order to handle DTLS retransmissions. */ 955 956 /* 957 * This function is called immediately after a message has 958 * been serialized 959 */ 960 OPENSSL_assert(s->internal->init_off == 0); 961 962 frag = dtls1_hm_fragment_new(s->internal->init_num, 0); 963 if (frag == NULL) 964 return 0; 965 966 memcpy(frag->fragment, s->internal->init_buf->data, s->internal->init_num); 967 968 OPENSSL_assert(D1I(s)->w_msg_hdr.msg_len + 969 (is_ccs ? DTLS1_CCS_HEADER_LENGTH : DTLS1_HM_HEADER_LENGTH) == 970 (unsigned int)s->internal->init_num); 971 972 frag->msg_header.msg_len = D1I(s)->w_msg_hdr.msg_len; 973 frag->msg_header.seq = D1I(s)->w_msg_hdr.seq; 974 frag->msg_header.type = D1I(s)->w_msg_hdr.type; 975 frag->msg_header.frag_off = 0; 976 frag->msg_header.frag_len = D1I(s)->w_msg_hdr.msg_len; 977 frag->msg_header.is_ccs = is_ccs; 978 979 /* save current state*/ 980 frag->msg_header.saved_retransmit_state.enc_write_ctx = s->internal->enc_write_ctx; 981 frag->msg_header.saved_retransmit_state.write_hash = s->internal->write_hash; 982 frag->msg_header.saved_retransmit_state.session = s->session; 983 frag->msg_header.saved_retransmit_state.epoch = D1I(s)->w_epoch; 984 985 memset(seq64be, 0, sizeof(seq64be)); 986 seq64be[6] = (unsigned char)(dtls1_get_queue_priority( 987 frag->msg_header.seq, frag->msg_header.is_ccs) >> 8); 988 seq64be[7] = (unsigned char)(dtls1_get_queue_priority( 989 frag->msg_header.seq, frag->msg_header.is_ccs)); 990 991 item = pitem_new(seq64be, frag); 992 if (item == NULL) { 993 dtls1_hm_fragment_free(frag); 994 return 0; 995 } 996 997 pqueue_insert(s->d1->sent_messages, item); 998 return 1; 999 } 1000 1001 int 1002 dtls1_retransmit_message(SSL *s, unsigned short seq, unsigned long frag_off, 1003 int *found) 1004 { 1005 int ret; 1006 /* XDTLS: for now assuming that read/writes are blocking */ 1007 pitem *item; 1008 hm_fragment *frag; 1009 unsigned long header_length; 1010 unsigned char seq64be[8]; 1011 struct dtls1_retransmit_state saved_state; 1012 1013 /* 1014 OPENSSL_assert(s->internal->init_num == 0); 1015 OPENSSL_assert(s->internal->init_off == 0); 1016 */ 1017 1018 /* XDTLS: the requested message ought to be found, otherwise error */ 1019 memset(seq64be, 0, sizeof(seq64be)); 1020 seq64be[6] = (unsigned char)(seq >> 8); 1021 seq64be[7] = (unsigned char)seq; 1022 1023 item = pqueue_find(s->d1->sent_messages, seq64be); 1024 if (item == NULL) { 1025 #ifdef DEBUG 1026 fprintf(stderr, "retransmit: message %d non-existent\n", seq); 1027 #endif 1028 *found = 0; 1029 return 0; 1030 } 1031 1032 *found = 1; 1033 frag = (hm_fragment *)item->data; 1034 1035 if (frag->msg_header.is_ccs) 1036 header_length = DTLS1_CCS_HEADER_LENGTH; 1037 else 1038 header_length = DTLS1_HM_HEADER_LENGTH; 1039 1040 memcpy(s->internal->init_buf->data, frag->fragment, 1041 frag->msg_header.msg_len + header_length); 1042 s->internal->init_num = frag->msg_header.msg_len + header_length; 1043 1044 dtls1_set_message_header_int(s, frag->msg_header.type, 1045 frag->msg_header.msg_len, frag->msg_header.seq, 0, 1046 frag->msg_header.frag_len); 1047 1048 /* save current state */ 1049 saved_state.session = s->session; 1050 saved_state.epoch = D1I(s)->w_epoch; 1051 1052 D1I(s)->retransmitting = 1; 1053 1054 /* restore state in which the message was originally sent */ 1055 s->session = frag->msg_header.saved_retransmit_state.session; 1056 D1I(s)->w_epoch = frag->msg_header.saved_retransmit_state.epoch; 1057 1058 if (!tls12_record_layer_use_write_epoch(s->internal->rl, D1I(s)->w_epoch)) 1059 return 0; 1060 1061 ret = dtls1_do_write(s, frag->msg_header.is_ccs ? 1062 SSL3_RT_CHANGE_CIPHER_SPEC : SSL3_RT_HANDSHAKE); 1063 1064 /* restore current state */ 1065 s->session = saved_state.session; 1066 D1I(s)->w_epoch = saved_state.epoch; 1067 1068 if (!tls12_record_layer_use_write_epoch(s->internal->rl, D1I(s)->w_epoch)) 1069 return 0; 1070 1071 D1I(s)->retransmitting = 0; 1072 1073 (void)BIO_flush(SSL_get_wbio(s)); 1074 return ret; 1075 } 1076 1077 /* call this function when the buffered messages are no longer needed */ 1078 void 1079 dtls1_clear_record_buffer(SSL *s) 1080 { 1081 pitem *item; 1082 1083 for(item = pqueue_pop(s->d1->sent_messages); item != NULL; 1084 item = pqueue_pop(s->d1->sent_messages)) { 1085 dtls1_hm_fragment_free((hm_fragment *)item->data); 1086 pitem_free(item); 1087 } 1088 } 1089 1090 void 1091 dtls1_set_message_header(SSL *s, unsigned char mt, unsigned long len, 1092 unsigned long frag_off, unsigned long frag_len) 1093 { 1094 /* Don't change sequence numbers while listening */ 1095 if (frag_off == 0 && !D1I(s)->listen) { 1096 D1I(s)->handshake_write_seq = D1I(s)->next_handshake_write_seq; 1097 D1I(s)->next_handshake_write_seq++; 1098 } 1099 1100 dtls1_set_message_header_int(s, mt, len, D1I(s)->handshake_write_seq, 1101 frag_off, frag_len); 1102 } 1103 1104 /* don't actually do the writing, wait till the MTU has been retrieved */ 1105 void 1106 dtls1_set_message_header_int(SSL *s, unsigned char mt, unsigned long len, 1107 unsigned short seq_num, unsigned long frag_off, unsigned long frag_len) 1108 { 1109 struct hm_header_st *msg_hdr = &D1I(s)->w_msg_hdr; 1110 1111 msg_hdr->type = mt; 1112 msg_hdr->msg_len = len; 1113 msg_hdr->seq = seq_num; 1114 msg_hdr->frag_off = frag_off; 1115 msg_hdr->frag_len = frag_len; 1116 } 1117 1118 static void 1119 dtls1_fix_message_header(SSL *s, unsigned long frag_off, unsigned long frag_len) 1120 { 1121 struct hm_header_st *msg_hdr = &D1I(s)->w_msg_hdr; 1122 1123 msg_hdr->frag_off = frag_off; 1124 msg_hdr->frag_len = frag_len; 1125 } 1126 1127 static int 1128 dtls1_write_message_header(const struct hm_header_st *msg_hdr, 1129 unsigned long frag_off, unsigned long frag_len, unsigned char *p) 1130 { 1131 CBB cbb; 1132 1133 /* We assume DTLS1_HM_HEADER_LENGTH bytes are available for now... */ 1134 if (!CBB_init_fixed(&cbb, p, DTLS1_HM_HEADER_LENGTH)) 1135 return 0; 1136 if (!CBB_add_u8(&cbb, msg_hdr->type)) 1137 goto err; 1138 if (!CBB_add_u24(&cbb, msg_hdr->msg_len)) 1139 goto err; 1140 if (!CBB_add_u16(&cbb, msg_hdr->seq)) 1141 goto err; 1142 if (!CBB_add_u24(&cbb, frag_off)) 1143 goto err; 1144 if (!CBB_add_u24(&cbb, frag_len)) 1145 goto err; 1146 if (!CBB_finish(&cbb, NULL, NULL)) 1147 goto err; 1148 1149 return 1; 1150 1151 err: 1152 CBB_cleanup(&cbb); 1153 return 0; 1154 } 1155 1156 unsigned int 1157 dtls1_min_mtu(void) 1158 { 1159 return (g_probable_mtu[(sizeof(g_probable_mtu) / 1160 sizeof(g_probable_mtu[0])) - 1]); 1161 } 1162 1163 static unsigned int 1164 dtls1_guess_mtu(unsigned int curr_mtu) 1165 { 1166 unsigned int i; 1167 1168 if (curr_mtu == 0) 1169 return g_probable_mtu[0]; 1170 1171 for (i = 0; i < sizeof(g_probable_mtu) / sizeof(g_probable_mtu[0]); i++) 1172 if (curr_mtu > g_probable_mtu[i]) 1173 return g_probable_mtu[i]; 1174 1175 return curr_mtu; 1176 } 1177 1178 int 1179 dtls1_get_message_header(unsigned char *data, struct hm_header_st *msg_hdr) 1180 { 1181 CBS header; 1182 uint32_t msg_len, frag_off, frag_len; 1183 uint16_t seq; 1184 uint8_t type; 1185 1186 CBS_init(&header, data, sizeof(*msg_hdr)); 1187 1188 memset(msg_hdr, 0, sizeof(*msg_hdr)); 1189 1190 if (!CBS_get_u8(&header, &type)) 1191 return 0; 1192 if (!CBS_get_u24(&header, &msg_len)) 1193 return 0; 1194 if (!CBS_get_u16(&header, &seq)) 1195 return 0; 1196 if (!CBS_get_u24(&header, &frag_off)) 1197 return 0; 1198 if (!CBS_get_u24(&header, &frag_len)) 1199 return 0; 1200 1201 msg_hdr->type = type; 1202 msg_hdr->msg_len = msg_len; 1203 msg_hdr->seq = seq; 1204 msg_hdr->frag_off = frag_off; 1205 msg_hdr->frag_len = frag_len; 1206 1207 return 1; 1208 } 1209 1210 void 1211 dtls1_get_ccs_header(unsigned char *data, struct ccs_header_st *ccs_hdr) 1212 { 1213 memset(ccs_hdr, 0, sizeof(struct ccs_header_st)); 1214 1215 ccs_hdr->type = *(data++); 1216 } 1217