xref: /onnv-gate/usr/src/common/openssl/ssl/s2_clnt.c (revision 8545:a1b3fd5884fd)
10Sstevel@tonic-gate /* ssl/s2_clnt.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 /* ====================================================================
590Sstevel@tonic-gate  * Copyright (c) 1998-2001 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  */
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate #include "ssl_locl.h"
1130Sstevel@tonic-gate #ifndef OPENSSL_NO_SSL2
1140Sstevel@tonic-gate #include <stdio.h>
1150Sstevel@tonic-gate #include <openssl/rand.h>
1160Sstevel@tonic-gate #include <openssl/buffer.h>
1170Sstevel@tonic-gate #include <openssl/objects.h>
1180Sstevel@tonic-gate #include <openssl/evp.h>
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate static SSL_METHOD *ssl2_get_client_method(int ver);
1210Sstevel@tonic-gate static int get_server_finished(SSL *s);
1220Sstevel@tonic-gate static int get_server_verify(SSL *s);
1230Sstevel@tonic-gate static int get_server_hello(SSL *s);
1240Sstevel@tonic-gate static int client_hello(SSL *s);
1250Sstevel@tonic-gate static int client_master_key(SSL *s);
1260Sstevel@tonic-gate static int client_finished(SSL *s);
1270Sstevel@tonic-gate static int client_certificate(SSL *s);
1280Sstevel@tonic-gate static int ssl_rsa_public_encrypt(SESS_CERT *sc, int len, unsigned char *from,
1290Sstevel@tonic-gate 	unsigned char *to,int padding);
1300Sstevel@tonic-gate #define BREAK	break
1310Sstevel@tonic-gate 
ssl2_get_client_method(int ver)1320Sstevel@tonic-gate static SSL_METHOD *ssl2_get_client_method(int ver)
1330Sstevel@tonic-gate 	{
1340Sstevel@tonic-gate 	if (ver == SSL2_VERSION)
1350Sstevel@tonic-gate 		return(SSLv2_client_method());
1360Sstevel@tonic-gate 	else
1370Sstevel@tonic-gate 		return(NULL);
1380Sstevel@tonic-gate 	}
1390Sstevel@tonic-gate 
IMPLEMENT_ssl2_meth_func(SSLv2_client_method,ssl_undefined_function,ssl2_connect,ssl2_get_client_method)1402139Sjp161948 IMPLEMENT_ssl2_meth_func(SSLv2_client_method,
1412139Sjp161948 			ssl_undefined_function,
1422139Sjp161948 			ssl2_connect,
1432139Sjp161948 			ssl2_get_client_method)
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate int ssl2_connect(SSL *s)
1460Sstevel@tonic-gate 	{
1470Sstevel@tonic-gate 	unsigned long l=time(NULL);
1480Sstevel@tonic-gate 	BUF_MEM *buf=NULL;
1490Sstevel@tonic-gate 	int ret= -1;
1500Sstevel@tonic-gate 	void (*cb)(const SSL *ssl,int type,int val)=NULL;
1510Sstevel@tonic-gate 	int new_state,state;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	RAND_add(&l,sizeof(l),0);
1540Sstevel@tonic-gate 	ERR_clear_error();
1550Sstevel@tonic-gate 	clear_sys_error();
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	if (s->info_callback != NULL)
1580Sstevel@tonic-gate 		cb=s->info_callback;
1590Sstevel@tonic-gate 	else if (s->ctx->info_callback != NULL)
1600Sstevel@tonic-gate 		cb=s->ctx->info_callback;
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	/* init things to blank */
1630Sstevel@tonic-gate 	s->in_handshake++;
1640Sstevel@tonic-gate 	if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	for (;;)
1670Sstevel@tonic-gate 		{
1680Sstevel@tonic-gate 		state=s->state;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 		switch (s->state)
1710Sstevel@tonic-gate 			{
1720Sstevel@tonic-gate 		case SSL_ST_BEFORE:
1730Sstevel@tonic-gate 		case SSL_ST_CONNECT:
1740Sstevel@tonic-gate 		case SSL_ST_BEFORE|SSL_ST_CONNECT:
1750Sstevel@tonic-gate 		case SSL_ST_OK|SSL_ST_CONNECT:
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 			s->server=0;
1780Sstevel@tonic-gate 			if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 			s->version=SSL2_VERSION;
1810Sstevel@tonic-gate 			s->type=SSL_ST_CONNECT;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 			buf=s->init_buf;
1840Sstevel@tonic-gate 			if ((buf == NULL) && ((buf=BUF_MEM_new()) == NULL))
1850Sstevel@tonic-gate 				{
1860Sstevel@tonic-gate 				ret= -1;
1870Sstevel@tonic-gate 				goto end;
1880Sstevel@tonic-gate 				}
1890Sstevel@tonic-gate 			if (!BUF_MEM_grow(buf,
1900Sstevel@tonic-gate 				SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))
1910Sstevel@tonic-gate 				{
1920Sstevel@tonic-gate 				if (buf == s->init_buf)
1930Sstevel@tonic-gate 					buf=NULL;
1940Sstevel@tonic-gate 				ret= -1;
1950Sstevel@tonic-gate 				goto end;
1960Sstevel@tonic-gate 				}
1970Sstevel@tonic-gate 			s->init_buf=buf;
1980Sstevel@tonic-gate 			buf=NULL;
1990Sstevel@tonic-gate 			s->init_num=0;
2000Sstevel@tonic-gate 			s->state=SSL2_ST_SEND_CLIENT_HELLO_A;
2010Sstevel@tonic-gate 			s->ctx->stats.sess_connect++;
2020Sstevel@tonic-gate 			s->handshake_func=ssl2_connect;
2030Sstevel@tonic-gate 			BREAK;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 		case SSL2_ST_SEND_CLIENT_HELLO_A:
2060Sstevel@tonic-gate 		case SSL2_ST_SEND_CLIENT_HELLO_B:
2070Sstevel@tonic-gate 			s->shutdown=0;
2080Sstevel@tonic-gate 			ret=client_hello(s);
2090Sstevel@tonic-gate 			if (ret <= 0) goto end;
2100Sstevel@tonic-gate 			s->init_num=0;
2110Sstevel@tonic-gate 			s->state=SSL2_ST_GET_SERVER_HELLO_A;
2120Sstevel@tonic-gate 			BREAK;
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 		case SSL2_ST_GET_SERVER_HELLO_A:
2150Sstevel@tonic-gate 		case SSL2_ST_GET_SERVER_HELLO_B:
2160Sstevel@tonic-gate 			ret=get_server_hello(s);
2170Sstevel@tonic-gate 			if (ret <= 0) goto end;
2180Sstevel@tonic-gate 			s->init_num=0;
2190Sstevel@tonic-gate 			if (!s->hit) /* new session */
2200Sstevel@tonic-gate 				{
2210Sstevel@tonic-gate 				s->state=SSL2_ST_SEND_CLIENT_MASTER_KEY_A;
2220Sstevel@tonic-gate 				BREAK;
2230Sstevel@tonic-gate 				}
2240Sstevel@tonic-gate 			else
2250Sstevel@tonic-gate 				{
2260Sstevel@tonic-gate 				s->state=SSL2_ST_CLIENT_START_ENCRYPTION;
2270Sstevel@tonic-gate 				break;
2280Sstevel@tonic-gate 				}
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 		case SSL2_ST_SEND_CLIENT_MASTER_KEY_A:
2310Sstevel@tonic-gate 		case SSL2_ST_SEND_CLIENT_MASTER_KEY_B:
2320Sstevel@tonic-gate 			ret=client_master_key(s);
2330Sstevel@tonic-gate 			if (ret <= 0) goto end;
2340Sstevel@tonic-gate 			s->init_num=0;
2350Sstevel@tonic-gate 			s->state=SSL2_ST_CLIENT_START_ENCRYPTION;
2360Sstevel@tonic-gate 			break;
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 		case SSL2_ST_CLIENT_START_ENCRYPTION:
2390Sstevel@tonic-gate 			/* Ok, we now have all the stuff needed to
2400Sstevel@tonic-gate 			 * start encrypting, so lets fire it up :-) */
2410Sstevel@tonic-gate 			if (!ssl2_enc_init(s,1))
2420Sstevel@tonic-gate 				{
2430Sstevel@tonic-gate 				ret= -1;
2440Sstevel@tonic-gate 				goto end;
2450Sstevel@tonic-gate 				}
2460Sstevel@tonic-gate 			s->s2->clear_text=0;
2470Sstevel@tonic-gate 			s->state=SSL2_ST_SEND_CLIENT_FINISHED_A;
2480Sstevel@tonic-gate 			break;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 		case SSL2_ST_SEND_CLIENT_FINISHED_A:
2510Sstevel@tonic-gate 		case SSL2_ST_SEND_CLIENT_FINISHED_B:
2520Sstevel@tonic-gate 			ret=client_finished(s);
2530Sstevel@tonic-gate 			if (ret <= 0) goto end;
2540Sstevel@tonic-gate 			s->init_num=0;
2550Sstevel@tonic-gate 			s->state=SSL2_ST_GET_SERVER_VERIFY_A;
2560Sstevel@tonic-gate 			break;
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 		case SSL2_ST_GET_SERVER_VERIFY_A:
2590Sstevel@tonic-gate 		case SSL2_ST_GET_SERVER_VERIFY_B:
2600Sstevel@tonic-gate 			ret=get_server_verify(s);
2610Sstevel@tonic-gate 			if (ret <= 0) goto end;
2620Sstevel@tonic-gate 			s->init_num=0;
2630Sstevel@tonic-gate 			s->state=SSL2_ST_GET_SERVER_FINISHED_A;
2640Sstevel@tonic-gate 			break;
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 		case SSL2_ST_GET_SERVER_FINISHED_A:
2670Sstevel@tonic-gate 		case SSL2_ST_GET_SERVER_FINISHED_B:
2680Sstevel@tonic-gate 			ret=get_server_finished(s);
2690Sstevel@tonic-gate 			if (ret <= 0) goto end;
2700Sstevel@tonic-gate 			break;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 		case SSL2_ST_SEND_CLIENT_CERTIFICATE_A:
2730Sstevel@tonic-gate 		case SSL2_ST_SEND_CLIENT_CERTIFICATE_B:
2740Sstevel@tonic-gate 		case SSL2_ST_SEND_CLIENT_CERTIFICATE_C:
2750Sstevel@tonic-gate 		case SSL2_ST_SEND_CLIENT_CERTIFICATE_D:
2760Sstevel@tonic-gate 		case SSL2_ST_X509_GET_CLIENT_CERTIFICATE:
2770Sstevel@tonic-gate 			ret=client_certificate(s);
2780Sstevel@tonic-gate 			if (ret <= 0) goto end;
2790Sstevel@tonic-gate 			s->init_num=0;
2800Sstevel@tonic-gate 			s->state=SSL2_ST_GET_SERVER_FINISHED_A;
2810Sstevel@tonic-gate 			break;
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 		case SSL_ST_OK:
2840Sstevel@tonic-gate 			if (s->init_buf != NULL)
2850Sstevel@tonic-gate 				{
2860Sstevel@tonic-gate 				BUF_MEM_free(s->init_buf);
2870Sstevel@tonic-gate 				s->init_buf=NULL;
2880Sstevel@tonic-gate 				}
2890Sstevel@tonic-gate 			s->init_num=0;
2900Sstevel@tonic-gate 		/*	ERR_clear_error();*/
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 			/* If we want to cache session-ids in the client
2930Sstevel@tonic-gate 			 * and we successfully add the session-id to the
2940Sstevel@tonic-gate 			 * cache, and there is a callback, then pass it out.
2950Sstevel@tonic-gate 			 * 26/11/96 - eay - only add if not a re-used session.
2960Sstevel@tonic-gate 			 */
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 			ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
2990Sstevel@tonic-gate 			if (s->hit) s->ctx->stats.sess_hit++;
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 			ret=1;
3020Sstevel@tonic-gate 			/* s->server=0; */
3030Sstevel@tonic-gate 			s->ctx->stats.sess_connect_good++;
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 			if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 			goto end;
3080Sstevel@tonic-gate 			/* break; */
3090Sstevel@tonic-gate 		default:
3100Sstevel@tonic-gate 			SSLerr(SSL_F_SSL2_CONNECT,SSL_R_UNKNOWN_STATE);
3110Sstevel@tonic-gate 			return(-1);
3120Sstevel@tonic-gate 			/* break; */
3130Sstevel@tonic-gate 			}
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 		if ((cb != NULL) && (s->state != state))
3160Sstevel@tonic-gate 			{
3170Sstevel@tonic-gate 			new_state=s->state;
3180Sstevel@tonic-gate 			s->state=state;
3190Sstevel@tonic-gate 			cb(s,SSL_CB_CONNECT_LOOP,1);
3200Sstevel@tonic-gate 			s->state=new_state;
3210Sstevel@tonic-gate 			}
3220Sstevel@tonic-gate 		}
3230Sstevel@tonic-gate end:
3240Sstevel@tonic-gate 	s->in_handshake--;
3250Sstevel@tonic-gate 	if (buf != NULL)
3260Sstevel@tonic-gate 		BUF_MEM_free(buf);
3270Sstevel@tonic-gate 	if (cb != NULL)
3280Sstevel@tonic-gate 		cb(s,SSL_CB_CONNECT_EXIT,ret);
3290Sstevel@tonic-gate 	return(ret);
3300Sstevel@tonic-gate 	}
3310Sstevel@tonic-gate 
get_server_hello(SSL * s)3320Sstevel@tonic-gate static int get_server_hello(SSL *s)
3330Sstevel@tonic-gate 	{
3340Sstevel@tonic-gate 	unsigned char *buf;
3350Sstevel@tonic-gate 	unsigned char *p;
3360Sstevel@tonic-gate 	int i,j;
3370Sstevel@tonic-gate 	unsigned long len;
3380Sstevel@tonic-gate 	STACK_OF(SSL_CIPHER) *sk=NULL,*cl, *prio, *allow;
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	buf=(unsigned char *)s->init_buf->data;
3410Sstevel@tonic-gate 	p=buf;
3420Sstevel@tonic-gate 	if (s->state == SSL2_ST_GET_SERVER_HELLO_A)
3430Sstevel@tonic-gate 		{
3440Sstevel@tonic-gate 		i=ssl2_read(s,(char *)&(buf[s->init_num]),11-s->init_num);
3450Sstevel@tonic-gate 		if (i < (11-s->init_num))
3460Sstevel@tonic-gate 			return(ssl2_part_read(s,SSL_F_GET_SERVER_HELLO,i));
3470Sstevel@tonic-gate 		s->init_num = 11;
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 		if (*(p++) != SSL2_MT_SERVER_HELLO)
3500Sstevel@tonic-gate 			{
3510Sstevel@tonic-gate 			if (p[-1] != SSL2_MT_ERROR)
3520Sstevel@tonic-gate 				{
3530Sstevel@tonic-gate 				ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
3540Sstevel@tonic-gate 				SSLerr(SSL_F_GET_SERVER_HELLO,
3550Sstevel@tonic-gate 					SSL_R_READ_WRONG_PACKET_TYPE);
3560Sstevel@tonic-gate 				}
3570Sstevel@tonic-gate 			else
3580Sstevel@tonic-gate 				SSLerr(SSL_F_GET_SERVER_HELLO,
3590Sstevel@tonic-gate 					SSL_R_PEER_ERROR);
3600Sstevel@tonic-gate 			return(-1);
3610Sstevel@tonic-gate 			}
3620Sstevel@tonic-gate #ifdef __APPLE_CC__
3630Sstevel@tonic-gate 		/* The Rhapsody 5.5 (a.k.a. MacOS X) compiler bug
3640Sstevel@tonic-gate 		 * workaround. <appro@fy.chalmers.se> */
3650Sstevel@tonic-gate 		s->hit=(i=*(p++))?1:0;
3660Sstevel@tonic-gate #else
3670Sstevel@tonic-gate 		s->hit=(*(p++))?1:0;
3680Sstevel@tonic-gate #endif
3690Sstevel@tonic-gate 		s->s2->tmp.cert_type= *(p++);
3700Sstevel@tonic-gate 		n2s(p,i);
3710Sstevel@tonic-gate 		if (i < s->version) s->version=i;
3720Sstevel@tonic-gate 		n2s(p,i); s->s2->tmp.cert_length=i;
3730Sstevel@tonic-gate 		n2s(p,i); s->s2->tmp.csl=i;
3740Sstevel@tonic-gate 		n2s(p,i); s->s2->tmp.conn_id_length=i;
3750Sstevel@tonic-gate 		s->state=SSL2_ST_GET_SERVER_HELLO_B;
3760Sstevel@tonic-gate 		}
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	/* SSL2_ST_GET_SERVER_HELLO_B */
3790Sstevel@tonic-gate 	len = 11 + (unsigned long)s->s2->tmp.cert_length + (unsigned long)s->s2->tmp.csl + (unsigned long)s->s2->tmp.conn_id_length;
3800Sstevel@tonic-gate 	if (len > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)
3810Sstevel@tonic-gate 		{
3820Sstevel@tonic-gate 		SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_MESSAGE_TOO_LONG);
3830Sstevel@tonic-gate 		return -1;
3840Sstevel@tonic-gate 		}
3850Sstevel@tonic-gate 	j = (int)len - s->init_num;
3860Sstevel@tonic-gate 	i = ssl2_read(s,(char *)&(buf[s->init_num]),j);
3870Sstevel@tonic-gate 	if (i != j) return(ssl2_part_read(s,SSL_F_GET_SERVER_HELLO,i));
3880Sstevel@tonic-gate 	if (s->msg_callback)
3890Sstevel@tonic-gate 		s->msg_callback(0, s->version, 0, buf, (size_t)len, s, s->msg_callback_arg); /* SERVER-HELLO */
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	/* things are looking good */
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	p = buf + 11;
3940Sstevel@tonic-gate 	if (s->hit)
3950Sstevel@tonic-gate 		{
3960Sstevel@tonic-gate 		if (s->s2->tmp.cert_length != 0)
3970Sstevel@tonic-gate 			{
3980Sstevel@tonic-gate 			SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_REUSE_CERT_LENGTH_NOT_ZERO);
3990Sstevel@tonic-gate 			return(-1);
4000Sstevel@tonic-gate 			}
4010Sstevel@tonic-gate 		if (s->s2->tmp.cert_type != 0)
4020Sstevel@tonic-gate 			{
4030Sstevel@tonic-gate 			if (!(s->options &
4040Sstevel@tonic-gate 				SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG))
4050Sstevel@tonic-gate 				{
4060Sstevel@tonic-gate 				SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_REUSE_CERT_TYPE_NOT_ZERO);
4070Sstevel@tonic-gate 				return(-1);
4080Sstevel@tonic-gate 				}
4090Sstevel@tonic-gate 			}
4100Sstevel@tonic-gate 		if (s->s2->tmp.csl != 0)
4110Sstevel@tonic-gate 			{
4120Sstevel@tonic-gate 			SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_REUSE_CIPHER_LIST_NOT_ZERO);
4130Sstevel@tonic-gate 			return(-1);
4140Sstevel@tonic-gate 			}
4150Sstevel@tonic-gate 		}
4160Sstevel@tonic-gate 	else
4170Sstevel@tonic-gate 		{
4180Sstevel@tonic-gate #ifdef undef
4190Sstevel@tonic-gate 		/* very bad */
4200Sstevel@tonic-gate 		memset(s->session->session_id,0,
4210Sstevel@tonic-gate 			SSL_MAX_SSL_SESSION_ID_LENGTH_IN_BYTES);
4220Sstevel@tonic-gate 		s->session->session_id_length=0;
4230Sstevel@tonic-gate 		*/
4240Sstevel@tonic-gate #endif
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 		/* we need to do this in case we were trying to reuse a
4270Sstevel@tonic-gate 		 * client session but others are already reusing it.
4280Sstevel@tonic-gate 		 * If this was a new 'blank' session ID, the session-id
4290Sstevel@tonic-gate 		 * length will still be 0 */
4300Sstevel@tonic-gate 		if (s->session->session_id_length > 0)
4310Sstevel@tonic-gate 			{
4320Sstevel@tonic-gate 			if (!ssl_get_new_session(s,0))
4330Sstevel@tonic-gate 				{
4340Sstevel@tonic-gate 				ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
4350Sstevel@tonic-gate 				return(-1);
4360Sstevel@tonic-gate 				}
4370Sstevel@tonic-gate 			}
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 		if (ssl2_set_certificate(s,s->s2->tmp.cert_type,
4400Sstevel@tonic-gate 			s->s2->tmp.cert_length,p) <= 0)
4410Sstevel@tonic-gate 			{
4420Sstevel@tonic-gate 			ssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE);
4430Sstevel@tonic-gate 			return(-1);
4440Sstevel@tonic-gate 			}
4450Sstevel@tonic-gate 		p+=s->s2->tmp.cert_length;
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 		if (s->s2->tmp.csl == 0)
4480Sstevel@tonic-gate 			{
4490Sstevel@tonic-gate 			ssl2_return_error(s,SSL2_PE_NO_CIPHER);
4500Sstevel@tonic-gate 			SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_NO_CIPHER_LIST);
4510Sstevel@tonic-gate 			return(-1);
4520Sstevel@tonic-gate 			}
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 		/* We have just received a list of ciphers back from the
4550Sstevel@tonic-gate 		 * server.  We need to get the ones that match, then select
4560Sstevel@tonic-gate 		 * the one we want the most :-). */
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 		/* load the ciphers */
4590Sstevel@tonic-gate 		sk=ssl_bytes_to_cipher_list(s,p,s->s2->tmp.csl,
4600Sstevel@tonic-gate 					    &s->session->ciphers);
4610Sstevel@tonic-gate 		p+=s->s2->tmp.csl;
4620Sstevel@tonic-gate 		if (sk == NULL)
4630Sstevel@tonic-gate 			{
4640Sstevel@tonic-gate 			ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
4650Sstevel@tonic-gate 			SSLerr(SSL_F_GET_SERVER_HELLO,ERR_R_MALLOC_FAILURE);
4660Sstevel@tonic-gate 			return(-1);
4670Sstevel@tonic-gate 			}
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 		sk_SSL_CIPHER_set_cmp_func(sk,ssl_cipher_ptr_id_cmp);
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 		/* get the array of ciphers we will accept */
4720Sstevel@tonic-gate 		cl=SSL_get_ciphers(s);
4730Sstevel@tonic-gate 		sk_SSL_CIPHER_set_cmp_func(cl,ssl_cipher_ptr_id_cmp);
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 		/*
4760Sstevel@tonic-gate 		 * If server preference flag set, choose the first
4770Sstevel@tonic-gate 		 * (highest priority) cipher the server sends, otherwise
4780Sstevel@tonic-gate 		 * client preference has priority.
4790Sstevel@tonic-gate 		 */
4800Sstevel@tonic-gate 		if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
4810Sstevel@tonic-gate 		    {
4820Sstevel@tonic-gate 		    prio = sk;
4830Sstevel@tonic-gate 		    allow = cl;
4840Sstevel@tonic-gate 		    }
4850Sstevel@tonic-gate 		else
4860Sstevel@tonic-gate 		    {
4870Sstevel@tonic-gate 		    prio = cl;
4880Sstevel@tonic-gate 		    allow = sk;
4890Sstevel@tonic-gate 		    }
4900Sstevel@tonic-gate 		/* In theory we could have ciphers sent back that we
4910Sstevel@tonic-gate 		 * don't want to use but that does not matter since we
4920Sstevel@tonic-gate 		 * will check against the list we originally sent and
4930Sstevel@tonic-gate 		 * for performance reasons we should not bother to match
4940Sstevel@tonic-gate 		 * the two lists up just to check. */
4950Sstevel@tonic-gate 		for (i=0; i<sk_SSL_CIPHER_num(prio); i++)
4960Sstevel@tonic-gate 			{
4970Sstevel@tonic-gate 			if (sk_SSL_CIPHER_find(allow,
4980Sstevel@tonic-gate 					     sk_SSL_CIPHER_value(prio,i)) >= 0)
4990Sstevel@tonic-gate 				break;
5000Sstevel@tonic-gate 			}
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 		if (i >= sk_SSL_CIPHER_num(prio))
5030Sstevel@tonic-gate 			{
5040Sstevel@tonic-gate 			ssl2_return_error(s,SSL2_PE_NO_CIPHER);
5050Sstevel@tonic-gate 			SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_NO_CIPHER_MATCH);
5060Sstevel@tonic-gate 			return(-1);
5070Sstevel@tonic-gate 			}
5080Sstevel@tonic-gate 		s->session->cipher=sk_SSL_CIPHER_value(prio,i);
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 		if (s->session->peer != NULL) /* can't happen*/
5120Sstevel@tonic-gate 			{
5130Sstevel@tonic-gate 			ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
5140Sstevel@tonic-gate 			SSLerr(SSL_F_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
5150Sstevel@tonic-gate 			return(-1);
5160Sstevel@tonic-gate 			}
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 		s->session->peer = s->session->sess_cert->peer_key->x509;
5190Sstevel@tonic-gate 		/* peer_key->x509 has been set by ssl2_set_certificate. */
5200Sstevel@tonic-gate 		CRYPTO_add(&s->session->peer->references, 1, CRYPTO_LOCK_X509);
5210Sstevel@tonic-gate 		}
5220Sstevel@tonic-gate 
5233018Svk199839 	if (s->session->sess_cert == NULL
5243018Svk199839       || s->session->peer != s->session->sess_cert->peer_key->x509)
5250Sstevel@tonic-gate 		/* can't happen */
5260Sstevel@tonic-gate 		{
5270Sstevel@tonic-gate 		ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
5280Sstevel@tonic-gate 		SSLerr(SSL_F_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
5290Sstevel@tonic-gate 		return(-1);
5300Sstevel@tonic-gate 		}
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	s->s2->conn_id_length=s->s2->tmp.conn_id_length;
5330Sstevel@tonic-gate 	if (s->s2->conn_id_length > sizeof s->s2->conn_id)
5340Sstevel@tonic-gate 		{
5350Sstevel@tonic-gate 		ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
5360Sstevel@tonic-gate 		SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_SSL2_CONNECTION_ID_TOO_LONG);
5370Sstevel@tonic-gate 		return -1;
5380Sstevel@tonic-gate 		}
5390Sstevel@tonic-gate 	memcpy(s->s2->conn_id,p,s->s2->tmp.conn_id_length);
5400Sstevel@tonic-gate 	return(1);
5410Sstevel@tonic-gate 	}
5420Sstevel@tonic-gate 
client_hello(SSL * s)5430Sstevel@tonic-gate static int client_hello(SSL *s)
5440Sstevel@tonic-gate 	{
5450Sstevel@tonic-gate 	unsigned char *buf;
5460Sstevel@tonic-gate 	unsigned char *p,*d;
5470Sstevel@tonic-gate /*	CIPHER **cipher;*/
5480Sstevel@tonic-gate 	int i,n,j;
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	buf=(unsigned char *)s->init_buf->data;
5510Sstevel@tonic-gate 	if (s->state == SSL2_ST_SEND_CLIENT_HELLO_A)
5520Sstevel@tonic-gate 		{
5530Sstevel@tonic-gate 		if ((s->session == NULL) ||
5540Sstevel@tonic-gate 			(s->session->ssl_version != s->version))
5550Sstevel@tonic-gate 			{
5560Sstevel@tonic-gate 			if (!ssl_get_new_session(s,0))
5570Sstevel@tonic-gate 				{
5580Sstevel@tonic-gate 				ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
5590Sstevel@tonic-gate 				return(-1);
5600Sstevel@tonic-gate 				}
5610Sstevel@tonic-gate 			}
5620Sstevel@tonic-gate 		/* else use the pre-loaded session */
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 		p=buf;					/* header */
5650Sstevel@tonic-gate 		d=p+9;					/* data section */
5660Sstevel@tonic-gate 		*(p++)=SSL2_MT_CLIENT_HELLO;		/* type */
5670Sstevel@tonic-gate 		s2n(SSL2_VERSION,p);			/* version */
5680Sstevel@tonic-gate 		n=j=0;
5690Sstevel@tonic-gate 
5702139Sjp161948 		n=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),d,0);
5710Sstevel@tonic-gate 		d+=n;
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 		if (n == 0)
5740Sstevel@tonic-gate 			{
5750Sstevel@tonic-gate 			SSLerr(SSL_F_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
5760Sstevel@tonic-gate 			return(-1);
5770Sstevel@tonic-gate 			}
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 		s2n(n,p);			/* cipher spec num bytes */
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 		if ((s->session->session_id_length > 0) &&
5820Sstevel@tonic-gate 			(s->session->session_id_length <=
5830Sstevel@tonic-gate 			SSL2_MAX_SSL_SESSION_ID_LENGTH))
5840Sstevel@tonic-gate 			{
5850Sstevel@tonic-gate 			i=s->session->session_id_length;
5860Sstevel@tonic-gate 			s2n(i,p);		/* session id length */
5870Sstevel@tonic-gate 			memcpy(d,s->session->session_id,(unsigned int)i);
5880Sstevel@tonic-gate 			d+=i;
5890Sstevel@tonic-gate 			}
5900Sstevel@tonic-gate 		else
5910Sstevel@tonic-gate 			{
5920Sstevel@tonic-gate 			s2n(0,p);
5930Sstevel@tonic-gate 			}
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 		s->s2->challenge_length=SSL2_CHALLENGE_LENGTH;
5960Sstevel@tonic-gate 		s2n(SSL2_CHALLENGE_LENGTH,p);		/* challenge length */
5970Sstevel@tonic-gate 		/*challenge id data*/
5982139Sjp161948 		if (RAND_pseudo_bytes(s->s2->challenge,SSL2_CHALLENGE_LENGTH) <= 0)
5992139Sjp161948 			return -1;
6000Sstevel@tonic-gate 		memcpy(d,s->s2->challenge,SSL2_CHALLENGE_LENGTH);
6010Sstevel@tonic-gate 		d+=SSL2_CHALLENGE_LENGTH;
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 		s->state=SSL2_ST_SEND_CLIENT_HELLO_B;
6040Sstevel@tonic-gate 		s->init_num=d-buf;
6050Sstevel@tonic-gate 		s->init_off=0;
6060Sstevel@tonic-gate 		}
6070Sstevel@tonic-gate 	/* SSL2_ST_SEND_CLIENT_HELLO_B */
6080Sstevel@tonic-gate 	return(ssl2_do_write(s));
6090Sstevel@tonic-gate 	}
6100Sstevel@tonic-gate 
client_master_key(SSL * s)6110Sstevel@tonic-gate static int client_master_key(SSL *s)
6120Sstevel@tonic-gate 	{
6130Sstevel@tonic-gate 	unsigned char *buf;
6140Sstevel@tonic-gate 	unsigned char *p,*d;
6150Sstevel@tonic-gate 	int clear,enc,karg,i;
6160Sstevel@tonic-gate 	SSL_SESSION *sess;
6170Sstevel@tonic-gate 	const EVP_CIPHER *c;
6180Sstevel@tonic-gate 	const EVP_MD *md;
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	buf=(unsigned char *)s->init_buf->data;
6210Sstevel@tonic-gate 	if (s->state == SSL2_ST_SEND_CLIENT_MASTER_KEY_A)
6220Sstevel@tonic-gate 		{
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 		if (!ssl_cipher_get_evp(s->session,&c,&md,NULL))
6250Sstevel@tonic-gate 			{
6260Sstevel@tonic-gate 			ssl2_return_error(s,SSL2_PE_NO_CIPHER);
6270Sstevel@tonic-gate 			SSLerr(SSL_F_CLIENT_MASTER_KEY,SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS);
6280Sstevel@tonic-gate 			return(-1);
6290Sstevel@tonic-gate 			}
6300Sstevel@tonic-gate 		sess=s->session;
6310Sstevel@tonic-gate 		p=buf;
6320Sstevel@tonic-gate 		d=p+10;
6330Sstevel@tonic-gate 		*(p++)=SSL2_MT_CLIENT_MASTER_KEY;/* type */
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 		i=ssl_put_cipher_by_char(s,sess->cipher,p);
6360Sstevel@tonic-gate 		p+=i;
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 		/* make key_arg data */
6390Sstevel@tonic-gate 		i=EVP_CIPHER_iv_length(c);
6400Sstevel@tonic-gate 		sess->key_arg_length=i;
6410Sstevel@tonic-gate 		if (i > SSL_MAX_KEY_ARG_LENGTH)
6420Sstevel@tonic-gate 			{
6430Sstevel@tonic-gate 			ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
6440Sstevel@tonic-gate 			SSLerr(SSL_F_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR);
6450Sstevel@tonic-gate 			return -1;
6460Sstevel@tonic-gate 			}
6472139Sjp161948 		if (i > 0)
6482139Sjp161948 			if (RAND_pseudo_bytes(sess->key_arg,i) <= 0)
6492139Sjp161948 				return -1;
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 		/* make a master key */
6520Sstevel@tonic-gate 		i=EVP_CIPHER_key_length(c);
6530Sstevel@tonic-gate 		sess->master_key_length=i;
6540Sstevel@tonic-gate 		if (i > 0)
6550Sstevel@tonic-gate 			{
6562139Sjp161948 			if (i > (int)sizeof(sess->master_key))
6570Sstevel@tonic-gate 				{
6580Sstevel@tonic-gate 				ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
6590Sstevel@tonic-gate 				SSLerr(SSL_F_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR);
6600Sstevel@tonic-gate 				return -1;
6610Sstevel@tonic-gate 				}
6620Sstevel@tonic-gate 			if (RAND_bytes(sess->master_key,i) <= 0)
6630Sstevel@tonic-gate 				{
6640Sstevel@tonic-gate 				ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
6650Sstevel@tonic-gate 				return(-1);
6660Sstevel@tonic-gate 				}
6670Sstevel@tonic-gate 			}
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 		if (sess->cipher->algorithm2 & SSL2_CF_8_BYTE_ENC)
6700Sstevel@tonic-gate 			enc=8;
6710Sstevel@tonic-gate 		else if (SSL_C_IS_EXPORT(sess->cipher))
6720Sstevel@tonic-gate 			enc=5;
6730Sstevel@tonic-gate 		else
6740Sstevel@tonic-gate 			enc=i;
6750Sstevel@tonic-gate 
6762139Sjp161948 		if ((int)i < enc)
6770Sstevel@tonic-gate 			{
6780Sstevel@tonic-gate 			ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
6790Sstevel@tonic-gate 			SSLerr(SSL_F_CLIENT_MASTER_KEY,SSL_R_CIPHER_TABLE_SRC_ERROR);
6800Sstevel@tonic-gate 			return(-1);
6810Sstevel@tonic-gate 			}
6820Sstevel@tonic-gate 		clear=i-enc;
6830Sstevel@tonic-gate 		s2n(clear,p);
6840Sstevel@tonic-gate 		memcpy(d,sess->master_key,(unsigned int)clear);
6850Sstevel@tonic-gate 		d+=clear;
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 		enc=ssl_rsa_public_encrypt(sess->sess_cert,enc,
6880Sstevel@tonic-gate 			&(sess->master_key[clear]),d,
6890Sstevel@tonic-gate 			(s->s2->ssl2_rollback)?RSA_SSLV23_PADDING:RSA_PKCS1_PADDING);
6900Sstevel@tonic-gate 		if (enc <= 0)
6910Sstevel@tonic-gate 			{
6920Sstevel@tonic-gate 			ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
6930Sstevel@tonic-gate 			SSLerr(SSL_F_CLIENT_MASTER_KEY,SSL_R_PUBLIC_KEY_ENCRYPT_ERROR);
6940Sstevel@tonic-gate 			return(-1);
6950Sstevel@tonic-gate 			}
6960Sstevel@tonic-gate #ifdef PKCS1_CHECK
6970Sstevel@tonic-gate 		if (s->options & SSL_OP_PKCS1_CHECK_1) d[1]++;
6980Sstevel@tonic-gate 		if (s->options & SSL_OP_PKCS1_CHECK_2)
6990Sstevel@tonic-gate 			sess->master_key[clear]++;
7000Sstevel@tonic-gate #endif
7010Sstevel@tonic-gate 		s2n(enc,p);
7020Sstevel@tonic-gate 		d+=enc;
7030Sstevel@tonic-gate 		karg=sess->key_arg_length;
7040Sstevel@tonic-gate 		s2n(karg,p); /* key arg size */
7052139Sjp161948 		if (karg > (int)sizeof(sess->key_arg))
7060Sstevel@tonic-gate 			{
7070Sstevel@tonic-gate 			ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
7080Sstevel@tonic-gate 			SSLerr(SSL_F_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR);
7090Sstevel@tonic-gate 			return -1;
7100Sstevel@tonic-gate 			}
7110Sstevel@tonic-gate 		memcpy(d,sess->key_arg,(unsigned int)karg);
7120Sstevel@tonic-gate 		d+=karg;
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 		s->state=SSL2_ST_SEND_CLIENT_MASTER_KEY_B;
7150Sstevel@tonic-gate 		s->init_num=d-buf;
7160Sstevel@tonic-gate 		s->init_off=0;
7170Sstevel@tonic-gate 		}
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	/* SSL2_ST_SEND_CLIENT_MASTER_KEY_B */
7200Sstevel@tonic-gate 	return(ssl2_do_write(s));
7210Sstevel@tonic-gate 	}
7220Sstevel@tonic-gate 
client_finished(SSL * s)7230Sstevel@tonic-gate static int client_finished(SSL *s)
7240Sstevel@tonic-gate 	{
7250Sstevel@tonic-gate 	unsigned char *p;
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	if (s->state == SSL2_ST_SEND_CLIENT_FINISHED_A)
7280Sstevel@tonic-gate 		{
7290Sstevel@tonic-gate 		p=(unsigned char *)s->init_buf->data;
7300Sstevel@tonic-gate 		*(p++)=SSL2_MT_CLIENT_FINISHED;
7310Sstevel@tonic-gate 		if (s->s2->conn_id_length > sizeof s->s2->conn_id)
7320Sstevel@tonic-gate 			{
7330Sstevel@tonic-gate 			SSLerr(SSL_F_CLIENT_FINISHED, ERR_R_INTERNAL_ERROR);
7340Sstevel@tonic-gate 			return -1;
7350Sstevel@tonic-gate 			}
7360Sstevel@tonic-gate 		memcpy(p,s->s2->conn_id,(unsigned int)s->s2->conn_id_length);
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 		s->state=SSL2_ST_SEND_CLIENT_FINISHED_B;
7390Sstevel@tonic-gate 		s->init_num=s->s2->conn_id_length+1;
7400Sstevel@tonic-gate 		s->init_off=0;
7410Sstevel@tonic-gate 		}
7420Sstevel@tonic-gate 	return(ssl2_do_write(s));
7430Sstevel@tonic-gate 	}
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate /* read the data and then respond */
client_certificate(SSL * s)7460Sstevel@tonic-gate static int client_certificate(SSL *s)
7470Sstevel@tonic-gate 	{
7480Sstevel@tonic-gate 	unsigned char *buf;
7490Sstevel@tonic-gate 	unsigned char *p,*d;
7500Sstevel@tonic-gate 	int i;
7510Sstevel@tonic-gate 	unsigned int n;
7520Sstevel@tonic-gate 	int cert_ch_len;
7530Sstevel@tonic-gate 	unsigned char *cert_ch;
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	buf=(unsigned char *)s->init_buf->data;
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 	/* We have a cert associated with the SSL, so attach it to
7580Sstevel@tonic-gate 	 * the session if it does not have one */
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_A)
7610Sstevel@tonic-gate 		{
7620Sstevel@tonic-gate 		i=ssl2_read(s,(char *)&(buf[s->init_num]),
7630Sstevel@tonic-gate 			SSL2_MAX_CERT_CHALLENGE_LENGTH+2-s->init_num);
7640Sstevel@tonic-gate 		if (i<(SSL2_MIN_CERT_CHALLENGE_LENGTH+2-s->init_num))
7650Sstevel@tonic-gate 			return(ssl2_part_read(s,SSL_F_CLIENT_CERTIFICATE,i));
7660Sstevel@tonic-gate 		s->init_num += i;
7670Sstevel@tonic-gate 		if (s->msg_callback)
7680Sstevel@tonic-gate 			s->msg_callback(0, s->version, 0, buf, (size_t)s->init_num, s, s->msg_callback_arg); /* REQUEST-CERTIFICATE */
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 		/* type=buf[0]; */
7710Sstevel@tonic-gate 		/* type eq x509 */
7720Sstevel@tonic-gate 		if (buf[1] != SSL2_AT_MD5_WITH_RSA_ENCRYPTION)
7730Sstevel@tonic-gate 			{
7740Sstevel@tonic-gate 			ssl2_return_error(s,SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE);
7750Sstevel@tonic-gate 			SSLerr(SSL_F_CLIENT_CERTIFICATE,SSL_R_BAD_AUTHENTICATION_TYPE);
7760Sstevel@tonic-gate 			return(-1);
7770Sstevel@tonic-gate 			}
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 		if ((s->cert == NULL) ||
7800Sstevel@tonic-gate 			(s->cert->key->x509 == NULL) ||
7810Sstevel@tonic-gate 			(s->cert->key->privatekey == NULL))
7820Sstevel@tonic-gate 			{
7830Sstevel@tonic-gate 			s->state=SSL2_ST_X509_GET_CLIENT_CERTIFICATE;
7840Sstevel@tonic-gate 			}
7850Sstevel@tonic-gate 		else
7860Sstevel@tonic-gate 			s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_C;
7870Sstevel@tonic-gate 		}
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	cert_ch = buf + 2;
7900Sstevel@tonic-gate 	cert_ch_len = s->init_num - 2;
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	if (s->state == SSL2_ST_X509_GET_CLIENT_CERTIFICATE)
7930Sstevel@tonic-gate 		{
7940Sstevel@tonic-gate 		X509 *x509=NULL;
7950Sstevel@tonic-gate 		EVP_PKEY *pkey=NULL;
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 		/* If we get an error we need to
7980Sstevel@tonic-gate 		 * ssl->rwstate=SSL_X509_LOOKUP;
7990Sstevel@tonic-gate 		 * return(error);
8000Sstevel@tonic-gate 		 * We should then be retried when things are ok and we
8010Sstevel@tonic-gate 		 * can get a cert or not */
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 		i=0;
8040Sstevel@tonic-gate 		if (s->ctx->client_cert_cb != NULL)
8050Sstevel@tonic-gate 			{
8060Sstevel@tonic-gate 			i=s->ctx->client_cert_cb(s,&(x509),&(pkey));
8070Sstevel@tonic-gate 			}
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 		if (i < 0)
8100Sstevel@tonic-gate 			{
8110Sstevel@tonic-gate 			s->rwstate=SSL_X509_LOOKUP;
8120Sstevel@tonic-gate 			return(-1);
8130Sstevel@tonic-gate 			}
8140Sstevel@tonic-gate 		s->rwstate=SSL_NOTHING;
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 		if ((i == 1) && (pkey != NULL) && (x509 != NULL))
8170Sstevel@tonic-gate 			{
8180Sstevel@tonic-gate 			s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_C;
8190Sstevel@tonic-gate 			if (	!SSL_use_certificate(s,x509) ||
8200Sstevel@tonic-gate 				!SSL_use_PrivateKey(s,pkey))
8210Sstevel@tonic-gate 				{
8220Sstevel@tonic-gate 				i=0;
8230Sstevel@tonic-gate 				}
8240Sstevel@tonic-gate 			X509_free(x509);
8250Sstevel@tonic-gate 			EVP_PKEY_free(pkey);
8260Sstevel@tonic-gate 			}
8270Sstevel@tonic-gate 		else if (i == 1)
8280Sstevel@tonic-gate 			{
8290Sstevel@tonic-gate 			if (x509 != NULL) X509_free(x509);
8300Sstevel@tonic-gate 			if (pkey != NULL) EVP_PKEY_free(pkey);
8310Sstevel@tonic-gate 			SSLerr(SSL_F_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
8320Sstevel@tonic-gate 			i=0;
8330Sstevel@tonic-gate 			}
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 		if (i == 0)
8360Sstevel@tonic-gate 			{
8370Sstevel@tonic-gate 			/* We have no client certificate to respond with
8380Sstevel@tonic-gate 			 * so send the correct error message back */
8390Sstevel@tonic-gate 			s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_B;
8400Sstevel@tonic-gate 			p=buf;
8410Sstevel@tonic-gate 			*(p++)=SSL2_MT_ERROR;
8420Sstevel@tonic-gate 			s2n(SSL2_PE_NO_CERTIFICATE,p);
8430Sstevel@tonic-gate 			s->init_off=0;
8440Sstevel@tonic-gate 			s->init_num=3;
8450Sstevel@tonic-gate 			/* Write is done at the end */
8460Sstevel@tonic-gate 			}
8470Sstevel@tonic-gate 		}
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 	if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_B)
8500Sstevel@tonic-gate 		{
8510Sstevel@tonic-gate 		return(ssl2_do_write(s));
8520Sstevel@tonic-gate 		}
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 	if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_C)
8550Sstevel@tonic-gate 		{
8560Sstevel@tonic-gate 		EVP_MD_CTX ctx;
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 		/* ok, now we calculate the checksum
8590Sstevel@tonic-gate 		 * do it first so we can reuse buf :-) */
8600Sstevel@tonic-gate 		p=buf;
8610Sstevel@tonic-gate 		EVP_MD_CTX_init(&ctx);
8620Sstevel@tonic-gate 		EVP_SignInit_ex(&ctx,s->ctx->rsa_md5, NULL);
8630Sstevel@tonic-gate 		EVP_SignUpdate(&ctx,s->s2->key_material,
8640Sstevel@tonic-gate 			       s->s2->key_material_length);
8650Sstevel@tonic-gate 		EVP_SignUpdate(&ctx,cert_ch,(unsigned int)cert_ch_len);
8660Sstevel@tonic-gate 		n=i2d_X509(s->session->sess_cert->peer_key->x509,&p);
8670Sstevel@tonic-gate 		EVP_SignUpdate(&ctx,buf,(unsigned int)n);
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 		p=buf;
8700Sstevel@tonic-gate 		d=p+6;
8710Sstevel@tonic-gate 		*(p++)=SSL2_MT_CLIENT_CERTIFICATE;
8720Sstevel@tonic-gate 		*(p++)=SSL2_CT_X509_CERTIFICATE;
8730Sstevel@tonic-gate 		n=i2d_X509(s->cert->key->x509,&d);
8740Sstevel@tonic-gate 		s2n(n,p);
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 		if (!EVP_SignFinal(&ctx,d,&n,s->cert->key->privatekey))
8770Sstevel@tonic-gate 			{
8780Sstevel@tonic-gate 			/* this is not good.  If things have failed it
8790Sstevel@tonic-gate 			 * means there so something wrong with the key.
8800Sstevel@tonic-gate 			 * We will continue with a 0 length signature
8810Sstevel@tonic-gate 			 */
8820Sstevel@tonic-gate 			}
8830Sstevel@tonic-gate 		EVP_MD_CTX_cleanup(&ctx);
8840Sstevel@tonic-gate 		s2n(n,p);
8850Sstevel@tonic-gate 		d+=n;
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 		s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_D;
8880Sstevel@tonic-gate 		s->init_num=d-buf;
8890Sstevel@tonic-gate 		s->init_off=0;
8900Sstevel@tonic-gate 		}
8910Sstevel@tonic-gate 	/* if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_D) */
8920Sstevel@tonic-gate 	return(ssl2_do_write(s));
8930Sstevel@tonic-gate 	}
8940Sstevel@tonic-gate 
get_server_verify(SSL * s)8950Sstevel@tonic-gate static int get_server_verify(SSL *s)
8960Sstevel@tonic-gate 	{
8970Sstevel@tonic-gate 	unsigned char *p;
8980Sstevel@tonic-gate 	int i, n, len;
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 	p=(unsigned char *)s->init_buf->data;
9010Sstevel@tonic-gate 	if (s->state == SSL2_ST_GET_SERVER_VERIFY_A)
9020Sstevel@tonic-gate 		{
9030Sstevel@tonic-gate 		i=ssl2_read(s,(char *)&(p[s->init_num]),1-s->init_num);
9040Sstevel@tonic-gate 		if (i < (1-s->init_num))
9050Sstevel@tonic-gate 			return(ssl2_part_read(s,SSL_F_GET_SERVER_VERIFY,i));
9060Sstevel@tonic-gate 		s->init_num += i;
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 		s->state= SSL2_ST_GET_SERVER_VERIFY_B;
9090Sstevel@tonic-gate 		if (*p != SSL2_MT_SERVER_VERIFY)
9100Sstevel@tonic-gate 			{
9110Sstevel@tonic-gate 			if (p[0] != SSL2_MT_ERROR)
9120Sstevel@tonic-gate 				{
9130Sstevel@tonic-gate 				ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
9140Sstevel@tonic-gate 				SSLerr(SSL_F_GET_SERVER_VERIFY,
9150Sstevel@tonic-gate 					SSL_R_READ_WRONG_PACKET_TYPE);
9160Sstevel@tonic-gate 				}
9170Sstevel@tonic-gate 			else
9180Sstevel@tonic-gate 				{
9190Sstevel@tonic-gate 				SSLerr(SSL_F_GET_SERVER_VERIFY,SSL_R_PEER_ERROR);
9200Sstevel@tonic-gate 				/* try to read the error message */
9210Sstevel@tonic-gate 				i=ssl2_read(s,(char *)&(p[s->init_num]),3-s->init_num);
9220Sstevel@tonic-gate 				return ssl2_part_read(s,SSL_F_GET_SERVER_VERIFY,i);
9230Sstevel@tonic-gate 				}
9240Sstevel@tonic-gate 			return(-1);
9250Sstevel@tonic-gate 			}
9260Sstevel@tonic-gate 		}
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	p=(unsigned char *)s->init_buf->data;
9290Sstevel@tonic-gate 	len = 1 + s->s2->challenge_length;
9300Sstevel@tonic-gate 	n =  len - s->init_num;
9310Sstevel@tonic-gate 	i = ssl2_read(s,(char *)&(p[s->init_num]),n);
9320Sstevel@tonic-gate 	if (i < n)
9330Sstevel@tonic-gate 		return(ssl2_part_read(s,SSL_F_GET_SERVER_VERIFY,i));
9340Sstevel@tonic-gate 	if (s->msg_callback)
9350Sstevel@tonic-gate 		s->msg_callback(0, s->version, 0, p, len, s, s->msg_callback_arg); /* SERVER-VERIFY */
9360Sstevel@tonic-gate 	p += 1;
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	if (memcmp(p,s->s2->challenge,s->s2->challenge_length) != 0)
9390Sstevel@tonic-gate 		{
9400Sstevel@tonic-gate 		ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
9410Sstevel@tonic-gate 		SSLerr(SSL_F_GET_SERVER_VERIFY,SSL_R_CHALLENGE_IS_DIFFERENT);
9420Sstevel@tonic-gate 		return(-1);
9430Sstevel@tonic-gate 		}
9440Sstevel@tonic-gate 	return(1);
9450Sstevel@tonic-gate 	}
9460Sstevel@tonic-gate 
get_server_finished(SSL * s)9470Sstevel@tonic-gate static int get_server_finished(SSL *s)
9480Sstevel@tonic-gate 	{
9490Sstevel@tonic-gate 	unsigned char *buf;
9500Sstevel@tonic-gate 	unsigned char *p;
9510Sstevel@tonic-gate 	int i, n, len;
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 	buf=(unsigned char *)s->init_buf->data;
9540Sstevel@tonic-gate 	p=buf;
9550Sstevel@tonic-gate 	if (s->state == SSL2_ST_GET_SERVER_FINISHED_A)
9560Sstevel@tonic-gate 		{
9570Sstevel@tonic-gate 		i=ssl2_read(s,(char *)&(buf[s->init_num]),1-s->init_num);
9580Sstevel@tonic-gate 		if (i < (1-s->init_num))
9590Sstevel@tonic-gate 			return(ssl2_part_read(s,SSL_F_GET_SERVER_FINISHED,i));
9600Sstevel@tonic-gate 		s->init_num += i;
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 		if (*p == SSL2_MT_REQUEST_CERTIFICATE)
9630Sstevel@tonic-gate 			{
9640Sstevel@tonic-gate 			s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_A;
9650Sstevel@tonic-gate 			return(1);
9660Sstevel@tonic-gate 			}
9670Sstevel@tonic-gate 		else if (*p != SSL2_MT_SERVER_FINISHED)
9680Sstevel@tonic-gate 			{
9690Sstevel@tonic-gate 			if (p[0] != SSL2_MT_ERROR)
9700Sstevel@tonic-gate 				{
9710Sstevel@tonic-gate 				ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
9720Sstevel@tonic-gate 				SSLerr(SSL_F_GET_SERVER_FINISHED,SSL_R_READ_WRONG_PACKET_TYPE);
9730Sstevel@tonic-gate 				}
9740Sstevel@tonic-gate 			else
9750Sstevel@tonic-gate 				{
9760Sstevel@tonic-gate 				SSLerr(SSL_F_GET_SERVER_FINISHED,SSL_R_PEER_ERROR);
9770Sstevel@tonic-gate 				/* try to read the error message */
9780Sstevel@tonic-gate 				i=ssl2_read(s,(char *)&(p[s->init_num]),3-s->init_num);
9790Sstevel@tonic-gate 				return ssl2_part_read(s,SSL_F_GET_SERVER_VERIFY,i);
9800Sstevel@tonic-gate 				}
9810Sstevel@tonic-gate 			return(-1);
9820Sstevel@tonic-gate 			}
9830Sstevel@tonic-gate 		s->state=SSL2_ST_GET_SERVER_FINISHED_B;
9840Sstevel@tonic-gate 		}
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 	len = 1 + SSL2_SSL_SESSION_ID_LENGTH;
9870Sstevel@tonic-gate 	n = len - s->init_num;
9880Sstevel@tonic-gate 	i = ssl2_read(s,(char *)&(buf[s->init_num]), n);
9890Sstevel@tonic-gate 	if (i < n) /* XXX could be shorter than SSL2_SSL_SESSION_ID_LENGTH, that's the maximum */
9900Sstevel@tonic-gate 		return(ssl2_part_read(s,SSL_F_GET_SERVER_FINISHED,i));
9910Sstevel@tonic-gate 	s->init_num += i;
9920Sstevel@tonic-gate 	if (s->msg_callback)
9930Sstevel@tonic-gate 		s->msg_callback(0, s->version, 0, buf, (size_t)s->init_num, s, s->msg_callback_arg); /* SERVER-FINISHED */
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 	if (!s->hit) /* new session */
9960Sstevel@tonic-gate 		{
9970Sstevel@tonic-gate 		/* new session-id */
9980Sstevel@tonic-gate 		/* Make sure we were not trying to re-use an old SSL_SESSION
9990Sstevel@tonic-gate 		 * or bad things can happen */
10000Sstevel@tonic-gate 		/* ZZZZZZZZZZZZZ */
10010Sstevel@tonic-gate 		s->session->session_id_length=SSL2_SSL_SESSION_ID_LENGTH;
10020Sstevel@tonic-gate 		memcpy(s->session->session_id,p+1,SSL2_SSL_SESSION_ID_LENGTH);
10030Sstevel@tonic-gate 		}
10040Sstevel@tonic-gate 	else
10050Sstevel@tonic-gate 		{
10060Sstevel@tonic-gate 		if (!(s->options & SSL_OP_MICROSOFT_SESS_ID_BUG))
10070Sstevel@tonic-gate 			{
10080Sstevel@tonic-gate 			if ((s->session->session_id_length > sizeof s->session->session_id)
10090Sstevel@tonic-gate 			    || (0 != memcmp(buf + 1, s->session->session_id,
10100Sstevel@tonic-gate 			                    (unsigned int)s->session->session_id_length)))
10110Sstevel@tonic-gate 				{
10120Sstevel@tonic-gate 				ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
10130Sstevel@tonic-gate 				SSLerr(SSL_F_GET_SERVER_FINISHED,SSL_R_SSL_SESSION_ID_IS_DIFFERENT);
10140Sstevel@tonic-gate 				return(-1);
10150Sstevel@tonic-gate 				}
10160Sstevel@tonic-gate 			}
10170Sstevel@tonic-gate 		}
10180Sstevel@tonic-gate 	s->state = SSL_ST_OK;
10190Sstevel@tonic-gate 	return(1);
10200Sstevel@tonic-gate 	}
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate /* loads in the certificate from the server */
ssl2_set_certificate(SSL * s,int type,int len,const unsigned char * data)10232139Sjp161948 int ssl2_set_certificate(SSL *s, int type, int len, const unsigned char *data)
10240Sstevel@tonic-gate 	{
10250Sstevel@tonic-gate 	STACK_OF(X509) *sk=NULL;
10260Sstevel@tonic-gate 	EVP_PKEY *pkey=NULL;
10270Sstevel@tonic-gate 	SESS_CERT *sc=NULL;
10280Sstevel@tonic-gate 	int i;
10290Sstevel@tonic-gate 	X509 *x509=NULL;
10300Sstevel@tonic-gate 	int ret=0;
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	x509=d2i_X509(NULL,&data,(long)len);
10330Sstevel@tonic-gate 	if (x509 == NULL)
10340Sstevel@tonic-gate 		{
10350Sstevel@tonic-gate 		SSLerr(SSL_F_SSL2_SET_CERTIFICATE,ERR_R_X509_LIB);
10360Sstevel@tonic-gate 		goto err;
10370Sstevel@tonic-gate 		}
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 	if ((sk=sk_X509_new_null()) == NULL || !sk_X509_push(sk,x509))
10400Sstevel@tonic-gate 		{
10410Sstevel@tonic-gate 		SSLerr(SSL_F_SSL2_SET_CERTIFICATE,ERR_R_MALLOC_FAILURE);
10420Sstevel@tonic-gate 		goto err;
10430Sstevel@tonic-gate 		}
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate 	i=ssl_verify_cert_chain(s,sk);
10460Sstevel@tonic-gate 
1047*8545SJan.Pechanec@Sun.COM 	if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0))
10480Sstevel@tonic-gate 		{
10490Sstevel@tonic-gate 		SSLerr(SSL_F_SSL2_SET_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
10500Sstevel@tonic-gate 		goto err;
10510Sstevel@tonic-gate 		}
10520Sstevel@tonic-gate 	ERR_clear_error(); /* but we keep s->verify_result */
10530Sstevel@tonic-gate 	s->session->verify_result = s->verify_result;
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 	/* server's cert for this session */
10560Sstevel@tonic-gate 	sc=ssl_sess_cert_new();
10570Sstevel@tonic-gate 	if (sc == NULL)
10580Sstevel@tonic-gate 		{
10590Sstevel@tonic-gate 		ret= -1;
10600Sstevel@tonic-gate 		goto err;
10610Sstevel@tonic-gate 		}
10620Sstevel@tonic-gate 	if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
10630Sstevel@tonic-gate 	s->session->sess_cert=sc;
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate 	sc->peer_pkeys[SSL_PKEY_RSA_ENC].x509=x509;
10660Sstevel@tonic-gate 	sc->peer_key= &(sc->peer_pkeys[SSL_PKEY_RSA_ENC]);
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 	pkey=X509_get_pubkey(x509);
10690Sstevel@tonic-gate 	x509=NULL;
10700Sstevel@tonic-gate 	if (pkey == NULL)
10710Sstevel@tonic-gate 		{
10720Sstevel@tonic-gate 		SSLerr(SSL_F_SSL2_SET_CERTIFICATE,SSL_R_UNABLE_TO_EXTRACT_PUBLIC_KEY);
10730Sstevel@tonic-gate 		goto err;
10740Sstevel@tonic-gate 		}
10750Sstevel@tonic-gate 	if (pkey->type != EVP_PKEY_RSA)
10760Sstevel@tonic-gate 		{
10770Sstevel@tonic-gate 		SSLerr(SSL_F_SSL2_SET_CERTIFICATE,SSL_R_PUBLIC_KEY_NOT_RSA);
10780Sstevel@tonic-gate 		goto err;
10790Sstevel@tonic-gate 		}
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 	if (!ssl_set_peer_cert_type(sc,SSL2_CT_X509_CERTIFICATE))
10820Sstevel@tonic-gate 		goto err;
10830Sstevel@tonic-gate 	ret=1;
10840Sstevel@tonic-gate err:
10850Sstevel@tonic-gate 	sk_X509_free(sk);
10860Sstevel@tonic-gate 	X509_free(x509);
10870Sstevel@tonic-gate 	EVP_PKEY_free(pkey);
10880Sstevel@tonic-gate 	return(ret);
10890Sstevel@tonic-gate 	}
10900Sstevel@tonic-gate 
ssl_rsa_public_encrypt(SESS_CERT * sc,int len,unsigned char * from,unsigned char * to,int padding)10910Sstevel@tonic-gate static int ssl_rsa_public_encrypt(SESS_CERT *sc, int len, unsigned char *from,
10920Sstevel@tonic-gate 	     unsigned char *to, int padding)
10930Sstevel@tonic-gate 	{
10940Sstevel@tonic-gate 	EVP_PKEY *pkey=NULL;
10950Sstevel@tonic-gate 	int i= -1;
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 	if ((sc == NULL) || (sc->peer_key->x509 == NULL) ||
10980Sstevel@tonic-gate 		((pkey=X509_get_pubkey(sc->peer_key->x509)) == NULL))
10990Sstevel@tonic-gate 		{
11000Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT,SSL_R_NO_PUBLICKEY);
11010Sstevel@tonic-gate 		return(-1);
11020Sstevel@tonic-gate 		}
11030Sstevel@tonic-gate 	if (pkey->type != EVP_PKEY_RSA)
11040Sstevel@tonic-gate 		{
11050Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT,SSL_R_PUBLIC_KEY_IS_NOT_RSA);
11060Sstevel@tonic-gate 		goto end;
11070Sstevel@tonic-gate 		}
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 	/* we have the public key */
11100Sstevel@tonic-gate 	i=RSA_public_encrypt(len,from,to,pkey->pkey.rsa,padding);
11110Sstevel@tonic-gate 	if (i < 0)
11120Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT,ERR_R_RSA_LIB);
11130Sstevel@tonic-gate end:
11140Sstevel@tonic-gate 	EVP_PKEY_free(pkey);
11150Sstevel@tonic-gate 	return(i);
11160Sstevel@tonic-gate 	}
11170Sstevel@tonic-gate #else /* !OPENSSL_NO_SSL2 */
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate # if PEDANTIC
11200Sstevel@tonic-gate static void *dummy=&dummy;
11210Sstevel@tonic-gate # endif
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate #endif
1124