1 /* ssl/s3_pkt.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58 /* ====================================================================
59 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 *
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 *
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
71 * distribution.
72 *
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 *
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
82 *
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
86 *
87 * 6. Redistributions of any form whatsoever must retain the following
88 * acknowledgment:
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 *
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
105 *
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
109 *
110 */
111
112 #include <stdio.h>
113 #include <errno.h>
114 #define USE_SOCKETS
115 #include "ssl_locl.h"
116 #include <openssl/evp.h>
117 #include <openssl/buffer.h>
118
119 __RCSID("$MirOS: src/lib/libssl/src/ssl/s3_pkt.c,v 1.9 2014/06/05 13:50:17 tg Exp $");
120
121 static int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
122 unsigned int len, int create_empty_fragment);
123 static int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
124 unsigned int len);
125 static int ssl3_get_record(SSL *s);
126 static int do_compress(SSL *ssl);
127 static int do_uncompress(SSL *ssl);
128 static int do_change_cipher_spec(SSL *ssl);
129
130 /* used only by ssl3_get_record */
ssl3_read_n(SSL * s,int n,int max,int extend)131 static int ssl3_read_n(SSL *s, int n, int max, int extend)
132 {
133 /* If extend == 0, obtain new n-byte packet; if extend == 1, increase
134 * packet by another n bytes.
135 * The packet will be in the sub-array of s->s3->rbuf.buf specified
136 * by s->packet and s->packet_length.
137 * (If s->read_ahead is set, 'max' bytes may be stored in rbuf
138 * [plus s->packet_length bytes if extend == 1].)
139 */
140 int i,off,newb;
141
142 if (!extend)
143 {
144 /* start with empty packet ... */
145 if (s->s3->rbuf.left == 0)
146 s->s3->rbuf.offset = 0;
147 s->packet = s->s3->rbuf.buf + s->s3->rbuf.offset;
148 s->packet_length = 0;
149 /* ... now we can act as if 'extend' was set */
150 }
151
152 /* if there is enough in the buffer from a previous read, take some */
153 if (s->s3->rbuf.left >= (int)n)
154 {
155 s->packet_length+=n;
156 s->s3->rbuf.left-=n;
157 s->s3->rbuf.offset+=n;
158 return(n);
159 }
160
161 /* else we need to read more data */
162 if (!s->read_ahead)
163 max=n;
164
165 {
166 /* avoid buffer overflow */
167 int max_max = s->s3->rbuf.len - s->packet_length;
168 if (max > max_max)
169 max = max_max;
170 }
171 if (n > max) /* does not happen */
172 {
173 SSLerr(SSL_F_SSL3_READ_N,ERR_R_INTERNAL_ERROR);
174 return -1;
175 }
176
177 off = s->packet_length;
178 newb = s->s3->rbuf.left;
179 /* Move any available bytes to front of buffer:
180 * 'off' bytes already pointed to by 'packet',
181 * 'newb' extra ones at the end */
182 if (s->packet != s->s3->rbuf.buf)
183 {
184 /* off > 0 */
185 memmove(s->s3->rbuf.buf, s->packet, off+newb);
186 s->packet = s->s3->rbuf.buf;
187 }
188
189 while (newb < n)
190 {
191 /* Now we have off+newb bytes at the front of s->s3->rbuf.buf and need
192 * to read in more until we have off+n (up to off+max if possible) */
193
194 clear_sys_error();
195 if (s->rbio != NULL)
196 {
197 s->rwstate=SSL_READING;
198 i=BIO_read(s->rbio, &(s->s3->rbuf.buf[off+newb]), max-newb);
199 }
200 else
201 {
202 SSLerr(SSL_F_SSL3_READ_N,SSL_R_READ_BIO_NOT_SET);
203 i = -1;
204 }
205
206 if (i <= 0)
207 {
208 s->s3->rbuf.left = newb;
209 return(i);
210 }
211 newb+=i;
212 }
213
214 /* done reading, now the book-keeping */
215 s->s3->rbuf.offset = off + n;
216 s->s3->rbuf.left = newb - n;
217 s->packet_length += n;
218 s->rwstate=SSL_NOTHING;
219 return(n);
220 }
221
222 /* Call this to get a new input record.
223 * It will return <= 0 if more data is needed, normally due to an error
224 * or non-blocking IO.
225 * When it finishes, one packet has been decoded and can be found in
226 * ssl->s3->rrec.type - is the type of record
227 * ssl->s3->rrec.data, - data
228 * ssl->s3->rrec.length, - number of bytes
229 */
230 /* used only by ssl3_read_bytes */
ssl3_get_record(SSL * s)231 static int ssl3_get_record(SSL *s)
232 {
233 int ssl_major,ssl_minor,al;
234 int enc_err,n,i,ret= -1;
235 SSL3_RECORD *rr;
236 SSL_SESSION *sess;
237 unsigned char *p;
238 unsigned char md[EVP_MAX_MD_SIZE];
239 short version;
240 unsigned int mac_size;
241 int clear=0;
242 size_t extra;
243 int decryption_failed_or_bad_record_mac = 0;
244 unsigned char *mac = NULL;
245
246 rr= &(s->s3->rrec);
247 sess=s->session;
248
249 if (s->options & SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER)
250 extra=SSL3_RT_MAX_EXTRA;
251 else
252 extra=0;
253 if (extra != s->s3->rbuf.len - SSL3_RT_MAX_PACKET_SIZE)
254 {
255 /* actually likely an application error: SLS_OP_MICROSOFT_BIG_SSLV3_BUFFER
256 * set after ssl3_setup_buffers() was done */
257 SSLerr(SSL_F_SSL3_GET_RECORD, ERR_R_INTERNAL_ERROR);
258 return -1;
259 }
260
261 again:
262 /* check if we have the header */
263 if ( (s->rstate != SSL_ST_READ_BODY) ||
264 (s->packet_length < SSL3_RT_HEADER_LENGTH))
265 {
266 n=ssl3_read_n(s, SSL3_RT_HEADER_LENGTH, s->s3->rbuf.len, 0);
267 if (n <= 0) return(n); /* error or non-blocking */
268 s->rstate=SSL_ST_READ_BODY;
269
270 p=s->packet;
271
272 /* Pull apart the header into the SSL3_RECORD */
273 rr->type= *(p++);
274 ssl_major= *(p++);
275 ssl_minor= *(p++);
276 version=(ssl_major<<8)|ssl_minor;
277 n2s(p,rr->length);
278
279 /* Lets check version */
280 if (!s->first_packet)
281 {
282 if (version != s->version)
283 {
284 SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_WRONG_VERSION_NUMBER);
285 if ((s->version & 0xFF00) == (version & 0xFF00))
286 /* Send back error using their minor version number :-) */
287 s->version = (unsigned short)version;
288 al=SSL_AD_PROTOCOL_VERSION;
289 goto f_err;
290 }
291 }
292
293 if ((version>>8) != SSL3_VERSION_MAJOR)
294 {
295 SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_WRONG_VERSION_NUMBER);
296 goto err;
297 }
298
299 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH+extra)
300 {
301 al=SSL_AD_RECORD_OVERFLOW;
302 SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_PACKET_LENGTH_TOO_LONG);
303 goto f_err;
304 }
305
306 /* now s->rstate == SSL_ST_READ_BODY */
307 }
308
309 /* s->rstate == SSL_ST_READ_BODY, get and decode the data */
310
311 if (rr->length > s->packet_length-SSL3_RT_HEADER_LENGTH)
312 {
313 /* now s->packet_length == SSL3_RT_HEADER_LENGTH */
314 i=rr->length;
315 n=ssl3_read_n(s,i,i,1);
316 if (n <= 0) return(n); /* error or non-blocking io */
317 /* now n == rr->length,
318 * and s->packet_length == SSL3_RT_HEADER_LENGTH + rr->length */
319 }
320
321 s->rstate=SSL_ST_READ_HEADER; /* set state for later operations */
322
323 /* At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length,
324 * and we have that many bytes in s->packet
325 */
326 rr->input= &(s->packet[SSL3_RT_HEADER_LENGTH]);
327
328 /* ok, we can now read from 's->packet' data into 'rr'
329 * rr->input points at rr->length bytes, which
330 * need to be copied into rr->data by either
331 * the decryption or by the decompression
332 * When the data is 'copied' into the rr->data buffer,
333 * rr->input will be pointed at the new buffer */
334
335 /* We now have - encrypted [ MAC [ compressed [ plain ] ] ]
336 * rr->length bytes of encrypted compressed stuff. */
337
338 /* check is not needed I believe */
339 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH+extra)
340 {
341 al=SSL_AD_RECORD_OVERFLOW;
342 SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
343 goto f_err;
344 }
345
346 /* decrypt in place in 'rr->input' */
347 rr->data=rr->input;
348
349 enc_err = s->method->ssl3_enc->enc(s,0);
350 if (enc_err <= 0)
351 {
352 if (enc_err == 0)
353 /* SSLerr() and ssl3_send_alert() have been called */
354 goto err;
355
356 /* Otherwise enc_err == -1, which indicates bad padding
357 * (rec->length has not been changed in this case).
358 * To minimize information leaked via timing, we will perform
359 * the MAC computation anyway. */
360 decryption_failed_or_bad_record_mac = 1;
361 }
362
363 #ifdef TLS_DEBUG
364 printf("dec %d\n",rr->length);
365 { unsigned int z; for (z=0; z<rr->length; z++) printf("%02X%c",rr->data[z],((z+1)%16)?' ':'\n'); }
366 printf("\n");
367 #endif
368
369 /* r->length is now the compressed data plus mac */
370 if ( (sess == NULL) ||
371 (s->enc_read_ctx == NULL) ||
372 (s->read_hash == NULL))
373 clear=1;
374
375 if (!clear)
376 {
377 mac_size=EVP_MD_size(s->read_hash);
378
379 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH+extra+mac_size)
380 {
381 #if 0 /* OK only for stream ciphers (then rr->length is visible from ciphertext anyway) */
382 al=SSL_AD_RECORD_OVERFLOW;
383 SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_PRE_MAC_LENGTH_TOO_LONG);
384 goto f_err;
385 #else
386 decryption_failed_or_bad_record_mac = 1;
387 #endif
388 }
389 /* check the MAC for rr->input (it's in mac_size bytes at the tail) */
390 if (rr->length >= mac_size)
391 {
392 rr->length -= mac_size;
393 mac = &rr->data[rr->length];
394 }
395 else
396 {
397 /* record (minus padding) is too short to contain a MAC */
398 #if 0 /* OK only for stream ciphers */
399 al=SSL_AD_DECODE_ERROR;
400 SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_LENGTH_TOO_SHORT);
401 goto f_err;
402 #else
403 decryption_failed_or_bad_record_mac = 1;
404 rr->length = 0;
405 #endif
406 }
407 i=s->method->ssl3_enc->mac(s,md,0);
408 if (mac == NULL || memcmp(md, mac, mac_size) != 0)
409 {
410 decryption_failed_or_bad_record_mac = 1;
411 }
412 }
413
414 if (decryption_failed_or_bad_record_mac)
415 {
416 /* A separate 'decryption_failed' alert was introduced with TLS 1.0,
417 * SSL 3.0 only has 'bad_record_mac'. But unless a decryption
418 * failure is directly visible from the ciphertext anyway,
419 * we should not reveal which kind of error occured -- this
420 * might become visible to an attacker (e.g. via a logfile) */
421 al=SSL_AD_BAD_RECORD_MAC;
422 SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
423 goto f_err;
424 }
425
426 /* r->length is now just compressed */
427 if (s->expand != NULL)
428 {
429 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH+extra)
430 {
431 al=SSL_AD_RECORD_OVERFLOW;
432 SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_COMPRESSED_LENGTH_TOO_LONG);
433 goto f_err;
434 }
435 if (!do_uncompress(s))
436 {
437 al=SSL_AD_DECOMPRESSION_FAILURE;
438 SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_BAD_DECOMPRESSION);
439 goto f_err;
440 }
441 }
442
443 if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH+extra)
444 {
445 al=SSL_AD_RECORD_OVERFLOW;
446 SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_DATA_LENGTH_TOO_LONG);
447 goto f_err;
448 }
449
450 rr->off=0;
451 /* So at this point the following is true
452 * ssl->s3->rrec.type is the type of record
453 * ssl->s3->rrec.length == number of bytes in record
454 * ssl->s3->rrec.off == offset to first valid byte
455 * ssl->s3->rrec.data == where to take bytes from, increment
456 * after use :-).
457 */
458
459 /* we have pulled in a full packet so zero things */
460 s->packet_length=0;
461
462 /* just read a 0 length packet */
463 if (rr->length == 0) goto again;
464
465 return(1);
466
467 f_err:
468 ssl3_send_alert(s,SSL3_AL_FATAL,al);
469 err:
470 return(ret);
471 }
472
do_uncompress(SSL * ssl)473 static int do_uncompress(SSL *ssl)
474 {
475 int i;
476 SSL3_RECORD *rr;
477
478 rr= &(ssl->s3->rrec);
479 i=COMP_expand_block(ssl->expand,rr->comp,
480 SSL3_RT_MAX_PLAIN_LENGTH,rr->data,(int)rr->length);
481 if (i < 0)
482 return(0);
483 else
484 rr->length=i;
485 rr->data=rr->comp;
486
487 return(1);
488 }
489
do_compress(SSL * ssl)490 static int do_compress(SSL *ssl)
491 {
492 int i;
493 SSL3_RECORD *wr;
494
495 wr= &(ssl->s3->wrec);
496 i=COMP_compress_block(ssl->compress,wr->data,
497 SSL3_RT_MAX_COMPRESSED_LENGTH,
498 wr->input,(int)wr->length);
499 if (i < 0)
500 return(0);
501 else
502 wr->length=i;
503
504 wr->input=wr->data;
505 return(1);
506 }
507
508 /* Call this to write data in records of type 'type'
509 * It will return <= 0 if not all data has been sent or non-blocking IO.
510 */
ssl3_write_bytes(SSL * s,int type,const void * buf_,int len)511 int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
512 {
513 const unsigned char *buf=buf_;
514 unsigned int tot,n,nw;
515 int i;
516
517 s->rwstate=SSL_NOTHING;
518 tot=s->s3->wnum;
519 s->s3->wnum=0;
520
521 if (SSL_in_init(s) && !s->in_handshake)
522 {
523 i=s->handshake_func(s);
524 if (i < 0) return(i);
525 if (i == 0)
526 {
527 SSLerr(SSL_F_SSL3_WRITE_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);
528 return -1;
529 }
530 }
531
532 /* ensure that if we end up with a smaller value of data to write
533 * out than the the original len from a write which didn't complete
534 * for non-blocking I/O and also somehow ended up avoiding
535 * the check for this in ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as
536 * it must never be possible to end up with (len-tot) as a large
537 * number that will then promptly send beyond the end of the users
538 * buffer ... so we trap and report the error in a way the user
539 * will notice
540 */
541 if ( len < tot)
542 {
543 SSLerr(SSL_F_SSL3_WRITE_BYTES,SSL_R_BAD_LENGTH);
544 return(-1);
545 }
546
547
548 n=(len-tot);
549 for (;;)
550 {
551 if (n > SSL3_RT_MAX_PLAIN_LENGTH)
552 nw=SSL3_RT_MAX_PLAIN_LENGTH;
553 else
554 nw=n;
555
556 i=do_ssl3_write(s, type, &(buf[tot]), nw, 0);
557 if (i <= 0)
558 {
559 s->s3->wnum=tot;
560 return i;
561 }
562
563 if ((i == (int)n) ||
564 (type == SSL3_RT_APPLICATION_DATA &&
565 (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE)))
566 {
567 /* next chunk of data should get another prepended empty fragment
568 * in ciphersuites with known-IV weakness: */
569 s->s3->empty_fragment_done = 0;
570
571 return tot+i;
572 }
573
574 n-=i;
575 tot+=i;
576 }
577 }
578
579 extern int ssl3_setup_write_buffer(SSL *);
580
do_ssl3_write(SSL * s,int type,const unsigned char * buf,unsigned int len,int create_empty_fragment)581 static int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
582 unsigned int len, int create_empty_fragment)
583 {
584 unsigned char *p,*plen;
585 int i,mac_size,clear=0;
586 int prefix_len = 0;
587 SSL3_RECORD *wr;
588 SSL3_BUFFER *wb;
589 SSL_SESSION *sess;
590
591 /* first check if there is a SSL3_BUFFER still being written
592 * out. This will happen with non blocking IO */
593 if (s->s3->wbuf.left != 0)
594 return(ssl3_write_pending(s,type,buf,len));
595
596 /* If we have an alert to send, lets send it */
597 if (s->s3->alert_dispatch)
598 {
599 i=ssl3_dispatch_alert(s);
600 if (i <= 0)
601 return(i);
602 /* if it went, fall through and send more stuff */
603 }
604
605 /* we may have released our buffer, so get it again */
606 if (s->s3->wbuf.buf == NULL)
607 if (!ssl3_setup_write_buffer(s))
608 return (-1);
609
610 if (len == 0 && !create_empty_fragment)
611 return 0;
612
613 wr= &(s->s3->wrec);
614 wb= &(s->s3->wbuf);
615 sess=s->session;
616
617 if ( (sess == NULL) ||
618 (s->enc_write_ctx == NULL) ||
619 (s->write_hash == NULL))
620 clear=1;
621
622 if (clear)
623 mac_size=0;
624 else
625 mac_size=EVP_MD_size(s->write_hash);
626
627 /* 'create_empty_fragment' is true only when this function calls itself */
628 if (!clear && !create_empty_fragment && !s->s3->empty_fragment_done)
629 {
630 /* countermeasure against known-IV weakness in CBC ciphersuites
631 * (see http://www.openssl.org/~bodo/tls-cbc.txt) */
632
633 if (s->s3->need_empty_fragments && type == SSL3_RT_APPLICATION_DATA)
634 {
635 /* recursive function call with 'create_empty_fragment' set;
636 * this prepares and buffers the data for an empty fragment
637 * (these 'prefix_len' bytes are sent out later
638 * together with the actual payload) */
639 prefix_len = do_ssl3_write(s, type, buf, 0, 1);
640 if (prefix_len <= 0)
641 goto err;
642
643 if (s->s3->wbuf.len < (size_t)prefix_len + SSL3_RT_MAX_PACKET_SIZE)
644 {
645 /* insufficient space */
646 SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);
647 goto err;
648 }
649 }
650
651 s->s3->empty_fragment_done = 1;
652 }
653
654 p = wb->buf + prefix_len;
655
656 /* write the header */
657
658 *(p++)=type&0xff;
659 wr->type=type;
660
661 *(p++)=(s->version>>8);
662 *(p++)=s->version&0xff;
663
664 /* field where we are to write out packet length */
665 plen=p;
666 p+=2;
667
668 /* lets setup the record stuff. */
669 wr->data=p;
670 wr->length=(int)len;
671 wr->input=(unsigned char *)buf;
672
673 /* we now 'read' from wr->input, wr->length bytes into
674 * wr->data */
675
676 /* first we compress */
677 if (s->compress != NULL)
678 {
679 if (!do_compress(s))
680 {
681 SSLerr(SSL_F_DO_SSL3_WRITE,SSL_R_COMPRESSION_FAILURE);
682 goto err;
683 }
684 }
685 else
686 {
687 memcpy(wr->data,wr->input,wr->length);
688 wr->input=wr->data;
689 }
690
691 /* we should still have the output to wr->data and the input
692 * from wr->input. Length should be wr->length.
693 * wr->data still points in the wb->buf */
694
695 if (mac_size != 0)
696 {
697 s->method->ssl3_enc->mac(s,&(p[wr->length]),1);
698 wr->length+=mac_size;
699 wr->input=p;
700 wr->data=p;
701 }
702
703 /* ssl3_enc can only have an error on read */
704 s->method->ssl3_enc->enc(s,1);
705
706 /* record length after mac and block padding */
707 s2n(wr->length,plen);
708
709 /* we should now have
710 * wr->data pointing to the encrypted data, which is
711 * wr->length long */
712 wr->type=type; /* not needed but helps for debugging */
713 wr->length+=SSL3_RT_HEADER_LENGTH;
714
715 if (create_empty_fragment)
716 {
717 /* we are in a recursive call;
718 * just return the length, don't write out anything here
719 */
720 return wr->length;
721 }
722
723 /* now let's set up wb */
724 wb->left = prefix_len + wr->length;
725 wb->offset = 0;
726
727 /* memorize arguments so that ssl3_write_pending can detect bad write retries later */
728 s->s3->wpend_tot=len;
729 s->s3->wpend_buf=buf;
730 s->s3->wpend_type=type;
731 s->s3->wpend_ret=len;
732
733 /* we now just need to write the buffer */
734 return ssl3_write_pending(s,type,buf,len);
735 err:
736 return -1;
737 }
738
739 /* if s->s3->wbuf.left != 0, we need to call this */
ssl3_write_pending(SSL * s,int type,const unsigned char * buf,unsigned int len)740 static int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
741 unsigned int len)
742 {
743 int i;
744
745 /* XXXX */
746 if ((s->s3->wpend_tot > (int)len)
747 || ((s->s3->wpend_buf != buf) &&
748 !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER))
749 || (s->s3->wpend_type != type))
750 {
751 SSLerr(SSL_F_SSL3_WRITE_PENDING,SSL_R_BAD_WRITE_RETRY);
752 return(-1);
753 }
754
755 for (;;)
756 {
757 clear_sys_error();
758 if (s->wbio != NULL)
759 {
760 s->rwstate=SSL_WRITING;
761 i=BIO_write(s->wbio,
762 (char *)&(s->s3->wbuf.buf[s->s3->wbuf.offset]),
763 (unsigned int)s->s3->wbuf.left);
764 }
765 else
766 {
767 SSLerr(SSL_F_SSL3_WRITE_PENDING,SSL_R_BIO_NOT_SET);
768 i= -1;
769 }
770 if (i == s->s3->wbuf.left)
771 {
772 s->s3->wbuf.left=0;
773 s->rwstate=SSL_NOTHING;
774 return(s->s3->wpend_ret);
775 }
776 else if (i <= 0)
777 return(i);
778 s->s3->wbuf.offset+=i;
779 s->s3->wbuf.left-=i;
780 }
781 }
782
783 /* Return up to 'len' payload bytes received in 'type' records.
784 * 'type' is one of the following:
785 *
786 * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
787 * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
788 * - 0 (during a shutdown, no data has to be returned)
789 *
790 * If we don't have stored data to work from, read a SSL/TLS record first
791 * (possibly multiple records if we still don't have anything to return).
792 *
793 * This function must handle any surprises the peer may have for us, such as
794 * Alert records (e.g. close_notify), ChangeCipherSpec records (not really
795 * a surprise, but handled as if it were), or renegotiation requests.
796 * Also if record payloads contain fragments too small to process, we store
797 * them until there is enough for the respective protocol (the record protocol
798 * may use arbitrary fragmentation and even interleaving):
799 * Change cipher spec protocol
800 * just 1 byte needed, no need for keeping anything stored
801 * Alert protocol
802 * 2 bytes needed (AlertLevel, AlertDescription)
803 * Handshake protocol
804 * 4 bytes needed (HandshakeType, uint24 length) -- we just have
805 * to detect unexpected Client Hello and Hello Request messages
806 * here, anything else is handled by higher layers
807 * Application data protocol
808 * none of our business
809 */
ssl3_read_bytes(SSL * s,int type,unsigned char * buf,int len,int peek)810 int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
811 {
812 int al,i,j,ret;
813 unsigned int n;
814 SSL3_RECORD *rr;
815 void (*cb)(const SSL *ssl,int type2,int val)=NULL;
816
817 if (s->s3->rbuf.buf == NULL) /* Not initialized yet */
818 if (!ssl3_setup_buffers(s))
819 return(-1);
820
821 if ((type && (type != SSL3_RT_APPLICATION_DATA) && (type != SSL3_RT_HANDSHAKE) && type) ||
822 (peek && (type != SSL3_RT_APPLICATION_DATA)))
823 {
824 SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR);
825 return -1;
826 }
827
828 if ((type == SSL3_RT_HANDSHAKE) && (s->s3->handshake_fragment_len > 0))
829 /* (partially) satisfy request from storage */
830 {
831 unsigned char *src = s->s3->handshake_fragment;
832 unsigned char *dst = buf;
833 unsigned int k;
834
835 /* peek == 0 */
836 n = 0;
837 while ((len > 0) && (s->s3->handshake_fragment_len > 0))
838 {
839 *dst++ = *src++;
840 len--; s->s3->handshake_fragment_len--;
841 n++;
842 }
843 /* move any remaining fragment bytes: */
844 for (k = 0; k < s->s3->handshake_fragment_len; k++)
845 s->s3->handshake_fragment[k] = *src++;
846 return n;
847 }
848
849 /* Now s->s3->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE. */
850
851 if (!s->in_handshake && SSL_in_init(s))
852 {
853 /* type == SSL3_RT_APPLICATION_DATA */
854 i=s->handshake_func(s);
855 if (i < 0) return(i);
856 if (i == 0)
857 {
858 SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);
859 return(-1);
860 }
861 }
862 start:
863 s->rwstate=SSL_NOTHING;
864
865 /* s->s3->rrec.type - is the type of record
866 * s->s3->rrec.data, - data
867 * s->s3->rrec.off, - offset into 'data' for next read
868 * s->s3->rrec.length, - number of bytes. */
869 rr = &(s->s3->rrec);
870
871 /* get new packet if necessary */
872 if ((rr->length == 0) || (s->rstate == SSL_ST_READ_BODY))
873 {
874 ret=ssl3_get_record(s);
875 if (ret <= 0) return(ret);
876 }
877
878 /* we now have a packet which can be read and processed */
879
880 if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec,
881 * reset by ssl3_get_finished */
882 && (rr->type != SSL3_RT_HANDSHAKE))
883 {
884 al=SSL_AD_UNEXPECTED_MESSAGE;
885 SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);
886 goto f_err;
887 }
888
889 /* If the other end has shut down, throw anything we read away
890 * (even in 'peek' mode) */
891 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
892 {
893 rr->length=0;
894 s->rwstate=SSL_NOTHING;
895 return(0);
896 }
897
898
899 if (type == rr->type) /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */
900 {
901 /* make sure that we are not getting application data when we
902 * are doing a handshake for the first time */
903 if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&
904 (s->enc_read_ctx == NULL))
905 {
906 al=SSL_AD_UNEXPECTED_MESSAGE;
907 SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_APP_DATA_IN_HANDSHAKE);
908 goto f_err;
909 }
910
911 if (len <= 0) return(len);
912
913 if ((unsigned int)len > rr->length)
914 n = rr->length;
915 else
916 n = (unsigned int)len;
917
918 memcpy(buf,&(rr->data[rr->off]),n);
919 if (!peek)
920 {
921 rr->length-=n;
922 rr->off+=n;
923 if (rr->length == 0)
924 {
925 s->rstate=SSL_ST_READ_HEADER;
926 rr->off=0;
927 }
928 }
929 return(n);
930 }
931
932
933 /* If we get here, then type != rr->type; if we have a handshake
934 * message, then it was unexpected (Hello Request or Client Hello). */
935
936 /* In case of record types for which we have 'fragment' storage,
937 * fill that so that we can process the data at a fixed place.
938 */
939 {
940 unsigned int dest_maxlen = 0;
941 unsigned char *dest = NULL;
942 unsigned int *dest_len = NULL;
943
944 if (rr->type == SSL3_RT_HANDSHAKE)
945 {
946 dest_maxlen = sizeof s->s3->handshake_fragment;
947 dest = s->s3->handshake_fragment;
948 dest_len = &s->s3->handshake_fragment_len;
949 }
950 else if (rr->type == SSL3_RT_ALERT)
951 {
952 dest_maxlen = sizeof s->s3->alert_fragment;
953 dest = s->s3->alert_fragment;
954 dest_len = &s->s3->alert_fragment_len;
955 }
956
957 if (dest_maxlen > 0)
958 {
959 n = dest_maxlen - *dest_len; /* available space in 'dest' */
960 if (rr->length < n)
961 n = rr->length; /* available bytes */
962
963 /* now move 'n' bytes: */
964 while (n-- > 0)
965 {
966 dest[(*dest_len)++] = rr->data[rr->off++];
967 rr->length--;
968 }
969
970 if (*dest_len < dest_maxlen)
971 goto start; /* fragment was too small */
972 }
973 }
974
975 /* s->s3->handshake_fragment_len == 4 iff rr->type == SSL3_RT_HANDSHAKE;
976 * s->s3->alert_fragment_len == 2 iff rr->type == SSL3_RT_ALERT.
977 * (Possibly rr is 'empty' now, i.e. rr->length may be 0.) */
978
979 /* If we are a client, check for an incoming 'Hello Request': */
980 if ((!s->server) &&
981 (s->s3->handshake_fragment_len >= 4) &&
982 (s->s3->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST) &&
983 (s->session != NULL) && (s->session->cipher != NULL))
984 {
985 s->s3->handshake_fragment_len = 0;
986
987 if ((s->s3->handshake_fragment[1] != 0) ||
988 (s->s3->handshake_fragment[2] != 0) ||
989 (s->s3->handshake_fragment[3] != 0))
990 {
991 al=SSL_AD_DECODE_ERROR;
992 SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_BAD_HELLO_REQUEST);
993 goto f_err;
994 }
995
996 if (s->msg_callback)
997 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->s3->handshake_fragment, 4, s, s->msg_callback_arg);
998
999 if (SSL_is_init_finished(s) &&
1000 !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) &&
1001 (s->s3->flags & SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION) &&
1002 !s->s3->renegotiate)
1003 {
1004 ssl3_renegotiate(s);
1005 if (ssl3_renegotiate_check(s))
1006 {
1007 i=s->handshake_func(s);
1008 if (i < 0) return(i);
1009 if (i == 0)
1010 {
1011 SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);
1012 return(-1);
1013 }
1014
1015 if (!(s->mode & SSL_MODE_AUTO_RETRY))
1016 {
1017 if (s->s3->rbuf.left == 0) /* no read-ahead left? */
1018 {
1019 BIO *bio;
1020 /* In the case where we try to read application data,
1021 * but we trigger an SSL handshake, we return -1 with
1022 * the retry option set. Otherwise renegotiation may
1023 * cause nasty problems in the blocking world */
1024 s->rwstate=SSL_READING;
1025 bio=SSL_get_rbio(s);
1026 BIO_clear_retry_flags(bio);
1027 BIO_set_retry_read(bio);
1028 return(-1);
1029 }
1030 }
1031 }
1032 }
1033 /* we either finished a handshake or ignored the request,
1034 * now try again to obtain the (application) data we were asked for */
1035 goto start;
1036 }
1037
1038 if (s->s3->alert_fragment_len >= 2)
1039 {
1040 int alert_level = s->s3->alert_fragment[0];
1041 int alert_descr = s->s3->alert_fragment[1];
1042
1043 s->s3->alert_fragment_len = 0;
1044
1045 if (s->msg_callback)
1046 s->msg_callback(0, s->version, SSL3_RT_ALERT, s->s3->alert_fragment, 2, s, s->msg_callback_arg);
1047
1048 if (s->info_callback != NULL)
1049 cb=s->info_callback;
1050 else if (s->ctx->info_callback != NULL)
1051 cb=s->ctx->info_callback;
1052
1053 if (cb != NULL)
1054 {
1055 j = (alert_level << 8) | alert_descr;
1056 cb(s, SSL_CB_READ_ALERT, j);
1057 }
1058
1059 if (alert_level == 1) /* warning */
1060 {
1061 s->s3->warn_alert = alert_descr;
1062 if (alert_descr == SSL_AD_CLOSE_NOTIFY)
1063 {
1064 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
1065 return(0);
1066 }
1067 }
1068 else if (alert_level == 2) /* fatal */
1069 {
1070 char tmp[16];
1071
1072 s->rwstate=SSL_NOTHING;
1073 s->s3->fatal_alert = alert_descr;
1074 SSLerr(SSL_F_SSL3_READ_BYTES, SSL_AD_REASON_OFFSET + alert_descr);
1075 BIO_snprintf(tmp,sizeof tmp,"%d",alert_descr);
1076 ERR_add_error_data(2,"SSL alert number ",tmp);
1077 s->shutdown|=SSL_RECEIVED_SHUTDOWN;
1078 SSL_CTX_remove_session(s->ctx,s->session);
1079 return(0);
1080 }
1081 else
1082 {
1083 al=SSL_AD_ILLEGAL_PARAMETER;
1084 SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_UNKNOWN_ALERT_TYPE);
1085 goto f_err;
1086 }
1087
1088 goto start;
1089 }
1090
1091 if (s->shutdown & SSL_SENT_SHUTDOWN) /* but we have not received a shutdown */
1092 {
1093 s->rwstate=SSL_NOTHING;
1094 rr->length=0;
1095 return(0);
1096 }
1097
1098 if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC)
1099 {
1100 /* 'Change Cipher Spec' is just a single byte, so we know
1101 * exactly what the record payload has to look like */
1102 if ( (rr->length != 1) || (rr->off != 0) ||
1103 (rr->data[0] != SSL3_MT_CCS))
1104 {
1105 al=SSL_AD_ILLEGAL_PARAMETER;
1106 SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_BAD_CHANGE_CIPHER_SPEC);
1107 goto f_err;
1108 }
1109
1110 /* Check we have a cipher to change to */
1111 if (s->s3->tmp.new_cipher == NULL)
1112 {
1113 al=SSL_AD_UNEXPECTED_MESSAGE;
1114 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY);
1115 goto f_err;
1116 }
1117
1118 if (!(s->s3->flags & SSL3_FLAGS_CCS_OK))
1119 {
1120 al=SSL_AD_UNEXPECTED_MESSAGE;
1121 SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_CCS_RECEIVED_EARLY);
1122 goto f_err;
1123 }
1124
1125 s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
1126
1127 rr->length=0;
1128
1129 if (s->msg_callback)
1130 s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, 1, s, s->msg_callback_arg);
1131
1132 s->s3->change_cipher_spec=1;
1133 if (!do_change_cipher_spec(s))
1134 goto err;
1135 else
1136 goto start;
1137 }
1138
1139 /* Unexpected handshake message (Client Hello, or protocol violation) */
1140 if ((s->s3->handshake_fragment_len >= 4) && !s->in_handshake)
1141 {
1142 if (((s->state&SSL_ST_MASK) == SSL_ST_OK) &&
1143 !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) &&
1144 (s->s3->flags & SSL3_FLAGS_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
1145 {
1146 #if 0 /* worked only because C operator preferences are not as expected (and
1147 * because this is not really needed for clients except for detecting
1148 * protocol violations): */
1149 s->state=SSL_ST_BEFORE|(s->server)
1150 ?SSL_ST_ACCEPT
1151 :SSL_ST_CONNECT;
1152 #else
1153 s->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT;
1154 #endif
1155 s->new_session=1;
1156 }
1157 i=s->handshake_func(s);
1158 if (i < 0) return(i);
1159 if (i == 0)
1160 {
1161 SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);
1162 return(-1);
1163 }
1164
1165 if (!(s->mode & SSL_MODE_AUTO_RETRY))
1166 {
1167 if (s->s3->rbuf.left == 0) /* no read-ahead left? */
1168 {
1169 BIO *bio;
1170 /* In the case where we try to read application data,
1171 * but we trigger an SSL handshake, we return -1 with
1172 * the retry option set. Otherwise renegotiation may
1173 * cause nasty problems in the blocking world */
1174 s->rwstate=SSL_READING;
1175 bio=SSL_get_rbio(s);
1176 BIO_clear_retry_flags(bio);
1177 BIO_set_retry_read(bio);
1178 return(-1);
1179 }
1180 }
1181 goto start;
1182 }
1183
1184 switch (rr->type)
1185 {
1186 default:
1187 #ifndef OPENSSL_NO_TLS
1188 /* TLS just ignores unknown message types */
1189 if (s->version == TLS1_VERSION)
1190 {
1191 rr->length = 0;
1192 goto start;
1193 }
1194 #endif
1195 al=SSL_AD_UNEXPECTED_MESSAGE;
1196 SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_UNEXPECTED_RECORD);
1197 goto f_err;
1198 case SSL3_RT_CHANGE_CIPHER_SPEC:
1199 case SSL3_RT_ALERT:
1200 case SSL3_RT_HANDSHAKE:
1201 /* we already handled all of these, with the possible exception
1202 * of SSL3_RT_HANDSHAKE when s->in_handshake is set, but that
1203 * should not happen when type != rr->type */
1204 al=SSL_AD_UNEXPECTED_MESSAGE;
1205 SSLerr(SSL_F_SSL3_READ_BYTES,ERR_R_INTERNAL_ERROR);
1206 goto f_err;
1207 case SSL3_RT_APPLICATION_DATA:
1208 /* At this point, we were expecting handshake data,
1209 * but have application data. If the library was
1210 * running inside ssl3_read() (i.e. in_read_app_data
1211 * is set) and it makes sense to read application data
1212 * at this point (session renegotiation not yet started),
1213 * we will indulge it.
1214 */
1215 if (s->s3->in_read_app_data &&
1216 (s->s3->total_renegotiations != 0) &&
1217 ((
1218 (s->state & SSL_ST_CONNECT) &&
1219 (s->state >= SSL3_ST_CW_CLNT_HELLO_A) &&
1220 (s->state <= SSL3_ST_CR_SRVR_HELLO_A)
1221 ) || (
1222 (s->state & SSL_ST_ACCEPT) &&
1223 (s->state <= SSL3_ST_SW_HELLO_REQ_A) &&
1224 (s->state >= SSL3_ST_SR_CLNT_HELLO_A)
1225 )
1226 ))
1227 {
1228 s->s3->in_read_app_data=2;
1229 return(-1);
1230 }
1231 else
1232 {
1233 al=SSL_AD_UNEXPECTED_MESSAGE;
1234 SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_UNEXPECTED_RECORD);
1235 goto f_err;
1236 }
1237 }
1238 /* not reached */
1239
1240 f_err:
1241 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1242 err:
1243 return(-1);
1244 }
1245
do_change_cipher_spec(SSL * s)1246 static int do_change_cipher_spec(SSL *s)
1247 {
1248 int i;
1249 const char *sender;
1250 int slen;
1251
1252 if (s->state & SSL_ST_ACCEPT)
1253 i=SSL3_CHANGE_CIPHER_SERVER_READ;
1254 else
1255 i=SSL3_CHANGE_CIPHER_CLIENT_READ;
1256
1257 if (s->s3->tmp.key_block == NULL)
1258 {
1259 if (s->session == NULL || s->session->master_key_length == 0)
1260 {
1261 SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_CCS_RECEIVED_EARLY);
1262 return (0);
1263 }
1264 s->session->cipher=s->s3->tmp.new_cipher;
1265 if (!s->method->ssl3_enc->setup_key_block(s)) return(0);
1266 }
1267
1268 if (!s->method->ssl3_enc->change_cipher_state(s,i))
1269 return(0);
1270
1271 /* we have to record the message digest at
1272 * this point so we can get it before we read
1273 * the finished message */
1274 if (s->state & SSL_ST_CONNECT)
1275 {
1276 sender=s->method->ssl3_enc->server_finished_label;
1277 slen=s->method->ssl3_enc->server_finished_label_len;
1278 }
1279 else
1280 {
1281 sender=s->method->ssl3_enc->client_finished_label;
1282 slen=s->method->ssl3_enc->client_finished_label_len;
1283 }
1284
1285 s->s3->tmp.peer_finish_md_len = s->method->ssl3_enc->final_finish_mac(s,
1286 &(s->s3->finish_dgst1),
1287 &(s->s3->finish_dgst2),
1288 sender,slen,s->s3->tmp.peer_finish_md);
1289
1290 return(1);
1291 }
1292
ssl3_send_alert(SSL * s,int level,int desc)1293 void ssl3_send_alert(SSL *s, int level, int desc)
1294 {
1295 /* Map tls/ssl alert value to correct one */
1296 desc=s->method->ssl3_enc->alert_value(desc);
1297 if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION)
1298 desc = SSL_AD_HANDSHAKE_FAILURE; /* SSL 3.0 does not have protocol_version alerts */
1299 if (desc < 0) return;
1300 /* If a fatal one, remove from cache */
1301 if ((level == 2) && (s->session != NULL))
1302 SSL_CTX_remove_session(s->ctx,s->session);
1303
1304 s->s3->alert_dispatch=1;
1305 s->s3->send_alert[0]=level;
1306 s->s3->send_alert[1]=desc;
1307 if (s->s3->wbuf.left == 0) /* data still being written out? */
1308 ssl3_dispatch_alert(s);
1309 /* else data is still being written out, we will get written
1310 * some time in the future */
1311 }
1312
ssl3_dispatch_alert(SSL * s)1313 int ssl3_dispatch_alert(SSL *s)
1314 {
1315 int i,j;
1316 void (*cb)(const SSL *ssl,int type,int val)=NULL;
1317
1318 s->s3->alert_dispatch=0;
1319 i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], 2, 0);
1320 if (i <= 0)
1321 {
1322 s->s3->alert_dispatch=1;
1323 }
1324 else
1325 {
1326 /* Alert sent to BIO. If it is important, flush it now.
1327 * If the message does not get sent due to non-blocking IO,
1328 * we will not worry too much. */
1329 if (s->s3->send_alert[0] == SSL3_AL_FATAL)
1330 (void)BIO_flush(s->wbio);
1331
1332 if (s->msg_callback)
1333 s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 2, s, s->msg_callback_arg);
1334
1335 if (s->info_callback != NULL)
1336 cb=s->info_callback;
1337 else if (s->ctx->info_callback != NULL)
1338 cb=s->ctx->info_callback;
1339
1340 if (cb != NULL)
1341 {
1342 j=(s->s3->send_alert[0]<<8)|s->s3->send_alert[1];
1343 cb(s,SSL_CB_WRITE_ALERT,j);
1344 }
1345 }
1346 return(i);
1347 }
1348