sl@0: /* ssl/s2_srvr.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-2001 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: © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. sl@0: */ sl@0: sl@0: #include "ssl_locl.h" sl@0: #ifndef OPENSSL_NO_SSL2 sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__))) sl@0: #include "libssl_wsd.h" sl@0: #endif sl@0: sl@0: #ifdef EMULATOR sl@0: sl@0: GET_STATIC_VAR_FROM_TLS(SSLv2_server_method_data,s2_srvr,SSL_METHOD) sl@0: sl@0: #define SSLv2_server_method_data (*GET_WSD_VAR_NAME(SSLv2_server_method_data,s2_srvr,s)()) sl@0: sl@0: #endif sl@0: static SSL_METHOD *ssl2_get_server_method(int ver); sl@0: static int get_client_master_key(SSL *s); sl@0: static int get_client_hello(SSL *s); sl@0: static int server_hello(SSL *s); sl@0: static int get_client_finished(SSL *s); sl@0: static int server_verify(SSL *s); sl@0: static int server_finish(SSL *s); sl@0: static int request_certificate(SSL *s); sl@0: static int ssl_rsa_private_decrypt(CERT *c, int len, unsigned char *from, sl@0: unsigned char *to,int padding); sl@0: #define BREAK break sl@0: sl@0: static SSL_METHOD *ssl2_get_server_method(int ver) sl@0: { sl@0: if (ver == SSL2_VERSION) sl@0: return(SSLv2_server_method()); sl@0: else sl@0: return(NULL); sl@0: } sl@0: sl@0: EXPORT_C IMPLEMENT_ssl2_meth_func(SSLv2_server_method, sl@0: ssl2_accept, sl@0: ssl_undefined_function, sl@0: ssl2_get_server_method) sl@0: sl@0: int ssl2_accept(SSL *s) sl@0: { sl@0: unsigned long l=(unsigned long)time(NULL); sl@0: BUF_MEM *buf=NULL; sl@0: int ret= -1; sl@0: long num1; sl@0: void (*cb)(const SSL *ssl,int type,int val)=NULL; sl@0: int new_state,state; sl@0: sl@0: RAND_add(&l,sizeof(l),0); sl@0: ERR_clear_error(); sl@0: clear_sys_error(); 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: /* init things to blank */ sl@0: s->in_handshake++; sl@0: if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); sl@0: sl@0: if (s->cert == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL2_ACCEPT,SSL_R_NO_CERTIFICATE_SET); sl@0: return(-1); sl@0: } sl@0: sl@0: clear_sys_error(); sl@0: for (;;) sl@0: { sl@0: state=s->state; sl@0: sl@0: switch (s->state) sl@0: { sl@0: case SSL_ST_BEFORE: sl@0: case SSL_ST_ACCEPT: sl@0: case SSL_ST_BEFORE|SSL_ST_ACCEPT: sl@0: case SSL_ST_OK|SSL_ST_ACCEPT: sl@0: sl@0: s->server=1; sl@0: if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); sl@0: sl@0: s->version=SSL2_VERSION; sl@0: s->type=SSL_ST_ACCEPT; sl@0: sl@0: buf=s->init_buf; sl@0: if ((buf == NULL) && ((buf=BUF_MEM_new()) == NULL)) sl@0: { ret= -1; goto end; } sl@0: if (!BUF_MEM_grow(buf,(int) sl@0: SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)) sl@0: { ret= -1; goto end; } sl@0: s->init_buf=buf; sl@0: s->init_num=0; sl@0: s->ctx->stats.sess_accept++; sl@0: s->handshake_func=ssl2_accept; sl@0: s->state=SSL2_ST_GET_CLIENT_HELLO_A; sl@0: BREAK; sl@0: sl@0: case SSL2_ST_GET_CLIENT_HELLO_A: sl@0: case SSL2_ST_GET_CLIENT_HELLO_B: sl@0: case SSL2_ST_GET_CLIENT_HELLO_C: sl@0: s->shutdown=0; sl@0: ret=get_client_hello(s); sl@0: if (ret <= 0) goto end; sl@0: s->init_num=0; sl@0: s->state=SSL2_ST_SEND_SERVER_HELLO_A; sl@0: BREAK; sl@0: sl@0: case SSL2_ST_SEND_SERVER_HELLO_A: sl@0: case SSL2_ST_SEND_SERVER_HELLO_B: sl@0: ret=server_hello(s); sl@0: if (ret <= 0) goto end; sl@0: s->init_num=0; sl@0: if (!s->hit) sl@0: { sl@0: s->state=SSL2_ST_GET_CLIENT_MASTER_KEY_A; sl@0: BREAK; sl@0: } sl@0: else sl@0: { sl@0: s->state=SSL2_ST_SERVER_START_ENCRYPTION; sl@0: BREAK; sl@0: } sl@0: case SSL2_ST_GET_CLIENT_MASTER_KEY_A: sl@0: case SSL2_ST_GET_CLIENT_MASTER_KEY_B: sl@0: ret=get_client_master_key(s); sl@0: if (ret <= 0) goto end; sl@0: s->init_num=0; sl@0: s->state=SSL2_ST_SERVER_START_ENCRYPTION; sl@0: BREAK; sl@0: sl@0: case SSL2_ST_SERVER_START_ENCRYPTION: sl@0: /* Ok we how have sent all the stuff needed to sl@0: * start encrypting, the next packet back will sl@0: * be encrypted. */ sl@0: if (!ssl2_enc_init(s,0)) sl@0: { ret= -1; goto end; } sl@0: s->s2->clear_text=0; sl@0: s->state=SSL2_ST_SEND_SERVER_VERIFY_A; sl@0: BREAK; sl@0: sl@0: case SSL2_ST_SEND_SERVER_VERIFY_A: sl@0: case SSL2_ST_SEND_SERVER_VERIFY_B: sl@0: ret=server_verify(s); sl@0: if (ret <= 0) goto end; sl@0: s->init_num=0; sl@0: if (s->hit) sl@0: { sl@0: /* If we are in here, we have been sl@0: * buffering the output, so we need to sl@0: * flush it and remove buffering from sl@0: * future traffic */ sl@0: s->state=SSL2_ST_SEND_SERVER_VERIFY_C; sl@0: BREAK; sl@0: } sl@0: else sl@0: { sl@0: s->state=SSL2_ST_GET_CLIENT_FINISHED_A; sl@0: break; sl@0: } sl@0: sl@0: case SSL2_ST_SEND_SERVER_VERIFY_C: sl@0: /* get the number of bytes to write */ sl@0: num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL); sl@0: if (num1 != 0) sl@0: { sl@0: s->rwstate=SSL_WRITING; sl@0: num1=BIO_flush(s->wbio); sl@0: if (num1 <= 0) { ret= -1; goto end; } sl@0: s->rwstate=SSL_NOTHING; sl@0: } sl@0: sl@0: /* flushed and now remove buffering */ sl@0: s->wbio=BIO_pop(s->wbio); sl@0: sl@0: s->state=SSL2_ST_GET_CLIENT_FINISHED_A; sl@0: BREAK; sl@0: sl@0: case SSL2_ST_GET_CLIENT_FINISHED_A: sl@0: case SSL2_ST_GET_CLIENT_FINISHED_B: sl@0: ret=get_client_finished(s); sl@0: if (ret <= 0) sl@0: goto end; sl@0: s->init_num=0; sl@0: s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_A; sl@0: BREAK; sl@0: sl@0: case SSL2_ST_SEND_REQUEST_CERTIFICATE_A: sl@0: case SSL2_ST_SEND_REQUEST_CERTIFICATE_B: sl@0: case SSL2_ST_SEND_REQUEST_CERTIFICATE_C: sl@0: case SSL2_ST_SEND_REQUEST_CERTIFICATE_D: sl@0: /* don't do a 'request certificate' if we sl@0: * don't want to, or we already have one, and sl@0: * we only want to do it once. */ sl@0: if (!(s->verify_mode & SSL_VERIFY_PEER) || sl@0: ((s->session->peer != NULL) && sl@0: (s->verify_mode & SSL_VERIFY_CLIENT_ONCE))) sl@0: { sl@0: s->state=SSL2_ST_SEND_SERVER_FINISHED_A; sl@0: break; sl@0: } sl@0: else sl@0: { sl@0: ret=request_certificate(s); sl@0: if (ret <= 0) goto end; sl@0: s->init_num=0; sl@0: s->state=SSL2_ST_SEND_SERVER_FINISHED_A; sl@0: } sl@0: BREAK; sl@0: sl@0: case SSL2_ST_SEND_SERVER_FINISHED_A: sl@0: case SSL2_ST_SEND_SERVER_FINISHED_B: sl@0: ret=server_finish(s); sl@0: if (ret <= 0) goto end; sl@0: s->init_num=0; sl@0: s->state=SSL_ST_OK; sl@0: break; sl@0: sl@0: case SSL_ST_OK: sl@0: BUF_MEM_free(s->init_buf); sl@0: ssl_free_wbio_buffer(s); sl@0: s->init_buf=NULL; sl@0: s->init_num=0; sl@0: /* ERR_clear_error();*/ sl@0: sl@0: ssl_update_cache(s,SSL_SESS_CACHE_SERVER); sl@0: sl@0: s->ctx->stats.sess_accept_good++; sl@0: /* s->server=1; */ sl@0: ret=1; sl@0: sl@0: if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1); sl@0: sl@0: goto end; sl@0: /* BREAK; */ sl@0: sl@0: default: sl@0: SSLerr(SSL_F_SSL2_ACCEPT,SSL_R_UNKNOWN_STATE); sl@0: ret= -1; sl@0: goto end; sl@0: /* BREAK; */ sl@0: } sl@0: sl@0: if ((cb != NULL) && (s->state != state)) sl@0: { sl@0: new_state=s->state; sl@0: s->state=state; sl@0: cb(s,SSL_CB_ACCEPT_LOOP,1); sl@0: s->state=new_state; sl@0: } sl@0: } sl@0: end: sl@0: s->in_handshake--; sl@0: if (cb != NULL) sl@0: cb(s,SSL_CB_ACCEPT_EXIT,ret); sl@0: return(ret); sl@0: } sl@0: sl@0: static int get_client_master_key(SSL *s) sl@0: { sl@0: int is_export,i,n,keya,ek; sl@0: unsigned long len; sl@0: unsigned char *p; sl@0: SSL_CIPHER *cp; sl@0: const EVP_CIPHER *c; sl@0: const EVP_MD *md; sl@0: sl@0: p=(unsigned char *)s->init_buf->data; sl@0: if (s->state == SSL2_ST_GET_CLIENT_MASTER_KEY_A) sl@0: { sl@0: i=ssl2_read(s,(char *)&(p[s->init_num]),10-s->init_num); sl@0: sl@0: if (i < (10-s->init_num)) sl@0: return(ssl2_part_read(s,SSL_F_GET_CLIENT_MASTER_KEY,i)); sl@0: s->init_num = 10; sl@0: sl@0: if (*(p++) != SSL2_MT_CLIENT_MASTER_KEY) sl@0: { sl@0: if (p[-1] != SSL2_MT_ERROR) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_READ_WRONG_PACKET_TYPE); sl@0: } sl@0: else sl@0: SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, SSL_R_PEER_ERROR); sl@0: return(-1); sl@0: } sl@0: sl@0: cp=ssl2_get_cipher_by_char(p); sl@0: if (cp == NULL) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_NO_CIPHER); sl@0: SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, SSL_R_NO_CIPHER_MATCH); sl@0: return(-1); sl@0: } sl@0: s->session->cipher= cp; sl@0: sl@0: p+=3; sl@0: n2s(p,i); s->s2->tmp.clear=i; sl@0: n2s(p,i); s->s2->tmp.enc=i; sl@0: n2s(p,i); s->session->key_arg_length=i; sl@0: if(s->session->key_arg_length > SSL_MAX_KEY_ARG_LENGTH) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, SSL_R_KEY_ARG_TOO_LONG); sl@0: return -1; sl@0: } sl@0: s->state=SSL2_ST_GET_CLIENT_MASTER_KEY_B; sl@0: } sl@0: sl@0: /* SSL2_ST_GET_CLIENT_MASTER_KEY_B */ sl@0: p=(unsigned char *)s->init_buf->data; sl@0: if (s->init_buf->length < SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR); sl@0: return -1; sl@0: } sl@0: keya=s->session->key_arg_length; sl@0: len = 10 + (unsigned long)s->s2->tmp.clear + (unsigned long)s->s2->tmp.enc + (unsigned long)keya; sl@0: if (len > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_MESSAGE_TOO_LONG); sl@0: return -1; sl@0: } sl@0: n = (int)len - s->init_num; sl@0: i = ssl2_read(s,(char *)&(p[s->init_num]),n); sl@0: if (i != n) return(ssl2_part_read(s,SSL_F_GET_CLIENT_MASTER_KEY,i)); sl@0: if (s->msg_callback) sl@0: s->msg_callback(0, s->version, 0, p, (size_t)len, s, s->msg_callback_arg); /* CLIENT-MASTER-KEY */ sl@0: p += 10; sl@0: sl@0: memcpy(s->session->key_arg,&(p[s->s2->tmp.clear+s->s2->tmp.enc]), sl@0: (unsigned int)keya); sl@0: sl@0: if (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_NO_PRIVATEKEY); sl@0: return(-1); sl@0: } sl@0: i=ssl_rsa_private_decrypt(s->cert,s->s2->tmp.enc, sl@0: &(p[s->s2->tmp.clear]),&(p[s->s2->tmp.clear]), sl@0: (s->s2->ssl2_rollback)?RSA_SSLV23_PADDING:RSA_PKCS1_PADDING); sl@0: sl@0: is_export=SSL_C_IS_EXPORT(s->session->cipher); sl@0: sl@0: if (!ssl_cipher_get_evp(s->session,&c,&md,NULL)) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_NO_CIPHER); sl@0: SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS); sl@0: return(0); sl@0: } sl@0: sl@0: if (s->session->cipher->algorithm2 & SSL2_CF_8_BYTE_ENC) sl@0: { sl@0: is_export=1; sl@0: ek=8; sl@0: } sl@0: else sl@0: ek=5; sl@0: sl@0: /* bad decrypt */ sl@0: #if 1 sl@0: /* If a bad decrypt, continue with protocol but with a sl@0: * random master secret (Bleichenbacher attack) */ sl@0: if ((i < 0) || sl@0: ((!is_export && (i != EVP_CIPHER_key_length(c))) sl@0: || (is_export && ((i != ek) || (s->s2->tmp.clear+(unsigned int)i != sl@0: (unsigned int)EVP_CIPHER_key_length(c)))))) sl@0: { sl@0: ERR_clear_error(); sl@0: if (is_export) sl@0: i=ek; sl@0: else sl@0: i=EVP_CIPHER_key_length(c); sl@0: if (RAND_pseudo_bytes(p,i) <= 0) sl@0: return 0; sl@0: } sl@0: #else sl@0: if (i < 0) sl@0: { sl@0: error=1; sl@0: SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_BAD_RSA_DECRYPT); sl@0: } sl@0: /* incorrect number of key bytes for non export cipher */ sl@0: else if ((!is_export && (i != EVP_CIPHER_key_length(c))) sl@0: || (is_export && ((i != ek) || (s->s2->tmp.clear+i != sl@0: EVP_CIPHER_key_length(c))))) sl@0: { sl@0: error=1; sl@0: SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_WRONG_NUMBER_OF_KEY_BITS); sl@0: } sl@0: if (error) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: return(-1); sl@0: } sl@0: #endif sl@0: sl@0: if (is_export) i+=s->s2->tmp.clear; sl@0: sl@0: if (i > SSL_MAX_MASTER_KEY_LENGTH) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: SSLerr(SSL_F_GET_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR); sl@0: return -1; sl@0: } sl@0: s->session->master_key_length=i; sl@0: memcpy(s->session->master_key,p,(unsigned int)i); sl@0: return(1); sl@0: } sl@0: sl@0: static int get_client_hello(SSL *s) sl@0: { sl@0: int i,n; sl@0: unsigned long len; sl@0: unsigned char *p; sl@0: STACK_OF(SSL_CIPHER) *cs; /* a stack of SSL_CIPHERS */ sl@0: STACK_OF(SSL_CIPHER) *cl; /* the ones we want to use */ sl@0: STACK_OF(SSL_CIPHER) *prio, *allow; sl@0: int z; sl@0: sl@0: /* This is a bit of a hack to check for the correct packet sl@0: * type the first time round. */ sl@0: if (s->state == SSL2_ST_GET_CLIENT_HELLO_A) sl@0: { sl@0: s->first_packet=1; sl@0: s->state=SSL2_ST_GET_CLIENT_HELLO_B; sl@0: } sl@0: sl@0: p=(unsigned char *)s->init_buf->data; sl@0: if (s->state == SSL2_ST_GET_CLIENT_HELLO_B) sl@0: { sl@0: i=ssl2_read(s,(char *)&(p[s->init_num]),9-s->init_num); sl@0: if (i < (9-s->init_num)) sl@0: return(ssl2_part_read(s,SSL_F_GET_CLIENT_HELLO,i)); sl@0: s->init_num = 9; sl@0: sl@0: if (*(p++) != SSL2_MT_CLIENT_HELLO) sl@0: { sl@0: if (p[-1] != SSL2_MT_ERROR) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_READ_WRONG_PACKET_TYPE); sl@0: } sl@0: else sl@0: SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_PEER_ERROR); sl@0: return(-1); sl@0: } sl@0: n2s(p,i); sl@0: if (i < s->version) s->version=i; sl@0: n2s(p,i); s->s2->tmp.cipher_spec_length=i; sl@0: n2s(p,i); s->s2->tmp.session_id_length=i; sl@0: n2s(p,i); s->s2->challenge_length=i; sl@0: if ( (i < SSL2_MIN_CHALLENGE_LENGTH) || sl@0: (i > SSL2_MAX_CHALLENGE_LENGTH)) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_INVALID_CHALLENGE_LENGTH); sl@0: return(-1); sl@0: } sl@0: s->state=SSL2_ST_GET_CLIENT_HELLO_C; sl@0: } sl@0: sl@0: /* SSL2_ST_GET_CLIENT_HELLO_C */ sl@0: p=(unsigned char *)s->init_buf->data; sl@0: len = 9 + (unsigned long)s->s2->tmp.cipher_spec_length + (unsigned long)s->s2->challenge_length + (unsigned long)s->s2->tmp.session_id_length; sl@0: if (len > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_MESSAGE_TOO_LONG); sl@0: return -1; sl@0: } sl@0: n = (int)len - s->init_num; sl@0: i = ssl2_read(s,(char *)&(p[s->init_num]),n); sl@0: if (i != n) return(ssl2_part_read(s,SSL_F_GET_CLIENT_HELLO,i)); sl@0: if (s->msg_callback) sl@0: s->msg_callback(0, s->version, 0, p, (size_t)len, s, s->msg_callback_arg); /* CLIENT-HELLO */ sl@0: p += 9; sl@0: sl@0: /* get session-id before cipher stuff so we can get out session sl@0: * structure if it is cached */ sl@0: /* session-id */ sl@0: if ((s->s2->tmp.session_id_length != 0) && sl@0: (s->s2->tmp.session_id_length != SSL2_SSL_SESSION_ID_LENGTH)) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_BAD_SSL_SESSION_ID_LENGTH); sl@0: return(-1); sl@0: } sl@0: sl@0: if (s->s2->tmp.session_id_length == 0) sl@0: { sl@0: if (!ssl_get_new_session(s,1)) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: return(-1); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: i=ssl_get_prev_session(s,&(p[s->s2->tmp.cipher_spec_length]), sl@0: s->s2->tmp.session_id_length, NULL); sl@0: if (i == 1) sl@0: { /* previous session */ sl@0: s->hit=1; sl@0: } sl@0: else if (i == -1) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: return(-1); sl@0: } sl@0: else sl@0: { sl@0: if (s->cert == NULL) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_NO_CERTIFICATE); sl@0: SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_NO_CERTIFICATE_SET); sl@0: return(-1); sl@0: } sl@0: sl@0: if (!ssl_get_new_session(s,1)) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: return(-1); sl@0: } sl@0: } sl@0: } sl@0: sl@0: if (!s->hit) sl@0: { sl@0: cs=ssl_bytes_to_cipher_list(s,p,s->s2->tmp.cipher_spec_length, sl@0: &s->session->ciphers); sl@0: if (cs == NULL) goto mem_err; sl@0: sl@0: cl=SSL_get_ciphers(s); sl@0: sl@0: if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) sl@0: { sl@0: prio=sk_SSL_CIPHER_dup(cl); sl@0: if (prio == NULL) goto mem_err; sl@0: allow = cs; sl@0: } sl@0: else sl@0: { sl@0: prio = cs; sl@0: allow = cl; sl@0: } sl@0: for (z=0; zoptions & SSL_OP_CIPHER_SERVER_PREFERENCE) sl@0: { sl@0: sk_SSL_CIPHER_free(s->session->ciphers); sl@0: s->session->ciphers = prio; sl@0: } sl@0: /* s->session->ciphers should now have a list of sl@0: * ciphers that are on both the client and server. sl@0: * This list is ordered by the order the client sent sl@0: * the ciphers or in the order of the server's preference sl@0: * if SSL_OP_CIPHER_SERVER_PREFERENCE was set. sl@0: */ sl@0: } sl@0: p+=s->s2->tmp.cipher_spec_length; sl@0: /* done cipher selection */ sl@0: sl@0: /* session id extracted already */ sl@0: p+=s->s2->tmp.session_id_length; sl@0: sl@0: /* challenge */ sl@0: if (s->s2->challenge_length > sizeof s->s2->challenge) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: SSLerr(SSL_F_GET_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); sl@0: return -1; sl@0: } sl@0: memcpy(s->s2->challenge,p,(unsigned int)s->s2->challenge_length); sl@0: return(1); sl@0: mem_err: sl@0: SSLerr(SSL_F_GET_CLIENT_HELLO,ERR_R_MALLOC_FAILURE); sl@0: return(0); sl@0: } sl@0: sl@0: static int server_hello(SSL *s) sl@0: { sl@0: unsigned char *p,*d; sl@0: int n,hit; sl@0: STACK_OF(SSL_CIPHER) *sk; sl@0: sl@0: p=(unsigned char *)s->init_buf->data; sl@0: if (s->state == SSL2_ST_SEND_SERVER_HELLO_A) sl@0: { sl@0: d=p+11; sl@0: *(p++)=SSL2_MT_SERVER_HELLO; /* type */ sl@0: hit=s->hit; sl@0: *(p++)=(unsigned char)hit; sl@0: #if 1 sl@0: if (!hit) sl@0: { sl@0: if (s->session->sess_cert != NULL) sl@0: /* This can't really happen because get_client_hello sl@0: * has called ssl_get_new_session, which does not set sl@0: * sess_cert. */ sl@0: ssl_sess_cert_free(s->session->sess_cert); sl@0: s->session->sess_cert = ssl_sess_cert_new(); sl@0: if (s->session->sess_cert == NULL) sl@0: { sl@0: SSLerr(SSL_F_SERVER_HELLO, ERR_R_MALLOC_FAILURE); sl@0: return(-1); sl@0: } sl@0: } sl@0: /* If 'hit' is set, then s->sess_cert may be non-NULL or NULL, sl@0: * depending on whether it survived in the internal cache sl@0: * or was retrieved from an external cache. sl@0: * If it is NULL, we cannot put any useful data in it anyway, sl@0: * so we don't touch it. sl@0: */ sl@0: sl@0: #else /* That's what used to be done when cert_st and sess_cert_st were sl@0: * the same. */ sl@0: if (!hit) sl@0: { /* else add cert to session */ sl@0: CRYPTO_add(&s->cert->references,1,CRYPTO_LOCK_SSL_CERT); sl@0: if (s->session->sess_cert != NULL) sl@0: ssl_cert_free(s->session->sess_cert); sl@0: s->session->sess_cert=s->cert; sl@0: } sl@0: else /* We have a session id-cache hit, if the sl@0: * session-id has no certificate listed against sl@0: * the 'cert' structure, grab the 'old' one sl@0: * listed against the SSL connection */ sl@0: { sl@0: if (s->session->sess_cert == NULL) sl@0: { sl@0: CRYPTO_add(&s->cert->references,1, sl@0: CRYPTO_LOCK_SSL_CERT); sl@0: s->session->sess_cert=s->cert; sl@0: } sl@0: } sl@0: #endif sl@0: sl@0: if (s->cert == NULL) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_NO_CERTIFICATE); sl@0: SSLerr(SSL_F_SERVER_HELLO,SSL_R_NO_CERTIFICATE_SPECIFIED); sl@0: return(-1); sl@0: } sl@0: sl@0: if (hit) sl@0: { sl@0: *(p++)=0; /* no certificate type */ sl@0: s2n(s->version,p); /* version */ sl@0: s2n(0,p); /* cert len */ sl@0: s2n(0,p); /* ciphers len */ sl@0: } sl@0: else sl@0: { sl@0: /* EAY EAY */ sl@0: /* put certificate type */ sl@0: *(p++)=SSL2_CT_X509_CERTIFICATE; sl@0: s2n(s->version,p); /* version */ sl@0: n=i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,NULL); sl@0: s2n(n,p); /* certificate length */ sl@0: i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,&d); sl@0: n=0; sl@0: sl@0: /* lets send out the ciphers we like in the sl@0: * prefered order */ sl@0: sk= s->session->ciphers; sl@0: n=ssl_cipher_list_to_bytes(s,s->session->ciphers,d,0); sl@0: d+=n; sl@0: s2n(n,p); /* add cipher length */ sl@0: } sl@0: sl@0: /* make and send conn_id */ sl@0: s2n(SSL2_CONNECTION_ID_LENGTH,p); /* add conn_id length */ sl@0: s->s2->conn_id_length=SSL2_CONNECTION_ID_LENGTH; sl@0: if (RAND_pseudo_bytes(s->s2->conn_id,(int)s->s2->conn_id_length) <= 0) sl@0: return -1; sl@0: memcpy(d,s->s2->conn_id,SSL2_CONNECTION_ID_LENGTH); sl@0: d+=SSL2_CONNECTION_ID_LENGTH; sl@0: sl@0: s->state=SSL2_ST_SEND_SERVER_HELLO_B; sl@0: s->init_num=d-(unsigned char *)s->init_buf->data; sl@0: s->init_off=0; sl@0: } sl@0: /* SSL2_ST_SEND_SERVER_HELLO_B */ sl@0: /* If we are using TCP/IP, the performance is bad if we do 2 sl@0: * writes without a read between them. This occurs when sl@0: * Session-id reuse is used, so I will put in a buffering module sl@0: */ sl@0: if (s->hit) sl@0: { sl@0: if (!ssl_init_wbio_buffer(s,1)) return(-1); sl@0: } sl@0: sl@0: return(ssl2_do_write(s)); sl@0: } sl@0: sl@0: static int get_client_finished(SSL *s) sl@0: { sl@0: unsigned char *p; sl@0: int i, n; sl@0: unsigned long len; sl@0: sl@0: p=(unsigned char *)s->init_buf->data; sl@0: if (s->state == SSL2_ST_GET_CLIENT_FINISHED_A) sl@0: { sl@0: i=ssl2_read(s,(char *)&(p[s->init_num]),1-s->init_num); sl@0: if (i < 1-s->init_num) sl@0: return(ssl2_part_read(s,SSL_F_GET_CLIENT_FINISHED,i)); sl@0: s->init_num += i; sl@0: sl@0: if (*p != SSL2_MT_CLIENT_FINISHED) sl@0: { sl@0: if (*p != SSL2_MT_ERROR) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: SSLerr(SSL_F_GET_CLIENT_FINISHED,SSL_R_READ_WRONG_PACKET_TYPE); sl@0: } sl@0: else sl@0: { sl@0: SSLerr(SSL_F_GET_CLIENT_FINISHED,SSL_R_PEER_ERROR); sl@0: /* try to read the error message */ sl@0: i=ssl2_read(s,(char *)&(p[s->init_num]),3-s->init_num); sl@0: return ssl2_part_read(s,SSL_F_GET_SERVER_VERIFY,i); sl@0: } sl@0: return(-1); sl@0: } sl@0: s->state=SSL2_ST_GET_CLIENT_FINISHED_B; sl@0: } sl@0: sl@0: /* SSL2_ST_GET_CLIENT_FINISHED_B */ sl@0: if (s->s2->conn_id_length > sizeof s->s2->conn_id) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: SSLerr(SSL_F_GET_CLIENT_FINISHED, ERR_R_INTERNAL_ERROR); sl@0: return -1; sl@0: } sl@0: len = 1 + (unsigned long)s->s2->conn_id_length; sl@0: n = (int)len - s->init_num; sl@0: i = ssl2_read(s,(char *)&(p[s->init_num]),n); sl@0: if (i < n) sl@0: { sl@0: return(ssl2_part_read(s,SSL_F_GET_CLIENT_FINISHED,i)); sl@0: } sl@0: if (s->msg_callback) sl@0: s->msg_callback(0, s->version, 0, p, len, s, s->msg_callback_arg); /* CLIENT-FINISHED */ sl@0: p += 1; sl@0: if (memcmp(p,s->s2->conn_id,s->s2->conn_id_length) != 0) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: SSLerr(SSL_F_GET_CLIENT_FINISHED,SSL_R_CONNECTION_ID_IS_DIFFERENT); sl@0: return(-1); sl@0: } sl@0: return(1); sl@0: } sl@0: sl@0: static int server_verify(SSL *s) sl@0: { sl@0: unsigned char *p; sl@0: sl@0: if (s->state == SSL2_ST_SEND_SERVER_VERIFY_A) sl@0: { sl@0: p=(unsigned char *)s->init_buf->data; sl@0: *(p++)=SSL2_MT_SERVER_VERIFY; sl@0: if (s->s2->challenge_length > sizeof s->s2->challenge) sl@0: { sl@0: SSLerr(SSL_F_SERVER_VERIFY, ERR_R_INTERNAL_ERROR); sl@0: return -1; sl@0: } sl@0: memcpy(p,s->s2->challenge,(unsigned int)s->s2->challenge_length); sl@0: /* p+=s->s2->challenge_length; */ sl@0: sl@0: s->state=SSL2_ST_SEND_SERVER_VERIFY_B; sl@0: s->init_num=s->s2->challenge_length+1; sl@0: s->init_off=0; sl@0: } sl@0: return(ssl2_do_write(s)); sl@0: } sl@0: sl@0: static int server_finish(SSL *s) sl@0: { sl@0: unsigned char *p; sl@0: sl@0: if (s->state == SSL2_ST_SEND_SERVER_FINISHED_A) sl@0: { sl@0: p=(unsigned char *)s->init_buf->data; sl@0: *(p++)=SSL2_MT_SERVER_FINISHED; sl@0: sl@0: if (s->session->session_id_length > sizeof s->session->session_id) sl@0: { sl@0: SSLerr(SSL_F_SERVER_FINISH, ERR_R_INTERNAL_ERROR); sl@0: return -1; sl@0: } sl@0: memcpy(p,s->session->session_id, (unsigned int)s->session->session_id_length); sl@0: /* p+=s->session->session_id_length; */ sl@0: sl@0: s->state=SSL2_ST_SEND_SERVER_FINISHED_B; sl@0: s->init_num=s->session->session_id_length+1; sl@0: s->init_off=0; sl@0: } sl@0: sl@0: /* SSL2_ST_SEND_SERVER_FINISHED_B */ sl@0: return(ssl2_do_write(s)); sl@0: } sl@0: sl@0: /* send the request and check the response */ sl@0: static int request_certificate(SSL *s) sl@0: { sl@0: const unsigned char *cp; sl@0: unsigned char *p,*p2,*buf2; sl@0: unsigned char *ccd; sl@0: int i,j,ctype,ret= -1; sl@0: unsigned long len; sl@0: X509 *x509=NULL; sl@0: STACK_OF(X509) *sk=NULL; sl@0: sl@0: ccd=s->s2->tmp.ccl; sl@0: if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_A) sl@0: { sl@0: p=(unsigned char *)s->init_buf->data; sl@0: *(p++)=SSL2_MT_REQUEST_CERTIFICATE; sl@0: *(p++)=SSL2_AT_MD5_WITH_RSA_ENCRYPTION; sl@0: if (RAND_pseudo_bytes(ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH) <= 0) sl@0: return -1; sl@0: memcpy(p,ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH); sl@0: sl@0: s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_B; sl@0: s->init_num=SSL2_MIN_CERT_CHALLENGE_LENGTH+2; sl@0: s->init_off=0; sl@0: } sl@0: sl@0: if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_B) sl@0: { sl@0: i=ssl2_do_write(s); sl@0: if (i <= 0) sl@0: { sl@0: ret=i; sl@0: goto end; sl@0: } sl@0: sl@0: s->init_num=0; sl@0: s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_C; sl@0: } sl@0: sl@0: if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_C) sl@0: { sl@0: p=(unsigned char *)s->init_buf->data; sl@0: i=ssl2_read(s,(char *)&(p[s->init_num]),6-s->init_num); /* try to read 6 octets ... */ sl@0: if (i < 3-s->init_num) /* ... but don't call ssl2_part_read now if we got at least 3 sl@0: * (probably NO-CERTIFICATE-ERROR) */ sl@0: { sl@0: ret=ssl2_part_read(s,SSL_F_REQUEST_CERTIFICATE,i); sl@0: goto end; sl@0: } sl@0: s->init_num += i; sl@0: sl@0: if ((s->init_num >= 3) && (p[0] == SSL2_MT_ERROR)) sl@0: { sl@0: n2s(p,i); sl@0: if (i != SSL2_PE_NO_CERTIFICATE) sl@0: { sl@0: /* not the error message we expected -- let ssl2_part_read handle it */ sl@0: s->init_num -= 3; sl@0: ret = ssl2_part_read(s,SSL_F_REQUEST_CERTIFICATE, 3); sl@0: goto end; sl@0: } sl@0: sl@0: if (s->msg_callback) sl@0: s->msg_callback(0, s->version, 0, p, 3, s, s->msg_callback_arg); /* ERROR */ sl@0: sl@0: /* this is the one place where we can recover from an SSL 2.0 error */ sl@0: sl@0: if (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE); sl@0: SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); sl@0: goto end; sl@0: } sl@0: ret=1; sl@0: goto end; sl@0: } sl@0: if ((*(p++) != SSL2_MT_CLIENT_CERTIFICATE) || (s->init_num < 6)) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_SHORT_READ); sl@0: goto end; sl@0: } sl@0: if (s->init_num != 6) sl@0: { sl@0: SSLerr(SSL_F_REQUEST_CERTIFICATE, ERR_R_INTERNAL_ERROR); sl@0: goto end; sl@0: } sl@0: sl@0: /* ok we have a response */ sl@0: /* certificate type, there is only one right now. */ sl@0: ctype= *(p++); sl@0: if (ctype != SSL2_AT_MD5_WITH_RSA_ENCRYPTION) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE); sl@0: SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_BAD_RESPONSE_ARGUMENT); sl@0: goto end; sl@0: } sl@0: n2s(p,i); s->s2->tmp.clen=i; sl@0: n2s(p,i); s->s2->tmp.rlen=i; sl@0: s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_D; sl@0: } sl@0: sl@0: /* SSL2_ST_SEND_REQUEST_CERTIFICATE_D */ sl@0: p=(unsigned char *)s->init_buf->data; sl@0: len = 6 + (unsigned long)s->s2->tmp.clen + (unsigned long)s->s2->tmp.rlen; sl@0: if (len > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER) sl@0: { sl@0: SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_MESSAGE_TOO_LONG); sl@0: goto end; sl@0: } sl@0: j = (int)len - s->init_num; sl@0: i = ssl2_read(s,(char *)&(p[s->init_num]),j); sl@0: if (i < j) sl@0: { sl@0: ret=ssl2_part_read(s,SSL_F_REQUEST_CERTIFICATE,i); sl@0: goto end; sl@0: } sl@0: if (s->msg_callback) sl@0: s->msg_callback(0, s->version, 0, p, len, s, s->msg_callback_arg); /* CLIENT-CERTIFICATE */ sl@0: p += 6; sl@0: sl@0: cp = p; sl@0: x509=(X509 *)d2i_X509(NULL,&cp,(long)s->s2->tmp.clen); sl@0: if (x509 == NULL) sl@0: { sl@0: SSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_X509_LIB); sl@0: goto msg_end; sl@0: } sl@0: sl@0: if (((sk=sk_X509_new_null()) == NULL) || (!sk_X509_push(sk,x509))) sl@0: { sl@0: SSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_MALLOC_FAILURE); sl@0: goto msg_end; sl@0: } sl@0: sl@0: i=ssl_verify_cert_chain(s,sk); sl@0: sl@0: if (i) /* we like the packet, now check the chksum */ sl@0: { sl@0: EVP_MD_CTX ctx; sl@0: EVP_PKEY *pkey=NULL; sl@0: sl@0: EVP_MD_CTX_init(&ctx); sl@0: EVP_VerifyInit_ex(&ctx,s->ctx->rsa_md5, NULL); sl@0: EVP_VerifyUpdate(&ctx,s->s2->key_material, sl@0: s->s2->key_material_length); sl@0: EVP_VerifyUpdate(&ctx,ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH); sl@0: sl@0: i=i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,NULL); sl@0: buf2=OPENSSL_malloc((unsigned int)i); sl@0: if (buf2 == NULL) sl@0: { sl@0: SSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_MALLOC_FAILURE); sl@0: goto msg_end; sl@0: } sl@0: p2=buf2; sl@0: i=i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,&p2); sl@0: EVP_VerifyUpdate(&ctx,buf2,(unsigned int)i); sl@0: OPENSSL_free(buf2); sl@0: sl@0: pkey=X509_get_pubkey(x509); sl@0: if (pkey == NULL) goto end; sl@0: i=EVP_VerifyFinal(&ctx,cp,s->s2->tmp.rlen,pkey); sl@0: EVP_PKEY_free(pkey); sl@0: EVP_MD_CTX_cleanup(&ctx); sl@0: sl@0: if (i) sl@0: { sl@0: if (s->session->peer != NULL) sl@0: X509_free(s->session->peer); sl@0: s->session->peer=x509; sl@0: CRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509); sl@0: s->session->verify_result = s->verify_result; sl@0: ret=1; sl@0: goto end; sl@0: } sl@0: else sl@0: { sl@0: SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_BAD_CHECKSUM); sl@0: goto msg_end; sl@0: } sl@0: } sl@0: else sl@0: { sl@0: msg_end: sl@0: ssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE); sl@0: } sl@0: end: sl@0: sk_X509_free(sk); sl@0: X509_free(x509); sl@0: return(ret); sl@0: } sl@0: sl@0: static int ssl_rsa_private_decrypt(CERT *c, int len, unsigned char *from, sl@0: unsigned char *to, int padding) sl@0: { sl@0: RSA *rsa; sl@0: int i; sl@0: sl@0: if ((c == NULL) || (c->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL)) sl@0: { sl@0: SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT,SSL_R_NO_PRIVATEKEY); sl@0: return(-1); sl@0: } sl@0: if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey->type != EVP_PKEY_RSA) sl@0: { sl@0: SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT,SSL_R_PUBLIC_KEY_IS_NOT_RSA); sl@0: return(-1); sl@0: } sl@0: rsa=c->pkeys[SSL_PKEY_RSA_ENC].privatekey->pkey.rsa; sl@0: sl@0: /* we have the public key */ sl@0: i=RSA_private_decrypt(len,from,to,rsa,padding); sl@0: if (i < 0) sl@0: SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT,ERR_R_RSA_LIB); sl@0: return(i); sl@0: } sl@0: #else /* !OPENSSL_NO_SSL2 */ sl@0: sl@0: # if PEDANTIC sl@0: static void *dummy=&dummy; sl@0: # endif sl@0: sl@0: #endif