12139Sjp161948 /* ssl/d1_pkt.c */
22139Sjp161948 /*
32139Sjp161948 * DTLS implementation written by Nagendra Modadugu
42139Sjp161948 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
52139Sjp161948 */
62139Sjp161948 /* ====================================================================
72139Sjp161948 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
82139Sjp161948 *
92139Sjp161948 * Redistribution and use in source and binary forms, with or without
102139Sjp161948 * modification, are permitted provided that the following conditions
112139Sjp161948 * are met:
122139Sjp161948 *
132139Sjp161948 * 1. Redistributions of source code must retain the above copyright
142139Sjp161948 * notice, this list of conditions and the following disclaimer.
152139Sjp161948 *
162139Sjp161948 * 2. Redistributions in binary form must reproduce the above copyright
172139Sjp161948 * notice, this list of conditions and the following disclaimer in
182139Sjp161948 * the documentation and/or other materials provided with the
192139Sjp161948 * distribution.
202139Sjp161948 *
212139Sjp161948 * 3. All advertising materials mentioning features or use of this
222139Sjp161948 * software must display the following acknowledgment:
232139Sjp161948 * "This product includes software developed by the OpenSSL Project
242139Sjp161948 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
252139Sjp161948 *
262139Sjp161948 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
272139Sjp161948 * endorse or promote products derived from this software without
282139Sjp161948 * prior written permission. For written permission, please contact
292139Sjp161948 * openssl-core@openssl.org.
302139Sjp161948 *
312139Sjp161948 * 5. Products derived from this software may not be called "OpenSSL"
322139Sjp161948 * nor may "OpenSSL" appear in their names without prior written
332139Sjp161948 * permission of the OpenSSL Project.
342139Sjp161948 *
352139Sjp161948 * 6. Redistributions of any form whatsoever must retain the following
362139Sjp161948 * acknowledgment:
372139Sjp161948 * "This product includes software developed by the OpenSSL Project
382139Sjp161948 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
392139Sjp161948 *
402139Sjp161948 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
412139Sjp161948 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
422139Sjp161948 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
432139Sjp161948 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
442139Sjp161948 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
452139Sjp161948 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
462139Sjp161948 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
472139Sjp161948 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
482139Sjp161948 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
492139Sjp161948 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
502139Sjp161948 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
512139Sjp161948 * OF THE POSSIBILITY OF SUCH DAMAGE.
522139Sjp161948 * ====================================================================
532139Sjp161948 *
542139Sjp161948 * This product includes cryptographic software written by Eric Young
552139Sjp161948 * (eay@cryptsoft.com). This product includes software written by Tim
562139Sjp161948 * Hudson (tjh@cryptsoft.com).
572139Sjp161948 *
582139Sjp161948 */
592139Sjp161948 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
602139Sjp161948 * All rights reserved.
612139Sjp161948 *
622139Sjp161948 * This package is an SSL implementation written
632139Sjp161948 * by Eric Young (eay@cryptsoft.com).
642139Sjp161948 * The implementation was written so as to conform with Netscapes SSL.
652139Sjp161948 *
662139Sjp161948 * This library is free for commercial and non-commercial use as long as
672139Sjp161948 * the following conditions are aheared to. The following conditions
682139Sjp161948 * apply to all code found in this distribution, be it the RC4, RSA,
692139Sjp161948 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
702139Sjp161948 * included with this distribution is covered by the same copyright terms
712139Sjp161948 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
722139Sjp161948 *
732139Sjp161948 * Copyright remains Eric Young's, and as such any Copyright notices in
742139Sjp161948 * the code are not to be removed.
752139Sjp161948 * If this package is used in a product, Eric Young should be given attribution
762139Sjp161948 * as the author of the parts of the library used.
772139Sjp161948 * This can be in the form of a textual message at program startup or
782139Sjp161948 * in documentation (online or textual) provided with the package.
792139Sjp161948 *
802139Sjp161948 * Redistribution and use in source and binary forms, with or without
812139Sjp161948 * modification, are permitted provided that the following conditions
822139Sjp161948 * are met:
832139Sjp161948 * 1. Redistributions of source code must retain the copyright
842139Sjp161948 * notice, this list of conditions and the following disclaimer.
852139Sjp161948 * 2. Redistributions in binary form must reproduce the above copyright
862139Sjp161948 * notice, this list of conditions and the following disclaimer in the
872139Sjp161948 * documentation and/or other materials provided with the distribution.
882139Sjp161948 * 3. All advertising materials mentioning features or use of this software
892139Sjp161948 * must display the following acknowledgement:
902139Sjp161948 * "This product includes cryptographic software written by
912139Sjp161948 * Eric Young (eay@cryptsoft.com)"
922139Sjp161948 * The word 'cryptographic' can be left out if the rouines from the library
932139Sjp161948 * being used are not cryptographic related :-).
942139Sjp161948 * 4. If you include any Windows specific code (or a derivative thereof) from
952139Sjp161948 * the apps directory (application code) you must include an acknowledgement:
962139Sjp161948 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
972139Sjp161948 *
982139Sjp161948 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
992139Sjp161948 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1002139Sjp161948 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1012139Sjp161948 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1022139Sjp161948 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1032139Sjp161948 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1042139Sjp161948 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1052139Sjp161948 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1062139Sjp161948 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1072139Sjp161948 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1082139Sjp161948 * SUCH DAMAGE.
1092139Sjp161948 *
1102139Sjp161948 * The licence and distribution terms for any publically available version or
1112139Sjp161948 * derivative of this code cannot be changed. i.e. this code cannot simply be
1122139Sjp161948 * copied and put under another distribution licence
1132139Sjp161948 * [including the GNU Public Licence.]
1142139Sjp161948 */
1152139Sjp161948
1162139Sjp161948 #include <stdio.h>
1172139Sjp161948 #include <errno.h>
1182139Sjp161948 #define USE_SOCKETS
1192139Sjp161948 #include "ssl_locl.h"
1202139Sjp161948 #include <openssl/evp.h>
1212139Sjp161948 #include <openssl/buffer.h>
1222139Sjp161948 #include <openssl/pqueue.h>
123*5434Sjp161948 #include <openssl/rand.h>
1242139Sjp161948
1252139Sjp161948 static int have_handshake_fragment(SSL *s, int type, unsigned char *buf,
1262139Sjp161948 int len, int peek);
1272139Sjp161948 static int dtls1_record_replay_check(SSL *s, DTLS1_BITMAP *bitmap,
1282139Sjp161948 PQ_64BIT *seq_num);
1292139Sjp161948 static void dtls1_record_bitmap_update(SSL *s, DTLS1_BITMAP *bitmap);
1302139Sjp161948 static DTLS1_BITMAP *dtls1_get_bitmap(SSL *s, SSL3_RECORD *rr,
1312139Sjp161948 unsigned int *is_next_epoch);
1322139Sjp161948 #if 0
1332139Sjp161948 static int dtls1_record_needs_buffering(SSL *s, SSL3_RECORD *rr,
1342139Sjp161948 unsigned short *priority, unsigned long *offset);
1352139Sjp161948 #endif
1362139Sjp161948 static int dtls1_buffer_record(SSL *s, record_pqueue *q,
1372139Sjp161948 PQ_64BIT priority);
1382139Sjp161948 static int dtls1_process_record(SSL *s);
1392139Sjp161948 #if PQ_64BIT_IS_INTEGER
1402139Sjp161948 static PQ_64BIT bytes_to_long_long(unsigned char *bytes, PQ_64BIT *num);
1412139Sjp161948 #endif
1422139Sjp161948 static void dtls1_clear_timeouts(SSL *s);
1432139Sjp161948
1442139Sjp161948 /* copy buffered record into SSL structure */
1452139Sjp161948 static int
dtls1_copy_record(SSL * s,pitem * item)1462139Sjp161948 dtls1_copy_record(SSL *s, pitem *item)
1472139Sjp161948 {
1482139Sjp161948 DTLS1_RECORD_DATA *rdata;
1492139Sjp161948
1502139Sjp161948 rdata = (DTLS1_RECORD_DATA *)item->data;
1512139Sjp161948
1522139Sjp161948 if (s->s3->rbuf.buf != NULL)
1532139Sjp161948 OPENSSL_free(s->s3->rbuf.buf);
1542139Sjp161948
1552139Sjp161948 s->packet = rdata->packet;
1562139Sjp161948 s->packet_length = rdata->packet_length;
1572139Sjp161948 memcpy(&(s->s3->rbuf), &(rdata->rbuf), sizeof(SSL3_BUFFER));
1582139Sjp161948 memcpy(&(s->s3->rrec), &(rdata->rrec), sizeof(SSL3_RECORD));
1592139Sjp161948
1602139Sjp161948 return(1);
1612139Sjp161948 }
1622139Sjp161948
1632139Sjp161948
1642139Sjp161948 static int
dtls1_buffer_record(SSL * s,record_pqueue * queue,PQ_64BIT priority)1652139Sjp161948 dtls1_buffer_record(SSL *s, record_pqueue *queue, PQ_64BIT priority)
1662139Sjp161948 {
1672139Sjp161948 DTLS1_RECORD_DATA *rdata;
1682139Sjp161948 pitem *item;
1692139Sjp161948
1702139Sjp161948 rdata = OPENSSL_malloc(sizeof(DTLS1_RECORD_DATA));
1712139Sjp161948 item = pitem_new(priority, rdata);
1722139Sjp161948 if (rdata == NULL || item == NULL)
1732139Sjp161948 {
1742139Sjp161948 if (rdata != NULL) OPENSSL_free(rdata);
1752139Sjp161948 if (item != NULL) pitem_free(item);
1762139Sjp161948
1772139Sjp161948 SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR);
1782139Sjp161948 return(0);
1792139Sjp161948 }
1802139Sjp161948
1812139Sjp161948 rdata->packet = s->packet;
1822139Sjp161948 rdata->packet_length = s->packet_length;
1832139Sjp161948 memcpy(&(rdata->rbuf), &(s->s3->rbuf), sizeof(SSL3_BUFFER));
1842139Sjp161948 memcpy(&(rdata->rrec), &(s->s3->rrec), sizeof(SSL3_RECORD));
1852139Sjp161948
1862139Sjp161948 item->data = rdata;
1872139Sjp161948
1882139Sjp161948 /* insert should not fail, since duplicates are dropped */
1892139Sjp161948 if (pqueue_insert(queue->q, item) == NULL)
1902139Sjp161948 {
1912139Sjp161948 OPENSSL_free(rdata);
1922139Sjp161948 pitem_free(item);
1932139Sjp161948 return(0);
1942139Sjp161948 }
1952139Sjp161948
1962139Sjp161948 s->packet = NULL;
1972139Sjp161948 s->packet_length = 0;
1982139Sjp161948 memset(&(s->s3->rbuf), 0, sizeof(SSL3_BUFFER));
1992139Sjp161948 memset(&(s->s3->rrec), 0, sizeof(SSL3_RECORD));
2002139Sjp161948
201*5434Sjp161948 if (!ssl3_setup_buffers(s))
202*5434Sjp161948 {
203*5434Sjp161948 SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR);
204*5434Sjp161948 OPENSSL_free(rdata);
205*5434Sjp161948 pitem_free(item);
206*5434Sjp161948 return(0);
207*5434Sjp161948 }
2082139Sjp161948
2092139Sjp161948 return(1);
2102139Sjp161948 }
2112139Sjp161948
2122139Sjp161948
2132139Sjp161948 static int
dtls1_retrieve_buffered_record(SSL * s,record_pqueue * queue)2142139Sjp161948 dtls1_retrieve_buffered_record(SSL *s, record_pqueue *queue)
2152139Sjp161948 {
2162139Sjp161948 pitem *item;
2172139Sjp161948
2182139Sjp161948 item = pqueue_pop(queue->q);
2192139Sjp161948 if (item)
2202139Sjp161948 {
2212139Sjp161948 dtls1_copy_record(s, item);
2222139Sjp161948
2232139Sjp161948 OPENSSL_free(item->data);
2242139Sjp161948 pitem_free(item);
2252139Sjp161948
2262139Sjp161948 return(1);
2272139Sjp161948 }
2282139Sjp161948
2292139Sjp161948 return(0);
2302139Sjp161948 }
2312139Sjp161948
2322139Sjp161948
2332139Sjp161948 /* retrieve a buffered record that belongs to the new epoch, i.e., not processed
2342139Sjp161948 * yet */
2352139Sjp161948 #define dtls1_get_unprocessed_record(s) \
2362139Sjp161948 dtls1_retrieve_buffered_record((s), \
2372139Sjp161948 &((s)->d1->unprocessed_rcds))
2382139Sjp161948
2392139Sjp161948 /* retrieve a buffered record that belongs to the current epoch, ie, processed */
2402139Sjp161948 #define dtls1_get_processed_record(s) \
2412139Sjp161948 dtls1_retrieve_buffered_record((s), \
2422139Sjp161948 &((s)->d1->processed_rcds))
2432139Sjp161948
2442139Sjp161948 static int
dtls1_process_buffered_records(SSL * s)2452139Sjp161948 dtls1_process_buffered_records(SSL *s)
2462139Sjp161948 {
2472139Sjp161948 pitem *item;
2482139Sjp161948
2492139Sjp161948 item = pqueue_peek(s->d1->unprocessed_rcds.q);
2502139Sjp161948 if (item)
2512139Sjp161948 {
2522139Sjp161948 DTLS1_RECORD_DATA *rdata;
2532139Sjp161948 rdata = (DTLS1_RECORD_DATA *)item->data;
2542139Sjp161948
2552139Sjp161948 /* Check if epoch is current. */
2562139Sjp161948 if (s->d1->unprocessed_rcds.epoch != s->d1->r_epoch)
2572139Sjp161948 return(1); /* Nothing to do. */
2582139Sjp161948
2592139Sjp161948 /* Process all the records. */
2602139Sjp161948 while (pqueue_peek(s->d1->unprocessed_rcds.q))
2612139Sjp161948 {
2622139Sjp161948 dtls1_get_unprocessed_record(s);
2632139Sjp161948 if ( ! dtls1_process_record(s))
2642139Sjp161948 return(0);
2652139Sjp161948 dtls1_buffer_record(s, &(s->d1->processed_rcds),
2662139Sjp161948 s->s3->rrec.seq_num);
2672139Sjp161948 }
2682139Sjp161948 }
2692139Sjp161948
2702139Sjp161948 /* sync epoch numbers once all the unprocessed records
2712139Sjp161948 * have been processed */
2722139Sjp161948 s->d1->processed_rcds.epoch = s->d1->r_epoch;
2732139Sjp161948 s->d1->unprocessed_rcds.epoch = s->d1->r_epoch + 1;
2742139Sjp161948
2752139Sjp161948 return(1);
2762139Sjp161948 }
2772139Sjp161948
2782139Sjp161948
2792139Sjp161948 #if 0
2802139Sjp161948
2812139Sjp161948 static int
2822139Sjp161948 dtls1_get_buffered_record(SSL *s)
2832139Sjp161948 {
2842139Sjp161948 pitem *item;
2852139Sjp161948 PQ_64BIT priority =
2862139Sjp161948 (((PQ_64BIT)s->d1->handshake_read_seq) << 32) |
2872139Sjp161948 ((PQ_64BIT)s->d1->r_msg_hdr.frag_off);
2882139Sjp161948
2892139Sjp161948 if ( ! SSL_in_init(s)) /* if we're not (re)negotiating,
2902139Sjp161948 nothing buffered */
2912139Sjp161948 return 0;
2922139Sjp161948
2932139Sjp161948
2942139Sjp161948 item = pqueue_peek(s->d1->rcvd_records);
2952139Sjp161948 if (item && item->priority == priority)
2962139Sjp161948 {
2972139Sjp161948 /* Check if we've received the record of interest. It must be
2982139Sjp161948 * a handshake record, since data records as passed up without
2992139Sjp161948 * buffering */
3002139Sjp161948 DTLS1_RECORD_DATA *rdata;
3012139Sjp161948 item = pqueue_pop(s->d1->rcvd_records);
3022139Sjp161948 rdata = (DTLS1_RECORD_DATA *)item->data;
3032139Sjp161948
3042139Sjp161948 if (s->s3->rbuf.buf != NULL)
3052139Sjp161948 OPENSSL_free(s->s3->rbuf.buf);
3062139Sjp161948
3072139Sjp161948 s->packet = rdata->packet;
3082139Sjp161948 s->packet_length = rdata->packet_length;
3092139Sjp161948 memcpy(&(s->s3->rbuf), &(rdata->rbuf), sizeof(SSL3_BUFFER));
3102139Sjp161948 memcpy(&(s->s3->rrec), &(rdata->rrec), sizeof(SSL3_RECORD));
3112139Sjp161948
3122139Sjp161948 OPENSSL_free(item->data);
3132139Sjp161948 pitem_free(item);
3142139Sjp161948
3152139Sjp161948 /* s->d1->next_expected_seq_num++; */
3162139Sjp161948 return(1);
3172139Sjp161948 }
3182139Sjp161948
3192139Sjp161948 return 0;
3202139Sjp161948 }
3212139Sjp161948
3222139Sjp161948 #endif
3232139Sjp161948
3242139Sjp161948 static int
dtls1_process_record(SSL * s)3252139Sjp161948 dtls1_process_record(SSL *s)
3262139Sjp161948 {
3272139Sjp161948 int i,al;
3282139Sjp161948 int clear=0;
3292139Sjp161948 int enc_err;
3302139Sjp161948 SSL_SESSION *sess;
3312139Sjp161948 SSL3_RECORD *rr;
3322139Sjp161948 unsigned int mac_size;
3332139Sjp161948 unsigned char md[EVP_MAX_MD_SIZE];
3342139Sjp161948
3352139Sjp161948
3362139Sjp161948 rr= &(s->s3->rrec);
3372139Sjp161948 sess = s->session;
3382139Sjp161948
3392139Sjp161948 /* At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length,
3402139Sjp161948 * and we have that many bytes in s->packet
3412139Sjp161948 */
3422139Sjp161948 rr->input= &(s->packet[DTLS1_RT_HEADER_LENGTH]);
3432139Sjp161948
3442139Sjp161948 /* ok, we can now read from 's->packet' data into 'rr'
3452139Sjp161948 * rr->input points at rr->length bytes, which
3462139Sjp161948 * need to be copied into rr->data by either
3472139Sjp161948 * the decryption or by the decompression
3482139Sjp161948 * When the data is 'copied' into the rr->data buffer,
3492139Sjp161948 * rr->input will be pointed at the new buffer */
3502139Sjp161948
3512139Sjp161948 /* We now have - encrypted [ MAC [ compressed [ plain ] ] ]
3522139Sjp161948 * rr->length bytes of encrypted compressed stuff. */
3532139Sjp161948
3542139Sjp161948 /* check is not needed I believe */
3552139Sjp161948 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH)
3562139Sjp161948 {
3572139Sjp161948 al=SSL_AD_RECORD_OVERFLOW;
3582139Sjp161948 SSLerr(SSL_F_DTLS1_PROCESS_RECORD,SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
3592139Sjp161948 goto f_err;
3602139Sjp161948 }
3612139Sjp161948
3622139Sjp161948 /* decrypt in place in 'rr->input' */
3632139Sjp161948 rr->data=rr->input;
3642139Sjp161948
3652139Sjp161948 enc_err = s->method->ssl3_enc->enc(s,0);
3662139Sjp161948 if (enc_err <= 0)
3672139Sjp161948 {
3682139Sjp161948 if (enc_err == 0)
3692139Sjp161948 /* SSLerr() and ssl3_send_alert() have been called */
3702139Sjp161948 goto err;
3712139Sjp161948
3722139Sjp161948 /* otherwise enc_err == -1 */
3732139Sjp161948 goto decryption_failed_or_bad_record_mac;
3742139Sjp161948 }
3752139Sjp161948
3762139Sjp161948 #ifdef TLS_DEBUG
3772139Sjp161948 printf("dec %d\n",rr->length);
3782139Sjp161948 { unsigned int z; for (z=0; z<rr->length; z++) printf("%02X%c",rr->data[z],((z+1)%16)?' ':'\n'); }
3792139Sjp161948 printf("\n");
3802139Sjp161948 #endif
3812139Sjp161948
3822139Sjp161948 /* r->length is now the compressed data plus mac */
3832139Sjp161948 if ( (sess == NULL) ||
3842139Sjp161948 (s->enc_read_ctx == NULL) ||
3852139Sjp161948 (s->read_hash == NULL))
3862139Sjp161948 clear=1;
3872139Sjp161948
3882139Sjp161948 if (!clear)
3892139Sjp161948 {
3902139Sjp161948 mac_size=EVP_MD_size(s->read_hash);
3912139Sjp161948
3922139Sjp161948 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH+mac_size)
3932139Sjp161948 {
3942139Sjp161948 #if 0 /* OK only for stream ciphers (then rr->length is visible from ciphertext anyway) */
3952139Sjp161948 al=SSL_AD_RECORD_OVERFLOW;
3962139Sjp161948 SSLerr(SSL_F_DTLS1_PROCESS_RECORD,SSL_R_PRE_MAC_LENGTH_TOO_LONG);
3972139Sjp161948 goto f_err;
3982139Sjp161948 #else
3992139Sjp161948 goto decryption_failed_or_bad_record_mac;
4002139Sjp161948 #endif
4012139Sjp161948 }
4022139Sjp161948 /* check the MAC for rr->input (it's in mac_size bytes at the tail) */
4032139Sjp161948 if (rr->length < mac_size)
4042139Sjp161948 {
4052139Sjp161948 #if 0 /* OK only for stream ciphers */
4062139Sjp161948 al=SSL_AD_DECODE_ERROR;
4072139Sjp161948 SSLerr(SSL_F_DTLS1_PROCESS_RECORD,SSL_R_LENGTH_TOO_SHORT);
4082139Sjp161948 goto f_err;
4092139Sjp161948 #else
4102139Sjp161948 goto decryption_failed_or_bad_record_mac;
4112139Sjp161948 #endif
4122139Sjp161948 }
4132139Sjp161948 rr->length-=mac_size;
4142139Sjp161948 i=s->method->ssl3_enc->mac(s,md,0);
4152139Sjp161948 if (memcmp(md,&(rr->data[rr->length]),mac_size) != 0)
4162139Sjp161948 {
4172139Sjp161948 goto decryption_failed_or_bad_record_mac;
4182139Sjp161948 }
4192139Sjp161948 }
4202139Sjp161948
4212139Sjp161948 /* r->length is now just compressed */
4222139Sjp161948 if (s->expand != NULL)
4232139Sjp161948 {
4242139Sjp161948 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH)
4252139Sjp161948 {
4262139Sjp161948 al=SSL_AD_RECORD_OVERFLOW;
4272139Sjp161948 SSLerr(SSL_F_DTLS1_PROCESS_RECORD,SSL_R_COMPRESSED_LENGTH_TOO_LONG);
4282139Sjp161948 goto f_err;
4292139Sjp161948 }
4302139Sjp161948 if (!ssl3_do_uncompress(s))
4312139Sjp161948 {
4322139Sjp161948 al=SSL_AD_DECOMPRESSION_FAILURE;
4332139Sjp161948 SSLerr(SSL_F_DTLS1_PROCESS_RECORD,SSL_R_BAD_DECOMPRESSION);
4342139Sjp161948 goto f_err;
4352139Sjp161948 }
4362139Sjp161948 }
4372139Sjp161948
4382139Sjp161948 if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH)
4392139Sjp161948 {
4402139Sjp161948 al=SSL_AD_RECORD_OVERFLOW;
4412139Sjp161948 SSLerr(SSL_F_DTLS1_PROCESS_RECORD,SSL_R_DATA_LENGTH_TOO_LONG);
4422139Sjp161948 goto f_err;
4432139Sjp161948 }
4442139Sjp161948
4452139Sjp161948 rr->off=0;
4462139Sjp161948 /* So at this point the following is true
4472139Sjp161948 * ssl->s3->rrec.type is the type of record
4482139Sjp161948 * ssl->s3->rrec.length == number of bytes in record
4492139Sjp161948 * ssl->s3->rrec.off == offset to first valid byte
4502139Sjp161948 * ssl->s3->rrec.data == where to take bytes from, increment
4512139Sjp161948 * after use :-).
4522139Sjp161948 */
4532139Sjp161948
4542139Sjp161948 /* we have pulled in a full packet so zero things */
4552139Sjp161948 s->packet_length=0;
4562139Sjp161948 dtls1_record_bitmap_update(s, &(s->d1->bitmap));/* Mark receipt of record. */
4572139Sjp161948 return(1);
4582139Sjp161948
4592139Sjp161948 decryption_failed_or_bad_record_mac:
4602139Sjp161948 /* Separate 'decryption_failed' alert was introduced with TLS 1.0,
4612139Sjp161948 * SSL 3.0 only has 'bad_record_mac'. But unless a decryption
4622139Sjp161948 * failure is directly visible from the ciphertext anyway,
4632139Sjp161948 * we should not reveal which kind of error occured -- this
4642139Sjp161948 * might become visible to an attacker (e.g. via logfile) */
4652139Sjp161948 al=SSL_AD_BAD_RECORD_MAC;
4662139Sjp161948 SSLerr(SSL_F_DTLS1_PROCESS_RECORD,SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
4672139Sjp161948 f_err:
4682139Sjp161948 ssl3_send_alert(s,SSL3_AL_FATAL,al);
4692139Sjp161948 err:
4702139Sjp161948 return(0);
4712139Sjp161948 }
4722139Sjp161948
4732139Sjp161948
4742139Sjp161948 /* Call this to get a new input record.
4752139Sjp161948 * It will return <= 0 if more data is needed, normally due to an error
4762139Sjp161948 * or non-blocking IO.
4772139Sjp161948 * When it finishes, one packet has been decoded and can be found in
4782139Sjp161948 * ssl->s3->rrec.type - is the type of record
4792139Sjp161948 * ssl->s3->rrec.data, - data
4802139Sjp161948 * ssl->s3->rrec.length, - number of bytes
4812139Sjp161948 */
4822139Sjp161948 /* used only by dtls1_read_bytes */
dtls1_get_record(SSL * s)4832139Sjp161948 int dtls1_get_record(SSL *s)
4842139Sjp161948 {
4852139Sjp161948 int ssl_major,ssl_minor,al;
4862139Sjp161948 int i,n;
4872139Sjp161948 SSL3_RECORD *rr;
4882139Sjp161948 SSL_SESSION *sess;
4892139Sjp161948 unsigned char *p;
490*5434Sjp161948 unsigned short version;
4912139Sjp161948 DTLS1_BITMAP *bitmap;
492*5434Sjp161948 unsigned int is_next_epoch;
4932139Sjp161948
4942139Sjp161948 rr= &(s->s3->rrec);
4952139Sjp161948 sess=s->session;
4962139Sjp161948
4972139Sjp161948 /* The epoch may have changed. If so, process all the
4982139Sjp161948 * pending records. This is a non-blocking operation. */
4992139Sjp161948 if ( ! dtls1_process_buffered_records(s))
5002139Sjp161948 return 0;
5012139Sjp161948
5022139Sjp161948 /* if we're renegotiating, then there may be buffered records */
5032139Sjp161948 if (dtls1_get_processed_record(s))
5042139Sjp161948 return 1;
5052139Sjp161948
5062139Sjp161948 /* get something from the wire */
5072139Sjp161948 again:
5082139Sjp161948 /* check if we have the header */
5092139Sjp161948 if ( (s->rstate != SSL_ST_READ_BODY) ||
5102139Sjp161948 (s->packet_length < DTLS1_RT_HEADER_LENGTH))
5112139Sjp161948 {
5122139Sjp161948 n=ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH, s->s3->rbuf.len, 0);
5132139Sjp161948 /* read timeout is handled by dtls1_read_bytes */
5142139Sjp161948 if (n <= 0) return(n); /* error or non-blocking */
5152139Sjp161948
5162139Sjp161948 OPENSSL_assert(s->packet_length == DTLS1_RT_HEADER_LENGTH);
5172139Sjp161948
5182139Sjp161948 s->rstate=SSL_ST_READ_BODY;
5192139Sjp161948
5202139Sjp161948 p=s->packet;
5212139Sjp161948
5222139Sjp161948 /* Pull apart the header into the DTLS1_RECORD */
5232139Sjp161948 rr->type= *(p++);
5242139Sjp161948 ssl_major= *(p++);
5252139Sjp161948 ssl_minor= *(p++);
5262139Sjp161948 version=(ssl_major<<8)|ssl_minor;
5272139Sjp161948
528*5434Sjp161948 /* sequence number is 64 bits, with top 2 bytes = epoch */
5292139Sjp161948 n2s(p,rr->epoch);
5302139Sjp161948
5312139Sjp161948 memcpy(&(s->s3->read_sequence[2]), p, 6);
5322139Sjp161948 p+=6;
5332139Sjp161948
5342139Sjp161948 n2s(p,rr->length);
5352139Sjp161948
5362139Sjp161948 /* Lets check version */
537*5434Sjp161948 if (!s->first_packet)
5382139Sjp161948 {
539*5434Sjp161948 if (version != s->version && version != DTLS1_BAD_VER)
5402139Sjp161948 {
5412139Sjp161948 SSLerr(SSL_F_DTLS1_GET_RECORD,SSL_R_WRONG_VERSION_NUMBER);
5422139Sjp161948 /* Send back error using their
5432139Sjp161948 * version number :-) */
5442139Sjp161948 s->version=version;
5452139Sjp161948 al=SSL_AD_PROTOCOL_VERSION;
5462139Sjp161948 goto f_err;
5472139Sjp161948 }
5482139Sjp161948 }
5492139Sjp161948
550*5434Sjp161948 if ((version & 0xff00) != (DTLS1_VERSION & 0xff00) &&
551*5434Sjp161948 (version & 0xff00) != (DTLS1_BAD_VER & 0xff00))
5522139Sjp161948 {
5532139Sjp161948 SSLerr(SSL_F_DTLS1_GET_RECORD,SSL_R_WRONG_VERSION_NUMBER);
5542139Sjp161948 goto err;
5552139Sjp161948 }
5562139Sjp161948
5572139Sjp161948 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH)
5582139Sjp161948 {
5592139Sjp161948 al=SSL_AD_RECORD_OVERFLOW;
5602139Sjp161948 SSLerr(SSL_F_DTLS1_GET_RECORD,SSL_R_PACKET_LENGTH_TOO_LONG);
5612139Sjp161948 goto f_err;
5622139Sjp161948 }
5632139Sjp161948
564*5434Sjp161948 s->client_version = version;
5652139Sjp161948 /* now s->rstate == SSL_ST_READ_BODY */
5662139Sjp161948 }
5672139Sjp161948
5682139Sjp161948 /* s->rstate == SSL_ST_READ_BODY, get and decode the data */
5692139Sjp161948
5702139Sjp161948 if (rr->length > s->packet_length-DTLS1_RT_HEADER_LENGTH)
5712139Sjp161948 {
5722139Sjp161948 /* now s->packet_length == DTLS1_RT_HEADER_LENGTH */
5732139Sjp161948 i=rr->length;
5742139Sjp161948 n=ssl3_read_n(s,i,i,1);
5752139Sjp161948 if (n <= 0) return(n); /* error or non-blocking io */
5762139Sjp161948
5772139Sjp161948 /* this packet contained a partial record, dump it */
5782139Sjp161948 if ( n != i)
5792139Sjp161948 {
5802139Sjp161948 s->packet_length = 0;
5812139Sjp161948 goto again;
5822139Sjp161948 }
5832139Sjp161948
5842139Sjp161948 /* now n == rr->length,
5852139Sjp161948 * and s->packet_length == DTLS1_RT_HEADER_LENGTH + rr->length */
5862139Sjp161948 }
5872139Sjp161948 s->rstate=SSL_ST_READ_HEADER; /* set state for later operations */
5882139Sjp161948
5892139Sjp161948 /* match epochs. NULL means the packet is dropped on the floor */
5902139Sjp161948 bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);
5912139Sjp161948 if ( bitmap == NULL)
5922139Sjp161948 {
5932139Sjp161948 s->packet_length = 0; /* dump this record */
5942139Sjp161948 goto again; /* get another record */
5952139Sjp161948 }
5962139Sjp161948
5972139Sjp161948 /* check whether this is a repeat, or aged record */
5982139Sjp161948 if ( ! dtls1_record_replay_check(s, bitmap, &(rr->seq_num)))
5992139Sjp161948 {
6002139Sjp161948 s->packet_length=0; /* dump this record */
6012139Sjp161948 goto again; /* get another record */
6022139Sjp161948 }
6032139Sjp161948
6042139Sjp161948 /* just read a 0 length packet */
6052139Sjp161948 if (rr->length == 0) goto again;
6062139Sjp161948
6072139Sjp161948 /* If this record is from the next epoch (either HM or ALERT), buffer it
6082139Sjp161948 * since it cannot be processed at this time.
6092139Sjp161948 * Records from the next epoch are marked as received even though they are
6102139Sjp161948 * not processed, so as to prevent any potential resource DoS attack */
6112139Sjp161948 if (is_next_epoch)
6122139Sjp161948 {
6132139Sjp161948 dtls1_record_bitmap_update(s, bitmap);
6142139Sjp161948 dtls1_buffer_record(s, &(s->d1->unprocessed_rcds), rr->seq_num);
6152139Sjp161948 s->packet_length = 0;
6162139Sjp161948 goto again;
6172139Sjp161948 }
6182139Sjp161948
6192139Sjp161948 if ( ! dtls1_process_record(s))
6202139Sjp161948 return(0);
6212139Sjp161948
6222139Sjp161948 dtls1_clear_timeouts(s); /* done waiting */
6232139Sjp161948 return(1);
6242139Sjp161948
6252139Sjp161948 f_err:
6262139Sjp161948 ssl3_send_alert(s,SSL3_AL_FATAL,al);
6272139Sjp161948 err:
6282139Sjp161948 return(0);
6292139Sjp161948 }
6302139Sjp161948
6312139Sjp161948 /* Return up to 'len' payload bytes received in 'type' records.
6322139Sjp161948 * 'type' is one of the following:
6332139Sjp161948 *
6342139Sjp161948 * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
6352139Sjp161948 * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
6362139Sjp161948 * - 0 (during a shutdown, no data has to be returned)
6372139Sjp161948 *
6382139Sjp161948 * If we don't have stored data to work from, read a SSL/TLS record first
6392139Sjp161948 * (possibly multiple records if we still don't have anything to return).
6402139Sjp161948 *
6412139Sjp161948 * This function must handle any surprises the peer may have for us, such as
6422139Sjp161948 * Alert records (e.g. close_notify), ChangeCipherSpec records (not really
6432139Sjp161948 * a surprise, but handled as if it were), or renegotiation requests.
6442139Sjp161948 * Also if record payloads contain fragments too small to process, we store
6452139Sjp161948 * them until there is enough for the respective protocol (the record protocol
6462139Sjp161948 * may use arbitrary fragmentation and even interleaving):
6472139Sjp161948 * Change cipher spec protocol
6482139Sjp161948 * just 1 byte needed, no need for keeping anything stored
6492139Sjp161948 * Alert protocol
6502139Sjp161948 * 2 bytes needed (AlertLevel, AlertDescription)
6512139Sjp161948 * Handshake protocol
6522139Sjp161948 * 4 bytes needed (HandshakeType, uint24 length) -- we just have
6532139Sjp161948 * to detect unexpected Client Hello and Hello Request messages
6542139Sjp161948 * here, anything else is handled by higher layers
6552139Sjp161948 * Application data protocol
6562139Sjp161948 * none of our business
6572139Sjp161948 */
dtls1_read_bytes(SSL * s,int type,unsigned char * buf,int len,int peek)6582139Sjp161948 int dtls1_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
6592139Sjp161948 {
6602139Sjp161948 int al,i,j,ret;
6612139Sjp161948 unsigned int n;
6622139Sjp161948 SSL3_RECORD *rr;
6632139Sjp161948 void (*cb)(const SSL *ssl,int type2,int val)=NULL;
6642139Sjp161948
6652139Sjp161948 if (s->s3->rbuf.buf == NULL) /* Not initialized yet */
6662139Sjp161948 if (!ssl3_setup_buffers(s))
6672139Sjp161948 return(-1);
6682139Sjp161948
6692139Sjp161948 /* XXX: check what the second '&& type' is about */
6702139Sjp161948 if ((type && (type != SSL3_RT_APPLICATION_DATA) &&
6712139Sjp161948 (type != SSL3_RT_HANDSHAKE) && type) ||
6722139Sjp161948 (peek && (type != SSL3_RT_APPLICATION_DATA)))
6732139Sjp161948 {
6742139Sjp161948 SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);
6752139Sjp161948 return -1;
6762139Sjp161948 }
6772139Sjp161948
6782139Sjp161948 /* check whether there's a handshake message (client hello?) waiting */
6792139Sjp161948 if ( (ret = have_handshake_fragment(s, type, buf, len, peek)))
6802139Sjp161948 return ret;
6812139Sjp161948
6822139Sjp161948 /* Now s->d1->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE. */
6832139Sjp161948
6842139Sjp161948 if (!s->in_handshake && SSL_in_init(s))
6852139Sjp161948 {
6862139Sjp161948 /* type == SSL3_RT_APPLICATION_DATA */
6872139Sjp161948 i=s->handshake_func(s);
6882139Sjp161948 if (i < 0) return(i);
6892139Sjp161948 if (i == 0)
6902139Sjp161948 {
6912139Sjp161948 SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);
6922139Sjp161948 return(-1);
6932139Sjp161948 }
6942139Sjp161948 }
6952139Sjp161948
6962139Sjp161948 start:
6972139Sjp161948 s->rwstate=SSL_NOTHING;
6982139Sjp161948
6992139Sjp161948 /* s->s3->rrec.type - is the type of record
7002139Sjp161948 * s->s3->rrec.data, - data
7012139Sjp161948 * s->s3->rrec.off, - offset into 'data' for next read
7022139Sjp161948 * s->s3->rrec.length, - number of bytes. */
7032139Sjp161948 rr = &(s->s3->rrec);
7042139Sjp161948
7052139Sjp161948 /* get new packet if necessary */
7062139Sjp161948 if ((rr->length == 0) || (s->rstate == SSL_ST_READ_BODY))
7072139Sjp161948 {
7082139Sjp161948 ret=dtls1_get_record(s);
7092139Sjp161948 if (ret <= 0)
7102139Sjp161948 {
7112139Sjp161948 ret = dtls1_read_failed(s, ret);
7122139Sjp161948 /* anything other than a timeout is an error */
7132139Sjp161948 if (ret <= 0)
7142139Sjp161948 return(ret);
7152139Sjp161948 else
7162139Sjp161948 goto start;
7172139Sjp161948 }
7182139Sjp161948 }
7192139Sjp161948
7202139Sjp161948 /* we now have a packet which can be read and processed */
7212139Sjp161948
7222139Sjp161948 if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec,
7232139Sjp161948 * reset by ssl3_get_finished */
7242139Sjp161948 && (rr->type != SSL3_RT_HANDSHAKE))
7252139Sjp161948 {
7262139Sjp161948 al=SSL_AD_UNEXPECTED_MESSAGE;
7272139Sjp161948 SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);
7282139Sjp161948 goto err;
7292139Sjp161948 }
7302139Sjp161948
7312139Sjp161948 /* If the other end has shut down, throw anything we read away
7322139Sjp161948 * (even in 'peek' mode) */
7332139Sjp161948 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
7342139Sjp161948 {
7352139Sjp161948 rr->length=0;
7362139Sjp161948 s->rwstate=SSL_NOTHING;
7372139Sjp161948 return(0);
7382139Sjp161948 }
7392139Sjp161948
7402139Sjp161948
7412139Sjp161948 if (type == rr->type) /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */
7422139Sjp161948 {
7432139Sjp161948 /* make sure that we are not getting application data when we
7442139Sjp161948 * are doing a handshake for the first time */
7452139Sjp161948 if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&
7462139Sjp161948 (s->enc_read_ctx == NULL))
7472139Sjp161948 {
7482139Sjp161948 al=SSL_AD_UNEXPECTED_MESSAGE;
7492139Sjp161948 SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_APP_DATA_IN_HANDSHAKE);
7502139Sjp161948 goto f_err;
7512139Sjp161948 }
7522139Sjp161948
7532139Sjp161948 if (len <= 0) return(len);
7542139Sjp161948
7552139Sjp161948 if ((unsigned int)len > rr->length)
7562139Sjp161948 n = rr->length;
7572139Sjp161948 else
7582139Sjp161948 n = (unsigned int)len;
7592139Sjp161948
7602139Sjp161948 memcpy(buf,&(rr->data[rr->off]),n);
7612139Sjp161948 if (!peek)
7622139Sjp161948 {
7632139Sjp161948 rr->length-=n;
7642139Sjp161948 rr->off+=n;
7652139Sjp161948 if (rr->length == 0)
7662139Sjp161948 {
7672139Sjp161948 s->rstate=SSL_ST_READ_HEADER;
7682139Sjp161948 rr->off=0;
7692139Sjp161948 }
7702139Sjp161948 }
7712139Sjp161948 return(n);
7722139Sjp161948 }
7732139Sjp161948
7742139Sjp161948
7752139Sjp161948 /* If we get here, then type != rr->type; if we have a handshake
7762139Sjp161948 * message, then it was unexpected (Hello Request or Client Hello). */
7772139Sjp161948
7782139Sjp161948 /* In case of record types for which we have 'fragment' storage,
7792139Sjp161948 * fill that so that we can process the data at a fixed place.
7802139Sjp161948 */
7812139Sjp161948 {
7822139Sjp161948 unsigned int k, dest_maxlen = 0;
7832139Sjp161948 unsigned char *dest = NULL;
7842139Sjp161948 unsigned int *dest_len = NULL;
7852139Sjp161948
7862139Sjp161948 if (rr->type == SSL3_RT_HANDSHAKE)
7872139Sjp161948 {
7882139Sjp161948 dest_maxlen = sizeof s->d1->handshake_fragment;
7892139Sjp161948 dest = s->d1->handshake_fragment;
7902139Sjp161948 dest_len = &s->d1->handshake_fragment_len;
7912139Sjp161948 }
7922139Sjp161948 else if (rr->type == SSL3_RT_ALERT)
7932139Sjp161948 {
7942139Sjp161948 dest_maxlen = sizeof(s->d1->alert_fragment);
7952139Sjp161948 dest = s->d1->alert_fragment;
7962139Sjp161948 dest_len = &s->d1->alert_fragment_len;
7972139Sjp161948 }
798*5434Sjp161948 /* else it's a CCS message, or it's wrong */
799*5434Sjp161948 else if (rr->type != SSL3_RT_CHANGE_CIPHER_SPEC)
800*5434Sjp161948 {
801*5434Sjp161948 /* Not certain if this is the right error handling */
802*5434Sjp161948 al=SSL_AD_UNEXPECTED_MESSAGE;
803*5434Sjp161948 SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_UNEXPECTED_RECORD);
804*5434Sjp161948 goto f_err;
805*5434Sjp161948 }
8062139Sjp161948
8072139Sjp161948
8082139Sjp161948 if (dest_maxlen > 0)
8092139Sjp161948 {
8102139Sjp161948 /* XDTLS: In a pathalogical case, the Client Hello
8112139Sjp161948 * may be fragmented--don't always expect dest_maxlen bytes */
8122139Sjp161948 if ( rr->length < dest_maxlen)
8132139Sjp161948 {
8142139Sjp161948 s->rstate=SSL_ST_READ_HEADER;
8152139Sjp161948 rr->length = 0;
8162139Sjp161948 goto start;
8172139Sjp161948 }
8182139Sjp161948
8192139Sjp161948 /* now move 'n' bytes: */
8202139Sjp161948 for ( k = 0; k < dest_maxlen; k++)
8212139Sjp161948 {
8222139Sjp161948 dest[k] = rr->data[rr->off++];
8232139Sjp161948 rr->length--;
8242139Sjp161948 }
8252139Sjp161948 *dest_len = dest_maxlen;
8262139Sjp161948 }
8272139Sjp161948 }
8282139Sjp161948
8292139Sjp161948 /* s->d1->handshake_fragment_len == 12 iff rr->type == SSL3_RT_HANDSHAKE;
8302139Sjp161948 * s->d1->alert_fragment_len == 7 iff rr->type == SSL3_RT_ALERT.
8312139Sjp161948 * (Possibly rr is 'empty' now, i.e. rr->length may be 0.) */
8322139Sjp161948
8332139Sjp161948 /* If we are a client, check for an incoming 'Hello Request': */
8342139Sjp161948 if ((!s->server) &&
8352139Sjp161948 (s->d1->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH) &&
8362139Sjp161948 (s->d1->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST) &&
8372139Sjp161948 (s->session != NULL) && (s->session->cipher != NULL))
8382139Sjp161948 {
8392139Sjp161948 s->d1->handshake_fragment_len = 0;
8402139Sjp161948
8412139Sjp161948 if ((s->d1->handshake_fragment[1] != 0) ||
8422139Sjp161948 (s->d1->handshake_fragment[2] != 0) ||
8432139Sjp161948 (s->d1->handshake_fragment[3] != 0))
8442139Sjp161948 {
8452139Sjp161948 al=SSL_AD_DECODE_ERROR;
8462139Sjp161948 SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_BAD_HELLO_REQUEST);
8472139Sjp161948 goto err;
8482139Sjp161948 }
8492139Sjp161948
8502139Sjp161948 /* no need to check sequence number on HELLO REQUEST messages */
8512139Sjp161948
8522139Sjp161948 if (s->msg_callback)
8532139Sjp161948 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
8542139Sjp161948 s->d1->handshake_fragment, 4, s, s->msg_callback_arg);
8552139Sjp161948
8562139Sjp161948 if (SSL_is_init_finished(s) &&
8572139Sjp161948 !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) &&
8582139Sjp161948 !s->s3->renegotiate)
8592139Sjp161948 {
8602139Sjp161948 ssl3_renegotiate(s);
8612139Sjp161948 if (ssl3_renegotiate_check(s))
8622139Sjp161948 {
8632139Sjp161948 i=s->handshake_func(s);
8642139Sjp161948 if (i < 0) return(i);
8652139Sjp161948 if (i == 0)
8662139Sjp161948 {
8672139Sjp161948 SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);
8682139Sjp161948 return(-1);
8692139Sjp161948 }
8702139Sjp161948
8712139Sjp161948 if (!(s->mode & SSL_MODE_AUTO_RETRY))
8722139Sjp161948 {
8732139Sjp161948 if (s->s3->rbuf.left == 0) /* no read-ahead left? */
8742139Sjp161948 {
8752139Sjp161948 BIO *bio;
8762139Sjp161948 /* In the case where we try to read application data,
8772139Sjp161948 * but we trigger an SSL handshake, we return -1 with
8782139Sjp161948 * the retry option set. Otherwise renegotiation may
8792139Sjp161948 * cause nasty problems in the blocking world */
8802139Sjp161948 s->rwstate=SSL_READING;
8812139Sjp161948 bio=SSL_get_rbio(s);
8822139Sjp161948 BIO_clear_retry_flags(bio);
8832139Sjp161948 BIO_set_retry_read(bio);
8842139Sjp161948 return(-1);
8852139Sjp161948 }
8862139Sjp161948 }
8872139Sjp161948 }
8882139Sjp161948 }
8892139Sjp161948 /* we either finished a handshake or ignored the request,
8902139Sjp161948 * now try again to obtain the (application) data we were asked for */
8912139Sjp161948 goto start;
8922139Sjp161948 }
8932139Sjp161948
8942139Sjp161948 if (s->d1->alert_fragment_len >= DTLS1_AL_HEADER_LENGTH)
8952139Sjp161948 {
8962139Sjp161948 int alert_level = s->d1->alert_fragment[0];
8972139Sjp161948 int alert_descr = s->d1->alert_fragment[1];
8982139Sjp161948
8992139Sjp161948 s->d1->alert_fragment_len = 0;
9002139Sjp161948
9012139Sjp161948 if (s->msg_callback)
9022139Sjp161948 s->msg_callback(0, s->version, SSL3_RT_ALERT,
9032139Sjp161948 s->d1->alert_fragment, 2, s, s->msg_callback_arg);
9042139Sjp161948
9052139Sjp161948 if (s->info_callback != NULL)
9062139Sjp161948 cb=s->info_callback;
9072139Sjp161948 else if (s->ctx->info_callback != NULL)
9082139Sjp161948 cb=s->ctx->info_callback;
9092139Sjp161948
9102139Sjp161948 if (cb != NULL)
9112139Sjp161948 {
9122139Sjp161948 j = (alert_level << 8) | alert_descr;
9132139Sjp161948 cb(s, SSL_CB_READ_ALERT, j);
9142139Sjp161948 }
9152139Sjp161948
9162139Sjp161948 if (alert_level == 1) /* warning */
9172139Sjp161948 {
9182139Sjp161948 s->s3->warn_alert = alert_descr;
9192139Sjp161948 if (alert_descr == SSL_AD_CLOSE_NOTIFY)
9202139Sjp161948 {
9212139Sjp161948 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
9222139Sjp161948 return(0);
9232139Sjp161948 }
9242139Sjp161948 #if 0
9252139Sjp161948 /* XXX: this is a possible improvement in the future */
9262139Sjp161948 /* now check if it's a missing record */
9272139Sjp161948 if (alert_descr == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE)
9282139Sjp161948 {
9292139Sjp161948 unsigned short seq;
9302139Sjp161948 unsigned int frag_off;
9312139Sjp161948 unsigned char *p = &(s->d1->alert_fragment[2]);
9322139Sjp161948
9332139Sjp161948 n2s(p, seq);
9342139Sjp161948 n2l3(p, frag_off);
9352139Sjp161948
9362139Sjp161948 dtls1_retransmit_message(s, seq, frag_off, &found);
9372139Sjp161948 if ( ! found && SSL_in_init(s))
9382139Sjp161948 {
9392139Sjp161948 /* fprintf( stderr,"in init = %d\n", SSL_in_init(s)); */
9402139Sjp161948 /* requested a message not yet sent,
9412139Sjp161948 send an alert ourselves */
9422139Sjp161948 ssl3_send_alert(s,SSL3_AL_WARNING,
9432139Sjp161948 DTLS1_AD_MISSING_HANDSHAKE_MESSAGE);
9442139Sjp161948 }
9452139Sjp161948 }
9462139Sjp161948 #endif
9472139Sjp161948 }
9482139Sjp161948 else if (alert_level == 2) /* fatal */
9492139Sjp161948 {
9502139Sjp161948 char tmp[16];
9512139Sjp161948
9522139Sjp161948 s->rwstate=SSL_NOTHING;
9532139Sjp161948 s->s3->fatal_alert = alert_descr;
9542139Sjp161948 SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_AD_REASON_OFFSET + alert_descr);
9552139Sjp161948 BIO_snprintf(tmp,sizeof tmp,"%d",alert_descr);
9562139Sjp161948 ERR_add_error_data(2,"SSL alert number ",tmp);
9572139Sjp161948 s->shutdown|=SSL_RECEIVED_SHUTDOWN;
9582139Sjp161948 SSL_CTX_remove_session(s->ctx,s->session);
9592139Sjp161948 return(0);
9602139Sjp161948 }
9612139Sjp161948 else
9622139Sjp161948 {
9632139Sjp161948 al=SSL_AD_ILLEGAL_PARAMETER;
9642139Sjp161948 SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_UNKNOWN_ALERT_TYPE);
9652139Sjp161948 goto f_err;
9662139Sjp161948 }
9672139Sjp161948
9682139Sjp161948 goto start;
9692139Sjp161948 }
9702139Sjp161948
9712139Sjp161948 if (s->shutdown & SSL_SENT_SHUTDOWN) /* but we have not received a shutdown */
9722139Sjp161948 {
9732139Sjp161948 s->rwstate=SSL_NOTHING;
9742139Sjp161948 rr->length=0;
9752139Sjp161948 return(0);
9762139Sjp161948 }
9772139Sjp161948
9782139Sjp161948 if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC)
979*5434Sjp161948 {
980*5434Sjp161948 struct ccs_header_st ccs_hdr;
9812139Sjp161948
9822139Sjp161948 dtls1_get_ccs_header(rr->data, &ccs_hdr);
9832139Sjp161948
984*5434Sjp161948 /* 'Change Cipher Spec' is just a single byte, so we know
985*5434Sjp161948 * exactly what the record payload has to look like */
986*5434Sjp161948 /* XDTLS: check that epoch is consistent */
987*5434Sjp161948 if ( (s->client_version == DTLS1_BAD_VER && rr->length != 3) ||
988*5434Sjp161948 (s->client_version != DTLS1_BAD_VER && rr->length != DTLS1_CCS_HEADER_LENGTH) ||
989*5434Sjp161948 (rr->off != 0) || (rr->data[0] != SSL3_MT_CCS))
9902139Sjp161948 {
991*5434Sjp161948 i=SSL_AD_ILLEGAL_PARAMETER;
992*5434Sjp161948 SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_BAD_CHANGE_CIPHER_SPEC);
993*5434Sjp161948 goto err;
994*5434Sjp161948 }
995*5434Sjp161948
996*5434Sjp161948 rr->length=0;
997*5434Sjp161948
998*5434Sjp161948 if (s->msg_callback)
999*5434Sjp161948 s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC,
1000*5434Sjp161948 rr->data, 1, s, s->msg_callback_arg);
1001*5434Sjp161948
1002*5434Sjp161948 s->s3->change_cipher_spec=1;
1003*5434Sjp161948 if (!ssl3_do_change_cipher_spec(s))
1004*5434Sjp161948 goto err;
1005*5434Sjp161948
1006*5434Sjp161948 /* do this whenever CCS is processed */
1007*5434Sjp161948 dtls1_reset_seq_numbers(s, SSL3_CC_READ);
1008*5434Sjp161948
1009*5434Sjp161948 if (s->client_version == DTLS1_BAD_VER)
10102139Sjp161948 s->d1->handshake_read_seq++;
1011*5434Sjp161948
1012*5434Sjp161948 goto start;
10132139Sjp161948 }
10142139Sjp161948
10152139Sjp161948 /* Unexpected handshake message (Client Hello, or protocol violation) */
10162139Sjp161948 if ((s->d1->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH) &&
10172139Sjp161948 !s->in_handshake)
10182139Sjp161948 {
10192139Sjp161948 struct hm_header_st msg_hdr;
10202139Sjp161948
10212139Sjp161948 /* this may just be a stale retransmit */
10222139Sjp161948 dtls1_get_message_header(rr->data, &msg_hdr);
10232139Sjp161948 if( rr->epoch != s->d1->r_epoch)
10242139Sjp161948 {
10252139Sjp161948 rr->length = 0;
10262139Sjp161948 goto start;
10272139Sjp161948 }
10282139Sjp161948
10292139Sjp161948 if (((s->state&SSL_ST_MASK) == SSL_ST_OK) &&
10302139Sjp161948 !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS))
10312139Sjp161948 {
10322139Sjp161948 #if 0 /* worked only because C operator preferences are not as expected (and
10332139Sjp161948 * because this is not really needed for clients except for detecting
10342139Sjp161948 * protocol violations): */
10352139Sjp161948 s->state=SSL_ST_BEFORE|(s->server)
10362139Sjp161948 ?SSL_ST_ACCEPT
10372139Sjp161948 :SSL_ST_CONNECT;
10382139Sjp161948 #else
10392139Sjp161948 s->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT;
10402139Sjp161948 #endif
10412139Sjp161948 s->new_session=1;
10422139Sjp161948 }
10432139Sjp161948 i=s->handshake_func(s);
10442139Sjp161948 if (i < 0) return(i);
10452139Sjp161948 if (i == 0)
10462139Sjp161948 {
10472139Sjp161948 SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);
10482139Sjp161948 return(-1);
10492139Sjp161948 }
10502139Sjp161948
10512139Sjp161948 if (!(s->mode & SSL_MODE_AUTO_RETRY))
10522139Sjp161948 {
10532139Sjp161948 if (s->s3->rbuf.left == 0) /* no read-ahead left? */
10542139Sjp161948 {
10552139Sjp161948 BIO *bio;
10562139Sjp161948 /* In the case where we try to read application data,
10572139Sjp161948 * but we trigger an SSL handshake, we return -1 with
10582139Sjp161948 * the retry option set. Otherwise renegotiation may
10592139Sjp161948 * cause nasty problems in the blocking world */
10602139Sjp161948 s->rwstate=SSL_READING;
10612139Sjp161948 bio=SSL_get_rbio(s);
10622139Sjp161948 BIO_clear_retry_flags(bio);
10632139Sjp161948 BIO_set_retry_read(bio);
10642139Sjp161948 return(-1);
10652139Sjp161948 }
10662139Sjp161948 }
10672139Sjp161948 goto start;
10682139Sjp161948 }
10692139Sjp161948
10702139Sjp161948 switch (rr->type)
10712139Sjp161948 {
10722139Sjp161948 default:
10732139Sjp161948 #ifndef OPENSSL_NO_TLS
10742139Sjp161948 /* TLS just ignores unknown message types */
10752139Sjp161948 if (s->version == TLS1_VERSION)
10762139Sjp161948 {
10772139Sjp161948 rr->length = 0;
10782139Sjp161948 goto start;
10792139Sjp161948 }
10802139Sjp161948 #endif
10812139Sjp161948 al=SSL_AD_UNEXPECTED_MESSAGE;
10822139Sjp161948 SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_UNEXPECTED_RECORD);
10832139Sjp161948 goto f_err;
10842139Sjp161948 case SSL3_RT_CHANGE_CIPHER_SPEC:
10852139Sjp161948 case SSL3_RT_ALERT:
10862139Sjp161948 case SSL3_RT_HANDSHAKE:
10872139Sjp161948 /* we already handled all of these, with the possible exception
10882139Sjp161948 * of SSL3_RT_HANDSHAKE when s->in_handshake is set, but that
10892139Sjp161948 * should not happen when type != rr->type */
10902139Sjp161948 al=SSL_AD_UNEXPECTED_MESSAGE;
10912139Sjp161948 SSLerr(SSL_F_DTLS1_READ_BYTES,ERR_R_INTERNAL_ERROR);
10922139Sjp161948 goto f_err;
10932139Sjp161948 case SSL3_RT_APPLICATION_DATA:
10942139Sjp161948 /* At this point, we were expecting handshake data,
10952139Sjp161948 * but have application data. If the library was
10962139Sjp161948 * running inside ssl3_read() (i.e. in_read_app_data
10972139Sjp161948 * is set) and it makes sense to read application data
10982139Sjp161948 * at this point (session renegotiation not yet started),
10992139Sjp161948 * we will indulge it.
11002139Sjp161948 */
11012139Sjp161948 if (s->s3->in_read_app_data &&
11022139Sjp161948 (s->s3->total_renegotiations != 0) &&
11032139Sjp161948 ((
11042139Sjp161948 (s->state & SSL_ST_CONNECT) &&
11052139Sjp161948 (s->state >= SSL3_ST_CW_CLNT_HELLO_A) &&
11062139Sjp161948 (s->state <= SSL3_ST_CR_SRVR_HELLO_A)
11072139Sjp161948 ) || (
11082139Sjp161948 (s->state & SSL_ST_ACCEPT) &&
11092139Sjp161948 (s->state <= SSL3_ST_SW_HELLO_REQ_A) &&
11102139Sjp161948 (s->state >= SSL3_ST_SR_CLNT_HELLO_A)
11112139Sjp161948 )
11122139Sjp161948 ))
11132139Sjp161948 {
11142139Sjp161948 s->s3->in_read_app_data=2;
11152139Sjp161948 return(-1);
11162139Sjp161948 }
11172139Sjp161948 else
11182139Sjp161948 {
11192139Sjp161948 al=SSL_AD_UNEXPECTED_MESSAGE;
11202139Sjp161948 SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_UNEXPECTED_RECORD);
11212139Sjp161948 goto f_err;
11222139Sjp161948 }
11232139Sjp161948 }
11242139Sjp161948 /* not reached */
11252139Sjp161948
11262139Sjp161948 f_err:
11272139Sjp161948 ssl3_send_alert(s,SSL3_AL_FATAL,al);
11282139Sjp161948 err:
11292139Sjp161948 return(-1);
11302139Sjp161948 }
11312139Sjp161948
11322139Sjp161948 int
dtls1_write_app_data_bytes(SSL * s,int type,const void * buf_,int len)11332139Sjp161948 dtls1_write_app_data_bytes(SSL *s, int type, const void *buf_, int len)
11342139Sjp161948 {
11352139Sjp161948 unsigned int n,tot;
11362139Sjp161948 int i;
11372139Sjp161948
11382139Sjp161948 if (SSL_in_init(s) && !s->in_handshake)
11392139Sjp161948 {
11402139Sjp161948 i=s->handshake_func(s);
11412139Sjp161948 if (i < 0) return(i);
11422139Sjp161948 if (i == 0)
11432139Sjp161948 {
11442139Sjp161948 SSLerr(SSL_F_DTLS1_WRITE_APP_DATA_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);
11452139Sjp161948 return -1;
11462139Sjp161948 }
11472139Sjp161948 }
11482139Sjp161948
11492139Sjp161948 tot = s->s3->wnum;
11502139Sjp161948 n = len - tot;
11512139Sjp161948
11522139Sjp161948 while( n)
11532139Sjp161948 {
11542139Sjp161948 /* dtls1_write_bytes sends one record at a time, sized according to
11552139Sjp161948 * the currently known MTU */
11562139Sjp161948 i = dtls1_write_bytes(s, type, buf_, len);
11572139Sjp161948 if (i <= 0) return i;
11582139Sjp161948
11592139Sjp161948 if ((i == (int)n) ||
11602139Sjp161948 (type == SSL3_RT_APPLICATION_DATA &&
11612139Sjp161948 (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE)))
11622139Sjp161948 {
11632139Sjp161948 /* next chunk of data should get another prepended empty fragment
11642139Sjp161948 * in ciphersuites with known-IV weakness: */
11652139Sjp161948 s->s3->empty_fragment_done = 0;
11662139Sjp161948 return tot+i;
11672139Sjp161948 }
11682139Sjp161948
11692139Sjp161948 tot += i;
11702139Sjp161948 n-=i;
11712139Sjp161948 }
11722139Sjp161948
11732139Sjp161948 return tot;
11742139Sjp161948 }
11752139Sjp161948
11762139Sjp161948
11772139Sjp161948 /* this only happens when a client hello is received and a handshake
11782139Sjp161948 * is started. */
11792139Sjp161948 static int
have_handshake_fragment(SSL * s,int type,unsigned char * buf,int len,int peek)11802139Sjp161948 have_handshake_fragment(SSL *s, int type, unsigned char *buf,
11812139Sjp161948 int len, int peek)
11822139Sjp161948 {
11832139Sjp161948
11842139Sjp161948 if ((type == SSL3_RT_HANDSHAKE) && (s->d1->handshake_fragment_len > 0))
11852139Sjp161948 /* (partially) satisfy request from storage */
11862139Sjp161948 {
11872139Sjp161948 unsigned char *src = s->d1->handshake_fragment;
11882139Sjp161948 unsigned char *dst = buf;
11892139Sjp161948 unsigned int k,n;
11902139Sjp161948
11912139Sjp161948 /* peek == 0 */
11922139Sjp161948 n = 0;
11932139Sjp161948 while ((len > 0) && (s->d1->handshake_fragment_len > 0))
11942139Sjp161948 {
11952139Sjp161948 *dst++ = *src++;
11962139Sjp161948 len--; s->d1->handshake_fragment_len--;
11972139Sjp161948 n++;
11982139Sjp161948 }
11992139Sjp161948 /* move any remaining fragment bytes: */
12002139Sjp161948 for (k = 0; k < s->d1->handshake_fragment_len; k++)
12012139Sjp161948 s->d1->handshake_fragment[k] = *src++;
12022139Sjp161948 return n;
12032139Sjp161948 }
12042139Sjp161948
12052139Sjp161948 return 0;
12062139Sjp161948 }
12072139Sjp161948
12082139Sjp161948
12092139Sjp161948
12102139Sjp161948
12112139Sjp161948 /* Call this to write data in records of type 'type'
12122139Sjp161948 * It will return <= 0 if not all data has been sent or non-blocking IO.
12132139Sjp161948 */
dtls1_write_bytes(SSL * s,int type,const void * buf_,int len)12142139Sjp161948 int dtls1_write_bytes(SSL *s, int type, const void *buf_, int len)
12152139Sjp161948 {
12162139Sjp161948 const unsigned char *buf=buf_;
12172139Sjp161948 unsigned int tot,n,nw;
12182139Sjp161948 int i;
12192139Sjp161948 unsigned int mtu;
12202139Sjp161948
12212139Sjp161948 s->rwstate=SSL_NOTHING;
12222139Sjp161948 tot=s->s3->wnum;
12232139Sjp161948
12242139Sjp161948 n=(len-tot);
12252139Sjp161948
12262139Sjp161948 /* handshake layer figures out MTU for itself, but data records
12272139Sjp161948 * are also sent through this interface, so need to figure out MTU */
12282139Sjp161948 #if 0
12292139Sjp161948 mtu = BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_GET_MTU, 0, NULL);
12302139Sjp161948 mtu += DTLS1_HM_HEADER_LENGTH; /* HM already inserted */
12312139Sjp161948 #endif
12322139Sjp161948 mtu = s->d1->mtu;
12332139Sjp161948
12342139Sjp161948 if (mtu > SSL3_RT_MAX_PLAIN_LENGTH)
12352139Sjp161948 mtu = SSL3_RT_MAX_PLAIN_LENGTH;
12362139Sjp161948
12372139Sjp161948 if (n > mtu)
12382139Sjp161948 nw=mtu;
12392139Sjp161948 else
12402139Sjp161948 nw=n;
12412139Sjp161948
12422139Sjp161948 i=do_dtls1_write(s, type, &(buf[tot]), nw, 0);
12432139Sjp161948 if (i <= 0)
12442139Sjp161948 {
12452139Sjp161948 s->s3->wnum=tot;
12462139Sjp161948 return i;
12472139Sjp161948 }
12482139Sjp161948
12492139Sjp161948 if ( (int)s->s3->wnum + i == len)
12502139Sjp161948 s->s3->wnum = 0;
12512139Sjp161948 else
12522139Sjp161948 s->s3->wnum += i;
12532139Sjp161948
12542139Sjp161948 return tot + i;
12552139Sjp161948 }
12562139Sjp161948
do_dtls1_write(SSL * s,int type,const unsigned char * buf,unsigned int len,int create_empty_fragment)12572139Sjp161948 int do_dtls1_write(SSL *s, int type, const unsigned char *buf, unsigned int len, int create_empty_fragment)
12582139Sjp161948 {
12592139Sjp161948 unsigned char *p,*pseq;
12602139Sjp161948 int i,mac_size,clear=0;
12612139Sjp161948 int prefix_len = 0;
12622139Sjp161948 SSL3_RECORD *wr;
12632139Sjp161948 SSL3_BUFFER *wb;
12642139Sjp161948 SSL_SESSION *sess;
12652139Sjp161948 int bs;
12662139Sjp161948
12672139Sjp161948 /* first check if there is a SSL3_BUFFER still being written
12682139Sjp161948 * out. This will happen with non blocking IO */
12692139Sjp161948 if (s->s3->wbuf.left != 0)
12702139Sjp161948 {
12712139Sjp161948 OPENSSL_assert(0); /* XDTLS: want to see if we ever get here */
12722139Sjp161948 return(ssl3_write_pending(s,type,buf,len));
12732139Sjp161948 }
12742139Sjp161948
12752139Sjp161948 /* If we have an alert to send, lets send it */
12762139Sjp161948 if (s->s3->alert_dispatch)
12772139Sjp161948 {
12782139Sjp161948 i=s->method->ssl_dispatch_alert(s);
12792139Sjp161948 if (i <= 0)
12802139Sjp161948 return(i);
12812139Sjp161948 /* if it went, fall through and send more stuff */
12822139Sjp161948 }
12832139Sjp161948
12842139Sjp161948 if (len == 0 && !create_empty_fragment)
12852139Sjp161948 return 0;
12862139Sjp161948
12872139Sjp161948 wr= &(s->s3->wrec);
12882139Sjp161948 wb= &(s->s3->wbuf);
12892139Sjp161948 sess=s->session;
12902139Sjp161948
12912139Sjp161948 if ( (sess == NULL) ||
12922139Sjp161948 (s->enc_write_ctx == NULL) ||
12932139Sjp161948 (s->write_hash == NULL))
12942139Sjp161948 clear=1;
12952139Sjp161948
12962139Sjp161948 if (clear)
12972139Sjp161948 mac_size=0;
12982139Sjp161948 else
12992139Sjp161948 mac_size=EVP_MD_size(s->write_hash);
13002139Sjp161948
13012139Sjp161948 /* DTLS implements explicit IV, so no need for empty fragments */
13022139Sjp161948 #if 0
13032139Sjp161948 /* 'create_empty_fragment' is true only when this function calls itself */
13042139Sjp161948 if (!clear && !create_empty_fragment && !s->s3->empty_fragment_done
13052139Sjp161948 && SSL_version(s) != DTLS1_VERSION)
13062139Sjp161948 {
13072139Sjp161948 /* countermeasure against known-IV weakness in CBC ciphersuites
13082139Sjp161948 * (see http://www.openssl.org/~bodo/tls-cbc.txt)
13092139Sjp161948 */
13102139Sjp161948
13112139Sjp161948 if (s->s3->need_empty_fragments && type == SSL3_RT_APPLICATION_DATA)
13122139Sjp161948 {
13132139Sjp161948 /* recursive function call with 'create_empty_fragment' set;
13142139Sjp161948 * this prepares and buffers the data for an empty fragment
13152139Sjp161948 * (these 'prefix_len' bytes are sent out later
13162139Sjp161948 * together with the actual payload) */
13172139Sjp161948 prefix_len = s->method->do_ssl_write(s, type, buf, 0, 1);
13182139Sjp161948 if (prefix_len <= 0)
13192139Sjp161948 goto err;
13202139Sjp161948
13212139Sjp161948 if (s->s3->wbuf.len < (size_t)prefix_len + SSL3_RT_MAX_PACKET_SIZE)
13222139Sjp161948 {
13232139Sjp161948 /* insufficient space */
13242139Sjp161948 SSLerr(SSL_F_DO_DTLS1_WRITE, ERR_R_INTERNAL_ERROR);
13252139Sjp161948 goto err;
13262139Sjp161948 }
13272139Sjp161948 }
13282139Sjp161948
13292139Sjp161948 s->s3->empty_fragment_done = 1;
13302139Sjp161948 }
13312139Sjp161948 #endif
13322139Sjp161948
13332139Sjp161948 p = wb->buf + prefix_len;
13342139Sjp161948
13352139Sjp161948 /* write the header */
13362139Sjp161948
13372139Sjp161948 *(p++)=type&0xff;
13382139Sjp161948 wr->type=type;
13392139Sjp161948
1340*5434Sjp161948 if (s->client_version == DTLS1_BAD_VER)
1341*5434Sjp161948 *(p++) = DTLS1_BAD_VER>>8,
1342*5434Sjp161948 *(p++) = DTLS1_BAD_VER&0xff;
1343*5434Sjp161948 else
1344*5434Sjp161948 *(p++)=(s->version>>8),
1345*5434Sjp161948 *(p++)=s->version&0xff;
13462139Sjp161948
13472139Sjp161948 /* field where we are to write out packet epoch, seq num and len */
13482139Sjp161948 pseq=p;
13492139Sjp161948 p+=10;
13502139Sjp161948
13512139Sjp161948 /* lets setup the record stuff. */
13522139Sjp161948
13532139Sjp161948 /* Make space for the explicit IV in case of CBC.
13542139Sjp161948 * (this is a bit of a boundary violation, but what the heck).
13552139Sjp161948 */
13562139Sjp161948 if ( s->enc_write_ctx &&
13572139Sjp161948 (EVP_CIPHER_mode( s->enc_write_ctx->cipher ) & EVP_CIPH_CBC_MODE))
13582139Sjp161948 bs = EVP_CIPHER_block_size(s->enc_write_ctx->cipher);
13592139Sjp161948 else
13602139Sjp161948 bs = 0;
13612139Sjp161948
13622139Sjp161948 wr->data=p + bs; /* make room for IV in case of CBC */
13632139Sjp161948 wr->length=(int)len;
13642139Sjp161948 wr->input=(unsigned char *)buf;
13652139Sjp161948
13662139Sjp161948 /* we now 'read' from wr->input, wr->length bytes into
13672139Sjp161948 * wr->data */
13682139Sjp161948
13692139Sjp161948 /* first we compress */
13702139Sjp161948 if (s->compress != NULL)
13712139Sjp161948 {
13722139Sjp161948 if (!ssl3_do_compress(s))
13732139Sjp161948 {
13742139Sjp161948 SSLerr(SSL_F_DO_DTLS1_WRITE,SSL_R_COMPRESSION_FAILURE);
13752139Sjp161948 goto err;
13762139Sjp161948 }
13772139Sjp161948 }
13782139Sjp161948 else
13792139Sjp161948 {
13802139Sjp161948 memcpy(wr->data,wr->input,wr->length);
13812139Sjp161948 wr->input=wr->data;
13822139Sjp161948 }
13832139Sjp161948
13842139Sjp161948 /* we should still have the output to wr->data and the input
13852139Sjp161948 * from wr->input. Length should be wr->length.
13862139Sjp161948 * wr->data still points in the wb->buf */
13872139Sjp161948
13882139Sjp161948 if (mac_size != 0)
13892139Sjp161948 {
13902139Sjp161948 s->method->ssl3_enc->mac(s,&(p[wr->length + bs]),1);
13912139Sjp161948 wr->length+=mac_size;
13922139Sjp161948 }
13932139Sjp161948
13942139Sjp161948 /* this is true regardless of mac size */
13952139Sjp161948 wr->input=p;
13962139Sjp161948 wr->data=p;
13972139Sjp161948
13982139Sjp161948
13992139Sjp161948 /* ssl3_enc can only have an error on read */
1400*5434Sjp161948 if (bs) /* bs != 0 in case of CBC */
1401*5434Sjp161948 {
1402*5434Sjp161948 RAND_pseudo_bytes(p,bs);
1403*5434Sjp161948 /* master IV and last CBC residue stand for
1404*5434Sjp161948 * the rest of randomness */
1405*5434Sjp161948 wr->length += bs;
1406*5434Sjp161948 }
1407*5434Sjp161948
14082139Sjp161948 s->method->ssl3_enc->enc(s,1);
14092139Sjp161948
14102139Sjp161948 /* record length after mac and block padding */
14112139Sjp161948 /* if (type == SSL3_RT_APPLICATION_DATA ||
14122139Sjp161948 (type == SSL3_RT_ALERT && ! SSL_in_init(s))) */
14132139Sjp161948
14142139Sjp161948 /* there's only one epoch between handshake and app data */
14152139Sjp161948
14162139Sjp161948 s2n(s->d1->w_epoch, pseq);
14172139Sjp161948
14182139Sjp161948 /* XDTLS: ?? */
14192139Sjp161948 /* else
14202139Sjp161948 s2n(s->d1->handshake_epoch, pseq); */
14212139Sjp161948
14222139Sjp161948 memcpy(pseq, &(s->s3->write_sequence[2]), 6);
14232139Sjp161948 pseq+=6;
14242139Sjp161948 s2n(wr->length,pseq);
14252139Sjp161948
14262139Sjp161948 /* we should now have
14272139Sjp161948 * wr->data pointing to the encrypted data, which is
14282139Sjp161948 * wr->length long */
14292139Sjp161948 wr->type=type; /* not needed but helps for debugging */
14302139Sjp161948 wr->length+=DTLS1_RT_HEADER_LENGTH;
14312139Sjp161948
14322139Sjp161948 #if 0 /* this is now done at the message layer */
14332139Sjp161948 /* buffer the record, making it easy to handle retransmits */
14342139Sjp161948 if ( type == SSL3_RT_HANDSHAKE || type == SSL3_RT_CHANGE_CIPHER_SPEC)
14352139Sjp161948 dtls1_buffer_record(s, wr->data, wr->length,
14362139Sjp161948 *((PQ_64BIT *)&(s->s3->write_sequence[0])));
14372139Sjp161948 #endif
14382139Sjp161948
14392139Sjp161948 ssl3_record_sequence_update(&(s->s3->write_sequence[0]));
14402139Sjp161948
14412139Sjp161948 if (create_empty_fragment)
14422139Sjp161948 {
14432139Sjp161948 /* we are in a recursive call;
14442139Sjp161948 * just return the length, don't write out anything here
14452139Sjp161948 */
14462139Sjp161948 return wr->length;
14472139Sjp161948 }
14482139Sjp161948
14492139Sjp161948 /* now let's set up wb */
14502139Sjp161948 wb->left = prefix_len + wr->length;
14512139Sjp161948 wb->offset = 0;
14522139Sjp161948
14532139Sjp161948 /* memorize arguments so that ssl3_write_pending can detect bad write retries later */
14542139Sjp161948 s->s3->wpend_tot=len;
14552139Sjp161948 s->s3->wpend_buf=buf;
14562139Sjp161948 s->s3->wpend_type=type;
14572139Sjp161948 s->s3->wpend_ret=len;
14582139Sjp161948
14592139Sjp161948 /* we now just need to write the buffer */
14602139Sjp161948 return ssl3_write_pending(s,type,buf,len);
14612139Sjp161948 err:
14622139Sjp161948 return -1;
14632139Sjp161948 }
14642139Sjp161948
14652139Sjp161948
14662139Sjp161948
dtls1_record_replay_check(SSL * s,DTLS1_BITMAP * bitmap,PQ_64BIT * seq_num)14672139Sjp161948 static int dtls1_record_replay_check(SSL *s, DTLS1_BITMAP *bitmap,
14682139Sjp161948 PQ_64BIT *seq_num)
14692139Sjp161948 {
14702139Sjp161948 #if PQ_64BIT_IS_INTEGER
14712139Sjp161948 PQ_64BIT mask = 0x0000000000000001L;
14722139Sjp161948 #endif
14732139Sjp161948 PQ_64BIT rcd_num, tmp;
14742139Sjp161948
14752139Sjp161948 pq_64bit_init(&rcd_num);
14762139Sjp161948 pq_64bit_init(&tmp);
14772139Sjp161948
14782139Sjp161948 /* this is the sequence number for the record just read */
14792139Sjp161948 pq_64bit_bin2num(&rcd_num, s->s3->read_sequence, 8);
14802139Sjp161948
14812139Sjp161948
14822139Sjp161948 if (pq_64bit_gt(&rcd_num, &(bitmap->max_seq_num)) ||
14832139Sjp161948 pq_64bit_eq(&rcd_num, &(bitmap->max_seq_num)))
14842139Sjp161948 {
14852139Sjp161948 pq_64bit_assign(seq_num, &rcd_num);
14862139Sjp161948 pq_64bit_free(&rcd_num);
14872139Sjp161948 pq_64bit_free(&tmp);
14882139Sjp161948 return 1; /* this record is new */
14892139Sjp161948 }
14902139Sjp161948
14912139Sjp161948 pq_64bit_sub(&tmp, &(bitmap->max_seq_num), &rcd_num);
14922139Sjp161948
14932139Sjp161948 if ( pq_64bit_get_word(&tmp) > bitmap->length)
14942139Sjp161948 {
14952139Sjp161948 pq_64bit_free(&rcd_num);
14962139Sjp161948 pq_64bit_free(&tmp);
14972139Sjp161948 return 0; /* stale, outside the window */
14982139Sjp161948 }
14992139Sjp161948
15002139Sjp161948 #if PQ_64BIT_IS_BIGNUM
15012139Sjp161948 {
15022139Sjp161948 int offset;
15032139Sjp161948 pq_64bit_sub(&tmp, &(bitmap->max_seq_num), &rcd_num);
15042139Sjp161948 pq_64bit_sub_word(&tmp, 1);
15052139Sjp161948 offset = pq_64bit_get_word(&tmp);
15062139Sjp161948 if ( pq_64bit_is_bit_set(&(bitmap->map), offset))
15072139Sjp161948 {
15082139Sjp161948 pq_64bit_free(&rcd_num);
15092139Sjp161948 pq_64bit_free(&tmp);
15102139Sjp161948 return 0;
15112139Sjp161948 }
15122139Sjp161948 }
15132139Sjp161948 #else
15142139Sjp161948 mask <<= (bitmap->max_seq_num - rcd_num - 1);
15152139Sjp161948 if (bitmap->map & mask)
15162139Sjp161948 return 0; /* record previously received */
15172139Sjp161948 #endif
15182139Sjp161948
15192139Sjp161948 pq_64bit_assign(seq_num, &rcd_num);
15202139Sjp161948 pq_64bit_free(&rcd_num);
15212139Sjp161948 pq_64bit_free(&tmp);
15222139Sjp161948 return 1;
15232139Sjp161948 }
15242139Sjp161948
15252139Sjp161948
dtls1_record_bitmap_update(SSL * s,DTLS1_BITMAP * bitmap)15262139Sjp161948 static void dtls1_record_bitmap_update(SSL *s, DTLS1_BITMAP *bitmap)
15272139Sjp161948 {
15282139Sjp161948 unsigned int shift;
15292139Sjp161948 PQ_64BIT rcd_num;
15302139Sjp161948 PQ_64BIT tmp;
15312139Sjp161948 PQ_64BIT_CTX *ctx;
15322139Sjp161948
15332139Sjp161948 pq_64bit_init(&rcd_num);
15342139Sjp161948 pq_64bit_init(&tmp);
15352139Sjp161948
15362139Sjp161948 pq_64bit_bin2num(&rcd_num, s->s3->read_sequence, 8);
15372139Sjp161948
15382139Sjp161948 /* unfortunate code complexity due to 64-bit manipulation support
15392139Sjp161948 * on 32-bit machines */
15402139Sjp161948 if ( pq_64bit_gt(&rcd_num, &(bitmap->max_seq_num)) ||
15412139Sjp161948 pq_64bit_eq(&rcd_num, &(bitmap->max_seq_num)))
15422139Sjp161948 {
15432139Sjp161948 pq_64bit_sub(&tmp, &rcd_num, &(bitmap->max_seq_num));
15442139Sjp161948 pq_64bit_add_word(&tmp, 1);
15452139Sjp161948
15462139Sjp161948 shift = (unsigned int)pq_64bit_get_word(&tmp);
15472139Sjp161948
15482139Sjp161948 pq_64bit_lshift(&(tmp), &(bitmap->map), shift);
15492139Sjp161948 pq_64bit_assign(&(bitmap->map), &tmp);
15502139Sjp161948
15512139Sjp161948 pq_64bit_set_bit(&(bitmap->map), 0);
15522139Sjp161948 pq_64bit_add_word(&rcd_num, 1);
15532139Sjp161948 pq_64bit_assign(&(bitmap->max_seq_num), &rcd_num);
15542139Sjp161948
15552139Sjp161948 pq_64bit_assign_word(&tmp, 1);
15562139Sjp161948 pq_64bit_lshift(&tmp, &tmp, bitmap->length);
15572139Sjp161948 ctx = pq_64bit_ctx_new(&ctx);
15582139Sjp161948 pq_64bit_mod(&(bitmap->map), &(bitmap->map), &tmp, ctx);
15592139Sjp161948 pq_64bit_ctx_free(ctx);
15602139Sjp161948 }
15612139Sjp161948 else
15622139Sjp161948 {
15632139Sjp161948 pq_64bit_sub(&tmp, &(bitmap->max_seq_num), &rcd_num);
15642139Sjp161948 pq_64bit_sub_word(&tmp, 1);
15652139Sjp161948 shift = (unsigned int)pq_64bit_get_word(&tmp);
15662139Sjp161948
15672139Sjp161948 pq_64bit_set_bit(&(bitmap->map), shift);
15682139Sjp161948 }
15692139Sjp161948
15702139Sjp161948 pq_64bit_free(&rcd_num);
15712139Sjp161948 pq_64bit_free(&tmp);
15722139Sjp161948 }
15732139Sjp161948
15742139Sjp161948
dtls1_dispatch_alert(SSL * s)15752139Sjp161948 int dtls1_dispatch_alert(SSL *s)
15762139Sjp161948 {
15772139Sjp161948 int i,j;
15782139Sjp161948 void (*cb)(const SSL *ssl,int type,int val)=NULL;
15792139Sjp161948 unsigned char buf[2 + 2 + 3]; /* alert level + alert desc + message seq +frag_off */
15802139Sjp161948 unsigned char *ptr = &buf[0];
15812139Sjp161948
15822139Sjp161948 s->s3->alert_dispatch=0;
15832139Sjp161948
15842139Sjp161948 memset(buf, 0x00, sizeof(buf));
15852139Sjp161948 *ptr++ = s->s3->send_alert[0];
15862139Sjp161948 *ptr++ = s->s3->send_alert[1];
15872139Sjp161948
15882139Sjp161948 if (s->s3->send_alert[1] == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE)
15892139Sjp161948 {
15902139Sjp161948 s2n(s->d1->handshake_read_seq, ptr);
15912139Sjp161948 #if 0
15922139Sjp161948 if ( s->d1->r_msg_hdr.frag_off == 0) /* waiting for a new msg */
15932139Sjp161948
15942139Sjp161948 else
15952139Sjp161948 s2n(s->d1->r_msg_hdr.seq, ptr); /* partial msg read */
15962139Sjp161948 #endif
15972139Sjp161948
15982139Sjp161948 #if 0
15992139Sjp161948 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);
16002139Sjp161948 #endif
16012139Sjp161948 l2n3(s->d1->r_msg_hdr.frag_off, ptr);
16022139Sjp161948 }
16032139Sjp161948
16042139Sjp161948 i = do_dtls1_write(s, SSL3_RT_ALERT, &buf[0], sizeof(buf), 0);
16052139Sjp161948 if (i <= 0)
16062139Sjp161948 {
16072139Sjp161948 s->s3->alert_dispatch=1;
16082139Sjp161948 /* fprintf( stderr, "not done with alert\n" ); */
16092139Sjp161948 }
16102139Sjp161948 else
16112139Sjp161948 {
16122139Sjp161948 if ( s->s3->send_alert[0] == SSL3_AL_FATAL ||
16132139Sjp161948 s->s3->send_alert[1] == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE)
16142139Sjp161948 (void)BIO_flush(s->wbio);
16152139Sjp161948
16162139Sjp161948 if (s->msg_callback)
16172139Sjp161948 s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert,
16182139Sjp161948 2, s, s->msg_callback_arg);
16192139Sjp161948
16202139Sjp161948 if (s->info_callback != NULL)
16212139Sjp161948 cb=s->info_callback;
16222139Sjp161948 else if (s->ctx->info_callback != NULL)
16232139Sjp161948 cb=s->ctx->info_callback;
16242139Sjp161948
16252139Sjp161948 if (cb != NULL)
16262139Sjp161948 {
16272139Sjp161948 j=(s->s3->send_alert[0]<<8)|s->s3->send_alert[1];
16282139Sjp161948 cb(s,SSL_CB_WRITE_ALERT,j);
16292139Sjp161948 }
16302139Sjp161948 }
16312139Sjp161948 return(i);
16322139Sjp161948 }
16332139Sjp161948
16342139Sjp161948
16352139Sjp161948 static DTLS1_BITMAP *
dtls1_get_bitmap(SSL * s,SSL3_RECORD * rr,unsigned int * is_next_epoch)16362139Sjp161948 dtls1_get_bitmap(SSL *s, SSL3_RECORD *rr, unsigned int *is_next_epoch)
16372139Sjp161948 {
16382139Sjp161948
16392139Sjp161948 *is_next_epoch = 0;
16402139Sjp161948
16412139Sjp161948 /* In current epoch, accept HM, CCS, DATA, & ALERT */
16422139Sjp161948 if (rr->epoch == s->d1->r_epoch)
16432139Sjp161948 return &s->d1->bitmap;
16442139Sjp161948
16452139Sjp161948 /* Only HM and ALERT messages can be from the next epoch */
16462139Sjp161948 else if (rr->epoch == (unsigned long)(s->d1->r_epoch + 1) &&
16472139Sjp161948 (rr->type == SSL3_RT_HANDSHAKE ||
16482139Sjp161948 rr->type == SSL3_RT_ALERT))
16492139Sjp161948 {
16502139Sjp161948 *is_next_epoch = 1;
16512139Sjp161948 return &s->d1->next_bitmap;
16522139Sjp161948 }
16532139Sjp161948
16542139Sjp161948 return NULL;
16552139Sjp161948 }
16562139Sjp161948
16572139Sjp161948 #if 0
16582139Sjp161948 static int
16592139Sjp161948 dtls1_record_needs_buffering(SSL *s, SSL3_RECORD *rr, unsigned short *priority,
16602139Sjp161948 unsigned long *offset)
16612139Sjp161948 {
16622139Sjp161948
16632139Sjp161948 /* alerts are passed up immediately */
16642139Sjp161948 if ( rr->type == SSL3_RT_APPLICATION_DATA ||
16652139Sjp161948 rr->type == SSL3_RT_ALERT)
16662139Sjp161948 return 0;
16672139Sjp161948
16682139Sjp161948 /* Only need to buffer if a handshake is underway.
16692139Sjp161948 * (this implies that Hello Request and Client Hello are passed up
16702139Sjp161948 * immediately) */
16712139Sjp161948 if ( SSL_in_init(s))
16722139Sjp161948 {
16732139Sjp161948 unsigned char *data = rr->data;
16742139Sjp161948 /* need to extract the HM/CCS sequence number here */
16752139Sjp161948 if ( rr->type == SSL3_RT_HANDSHAKE ||
16762139Sjp161948 rr->type == SSL3_RT_CHANGE_CIPHER_SPEC)
16772139Sjp161948 {
16782139Sjp161948 unsigned short seq_num;
16792139Sjp161948 struct hm_header_st msg_hdr;
16802139Sjp161948 struct ccs_header_st ccs_hdr;
16812139Sjp161948
16822139Sjp161948 if ( rr->type == SSL3_RT_HANDSHAKE)
16832139Sjp161948 {
16842139Sjp161948 dtls1_get_message_header(data, &msg_hdr);
16852139Sjp161948 seq_num = msg_hdr.seq;
16862139Sjp161948 *offset = msg_hdr.frag_off;
16872139Sjp161948 }
16882139Sjp161948 else
16892139Sjp161948 {
16902139Sjp161948 dtls1_get_ccs_header(data, &ccs_hdr);
16912139Sjp161948 seq_num = ccs_hdr.seq;
16922139Sjp161948 *offset = 0;
16932139Sjp161948 }
16942139Sjp161948
16952139Sjp161948 /* this is either a record we're waiting for, or a
16962139Sjp161948 * retransmit of something we happened to previously
16972139Sjp161948 * receive (higher layers will drop the repeat silently */
16982139Sjp161948 if ( seq_num < s->d1->handshake_read_seq)
16992139Sjp161948 return 0;
17002139Sjp161948 if (rr->type == SSL3_RT_HANDSHAKE &&
17012139Sjp161948 seq_num == s->d1->handshake_read_seq &&
17022139Sjp161948 msg_hdr.frag_off < s->d1->r_msg_hdr.frag_off)
17032139Sjp161948 return 0;
17042139Sjp161948 else if ( seq_num == s->d1->handshake_read_seq &&
17052139Sjp161948 (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC ||
17062139Sjp161948 msg_hdr.frag_off == s->d1->r_msg_hdr.frag_off))
17072139Sjp161948 return 0;
17082139Sjp161948 else
17092139Sjp161948 {
17102139Sjp161948 *priority = seq_num;
17112139Sjp161948 return 1;
17122139Sjp161948 }
17132139Sjp161948 }
17142139Sjp161948 else /* unknown record type */
17152139Sjp161948 return 0;
17162139Sjp161948 }
17172139Sjp161948
17182139Sjp161948 return 0;
17192139Sjp161948 }
17202139Sjp161948 #endif
17212139Sjp161948
17222139Sjp161948 void
dtls1_reset_seq_numbers(SSL * s,int rw)17232139Sjp161948 dtls1_reset_seq_numbers(SSL *s, int rw)
17242139Sjp161948 {
17252139Sjp161948 unsigned char *seq;
17262139Sjp161948 unsigned int seq_bytes = sizeof(s->s3->read_sequence);
17272139Sjp161948
17282139Sjp161948 if ( rw & SSL3_CC_READ)
17292139Sjp161948 {
17302139Sjp161948 seq = s->s3->read_sequence;
17312139Sjp161948 s->d1->r_epoch++;
17322139Sjp161948
17332139Sjp161948 pq_64bit_assign(&(s->d1->bitmap.map), &(s->d1->next_bitmap.map));
17342139Sjp161948 s->d1->bitmap.length = s->d1->next_bitmap.length;
17352139Sjp161948 pq_64bit_assign(&(s->d1->bitmap.max_seq_num),
17362139Sjp161948 &(s->d1->next_bitmap.max_seq_num));
17372139Sjp161948
17382139Sjp161948 pq_64bit_free(&(s->d1->next_bitmap.map));
17392139Sjp161948 pq_64bit_free(&(s->d1->next_bitmap.max_seq_num));
17402139Sjp161948 memset(&(s->d1->next_bitmap), 0x00, sizeof(DTLS1_BITMAP));
17412139Sjp161948 pq_64bit_init(&(s->d1->next_bitmap.map));
17422139Sjp161948 pq_64bit_init(&(s->d1->next_bitmap.max_seq_num));
17432139Sjp161948 }
17442139Sjp161948 else
17452139Sjp161948 {
17462139Sjp161948 seq = s->s3->write_sequence;
17472139Sjp161948 s->d1->w_epoch++;
17482139Sjp161948 }
17492139Sjp161948
17502139Sjp161948 memset(seq, 0x00, seq_bytes);
17512139Sjp161948 }
17522139Sjp161948
17532139Sjp161948 #if PQ_64BIT_IS_INTEGER
17542139Sjp161948 static PQ_64BIT
bytes_to_long_long(unsigned char * bytes,PQ_64BIT * num)17552139Sjp161948 bytes_to_long_long(unsigned char *bytes, PQ_64BIT *num)
17562139Sjp161948 {
17572139Sjp161948 PQ_64BIT _num;
17582139Sjp161948
17592139Sjp161948 _num = (((PQ_64BIT)bytes[0]) << 56) |
17602139Sjp161948 (((PQ_64BIT)bytes[1]) << 48) |
17612139Sjp161948 (((PQ_64BIT)bytes[2]) << 40) |
17622139Sjp161948 (((PQ_64BIT)bytes[3]) << 32) |
17632139Sjp161948 (((PQ_64BIT)bytes[4]) << 24) |
17642139Sjp161948 (((PQ_64BIT)bytes[5]) << 16) |
17652139Sjp161948 (((PQ_64BIT)bytes[6]) << 8) |
17662139Sjp161948 (((PQ_64BIT)bytes[7]) );
17672139Sjp161948
17682139Sjp161948 *num = _num ;
17692139Sjp161948 return _num;
17702139Sjp161948 }
17712139Sjp161948 #endif
17722139Sjp161948
17732139Sjp161948
17742139Sjp161948 static void
dtls1_clear_timeouts(SSL * s)17752139Sjp161948 dtls1_clear_timeouts(SSL *s)
17762139Sjp161948 {
17772139Sjp161948 memset(&(s->d1->timeout), 0x00, sizeof(struct dtls1_timeout_st));
17782139Sjp161948 }
1779