xref: /onnv-gate/usr/src/common/openssl/ssl/s2_clnt.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* ssl/s2_clnt.c */
2*0Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3*0Sstevel@tonic-gate  * All rights reserved.
4*0Sstevel@tonic-gate  *
5*0Sstevel@tonic-gate  * This package is an SSL implementation written
6*0Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
7*0Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
10*0Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
11*0Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
12*0Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13*0Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
14*0Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15*0Sstevel@tonic-gate  *
16*0Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
17*0Sstevel@tonic-gate  * the code are not to be removed.
18*0Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
19*0Sstevel@tonic-gate  * as the author of the parts of the library used.
20*0Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
21*0Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
22*0Sstevel@tonic-gate  *
23*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
24*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
25*0Sstevel@tonic-gate  * are met:
26*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
27*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
28*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
29*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
30*0Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
31*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
32*0Sstevel@tonic-gate  *    must display the following acknowledgement:
33*0Sstevel@tonic-gate  *    "This product includes cryptographic software written by
34*0Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
35*0Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
36*0Sstevel@tonic-gate  *    being used are not cryptographic related :-).
37*0Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
38*0Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
39*0Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42*0Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44*0Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45*0Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46*0Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47*0Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49*0Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50*0Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51*0Sstevel@tonic-gate  * SUCH DAMAGE.
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
54*0Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55*0Sstevel@tonic-gate  * copied and put under another distribution licence
56*0Sstevel@tonic-gate  * [including the GNU Public Licence.]
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate /* ====================================================================
59*0Sstevel@tonic-gate  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
60*0Sstevel@tonic-gate  *
61*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
62*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
63*0Sstevel@tonic-gate  * are met:
64*0Sstevel@tonic-gate  *
65*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
66*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
67*0Sstevel@tonic-gate  *
68*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
69*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
70*0Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
71*0Sstevel@tonic-gate  *    distribution.
72*0Sstevel@tonic-gate  *
73*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
74*0Sstevel@tonic-gate  *    software must display the following acknowledgment:
75*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
76*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77*0Sstevel@tonic-gate  *
78*0Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79*0Sstevel@tonic-gate  *    endorse or promote products derived from this software without
80*0Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
81*0Sstevel@tonic-gate  *    openssl-core@openssl.org.
82*0Sstevel@tonic-gate  *
83*0Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
84*0Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
85*0Sstevel@tonic-gate  *    permission of the OpenSSL Project.
86*0Sstevel@tonic-gate  *
87*0Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
88*0Sstevel@tonic-gate  *    acknowledgment:
89*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
90*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91*0Sstevel@tonic-gate  *
92*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93*0Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95*0Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96*0Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97*0Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98*0Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99*0Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101*0Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102*0Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103*0Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
104*0Sstevel@tonic-gate  * ====================================================================
105*0Sstevel@tonic-gate  *
106*0Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
107*0Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
108*0Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
109*0Sstevel@tonic-gate  *
110*0Sstevel@tonic-gate  */
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate #include "ssl_locl.h"
113*0Sstevel@tonic-gate #ifndef OPENSSL_NO_SSL2
114*0Sstevel@tonic-gate #include <stdio.h>
115*0Sstevel@tonic-gate #include <openssl/rand.h>
116*0Sstevel@tonic-gate #include <openssl/buffer.h>
117*0Sstevel@tonic-gate #include <openssl/objects.h>
118*0Sstevel@tonic-gate #include <openssl/evp.h>
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate static SSL_METHOD *ssl2_get_client_method(int ver);
121*0Sstevel@tonic-gate static int get_server_finished(SSL *s);
122*0Sstevel@tonic-gate static int get_server_verify(SSL *s);
123*0Sstevel@tonic-gate static int get_server_hello(SSL *s);
124*0Sstevel@tonic-gate static int client_hello(SSL *s);
125*0Sstevel@tonic-gate static int client_master_key(SSL *s);
126*0Sstevel@tonic-gate static int client_finished(SSL *s);
127*0Sstevel@tonic-gate static int client_certificate(SSL *s);
128*0Sstevel@tonic-gate static int ssl_rsa_public_encrypt(SESS_CERT *sc, int len, unsigned char *from,
129*0Sstevel@tonic-gate 	unsigned char *to,int padding);
130*0Sstevel@tonic-gate #define BREAK	break
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate static SSL_METHOD *ssl2_get_client_method(int ver)
133*0Sstevel@tonic-gate 	{
134*0Sstevel@tonic-gate 	if (ver == SSL2_VERSION)
135*0Sstevel@tonic-gate 		return(SSLv2_client_method());
136*0Sstevel@tonic-gate 	else
137*0Sstevel@tonic-gate 		return(NULL);
138*0Sstevel@tonic-gate 	}
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate SSL_METHOD *SSLv2_client_method(void)
141*0Sstevel@tonic-gate 	{
142*0Sstevel@tonic-gate 	static int init=1;
143*0Sstevel@tonic-gate 	static SSL_METHOD SSLv2_client_data;
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	if (init)
146*0Sstevel@tonic-gate 		{
147*0Sstevel@tonic-gate 		CRYPTO_w_lock(CRYPTO_LOCK_SSL_METHOD);
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 		if (init)
150*0Sstevel@tonic-gate 			{
151*0Sstevel@tonic-gate 			memcpy((char *)&SSLv2_client_data,(char *)sslv2_base_method(),
152*0Sstevel@tonic-gate 				sizeof(SSL_METHOD));
153*0Sstevel@tonic-gate 			SSLv2_client_data.ssl_connect=ssl2_connect;
154*0Sstevel@tonic-gate 			SSLv2_client_data.get_ssl_method=ssl2_get_client_method;
155*0Sstevel@tonic-gate 			init=0;
156*0Sstevel@tonic-gate 			}
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 		CRYPTO_w_unlock(CRYPTO_LOCK_SSL_METHOD);
159*0Sstevel@tonic-gate 		}
160*0Sstevel@tonic-gate 	return(&SSLv2_client_data);
161*0Sstevel@tonic-gate 	}
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate int ssl2_connect(SSL *s)
164*0Sstevel@tonic-gate 	{
165*0Sstevel@tonic-gate 	unsigned long l=time(NULL);
166*0Sstevel@tonic-gate 	BUF_MEM *buf=NULL;
167*0Sstevel@tonic-gate 	int ret= -1;
168*0Sstevel@tonic-gate 	void (*cb)(const SSL *ssl,int type,int val)=NULL;
169*0Sstevel@tonic-gate 	int new_state,state;
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	RAND_add(&l,sizeof(l),0);
172*0Sstevel@tonic-gate 	ERR_clear_error();
173*0Sstevel@tonic-gate 	clear_sys_error();
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 	if (s->info_callback != NULL)
176*0Sstevel@tonic-gate 		cb=s->info_callback;
177*0Sstevel@tonic-gate 	else if (s->ctx->info_callback != NULL)
178*0Sstevel@tonic-gate 		cb=s->ctx->info_callback;
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	/* init things to blank */
181*0Sstevel@tonic-gate 	s->in_handshake++;
182*0Sstevel@tonic-gate 	if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	for (;;)
185*0Sstevel@tonic-gate 		{
186*0Sstevel@tonic-gate 		state=s->state;
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 		switch (s->state)
189*0Sstevel@tonic-gate 			{
190*0Sstevel@tonic-gate 		case SSL_ST_BEFORE:
191*0Sstevel@tonic-gate 		case SSL_ST_CONNECT:
192*0Sstevel@tonic-gate 		case SSL_ST_BEFORE|SSL_ST_CONNECT:
193*0Sstevel@tonic-gate 		case SSL_ST_OK|SSL_ST_CONNECT:
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 			s->server=0;
196*0Sstevel@tonic-gate 			if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 			s->version=SSL2_VERSION;
199*0Sstevel@tonic-gate 			s->type=SSL_ST_CONNECT;
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 			buf=s->init_buf;
202*0Sstevel@tonic-gate 			if ((buf == NULL) && ((buf=BUF_MEM_new()) == NULL))
203*0Sstevel@tonic-gate 				{
204*0Sstevel@tonic-gate 				ret= -1;
205*0Sstevel@tonic-gate 				goto end;
206*0Sstevel@tonic-gate 				}
207*0Sstevel@tonic-gate 			if (!BUF_MEM_grow(buf,
208*0Sstevel@tonic-gate 				SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))
209*0Sstevel@tonic-gate 				{
210*0Sstevel@tonic-gate 				if (buf == s->init_buf)
211*0Sstevel@tonic-gate 					buf=NULL;
212*0Sstevel@tonic-gate 				ret= -1;
213*0Sstevel@tonic-gate 				goto end;
214*0Sstevel@tonic-gate 				}
215*0Sstevel@tonic-gate 			s->init_buf=buf;
216*0Sstevel@tonic-gate 			buf=NULL;
217*0Sstevel@tonic-gate 			s->init_num=0;
218*0Sstevel@tonic-gate 			s->state=SSL2_ST_SEND_CLIENT_HELLO_A;
219*0Sstevel@tonic-gate 			s->ctx->stats.sess_connect++;
220*0Sstevel@tonic-gate 			s->handshake_func=ssl2_connect;
221*0Sstevel@tonic-gate 			BREAK;
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 		case SSL2_ST_SEND_CLIENT_HELLO_A:
224*0Sstevel@tonic-gate 		case SSL2_ST_SEND_CLIENT_HELLO_B:
225*0Sstevel@tonic-gate 			s->shutdown=0;
226*0Sstevel@tonic-gate 			ret=client_hello(s);
227*0Sstevel@tonic-gate 			if (ret <= 0) goto end;
228*0Sstevel@tonic-gate 			s->init_num=0;
229*0Sstevel@tonic-gate 			s->state=SSL2_ST_GET_SERVER_HELLO_A;
230*0Sstevel@tonic-gate 			BREAK;
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 		case SSL2_ST_GET_SERVER_HELLO_A:
233*0Sstevel@tonic-gate 		case SSL2_ST_GET_SERVER_HELLO_B:
234*0Sstevel@tonic-gate 			ret=get_server_hello(s);
235*0Sstevel@tonic-gate 			if (ret <= 0) goto end;
236*0Sstevel@tonic-gate 			s->init_num=0;
237*0Sstevel@tonic-gate 			if (!s->hit) /* new session */
238*0Sstevel@tonic-gate 				{
239*0Sstevel@tonic-gate 				s->state=SSL2_ST_SEND_CLIENT_MASTER_KEY_A;
240*0Sstevel@tonic-gate 				BREAK;
241*0Sstevel@tonic-gate 				}
242*0Sstevel@tonic-gate 			else
243*0Sstevel@tonic-gate 				{
244*0Sstevel@tonic-gate 				s->state=SSL2_ST_CLIENT_START_ENCRYPTION;
245*0Sstevel@tonic-gate 				break;
246*0Sstevel@tonic-gate 				}
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 		case SSL2_ST_SEND_CLIENT_MASTER_KEY_A:
249*0Sstevel@tonic-gate 		case SSL2_ST_SEND_CLIENT_MASTER_KEY_B:
250*0Sstevel@tonic-gate 			ret=client_master_key(s);
251*0Sstevel@tonic-gate 			if (ret <= 0) goto end;
252*0Sstevel@tonic-gate 			s->init_num=0;
253*0Sstevel@tonic-gate 			s->state=SSL2_ST_CLIENT_START_ENCRYPTION;
254*0Sstevel@tonic-gate 			break;
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 		case SSL2_ST_CLIENT_START_ENCRYPTION:
257*0Sstevel@tonic-gate 			/* Ok, we now have all the stuff needed to
258*0Sstevel@tonic-gate 			 * start encrypting, so lets fire it up :-) */
259*0Sstevel@tonic-gate 			if (!ssl2_enc_init(s,1))
260*0Sstevel@tonic-gate 				{
261*0Sstevel@tonic-gate 				ret= -1;
262*0Sstevel@tonic-gate 				goto end;
263*0Sstevel@tonic-gate 				}
264*0Sstevel@tonic-gate 			s->s2->clear_text=0;
265*0Sstevel@tonic-gate 			s->state=SSL2_ST_SEND_CLIENT_FINISHED_A;
266*0Sstevel@tonic-gate 			break;
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate 		case SSL2_ST_SEND_CLIENT_FINISHED_A:
269*0Sstevel@tonic-gate 		case SSL2_ST_SEND_CLIENT_FINISHED_B:
270*0Sstevel@tonic-gate 			ret=client_finished(s);
271*0Sstevel@tonic-gate 			if (ret <= 0) goto end;
272*0Sstevel@tonic-gate 			s->init_num=0;
273*0Sstevel@tonic-gate 			s->state=SSL2_ST_GET_SERVER_VERIFY_A;
274*0Sstevel@tonic-gate 			break;
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate 		case SSL2_ST_GET_SERVER_VERIFY_A:
277*0Sstevel@tonic-gate 		case SSL2_ST_GET_SERVER_VERIFY_B:
278*0Sstevel@tonic-gate 			ret=get_server_verify(s);
279*0Sstevel@tonic-gate 			if (ret <= 0) goto end;
280*0Sstevel@tonic-gate 			s->init_num=0;
281*0Sstevel@tonic-gate 			s->state=SSL2_ST_GET_SERVER_FINISHED_A;
282*0Sstevel@tonic-gate 			break;
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate 		case SSL2_ST_GET_SERVER_FINISHED_A:
285*0Sstevel@tonic-gate 		case SSL2_ST_GET_SERVER_FINISHED_B:
286*0Sstevel@tonic-gate 			ret=get_server_finished(s);
287*0Sstevel@tonic-gate 			if (ret <= 0) goto end;
288*0Sstevel@tonic-gate 			break;
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate 		case SSL2_ST_SEND_CLIENT_CERTIFICATE_A:
291*0Sstevel@tonic-gate 		case SSL2_ST_SEND_CLIENT_CERTIFICATE_B:
292*0Sstevel@tonic-gate 		case SSL2_ST_SEND_CLIENT_CERTIFICATE_C:
293*0Sstevel@tonic-gate 		case SSL2_ST_SEND_CLIENT_CERTIFICATE_D:
294*0Sstevel@tonic-gate 		case SSL2_ST_X509_GET_CLIENT_CERTIFICATE:
295*0Sstevel@tonic-gate 			ret=client_certificate(s);
296*0Sstevel@tonic-gate 			if (ret <= 0) goto end;
297*0Sstevel@tonic-gate 			s->init_num=0;
298*0Sstevel@tonic-gate 			s->state=SSL2_ST_GET_SERVER_FINISHED_A;
299*0Sstevel@tonic-gate 			break;
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate 		case SSL_ST_OK:
302*0Sstevel@tonic-gate 			if (s->init_buf != NULL)
303*0Sstevel@tonic-gate 				{
304*0Sstevel@tonic-gate 				BUF_MEM_free(s->init_buf);
305*0Sstevel@tonic-gate 				s->init_buf=NULL;
306*0Sstevel@tonic-gate 				}
307*0Sstevel@tonic-gate 			s->init_num=0;
308*0Sstevel@tonic-gate 		/*	ERR_clear_error();*/
309*0Sstevel@tonic-gate 
310*0Sstevel@tonic-gate 			/* If we want to cache session-ids in the client
311*0Sstevel@tonic-gate 			 * and we successfully add the session-id to the
312*0Sstevel@tonic-gate 			 * cache, and there is a callback, then pass it out.
313*0Sstevel@tonic-gate 			 * 26/11/96 - eay - only add if not a re-used session.
314*0Sstevel@tonic-gate 			 */
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate 			ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
317*0Sstevel@tonic-gate 			if (s->hit) s->ctx->stats.sess_hit++;
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 			ret=1;
320*0Sstevel@tonic-gate 			/* s->server=0; */
321*0Sstevel@tonic-gate 			s->ctx->stats.sess_connect_good++;
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 			if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate 			goto end;
326*0Sstevel@tonic-gate 			/* break; */
327*0Sstevel@tonic-gate 		default:
328*0Sstevel@tonic-gate 			SSLerr(SSL_F_SSL2_CONNECT,SSL_R_UNKNOWN_STATE);
329*0Sstevel@tonic-gate 			return(-1);
330*0Sstevel@tonic-gate 			/* break; */
331*0Sstevel@tonic-gate 			}
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate 		if ((cb != NULL) && (s->state != state))
334*0Sstevel@tonic-gate 			{
335*0Sstevel@tonic-gate 			new_state=s->state;
336*0Sstevel@tonic-gate 			s->state=state;
337*0Sstevel@tonic-gate 			cb(s,SSL_CB_CONNECT_LOOP,1);
338*0Sstevel@tonic-gate 			s->state=new_state;
339*0Sstevel@tonic-gate 			}
340*0Sstevel@tonic-gate 		}
341*0Sstevel@tonic-gate end:
342*0Sstevel@tonic-gate 	s->in_handshake--;
343*0Sstevel@tonic-gate 	if (buf != NULL)
344*0Sstevel@tonic-gate 		BUF_MEM_free(buf);
345*0Sstevel@tonic-gate 	if (cb != NULL)
346*0Sstevel@tonic-gate 		cb(s,SSL_CB_CONNECT_EXIT,ret);
347*0Sstevel@tonic-gate 	return(ret);
348*0Sstevel@tonic-gate 	}
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate static int get_server_hello(SSL *s)
351*0Sstevel@tonic-gate 	{
352*0Sstevel@tonic-gate 	unsigned char *buf;
353*0Sstevel@tonic-gate 	unsigned char *p;
354*0Sstevel@tonic-gate 	int i,j;
355*0Sstevel@tonic-gate 	unsigned long len;
356*0Sstevel@tonic-gate 	STACK_OF(SSL_CIPHER) *sk=NULL,*cl, *prio, *allow;
357*0Sstevel@tonic-gate 
358*0Sstevel@tonic-gate 	buf=(unsigned char *)s->init_buf->data;
359*0Sstevel@tonic-gate 	p=buf;
360*0Sstevel@tonic-gate 	if (s->state == SSL2_ST_GET_SERVER_HELLO_A)
361*0Sstevel@tonic-gate 		{
362*0Sstevel@tonic-gate 		i=ssl2_read(s,(char *)&(buf[s->init_num]),11-s->init_num);
363*0Sstevel@tonic-gate 		if (i < (11-s->init_num))
364*0Sstevel@tonic-gate 			return(ssl2_part_read(s,SSL_F_GET_SERVER_HELLO,i));
365*0Sstevel@tonic-gate 		s->init_num = 11;
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate 		if (*(p++) != SSL2_MT_SERVER_HELLO)
368*0Sstevel@tonic-gate 			{
369*0Sstevel@tonic-gate 			if (p[-1] != SSL2_MT_ERROR)
370*0Sstevel@tonic-gate 				{
371*0Sstevel@tonic-gate 				ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
372*0Sstevel@tonic-gate 				SSLerr(SSL_F_GET_SERVER_HELLO,
373*0Sstevel@tonic-gate 					SSL_R_READ_WRONG_PACKET_TYPE);
374*0Sstevel@tonic-gate 				}
375*0Sstevel@tonic-gate 			else
376*0Sstevel@tonic-gate 				SSLerr(SSL_F_GET_SERVER_HELLO,
377*0Sstevel@tonic-gate 					SSL_R_PEER_ERROR);
378*0Sstevel@tonic-gate 			return(-1);
379*0Sstevel@tonic-gate 			}
380*0Sstevel@tonic-gate #ifdef __APPLE_CC__
381*0Sstevel@tonic-gate 		/* The Rhapsody 5.5 (a.k.a. MacOS X) compiler bug
382*0Sstevel@tonic-gate 		 * workaround. <appro@fy.chalmers.se> */
383*0Sstevel@tonic-gate 		s->hit=(i=*(p++))?1:0;
384*0Sstevel@tonic-gate #else
385*0Sstevel@tonic-gate 		s->hit=(*(p++))?1:0;
386*0Sstevel@tonic-gate #endif
387*0Sstevel@tonic-gate 		s->s2->tmp.cert_type= *(p++);
388*0Sstevel@tonic-gate 		n2s(p,i);
389*0Sstevel@tonic-gate 		if (i < s->version) s->version=i;
390*0Sstevel@tonic-gate 		n2s(p,i); s->s2->tmp.cert_length=i;
391*0Sstevel@tonic-gate 		n2s(p,i); s->s2->tmp.csl=i;
392*0Sstevel@tonic-gate 		n2s(p,i); s->s2->tmp.conn_id_length=i;
393*0Sstevel@tonic-gate 		s->state=SSL2_ST_GET_SERVER_HELLO_B;
394*0Sstevel@tonic-gate 		}
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 	/* SSL2_ST_GET_SERVER_HELLO_B */
397*0Sstevel@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;
398*0Sstevel@tonic-gate 	if (len > SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)
399*0Sstevel@tonic-gate 		{
400*0Sstevel@tonic-gate 		SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_MESSAGE_TOO_LONG);
401*0Sstevel@tonic-gate 		return -1;
402*0Sstevel@tonic-gate 		}
403*0Sstevel@tonic-gate 	j = (int)len - s->init_num;
404*0Sstevel@tonic-gate 	i = ssl2_read(s,(char *)&(buf[s->init_num]),j);
405*0Sstevel@tonic-gate 	if (i != j) return(ssl2_part_read(s,SSL_F_GET_SERVER_HELLO,i));
406*0Sstevel@tonic-gate 	if (s->msg_callback)
407*0Sstevel@tonic-gate 		s->msg_callback(0, s->version, 0, buf, (size_t)len, s, s->msg_callback_arg); /* SERVER-HELLO */
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate 	/* things are looking good */
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate 	p = buf + 11;
412*0Sstevel@tonic-gate 	if (s->hit)
413*0Sstevel@tonic-gate 		{
414*0Sstevel@tonic-gate 		if (s->s2->tmp.cert_length != 0)
415*0Sstevel@tonic-gate 			{
416*0Sstevel@tonic-gate 			SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_REUSE_CERT_LENGTH_NOT_ZERO);
417*0Sstevel@tonic-gate 			return(-1);
418*0Sstevel@tonic-gate 			}
419*0Sstevel@tonic-gate 		if (s->s2->tmp.cert_type != 0)
420*0Sstevel@tonic-gate 			{
421*0Sstevel@tonic-gate 			if (!(s->options &
422*0Sstevel@tonic-gate 				SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG))
423*0Sstevel@tonic-gate 				{
424*0Sstevel@tonic-gate 				SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_REUSE_CERT_TYPE_NOT_ZERO);
425*0Sstevel@tonic-gate 				return(-1);
426*0Sstevel@tonic-gate 				}
427*0Sstevel@tonic-gate 			}
428*0Sstevel@tonic-gate 		if (s->s2->tmp.csl != 0)
429*0Sstevel@tonic-gate 			{
430*0Sstevel@tonic-gate 			SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_REUSE_CIPHER_LIST_NOT_ZERO);
431*0Sstevel@tonic-gate 			return(-1);
432*0Sstevel@tonic-gate 			}
433*0Sstevel@tonic-gate 		}
434*0Sstevel@tonic-gate 	else
435*0Sstevel@tonic-gate 		{
436*0Sstevel@tonic-gate #ifdef undef
437*0Sstevel@tonic-gate 		/* very bad */
438*0Sstevel@tonic-gate 		memset(s->session->session_id,0,
439*0Sstevel@tonic-gate 			SSL_MAX_SSL_SESSION_ID_LENGTH_IN_BYTES);
440*0Sstevel@tonic-gate 		s->session->session_id_length=0;
441*0Sstevel@tonic-gate 		*/
442*0Sstevel@tonic-gate #endif
443*0Sstevel@tonic-gate 
444*0Sstevel@tonic-gate 		/* we need to do this in case we were trying to reuse a
445*0Sstevel@tonic-gate 		 * client session but others are already reusing it.
446*0Sstevel@tonic-gate 		 * If this was a new 'blank' session ID, the session-id
447*0Sstevel@tonic-gate 		 * length will still be 0 */
448*0Sstevel@tonic-gate 		if (s->session->session_id_length > 0)
449*0Sstevel@tonic-gate 			{
450*0Sstevel@tonic-gate 			if (!ssl_get_new_session(s,0))
451*0Sstevel@tonic-gate 				{
452*0Sstevel@tonic-gate 				ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
453*0Sstevel@tonic-gate 				return(-1);
454*0Sstevel@tonic-gate 				}
455*0Sstevel@tonic-gate 			}
456*0Sstevel@tonic-gate 
457*0Sstevel@tonic-gate 		if (ssl2_set_certificate(s,s->s2->tmp.cert_type,
458*0Sstevel@tonic-gate 			s->s2->tmp.cert_length,p) <= 0)
459*0Sstevel@tonic-gate 			{
460*0Sstevel@tonic-gate 			ssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE);
461*0Sstevel@tonic-gate 			return(-1);
462*0Sstevel@tonic-gate 			}
463*0Sstevel@tonic-gate 		p+=s->s2->tmp.cert_length;
464*0Sstevel@tonic-gate 
465*0Sstevel@tonic-gate 		if (s->s2->tmp.csl == 0)
466*0Sstevel@tonic-gate 			{
467*0Sstevel@tonic-gate 			ssl2_return_error(s,SSL2_PE_NO_CIPHER);
468*0Sstevel@tonic-gate 			SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_NO_CIPHER_LIST);
469*0Sstevel@tonic-gate 			return(-1);
470*0Sstevel@tonic-gate 			}
471*0Sstevel@tonic-gate 
472*0Sstevel@tonic-gate 		/* We have just received a list of ciphers back from the
473*0Sstevel@tonic-gate 		 * server.  We need to get the ones that match, then select
474*0Sstevel@tonic-gate 		 * the one we want the most :-). */
475*0Sstevel@tonic-gate 
476*0Sstevel@tonic-gate 		/* load the ciphers */
477*0Sstevel@tonic-gate 		sk=ssl_bytes_to_cipher_list(s,p,s->s2->tmp.csl,
478*0Sstevel@tonic-gate 					    &s->session->ciphers);
479*0Sstevel@tonic-gate 		p+=s->s2->tmp.csl;
480*0Sstevel@tonic-gate 		if (sk == NULL)
481*0Sstevel@tonic-gate 			{
482*0Sstevel@tonic-gate 			ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
483*0Sstevel@tonic-gate 			SSLerr(SSL_F_GET_SERVER_HELLO,ERR_R_MALLOC_FAILURE);
484*0Sstevel@tonic-gate 			return(-1);
485*0Sstevel@tonic-gate 			}
486*0Sstevel@tonic-gate 
487*0Sstevel@tonic-gate 		sk_SSL_CIPHER_set_cmp_func(sk,ssl_cipher_ptr_id_cmp);
488*0Sstevel@tonic-gate 
489*0Sstevel@tonic-gate 		/* get the array of ciphers we will accept */
490*0Sstevel@tonic-gate 		cl=SSL_get_ciphers(s);
491*0Sstevel@tonic-gate 		sk_SSL_CIPHER_set_cmp_func(cl,ssl_cipher_ptr_id_cmp);
492*0Sstevel@tonic-gate 
493*0Sstevel@tonic-gate 		/*
494*0Sstevel@tonic-gate 		 * If server preference flag set, choose the first
495*0Sstevel@tonic-gate 		 * (highest priority) cipher the server sends, otherwise
496*0Sstevel@tonic-gate 		 * client preference has priority.
497*0Sstevel@tonic-gate 		 */
498*0Sstevel@tonic-gate 		if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE)
499*0Sstevel@tonic-gate 		    {
500*0Sstevel@tonic-gate 		    prio = sk;
501*0Sstevel@tonic-gate 		    allow = cl;
502*0Sstevel@tonic-gate 		    }
503*0Sstevel@tonic-gate 		else
504*0Sstevel@tonic-gate 		    {
505*0Sstevel@tonic-gate 		    prio = cl;
506*0Sstevel@tonic-gate 		    allow = sk;
507*0Sstevel@tonic-gate 		    }
508*0Sstevel@tonic-gate 		/* In theory we could have ciphers sent back that we
509*0Sstevel@tonic-gate 		 * don't want to use but that does not matter since we
510*0Sstevel@tonic-gate 		 * will check against the list we originally sent and
511*0Sstevel@tonic-gate 		 * for performance reasons we should not bother to match
512*0Sstevel@tonic-gate 		 * the two lists up just to check. */
513*0Sstevel@tonic-gate 		for (i=0; i<sk_SSL_CIPHER_num(prio); i++)
514*0Sstevel@tonic-gate 			{
515*0Sstevel@tonic-gate 			if (sk_SSL_CIPHER_find(allow,
516*0Sstevel@tonic-gate 					     sk_SSL_CIPHER_value(prio,i)) >= 0)
517*0Sstevel@tonic-gate 				break;
518*0Sstevel@tonic-gate 			}
519*0Sstevel@tonic-gate 
520*0Sstevel@tonic-gate 		if (i >= sk_SSL_CIPHER_num(prio))
521*0Sstevel@tonic-gate 			{
522*0Sstevel@tonic-gate 			ssl2_return_error(s,SSL2_PE_NO_CIPHER);
523*0Sstevel@tonic-gate 			SSLerr(SSL_F_GET_SERVER_HELLO,SSL_R_NO_CIPHER_MATCH);
524*0Sstevel@tonic-gate 			return(-1);
525*0Sstevel@tonic-gate 			}
526*0Sstevel@tonic-gate 		s->session->cipher=sk_SSL_CIPHER_value(prio,i);
527*0Sstevel@tonic-gate 
528*0Sstevel@tonic-gate 
529*0Sstevel@tonic-gate 		if (s->session->peer != NULL) /* can't happen*/
530*0Sstevel@tonic-gate 			{
531*0Sstevel@tonic-gate 			ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
532*0Sstevel@tonic-gate 			SSLerr(SSL_F_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
533*0Sstevel@tonic-gate 			return(-1);
534*0Sstevel@tonic-gate 			}
535*0Sstevel@tonic-gate 
536*0Sstevel@tonic-gate 		s->session->peer = s->session->sess_cert->peer_key->x509;
537*0Sstevel@tonic-gate 		/* peer_key->x509 has been set by ssl2_set_certificate. */
538*0Sstevel@tonic-gate 		CRYPTO_add(&s->session->peer->references, 1, CRYPTO_LOCK_X509);
539*0Sstevel@tonic-gate 		}
540*0Sstevel@tonic-gate 
541*0Sstevel@tonic-gate 	if (s->session->peer != s->session->sess_cert->peer_key->x509)
542*0Sstevel@tonic-gate 		/* can't happen */
543*0Sstevel@tonic-gate 		{
544*0Sstevel@tonic-gate 		ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
545*0Sstevel@tonic-gate 		SSLerr(SSL_F_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
546*0Sstevel@tonic-gate 		return(-1);
547*0Sstevel@tonic-gate 		}
548*0Sstevel@tonic-gate 
549*0Sstevel@tonic-gate 	s->s2->conn_id_length=s->s2->tmp.conn_id_length;
550*0Sstevel@tonic-gate 	if (s->s2->conn_id_length > sizeof s->s2->conn_id)
551*0Sstevel@tonic-gate 		{
552*0Sstevel@tonic-gate 		ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
553*0Sstevel@tonic-gate 		SSLerr(SSL_F_GET_SERVER_HELLO, SSL_R_SSL2_CONNECTION_ID_TOO_LONG);
554*0Sstevel@tonic-gate 		return -1;
555*0Sstevel@tonic-gate 		}
556*0Sstevel@tonic-gate 	memcpy(s->s2->conn_id,p,s->s2->tmp.conn_id_length);
557*0Sstevel@tonic-gate 	return(1);
558*0Sstevel@tonic-gate 	}
559*0Sstevel@tonic-gate 
560*0Sstevel@tonic-gate static int client_hello(SSL *s)
561*0Sstevel@tonic-gate 	{
562*0Sstevel@tonic-gate 	unsigned char *buf;
563*0Sstevel@tonic-gate 	unsigned char *p,*d;
564*0Sstevel@tonic-gate /*	CIPHER **cipher;*/
565*0Sstevel@tonic-gate 	int i,n,j;
566*0Sstevel@tonic-gate 
567*0Sstevel@tonic-gate 	buf=(unsigned char *)s->init_buf->data;
568*0Sstevel@tonic-gate 	if (s->state == SSL2_ST_SEND_CLIENT_HELLO_A)
569*0Sstevel@tonic-gate 		{
570*0Sstevel@tonic-gate 		if ((s->session == NULL) ||
571*0Sstevel@tonic-gate 			(s->session->ssl_version != s->version))
572*0Sstevel@tonic-gate 			{
573*0Sstevel@tonic-gate 			if (!ssl_get_new_session(s,0))
574*0Sstevel@tonic-gate 				{
575*0Sstevel@tonic-gate 				ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
576*0Sstevel@tonic-gate 				return(-1);
577*0Sstevel@tonic-gate 				}
578*0Sstevel@tonic-gate 			}
579*0Sstevel@tonic-gate 		/* else use the pre-loaded session */
580*0Sstevel@tonic-gate 
581*0Sstevel@tonic-gate 		p=buf;					/* header */
582*0Sstevel@tonic-gate 		d=p+9;					/* data section */
583*0Sstevel@tonic-gate 		*(p++)=SSL2_MT_CLIENT_HELLO;		/* type */
584*0Sstevel@tonic-gate 		s2n(SSL2_VERSION,p);			/* version */
585*0Sstevel@tonic-gate 		n=j=0;
586*0Sstevel@tonic-gate 
587*0Sstevel@tonic-gate 		n=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),d);
588*0Sstevel@tonic-gate 		d+=n;
589*0Sstevel@tonic-gate 
590*0Sstevel@tonic-gate 		if (n == 0)
591*0Sstevel@tonic-gate 			{
592*0Sstevel@tonic-gate 			SSLerr(SSL_F_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
593*0Sstevel@tonic-gate 			return(-1);
594*0Sstevel@tonic-gate 			}
595*0Sstevel@tonic-gate 
596*0Sstevel@tonic-gate 		s2n(n,p);			/* cipher spec num bytes */
597*0Sstevel@tonic-gate 
598*0Sstevel@tonic-gate 		if ((s->session->session_id_length > 0) &&
599*0Sstevel@tonic-gate 			(s->session->session_id_length <=
600*0Sstevel@tonic-gate 			SSL2_MAX_SSL_SESSION_ID_LENGTH))
601*0Sstevel@tonic-gate 			{
602*0Sstevel@tonic-gate 			i=s->session->session_id_length;
603*0Sstevel@tonic-gate 			s2n(i,p);		/* session id length */
604*0Sstevel@tonic-gate 			memcpy(d,s->session->session_id,(unsigned int)i);
605*0Sstevel@tonic-gate 			d+=i;
606*0Sstevel@tonic-gate 			}
607*0Sstevel@tonic-gate 		else
608*0Sstevel@tonic-gate 			{
609*0Sstevel@tonic-gate 			s2n(0,p);
610*0Sstevel@tonic-gate 			}
611*0Sstevel@tonic-gate 
612*0Sstevel@tonic-gate 		s->s2->challenge_length=SSL2_CHALLENGE_LENGTH;
613*0Sstevel@tonic-gate 		s2n(SSL2_CHALLENGE_LENGTH,p);		/* challenge length */
614*0Sstevel@tonic-gate 		/*challenge id data*/
615*0Sstevel@tonic-gate 		RAND_pseudo_bytes(s->s2->challenge,SSL2_CHALLENGE_LENGTH);
616*0Sstevel@tonic-gate 		memcpy(d,s->s2->challenge,SSL2_CHALLENGE_LENGTH);
617*0Sstevel@tonic-gate 		d+=SSL2_CHALLENGE_LENGTH;
618*0Sstevel@tonic-gate 
619*0Sstevel@tonic-gate 		s->state=SSL2_ST_SEND_CLIENT_HELLO_B;
620*0Sstevel@tonic-gate 		s->init_num=d-buf;
621*0Sstevel@tonic-gate 		s->init_off=0;
622*0Sstevel@tonic-gate 		}
623*0Sstevel@tonic-gate 	/* SSL2_ST_SEND_CLIENT_HELLO_B */
624*0Sstevel@tonic-gate 	return(ssl2_do_write(s));
625*0Sstevel@tonic-gate 	}
626*0Sstevel@tonic-gate 
627*0Sstevel@tonic-gate static int client_master_key(SSL *s)
628*0Sstevel@tonic-gate 	{
629*0Sstevel@tonic-gate 	unsigned char *buf;
630*0Sstevel@tonic-gate 	unsigned char *p,*d;
631*0Sstevel@tonic-gate 	int clear,enc,karg,i;
632*0Sstevel@tonic-gate 	SSL_SESSION *sess;
633*0Sstevel@tonic-gate 	const EVP_CIPHER *c;
634*0Sstevel@tonic-gate 	const EVP_MD *md;
635*0Sstevel@tonic-gate 
636*0Sstevel@tonic-gate 	buf=(unsigned char *)s->init_buf->data;
637*0Sstevel@tonic-gate 	if (s->state == SSL2_ST_SEND_CLIENT_MASTER_KEY_A)
638*0Sstevel@tonic-gate 		{
639*0Sstevel@tonic-gate 
640*0Sstevel@tonic-gate 		if (!ssl_cipher_get_evp(s->session,&c,&md,NULL))
641*0Sstevel@tonic-gate 			{
642*0Sstevel@tonic-gate 			ssl2_return_error(s,SSL2_PE_NO_CIPHER);
643*0Sstevel@tonic-gate 			SSLerr(SSL_F_CLIENT_MASTER_KEY,SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS);
644*0Sstevel@tonic-gate 			return(-1);
645*0Sstevel@tonic-gate 			}
646*0Sstevel@tonic-gate 		sess=s->session;
647*0Sstevel@tonic-gate 		p=buf;
648*0Sstevel@tonic-gate 		d=p+10;
649*0Sstevel@tonic-gate 		*(p++)=SSL2_MT_CLIENT_MASTER_KEY;/* type */
650*0Sstevel@tonic-gate 
651*0Sstevel@tonic-gate 		i=ssl_put_cipher_by_char(s,sess->cipher,p);
652*0Sstevel@tonic-gate 		p+=i;
653*0Sstevel@tonic-gate 
654*0Sstevel@tonic-gate 		/* make key_arg data */
655*0Sstevel@tonic-gate 		i=EVP_CIPHER_iv_length(c);
656*0Sstevel@tonic-gate 		sess->key_arg_length=i;
657*0Sstevel@tonic-gate 		if (i > SSL_MAX_KEY_ARG_LENGTH)
658*0Sstevel@tonic-gate 			{
659*0Sstevel@tonic-gate 			ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
660*0Sstevel@tonic-gate 			SSLerr(SSL_F_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR);
661*0Sstevel@tonic-gate 			return -1;
662*0Sstevel@tonic-gate 			}
663*0Sstevel@tonic-gate 		if (i > 0) RAND_pseudo_bytes(sess->key_arg,i);
664*0Sstevel@tonic-gate 
665*0Sstevel@tonic-gate 		/* make a master key */
666*0Sstevel@tonic-gate 		i=EVP_CIPHER_key_length(c);
667*0Sstevel@tonic-gate 		sess->master_key_length=i;
668*0Sstevel@tonic-gate 		if (i > 0)
669*0Sstevel@tonic-gate 			{
670*0Sstevel@tonic-gate 			if (i > sizeof sess->master_key)
671*0Sstevel@tonic-gate 				{
672*0Sstevel@tonic-gate 				ssl2_return_error(s, SSL2_PE_UNDEFINED_ERROR);
673*0Sstevel@tonic-gate 				SSLerr(SSL_F_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR);
674*0Sstevel@tonic-gate 				return -1;
675*0Sstevel@tonic-gate 				}
676*0Sstevel@tonic-gate 			if (RAND_bytes(sess->master_key,i) <= 0)
677*0Sstevel@tonic-gate 				{
678*0Sstevel@tonic-gate 				ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
679*0Sstevel@tonic-gate 				return(-1);
680*0Sstevel@tonic-gate 				}
681*0Sstevel@tonic-gate 			}
682*0Sstevel@tonic-gate 
683*0Sstevel@tonic-gate 		if (sess->cipher->algorithm2 & SSL2_CF_8_BYTE_ENC)
684*0Sstevel@tonic-gate 			enc=8;
685*0Sstevel@tonic-gate 		else if (SSL_C_IS_EXPORT(sess->cipher))
686*0Sstevel@tonic-gate 			enc=5;
687*0Sstevel@tonic-gate 		else
688*0Sstevel@tonic-gate 			enc=i;
689*0Sstevel@tonic-gate 
690*0Sstevel@tonic-gate 		if (i < enc)
691*0Sstevel@tonic-gate 			{
692*0Sstevel@tonic-gate 			ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
693*0Sstevel@tonic-gate 			SSLerr(SSL_F_CLIENT_MASTER_KEY,SSL_R_CIPHER_TABLE_SRC_ERROR);
694*0Sstevel@tonic-gate 			return(-1);
695*0Sstevel@tonic-gate 			}
696*0Sstevel@tonic-gate 		clear=i-enc;
697*0Sstevel@tonic-gate 		s2n(clear,p);
698*0Sstevel@tonic-gate 		memcpy(d,sess->master_key,(unsigned int)clear);
699*0Sstevel@tonic-gate 		d+=clear;
700*0Sstevel@tonic-gate 
701*0Sstevel@tonic-gate 		enc=ssl_rsa_public_encrypt(sess->sess_cert,enc,
702*0Sstevel@tonic-gate 			&(sess->master_key[clear]),d,
703*0Sstevel@tonic-gate 			(s->s2->ssl2_rollback)?RSA_SSLV23_PADDING:RSA_PKCS1_PADDING);
704*0Sstevel@tonic-gate 		if (enc <= 0)
705*0Sstevel@tonic-gate 			{
706*0Sstevel@tonic-gate 			ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
707*0Sstevel@tonic-gate 			SSLerr(SSL_F_CLIENT_MASTER_KEY,SSL_R_PUBLIC_KEY_ENCRYPT_ERROR);
708*0Sstevel@tonic-gate 			return(-1);
709*0Sstevel@tonic-gate 			}
710*0Sstevel@tonic-gate #ifdef PKCS1_CHECK
711*0Sstevel@tonic-gate 		if (s->options & SSL_OP_PKCS1_CHECK_1) d[1]++;
712*0Sstevel@tonic-gate 		if (s->options & SSL_OP_PKCS1_CHECK_2)
713*0Sstevel@tonic-gate 			sess->master_key[clear]++;
714*0Sstevel@tonic-gate #endif
715*0Sstevel@tonic-gate 		s2n(enc,p);
716*0Sstevel@tonic-gate 		d+=enc;
717*0Sstevel@tonic-gate 		karg=sess->key_arg_length;
718*0Sstevel@tonic-gate 		s2n(karg,p); /* key arg size */
719*0Sstevel@tonic-gate 		if (karg > sizeof sess->key_arg)
720*0Sstevel@tonic-gate 			{
721*0Sstevel@tonic-gate 			ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
722*0Sstevel@tonic-gate 			SSLerr(SSL_F_CLIENT_MASTER_KEY, ERR_R_INTERNAL_ERROR);
723*0Sstevel@tonic-gate 			return -1;
724*0Sstevel@tonic-gate 			}
725*0Sstevel@tonic-gate 		memcpy(d,sess->key_arg,(unsigned int)karg);
726*0Sstevel@tonic-gate 		d+=karg;
727*0Sstevel@tonic-gate 
728*0Sstevel@tonic-gate 		s->state=SSL2_ST_SEND_CLIENT_MASTER_KEY_B;
729*0Sstevel@tonic-gate 		s->init_num=d-buf;
730*0Sstevel@tonic-gate 		s->init_off=0;
731*0Sstevel@tonic-gate 		}
732*0Sstevel@tonic-gate 
733*0Sstevel@tonic-gate 	/* SSL2_ST_SEND_CLIENT_MASTER_KEY_B */
734*0Sstevel@tonic-gate 	return(ssl2_do_write(s));
735*0Sstevel@tonic-gate 	}
736*0Sstevel@tonic-gate 
737*0Sstevel@tonic-gate static int client_finished(SSL *s)
738*0Sstevel@tonic-gate 	{
739*0Sstevel@tonic-gate 	unsigned char *p;
740*0Sstevel@tonic-gate 
741*0Sstevel@tonic-gate 	if (s->state == SSL2_ST_SEND_CLIENT_FINISHED_A)
742*0Sstevel@tonic-gate 		{
743*0Sstevel@tonic-gate 		p=(unsigned char *)s->init_buf->data;
744*0Sstevel@tonic-gate 		*(p++)=SSL2_MT_CLIENT_FINISHED;
745*0Sstevel@tonic-gate 		if (s->s2->conn_id_length > sizeof s->s2->conn_id)
746*0Sstevel@tonic-gate 			{
747*0Sstevel@tonic-gate 			SSLerr(SSL_F_CLIENT_FINISHED, ERR_R_INTERNAL_ERROR);
748*0Sstevel@tonic-gate 			return -1;
749*0Sstevel@tonic-gate 			}
750*0Sstevel@tonic-gate 		memcpy(p,s->s2->conn_id,(unsigned int)s->s2->conn_id_length);
751*0Sstevel@tonic-gate 
752*0Sstevel@tonic-gate 		s->state=SSL2_ST_SEND_CLIENT_FINISHED_B;
753*0Sstevel@tonic-gate 		s->init_num=s->s2->conn_id_length+1;
754*0Sstevel@tonic-gate 		s->init_off=0;
755*0Sstevel@tonic-gate 		}
756*0Sstevel@tonic-gate 	return(ssl2_do_write(s));
757*0Sstevel@tonic-gate 	}
758*0Sstevel@tonic-gate 
759*0Sstevel@tonic-gate /* read the data and then respond */
760*0Sstevel@tonic-gate static int client_certificate(SSL *s)
761*0Sstevel@tonic-gate 	{
762*0Sstevel@tonic-gate 	unsigned char *buf;
763*0Sstevel@tonic-gate 	unsigned char *p,*d;
764*0Sstevel@tonic-gate 	int i;
765*0Sstevel@tonic-gate 	unsigned int n;
766*0Sstevel@tonic-gate 	int cert_ch_len;
767*0Sstevel@tonic-gate 	unsigned char *cert_ch;
768*0Sstevel@tonic-gate 
769*0Sstevel@tonic-gate 	buf=(unsigned char *)s->init_buf->data;
770*0Sstevel@tonic-gate 
771*0Sstevel@tonic-gate 	/* We have a cert associated with the SSL, so attach it to
772*0Sstevel@tonic-gate 	 * the session if it does not have one */
773*0Sstevel@tonic-gate 
774*0Sstevel@tonic-gate 	if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_A)
775*0Sstevel@tonic-gate 		{
776*0Sstevel@tonic-gate 		i=ssl2_read(s,(char *)&(buf[s->init_num]),
777*0Sstevel@tonic-gate 			SSL2_MAX_CERT_CHALLENGE_LENGTH+2-s->init_num);
778*0Sstevel@tonic-gate 		if (i<(SSL2_MIN_CERT_CHALLENGE_LENGTH+2-s->init_num))
779*0Sstevel@tonic-gate 			return(ssl2_part_read(s,SSL_F_CLIENT_CERTIFICATE,i));
780*0Sstevel@tonic-gate 		s->init_num += i;
781*0Sstevel@tonic-gate 		if (s->msg_callback)
782*0Sstevel@tonic-gate 			s->msg_callback(0, s->version, 0, buf, (size_t)s->init_num, s, s->msg_callback_arg); /* REQUEST-CERTIFICATE */
783*0Sstevel@tonic-gate 
784*0Sstevel@tonic-gate 		/* type=buf[0]; */
785*0Sstevel@tonic-gate 		/* type eq x509 */
786*0Sstevel@tonic-gate 		if (buf[1] != SSL2_AT_MD5_WITH_RSA_ENCRYPTION)
787*0Sstevel@tonic-gate 			{
788*0Sstevel@tonic-gate 			ssl2_return_error(s,SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE);
789*0Sstevel@tonic-gate 			SSLerr(SSL_F_CLIENT_CERTIFICATE,SSL_R_BAD_AUTHENTICATION_TYPE);
790*0Sstevel@tonic-gate 			return(-1);
791*0Sstevel@tonic-gate 			}
792*0Sstevel@tonic-gate 
793*0Sstevel@tonic-gate 		if ((s->cert == NULL) ||
794*0Sstevel@tonic-gate 			(s->cert->key->x509 == NULL) ||
795*0Sstevel@tonic-gate 			(s->cert->key->privatekey == NULL))
796*0Sstevel@tonic-gate 			{
797*0Sstevel@tonic-gate 			s->state=SSL2_ST_X509_GET_CLIENT_CERTIFICATE;
798*0Sstevel@tonic-gate 			}
799*0Sstevel@tonic-gate 		else
800*0Sstevel@tonic-gate 			s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_C;
801*0Sstevel@tonic-gate 		}
802*0Sstevel@tonic-gate 
803*0Sstevel@tonic-gate 	cert_ch = buf + 2;
804*0Sstevel@tonic-gate 	cert_ch_len = s->init_num - 2;
805*0Sstevel@tonic-gate 
806*0Sstevel@tonic-gate 	if (s->state == SSL2_ST_X509_GET_CLIENT_CERTIFICATE)
807*0Sstevel@tonic-gate 		{
808*0Sstevel@tonic-gate 		X509 *x509=NULL;
809*0Sstevel@tonic-gate 		EVP_PKEY *pkey=NULL;
810*0Sstevel@tonic-gate 
811*0Sstevel@tonic-gate 		/* If we get an error we need to
812*0Sstevel@tonic-gate 		 * ssl->rwstate=SSL_X509_LOOKUP;
813*0Sstevel@tonic-gate 		 * return(error);
814*0Sstevel@tonic-gate 		 * We should then be retried when things are ok and we
815*0Sstevel@tonic-gate 		 * can get a cert or not */
816*0Sstevel@tonic-gate 
817*0Sstevel@tonic-gate 		i=0;
818*0Sstevel@tonic-gate 		if (s->ctx->client_cert_cb != NULL)
819*0Sstevel@tonic-gate 			{
820*0Sstevel@tonic-gate 			i=s->ctx->client_cert_cb(s,&(x509),&(pkey));
821*0Sstevel@tonic-gate 			}
822*0Sstevel@tonic-gate 
823*0Sstevel@tonic-gate 		if (i < 0)
824*0Sstevel@tonic-gate 			{
825*0Sstevel@tonic-gate 			s->rwstate=SSL_X509_LOOKUP;
826*0Sstevel@tonic-gate 			return(-1);
827*0Sstevel@tonic-gate 			}
828*0Sstevel@tonic-gate 		s->rwstate=SSL_NOTHING;
829*0Sstevel@tonic-gate 
830*0Sstevel@tonic-gate 		if ((i == 1) && (pkey != NULL) && (x509 != NULL))
831*0Sstevel@tonic-gate 			{
832*0Sstevel@tonic-gate 			s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_C;
833*0Sstevel@tonic-gate 			if (	!SSL_use_certificate(s,x509) ||
834*0Sstevel@tonic-gate 				!SSL_use_PrivateKey(s,pkey))
835*0Sstevel@tonic-gate 				{
836*0Sstevel@tonic-gate 				i=0;
837*0Sstevel@tonic-gate 				}
838*0Sstevel@tonic-gate 			X509_free(x509);
839*0Sstevel@tonic-gate 			EVP_PKEY_free(pkey);
840*0Sstevel@tonic-gate 			}
841*0Sstevel@tonic-gate 		else if (i == 1)
842*0Sstevel@tonic-gate 			{
843*0Sstevel@tonic-gate 			if (x509 != NULL) X509_free(x509);
844*0Sstevel@tonic-gate 			if (pkey != NULL) EVP_PKEY_free(pkey);
845*0Sstevel@tonic-gate 			SSLerr(SSL_F_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
846*0Sstevel@tonic-gate 			i=0;
847*0Sstevel@tonic-gate 			}
848*0Sstevel@tonic-gate 
849*0Sstevel@tonic-gate 		if (i == 0)
850*0Sstevel@tonic-gate 			{
851*0Sstevel@tonic-gate 			/* We have no client certificate to respond with
852*0Sstevel@tonic-gate 			 * so send the correct error message back */
853*0Sstevel@tonic-gate 			s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_B;
854*0Sstevel@tonic-gate 			p=buf;
855*0Sstevel@tonic-gate 			*(p++)=SSL2_MT_ERROR;
856*0Sstevel@tonic-gate 			s2n(SSL2_PE_NO_CERTIFICATE,p);
857*0Sstevel@tonic-gate 			s->init_off=0;
858*0Sstevel@tonic-gate 			s->init_num=3;
859*0Sstevel@tonic-gate 			/* Write is done at the end */
860*0Sstevel@tonic-gate 			}
861*0Sstevel@tonic-gate 		}
862*0Sstevel@tonic-gate 
863*0Sstevel@tonic-gate 	if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_B)
864*0Sstevel@tonic-gate 		{
865*0Sstevel@tonic-gate 		return(ssl2_do_write(s));
866*0Sstevel@tonic-gate 		}
867*0Sstevel@tonic-gate 
868*0Sstevel@tonic-gate 	if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_C)
869*0Sstevel@tonic-gate 		{
870*0Sstevel@tonic-gate 		EVP_MD_CTX ctx;
871*0Sstevel@tonic-gate 
872*0Sstevel@tonic-gate 		/* ok, now we calculate the checksum
873*0Sstevel@tonic-gate 		 * do it first so we can reuse buf :-) */
874*0Sstevel@tonic-gate 		p=buf;
875*0Sstevel@tonic-gate 		EVP_MD_CTX_init(&ctx);
876*0Sstevel@tonic-gate 		EVP_SignInit_ex(&ctx,s->ctx->rsa_md5, NULL);
877*0Sstevel@tonic-gate 		EVP_SignUpdate(&ctx,s->s2->key_material,
878*0Sstevel@tonic-gate 			       s->s2->key_material_length);
879*0Sstevel@tonic-gate 		EVP_SignUpdate(&ctx,cert_ch,(unsigned int)cert_ch_len);
880*0Sstevel@tonic-gate 		n=i2d_X509(s->session->sess_cert->peer_key->x509,&p);
881*0Sstevel@tonic-gate 		EVP_SignUpdate(&ctx,buf,(unsigned int)n);
882*0Sstevel@tonic-gate 
883*0Sstevel@tonic-gate 		p=buf;
884*0Sstevel@tonic-gate 		d=p+6;
885*0Sstevel@tonic-gate 		*(p++)=SSL2_MT_CLIENT_CERTIFICATE;
886*0Sstevel@tonic-gate 		*(p++)=SSL2_CT_X509_CERTIFICATE;
887*0Sstevel@tonic-gate 		n=i2d_X509(s->cert->key->x509,&d);
888*0Sstevel@tonic-gate 		s2n(n,p);
889*0Sstevel@tonic-gate 
890*0Sstevel@tonic-gate 		if (!EVP_SignFinal(&ctx,d,&n,s->cert->key->privatekey))
891*0Sstevel@tonic-gate 			{
892*0Sstevel@tonic-gate 			/* this is not good.  If things have failed it
893*0Sstevel@tonic-gate 			 * means there so something wrong with the key.
894*0Sstevel@tonic-gate 			 * We will continue with a 0 length signature
895*0Sstevel@tonic-gate 			 */
896*0Sstevel@tonic-gate 			}
897*0Sstevel@tonic-gate 		EVP_MD_CTX_cleanup(&ctx);
898*0Sstevel@tonic-gate 		s2n(n,p);
899*0Sstevel@tonic-gate 		d+=n;
900*0Sstevel@tonic-gate 
901*0Sstevel@tonic-gate 		s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_D;
902*0Sstevel@tonic-gate 		s->init_num=d-buf;
903*0Sstevel@tonic-gate 		s->init_off=0;
904*0Sstevel@tonic-gate 		}
905*0Sstevel@tonic-gate 	/* if (s->state == SSL2_ST_SEND_CLIENT_CERTIFICATE_D) */
906*0Sstevel@tonic-gate 	return(ssl2_do_write(s));
907*0Sstevel@tonic-gate 	}
908*0Sstevel@tonic-gate 
909*0Sstevel@tonic-gate static int get_server_verify(SSL *s)
910*0Sstevel@tonic-gate 	{
911*0Sstevel@tonic-gate 	unsigned char *p;
912*0Sstevel@tonic-gate 	int i, n, len;
913*0Sstevel@tonic-gate 
914*0Sstevel@tonic-gate 	p=(unsigned char *)s->init_buf->data;
915*0Sstevel@tonic-gate 	if (s->state == SSL2_ST_GET_SERVER_VERIFY_A)
916*0Sstevel@tonic-gate 		{
917*0Sstevel@tonic-gate 		i=ssl2_read(s,(char *)&(p[s->init_num]),1-s->init_num);
918*0Sstevel@tonic-gate 		if (i < (1-s->init_num))
919*0Sstevel@tonic-gate 			return(ssl2_part_read(s,SSL_F_GET_SERVER_VERIFY,i));
920*0Sstevel@tonic-gate 		s->init_num += i;
921*0Sstevel@tonic-gate 
922*0Sstevel@tonic-gate 		s->state= SSL2_ST_GET_SERVER_VERIFY_B;
923*0Sstevel@tonic-gate 		if (*p != SSL2_MT_SERVER_VERIFY)
924*0Sstevel@tonic-gate 			{
925*0Sstevel@tonic-gate 			if (p[0] != SSL2_MT_ERROR)
926*0Sstevel@tonic-gate 				{
927*0Sstevel@tonic-gate 				ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
928*0Sstevel@tonic-gate 				SSLerr(SSL_F_GET_SERVER_VERIFY,
929*0Sstevel@tonic-gate 					SSL_R_READ_WRONG_PACKET_TYPE);
930*0Sstevel@tonic-gate 				}
931*0Sstevel@tonic-gate 			else
932*0Sstevel@tonic-gate 				{
933*0Sstevel@tonic-gate 				SSLerr(SSL_F_GET_SERVER_VERIFY,SSL_R_PEER_ERROR);
934*0Sstevel@tonic-gate 				/* try to read the error message */
935*0Sstevel@tonic-gate 				i=ssl2_read(s,(char *)&(p[s->init_num]),3-s->init_num);
936*0Sstevel@tonic-gate 				return ssl2_part_read(s,SSL_F_GET_SERVER_VERIFY,i);
937*0Sstevel@tonic-gate 				}
938*0Sstevel@tonic-gate 			return(-1);
939*0Sstevel@tonic-gate 			}
940*0Sstevel@tonic-gate 		}
941*0Sstevel@tonic-gate 
942*0Sstevel@tonic-gate 	p=(unsigned char *)s->init_buf->data;
943*0Sstevel@tonic-gate 	len = 1 + s->s2->challenge_length;
944*0Sstevel@tonic-gate 	n =  len - s->init_num;
945*0Sstevel@tonic-gate 	i = ssl2_read(s,(char *)&(p[s->init_num]),n);
946*0Sstevel@tonic-gate 	if (i < n)
947*0Sstevel@tonic-gate 		return(ssl2_part_read(s,SSL_F_GET_SERVER_VERIFY,i));
948*0Sstevel@tonic-gate 	if (s->msg_callback)
949*0Sstevel@tonic-gate 		s->msg_callback(0, s->version, 0, p, len, s, s->msg_callback_arg); /* SERVER-VERIFY */
950*0Sstevel@tonic-gate 	p += 1;
951*0Sstevel@tonic-gate 
952*0Sstevel@tonic-gate 	if (memcmp(p,s->s2->challenge,s->s2->challenge_length) != 0)
953*0Sstevel@tonic-gate 		{
954*0Sstevel@tonic-gate 		ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
955*0Sstevel@tonic-gate 		SSLerr(SSL_F_GET_SERVER_VERIFY,SSL_R_CHALLENGE_IS_DIFFERENT);
956*0Sstevel@tonic-gate 		return(-1);
957*0Sstevel@tonic-gate 		}
958*0Sstevel@tonic-gate 	return(1);
959*0Sstevel@tonic-gate 	}
960*0Sstevel@tonic-gate 
961*0Sstevel@tonic-gate static int get_server_finished(SSL *s)
962*0Sstevel@tonic-gate 	{
963*0Sstevel@tonic-gate 	unsigned char *buf;
964*0Sstevel@tonic-gate 	unsigned char *p;
965*0Sstevel@tonic-gate 	int i, n, len;
966*0Sstevel@tonic-gate 
967*0Sstevel@tonic-gate 	buf=(unsigned char *)s->init_buf->data;
968*0Sstevel@tonic-gate 	p=buf;
969*0Sstevel@tonic-gate 	if (s->state == SSL2_ST_GET_SERVER_FINISHED_A)
970*0Sstevel@tonic-gate 		{
971*0Sstevel@tonic-gate 		i=ssl2_read(s,(char *)&(buf[s->init_num]),1-s->init_num);
972*0Sstevel@tonic-gate 		if (i < (1-s->init_num))
973*0Sstevel@tonic-gate 			return(ssl2_part_read(s,SSL_F_GET_SERVER_FINISHED,i));
974*0Sstevel@tonic-gate 		s->init_num += i;
975*0Sstevel@tonic-gate 
976*0Sstevel@tonic-gate 		if (*p == SSL2_MT_REQUEST_CERTIFICATE)
977*0Sstevel@tonic-gate 			{
978*0Sstevel@tonic-gate 			s->state=SSL2_ST_SEND_CLIENT_CERTIFICATE_A;
979*0Sstevel@tonic-gate 			return(1);
980*0Sstevel@tonic-gate 			}
981*0Sstevel@tonic-gate 		else if (*p != SSL2_MT_SERVER_FINISHED)
982*0Sstevel@tonic-gate 			{
983*0Sstevel@tonic-gate 			if (p[0] != SSL2_MT_ERROR)
984*0Sstevel@tonic-gate 				{
985*0Sstevel@tonic-gate 				ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
986*0Sstevel@tonic-gate 				SSLerr(SSL_F_GET_SERVER_FINISHED,SSL_R_READ_WRONG_PACKET_TYPE);
987*0Sstevel@tonic-gate 				}
988*0Sstevel@tonic-gate 			else
989*0Sstevel@tonic-gate 				{
990*0Sstevel@tonic-gate 				SSLerr(SSL_F_GET_SERVER_FINISHED,SSL_R_PEER_ERROR);
991*0Sstevel@tonic-gate 				/* try to read the error message */
992*0Sstevel@tonic-gate 				i=ssl2_read(s,(char *)&(p[s->init_num]),3-s->init_num);
993*0Sstevel@tonic-gate 				return ssl2_part_read(s,SSL_F_GET_SERVER_VERIFY,i);
994*0Sstevel@tonic-gate 				}
995*0Sstevel@tonic-gate 			return(-1);
996*0Sstevel@tonic-gate 			}
997*0Sstevel@tonic-gate 		s->state=SSL2_ST_GET_SERVER_FINISHED_B;
998*0Sstevel@tonic-gate 		}
999*0Sstevel@tonic-gate 
1000*0Sstevel@tonic-gate 	len = 1 + SSL2_SSL_SESSION_ID_LENGTH;
1001*0Sstevel@tonic-gate 	n = len - s->init_num;
1002*0Sstevel@tonic-gate 	i = ssl2_read(s,(char *)&(buf[s->init_num]), n);
1003*0Sstevel@tonic-gate 	if (i < n) /* XXX could be shorter than SSL2_SSL_SESSION_ID_LENGTH, that's the maximum */
1004*0Sstevel@tonic-gate 		return(ssl2_part_read(s,SSL_F_GET_SERVER_FINISHED,i));
1005*0Sstevel@tonic-gate 	s->init_num += i;
1006*0Sstevel@tonic-gate 	if (s->msg_callback)
1007*0Sstevel@tonic-gate 		s->msg_callback(0, s->version, 0, buf, (size_t)s->init_num, s, s->msg_callback_arg); /* SERVER-FINISHED */
1008*0Sstevel@tonic-gate 
1009*0Sstevel@tonic-gate 	if (!s->hit) /* new session */
1010*0Sstevel@tonic-gate 		{
1011*0Sstevel@tonic-gate 		/* new session-id */
1012*0Sstevel@tonic-gate 		/* Make sure we were not trying to re-use an old SSL_SESSION
1013*0Sstevel@tonic-gate 		 * or bad things can happen */
1014*0Sstevel@tonic-gate 		/* ZZZZZZZZZZZZZ */
1015*0Sstevel@tonic-gate 		s->session->session_id_length=SSL2_SSL_SESSION_ID_LENGTH;
1016*0Sstevel@tonic-gate 		memcpy(s->session->session_id,p+1,SSL2_SSL_SESSION_ID_LENGTH);
1017*0Sstevel@tonic-gate 		}
1018*0Sstevel@tonic-gate 	else
1019*0Sstevel@tonic-gate 		{
1020*0Sstevel@tonic-gate 		if (!(s->options & SSL_OP_MICROSOFT_SESS_ID_BUG))
1021*0Sstevel@tonic-gate 			{
1022*0Sstevel@tonic-gate 			if ((s->session->session_id_length > sizeof s->session->session_id)
1023*0Sstevel@tonic-gate 			    || (0 != memcmp(buf + 1, s->session->session_id,
1024*0Sstevel@tonic-gate 			                    (unsigned int)s->session->session_id_length)))
1025*0Sstevel@tonic-gate 				{
1026*0Sstevel@tonic-gate 				ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
1027*0Sstevel@tonic-gate 				SSLerr(SSL_F_GET_SERVER_FINISHED,SSL_R_SSL_SESSION_ID_IS_DIFFERENT);
1028*0Sstevel@tonic-gate 				return(-1);
1029*0Sstevel@tonic-gate 				}
1030*0Sstevel@tonic-gate 			}
1031*0Sstevel@tonic-gate 		}
1032*0Sstevel@tonic-gate 	s->state = SSL_ST_OK;
1033*0Sstevel@tonic-gate 	return(1);
1034*0Sstevel@tonic-gate 	}
1035*0Sstevel@tonic-gate 
1036*0Sstevel@tonic-gate /* loads in the certificate from the server */
1037*0Sstevel@tonic-gate int ssl2_set_certificate(SSL *s, int type, int len, unsigned char *data)
1038*0Sstevel@tonic-gate 	{
1039*0Sstevel@tonic-gate 	STACK_OF(X509) *sk=NULL;
1040*0Sstevel@tonic-gate 	EVP_PKEY *pkey=NULL;
1041*0Sstevel@tonic-gate 	SESS_CERT *sc=NULL;
1042*0Sstevel@tonic-gate 	int i;
1043*0Sstevel@tonic-gate 	X509 *x509=NULL;
1044*0Sstevel@tonic-gate 	int ret=0;
1045*0Sstevel@tonic-gate 
1046*0Sstevel@tonic-gate 	x509=d2i_X509(NULL,&data,(long)len);
1047*0Sstevel@tonic-gate 	if (x509 == NULL)
1048*0Sstevel@tonic-gate 		{
1049*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL2_SET_CERTIFICATE,ERR_R_X509_LIB);
1050*0Sstevel@tonic-gate 		goto err;
1051*0Sstevel@tonic-gate 		}
1052*0Sstevel@tonic-gate 
1053*0Sstevel@tonic-gate 	if ((sk=sk_X509_new_null()) == NULL || !sk_X509_push(sk,x509))
1054*0Sstevel@tonic-gate 		{
1055*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL2_SET_CERTIFICATE,ERR_R_MALLOC_FAILURE);
1056*0Sstevel@tonic-gate 		goto err;
1057*0Sstevel@tonic-gate 		}
1058*0Sstevel@tonic-gate 
1059*0Sstevel@tonic-gate 	i=ssl_verify_cert_chain(s,sk);
1060*0Sstevel@tonic-gate 
1061*0Sstevel@tonic-gate 	if ((s->verify_mode != SSL_VERIFY_NONE) && (!i))
1062*0Sstevel@tonic-gate 		{
1063*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL2_SET_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
1064*0Sstevel@tonic-gate 		goto err;
1065*0Sstevel@tonic-gate 		}
1066*0Sstevel@tonic-gate 	ERR_clear_error(); /* but we keep s->verify_result */
1067*0Sstevel@tonic-gate 	s->session->verify_result = s->verify_result;
1068*0Sstevel@tonic-gate 
1069*0Sstevel@tonic-gate 	/* server's cert for this session */
1070*0Sstevel@tonic-gate 	sc=ssl_sess_cert_new();
1071*0Sstevel@tonic-gate 	if (sc == NULL)
1072*0Sstevel@tonic-gate 		{
1073*0Sstevel@tonic-gate 		ret= -1;
1074*0Sstevel@tonic-gate 		goto err;
1075*0Sstevel@tonic-gate 		}
1076*0Sstevel@tonic-gate 	if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
1077*0Sstevel@tonic-gate 	s->session->sess_cert=sc;
1078*0Sstevel@tonic-gate 
1079*0Sstevel@tonic-gate 	sc->peer_pkeys[SSL_PKEY_RSA_ENC].x509=x509;
1080*0Sstevel@tonic-gate 	sc->peer_key= &(sc->peer_pkeys[SSL_PKEY_RSA_ENC]);
1081*0Sstevel@tonic-gate 
1082*0Sstevel@tonic-gate 	pkey=X509_get_pubkey(x509);
1083*0Sstevel@tonic-gate 	x509=NULL;
1084*0Sstevel@tonic-gate 	if (pkey == NULL)
1085*0Sstevel@tonic-gate 		{
1086*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL2_SET_CERTIFICATE,SSL_R_UNABLE_TO_EXTRACT_PUBLIC_KEY);
1087*0Sstevel@tonic-gate 		goto err;
1088*0Sstevel@tonic-gate 		}
1089*0Sstevel@tonic-gate 	if (pkey->type != EVP_PKEY_RSA)
1090*0Sstevel@tonic-gate 		{
1091*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL2_SET_CERTIFICATE,SSL_R_PUBLIC_KEY_NOT_RSA);
1092*0Sstevel@tonic-gate 		goto err;
1093*0Sstevel@tonic-gate 		}
1094*0Sstevel@tonic-gate 
1095*0Sstevel@tonic-gate 	if (!ssl_set_peer_cert_type(sc,SSL2_CT_X509_CERTIFICATE))
1096*0Sstevel@tonic-gate 		goto err;
1097*0Sstevel@tonic-gate 	ret=1;
1098*0Sstevel@tonic-gate err:
1099*0Sstevel@tonic-gate 	sk_X509_free(sk);
1100*0Sstevel@tonic-gate 	X509_free(x509);
1101*0Sstevel@tonic-gate 	EVP_PKEY_free(pkey);
1102*0Sstevel@tonic-gate 	return(ret);
1103*0Sstevel@tonic-gate 	}
1104*0Sstevel@tonic-gate 
1105*0Sstevel@tonic-gate static int ssl_rsa_public_encrypt(SESS_CERT *sc, int len, unsigned char *from,
1106*0Sstevel@tonic-gate 	     unsigned char *to, int padding)
1107*0Sstevel@tonic-gate 	{
1108*0Sstevel@tonic-gate 	EVP_PKEY *pkey=NULL;
1109*0Sstevel@tonic-gate 	int i= -1;
1110*0Sstevel@tonic-gate 
1111*0Sstevel@tonic-gate 	if ((sc == NULL) || (sc->peer_key->x509 == NULL) ||
1112*0Sstevel@tonic-gate 		((pkey=X509_get_pubkey(sc->peer_key->x509)) == NULL))
1113*0Sstevel@tonic-gate 		{
1114*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT,SSL_R_NO_PUBLICKEY);
1115*0Sstevel@tonic-gate 		return(-1);
1116*0Sstevel@tonic-gate 		}
1117*0Sstevel@tonic-gate 	if (pkey->type != EVP_PKEY_RSA)
1118*0Sstevel@tonic-gate 		{
1119*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT,SSL_R_PUBLIC_KEY_IS_NOT_RSA);
1120*0Sstevel@tonic-gate 		goto end;
1121*0Sstevel@tonic-gate 		}
1122*0Sstevel@tonic-gate 
1123*0Sstevel@tonic-gate 	/* we have the public key */
1124*0Sstevel@tonic-gate 	i=RSA_public_encrypt(len,from,to,pkey->pkey.rsa,padding);
1125*0Sstevel@tonic-gate 	if (i < 0)
1126*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_RSA_PUBLIC_ENCRYPT,ERR_R_RSA_LIB);
1127*0Sstevel@tonic-gate end:
1128*0Sstevel@tonic-gate 	EVP_PKEY_free(pkey);
1129*0Sstevel@tonic-gate 	return(i);
1130*0Sstevel@tonic-gate 	}
1131*0Sstevel@tonic-gate #else /* !OPENSSL_NO_SSL2 */
1132*0Sstevel@tonic-gate 
1133*0Sstevel@tonic-gate # if PEDANTIC
1134*0Sstevel@tonic-gate static void *dummy=&dummy;
1135*0Sstevel@tonic-gate # endif
1136*0Sstevel@tonic-gate 
1137*0Sstevel@tonic-gate #endif
1138