Home | History | Annotate | Download | only in ssl
      1 /*! \file ssl/ssl_lib.c
      2  *  \brief Version independent SSL functions.
      3  */
      4 /* Copyright (C) 1995-1998 Eric Young (eay (at) cryptsoft.com)
      5  * All rights reserved.
      6  *
      7  * This package is an SSL implementation written
      8  * by Eric Young (eay (at) cryptsoft.com).
      9  * The implementation was written so as to conform with Netscapes SSL.
     10  *
     11  * This library is free for commercial and non-commercial use as long as
     12  * the following conditions are aheared to.  The following conditions
     13  * apply to all code found in this distribution, be it the RC4, RSA,
     14  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
     15  * included with this distribution is covered by the same copyright terms
     16  * except that the holder is Tim Hudson (tjh (at) cryptsoft.com).
     17  *
     18  * Copyright remains Eric Young's, and as such any Copyright notices in
     19  * the code are not to be removed.
     20  * If this package is used in a product, Eric Young should be given attribution
     21  * as the author of the parts of the library used.
     22  * This can be in the form of a textual message at program startup or
     23  * in documentation (online or textual) provided with the package.
     24  *
     25  * Redistribution and use in source and binary forms, with or without
     26  * modification, are permitted provided that the following conditions
     27  * are met:
     28  * 1. Redistributions of source code must retain the copyright
     29  *    notice, this list of conditions and the following disclaimer.
     30  * 2. Redistributions in binary form must reproduce the above copyright
     31  *    notice, this list of conditions and the following disclaimer in the
     32  *    documentation and/or other materials provided with the distribution.
     33  * 3. All advertising materials mentioning features or use of this software
     34  *    must display the following acknowledgement:
     35  *    "This product includes cryptographic software written by
     36  *     Eric Young (eay (at) cryptsoft.com)"
     37  *    The word 'cryptographic' can be left out if the rouines from the library
     38  *    being used are not cryptographic related :-).
     39  * 4. If you include any Windows specific code (or a derivative thereof) from
     40  *    the apps directory (application code) you must include an acknowledgement:
     41  *    "This product includes software written by Tim Hudson (tjh (at) cryptsoft.com)"
     42  *
     43  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
     44  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     45  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     46  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     47  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     48  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     49  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     50  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     51  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     52  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     53  * SUCH DAMAGE.
     54  *
     55  * The licence and distribution terms for any publically available version or
     56  * derivative of this code cannot be changed.  i.e. this code cannot simply be
     57  * copied and put under another distribution licence
     58  * [including the GNU Public Licence.]
     59  */
     60 /* ====================================================================
     61  * Copyright (c) 1998-2007 The OpenSSL Project.  All rights reserved.
     62  *
     63  * Redistribution and use in source and binary forms, with or without
     64  * modification, are permitted provided that the following conditions
     65  * are met:
     66  *
     67  * 1. Redistributions of source code must retain the above copyright
     68  *    notice, this list of conditions and the following disclaimer.
     69  *
     70  * 2. Redistributions in binary form must reproduce the above copyright
     71  *    notice, this list of conditions and the following disclaimer in
     72  *    the documentation and/or other materials provided with the
     73  *    distribution.
     74  *
     75  * 3. All advertising materials mentioning features or use of this
     76  *    software must display the following acknowledgment:
     77  *    "This product includes software developed by the OpenSSL Project
     78  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
     79  *
     80  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
     81  *    endorse or promote products derived from this software without
     82  *    prior written permission. For written permission, please contact
     83  *    openssl-core (at) openssl.org.
     84  *
     85  * 5. Products derived from this software may not be called "OpenSSL"
     86  *    nor may "OpenSSL" appear in their names without prior written
     87  *    permission of the OpenSSL Project.
     88  *
     89  * 6. Redistributions of any form whatsoever must retain the following
     90  *    acknowledgment:
     91  *    "This product includes software developed by the OpenSSL Project
     92  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
     93  *
     94  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
     95  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     96  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     97  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
     98  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     99  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    100  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    101  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    102  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    103  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    104  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    105  * OF THE POSSIBILITY OF SUCH DAMAGE.
    106  * ====================================================================
    107  *
    108  * This product includes cryptographic software written by Eric Young
    109  * (eay (at) cryptsoft.com).  This product includes software written by Tim
    110  * Hudson (tjh (at) cryptsoft.com).
    111  *
    112  */
    113 /* ====================================================================
    114  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
    115  * ECC cipher suite support in OpenSSL originally developed by
    116  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
    117  */
    118 /* ====================================================================
    119  * Copyright 2005 Nokia. All rights reserved.
    120  *
    121  * The portions of the attached software ("Contribution") is developed by
    122  * Nokia Corporation and is licensed pursuant to the OpenSSL open source
    123  * license.
    124  *
    125  * The Contribution, originally written by Mika Kousa and Pasi Eronen of
    126  * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
    127  * support (see RFC 4279) to OpenSSL.
    128  *
    129  * No patent licenses or other rights except those expressly stated in
    130  * the OpenSSL open source license shall be deemed granted or received
    131  * expressly, by implication, estoppel, or otherwise.
    132  *
    133  * No assurances are provided by Nokia that the Contribution does not
    134  * infringe the patent or other intellectual property rights of any third
    135  * party or that the license provides you with all the necessary rights
    136  * to make use of the Contribution.
    137  *
    138  * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
    139  * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
    140  * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
    141  * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
    142  * OTHERWISE.
    143  */
    144 
    145 #ifdef REF_CHECK
    146 #  include <assert.h>
    147 #endif
    148 #include <stdio.h>
    149 #include "ssl_locl.h"
    150 #include "kssl_lcl.h"
    151 #include <openssl/objects.h>
    152 #include <openssl/lhash.h>
    153 #include <openssl/x509v3.h>
    154 #include <openssl/rand.h>
    155 #include <openssl/ocsp.h>
    156 #ifndef OPENSSL_NO_DH
    157 #include <openssl/dh.h>
    158 #endif
    159 #ifndef OPENSSL_NO_ENGINE
    160 #include <openssl/engine.h>
    161 #endif
    162 
    163 const char *SSL_version_str=OPENSSL_VERSION_TEXT;
    164 
    165 SSL3_ENC_METHOD ssl3_undef_enc_method={
    166 	/* evil casts, but these functions are only called if there's a library bug */
    167 	(int (*)(SSL *,int))ssl_undefined_function,
    168 	(int (*)(SSL *, unsigned char *, int))ssl_undefined_function,
    169 	ssl_undefined_function,
    170 	(int (*)(SSL *, unsigned char *, unsigned char *, int))ssl_undefined_function,
    171 	(int (*)(SSL*, int))ssl_undefined_function,
    172 	(int (*)(SSL *,  const char*, int, unsigned char *))ssl_undefined_function,
    173 	0,	/* finish_mac_length */
    174 	(int (*)(SSL *, int, unsigned char *))ssl_undefined_function,
    175 	NULL,	/* client_finished_label */
    176 	0,	/* client_finished_label_len */
    177 	NULL,	/* server_finished_label */
    178 	0,	/* server_finished_label_len */
    179 	(int (*)(int))ssl_undefined_function,
    180 	(int (*)(SSL *, unsigned char *, size_t, const char *,
    181 		 size_t, const unsigned char *, size_t,
    182 		 int use_context)) ssl_undefined_function,
    183 	};
    184 
    185 int SSL_clear(SSL *s)
    186 	{
    187 
    188 	if (s->method == NULL)
    189 		{
    190 		SSLerr(SSL_F_SSL_CLEAR,SSL_R_NO_METHOD_SPECIFIED);
    191 		return(0);
    192 		}
    193 
    194 	if (ssl_clear_bad_session(s))
    195 		{
    196 		SSL_SESSION_free(s->session);
    197 		s->session=NULL;
    198 		}
    199 
    200 	s->error=0;
    201 	s->hit=0;
    202 	s->shutdown=0;
    203 
    204 #if 0 /* Disabled since version 1.10 of this file (early return not
    205        * needed because SSL_clear is not called when doing renegotiation) */
    206 	/* This is set if we are doing dynamic renegotiation so keep
    207 	 * the old cipher.  It is sort of a SSL_clear_lite :-) */
    208 	if (s->renegotiate) return(1);
    209 #else
    210 	if (s->renegotiate)
    211 		{
    212 		SSLerr(SSL_F_SSL_CLEAR,ERR_R_INTERNAL_ERROR);
    213 		return 0;
    214 		}
    215 #endif
    216 
    217 	s->type=0;
    218 
    219 	s->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT);
    220 
    221 	s->version=s->method->version;
    222 	s->client_version=s->version;
    223 	s->rwstate=SSL_NOTHING;
    224 	s->rstate=SSL_ST_READ_HEADER;
    225 #if 0
    226 	s->read_ahead=s->ctx->read_ahead;
    227 #endif
    228 
    229 	if (s->init_buf != NULL)
    230 		{
    231 		BUF_MEM_free(s->init_buf);
    232 		s->init_buf=NULL;
    233 		}
    234 
    235 	ssl_clear_cipher_ctx(s);
    236 	ssl_clear_hash_ctx(&s->read_hash);
    237 	ssl_clear_hash_ctx(&s->write_hash);
    238 
    239 	s->first_packet=0;
    240 
    241 #if 1
    242 	/* Check to see if we were changed into a different method, if
    243 	 * so, revert back if we are not doing session-id reuse. */
    244 	if (!s->in_handshake && (s->session == NULL) && (s->method != s->ctx->method))
    245 		{
    246 		s->method->ssl_free(s);
    247 		s->method=s->ctx->method;
    248 		if (!s->method->ssl_new(s))
    249 			return(0);
    250 		}
    251 	else
    252 #endif
    253 		s->method->ssl_clear(s);
    254 	return(1);
    255 	}
    256 
    257 /** Used to change an SSL_CTXs default SSL method type */
    258 int SSL_CTX_set_ssl_version(SSL_CTX *ctx,const SSL_METHOD *meth)
    259 	{
    260 	STACK_OF(SSL_CIPHER) *sk;
    261 
    262 	ctx->method=meth;
    263 
    264 	sk=ssl_create_cipher_list(ctx->method,&(ctx->cipher_list),
    265 		&(ctx->cipher_list_by_id),
    266 		meth->version == SSL2_VERSION ? "SSLv2" : SSL_DEFAULT_CIPHER_LIST);
    267 	if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0))
    268 		{
    269 		SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION,SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
    270 		return(0);
    271 		}
    272 	return(1);
    273 	}
    274 
    275 SSL *SSL_new(SSL_CTX *ctx)
    276 	{
    277 	SSL *s;
    278 
    279 	if (ctx == NULL)
    280 		{
    281 		SSLerr(SSL_F_SSL_NEW,SSL_R_NULL_SSL_CTX);
    282 		return(NULL);
    283 		}
    284 	if (ctx->method == NULL)
    285 		{
    286 		SSLerr(SSL_F_SSL_NEW,SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
    287 		return(NULL);
    288 		}
    289 
    290 	s=(SSL *)OPENSSL_malloc(sizeof(SSL));
    291 	if (s == NULL) goto err;
    292 	memset(s,0,sizeof(SSL));
    293 
    294 #ifndef	OPENSSL_NO_KRB5
    295 	s->kssl_ctx = kssl_ctx_new();
    296 #endif	/* OPENSSL_NO_KRB5 */
    297 
    298 	s->options=ctx->options;
    299 	s->mode=ctx->mode;
    300 	s->max_cert_list=ctx->max_cert_list;
    301 
    302 	if (ctx->cert != NULL)
    303 		{
    304 		/* Earlier library versions used to copy the pointer to
    305 		 * the CERT, not its contents; only when setting new
    306 		 * parameters for the per-SSL copy, ssl_cert_new would be
    307 		 * called (and the direct reference to the per-SSL_CTX
    308 		 * settings would be lost, but those still were indirectly
    309 		 * accessed for various purposes, and for that reason they
    310 		 * used to be known as s->ctx->default_cert).
    311 		 * Now we don't look at the SSL_CTX's CERT after having
    312 		 * duplicated it once. */
    313 
    314 		s->cert = ssl_cert_dup(ctx->cert);
    315 		if (s->cert == NULL)
    316 			goto err;
    317 		}
    318 	else
    319 		s->cert=NULL; /* Cannot really happen (see SSL_CTX_new) */
    320 
    321 	s->read_ahead=ctx->read_ahead;
    322 	s->msg_callback=ctx->msg_callback;
    323 	s->msg_callback_arg=ctx->msg_callback_arg;
    324 	s->verify_mode=ctx->verify_mode;
    325 #if 0
    326 	s->verify_depth=ctx->verify_depth;
    327 #endif
    328 	s->sid_ctx_length=ctx->sid_ctx_length;
    329 	OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx);
    330 	memcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));
    331 	s->verify_callback=ctx->default_verify_callback;
    332 	s->session_creation_enabled=1;
    333 	s->generate_session_id=ctx->generate_session_id;
    334 
    335 	s->param = X509_VERIFY_PARAM_new();
    336 	if (!s->param)
    337 		goto err;
    338 	X509_VERIFY_PARAM_inherit(s->param, ctx->param);
    339 #if 0
    340 	s->purpose = ctx->purpose;
    341 	s->trust = ctx->trust;
    342 #endif
    343 	s->quiet_shutdown=ctx->quiet_shutdown;
    344 	s->max_send_fragment = ctx->max_send_fragment;
    345 
    346 	CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
    347 	s->ctx=ctx;
    348 #ifndef OPENSSL_NO_TLSEXT
    349 	s->tlsext_debug_cb = 0;
    350 	s->tlsext_debug_arg = NULL;
    351 	s->tlsext_ticket_expected = 0;
    352 	s->tlsext_status_type = -1;
    353 	s->tlsext_status_expected = 0;
    354 	s->tlsext_ocsp_ids = NULL;
    355 	s->tlsext_ocsp_exts = NULL;
    356 	s->tlsext_ocsp_resp = NULL;
    357 	s->tlsext_ocsp_resplen = -1;
    358 	CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
    359 	s->initial_ctx=ctx;
    360 # ifndef OPENSSL_NO_NEXTPROTONEG
    361 	s->next_proto_negotiated = NULL;
    362 # endif
    363 
    364 	if (s->ctx->alpn_client_proto_list)
    365 		{
    366 		s->alpn_client_proto_list =
    367 			OPENSSL_malloc(s->ctx->alpn_client_proto_list_len);
    368 		if (s->alpn_client_proto_list == NULL)
    369 			goto err;
    370 		memcpy(s->alpn_client_proto_list, s->ctx->alpn_client_proto_list,
    371 		       s->ctx->alpn_client_proto_list_len);
    372 		s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
    373 		}
    374 #endif
    375 
    376 	s->verify_result=X509_V_OK;
    377 
    378 	s->method=ctx->method;
    379 
    380 	if (!s->method->ssl_new(s))
    381 		goto err;
    382 
    383 	s->references=1;
    384 	s->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;
    385 
    386 	SSL_clear(s);
    387 
    388 	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
    389 
    390 #ifndef OPENSSL_NO_PSK
    391 	s->psk_client_callback=ctx->psk_client_callback;
    392 	s->psk_server_callback=ctx->psk_server_callback;
    393 #endif
    394 
    395 	return(s);
    396 err:
    397 	if (s != NULL)
    398 		{
    399 		if (s->cert != NULL)
    400 			ssl_cert_free(s->cert);
    401 		if (s->ctx != NULL)
    402 			SSL_CTX_free(s->ctx); /* decrement reference count */
    403 		OPENSSL_free(s);
    404 		}
    405 	SSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE);
    406 	return(NULL);
    407 	}
    408 
    409 int SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx,
    410 				   unsigned int sid_ctx_len)
    411     {
    412     if(sid_ctx_len > sizeof ctx->sid_ctx)
    413 	{
    414 	SSLerr(SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
    415 	return 0;
    416 	}
    417     ctx->sid_ctx_length=sid_ctx_len;
    418     memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len);
    419 
    420     return 1;
    421     }
    422 
    423 int SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx,
    424 			       unsigned int sid_ctx_len)
    425     {
    426     if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
    427 	{
    428 	SSLerr(SSL_F_SSL_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
    429 	return 0;
    430 	}
    431     ssl->sid_ctx_length=sid_ctx_len;
    432     memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len);
    433 
    434     return 1;
    435     }
    436 
    437 int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
    438 	{
    439 	CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
    440 	ctx->generate_session_id = cb;
    441 	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
    442 	return 1;
    443 	}
    444 
    445 int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
    446 	{
    447 	CRYPTO_w_lock(CRYPTO_LOCK_SSL);
    448 	ssl->generate_session_id = cb;
    449 	CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
    450 	return 1;
    451 	}
    452 
    453 int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
    454 				unsigned int id_len)
    455 	{
    456 	/* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
    457 	 * we can "construct" a session to give us the desired check - ie. to
    458 	 * find if there's a session in the hash table that would conflict with
    459 	 * any new session built out of this id/id_len and the ssl_version in
    460 	 * use by this SSL. */
    461 	SSL_SESSION r, *p;
    462 
    463 	if(id_len > sizeof r.session_id)
    464 		return 0;
    465 
    466 	r.ssl_version = ssl->version;
    467 	r.session_id_length = id_len;
    468 	memcpy(r.session_id, id, id_len);
    469 	/* NB: SSLv2 always uses a fixed 16-byte session ID, so even if a
    470 	 * callback is calling us to check the uniqueness of a shorter ID, it
    471 	 * must be compared as a padded-out ID because that is what it will be
    472 	 * converted to when the callback has finished choosing it. */
    473 	if((r.ssl_version == SSL2_VERSION) &&
    474 			(id_len < SSL2_SSL_SESSION_ID_LENGTH))
    475 		{
    476 		memset(r.session_id + id_len, 0,
    477 			SSL2_SSL_SESSION_ID_LENGTH - id_len);
    478 		r.session_id_length = SSL2_SSL_SESSION_ID_LENGTH;
    479 		}
    480 
    481 	CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
    482 	p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
    483 	CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
    484 	return (p != NULL);
    485 	}
    486 
    487 int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
    488 	{
    489 	return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
    490 	}
    491 
    492 int SSL_set_purpose(SSL *s, int purpose)
    493 	{
    494 	return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
    495 	}
    496 
    497 int SSL_CTX_set_trust(SSL_CTX *s, int trust)
    498 	{
    499 	return X509_VERIFY_PARAM_set_trust(s->param, trust);
    500 	}
    501 
    502 int SSL_set_trust(SSL *s, int trust)
    503 	{
    504 	return X509_VERIFY_PARAM_set_trust(s->param, trust);
    505 	}
    506 
    507 int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
    508 	{
    509 	return X509_VERIFY_PARAM_set1(ctx->param, vpm);
    510 	}
    511 
    512 int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
    513 	{
    514 	return X509_VERIFY_PARAM_set1(ssl->param, vpm);
    515 	}
    516 
    517 void SSL_free(SSL *s)
    518 	{
    519 	int i;
    520 
    521 	if(s == NULL)
    522 	    return;
    523 
    524 	i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);
    525 #ifdef REF_PRINT
    526 	REF_PRINT("SSL",s);
    527 #endif
    528 	if (i > 0) return;
    529 #ifdef REF_CHECK
    530 	if (i < 0)
    531 		{
    532 		fprintf(stderr,"SSL_free, bad reference count\n");
    533 		abort(); /* ok */
    534 		}
    535 #endif
    536 
    537 	if (s->param)
    538 		X509_VERIFY_PARAM_free(s->param);
    539 
    540 	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
    541 
    542 	if (s->bbio != NULL)
    543 		{
    544 		/* If the buffering BIO is in place, pop it off */
    545 		if (s->bbio == s->wbio)
    546 			{
    547 			s->wbio=BIO_pop(s->wbio);
    548 			}
    549 		BIO_free(s->bbio);
    550 		s->bbio=NULL;
    551 		}
    552 	if (s->rbio != NULL)
    553 		BIO_free_all(s->rbio);
    554 	if ((s->wbio != NULL) && (s->wbio != s->rbio))
    555 		BIO_free_all(s->wbio);
    556 
    557 	if (s->init_buf != NULL) BUF_MEM_free(s->init_buf);
    558 
    559 	/* add extra stuff */
    560 	if (s->cipher_list != NULL) sk_SSL_CIPHER_free(s->cipher_list);
    561 	if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);
    562 
    563 	/* Make the next call work :-) */
    564 	if (s->session != NULL)
    565 		{
    566 		ssl_clear_bad_session(s);
    567 		SSL_SESSION_free(s->session);
    568 		}
    569 
    570 	ssl_clear_cipher_ctx(s);
    571 	ssl_clear_hash_ctx(&s->read_hash);
    572 	ssl_clear_hash_ctx(&s->write_hash);
    573 
    574 	if (s->cert != NULL) ssl_cert_free(s->cert);
    575 	/* Free up if allocated */
    576 
    577 #ifndef OPENSSL_NO_TLSEXT
    578 	if (s->tlsext_hostname)
    579 		OPENSSL_free(s->tlsext_hostname);
    580 	if (s->initial_ctx) SSL_CTX_free(s->initial_ctx);
    581 #ifndef OPENSSL_NO_EC
    582 	if (s->tlsext_ecpointformatlist) OPENSSL_free(s->tlsext_ecpointformatlist);
    583 	if (s->tlsext_ellipticcurvelist) OPENSSL_free(s->tlsext_ellipticcurvelist);
    584 #endif /* OPENSSL_NO_EC */
    585 	if (s->tlsext_opaque_prf_input) OPENSSL_free(s->tlsext_opaque_prf_input);
    586 	if (s->tlsext_ocsp_exts)
    587 		sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
    588 						X509_EXTENSION_free);
    589 	if (s->tlsext_ocsp_ids)
    590 		sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free);
    591 	if (s->tlsext_ocsp_resp)
    592 		OPENSSL_free(s->tlsext_ocsp_resp);
    593 	if (s->tlsext_channel_id_private)
    594 		EVP_PKEY_free(s->tlsext_channel_id_private);
    595 	if (s->alpn_client_proto_list)
    596 		OPENSSL_free(s->alpn_client_proto_list);
    597 #endif
    598 
    599 	if (s->client_CA != NULL)
    600 		sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);
    601 
    602 	if (s->method != NULL) s->method->ssl_free(s);
    603 
    604 	if (s->ctx) SSL_CTX_free(s->ctx);
    605 
    606 #ifndef	OPENSSL_NO_KRB5
    607 	if (s->kssl_ctx != NULL)
    608 		kssl_ctx_free(s->kssl_ctx);
    609 #endif	/* OPENSSL_NO_KRB5 */
    610 
    611 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
    612 	if (s->next_proto_negotiated)
    613 		OPENSSL_free(s->next_proto_negotiated);
    614 #endif
    615 
    616 #ifndef OPENSSL_NO_SRTP
    617         if (s->srtp_profiles)
    618             sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
    619 #endif
    620 
    621 	OPENSSL_free(s);
    622 	}
    623 
    624 void SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio)
    625 	{
    626 	/* If the output buffering BIO is still in place, remove it
    627 	 */
    628 	if (s->bbio != NULL)
    629 		{
    630 		if (s->wbio == s->bbio)
    631 			{
    632 			s->wbio=s->wbio->next_bio;
    633 			s->bbio->next_bio=NULL;
    634 			}
    635 		}
    636 	if ((s->rbio != NULL) && (s->rbio != rbio))
    637 		BIO_free_all(s->rbio);
    638 	if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
    639 		BIO_free_all(s->wbio);
    640 	s->rbio=rbio;
    641 	s->wbio=wbio;
    642 	}
    643 
    644 BIO *SSL_get_rbio(const SSL *s)
    645 	{ return(s->rbio); }
    646 
    647 BIO *SSL_get_wbio(const SSL *s)
    648 	{ return(s->wbio); }
    649 
    650 int SSL_get_fd(const SSL *s)
    651 	{
    652 	return(SSL_get_rfd(s));
    653 	}
    654 
    655 int SSL_get_rfd(const SSL *s)
    656 	{
    657 	int ret= -1;
    658 	BIO *b,*r;
    659 
    660 	b=SSL_get_rbio(s);
    661 	r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
    662 	if (r != NULL)
    663 		BIO_get_fd(r,&ret);
    664 	return(ret);
    665 	}
    666 
    667 int SSL_get_wfd(const SSL *s)
    668 	{
    669 	int ret= -1;
    670 	BIO *b,*r;
    671 
    672 	b=SSL_get_wbio(s);
    673 	r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
    674 	if (r != NULL)
    675 		BIO_get_fd(r,&ret);
    676 	return(ret);
    677 	}
    678 
    679 #ifndef OPENSSL_NO_SOCK
    680 int SSL_set_fd(SSL *s,int fd)
    681 	{
    682 	int ret=0;
    683 	BIO *bio=NULL;
    684 
    685 	bio=BIO_new(BIO_s_socket());
    686 
    687 	if (bio == NULL)
    688 		{
    689 		SSLerr(SSL_F_SSL_SET_FD,ERR_R_BUF_LIB);
    690 		goto err;
    691 		}
    692 	BIO_set_fd(bio,fd,BIO_NOCLOSE);
    693 	SSL_set_bio(s,bio,bio);
    694 	ret=1;
    695 err:
    696 	return(ret);
    697 	}
    698 
    699 int SSL_set_wfd(SSL *s,int fd)
    700 	{
    701 	int ret=0;
    702 	BIO *bio=NULL;
    703 
    704 	if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET)
    705 		|| ((int)BIO_get_fd(s->rbio,NULL) != fd))
    706 		{
    707 		bio=BIO_new(BIO_s_socket());
    708 
    709 		if (bio == NULL)
    710 			{ SSLerr(SSL_F_SSL_SET_WFD,ERR_R_BUF_LIB); goto err; }
    711 		BIO_set_fd(bio,fd,BIO_NOCLOSE);
    712 		SSL_set_bio(s,SSL_get_rbio(s),bio);
    713 		}
    714 	else
    715 		SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s));
    716 	ret=1;
    717 err:
    718 	return(ret);
    719 	}
    720 
    721 int SSL_set_rfd(SSL *s,int fd)
    722 	{
    723 	int ret=0;
    724 	BIO *bio=NULL;
    725 
    726 	if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET)
    727 		|| ((int)BIO_get_fd(s->wbio,NULL) != fd))
    728 		{
    729 		bio=BIO_new(BIO_s_socket());
    730 
    731 		if (bio == NULL)
    732 			{
    733 			SSLerr(SSL_F_SSL_SET_RFD,ERR_R_BUF_LIB);
    734 			goto err;
    735 			}
    736 		BIO_set_fd(bio,fd,BIO_NOCLOSE);
    737 		SSL_set_bio(s,bio,SSL_get_wbio(s));
    738 		}
    739 	else
    740 		SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s));
    741 	ret=1;
    742 err:
    743 	return(ret);
    744 	}
    745 #endif
    746 
    747 
    748 /* return length of latest Finished message we sent, copy to 'buf' */
    749 size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
    750 	{
    751 	size_t ret = 0;
    752 
    753 	if (s->s3 != NULL)
    754 		{
    755 		ret = s->s3->tmp.finish_md_len;
    756 		if (count > ret)
    757 			count = ret;
    758 		memcpy(buf, s->s3->tmp.finish_md, count);
    759 		}
    760 	return ret;
    761 	}
    762 
    763 /* return length of latest Finished message we expected, copy to 'buf' */
    764 size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
    765 	{
    766 	size_t ret = 0;
    767 
    768 	if (s->s3 != NULL)
    769 		{
    770 		ret = s->s3->tmp.peer_finish_md_len;
    771 		if (count > ret)
    772 			count = ret;
    773 		memcpy(buf, s->s3->tmp.peer_finish_md, count);
    774 		}
    775 	return ret;
    776 	}
    777 
    778 
    779 int SSL_get_verify_mode(const SSL *s)
    780 	{
    781 	return(s->verify_mode);
    782 	}
    783 
    784 int SSL_get_verify_depth(const SSL *s)
    785 	{
    786 	return X509_VERIFY_PARAM_get_depth(s->param);
    787 	}
    788 
    789 int (*SSL_get_verify_callback(const SSL *s))(int,X509_STORE_CTX *)
    790 	{
    791 	return(s->verify_callback);
    792 	}
    793 
    794 int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
    795 	{
    796 	return(ctx->verify_mode);
    797 	}
    798 
    799 int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
    800 	{
    801 	return X509_VERIFY_PARAM_get_depth(ctx->param);
    802 	}
    803 
    804 int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int,X509_STORE_CTX *)
    805 	{
    806 	return(ctx->default_verify_callback);
    807 	}
    808 
    809 void SSL_set_verify(SSL *s,int mode,
    810 		    int (*callback)(int ok,X509_STORE_CTX *ctx))
    811 	{
    812 	s->verify_mode=mode;
    813 	if (callback != NULL)
    814 		s->verify_callback=callback;
    815 	}
    816 
    817 void SSL_set_verify_depth(SSL *s,int depth)
    818 	{
    819 	X509_VERIFY_PARAM_set_depth(s->param, depth);
    820 	}
    821 
    822 void SSL_set_read_ahead(SSL *s,int yes)
    823 	{
    824 	s->read_ahead=yes;
    825 	}
    826 
    827 int SSL_get_read_ahead(const SSL *s)
    828 	{
    829 	return(s->read_ahead);
    830 	}
    831 
    832 int SSL_pending(const SSL *s)
    833 	{
    834 	/* SSL_pending cannot work properly if read-ahead is enabled
    835 	 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)),
    836 	 * and it is impossible to fix since SSL_pending cannot report
    837 	 * errors that may be observed while scanning the new data.
    838 	 * (Note that SSL_pending() is often used as a boolean value,
    839 	 * so we'd better not return -1.)
    840 	 */
    841 	return(s->method->ssl_pending(s));
    842 	}
    843 
    844 X509 *SSL_get_peer_certificate(const SSL *s)
    845 	{
    846 	X509 *r;
    847 
    848 	if ((s == NULL) || (s->session == NULL))
    849 		r=NULL;
    850 	else
    851 		r=s->session->peer;
    852 
    853 	if (r == NULL) return(r);
    854 
    855 	CRYPTO_add(&r->references,1,CRYPTO_LOCK_X509);
    856 
    857 	return(r);
    858 	}
    859 
    860 STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
    861 	{
    862 	STACK_OF(X509) *r;
    863 
    864 	if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL))
    865 		r=NULL;
    866 	else
    867 		r=s->session->sess_cert->cert_chain;
    868 
    869 	/* If we are a client, cert_chain includes the peer's own
    870 	 * certificate; if we are a server, it does not. */
    871 
    872 	return(r);
    873 	}
    874 
    875 /* Now in theory, since the calling process own 't' it should be safe to
    876  * modify.  We need to be able to read f without being hassled */
    877 void SSL_copy_session_id(SSL *t,const SSL *f)
    878 	{
    879 	CERT *tmp;
    880 
    881 	/* Do we need to to SSL locking? */
    882 	SSL_set_session(t,SSL_get_session(f));
    883 
    884 	/* what if we are setup as SSLv2 but want to talk SSLv3 or
    885 	 * vice-versa */
    886 	if (t->method != f->method)
    887 		{
    888 		t->method->ssl_free(t);	/* cleanup current */
    889 		t->method=f->method;	/* change method */
    890 		t->method->ssl_new(t);	/* setup new */
    891 		}
    892 
    893 	tmp=t->cert;
    894 	if (f->cert != NULL)
    895 		{
    896 		CRYPTO_add(&f->cert->references,1,CRYPTO_LOCK_SSL_CERT);
    897 		t->cert=f->cert;
    898 		}
    899 	else
    900 		t->cert=NULL;
    901 	if (tmp != NULL) ssl_cert_free(tmp);
    902 	SSL_set_session_id_context(t,f->sid_ctx,f->sid_ctx_length);
    903 	}
    904 
    905 /* Fix this so it checks all the valid key/cert options */
    906 int SSL_CTX_check_private_key(const SSL_CTX *ctx)
    907 	{
    908 	if (	(ctx == NULL) ||
    909 		(ctx->cert == NULL) ||
    910 		(ctx->cert->key->x509 == NULL))
    911 		{
    912 		SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
    913 		return(0);
    914 		}
    915 	if 	(ctx->cert->key->privatekey == NULL)
    916 		{
    917 		SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED);
    918 		return(0);
    919 		}
    920 	return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey));
    921 	}
    922 
    923 /* Fix this function so that it takes an optional type parameter */
    924 int SSL_check_private_key(const SSL *ssl)
    925 	{
    926 	if (ssl == NULL)
    927 		{
    928 		SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,ERR_R_PASSED_NULL_PARAMETER);
    929 		return(0);
    930 		}
    931 	if (ssl->cert == NULL)
    932 		{
    933 		SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
    934 		return 0;
    935 		}
    936 	if (ssl->cert->key->x509 == NULL)
    937 		{
    938 		SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
    939 		return(0);
    940 		}
    941 	if (ssl->cert->key->privatekey == NULL)
    942 		{
    943 		SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED);
    944 		return(0);
    945 		}
    946 	return(X509_check_private_key(ssl->cert->key->x509,
    947 		ssl->cert->key->privatekey));
    948 	}
    949 
    950 int SSL_accept(SSL *s)
    951 	{
    952 	if (s->handshake_func == 0)
    953 		/* Not properly initialized yet */
    954 		SSL_set_accept_state(s);
    955 
    956 	return(s->method->ssl_accept(s));
    957 	}
    958 
    959 int SSL_connect(SSL *s)
    960 	{
    961 	if (s->handshake_func == 0)
    962 		/* Not properly initialized yet */
    963 		SSL_set_connect_state(s);
    964 
    965 	return(s->method->ssl_connect(s));
    966 	}
    967 
    968 long SSL_get_default_timeout(const SSL *s)
    969 	{
    970 	return(s->method->get_timeout());
    971 	}
    972 
    973 int SSL_read(SSL *s,void *buf,int num)
    974 	{
    975 	if (s->handshake_func == 0)
    976 		{
    977 		SSLerr(SSL_F_SSL_READ, SSL_R_UNINITIALIZED);
    978 		return -1;
    979 		}
    980 
    981 	if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
    982 		{
    983 		s->rwstate=SSL_NOTHING;
    984 		return(0);
    985 		}
    986 	return(s->method->ssl_read(s,buf,num));
    987 	}
    988 
    989 int SSL_peek(SSL *s,void *buf,int num)
    990 	{
    991 	if (s->handshake_func == 0)
    992 		{
    993 		SSLerr(SSL_F_SSL_PEEK, SSL_R_UNINITIALIZED);
    994 		return -1;
    995 		}
    996 
    997 	if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
    998 		{
    999 		return(0);
   1000 		}
   1001 	return(s->method->ssl_peek(s,buf,num));
   1002 	}
   1003 
   1004 int SSL_write(SSL *s,const void *buf,int num)
   1005 	{
   1006 	if (s->handshake_func == 0)
   1007 		{
   1008 		SSLerr(SSL_F_SSL_WRITE, SSL_R_UNINITIALIZED);
   1009 		return -1;
   1010 		}
   1011 
   1012 	if (s->shutdown & SSL_SENT_SHUTDOWN)
   1013 		{
   1014 		s->rwstate=SSL_NOTHING;
   1015 		SSLerr(SSL_F_SSL_WRITE,SSL_R_PROTOCOL_IS_SHUTDOWN);
   1016 		return(-1);
   1017 		}
   1018 	return(s->method->ssl_write(s,buf,num));
   1019 	}
   1020 
   1021 int SSL_shutdown(SSL *s)
   1022 	{
   1023 	/* Note that this function behaves differently from what one might
   1024 	 * expect.  Return values are 0 for no success (yet),
   1025 	 * 1 for success; but calling it once is usually not enough,
   1026 	 * even if blocking I/O is used (see ssl3_shutdown).
   1027 	 */
   1028 
   1029 	if (s->handshake_func == 0)
   1030 		{
   1031 		SSLerr(SSL_F_SSL_SHUTDOWN, SSL_R_UNINITIALIZED);
   1032 		return -1;
   1033 		}
   1034 
   1035 	if ((s != NULL) && !SSL_in_init(s))
   1036 		return(s->method->ssl_shutdown(s));
   1037 	else
   1038 		return(1);
   1039 	}
   1040 
   1041 int SSL_renegotiate(SSL *s)
   1042 	{
   1043 	if (s->renegotiate == 0)
   1044 		s->renegotiate=1;
   1045 
   1046 	s->new_session=1;
   1047 
   1048 	return(s->method->ssl_renegotiate(s));
   1049 	}
   1050 
   1051 int SSL_renegotiate_abbreviated(SSL *s)
   1052 	{
   1053 	if (s->renegotiate == 0)
   1054 		s->renegotiate=1;
   1055 
   1056 	s->new_session=0;
   1057 
   1058 	return(s->method->ssl_renegotiate(s));
   1059 	}
   1060 
   1061 int SSL_renegotiate_pending(SSL *s)
   1062 	{
   1063 	/* becomes true when negotiation is requested;
   1064 	 * false again once a handshake has finished */
   1065 	return (s->renegotiate != 0);
   1066 	}
   1067 
   1068 long SSL_ctrl(SSL *s,int cmd,long larg,void *parg)
   1069 	{
   1070 	long l;
   1071 
   1072 	switch (cmd)
   1073 		{
   1074 	case SSL_CTRL_GET_READ_AHEAD:
   1075 		return(s->read_ahead);
   1076 	case SSL_CTRL_SET_READ_AHEAD:
   1077 		l=s->read_ahead;
   1078 		s->read_ahead=larg;
   1079 		return(l);
   1080 
   1081 	case SSL_CTRL_SET_MSG_CALLBACK_ARG:
   1082 		s->msg_callback_arg = parg;
   1083 		return 1;
   1084 
   1085 	case SSL_CTRL_OPTIONS:
   1086 		return(s->options|=larg);
   1087 	case SSL_CTRL_CLEAR_OPTIONS:
   1088 		return(s->options&=~larg);
   1089 	case SSL_CTRL_MODE:
   1090 		return(s->mode|=larg);
   1091 	case SSL_CTRL_CLEAR_MODE:
   1092 		return(s->mode &=~larg);
   1093 	case SSL_CTRL_GET_MAX_CERT_LIST:
   1094 		return(s->max_cert_list);
   1095 	case SSL_CTRL_SET_MAX_CERT_LIST:
   1096 		l=s->max_cert_list;
   1097 		s->max_cert_list=larg;
   1098 		return(l);
   1099 	case SSL_CTRL_SET_MTU:
   1100 #ifndef OPENSSL_NO_DTLS1
   1101 		if (larg < (long)dtls1_min_mtu())
   1102 			return 0;
   1103 #endif
   1104 
   1105 		if (SSL_version(s) == DTLS1_VERSION ||
   1106 		    SSL_version(s) == DTLS1_BAD_VER)
   1107 			{
   1108 			s->d1->mtu = larg;
   1109 			return larg;
   1110 			}
   1111 		return 0;
   1112 	case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
   1113 		if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
   1114 			return 0;
   1115 		s->max_send_fragment = larg;
   1116 		return 1;
   1117 	case SSL_CTRL_GET_RI_SUPPORT:
   1118 		if (s->s3)
   1119 			return s->s3->send_connection_binding;
   1120 		else return 0;
   1121 	default:
   1122 		return(s->method->ssl_ctrl(s,cmd,larg,parg));
   1123 		}
   1124 	}
   1125 
   1126 long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
   1127 	{
   1128 	switch(cmd)
   1129 		{
   1130 	case SSL_CTRL_SET_MSG_CALLBACK:
   1131 		s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
   1132 		return 1;
   1133 
   1134 	default:
   1135 		return(s->method->ssl_callback_ctrl(s,cmd,fp));
   1136 		}
   1137 	}
   1138 
   1139 LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
   1140 	{
   1141 	return ctx->sessions;
   1142 	}
   1143 
   1144 long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg)
   1145 	{
   1146 	long l;
   1147 
   1148 	switch (cmd)
   1149 		{
   1150 	case SSL_CTRL_GET_READ_AHEAD:
   1151 		return(ctx->read_ahead);
   1152 	case SSL_CTRL_SET_READ_AHEAD:
   1153 		l=ctx->read_ahead;
   1154 		ctx->read_ahead=larg;
   1155 		return(l);
   1156 
   1157 	case SSL_CTRL_SET_MSG_CALLBACK_ARG:
   1158 		ctx->msg_callback_arg = parg;
   1159 		return 1;
   1160 
   1161 	case SSL_CTRL_GET_MAX_CERT_LIST:
   1162 		return(ctx->max_cert_list);
   1163 	case SSL_CTRL_SET_MAX_CERT_LIST:
   1164 		l=ctx->max_cert_list;
   1165 		ctx->max_cert_list=larg;
   1166 		return(l);
   1167 
   1168 	case SSL_CTRL_SET_SESS_CACHE_SIZE:
   1169 		l=ctx->session_cache_size;
   1170 		ctx->session_cache_size=larg;
   1171 		return(l);
   1172 	case SSL_CTRL_GET_SESS_CACHE_SIZE:
   1173 		return(ctx->session_cache_size);
   1174 	case SSL_CTRL_SET_SESS_CACHE_MODE:
   1175 		l=ctx->session_cache_mode;
   1176 		ctx->session_cache_mode=larg;
   1177 		return(l);
   1178 	case SSL_CTRL_GET_SESS_CACHE_MODE:
   1179 		return(ctx->session_cache_mode);
   1180 
   1181 	case SSL_CTRL_SESS_NUMBER:
   1182 		return(lh_SSL_SESSION_num_items(ctx->sessions));
   1183 	case SSL_CTRL_SESS_CONNECT:
   1184 		return(ctx->stats.sess_connect);
   1185 	case SSL_CTRL_SESS_CONNECT_GOOD:
   1186 		return(ctx->stats.sess_connect_good);
   1187 	case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
   1188 		return(ctx->stats.sess_connect_renegotiate);
   1189 	case SSL_CTRL_SESS_ACCEPT:
   1190 		return(ctx->stats.sess_accept);
   1191 	case SSL_CTRL_SESS_ACCEPT_GOOD:
   1192 		return(ctx->stats.sess_accept_good);
   1193 	case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
   1194 		return(ctx->stats.sess_accept_renegotiate);
   1195 	case SSL_CTRL_SESS_HIT:
   1196 		return(ctx->stats.sess_hit);
   1197 	case SSL_CTRL_SESS_CB_HIT:
   1198 		return(ctx->stats.sess_cb_hit);
   1199 	case SSL_CTRL_SESS_MISSES:
   1200 		return(ctx->stats.sess_miss);
   1201 	case SSL_CTRL_SESS_TIMEOUTS:
   1202 		return(ctx->stats.sess_timeout);
   1203 	case SSL_CTRL_SESS_CACHE_FULL:
   1204 		return(ctx->stats.sess_cache_full);
   1205 	case SSL_CTRL_OPTIONS:
   1206 		return(ctx->options|=larg);
   1207 	case SSL_CTRL_CLEAR_OPTIONS:
   1208 		return(ctx->options&=~larg);
   1209 	case SSL_CTRL_MODE:
   1210 		return(ctx->mode|=larg);
   1211 	case SSL_CTRL_CLEAR_MODE:
   1212 		return(ctx->mode&=~larg);
   1213 	case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
   1214 		if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
   1215 			return 0;
   1216 		ctx->max_send_fragment = larg;
   1217 		return 1;
   1218 	default:
   1219 		return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
   1220 		}
   1221 	}
   1222 
   1223 long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
   1224 	{
   1225 	switch(cmd)
   1226 		{
   1227 	case SSL_CTRL_SET_MSG_CALLBACK:
   1228 		ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
   1229 		return 1;
   1230 
   1231 	default:
   1232 		return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp));
   1233 		}
   1234 	}
   1235 
   1236 int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b)
   1237 	{
   1238 	long l;
   1239 
   1240 	l=a->id-b->id;
   1241 	if (l == 0L)
   1242 		return(0);
   1243 	else
   1244 		return((l > 0)?1:-1);
   1245 	}
   1246 
   1247 int ssl_cipher_ptr_id_cmp(const SSL_CIPHER * const *ap,
   1248 			const SSL_CIPHER * const *bp)
   1249 	{
   1250 	long l;
   1251 
   1252 	l=(*ap)->id-(*bp)->id;
   1253 	if (l == 0L)
   1254 		return(0);
   1255 	else
   1256 		return((l > 0)?1:-1);
   1257 	}
   1258 
   1259 /** return a STACK of the ciphers available for the SSL and in order of
   1260  * preference */
   1261 STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
   1262 	{
   1263 	if (s != NULL)
   1264 		{
   1265 		if (s->cipher_list != NULL)
   1266 			{
   1267 			return(s->cipher_list);
   1268 			}
   1269 		else if ((s->ctx != NULL) &&
   1270 			(s->ctx->cipher_list != NULL))
   1271 			{
   1272 			return(s->ctx->cipher_list);
   1273 			}
   1274 		}
   1275 	return(NULL);
   1276 	}
   1277 
   1278 /** return a STACK of the ciphers available for the SSL and in order of
   1279  * algorithm id */
   1280 STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
   1281 	{
   1282 	if (s != NULL)
   1283 		{
   1284 		if (s->cipher_list_by_id != NULL)
   1285 			{
   1286 			return(s->cipher_list_by_id);
   1287 			}
   1288 		else if ((s->ctx != NULL) &&
   1289 			(s->ctx->cipher_list_by_id != NULL))
   1290 			{
   1291 			return(s->ctx->cipher_list_by_id);
   1292 			}
   1293 		}
   1294 	return(NULL);
   1295 	}
   1296 
   1297 /** The old interface to get the same thing as SSL_get_ciphers() */
   1298 const char *SSL_get_cipher_list(const SSL *s,int n)
   1299 	{
   1300 	SSL_CIPHER *c;
   1301 	STACK_OF(SSL_CIPHER) *sk;
   1302 
   1303 	if (s == NULL) return(NULL);
   1304 	sk=SSL_get_ciphers(s);
   1305 	if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n))
   1306 		return(NULL);
   1307 	c=sk_SSL_CIPHER_value(sk,n);
   1308 	if (c == NULL) return(NULL);
   1309 	return(c->name);
   1310 	}
   1311 
   1312 /** specify the ciphers to be used by default by the SSL_CTX */
   1313 int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
   1314 	{
   1315 	STACK_OF(SSL_CIPHER) *sk;
   1316 
   1317 	sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
   1318 		&ctx->cipher_list_by_id,str);
   1319 	/* ssl_create_cipher_list may return an empty stack if it
   1320 	 * was unable to find a cipher matching the given rule string
   1321 	 * (for example if the rule string specifies a cipher which
   1322 	 * has been disabled). This is not an error as far as
   1323 	 * ssl_create_cipher_list is concerned, and hence
   1324 	 * ctx->cipher_list and ctx->cipher_list_by_id has been
   1325 	 * updated. */
   1326 	if (sk == NULL)
   1327 		return 0;
   1328 	else if (sk_SSL_CIPHER_num(sk) == 0)
   1329 		{
   1330 		SSLerr(SSL_F_SSL_CTX_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH);
   1331 		return 0;
   1332 		}
   1333 	return 1;
   1334 	}
   1335 
   1336 /** specify the ciphers to be used by the SSL */
   1337 int SSL_set_cipher_list(SSL *s,const char *str)
   1338 	{
   1339 	STACK_OF(SSL_CIPHER) *sk;
   1340 
   1341 	sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
   1342 		&s->cipher_list_by_id,str);
   1343 	/* see comment in SSL_CTX_set_cipher_list */
   1344 	if (sk == NULL)
   1345 		return 0;
   1346 	else if (sk_SSL_CIPHER_num(sk) == 0)
   1347 		{
   1348 		SSLerr(SSL_F_SSL_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH);
   1349 		return 0;
   1350 		}
   1351 	return 1;
   1352 	}
   1353 
   1354 /** specify the ciphers to be used by the SSL */
   1355 int SSL_set_cipher_lists(SSL *s,STACK_OF(SSL_CIPHER) *sk)
   1356 	{
   1357 	STACK_OF(SSL_CIPHER) *tmp_cipher_list;
   1358 
   1359 	if (sk == NULL)
   1360 		return 0;
   1361 
   1362         /* Based on end of ssl_create_cipher_list */
   1363 	tmp_cipher_list = sk_SSL_CIPHER_dup(sk);
   1364 	if (tmp_cipher_list == NULL)
   1365 		{
   1366 		return 0;
   1367 		}
   1368 	if (s->cipher_list != NULL)
   1369 		sk_SSL_CIPHER_free(s->cipher_list);
   1370 	s->cipher_list = sk;
   1371 	if (s->cipher_list_by_id != NULL)
   1372 		sk_SSL_CIPHER_free(s->cipher_list_by_id);
   1373 	s->cipher_list_by_id = tmp_cipher_list;
   1374 	(void)sk_SSL_CIPHER_set_cmp_func(s->cipher_list_by_id,ssl_cipher_ptr_id_cmp);
   1375 
   1376 	sk_SSL_CIPHER_sort(s->cipher_list_by_id);
   1377 	return 1;
   1378 	}
   1379 
   1380 /* works well for SSLv2, not so good for SSLv3 */
   1381 char *SSL_get_shared_ciphers(const SSL *s,char *buf,int len)
   1382 	{
   1383 	char *p;
   1384 	STACK_OF(SSL_CIPHER) *sk;
   1385 	SSL_CIPHER *c;
   1386 	int i;
   1387 
   1388 	if ((s->session == NULL) || (s->session->ciphers == NULL) ||
   1389 		(len < 2))
   1390 		return(NULL);
   1391 
   1392 	p=buf;
   1393 	sk=s->session->ciphers;
   1394 	for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
   1395 		{
   1396 		int n;
   1397 
   1398 		c=sk_SSL_CIPHER_value(sk,i);
   1399 		n=strlen(c->name);
   1400 		if (n+1 > len)
   1401 			{
   1402 			if (p != buf)
   1403 				--p;
   1404 			*p='\0';
   1405 			return buf;
   1406 			}
   1407 		strcpy(p,c->name);
   1408 		p+=n;
   1409 		*(p++)=':';
   1410 		len-=n+1;
   1411 		}
   1412 	p[-1]='\0';
   1413 	return(buf);
   1414 	}
   1415 
   1416 int ssl_cipher_list_to_bytes(SSL *s,STACK_OF(SSL_CIPHER) *sk,unsigned char *p,
   1417 			     int (*put_cb)(const SSL_CIPHER *, unsigned char *))
   1418 	{
   1419 	int i,j=0;
   1420 	SSL_CIPHER *c;
   1421 	unsigned char *q;
   1422 #ifndef OPENSSL_NO_KRB5
   1423 	int nokrb5 = !kssl_tgt_is_available(s->kssl_ctx);
   1424 #endif /* OPENSSL_NO_KRB5 */
   1425 
   1426 	if (sk == NULL) return(0);
   1427 	q=p;
   1428 
   1429 	for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
   1430 		{
   1431 		c=sk_SSL_CIPHER_value(sk,i);
   1432 		/* Skip TLS v1.2 only ciphersuites if lower than v1.2 */
   1433 		if ((c->algorithm_ssl & SSL_TLSV1_2) &&
   1434 			(TLS1_get_client_version(s) < TLS1_2_VERSION))
   1435 			continue;
   1436 #ifndef OPENSSL_NO_KRB5
   1437 		if (((c->algorithm_mkey & SSL_kKRB5) || (c->algorithm_auth & SSL_aKRB5)) &&
   1438 		    nokrb5)
   1439 		    continue;
   1440 #endif /* OPENSSL_NO_KRB5 */
   1441 #ifndef OPENSSL_NO_PSK
   1442 		/* with PSK there must be client callback set */
   1443 		if (((c->algorithm_mkey & SSL_kPSK) || (c->algorithm_auth & SSL_aPSK)) &&
   1444 		    s->psk_client_callback == NULL)
   1445 			continue;
   1446 #endif /* OPENSSL_NO_PSK */
   1447 		j = put_cb ? put_cb(c,p) : ssl_put_cipher_by_char(s,c,p);
   1448 		p+=j;
   1449 		}
   1450 	/* If p == q, no ciphers and caller indicates an error. Otherwise
   1451 	 * add SCSV if not renegotiating.
   1452 	 */
   1453 	if (p != q && !s->renegotiate)
   1454 		{
   1455 		static SSL_CIPHER scsv =
   1456 			{
   1457 			0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
   1458 			};
   1459 		j = put_cb ? put_cb(&scsv,p) : ssl_put_cipher_by_char(s,&scsv,p);
   1460 		p+=j;
   1461 #ifdef OPENSSL_RI_DEBUG
   1462 		fprintf(stderr, "SCSV sent by client\n");
   1463 #endif
   1464 		}
   1465 
   1466 	return(p-q);
   1467 	}
   1468 
   1469 STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,unsigned char *p,int num,
   1470 					       STACK_OF(SSL_CIPHER) **skp)
   1471 	{
   1472 	const SSL_CIPHER *c;
   1473 	STACK_OF(SSL_CIPHER) *sk;
   1474 	int i,n;
   1475 	if (s->s3)
   1476 		s->s3->send_connection_binding = 0;
   1477 
   1478 	n=ssl_put_cipher_by_char(s,NULL,NULL);
   1479 	if ((num%n) != 0)
   1480 		{
   1481 		SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
   1482 		return(NULL);
   1483 		}
   1484 	if ((skp == NULL) || (*skp == NULL))
   1485 		sk=sk_SSL_CIPHER_new_null(); /* change perhaps later */
   1486 	else
   1487 		{
   1488 		sk= *skp;
   1489 		sk_SSL_CIPHER_zero(sk);
   1490 		}
   1491 
   1492 	for (i=0; i<num; i+=n)
   1493 		{
   1494 		/* Check for SCSV */
   1495 		if (s->s3 && (n != 3 || !p[0]) &&
   1496 			(p[n-2] == ((SSL3_CK_SCSV >> 8) & 0xff)) &&
   1497 			(p[n-1] == (SSL3_CK_SCSV & 0xff)))
   1498 			{
   1499 			/* SCSV fatal if renegotiating */
   1500 			if (s->renegotiate)
   1501 				{
   1502 				SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
   1503 				ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
   1504 				goto err;
   1505 				}
   1506 			s->s3->send_connection_binding = 1;
   1507 			p += n;
   1508 #ifdef OPENSSL_RI_DEBUG
   1509 			fprintf(stderr, "SCSV received by server\n");
   1510 #endif
   1511 			continue;
   1512 			}
   1513 
   1514 		c=ssl_get_cipher_by_char(s,p);
   1515 		p+=n;
   1516 		if (c != NULL)
   1517 			{
   1518 			if (!sk_SSL_CIPHER_push(sk,c))
   1519 				{
   1520 				SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
   1521 				goto err;
   1522 				}
   1523 			}
   1524 		}
   1525 
   1526 	if (skp != NULL)
   1527 		*skp=sk;
   1528 	return(sk);
   1529 err:
   1530 	if ((skp == NULL) || (*skp == NULL))
   1531 		sk_SSL_CIPHER_free(sk);
   1532 	return(NULL);
   1533 	}
   1534 
   1535 
   1536 #ifndef OPENSSL_NO_TLSEXT
   1537 /** return a servername extension value if provided in Client Hello, or NULL.
   1538  * So far, only host_name types are defined (RFC 3546).
   1539  */
   1540 
   1541 const char *SSL_get_servername(const SSL *s, const int type)
   1542 	{
   1543 	if (type != TLSEXT_NAMETYPE_host_name)
   1544 		return NULL;
   1545 
   1546 	return s->session && !s->tlsext_hostname ?
   1547 		s->session->tlsext_hostname :
   1548 		s->tlsext_hostname;
   1549 	}
   1550 
   1551 int SSL_get_servername_type(const SSL *s)
   1552 	{
   1553 	if (s->session && (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname))
   1554 		return TLSEXT_NAMETYPE_host_name;
   1555 	return -1;
   1556 	}
   1557 
   1558 # ifndef OPENSSL_NO_NEXTPROTONEG
   1559 /* SSL_select_next_proto implements the standard protocol selection. It is
   1560  * expected that this function is called from the callback set by
   1561  * SSL_CTX_set_next_proto_select_cb.
   1562  *
   1563  * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
   1564  * strings. The length byte itself is not included in the length. A byte
   1565  * string of length 0 is invalid. No byte string may be truncated.
   1566  *
   1567  * The current, but experimental algorithm for selecting the protocol is:
   1568  *
   1569  * 1) If the server doesn't support NPN then this is indicated to the
   1570  * callback. In this case, the client application has to abort the connection
   1571  * or have a default application level protocol.
   1572  *
   1573  * 2) If the server supports NPN, but advertises an empty list then the
   1574  * client selects the first protcol in its list, but indicates via the
   1575  * API that this fallback case was enacted.
   1576  *
   1577  * 3) Otherwise, the client finds the first protocol in the server's list
   1578  * that it supports and selects this protocol. This is because it's
   1579  * assumed that the server has better information about which protocol
   1580  * a client should use.
   1581  *
   1582  * 4) If the client doesn't support any of the server's advertised
   1583  * protocols, then this is treated the same as case 2.
   1584  *
   1585  * It returns either
   1586  * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
   1587  * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
   1588  */
   1589 int SSL_select_next_proto(unsigned char **out, unsigned char *outlen, const unsigned char *server, unsigned int server_len, const unsigned char *client, unsigned int client_len)
   1590 	{
   1591 	unsigned int i, j;
   1592 	const unsigned char *result;
   1593 	int status = OPENSSL_NPN_UNSUPPORTED;
   1594 
   1595 	/* For each protocol in server preference order, see if we support it. */
   1596 	for (i = 0; i < server_len; )
   1597 		{
   1598 		for (j = 0; j < client_len; )
   1599 			{
   1600 			if (server[i] == client[j] &&
   1601 			    memcmp(&server[i+1], &client[j+1], server[i]) == 0)
   1602 				{
   1603 				/* We found a match */
   1604 				result = &server[i];
   1605 				status = OPENSSL_NPN_NEGOTIATED;
   1606 				goto found;
   1607 				}
   1608 			j += client[j];
   1609 			j++;
   1610 			}
   1611 		i += server[i];
   1612 		i++;
   1613 		}
   1614 
   1615 	/* There's no overlap between our protocols and the server's list. */
   1616 	result = client;
   1617 	status = OPENSSL_NPN_NO_OVERLAP;
   1618 
   1619 	found:
   1620 	*out = (unsigned char *) result + 1;
   1621 	*outlen = result[0];
   1622 	return status;
   1623 	}
   1624 
   1625 /* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
   1626  * requested protocol for this connection and returns 0. If the client didn't
   1627  * request any protocol, then *data is set to NULL.
   1628  *
   1629  * Note that the client can request any protocol it chooses. The value returned
   1630  * from this function need not be a member of the list of supported protocols
   1631  * provided by the callback.
   1632  */
   1633 void SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data, unsigned *len)
   1634 	{
   1635 	*data = s->next_proto_negotiated;
   1636 	if (!*data) {
   1637 		*len = 0;
   1638 	} else {
   1639 		*len = s->next_proto_negotiated_len;
   1640 	}
   1641 }
   1642 
   1643 /* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
   1644  * TLS server needs a list of supported protocols for Next Protocol
   1645  * Negotiation. The returned list must be in wire format.  The list is returned
   1646  * by setting |out| to point to it and |outlen| to its length. This memory will
   1647  * not be modified, but one should assume that the SSL* keeps a reference to
   1648  * it.
   1649  *
   1650  * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise. Otherwise, no
   1651  * such extension will be included in the ServerHello. */
   1652 void SSL_CTX_set_next_protos_advertised_cb(SSL_CTX *ctx, int (*cb) (SSL *ssl, const unsigned char **out, unsigned int *outlen, void *arg), void *arg)
   1653 	{
   1654 	ctx->next_protos_advertised_cb = cb;
   1655 	ctx->next_protos_advertised_cb_arg = arg;
   1656 	}
   1657 
   1658 /* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
   1659  * client needs to select a protocol from the server's provided list. |out|
   1660  * must be set to point to the selected protocol (which may be within |in|).
   1661  * The length of the protocol name must be written into |outlen|. The server's
   1662  * advertised protocols are provided in |in| and |inlen|. The callback can
   1663  * assume that |in| is syntactically valid.
   1664  *
   1665  * The client must select a protocol. It is fatal to the connection if this
   1666  * callback returns a value other than SSL_TLSEXT_ERR_OK.
   1667  */
   1668 void SSL_CTX_set_next_proto_select_cb(SSL_CTX *ctx, int (*cb) (SSL *s, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg), void *arg)
   1669 	{
   1670 	ctx->next_proto_select_cb = cb;
   1671 	ctx->next_proto_select_cb_arg = arg;
   1672 	}
   1673 # endif
   1674 
   1675 /* SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
   1676  * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
   1677  * length-prefixed strings).
   1678  *
   1679  * Returns 0 on success. */
   1680 int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char* protos,
   1681 			    unsigned protos_len)
   1682 	{
   1683 	if (ctx->alpn_client_proto_list)
   1684 		OPENSSL_free(ctx->alpn_client_proto_list);
   1685 
   1686 	ctx->alpn_client_proto_list = OPENSSL_malloc(protos_len);
   1687 	if (!ctx->alpn_client_proto_list)
   1688 		return 1;
   1689 	memcpy(ctx->alpn_client_proto_list, protos, protos_len);
   1690 	ctx->alpn_client_proto_list_len = protos_len;
   1691 
   1692 	return 0;
   1693 	}
   1694 
   1695 /* SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
   1696  * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
   1697  * length-prefixed strings).
   1698  *
   1699  * Returns 0 on success. */
   1700 int SSL_set_alpn_protos(SSL *ssl, const unsigned char* protos,
   1701 			unsigned protos_len)
   1702 	{
   1703 	if (ssl->alpn_client_proto_list)
   1704 		OPENSSL_free(ssl->alpn_client_proto_list);
   1705 
   1706 	ssl->alpn_client_proto_list = OPENSSL_malloc(protos_len);
   1707 	if (!ssl->alpn_client_proto_list)
   1708 		return 1;
   1709 	memcpy(ssl->alpn_client_proto_list, protos, protos_len);
   1710 	ssl->alpn_client_proto_list_len = protos_len;
   1711 
   1712 	return 0;
   1713 	}
   1714 
   1715 /* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
   1716  * during ClientHello processing in order to select an ALPN protocol from the
   1717  * client's list of offered protocols. */
   1718 void SSL_CTX_set_alpn_select_cb(SSL_CTX* ctx,
   1719 				int (*cb) (SSL *ssl,
   1720 					   const unsigned char **out,
   1721 					   unsigned char *outlen,
   1722 					   const unsigned char *in,
   1723 					   unsigned int inlen,
   1724 					   void *arg),
   1725 				void *arg)
   1726 	{
   1727 	ctx->alpn_select_cb = cb;
   1728 	ctx->alpn_select_cb_arg = arg;
   1729 	}
   1730 
   1731 /* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
   1732  * On return it sets |*data| to point to |*len| bytes of protocol name (not
   1733  * including the leading length-prefix byte). If the server didn't respond with
   1734  * a negotiated protocol then |*len| will be zero. */
   1735 void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
   1736 			    unsigned *len)
   1737 	{
   1738 	*data = NULL;
   1739 	if (ssl->s3)
   1740 		*data = ssl->s3->alpn_selected;
   1741 	if (*data == NULL)
   1742 		*len = 0;
   1743 	else
   1744 		*len = ssl->s3->alpn_selected_len;
   1745 	}
   1746 #endif
   1747 
   1748 int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
   1749 	const char *label, size_t llen, const unsigned char *p, size_t plen,
   1750 	int use_context)
   1751 	{
   1752 	if (s->version < TLS1_VERSION)
   1753 		return -1;
   1754 
   1755 	return s->method->ssl3_enc->export_keying_material(s, out, olen, label,
   1756 							   llen, p, plen,
   1757 							   use_context);
   1758 	}
   1759 
   1760 static unsigned long ssl_session_hash(const SSL_SESSION *a)
   1761 	{
   1762 	unsigned long l;
   1763 
   1764 	l=(unsigned long)
   1765 		((unsigned int) a->session_id[0]     )|
   1766 		((unsigned int) a->session_id[1]<< 8L)|
   1767 		((unsigned long)a->session_id[2]<<16L)|
   1768 		((unsigned long)a->session_id[3]<<24L);
   1769 	return(l);
   1770 	}
   1771 
   1772 /* NB: If this function (or indeed the hash function which uses a sort of
   1773  * coarser function than this one) is changed, ensure
   1774  * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
   1775  * able to construct an SSL_SESSION that will collide with any existing session
   1776  * with a matching session ID. */
   1777 static int ssl_session_cmp(const SSL_SESSION *a,const SSL_SESSION *b)
   1778 	{
   1779 	if (a->ssl_version != b->ssl_version)
   1780 		return(1);
   1781 	if (a->session_id_length != b->session_id_length)
   1782 		return(1);
   1783 	return(memcmp(a->session_id,b->session_id,a->session_id_length));
   1784 	}
   1785 
   1786 /* These wrapper functions should remain rather than redeclaring
   1787  * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
   1788  * variable. The reason is that the functions aren't static, they're exposed via
   1789  * ssl.h. */
   1790 static IMPLEMENT_LHASH_HASH_FN(ssl_session, SSL_SESSION)
   1791 static IMPLEMENT_LHASH_COMP_FN(ssl_session, SSL_SESSION)
   1792 
   1793 SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
   1794 	{
   1795 	SSL_CTX *ret=NULL;
   1796 
   1797 	if (meth == NULL)
   1798 		{
   1799 		SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_NULL_SSL_METHOD_PASSED);
   1800 		return(NULL);
   1801 		}
   1802 
   1803 #ifdef OPENSSL_FIPS
   1804 	if (FIPS_mode() && (meth->version < TLS1_VERSION))
   1805 		{
   1806 		SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
   1807 		return NULL;
   1808 		}
   1809 #endif
   1810 
   1811 	if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
   1812 		{
   1813 		SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
   1814 		goto err;
   1815 		}
   1816 	ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
   1817 	if (ret == NULL)
   1818 		goto err;
   1819 
   1820 	memset(ret,0,sizeof(SSL_CTX));
   1821 
   1822 	ret->method=meth;
   1823 
   1824 	ret->cert_store=NULL;
   1825 	ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
   1826 	ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
   1827 	ret->session_cache_head=NULL;
   1828 	ret->session_cache_tail=NULL;
   1829 
   1830 	/* We take the system default */
   1831 	ret->session_timeout=meth->get_timeout();
   1832 
   1833 	ret->new_session_cb=0;
   1834 	ret->remove_session_cb=0;
   1835 	ret->get_session_cb=0;
   1836 	ret->generate_session_id=0;
   1837 
   1838 	memset((char *)&ret->stats,0,sizeof(ret->stats));
   1839 
   1840 	ret->references=1;
   1841 	ret->quiet_shutdown=0;
   1842 
   1843 /*	ret->cipher=NULL;*/
   1844 /*	ret->s2->challenge=NULL;
   1845 	ret->master_key=NULL;
   1846 	ret->key_arg=NULL;
   1847 	ret->s2->conn_id=NULL; */
   1848 
   1849 	ret->info_callback=NULL;
   1850 
   1851 	ret->app_verify_callback=0;
   1852 	ret->app_verify_arg=NULL;
   1853 
   1854 	ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT;
   1855 	ret->read_ahead=0;
   1856 	ret->msg_callback=0;
   1857 	ret->msg_callback_arg=NULL;
   1858 	ret->verify_mode=SSL_VERIFY_NONE;
   1859 #if 0
   1860 	ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */
   1861 #endif
   1862 	ret->sid_ctx_length=0;
   1863 	ret->default_verify_callback=NULL;
   1864 	if ((ret->cert=ssl_cert_new()) == NULL)
   1865 		goto err;
   1866 
   1867 	ret->default_passwd_callback=0;
   1868 	ret->default_passwd_callback_userdata=NULL;
   1869 	ret->client_cert_cb=0;
   1870 	ret->app_gen_cookie_cb=0;
   1871 	ret->app_verify_cookie_cb=0;
   1872 
   1873 	ret->sessions=lh_SSL_SESSION_new();
   1874 	if (ret->sessions == NULL) goto err;
   1875 	ret->cert_store=X509_STORE_new();
   1876 	if (ret->cert_store == NULL) goto err;
   1877 
   1878 	ssl_create_cipher_list(ret->method,
   1879 		&ret->cipher_list,&ret->cipher_list_by_id,
   1880 		meth->version == SSL2_VERSION ? "SSLv2" : SSL_DEFAULT_CIPHER_LIST);
   1881 	if (ret->cipher_list == NULL
   1882 	    || sk_SSL_CIPHER_num(ret->cipher_list) <= 0)
   1883 		{
   1884 		SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_LIBRARY_HAS_NO_CIPHERS);
   1885 		goto err2;
   1886 		}
   1887 
   1888 	ret->param = X509_VERIFY_PARAM_new();
   1889 	if (!ret->param)
   1890 		goto err;
   1891 
   1892 	if ((ret->rsa_md5=EVP_get_digestbyname("ssl2-md5")) == NULL)
   1893 		{
   1894 		SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL2_MD5_ROUTINES);
   1895 		goto err2;
   1896 		}
   1897 	if ((ret->md5=EVP_get_digestbyname("ssl3-md5")) == NULL)
   1898 		{
   1899 		SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES);
   1900 		goto err2;
   1901 		}
   1902 	if ((ret->sha1=EVP_get_digestbyname("ssl3-sha1")) == NULL)
   1903 		{
   1904 		SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES);
   1905 		goto err2;
   1906 		}
   1907 
   1908 	if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL)
   1909 		goto err;
   1910 
   1911 	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
   1912 
   1913 	ret->extra_certs=NULL;
   1914 	ret->comp_methods=SSL_COMP_get_compression_methods();
   1915 
   1916 	ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
   1917 
   1918 #ifndef OPENSSL_NO_TLSEXT
   1919 	ret->tlsext_servername_callback = 0;
   1920 	ret->tlsext_servername_arg = NULL;
   1921 	/* Setup RFC4507 ticket keys */
   1922 	if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0)
   1923 		|| (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)
   1924 		|| (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))
   1925 		ret->options |= SSL_OP_NO_TICKET;
   1926 
   1927 	ret->tlsext_status_cb = 0;
   1928 	ret->tlsext_status_arg = NULL;
   1929 
   1930 # ifndef OPENSSL_NO_NEXTPROTONEG
   1931 	ret->next_protos_advertised_cb = 0;
   1932 	ret->next_proto_select_cb = 0;
   1933 # endif
   1934 #endif
   1935 #ifndef OPENSSL_NO_PSK
   1936 	ret->psk_identity_hint=NULL;
   1937 	ret->psk_client_callback=NULL;
   1938 	ret->psk_server_callback=NULL;
   1939 #endif
   1940 #ifndef OPENSSL_NO_SRP
   1941 	SSL_CTX_SRP_CTX_init(ret);
   1942 #endif
   1943 #ifndef OPENSSL_NO_BUF_FREELISTS
   1944 	ret->freelist_max_len = SSL_MAX_BUF_FREELIST_LEN_DEFAULT;
   1945 	ret->rbuf_freelist = OPENSSL_malloc(sizeof(SSL3_BUF_FREELIST));
   1946 	if (!ret->rbuf_freelist)
   1947 		goto err;
   1948 	ret->rbuf_freelist->chunklen = 0;
   1949 	ret->rbuf_freelist->len = 0;
   1950 	ret->rbuf_freelist->head = NULL;
   1951 	ret->wbuf_freelist = OPENSSL_malloc(sizeof(SSL3_BUF_FREELIST));
   1952 	if (!ret->wbuf_freelist)
   1953 		{
   1954 		OPENSSL_free(ret->rbuf_freelist);
   1955 		goto err;
   1956 		}
   1957 	ret->wbuf_freelist->chunklen = 0;
   1958 	ret->wbuf_freelist->len = 0;
   1959 	ret->wbuf_freelist->head = NULL;
   1960 #endif
   1961 #ifndef OPENSSL_NO_ENGINE
   1962 	ret->client_cert_engine = NULL;
   1963 #ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO
   1964 #define eng_strx(x)	#x
   1965 #define eng_str(x)	eng_strx(x)
   1966 	/* Use specific client engine automatically... ignore errors */
   1967 	{
   1968 	ENGINE *eng;
   1969 	eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
   1970 	if (!eng)
   1971 		{
   1972 		ERR_clear_error();
   1973 		ENGINE_load_builtin_engines();
   1974 		eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
   1975 		}
   1976 	if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng))
   1977 		ERR_clear_error();
   1978 	}
   1979 #endif
   1980 #endif
   1981 	/* Default is to connect to non-RI servers. When RI is more widely
   1982 	 * deployed might change this.
   1983 	 */
   1984 	ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
   1985 
   1986 	return(ret);
   1987 err:
   1988 	SSLerr(SSL_F_SSL_CTX_NEW,ERR_R_MALLOC_FAILURE);
   1989 err2:
   1990 	if (ret != NULL) SSL_CTX_free(ret);
   1991 	return(NULL);
   1992 	}
   1993 
   1994 #if 0
   1995 static void SSL_COMP_free(SSL_COMP *comp)
   1996     { OPENSSL_free(comp); }
   1997 #endif
   1998 
   1999 #ifndef OPENSSL_NO_BUF_FREELISTS
   2000 static void
   2001 ssl_buf_freelist_free(SSL3_BUF_FREELIST *list)
   2002 	{
   2003 	SSL3_BUF_FREELIST_ENTRY *ent, *next;
   2004 	for (ent = list->head; ent; ent = next)
   2005 		{
   2006 		next = ent->next;
   2007 		OPENSSL_free(ent);
   2008 		}
   2009 	OPENSSL_free(list);
   2010 	}
   2011 #endif
   2012 
   2013 void SSL_CTX_free(SSL_CTX *a)
   2014 	{
   2015 	int i;
   2016 
   2017 	if (a == NULL) return;
   2018 
   2019 	i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
   2020 #ifdef REF_PRINT
   2021 	REF_PRINT("SSL_CTX",a);
   2022 #endif
   2023 	if (i > 0) return;
   2024 #ifdef REF_CHECK
   2025 	if (i < 0)
   2026 		{
   2027 		fprintf(stderr,"SSL_CTX_free, bad reference count\n");
   2028 		abort(); /* ok */
   2029 		}
   2030 #endif
   2031 
   2032 	if (a->param)
   2033 		X509_VERIFY_PARAM_free(a->param);
   2034 
   2035 	/*
   2036 	 * Free internal session cache. However: the remove_cb() may reference
   2037 	 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
   2038 	 * after the sessions were flushed.
   2039 	 * As the ex_data handling routines might also touch the session cache,
   2040 	 * the most secure solution seems to be: empty (flush) the cache, then
   2041 	 * free ex_data, then finally free the cache.
   2042 	 * (See ticket [openssl.org #212].)
   2043 	 */
   2044 	if (a->sessions != NULL)
   2045 		SSL_CTX_flush_sessions(a,0);
   2046 
   2047 	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
   2048 
   2049 	if (a->sessions != NULL)
   2050 		lh_SSL_SESSION_free(a->sessions);
   2051 
   2052 	if (a->cert_store != NULL)
   2053 		X509_STORE_free(a->cert_store);
   2054 	if (a->cipher_list != NULL)
   2055 		sk_SSL_CIPHER_free(a->cipher_list);
   2056 	if (a->cipher_list_by_id != NULL)
   2057 		sk_SSL_CIPHER_free(a->cipher_list_by_id);
   2058 	if (a->cert != NULL)
   2059 		ssl_cert_free(a->cert);
   2060 	if (a->client_CA != NULL)
   2061 		sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);
   2062 	if (a->extra_certs != NULL)
   2063 		sk_X509_pop_free(a->extra_certs,X509_free);
   2064 #if 0 /* This should never be done, since it removes a global database */
   2065 	if (a->comp_methods != NULL)
   2066 		sk_SSL_COMP_pop_free(a->comp_methods,SSL_COMP_free);
   2067 #else
   2068 	a->comp_methods = NULL;
   2069 #endif
   2070 
   2071 #ifndef OPENSSL_NO_SRTP
   2072         if (a->srtp_profiles)
   2073                 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
   2074 #endif
   2075 
   2076 #ifndef OPENSSL_NO_PSK
   2077 	if (a->psk_identity_hint)
   2078 		OPENSSL_free(a->psk_identity_hint);
   2079 #endif
   2080 #ifndef OPENSSL_NO_SRP
   2081 	SSL_CTX_SRP_CTX_free(a);
   2082 #endif
   2083 #ifndef OPENSSL_NO_ENGINE
   2084 	if (a->client_cert_engine)
   2085 		ENGINE_finish(a->client_cert_engine);
   2086 #endif
   2087 
   2088 #ifndef OPENSSL_NO_BUF_FREELISTS
   2089 	if (a->wbuf_freelist)
   2090 		ssl_buf_freelist_free(a->wbuf_freelist);
   2091 	if (a->rbuf_freelist)
   2092 		ssl_buf_freelist_free(a->rbuf_freelist);
   2093 #endif
   2094 
   2095 #ifndef OPENSSL_NO_TLSEXT
   2096 	if (a->tlsext_channel_id_private)
   2097 		EVP_PKEY_free(a->tlsext_channel_id_private);
   2098 	if (a->alpn_client_proto_list != NULL)
   2099 		OPENSSL_free(a->alpn_client_proto_list);
   2100 #endif
   2101 
   2102 	OPENSSL_free(a);
   2103 	}
   2104 
   2105 void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
   2106 	{
   2107 	ctx->default_passwd_callback=cb;
   2108 	}
   2109 
   2110 void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u)
   2111 	{
   2112 	ctx->default_passwd_callback_userdata=u;
   2113 	}
   2114 
   2115 void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg)
   2116 	{
   2117 	ctx->app_verify_callback=cb;
   2118 	ctx->app_verify_arg=arg;
   2119 	}
   2120 
   2121 void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
   2122 	{
   2123 	ctx->verify_mode=mode;
   2124 	ctx->default_verify_callback=cb;
   2125 	}
   2126 
   2127 void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth)
   2128 	{
   2129 	X509_VERIFY_PARAM_set_depth(ctx->param, depth);
   2130 	}
   2131 
   2132 void ssl_set_cert_masks(CERT *c, const SSL_CIPHER *cipher)
   2133 	{
   2134 	CERT_PKEY *cpk;
   2135 	int rsa_enc,rsa_tmp,rsa_sign,dh_tmp,dh_rsa,dh_dsa,dsa_sign;
   2136 	int rsa_enc_export,dh_rsa_export,dh_dsa_export;
   2137 	int rsa_tmp_export,dh_tmp_export,kl;
   2138 	unsigned long mask_k,mask_a,emask_k,emask_a;
   2139 	int have_ecc_cert, ecdh_ok, ecdsa_ok, ecc_pkey_size;
   2140 #ifndef OPENSSL_NO_ECDH
   2141 	int have_ecdh_tmp;
   2142 #endif
   2143 	X509 *x = NULL;
   2144 	EVP_PKEY *ecc_pkey = NULL;
   2145 	int signature_nid = 0, pk_nid = 0, md_nid = 0;
   2146 
   2147 	if (c == NULL) return;
   2148 
   2149 	kl=SSL_C_EXPORT_PKEYLENGTH(cipher);
   2150 
   2151 #ifndef OPENSSL_NO_RSA
   2152 	rsa_tmp=(c->rsa_tmp != NULL || c->rsa_tmp_cb != NULL);
   2153 	rsa_tmp_export=(c->rsa_tmp_cb != NULL ||
   2154 		(rsa_tmp && RSA_size(c->rsa_tmp)*8 <= kl));
   2155 #else
   2156 	rsa_tmp=rsa_tmp_export=0;
   2157 #endif
   2158 #ifndef OPENSSL_NO_DH
   2159 	dh_tmp=(c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
   2160 	dh_tmp_export=(c->dh_tmp_cb != NULL ||
   2161 		(dh_tmp && DH_size(c->dh_tmp)*8 <= kl));
   2162 #else
   2163 	dh_tmp=dh_tmp_export=0;
   2164 #endif
   2165 
   2166 #ifndef OPENSSL_NO_ECDH
   2167 	have_ecdh_tmp=(c->ecdh_tmp != NULL || c->ecdh_tmp_cb != NULL);
   2168 #endif
   2169 	cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]);
   2170 	rsa_enc= (cpk->x509 != NULL && cpk->privatekey != NULL);
   2171 	rsa_enc_export=(rsa_enc && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
   2172 	cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]);
   2173 	rsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL);
   2174 	cpk= &(c->pkeys[SSL_PKEY_DSA_SIGN]);
   2175 	dsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL);
   2176 	cpk= &(c->pkeys[SSL_PKEY_DH_RSA]);
   2177 	dh_rsa=  (cpk->x509 != NULL && cpk->privatekey != NULL);
   2178 	dh_rsa_export=(dh_rsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
   2179 	cpk= &(c->pkeys[SSL_PKEY_DH_DSA]);
   2180 /* FIX THIS EAY EAY EAY */
   2181 	dh_dsa=  (cpk->x509 != NULL && cpk->privatekey != NULL);
   2182 	dh_dsa_export=(dh_dsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
   2183 	cpk= &(c->pkeys[SSL_PKEY_ECC]);
   2184 	have_ecc_cert= (cpk->x509 != NULL && cpk->privatekey != NULL);
   2185 	mask_k=0;
   2186 	mask_a=0;
   2187 	emask_k=0;
   2188 	emask_a=0;
   2189 
   2190 
   2191 
   2192 #ifdef CIPHER_DEBUG
   2193 	printf("rt=%d rte=%d dht=%d ecdht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n",
   2194 	        rsa_tmp,rsa_tmp_export,dh_tmp,have_ecdh_tmp,
   2195 		rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
   2196 #endif
   2197 
   2198 	cpk = &(c->pkeys[SSL_PKEY_GOST01]);
   2199 	if (cpk->x509 != NULL && cpk->privatekey !=NULL) {
   2200 		mask_k |= SSL_kGOST;
   2201 		mask_a |= SSL_aGOST01;
   2202 	}
   2203 	cpk = &(c->pkeys[SSL_PKEY_GOST94]);
   2204 	if (cpk->x509 != NULL && cpk->privatekey !=NULL) {
   2205 		mask_k |= SSL_kGOST;
   2206 		mask_a |= SSL_aGOST94;
   2207 	}
   2208 
   2209 	if (rsa_enc || (rsa_tmp && rsa_sign))
   2210 		mask_k|=SSL_kRSA;
   2211 	if (rsa_enc_export || (rsa_tmp_export && (rsa_sign || rsa_enc)))
   2212 		emask_k|=SSL_kRSA;
   2213 
   2214 #if 0
   2215 	/* The match needs to be both kEDH and aRSA or aDSA, so don't worry */
   2216 	if (	(dh_tmp || dh_rsa || dh_dsa) &&
   2217 		(rsa_enc || rsa_sign || dsa_sign))
   2218 		mask_k|=SSL_kEDH;
   2219 	if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) &&
   2220 		(rsa_enc || rsa_sign || dsa_sign))
   2221 		emask_k|=SSL_kEDH;
   2222 #endif
   2223 
   2224 	if (dh_tmp_export)
   2225 		emask_k|=SSL_kEDH;
   2226 
   2227 	if (dh_tmp)
   2228 		mask_k|=SSL_kEDH;
   2229 
   2230 	if (dh_rsa) mask_k|=SSL_kDHr;
   2231 	if (dh_rsa_export) emask_k|=SSL_kDHr;
   2232 
   2233 	if (dh_dsa) mask_k|=SSL_kDHd;
   2234 	if (dh_dsa_export) emask_k|=SSL_kDHd;
   2235 
   2236 	if (rsa_enc || rsa_sign)
   2237 		{
   2238 		mask_a|=SSL_aRSA;
   2239 		emask_a|=SSL_aRSA;
   2240 		}
   2241 
   2242 	if (dsa_sign)
   2243 		{
   2244 		mask_a|=SSL_aDSS;
   2245 		emask_a|=SSL_aDSS;
   2246 		}
   2247 
   2248 	mask_a|=SSL_aNULL;
   2249 	emask_a|=SSL_aNULL;
   2250 
   2251 #ifndef OPENSSL_NO_KRB5
   2252 	mask_k|=SSL_kKRB5;
   2253 	mask_a|=SSL_aKRB5;
   2254 	emask_k|=SSL_kKRB5;
   2255 	emask_a|=SSL_aKRB5;
   2256 #endif
   2257 
   2258 	/* An ECC certificate may be usable for ECDH and/or
   2259 	 * ECDSA cipher suites depending on the key usage extension.
   2260 	 */
   2261 	if (have_ecc_cert)
   2262 		{
   2263 		/* This call populates extension flags (ex_flags) */
   2264 		x = (c->pkeys[SSL_PKEY_ECC]).x509;
   2265 		X509_check_purpose(x, -1, 0);
   2266 		ecdh_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
   2267 		    (x->ex_kusage & X509v3_KU_KEY_AGREEMENT) : 1;
   2268 		ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
   2269 		    (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
   2270 		ecc_pkey = X509_get_pubkey(x);
   2271 		ecc_pkey_size = (ecc_pkey != NULL) ?
   2272 		    EVP_PKEY_bits(ecc_pkey) : 0;
   2273 		EVP_PKEY_free(ecc_pkey);
   2274 		if ((x->sig_alg) && (x->sig_alg->algorithm))
   2275 			{
   2276 			signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
   2277 			OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
   2278 			}
   2279 #ifndef OPENSSL_NO_ECDH
   2280 		if (ecdh_ok)
   2281 			{
   2282 
   2283 			if (pk_nid == NID_rsaEncryption || pk_nid == NID_rsa)
   2284 				{
   2285 				mask_k|=SSL_kECDHr;
   2286 				mask_a|=SSL_aECDH;
   2287 				if (ecc_pkey_size <= 163)
   2288 					{
   2289 					emask_k|=SSL_kECDHr;
   2290 					emask_a|=SSL_aECDH;
   2291 					}
   2292 				}
   2293 
   2294 			if (pk_nid == NID_X9_62_id_ecPublicKey)
   2295 				{
   2296 				mask_k|=SSL_kECDHe;
   2297 				mask_a|=SSL_aECDH;
   2298 				if (ecc_pkey_size <= 163)
   2299 					{
   2300 					emask_k|=SSL_kECDHe;
   2301 					emask_a|=SSL_aECDH;
   2302 					}
   2303 				}
   2304 			}
   2305 #endif
   2306 #ifndef OPENSSL_NO_ECDSA
   2307 		if (ecdsa_ok)
   2308 			{
   2309 			mask_a|=SSL_aECDSA;
   2310 			emask_a|=SSL_aECDSA;
   2311 			}
   2312 #endif
   2313 		}
   2314 
   2315 #ifndef OPENSSL_NO_ECDH
   2316 	if (have_ecdh_tmp)
   2317 		{
   2318 		mask_k|=SSL_kEECDH;
   2319 		emask_k|=SSL_kEECDH;
   2320 		}
   2321 #endif
   2322 
   2323 #ifndef OPENSSL_NO_PSK
   2324 	mask_k |= SSL_kPSK;
   2325 	mask_a |= SSL_aPSK;
   2326 	emask_k |= SSL_kPSK;
   2327 	emask_a |= SSL_aPSK;
   2328 #endif
   2329 
   2330 	c->mask_k=mask_k;
   2331 	c->mask_a=mask_a;
   2332 	c->export_mask_k=emask_k;
   2333 	c->export_mask_a=emask_a;
   2334 	c->valid=1;
   2335 	}
   2336 
   2337 /* This handy macro borrowed from crypto/x509v3/v3_purp.c */
   2338 #define ku_reject(x, usage) \
   2339 	(((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
   2340 
   2341 #ifndef OPENSSL_NO_EC
   2342 
   2343 int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
   2344 	{
   2345 	unsigned long alg_k, alg_a;
   2346 	EVP_PKEY *pkey = NULL;
   2347 	int keysize = 0;
   2348 	int signature_nid = 0, md_nid = 0, pk_nid = 0;
   2349 	const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
   2350 
   2351 	alg_k = cs->algorithm_mkey;
   2352 	alg_a = cs->algorithm_auth;
   2353 
   2354 	if (SSL_C_IS_EXPORT(cs))
   2355 		{
   2356 		/* ECDH key length in export ciphers must be <= 163 bits */
   2357 		pkey = X509_get_pubkey(x);
   2358 		if (pkey == NULL) return 0;
   2359 		keysize = EVP_PKEY_bits(pkey);
   2360 		EVP_PKEY_free(pkey);
   2361 		if (keysize > 163) return 0;
   2362 		}
   2363 
   2364 	/* This call populates the ex_flags field correctly */
   2365 	X509_check_purpose(x, -1, 0);
   2366 	if ((x->sig_alg) && (x->sig_alg->algorithm))
   2367 		{
   2368 		signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
   2369 		OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
   2370 		}
   2371 	if (alg_k & SSL_kECDHe || alg_k & SSL_kECDHr)
   2372 		{
   2373 		/* key usage, if present, must allow key agreement */
   2374 		if (ku_reject(x, X509v3_KU_KEY_AGREEMENT))
   2375 			{
   2376 			SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_NOT_FOR_KEY_AGREEMENT);
   2377 			return 0;
   2378 			}
   2379 		if ((alg_k & SSL_kECDHe) && TLS1_get_version(s) < TLS1_2_VERSION)
   2380 			{
   2381 			/* signature alg must be ECDSA */
   2382 			if (pk_nid != NID_X9_62_id_ecPublicKey)
   2383 				{
   2384 				SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_SHOULD_HAVE_SHA1_SIGNATURE);
   2385 				return 0;
   2386 				}
   2387 			}
   2388 		if ((alg_k & SSL_kECDHr) && TLS1_get_version(s) < TLS1_2_VERSION)
   2389 			{
   2390 			/* signature alg must be RSA */
   2391 
   2392 			if (pk_nid != NID_rsaEncryption && pk_nid != NID_rsa)
   2393 				{
   2394 				SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_SHOULD_HAVE_RSA_SIGNATURE);
   2395 				return 0;
   2396 				}
   2397 			}
   2398 		}
   2399 	if (alg_a & SSL_aECDSA)
   2400 		{
   2401 		/* key usage, if present, must allow signing */
   2402 		if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE))
   2403 			{
   2404 			SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
   2405 			return 0;
   2406 			}
   2407 		}
   2408 
   2409 	return 1;  /* all checks are ok */
   2410 	}
   2411 
   2412 #endif
   2413 
   2414 /* THIS NEEDS CLEANING UP */
   2415 CERT_PKEY *ssl_get_server_send_pkey(const SSL *s)
   2416 	{
   2417 	unsigned long alg_k,alg_a;
   2418 	CERT *c;
   2419 	int i;
   2420 
   2421 	c=s->cert;
   2422 	ssl_set_cert_masks(c, s->s3->tmp.new_cipher);
   2423 
   2424 	alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
   2425 	alg_a = s->s3->tmp.new_cipher->algorithm_auth;
   2426 
   2427 	if (alg_k & (SSL_kECDHr|SSL_kECDHe))
   2428 		{
   2429 		/* we don't need to look at SSL_kEECDH
   2430 		 * since no certificate is needed for
   2431 		 * anon ECDH and for authenticated
   2432 		 * EECDH, the check for the auth
   2433 		 * algorithm will set i correctly
   2434 		 * NOTE: For ECDH-RSA, we need an ECC
   2435 		 * not an RSA cert but for EECDH-RSA
   2436 		 * we need an RSA cert. Placing the
   2437 		 * checks for SSL_kECDH before RSA
   2438 		 * checks ensures the correct cert is chosen.
   2439 		 */
   2440 		i=SSL_PKEY_ECC;
   2441 		}
   2442 	else if (alg_a & SSL_aECDSA)
   2443 		{
   2444 		i=SSL_PKEY_ECC;
   2445 		}
   2446 	else if (alg_k & SSL_kDHr)
   2447 		i=SSL_PKEY_DH_RSA;
   2448 	else if (alg_k & SSL_kDHd)
   2449 		i=SSL_PKEY_DH_DSA;
   2450 	else if (alg_a & SSL_aDSS)
   2451 		i=SSL_PKEY_DSA_SIGN;
   2452 	else if (alg_a & SSL_aRSA)
   2453 		{
   2454 		if (c->pkeys[SSL_PKEY_RSA_ENC].x509 == NULL)
   2455 			i=SSL_PKEY_RSA_SIGN;
   2456 		else
   2457 			i=SSL_PKEY_RSA_ENC;
   2458 		}
   2459 	else if (alg_a & SSL_aKRB5)
   2460 		{
   2461 		/* VRS something else here? */
   2462 		return(NULL);
   2463 		}
   2464 	else if (alg_a & SSL_aGOST94)
   2465 		i=SSL_PKEY_GOST94;
   2466 	else if (alg_a & SSL_aGOST01)
   2467 		i=SSL_PKEY_GOST01;
   2468 	else /* if (alg_a & SSL_aNULL) */
   2469 		{
   2470 		SSLerr(SSL_F_SSL_GET_SERVER_SEND_PKEY,ERR_R_INTERNAL_ERROR);
   2471 		return(NULL);
   2472 		}
   2473 
   2474 	return c->pkeys + i;
   2475 	}
   2476 
   2477 X509 *ssl_get_server_send_cert(const SSL *s)
   2478 	{
   2479 	CERT_PKEY *cpk;
   2480 	cpk = ssl_get_server_send_pkey(s);
   2481 	if (!cpk)
   2482 		return NULL;
   2483 	return cpk->x509;
   2484 	}
   2485 
   2486 EVP_PKEY *ssl_get_sign_pkey(SSL *s,const SSL_CIPHER *cipher, const EVP_MD **pmd)
   2487 	{
   2488 	unsigned long alg_a;
   2489 	CERT *c;
   2490 
   2491 	alg_a = cipher->algorithm_auth;
   2492 	c=s->cert;
   2493 
   2494 	/* SHA1 is the default for all signature algorithms up to TLS 1.2,
   2495 	 * except RSA which is handled specially in s3_srvr.c */
   2496 	if (pmd)
   2497 		*pmd = EVP_sha1();
   2498 
   2499 	if ((alg_a & SSL_aDSS) &&
   2500 	    (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL))
   2501 		{
   2502 		if (pmd && s->s3 && s->s3->digest_dsa)
   2503 			*pmd = s->s3->digest_dsa;
   2504 		return c->pkeys[SSL_PKEY_DSA_SIGN].privatekey;
   2505 		}
   2506 	else if (alg_a & SSL_aRSA)
   2507 		{
   2508 		if (pmd && s->s3 && s->s3->digest_rsa)
   2509 			*pmd = s->s3->digest_rsa;
   2510 		if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
   2511 			return c->pkeys[SSL_PKEY_RSA_SIGN].privatekey;
   2512 		if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
   2513 			return c->pkeys[SSL_PKEY_RSA_ENC].privatekey;
   2514 		}
   2515 	else if ((alg_a & SSL_aECDSA) &&
   2516 	         (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
   2517 		{
   2518 		if (pmd && s->s3 && s->s3->digest_ecdsa)
   2519 			*pmd = s->s3->digest_ecdsa;
   2520 		return c->pkeys[SSL_PKEY_ECC].privatekey;
   2521 		}
   2522 
   2523 	SSLerr(SSL_F_SSL_GET_SIGN_PKEY,ERR_R_INTERNAL_ERROR);
   2524 	return(NULL);
   2525 	}
   2526 
   2527 void ssl_update_cache(SSL *s,int mode)
   2528 	{
   2529 	int i;
   2530 
   2531 	/* If the session_id_length is 0, we are not supposed to cache it,
   2532 	 * and it would be rather hard to do anyway :-) */
   2533 	if (s->session->session_id_length == 0) return;
   2534 
   2535 	i=s->session_ctx->session_cache_mode;
   2536 	if ((i & mode) && (!s->hit)
   2537 		&& ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
   2538 		    || SSL_CTX_add_session(s->session_ctx,s->session))
   2539 		&& (s->session_ctx->new_session_cb != NULL))
   2540 		{
   2541 		CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
   2542 		if (!s->session_ctx->new_session_cb(s,s->session))
   2543 			SSL_SESSION_free(s->session);
   2544 		}
   2545 
   2546 	/* auto flush every 255 connections */
   2547 	if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
   2548 		((i & mode) == mode))
   2549 		{
   2550 		if (  (((mode & SSL_SESS_CACHE_CLIENT)
   2551 			?s->session_ctx->stats.sess_connect_good
   2552 			:s->session_ctx->stats.sess_accept_good) & 0xff) == 0xff)
   2553 			{
   2554 			SSL_CTX_flush_sessions(s->session_ctx,(unsigned long)time(NULL));
   2555 			}
   2556 		}
   2557 	}
   2558 
   2559 const SSL_METHOD *SSL_get_ssl_method(SSL *s)
   2560 	{
   2561 	return(s->method);
   2562 	}
   2563 
   2564 int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
   2565 	{
   2566 	int conn= -1;
   2567 	int ret=1;
   2568 
   2569 	if (s->method != meth)
   2570 		{
   2571 		if (s->handshake_func != NULL)
   2572 			conn=(s->handshake_func == s->method->ssl_connect);
   2573 
   2574 		if (s->method->version == meth->version)
   2575 			s->method=meth;
   2576 		else
   2577 			{
   2578 			s->method->ssl_free(s);
   2579 			s->method=meth;
   2580 			ret=s->method->ssl_new(s);
   2581 			}
   2582 
   2583 		if (conn == 1)
   2584 			s->handshake_func=meth->ssl_connect;
   2585 		else if (conn == 0)
   2586 			s->handshake_func=meth->ssl_accept;
   2587 		}
   2588 	return(ret);
   2589 	}
   2590 
   2591 int SSL_get_error(const SSL *s,int i)
   2592 	{
   2593 	int reason;
   2594 	unsigned long l;
   2595 	BIO *bio;
   2596 
   2597 	if (i > 0) return(SSL_ERROR_NONE);
   2598 
   2599 	/* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
   2600 	 * etc, where we do encode the error */
   2601 	if ((l=ERR_peek_error()) != 0)
   2602 		{
   2603 		if (ERR_GET_LIB(l) == ERR_LIB_SYS)
   2604 			return(SSL_ERROR_SYSCALL);
   2605 		else
   2606 			return(SSL_ERROR_SSL);
   2607 		}
   2608 
   2609 	if ((i < 0) && SSL_want_read(s))
   2610 		{
   2611 		bio=SSL_get_rbio(s);
   2612 		if (BIO_should_read(bio))
   2613 			return(SSL_ERROR_WANT_READ);
   2614 		else if (BIO_should_write(bio))
   2615 			/* This one doesn't make too much sense ... We never try
   2616 			 * to write to the rbio, and an application program where
   2617 			 * rbio and wbio are separate couldn't even know what it
   2618 			 * should wait for.
   2619 			 * However if we ever set s->rwstate incorrectly
   2620 			 * (so that we have SSL_want_read(s) instead of
   2621 			 * SSL_want_write(s)) and rbio and wbio *are* the same,
   2622 			 * this test works around that bug; so it might be safer
   2623 			 * to keep it. */
   2624 			return(SSL_ERROR_WANT_WRITE);
   2625 		else if (BIO_should_io_special(bio))
   2626 			{
   2627 			reason=BIO_get_retry_reason(bio);
   2628 			if (reason == BIO_RR_CONNECT)
   2629 				return(SSL_ERROR_WANT_CONNECT);
   2630 			else if (reason == BIO_RR_ACCEPT)
   2631 				return(SSL_ERROR_WANT_ACCEPT);
   2632 			else
   2633 				return(SSL_ERROR_SYSCALL); /* unknown */
   2634 			}
   2635 		}
   2636 
   2637 	if ((i < 0) && SSL_want_write(s))
   2638 		{
   2639 		bio=SSL_get_wbio(s);
   2640 		if (BIO_should_write(bio))
   2641 			return(SSL_ERROR_WANT_WRITE);
   2642 		else if (BIO_should_read(bio))
   2643 			/* See above (SSL_want_read(s) with BIO_should_write(bio)) */
   2644 			return(SSL_ERROR_WANT_READ);
   2645 		else if (BIO_should_io_special(bio))
   2646 			{
   2647 			reason=BIO_get_retry_reason(bio);
   2648 			if (reason == BIO_RR_CONNECT)
   2649 				return(SSL_ERROR_WANT_CONNECT);
   2650 			else if (reason == BIO_RR_ACCEPT)
   2651 				return(SSL_ERROR_WANT_ACCEPT);
   2652 			else
   2653 				return(SSL_ERROR_SYSCALL);
   2654 			}
   2655 		}
   2656 	if ((i < 0) && SSL_want_x509_lookup(s))
   2657 		{
   2658 		return(SSL_ERROR_WANT_X509_LOOKUP);
   2659 		}
   2660 
   2661 	if (i == 0)
   2662 		{
   2663 		if (s->version == SSL2_VERSION)
   2664 			{
   2665 			/* assume it is the socket being closed */
   2666 			return(SSL_ERROR_ZERO_RETURN);
   2667 			}
   2668 		else
   2669 			{
   2670 			if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
   2671 				(s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
   2672 				return(SSL_ERROR_ZERO_RETURN);
   2673 			}
   2674 		}
   2675 	return(SSL_ERROR_SYSCALL);
   2676 	}
   2677 
   2678 int SSL_do_handshake(SSL *s)
   2679 	{
   2680 	int ret=1;
   2681 
   2682 	if (s->handshake_func == NULL)
   2683 		{
   2684 		SSLerr(SSL_F_SSL_DO_HANDSHAKE,SSL_R_CONNECTION_TYPE_NOT_SET);
   2685 		return(-1);
   2686 		}
   2687 
   2688 	s->method->ssl_renegotiate_check(s);
   2689 
   2690 	if (SSL_in_init(s) || SSL_in_before(s))
   2691 		{
   2692 		ret=s->handshake_func(s);
   2693 		}
   2694 	return(ret);
   2695 	}
   2696 
   2697 /* For the next 2 functions, SSL_clear() sets shutdown and so
   2698  * one of these calls will reset it */
   2699 void SSL_set_accept_state(SSL *s)
   2700 	{
   2701 	s->server=1;
   2702 	s->shutdown=0;
   2703 	s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
   2704 	s->handshake_func=s->method->ssl_accept;
   2705 	/* clear the current cipher */
   2706 	ssl_clear_cipher_ctx(s);
   2707 	ssl_clear_hash_ctx(&s->read_hash);
   2708 	ssl_clear_hash_ctx(&s->write_hash);
   2709 	}
   2710 
   2711 void SSL_set_connect_state(SSL *s)
   2712 	{
   2713 	s->server=0;
   2714 	s->shutdown=0;
   2715 	s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
   2716 	s->handshake_func=s->method->ssl_connect;
   2717 	/* clear the current cipher */
   2718 	ssl_clear_cipher_ctx(s);
   2719 	ssl_clear_hash_ctx(&s->read_hash);
   2720 	ssl_clear_hash_ctx(&s->write_hash);
   2721 	}
   2722 
   2723 int ssl_undefined_function(SSL *s)
   2724 	{
   2725 	SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
   2726 	return(0);
   2727 	}
   2728 
   2729 int ssl_undefined_void_function(void)
   2730 	{
   2731 	SSLerr(SSL_F_SSL_UNDEFINED_VOID_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
   2732 	return(0);
   2733 	}
   2734 
   2735 int ssl_undefined_const_function(const SSL *s)
   2736 	{
   2737 	SSLerr(SSL_F_SSL_UNDEFINED_CONST_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
   2738 	return(0);
   2739 	}
   2740 
   2741 SSL_METHOD *ssl_bad_method(int ver)
   2742 	{
   2743 	SSLerr(SSL_F_SSL_BAD_METHOD,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
   2744 	return(NULL);
   2745 	}
   2746 
   2747 static const char *ssl_get_version(int version)
   2748 	{
   2749 	if (version == TLS1_2_VERSION)
   2750 		return("TLSv1.2");
   2751 	else if (version == TLS1_1_VERSION)
   2752 		return("TLSv1.1");
   2753 	else if (version == TLS1_VERSION)
   2754 		return("TLSv1");
   2755 	else if (version == SSL3_VERSION)
   2756 		return("SSLv3");
   2757 	else if (version == SSL2_VERSION)
   2758 		return("SSLv2");
   2759 	else
   2760 		return("unknown");
   2761 	}
   2762 
   2763 const char *SSL_get_version(const SSL *s)
   2764 	{
   2765 		return ssl_get_version(s->version);
   2766 	}
   2767 
   2768 const char *SSL_SESSION_get_version(const SSL_SESSION *s)
   2769 	{
   2770 		return ssl_get_version(s->ssl_version);
   2771 	}
   2772 
   2773 const char* SSL_authentication_method(const SSL* ssl)
   2774 	{
   2775 	if (ssl->cert != NULL && ssl->cert->rsa_tmp != NULL)
   2776 		return SSL_TXT_RSA "_" SSL_TXT_EXPORT;
   2777 	switch (ssl->version)
   2778 		{
   2779 	case SSL2_VERSION:
   2780 		return SSL_TXT_RSA;
   2781 	default:
   2782 		return SSL_CIPHER_authentication_method(ssl->s3->tmp.new_cipher);
   2783 		}
   2784 	}
   2785 
   2786 SSL *SSL_dup(SSL *s)
   2787 	{
   2788 	STACK_OF(X509_NAME) *sk;
   2789 	X509_NAME *xn;
   2790 	SSL *ret;
   2791 	int i;
   2792 
   2793 	if ((ret=SSL_new(SSL_get_SSL_CTX(s))) == NULL)
   2794 	    return(NULL);
   2795 
   2796 	ret->version = s->version;
   2797 	ret->type = s->type;
   2798 	ret->method = s->method;
   2799 
   2800 	if (s->session != NULL)
   2801 		{
   2802 		/* This copies session-id, SSL_METHOD, sid_ctx, and 'cert' */
   2803 		SSL_copy_session_id(ret,s);
   2804 		}
   2805 	else
   2806 		{
   2807 		/* No session has been established yet, so we have to expect
   2808 		 * that s->cert or ret->cert will be changed later --
   2809 		 * they should not both point to the same object,
   2810 		 * and thus we can't use SSL_copy_session_id. */
   2811 
   2812 		ret->method->ssl_free(ret);
   2813 		ret->method = s->method;
   2814 		ret->method->ssl_new(ret);
   2815 
   2816 		if (s->cert != NULL)
   2817 			{
   2818 			if (ret->cert != NULL)
   2819 				{
   2820 				ssl_cert_free(ret->cert);
   2821 				}
   2822 			ret->cert = ssl_cert_dup(s->cert);
   2823 			if (ret->cert == NULL)
   2824 				goto err;
   2825 			}
   2826 
   2827 		SSL_set_session_id_context(ret,
   2828 			s->sid_ctx, s->sid_ctx_length);
   2829 		}
   2830 
   2831 	ret->options=s->options;
   2832 	ret->mode=s->mode;
   2833 	SSL_set_max_cert_list(ret,SSL_get_max_cert_list(s));
   2834 	SSL_set_read_ahead(ret,SSL_get_read_ahead(s));
   2835 	ret->msg_callback = s->msg_callback;
   2836 	ret->msg_callback_arg = s->msg_callback_arg;
   2837 	SSL_set_verify(ret,SSL_get_verify_mode(s),
   2838 		SSL_get_verify_callback(s));
   2839 	SSL_set_verify_depth(ret,SSL_get_verify_depth(s));
   2840 	ret->generate_session_id = s->generate_session_id;
   2841 
   2842 	SSL_set_info_callback(ret,SSL_get_info_callback(s));
   2843 
   2844 	ret->debug=s->debug;
   2845 
   2846 	/* copy app data, a little dangerous perhaps */
   2847 	if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data))
   2848 		goto err;
   2849 
   2850 	/* setup rbio, and wbio */
   2851 	if (s->rbio != NULL)
   2852 		{
   2853 		if (!BIO_dup_state(s->rbio,(char *)&ret->rbio))
   2854 			goto err;
   2855 		}
   2856 	if (s->wbio != NULL)
   2857 		{
   2858 		if (s->wbio != s->rbio)
   2859 			{
   2860 			if (!BIO_dup_state(s->wbio,(char *)&ret->wbio))
   2861 				goto err;
   2862 			}
   2863 		else
   2864 			ret->wbio=ret->rbio;
   2865 		}
   2866 	ret->rwstate = s->rwstate;
   2867 	ret->in_handshake = s->in_handshake;
   2868 	ret->handshake_func = s->handshake_func;
   2869 	ret->server = s->server;
   2870 	ret->renegotiate = s->renegotiate;
   2871 	ret->new_session = s->new_session;
   2872 	ret->quiet_shutdown = s->quiet_shutdown;
   2873 	ret->shutdown=s->shutdown;
   2874 	ret->state=s->state; /* SSL_dup does not really work at any state, though */
   2875 	ret->rstate=s->rstate;
   2876 	ret->init_num = 0; /* would have to copy ret->init_buf, ret->init_msg, ret->init_num, ret->init_off */
   2877 	ret->hit=s->hit;
   2878 
   2879 	X509_VERIFY_PARAM_inherit(ret->param, s->param);
   2880 
   2881 	/* dup the cipher_list and cipher_list_by_id stacks */
   2882 	if (s->cipher_list != NULL)
   2883 		{
   2884 		if ((ret->cipher_list=sk_SSL_CIPHER_dup(s->cipher_list)) == NULL)
   2885 			goto err;
   2886 		}
   2887 	if (s->cipher_list_by_id != NULL)
   2888 		if ((ret->cipher_list_by_id=sk_SSL_CIPHER_dup(s->cipher_list_by_id))
   2889 			== NULL)
   2890 			goto err;
   2891 
   2892 	/* Dup the client_CA list */
   2893 	if (s->client_CA != NULL)
   2894 		{
   2895 		if ((sk=sk_X509_NAME_dup(s->client_CA)) == NULL) goto err;
   2896 		ret->client_CA=sk;
   2897 		for (i=0; i<sk_X509_NAME_num(sk); i++)
   2898 			{
   2899 			xn=sk_X509_NAME_value(sk,i);
   2900 			if (sk_X509_NAME_set(sk,i,X509_NAME_dup(xn)) == NULL)
   2901 				{
   2902 				X509_NAME_free(xn);
   2903 				goto err;
   2904 				}
   2905 			}
   2906 		}
   2907 
   2908 	if (0)
   2909 		{
   2910 err:
   2911 		if (ret != NULL) SSL_free(ret);
   2912 		ret=NULL;
   2913 		}
   2914 	return(ret);
   2915 	}
   2916 
   2917 void ssl_clear_cipher_ctx(SSL *s)
   2918 	{
   2919 	if (s->enc_read_ctx != NULL)
   2920 		{
   2921 		EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
   2922 		OPENSSL_free(s->enc_read_ctx);
   2923 		s->enc_read_ctx=NULL;
   2924 		}
   2925 	if (s->enc_write_ctx != NULL)
   2926 		{
   2927 		EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
   2928 		OPENSSL_free(s->enc_write_ctx);
   2929 		s->enc_write_ctx=NULL;
   2930 		}
   2931 #ifndef OPENSSL_NO_COMP
   2932 	if (s->expand != NULL)
   2933 		{
   2934 		COMP_CTX_free(s->expand);
   2935 		s->expand=NULL;
   2936 		}
   2937 	if (s->compress != NULL)
   2938 		{
   2939 		COMP_CTX_free(s->compress);
   2940 		s->compress=NULL;
   2941 		}
   2942 #endif
   2943 	}
   2944 
   2945 /* Fix this function so that it takes an optional type parameter */
   2946 X509 *SSL_get_certificate(const SSL *s)
   2947 	{
   2948 	if (s->server)
   2949 		return(ssl_get_server_send_cert(s));
   2950 	else if (s->cert != NULL)
   2951 		return(s->cert->key->x509);
   2952 	else
   2953 		return(NULL);
   2954 	}
   2955 
   2956 /* Fix this function so that it takes an optional type parameter */
   2957 EVP_PKEY *SSL_get_privatekey(SSL *s)
   2958 	{
   2959 	if (s->cert != NULL)
   2960 		return(s->cert->key->privatekey);
   2961 	else
   2962 		return(NULL);
   2963 	}
   2964 
   2965 const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
   2966 	{
   2967 	if ((s->session != NULL) && (s->session->cipher != NULL))
   2968 		return(s->session->cipher);
   2969 	return(NULL);
   2970 	}
   2971 #ifdef OPENSSL_NO_COMP
   2972 const void *SSL_get_current_compression(SSL *s)
   2973 	{
   2974 	return NULL;
   2975 	}
   2976 const void *SSL_get_current_expansion(SSL *s)
   2977 	{
   2978 	return NULL;
   2979 	}
   2980 #else
   2981 
   2982 const COMP_METHOD *SSL_get_current_compression(SSL *s)
   2983 	{
   2984 	if (s->compress != NULL)
   2985 		return(s->compress->meth);
   2986 	return(NULL);
   2987 	}
   2988 
   2989 const COMP_METHOD *SSL_get_current_expansion(SSL *s)
   2990 	{
   2991 	if (s->expand != NULL)
   2992 		return(s->expand->meth);
   2993 	return(NULL);
   2994 	}
   2995 #endif
   2996 
   2997 int ssl_init_wbio_buffer(SSL *s,int push)
   2998 	{
   2999 	BIO *bbio;
   3000 
   3001 	if (s->bbio == NULL)
   3002 		{
   3003 		bbio=BIO_new(BIO_f_buffer());
   3004 		if (bbio == NULL) return(0);
   3005 		s->bbio=bbio;
   3006 		}
   3007 	else
   3008 		{
   3009 		bbio=s->bbio;
   3010 		if (s->bbio == s->wbio)
   3011 			s->wbio=BIO_pop(s->wbio);
   3012 		}
   3013 	(void)BIO_reset(bbio);
   3014 /*	if (!BIO_set_write_buffer_size(bbio,16*1024)) */
   3015 	if (!BIO_set_read_buffer_size(bbio,1))
   3016 		{
   3017 		SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER,ERR_R_BUF_LIB);
   3018 		return(0);
   3019 		}
   3020 	if (push)
   3021 		{
   3022 		if (s->wbio != bbio)
   3023 			s->wbio=BIO_push(bbio,s->wbio);
   3024 		}
   3025 	else
   3026 		{
   3027 		if (s->wbio == bbio)
   3028 			s->wbio=BIO_pop(bbio);
   3029 		}
   3030 	return(1);
   3031 	}
   3032 
   3033 void ssl_free_wbio_buffer(SSL *s)
   3034 	{
   3035 	if (s->bbio == NULL) return;
   3036 
   3037 	if (s->bbio == s->wbio)
   3038 		{
   3039 		/* remove buffering */
   3040 		s->wbio=BIO_pop(s->wbio);
   3041 #ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
   3042 		assert(s->wbio != NULL);
   3043 #endif
   3044 	}
   3045 	BIO_free(s->bbio);
   3046 	s->bbio=NULL;
   3047 	}
   3048 
   3049 void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
   3050 	{
   3051 	ctx->quiet_shutdown=mode;
   3052 	}
   3053 
   3054 int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
   3055 	{
   3056 	return(ctx->quiet_shutdown);
   3057 	}
   3058 
   3059 void SSL_set_quiet_shutdown(SSL *s,int mode)
   3060 	{
   3061 	s->quiet_shutdown=mode;
   3062 	}
   3063 
   3064 int SSL_get_quiet_shutdown(const SSL *s)
   3065 	{
   3066 	return(s->quiet_shutdown);
   3067 	}
   3068 
   3069 void SSL_set_shutdown(SSL *s,int mode)
   3070 	{
   3071 	s->shutdown=mode;
   3072 	}
   3073 
   3074 int SSL_get_shutdown(const SSL *s)
   3075 	{
   3076 	return(s->shutdown);
   3077 	}
   3078 
   3079 int SSL_version(const SSL *s)
   3080 	{
   3081 	return(s->version);
   3082 	}
   3083 
   3084 SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
   3085 	{
   3086 	return(ssl->ctx);
   3087 	}
   3088 
   3089 SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
   3090 	{
   3091 	if (ssl->ctx == ctx)
   3092 		return ssl->ctx;
   3093 #ifndef OPENSSL_NO_TLSEXT
   3094 	if (ctx == NULL)
   3095 		ctx = ssl->initial_ctx;
   3096 #endif
   3097 	if (ssl->cert != NULL)
   3098 		ssl_cert_free(ssl->cert);
   3099 	ssl->cert = ssl_cert_dup(ctx->cert);
   3100 	CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
   3101 	if (ssl->ctx != NULL)
   3102 		SSL_CTX_free(ssl->ctx); /* decrement reference count */
   3103 	ssl->ctx = ctx;
   3104 	return(ssl->ctx);
   3105 	}
   3106 
   3107 #ifndef OPENSSL_NO_STDIO
   3108 int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
   3109 	{
   3110 	return(X509_STORE_set_default_paths(ctx->cert_store));
   3111 	}
   3112 
   3113 int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
   3114 		const char *CApath)
   3115 	{
   3116 	return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
   3117 	}
   3118 #endif
   3119 
   3120 void SSL_set_info_callback(SSL *ssl,
   3121 	void (*cb)(const SSL *ssl,int type,int val))
   3122 	{
   3123 	ssl->info_callback=cb;
   3124 	}
   3125 
   3126 /* One compiler (Diab DCC) doesn't like argument names in returned
   3127    function pointer.  */
   3128 void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
   3129 	{
   3130 	return ssl->info_callback;
   3131 	}
   3132 
   3133 int SSL_state(const SSL *ssl)
   3134 	{
   3135 	return(ssl->state);
   3136 	}
   3137 
   3138 void SSL_set_state(SSL *ssl, int state)
   3139 	{
   3140 	ssl->state = state;
   3141 	}
   3142 
   3143 void SSL_set_verify_result(SSL *ssl,long arg)
   3144 	{
   3145 	ssl->verify_result=arg;
   3146 	}
   3147 
   3148 long SSL_get_verify_result(const SSL *ssl)
   3149 	{
   3150 	return(ssl->verify_result);
   3151 	}
   3152 
   3153 int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
   3154 			 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
   3155 	{
   3156 	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
   3157 				new_func, dup_func, free_func);
   3158 	}
   3159 
   3160 int SSL_set_ex_data(SSL *s,int idx,void *arg)
   3161 	{
   3162 	return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
   3163 	}
   3164 
   3165 void *SSL_get_ex_data(const SSL *s,int idx)
   3166 	{
   3167 	return(CRYPTO_get_ex_data(&s->ex_data,idx));
   3168 	}
   3169 
   3170 int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
   3171 			     CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
   3172 	{
   3173 	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
   3174 				new_func, dup_func, free_func);
   3175 	}
   3176 
   3177 int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
   3178 	{
   3179 	return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
   3180 	}
   3181 
   3182 void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx)
   3183 	{
   3184 	return(CRYPTO_get_ex_data(&s->ex_data,idx));
   3185 	}
   3186 
   3187 int ssl_ok(SSL *s)
   3188 	{
   3189 	return(1);
   3190 	}
   3191 
   3192 X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
   3193 	{
   3194 	return(ctx->cert_store);
   3195 	}
   3196 
   3197 void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
   3198 	{
   3199 	if (ctx->cert_store != NULL)
   3200 		X509_STORE_free(ctx->cert_store);
   3201 	ctx->cert_store=store;
   3202 	}
   3203 
   3204 int SSL_want(const SSL *s)
   3205 	{
   3206 	return(s->rwstate);
   3207 	}
   3208 
   3209 /*!
   3210  * \brief Set the callback for generating temporary RSA keys.
   3211  * \param ctx the SSL context.
   3212  * \param cb the callback
   3213  */
   3214 
   3215 #ifndef OPENSSL_NO_RSA
   3216 void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
   3217 							  int is_export,
   3218 							  int keylength))
   3219     {
   3220     SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
   3221     }
   3222 
   3223 void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
   3224 						  int is_export,
   3225 						  int keylength))
   3226     {
   3227     SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
   3228     }
   3229 #endif
   3230 
   3231 #ifdef DOXYGEN
   3232 /*!
   3233  * \brief The RSA temporary key callback function.
   3234  * \param ssl the SSL session.
   3235  * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
   3236  * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
   3237  * of the required key in bits.
   3238  * \return the temporary RSA key.
   3239  * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
   3240  */
   3241 
   3242 RSA *cb(SSL *ssl,int is_export,int keylength)
   3243     {}
   3244 #endif
   3245 
   3246 /*!
   3247  * \brief Set the callback for generating temporary DH keys.
   3248  * \param ctx the SSL context.
   3249  * \param dh the callback
   3250  */
   3251 
   3252 #ifndef OPENSSL_NO_DH
   3253 void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
   3254                                                         int keylength))
   3255 	{
   3256 	SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
   3257 	}
   3258 
   3259 void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
   3260                                                 int keylength))
   3261 	{
   3262 	SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
   3263 	}
   3264 #endif
   3265 
   3266 #ifndef OPENSSL_NO_ECDH
   3267 void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
   3268                                                                 int keylength))
   3269 	{
   3270 	SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
   3271 	}
   3272 
   3273 void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
   3274                                                         int keylength))
   3275 	{
   3276 	SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
   3277 	}
   3278 #endif
   3279 
   3280 #ifndef OPENSSL_NO_PSK
   3281 int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
   3282 	{
   3283 	if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
   3284 		{
   3285 		SSLerr(SSL_F_SSL_CTX_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG);
   3286 		return 0;
   3287 		}
   3288 	if (ctx->psk_identity_hint != NULL)
   3289 		OPENSSL_free(ctx->psk_identity_hint);
   3290 	if (identity_hint != NULL)
   3291 		{
   3292 		ctx->psk_identity_hint = BUF_strdup(identity_hint);
   3293 		if (ctx->psk_identity_hint == NULL)
   3294 			return 0;
   3295 		}
   3296 	else
   3297 		ctx->psk_identity_hint = NULL;
   3298 	return 1;
   3299 	}
   3300 
   3301 int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
   3302 	{
   3303 	if (s == NULL)
   3304 		return 0;
   3305 
   3306 	if (s->session == NULL)
   3307 		return 1; /* session not created yet, ignored */
   3308 
   3309 	if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
   3310 		{
   3311 		SSLerr(SSL_F_SSL_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG);
   3312 		return 0;
   3313 		}
   3314 	if (s->session->psk_identity_hint != NULL)
   3315 		OPENSSL_free(s->session->psk_identity_hint);
   3316 	if (identity_hint != NULL)
   3317 		{
   3318 		s->session->psk_identity_hint = BUF_strdup(identity_hint);
   3319 		if (s->session->psk_identity_hint == NULL)
   3320 			return 0;
   3321 		}
   3322 	else
   3323 		s->session->psk_identity_hint = NULL;
   3324 	return 1;
   3325 	}
   3326 
   3327 const char *SSL_get_psk_identity_hint(const SSL *s)
   3328 	{
   3329 	if (s == NULL || s->session == NULL)
   3330 		return NULL;
   3331 	return(s->session->psk_identity_hint);
   3332 	}
   3333 
   3334 const char *SSL_get_psk_identity(const SSL *s)
   3335 	{
   3336 	if (s == NULL || s->session == NULL)
   3337 		return NULL;
   3338 	return(s->session->psk_identity);
   3339 	}
   3340 
   3341 void SSL_set_psk_client_callback(SSL *s,
   3342     unsigned int (*cb)(SSL *ssl, const char *hint,
   3343                        char *identity, unsigned int max_identity_len, unsigned char *psk,
   3344                        unsigned int max_psk_len))
   3345 	{
   3346 	s->psk_client_callback = cb;
   3347 	}
   3348 
   3349 void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
   3350     unsigned int (*cb)(SSL *ssl, const char *hint,
   3351                        char *identity, unsigned int max_identity_len, unsigned char *psk,
   3352                        unsigned int max_psk_len))
   3353 	{
   3354 	ctx->psk_client_callback = cb;
   3355 	}
   3356 
   3357 void SSL_set_psk_server_callback(SSL *s,
   3358     unsigned int (*cb)(SSL *ssl, const char *identity,
   3359                        unsigned char *psk, unsigned int max_psk_len))
   3360 	{
   3361 	s->psk_server_callback = cb;
   3362 	}
   3363 
   3364 void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
   3365     unsigned int (*cb)(SSL *ssl, const char *identity,
   3366                        unsigned char *psk, unsigned int max_psk_len))
   3367 	{
   3368 	ctx->psk_server_callback = cb;
   3369 	}
   3370 #endif
   3371 
   3372 void SSL_CTX_set_msg_callback(SSL_CTX *ctx, void (*cb)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))
   3373 	{
   3374 	SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
   3375 	}
   3376 void SSL_set_msg_callback(SSL *ssl, void (*cb)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))
   3377 	{
   3378 	SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
   3379 	}
   3380 
   3381 int SSL_cutthrough_complete(const SSL *s)
   3382 	{
   3383 	return (!s->server &&                 /* cutthrough only applies to clients */
   3384 		!s->hit &&                        /* full-handshake */
   3385 		s->version >= SSL3_VERSION &&
   3386 		s->s3->in_read_app_data == 0 &&   /* cutthrough only applies to write() */
   3387 		(SSL_get_mode((SSL*)s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) &&  /* cutthrough enabled */
   3388 		SSL_get_cipher_bits(s, NULL) >= 128 &&                      /* strong cipher choosen */
   3389 		s->s3->previous_server_finished_len == 0 &&                 /* not a renegotiation handshake */
   3390 		(s->state == SSL3_ST_CR_SESSION_TICKET_A ||                 /* ready to write app-data*/
   3391 			s->state == SSL3_ST_CR_FINISHED_A));
   3392 	}
   3393 
   3394 /* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
   3395  * vairable, freeing  EVP_MD_CTX previously stored in that variable, if
   3396  * any. If EVP_MD pointer is passed, initializes ctx with this md
   3397  * Returns newly allocated ctx;
   3398  */
   3399 
   3400 EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md)
   3401 {
   3402 	ssl_clear_hash_ctx(hash);
   3403 	*hash = EVP_MD_CTX_create();
   3404 	if (md) EVP_DigestInit_ex(*hash,md,NULL);
   3405 	return *hash;
   3406 }
   3407 void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
   3408 {
   3409 
   3410 	if (*hash) EVP_MD_CTX_destroy(*hash);
   3411 	*hash=NULL;
   3412 }
   3413 
   3414 void SSL_set_debug(SSL *s, int debug)
   3415 	{
   3416 	s->debug = debug;
   3417 	}
   3418 
   3419 int SSL_cache_hit(SSL *s)
   3420 	{
   3421 	return s->hit;
   3422 	}
   3423 
   3424 #if defined(_WINDLL) && defined(OPENSSL_SYS_WIN16)
   3425 #include "../crypto/bio/bss_file.c"
   3426 #endif
   3427 
   3428 IMPLEMENT_STACK_OF(SSL_CIPHER)
   3429 IMPLEMENT_STACK_OF(SSL_COMP)
   3430 IMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER,
   3431 				    ssl_cipher_id);
   3432