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