10Sstevel@tonic-gate /* ssl/s3_pkt.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * This package is an SSL implementation written
60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate *
160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate * the code are not to be removed.
180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate * as the author of the parts of the library used.
200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate *
230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate * are met:
260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate * must display the following acknowledgement:
330Sstevel@tonic-gate * "This product includes cryptographic software written by
340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate * being used are not cryptographic related :-).
370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate * SUCH DAMAGE.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
550Sstevel@tonic-gate * copied and put under another distribution licence
560Sstevel@tonic-gate * [including the GNU Public Licence.]
570Sstevel@tonic-gate */
580Sstevel@tonic-gate /* ====================================================================
590Sstevel@tonic-gate * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
600Sstevel@tonic-gate *
610Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
620Sstevel@tonic-gate * modification, are permitted provided that the following conditions
630Sstevel@tonic-gate * are met:
640Sstevel@tonic-gate *
650Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
660Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
670Sstevel@tonic-gate *
680Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
690Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in
700Sstevel@tonic-gate * the documentation and/or other materials provided with the
710Sstevel@tonic-gate * distribution.
720Sstevel@tonic-gate *
730Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this
740Sstevel@tonic-gate * software must display the following acknowledgment:
750Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
760Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
770Sstevel@tonic-gate *
780Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
790Sstevel@tonic-gate * endorse or promote products derived from this software without
800Sstevel@tonic-gate * prior written permission. For written permission, please contact
810Sstevel@tonic-gate * openssl-core@openssl.org.
820Sstevel@tonic-gate *
830Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL"
840Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written
850Sstevel@tonic-gate * permission of the OpenSSL Project.
860Sstevel@tonic-gate *
870Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following
880Sstevel@tonic-gate * acknowledgment:
890Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
900Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
910Sstevel@tonic-gate *
920Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
930Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
940Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
950Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
960Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
970Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
980Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
990Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1000Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1010Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1020Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1030Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE.
1040Sstevel@tonic-gate * ====================================================================
1050Sstevel@tonic-gate *
1060Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young
1070Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim
1080Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com).
1090Sstevel@tonic-gate *
1100Sstevel@tonic-gate */
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate #include <stdio.h>
1130Sstevel@tonic-gate #include <errno.h>
1140Sstevel@tonic-gate #define USE_SOCKETS
1150Sstevel@tonic-gate #include "ssl_locl.h"
1160Sstevel@tonic-gate #include <openssl/evp.h>
1170Sstevel@tonic-gate #include <openssl/buffer.h>
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate static int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
1200Sstevel@tonic-gate unsigned int len, int create_empty_fragment);
1210Sstevel@tonic-gate static int ssl3_get_record(SSL *s);
1220Sstevel@tonic-gate
ssl3_read_n(SSL * s,int n,int max,int extend)123*2139Sjp161948 int ssl3_read_n(SSL *s, int n, int max, int extend)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate /* If extend == 0, obtain new n-byte packet; if extend == 1, increase
1260Sstevel@tonic-gate * packet by another n bytes.
1270Sstevel@tonic-gate * The packet will be in the sub-array of s->s3->rbuf.buf specified
1280Sstevel@tonic-gate * by s->packet and s->packet_length.
1290Sstevel@tonic-gate * (If s->read_ahead is set, 'max' bytes may be stored in rbuf
1300Sstevel@tonic-gate * [plus s->packet_length bytes if extend == 1].)
1310Sstevel@tonic-gate */
1320Sstevel@tonic-gate int i,off,newb;
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate if (!extend)
1350Sstevel@tonic-gate {
1360Sstevel@tonic-gate /* start with empty packet ... */
1370Sstevel@tonic-gate if (s->s3->rbuf.left == 0)
1380Sstevel@tonic-gate s->s3->rbuf.offset = 0;
1390Sstevel@tonic-gate s->packet = s->s3->rbuf.buf + s->s3->rbuf.offset;
1400Sstevel@tonic-gate s->packet_length = 0;
1410Sstevel@tonic-gate /* ... now we can act as if 'extend' was set */
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate
144*2139Sjp161948 /* extend reads should not span multiple packets for DTLS */
145*2139Sjp161948 if ( SSL_version(s) == DTLS1_VERSION &&
146*2139Sjp161948 extend)
147*2139Sjp161948 {
148*2139Sjp161948 if ( s->s3->rbuf.left > 0 && n > s->s3->rbuf.left)
149*2139Sjp161948 n = s->s3->rbuf.left;
150*2139Sjp161948 }
151*2139Sjp161948
1520Sstevel@tonic-gate /* if there is enough in the buffer from a previous read, take some */
1530Sstevel@tonic-gate if (s->s3->rbuf.left >= (int)n)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate s->packet_length+=n;
1560Sstevel@tonic-gate s->s3->rbuf.left-=n;
1570Sstevel@tonic-gate s->s3->rbuf.offset+=n;
1580Sstevel@tonic-gate return(n);
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate /* else we need to read more data */
1620Sstevel@tonic-gate if (!s->read_ahead)
1630Sstevel@tonic-gate max=n;
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate {
1660Sstevel@tonic-gate /* avoid buffer overflow */
1670Sstevel@tonic-gate int max_max = s->s3->rbuf.len - s->packet_length;
1680Sstevel@tonic-gate if (max > max_max)
1690Sstevel@tonic-gate max = max_max;
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate if (n > max) /* does not happen */
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate SSLerr(SSL_F_SSL3_READ_N,ERR_R_INTERNAL_ERROR);
1740Sstevel@tonic-gate return -1;
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate off = s->packet_length;
1780Sstevel@tonic-gate newb = s->s3->rbuf.left;
1790Sstevel@tonic-gate /* Move any available bytes to front of buffer:
1800Sstevel@tonic-gate * 'off' bytes already pointed to by 'packet',
1810Sstevel@tonic-gate * 'newb' extra ones at the end */
1820Sstevel@tonic-gate if (s->packet != s->s3->rbuf.buf)
1830Sstevel@tonic-gate {
1840Sstevel@tonic-gate /* off > 0 */
1850Sstevel@tonic-gate memmove(s->s3->rbuf.buf, s->packet, off+newb);
1860Sstevel@tonic-gate s->packet = s->s3->rbuf.buf;
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate while (newb < n)
1900Sstevel@tonic-gate {
1910Sstevel@tonic-gate /* Now we have off+newb bytes at the front of s->s3->rbuf.buf and need
1920Sstevel@tonic-gate * to read in more until we have off+n (up to off+max if possible) */
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate clear_sys_error();
1950Sstevel@tonic-gate if (s->rbio != NULL)
1960Sstevel@tonic-gate {
1970Sstevel@tonic-gate s->rwstate=SSL_READING;
1980Sstevel@tonic-gate i=BIO_read(s->rbio, &(s->s3->rbuf.buf[off+newb]), max-newb);
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate else
2010Sstevel@tonic-gate {
2020Sstevel@tonic-gate SSLerr(SSL_F_SSL3_READ_N,SSL_R_READ_BIO_NOT_SET);
2030Sstevel@tonic-gate i = -1;
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate if (i <= 0)
2070Sstevel@tonic-gate {
2080Sstevel@tonic-gate s->s3->rbuf.left = newb;
2090Sstevel@tonic-gate return(i);
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate newb+=i;
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate /* done reading, now the book-keeping */
2150Sstevel@tonic-gate s->s3->rbuf.offset = off + n;
2160Sstevel@tonic-gate s->s3->rbuf.left = newb - n;
2170Sstevel@tonic-gate s->packet_length += n;
2180Sstevel@tonic-gate s->rwstate=SSL_NOTHING;
2190Sstevel@tonic-gate return(n);
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate /* Call this to get a new input record.
2230Sstevel@tonic-gate * It will return <= 0 if more data is needed, normally due to an error
2240Sstevel@tonic-gate * or non-blocking IO.
2250Sstevel@tonic-gate * When it finishes, one packet has been decoded and can be found in
2260Sstevel@tonic-gate * ssl->s3->rrec.type - is the type of record
2270Sstevel@tonic-gate * ssl->s3->rrec.data, - data
2280Sstevel@tonic-gate * ssl->s3->rrec.length, - number of bytes
2290Sstevel@tonic-gate */
2300Sstevel@tonic-gate /* used only by ssl3_read_bytes */
ssl3_get_record(SSL * s)2310Sstevel@tonic-gate static int ssl3_get_record(SSL *s)
2320Sstevel@tonic-gate {
2330Sstevel@tonic-gate int ssl_major,ssl_minor,al;
2340Sstevel@tonic-gate int enc_err,n,i,ret= -1;
2350Sstevel@tonic-gate SSL3_RECORD *rr;
2360Sstevel@tonic-gate SSL_SESSION *sess;
2370Sstevel@tonic-gate unsigned char *p;
2380Sstevel@tonic-gate unsigned char md[EVP_MAX_MD_SIZE];
2390Sstevel@tonic-gate short version;
2400Sstevel@tonic-gate unsigned int mac_size;
2410Sstevel@tonic-gate int clear=0;
2420Sstevel@tonic-gate size_t extra;
2430Sstevel@tonic-gate int decryption_failed_or_bad_record_mac = 0;
2440Sstevel@tonic-gate unsigned char *mac = NULL;
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate rr= &(s->s3->rrec);
2470Sstevel@tonic-gate sess=s->session;
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate if (s->options & SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER)
2500Sstevel@tonic-gate extra=SSL3_RT_MAX_EXTRA;
2510Sstevel@tonic-gate else
2520Sstevel@tonic-gate extra=0;
2530Sstevel@tonic-gate if (extra != s->s3->rbuf.len - SSL3_RT_MAX_PACKET_SIZE)
2540Sstevel@tonic-gate {
2550Sstevel@tonic-gate /* actually likely an application error: SLS_OP_MICROSOFT_BIG_SSLV3_BUFFER
2560Sstevel@tonic-gate * set after ssl3_setup_buffers() was done */
2570Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_RECORD, ERR_R_INTERNAL_ERROR);
2580Sstevel@tonic-gate return -1;
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate again:
2620Sstevel@tonic-gate /* check if we have the header */
2630Sstevel@tonic-gate if ( (s->rstate != SSL_ST_READ_BODY) ||
2640Sstevel@tonic-gate (s->packet_length < SSL3_RT_HEADER_LENGTH))
2650Sstevel@tonic-gate {
2660Sstevel@tonic-gate n=ssl3_read_n(s, SSL3_RT_HEADER_LENGTH, s->s3->rbuf.len, 0);
2670Sstevel@tonic-gate if (n <= 0) return(n); /* error or non-blocking */
2680Sstevel@tonic-gate s->rstate=SSL_ST_READ_BODY;
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate p=s->packet;
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate /* Pull apart the header into the SSL3_RECORD */
2730Sstevel@tonic-gate rr->type= *(p++);
2740Sstevel@tonic-gate ssl_major= *(p++);
2750Sstevel@tonic-gate ssl_minor= *(p++);
2760Sstevel@tonic-gate version=(ssl_major<<8)|ssl_minor;
2770Sstevel@tonic-gate n2s(p,rr->length);
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate /* Lets check version */
2800Sstevel@tonic-gate if (s->first_packet)
2810Sstevel@tonic-gate {
2820Sstevel@tonic-gate s->first_packet=0;
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate else
2850Sstevel@tonic-gate {
2860Sstevel@tonic-gate if (version != s->version)
2870Sstevel@tonic-gate {
2880Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_WRONG_VERSION_NUMBER);
2890Sstevel@tonic-gate /* Send back error using their
2900Sstevel@tonic-gate * version number :-) */
2910Sstevel@tonic-gate s->version=version;
2920Sstevel@tonic-gate al=SSL_AD_PROTOCOL_VERSION;
2930Sstevel@tonic-gate goto f_err;
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate
2970Sstevel@tonic-gate if ((version>>8) != SSL3_VERSION_MAJOR)
2980Sstevel@tonic-gate {
2990Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_WRONG_VERSION_NUMBER);
3000Sstevel@tonic-gate goto err;
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate
3030Sstevel@tonic-gate if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH+extra)
3040Sstevel@tonic-gate {
3050Sstevel@tonic-gate al=SSL_AD_RECORD_OVERFLOW;
3060Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_PACKET_LENGTH_TOO_LONG);
3070Sstevel@tonic-gate goto f_err;
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate
3100Sstevel@tonic-gate /* now s->rstate == SSL_ST_READ_BODY */
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate
3130Sstevel@tonic-gate /* s->rstate == SSL_ST_READ_BODY, get and decode the data */
3140Sstevel@tonic-gate
3150Sstevel@tonic-gate if (rr->length > s->packet_length-SSL3_RT_HEADER_LENGTH)
3160Sstevel@tonic-gate {
3170Sstevel@tonic-gate /* now s->packet_length == SSL3_RT_HEADER_LENGTH */
3180Sstevel@tonic-gate i=rr->length;
3190Sstevel@tonic-gate n=ssl3_read_n(s,i,i,1);
3200Sstevel@tonic-gate if (n <= 0) return(n); /* error or non-blocking io */
3210Sstevel@tonic-gate /* now n == rr->length,
3220Sstevel@tonic-gate * and s->packet_length == SSL3_RT_HEADER_LENGTH + rr->length */
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate
3250Sstevel@tonic-gate s->rstate=SSL_ST_READ_HEADER; /* set state for later operations */
3260Sstevel@tonic-gate
3270Sstevel@tonic-gate /* At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length,
3280Sstevel@tonic-gate * and we have that many bytes in s->packet
3290Sstevel@tonic-gate */
3300Sstevel@tonic-gate rr->input= &(s->packet[SSL3_RT_HEADER_LENGTH]);
3310Sstevel@tonic-gate
3320Sstevel@tonic-gate /* ok, we can now read from 's->packet' data into 'rr'
3330Sstevel@tonic-gate * rr->input points at rr->length bytes, which
3340Sstevel@tonic-gate * need to be copied into rr->data by either
3350Sstevel@tonic-gate * the decryption or by the decompression
3360Sstevel@tonic-gate * When the data is 'copied' into the rr->data buffer,
3370Sstevel@tonic-gate * rr->input will be pointed at the new buffer */
3380Sstevel@tonic-gate
3390Sstevel@tonic-gate /* We now have - encrypted [ MAC [ compressed [ plain ] ] ]
3400Sstevel@tonic-gate * rr->length bytes of encrypted compressed stuff. */
3410Sstevel@tonic-gate
3420Sstevel@tonic-gate /* check is not needed I believe */
3430Sstevel@tonic-gate if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH+extra)
3440Sstevel@tonic-gate {
3450Sstevel@tonic-gate al=SSL_AD_RECORD_OVERFLOW;
3460Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
3470Sstevel@tonic-gate goto f_err;
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate
3500Sstevel@tonic-gate /* decrypt in place in 'rr->input' */
3510Sstevel@tonic-gate rr->data=rr->input;
3520Sstevel@tonic-gate
3530Sstevel@tonic-gate enc_err = s->method->ssl3_enc->enc(s,0);
3540Sstevel@tonic-gate if (enc_err <= 0)
3550Sstevel@tonic-gate {
3560Sstevel@tonic-gate if (enc_err == 0)
3570Sstevel@tonic-gate /* SSLerr() and ssl3_send_alert() have been called */
3580Sstevel@tonic-gate goto err;
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate /* Otherwise enc_err == -1, which indicates bad padding
3610Sstevel@tonic-gate * (rec->length has not been changed in this case).
3620Sstevel@tonic-gate * To minimize information leaked via timing, we will perform
3630Sstevel@tonic-gate * the MAC computation anyway. */
3640Sstevel@tonic-gate decryption_failed_or_bad_record_mac = 1;
3650Sstevel@tonic-gate }
3660Sstevel@tonic-gate
3670Sstevel@tonic-gate #ifdef TLS_DEBUG
3680Sstevel@tonic-gate printf("dec %d\n",rr->length);
3690Sstevel@tonic-gate { unsigned int z; for (z=0; z<rr->length; z++) printf("%02X%c",rr->data[z],((z+1)%16)?' ':'\n'); }
3700Sstevel@tonic-gate printf("\n");
3710Sstevel@tonic-gate #endif
3720Sstevel@tonic-gate
3730Sstevel@tonic-gate /* r->length is now the compressed data plus mac */
3740Sstevel@tonic-gate if ( (sess == NULL) ||
3750Sstevel@tonic-gate (s->enc_read_ctx == NULL) ||
3760Sstevel@tonic-gate (s->read_hash == NULL))
3770Sstevel@tonic-gate clear=1;
3780Sstevel@tonic-gate
3790Sstevel@tonic-gate if (!clear)
3800Sstevel@tonic-gate {
3810Sstevel@tonic-gate mac_size=EVP_MD_size(s->read_hash);
3820Sstevel@tonic-gate
3830Sstevel@tonic-gate if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH+extra+mac_size)
3840Sstevel@tonic-gate {
3850Sstevel@tonic-gate #if 0 /* OK only for stream ciphers (then rr->length is visible from ciphertext anyway) */
3860Sstevel@tonic-gate al=SSL_AD_RECORD_OVERFLOW;
3870Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_PRE_MAC_LENGTH_TOO_LONG);
3880Sstevel@tonic-gate goto f_err;
3890Sstevel@tonic-gate #else
3900Sstevel@tonic-gate decryption_failed_or_bad_record_mac = 1;
3910Sstevel@tonic-gate #endif
3920Sstevel@tonic-gate }
3930Sstevel@tonic-gate /* check the MAC for rr->input (it's in mac_size bytes at the tail) */
3940Sstevel@tonic-gate if (rr->length >= mac_size)
3950Sstevel@tonic-gate {
3960Sstevel@tonic-gate rr->length -= mac_size;
3970Sstevel@tonic-gate mac = &rr->data[rr->length];
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate else
4000Sstevel@tonic-gate {
4010Sstevel@tonic-gate /* record (minus padding) is too short to contain a MAC */
4020Sstevel@tonic-gate #if 0 /* OK only for stream ciphers */
4030Sstevel@tonic-gate al=SSL_AD_DECODE_ERROR;
4040Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_LENGTH_TOO_SHORT);
4050Sstevel@tonic-gate goto f_err;
4060Sstevel@tonic-gate #else
4070Sstevel@tonic-gate decryption_failed_or_bad_record_mac = 1;
4080Sstevel@tonic-gate rr->length = 0;
4090Sstevel@tonic-gate #endif
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate i=s->method->ssl3_enc->mac(s,md,0);
4120Sstevel@tonic-gate if (mac == NULL || memcmp(md, mac, mac_size) != 0)
4130Sstevel@tonic-gate {
4140Sstevel@tonic-gate decryption_failed_or_bad_record_mac = 1;
4150Sstevel@tonic-gate }
4160Sstevel@tonic-gate }
4170Sstevel@tonic-gate
4180Sstevel@tonic-gate if (decryption_failed_or_bad_record_mac)
4190Sstevel@tonic-gate {
4200Sstevel@tonic-gate /* A separate 'decryption_failed' alert was introduced with TLS 1.0,
4210Sstevel@tonic-gate * SSL 3.0 only has 'bad_record_mac'. But unless a decryption
4220Sstevel@tonic-gate * failure is directly visible from the ciphertext anyway,
4230Sstevel@tonic-gate * we should not reveal which kind of error occured -- this
4240Sstevel@tonic-gate * might become visible to an attacker (e.g. via a logfile) */
4250Sstevel@tonic-gate al=SSL_AD_BAD_RECORD_MAC;
4260Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
4270Sstevel@tonic-gate goto f_err;
4280Sstevel@tonic-gate }
4290Sstevel@tonic-gate
4300Sstevel@tonic-gate /* r->length is now just compressed */
4310Sstevel@tonic-gate if (s->expand != NULL)
4320Sstevel@tonic-gate {
4330Sstevel@tonic-gate if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH+extra)
4340Sstevel@tonic-gate {
4350Sstevel@tonic-gate al=SSL_AD_RECORD_OVERFLOW;
4360Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_COMPRESSED_LENGTH_TOO_LONG);
4370Sstevel@tonic-gate goto f_err;
4380Sstevel@tonic-gate }
439*2139Sjp161948 if (!ssl3_do_uncompress(s))
4400Sstevel@tonic-gate {
4410Sstevel@tonic-gate al=SSL_AD_DECOMPRESSION_FAILURE;
4420Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_BAD_DECOMPRESSION);
4430Sstevel@tonic-gate goto f_err;
4440Sstevel@tonic-gate }
4450Sstevel@tonic-gate }
4460Sstevel@tonic-gate
4470Sstevel@tonic-gate if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH+extra)
4480Sstevel@tonic-gate {
4490Sstevel@tonic-gate al=SSL_AD_RECORD_OVERFLOW;
4500Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_DATA_LENGTH_TOO_LONG);
4510Sstevel@tonic-gate goto f_err;
4520Sstevel@tonic-gate }
4530Sstevel@tonic-gate
4540Sstevel@tonic-gate rr->off=0;
4550Sstevel@tonic-gate /* So at this point the following is true
4560Sstevel@tonic-gate * ssl->s3->rrec.type is the type of record
4570Sstevel@tonic-gate * ssl->s3->rrec.length == number of bytes in record
4580Sstevel@tonic-gate * ssl->s3->rrec.off == offset to first valid byte
4590Sstevel@tonic-gate * ssl->s3->rrec.data == where to take bytes from, increment
4600Sstevel@tonic-gate * after use :-).
4610Sstevel@tonic-gate */
4620Sstevel@tonic-gate
4630Sstevel@tonic-gate /* we have pulled in a full packet so zero things */
4640Sstevel@tonic-gate s->packet_length=0;
4650Sstevel@tonic-gate
4660Sstevel@tonic-gate /* just read a 0 length packet */
4670Sstevel@tonic-gate if (rr->length == 0) goto again;
4680Sstevel@tonic-gate
4690Sstevel@tonic-gate return(1);
4700Sstevel@tonic-gate
4710Sstevel@tonic-gate f_err:
4720Sstevel@tonic-gate ssl3_send_alert(s,SSL3_AL_FATAL,al);
4730Sstevel@tonic-gate err:
4740Sstevel@tonic-gate return(ret);
4750Sstevel@tonic-gate }
4760Sstevel@tonic-gate
ssl3_do_uncompress(SSL * ssl)477*2139Sjp161948 int ssl3_do_uncompress(SSL *ssl)
4780Sstevel@tonic-gate {
479*2139Sjp161948 #ifndef OPENSSL_NO_COMP
4800Sstevel@tonic-gate int i;
4810Sstevel@tonic-gate SSL3_RECORD *rr;
4820Sstevel@tonic-gate
4830Sstevel@tonic-gate rr= &(ssl->s3->rrec);
4840Sstevel@tonic-gate i=COMP_expand_block(ssl->expand,rr->comp,
4850Sstevel@tonic-gate SSL3_RT_MAX_PLAIN_LENGTH,rr->data,(int)rr->length);
4860Sstevel@tonic-gate if (i < 0)
4870Sstevel@tonic-gate return(0);
4880Sstevel@tonic-gate else
4890Sstevel@tonic-gate rr->length=i;
4900Sstevel@tonic-gate rr->data=rr->comp;
491*2139Sjp161948 #endif
4920Sstevel@tonic-gate return(1);
4930Sstevel@tonic-gate }
4940Sstevel@tonic-gate
ssl3_do_compress(SSL * ssl)495*2139Sjp161948 int ssl3_do_compress(SSL *ssl)
4960Sstevel@tonic-gate {
497*2139Sjp161948 #ifndef OPENSSL_NO_COMP
4980Sstevel@tonic-gate int i;
4990Sstevel@tonic-gate SSL3_RECORD *wr;
5000Sstevel@tonic-gate
5010Sstevel@tonic-gate wr= &(ssl->s3->wrec);
5020Sstevel@tonic-gate i=COMP_compress_block(ssl->compress,wr->data,
5030Sstevel@tonic-gate SSL3_RT_MAX_COMPRESSED_LENGTH,
5040Sstevel@tonic-gate wr->input,(int)wr->length);
5050Sstevel@tonic-gate if (i < 0)
5060Sstevel@tonic-gate return(0);
5070Sstevel@tonic-gate else
5080Sstevel@tonic-gate wr->length=i;
5090Sstevel@tonic-gate
5100Sstevel@tonic-gate wr->input=wr->data;
511*2139Sjp161948 #endif
5120Sstevel@tonic-gate return(1);
5130Sstevel@tonic-gate }
5140Sstevel@tonic-gate
5150Sstevel@tonic-gate /* Call this to write data in records of type 'type'
5160Sstevel@tonic-gate * It will return <= 0 if not all data has been sent or non-blocking IO.
5170Sstevel@tonic-gate */
ssl3_write_bytes(SSL * s,int type,const void * buf_,int len)5180Sstevel@tonic-gate int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
5190Sstevel@tonic-gate {
5200Sstevel@tonic-gate const unsigned char *buf=buf_;
5210Sstevel@tonic-gate unsigned int tot,n,nw;
5220Sstevel@tonic-gate int i;
5230Sstevel@tonic-gate
5240Sstevel@tonic-gate s->rwstate=SSL_NOTHING;
5250Sstevel@tonic-gate tot=s->s3->wnum;
5260Sstevel@tonic-gate s->s3->wnum=0;
5270Sstevel@tonic-gate
5280Sstevel@tonic-gate if (SSL_in_init(s) && !s->in_handshake)
5290Sstevel@tonic-gate {
5300Sstevel@tonic-gate i=s->handshake_func(s);
5310Sstevel@tonic-gate if (i < 0) return(i);
5320Sstevel@tonic-gate if (i == 0)
5330Sstevel@tonic-gate {
5340Sstevel@tonic-gate SSLerr(SSL_F_SSL3_WRITE_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);
5350Sstevel@tonic-gate return -1;
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate }
5380Sstevel@tonic-gate
5390Sstevel@tonic-gate n=(len-tot);
5400Sstevel@tonic-gate for (;;)
5410Sstevel@tonic-gate {
5420Sstevel@tonic-gate if (n > SSL3_RT_MAX_PLAIN_LENGTH)
5430Sstevel@tonic-gate nw=SSL3_RT_MAX_PLAIN_LENGTH;
5440Sstevel@tonic-gate else
5450Sstevel@tonic-gate nw=n;
5460Sstevel@tonic-gate
5470Sstevel@tonic-gate i=do_ssl3_write(s, type, &(buf[tot]), nw, 0);
5480Sstevel@tonic-gate if (i <= 0)
5490Sstevel@tonic-gate {
5500Sstevel@tonic-gate s->s3->wnum=tot;
5510Sstevel@tonic-gate return i;
5520Sstevel@tonic-gate }
5530Sstevel@tonic-gate
5540Sstevel@tonic-gate if ((i == (int)n) ||
5550Sstevel@tonic-gate (type == SSL3_RT_APPLICATION_DATA &&
5560Sstevel@tonic-gate (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE)))
5570Sstevel@tonic-gate {
5580Sstevel@tonic-gate /* next chunk of data should get another prepended empty fragment
5590Sstevel@tonic-gate * in ciphersuites with known-IV weakness: */
5600Sstevel@tonic-gate s->s3->empty_fragment_done = 0;
5610Sstevel@tonic-gate
5620Sstevel@tonic-gate return tot+i;
5630Sstevel@tonic-gate }
5640Sstevel@tonic-gate
5650Sstevel@tonic-gate n-=i;
5660Sstevel@tonic-gate tot+=i;
5670Sstevel@tonic-gate }
5680Sstevel@tonic-gate }
5690Sstevel@tonic-gate
do_ssl3_write(SSL * s,int type,const unsigned char * buf,unsigned int len,int create_empty_fragment)5700Sstevel@tonic-gate static int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
5710Sstevel@tonic-gate unsigned int len, int create_empty_fragment)
5720Sstevel@tonic-gate {
5730Sstevel@tonic-gate unsigned char *p,*plen;
5740Sstevel@tonic-gate int i,mac_size,clear=0;
5750Sstevel@tonic-gate int prefix_len = 0;
5760Sstevel@tonic-gate SSL3_RECORD *wr;
5770Sstevel@tonic-gate SSL3_BUFFER *wb;
5780Sstevel@tonic-gate SSL_SESSION *sess;
5790Sstevel@tonic-gate
5800Sstevel@tonic-gate /* first check if there is a SSL3_BUFFER still being written
5810Sstevel@tonic-gate * out. This will happen with non blocking IO */
5820Sstevel@tonic-gate if (s->s3->wbuf.left != 0)
5830Sstevel@tonic-gate return(ssl3_write_pending(s,type,buf,len));
5840Sstevel@tonic-gate
5850Sstevel@tonic-gate /* If we have an alert to send, lets send it */
5860Sstevel@tonic-gate if (s->s3->alert_dispatch)
5870Sstevel@tonic-gate {
588*2139Sjp161948 i=s->method->ssl_dispatch_alert(s);
5890Sstevel@tonic-gate if (i <= 0)
5900Sstevel@tonic-gate return(i);
5910Sstevel@tonic-gate /* if it went, fall through and send more stuff */
5920Sstevel@tonic-gate }
5930Sstevel@tonic-gate
5940Sstevel@tonic-gate if (len == 0 && !create_empty_fragment)
5950Sstevel@tonic-gate return 0;
5960Sstevel@tonic-gate
5970Sstevel@tonic-gate wr= &(s->s3->wrec);
5980Sstevel@tonic-gate wb= &(s->s3->wbuf);
5990Sstevel@tonic-gate sess=s->session;
6000Sstevel@tonic-gate
6010Sstevel@tonic-gate if ( (sess == NULL) ||
6020Sstevel@tonic-gate (s->enc_write_ctx == NULL) ||
6030Sstevel@tonic-gate (s->write_hash == NULL))
6040Sstevel@tonic-gate clear=1;
6050Sstevel@tonic-gate
6060Sstevel@tonic-gate if (clear)
6070Sstevel@tonic-gate mac_size=0;
6080Sstevel@tonic-gate else
6090Sstevel@tonic-gate mac_size=EVP_MD_size(s->write_hash);
6100Sstevel@tonic-gate
6110Sstevel@tonic-gate /* 'create_empty_fragment' is true only when this function calls itself */
6120Sstevel@tonic-gate if (!clear && !create_empty_fragment && !s->s3->empty_fragment_done)
6130Sstevel@tonic-gate {
6140Sstevel@tonic-gate /* countermeasure against known-IV weakness in CBC ciphersuites
6150Sstevel@tonic-gate * (see http://www.openssl.org/~bodo/tls-cbc.txt) */
6160Sstevel@tonic-gate
6170Sstevel@tonic-gate if (s->s3->need_empty_fragments && type == SSL3_RT_APPLICATION_DATA)
6180Sstevel@tonic-gate {
6190Sstevel@tonic-gate /* recursive function call with 'create_empty_fragment' set;
6200Sstevel@tonic-gate * this prepares and buffers the data for an empty fragment
6210Sstevel@tonic-gate * (these 'prefix_len' bytes are sent out later
6220Sstevel@tonic-gate * together with the actual payload) */
6230Sstevel@tonic-gate prefix_len = do_ssl3_write(s, type, buf, 0, 1);
6240Sstevel@tonic-gate if (prefix_len <= 0)
6250Sstevel@tonic-gate goto err;
6260Sstevel@tonic-gate
6270Sstevel@tonic-gate if (s->s3->wbuf.len < (size_t)prefix_len + SSL3_RT_MAX_PACKET_SIZE)
6280Sstevel@tonic-gate {
6290Sstevel@tonic-gate /* insufficient space */
6300Sstevel@tonic-gate SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);
6310Sstevel@tonic-gate goto err;
6320Sstevel@tonic-gate }
6330Sstevel@tonic-gate }
6340Sstevel@tonic-gate
6350Sstevel@tonic-gate s->s3->empty_fragment_done = 1;
6360Sstevel@tonic-gate }
6370Sstevel@tonic-gate
6380Sstevel@tonic-gate p = wb->buf + prefix_len;
6390Sstevel@tonic-gate
6400Sstevel@tonic-gate /* write the header */
6410Sstevel@tonic-gate
6420Sstevel@tonic-gate *(p++)=type&0xff;
6430Sstevel@tonic-gate wr->type=type;
6440Sstevel@tonic-gate
6450Sstevel@tonic-gate *(p++)=(s->version>>8);
6460Sstevel@tonic-gate *(p++)=s->version&0xff;
6470Sstevel@tonic-gate
6480Sstevel@tonic-gate /* field where we are to write out packet length */
6490Sstevel@tonic-gate plen=p;
6500Sstevel@tonic-gate p+=2;
6510Sstevel@tonic-gate
6520Sstevel@tonic-gate /* lets setup the record stuff. */
6530Sstevel@tonic-gate wr->data=p;
6540Sstevel@tonic-gate wr->length=(int)len;
6550Sstevel@tonic-gate wr->input=(unsigned char *)buf;
6560Sstevel@tonic-gate
6570Sstevel@tonic-gate /* we now 'read' from wr->input, wr->length bytes into
6580Sstevel@tonic-gate * wr->data */
6590Sstevel@tonic-gate
6600Sstevel@tonic-gate /* first we compress */
6610Sstevel@tonic-gate if (s->compress != NULL)
6620Sstevel@tonic-gate {
663*2139Sjp161948 if (!ssl3_do_compress(s))
6640Sstevel@tonic-gate {
6650Sstevel@tonic-gate SSLerr(SSL_F_DO_SSL3_WRITE,SSL_R_COMPRESSION_FAILURE);
6660Sstevel@tonic-gate goto err;
6670Sstevel@tonic-gate }
6680Sstevel@tonic-gate }
6690Sstevel@tonic-gate else
6700Sstevel@tonic-gate {
6710Sstevel@tonic-gate memcpy(wr->data,wr->input,wr->length);
6720Sstevel@tonic-gate wr->input=wr->data;
6730Sstevel@tonic-gate }
6740Sstevel@tonic-gate
6750Sstevel@tonic-gate /* we should still have the output to wr->data and the input
6760Sstevel@tonic-gate * from wr->input. Length should be wr->length.
6770Sstevel@tonic-gate * wr->data still points in the wb->buf */
6780Sstevel@tonic-gate
6790Sstevel@tonic-gate if (mac_size != 0)
6800Sstevel@tonic-gate {
6810Sstevel@tonic-gate s->method->ssl3_enc->mac(s,&(p[wr->length]),1);
6820Sstevel@tonic-gate wr->length+=mac_size;
6830Sstevel@tonic-gate wr->input=p;
6840Sstevel@tonic-gate wr->data=p;
6850Sstevel@tonic-gate }
6860Sstevel@tonic-gate
6870Sstevel@tonic-gate /* ssl3_enc can only have an error on read */
6880Sstevel@tonic-gate s->method->ssl3_enc->enc(s,1);
6890Sstevel@tonic-gate
6900Sstevel@tonic-gate /* record length after mac and block padding */
6910Sstevel@tonic-gate s2n(wr->length,plen);
6920Sstevel@tonic-gate
6930Sstevel@tonic-gate /* we should now have
6940Sstevel@tonic-gate * wr->data pointing to the encrypted data, which is
6950Sstevel@tonic-gate * wr->length long */
6960Sstevel@tonic-gate wr->type=type; /* not needed but helps for debugging */
6970Sstevel@tonic-gate wr->length+=SSL3_RT_HEADER_LENGTH;
6980Sstevel@tonic-gate
6990Sstevel@tonic-gate if (create_empty_fragment)
7000Sstevel@tonic-gate {
7010Sstevel@tonic-gate /* we are in a recursive call;
7020Sstevel@tonic-gate * just return the length, don't write out anything here
7030Sstevel@tonic-gate */
7040Sstevel@tonic-gate return wr->length;
7050Sstevel@tonic-gate }
7060Sstevel@tonic-gate
7070Sstevel@tonic-gate /* now let's set up wb */
7080Sstevel@tonic-gate wb->left = prefix_len + wr->length;
7090Sstevel@tonic-gate wb->offset = 0;
7100Sstevel@tonic-gate
7110Sstevel@tonic-gate /* memorize arguments so that ssl3_write_pending can detect bad write retries later */
7120Sstevel@tonic-gate s->s3->wpend_tot=len;
7130Sstevel@tonic-gate s->s3->wpend_buf=buf;
7140Sstevel@tonic-gate s->s3->wpend_type=type;
7150Sstevel@tonic-gate s->s3->wpend_ret=len;
7160Sstevel@tonic-gate
7170Sstevel@tonic-gate /* we now just need to write the buffer */
7180Sstevel@tonic-gate return ssl3_write_pending(s,type,buf,len);
7190Sstevel@tonic-gate err:
7200Sstevel@tonic-gate return -1;
7210Sstevel@tonic-gate }
7220Sstevel@tonic-gate
7230Sstevel@tonic-gate /* 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)724*2139Sjp161948 int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
725*2139Sjp161948 unsigned int len)
7260Sstevel@tonic-gate {
7270Sstevel@tonic-gate int i;
7280Sstevel@tonic-gate
7290Sstevel@tonic-gate /* XXXX */
7300Sstevel@tonic-gate if ((s->s3->wpend_tot > (int)len)
7310Sstevel@tonic-gate || ((s->s3->wpend_buf != buf) &&
7320Sstevel@tonic-gate !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER))
7330Sstevel@tonic-gate || (s->s3->wpend_type != type))
7340Sstevel@tonic-gate {
7350Sstevel@tonic-gate SSLerr(SSL_F_SSL3_WRITE_PENDING,SSL_R_BAD_WRITE_RETRY);
7360Sstevel@tonic-gate return(-1);
7370Sstevel@tonic-gate }
7380Sstevel@tonic-gate
7390Sstevel@tonic-gate for (;;)
7400Sstevel@tonic-gate {
7410Sstevel@tonic-gate clear_sys_error();
7420Sstevel@tonic-gate if (s->wbio != NULL)
7430Sstevel@tonic-gate {
7440Sstevel@tonic-gate s->rwstate=SSL_WRITING;
7450Sstevel@tonic-gate i=BIO_write(s->wbio,
7460Sstevel@tonic-gate (char *)&(s->s3->wbuf.buf[s->s3->wbuf.offset]),
7470Sstevel@tonic-gate (unsigned int)s->s3->wbuf.left);
7480Sstevel@tonic-gate }
7490Sstevel@tonic-gate else
7500Sstevel@tonic-gate {
7510Sstevel@tonic-gate SSLerr(SSL_F_SSL3_WRITE_PENDING,SSL_R_BIO_NOT_SET);
7520Sstevel@tonic-gate i= -1;
7530Sstevel@tonic-gate }
7540Sstevel@tonic-gate if (i == s->s3->wbuf.left)
7550Sstevel@tonic-gate {
7560Sstevel@tonic-gate s->s3->wbuf.left=0;
7570Sstevel@tonic-gate s->rwstate=SSL_NOTHING;
7580Sstevel@tonic-gate return(s->s3->wpend_ret);
7590Sstevel@tonic-gate }
7600Sstevel@tonic-gate else if (i <= 0)
7610Sstevel@tonic-gate return(i);
7620Sstevel@tonic-gate s->s3->wbuf.offset+=i;
7630Sstevel@tonic-gate s->s3->wbuf.left-=i;
7640Sstevel@tonic-gate }
7650Sstevel@tonic-gate }
7660Sstevel@tonic-gate
7670Sstevel@tonic-gate /* Return up to 'len' payload bytes received in 'type' records.
7680Sstevel@tonic-gate * 'type' is one of the following:
7690Sstevel@tonic-gate *
7700Sstevel@tonic-gate * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
7710Sstevel@tonic-gate * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
7720Sstevel@tonic-gate * - 0 (during a shutdown, no data has to be returned)
7730Sstevel@tonic-gate *
7740Sstevel@tonic-gate * If we don't have stored data to work from, read a SSL/TLS record first
7750Sstevel@tonic-gate * (possibly multiple records if we still don't have anything to return).
7760Sstevel@tonic-gate *
7770Sstevel@tonic-gate * This function must handle any surprises the peer may have for us, such as
7780Sstevel@tonic-gate * Alert records (e.g. close_notify), ChangeCipherSpec records (not really
7790Sstevel@tonic-gate * a surprise, but handled as if it were), or renegotiation requests.
7800Sstevel@tonic-gate * Also if record payloads contain fragments too small to process, we store
7810Sstevel@tonic-gate * them until there is enough for the respective protocol (the record protocol
7820Sstevel@tonic-gate * may use arbitrary fragmentation and even interleaving):
7830Sstevel@tonic-gate * Change cipher spec protocol
7840Sstevel@tonic-gate * just 1 byte needed, no need for keeping anything stored
7850Sstevel@tonic-gate * Alert protocol
7860Sstevel@tonic-gate * 2 bytes needed (AlertLevel, AlertDescription)
7870Sstevel@tonic-gate * Handshake protocol
7880Sstevel@tonic-gate * 4 bytes needed (HandshakeType, uint24 length) -- we just have
7890Sstevel@tonic-gate * to detect unexpected Client Hello and Hello Request messages
7900Sstevel@tonic-gate * here, anything else is handled by higher layers
7910Sstevel@tonic-gate * Application data protocol
7920Sstevel@tonic-gate * none of our business
7930Sstevel@tonic-gate */
ssl3_read_bytes(SSL * s,int type,unsigned char * buf,int len,int peek)7940Sstevel@tonic-gate int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
7950Sstevel@tonic-gate {
7960Sstevel@tonic-gate int al,i,j,ret;
7970Sstevel@tonic-gate unsigned int n;
7980Sstevel@tonic-gate SSL3_RECORD *rr;
7990Sstevel@tonic-gate void (*cb)(const SSL *ssl,int type2,int val)=NULL;
8000Sstevel@tonic-gate
8010Sstevel@tonic-gate if (s->s3->rbuf.buf == NULL) /* Not initialized yet */
8020Sstevel@tonic-gate if (!ssl3_setup_buffers(s))
8030Sstevel@tonic-gate return(-1);
8040Sstevel@tonic-gate
8050Sstevel@tonic-gate if ((type && (type != SSL3_RT_APPLICATION_DATA) && (type != SSL3_RT_HANDSHAKE) && type) ||
8060Sstevel@tonic-gate (peek && (type != SSL3_RT_APPLICATION_DATA)))
8070Sstevel@tonic-gate {
8080Sstevel@tonic-gate SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR);
8090Sstevel@tonic-gate return -1;
8100Sstevel@tonic-gate }
8110Sstevel@tonic-gate
8120Sstevel@tonic-gate if ((type == SSL3_RT_HANDSHAKE) && (s->s3->handshake_fragment_len > 0))
8130Sstevel@tonic-gate /* (partially) satisfy request from storage */
8140Sstevel@tonic-gate {
8150Sstevel@tonic-gate unsigned char *src = s->s3->handshake_fragment;
8160Sstevel@tonic-gate unsigned char *dst = buf;
8170Sstevel@tonic-gate unsigned int k;
8180Sstevel@tonic-gate
8190Sstevel@tonic-gate /* peek == 0 */
8200Sstevel@tonic-gate n = 0;
8210Sstevel@tonic-gate while ((len > 0) && (s->s3->handshake_fragment_len > 0))
8220Sstevel@tonic-gate {
8230Sstevel@tonic-gate *dst++ = *src++;
8240Sstevel@tonic-gate len--; s->s3->handshake_fragment_len--;
8250Sstevel@tonic-gate n++;
8260Sstevel@tonic-gate }
8270Sstevel@tonic-gate /* move any remaining fragment bytes: */
8280Sstevel@tonic-gate for (k = 0; k < s->s3->handshake_fragment_len; k++)
8290Sstevel@tonic-gate s->s3->handshake_fragment[k] = *src++;
8300Sstevel@tonic-gate return n;
8310Sstevel@tonic-gate }
8320Sstevel@tonic-gate
8330Sstevel@tonic-gate /* Now s->s3->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE. */
8340Sstevel@tonic-gate
8350Sstevel@tonic-gate if (!s->in_handshake && SSL_in_init(s))
8360Sstevel@tonic-gate {
8370Sstevel@tonic-gate /* type == SSL3_RT_APPLICATION_DATA */
8380Sstevel@tonic-gate i=s->handshake_func(s);
8390Sstevel@tonic-gate if (i < 0) return(i);
8400Sstevel@tonic-gate if (i == 0)
8410Sstevel@tonic-gate {
8420Sstevel@tonic-gate SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);
8430Sstevel@tonic-gate return(-1);
8440Sstevel@tonic-gate }
8450Sstevel@tonic-gate }
8460Sstevel@tonic-gate start:
8470Sstevel@tonic-gate s->rwstate=SSL_NOTHING;
8480Sstevel@tonic-gate
8490Sstevel@tonic-gate /* s->s3->rrec.type - is the type of record
8500Sstevel@tonic-gate * s->s3->rrec.data, - data
8510Sstevel@tonic-gate * s->s3->rrec.off, - offset into 'data' for next read
8520Sstevel@tonic-gate * s->s3->rrec.length, - number of bytes. */
8530Sstevel@tonic-gate rr = &(s->s3->rrec);
8540Sstevel@tonic-gate
8550Sstevel@tonic-gate /* get new packet if necessary */
8560Sstevel@tonic-gate if ((rr->length == 0) || (s->rstate == SSL_ST_READ_BODY))
8570Sstevel@tonic-gate {
8580Sstevel@tonic-gate ret=ssl3_get_record(s);
8590Sstevel@tonic-gate if (ret <= 0) return(ret);
8600Sstevel@tonic-gate }
8610Sstevel@tonic-gate
8620Sstevel@tonic-gate /* we now have a packet which can be read and processed */
8630Sstevel@tonic-gate
8640Sstevel@tonic-gate if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec,
8650Sstevel@tonic-gate * reset by ssl3_get_finished */
8660Sstevel@tonic-gate && (rr->type != SSL3_RT_HANDSHAKE))
8670Sstevel@tonic-gate {
8680Sstevel@tonic-gate al=SSL_AD_UNEXPECTED_MESSAGE;
8690Sstevel@tonic-gate SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);
870*2139Sjp161948 goto f_err;
8710Sstevel@tonic-gate }
8720Sstevel@tonic-gate
8730Sstevel@tonic-gate /* If the other end has shut down, throw anything we read away
8740Sstevel@tonic-gate * (even in 'peek' mode) */
8750Sstevel@tonic-gate if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
8760Sstevel@tonic-gate {
8770Sstevel@tonic-gate rr->length=0;
8780Sstevel@tonic-gate s->rwstate=SSL_NOTHING;
8790Sstevel@tonic-gate return(0);
8800Sstevel@tonic-gate }
8810Sstevel@tonic-gate
8820Sstevel@tonic-gate
8830Sstevel@tonic-gate if (type == rr->type) /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */
8840Sstevel@tonic-gate {
8850Sstevel@tonic-gate /* make sure that we are not getting application data when we
8860Sstevel@tonic-gate * are doing a handshake for the first time */
8870Sstevel@tonic-gate if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&
8880Sstevel@tonic-gate (s->enc_read_ctx == NULL))
8890Sstevel@tonic-gate {
8900Sstevel@tonic-gate al=SSL_AD_UNEXPECTED_MESSAGE;
8910Sstevel@tonic-gate SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_APP_DATA_IN_HANDSHAKE);
8920Sstevel@tonic-gate goto f_err;
8930Sstevel@tonic-gate }
8940Sstevel@tonic-gate
8950Sstevel@tonic-gate if (len <= 0) return(len);
8960Sstevel@tonic-gate
8970Sstevel@tonic-gate if ((unsigned int)len > rr->length)
8980Sstevel@tonic-gate n = rr->length;
8990Sstevel@tonic-gate else
9000Sstevel@tonic-gate n = (unsigned int)len;
9010Sstevel@tonic-gate
9020Sstevel@tonic-gate memcpy(buf,&(rr->data[rr->off]),n);
9030Sstevel@tonic-gate if (!peek)
9040Sstevel@tonic-gate {
9050Sstevel@tonic-gate rr->length-=n;
9060Sstevel@tonic-gate rr->off+=n;
9070Sstevel@tonic-gate if (rr->length == 0)
9080Sstevel@tonic-gate {
9090Sstevel@tonic-gate s->rstate=SSL_ST_READ_HEADER;
9100Sstevel@tonic-gate rr->off=0;
9110Sstevel@tonic-gate }
9120Sstevel@tonic-gate }
9130Sstevel@tonic-gate return(n);
9140Sstevel@tonic-gate }
9150Sstevel@tonic-gate
9160Sstevel@tonic-gate
9170Sstevel@tonic-gate /* If we get here, then type != rr->type; if we have a handshake
9180Sstevel@tonic-gate * message, then it was unexpected (Hello Request or Client Hello). */
9190Sstevel@tonic-gate
9200Sstevel@tonic-gate /* In case of record types for which we have 'fragment' storage,
9210Sstevel@tonic-gate * fill that so that we can process the data at a fixed place.
9220Sstevel@tonic-gate */
9230Sstevel@tonic-gate {
9240Sstevel@tonic-gate unsigned int dest_maxlen = 0;
9250Sstevel@tonic-gate unsigned char *dest = NULL;
9260Sstevel@tonic-gate unsigned int *dest_len = NULL;
9270Sstevel@tonic-gate
9280Sstevel@tonic-gate if (rr->type == SSL3_RT_HANDSHAKE)
9290Sstevel@tonic-gate {
9300Sstevel@tonic-gate dest_maxlen = sizeof s->s3->handshake_fragment;
9310Sstevel@tonic-gate dest = s->s3->handshake_fragment;
9320Sstevel@tonic-gate dest_len = &s->s3->handshake_fragment_len;
9330Sstevel@tonic-gate }
9340Sstevel@tonic-gate else if (rr->type == SSL3_RT_ALERT)
9350Sstevel@tonic-gate {
9360Sstevel@tonic-gate dest_maxlen = sizeof s->s3->alert_fragment;
9370Sstevel@tonic-gate dest = s->s3->alert_fragment;
9380Sstevel@tonic-gate dest_len = &s->s3->alert_fragment_len;
9390Sstevel@tonic-gate }
9400Sstevel@tonic-gate
9410Sstevel@tonic-gate if (dest_maxlen > 0)
9420Sstevel@tonic-gate {
9430Sstevel@tonic-gate n = dest_maxlen - *dest_len; /* available space in 'dest' */
9440Sstevel@tonic-gate if (rr->length < n)
9450Sstevel@tonic-gate n = rr->length; /* available bytes */
9460Sstevel@tonic-gate
9470Sstevel@tonic-gate /* now move 'n' bytes: */
9480Sstevel@tonic-gate while (n-- > 0)
9490Sstevel@tonic-gate {
9500Sstevel@tonic-gate dest[(*dest_len)++] = rr->data[rr->off++];
9510Sstevel@tonic-gate rr->length--;
9520Sstevel@tonic-gate }
9530Sstevel@tonic-gate
9540Sstevel@tonic-gate if (*dest_len < dest_maxlen)
9550Sstevel@tonic-gate goto start; /* fragment was too small */
9560Sstevel@tonic-gate }
9570Sstevel@tonic-gate }
9580Sstevel@tonic-gate
9590Sstevel@tonic-gate /* s->s3->handshake_fragment_len == 4 iff rr->type == SSL3_RT_HANDSHAKE;
9600Sstevel@tonic-gate * s->s3->alert_fragment_len == 2 iff rr->type == SSL3_RT_ALERT.
9610Sstevel@tonic-gate * (Possibly rr is 'empty' now, i.e. rr->length may be 0.) */
9620Sstevel@tonic-gate
9630Sstevel@tonic-gate /* If we are a client, check for an incoming 'Hello Request': */
9640Sstevel@tonic-gate if ((!s->server) &&
9650Sstevel@tonic-gate (s->s3->handshake_fragment_len >= 4) &&
9660Sstevel@tonic-gate (s->s3->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST) &&
9670Sstevel@tonic-gate (s->session != NULL) && (s->session->cipher != NULL))
9680Sstevel@tonic-gate {
9690Sstevel@tonic-gate s->s3->handshake_fragment_len = 0;
9700Sstevel@tonic-gate
9710Sstevel@tonic-gate if ((s->s3->handshake_fragment[1] != 0) ||
9720Sstevel@tonic-gate (s->s3->handshake_fragment[2] != 0) ||
9730Sstevel@tonic-gate (s->s3->handshake_fragment[3] != 0))
9740Sstevel@tonic-gate {
9750Sstevel@tonic-gate al=SSL_AD_DECODE_ERROR;
9760Sstevel@tonic-gate SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_BAD_HELLO_REQUEST);
977*2139Sjp161948 goto f_err;
9780Sstevel@tonic-gate }
9790Sstevel@tonic-gate
9800Sstevel@tonic-gate if (s->msg_callback)
9810Sstevel@tonic-gate s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->s3->handshake_fragment, 4, s, s->msg_callback_arg);
9820Sstevel@tonic-gate
9830Sstevel@tonic-gate if (SSL_is_init_finished(s) &&
9840Sstevel@tonic-gate !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) &&
9850Sstevel@tonic-gate !s->s3->renegotiate)
9860Sstevel@tonic-gate {
9870Sstevel@tonic-gate ssl3_renegotiate(s);
9880Sstevel@tonic-gate if (ssl3_renegotiate_check(s))
9890Sstevel@tonic-gate {
9900Sstevel@tonic-gate i=s->handshake_func(s);
9910Sstevel@tonic-gate if (i < 0) return(i);
9920Sstevel@tonic-gate if (i == 0)
9930Sstevel@tonic-gate {
9940Sstevel@tonic-gate SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);
9950Sstevel@tonic-gate return(-1);
9960Sstevel@tonic-gate }
9970Sstevel@tonic-gate
9980Sstevel@tonic-gate if (!(s->mode & SSL_MODE_AUTO_RETRY))
9990Sstevel@tonic-gate {
10000Sstevel@tonic-gate if (s->s3->rbuf.left == 0) /* no read-ahead left? */
10010Sstevel@tonic-gate {
10020Sstevel@tonic-gate BIO *bio;
10030Sstevel@tonic-gate /* In the case where we try to read application data,
10040Sstevel@tonic-gate * but we trigger an SSL handshake, we return -1 with
10050Sstevel@tonic-gate * the retry option set. Otherwise renegotiation may
10060Sstevel@tonic-gate * cause nasty problems in the blocking world */
10070Sstevel@tonic-gate s->rwstate=SSL_READING;
10080Sstevel@tonic-gate bio=SSL_get_rbio(s);
10090Sstevel@tonic-gate BIO_clear_retry_flags(bio);
10100Sstevel@tonic-gate BIO_set_retry_read(bio);
10110Sstevel@tonic-gate return(-1);
10120Sstevel@tonic-gate }
10130Sstevel@tonic-gate }
10140Sstevel@tonic-gate }
10150Sstevel@tonic-gate }
10160Sstevel@tonic-gate /* we either finished a handshake or ignored the request,
10170Sstevel@tonic-gate * now try again to obtain the (application) data we were asked for */
10180Sstevel@tonic-gate goto start;
10190Sstevel@tonic-gate }
10200Sstevel@tonic-gate
10210Sstevel@tonic-gate if (s->s3->alert_fragment_len >= 2)
10220Sstevel@tonic-gate {
10230Sstevel@tonic-gate int alert_level = s->s3->alert_fragment[0];
10240Sstevel@tonic-gate int alert_descr = s->s3->alert_fragment[1];
10250Sstevel@tonic-gate
10260Sstevel@tonic-gate s->s3->alert_fragment_len = 0;
10270Sstevel@tonic-gate
10280Sstevel@tonic-gate if (s->msg_callback)
10290Sstevel@tonic-gate s->msg_callback(0, s->version, SSL3_RT_ALERT, s->s3->alert_fragment, 2, s, s->msg_callback_arg);
10300Sstevel@tonic-gate
10310Sstevel@tonic-gate if (s->info_callback != NULL)
10320Sstevel@tonic-gate cb=s->info_callback;
10330Sstevel@tonic-gate else if (s->ctx->info_callback != NULL)
10340Sstevel@tonic-gate cb=s->ctx->info_callback;
10350Sstevel@tonic-gate
10360Sstevel@tonic-gate if (cb != NULL)
10370Sstevel@tonic-gate {
10380Sstevel@tonic-gate j = (alert_level << 8) | alert_descr;
10390Sstevel@tonic-gate cb(s, SSL_CB_READ_ALERT, j);
10400Sstevel@tonic-gate }
10410Sstevel@tonic-gate
10420Sstevel@tonic-gate if (alert_level == 1) /* warning */
10430Sstevel@tonic-gate {
10440Sstevel@tonic-gate s->s3->warn_alert = alert_descr;
10450Sstevel@tonic-gate if (alert_descr == SSL_AD_CLOSE_NOTIFY)
10460Sstevel@tonic-gate {
10470Sstevel@tonic-gate s->shutdown |= SSL_RECEIVED_SHUTDOWN;
10480Sstevel@tonic-gate return(0);
10490Sstevel@tonic-gate }
10500Sstevel@tonic-gate }
10510Sstevel@tonic-gate else if (alert_level == 2) /* fatal */
10520Sstevel@tonic-gate {
10530Sstevel@tonic-gate char tmp[16];
10540Sstevel@tonic-gate
10550Sstevel@tonic-gate s->rwstate=SSL_NOTHING;
10560Sstevel@tonic-gate s->s3->fatal_alert = alert_descr;
10570Sstevel@tonic-gate SSLerr(SSL_F_SSL3_READ_BYTES, SSL_AD_REASON_OFFSET + alert_descr);
10580Sstevel@tonic-gate BIO_snprintf(tmp,sizeof tmp,"%d",alert_descr);
10590Sstevel@tonic-gate ERR_add_error_data(2,"SSL alert number ",tmp);
10600Sstevel@tonic-gate s->shutdown|=SSL_RECEIVED_SHUTDOWN;
10610Sstevel@tonic-gate SSL_CTX_remove_session(s->ctx,s->session);
10620Sstevel@tonic-gate return(0);
10630Sstevel@tonic-gate }
10640Sstevel@tonic-gate else
10650Sstevel@tonic-gate {
10660Sstevel@tonic-gate al=SSL_AD_ILLEGAL_PARAMETER;
10670Sstevel@tonic-gate SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_UNKNOWN_ALERT_TYPE);
10680Sstevel@tonic-gate goto f_err;
10690Sstevel@tonic-gate }
10700Sstevel@tonic-gate
10710Sstevel@tonic-gate goto start;
10720Sstevel@tonic-gate }
10730Sstevel@tonic-gate
10740Sstevel@tonic-gate if (s->shutdown & SSL_SENT_SHUTDOWN) /* but we have not received a shutdown */
10750Sstevel@tonic-gate {
10760Sstevel@tonic-gate s->rwstate=SSL_NOTHING;
10770Sstevel@tonic-gate rr->length=0;
10780Sstevel@tonic-gate return(0);
10790Sstevel@tonic-gate }
10800Sstevel@tonic-gate
10810Sstevel@tonic-gate if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC)
10820Sstevel@tonic-gate {
10830Sstevel@tonic-gate /* 'Change Cipher Spec' is just a single byte, so we know
10840Sstevel@tonic-gate * exactly what the record payload has to look like */
10850Sstevel@tonic-gate if ( (rr->length != 1) || (rr->off != 0) ||
10860Sstevel@tonic-gate (rr->data[0] != SSL3_MT_CCS))
10870Sstevel@tonic-gate {
1088*2139Sjp161948 al=SSL_AD_ILLEGAL_PARAMETER;
10890Sstevel@tonic-gate SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_BAD_CHANGE_CIPHER_SPEC);
1090*2139Sjp161948 goto f_err;
10910Sstevel@tonic-gate }
10920Sstevel@tonic-gate
10930Sstevel@tonic-gate /* Check we have a cipher to change to */
10940Sstevel@tonic-gate if (s->s3->tmp.new_cipher == NULL)
10950Sstevel@tonic-gate {
1096*2139Sjp161948 al=SSL_AD_UNEXPECTED_MESSAGE;
1097*2139Sjp161948 SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_CCS_RECEIVED_EARLY);
1098*2139Sjp161948 goto f_err;
10990Sstevel@tonic-gate }
11000Sstevel@tonic-gate
11010Sstevel@tonic-gate rr->length=0;
11020Sstevel@tonic-gate
11030Sstevel@tonic-gate if (s->msg_callback)
11040Sstevel@tonic-gate s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, 1, s, s->msg_callback_arg);
11050Sstevel@tonic-gate
11060Sstevel@tonic-gate s->s3->change_cipher_spec=1;
1107*2139Sjp161948 if (!ssl3_do_change_cipher_spec(s))
11080Sstevel@tonic-gate goto err;
11090Sstevel@tonic-gate else
11100Sstevel@tonic-gate goto start;
11110Sstevel@tonic-gate }
11120Sstevel@tonic-gate
11130Sstevel@tonic-gate /* Unexpected handshake message (Client Hello, or protocol violation) */
11140Sstevel@tonic-gate if ((s->s3->handshake_fragment_len >= 4) && !s->in_handshake)
11150Sstevel@tonic-gate {
11160Sstevel@tonic-gate if (((s->state&SSL_ST_MASK) == SSL_ST_OK) &&
11170Sstevel@tonic-gate !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS))
11180Sstevel@tonic-gate {
11190Sstevel@tonic-gate #if 0 /* worked only because C operator preferences are not as expected (and
11200Sstevel@tonic-gate * because this is not really needed for clients except for detecting
11210Sstevel@tonic-gate * protocol violations): */
11220Sstevel@tonic-gate s->state=SSL_ST_BEFORE|(s->server)
11230Sstevel@tonic-gate ?SSL_ST_ACCEPT
11240Sstevel@tonic-gate :SSL_ST_CONNECT;
11250Sstevel@tonic-gate #else
11260Sstevel@tonic-gate s->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT;
11270Sstevel@tonic-gate #endif
11280Sstevel@tonic-gate s->new_session=1;
11290Sstevel@tonic-gate }
11300Sstevel@tonic-gate i=s->handshake_func(s);
11310Sstevel@tonic-gate if (i < 0) return(i);
11320Sstevel@tonic-gate if (i == 0)
11330Sstevel@tonic-gate {
11340Sstevel@tonic-gate SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE);
11350Sstevel@tonic-gate return(-1);
11360Sstevel@tonic-gate }
11370Sstevel@tonic-gate
11380Sstevel@tonic-gate if (!(s->mode & SSL_MODE_AUTO_RETRY))
11390Sstevel@tonic-gate {
11400Sstevel@tonic-gate if (s->s3->rbuf.left == 0) /* no read-ahead left? */
11410Sstevel@tonic-gate {
11420Sstevel@tonic-gate BIO *bio;
11430Sstevel@tonic-gate /* In the case where we try to read application data,
11440Sstevel@tonic-gate * but we trigger an SSL handshake, we return -1 with
11450Sstevel@tonic-gate * the retry option set. Otherwise renegotiation may
11460Sstevel@tonic-gate * cause nasty problems in the blocking world */
11470Sstevel@tonic-gate s->rwstate=SSL_READING;
11480Sstevel@tonic-gate bio=SSL_get_rbio(s);
11490Sstevel@tonic-gate BIO_clear_retry_flags(bio);
11500Sstevel@tonic-gate BIO_set_retry_read(bio);
11510Sstevel@tonic-gate return(-1);
11520Sstevel@tonic-gate }
11530Sstevel@tonic-gate }
11540Sstevel@tonic-gate goto start;
11550Sstevel@tonic-gate }
11560Sstevel@tonic-gate
11570Sstevel@tonic-gate switch (rr->type)
11580Sstevel@tonic-gate {
11590Sstevel@tonic-gate default:
11600Sstevel@tonic-gate #ifndef OPENSSL_NO_TLS
11610Sstevel@tonic-gate /* TLS just ignores unknown message types */
11620Sstevel@tonic-gate if (s->version == TLS1_VERSION)
11630Sstevel@tonic-gate {
11640Sstevel@tonic-gate rr->length = 0;
11650Sstevel@tonic-gate goto start;
11660Sstevel@tonic-gate }
11670Sstevel@tonic-gate #endif
11680Sstevel@tonic-gate al=SSL_AD_UNEXPECTED_MESSAGE;
11690Sstevel@tonic-gate SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_UNEXPECTED_RECORD);
11700Sstevel@tonic-gate goto f_err;
11710Sstevel@tonic-gate case SSL3_RT_CHANGE_CIPHER_SPEC:
11720Sstevel@tonic-gate case SSL3_RT_ALERT:
11730Sstevel@tonic-gate case SSL3_RT_HANDSHAKE:
11740Sstevel@tonic-gate /* we already handled all of these, with the possible exception
11750Sstevel@tonic-gate * of SSL3_RT_HANDSHAKE when s->in_handshake is set, but that
11760Sstevel@tonic-gate * should not happen when type != rr->type */
11770Sstevel@tonic-gate al=SSL_AD_UNEXPECTED_MESSAGE;
11780Sstevel@tonic-gate SSLerr(SSL_F_SSL3_READ_BYTES,ERR_R_INTERNAL_ERROR);
11790Sstevel@tonic-gate goto f_err;
11800Sstevel@tonic-gate case SSL3_RT_APPLICATION_DATA:
11810Sstevel@tonic-gate /* At this point, we were expecting handshake data,
11820Sstevel@tonic-gate * but have application data. If the library was
11830Sstevel@tonic-gate * running inside ssl3_read() (i.e. in_read_app_data
11840Sstevel@tonic-gate * is set) and it makes sense to read application data
11850Sstevel@tonic-gate * at this point (session renegotiation not yet started),
11860Sstevel@tonic-gate * we will indulge it.
11870Sstevel@tonic-gate */
11880Sstevel@tonic-gate if (s->s3->in_read_app_data &&
11890Sstevel@tonic-gate (s->s3->total_renegotiations != 0) &&
11900Sstevel@tonic-gate ((
11910Sstevel@tonic-gate (s->state & SSL_ST_CONNECT) &&
11920Sstevel@tonic-gate (s->state >= SSL3_ST_CW_CLNT_HELLO_A) &&
11930Sstevel@tonic-gate (s->state <= SSL3_ST_CR_SRVR_HELLO_A)
11940Sstevel@tonic-gate ) || (
11950Sstevel@tonic-gate (s->state & SSL_ST_ACCEPT) &&
11960Sstevel@tonic-gate (s->state <= SSL3_ST_SW_HELLO_REQ_A) &&
11970Sstevel@tonic-gate (s->state >= SSL3_ST_SR_CLNT_HELLO_A)
11980Sstevel@tonic-gate )
11990Sstevel@tonic-gate ))
12000Sstevel@tonic-gate {
12010Sstevel@tonic-gate s->s3->in_read_app_data=2;
12020Sstevel@tonic-gate return(-1);
12030Sstevel@tonic-gate }
12040Sstevel@tonic-gate else
12050Sstevel@tonic-gate {
12060Sstevel@tonic-gate al=SSL_AD_UNEXPECTED_MESSAGE;
12070Sstevel@tonic-gate SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_UNEXPECTED_RECORD);
12080Sstevel@tonic-gate goto f_err;
12090Sstevel@tonic-gate }
12100Sstevel@tonic-gate }
12110Sstevel@tonic-gate /* not reached */
12120Sstevel@tonic-gate
12130Sstevel@tonic-gate f_err:
12140Sstevel@tonic-gate ssl3_send_alert(s,SSL3_AL_FATAL,al);
12150Sstevel@tonic-gate err:
12160Sstevel@tonic-gate return(-1);
12170Sstevel@tonic-gate }
12180Sstevel@tonic-gate
ssl3_do_change_cipher_spec(SSL * s)1219*2139Sjp161948 int ssl3_do_change_cipher_spec(SSL *s)
12200Sstevel@tonic-gate {
12210Sstevel@tonic-gate int i;
12220Sstevel@tonic-gate const char *sender;
12230Sstevel@tonic-gate int slen;
12240Sstevel@tonic-gate
12250Sstevel@tonic-gate if (s->state & SSL_ST_ACCEPT)
12260Sstevel@tonic-gate i=SSL3_CHANGE_CIPHER_SERVER_READ;
12270Sstevel@tonic-gate else
12280Sstevel@tonic-gate i=SSL3_CHANGE_CIPHER_CLIENT_READ;
12290Sstevel@tonic-gate
12300Sstevel@tonic-gate if (s->s3->tmp.key_block == NULL)
12310Sstevel@tonic-gate {
12320Sstevel@tonic-gate s->session->cipher=s->s3->tmp.new_cipher;
12330Sstevel@tonic-gate if (!s->method->ssl3_enc->setup_key_block(s)) return(0);
12340Sstevel@tonic-gate }
12350Sstevel@tonic-gate
12360Sstevel@tonic-gate if (!s->method->ssl3_enc->change_cipher_state(s,i))
12370Sstevel@tonic-gate return(0);
12380Sstevel@tonic-gate
12390Sstevel@tonic-gate /* we have to record the message digest at
12400Sstevel@tonic-gate * this point so we can get it before we read
12410Sstevel@tonic-gate * the finished message */
12420Sstevel@tonic-gate if (s->state & SSL_ST_CONNECT)
12430Sstevel@tonic-gate {
12440Sstevel@tonic-gate sender=s->method->ssl3_enc->server_finished_label;
12450Sstevel@tonic-gate slen=s->method->ssl3_enc->server_finished_label_len;
12460Sstevel@tonic-gate }
12470Sstevel@tonic-gate else
12480Sstevel@tonic-gate {
12490Sstevel@tonic-gate sender=s->method->ssl3_enc->client_finished_label;
12500Sstevel@tonic-gate slen=s->method->ssl3_enc->client_finished_label_len;
12510Sstevel@tonic-gate }
12520Sstevel@tonic-gate
12530Sstevel@tonic-gate s->s3->tmp.peer_finish_md_len = s->method->ssl3_enc->final_finish_mac(s,
12540Sstevel@tonic-gate &(s->s3->finish_dgst1),
12550Sstevel@tonic-gate &(s->s3->finish_dgst2),
12560Sstevel@tonic-gate sender,slen,s->s3->tmp.peer_finish_md);
12570Sstevel@tonic-gate
12580Sstevel@tonic-gate return(1);
12590Sstevel@tonic-gate }
12600Sstevel@tonic-gate
ssl3_send_alert(SSL * s,int level,int desc)12610Sstevel@tonic-gate void ssl3_send_alert(SSL *s, int level, int desc)
12620Sstevel@tonic-gate {
12630Sstevel@tonic-gate /* Map tls/ssl alert value to correct one */
12640Sstevel@tonic-gate desc=s->method->ssl3_enc->alert_value(desc);
12650Sstevel@tonic-gate if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION)
12660Sstevel@tonic-gate desc = SSL_AD_HANDSHAKE_FAILURE; /* SSL 3.0 does not have protocol_version alerts */
12670Sstevel@tonic-gate if (desc < 0) return;
12680Sstevel@tonic-gate /* If a fatal one, remove from cache */
12690Sstevel@tonic-gate if ((level == 2) && (s->session != NULL))
12700Sstevel@tonic-gate SSL_CTX_remove_session(s->ctx,s->session);
12710Sstevel@tonic-gate
12720Sstevel@tonic-gate s->s3->alert_dispatch=1;
12730Sstevel@tonic-gate s->s3->send_alert[0]=level;
12740Sstevel@tonic-gate s->s3->send_alert[1]=desc;
12750Sstevel@tonic-gate if (s->s3->wbuf.left == 0) /* data still being written out? */
1276*2139Sjp161948 s->method->ssl_dispatch_alert(s);
12770Sstevel@tonic-gate /* else data is still being written out, we will get written
12780Sstevel@tonic-gate * some time in the future */
12790Sstevel@tonic-gate }
12800Sstevel@tonic-gate
ssl3_dispatch_alert(SSL * s)12810Sstevel@tonic-gate int ssl3_dispatch_alert(SSL *s)
12820Sstevel@tonic-gate {
12830Sstevel@tonic-gate int i,j;
12840Sstevel@tonic-gate void (*cb)(const SSL *ssl,int type,int val)=NULL;
12850Sstevel@tonic-gate
12860Sstevel@tonic-gate s->s3->alert_dispatch=0;
12870Sstevel@tonic-gate i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], 2, 0);
12880Sstevel@tonic-gate if (i <= 0)
12890Sstevel@tonic-gate {
12900Sstevel@tonic-gate s->s3->alert_dispatch=1;
12910Sstevel@tonic-gate }
12920Sstevel@tonic-gate else
12930Sstevel@tonic-gate {
12940Sstevel@tonic-gate /* Alert sent to BIO. If it is important, flush it now.
12950Sstevel@tonic-gate * If the message does not get sent due to non-blocking IO,
12960Sstevel@tonic-gate * we will not worry too much. */
12970Sstevel@tonic-gate if (s->s3->send_alert[0] == SSL3_AL_FATAL)
12980Sstevel@tonic-gate (void)BIO_flush(s->wbio);
12990Sstevel@tonic-gate
13000Sstevel@tonic-gate if (s->msg_callback)
13010Sstevel@tonic-gate s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 2, s, s->msg_callback_arg);
13020Sstevel@tonic-gate
13030Sstevel@tonic-gate if (s->info_callback != NULL)
13040Sstevel@tonic-gate cb=s->info_callback;
13050Sstevel@tonic-gate else if (s->ctx->info_callback != NULL)
13060Sstevel@tonic-gate cb=s->ctx->info_callback;
13070Sstevel@tonic-gate
13080Sstevel@tonic-gate if (cb != NULL)
13090Sstevel@tonic-gate {
13100Sstevel@tonic-gate j=(s->s3->send_alert[0]<<8)|s->s3->send_alert[1];
13110Sstevel@tonic-gate cb(s,SSL_CB_WRITE_ALERT,j);
13120Sstevel@tonic-gate }
13130Sstevel@tonic-gate }
13140Sstevel@tonic-gate return(i);
13150Sstevel@tonic-gate }
1316