ssl_lib.c revision 269686
155714Skris/*! \file ssl/ssl_lib.c
255714Skris *  \brief Version independent SSL functions.
355714Skris */
455714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
555714Skris * All rights reserved.
655714Skris *
755714Skris * This package is an SSL implementation written
855714Skris * by Eric Young (eay@cryptsoft.com).
955714Skris * The implementation was written so as to conform with Netscapes SSL.
1055714Skris *
1155714Skris * This library is free for commercial and non-commercial use as long as
1255714Skris * the following conditions are aheared to.  The following conditions
1355714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1455714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1555714Skris * included with this distribution is covered by the same copyright terms
1655714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1755714Skris *
1855714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1955714Skris * the code are not to be removed.
2055714Skris * If this package is used in a product, Eric Young should be given attribution
2155714Skris * as the author of the parts of the library used.
2255714Skris * This can be in the form of a textual message at program startup or
2355714Skris * in documentation (online or textual) provided with the package.
2455714Skris *
2555714Skris * Redistribution and use in source and binary forms, with or without
2655714Skris * modification, are permitted provided that the following conditions
2755714Skris * are met:
2855714Skris * 1. Redistributions of source code must retain the copyright
2955714Skris *    notice, this list of conditions and the following disclaimer.
3055714Skris * 2. Redistributions in binary form must reproduce the above copyright
3155714Skris *    notice, this list of conditions and the following disclaimer in the
3255714Skris *    documentation and/or other materials provided with the distribution.
3355714Skris * 3. All advertising materials mentioning features or use of this software
3455714Skris *    must display the following acknowledgement:
3555714Skris *    "This product includes cryptographic software written by
3655714Skris *     Eric Young (eay@cryptsoft.com)"
3755714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3855714Skris *    being used are not cryptographic related :-).
3955714Skris * 4. If you include any Windows specific code (or a derivative thereof) from
4055714Skris *    the apps directory (application code) you must include an acknowledgement:
4155714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4255714Skris *
4355714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4455714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4555714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4655714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4755714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4855714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4955714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5055714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5155714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5255714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5355714Skris * SUCH DAMAGE.
5455714Skris *
5555714Skris * The licence and distribution terms for any publically available version or
5655714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5755714Skris * copied and put under another distribution licence
5855714Skris * [including the GNU Public Licence.]
5955714Skris */
60109998Smarkm/* ====================================================================
61238405Sjkim * Copyright (c) 1998-2007 The OpenSSL Project.  All rights reserved.
62109998Smarkm *
63109998Smarkm * Redistribution and use in source and binary forms, with or without
64109998Smarkm * modification, are permitted provided that the following conditions
65109998Smarkm * are met:
66109998Smarkm *
67109998Smarkm * 1. Redistributions of source code must retain the above copyright
68109998Smarkm *    notice, this list of conditions and the following disclaimer.
69109998Smarkm *
70109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright
71109998Smarkm *    notice, this list of conditions and the following disclaimer in
72109998Smarkm *    the documentation and/or other materials provided with the
73109998Smarkm *    distribution.
74109998Smarkm *
75109998Smarkm * 3. All advertising materials mentioning features or use of this
76109998Smarkm *    software must display the following acknowledgment:
77109998Smarkm *    "This product includes software developed by the OpenSSL Project
78109998Smarkm *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
79109998Smarkm *
80109998Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
81109998Smarkm *    endorse or promote products derived from this software without
82109998Smarkm *    prior written permission. For written permission, please contact
83109998Smarkm *    openssl-core@openssl.org.
84109998Smarkm *
85109998Smarkm * 5. Products derived from this software may not be called "OpenSSL"
86109998Smarkm *    nor may "OpenSSL" appear in their names without prior written
87109998Smarkm *    permission of the OpenSSL Project.
88109998Smarkm *
89109998Smarkm * 6. Redistributions of any form whatsoever must retain the following
90109998Smarkm *    acknowledgment:
91109998Smarkm *    "This product includes software developed by the OpenSSL Project
92109998Smarkm *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
93109998Smarkm *
94109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
95109998Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
96109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
97109998Smarkm * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
98109998Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
99109998Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
100109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
101109998Smarkm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
102109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
103109998Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
104109998Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
105109998Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE.
106109998Smarkm * ====================================================================
107109998Smarkm *
108109998Smarkm * This product includes cryptographic software written by Eric Young
109109998Smarkm * (eay@cryptsoft.com).  This product includes software written by Tim
110109998Smarkm * Hudson (tjh@cryptsoft.com).
111109998Smarkm *
112109998Smarkm */
113160814Ssimon/* ====================================================================
114160814Ssimon * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
115160814Ssimon * ECC cipher suite support in OpenSSL originally developed by
116160814Ssimon * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
117160814Ssimon */
118238405Sjkim/* ====================================================================
119238405Sjkim * Copyright 2005 Nokia. All rights reserved.
120238405Sjkim *
121238405Sjkim * The portions of the attached software ("Contribution") is developed by
122238405Sjkim * Nokia Corporation and is licensed pursuant to the OpenSSL open source
123238405Sjkim * license.
124238405Sjkim *
125238405Sjkim * The Contribution, originally written by Mika Kousa and Pasi Eronen of
126238405Sjkim * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
127238405Sjkim * support (see RFC 4279) to OpenSSL.
128238405Sjkim *
129238405Sjkim * No patent licenses or other rights except those expressly stated in
130238405Sjkim * the OpenSSL open source license shall be deemed granted or received
131238405Sjkim * expressly, by implication, estoppel, or otherwise.
132238405Sjkim *
133238405Sjkim * No assurances are provided by Nokia that the Contribution does not
134238405Sjkim * infringe the patent or other intellectual property rights of any third
135238405Sjkim * party or that the license provides you with all the necessary rights
136238405Sjkim * to make use of the Contribution.
137238405Sjkim *
138238405Sjkim * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
139238405Sjkim * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
140238405Sjkim * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
141238405Sjkim * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
142238405Sjkim * OTHERWISE.
143238405Sjkim */
14455714Skris
145109998Smarkm#ifdef REF_CHECK
146109998Smarkm#  include <assert.h>
147109998Smarkm#endif
14855714Skris#include <stdio.h>
149109998Smarkm#include "ssl_locl.h"
150109998Smarkm#include "kssl_lcl.h"
15155714Skris#include <openssl/objects.h>
15255714Skris#include <openssl/lhash.h>
15359191Skris#include <openssl/x509v3.h>
154194206Ssimon#include <openssl/rand.h>
155194206Ssimon#include <openssl/ocsp.h>
156160814Ssimon#ifndef OPENSSL_NO_DH
157160814Ssimon#include <openssl/dh.h>
158160814Ssimon#endif
159194206Ssimon#ifndef OPENSSL_NO_ENGINE
160194206Ssimon#include <openssl/engine.h>
161194206Ssimon#endif
16255714Skris
16359191Skrisconst char *SSL_version_str=OPENSSL_VERSION_TEXT;
16455714Skris
165160814SsimonSSL3_ENC_METHOD ssl3_undef_enc_method={
16659191Skris	/* evil casts, but these functions are only called if there's a library bug */
16759191Skris	(int (*)(SSL *,int))ssl_undefined_function,
16859191Skris	(int (*)(SSL *, unsigned char *, int))ssl_undefined_function,
16955714Skris	ssl_undefined_function,
17059191Skris	(int (*)(SSL *, unsigned char *, unsigned char *, int))ssl_undefined_function,
17159191Skris	(int (*)(SSL*, int))ssl_undefined_function,
172238405Sjkim	(int (*)(SSL *,  const char*, int, unsigned char *))ssl_undefined_function,
173160814Ssimon	0,	/* finish_mac_length */
174238405Sjkim	(int (*)(SSL *, int, unsigned char *))ssl_undefined_function,
175160814Ssimon	NULL,	/* client_finished_label */
176160814Ssimon	0,	/* client_finished_label_len */
177160814Ssimon	NULL,	/* server_finished_label */
178160814Ssimon	0,	/* server_finished_label_len */
179238405Sjkim	(int (*)(int))ssl_undefined_function,
180238405Sjkim	(int (*)(SSL *, unsigned char *, size_t, const char *,
181238405Sjkim		 size_t, const unsigned char *, size_t,
182238405Sjkim		 int use_context)) ssl_undefined_function,
18355714Skris	};
18455714Skris
18555714Skrisint SSL_clear(SSL *s)
18655714Skris	{
18755714Skris
18855714Skris	if (s->method == NULL)
18955714Skris		{
19055714Skris		SSLerr(SSL_F_SSL_CLEAR,SSL_R_NO_METHOD_SPECIFIED);
19155714Skris		return(0);
19255714Skris		}
19355714Skris
194100928Snectar	if (ssl_clear_bad_session(s))
195100928Snectar		{
196100928Snectar		SSL_SESSION_free(s->session);
197100928Snectar		s->session=NULL;
198100928Snectar		}
199100928Snectar
20055714Skris	s->error=0;
20155714Skris	s->hit=0;
20255714Skris	s->shutdown=0;
20355714Skris
20459191Skris#if 0 /* Disabled since version 1.10 of this file (early return not
20559191Skris       * needed because SSL_clear is not called when doing renegotiation) */
20655714Skris	/* This is set if we are doing dynamic renegotiation so keep
20755714Skris	 * the old cipher.  It is sort of a SSL_clear_lite :-) */
208238405Sjkim	if (s->renegotiate) return(1);
20959191Skris#else
210238405Sjkim	if (s->renegotiate)
21159191Skris		{
212109998Smarkm		SSLerr(SSL_F_SSL_CLEAR,ERR_R_INTERNAL_ERROR);
21359191Skris		return 0;
21459191Skris		}
21555714Skris#endif
21655714Skris
21755714Skris	s->type=0;
21855714Skris
21955714Skris	s->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT);
22055714Skris
22155714Skris	s->version=s->method->version;
22255714Skris	s->client_version=s->version;
22355714Skris	s->rwstate=SSL_NOTHING;
22455714Skris	s->rstate=SSL_ST_READ_HEADER;
22572613Skris#if 0
22655714Skris	s->read_ahead=s->ctx->read_ahead;
22772613Skris#endif
22855714Skris
22955714Skris	if (s->init_buf != NULL)
23055714Skris		{
23155714Skris		BUF_MEM_free(s->init_buf);
23255714Skris		s->init_buf=NULL;
23355714Skris		}
23455714Skris
23555714Skris	ssl_clear_cipher_ctx(s);
236238405Sjkim	ssl_clear_hash_ctx(&s->read_hash);
237238405Sjkim	ssl_clear_hash_ctx(&s->write_hash);
23855714Skris
23955714Skris	s->first_packet=0;
24055714Skris
24155714Skris#if 1
24255714Skris	/* Check to see if we were changed into a different method, if
24355714Skris	 * so, revert back if we are not doing session-id reuse. */
24489837Skris	if (!s->in_handshake && (s->session == NULL) && (s->method != s->ctx->method))
24555714Skris		{
24655714Skris		s->method->ssl_free(s);
24755714Skris		s->method=s->ctx->method;
24855714Skris		if (!s->method->ssl_new(s))
24955714Skris			return(0);
25055714Skris		}
25155714Skris	else
25255714Skris#endif
25355714Skris		s->method->ssl_clear(s);
25455714Skris	return(1);
25555714Skris	}
25655714Skris
25755714Skris/** Used to change an SSL_CTXs default SSL method type */
258238405Sjkimint SSL_CTX_set_ssl_version(SSL_CTX *ctx,const SSL_METHOD *meth)
25955714Skris	{
26055714Skris	STACK_OF(SSL_CIPHER) *sk;
26155714Skris
26255714Skris	ctx->method=meth;
26355714Skris
26455714Skris	sk=ssl_create_cipher_list(ctx->method,&(ctx->cipher_list),
265238405Sjkim		&(ctx->cipher_list_by_id),
266238405Sjkim		meth->version == SSL2_VERSION ? "SSLv2" : SSL_DEFAULT_CIPHER_LIST);
26755714Skris	if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0))
26855714Skris		{
26955714Skris		SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION,SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
27055714Skris		return(0);
27155714Skris		}
27255714Skris	return(1);
27355714Skris	}
27455714Skris
27555714SkrisSSL *SSL_new(SSL_CTX *ctx)
27655714Skris	{
27755714Skris	SSL *s;
27855714Skris
27955714Skris	if (ctx == NULL)
28055714Skris		{
28155714Skris		SSLerr(SSL_F_SSL_NEW,SSL_R_NULL_SSL_CTX);
28255714Skris		return(NULL);
28355714Skris		}
28455714Skris	if (ctx->method == NULL)
28555714Skris		{
28655714Skris		SSLerr(SSL_F_SSL_NEW,SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
28755714Skris		return(NULL);
28855714Skris		}
28955714Skris
29068651Skris	s=(SSL *)OPENSSL_malloc(sizeof(SSL));
29155714Skris	if (s == NULL) goto err;
29255714Skris	memset(s,0,sizeof(SSL));
29355714Skris
294109998Smarkm#ifndef	OPENSSL_NO_KRB5
295109998Smarkm	s->kssl_ctx = kssl_ctx_new();
296109998Smarkm#endif	/* OPENSSL_NO_KRB5 */
297109998Smarkm
298109998Smarkm	s->options=ctx->options;
299109998Smarkm	s->mode=ctx->mode;
300109998Smarkm	s->max_cert_list=ctx->max_cert_list;
301109998Smarkm
30255714Skris	if (ctx->cert != NULL)
30355714Skris		{
30455714Skris		/* Earlier library versions used to copy the pointer to
30555714Skris		 * the CERT, not its contents; only when setting new
30655714Skris		 * parameters for the per-SSL copy, ssl_cert_new would be
30755714Skris		 * called (and the direct reference to the per-SSL_CTX
30855714Skris		 * settings would be lost, but those still were indirectly
30955714Skris		 * accessed for various purposes, and for that reason they
31055714Skris		 * used to be known as s->ctx->default_cert).
31155714Skris		 * Now we don't look at the SSL_CTX's CERT after having
31255714Skris		 * duplicated it once. */
31355714Skris
31455714Skris		s->cert = ssl_cert_dup(ctx->cert);
31555714Skris		if (s->cert == NULL)
31655714Skris			goto err;
31755714Skris		}
31855714Skris	else
31955714Skris		s->cert=NULL; /* Cannot really happen (see SSL_CTX_new) */
320109998Smarkm
321109998Smarkm	s->read_ahead=ctx->read_ahead;
322109998Smarkm	s->msg_callback=ctx->msg_callback;
323109998Smarkm	s->msg_callback_arg=ctx->msg_callback_arg;
324109998Smarkm	s->verify_mode=ctx->verify_mode;
325160814Ssimon#if 0
326109998Smarkm	s->verify_depth=ctx->verify_depth;
327160814Ssimon#endif
32855714Skris	s->sid_ctx_length=ctx->sid_ctx_length;
329109998Smarkm	OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx);
33055714Skris	memcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));
33155714Skris	s->verify_callback=ctx->default_verify_callback;
332109998Smarkm	s->generate_session_id=ctx->generate_session_id;
333160814Ssimon
334160814Ssimon	s->param = X509_VERIFY_PARAM_new();
335160814Ssimon	if (!s->param)
336160814Ssimon		goto err;
337160814Ssimon	X509_VERIFY_PARAM_inherit(s->param, ctx->param);
338160814Ssimon#if 0
33959191Skris	s->purpose = ctx->purpose;
34059191Skris	s->trust = ctx->trust;
341160814Ssimon#endif
342109998Smarkm	s->quiet_shutdown=ctx->quiet_shutdown;
343238405Sjkim	s->max_send_fragment = ctx->max_send_fragment;
344109998Smarkm
34555714Skris	CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
34655714Skris	s->ctx=ctx;
347194206Ssimon#ifndef OPENSSL_NO_TLSEXT
348194206Ssimon	s->tlsext_debug_cb = 0;
349194206Ssimon	s->tlsext_debug_arg = NULL;
350194206Ssimon	s->tlsext_ticket_expected = 0;
351194206Ssimon	s->tlsext_status_type = -1;
352194206Ssimon	s->tlsext_status_expected = 0;
353194206Ssimon	s->tlsext_ocsp_ids = NULL;
354194206Ssimon	s->tlsext_ocsp_exts = NULL;
355194206Ssimon	s->tlsext_ocsp_resp = NULL;
356194206Ssimon	s->tlsext_ocsp_resplen = -1;
357194206Ssimon	CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
358194206Ssimon	s->initial_ctx=ctx;
359238405Sjkim# ifndef OPENSSL_NO_NEXTPROTONEG
360238405Sjkim	s->next_proto_negotiated = NULL;
361238405Sjkim# endif
362194206Ssimon#endif
363238405Sjkim
36455714Skris	s->verify_result=X509_V_OK;
36555714Skris
36655714Skris	s->method=ctx->method;
36755714Skris
36855714Skris	if (!s->method->ssl_new(s))
36955714Skris		goto err;
37055714Skris
37155714Skris	s->references=1;
37255714Skris	s->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;
373109998Smarkm
37455714Skris	SSL_clear(s);
37555714Skris
376109998Smarkm	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
37755714Skris
378238405Sjkim#ifndef OPENSSL_NO_PSK
379238405Sjkim	s->psk_client_callback=ctx->psk_client_callback;
380238405Sjkim	s->psk_server_callback=ctx->psk_server_callback;
381238405Sjkim#endif
382238405Sjkim
38355714Skris	return(s);
38455714Skriserr:
38555714Skris	if (s != NULL)
38655714Skris		{
38755714Skris		if (s->cert != NULL)
38855714Skris			ssl_cert_free(s->cert);
38955714Skris		if (s->ctx != NULL)
39055714Skris			SSL_CTX_free(s->ctx); /* decrement reference count */
39168651Skris		OPENSSL_free(s);
39255714Skris		}
39355714Skris	SSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE);
39455714Skris	return(NULL);
39555714Skris	}
39655714Skris
39755714Skrisint SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx,
39855714Skris				   unsigned int sid_ctx_len)
39955714Skris    {
400109998Smarkm    if(sid_ctx_len > sizeof ctx->sid_ctx)
40155714Skris	{
40255714Skris	SSLerr(SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
40355714Skris	return 0;
40455714Skris	}
40555714Skris    ctx->sid_ctx_length=sid_ctx_len;
40655714Skris    memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len);
40755714Skris
40855714Skris    return 1;
40955714Skris    }
41055714Skris
41155714Skrisint SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx,
41255714Skris			       unsigned int sid_ctx_len)
41355714Skris    {
41455714Skris    if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
41555714Skris	{
41655714Skris	SSLerr(SSL_F_SSL_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
41755714Skris	return 0;
41855714Skris	}
41955714Skris    ssl->sid_ctx_length=sid_ctx_len;
42055714Skris    memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len);
42155714Skris
42255714Skris    return 1;
42355714Skris    }
42455714Skris
425109998Smarkmint SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
426109998Smarkm	{
427109998Smarkm	CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
428109998Smarkm	ctx->generate_session_id = cb;
429109998Smarkm	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
430109998Smarkm	return 1;
43159191Skris	}
432109998Smarkm
433109998Smarkmint SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
434109998Smarkm	{
435109998Smarkm	CRYPTO_w_lock(CRYPTO_LOCK_SSL);
436109998Smarkm	ssl->generate_session_id = cb;
437109998Smarkm	CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
43859191Skris	return 1;
439109998Smarkm	}
44059191Skris
441109998Smarkmint SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
442109998Smarkm				unsigned int id_len)
443109998Smarkm	{
444109998Smarkm	/* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
445109998Smarkm	 * we can "construct" a session to give us the desired check - ie. to
446109998Smarkm	 * find if there's a session in the hash table that would conflict with
447109998Smarkm	 * any new session built out of this id/id_len and the ssl_version in
448109998Smarkm	 * use by this SSL. */
449109998Smarkm	SSL_SESSION r, *p;
450109998Smarkm
451109998Smarkm	if(id_len > sizeof r.session_id)
45259191Skris		return 0;
453109998Smarkm
454109998Smarkm	r.ssl_version = ssl->version;
455109998Smarkm	r.session_id_length = id_len;
456109998Smarkm	memcpy(r.session_id, id, id_len);
457109998Smarkm	/* NB: SSLv2 always uses a fixed 16-byte session ID, so even if a
458109998Smarkm	 * callback is calling us to check the uniqueness of a shorter ID, it
459109998Smarkm	 * must be compared as a padded-out ID because that is what it will be
460109998Smarkm	 * converted to when the callback has finished choosing it. */
461109998Smarkm	if((r.ssl_version == SSL2_VERSION) &&
462109998Smarkm			(id_len < SSL2_SSL_SESSION_ID_LENGTH))
463109998Smarkm		{
464109998Smarkm		memset(r.session_id + id_len, 0,
465109998Smarkm			SSL2_SSL_SESSION_ID_LENGTH - id_len);
466109998Smarkm		r.session_id_length = SSL2_SSL_SESSION_ID_LENGTH;
467109998Smarkm		}
468109998Smarkm
469109998Smarkm	CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
470238405Sjkim	p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
471109998Smarkm	CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
472109998Smarkm	return (p != NULL);
47359191Skris	}
474109998Smarkm
475109998Smarkmint SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
476109998Smarkm	{
477160814Ssimon	return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
478109998Smarkm	}
479109998Smarkm
480109998Smarkmint SSL_set_purpose(SSL *s, int purpose)
481109998Smarkm	{
482160814Ssimon	return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
483109998Smarkm	}
484109998Smarkm
48559191Skrisint SSL_CTX_set_trust(SSL_CTX *s, int trust)
486109998Smarkm	{
487160814Ssimon	return X509_VERIFY_PARAM_set_trust(s->param, trust);
48859191Skris	}
48959191Skris
49059191Skrisint SSL_set_trust(SSL *s, int trust)
491109998Smarkm	{
492160814Ssimon	return X509_VERIFY_PARAM_set_trust(s->param, trust);
49359191Skris	}
49459191Skris
495238405Sjkimint SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
496238405Sjkim	{
497238405Sjkim	return X509_VERIFY_PARAM_set1(ctx->param, vpm);
498238405Sjkim	}
499238405Sjkim
500238405Sjkimint SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
501238405Sjkim	{
502238405Sjkim	return X509_VERIFY_PARAM_set1(ssl->param, vpm);
503238405Sjkim	}
504238405Sjkim
50555714Skrisvoid SSL_free(SSL *s)
50655714Skris	{
50755714Skris	int i;
50855714Skris
50955714Skris	if(s == NULL)
51055714Skris	    return;
51155714Skris
51255714Skris	i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);
51355714Skris#ifdef REF_PRINT
51455714Skris	REF_PRINT("SSL",s);
51555714Skris#endif
51655714Skris	if (i > 0) return;
51755714Skris#ifdef REF_CHECK
51855714Skris	if (i < 0)
51955714Skris		{
52055714Skris		fprintf(stderr,"SSL_free, bad reference count\n");
52155714Skris		abort(); /* ok */
52255714Skris		}
52355714Skris#endif
52455714Skris
525160814Ssimon	if (s->param)
526160814Ssimon		X509_VERIFY_PARAM_free(s->param);
527160814Ssimon
528109998Smarkm	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
52955714Skris
53055714Skris	if (s->bbio != NULL)
53155714Skris		{
53255714Skris		/* If the buffering BIO is in place, pop it off */
53355714Skris		if (s->bbio == s->wbio)
53455714Skris			{
53555714Skris			s->wbio=BIO_pop(s->wbio);
53655714Skris			}
53755714Skris		BIO_free(s->bbio);
53855714Skris		s->bbio=NULL;
53955714Skris		}
54055714Skris	if (s->rbio != NULL)
54155714Skris		BIO_free_all(s->rbio);
54255714Skris	if ((s->wbio != NULL) && (s->wbio != s->rbio))
54355714Skris		BIO_free_all(s->wbio);
54455714Skris
54555714Skris	if (s->init_buf != NULL) BUF_MEM_free(s->init_buf);
54655714Skris
54755714Skris	/* add extra stuff */
54855714Skris	if (s->cipher_list != NULL) sk_SSL_CIPHER_free(s->cipher_list);
54955714Skris	if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);
55055714Skris
55155714Skris	/* Make the next call work :-) */
55255714Skris	if (s->session != NULL)
55355714Skris		{
55455714Skris		ssl_clear_bad_session(s);
55555714Skris		SSL_SESSION_free(s->session);
55655714Skris		}
55755714Skris
55855714Skris	ssl_clear_cipher_ctx(s);
559238405Sjkim	ssl_clear_hash_ctx(&s->read_hash);
560238405Sjkim	ssl_clear_hash_ctx(&s->write_hash);
56155714Skris
56255714Skris	if (s->cert != NULL) ssl_cert_free(s->cert);
56355714Skris	/* Free up if allocated */
56455714Skris
565194206Ssimon#ifndef OPENSSL_NO_TLSEXT
566194206Ssimon	if (s->tlsext_hostname)
567194206Ssimon		OPENSSL_free(s->tlsext_hostname);
568194206Ssimon	if (s->initial_ctx) SSL_CTX_free(s->initial_ctx);
569238405Sjkim#ifndef OPENSSL_NO_EC
570238405Sjkim	if (s->tlsext_ecpointformatlist) OPENSSL_free(s->tlsext_ecpointformatlist);
571238405Sjkim	if (s->tlsext_ellipticcurvelist) OPENSSL_free(s->tlsext_ellipticcurvelist);
572238405Sjkim#endif /* OPENSSL_NO_EC */
573238405Sjkim	if (s->tlsext_opaque_prf_input) OPENSSL_free(s->tlsext_opaque_prf_input);
574194206Ssimon	if (s->tlsext_ocsp_exts)
575194206Ssimon		sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
576194206Ssimon						X509_EXTENSION_free);
577194206Ssimon	if (s->tlsext_ocsp_ids)
578194206Ssimon		sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free);
579194206Ssimon	if (s->tlsext_ocsp_resp)
580194206Ssimon		OPENSSL_free(s->tlsext_ocsp_resp);
581194206Ssimon#endif
582238405Sjkim
58355714Skris	if (s->client_CA != NULL)
58455714Skris		sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);
58555714Skris
58655714Skris	if (s->method != NULL) s->method->ssl_free(s);
58755714Skris
588205128Ssimon	if (s->ctx) SSL_CTX_free(s->ctx);
589205128Ssimon
590120631Snectar#ifndef	OPENSSL_NO_KRB5
591120631Snectar	if (s->kssl_ctx != NULL)
592120631Snectar		kssl_ctx_free(s->kssl_ctx);
593120631Snectar#endif	/* OPENSSL_NO_KRB5 */
594120631Snectar
595238405Sjkim#if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
596238405Sjkim	if (s->next_proto_negotiated)
597238405Sjkim		OPENSSL_free(s->next_proto_negotiated);
598238405Sjkim#endif
599238405Sjkim
600246772Sjkim#ifndef OPENSSL_NO_SRTP
601238405Sjkim        if (s->srtp_profiles)
602238405Sjkim            sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
603246772Sjkim#endif
604238405Sjkim
60568651Skris	OPENSSL_free(s);
60655714Skris	}
60755714Skris
60855714Skrisvoid SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio)
60955714Skris	{
61055714Skris	/* If the output buffering BIO is still in place, remove it
61155714Skris	 */
61255714Skris	if (s->bbio != NULL)
61355714Skris		{
61455714Skris		if (s->wbio == s->bbio)
61555714Skris			{
61655714Skris			s->wbio=s->wbio->next_bio;
61755714Skris			s->bbio->next_bio=NULL;
61855714Skris			}
61955714Skris		}
62055714Skris	if ((s->rbio != NULL) && (s->rbio != rbio))
62155714Skris		BIO_free_all(s->rbio);
62255714Skris	if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
62355714Skris		BIO_free_all(s->wbio);
62455714Skris	s->rbio=rbio;
62555714Skris	s->wbio=wbio;
62655714Skris	}
62755714Skris
628160814SsimonBIO *SSL_get_rbio(const SSL *s)
62955714Skris	{ return(s->rbio); }
63055714Skris
631160814SsimonBIO *SSL_get_wbio(const SSL *s)
63255714Skris	{ return(s->wbio); }
63355714Skris
634160814Ssimonint SSL_get_fd(const SSL *s)
63555714Skris	{
63689837Skris	return(SSL_get_rfd(s));
63789837Skris	}
63889837Skris
639160814Ssimonint SSL_get_rfd(const SSL *s)
64089837Skris	{
64155714Skris	int ret= -1;
64255714Skris	BIO *b,*r;
64355714Skris
64455714Skris	b=SSL_get_rbio(s);
64555714Skris	r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
64655714Skris	if (r != NULL)
64755714Skris		BIO_get_fd(r,&ret);
64855714Skris	return(ret);
64955714Skris	}
65055714Skris
651160814Ssimonint SSL_get_wfd(const SSL *s)
65289837Skris	{
65389837Skris	int ret= -1;
65489837Skris	BIO *b,*r;
65589837Skris
65689837Skris	b=SSL_get_wbio(s);
65789837Skris	r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
65889837Skris	if (r != NULL)
65989837Skris		BIO_get_fd(r,&ret);
66089837Skris	return(ret);
66189837Skris	}
66289837Skris
663109998Smarkm#ifndef OPENSSL_NO_SOCK
66455714Skrisint SSL_set_fd(SSL *s,int fd)
66555714Skris	{
66655714Skris	int ret=0;
66755714Skris	BIO *bio=NULL;
66855714Skris
66955714Skris	bio=BIO_new(BIO_s_socket());
67055714Skris
67155714Skris	if (bio == NULL)
67255714Skris		{
67355714Skris		SSLerr(SSL_F_SSL_SET_FD,ERR_R_BUF_LIB);
67455714Skris		goto err;
67555714Skris		}
67655714Skris	BIO_set_fd(bio,fd,BIO_NOCLOSE);
67755714Skris	SSL_set_bio(s,bio,bio);
67855714Skris	ret=1;
67955714Skriserr:
68055714Skris	return(ret);
68155714Skris	}
68255714Skris
68355714Skrisint SSL_set_wfd(SSL *s,int fd)
68455714Skris	{
68555714Skris	int ret=0;
68655714Skris	BIO *bio=NULL;
68755714Skris
68855714Skris	if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET)
68955714Skris		|| ((int)BIO_get_fd(s->rbio,NULL) != fd))
69055714Skris		{
69155714Skris		bio=BIO_new(BIO_s_socket());
69255714Skris
69355714Skris		if (bio == NULL)
69455714Skris			{ SSLerr(SSL_F_SSL_SET_WFD,ERR_R_BUF_LIB); goto err; }
69555714Skris		BIO_set_fd(bio,fd,BIO_NOCLOSE);
69655714Skris		SSL_set_bio(s,SSL_get_rbio(s),bio);
69755714Skris		}
69855714Skris	else
69955714Skris		SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s));
70055714Skris	ret=1;
70155714Skriserr:
70255714Skris	return(ret);
70355714Skris	}
70455714Skris
70555714Skrisint SSL_set_rfd(SSL *s,int fd)
70655714Skris	{
70755714Skris	int ret=0;
70855714Skris	BIO *bio=NULL;
70955714Skris
71055714Skris	if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET)
71155714Skris		|| ((int)BIO_get_fd(s->wbio,NULL) != fd))
71255714Skris		{
71355714Skris		bio=BIO_new(BIO_s_socket());
71455714Skris
71555714Skris		if (bio == NULL)
71655714Skris			{
71755714Skris			SSLerr(SSL_F_SSL_SET_RFD,ERR_R_BUF_LIB);
71855714Skris			goto err;
71955714Skris			}
72055714Skris		BIO_set_fd(bio,fd,BIO_NOCLOSE);
72155714Skris		SSL_set_bio(s,bio,SSL_get_wbio(s));
72255714Skris		}
72355714Skris	else
72455714Skris		SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s));
72555714Skris	ret=1;
72655714Skriserr:
72755714Skris	return(ret);
72855714Skris	}
72955714Skris#endif
73055714Skris
73159191Skris
73259191Skris/* return length of latest Finished message we sent, copy to 'buf' */
733160814Ssimonsize_t SSL_get_finished(const SSL *s, void *buf, size_t count)
73459191Skris	{
73559191Skris	size_t ret = 0;
73659191Skris
73759191Skris	if (s->s3 != NULL)
73859191Skris		{
73959191Skris		ret = s->s3->tmp.finish_md_len;
74059191Skris		if (count > ret)
74159191Skris			count = ret;
74259191Skris		memcpy(buf, s->s3->tmp.finish_md, count);
74359191Skris		}
74459191Skris	return ret;
74559191Skris	}
74659191Skris
74759191Skris/* return length of latest Finished message we expected, copy to 'buf' */
748160814Ssimonsize_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
74959191Skris	{
75059191Skris	size_t ret = 0;
75159191Skris
75259191Skris	if (s->s3 != NULL)
75359191Skris		{
75459191Skris		ret = s->s3->tmp.peer_finish_md_len;
75559191Skris		if (count > ret)
75659191Skris			count = ret;
75759191Skris		memcpy(buf, s->s3->tmp.peer_finish_md, count);
75859191Skris		}
75959191Skris	return ret;
76059191Skris	}
76159191Skris
76259191Skris
763160814Ssimonint SSL_get_verify_mode(const SSL *s)
76455714Skris	{
76555714Skris	return(s->verify_mode);
76655714Skris	}
76755714Skris
768160814Ssimonint SSL_get_verify_depth(const SSL *s)
76955714Skris	{
770160814Ssimon	return X509_VERIFY_PARAM_get_depth(s->param);
77155714Skris	}
77255714Skris
773160814Ssimonint (*SSL_get_verify_callback(const SSL *s))(int,X509_STORE_CTX *)
77455714Skris	{
77555714Skris	return(s->verify_callback);
77655714Skris	}
77755714Skris
778160814Ssimonint SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
77955714Skris	{
78055714Skris	return(ctx->verify_mode);
78155714Skris	}
78255714Skris
783160814Ssimonint SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
78455714Skris	{
785160814Ssimon	return X509_VERIFY_PARAM_get_depth(ctx->param);
78655714Skris	}
78755714Skris
788160814Ssimonint (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int,X509_STORE_CTX *)
78955714Skris	{
79055714Skris	return(ctx->default_verify_callback);
79155714Skris	}
79255714Skris
79355714Skrisvoid SSL_set_verify(SSL *s,int mode,
79455714Skris		    int (*callback)(int ok,X509_STORE_CTX *ctx))
79555714Skris	{
79655714Skris	s->verify_mode=mode;
79755714Skris	if (callback != NULL)
79855714Skris		s->verify_callback=callback;
79955714Skris	}
80055714Skris
80155714Skrisvoid SSL_set_verify_depth(SSL *s,int depth)
80255714Skris	{
803160814Ssimon	X509_VERIFY_PARAM_set_depth(s->param, depth);
80455714Skris	}
80555714Skris
80655714Skrisvoid SSL_set_read_ahead(SSL *s,int yes)
80755714Skris	{
80855714Skris	s->read_ahead=yes;
80955714Skris	}
81055714Skris
811160814Ssimonint SSL_get_read_ahead(const SSL *s)
81255714Skris	{
81355714Skris	return(s->read_ahead);
81455714Skris	}
81555714Skris
816160814Ssimonint SSL_pending(const SSL *s)
81755714Skris	{
818109998Smarkm	/* SSL_pending cannot work properly if read-ahead is enabled
819109998Smarkm	 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)),
820109998Smarkm	 * and it is impossible to fix since SSL_pending cannot report
821109998Smarkm	 * errors that may be observed while scanning the new data.
822109998Smarkm	 * (Note that SSL_pending() is often used as a boolean value,
823109998Smarkm	 * so we'd better not return -1.)
824109998Smarkm	 */
82555714Skris	return(s->method->ssl_pending(s));
82655714Skris	}
82755714Skris
828160814SsimonX509 *SSL_get_peer_certificate(const SSL *s)
82955714Skris	{
83055714Skris	X509 *r;
83155714Skris
83255714Skris	if ((s == NULL) || (s->session == NULL))
83355714Skris		r=NULL;
83455714Skris	else
83555714Skris		r=s->session->peer;
83655714Skris
83755714Skris	if (r == NULL) return(r);
83855714Skris
83955714Skris	CRYPTO_add(&r->references,1,CRYPTO_LOCK_X509);
84055714Skris
84155714Skris	return(r);
84255714Skris	}
84355714Skris
844160814SsimonSTACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
84555714Skris	{
84655714Skris	STACK_OF(X509) *r;
84755714Skris
84855714Skris	if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL))
84955714Skris		r=NULL;
85055714Skris	else
85155714Skris		r=s->session->sess_cert->cert_chain;
85255714Skris
85359191Skris	/* If we are a client, cert_chain includes the peer's own
85459191Skris	 * certificate; if we are a server, it does not. */
85559191Skris
85655714Skris	return(r);
85755714Skris	}
85855714Skris
85955714Skris/* Now in theory, since the calling process own 't' it should be safe to
86055714Skris * modify.  We need to be able to read f without being hassled */
861160814Ssimonvoid SSL_copy_session_id(SSL *t,const SSL *f)
86255714Skris	{
86355714Skris	CERT *tmp;
86455714Skris
86555714Skris	/* Do we need to to SSL locking? */
86655714Skris	SSL_set_session(t,SSL_get_session(f));
86755714Skris
86855714Skris	/* what if we are setup as SSLv2 but want to talk SSLv3 or
86955714Skris	 * vice-versa */
87055714Skris	if (t->method != f->method)
87155714Skris		{
87255714Skris		t->method->ssl_free(t);	/* cleanup current */
87355714Skris		t->method=f->method;	/* change method */
87455714Skris		t->method->ssl_new(t);	/* setup new */
87555714Skris		}
87655714Skris
87755714Skris	tmp=t->cert;
87855714Skris	if (f->cert != NULL)
87955714Skris		{
88055714Skris		CRYPTO_add(&f->cert->references,1,CRYPTO_LOCK_SSL_CERT);
88155714Skris		t->cert=f->cert;
88255714Skris		}
88355714Skris	else
88455714Skris		t->cert=NULL;
88555714Skris	if (tmp != NULL) ssl_cert_free(tmp);
88655714Skris	SSL_set_session_id_context(t,f->sid_ctx,f->sid_ctx_length);
88755714Skris	}
88855714Skris
88955714Skris/* Fix this so it checks all the valid key/cert options */
890160814Ssimonint SSL_CTX_check_private_key(const SSL_CTX *ctx)
89155714Skris	{
89255714Skris	if (	(ctx == NULL) ||
89355714Skris		(ctx->cert == NULL) ||
89455714Skris		(ctx->cert->key->x509 == NULL))
89555714Skris		{
89655714Skris		SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
89755714Skris		return(0);
89855714Skris		}
89955714Skris	if 	(ctx->cert->key->privatekey == NULL)
90055714Skris		{
90155714Skris		SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED);
90255714Skris		return(0);
90355714Skris		}
90455714Skris	return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey));
90555714Skris	}
90655714Skris
90755714Skris/* Fix this function so that it takes an optional type parameter */
908160814Ssimonint SSL_check_private_key(const SSL *ssl)
90955714Skris	{
91055714Skris	if (ssl == NULL)
91155714Skris		{
91255714Skris		SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,ERR_R_PASSED_NULL_PARAMETER);
91355714Skris		return(0);
91455714Skris		}
91555714Skris	if (ssl->cert == NULL)
91655714Skris		{
917238405Sjkim		SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
91855714Skris		return 0;
91955714Skris		}
92055714Skris	if (ssl->cert->key->x509 == NULL)
92155714Skris		{
92255714Skris		SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
92355714Skris		return(0);
92455714Skris		}
92555714Skris	if (ssl->cert->key->privatekey == NULL)
92655714Skris		{
92755714Skris		SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED);
92855714Skris		return(0);
92955714Skris		}
93055714Skris	return(X509_check_private_key(ssl->cert->key->x509,
93155714Skris		ssl->cert->key->privatekey));
93255714Skris	}
93355714Skris
93455714Skrisint SSL_accept(SSL *s)
93555714Skris	{
93655714Skris	if (s->handshake_func == 0)
93755714Skris		/* Not properly initialized yet */
93855714Skris		SSL_set_accept_state(s);
93955714Skris
94055714Skris	return(s->method->ssl_accept(s));
94155714Skris	}
94255714Skris
94355714Skrisint SSL_connect(SSL *s)
94455714Skris	{
94555714Skris	if (s->handshake_func == 0)
94655714Skris		/* Not properly initialized yet */
94755714Skris		SSL_set_connect_state(s);
94855714Skris
94955714Skris	return(s->method->ssl_connect(s));
95055714Skris	}
95155714Skris
952160814Ssimonlong SSL_get_default_timeout(const SSL *s)
95355714Skris	{
95455714Skris	return(s->method->get_timeout());
95555714Skris	}
95655714Skris
95776866Skrisint SSL_read(SSL *s,void *buf,int num)
95855714Skris	{
95955714Skris	if (s->handshake_func == 0)
96055714Skris		{
96155714Skris		SSLerr(SSL_F_SSL_READ, SSL_R_UNINITIALIZED);
96255714Skris		return -1;
96355714Skris		}
96455714Skris
96555714Skris	if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
96655714Skris		{
96755714Skris		s->rwstate=SSL_NOTHING;
96855714Skris		return(0);
96955714Skris		}
97055714Skris	return(s->method->ssl_read(s,buf,num));
97155714Skris	}
97255714Skris
97376866Skrisint SSL_peek(SSL *s,void *buf,int num)
97455714Skris	{
97576866Skris	if (s->handshake_func == 0)
97676866Skris		{
977160814Ssimon		SSLerr(SSL_F_SSL_PEEK, SSL_R_UNINITIALIZED);
97876866Skris		return -1;
97976866Skris		}
98076866Skris
98155714Skris	if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
98255714Skris		{
98355714Skris		return(0);
98455714Skris		}
98555714Skris	return(s->method->ssl_peek(s,buf,num));
98655714Skris	}
98755714Skris
98876866Skrisint SSL_write(SSL *s,const void *buf,int num)
98955714Skris	{
99055714Skris	if (s->handshake_func == 0)
99155714Skris		{
99255714Skris		SSLerr(SSL_F_SSL_WRITE, SSL_R_UNINITIALIZED);
99355714Skris		return -1;
99455714Skris		}
99555714Skris
99655714Skris	if (s->shutdown & SSL_SENT_SHUTDOWN)
99755714Skris		{
99855714Skris		s->rwstate=SSL_NOTHING;
99955714Skris		SSLerr(SSL_F_SSL_WRITE,SSL_R_PROTOCOL_IS_SHUTDOWN);
100055714Skris		return(-1);
100155714Skris		}
100255714Skris	return(s->method->ssl_write(s,buf,num));
100355714Skris	}
100455714Skris
100555714Skrisint SSL_shutdown(SSL *s)
100655714Skris	{
100755714Skris	/* Note that this function behaves differently from what one might
100855714Skris	 * expect.  Return values are 0 for no success (yet),
100955714Skris	 * 1 for success; but calling it once is usually not enough,
101055714Skris	 * even if blocking I/O is used (see ssl3_shutdown).
101155714Skris	 */
101255714Skris
101355714Skris	if (s->handshake_func == 0)
101455714Skris		{
101555714Skris		SSLerr(SSL_F_SSL_SHUTDOWN, SSL_R_UNINITIALIZED);
101655714Skris		return -1;
101755714Skris		}
101855714Skris
101955714Skris	if ((s != NULL) && !SSL_in_init(s))
102055714Skris		return(s->method->ssl_shutdown(s));
102155714Skris	else
102255714Skris		return(1);
102355714Skris	}
102455714Skris
102555714Skrisint SSL_renegotiate(SSL *s)
102655714Skris	{
1027238405Sjkim	if (s->renegotiate == 0)
1028238405Sjkim		s->renegotiate=1;
1029238405Sjkim
1030238405Sjkim	s->new_session=1;
1031238405Sjkim
103255714Skris	return(s->method->ssl_renegotiate(s));
103355714Skris	}
103455714Skris
1035238405Sjkimint SSL_renegotiate_abbreviated(SSL *s)
1036238405Sjkim	{
1037238405Sjkim	if (s->renegotiate == 0)
1038238405Sjkim		s->renegotiate=1;
1039238405Sjkim
1040238405Sjkim	s->new_session=0;
1041238405Sjkim
1042238405Sjkim	return(s->method->ssl_renegotiate(s));
1043238405Sjkim	}
1044238405Sjkim
1045109998Smarkmint SSL_renegotiate_pending(SSL *s)
104655714Skris	{
1047109998Smarkm	/* becomes true when negotiation is requested;
1048109998Smarkm	 * false again once a handshake has finished */
1049238405Sjkim	return (s->renegotiate != 0);
1050109998Smarkm	}
1051109998Smarkm
1052109998Smarkmlong SSL_ctrl(SSL *s,int cmd,long larg,void *parg)
1053109998Smarkm	{
105455714Skris	long l;
105555714Skris
105655714Skris	switch (cmd)
105755714Skris		{
105855714Skris	case SSL_CTRL_GET_READ_AHEAD:
105955714Skris		return(s->read_ahead);
106055714Skris	case SSL_CTRL_SET_READ_AHEAD:
106155714Skris		l=s->read_ahead;
106255714Skris		s->read_ahead=larg;
106355714Skris		return(l);
1064109998Smarkm
1065109998Smarkm	case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1066109998Smarkm		s->msg_callback_arg = parg;
1067109998Smarkm		return 1;
1068109998Smarkm
106955714Skris	case SSL_CTRL_OPTIONS:
107055714Skris		return(s->options|=larg);
1071205128Ssimon	case SSL_CTRL_CLEAR_OPTIONS:
1072205128Ssimon		return(s->options&=~larg);
107355714Skris	case SSL_CTRL_MODE:
107455714Skris		return(s->mode|=larg);
1075205128Ssimon	case SSL_CTRL_CLEAR_MODE:
1076205128Ssimon		return(s->mode &=~larg);
1077109998Smarkm	case SSL_CTRL_GET_MAX_CERT_LIST:
1078109998Smarkm		return(s->max_cert_list);
1079109998Smarkm	case SSL_CTRL_SET_MAX_CERT_LIST:
1080109998Smarkm		l=s->max_cert_list;
1081109998Smarkm		s->max_cert_list=larg;
1082109998Smarkm		return(l);
1083160814Ssimon	case SSL_CTRL_SET_MTU:
1084237657Sjkim#ifndef OPENSSL_NO_DTLS1
1085237657Sjkim		if (larg < (long)dtls1_min_mtu())
1086237657Sjkim			return 0;
1087237657Sjkim#endif
1088237657Sjkim
1089205128Ssimon		if (SSL_version(s) == DTLS1_VERSION ||
1090205128Ssimon		    SSL_version(s) == DTLS1_BAD_VER)
1091160814Ssimon			{
1092160814Ssimon			s->d1->mtu = larg;
1093160814Ssimon			return larg;
1094160814Ssimon			}
1095160814Ssimon		return 0;
1096238405Sjkim	case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1097238405Sjkim		if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1098238405Sjkim			return 0;
1099238405Sjkim		s->max_send_fragment = larg;
1100238405Sjkim		return 1;
1101205128Ssimon	case SSL_CTRL_GET_RI_SUPPORT:
1102205128Ssimon		if (s->s3)
1103205128Ssimon			return s->s3->send_connection_binding;
1104205128Ssimon		else return 0;
110555714Skris	default:
110655714Skris		return(s->method->ssl_ctrl(s,cmd,larg,parg));
110755714Skris		}
110855714Skris	}
110955714Skris
1110160814Ssimonlong SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
111159191Skris	{
111259191Skris	switch(cmd)
111359191Skris		{
1114109998Smarkm	case SSL_CTRL_SET_MSG_CALLBACK:
1115109998Smarkm		s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1116109998Smarkm		return 1;
1117109998Smarkm
111859191Skris	default:
111959191Skris		return(s->method->ssl_callback_ctrl(s,cmd,fp));
112059191Skris		}
112159191Skris	}
112259191Skris
1123238405SjkimLHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
112459191Skris	{
112559191Skris	return ctx->sessions;
112659191Skris	}
112759191Skris
1128109998Smarkmlong SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg)
112955714Skris	{
113055714Skris	long l;
113155714Skris
113255714Skris	switch (cmd)
113355714Skris		{
113455714Skris	case SSL_CTRL_GET_READ_AHEAD:
113555714Skris		return(ctx->read_ahead);
113655714Skris	case SSL_CTRL_SET_READ_AHEAD:
113755714Skris		l=ctx->read_ahead;
113855714Skris		ctx->read_ahead=larg;
113955714Skris		return(l);
1140109998Smarkm
1141109998Smarkm	case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1142109998Smarkm		ctx->msg_callback_arg = parg;
1143109998Smarkm		return 1;
114455714Skris
1145109998Smarkm	case SSL_CTRL_GET_MAX_CERT_LIST:
1146109998Smarkm		return(ctx->max_cert_list);
1147109998Smarkm	case SSL_CTRL_SET_MAX_CERT_LIST:
1148109998Smarkm		l=ctx->max_cert_list;
1149109998Smarkm		ctx->max_cert_list=larg;
1150109998Smarkm		return(l);
1151109998Smarkm
115255714Skris	case SSL_CTRL_SET_SESS_CACHE_SIZE:
115355714Skris		l=ctx->session_cache_size;
115455714Skris		ctx->session_cache_size=larg;
115555714Skris		return(l);
115655714Skris	case SSL_CTRL_GET_SESS_CACHE_SIZE:
115755714Skris		return(ctx->session_cache_size);
115855714Skris	case SSL_CTRL_SET_SESS_CACHE_MODE:
115955714Skris		l=ctx->session_cache_mode;
116055714Skris		ctx->session_cache_mode=larg;
116155714Skris		return(l);
116255714Skris	case SSL_CTRL_GET_SESS_CACHE_MODE:
116355714Skris		return(ctx->session_cache_mode);
116455714Skris
116555714Skris	case SSL_CTRL_SESS_NUMBER:
1166238405Sjkim		return(lh_SSL_SESSION_num_items(ctx->sessions));
116755714Skris	case SSL_CTRL_SESS_CONNECT:
116855714Skris		return(ctx->stats.sess_connect);
116955714Skris	case SSL_CTRL_SESS_CONNECT_GOOD:
117055714Skris		return(ctx->stats.sess_connect_good);
117155714Skris	case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
117255714Skris		return(ctx->stats.sess_connect_renegotiate);
117355714Skris	case SSL_CTRL_SESS_ACCEPT:
117455714Skris		return(ctx->stats.sess_accept);
117555714Skris	case SSL_CTRL_SESS_ACCEPT_GOOD:
117655714Skris		return(ctx->stats.sess_accept_good);
117755714Skris	case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
117855714Skris		return(ctx->stats.sess_accept_renegotiate);
117955714Skris	case SSL_CTRL_SESS_HIT:
118055714Skris		return(ctx->stats.sess_hit);
118155714Skris	case SSL_CTRL_SESS_CB_HIT:
118255714Skris		return(ctx->stats.sess_cb_hit);
118355714Skris	case SSL_CTRL_SESS_MISSES:
118455714Skris		return(ctx->stats.sess_miss);
118555714Skris	case SSL_CTRL_SESS_TIMEOUTS:
118655714Skris		return(ctx->stats.sess_timeout);
118755714Skris	case SSL_CTRL_SESS_CACHE_FULL:
118855714Skris		return(ctx->stats.sess_cache_full);
118955714Skris	case SSL_CTRL_OPTIONS:
119055714Skris		return(ctx->options|=larg);
1191205128Ssimon	case SSL_CTRL_CLEAR_OPTIONS:
1192205128Ssimon		return(ctx->options&=~larg);
119355714Skris	case SSL_CTRL_MODE:
119455714Skris		return(ctx->mode|=larg);
1195205128Ssimon	case SSL_CTRL_CLEAR_MODE:
1196205128Ssimon		return(ctx->mode&=~larg);
1197238405Sjkim	case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1198238405Sjkim		if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1199238405Sjkim			return 0;
1200238405Sjkim		ctx->max_send_fragment = larg;
1201238405Sjkim		return 1;
120255714Skris	default:
120355714Skris		return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
120455714Skris		}
120555714Skris	}
120655714Skris
1207160814Ssimonlong SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
120859191Skris	{
120959191Skris	switch(cmd)
121059191Skris		{
1211109998Smarkm	case SSL_CTRL_SET_MSG_CALLBACK:
1212109998Smarkm		ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1213109998Smarkm		return 1;
1214109998Smarkm
121559191Skris	default:
121659191Skris		return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp));
121759191Skris		}
121859191Skris	}
121959191Skris
122068651Skrisint ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b)
122155714Skris	{
122255714Skris	long l;
122355714Skris
122455714Skris	l=a->id-b->id;
122555714Skris	if (l == 0L)
122655714Skris		return(0);
122755714Skris	else
122855714Skris		return((l > 0)?1:-1);
122955714Skris	}
123055714Skris
123168651Skrisint ssl_cipher_ptr_id_cmp(const SSL_CIPHER * const *ap,
123268651Skris			const SSL_CIPHER * const *bp)
123355714Skris	{
123455714Skris	long l;
123555714Skris
123655714Skris	l=(*ap)->id-(*bp)->id;
123755714Skris	if (l == 0L)
123855714Skris		return(0);
123955714Skris	else
124055714Skris		return((l > 0)?1:-1);
124155714Skris	}
124255714Skris
124355714Skris/** return a STACK of the ciphers available for the SSL and in order of
124455714Skris * preference */
1245160814SsimonSTACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
124655714Skris	{
1247111147Snectar	if (s != NULL)
124855714Skris		{
1249111147Snectar		if (s->cipher_list != NULL)
1250111147Snectar			{
1251111147Snectar			return(s->cipher_list);
1252111147Snectar			}
1253111147Snectar		else if ((s->ctx != NULL) &&
1254111147Snectar			(s->ctx->cipher_list != NULL))
1255111147Snectar			{
1256111147Snectar			return(s->ctx->cipher_list);
1257111147Snectar			}
125855714Skris		}
125955714Skris	return(NULL);
126055714Skris	}
126155714Skris
126255714Skris/** return a STACK of the ciphers available for the SSL and in order of
126355714Skris * algorithm id */
126455714SkrisSTACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
126555714Skris	{
1266111147Snectar	if (s != NULL)
126755714Skris		{
1268111147Snectar		if (s->cipher_list_by_id != NULL)
1269111147Snectar			{
1270111147Snectar			return(s->cipher_list_by_id);
1271111147Snectar			}
1272111147Snectar		else if ((s->ctx != NULL) &&
1273111147Snectar			(s->ctx->cipher_list_by_id != NULL))
1274111147Snectar			{
1275111147Snectar			return(s->ctx->cipher_list_by_id);
1276111147Snectar			}
127755714Skris		}
127855714Skris	return(NULL);
127955714Skris	}
128055714Skris
128155714Skris/** The old interface to get the same thing as SSL_get_ciphers() */
1282160814Ssimonconst char *SSL_get_cipher_list(const SSL *s,int n)
128355714Skris	{
128455714Skris	SSL_CIPHER *c;
128555714Skris	STACK_OF(SSL_CIPHER) *sk;
128655714Skris
128755714Skris	if (s == NULL) return(NULL);
128855714Skris	sk=SSL_get_ciphers(s);
128955714Skris	if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n))
129055714Skris		return(NULL);
129155714Skris	c=sk_SSL_CIPHER_value(sk,n);
129255714Skris	if (c == NULL) return(NULL);
129355714Skris	return(c->name);
129455714Skris	}
129555714Skris
129659191Skris/** specify the ciphers to be used by default by the SSL_CTX */
129759191Skrisint SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
129855714Skris	{
129955714Skris	STACK_OF(SSL_CIPHER) *sk;
130055714Skris
130155714Skris	sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
130255714Skris		&ctx->cipher_list_by_id,str);
1303160814Ssimon	/* ssl_create_cipher_list may return an empty stack if it
1304160814Ssimon	 * was unable to find a cipher matching the given rule string
1305160814Ssimon	 * (for example if the rule string specifies a cipher which
1306238405Sjkim	 * has been disabled). This is not an error as far as
1307238405Sjkim	 * ssl_create_cipher_list is concerned, and hence
1308160814Ssimon	 * ctx->cipher_list and ctx->cipher_list_by_id has been
1309160814Ssimon	 * updated. */
1310160814Ssimon	if (sk == NULL)
1311160814Ssimon		return 0;
1312160814Ssimon	else if (sk_SSL_CIPHER_num(sk) == 0)
1313160814Ssimon		{
1314160814Ssimon		SSLerr(SSL_F_SSL_CTX_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH);
1315160814Ssimon		return 0;
1316160814Ssimon		}
1317160814Ssimon	return 1;
131855714Skris	}
131955714Skris
132055714Skris/** specify the ciphers to be used by the SSL */
132159191Skrisint SSL_set_cipher_list(SSL *s,const char *str)
132255714Skris	{
132355714Skris	STACK_OF(SSL_CIPHER) *sk;
132455714Skris
132555714Skris	sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
132655714Skris		&s->cipher_list_by_id,str);
1327160814Ssimon	/* see comment in SSL_CTX_set_cipher_list */
1328160814Ssimon	if (sk == NULL)
1329160814Ssimon		return 0;
1330160814Ssimon	else if (sk_SSL_CIPHER_num(sk) == 0)
1331160814Ssimon		{
1332160814Ssimon		SSLerr(SSL_F_SSL_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH);
1333160814Ssimon		return 0;
1334160814Ssimon		}
1335160814Ssimon	return 1;
133655714Skris	}
133755714Skris
133855714Skris/* works well for SSLv2, not so good for SSLv3 */
1339160814Ssimonchar *SSL_get_shared_ciphers(const SSL *s,char *buf,int len)
134055714Skris	{
134155714Skris	char *p;
134255714Skris	STACK_OF(SSL_CIPHER) *sk;
134355714Skris	SSL_CIPHER *c;
134455714Skris	int i;
134555714Skris
134655714Skris	if ((s->session == NULL) || (s->session->ciphers == NULL) ||
134755714Skris		(len < 2))
134855714Skris		return(NULL);
134955714Skris
135055714Skris	p=buf;
135155714Skris	sk=s->session->ciphers;
1352267258Sjkim
1353267258Sjkim	if (sk_SSL_CIPHER_num(sk) == 0)
1354267258Sjkim		return NULL;
1355267258Sjkim
135655714Skris	for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
135755714Skris		{
1358172429Ssimon		int n;
1359172429Ssimon
136055714Skris		c=sk_SSL_CIPHER_value(sk,i);
1361172429Ssimon		n=strlen(c->name);
1362172429Ssimon		if (n+1 > len)
136355714Skris			{
1364172429Ssimon			if (p != buf)
1365172429Ssimon				--p;
1366172429Ssimon			*p='\0';
1367172429Ssimon			return buf;
136855714Skris			}
1369172429Ssimon		strcpy(p,c->name);
1370172429Ssimon		p+=n;
137155714Skris		*(p++)=':';
1372172429Ssimon		len-=n+1;
137355714Skris		}
137455714Skris	p[-1]='\0';
137555714Skris	return(buf);
137655714Skris	}
137755714Skris
1378160814Ssimonint ssl_cipher_list_to_bytes(SSL *s,STACK_OF(SSL_CIPHER) *sk,unsigned char *p,
1379238405Sjkim			     int (*put_cb)(const SSL_CIPHER *, unsigned char *))
138055714Skris	{
138155714Skris	int i,j=0;
138255714Skris	SSL_CIPHER *c;
138355714Skris	unsigned char *q;
1384109998Smarkm#ifndef OPENSSL_NO_KRB5
1385238405Sjkim	int nokrb5 = !kssl_tgt_is_available(s->kssl_ctx);
1386109998Smarkm#endif /* OPENSSL_NO_KRB5 */
138755714Skris
138855714Skris	if (sk == NULL) return(0);
138955714Skris	q=p;
139055714Skris
139155714Skris	for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
139255714Skris		{
139355714Skris		c=sk_SSL_CIPHER_value(sk,i);
1394238405Sjkim		/* Skip TLS v1.2 only ciphersuites if lower than v1.2 */
1395238405Sjkim		if ((c->algorithm_ssl & SSL_TLSV1_2) &&
1396238405Sjkim			(TLS1_get_client_version(s) < TLS1_2_VERSION))
1397238405Sjkim			continue;
1398109998Smarkm#ifndef OPENSSL_NO_KRB5
1399238405Sjkim		if (((c->algorithm_mkey & SSL_kKRB5) || (c->algorithm_auth & SSL_aKRB5)) &&
1400238405Sjkim		    nokrb5)
1401238405Sjkim		    continue;
1402238405Sjkim#endif /* OPENSSL_NO_KRB5 */
1403238405Sjkim#ifndef OPENSSL_NO_PSK
1404238405Sjkim		/* with PSK there must be client callback set */
1405238405Sjkim		if (((c->algorithm_mkey & SSL_kPSK) || (c->algorithm_auth & SSL_aPSK)) &&
1406238405Sjkim		    s->psk_client_callback == NULL)
1407238405Sjkim			continue;
1408238405Sjkim#endif /* OPENSSL_NO_PSK */
1409269686Sjkim#ifndef OPENSSL_NO_SRP
1410269686Sjkim		if (((c->algorithm_mkey & SSL_kSRP) || (c->algorithm_auth & SSL_aSRP)) &&
1411269686Sjkim		    !(s->srp_ctx.srp_Mask & SSL_kSRP))
1412269686Sjkim		    continue;
1413269686Sjkim#endif /* OPENSSL_NO_SRP */
1414160814Ssimon		j = put_cb ? put_cb(c,p) : ssl_put_cipher_by_char(s,c,p);
141555714Skris		p+=j;
141655714Skris		}
1417205128Ssimon	/* If p == q, no ciphers and caller indicates an error. Otherwise
1418205128Ssimon	 * add SCSV if not renegotiating.
1419205128Ssimon	 */
1420238405Sjkim	if (p != q && !s->renegotiate)
1421205128Ssimon		{
1422205128Ssimon		static SSL_CIPHER scsv =
1423205128Ssimon			{
1424238405Sjkim			0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
1425205128Ssimon			};
1426205128Ssimon		j = put_cb ? put_cb(&scsv,p) : ssl_put_cipher_by_char(s,&scsv,p);
1427205128Ssimon		p+=j;
1428205128Ssimon#ifdef OPENSSL_RI_DEBUG
1429205128Ssimon		fprintf(stderr, "SCSV sent by client\n");
1430205128Ssimon#endif
1431205128Ssimon		}
1432205128Ssimon
143355714Skris	return(p-q);
143455714Skris	}
143555714Skris
143655714SkrisSTACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,unsigned char *p,int num,
143755714Skris					       STACK_OF(SSL_CIPHER) **skp)
143855714Skris	{
1439238405Sjkim	const SSL_CIPHER *c;
144055714Skris	STACK_OF(SSL_CIPHER) *sk;
144155714Skris	int i,n;
1442205128Ssimon	if (s->s3)
1443205128Ssimon		s->s3->send_connection_binding = 0;
144455714Skris
144555714Skris	n=ssl_put_cipher_by_char(s,NULL,NULL);
144655714Skris	if ((num%n) != 0)
144755714Skris		{
144855714Skris		SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
144955714Skris		return(NULL);
145055714Skris		}
145155714Skris	if ((skp == NULL) || (*skp == NULL))
145268651Skris		sk=sk_SSL_CIPHER_new_null(); /* change perhaps later */
145355714Skris	else
145455714Skris		{
145555714Skris		sk= *skp;
145655714Skris		sk_SSL_CIPHER_zero(sk);
145755714Skris		}
145855714Skris
145955714Skris	for (i=0; i<num; i+=n)
146055714Skris		{
1461205128Ssimon		/* Check for SCSV */
1462205128Ssimon		if (s->s3 && (n != 3 || !p[0]) &&
1463205128Ssimon			(p[n-2] == ((SSL3_CK_SCSV >> 8) & 0xff)) &&
1464205128Ssimon			(p[n-1] == (SSL3_CK_SCSV & 0xff)))
1465205128Ssimon			{
1466205128Ssimon			/* SCSV fatal if renegotiating */
1467238405Sjkim			if (s->renegotiate)
1468205128Ssimon				{
1469205128Ssimon				SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1470205128Ssimon				ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1471205128Ssimon				goto err;
1472205128Ssimon				}
1473205128Ssimon			s->s3->send_connection_binding = 1;
1474205128Ssimon			p += n;
1475205128Ssimon#ifdef OPENSSL_RI_DEBUG
1476205128Ssimon			fprintf(stderr, "SCSV received by server\n");
1477205128Ssimon#endif
1478205128Ssimon			continue;
1479205128Ssimon			}
1480205128Ssimon
148155714Skris		c=ssl_get_cipher_by_char(s,p);
148255714Skris		p+=n;
148355714Skris		if (c != NULL)
148455714Skris			{
148555714Skris			if (!sk_SSL_CIPHER_push(sk,c))
148655714Skris				{
148755714Skris				SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
148855714Skris				goto err;
148955714Skris				}
149055714Skris			}
149155714Skris		}
149255714Skris
149355714Skris	if (skp != NULL)
149455714Skris		*skp=sk;
149555714Skris	return(sk);
149655714Skriserr:
149755714Skris	if ((skp == NULL) || (*skp == NULL))
149855714Skris		sk_SSL_CIPHER_free(sk);
149955714Skris	return(NULL);
150055714Skris	}
150155714Skris
1502238405Sjkim
1503194206Ssimon#ifndef OPENSSL_NO_TLSEXT
1504194206Ssimon/** return a servername extension value if provided in Client Hello, or NULL.
1505194206Ssimon * So far, only host_name types are defined (RFC 3546).
1506194206Ssimon */
1507194206Ssimon
1508194206Ssimonconst char *SSL_get_servername(const SSL *s, const int type)
1509194206Ssimon	{
1510194206Ssimon	if (type != TLSEXT_NAMETYPE_host_name)
1511194206Ssimon		return NULL;
1512194206Ssimon
1513194206Ssimon	return s->session && !s->tlsext_hostname ?
1514194206Ssimon		s->session->tlsext_hostname :
1515194206Ssimon		s->tlsext_hostname;
1516194206Ssimon	}
1517194206Ssimon
1518194206Ssimonint SSL_get_servername_type(const SSL *s)
1519194206Ssimon	{
1520194206Ssimon	if (s->session && (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname))
1521194206Ssimon		return TLSEXT_NAMETYPE_host_name;
1522194206Ssimon	return -1;
1523194206Ssimon	}
1524238405Sjkim
1525238405Sjkim# ifndef OPENSSL_NO_NEXTPROTONEG
1526238405Sjkim/* SSL_select_next_proto implements the standard protocol selection. It is
1527238405Sjkim * expected that this function is called from the callback set by
1528238405Sjkim * SSL_CTX_set_next_proto_select_cb.
1529238405Sjkim *
1530238405Sjkim * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1531238405Sjkim * strings. The length byte itself is not included in the length. A byte
1532238405Sjkim * string of length 0 is invalid. No byte string may be truncated.
1533238405Sjkim *
1534238405Sjkim * The current, but experimental algorithm for selecting the protocol is:
1535238405Sjkim *
1536238405Sjkim * 1) If the server doesn't support NPN then this is indicated to the
1537238405Sjkim * callback. In this case, the client application has to abort the connection
1538238405Sjkim * or have a default application level protocol.
1539238405Sjkim *
1540238405Sjkim * 2) If the server supports NPN, but advertises an empty list then the
1541238405Sjkim * client selects the first protcol in its list, but indicates via the
1542238405Sjkim * API that this fallback case was enacted.
1543238405Sjkim *
1544238405Sjkim * 3) Otherwise, the client finds the first protocol in the server's list
1545238405Sjkim * that it supports and selects this protocol. This is because it's
1546238405Sjkim * assumed that the server has better information about which protocol
1547238405Sjkim * a client should use.
1548238405Sjkim *
1549238405Sjkim * 4) If the client doesn't support any of the server's advertised
1550238405Sjkim * protocols, then this is treated the same as case 2.
1551238405Sjkim *
1552238405Sjkim * It returns either
1553238405Sjkim * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1554238405Sjkim * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1555238405Sjkim */
1556238405Sjkimint 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)
1557238405Sjkim	{
1558238405Sjkim	unsigned int i, j;
1559238405Sjkim	const unsigned char *result;
1560238405Sjkim	int status = OPENSSL_NPN_UNSUPPORTED;
1561238405Sjkim
1562238405Sjkim	/* For each protocol in server preference order, see if we support it. */
1563238405Sjkim	for (i = 0; i < server_len; )
1564238405Sjkim		{
1565238405Sjkim		for (j = 0; j < client_len; )
1566238405Sjkim			{
1567238405Sjkim			if (server[i] == client[j] &&
1568238405Sjkim			    memcmp(&server[i+1], &client[j+1], server[i]) == 0)
1569238405Sjkim				{
1570238405Sjkim				/* We found a match */
1571238405Sjkim				result = &server[i];
1572238405Sjkim				status = OPENSSL_NPN_NEGOTIATED;
1573238405Sjkim				goto found;
1574238405Sjkim				}
1575238405Sjkim			j += client[j];
1576238405Sjkim			j++;
1577238405Sjkim			}
1578238405Sjkim		i += server[i];
1579238405Sjkim		i++;
1580238405Sjkim		}
1581238405Sjkim
1582238405Sjkim	/* There's no overlap between our protocols and the server's list. */
1583238405Sjkim	result = client;
1584238405Sjkim	status = OPENSSL_NPN_NO_OVERLAP;
1585238405Sjkim
1586238405Sjkim	found:
1587238405Sjkim	*out = (unsigned char *) result + 1;
1588238405Sjkim	*outlen = result[0];
1589238405Sjkim	return status;
1590238405Sjkim	}
1591238405Sjkim
1592238405Sjkim/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1593238405Sjkim * requested protocol for this connection and returns 0. If the client didn't
1594238405Sjkim * request any protocol, then *data is set to NULL.
1595238405Sjkim *
1596238405Sjkim * Note that the client can request any protocol it chooses. The value returned
1597238405Sjkim * from this function need not be a member of the list of supported protocols
1598238405Sjkim * provided by the callback.
1599238405Sjkim */
1600238405Sjkimvoid SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data, unsigned *len)
1601238405Sjkim	{
1602238405Sjkim	*data = s->next_proto_negotiated;
1603238405Sjkim	if (!*data) {
1604238405Sjkim		*len = 0;
1605238405Sjkim	} else {
1606238405Sjkim		*len = s->next_proto_negotiated_len;
1607238405Sjkim	}
1608238405Sjkim}
1609238405Sjkim
1610238405Sjkim/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1611238405Sjkim * TLS server needs a list of supported protocols for Next Protocol
1612238405Sjkim * Negotiation. The returned list must be in wire format.  The list is returned
1613238405Sjkim * by setting |out| to point to it and |outlen| to its length. This memory will
1614238405Sjkim * not be modified, but one should assume that the SSL* keeps a reference to
1615238405Sjkim * it.
1616238405Sjkim *
1617238405Sjkim * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise. Otherwise, no
1618238405Sjkim * such extension will be included in the ServerHello. */
1619238405Sjkimvoid 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)
1620238405Sjkim	{
1621238405Sjkim	ctx->next_protos_advertised_cb = cb;
1622238405Sjkim	ctx->next_protos_advertised_cb_arg = arg;
1623238405Sjkim	}
1624238405Sjkim
1625238405Sjkim/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1626238405Sjkim * client needs to select a protocol from the server's provided list. |out|
1627238405Sjkim * must be set to point to the selected protocol (which may be within |in|).
1628238405Sjkim * The length of the protocol name must be written into |outlen|. The server's
1629238405Sjkim * advertised protocols are provided in |in| and |inlen|. The callback can
1630238405Sjkim * assume that |in| is syntactically valid.
1631238405Sjkim *
1632238405Sjkim * The client must select a protocol. It is fatal to the connection if this
1633238405Sjkim * callback returns a value other than SSL_TLSEXT_ERR_OK.
1634238405Sjkim */
1635238405Sjkimvoid 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)
1636238405Sjkim	{
1637238405Sjkim	ctx->next_proto_select_cb = cb;
1638238405Sjkim	ctx->next_proto_select_cb_arg = arg;
1639238405Sjkim	}
1640238405Sjkim# endif
1641194206Ssimon#endif
1642194206Ssimon
1643238405Sjkimint SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
1644238405Sjkim	const char *label, size_t llen, const unsigned char *p, size_t plen,
1645238405Sjkim	int use_context)
164655714Skris	{
1647238405Sjkim	if (s->version < TLS1_VERSION)
1648238405Sjkim		return -1;
1649238405Sjkim
1650238405Sjkim	return s->method->ssl3_enc->export_keying_material(s, out, olen, label,
1651238405Sjkim							   llen, p, plen,
1652238405Sjkim							   use_context);
1653238405Sjkim	}
1654238405Sjkim
1655238405Sjkimstatic unsigned long ssl_session_hash(const SSL_SESSION *a)
1656238405Sjkim	{
165755714Skris	unsigned long l;
165855714Skris
165955714Skris	l=(unsigned long)
166055714Skris		((unsigned int) a->session_id[0]     )|
166155714Skris		((unsigned int) a->session_id[1]<< 8L)|
166255714Skris		((unsigned long)a->session_id[2]<<16L)|
166355714Skris		((unsigned long)a->session_id[3]<<24L);
166455714Skris	return(l);
166555714Skris	}
166655714Skris
1667109998Smarkm/* NB: If this function (or indeed the hash function which uses a sort of
1668109998Smarkm * coarser function than this one) is changed, ensure
1669109998Smarkm * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1670109998Smarkm * able to construct an SSL_SESSION that will collide with any existing session
1671109998Smarkm * with a matching session ID. */
1672238405Sjkimstatic int ssl_session_cmp(const SSL_SESSION *a,const SSL_SESSION *b)
167355714Skris	{
167455714Skris	if (a->ssl_version != b->ssl_version)
167555714Skris		return(1);
167655714Skris	if (a->session_id_length != b->session_id_length)
167755714Skris		return(1);
167855714Skris	return(memcmp(a->session_id,b->session_id,a->session_id_length));
167955714Skris	}
168055714Skris
1681109998Smarkm/* These wrapper functions should remain rather than redeclaring
1682109998Smarkm * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
1683109998Smarkm * variable. The reason is that the functions aren't static, they're exposed via
1684109998Smarkm * ssl.h. */
1685238405Sjkimstatic IMPLEMENT_LHASH_HASH_FN(ssl_session, SSL_SESSION)
1686238405Sjkimstatic IMPLEMENT_LHASH_COMP_FN(ssl_session, SSL_SESSION)
1687109998Smarkm
1688238405SjkimSSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
168955714Skris	{
169055714Skris	SSL_CTX *ret=NULL;
1691238405Sjkim
169255714Skris	if (meth == NULL)
169355714Skris		{
169455714Skris		SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_NULL_SSL_METHOD_PASSED);
169555714Skris		return(NULL);
169655714Skris		}
169755714Skris
1698194206Ssimon#ifdef OPENSSL_FIPS
1699194206Ssimon	if (FIPS_mode() && (meth->version < TLS1_VERSION))
1700194206Ssimon		{
1701194206Ssimon		SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
1702194206Ssimon		return NULL;
1703194206Ssimon		}
1704194206Ssimon#endif
1705194206Ssimon
170655714Skris	if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
170755714Skris		{
170855714Skris		SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
170955714Skris		goto err;
171055714Skris		}
171168651Skris	ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
171255714Skris	if (ret == NULL)
171355714Skris		goto err;
171455714Skris
171555714Skris	memset(ret,0,sizeof(SSL_CTX));
171655714Skris
171755714Skris	ret->method=meth;
171855714Skris
171955714Skris	ret->cert_store=NULL;
172055714Skris	ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
172155714Skris	ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
172255714Skris	ret->session_cache_head=NULL;
172355714Skris	ret->session_cache_tail=NULL;
172455714Skris
172555714Skris	/* We take the system default */
172655714Skris	ret->session_timeout=meth->get_timeout();
172755714Skris
1728109998Smarkm	ret->new_session_cb=0;
1729109998Smarkm	ret->remove_session_cb=0;
1730109998Smarkm	ret->get_session_cb=0;
1731109998Smarkm	ret->generate_session_id=0;
173255714Skris
173355714Skris	memset((char *)&ret->stats,0,sizeof(ret->stats));
173455714Skris
173555714Skris	ret->references=1;
173655714Skris	ret->quiet_shutdown=0;
173755714Skris
173855714Skris/*	ret->cipher=NULL;*/
173955714Skris/*	ret->s2->challenge=NULL;
174055714Skris	ret->master_key=NULL;
174155714Skris	ret->key_arg=NULL;
174255714Skris	ret->s2->conn_id=NULL; */
174355714Skris
174455714Skris	ret->info_callback=NULL;
174555714Skris
1746109998Smarkm	ret->app_verify_callback=0;
174755714Skris	ret->app_verify_arg=NULL;
174855714Skris
1749109998Smarkm	ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT;
175055714Skris	ret->read_ahead=0;
1751109998Smarkm	ret->msg_callback=0;
1752109998Smarkm	ret->msg_callback_arg=NULL;
175355714Skris	ret->verify_mode=SSL_VERIFY_NONE;
1754160814Ssimon#if 0
175555714Skris	ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */
1756160814Ssimon#endif
1757109998Smarkm	ret->sid_ctx_length=0;
175855714Skris	ret->default_verify_callback=NULL;
175955714Skris	if ((ret->cert=ssl_cert_new()) == NULL)
176055714Skris		goto err;
176155714Skris
1762109998Smarkm	ret->default_passwd_callback=0;
176355714Skris	ret->default_passwd_callback_userdata=NULL;
1764109998Smarkm	ret->client_cert_cb=0;
1765160814Ssimon	ret->app_gen_cookie_cb=0;
1766160814Ssimon	ret->app_verify_cookie_cb=0;
176755714Skris
1768238405Sjkim	ret->sessions=lh_SSL_SESSION_new();
176955714Skris	if (ret->sessions == NULL) goto err;
177055714Skris	ret->cert_store=X509_STORE_new();
177155714Skris	if (ret->cert_store == NULL) goto err;
177255714Skris
177355714Skris	ssl_create_cipher_list(ret->method,
177455714Skris		&ret->cipher_list,&ret->cipher_list_by_id,
1775238405Sjkim		meth->version == SSL2_VERSION ? "SSLv2" : SSL_DEFAULT_CIPHER_LIST);
177655714Skris	if (ret->cipher_list == NULL
177755714Skris	    || sk_SSL_CIPHER_num(ret->cipher_list) <= 0)
177855714Skris		{
177955714Skris		SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_LIBRARY_HAS_NO_CIPHERS);
178055714Skris		goto err2;
178155714Skris		}
178255714Skris
1783160814Ssimon	ret->param = X509_VERIFY_PARAM_new();
1784160814Ssimon	if (!ret->param)
1785160814Ssimon		goto err;
1786160814Ssimon
178755714Skris	if ((ret->rsa_md5=EVP_get_digestbyname("ssl2-md5")) == NULL)
178855714Skris		{
178955714Skris		SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL2_MD5_ROUTINES);
179055714Skris		goto err2;
179155714Skris		}
179255714Skris	if ((ret->md5=EVP_get_digestbyname("ssl3-md5")) == NULL)
179355714Skris		{
179455714Skris		SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES);
179555714Skris		goto err2;
179655714Skris		}
179755714Skris	if ((ret->sha1=EVP_get_digestbyname("ssl3-sha1")) == NULL)
179855714Skris		{
179955714Skris		SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES);
180055714Skris		goto err2;
180155714Skris		}
180255714Skris
180355714Skris	if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL)
180455714Skris		goto err;
180555714Skris
1806109998Smarkm	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
180755714Skris
180855714Skris	ret->extra_certs=NULL;
1809264331Sjkim	/* No compression for DTLS */
1810264331Sjkim	if (meth->version != DTLS1_VERSION)
1811264331Sjkim		ret->comp_methods=SSL_COMP_get_compression_methods();
181255714Skris
1813238405Sjkim	ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1814238405Sjkim
1815194206Ssimon#ifndef OPENSSL_NO_TLSEXT
1816194206Ssimon	ret->tlsext_servername_callback = 0;
1817194206Ssimon	ret->tlsext_servername_arg = NULL;
1818194206Ssimon	/* Setup RFC4507 ticket keys */
1819194206Ssimon	if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0)
1820194206Ssimon		|| (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)
1821194206Ssimon		|| (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))
1822194206Ssimon		ret->options |= SSL_OP_NO_TICKET;
1823194206Ssimon
1824194206Ssimon	ret->tlsext_status_cb = 0;
1825194206Ssimon	ret->tlsext_status_arg = NULL;
1826194206Ssimon
1827238405Sjkim# ifndef OPENSSL_NO_NEXTPROTONEG
1828238405Sjkim	ret->next_protos_advertised_cb = 0;
1829238405Sjkim	ret->next_proto_select_cb = 0;
1830238405Sjkim# endif
1831194206Ssimon#endif
1832238405Sjkim#ifndef OPENSSL_NO_PSK
1833238405Sjkim	ret->psk_identity_hint=NULL;
1834238405Sjkim	ret->psk_client_callback=NULL;
1835238405Sjkim	ret->psk_server_callback=NULL;
1836238405Sjkim#endif
1837238405Sjkim#ifndef OPENSSL_NO_SRP
1838238405Sjkim	SSL_CTX_SRP_CTX_init(ret);
1839238405Sjkim#endif
1840238405Sjkim#ifndef OPENSSL_NO_BUF_FREELISTS
1841238405Sjkim	ret->freelist_max_len = SSL_MAX_BUF_FREELIST_LEN_DEFAULT;
1842238405Sjkim	ret->rbuf_freelist = OPENSSL_malloc(sizeof(SSL3_BUF_FREELIST));
1843238405Sjkim	if (!ret->rbuf_freelist)
1844238405Sjkim		goto err;
1845238405Sjkim	ret->rbuf_freelist->chunklen = 0;
1846238405Sjkim	ret->rbuf_freelist->len = 0;
1847238405Sjkim	ret->rbuf_freelist->head = NULL;
1848238405Sjkim	ret->wbuf_freelist = OPENSSL_malloc(sizeof(SSL3_BUF_FREELIST));
1849238405Sjkim	if (!ret->wbuf_freelist)
1850238405Sjkim		{
1851238405Sjkim		OPENSSL_free(ret->rbuf_freelist);
1852238405Sjkim		goto err;
1853238405Sjkim		}
1854238405Sjkim	ret->wbuf_freelist->chunklen = 0;
1855238405Sjkim	ret->wbuf_freelist->len = 0;
1856238405Sjkim	ret->wbuf_freelist->head = NULL;
1857238405Sjkim#endif
1858194206Ssimon#ifndef OPENSSL_NO_ENGINE
1859194206Ssimon	ret->client_cert_engine = NULL;
1860194206Ssimon#ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO
1861194206Ssimon#define eng_strx(x)	#x
1862194206Ssimon#define eng_str(x)	eng_strx(x)
1863194206Ssimon	/* Use specific client engine automatically... ignore errors */
1864194206Ssimon	{
1865194206Ssimon	ENGINE *eng;
1866194206Ssimon	eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
1867194206Ssimon	if (!eng)
1868194206Ssimon		{
1869194206Ssimon		ERR_clear_error();
1870194206Ssimon		ENGINE_load_builtin_engines();
1871194206Ssimon		eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
1872194206Ssimon		}
1873194206Ssimon	if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng))
1874194206Ssimon		ERR_clear_error();
1875194206Ssimon	}
1876194206Ssimon#endif
1877194206Ssimon#endif
1878205128Ssimon	/* Default is to connect to non-RI servers. When RI is more widely
1879205128Ssimon	 * deployed might change this.
1880205128Ssimon	 */
1881205128Ssimon	ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1882194206Ssimon
188355714Skris	return(ret);
188455714Skriserr:
188555714Skris	SSLerr(SSL_F_SSL_CTX_NEW,ERR_R_MALLOC_FAILURE);
188655714Skriserr2:
188755714Skris	if (ret != NULL) SSL_CTX_free(ret);
188855714Skris	return(NULL);
188955714Skris	}
189055714Skris
1891109998Smarkm#if 0
189255714Skrisstatic void SSL_COMP_free(SSL_COMP *comp)
189368651Skris    { OPENSSL_free(comp); }
1894109998Smarkm#endif
189555714Skris
1896238405Sjkim#ifndef OPENSSL_NO_BUF_FREELISTS
1897238405Sjkimstatic void
1898238405Sjkimssl_buf_freelist_free(SSL3_BUF_FREELIST *list)
1899238405Sjkim	{
1900238405Sjkim	SSL3_BUF_FREELIST_ENTRY *ent, *next;
1901238405Sjkim	for (ent = list->head; ent; ent = next)
1902238405Sjkim		{
1903238405Sjkim		next = ent->next;
1904238405Sjkim		OPENSSL_free(ent);
1905238405Sjkim		}
1906238405Sjkim	OPENSSL_free(list);
1907238405Sjkim	}
1908238405Sjkim#endif
1909238405Sjkim
191055714Skrisvoid SSL_CTX_free(SSL_CTX *a)
191155714Skris	{
191255714Skris	int i;
191355714Skris
191455714Skris	if (a == NULL) return;
191555714Skris
191655714Skris	i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
191755714Skris#ifdef REF_PRINT
191855714Skris	REF_PRINT("SSL_CTX",a);
191955714Skris#endif
192055714Skris	if (i > 0) return;
192155714Skris#ifdef REF_CHECK
192255714Skris	if (i < 0)
192355714Skris		{
192455714Skris		fprintf(stderr,"SSL_CTX_free, bad reference count\n");
192555714Skris		abort(); /* ok */
192655714Skris		}
192755714Skris#endif
192855714Skris
1929160814Ssimon	if (a->param)
1930160814Ssimon		X509_VERIFY_PARAM_free(a->param);
1931160814Ssimon
1932109998Smarkm	/*
1933109998Smarkm	 * Free internal session cache. However: the remove_cb() may reference
1934109998Smarkm	 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
1935109998Smarkm	 * after the sessions were flushed.
1936109998Smarkm	 * As the ex_data handling routines might also touch the session cache,
1937109998Smarkm	 * the most secure solution seems to be: empty (flush) the cache, then
1938109998Smarkm	 * free ex_data, then finally free the cache.
1939109998Smarkm	 * (See ticket [openssl.org #212].)
1940109998Smarkm	 */
194155714Skris	if (a->sessions != NULL)
194255714Skris		SSL_CTX_flush_sessions(a,0);
1943109998Smarkm
1944109998Smarkm	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
1945109998Smarkm
1946109998Smarkm	if (a->sessions != NULL)
1947238405Sjkim		lh_SSL_SESSION_free(a->sessions);
1948109998Smarkm
194955714Skris	if (a->cert_store != NULL)
195055714Skris		X509_STORE_free(a->cert_store);
195155714Skris	if (a->cipher_list != NULL)
195255714Skris		sk_SSL_CIPHER_free(a->cipher_list);
195355714Skris	if (a->cipher_list_by_id != NULL)
195455714Skris		sk_SSL_CIPHER_free(a->cipher_list_by_id);
195555714Skris	if (a->cert != NULL)
195655714Skris		ssl_cert_free(a->cert);
195755714Skris	if (a->client_CA != NULL)
195855714Skris		sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);
195955714Skris	if (a->extra_certs != NULL)
196055714Skris		sk_X509_pop_free(a->extra_certs,X509_free);
1961109998Smarkm#if 0 /* This should never be done, since it removes a global database */
196255714Skris	if (a->comp_methods != NULL)
196355714Skris		sk_SSL_COMP_pop_free(a->comp_methods,SSL_COMP_free);
1964109998Smarkm#else
1965109998Smarkm	a->comp_methods = NULL;
1966109998Smarkm#endif
1967238405Sjkim
1968246772Sjkim#ifndef OPENSSL_NO_SRTP
1969238405Sjkim        if (a->srtp_profiles)
1970238405Sjkim                sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
1971246772Sjkim#endif
1972238405Sjkim
1973238405Sjkim#ifndef OPENSSL_NO_PSK
1974238405Sjkim	if (a->psk_identity_hint)
1975238405Sjkim		OPENSSL_free(a->psk_identity_hint);
1976238405Sjkim#endif
1977238405Sjkim#ifndef OPENSSL_NO_SRP
1978238405Sjkim	SSL_CTX_SRP_CTX_free(a);
1979238405Sjkim#endif
1980194206Ssimon#ifndef OPENSSL_NO_ENGINE
1981194206Ssimon	if (a->client_cert_engine)
1982194206Ssimon		ENGINE_finish(a->client_cert_engine);
1983194206Ssimon#endif
1984238405Sjkim
1985238405Sjkim#ifndef OPENSSL_NO_BUF_FREELISTS
1986238405Sjkim	if (a->wbuf_freelist)
1987238405Sjkim		ssl_buf_freelist_free(a->wbuf_freelist);
1988238405Sjkim	if (a->rbuf_freelist)
1989238405Sjkim		ssl_buf_freelist_free(a->rbuf_freelist);
1990238405Sjkim#endif
1991238405Sjkim
199268651Skris	OPENSSL_free(a);
199355714Skris	}
199455714Skris
199555714Skrisvoid SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
199655714Skris	{
199755714Skris	ctx->default_passwd_callback=cb;
199855714Skris	}
199955714Skris
200055714Skrisvoid SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u)
200155714Skris	{
200255714Skris	ctx->default_passwd_callback_userdata=u;
200355714Skris	}
200455714Skris
2005109998Smarkmvoid SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg)
200655714Skris	{
200755714Skris	ctx->app_verify_callback=cb;
2008109998Smarkm	ctx->app_verify_arg=arg;
200955714Skris	}
201055714Skris
201155714Skrisvoid SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
201255714Skris	{
201355714Skris	ctx->verify_mode=mode;
201455714Skris	ctx->default_verify_callback=cb;
201555714Skris	}
201655714Skris
201755714Skrisvoid SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth)
201855714Skris	{
2019160814Ssimon	X509_VERIFY_PARAM_set_depth(ctx->param, depth);
202055714Skris	}
202155714Skris
2022238405Sjkimvoid ssl_set_cert_masks(CERT *c, const SSL_CIPHER *cipher)
202355714Skris	{
202455714Skris	CERT_PKEY *cpk;
202555714Skris	int rsa_enc,rsa_tmp,rsa_sign,dh_tmp,dh_rsa,dh_dsa,dsa_sign;
202655714Skris	int rsa_enc_export,dh_rsa_export,dh_dsa_export;
202755714Skris	int rsa_tmp_export,dh_tmp_export,kl;
2028238405Sjkim	unsigned long mask_k,mask_a,emask_k,emask_a;
2029160814Ssimon	int have_ecc_cert, ecdh_ok, ecdsa_ok, ecc_pkey_size;
2030160814Ssimon#ifndef OPENSSL_NO_ECDH
2031160814Ssimon	int have_ecdh_tmp;
2032160814Ssimon#endif
2033160814Ssimon	X509 *x = NULL;
2034160814Ssimon	EVP_PKEY *ecc_pkey = NULL;
2035238405Sjkim	int signature_nid = 0, pk_nid = 0, md_nid = 0;
203655714Skris
203755714Skris	if (c == NULL) return;
203855714Skris
203955714Skris	kl=SSL_C_EXPORT_PKEYLENGTH(cipher);
204055714Skris
2041109998Smarkm#ifndef OPENSSL_NO_RSA
204255714Skris	rsa_tmp=(c->rsa_tmp != NULL || c->rsa_tmp_cb != NULL);
204355714Skris	rsa_tmp_export=(c->rsa_tmp_cb != NULL ||
204455714Skris		(rsa_tmp && RSA_size(c->rsa_tmp)*8 <= kl));
204555714Skris#else
204655714Skris	rsa_tmp=rsa_tmp_export=0;
204755714Skris#endif
2048109998Smarkm#ifndef OPENSSL_NO_DH
204955714Skris	dh_tmp=(c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
205055714Skris	dh_tmp_export=(c->dh_tmp_cb != NULL ||
205155714Skris		(dh_tmp && DH_size(c->dh_tmp)*8 <= kl));
205255714Skris#else
205355714Skris	dh_tmp=dh_tmp_export=0;
205455714Skris#endif
205555714Skris
2056160814Ssimon#ifndef OPENSSL_NO_ECDH
2057160814Ssimon	have_ecdh_tmp=(c->ecdh_tmp != NULL || c->ecdh_tmp_cb != NULL);
2058160814Ssimon#endif
205955714Skris	cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]);
206055714Skris	rsa_enc= (cpk->x509 != NULL && cpk->privatekey != NULL);
206155714Skris	rsa_enc_export=(rsa_enc && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
206255714Skris	cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]);
206355714Skris	rsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL);
206455714Skris	cpk= &(c->pkeys[SSL_PKEY_DSA_SIGN]);
206555714Skris	dsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL);
206655714Skris	cpk= &(c->pkeys[SSL_PKEY_DH_RSA]);
206755714Skris	dh_rsa=  (cpk->x509 != NULL && cpk->privatekey != NULL);
206855714Skris	dh_rsa_export=(dh_rsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
206955714Skris	cpk= &(c->pkeys[SSL_PKEY_DH_DSA]);
207055714Skris/* FIX THIS EAY EAY EAY */
207155714Skris	dh_dsa=  (cpk->x509 != NULL && cpk->privatekey != NULL);
207255714Skris	dh_dsa_export=(dh_dsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
2073160814Ssimon	cpk= &(c->pkeys[SSL_PKEY_ECC]);
2074160814Ssimon	have_ecc_cert= (cpk->x509 != NULL && cpk->privatekey != NULL);
2075238405Sjkim	mask_k=0;
2076238405Sjkim	mask_a=0;
2077238405Sjkim	emask_k=0;
2078238405Sjkim	emask_a=0;
207955714Skris
2080238405Sjkim
2081238405Sjkim
208255714Skris#ifdef CIPHER_DEBUG
2083238405Sjkim	printf("rt=%d rte=%d dht=%d ecdht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n",
2084238405Sjkim	        rsa_tmp,rsa_tmp_export,dh_tmp,have_ecdh_tmp,
208555714Skris		rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
208655714Skris#endif
2087238405Sjkim
2088238405Sjkim	cpk = &(c->pkeys[SSL_PKEY_GOST01]);
2089238405Sjkim	if (cpk->x509 != NULL && cpk->privatekey !=NULL) {
2090238405Sjkim		mask_k |= SSL_kGOST;
2091238405Sjkim		mask_a |= SSL_aGOST01;
2092238405Sjkim	}
2093238405Sjkim	cpk = &(c->pkeys[SSL_PKEY_GOST94]);
2094238405Sjkim	if (cpk->x509 != NULL && cpk->privatekey !=NULL) {
2095238405Sjkim		mask_k |= SSL_kGOST;
2096238405Sjkim		mask_a |= SSL_aGOST94;
2097238405Sjkim	}
209855714Skris
209955714Skris	if (rsa_enc || (rsa_tmp && rsa_sign))
2100238405Sjkim		mask_k|=SSL_kRSA;
210155714Skris	if (rsa_enc_export || (rsa_tmp_export && (rsa_sign || rsa_enc)))
2102238405Sjkim		emask_k|=SSL_kRSA;
210355714Skris
210455714Skris#if 0
210555714Skris	/* The match needs to be both kEDH and aRSA or aDSA, so don't worry */
2106238405Sjkim	if (	(dh_tmp || dh_rsa || dh_dsa) &&
210755714Skris		(rsa_enc || rsa_sign || dsa_sign))
2108238405Sjkim		mask_k|=SSL_kEDH;
210955714Skris	if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) &&
211055714Skris		(rsa_enc || rsa_sign || dsa_sign))
2111238405Sjkim		emask_k|=SSL_kEDH;
211255714Skris#endif
211355714Skris
2114238405Sjkim	if (dh_tmp_export)
2115238405Sjkim		emask_k|=SSL_kEDH;
211655714Skris
211755714Skris	if (dh_tmp)
2118238405Sjkim		mask_k|=SSL_kEDH;
211955714Skris
2120238405Sjkim	if (dh_rsa) mask_k|=SSL_kDHr;
2121238405Sjkim	if (dh_rsa_export) emask_k|=SSL_kDHr;
212255714Skris
2123238405Sjkim	if (dh_dsa) mask_k|=SSL_kDHd;
2124238405Sjkim	if (dh_dsa_export) emask_k|=SSL_kDHd;
212555714Skris
212655714Skris	if (rsa_enc || rsa_sign)
212755714Skris		{
2128238405Sjkim		mask_a|=SSL_aRSA;
2129238405Sjkim		emask_a|=SSL_aRSA;
213055714Skris		}
213155714Skris
213255714Skris	if (dsa_sign)
213355714Skris		{
2134238405Sjkim		mask_a|=SSL_aDSS;
2135238405Sjkim		emask_a|=SSL_aDSS;
213655714Skris		}
213755714Skris
2138238405Sjkim	mask_a|=SSL_aNULL;
2139238405Sjkim	emask_a|=SSL_aNULL;
214055714Skris
2141109998Smarkm#ifndef OPENSSL_NO_KRB5
2142238405Sjkim	mask_k|=SSL_kKRB5;
2143238405Sjkim	mask_a|=SSL_aKRB5;
2144238405Sjkim	emask_k|=SSL_kKRB5;
2145238405Sjkim	emask_a|=SSL_aKRB5;
2146109998Smarkm#endif
2147109998Smarkm
2148160814Ssimon	/* An ECC certificate may be usable for ECDH and/or
2149160814Ssimon	 * ECDSA cipher suites depending on the key usage extension.
2150160814Ssimon	 */
2151160814Ssimon	if (have_ecc_cert)
2152160814Ssimon		{
2153238405Sjkim		/* This call populates extension flags (ex_flags) */
2154160814Ssimon		x = (c->pkeys[SSL_PKEY_ECC]).x509;
2155160814Ssimon		X509_check_purpose(x, -1, 0);
2156160814Ssimon		ecdh_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
2157160814Ssimon		    (x->ex_kusage & X509v3_KU_KEY_AGREEMENT) : 1;
2158160814Ssimon		ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
2159160814Ssimon		    (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
2160160814Ssimon		ecc_pkey = X509_get_pubkey(x);
2161238405Sjkim		ecc_pkey_size = (ecc_pkey != NULL) ?
2162160814Ssimon		    EVP_PKEY_bits(ecc_pkey) : 0;
2163160814Ssimon		EVP_PKEY_free(ecc_pkey);
2164160814Ssimon		if ((x->sig_alg) && (x->sig_alg->algorithm))
2165238405Sjkim			{
2166160814Ssimon			signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2167238405Sjkim			OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2168238405Sjkim			}
2169160814Ssimon#ifndef OPENSSL_NO_ECDH
2170160814Ssimon		if (ecdh_ok)
2171160814Ssimon			{
2172238405Sjkim
2173238405Sjkim			if (pk_nid == NID_rsaEncryption || pk_nid == NID_rsa)
2174160814Ssimon				{
2175238405Sjkim				mask_k|=SSL_kECDHr;
2176238405Sjkim				mask_a|=SSL_aECDH;
2177160814Ssimon				if (ecc_pkey_size <= 163)
2178238405Sjkim					{
2179238405Sjkim					emask_k|=SSL_kECDHr;
2180238405Sjkim					emask_a|=SSL_aECDH;
2181238405Sjkim					}
2182160814Ssimon				}
2183238405Sjkim
2184238405Sjkim			if (pk_nid == NID_X9_62_id_ecPublicKey)
2185160814Ssimon				{
2186238405Sjkim				mask_k|=SSL_kECDHe;
2187238405Sjkim				mask_a|=SSL_aECDH;
2188160814Ssimon				if (ecc_pkey_size <= 163)
2189238405Sjkim					{
2190238405Sjkim					emask_k|=SSL_kECDHe;
2191238405Sjkim					emask_a|=SSL_aECDH;
2192238405Sjkim					}
2193160814Ssimon				}
2194160814Ssimon			}
2195160814Ssimon#endif
2196160814Ssimon#ifndef OPENSSL_NO_ECDSA
2197160814Ssimon		if (ecdsa_ok)
2198160814Ssimon			{
2199238405Sjkim			mask_a|=SSL_aECDSA;
2200238405Sjkim			emask_a|=SSL_aECDSA;
2201160814Ssimon			}
2202160814Ssimon#endif
2203160814Ssimon		}
2204160814Ssimon
2205160814Ssimon#ifndef OPENSSL_NO_ECDH
2206160814Ssimon	if (have_ecdh_tmp)
2207160814Ssimon		{
2208238405Sjkim		mask_k|=SSL_kEECDH;
2209238405Sjkim		emask_k|=SSL_kEECDH;
2210160814Ssimon		}
2211160814Ssimon#endif
2212238405Sjkim
2213238405Sjkim#ifndef OPENSSL_NO_PSK
2214238405Sjkim	mask_k |= SSL_kPSK;
2215238405Sjkim	mask_a |= SSL_aPSK;
2216238405Sjkim	emask_k |= SSL_kPSK;
2217238405Sjkim	emask_a |= SSL_aPSK;
2218238405Sjkim#endif
2219238405Sjkim
2220238405Sjkim	c->mask_k=mask_k;
2221238405Sjkim	c->mask_a=mask_a;
2222238405Sjkim	c->export_mask_k=emask_k;
2223238405Sjkim	c->export_mask_a=emask_a;
222455714Skris	c->valid=1;
222555714Skris	}
222655714Skris
2227160814Ssimon/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2228160814Ssimon#define ku_reject(x, usage) \
2229160814Ssimon	(((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
2230160814Ssimon
2231238405Sjkim#ifndef OPENSSL_NO_EC
2232238405Sjkim
2233238405Sjkimint ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
2234160814Ssimon	{
2235238405Sjkim	unsigned long alg_k, alg_a;
2236160814Ssimon	EVP_PKEY *pkey = NULL;
2237160814Ssimon	int keysize = 0;
2238238405Sjkim	int signature_nid = 0, md_nid = 0, pk_nid = 0;
2239238405Sjkim	const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
2240160814Ssimon
2241238405Sjkim	alg_k = cs->algorithm_mkey;
2242238405Sjkim	alg_a = cs->algorithm_auth;
2243238405Sjkim
2244160814Ssimon	if (SSL_C_IS_EXPORT(cs))
2245160814Ssimon		{
2246160814Ssimon		/* ECDH key length in export ciphers must be <= 163 bits */
2247160814Ssimon		pkey = X509_get_pubkey(x);
2248160814Ssimon		if (pkey == NULL) return 0;
2249160814Ssimon		keysize = EVP_PKEY_bits(pkey);
2250160814Ssimon		EVP_PKEY_free(pkey);
2251160814Ssimon		if (keysize > 163) return 0;
2252160814Ssimon		}
2253160814Ssimon
2254160814Ssimon	/* This call populates the ex_flags field correctly */
2255160814Ssimon	X509_check_purpose(x, -1, 0);
2256160814Ssimon	if ((x->sig_alg) && (x->sig_alg->algorithm))
2257238405Sjkim		{
2258160814Ssimon		signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2259238405Sjkim		OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2260238405Sjkim		}
2261238405Sjkim	if (alg_k & SSL_kECDHe || alg_k & SSL_kECDHr)
2262160814Ssimon		{
2263160814Ssimon		/* key usage, if present, must allow key agreement */
2264160814Ssimon		if (ku_reject(x, X509v3_KU_KEY_AGREEMENT))
2265160814Ssimon			{
2266238405Sjkim			SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_NOT_FOR_KEY_AGREEMENT);
2267160814Ssimon			return 0;
2268160814Ssimon			}
2269238405Sjkim		if ((alg_k & SSL_kECDHe) && TLS1_get_version(s) < TLS1_2_VERSION)
2270160814Ssimon			{
2271160814Ssimon			/* signature alg must be ECDSA */
2272238405Sjkim			if (pk_nid != NID_X9_62_id_ecPublicKey)
2273160814Ssimon				{
2274238405Sjkim				SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_SHOULD_HAVE_SHA1_SIGNATURE);
2275160814Ssimon				return 0;
2276160814Ssimon				}
2277160814Ssimon			}
2278238405Sjkim		if ((alg_k & SSL_kECDHr) && TLS1_get_version(s) < TLS1_2_VERSION)
2279160814Ssimon			{
2280160814Ssimon			/* signature alg must be RSA */
2281238405Sjkim
2282238405Sjkim			if (pk_nid != NID_rsaEncryption && pk_nid != NID_rsa)
2283160814Ssimon				{
2284238405Sjkim				SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_SHOULD_HAVE_RSA_SIGNATURE);
2285160814Ssimon				return 0;
2286160814Ssimon				}
2287160814Ssimon			}
2288238405Sjkim		}
2289238405Sjkim	if (alg_a & SSL_aECDSA)
2290160814Ssimon		{
2291160814Ssimon		/* key usage, if present, must allow signing */
2292160814Ssimon		if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE))
2293160814Ssimon			{
2294238405Sjkim			SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2295160814Ssimon			return 0;
2296160814Ssimon			}
2297160814Ssimon		}
2298160814Ssimon
2299160814Ssimon	return 1;  /* all checks are ok */
2300160814Ssimon	}
2301160814Ssimon
2302238405Sjkim#endif
2303238405Sjkim
230455714Skris/* THIS NEEDS CLEANING UP */
2305246772SjkimCERT_PKEY *ssl_get_server_send_pkey(const SSL *s)
230655714Skris	{
2307238405Sjkim	unsigned long alg_k,alg_a;
230855714Skris	CERT *c;
2309215697Ssimon	int i;
231055714Skris
231155714Skris	c=s->cert;
231255714Skris	ssl_set_cert_masks(c, s->s3->tmp.new_cipher);
2313238405Sjkim
2314238405Sjkim	alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2315238405Sjkim	alg_a = s->s3->tmp.new_cipher->algorithm_auth;
231655714Skris
2317238405Sjkim	if (alg_k & (SSL_kECDHr|SSL_kECDHe))
2318160814Ssimon		{
2319238405Sjkim		/* we don't need to look at SSL_kEECDH
2320160814Ssimon		 * since no certificate is needed for
2321160814Ssimon		 * anon ECDH and for authenticated
2322238405Sjkim		 * EECDH, the check for the auth
2323160814Ssimon		 * algorithm will set i correctly
2324160814Ssimon		 * NOTE: For ECDH-RSA, we need an ECC
2325238405Sjkim		 * not an RSA cert but for EECDH-RSA
2326160814Ssimon		 * we need an RSA cert. Placing the
2327160814Ssimon		 * checks for SSL_kECDH before RSA
2328160814Ssimon		 * checks ensures the correct cert is chosen.
2329160814Ssimon		 */
2330160814Ssimon		i=SSL_PKEY_ECC;
2331160814Ssimon		}
2332238405Sjkim	else if (alg_a & SSL_aECDSA)
2333160814Ssimon		{
2334160814Ssimon		i=SSL_PKEY_ECC;
2335160814Ssimon		}
2336238405Sjkim	else if (alg_k & SSL_kDHr)
233755714Skris		i=SSL_PKEY_DH_RSA;
2338238405Sjkim	else if (alg_k & SSL_kDHd)
233955714Skris		i=SSL_PKEY_DH_DSA;
2340238405Sjkim	else if (alg_a & SSL_aDSS)
234155714Skris		i=SSL_PKEY_DSA_SIGN;
2342238405Sjkim	else if (alg_a & SSL_aRSA)
234355714Skris		{
234455714Skris		if (c->pkeys[SSL_PKEY_RSA_ENC].x509 == NULL)
234555714Skris			i=SSL_PKEY_RSA_SIGN;
234655714Skris		else
234755714Skris			i=SSL_PKEY_RSA_ENC;
234855714Skris		}
2349238405Sjkim	else if (alg_a & SSL_aKRB5)
2350109998Smarkm		{
2351109998Smarkm		/* VRS something else here? */
2352109998Smarkm		return(NULL);
2353109998Smarkm		}
2354238405Sjkim	else if (alg_a & SSL_aGOST94)
2355238405Sjkim		i=SSL_PKEY_GOST94;
2356238405Sjkim	else if (alg_a & SSL_aGOST01)
2357238405Sjkim		i=SSL_PKEY_GOST01;
2358238405Sjkim	else /* if (alg_a & SSL_aNULL) */
235955714Skris		{
2360246772Sjkim		SSLerr(SSL_F_SSL_GET_SERVER_SEND_PKEY,ERR_R_INTERNAL_ERROR);
236155714Skris		return(NULL);
236255714Skris		}
2363160814Ssimon
2364246772Sjkim	return c->pkeys + i;
236555714Skris	}
236655714Skris
2367246772SjkimX509 *ssl_get_server_send_cert(const SSL *s)
2368246772Sjkim	{
2369246772Sjkim	CERT_PKEY *cpk;
2370246772Sjkim	cpk = ssl_get_server_send_pkey(s);
2371246772Sjkim	if (!cpk)
2372246772Sjkim		return NULL;
2373246772Sjkim	return cpk->x509;
2374246772Sjkim	}
2375246772Sjkim
2376238405SjkimEVP_PKEY *ssl_get_sign_pkey(SSL *s,const SSL_CIPHER *cipher, const EVP_MD **pmd)
237755714Skris	{
2378238405Sjkim	unsigned long alg_a;
237955714Skris	CERT *c;
2380238405Sjkim	int idx = -1;
238155714Skris
2382238405Sjkim	alg_a = cipher->algorithm_auth;
238355714Skris	c=s->cert;
238455714Skris
2385238405Sjkim	if ((alg_a & SSL_aDSS) &&
238655714Skris		(c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL))
2387238405Sjkim		idx = SSL_PKEY_DSA_SIGN;
2388238405Sjkim	else if (alg_a & SSL_aRSA)
238955714Skris		{
239055714Skris		if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
2391238405Sjkim			idx = SSL_PKEY_RSA_SIGN;
239255714Skris		else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
2393238405Sjkim			idx = SSL_PKEY_RSA_ENC;
239455714Skris		}
2395238405Sjkim	else if ((alg_a & SSL_aECDSA) &&
2396160814Ssimon	         (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2397238405Sjkim		idx = SSL_PKEY_ECC;
2398238405Sjkim	if (idx == -1)
239955714Skris		{
2400109998Smarkm		SSLerr(SSL_F_SSL_GET_SIGN_PKEY,ERR_R_INTERNAL_ERROR);
240155714Skris		return(NULL);
240255714Skris		}
2403238405Sjkim	if (pmd)
2404238405Sjkim		*pmd = c->pkeys[idx].digest;
2405238405Sjkim	return c->pkeys[idx].privatekey;
240655714Skris	}
240755714Skris
240855714Skrisvoid ssl_update_cache(SSL *s,int mode)
240955714Skris	{
241055714Skris	int i;
241155714Skris
241255714Skris	/* If the session_id_length is 0, we are not supposed to cache it,
241355714Skris	 * and it would be rather hard to do anyway :-) */
241455714Skris	if (s->session->session_id_length == 0) return;
241555714Skris
2416238405Sjkim	i=s->session_ctx->session_cache_mode;
2417100928Snectar	if ((i & mode) && (!s->hit)
2418109998Smarkm		&& ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
2419238405Sjkim		    || SSL_CTX_add_session(s->session_ctx,s->session))
2420238405Sjkim		&& (s->session_ctx->new_session_cb != NULL))
242155714Skris		{
242255714Skris		CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
2423238405Sjkim		if (!s->session_ctx->new_session_cb(s,s->session))
242455714Skris			SSL_SESSION_free(s->session);
242555714Skris		}
242655714Skris
242755714Skris	/* auto flush every 255 connections */
242855714Skris	if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
242955714Skris		((i & mode) == mode))
243055714Skris		{
243155714Skris		if (  (((mode & SSL_SESS_CACHE_CLIENT)
2432238405Sjkim			?s->session_ctx->stats.sess_connect_good
2433238405Sjkim			:s->session_ctx->stats.sess_accept_good) & 0xff) == 0xff)
243455714Skris			{
2435238405Sjkim			SSL_CTX_flush_sessions(s->session_ctx,(unsigned long)time(NULL));
243655714Skris			}
243755714Skris		}
243855714Skris	}
243955714Skris
2440238405Sjkimconst SSL_METHOD *SSL_get_ssl_method(SSL *s)
244155714Skris	{
244255714Skris	return(s->method);
244355714Skris	}
244455714Skris
2445238405Sjkimint SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
244655714Skris	{
244755714Skris	int conn= -1;
244855714Skris	int ret=1;
244955714Skris
245055714Skris	if (s->method != meth)
245155714Skris		{
245255714Skris		if (s->handshake_func != NULL)
245355714Skris			conn=(s->handshake_func == s->method->ssl_connect);
245455714Skris
245555714Skris		if (s->method->version == meth->version)
245655714Skris			s->method=meth;
245755714Skris		else
245855714Skris			{
245955714Skris			s->method->ssl_free(s);
246055714Skris			s->method=meth;
246155714Skris			ret=s->method->ssl_new(s);
246255714Skris			}
246355714Skris
246455714Skris		if (conn == 1)
246555714Skris			s->handshake_func=meth->ssl_connect;
246655714Skris		else if (conn == 0)
246755714Skris			s->handshake_func=meth->ssl_accept;
246855714Skris		}
246955714Skris	return(ret);
247055714Skris	}
247155714Skris
2472160814Ssimonint SSL_get_error(const SSL *s,int i)
247355714Skris	{
247455714Skris	int reason;
247555714Skris	unsigned long l;
247655714Skris	BIO *bio;
247755714Skris
247855714Skris	if (i > 0) return(SSL_ERROR_NONE);
247955714Skris
248055714Skris	/* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
248155714Skris	 * etc, where we do encode the error */
248255714Skris	if ((l=ERR_peek_error()) != 0)
248355714Skris		{
248455714Skris		if (ERR_GET_LIB(l) == ERR_LIB_SYS)
248555714Skris			return(SSL_ERROR_SYSCALL);
248655714Skris		else
248755714Skris			return(SSL_ERROR_SSL);
248855714Skris		}
248955714Skris
249055714Skris	if ((i < 0) && SSL_want_read(s))
249155714Skris		{
249255714Skris		bio=SSL_get_rbio(s);
249355714Skris		if (BIO_should_read(bio))
249455714Skris			return(SSL_ERROR_WANT_READ);
249555714Skris		else if (BIO_should_write(bio))
249655714Skris			/* This one doesn't make too much sense ... We never try
249755714Skris			 * to write to the rbio, and an application program where
249855714Skris			 * rbio and wbio are separate couldn't even know what it
249955714Skris			 * should wait for.
250055714Skris			 * However if we ever set s->rwstate incorrectly
250155714Skris			 * (so that we have SSL_want_read(s) instead of
250255714Skris			 * SSL_want_write(s)) and rbio and wbio *are* the same,
250355714Skris			 * this test works around that bug; so it might be safer
250455714Skris			 * to keep it. */
250555714Skris			return(SSL_ERROR_WANT_WRITE);
250655714Skris		else if (BIO_should_io_special(bio))
250755714Skris			{
250855714Skris			reason=BIO_get_retry_reason(bio);
250955714Skris			if (reason == BIO_RR_CONNECT)
251055714Skris				return(SSL_ERROR_WANT_CONNECT);
2511109998Smarkm			else if (reason == BIO_RR_ACCEPT)
2512109998Smarkm				return(SSL_ERROR_WANT_ACCEPT);
251355714Skris			else
251455714Skris				return(SSL_ERROR_SYSCALL); /* unknown */
251555714Skris			}
251655714Skris		}
251755714Skris
251855714Skris	if ((i < 0) && SSL_want_write(s))
251955714Skris		{
252055714Skris		bio=SSL_get_wbio(s);
252155714Skris		if (BIO_should_write(bio))
252255714Skris			return(SSL_ERROR_WANT_WRITE);
252355714Skris		else if (BIO_should_read(bio))
252455714Skris			/* See above (SSL_want_read(s) with BIO_should_write(bio)) */
252555714Skris			return(SSL_ERROR_WANT_READ);
252655714Skris		else if (BIO_should_io_special(bio))
252755714Skris			{
252855714Skris			reason=BIO_get_retry_reason(bio);
252955714Skris			if (reason == BIO_RR_CONNECT)
253055714Skris				return(SSL_ERROR_WANT_CONNECT);
2531109998Smarkm			else if (reason == BIO_RR_ACCEPT)
2532109998Smarkm				return(SSL_ERROR_WANT_ACCEPT);
253355714Skris			else
253455714Skris				return(SSL_ERROR_SYSCALL);
253555714Skris			}
253655714Skris		}
253755714Skris	if ((i < 0) && SSL_want_x509_lookup(s))
253855714Skris		{
253955714Skris		return(SSL_ERROR_WANT_X509_LOOKUP);
254055714Skris		}
254155714Skris
254255714Skris	if (i == 0)
254355714Skris		{
254455714Skris		if (s->version == SSL2_VERSION)
254555714Skris			{
254655714Skris			/* assume it is the socket being closed */
254755714Skris			return(SSL_ERROR_ZERO_RETURN);
254855714Skris			}
254955714Skris		else
255055714Skris			{
255155714Skris			if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
255255714Skris				(s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
255355714Skris				return(SSL_ERROR_ZERO_RETURN);
255455714Skris			}
255555714Skris		}
255655714Skris	return(SSL_ERROR_SYSCALL);
255755714Skris	}
255855714Skris
255955714Skrisint SSL_do_handshake(SSL *s)
256055714Skris	{
256155714Skris	int ret=1;
256255714Skris
256355714Skris	if (s->handshake_func == NULL)
256455714Skris		{
256555714Skris		SSLerr(SSL_F_SSL_DO_HANDSHAKE,SSL_R_CONNECTION_TYPE_NOT_SET);
256655714Skris		return(-1);
256755714Skris		}
256855714Skris
256955714Skris	s->method->ssl_renegotiate_check(s);
257055714Skris
257155714Skris	if (SSL_in_init(s) || SSL_in_before(s))
257255714Skris		{
257355714Skris		ret=s->handshake_func(s);
257455714Skris		}
257555714Skris	return(ret);
257655714Skris	}
257755714Skris
257855714Skris/* For the next 2 functions, SSL_clear() sets shutdown and so
257955714Skris * one of these calls will reset it */
258055714Skrisvoid SSL_set_accept_state(SSL *s)
258155714Skris	{
258255714Skris	s->server=1;
258355714Skris	s->shutdown=0;
258455714Skris	s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
258555714Skris	s->handshake_func=s->method->ssl_accept;
258655714Skris	/* clear the current cipher */
258755714Skris	ssl_clear_cipher_ctx(s);
2588238405Sjkim	ssl_clear_hash_ctx(&s->read_hash);
2589238405Sjkim	ssl_clear_hash_ctx(&s->write_hash);
259055714Skris	}
259155714Skris
259255714Skrisvoid SSL_set_connect_state(SSL *s)
259355714Skris	{
259455714Skris	s->server=0;
259555714Skris	s->shutdown=0;
259655714Skris	s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
259755714Skris	s->handshake_func=s->method->ssl_connect;
259855714Skris	/* clear the current cipher */
259955714Skris	ssl_clear_cipher_ctx(s);
2600238405Sjkim	ssl_clear_hash_ctx(&s->read_hash);
2601238405Sjkim	ssl_clear_hash_ctx(&s->write_hash);
260255714Skris	}
260355714Skris
260455714Skrisint ssl_undefined_function(SSL *s)
260555714Skris	{
260655714Skris	SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
260755714Skris	return(0);
260855714Skris	}
260955714Skris
2610160814Ssimonint ssl_undefined_void_function(void)
2611160814Ssimon	{
2612160814Ssimon	SSLerr(SSL_F_SSL_UNDEFINED_VOID_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2613160814Ssimon	return(0);
2614160814Ssimon	}
2615160814Ssimon
2616160814Ssimonint ssl_undefined_const_function(const SSL *s)
2617160814Ssimon	{
2618160814Ssimon	SSLerr(SSL_F_SSL_UNDEFINED_CONST_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2619160814Ssimon	return(0);
2620160814Ssimon	}
2621160814Ssimon
262255714SkrisSSL_METHOD *ssl_bad_method(int ver)
262355714Skris	{
262455714Skris	SSLerr(SSL_F_SSL_BAD_METHOD,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
262555714Skris	return(NULL);
262655714Skris	}
262755714Skris
2628160814Ssimonconst char *SSL_get_version(const SSL *s)
262955714Skris	{
2630238405Sjkim	if (s->version == TLS1_2_VERSION)
2631238405Sjkim		return("TLSv1.2");
2632238405Sjkim	else if (s->version == TLS1_1_VERSION)
2633238405Sjkim		return("TLSv1.1");
2634246772Sjkim	else if (s->version == TLS1_VERSION)
263555714Skris		return("TLSv1");
263655714Skris	else if (s->version == SSL3_VERSION)
263755714Skris		return("SSLv3");
263855714Skris	else if (s->version == SSL2_VERSION)
263955714Skris		return("SSLv2");
264055714Skris	else
264155714Skris		return("unknown");
264255714Skris	}
264355714Skris
264455714SkrisSSL *SSL_dup(SSL *s)
264555714Skris	{
264655714Skris	STACK_OF(X509_NAME) *sk;
264755714Skris	X509_NAME *xn;
264855714Skris	SSL *ret;
264955714Skris	int i;
2650238405Sjkim
265155714Skris	if ((ret=SSL_new(SSL_get_SSL_CTX(s))) == NULL)
265255714Skris	    return(NULL);
2653109998Smarkm
2654109998Smarkm	ret->version = s->version;
2655109998Smarkm	ret->type = s->type;
2656109998Smarkm	ret->method = s->method;
2657109998Smarkm
265855714Skris	if (s->session != NULL)
265955714Skris		{
266055714Skris		/* This copies session-id, SSL_METHOD, sid_ctx, and 'cert' */
266155714Skris		SSL_copy_session_id(ret,s);
266255714Skris		}
266355714Skris	else
266455714Skris		{
266555714Skris		/* No session has been established yet, so we have to expect
266655714Skris		 * that s->cert or ret->cert will be changed later --
266755714Skris		 * they should not both point to the same object,
266855714Skris		 * and thus we can't use SSL_copy_session_id. */
266955714Skris
2670111147Snectar		ret->method->ssl_free(ret);
267155714Skris		ret->method = s->method;
267255714Skris		ret->method->ssl_new(ret);
267355714Skris
267455714Skris		if (s->cert != NULL)
267555714Skris			{
267676866Skris			if (ret->cert != NULL)
267776866Skris				{
267876866Skris				ssl_cert_free(ret->cert);
267976866Skris				}
268055714Skris			ret->cert = ssl_cert_dup(s->cert);
268155714Skris			if (ret->cert == NULL)
268255714Skris				goto err;
268355714Skris			}
268455714Skris
268555714Skris		SSL_set_session_id_context(ret,
268655714Skris			s->sid_ctx, s->sid_ctx_length);
268755714Skris		}
268855714Skris
2689109998Smarkm	ret->options=s->options;
2690109998Smarkm	ret->mode=s->mode;
2691109998Smarkm	SSL_set_max_cert_list(ret,SSL_get_max_cert_list(s));
269255714Skris	SSL_set_read_ahead(ret,SSL_get_read_ahead(s));
2693109998Smarkm	ret->msg_callback = s->msg_callback;
2694109998Smarkm	ret->msg_callback_arg = s->msg_callback_arg;
269555714Skris	SSL_set_verify(ret,SSL_get_verify_mode(s),
269655714Skris		SSL_get_verify_callback(s));
269755714Skris	SSL_set_verify_depth(ret,SSL_get_verify_depth(s));
2698109998Smarkm	ret->generate_session_id = s->generate_session_id;
269955714Skris
270055714Skris	SSL_set_info_callback(ret,SSL_get_info_callback(s));
270155714Skris
270255714Skris	ret->debug=s->debug;
270355714Skris
270455714Skris	/* copy app data, a little dangerous perhaps */
2705109998Smarkm	if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data))
270655714Skris		goto err;
270755714Skris
270855714Skris	/* setup rbio, and wbio */
270955714Skris	if (s->rbio != NULL)
271055714Skris		{
271155714Skris		if (!BIO_dup_state(s->rbio,(char *)&ret->rbio))
271255714Skris			goto err;
271355714Skris		}
271455714Skris	if (s->wbio != NULL)
271555714Skris		{
271655714Skris		if (s->wbio != s->rbio)
271755714Skris			{
271855714Skris			if (!BIO_dup_state(s->wbio,(char *)&ret->wbio))
271955714Skris				goto err;
272055714Skris			}
272155714Skris		else
272255714Skris			ret->wbio=ret->rbio;
272355714Skris		}
2724109998Smarkm	ret->rwstate = s->rwstate;
2725109998Smarkm	ret->in_handshake = s->in_handshake;
2726109998Smarkm	ret->handshake_func = s->handshake_func;
2727109998Smarkm	ret->server = s->server;
2728238405Sjkim	ret->renegotiate = s->renegotiate;
2729109998Smarkm	ret->new_session = s->new_session;
2730109998Smarkm	ret->quiet_shutdown = s->quiet_shutdown;
2731109998Smarkm	ret->shutdown=s->shutdown;
2732109998Smarkm	ret->state=s->state; /* SSL_dup does not really work at any state, though */
2733109998Smarkm	ret->rstate=s->rstate;
2734109998Smarkm	ret->init_num = 0; /* would have to copy ret->init_buf, ret->init_msg, ret->init_num, ret->init_off */
2735109998Smarkm	ret->hit=s->hit;
273655714Skris
2737160814Ssimon	X509_VERIFY_PARAM_inherit(ret->param, s->param);
2738160814Ssimon
273955714Skris	/* dup the cipher_list and cipher_list_by_id stacks */
274055714Skris	if (s->cipher_list != NULL)
274155714Skris		{
274255714Skris		if ((ret->cipher_list=sk_SSL_CIPHER_dup(s->cipher_list)) == NULL)
274355714Skris			goto err;
274455714Skris		}
274555714Skris	if (s->cipher_list_by_id != NULL)
274655714Skris		if ((ret->cipher_list_by_id=sk_SSL_CIPHER_dup(s->cipher_list_by_id))
274755714Skris			== NULL)
274855714Skris			goto err;
274955714Skris
275055714Skris	/* Dup the client_CA list */
275155714Skris	if (s->client_CA != NULL)
275255714Skris		{
275355714Skris		if ((sk=sk_X509_NAME_dup(s->client_CA)) == NULL) goto err;
275455714Skris		ret->client_CA=sk;
275555714Skris		for (i=0; i<sk_X509_NAME_num(sk); i++)
275655714Skris			{
275755714Skris			xn=sk_X509_NAME_value(sk,i);
275855714Skris			if (sk_X509_NAME_set(sk,i,X509_NAME_dup(xn)) == NULL)
275955714Skris				{
276055714Skris				X509_NAME_free(xn);
276155714Skris				goto err;
276255714Skris				}
276355714Skris			}
276455714Skris		}
276555714Skris
276655714Skris	if (0)
276755714Skris		{
276855714Skriserr:
276955714Skris		if (ret != NULL) SSL_free(ret);
277055714Skris		ret=NULL;
277155714Skris		}
277255714Skris	return(ret);
277355714Skris	}
277455714Skris
277555714Skrisvoid ssl_clear_cipher_ctx(SSL *s)
277655714Skris	{
277755714Skris	if (s->enc_read_ctx != NULL)
277855714Skris		{
277955714Skris		EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
278068651Skris		OPENSSL_free(s->enc_read_ctx);
278155714Skris		s->enc_read_ctx=NULL;
278255714Skris		}
278355714Skris	if (s->enc_write_ctx != NULL)
278455714Skris		{
278555714Skris		EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
278668651Skris		OPENSSL_free(s->enc_write_ctx);
278755714Skris		s->enc_write_ctx=NULL;
278855714Skris		}
2789160814Ssimon#ifndef OPENSSL_NO_COMP
279055714Skris	if (s->expand != NULL)
279155714Skris		{
279255714Skris		COMP_CTX_free(s->expand);
279355714Skris		s->expand=NULL;
279455714Skris		}
279555714Skris	if (s->compress != NULL)
279655714Skris		{
279755714Skris		COMP_CTX_free(s->compress);
279855714Skris		s->compress=NULL;
279955714Skris		}
2800160814Ssimon#endif
280155714Skris	}
280255714Skris
280355714Skris/* Fix this function so that it takes an optional type parameter */
2804160814SsimonX509 *SSL_get_certificate(const SSL *s)
280555714Skris	{
2806264331Sjkim	if (s->cert != NULL)
280755714Skris		return(s->cert->key->x509);
280855714Skris	else
280955714Skris		return(NULL);
281055714Skris	}
281155714Skris
281255714Skris/* Fix this function so that it takes an optional type parameter */
281355714SkrisEVP_PKEY *SSL_get_privatekey(SSL *s)
281455714Skris	{
281555714Skris	if (s->cert != NULL)
281655714Skris		return(s->cert->key->privatekey);
281755714Skris	else
281855714Skris		return(NULL);
281955714Skris	}
282055714Skris
2821238405Sjkimconst SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
282255714Skris	{
282355714Skris	if ((s->session != NULL) && (s->session->cipher != NULL))
282455714Skris		return(s->session->cipher);
282555714Skris	return(NULL);
282655714Skris	}
2827160814Ssimon#ifdef OPENSSL_NO_COMP
2828160814Ssimonconst void *SSL_get_current_compression(SSL *s)
2829160814Ssimon	{
2830160814Ssimon	return NULL;
2831160814Ssimon	}
2832160814Ssimonconst void *SSL_get_current_expansion(SSL *s)
2833160814Ssimon	{
2834160814Ssimon	return NULL;
2835160814Ssimon	}
2836160814Ssimon#else
283755714Skris
2838160814Ssimonconst COMP_METHOD *SSL_get_current_compression(SSL *s)
2839160814Ssimon	{
2840160814Ssimon	if (s->compress != NULL)
2841160814Ssimon		return(s->compress->meth);
2842160814Ssimon	return(NULL);
2843160814Ssimon	}
2844160814Ssimon
2845160814Ssimonconst COMP_METHOD *SSL_get_current_expansion(SSL *s)
2846160814Ssimon	{
2847160814Ssimon	if (s->expand != NULL)
2848160814Ssimon		return(s->expand->meth);
2849160814Ssimon	return(NULL);
2850160814Ssimon	}
2851160814Ssimon#endif
2852160814Ssimon
285355714Skrisint ssl_init_wbio_buffer(SSL *s,int push)
285455714Skris	{
285555714Skris	BIO *bbio;
285655714Skris
285755714Skris	if (s->bbio == NULL)
285855714Skris		{
285955714Skris		bbio=BIO_new(BIO_f_buffer());
286055714Skris		if (bbio == NULL) return(0);
286155714Skris		s->bbio=bbio;
286255714Skris		}
286355714Skris	else
286455714Skris		{
286555714Skris		bbio=s->bbio;
286655714Skris		if (s->bbio == s->wbio)
286755714Skris			s->wbio=BIO_pop(s->wbio);
286855714Skris		}
286955714Skris	(void)BIO_reset(bbio);
287055714Skris/*	if (!BIO_set_write_buffer_size(bbio,16*1024)) */
287155714Skris	if (!BIO_set_read_buffer_size(bbio,1))
287255714Skris		{
287355714Skris		SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER,ERR_R_BUF_LIB);
287455714Skris		return(0);
287555714Skris		}
287655714Skris	if (push)
287755714Skris		{
287855714Skris		if (s->wbio != bbio)
287955714Skris			s->wbio=BIO_push(bbio,s->wbio);
288055714Skris		}
288155714Skris	else
288255714Skris		{
288355714Skris		if (s->wbio == bbio)
288455714Skris			s->wbio=BIO_pop(bbio);
288555714Skris		}
288655714Skris	return(1);
288755714Skris	}
288855714Skris
288955714Skrisvoid ssl_free_wbio_buffer(SSL *s)
289055714Skris	{
289155714Skris	if (s->bbio == NULL) return;
289255714Skris
289355714Skris	if (s->bbio == s->wbio)
289455714Skris		{
289555714Skris		/* remove buffering */
289668651Skris		s->wbio=BIO_pop(s->wbio);
289768651Skris#ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
289868651Skris		assert(s->wbio != NULL);
2899238405Sjkim#endif
290068651Skris	}
290155714Skris	BIO_free(s->bbio);
290255714Skris	s->bbio=NULL;
290355714Skris	}
290455714Skris
290555714Skrisvoid SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
290655714Skris	{
290755714Skris	ctx->quiet_shutdown=mode;
290855714Skris	}
290955714Skris
2910160814Ssimonint SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
291155714Skris	{
291255714Skris	return(ctx->quiet_shutdown);
291355714Skris	}
291455714Skris
291555714Skrisvoid SSL_set_quiet_shutdown(SSL *s,int mode)
291655714Skris	{
291755714Skris	s->quiet_shutdown=mode;
291855714Skris	}
291955714Skris
2920160814Ssimonint SSL_get_quiet_shutdown(const SSL *s)
292155714Skris	{
292255714Skris	return(s->quiet_shutdown);
292355714Skris	}
292455714Skris
292555714Skrisvoid SSL_set_shutdown(SSL *s,int mode)
292655714Skris	{
292755714Skris	s->shutdown=mode;
292855714Skris	}
292955714Skris
2930160814Ssimonint SSL_get_shutdown(const SSL *s)
293155714Skris	{
293255714Skris	return(s->shutdown);
293355714Skris	}
293455714Skris
2935160814Ssimonint SSL_version(const SSL *s)
293655714Skris	{
293755714Skris	return(s->version);
293855714Skris	}
293955714Skris
2940160814SsimonSSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
294155714Skris	{
294255714Skris	return(ssl->ctx);
294355714Skris	}
294455714Skris
2945194206SsimonSSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
2946194206Ssimon	{
2947194206Ssimon	if (ssl->ctx == ctx)
2948194206Ssimon		return ssl->ctx;
2949194206Ssimon#ifndef OPENSSL_NO_TLSEXT
2950194206Ssimon	if (ctx == NULL)
2951194206Ssimon		ctx = ssl->initial_ctx;
2952194206Ssimon#endif
2953194206Ssimon	if (ssl->cert != NULL)
2954194206Ssimon		ssl_cert_free(ssl->cert);
2955194206Ssimon	ssl->cert = ssl_cert_dup(ctx->cert);
2956194206Ssimon	CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
2957194206Ssimon	if (ssl->ctx != NULL)
2958194206Ssimon		SSL_CTX_free(ssl->ctx); /* decrement reference count */
2959194206Ssimon	ssl->ctx = ctx;
2960194206Ssimon	return(ssl->ctx);
2961194206Ssimon	}
2962194206Ssimon
2963109998Smarkm#ifndef OPENSSL_NO_STDIO
296455714Skrisint SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
296555714Skris	{
296655714Skris	return(X509_STORE_set_default_paths(ctx->cert_store));
296755714Skris	}
296855714Skris
296955714Skrisint SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
297055714Skris		const char *CApath)
297155714Skris	{
2972160814Ssimon	return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
297355714Skris	}
297455714Skris#endif
297555714Skris
2976109998Smarkmvoid SSL_set_info_callback(SSL *ssl,
2977167612Ssimon	void (*cb)(const SSL *ssl,int type,int val))
297855714Skris	{
297955714Skris	ssl->info_callback=cb;
298055714Skris	}
298155714Skris
2982160814Ssimon/* One compiler (Diab DCC) doesn't like argument names in returned
2983160814Ssimon   function pointer.  */
2984238405Sjkimvoid (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
298555714Skris	{
2986109998Smarkm	return ssl->info_callback;
298755714Skris	}
298855714Skris
2989160814Ssimonint SSL_state(const SSL *ssl)
299055714Skris	{
299155714Skris	return(ssl->state);
299255714Skris	}
299355714Skris
2994238405Sjkimvoid SSL_set_state(SSL *ssl, int state)
2995238405Sjkim	{
2996238405Sjkim	ssl->state = state;
2997238405Sjkim	}
2998238405Sjkim
299955714Skrisvoid SSL_set_verify_result(SSL *ssl,long arg)
300055714Skris	{
300155714Skris	ssl->verify_result=arg;
300255714Skris	}
300355714Skris
3004160814Ssimonlong SSL_get_verify_result(const SSL *ssl)
300555714Skris	{
300655714Skris	return(ssl->verify_result);
300755714Skris	}
300855714Skris
300959191Skrisint SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
301059191Skris			 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
301155714Skris	{
3012109998Smarkm	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
3013109998Smarkm				new_func, dup_func, free_func);
301455714Skris	}
301555714Skris
301655714Skrisint SSL_set_ex_data(SSL *s,int idx,void *arg)
301755714Skris	{
301855714Skris	return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
301955714Skris	}
302055714Skris
3021160814Ssimonvoid *SSL_get_ex_data(const SSL *s,int idx)
302255714Skris	{
302355714Skris	return(CRYPTO_get_ex_data(&s->ex_data,idx));
302455714Skris	}
302555714Skris
302659191Skrisint SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
302759191Skris			     CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
302855714Skris	{
3029109998Smarkm	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
3030109998Smarkm				new_func, dup_func, free_func);
303155714Skris	}
303255714Skris
303355714Skrisint SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
303455714Skris	{
303555714Skris	return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
303655714Skris	}
303755714Skris
3038160814Ssimonvoid *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx)
303955714Skris	{
304055714Skris	return(CRYPTO_get_ex_data(&s->ex_data,idx));
304155714Skris	}
304255714Skris
304355714Skrisint ssl_ok(SSL *s)
304455714Skris	{
304555714Skris	return(1);
304655714Skris	}
304755714Skris
3048160814SsimonX509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
304955714Skris	{
305055714Skris	return(ctx->cert_store);
305155714Skris	}
305255714Skris
305355714Skrisvoid SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
305455714Skris	{
305555714Skris	if (ctx->cert_store != NULL)
305655714Skris		X509_STORE_free(ctx->cert_store);
305755714Skris	ctx->cert_store=store;
305855714Skris	}
305955714Skris
3060160814Ssimonint SSL_want(const SSL *s)
306155714Skris	{
306255714Skris	return(s->rwstate);
306355714Skris	}
306455714Skris
306555714Skris/*!
306655714Skris * \brief Set the callback for generating temporary RSA keys.
306755714Skris * \param ctx the SSL context.
306855714Skris * \param cb the callback
306955714Skris */
307055714Skris
3071109998Smarkm#ifndef OPENSSL_NO_RSA
307255714Skrisvoid SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
307355714Skris							  int is_export,
307455714Skris							  int keylength))
307559191Skris    {
3076160814Ssimon    SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
307759191Skris    }
307855714Skris
307959191Skrisvoid SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
308059191Skris						  int is_export,
308159191Skris						  int keylength))
308259191Skris    {
3083160814Ssimon    SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
308459191Skris    }
308555714Skris#endif
308655714Skris
308755714Skris#ifdef DOXYGEN
308855714Skris/*!
308955714Skris * \brief The RSA temporary key callback function.
309055714Skris * \param ssl the SSL session.
309155714Skris * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
309255714Skris * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
309355714Skris * of the required key in bits.
309455714Skris * \return the temporary RSA key.
309555714Skris * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
309655714Skris */
309755714Skris
309855714SkrisRSA *cb(SSL *ssl,int is_export,int keylength)
309955714Skris    {}
310055714Skris#endif
310155714Skris
310255714Skris/*!
310355714Skris * \brief Set the callback for generating temporary DH keys.
310455714Skris * \param ctx the SSL context.
310555714Skris * \param dh the callback
310655714Skris */
310755714Skris
3108109998Smarkm#ifndef OPENSSL_NO_DH
310955714Skrisvoid SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
3110238405Sjkim                                                        int keylength))
3111109998Smarkm	{
3112160814Ssimon	SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
3113109998Smarkm	}
311455714Skris
311555714Skrisvoid SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
3116238405Sjkim                                                int keylength))
3117109998Smarkm	{
3118160814Ssimon	SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
3119109998Smarkm	}
312055714Skris#endif
312155714Skris
3122160814Ssimon#ifndef OPENSSL_NO_ECDH
3123160814Ssimonvoid SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
3124238405Sjkim                                                                int keylength))
3125160814Ssimon	{
3126160814Ssimon	SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
3127160814Ssimon	}
3128109998Smarkm
3129160814Ssimonvoid SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
3130238405Sjkim                                                        int keylength))
3131160814Ssimon	{
3132160814Ssimon	SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
3133160814Ssimon	}
3134160814Ssimon#endif
3135160814Ssimon
3136238405Sjkim#ifndef OPENSSL_NO_PSK
3137238405Sjkimint SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
3138238405Sjkim	{
3139238405Sjkim	if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
3140238405Sjkim		{
3141238405Sjkim		SSLerr(SSL_F_SSL_CTX_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG);
3142238405Sjkim		return 0;
3143238405Sjkim		}
3144238405Sjkim	if (ctx->psk_identity_hint != NULL)
3145238405Sjkim		OPENSSL_free(ctx->psk_identity_hint);
3146238405Sjkim	if (identity_hint != NULL)
3147238405Sjkim		{
3148238405Sjkim		ctx->psk_identity_hint = BUF_strdup(identity_hint);
3149238405Sjkim		if (ctx->psk_identity_hint == NULL)
3150238405Sjkim			return 0;
3151238405Sjkim		}
3152238405Sjkim	else
3153238405Sjkim		ctx->psk_identity_hint = NULL;
3154238405Sjkim	return 1;
3155238405Sjkim	}
3156160814Ssimon
3157238405Sjkimint SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
3158238405Sjkim	{
3159238405Sjkim	if (s == NULL)
3160238405Sjkim		return 0;
3161238405Sjkim
3162238405Sjkim	if (s->session == NULL)
3163238405Sjkim		return 1; /* session not created yet, ignored */
3164238405Sjkim
3165238405Sjkim	if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
3166238405Sjkim		{
3167238405Sjkim		SSLerr(SSL_F_SSL_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG);
3168238405Sjkim		return 0;
3169238405Sjkim		}
3170238405Sjkim	if (s->session->psk_identity_hint != NULL)
3171238405Sjkim		OPENSSL_free(s->session->psk_identity_hint);
3172238405Sjkim	if (identity_hint != NULL)
3173238405Sjkim		{
3174238405Sjkim		s->session->psk_identity_hint = BUF_strdup(identity_hint);
3175238405Sjkim		if (s->session->psk_identity_hint == NULL)
3176238405Sjkim			return 0;
3177238405Sjkim		}
3178238405Sjkim	else
3179238405Sjkim		s->session->psk_identity_hint = NULL;
3180238405Sjkim	return 1;
3181238405Sjkim	}
3182238405Sjkim
3183238405Sjkimconst char *SSL_get_psk_identity_hint(const SSL *s)
3184238405Sjkim	{
3185238405Sjkim	if (s == NULL || s->session == NULL)
3186238405Sjkim		return NULL;
3187238405Sjkim	return(s->session->psk_identity_hint);
3188238405Sjkim	}
3189238405Sjkim
3190238405Sjkimconst char *SSL_get_psk_identity(const SSL *s)
3191238405Sjkim	{
3192238405Sjkim	if (s == NULL || s->session == NULL)
3193238405Sjkim		return NULL;
3194238405Sjkim	return(s->session->psk_identity);
3195238405Sjkim	}
3196238405Sjkim
3197238405Sjkimvoid SSL_set_psk_client_callback(SSL *s,
3198238405Sjkim    unsigned int (*cb)(SSL *ssl, const char *hint,
3199238405Sjkim                       char *identity, unsigned int max_identity_len, unsigned char *psk,
3200238405Sjkim                       unsigned int max_psk_len))
3201238405Sjkim	{
3202238405Sjkim	s->psk_client_callback = cb;
3203238405Sjkim	}
3204238405Sjkim
3205238405Sjkimvoid SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
3206238405Sjkim    unsigned int (*cb)(SSL *ssl, const char *hint,
3207238405Sjkim                       char *identity, unsigned int max_identity_len, unsigned char *psk,
3208238405Sjkim                       unsigned int max_psk_len))
3209238405Sjkim	{
3210238405Sjkim	ctx->psk_client_callback = cb;
3211238405Sjkim	}
3212238405Sjkim
3213238405Sjkimvoid SSL_set_psk_server_callback(SSL *s,
3214238405Sjkim    unsigned int (*cb)(SSL *ssl, const char *identity,
3215238405Sjkim                       unsigned char *psk, unsigned int max_psk_len))
3216238405Sjkim	{
3217238405Sjkim	s->psk_server_callback = cb;
3218238405Sjkim	}
3219238405Sjkim
3220238405Sjkimvoid SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
3221238405Sjkim    unsigned int (*cb)(SSL *ssl, const char *identity,
3222238405Sjkim                       unsigned char *psk, unsigned int max_psk_len))
3223238405Sjkim	{
3224238405Sjkim	ctx->psk_server_callback = cb;
3225238405Sjkim	}
3226238405Sjkim#endif
3227238405Sjkim
3228109998Smarkmvoid 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))
3229109998Smarkm	{
3230160814Ssimon	SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3231109998Smarkm	}
3232109998Smarkmvoid 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))
3233109998Smarkm	{
3234160814Ssimon	SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3235109998Smarkm	}
3236109998Smarkm
3237238405Sjkim/* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
3238238405Sjkim * vairable, freeing  EVP_MD_CTX previously stored in that variable, if
3239238405Sjkim * any. If EVP_MD pointer is passed, initializes ctx with this md
3240238405Sjkim * Returns newly allocated ctx;
3241238405Sjkim */
3242109998Smarkm
3243238405SjkimEVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md)
3244238405Sjkim{
3245238405Sjkim	ssl_clear_hash_ctx(hash);
3246238405Sjkim	*hash = EVP_MD_CTX_create();
3247238405Sjkim	if (md) EVP_DigestInit_ex(*hash,md,NULL);
3248238405Sjkim	return *hash;
3249238405Sjkim}
3250238405Sjkimvoid ssl_clear_hash_ctx(EVP_MD_CTX **hash)
3251238405Sjkim{
3252109998Smarkm
3253238405Sjkim	if (*hash) EVP_MD_CTX_destroy(*hash);
3254238405Sjkim	*hash=NULL;
3255238405Sjkim}
3256238405Sjkim
3257238405Sjkimvoid SSL_set_debug(SSL *s, int debug)
3258238405Sjkim	{
3259238405Sjkim	s->debug = debug;
3260238405Sjkim	}
3261238405Sjkim
3262238405Sjkimint SSL_cache_hit(SSL *s)
3263238405Sjkim	{
3264238405Sjkim	return s->hit;
3265238405Sjkim	}
3266238405Sjkim
3267109998Smarkm#if defined(_WINDLL) && defined(OPENSSL_SYS_WIN16)
326855714Skris#include "../crypto/bio/bss_file.c"
326955714Skris#endif
327055714Skris
327155714SkrisIMPLEMENT_STACK_OF(SSL_CIPHER)
327255714SkrisIMPLEMENT_STACK_OF(SSL_COMP)
3273238405SjkimIMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER,
3274238405Sjkim				    ssl_cipher_id);
3275