sl@0: /* ssl/s3_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-2003 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: * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. sl@0: * sl@0: * Portions of the attached software ("Contribution") are developed by sl@0: * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. sl@0: * sl@0: * The Contribution is licensed pursuant to the OpenSSL open source sl@0: * license provided above. sl@0: * sl@0: * ECC cipher suite support in OpenSSL originally written by sl@0: * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories. sl@0: * sl@0: */ sl@0: /* sl@0: © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. sl@0: */ sl@0: sl@0: #include sl@0: #include "ssl_locl.h" sl@0: #include "kssl_lcl.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #ifndef OPENSSL_NO_DH sl@0: #include sl@0: #endif sl@0: #include sl@0: 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(SSLv3_client_method_data,s3_clnt,SSL_METHOD) sl@0: sl@0: #define SSLv3_client_method_data (*GET_WSD_VAR_NAME(SSLv3_client_method_data,s3_clnt,s)()) sl@0: sl@0: #endif sl@0: sl@0: static SSL_METHOD *ssl3_get_client_method(int ver); sl@0: static int ca_dn_cmp(const X509_NAME * const *a,const X509_NAME * const *b); sl@0: sl@0: #ifndef OPENSSL_NO_ECDH sl@0: static int curve_id2nid(int curve_id); sl@0: int check_srvr_ecc_cert_and_alg(X509 *x, SSL_CIPHER *cs); sl@0: #endif sl@0: sl@0: static SSL_METHOD *ssl3_get_client_method(int ver) sl@0: { sl@0: if (ver == SSL3_VERSION) sl@0: return(SSLv3_client_method()); sl@0: else sl@0: return(NULL); sl@0: } sl@0: sl@0: EXPORT_C IMPLEMENT_ssl3_meth_func(SSLv3_client_method, sl@0: ssl_undefined_function, sl@0: ssl3_connect, sl@0: ssl3_get_client_method) sl@0: sl@0: int ssl3_connect(SSL *s) sl@0: { sl@0: BUF_MEM *buf=NULL; sl@0: unsigned long Time=(unsigned long)time(NULL),l; sl@0: long num1; sl@0: void (*cb)(const SSL *ssl,int type,int val)=NULL; sl@0: int ret= -1; sl@0: int new_state,state,skip=0;; sl@0: sl@0: RAND_add(&Time,sizeof(Time),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: 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_RENEGOTIATE: sl@0: s->new_session=1; sl@0: s->state=SSL_ST_CONNECT; sl@0: s->ctx->stats.sess_connect_renegotiate++; sl@0: /* break */ 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: if ((s->version & 0xff00 ) != 0x0300) sl@0: { sl@0: SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR); sl@0: ret = -1; sl@0: goto end; sl@0: } sl@0: sl@0: /* s->version=SSL3_VERSION; */ sl@0: s->type=SSL_ST_CONNECT; sl@0: sl@0: if (s->init_buf == NULL) sl@0: { sl@0: if ((buf=BUF_MEM_new()) == NULL) sl@0: { sl@0: ret= -1; sl@0: goto end; sl@0: } sl@0: if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH)) sl@0: { sl@0: ret= -1; sl@0: goto end; sl@0: } sl@0: s->init_buf=buf; sl@0: buf=NULL; sl@0: } sl@0: sl@0: if (!ssl3_setup_buffers(s)) { ret= -1; goto end; } sl@0: sl@0: /* setup buffing BIO */ sl@0: if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; } sl@0: sl@0: /* don't push the buffering BIO quite yet */ sl@0: sl@0: ssl3_init_finished_mac(s); sl@0: sl@0: s->state=SSL3_ST_CW_CLNT_HELLO_A; sl@0: s->ctx->stats.sess_connect++; sl@0: s->init_num=0; sl@0: break; sl@0: sl@0: case SSL3_ST_CW_CLNT_HELLO_A: sl@0: case SSL3_ST_CW_CLNT_HELLO_B: sl@0: sl@0: s->shutdown=0; sl@0: ret=ssl3_client_hello(s); sl@0: if (ret <= 0) goto end; sl@0: s->state=SSL3_ST_CR_SRVR_HELLO_A; sl@0: s->init_num=0; sl@0: sl@0: /* turn on buffering for the next lot of output */ sl@0: if (s->bbio != s->wbio) sl@0: s->wbio=BIO_push(s->bbio,s->wbio); sl@0: sl@0: break; sl@0: sl@0: case SSL3_ST_CR_SRVR_HELLO_A: sl@0: case SSL3_ST_CR_SRVR_HELLO_B: sl@0: ret=ssl3_get_server_hello(s); sl@0: if (ret <= 0) goto end; sl@0: if (s->hit) sl@0: s->state=SSL3_ST_CR_FINISHED_A; sl@0: else sl@0: s->state=SSL3_ST_CR_CERT_A; sl@0: s->init_num=0; sl@0: break; sl@0: sl@0: case SSL3_ST_CR_CERT_A: sl@0: case SSL3_ST_CR_CERT_B: sl@0: sl@0: /* Check if it is anon DH/ECDH */ sl@0: if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)) sl@0: { sl@0: ret=ssl3_get_server_certificate(s); sl@0: if (ret <= 0) goto end; sl@0: } sl@0: else sl@0: skip=1; sl@0: s->state=SSL3_ST_CR_KEY_EXCH_A; sl@0: s->init_num=0; sl@0: break; sl@0: sl@0: case SSL3_ST_CR_KEY_EXCH_A: sl@0: case SSL3_ST_CR_KEY_EXCH_B: sl@0: ret=ssl3_get_key_exchange(s); sl@0: if (ret <= 0) goto end; sl@0: s->state=SSL3_ST_CR_CERT_REQ_A; sl@0: s->init_num=0; sl@0: sl@0: /* at this point we check that we have the sl@0: * required stuff from the server */ sl@0: if (!ssl3_check_cert_and_algorithm(s)) sl@0: { sl@0: ret= -1; sl@0: goto end; sl@0: } sl@0: break; sl@0: sl@0: case SSL3_ST_CR_CERT_REQ_A: sl@0: case SSL3_ST_CR_CERT_REQ_B: sl@0: ret=ssl3_get_certificate_request(s); sl@0: if (ret <= 0) goto end; sl@0: s->state=SSL3_ST_CR_SRVR_DONE_A; sl@0: s->init_num=0; sl@0: break; sl@0: sl@0: case SSL3_ST_CR_SRVR_DONE_A: sl@0: case SSL3_ST_CR_SRVR_DONE_B: sl@0: ret=ssl3_get_server_done(s); sl@0: if (ret <= 0) goto end; sl@0: if (s->s3->tmp.cert_req) sl@0: s->state=SSL3_ST_CW_CERT_A; sl@0: else sl@0: s->state=SSL3_ST_CW_KEY_EXCH_A; sl@0: s->init_num=0; sl@0: sl@0: break; sl@0: sl@0: case SSL3_ST_CW_CERT_A: sl@0: case SSL3_ST_CW_CERT_B: sl@0: case SSL3_ST_CW_CERT_C: sl@0: case SSL3_ST_CW_CERT_D: sl@0: ret=ssl3_send_client_certificate(s); sl@0: if (ret <= 0) goto end; sl@0: s->state=SSL3_ST_CW_KEY_EXCH_A; sl@0: s->init_num=0; sl@0: break; sl@0: sl@0: case SSL3_ST_CW_KEY_EXCH_A: sl@0: case SSL3_ST_CW_KEY_EXCH_B: sl@0: ret=ssl3_send_client_key_exchange(s); sl@0: if (ret <= 0) goto end; sl@0: l=s->s3->tmp.new_cipher->algorithms; sl@0: /* EAY EAY EAY need to check for DH fix cert sl@0: * sent back */ sl@0: /* For TLS, cert_req is set to 2, so a cert chain sl@0: * of nothing is sent, but no verify packet is sent */ sl@0: /* XXX: For now, we do not support client sl@0: * authentication in ECDH cipher suites with sl@0: * ECDH (rather than ECDSA) certificates. sl@0: * We need to skip the certificate verify sl@0: * message when client's ECDH public key is sent sl@0: * inside the client certificate. sl@0: */ sl@0: if (s->s3->tmp.cert_req == 1) sl@0: { sl@0: s->state=SSL3_ST_CW_CERT_VRFY_A; sl@0: } sl@0: else sl@0: { sl@0: s->state=SSL3_ST_CW_CHANGE_A; sl@0: s->s3->change_cipher_spec=0; sl@0: } sl@0: sl@0: s->init_num=0; sl@0: break; sl@0: sl@0: case SSL3_ST_CW_CERT_VRFY_A: sl@0: case SSL3_ST_CW_CERT_VRFY_B: sl@0: ret=ssl3_send_client_verify(s); sl@0: if (ret <= 0) goto end; sl@0: s->state=SSL3_ST_CW_CHANGE_A; sl@0: s->init_num=0; sl@0: s->s3->change_cipher_spec=0; sl@0: break; sl@0: sl@0: case SSL3_ST_CW_CHANGE_A: sl@0: case SSL3_ST_CW_CHANGE_B: sl@0: ret=ssl3_send_change_cipher_spec(s, sl@0: SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B); sl@0: if (ret <= 0) goto end; sl@0: s->state=SSL3_ST_CW_FINISHED_A; sl@0: s->init_num=0; sl@0: sl@0: s->session->cipher=s->s3->tmp.new_cipher; sl@0: #ifdef OPENSSL_NO_COMP sl@0: s->session->compress_meth=0; sl@0: #else sl@0: if (s->s3->tmp.new_compression == NULL) sl@0: s->session->compress_meth=0; sl@0: else sl@0: s->session->compress_meth= sl@0: s->s3->tmp.new_compression->id; sl@0: #endif sl@0: if (!s->method->ssl3_enc->setup_key_block(s)) sl@0: { sl@0: ret= -1; sl@0: goto end; sl@0: } sl@0: sl@0: if (!s->method->ssl3_enc->change_cipher_state(s, sl@0: SSL3_CHANGE_CIPHER_CLIENT_WRITE)) sl@0: { sl@0: ret= -1; sl@0: goto end; sl@0: } sl@0: sl@0: break; sl@0: sl@0: case SSL3_ST_CW_FINISHED_A: sl@0: case SSL3_ST_CW_FINISHED_B: sl@0: ret=ssl3_send_finished(s, sl@0: SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B, sl@0: s->method->ssl3_enc->client_finished_label, sl@0: s->method->ssl3_enc->client_finished_label_len); sl@0: if (ret <= 0) goto end; sl@0: s->state=SSL3_ST_CW_FLUSH; sl@0: sl@0: /* clear flags */ sl@0: s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER; sl@0: if (s->hit) sl@0: { sl@0: s->s3->tmp.next_state=SSL_ST_OK; sl@0: if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) sl@0: { sl@0: s->state=SSL_ST_OK; sl@0: s->s3->flags|=SSL3_FLAGS_POP_BUFFER; sl@0: s->s3->delay_buf_pop_ret=0; sl@0: } sl@0: } sl@0: else sl@0: { sl@0: sl@0: sl@0: s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A; sl@0: } sl@0: s->init_num=0; sl@0: break; sl@0: sl@0: sl@0: sl@0: case SSL3_ST_CR_FINISHED_A: sl@0: case SSL3_ST_CR_FINISHED_B: sl@0: sl@0: ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A, sl@0: SSL3_ST_CR_FINISHED_B); sl@0: if (ret <= 0) goto end; sl@0: sl@0: if (s->hit) sl@0: s->state=SSL3_ST_CW_CHANGE_A; sl@0: else sl@0: s->state=SSL_ST_OK; sl@0: s->init_num=0; sl@0: break; sl@0: sl@0: case SSL3_ST_CW_FLUSH: sl@0: /* number of bytes to be flushed */ 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: s->state=s->s3->tmp.next_state; sl@0: break; sl@0: sl@0: case SSL_ST_OK: sl@0: /* clean a few things up */ sl@0: ssl3_cleanup_key_block(s); sl@0: 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: sl@0: /* If we are not 'joining' the last two packets, sl@0: * remove the buffering now */ sl@0: if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER)) sl@0: ssl_free_wbio_buffer(s); sl@0: /* else do it later in ssl3_write */ sl@0: sl@0: s->init_num=0; sl@0: s->new_session=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->handshake_func=ssl3_connect; 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: sl@0: default: sl@0: SSLerr(SSL_F_SSL3_CONNECT,SSL_R_UNKNOWN_STATE); sl@0: ret= -1; sl@0: goto end; sl@0: /* break; */ sl@0: } sl@0: sl@0: /* did we do anything */ sl@0: if (!s->s3->tmp.reuse_message && !skip) sl@0: { sl@0: if (s->debug) sl@0: { sl@0: if ((ret=BIO_flush(s->wbio)) <= 0) sl@0: goto end; 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: skip=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: sl@0: int ssl3_client_hello(SSL *s) sl@0: { sl@0: unsigned char *buf; sl@0: unsigned char *p,*d; sl@0: int i; sl@0: unsigned long Time,l; sl@0: #ifndef OPENSSL_NO_COMP sl@0: int j; sl@0: SSL_COMP *comp; sl@0: #endif sl@0: sl@0: buf=(unsigned char *)s->init_buf->data; sl@0: if (s->state == SSL3_ST_CW_CLNT_HELLO_A) sl@0: { sl@0: if ((s->session == NULL) || sl@0: (s->session->ssl_version != s->version) || sl@0: (s->session->not_resumable)) sl@0: { sl@0: if (!ssl_get_new_session(s,0)) sl@0: goto err; sl@0: } sl@0: /* else use the pre-loaded session */ sl@0: sl@0: p=s->s3->client_random; sl@0: Time=(unsigned long)time(NULL); /* Time */ sl@0: l2n(Time,p); sl@0: if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0) sl@0: goto err; sl@0: sl@0: /* Do the message type and length last */ sl@0: d=p= &(buf[4]); sl@0: sl@0: *(p++)=s->version>>8; sl@0: *(p++)=s->version&0xff; sl@0: s->client_version=s->version; sl@0: sl@0: /* Random stuff */ sl@0: memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE); sl@0: p+=SSL3_RANDOM_SIZE; sl@0: sl@0: /* Session ID */ sl@0: if (s->new_session) sl@0: i=0; sl@0: else sl@0: i=s->session->session_id_length; sl@0: *(p++)=i; sl@0: if (i != 0) sl@0: { sl@0: if (i > (int)sizeof(s->session->session_id)) sl@0: { sl@0: SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); sl@0: goto err; sl@0: } sl@0: memcpy(p,s->session->session_id,i); sl@0: p+=i; sl@0: } sl@0: sl@0: /* Ciphers supported */ sl@0: i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),0); sl@0: if (i == 0) sl@0: { sl@0: SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE); sl@0: goto err; sl@0: } sl@0: s2n(i,p); sl@0: p+=i; sl@0: sl@0: /* COMPRESSION */ sl@0: #ifdef OPENSSL_NO_COMP sl@0: *(p++)=1; sl@0: #else sl@0: if (s->ctx->comp_methods == NULL) sl@0: j=0; sl@0: else sl@0: j=sk_SSL_COMP_num(s->ctx->comp_methods); sl@0: *(p++)=1+j; sl@0: for (i=0; ictx->comp_methods,i); sl@0: *(p++)=comp->id; sl@0: } sl@0: #endif sl@0: *(p++)=0; /* Add the NULL method */ sl@0: sl@0: l=(p-d); sl@0: d=buf; sl@0: *(d++)=SSL3_MT_CLIENT_HELLO; sl@0: l2n3(l,d); sl@0: sl@0: s->state=SSL3_ST_CW_CLNT_HELLO_B; sl@0: /* number of bytes to write */ sl@0: s->init_num=p-buf; sl@0: s->init_off=0; sl@0: } sl@0: sl@0: /* SSL3_ST_CW_CLNT_HELLO_B */ sl@0: return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); sl@0: err: sl@0: return(-1); sl@0: } sl@0: sl@0: int ssl3_get_server_hello(SSL *s) sl@0: { sl@0: STACK_OF(SSL_CIPHER) *sk; sl@0: SSL_CIPHER *c; sl@0: unsigned char *p,*d; sl@0: int i,al,ok; sl@0: unsigned int j; sl@0: long n; sl@0: #ifndef OPENSSL_NO_COMP sl@0: SSL_COMP *comp; sl@0: #endif sl@0: sl@0: n=s->method->ssl_get_message(s, sl@0: SSL3_ST_CR_SRVR_HELLO_A, sl@0: SSL3_ST_CR_SRVR_HELLO_B, sl@0: -1, sl@0: 20000, /* ?? */ sl@0: &ok); sl@0: sl@0: if (!ok) return((int)n); sl@0: sl@0: if ( SSL_version(s) == DTLS1_VERSION) sl@0: { sl@0: if ( s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) sl@0: { sl@0: if ( s->d1->send_cookie == 0) sl@0: { sl@0: s->s3->tmp.reuse_message = 1; sl@0: return 1; sl@0: } sl@0: else /* already sent a cookie */ sl@0: { sl@0: al=SSL_AD_UNEXPECTED_MESSAGE; sl@0: SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE); sl@0: goto f_err; sl@0: } sl@0: } sl@0: } sl@0: sl@0: if ( s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) sl@0: { sl@0: al=SSL_AD_UNEXPECTED_MESSAGE; sl@0: SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE); sl@0: goto f_err; sl@0: } sl@0: sl@0: d=p=(unsigned char *)s->init_msg; sl@0: sl@0: if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff))) sl@0: { sl@0: SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION); sl@0: s->version=(s->version&0xff00)|p[1]; sl@0: al=SSL_AD_PROTOCOL_VERSION; sl@0: goto f_err; sl@0: } sl@0: p+=2; sl@0: sl@0: /* load the server hello data */ sl@0: /* load the server random */ sl@0: memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE); sl@0: p+=SSL3_RANDOM_SIZE; sl@0: sl@0: /* get the session-id */ sl@0: j= *(p++); sl@0: sl@0: if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE)) sl@0: { sl@0: al=SSL_AD_ILLEGAL_PARAMETER; sl@0: SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_LONG); sl@0: goto f_err; sl@0: } sl@0: sl@0: if (j != 0 && j == s->session->session_id_length sl@0: && memcmp(p,s->session->session_id,j) == 0) sl@0: { sl@0: if(s->sid_ctx_length != s->session->sid_ctx_length sl@0: || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length)) sl@0: { sl@0: /* actually a client application bug */ sl@0: al=SSL_AD_ILLEGAL_PARAMETER; sl@0: SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); sl@0: goto f_err; sl@0: } sl@0: s->hit=1; sl@0: } sl@0: else /* a miss or crap from the other end */ sl@0: { sl@0: /* If we were trying for session-id reuse, make a new sl@0: * SSL_SESSION so we don't stuff up other people */ sl@0: s->hit=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: al=SSL_AD_INTERNAL_ERROR; sl@0: goto f_err; sl@0: } sl@0: } sl@0: s->session->session_id_length=j; sl@0: memcpy(s->session->session_id,p,j); /* j could be 0 */ sl@0: } sl@0: p+=j; sl@0: c=ssl_get_cipher_by_char(s,p); sl@0: if (c == NULL) sl@0: { sl@0: /* unknown cipher */ sl@0: al=SSL_AD_ILLEGAL_PARAMETER; sl@0: SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNKNOWN_CIPHER_RETURNED); sl@0: goto f_err; sl@0: } sl@0: p+=ssl_put_cipher_by_char(s,NULL,NULL); sl@0: sl@0: sk=ssl_get_ciphers_by_id(s); sl@0: i=sk_SSL_CIPHER_find(sk,c); sl@0: if (i < 0) sl@0: { sl@0: /* we did not say we would use this cipher */ sl@0: al=SSL_AD_ILLEGAL_PARAMETER; sl@0: SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED); sl@0: goto f_err; sl@0: } sl@0: sl@0: /* Depending on the session caching (internal/external), the cipher sl@0: and/or cipher_id values may not be set. Make sure that sl@0: cipher_id is set and use it for comparison. */ sl@0: if (s->session->cipher) sl@0: s->session->cipher_id = s->session->cipher->id; sl@0: if (s->hit && (s->session->cipher_id != c->id)) sl@0: { sl@0: if (!(s->options & sl@0: SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG)) sl@0: { sl@0: al=SSL_AD_ILLEGAL_PARAMETER; sl@0: SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); sl@0: goto f_err; sl@0: } sl@0: } sl@0: s->s3->tmp.new_cipher=c; sl@0: sl@0: /* lets get the compression algorithm */ sl@0: /* COMPRESSION */ sl@0: #ifdef OPENSSL_NO_COMP sl@0: if (*(p++) != 0) sl@0: { sl@0: al=SSL_AD_ILLEGAL_PARAMETER; sl@0: SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); sl@0: goto f_err; sl@0: } sl@0: #else sl@0: j= *(p++); sl@0: if (j == 0) sl@0: comp=NULL; sl@0: else sl@0: comp=ssl3_comp_find(s->ctx->comp_methods,j); sl@0: sl@0: if ((j != 0) && (comp == NULL)) sl@0: { sl@0: al=SSL_AD_ILLEGAL_PARAMETER; sl@0: SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); sl@0: goto f_err; sl@0: } sl@0: else sl@0: { sl@0: s->s3->tmp.new_compression=comp; sl@0: } sl@0: #endif sl@0: sl@0: sl@0: sl@0: if (p != (d+n)) sl@0: { sl@0: /* wrong packet length */ sl@0: al=SSL_AD_DECODE_ERROR; sl@0: SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH); sl@0: goto err; sl@0: } sl@0: sl@0: return(1); 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_get_server_certificate(SSL *s) sl@0: { sl@0: int al,i,ok,ret= -1; sl@0: unsigned long n,nc,llen,l; sl@0: X509 *x=NULL; sl@0: const unsigned char *q,*p; sl@0: unsigned char *d; sl@0: STACK_OF(X509) *sk=NULL; sl@0: SESS_CERT *sc; sl@0: EVP_PKEY *pkey=NULL; sl@0: int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */ sl@0: sl@0: n=s->method->ssl_get_message(s, sl@0: SSL3_ST_CR_CERT_A, sl@0: SSL3_ST_CR_CERT_B, sl@0: -1, sl@0: s->max_cert_list, sl@0: &ok); sl@0: sl@0: if (!ok) return((int)n); sl@0: sl@0: if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) || sl@0: ((s->s3->tmp.new_cipher->algorithms & SSL_aKRB5) && sl@0: (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE))) sl@0: { sl@0: s->s3->tmp.reuse_message=1; sl@0: return(1); sl@0: } sl@0: sl@0: if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) sl@0: { sl@0: al=SSL_AD_UNEXPECTED_MESSAGE; sl@0: SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE); sl@0: goto f_err; sl@0: } sl@0: p=d=(unsigned char *)s->init_msg; sl@0: sl@0: if ((sk=sk_X509_new_null()) == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: sl@0: n2l3(p,llen); sl@0: if (llen+3 != n) sl@0: { sl@0: al=SSL_AD_DECODE_ERROR; sl@0: SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH); sl@0: goto f_err; sl@0: } sl@0: for (nc=0; nc llen) sl@0: { sl@0: al=SSL_AD_DECODE_ERROR; sl@0: SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH); sl@0: goto f_err; sl@0: } sl@0: sl@0: q=p; sl@0: x=d2i_X509(NULL,&q,l); sl@0: if (x == NULL) sl@0: { sl@0: al=SSL_AD_BAD_CERTIFICATE; sl@0: SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB); sl@0: goto f_err; sl@0: } sl@0: if (q != (p+l)) sl@0: { sl@0: al=SSL_AD_DECODE_ERROR; sl@0: SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH); sl@0: goto f_err; sl@0: } sl@0: if (!sk_X509_push(sk,x)) sl@0: { sl@0: SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: x=NULL; sl@0: nc+=l+3; sl@0: p=q; sl@0: } sl@0: sl@0: i=ssl_verify_cert_chain(s,sk); sl@0: if ((s->verify_mode != SSL_VERIFY_NONE) && (!i) sl@0: #ifndef OPENSSL_NO_KRB5 sl@0: && (s->s3->tmp.new_cipher->algorithms & (SSL_MKEY_MASK|SSL_AUTH_MASK)) sl@0: != (SSL_aKRB5|SSL_kKRB5) sl@0: #endif /* OPENSSL_NO_KRB5 */ sl@0: ) sl@0: { sl@0: al=ssl_verify_alarm_type(s->verify_result); sl@0: SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED); sl@0: goto f_err; sl@0: } sl@0: ERR_clear_error(); /* but we keep s->verify_result */ sl@0: sl@0: sc=ssl_sess_cert_new(); sl@0: if (sc == NULL) 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->cert_chain=sk; sl@0: /* Inconsistency alert: cert_chain does include the peer's sl@0: * certificate, which we don't include in s3_srvr.c */ sl@0: x=sk_X509_value(sk,0); sl@0: sk=NULL; sl@0: /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/ sl@0: sl@0: pkey=X509_get_pubkey(x); sl@0: sl@0: /* VRS: allow null cert if auth == KRB5 */ sl@0: need_cert = ((s->s3->tmp.new_cipher->algorithms sl@0: & (SSL_MKEY_MASK|SSL_AUTH_MASK)) sl@0: == (SSL_aKRB5|SSL_kKRB5))? 0: 1; sl@0: sl@0: #ifdef KSSL_DEBUG sl@0: printf("pkey,x = %p, %p\n", pkey,x); sl@0: printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey)); sl@0: printf("cipher, alg, nc = %s, %lx, %d\n", s->s3->tmp.new_cipher->name, sl@0: s->s3->tmp.new_cipher->algorithms, need_cert); sl@0: #endif /* KSSL_DEBUG */ sl@0: sl@0: if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))) sl@0: { sl@0: x=NULL; sl@0: al=SSL3_AL_FATAL; sl@0: SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, sl@0: SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS); sl@0: goto f_err; sl@0: } sl@0: sl@0: i=ssl_cert_type(x,pkey); sl@0: if (need_cert && i < 0) sl@0: { sl@0: x=NULL; sl@0: al=SSL3_AL_FATAL; sl@0: SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, sl@0: SSL_R_UNKNOWN_CERTIFICATE_TYPE); sl@0: goto f_err; sl@0: } sl@0: sl@0: if (need_cert) sl@0: { sl@0: sc->peer_cert_type=i; sl@0: CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509); sl@0: /* Why would the following ever happen? sl@0: * We just created sc a couple of lines ago. */ sl@0: if (sc->peer_pkeys[i].x509 != NULL) sl@0: X509_free(sc->peer_pkeys[i].x509); sl@0: sc->peer_pkeys[i].x509=x; sl@0: sc->peer_key= &(sc->peer_pkeys[i]); sl@0: sl@0: if (s->session->peer != NULL) sl@0: X509_free(s->session->peer); sl@0: CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509); sl@0: s->session->peer=x; sl@0: } sl@0: else sl@0: { sl@0: sc->peer_cert_type=i; sl@0: sc->peer_key= NULL; sl@0: sl@0: if (s->session->peer != NULL) sl@0: X509_free(s->session->peer); sl@0: s->session->peer=NULL; sl@0: } sl@0: s->session->verify_result = s->verify_result; sl@0: sl@0: x=NULL; sl@0: ret=1; sl@0: sl@0: if (0) sl@0: { sl@0: f_err: sl@0: ssl3_send_alert(s,SSL3_AL_FATAL,al); sl@0: } sl@0: err: sl@0: EVP_PKEY_free(pkey); sl@0: X509_free(x); sl@0: sk_X509_pop_free(sk,X509_free); sl@0: return(ret); sl@0: } sl@0: sl@0: int ssl3_get_key_exchange(SSL *s) sl@0: { sl@0: #ifndef OPENSSL_NO_RSA sl@0: unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2]; sl@0: #endif sl@0: EVP_MD_CTX md_ctx; sl@0: unsigned char *param,*p; sl@0: int al,i,j,param_len,ok; sl@0: long n,alg; sl@0: EVP_PKEY *pkey=NULL; sl@0: #ifndef OPENSSL_NO_RSA sl@0: RSA *rsa=NULL; sl@0: #endif sl@0: #ifndef OPENSSL_NO_DH sl@0: DH *dh=NULL; sl@0: #endif sl@0: #ifndef OPENSSL_NO_ECDH sl@0: EC_KEY *ecdh = NULL; sl@0: BN_CTX *bn_ctx = NULL; sl@0: EC_POINT *srvr_ecpoint = NULL; sl@0: int curve_nid = 0; sl@0: int encoded_pt_len = 0; sl@0: #endif sl@0: sl@0: /* use same message size as in ssl3_get_certificate_request() sl@0: * as ServerKeyExchange message may be skipped */ sl@0: n=s->method->ssl_get_message(s, sl@0: SSL3_ST_CR_KEY_EXCH_A, sl@0: SSL3_ST_CR_KEY_EXCH_B, sl@0: -1, sl@0: s->max_cert_list, sl@0: &ok); sl@0: sl@0: if (!ok) return((int)n); sl@0: sl@0: if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) sl@0: { sl@0: s->s3->tmp.reuse_message=1; sl@0: return(1); sl@0: } sl@0: sl@0: param=p=(unsigned char *)s->init_msg; sl@0: sl@0: if (s->session->sess_cert != NULL) sl@0: { sl@0: #ifndef OPENSSL_NO_RSA sl@0: if (s->session->sess_cert->peer_rsa_tmp != NULL) sl@0: { sl@0: RSA_free(s->session->sess_cert->peer_rsa_tmp); sl@0: s->session->sess_cert->peer_rsa_tmp=NULL; sl@0: } sl@0: #endif sl@0: #ifndef OPENSSL_NO_DH sl@0: if (s->session->sess_cert->peer_dh_tmp) sl@0: { sl@0: DH_free(s->session->sess_cert->peer_dh_tmp); sl@0: s->session->sess_cert->peer_dh_tmp=NULL; sl@0: } sl@0: #endif sl@0: #ifndef OPENSSL_NO_ECDH sl@0: if (s->session->sess_cert->peer_ecdh_tmp) sl@0: { sl@0: EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp); sl@0: s->session->sess_cert->peer_ecdh_tmp=NULL; sl@0: } sl@0: #endif sl@0: } sl@0: else sl@0: { sl@0: s->session->sess_cert=ssl_sess_cert_new(); sl@0: } sl@0: sl@0: param_len=0; sl@0: alg=s->s3->tmp.new_cipher->algorithms; sl@0: EVP_MD_CTX_init(&md_ctx); sl@0: sl@0: #ifndef OPENSSL_NO_RSA sl@0: if (alg & SSL_kRSA) sl@0: { sl@0: if ((rsa=RSA_new()) == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: n2s(p,i); sl@0: param_len=i+2; sl@0: if (param_len > n) sl@0: { sl@0: al=SSL_AD_DECODE_ERROR; sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH); sl@0: goto f_err; sl@0: } sl@0: if (!(rsa->n=BN_bin2bn(p,i,rsa->n))) sl@0: { sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB); sl@0: goto err; sl@0: } sl@0: p+=i; sl@0: sl@0: n2s(p,i); sl@0: param_len+=i+2; sl@0: if (param_len > n) sl@0: { sl@0: al=SSL_AD_DECODE_ERROR; sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH); sl@0: goto f_err; sl@0: } sl@0: if (!(rsa->e=BN_bin2bn(p,i,rsa->e))) sl@0: { sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB); sl@0: goto err; sl@0: } sl@0: p+=i; sl@0: n-=param_len; sl@0: sl@0: /* this should be because we are using an export cipher */ sl@0: if (alg & SSL_aRSA) sl@0: pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); sl@0: else sl@0: { sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR); sl@0: goto err; sl@0: } sl@0: s->session->sess_cert->peer_rsa_tmp=rsa; sl@0: rsa=NULL; sl@0: } sl@0: #else /* OPENSSL_NO_RSA */ sl@0: if (0) sl@0: ; sl@0: #endif sl@0: #ifndef OPENSSL_NO_DH sl@0: else if (alg & SSL_kEDH) sl@0: { sl@0: if ((dh=DH_new()) == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB); sl@0: goto err; sl@0: } sl@0: n2s(p,i); sl@0: param_len=i+2; sl@0: if (param_len > n) sl@0: { sl@0: al=SSL_AD_DECODE_ERROR; sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH); sl@0: goto f_err; sl@0: } sl@0: if (!(dh->p=BN_bin2bn(p,i,NULL))) sl@0: { sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB); sl@0: goto err; sl@0: } sl@0: p+=i; sl@0: sl@0: n2s(p,i); sl@0: param_len+=i+2; sl@0: if (param_len > n) sl@0: { sl@0: al=SSL_AD_DECODE_ERROR; sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH); sl@0: goto f_err; sl@0: } sl@0: if (!(dh->g=BN_bin2bn(p,i,NULL))) sl@0: { sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB); sl@0: goto err; sl@0: } sl@0: p+=i; sl@0: sl@0: n2s(p,i); sl@0: param_len+=i+2; sl@0: if (param_len > n) sl@0: { sl@0: al=SSL_AD_DECODE_ERROR; sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH); sl@0: goto f_err; sl@0: } sl@0: if (!(dh->pub_key=BN_bin2bn(p,i,NULL))) sl@0: { sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB); sl@0: goto err; sl@0: } sl@0: p+=i; sl@0: n-=param_len; sl@0: sl@0: #ifndef OPENSSL_NO_RSA sl@0: if (alg & SSL_aRSA) sl@0: pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); sl@0: #else sl@0: if (0) sl@0: ; sl@0: #endif sl@0: #ifndef OPENSSL_NO_DSA sl@0: else if (alg & SSL_aDSS) sl@0: pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509); sl@0: #endif sl@0: /* else anonymous DH, so no certificate or pkey. */ sl@0: sl@0: s->session->sess_cert->peer_dh_tmp=dh; sl@0: dh=NULL; sl@0: } sl@0: else if ((alg & SSL_kDHr) || (alg & SSL_kDHd)) sl@0: { sl@0: al=SSL_AD_ILLEGAL_PARAMETER; sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER); sl@0: goto f_err; sl@0: } sl@0: #endif /* !OPENSSL_NO_DH */ sl@0: sl@0: #ifndef OPENSSL_NO_ECDH sl@0: else if (alg & SSL_kECDHE) sl@0: { sl@0: EC_GROUP *ngroup; sl@0: const EC_GROUP *group; sl@0: sl@0: if ((ecdh=EC_KEY_new()) == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: sl@0: /* Extract elliptic curve parameters and the sl@0: * server's ephemeral ECDH public key. sl@0: * Keep accumulating lengths of various components in sl@0: * param_len and make sure it never exceeds n. sl@0: */ sl@0: sl@0: /* XXX: For now we only support named (not generic) curves sl@0: * and the ECParameters in this case is just three bytes. sl@0: */ sl@0: param_len=3; sl@0: if ((param_len > n) || sl@0: (*p != NAMED_CURVE_TYPE) || sl@0: ((curve_nid = curve_id2nid(*(p + 2))) == 0)) sl@0: { sl@0: al=SSL_AD_INTERNAL_ERROR; sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS); sl@0: goto f_err; sl@0: } sl@0: sl@0: ngroup = EC_GROUP_new_by_curve_name(curve_nid); sl@0: if (ngroup == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB); sl@0: goto err; sl@0: } sl@0: if (EC_KEY_set_group(ecdh, ngroup) == 0) sl@0: { sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB); sl@0: goto err; sl@0: } sl@0: EC_GROUP_free(ngroup); sl@0: sl@0: group = EC_KEY_get0_group(ecdh); sl@0: sl@0: if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && sl@0: (EC_GROUP_get_degree(group) > 163)) sl@0: { sl@0: al=SSL_AD_EXPORT_RESTRICTION; sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER); sl@0: goto f_err; sl@0: } sl@0: sl@0: p+=3; sl@0: sl@0: /* Next, get the encoded ECPoint */ sl@0: if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) || sl@0: ((bn_ctx = BN_CTX_new()) == NULL)) sl@0: { sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: sl@0: encoded_pt_len = *p; /* length of encoded point */ sl@0: p+=1; sl@0: param_len += (1 + encoded_pt_len); sl@0: if ((param_len > n) || sl@0: (EC_POINT_oct2point(group, srvr_ecpoint, sl@0: p, encoded_pt_len, bn_ctx) == 0)) sl@0: { sl@0: al=SSL_AD_DECODE_ERROR; sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_ECPOINT); sl@0: goto f_err; sl@0: } sl@0: sl@0: n-=param_len; sl@0: p+=encoded_pt_len; sl@0: sl@0: /* The ECC/TLS specification does not mention sl@0: * the use of DSA to sign ECParameters in the server sl@0: * key exchange message. We do support RSA and ECDSA. sl@0: */ sl@0: if (0) ; sl@0: #ifndef OPENSSL_NO_RSA sl@0: else if (alg & SSL_aRSA) sl@0: pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); sl@0: #endif sl@0: #ifndef OPENSSL_NO_ECDSA sl@0: else if (alg & SSL_aECDSA) sl@0: pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509); sl@0: #endif sl@0: /* else anonymous ECDH, so no certificate or pkey. */ sl@0: EC_KEY_set_public_key(ecdh, srvr_ecpoint); sl@0: s->session->sess_cert->peer_ecdh_tmp=ecdh; sl@0: ecdh=NULL; sl@0: BN_CTX_free(bn_ctx); sl@0: EC_POINT_free(srvr_ecpoint); sl@0: srvr_ecpoint = NULL; sl@0: } sl@0: else if (alg & SSL_kECDH) sl@0: { sl@0: al=SSL_AD_UNEXPECTED_MESSAGE; sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNEXPECTED_MESSAGE); sl@0: goto f_err; sl@0: } sl@0: #endif /* !OPENSSL_NO_ECDH */ sl@0: if (alg & SSL_aFZA) sl@0: { sl@0: al=SSL_AD_HANDSHAKE_FAILURE; sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER); sl@0: goto f_err; sl@0: } sl@0: sl@0: sl@0: /* p points to the next byte, there are 'n' bytes left */ sl@0: sl@0: /* if it was signed, check the signature */ sl@0: if (pkey != NULL) sl@0: { sl@0: n2s(p,i); sl@0: n-=2; sl@0: j=EVP_PKEY_size(pkey); sl@0: sl@0: if ((i != n) || (n > j) || (n <= 0)) sl@0: { sl@0: /* wrong packet length */ sl@0: al=SSL_AD_DECODE_ERROR; sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH); sl@0: goto f_err; sl@0: } sl@0: sl@0: #ifndef OPENSSL_NO_RSA sl@0: if (pkey->type == EVP_PKEY_RSA) sl@0: { sl@0: int num; sl@0: sl@0: j=0; sl@0: q=md_buf; sl@0: for (num=2; num > 0; num--) sl@0: { sl@0: EVP_DigestInit_ex(&md_ctx,(num == 2) sl@0: ?s->ctx->md5:s->ctx->sha1, NULL); sl@0: EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); sl@0: EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); sl@0: EVP_DigestUpdate(&md_ctx,param,param_len); sl@0: EVP_DigestFinal_ex(&md_ctx,q,(unsigned int *)&i); sl@0: q+=i; sl@0: j+=i; sl@0: } sl@0: i=RSA_verify(NID_md5_sha1, md_buf, j, p, n, sl@0: pkey->pkey.rsa); sl@0: if (i < 0) sl@0: { sl@0: al=SSL_AD_DECRYPT_ERROR; sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT); sl@0: goto f_err; sl@0: } sl@0: if (i == 0) sl@0: { sl@0: /* bad signature */ sl@0: al=SSL_AD_DECRYPT_ERROR; sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE); sl@0: goto f_err; sl@0: } sl@0: } sl@0: else sl@0: #endif sl@0: #ifndef OPENSSL_NO_DSA sl@0: if (pkey->type == EVP_PKEY_DSA) sl@0: { sl@0: /* lets do DSS */ sl@0: EVP_VerifyInit_ex(&md_ctx,EVP_dss1(), NULL); sl@0: EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); sl@0: EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); sl@0: EVP_VerifyUpdate(&md_ctx,param,param_len); sl@0: if (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey)) sl@0: { sl@0: /* bad signature */ sl@0: al=SSL_AD_DECRYPT_ERROR; sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE); sl@0: goto f_err; sl@0: } sl@0: } sl@0: else sl@0: #endif sl@0: #ifndef OPENSSL_NO_ECDSA sl@0: if (pkey->type == EVP_PKEY_EC) sl@0: { sl@0: /* let's do ECDSA */ sl@0: EVP_VerifyInit_ex(&md_ctx,EVP_ecdsa(), NULL); sl@0: EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); sl@0: EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); sl@0: EVP_VerifyUpdate(&md_ctx,param,param_len); sl@0: if (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey)) sl@0: { sl@0: /* bad signature */ sl@0: al=SSL_AD_DECRYPT_ERROR; sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE); sl@0: goto f_err; sl@0: } sl@0: } sl@0: else sl@0: #endif sl@0: { sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR); sl@0: goto err; sl@0: } sl@0: } sl@0: else sl@0: { sl@0: /* still data left over */ sl@0: if (!(alg & SSL_aNULL)) sl@0: { sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR); sl@0: goto err; sl@0: } sl@0: if (n != 0) sl@0: { sl@0: al=SSL_AD_DECODE_ERROR; sl@0: SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE); sl@0: goto f_err; sl@0: } sl@0: } sl@0: EVP_PKEY_free(pkey); sl@0: EVP_MD_CTX_cleanup(&md_ctx); sl@0: return(1); sl@0: f_err: sl@0: ssl3_send_alert(s,SSL3_AL_FATAL,al); sl@0: err: sl@0: EVP_PKEY_free(pkey); sl@0: #ifndef OPENSSL_NO_RSA sl@0: if (rsa != NULL) sl@0: RSA_free(rsa); sl@0: #endif sl@0: #ifndef OPENSSL_NO_DH sl@0: if (dh != NULL) sl@0: DH_free(dh); sl@0: #endif sl@0: #ifndef OPENSSL_NO_ECDH sl@0: BN_CTX_free(bn_ctx); sl@0: EC_POINT_free(srvr_ecpoint); sl@0: if (ecdh != NULL) sl@0: EC_KEY_free(ecdh); sl@0: #endif sl@0: EVP_MD_CTX_cleanup(&md_ctx); sl@0: return(-1); sl@0: } sl@0: sl@0: int ssl3_get_certificate_request(SSL *s) sl@0: { sl@0: int ok,ret=0; sl@0: unsigned long n,nc,l; sl@0: unsigned int llen,ctype_num,i; sl@0: X509_NAME *xn=NULL; sl@0: const unsigned char *p,*q; sl@0: unsigned char *d; sl@0: STACK_OF(X509_NAME) *ca_sk=NULL; sl@0: sl@0: n=s->method->ssl_get_message(s, sl@0: SSL3_ST_CR_CERT_REQ_A, sl@0: SSL3_ST_CR_CERT_REQ_B, sl@0: -1, sl@0: s->max_cert_list, sl@0: &ok); sl@0: sl@0: if (!ok) return((int)n); sl@0: sl@0: s->s3->tmp.cert_req=0; sl@0: sl@0: if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) sl@0: { sl@0: s->s3->tmp.reuse_message=1; sl@0: return(1); sl@0: } sl@0: sl@0: if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) sl@0: { sl@0: ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE); sl@0: SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE); sl@0: goto err; sl@0: } sl@0: sl@0: /* TLS does not like anon-DH with client cert */ sl@0: if (s->version > SSL3_VERSION) sl@0: { sl@0: l=s->s3->tmp.new_cipher->algorithms; sl@0: if (l & SSL_aNULL) sl@0: { sl@0: ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE); sl@0: SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER); sl@0: goto err; sl@0: } sl@0: } sl@0: sl@0: p=d=(unsigned char *)s->init_msg; sl@0: sl@0: if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: sl@0: /* get the certificate types */ sl@0: ctype_num= *(p++); sl@0: if (ctype_num > SSL3_CT_NUMBER) sl@0: ctype_num=SSL3_CT_NUMBER; sl@0: for (i=0; is3->tmp.ctype[i]= p[i]; sl@0: p+=ctype_num; sl@0: sl@0: /* get the CA RDNs */ sl@0: n2s(p,llen); sl@0: #if 0 sl@0: { sl@0: FILE *out; sl@0: out=fopen("/tmp/vsign.der","w"); sl@0: fwrite(p,1,llen,out); sl@0: fclose(out); sl@0: } sl@0: #endif sl@0: sl@0: if ((llen+ctype_num+2+1) != n) sl@0: { sl@0: ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); sl@0: SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH); sl@0: goto err; sl@0: } sl@0: sl@0: for (nc=0; nc llen) sl@0: { sl@0: if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) sl@0: goto cont; /* netscape bugs */ sl@0: ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); sl@0: SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG); sl@0: goto err; sl@0: } sl@0: sl@0: q=p; sl@0: sl@0: if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL) sl@0: { sl@0: /* If netscape tolerance is on, ignore errors */ sl@0: if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG) sl@0: goto cont; sl@0: else sl@0: { sl@0: ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); sl@0: SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB); sl@0: goto err; sl@0: } sl@0: } sl@0: sl@0: if (q != (p+l)) sl@0: { sl@0: ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); sl@0: SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH); sl@0: goto err; sl@0: } sl@0: if (!sk_X509_NAME_push(ca_sk,xn)) sl@0: { sl@0: SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: sl@0: p+=l; sl@0: nc+=l+2; sl@0: } sl@0: sl@0: if (0) sl@0: { sl@0: cont: sl@0: ERR_clear_error(); sl@0: } sl@0: sl@0: /* we should setup a certificate to return.... */ sl@0: s->s3->tmp.cert_req=1; sl@0: s->s3->tmp.ctype_num=ctype_num; sl@0: if (s->s3->tmp.ca_names != NULL) sl@0: sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free); sl@0: s->s3->tmp.ca_names=ca_sk; sl@0: ca_sk=NULL; sl@0: sl@0: ret=1; sl@0: err: sl@0: if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free); sl@0: return(ret); sl@0: } sl@0: sl@0: static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b) sl@0: { sl@0: return(X509_NAME_cmp(*a,*b)); sl@0: } sl@0: sl@0: int ssl3_get_server_done(SSL *s) sl@0: { sl@0: int ok,ret=0; sl@0: long n; sl@0: sl@0: n=s->method->ssl_get_message(s, sl@0: SSL3_ST_CR_SRVR_DONE_A, sl@0: SSL3_ST_CR_SRVR_DONE_B, sl@0: SSL3_MT_SERVER_DONE, sl@0: 30, /* should be very small, like 0 :-) */ sl@0: &ok); sl@0: sl@0: if (!ok) return((int)n); sl@0: if (n > 0) sl@0: { sl@0: /* should contain no data */ sl@0: ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); sl@0: SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH); sl@0: return -1; sl@0: } sl@0: ret=1; sl@0: return(ret); sl@0: } sl@0: sl@0: sl@0: int ssl3_send_client_key_exchange(SSL *s) sl@0: { sl@0: unsigned char *p,*d; sl@0: int n; sl@0: unsigned long l; sl@0: #ifndef OPENSSL_NO_RSA sl@0: unsigned char *q; sl@0: EVP_PKEY *pkey=NULL; sl@0: #endif sl@0: #ifndef OPENSSL_NO_KRB5 sl@0: KSSL_ERR kssl_err; sl@0: #endif /* OPENSSL_NO_KRB5 */ sl@0: #ifndef OPENSSL_NO_ECDH sl@0: EC_KEY *clnt_ecdh = NULL; sl@0: const EC_POINT *srvr_ecpoint = NULL; sl@0: EVP_PKEY *srvr_pub_pkey = NULL; sl@0: unsigned char *encodedPoint = NULL; sl@0: int encoded_pt_len = 0; sl@0: BN_CTX * bn_ctx = NULL; sl@0: #endif sl@0: sl@0: if (s->state == SSL3_ST_CW_KEY_EXCH_A) sl@0: { sl@0: d=(unsigned char *)s->init_buf->data; sl@0: p= &(d[4]); sl@0: sl@0: l=s->s3->tmp.new_cipher->algorithms; sl@0: sl@0: /* Fool emacs indentation */ sl@0: if (0) {} sl@0: #ifndef OPENSSL_NO_RSA sl@0: else if (l & SSL_kRSA) sl@0: { sl@0: RSA *rsa; sl@0: unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; sl@0: sl@0: if (s->session->sess_cert->peer_rsa_tmp != NULL) sl@0: rsa=s->session->sess_cert->peer_rsa_tmp; sl@0: else sl@0: { sl@0: pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); sl@0: if ((pkey == NULL) || sl@0: (pkey->type != EVP_PKEY_RSA) || sl@0: (pkey->pkey.rsa == NULL)) sl@0: { sl@0: SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR); sl@0: goto err; sl@0: } sl@0: rsa=pkey->pkey.rsa; sl@0: EVP_PKEY_free(pkey); sl@0: } sl@0: sl@0: tmp_buf[0]=s->client_version>>8; sl@0: tmp_buf[1]=s->client_version&0xff; sl@0: if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0) sl@0: goto err; sl@0: sl@0: s->session->master_key_length=sizeof tmp_buf; sl@0: sl@0: q=p; sl@0: /* Fix buf for TLS and beyond */ sl@0: if (s->version > SSL3_VERSION) sl@0: p+=2; sl@0: n=RSA_public_encrypt(sizeof tmp_buf, sl@0: tmp_buf,p,rsa,RSA_PKCS1_PADDING); sl@0: #ifdef PKCS1_CHECK sl@0: if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++; sl@0: if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70; sl@0: #endif sl@0: if (n <= 0) sl@0: { sl@0: SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT); sl@0: goto err; sl@0: } sl@0: sl@0: /* Fix buf for TLS and beyond */ sl@0: if (s->version > SSL3_VERSION) sl@0: { sl@0: s2n(n,q); sl@0: n+=2; sl@0: } sl@0: sl@0: s->session->master_key_length= sl@0: s->method->ssl3_enc->generate_master_secret(s, sl@0: s->session->master_key, sl@0: tmp_buf,sizeof tmp_buf); sl@0: OPENSSL_cleanse(tmp_buf,sizeof tmp_buf); sl@0: } sl@0: #endif sl@0: #ifndef OPENSSL_NO_KRB5 sl@0: else if (l & SSL_kKRB5) sl@0: { sl@0: krb5_error_code krb5rc; sl@0: KSSL_CTX *kssl_ctx = s->kssl_ctx; sl@0: /* krb5_data krb5_ap_req; */ sl@0: krb5_data *enc_ticket; sl@0: krb5_data authenticator, *authp = NULL; sl@0: EVP_CIPHER_CTX ciph_ctx; sl@0: EVP_CIPHER *enc = NULL; sl@0: unsigned char iv[EVP_MAX_IV_LENGTH]; sl@0: unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; sl@0: unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH sl@0: + EVP_MAX_IV_LENGTH]; sl@0: int padl, outl = sizeof(epms); sl@0: sl@0: EVP_CIPHER_CTX_init(&ciph_ctx); sl@0: sl@0: #ifdef KSSL_DEBUG sl@0: printf("ssl3_send_client_key_exchange(%lx & %lx)\n", sl@0: l, SSL_kKRB5); sl@0: #endif /* KSSL_DEBUG */ sl@0: sl@0: authp = NULL; sl@0: #ifdef KRB5SENDAUTH sl@0: if (KRB5SENDAUTH) authp = &authenticator; sl@0: #endif /* KRB5SENDAUTH */ sl@0: sl@0: krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp, sl@0: &kssl_err); sl@0: enc = kssl_map_enc(kssl_ctx->enctype); sl@0: if (enc == NULL) sl@0: goto err; sl@0: #ifdef KSSL_DEBUG sl@0: { sl@0: printf("kssl_cget_tkt rtn %d\n", krb5rc); sl@0: if (krb5rc && kssl_err.text) sl@0: printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text); sl@0: } sl@0: #endif /* KSSL_DEBUG */ sl@0: sl@0: if (krb5rc) sl@0: { sl@0: ssl3_send_alert(s,SSL3_AL_FATAL, sl@0: SSL_AD_HANDSHAKE_FAILURE); sl@0: SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, sl@0: kssl_err.reason); sl@0: goto err; sl@0: } sl@0: sl@0: /* 20010406 VRS - Earlier versions used KRB5 AP_REQ sl@0: ** in place of RFC 2712 KerberosWrapper, as in: sl@0: ** sl@0: ** Send ticket (copy to *p, set n = length) sl@0: ** n = krb5_ap_req.length; sl@0: ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length); sl@0: ** if (krb5_ap_req.data) sl@0: ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req); sl@0: ** sl@0: ** Now using real RFC 2712 KerberosWrapper sl@0: ** (Thanks to Simon Wilkinson ) sl@0: ** Note: 2712 "opaque" types are here replaced sl@0: ** with a 2-byte length followed by the value. sl@0: ** Example: sl@0: ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms sl@0: ** Where "xx xx" = length bytes. Shown here with sl@0: ** optional authenticator omitted. sl@0: */ sl@0: sl@0: /* KerberosWrapper.Ticket */ sl@0: s2n(enc_ticket->length,p); sl@0: memcpy(p, enc_ticket->data, enc_ticket->length); sl@0: p+= enc_ticket->length; sl@0: n = enc_ticket->length + 2; sl@0: sl@0: /* KerberosWrapper.Authenticator */ sl@0: if (authp && authp->length) sl@0: { sl@0: s2n(authp->length,p); sl@0: memcpy(p, authp->data, authp->length); sl@0: p+= authp->length; sl@0: n+= authp->length + 2; sl@0: sl@0: free(authp->data); sl@0: authp->data = NULL; sl@0: authp->length = 0; sl@0: } sl@0: else sl@0: { sl@0: s2n(0,p);/* null authenticator length */ sl@0: n+=2; sl@0: } sl@0: sl@0: tmp_buf[0]=s->client_version>>8; sl@0: tmp_buf[1]=s->client_version&0xff; sl@0: if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0) sl@0: goto err; sl@0: sl@0: /* 20010420 VRS. Tried it this way; failed. sl@0: ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL); sl@0: ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx, sl@0: ** kssl_ctx->length); sl@0: ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv); sl@0: */ sl@0: sl@0: memset(iv, 0, sizeof iv); /* per RFC 1510 */ sl@0: EVP_EncryptInit_ex(&ciph_ctx,enc, NULL, sl@0: kssl_ctx->key,iv); sl@0: EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf, sl@0: sizeof tmp_buf); sl@0: EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl); sl@0: outl += padl; sl@0: if (outl > sizeof epms) sl@0: { sl@0: SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); sl@0: goto err; sl@0: } sl@0: EVP_CIPHER_CTX_cleanup(&ciph_ctx); sl@0: sl@0: /* KerberosWrapper.EncryptedPreMasterSecret */ sl@0: s2n(outl,p); sl@0: memcpy(p, epms, outl); sl@0: p+=outl; sl@0: n+=outl + 2; sl@0: sl@0: s->session->master_key_length= sl@0: s->method->ssl3_enc->generate_master_secret(s, sl@0: s->session->master_key, sl@0: tmp_buf, sizeof tmp_buf); sl@0: sl@0: OPENSSL_cleanse(tmp_buf, sizeof tmp_buf); sl@0: OPENSSL_cleanse(epms, outl); sl@0: } sl@0: #endif sl@0: #ifndef OPENSSL_NO_DH sl@0: else if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) sl@0: { sl@0: DH *dh_srvr,*dh_clnt; sl@0: sl@0: if (s->session->sess_cert->peer_dh_tmp != NULL) sl@0: dh_srvr=s->session->sess_cert->peer_dh_tmp; sl@0: else sl@0: { sl@0: /* we get them from the cert */ sl@0: ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); sl@0: SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS); sl@0: goto err; sl@0: } sl@0: sl@0: /* generate a new random key */ sl@0: if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); sl@0: goto err; sl@0: } sl@0: if (!DH_generate_key(dh_clnt)) sl@0: { sl@0: SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); sl@0: goto err; sl@0: } sl@0: sl@0: /* use the 'p' output buffer for the DH key, but sl@0: * make sure to clear it out afterwards */ sl@0: sl@0: n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt); sl@0: sl@0: if (n <= 0) sl@0: { sl@0: SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); sl@0: goto err; sl@0: } sl@0: sl@0: /* generate master key from the result */ sl@0: s->session->master_key_length= sl@0: s->method->ssl3_enc->generate_master_secret(s, sl@0: s->session->master_key,p,n); sl@0: /* clean up */ sl@0: memset(p,0,n); sl@0: sl@0: /* send off the data */ sl@0: n=BN_num_bytes(dh_clnt->pub_key); sl@0: s2n(n,p); sl@0: BN_bn2bin(dh_clnt->pub_key,p); sl@0: n+=2; sl@0: sl@0: DH_free(dh_clnt); sl@0: sl@0: /* perhaps clean things up a bit EAY EAY EAY EAY*/ sl@0: } sl@0: #endif sl@0: sl@0: #ifndef OPENSSL_NO_ECDH sl@0: else if ((l & SSL_kECDH) || (l & SSL_kECDHE)) sl@0: { sl@0: const EC_GROUP *srvr_group = NULL; sl@0: EC_KEY *tkey; sl@0: int ecdh_clnt_cert = 0; sl@0: int field_size = 0; sl@0: sl@0: /* Did we send out the client's sl@0: * ECDH share for use in premaster sl@0: * computation as part of client certificate? sl@0: * If so, set ecdh_clnt_cert to 1. sl@0: */ sl@0: if ((l & SSL_kECDH) && (s->cert != NULL)) sl@0: { sl@0: /* XXX: For now, we do not support client sl@0: * authentication using ECDH certificates. sl@0: * To add such support, one needs to add sl@0: * code that checks for appropriate sl@0: * conditions and sets ecdh_clnt_cert to 1. sl@0: * For example, the cert have an ECC sl@0: * key on the same curve as the server's sl@0: * and the key should be authorized for sl@0: * key agreement. sl@0: * sl@0: * One also needs to add code in ssl3_connect sl@0: * to skip sending the certificate verify sl@0: * message. sl@0: * sl@0: * if ((s->cert->key->privatekey != NULL) && sl@0: * (s->cert->key->privatekey->type == sl@0: * EVP_PKEY_EC) && ...) sl@0: * ecdh_clnt_cert = 1; sl@0: */ sl@0: } sl@0: sl@0: if (s->session->sess_cert->peer_ecdh_tmp != NULL) sl@0: { sl@0: tkey = s->session->sess_cert->peer_ecdh_tmp; sl@0: } sl@0: else sl@0: { sl@0: /* Get the Server Public Key from Cert */ sl@0: srvr_pub_pkey = X509_get_pubkey(s->session-> \ sl@0: sess_cert->peer_pkeys[SSL_PKEY_ECC].x509); sl@0: if ((srvr_pub_pkey == NULL) || sl@0: (srvr_pub_pkey->type != EVP_PKEY_EC) || sl@0: (srvr_pub_pkey->pkey.ec == NULL)) sl@0: { sl@0: SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, sl@0: ERR_R_INTERNAL_ERROR); sl@0: goto err; sl@0: } sl@0: sl@0: tkey = srvr_pub_pkey->pkey.ec; sl@0: } sl@0: sl@0: srvr_group = EC_KEY_get0_group(tkey); sl@0: srvr_ecpoint = EC_KEY_get0_public_key(tkey); sl@0: sl@0: if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) sl@0: { sl@0: SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, sl@0: ERR_R_INTERNAL_ERROR); sl@0: goto err; sl@0: } sl@0: sl@0: if ((clnt_ecdh=EC_KEY_new()) == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: sl@0: if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) sl@0: { sl@0: SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB); sl@0: goto err; sl@0: } sl@0: if (ecdh_clnt_cert) sl@0: { sl@0: /* Reuse key info from our certificate sl@0: * We only need our private key to perform sl@0: * the ECDH computation. sl@0: */ sl@0: const BIGNUM *priv_key; sl@0: tkey = s->cert->key->privatekey->pkey.ec; sl@0: priv_key = EC_KEY_get0_private_key(tkey); sl@0: if (priv_key == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) sl@0: { sl@0: SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB); sl@0: goto err; sl@0: } sl@0: } sl@0: else sl@0: { sl@0: /* Generate a new ECDH key pair */ sl@0: if (!(EC_KEY_generate_key(clnt_ecdh))) sl@0: { sl@0: SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB); sl@0: goto err; sl@0: } sl@0: } sl@0: sl@0: /* use the 'p' output buffer for the ECDH key, but sl@0: * make sure to clear it out afterwards sl@0: */ sl@0: sl@0: field_size = EC_GROUP_get_degree(srvr_group); sl@0: if (field_size <= 0) sl@0: { sl@0: SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, sl@0: ERR_R_ECDH_LIB); sl@0: goto err; sl@0: } sl@0: n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL); sl@0: if (n <= 0) sl@0: { sl@0: SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, sl@0: ERR_R_ECDH_LIB); sl@0: goto err; sl@0: } sl@0: sl@0: /* generate master key from the result */ sl@0: s->session->master_key_length = s->method->ssl3_enc \ sl@0: -> generate_master_secret(s, sl@0: s->session->master_key, sl@0: p, n); sl@0: sl@0: memset(p, 0, n); /* clean up */ sl@0: sl@0: if (ecdh_clnt_cert) sl@0: { sl@0: /* Send empty client key exch message */ sl@0: n = 0; sl@0: } sl@0: else sl@0: { sl@0: /* First check the size of encoding and sl@0: * allocate memory accordingly. sl@0: */ sl@0: encoded_pt_len = sl@0: EC_POINT_point2oct(srvr_group, sl@0: EC_KEY_get0_public_key(clnt_ecdh), sl@0: POINT_CONVERSION_UNCOMPRESSED, sl@0: NULL, 0, NULL); sl@0: sl@0: encodedPoint = (unsigned char *) sl@0: OPENSSL_malloc(encoded_pt_len * sl@0: sizeof(unsigned char)); sl@0: bn_ctx = BN_CTX_new(); sl@0: if ((encodedPoint == NULL) || sl@0: (bn_ctx == NULL)) sl@0: { sl@0: SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: sl@0: /* Encode the public key */ sl@0: n = EC_POINT_point2oct(srvr_group, sl@0: EC_KEY_get0_public_key(clnt_ecdh), sl@0: POINT_CONVERSION_UNCOMPRESSED, sl@0: encodedPoint, encoded_pt_len, bn_ctx); sl@0: sl@0: *p = n; /* length of encoded point */ sl@0: /* Encoded point will be copied here */ sl@0: p += 1; sl@0: /* copy the point */ sl@0: memcpy((unsigned char *)p, encodedPoint, n); sl@0: /* increment n to account for length field */ sl@0: n += 1; sl@0: } sl@0: sl@0: /* Free allocated memory */ sl@0: BN_CTX_free(bn_ctx); sl@0: if (encodedPoint != NULL) OPENSSL_free(encodedPoint); sl@0: if (clnt_ecdh != NULL) sl@0: EC_KEY_free(clnt_ecdh); sl@0: EVP_PKEY_free(srvr_pub_pkey); sl@0: } sl@0: #endif /* !OPENSSL_NO_ECDH */ sl@0: else sl@0: { sl@0: ssl3_send_alert(s, SSL3_AL_FATAL, sl@0: SSL_AD_HANDSHAKE_FAILURE); sl@0: SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, sl@0: ERR_R_INTERNAL_ERROR); sl@0: goto err; sl@0: } sl@0: sl@0: *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE; sl@0: l2n3(n,d); sl@0: sl@0: s->state=SSL3_ST_CW_KEY_EXCH_B; sl@0: /* number of bytes to write */ sl@0: s->init_num=n+4; sl@0: s->init_off=0; sl@0: } sl@0: sl@0: /* SSL3_ST_CW_KEY_EXCH_B */ sl@0: return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); sl@0: err: sl@0: #ifndef OPENSSL_NO_ECDH sl@0: BN_CTX_free(bn_ctx); sl@0: if (encodedPoint != NULL) OPENSSL_free(encodedPoint); sl@0: if (clnt_ecdh != NULL) sl@0: EC_KEY_free(clnt_ecdh); sl@0: EVP_PKEY_free(srvr_pub_pkey); sl@0: #endif sl@0: return(-1); sl@0: } sl@0: sl@0: int ssl3_send_client_verify(SSL *s) sl@0: { sl@0: unsigned char *p,*d; sl@0: unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH]; sl@0: EVP_PKEY *pkey; sl@0: #ifndef OPENSSL_NO_RSA sl@0: unsigned u=0; sl@0: #endif sl@0: unsigned long n; sl@0: #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA) sl@0: int j; sl@0: #endif sl@0: sl@0: if (s->state == SSL3_ST_CW_CERT_VRFY_A) sl@0: { sl@0: d=(unsigned char *)s->init_buf->data; sl@0: p= &(d[4]); sl@0: pkey=s->cert->key->privatekey; sl@0: sl@0: s->method->ssl3_enc->cert_verify_mac(s,&(s->s3->finish_dgst2), sl@0: &(data[MD5_DIGEST_LENGTH])); sl@0: sl@0: #ifndef OPENSSL_NO_RSA sl@0: if (pkey->type == EVP_PKEY_RSA) sl@0: { sl@0: s->method->ssl3_enc->cert_verify_mac(s, sl@0: &(s->s3->finish_dgst1),&(data[0])); sl@0: if (RSA_sign(NID_md5_sha1, data, sl@0: MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, sl@0: &(p[2]), &u, pkey->pkey.rsa) <= 0 ) sl@0: { sl@0: SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB); sl@0: goto err; sl@0: } sl@0: s2n(u,p); sl@0: n=u+2; sl@0: } sl@0: else sl@0: #endif sl@0: #ifndef OPENSSL_NO_DSA sl@0: if (pkey->type == EVP_PKEY_DSA) sl@0: { sl@0: if (!DSA_sign(pkey->save_type, sl@0: &(data[MD5_DIGEST_LENGTH]), sl@0: SHA_DIGEST_LENGTH,&(p[2]), sl@0: (unsigned int *)&j,pkey->pkey.dsa)) sl@0: { sl@0: SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB); sl@0: goto err; sl@0: } sl@0: s2n(j,p); sl@0: n=j+2; sl@0: } sl@0: else sl@0: #endif sl@0: #ifndef OPENSSL_NO_ECDSA sl@0: if (pkey->type == EVP_PKEY_EC) sl@0: { sl@0: if (!ECDSA_sign(pkey->save_type, sl@0: &(data[MD5_DIGEST_LENGTH]), sl@0: SHA_DIGEST_LENGTH,&(p[2]), sl@0: (unsigned int *)&j,pkey->pkey.ec)) sl@0: { sl@0: SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, sl@0: ERR_R_ECDSA_LIB); sl@0: goto err; sl@0: } sl@0: s2n(j,p); sl@0: n=j+2; sl@0: } sl@0: else sl@0: #endif sl@0: { sl@0: SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR); sl@0: goto err; sl@0: } sl@0: *(d++)=SSL3_MT_CERTIFICATE_VERIFY; sl@0: l2n3(n,d); sl@0: sl@0: s->state=SSL3_ST_CW_CERT_VRFY_B; sl@0: s->init_num=(int)n+4; sl@0: s->init_off=0; sl@0: } sl@0: return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); sl@0: err: sl@0: return(-1); sl@0: } sl@0: sl@0: int ssl3_send_client_certificate(SSL *s) sl@0: { sl@0: X509 *x509=NULL; sl@0: EVP_PKEY *pkey=NULL; sl@0: int i; sl@0: unsigned long l; sl@0: sl@0: if (s->state == SSL3_ST_CW_CERT_A) sl@0: { sl@0: if ((s->cert == NULL) || sl@0: (s->cert->key->x509 == NULL) || sl@0: (s->cert->key->privatekey == NULL)) sl@0: s->state=SSL3_ST_CW_CERT_B; sl@0: else sl@0: s->state=SSL3_ST_CW_CERT_C; sl@0: } sl@0: sl@0: /* We need to get a client cert */ sl@0: if (s->state == SSL3_ST_CW_CERT_B) sl@0: { sl@0: /* If we get an error, we need to sl@0: * ssl->rwstate=SSL_X509_LOOKUP; return(-1); sl@0: * We then get retied later */ sl@0: i=0; sl@0: if (s->ctx->client_cert_cb != NULL) sl@0: i=s->ctx->client_cert_cb(s,&(x509),&(pkey)); 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: if ((i == 1) && (pkey != NULL) && (x509 != NULL)) sl@0: { sl@0: s->state=SSL3_ST_CW_CERT_B; sl@0: if ( !SSL_use_certificate(s,x509) || sl@0: !SSL_use_PrivateKey(s,pkey)) sl@0: i=0; sl@0: } sl@0: else if (i == 1) sl@0: { sl@0: i=0; sl@0: SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK); sl@0: } sl@0: sl@0: if (x509 != NULL) X509_free(x509); sl@0: if (pkey != NULL) EVP_PKEY_free(pkey); sl@0: if (i == 0) sl@0: { sl@0: if (s->version == SSL3_VERSION) sl@0: { sl@0: s->s3->tmp.cert_req=0; sl@0: ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE); sl@0: return(1); sl@0: } sl@0: else sl@0: { sl@0: s->s3->tmp.cert_req=2; sl@0: } sl@0: } sl@0: sl@0: /* Ok, we have a cert */ sl@0: s->state=SSL3_ST_CW_CERT_C; sl@0: } sl@0: sl@0: if (s->state == SSL3_ST_CW_CERT_C) sl@0: { sl@0: s->state=SSL3_ST_CW_CERT_D; sl@0: l=ssl3_output_cert_chain(s, sl@0: (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509); sl@0: s->init_num=(int)l; sl@0: s->init_off=0; sl@0: } sl@0: /* SSL3_ST_CW_CERT_D */ sl@0: return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); sl@0: } sl@0: sl@0: #define has_bits(i,m) (((i)&(m)) == (m)) sl@0: sl@0: int ssl3_check_cert_and_algorithm(SSL *s) sl@0: { sl@0: int i,idx; sl@0: long algs; sl@0: EVP_PKEY *pkey=NULL; sl@0: SESS_CERT *sc; sl@0: #ifndef OPENSSL_NO_RSA sl@0: RSA *rsa; sl@0: #endif sl@0: #ifndef OPENSSL_NO_DH sl@0: DH *dh; sl@0: #endif sl@0: sl@0: sc=s->session->sess_cert; sl@0: sl@0: algs=s->s3->tmp.new_cipher->algorithms; sl@0: sl@0: /* we don't have a certificate */ sl@0: if (algs & (SSL_aDH|SSL_aNULL|SSL_aKRB5)) sl@0: return(1); sl@0: sl@0: if (sc == NULL) sl@0: { sl@0: SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,ERR_R_INTERNAL_ERROR); sl@0: goto err; sl@0: } sl@0: sl@0: #ifndef OPENSSL_NO_RSA sl@0: rsa=s->session->sess_cert->peer_rsa_tmp; sl@0: #endif sl@0: #ifndef OPENSSL_NO_DH sl@0: dh=s->session->sess_cert->peer_dh_tmp; sl@0: #endif sl@0: sl@0: /* This is the passed certificate */ sl@0: sl@0: idx=sc->peer_cert_type; sl@0: #ifndef OPENSSL_NO_ECDH sl@0: if (idx == SSL_PKEY_ECC) sl@0: { sl@0: if (check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, sl@0: s->s3->tmp.new_cipher) == 0) sl@0: { /* check failed */ sl@0: SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_BAD_ECC_CERT); sl@0: goto f_err; sl@0: } sl@0: else sl@0: { sl@0: return 1; sl@0: } sl@0: } sl@0: #endif sl@0: pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509); sl@0: i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey); sl@0: EVP_PKEY_free(pkey); sl@0: sl@0: sl@0: /* Check that we have a certificate if we require one */ sl@0: if ((algs & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN)) sl@0: { sl@0: SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT); sl@0: goto f_err; sl@0: } sl@0: #ifndef OPENSSL_NO_DSA sl@0: else if ((algs & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN)) sl@0: { sl@0: SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT); sl@0: goto f_err; sl@0: } sl@0: #endif sl@0: #ifndef OPENSSL_NO_RSA sl@0: if ((algs & SSL_kRSA) && sl@0: !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL))) sl@0: { sl@0: SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT); sl@0: goto f_err; sl@0: } sl@0: #endif sl@0: #ifndef OPENSSL_NO_DH sl@0: if ((algs & SSL_kEDH) && sl@0: !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL))) sl@0: { sl@0: SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY); sl@0: goto f_err; sl@0: } sl@0: else if ((algs & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA)) sl@0: { sl@0: SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT); sl@0: goto f_err; sl@0: } sl@0: #ifndef OPENSSL_NO_DSA sl@0: else if ((algs & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA)) sl@0: { sl@0: SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT); sl@0: goto f_err; sl@0: } sl@0: #endif sl@0: #endif sl@0: sl@0: if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP)) sl@0: { sl@0: #ifndef OPENSSL_NO_RSA sl@0: if (algs & SSL_kRSA) sl@0: { sl@0: if (rsa == NULL sl@0: || RSA_size(rsa)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) sl@0: { sl@0: SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY); sl@0: goto f_err; sl@0: } sl@0: } sl@0: else sl@0: #endif sl@0: #ifndef OPENSSL_NO_DH sl@0: if (algs & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) sl@0: { sl@0: if (dh == NULL sl@0: || DH_size(dh)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) sl@0: { sl@0: SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY); sl@0: goto f_err; sl@0: } sl@0: } sl@0: else sl@0: #endif sl@0: { sl@0: SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); sl@0: goto f_err; sl@0: } sl@0: } sl@0: return(1); sl@0: f_err: sl@0: ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); sl@0: err: sl@0: return(0); sl@0: } sl@0: sl@0: sl@0: #ifndef OPENSSL_NO_ECDH sl@0: /* This is the complement of nid2curve_id in s3_srvr.c. */ sl@0: static int curve_id2nid(int curve_id) sl@0: { sl@0: /* ECC curves from draft-ietf-tls-ecc-01.txt (Mar 15, 2001) sl@0: * (no changes in draft-ietf-tls-ecc-03.txt [June 2003]) */ sl@0: static int nid_list[26] = sl@0: { sl@0: 0, sl@0: NID_sect163k1, /* sect163k1 (1) */ sl@0: NID_sect163r1, /* sect163r1 (2) */ sl@0: NID_sect163r2, /* sect163r2 (3) */ sl@0: NID_sect193r1, /* sect193r1 (4) */ sl@0: NID_sect193r2, /* sect193r2 (5) */ sl@0: NID_sect233k1, /* sect233k1 (6) */ sl@0: NID_sect233r1, /* sect233r1 (7) */ sl@0: NID_sect239k1, /* sect239k1 (8) */ sl@0: NID_sect283k1, /* sect283k1 (9) */ sl@0: NID_sect283r1, /* sect283r1 (10) */ sl@0: NID_sect409k1, /* sect409k1 (11) */ sl@0: NID_sect409r1, /* sect409r1 (12) */ sl@0: NID_sect571k1, /* sect571k1 (13) */ sl@0: NID_sect571r1, /* sect571r1 (14) */ sl@0: NID_secp160k1, /* secp160k1 (15) */ sl@0: NID_secp160r1, /* secp160r1 (16) */ sl@0: NID_secp160r2, /* secp160r2 (17) */ sl@0: NID_secp192k1, /* secp192k1 (18) */ sl@0: NID_X9_62_prime192v1, /* secp192r1 (19) */ sl@0: NID_secp224k1, /* secp224k1 (20) */ sl@0: NID_secp224r1, /* secp224r1 (21) */ sl@0: NID_secp256k1, /* secp256k1 (22) */ sl@0: NID_X9_62_prime256v1, /* secp256r1 (23) */ sl@0: NID_secp384r1, /* secp384r1 (24) */ sl@0: NID_secp521r1 /* secp521r1 (25) */ sl@0: }; sl@0: sl@0: if ((curve_id < 1) || (curve_id > 25)) return 0; sl@0: sl@0: return nid_list[curve_id]; sl@0: } sl@0: #endif sl@0: sl@0: