PR: 1981
[openssl.git] / ssl / d1_pkt.c
index 4b3aabcfa7b6aa3be70659a07524ef9af37bd7ea..a9404492ddc8f3f2588b0518560f3508a0fe3c8d 100644 (file)
 #include <openssl/evp.h>
 #include <openssl/buffer.h>
 #include <openssl/pqueue.h>
+#include <openssl/rand.h>
+
+/* mod 128 saturating subtract of two 64-bit values in big-endian order */
+static int satsub64be(const unsigned char *v1,const unsigned char *v2)
+{      int ret,sat,brw,i;
+
+       if (sizeof(long) == 8) do
+       {       const union { long one; char little; } is_endian = {1};
+               long l;
+
+               if (is_endian.little)                   break;
+               /* not reached on little-endians */
+               /* following test is redundant, because input is
+                * always aligned, but I take no chances... */
+               if (((size_t)v1|(size_t)v2)&0x7)        break;
+
+               l  = *((long *)v1);
+               l -= *((long *)v2);
+               if (l>128)              return 128;
+               else if (l<-128)        return -128;
+               else                    return (int)l;
+       } while (0);
+
+       ret = (int)v1[7]-(int)v2[7];
+       sat = 0;
+       brw = ret>>8;   /* brw is either 0 or -1 */
+       if (ret & 0x80)
+       {       for (i=6;i>=0;i--)
+               {       brw += (int)v1[i]-(int)v2[i];
+                       sat |= ~brw;
+                       brw >>= 8;
+               }
+       }
+       else
+       {       for (i=6;i>=0;i--)
+               {       brw += (int)v1[i]-(int)v2[i];
+                       sat |= brw;
+                       brw >>= 8;
+               }
+       }
+       brw <<= 8;      /* brw is either 0 or -256 */
+
+       if (sat&0xff)   return brw | 0x80;
+       else            return brw + (ret&0xFF);
+}
 
 static int have_handshake_fragment(SSL *s, int type, unsigned char *buf, 
        int len, int peek);
-static int dtls1_record_replay_check(SSL *s, DTLS1_BITMAP *bitmap,
-       unsigned long long *seq_num);
+static int dtls1_record_replay_check(SSL *s, DTLS1_BITMAP *bitmap);
 static void dtls1_record_bitmap_update(SSL *s, DTLS1_BITMAP *bitmap);
 static DTLS1_BITMAP *dtls1_get_bitmap(SSL *s, SSL3_RECORD *rr, 
     unsigned int *is_next_epoch);
@@ -133,13 +177,10 @@ static int dtls1_record_needs_buffering(SSL *s, SSL3_RECORD *rr,
        unsigned short *priority, unsigned long *offset);
 #endif
 static int dtls1_buffer_record(SSL *s, record_pqueue *q,
-       unsigned long long priority);
+       unsigned char *priority);
 static int dtls1_process_record(SSL *s);
-static unsigned long long bytes_to_long_long(unsigned char *bytes);
-static void long_long_to_bytes(unsigned long long num, unsigned char *bytes);
 static void dtls1_clear_timeouts(SSL *s);
 
-
 /* copy buffered record into SSL structure */
 static int
 dtls1_copy_record(SSL *s, pitem *item)
@@ -161,11 +202,15 @@ dtls1_copy_record(SSL *s, pitem *item)
 
 
 static int
-dtls1_buffer_record(SSL *s, record_pqueue *queue, unsigned long long priority)
-{
-    DTLS1_RECORD_DATA *rdata;
+dtls1_buffer_record(SSL *s, record_pqueue *queue, unsigned char *priority)
+       {
+       DTLS1_RECORD_DATA *rdata;
        pitem *item;
 
+       /* Limit the size of the queue to prevent DOS attacks */
+       if (pqueue_size(queue->q) >= 100)
+               return 0;
+               
        rdata = OPENSSL_malloc(sizeof(DTLS1_RECORD_DATA));
        item = pitem_new(priority, rdata);
        if (rdata == NULL || item == NULL)
@@ -173,7 +218,7 @@ dtls1_buffer_record(SSL *s, record_pqueue *queue, unsigned long long priority)
                if (rdata != NULL) OPENSSL_free(rdata);
                if (item != NULL) pitem_free(item);
                
-               SSLerr(SSL_F_SSL3_GET_RECORD, ERR_R_INTERNAL_ERROR);
+               SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR);
                return(0);
                }
        
@@ -197,10 +242,16 @@ dtls1_buffer_record(SSL *s, record_pqueue *queue, unsigned long long priority)
        memset(&(s->s3->rbuf), 0, sizeof(SSL3_BUFFER));
        memset(&(s->s3->rrec), 0, sizeof(SSL3_RECORD));
        
-       ssl3_setup_buffers(s);
+       if (!ssl3_setup_buffers(s))
+               {
+               SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR);
+               OPENSSL_free(rdata);
+               pitem_free(item);
+               return(0);
+               }
        
        return(1);
-    }
+       }
 
 
 static int
@@ -275,9 +326,9 @@ static int
 dtls1_get_buffered_record(SSL *s)
        {
        pitem *item;
-       unsigned long long priority = 
-               (((unsigned long long)s->d1->handshake_read_seq) << 32) | 
-               ((unsigned long long)s->d1->r_msg_hdr.frag_off);
+       PQ_64BIT priority = 
+               (((PQ_64BIT)s->d1->handshake_read_seq) << 32) | 
+               ((PQ_64BIT)s->d1->r_msg_hdr.frag_off);
        
        if ( ! SSL_in_init(s))  /* if we're not (re)negotiating, 
                                                           nothing buffered */
@@ -317,17 +368,17 @@ dtls1_get_buffered_record(SSL *s)
 static int
 dtls1_process_record(SSL *s)
 {
-    int i,al;
+       int i,al;
        int clear=0;
-    int enc_err;
+       int enc_err;
        SSL_SESSION *sess;
-    SSL3_RECORD *rr;
+       SSL3_RECORD *rr;
        unsigned int mac_size;
        unsigned char md[EVP_MAX_MD_SIZE];
 
 
        rr= &(s->s3->rrec);
-    sess = s->session;
+       sess = s->session;
 
        /* At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length,
         * and we have that many bytes in s->packet
@@ -348,7 +399,7 @@ dtls1_process_record(SSL *s)
        if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH)
                {
                al=SSL_AD_RECORD_OVERFLOW;
-               SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
+               SSLerr(SSL_F_DTLS1_PROCESS_RECORD,SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
                goto f_err;
                }
 
@@ -373,20 +424,24 @@ printf("\n");
 #endif
 
        /* r->length is now the compressed data plus mac */
-if (   (sess == NULL) ||
+       if (    (sess == NULL) ||
                (s->enc_read_ctx == NULL) ||
                (s->read_hash == NULL))
-    clear=1;
+               clear=1;
 
        if (!clear)
                {
-               mac_size=EVP_MD_size(s->read_hash);
+               /* !clear => s->read_hash != NULL => mac_size != -1 */
+               int t;
+               t=EVP_MD_CTX_size(s->read_hash);
+               OPENSSL_assert(t >= 0);
+               mac_size=t;
 
                if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH+mac_size)
                        {
 #if 0 /* OK only for stream ciphers (then rr->length is visible from ciphertext anyway) */
                        al=SSL_AD_RECORD_OVERFLOW;
-                       SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_PRE_MAC_LENGTH_TOO_LONG);
+                       SSLerr(SSL_F_DTLS1_PROCESS_RECORD,SSL_R_PRE_MAC_LENGTH_TOO_LONG);
                        goto f_err;
 #else
                        goto decryption_failed_or_bad_record_mac;
@@ -397,7 +452,7 @@ if (        (sess == NULL) ||
                        {
 #if 0 /* OK only for stream ciphers */
                        al=SSL_AD_DECODE_ERROR;
-                       SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_LENGTH_TOO_SHORT);
+                       SSLerr(SSL_F_DTLS1_PROCESS_RECORD,SSL_R_LENGTH_TOO_SHORT);
                        goto f_err;
 #else
                        goto decryption_failed_or_bad_record_mac;
@@ -405,7 +460,7 @@ if (        (sess == NULL) ||
                        }
                rr->length-=mac_size;
                i=s->method->ssl3_enc->mac(s,md,0);
-               if (memcmp(md,&(rr->data[rr->length]),mac_size) != 0)
+               if (i < 0 || memcmp(md,&(rr->data[rr->length]),mac_size) != 0)
                        {
                        goto decryption_failed_or_bad_record_mac;
                        }
@@ -417,13 +472,13 @@ if (      (sess == NULL) ||
                if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH)
                        {
                        al=SSL_AD_RECORD_OVERFLOW;
-                       SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_COMPRESSED_LENGTH_TOO_LONG);
+                       SSLerr(SSL_F_DTLS1_PROCESS_RECORD,SSL_R_COMPRESSED_LENGTH_TOO_LONG);
                        goto f_err;
                        }
                if (!ssl3_do_uncompress(s))
                        {
                        al=SSL_AD_DECOMPRESSION_FAILURE;
-                       SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_BAD_DECOMPRESSION);
+                       SSLerr(SSL_F_DTLS1_PROCESS_RECORD,SSL_R_BAD_DECOMPRESSION);
                        goto f_err;
                        }
                }
@@ -431,7 +486,7 @@ if (        (sess == NULL) ||
        if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH)
                {
                al=SSL_AD_RECORD_OVERFLOW;
-               SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_DATA_LENGTH_TOO_LONG);
+               SSLerr(SSL_F_DTLS1_PROCESS_RECORD,SSL_R_DATA_LENGTH_TOO_LONG);
                goto f_err;
                }
 
@@ -446,8 +501,8 @@ if (        (sess == NULL) ||
 
        /* we have pulled in a full packet so zero things */
        s->packet_length=0;
-    dtls1_record_bitmap_update(s, &(s->d1->bitmap));/* Mark receipt of record. */
-    return(1);
+       dtls1_record_bitmap_update(s, &(s->d1->bitmap));/* Mark receipt of record. */
+       return(1);
 
 decryption_failed_or_bad_record_mac:
        /* Separate 'decryption_failed' alert was introduced with TLS 1.0,
@@ -456,7 +511,7 @@ decryption_failed_or_bad_record_mac:
         * we should not reveal which kind of error occured -- this
         * might become visible to an attacker (e.g. via logfile) */
        al=SSL_AD_BAD_RECORD_MAC;
-       SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
+       SSLerr(SSL_F_DTLS1_PROCESS_RECORD,SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
 f_err:
        ssl3_send_alert(s,SSL3_AL_FATAL,al);
 err:
@@ -480,18 +535,17 @@ int dtls1_get_record(SSL *s)
        SSL3_RECORD *rr;
        SSL_SESSION *sess;
        unsigned char *p;
-       short version;
+       unsigned short version;
        DTLS1_BITMAP *bitmap;
-       unsigned long long read_sequence;
-    unsigned int is_next_epoch;
+       unsigned int is_next_epoch;
 
        rr= &(s->s3->rrec);
        sess=s->session;
 
-    /* The epoch may have changed.  If so, process all the
-     * pending records.  This is a non-blocking operation. */
-    if ( ! dtls1_process_buffered_records(s))
-        return 0;
+       /* The epoch may have changed.  If so, process all the
+        * pending records.  This is a non-blocking operation. */
+       if ( ! dtls1_process_buffered_records(s))
+            return 0;
 
        /* if we're renegotiating, then there may be buffered records */
        if (dtls1_get_processed_record(s))
@@ -519,43 +573,40 @@ again:
                ssl_minor= *(p++);
                version=(ssl_major<<8)|ssl_minor;
 
-        /* sequence number is 64 bits, with top 2 bytes = epoch */ 
+               /* sequence number is 64 bits, with top 2 bytes = epoch */ 
                n2s(p,rr->epoch);
 
-               read_sequence = 0;
-               n2l6(p, read_sequence);
-               long_long_to_bytes(read_sequence, s->s3->read_sequence);
+               memcpy(&(s->s3->read_sequence[2]), p, 6);
+               p+=6;
+
                n2s(p,rr->length);
 
                /* Lets check version */
-               if (s->first_packet)
-                       {
-                       s->first_packet=0;
-                       }
-               else
+               if (!s->first_packet)
                        {
                        if (version != s->version)
                                {
-                               SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_WRONG_VERSION_NUMBER);
-                               /* Send back error using their
-                                * version number :-) */
-                               s->version=version;
-                               al=SSL_AD_PROTOCOL_VERSION;
-                               goto f_err;
+                               /* unexpected version, silently discard */
+                               rr->length = 0;
+                               s->packet_length = 0;
+                               goto again;
                                }
                        }
 
-               if ((version & 0xff00) != (DTLS1_VERSION & 0xff00))
+               if ((version & 0xff00) != (s->version & 0xff00))
                        {
-                       SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_WRONG_VERSION_NUMBER);
-                       goto err;
+                       /* wrong version, silently discard record */
+                       rr->length = 0;
+                       s->packet_length = 0;
+                       goto again;
                        }
 
                if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH)
                        {
-                       al=SSL_AD_RECORD_OVERFLOW;
-                       SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_PACKET_LENGTH_TOO_LONG);
-                       goto f_err;
+                       /* record too long, silently discard it */
+                       rr->length = 0;
+                       s->packet_length = 0;
+                       goto again;
                        }
 
                /* now s->rstate == SSL_ST_READ_BODY */
@@ -573,6 +624,7 @@ again:
                /* this packet contained a partial record, dump it */
                if ( n != i)
                        {
+                       rr->length = 0;
                        s->packet_length = 0;
                        goto again;
                        }
@@ -585,14 +637,16 @@ again:
        /* match epochs.  NULL means the packet is dropped on the floor */
        bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);
        if ( bitmap == NULL)
-        {
-        s->packet_length = 0;  /* dump this record */
-        goto again;   /* get another record */
+               {
+               rr->length = 0;
+               s->packet_length = 0;  /* dump this record */
+               goto again;   /* get another record */
                }
 
        /* check whether this is a repeat, or aged record */
-       if ( ! dtls1_record_replay_check(s, bitmap, &(rr->seq_num)))
+       if ( ! dtls1_record_replay_check(s, bitmap))
                {
+               rr->length = 0;
                s->packet_length=0; /* dump this record */
                goto again;     /* get another record */
                }
@@ -600,20 +654,22 @@ again:
        /* just read a 0 length packet */
        if (rr->length == 0) goto again;
 
-    /* If this record is from the next epoch (either HM or ALERT), buffer it
-     * since it cannot be processed at this time.
-     * Records from the next epoch are marked as received even though they are 
-     * not processed, so as to prevent any potential resource DoS attack */
-    if (is_next_epoch)
-        {
-        dtls1_record_bitmap_update(s, bitmap);
-        dtls1_buffer_record(s, &(s->d1->unprocessed_rcds), rr->seq_num);
-        s->packet_length = 0;
-        goto again;
-        }
+       /* If this record is from the next epoch (either HM or ALERT),
+        * buffer it since it cannot be processed at this time. Records
+        * from the next epoch are marked as received even though they
+        * are not processed, so as to prevent any potential resource
+        * DoS attack */
+       if (is_next_epoch)
+               {
+               dtls1_record_bitmap_update(s, bitmap);
+               dtls1_buffer_record(s, &(s->d1->unprocessed_rcds), rr->seq_num);
+               rr->length = 0;
+               s->packet_length = 0;
+               goto again;
+               }
 
-    if ( ! dtls1_process_record(s))
-        return(0);
+       if ( ! dtls1_process_record(s))
+               return(0);
 
        dtls1_clear_timeouts(s);  /* done waiting */
        return(1);
@@ -667,7 +723,7 @@ int dtls1_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
                (type != SSL3_RT_HANDSHAKE) && type) ||
            (peek && (type != SSL3_RT_APPLICATION_DATA)))
                {
-               SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR);
+               SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);
                return -1;
                }
 
@@ -684,7 +740,7 @@ int dtls1_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
                if (i < 0) return(i);
                if (i == 0)
                        {
-                       SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);
+                       SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);
                        return(-1);
                        }
                }
@@ -698,6 +754,30 @@ start:
         * s->s3->rrec.length,  - number of bytes. */
        rr = &(s->s3->rrec);
 
+       /* We are not handshaking and have no data yet,
+        * so process data buffered during the last handshake
+        * in advance, if any.
+        */
+       if (s->state == SSL_ST_OK && rr->length == 0)
+               {
+               pitem *item;
+               item = pqueue_pop(s->d1->buffered_app_data.q);
+               if (item)
+                       {
+                       dtls1_copy_record(s, item);
+
+                       OPENSSL_free(item->data);
+                       pitem_free(item);
+                       }
+               }
+
+       /* Check for timeout */
+       if (dtls1_is_timer_expired(s))
+               {
+               if (dtls1_read_failed(s, -1) > 0);
+                       goto start;
+               }
+
        /* get new packet if necessary */
        if ((rr->length == 0) || (s->rstate == SSL_ST_READ_BODY))
                {
@@ -719,9 +799,14 @@ start:
                                       * reset by ssl3_get_finished */
                && (rr->type != SSL3_RT_HANDSHAKE))
                {
-               al=SSL_AD_UNEXPECTED_MESSAGE;
-               SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);
-               goto err;
+               /* We now have application data between CCS and Finished.
+                * Most likely the packets were reordered on their way, so
+                * buffer the application data for later processing rather
+                * than dropping the connection.
+                */
+               dtls1_buffer_record(s, &(s->d1->buffered_app_data), 0);
+               rr->length = 0;
+               goto start;
                }
 
        /* If the other end has shut down, throw anything we read away
@@ -742,7 +827,7 @@ start:
                        (s->enc_read_ctx == NULL))
                        {
                        al=SSL_AD_UNEXPECTED_MESSAGE;
-                       SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_APP_DATA_IN_HANDSHAKE);
+                       SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_APP_DATA_IN_HANDSHAKE);
                        goto f_err;
                        }
 
@@ -775,7 +860,7 @@ start:
         * fill that so that we can process the data at a fixed place.
         */
                {
-               unsigned int i, dest_maxlen = 0;
+               unsigned int k, dest_maxlen = 0;
                unsigned char *dest = NULL;
                unsigned int *dest_len = NULL;
 
@@ -791,9 +876,28 @@ start:
                        dest = s->d1->alert_fragment;
                        dest_len = &s->d1->alert_fragment_len;
                        }
-               else    /* else it's a CCS message */
-                       OPENSSL_assert(rr->type == SSL3_RT_CHANGE_CIPHER_SPEC);
+               /* else it's a CCS message, or application data or wrong */
+               else if (rr->type != SSL3_RT_CHANGE_CIPHER_SPEC)
+                       {
+                       /* Application data while renegotiating
+                        * is allowed. Try again reading.
+                        */
+                       if (rr->type == SSL3_RT_APPLICATION_DATA)
+                               {
+                               BIO *bio;
+                               s->s3->in_read_app_data=2;
+                               bio=SSL_get_rbio(s);
+                               s->rwstate=SSL_READING;
+                               BIO_clear_retry_flags(bio);
+                               BIO_set_retry_read(bio);
+                               return(-1);
+                               }
 
+                       /* Not certain if this is the right error handling */
+                       al=SSL_AD_UNEXPECTED_MESSAGE;
+                       SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_UNEXPECTED_RECORD);
+                       goto f_err;
+                       }
 
                if (dest_maxlen > 0)
                        {
@@ -801,15 +905,23 @@ start:
              *  may be fragmented--don't always expect dest_maxlen bytes */
                        if ( rr->length < dest_maxlen)
                                {
+#ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
+                               /*
+                                * for normal alerts rr->length is 2, while
+                                * dest_maxlen is 7 if we were to handle this
+                                * non-existing alert...
+                                */
+                               FIX ME
+#endif
                                s->rstate=SSL_ST_READ_HEADER;
                                rr->length = 0;
                                goto start;
                                }
 
                        /* now move 'n' bytes: */
-                       for ( i = 0; i < dest_maxlen; i++)
+                       for ( k = 0; k < dest_maxlen; k++)
                                {
-                               dest[i] = rr->data[rr->off++];
+                               dest[k] = rr->data[rr->off++];
                                rr->length--;
                                }
                        *dest_len = dest_maxlen;
@@ -833,7 +945,7 @@ start:
                        (s->d1->handshake_fragment[3] != 0))
                        {
                        al=SSL_AD_DECODE_ERROR;
-                       SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_BAD_HELLO_REQUEST);
+                       SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_BAD_HELLO_REQUEST);
                        goto err;
                        }
 
@@ -854,7 +966,7 @@ start:
                                if (i < 0) return(i);
                                if (i == 0)
                                        {
-                                       SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);
+                                       SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);
                                        return(-1);
                                        }
 
@@ -923,7 +1035,9 @@ start:
                                n2s(p, seq);
                                n2l3(p, frag_off);
 
-                               dtls1_retransmit_message(s, seq, frag_off, &found);
+                               dtls1_retransmit_message(s,
+                                                                                dtls1_get_queue_priority(frag->msg_header.seq, 0),
+                                                                                frag_off, &found);
                                if ( ! found  && SSL_in_init(s))
                                        {
                                        /* fprintf( stderr,"in init = %d\n", SSL_in_init(s)); */
@@ -941,7 +1055,7 @@ start:
 
                        s->rwstate=SSL_NOTHING;
                        s->s3->fatal_alert = alert_descr;
-                       SSLerr(SSL_F_SSL3_READ_BYTES, SSL_AD_REASON_OFFSET + alert_descr);
+                       SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_AD_REASON_OFFSET + alert_descr);
                        BIO_snprintf(tmp,sizeof tmp,"%d",alert_descr);
                        ERR_add_error_data(2,"SSL alert number ",tmp);
                        s->shutdown|=SSL_RECEIVED_SHUTDOWN;
@@ -951,7 +1065,7 @@ start:
                else
                        {
                        al=SSL_AD_ILLEGAL_PARAMETER;
-                       SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_UNKNOWN_ALERT_TYPE);
+                       SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_UNKNOWN_ALERT_TYPE);
                        goto f_err;
                        }
 
@@ -966,47 +1080,53 @@ start:
                }
 
        if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC)
-        {
-        struct ccs_header_st ccs_hdr;
+               {
+               struct ccs_header_st ccs_hdr;
+               unsigned int ccs_hdr_len = DTLS1_CCS_HEADER_LENGTH;
 
                dtls1_get_ccs_header(rr->data, &ccs_hdr);
 
-               if ( ccs_hdr.seq == s->d1->handshake_read_seq)
+               if (s->version == DTLS1_BAD_VER)
+                       ccs_hdr_len = 3;
+
+               /* 'Change Cipher Spec' is just a single byte, so we know
+                * exactly what the record payload has to look like */
+               /* XDTLS: check that epoch is consistent */
+               if (    (rr->length != ccs_hdr_len) || 
+                       (rr->off != 0) || (rr->data[0] != SSL3_MT_CCS))
                        {
-                       /* 'Change Cipher Spec' is just a single byte, so we know
-                        * exactly what the record payload has to look like */
-                       /* XDTLS: check that epoch is consistent */
-                       if (    (rr->length != DTLS1_CCS_HEADER_LENGTH) || 
-                               (rr->off != 0) || (rr->data[0] != SSL3_MT_CCS))
-                               {
-                               i=SSL_AD_ILLEGAL_PARAMETER;
-                               SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_BAD_CHANGE_CIPHER_SPEC);
-                               goto err;
-                               }
-                       
-                       rr->length=0;
-                       
-                       if (s->msg_callback)
-                               s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, 
-                                       rr->data, 1, s, s->msg_callback_arg);
-                       
-                       s->s3->change_cipher_spec=1;
-                       if (!ssl3_do_change_cipher_spec(s))
-                               goto err;
-                       
-                       /* do this whenever CCS is processed */
-                       dtls1_reset_seq_numbers(s, SSL3_CC_READ);
-                       
-                       /* handshake read seq is reset upon handshake completion */
-                       s->d1->handshake_read_seq++;
-                       
-                       goto start;
+                       i=SSL_AD_ILLEGAL_PARAMETER;
+                       SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_BAD_CHANGE_CIPHER_SPEC);
+                       goto err;
                        }
-               else
+
+               rr->length=0;
+
+               if (s->msg_callback)
+                       s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, 
+                               rr->data, 1, s, s->msg_callback_arg);
+
+               /* We can't process a CCS now, because previous handshake
+                * messages are still missing, so just drop it.
+                */
+               if (!s->d1->change_cipher_spec_ok)
                        {
-                       rr->length = 0;
                        goto start;
                        }
+
+               s->d1->change_cipher_spec_ok = 0;
+
+               s->s3->change_cipher_spec=1;
+               if (!ssl3_do_change_cipher_spec(s))
+                       goto err;
+
+               /* do this whenever CCS is processed */
+               dtls1_reset_seq_numbers(s, SSL3_CC_READ);
+
+               if (s->version == DTLS1_BAD_VER)
+                       s->d1->handshake_read_seq++;
+
+               goto start;
                }
 
        /* Unexpected handshake message (Client Hello, or protocol violation) */
@@ -1023,6 +1143,16 @@ start:
                        goto start;
                        }
 
+               /* If we are server, we may have a repeated FINISHED of the
+                * client here, then retransmit our CCS and FINISHED.
+                */
+               if (msg_hdr.type == SSL3_MT_FINISHED)
+                       {
+                       dtls1_retransmit_buffered_messages(s);
+                       rr->length = 0;
+                       goto start;
+                       }
+
                if (((s->state&SSL_ST_MASK) == SSL_ST_OK) &&
                        !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS))
                        {
@@ -1041,7 +1171,7 @@ start:
                if (i < 0) return(i);
                if (i == 0)
                        {
-                       SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);
+                       SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);
                        return(-1);
                        }
 
@@ -1076,7 +1206,7 @@ start:
                        }
 #endif
                al=SSL_AD_UNEXPECTED_MESSAGE;
-               SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_UNEXPECTED_RECORD);
+               SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_UNEXPECTED_RECORD);
                goto f_err;
        case SSL3_RT_CHANGE_CIPHER_SPEC:
        case SSL3_RT_ALERT:
@@ -1085,7 +1215,7 @@ start:
                 * of SSL3_RT_HANDSHAKE when s->in_handshake is set, but that
                 * should not happen when type != rr->type */
                al=SSL_AD_UNEXPECTED_MESSAGE;
-               SSLerr(SSL_F_SSL3_READ_BYTES,ERR_R_INTERNAL_ERROR);
+               SSLerr(SSL_F_DTLS1_READ_BYTES,ERR_R_INTERNAL_ERROR);
                goto f_err;
        case SSL3_RT_APPLICATION_DATA:
                /* At this point, we were expecting handshake data,
@@ -1114,7 +1244,7 @@ start:
                else
                        {
                        al=SSL_AD_UNEXPECTED_MESSAGE;
-                       SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_UNEXPECTED_RECORD);
+                       SSLerr(SSL_F_DTLS1_READ_BYTES,SSL_R_UNEXPECTED_RECORD);
                        goto f_err;
                        }
                }
@@ -1138,7 +1268,7 @@ dtls1_write_app_data_bytes(SSL *s, int type, const void *buf_, int len)
                if (i < 0) return(i);
                if (i == 0)
                        {
-                       SSLerr(SSL_F_SSL3_WRITE_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);
+                       SSLerr(SSL_F_DTLS1_WRITE_APP_DATA_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);
                        return -1;
                        }
                }
@@ -1243,16 +1373,15 @@ int dtls1_write_bytes(SSL *s, int type, const void *buf_, int len)
                return i;
                }
 
-       if ( s->s3->wnum + i == len)
+       if ( (int)s->s3->wnum + i == len)
                s->s3->wnum = 0;
        else 
                s->s3->wnum += i;
 
-       return tot + i;
+       return i;
        }
 
-int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
-       unsigned int len, int create_empty_fragment)
+int do_dtls1_write(SSL *s, int type, const unsigned char *buf, unsigned int len, int create_empty_fragment)
        {
        unsigned char *p,*pseq;
        int i,mac_size,clear=0;
@@ -1288,19 +1417,23 @@ int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
 
        if (    (sess == NULL) ||
                (s->enc_write_ctx == NULL) ||
-               (s->write_hash == NULL))
+               (EVP_MD_CTX_md(s->write_hash) == NULL))
                clear=1;
 
        if (clear)
                mac_size=0;
        else
-               mac_size=EVP_MD_size(s->write_hash);
+               {
+               mac_size=EVP_MD_CTX_size(s->write_hash);
+               if (mac_size < 0)
+                       goto err;
+               }
 
        /* DTLS implements explicit IV, so no need for empty fragments */
 #if 0
        /* 'create_empty_fragment' is true only when this function calls itself */
        if (!clear && !create_empty_fragment && !s->s3->empty_fragment_done
-               && SSL_version(s) != DTLS1_VERSION)
+           && SSL_version(s) != DTLS1_VERSION && SSL_version(s) != DTLS1_BAD_VER)
                {
                /* countermeasure against known-IV weakness in CBC ciphersuites
                 * (see http://www.openssl.org/~bodo/tls-cbc.txt) 
@@ -1319,7 +1452,7 @@ int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
                        if (s->s3->wbuf.len < (size_t)prefix_len + SSL3_RT_MAX_PACKET_SIZE)
                                {
                                /* insufficient space */
-                               SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);
+                               SSLerr(SSL_F_DO_DTLS1_WRITE, ERR_R_INTERNAL_ERROR);
                                goto err;
                                }
                        }
@@ -1327,7 +1460,6 @@ int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
                s->s3->empty_fragment_done = 1;
                }
 #endif
-
        p = wb->buf + prefix_len;
 
        /* write the header */
@@ -1365,7 +1497,7 @@ int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
                {
                if (!ssl3_do_compress(s))
                        {
-                       SSLerr(SSL_F_DO_SSL3_WRITE,SSL_R_COMPRESSION_FAILURE);
+                       SSLerr(SSL_F_DO_DTLS1_WRITE,SSL_R_COMPRESSION_FAILURE);
                        goto err;
                        }
                }
@@ -1381,7 +1513,8 @@ int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
 
        if (mac_size != 0)
                {
-               s->method->ssl3_enc->mac(s,&(p[wr->length + bs]),1);
+               if(s->method->ssl3_enc->mac(s,&(p[wr->length + bs]),1) < 0)
+                       goto err;
                wr->length+=mac_size;
                }
 
@@ -1391,8 +1524,14 @@ int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
 
 
        /* ssl3_enc can only have an error on read */
-       wr->length += bs;  /* bs != 0 in case of CBC.  The enc fn provides
-                                               * the randomness */ 
+       if (bs) /* bs != 0 in case of CBC */
+               {
+               RAND_pseudo_bytes(p,bs);
+               /* master IV and last CBC residue stand for
+                * the rest of randomness */
+               wr->length += bs;
+               }
+
        s->method->ssl3_enc->enc(s,1);
 
        /* record length after mac and block padding */
@@ -1407,7 +1546,8 @@ int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
 /*     else
        s2n(s->d1->handshake_epoch, pseq); */
 
-       l2n6(bytes_to_long_long(s->s3->write_sequence), pseq);
+       memcpy(pseq, &(s->s3->write_sequence[2]), 6);
+       pseq+=6;
        s2n(wr->length,pseq);
 
        /* we should now have
@@ -1420,7 +1560,7 @@ int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
        /* buffer the record, making it easy to handle retransmits */
        if ( type == SSL3_RT_HANDSHAKE || type == SSL3_RT_CHANGE_CIPHER_SPEC)
                dtls1_buffer_record(s, wr->data, wr->length, 
-                       *((unsigned long long *)&(s->s3->write_sequence[0])));
+                       *((PQ_64BIT *)&(s->s3->write_sequence[0])));
 #endif
 
        ssl3_record_sequence_update(&(s->s3->write_sequence[0]));
@@ -1451,51 +1591,49 @@ err:
 
 
 
-static int dtls1_record_replay_check(SSL *s, DTLS1_BITMAP *bitmap,
-       unsigned long long *seq_num)
+static int dtls1_record_replay_check(SSL *s, DTLS1_BITMAP *bitmap)
        {
-       unsigned long long mask = 0x0000000000000001LL;
-       unsigned long long rcd_num;
+       int cmp;
+       unsigned int shift;
+       const unsigned char *seq = s->s3->read_sequence;
 
-       rcd_num = bytes_to_long_long(s->s3->read_sequence);
-       
-       if (rcd_num >= bitmap->max_seq_num)
+       cmp = satsub64be(seq,bitmap->max_seq_num);
+       if (cmp > 0)
                {
-               *seq_num = rcd_num;
-               return 1;  /* this record is new */
+               memcpy (s->s3->rrec.seq_num,seq,8);
+               return 1; /* this record in new */
                }
-       
-       if (bitmap->max_seq_num - rcd_num > bitmap->length)
-               return 0;  /* stale, outside the window */
-
-       mask <<= (bitmap->max_seq_num - rcd_num - 1);
-       if (bitmap->map & mask)
+       shift = -cmp;
+       if (shift >= sizeof(bitmap->map)*8)
+               return 0; /* stale, outside the window */
+       else if (bitmap->map & (1UL<<shift))
                return 0; /* record previously received */
-       
-       *seq_num = rcd_num;
+
+       memcpy (s->s3->rrec.seq_num,seq,8);
        return 1;
        }
 
 
 static void dtls1_record_bitmap_update(SSL *s, DTLS1_BITMAP *bitmap)
        {
+       int cmp;
        unsigned int shift;
-       unsigned long long mask = 0x0000000000000001L;
-       unsigned long long rcd_num;
-
-       rcd_num = bytes_to_long_long(s->s3->read_sequence);
+       const unsigned char *seq = s->s3->read_sequence;
 
-       if (rcd_num >= bitmap->max_seq_num)
+       cmp = satsub64be(seq,bitmap->max_seq_num);
+       if (cmp > 0)
                {
-               shift = rcd_num - bitmap->max_seq_num + 1;
-               bitmap->max_seq_num = rcd_num + 1;
-               bitmap->map <<= shift;
-               bitmap->map |= 0x0000000000000001LL;
+               shift = cmp;
+               if (shift < sizeof(bitmap->map)*8)
+                       bitmap->map <<= shift, bitmap->map |= 1UL;
+               else
+                       bitmap->map = 1UL;
+               memcpy(bitmap->max_seq_num,seq,8);
                }
-       else
-               {
-               mask <<= (bitmap->max_seq_num - rcd_num - 1);
-               bitmap->map |= mask;
+       else    {
+               shift = -cmp;
+               if (shift < sizeof(bitmap->map)*8)
+                       bitmap->map |= 1UL<<shift;
                }
        }
 
@@ -1504,8 +1642,8 @@ int dtls1_dispatch_alert(SSL *s)
        {
        int i,j;
        void (*cb)(const SSL *ssl,int type,int val)=NULL;
-       char buf[2 + 2 + 3]; /* alert level + alert desc + message seq +frag_off */
-       char *ptr = &buf[0];
+       unsigned char buf[DTLS1_AL_HEADER_LENGTH];
+       unsigned char *ptr = &buf[0];
 
        s->s3->alert_dispatch=0;
 
@@ -1513,6 +1651,7 @@ int dtls1_dispatch_alert(SSL *s)
        *ptr++ = s->s3->send_alert[0];
        *ptr++ = s->s3->send_alert[1];
 
+#ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
        if (s->s3->send_alert[1] == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE)
                {       
                s2n(s->d1->handshake_read_seq, ptr);
@@ -1528,6 +1667,7 @@ int dtls1_dispatch_alert(SSL *s)
 #endif
                l2n3(s->d1->r_msg_hdr.frag_off, ptr);
                }
+#endif
 
        i = do_dtls1_write(s, SSL3_RT_ALERT, &buf[0], sizeof(buf), 0);
        if (i <= 0)
@@ -1537,8 +1677,11 @@ int dtls1_dispatch_alert(SSL *s)
                }
        else
                {
-               if ( s->s3->send_alert[0] == SSL3_AL_FATAL ||
-                       s->s3->send_alert[1] == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE)
+               if (s->s3->send_alert[0] == SSL3_AL_FATAL
+#ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
+                   || s->s3->send_alert[1] == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
+#endif
+                   )
                        (void)BIO_flush(s->wbio);
 
                if (s->msg_callback)
@@ -1571,7 +1714,7 @@ dtls1_get_bitmap(SSL *s, SSL3_RECORD *rr, unsigned int *is_next_epoch)
         return &s->d1->bitmap;
 
     /* Only HM and ALERT messages can be from the next epoch */
-    else if (rr->epoch == s->d1->r_epoch + 1 &&
+    else if (rr->epoch == (unsigned long)(s->d1->r_epoch + 1) &&
         (rr->type == SSL3_RT_HANDSHAKE ||
             rr->type == SSL3_RT_ALERT))
         {
@@ -1663,6 +1806,7 @@ dtls1_reset_seq_numbers(SSL *s, int rw)
        else
                {
                seq = s->s3->write_sequence;
+               memcpy(s->d1->last_write_sequence, seq, sizeof(s->s3->write_sequence));
                s->d1->w_epoch++;
                }
 
@@ -1670,36 +1814,6 @@ dtls1_reset_seq_numbers(SSL *s, int rw)
        }
 
 
-static unsigned long long
-bytes_to_long_long(unsigned char *bytes)
-       {
-       unsigned long long num;
-
-       num = (((unsigned long long)bytes[0]) << 56) |
-               (((unsigned long long)bytes[1]) << 48) |
-               (((unsigned long long)bytes[2]) << 40) |
-               (((unsigned long long)bytes[3]) << 32) |
-               (((unsigned long long)bytes[4]) << 24) |
-               (((unsigned long long)bytes[5]) << 16) |
-               (((unsigned long long)bytes[6]) <<  8) |
-               (((unsigned long long)bytes[7])      );
-
-       return num;
-       }
-
-static void
-long_long_to_bytes(unsigned long long num, unsigned char *bytes)
-       {
-       bytes[0] = (unsigned char)((num >> 56)&0xff);
-       bytes[1] = (unsigned char)((num >> 48)&0xff);
-       bytes[2] = (unsigned char)((num >> 40)&0xff);
-       bytes[3] = (unsigned char)((num >> 32)&0xff);
-       bytes[4] = (unsigned char)((num >> 24)&0xff);
-       bytes[5] = (unsigned char)((num >> 16)&0xff);
-       bytes[6] = (unsigned char)((num >>  8)&0xff);
-       bytes[7] = (unsigned char)((num      )&0xff);
-       }
-
 static void
 dtls1_clear_timeouts(SSL *s)
        {