summaryrefslogtreecommitdiff
path: root/src/lib/libssl/s3_pkt.c
diff options
context:
space:
mode:
authordjm <>2010-10-01 22:59:01 +0000
committerdjm <>2010-10-01 22:59:01 +0000
commitfe047d8b632246cb2db3234a0a4f32e5c318857b (patch)
tree939b752540947d33507b3acc48d76a8bfb7c3dc3 /src/lib/libssl/s3_pkt.c
parent2ea67f4aa254b09ded62e6e14fc893bbe6381579 (diff)
downloadopenbsd-fe047d8b632246cb2db3234a0a4f32e5c318857b.tar.gz
openbsd-fe047d8b632246cb2db3234a0a4f32e5c318857b.tar.bz2
openbsd-fe047d8b632246cb2db3234a0a4f32e5c318857b.zip
resolve conflicts, fix local changes
Diffstat (limited to 'src/lib/libssl/s3_pkt.c')
-rw-r--r--src/lib/libssl/s3_pkt.c283
1 files changed, 206 insertions, 77 deletions
diff --git a/src/lib/libssl/s3_pkt.c b/src/lib/libssl/s3_pkt.c
index 515739c08c..e3f6050a26 100644
--- a/src/lib/libssl/s3_pkt.c
+++ b/src/lib/libssl/s3_pkt.c
@@ -129,73 +129,113 @@ int ssl3_read_n(SSL *s, int n, int max, int extend)
129 * (If s->read_ahead is set, 'max' bytes may be stored in rbuf 129 * (If s->read_ahead is set, 'max' bytes may be stored in rbuf
130 * [plus s->packet_length bytes if extend == 1].) 130 * [plus s->packet_length bytes if extend == 1].)
131 */ 131 */
132 int i,off,newb; 132 int i,len,left;
133 long align=0;
134 unsigned char *pkt;
135 SSL3_BUFFER *rb;
136
137 if (n <= 0) return n;
138
139 rb = &(s->s3->rbuf);
140 if (rb->buf == NULL)
141 if (!ssl3_setup_read_buffer(s))
142 return -1;
143
144 left = rb->left;
145#if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
146 align = (long)rb->buf + SSL3_RT_HEADER_LENGTH;
147 align = (-align)&(SSL3_ALIGN_PAYLOAD-1);
148#endif
133 149
134 if (!extend) 150 if (!extend)
135 { 151 {
136 /* start with empty packet ... */ 152 /* start with empty packet ... */
137 if (s->s3->rbuf.left == 0) 153 if (left == 0)
138 s->s3->rbuf.offset = 0; 154 rb->offset = align;
139 s->packet = s->s3->rbuf.buf + s->s3->rbuf.offset; 155 else if (align != 0 && left >= SSL3_RT_HEADER_LENGTH)
156 {
157 /* check if next packet length is large
158 * enough to justify payload alignment... */
159 pkt = rb->buf + rb->offset;
160 if (pkt[0] == SSL3_RT_APPLICATION_DATA
161 && (pkt[3]<<8|pkt[4]) >= 128)
162 {
163 /* Note that even if packet is corrupted
164 * and its length field is insane, we can
165 * only be led to wrong decision about
166 * whether memmove will occur or not.
167 * Header values has no effect on memmove
168 * arguments and therefore no buffer
169 * overrun can be triggered. */
170 memmove (rb->buf+align,pkt,left);
171 rb->offset = align;
172 }
173 }
174 s->packet = rb->buf + rb->offset;
140 s->packet_length = 0; 175 s->packet_length = 0;
141 /* ... now we can act as if 'extend' was set */ 176 /* ... now we can act as if 'extend' was set */
142 } 177 }
143 178
144 /* extend reads should not span multiple packets for DTLS */ 179 /* For DTLS/UDP reads should not span multiple packets
145 if ( SSL_version(s) == DTLS1_VERSION && 180 * because the read operation returns the whole packet
146 extend) 181 * at once (as long as it fits into the buffer). */
182 if (SSL_version(s) == DTLS1_VERSION || SSL_version(s) == DTLS1_BAD_VER)
147 { 183 {
148 if ( s->s3->rbuf.left > 0 && n > s->s3->rbuf.left) 184 if (left > 0 && n > left)
149 n = s->s3->rbuf.left; 185 n = left;
150 } 186 }
151 187
152 /* if there is enough in the buffer from a previous read, take some */ 188 /* if there is enough in the buffer from a previous read, take some */
153 if (s->s3->rbuf.left >= (int)n) 189 if (left >= n)
154 { 190 {
155 s->packet_length+=n; 191 s->packet_length+=n;
156 s->s3->rbuf.left-=n; 192 rb->left=left-n;
157 s->s3->rbuf.offset+=n; 193 rb->offset+=n;
158 return(n); 194 return(n);
159 } 195 }
160 196
161 /* else we need to read more data */ 197 /* else we need to read more data */
162 if (!s->read_ahead)
163 max=n;
164 198
165 { 199 len = s->packet_length;
166 /* avoid buffer overflow */ 200 pkt = rb->buf+align;
167 int max_max = s->s3->rbuf.len - s->packet_length; 201 /* Move any available bytes to front of buffer:
168 if (max > max_max) 202 * 'len' bytes already pointed to by 'packet',
169 max = max_max; 203 * 'left' extra ones at the end */
170 } 204 if (s->packet != pkt) /* len > 0 */
171 if (n > max) /* does not happen */ 205 {
206 memmove(pkt, s->packet, len+left);
207 s->packet = pkt;
208 rb->offset = len + align;
209 }
210
211 if (n > (int)(rb->len - rb->offset)) /* does not happen */
172 { 212 {
173 SSLerr(SSL_F_SSL3_READ_N,ERR_R_INTERNAL_ERROR); 213 SSLerr(SSL_F_SSL3_READ_N,ERR_R_INTERNAL_ERROR);
174 return -1; 214 return -1;
175 } 215 }
176 216
177 off = s->packet_length; 217 if (!s->read_ahead)
178 newb = s->s3->rbuf.left; 218 /* ignore max parameter */
179 /* Move any available bytes to front of buffer: 219 max = n;
180 * 'off' bytes already pointed to by 'packet', 220 else
181 * 'newb' extra ones at the end */
182 if (s->packet != s->s3->rbuf.buf)
183 { 221 {
184 /* off > 0 */ 222 if (max < n)
185 memmove(s->s3->rbuf.buf, s->packet, off+newb); 223 max = n;
186 s->packet = s->s3->rbuf.buf; 224 if (max > (int)(rb->len - rb->offset))
225 max = rb->len - rb->offset;
187 } 226 }
188 227
189 while (newb < n) 228 while (left < n)
190 { 229 {
191 /* Now we have off+newb bytes at the front of s->s3->rbuf.buf and need 230 /* Now we have len+left bytes at the front of s->s3->rbuf.buf
192 * to read in more until we have off+n (up to off+max if possible) */ 231 * and need to read in more until we have len+n (up to
232 * len+max if possible) */
193 233
194 clear_sys_error(); 234 clear_sys_error();
195 if (s->rbio != NULL) 235 if (s->rbio != NULL)
196 { 236 {
197 s->rwstate=SSL_READING; 237 s->rwstate=SSL_READING;
198 i=BIO_read(s->rbio, &(s->s3->rbuf.buf[off+newb]), max-newb); 238 i=BIO_read(s->rbio,pkt+len+left, max-left);
199 } 239 }
200 else 240 else
201 { 241 {
@@ -205,15 +245,26 @@ int ssl3_read_n(SSL *s, int n, int max, int extend)
205 245
206 if (i <= 0) 246 if (i <= 0)
207 { 247 {
208 s->s3->rbuf.left = newb; 248 rb->left = left;
249 if (s->mode & SSL_MODE_RELEASE_BUFFERS)
250 if (len+left == 0)
251 ssl3_release_read_buffer(s);
209 return(i); 252 return(i);
210 } 253 }
211 newb+=i; 254 left+=i;
255 /* reads should *never* span multiple packets for DTLS because
256 * the underlying transport protocol is message oriented as opposed
257 * to byte oriented as in the TLS case. */
258 if (SSL_version(s) == DTLS1_VERSION || SSL_version(s) == DTLS1_BAD_VER)
259 {
260 if (n > left)
261 n = left; /* makes the while condition false */
262 }
212 } 263 }
213 264
214 /* done reading, now the book-keeping */ 265 /* done reading, now the book-keeping */
215 s->s3->rbuf.offset = off + n; 266 rb->offset += n;
216 s->s3->rbuf.left = newb - n; 267 rb->left = left - n;
217 s->packet_length += n; 268 s->packet_length += n;
218 s->rwstate=SSL_NOTHING; 269 s->rwstate=SSL_NOTHING;
219 return(n); 270 return(n);
@@ -237,7 +288,7 @@ static int ssl3_get_record(SSL *s)
237 unsigned char *p; 288 unsigned char *p;
238 unsigned char md[EVP_MAX_MD_SIZE]; 289 unsigned char md[EVP_MAX_MD_SIZE];
239 short version; 290 short version;
240 unsigned int mac_size; 291 int mac_size;
241 int clear=0; 292 int clear=0;
242 size_t extra; 293 size_t extra;
243 int decryption_failed_or_bad_record_mac = 0; 294 int decryption_failed_or_bad_record_mac = 0;
@@ -250,9 +301,9 @@ static int ssl3_get_record(SSL *s)
250 extra=SSL3_RT_MAX_EXTRA; 301 extra=SSL3_RT_MAX_EXTRA;
251 else 302 else
252 extra=0; 303 extra=0;
253 if (extra != s->s3->rbuf.len - SSL3_RT_MAX_PACKET_SIZE) 304 if (extra && !s->s3->init_extra)
254 { 305 {
255 /* actually likely an application error: SLS_OP_MICROSOFT_BIG_SSLV3_BUFFER 306 /* An application error: SLS_OP_MICROSOFT_BIG_SSLV3_BUFFER
256 * set after ssl3_setup_buffers() was done */ 307 * set after ssl3_setup_buffers() was done */
257 SSLerr(SSL_F_SSL3_GET_RECORD, ERR_R_INTERNAL_ERROR); 308 SSLerr(SSL_F_SSL3_GET_RECORD, ERR_R_INTERNAL_ERROR);
258 return -1; 309 return -1;
@@ -275,6 +326,9 @@ again:
275 ssl_minor= *(p++); 326 ssl_minor= *(p++);
276 version=(ssl_major<<8)|ssl_minor; 327 version=(ssl_major<<8)|ssl_minor;
277 n2s(p,rr->length); 328 n2s(p,rr->length);
329#if 0
330fprintf(stderr, "Record type=%d, Length=%d\n", rr->type, rr->length);
331#endif
278 332
279 /* Lets check version */ 333 /* Lets check version */
280 if (!s->first_packet) 334 if (!s->first_packet)
@@ -283,8 +337,7 @@ again:
283 { 337 {
284 SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_WRONG_VERSION_NUMBER); 338 SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_WRONG_VERSION_NUMBER);
285 if ((s->version & 0xFF00) == (version & 0xFF00)) 339 if ((s->version & 0xFF00) == (version & 0xFF00))
286 /* Send back error using their 340 /* Send back error using their minor version number :-) */
287 * minor version number :-) */
288 s->version = (unsigned short)version; 341 s->version = (unsigned short)version;
289 al=SSL_AD_PROTOCOL_VERSION; 342 al=SSL_AD_PROTOCOL_VERSION;
290 goto f_err; 343 goto f_err;
@@ -297,7 +350,7 @@ again:
297 goto err; 350 goto err;
298 } 351 }
299 352
300 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH+extra) 353 if (rr->length > s->s3->rbuf.len - SSL3_RT_HEADER_LENGTH)
301 { 354 {
302 al=SSL_AD_RECORD_OVERFLOW; 355 al=SSL_AD_RECORD_OVERFLOW;
303 SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_PACKET_LENGTH_TOO_LONG); 356 SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_PACKET_LENGTH_TOO_LONG);
@@ -370,12 +423,14 @@ printf("\n");
370 /* r->length is now the compressed data plus mac */ 423 /* r->length is now the compressed data plus mac */
371 if ( (sess == NULL) || 424 if ( (sess == NULL) ||
372 (s->enc_read_ctx == NULL) || 425 (s->enc_read_ctx == NULL) ||
373 (s->read_hash == NULL)) 426 (EVP_MD_CTX_md(s->read_hash) == NULL))
374 clear=1; 427 clear=1;
375 428
376 if (!clear) 429 if (!clear)
377 { 430 {
378 mac_size=EVP_MD_size(s->read_hash); 431 /* !clear => s->read_hash != NULL => mac_size != -1 */
432 mac_size=EVP_MD_CTX_size(s->read_hash);
433 OPENSSL_assert(mac_size >= 0);
379 434
380 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH+extra+mac_size) 435 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH+extra+mac_size)
381 { 436 {
@@ -388,7 +443,7 @@ printf("\n");
388#endif 443#endif
389 } 444 }
390 /* check the MAC for rr->input (it's in mac_size bytes at the tail) */ 445 /* check the MAC for rr->input (it's in mac_size bytes at the tail) */
391 if (rr->length >= mac_size) 446 if (rr->length >= (unsigned int)mac_size)
392 { 447 {
393 rr->length -= mac_size; 448 rr->length -= mac_size;
394 mac = &rr->data[rr->length]; 449 mac = &rr->data[rr->length];
@@ -406,7 +461,7 @@ printf("\n");
406#endif 461#endif
407 } 462 }
408 i=s->method->ssl3_enc->mac(s,md,0); 463 i=s->method->ssl3_enc->mac(s,md,0);
409 if (mac == NULL || memcmp(md, mac, mac_size) != 0) 464 if (i < 0 || mac == NULL || memcmp(md, mac, (size_t)mac_size) != 0)
410 { 465 {
411 decryption_failed_or_bad_record_mac = 1; 466 decryption_failed_or_bad_record_mac = 1;
412 } 467 }
@@ -463,6 +518,10 @@ printf("\n");
463 /* just read a 0 length packet */ 518 /* just read a 0 length packet */
464 if (rr->length == 0) goto again; 519 if (rr->length == 0) goto again;
465 520
521#if 0
522fprintf(stderr, "Ultimate Record type=%d, Length=%d\n", rr->type, rr->length);
523#endif
524
466 return(1); 525 return(1);
467 526
468f_err: 527f_err:
@@ -536,8 +595,8 @@ int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
536 n=(len-tot); 595 n=(len-tot);
537 for (;;) 596 for (;;)
538 { 597 {
539 if (n > SSL3_RT_MAX_PLAIN_LENGTH) 598 if (n > s->max_send_fragment)
540 nw=SSL3_RT_MAX_PLAIN_LENGTH; 599 nw=s->max_send_fragment;
541 else 600 else
542 nw=n; 601 nw=n;
543 602
@@ -569,14 +628,19 @@ static int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
569 { 628 {
570 unsigned char *p,*plen; 629 unsigned char *p,*plen;
571 int i,mac_size,clear=0; 630 int i,mac_size,clear=0;
572 int prefix_len = 0; 631 int prefix_len=0;
632 long align=0;
573 SSL3_RECORD *wr; 633 SSL3_RECORD *wr;
574 SSL3_BUFFER *wb; 634 SSL3_BUFFER *wb=&(s->s3->wbuf);
575 SSL_SESSION *sess; 635 SSL_SESSION *sess;
576 636
637 if (wb->buf == NULL)
638 if (!ssl3_setup_write_buffer(s))
639 return -1;
640
577 /* first check if there is a SSL3_BUFFER still being written 641 /* first check if there is a SSL3_BUFFER still being written
578 * out. This will happen with non blocking IO */ 642 * out. This will happen with non blocking IO */
579 if (s->s3->wbuf.left != 0) 643 if (wb->left != 0)
580 return(ssl3_write_pending(s,type,buf,len)); 644 return(ssl3_write_pending(s,type,buf,len));
581 645
582 /* If we have an alert to send, lets send it */ 646 /* If we have an alert to send, lets send it */
@@ -592,18 +656,21 @@ static int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
592 return 0; 656 return 0;
593 657
594 wr= &(s->s3->wrec); 658 wr= &(s->s3->wrec);
595 wb= &(s->s3->wbuf);
596 sess=s->session; 659 sess=s->session;
597 660
598 if ( (sess == NULL) || 661 if ( (sess == NULL) ||
599 (s->enc_write_ctx == NULL) || 662 (s->enc_write_ctx == NULL) ||
600 (s->write_hash == NULL)) 663 (EVP_MD_CTX_md(s->write_hash) == NULL))
601 clear=1; 664 clear=1;
602 665
603 if (clear) 666 if (clear)
604 mac_size=0; 667 mac_size=0;
605 else 668 else
606 mac_size=EVP_MD_size(s->write_hash); 669 {
670 mac_size=EVP_MD_CTX_size(s->write_hash);
671 if (mac_size < 0)
672 goto err;
673 }
607 674
608 /* 'create_empty_fragment' is true only when this function calls itself */ 675 /* 'create_empty_fragment' is true only when this function calls itself */
609 if (!clear && !create_empty_fragment && !s->s3->empty_fragment_done) 676 if (!clear && !create_empty_fragment && !s->s3->empty_fragment_done)
@@ -621,7 +688,8 @@ static int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
621 if (prefix_len <= 0) 688 if (prefix_len <= 0)
622 goto err; 689 goto err;
623 690
624 if (s->s3->wbuf.len < (size_t)prefix_len + SSL3_RT_MAX_PACKET_SIZE) 691 if (prefix_len >
692 (SSL3_RT_HEADER_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD))
625 { 693 {
626 /* insufficient space */ 694 /* insufficient space */
627 SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR); 695 SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);
@@ -632,7 +700,32 @@ static int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
632 s->s3->empty_fragment_done = 1; 700 s->s3->empty_fragment_done = 1;
633 } 701 }
634 702
635 p = wb->buf + prefix_len; 703 if (create_empty_fragment)
704 {
705#if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
706 /* extra fragment would be couple of cipher blocks,
707 * which would be multiple of SSL3_ALIGN_PAYLOAD, so
708 * if we want to align the real payload, then we can
709 * just pretent we simply have two headers. */
710 align = (long)wb->buf + 2*SSL3_RT_HEADER_LENGTH;
711 align = (-align)&(SSL3_ALIGN_PAYLOAD-1);
712#endif
713 p = wb->buf + align;
714 wb->offset = align;
715 }
716 else if (prefix_len)
717 {
718 p = wb->buf + wb->offset + prefix_len;
719 }
720 else
721 {
722#if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0
723 align = (long)wb->buf + SSL3_RT_HEADER_LENGTH;
724 align = (-align)&(SSL3_ALIGN_PAYLOAD-1);
725#endif
726 p = wb->buf + align;
727 wb->offset = align;
728 }
636 729
637 /* write the header */ 730 /* write the header */
638 731
@@ -675,7 +768,8 @@ static int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
675 768
676 if (mac_size != 0) 769 if (mac_size != 0)
677 { 770 {
678 s->method->ssl3_enc->mac(s,&(p[wr->length]),1); 771 if (s->method->ssl3_enc->mac(s,&(p[wr->length]),1) < 0)
772 goto err;
679 wr->length+=mac_size; 773 wr->length+=mac_size;
680 wr->input=p; 774 wr->input=p;
681 wr->data=p; 775 wr->data=p;
@@ -703,7 +797,6 @@ static int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
703 797
704 /* now let's set up wb */ 798 /* now let's set up wb */
705 wb->left = prefix_len + wr->length; 799 wb->left = prefix_len + wr->length;
706 wb->offset = 0;
707 800
708 /* memorize arguments so that ssl3_write_pending can detect bad write retries later */ 801 /* memorize arguments so that ssl3_write_pending can detect bad write retries later */
709 s->s3->wpend_tot=len; 802 s->s3->wpend_tot=len;
@@ -722,6 +815,7 @@ int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
722 unsigned int len) 815 unsigned int len)
723 { 816 {
724 int i; 817 int i;
818 SSL3_BUFFER *wb=&(s->s3->wbuf);
725 819
726/* XXXX */ 820/* XXXX */
727 if ((s->s3->wpend_tot > (int)len) 821 if ((s->s3->wpend_tot > (int)len)
@@ -740,17 +834,20 @@ int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
740 { 834 {
741 s->rwstate=SSL_WRITING; 835 s->rwstate=SSL_WRITING;
742 i=BIO_write(s->wbio, 836 i=BIO_write(s->wbio,
743 (char *)&(s->s3->wbuf.buf[s->s3->wbuf.offset]), 837 (char *)&(wb->buf[wb->offset]),
744 (unsigned int)s->s3->wbuf.left); 838 (unsigned int)wb->left);
745 } 839 }
746 else 840 else
747 { 841 {
748 SSLerr(SSL_F_SSL3_WRITE_PENDING,SSL_R_BIO_NOT_SET); 842 SSLerr(SSL_F_SSL3_WRITE_PENDING,SSL_R_BIO_NOT_SET);
749 i= -1; 843 i= -1;
750 } 844 }
751 if (i == s->s3->wbuf.left) 845 if (i == wb->left)
752 { 846 {
753 s->s3->wbuf.left=0; 847 wb->left=0;
848 wb->offset+=i;
849 if (s->mode & SSL_MODE_RELEASE_BUFFERS)
850 ssl3_release_write_buffer(s);
754 s->rwstate=SSL_NOTHING; 851 s->rwstate=SSL_NOTHING;
755 return(s->s3->wpend_ret); 852 return(s->s3->wpend_ret);
756 } 853 }
@@ -759,12 +856,12 @@ int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
759 s->version == DTLS1_BAD_VER) { 856 s->version == DTLS1_BAD_VER) {
760 /* For DTLS, just drop it. That's kind of the whole 857 /* For DTLS, just drop it. That's kind of the whole
761 point in using a datagram service */ 858 point in using a datagram service */
762 s->s3->wbuf.left = 0; 859 wb->left = 0;
763 } 860 }
764 return(i); 861 return(i);
765 } 862 }
766 s->s3->wbuf.offset+=i; 863 wb->offset+=i;
767 s->s3->wbuf.left-=i; 864 wb->left-=i;
768 } 865 }
769 } 866 }
770 867
@@ -803,7 +900,7 @@ int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
803 void (*cb)(const SSL *ssl,int type2,int val)=NULL; 900 void (*cb)(const SSL *ssl,int type2,int val)=NULL;
804 901
805 if (s->s3->rbuf.buf == NULL) /* Not initialized yet */ 902 if (s->s3->rbuf.buf == NULL) /* Not initialized yet */
806 if (!ssl3_setup_buffers(s)) 903 if (!ssl3_setup_read_buffer(s))
807 return(-1); 904 return(-1);
808 905
809 if ((type && (type != SSL3_RT_APPLICATION_DATA) && (type != SSL3_RT_HANDSHAKE) && type) || 906 if ((type && (type != SSL3_RT_APPLICATION_DATA) && (type != SSL3_RT_HANDSHAKE) && type) ||
@@ -912,6 +1009,8 @@ start:
912 { 1009 {
913 s->rstate=SSL_ST_READ_HEADER; 1010 s->rstate=SSL_ST_READ_HEADER;
914 rr->off=0; 1011 rr->off=0;
1012 if (s->mode & SSL_MODE_RELEASE_BUFFERS)
1013 ssl3_release_read_buffer(s);
915 } 1014 }
916 } 1015 }
917 return(n); 1016 return(n);
@@ -986,7 +1085,6 @@ start:
986 1085
987 if (SSL_is_init_finished(s) && 1086 if (SSL_is_init_finished(s) &&
988 !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) && 1087 !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) &&
989 (s->s3->flags & SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION) &&
990 !s->s3->renegotiate) 1088 !s->s3->renegotiate)
991 { 1089 {
992 ssl3_renegotiate(s); 1090 ssl3_renegotiate(s);
@@ -1022,7 +1120,25 @@ start:
1022 * now try again to obtain the (application) data we were asked for */ 1120 * now try again to obtain the (application) data we were asked for */
1023 goto start; 1121 goto start;
1024 } 1122 }
1025 1123 /* If we are a server and get a client hello when renegotiation isn't
1124 * allowed send back a no renegotiation alert and carry on.
1125 * WARNING: experimental code, needs reviewing (steve)
1126 */
1127 if (s->server &&
1128 SSL_is_init_finished(s) &&
1129 !s->s3->send_connection_binding &&
1130 (s->version > SSL3_VERSION) &&
1131 (s->s3->handshake_fragment_len >= 4) &&
1132 (s->s3->handshake_fragment[0] == SSL3_MT_CLIENT_HELLO) &&
1133 (s->session != NULL) && (s->session->cipher != NULL) &&
1134 !(s->ctx->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
1135
1136 {
1137 /*s->s3->handshake_fragment_len = 0;*/
1138 rr->length = 0;
1139 ssl3_send_alert(s,SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION);
1140 goto start;
1141 }
1026 if (s->s3->alert_fragment_len >= 2) 1142 if (s->s3->alert_fragment_len >= 2)
1027 { 1143 {
1028 int alert_level = s->s3->alert_fragment[0]; 1144 int alert_level = s->s3->alert_fragment[0];
@@ -1052,6 +1168,21 @@ start:
1052 s->shutdown |= SSL_RECEIVED_SHUTDOWN; 1168 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
1053 return(0); 1169 return(0);
1054 } 1170 }
1171 /* This is a warning but we receive it if we requested
1172 * renegotiation and the peer denied it. Terminate with
1173 * a fatal alert because if application tried to
1174 * renegotiatie it presumably had a good reason and
1175 * expects it to succeed.
1176 *
1177 * In future we might have a renegotiation where we
1178 * don't care if the peer refused it where we carry on.
1179 */
1180 else if (alert_descr == SSL_AD_NO_RENEGOTIATION)
1181 {
1182 al = SSL_AD_HANDSHAKE_FAILURE;
1183 SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_NO_RENEGOTIATION);
1184 goto f_err;
1185 }
1055 } 1186 }
1056 else if (alert_level == 2) /* fatal */ 1187 else if (alert_level == 2) /* fatal */
1057 { 1188 {
@@ -1119,8 +1250,7 @@ start:
1119 if ((s->s3->handshake_fragment_len >= 4) && !s->in_handshake) 1250 if ((s->s3->handshake_fragment_len >= 4) && !s->in_handshake)
1120 { 1251 {
1121 if (((s->state&SSL_ST_MASK) == SSL_ST_OK) && 1252 if (((s->state&SSL_ST_MASK) == SSL_ST_OK) &&
1122 !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) && 1253 !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS))
1123 (s->s3->flags & SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
1124 { 1254 {
1125#if 0 /* worked only because C operator preferences are not as expected (and 1255#if 0 /* worked only because C operator preferences are not as expected (and
1126 * because this is not really needed for clients except for detecting 1256 * because this is not really needed for clients except for detecting
@@ -1264,20 +1394,18 @@ int ssl3_do_change_cipher_spec(SSL *s)
1264 } 1394 }
1265 1395
1266 s->s3->tmp.peer_finish_md_len = s->method->ssl3_enc->final_finish_mac(s, 1396 s->s3->tmp.peer_finish_md_len = s->method->ssl3_enc->final_finish_mac(s,
1267 &(s->s3->finish_dgst1),
1268 &(s->s3->finish_dgst2),
1269 sender,slen,s->s3->tmp.peer_finish_md); 1397 sender,slen,s->s3->tmp.peer_finish_md);
1270 1398
1271 return(1); 1399 return(1);
1272 } 1400 }
1273 1401
1274void ssl3_send_alert(SSL *s, int level, int desc) 1402int ssl3_send_alert(SSL *s, int level, int desc)
1275 { 1403 {
1276 /* Map tls/ssl alert value to correct one */ 1404 /* Map tls/ssl alert value to correct one */
1277 desc=s->method->ssl3_enc->alert_value(desc); 1405 desc=s->method->ssl3_enc->alert_value(desc);
1278 if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION) 1406 if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION)
1279 desc = SSL_AD_HANDSHAKE_FAILURE; /* SSL 3.0 does not have protocol_version alerts */ 1407 desc = SSL_AD_HANDSHAKE_FAILURE; /* SSL 3.0 does not have protocol_version alerts */
1280 if (desc < 0) return; 1408 if (desc < 0) return -1;
1281 /* If a fatal one, remove from cache */ 1409 /* If a fatal one, remove from cache */
1282 if ((level == 2) && (s->session != NULL)) 1410 if ((level == 2) && (s->session != NULL))
1283 SSL_CTX_remove_session(s->ctx,s->session); 1411 SSL_CTX_remove_session(s->ctx,s->session);
@@ -1286,9 +1414,10 @@ void ssl3_send_alert(SSL *s, int level, int desc)
1286 s->s3->send_alert[0]=level; 1414 s->s3->send_alert[0]=level;
1287 s->s3->send_alert[1]=desc; 1415 s->s3->send_alert[1]=desc;
1288 if (s->s3->wbuf.left == 0) /* data still being written out? */ 1416 if (s->s3->wbuf.left == 0) /* data still being written out? */
1289 s->method->ssl_dispatch_alert(s); 1417 return s->method->ssl_dispatch_alert(s);
1290 /* else data is still being written out, we will get written 1418 /* else data is still being written out, we will get written
1291 * some time in the future */ 1419 * some time in the future */
1420 return -1;
1292 } 1421 }
1293 1422
1294int ssl3_dispatch_alert(SSL *s) 1423int ssl3_dispatch_alert(SSL *s)