ssl/libssl/src/s3_clnt.c
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /* ssl/s3_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-2003 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  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
       
   113  *
       
   114  * Portions of the attached software ("Contribution") are developed by 
       
   115  * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
       
   116  *
       
   117  * The Contribution is licensed pursuant to the OpenSSL open source
       
   118  * license provided above.
       
   119  *
       
   120  * ECC cipher suite support in OpenSSL originally written by
       
   121  * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
       
   122  *
       
   123  */
       
   124 /*
       
   125  © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
       
   126  */
       
   127  
       
   128 #include <stdio.h>
       
   129 #include "ssl_locl.h"
       
   130 #include "kssl_lcl.h"
       
   131 #include <openssl/buffer.h>
       
   132 #include <openssl/rand.h>
       
   133 #include <openssl/objects.h>
       
   134 #include <openssl/evp.h>
       
   135 #include <openssl/md5.h>
       
   136 #ifndef OPENSSL_NO_DH
       
   137 #include <openssl/dh.h>
       
   138 #endif
       
   139 #include <openssl/bn.h>
       
   140 
       
   141 
       
   142 #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
       
   143 #include "libssl_wsd.h"
       
   144 #endif
       
   145 
       
   146 
       
   147 #ifdef EMULATOR
       
   148 
       
   149 	GET_STATIC_VAR_FROM_TLS(SSLv3_client_method_data,s3_clnt,SSL_METHOD)
       
   150 	
       
   151 	#define SSLv3_client_method_data (*GET_WSD_VAR_NAME(SSLv3_client_method_data,s3_clnt,s)())
       
   152 	
       
   153 #endif
       
   154 
       
   155 static SSL_METHOD *ssl3_get_client_method(int ver);
       
   156 static int ca_dn_cmp(const X509_NAME * const *a,const X509_NAME * const *b);
       
   157 
       
   158 #ifndef OPENSSL_NO_ECDH
       
   159 static int curve_id2nid(int curve_id);
       
   160 int check_srvr_ecc_cert_and_alg(X509 *x, SSL_CIPHER *cs);
       
   161 #endif
       
   162 
       
   163 static SSL_METHOD *ssl3_get_client_method(int ver)
       
   164 	{
       
   165 	if (ver == SSL3_VERSION)
       
   166 		return(SSLv3_client_method());
       
   167 	else
       
   168 		return(NULL);
       
   169 	}
       
   170 
       
   171 EXPORT_C IMPLEMENT_ssl3_meth_func(SSLv3_client_method,
       
   172 			ssl_undefined_function,
       
   173 			ssl3_connect,
       
   174 			ssl3_get_client_method)
       
   175 
       
   176 int ssl3_connect(SSL *s)
       
   177 	{
       
   178 	BUF_MEM *buf=NULL;
       
   179 	unsigned long Time=(unsigned long)time(NULL),l;
       
   180 	long num1;
       
   181 	void (*cb)(const SSL *ssl,int type,int val)=NULL;
       
   182 	int ret= -1;
       
   183 	int new_state,state,skip=0;;
       
   184 
       
   185 	RAND_add(&Time,sizeof(Time),0);
       
   186 	ERR_clear_error();
       
   187 	clear_sys_error();
       
   188 
       
   189 	if (s->info_callback != NULL)
       
   190 		cb=s->info_callback;
       
   191 	else if (s->ctx->info_callback != NULL)
       
   192 		cb=s->ctx->info_callback;
       
   193 	
       
   194 	s->in_handshake++;
       
   195 	if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); 
       
   196 
       
   197 	for (;;)
       
   198 		{
       
   199 		state=s->state;
       
   200 
       
   201 		switch(s->state)
       
   202 			{
       
   203 		case SSL_ST_RENEGOTIATE:
       
   204 			s->new_session=1;
       
   205 			s->state=SSL_ST_CONNECT;
       
   206 			s->ctx->stats.sess_connect_renegotiate++;
       
   207 			/* break */
       
   208 		case SSL_ST_BEFORE:
       
   209 		case SSL_ST_CONNECT:
       
   210 		case SSL_ST_BEFORE|SSL_ST_CONNECT:
       
   211 		case SSL_ST_OK|SSL_ST_CONNECT:
       
   212 
       
   213 			s->server=0;
       
   214 			if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
       
   215 
       
   216 			if ((s->version & 0xff00 ) != 0x0300)
       
   217 				{
       
   218 				SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
       
   219 				ret = -1;
       
   220 				goto end;
       
   221 				}
       
   222 				
       
   223 			/* s->version=SSL3_VERSION; */
       
   224 			s->type=SSL_ST_CONNECT;
       
   225 
       
   226 			if (s->init_buf == NULL)
       
   227 				{
       
   228 				if ((buf=BUF_MEM_new()) == NULL)
       
   229 					{
       
   230 					ret= -1;
       
   231 					goto end;
       
   232 					}
       
   233 				if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
       
   234 					{
       
   235 					ret= -1;
       
   236 					goto end;
       
   237 					}
       
   238 				s->init_buf=buf;
       
   239 				buf=NULL;
       
   240 				}
       
   241 
       
   242 			if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
       
   243 
       
   244 			/* setup buffing BIO */
       
   245 			if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
       
   246 
       
   247 			/* don't push the buffering BIO quite yet */
       
   248 
       
   249 			ssl3_init_finished_mac(s);
       
   250 
       
   251 			s->state=SSL3_ST_CW_CLNT_HELLO_A;
       
   252 			s->ctx->stats.sess_connect++;
       
   253 			s->init_num=0;
       
   254 			break;
       
   255 
       
   256 		case SSL3_ST_CW_CLNT_HELLO_A:
       
   257 		case SSL3_ST_CW_CLNT_HELLO_B:
       
   258 
       
   259 			s->shutdown=0;
       
   260 			ret=ssl3_client_hello(s);
       
   261 			if (ret <= 0) goto end;
       
   262 			s->state=SSL3_ST_CR_SRVR_HELLO_A;
       
   263 			s->init_num=0;
       
   264 
       
   265 			/* turn on buffering for the next lot of output */
       
   266 			if (s->bbio != s->wbio)
       
   267 				s->wbio=BIO_push(s->bbio,s->wbio);
       
   268 
       
   269 			break;
       
   270 
       
   271 		case SSL3_ST_CR_SRVR_HELLO_A:
       
   272 		case SSL3_ST_CR_SRVR_HELLO_B:
       
   273 			ret=ssl3_get_server_hello(s);
       
   274 			if (ret <= 0) goto end;
       
   275 			if (s->hit)
       
   276 				s->state=SSL3_ST_CR_FINISHED_A;
       
   277 			else
       
   278 				s->state=SSL3_ST_CR_CERT_A;
       
   279 			s->init_num=0;
       
   280 			break;
       
   281 
       
   282 		case SSL3_ST_CR_CERT_A:
       
   283 		case SSL3_ST_CR_CERT_B:
       
   284 
       
   285 			/* Check if it is anon DH/ECDH */
       
   286 			if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
       
   287 				{
       
   288 				ret=ssl3_get_server_certificate(s);
       
   289 				if (ret <= 0) goto end;
       
   290 				}
       
   291 			else
       
   292 				skip=1;
       
   293 			s->state=SSL3_ST_CR_KEY_EXCH_A;
       
   294 			s->init_num=0;
       
   295 			break;
       
   296 
       
   297 		case SSL3_ST_CR_KEY_EXCH_A:
       
   298 		case SSL3_ST_CR_KEY_EXCH_B:
       
   299 			ret=ssl3_get_key_exchange(s);
       
   300 			if (ret <= 0) goto end;
       
   301 			s->state=SSL3_ST_CR_CERT_REQ_A;
       
   302 			s->init_num=0;
       
   303 
       
   304 			/* at this point we check that we have the
       
   305 			 * required stuff from the server */
       
   306 			if (!ssl3_check_cert_and_algorithm(s))
       
   307 				{
       
   308 				ret= -1;
       
   309 				goto end;
       
   310 				}
       
   311 			break;
       
   312 
       
   313 		case SSL3_ST_CR_CERT_REQ_A:
       
   314 		case SSL3_ST_CR_CERT_REQ_B:
       
   315 			ret=ssl3_get_certificate_request(s);
       
   316 			if (ret <= 0) goto end;
       
   317 			s->state=SSL3_ST_CR_SRVR_DONE_A;
       
   318 			s->init_num=0;
       
   319 			break;
       
   320 
       
   321 		case SSL3_ST_CR_SRVR_DONE_A:
       
   322 		case SSL3_ST_CR_SRVR_DONE_B:
       
   323 			ret=ssl3_get_server_done(s);
       
   324 			if (ret <= 0) goto end;
       
   325 			if (s->s3->tmp.cert_req)
       
   326 				s->state=SSL3_ST_CW_CERT_A;
       
   327 			else
       
   328 				s->state=SSL3_ST_CW_KEY_EXCH_A;
       
   329 			s->init_num=0;
       
   330 
       
   331 			break;
       
   332 
       
   333 		case SSL3_ST_CW_CERT_A:
       
   334 		case SSL3_ST_CW_CERT_B:
       
   335 		case SSL3_ST_CW_CERT_C:
       
   336 		case SSL3_ST_CW_CERT_D:
       
   337 			ret=ssl3_send_client_certificate(s);
       
   338 			if (ret <= 0) goto end;
       
   339 			s->state=SSL3_ST_CW_KEY_EXCH_A;
       
   340 			s->init_num=0;
       
   341 			break;
       
   342 
       
   343 		case SSL3_ST_CW_KEY_EXCH_A:
       
   344 		case SSL3_ST_CW_KEY_EXCH_B:
       
   345 			ret=ssl3_send_client_key_exchange(s);
       
   346 			if (ret <= 0) goto end;
       
   347 			l=s->s3->tmp.new_cipher->algorithms;
       
   348 			/* EAY EAY EAY need to check for DH fix cert
       
   349 			 * sent back */
       
   350 			/* For TLS, cert_req is set to 2, so a cert chain
       
   351 			 * of nothing is sent, but no verify packet is sent */
       
   352 			/* XXX: For now, we do not support client 
       
   353 			 * authentication in ECDH cipher suites with
       
   354 			 * ECDH (rather than ECDSA) certificates.
       
   355 			 * We need to skip the certificate verify 
       
   356 			 * message when client's ECDH public key is sent 
       
   357 			 * inside the client certificate.
       
   358 			 */
       
   359 			if (s->s3->tmp.cert_req == 1)
       
   360 				{
       
   361 				s->state=SSL3_ST_CW_CERT_VRFY_A;
       
   362 				}
       
   363 			else
       
   364 				{
       
   365 				s->state=SSL3_ST_CW_CHANGE_A;
       
   366 				s->s3->change_cipher_spec=0;
       
   367 				}
       
   368 
       
   369 			s->init_num=0;
       
   370 			break;
       
   371 
       
   372 		case SSL3_ST_CW_CERT_VRFY_A:
       
   373 		case SSL3_ST_CW_CERT_VRFY_B:
       
   374 			ret=ssl3_send_client_verify(s);
       
   375 			if (ret <= 0) goto end;
       
   376 			s->state=SSL3_ST_CW_CHANGE_A;
       
   377 			s->init_num=0;
       
   378 			s->s3->change_cipher_spec=0;
       
   379 			break;
       
   380 
       
   381 		case SSL3_ST_CW_CHANGE_A:
       
   382 		case SSL3_ST_CW_CHANGE_B:
       
   383 			ret=ssl3_send_change_cipher_spec(s,
       
   384 				SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
       
   385 			if (ret <= 0) goto end;
       
   386 			s->state=SSL3_ST_CW_FINISHED_A;
       
   387 			s->init_num=0;
       
   388 
       
   389 			s->session->cipher=s->s3->tmp.new_cipher;
       
   390 #ifdef OPENSSL_NO_COMP
       
   391 			s->session->compress_meth=0;
       
   392 #else
       
   393 			if (s->s3->tmp.new_compression == NULL)
       
   394 				s->session->compress_meth=0;
       
   395 			else
       
   396 				s->session->compress_meth=
       
   397 					s->s3->tmp.new_compression->id;
       
   398 #endif
       
   399 			if (!s->method->ssl3_enc->setup_key_block(s))
       
   400 				{
       
   401 				ret= -1;
       
   402 				goto end;
       
   403 				}
       
   404 
       
   405 			if (!s->method->ssl3_enc->change_cipher_state(s,
       
   406 				SSL3_CHANGE_CIPHER_CLIENT_WRITE))
       
   407 				{
       
   408 				ret= -1;
       
   409 				goto end;
       
   410 				}
       
   411 
       
   412 			break;
       
   413 
       
   414 		case SSL3_ST_CW_FINISHED_A:
       
   415 		case SSL3_ST_CW_FINISHED_B:
       
   416 			ret=ssl3_send_finished(s,
       
   417 				SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
       
   418 				s->method->ssl3_enc->client_finished_label,
       
   419 				s->method->ssl3_enc->client_finished_label_len);
       
   420 			if (ret <= 0) goto end;
       
   421 			s->state=SSL3_ST_CW_FLUSH;
       
   422 
       
   423 			/* clear flags */
       
   424 			s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
       
   425 			if (s->hit)
       
   426 				{
       
   427 				s->s3->tmp.next_state=SSL_ST_OK;
       
   428 				if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
       
   429 					{
       
   430 					s->state=SSL_ST_OK;
       
   431 					s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
       
   432 					s->s3->delay_buf_pop_ret=0;
       
   433 					}
       
   434 				}
       
   435 			else
       
   436 				{
       
   437 
       
   438 				
       
   439 				s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
       
   440 				}
       
   441 			s->init_num=0;
       
   442 			break;
       
   443 
       
   444 
       
   445 
       
   446 		case SSL3_ST_CR_FINISHED_A:
       
   447 		case SSL3_ST_CR_FINISHED_B:
       
   448 
       
   449 			ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
       
   450 				SSL3_ST_CR_FINISHED_B);
       
   451 			if (ret <= 0) goto end;
       
   452 
       
   453 			if (s->hit)
       
   454 				s->state=SSL3_ST_CW_CHANGE_A;
       
   455 			else
       
   456 				s->state=SSL_ST_OK;
       
   457 			s->init_num=0;
       
   458 			break;
       
   459 
       
   460 		case SSL3_ST_CW_FLUSH:
       
   461 			/* number of bytes to be flushed */
       
   462 			num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
       
   463 			if (num1 > 0)
       
   464 				{
       
   465 				s->rwstate=SSL_WRITING;
       
   466 				num1=BIO_flush(s->wbio);
       
   467 				if (num1 <= 0) { ret= -1; goto end; }
       
   468 				s->rwstate=SSL_NOTHING;
       
   469 				}
       
   470 
       
   471 			s->state=s->s3->tmp.next_state;
       
   472 			break;
       
   473 
       
   474 		case SSL_ST_OK:
       
   475 			/* clean a few things up */
       
   476 			ssl3_cleanup_key_block(s);
       
   477 
       
   478 			if (s->init_buf != NULL)
       
   479 				{
       
   480 				BUF_MEM_free(s->init_buf);
       
   481 				s->init_buf=NULL;
       
   482 				}
       
   483 
       
   484 			/* If we are not 'joining' the last two packets,
       
   485 			 * remove the buffering now */
       
   486 			if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
       
   487 				ssl_free_wbio_buffer(s);
       
   488 			/* else do it later in ssl3_write */
       
   489 
       
   490 			s->init_num=0;
       
   491 			s->new_session=0;
       
   492 
       
   493 			ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
       
   494 			if (s->hit) s->ctx->stats.sess_hit++;
       
   495 
       
   496 			ret=1;
       
   497 			/* s->server=0; */
       
   498 			s->handshake_func=ssl3_connect;
       
   499 			s->ctx->stats.sess_connect_good++;
       
   500 
       
   501 			if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
       
   502 
       
   503 			goto end;
       
   504 			/* break; */
       
   505 			
       
   506 		default:
       
   507 			SSLerr(SSL_F_SSL3_CONNECT,SSL_R_UNKNOWN_STATE);
       
   508 			ret= -1;
       
   509 			goto end;
       
   510 			/* break; */
       
   511 			}
       
   512 
       
   513 		/* did we do anything */
       
   514 		if (!s->s3->tmp.reuse_message && !skip)
       
   515 			{
       
   516 			if (s->debug)
       
   517 				{
       
   518 				if ((ret=BIO_flush(s->wbio)) <= 0)
       
   519 					goto end;
       
   520 				}
       
   521 
       
   522 			if ((cb != NULL) && (s->state != state))
       
   523 				{
       
   524 				new_state=s->state;
       
   525 				s->state=state;
       
   526 				cb(s,SSL_CB_CONNECT_LOOP,1);
       
   527 				s->state=new_state;
       
   528 				}
       
   529 			}
       
   530 		skip=0;
       
   531 		}
       
   532 end:
       
   533 	s->in_handshake--;
       
   534 	if (buf != NULL)
       
   535 		BUF_MEM_free(buf);
       
   536 	if (cb != NULL)
       
   537 		cb(s,SSL_CB_CONNECT_EXIT,ret);
       
   538 	return(ret);
       
   539 	}
       
   540 
       
   541 
       
   542 int ssl3_client_hello(SSL *s)
       
   543 	{
       
   544 	unsigned char *buf;
       
   545 	unsigned char *p,*d;
       
   546 	int i;
       
   547 	unsigned long Time,l;
       
   548 #ifndef OPENSSL_NO_COMP
       
   549 	int j;
       
   550 	SSL_COMP *comp;
       
   551 #endif
       
   552 
       
   553 	buf=(unsigned char *)s->init_buf->data;
       
   554 	if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
       
   555 		{
       
   556 		if ((s->session == NULL) ||
       
   557 			(s->session->ssl_version != s->version) ||
       
   558 			(s->session->not_resumable))
       
   559 			{
       
   560 			if (!ssl_get_new_session(s,0))
       
   561 				goto err;
       
   562 			}
       
   563 		/* else use the pre-loaded session */
       
   564 
       
   565 		p=s->s3->client_random;
       
   566 		Time=(unsigned long)time(NULL);			/* Time */
       
   567 		l2n(Time,p);
       
   568 		if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
       
   569 			goto err;
       
   570 
       
   571 		/* Do the message type and length last */
       
   572 		d=p= &(buf[4]);
       
   573 
       
   574 		*(p++)=s->version>>8;
       
   575 		*(p++)=s->version&0xff;
       
   576 		s->client_version=s->version;
       
   577 
       
   578 		/* Random stuff */
       
   579 		memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
       
   580 		p+=SSL3_RANDOM_SIZE;
       
   581 
       
   582 		/* Session ID */
       
   583 		if (s->new_session)
       
   584 			i=0;
       
   585 		else
       
   586 			i=s->session->session_id_length;
       
   587 		*(p++)=i;
       
   588 		if (i != 0)
       
   589 			{
       
   590 			if (i > (int)sizeof(s->session->session_id))
       
   591 				{
       
   592 				SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
       
   593 				goto err;
       
   594 				}
       
   595 			memcpy(p,s->session->session_id,i);
       
   596 			p+=i;
       
   597 			}
       
   598 		
       
   599 		/* Ciphers supported */
       
   600 		i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),0);
       
   601 		if (i == 0)
       
   602 			{
       
   603 			SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
       
   604 			goto err;
       
   605 			}
       
   606 		s2n(i,p);
       
   607 		p+=i;
       
   608 
       
   609 		/* COMPRESSION */
       
   610 #ifdef OPENSSL_NO_COMP
       
   611 		*(p++)=1;
       
   612 #else
       
   613 		if (s->ctx->comp_methods == NULL)
       
   614 			j=0;
       
   615 		else
       
   616 			j=sk_SSL_COMP_num(s->ctx->comp_methods);
       
   617 		*(p++)=1+j;
       
   618 		for (i=0; i<j; i++)
       
   619 			{
       
   620 			comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
       
   621 			*(p++)=comp->id;
       
   622 			}
       
   623 #endif
       
   624 		*(p++)=0; /* Add the NULL method */
       
   625 		
       
   626 		l=(p-d);
       
   627 		d=buf;
       
   628 		*(d++)=SSL3_MT_CLIENT_HELLO;
       
   629 		l2n3(l,d);
       
   630 
       
   631 		s->state=SSL3_ST_CW_CLNT_HELLO_B;
       
   632 		/* number of bytes to write */
       
   633 		s->init_num=p-buf;
       
   634 		s->init_off=0;
       
   635 		}
       
   636 
       
   637 	/* SSL3_ST_CW_CLNT_HELLO_B */
       
   638 	return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
       
   639 err:
       
   640 	return(-1);
       
   641 	}
       
   642 
       
   643 int ssl3_get_server_hello(SSL *s)
       
   644 	{
       
   645 	STACK_OF(SSL_CIPHER) *sk;
       
   646 	SSL_CIPHER *c;
       
   647 	unsigned char *p,*d;
       
   648 	int i,al,ok;
       
   649 	unsigned int j;
       
   650 	long n;
       
   651 #ifndef OPENSSL_NO_COMP
       
   652 	SSL_COMP *comp;
       
   653 #endif
       
   654 
       
   655 	n=s->method->ssl_get_message(s,
       
   656 		SSL3_ST_CR_SRVR_HELLO_A,
       
   657 		SSL3_ST_CR_SRVR_HELLO_B,
       
   658 		-1,
       
   659 		20000, /* ?? */
       
   660 		&ok);
       
   661 
       
   662 	if (!ok) return((int)n);
       
   663 
       
   664 	if ( SSL_version(s) == DTLS1_VERSION)
       
   665 		{
       
   666 		if ( s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST)
       
   667 			{
       
   668 			if ( s->d1->send_cookie == 0)
       
   669 				{
       
   670 				s->s3->tmp.reuse_message = 1;
       
   671 				return 1;
       
   672 				}
       
   673 			else /* already sent a cookie */
       
   674 				{
       
   675 				al=SSL_AD_UNEXPECTED_MESSAGE;
       
   676 				SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE);
       
   677 				goto f_err;
       
   678 				}
       
   679 			}
       
   680 		}
       
   681 	
       
   682 	if ( s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO)
       
   683 		{
       
   684 		al=SSL_AD_UNEXPECTED_MESSAGE;
       
   685 		SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE);
       
   686 		goto f_err;
       
   687 		}
       
   688 
       
   689 	d=p=(unsigned char *)s->init_msg;
       
   690 
       
   691 	if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))
       
   692 		{
       
   693 		SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION);
       
   694 		s->version=(s->version&0xff00)|p[1];
       
   695 		al=SSL_AD_PROTOCOL_VERSION;
       
   696 		goto f_err;
       
   697 		}
       
   698 	p+=2;
       
   699 
       
   700 	/* load the server hello data */
       
   701 	/* load the server random */
       
   702 	memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE);
       
   703 	p+=SSL3_RANDOM_SIZE;
       
   704 
       
   705 	/* get the session-id */
       
   706 	j= *(p++);
       
   707 
       
   708 	if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE))
       
   709 		{
       
   710 		al=SSL_AD_ILLEGAL_PARAMETER;
       
   711 		SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_LONG);
       
   712 		goto f_err;
       
   713 		}
       
   714 
       
   715 	if (j != 0 && j == s->session->session_id_length
       
   716 	    && memcmp(p,s->session->session_id,j) == 0)
       
   717 	    {
       
   718 	    if(s->sid_ctx_length != s->session->sid_ctx_length
       
   719 	       || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length))
       
   720 		{
       
   721 		/* actually a client application bug */
       
   722 		al=SSL_AD_ILLEGAL_PARAMETER;
       
   723 		SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
       
   724 		goto f_err;
       
   725 		}
       
   726 	    s->hit=1;
       
   727 	    }
       
   728 	else	/* a miss or crap from the other end */
       
   729 		{
       
   730 		/* If we were trying for session-id reuse, make a new
       
   731 		 * SSL_SESSION so we don't stuff up other people */
       
   732 		s->hit=0;
       
   733 		if (s->session->session_id_length > 0)
       
   734 			{
       
   735 			if (!ssl_get_new_session(s,0))
       
   736 				{
       
   737 				al=SSL_AD_INTERNAL_ERROR;
       
   738 				goto f_err;
       
   739 				}
       
   740 			}
       
   741 		s->session->session_id_length=j;
       
   742 		memcpy(s->session->session_id,p,j); /* j could be 0 */
       
   743 		}
       
   744 	p+=j;
       
   745 	c=ssl_get_cipher_by_char(s,p);
       
   746 	if (c == NULL)
       
   747 		{
       
   748 		/* unknown cipher */
       
   749 		al=SSL_AD_ILLEGAL_PARAMETER;
       
   750 		SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNKNOWN_CIPHER_RETURNED);
       
   751 		goto f_err;
       
   752 		}
       
   753 	p+=ssl_put_cipher_by_char(s,NULL,NULL);
       
   754 
       
   755 	sk=ssl_get_ciphers_by_id(s);
       
   756 	i=sk_SSL_CIPHER_find(sk,c);
       
   757 	if (i < 0)
       
   758 		{
       
   759 		/* we did not say we would use this cipher */
       
   760 		al=SSL_AD_ILLEGAL_PARAMETER;
       
   761 		SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED);
       
   762 		goto f_err;
       
   763 		}
       
   764 
       
   765 	/* Depending on the session caching (internal/external), the cipher
       
   766 	   and/or cipher_id values may not be set. Make sure that
       
   767 	   cipher_id is set and use it for comparison. */
       
   768 	if (s->session->cipher)
       
   769 		s->session->cipher_id = s->session->cipher->id;
       
   770 	if (s->hit && (s->session->cipher_id != c->id))
       
   771 		{
       
   772 		if (!(s->options &
       
   773 			SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
       
   774 			{
       
   775 			al=SSL_AD_ILLEGAL_PARAMETER;
       
   776 			SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
       
   777 			goto f_err;
       
   778 			}
       
   779 		}
       
   780 	s->s3->tmp.new_cipher=c;
       
   781 
       
   782 	/* lets get the compression algorithm */
       
   783 	/* COMPRESSION */
       
   784 #ifdef OPENSSL_NO_COMP
       
   785 	if (*(p++) != 0)
       
   786 		{
       
   787 		al=SSL_AD_ILLEGAL_PARAMETER;
       
   788 		SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
       
   789 		goto f_err;
       
   790 		}
       
   791 #else
       
   792 	j= *(p++);
       
   793 	if (j == 0)
       
   794 		comp=NULL;
       
   795 	else
       
   796 		comp=ssl3_comp_find(s->ctx->comp_methods,j);
       
   797 	
       
   798 	if ((j != 0) && (comp == NULL))
       
   799 		{
       
   800 		al=SSL_AD_ILLEGAL_PARAMETER;
       
   801 		SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
       
   802 		goto f_err;
       
   803 		}
       
   804 	else
       
   805 		{
       
   806 		s->s3->tmp.new_compression=comp;
       
   807 		}
       
   808 #endif
       
   809 
       
   810 
       
   811 
       
   812 	if (p != (d+n))
       
   813 		{
       
   814 		/* wrong packet length */
       
   815 		al=SSL_AD_DECODE_ERROR;
       
   816 		SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH);
       
   817 		goto err;
       
   818 		}
       
   819 
       
   820 	return(1);
       
   821 f_err:
       
   822 	ssl3_send_alert(s,SSL3_AL_FATAL,al);
       
   823 err:
       
   824 	return(-1);
       
   825 	}
       
   826 
       
   827 int ssl3_get_server_certificate(SSL *s)
       
   828 	{
       
   829 	int al,i,ok,ret= -1;
       
   830 	unsigned long n,nc,llen,l;
       
   831 	X509 *x=NULL;
       
   832 	const unsigned char *q,*p;
       
   833 	unsigned char *d;
       
   834 	STACK_OF(X509) *sk=NULL;
       
   835 	SESS_CERT *sc;
       
   836 	EVP_PKEY *pkey=NULL;
       
   837 	int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */
       
   838 
       
   839 	n=s->method->ssl_get_message(s,
       
   840 		SSL3_ST_CR_CERT_A,
       
   841 		SSL3_ST_CR_CERT_B,
       
   842 		-1,
       
   843 		s->max_cert_list,
       
   844 		&ok);
       
   845 
       
   846 	if (!ok) return((int)n);
       
   847 
       
   848 	if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) ||
       
   849 		((s->s3->tmp.new_cipher->algorithms & SSL_aKRB5) && 
       
   850 		(s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)))
       
   851 		{
       
   852 		s->s3->tmp.reuse_message=1;
       
   853 		return(1);
       
   854 		}
       
   855 
       
   856 	if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
       
   857 		{
       
   858 		al=SSL_AD_UNEXPECTED_MESSAGE;
       
   859 		SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE);
       
   860 		goto f_err;
       
   861 		}
       
   862 	p=d=(unsigned char *)s->init_msg;
       
   863 
       
   864 	if ((sk=sk_X509_new_null()) == NULL)
       
   865 		{
       
   866 		SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
       
   867 		goto err;
       
   868 		}
       
   869 
       
   870 	n2l3(p,llen);
       
   871 	if (llen+3 != n)
       
   872 		{
       
   873 		al=SSL_AD_DECODE_ERROR;
       
   874 		SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
       
   875 		goto f_err;
       
   876 		}
       
   877 	for (nc=0; nc<llen; )
       
   878 		{
       
   879 		n2l3(p,l);
       
   880 		if ((l+nc+3) > llen)
       
   881 			{
       
   882 			al=SSL_AD_DECODE_ERROR;
       
   883 			SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
       
   884 			goto f_err;
       
   885 			}
       
   886 
       
   887 		q=p;
       
   888 		x=d2i_X509(NULL,&q,l);
       
   889 		if (x == NULL)
       
   890 			{
       
   891 			al=SSL_AD_BAD_CERTIFICATE;
       
   892 			SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB);
       
   893 			goto f_err;
       
   894 			}
       
   895 		if (q != (p+l))
       
   896 			{
       
   897 			al=SSL_AD_DECODE_ERROR;
       
   898 			SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
       
   899 			goto f_err;
       
   900 			}
       
   901 		if (!sk_X509_push(sk,x))
       
   902 			{
       
   903 			SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
       
   904 			goto err;
       
   905 			}
       
   906 		x=NULL;
       
   907 		nc+=l+3;
       
   908 		p=q;
       
   909 		}
       
   910 
       
   911 	i=ssl_verify_cert_chain(s,sk);
       
   912 	if ((s->verify_mode != SSL_VERIFY_NONE) && (!i)
       
   913 #ifndef OPENSSL_NO_KRB5
       
   914 	        && (s->s3->tmp.new_cipher->algorithms & (SSL_MKEY_MASK|SSL_AUTH_MASK))
       
   915 	        != (SSL_aKRB5|SSL_kKRB5)
       
   916 #endif /* OPENSSL_NO_KRB5 */
       
   917 	        )
       
   918 		{
       
   919 		al=ssl_verify_alarm_type(s->verify_result);
       
   920 		SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
       
   921 		goto f_err; 
       
   922 		}
       
   923 	ERR_clear_error(); /* but we keep s->verify_result */
       
   924 
       
   925 	sc=ssl_sess_cert_new();
       
   926 	if (sc == NULL) goto err;
       
   927 
       
   928 	if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
       
   929 	s->session->sess_cert=sc;
       
   930 
       
   931 	sc->cert_chain=sk;
       
   932 	/* Inconsistency alert: cert_chain does include the peer's
       
   933 	 * certificate, which we don't include in s3_srvr.c */
       
   934 	x=sk_X509_value(sk,0);
       
   935 	sk=NULL;
       
   936  	/* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
       
   937 
       
   938 	pkey=X509_get_pubkey(x);
       
   939 
       
   940 	/* VRS: allow null cert if auth == KRB5 */
       
   941 	need_cert =	((s->s3->tmp.new_cipher->algorithms
       
   942 	                 & (SSL_MKEY_MASK|SSL_AUTH_MASK))
       
   943 	                 == (SSL_aKRB5|SSL_kKRB5))? 0: 1;
       
   944 
       
   945 #ifdef KSSL_DEBUG
       
   946 	printf("pkey,x = %p, %p\n", pkey,x);
       
   947 	printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey));
       
   948 	printf("cipher, alg, nc = %s, %lx, %d\n", s->s3->tmp.new_cipher->name,
       
   949 	        s->s3->tmp.new_cipher->algorithms, need_cert);
       
   950 #endif    /* KSSL_DEBUG */
       
   951 
       
   952 	if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey)))
       
   953 		{
       
   954 		x=NULL;
       
   955 		al=SSL3_AL_FATAL;
       
   956 		SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
       
   957 			SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
       
   958 		goto f_err;
       
   959 		}
       
   960 
       
   961 	i=ssl_cert_type(x,pkey);
       
   962 	if (need_cert && i < 0)
       
   963 		{
       
   964 		x=NULL;
       
   965 		al=SSL3_AL_FATAL;
       
   966 		SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
       
   967 			SSL_R_UNKNOWN_CERTIFICATE_TYPE);
       
   968 		goto f_err;
       
   969 		}
       
   970 
       
   971 	if (need_cert)
       
   972 		{
       
   973 		sc->peer_cert_type=i;
       
   974 		CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
       
   975 		/* Why would the following ever happen?
       
   976 		 * We just created sc a couple of lines ago. */
       
   977 		if (sc->peer_pkeys[i].x509 != NULL)
       
   978 			X509_free(sc->peer_pkeys[i].x509);
       
   979 		sc->peer_pkeys[i].x509=x;
       
   980 		sc->peer_key= &(sc->peer_pkeys[i]);
       
   981 
       
   982 		if (s->session->peer != NULL)
       
   983 			X509_free(s->session->peer);
       
   984 		CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
       
   985 		s->session->peer=x;
       
   986 		}
       
   987 	else
       
   988 		{
       
   989 		sc->peer_cert_type=i;
       
   990 		sc->peer_key= NULL;
       
   991 
       
   992 		if (s->session->peer != NULL)
       
   993 			X509_free(s->session->peer);
       
   994 		s->session->peer=NULL;
       
   995 		}
       
   996 	s->session->verify_result = s->verify_result;
       
   997 
       
   998 	x=NULL;
       
   999 	ret=1;
       
  1000 
       
  1001 	if (0)
       
  1002 		{
       
  1003 f_err:
       
  1004 		ssl3_send_alert(s,SSL3_AL_FATAL,al);
       
  1005 		}
       
  1006 err:
       
  1007 	EVP_PKEY_free(pkey);
       
  1008 	X509_free(x);
       
  1009 	sk_X509_pop_free(sk,X509_free);
       
  1010 	return(ret);
       
  1011 	}
       
  1012 
       
  1013 int ssl3_get_key_exchange(SSL *s)
       
  1014 	{
       
  1015 #ifndef OPENSSL_NO_RSA
       
  1016 	unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
       
  1017 #endif
       
  1018 	EVP_MD_CTX md_ctx;
       
  1019 	unsigned char *param,*p;
       
  1020 	int al,i,j,param_len,ok;
       
  1021 	long n,alg;
       
  1022 	EVP_PKEY *pkey=NULL;
       
  1023 #ifndef OPENSSL_NO_RSA
       
  1024 	RSA *rsa=NULL;
       
  1025 #endif
       
  1026 #ifndef OPENSSL_NO_DH
       
  1027 	DH *dh=NULL;
       
  1028 #endif
       
  1029 #ifndef OPENSSL_NO_ECDH
       
  1030 	EC_KEY *ecdh = NULL;
       
  1031 	BN_CTX *bn_ctx = NULL;
       
  1032 	EC_POINT *srvr_ecpoint = NULL;
       
  1033 	int curve_nid = 0;
       
  1034 	int encoded_pt_len = 0;
       
  1035 #endif
       
  1036 
       
  1037 	/* use same message size as in ssl3_get_certificate_request()
       
  1038 	 * as ServerKeyExchange message may be skipped */
       
  1039 	n=s->method->ssl_get_message(s,
       
  1040 		SSL3_ST_CR_KEY_EXCH_A,
       
  1041 		SSL3_ST_CR_KEY_EXCH_B,
       
  1042 		-1,
       
  1043 		s->max_cert_list,
       
  1044 		&ok);
       
  1045 
       
  1046 	if (!ok) return((int)n);
       
  1047 
       
  1048 	if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
       
  1049 		{
       
  1050 		s->s3->tmp.reuse_message=1;
       
  1051 		return(1);
       
  1052 		}
       
  1053 
       
  1054 	param=p=(unsigned char *)s->init_msg;
       
  1055 
       
  1056 	if (s->session->sess_cert != NULL)
       
  1057 		{
       
  1058 #ifndef OPENSSL_NO_RSA
       
  1059 		if (s->session->sess_cert->peer_rsa_tmp != NULL)
       
  1060 			{
       
  1061 			RSA_free(s->session->sess_cert->peer_rsa_tmp);
       
  1062 			s->session->sess_cert->peer_rsa_tmp=NULL;
       
  1063 			}
       
  1064 #endif
       
  1065 #ifndef OPENSSL_NO_DH
       
  1066 		if (s->session->sess_cert->peer_dh_tmp)
       
  1067 			{
       
  1068 			DH_free(s->session->sess_cert->peer_dh_tmp);
       
  1069 			s->session->sess_cert->peer_dh_tmp=NULL;
       
  1070 			}
       
  1071 #endif
       
  1072 #ifndef OPENSSL_NO_ECDH
       
  1073 		if (s->session->sess_cert->peer_ecdh_tmp)
       
  1074 			{
       
  1075 			EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
       
  1076 			s->session->sess_cert->peer_ecdh_tmp=NULL;
       
  1077 			}
       
  1078 #endif
       
  1079 		}
       
  1080 	else
       
  1081 		{
       
  1082 		s->session->sess_cert=ssl_sess_cert_new();
       
  1083 		}
       
  1084 
       
  1085 	param_len=0;
       
  1086 	alg=s->s3->tmp.new_cipher->algorithms;
       
  1087 	EVP_MD_CTX_init(&md_ctx);
       
  1088 
       
  1089 #ifndef OPENSSL_NO_RSA
       
  1090 	if (alg & SSL_kRSA)
       
  1091 		{
       
  1092 		if ((rsa=RSA_new()) == NULL)
       
  1093 			{
       
  1094 			SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
       
  1095 			goto err;
       
  1096 			}
       
  1097 		n2s(p,i);
       
  1098 		param_len=i+2;
       
  1099 		if (param_len > n)
       
  1100 			{
       
  1101 			al=SSL_AD_DECODE_ERROR;
       
  1102 			SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH);
       
  1103 			goto f_err;
       
  1104 			}
       
  1105 		if (!(rsa->n=BN_bin2bn(p,i,rsa->n)))
       
  1106 			{
       
  1107 			SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
       
  1108 			goto err;
       
  1109 			}
       
  1110 		p+=i;
       
  1111 
       
  1112 		n2s(p,i);
       
  1113 		param_len+=i+2;
       
  1114 		if (param_len > n)
       
  1115 			{
       
  1116 			al=SSL_AD_DECODE_ERROR;
       
  1117 			SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH);
       
  1118 			goto f_err;
       
  1119 			}
       
  1120 		if (!(rsa->e=BN_bin2bn(p,i,rsa->e)))
       
  1121 			{
       
  1122 			SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
       
  1123 			goto err;
       
  1124 			}
       
  1125 		p+=i;
       
  1126 		n-=param_len;
       
  1127 
       
  1128 		/* this should be because we are using an export cipher */
       
  1129 		if (alg & SSL_aRSA)
       
  1130 			pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
       
  1131 		else
       
  1132 			{
       
  1133 			SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
       
  1134 			goto err;
       
  1135 			}
       
  1136 		s->session->sess_cert->peer_rsa_tmp=rsa;
       
  1137 		rsa=NULL;
       
  1138 		}
       
  1139 #else /* OPENSSL_NO_RSA */
       
  1140 	if (0)
       
  1141 		;
       
  1142 #endif
       
  1143 #ifndef OPENSSL_NO_DH
       
  1144 	else if (alg & SSL_kEDH)
       
  1145 		{
       
  1146 		if ((dh=DH_new()) == NULL)
       
  1147 			{
       
  1148 			SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB);
       
  1149 			goto err;
       
  1150 			}
       
  1151 		n2s(p,i);
       
  1152 		param_len=i+2;
       
  1153 		if (param_len > n)
       
  1154 			{
       
  1155 			al=SSL_AD_DECODE_ERROR;
       
  1156 			SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH);
       
  1157 			goto f_err;
       
  1158 			}
       
  1159 		if (!(dh->p=BN_bin2bn(p,i,NULL)))
       
  1160 			{
       
  1161 			SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
       
  1162 			goto err;
       
  1163 			}
       
  1164 		p+=i;
       
  1165 
       
  1166 		n2s(p,i);
       
  1167 		param_len+=i+2;
       
  1168 		if (param_len > n)
       
  1169 			{
       
  1170 			al=SSL_AD_DECODE_ERROR;
       
  1171 			SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH);
       
  1172 			goto f_err;
       
  1173 			}
       
  1174 		if (!(dh->g=BN_bin2bn(p,i,NULL)))
       
  1175 			{
       
  1176 			SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
       
  1177 			goto err;
       
  1178 			}
       
  1179 		p+=i;
       
  1180 
       
  1181 		n2s(p,i);
       
  1182 		param_len+=i+2;
       
  1183 		if (param_len > n)
       
  1184 			{
       
  1185 			al=SSL_AD_DECODE_ERROR;
       
  1186 			SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH);
       
  1187 			goto f_err;
       
  1188 			}
       
  1189 		if (!(dh->pub_key=BN_bin2bn(p,i,NULL)))
       
  1190 			{
       
  1191 			SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
       
  1192 			goto err;
       
  1193 			}
       
  1194 		p+=i;
       
  1195 		n-=param_len;
       
  1196 
       
  1197 #ifndef OPENSSL_NO_RSA
       
  1198 		if (alg & SSL_aRSA)
       
  1199 			pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
       
  1200 #else
       
  1201 		if (0)
       
  1202 			;
       
  1203 #endif
       
  1204 #ifndef OPENSSL_NO_DSA
       
  1205 		else if (alg & SSL_aDSS)
       
  1206 			pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
       
  1207 #endif
       
  1208 		/* else anonymous DH, so no certificate or pkey. */
       
  1209 
       
  1210 		s->session->sess_cert->peer_dh_tmp=dh;
       
  1211 		dh=NULL;
       
  1212 		}
       
  1213 	else if ((alg & SSL_kDHr) || (alg & SSL_kDHd))
       
  1214 		{
       
  1215 		al=SSL_AD_ILLEGAL_PARAMETER;
       
  1216 		SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
       
  1217 		goto f_err;
       
  1218 		}
       
  1219 #endif /* !OPENSSL_NO_DH */
       
  1220 
       
  1221 #ifndef OPENSSL_NO_ECDH
       
  1222 	else if (alg & SSL_kECDHE)
       
  1223 		{
       
  1224 		EC_GROUP *ngroup;
       
  1225 		const EC_GROUP *group;
       
  1226 
       
  1227 		if ((ecdh=EC_KEY_new()) == NULL)
       
  1228 			{
       
  1229 			SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
       
  1230 			goto err;
       
  1231 			}
       
  1232 
       
  1233 		/* Extract elliptic curve parameters and the
       
  1234 		 * server's ephemeral ECDH public key.
       
  1235 		 * Keep accumulating lengths of various components in
       
  1236 		 * param_len and make sure it never exceeds n.
       
  1237 		 */
       
  1238 
       
  1239 		/* XXX: For now we only support named (not generic) curves
       
  1240 		 * and the ECParameters in this case is just three bytes.
       
  1241 		 */
       
  1242 		param_len=3;
       
  1243 		if ((param_len > n) ||
       
  1244 		    (*p != NAMED_CURVE_TYPE) || 
       
  1245 		    ((curve_nid = curve_id2nid(*(p + 2))) == 0)) 
       
  1246 			{
       
  1247 			al=SSL_AD_INTERNAL_ERROR;
       
  1248 			SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
       
  1249 			goto f_err;
       
  1250 			}
       
  1251 
       
  1252 		ngroup = EC_GROUP_new_by_curve_name(curve_nid);
       
  1253 		if (ngroup == NULL)
       
  1254 			{
       
  1255 			SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB);
       
  1256 			goto err;
       
  1257 			}
       
  1258 		if (EC_KEY_set_group(ecdh, ngroup) == 0)
       
  1259 			{
       
  1260 			SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB);
       
  1261 			goto err;
       
  1262 			}
       
  1263 		EC_GROUP_free(ngroup);
       
  1264 
       
  1265 		group = EC_KEY_get0_group(ecdh);
       
  1266 
       
  1267 		if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
       
  1268 		    (EC_GROUP_get_degree(group) > 163))
       
  1269 			{
       
  1270 			al=SSL_AD_EXPORT_RESTRICTION;
       
  1271 			SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
       
  1272 			goto f_err;
       
  1273 			}
       
  1274 
       
  1275 		p+=3;
       
  1276 
       
  1277 		/* Next, get the encoded ECPoint */
       
  1278 		if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
       
  1279 		    ((bn_ctx = BN_CTX_new()) == NULL))
       
  1280 			{
       
  1281 			SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
       
  1282 			goto err;
       
  1283 			}
       
  1284 
       
  1285 		encoded_pt_len = *p;  /* length of encoded point */
       
  1286 		p+=1;
       
  1287 		param_len += (1 + encoded_pt_len);
       
  1288 		if ((param_len > n) ||
       
  1289 		    (EC_POINT_oct2point(group, srvr_ecpoint, 
       
  1290 			p, encoded_pt_len, bn_ctx) == 0))
       
  1291 			{
       
  1292 			al=SSL_AD_DECODE_ERROR;
       
  1293 			SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_ECPOINT);
       
  1294 			goto f_err;
       
  1295 			}
       
  1296 
       
  1297 		n-=param_len;
       
  1298 		p+=encoded_pt_len;
       
  1299 
       
  1300 		/* The ECC/TLS specification does not mention
       
  1301 		 * the use of DSA to sign ECParameters in the server
       
  1302 		 * key exchange message. We do support RSA and ECDSA.
       
  1303 		 */
       
  1304 		if (0) ;
       
  1305 #ifndef OPENSSL_NO_RSA
       
  1306 		else if (alg & SSL_aRSA)
       
  1307 			pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
       
  1308 #endif
       
  1309 #ifndef OPENSSL_NO_ECDSA
       
  1310 		else if (alg & SSL_aECDSA)
       
  1311 			pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
       
  1312 #endif
       
  1313 		/* else anonymous ECDH, so no certificate or pkey. */
       
  1314 		EC_KEY_set_public_key(ecdh, srvr_ecpoint);
       
  1315 		s->session->sess_cert->peer_ecdh_tmp=ecdh;
       
  1316 		ecdh=NULL;
       
  1317 		BN_CTX_free(bn_ctx);
       
  1318 		EC_POINT_free(srvr_ecpoint);
       
  1319 		srvr_ecpoint = NULL;
       
  1320 		}
       
  1321 	else if (alg & SSL_kECDH)
       
  1322 		{
       
  1323 		al=SSL_AD_UNEXPECTED_MESSAGE;
       
  1324 		SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNEXPECTED_MESSAGE);
       
  1325 		goto f_err;
       
  1326 		}
       
  1327 #endif /* !OPENSSL_NO_ECDH */
       
  1328 	if (alg & SSL_aFZA)
       
  1329 		{
       
  1330 		al=SSL_AD_HANDSHAKE_FAILURE;
       
  1331 		SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
       
  1332 		goto f_err;
       
  1333 		}
       
  1334 
       
  1335 
       
  1336 	/* p points to the next byte, there are 'n' bytes left */
       
  1337 
       
  1338 	/* if it was signed, check the signature */
       
  1339 	if (pkey != NULL)
       
  1340 		{
       
  1341 		n2s(p,i);
       
  1342 		n-=2;
       
  1343 		j=EVP_PKEY_size(pkey);
       
  1344 
       
  1345 		if ((i != n) || (n > j) || (n <= 0))
       
  1346 			{
       
  1347 			/* wrong packet length */
       
  1348 			al=SSL_AD_DECODE_ERROR;
       
  1349 			SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH);
       
  1350 			goto f_err;
       
  1351 			}
       
  1352 
       
  1353 #ifndef OPENSSL_NO_RSA
       
  1354 		if (pkey->type == EVP_PKEY_RSA)
       
  1355 			{
       
  1356 			int num;
       
  1357 
       
  1358 			j=0;
       
  1359 			q=md_buf;
       
  1360 			for (num=2; num > 0; num--)
       
  1361 				{
       
  1362 				EVP_DigestInit_ex(&md_ctx,(num == 2)
       
  1363 					?s->ctx->md5:s->ctx->sha1, NULL);
       
  1364 				EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
       
  1365 				EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
       
  1366 				EVP_DigestUpdate(&md_ctx,param,param_len);
       
  1367 				EVP_DigestFinal_ex(&md_ctx,q,(unsigned int *)&i);
       
  1368 				q+=i;
       
  1369 				j+=i;
       
  1370 				}
       
  1371 			i=RSA_verify(NID_md5_sha1, md_buf, j, p, n,
       
  1372 								pkey->pkey.rsa);
       
  1373 			if (i < 0)
       
  1374 				{
       
  1375 				al=SSL_AD_DECRYPT_ERROR;
       
  1376 				SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
       
  1377 				goto f_err;
       
  1378 				}
       
  1379 			if (i == 0)
       
  1380 				{
       
  1381 				/* bad signature */
       
  1382 				al=SSL_AD_DECRYPT_ERROR;
       
  1383 				SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
       
  1384 				goto f_err;
       
  1385 				}
       
  1386 			}
       
  1387 		else
       
  1388 #endif
       
  1389 #ifndef OPENSSL_NO_DSA
       
  1390 			if (pkey->type == EVP_PKEY_DSA)
       
  1391 			{
       
  1392 			/* lets do DSS */
       
  1393 			EVP_VerifyInit_ex(&md_ctx,EVP_dss1(), NULL);
       
  1394 			EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
       
  1395 			EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
       
  1396 			EVP_VerifyUpdate(&md_ctx,param,param_len);
       
  1397 			if (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey))
       
  1398 				{
       
  1399 				/* bad signature */
       
  1400 				al=SSL_AD_DECRYPT_ERROR;
       
  1401 				SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
       
  1402 				goto f_err;
       
  1403 				}
       
  1404 			}
       
  1405 		else
       
  1406 #endif
       
  1407 #ifndef OPENSSL_NO_ECDSA
       
  1408 			if (pkey->type == EVP_PKEY_EC)
       
  1409 			{
       
  1410 			/* let's do ECDSA */
       
  1411 			EVP_VerifyInit_ex(&md_ctx,EVP_ecdsa(), NULL);
       
  1412 			EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
       
  1413 			EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
       
  1414 			EVP_VerifyUpdate(&md_ctx,param,param_len);
       
  1415 			if (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey))
       
  1416 				{
       
  1417 				/* bad signature */
       
  1418 				al=SSL_AD_DECRYPT_ERROR;
       
  1419 				SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
       
  1420 				goto f_err;
       
  1421 				}
       
  1422 			}
       
  1423 		else
       
  1424 #endif
       
  1425 			{
       
  1426 			SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
       
  1427 			goto err;
       
  1428 			}
       
  1429 		}
       
  1430 	else
       
  1431 		{
       
  1432 		/* still data left over */
       
  1433 		if (!(alg & SSL_aNULL))
       
  1434 			{
       
  1435 			SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
       
  1436 			goto err;
       
  1437 			}
       
  1438 		if (n != 0)
       
  1439 			{
       
  1440 			al=SSL_AD_DECODE_ERROR;
       
  1441 			SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE);
       
  1442 			goto f_err;
       
  1443 			}
       
  1444 		}
       
  1445 	EVP_PKEY_free(pkey);
       
  1446 	EVP_MD_CTX_cleanup(&md_ctx);
       
  1447 	return(1);
       
  1448 f_err:
       
  1449 	ssl3_send_alert(s,SSL3_AL_FATAL,al);
       
  1450 err:
       
  1451 	EVP_PKEY_free(pkey);
       
  1452 #ifndef OPENSSL_NO_RSA
       
  1453 	if (rsa != NULL)
       
  1454 		RSA_free(rsa);
       
  1455 #endif
       
  1456 #ifndef OPENSSL_NO_DH
       
  1457 	if (dh != NULL)
       
  1458 		DH_free(dh);
       
  1459 #endif
       
  1460 #ifndef OPENSSL_NO_ECDH
       
  1461 	BN_CTX_free(bn_ctx);
       
  1462 	EC_POINT_free(srvr_ecpoint);
       
  1463 	if (ecdh != NULL)
       
  1464 		EC_KEY_free(ecdh);
       
  1465 #endif
       
  1466 	EVP_MD_CTX_cleanup(&md_ctx);
       
  1467 	return(-1);
       
  1468 	}
       
  1469 
       
  1470 int ssl3_get_certificate_request(SSL *s)
       
  1471 	{
       
  1472 	int ok,ret=0;
       
  1473 	unsigned long n,nc,l;
       
  1474 	unsigned int llen,ctype_num,i;
       
  1475 	X509_NAME *xn=NULL;
       
  1476 	const unsigned char *p,*q;
       
  1477 	unsigned char *d;
       
  1478 	STACK_OF(X509_NAME) *ca_sk=NULL;
       
  1479 
       
  1480 	n=s->method->ssl_get_message(s,
       
  1481 		SSL3_ST_CR_CERT_REQ_A,
       
  1482 		SSL3_ST_CR_CERT_REQ_B,
       
  1483 		-1,
       
  1484 		s->max_cert_list,
       
  1485 		&ok);
       
  1486 
       
  1487 	if (!ok) return((int)n);
       
  1488 
       
  1489 	s->s3->tmp.cert_req=0;
       
  1490 
       
  1491 	if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
       
  1492 		{
       
  1493 		s->s3->tmp.reuse_message=1;
       
  1494 		return(1);
       
  1495 		}
       
  1496 
       
  1497 	if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
       
  1498 		{
       
  1499 		ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
       
  1500 		SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE);
       
  1501 		goto err;
       
  1502 		}
       
  1503 
       
  1504 	/* TLS does not like anon-DH with client cert */
       
  1505 	if (s->version > SSL3_VERSION)
       
  1506 		{
       
  1507 		l=s->s3->tmp.new_cipher->algorithms;
       
  1508 		if (l & SSL_aNULL)
       
  1509 			{
       
  1510 			ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
       
  1511 			SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
       
  1512 			goto err;
       
  1513 			}
       
  1514 		}
       
  1515 
       
  1516 	p=d=(unsigned char *)s->init_msg;
       
  1517 
       
  1518 	if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL)
       
  1519 		{
       
  1520 		SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
       
  1521 		goto err;
       
  1522 		}
       
  1523 
       
  1524 	/* get the certificate types */
       
  1525 	ctype_num= *(p++);
       
  1526 	if (ctype_num > SSL3_CT_NUMBER)
       
  1527 		ctype_num=SSL3_CT_NUMBER;
       
  1528 	for (i=0; i<ctype_num; i++)
       
  1529 		s->s3->tmp.ctype[i]= p[i];
       
  1530 	p+=ctype_num;
       
  1531 
       
  1532 	/* get the CA RDNs */
       
  1533 	n2s(p,llen);
       
  1534 #if 0
       
  1535 {
       
  1536 FILE *out;
       
  1537 out=fopen("/tmp/vsign.der","w");
       
  1538 fwrite(p,1,llen,out);
       
  1539 fclose(out);
       
  1540 }
       
  1541 #endif
       
  1542 
       
  1543 	if ((llen+ctype_num+2+1) != n)
       
  1544 		{
       
  1545 		ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
       
  1546 		SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH);
       
  1547 		goto err;
       
  1548 		}
       
  1549 
       
  1550 	for (nc=0; nc<llen; )
       
  1551 		{
       
  1552 		n2s(p,l);
       
  1553 		if ((l+nc+2) > llen)
       
  1554 			{
       
  1555 			if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
       
  1556 				goto cont; /* netscape bugs */
       
  1557 			ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
       
  1558 			SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG);
       
  1559 			goto err;
       
  1560 			}
       
  1561 
       
  1562 		q=p;
       
  1563 
       
  1564 		if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL)
       
  1565 			{
       
  1566 			/* If netscape tolerance is on, ignore errors */
       
  1567 			if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
       
  1568 				goto cont;
       
  1569 			else
       
  1570 				{
       
  1571 				ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
       
  1572 				SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB);
       
  1573 				goto err;
       
  1574 				}
       
  1575 			}
       
  1576 
       
  1577 		if (q != (p+l))
       
  1578 			{
       
  1579 			ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
       
  1580 			SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH);
       
  1581 			goto err;
       
  1582 			}
       
  1583 		if (!sk_X509_NAME_push(ca_sk,xn))
       
  1584 			{
       
  1585 			SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
       
  1586 			goto err;
       
  1587 			}
       
  1588 
       
  1589 		p+=l;
       
  1590 		nc+=l+2;
       
  1591 		}
       
  1592 
       
  1593 	if (0)
       
  1594 		{
       
  1595 cont:
       
  1596 		ERR_clear_error();
       
  1597 		}
       
  1598 
       
  1599 	/* we should setup a certificate to return.... */
       
  1600 	s->s3->tmp.cert_req=1;
       
  1601 	s->s3->tmp.ctype_num=ctype_num;
       
  1602 	if (s->s3->tmp.ca_names != NULL)
       
  1603 		sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
       
  1604 	s->s3->tmp.ca_names=ca_sk;
       
  1605 	ca_sk=NULL;
       
  1606 
       
  1607 	ret=1;
       
  1608 err:
       
  1609 	if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
       
  1610 	return(ret);
       
  1611 	}
       
  1612 
       
  1613 static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b)
       
  1614 	{
       
  1615 	return(X509_NAME_cmp(*a,*b));
       
  1616 	}
       
  1617 
       
  1618 int ssl3_get_server_done(SSL *s)
       
  1619 	{
       
  1620 	int ok,ret=0;
       
  1621 	long n;
       
  1622 
       
  1623 	n=s->method->ssl_get_message(s,
       
  1624 		SSL3_ST_CR_SRVR_DONE_A,
       
  1625 		SSL3_ST_CR_SRVR_DONE_B,
       
  1626 		SSL3_MT_SERVER_DONE,
       
  1627 		30, /* should be very small, like 0 :-) */
       
  1628 		&ok);
       
  1629 
       
  1630 	if (!ok) return((int)n);
       
  1631 	if (n > 0)
       
  1632 		{
       
  1633 		/* should contain no data */
       
  1634 		ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
       
  1635 		SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH);
       
  1636 		return -1;
       
  1637 		}
       
  1638 	ret=1;
       
  1639 	return(ret);
       
  1640 	}
       
  1641 
       
  1642 
       
  1643 int ssl3_send_client_key_exchange(SSL *s)
       
  1644 	{
       
  1645 	unsigned char *p,*d;
       
  1646 	int n;
       
  1647 	unsigned long l;
       
  1648 #ifndef OPENSSL_NO_RSA
       
  1649 	unsigned char *q;
       
  1650 	EVP_PKEY *pkey=NULL;
       
  1651 #endif
       
  1652 #ifndef OPENSSL_NO_KRB5
       
  1653 	KSSL_ERR kssl_err;
       
  1654 #endif /* OPENSSL_NO_KRB5 */
       
  1655 #ifndef OPENSSL_NO_ECDH
       
  1656 	EC_KEY *clnt_ecdh = NULL;
       
  1657 	const EC_POINT *srvr_ecpoint = NULL;
       
  1658 	EVP_PKEY *srvr_pub_pkey = NULL;
       
  1659 	unsigned char *encodedPoint = NULL;
       
  1660 	int encoded_pt_len = 0;
       
  1661 	BN_CTX * bn_ctx = NULL;
       
  1662 #endif
       
  1663 
       
  1664 	if (s->state == SSL3_ST_CW_KEY_EXCH_A)
       
  1665 		{
       
  1666 		d=(unsigned char *)s->init_buf->data;
       
  1667 		p= &(d[4]);
       
  1668 
       
  1669 		l=s->s3->tmp.new_cipher->algorithms;
       
  1670 
       
  1671 		/* Fool emacs indentation */
       
  1672 		if (0) {}
       
  1673 #ifndef OPENSSL_NO_RSA
       
  1674 		else if (l & SSL_kRSA)
       
  1675 			{
       
  1676 			RSA *rsa;
       
  1677 			unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
       
  1678 
       
  1679 			if (s->session->sess_cert->peer_rsa_tmp != NULL)
       
  1680 				rsa=s->session->sess_cert->peer_rsa_tmp;
       
  1681 			else
       
  1682 				{
       
  1683 				pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
       
  1684 				if ((pkey == NULL) ||
       
  1685 					(pkey->type != EVP_PKEY_RSA) ||
       
  1686 					(pkey->pkey.rsa == NULL))
       
  1687 					{
       
  1688 					SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR);
       
  1689 					goto err;
       
  1690 					}
       
  1691 				rsa=pkey->pkey.rsa;
       
  1692 				EVP_PKEY_free(pkey);
       
  1693 				}
       
  1694 				
       
  1695 			tmp_buf[0]=s->client_version>>8;
       
  1696 			tmp_buf[1]=s->client_version&0xff;
       
  1697 			if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
       
  1698 					goto err;
       
  1699 
       
  1700 			s->session->master_key_length=sizeof tmp_buf;
       
  1701 
       
  1702 			q=p;
       
  1703 			/* Fix buf for TLS and beyond */
       
  1704 			if (s->version > SSL3_VERSION)
       
  1705 				p+=2;
       
  1706 			n=RSA_public_encrypt(sizeof tmp_buf,
       
  1707 				tmp_buf,p,rsa,RSA_PKCS1_PADDING);
       
  1708 #ifdef PKCS1_CHECK
       
  1709 			if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
       
  1710 			if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
       
  1711 #endif
       
  1712 			if (n <= 0)
       
  1713 				{
       
  1714 				SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
       
  1715 				goto err;
       
  1716 				}
       
  1717 
       
  1718 			/* Fix buf for TLS and beyond */
       
  1719 			if (s->version > SSL3_VERSION)
       
  1720 				{
       
  1721 				s2n(n,q);
       
  1722 				n+=2;
       
  1723 				}
       
  1724 
       
  1725 			s->session->master_key_length=
       
  1726 				s->method->ssl3_enc->generate_master_secret(s,
       
  1727 					s->session->master_key,
       
  1728 					tmp_buf,sizeof tmp_buf);
       
  1729 			OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
       
  1730 			}
       
  1731 #endif
       
  1732 #ifndef OPENSSL_NO_KRB5
       
  1733 		else if (l & SSL_kKRB5)
       
  1734 			{
       
  1735 			krb5_error_code	krb5rc;
       
  1736 			KSSL_CTX	*kssl_ctx = s->kssl_ctx;
       
  1737 			/*  krb5_data	krb5_ap_req;  */
       
  1738 			krb5_data	*enc_ticket;
       
  1739 			krb5_data	authenticator, *authp = NULL;
       
  1740 			EVP_CIPHER_CTX	ciph_ctx;
       
  1741 			EVP_CIPHER	*enc = NULL;
       
  1742 			unsigned char	iv[EVP_MAX_IV_LENGTH];
       
  1743 			unsigned char	tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
       
  1744 			unsigned char	epms[SSL_MAX_MASTER_KEY_LENGTH 
       
  1745 						+ EVP_MAX_IV_LENGTH];
       
  1746 			int 		padl, outl = sizeof(epms);
       
  1747 
       
  1748 			EVP_CIPHER_CTX_init(&ciph_ctx);
       
  1749 
       
  1750 #ifdef KSSL_DEBUG
       
  1751 			printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
       
  1752 			        l, SSL_kKRB5);
       
  1753 #endif	/* KSSL_DEBUG */
       
  1754 
       
  1755 			authp = NULL;
       
  1756 #ifdef KRB5SENDAUTH
       
  1757 			if (KRB5SENDAUTH)  authp = &authenticator;
       
  1758 #endif	/* KRB5SENDAUTH */
       
  1759 
       
  1760 			krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp,
       
  1761 				&kssl_err);
       
  1762 			enc = kssl_map_enc(kssl_ctx->enctype);
       
  1763 			if (enc == NULL)
       
  1764 			    goto err;
       
  1765 #ifdef KSSL_DEBUG
       
  1766 			{
       
  1767 			printf("kssl_cget_tkt rtn %d\n", krb5rc);
       
  1768 			if (krb5rc && kssl_err.text)
       
  1769 			  printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
       
  1770 			}
       
  1771 #endif	/* KSSL_DEBUG */
       
  1772 
       
  1773 			if (krb5rc)
       
  1774 				{
       
  1775 				ssl3_send_alert(s,SSL3_AL_FATAL,
       
  1776 						SSL_AD_HANDSHAKE_FAILURE);
       
  1777 				SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
       
  1778 						kssl_err.reason);
       
  1779 				goto err;
       
  1780 				}
       
  1781 
       
  1782 			/*  20010406 VRS - Earlier versions used KRB5 AP_REQ
       
  1783 			**  in place of RFC 2712 KerberosWrapper, as in:
       
  1784 			**
       
  1785 			**  Send ticket (copy to *p, set n = length)
       
  1786 			**  n = krb5_ap_req.length;
       
  1787 			**  memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
       
  1788 			**  if (krb5_ap_req.data)  
       
  1789 			**    kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
       
  1790 			**
       
  1791 			**  Now using real RFC 2712 KerberosWrapper
       
  1792 			**  (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
       
  1793 			**  Note: 2712 "opaque" types are here replaced
       
  1794 			**  with a 2-byte length followed by the value.
       
  1795 			**  Example:
       
  1796 			**  KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
       
  1797 			**  Where "xx xx" = length bytes.  Shown here with
       
  1798 			**  optional authenticator omitted.
       
  1799 			*/
       
  1800 
       
  1801 			/*  KerberosWrapper.Ticket		*/
       
  1802 			s2n(enc_ticket->length,p);
       
  1803 			memcpy(p, enc_ticket->data, enc_ticket->length);
       
  1804 			p+= enc_ticket->length;
       
  1805 			n = enc_ticket->length + 2;
       
  1806 
       
  1807 			/*  KerberosWrapper.Authenticator	*/
       
  1808 			if (authp  &&  authp->length)  
       
  1809 				{
       
  1810 				s2n(authp->length,p);
       
  1811 				memcpy(p, authp->data, authp->length);
       
  1812 				p+= authp->length;
       
  1813 				n+= authp->length + 2;
       
  1814 				
       
  1815 				free(authp->data);
       
  1816 				authp->data = NULL;
       
  1817 				authp->length = 0;
       
  1818 				}
       
  1819 			else
       
  1820 				{
       
  1821 				s2n(0,p);/*  null authenticator length	*/
       
  1822 				n+=2;
       
  1823 				}
       
  1824  
       
  1825 			    tmp_buf[0]=s->client_version>>8;
       
  1826 			    tmp_buf[1]=s->client_version&0xff;
       
  1827 			    if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
       
  1828 				goto err;
       
  1829 
       
  1830 			/*  20010420 VRS.  Tried it this way; failed.
       
  1831 			**	EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
       
  1832 			**	EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
       
  1833 			**				kssl_ctx->length);
       
  1834 			**	EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
       
  1835 			*/
       
  1836 
       
  1837 			memset(iv, 0, sizeof iv);  /* per RFC 1510 */
       
  1838 			EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,
       
  1839 				kssl_ctx->key,iv);
       
  1840 			EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf,
       
  1841 				sizeof tmp_buf);
       
  1842 			EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl);
       
  1843 			outl += padl;
       
  1844 			if (outl > sizeof epms)
       
  1845 				{
       
  1846 				SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
       
  1847 				goto err;
       
  1848 				}
       
  1849 			EVP_CIPHER_CTX_cleanup(&ciph_ctx);
       
  1850 
       
  1851 			/*  KerberosWrapper.EncryptedPreMasterSecret	*/
       
  1852 			s2n(outl,p);
       
  1853 			memcpy(p, epms, outl);
       
  1854 			p+=outl;
       
  1855 			n+=outl + 2;
       
  1856 
       
  1857 			s->session->master_key_length=
       
  1858 			        s->method->ssl3_enc->generate_master_secret(s,
       
  1859 					s->session->master_key,
       
  1860 					tmp_buf, sizeof tmp_buf);
       
  1861 
       
  1862 			OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
       
  1863 			OPENSSL_cleanse(epms, outl);
       
  1864 			}
       
  1865 #endif
       
  1866 #ifndef OPENSSL_NO_DH
       
  1867 		else if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
       
  1868 			{
       
  1869 			DH *dh_srvr,*dh_clnt;
       
  1870 
       
  1871 			if (s->session->sess_cert->peer_dh_tmp != NULL)
       
  1872 				dh_srvr=s->session->sess_cert->peer_dh_tmp;
       
  1873 			else
       
  1874 				{
       
  1875 				/* we get them from the cert */
       
  1876 				ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
       
  1877 				SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
       
  1878 				goto err;
       
  1879 				}
       
  1880 			
       
  1881 			/* generate a new random key */
       
  1882 			if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
       
  1883 				{
       
  1884 				SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
       
  1885 				goto err;
       
  1886 				}
       
  1887 			if (!DH_generate_key(dh_clnt))
       
  1888 				{
       
  1889 				SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
       
  1890 				goto err;
       
  1891 				}
       
  1892 
       
  1893 			/* use the 'p' output buffer for the DH key, but
       
  1894 			 * make sure to clear it out afterwards */
       
  1895 
       
  1896 			n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
       
  1897 
       
  1898 			if (n <= 0)
       
  1899 				{
       
  1900 				SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
       
  1901 				goto err;
       
  1902 				}
       
  1903 
       
  1904 			/* generate master key from the result */
       
  1905 			s->session->master_key_length=
       
  1906 				s->method->ssl3_enc->generate_master_secret(s,
       
  1907 					s->session->master_key,p,n);
       
  1908 			/* clean up */
       
  1909 			memset(p,0,n);
       
  1910 
       
  1911 			/* send off the data */
       
  1912 			n=BN_num_bytes(dh_clnt->pub_key);
       
  1913 			s2n(n,p);
       
  1914 			BN_bn2bin(dh_clnt->pub_key,p);
       
  1915 			n+=2;
       
  1916 
       
  1917 			DH_free(dh_clnt);
       
  1918 
       
  1919 			/* perhaps clean things up a bit EAY EAY EAY EAY*/
       
  1920 			}
       
  1921 #endif
       
  1922 
       
  1923 #ifndef OPENSSL_NO_ECDH 
       
  1924 		else if ((l & SSL_kECDH) || (l & SSL_kECDHE))
       
  1925 			{
       
  1926 			const EC_GROUP *srvr_group = NULL;
       
  1927 			EC_KEY *tkey;
       
  1928 			int ecdh_clnt_cert = 0;
       
  1929 			int field_size = 0;
       
  1930 
       
  1931 			/* Did we send out the client's
       
  1932 			 * ECDH share for use in premaster
       
  1933 			 * computation as part of client certificate?
       
  1934 			 * If so, set ecdh_clnt_cert to 1.
       
  1935 			 */
       
  1936 			if ((l & SSL_kECDH) && (s->cert != NULL)) 
       
  1937 				{
       
  1938 				/* XXX: For now, we do not support client
       
  1939 				 * authentication using ECDH certificates.
       
  1940 				 * To add such support, one needs to add
       
  1941 				 * code that checks for appropriate 
       
  1942 				 * conditions and sets ecdh_clnt_cert to 1.
       
  1943 				 * For example, the cert have an ECC
       
  1944 				 * key on the same curve as the server's
       
  1945 				 * and the key should be authorized for
       
  1946 				 * key agreement.
       
  1947 				 *
       
  1948 				 * One also needs to add code in ssl3_connect
       
  1949 				 * to skip sending the certificate verify
       
  1950 				 * message.
       
  1951 				 *
       
  1952 				 * if ((s->cert->key->privatekey != NULL) &&
       
  1953 				 *     (s->cert->key->privatekey->type ==
       
  1954 				 *      EVP_PKEY_EC) && ...)
       
  1955 				 * ecdh_clnt_cert = 1;
       
  1956 				 */
       
  1957 				}
       
  1958 
       
  1959 			if (s->session->sess_cert->peer_ecdh_tmp != NULL)
       
  1960 				{
       
  1961 				tkey = s->session->sess_cert->peer_ecdh_tmp;
       
  1962 				}
       
  1963 			else
       
  1964 				{
       
  1965 				/* Get the Server Public Key from Cert */
       
  1966 				srvr_pub_pkey = X509_get_pubkey(s->session-> \
       
  1967 				    sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
       
  1968 				if ((srvr_pub_pkey == NULL) ||
       
  1969 				    (srvr_pub_pkey->type != EVP_PKEY_EC) ||
       
  1970 				    (srvr_pub_pkey->pkey.ec == NULL))
       
  1971 					{
       
  1972 					SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
       
  1973 					    ERR_R_INTERNAL_ERROR);
       
  1974 					goto err;
       
  1975 					}
       
  1976 
       
  1977 				tkey = srvr_pub_pkey->pkey.ec;
       
  1978 				}
       
  1979 
       
  1980 			srvr_group   = EC_KEY_get0_group(tkey);
       
  1981 			srvr_ecpoint = EC_KEY_get0_public_key(tkey);
       
  1982 
       
  1983 			if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
       
  1984 				{
       
  1985 				SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
       
  1986 				    ERR_R_INTERNAL_ERROR);
       
  1987 				goto err;
       
  1988 				}
       
  1989 
       
  1990 			if ((clnt_ecdh=EC_KEY_new()) == NULL) 
       
  1991 				{
       
  1992 				SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
       
  1993 				goto err;
       
  1994 				}
       
  1995 
       
  1996 			if (!EC_KEY_set_group(clnt_ecdh, srvr_group))
       
  1997 				{
       
  1998 				SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
       
  1999 				goto err;
       
  2000 				}
       
  2001 			if (ecdh_clnt_cert) 
       
  2002 				{ 
       
  2003 				/* Reuse key info from our certificate
       
  2004 				 * We only need our private key to perform
       
  2005 				 * the ECDH computation.
       
  2006 				 */
       
  2007 				const BIGNUM *priv_key;
       
  2008 				tkey = s->cert->key->privatekey->pkey.ec;
       
  2009 				priv_key = EC_KEY_get0_private_key(tkey);
       
  2010 				if (priv_key == NULL)
       
  2011 					{
       
  2012 					SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
       
  2013 					goto err;
       
  2014 					}
       
  2015 				if (!EC_KEY_set_private_key(clnt_ecdh, priv_key))
       
  2016 					{
       
  2017 					SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB);
       
  2018 					goto err;
       
  2019 					}
       
  2020 				}
       
  2021 			else 
       
  2022 				{
       
  2023 				/* Generate a new ECDH key pair */
       
  2024 				if (!(EC_KEY_generate_key(clnt_ecdh)))
       
  2025 					{
       
  2026 					SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
       
  2027 					goto err;
       
  2028 					}
       
  2029 				}
       
  2030 
       
  2031 			/* use the 'p' output buffer for the ECDH key, but
       
  2032 			 * make sure to clear it out afterwards
       
  2033 			 */
       
  2034 
       
  2035 			field_size = EC_GROUP_get_degree(srvr_group);
       
  2036 			if (field_size <= 0)
       
  2037 				{
       
  2038 				SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 
       
  2039 				       ERR_R_ECDH_LIB);
       
  2040 				goto err;
       
  2041 				}
       
  2042 			n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL);
       
  2043 			if (n <= 0)
       
  2044 				{
       
  2045 				SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 
       
  2046 				       ERR_R_ECDH_LIB);
       
  2047 				goto err;
       
  2048 				}
       
  2049 
       
  2050 			/* generate master key from the result */
       
  2051 			s->session->master_key_length = s->method->ssl3_enc \
       
  2052 			    -> generate_master_secret(s, 
       
  2053 				s->session->master_key,
       
  2054 				p, n);
       
  2055 
       
  2056 			memset(p, 0, n); /* clean up */
       
  2057 
       
  2058 			if (ecdh_clnt_cert) 
       
  2059 				{
       
  2060 				/* Send empty client key exch message */
       
  2061 				n = 0;
       
  2062 				}
       
  2063 			else 
       
  2064 				{
       
  2065 				/* First check the size of encoding and
       
  2066 				 * allocate memory accordingly.
       
  2067 				 */
       
  2068 				encoded_pt_len = 
       
  2069 				    EC_POINT_point2oct(srvr_group, 
       
  2070 					EC_KEY_get0_public_key(clnt_ecdh), 
       
  2071 					POINT_CONVERSION_UNCOMPRESSED, 
       
  2072 					NULL, 0, NULL);
       
  2073 
       
  2074 				encodedPoint = (unsigned char *) 
       
  2075 				    OPENSSL_malloc(encoded_pt_len * 
       
  2076 					sizeof(unsigned char)); 
       
  2077 				bn_ctx = BN_CTX_new();
       
  2078 				if ((encodedPoint == NULL) || 
       
  2079 				    (bn_ctx == NULL)) 
       
  2080 					{
       
  2081 					SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
       
  2082 					goto err;
       
  2083 					}
       
  2084 
       
  2085 				/* Encode the public key */
       
  2086 				n = EC_POINT_point2oct(srvr_group, 
       
  2087 				    EC_KEY_get0_public_key(clnt_ecdh), 
       
  2088 				    POINT_CONVERSION_UNCOMPRESSED, 
       
  2089 				    encodedPoint, encoded_pt_len, bn_ctx);
       
  2090 
       
  2091 				*p = n; /* length of encoded point */
       
  2092 				/* Encoded point will be copied here */
       
  2093 				p += 1; 
       
  2094 				/* copy the point */
       
  2095 				memcpy((unsigned char *)p, encodedPoint, n);
       
  2096 				/* increment n to account for length field */
       
  2097 				n += 1; 
       
  2098 				}
       
  2099 
       
  2100 			/* Free allocated memory */
       
  2101 			BN_CTX_free(bn_ctx);
       
  2102 			if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
       
  2103 			if (clnt_ecdh != NULL) 
       
  2104 				 EC_KEY_free(clnt_ecdh);
       
  2105 			EVP_PKEY_free(srvr_pub_pkey);
       
  2106 			}
       
  2107 #endif /* !OPENSSL_NO_ECDH */
       
  2108 		else
       
  2109 			{
       
  2110 			ssl3_send_alert(s, SSL3_AL_FATAL,
       
  2111 			    SSL_AD_HANDSHAKE_FAILURE);
       
  2112 			SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
       
  2113 			    ERR_R_INTERNAL_ERROR);
       
  2114 			goto err;
       
  2115 			}
       
  2116 		
       
  2117 		*(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
       
  2118 		l2n3(n,d);
       
  2119 
       
  2120 		s->state=SSL3_ST_CW_KEY_EXCH_B;
       
  2121 		/* number of bytes to write */
       
  2122 		s->init_num=n+4;
       
  2123 		s->init_off=0;
       
  2124 		}
       
  2125 
       
  2126 	/* SSL3_ST_CW_KEY_EXCH_B */
       
  2127 	return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
       
  2128 err:
       
  2129 #ifndef OPENSSL_NO_ECDH
       
  2130 	BN_CTX_free(bn_ctx);
       
  2131 	if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
       
  2132 	if (clnt_ecdh != NULL) 
       
  2133 		EC_KEY_free(clnt_ecdh);
       
  2134 	EVP_PKEY_free(srvr_pub_pkey);
       
  2135 #endif
       
  2136 	return(-1);
       
  2137 	}
       
  2138 
       
  2139 int ssl3_send_client_verify(SSL *s)
       
  2140 	{
       
  2141 	unsigned char *p,*d;
       
  2142 	unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
       
  2143 	EVP_PKEY *pkey;
       
  2144 #ifndef OPENSSL_NO_RSA
       
  2145 	unsigned u=0;
       
  2146 #endif
       
  2147 	unsigned long n;
       
  2148 #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
       
  2149 	int j;
       
  2150 #endif
       
  2151 
       
  2152 	if (s->state == SSL3_ST_CW_CERT_VRFY_A)
       
  2153 		{
       
  2154 		d=(unsigned char *)s->init_buf->data;
       
  2155 		p= &(d[4]);
       
  2156 		pkey=s->cert->key->privatekey;
       
  2157 
       
  2158 		s->method->ssl3_enc->cert_verify_mac(s,&(s->s3->finish_dgst2),
       
  2159 			&(data[MD5_DIGEST_LENGTH]));
       
  2160 
       
  2161 #ifndef OPENSSL_NO_RSA
       
  2162 		if (pkey->type == EVP_PKEY_RSA)
       
  2163 			{
       
  2164 			s->method->ssl3_enc->cert_verify_mac(s,
       
  2165 				&(s->s3->finish_dgst1),&(data[0]));
       
  2166 			if (RSA_sign(NID_md5_sha1, data,
       
  2167 					 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
       
  2168 					&(p[2]), &u, pkey->pkey.rsa) <= 0 )
       
  2169 				{
       
  2170 				SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
       
  2171 				goto err;
       
  2172 				}
       
  2173 			s2n(u,p);
       
  2174 			n=u+2;
       
  2175 			}
       
  2176 		else
       
  2177 #endif
       
  2178 #ifndef OPENSSL_NO_DSA
       
  2179 			if (pkey->type == EVP_PKEY_DSA)
       
  2180 			{
       
  2181 			if (!DSA_sign(pkey->save_type,
       
  2182 				&(data[MD5_DIGEST_LENGTH]),
       
  2183 				SHA_DIGEST_LENGTH,&(p[2]),
       
  2184 				(unsigned int *)&j,pkey->pkey.dsa))
       
  2185 				{
       
  2186 				SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
       
  2187 				goto err;
       
  2188 				}
       
  2189 			s2n(j,p);
       
  2190 			n=j+2;
       
  2191 			}
       
  2192 		else
       
  2193 #endif
       
  2194 #ifndef OPENSSL_NO_ECDSA
       
  2195 			if (pkey->type == EVP_PKEY_EC)
       
  2196 			{
       
  2197 			if (!ECDSA_sign(pkey->save_type,
       
  2198 				&(data[MD5_DIGEST_LENGTH]),
       
  2199 				SHA_DIGEST_LENGTH,&(p[2]),
       
  2200 				(unsigned int *)&j,pkey->pkey.ec))
       
  2201 				{
       
  2202 				SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,
       
  2203 				    ERR_R_ECDSA_LIB);
       
  2204 				goto err;
       
  2205 				}
       
  2206 			s2n(j,p);
       
  2207 			n=j+2;
       
  2208 			}
       
  2209 		else
       
  2210 #endif
       
  2211 			{
       
  2212 			SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR);
       
  2213 			goto err;
       
  2214 			}
       
  2215 		*(d++)=SSL3_MT_CERTIFICATE_VERIFY;
       
  2216 		l2n3(n,d);
       
  2217 
       
  2218 		s->state=SSL3_ST_CW_CERT_VRFY_B;
       
  2219 		s->init_num=(int)n+4;
       
  2220 		s->init_off=0;
       
  2221 		}
       
  2222 	return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
       
  2223 err:
       
  2224 	return(-1);
       
  2225 	}
       
  2226 
       
  2227 int ssl3_send_client_certificate(SSL *s)
       
  2228 	{
       
  2229 	X509 *x509=NULL;
       
  2230 	EVP_PKEY *pkey=NULL;
       
  2231 	int i;
       
  2232 	unsigned long l;
       
  2233 
       
  2234 	if (s->state ==	SSL3_ST_CW_CERT_A)
       
  2235 		{
       
  2236 		if ((s->cert == NULL) ||
       
  2237 			(s->cert->key->x509 == NULL) ||
       
  2238 			(s->cert->key->privatekey == NULL))
       
  2239 			s->state=SSL3_ST_CW_CERT_B;
       
  2240 		else
       
  2241 			s->state=SSL3_ST_CW_CERT_C;
       
  2242 		}
       
  2243 
       
  2244 	/* We need to get a client cert */
       
  2245 	if (s->state == SSL3_ST_CW_CERT_B)
       
  2246 		{
       
  2247 		/* If we get an error, we need to
       
  2248 		 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
       
  2249 		 * We then get retied later */
       
  2250 		i=0;
       
  2251 		if (s->ctx->client_cert_cb != NULL)
       
  2252 			i=s->ctx->client_cert_cb(s,&(x509),&(pkey));
       
  2253 		if (i < 0)
       
  2254 			{
       
  2255 			s->rwstate=SSL_X509_LOOKUP;
       
  2256 			return(-1);
       
  2257 			}
       
  2258 		s->rwstate=SSL_NOTHING;
       
  2259 		if ((i == 1) && (pkey != NULL) && (x509 != NULL))
       
  2260 			{
       
  2261 			s->state=SSL3_ST_CW_CERT_B;
       
  2262 			if (	!SSL_use_certificate(s,x509) ||
       
  2263 				!SSL_use_PrivateKey(s,pkey))
       
  2264 				i=0;
       
  2265 			}
       
  2266 		else if (i == 1)
       
  2267 			{
       
  2268 			i=0;
       
  2269 			SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
       
  2270 			}
       
  2271 
       
  2272 		if (x509 != NULL) X509_free(x509);
       
  2273 		if (pkey != NULL) EVP_PKEY_free(pkey);
       
  2274 		if (i == 0)
       
  2275 			{
       
  2276 			if (s->version == SSL3_VERSION)
       
  2277 				{
       
  2278 				s->s3->tmp.cert_req=0;
       
  2279 				ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
       
  2280 				return(1);
       
  2281 				}
       
  2282 			else
       
  2283 				{
       
  2284 				s->s3->tmp.cert_req=2;
       
  2285 				}
       
  2286 			}
       
  2287 
       
  2288 		/* Ok, we have a cert */
       
  2289 		s->state=SSL3_ST_CW_CERT_C;
       
  2290 		}
       
  2291 
       
  2292 	if (s->state == SSL3_ST_CW_CERT_C)
       
  2293 		{
       
  2294 		s->state=SSL3_ST_CW_CERT_D;
       
  2295 		l=ssl3_output_cert_chain(s,
       
  2296 			(s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
       
  2297 		s->init_num=(int)l;
       
  2298 		s->init_off=0;
       
  2299 		}
       
  2300 	/* SSL3_ST_CW_CERT_D */
       
  2301 	return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
       
  2302 	}
       
  2303 
       
  2304 #define has_bits(i,m)	(((i)&(m)) == (m))
       
  2305 
       
  2306 int ssl3_check_cert_and_algorithm(SSL *s)
       
  2307 	{
       
  2308 	int i,idx;
       
  2309 	long algs;
       
  2310 	EVP_PKEY *pkey=NULL;
       
  2311 	SESS_CERT *sc;
       
  2312 #ifndef OPENSSL_NO_RSA
       
  2313 	RSA *rsa;
       
  2314 #endif
       
  2315 #ifndef OPENSSL_NO_DH
       
  2316 	DH *dh;
       
  2317 #endif
       
  2318 
       
  2319 	sc=s->session->sess_cert;
       
  2320 
       
  2321 	algs=s->s3->tmp.new_cipher->algorithms;
       
  2322 
       
  2323 	/* we don't have a certificate */
       
  2324 	if (algs & (SSL_aDH|SSL_aNULL|SSL_aKRB5))
       
  2325 		return(1);
       
  2326 
       
  2327 	if (sc == NULL)
       
  2328 		{
       
  2329 		SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,ERR_R_INTERNAL_ERROR);
       
  2330 		goto err;
       
  2331 		}
       
  2332 
       
  2333 #ifndef OPENSSL_NO_RSA
       
  2334 	rsa=s->session->sess_cert->peer_rsa_tmp;
       
  2335 #endif
       
  2336 #ifndef OPENSSL_NO_DH
       
  2337 	dh=s->session->sess_cert->peer_dh_tmp;
       
  2338 #endif
       
  2339 
       
  2340 	/* This is the passed certificate */
       
  2341 
       
  2342 	idx=sc->peer_cert_type;
       
  2343 #ifndef OPENSSL_NO_ECDH
       
  2344 	if (idx == SSL_PKEY_ECC)
       
  2345 		{
       
  2346 		if (check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509,
       
  2347 		    s->s3->tmp.new_cipher) == 0) 
       
  2348 			{ /* check failed */
       
  2349 			SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_BAD_ECC_CERT);
       
  2350 			goto f_err;			
       
  2351 			}
       
  2352 		else 
       
  2353 			{
       
  2354 			return 1;
       
  2355 			}
       
  2356 		}
       
  2357 #endif
       
  2358 	pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
       
  2359 	i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
       
  2360 	EVP_PKEY_free(pkey);
       
  2361 
       
  2362 	
       
  2363 	/* Check that we have a certificate if we require one */
       
  2364 	if ((algs & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
       
  2365 		{
       
  2366 		SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);
       
  2367 		goto f_err;
       
  2368 		}
       
  2369 #ifndef OPENSSL_NO_DSA
       
  2370 	else if ((algs & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
       
  2371 		{
       
  2372 		SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);
       
  2373 		goto f_err;
       
  2374 		}
       
  2375 #endif
       
  2376 #ifndef OPENSSL_NO_RSA
       
  2377 	if ((algs & SSL_kRSA) &&
       
  2378 		!(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
       
  2379 		{
       
  2380 		SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);
       
  2381 		goto f_err;
       
  2382 		}
       
  2383 #endif
       
  2384 #ifndef OPENSSL_NO_DH
       
  2385 	if ((algs & SSL_kEDH) &&
       
  2386 		!(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
       
  2387 		{
       
  2388 		SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);
       
  2389 		goto f_err;
       
  2390 		}
       
  2391 	else if ((algs & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
       
  2392 		{
       
  2393 		SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);
       
  2394 		goto f_err;
       
  2395 		}
       
  2396 #ifndef OPENSSL_NO_DSA
       
  2397 	else if ((algs & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
       
  2398 		{
       
  2399 		SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);
       
  2400 		goto f_err;
       
  2401 		}
       
  2402 #endif
       
  2403 #endif
       
  2404 
       
  2405 	if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))
       
  2406 		{
       
  2407 #ifndef OPENSSL_NO_RSA
       
  2408 		if (algs & SSL_kRSA)
       
  2409 			{
       
  2410 			if (rsa == NULL
       
  2411 			    || RSA_size(rsa)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
       
  2412 				{
       
  2413 				SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
       
  2414 				goto f_err;
       
  2415 				}
       
  2416 			}
       
  2417 		else
       
  2418 #endif
       
  2419 #ifndef OPENSSL_NO_DH
       
  2420 			if (algs & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
       
  2421 			    {
       
  2422 			    if (dh == NULL
       
  2423 				|| DH_size(dh)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
       
  2424 				{
       
  2425 				SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);
       
  2426 				goto f_err;
       
  2427 				}
       
  2428 			}
       
  2429 		else
       
  2430 #endif
       
  2431 			{
       
  2432 			SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
       
  2433 			goto f_err;
       
  2434 			}
       
  2435 		}
       
  2436 	return(1);
       
  2437 f_err:
       
  2438 	ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
       
  2439 err:
       
  2440 	return(0);
       
  2441 	}
       
  2442 
       
  2443 
       
  2444 #ifndef OPENSSL_NO_ECDH
       
  2445 /* This is the complement of nid2curve_id in s3_srvr.c. */
       
  2446 static int curve_id2nid(int curve_id)
       
  2447 {
       
  2448 	/* ECC curves from draft-ietf-tls-ecc-01.txt (Mar 15, 2001)
       
  2449 	 * (no changes in draft-ietf-tls-ecc-03.txt [June 2003]) */
       
  2450 	static int nid_list[26] =
       
  2451 	{
       
  2452 		0,
       
  2453 		NID_sect163k1, /* sect163k1 (1) */
       
  2454 		NID_sect163r1, /* sect163r1 (2) */
       
  2455 		NID_sect163r2, /* sect163r2 (3) */
       
  2456 		NID_sect193r1, /* sect193r1 (4) */ 
       
  2457 		NID_sect193r2, /* sect193r2 (5) */ 
       
  2458 		NID_sect233k1, /* sect233k1 (6) */
       
  2459 		NID_sect233r1, /* sect233r1 (7) */ 
       
  2460 		NID_sect239k1, /* sect239k1 (8) */ 
       
  2461 		NID_sect283k1, /* sect283k1 (9) */
       
  2462 		NID_sect283r1, /* sect283r1 (10) */ 
       
  2463 		NID_sect409k1, /* sect409k1 (11) */ 
       
  2464 		NID_sect409r1, /* sect409r1 (12) */
       
  2465 		NID_sect571k1, /* sect571k1 (13) */ 
       
  2466 		NID_sect571r1, /* sect571r1 (14) */ 
       
  2467 		NID_secp160k1, /* secp160k1 (15) */
       
  2468 		NID_secp160r1, /* secp160r1 (16) */ 
       
  2469 		NID_secp160r2, /* secp160r2 (17) */ 
       
  2470 		NID_secp192k1, /* secp192k1 (18) */
       
  2471 		NID_X9_62_prime192v1, /* secp192r1 (19) */ 
       
  2472 		NID_secp224k1, /* secp224k1 (20) */ 
       
  2473 		NID_secp224r1, /* secp224r1 (21) */
       
  2474 		NID_secp256k1, /* secp256k1 (22) */ 
       
  2475 		NID_X9_62_prime256v1, /* secp256r1 (23) */ 
       
  2476 		NID_secp384r1, /* secp384r1 (24) */
       
  2477 		NID_secp521r1  /* secp521r1 (25) */	
       
  2478 	};
       
  2479 	
       
  2480 	if ((curve_id < 1) || (curve_id > 25)) return 0;
       
  2481 
       
  2482 	return nid_list[curve_id];
       
  2483 }
       
  2484 #endif
       
  2485 
       
  2486