Index: openssl/ssl/d1_both.c |
=================================================================== |
--- openssl/ssl/d1_both.c (revision 105093) |
+++ openssl/ssl/d1_both.c (working copy) |
@@ -153,12 +153,11 @@ |
#endif |
static unsigned char bitmask_start_values[] = {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80}; |
-static unsigned char bitmask_end_values[] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f}; |
+static unsigned char bitmask_end_values[] = {0xff, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f}; |
/* XDTLS: figure out the right values */ |
static unsigned int g_probable_mtu[] = {1500 - 28, 512 - 28, 256 - 28}; |
-static unsigned int dtls1_min_mtu(void); |
static unsigned int dtls1_guess_mtu(unsigned int curr_mtu); |
static void dtls1_fix_message_header(SSL *s, unsigned long frag_off, |
unsigned long frag_len); |
@@ -264,17 +263,16 @@ |
return ret; |
mtu = s->d1->mtu - (DTLS1_HM_HEADER_LENGTH + DTLS1_RT_HEADER_LENGTH); |
} |
- |
- OPENSSL_assert(mtu > 0); /* should have something reasonable now */ |
- |
#endif |
+ OPENSSL_assert(s->d1->mtu >= dtls1_min_mtu()); /* should have something reasonable now */ |
+ |
if ( s->init_off == 0 && type == SSL3_RT_HANDSHAKE) |
OPENSSL_assert(s->init_num == |
(int)s->d1->w_msg_hdr.msg_len + DTLS1_HM_HEADER_LENGTH); |
if (s->write_hash) |
- mac_size = EVP_MD_size(s->write_hash); |
+ mac_size = EVP_MD_CTX_size(s->write_hash); |
else |
mac_size = 0; |
@@ -360,7 +358,7 @@ |
const struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr; |
int xlen; |
- if (frag_off == 0 && s->client_version != DTLS1_BAD_VER) |
+ if (frag_off == 0 && s->version != DTLS1_BAD_VER) |
{ |
/* reconstruct message header is if it |
* is being sent in single fragment */ |
@@ -464,20 +462,9 @@ |
memset(msg_hdr, 0x00, sizeof(struct hm_header_st)); |
- s->d1->handshake_read_seq++; |
- /* we just read a handshake message from the other side: |
- * this means that we don't need to retransmit of the |
- * buffered messages. |
- * XDTLS: may be able clear out this |
- * buffer a little sooner (i.e if an out-of-order |
- * handshake message/record is received at the record |
- * layer. |
- * XDTLS: exception is that the server needs to |
- * know that change cipher spec and finished messages |
- * have been received by the client before clearing this |
- * buffer. this can simply be done by waiting for the |
- * first data segment, but is there a better way? */ |
- dtls1_clear_record_buffer(s); |
+ /* Don't change sequence numbers while listening */ |
+ if (!s->d1->listen) |
+ s->d1->handshake_read_seq++; |
s->init_msg = s->init_buf->data + DTLS1_HM_HEADER_LENGTH; |
return s->init_num; |
@@ -514,7 +501,7 @@ |
{ |
/* msg_len is limited to 2^24, but is effectively checked |
* against max above */ |
- if (!BUF_MEM_grow_clean(s->init_buf,(int)msg_len+DTLS1_HM_HEADER_LENGTH)) |
+ if (!BUF_MEM_grow_clean(s->init_buf,msg_len+DTLS1_HM_HEADER_LENGTH)) |
{ |
SSLerr(SSL_F_DTLS1_PREPROCESS_FRAGMENT,ERR_R_BUF_LIB); |
return SSL_AD_INTERNAL_ERROR; |
@@ -600,7 +587,7 @@ |
hm_fragment *frag = NULL; |
pitem *item = NULL; |
int i = -1, is_complete; |
- PQ_64BIT seq64; |
+ unsigned char seq64be[8]; |
unsigned long frag_len = msg_hdr->frag_len, max_len; |
if ((msg_hdr->frag_off+frag_len) > msg_hdr->msg_len) |
@@ -618,10 +605,10 @@ |
goto err; |
/* Try to find item in queue */ |
- pq_64bit_init(&seq64); |
- pq_64bit_assign_word(&seq64, msg_hdr->seq); |
- item = pqueue_find(s->d1->buffered_messages, seq64); |
- pq_64bit_free(&seq64); |
+ memset(seq64be,0,sizeof(seq64be)); |
+ seq64be[6] = (unsigned char) (msg_hdr->seq>>8); |
+ seq64be[7] = (unsigned char) msg_hdr->seq; |
+ item = pqueue_find(s->d1->buffered_messages, seq64be); |
if (item == NULL) |
{ |
@@ -673,11 +660,11 @@ |
if (item == NULL) |
{ |
- pq_64bit_init(&seq64); |
- pq_64bit_assign_word(&seq64, msg_hdr->seq); |
- item = pitem_new(seq64, frag); |
- pq_64bit_free(&seq64); |
+ memset(seq64be,0,sizeof(seq64be)); |
+ seq64be[6] = (unsigned char)(msg_hdr->seq>>8); |
+ seq64be[7] = (unsigned char)(msg_hdr->seq); |
+ item = pitem_new(seq64be, frag); |
if (item == NULL) |
{ |
goto err; |
@@ -703,17 +690,17 @@ |
int i=-1; |
hm_fragment *frag = NULL; |
pitem *item = NULL; |
- PQ_64BIT seq64; |
+ unsigned char seq64be[8]; |
unsigned long frag_len = msg_hdr->frag_len; |
if ((msg_hdr->frag_off+frag_len) > msg_hdr->msg_len) |
goto err; |
/* Try to find item in queue, to prevent duplicate entries */ |
- pq_64bit_init(&seq64); |
- pq_64bit_assign_word(&seq64, msg_hdr->seq); |
- item = pqueue_find(s->d1->buffered_messages, seq64); |
- pq_64bit_free(&seq64); |
+ memset(seq64be,0,sizeof(seq64be)); |
+ seq64be[6] = (unsigned char) (msg_hdr->seq>>8); |
+ seq64be[7] = (unsigned char) msg_hdr->seq; |
+ item = pqueue_find(s->d1->buffered_messages, seq64be); |
/* If we already have an entry and this one is a fragment, |
* don't discard it and rather try to reassemble it. |
@@ -754,18 +741,18 @@ |
if (frag_len) |
{ |
- /* read the body of the fragment (header has already been read) */ |
+ /* read the body of the fragment (header has already been read */ |
i = s->method->ssl_read_bytes(s,SSL3_RT_HANDSHAKE, |
frag->fragment,frag_len,0); |
if (i<=0 || (unsigned long)i!=frag_len) |
goto err; |
} |
- pq_64bit_init(&seq64); |
- pq_64bit_assign_word(&seq64, msg_hdr->seq); |
+ memset(seq64be,0,sizeof(seq64be)); |
+ seq64be[6] = (unsigned char)(msg_hdr->seq>>8); |
+ seq64be[7] = (unsigned char)(msg_hdr->seq); |
- item = pitem_new(seq64, frag); |
- pq_64bit_free(&seq64); |
+ item = pitem_new(seq64be, frag); |
if ( item == NULL) |
goto err; |
@@ -806,16 +793,24 @@ |
*ok = 0; |
return i; |
} |
- OPENSSL_assert(i == DTLS1_HM_HEADER_LENGTH); |
+ /* Handshake fails if message header is incomplete */ |
+ if (i != DTLS1_HM_HEADER_LENGTH) |
+ { |
+ al=SSL_AD_UNEXPECTED_MESSAGE; |
+ SSLerr(SSL_F_DTLS1_GET_MESSAGE_FRAGMENT,SSL_R_UNEXPECTED_MESSAGE); |
+ goto f_err; |
+ } |
/* parse the message fragment header */ |
dtls1_get_message_header(wire, &msg_hdr); |
/* |
* if this is a future (or stale) message it gets buffered |
- * (or dropped)--no further processing at this time |
+ * (or dropped)--no further processing at this time |
+ * While listening, we accept seq 1 (ClientHello with cookie) |
+ * although we're still expecting seq 0 (ClientHello) |
*/ |
- if ( msg_hdr.seq != s->d1->handshake_read_seq) |
+ if (msg_hdr.seq != s->d1->handshake_read_seq && !(s->d1->listen && msg_hdr.seq == 1)) |
return dtls1_process_out_of_seq_message(s, &msg_hdr, ok); |
len = msg_hdr.msg_len; |
@@ -876,7 +871,12 @@ |
/* XDTLS: an incorrectly formatted fragment should cause the |
* handshake to fail */ |
- OPENSSL_assert(i == (int)frag_len); |
+ if (i != (int)frag_len) |
+ { |
+ al=SSL3_AD_ILLEGAL_PARAMETER; |
+ SSLerr(SSL_F_DTLS1_GET_MESSAGE_FRAGMENT,SSL3_AD_ILLEGAL_PARAMETER); |
+ goto f_err; |
+ } |
*ok = 1; |
@@ -907,8 +907,6 @@ |
p= &(d[DTLS1_HM_HEADER_LENGTH]); |
i=s->method->ssl3_enc->final_finish_mac(s, |
- &(s->s3->finish_dgst1), |
- &(s->s3->finish_dgst2), |
sender,slen,s->s3->tmp.finish_md); |
s->s3->tmp.finish_md_len = i; |
memcpy(p, s->s3->tmp.finish_md, i); |
@@ -973,12 +971,11 @@ |
s->d1->handshake_write_seq = s->d1->next_handshake_write_seq; |
s->init_num=DTLS1_CCS_HEADER_LENGTH; |
- if (s->client_version == DTLS1_BAD_VER) |
- { |
+ if (s->version == DTLS1_BAD_VER) { |
s->d1->next_handshake_write_seq++; |
s2n(s->d1->handshake_write_seq,p); |
s->init_num+=2; |
- } |
+ } |
s->init_off=0; |
@@ -997,21 +994,21 @@ |
static int dtls1_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x) |
{ |
- int n; |
- unsigned char *p; |
+ int n; |
+ unsigned char *p; |
- n=i2d_X509(x,NULL); |
- if (!BUF_MEM_grow_clean(buf,(int)(n+(*l)+3))) |
- { |
- SSLerr(SSL_F_DTLS1_ADD_CERT_TO_BUF,ERR_R_BUF_LIB); |
- return 0; |
- } |
- p=(unsigned char *)&(buf->data[*l]); |
- l2n3(n,p); |
- i2d_X509(x,&p); |
- *l+=n+3; |
+ n=i2d_X509(x,NULL); |
+ if (!BUF_MEM_grow_clean(buf,(int)(n+(*l)+3))) |
+ { |
+ SSLerr(SSL_F_DTLS1_ADD_CERT_TO_BUF,ERR_R_BUF_LIB); |
+ return 0; |
+ } |
+ p=(unsigned char *)&(buf->data[*l]); |
+ l2n3(n,p); |
+ i2d_X509(x,&p); |
+ *l+=n+3; |
- return 1; |
+ return 1; |
} |
unsigned long dtls1_output_cert_chain(SSL *s, X509 *x) |
{ |
@@ -1030,7 +1027,7 @@ |
if (x != NULL) |
{ |
X509_STORE_CTX xs_ctx; |
- |
+ |
if (!X509_STORE_CTX_init(&xs_ctx,s->ctx->cert_store,x,NULL)) |
{ |
SSLerr(SSL_F_DTLS1_OUTPUT_CERT_CHAIN,ERR_R_X509_LIB); |
@@ -1052,7 +1049,7 @@ |
} |
X509_STORE_CTX_cleanup(&xs_ctx); |
} |
- /* Thawte special :-) */ |
+ /* Thawte special :-) */ |
for (i=0; i<sk_X509_num(s->ctx->extra_certs); i++) |
{ |
x=sk_X509_value(s->ctx->extra_certs,i); |
@@ -1156,7 +1153,7 @@ |
{ |
pitem *item; |
hm_fragment *frag; |
- PQ_64BIT seq64; |
+ unsigned char seq64be[8]; |
/* this function is called immediately after a message has |
* been serialized */ |
@@ -1169,7 +1166,7 @@ |
if ( is_ccs) |
{ |
OPENSSL_assert(s->d1->w_msg_hdr.msg_len + |
- DTLS1_CCS_HEADER_LENGTH <= (unsigned int)s->init_num); |
+ ((s->version==DTLS1_VERSION)?DTLS1_CCS_HEADER_LENGTH:3) == (unsigned int)s->init_num); |
} |
else |
{ |
@@ -1190,15 +1187,14 @@ |
frag->msg_header.saved_retransmit_state.compress = s->compress; |
frag->msg_header.saved_retransmit_state.session = s->session; |
frag->msg_header.saved_retransmit_state.epoch = s->d1->w_epoch; |
+ |
+ memset(seq64be,0,sizeof(seq64be)); |
+ seq64be[6] = (unsigned char)(dtls1_get_queue_priority(frag->msg_header.seq, |
+ frag->msg_header.is_ccs)>>8); |
+ seq64be[7] = (unsigned char)(dtls1_get_queue_priority(frag->msg_header.seq, |
+ frag->msg_header.is_ccs)); |
- pq_64bit_init(&seq64); |
- |
- pq_64bit_assign_word(&seq64, |
- dtls1_get_queue_priority(frag->msg_header.seq, |
- frag->msg_header.is_ccs)); |
- |
- item = pitem_new(seq64, frag); |
- pq_64bit_free(&seq64); |
+ item = pitem_new(seq64be, frag); |
if ( item == NULL) |
{ |
dtls1_hm_fragment_free(frag); |
@@ -1224,7 +1220,7 @@ |
pitem *item; |
hm_fragment *frag ; |
unsigned long header_length; |
- PQ_64BIT seq64; |
+ unsigned char seq64be[8]; |
struct dtls1_retransmit_state saved_state; |
unsigned char save_write_sequence[8]; |
@@ -1234,11 +1230,11 @@ |
*/ |
/* XDTLS: the requested message ought to be found, otherwise error */ |
- pq_64bit_init(&seq64); |
- pq_64bit_assign_word(&seq64, seq); |
+ memset(seq64be,0,sizeof(seq64be)); |
+ seq64be[6] = (unsigned char)(seq>>8); |
+ seq64be[7] = (unsigned char)seq; |
- item = pqueue_find(s->d1->sent_messages, seq64); |
- pq_64bit_free(&seq64); |
+ item = pqueue_find(s->d1->sent_messages, seq64be); |
if ( item == NULL) |
{ |
fprintf(stderr, "retransmit: message %d non-existant\n", seq); |
@@ -1326,7 +1322,8 @@ |
dtls1_set_message_header(SSL *s, unsigned char *p, unsigned char mt, |
unsigned long len, unsigned long frag_off, unsigned long frag_len) |
{ |
- if ( frag_off == 0) |
+ /* Don't change sequence numbers while listening */ |
+ if (frag_off == 0 && !s->d1->listen) |
{ |
s->d1->handshake_write_seq = s->d1->next_handshake_write_seq; |
s->d1->next_handshake_write_seq++; |
@@ -1379,7 +1376,7 @@ |
return p; |
} |
-static unsigned int |
+unsigned int |
dtls1_min_mtu(void) |
{ |
return (g_probable_mtu[(sizeof(g_probable_mtu) / |
@@ -1389,7 +1386,7 @@ |
static unsigned int |
dtls1_guess_mtu(unsigned int curr_mtu) |
{ |
- size_t i; |
+ unsigned int i; |
if ( curr_mtu == 0 ) |
return g_probable_mtu[0] ; |