summaryrefslogtreecommitdiff
path: root/src/lib/libssl/s3_pkt.c
diff options
context:
space:
mode:
authordjm <>2010-10-01 22:54:21 +0000
committerdjm <>2010-10-01 22:54:21 +0000
commit829fd51d4f8dde4a7f3bf54754f3c1d1a502f5e2 (patch)
treee03b9f1bd051e844b971936729e9df549a209130 /src/lib/libssl/s3_pkt.c
parente6b755d2a53d3cac7a344dfdd6bf7c951cac754c (diff)
downloadopenbsd-829fd51d4f8dde4a7f3bf54754f3c1d1a502f5e2.tar.gz
openbsd-829fd51d4f8dde4a7f3bf54754f3c1d1a502f5e2.tar.bz2
openbsd-829fd51d4f8dde4a7f3bf54754f3c1d1a502f5e2.zip
import OpenSSL-1.0.0a
Diffstat (limited to 'src/lib/libssl/s3_pkt.c')
-rw-r--r--src/lib/libssl/s3_pkt.c282
1 files changed, 207 insertions, 75 deletions
diff --git a/src/lib/libssl/s3_pkt.c b/src/lib/libssl/s3_pkt.c
index 9476dcddf6..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)
@@ -282,9 +336,9 @@ again:
282 if (version != s->version) 336 if (version != s->version)
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 /* Send back error using their 339 if ((s->version & 0xFF00) == (version & 0xFF00))
286 * version number :-) */ 340 /* Send back error using their minor version number :-) */
287 s->version=version; 341 s->version = (unsigned short)version;
288 al=SSL_AD_PROTOCOL_VERSION; 342 al=SSL_AD_PROTOCOL_VERSION;
289 goto f_err; 343 goto f_err;
290 } 344 }
@@ -296,7 +350,7 @@ again:
296 goto err; 350 goto err;
297 } 351 }
298 352
299 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH+extra) 353 if (rr->length > s->s3->rbuf.len - SSL3_RT_HEADER_LENGTH)
300 { 354 {
301 al=SSL_AD_RECORD_OVERFLOW; 355 al=SSL_AD_RECORD_OVERFLOW;
302 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);
@@ -369,12 +423,14 @@ printf("\n");
369 /* r->length is now the compressed data plus mac */ 423 /* r->length is now the compressed data plus mac */
370 if ( (sess == NULL) || 424 if ( (sess == NULL) ||
371 (s->enc_read_ctx == NULL) || 425 (s->enc_read_ctx == NULL) ||
372 (s->read_hash == NULL)) 426 (EVP_MD_CTX_md(s->read_hash) == NULL))
373 clear=1; 427 clear=1;
374 428
375 if (!clear) 429 if (!clear)
376 { 430 {
377 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);
378 434
379 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH+extra+mac_size) 435 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH+extra+mac_size)
380 { 436 {
@@ -387,7 +443,7 @@ printf("\n");
387#endif 443#endif
388 } 444 }
389 /* 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) */
390 if (rr->length >= mac_size) 446 if (rr->length >= (unsigned int)mac_size)
391 { 447 {
392 rr->length -= mac_size; 448 rr->length -= mac_size;
393 mac = &rr->data[rr->length]; 449 mac = &rr->data[rr->length];
@@ -405,7 +461,7 @@ printf("\n");
405#endif 461#endif
406 } 462 }
407 i=s->method->ssl3_enc->mac(s,md,0); 463 i=s->method->ssl3_enc->mac(s,md,0);
408 if (mac == NULL || memcmp(md, mac, mac_size) != 0) 464 if (i < 0 || mac == NULL || memcmp(md, mac, (size_t)mac_size) != 0)
409 { 465 {
410 decryption_failed_or_bad_record_mac = 1; 466 decryption_failed_or_bad_record_mac = 1;
411 } 467 }
@@ -462,6 +518,10 @@ printf("\n");
462 /* just read a 0 length packet */ 518 /* just read a 0 length packet */
463 if (rr->length == 0) goto again; 519 if (rr->length == 0) goto again;
464 520
521#if 0
522fprintf(stderr, "Ultimate Record type=%d, Length=%d\n", rr->type, rr->length);
523#endif
524
465 return(1); 525 return(1);
466 526
467f_err: 527f_err:
@@ -535,8 +595,8 @@ int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
535 n=(len-tot); 595 n=(len-tot);
536 for (;;) 596 for (;;)
537 { 597 {
538 if (n > SSL3_RT_MAX_PLAIN_LENGTH) 598 if (n > s->max_send_fragment)
539 nw=SSL3_RT_MAX_PLAIN_LENGTH; 599 nw=s->max_send_fragment;
540 else 600 else
541 nw=n; 601 nw=n;
542 602
@@ -568,14 +628,19 @@ static int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
568 { 628 {
569 unsigned char *p,*plen; 629 unsigned char *p,*plen;
570 int i,mac_size,clear=0; 630 int i,mac_size,clear=0;
571 int prefix_len = 0; 631 int prefix_len=0;
632 long align=0;
572 SSL3_RECORD *wr; 633 SSL3_RECORD *wr;
573 SSL3_BUFFER *wb; 634 SSL3_BUFFER *wb=&(s->s3->wbuf);
574 SSL_SESSION *sess; 635 SSL_SESSION *sess;
575 636
637 if (wb->buf == NULL)
638 if (!ssl3_setup_write_buffer(s))
639 return -1;
640
576 /* first check if there is a SSL3_BUFFER still being written 641 /* first check if there is a SSL3_BUFFER still being written
577 * out. This will happen with non blocking IO */ 642 * out. This will happen with non blocking IO */
578 if (s->s3->wbuf.left != 0) 643 if (wb->left != 0)
579 return(ssl3_write_pending(s,type,buf,len)); 644 return(ssl3_write_pending(s,type,buf,len));
580 645
581 /* If we have an alert to send, lets send it */ 646 /* If we have an alert to send, lets send it */
@@ -591,18 +656,21 @@ static int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
591 return 0; 656 return 0;
592 657
593 wr= &(s->s3->wrec); 658 wr= &(s->s3->wrec);
594 wb= &(s->s3->wbuf);
595 sess=s->session; 659 sess=s->session;
596 660
597 if ( (sess == NULL) || 661 if ( (sess == NULL) ||
598 (s->enc_write_ctx == NULL) || 662 (s->enc_write_ctx == NULL) ||
599 (s->write_hash == NULL)) 663 (EVP_MD_CTX_md(s->write_hash) == NULL))
600 clear=1; 664 clear=1;
601 665
602 if (clear) 666 if (clear)
603 mac_size=0; 667 mac_size=0;
604 else 668 else
605 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 }
606 674
607 /* 'create_empty_fragment' is true only when this function calls itself */ 675 /* 'create_empty_fragment' is true only when this function calls itself */
608 if (!clear && !create_empty_fragment && !s->s3->empty_fragment_done) 676 if (!clear && !create_empty_fragment && !s->s3->empty_fragment_done)
@@ -620,7 +688,8 @@ static int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
620 if (prefix_len <= 0) 688 if (prefix_len <= 0)
621 goto err; 689 goto err;
622 690
623 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))
624 { 693 {
625 /* insufficient space */ 694 /* insufficient space */
626 SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR); 695 SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);
@@ -631,7 +700,32 @@ static int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
631 s->s3->empty_fragment_done = 1; 700 s->s3->empty_fragment_done = 1;
632 } 701 }
633 702
634 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 }
635 729
636 /* write the header */ 730 /* write the header */
637 731
@@ -674,7 +768,8 @@ static int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
674 768
675 if (mac_size != 0) 769 if (mac_size != 0)
676 { 770 {
677 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;
678 wr->length+=mac_size; 773 wr->length+=mac_size;
679 wr->input=p; 774 wr->input=p;
680 wr->data=p; 775 wr->data=p;
@@ -702,7 +797,6 @@ static int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
702 797
703 /* now let's set up wb */ 798 /* now let's set up wb */
704 wb->left = prefix_len + wr->length; 799 wb->left = prefix_len + wr->length;
705 wb->offset = 0;
706 800
707 /* 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 */
708 s->s3->wpend_tot=len; 802 s->s3->wpend_tot=len;
@@ -721,6 +815,7 @@ int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
721 unsigned int len) 815 unsigned int len)
722 { 816 {
723 int i; 817 int i;
818 SSL3_BUFFER *wb=&(s->s3->wbuf);
724 819
725/* XXXX */ 820/* XXXX */
726 if ((s->s3->wpend_tot > (int)len) 821 if ((s->s3->wpend_tot > (int)len)
@@ -739,17 +834,20 @@ int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
739 { 834 {
740 s->rwstate=SSL_WRITING; 835 s->rwstate=SSL_WRITING;
741 i=BIO_write(s->wbio, 836 i=BIO_write(s->wbio,
742 (char *)&(s->s3->wbuf.buf[s->s3->wbuf.offset]), 837 (char *)&(wb->buf[wb->offset]),
743 (unsigned int)s->s3->wbuf.left); 838 (unsigned int)wb->left);
744 } 839 }
745 else 840 else
746 { 841 {
747 SSLerr(SSL_F_SSL3_WRITE_PENDING,SSL_R_BIO_NOT_SET); 842 SSLerr(SSL_F_SSL3_WRITE_PENDING,SSL_R_BIO_NOT_SET);
748 i= -1; 843 i= -1;
749 } 844 }
750 if (i == s->s3->wbuf.left) 845 if (i == wb->left)
751 { 846 {
752 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);
753 s->rwstate=SSL_NOTHING; 851 s->rwstate=SSL_NOTHING;
754 return(s->s3->wpend_ret); 852 return(s->s3->wpend_ret);
755 } 853 }
@@ -758,12 +856,12 @@ int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
758 s->version == DTLS1_BAD_VER) { 856 s->version == DTLS1_BAD_VER) {
759 /* For DTLS, just drop it. That's kind of the whole 857 /* For DTLS, just drop it. That's kind of the whole
760 point in using a datagram service */ 858 point in using a datagram service */
761 s->s3->wbuf.left = 0; 859 wb->left = 0;
762 } 860 }
763 return(i); 861 return(i);
764 } 862 }
765 s->s3->wbuf.offset+=i; 863 wb->offset+=i;
766 s->s3->wbuf.left-=i; 864 wb->left-=i;
767 } 865 }
768 } 866 }
769 867
@@ -802,7 +900,7 @@ int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
802 void (*cb)(const SSL *ssl,int type2,int val)=NULL; 900 void (*cb)(const SSL *ssl,int type2,int val)=NULL;
803 901
804 if (s->s3->rbuf.buf == NULL) /* Not initialized yet */ 902 if (s->s3->rbuf.buf == NULL) /* Not initialized yet */
805 if (!ssl3_setup_buffers(s)) 903 if (!ssl3_setup_read_buffer(s))
806 return(-1); 904 return(-1);
807 905
808 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) ||
@@ -911,6 +1009,8 @@ start:
911 { 1009 {
912 s->rstate=SSL_ST_READ_HEADER; 1010 s->rstate=SSL_ST_READ_HEADER;
913 rr->off=0; 1011 rr->off=0;
1012 if (s->mode & SSL_MODE_RELEASE_BUFFERS)
1013 ssl3_release_read_buffer(s);
914 } 1014 }
915 } 1015 }
916 return(n); 1016 return(n);
@@ -1020,7 +1120,25 @@ start:
1020 * 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 */
1021 goto start; 1121 goto start;
1022 } 1122 }
1023 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 }
1024 if (s->s3->alert_fragment_len >= 2) 1142 if (s->s3->alert_fragment_len >= 2)
1025 { 1143 {
1026 int alert_level = s->s3->alert_fragment[0]; 1144 int alert_level = s->s3->alert_fragment[0];
@@ -1050,6 +1168,21 @@ start:
1050 s->shutdown |= SSL_RECEIVED_SHUTDOWN; 1168 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
1051 return(0); 1169 return(0);
1052 } 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 }
1053 } 1186 }
1054 else if (alert_level == 2) /* fatal */ 1187 else if (alert_level == 2) /* fatal */
1055 { 1188 {
@@ -1261,20 +1394,18 @@ int ssl3_do_change_cipher_spec(SSL *s)
1261 } 1394 }
1262 1395
1263 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,
1264 &(s->s3->finish_dgst1),
1265 &(s->s3->finish_dgst2),
1266 sender,slen,s->s3->tmp.peer_finish_md); 1397 sender,slen,s->s3->tmp.peer_finish_md);
1267 1398
1268 return(1); 1399 return(1);
1269 } 1400 }
1270 1401
1271void ssl3_send_alert(SSL *s, int level, int desc) 1402int ssl3_send_alert(SSL *s, int level, int desc)
1272 { 1403 {
1273 /* Map tls/ssl alert value to correct one */ 1404 /* Map tls/ssl alert value to correct one */
1274 desc=s->method->ssl3_enc->alert_value(desc); 1405 desc=s->method->ssl3_enc->alert_value(desc);
1275 if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION) 1406 if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION)
1276 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 */
1277 if (desc < 0) return; 1408 if (desc < 0) return -1;
1278 /* If a fatal one, remove from cache */ 1409 /* If a fatal one, remove from cache */
1279 if ((level == 2) && (s->session != NULL)) 1410 if ((level == 2) && (s->session != NULL))
1280 SSL_CTX_remove_session(s->ctx,s->session); 1411 SSL_CTX_remove_session(s->ctx,s->session);
@@ -1283,9 +1414,10 @@ void ssl3_send_alert(SSL *s, int level, int desc)
1283 s->s3->send_alert[0]=level; 1414 s->s3->send_alert[0]=level;
1284 s->s3->send_alert[1]=desc; 1415 s->s3->send_alert[1]=desc;
1285 if (s->s3->wbuf.left == 0) /* data still being written out? */ 1416 if (s->s3->wbuf.left == 0) /* data still being written out? */
1286 s->method->ssl_dispatch_alert(s); 1417 return s->method->ssl_dispatch_alert(s);
1287 /* else data is still being written out, we will get written 1418 /* else data is still being written out, we will get written
1288 * some time in the future */ 1419 * some time in the future */
1420 return -1;
1289 } 1421 }
1290 1422
1291int ssl3_dispatch_alert(SSL *s) 1423int ssl3_dispatch_alert(SSL *s)