sl@0: /* ssl/s2_clnt.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: sl@0: #ifdef EMULATOR sl@0: sl@0: GET_STATIC_VAR_FROM_TLS(SSLv2_client_method_data,s2_clnt,SSL_METHOD) sl@0: sl@0: #define SSLv2_client_method_data (*GET_WSD_VAR_NAME(SSLv2_client_method_data,s2_clnt,s)()) sl@0: sl@0: #endif sl@0: static SSL_METHOD *ssl2_get_client_method(int ver); sl@0: static int get_server_finished(SSL *s); sl@0: static int get_server_verify(SSL *s); sl@0: static int get_server_hello(SSL *s); sl@0: static int client_hello(SSL *s); sl@0: static int client_master_key(SSL *s); sl@0: static int client_finished(SSL *s); sl@0: static int client_certificate(SSL *s); sl@0: static int ssl_rsa_public_encrypt(SESS_CERT *sc, 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_client_method(int ver) sl@0: { sl@0: if (ver == SSL2_VERSION) sl@0: return(SSLv2_client_method()); sl@0: else sl@0: return(NULL); sl@0: } sl@0: sl@0: EXPORT_C IMPLEMENT_ssl2_meth_func(SSLv2_client_method, sl@0: ssl_undefined_function, sl@0: ssl2_connect, sl@0: ssl2_get_client_method) sl@0: sl@0: int ssl2_connect(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: 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: 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_CONNECT: sl@0: case SSL_ST_BEFORE|SSL_ST_CONNECT: sl@0: case SSL_ST_OK|SSL_ST_CONNECT: sl@0: sl@0: s->server=0; 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_CONNECT; sl@0: sl@0: buf=s->init_buf; sl@0: if ((buf == NULL) && ((buf=BUF_MEM_new()) == NULL)) sl@0: { sl@0: ret= -1; sl@0: goto end; sl@0: } sl@0: if (!BUF_MEM_grow(buf, sl@0: SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)) sl@0: { sl@0: if (buf == s->init_buf) sl@0: buf=NULL; sl@0: ret= -1; sl@0: goto end; sl@0: } sl@0: s->init_buf=buf; sl@0: buf=NULL; sl@0: s->init_num=0; sl@0: s->state=SSL2_ST_SEND_CLIENT_HELLO_A; sl@0: s->ctx->stats.sess_connect++; sl@0: s->handshake_func=ssl2_connect; sl@0: BREAK; sl@0: sl@0: case SSL2_ST_SEND_CLIENT_HELLO_A: sl@0: case SSL2_ST_SEND_CLIENT_HELLO_B: sl@0: s->shutdown=0; sl@0: ret=client_hello(s); sl@0: if (ret <= 0) goto end; sl@0: s->init_num=0; sl@0: s->state=SSL2_ST_GET_SERVER_HELLO_A; sl@0: BREAK; sl@0: sl@0: case SSL2_ST_GET_SERVER_HELLO_A: sl@0: case SSL2_ST_GET_SERVER_HELLO_B: sl@0: ret=get_server_hello(s); sl@0: if (ret <= 0) goto end; sl@0: s->init_num=0; sl@0: if (!s->hit) /* new session */ sl@0: { sl@0: s->state=SSL2_ST_SEND_CLIENT_MASTER_KEY_A; sl@0: BREAK; sl@0: } sl@0: else sl@0: { sl@0: s->state=SSL2_ST_CLIENT_START_ENCRYPTION; sl@0: break; sl@0: } sl@0: sl@0: case SSL2_ST_SEND_CLIENT_MASTER_KEY_A: sl@0: case SSL2_ST_SEND_CLIENT_MASTER_KEY_B: sl@0: ret=client_master_key(s); sl@0: if (ret <= 0) goto end; sl@0: s->init_num=0; sl@0: s->state=SSL2_ST_CLIENT_START_ENCRYPTION; sl@0: break; sl@0: sl@0: case SSL2_ST_CLIENT_START_ENCRYPTION: sl@0: /* Ok, we now have all the stuff needed to sl@0: * start encrypting, so lets fire it up :-) */ sl@0: if (!ssl2_enc_init(s,1)) sl@0: { sl@0: ret= -1; sl@0: goto end; sl@0: } sl@0: s->s2->clear_text=0; sl@0: s->state=SSL2_ST_SEND_CLIENT_FINISHED_A; sl@0: break; sl@0: sl@0: case SSL2_ST_SEND_CLIENT_FINISHED_A: sl@0: case SSL2_ST_SEND_CLIENT_FINISHED_B: sl@0: ret=client_finished(s); sl@0: if (ret <= 0) goto end; sl@0: s->init_num=0; sl@0: s->state=SSL2_ST_GET_SERVER_VERIFY_A; sl@0: break; sl@0: sl@0: case SSL2_ST_GET_SERVER_VERIFY_A: sl@0: case SSL2_ST_GET_SERVER_VERIFY_B: sl@0: ret=get_server_verify(s); sl@0: if (ret <= 0) goto end; sl@0: s->init_num=0; sl@0: s->state=SSL2_ST_GET_SERVER_FINISHED_A; sl@0: break; sl@0: sl@0: case SSL2_ST_GET_SERVER_FINISHED_A: sl@0: case SSL2_ST_GET_SERVER_FINISHED_B: sl@0: ret=get_server_finished(s); sl@0: if (ret <= 0) goto end; sl@0: break; sl@0: sl@0: case SSL2_ST_SEND_CLIENT_CERTIFICATE_A: sl@0: case SSL2_ST_SEND_CLIENT_CERTIFICATE_B: sl@0: case SSL2_ST_SEND_CLIENT_CERTIFICATE_C: sl@0: case SSL2_ST_SEND_CLIENT_CERTIFICATE_D: sl@0: case SSL2_ST_X509_GET_CLIENT_CERTIFICATE: sl@0: ret=client_certificate(s); sl@0: if (ret <= 0) goto end; sl@0: s->init_num=0; sl@0: s->state=SSL2_ST_GET_SERVER_FINISHED_A; sl@0: break; sl@0: sl@0: case SSL_ST_OK: sl@0: if (s->init_buf != NULL) sl@0: { sl@0: BUF_MEM_free(s->init_buf); sl@0: s->init_buf=NULL; sl@0: } sl@0: s->init_num=0; sl@0: /* ERR_clear_error();*/ sl@0: sl@0: /* If we want to cache session-ids in the client sl@0: * and we successfully add the session-id to the sl@0: * cache, and there is a callback, then pass it out. sl@0: * 26/11/96 - eay - only add if not a re-used session. sl@0: */ sl@0: sl@0: ssl_update_cache(s,SSL_SESS_CACHE_CLIENT); sl@0: if (s->hit) s->ctx->stats.sess_hit++; sl@0: sl@0: ret=1; sl@0: /* s->server=0; */ sl@0: s->ctx->stats.sess_connect_good++; 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: default: sl@0: SSLerr(SSL_F_SSL2_CONNECT,SSL_R_UNKNOWN_STATE); sl@0: return(-1); 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_CONNECT_LOOP,1); sl@0: s->state=new_state; sl@0: } sl@0: } sl@0: end: sl@0: s->in_handshake--; sl@0: if (buf != NULL) sl@0: BUF_MEM_free(buf); sl@0: if (cb != NULL) sl@0: cb(s,SSL_CB_CONNECT_EXIT,ret); sl@0: return(ret); sl@0: } sl@0: sl@0: static int get_server_hello(SSL *s) sl@0: { sl@0: unsigned char *buf; sl@0: unsigned char *p; sl@0: int i,j; sl@0: unsigned long len; sl@0: STACK_OF(SSL_CIPHER) *sk=NULL,*cl, *prio, *allow; sl@0: sl@0: buf=(unsigned char *)s->init_buf->data; sl@0: p=buf; sl@0: if (s->state == SSL2_ST_GET_SERVER_HELLO_A) sl@0: { sl@0: i=ssl2_read(s,(char *)&(buf[s->init_num]),11-s->init_num); sl@0: if (i < (11-s->init_num)) sl@0: return(ssl2_part_read(s,SSL_F_GET_SERVER_HELLO,i)); sl@0: s->init_num = 11; sl@0: sl@0: if (*(p++) != SSL2_MT_SERVER_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_SERVER_HELLO, sl@0: SSL_R_READ_WRONG_PACKET_TYPE); sl@0: } sl@0: else sl@0: SSLerr(SSL_F_GET_SERVER_HELLO, sl@0: SSL_R_PEER_ERROR); sl@0: return(-1); sl@0: } sl@0: #ifdef __APPLE_CC__ sl@0: /* The Rhapsody 5.5 (a.k.a. MacOS X) compiler bug sl@0: * workaround. */ sl@0: s->hit=(i=*(p++))?1:0; sl@0: #else sl@0: s->hit=(*(p++))?1:0; sl@0: #endif sl@0: s->s2->tmp.cert_type= *(p++); sl@0: n2s(p,i); sl@0: if (i < s->version) s->version=i; sl@0: n2s(p,i); s->s2->tmp.cert_length=i; sl@0: n2s(p,i); s->s2->tmp.csl=i; sl@0: n2s(p,i); s->s2->tmp.conn_id_length=i; sl@0: s->state=SSL2_ST_GET_SERVER_HELLO_B; sl@0: } sl@0: sl@0: /* SSL2_ST_GET_SERVER_HELLO_B */ sl@0: len = 11 + (unsigned long)s->s2->tmp.cert_length + (unsigned long)s->s2->tmp.csl + (unsigned long)s->s2->tmp.conn_id_length; sl@0: if (len > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER) sl@0: { sl@0: SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_MESSAGE_TOO_LONG); sl@0: return -1; sl@0: } sl@0: j = (int)len - s->init_num; sl@0: i = ssl2_read(s,(char *)&(buf[s->init_num]),j); sl@0: if (i != j) return(ssl2_part_read(s,SSL_F_GET_SERVER_HELLO,i)); sl@0: if (s->msg_callback) sl@0: s->msg_callback(0, s->version, 0, buf, (size_t)len, s, s->msg_callback_arg); /* SERVER-HELLO */ sl@0: sl@0: /* things are looking good */ sl@0: sl@0: p = buf + 11; sl@0: if (s->hit) sl@0: { sl@0: if (s->s2->tmp.cert_length != 0) sl@0: { sl@0: SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_REUSE_CERT_LENGTH_NOT_ZERO); sl@0: return(-1); sl@0: } sl@0: if (s->s2->tmp.cert_type != 0) sl@0: { sl@0: if (!(s->options & sl@0: SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG)) sl@0: { sl@0: SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_REUSE_CERT_TYPE_NOT_ZERO); sl@0: return(-1); sl@0: } sl@0: } sl@0: if (s->s2->tmp.csl != 0) sl@0: { sl@0: SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_REUSE_CIPHER_LIST_NOT_ZERO); sl@0: return(-1); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: #ifdef undef sl@0: /* very bad */ sl@0: memset(s->session->session_id,0, sl@0: SSL_MAX_SSL_SESSION_ID_LENGTH_IN_BYTES); sl@0: s->session->session_id_length=0; sl@0: */ sl@0: #endif sl@0: sl@0: /* we need to do this in case we were trying to reuse a sl@0: * client session but others are already reusing it. sl@0: * If this was a new 'blank' session ID, the session-id sl@0: * length will still be 0 */ sl@0: if (s->session->session_id_length > 0) sl@0: { sl@0: if (!ssl_get_new_session(s,0)) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: return(-1); sl@0: } sl@0: } sl@0: sl@0: if (ssl2_set_certificate(s,s->s2->tmp.cert_type, sl@0: s->s2->tmp.cert_length,p) <= 0) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE); sl@0: return(-1); sl@0: } sl@0: p+=s->s2->tmp.cert_length; sl@0: sl@0: if (s->s2->tmp.csl == 0) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_NO_CIPHER); sl@0: SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_NO_CIPHER_LIST); sl@0: return(-1); sl@0: } sl@0: sl@0: /* We have just received a list of ciphers back from the sl@0: * server. We need to get the ones that match, then select sl@0: * the one we want the most :-). */ sl@0: sl@0: /* load the ciphers */ sl@0: sk=ssl_bytes_to_cipher_list(s,p,s->s2->tmp.csl, sl@0: &s->session->ciphers); sl@0: p+=s->s2->tmp.csl; sl@0: if (sk == NULL) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: SSLerr(SSL_F_GET_SERVER_HELLO,ERR_R_MALLOC_FAILURE); sl@0: return(-1); sl@0: } sl@0: sl@0: (void)sk_SSL_CIPHER_set_cmp_func(sk,ssl_cipher_ptr_id_cmp); sl@0: sl@0: /* get the array of ciphers we will accept */ sl@0: cl=SSL_get_ciphers(s); sl@0: (void)sk_SSL_CIPHER_set_cmp_func(cl,ssl_cipher_ptr_id_cmp); sl@0: sl@0: /* sl@0: * If server preference flag set, choose the first sl@0: * (highest priority) cipher the server sends, otherwise sl@0: * client preference has priority. sl@0: */ sl@0: if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) sl@0: { sl@0: prio = sk; sl@0: allow = cl; sl@0: } sl@0: else sl@0: { sl@0: prio = cl; sl@0: allow = sk; sl@0: } sl@0: /* In theory we could have ciphers sent back that we sl@0: * don't want to use but that does not matter since we sl@0: * will check against the list we originally sent and sl@0: * for performance reasons we should not bother to match sl@0: * the two lists up just to check. */ sl@0: for (i=0; i= 0) sl@0: break; sl@0: } sl@0: sl@0: if (i >= sk_SSL_CIPHER_num(prio)) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_NO_CIPHER); sl@0: SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_NO_CIPHER_MATCH); sl@0: return(-1); sl@0: } sl@0: s->session->cipher=sk_SSL_CIPHER_value(prio,i); sl@0: sl@0: sl@0: if (s->session->peer != NULL) /* can't happen*/ sl@0: { sl@0: ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR); sl@0: SSLerr(SSL_F_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR); sl@0: return(-1); sl@0: } sl@0: sl@0: s->session->peer = s->session->sess_cert->peer_key->x509; sl@0: /* peer_key->x509 has been set by ssl2_set_certificate. */ sl@0: CRYPTO_add(&s->session->peer->references, 1, CRYPTO_LOCK_X509); sl@0: } sl@0: sl@0: if (s->session->sess_cert == NULL sl@0: || s->session->peer != s->session->sess_cert->peer_key->x509) sl@0: /* can't happen */ sl@0: { sl@0: ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR); sl@0: SSLerr(SSL_F_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR); sl@0: return(-1); sl@0: } sl@0: sl@0: s->s2->conn_id_length=s->s2->tmp.conn_id_length; 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_SERVER_HELLO, SSL_R_SSL2_CONNECTION_ID_TOO_LONG); sl@0: return -1; sl@0: } sl@0: memcpy(s->s2->conn_id,p,s->s2->tmp.conn_id_length); sl@0: return(1); sl@0: } sl@0: sl@0: static int client_hello(SSL *s) sl@0: { sl@0: unsigned char *buf; sl@0: unsigned char *p,*d; sl@0: /* CIPHER **cipher;*/ sl@0: int i,n,j; sl@0: sl@0: buf=(unsigned char *)s->init_buf->data; sl@0: if (s->state == SSL2_ST_SEND_CLIENT_HELLO_A) sl@0: { sl@0: if ((s->session == NULL) || sl@0: (s->session->ssl_version != s->version)) sl@0: { sl@0: if (!ssl_get_new_session(s,0)) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: return(-1); sl@0: } sl@0: } sl@0: /* else use the pre-loaded session */ sl@0: sl@0: p=buf; /* header */ sl@0: d=p+9; /* data section */ sl@0: *(p++)=SSL2_MT_CLIENT_HELLO; /* type */ sl@0: s2n(SSL2_VERSION,p); /* version */ sl@0: n=j=0; sl@0: sl@0: n=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),d,0); sl@0: d+=n; sl@0: sl@0: if (n == 0) sl@0: { sl@0: SSLerr(SSL_F_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE); sl@0: return(-1); sl@0: } sl@0: sl@0: s2n(n,p); /* cipher spec num bytes */ sl@0: sl@0: if ((s->session->session_id_length > 0) && sl@0: (s->session->session_id_length <= sl@0: SSL2_MAX_SSL_SESSION_ID_LENGTH)) sl@0: { sl@0: i=s->session->session_id_length; sl@0: s2n(i,p); /* session id length */ sl@0: memcpy(d,s->session->session_id,(unsigned int)i); sl@0: d+=i; sl@0: } sl@0: else sl@0: { sl@0: s2n(0,p); sl@0: } sl@0: sl@0: s->s2->challenge_length=SSL2_CHALLENGE_LENGTH; sl@0: s2n(SSL2_CHALLENGE_LENGTH,p); /* challenge length */ sl@0: /*challenge id data*/ sl@0: if (RAND_pseudo_bytes(s->s2->challenge,SSL2_CHALLENGE_LENGTH) <= 0) sl@0: return -1; sl@0: memcpy(d,s->s2->challenge,SSL2_CHALLENGE_LENGTH); sl@0: d+=SSL2_CHALLENGE_LENGTH; sl@0: sl@0: s->state=SSL2_ST_SEND_CLIENT_HELLO_B; sl@0: s->init_num=d-buf; sl@0: s->init_off=0; sl@0: } sl@0: /* SSL2_ST_SEND_CLIENT_HELLO_B */ sl@0: return(ssl2_do_write(s)); sl@0: } sl@0: sl@0: static int client_master_key(SSL *s) sl@0: { sl@0: unsigned char *buf; sl@0: unsigned char *p,*d; sl@0: int clear,enc,karg,i; sl@0: SSL_SESSION *sess; sl@0: const EVP_CIPHER *c; sl@0: const EVP_MD *md; sl@0: sl@0: buf=(unsigned char *)s->init_buf->data; sl@0: if (s->state == SSL2_ST_SEND_CLIENT_MASTER_KEY_A) sl@0: { 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_CLIENT_MASTER_KEY,SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS); sl@0: return(-1); sl@0: } sl@0: sess=s->session; sl@0: p=buf; sl@0: d=p+10; sl@0: *(p++)=SSL2_MT_CLIENT_MASTER_KEY;/* type */ sl@0: sl@0: i=ssl_put_cipher_by_char(s,sess->cipher,p); sl@0: p+=i; sl@0: sl@0: /* make key_arg data */ sl@0: i=EVP_CIPHER_iv_length(c); sl@0: sess->key_arg_length=i; sl@0: if (i > SSL_MAX_KEY_ARG_LENGTH) sl@0: { sl@0: ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR); sl@0: SSLerr(SSL_F_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR); sl@0: return -1; sl@0: } sl@0: if (i > 0) sl@0: if (RAND_pseudo_bytes(sess->key_arg,i) <= 0) sl@0: return -1; sl@0: sl@0: /* make a master key */ sl@0: i=EVP_CIPHER_key_length(c); sl@0: sess->master_key_length=i; sl@0: if (i > 0) sl@0: { sl@0: if (i > (int)sizeof(sess->master_key)) sl@0: { sl@0: ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR); sl@0: SSLerr(SSL_F_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR); sl@0: return -1; sl@0: } sl@0: if (RAND_bytes(sess->master_key,i) <= 0) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: return(-1); sl@0: } sl@0: } sl@0: sl@0: if (sess->cipher->algorithm2 & SSL2_CF_8_BYTE_ENC) sl@0: enc=8; sl@0: else if (SSL_C_IS_EXPORT(sess->cipher)) sl@0: enc=5; sl@0: else sl@0: enc=i; sl@0: sl@0: if ((int)i < enc) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: SSLerr(SSL_F_CLIENT_MASTER_KEY,SSL_R_CIPHER_TABLE_SRC_ERROR); sl@0: return(-1); sl@0: } sl@0: clear=i-enc; sl@0: s2n(clear,p); sl@0: memcpy(d,sess->master_key,(unsigned int)clear); sl@0: d+=clear; sl@0: sl@0: enc=ssl_rsa_public_encrypt(sess->sess_cert,enc, sl@0: &(sess->master_key[clear]),d, sl@0: (s->s2->ssl2_rollback)?RSA_SSLV23_PADDING:RSA_PKCS1_PADDING); sl@0: if (enc <= 0) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: SSLerr(SSL_F_CLIENT_MASTER_KEY,SSL_R_PUBLIC_KEY_ENCRYPT_ERROR); sl@0: return(-1); sl@0: } sl@0: #ifdef PKCS1_CHECK sl@0: if (s->options & SSL_OP_PKCS1_CHECK_1) d[1]++; sl@0: if (s->options & SSL_OP_PKCS1_CHECK_2) sl@0: sess->master_key[clear]++; sl@0: #endif sl@0: s2n(enc,p); sl@0: d+=enc; sl@0: karg=sess->key_arg_length; sl@0: s2n(karg,p); /* key arg size */ sl@0: if (karg > (int)sizeof(sess->key_arg)) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: SSLerr(SSL_F_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR); sl@0: return -1; sl@0: } sl@0: memcpy(d,sess->key_arg,(unsigned int)karg); sl@0: d+=karg; sl@0: sl@0: s->state=SSL2_ST_SEND_CLIENT_MASTER_KEY_B; sl@0: s->init_num=d-buf; sl@0: s->init_off=0; sl@0: } sl@0: sl@0: /* SSL2_ST_SEND_CLIENT_MASTER_KEY_B */ sl@0: return(ssl2_do_write(s)); sl@0: } sl@0: sl@0: static int client_finished(SSL *s) sl@0: { sl@0: unsigned char *p; sl@0: sl@0: if (s->state == SSL2_ST_SEND_CLIENT_FINISHED_A) sl@0: { sl@0: p=(unsigned char *)s->init_buf->data; sl@0: *(p++)=SSL2_MT_CLIENT_FINISHED; sl@0: if (s->s2->conn_id_length > sizeof s->s2->conn_id) sl@0: { sl@0: SSLerr(SSL_F_CLIENT_FINISHED, ERR_R_INTERNAL_ERROR); sl@0: return -1; sl@0: } sl@0: memcpy(p,s->s2->conn_id,(unsigned int)s->s2->conn_id_length); sl@0: sl@0: s->state=SSL2_ST_SEND_CLIENT_FINISHED_B; sl@0: s->init_num=s->s2->conn_id_length+1; sl@0: s->init_off=0; sl@0: } sl@0: return(ssl2_do_write(s)); sl@0: } sl@0: sl@0: /* read the data and then respond */ sl@0: static int client_certificate(SSL *s) sl@0: { sl@0: unsigned char *buf; sl@0: unsigned char *p,*d; sl@0: int i; sl@0: unsigned int n; sl@0: int cert_ch_len; sl@0: unsigned char *cert_ch; sl@0: sl@0: buf=(unsigned char *)s->init_buf->data; sl@0: sl@0: /* We have a cert associated with the SSL, so attach it to sl@0: * the session if it does not have one */ sl@0: sl@0: if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_A) sl@0: { sl@0: i=ssl2_read(s,(char *)&(buf[s->init_num]), sl@0: SSL2_MAX_CERT_CHALLENGE_LENGTH+2-s->init_num); sl@0: if (i<(SSL2_MIN_CERT_CHALLENGE_LENGTH+2-s->init_num)) sl@0: return(ssl2_part_read(s,SSL_F_CLIENT_CERTIFICATE,i)); sl@0: s->init_num += i; sl@0: if (s->msg_callback) sl@0: s->msg_callback(0, s->version, 0, buf, (size_t)s->init_num, s, s->msg_callback_arg); /* REQUEST-CERTIFICATE */ sl@0: sl@0: /* type=buf[0]; */ sl@0: /* type eq x509 */ sl@0: if (buf[1] != SSL2_AT_MD5_WITH_RSA_ENCRYPTION) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE); sl@0: SSLerr(SSL_F_CLIENT_CERTIFICATE,SSL_R_BAD_AUTHENTICATION_TYPE); sl@0: return(-1); sl@0: } sl@0: sl@0: if ((s->cert == NULL) || sl@0: (s->cert->key->x509 == NULL) || sl@0: (s->cert->key->privatekey == NULL)) sl@0: { sl@0: s->state=SSL2_ST_X509_GET_CLIENT_CERTIFICATE; sl@0: } sl@0: else sl@0: s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_C; sl@0: } sl@0: sl@0: cert_ch = buf + 2; sl@0: cert_ch_len = s->init_num - 2; sl@0: sl@0: if (s->state == SSL2_ST_X509_GET_CLIENT_CERTIFICATE) sl@0: { sl@0: X509 *x509=NULL; sl@0: EVP_PKEY *pkey=NULL; sl@0: sl@0: /* If we get an error we need to sl@0: * ssl->rwstate=SSL_X509_LOOKUP; sl@0: * return(error); sl@0: * We should then be retried when things are ok and we sl@0: * can get a cert or not */ sl@0: sl@0: i=0; sl@0: if (s->ctx->client_cert_cb != NULL) sl@0: { sl@0: i=s->ctx->client_cert_cb(s,&(x509),&(pkey)); sl@0: } sl@0: sl@0: if (i < 0) sl@0: { sl@0: s->rwstate=SSL_X509_LOOKUP; sl@0: return(-1); sl@0: } sl@0: s->rwstate=SSL_NOTHING; sl@0: sl@0: if ((i == 1) && (pkey != NULL) && (x509 != NULL)) sl@0: { sl@0: s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_C; sl@0: if ( !SSL_use_certificate(s,x509) || sl@0: !SSL_use_PrivateKey(s,pkey)) sl@0: { sl@0: i=0; sl@0: } sl@0: X509_free(x509); sl@0: EVP_PKEY_free(pkey); sl@0: } sl@0: else if (i == 1) sl@0: { sl@0: if (x509 != NULL) X509_free(x509); sl@0: if (pkey != NULL) EVP_PKEY_free(pkey); sl@0: SSLerr(SSL_F_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK); sl@0: i=0; sl@0: } sl@0: sl@0: if (i == 0) sl@0: { sl@0: /* We have no client certificate to respond with sl@0: * so send the correct error message back */ sl@0: s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_B; sl@0: p=buf; sl@0: *(p++)=SSL2_MT_ERROR; sl@0: s2n(SSL2_PE_NO_CERTIFICATE,p); sl@0: s->init_off=0; sl@0: s->init_num=3; sl@0: /* Write is done at the end */ sl@0: } sl@0: } sl@0: sl@0: if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_B) sl@0: { sl@0: return(ssl2_do_write(s)); sl@0: } sl@0: sl@0: if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_C) sl@0: { sl@0: EVP_MD_CTX ctx; sl@0: sl@0: /* ok, now we calculate the checksum sl@0: * do it first so we can reuse buf :-) */ sl@0: p=buf; sl@0: EVP_MD_CTX_init(&ctx); sl@0: EVP_SignInit_ex(&ctx,s->ctx->rsa_md5, NULL); sl@0: EVP_SignUpdate(&ctx,s->s2->key_material, sl@0: s->s2->key_material_length); sl@0: EVP_SignUpdate(&ctx,cert_ch,(unsigned int)cert_ch_len); sl@0: n=i2d_X509(s->session->sess_cert->peer_key->x509,&p); sl@0: EVP_SignUpdate(&ctx,buf,(unsigned int)n); sl@0: sl@0: p=buf; sl@0: d=p+6; sl@0: *(p++)=SSL2_MT_CLIENT_CERTIFICATE; sl@0: *(p++)=SSL2_CT_X509_CERTIFICATE; sl@0: n=i2d_X509(s->cert->key->x509,&d); sl@0: s2n(n,p); sl@0: sl@0: if (!EVP_SignFinal(&ctx,d,&n,s->cert->key->privatekey)) sl@0: { sl@0: /* this is not good. If things have failed it sl@0: * means there so something wrong with the key. sl@0: * We will continue with a 0 length signature sl@0: */ sl@0: } sl@0: EVP_MD_CTX_cleanup(&ctx); sl@0: s2n(n,p); sl@0: d+=n; sl@0: sl@0: s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_D; sl@0: s->init_num=d-buf; sl@0: s->init_off=0; sl@0: } sl@0: /* if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_D) */ sl@0: return(ssl2_do_write(s)); sl@0: } sl@0: sl@0: static int get_server_verify(SSL *s) sl@0: { sl@0: unsigned char *p; sl@0: int i, n, len; sl@0: sl@0: p=(unsigned char *)s->init_buf->data; sl@0: if (s->state == SSL2_ST_GET_SERVER_VERIFY_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_SERVER_VERIFY,i)); sl@0: s->init_num += i; sl@0: sl@0: s->state= SSL2_ST_GET_SERVER_VERIFY_B; sl@0: if (*p != SSL2_MT_SERVER_VERIFY) sl@0: { sl@0: if (p[0] != SSL2_MT_ERROR) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: SSLerr(SSL_F_GET_SERVER_VERIFY, sl@0: SSL_R_READ_WRONG_PACKET_TYPE); sl@0: } sl@0: else sl@0: { sl@0: SSLerr(SSL_F_GET_SERVER_VERIFY,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: } sl@0: sl@0: p=(unsigned char *)s->init_buf->data; sl@0: len = 1 + s->s2->challenge_length; sl@0: n = len - s->init_num; sl@0: i = ssl2_read(s,(char *)&(p[s->init_num]),n); sl@0: if (i < n) sl@0: return(ssl2_part_read(s,SSL_F_GET_SERVER_VERIFY,i)); sl@0: if (s->msg_callback) sl@0: s->msg_callback(0, s->version, 0, p, len, s, s->msg_callback_arg); /* SERVER-VERIFY */ sl@0: p += 1; sl@0: sl@0: if (memcmp(p,s->s2->challenge,s->s2->challenge_length) != 0) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: SSLerr(SSL_F_GET_SERVER_VERIFY,SSL_R_CHALLENGE_IS_DIFFERENT); sl@0: return(-1); sl@0: } sl@0: return(1); sl@0: } sl@0: sl@0: static int get_server_finished(SSL *s) sl@0: { sl@0: unsigned char *buf; sl@0: unsigned char *p; sl@0: int i, n, len; sl@0: sl@0: buf=(unsigned char *)s->init_buf->data; sl@0: p=buf; sl@0: if (s->state == SSL2_ST_GET_SERVER_FINISHED_A) sl@0: { sl@0: i=ssl2_read(s,(char *)&(buf[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_SERVER_FINISHED,i)); sl@0: s->init_num += i; sl@0: sl@0: if (*p == SSL2_MT_REQUEST_CERTIFICATE) sl@0: { sl@0: s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_A; sl@0: return(1); sl@0: } sl@0: else if (*p != SSL2_MT_SERVER_FINISHED) sl@0: { sl@0: if (p[0] != SSL2_MT_ERROR) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: SSLerr(SSL_F_GET_SERVER_FINISHED,SSL_R_READ_WRONG_PACKET_TYPE); sl@0: } sl@0: else sl@0: { sl@0: SSLerr(SSL_F_GET_SERVER_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_SERVER_FINISHED_B; sl@0: } sl@0: sl@0: len = 1 + SSL2_SSL_SESSION_ID_LENGTH; sl@0: n = len - s->init_num; sl@0: i = ssl2_read(s,(char *)&(buf[s->init_num]), n); sl@0: if (i < n) /* XXX could be shorter than SSL2_SSL_SESSION_ID_LENGTH, that's the maximum */ sl@0: return(ssl2_part_read(s,SSL_F_GET_SERVER_FINISHED,i)); sl@0: s->init_num += i; sl@0: if (s->msg_callback) sl@0: s->msg_callback(0, s->version, 0, buf, (size_t)s->init_num, s, s->msg_callback_arg); /* SERVER-FINISHED */ sl@0: sl@0: if (!s->hit) /* new session */ sl@0: { sl@0: /* new session-id */ sl@0: /* Make sure we were not trying to re-use an old SSL_SESSION sl@0: * or bad things can happen */ sl@0: /* ZZZZZZZZZZZZZ */ sl@0: s->session->session_id_length=SSL2_SSL_SESSION_ID_LENGTH; sl@0: memcpy(s->session->session_id,p+1,SSL2_SSL_SESSION_ID_LENGTH); sl@0: } sl@0: else sl@0: { sl@0: if (!(s->options & SSL_OP_MICROSOFT_SESS_ID_BUG)) sl@0: { sl@0: if ((s->session->session_id_length > sizeof s->session->session_id) sl@0: || (0 != memcmp(buf + 1, s->session->session_id, sl@0: (unsigned int)s->session->session_id_length))) sl@0: { sl@0: ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR); sl@0: SSLerr(SSL_F_GET_SERVER_FINISHED,SSL_R_SSL_SESSION_ID_IS_DIFFERENT); sl@0: return(-1); sl@0: } sl@0: } sl@0: } sl@0: s->state = SSL_ST_OK; sl@0: return(1); sl@0: } sl@0: sl@0: /* loads in the certificate from the server */ sl@0: int ssl2_set_certificate(SSL *s, int type, int len, const unsigned char *data) sl@0: { sl@0: STACK_OF(X509) *sk=NULL; sl@0: EVP_PKEY *pkey=NULL; sl@0: SESS_CERT *sc=NULL; sl@0: int i; sl@0: X509 *x509=NULL; sl@0: int ret=0; sl@0: sl@0: x509=d2i_X509(NULL,&data,(long)len); sl@0: if (x509 == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL2_SET_CERTIFICATE,ERR_R_X509_LIB); sl@0: goto err; 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_SSL2_SET_CERTIFICATE,ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: sl@0: i=ssl_verify_cert_chain(s,sk); sl@0: sl@0: if ((s->verify_mode != SSL_VERIFY_NONE) && (!i)) sl@0: { sl@0: SSLerr(SSL_F_SSL2_SET_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED); sl@0: goto err; sl@0: } sl@0: ERR_clear_error(); /* but we keep s->verify_result */ sl@0: s->session->verify_result = s->verify_result; sl@0: sl@0: /* server's cert for this session */ sl@0: sc=ssl_sess_cert_new(); sl@0: if (sc == NULL) sl@0: { sl@0: ret= -1; sl@0: goto err; sl@0: } sl@0: if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert); sl@0: s->session->sess_cert=sc; sl@0: sl@0: sc->peer_pkeys[SSL_PKEY_RSA_ENC].x509=x509; sl@0: sc->peer_key= &(sc->peer_pkeys[SSL_PKEY_RSA_ENC]); sl@0: sl@0: pkey=X509_get_pubkey(x509); sl@0: x509=NULL; sl@0: if (pkey == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL2_SET_CERTIFICATE,SSL_R_UNABLE_TO_EXTRACT_PUBLIC_KEY); sl@0: goto err; sl@0: } sl@0: if (pkey->type != EVP_PKEY_RSA) sl@0: { sl@0: SSLerr(SSL_F_SSL2_SET_CERTIFICATE,SSL_R_PUBLIC_KEY_NOT_RSA); sl@0: goto err; sl@0: } sl@0: sl@0: if (!ssl_set_peer_cert_type(sc,SSL2_CT_X509_CERTIFICATE)) sl@0: goto err; sl@0: ret=1; sl@0: err: sl@0: sk_X509_free(sk); sl@0: X509_free(x509); sl@0: EVP_PKEY_free(pkey); sl@0: return(ret); sl@0: } sl@0: sl@0: static int ssl_rsa_public_encrypt(SESS_CERT *sc, int len, unsigned char *from, sl@0: unsigned char *to, int padding) sl@0: { sl@0: EVP_PKEY *pkey=NULL; sl@0: int i= -1; sl@0: sl@0: if ((sc == NULL) || (sc->peer_key->x509 == NULL) || sl@0: ((pkey=X509_get_pubkey(sc->peer_key->x509)) == NULL)) sl@0: { sl@0: SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT,SSL_R_NO_PUBLICKEY); sl@0: return(-1); sl@0: } sl@0: if (pkey->type != EVP_PKEY_RSA) sl@0: { sl@0: SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT,SSL_R_PUBLIC_KEY_IS_NOT_RSA); sl@0: goto end; sl@0: } sl@0: sl@0: /* we have the public key */ sl@0: i=RSA_public_encrypt(len,from,to,pkey->pkey.rsa,padding); sl@0: if (i < 0) sl@0: SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT,ERR_R_RSA_LIB); sl@0: end: sl@0: EVP_PKEY_free(pkey); 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