sl@0: /* ssl/s3_pkt.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * openssl-core@openssl.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.openssl.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #define USE_SOCKETS sl@0: #include "ssl_locl.h" sl@0: #include sl@0: #include sl@0: sl@0: static int do_ssl3_write(SSL *s, int type, const unsigned char *buf, sl@0: unsigned int len, int create_empty_fragment); sl@0: static int ssl3_get_record(SSL *s); sl@0: sl@0: int ssl3_read_n(SSL *s, int n, int max, int extend) sl@0: { sl@0: /* If extend == 0, obtain new n-byte packet; if extend == 1, increase sl@0: * packet by another n bytes. sl@0: * The packet will be in the sub-array of s->s3->rbuf.buf specified sl@0: * by s->packet and s->packet_length. sl@0: * (If s->read_ahead is set, 'max' bytes may be stored in rbuf sl@0: * [plus s->packet_length bytes if extend == 1].) sl@0: */ sl@0: int i,off,newb; sl@0: sl@0: if (!extend) sl@0: { sl@0: /* start with empty packet ... */ sl@0: if (s->s3->rbuf.left == 0) sl@0: s->s3->rbuf.offset = 0; sl@0: s->packet = s->s3->rbuf.buf + s->s3->rbuf.offset; sl@0: s->packet_length = 0; sl@0: /* ... now we can act as if 'extend' was set */ sl@0: } sl@0: sl@0: /* extend reads should not span multiple packets for DTLS */ sl@0: if ( SSL_version(s) == DTLS1_VERSION && sl@0: extend) sl@0: { sl@0: if ( s->s3->rbuf.left > 0 && n > s->s3->rbuf.left) sl@0: n = s->s3->rbuf.left; sl@0: } sl@0: sl@0: /* if there is enough in the buffer from a previous read, take some */ sl@0: if (s->s3->rbuf.left >= (int)n) sl@0: { sl@0: s->packet_length+=n; sl@0: s->s3->rbuf.left-=n; sl@0: s->s3->rbuf.offset+=n; sl@0: return(n); sl@0: } sl@0: sl@0: /* else we need to read more data */ sl@0: if (!s->read_ahead) sl@0: max=n; sl@0: sl@0: { sl@0: /* avoid buffer overflow */ sl@0: int max_max = s->s3->rbuf.len - s->packet_length; sl@0: if (max > max_max) sl@0: max = max_max; sl@0: } sl@0: if (n > max) /* does not happen */ sl@0: { sl@0: SSLerr(SSL_F_SSL3_READ_N,ERR_R_INTERNAL_ERROR); sl@0: return -1; sl@0: } sl@0: sl@0: off = s->packet_length; sl@0: newb = s->s3->rbuf.left; sl@0: /* Move any available bytes to front of buffer: sl@0: * 'off' bytes already pointed to by 'packet', sl@0: * 'newb' extra ones at the end */ sl@0: if (s->packet != s->s3->rbuf.buf) sl@0: { sl@0: /* off > 0 */ sl@0: memmove(s->s3->rbuf.buf, s->packet, off+newb); sl@0: s->packet = s->s3->rbuf.buf; sl@0: } sl@0: sl@0: while (newb < n) sl@0: { sl@0: /* Now we have off+newb bytes at the front of s->s3->rbuf.buf and need sl@0: * to read in more until we have off+n (up to off+max if possible) */ sl@0: sl@0: clear_sys_error(); sl@0: if (s->rbio != NULL) sl@0: { sl@0: s->rwstate=SSL_READING; sl@0: i=BIO_read(s->rbio, &(s->s3->rbuf.buf[off+newb]), max-newb); sl@0: } sl@0: else sl@0: { sl@0: SSLerr(SSL_F_SSL3_READ_N,SSL_R_READ_BIO_NOT_SET); sl@0: i = -1; sl@0: } sl@0: sl@0: if (i <= 0) sl@0: { sl@0: s->s3->rbuf.left = newb; sl@0: return(i); sl@0: } sl@0: newb+=i; sl@0: } sl@0: sl@0: /* done reading, now the book-keeping */ sl@0: s->s3->rbuf.offset = off + n; sl@0: s->s3->rbuf.left = newb - n; sl@0: s->packet_length += n; sl@0: s->rwstate=SSL_NOTHING; sl@0: return(n); sl@0: } sl@0: sl@0: /* Call this to get a new input record. sl@0: * It will return <= 0 if more data is needed, normally due to an error sl@0: * or non-blocking IO. sl@0: * When it finishes, one packet has been decoded and can be found in sl@0: * ssl->s3->rrec.type - is the type of record sl@0: * ssl->s3->rrec.data, - data sl@0: * ssl->s3->rrec.length, - number of bytes sl@0: */ sl@0: /* used only by ssl3_read_bytes */ sl@0: static int ssl3_get_record(SSL *s) sl@0: { sl@0: int ssl_major,ssl_minor,al; sl@0: int enc_err,n,i,ret= -1; sl@0: SSL3_RECORD *rr; sl@0: SSL_SESSION *sess; sl@0: unsigned char *p; sl@0: unsigned char md[EVP_MAX_MD_SIZE]; sl@0: short version; sl@0: unsigned int mac_size; sl@0: int clear=0; sl@0: size_t extra; sl@0: int decryption_failed_or_bad_record_mac = 0; sl@0: unsigned char *mac = NULL; sl@0: sl@0: rr= &(s->s3->rrec); sl@0: sess=s->session; sl@0: sl@0: if (s->options & SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER) sl@0: extra=SSL3_RT_MAX_EXTRA; sl@0: else sl@0: extra=0; sl@0: if (extra != s->s3->rbuf.len - SSL3_RT_MAX_PACKET_SIZE) sl@0: { sl@0: /* actually likely an application error: SLS_OP_MICROSOFT_BIG_SSLV3_BUFFER sl@0: * set after ssl3_setup_buffers() was done */ sl@0: SSLerr(SSL_F_SSL3_GET_RECORD, ERR_R_INTERNAL_ERROR); sl@0: return -1; sl@0: } sl@0: sl@0: again: sl@0: /* check if we have the header */ sl@0: if ( (s->rstate != SSL_ST_READ_BODY) || sl@0: (s->packet_length < SSL3_RT_HEADER_LENGTH)) sl@0: { sl@0: n=ssl3_read_n(s, SSL3_RT_HEADER_LENGTH, s->s3->rbuf.len, 0); sl@0: if (n <= 0) return(n); /* error or non-blocking */ sl@0: s->rstate=SSL_ST_READ_BODY; sl@0: sl@0: p=s->packet; sl@0: sl@0: /* Pull apart the header into the SSL3_RECORD */ sl@0: rr->type= *(p++); sl@0: ssl_major= *(p++); sl@0: ssl_minor= *(p++); sl@0: version=(ssl_major<<8)|ssl_minor; sl@0: n2s(p,rr->length); sl@0: sl@0: /* Lets check version */ sl@0: if (!s->first_packet) sl@0: { sl@0: if (version != s->version) sl@0: { sl@0: SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_WRONG_VERSION_NUMBER); sl@0: /* Send back error using their sl@0: * version number :-) */ sl@0: s->version=version; sl@0: al=SSL_AD_PROTOCOL_VERSION; sl@0: goto f_err; sl@0: } sl@0: } sl@0: sl@0: if ((version>>8) != SSL3_VERSION_MAJOR) sl@0: { sl@0: SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_WRONG_VERSION_NUMBER); sl@0: goto err; sl@0: } sl@0: sl@0: if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH+extra) sl@0: { sl@0: al=SSL_AD_RECORD_OVERFLOW; sl@0: SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_PACKET_LENGTH_TOO_LONG); sl@0: goto f_err; sl@0: } sl@0: sl@0: /* now s->rstate == SSL_ST_READ_BODY */ sl@0: } sl@0: sl@0: /* s->rstate == SSL_ST_READ_BODY, get and decode the data */ sl@0: sl@0: if (rr->length > s->packet_length-SSL3_RT_HEADER_LENGTH) sl@0: { sl@0: /* now s->packet_length == SSL3_RT_HEADER_LENGTH */ sl@0: i=rr->length; sl@0: n=ssl3_read_n(s,i,i,1); sl@0: if (n <= 0) return(n); /* error or non-blocking io */ sl@0: /* now n == rr->length, sl@0: * and s->packet_length == SSL3_RT_HEADER_LENGTH + rr->length */ sl@0: } sl@0: sl@0: s->rstate=SSL_ST_READ_HEADER; /* set state for later operations */ sl@0: sl@0: /* At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length, sl@0: * and we have that many bytes in s->packet sl@0: */ sl@0: rr->input= &(s->packet[SSL3_RT_HEADER_LENGTH]); sl@0: sl@0: /* ok, we can now read from 's->packet' data into 'rr' sl@0: * rr->input points at rr->length bytes, which sl@0: * need to be copied into rr->data by either sl@0: * the decryption or by the decompression sl@0: * When the data is 'copied' into the rr->data buffer, sl@0: * rr->input will be pointed at the new buffer */ sl@0: sl@0: /* We now have - encrypted [ MAC [ compressed [ plain ] ] ] sl@0: * rr->length bytes of encrypted compressed stuff. */ sl@0: sl@0: /* check is not needed I believe */ sl@0: if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH+extra) sl@0: { sl@0: al=SSL_AD_RECORD_OVERFLOW; sl@0: SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_ENCRYPTED_LENGTH_TOO_LONG); sl@0: goto f_err; sl@0: } sl@0: sl@0: /* decrypt in place in 'rr->input' */ sl@0: rr->data=rr->input; sl@0: sl@0: enc_err = s->method->ssl3_enc->enc(s,0); sl@0: if (enc_err <= 0) sl@0: { sl@0: if (enc_err == 0) sl@0: /* SSLerr() and ssl3_send_alert() have been called */ sl@0: goto err; sl@0: sl@0: /* Otherwise enc_err == -1, which indicates bad padding sl@0: * (rec->length has not been changed in this case). sl@0: * To minimize information leaked via timing, we will perform sl@0: * the MAC computation anyway. */ sl@0: decryption_failed_or_bad_record_mac = 1; sl@0: } sl@0: sl@0: #ifdef TLS_DEBUG sl@0: printf("dec %d\n",rr->length); sl@0: { unsigned int z; for (z=0; zlength; z++) printf("%02X%c",rr->data[z],((z+1)%16)?' ':'\n'); } sl@0: printf("\n"); sl@0: #endif sl@0: sl@0: /* r->length is now the compressed data plus mac */ sl@0: if ( (sess == NULL) || sl@0: (s->enc_read_ctx == NULL) || sl@0: (s->read_hash == NULL)) sl@0: clear=1; sl@0: sl@0: if (!clear) sl@0: { sl@0: mac_size=EVP_MD_size(s->read_hash); sl@0: sl@0: if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH+extra+mac_size) sl@0: { sl@0: #if 0 /* OK only for stream ciphers (then rr->length is visible from ciphertext anyway) */ sl@0: al=SSL_AD_RECORD_OVERFLOW; sl@0: SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_PRE_MAC_LENGTH_TOO_LONG); sl@0: goto f_err; sl@0: #else sl@0: decryption_failed_or_bad_record_mac = 1; sl@0: #endif sl@0: } sl@0: /* check the MAC for rr->input (it's in mac_size bytes at the tail) */ sl@0: if (rr->length >= mac_size) sl@0: { sl@0: rr->length -= mac_size; sl@0: mac = &rr->data[rr->length]; sl@0: } sl@0: else sl@0: { sl@0: /* record (minus padding) is too short to contain a MAC */ sl@0: #if 0 /* OK only for stream ciphers */ sl@0: al=SSL_AD_DECODE_ERROR; sl@0: SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_LENGTH_TOO_SHORT); sl@0: goto f_err; sl@0: #else sl@0: decryption_failed_or_bad_record_mac = 1; sl@0: rr->length = 0; sl@0: #endif sl@0: } sl@0: i=s->method->ssl3_enc->mac(s,md,0); sl@0: if (mac == NULL || memcmp(md, mac, mac_size) != 0) sl@0: { sl@0: decryption_failed_or_bad_record_mac = 1; sl@0: } sl@0: } sl@0: sl@0: if (decryption_failed_or_bad_record_mac) sl@0: { sl@0: /* A separate 'decryption_failed' alert was introduced with TLS 1.0, sl@0: * SSL 3.0 only has 'bad_record_mac'. But unless a decryption sl@0: * failure is directly visible from the ciphertext anyway, sl@0: * we should not reveal which kind of error occured -- this sl@0: * might become visible to an attacker (e.g. via a logfile) */ sl@0: al=SSL_AD_BAD_RECORD_MAC; sl@0: SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC); sl@0: goto f_err; sl@0: } sl@0: sl@0: /* r->length is now just compressed */ sl@0: if (s->expand != NULL) sl@0: { sl@0: if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH+extra) sl@0: { sl@0: al=SSL_AD_RECORD_OVERFLOW; sl@0: SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_COMPRESSED_LENGTH_TOO_LONG); sl@0: goto f_err; sl@0: } sl@0: if (!ssl3_do_uncompress(s)) sl@0: { sl@0: al=SSL_AD_DECOMPRESSION_FAILURE; sl@0: SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_BAD_DECOMPRESSION); sl@0: goto f_err; sl@0: } sl@0: } sl@0: sl@0: if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH+extra) sl@0: { sl@0: al=SSL_AD_RECORD_OVERFLOW; sl@0: SSLerr(SSL_F_SSL3_GET_RECORD,SSL_R_DATA_LENGTH_TOO_LONG); sl@0: goto f_err; sl@0: } sl@0: sl@0: rr->off=0; sl@0: /* So at this point the following is true sl@0: * ssl->s3->rrec.type is the type of record sl@0: * ssl->s3->rrec.length == number of bytes in record sl@0: * ssl->s3->rrec.off == offset to first valid byte sl@0: * ssl->s3->rrec.data == where to take bytes from, increment sl@0: * after use :-). sl@0: */ sl@0: sl@0: /* we have pulled in a full packet so zero things */ sl@0: s->packet_length=0; sl@0: sl@0: /* just read a 0 length packet */ sl@0: if (rr->length == 0) goto again; sl@0: sl@0: return(1); sl@0: sl@0: f_err: sl@0: ssl3_send_alert(s,SSL3_AL_FATAL,al); sl@0: err: sl@0: return(ret); sl@0: } sl@0: sl@0: int ssl3_do_uncompress(SSL *ssl) sl@0: { sl@0: #ifndef OPENSSL_NO_COMP sl@0: int i; sl@0: SSL3_RECORD *rr; sl@0: sl@0: rr= &(ssl->s3->rrec); sl@0: i=COMP_expand_block(ssl->expand,rr->comp, sl@0: SSL3_RT_MAX_PLAIN_LENGTH,rr->data,(int)rr->length); sl@0: if (i < 0) sl@0: return(0); sl@0: else sl@0: rr->length=i; sl@0: rr->data=rr->comp; sl@0: #endif sl@0: return(1); sl@0: } sl@0: sl@0: int ssl3_do_compress(SSL *ssl) sl@0: { sl@0: #ifndef OPENSSL_NO_COMP sl@0: int i; sl@0: SSL3_RECORD *wr; sl@0: sl@0: wr= &(ssl->s3->wrec); sl@0: i=COMP_compress_block(ssl->compress,wr->data, sl@0: SSL3_RT_MAX_COMPRESSED_LENGTH, sl@0: wr->input,(int)wr->length); sl@0: if (i < 0) sl@0: return(0); sl@0: else sl@0: wr->length=i; sl@0: sl@0: wr->input=wr->data; sl@0: #endif sl@0: return(1); sl@0: } sl@0: sl@0: /* Call this to write data in records of type 'type' sl@0: * It will return <= 0 if not all data has been sent or non-blocking IO. sl@0: */ sl@0: int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len) sl@0: { sl@0: const unsigned char *buf=buf_; sl@0: unsigned int tot,n,nw; sl@0: int i; sl@0: sl@0: s->rwstate=SSL_NOTHING; sl@0: tot=s->s3->wnum; sl@0: s->s3->wnum=0; sl@0: sl@0: if (SSL_in_init(s) && !s->in_handshake) sl@0: { sl@0: i=s->handshake_func(s); sl@0: if (i < 0) return(i); sl@0: if (i == 0) sl@0: { sl@0: SSLerr(SSL_F_SSL3_WRITE_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE); sl@0: return -1; sl@0: } sl@0: } sl@0: sl@0: n=(len-tot); sl@0: for (;;) sl@0: { sl@0: if (n > SSL3_RT_MAX_PLAIN_LENGTH) sl@0: nw=SSL3_RT_MAX_PLAIN_LENGTH; sl@0: else sl@0: nw=n; sl@0: sl@0: i=do_ssl3_write(s, type, &(buf[tot]), nw, 0); sl@0: if (i <= 0) sl@0: { sl@0: s->s3->wnum=tot; sl@0: return i; sl@0: } sl@0: sl@0: if ((i == (int)n) || sl@0: (type == SSL3_RT_APPLICATION_DATA && sl@0: (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) sl@0: { sl@0: /* next chunk of data should get another prepended empty fragment sl@0: * in ciphersuites with known-IV weakness: */ sl@0: s->s3->empty_fragment_done = 0; sl@0: sl@0: return tot+i; sl@0: } sl@0: sl@0: n-=i; sl@0: tot+=i; sl@0: } sl@0: } sl@0: sl@0: static int do_ssl3_write(SSL *s, int type, const unsigned char *buf, sl@0: unsigned int len, int create_empty_fragment) sl@0: { sl@0: unsigned char *p,*plen; sl@0: int i,mac_size,clear=0; sl@0: int prefix_len = 0; sl@0: SSL3_RECORD *wr; sl@0: SSL3_BUFFER *wb; sl@0: SSL_SESSION *sess; sl@0: sl@0: /* first check if there is a SSL3_BUFFER still being written sl@0: * out. This will happen with non blocking IO */ sl@0: if (s->s3->wbuf.left != 0) sl@0: return(ssl3_write_pending(s,type,buf,len)); sl@0: sl@0: /* If we have an alert to send, lets send it */ sl@0: if (s->s3->alert_dispatch) sl@0: { sl@0: i=s->method->ssl_dispatch_alert(s); sl@0: if (i <= 0) sl@0: return(i); sl@0: /* if it went, fall through and send more stuff */ sl@0: } sl@0: sl@0: if (len == 0 && !create_empty_fragment) sl@0: return 0; sl@0: sl@0: wr= &(s->s3->wrec); sl@0: wb= &(s->s3->wbuf); sl@0: sess=s->session; sl@0: sl@0: if ( (sess == NULL) || sl@0: (s->enc_write_ctx == NULL) || sl@0: (s->write_hash == NULL)) sl@0: clear=1; sl@0: sl@0: if (clear) sl@0: mac_size=0; sl@0: else sl@0: mac_size=EVP_MD_size(s->write_hash); sl@0: sl@0: /* 'create_empty_fragment' is true only when this function calls itself */ sl@0: if (!clear && !create_empty_fragment && !s->s3->empty_fragment_done) sl@0: { sl@0: /* countermeasure against known-IV weakness in CBC ciphersuites sl@0: * (see http://www.openssl.org/~bodo/tls-cbc.txt) */ sl@0: sl@0: if (s->s3->need_empty_fragments && type == SSL3_RT_APPLICATION_DATA) sl@0: { sl@0: /* recursive function call with 'create_empty_fragment' set; sl@0: * this prepares and buffers the data for an empty fragment sl@0: * (these 'prefix_len' bytes are sent out later sl@0: * together with the actual payload) */ sl@0: prefix_len = do_ssl3_write(s, type, buf, 0, 1); sl@0: if (prefix_len <= 0) sl@0: goto err; sl@0: sl@0: if (s->s3->wbuf.len < (size_t)prefix_len + SSL3_RT_MAX_PACKET_SIZE) sl@0: { sl@0: /* insufficient space */ sl@0: SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR); sl@0: goto err; sl@0: } sl@0: } sl@0: sl@0: s->s3->empty_fragment_done = 1; sl@0: } sl@0: sl@0: p = wb->buf + prefix_len; sl@0: sl@0: /* write the header */ sl@0: sl@0: *(p++)=type&0xff; sl@0: wr->type=type; sl@0: sl@0: *(p++)=(s->version>>8); sl@0: *(p++)=s->version&0xff; sl@0: sl@0: /* field where we are to write out packet length */ sl@0: plen=p; sl@0: p+=2; sl@0: sl@0: /* lets setup the record stuff. */ sl@0: wr->data=p; sl@0: wr->length=(int)len; sl@0: wr->input=(unsigned char *)buf; sl@0: sl@0: /* we now 'read' from wr->input, wr->length bytes into sl@0: * wr->data */ sl@0: sl@0: /* first we compress */ sl@0: if (s->compress != NULL) sl@0: { sl@0: if (!ssl3_do_compress(s)) sl@0: { sl@0: SSLerr(SSL_F_DO_SSL3_WRITE,SSL_R_COMPRESSION_FAILURE); sl@0: goto err; sl@0: } sl@0: } sl@0: else sl@0: { sl@0: memcpy(wr->data,wr->input,wr->length); sl@0: wr->input=wr->data; sl@0: } sl@0: sl@0: /* we should still have the output to wr->data and the input sl@0: * from wr->input. Length should be wr->length. sl@0: * wr->data still points in the wb->buf */ sl@0: sl@0: if (mac_size != 0) sl@0: { sl@0: s->method->ssl3_enc->mac(s,&(p[wr->length]),1); sl@0: wr->length+=mac_size; sl@0: wr->input=p; sl@0: wr->data=p; sl@0: } sl@0: sl@0: /* ssl3_enc can only have an error on read */ sl@0: s->method->ssl3_enc->enc(s,1); sl@0: sl@0: /* record length after mac and block padding */ sl@0: s2n(wr->length,plen); sl@0: sl@0: /* we should now have sl@0: * wr->data pointing to the encrypted data, which is sl@0: * wr->length long */ sl@0: wr->type=type; /* not needed but helps for debugging */ sl@0: wr->length+=SSL3_RT_HEADER_LENGTH; sl@0: sl@0: if (create_empty_fragment) sl@0: { sl@0: /* we are in a recursive call; sl@0: * just return the length, don't write out anything here sl@0: */ sl@0: return wr->length; sl@0: } sl@0: sl@0: /* now let's set up wb */ sl@0: wb->left = prefix_len + wr->length; sl@0: wb->offset = 0; sl@0: sl@0: /* memorize arguments so that ssl3_write_pending can detect bad write retries later */ sl@0: s->s3->wpend_tot=len; sl@0: s->s3->wpend_buf=buf; sl@0: s->s3->wpend_type=type; sl@0: s->s3->wpend_ret=len; sl@0: sl@0: /* we now just need to write the buffer */ sl@0: return ssl3_write_pending(s,type,buf,len); sl@0: err: sl@0: return -1; sl@0: } sl@0: sl@0: /* if s->s3->wbuf.left != 0, we need to call this */ sl@0: int ssl3_write_pending(SSL *s, int type, const unsigned char *buf, sl@0: unsigned int len) sl@0: { sl@0: int i; sl@0: sl@0: /* XXXX */ sl@0: if ((s->s3->wpend_tot > (int)len) sl@0: || ((s->s3->wpend_buf != buf) && sl@0: !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)) sl@0: || (s->s3->wpend_type != type)) sl@0: { sl@0: SSLerr(SSL_F_SSL3_WRITE_PENDING,SSL_R_BAD_WRITE_RETRY); sl@0: return(-1); sl@0: } sl@0: sl@0: for (;;) sl@0: { sl@0: clear_sys_error(); sl@0: if (s->wbio != NULL) sl@0: { sl@0: s->rwstate=SSL_WRITING; sl@0: i=BIO_write(s->wbio, sl@0: (char *)&(s->s3->wbuf.buf[s->s3->wbuf.offset]), sl@0: (unsigned int)s->s3->wbuf.left); sl@0: } sl@0: else sl@0: { sl@0: SSLerr(SSL_F_SSL3_WRITE_PENDING,SSL_R_BIO_NOT_SET); sl@0: i= -1; sl@0: } sl@0: if (i == s->s3->wbuf.left) sl@0: { sl@0: s->s3->wbuf.left=0; sl@0: s->rwstate=SSL_NOTHING; sl@0: return(s->s3->wpend_ret); sl@0: } sl@0: else if (i <= 0) sl@0: return(i); sl@0: s->s3->wbuf.offset+=i; sl@0: s->s3->wbuf.left-=i; sl@0: } sl@0: } sl@0: sl@0: /* Return up to 'len' payload bytes received in 'type' records. sl@0: * 'type' is one of the following: sl@0: * sl@0: * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us) sl@0: * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us) sl@0: * - 0 (during a shutdown, no data has to be returned) sl@0: * sl@0: * If we don't have stored data to work from, read a SSL/TLS record first sl@0: * (possibly multiple records if we still don't have anything to return). sl@0: * sl@0: * This function must handle any surprises the peer may have for us, such as sl@0: * Alert records (e.g. close_notify), ChangeCipherSpec records (not really sl@0: * a surprise, but handled as if it were), or renegotiation requests. sl@0: * Also if record payloads contain fragments too small to process, we store sl@0: * them until there is enough for the respective protocol (the record protocol sl@0: * may use arbitrary fragmentation and even interleaving): sl@0: * Change cipher spec protocol sl@0: * just 1 byte needed, no need for keeping anything stored sl@0: * Alert protocol sl@0: * 2 bytes needed (AlertLevel, AlertDescription) sl@0: * Handshake protocol sl@0: * 4 bytes needed (HandshakeType, uint24 length) -- we just have sl@0: * to detect unexpected Client Hello and Hello Request messages sl@0: * here, anything else is handled by higher layers sl@0: * Application data protocol sl@0: * none of our business sl@0: */ sl@0: int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek) sl@0: { sl@0: int al,i,j,ret; sl@0: unsigned int n; sl@0: SSL3_RECORD *rr; sl@0: void (*cb)(const SSL *ssl,int type2,int val)=NULL; sl@0: sl@0: if (s->s3->rbuf.buf == NULL) /* Not initialized yet */ sl@0: if (!ssl3_setup_buffers(s)) sl@0: return(-1); sl@0: sl@0: if ((type && (type != SSL3_RT_APPLICATION_DATA) && (type != SSL3_RT_HANDSHAKE) && type) || sl@0: (peek && (type != SSL3_RT_APPLICATION_DATA))) sl@0: { sl@0: SSLerr(SSL_F_SSL3_READ_BYTES, ERR_R_INTERNAL_ERROR); sl@0: return -1; sl@0: } sl@0: sl@0: if ((type == SSL3_RT_HANDSHAKE) && (s->s3->handshake_fragment_len > 0)) sl@0: /* (partially) satisfy request from storage */ sl@0: { sl@0: unsigned char *src = s->s3->handshake_fragment; sl@0: unsigned char *dst = buf; sl@0: unsigned int k; sl@0: sl@0: /* peek == 0 */ sl@0: n = 0; sl@0: while ((len > 0) && (s->s3->handshake_fragment_len > 0)) sl@0: { sl@0: *dst++ = *src++; sl@0: len--; s->s3->handshake_fragment_len--; sl@0: n++; sl@0: } sl@0: /* move any remaining fragment bytes: */ sl@0: for (k = 0; k < s->s3->handshake_fragment_len; k++) sl@0: s->s3->handshake_fragment[k] = *src++; sl@0: return n; sl@0: } sl@0: sl@0: /* Now s->s3->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE. */ sl@0: sl@0: if (!s->in_handshake && SSL_in_init(s)) sl@0: { sl@0: /* type == SSL3_RT_APPLICATION_DATA */ sl@0: i=s->handshake_func(s); sl@0: if (i < 0) return(i); sl@0: if (i == 0) sl@0: { sl@0: SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE); sl@0: return(-1); sl@0: } sl@0: } sl@0: start: sl@0: s->rwstate=SSL_NOTHING; sl@0: sl@0: /* s->s3->rrec.type - is the type of record sl@0: * s->s3->rrec.data, - data sl@0: * s->s3->rrec.off, - offset into 'data' for next read sl@0: * s->s3->rrec.length, - number of bytes. */ sl@0: rr = &(s->s3->rrec); sl@0: sl@0: /* get new packet if necessary */ sl@0: if ((rr->length == 0) || (s->rstate == SSL_ST_READ_BODY)) sl@0: { sl@0: ret=ssl3_get_record(s); sl@0: if (ret <= 0) return(ret); sl@0: } sl@0: sl@0: /* we now have a packet which can be read and processed */ sl@0: sl@0: if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec, sl@0: * reset by ssl3_get_finished */ sl@0: && (rr->type != SSL3_RT_HANDSHAKE)) sl@0: { sl@0: al=SSL_AD_UNEXPECTED_MESSAGE; sl@0: SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_DATA_BETWEEN_CCS_AND_FINISHED); sl@0: goto f_err; sl@0: } sl@0: sl@0: /* If the other end has shut down, throw anything we read away sl@0: * (even in 'peek' mode) */ sl@0: if (s->shutdown & SSL_RECEIVED_SHUTDOWN) sl@0: { sl@0: rr->length=0; sl@0: s->rwstate=SSL_NOTHING; sl@0: return(0); sl@0: } sl@0: sl@0: sl@0: if (type == rr->type) /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */ sl@0: { sl@0: /* make sure that we are not getting application data when we sl@0: * are doing a handshake for the first time */ sl@0: if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) && sl@0: (s->enc_read_ctx == NULL)) sl@0: { sl@0: al=SSL_AD_UNEXPECTED_MESSAGE; sl@0: SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_APP_DATA_IN_HANDSHAKE); sl@0: goto f_err; sl@0: } sl@0: sl@0: if (len <= 0) return(len); sl@0: sl@0: if ((unsigned int)len > rr->length) sl@0: n = rr->length; sl@0: else sl@0: n = (unsigned int)len; sl@0: sl@0: memcpy(buf,&(rr->data[rr->off]),n); sl@0: if (!peek) sl@0: { sl@0: rr->length-=n; sl@0: rr->off+=n; sl@0: if (rr->length == 0) sl@0: { sl@0: s->rstate=SSL_ST_READ_HEADER; sl@0: rr->off=0; sl@0: } sl@0: } sl@0: return(n); sl@0: } sl@0: sl@0: sl@0: /* If we get here, then type != rr->type; if we have a handshake sl@0: * message, then it was unexpected (Hello Request or Client Hello). */ sl@0: sl@0: /* In case of record types for which we have 'fragment' storage, sl@0: * fill that so that we can process the data at a fixed place. sl@0: */ sl@0: { sl@0: unsigned int dest_maxlen = 0; sl@0: unsigned char *dest = NULL; sl@0: unsigned int *dest_len = NULL; sl@0: sl@0: if (rr->type == SSL3_RT_HANDSHAKE) sl@0: { sl@0: dest_maxlen = sizeof s->s3->handshake_fragment; sl@0: dest = s->s3->handshake_fragment; sl@0: dest_len = &s->s3->handshake_fragment_len; sl@0: } sl@0: else if (rr->type == SSL3_RT_ALERT) sl@0: { sl@0: dest_maxlen = sizeof s->s3->alert_fragment; sl@0: dest = s->s3->alert_fragment; sl@0: dest_len = &s->s3->alert_fragment_len; sl@0: } sl@0: sl@0: if (dest_maxlen > 0) sl@0: { sl@0: n = dest_maxlen - *dest_len; /* available space in 'dest' */ sl@0: if (rr->length < n) sl@0: n = rr->length; /* available bytes */ sl@0: sl@0: /* now move 'n' bytes: */ sl@0: while (n-- > 0) sl@0: { sl@0: dest[(*dest_len)++] = rr->data[rr->off++]; sl@0: rr->length--; sl@0: } sl@0: sl@0: if (*dest_len < dest_maxlen) sl@0: goto start; /* fragment was too small */ sl@0: } sl@0: } sl@0: sl@0: /* s->s3->handshake_fragment_len == 4 iff rr->type == SSL3_RT_HANDSHAKE; sl@0: * s->s3->alert_fragment_len == 2 iff rr->type == SSL3_RT_ALERT. sl@0: * (Possibly rr is 'empty' now, i.e. rr->length may be 0.) */ sl@0: sl@0: /* If we are a client, check for an incoming 'Hello Request': */ sl@0: if ((!s->server) && sl@0: (s->s3->handshake_fragment_len >= 4) && sl@0: (s->s3->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST) && sl@0: (s->session != NULL) && (s->session->cipher != NULL)) sl@0: { sl@0: s->s3->handshake_fragment_len = 0; sl@0: sl@0: if ((s->s3->handshake_fragment[1] != 0) || sl@0: (s->s3->handshake_fragment[2] != 0) || sl@0: (s->s3->handshake_fragment[3] != 0)) sl@0: { sl@0: al=SSL_AD_DECODE_ERROR; sl@0: SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_BAD_HELLO_REQUEST); sl@0: goto f_err; sl@0: } sl@0: sl@0: if (s->msg_callback) sl@0: s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->s3->handshake_fragment, 4, s, s->msg_callback_arg); sl@0: sl@0: if (SSL_is_init_finished(s) && sl@0: !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) && sl@0: !s->s3->renegotiate) sl@0: { sl@0: ssl3_renegotiate(s); sl@0: if (ssl3_renegotiate_check(s)) sl@0: { sl@0: i=s->handshake_func(s); sl@0: if (i < 0) return(i); sl@0: if (i == 0) sl@0: { sl@0: SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE); sl@0: return(-1); sl@0: } sl@0: sl@0: if (!(s->mode & SSL_MODE_AUTO_RETRY)) sl@0: { sl@0: if (s->s3->rbuf.left == 0) /* no read-ahead left? */ sl@0: { sl@0: BIO *bio; sl@0: /* In the case where we try to read application data, sl@0: * but we trigger an SSL handshake, we return -1 with sl@0: * the retry option set. Otherwise renegotiation may sl@0: * cause nasty problems in the blocking world */ sl@0: s->rwstate=SSL_READING; sl@0: bio=SSL_get_rbio(s); sl@0: BIO_clear_retry_flags(bio); sl@0: BIO_set_retry_read(bio); sl@0: return(-1); sl@0: } sl@0: } sl@0: } sl@0: } sl@0: /* we either finished a handshake or ignored the request, sl@0: * now try again to obtain the (application) data we were asked for */ sl@0: goto start; sl@0: } sl@0: sl@0: if (s->s3->alert_fragment_len >= 2) sl@0: { sl@0: int alert_level = s->s3->alert_fragment[0]; sl@0: int alert_descr = s->s3->alert_fragment[1]; sl@0: sl@0: s->s3->alert_fragment_len = 0; sl@0: sl@0: if (s->msg_callback) sl@0: s->msg_callback(0, s->version, SSL3_RT_ALERT, s->s3->alert_fragment, 2, s, s->msg_callback_arg); sl@0: sl@0: if (s->info_callback != NULL) sl@0: cb=s->info_callback; sl@0: else if (s->ctx->info_callback != NULL) sl@0: cb=s->ctx->info_callback; sl@0: sl@0: if (cb != NULL) sl@0: { sl@0: j = (alert_level << 8) | alert_descr; sl@0: cb(s, SSL_CB_READ_ALERT, j); sl@0: } sl@0: sl@0: if (alert_level == 1) /* warning */ sl@0: { sl@0: s->s3->warn_alert = alert_descr; sl@0: if (alert_descr == SSL_AD_CLOSE_NOTIFY) sl@0: { sl@0: s->shutdown |= SSL_RECEIVED_SHUTDOWN; sl@0: return(0); sl@0: } sl@0: } sl@0: else if (alert_level == 2) /* fatal */ sl@0: { sl@0: char tmp[16]; sl@0: sl@0: s->rwstate=SSL_NOTHING; sl@0: s->s3->fatal_alert = alert_descr; sl@0: SSLerr(SSL_F_SSL3_READ_BYTES, SSL_AD_REASON_OFFSET + alert_descr); sl@0: BIO_snprintf(tmp,sizeof tmp,"%d",alert_descr); sl@0: ERR_add_error_data(2,"SSL alert number ",tmp); sl@0: s->shutdown|=SSL_RECEIVED_SHUTDOWN; sl@0: SSL_CTX_remove_session(s->ctx,s->session); sl@0: return(0); sl@0: } sl@0: else sl@0: { sl@0: al=SSL_AD_ILLEGAL_PARAMETER; sl@0: SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_UNKNOWN_ALERT_TYPE); sl@0: goto f_err; sl@0: } sl@0: sl@0: goto start; sl@0: } sl@0: sl@0: if (s->shutdown & SSL_SENT_SHUTDOWN) /* but we have not received a shutdown */ sl@0: { sl@0: s->rwstate=SSL_NOTHING; sl@0: rr->length=0; sl@0: return(0); sl@0: } sl@0: sl@0: if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) sl@0: { sl@0: /* 'Change Cipher Spec' is just a single byte, so we know sl@0: * exactly what the record payload has to look like */ sl@0: if ( (rr->length != 1) || (rr->off != 0) || sl@0: (rr->data[0] != SSL3_MT_CCS)) sl@0: { sl@0: al=SSL_AD_ILLEGAL_PARAMETER; sl@0: SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_BAD_CHANGE_CIPHER_SPEC); sl@0: goto f_err; sl@0: } sl@0: sl@0: /* Check we have a cipher to change to */ sl@0: if (s->s3->tmp.new_cipher == NULL) sl@0: { sl@0: al=SSL_AD_UNEXPECTED_MESSAGE; sl@0: SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_CCS_RECEIVED_EARLY); sl@0: goto f_err; sl@0: } sl@0: sl@0: rr->length=0; sl@0: sl@0: if (s->msg_callback) sl@0: s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, 1, s, s->msg_callback_arg); sl@0: sl@0: s->s3->change_cipher_spec=1; sl@0: if (!ssl3_do_change_cipher_spec(s)) sl@0: goto err; sl@0: else sl@0: goto start; sl@0: } sl@0: sl@0: /* Unexpected handshake message (Client Hello, or protocol violation) */ sl@0: if ((s->s3->handshake_fragment_len >= 4) && !s->in_handshake) sl@0: { sl@0: if (((s->state&SSL_ST_MASK) == SSL_ST_OK) && sl@0: !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)) sl@0: { sl@0: #if 0 /* worked only because C operator preferences are not as expected (and sl@0: * because this is not really needed for clients except for detecting sl@0: * protocol violations): */ sl@0: s->state=SSL_ST_BEFORE|(s->server) sl@0: ?SSL_ST_ACCEPT sl@0: :SSL_ST_CONNECT; sl@0: #else sl@0: s->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT; sl@0: #endif sl@0: s->new_session=1; sl@0: } sl@0: i=s->handshake_func(s); sl@0: if (i < 0) return(i); sl@0: if (i == 0) sl@0: { sl@0: SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_SSL_HANDSHAKE_FAILURE); sl@0: return(-1); sl@0: } sl@0: sl@0: if (!(s->mode & SSL_MODE_AUTO_RETRY)) sl@0: { sl@0: if (s->s3->rbuf.left == 0) /* no read-ahead left? */ sl@0: { sl@0: BIO *bio; sl@0: /* In the case where we try to read application data, sl@0: * but we trigger an SSL handshake, we return -1 with sl@0: * the retry option set. Otherwise renegotiation may sl@0: * cause nasty problems in the blocking world */ sl@0: s->rwstate=SSL_READING; sl@0: bio=SSL_get_rbio(s); sl@0: BIO_clear_retry_flags(bio); sl@0: BIO_set_retry_read(bio); sl@0: return(-1); sl@0: } sl@0: } sl@0: goto start; sl@0: } sl@0: sl@0: switch (rr->type) sl@0: { sl@0: default: sl@0: #ifndef OPENSSL_NO_TLS sl@0: /* TLS just ignores unknown message types */ sl@0: if (s->version == TLS1_VERSION) sl@0: { sl@0: rr->length = 0; sl@0: goto start; sl@0: } sl@0: #endif sl@0: al=SSL_AD_UNEXPECTED_MESSAGE; sl@0: SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_UNEXPECTED_RECORD); sl@0: goto f_err; sl@0: case SSL3_RT_CHANGE_CIPHER_SPEC: sl@0: case SSL3_RT_ALERT: sl@0: case SSL3_RT_HANDSHAKE: sl@0: /* we already handled all of these, with the possible exception sl@0: * of SSL3_RT_HANDSHAKE when s->in_handshake is set, but that sl@0: * should not happen when type != rr->type */ sl@0: al=SSL_AD_UNEXPECTED_MESSAGE; sl@0: SSLerr(SSL_F_SSL3_READ_BYTES,ERR_R_INTERNAL_ERROR); sl@0: goto f_err; sl@0: case SSL3_RT_APPLICATION_DATA: sl@0: /* At this point, we were expecting handshake data, sl@0: * but have application data. If the library was sl@0: * running inside ssl3_read() (i.e. in_read_app_data sl@0: * is set) and it makes sense to read application data sl@0: * at this point (session renegotiation not yet started), sl@0: * we will indulge it. sl@0: */ sl@0: if (s->s3->in_read_app_data && sl@0: (s->s3->total_renegotiations != 0) && sl@0: (( sl@0: (s->state & SSL_ST_CONNECT) && sl@0: (s->state >= SSL3_ST_CW_CLNT_HELLO_A) && sl@0: (s->state <= SSL3_ST_CR_SRVR_HELLO_A) sl@0: ) || ( sl@0: (s->state & SSL_ST_ACCEPT) && sl@0: (s->state <= SSL3_ST_SW_HELLO_REQ_A) && sl@0: (s->state >= SSL3_ST_SR_CLNT_HELLO_A) sl@0: ) sl@0: )) sl@0: { sl@0: s->s3->in_read_app_data=2; sl@0: return(-1); sl@0: } sl@0: else sl@0: { sl@0: al=SSL_AD_UNEXPECTED_MESSAGE; sl@0: SSLerr(SSL_F_SSL3_READ_BYTES,SSL_R_UNEXPECTED_RECORD); sl@0: goto f_err; sl@0: } sl@0: } sl@0: /* not reached */ sl@0: sl@0: f_err: sl@0: ssl3_send_alert(s,SSL3_AL_FATAL,al); sl@0: err: sl@0: return(-1); sl@0: } sl@0: sl@0: int ssl3_do_change_cipher_spec(SSL *s) sl@0: { sl@0: int i; sl@0: const char *sender; sl@0: int slen; sl@0: sl@0: if (s->state & SSL_ST_ACCEPT) sl@0: i=SSL3_CHANGE_CIPHER_SERVER_READ; sl@0: else sl@0: i=SSL3_CHANGE_CIPHER_CLIENT_READ; sl@0: sl@0: if (s->s3->tmp.key_block == NULL) sl@0: { sl@0: s->session->cipher=s->s3->tmp.new_cipher; sl@0: if (!s->method->ssl3_enc->setup_key_block(s)) return(0); sl@0: } sl@0: sl@0: if (!s->method->ssl3_enc->change_cipher_state(s,i)) sl@0: return(0); sl@0: sl@0: /* we have to record the message digest at sl@0: * this point so we can get it before we read sl@0: * the finished message */ sl@0: if (s->state & SSL_ST_CONNECT) sl@0: { sl@0: sender=s->method->ssl3_enc->server_finished_label; sl@0: slen=s->method->ssl3_enc->server_finished_label_len; sl@0: } sl@0: else sl@0: { sl@0: sender=s->method->ssl3_enc->client_finished_label; sl@0: slen=s->method->ssl3_enc->client_finished_label_len; sl@0: } sl@0: sl@0: s->s3->tmp.peer_finish_md_len = s->method->ssl3_enc->final_finish_mac(s, sl@0: &(s->s3->finish_dgst1), sl@0: &(s->s3->finish_dgst2), sl@0: sender,slen,s->s3->tmp.peer_finish_md); sl@0: sl@0: return(1); sl@0: } sl@0: sl@0: void ssl3_send_alert(SSL *s, int level, int desc) sl@0: { sl@0: /* Map tls/ssl alert value to correct one */ sl@0: desc=s->method->ssl3_enc->alert_value(desc); sl@0: if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION) sl@0: desc = SSL_AD_HANDSHAKE_FAILURE; /* SSL 3.0 does not have protocol_version alerts */ sl@0: if (desc < 0) return; sl@0: /* If a fatal one, remove from cache */ sl@0: if ((level == 2) && (s->session != NULL)) sl@0: SSL_CTX_remove_session(s->ctx,s->session); sl@0: sl@0: s->s3->alert_dispatch=1; sl@0: s->s3->send_alert[0]=level; sl@0: s->s3->send_alert[1]=desc; sl@0: if (s->s3->wbuf.left == 0) /* data still being written out? */ sl@0: s->method->ssl_dispatch_alert(s); sl@0: /* else data is still being written out, we will get written sl@0: * some time in the future */ sl@0: } sl@0: sl@0: int ssl3_dispatch_alert(SSL *s) sl@0: { sl@0: int i,j; sl@0: void (*cb)(const SSL *ssl,int type,int val)=NULL; sl@0: sl@0: s->s3->alert_dispatch=0; sl@0: i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], 2, 0); sl@0: if (i <= 0) sl@0: { sl@0: s->s3->alert_dispatch=1; sl@0: } sl@0: else sl@0: { sl@0: /* Alert sent to BIO. If it is important, flush it now. sl@0: * If the message does not get sent due to non-blocking IO, sl@0: * we will not worry too much. */ sl@0: if (s->s3->send_alert[0] == SSL3_AL_FATAL) sl@0: (void)BIO_flush(s->wbio); sl@0: sl@0: if (s->msg_callback) sl@0: s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 2, s, s->msg_callback_arg); sl@0: sl@0: if (s->info_callback != NULL) sl@0: cb=s->info_callback; sl@0: else if (s->ctx->info_callback != NULL) sl@0: cb=s->ctx->info_callback; sl@0: sl@0: if (cb != NULL) sl@0: { sl@0: j=(s->s3->send_alert[0]<<8)|s->s3->send_alert[1]; sl@0: cb(s,SSL_CB_WRITE_ALERT,j); sl@0: } sl@0: } sl@0: return(i); sl@0: }