10Sstevel@tonic-gate /* ssl/s3_srvr.c */ 20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 30Sstevel@tonic-gate * All rights reserved. 40Sstevel@tonic-gate * 50Sstevel@tonic-gate * This package is an SSL implementation written 60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com). 70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as 100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions 110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA, 120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation 130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms 140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com). 150Sstevel@tonic-gate * 160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in 170Sstevel@tonic-gate * the code are not to be removed. 180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution 190Sstevel@tonic-gate * as the author of the parts of the library used. 200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or 210Sstevel@tonic-gate * in documentation (online or textual) provided with the package. 220Sstevel@tonic-gate * 230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 240Sstevel@tonic-gate * modification, are permitted provided that the following conditions 250Sstevel@tonic-gate * are met: 260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright 270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 320Sstevel@tonic-gate * must display the following acknowledgement: 330Sstevel@tonic-gate * "This product includes cryptographic software written by 340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)" 350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library 360Sstevel@tonic-gate * being used are not cryptographic related :-). 370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from 380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement: 390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 400Sstevel@tonic-gate * 410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 510Sstevel@tonic-gate * SUCH DAMAGE. 520Sstevel@tonic-gate * 530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or 540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be 550Sstevel@tonic-gate * copied and put under another distribution licence 560Sstevel@tonic-gate * [including the GNU Public Licence.] 570Sstevel@tonic-gate */ 580Sstevel@tonic-gate /* ==================================================================== 592139Sjp161948 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. 600Sstevel@tonic-gate * 610Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 620Sstevel@tonic-gate * modification, are permitted provided that the following conditions 630Sstevel@tonic-gate * are met: 640Sstevel@tonic-gate * 650Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 660Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 670Sstevel@tonic-gate * 680Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 690Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in 700Sstevel@tonic-gate * the documentation and/or other materials provided with the 710Sstevel@tonic-gate * distribution. 720Sstevel@tonic-gate * 730Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this 740Sstevel@tonic-gate * software must display the following acknowledgment: 750Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project 760Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 770Sstevel@tonic-gate * 780Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 790Sstevel@tonic-gate * endorse or promote products derived from this software without 800Sstevel@tonic-gate * prior written permission. For written permission, please contact 810Sstevel@tonic-gate * openssl-core@openssl.org. 820Sstevel@tonic-gate * 830Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL" 840Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written 850Sstevel@tonic-gate * permission of the OpenSSL Project. 860Sstevel@tonic-gate * 870Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following 880Sstevel@tonic-gate * acknowledgment: 890Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project 900Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 910Sstevel@tonic-gate * 920Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 930Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 940Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 950Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 960Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 970Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 980Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 990Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 1000Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 1010Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1020Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 1030Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE. 1040Sstevel@tonic-gate * ==================================================================== 1050Sstevel@tonic-gate * 1060Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young 1070Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim 1080Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com). 1090Sstevel@tonic-gate * 1100Sstevel@tonic-gate */ 1112139Sjp161948 /* ==================================================================== 1122139Sjp161948 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 1132139Sjp161948 * 1142139Sjp161948 * Portions of the attached software ("Contribution") are developed by 1152139Sjp161948 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. 1162139Sjp161948 * 1172139Sjp161948 * The Contribution is licensed pursuant to the OpenSSL open source 1182139Sjp161948 * license provided above. 1192139Sjp161948 * 1202139Sjp161948 * ECC cipher suite support in OpenSSL originally written by 1212139Sjp161948 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories. 1222139Sjp161948 * 1232139Sjp161948 */ 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate #define REUSE_CIPHER_BUG 1260Sstevel@tonic-gate #define NETSCAPE_HANG_BUG 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate #include <stdio.h> 1290Sstevel@tonic-gate #include "ssl_locl.h" 1300Sstevel@tonic-gate #include "kssl_lcl.h" 1310Sstevel@tonic-gate #include <openssl/buffer.h> 1320Sstevel@tonic-gate #include <openssl/rand.h> 1330Sstevel@tonic-gate #include <openssl/objects.h> 1340Sstevel@tonic-gate #include <openssl/evp.h> 1350Sstevel@tonic-gate #include <openssl/x509.h> 1362139Sjp161948 #ifndef OPENSSL_NO_DH 1372139Sjp161948 #include <openssl/dh.h> 1382139Sjp161948 #endif 1392139Sjp161948 #include <openssl/bn.h> 1400Sstevel@tonic-gate #ifndef OPENSSL_NO_KRB5 1410Sstevel@tonic-gate #include <openssl/krb5_asn.h> 1420Sstevel@tonic-gate #endif 1430Sstevel@tonic-gate #include <openssl/md5.h> 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate static SSL_METHOD *ssl3_get_server_method(int ver); 1462139Sjp161948 1472139Sjp161948 #ifndef OPENSSL_NO_ECDH 1482139Sjp161948 static int nid2curve_id(int nid); 1492139Sjp161948 #endif 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate static SSL_METHOD *ssl3_get_server_method(int ver) 1520Sstevel@tonic-gate { 1530Sstevel@tonic-gate if (ver == SSL3_VERSION) 1540Sstevel@tonic-gate return(SSLv3_server_method()); 1550Sstevel@tonic-gate else 1560Sstevel@tonic-gate return(NULL); 1570Sstevel@tonic-gate } 1580Sstevel@tonic-gate 1592139Sjp161948 IMPLEMENT_ssl3_meth_func(SSLv3_server_method, 1602139Sjp161948 ssl3_accept, 1612139Sjp161948 ssl_undefined_function, 1622139Sjp161948 ssl3_get_server_method) 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate int ssl3_accept(SSL *s) 1650Sstevel@tonic-gate { 1660Sstevel@tonic-gate BUF_MEM *buf; 1670Sstevel@tonic-gate unsigned long l,Time=time(NULL); 1680Sstevel@tonic-gate void (*cb)(const SSL *ssl,int type,int val)=NULL; 1690Sstevel@tonic-gate long num1; 1700Sstevel@tonic-gate int ret= -1; 1710Sstevel@tonic-gate int new_state,state,skip=0; 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate RAND_add(&Time,sizeof(Time),0); 1740Sstevel@tonic-gate ERR_clear_error(); 1750Sstevel@tonic-gate clear_sys_error(); 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate if (s->info_callback != NULL) 1780Sstevel@tonic-gate cb=s->info_callback; 1790Sstevel@tonic-gate else if (s->ctx->info_callback != NULL) 1800Sstevel@tonic-gate cb=s->ctx->info_callback; 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate /* init things to blank */ 1830Sstevel@tonic-gate s->in_handshake++; 1840Sstevel@tonic-gate if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate if (s->cert == NULL) 1870Sstevel@tonic-gate { 1880Sstevel@tonic-gate SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET); 1890Sstevel@tonic-gate return(-1); 1900Sstevel@tonic-gate } 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate for (;;) 1930Sstevel@tonic-gate { 1940Sstevel@tonic-gate state=s->state; 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate switch (s->state) 1970Sstevel@tonic-gate { 1980Sstevel@tonic-gate case SSL_ST_RENEGOTIATE: 1990Sstevel@tonic-gate s->new_session=1; 2000Sstevel@tonic-gate /* s->state=SSL_ST_ACCEPT; */ 2010Sstevel@tonic-gate 2020Sstevel@tonic-gate case SSL_ST_BEFORE: 2030Sstevel@tonic-gate case SSL_ST_ACCEPT: 2040Sstevel@tonic-gate case SSL_ST_BEFORE|SSL_ST_ACCEPT: 2050Sstevel@tonic-gate case SSL_ST_OK|SSL_ST_ACCEPT: 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate s->server=1; 2080Sstevel@tonic-gate if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate if ((s->version>>8) != 3) 2110Sstevel@tonic-gate { 2120Sstevel@tonic-gate SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR); 2130Sstevel@tonic-gate return -1; 2140Sstevel@tonic-gate } 2150Sstevel@tonic-gate s->type=SSL_ST_ACCEPT; 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate if (s->init_buf == NULL) 2180Sstevel@tonic-gate { 2190Sstevel@tonic-gate if ((buf=BUF_MEM_new()) == NULL) 2200Sstevel@tonic-gate { 2210Sstevel@tonic-gate ret= -1; 2220Sstevel@tonic-gate goto end; 2230Sstevel@tonic-gate } 2240Sstevel@tonic-gate if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH)) 2250Sstevel@tonic-gate { 2260Sstevel@tonic-gate ret= -1; 2270Sstevel@tonic-gate goto end; 2280Sstevel@tonic-gate } 2290Sstevel@tonic-gate s->init_buf=buf; 2300Sstevel@tonic-gate } 2310Sstevel@tonic-gate 2320Sstevel@tonic-gate if (!ssl3_setup_buffers(s)) 2330Sstevel@tonic-gate { 2340Sstevel@tonic-gate ret= -1; 2350Sstevel@tonic-gate goto end; 2360Sstevel@tonic-gate } 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate s->init_num=0; 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate if (s->state != SSL_ST_RENEGOTIATE) 2410Sstevel@tonic-gate { 2420Sstevel@tonic-gate /* Ok, we now need to push on a buffering BIO so that 2430Sstevel@tonic-gate * the output is sent in a way that TCP likes :-) 2440Sstevel@tonic-gate */ 2450Sstevel@tonic-gate if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; } 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate ssl3_init_finished_mac(s); 2480Sstevel@tonic-gate s->state=SSL3_ST_SR_CLNT_HELLO_A; 2490Sstevel@tonic-gate s->ctx->stats.sess_accept++; 2500Sstevel@tonic-gate } 2510Sstevel@tonic-gate else 2520Sstevel@tonic-gate { 2530Sstevel@tonic-gate /* s->state == SSL_ST_RENEGOTIATE, 2540Sstevel@tonic-gate * we will just send a HelloRequest */ 2550Sstevel@tonic-gate s->ctx->stats.sess_accept_renegotiate++; 2560Sstevel@tonic-gate s->state=SSL3_ST_SW_HELLO_REQ_A; 2570Sstevel@tonic-gate } 2580Sstevel@tonic-gate break; 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate case SSL3_ST_SW_HELLO_REQ_A: 2610Sstevel@tonic-gate case SSL3_ST_SW_HELLO_REQ_B: 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate s->shutdown=0; 2640Sstevel@tonic-gate ret=ssl3_send_hello_request(s); 2650Sstevel@tonic-gate if (ret <= 0) goto end; 2660Sstevel@tonic-gate s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C; 2670Sstevel@tonic-gate s->state=SSL3_ST_SW_FLUSH; 2680Sstevel@tonic-gate s->init_num=0; 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate ssl3_init_finished_mac(s); 2710Sstevel@tonic-gate break; 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate case SSL3_ST_SW_HELLO_REQ_C: 2740Sstevel@tonic-gate s->state=SSL_ST_OK; 2750Sstevel@tonic-gate break; 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate case SSL3_ST_SR_CLNT_HELLO_A: 2780Sstevel@tonic-gate case SSL3_ST_SR_CLNT_HELLO_B: 2790Sstevel@tonic-gate case SSL3_ST_SR_CLNT_HELLO_C: 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate s->shutdown=0; 2820Sstevel@tonic-gate ret=ssl3_get_client_hello(s); 2830Sstevel@tonic-gate if (ret <= 0) goto end; 2840Sstevel@tonic-gate s->new_session = 2; 2850Sstevel@tonic-gate s->state=SSL3_ST_SW_SRVR_HELLO_A; 2860Sstevel@tonic-gate s->init_num=0; 2870Sstevel@tonic-gate break; 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate case SSL3_ST_SW_SRVR_HELLO_A: 2900Sstevel@tonic-gate case SSL3_ST_SW_SRVR_HELLO_B: 2910Sstevel@tonic-gate ret=ssl3_send_server_hello(s); 2920Sstevel@tonic-gate if (ret <= 0) goto end; 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate if (s->hit) 2950Sstevel@tonic-gate s->state=SSL3_ST_SW_CHANGE_A; 2960Sstevel@tonic-gate else 2970Sstevel@tonic-gate s->state=SSL3_ST_SW_CERT_A; 2980Sstevel@tonic-gate s->init_num=0; 2990Sstevel@tonic-gate break; 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate case SSL3_ST_SW_CERT_A: 3020Sstevel@tonic-gate case SSL3_ST_SW_CERT_B: 3032139Sjp161948 /* Check if it is anon DH or anon ECDH */ 3040Sstevel@tonic-gate if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)) 3050Sstevel@tonic-gate { 3060Sstevel@tonic-gate ret=ssl3_send_server_certificate(s); 3070Sstevel@tonic-gate if (ret <= 0) goto end; 3080Sstevel@tonic-gate } 3090Sstevel@tonic-gate else 3100Sstevel@tonic-gate skip=1; 3110Sstevel@tonic-gate s->state=SSL3_ST_SW_KEY_EXCH_A; 3120Sstevel@tonic-gate s->init_num=0; 3130Sstevel@tonic-gate break; 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate case SSL3_ST_SW_KEY_EXCH_A: 3160Sstevel@tonic-gate case SSL3_ST_SW_KEY_EXCH_B: 3170Sstevel@tonic-gate l=s->s3->tmp.new_cipher->algorithms; 3180Sstevel@tonic-gate 3190Sstevel@tonic-gate /* clear this, it may get reset by 3200Sstevel@tonic-gate * send_server_key_exchange */ 3210Sstevel@tonic-gate if ((s->options & SSL_OP_EPHEMERAL_RSA) 3220Sstevel@tonic-gate #ifndef OPENSSL_NO_KRB5 3230Sstevel@tonic-gate && !(l & SSL_KRB5) 3240Sstevel@tonic-gate #endif /* OPENSSL_NO_KRB5 */ 3250Sstevel@tonic-gate ) 3260Sstevel@tonic-gate /* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key 3270Sstevel@tonic-gate * even when forbidden by protocol specs 3280Sstevel@tonic-gate * (handshake may fail as clients are not required to 3290Sstevel@tonic-gate * be able to handle this) */ 3300Sstevel@tonic-gate s->s3->tmp.use_rsa_tmp=1; 3310Sstevel@tonic-gate else 3320Sstevel@tonic-gate s->s3->tmp.use_rsa_tmp=0; 3330Sstevel@tonic-gate 3342139Sjp161948 3350Sstevel@tonic-gate /* only send if a DH key exchange, fortezza or 3362139Sjp161948 * RSA but we have a sign only certificate 3372139Sjp161948 * 3382139Sjp161948 * For ECC ciphersuites, we send a serverKeyExchange 3392139Sjp161948 * message only if the cipher suite is either 3402139Sjp161948 * ECDH-anon or ECDHE. In other cases, the 3412139Sjp161948 * server certificate contains the server's 3422139Sjp161948 * public key for key exchange. 3432139Sjp161948 */ 3440Sstevel@tonic-gate if (s->s3->tmp.use_rsa_tmp 3452139Sjp161948 || (l & SSL_kECDHE) 3460Sstevel@tonic-gate || (l & (SSL_DH|SSL_kFZA)) 3470Sstevel@tonic-gate || ((l & SSL_kRSA) 3480Sstevel@tonic-gate && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL 3490Sstevel@tonic-gate || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) 3500Sstevel@tonic-gate && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher) 3510Sstevel@tonic-gate ) 3520Sstevel@tonic-gate ) 3530Sstevel@tonic-gate ) 3540Sstevel@tonic-gate ) 3550Sstevel@tonic-gate { 3560Sstevel@tonic-gate ret=ssl3_send_server_key_exchange(s); 3570Sstevel@tonic-gate if (ret <= 0) goto end; 3580Sstevel@tonic-gate } 3590Sstevel@tonic-gate else 3600Sstevel@tonic-gate skip=1; 3610Sstevel@tonic-gate 3620Sstevel@tonic-gate s->state=SSL3_ST_SW_CERT_REQ_A; 3630Sstevel@tonic-gate s->init_num=0; 3640Sstevel@tonic-gate break; 3650Sstevel@tonic-gate 3660Sstevel@tonic-gate case SSL3_ST_SW_CERT_REQ_A: 3670Sstevel@tonic-gate case SSL3_ST_SW_CERT_REQ_B: 3680Sstevel@tonic-gate if (/* don't request cert unless asked for it: */ 3690Sstevel@tonic-gate !(s->verify_mode & SSL_VERIFY_PEER) || 3700Sstevel@tonic-gate /* if SSL_VERIFY_CLIENT_ONCE is set, 3710Sstevel@tonic-gate * don't request cert during re-negotiation: */ 3720Sstevel@tonic-gate ((s->session->peer != NULL) && 3730Sstevel@tonic-gate (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) || 3740Sstevel@tonic-gate /* never request cert in anonymous ciphersuites 3750Sstevel@tonic-gate * (see section "Certificate request" in SSL 3 drafts 3760Sstevel@tonic-gate * and in RFC 2246): */ 3770Sstevel@tonic-gate ((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) && 3780Sstevel@tonic-gate /* ... except when the application insists on verification 3790Sstevel@tonic-gate * (against the specs, but s3_clnt.c accepts this for SSL 3) */ 3800Sstevel@tonic-gate !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) || 3810Sstevel@tonic-gate /* never request cert in Kerberos ciphersuites */ 3820Sstevel@tonic-gate (s->s3->tmp.new_cipher->algorithms & SSL_aKRB5)) 3830Sstevel@tonic-gate { 3840Sstevel@tonic-gate /* no cert request */ 3850Sstevel@tonic-gate skip=1; 3860Sstevel@tonic-gate s->s3->tmp.cert_request=0; 3870Sstevel@tonic-gate s->state=SSL3_ST_SW_SRVR_DONE_A; 3880Sstevel@tonic-gate } 3890Sstevel@tonic-gate else 3900Sstevel@tonic-gate { 3910Sstevel@tonic-gate s->s3->tmp.cert_request=1; 3920Sstevel@tonic-gate ret=ssl3_send_certificate_request(s); 3930Sstevel@tonic-gate if (ret <= 0) goto end; 3940Sstevel@tonic-gate #ifndef NETSCAPE_HANG_BUG 3950Sstevel@tonic-gate s->state=SSL3_ST_SW_SRVR_DONE_A; 3960Sstevel@tonic-gate #else 3970Sstevel@tonic-gate s->state=SSL3_ST_SW_FLUSH; 3980Sstevel@tonic-gate s->s3->tmp.next_state=SSL3_ST_SR_CERT_A; 3990Sstevel@tonic-gate #endif 4000Sstevel@tonic-gate s->init_num=0; 4010Sstevel@tonic-gate } 4020Sstevel@tonic-gate break; 4030Sstevel@tonic-gate 4040Sstevel@tonic-gate case SSL3_ST_SW_SRVR_DONE_A: 4050Sstevel@tonic-gate case SSL3_ST_SW_SRVR_DONE_B: 4060Sstevel@tonic-gate ret=ssl3_send_server_done(s); 4070Sstevel@tonic-gate if (ret <= 0) goto end; 4080Sstevel@tonic-gate s->s3->tmp.next_state=SSL3_ST_SR_CERT_A; 4090Sstevel@tonic-gate s->state=SSL3_ST_SW_FLUSH; 4100Sstevel@tonic-gate s->init_num=0; 4110Sstevel@tonic-gate break; 4120Sstevel@tonic-gate 4130Sstevel@tonic-gate case SSL3_ST_SW_FLUSH: 4140Sstevel@tonic-gate /* number of bytes to be flushed */ 4150Sstevel@tonic-gate num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL); 4160Sstevel@tonic-gate if (num1 > 0) 4170Sstevel@tonic-gate { 4180Sstevel@tonic-gate s->rwstate=SSL_WRITING; 4190Sstevel@tonic-gate num1=BIO_flush(s->wbio); 4200Sstevel@tonic-gate if (num1 <= 0) { ret= -1; goto end; } 4210Sstevel@tonic-gate s->rwstate=SSL_NOTHING; 4220Sstevel@tonic-gate } 4230Sstevel@tonic-gate 4240Sstevel@tonic-gate s->state=s->s3->tmp.next_state; 4250Sstevel@tonic-gate break; 4260Sstevel@tonic-gate 4270Sstevel@tonic-gate case SSL3_ST_SR_CERT_A: 4280Sstevel@tonic-gate case SSL3_ST_SR_CERT_B: 4290Sstevel@tonic-gate /* Check for second client hello (MS SGC) */ 4300Sstevel@tonic-gate ret = ssl3_check_client_hello(s); 4310Sstevel@tonic-gate if (ret <= 0) 4320Sstevel@tonic-gate goto end; 4330Sstevel@tonic-gate if (ret == 2) 4340Sstevel@tonic-gate s->state = SSL3_ST_SR_CLNT_HELLO_C; 4350Sstevel@tonic-gate else { 4360Sstevel@tonic-gate if (s->s3->tmp.cert_request) 4370Sstevel@tonic-gate { 4380Sstevel@tonic-gate ret=ssl3_get_client_certificate(s); 4390Sstevel@tonic-gate if (ret <= 0) goto end; 4400Sstevel@tonic-gate } 4410Sstevel@tonic-gate s->init_num=0; 4420Sstevel@tonic-gate s->state=SSL3_ST_SR_KEY_EXCH_A; 4430Sstevel@tonic-gate } 4440Sstevel@tonic-gate break; 4450Sstevel@tonic-gate 4460Sstevel@tonic-gate case SSL3_ST_SR_KEY_EXCH_A: 4470Sstevel@tonic-gate case SSL3_ST_SR_KEY_EXCH_B: 4480Sstevel@tonic-gate ret=ssl3_get_client_key_exchange(s); 4492139Sjp161948 if (ret <= 0) 4502139Sjp161948 goto end; 4512139Sjp161948 if (ret == 2) 4522139Sjp161948 { 4532139Sjp161948 /* For the ECDH ciphersuites when 4542139Sjp161948 * the client sends its ECDH pub key in 4552139Sjp161948 * a certificate, the CertificateVerify 4562139Sjp161948 * message is not sent. 4572139Sjp161948 */ 4582139Sjp161948 s->state=SSL3_ST_SR_FINISHED_A; 4592139Sjp161948 s->init_num = 0; 4602139Sjp161948 } 4612139Sjp161948 else 4622139Sjp161948 { 4632139Sjp161948 s->state=SSL3_ST_SR_CERT_VRFY_A; 4642139Sjp161948 s->init_num=0; 4650Sstevel@tonic-gate 4662139Sjp161948 /* We need to get hashes here so if there is 4672139Sjp161948 * a client cert, it can be verified 4682139Sjp161948 */ 4692139Sjp161948 s->method->ssl3_enc->cert_verify_mac(s, 4702139Sjp161948 &(s->s3->finish_dgst1), 4712139Sjp161948 &(s->s3->tmp.cert_verify_md[0])); 4722139Sjp161948 s->method->ssl3_enc->cert_verify_mac(s, 4732139Sjp161948 &(s->s3->finish_dgst2), 4742139Sjp161948 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH])); 4752139Sjp161948 } 4760Sstevel@tonic-gate break; 4770Sstevel@tonic-gate 4780Sstevel@tonic-gate case SSL3_ST_SR_CERT_VRFY_A: 4790Sstevel@tonic-gate case SSL3_ST_SR_CERT_VRFY_B: 4800Sstevel@tonic-gate 4810Sstevel@tonic-gate /* we should decide if we expected this one */ 4820Sstevel@tonic-gate ret=ssl3_get_cert_verify(s); 4830Sstevel@tonic-gate if (ret <= 0) goto end; 4840Sstevel@tonic-gate 4850Sstevel@tonic-gate s->state=SSL3_ST_SR_FINISHED_A; 4860Sstevel@tonic-gate s->init_num=0; 4870Sstevel@tonic-gate break; 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate case SSL3_ST_SR_FINISHED_A: 4900Sstevel@tonic-gate case SSL3_ST_SR_FINISHED_B: 4910Sstevel@tonic-gate ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A, 4920Sstevel@tonic-gate SSL3_ST_SR_FINISHED_B); 4930Sstevel@tonic-gate if (ret <= 0) goto end; 4940Sstevel@tonic-gate if (s->hit) 4950Sstevel@tonic-gate s->state=SSL_ST_OK; 4960Sstevel@tonic-gate else 4970Sstevel@tonic-gate s->state=SSL3_ST_SW_CHANGE_A; 4980Sstevel@tonic-gate s->init_num=0; 4990Sstevel@tonic-gate break; 5000Sstevel@tonic-gate 5010Sstevel@tonic-gate case SSL3_ST_SW_CHANGE_A: 5020Sstevel@tonic-gate case SSL3_ST_SW_CHANGE_B: 5030Sstevel@tonic-gate 5040Sstevel@tonic-gate s->session->cipher=s->s3->tmp.new_cipher; 5050Sstevel@tonic-gate if (!s->method->ssl3_enc->setup_key_block(s)) 5060Sstevel@tonic-gate { ret= -1; goto end; } 5070Sstevel@tonic-gate 5080Sstevel@tonic-gate ret=ssl3_send_change_cipher_spec(s, 5090Sstevel@tonic-gate SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B); 5100Sstevel@tonic-gate 5110Sstevel@tonic-gate if (ret <= 0) goto end; 5120Sstevel@tonic-gate s->state=SSL3_ST_SW_FINISHED_A; 5130Sstevel@tonic-gate s->init_num=0; 5140Sstevel@tonic-gate 5150Sstevel@tonic-gate if (!s->method->ssl3_enc->change_cipher_state(s, 5160Sstevel@tonic-gate SSL3_CHANGE_CIPHER_SERVER_WRITE)) 5170Sstevel@tonic-gate { 5180Sstevel@tonic-gate ret= -1; 5190Sstevel@tonic-gate goto end; 5200Sstevel@tonic-gate } 5210Sstevel@tonic-gate 5220Sstevel@tonic-gate break; 5230Sstevel@tonic-gate 5240Sstevel@tonic-gate case SSL3_ST_SW_FINISHED_A: 5250Sstevel@tonic-gate case SSL3_ST_SW_FINISHED_B: 5260Sstevel@tonic-gate ret=ssl3_send_finished(s, 5270Sstevel@tonic-gate SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B, 5280Sstevel@tonic-gate s->method->ssl3_enc->server_finished_label, 5290Sstevel@tonic-gate s->method->ssl3_enc->server_finished_label_len); 5300Sstevel@tonic-gate if (ret <= 0) goto end; 5310Sstevel@tonic-gate s->state=SSL3_ST_SW_FLUSH; 5320Sstevel@tonic-gate if (s->hit) 5330Sstevel@tonic-gate s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A; 5340Sstevel@tonic-gate else 5350Sstevel@tonic-gate s->s3->tmp.next_state=SSL_ST_OK; 5360Sstevel@tonic-gate s->init_num=0; 5370Sstevel@tonic-gate break; 5380Sstevel@tonic-gate 5390Sstevel@tonic-gate case SSL_ST_OK: 5400Sstevel@tonic-gate /* clean a few things up */ 5410Sstevel@tonic-gate ssl3_cleanup_key_block(s); 5420Sstevel@tonic-gate 5430Sstevel@tonic-gate BUF_MEM_free(s->init_buf); 5440Sstevel@tonic-gate s->init_buf=NULL; 5450Sstevel@tonic-gate 5460Sstevel@tonic-gate /* remove buffering on output */ 5470Sstevel@tonic-gate ssl_free_wbio_buffer(s); 5480Sstevel@tonic-gate 5490Sstevel@tonic-gate s->init_num=0; 5500Sstevel@tonic-gate 5510Sstevel@tonic-gate if (s->new_session == 2) /* skipped if we just sent a HelloRequest */ 5520Sstevel@tonic-gate { 5530Sstevel@tonic-gate /* actually not necessarily a 'new' session unless 5540Sstevel@tonic-gate * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set */ 5550Sstevel@tonic-gate 5560Sstevel@tonic-gate s->new_session=0; 5570Sstevel@tonic-gate 5580Sstevel@tonic-gate ssl_update_cache(s,SSL_SESS_CACHE_SERVER); 5590Sstevel@tonic-gate 5600Sstevel@tonic-gate s->ctx->stats.sess_accept_good++; 5610Sstevel@tonic-gate /* s->server=1; */ 5620Sstevel@tonic-gate s->handshake_func=ssl3_accept; 5630Sstevel@tonic-gate 5640Sstevel@tonic-gate if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1); 5650Sstevel@tonic-gate } 5660Sstevel@tonic-gate 5670Sstevel@tonic-gate ret = 1; 5680Sstevel@tonic-gate goto end; 5690Sstevel@tonic-gate /* break; */ 5700Sstevel@tonic-gate 5710Sstevel@tonic-gate default: 5720Sstevel@tonic-gate SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE); 5730Sstevel@tonic-gate ret= -1; 5740Sstevel@tonic-gate goto end; 5750Sstevel@tonic-gate /* break; */ 5760Sstevel@tonic-gate } 5770Sstevel@tonic-gate 5780Sstevel@tonic-gate if (!s->s3->tmp.reuse_message && !skip) 5790Sstevel@tonic-gate { 5800Sstevel@tonic-gate if (s->debug) 5810Sstevel@tonic-gate { 5820Sstevel@tonic-gate if ((ret=BIO_flush(s->wbio)) <= 0) 5830Sstevel@tonic-gate goto end; 5840Sstevel@tonic-gate } 5850Sstevel@tonic-gate 5860Sstevel@tonic-gate 5870Sstevel@tonic-gate if ((cb != NULL) && (s->state != state)) 5880Sstevel@tonic-gate { 5890Sstevel@tonic-gate new_state=s->state; 5900Sstevel@tonic-gate s->state=state; 5910Sstevel@tonic-gate cb(s,SSL_CB_ACCEPT_LOOP,1); 5920Sstevel@tonic-gate s->state=new_state; 5930Sstevel@tonic-gate } 5940Sstevel@tonic-gate } 5950Sstevel@tonic-gate skip=0; 5960Sstevel@tonic-gate } 5970Sstevel@tonic-gate end: 5980Sstevel@tonic-gate /* BIO_flush(s->wbio); */ 5990Sstevel@tonic-gate 6000Sstevel@tonic-gate s->in_handshake--; 6010Sstevel@tonic-gate if (cb != NULL) 6020Sstevel@tonic-gate cb(s,SSL_CB_ACCEPT_EXIT,ret); 6030Sstevel@tonic-gate return(ret); 6040Sstevel@tonic-gate } 6050Sstevel@tonic-gate 6062139Sjp161948 int ssl3_send_hello_request(SSL *s) 6070Sstevel@tonic-gate { 6080Sstevel@tonic-gate unsigned char *p; 6090Sstevel@tonic-gate 6100Sstevel@tonic-gate if (s->state == SSL3_ST_SW_HELLO_REQ_A) 6110Sstevel@tonic-gate { 6120Sstevel@tonic-gate p=(unsigned char *)s->init_buf->data; 6130Sstevel@tonic-gate *(p++)=SSL3_MT_HELLO_REQUEST; 6140Sstevel@tonic-gate *(p++)=0; 6150Sstevel@tonic-gate *(p++)=0; 6160Sstevel@tonic-gate *(p++)=0; 6170Sstevel@tonic-gate 6180Sstevel@tonic-gate s->state=SSL3_ST_SW_HELLO_REQ_B; 6190Sstevel@tonic-gate /* number of bytes to write */ 6200Sstevel@tonic-gate s->init_num=4; 6210Sstevel@tonic-gate s->init_off=0; 6220Sstevel@tonic-gate } 6230Sstevel@tonic-gate 6240Sstevel@tonic-gate /* SSL3_ST_SW_HELLO_REQ_B */ 6250Sstevel@tonic-gate return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 6260Sstevel@tonic-gate } 6270Sstevel@tonic-gate 6282139Sjp161948 int ssl3_check_client_hello(SSL *s) 6290Sstevel@tonic-gate { 6300Sstevel@tonic-gate int ok; 6310Sstevel@tonic-gate long n; 6320Sstevel@tonic-gate 6330Sstevel@tonic-gate /* this function is called when we really expect a Certificate message, 6340Sstevel@tonic-gate * so permit appropriate message length */ 6352139Sjp161948 n=s->method->ssl_get_message(s, 6360Sstevel@tonic-gate SSL3_ST_SR_CERT_A, 6370Sstevel@tonic-gate SSL3_ST_SR_CERT_B, 6380Sstevel@tonic-gate -1, 6390Sstevel@tonic-gate s->max_cert_list, 6400Sstevel@tonic-gate &ok); 6410Sstevel@tonic-gate if (!ok) return((int)n); 6420Sstevel@tonic-gate s->s3->tmp.reuse_message = 1; 6430Sstevel@tonic-gate if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO) 6440Sstevel@tonic-gate { 6450Sstevel@tonic-gate /* Throw away what we have done so far in the current handshake, 6460Sstevel@tonic-gate * which will now be aborted. (A full SSL_clear would be too much.) 6470Sstevel@tonic-gate * I hope that tmp.dh is the only thing that may need to be cleared 6480Sstevel@tonic-gate * when a handshake is not completed ... */ 6490Sstevel@tonic-gate #ifndef OPENSSL_NO_DH 6500Sstevel@tonic-gate if (s->s3->tmp.dh != NULL) 6510Sstevel@tonic-gate { 6520Sstevel@tonic-gate DH_free(s->s3->tmp.dh); 6530Sstevel@tonic-gate s->s3->tmp.dh = NULL; 6540Sstevel@tonic-gate } 6550Sstevel@tonic-gate #endif 6560Sstevel@tonic-gate return 2; 6570Sstevel@tonic-gate } 6580Sstevel@tonic-gate return 1; 6590Sstevel@tonic-gate } 6600Sstevel@tonic-gate 6612139Sjp161948 int ssl3_get_client_hello(SSL *s) 6620Sstevel@tonic-gate { 6630Sstevel@tonic-gate int i,j,ok,al,ret= -1; 6642139Sjp161948 unsigned int cookie_len; 6650Sstevel@tonic-gate long n; 6660Sstevel@tonic-gate unsigned long id; 6670Sstevel@tonic-gate unsigned char *p,*d,*q; 6680Sstevel@tonic-gate SSL_CIPHER *c; 6692139Sjp161948 #ifndef OPENSSL_NO_COMP 6700Sstevel@tonic-gate SSL_COMP *comp=NULL; 6712139Sjp161948 #endif 6720Sstevel@tonic-gate STACK_OF(SSL_CIPHER) *ciphers=NULL; 6730Sstevel@tonic-gate 6740Sstevel@tonic-gate /* We do this so that we will respond with our native type. 6750Sstevel@tonic-gate * If we are TLSv1 and we get SSLv3, we will respond with TLSv1, 6760Sstevel@tonic-gate * This down switching should be handled by a different method. 6770Sstevel@tonic-gate * If we are SSLv3, we will respond with SSLv3, even if prompted with 6780Sstevel@tonic-gate * TLSv1. 6790Sstevel@tonic-gate */ 6800Sstevel@tonic-gate if (s->state == SSL3_ST_SR_CLNT_HELLO_A) 6810Sstevel@tonic-gate { 6820Sstevel@tonic-gate s->first_packet=1; 6830Sstevel@tonic-gate s->state=SSL3_ST_SR_CLNT_HELLO_B; 6840Sstevel@tonic-gate } 6852139Sjp161948 n=s->method->ssl_get_message(s, 6860Sstevel@tonic-gate SSL3_ST_SR_CLNT_HELLO_B, 6870Sstevel@tonic-gate SSL3_ST_SR_CLNT_HELLO_C, 6880Sstevel@tonic-gate SSL3_MT_CLIENT_HELLO, 6890Sstevel@tonic-gate SSL3_RT_MAX_PLAIN_LENGTH, 6900Sstevel@tonic-gate &ok); 6910Sstevel@tonic-gate 6920Sstevel@tonic-gate if (!ok) return((int)n); 6930Sstevel@tonic-gate d=p=(unsigned char *)s->init_msg; 6940Sstevel@tonic-gate 6950Sstevel@tonic-gate /* use version from inside client hello, not from record header 6960Sstevel@tonic-gate * (may differ: see RFC 2246, Appendix E, second paragraph) */ 6970Sstevel@tonic-gate s->client_version=(((int)p[0])<<8)|(int)p[1]; 6980Sstevel@tonic-gate p+=2; 6990Sstevel@tonic-gate 7000Sstevel@tonic-gate if (s->client_version < s->version) 7010Sstevel@tonic-gate { 7020Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER); 7030Sstevel@tonic-gate if ((s->client_version>>8) == SSL3_VERSION_MAJOR) 7040Sstevel@tonic-gate { 7050Sstevel@tonic-gate /* similar to ssl3_get_record, send alert using remote version number */ 7060Sstevel@tonic-gate s->version = s->client_version; 7070Sstevel@tonic-gate } 7080Sstevel@tonic-gate al = SSL_AD_PROTOCOL_VERSION; 7090Sstevel@tonic-gate goto f_err; 7100Sstevel@tonic-gate } 7110Sstevel@tonic-gate 7120Sstevel@tonic-gate /* load the client random */ 7130Sstevel@tonic-gate memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE); 7140Sstevel@tonic-gate p+=SSL3_RANDOM_SIZE; 7150Sstevel@tonic-gate 7160Sstevel@tonic-gate /* get the session-id */ 7170Sstevel@tonic-gate j= *(p++); 7180Sstevel@tonic-gate 7190Sstevel@tonic-gate s->hit=0; 7200Sstevel@tonic-gate /* Versions before 0.9.7 always allow session reuse during renegotiation 7210Sstevel@tonic-gate * (i.e. when s->new_session is true), option 7220Sstevel@tonic-gate * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is new with 0.9.7. 7230Sstevel@tonic-gate * Maybe this optional behaviour should always have been the default, 7240Sstevel@tonic-gate * but we cannot safely change the default behaviour (or new applications 7250Sstevel@tonic-gate * might be written that become totally unsecure when compiled with 7260Sstevel@tonic-gate * an earlier library version) 7270Sstevel@tonic-gate */ 7280Sstevel@tonic-gate if (j == 0 || (s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) 7290Sstevel@tonic-gate { 7300Sstevel@tonic-gate if (!ssl_get_new_session(s,1)) 7310Sstevel@tonic-gate goto err; 7320Sstevel@tonic-gate } 7330Sstevel@tonic-gate else 7340Sstevel@tonic-gate { 7350Sstevel@tonic-gate i=ssl_get_prev_session(s,p,j); 7360Sstevel@tonic-gate if (i == 1) 7370Sstevel@tonic-gate { /* previous session */ 7380Sstevel@tonic-gate s->hit=1; 7390Sstevel@tonic-gate } 7400Sstevel@tonic-gate else if (i == -1) 7410Sstevel@tonic-gate goto err; 7420Sstevel@tonic-gate else /* i == 0 */ 7430Sstevel@tonic-gate { 7440Sstevel@tonic-gate if (!ssl_get_new_session(s,1)) 7450Sstevel@tonic-gate goto err; 7460Sstevel@tonic-gate } 7470Sstevel@tonic-gate } 7480Sstevel@tonic-gate 7490Sstevel@tonic-gate p+=j; 7502139Sjp161948 7512139Sjp161948 if (SSL_version(s) == DTLS1_VERSION) 7522139Sjp161948 { 7532139Sjp161948 /* cookie stuff */ 7542139Sjp161948 cookie_len = *(p++); 7552139Sjp161948 7562139Sjp161948 if ( (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) && 7572139Sjp161948 s->d1->send_cookie == 0) 7582139Sjp161948 { 7592139Sjp161948 /* HelloVerifyMessage has already been sent */ 7602139Sjp161948 if ( cookie_len != s->d1->cookie_len) 7612139Sjp161948 { 7622139Sjp161948 al = SSL_AD_HANDSHAKE_FAILURE; 7632139Sjp161948 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH); 7642139Sjp161948 goto f_err; 7652139Sjp161948 } 7662139Sjp161948 } 7672139Sjp161948 7682139Sjp161948 /* 7692139Sjp161948 * The ClientHello may contain a cookie even if the 7702139Sjp161948 * HelloVerify message has not been sent--make sure that it 7712139Sjp161948 * does not cause an overflow. 7722139Sjp161948 */ 7732139Sjp161948 if ( cookie_len > sizeof(s->d1->rcvd_cookie)) 7742139Sjp161948 { 7752139Sjp161948 /* too much data */ 7762139Sjp161948 al = SSL_AD_DECODE_ERROR; 7772139Sjp161948 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH); 7782139Sjp161948 goto f_err; 7792139Sjp161948 } 7802139Sjp161948 7812139Sjp161948 /* verify the cookie if appropriate option is set. */ 7822139Sjp161948 if ( (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) && 7832139Sjp161948 cookie_len > 0) 7842139Sjp161948 { 7852139Sjp161948 memcpy(s->d1->rcvd_cookie, p, cookie_len); 7862139Sjp161948 7872139Sjp161948 if ( s->ctx->app_verify_cookie_cb != NULL) 7882139Sjp161948 { 7892139Sjp161948 if ( s->ctx->app_verify_cookie_cb(s, s->d1->rcvd_cookie, 7902139Sjp161948 cookie_len) == 0) 7912139Sjp161948 { 7922139Sjp161948 al=SSL_AD_HANDSHAKE_FAILURE; 7932139Sjp161948 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, 7942139Sjp161948 SSL_R_COOKIE_MISMATCH); 7952139Sjp161948 goto f_err; 7962139Sjp161948 } 7972139Sjp161948 /* else cookie verification succeeded */ 7982139Sjp161948 } 7992139Sjp161948 else if ( memcmp(s->d1->rcvd_cookie, s->d1->cookie, 8002139Sjp161948 s->d1->cookie_len) != 0) /* default verification */ 8012139Sjp161948 { 8022139Sjp161948 al=SSL_AD_HANDSHAKE_FAILURE; 8032139Sjp161948 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, 8042139Sjp161948 SSL_R_COOKIE_MISMATCH); 8052139Sjp161948 goto f_err; 8062139Sjp161948 } 8072139Sjp161948 } 8082139Sjp161948 8092139Sjp161948 p += cookie_len; 8102139Sjp161948 } 8112139Sjp161948 8120Sstevel@tonic-gate n2s(p,i); 8130Sstevel@tonic-gate if ((i == 0) && (j != 0)) 8140Sstevel@tonic-gate { 8150Sstevel@tonic-gate /* we need a cipher if we are not resuming a session */ 8160Sstevel@tonic-gate al=SSL_AD_ILLEGAL_PARAMETER; 8170Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED); 8180Sstevel@tonic-gate goto f_err; 8190Sstevel@tonic-gate } 8200Sstevel@tonic-gate if ((p+i) >= (d+n)) 8210Sstevel@tonic-gate { 8220Sstevel@tonic-gate /* not enough data */ 8230Sstevel@tonic-gate al=SSL_AD_DECODE_ERROR; 8240Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH); 8250Sstevel@tonic-gate goto f_err; 8260Sstevel@tonic-gate } 8270Sstevel@tonic-gate if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers)) 8280Sstevel@tonic-gate == NULL)) 8290Sstevel@tonic-gate { 8300Sstevel@tonic-gate goto err; 8310Sstevel@tonic-gate } 8320Sstevel@tonic-gate p+=i; 8330Sstevel@tonic-gate 8340Sstevel@tonic-gate /* If it is a hit, check that the cipher is in the list */ 8350Sstevel@tonic-gate if ((s->hit) && (i > 0)) 8360Sstevel@tonic-gate { 8370Sstevel@tonic-gate j=0; 8380Sstevel@tonic-gate id=s->session->cipher->id; 8390Sstevel@tonic-gate 8400Sstevel@tonic-gate #ifdef CIPHER_DEBUG 8410Sstevel@tonic-gate printf("client sent %d ciphers\n",sk_num(ciphers)); 8420Sstevel@tonic-gate #endif 8430Sstevel@tonic-gate for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++) 8440Sstevel@tonic-gate { 8450Sstevel@tonic-gate c=sk_SSL_CIPHER_value(ciphers,i); 8460Sstevel@tonic-gate #ifdef CIPHER_DEBUG 8470Sstevel@tonic-gate printf("client [%2d of %2d]:%s\n", 8480Sstevel@tonic-gate i,sk_num(ciphers),SSL_CIPHER_get_name(c)); 8490Sstevel@tonic-gate #endif 8500Sstevel@tonic-gate if (c->id == id) 8510Sstevel@tonic-gate { 8520Sstevel@tonic-gate j=1; 8530Sstevel@tonic-gate break; 8540Sstevel@tonic-gate } 8550Sstevel@tonic-gate } 8560Sstevel@tonic-gate if (j == 0) 8570Sstevel@tonic-gate { 8580Sstevel@tonic-gate if ((s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1)) 8590Sstevel@tonic-gate { 8600Sstevel@tonic-gate /* Very bad for multi-threading.... */ 8612139Sjp161948 s->session->cipher=sk_SSL_CIPHER_value(ciphers, 0); 8620Sstevel@tonic-gate } 8630Sstevel@tonic-gate else 8640Sstevel@tonic-gate { 8650Sstevel@tonic-gate /* we need to have the cipher in the cipher 8660Sstevel@tonic-gate * list if we are asked to reuse it */ 8670Sstevel@tonic-gate al=SSL_AD_ILLEGAL_PARAMETER; 8680Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING); 8690Sstevel@tonic-gate goto f_err; 8700Sstevel@tonic-gate } 8710Sstevel@tonic-gate } 8720Sstevel@tonic-gate } 8730Sstevel@tonic-gate 8740Sstevel@tonic-gate /* compression */ 8750Sstevel@tonic-gate i= *(p++); 8760Sstevel@tonic-gate if ((p+i) > (d+n)) 8770Sstevel@tonic-gate { 8780Sstevel@tonic-gate /* not enough data */ 8790Sstevel@tonic-gate al=SSL_AD_DECODE_ERROR; 8800Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH); 8810Sstevel@tonic-gate goto f_err; 8820Sstevel@tonic-gate } 8830Sstevel@tonic-gate q=p; 8840Sstevel@tonic-gate for (j=0; j<i; j++) 8850Sstevel@tonic-gate { 8860Sstevel@tonic-gate if (p[j] == 0) break; 8870Sstevel@tonic-gate } 8880Sstevel@tonic-gate 8890Sstevel@tonic-gate p+=i; 8900Sstevel@tonic-gate if (j >= i) 8910Sstevel@tonic-gate { 8920Sstevel@tonic-gate /* no compress */ 8930Sstevel@tonic-gate al=SSL_AD_DECODE_ERROR; 8940Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIED); 8950Sstevel@tonic-gate goto f_err; 8960Sstevel@tonic-gate } 8970Sstevel@tonic-gate 8980Sstevel@tonic-gate /* Worst case, we will use the NULL compression, but if we have other 8990Sstevel@tonic-gate * options, we will now look for them. We have i-1 compression 9000Sstevel@tonic-gate * algorithms from the client, starting at q. */ 9010Sstevel@tonic-gate s->s3->tmp.new_compression=NULL; 9022139Sjp161948 #ifndef OPENSSL_NO_COMP 9030Sstevel@tonic-gate if (s->ctx->comp_methods != NULL) 9040Sstevel@tonic-gate { /* See if we have a match */ 9050Sstevel@tonic-gate int m,nn,o,v,done=0; 9060Sstevel@tonic-gate 9070Sstevel@tonic-gate nn=sk_SSL_COMP_num(s->ctx->comp_methods); 9080Sstevel@tonic-gate for (m=0; m<nn; m++) 9090Sstevel@tonic-gate { 9100Sstevel@tonic-gate comp=sk_SSL_COMP_value(s->ctx->comp_methods,m); 9110Sstevel@tonic-gate v=comp->id; 9120Sstevel@tonic-gate for (o=0; o<i; o++) 9130Sstevel@tonic-gate { 9140Sstevel@tonic-gate if (v == q[o]) 9150Sstevel@tonic-gate { 9160Sstevel@tonic-gate done=1; 9170Sstevel@tonic-gate break; 9180Sstevel@tonic-gate } 9190Sstevel@tonic-gate } 9200Sstevel@tonic-gate if (done) break; 9210Sstevel@tonic-gate } 9220Sstevel@tonic-gate if (done) 9230Sstevel@tonic-gate s->s3->tmp.new_compression=comp; 9240Sstevel@tonic-gate else 9250Sstevel@tonic-gate comp=NULL; 9260Sstevel@tonic-gate } 9272139Sjp161948 #endif 9280Sstevel@tonic-gate 9290Sstevel@tonic-gate /* TLS does not mind if there is extra stuff */ 9300Sstevel@tonic-gate #if 0 /* SSL 3.0 does not mind either, so we should disable this test 9310Sstevel@tonic-gate * (was enabled in 0.9.6d through 0.9.6j and 0.9.7 through 0.9.7b, 9320Sstevel@tonic-gate * in earlier SSLeay/OpenSSL releases this test existed but was buggy) */ 9330Sstevel@tonic-gate if (s->version == SSL3_VERSION) 9340Sstevel@tonic-gate { 9350Sstevel@tonic-gate if (p < (d+n)) 9360Sstevel@tonic-gate { 9370Sstevel@tonic-gate /* wrong number of bytes, 9380Sstevel@tonic-gate * there could be more to follow */ 9390Sstevel@tonic-gate al=SSL_AD_DECODE_ERROR; 9400Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH); 9410Sstevel@tonic-gate goto f_err; 9420Sstevel@tonic-gate } 9430Sstevel@tonic-gate } 9440Sstevel@tonic-gate #endif 9450Sstevel@tonic-gate 9460Sstevel@tonic-gate /* Given s->session->ciphers and SSL_get_ciphers, we must 9470Sstevel@tonic-gate * pick a cipher */ 9480Sstevel@tonic-gate 9490Sstevel@tonic-gate if (!s->hit) 9500Sstevel@tonic-gate { 9512139Sjp161948 #ifdef OPENSSL_NO_COMP 9522139Sjp161948 s->session->compress_meth=0; 9532139Sjp161948 #else 9540Sstevel@tonic-gate s->session->compress_meth=(comp == NULL)?0:comp->id; 9552139Sjp161948 #endif 9560Sstevel@tonic-gate if (s->session->ciphers != NULL) 9570Sstevel@tonic-gate sk_SSL_CIPHER_free(s->session->ciphers); 9580Sstevel@tonic-gate s->session->ciphers=ciphers; 9590Sstevel@tonic-gate if (ciphers == NULL) 9600Sstevel@tonic-gate { 9610Sstevel@tonic-gate al=SSL_AD_ILLEGAL_PARAMETER; 9620Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASSED); 9630Sstevel@tonic-gate goto f_err; 9640Sstevel@tonic-gate } 9650Sstevel@tonic-gate ciphers=NULL; 9660Sstevel@tonic-gate c=ssl3_choose_cipher(s,s->session->ciphers, 9670Sstevel@tonic-gate SSL_get_ciphers(s)); 9680Sstevel@tonic-gate 9690Sstevel@tonic-gate if (c == NULL) 9700Sstevel@tonic-gate { 9710Sstevel@tonic-gate al=SSL_AD_HANDSHAKE_FAILURE; 9720Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER); 9730Sstevel@tonic-gate goto f_err; 9740Sstevel@tonic-gate } 9750Sstevel@tonic-gate s->s3->tmp.new_cipher=c; 9760Sstevel@tonic-gate } 9770Sstevel@tonic-gate else 9780Sstevel@tonic-gate { 9790Sstevel@tonic-gate /* Session-id reuse */ 9800Sstevel@tonic-gate #ifdef REUSE_CIPHER_BUG 9810Sstevel@tonic-gate STACK_OF(SSL_CIPHER) *sk; 9820Sstevel@tonic-gate SSL_CIPHER *nc=NULL; 9830Sstevel@tonic-gate SSL_CIPHER *ec=NULL; 9840Sstevel@tonic-gate 9850Sstevel@tonic-gate if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG) 9860Sstevel@tonic-gate { 9870Sstevel@tonic-gate sk=s->session->ciphers; 9880Sstevel@tonic-gate for (i=0; i<sk_SSL_CIPHER_num(sk); i++) 9890Sstevel@tonic-gate { 9900Sstevel@tonic-gate c=sk_SSL_CIPHER_value(sk,i); 9910Sstevel@tonic-gate if (c->algorithms & SSL_eNULL) 9920Sstevel@tonic-gate nc=c; 9930Sstevel@tonic-gate if (SSL_C_IS_EXPORT(c)) 9940Sstevel@tonic-gate ec=c; 9950Sstevel@tonic-gate } 9960Sstevel@tonic-gate if (nc != NULL) 9970Sstevel@tonic-gate s->s3->tmp.new_cipher=nc; 9980Sstevel@tonic-gate else if (ec != NULL) 9990Sstevel@tonic-gate s->s3->tmp.new_cipher=ec; 10000Sstevel@tonic-gate else 10010Sstevel@tonic-gate s->s3->tmp.new_cipher=s->session->cipher; 10020Sstevel@tonic-gate } 10030Sstevel@tonic-gate else 10040Sstevel@tonic-gate #endif 10050Sstevel@tonic-gate s->s3->tmp.new_cipher=s->session->cipher; 10060Sstevel@tonic-gate } 10070Sstevel@tonic-gate 10080Sstevel@tonic-gate /* we now have the following setup. 10090Sstevel@tonic-gate * client_random 10100Sstevel@tonic-gate * cipher_list - our prefered list of ciphers 10110Sstevel@tonic-gate * ciphers - the clients prefered list of ciphers 10120Sstevel@tonic-gate * compression - basically ignored right now 10130Sstevel@tonic-gate * ssl version is set - sslv3 10140Sstevel@tonic-gate * s->session - The ssl session has been setup. 10150Sstevel@tonic-gate * s->hit - session reuse flag 10160Sstevel@tonic-gate * s->tmp.new_cipher - the new cipher to use. 10170Sstevel@tonic-gate */ 10180Sstevel@tonic-gate 10190Sstevel@tonic-gate ret=1; 10200Sstevel@tonic-gate if (0) 10210Sstevel@tonic-gate { 10220Sstevel@tonic-gate f_err: 10230Sstevel@tonic-gate ssl3_send_alert(s,SSL3_AL_FATAL,al); 10240Sstevel@tonic-gate } 10250Sstevel@tonic-gate err: 10260Sstevel@tonic-gate if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers); 10270Sstevel@tonic-gate return(ret); 10280Sstevel@tonic-gate } 10290Sstevel@tonic-gate 10302139Sjp161948 int ssl3_send_server_hello(SSL *s) 10310Sstevel@tonic-gate { 10320Sstevel@tonic-gate unsigned char *buf; 10330Sstevel@tonic-gate unsigned char *p,*d; 10340Sstevel@tonic-gate int i,sl; 10350Sstevel@tonic-gate unsigned long l,Time; 10360Sstevel@tonic-gate 10370Sstevel@tonic-gate if (s->state == SSL3_ST_SW_SRVR_HELLO_A) 10380Sstevel@tonic-gate { 10390Sstevel@tonic-gate buf=(unsigned char *)s->init_buf->data; 10400Sstevel@tonic-gate p=s->s3->server_random; 10410Sstevel@tonic-gate Time=time(NULL); /* Time */ 10420Sstevel@tonic-gate l2n(Time,p); 10432139Sjp161948 if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0) 10442139Sjp161948 return -1; 10450Sstevel@tonic-gate /* Do the message type and length last */ 10460Sstevel@tonic-gate d=p= &(buf[4]); 10470Sstevel@tonic-gate 10480Sstevel@tonic-gate *(p++)=s->version>>8; 10490Sstevel@tonic-gate *(p++)=s->version&0xff; 10500Sstevel@tonic-gate 10510Sstevel@tonic-gate /* Random stuff */ 10520Sstevel@tonic-gate memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE); 10530Sstevel@tonic-gate p+=SSL3_RANDOM_SIZE; 10540Sstevel@tonic-gate 10550Sstevel@tonic-gate /* now in theory we have 3 options to sending back the 10560Sstevel@tonic-gate * session id. If it is a re-use, we send back the 10570Sstevel@tonic-gate * old session-id, if it is a new session, we send 10580Sstevel@tonic-gate * back the new session-id or we send back a 0 length 10590Sstevel@tonic-gate * session-id if we want it to be single use. 10600Sstevel@tonic-gate * Currently I will not implement the '0' length session-id 10610Sstevel@tonic-gate * 12-Jan-98 - I'll now support the '0' length stuff. 10620Sstevel@tonic-gate */ 10630Sstevel@tonic-gate if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) 10640Sstevel@tonic-gate s->session->session_id_length=0; 10650Sstevel@tonic-gate 10660Sstevel@tonic-gate sl=s->session->session_id_length; 10672139Sjp161948 if (sl > (int)sizeof(s->session->session_id)) 10680Sstevel@tonic-gate { 10690Sstevel@tonic-gate SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR); 10700Sstevel@tonic-gate return -1; 10710Sstevel@tonic-gate } 10720Sstevel@tonic-gate *(p++)=sl; 10730Sstevel@tonic-gate memcpy(p,s->session->session_id,sl); 10740Sstevel@tonic-gate p+=sl; 10750Sstevel@tonic-gate 10760Sstevel@tonic-gate /* put the cipher */ 10770Sstevel@tonic-gate i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p); 10780Sstevel@tonic-gate p+=i; 10790Sstevel@tonic-gate 10800Sstevel@tonic-gate /* put the compression method */ 10812139Sjp161948 #ifdef OPENSSL_NO_COMP 10822139Sjp161948 *(p++)=0; 10832139Sjp161948 #else 10840Sstevel@tonic-gate if (s->s3->tmp.new_compression == NULL) 10850Sstevel@tonic-gate *(p++)=0; 10860Sstevel@tonic-gate else 10870Sstevel@tonic-gate *(p++)=s->s3->tmp.new_compression->id; 10882139Sjp161948 #endif 10890Sstevel@tonic-gate 10900Sstevel@tonic-gate /* do the header */ 10910Sstevel@tonic-gate l=(p-d); 10920Sstevel@tonic-gate d=buf; 10930Sstevel@tonic-gate *(d++)=SSL3_MT_SERVER_HELLO; 10940Sstevel@tonic-gate l2n3(l,d); 10950Sstevel@tonic-gate 10960Sstevel@tonic-gate s->state=SSL3_ST_CW_CLNT_HELLO_B; 10970Sstevel@tonic-gate /* number of bytes to write */ 10980Sstevel@tonic-gate s->init_num=p-buf; 10990Sstevel@tonic-gate s->init_off=0; 11000Sstevel@tonic-gate } 11010Sstevel@tonic-gate 11020Sstevel@tonic-gate /* SSL3_ST_CW_CLNT_HELLO_B */ 11030Sstevel@tonic-gate return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 11040Sstevel@tonic-gate } 11050Sstevel@tonic-gate 11062139Sjp161948 int ssl3_send_server_done(SSL *s) 11070Sstevel@tonic-gate { 11080Sstevel@tonic-gate unsigned char *p; 11090Sstevel@tonic-gate 11100Sstevel@tonic-gate if (s->state == SSL3_ST_SW_SRVR_DONE_A) 11110Sstevel@tonic-gate { 11120Sstevel@tonic-gate p=(unsigned char *)s->init_buf->data; 11130Sstevel@tonic-gate 11140Sstevel@tonic-gate /* do the header */ 11150Sstevel@tonic-gate *(p++)=SSL3_MT_SERVER_DONE; 11160Sstevel@tonic-gate *(p++)=0; 11170Sstevel@tonic-gate *(p++)=0; 11180Sstevel@tonic-gate *(p++)=0; 11190Sstevel@tonic-gate 11200Sstevel@tonic-gate s->state=SSL3_ST_SW_SRVR_DONE_B; 11210Sstevel@tonic-gate /* number of bytes to write */ 11220Sstevel@tonic-gate s->init_num=4; 11230Sstevel@tonic-gate s->init_off=0; 11240Sstevel@tonic-gate } 11250Sstevel@tonic-gate 11260Sstevel@tonic-gate /* SSL3_ST_CW_CLNT_HELLO_B */ 11270Sstevel@tonic-gate return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 11280Sstevel@tonic-gate } 11290Sstevel@tonic-gate 11302139Sjp161948 int ssl3_send_server_key_exchange(SSL *s) 11310Sstevel@tonic-gate { 11320Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA 11330Sstevel@tonic-gate unsigned char *q; 11340Sstevel@tonic-gate int j,num; 11350Sstevel@tonic-gate RSA *rsa; 11360Sstevel@tonic-gate unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH]; 11370Sstevel@tonic-gate unsigned int u; 11380Sstevel@tonic-gate #endif 11390Sstevel@tonic-gate #ifndef OPENSSL_NO_DH 11400Sstevel@tonic-gate DH *dh=NULL,*dhp; 11410Sstevel@tonic-gate #endif 11422139Sjp161948 #ifndef OPENSSL_NO_ECDH 11432139Sjp161948 EC_KEY *ecdh=NULL, *ecdhp; 11442139Sjp161948 unsigned char *encodedPoint = NULL; 11452139Sjp161948 int encodedlen = 0; 11462139Sjp161948 int curve_id = 0; 11472139Sjp161948 BN_CTX *bn_ctx = NULL; 11482139Sjp161948 #endif 11490Sstevel@tonic-gate EVP_PKEY *pkey; 11500Sstevel@tonic-gate unsigned char *p,*d; 11510Sstevel@tonic-gate int al,i; 11520Sstevel@tonic-gate unsigned long type; 11530Sstevel@tonic-gate int n; 11540Sstevel@tonic-gate CERT *cert; 11550Sstevel@tonic-gate BIGNUM *r[4]; 11560Sstevel@tonic-gate int nr[4],kn; 11570Sstevel@tonic-gate BUF_MEM *buf; 11580Sstevel@tonic-gate EVP_MD_CTX md_ctx; 11590Sstevel@tonic-gate 11600Sstevel@tonic-gate EVP_MD_CTX_init(&md_ctx); 11610Sstevel@tonic-gate if (s->state == SSL3_ST_SW_KEY_EXCH_A) 11620Sstevel@tonic-gate { 11630Sstevel@tonic-gate type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK; 11640Sstevel@tonic-gate cert=s->cert; 11650Sstevel@tonic-gate 11660Sstevel@tonic-gate buf=s->init_buf; 11670Sstevel@tonic-gate 11680Sstevel@tonic-gate r[0]=r[1]=r[2]=r[3]=NULL; 11690Sstevel@tonic-gate n=0; 11700Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA 11710Sstevel@tonic-gate if (type & SSL_kRSA) 11720Sstevel@tonic-gate { 11730Sstevel@tonic-gate rsa=cert->rsa_tmp; 11740Sstevel@tonic-gate if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) 11750Sstevel@tonic-gate { 11760Sstevel@tonic-gate rsa=s->cert->rsa_tmp_cb(s, 11770Sstevel@tonic-gate SSL_C_IS_EXPORT(s->s3->tmp.new_cipher), 11780Sstevel@tonic-gate SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)); 11790Sstevel@tonic-gate if(rsa == NULL) 11800Sstevel@tonic-gate { 11810Sstevel@tonic-gate al=SSL_AD_HANDSHAKE_FAILURE; 11820Sstevel@tonic-gate SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY); 11830Sstevel@tonic-gate goto f_err; 11840Sstevel@tonic-gate } 11850Sstevel@tonic-gate RSA_up_ref(rsa); 11860Sstevel@tonic-gate cert->rsa_tmp=rsa; 11870Sstevel@tonic-gate } 11880Sstevel@tonic-gate if (rsa == NULL) 11890Sstevel@tonic-gate { 11900Sstevel@tonic-gate al=SSL_AD_HANDSHAKE_FAILURE; 11910Sstevel@tonic-gate SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY); 11920Sstevel@tonic-gate goto f_err; 11930Sstevel@tonic-gate } 11940Sstevel@tonic-gate r[0]=rsa->n; 11950Sstevel@tonic-gate r[1]=rsa->e; 11960Sstevel@tonic-gate s->s3->tmp.use_rsa_tmp=1; 11970Sstevel@tonic-gate } 11980Sstevel@tonic-gate else 11990Sstevel@tonic-gate #endif 12000Sstevel@tonic-gate #ifndef OPENSSL_NO_DH 12010Sstevel@tonic-gate if (type & SSL_kEDH) 12020Sstevel@tonic-gate { 12030Sstevel@tonic-gate dhp=cert->dh_tmp; 12040Sstevel@tonic-gate if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL)) 12050Sstevel@tonic-gate dhp=s->cert->dh_tmp_cb(s, 12060Sstevel@tonic-gate SSL_C_IS_EXPORT(s->s3->tmp.new_cipher), 12070Sstevel@tonic-gate SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)); 12080Sstevel@tonic-gate if (dhp == NULL) 12090Sstevel@tonic-gate { 12100Sstevel@tonic-gate al=SSL_AD_HANDSHAKE_FAILURE; 12110Sstevel@tonic-gate SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY); 12120Sstevel@tonic-gate goto f_err; 12130Sstevel@tonic-gate } 12140Sstevel@tonic-gate 12150Sstevel@tonic-gate if (s->s3->tmp.dh != NULL) 12160Sstevel@tonic-gate { 12170Sstevel@tonic-gate DH_free(dh); 12180Sstevel@tonic-gate SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); 12190Sstevel@tonic-gate goto err; 12200Sstevel@tonic-gate } 12210Sstevel@tonic-gate 12220Sstevel@tonic-gate if ((dh=DHparams_dup(dhp)) == NULL) 12230Sstevel@tonic-gate { 12240Sstevel@tonic-gate SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB); 12250Sstevel@tonic-gate goto err; 12260Sstevel@tonic-gate } 12270Sstevel@tonic-gate 12280Sstevel@tonic-gate s->s3->tmp.dh=dh; 12290Sstevel@tonic-gate if ((dhp->pub_key == NULL || 12300Sstevel@tonic-gate dhp->priv_key == NULL || 12310Sstevel@tonic-gate (s->options & SSL_OP_SINGLE_DH_USE))) 12320Sstevel@tonic-gate { 12330Sstevel@tonic-gate if(!DH_generate_key(dh)) 12340Sstevel@tonic-gate { 12350Sstevel@tonic-gate SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, 12360Sstevel@tonic-gate ERR_R_DH_LIB); 12370Sstevel@tonic-gate goto err; 12380Sstevel@tonic-gate } 12390Sstevel@tonic-gate } 12400Sstevel@tonic-gate else 12410Sstevel@tonic-gate { 12420Sstevel@tonic-gate dh->pub_key=BN_dup(dhp->pub_key); 12430Sstevel@tonic-gate dh->priv_key=BN_dup(dhp->priv_key); 12440Sstevel@tonic-gate if ((dh->pub_key == NULL) || 12450Sstevel@tonic-gate (dh->priv_key == NULL)) 12460Sstevel@tonic-gate { 12470Sstevel@tonic-gate SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB); 12480Sstevel@tonic-gate goto err; 12490Sstevel@tonic-gate } 12500Sstevel@tonic-gate } 12510Sstevel@tonic-gate r[0]=dh->p; 12520Sstevel@tonic-gate r[1]=dh->g; 12530Sstevel@tonic-gate r[2]=dh->pub_key; 12540Sstevel@tonic-gate } 12550Sstevel@tonic-gate else 12560Sstevel@tonic-gate #endif 12572139Sjp161948 #ifndef OPENSSL_NO_ECDH 12582139Sjp161948 if (type & SSL_kECDHE) 12592139Sjp161948 { 12602139Sjp161948 const EC_GROUP *group; 12612139Sjp161948 12622139Sjp161948 ecdhp=cert->ecdh_tmp; 12632139Sjp161948 if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL)) 12642139Sjp161948 { 12652139Sjp161948 ecdhp=s->cert->ecdh_tmp_cb(s, 12662139Sjp161948 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher), 12672139Sjp161948 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)); 12682139Sjp161948 } 12692139Sjp161948 if (ecdhp == NULL) 12702139Sjp161948 { 12712139Sjp161948 al=SSL_AD_HANDSHAKE_FAILURE; 12722139Sjp161948 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY); 12732139Sjp161948 goto f_err; 12742139Sjp161948 } 12752139Sjp161948 12762139Sjp161948 if (s->s3->tmp.ecdh != NULL) 12772139Sjp161948 { 12782139Sjp161948 EC_KEY_free(s->s3->tmp.ecdh); 12792139Sjp161948 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); 12802139Sjp161948 goto err; 12812139Sjp161948 } 12822139Sjp161948 12832139Sjp161948 /* Duplicate the ECDH structure. */ 12842139Sjp161948 if (ecdhp == NULL) 12852139Sjp161948 { 12862139Sjp161948 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB); 12872139Sjp161948 goto err; 12882139Sjp161948 } 12892139Sjp161948 if (!EC_KEY_up_ref(ecdhp)) 12902139Sjp161948 { 12912139Sjp161948 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB); 12922139Sjp161948 goto err; 12932139Sjp161948 } 12942139Sjp161948 ecdh = ecdhp; 12952139Sjp161948 12962139Sjp161948 s->s3->tmp.ecdh=ecdh; 12972139Sjp161948 if ((EC_KEY_get0_public_key(ecdh) == NULL) || 12982139Sjp161948 (EC_KEY_get0_private_key(ecdh) == NULL) || 12992139Sjp161948 (s->options & SSL_OP_SINGLE_ECDH_USE)) 13002139Sjp161948 { 13012139Sjp161948 if(!EC_KEY_generate_key(ecdh)) 13022139Sjp161948 { 13032139Sjp161948 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB); 13042139Sjp161948 goto err; 13052139Sjp161948 } 13062139Sjp161948 } 13072139Sjp161948 13082139Sjp161948 if (((group = EC_KEY_get0_group(ecdh)) == NULL) || 13092139Sjp161948 (EC_KEY_get0_public_key(ecdh) == NULL) || 13102139Sjp161948 (EC_KEY_get0_private_key(ecdh) == NULL)) 13112139Sjp161948 { 13122139Sjp161948 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB); 13132139Sjp161948 goto err; 13142139Sjp161948 } 13152139Sjp161948 13162139Sjp161948 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && 13172139Sjp161948 (EC_GROUP_get_degree(group) > 163)) 13182139Sjp161948 { 13192139Sjp161948 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER); 13202139Sjp161948 goto err; 13212139Sjp161948 } 13222139Sjp161948 13232139Sjp161948 /* XXX: For now, we only support ephemeral ECDH 13242139Sjp161948 * keys over named (not generic) curves. For 13252139Sjp161948 * supported named curves, curve_id is non-zero. 13262139Sjp161948 */ 13272139Sjp161948 if ((curve_id = 13282139Sjp161948 nid2curve_id(EC_GROUP_get_curve_name(group))) 13292139Sjp161948 == 0) 13302139Sjp161948 { 13312139Sjp161948 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); 13322139Sjp161948 goto err; 13332139Sjp161948 } 13342139Sjp161948 13352139Sjp161948 /* Encode the public key. 13362139Sjp161948 * First check the size of encoding and 13372139Sjp161948 * allocate memory accordingly. 13382139Sjp161948 */ 13392139Sjp161948 encodedlen = EC_POINT_point2oct(group, 13402139Sjp161948 EC_KEY_get0_public_key(ecdh), 13412139Sjp161948 POINT_CONVERSION_UNCOMPRESSED, 13422139Sjp161948 NULL, 0, NULL); 13432139Sjp161948 13442139Sjp161948 encodedPoint = (unsigned char *) 13452139Sjp161948 OPENSSL_malloc(encodedlen*sizeof(unsigned char)); 13462139Sjp161948 bn_ctx = BN_CTX_new(); 13472139Sjp161948 if ((encodedPoint == NULL) || (bn_ctx == NULL)) 13482139Sjp161948 { 13492139Sjp161948 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE); 13502139Sjp161948 goto err; 13512139Sjp161948 } 13522139Sjp161948 13532139Sjp161948 13542139Sjp161948 encodedlen = EC_POINT_point2oct(group, 13552139Sjp161948 EC_KEY_get0_public_key(ecdh), 13562139Sjp161948 POINT_CONVERSION_UNCOMPRESSED, 13572139Sjp161948 encodedPoint, encodedlen, bn_ctx); 13582139Sjp161948 13592139Sjp161948 if (encodedlen == 0) 13602139Sjp161948 { 13612139Sjp161948 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB); 13622139Sjp161948 goto err; 13632139Sjp161948 } 13642139Sjp161948 13652139Sjp161948 BN_CTX_free(bn_ctx); bn_ctx=NULL; 13662139Sjp161948 13672139Sjp161948 /* XXX: For now, we only support named (not 13682139Sjp161948 * generic) curves in ECDH ephemeral key exchanges. 13692139Sjp161948 * In this situation, we need three additional bytes 13702139Sjp161948 * to encode the entire ServerECDHParams 13712139Sjp161948 * structure. 13722139Sjp161948 */ 13732139Sjp161948 n = 3 + encodedlen; 13742139Sjp161948 13752139Sjp161948 /* We'll generate the serverKeyExchange message 13762139Sjp161948 * explicitly so we can set these to NULLs 13772139Sjp161948 */ 13782139Sjp161948 r[0]=NULL; 13792139Sjp161948 r[1]=NULL; 13802139Sjp161948 r[2]=NULL; 13812139Sjp161948 } 13822139Sjp161948 else 13832139Sjp161948 #endif /* !OPENSSL_NO_ECDH */ 13840Sstevel@tonic-gate { 13850Sstevel@tonic-gate al=SSL_AD_HANDSHAKE_FAILURE; 13860Sstevel@tonic-gate SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); 13870Sstevel@tonic-gate goto f_err; 13880Sstevel@tonic-gate } 13890Sstevel@tonic-gate for (i=0; r[i] != NULL; i++) 13900Sstevel@tonic-gate { 13910Sstevel@tonic-gate nr[i]=BN_num_bytes(r[i]); 13920Sstevel@tonic-gate n+=2+nr[i]; 13930Sstevel@tonic-gate } 13940Sstevel@tonic-gate 13950Sstevel@tonic-gate if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)) 13960Sstevel@tonic-gate { 13970Sstevel@tonic-gate if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher)) 13980Sstevel@tonic-gate == NULL) 13990Sstevel@tonic-gate { 14000Sstevel@tonic-gate al=SSL_AD_DECODE_ERROR; 14010Sstevel@tonic-gate goto f_err; 14020Sstevel@tonic-gate } 14030Sstevel@tonic-gate kn=EVP_PKEY_size(pkey); 14040Sstevel@tonic-gate } 14050Sstevel@tonic-gate else 14060Sstevel@tonic-gate { 14070Sstevel@tonic-gate pkey=NULL; 14080Sstevel@tonic-gate kn=0; 14090Sstevel@tonic-gate } 14100Sstevel@tonic-gate 14110Sstevel@tonic-gate if (!BUF_MEM_grow_clean(buf,n+4+kn)) 14120Sstevel@tonic-gate { 14130Sstevel@tonic-gate SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF); 14140Sstevel@tonic-gate goto err; 14150Sstevel@tonic-gate } 14160Sstevel@tonic-gate d=(unsigned char *)s->init_buf->data; 14170Sstevel@tonic-gate p= &(d[4]); 14180Sstevel@tonic-gate 14190Sstevel@tonic-gate for (i=0; r[i] != NULL; i++) 14200Sstevel@tonic-gate { 14210Sstevel@tonic-gate s2n(nr[i],p); 14220Sstevel@tonic-gate BN_bn2bin(r[i],p); 14230Sstevel@tonic-gate p+=nr[i]; 14240Sstevel@tonic-gate } 14250Sstevel@tonic-gate 14262139Sjp161948 #ifndef OPENSSL_NO_ECDH 14272139Sjp161948 if (type & SSL_kECDHE) 14282139Sjp161948 { 14292139Sjp161948 /* XXX: For now, we only support named (not generic) curves. 14302139Sjp161948 * In this situation, the serverKeyExchange message has: 14312139Sjp161948 * [1 byte CurveType], [1 byte CurveName] 14322139Sjp161948 * [1 byte length of encoded point], followed by 14332139Sjp161948 * the actual encoded point itself 14342139Sjp161948 */ 14352139Sjp161948 *p = NAMED_CURVE_TYPE; 14362139Sjp161948 p += 1; 14372139Sjp161948 *p = curve_id; 14382139Sjp161948 p += 1; 14392139Sjp161948 *p = encodedlen; 14402139Sjp161948 p += 1; 14412139Sjp161948 memcpy((unsigned char*)p, 14422139Sjp161948 (unsigned char *)encodedPoint, 14432139Sjp161948 encodedlen); 14442139Sjp161948 OPENSSL_free(encodedPoint); 14452139Sjp161948 p += encodedlen; 14462139Sjp161948 } 14472139Sjp161948 #endif 14482139Sjp161948 14490Sstevel@tonic-gate /* not anonymous */ 14500Sstevel@tonic-gate if (pkey != NULL) 14510Sstevel@tonic-gate { 14520Sstevel@tonic-gate /* n is the length of the params, they start at &(d[4]) 14530Sstevel@tonic-gate * and p points to the space at the end. */ 14540Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA 14550Sstevel@tonic-gate if (pkey->type == EVP_PKEY_RSA) 14560Sstevel@tonic-gate { 14570Sstevel@tonic-gate q=md_buf; 14580Sstevel@tonic-gate j=0; 14590Sstevel@tonic-gate for (num=2; num > 0; num--) 14600Sstevel@tonic-gate { 14610Sstevel@tonic-gate EVP_DigestInit_ex(&md_ctx,(num == 2) 14620Sstevel@tonic-gate ?s->ctx->md5:s->ctx->sha1, NULL); 14630Sstevel@tonic-gate EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); 14640Sstevel@tonic-gate EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); 14650Sstevel@tonic-gate EVP_DigestUpdate(&md_ctx,&(d[4]),n); 14660Sstevel@tonic-gate EVP_DigestFinal_ex(&md_ctx,q, 14670Sstevel@tonic-gate (unsigned int *)&i); 14680Sstevel@tonic-gate q+=i; 14690Sstevel@tonic-gate j+=i; 14700Sstevel@tonic-gate } 14710Sstevel@tonic-gate if (RSA_sign(NID_md5_sha1, md_buf, j, 14720Sstevel@tonic-gate &(p[2]), &u, pkey->pkey.rsa) <= 0) 14730Sstevel@tonic-gate { 14740Sstevel@tonic-gate SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA); 14750Sstevel@tonic-gate goto err; 14760Sstevel@tonic-gate } 14770Sstevel@tonic-gate s2n(u,p); 14780Sstevel@tonic-gate n+=u+2; 14790Sstevel@tonic-gate } 14800Sstevel@tonic-gate else 14810Sstevel@tonic-gate #endif 14820Sstevel@tonic-gate #if !defined(OPENSSL_NO_DSA) 14830Sstevel@tonic-gate if (pkey->type == EVP_PKEY_DSA) 14840Sstevel@tonic-gate { 14850Sstevel@tonic-gate /* lets do DSS */ 14860Sstevel@tonic-gate EVP_SignInit_ex(&md_ctx,EVP_dss1(), NULL); 14870Sstevel@tonic-gate EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); 14880Sstevel@tonic-gate EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); 14890Sstevel@tonic-gate EVP_SignUpdate(&md_ctx,&(d[4]),n); 14900Sstevel@tonic-gate if (!EVP_SignFinal(&md_ctx,&(p[2]), 14910Sstevel@tonic-gate (unsigned int *)&i,pkey)) 14920Sstevel@tonic-gate { 14930Sstevel@tonic-gate SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA); 14940Sstevel@tonic-gate goto err; 14950Sstevel@tonic-gate } 14960Sstevel@tonic-gate s2n(i,p); 14970Sstevel@tonic-gate n+=i+2; 14980Sstevel@tonic-gate } 14990Sstevel@tonic-gate else 15000Sstevel@tonic-gate #endif 15012139Sjp161948 #if !defined(OPENSSL_NO_ECDSA) 15022139Sjp161948 if (pkey->type == EVP_PKEY_EC) 15032139Sjp161948 { 15042139Sjp161948 /* let's do ECDSA */ 15052139Sjp161948 EVP_SignInit_ex(&md_ctx,EVP_ecdsa(), NULL); 15062139Sjp161948 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); 15072139Sjp161948 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); 15082139Sjp161948 EVP_SignUpdate(&md_ctx,&(d[4]),n); 15092139Sjp161948 if (!EVP_SignFinal(&md_ctx,&(p[2]), 15102139Sjp161948 (unsigned int *)&i,pkey)) 15112139Sjp161948 { 15122139Sjp161948 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_ECDSA); 15132139Sjp161948 goto err; 15142139Sjp161948 } 15152139Sjp161948 s2n(i,p); 15162139Sjp161948 n+=i+2; 15172139Sjp161948 } 15182139Sjp161948 else 15192139Sjp161948 #endif 15200Sstevel@tonic-gate { 15210Sstevel@tonic-gate /* Is this error check actually needed? */ 15220Sstevel@tonic-gate al=SSL_AD_HANDSHAKE_FAILURE; 15230Sstevel@tonic-gate SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE); 15240Sstevel@tonic-gate goto f_err; 15250Sstevel@tonic-gate } 15260Sstevel@tonic-gate } 15270Sstevel@tonic-gate 15280Sstevel@tonic-gate *(d++)=SSL3_MT_SERVER_KEY_EXCHANGE; 15290Sstevel@tonic-gate l2n3(n,d); 15300Sstevel@tonic-gate 15310Sstevel@tonic-gate /* we should now have things packed up, so lets send 15320Sstevel@tonic-gate * it off */ 15330Sstevel@tonic-gate s->init_num=n+4; 15340Sstevel@tonic-gate s->init_off=0; 15350Sstevel@tonic-gate } 15360Sstevel@tonic-gate 15370Sstevel@tonic-gate s->state = SSL3_ST_SW_KEY_EXCH_B; 15380Sstevel@tonic-gate EVP_MD_CTX_cleanup(&md_ctx); 15390Sstevel@tonic-gate return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 15400Sstevel@tonic-gate f_err: 15410Sstevel@tonic-gate ssl3_send_alert(s,SSL3_AL_FATAL,al); 15420Sstevel@tonic-gate err: 15432139Sjp161948 #ifndef OPENSSL_NO_ECDH 15442139Sjp161948 if (encodedPoint != NULL) OPENSSL_free(encodedPoint); 15452139Sjp161948 BN_CTX_free(bn_ctx); 15462139Sjp161948 #endif 15470Sstevel@tonic-gate EVP_MD_CTX_cleanup(&md_ctx); 15480Sstevel@tonic-gate return(-1); 15490Sstevel@tonic-gate } 15500Sstevel@tonic-gate 15512139Sjp161948 int ssl3_send_certificate_request(SSL *s) 15520Sstevel@tonic-gate { 15530Sstevel@tonic-gate unsigned char *p,*d; 15540Sstevel@tonic-gate int i,j,nl,off,n; 15550Sstevel@tonic-gate STACK_OF(X509_NAME) *sk=NULL; 15560Sstevel@tonic-gate X509_NAME *name; 15570Sstevel@tonic-gate BUF_MEM *buf; 15580Sstevel@tonic-gate 15590Sstevel@tonic-gate if (s->state == SSL3_ST_SW_CERT_REQ_A) 15600Sstevel@tonic-gate { 15610Sstevel@tonic-gate buf=s->init_buf; 15620Sstevel@tonic-gate 15630Sstevel@tonic-gate d=p=(unsigned char *)&(buf->data[4]); 15640Sstevel@tonic-gate 15650Sstevel@tonic-gate /* get the list of acceptable cert types */ 15660Sstevel@tonic-gate p++; 15670Sstevel@tonic-gate n=ssl3_get_req_cert_type(s,p); 15680Sstevel@tonic-gate d[0]=n; 15690Sstevel@tonic-gate p+=n; 15700Sstevel@tonic-gate n++; 15710Sstevel@tonic-gate 15720Sstevel@tonic-gate off=n; 15730Sstevel@tonic-gate p+=2; 15740Sstevel@tonic-gate n+=2; 15750Sstevel@tonic-gate 15760Sstevel@tonic-gate sk=SSL_get_client_CA_list(s); 15770Sstevel@tonic-gate nl=0; 15780Sstevel@tonic-gate if (sk != NULL) 15790Sstevel@tonic-gate { 15800Sstevel@tonic-gate for (i=0; i<sk_X509_NAME_num(sk); i++) 15810Sstevel@tonic-gate { 15820Sstevel@tonic-gate name=sk_X509_NAME_value(sk,i); 15830Sstevel@tonic-gate j=i2d_X509_NAME(name,NULL); 15840Sstevel@tonic-gate if (!BUF_MEM_grow_clean(buf,4+n+j+2)) 15850Sstevel@tonic-gate { 15860Sstevel@tonic-gate SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB); 15870Sstevel@tonic-gate goto err; 15880Sstevel@tonic-gate } 15890Sstevel@tonic-gate p=(unsigned char *)&(buf->data[4+n]); 15900Sstevel@tonic-gate if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) 15910Sstevel@tonic-gate { 15920Sstevel@tonic-gate s2n(j,p); 15930Sstevel@tonic-gate i2d_X509_NAME(name,&p); 15940Sstevel@tonic-gate n+=2+j; 15950Sstevel@tonic-gate nl+=2+j; 15960Sstevel@tonic-gate } 15970Sstevel@tonic-gate else 15980Sstevel@tonic-gate { 15990Sstevel@tonic-gate d=p; 16000Sstevel@tonic-gate i2d_X509_NAME(name,&p); 16010Sstevel@tonic-gate j-=2; s2n(j,d); j+=2; 16020Sstevel@tonic-gate n+=j; 16030Sstevel@tonic-gate nl+=j; 16040Sstevel@tonic-gate } 16050Sstevel@tonic-gate } 16060Sstevel@tonic-gate } 16070Sstevel@tonic-gate /* else no CA names */ 16080Sstevel@tonic-gate p=(unsigned char *)&(buf->data[4+off]); 16090Sstevel@tonic-gate s2n(nl,p); 16100Sstevel@tonic-gate 16110Sstevel@tonic-gate d=(unsigned char *)buf->data; 16120Sstevel@tonic-gate *(d++)=SSL3_MT_CERTIFICATE_REQUEST; 16130Sstevel@tonic-gate l2n3(n,d); 16140Sstevel@tonic-gate 16150Sstevel@tonic-gate /* we should now have things packed up, so lets send 16160Sstevel@tonic-gate * it off */ 16170Sstevel@tonic-gate 16180Sstevel@tonic-gate s->init_num=n+4; 16190Sstevel@tonic-gate s->init_off=0; 16200Sstevel@tonic-gate #ifdef NETSCAPE_HANG_BUG 16210Sstevel@tonic-gate p=(unsigned char *)s->init_buf->data + s->init_num; 16220Sstevel@tonic-gate 16230Sstevel@tonic-gate /* do the header */ 16240Sstevel@tonic-gate *(p++)=SSL3_MT_SERVER_DONE; 16250Sstevel@tonic-gate *(p++)=0; 16260Sstevel@tonic-gate *(p++)=0; 16270Sstevel@tonic-gate *(p++)=0; 16280Sstevel@tonic-gate s->init_num += 4; 16290Sstevel@tonic-gate #endif 16300Sstevel@tonic-gate 16310Sstevel@tonic-gate s->state = SSL3_ST_SW_CERT_REQ_B; 16320Sstevel@tonic-gate } 16330Sstevel@tonic-gate 16340Sstevel@tonic-gate /* SSL3_ST_SW_CERT_REQ_B */ 16350Sstevel@tonic-gate return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 16360Sstevel@tonic-gate err: 16370Sstevel@tonic-gate return(-1); 16380Sstevel@tonic-gate } 16390Sstevel@tonic-gate 16402139Sjp161948 16412139Sjp161948 #ifndef OPENSSL_NO_ECDH 16422139Sjp161948 static const int KDF1_SHA1_len = 20; 16432139Sjp161948 static void *KDF1_SHA1(const void *in, size_t inlen, void *out, size_t *outlen) 16442139Sjp161948 { 16452139Sjp161948 #ifndef OPENSSL_NO_SHA 16462139Sjp161948 if (*outlen < SHA_DIGEST_LENGTH) 16472139Sjp161948 return NULL; 16482139Sjp161948 else 16492139Sjp161948 *outlen = SHA_DIGEST_LENGTH; 16502139Sjp161948 return SHA1(in, inlen, out); 16512139Sjp161948 #else 16522139Sjp161948 return NULL; 16532139Sjp161948 #endif /* OPENSSL_NO_SHA */ 16542139Sjp161948 } 16552139Sjp161948 #endif /* OPENSSL_NO_ECDH */ 16562139Sjp161948 16572139Sjp161948 int ssl3_get_client_key_exchange(SSL *s) 16580Sstevel@tonic-gate { 16590Sstevel@tonic-gate int i,al,ok; 16600Sstevel@tonic-gate long n; 16610Sstevel@tonic-gate unsigned long l; 16620Sstevel@tonic-gate unsigned char *p; 16630Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA 16640Sstevel@tonic-gate RSA *rsa=NULL; 16650Sstevel@tonic-gate EVP_PKEY *pkey=NULL; 16660Sstevel@tonic-gate #endif 16670Sstevel@tonic-gate #ifndef OPENSSL_NO_DH 16680Sstevel@tonic-gate BIGNUM *pub=NULL; 16690Sstevel@tonic-gate DH *dh_srvr; 16700Sstevel@tonic-gate #endif 16710Sstevel@tonic-gate #ifndef OPENSSL_NO_KRB5 16720Sstevel@tonic-gate KSSL_ERR kssl_err; 16730Sstevel@tonic-gate #endif /* OPENSSL_NO_KRB5 */ 16740Sstevel@tonic-gate 16752139Sjp161948 #ifndef OPENSSL_NO_ECDH 16762139Sjp161948 EC_KEY *srvr_ecdh = NULL; 16772139Sjp161948 EVP_PKEY *clnt_pub_pkey = NULL; 16782139Sjp161948 EC_POINT *clnt_ecpoint = NULL; 16792139Sjp161948 BN_CTX *bn_ctx = NULL; 16802139Sjp161948 #endif 16812139Sjp161948 16822139Sjp161948 n=s->method->ssl_get_message(s, 16830Sstevel@tonic-gate SSL3_ST_SR_KEY_EXCH_A, 16840Sstevel@tonic-gate SSL3_ST_SR_KEY_EXCH_B, 16850Sstevel@tonic-gate SSL3_MT_CLIENT_KEY_EXCHANGE, 16860Sstevel@tonic-gate 2048, /* ??? */ 16870Sstevel@tonic-gate &ok); 16880Sstevel@tonic-gate 16890Sstevel@tonic-gate if (!ok) return((int)n); 16900Sstevel@tonic-gate p=(unsigned char *)s->init_msg; 16910Sstevel@tonic-gate 16920Sstevel@tonic-gate l=s->s3->tmp.new_cipher->algorithms; 16930Sstevel@tonic-gate 16940Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA 16950Sstevel@tonic-gate if (l & SSL_kRSA) 16960Sstevel@tonic-gate { 16970Sstevel@tonic-gate /* FIX THIS UP EAY EAY EAY EAY */ 16980Sstevel@tonic-gate if (s->s3->tmp.use_rsa_tmp) 16990Sstevel@tonic-gate { 17000Sstevel@tonic-gate if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL)) 17010Sstevel@tonic-gate rsa=s->cert->rsa_tmp; 17020Sstevel@tonic-gate /* Don't do a callback because rsa_tmp should 17030Sstevel@tonic-gate * be sent already */ 17040Sstevel@tonic-gate if (rsa == NULL) 17050Sstevel@tonic-gate { 17060Sstevel@tonic-gate al=SSL_AD_HANDSHAKE_FAILURE; 17070Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY); 17080Sstevel@tonic-gate goto f_err; 17090Sstevel@tonic-gate 17100Sstevel@tonic-gate } 17110Sstevel@tonic-gate } 17120Sstevel@tonic-gate else 17130Sstevel@tonic-gate { 17140Sstevel@tonic-gate pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey; 17150Sstevel@tonic-gate if ( (pkey == NULL) || 17160Sstevel@tonic-gate (pkey->type != EVP_PKEY_RSA) || 17170Sstevel@tonic-gate (pkey->pkey.rsa == NULL)) 17180Sstevel@tonic-gate { 17190Sstevel@tonic-gate al=SSL_AD_HANDSHAKE_FAILURE; 17200Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE); 17210Sstevel@tonic-gate goto f_err; 17220Sstevel@tonic-gate } 17230Sstevel@tonic-gate rsa=pkey->pkey.rsa; 17240Sstevel@tonic-gate } 17250Sstevel@tonic-gate 17260Sstevel@tonic-gate /* TLS */ 17270Sstevel@tonic-gate if (s->version > SSL3_VERSION) 17280Sstevel@tonic-gate { 17290Sstevel@tonic-gate n2s(p,i); 17300Sstevel@tonic-gate if (n != i+2) 17310Sstevel@tonic-gate { 17320Sstevel@tonic-gate if (!(s->options & SSL_OP_TLS_D5_BUG)) 17330Sstevel@tonic-gate { 17340Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG); 17350Sstevel@tonic-gate goto err; 17360Sstevel@tonic-gate } 17370Sstevel@tonic-gate else 17380Sstevel@tonic-gate p-=2; 17390Sstevel@tonic-gate } 17400Sstevel@tonic-gate else 17410Sstevel@tonic-gate n=i; 17420Sstevel@tonic-gate } 17430Sstevel@tonic-gate 17440Sstevel@tonic-gate i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING); 17450Sstevel@tonic-gate 17460Sstevel@tonic-gate al = -1; 17470Sstevel@tonic-gate 17480Sstevel@tonic-gate if (i != SSL_MAX_MASTER_KEY_LENGTH) 17490Sstevel@tonic-gate { 17500Sstevel@tonic-gate al=SSL_AD_DECODE_ERROR; 17510Sstevel@tonic-gate /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT); */ 17520Sstevel@tonic-gate } 17530Sstevel@tonic-gate 17540Sstevel@tonic-gate if ((al == -1) && !((p[0] == (s->client_version>>8)) && (p[1] == (s->client_version & 0xff)))) 17550Sstevel@tonic-gate { 17560Sstevel@tonic-gate /* The premaster secret must contain the same version number as the 17570Sstevel@tonic-gate * ClientHello to detect version rollback attacks (strangely, the 17580Sstevel@tonic-gate * protocol does not offer such protection for DH ciphersuites). 17590Sstevel@tonic-gate * However, buggy clients exist that send the negotiated protocol 17600Sstevel@tonic-gate * version instead if the server does not support the requested 17610Sstevel@tonic-gate * protocol version. 17620Sstevel@tonic-gate * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */ 17630Sstevel@tonic-gate if (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) && 17640Sstevel@tonic-gate (p[0] == (s->version>>8)) && (p[1] == (s->version & 0xff)))) 17650Sstevel@tonic-gate { 17660Sstevel@tonic-gate al=SSL_AD_DECODE_ERROR; 17670Sstevel@tonic-gate /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER); */ 17680Sstevel@tonic-gate 17690Sstevel@tonic-gate /* The Klima-Pokorny-Rosa extension of Bleichenbacher's attack 17700Sstevel@tonic-gate * (http://eprint.iacr.org/2003/052/) exploits the version 17710Sstevel@tonic-gate * number check as a "bad version oracle" -- an alert would 17720Sstevel@tonic-gate * reveal that the plaintext corresponding to some ciphertext 17730Sstevel@tonic-gate * made up by the adversary is properly formatted except 17740Sstevel@tonic-gate * that the version number is wrong. To avoid such attacks, 17750Sstevel@tonic-gate * we should treat this just like any other decryption error. */ 17760Sstevel@tonic-gate } 17770Sstevel@tonic-gate } 17780Sstevel@tonic-gate 17790Sstevel@tonic-gate if (al != -1) 17800Sstevel@tonic-gate { 17810Sstevel@tonic-gate /* Some decryption failure -- use random value instead as countermeasure 17820Sstevel@tonic-gate * against Bleichenbacher's attack on PKCS #1 v1.5 RSA padding 17830Sstevel@tonic-gate * (see RFC 2246, section 7.4.7.1). */ 17840Sstevel@tonic-gate ERR_clear_error(); 17850Sstevel@tonic-gate i = SSL_MAX_MASTER_KEY_LENGTH; 17860Sstevel@tonic-gate p[0] = s->client_version >> 8; 17870Sstevel@tonic-gate p[1] = s->client_version & 0xff; 17882139Sjp161948 if (RAND_pseudo_bytes(p+2, i-2) <= 0) /* should be RAND_bytes, but we cannot work around a failure */ 17892139Sjp161948 goto err; 17900Sstevel@tonic-gate } 17910Sstevel@tonic-gate 17920Sstevel@tonic-gate s->session->master_key_length= 17930Sstevel@tonic-gate s->method->ssl3_enc->generate_master_secret(s, 17940Sstevel@tonic-gate s->session->master_key, 17950Sstevel@tonic-gate p,i); 17960Sstevel@tonic-gate OPENSSL_cleanse(p,i); 17970Sstevel@tonic-gate } 17980Sstevel@tonic-gate else 17990Sstevel@tonic-gate #endif 18000Sstevel@tonic-gate #ifndef OPENSSL_NO_DH 18010Sstevel@tonic-gate if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) 18020Sstevel@tonic-gate { 18030Sstevel@tonic-gate n2s(p,i); 18040Sstevel@tonic-gate if (n != i+2) 18050Sstevel@tonic-gate { 18060Sstevel@tonic-gate if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG)) 18070Sstevel@tonic-gate { 18080Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG); 18090Sstevel@tonic-gate goto err; 18100Sstevel@tonic-gate } 18110Sstevel@tonic-gate else 18120Sstevel@tonic-gate { 18130Sstevel@tonic-gate p-=2; 18140Sstevel@tonic-gate i=(int)n; 18150Sstevel@tonic-gate } 18160Sstevel@tonic-gate } 18170Sstevel@tonic-gate 18180Sstevel@tonic-gate if (n == 0L) /* the parameters are in the cert */ 18190Sstevel@tonic-gate { 18200Sstevel@tonic-gate al=SSL_AD_HANDSHAKE_FAILURE; 18210Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_DECODE_DH_CERTS); 18220Sstevel@tonic-gate goto f_err; 18230Sstevel@tonic-gate } 18240Sstevel@tonic-gate else 18250Sstevel@tonic-gate { 18260Sstevel@tonic-gate if (s->s3->tmp.dh == NULL) 18270Sstevel@tonic-gate { 18280Sstevel@tonic-gate al=SSL_AD_HANDSHAKE_FAILURE; 18290Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY); 18300Sstevel@tonic-gate goto f_err; 18310Sstevel@tonic-gate } 18320Sstevel@tonic-gate else 18330Sstevel@tonic-gate dh_srvr=s->s3->tmp.dh; 18340Sstevel@tonic-gate } 18350Sstevel@tonic-gate 18360Sstevel@tonic-gate pub=BN_bin2bn(p,i,NULL); 18370Sstevel@tonic-gate if (pub == NULL) 18380Sstevel@tonic-gate { 18390Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB); 18400Sstevel@tonic-gate goto err; 18410Sstevel@tonic-gate } 18420Sstevel@tonic-gate 18430Sstevel@tonic-gate i=DH_compute_key(p,pub,dh_srvr); 18440Sstevel@tonic-gate 18450Sstevel@tonic-gate if (i <= 0) 18460Sstevel@tonic-gate { 18470Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); 18480Sstevel@tonic-gate goto err; 18490Sstevel@tonic-gate } 18500Sstevel@tonic-gate 18510Sstevel@tonic-gate DH_free(s->s3->tmp.dh); 18520Sstevel@tonic-gate s->s3->tmp.dh=NULL; 18530Sstevel@tonic-gate 18540Sstevel@tonic-gate BN_clear_free(pub); 18550Sstevel@tonic-gate pub=NULL; 18560Sstevel@tonic-gate s->session->master_key_length= 18570Sstevel@tonic-gate s->method->ssl3_enc->generate_master_secret(s, 18580Sstevel@tonic-gate s->session->master_key,p,i); 18590Sstevel@tonic-gate OPENSSL_cleanse(p,i); 18600Sstevel@tonic-gate } 18610Sstevel@tonic-gate else 18620Sstevel@tonic-gate #endif 18630Sstevel@tonic-gate #ifndef OPENSSL_NO_KRB5 18640Sstevel@tonic-gate if (l & SSL_kKRB5) 18650Sstevel@tonic-gate { 18660Sstevel@tonic-gate krb5_error_code krb5rc; 18670Sstevel@tonic-gate krb5_data enc_ticket; 18680Sstevel@tonic-gate krb5_data authenticator; 18690Sstevel@tonic-gate krb5_data enc_pms; 18700Sstevel@tonic-gate KSSL_CTX *kssl_ctx = s->kssl_ctx; 18710Sstevel@tonic-gate EVP_CIPHER_CTX ciph_ctx; 18720Sstevel@tonic-gate EVP_CIPHER *enc = NULL; 18730Sstevel@tonic-gate unsigned char iv[EVP_MAX_IV_LENGTH]; 18740Sstevel@tonic-gate unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH 18750Sstevel@tonic-gate + EVP_MAX_BLOCK_LENGTH]; 18760Sstevel@tonic-gate int padl, outl; 18770Sstevel@tonic-gate krb5_timestamp authtime = 0; 18780Sstevel@tonic-gate krb5_ticket_times ttimes; 18790Sstevel@tonic-gate 18800Sstevel@tonic-gate EVP_CIPHER_CTX_init(&ciph_ctx); 18810Sstevel@tonic-gate 18820Sstevel@tonic-gate if (!kssl_ctx) kssl_ctx = kssl_ctx_new(); 18830Sstevel@tonic-gate 18840Sstevel@tonic-gate n2s(p,i); 18850Sstevel@tonic-gate enc_ticket.length = i; 18860Sstevel@tonic-gate 18872139Sjp161948 if (n < (int)enc_ticket.length + 6) 18880Sstevel@tonic-gate { 18890Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 18900Sstevel@tonic-gate SSL_R_DATA_LENGTH_TOO_LONG); 18910Sstevel@tonic-gate goto err; 18920Sstevel@tonic-gate } 18930Sstevel@tonic-gate 18940Sstevel@tonic-gate enc_ticket.data = (char *)p; 18950Sstevel@tonic-gate p+=enc_ticket.length; 18960Sstevel@tonic-gate 18970Sstevel@tonic-gate n2s(p,i); 18980Sstevel@tonic-gate authenticator.length = i; 18990Sstevel@tonic-gate 19002139Sjp161948 if (n < (int)(enc_ticket.length + authenticator.length) + 6) 19010Sstevel@tonic-gate { 19020Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 19030Sstevel@tonic-gate SSL_R_DATA_LENGTH_TOO_LONG); 19040Sstevel@tonic-gate goto err; 19050Sstevel@tonic-gate } 19060Sstevel@tonic-gate 19070Sstevel@tonic-gate authenticator.data = (char *)p; 19080Sstevel@tonic-gate p+=authenticator.length; 19090Sstevel@tonic-gate 19100Sstevel@tonic-gate n2s(p,i); 19110Sstevel@tonic-gate enc_pms.length = i; 19120Sstevel@tonic-gate enc_pms.data = (char *)p; 19130Sstevel@tonic-gate p+=enc_pms.length; 19140Sstevel@tonic-gate 19150Sstevel@tonic-gate /* Note that the length is checked again below, 19160Sstevel@tonic-gate ** after decryption 19170Sstevel@tonic-gate */ 19180Sstevel@tonic-gate if(enc_pms.length > sizeof pms) 19190Sstevel@tonic-gate { 19200Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 19210Sstevel@tonic-gate SSL_R_DATA_LENGTH_TOO_LONG); 19220Sstevel@tonic-gate goto err; 19230Sstevel@tonic-gate } 19240Sstevel@tonic-gate 19252139Sjp161948 if (n != (long)(enc_ticket.length + authenticator.length + 19262139Sjp161948 enc_pms.length + 6)) 19270Sstevel@tonic-gate { 19280Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 19290Sstevel@tonic-gate SSL_R_DATA_LENGTH_TOO_LONG); 19300Sstevel@tonic-gate goto err; 19310Sstevel@tonic-gate } 19320Sstevel@tonic-gate 19330Sstevel@tonic-gate if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes, 19340Sstevel@tonic-gate &kssl_err)) != 0) 19350Sstevel@tonic-gate { 19360Sstevel@tonic-gate #ifdef KSSL_DEBUG 19370Sstevel@tonic-gate printf("kssl_sget_tkt rtn %d [%d]\n", 19380Sstevel@tonic-gate krb5rc, kssl_err.reason); 19390Sstevel@tonic-gate if (kssl_err.text) 19400Sstevel@tonic-gate printf("kssl_err text= %s\n", kssl_err.text); 19410Sstevel@tonic-gate #endif /* KSSL_DEBUG */ 19422139Sjp161948 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 19430Sstevel@tonic-gate kssl_err.reason); 19440Sstevel@tonic-gate goto err; 19450Sstevel@tonic-gate } 19460Sstevel@tonic-gate 19470Sstevel@tonic-gate /* Note: no authenticator is not considered an error, 19480Sstevel@tonic-gate ** but will return authtime == 0. 19490Sstevel@tonic-gate */ 19500Sstevel@tonic-gate if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator, 19510Sstevel@tonic-gate &authtime, &kssl_err)) != 0) 19520Sstevel@tonic-gate { 19530Sstevel@tonic-gate #ifdef KSSL_DEBUG 19540Sstevel@tonic-gate printf("kssl_check_authent rtn %d [%d]\n", 19550Sstevel@tonic-gate krb5rc, kssl_err.reason); 19560Sstevel@tonic-gate if (kssl_err.text) 19570Sstevel@tonic-gate printf("kssl_err text= %s\n", kssl_err.text); 19580Sstevel@tonic-gate #endif /* KSSL_DEBUG */ 19592139Sjp161948 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 19600Sstevel@tonic-gate kssl_err.reason); 19610Sstevel@tonic-gate goto err; 19620Sstevel@tonic-gate } 19630Sstevel@tonic-gate 19640Sstevel@tonic-gate if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0) 19650Sstevel@tonic-gate { 19662139Sjp161948 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, krb5rc); 19670Sstevel@tonic-gate goto err; 19680Sstevel@tonic-gate } 19690Sstevel@tonic-gate 19700Sstevel@tonic-gate #ifdef KSSL_DEBUG 19710Sstevel@tonic-gate kssl_ctx_show(kssl_ctx); 19720Sstevel@tonic-gate #endif /* KSSL_DEBUG */ 19730Sstevel@tonic-gate 19740Sstevel@tonic-gate enc = kssl_map_enc(kssl_ctx->enctype); 19750Sstevel@tonic-gate if (enc == NULL) 19760Sstevel@tonic-gate goto err; 19770Sstevel@tonic-gate 19780Sstevel@tonic-gate memset(iv, 0, sizeof iv); /* per RFC 1510 */ 19790Sstevel@tonic-gate 19800Sstevel@tonic-gate if (!EVP_DecryptInit_ex(&ciph_ctx,enc,NULL,kssl_ctx->key,iv)) 19810Sstevel@tonic-gate { 19820Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 19830Sstevel@tonic-gate SSL_R_DECRYPTION_FAILED); 19840Sstevel@tonic-gate goto err; 19850Sstevel@tonic-gate } 19860Sstevel@tonic-gate if (!EVP_DecryptUpdate(&ciph_ctx, pms,&outl, 19870Sstevel@tonic-gate (unsigned char *)enc_pms.data, enc_pms.length)) 19880Sstevel@tonic-gate { 19890Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 19900Sstevel@tonic-gate SSL_R_DECRYPTION_FAILED); 19910Sstevel@tonic-gate goto err; 19920Sstevel@tonic-gate } 19930Sstevel@tonic-gate if (outl > SSL_MAX_MASTER_KEY_LENGTH) 19940Sstevel@tonic-gate { 19950Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 19960Sstevel@tonic-gate SSL_R_DATA_LENGTH_TOO_LONG); 19970Sstevel@tonic-gate goto err; 19980Sstevel@tonic-gate } 19990Sstevel@tonic-gate if (!EVP_DecryptFinal_ex(&ciph_ctx,&(pms[outl]),&padl)) 20000Sstevel@tonic-gate { 20010Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 20020Sstevel@tonic-gate SSL_R_DECRYPTION_FAILED); 20030Sstevel@tonic-gate goto err; 20040Sstevel@tonic-gate } 20050Sstevel@tonic-gate outl += padl; 20060Sstevel@tonic-gate if (outl > SSL_MAX_MASTER_KEY_LENGTH) 20070Sstevel@tonic-gate { 20080Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 20090Sstevel@tonic-gate SSL_R_DATA_LENGTH_TOO_LONG); 20100Sstevel@tonic-gate goto err; 20110Sstevel@tonic-gate } 20120Sstevel@tonic-gate EVP_CIPHER_CTX_cleanup(&ciph_ctx); 20130Sstevel@tonic-gate 20140Sstevel@tonic-gate s->session->master_key_length= 20150Sstevel@tonic-gate s->method->ssl3_enc->generate_master_secret(s, 20160Sstevel@tonic-gate s->session->master_key, pms, outl); 20170Sstevel@tonic-gate 20180Sstevel@tonic-gate if (kssl_ctx->client_princ) 20190Sstevel@tonic-gate { 2020*3018Svk199839 size_t len = strlen(kssl_ctx->client_princ); 20210Sstevel@tonic-gate if ( len < SSL_MAX_KRB5_PRINCIPAL_LENGTH ) 20220Sstevel@tonic-gate { 20230Sstevel@tonic-gate s->session->krb5_client_princ_len = len; 20240Sstevel@tonic-gate memcpy(s->session->krb5_client_princ,kssl_ctx->client_princ,len); 20250Sstevel@tonic-gate } 20260Sstevel@tonic-gate } 20270Sstevel@tonic-gate 20280Sstevel@tonic-gate 20290Sstevel@tonic-gate /* Was doing kssl_ctx_free() here, 20300Sstevel@tonic-gate ** but it caused problems for apache. 20310Sstevel@tonic-gate ** kssl_ctx = kssl_ctx_free(kssl_ctx); 20320Sstevel@tonic-gate ** if (s->kssl_ctx) s->kssl_ctx = NULL; 20330Sstevel@tonic-gate */ 20340Sstevel@tonic-gate } 20350Sstevel@tonic-gate else 20360Sstevel@tonic-gate #endif /* OPENSSL_NO_KRB5 */ 20372139Sjp161948 20382139Sjp161948 #ifndef OPENSSL_NO_ECDH 20392139Sjp161948 if ((l & SSL_kECDH) || (l & SSL_kECDHE)) 20402139Sjp161948 { 20412139Sjp161948 int ret = 1; 20422139Sjp161948 int field_size = 0; 20432139Sjp161948 const EC_KEY *tkey; 20442139Sjp161948 const EC_GROUP *group; 20452139Sjp161948 const BIGNUM *priv_key; 20462139Sjp161948 20472139Sjp161948 /* initialize structures for server's ECDH key pair */ 20482139Sjp161948 if ((srvr_ecdh = EC_KEY_new()) == NULL) 20492139Sjp161948 { 20502139Sjp161948 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 20512139Sjp161948 ERR_R_MALLOC_FAILURE); 20522139Sjp161948 goto err; 20532139Sjp161948 } 20542139Sjp161948 20552139Sjp161948 /* Let's get server private key and group information */ 20562139Sjp161948 if (l & SSL_kECDH) 20572139Sjp161948 { 20582139Sjp161948 /* use the certificate */ 20592139Sjp161948 tkey = s->cert->key->privatekey->pkey.ec; 20602139Sjp161948 } 20612139Sjp161948 else 20622139Sjp161948 { 20632139Sjp161948 /* use the ephermeral values we saved when 20642139Sjp161948 * generating the ServerKeyExchange msg. 20652139Sjp161948 */ 20662139Sjp161948 tkey = s->s3->tmp.ecdh; 20672139Sjp161948 } 20682139Sjp161948 20692139Sjp161948 group = EC_KEY_get0_group(tkey); 20702139Sjp161948 priv_key = EC_KEY_get0_private_key(tkey); 20712139Sjp161948 20722139Sjp161948 if (!EC_KEY_set_group(srvr_ecdh, group) || 20732139Sjp161948 !EC_KEY_set_private_key(srvr_ecdh, priv_key)) 20742139Sjp161948 { 20752139Sjp161948 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 20762139Sjp161948 ERR_R_EC_LIB); 20772139Sjp161948 goto err; 20782139Sjp161948 } 20792139Sjp161948 20802139Sjp161948 /* Let's get client's public key */ 20812139Sjp161948 if ((clnt_ecpoint = EC_POINT_new(group)) == NULL) 20822139Sjp161948 { 20832139Sjp161948 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 20842139Sjp161948 ERR_R_MALLOC_FAILURE); 20852139Sjp161948 goto err; 20862139Sjp161948 } 20872139Sjp161948 20882139Sjp161948 if (n == 0L) 20892139Sjp161948 { 20902139Sjp161948 /* Client Publickey was in Client Certificate */ 20912139Sjp161948 20922139Sjp161948 if (l & SSL_kECDHE) 20932139Sjp161948 { 20942139Sjp161948 al=SSL_AD_HANDSHAKE_FAILURE; 20952139Sjp161948 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY); 20962139Sjp161948 goto f_err; 20972139Sjp161948 } 20982139Sjp161948 if (((clnt_pub_pkey=X509_get_pubkey(s->session->peer)) 20992139Sjp161948 == NULL) || 21002139Sjp161948 (clnt_pub_pkey->type != EVP_PKEY_EC)) 21012139Sjp161948 { 21022139Sjp161948 /* XXX: For now, we do not support client 21032139Sjp161948 * authentication using ECDH certificates 21042139Sjp161948 * so this branch (n == 0L) of the code is 21052139Sjp161948 * never executed. When that support is 21062139Sjp161948 * added, we ought to ensure the key 21072139Sjp161948 * received in the certificate is 21082139Sjp161948 * authorized for key agreement. 21092139Sjp161948 * ECDH_compute_key implicitly checks that 21102139Sjp161948 * the two ECDH shares are for the same 21112139Sjp161948 * group. 21122139Sjp161948 */ 21132139Sjp161948 al=SSL_AD_HANDSHAKE_FAILURE; 21142139Sjp161948 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 21152139Sjp161948 SSL_R_UNABLE_TO_DECODE_ECDH_CERTS); 21162139Sjp161948 goto f_err; 21172139Sjp161948 } 21182139Sjp161948 21192139Sjp161948 EC_POINT_copy(clnt_ecpoint, 21202139Sjp161948 EC_KEY_get0_public_key(clnt_pub_pkey->pkey.ec)); 21212139Sjp161948 ret = 2; /* Skip certificate verify processing */ 21222139Sjp161948 } 21232139Sjp161948 else 21242139Sjp161948 { 21252139Sjp161948 /* Get client's public key from encoded point 21262139Sjp161948 * in the ClientKeyExchange message. 21272139Sjp161948 */ 21282139Sjp161948 if ((bn_ctx = BN_CTX_new()) == NULL) 21292139Sjp161948 { 21302139Sjp161948 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 21312139Sjp161948 ERR_R_MALLOC_FAILURE); 21322139Sjp161948 goto err; 21332139Sjp161948 } 21342139Sjp161948 21352139Sjp161948 /* Get encoded point length */ 21362139Sjp161948 i = *p; 21372139Sjp161948 p += 1; 21382139Sjp161948 if (EC_POINT_oct2point(group, 21392139Sjp161948 clnt_ecpoint, p, i, bn_ctx) == 0) 21402139Sjp161948 { 21412139Sjp161948 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 21422139Sjp161948 ERR_R_EC_LIB); 21432139Sjp161948 goto err; 21442139Sjp161948 } 21452139Sjp161948 /* p is pointing to somewhere in the buffer 21462139Sjp161948 * currently, so set it to the start 21472139Sjp161948 */ 21482139Sjp161948 p=(unsigned char *)s->init_buf->data; 21492139Sjp161948 } 21502139Sjp161948 21512139Sjp161948 /* Compute the shared pre-master secret */ 21522139Sjp161948 field_size = EC_GROUP_get_degree(group); 21532139Sjp161948 if (field_size <= 0) 21542139Sjp161948 { 21552139Sjp161948 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 21562139Sjp161948 ERR_R_ECDH_LIB); 21572139Sjp161948 goto err; 21582139Sjp161948 } 21592139Sjp161948 /* If field size is not more than 24 octets, then use SHA-1 hash of result; 21602139Sjp161948 * otherwise, use result (see section 4.8 of draft-ietf-tls-ecc-03.txt; 21612139Sjp161948 * this is new with this version of the Internet Draft). 21622139Sjp161948 */ 21632139Sjp161948 if (field_size <= 24 * 8) 21642139Sjp161948 i = ECDH_compute_key(p, KDF1_SHA1_len, clnt_ecpoint, srvr_ecdh, KDF1_SHA1); 21652139Sjp161948 else 21662139Sjp161948 i = ECDH_compute_key(p, (field_size+7)/8, clnt_ecpoint, srvr_ecdh, NULL); 21672139Sjp161948 if (i <= 0) 21682139Sjp161948 { 21692139Sjp161948 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 21702139Sjp161948 ERR_R_ECDH_LIB); 21712139Sjp161948 goto err; 21722139Sjp161948 } 21732139Sjp161948 21742139Sjp161948 EVP_PKEY_free(clnt_pub_pkey); 21752139Sjp161948 EC_POINT_free(clnt_ecpoint); 21762139Sjp161948 if (srvr_ecdh != NULL) 21772139Sjp161948 EC_KEY_free(srvr_ecdh); 21782139Sjp161948 BN_CTX_free(bn_ctx); 21792139Sjp161948 21802139Sjp161948 /* Compute the master secret */ 21812139Sjp161948 s->session->master_key_length = s->method->ssl3_enc-> \ 21822139Sjp161948 generate_master_secret(s, s->session->master_key, p, i); 21832139Sjp161948 21842139Sjp161948 OPENSSL_cleanse(p, i); 21852139Sjp161948 return (ret); 21862139Sjp161948 } 21872139Sjp161948 else 21882139Sjp161948 #endif 21890Sstevel@tonic-gate { 21900Sstevel@tonic-gate al=SSL_AD_HANDSHAKE_FAILURE; 21910Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 21920Sstevel@tonic-gate SSL_R_UNKNOWN_CIPHER_TYPE); 21930Sstevel@tonic-gate goto f_err; 21940Sstevel@tonic-gate } 21950Sstevel@tonic-gate 21960Sstevel@tonic-gate return(1); 21970Sstevel@tonic-gate f_err: 21980Sstevel@tonic-gate ssl3_send_alert(s,SSL3_AL_FATAL,al); 21992139Sjp161948 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_ECDH) 22000Sstevel@tonic-gate err: 22010Sstevel@tonic-gate #endif 22022139Sjp161948 #ifndef OPENSSL_NO_ECDH 22032139Sjp161948 EVP_PKEY_free(clnt_pub_pkey); 22042139Sjp161948 EC_POINT_free(clnt_ecpoint); 22052139Sjp161948 if (srvr_ecdh != NULL) 22062139Sjp161948 EC_KEY_free(srvr_ecdh); 22072139Sjp161948 BN_CTX_free(bn_ctx); 22082139Sjp161948 #endif 22090Sstevel@tonic-gate return(-1); 22100Sstevel@tonic-gate } 22110Sstevel@tonic-gate 22122139Sjp161948 int ssl3_get_cert_verify(SSL *s) 22130Sstevel@tonic-gate { 22140Sstevel@tonic-gate EVP_PKEY *pkey=NULL; 22150Sstevel@tonic-gate unsigned char *p; 22160Sstevel@tonic-gate int al,ok,ret=0; 22170Sstevel@tonic-gate long n; 22180Sstevel@tonic-gate int type=0,i,j; 22190Sstevel@tonic-gate X509 *peer; 22200Sstevel@tonic-gate 22212139Sjp161948 n=s->method->ssl_get_message(s, 22220Sstevel@tonic-gate SSL3_ST_SR_CERT_VRFY_A, 22230Sstevel@tonic-gate SSL3_ST_SR_CERT_VRFY_B, 22240Sstevel@tonic-gate -1, 22250Sstevel@tonic-gate 514, /* 514? */ 22260Sstevel@tonic-gate &ok); 22270Sstevel@tonic-gate 22280Sstevel@tonic-gate if (!ok) return((int)n); 22290Sstevel@tonic-gate 22300Sstevel@tonic-gate if (s->session->peer != NULL) 22310Sstevel@tonic-gate { 22320Sstevel@tonic-gate peer=s->session->peer; 22330Sstevel@tonic-gate pkey=X509_get_pubkey(peer); 22340Sstevel@tonic-gate type=X509_certificate_type(peer,pkey); 22350Sstevel@tonic-gate } 22360Sstevel@tonic-gate else 22370Sstevel@tonic-gate { 22380Sstevel@tonic-gate peer=NULL; 22390Sstevel@tonic-gate pkey=NULL; 22400Sstevel@tonic-gate } 22410Sstevel@tonic-gate 22420Sstevel@tonic-gate if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY) 22430Sstevel@tonic-gate { 22440Sstevel@tonic-gate s->s3->tmp.reuse_message=1; 22450Sstevel@tonic-gate if ((peer != NULL) && (type | EVP_PKT_SIGN)) 22460Sstevel@tonic-gate { 22470Sstevel@tonic-gate al=SSL_AD_UNEXPECTED_MESSAGE; 22480Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_MESSAGE); 22490Sstevel@tonic-gate goto f_err; 22500Sstevel@tonic-gate } 22510Sstevel@tonic-gate ret=1; 22520Sstevel@tonic-gate goto end; 22530Sstevel@tonic-gate } 22540Sstevel@tonic-gate 22550Sstevel@tonic-gate if (peer == NULL) 22560Sstevel@tonic-gate { 22570Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED); 22580Sstevel@tonic-gate al=SSL_AD_UNEXPECTED_MESSAGE; 22590Sstevel@tonic-gate goto f_err; 22600Sstevel@tonic-gate } 22610Sstevel@tonic-gate 22620Sstevel@tonic-gate if (!(type & EVP_PKT_SIGN)) 22630Sstevel@tonic-gate { 22640Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE); 22650Sstevel@tonic-gate al=SSL_AD_ILLEGAL_PARAMETER; 22660Sstevel@tonic-gate goto f_err; 22670Sstevel@tonic-gate } 22680Sstevel@tonic-gate 22690Sstevel@tonic-gate if (s->s3->change_cipher_spec) 22700Sstevel@tonic-gate { 22710Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY); 22720Sstevel@tonic-gate al=SSL_AD_UNEXPECTED_MESSAGE; 22730Sstevel@tonic-gate goto f_err; 22740Sstevel@tonic-gate } 22750Sstevel@tonic-gate 22760Sstevel@tonic-gate /* we now have a signature that we need to verify */ 22770Sstevel@tonic-gate p=(unsigned char *)s->init_msg; 22780Sstevel@tonic-gate n2s(p,i); 22790Sstevel@tonic-gate n-=2; 22800Sstevel@tonic-gate if (i > n) 22810Sstevel@tonic-gate { 22820Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH); 22830Sstevel@tonic-gate al=SSL_AD_DECODE_ERROR; 22840Sstevel@tonic-gate goto f_err; 22850Sstevel@tonic-gate } 22860Sstevel@tonic-gate 22870Sstevel@tonic-gate j=EVP_PKEY_size(pkey); 22880Sstevel@tonic-gate if ((i > j) || (n > j) || (n <= 0)) 22890Sstevel@tonic-gate { 22900Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE); 22910Sstevel@tonic-gate al=SSL_AD_DECODE_ERROR; 22920Sstevel@tonic-gate goto f_err; 22930Sstevel@tonic-gate } 22940Sstevel@tonic-gate 22950Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA 22960Sstevel@tonic-gate if (pkey->type == EVP_PKEY_RSA) 22970Sstevel@tonic-gate { 22980Sstevel@tonic-gate i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md, 22990Sstevel@tonic-gate MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, p, i, 23000Sstevel@tonic-gate pkey->pkey.rsa); 23010Sstevel@tonic-gate if (i < 0) 23020Sstevel@tonic-gate { 23030Sstevel@tonic-gate al=SSL_AD_DECRYPT_ERROR; 23040Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT); 23050Sstevel@tonic-gate goto f_err; 23060Sstevel@tonic-gate } 23070Sstevel@tonic-gate if (i == 0) 23080Sstevel@tonic-gate { 23090Sstevel@tonic-gate al=SSL_AD_DECRYPT_ERROR; 23100Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATURE); 23110Sstevel@tonic-gate goto f_err; 23120Sstevel@tonic-gate } 23130Sstevel@tonic-gate } 23140Sstevel@tonic-gate else 23150Sstevel@tonic-gate #endif 23160Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA 23170Sstevel@tonic-gate if (pkey->type == EVP_PKEY_DSA) 23180Sstevel@tonic-gate { 23190Sstevel@tonic-gate j=DSA_verify(pkey->save_type, 23200Sstevel@tonic-gate &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]), 23210Sstevel@tonic-gate SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa); 23220Sstevel@tonic-gate if (j <= 0) 23230Sstevel@tonic-gate { 23240Sstevel@tonic-gate /* bad signature */ 23250Sstevel@tonic-gate al=SSL_AD_DECRYPT_ERROR; 23260Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATURE); 23270Sstevel@tonic-gate goto f_err; 23280Sstevel@tonic-gate } 23290Sstevel@tonic-gate } 23300Sstevel@tonic-gate else 23310Sstevel@tonic-gate #endif 23322139Sjp161948 #ifndef OPENSSL_NO_ECDSA 23332139Sjp161948 if (pkey->type == EVP_PKEY_EC) 23342139Sjp161948 { 23352139Sjp161948 j=ECDSA_verify(pkey->save_type, 23362139Sjp161948 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]), 23372139Sjp161948 SHA_DIGEST_LENGTH,p,i,pkey->pkey.ec); 23382139Sjp161948 if (j <= 0) 23392139Sjp161948 { 23402139Sjp161948 /* bad signature */ 23412139Sjp161948 al=SSL_AD_DECRYPT_ERROR; 23422139Sjp161948 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, 23432139Sjp161948 SSL_R_BAD_ECDSA_SIGNATURE); 23442139Sjp161948 goto f_err; 23452139Sjp161948 } 23462139Sjp161948 } 23472139Sjp161948 else 23482139Sjp161948 #endif 23490Sstevel@tonic-gate { 23500Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,ERR_R_INTERNAL_ERROR); 23510Sstevel@tonic-gate al=SSL_AD_UNSUPPORTED_CERTIFICATE; 23520Sstevel@tonic-gate goto f_err; 23530Sstevel@tonic-gate } 23540Sstevel@tonic-gate 23550Sstevel@tonic-gate 23560Sstevel@tonic-gate ret=1; 23570Sstevel@tonic-gate if (0) 23580Sstevel@tonic-gate { 23590Sstevel@tonic-gate f_err: 23600Sstevel@tonic-gate ssl3_send_alert(s,SSL3_AL_FATAL,al); 23610Sstevel@tonic-gate } 23620Sstevel@tonic-gate end: 23630Sstevel@tonic-gate EVP_PKEY_free(pkey); 23640Sstevel@tonic-gate return(ret); 23650Sstevel@tonic-gate } 23660Sstevel@tonic-gate 23672139Sjp161948 int ssl3_get_client_certificate(SSL *s) 23680Sstevel@tonic-gate { 23690Sstevel@tonic-gate int i,ok,al,ret= -1; 23700Sstevel@tonic-gate X509 *x=NULL; 23710Sstevel@tonic-gate unsigned long l,nc,llen,n; 23722139Sjp161948 const unsigned char *p,*q; 23732139Sjp161948 unsigned char *d; 23740Sstevel@tonic-gate STACK_OF(X509) *sk=NULL; 23750Sstevel@tonic-gate 23762139Sjp161948 n=s->method->ssl_get_message(s, 23770Sstevel@tonic-gate SSL3_ST_SR_CERT_A, 23780Sstevel@tonic-gate SSL3_ST_SR_CERT_B, 23790Sstevel@tonic-gate -1, 23800Sstevel@tonic-gate s->max_cert_list, 23810Sstevel@tonic-gate &ok); 23820Sstevel@tonic-gate 23830Sstevel@tonic-gate if (!ok) return((int)n); 23840Sstevel@tonic-gate 23850Sstevel@tonic-gate if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) 23860Sstevel@tonic-gate { 23870Sstevel@tonic-gate if ( (s->verify_mode & SSL_VERIFY_PEER) && 23880Sstevel@tonic-gate (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) 23890Sstevel@tonic-gate { 23900Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); 23910Sstevel@tonic-gate al=SSL_AD_HANDSHAKE_FAILURE; 23920Sstevel@tonic-gate goto f_err; 23930Sstevel@tonic-gate } 23940Sstevel@tonic-gate /* If tls asked for a client cert, the client must return a 0 list */ 23950Sstevel@tonic-gate if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request) 23960Sstevel@tonic-gate { 23970Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST); 23980Sstevel@tonic-gate al=SSL_AD_UNEXPECTED_MESSAGE; 23990Sstevel@tonic-gate goto f_err; 24000Sstevel@tonic-gate } 24010Sstevel@tonic-gate s->s3->tmp.reuse_message=1; 24020Sstevel@tonic-gate return(1); 24030Sstevel@tonic-gate } 24040Sstevel@tonic-gate 24050Sstevel@tonic-gate if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) 24060Sstevel@tonic-gate { 24070Sstevel@tonic-gate al=SSL_AD_UNEXPECTED_MESSAGE; 24080Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYPE); 24090Sstevel@tonic-gate goto f_err; 24100Sstevel@tonic-gate } 24112139Sjp161948 p=d=(unsigned char *)s->init_msg; 24120Sstevel@tonic-gate 24130Sstevel@tonic-gate if ((sk=sk_X509_new_null()) == NULL) 24140Sstevel@tonic-gate { 24150Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE); 24160Sstevel@tonic-gate goto err; 24170Sstevel@tonic-gate } 24180Sstevel@tonic-gate 24190Sstevel@tonic-gate n2l3(p,llen); 24200Sstevel@tonic-gate if (llen+3 != n) 24210Sstevel@tonic-gate { 24220Sstevel@tonic-gate al=SSL_AD_DECODE_ERROR; 24230Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH); 24240Sstevel@tonic-gate goto f_err; 24250Sstevel@tonic-gate } 24260Sstevel@tonic-gate for (nc=0; nc<llen; ) 24270Sstevel@tonic-gate { 24280Sstevel@tonic-gate n2l3(p,l); 24290Sstevel@tonic-gate if ((l+nc+3) > llen) 24300Sstevel@tonic-gate { 24310Sstevel@tonic-gate al=SSL_AD_DECODE_ERROR; 24320Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH); 24330Sstevel@tonic-gate goto f_err; 24340Sstevel@tonic-gate } 24350Sstevel@tonic-gate 24360Sstevel@tonic-gate q=p; 24370Sstevel@tonic-gate x=d2i_X509(NULL,&p,l); 24380Sstevel@tonic-gate if (x == NULL) 24390Sstevel@tonic-gate { 24400Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB); 24410Sstevel@tonic-gate goto err; 24420Sstevel@tonic-gate } 24430Sstevel@tonic-gate if (p != (q+l)) 24440Sstevel@tonic-gate { 24450Sstevel@tonic-gate al=SSL_AD_DECODE_ERROR; 24460Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH); 24470Sstevel@tonic-gate goto f_err; 24480Sstevel@tonic-gate } 24490Sstevel@tonic-gate if (!sk_X509_push(sk,x)) 24500Sstevel@tonic-gate { 24510Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE); 24520Sstevel@tonic-gate goto err; 24530Sstevel@tonic-gate } 24540Sstevel@tonic-gate x=NULL; 24550Sstevel@tonic-gate nc+=l+3; 24560Sstevel@tonic-gate } 24570Sstevel@tonic-gate 24580Sstevel@tonic-gate if (sk_X509_num(sk) <= 0) 24590Sstevel@tonic-gate { 24600Sstevel@tonic-gate /* TLS does not mind 0 certs returned */ 24610Sstevel@tonic-gate if (s->version == SSL3_VERSION) 24620Sstevel@tonic-gate { 24630Sstevel@tonic-gate al=SSL_AD_HANDSHAKE_FAILURE; 24640Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATES_RETURNED); 24650Sstevel@tonic-gate goto f_err; 24660Sstevel@tonic-gate } 24670Sstevel@tonic-gate /* Fail for TLS only if we required a certificate */ 24680Sstevel@tonic-gate else if ((s->verify_mode & SSL_VERIFY_PEER) && 24690Sstevel@tonic-gate (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) 24700Sstevel@tonic-gate { 24710Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); 24720Sstevel@tonic-gate al=SSL_AD_HANDSHAKE_FAILURE; 24730Sstevel@tonic-gate goto f_err; 24740Sstevel@tonic-gate } 24750Sstevel@tonic-gate } 24760Sstevel@tonic-gate else 24770Sstevel@tonic-gate { 24780Sstevel@tonic-gate i=ssl_verify_cert_chain(s,sk); 24790Sstevel@tonic-gate if (!i) 24800Sstevel@tonic-gate { 24810Sstevel@tonic-gate al=ssl_verify_alarm_type(s->verify_result); 24820Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATE_RETURNED); 24830Sstevel@tonic-gate goto f_err; 24840Sstevel@tonic-gate } 24850Sstevel@tonic-gate } 24860Sstevel@tonic-gate 24870Sstevel@tonic-gate if (s->session->peer != NULL) /* This should not be needed */ 24880Sstevel@tonic-gate X509_free(s->session->peer); 24890Sstevel@tonic-gate s->session->peer=sk_X509_shift(sk); 24900Sstevel@tonic-gate s->session->verify_result = s->verify_result; 24910Sstevel@tonic-gate 24920Sstevel@tonic-gate /* With the current implementation, sess_cert will always be NULL 24930Sstevel@tonic-gate * when we arrive here. */ 24940Sstevel@tonic-gate if (s->session->sess_cert == NULL) 24950Sstevel@tonic-gate { 24960Sstevel@tonic-gate s->session->sess_cert = ssl_sess_cert_new(); 24970Sstevel@tonic-gate if (s->session->sess_cert == NULL) 24980Sstevel@tonic-gate { 24990Sstevel@tonic-gate SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE); 25000Sstevel@tonic-gate goto err; 25010Sstevel@tonic-gate } 25020Sstevel@tonic-gate } 25030Sstevel@tonic-gate if (s->session->sess_cert->cert_chain != NULL) 25040Sstevel@tonic-gate sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free); 25050Sstevel@tonic-gate s->session->sess_cert->cert_chain=sk; 25060Sstevel@tonic-gate /* Inconsistency alert: cert_chain does *not* include the 25070Sstevel@tonic-gate * peer's own certificate, while we do include it in s3_clnt.c */ 25080Sstevel@tonic-gate 25090Sstevel@tonic-gate sk=NULL; 25100Sstevel@tonic-gate 25110Sstevel@tonic-gate ret=1; 25120Sstevel@tonic-gate if (0) 25130Sstevel@tonic-gate { 25140Sstevel@tonic-gate f_err: 25150Sstevel@tonic-gate ssl3_send_alert(s,SSL3_AL_FATAL,al); 25160Sstevel@tonic-gate } 25170Sstevel@tonic-gate err: 25180Sstevel@tonic-gate if (x != NULL) X509_free(x); 25190Sstevel@tonic-gate if (sk != NULL) sk_X509_pop_free(sk,X509_free); 25200Sstevel@tonic-gate return(ret); 25210Sstevel@tonic-gate } 25220Sstevel@tonic-gate 25230Sstevel@tonic-gate int ssl3_send_server_certificate(SSL *s) 25240Sstevel@tonic-gate { 25250Sstevel@tonic-gate unsigned long l; 25260Sstevel@tonic-gate X509 *x; 25270Sstevel@tonic-gate 25280Sstevel@tonic-gate if (s->state == SSL3_ST_SW_CERT_A) 25290Sstevel@tonic-gate { 25300Sstevel@tonic-gate x=ssl_get_server_send_cert(s); 25310Sstevel@tonic-gate if (x == NULL && 25320Sstevel@tonic-gate /* VRS: allow null cert if auth == KRB5 */ 25330Sstevel@tonic-gate (s->s3->tmp.new_cipher->algorithms 25340Sstevel@tonic-gate & (SSL_MKEY_MASK|SSL_AUTH_MASK)) 25350Sstevel@tonic-gate != (SSL_aKRB5|SSL_kKRB5)) 25360Sstevel@tonic-gate { 25370Sstevel@tonic-gate SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR); 25380Sstevel@tonic-gate return(0); 25390Sstevel@tonic-gate } 25400Sstevel@tonic-gate 25410Sstevel@tonic-gate l=ssl3_output_cert_chain(s,x); 25420Sstevel@tonic-gate s->state=SSL3_ST_SW_CERT_B; 25430Sstevel@tonic-gate s->init_num=(int)l; 25440Sstevel@tonic-gate s->init_off=0; 25450Sstevel@tonic-gate } 25460Sstevel@tonic-gate 25470Sstevel@tonic-gate /* SSL3_ST_SW_CERT_B */ 25480Sstevel@tonic-gate return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 25490Sstevel@tonic-gate } 25502139Sjp161948 25512139Sjp161948 25522139Sjp161948 #ifndef OPENSSL_NO_ECDH 25532139Sjp161948 /* This is the complement of curve_id2nid in s3_clnt.c. */ 25542139Sjp161948 static int nid2curve_id(int nid) 25552139Sjp161948 { 25562139Sjp161948 /* ECC curves from draft-ietf-tls-ecc-01.txt (Mar 15, 2001) 25572139Sjp161948 * (no changes in draft-ietf-tls-ecc-03.txt [June 2003]) */ 25582139Sjp161948 switch (nid) { 25592139Sjp161948 case NID_sect163k1: /* sect163k1 (1) */ 25602139Sjp161948 return 1; 25612139Sjp161948 case NID_sect163r1: /* sect163r1 (2) */ 25622139Sjp161948 return 2; 25632139Sjp161948 case NID_sect163r2: /* sect163r2 (3) */ 25642139Sjp161948 return 3; 25652139Sjp161948 case NID_sect193r1: /* sect193r1 (4) */ 25662139Sjp161948 return 4; 25672139Sjp161948 case NID_sect193r2: /* sect193r2 (5) */ 25682139Sjp161948 return 5; 25692139Sjp161948 case NID_sect233k1: /* sect233k1 (6) */ 25702139Sjp161948 return 6; 25712139Sjp161948 case NID_sect233r1: /* sect233r1 (7) */ 25722139Sjp161948 return 7; 25732139Sjp161948 case NID_sect239k1: /* sect239k1 (8) */ 25742139Sjp161948 return 8; 25752139Sjp161948 case NID_sect283k1: /* sect283k1 (9) */ 25762139Sjp161948 return 9; 25772139Sjp161948 case NID_sect283r1: /* sect283r1 (10) */ 25782139Sjp161948 return 10; 25792139Sjp161948 case NID_sect409k1: /* sect409k1 (11) */ 25802139Sjp161948 return 11; 25812139Sjp161948 case NID_sect409r1: /* sect409r1 (12) */ 25822139Sjp161948 return 12; 25832139Sjp161948 case NID_sect571k1: /* sect571k1 (13) */ 25842139Sjp161948 return 13; 25852139Sjp161948 case NID_sect571r1: /* sect571r1 (14) */ 25862139Sjp161948 return 14; 25872139Sjp161948 case NID_secp160k1: /* secp160k1 (15) */ 25882139Sjp161948 return 15; 25892139Sjp161948 case NID_secp160r1: /* secp160r1 (16) */ 25902139Sjp161948 return 16; 25912139Sjp161948 case NID_secp160r2: /* secp160r2 (17) */ 25922139Sjp161948 return 17; 25932139Sjp161948 case NID_secp192k1: /* secp192k1 (18) */ 25942139Sjp161948 return 18; 25952139Sjp161948 case NID_X9_62_prime192v1: /* secp192r1 (19) */ 25962139Sjp161948 return 19; 25972139Sjp161948 case NID_secp224k1: /* secp224k1 (20) */ 25982139Sjp161948 return 20; 25992139Sjp161948 case NID_secp224r1: /* secp224r1 (21) */ 26002139Sjp161948 return 21; 26012139Sjp161948 case NID_secp256k1: /* secp256k1 (22) */ 26022139Sjp161948 return 22; 26032139Sjp161948 case NID_X9_62_prime256v1: /* secp256r1 (23) */ 26042139Sjp161948 return 23; 26052139Sjp161948 case NID_secp384r1: /* secp384r1 (24) */ 26062139Sjp161948 return 24; 26072139Sjp161948 case NID_secp521r1: /* secp521r1 (25) */ 26082139Sjp161948 return 25; 26092139Sjp161948 default: 26102139Sjp161948 return 0; 26112139Sjp161948 } 26122139Sjp161948 } 26132139Sjp161948 #endif 2614