1*2139Sjp161948 /* ssl/d1_pkt.c */ 2*2139Sjp161948 /* 3*2139Sjp161948 * DTLS implementation written by Nagendra Modadugu 4*2139Sjp161948 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. 5*2139Sjp161948 */ 6*2139Sjp161948 /* ==================================================================== 7*2139Sjp161948 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. 8*2139Sjp161948 * 9*2139Sjp161948 * Redistribution and use in source and binary forms, with or without 10*2139Sjp161948 * modification, are permitted provided that the following conditions 11*2139Sjp161948 * are met: 12*2139Sjp161948 * 13*2139Sjp161948 * 1. Redistributions of source code must retain the above copyright 14*2139Sjp161948 * notice, this list of conditions and the following disclaimer. 15*2139Sjp161948 * 16*2139Sjp161948 * 2. Redistributions in binary form must reproduce the above copyright 17*2139Sjp161948 * notice, this list of conditions and the following disclaimer in 18*2139Sjp161948 * the documentation and/or other materials provided with the 19*2139Sjp161948 * distribution. 20*2139Sjp161948 * 21*2139Sjp161948 * 3. All advertising materials mentioning features or use of this 22*2139Sjp161948 * software must display the following acknowledgment: 23*2139Sjp161948 * "This product includes software developed by the OpenSSL Project 24*2139Sjp161948 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 25*2139Sjp161948 * 26*2139Sjp161948 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 27*2139Sjp161948 * endorse or promote products derived from this software without 28*2139Sjp161948 * prior written permission. For written permission, please contact 29*2139Sjp161948 * openssl-core@openssl.org. 30*2139Sjp161948 * 31*2139Sjp161948 * 5. Products derived from this software may not be called "OpenSSL" 32*2139Sjp161948 * nor may "OpenSSL" appear in their names without prior written 33*2139Sjp161948 * permission of the OpenSSL Project. 34*2139Sjp161948 * 35*2139Sjp161948 * 6. Redistributions of any form whatsoever must retain the following 36*2139Sjp161948 * acknowledgment: 37*2139Sjp161948 * "This product includes software developed by the OpenSSL Project 38*2139Sjp161948 * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 39*2139Sjp161948 * 40*2139Sjp161948 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 41*2139Sjp161948 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42*2139Sjp161948 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 43*2139Sjp161948 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 44*2139Sjp161948 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45*2139Sjp161948 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 46*2139Sjp161948 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 47*2139Sjp161948 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48*2139Sjp161948 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 49*2139Sjp161948 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 50*2139Sjp161948 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 51*2139Sjp161948 * OF THE POSSIBILITY OF SUCH DAMAGE. 52*2139Sjp161948 * ==================================================================== 53*2139Sjp161948 * 54*2139Sjp161948 * This product includes cryptographic software written by Eric Young 55*2139Sjp161948 * (eay@cryptsoft.com). This product includes software written by Tim 56*2139Sjp161948 * Hudson (tjh@cryptsoft.com). 57*2139Sjp161948 * 58*2139Sjp161948 */ 59*2139Sjp161948 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 60*2139Sjp161948 * All rights reserved. 61*2139Sjp161948 * 62*2139Sjp161948 * This package is an SSL implementation written 63*2139Sjp161948 * by Eric Young (eay@cryptsoft.com). 64*2139Sjp161948 * The implementation was written so as to conform with Netscapes SSL. 65*2139Sjp161948 * 66*2139Sjp161948 * This library is free for commercial and non-commercial use as long as 67*2139Sjp161948 * the following conditions are aheared to. The following conditions 68*2139Sjp161948 * apply to all code found in this distribution, be it the RC4, RSA, 69*2139Sjp161948 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 70*2139Sjp161948 * included with this distribution is covered by the same copyright terms 71*2139Sjp161948 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 72*2139Sjp161948 * 73*2139Sjp161948 * Copyright remains Eric Young's, and as such any Copyright notices in 74*2139Sjp161948 * the code are not to be removed. 75*2139Sjp161948 * If this package is used in a product, Eric Young should be given attribution 76*2139Sjp161948 * as the author of the parts of the library used. 77*2139Sjp161948 * This can be in the form of a textual message at program startup or 78*2139Sjp161948 * in documentation (online or textual) provided with the package. 79*2139Sjp161948 * 80*2139Sjp161948 * Redistribution and use in source and binary forms, with or without 81*2139Sjp161948 * modification, are permitted provided that the following conditions 82*2139Sjp161948 * are met: 83*2139Sjp161948 * 1. Redistributions of source code must retain the copyright 84*2139Sjp161948 * notice, this list of conditions and the following disclaimer. 85*2139Sjp161948 * 2. Redistributions in binary form must reproduce the above copyright 86*2139Sjp161948 * notice, this list of conditions and the following disclaimer in the 87*2139Sjp161948 * documentation and/or other materials provided with the distribution. 88*2139Sjp161948 * 3. All advertising materials mentioning features or use of this software 89*2139Sjp161948 * must display the following acknowledgement: 90*2139Sjp161948 * "This product includes cryptographic software written by 91*2139Sjp161948 * Eric Young (eay@cryptsoft.com)" 92*2139Sjp161948 * The word 'cryptographic' can be left out if the rouines from the library 93*2139Sjp161948 * being used are not cryptographic related :-). 94*2139Sjp161948 * 4. If you include any Windows specific code (or a derivative thereof) from 95*2139Sjp161948 * the apps directory (application code) you must include an acknowledgement: 96*2139Sjp161948 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 97*2139Sjp161948 * 98*2139Sjp161948 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 99*2139Sjp161948 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 100*2139Sjp161948 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 101*2139Sjp161948 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 102*2139Sjp161948 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 103*2139Sjp161948 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 104*2139Sjp161948 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 105*2139Sjp161948 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 106*2139Sjp161948 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 107*2139Sjp161948 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 108*2139Sjp161948 * SUCH DAMAGE. 109*2139Sjp161948 * 110*2139Sjp161948 * The licence and distribution terms for any publically available version or 111*2139Sjp161948 * derivative of this code cannot be changed. i.e. this code cannot simply be 112*2139Sjp161948 * copied and put under another distribution licence 113*2139Sjp161948 * [including the GNU Public Licence.] 114*2139Sjp161948 */ 115*2139Sjp161948 116*2139Sjp161948 #include <stdio.h> 117*2139Sjp161948 #include <errno.h> 118*2139Sjp161948 #define USE_SOCKETS 119*2139Sjp161948 #include "ssl_locl.h" 120*2139Sjp161948 #include <openssl/evp.h> 121*2139Sjp161948 #include <openssl/buffer.h> 122*2139Sjp161948 #include <openssl/pqueue.h> 123*2139Sjp161948 124*2139Sjp161948 static int have_handshake_fragment(SSL *s, int type, unsigned char *buf, 125*2139Sjp161948 int len, int peek); 126*2139Sjp161948 static int dtls1_record_replay_check(SSL *s, DTLS1_BITMAP *bitmap, 127*2139Sjp161948 PQ_64BIT *seq_num); 128*2139Sjp161948 static void dtls1_record_bitmap_update(SSL *s, DTLS1_BITMAP *bitmap); 129*2139Sjp161948 static DTLS1_BITMAP *dtls1_get_bitmap(SSL *s, SSL3_RECORD *rr, 130*2139Sjp161948 unsigned int *is_next_epoch); 131*2139Sjp161948 #if 0 132*2139Sjp161948 static int dtls1_record_needs_buffering(SSL *s, SSL3_RECORD *rr, 133*2139Sjp161948 unsigned short *priority, unsigned long *offset); 134*2139Sjp161948 #endif 135*2139Sjp161948 static int dtls1_buffer_record(SSL *s, record_pqueue *q, 136*2139Sjp161948 PQ_64BIT priority); 137*2139Sjp161948 static int dtls1_process_record(SSL *s); 138*2139Sjp161948 #if PQ_64BIT_IS_INTEGER 139*2139Sjp161948 static PQ_64BIT bytes_to_long_long(unsigned char *bytes, PQ_64BIT *num); 140*2139Sjp161948 #endif 141*2139Sjp161948 static void dtls1_clear_timeouts(SSL *s); 142*2139Sjp161948 143*2139Sjp161948 /* copy buffered record into SSL structure */ 144*2139Sjp161948 static int 145*2139Sjp161948 dtls1_copy_record(SSL *s, pitem *item) 146*2139Sjp161948 { 147*2139Sjp161948 DTLS1_RECORD_DATA *rdata; 148*2139Sjp161948 149*2139Sjp161948 rdata = (DTLS1_RECORD_DATA *)item->data; 150*2139Sjp161948 151*2139Sjp161948 if (s->s3->rbuf.buf != NULL) 152*2139Sjp161948 OPENSSL_free(s->s3->rbuf.buf); 153*2139Sjp161948 154*2139Sjp161948 s->packet = rdata->packet; 155*2139Sjp161948 s->packet_length = rdata->packet_length; 156*2139Sjp161948 memcpy(&(s->s3->rbuf), &(rdata->rbuf), sizeof(SSL3_BUFFER)); 157*2139Sjp161948 memcpy(&(s->s3->rrec), &(rdata->rrec), sizeof(SSL3_RECORD)); 158*2139Sjp161948 159*2139Sjp161948 return(1); 160*2139Sjp161948 } 161*2139Sjp161948 162*2139Sjp161948 163*2139Sjp161948 static int 164*2139Sjp161948 dtls1_buffer_record(SSL *s, record_pqueue *queue, PQ_64BIT priority) 165*2139Sjp161948 { 166*2139Sjp161948 DTLS1_RECORD_DATA *rdata; 167*2139Sjp161948 pitem *item; 168*2139Sjp161948 169*2139Sjp161948 rdata = OPENSSL_malloc(sizeof(DTLS1_RECORD_DATA)); 170*2139Sjp161948 item = pitem_new(priority, rdata); 171*2139Sjp161948 if (rdata == NULL || item == NULL) 172*2139Sjp161948 { 173*2139Sjp161948 if (rdata != NULL) OPENSSL_free(rdata); 174*2139Sjp161948 if (item != NULL) pitem_free(item); 175*2139Sjp161948 176*2139Sjp161948 SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR); 177*2139Sjp161948 return(0); 178*2139Sjp161948 } 179*2139Sjp161948 180*2139Sjp161948 rdata->packet = s->packet; 181*2139Sjp161948 rdata->packet_length = s->packet_length; 182*2139Sjp161948 memcpy(&(rdata->rbuf), &(s->s3->rbuf), sizeof(SSL3_BUFFER)); 183*2139Sjp161948 memcpy(&(rdata->rrec), &(s->s3->rrec), sizeof(SSL3_RECORD)); 184*2139Sjp161948 185*2139Sjp161948 item->data = rdata; 186*2139Sjp161948 187*2139Sjp161948 /* insert should not fail, since duplicates are dropped */ 188*2139Sjp161948 if (pqueue_insert(queue->q, item) == NULL) 189*2139Sjp161948 { 190*2139Sjp161948 OPENSSL_free(rdata); 191*2139Sjp161948 pitem_free(item); 192*2139Sjp161948 return(0); 193*2139Sjp161948 } 194*2139Sjp161948 195*2139Sjp161948 s->packet = NULL; 196*2139Sjp161948 s->packet_length = 0; 197*2139Sjp161948 memset(&(s->s3->rbuf), 0, sizeof(SSL3_BUFFER)); 198*2139Sjp161948 memset(&(s->s3->rrec), 0, sizeof(SSL3_RECORD)); 199*2139Sjp161948 200*2139Sjp161948 ssl3_setup_buffers(s); 201*2139Sjp161948 202*2139Sjp161948 return(1); 203*2139Sjp161948 } 204*2139Sjp161948 205*2139Sjp161948 206*2139Sjp161948 static int 207*2139Sjp161948 dtls1_retrieve_buffered_record(SSL *s, record_pqueue *queue) 208*2139Sjp161948 { 209*2139Sjp161948 pitem *item; 210*2139Sjp161948 211*2139Sjp161948 item = pqueue_pop(queue->q); 212*2139Sjp161948 if (item) 213*2139Sjp161948 { 214*2139Sjp161948 dtls1_copy_record(s, item); 215*2139Sjp161948 216*2139Sjp161948 OPENSSL_free(item->data); 217*2139Sjp161948 pitem_free(item); 218*2139Sjp161948 219*2139Sjp161948 return(1); 220*2139Sjp161948 } 221*2139Sjp161948 222*2139Sjp161948 return(0); 223*2139Sjp161948 } 224*2139Sjp161948 225*2139Sjp161948 226*2139Sjp161948 /* retrieve a buffered record that belongs to the new epoch, i.e., not processed 227*2139Sjp161948 * yet */ 228*2139Sjp161948 #define dtls1_get_unprocessed_record(s) \ 229*2139Sjp161948 dtls1_retrieve_buffered_record((s), \ 230*2139Sjp161948 &((s)->d1->unprocessed_rcds)) 231*2139Sjp161948 232*2139Sjp161948 /* retrieve a buffered record that belongs to the current epoch, ie, processed */ 233*2139Sjp161948 #define dtls1_get_processed_record(s) \ 234*2139Sjp161948 dtls1_retrieve_buffered_record((s), \ 235*2139Sjp161948 &((s)->d1->processed_rcds)) 236*2139Sjp161948 237*2139Sjp161948 static int 238*2139Sjp161948 dtls1_process_buffered_records(SSL *s) 239*2139Sjp161948 { 240*2139Sjp161948 pitem *item; 241*2139Sjp161948 242*2139Sjp161948 item = pqueue_peek(s->d1->unprocessed_rcds.q); 243*2139Sjp161948 if (item) 244*2139Sjp161948 { 245*2139Sjp161948 DTLS1_RECORD_DATA *rdata; 246*2139Sjp161948 rdata = (DTLS1_RECORD_DATA *)item->data; 247*2139Sjp161948 248*2139Sjp161948 /* Check if epoch is current. */ 249*2139Sjp161948 if (s->d1->unprocessed_rcds.epoch != s->d1->r_epoch) 250*2139Sjp161948 return(1); /* Nothing to do. */ 251*2139Sjp161948 252*2139Sjp161948 /* Process all the records. */ 253*2139Sjp161948 while (pqueue_peek(s->d1->unprocessed_rcds.q)) 254*2139Sjp161948 { 255*2139Sjp161948 dtls1_get_unprocessed_record(s); 256*2139Sjp161948 if ( ! dtls1_process_record(s)) 257*2139Sjp161948 return(0); 258*2139Sjp161948 dtls1_buffer_record(s, &(s->d1->processed_rcds), 259*2139Sjp161948 s->s3->rrec.seq_num); 260*2139Sjp161948 } 261*2139Sjp161948 } 262*2139Sjp161948 263*2139Sjp161948 /* sync epoch numbers once all the unprocessed records 264*2139Sjp161948 * have been processed */ 265*2139Sjp161948 s->d1->processed_rcds.epoch = s->d1->r_epoch; 266*2139Sjp161948 s->d1->unprocessed_rcds.epoch = s->d1->r_epoch + 1; 267*2139Sjp161948 268*2139Sjp161948 return(1); 269*2139Sjp161948 } 270*2139Sjp161948 271*2139Sjp161948 272*2139Sjp161948 #if 0 273*2139Sjp161948 274*2139Sjp161948 static int 275*2139Sjp161948 dtls1_get_buffered_record(SSL *s) 276*2139Sjp161948 { 277*2139Sjp161948 pitem *item; 278*2139Sjp161948 PQ_64BIT priority = 279*2139Sjp161948 (((PQ_64BIT)s->d1->handshake_read_seq) << 32) | 280*2139Sjp161948 ((PQ_64BIT)s->d1->r_msg_hdr.frag_off); 281*2139Sjp161948 282*2139Sjp161948 if ( ! SSL_in_init(s)) /* if we're not (re)negotiating, 283*2139Sjp161948 nothing buffered */ 284*2139Sjp161948 return 0; 285*2139Sjp161948 286*2139Sjp161948 287*2139Sjp161948 item = pqueue_peek(s->d1->rcvd_records); 288*2139Sjp161948 if (item && item->priority == priority) 289*2139Sjp161948 { 290*2139Sjp161948 /* Check if we've received the record of interest. It must be 291*2139Sjp161948 * a handshake record, since data records as passed up without 292*2139Sjp161948 * buffering */ 293*2139Sjp161948 DTLS1_RECORD_DATA *rdata; 294*2139Sjp161948 item = pqueue_pop(s->d1->rcvd_records); 295*2139Sjp161948 rdata = (DTLS1_RECORD_DATA *)item->data; 296*2139Sjp161948 297*2139Sjp161948 if (s->s3->rbuf.buf != NULL) 298*2139Sjp161948 OPENSSL_free(s->s3->rbuf.buf); 299*2139Sjp161948 300*2139Sjp161948 s->packet = rdata->packet; 301*2139Sjp161948 s->packet_length = rdata->packet_length; 302*2139Sjp161948 memcpy(&(s->s3->rbuf), &(rdata->rbuf), sizeof(SSL3_BUFFER)); 303*2139Sjp161948 memcpy(&(s->s3->rrec), &(rdata->rrec), sizeof(SSL3_RECORD)); 304*2139Sjp161948 305*2139Sjp161948 OPENSSL_free(item->data); 306*2139Sjp161948 pitem_free(item); 307*2139Sjp161948 308*2139Sjp161948 /* s->d1->next_expected_seq_num++; */ 309*2139Sjp161948 return(1); 310*2139Sjp161948 } 311*2139Sjp161948 312*2139Sjp161948 return 0; 313*2139Sjp161948 } 314*2139Sjp161948 315*2139Sjp161948 #endif 316*2139Sjp161948 317*2139Sjp161948 static int 318*2139Sjp161948 dtls1_process_record(SSL *s) 319*2139Sjp161948 { 320*2139Sjp161948 int i,al; 321*2139Sjp161948 int clear=0; 322*2139Sjp161948 int enc_err; 323*2139Sjp161948 SSL_SESSION *sess; 324*2139Sjp161948 SSL3_RECORD *rr; 325*2139Sjp161948 unsigned int mac_size; 326*2139Sjp161948 unsigned char md[EVP_MAX_MD_SIZE]; 327*2139Sjp161948 328*2139Sjp161948 329*2139Sjp161948 rr= &(s->s3->rrec); 330*2139Sjp161948 sess = s->session; 331*2139Sjp161948 332*2139Sjp161948 /* At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length, 333*2139Sjp161948 * and we have that many bytes in s->packet 334*2139Sjp161948 */ 335*2139Sjp161948 rr->input= &(s->packet[DTLS1_RT_HEADER_LENGTH]); 336*2139Sjp161948 337*2139Sjp161948 /* ok, we can now read from 's->packet' data into 'rr' 338*2139Sjp161948 * rr->input points at rr->length bytes, which 339*2139Sjp161948 * need to be copied into rr->data by either 340*2139Sjp161948 * the decryption or by the decompression 341*2139Sjp161948 * When the data is 'copied' into the rr->data buffer, 342*2139Sjp161948 * rr->input will be pointed at the new buffer */ 343*2139Sjp161948 344*2139Sjp161948 /* We now have - encrypted [ MAC [ compressed [ plain ] ] ] 345*2139Sjp161948 * rr->length bytes of encrypted compressed stuff. */ 346*2139Sjp161948 347*2139Sjp161948 /* check is not needed I believe */ 348*2139Sjp161948 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) 349*2139Sjp161948 { 350*2139Sjp161948 al=SSL_AD_RECORD_OVERFLOW; 351*2139Sjp161948 SSLerr(SSL_F_DTLS1_PROCESS_RECORD,SSL_R_ENCRYPTED_LENGTH_TOO_LONG); 352*2139Sjp161948 goto f_err; 353*2139Sjp161948 } 354*2139Sjp161948 355*2139Sjp161948 /* decrypt in place in 'rr->input' */ 356*2139Sjp161948 rr->data=rr->input; 357*2139Sjp161948 358*2139Sjp161948 enc_err = s->method->ssl3_enc->enc(s,0); 359*2139Sjp161948 if (enc_err <= 0) 360*2139Sjp161948 { 361*2139Sjp161948 if (enc_err == 0) 362*2139Sjp161948 /* SSLerr() and ssl3_send_alert() have been called */ 363*2139Sjp161948 goto err; 364*2139Sjp161948 365*2139Sjp161948 /* otherwise enc_err == -1 */ 366*2139Sjp161948 goto decryption_failed_or_bad_record_mac; 367*2139Sjp161948 } 368*2139Sjp161948 369*2139Sjp161948 #ifdef TLS_DEBUG 370*2139Sjp161948 printf("dec %d\n",rr->length); 371*2139Sjp161948 { unsigned int z; for (z=0; z<rr->length; z++) printf("%02X%c",rr->data[z],((z+1)%16)?' ':'\n'); } 372*2139Sjp161948 printf("\n"); 373*2139Sjp161948 #endif 374*2139Sjp161948 375*2139Sjp161948 /* r->length is now the compressed data plus mac */ 376*2139Sjp161948 if ( (sess == NULL) || 377*2139Sjp161948 (s->enc_read_ctx == NULL) || 378*2139Sjp161948 (s->read_hash == NULL)) 379*2139Sjp161948 clear=1; 380*2139Sjp161948 381*2139Sjp161948 if (!clear) 382*2139Sjp161948 { 383*2139Sjp161948 mac_size=EVP_MD_size(s->read_hash); 384*2139Sjp161948 385*2139Sjp161948 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH+mac_size) 386*2139Sjp161948 { 387*2139Sjp161948 #if 0 /* OK only for stream ciphers (then rr->length is visible from ciphertext anyway) */ 388*2139Sjp161948 al=SSL_AD_RECORD_OVERFLOW; 389*2139Sjp161948 SSLerr(SSL_F_DTLS1_PROCESS_RECORD,SSL_R_PRE_MAC_LENGTH_TOO_LONG); 390*2139Sjp161948 goto f_err; 391*2139Sjp161948 #else 392*2139Sjp161948 goto decryption_failed_or_bad_record_mac; 393*2139Sjp161948 #endif 394*2139Sjp161948 } 395*2139Sjp161948 /* check the MAC for rr->input (it's in mac_size bytes at the tail) */ 396*2139Sjp161948 if (rr->length < mac_size) 397*2139Sjp161948 { 398*2139Sjp161948 #if 0 /* OK only for stream ciphers */ 399*2139Sjp161948 al=SSL_AD_DECODE_ERROR; 400*2139Sjp161948 SSLerr(SSL_F_DTLS1_PROCESS_RECORD,SSL_R_LENGTH_TOO_SHORT); 401*2139Sjp161948 goto f_err; 402*2139Sjp161948 #else 403*2139Sjp161948 goto decryption_failed_or_bad_record_mac; 404*2139Sjp161948 #endif 405*2139Sjp161948 } 406*2139Sjp161948 rr->length-=mac_size; 407*2139Sjp161948 i=s->method->ssl3_enc->mac(s,md,0); 408*2139Sjp161948 if (memcmp(md,&(rr->data[rr->length]),mac_size) != 0) 409*2139Sjp161948 { 410*2139Sjp161948 goto decryption_failed_or_bad_record_mac; 411*2139Sjp161948 } 412*2139Sjp161948 } 413*2139Sjp161948 414*2139Sjp161948 /* r->length is now just compressed */ 415*2139Sjp161948 if (s->expand != NULL) 416*2139Sjp161948 { 417*2139Sjp161948 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) 418*2139Sjp161948 { 419*2139Sjp161948 al=SSL_AD_RECORD_OVERFLOW; 420*2139Sjp161948 SSLerr(SSL_F_DTLS1_PROCESS_RECORD,SSL_R_COMPRESSED_LENGTH_TOO_LONG); 421*2139Sjp161948 goto f_err; 422*2139Sjp161948 } 423*2139Sjp161948 if (!ssl3_do_uncompress(s)) 424*2139Sjp161948 { 425*2139Sjp161948 al=SSL_AD_DECOMPRESSION_FAILURE; 426*2139Sjp161948 SSLerr(SSL_F_DTLS1_PROCESS_RECORD,SSL_R_BAD_DECOMPRESSION); 427*2139Sjp161948 goto f_err; 428*2139Sjp161948 } 429*2139Sjp161948 } 430*2139Sjp161948 431*2139Sjp161948 if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH) 432*2139Sjp161948 { 433*2139Sjp161948 al=SSL_AD_RECORD_OVERFLOW; 434*2139Sjp161948 SSLerr(SSL_F_DTLS1_PROCESS_RECORD,SSL_R_DATA_LENGTH_TOO_LONG); 435*2139Sjp161948 goto f_err; 436*2139Sjp161948 } 437*2139Sjp161948 438*2139Sjp161948 rr->off=0; 439*2139Sjp161948 /* So at this point the following is true 440*2139Sjp161948 * ssl->s3->rrec.type is the type of record 441*2139Sjp161948 * ssl->s3->rrec.length == number of bytes in record 442*2139Sjp161948 * ssl->s3->rrec.off == offset to first valid byte 443*2139Sjp161948 * ssl->s3->rrec.data == where to take bytes from, increment 444*2139Sjp161948 * after use :-). 445*2139Sjp161948 */ 446*2139Sjp161948 447*2139Sjp161948 /* we have pulled in a full packet so zero things */ 448*2139Sjp161948 s->packet_length=0; 449*2139Sjp161948 dtls1_record_bitmap_update(s, &(s->d1->bitmap));/* Mark receipt of record. */ 450*2139Sjp161948 return(1); 451*2139Sjp161948 452*2139Sjp161948 decryption_failed_or_bad_record_mac: 453*2139Sjp161948 /* Separate 'decryption_failed' alert was introduced with TLS 1.0, 454*2139Sjp161948 * SSL 3.0 only has 'bad_record_mac'. But unless a decryption 455*2139Sjp161948 * failure is directly visible from the ciphertext anyway, 456*2139Sjp161948 * we should not reveal which kind of error occured -- this 457*2139Sjp161948 * might become visible to an attacker (e.g. via logfile) */ 458*2139Sjp161948 al=SSL_AD_BAD_RECORD_MAC; 459*2139Sjp161948 SSLerr(SSL_F_DTLS1_PROCESS_RECORD,SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC); 460*2139Sjp161948 f_err: 461*2139Sjp161948 ssl3_send_alert(s,SSL3_AL_FATAL,al); 462*2139Sjp161948 err: 463*2139Sjp161948 return(0); 464*2139Sjp161948 } 465*2139Sjp161948 466*2139Sjp161948 467*2139Sjp161948 /* Call this to get a new input record. 468*2139Sjp161948 * It will return <= 0 if more data is needed, normally due to an error 469*2139Sjp161948 * or non-blocking IO. 470*2139Sjp161948 * When it finishes, one packet has been decoded and can be found in 471*2139Sjp161948 * ssl->s3->rrec.type - is the type of record 472*2139Sjp161948 * ssl->s3->rrec.data, - data 473*2139Sjp161948 * ssl->s3->rrec.length, - number of bytes 474*2139Sjp161948 */ 475*2139Sjp161948 /* used only by dtls1_read_bytes */ 476*2139Sjp161948 int dtls1_get_record(SSL *s) 477*2139Sjp161948 { 478*2139Sjp161948 int ssl_major,ssl_minor,al; 479*2139Sjp161948 int i,n; 480*2139Sjp161948 SSL3_RECORD *rr; 481*2139Sjp161948 SSL_SESSION *sess; 482*2139Sjp161948 unsigned char *p; 483*2139Sjp161948 short version; 484*2139Sjp161948 DTLS1_BITMAP *bitmap; 485*2139Sjp161948 unsigned int is_next_epoch; 486*2139Sjp161948 487*2139Sjp161948 rr= &(s->s3->rrec); 488*2139Sjp161948 sess=s->session; 489*2139Sjp161948 490*2139Sjp161948 /* The epoch may have changed. If so, process all the 491*2139Sjp161948 * pending records. This is a non-blocking operation. */ 492*2139Sjp161948 if ( ! dtls1_process_buffered_records(s)) 493*2139Sjp161948 return 0; 494*2139Sjp161948 495*2139Sjp161948 /* if we're renegotiating, then there may be buffered records */ 496*2139Sjp161948 if (dtls1_get_processed_record(s)) 497*2139Sjp161948 return 1; 498*2139Sjp161948 499*2139Sjp161948 /* get something from the wire */ 500*2139Sjp161948 again: 501*2139Sjp161948 /* check if we have the header */ 502*2139Sjp161948 if ( (s->rstate != SSL_ST_READ_BODY) || 503*2139Sjp161948 (s->packet_length < DTLS1_RT_HEADER_LENGTH)) 504*2139Sjp161948 { 505*2139Sjp161948 n=ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH, s->s3->rbuf.len, 0); 506*2139Sjp161948 /* read timeout is handled by dtls1_read_bytes */ 507*2139Sjp161948 if (n <= 0) return(n); /* error or non-blocking */ 508*2139Sjp161948 509*2139Sjp161948 OPENSSL_assert(s->packet_length == DTLS1_RT_HEADER_LENGTH); 510*2139Sjp161948 511*2139Sjp161948 s->rstate=SSL_ST_READ_BODY; 512*2139Sjp161948 513*2139Sjp161948 p=s->packet; 514*2139Sjp161948 515*2139Sjp161948 /* Pull apart the header into the DTLS1_RECORD */ 516*2139Sjp161948 rr->type= *(p++); 517*2139Sjp161948 ssl_major= *(p++); 518*2139Sjp161948 ssl_minor= *(p++); 519*2139Sjp161948 version=(ssl_major<<8)|ssl_minor; 520*2139Sjp161948 521*2139Sjp161948 /* sequence number is 64 bits, with top 2 bytes = epoch */ 522*2139Sjp161948 n2s(p,rr->epoch); 523*2139Sjp161948 524*2139Sjp161948 memcpy(&(s->s3->read_sequence[2]), p, 6); 525*2139Sjp161948 p+=6; 526*2139Sjp161948 527*2139Sjp161948 n2s(p,rr->length); 528*2139Sjp161948 529*2139Sjp161948 /* Lets check version */ 530*2139Sjp161948 if (s->first_packet) 531*2139Sjp161948 { 532*2139Sjp161948 s->first_packet=0; 533*2139Sjp161948 } 534*2139Sjp161948 else 535*2139Sjp161948 { 536*2139Sjp161948 if (version != s->version) 537*2139Sjp161948 { 538*2139Sjp161948 SSLerr(SSL_F_DTLS1_GET_RECORD,SSL_R_WRONG_VERSION_NUMBER); 539*2139Sjp161948 /* Send back error using their 540*2139Sjp161948 * version number :-) */ 541*2139Sjp161948 s->version=version; 542*2139Sjp161948 al=SSL_AD_PROTOCOL_VERSION; 543*2139Sjp161948 goto f_err; 544*2139Sjp161948 } 545*2139Sjp161948 } 546*2139Sjp161948 547*2139Sjp161948 if ((version & 0xff00) != (DTLS1_VERSION & 0xff00)) 548*2139Sjp161948 { 549*2139Sjp161948 SSLerr(SSL_F_DTLS1_GET_RECORD,SSL_R_WRONG_VERSION_NUMBER); 550*2139Sjp161948 goto err; 551*2139Sjp161948 } 552*2139Sjp161948 553*2139Sjp161948 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) 554*2139Sjp161948 { 555*2139Sjp161948 al=SSL_AD_RECORD_OVERFLOW; 556*2139Sjp161948 SSLerr(SSL_F_DTLS1_GET_RECORD,SSL_R_PACKET_LENGTH_TOO_LONG); 557*2139Sjp161948 goto f_err; 558*2139Sjp161948 } 559*2139Sjp161948 560*2139Sjp161948 /* now s->rstate == SSL_ST_READ_BODY */ 561*2139Sjp161948 } 562*2139Sjp161948 563*2139Sjp161948 /* s->rstate == SSL_ST_READ_BODY, get and decode the data */ 564*2139Sjp161948 565*2139Sjp161948 if (rr->length > s->packet_length-DTLS1_RT_HEADER_LENGTH) 566*2139Sjp161948 { 567*2139Sjp161948 /* now s->packet_length == DTLS1_RT_HEADER_LENGTH */ 568*2139Sjp161948 i=rr->length; 569*2139Sjp161948 n=ssl3_read_n(s,i,i,1); 570*2139Sjp161948 if (n <= 0) return(n); /* error or non-blocking io */ 571*2139Sjp161948 572*2139Sjp161948 /* this packet contained a partial record, dump it */ 573*2139Sjp161948 if ( n != i) 574*2139Sjp161948 { 575*2139Sjp161948 s->packet_length = 0; 576*2139Sjp161948 goto again; 577*2139Sjp161948 } 578*2139Sjp161948 579*2139Sjp161948 /* now n == rr->length, 580*2139Sjp161948 * and s->packet_length == DTLS1_RT_HEADER_LENGTH + rr->length */ 581*2139Sjp161948 } 582*2139Sjp161948 s->rstate=SSL_ST_READ_HEADER; /* set state for later operations */ 583*2139Sjp161948 584*2139Sjp161948 /* match epochs. NULL means the packet is dropped on the floor */ 585*2139Sjp161948 bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch); 586*2139Sjp161948 if ( bitmap == NULL) 587*2139Sjp161948 { 588*2139Sjp161948 s->packet_length = 0; /* dump this record */ 589*2139Sjp161948 goto again; /* get another record */ 590*2139Sjp161948 } 591*2139Sjp161948 592*2139Sjp161948 /* check whether this is a repeat, or aged record */ 593*2139Sjp161948 if ( ! dtls1_record_replay_check(s, bitmap, &(rr->seq_num))) 594*2139Sjp161948 { 595*2139Sjp161948 s->packet_length=0; /* dump this record */ 596*2139Sjp161948 goto again; /* get another record */ 597*2139Sjp161948 } 598*2139Sjp161948 599*2139Sjp161948 /* just read a 0 length packet */ 600*2139Sjp161948 if (rr->length == 0) goto again; 601*2139Sjp161948 602*2139Sjp161948 /* If this record is from the next epoch (either HM or ALERT), buffer it 603*2139Sjp161948 * since it cannot be processed at this time. 604*2139Sjp161948 * Records from the next epoch are marked as received even though they are 605*2139Sjp161948 * not processed, so as to prevent any potential resource DoS attack */ 606*2139Sjp161948 if (is_next_epoch) 607*2139Sjp161948 { 608*2139Sjp161948 dtls1_record_bitmap_update(s, bitmap); 609*2139Sjp161948 dtls1_buffer_record(s, &(s->d1->unprocessed_rcds), rr->seq_num); 610*2139Sjp161948 s->packet_length = 0; 611*2139Sjp161948 goto again; 612*2139Sjp161948 } 613*2139Sjp161948 614*2139Sjp161948 if ( ! dtls1_process_record(s)) 615*2139Sjp161948 return(0); 616*2139Sjp161948 617*2139Sjp161948 dtls1_clear_timeouts(s); /* done waiting */ 618*2139Sjp161948 return(1); 619*2139Sjp161948 620*2139Sjp161948 f_err: 621*2139Sjp161948 ssl3_send_alert(s,SSL3_AL_FATAL,al); 622*2139Sjp161948 err: 623*2139Sjp161948 return(0); 624*2139Sjp161948 } 625*2139Sjp161948 626*2139Sjp161948 /* Return up to 'len' payload bytes received in 'type' records. 627*2139Sjp161948 * 'type' is one of the following: 628*2139Sjp161948 * 629*2139Sjp161948 * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us) 630*2139Sjp161948 * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us) 631*2139Sjp161948 * - 0 (during a shutdown, no data has to be returned) 632*2139Sjp161948 * 633*2139Sjp161948 * If we don't have stored data to work from, read a SSL/TLS record first 634*2139Sjp161948 * (possibly multiple records if we still don't have anything to return). 635*2139Sjp161948 * 636*2139Sjp161948 * This function must handle any surprises the peer may have for us, such as 637*2139Sjp161948 * Alert records (e.g. close_notify), ChangeCipherSpec records (not really 638*2139Sjp161948 * a surprise, but handled as if it were), or renegotiation requests. 639*2139Sjp161948 * Also if record payloads contain fragments too small to process, we store 640*2139Sjp161948 * them until there is enough for the respective protocol (the record protocol 641*2139Sjp161948 * may use arbitrary fragmentation and even interleaving): 642*2139Sjp161948 * Change cipher spec protocol 643*2139Sjp161948 * just 1 byte needed, no need for keeping anything stored 644*2139Sjp161948 * Alert protocol 645*2139Sjp161948 * 2 bytes needed (AlertLevel, AlertDescription) 646*2139Sjp161948 * Handshake protocol 647*2139Sjp161948 * 4 bytes needed (HandshakeType, uint24 length) -- we just have 648*2139Sjp161948 * to detect unexpected Client Hello and Hello Request messages 649*2139Sjp161948 * here, anything else is handled by higher layers 650*2139Sjp161948 * Application data protocol 651*2139Sjp161948 * none of our business 652*2139Sjp161948 */ 653*2139Sjp161948 int dtls1_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek) 654*2139Sjp161948 { 655*2139Sjp161948 int al,i,j,ret; 656*2139Sjp161948 unsigned int n; 657*2139Sjp161948 SSL3_RECORD *rr; 658*2139Sjp161948 void (*cb)(const SSL *ssl,int type2,int val)=NULL; 659*2139Sjp161948 660*2139Sjp161948 if (s->s3->rbuf.buf == NULL) /* Not initialized yet */ 661*2139Sjp161948 if (!ssl3_setup_buffers(s)) 662*2139Sjp161948 return(-1); 663*2139Sjp161948 664*2139Sjp161948 /* XXX: check what the second '&& type' is about */ 665*2139Sjp161948 if ((type && (type != SSL3_RT_APPLICATION_DATA) && 666*2139Sjp161948 (type != SSL3_RT_HANDSHAKE) && type) || 667*2139Sjp161948 (peek && (type != SSL3_RT_APPLICATION_DATA))) 668*2139Sjp161948 { 669*2139Sjp161948 SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR); 670*2139Sjp161948 return -1; 671*2139Sjp161948 } 672*2139Sjp161948 673*2139Sjp161948 /* check whether there's a handshake message (client hello?) waiting */ 674*2139Sjp161948 if ( (ret = have_handshake_fragment(s, type, buf, len, peek))) 675*2139Sjp161948 return ret; 676*2139Sjp161948 677*2139Sjp161948 /* Now s->d1->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE. */ 678*2139Sjp161948 679*2139Sjp161948 if (!s->in_handshake && SSL_in_init(s)) 680*2139Sjp161948 { 681*2139Sjp161948 /* type == SSL3_RT_APPLICATION_DATA */ 682*2139Sjp161948 i=s->handshake_func(s); 683*2139Sjp161948 if (i < 0) return(i); 684*2139Sjp161948 if (i == 0) 685*2139Sjp161948 { 686*2139Sjp161948 SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE); 687*2139Sjp161948 return(-1); 688*2139Sjp161948 } 689*2139Sjp161948 } 690*2139Sjp161948 691*2139Sjp161948 start: 692*2139Sjp161948 s->rwstate=SSL_NOTHING; 693*2139Sjp161948 694*2139Sjp161948 /* s->s3->rrec.type - is the type of record 695*2139Sjp161948 * s->s3->rrec.data, - data 696*2139Sjp161948 * s->s3->rrec.off, - offset into 'data' for next read 697*2139Sjp161948 * s->s3->rrec.length, - number of bytes. */ 698*2139Sjp161948 rr = &(s->s3->rrec); 699*2139Sjp161948 700*2139Sjp161948 /* get new packet if necessary */ 701*2139Sjp161948 if ((rr->length == 0) || (s->rstate == SSL_ST_READ_BODY)) 702*2139Sjp161948 { 703*2139Sjp161948 ret=dtls1_get_record(s); 704*2139Sjp161948 if (ret <= 0) 705*2139Sjp161948 { 706*2139Sjp161948 ret = dtls1_read_failed(s, ret); 707*2139Sjp161948 /* anything other than a timeout is an error */ 708*2139Sjp161948 if (ret <= 0) 709*2139Sjp161948 return(ret); 710*2139Sjp161948 else 711*2139Sjp161948 goto start; 712*2139Sjp161948 } 713*2139Sjp161948 } 714*2139Sjp161948 715*2139Sjp161948 /* we now have a packet which can be read and processed */ 716*2139Sjp161948 717*2139Sjp161948 if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec, 718*2139Sjp161948 * reset by ssl3_get_finished */ 719*2139Sjp161948 && (rr->type != SSL3_RT_HANDSHAKE)) 720*2139Sjp161948 { 721*2139Sjp161948 al=SSL_AD_UNEXPECTED_MESSAGE; 722*2139Sjp161948 SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_DATA_BETWEEN_CCS_AND_FINISHED); 723*2139Sjp161948 goto err; 724*2139Sjp161948 } 725*2139Sjp161948 726*2139Sjp161948 /* If the other end has shut down, throw anything we read away 727*2139Sjp161948 * (even in 'peek' mode) */ 728*2139Sjp161948 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) 729*2139Sjp161948 { 730*2139Sjp161948 rr->length=0; 731*2139Sjp161948 s->rwstate=SSL_NOTHING; 732*2139Sjp161948 return(0); 733*2139Sjp161948 } 734*2139Sjp161948 735*2139Sjp161948 736*2139Sjp161948 if (type == rr->type) /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */ 737*2139Sjp161948 { 738*2139Sjp161948 /* make sure that we are not getting application data when we 739*2139Sjp161948 * are doing a handshake for the first time */ 740*2139Sjp161948 if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) && 741*2139Sjp161948 (s->enc_read_ctx == NULL)) 742*2139Sjp161948 { 743*2139Sjp161948 al=SSL_AD_UNEXPECTED_MESSAGE; 744*2139Sjp161948 SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_APP_DATA_IN_HANDSHAKE); 745*2139Sjp161948 goto f_err; 746*2139Sjp161948 } 747*2139Sjp161948 748*2139Sjp161948 if (len <= 0) return(len); 749*2139Sjp161948 750*2139Sjp161948 if ((unsigned int)len > rr->length) 751*2139Sjp161948 n = rr->length; 752*2139Sjp161948 else 753*2139Sjp161948 n = (unsigned int)len; 754*2139Sjp161948 755*2139Sjp161948 memcpy(buf,&(rr->data[rr->off]),n); 756*2139Sjp161948 if (!peek) 757*2139Sjp161948 { 758*2139Sjp161948 rr->length-=n; 759*2139Sjp161948 rr->off+=n; 760*2139Sjp161948 if (rr->length == 0) 761*2139Sjp161948 { 762*2139Sjp161948 s->rstate=SSL_ST_READ_HEADER; 763*2139Sjp161948 rr->off=0; 764*2139Sjp161948 } 765*2139Sjp161948 } 766*2139Sjp161948 return(n); 767*2139Sjp161948 } 768*2139Sjp161948 769*2139Sjp161948 770*2139Sjp161948 /* If we get here, then type != rr->type; if we have a handshake 771*2139Sjp161948 * message, then it was unexpected (Hello Request or Client Hello). */ 772*2139Sjp161948 773*2139Sjp161948 /* In case of record types for which we have 'fragment' storage, 774*2139Sjp161948 * fill that so that we can process the data at a fixed place. 775*2139Sjp161948 */ 776*2139Sjp161948 { 777*2139Sjp161948 unsigned int k, dest_maxlen = 0; 778*2139Sjp161948 unsigned char *dest = NULL; 779*2139Sjp161948 unsigned int *dest_len = NULL; 780*2139Sjp161948 781*2139Sjp161948 if (rr->type == SSL3_RT_HANDSHAKE) 782*2139Sjp161948 { 783*2139Sjp161948 dest_maxlen = sizeof s->d1->handshake_fragment; 784*2139Sjp161948 dest = s->d1->handshake_fragment; 785*2139Sjp161948 dest_len = &s->d1->handshake_fragment_len; 786*2139Sjp161948 } 787*2139Sjp161948 else if (rr->type == SSL3_RT_ALERT) 788*2139Sjp161948 { 789*2139Sjp161948 dest_maxlen = sizeof(s->d1->alert_fragment); 790*2139Sjp161948 dest = s->d1->alert_fragment; 791*2139Sjp161948 dest_len = &s->d1->alert_fragment_len; 792*2139Sjp161948 } 793*2139Sjp161948 else /* else it's a CCS message */ 794*2139Sjp161948 OPENSSL_assert(rr->type == SSL3_RT_CHANGE_CIPHER_SPEC); 795*2139Sjp161948 796*2139Sjp161948 797*2139Sjp161948 if (dest_maxlen > 0) 798*2139Sjp161948 { 799*2139Sjp161948 /* XDTLS: In a pathalogical case, the Client Hello 800*2139Sjp161948 * may be fragmented--don't always expect dest_maxlen bytes */ 801*2139Sjp161948 if ( rr->length < dest_maxlen) 802*2139Sjp161948 { 803*2139Sjp161948 s->rstate=SSL_ST_READ_HEADER; 804*2139Sjp161948 rr->length = 0; 805*2139Sjp161948 goto start; 806*2139Sjp161948 } 807*2139Sjp161948 808*2139Sjp161948 /* now move 'n' bytes: */ 809*2139Sjp161948 for ( k = 0; k < dest_maxlen; k++) 810*2139Sjp161948 { 811*2139Sjp161948 dest[k] = rr->data[rr->off++]; 812*2139Sjp161948 rr->length--; 813*2139Sjp161948 } 814*2139Sjp161948 *dest_len = dest_maxlen; 815*2139Sjp161948 } 816*2139Sjp161948 } 817*2139Sjp161948 818*2139Sjp161948 /* s->d1->handshake_fragment_len == 12 iff rr->type == SSL3_RT_HANDSHAKE; 819*2139Sjp161948 * s->d1->alert_fragment_len == 7 iff rr->type == SSL3_RT_ALERT. 820*2139Sjp161948 * (Possibly rr is 'empty' now, i.e. rr->length may be 0.) */ 821*2139Sjp161948 822*2139Sjp161948 /* If we are a client, check for an incoming 'Hello Request': */ 823*2139Sjp161948 if ((!s->server) && 824*2139Sjp161948 (s->d1->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH) && 825*2139Sjp161948 (s->d1->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST) && 826*2139Sjp161948 (s->session != NULL) && (s->session->cipher != NULL)) 827*2139Sjp161948 { 828*2139Sjp161948 s->d1->handshake_fragment_len = 0; 829*2139Sjp161948 830*2139Sjp161948 if ((s->d1->handshake_fragment[1] != 0) || 831*2139Sjp161948 (s->d1->handshake_fragment[2] != 0) || 832*2139Sjp161948 (s->d1->handshake_fragment[3] != 0)) 833*2139Sjp161948 { 834*2139Sjp161948 al=SSL_AD_DECODE_ERROR; 835*2139Sjp161948 SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_BAD_HELLO_REQUEST); 836*2139Sjp161948 goto err; 837*2139Sjp161948 } 838*2139Sjp161948 839*2139Sjp161948 /* no need to check sequence number on HELLO REQUEST messages */ 840*2139Sjp161948 841*2139Sjp161948 if (s->msg_callback) 842*2139Sjp161948 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, 843*2139Sjp161948 s->d1->handshake_fragment, 4, s, s->msg_callback_arg); 844*2139Sjp161948 845*2139Sjp161948 if (SSL_is_init_finished(s) && 846*2139Sjp161948 !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) && 847*2139Sjp161948 !s->s3->renegotiate) 848*2139Sjp161948 { 849*2139Sjp161948 ssl3_renegotiate(s); 850*2139Sjp161948 if (ssl3_renegotiate_check(s)) 851*2139Sjp161948 { 852*2139Sjp161948 i=s->handshake_func(s); 853*2139Sjp161948 if (i < 0) return(i); 854*2139Sjp161948 if (i == 0) 855*2139Sjp161948 { 856*2139Sjp161948 SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE); 857*2139Sjp161948 return(-1); 858*2139Sjp161948 } 859*2139Sjp161948 860*2139Sjp161948 if (!(s->mode & SSL_MODE_AUTO_RETRY)) 861*2139Sjp161948 { 862*2139Sjp161948 if (s->s3->rbuf.left == 0) /* no read-ahead left? */ 863*2139Sjp161948 { 864*2139Sjp161948 BIO *bio; 865*2139Sjp161948 /* In the case where we try to read application data, 866*2139Sjp161948 * but we trigger an SSL handshake, we return -1 with 867*2139Sjp161948 * the retry option set. Otherwise renegotiation may 868*2139Sjp161948 * cause nasty problems in the blocking world */ 869*2139Sjp161948 s->rwstate=SSL_READING; 870*2139Sjp161948 bio=SSL_get_rbio(s); 871*2139Sjp161948 BIO_clear_retry_flags(bio); 872*2139Sjp161948 BIO_set_retry_read(bio); 873*2139Sjp161948 return(-1); 874*2139Sjp161948 } 875*2139Sjp161948 } 876*2139Sjp161948 } 877*2139Sjp161948 } 878*2139Sjp161948 /* we either finished a handshake or ignored the request, 879*2139Sjp161948 * now try again to obtain the (application) data we were asked for */ 880*2139Sjp161948 goto start; 881*2139Sjp161948 } 882*2139Sjp161948 883*2139Sjp161948 if (s->d1->alert_fragment_len >= DTLS1_AL_HEADER_LENGTH) 884*2139Sjp161948 { 885*2139Sjp161948 int alert_level = s->d1->alert_fragment[0]; 886*2139Sjp161948 int alert_descr = s->d1->alert_fragment[1]; 887*2139Sjp161948 888*2139Sjp161948 s->d1->alert_fragment_len = 0; 889*2139Sjp161948 890*2139Sjp161948 if (s->msg_callback) 891*2139Sjp161948 s->msg_callback(0, s->version, SSL3_RT_ALERT, 892*2139Sjp161948 s->d1->alert_fragment, 2, s, s->msg_callback_arg); 893*2139Sjp161948 894*2139Sjp161948 if (s->info_callback != NULL) 895*2139Sjp161948 cb=s->info_callback; 896*2139Sjp161948 else if (s->ctx->info_callback != NULL) 897*2139Sjp161948 cb=s->ctx->info_callback; 898*2139Sjp161948 899*2139Sjp161948 if (cb != NULL) 900*2139Sjp161948 { 901*2139Sjp161948 j = (alert_level << 8) | alert_descr; 902*2139Sjp161948 cb(s, SSL_CB_READ_ALERT, j); 903*2139Sjp161948 } 904*2139Sjp161948 905*2139Sjp161948 if (alert_level == 1) /* warning */ 906*2139Sjp161948 { 907*2139Sjp161948 s->s3->warn_alert = alert_descr; 908*2139Sjp161948 if (alert_descr == SSL_AD_CLOSE_NOTIFY) 909*2139Sjp161948 { 910*2139Sjp161948 s->shutdown |= SSL_RECEIVED_SHUTDOWN; 911*2139Sjp161948 return(0); 912*2139Sjp161948 } 913*2139Sjp161948 #if 0 914*2139Sjp161948 /* XXX: this is a possible improvement in the future */ 915*2139Sjp161948 /* now check if it's a missing record */ 916*2139Sjp161948 if (alert_descr == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE) 917*2139Sjp161948 { 918*2139Sjp161948 unsigned short seq; 919*2139Sjp161948 unsigned int frag_off; 920*2139Sjp161948 unsigned char *p = &(s->d1->alert_fragment[2]); 921*2139Sjp161948 922*2139Sjp161948 n2s(p, seq); 923*2139Sjp161948 n2l3(p, frag_off); 924*2139Sjp161948 925*2139Sjp161948 dtls1_retransmit_message(s, seq, frag_off, &found); 926*2139Sjp161948 if ( ! found && SSL_in_init(s)) 927*2139Sjp161948 { 928*2139Sjp161948 /* fprintf( stderr,"in init = %d\n", SSL_in_init(s)); */ 929*2139Sjp161948 /* requested a message not yet sent, 930*2139Sjp161948 send an alert ourselves */ 931*2139Sjp161948 ssl3_send_alert(s,SSL3_AL_WARNING, 932*2139Sjp161948 DTLS1_AD_MISSING_HANDSHAKE_MESSAGE); 933*2139Sjp161948 } 934*2139Sjp161948 } 935*2139Sjp161948 #endif 936*2139Sjp161948 } 937*2139Sjp161948 else if (alert_level == 2) /* fatal */ 938*2139Sjp161948 { 939*2139Sjp161948 char tmp[16]; 940*2139Sjp161948 941*2139Sjp161948 s->rwstate=SSL_NOTHING; 942*2139Sjp161948 s->s3->fatal_alert = alert_descr; 943*2139Sjp161948 SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_AD_REASON_OFFSET + alert_descr); 944*2139Sjp161948 BIO_snprintf(tmp,sizeof tmp,"%d",alert_descr); 945*2139Sjp161948 ERR_add_error_data(2,"SSL alert number ",tmp); 946*2139Sjp161948 s->shutdown|=SSL_RECEIVED_SHUTDOWN; 947*2139Sjp161948 SSL_CTX_remove_session(s->ctx,s->session); 948*2139Sjp161948 return(0); 949*2139Sjp161948 } 950*2139Sjp161948 else 951*2139Sjp161948 { 952*2139Sjp161948 al=SSL_AD_ILLEGAL_PARAMETER; 953*2139Sjp161948 SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_UNKNOWN_ALERT_TYPE); 954*2139Sjp161948 goto f_err; 955*2139Sjp161948 } 956*2139Sjp161948 957*2139Sjp161948 goto start; 958*2139Sjp161948 } 959*2139Sjp161948 960*2139Sjp161948 if (s->shutdown & SSL_SENT_SHUTDOWN) /* but we have not received a shutdown */ 961*2139Sjp161948 { 962*2139Sjp161948 s->rwstate=SSL_NOTHING; 963*2139Sjp161948 rr->length=0; 964*2139Sjp161948 return(0); 965*2139Sjp161948 } 966*2139Sjp161948 967*2139Sjp161948 if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) 968*2139Sjp161948 { 969*2139Sjp161948 struct ccs_header_st ccs_hdr; 970*2139Sjp161948 971*2139Sjp161948 dtls1_get_ccs_header(rr->data, &ccs_hdr); 972*2139Sjp161948 973*2139Sjp161948 if ( ccs_hdr.seq == s->d1->handshake_read_seq) 974*2139Sjp161948 { 975*2139Sjp161948 /* 'Change Cipher Spec' is just a single byte, so we know 976*2139Sjp161948 * exactly what the record payload has to look like */ 977*2139Sjp161948 /* XDTLS: check that epoch is consistent */ 978*2139Sjp161948 if ( (rr->length != DTLS1_CCS_HEADER_LENGTH) || 979*2139Sjp161948 (rr->off != 0) || (rr->data[0] != SSL3_MT_CCS)) 980*2139Sjp161948 { 981*2139Sjp161948 i=SSL_AD_ILLEGAL_PARAMETER; 982*2139Sjp161948 SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_BAD_CHANGE_CIPHER_SPEC); 983*2139Sjp161948 goto err; 984*2139Sjp161948 } 985*2139Sjp161948 986*2139Sjp161948 rr->length=0; 987*2139Sjp161948 988*2139Sjp161948 if (s->msg_callback) 989*2139Sjp161948 s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, 990*2139Sjp161948 rr->data, 1, s, s->msg_callback_arg); 991*2139Sjp161948 992*2139Sjp161948 s->s3->change_cipher_spec=1; 993*2139Sjp161948 if (!ssl3_do_change_cipher_spec(s)) 994*2139Sjp161948 goto err; 995*2139Sjp161948 996*2139Sjp161948 /* do this whenever CCS is processed */ 997*2139Sjp161948 dtls1_reset_seq_numbers(s, SSL3_CC_READ); 998*2139Sjp161948 999*2139Sjp161948 /* handshake read seq is reset upon handshake completion */ 1000*2139Sjp161948 s->d1->handshake_read_seq++; 1001*2139Sjp161948 1002*2139Sjp161948 goto start; 1003*2139Sjp161948 } 1004*2139Sjp161948 else 1005*2139Sjp161948 { 1006*2139Sjp161948 rr->length = 0; 1007*2139Sjp161948 goto start; 1008*2139Sjp161948 } 1009*2139Sjp161948 } 1010*2139Sjp161948 1011*2139Sjp161948 /* Unexpected handshake message (Client Hello, or protocol violation) */ 1012*2139Sjp161948 if ((s->d1->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH) && 1013*2139Sjp161948 !s->in_handshake) 1014*2139Sjp161948 { 1015*2139Sjp161948 struct hm_header_st msg_hdr; 1016*2139Sjp161948 1017*2139Sjp161948 /* this may just be a stale retransmit */ 1018*2139Sjp161948 dtls1_get_message_header(rr->data, &msg_hdr); 1019*2139Sjp161948 if( rr->epoch != s->d1->r_epoch) 1020*2139Sjp161948 { 1021*2139Sjp161948 rr->length = 0; 1022*2139Sjp161948 goto start; 1023*2139Sjp161948 } 1024*2139Sjp161948 1025*2139Sjp161948 if (((s->state&SSL_ST_MASK) == SSL_ST_OK) && 1026*2139Sjp161948 !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)) 1027*2139Sjp161948 { 1028*2139Sjp161948 #if 0 /* worked only because C operator preferences are not as expected (and 1029*2139Sjp161948 * because this is not really needed for clients except for detecting 1030*2139Sjp161948 * protocol violations): */ 1031*2139Sjp161948 s->state=SSL_ST_BEFORE|(s->server) 1032*2139Sjp161948 ?SSL_ST_ACCEPT 1033*2139Sjp161948 :SSL_ST_CONNECT; 1034*2139Sjp161948 #else 1035*2139Sjp161948 s->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT; 1036*2139Sjp161948 #endif 1037*2139Sjp161948 s->new_session=1; 1038*2139Sjp161948 } 1039*2139Sjp161948 i=s->handshake_func(s); 1040*2139Sjp161948 if (i < 0) return(i); 1041*2139Sjp161948 if (i == 0) 1042*2139Sjp161948 { 1043*2139Sjp161948 SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE); 1044*2139Sjp161948 return(-1); 1045*2139Sjp161948 } 1046*2139Sjp161948 1047*2139Sjp161948 if (!(s->mode & SSL_MODE_AUTO_RETRY)) 1048*2139Sjp161948 { 1049*2139Sjp161948 if (s->s3->rbuf.left == 0) /* no read-ahead left? */ 1050*2139Sjp161948 { 1051*2139Sjp161948 BIO *bio; 1052*2139Sjp161948 /* In the case where we try to read application data, 1053*2139Sjp161948 * but we trigger an SSL handshake, we return -1 with 1054*2139Sjp161948 * the retry option set. Otherwise renegotiation may 1055*2139Sjp161948 * cause nasty problems in the blocking world */ 1056*2139Sjp161948 s->rwstate=SSL_READING; 1057*2139Sjp161948 bio=SSL_get_rbio(s); 1058*2139Sjp161948 BIO_clear_retry_flags(bio); 1059*2139Sjp161948 BIO_set_retry_read(bio); 1060*2139Sjp161948 return(-1); 1061*2139Sjp161948 } 1062*2139Sjp161948 } 1063*2139Sjp161948 goto start; 1064*2139Sjp161948 } 1065*2139Sjp161948 1066*2139Sjp161948 switch (rr->type) 1067*2139Sjp161948 { 1068*2139Sjp161948 default: 1069*2139Sjp161948 #ifndef OPENSSL_NO_TLS 1070*2139Sjp161948 /* TLS just ignores unknown message types */ 1071*2139Sjp161948 if (s->version == TLS1_VERSION) 1072*2139Sjp161948 { 1073*2139Sjp161948 rr->length = 0; 1074*2139Sjp161948 goto start; 1075*2139Sjp161948 } 1076*2139Sjp161948 #endif 1077*2139Sjp161948 al=SSL_AD_UNEXPECTED_MESSAGE; 1078*2139Sjp161948 SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_UNEXPECTED_RECORD); 1079*2139Sjp161948 goto f_err; 1080*2139Sjp161948 case SSL3_RT_CHANGE_CIPHER_SPEC: 1081*2139Sjp161948 case SSL3_RT_ALERT: 1082*2139Sjp161948 case SSL3_RT_HANDSHAKE: 1083*2139Sjp161948 /* we already handled all of these, with the possible exception 1084*2139Sjp161948 * of SSL3_RT_HANDSHAKE when s->in_handshake is set, but that 1085*2139Sjp161948 * should not happen when type != rr->type */ 1086*2139Sjp161948 al=SSL_AD_UNEXPECTED_MESSAGE; 1087*2139Sjp161948 SSLerr(SSL_F_DTLS1_READ_BYTES,ERR_R_INTERNAL_ERROR); 1088*2139Sjp161948 goto f_err; 1089*2139Sjp161948 case SSL3_RT_APPLICATION_DATA: 1090*2139Sjp161948 /* At this point, we were expecting handshake data, 1091*2139Sjp161948 * but have application data. If the library was 1092*2139Sjp161948 * running inside ssl3_read() (i.e. in_read_app_data 1093*2139Sjp161948 * is set) and it makes sense to read application data 1094*2139Sjp161948 * at this point (session renegotiation not yet started), 1095*2139Sjp161948 * we will indulge it. 1096*2139Sjp161948 */ 1097*2139Sjp161948 if (s->s3->in_read_app_data && 1098*2139Sjp161948 (s->s3->total_renegotiations != 0) && 1099*2139Sjp161948 (( 1100*2139Sjp161948 (s->state & SSL_ST_CONNECT) && 1101*2139Sjp161948 (s->state >= SSL3_ST_CW_CLNT_HELLO_A) && 1102*2139Sjp161948 (s->state <= SSL3_ST_CR_SRVR_HELLO_A) 1103*2139Sjp161948 ) || ( 1104*2139Sjp161948 (s->state & SSL_ST_ACCEPT) && 1105*2139Sjp161948 (s->state <= SSL3_ST_SW_HELLO_REQ_A) && 1106*2139Sjp161948 (s->state >= SSL3_ST_SR_CLNT_HELLO_A) 1107*2139Sjp161948 ) 1108*2139Sjp161948 )) 1109*2139Sjp161948 { 1110*2139Sjp161948 s->s3->in_read_app_data=2; 1111*2139Sjp161948 return(-1); 1112*2139Sjp161948 } 1113*2139Sjp161948 else 1114*2139Sjp161948 { 1115*2139Sjp161948 al=SSL_AD_UNEXPECTED_MESSAGE; 1116*2139Sjp161948 SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_UNEXPECTED_RECORD); 1117*2139Sjp161948 goto f_err; 1118*2139Sjp161948 } 1119*2139Sjp161948 } 1120*2139Sjp161948 /* not reached */ 1121*2139Sjp161948 1122*2139Sjp161948 f_err: 1123*2139Sjp161948 ssl3_send_alert(s,SSL3_AL_FATAL,al); 1124*2139Sjp161948 err: 1125*2139Sjp161948 return(-1); 1126*2139Sjp161948 } 1127*2139Sjp161948 1128*2139Sjp161948 int 1129*2139Sjp161948 dtls1_write_app_data_bytes(SSL *s, int type, const void *buf_, int len) 1130*2139Sjp161948 { 1131*2139Sjp161948 unsigned int n,tot; 1132*2139Sjp161948 int i; 1133*2139Sjp161948 1134*2139Sjp161948 if (SSL_in_init(s) && !s->in_handshake) 1135*2139Sjp161948 { 1136*2139Sjp161948 i=s->handshake_func(s); 1137*2139Sjp161948 if (i < 0) return(i); 1138*2139Sjp161948 if (i == 0) 1139*2139Sjp161948 { 1140*2139Sjp161948 SSLerr(SSL_F_DTLS1_WRITE_APP_DATA_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE); 1141*2139Sjp161948 return -1; 1142*2139Sjp161948 } 1143*2139Sjp161948 } 1144*2139Sjp161948 1145*2139Sjp161948 tot = s->s3->wnum; 1146*2139Sjp161948 n = len - tot; 1147*2139Sjp161948 1148*2139Sjp161948 while( n) 1149*2139Sjp161948 { 1150*2139Sjp161948 /* dtls1_write_bytes sends one record at a time, sized according to 1151*2139Sjp161948 * the currently known MTU */ 1152*2139Sjp161948 i = dtls1_write_bytes(s, type, buf_, len); 1153*2139Sjp161948 if (i <= 0) return i; 1154*2139Sjp161948 1155*2139Sjp161948 if ((i == (int)n) || 1156*2139Sjp161948 (type == SSL3_RT_APPLICATION_DATA && 1157*2139Sjp161948 (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) 1158*2139Sjp161948 { 1159*2139Sjp161948 /* next chunk of data should get another prepended empty fragment 1160*2139Sjp161948 * in ciphersuites with known-IV weakness: */ 1161*2139Sjp161948 s->s3->empty_fragment_done = 0; 1162*2139Sjp161948 return tot+i; 1163*2139Sjp161948 } 1164*2139Sjp161948 1165*2139Sjp161948 tot += i; 1166*2139Sjp161948 n-=i; 1167*2139Sjp161948 } 1168*2139Sjp161948 1169*2139Sjp161948 return tot; 1170*2139Sjp161948 } 1171*2139Sjp161948 1172*2139Sjp161948 1173*2139Sjp161948 /* this only happens when a client hello is received and a handshake 1174*2139Sjp161948 * is started. */ 1175*2139Sjp161948 static int 1176*2139Sjp161948 have_handshake_fragment(SSL *s, int type, unsigned char *buf, 1177*2139Sjp161948 int len, int peek) 1178*2139Sjp161948 { 1179*2139Sjp161948 1180*2139Sjp161948 if ((type == SSL3_RT_HANDSHAKE) && (s->d1->handshake_fragment_len > 0)) 1181*2139Sjp161948 /* (partially) satisfy request from storage */ 1182*2139Sjp161948 { 1183*2139Sjp161948 unsigned char *src = s->d1->handshake_fragment; 1184*2139Sjp161948 unsigned char *dst = buf; 1185*2139Sjp161948 unsigned int k,n; 1186*2139Sjp161948 1187*2139Sjp161948 /* peek == 0 */ 1188*2139Sjp161948 n = 0; 1189*2139Sjp161948 while ((len > 0) && (s->d1->handshake_fragment_len > 0)) 1190*2139Sjp161948 { 1191*2139Sjp161948 *dst++ = *src++; 1192*2139Sjp161948 len--; s->d1->handshake_fragment_len--; 1193*2139Sjp161948 n++; 1194*2139Sjp161948 } 1195*2139Sjp161948 /* move any remaining fragment bytes: */ 1196*2139Sjp161948 for (k = 0; k < s->d1->handshake_fragment_len; k++) 1197*2139Sjp161948 s->d1->handshake_fragment[k] = *src++; 1198*2139Sjp161948 return n; 1199*2139Sjp161948 } 1200*2139Sjp161948 1201*2139Sjp161948 return 0; 1202*2139Sjp161948 } 1203*2139Sjp161948 1204*2139Sjp161948 1205*2139Sjp161948 1206*2139Sjp161948 1207*2139Sjp161948 /* Call this to write data in records of type 'type' 1208*2139Sjp161948 * It will return <= 0 if not all data has been sent or non-blocking IO. 1209*2139Sjp161948 */ 1210*2139Sjp161948 int dtls1_write_bytes(SSL *s, int type, const void *buf_, int len) 1211*2139Sjp161948 { 1212*2139Sjp161948 const unsigned char *buf=buf_; 1213*2139Sjp161948 unsigned int tot,n,nw; 1214*2139Sjp161948 int i; 1215*2139Sjp161948 unsigned int mtu; 1216*2139Sjp161948 1217*2139Sjp161948 s->rwstate=SSL_NOTHING; 1218*2139Sjp161948 tot=s->s3->wnum; 1219*2139Sjp161948 1220*2139Sjp161948 n=(len-tot); 1221*2139Sjp161948 1222*2139Sjp161948 /* handshake layer figures out MTU for itself, but data records 1223*2139Sjp161948 * are also sent through this interface, so need to figure out MTU */ 1224*2139Sjp161948 #if 0 1225*2139Sjp161948 mtu = BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_GET_MTU, 0, NULL); 1226*2139Sjp161948 mtu += DTLS1_HM_HEADER_LENGTH; /* HM already inserted */ 1227*2139Sjp161948 #endif 1228*2139Sjp161948 mtu = s->d1->mtu; 1229*2139Sjp161948 1230*2139Sjp161948 if (mtu > SSL3_RT_MAX_PLAIN_LENGTH) 1231*2139Sjp161948 mtu = SSL3_RT_MAX_PLAIN_LENGTH; 1232*2139Sjp161948 1233*2139Sjp161948 if (n > mtu) 1234*2139Sjp161948 nw=mtu; 1235*2139Sjp161948 else 1236*2139Sjp161948 nw=n; 1237*2139Sjp161948 1238*2139Sjp161948 i=do_dtls1_write(s, type, &(buf[tot]), nw, 0); 1239*2139Sjp161948 if (i <= 0) 1240*2139Sjp161948 { 1241*2139Sjp161948 s->s3->wnum=tot; 1242*2139Sjp161948 return i; 1243*2139Sjp161948 } 1244*2139Sjp161948 1245*2139Sjp161948 if ( (int)s->s3->wnum + i == len) 1246*2139Sjp161948 s->s3->wnum = 0; 1247*2139Sjp161948 else 1248*2139Sjp161948 s->s3->wnum += i; 1249*2139Sjp161948 1250*2139Sjp161948 return tot + i; 1251*2139Sjp161948 } 1252*2139Sjp161948 1253*2139Sjp161948 int do_dtls1_write(SSL *s, int type, const unsigned char *buf, unsigned int len, int create_empty_fragment) 1254*2139Sjp161948 { 1255*2139Sjp161948 unsigned char *p,*pseq; 1256*2139Sjp161948 int i,mac_size,clear=0; 1257*2139Sjp161948 int prefix_len = 0; 1258*2139Sjp161948 SSL3_RECORD *wr; 1259*2139Sjp161948 SSL3_BUFFER *wb; 1260*2139Sjp161948 SSL_SESSION *sess; 1261*2139Sjp161948 int bs; 1262*2139Sjp161948 1263*2139Sjp161948 /* first check if there is a SSL3_BUFFER still being written 1264*2139Sjp161948 * out. This will happen with non blocking IO */ 1265*2139Sjp161948 if (s->s3->wbuf.left != 0) 1266*2139Sjp161948 { 1267*2139Sjp161948 OPENSSL_assert(0); /* XDTLS: want to see if we ever get here */ 1268*2139Sjp161948 return(ssl3_write_pending(s,type,buf,len)); 1269*2139Sjp161948 } 1270*2139Sjp161948 1271*2139Sjp161948 /* If we have an alert to send, lets send it */ 1272*2139Sjp161948 if (s->s3->alert_dispatch) 1273*2139Sjp161948 { 1274*2139Sjp161948 i=s->method->ssl_dispatch_alert(s); 1275*2139Sjp161948 if (i <= 0) 1276*2139Sjp161948 return(i); 1277*2139Sjp161948 /* if it went, fall through and send more stuff */ 1278*2139Sjp161948 } 1279*2139Sjp161948 1280*2139Sjp161948 if (len == 0 && !create_empty_fragment) 1281*2139Sjp161948 return 0; 1282*2139Sjp161948 1283*2139Sjp161948 wr= &(s->s3->wrec); 1284*2139Sjp161948 wb= &(s->s3->wbuf); 1285*2139Sjp161948 sess=s->session; 1286*2139Sjp161948 1287*2139Sjp161948 if ( (sess == NULL) || 1288*2139Sjp161948 (s->enc_write_ctx == NULL) || 1289*2139Sjp161948 (s->write_hash == NULL)) 1290*2139Sjp161948 clear=1; 1291*2139Sjp161948 1292*2139Sjp161948 if (clear) 1293*2139Sjp161948 mac_size=0; 1294*2139Sjp161948 else 1295*2139Sjp161948 mac_size=EVP_MD_size(s->write_hash); 1296*2139Sjp161948 1297*2139Sjp161948 /* DTLS implements explicit IV, so no need for empty fragments */ 1298*2139Sjp161948 #if 0 1299*2139Sjp161948 /* 'create_empty_fragment' is true only when this function calls itself */ 1300*2139Sjp161948 if (!clear && !create_empty_fragment && !s->s3->empty_fragment_done 1301*2139Sjp161948 && SSL_version(s) != DTLS1_VERSION) 1302*2139Sjp161948 { 1303*2139Sjp161948 /* countermeasure against known-IV weakness in CBC ciphersuites 1304*2139Sjp161948 * (see http://www.openssl.org/~bodo/tls-cbc.txt) 1305*2139Sjp161948 */ 1306*2139Sjp161948 1307*2139Sjp161948 if (s->s3->need_empty_fragments && type == SSL3_RT_APPLICATION_DATA) 1308*2139Sjp161948 { 1309*2139Sjp161948 /* recursive function call with 'create_empty_fragment' set; 1310*2139Sjp161948 * this prepares and buffers the data for an empty fragment 1311*2139Sjp161948 * (these 'prefix_len' bytes are sent out later 1312*2139Sjp161948 * together with the actual payload) */ 1313*2139Sjp161948 prefix_len = s->method->do_ssl_write(s, type, buf, 0, 1); 1314*2139Sjp161948 if (prefix_len <= 0) 1315*2139Sjp161948 goto err; 1316*2139Sjp161948 1317*2139Sjp161948 if (s->s3->wbuf.len < (size_t)prefix_len + SSL3_RT_MAX_PACKET_SIZE) 1318*2139Sjp161948 { 1319*2139Sjp161948 /* insufficient space */ 1320*2139Sjp161948 SSLerr(SSL_F_DO_DTLS1_WRITE, ERR_R_INTERNAL_ERROR); 1321*2139Sjp161948 goto err; 1322*2139Sjp161948 } 1323*2139Sjp161948 } 1324*2139Sjp161948 1325*2139Sjp161948 s->s3->empty_fragment_done = 1; 1326*2139Sjp161948 } 1327*2139Sjp161948 #endif 1328*2139Sjp161948 1329*2139Sjp161948 p = wb->buf + prefix_len; 1330*2139Sjp161948 1331*2139Sjp161948 /* write the header */ 1332*2139Sjp161948 1333*2139Sjp161948 *(p++)=type&0xff; 1334*2139Sjp161948 wr->type=type; 1335*2139Sjp161948 1336*2139Sjp161948 *(p++)=(s->version>>8); 1337*2139Sjp161948 *(p++)=s->version&0xff; 1338*2139Sjp161948 1339*2139Sjp161948 /* field where we are to write out packet epoch, seq num and len */ 1340*2139Sjp161948 pseq=p; 1341*2139Sjp161948 p+=10; 1342*2139Sjp161948 1343*2139Sjp161948 /* lets setup the record stuff. */ 1344*2139Sjp161948 1345*2139Sjp161948 /* Make space for the explicit IV in case of CBC. 1346*2139Sjp161948 * (this is a bit of a boundary violation, but what the heck). 1347*2139Sjp161948 */ 1348*2139Sjp161948 if ( s->enc_write_ctx && 1349*2139Sjp161948 (EVP_CIPHER_mode( s->enc_write_ctx->cipher ) & EVP_CIPH_CBC_MODE)) 1350*2139Sjp161948 bs = EVP_CIPHER_block_size(s->enc_write_ctx->cipher); 1351*2139Sjp161948 else 1352*2139Sjp161948 bs = 0; 1353*2139Sjp161948 1354*2139Sjp161948 wr->data=p + bs; /* make room for IV in case of CBC */ 1355*2139Sjp161948 wr->length=(int)len; 1356*2139Sjp161948 wr->input=(unsigned char *)buf; 1357*2139Sjp161948 1358*2139Sjp161948 /* we now 'read' from wr->input, wr->length bytes into 1359*2139Sjp161948 * wr->data */ 1360*2139Sjp161948 1361*2139Sjp161948 /* first we compress */ 1362*2139Sjp161948 if (s->compress != NULL) 1363*2139Sjp161948 { 1364*2139Sjp161948 if (!ssl3_do_compress(s)) 1365*2139Sjp161948 { 1366*2139Sjp161948 SSLerr(SSL_F_DO_DTLS1_WRITE,SSL_R_COMPRESSION_FAILURE); 1367*2139Sjp161948 goto err; 1368*2139Sjp161948 } 1369*2139Sjp161948 } 1370*2139Sjp161948 else 1371*2139Sjp161948 { 1372*2139Sjp161948 memcpy(wr->data,wr->input,wr->length); 1373*2139Sjp161948 wr->input=wr->data; 1374*2139Sjp161948 } 1375*2139Sjp161948 1376*2139Sjp161948 /* we should still have the output to wr->data and the input 1377*2139Sjp161948 * from wr->input. Length should be wr->length. 1378*2139Sjp161948 * wr->data still points in the wb->buf */ 1379*2139Sjp161948 1380*2139Sjp161948 if (mac_size != 0) 1381*2139Sjp161948 { 1382*2139Sjp161948 s->method->ssl3_enc->mac(s,&(p[wr->length + bs]),1); 1383*2139Sjp161948 wr->length+=mac_size; 1384*2139Sjp161948 } 1385*2139Sjp161948 1386*2139Sjp161948 /* this is true regardless of mac size */ 1387*2139Sjp161948 wr->input=p; 1388*2139Sjp161948 wr->data=p; 1389*2139Sjp161948 1390*2139Sjp161948 1391*2139Sjp161948 /* ssl3_enc can only have an error on read */ 1392*2139Sjp161948 wr->length += bs; /* bs != 0 in case of CBC. The enc fn provides 1393*2139Sjp161948 * the randomness */ 1394*2139Sjp161948 s->method->ssl3_enc->enc(s,1); 1395*2139Sjp161948 1396*2139Sjp161948 /* record length after mac and block padding */ 1397*2139Sjp161948 /* if (type == SSL3_RT_APPLICATION_DATA || 1398*2139Sjp161948 (type == SSL3_RT_ALERT && ! SSL_in_init(s))) */ 1399*2139Sjp161948 1400*2139Sjp161948 /* there's only one epoch between handshake and app data */ 1401*2139Sjp161948 1402*2139Sjp161948 s2n(s->d1->w_epoch, pseq); 1403*2139Sjp161948 1404*2139Sjp161948 /* XDTLS: ?? */ 1405*2139Sjp161948 /* else 1406*2139Sjp161948 s2n(s->d1->handshake_epoch, pseq); */ 1407*2139Sjp161948 1408*2139Sjp161948 memcpy(pseq, &(s->s3->write_sequence[2]), 6); 1409*2139Sjp161948 pseq+=6; 1410*2139Sjp161948 s2n(wr->length,pseq); 1411*2139Sjp161948 1412*2139Sjp161948 /* we should now have 1413*2139Sjp161948 * wr->data pointing to the encrypted data, which is 1414*2139Sjp161948 * wr->length long */ 1415*2139Sjp161948 wr->type=type; /* not needed but helps for debugging */ 1416*2139Sjp161948 wr->length+=DTLS1_RT_HEADER_LENGTH; 1417*2139Sjp161948 1418*2139Sjp161948 #if 0 /* this is now done at the message layer */ 1419*2139Sjp161948 /* buffer the record, making it easy to handle retransmits */ 1420*2139Sjp161948 if ( type == SSL3_RT_HANDSHAKE || type == SSL3_RT_CHANGE_CIPHER_SPEC) 1421*2139Sjp161948 dtls1_buffer_record(s, wr->data, wr->length, 1422*2139Sjp161948 *((PQ_64BIT *)&(s->s3->write_sequence[0]))); 1423*2139Sjp161948 #endif 1424*2139Sjp161948 1425*2139Sjp161948 ssl3_record_sequence_update(&(s->s3->write_sequence[0])); 1426*2139Sjp161948 1427*2139Sjp161948 if (create_empty_fragment) 1428*2139Sjp161948 { 1429*2139Sjp161948 /* we are in a recursive call; 1430*2139Sjp161948 * just return the length, don't write out anything here 1431*2139Sjp161948 */ 1432*2139Sjp161948 return wr->length; 1433*2139Sjp161948 } 1434*2139Sjp161948 1435*2139Sjp161948 /* now let's set up wb */ 1436*2139Sjp161948 wb->left = prefix_len + wr->length; 1437*2139Sjp161948 wb->offset = 0; 1438*2139Sjp161948 1439*2139Sjp161948 /* memorize arguments so that ssl3_write_pending can detect bad write retries later */ 1440*2139Sjp161948 s->s3->wpend_tot=len; 1441*2139Sjp161948 s->s3->wpend_buf=buf; 1442*2139Sjp161948 s->s3->wpend_type=type; 1443*2139Sjp161948 s->s3->wpend_ret=len; 1444*2139Sjp161948 1445*2139Sjp161948 /* we now just need to write the buffer */ 1446*2139Sjp161948 return ssl3_write_pending(s,type,buf,len); 1447*2139Sjp161948 err: 1448*2139Sjp161948 return -1; 1449*2139Sjp161948 } 1450*2139Sjp161948 1451*2139Sjp161948 1452*2139Sjp161948 1453*2139Sjp161948 static int dtls1_record_replay_check(SSL *s, DTLS1_BITMAP *bitmap, 1454*2139Sjp161948 PQ_64BIT *seq_num) 1455*2139Sjp161948 { 1456*2139Sjp161948 #if PQ_64BIT_IS_INTEGER 1457*2139Sjp161948 PQ_64BIT mask = 0x0000000000000001L; 1458*2139Sjp161948 #endif 1459*2139Sjp161948 PQ_64BIT rcd_num, tmp; 1460*2139Sjp161948 1461*2139Sjp161948 pq_64bit_init(&rcd_num); 1462*2139Sjp161948 pq_64bit_init(&tmp); 1463*2139Sjp161948 1464*2139Sjp161948 /* this is the sequence number for the record just read */ 1465*2139Sjp161948 pq_64bit_bin2num(&rcd_num, s->s3->read_sequence, 8); 1466*2139Sjp161948 1467*2139Sjp161948 1468*2139Sjp161948 if (pq_64bit_gt(&rcd_num, &(bitmap->max_seq_num)) || 1469*2139Sjp161948 pq_64bit_eq(&rcd_num, &(bitmap->max_seq_num))) 1470*2139Sjp161948 { 1471*2139Sjp161948 pq_64bit_assign(seq_num, &rcd_num); 1472*2139Sjp161948 pq_64bit_free(&rcd_num); 1473*2139Sjp161948 pq_64bit_free(&tmp); 1474*2139Sjp161948 return 1; /* this record is new */ 1475*2139Sjp161948 } 1476*2139Sjp161948 1477*2139Sjp161948 pq_64bit_sub(&tmp, &(bitmap->max_seq_num), &rcd_num); 1478*2139Sjp161948 1479*2139Sjp161948 if ( pq_64bit_get_word(&tmp) > bitmap->length) 1480*2139Sjp161948 { 1481*2139Sjp161948 pq_64bit_free(&rcd_num); 1482*2139Sjp161948 pq_64bit_free(&tmp); 1483*2139Sjp161948 return 0; /* stale, outside the window */ 1484*2139Sjp161948 } 1485*2139Sjp161948 1486*2139Sjp161948 #if PQ_64BIT_IS_BIGNUM 1487*2139Sjp161948 { 1488*2139Sjp161948 int offset; 1489*2139Sjp161948 pq_64bit_sub(&tmp, &(bitmap->max_seq_num), &rcd_num); 1490*2139Sjp161948 pq_64bit_sub_word(&tmp, 1); 1491*2139Sjp161948 offset = pq_64bit_get_word(&tmp); 1492*2139Sjp161948 if ( pq_64bit_is_bit_set(&(bitmap->map), offset)) 1493*2139Sjp161948 { 1494*2139Sjp161948 pq_64bit_free(&rcd_num); 1495*2139Sjp161948 pq_64bit_free(&tmp); 1496*2139Sjp161948 return 0; 1497*2139Sjp161948 } 1498*2139Sjp161948 } 1499*2139Sjp161948 #else 1500*2139Sjp161948 mask <<= (bitmap->max_seq_num - rcd_num - 1); 1501*2139Sjp161948 if (bitmap->map & mask) 1502*2139Sjp161948 return 0; /* record previously received */ 1503*2139Sjp161948 #endif 1504*2139Sjp161948 1505*2139Sjp161948 pq_64bit_assign(seq_num, &rcd_num); 1506*2139Sjp161948 pq_64bit_free(&rcd_num); 1507*2139Sjp161948 pq_64bit_free(&tmp); 1508*2139Sjp161948 return 1; 1509*2139Sjp161948 } 1510*2139Sjp161948 1511*2139Sjp161948 1512*2139Sjp161948 static void dtls1_record_bitmap_update(SSL *s, DTLS1_BITMAP *bitmap) 1513*2139Sjp161948 { 1514*2139Sjp161948 unsigned int shift; 1515*2139Sjp161948 PQ_64BIT rcd_num; 1516*2139Sjp161948 PQ_64BIT tmp; 1517*2139Sjp161948 PQ_64BIT_CTX *ctx; 1518*2139Sjp161948 1519*2139Sjp161948 pq_64bit_init(&rcd_num); 1520*2139Sjp161948 pq_64bit_init(&tmp); 1521*2139Sjp161948 1522*2139Sjp161948 pq_64bit_bin2num(&rcd_num, s->s3->read_sequence, 8); 1523*2139Sjp161948 1524*2139Sjp161948 /* unfortunate code complexity due to 64-bit manipulation support 1525*2139Sjp161948 * on 32-bit machines */ 1526*2139Sjp161948 if ( pq_64bit_gt(&rcd_num, &(bitmap->max_seq_num)) || 1527*2139Sjp161948 pq_64bit_eq(&rcd_num, &(bitmap->max_seq_num))) 1528*2139Sjp161948 { 1529*2139Sjp161948 pq_64bit_sub(&tmp, &rcd_num, &(bitmap->max_seq_num)); 1530*2139Sjp161948 pq_64bit_add_word(&tmp, 1); 1531*2139Sjp161948 1532*2139Sjp161948 shift = (unsigned int)pq_64bit_get_word(&tmp); 1533*2139Sjp161948 1534*2139Sjp161948 pq_64bit_lshift(&(tmp), &(bitmap->map), shift); 1535*2139Sjp161948 pq_64bit_assign(&(bitmap->map), &tmp); 1536*2139Sjp161948 1537*2139Sjp161948 pq_64bit_set_bit(&(bitmap->map), 0); 1538*2139Sjp161948 pq_64bit_add_word(&rcd_num, 1); 1539*2139Sjp161948 pq_64bit_assign(&(bitmap->max_seq_num), &rcd_num); 1540*2139Sjp161948 1541*2139Sjp161948 pq_64bit_assign_word(&tmp, 1); 1542*2139Sjp161948 pq_64bit_lshift(&tmp, &tmp, bitmap->length); 1543*2139Sjp161948 ctx = pq_64bit_ctx_new(&ctx); 1544*2139Sjp161948 pq_64bit_mod(&(bitmap->map), &(bitmap->map), &tmp, ctx); 1545*2139Sjp161948 pq_64bit_ctx_free(ctx); 1546*2139Sjp161948 } 1547*2139Sjp161948 else 1548*2139Sjp161948 { 1549*2139Sjp161948 pq_64bit_sub(&tmp, &(bitmap->max_seq_num), &rcd_num); 1550*2139Sjp161948 pq_64bit_sub_word(&tmp, 1); 1551*2139Sjp161948 shift = (unsigned int)pq_64bit_get_word(&tmp); 1552*2139Sjp161948 1553*2139Sjp161948 pq_64bit_set_bit(&(bitmap->map), shift); 1554*2139Sjp161948 } 1555*2139Sjp161948 1556*2139Sjp161948 pq_64bit_free(&rcd_num); 1557*2139Sjp161948 pq_64bit_free(&tmp); 1558*2139Sjp161948 } 1559*2139Sjp161948 1560*2139Sjp161948 1561*2139Sjp161948 int dtls1_dispatch_alert(SSL *s) 1562*2139Sjp161948 { 1563*2139Sjp161948 int i,j; 1564*2139Sjp161948 void (*cb)(const SSL *ssl,int type,int val)=NULL; 1565*2139Sjp161948 unsigned char buf[2 + 2 + 3]; /* alert level + alert desc + message seq +frag_off */ 1566*2139Sjp161948 unsigned char *ptr = &buf[0]; 1567*2139Sjp161948 1568*2139Sjp161948 s->s3->alert_dispatch=0; 1569*2139Sjp161948 1570*2139Sjp161948 memset(buf, 0x00, sizeof(buf)); 1571*2139Sjp161948 *ptr++ = s->s3->send_alert[0]; 1572*2139Sjp161948 *ptr++ = s->s3->send_alert[1]; 1573*2139Sjp161948 1574*2139Sjp161948 if (s->s3->send_alert[1] == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE) 1575*2139Sjp161948 { 1576*2139Sjp161948 s2n(s->d1->handshake_read_seq, ptr); 1577*2139Sjp161948 #if 0 1578*2139Sjp161948 if ( s->d1->r_msg_hdr.frag_off == 0) /* waiting for a new msg */ 1579*2139Sjp161948 1580*2139Sjp161948 else 1581*2139Sjp161948 s2n(s->d1->r_msg_hdr.seq, ptr); /* partial msg read */ 1582*2139Sjp161948 #endif 1583*2139Sjp161948 1584*2139Sjp161948 #if 0 1585*2139Sjp161948 fprintf(stderr, "s->d1->handshake_read_seq = %d, s->d1->r_msg_hdr.seq = %d\n",s->d1->handshake_read_seq,s->d1->r_msg_hdr.seq); 1586*2139Sjp161948 #endif 1587*2139Sjp161948 l2n3(s->d1->r_msg_hdr.frag_off, ptr); 1588*2139Sjp161948 } 1589*2139Sjp161948 1590*2139Sjp161948 i = do_dtls1_write(s, SSL3_RT_ALERT, &buf[0], sizeof(buf), 0); 1591*2139Sjp161948 if (i <= 0) 1592*2139Sjp161948 { 1593*2139Sjp161948 s->s3->alert_dispatch=1; 1594*2139Sjp161948 /* fprintf( stderr, "not done with alert\n" ); */ 1595*2139Sjp161948 } 1596*2139Sjp161948 else 1597*2139Sjp161948 { 1598*2139Sjp161948 if ( s->s3->send_alert[0] == SSL3_AL_FATAL || 1599*2139Sjp161948 s->s3->send_alert[1] == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE) 1600*2139Sjp161948 (void)BIO_flush(s->wbio); 1601*2139Sjp161948 1602*2139Sjp161948 if (s->msg_callback) 1603*2139Sjp161948 s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 1604*2139Sjp161948 2, s, s->msg_callback_arg); 1605*2139Sjp161948 1606*2139Sjp161948 if (s->info_callback != NULL) 1607*2139Sjp161948 cb=s->info_callback; 1608*2139Sjp161948 else if (s->ctx->info_callback != NULL) 1609*2139Sjp161948 cb=s->ctx->info_callback; 1610*2139Sjp161948 1611*2139Sjp161948 if (cb != NULL) 1612*2139Sjp161948 { 1613*2139Sjp161948 j=(s->s3->send_alert[0]<<8)|s->s3->send_alert[1]; 1614*2139Sjp161948 cb(s,SSL_CB_WRITE_ALERT,j); 1615*2139Sjp161948 } 1616*2139Sjp161948 } 1617*2139Sjp161948 return(i); 1618*2139Sjp161948 } 1619*2139Sjp161948 1620*2139Sjp161948 1621*2139Sjp161948 static DTLS1_BITMAP * 1622*2139Sjp161948 dtls1_get_bitmap(SSL *s, SSL3_RECORD *rr, unsigned int *is_next_epoch) 1623*2139Sjp161948 { 1624*2139Sjp161948 1625*2139Sjp161948 *is_next_epoch = 0; 1626*2139Sjp161948 1627*2139Sjp161948 /* In current epoch, accept HM, CCS, DATA, & ALERT */ 1628*2139Sjp161948 if (rr->epoch == s->d1->r_epoch) 1629*2139Sjp161948 return &s->d1->bitmap; 1630*2139Sjp161948 1631*2139Sjp161948 /* Only HM and ALERT messages can be from the next epoch */ 1632*2139Sjp161948 else if (rr->epoch == (unsigned long)(s->d1->r_epoch + 1) && 1633*2139Sjp161948 (rr->type == SSL3_RT_HANDSHAKE || 1634*2139Sjp161948 rr->type == SSL3_RT_ALERT)) 1635*2139Sjp161948 { 1636*2139Sjp161948 *is_next_epoch = 1; 1637*2139Sjp161948 return &s->d1->next_bitmap; 1638*2139Sjp161948 } 1639*2139Sjp161948 1640*2139Sjp161948 return NULL; 1641*2139Sjp161948 } 1642*2139Sjp161948 1643*2139Sjp161948 #if 0 1644*2139Sjp161948 static int 1645*2139Sjp161948 dtls1_record_needs_buffering(SSL *s, SSL3_RECORD *rr, unsigned short *priority, 1646*2139Sjp161948 unsigned long *offset) 1647*2139Sjp161948 { 1648*2139Sjp161948 1649*2139Sjp161948 /* alerts are passed up immediately */ 1650*2139Sjp161948 if ( rr->type == SSL3_RT_APPLICATION_DATA || 1651*2139Sjp161948 rr->type == SSL3_RT_ALERT) 1652*2139Sjp161948 return 0; 1653*2139Sjp161948 1654*2139Sjp161948 /* Only need to buffer if a handshake is underway. 1655*2139Sjp161948 * (this implies that Hello Request and Client Hello are passed up 1656*2139Sjp161948 * immediately) */ 1657*2139Sjp161948 if ( SSL_in_init(s)) 1658*2139Sjp161948 { 1659*2139Sjp161948 unsigned char *data = rr->data; 1660*2139Sjp161948 /* need to extract the HM/CCS sequence number here */ 1661*2139Sjp161948 if ( rr->type == SSL3_RT_HANDSHAKE || 1662*2139Sjp161948 rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) 1663*2139Sjp161948 { 1664*2139Sjp161948 unsigned short seq_num; 1665*2139Sjp161948 struct hm_header_st msg_hdr; 1666*2139Sjp161948 struct ccs_header_st ccs_hdr; 1667*2139Sjp161948 1668*2139Sjp161948 if ( rr->type == SSL3_RT_HANDSHAKE) 1669*2139Sjp161948 { 1670*2139Sjp161948 dtls1_get_message_header(data, &msg_hdr); 1671*2139Sjp161948 seq_num = msg_hdr.seq; 1672*2139Sjp161948 *offset = msg_hdr.frag_off; 1673*2139Sjp161948 } 1674*2139Sjp161948 else 1675*2139Sjp161948 { 1676*2139Sjp161948 dtls1_get_ccs_header(data, &ccs_hdr); 1677*2139Sjp161948 seq_num = ccs_hdr.seq; 1678*2139Sjp161948 *offset = 0; 1679*2139Sjp161948 } 1680*2139Sjp161948 1681*2139Sjp161948 /* this is either a record we're waiting for, or a 1682*2139Sjp161948 * retransmit of something we happened to previously 1683*2139Sjp161948 * receive (higher layers will drop the repeat silently */ 1684*2139Sjp161948 if ( seq_num < s->d1->handshake_read_seq) 1685*2139Sjp161948 return 0; 1686*2139Sjp161948 if (rr->type == SSL3_RT_HANDSHAKE && 1687*2139Sjp161948 seq_num == s->d1->handshake_read_seq && 1688*2139Sjp161948 msg_hdr.frag_off < s->d1->r_msg_hdr.frag_off) 1689*2139Sjp161948 return 0; 1690*2139Sjp161948 else if ( seq_num == s->d1->handshake_read_seq && 1691*2139Sjp161948 (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC || 1692*2139Sjp161948 msg_hdr.frag_off == s->d1->r_msg_hdr.frag_off)) 1693*2139Sjp161948 return 0; 1694*2139Sjp161948 else 1695*2139Sjp161948 { 1696*2139Sjp161948 *priority = seq_num; 1697*2139Sjp161948 return 1; 1698*2139Sjp161948 } 1699*2139Sjp161948 } 1700*2139Sjp161948 else /* unknown record type */ 1701*2139Sjp161948 return 0; 1702*2139Sjp161948 } 1703*2139Sjp161948 1704*2139Sjp161948 return 0; 1705*2139Sjp161948 } 1706*2139Sjp161948 #endif 1707*2139Sjp161948 1708*2139Sjp161948 void 1709*2139Sjp161948 dtls1_reset_seq_numbers(SSL *s, int rw) 1710*2139Sjp161948 { 1711*2139Sjp161948 unsigned char *seq; 1712*2139Sjp161948 unsigned int seq_bytes = sizeof(s->s3->read_sequence); 1713*2139Sjp161948 1714*2139Sjp161948 if ( rw & SSL3_CC_READ) 1715*2139Sjp161948 { 1716*2139Sjp161948 seq = s->s3->read_sequence; 1717*2139Sjp161948 s->d1->r_epoch++; 1718*2139Sjp161948 1719*2139Sjp161948 pq_64bit_assign(&(s->d1->bitmap.map), &(s->d1->next_bitmap.map)); 1720*2139Sjp161948 s->d1->bitmap.length = s->d1->next_bitmap.length; 1721*2139Sjp161948 pq_64bit_assign(&(s->d1->bitmap.max_seq_num), 1722*2139Sjp161948 &(s->d1->next_bitmap.max_seq_num)); 1723*2139Sjp161948 1724*2139Sjp161948 pq_64bit_free(&(s->d1->next_bitmap.map)); 1725*2139Sjp161948 pq_64bit_free(&(s->d1->next_bitmap.max_seq_num)); 1726*2139Sjp161948 memset(&(s->d1->next_bitmap), 0x00, sizeof(DTLS1_BITMAP)); 1727*2139Sjp161948 pq_64bit_init(&(s->d1->next_bitmap.map)); 1728*2139Sjp161948 pq_64bit_init(&(s->d1->next_bitmap.max_seq_num)); 1729*2139Sjp161948 } 1730*2139Sjp161948 else 1731*2139Sjp161948 { 1732*2139Sjp161948 seq = s->s3->write_sequence; 1733*2139Sjp161948 s->d1->w_epoch++; 1734*2139Sjp161948 } 1735*2139Sjp161948 1736*2139Sjp161948 memset(seq, 0x00, seq_bytes); 1737*2139Sjp161948 } 1738*2139Sjp161948 1739*2139Sjp161948 #if PQ_64BIT_IS_INTEGER 1740*2139Sjp161948 static PQ_64BIT 1741*2139Sjp161948 bytes_to_long_long(unsigned char *bytes, PQ_64BIT *num) 1742*2139Sjp161948 { 1743*2139Sjp161948 PQ_64BIT _num; 1744*2139Sjp161948 1745*2139Sjp161948 _num = (((PQ_64BIT)bytes[0]) << 56) | 1746*2139Sjp161948 (((PQ_64BIT)bytes[1]) << 48) | 1747*2139Sjp161948 (((PQ_64BIT)bytes[2]) << 40) | 1748*2139Sjp161948 (((PQ_64BIT)bytes[3]) << 32) | 1749*2139Sjp161948 (((PQ_64BIT)bytes[4]) << 24) | 1750*2139Sjp161948 (((PQ_64BIT)bytes[5]) << 16) | 1751*2139Sjp161948 (((PQ_64BIT)bytes[6]) << 8) | 1752*2139Sjp161948 (((PQ_64BIT)bytes[7]) ); 1753*2139Sjp161948 1754*2139Sjp161948 *num = _num ; 1755*2139Sjp161948 return _num; 1756*2139Sjp161948 } 1757*2139Sjp161948 #endif 1758*2139Sjp161948 1759*2139Sjp161948 1760*2139Sjp161948 static void 1761*2139Sjp161948 dtls1_clear_timeouts(SSL *s) 1762*2139Sjp161948 { 1763*2139Sjp161948 memset(&(s->d1->timeout), 0x00, sizeof(struct dtls1_timeout_st)); 1764*2139Sjp161948 } 1765