sl@0: /* ssl/d1_pkt.c */ sl@0: /* sl@0: * DTLS implementation written by Nagendra Modadugu sl@0: * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * openssl-core@openssl.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.openssl.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #define USE_SOCKETS sl@0: #include "ssl_locl.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: static int have_handshake_fragment(SSL *s, int type, unsigned char *buf, sl@0: int len, int peek); sl@0: static int dtls1_record_replay_check(SSL *s, DTLS1_BITMAP *bitmap, sl@0: PQ_64BIT *seq_num); sl@0: static void dtls1_record_bitmap_update(SSL *s, DTLS1_BITMAP *bitmap); sl@0: static DTLS1_BITMAP *dtls1_get_bitmap(SSL *s, SSL3_RECORD *rr, sl@0: unsigned int *is_next_epoch); sl@0: #if 0 sl@0: static int dtls1_record_needs_buffering(SSL *s, SSL3_RECORD *rr, sl@0: unsigned short *priority, unsigned long *offset); sl@0: #endif sl@0: static int dtls1_buffer_record(SSL *s, record_pqueue *q, sl@0: PQ_64BIT priority); sl@0: static int dtls1_process_record(SSL *s); sl@0: #if PQ_64BIT_IS_INTEGER sl@0: static PQ_64BIT bytes_to_long_long(unsigned char *bytes, PQ_64BIT *num); sl@0: #endif sl@0: static void dtls1_clear_timeouts(SSL *s); sl@0: sl@0: /* copy buffered record into SSL structure */ sl@0: static int sl@0: dtls1_copy_record(SSL *s, pitem *item) sl@0: { sl@0: DTLS1_RECORD_DATA *rdata; sl@0: sl@0: rdata = (DTLS1_RECORD_DATA *)item->data; sl@0: sl@0: if (s->s3->rbuf.buf != NULL) sl@0: OPENSSL_free(s->s3->rbuf.buf); sl@0: sl@0: s->packet = rdata->packet; sl@0: s->packet_length = rdata->packet_length; sl@0: memcpy(&(s->s3->rbuf), &(rdata->rbuf), sizeof(SSL3_BUFFER)); sl@0: memcpy(&(s->s3->rrec), &(rdata->rrec), sizeof(SSL3_RECORD)); sl@0: sl@0: return(1); sl@0: } sl@0: sl@0: sl@0: static int sl@0: dtls1_buffer_record(SSL *s, record_pqueue *queue, PQ_64BIT priority) sl@0: { sl@0: DTLS1_RECORD_DATA *rdata; sl@0: pitem *item; sl@0: sl@0: rdata = OPENSSL_malloc(sizeof(DTLS1_RECORD_DATA)); sl@0: item = pitem_new(priority, rdata); sl@0: if (rdata == NULL || item == NULL) sl@0: { sl@0: if (rdata != NULL) OPENSSL_free(rdata); sl@0: if (item != NULL) pitem_free(item); sl@0: sl@0: SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR); sl@0: return(0); sl@0: } sl@0: sl@0: rdata->packet = s->packet; sl@0: rdata->packet_length = s->packet_length; sl@0: memcpy(&(rdata->rbuf), &(s->s3->rbuf), sizeof(SSL3_BUFFER)); sl@0: memcpy(&(rdata->rrec), &(s->s3->rrec), sizeof(SSL3_RECORD)); sl@0: sl@0: item->data = rdata; sl@0: sl@0: /* insert should not fail, since duplicates are dropped */ sl@0: if (pqueue_insert(queue->q, item) == NULL) sl@0: { sl@0: OPENSSL_free(rdata); sl@0: pitem_free(item); sl@0: return(0); sl@0: } sl@0: sl@0: s->packet = NULL; sl@0: s->packet_length = 0; sl@0: memset(&(s->s3->rbuf), 0, sizeof(SSL3_BUFFER)); sl@0: memset(&(s->s3->rrec), 0, sizeof(SSL3_RECORD)); sl@0: sl@0: if (!ssl3_setup_buffers(s)) sl@0: { sl@0: SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR); sl@0: OPENSSL_free(rdata); sl@0: pitem_free(item); sl@0: return(0); sl@0: } sl@0: sl@0: return(1); sl@0: } sl@0: sl@0: sl@0: static int sl@0: dtls1_retrieve_buffered_record(SSL *s, record_pqueue *queue) sl@0: { sl@0: pitem *item; sl@0: sl@0: item = pqueue_pop(queue->q); sl@0: if (item) sl@0: { sl@0: dtls1_copy_record(s, item); sl@0: sl@0: OPENSSL_free(item->data); sl@0: pitem_free(item); sl@0: sl@0: return(1); sl@0: } sl@0: sl@0: return(0); sl@0: } sl@0: sl@0: sl@0: /* retrieve a buffered record that belongs to the new epoch, i.e., not processed sl@0: * yet */ sl@0: #define dtls1_get_unprocessed_record(s) \ sl@0: dtls1_retrieve_buffered_record((s), \ sl@0: &((s)->d1->unprocessed_rcds)) sl@0: sl@0: /* retrieve a buffered record that belongs to the current epoch, ie, processed */ sl@0: #define dtls1_get_processed_record(s) \ sl@0: dtls1_retrieve_buffered_record((s), \ sl@0: &((s)->d1->processed_rcds)) sl@0: sl@0: static int sl@0: dtls1_process_buffered_records(SSL *s) sl@0: { sl@0: pitem *item; sl@0: sl@0: item = pqueue_peek(s->d1->unprocessed_rcds.q); sl@0: if (item) sl@0: { sl@0: DTLS1_RECORD_DATA *rdata; sl@0: rdata = (DTLS1_RECORD_DATA *)item->data; sl@0: sl@0: /* Check if epoch is current. */ sl@0: if (s->d1->unprocessed_rcds.epoch != s->d1->r_epoch) sl@0: return(1); /* Nothing to do. */ sl@0: sl@0: /* Process all the records. */ sl@0: while (pqueue_peek(s->d1->unprocessed_rcds.q)) sl@0: { sl@0: dtls1_get_unprocessed_record(s); sl@0: if ( ! dtls1_process_record(s)) sl@0: return(0); sl@0: dtls1_buffer_record(s, &(s->d1->processed_rcds), sl@0: s->s3->rrec.seq_num); sl@0: } sl@0: } sl@0: sl@0: /* sync epoch numbers once all the unprocessed records sl@0: * have been processed */ sl@0: s->d1->processed_rcds.epoch = s->d1->r_epoch; sl@0: s->d1->unprocessed_rcds.epoch = s->d1->r_epoch + 1; sl@0: sl@0: return(1); sl@0: } sl@0: sl@0: sl@0: #if 0 sl@0: sl@0: static int sl@0: dtls1_get_buffered_record(SSL *s) sl@0: { sl@0: pitem *item; sl@0: PQ_64BIT priority = sl@0: (((PQ_64BIT)s->d1->handshake_read_seq) << 32) | sl@0: ((PQ_64BIT)s->d1->r_msg_hdr.frag_off); sl@0: sl@0: if ( ! SSL_in_init(s)) /* if we're not (re)negotiating, sl@0: nothing buffered */ sl@0: return 0; sl@0: sl@0: sl@0: item = pqueue_peek(s->d1->rcvd_records); sl@0: if (item && item->priority == priority) sl@0: { sl@0: /* Check if we've received the record of interest. It must be sl@0: * a handshake record, since data records as passed up without sl@0: * buffering */ sl@0: DTLS1_RECORD_DATA *rdata; sl@0: item = pqueue_pop(s->d1->rcvd_records); sl@0: rdata = (DTLS1_RECORD_DATA *)item->data; sl@0: sl@0: if (s->s3->rbuf.buf != NULL) sl@0: OPENSSL_free(s->s3->rbuf.buf); sl@0: sl@0: s->packet = rdata->packet; sl@0: s->packet_length = rdata->packet_length; sl@0: memcpy(&(s->s3->rbuf), &(rdata->rbuf), sizeof(SSL3_BUFFER)); sl@0: memcpy(&(s->s3->rrec), &(rdata->rrec), sizeof(SSL3_RECORD)); sl@0: sl@0: OPENSSL_free(item->data); sl@0: pitem_free(item); sl@0: sl@0: /* s->d1->next_expected_seq_num++; */ sl@0: return(1); sl@0: } sl@0: sl@0: return 0; sl@0: } sl@0: sl@0: #endif sl@0: sl@0: static int sl@0: dtls1_process_record(SSL *s) sl@0: { sl@0: int i,al; sl@0: int clear=0; sl@0: int enc_err; sl@0: SSL_SESSION *sess; sl@0: SSL3_RECORD *rr; sl@0: unsigned int mac_size; sl@0: unsigned char md[EVP_MAX_MD_SIZE]; sl@0: sl@0: sl@0: rr= &(s->s3->rrec); sl@0: sess = s->session; sl@0: sl@0: /* At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length, sl@0: * and we have that many bytes in s->packet sl@0: */ sl@0: rr->input= &(s->packet[DTLS1_RT_HEADER_LENGTH]); sl@0: sl@0: /* ok, we can now read from 's->packet' data into 'rr' sl@0: * rr->input points at rr->length bytes, which sl@0: * need to be copied into rr->data by either sl@0: * the decryption or by the decompression sl@0: * When the data is 'copied' into the rr->data buffer, sl@0: * rr->input will be pointed at the new buffer */ sl@0: sl@0: /* We now have - encrypted [ MAC [ compressed [ plain ] ] ] sl@0: * rr->length bytes of encrypted compressed stuff. */ sl@0: sl@0: /* check is not needed I believe */ sl@0: if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) sl@0: { sl@0: al=SSL_AD_RECORD_OVERFLOW; sl@0: SSLerr(SSL_F_DTLS1_PROCESS_RECORD,SSL_R_ENCRYPTED_LENGTH_TOO_LONG); sl@0: goto f_err; sl@0: } sl@0: sl@0: /* decrypt in place in 'rr->input' */ sl@0: rr->data=rr->input; sl@0: sl@0: enc_err = s->method->ssl3_enc->enc(s,0); sl@0: if (enc_err <= 0) sl@0: { sl@0: if (enc_err == 0) sl@0: /* SSLerr() and ssl3_send_alert() have been called */ sl@0: goto err; sl@0: sl@0: /* otherwise enc_err == -1 */ sl@0: goto decryption_failed_or_bad_record_mac; sl@0: } sl@0: sl@0: #ifdef TLS_DEBUG sl@0: printf("dec %d\n",rr->length); sl@0: { unsigned int z; for (z=0; zlength; z++) printf("%02X%c",rr->data[z],((z+1)%16)?' ':'\n'); } sl@0: printf("\n"); sl@0: #endif sl@0: sl@0: /* r->length is now the compressed data plus mac */ sl@0: if ( (sess == NULL) || sl@0: (s->enc_read_ctx == NULL) || sl@0: (s->read_hash == NULL)) sl@0: clear=1; sl@0: sl@0: if (!clear) sl@0: { sl@0: mac_size=EVP_MD_size(s->read_hash); sl@0: sl@0: if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH+mac_size) sl@0: { sl@0: #if 0 /* OK only for stream ciphers (then rr->length is visible from ciphertext anyway) */ sl@0: al=SSL_AD_RECORD_OVERFLOW; sl@0: SSLerr(SSL_F_DTLS1_PROCESS_RECORD,SSL_R_PRE_MAC_LENGTH_TOO_LONG); sl@0: goto f_err; sl@0: #else sl@0: goto decryption_failed_or_bad_record_mac; sl@0: #endif sl@0: } sl@0: /* check the MAC for rr->input (it's in mac_size bytes at the tail) */ sl@0: if (rr->length < mac_size) sl@0: { sl@0: #if 0 /* OK only for stream ciphers */ sl@0: al=SSL_AD_DECODE_ERROR; sl@0: SSLerr(SSL_F_DTLS1_PROCESS_RECORD,SSL_R_LENGTH_TOO_SHORT); sl@0: goto f_err; sl@0: #else sl@0: goto decryption_failed_or_bad_record_mac; sl@0: #endif sl@0: } sl@0: rr->length-=mac_size; sl@0: i=s->method->ssl3_enc->mac(s,md,0); sl@0: if (memcmp(md,&(rr->data[rr->length]),mac_size) != 0) sl@0: { sl@0: goto decryption_failed_or_bad_record_mac; sl@0: } sl@0: } sl@0: sl@0: /* r->length is now just compressed */ sl@0: if (s->expand != NULL) sl@0: { sl@0: if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) sl@0: { sl@0: al=SSL_AD_RECORD_OVERFLOW; sl@0: SSLerr(SSL_F_DTLS1_PROCESS_RECORD,SSL_R_COMPRESSED_LENGTH_TOO_LONG); sl@0: goto f_err; sl@0: } sl@0: if (!ssl3_do_uncompress(s)) sl@0: { sl@0: al=SSL_AD_DECOMPRESSION_FAILURE; sl@0: SSLerr(SSL_F_DTLS1_PROCESS_RECORD,SSL_R_BAD_DECOMPRESSION); sl@0: goto f_err; sl@0: } sl@0: } sl@0: sl@0: if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH) sl@0: { sl@0: al=SSL_AD_RECORD_OVERFLOW; sl@0: SSLerr(SSL_F_DTLS1_PROCESS_RECORD,SSL_R_DATA_LENGTH_TOO_LONG); sl@0: goto f_err; sl@0: } sl@0: sl@0: rr->off=0; sl@0: /* So at this point the following is true sl@0: * ssl->s3->rrec.type is the type of record sl@0: * ssl->s3->rrec.length == number of bytes in record sl@0: * ssl->s3->rrec.off == offset to first valid byte sl@0: * ssl->s3->rrec.data == where to take bytes from, increment sl@0: * after use :-). sl@0: */ sl@0: sl@0: /* we have pulled in a full packet so zero things */ sl@0: s->packet_length=0; sl@0: dtls1_record_bitmap_update(s, &(s->d1->bitmap));/* Mark receipt of record. */ sl@0: return(1); sl@0: sl@0: decryption_failed_or_bad_record_mac: sl@0: /* Separate 'decryption_failed' alert was introduced with TLS 1.0, sl@0: * SSL 3.0 only has 'bad_record_mac'. But unless a decryption sl@0: * failure is directly visible from the ciphertext anyway, sl@0: * we should not reveal which kind of error occured -- this sl@0: * might become visible to an attacker (e.g. via logfile) */ sl@0: al=SSL_AD_BAD_RECORD_MAC; sl@0: SSLerr(SSL_F_DTLS1_PROCESS_RECORD,SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC); sl@0: f_err: sl@0: ssl3_send_alert(s,SSL3_AL_FATAL,al); sl@0: err: sl@0: return(0); sl@0: } sl@0: sl@0: sl@0: /* Call this to get a new input record. sl@0: * It will return <= 0 if more data is needed, normally due to an error sl@0: * or non-blocking IO. sl@0: * When it finishes, one packet has been decoded and can be found in sl@0: * ssl->s3->rrec.type - is the type of record sl@0: * ssl->s3->rrec.data, - data sl@0: * ssl->s3->rrec.length, - number of bytes sl@0: */ sl@0: /* used only by dtls1_read_bytes */ sl@0: int dtls1_get_record(SSL *s) sl@0: { sl@0: int ssl_major,ssl_minor,al; sl@0: int i,n; sl@0: SSL3_RECORD *rr; sl@0: SSL_SESSION *sess; sl@0: unsigned char *p; sl@0: unsigned short version; sl@0: DTLS1_BITMAP *bitmap; sl@0: unsigned int is_next_epoch; sl@0: sl@0: rr= &(s->s3->rrec); sl@0: sess=s->session; sl@0: sl@0: /* The epoch may have changed. If so, process all the sl@0: * pending records. This is a non-blocking operation. */ sl@0: if ( ! dtls1_process_buffered_records(s)) sl@0: return 0; sl@0: sl@0: /* if we're renegotiating, then there may be buffered records */ sl@0: if (dtls1_get_processed_record(s)) sl@0: return 1; sl@0: sl@0: /* get something from the wire */ sl@0: again: sl@0: /* check if we have the header */ sl@0: if ( (s->rstate != SSL_ST_READ_BODY) || sl@0: (s->packet_length < DTLS1_RT_HEADER_LENGTH)) sl@0: { sl@0: n=ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH, s->s3->rbuf.len, 0); sl@0: /* read timeout is handled by dtls1_read_bytes */ sl@0: if (n <= 0) return(n); /* error or non-blocking */ sl@0: sl@0: OPENSSL_assert(s->packet_length == DTLS1_RT_HEADER_LENGTH); sl@0: sl@0: s->rstate=SSL_ST_READ_BODY; sl@0: sl@0: p=s->packet; sl@0: sl@0: /* Pull apart the header into the DTLS1_RECORD */ sl@0: rr->type= *(p++); sl@0: ssl_major= *(p++); sl@0: ssl_minor= *(p++); sl@0: version=(ssl_major<<8)|ssl_minor; sl@0: sl@0: /* sequence number is 64 bits, with top 2 bytes = epoch */ sl@0: n2s(p,rr->epoch); sl@0: sl@0: memcpy(&(s->s3->read_sequence[2]), p, 6); sl@0: p+=6; sl@0: sl@0: n2s(p,rr->length); sl@0: sl@0: /* Lets check version */ sl@0: if (!s->first_packet) sl@0: { sl@0: if (version != s->version && version != DTLS1_BAD_VER) sl@0: { sl@0: SSLerr(SSL_F_DTLS1_GET_RECORD,SSL_R_WRONG_VERSION_NUMBER); sl@0: /* Send back error using their sl@0: * version number :-) */ sl@0: s->version=version; sl@0: al=SSL_AD_PROTOCOL_VERSION; sl@0: goto f_err; sl@0: } sl@0: } sl@0: sl@0: if ((version & 0xff00) != (DTLS1_VERSION & 0xff00) && sl@0: (version & 0xff00) != (DTLS1_BAD_VER & 0xff00)) sl@0: { sl@0: SSLerr(SSL_F_DTLS1_GET_RECORD,SSL_R_WRONG_VERSION_NUMBER); sl@0: goto err; sl@0: } sl@0: sl@0: if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) sl@0: { sl@0: al=SSL_AD_RECORD_OVERFLOW; sl@0: SSLerr(SSL_F_DTLS1_GET_RECORD,SSL_R_PACKET_LENGTH_TOO_LONG); sl@0: goto f_err; sl@0: } sl@0: sl@0: s->client_version = version; sl@0: /* now s->rstate == SSL_ST_READ_BODY */ sl@0: } sl@0: sl@0: /* s->rstate == SSL_ST_READ_BODY, get and decode the data */ sl@0: sl@0: if (rr->length > s->packet_length-DTLS1_RT_HEADER_LENGTH) sl@0: { sl@0: /* now s->packet_length == DTLS1_RT_HEADER_LENGTH */ sl@0: i=rr->length; sl@0: n=ssl3_read_n(s,i,i,1); sl@0: if (n <= 0) return(n); /* error or non-blocking io */ sl@0: sl@0: /* this packet contained a partial record, dump it */ sl@0: if ( n != i) sl@0: { sl@0: s->packet_length = 0; sl@0: goto again; sl@0: } sl@0: sl@0: /* now n == rr->length, sl@0: * and s->packet_length == DTLS1_RT_HEADER_LENGTH + rr->length */ sl@0: } sl@0: s->rstate=SSL_ST_READ_HEADER; /* set state for later operations */ sl@0: sl@0: /* match epochs. NULL means the packet is dropped on the floor */ sl@0: bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch); sl@0: if ( bitmap == NULL) sl@0: { sl@0: s->packet_length = 0; /* dump this record */ sl@0: goto again; /* get another record */ sl@0: } sl@0: sl@0: /* check whether this is a repeat, or aged record */ sl@0: if ( ! dtls1_record_replay_check(s, bitmap, &(rr->seq_num))) sl@0: { sl@0: s->packet_length=0; /* dump this record */ sl@0: goto again; /* get another record */ sl@0: } sl@0: sl@0: /* just read a 0 length packet */ sl@0: if (rr->length == 0) goto again; sl@0: sl@0: /* If this record is from the next epoch (either HM or ALERT), buffer it sl@0: * since it cannot be processed at this time. sl@0: * Records from the next epoch are marked as received even though they are sl@0: * not processed, so as to prevent any potential resource DoS attack */ sl@0: if (is_next_epoch) sl@0: { sl@0: dtls1_record_bitmap_update(s, bitmap); sl@0: dtls1_buffer_record(s, &(s->d1->unprocessed_rcds), rr->seq_num); sl@0: s->packet_length = 0; sl@0: goto again; sl@0: } sl@0: sl@0: if ( ! dtls1_process_record(s)) sl@0: return(0); sl@0: sl@0: dtls1_clear_timeouts(s); /* done waiting */ sl@0: return(1); sl@0: sl@0: f_err: sl@0: ssl3_send_alert(s,SSL3_AL_FATAL,al); sl@0: err: sl@0: return(0); sl@0: } sl@0: sl@0: /* Return up to 'len' payload bytes received in 'type' records. sl@0: * 'type' is one of the following: sl@0: * sl@0: * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us) sl@0: * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us) sl@0: * - 0 (during a shutdown, no data has to be returned) sl@0: * sl@0: * If we don't have stored data to work from, read a SSL/TLS record first sl@0: * (possibly multiple records if we still don't have anything to return). sl@0: * sl@0: * This function must handle any surprises the peer may have for us, such as sl@0: * Alert records (e.g. close_notify), ChangeCipherSpec records (not really sl@0: * a surprise, but handled as if it were), or renegotiation requests. sl@0: * Also if record payloads contain fragments too small to process, we store sl@0: * them until there is enough for the respective protocol (the record protocol sl@0: * may use arbitrary fragmentation and even interleaving): sl@0: * Change cipher spec protocol sl@0: * just 1 byte needed, no need for keeping anything stored sl@0: * Alert protocol sl@0: * 2 bytes needed (AlertLevel, AlertDescription) sl@0: * Handshake protocol sl@0: * 4 bytes needed (HandshakeType, uint24 length) -- we just have sl@0: * to detect unexpected Client Hello and Hello Request messages sl@0: * here, anything else is handled by higher layers sl@0: * Application data protocol sl@0: * none of our business sl@0: */ sl@0: int dtls1_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek) sl@0: { sl@0: int al,i,j,ret; sl@0: unsigned int n; sl@0: SSL3_RECORD *rr; sl@0: void (*cb)(const SSL *ssl,int type2,int val)=NULL; sl@0: sl@0: if (s->s3->rbuf.buf == NULL) /* Not initialized yet */ sl@0: if (!ssl3_setup_buffers(s)) sl@0: return(-1); sl@0: sl@0: /* XXX: check what the second '&& type' is about */ sl@0: if ((type && (type != SSL3_RT_APPLICATION_DATA) && sl@0: (type != SSL3_RT_HANDSHAKE) && type) || sl@0: (peek && (type != SSL3_RT_APPLICATION_DATA))) sl@0: { sl@0: SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR); sl@0: return -1; sl@0: } sl@0: sl@0: /* check whether there's a handshake message (client hello?) waiting */ sl@0: if ( (ret = have_handshake_fragment(s, type, buf, len, peek))) sl@0: return ret; sl@0: sl@0: /* Now s->d1->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE. */ sl@0: sl@0: if (!s->in_handshake && SSL_in_init(s)) sl@0: { sl@0: /* type == SSL3_RT_APPLICATION_DATA */ sl@0: i=s->handshake_func(s); sl@0: if (i < 0) return(i); sl@0: if (i == 0) sl@0: { sl@0: SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE); sl@0: return(-1); sl@0: } sl@0: } sl@0: sl@0: start: sl@0: s->rwstate=SSL_NOTHING; sl@0: sl@0: /* s->s3->rrec.type - is the type of record sl@0: * s->s3->rrec.data, - data sl@0: * s->s3->rrec.off, - offset into 'data' for next read sl@0: * s->s3->rrec.length, - number of bytes. */ sl@0: rr = &(s->s3->rrec); sl@0: sl@0: /* get new packet if necessary */ sl@0: if ((rr->length == 0) || (s->rstate == SSL_ST_READ_BODY)) sl@0: { sl@0: ret=dtls1_get_record(s); sl@0: if (ret <= 0) sl@0: { sl@0: ret = dtls1_read_failed(s, ret); sl@0: /* anything other than a timeout is an error */ sl@0: if (ret <= 0) sl@0: return(ret); sl@0: else sl@0: goto start; sl@0: } sl@0: } sl@0: sl@0: /* we now have a packet which can be read and processed */ sl@0: sl@0: if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec, sl@0: * reset by ssl3_get_finished */ sl@0: && (rr->type != SSL3_RT_HANDSHAKE)) sl@0: { sl@0: al=SSL_AD_UNEXPECTED_MESSAGE; sl@0: SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_DATA_BETWEEN_CCS_AND_FINISHED); sl@0: goto err; sl@0: } sl@0: sl@0: /* If the other end has shut down, throw anything we read away sl@0: * (even in 'peek' mode) */ sl@0: if (s->shutdown & SSL_RECEIVED_SHUTDOWN) sl@0: { sl@0: rr->length=0; sl@0: s->rwstate=SSL_NOTHING; sl@0: return(0); sl@0: } sl@0: sl@0: sl@0: if (type == rr->type) /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */ sl@0: { sl@0: /* make sure that we are not getting application data when we sl@0: * are doing a handshake for the first time */ sl@0: if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) && sl@0: (s->enc_read_ctx == NULL)) sl@0: { sl@0: al=SSL_AD_UNEXPECTED_MESSAGE; sl@0: SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_APP_DATA_IN_HANDSHAKE); sl@0: goto f_err; sl@0: } sl@0: sl@0: if (len <= 0) return(len); sl@0: sl@0: if ((unsigned int)len > rr->length) sl@0: n = rr->length; sl@0: else sl@0: n = (unsigned int)len; sl@0: sl@0: memcpy(buf,&(rr->data[rr->off]),n); sl@0: if (!peek) sl@0: { sl@0: rr->length-=n; sl@0: rr->off+=n; sl@0: if (rr->length == 0) sl@0: { sl@0: s->rstate=SSL_ST_READ_HEADER; sl@0: rr->off=0; sl@0: } sl@0: } sl@0: return(n); sl@0: } sl@0: sl@0: sl@0: /* If we get here, then type != rr->type; if we have a handshake sl@0: * message, then it was unexpected (Hello Request or Client Hello). */ sl@0: sl@0: /* In case of record types for which we have 'fragment' storage, sl@0: * fill that so that we can process the data at a fixed place. sl@0: */ sl@0: { sl@0: unsigned int k, dest_maxlen = 0; sl@0: unsigned char *dest = NULL; sl@0: unsigned int *dest_len = NULL; sl@0: sl@0: if (rr->type == SSL3_RT_HANDSHAKE) sl@0: { sl@0: dest_maxlen = sizeof s->d1->handshake_fragment; sl@0: dest = s->d1->handshake_fragment; sl@0: dest_len = &s->d1->handshake_fragment_len; sl@0: } sl@0: else if (rr->type == SSL3_RT_ALERT) sl@0: { sl@0: dest_maxlen = sizeof(s->d1->alert_fragment); sl@0: dest = s->d1->alert_fragment; sl@0: dest_len = &s->d1->alert_fragment_len; sl@0: } sl@0: /* else it's a CCS message, or it's wrong */ sl@0: else if (rr->type != SSL3_RT_CHANGE_CIPHER_SPEC) sl@0: { sl@0: /* Not certain if this is the right error handling */ sl@0: al=SSL_AD_UNEXPECTED_MESSAGE; sl@0: SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_UNEXPECTED_RECORD); sl@0: goto f_err; sl@0: } sl@0: sl@0: sl@0: if (dest_maxlen > 0) sl@0: { sl@0: /* XDTLS: In a pathalogical case, the Client Hello sl@0: * may be fragmented--don't always expect dest_maxlen bytes */ sl@0: if ( rr->length < dest_maxlen) sl@0: { sl@0: s->rstate=SSL_ST_READ_HEADER; sl@0: rr->length = 0; sl@0: goto start; sl@0: } sl@0: sl@0: /* now move 'n' bytes: */ sl@0: for ( k = 0; k < dest_maxlen; k++) sl@0: { sl@0: dest[k] = rr->data[rr->off++]; sl@0: rr->length--; sl@0: } sl@0: *dest_len = dest_maxlen; sl@0: } sl@0: } sl@0: sl@0: /* s->d1->handshake_fragment_len == 12 iff rr->type == SSL3_RT_HANDSHAKE; sl@0: * s->d1->alert_fragment_len == 7 iff rr->type == SSL3_RT_ALERT. sl@0: * (Possibly rr is 'empty' now, i.e. rr->length may be 0.) */ sl@0: sl@0: /* If we are a client, check for an incoming 'Hello Request': */ sl@0: if ((!s->server) && sl@0: (s->d1->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH) && sl@0: (s->d1->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST) && sl@0: (s->session != NULL) && (s->session->cipher != NULL)) sl@0: { sl@0: s->d1->handshake_fragment_len = 0; sl@0: sl@0: if ((s->d1->handshake_fragment[1] != 0) || sl@0: (s->d1->handshake_fragment[2] != 0) || sl@0: (s->d1->handshake_fragment[3] != 0)) sl@0: { sl@0: al=SSL_AD_DECODE_ERROR; sl@0: SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_BAD_HELLO_REQUEST); sl@0: goto err; sl@0: } sl@0: sl@0: /* no need to check sequence number on HELLO REQUEST messages */ sl@0: sl@0: if (s->msg_callback) sl@0: s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, sl@0: s->d1->handshake_fragment, 4, s, s->msg_callback_arg); sl@0: sl@0: if (SSL_is_init_finished(s) && sl@0: !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) && sl@0: !s->s3->renegotiate) sl@0: { sl@0: ssl3_renegotiate(s); sl@0: if (ssl3_renegotiate_check(s)) sl@0: { sl@0: i=s->handshake_func(s); sl@0: if (i < 0) return(i); sl@0: if (i == 0) sl@0: { sl@0: SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE); sl@0: return(-1); sl@0: } sl@0: sl@0: if (!(s->mode & SSL_MODE_AUTO_RETRY)) sl@0: { sl@0: if (s->s3->rbuf.left == 0) /* no read-ahead left? */ sl@0: { sl@0: BIO *bio; sl@0: /* In the case where we try to read application data, sl@0: * but we trigger an SSL handshake, we return -1 with sl@0: * the retry option set. Otherwise renegotiation may sl@0: * cause nasty problems in the blocking world */ sl@0: s->rwstate=SSL_READING; sl@0: bio=SSL_get_rbio(s); sl@0: BIO_clear_retry_flags(bio); sl@0: BIO_set_retry_read(bio); sl@0: return(-1); sl@0: } sl@0: } sl@0: } sl@0: } sl@0: /* we either finished a handshake or ignored the request, sl@0: * now try again to obtain the (application) data we were asked for */ sl@0: goto start; sl@0: } sl@0: sl@0: if (s->d1->alert_fragment_len >= DTLS1_AL_HEADER_LENGTH) sl@0: { sl@0: int alert_level = s->d1->alert_fragment[0]; sl@0: int alert_descr = s->d1->alert_fragment[1]; sl@0: sl@0: s->d1->alert_fragment_len = 0; sl@0: sl@0: if (s->msg_callback) sl@0: s->msg_callback(0, s->version, SSL3_RT_ALERT, sl@0: s->d1->alert_fragment, 2, s, s->msg_callback_arg); sl@0: sl@0: if (s->info_callback != NULL) sl@0: cb=s->info_callback; sl@0: else if (s->ctx->info_callback != NULL) sl@0: cb=s->ctx->info_callback; sl@0: sl@0: if (cb != NULL) sl@0: { sl@0: j = (alert_level << 8) | alert_descr; sl@0: cb(s, SSL_CB_READ_ALERT, j); sl@0: } sl@0: sl@0: if (alert_level == 1) /* warning */ sl@0: { sl@0: s->s3->warn_alert = alert_descr; sl@0: if (alert_descr == SSL_AD_CLOSE_NOTIFY) sl@0: { sl@0: s->shutdown |= SSL_RECEIVED_SHUTDOWN; sl@0: return(0); sl@0: } sl@0: #if 0 sl@0: /* XXX: this is a possible improvement in the future */ sl@0: /* now check if it's a missing record */ sl@0: if (alert_descr == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE) sl@0: { sl@0: unsigned short seq; sl@0: unsigned int frag_off; sl@0: unsigned char *p = &(s->d1->alert_fragment[2]); sl@0: sl@0: n2s(p, seq); sl@0: n2l3(p, frag_off); sl@0: sl@0: dtls1_retransmit_message(s, seq, frag_off, &found); sl@0: if ( ! found && SSL_in_init(s)) sl@0: { sl@0: /* fprintf( stderr,"in init = %d\n", SSL_in_init(s)); */ sl@0: /* requested a message not yet sent, sl@0: send an alert ourselves */ sl@0: ssl3_send_alert(s,SSL3_AL_WARNING, sl@0: DTLS1_AD_MISSING_HANDSHAKE_MESSAGE); sl@0: } sl@0: } sl@0: #endif sl@0: } sl@0: else if (alert_level == 2) /* fatal */ sl@0: { sl@0: char tmp[16]; sl@0: sl@0: s->rwstate=SSL_NOTHING; sl@0: s->s3->fatal_alert = alert_descr; sl@0: SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_AD_REASON_OFFSET + alert_descr); sl@0: BIO_snprintf(tmp,sizeof tmp,"%d",alert_descr); sl@0: ERR_add_error_data(2,"SSL alert number ",tmp); sl@0: s->shutdown|=SSL_RECEIVED_SHUTDOWN; sl@0: SSL_CTX_remove_session(s->ctx,s->session); sl@0: return(0); sl@0: } sl@0: else sl@0: { sl@0: al=SSL_AD_ILLEGAL_PARAMETER; sl@0: SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_UNKNOWN_ALERT_TYPE); sl@0: goto f_err; sl@0: } sl@0: sl@0: goto start; sl@0: } sl@0: sl@0: if (s->shutdown & SSL_SENT_SHUTDOWN) /* but we have not received a shutdown */ sl@0: { sl@0: s->rwstate=SSL_NOTHING; sl@0: rr->length=0; sl@0: return(0); sl@0: } sl@0: sl@0: if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) sl@0: { sl@0: struct ccs_header_st ccs_hdr; sl@0: sl@0: dtls1_get_ccs_header(rr->data, &ccs_hdr); sl@0: sl@0: /* 'Change Cipher Spec' is just a single byte, so we know sl@0: * exactly what the record payload has to look like */ sl@0: /* XDTLS: check that epoch is consistent */ sl@0: if ( (s->client_version == DTLS1_BAD_VER && rr->length != 3) || sl@0: (s->client_version != DTLS1_BAD_VER && rr->length != DTLS1_CCS_HEADER_LENGTH) || sl@0: (rr->off != 0) || (rr->data[0] != SSL3_MT_CCS)) sl@0: { sl@0: i=SSL_AD_ILLEGAL_PARAMETER; sl@0: SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_BAD_CHANGE_CIPHER_SPEC); sl@0: goto err; sl@0: } sl@0: sl@0: rr->length=0; sl@0: sl@0: if (s->msg_callback) sl@0: s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, sl@0: rr->data, 1, s, s->msg_callback_arg); sl@0: sl@0: s->s3->change_cipher_spec=1; sl@0: if (!ssl3_do_change_cipher_spec(s)) sl@0: goto err; sl@0: sl@0: /* do this whenever CCS is processed */ sl@0: dtls1_reset_seq_numbers(s, SSL3_CC_READ); sl@0: sl@0: if (s->client_version == DTLS1_BAD_VER) sl@0: s->d1->handshake_read_seq++; sl@0: sl@0: goto start; sl@0: } sl@0: sl@0: /* Unexpected handshake message (Client Hello, or protocol violation) */ sl@0: if ((s->d1->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH) && sl@0: !s->in_handshake) sl@0: { sl@0: struct hm_header_st msg_hdr; sl@0: sl@0: /* this may just be a stale retransmit */ sl@0: dtls1_get_message_header(rr->data, &msg_hdr); sl@0: if( rr->epoch != s->d1->r_epoch) sl@0: { sl@0: rr->length = 0; sl@0: goto start; sl@0: } sl@0: sl@0: if (((s->state&SSL_ST_MASK) == SSL_ST_OK) && sl@0: !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)) sl@0: { sl@0: #if 0 /* worked only because C operator preferences are not as expected (and sl@0: * because this is not really needed for clients except for detecting sl@0: * protocol violations): */ sl@0: s->state=SSL_ST_BEFORE|(s->server) sl@0: ?SSL_ST_ACCEPT sl@0: :SSL_ST_CONNECT; sl@0: #else sl@0: s->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT; sl@0: #endif sl@0: s->new_session=1; sl@0: } sl@0: i=s->handshake_func(s); sl@0: if (i < 0) return(i); sl@0: if (i == 0) sl@0: { sl@0: SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE); sl@0: return(-1); sl@0: } sl@0: sl@0: if (!(s->mode & SSL_MODE_AUTO_RETRY)) sl@0: { sl@0: if (s->s3->rbuf.left == 0) /* no read-ahead left? */ sl@0: { sl@0: BIO *bio; sl@0: /* In the case where we try to read application data, sl@0: * but we trigger an SSL handshake, we return -1 with sl@0: * the retry option set. Otherwise renegotiation may sl@0: * cause nasty problems in the blocking world */ sl@0: s->rwstate=SSL_READING; sl@0: bio=SSL_get_rbio(s); sl@0: BIO_clear_retry_flags(bio); sl@0: BIO_set_retry_read(bio); sl@0: return(-1); sl@0: } sl@0: } sl@0: goto start; sl@0: } sl@0: sl@0: switch (rr->type) sl@0: { sl@0: default: sl@0: #ifndef OPENSSL_NO_TLS sl@0: /* TLS just ignores unknown message types */ sl@0: if (s->version == TLS1_VERSION) sl@0: { sl@0: rr->length = 0; sl@0: goto start; sl@0: } sl@0: #endif sl@0: al=SSL_AD_UNEXPECTED_MESSAGE; sl@0: SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_UNEXPECTED_RECORD); sl@0: goto f_err; sl@0: case SSL3_RT_CHANGE_CIPHER_SPEC: sl@0: case SSL3_RT_ALERT: sl@0: case SSL3_RT_HANDSHAKE: sl@0: /* we already handled all of these, with the possible exception sl@0: * of SSL3_RT_HANDSHAKE when s->in_handshake is set, but that sl@0: * should not happen when type != rr->type */ sl@0: al=SSL_AD_UNEXPECTED_MESSAGE; sl@0: SSLerr(SSL_F_DTLS1_READ_BYTES,ERR_R_INTERNAL_ERROR); sl@0: goto f_err; sl@0: case SSL3_RT_APPLICATION_DATA: sl@0: /* At this point, we were expecting handshake data, sl@0: * but have application data. If the library was sl@0: * running inside ssl3_read() (i.e. in_read_app_data sl@0: * is set) and it makes sense to read application data sl@0: * at this point (session renegotiation not yet started), sl@0: * we will indulge it. sl@0: */ sl@0: if (s->s3->in_read_app_data && sl@0: (s->s3->total_renegotiations != 0) && sl@0: (( sl@0: (s->state & SSL_ST_CONNECT) && sl@0: (s->state >= SSL3_ST_CW_CLNT_HELLO_A) && sl@0: (s->state <= SSL3_ST_CR_SRVR_HELLO_A) sl@0: ) || ( sl@0: (s->state & SSL_ST_ACCEPT) && sl@0: (s->state <= SSL3_ST_SW_HELLO_REQ_A) && sl@0: (s->state >= SSL3_ST_SR_CLNT_HELLO_A) sl@0: ) sl@0: )) sl@0: { sl@0: s->s3->in_read_app_data=2; sl@0: return(-1); sl@0: } sl@0: else sl@0: { sl@0: al=SSL_AD_UNEXPECTED_MESSAGE; sl@0: SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_UNEXPECTED_RECORD); sl@0: goto f_err; sl@0: } sl@0: } sl@0: /* not reached */ sl@0: sl@0: f_err: sl@0: ssl3_send_alert(s,SSL3_AL_FATAL,al); sl@0: err: sl@0: return(-1); sl@0: } sl@0: sl@0: int sl@0: dtls1_write_app_data_bytes(SSL *s, int type, const void *buf_, int len) sl@0: { sl@0: unsigned int n,tot; sl@0: int i; sl@0: sl@0: if (SSL_in_init(s) && !s->in_handshake) sl@0: { sl@0: i=s->handshake_func(s); sl@0: if (i < 0) return(i); sl@0: if (i == 0) sl@0: { sl@0: SSLerr(SSL_F_DTLS1_WRITE_APP_DATA_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE); sl@0: return -1; sl@0: } sl@0: } sl@0: sl@0: tot = s->s3->wnum; sl@0: n = len - tot; sl@0: sl@0: while( n) sl@0: { sl@0: /* dtls1_write_bytes sends one record at a time, sized according to sl@0: * the currently known MTU */ sl@0: i = dtls1_write_bytes(s, type, buf_, len); sl@0: if (i <= 0) return i; sl@0: sl@0: if ((i == (int)n) || sl@0: (type == SSL3_RT_APPLICATION_DATA && sl@0: (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) sl@0: { sl@0: /* next chunk of data should get another prepended empty fragment sl@0: * in ciphersuites with known-IV weakness: */ sl@0: s->s3->empty_fragment_done = 0; sl@0: return tot+i; sl@0: } sl@0: sl@0: tot += i; sl@0: n-=i; sl@0: } sl@0: sl@0: return tot; sl@0: } sl@0: sl@0: sl@0: /* this only happens when a client hello is received and a handshake sl@0: * is started. */ sl@0: static int sl@0: have_handshake_fragment(SSL *s, int type, unsigned char *buf, sl@0: int len, int peek) sl@0: { sl@0: sl@0: if ((type == SSL3_RT_HANDSHAKE) && (s->d1->handshake_fragment_len > 0)) sl@0: /* (partially) satisfy request from storage */ sl@0: { sl@0: unsigned char *src = s->d1->handshake_fragment; sl@0: unsigned char *dst = buf; sl@0: unsigned int k,n; sl@0: sl@0: /* peek == 0 */ sl@0: n = 0; sl@0: while ((len > 0) && (s->d1->handshake_fragment_len > 0)) sl@0: { sl@0: *dst++ = *src++; sl@0: len--; s->d1->handshake_fragment_len--; sl@0: n++; sl@0: } sl@0: /* move any remaining fragment bytes: */ sl@0: for (k = 0; k < s->d1->handshake_fragment_len; k++) sl@0: s->d1->handshake_fragment[k] = *src++; sl@0: return n; sl@0: } sl@0: sl@0: return 0; sl@0: } sl@0: sl@0: sl@0: sl@0: sl@0: /* Call this to write data in records of type 'type' sl@0: * It will return <= 0 if not all data has been sent or non-blocking IO. sl@0: */ sl@0: int dtls1_write_bytes(SSL *s, int type, const void *buf_, int len) sl@0: { sl@0: const unsigned char *buf=buf_; sl@0: unsigned int tot,n,nw; sl@0: int i; sl@0: unsigned int mtu; sl@0: sl@0: s->rwstate=SSL_NOTHING; sl@0: tot=s->s3->wnum; sl@0: sl@0: n=(len-tot); sl@0: sl@0: /* handshake layer figures out MTU for itself, but data records sl@0: * are also sent through this interface, so need to figure out MTU */ sl@0: #if 0 sl@0: mtu = BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_GET_MTU, 0, NULL); sl@0: mtu += DTLS1_HM_HEADER_LENGTH; /* HM already inserted */ sl@0: #endif sl@0: mtu = s->d1->mtu; sl@0: sl@0: if (mtu > SSL3_RT_MAX_PLAIN_LENGTH) sl@0: mtu = SSL3_RT_MAX_PLAIN_LENGTH; sl@0: sl@0: if (n > mtu) sl@0: nw=mtu; sl@0: else sl@0: nw=n; sl@0: sl@0: i=do_dtls1_write(s, type, &(buf[tot]), nw, 0); sl@0: if (i <= 0) sl@0: { sl@0: s->s3->wnum=tot; sl@0: return i; sl@0: } sl@0: sl@0: if ( (int)s->s3->wnum + i == len) sl@0: s->s3->wnum = 0; sl@0: else sl@0: s->s3->wnum += i; sl@0: sl@0: return tot + i; sl@0: } sl@0: sl@0: int do_dtls1_write(SSL *s, int type, const unsigned char *buf, unsigned int len, int create_empty_fragment) sl@0: { sl@0: unsigned char *p,*pseq; sl@0: int i,mac_size,clear=0; sl@0: int prefix_len = 0; sl@0: SSL3_RECORD *wr; sl@0: SSL3_BUFFER *wb; sl@0: SSL_SESSION *sess; sl@0: int bs; sl@0: sl@0: /* first check if there is a SSL3_BUFFER still being written sl@0: * out. This will happen with non blocking IO */ sl@0: if (s->s3->wbuf.left != 0) sl@0: { sl@0: OPENSSL_assert(0); /* XDTLS: want to see if we ever get here */ sl@0: return(ssl3_write_pending(s,type,buf,len)); sl@0: } sl@0: sl@0: /* If we have an alert to send, lets send it */ sl@0: if (s->s3->alert_dispatch) sl@0: { sl@0: i=s->method->ssl_dispatch_alert(s); sl@0: if (i <= 0) sl@0: return(i); sl@0: /* if it went, fall through and send more stuff */ sl@0: } sl@0: sl@0: if (len == 0 && !create_empty_fragment) sl@0: return 0; sl@0: sl@0: wr= &(s->s3->wrec); sl@0: wb= &(s->s3->wbuf); sl@0: sess=s->session; sl@0: sl@0: if ( (sess == NULL) || sl@0: (s->enc_write_ctx == NULL) || sl@0: (s->write_hash == NULL)) sl@0: clear=1; sl@0: sl@0: if (clear) sl@0: mac_size=0; sl@0: else sl@0: mac_size=EVP_MD_size(s->write_hash); sl@0: sl@0: /* DTLS implements explicit IV, so no need for empty fragments */ sl@0: #if 0 sl@0: /* 'create_empty_fragment' is true only when this function calls itself */ sl@0: if (!clear && !create_empty_fragment && !s->s3->empty_fragment_done sl@0: && SSL_version(s) != DTLS1_VERSION) sl@0: { sl@0: /* countermeasure against known-IV weakness in CBC ciphersuites sl@0: * (see http://www.openssl.org/~bodo/tls-cbc.txt) sl@0: */ sl@0: sl@0: if (s->s3->need_empty_fragments && type == SSL3_RT_APPLICATION_DATA) sl@0: { sl@0: /* recursive function call with 'create_empty_fragment' set; sl@0: * this prepares and buffers the data for an empty fragment sl@0: * (these 'prefix_len' bytes are sent out later sl@0: * together with the actual payload) */ sl@0: prefix_len = s->method->do_ssl_write(s, type, buf, 0, 1); sl@0: if (prefix_len <= 0) sl@0: goto err; sl@0: sl@0: if (s->s3->wbuf.len < (size_t)prefix_len + SSL3_RT_MAX_PACKET_SIZE) sl@0: { sl@0: /* insufficient space */ sl@0: SSLerr(SSL_F_DO_DTLS1_WRITE, ERR_R_INTERNAL_ERROR); sl@0: goto err; sl@0: } sl@0: } sl@0: sl@0: s->s3->empty_fragment_done = 1; sl@0: } sl@0: #endif sl@0: sl@0: p = wb->buf + prefix_len; sl@0: sl@0: /* write the header */ sl@0: sl@0: *(p++)=type&0xff; sl@0: wr->type=type; sl@0: sl@0: if (s->client_version == DTLS1_BAD_VER) sl@0: *(p++) = DTLS1_BAD_VER>>8, sl@0: *(p++) = DTLS1_BAD_VER&0xff; sl@0: else sl@0: *(p++)=(s->version>>8), sl@0: *(p++)=s->version&0xff; sl@0: sl@0: /* field where we are to write out packet epoch, seq num and len */ sl@0: pseq=p; sl@0: p+=10; sl@0: sl@0: /* lets setup the record stuff. */ sl@0: sl@0: /* Make space for the explicit IV in case of CBC. sl@0: * (this is a bit of a boundary violation, but what the heck). sl@0: */ sl@0: if ( s->enc_write_ctx && sl@0: (EVP_CIPHER_mode( s->enc_write_ctx->cipher ) & EVP_CIPH_CBC_MODE)) sl@0: bs = EVP_CIPHER_block_size(s->enc_write_ctx->cipher); sl@0: else sl@0: bs = 0; sl@0: sl@0: wr->data=p + bs; /* make room for IV in case of CBC */ sl@0: wr->length=(int)len; sl@0: wr->input=(unsigned char *)buf; sl@0: sl@0: /* we now 'read' from wr->input, wr->length bytes into sl@0: * wr->data */ sl@0: sl@0: /* first we compress */ sl@0: if (s->compress != NULL) sl@0: { sl@0: if (!ssl3_do_compress(s)) sl@0: { sl@0: SSLerr(SSL_F_DO_DTLS1_WRITE,SSL_R_COMPRESSION_FAILURE); sl@0: goto err; sl@0: } sl@0: } sl@0: else sl@0: { sl@0: memcpy(wr->data,wr->input,wr->length); sl@0: wr->input=wr->data; sl@0: } sl@0: sl@0: /* we should still have the output to wr->data and the input sl@0: * from wr->input. Length should be wr->length. sl@0: * wr->data still points in the wb->buf */ sl@0: sl@0: if (mac_size != 0) sl@0: { sl@0: s->method->ssl3_enc->mac(s,&(p[wr->length + bs]),1); sl@0: wr->length+=mac_size; sl@0: } sl@0: sl@0: /* this is true regardless of mac size */ sl@0: wr->input=p; sl@0: wr->data=p; sl@0: sl@0: sl@0: /* ssl3_enc can only have an error on read */ sl@0: if (bs) /* bs != 0 in case of CBC */ sl@0: { sl@0: RAND_pseudo_bytes(p,bs); sl@0: /* master IV and last CBC residue stand for sl@0: * the rest of randomness */ sl@0: wr->length += bs; sl@0: } sl@0: sl@0: s->method->ssl3_enc->enc(s,1); sl@0: sl@0: /* record length after mac and block padding */ sl@0: /* if (type == SSL3_RT_APPLICATION_DATA || sl@0: (type == SSL3_RT_ALERT && ! SSL_in_init(s))) */ sl@0: sl@0: /* there's only one epoch between handshake and app data */ sl@0: sl@0: s2n(s->d1->w_epoch, pseq); sl@0: sl@0: /* XDTLS: ?? */ sl@0: /* else sl@0: s2n(s->d1->handshake_epoch, pseq); */ sl@0: sl@0: memcpy(pseq, &(s->s3->write_sequence[2]), 6); sl@0: pseq+=6; sl@0: s2n(wr->length,pseq); sl@0: sl@0: /* we should now have sl@0: * wr->data pointing to the encrypted data, which is sl@0: * wr->length long */ sl@0: wr->type=type; /* not needed but helps for debugging */ sl@0: wr->length+=DTLS1_RT_HEADER_LENGTH; sl@0: sl@0: #if 0 /* this is now done at the message layer */ sl@0: /* buffer the record, making it easy to handle retransmits */ sl@0: if ( type == SSL3_RT_HANDSHAKE || type == SSL3_RT_CHANGE_CIPHER_SPEC) sl@0: dtls1_buffer_record(s, wr->data, wr->length, sl@0: *((PQ_64BIT *)&(s->s3->write_sequence[0]))); sl@0: #endif sl@0: sl@0: ssl3_record_sequence_update(&(s->s3->write_sequence[0])); sl@0: sl@0: if (create_empty_fragment) sl@0: { sl@0: /* we are in a recursive call; sl@0: * just return the length, don't write out anything here sl@0: */ sl@0: return wr->length; sl@0: } sl@0: sl@0: /* now let's set up wb */ sl@0: wb->left = prefix_len + wr->length; sl@0: wb->offset = 0; sl@0: sl@0: /* memorize arguments so that ssl3_write_pending can detect bad write retries later */ sl@0: s->s3->wpend_tot=len; sl@0: s->s3->wpend_buf=buf; sl@0: s->s3->wpend_type=type; sl@0: s->s3->wpend_ret=len; sl@0: sl@0: /* we now just need to write the buffer */ sl@0: return ssl3_write_pending(s,type,buf,len); sl@0: err: sl@0: return -1; sl@0: } sl@0: sl@0: sl@0: sl@0: static int dtls1_record_replay_check(SSL *s, DTLS1_BITMAP *bitmap, sl@0: PQ_64BIT *seq_num) sl@0: { sl@0: #if PQ_64BIT_IS_INTEGER sl@0: PQ_64BIT mask = 0x0000000000000001L; sl@0: #endif sl@0: PQ_64BIT rcd_num, tmp; sl@0: sl@0: pq_64bit_init(&rcd_num); sl@0: pq_64bit_init(&tmp); sl@0: sl@0: /* this is the sequence number for the record just read */ sl@0: pq_64bit_bin2num(&rcd_num, s->s3->read_sequence, 8); sl@0: sl@0: sl@0: if (pq_64bit_gt(&rcd_num, &(bitmap->max_seq_num)) || sl@0: pq_64bit_eq(&rcd_num, &(bitmap->max_seq_num))) sl@0: { sl@0: pq_64bit_assign(seq_num, &rcd_num); sl@0: pq_64bit_free(&rcd_num); sl@0: pq_64bit_free(&tmp); sl@0: return 1; /* this record is new */ sl@0: } sl@0: sl@0: pq_64bit_sub(&tmp, &(bitmap->max_seq_num), &rcd_num); sl@0: sl@0: if ( pq_64bit_get_word(&tmp) > bitmap->length) sl@0: { sl@0: pq_64bit_free(&rcd_num); sl@0: pq_64bit_free(&tmp); sl@0: return 0; /* stale, outside the window */ sl@0: } sl@0: sl@0: #if PQ_64BIT_IS_BIGNUM sl@0: { sl@0: int offset; sl@0: pq_64bit_sub(&tmp, &(bitmap->max_seq_num), &rcd_num); sl@0: pq_64bit_sub_word(&tmp, 1); sl@0: offset = pq_64bit_get_word(&tmp); sl@0: if ( pq_64bit_is_bit_set(&(bitmap->map), offset)) sl@0: { sl@0: pq_64bit_free(&rcd_num); sl@0: pq_64bit_free(&tmp); sl@0: return 0; sl@0: } sl@0: } sl@0: #else sl@0: mask <<= (bitmap->max_seq_num - rcd_num - 1); sl@0: if (bitmap->map & mask) sl@0: return 0; /* record previously received */ sl@0: #endif sl@0: sl@0: pq_64bit_assign(seq_num, &rcd_num); sl@0: pq_64bit_free(&rcd_num); sl@0: pq_64bit_free(&tmp); sl@0: return 1; sl@0: } sl@0: sl@0: sl@0: static void dtls1_record_bitmap_update(SSL *s, DTLS1_BITMAP *bitmap) sl@0: { sl@0: unsigned int shift; sl@0: PQ_64BIT rcd_num; sl@0: PQ_64BIT tmp; sl@0: PQ_64BIT_CTX *ctx; sl@0: sl@0: pq_64bit_init(&rcd_num); sl@0: pq_64bit_init(&tmp); sl@0: sl@0: pq_64bit_bin2num(&rcd_num, s->s3->read_sequence, 8); sl@0: sl@0: /* unfortunate code complexity due to 64-bit manipulation support sl@0: * on 32-bit machines */ sl@0: if ( pq_64bit_gt(&rcd_num, &(bitmap->max_seq_num)) || sl@0: pq_64bit_eq(&rcd_num, &(bitmap->max_seq_num))) sl@0: { sl@0: pq_64bit_sub(&tmp, &rcd_num, &(bitmap->max_seq_num)); sl@0: pq_64bit_add_word(&tmp, 1); sl@0: sl@0: shift = (unsigned int)pq_64bit_get_word(&tmp); sl@0: sl@0: pq_64bit_lshift(&(tmp), &(bitmap->map), shift); sl@0: pq_64bit_assign(&(bitmap->map), &tmp); sl@0: sl@0: pq_64bit_set_bit(&(bitmap->map), 0); sl@0: pq_64bit_add_word(&rcd_num, 1); sl@0: pq_64bit_assign(&(bitmap->max_seq_num), &rcd_num); sl@0: sl@0: pq_64bit_assign_word(&tmp, 1); sl@0: pq_64bit_lshift(&tmp, &tmp, bitmap->length); sl@0: ctx = pq_64bit_ctx_new(&ctx); sl@0: pq_64bit_mod(&(bitmap->map), &(bitmap->map), &tmp, ctx); sl@0: pq_64bit_ctx_free(ctx); sl@0: } sl@0: else sl@0: { sl@0: pq_64bit_sub(&tmp, &(bitmap->max_seq_num), &rcd_num); sl@0: pq_64bit_sub_word(&tmp, 1); sl@0: shift = (unsigned int)pq_64bit_get_word(&tmp); sl@0: sl@0: pq_64bit_set_bit(&(bitmap->map), shift); sl@0: } sl@0: sl@0: pq_64bit_free(&rcd_num); sl@0: pq_64bit_free(&tmp); sl@0: } sl@0: sl@0: sl@0: int dtls1_dispatch_alert(SSL *s) sl@0: { sl@0: int i,j; sl@0: void (*cb)(const SSL *ssl,int type,int val)=NULL; sl@0: unsigned char buf[2 + 2 + 3]; /* alert level + alert desc + message seq +frag_off */ sl@0: unsigned char *ptr = &buf[0]; sl@0: sl@0: s->s3->alert_dispatch=0; sl@0: sl@0: memset(buf, 0x00, sizeof(buf)); sl@0: *ptr++ = s->s3->send_alert[0]; sl@0: *ptr++ = s->s3->send_alert[1]; sl@0: sl@0: if (s->s3->send_alert[1] == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE) sl@0: { sl@0: s2n(s->d1->handshake_read_seq, ptr); sl@0: #if 0 sl@0: if ( s->d1->r_msg_hdr.frag_off == 0) /* waiting for a new msg */ sl@0: sl@0: else sl@0: s2n(s->d1->r_msg_hdr.seq, ptr); /* partial msg read */ sl@0: #endif sl@0: sl@0: #if 0 sl@0: 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); sl@0: #endif sl@0: l2n3(s->d1->r_msg_hdr.frag_off, ptr); sl@0: } sl@0: sl@0: i = do_dtls1_write(s, SSL3_RT_ALERT, &buf[0], sizeof(buf), 0); sl@0: if (i <= 0) sl@0: { sl@0: s->s3->alert_dispatch=1; sl@0: /* fprintf( stderr, "not done with alert\n" ); */ sl@0: } sl@0: else sl@0: { sl@0: if ( s->s3->send_alert[0] == SSL3_AL_FATAL || sl@0: s->s3->send_alert[1] == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE) sl@0: (void)BIO_flush(s->wbio); sl@0: sl@0: if (s->msg_callback) sl@0: s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, sl@0: 2, s, s->msg_callback_arg); sl@0: sl@0: if (s->info_callback != NULL) sl@0: cb=s->info_callback; sl@0: else if (s->ctx->info_callback != NULL) sl@0: cb=s->ctx->info_callback; sl@0: sl@0: if (cb != NULL) sl@0: { sl@0: j=(s->s3->send_alert[0]<<8)|s->s3->send_alert[1]; sl@0: cb(s,SSL_CB_WRITE_ALERT,j); sl@0: } sl@0: } sl@0: return(i); sl@0: } sl@0: sl@0: sl@0: static DTLS1_BITMAP * sl@0: dtls1_get_bitmap(SSL *s, SSL3_RECORD *rr, unsigned int *is_next_epoch) sl@0: { sl@0: sl@0: *is_next_epoch = 0; sl@0: sl@0: /* In current epoch, accept HM, CCS, DATA, & ALERT */ sl@0: if (rr->epoch == s->d1->r_epoch) sl@0: return &s->d1->bitmap; sl@0: sl@0: /* Only HM and ALERT messages can be from the next epoch */ sl@0: else if (rr->epoch == (unsigned long)(s->d1->r_epoch + 1) && sl@0: (rr->type == SSL3_RT_HANDSHAKE || sl@0: rr->type == SSL3_RT_ALERT)) sl@0: { sl@0: *is_next_epoch = 1; sl@0: return &s->d1->next_bitmap; sl@0: } sl@0: sl@0: return NULL; sl@0: } sl@0: sl@0: #if 0 sl@0: static int sl@0: dtls1_record_needs_buffering(SSL *s, SSL3_RECORD *rr, unsigned short *priority, sl@0: unsigned long *offset) sl@0: { sl@0: sl@0: /* alerts are passed up immediately */ sl@0: if ( rr->type == SSL3_RT_APPLICATION_DATA || sl@0: rr->type == SSL3_RT_ALERT) sl@0: return 0; sl@0: sl@0: /* Only need to buffer if a handshake is underway. sl@0: * (this implies that Hello Request and Client Hello are passed up sl@0: * immediately) */ sl@0: if ( SSL_in_init(s)) sl@0: { sl@0: unsigned char *data = rr->data; sl@0: /* need to extract the HM/CCS sequence number here */ sl@0: if ( rr->type == SSL3_RT_HANDSHAKE || sl@0: rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) sl@0: { sl@0: unsigned short seq_num; sl@0: struct hm_header_st msg_hdr; sl@0: struct ccs_header_st ccs_hdr; sl@0: sl@0: if ( rr->type == SSL3_RT_HANDSHAKE) sl@0: { sl@0: dtls1_get_message_header(data, &msg_hdr); sl@0: seq_num = msg_hdr.seq; sl@0: *offset = msg_hdr.frag_off; sl@0: } sl@0: else sl@0: { sl@0: dtls1_get_ccs_header(data, &ccs_hdr); sl@0: seq_num = ccs_hdr.seq; sl@0: *offset = 0; sl@0: } sl@0: sl@0: /* this is either a record we're waiting for, or a sl@0: * retransmit of something we happened to previously sl@0: * receive (higher layers will drop the repeat silently */ sl@0: if ( seq_num < s->d1->handshake_read_seq) sl@0: return 0; sl@0: if (rr->type == SSL3_RT_HANDSHAKE && sl@0: seq_num == s->d1->handshake_read_seq && sl@0: msg_hdr.frag_off < s->d1->r_msg_hdr.frag_off) sl@0: return 0; sl@0: else if ( seq_num == s->d1->handshake_read_seq && sl@0: (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC || sl@0: msg_hdr.frag_off == s->d1->r_msg_hdr.frag_off)) sl@0: return 0; sl@0: else sl@0: { sl@0: *priority = seq_num; sl@0: return 1; sl@0: } sl@0: } sl@0: else /* unknown record type */ sl@0: return 0; sl@0: } sl@0: sl@0: return 0; sl@0: } sl@0: #endif sl@0: sl@0: void sl@0: dtls1_reset_seq_numbers(SSL *s, int rw) sl@0: { sl@0: unsigned char *seq; sl@0: unsigned int seq_bytes = sizeof(s->s3->read_sequence); sl@0: sl@0: if ( rw & SSL3_CC_READ) sl@0: { sl@0: seq = s->s3->read_sequence; sl@0: s->d1->r_epoch++; sl@0: sl@0: pq_64bit_assign(&(s->d1->bitmap.map), &(s->d1->next_bitmap.map)); sl@0: s->d1->bitmap.length = s->d1->next_bitmap.length; sl@0: pq_64bit_assign(&(s->d1->bitmap.max_seq_num), sl@0: &(s->d1->next_bitmap.max_seq_num)); sl@0: sl@0: pq_64bit_free(&(s->d1->next_bitmap.map)); sl@0: pq_64bit_free(&(s->d1->next_bitmap.max_seq_num)); sl@0: memset(&(s->d1->next_bitmap), 0x00, sizeof(DTLS1_BITMAP)); sl@0: pq_64bit_init(&(s->d1->next_bitmap.map)); sl@0: pq_64bit_init(&(s->d1->next_bitmap.max_seq_num)); sl@0: } sl@0: else sl@0: { sl@0: seq = s->s3->write_sequence; sl@0: s->d1->w_epoch++; sl@0: } sl@0: sl@0: memset(seq, 0x00, seq_bytes); sl@0: } sl@0: sl@0: #if PQ_64BIT_IS_INTEGER sl@0: static PQ_64BIT sl@0: bytes_to_long_long(unsigned char *bytes, PQ_64BIT *num) sl@0: { sl@0: PQ_64BIT _num; sl@0: sl@0: _num = (((PQ_64BIT)bytes[0]) << 56) | sl@0: (((PQ_64BIT)bytes[1]) << 48) | sl@0: (((PQ_64BIT)bytes[2]) << 40) | sl@0: (((PQ_64BIT)bytes[3]) << 32) | sl@0: (((PQ_64BIT)bytes[4]) << 24) | sl@0: (((PQ_64BIT)bytes[5]) << 16) | sl@0: (((PQ_64BIT)bytes[6]) << 8) | sl@0: (((PQ_64BIT)bytes[7]) ); sl@0: sl@0: *num = _num ; sl@0: return _num; sl@0: } sl@0: #endif sl@0: sl@0: sl@0: static void sl@0: dtls1_clear_timeouts(SSL *s) sl@0: { sl@0: memset(&(s->d1->timeout), 0x00, sizeof(struct dtls1_timeout_st)); sl@0: }