1*0Sstevel@tonic-gate /*! \file ssl/ssl_lib.c
2*0Sstevel@tonic-gate  *  \brief Version independent SSL functions.
3*0Sstevel@tonic-gate  */
4*0Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5*0Sstevel@tonic-gate  * All rights reserved.
6*0Sstevel@tonic-gate  *
7*0Sstevel@tonic-gate  * This package is an SSL implementation written
8*0Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
9*0Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
10*0Sstevel@tonic-gate  *
11*0Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
12*0Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
13*0Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
14*0Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
15*0Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
16*0Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
17*0Sstevel@tonic-gate  *
18*0Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
19*0Sstevel@tonic-gate  * the code are not to be removed.
20*0Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
21*0Sstevel@tonic-gate  * as the author of the parts of the library used.
22*0Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
23*0Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
24*0Sstevel@tonic-gate  *
25*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
26*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
27*0Sstevel@tonic-gate  * are met:
28*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
29*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
30*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
31*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
32*0Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
33*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
34*0Sstevel@tonic-gate  *    must display the following acknowledgement:
35*0Sstevel@tonic-gate  *    "This product includes cryptographic software written by
36*0Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
37*0Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
38*0Sstevel@tonic-gate  *    being used are not cryptographic related :-).
39*0Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
40*0Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
41*0Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
42*0Sstevel@tonic-gate  *
43*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
44*0Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46*0Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47*0Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48*0Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49*0Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51*0Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52*0Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53*0Sstevel@tonic-gate  * SUCH DAMAGE.
54*0Sstevel@tonic-gate  *
55*0Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
56*0Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
57*0Sstevel@tonic-gate  * copied and put under another distribution licence
58*0Sstevel@tonic-gate  * [including the GNU Public Licence.]
59*0Sstevel@tonic-gate  */
60*0Sstevel@tonic-gate /* ====================================================================
61*0Sstevel@tonic-gate  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
62*0Sstevel@tonic-gate  *
63*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
64*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
65*0Sstevel@tonic-gate  * are met:
66*0Sstevel@tonic-gate  *
67*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
68*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
69*0Sstevel@tonic-gate  *
70*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
71*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
72*0Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
73*0Sstevel@tonic-gate  *    distribution.
74*0Sstevel@tonic-gate  *
75*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
76*0Sstevel@tonic-gate  *    software must display the following acknowledgment:
77*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
78*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
79*0Sstevel@tonic-gate  *
80*0Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
81*0Sstevel@tonic-gate  *    endorse or promote products derived from this software without
82*0Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
83*0Sstevel@tonic-gate  *    openssl-core@openssl.org.
84*0Sstevel@tonic-gate  *
85*0Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
86*0Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
87*0Sstevel@tonic-gate  *    permission of the OpenSSL Project.
88*0Sstevel@tonic-gate  *
89*0Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
90*0Sstevel@tonic-gate  *    acknowledgment:
91*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
92*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
93*0Sstevel@tonic-gate  *
94*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
95*0Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
96*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
97*0Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
98*0Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
99*0Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
100*0Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
101*0Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
102*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
103*0Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
104*0Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
105*0Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
106*0Sstevel@tonic-gate  * ====================================================================
107*0Sstevel@tonic-gate  *
108*0Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
109*0Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
110*0Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
111*0Sstevel@tonic-gate  *
112*0Sstevel@tonic-gate  */
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate #ifdef REF_CHECK
116*0Sstevel@tonic-gate #  include <assert.h>
117*0Sstevel@tonic-gate #endif
118*0Sstevel@tonic-gate #include <stdio.h>
119*0Sstevel@tonic-gate #include "ssl_locl.h"
120*0Sstevel@tonic-gate #include "kssl_lcl.h"
121*0Sstevel@tonic-gate #include <openssl/objects.h>
122*0Sstevel@tonic-gate #include <openssl/lhash.h>
123*0Sstevel@tonic-gate #include <openssl/x509v3.h>
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate const char *SSL_version_str=OPENSSL_VERSION_TEXT;
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate OPENSSL_GLOBAL SSL3_ENC_METHOD ssl3_undef_enc_method={
128*0Sstevel@tonic-gate 	/* evil casts, but these functions are only called if there's a library bug */
129*0Sstevel@tonic-gate 	(int (*)(SSL *,int))ssl_undefined_function,
130*0Sstevel@tonic-gate 	(int (*)(SSL *, unsigned char *, int))ssl_undefined_function,
131*0Sstevel@tonic-gate 	ssl_undefined_function,
132*0Sstevel@tonic-gate 	(int (*)(SSL *, unsigned char *, unsigned char *, int))ssl_undefined_function,
133*0Sstevel@tonic-gate 	(int (*)(SSL*, int))ssl_undefined_function,
134*0Sstevel@tonic-gate 	(int (*)(SSL *, EVP_MD_CTX *, EVP_MD_CTX *, const char*, int, unsigned char *))ssl_undefined_function
135*0Sstevel@tonic-gate 	};
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate int SSL_clear(SSL *s)
138*0Sstevel@tonic-gate 	{
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 	if (s->method == NULL)
141*0Sstevel@tonic-gate 		{
142*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_CLEAR,SSL_R_NO_METHOD_SPECIFIED);
143*0Sstevel@tonic-gate 		return(0);
144*0Sstevel@tonic-gate 		}
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 	if (ssl_clear_bad_session(s))
147*0Sstevel@tonic-gate 		{
148*0Sstevel@tonic-gate 		SSL_SESSION_free(s->session);
149*0Sstevel@tonic-gate 		s->session=NULL;
150*0Sstevel@tonic-gate 		}
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	s->error=0;
153*0Sstevel@tonic-gate 	s->hit=0;
154*0Sstevel@tonic-gate 	s->shutdown=0;
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate #if 0 /* Disabled since version 1.10 of this file (early return not
157*0Sstevel@tonic-gate        * needed because SSL_clear is not called when doing renegotiation) */
158*0Sstevel@tonic-gate 	/* This is set if we are doing dynamic renegotiation so keep
159*0Sstevel@tonic-gate 	 * the old cipher.  It is sort of a SSL_clear_lite :-) */
160*0Sstevel@tonic-gate 	if (s->new_session) return(1);
161*0Sstevel@tonic-gate #else
162*0Sstevel@tonic-gate 	if (s->new_session)
163*0Sstevel@tonic-gate 		{
164*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_CLEAR,ERR_R_INTERNAL_ERROR);
165*0Sstevel@tonic-gate 		return 0;
166*0Sstevel@tonic-gate 		}
167*0Sstevel@tonic-gate #endif
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 	s->type=0;
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	s->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT);
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 	s->version=s->method->version;
174*0Sstevel@tonic-gate 	s->client_version=s->version;
175*0Sstevel@tonic-gate 	s->rwstate=SSL_NOTHING;
176*0Sstevel@tonic-gate 	s->rstate=SSL_ST_READ_HEADER;
177*0Sstevel@tonic-gate #if 0
178*0Sstevel@tonic-gate 	s->read_ahead=s->ctx->read_ahead;
179*0Sstevel@tonic-gate #endif
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	if (s->init_buf != NULL)
182*0Sstevel@tonic-gate 		{
183*0Sstevel@tonic-gate 		BUF_MEM_free(s->init_buf);
184*0Sstevel@tonic-gate 		s->init_buf=NULL;
185*0Sstevel@tonic-gate 		}
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 	ssl_clear_cipher_ctx(s);
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 	s->first_packet=0;
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate #if 1
192*0Sstevel@tonic-gate 	/* Check to see if we were changed into a different method, if
193*0Sstevel@tonic-gate 	 * so, revert back if we are not doing session-id reuse. */
194*0Sstevel@tonic-gate 	if (!s->in_handshake && (s->session == NULL) && (s->method != s->ctx->method))
195*0Sstevel@tonic-gate 		{
196*0Sstevel@tonic-gate 		s->method->ssl_free(s);
197*0Sstevel@tonic-gate 		s->method=s->ctx->method;
198*0Sstevel@tonic-gate 		if (!s->method->ssl_new(s))
199*0Sstevel@tonic-gate 			return(0);
200*0Sstevel@tonic-gate 		}
201*0Sstevel@tonic-gate 	else
202*0Sstevel@tonic-gate #endif
203*0Sstevel@tonic-gate 		s->method->ssl_clear(s);
204*0Sstevel@tonic-gate 	return(1);
205*0Sstevel@tonic-gate 	}
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate /** Used to change an SSL_CTXs default SSL method type */
208*0Sstevel@tonic-gate int SSL_CTX_set_ssl_version(SSL_CTX *ctx,SSL_METHOD *meth)
209*0Sstevel@tonic-gate 	{
210*0Sstevel@tonic-gate 	STACK_OF(SSL_CIPHER) *sk;
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	ctx->method=meth;
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 	sk=ssl_create_cipher_list(ctx->method,&(ctx->cipher_list),
215*0Sstevel@tonic-gate 		&(ctx->cipher_list_by_id),SSL_DEFAULT_CIPHER_LIST);
216*0Sstevel@tonic-gate 	if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0))
217*0Sstevel@tonic-gate 		{
218*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION,SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
219*0Sstevel@tonic-gate 		return(0);
220*0Sstevel@tonic-gate 		}
221*0Sstevel@tonic-gate 	return(1);
222*0Sstevel@tonic-gate 	}
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate SSL *SSL_new(SSL_CTX *ctx)
225*0Sstevel@tonic-gate 	{
226*0Sstevel@tonic-gate 	SSL *s;
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 	if (ctx == NULL)
229*0Sstevel@tonic-gate 		{
230*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_NEW,SSL_R_NULL_SSL_CTX);
231*0Sstevel@tonic-gate 		return(NULL);
232*0Sstevel@tonic-gate 		}
233*0Sstevel@tonic-gate 	if (ctx->method == NULL)
234*0Sstevel@tonic-gate 		{
235*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_NEW,SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
236*0Sstevel@tonic-gate 		return(NULL);
237*0Sstevel@tonic-gate 		}
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 	s=(SSL *)OPENSSL_malloc(sizeof(SSL));
240*0Sstevel@tonic-gate 	if (s == NULL) goto err;
241*0Sstevel@tonic-gate 	memset(s,0,sizeof(SSL));
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate #ifndef	OPENSSL_NO_KRB5
244*0Sstevel@tonic-gate 	s->kssl_ctx = kssl_ctx_new();
245*0Sstevel@tonic-gate #endif	/* OPENSSL_NO_KRB5 */
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 	s->options=ctx->options;
248*0Sstevel@tonic-gate 	s->mode=ctx->mode;
249*0Sstevel@tonic-gate 	s->max_cert_list=ctx->max_cert_list;
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 	if (ctx->cert != NULL)
252*0Sstevel@tonic-gate 		{
253*0Sstevel@tonic-gate 		/* Earlier library versions used to copy the pointer to
254*0Sstevel@tonic-gate 		 * the CERT, not its contents; only when setting new
255*0Sstevel@tonic-gate 		 * parameters for the per-SSL copy, ssl_cert_new would be
256*0Sstevel@tonic-gate 		 * called (and the direct reference to the per-SSL_CTX
257*0Sstevel@tonic-gate 		 * settings would be lost, but those still were indirectly
258*0Sstevel@tonic-gate 		 * accessed for various purposes, and for that reason they
259*0Sstevel@tonic-gate 		 * used to be known as s->ctx->default_cert).
260*0Sstevel@tonic-gate 		 * Now we don't look at the SSL_CTX's CERT after having
261*0Sstevel@tonic-gate 		 * duplicated it once. */
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate 		s->cert = ssl_cert_dup(ctx->cert);
264*0Sstevel@tonic-gate 		if (s->cert == NULL)
265*0Sstevel@tonic-gate 			goto err;
266*0Sstevel@tonic-gate 		}
267*0Sstevel@tonic-gate 	else
268*0Sstevel@tonic-gate 		s->cert=NULL; /* Cannot really happen (see SSL_CTX_new) */
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 	s->read_ahead=ctx->read_ahead;
271*0Sstevel@tonic-gate 	s->msg_callback=ctx->msg_callback;
272*0Sstevel@tonic-gate 	s->msg_callback_arg=ctx->msg_callback_arg;
273*0Sstevel@tonic-gate 	s->verify_mode=ctx->verify_mode;
274*0Sstevel@tonic-gate 	s->verify_depth=ctx->verify_depth;
275*0Sstevel@tonic-gate 	s->sid_ctx_length=ctx->sid_ctx_length;
276*0Sstevel@tonic-gate 	OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx);
277*0Sstevel@tonic-gate 	memcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));
278*0Sstevel@tonic-gate 	s->verify_callback=ctx->default_verify_callback;
279*0Sstevel@tonic-gate 	s->generate_session_id=ctx->generate_session_id;
280*0Sstevel@tonic-gate 	s->purpose = ctx->purpose;
281*0Sstevel@tonic-gate 	s->trust = ctx->trust;
282*0Sstevel@tonic-gate 	s->quiet_shutdown=ctx->quiet_shutdown;
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate 	CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
285*0Sstevel@tonic-gate 	s->ctx=ctx;
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate 	s->verify_result=X509_V_OK;
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate 	s->method=ctx->method;
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate 	if (!s->method->ssl_new(s))
292*0Sstevel@tonic-gate 		goto err;
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate 	s->references=1;
295*0Sstevel@tonic-gate 	s->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate 	SSL_clear(s);
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate 	return(s);
302*0Sstevel@tonic-gate err:
303*0Sstevel@tonic-gate 	if (s != NULL)
304*0Sstevel@tonic-gate 		{
305*0Sstevel@tonic-gate 		if (s->cert != NULL)
306*0Sstevel@tonic-gate 			ssl_cert_free(s->cert);
307*0Sstevel@tonic-gate 		if (s->ctx != NULL)
308*0Sstevel@tonic-gate 			SSL_CTX_free(s->ctx); /* decrement reference count */
309*0Sstevel@tonic-gate 		OPENSSL_free(s);
310*0Sstevel@tonic-gate 		}
311*0Sstevel@tonic-gate 	SSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE);
312*0Sstevel@tonic-gate 	return(NULL);
313*0Sstevel@tonic-gate 	}
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate int SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx,
316*0Sstevel@tonic-gate 				   unsigned int sid_ctx_len)
317*0Sstevel@tonic-gate     {
318*0Sstevel@tonic-gate     if(sid_ctx_len > sizeof ctx->sid_ctx)
319*0Sstevel@tonic-gate 	{
320*0Sstevel@tonic-gate 	SSLerr(SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
321*0Sstevel@tonic-gate 	return 0;
322*0Sstevel@tonic-gate 	}
323*0Sstevel@tonic-gate     ctx->sid_ctx_length=sid_ctx_len;
324*0Sstevel@tonic-gate     memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len);
325*0Sstevel@tonic-gate 
326*0Sstevel@tonic-gate     return 1;
327*0Sstevel@tonic-gate     }
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate int SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx,
330*0Sstevel@tonic-gate 			       unsigned int sid_ctx_len)
331*0Sstevel@tonic-gate     {
332*0Sstevel@tonic-gate     if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
333*0Sstevel@tonic-gate 	{
334*0Sstevel@tonic-gate 	SSLerr(SSL_F_SSL_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
335*0Sstevel@tonic-gate 	return 0;
336*0Sstevel@tonic-gate 	}
337*0Sstevel@tonic-gate     ssl->sid_ctx_length=sid_ctx_len;
338*0Sstevel@tonic-gate     memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len);
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate     return 1;
341*0Sstevel@tonic-gate     }
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
344*0Sstevel@tonic-gate 	{
345*0Sstevel@tonic-gate 	CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
346*0Sstevel@tonic-gate 	ctx->generate_session_id = cb;
347*0Sstevel@tonic-gate 	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
348*0Sstevel@tonic-gate 	return 1;
349*0Sstevel@tonic-gate 	}
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
352*0Sstevel@tonic-gate 	{
353*0Sstevel@tonic-gate 	CRYPTO_w_lock(CRYPTO_LOCK_SSL);
354*0Sstevel@tonic-gate 	ssl->generate_session_id = cb;
355*0Sstevel@tonic-gate 	CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
356*0Sstevel@tonic-gate 	return 1;
357*0Sstevel@tonic-gate 	}
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
360*0Sstevel@tonic-gate 				unsigned int id_len)
361*0Sstevel@tonic-gate 	{
362*0Sstevel@tonic-gate 	/* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
363*0Sstevel@tonic-gate 	 * we can "construct" a session to give us the desired check - ie. to
364*0Sstevel@tonic-gate 	 * find if there's a session in the hash table that would conflict with
365*0Sstevel@tonic-gate 	 * any new session built out of this id/id_len and the ssl_version in
366*0Sstevel@tonic-gate 	 * use by this SSL. */
367*0Sstevel@tonic-gate 	SSL_SESSION r, *p;
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate 	if(id_len > sizeof r.session_id)
370*0Sstevel@tonic-gate 		return 0;
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate 	r.ssl_version = ssl->version;
373*0Sstevel@tonic-gate 	r.session_id_length = id_len;
374*0Sstevel@tonic-gate 	memcpy(r.session_id, id, id_len);
375*0Sstevel@tonic-gate 	/* NB: SSLv2 always uses a fixed 16-byte session ID, so even if a
376*0Sstevel@tonic-gate 	 * callback is calling us to check the uniqueness of a shorter ID, it
377*0Sstevel@tonic-gate 	 * must be compared as a padded-out ID because that is what it will be
378*0Sstevel@tonic-gate 	 * converted to when the callback has finished choosing it. */
379*0Sstevel@tonic-gate 	if((r.ssl_version == SSL2_VERSION) &&
380*0Sstevel@tonic-gate 			(id_len < SSL2_SSL_SESSION_ID_LENGTH))
381*0Sstevel@tonic-gate 		{
382*0Sstevel@tonic-gate 		memset(r.session_id + id_len, 0,
383*0Sstevel@tonic-gate 			SSL2_SSL_SESSION_ID_LENGTH - id_len);
384*0Sstevel@tonic-gate 		r.session_id_length = SSL2_SSL_SESSION_ID_LENGTH;
385*0Sstevel@tonic-gate 		}
386*0Sstevel@tonic-gate 
387*0Sstevel@tonic-gate 	CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
388*0Sstevel@tonic-gate 	p = (SSL_SESSION *)lh_retrieve(ssl->ctx->sessions, &r);
389*0Sstevel@tonic-gate 	CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
390*0Sstevel@tonic-gate 	return (p != NULL);
391*0Sstevel@tonic-gate 	}
392*0Sstevel@tonic-gate 
393*0Sstevel@tonic-gate int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
394*0Sstevel@tonic-gate 	{
395*0Sstevel@tonic-gate 	return X509_PURPOSE_set(&s->purpose, purpose);
396*0Sstevel@tonic-gate 	}
397*0Sstevel@tonic-gate 
398*0Sstevel@tonic-gate int SSL_set_purpose(SSL *s, int purpose)
399*0Sstevel@tonic-gate 	{
400*0Sstevel@tonic-gate 	return X509_PURPOSE_set(&s->purpose, purpose);
401*0Sstevel@tonic-gate 	}
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate int SSL_CTX_set_trust(SSL_CTX *s, int trust)
404*0Sstevel@tonic-gate 	{
405*0Sstevel@tonic-gate 	return X509_TRUST_set(&s->trust, trust);
406*0Sstevel@tonic-gate 	}
407*0Sstevel@tonic-gate 
408*0Sstevel@tonic-gate int SSL_set_trust(SSL *s, int trust)
409*0Sstevel@tonic-gate 	{
410*0Sstevel@tonic-gate 	return X509_TRUST_set(&s->trust, trust);
411*0Sstevel@tonic-gate 	}
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate void SSL_free(SSL *s)
414*0Sstevel@tonic-gate 	{
415*0Sstevel@tonic-gate 	int i;
416*0Sstevel@tonic-gate 
417*0Sstevel@tonic-gate 	if(s == NULL)
418*0Sstevel@tonic-gate 	    return;
419*0Sstevel@tonic-gate 
420*0Sstevel@tonic-gate 	i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);
421*0Sstevel@tonic-gate #ifdef REF_PRINT
422*0Sstevel@tonic-gate 	REF_PRINT("SSL",s);
423*0Sstevel@tonic-gate #endif
424*0Sstevel@tonic-gate 	if (i > 0) return;
425*0Sstevel@tonic-gate #ifdef REF_CHECK
426*0Sstevel@tonic-gate 	if (i < 0)
427*0Sstevel@tonic-gate 		{
428*0Sstevel@tonic-gate 		fprintf(stderr,"SSL_free, bad reference count\n");
429*0Sstevel@tonic-gate 		abort(); /* ok */
430*0Sstevel@tonic-gate 		}
431*0Sstevel@tonic-gate #endif
432*0Sstevel@tonic-gate 
433*0Sstevel@tonic-gate 	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
434*0Sstevel@tonic-gate 
435*0Sstevel@tonic-gate 	if (s->bbio != NULL)
436*0Sstevel@tonic-gate 		{
437*0Sstevel@tonic-gate 		/* If the buffering BIO is in place, pop it off */
438*0Sstevel@tonic-gate 		if (s->bbio == s->wbio)
439*0Sstevel@tonic-gate 			{
440*0Sstevel@tonic-gate 			s->wbio=BIO_pop(s->wbio);
441*0Sstevel@tonic-gate 			}
442*0Sstevel@tonic-gate 		BIO_free(s->bbio);
443*0Sstevel@tonic-gate 		s->bbio=NULL;
444*0Sstevel@tonic-gate 		}
445*0Sstevel@tonic-gate 	if (s->rbio != NULL)
446*0Sstevel@tonic-gate 		BIO_free_all(s->rbio);
447*0Sstevel@tonic-gate 	if ((s->wbio != NULL) && (s->wbio != s->rbio))
448*0Sstevel@tonic-gate 		BIO_free_all(s->wbio);
449*0Sstevel@tonic-gate 
450*0Sstevel@tonic-gate 	if (s->init_buf != NULL) BUF_MEM_free(s->init_buf);
451*0Sstevel@tonic-gate 
452*0Sstevel@tonic-gate 	/* add extra stuff */
453*0Sstevel@tonic-gate 	if (s->cipher_list != NULL) sk_SSL_CIPHER_free(s->cipher_list);
454*0Sstevel@tonic-gate 	if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);
455*0Sstevel@tonic-gate 
456*0Sstevel@tonic-gate 	/* Make the next call work :-) */
457*0Sstevel@tonic-gate 	if (s->session != NULL)
458*0Sstevel@tonic-gate 		{
459*0Sstevel@tonic-gate 		ssl_clear_bad_session(s);
460*0Sstevel@tonic-gate 		SSL_SESSION_free(s->session);
461*0Sstevel@tonic-gate 		}
462*0Sstevel@tonic-gate 
463*0Sstevel@tonic-gate 	ssl_clear_cipher_ctx(s);
464*0Sstevel@tonic-gate 
465*0Sstevel@tonic-gate 	if (s->cert != NULL) ssl_cert_free(s->cert);
466*0Sstevel@tonic-gate 	/* Free up if allocated */
467*0Sstevel@tonic-gate 
468*0Sstevel@tonic-gate 	if (s->ctx) SSL_CTX_free(s->ctx);
469*0Sstevel@tonic-gate 
470*0Sstevel@tonic-gate 	if (s->client_CA != NULL)
471*0Sstevel@tonic-gate 		sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);
472*0Sstevel@tonic-gate 
473*0Sstevel@tonic-gate 	if (s->method != NULL) s->method->ssl_free(s);
474*0Sstevel@tonic-gate 
475*0Sstevel@tonic-gate #ifndef	OPENSSL_NO_KRB5
476*0Sstevel@tonic-gate 	if (s->kssl_ctx != NULL)
477*0Sstevel@tonic-gate 		kssl_ctx_free(s->kssl_ctx);
478*0Sstevel@tonic-gate #endif	/* OPENSSL_NO_KRB5 */
479*0Sstevel@tonic-gate 
480*0Sstevel@tonic-gate 	OPENSSL_free(s);
481*0Sstevel@tonic-gate 	}
482*0Sstevel@tonic-gate 
483*0Sstevel@tonic-gate void SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio)
484*0Sstevel@tonic-gate 	{
485*0Sstevel@tonic-gate 	/* If the output buffering BIO is still in place, remove it
486*0Sstevel@tonic-gate 	 */
487*0Sstevel@tonic-gate 	if (s->bbio != NULL)
488*0Sstevel@tonic-gate 		{
489*0Sstevel@tonic-gate 		if (s->wbio == s->bbio)
490*0Sstevel@tonic-gate 			{
491*0Sstevel@tonic-gate 			s->wbio=s->wbio->next_bio;
492*0Sstevel@tonic-gate 			s->bbio->next_bio=NULL;
493*0Sstevel@tonic-gate 			}
494*0Sstevel@tonic-gate 		}
495*0Sstevel@tonic-gate 	if ((s->rbio != NULL) && (s->rbio != rbio))
496*0Sstevel@tonic-gate 		BIO_free_all(s->rbio);
497*0Sstevel@tonic-gate 	if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
498*0Sstevel@tonic-gate 		BIO_free_all(s->wbio);
499*0Sstevel@tonic-gate 	s->rbio=rbio;
500*0Sstevel@tonic-gate 	s->wbio=wbio;
501*0Sstevel@tonic-gate 	}
502*0Sstevel@tonic-gate 
503*0Sstevel@tonic-gate BIO *SSL_get_rbio(SSL *s)
504*0Sstevel@tonic-gate 	{ return(s->rbio); }
505*0Sstevel@tonic-gate 
506*0Sstevel@tonic-gate BIO *SSL_get_wbio(SSL *s)
507*0Sstevel@tonic-gate 	{ return(s->wbio); }
508*0Sstevel@tonic-gate 
509*0Sstevel@tonic-gate int SSL_get_fd(SSL *s)
510*0Sstevel@tonic-gate 	{
511*0Sstevel@tonic-gate 	return(SSL_get_rfd(s));
512*0Sstevel@tonic-gate 	}
513*0Sstevel@tonic-gate 
514*0Sstevel@tonic-gate int SSL_get_rfd(SSL *s)
515*0Sstevel@tonic-gate 	{
516*0Sstevel@tonic-gate 	int ret= -1;
517*0Sstevel@tonic-gate 	BIO *b,*r;
518*0Sstevel@tonic-gate 
519*0Sstevel@tonic-gate 	b=SSL_get_rbio(s);
520*0Sstevel@tonic-gate 	r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
521*0Sstevel@tonic-gate 	if (r != NULL)
522*0Sstevel@tonic-gate 		BIO_get_fd(r,&ret);
523*0Sstevel@tonic-gate 	return(ret);
524*0Sstevel@tonic-gate 	}
525*0Sstevel@tonic-gate 
526*0Sstevel@tonic-gate int SSL_get_wfd(SSL *s)
527*0Sstevel@tonic-gate 	{
528*0Sstevel@tonic-gate 	int ret= -1;
529*0Sstevel@tonic-gate 	BIO *b,*r;
530*0Sstevel@tonic-gate 
531*0Sstevel@tonic-gate 	b=SSL_get_wbio(s);
532*0Sstevel@tonic-gate 	r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
533*0Sstevel@tonic-gate 	if (r != NULL)
534*0Sstevel@tonic-gate 		BIO_get_fd(r,&ret);
535*0Sstevel@tonic-gate 	return(ret);
536*0Sstevel@tonic-gate 	}
537*0Sstevel@tonic-gate 
538*0Sstevel@tonic-gate #ifndef OPENSSL_NO_SOCK
539*0Sstevel@tonic-gate int SSL_set_fd(SSL *s,int fd)
540*0Sstevel@tonic-gate 	{
541*0Sstevel@tonic-gate 	int ret=0;
542*0Sstevel@tonic-gate 	BIO *bio=NULL;
543*0Sstevel@tonic-gate 
544*0Sstevel@tonic-gate 	bio=BIO_new(BIO_s_socket());
545*0Sstevel@tonic-gate 
546*0Sstevel@tonic-gate 	if (bio == NULL)
547*0Sstevel@tonic-gate 		{
548*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_SET_FD,ERR_R_BUF_LIB);
549*0Sstevel@tonic-gate 		goto err;
550*0Sstevel@tonic-gate 		}
551*0Sstevel@tonic-gate 	BIO_set_fd(bio,fd,BIO_NOCLOSE);
552*0Sstevel@tonic-gate 	SSL_set_bio(s,bio,bio);
553*0Sstevel@tonic-gate 	ret=1;
554*0Sstevel@tonic-gate err:
555*0Sstevel@tonic-gate 	return(ret);
556*0Sstevel@tonic-gate 	}
557*0Sstevel@tonic-gate 
558*0Sstevel@tonic-gate int SSL_set_wfd(SSL *s,int fd)
559*0Sstevel@tonic-gate 	{
560*0Sstevel@tonic-gate 	int ret=0;
561*0Sstevel@tonic-gate 	BIO *bio=NULL;
562*0Sstevel@tonic-gate 
563*0Sstevel@tonic-gate 	if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET)
564*0Sstevel@tonic-gate 		|| ((int)BIO_get_fd(s->rbio,NULL) != fd))
565*0Sstevel@tonic-gate 		{
566*0Sstevel@tonic-gate 		bio=BIO_new(BIO_s_socket());
567*0Sstevel@tonic-gate 
568*0Sstevel@tonic-gate 		if (bio == NULL)
569*0Sstevel@tonic-gate 			{ SSLerr(SSL_F_SSL_SET_WFD,ERR_R_BUF_LIB); goto err; }
570*0Sstevel@tonic-gate 		BIO_set_fd(bio,fd,BIO_NOCLOSE);
571*0Sstevel@tonic-gate 		SSL_set_bio(s,SSL_get_rbio(s),bio);
572*0Sstevel@tonic-gate 		}
573*0Sstevel@tonic-gate 	else
574*0Sstevel@tonic-gate 		SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s));
575*0Sstevel@tonic-gate 	ret=1;
576*0Sstevel@tonic-gate err:
577*0Sstevel@tonic-gate 	return(ret);
578*0Sstevel@tonic-gate 	}
579*0Sstevel@tonic-gate 
580*0Sstevel@tonic-gate int SSL_set_rfd(SSL *s,int fd)
581*0Sstevel@tonic-gate 	{
582*0Sstevel@tonic-gate 	int ret=0;
583*0Sstevel@tonic-gate 	BIO *bio=NULL;
584*0Sstevel@tonic-gate 
585*0Sstevel@tonic-gate 	if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET)
586*0Sstevel@tonic-gate 		|| ((int)BIO_get_fd(s->wbio,NULL) != fd))
587*0Sstevel@tonic-gate 		{
588*0Sstevel@tonic-gate 		bio=BIO_new(BIO_s_socket());
589*0Sstevel@tonic-gate 
590*0Sstevel@tonic-gate 		if (bio == NULL)
591*0Sstevel@tonic-gate 			{
592*0Sstevel@tonic-gate 			SSLerr(SSL_F_SSL_SET_RFD,ERR_R_BUF_LIB);
593*0Sstevel@tonic-gate 			goto err;
594*0Sstevel@tonic-gate 			}
595*0Sstevel@tonic-gate 		BIO_set_fd(bio,fd,BIO_NOCLOSE);
596*0Sstevel@tonic-gate 		SSL_set_bio(s,bio,SSL_get_wbio(s));
597*0Sstevel@tonic-gate 		}
598*0Sstevel@tonic-gate 	else
599*0Sstevel@tonic-gate 		SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s));
600*0Sstevel@tonic-gate 	ret=1;
601*0Sstevel@tonic-gate err:
602*0Sstevel@tonic-gate 	return(ret);
603*0Sstevel@tonic-gate 	}
604*0Sstevel@tonic-gate #endif
605*0Sstevel@tonic-gate 
606*0Sstevel@tonic-gate 
607*0Sstevel@tonic-gate /* return length of latest Finished message we sent, copy to 'buf' */
608*0Sstevel@tonic-gate size_t SSL_get_finished(SSL *s, void *buf, size_t count)
609*0Sstevel@tonic-gate 	{
610*0Sstevel@tonic-gate 	size_t ret = 0;
611*0Sstevel@tonic-gate 
612*0Sstevel@tonic-gate 	if (s->s3 != NULL)
613*0Sstevel@tonic-gate 		{
614*0Sstevel@tonic-gate 		ret = s->s3->tmp.finish_md_len;
615*0Sstevel@tonic-gate 		if (count > ret)
616*0Sstevel@tonic-gate 			count = ret;
617*0Sstevel@tonic-gate 		memcpy(buf, s->s3->tmp.finish_md, count);
618*0Sstevel@tonic-gate 		}
619*0Sstevel@tonic-gate 	return ret;
620*0Sstevel@tonic-gate 	}
621*0Sstevel@tonic-gate 
622*0Sstevel@tonic-gate /* return length of latest Finished message we expected, copy to 'buf' */
623*0Sstevel@tonic-gate size_t SSL_get_peer_finished(SSL *s, void *buf, size_t count)
624*0Sstevel@tonic-gate 	{
625*0Sstevel@tonic-gate 	size_t ret = 0;
626*0Sstevel@tonic-gate 
627*0Sstevel@tonic-gate 	if (s->s3 != NULL)
628*0Sstevel@tonic-gate 		{
629*0Sstevel@tonic-gate 		ret = s->s3->tmp.peer_finish_md_len;
630*0Sstevel@tonic-gate 		if (count > ret)
631*0Sstevel@tonic-gate 			count = ret;
632*0Sstevel@tonic-gate 		memcpy(buf, s->s3->tmp.peer_finish_md, count);
633*0Sstevel@tonic-gate 		}
634*0Sstevel@tonic-gate 	return ret;
635*0Sstevel@tonic-gate 	}
636*0Sstevel@tonic-gate 
637*0Sstevel@tonic-gate 
638*0Sstevel@tonic-gate int SSL_get_verify_mode(SSL *s)
639*0Sstevel@tonic-gate 	{
640*0Sstevel@tonic-gate 	return(s->verify_mode);
641*0Sstevel@tonic-gate 	}
642*0Sstevel@tonic-gate 
643*0Sstevel@tonic-gate int SSL_get_verify_depth(SSL *s)
644*0Sstevel@tonic-gate 	{
645*0Sstevel@tonic-gate 	return(s->verify_depth);
646*0Sstevel@tonic-gate 	}
647*0Sstevel@tonic-gate 
648*0Sstevel@tonic-gate int (*SSL_get_verify_callback(SSL *s))(int,X509_STORE_CTX *)
649*0Sstevel@tonic-gate 	{
650*0Sstevel@tonic-gate 	return(s->verify_callback);
651*0Sstevel@tonic-gate 	}
652*0Sstevel@tonic-gate 
653*0Sstevel@tonic-gate int SSL_CTX_get_verify_mode(SSL_CTX *ctx)
654*0Sstevel@tonic-gate 	{
655*0Sstevel@tonic-gate 	return(ctx->verify_mode);
656*0Sstevel@tonic-gate 	}
657*0Sstevel@tonic-gate 
658*0Sstevel@tonic-gate int SSL_CTX_get_verify_depth(SSL_CTX *ctx)
659*0Sstevel@tonic-gate 	{
660*0Sstevel@tonic-gate 	return(ctx->verify_depth);
661*0Sstevel@tonic-gate 	}
662*0Sstevel@tonic-gate 
663*0Sstevel@tonic-gate int (*SSL_CTX_get_verify_callback(SSL_CTX *ctx))(int,X509_STORE_CTX *)
664*0Sstevel@tonic-gate 	{
665*0Sstevel@tonic-gate 	return(ctx->default_verify_callback);
666*0Sstevel@tonic-gate 	}
667*0Sstevel@tonic-gate 
668*0Sstevel@tonic-gate void SSL_set_verify(SSL *s,int mode,
669*0Sstevel@tonic-gate 		    int (*callback)(int ok,X509_STORE_CTX *ctx))
670*0Sstevel@tonic-gate 	{
671*0Sstevel@tonic-gate 	s->verify_mode=mode;
672*0Sstevel@tonic-gate 	if (callback != NULL)
673*0Sstevel@tonic-gate 		s->verify_callback=callback;
674*0Sstevel@tonic-gate 	}
675*0Sstevel@tonic-gate 
676*0Sstevel@tonic-gate void SSL_set_verify_depth(SSL *s,int depth)
677*0Sstevel@tonic-gate 	{
678*0Sstevel@tonic-gate 	s->verify_depth=depth;
679*0Sstevel@tonic-gate 	}
680*0Sstevel@tonic-gate 
681*0Sstevel@tonic-gate void SSL_set_read_ahead(SSL *s,int yes)
682*0Sstevel@tonic-gate 	{
683*0Sstevel@tonic-gate 	s->read_ahead=yes;
684*0Sstevel@tonic-gate 	}
685*0Sstevel@tonic-gate 
686*0Sstevel@tonic-gate int SSL_get_read_ahead(SSL *s)
687*0Sstevel@tonic-gate 	{
688*0Sstevel@tonic-gate 	return(s->read_ahead);
689*0Sstevel@tonic-gate 	}
690*0Sstevel@tonic-gate 
691*0Sstevel@tonic-gate int SSL_pending(SSL *s)
692*0Sstevel@tonic-gate 	{
693*0Sstevel@tonic-gate 	/* SSL_pending cannot work properly if read-ahead is enabled
694*0Sstevel@tonic-gate 	 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)),
695*0Sstevel@tonic-gate 	 * and it is impossible to fix since SSL_pending cannot report
696*0Sstevel@tonic-gate 	 * errors that may be observed while scanning the new data.
697*0Sstevel@tonic-gate 	 * (Note that SSL_pending() is often used as a boolean value,
698*0Sstevel@tonic-gate 	 * so we'd better not return -1.)
699*0Sstevel@tonic-gate 	 */
700*0Sstevel@tonic-gate 	return(s->method->ssl_pending(s));
701*0Sstevel@tonic-gate 	}
702*0Sstevel@tonic-gate 
703*0Sstevel@tonic-gate X509 *SSL_get_peer_certificate(SSL *s)
704*0Sstevel@tonic-gate 	{
705*0Sstevel@tonic-gate 	X509 *r;
706*0Sstevel@tonic-gate 
707*0Sstevel@tonic-gate 	if ((s == NULL) || (s->session == NULL))
708*0Sstevel@tonic-gate 		r=NULL;
709*0Sstevel@tonic-gate 	else
710*0Sstevel@tonic-gate 		r=s->session->peer;
711*0Sstevel@tonic-gate 
712*0Sstevel@tonic-gate 	if (r == NULL) return(r);
713*0Sstevel@tonic-gate 
714*0Sstevel@tonic-gate 	CRYPTO_add(&r->references,1,CRYPTO_LOCK_X509);
715*0Sstevel@tonic-gate 
716*0Sstevel@tonic-gate 	return(r);
717*0Sstevel@tonic-gate 	}
718*0Sstevel@tonic-gate 
719*0Sstevel@tonic-gate STACK_OF(X509) *SSL_get_peer_cert_chain(SSL *s)
720*0Sstevel@tonic-gate 	{
721*0Sstevel@tonic-gate 	STACK_OF(X509) *r;
722*0Sstevel@tonic-gate 
723*0Sstevel@tonic-gate 	if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL))
724*0Sstevel@tonic-gate 		r=NULL;
725*0Sstevel@tonic-gate 	else
726*0Sstevel@tonic-gate 		r=s->session->sess_cert->cert_chain;
727*0Sstevel@tonic-gate 
728*0Sstevel@tonic-gate 	/* If we are a client, cert_chain includes the peer's own
729*0Sstevel@tonic-gate 	 * certificate; if we are a server, it does not. */
730*0Sstevel@tonic-gate 
731*0Sstevel@tonic-gate 	return(r);
732*0Sstevel@tonic-gate 	}
733*0Sstevel@tonic-gate 
734*0Sstevel@tonic-gate /* Now in theory, since the calling process own 't' it should be safe to
735*0Sstevel@tonic-gate  * modify.  We need to be able to read f without being hassled */
736*0Sstevel@tonic-gate void SSL_copy_session_id(SSL *t,SSL *f)
737*0Sstevel@tonic-gate 	{
738*0Sstevel@tonic-gate 	CERT *tmp;
739*0Sstevel@tonic-gate 
740*0Sstevel@tonic-gate 	/* Do we need to to SSL locking? */
741*0Sstevel@tonic-gate 	SSL_set_session(t,SSL_get_session(f));
742*0Sstevel@tonic-gate 
743*0Sstevel@tonic-gate 	/* what if we are setup as SSLv2 but want to talk SSLv3 or
744*0Sstevel@tonic-gate 	 * vice-versa */
745*0Sstevel@tonic-gate 	if (t->method != f->method)
746*0Sstevel@tonic-gate 		{
747*0Sstevel@tonic-gate 		t->method->ssl_free(t);	/* cleanup current */
748*0Sstevel@tonic-gate 		t->method=f->method;	/* change method */
749*0Sstevel@tonic-gate 		t->method->ssl_new(t);	/* setup new */
750*0Sstevel@tonic-gate 		}
751*0Sstevel@tonic-gate 
752*0Sstevel@tonic-gate 	tmp=t->cert;
753*0Sstevel@tonic-gate 	if (f->cert != NULL)
754*0Sstevel@tonic-gate 		{
755*0Sstevel@tonic-gate 		CRYPTO_add(&f->cert->references,1,CRYPTO_LOCK_SSL_CERT);
756*0Sstevel@tonic-gate 		t->cert=f->cert;
757*0Sstevel@tonic-gate 		}
758*0Sstevel@tonic-gate 	else
759*0Sstevel@tonic-gate 		t->cert=NULL;
760*0Sstevel@tonic-gate 	if (tmp != NULL) ssl_cert_free(tmp);
761*0Sstevel@tonic-gate 	SSL_set_session_id_context(t,f->sid_ctx,f->sid_ctx_length);
762*0Sstevel@tonic-gate 	}
763*0Sstevel@tonic-gate 
764*0Sstevel@tonic-gate /* Fix this so it checks all the valid key/cert options */
765*0Sstevel@tonic-gate int SSL_CTX_check_private_key(SSL_CTX *ctx)
766*0Sstevel@tonic-gate 	{
767*0Sstevel@tonic-gate 	if (	(ctx == NULL) ||
768*0Sstevel@tonic-gate 		(ctx->cert == NULL) ||
769*0Sstevel@tonic-gate 		(ctx->cert->key->x509 == NULL))
770*0Sstevel@tonic-gate 		{
771*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
772*0Sstevel@tonic-gate 		return(0);
773*0Sstevel@tonic-gate 		}
774*0Sstevel@tonic-gate 	if 	(ctx->cert->key->privatekey == NULL)
775*0Sstevel@tonic-gate 		{
776*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED);
777*0Sstevel@tonic-gate 		return(0);
778*0Sstevel@tonic-gate 		}
779*0Sstevel@tonic-gate 	return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey));
780*0Sstevel@tonic-gate 	}
781*0Sstevel@tonic-gate 
782*0Sstevel@tonic-gate /* Fix this function so that it takes an optional type parameter */
783*0Sstevel@tonic-gate int SSL_check_private_key(SSL *ssl)
784*0Sstevel@tonic-gate 	{
785*0Sstevel@tonic-gate 	if (ssl == NULL)
786*0Sstevel@tonic-gate 		{
787*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,ERR_R_PASSED_NULL_PARAMETER);
788*0Sstevel@tonic-gate 		return(0);
789*0Sstevel@tonic-gate 		}
790*0Sstevel@tonic-gate 	if (ssl->cert == NULL)
791*0Sstevel@tonic-gate 		{
792*0Sstevel@tonic-gate                 SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
793*0Sstevel@tonic-gate 		return 0;
794*0Sstevel@tonic-gate 		}
795*0Sstevel@tonic-gate 	if (ssl->cert->key->x509 == NULL)
796*0Sstevel@tonic-gate 		{
797*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED);
798*0Sstevel@tonic-gate 		return(0);
799*0Sstevel@tonic-gate 		}
800*0Sstevel@tonic-gate 	if (ssl->cert->key->privatekey == NULL)
801*0Sstevel@tonic-gate 		{
802*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED);
803*0Sstevel@tonic-gate 		return(0);
804*0Sstevel@tonic-gate 		}
805*0Sstevel@tonic-gate 	return(X509_check_private_key(ssl->cert->key->x509,
806*0Sstevel@tonic-gate 		ssl->cert->key->privatekey));
807*0Sstevel@tonic-gate 	}
808*0Sstevel@tonic-gate 
809*0Sstevel@tonic-gate int SSL_accept(SSL *s)
810*0Sstevel@tonic-gate 	{
811*0Sstevel@tonic-gate 	if (s->handshake_func == 0)
812*0Sstevel@tonic-gate 		/* Not properly initialized yet */
813*0Sstevel@tonic-gate 		SSL_set_accept_state(s);
814*0Sstevel@tonic-gate 
815*0Sstevel@tonic-gate 	return(s->method->ssl_accept(s));
816*0Sstevel@tonic-gate 	}
817*0Sstevel@tonic-gate 
818*0Sstevel@tonic-gate int SSL_connect(SSL *s)
819*0Sstevel@tonic-gate 	{
820*0Sstevel@tonic-gate 	if (s->handshake_func == 0)
821*0Sstevel@tonic-gate 		/* Not properly initialized yet */
822*0Sstevel@tonic-gate 		SSL_set_connect_state(s);
823*0Sstevel@tonic-gate 
824*0Sstevel@tonic-gate 	return(s->method->ssl_connect(s));
825*0Sstevel@tonic-gate 	}
826*0Sstevel@tonic-gate 
827*0Sstevel@tonic-gate long SSL_get_default_timeout(SSL *s)
828*0Sstevel@tonic-gate 	{
829*0Sstevel@tonic-gate 	return(s->method->get_timeout());
830*0Sstevel@tonic-gate 	}
831*0Sstevel@tonic-gate 
832*0Sstevel@tonic-gate int SSL_read(SSL *s,void *buf,int num)
833*0Sstevel@tonic-gate 	{
834*0Sstevel@tonic-gate 	if (s->handshake_func == 0)
835*0Sstevel@tonic-gate 		{
836*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_READ, SSL_R_UNINITIALIZED);
837*0Sstevel@tonic-gate 		return -1;
838*0Sstevel@tonic-gate 		}
839*0Sstevel@tonic-gate 
840*0Sstevel@tonic-gate 	if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
841*0Sstevel@tonic-gate 		{
842*0Sstevel@tonic-gate 		s->rwstate=SSL_NOTHING;
843*0Sstevel@tonic-gate 		return(0);
844*0Sstevel@tonic-gate 		}
845*0Sstevel@tonic-gate 	return(s->method->ssl_read(s,buf,num));
846*0Sstevel@tonic-gate 	}
847*0Sstevel@tonic-gate 
848*0Sstevel@tonic-gate int SSL_peek(SSL *s,void *buf,int num)
849*0Sstevel@tonic-gate 	{
850*0Sstevel@tonic-gate 	if (s->handshake_func == 0)
851*0Sstevel@tonic-gate 		{
852*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_READ, SSL_R_UNINITIALIZED);
853*0Sstevel@tonic-gate 		return -1;
854*0Sstevel@tonic-gate 		}
855*0Sstevel@tonic-gate 
856*0Sstevel@tonic-gate 	if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
857*0Sstevel@tonic-gate 		{
858*0Sstevel@tonic-gate 		return(0);
859*0Sstevel@tonic-gate 		}
860*0Sstevel@tonic-gate 	return(s->method->ssl_peek(s,buf,num));
861*0Sstevel@tonic-gate 	}
862*0Sstevel@tonic-gate 
863*0Sstevel@tonic-gate int SSL_write(SSL *s,const void *buf,int num)
864*0Sstevel@tonic-gate 	{
865*0Sstevel@tonic-gate 	if (s->handshake_func == 0)
866*0Sstevel@tonic-gate 		{
867*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_WRITE, SSL_R_UNINITIALIZED);
868*0Sstevel@tonic-gate 		return -1;
869*0Sstevel@tonic-gate 		}
870*0Sstevel@tonic-gate 
871*0Sstevel@tonic-gate 	if (s->shutdown & SSL_SENT_SHUTDOWN)
872*0Sstevel@tonic-gate 		{
873*0Sstevel@tonic-gate 		s->rwstate=SSL_NOTHING;
874*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_WRITE,SSL_R_PROTOCOL_IS_SHUTDOWN);
875*0Sstevel@tonic-gate 		return(-1);
876*0Sstevel@tonic-gate 		}
877*0Sstevel@tonic-gate 	return(s->method->ssl_write(s,buf,num));
878*0Sstevel@tonic-gate 	}
879*0Sstevel@tonic-gate 
880*0Sstevel@tonic-gate int SSL_shutdown(SSL *s)
881*0Sstevel@tonic-gate 	{
882*0Sstevel@tonic-gate 	/* Note that this function behaves differently from what one might
883*0Sstevel@tonic-gate 	 * expect.  Return values are 0 for no success (yet),
884*0Sstevel@tonic-gate 	 * 1 for success; but calling it once is usually not enough,
885*0Sstevel@tonic-gate 	 * even if blocking I/O is used (see ssl3_shutdown).
886*0Sstevel@tonic-gate 	 */
887*0Sstevel@tonic-gate 
888*0Sstevel@tonic-gate 	if (s->handshake_func == 0)
889*0Sstevel@tonic-gate 		{
890*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_SHUTDOWN, SSL_R_UNINITIALIZED);
891*0Sstevel@tonic-gate 		return -1;
892*0Sstevel@tonic-gate 		}
893*0Sstevel@tonic-gate 
894*0Sstevel@tonic-gate 	if ((s != NULL) && !SSL_in_init(s))
895*0Sstevel@tonic-gate 		return(s->method->ssl_shutdown(s));
896*0Sstevel@tonic-gate 	else
897*0Sstevel@tonic-gate 		return(1);
898*0Sstevel@tonic-gate 	}
899*0Sstevel@tonic-gate 
900*0Sstevel@tonic-gate int SSL_renegotiate(SSL *s)
901*0Sstevel@tonic-gate 	{
902*0Sstevel@tonic-gate 	if (s->new_session == 0)
903*0Sstevel@tonic-gate 		{
904*0Sstevel@tonic-gate 		s->new_session=1;
905*0Sstevel@tonic-gate 		}
906*0Sstevel@tonic-gate 	return(s->method->ssl_renegotiate(s));
907*0Sstevel@tonic-gate 	}
908*0Sstevel@tonic-gate 
909*0Sstevel@tonic-gate int SSL_renegotiate_pending(SSL *s)
910*0Sstevel@tonic-gate 	{
911*0Sstevel@tonic-gate 	/* becomes true when negotiation is requested;
912*0Sstevel@tonic-gate 	 * false again once a handshake has finished */
913*0Sstevel@tonic-gate 	return (s->new_session != 0);
914*0Sstevel@tonic-gate 	}
915*0Sstevel@tonic-gate 
916*0Sstevel@tonic-gate long SSL_ctrl(SSL *s,int cmd,long larg,void *parg)
917*0Sstevel@tonic-gate 	{
918*0Sstevel@tonic-gate 	long l;
919*0Sstevel@tonic-gate 
920*0Sstevel@tonic-gate 	switch (cmd)
921*0Sstevel@tonic-gate 		{
922*0Sstevel@tonic-gate 	case SSL_CTRL_GET_READ_AHEAD:
923*0Sstevel@tonic-gate 		return(s->read_ahead);
924*0Sstevel@tonic-gate 	case SSL_CTRL_SET_READ_AHEAD:
925*0Sstevel@tonic-gate 		l=s->read_ahead;
926*0Sstevel@tonic-gate 		s->read_ahead=larg;
927*0Sstevel@tonic-gate 		return(l);
928*0Sstevel@tonic-gate 
929*0Sstevel@tonic-gate 	case SSL_CTRL_SET_MSG_CALLBACK_ARG:
930*0Sstevel@tonic-gate 		s->msg_callback_arg = parg;
931*0Sstevel@tonic-gate 		return 1;
932*0Sstevel@tonic-gate 
933*0Sstevel@tonic-gate 	case SSL_CTRL_OPTIONS:
934*0Sstevel@tonic-gate 		return(s->options|=larg);
935*0Sstevel@tonic-gate 	case SSL_CTRL_MODE:
936*0Sstevel@tonic-gate 		return(s->mode|=larg);
937*0Sstevel@tonic-gate 	case SSL_CTRL_GET_MAX_CERT_LIST:
938*0Sstevel@tonic-gate 		return(s->max_cert_list);
939*0Sstevel@tonic-gate 	case SSL_CTRL_SET_MAX_CERT_LIST:
940*0Sstevel@tonic-gate 		l=s->max_cert_list;
941*0Sstevel@tonic-gate 		s->max_cert_list=larg;
942*0Sstevel@tonic-gate 		return(l);
943*0Sstevel@tonic-gate 	default:
944*0Sstevel@tonic-gate 		return(s->method->ssl_ctrl(s,cmd,larg,parg));
945*0Sstevel@tonic-gate 		}
946*0Sstevel@tonic-gate 	}
947*0Sstevel@tonic-gate 
948*0Sstevel@tonic-gate long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)())
949*0Sstevel@tonic-gate 	{
950*0Sstevel@tonic-gate 	switch(cmd)
951*0Sstevel@tonic-gate 		{
952*0Sstevel@tonic-gate 	case SSL_CTRL_SET_MSG_CALLBACK:
953*0Sstevel@tonic-gate 		s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
954*0Sstevel@tonic-gate 		return 1;
955*0Sstevel@tonic-gate 
956*0Sstevel@tonic-gate 	default:
957*0Sstevel@tonic-gate 		return(s->method->ssl_callback_ctrl(s,cmd,fp));
958*0Sstevel@tonic-gate 		}
959*0Sstevel@tonic-gate 	}
960*0Sstevel@tonic-gate 
961*0Sstevel@tonic-gate struct lhash_st *SSL_CTX_sessions(SSL_CTX *ctx)
962*0Sstevel@tonic-gate 	{
963*0Sstevel@tonic-gate 	return ctx->sessions;
964*0Sstevel@tonic-gate 	}
965*0Sstevel@tonic-gate 
966*0Sstevel@tonic-gate long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg)
967*0Sstevel@tonic-gate 	{
968*0Sstevel@tonic-gate 	long l;
969*0Sstevel@tonic-gate 
970*0Sstevel@tonic-gate 	switch (cmd)
971*0Sstevel@tonic-gate 		{
972*0Sstevel@tonic-gate 	case SSL_CTRL_GET_READ_AHEAD:
973*0Sstevel@tonic-gate 		return(ctx->read_ahead);
974*0Sstevel@tonic-gate 	case SSL_CTRL_SET_READ_AHEAD:
975*0Sstevel@tonic-gate 		l=ctx->read_ahead;
976*0Sstevel@tonic-gate 		ctx->read_ahead=larg;
977*0Sstevel@tonic-gate 		return(l);
978*0Sstevel@tonic-gate 
979*0Sstevel@tonic-gate 	case SSL_CTRL_SET_MSG_CALLBACK_ARG:
980*0Sstevel@tonic-gate 		ctx->msg_callback_arg = parg;
981*0Sstevel@tonic-gate 		return 1;
982*0Sstevel@tonic-gate 
983*0Sstevel@tonic-gate 	case SSL_CTRL_GET_MAX_CERT_LIST:
984*0Sstevel@tonic-gate 		return(ctx->max_cert_list);
985*0Sstevel@tonic-gate 	case SSL_CTRL_SET_MAX_CERT_LIST:
986*0Sstevel@tonic-gate 		l=ctx->max_cert_list;
987*0Sstevel@tonic-gate 		ctx->max_cert_list=larg;
988*0Sstevel@tonic-gate 		return(l);
989*0Sstevel@tonic-gate 
990*0Sstevel@tonic-gate 	case SSL_CTRL_SET_SESS_CACHE_SIZE:
991*0Sstevel@tonic-gate 		l=ctx->session_cache_size;
992*0Sstevel@tonic-gate 		ctx->session_cache_size=larg;
993*0Sstevel@tonic-gate 		return(l);
994*0Sstevel@tonic-gate 	case SSL_CTRL_GET_SESS_CACHE_SIZE:
995*0Sstevel@tonic-gate 		return(ctx->session_cache_size);
996*0Sstevel@tonic-gate 	case SSL_CTRL_SET_SESS_CACHE_MODE:
997*0Sstevel@tonic-gate 		l=ctx->session_cache_mode;
998*0Sstevel@tonic-gate 		ctx->session_cache_mode=larg;
999*0Sstevel@tonic-gate 		return(l);
1000*0Sstevel@tonic-gate 	case SSL_CTRL_GET_SESS_CACHE_MODE:
1001*0Sstevel@tonic-gate 		return(ctx->session_cache_mode);
1002*0Sstevel@tonic-gate 
1003*0Sstevel@tonic-gate 	case SSL_CTRL_SESS_NUMBER:
1004*0Sstevel@tonic-gate 		return(ctx->sessions->num_items);
1005*0Sstevel@tonic-gate 	case SSL_CTRL_SESS_CONNECT:
1006*0Sstevel@tonic-gate 		return(ctx->stats.sess_connect);
1007*0Sstevel@tonic-gate 	case SSL_CTRL_SESS_CONNECT_GOOD:
1008*0Sstevel@tonic-gate 		return(ctx->stats.sess_connect_good);
1009*0Sstevel@tonic-gate 	case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1010*0Sstevel@tonic-gate 		return(ctx->stats.sess_connect_renegotiate);
1011*0Sstevel@tonic-gate 	case SSL_CTRL_SESS_ACCEPT:
1012*0Sstevel@tonic-gate 		return(ctx->stats.sess_accept);
1013*0Sstevel@tonic-gate 	case SSL_CTRL_SESS_ACCEPT_GOOD:
1014*0Sstevel@tonic-gate 		return(ctx->stats.sess_accept_good);
1015*0Sstevel@tonic-gate 	case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1016*0Sstevel@tonic-gate 		return(ctx->stats.sess_accept_renegotiate);
1017*0Sstevel@tonic-gate 	case SSL_CTRL_SESS_HIT:
1018*0Sstevel@tonic-gate 		return(ctx->stats.sess_hit);
1019*0Sstevel@tonic-gate 	case SSL_CTRL_SESS_CB_HIT:
1020*0Sstevel@tonic-gate 		return(ctx->stats.sess_cb_hit);
1021*0Sstevel@tonic-gate 	case SSL_CTRL_SESS_MISSES:
1022*0Sstevel@tonic-gate 		return(ctx->stats.sess_miss);
1023*0Sstevel@tonic-gate 	case SSL_CTRL_SESS_TIMEOUTS:
1024*0Sstevel@tonic-gate 		return(ctx->stats.sess_timeout);
1025*0Sstevel@tonic-gate 	case SSL_CTRL_SESS_CACHE_FULL:
1026*0Sstevel@tonic-gate 		return(ctx->stats.sess_cache_full);
1027*0Sstevel@tonic-gate 	case SSL_CTRL_OPTIONS:
1028*0Sstevel@tonic-gate 		return(ctx->options|=larg);
1029*0Sstevel@tonic-gate 	case SSL_CTRL_MODE:
1030*0Sstevel@tonic-gate 		return(ctx->mode|=larg);
1031*0Sstevel@tonic-gate 	default:
1032*0Sstevel@tonic-gate 		return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
1033*0Sstevel@tonic-gate 		}
1034*0Sstevel@tonic-gate 	}
1035*0Sstevel@tonic-gate 
1036*0Sstevel@tonic-gate long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)())
1037*0Sstevel@tonic-gate 	{
1038*0Sstevel@tonic-gate 	switch(cmd)
1039*0Sstevel@tonic-gate 		{
1040*0Sstevel@tonic-gate 	case SSL_CTRL_SET_MSG_CALLBACK:
1041*0Sstevel@tonic-gate 		ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1042*0Sstevel@tonic-gate 		return 1;
1043*0Sstevel@tonic-gate 
1044*0Sstevel@tonic-gate 	default:
1045*0Sstevel@tonic-gate 		return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp));
1046*0Sstevel@tonic-gate 		}
1047*0Sstevel@tonic-gate 	}
1048*0Sstevel@tonic-gate 
1049*0Sstevel@tonic-gate int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b)
1050*0Sstevel@tonic-gate 	{
1051*0Sstevel@tonic-gate 	long l;
1052*0Sstevel@tonic-gate 
1053*0Sstevel@tonic-gate 	l=a->id-b->id;
1054*0Sstevel@tonic-gate 	if (l == 0L)
1055*0Sstevel@tonic-gate 		return(0);
1056*0Sstevel@tonic-gate 	else
1057*0Sstevel@tonic-gate 		return((l > 0)?1:-1);
1058*0Sstevel@tonic-gate 	}
1059*0Sstevel@tonic-gate 
1060*0Sstevel@tonic-gate int ssl_cipher_ptr_id_cmp(const SSL_CIPHER * const *ap,
1061*0Sstevel@tonic-gate 			const SSL_CIPHER * const *bp)
1062*0Sstevel@tonic-gate 	{
1063*0Sstevel@tonic-gate 	long l;
1064*0Sstevel@tonic-gate 
1065*0Sstevel@tonic-gate 	l=(*ap)->id-(*bp)->id;
1066*0Sstevel@tonic-gate 	if (l == 0L)
1067*0Sstevel@tonic-gate 		return(0);
1068*0Sstevel@tonic-gate 	else
1069*0Sstevel@tonic-gate 		return((l > 0)?1:-1);
1070*0Sstevel@tonic-gate 	}
1071*0Sstevel@tonic-gate 
1072*0Sstevel@tonic-gate /** return a STACK of the ciphers available for the SSL and in order of
1073*0Sstevel@tonic-gate  * preference */
1074*0Sstevel@tonic-gate STACK_OF(SSL_CIPHER) *SSL_get_ciphers(SSL *s)
1075*0Sstevel@tonic-gate 	{
1076*0Sstevel@tonic-gate 	if (s != NULL)
1077*0Sstevel@tonic-gate 		{
1078*0Sstevel@tonic-gate 		if (s->cipher_list != NULL)
1079*0Sstevel@tonic-gate 			{
1080*0Sstevel@tonic-gate 			return(s->cipher_list);
1081*0Sstevel@tonic-gate 			}
1082*0Sstevel@tonic-gate 		else if ((s->ctx != NULL) &&
1083*0Sstevel@tonic-gate 			(s->ctx->cipher_list != NULL))
1084*0Sstevel@tonic-gate 			{
1085*0Sstevel@tonic-gate 			return(s->ctx->cipher_list);
1086*0Sstevel@tonic-gate 			}
1087*0Sstevel@tonic-gate 		}
1088*0Sstevel@tonic-gate 	return(NULL);
1089*0Sstevel@tonic-gate 	}
1090*0Sstevel@tonic-gate 
1091*0Sstevel@tonic-gate /** return a STACK of the ciphers available for the SSL and in order of
1092*0Sstevel@tonic-gate  * algorithm id */
1093*0Sstevel@tonic-gate STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
1094*0Sstevel@tonic-gate 	{
1095*0Sstevel@tonic-gate 	if (s != NULL)
1096*0Sstevel@tonic-gate 		{
1097*0Sstevel@tonic-gate 		if (s->cipher_list_by_id != NULL)
1098*0Sstevel@tonic-gate 			{
1099*0Sstevel@tonic-gate 			return(s->cipher_list_by_id);
1100*0Sstevel@tonic-gate 			}
1101*0Sstevel@tonic-gate 		else if ((s->ctx != NULL) &&
1102*0Sstevel@tonic-gate 			(s->ctx->cipher_list_by_id != NULL))
1103*0Sstevel@tonic-gate 			{
1104*0Sstevel@tonic-gate 			return(s->ctx->cipher_list_by_id);
1105*0Sstevel@tonic-gate 			}
1106*0Sstevel@tonic-gate 		}
1107*0Sstevel@tonic-gate 	return(NULL);
1108*0Sstevel@tonic-gate 	}
1109*0Sstevel@tonic-gate 
1110*0Sstevel@tonic-gate /** The old interface to get the same thing as SSL_get_ciphers() */
1111*0Sstevel@tonic-gate const char *SSL_get_cipher_list(SSL *s,int n)
1112*0Sstevel@tonic-gate 	{
1113*0Sstevel@tonic-gate 	SSL_CIPHER *c;
1114*0Sstevel@tonic-gate 	STACK_OF(SSL_CIPHER) *sk;
1115*0Sstevel@tonic-gate 
1116*0Sstevel@tonic-gate 	if (s == NULL) return(NULL);
1117*0Sstevel@tonic-gate 	sk=SSL_get_ciphers(s);
1118*0Sstevel@tonic-gate 	if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n))
1119*0Sstevel@tonic-gate 		return(NULL);
1120*0Sstevel@tonic-gate 	c=sk_SSL_CIPHER_value(sk,n);
1121*0Sstevel@tonic-gate 	if (c == NULL) return(NULL);
1122*0Sstevel@tonic-gate 	return(c->name);
1123*0Sstevel@tonic-gate 	}
1124*0Sstevel@tonic-gate 
1125*0Sstevel@tonic-gate /** specify the ciphers to be used by default by the SSL_CTX */
1126*0Sstevel@tonic-gate int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
1127*0Sstevel@tonic-gate 	{
1128*0Sstevel@tonic-gate 	STACK_OF(SSL_CIPHER) *sk;
1129*0Sstevel@tonic-gate 
1130*0Sstevel@tonic-gate 	sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
1131*0Sstevel@tonic-gate 		&ctx->cipher_list_by_id,str);
1132*0Sstevel@tonic-gate /* XXXX */
1133*0Sstevel@tonic-gate 	return((sk == NULL)?0:1);
1134*0Sstevel@tonic-gate 	}
1135*0Sstevel@tonic-gate 
1136*0Sstevel@tonic-gate /** specify the ciphers to be used by the SSL */
1137*0Sstevel@tonic-gate int SSL_set_cipher_list(SSL *s,const char *str)
1138*0Sstevel@tonic-gate 	{
1139*0Sstevel@tonic-gate 	STACK_OF(SSL_CIPHER) *sk;
1140*0Sstevel@tonic-gate 
1141*0Sstevel@tonic-gate 	sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
1142*0Sstevel@tonic-gate 		&s->cipher_list_by_id,str);
1143*0Sstevel@tonic-gate /* XXXX */
1144*0Sstevel@tonic-gate 	return((sk == NULL)?0:1);
1145*0Sstevel@tonic-gate 	}
1146*0Sstevel@tonic-gate 
1147*0Sstevel@tonic-gate /* works well for SSLv2, not so good for SSLv3 */
1148*0Sstevel@tonic-gate char *SSL_get_shared_ciphers(SSL *s,char *buf,int len)
1149*0Sstevel@tonic-gate 	{
1150*0Sstevel@tonic-gate 	char *p;
1151*0Sstevel@tonic-gate 	const char *cp;
1152*0Sstevel@tonic-gate 	STACK_OF(SSL_CIPHER) *sk;
1153*0Sstevel@tonic-gate 	SSL_CIPHER *c;
1154*0Sstevel@tonic-gate 	int i;
1155*0Sstevel@tonic-gate 
1156*0Sstevel@tonic-gate 	if ((s->session == NULL) || (s->session->ciphers == NULL) ||
1157*0Sstevel@tonic-gate 		(len < 2))
1158*0Sstevel@tonic-gate 		return(NULL);
1159*0Sstevel@tonic-gate 
1160*0Sstevel@tonic-gate 	p=buf;
1161*0Sstevel@tonic-gate 	sk=s->session->ciphers;
1162*0Sstevel@tonic-gate 	for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1163*0Sstevel@tonic-gate 		{
1164*0Sstevel@tonic-gate 		/* Decrement for either the ':' or a '\0' */
1165*0Sstevel@tonic-gate 		len--;
1166*0Sstevel@tonic-gate 		c=sk_SSL_CIPHER_value(sk,i);
1167*0Sstevel@tonic-gate 		for (cp=c->name; *cp; )
1168*0Sstevel@tonic-gate 			{
1169*0Sstevel@tonic-gate 			if (len-- == 0)
1170*0Sstevel@tonic-gate 				{
1171*0Sstevel@tonic-gate 				*p='\0';
1172*0Sstevel@tonic-gate 				return(buf);
1173*0Sstevel@tonic-gate 				}
1174*0Sstevel@tonic-gate 			else
1175*0Sstevel@tonic-gate 				*(p++)= *(cp++);
1176*0Sstevel@tonic-gate 			}
1177*0Sstevel@tonic-gate 		*(p++)=':';
1178*0Sstevel@tonic-gate 		}
1179*0Sstevel@tonic-gate 	p[-1]='\0';
1180*0Sstevel@tonic-gate 	return(buf);
1181*0Sstevel@tonic-gate 	}
1182*0Sstevel@tonic-gate 
1183*0Sstevel@tonic-gate int ssl_cipher_list_to_bytes(SSL *s,STACK_OF(SSL_CIPHER) *sk,unsigned char *p)
1184*0Sstevel@tonic-gate 	{
1185*0Sstevel@tonic-gate 	int i,j=0;
1186*0Sstevel@tonic-gate 	SSL_CIPHER *c;
1187*0Sstevel@tonic-gate 	unsigned char *q;
1188*0Sstevel@tonic-gate #ifndef OPENSSL_NO_KRB5
1189*0Sstevel@tonic-gate         int nokrb5 = !kssl_tgt_is_available(s->kssl_ctx);
1190*0Sstevel@tonic-gate #endif /* OPENSSL_NO_KRB5 */
1191*0Sstevel@tonic-gate 
1192*0Sstevel@tonic-gate 	if (sk == NULL) return(0);
1193*0Sstevel@tonic-gate 	q=p;
1194*0Sstevel@tonic-gate 
1195*0Sstevel@tonic-gate 	for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1196*0Sstevel@tonic-gate 		{
1197*0Sstevel@tonic-gate 		c=sk_SSL_CIPHER_value(sk,i);
1198*0Sstevel@tonic-gate #ifndef OPENSSL_NO_KRB5
1199*0Sstevel@tonic-gate                 if ((c->algorithms & SSL_KRB5) && nokrb5)
1200*0Sstevel@tonic-gate                     continue;
1201*0Sstevel@tonic-gate #endif /* OPENSSL_NO_KRB5 */
1202*0Sstevel@tonic-gate 		j=ssl_put_cipher_by_char(s,c,p);
1203*0Sstevel@tonic-gate 		p+=j;
1204*0Sstevel@tonic-gate 		}
1205*0Sstevel@tonic-gate 	return(p-q);
1206*0Sstevel@tonic-gate 	}
1207*0Sstevel@tonic-gate 
1208*0Sstevel@tonic-gate STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,unsigned char *p,int num,
1209*0Sstevel@tonic-gate 					       STACK_OF(SSL_CIPHER) **skp)
1210*0Sstevel@tonic-gate 	{
1211*0Sstevel@tonic-gate 	SSL_CIPHER *c;
1212*0Sstevel@tonic-gate 	STACK_OF(SSL_CIPHER) *sk;
1213*0Sstevel@tonic-gate 	int i,n;
1214*0Sstevel@tonic-gate 
1215*0Sstevel@tonic-gate 	n=ssl_put_cipher_by_char(s,NULL,NULL);
1216*0Sstevel@tonic-gate 	if ((num%n) != 0)
1217*0Sstevel@tonic-gate 		{
1218*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1219*0Sstevel@tonic-gate 		return(NULL);
1220*0Sstevel@tonic-gate 		}
1221*0Sstevel@tonic-gate 	if ((skp == NULL) || (*skp == NULL))
1222*0Sstevel@tonic-gate 		sk=sk_SSL_CIPHER_new_null(); /* change perhaps later */
1223*0Sstevel@tonic-gate 	else
1224*0Sstevel@tonic-gate 		{
1225*0Sstevel@tonic-gate 		sk= *skp;
1226*0Sstevel@tonic-gate 		sk_SSL_CIPHER_zero(sk);
1227*0Sstevel@tonic-gate 		}
1228*0Sstevel@tonic-gate 
1229*0Sstevel@tonic-gate 	for (i=0; i<num; i+=n)
1230*0Sstevel@tonic-gate 		{
1231*0Sstevel@tonic-gate 		c=ssl_get_cipher_by_char(s,p);
1232*0Sstevel@tonic-gate 		p+=n;
1233*0Sstevel@tonic-gate 		if (c != NULL)
1234*0Sstevel@tonic-gate 			{
1235*0Sstevel@tonic-gate 			if (!sk_SSL_CIPHER_push(sk,c))
1236*0Sstevel@tonic-gate 				{
1237*0Sstevel@tonic-gate 				SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,ERR_R_MALLOC_FAILURE);
1238*0Sstevel@tonic-gate 				goto err;
1239*0Sstevel@tonic-gate 				}
1240*0Sstevel@tonic-gate 			}
1241*0Sstevel@tonic-gate 		}
1242*0Sstevel@tonic-gate 
1243*0Sstevel@tonic-gate 	if (skp != NULL)
1244*0Sstevel@tonic-gate 		*skp=sk;
1245*0Sstevel@tonic-gate 	return(sk);
1246*0Sstevel@tonic-gate err:
1247*0Sstevel@tonic-gate 	if ((skp == NULL) || (*skp == NULL))
1248*0Sstevel@tonic-gate 		sk_SSL_CIPHER_free(sk);
1249*0Sstevel@tonic-gate 	return(NULL);
1250*0Sstevel@tonic-gate 	}
1251*0Sstevel@tonic-gate 
1252*0Sstevel@tonic-gate unsigned long SSL_SESSION_hash(SSL_SESSION *a)
1253*0Sstevel@tonic-gate 	{
1254*0Sstevel@tonic-gate 	unsigned long l;
1255*0Sstevel@tonic-gate 
1256*0Sstevel@tonic-gate 	l=(unsigned long)
1257*0Sstevel@tonic-gate 		((unsigned int) a->session_id[0]     )|
1258*0Sstevel@tonic-gate 		((unsigned int) a->session_id[1]<< 8L)|
1259*0Sstevel@tonic-gate 		((unsigned long)a->session_id[2]<<16L)|
1260*0Sstevel@tonic-gate 		((unsigned long)a->session_id[3]<<24L);
1261*0Sstevel@tonic-gate 	return(l);
1262*0Sstevel@tonic-gate 	}
1263*0Sstevel@tonic-gate 
1264*0Sstevel@tonic-gate /* NB: If this function (or indeed the hash function which uses a sort of
1265*0Sstevel@tonic-gate  * coarser function than this one) is changed, ensure
1266*0Sstevel@tonic-gate  * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1267*0Sstevel@tonic-gate  * able to construct an SSL_SESSION that will collide with any existing session
1268*0Sstevel@tonic-gate  * with a matching session ID. */
1269*0Sstevel@tonic-gate int SSL_SESSION_cmp(SSL_SESSION *a,SSL_SESSION *b)
1270*0Sstevel@tonic-gate 	{
1271*0Sstevel@tonic-gate 	if (a->ssl_version != b->ssl_version)
1272*0Sstevel@tonic-gate 		return(1);
1273*0Sstevel@tonic-gate 	if (a->session_id_length != b->session_id_length)
1274*0Sstevel@tonic-gate 		return(1);
1275*0Sstevel@tonic-gate 	return(memcmp(a->session_id,b->session_id,a->session_id_length));
1276*0Sstevel@tonic-gate 	}
1277*0Sstevel@tonic-gate 
1278*0Sstevel@tonic-gate /* These wrapper functions should remain rather than redeclaring
1279*0Sstevel@tonic-gate  * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
1280*0Sstevel@tonic-gate  * variable. The reason is that the functions aren't static, they're exposed via
1281*0Sstevel@tonic-gate  * ssl.h. */
1282*0Sstevel@tonic-gate static IMPLEMENT_LHASH_HASH_FN(SSL_SESSION_hash, SSL_SESSION *)
1283*0Sstevel@tonic-gate static IMPLEMENT_LHASH_COMP_FN(SSL_SESSION_cmp, SSL_SESSION *)
1284*0Sstevel@tonic-gate 
1285*0Sstevel@tonic-gate SSL_CTX *SSL_CTX_new(SSL_METHOD *meth)
1286*0Sstevel@tonic-gate 	{
1287*0Sstevel@tonic-gate 	SSL_CTX *ret=NULL;
1288*0Sstevel@tonic-gate 
1289*0Sstevel@tonic-gate 	if (meth == NULL)
1290*0Sstevel@tonic-gate 		{
1291*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_NULL_SSL_METHOD_PASSED);
1292*0Sstevel@tonic-gate 		return(NULL);
1293*0Sstevel@tonic-gate 		}
1294*0Sstevel@tonic-gate 
1295*0Sstevel@tonic-gate 	if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
1296*0Sstevel@tonic-gate 		{
1297*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1298*0Sstevel@tonic-gate 		goto err;
1299*0Sstevel@tonic-gate 		}
1300*0Sstevel@tonic-gate 	ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1301*0Sstevel@tonic-gate 	if (ret == NULL)
1302*0Sstevel@tonic-gate 		goto err;
1303*0Sstevel@tonic-gate 
1304*0Sstevel@tonic-gate 	memset(ret,0,sizeof(SSL_CTX));
1305*0Sstevel@tonic-gate 
1306*0Sstevel@tonic-gate 	ret->method=meth;
1307*0Sstevel@tonic-gate 
1308*0Sstevel@tonic-gate 	ret->cert_store=NULL;
1309*0Sstevel@tonic-gate 	ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
1310*0Sstevel@tonic-gate 	ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1311*0Sstevel@tonic-gate 	ret->session_cache_head=NULL;
1312*0Sstevel@tonic-gate 	ret->session_cache_tail=NULL;
1313*0Sstevel@tonic-gate 
1314*0Sstevel@tonic-gate 	/* We take the system default */
1315*0Sstevel@tonic-gate 	ret->session_timeout=meth->get_timeout();
1316*0Sstevel@tonic-gate 
1317*0Sstevel@tonic-gate 	ret->new_session_cb=0;
1318*0Sstevel@tonic-gate 	ret->remove_session_cb=0;
1319*0Sstevel@tonic-gate 	ret->get_session_cb=0;
1320*0Sstevel@tonic-gate 	ret->generate_session_id=0;
1321*0Sstevel@tonic-gate 
1322*0Sstevel@tonic-gate 	memset((char *)&ret->stats,0,sizeof(ret->stats));
1323*0Sstevel@tonic-gate 
1324*0Sstevel@tonic-gate 	ret->references=1;
1325*0Sstevel@tonic-gate 	ret->quiet_shutdown=0;
1326*0Sstevel@tonic-gate 
1327*0Sstevel@tonic-gate /*	ret->cipher=NULL;*/
1328*0Sstevel@tonic-gate /*	ret->s2->challenge=NULL;
1329*0Sstevel@tonic-gate 	ret->master_key=NULL;
1330*0Sstevel@tonic-gate 	ret->key_arg=NULL;
1331*0Sstevel@tonic-gate 	ret->s2->conn_id=NULL; */
1332*0Sstevel@tonic-gate 
1333*0Sstevel@tonic-gate 	ret->info_callback=NULL;
1334*0Sstevel@tonic-gate 
1335*0Sstevel@tonic-gate 	ret->app_verify_callback=0;
1336*0Sstevel@tonic-gate 	ret->app_verify_arg=NULL;
1337*0Sstevel@tonic-gate 
1338*0Sstevel@tonic-gate 	ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT;
1339*0Sstevel@tonic-gate 	ret->read_ahead=0;
1340*0Sstevel@tonic-gate 	ret->msg_callback=0;
1341*0Sstevel@tonic-gate 	ret->msg_callback_arg=NULL;
1342*0Sstevel@tonic-gate 	ret->verify_mode=SSL_VERIFY_NONE;
1343*0Sstevel@tonic-gate 	ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */
1344*0Sstevel@tonic-gate 	ret->sid_ctx_length=0;
1345*0Sstevel@tonic-gate 	ret->default_verify_callback=NULL;
1346*0Sstevel@tonic-gate 	if ((ret->cert=ssl_cert_new()) == NULL)
1347*0Sstevel@tonic-gate 		goto err;
1348*0Sstevel@tonic-gate 
1349*0Sstevel@tonic-gate 	ret->default_passwd_callback=0;
1350*0Sstevel@tonic-gate 	ret->default_passwd_callback_userdata=NULL;
1351*0Sstevel@tonic-gate 	ret->client_cert_cb=0;
1352*0Sstevel@tonic-gate 
1353*0Sstevel@tonic-gate 	ret->sessions=lh_new(LHASH_HASH_FN(SSL_SESSION_hash),
1354*0Sstevel@tonic-gate 			LHASH_COMP_FN(SSL_SESSION_cmp));
1355*0Sstevel@tonic-gate 	if (ret->sessions == NULL) goto err;
1356*0Sstevel@tonic-gate 	ret->cert_store=X509_STORE_new();
1357*0Sstevel@tonic-gate 	if (ret->cert_store == NULL) goto err;
1358*0Sstevel@tonic-gate 
1359*0Sstevel@tonic-gate 	ssl_create_cipher_list(ret->method,
1360*0Sstevel@tonic-gate 		&ret->cipher_list,&ret->cipher_list_by_id,
1361*0Sstevel@tonic-gate 		SSL_DEFAULT_CIPHER_LIST);
1362*0Sstevel@tonic-gate 	if (ret->cipher_list == NULL
1363*0Sstevel@tonic-gate 	    || sk_SSL_CIPHER_num(ret->cipher_list) <= 0)
1364*0Sstevel@tonic-gate 		{
1365*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_LIBRARY_HAS_NO_CIPHERS);
1366*0Sstevel@tonic-gate 		goto err2;
1367*0Sstevel@tonic-gate 		}
1368*0Sstevel@tonic-gate 
1369*0Sstevel@tonic-gate 	if ((ret->rsa_md5=EVP_get_digestbyname("ssl2-md5")) == NULL)
1370*0Sstevel@tonic-gate 		{
1371*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL2_MD5_ROUTINES);
1372*0Sstevel@tonic-gate 		goto err2;
1373*0Sstevel@tonic-gate 		}
1374*0Sstevel@tonic-gate 	if ((ret->md5=EVP_get_digestbyname("ssl3-md5")) == NULL)
1375*0Sstevel@tonic-gate 		{
1376*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES);
1377*0Sstevel@tonic-gate 		goto err2;
1378*0Sstevel@tonic-gate 		}
1379*0Sstevel@tonic-gate 	if ((ret->sha1=EVP_get_digestbyname("ssl3-sha1")) == NULL)
1380*0Sstevel@tonic-gate 		{
1381*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES);
1382*0Sstevel@tonic-gate 		goto err2;
1383*0Sstevel@tonic-gate 		}
1384*0Sstevel@tonic-gate 
1385*0Sstevel@tonic-gate 	if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL)
1386*0Sstevel@tonic-gate 		goto err;
1387*0Sstevel@tonic-gate 
1388*0Sstevel@tonic-gate 	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
1389*0Sstevel@tonic-gate 
1390*0Sstevel@tonic-gate 	ret->extra_certs=NULL;
1391*0Sstevel@tonic-gate 	ret->comp_methods=SSL_COMP_get_compression_methods();
1392*0Sstevel@tonic-gate 
1393*0Sstevel@tonic-gate 	return(ret);
1394*0Sstevel@tonic-gate err:
1395*0Sstevel@tonic-gate 	SSLerr(SSL_F_SSL_CTX_NEW,ERR_R_MALLOC_FAILURE);
1396*0Sstevel@tonic-gate err2:
1397*0Sstevel@tonic-gate 	if (ret != NULL) SSL_CTX_free(ret);
1398*0Sstevel@tonic-gate 	return(NULL);
1399*0Sstevel@tonic-gate 	}
1400*0Sstevel@tonic-gate 
1401*0Sstevel@tonic-gate #if 0
1402*0Sstevel@tonic-gate static void SSL_COMP_free(SSL_COMP *comp)
1403*0Sstevel@tonic-gate     { OPENSSL_free(comp); }
1404*0Sstevel@tonic-gate #endif
1405*0Sstevel@tonic-gate 
1406*0Sstevel@tonic-gate void SSL_CTX_free(SSL_CTX *a)
1407*0Sstevel@tonic-gate 	{
1408*0Sstevel@tonic-gate 	int i;
1409*0Sstevel@tonic-gate 
1410*0Sstevel@tonic-gate 	if (a == NULL) return;
1411*0Sstevel@tonic-gate 
1412*0Sstevel@tonic-gate 	i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
1413*0Sstevel@tonic-gate #ifdef REF_PRINT
1414*0Sstevel@tonic-gate 	REF_PRINT("SSL_CTX",a);
1415*0Sstevel@tonic-gate #endif
1416*0Sstevel@tonic-gate 	if (i > 0) return;
1417*0Sstevel@tonic-gate #ifdef REF_CHECK
1418*0Sstevel@tonic-gate 	if (i < 0)
1419*0Sstevel@tonic-gate 		{
1420*0Sstevel@tonic-gate 		fprintf(stderr,"SSL_CTX_free, bad reference count\n");
1421*0Sstevel@tonic-gate 		abort(); /* ok */
1422*0Sstevel@tonic-gate 		}
1423*0Sstevel@tonic-gate #endif
1424*0Sstevel@tonic-gate 
1425*0Sstevel@tonic-gate 	/*
1426*0Sstevel@tonic-gate 	 * Free internal session cache. However: the remove_cb() may reference
1427*0Sstevel@tonic-gate 	 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
1428*0Sstevel@tonic-gate 	 * after the sessions were flushed.
1429*0Sstevel@tonic-gate 	 * As the ex_data handling routines might also touch the session cache,
1430*0Sstevel@tonic-gate 	 * the most secure solution seems to be: empty (flush) the cache, then
1431*0Sstevel@tonic-gate 	 * free ex_data, then finally free the cache.
1432*0Sstevel@tonic-gate 	 * (See ticket [openssl.org #212].)
1433*0Sstevel@tonic-gate 	 */
1434*0Sstevel@tonic-gate 	if (a->sessions != NULL)
1435*0Sstevel@tonic-gate 		SSL_CTX_flush_sessions(a,0);
1436*0Sstevel@tonic-gate 
1437*0Sstevel@tonic-gate 	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
1438*0Sstevel@tonic-gate 
1439*0Sstevel@tonic-gate 	if (a->sessions != NULL)
1440*0Sstevel@tonic-gate 		lh_free(a->sessions);
1441*0Sstevel@tonic-gate 
1442*0Sstevel@tonic-gate 	if (a->cert_store != NULL)
1443*0Sstevel@tonic-gate 		X509_STORE_free(a->cert_store);
1444*0Sstevel@tonic-gate 	if (a->cipher_list != NULL)
1445*0Sstevel@tonic-gate 		sk_SSL_CIPHER_free(a->cipher_list);
1446*0Sstevel@tonic-gate 	if (a->cipher_list_by_id != NULL)
1447*0Sstevel@tonic-gate 		sk_SSL_CIPHER_free(a->cipher_list_by_id);
1448*0Sstevel@tonic-gate 	if (a->cert != NULL)
1449*0Sstevel@tonic-gate 		ssl_cert_free(a->cert);
1450*0Sstevel@tonic-gate 	if (a->client_CA != NULL)
1451*0Sstevel@tonic-gate 		sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);
1452*0Sstevel@tonic-gate 	if (a->extra_certs != NULL)
1453*0Sstevel@tonic-gate 		sk_X509_pop_free(a->extra_certs,X509_free);
1454*0Sstevel@tonic-gate #if 0 /* This should never be done, since it removes a global database */
1455*0Sstevel@tonic-gate 	if (a->comp_methods != NULL)
1456*0Sstevel@tonic-gate 		sk_SSL_COMP_pop_free(a->comp_methods,SSL_COMP_free);
1457*0Sstevel@tonic-gate #else
1458*0Sstevel@tonic-gate 	a->comp_methods = NULL;
1459*0Sstevel@tonic-gate #endif
1460*0Sstevel@tonic-gate 	OPENSSL_free(a);
1461*0Sstevel@tonic-gate 	}
1462*0Sstevel@tonic-gate 
1463*0Sstevel@tonic-gate void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
1464*0Sstevel@tonic-gate 	{
1465*0Sstevel@tonic-gate 	ctx->default_passwd_callback=cb;
1466*0Sstevel@tonic-gate 	}
1467*0Sstevel@tonic-gate 
1468*0Sstevel@tonic-gate void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u)
1469*0Sstevel@tonic-gate 	{
1470*0Sstevel@tonic-gate 	ctx->default_passwd_callback_userdata=u;
1471*0Sstevel@tonic-gate 	}
1472*0Sstevel@tonic-gate 
1473*0Sstevel@tonic-gate void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg)
1474*0Sstevel@tonic-gate 	{
1475*0Sstevel@tonic-gate 	ctx->app_verify_callback=cb;
1476*0Sstevel@tonic-gate 	ctx->app_verify_arg=arg;
1477*0Sstevel@tonic-gate 	}
1478*0Sstevel@tonic-gate 
1479*0Sstevel@tonic-gate void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
1480*0Sstevel@tonic-gate 	{
1481*0Sstevel@tonic-gate 	ctx->verify_mode=mode;
1482*0Sstevel@tonic-gate 	ctx->default_verify_callback=cb;
1483*0Sstevel@tonic-gate 	}
1484*0Sstevel@tonic-gate 
1485*0Sstevel@tonic-gate void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth)
1486*0Sstevel@tonic-gate 	{
1487*0Sstevel@tonic-gate 	ctx->verify_depth=depth;
1488*0Sstevel@tonic-gate 	}
1489*0Sstevel@tonic-gate 
1490*0Sstevel@tonic-gate void ssl_set_cert_masks(CERT *c, SSL_CIPHER *cipher)
1491*0Sstevel@tonic-gate 	{
1492*0Sstevel@tonic-gate 	CERT_PKEY *cpk;
1493*0Sstevel@tonic-gate 	int rsa_enc,rsa_tmp,rsa_sign,dh_tmp,dh_rsa,dh_dsa,dsa_sign;
1494*0Sstevel@tonic-gate 	int rsa_enc_export,dh_rsa_export,dh_dsa_export;
1495*0Sstevel@tonic-gate 	int rsa_tmp_export,dh_tmp_export,kl;
1496*0Sstevel@tonic-gate 	unsigned long mask,emask;
1497*0Sstevel@tonic-gate 
1498*0Sstevel@tonic-gate 	if (c == NULL) return;
1499*0Sstevel@tonic-gate 
1500*0Sstevel@tonic-gate 	kl=SSL_C_EXPORT_PKEYLENGTH(cipher);
1501*0Sstevel@tonic-gate 
1502*0Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
1503*0Sstevel@tonic-gate 	rsa_tmp=(c->rsa_tmp != NULL || c->rsa_tmp_cb != NULL);
1504*0Sstevel@tonic-gate 	rsa_tmp_export=(c->rsa_tmp_cb != NULL ||
1505*0Sstevel@tonic-gate 		(rsa_tmp && RSA_size(c->rsa_tmp)*8 <= kl));
1506*0Sstevel@tonic-gate #else
1507*0Sstevel@tonic-gate 	rsa_tmp=rsa_tmp_export=0;
1508*0Sstevel@tonic-gate #endif
1509*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
1510*0Sstevel@tonic-gate 	dh_tmp=(c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
1511*0Sstevel@tonic-gate 	dh_tmp_export=(c->dh_tmp_cb != NULL ||
1512*0Sstevel@tonic-gate 		(dh_tmp && DH_size(c->dh_tmp)*8 <= kl));
1513*0Sstevel@tonic-gate #else
1514*0Sstevel@tonic-gate 	dh_tmp=dh_tmp_export=0;
1515*0Sstevel@tonic-gate #endif
1516*0Sstevel@tonic-gate 
1517*0Sstevel@tonic-gate 	cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]);
1518*0Sstevel@tonic-gate 	rsa_enc= (cpk->x509 != NULL && cpk->privatekey != NULL);
1519*0Sstevel@tonic-gate 	rsa_enc_export=(rsa_enc && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
1520*0Sstevel@tonic-gate 	cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]);
1521*0Sstevel@tonic-gate 	rsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL);
1522*0Sstevel@tonic-gate 	cpk= &(c->pkeys[SSL_PKEY_DSA_SIGN]);
1523*0Sstevel@tonic-gate 	dsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL);
1524*0Sstevel@tonic-gate 	cpk= &(c->pkeys[SSL_PKEY_DH_RSA]);
1525*0Sstevel@tonic-gate 	dh_rsa=  (cpk->x509 != NULL && cpk->privatekey != NULL);
1526*0Sstevel@tonic-gate 	dh_rsa_export=(dh_rsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
1527*0Sstevel@tonic-gate 	cpk= &(c->pkeys[SSL_PKEY_DH_DSA]);
1528*0Sstevel@tonic-gate /* FIX THIS EAY EAY EAY */
1529*0Sstevel@tonic-gate 	dh_dsa=  (cpk->x509 != NULL && cpk->privatekey != NULL);
1530*0Sstevel@tonic-gate 	dh_dsa_export=(dh_dsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
1531*0Sstevel@tonic-gate 
1532*0Sstevel@tonic-gate 	mask=0;
1533*0Sstevel@tonic-gate 	emask=0;
1534*0Sstevel@tonic-gate 
1535*0Sstevel@tonic-gate #ifdef CIPHER_DEBUG
1536*0Sstevel@tonic-gate 	printf("rt=%d rte=%d dht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n",
1537*0Sstevel@tonic-gate 		rsa_tmp,rsa_tmp_export,dh_tmp,
1538*0Sstevel@tonic-gate 		rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
1539*0Sstevel@tonic-gate #endif
1540*0Sstevel@tonic-gate 
1541*0Sstevel@tonic-gate 	if (rsa_enc || (rsa_tmp && rsa_sign))
1542*0Sstevel@tonic-gate 		mask|=SSL_kRSA;
1543*0Sstevel@tonic-gate 	if (rsa_enc_export || (rsa_tmp_export && (rsa_sign || rsa_enc)))
1544*0Sstevel@tonic-gate 		emask|=SSL_kRSA;
1545*0Sstevel@tonic-gate 
1546*0Sstevel@tonic-gate #if 0
1547*0Sstevel@tonic-gate 	/* The match needs to be both kEDH and aRSA or aDSA, so don't worry */
1548*0Sstevel@tonic-gate 	if (	(dh_tmp || dh_rsa || dh_dsa) &&
1549*0Sstevel@tonic-gate 		(rsa_enc || rsa_sign || dsa_sign))
1550*0Sstevel@tonic-gate 		mask|=SSL_kEDH;
1551*0Sstevel@tonic-gate 	if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) &&
1552*0Sstevel@tonic-gate 		(rsa_enc || rsa_sign || dsa_sign))
1553*0Sstevel@tonic-gate 		emask|=SSL_kEDH;
1554*0Sstevel@tonic-gate #endif
1555*0Sstevel@tonic-gate 
1556*0Sstevel@tonic-gate 	if (dh_tmp_export)
1557*0Sstevel@tonic-gate 		emask|=SSL_kEDH;
1558*0Sstevel@tonic-gate 
1559*0Sstevel@tonic-gate 	if (dh_tmp)
1560*0Sstevel@tonic-gate 		mask|=SSL_kEDH;
1561*0Sstevel@tonic-gate 
1562*0Sstevel@tonic-gate 	if (dh_rsa) mask|=SSL_kDHr;
1563*0Sstevel@tonic-gate 	if (dh_rsa_export) emask|=SSL_kDHr;
1564*0Sstevel@tonic-gate 
1565*0Sstevel@tonic-gate 	if (dh_dsa) mask|=SSL_kDHd;
1566*0Sstevel@tonic-gate 	if (dh_dsa_export) emask|=SSL_kDHd;
1567*0Sstevel@tonic-gate 
1568*0Sstevel@tonic-gate 	if (rsa_enc || rsa_sign)
1569*0Sstevel@tonic-gate 		{
1570*0Sstevel@tonic-gate 		mask|=SSL_aRSA;
1571*0Sstevel@tonic-gate 		emask|=SSL_aRSA;
1572*0Sstevel@tonic-gate 		}
1573*0Sstevel@tonic-gate 
1574*0Sstevel@tonic-gate 	if (dsa_sign)
1575*0Sstevel@tonic-gate 		{
1576*0Sstevel@tonic-gate 		mask|=SSL_aDSS;
1577*0Sstevel@tonic-gate 		emask|=SSL_aDSS;
1578*0Sstevel@tonic-gate 		}
1579*0Sstevel@tonic-gate 
1580*0Sstevel@tonic-gate 	mask|=SSL_aNULL;
1581*0Sstevel@tonic-gate 	emask|=SSL_aNULL;
1582*0Sstevel@tonic-gate 
1583*0Sstevel@tonic-gate #ifndef OPENSSL_NO_KRB5
1584*0Sstevel@tonic-gate 	mask|=SSL_kKRB5|SSL_aKRB5;
1585*0Sstevel@tonic-gate 	emask|=SSL_kKRB5|SSL_aKRB5;
1586*0Sstevel@tonic-gate #endif
1587*0Sstevel@tonic-gate 
1588*0Sstevel@tonic-gate 	c->mask=mask;
1589*0Sstevel@tonic-gate 	c->export_mask=emask;
1590*0Sstevel@tonic-gate 	c->valid=1;
1591*0Sstevel@tonic-gate 	}
1592*0Sstevel@tonic-gate 
1593*0Sstevel@tonic-gate /* THIS NEEDS CLEANING UP */
1594*0Sstevel@tonic-gate X509 *ssl_get_server_send_cert(SSL *s)
1595*0Sstevel@tonic-gate 	{
1596*0Sstevel@tonic-gate 	unsigned long alg,mask,kalg;
1597*0Sstevel@tonic-gate 	CERT *c;
1598*0Sstevel@tonic-gate 	int i,is_export;
1599*0Sstevel@tonic-gate 
1600*0Sstevel@tonic-gate 	c=s->cert;
1601*0Sstevel@tonic-gate 	ssl_set_cert_masks(c, s->s3->tmp.new_cipher);
1602*0Sstevel@tonic-gate 	alg=s->s3->tmp.new_cipher->algorithms;
1603*0Sstevel@tonic-gate 	is_export=SSL_C_IS_EXPORT(s->s3->tmp.new_cipher);
1604*0Sstevel@tonic-gate 	mask=is_export?c->export_mask:c->mask;
1605*0Sstevel@tonic-gate 	kalg=alg&(SSL_MKEY_MASK|SSL_AUTH_MASK);
1606*0Sstevel@tonic-gate 
1607*0Sstevel@tonic-gate 	if 	(kalg & SSL_kDHr)
1608*0Sstevel@tonic-gate 		i=SSL_PKEY_DH_RSA;
1609*0Sstevel@tonic-gate 	else if (kalg & SSL_kDHd)
1610*0Sstevel@tonic-gate 		i=SSL_PKEY_DH_DSA;
1611*0Sstevel@tonic-gate 	else if (kalg & SSL_aDSS)
1612*0Sstevel@tonic-gate 		i=SSL_PKEY_DSA_SIGN;
1613*0Sstevel@tonic-gate 	else if (kalg & SSL_aRSA)
1614*0Sstevel@tonic-gate 		{
1615*0Sstevel@tonic-gate 		if (c->pkeys[SSL_PKEY_RSA_ENC].x509 == NULL)
1616*0Sstevel@tonic-gate 			i=SSL_PKEY_RSA_SIGN;
1617*0Sstevel@tonic-gate 		else
1618*0Sstevel@tonic-gate 			i=SSL_PKEY_RSA_ENC;
1619*0Sstevel@tonic-gate 		}
1620*0Sstevel@tonic-gate 	else if (kalg & SSL_aKRB5)
1621*0Sstevel@tonic-gate 		{
1622*0Sstevel@tonic-gate 		/* VRS something else here? */
1623*0Sstevel@tonic-gate 		return(NULL);
1624*0Sstevel@tonic-gate 		}
1625*0Sstevel@tonic-gate 	else /* if (kalg & SSL_aNULL) */
1626*0Sstevel@tonic-gate 		{
1627*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_GET_SERVER_SEND_CERT,ERR_R_INTERNAL_ERROR);
1628*0Sstevel@tonic-gate 		return(NULL);
1629*0Sstevel@tonic-gate 		}
1630*0Sstevel@tonic-gate 	if (c->pkeys[i].x509 == NULL) return(NULL);
1631*0Sstevel@tonic-gate 	return(c->pkeys[i].x509);
1632*0Sstevel@tonic-gate 	}
1633*0Sstevel@tonic-gate 
1634*0Sstevel@tonic-gate EVP_PKEY *ssl_get_sign_pkey(SSL *s,SSL_CIPHER *cipher)
1635*0Sstevel@tonic-gate 	{
1636*0Sstevel@tonic-gate 	unsigned long alg;
1637*0Sstevel@tonic-gate 	CERT *c;
1638*0Sstevel@tonic-gate 
1639*0Sstevel@tonic-gate 	alg=cipher->algorithms;
1640*0Sstevel@tonic-gate 	c=s->cert;
1641*0Sstevel@tonic-gate 
1642*0Sstevel@tonic-gate 	if ((alg & SSL_aDSS) &&
1643*0Sstevel@tonic-gate 		(c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL))
1644*0Sstevel@tonic-gate 		return(c->pkeys[SSL_PKEY_DSA_SIGN].privatekey);
1645*0Sstevel@tonic-gate 	else if (alg & SSL_aRSA)
1646*0Sstevel@tonic-gate 		{
1647*0Sstevel@tonic-gate 		if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
1648*0Sstevel@tonic-gate 			return(c->pkeys[SSL_PKEY_RSA_SIGN].privatekey);
1649*0Sstevel@tonic-gate 		else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
1650*0Sstevel@tonic-gate 			return(c->pkeys[SSL_PKEY_RSA_ENC].privatekey);
1651*0Sstevel@tonic-gate 		else
1652*0Sstevel@tonic-gate 			return(NULL);
1653*0Sstevel@tonic-gate 		}
1654*0Sstevel@tonic-gate 	else /* if (alg & SSL_aNULL) */
1655*0Sstevel@tonic-gate 		{
1656*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_GET_SIGN_PKEY,ERR_R_INTERNAL_ERROR);
1657*0Sstevel@tonic-gate 		return(NULL);
1658*0Sstevel@tonic-gate 		}
1659*0Sstevel@tonic-gate 	}
1660*0Sstevel@tonic-gate 
1661*0Sstevel@tonic-gate void ssl_update_cache(SSL *s,int mode)
1662*0Sstevel@tonic-gate 	{
1663*0Sstevel@tonic-gate 	int i;
1664*0Sstevel@tonic-gate 
1665*0Sstevel@tonic-gate 	/* If the session_id_length is 0, we are not supposed to cache it,
1666*0Sstevel@tonic-gate 	 * and it would be rather hard to do anyway :-) */
1667*0Sstevel@tonic-gate 	if (s->session->session_id_length == 0) return;
1668*0Sstevel@tonic-gate 
1669*0Sstevel@tonic-gate 	i=s->ctx->session_cache_mode;
1670*0Sstevel@tonic-gate 	if ((i & mode) && (!s->hit)
1671*0Sstevel@tonic-gate 		&& ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
1672*0Sstevel@tonic-gate 		    || SSL_CTX_add_session(s->ctx,s->session))
1673*0Sstevel@tonic-gate 		&& (s->ctx->new_session_cb != NULL))
1674*0Sstevel@tonic-gate 		{
1675*0Sstevel@tonic-gate 		CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
1676*0Sstevel@tonic-gate 		if (!s->ctx->new_session_cb(s,s->session))
1677*0Sstevel@tonic-gate 			SSL_SESSION_free(s->session);
1678*0Sstevel@tonic-gate 		}
1679*0Sstevel@tonic-gate 
1680*0Sstevel@tonic-gate 	/* auto flush every 255 connections */
1681*0Sstevel@tonic-gate 	if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
1682*0Sstevel@tonic-gate 		((i & mode) == mode))
1683*0Sstevel@tonic-gate 		{
1684*0Sstevel@tonic-gate 		if (  (((mode & SSL_SESS_CACHE_CLIENT)
1685*0Sstevel@tonic-gate 			?s->ctx->stats.sess_connect_good
1686*0Sstevel@tonic-gate 			:s->ctx->stats.sess_accept_good) & 0xff) == 0xff)
1687*0Sstevel@tonic-gate 			{
1688*0Sstevel@tonic-gate 			SSL_CTX_flush_sessions(s->ctx,time(NULL));
1689*0Sstevel@tonic-gate 			}
1690*0Sstevel@tonic-gate 		}
1691*0Sstevel@tonic-gate 	}
1692*0Sstevel@tonic-gate 
1693*0Sstevel@tonic-gate SSL_METHOD *SSL_get_ssl_method(SSL *s)
1694*0Sstevel@tonic-gate 	{
1695*0Sstevel@tonic-gate 	return(s->method);
1696*0Sstevel@tonic-gate 	}
1697*0Sstevel@tonic-gate 
1698*0Sstevel@tonic-gate int SSL_set_ssl_method(SSL *s,SSL_METHOD *meth)
1699*0Sstevel@tonic-gate 	{
1700*0Sstevel@tonic-gate 	int conn= -1;
1701*0Sstevel@tonic-gate 	int ret=1;
1702*0Sstevel@tonic-gate 
1703*0Sstevel@tonic-gate 	if (s->method != meth)
1704*0Sstevel@tonic-gate 		{
1705*0Sstevel@tonic-gate 		if (s->handshake_func != NULL)
1706*0Sstevel@tonic-gate 			conn=(s->handshake_func == s->method->ssl_connect);
1707*0Sstevel@tonic-gate 
1708*0Sstevel@tonic-gate 		if (s->method->version == meth->version)
1709*0Sstevel@tonic-gate 			s->method=meth;
1710*0Sstevel@tonic-gate 		else
1711*0Sstevel@tonic-gate 			{
1712*0Sstevel@tonic-gate 			s->method->ssl_free(s);
1713*0Sstevel@tonic-gate 			s->method=meth;
1714*0Sstevel@tonic-gate 			ret=s->method->ssl_new(s);
1715*0Sstevel@tonic-gate 			}
1716*0Sstevel@tonic-gate 
1717*0Sstevel@tonic-gate 		if (conn == 1)
1718*0Sstevel@tonic-gate 			s->handshake_func=meth->ssl_connect;
1719*0Sstevel@tonic-gate 		else if (conn == 0)
1720*0Sstevel@tonic-gate 			s->handshake_func=meth->ssl_accept;
1721*0Sstevel@tonic-gate 		}
1722*0Sstevel@tonic-gate 	return(ret);
1723*0Sstevel@tonic-gate 	}
1724*0Sstevel@tonic-gate 
1725*0Sstevel@tonic-gate int SSL_get_error(SSL *s,int i)
1726*0Sstevel@tonic-gate 	{
1727*0Sstevel@tonic-gate 	int reason;
1728*0Sstevel@tonic-gate 	unsigned long l;
1729*0Sstevel@tonic-gate 	BIO *bio;
1730*0Sstevel@tonic-gate 
1731*0Sstevel@tonic-gate 	if (i > 0) return(SSL_ERROR_NONE);
1732*0Sstevel@tonic-gate 
1733*0Sstevel@tonic-gate 	/* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
1734*0Sstevel@tonic-gate 	 * etc, where we do encode the error */
1735*0Sstevel@tonic-gate 	if ((l=ERR_peek_error()) != 0)
1736*0Sstevel@tonic-gate 		{
1737*0Sstevel@tonic-gate 		if (ERR_GET_LIB(l) == ERR_LIB_SYS)
1738*0Sstevel@tonic-gate 			return(SSL_ERROR_SYSCALL);
1739*0Sstevel@tonic-gate 		else
1740*0Sstevel@tonic-gate 			return(SSL_ERROR_SSL);
1741*0Sstevel@tonic-gate 		}
1742*0Sstevel@tonic-gate 
1743*0Sstevel@tonic-gate 	if ((i < 0) && SSL_want_read(s))
1744*0Sstevel@tonic-gate 		{
1745*0Sstevel@tonic-gate 		bio=SSL_get_rbio(s);
1746*0Sstevel@tonic-gate 		if (BIO_should_read(bio))
1747*0Sstevel@tonic-gate 			return(SSL_ERROR_WANT_READ);
1748*0Sstevel@tonic-gate 		else if (BIO_should_write(bio))
1749*0Sstevel@tonic-gate 			/* This one doesn't make too much sense ... We never try
1750*0Sstevel@tonic-gate 			 * to write to the rbio, and an application program where
1751*0Sstevel@tonic-gate 			 * rbio and wbio are separate couldn't even know what it
1752*0Sstevel@tonic-gate 			 * should wait for.
1753*0Sstevel@tonic-gate 			 * However if we ever set s->rwstate incorrectly
1754*0Sstevel@tonic-gate 			 * (so that we have SSL_want_read(s) instead of
1755*0Sstevel@tonic-gate 			 * SSL_want_write(s)) and rbio and wbio *are* the same,
1756*0Sstevel@tonic-gate 			 * this test works around that bug; so it might be safer
1757*0Sstevel@tonic-gate 			 * to keep it. */
1758*0Sstevel@tonic-gate 			return(SSL_ERROR_WANT_WRITE);
1759*0Sstevel@tonic-gate 		else if (BIO_should_io_special(bio))
1760*0Sstevel@tonic-gate 			{
1761*0Sstevel@tonic-gate 			reason=BIO_get_retry_reason(bio);
1762*0Sstevel@tonic-gate 			if (reason == BIO_RR_CONNECT)
1763*0Sstevel@tonic-gate 				return(SSL_ERROR_WANT_CONNECT);
1764*0Sstevel@tonic-gate 			else if (reason == BIO_RR_ACCEPT)
1765*0Sstevel@tonic-gate 				return(SSL_ERROR_WANT_ACCEPT);
1766*0Sstevel@tonic-gate 			else
1767*0Sstevel@tonic-gate 				return(SSL_ERROR_SYSCALL); /* unknown */
1768*0Sstevel@tonic-gate 			}
1769*0Sstevel@tonic-gate 		}
1770*0Sstevel@tonic-gate 
1771*0Sstevel@tonic-gate 	if ((i < 0) && SSL_want_write(s))
1772*0Sstevel@tonic-gate 		{
1773*0Sstevel@tonic-gate 		bio=SSL_get_wbio(s);
1774*0Sstevel@tonic-gate 		if (BIO_should_write(bio))
1775*0Sstevel@tonic-gate 			return(SSL_ERROR_WANT_WRITE);
1776*0Sstevel@tonic-gate 		else if (BIO_should_read(bio))
1777*0Sstevel@tonic-gate 			/* See above (SSL_want_read(s) with BIO_should_write(bio)) */
1778*0Sstevel@tonic-gate 			return(SSL_ERROR_WANT_READ);
1779*0Sstevel@tonic-gate 		else if (BIO_should_io_special(bio))
1780*0Sstevel@tonic-gate 			{
1781*0Sstevel@tonic-gate 			reason=BIO_get_retry_reason(bio);
1782*0Sstevel@tonic-gate 			if (reason == BIO_RR_CONNECT)
1783*0Sstevel@tonic-gate 				return(SSL_ERROR_WANT_CONNECT);
1784*0Sstevel@tonic-gate 			else if (reason == BIO_RR_ACCEPT)
1785*0Sstevel@tonic-gate 				return(SSL_ERROR_WANT_ACCEPT);
1786*0Sstevel@tonic-gate 			else
1787*0Sstevel@tonic-gate 				return(SSL_ERROR_SYSCALL);
1788*0Sstevel@tonic-gate 			}
1789*0Sstevel@tonic-gate 		}
1790*0Sstevel@tonic-gate 	if ((i < 0) && SSL_want_x509_lookup(s))
1791*0Sstevel@tonic-gate 		{
1792*0Sstevel@tonic-gate 		return(SSL_ERROR_WANT_X509_LOOKUP);
1793*0Sstevel@tonic-gate 		}
1794*0Sstevel@tonic-gate 
1795*0Sstevel@tonic-gate 	if (i == 0)
1796*0Sstevel@tonic-gate 		{
1797*0Sstevel@tonic-gate 		if (s->version == SSL2_VERSION)
1798*0Sstevel@tonic-gate 			{
1799*0Sstevel@tonic-gate 			/* assume it is the socket being closed */
1800*0Sstevel@tonic-gate 			return(SSL_ERROR_ZERO_RETURN);
1801*0Sstevel@tonic-gate 			}
1802*0Sstevel@tonic-gate 		else
1803*0Sstevel@tonic-gate 			{
1804*0Sstevel@tonic-gate 			if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
1805*0Sstevel@tonic-gate 				(s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
1806*0Sstevel@tonic-gate 				return(SSL_ERROR_ZERO_RETURN);
1807*0Sstevel@tonic-gate 			}
1808*0Sstevel@tonic-gate 		}
1809*0Sstevel@tonic-gate 	return(SSL_ERROR_SYSCALL);
1810*0Sstevel@tonic-gate 	}
1811*0Sstevel@tonic-gate 
1812*0Sstevel@tonic-gate int SSL_do_handshake(SSL *s)
1813*0Sstevel@tonic-gate 	{
1814*0Sstevel@tonic-gate 	int ret=1;
1815*0Sstevel@tonic-gate 
1816*0Sstevel@tonic-gate 	if (s->handshake_func == NULL)
1817*0Sstevel@tonic-gate 		{
1818*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_DO_HANDSHAKE,SSL_R_CONNECTION_TYPE_NOT_SET);
1819*0Sstevel@tonic-gate 		return(-1);
1820*0Sstevel@tonic-gate 		}
1821*0Sstevel@tonic-gate 
1822*0Sstevel@tonic-gate 	s->method->ssl_renegotiate_check(s);
1823*0Sstevel@tonic-gate 
1824*0Sstevel@tonic-gate 	if (SSL_in_init(s) || SSL_in_before(s))
1825*0Sstevel@tonic-gate 		{
1826*0Sstevel@tonic-gate 		ret=s->handshake_func(s);
1827*0Sstevel@tonic-gate 		}
1828*0Sstevel@tonic-gate 	return(ret);
1829*0Sstevel@tonic-gate 	}
1830*0Sstevel@tonic-gate 
1831*0Sstevel@tonic-gate /* For the next 2 functions, SSL_clear() sets shutdown and so
1832*0Sstevel@tonic-gate  * one of these calls will reset it */
1833*0Sstevel@tonic-gate void SSL_set_accept_state(SSL *s)
1834*0Sstevel@tonic-gate 	{
1835*0Sstevel@tonic-gate 	s->server=1;
1836*0Sstevel@tonic-gate 	s->shutdown=0;
1837*0Sstevel@tonic-gate 	s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
1838*0Sstevel@tonic-gate 	s->handshake_func=s->method->ssl_accept;
1839*0Sstevel@tonic-gate 	/* clear the current cipher */
1840*0Sstevel@tonic-gate 	ssl_clear_cipher_ctx(s);
1841*0Sstevel@tonic-gate 	}
1842*0Sstevel@tonic-gate 
1843*0Sstevel@tonic-gate void SSL_set_connect_state(SSL *s)
1844*0Sstevel@tonic-gate 	{
1845*0Sstevel@tonic-gate 	s->server=0;
1846*0Sstevel@tonic-gate 	s->shutdown=0;
1847*0Sstevel@tonic-gate 	s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
1848*0Sstevel@tonic-gate 	s->handshake_func=s->method->ssl_connect;
1849*0Sstevel@tonic-gate 	/* clear the current cipher */
1850*0Sstevel@tonic-gate 	ssl_clear_cipher_ctx(s);
1851*0Sstevel@tonic-gate 	}
1852*0Sstevel@tonic-gate 
1853*0Sstevel@tonic-gate int ssl_undefined_function(SSL *s)
1854*0Sstevel@tonic-gate 	{
1855*0Sstevel@tonic-gate 	SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1856*0Sstevel@tonic-gate 	return(0);
1857*0Sstevel@tonic-gate 	}
1858*0Sstevel@tonic-gate 
1859*0Sstevel@tonic-gate SSL_METHOD *ssl_bad_method(int ver)
1860*0Sstevel@tonic-gate 	{
1861*0Sstevel@tonic-gate 	SSLerr(SSL_F_SSL_BAD_METHOD,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1862*0Sstevel@tonic-gate 	return(NULL);
1863*0Sstevel@tonic-gate 	}
1864*0Sstevel@tonic-gate 
1865*0Sstevel@tonic-gate const char *SSL_get_version(SSL *s)
1866*0Sstevel@tonic-gate 	{
1867*0Sstevel@tonic-gate 	if (s->version == TLS1_VERSION)
1868*0Sstevel@tonic-gate 		return("TLSv1");
1869*0Sstevel@tonic-gate 	else if (s->version == SSL3_VERSION)
1870*0Sstevel@tonic-gate 		return("SSLv3");
1871*0Sstevel@tonic-gate 	else if (s->version == SSL2_VERSION)
1872*0Sstevel@tonic-gate 		return("SSLv2");
1873*0Sstevel@tonic-gate 	else
1874*0Sstevel@tonic-gate 		return("unknown");
1875*0Sstevel@tonic-gate 	}
1876*0Sstevel@tonic-gate 
1877*0Sstevel@tonic-gate SSL *SSL_dup(SSL *s)
1878*0Sstevel@tonic-gate 	{
1879*0Sstevel@tonic-gate 	STACK_OF(X509_NAME) *sk;
1880*0Sstevel@tonic-gate 	X509_NAME *xn;
1881*0Sstevel@tonic-gate 	SSL *ret;
1882*0Sstevel@tonic-gate 	int i;
1883*0Sstevel@tonic-gate 
1884*0Sstevel@tonic-gate 	if ((ret=SSL_new(SSL_get_SSL_CTX(s))) == NULL)
1885*0Sstevel@tonic-gate 	    return(NULL);
1886*0Sstevel@tonic-gate 
1887*0Sstevel@tonic-gate 	ret->version = s->version;
1888*0Sstevel@tonic-gate 	ret->type = s->type;
1889*0Sstevel@tonic-gate 	ret->method = s->method;
1890*0Sstevel@tonic-gate 
1891*0Sstevel@tonic-gate 	if (s->session != NULL)
1892*0Sstevel@tonic-gate 		{
1893*0Sstevel@tonic-gate 		/* This copies session-id, SSL_METHOD, sid_ctx, and 'cert' */
1894*0Sstevel@tonic-gate 		SSL_copy_session_id(ret,s);
1895*0Sstevel@tonic-gate 		}
1896*0Sstevel@tonic-gate 	else
1897*0Sstevel@tonic-gate 		{
1898*0Sstevel@tonic-gate 		/* No session has been established yet, so we have to expect
1899*0Sstevel@tonic-gate 		 * that s->cert or ret->cert will be changed later --
1900*0Sstevel@tonic-gate 		 * they should not both point to the same object,
1901*0Sstevel@tonic-gate 		 * and thus we can't use SSL_copy_session_id. */
1902*0Sstevel@tonic-gate 
1903*0Sstevel@tonic-gate 		ret->method->ssl_free(ret);
1904*0Sstevel@tonic-gate 		ret->method = s->method;
1905*0Sstevel@tonic-gate 		ret->method->ssl_new(ret);
1906*0Sstevel@tonic-gate 
1907*0Sstevel@tonic-gate 		if (s->cert != NULL)
1908*0Sstevel@tonic-gate 			{
1909*0Sstevel@tonic-gate 			if (ret->cert != NULL)
1910*0Sstevel@tonic-gate 				{
1911*0Sstevel@tonic-gate 				ssl_cert_free(ret->cert);
1912*0Sstevel@tonic-gate 				}
1913*0Sstevel@tonic-gate 			ret->cert = ssl_cert_dup(s->cert);
1914*0Sstevel@tonic-gate 			if (ret->cert == NULL)
1915*0Sstevel@tonic-gate 				goto err;
1916*0Sstevel@tonic-gate 			}
1917*0Sstevel@tonic-gate 
1918*0Sstevel@tonic-gate 		SSL_set_session_id_context(ret,
1919*0Sstevel@tonic-gate 			s->sid_ctx, s->sid_ctx_length);
1920*0Sstevel@tonic-gate 		}
1921*0Sstevel@tonic-gate 
1922*0Sstevel@tonic-gate 	ret->options=s->options;
1923*0Sstevel@tonic-gate 	ret->mode=s->mode;
1924*0Sstevel@tonic-gate 	SSL_set_max_cert_list(ret,SSL_get_max_cert_list(s));
1925*0Sstevel@tonic-gate 	SSL_set_read_ahead(ret,SSL_get_read_ahead(s));
1926*0Sstevel@tonic-gate 	ret->msg_callback = s->msg_callback;
1927*0Sstevel@tonic-gate 	ret->msg_callback_arg = s->msg_callback_arg;
1928*0Sstevel@tonic-gate 	SSL_set_verify(ret,SSL_get_verify_mode(s),
1929*0Sstevel@tonic-gate 		SSL_get_verify_callback(s));
1930*0Sstevel@tonic-gate 	SSL_set_verify_depth(ret,SSL_get_verify_depth(s));
1931*0Sstevel@tonic-gate 	ret->generate_session_id = s->generate_session_id;
1932*0Sstevel@tonic-gate 
1933*0Sstevel@tonic-gate 	SSL_set_info_callback(ret,SSL_get_info_callback(s));
1934*0Sstevel@tonic-gate 
1935*0Sstevel@tonic-gate 	ret->debug=s->debug;
1936*0Sstevel@tonic-gate 
1937*0Sstevel@tonic-gate 	/* copy app data, a little dangerous perhaps */
1938*0Sstevel@tonic-gate 	if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data))
1939*0Sstevel@tonic-gate 		goto err;
1940*0Sstevel@tonic-gate 
1941*0Sstevel@tonic-gate 	/* setup rbio, and wbio */
1942*0Sstevel@tonic-gate 	if (s->rbio != NULL)
1943*0Sstevel@tonic-gate 		{
1944*0Sstevel@tonic-gate 		if (!BIO_dup_state(s->rbio,(char *)&ret->rbio))
1945*0Sstevel@tonic-gate 			goto err;
1946*0Sstevel@tonic-gate 		}
1947*0Sstevel@tonic-gate 	if (s->wbio != NULL)
1948*0Sstevel@tonic-gate 		{
1949*0Sstevel@tonic-gate 		if (s->wbio != s->rbio)
1950*0Sstevel@tonic-gate 			{
1951*0Sstevel@tonic-gate 			if (!BIO_dup_state(s->wbio,(char *)&ret->wbio))
1952*0Sstevel@tonic-gate 				goto err;
1953*0Sstevel@tonic-gate 			}
1954*0Sstevel@tonic-gate 		else
1955*0Sstevel@tonic-gate 			ret->wbio=ret->rbio;
1956*0Sstevel@tonic-gate 		}
1957*0Sstevel@tonic-gate 	ret->rwstate = s->rwstate;
1958*0Sstevel@tonic-gate 	ret->in_handshake = s->in_handshake;
1959*0Sstevel@tonic-gate 	ret->handshake_func = s->handshake_func;
1960*0Sstevel@tonic-gate 	ret->server = s->server;
1961*0Sstevel@tonic-gate 	ret->new_session = s->new_session;
1962*0Sstevel@tonic-gate 	ret->quiet_shutdown = s->quiet_shutdown;
1963*0Sstevel@tonic-gate 	ret->shutdown=s->shutdown;
1964*0Sstevel@tonic-gate 	ret->state=s->state; /* SSL_dup does not really work at any state, though */
1965*0Sstevel@tonic-gate 	ret->rstate=s->rstate;
1966*0Sstevel@tonic-gate 	ret->init_num = 0; /* would have to copy ret->init_buf, ret->init_msg, ret->init_num, ret->init_off */
1967*0Sstevel@tonic-gate 	ret->hit=s->hit;
1968*0Sstevel@tonic-gate 	ret->purpose=s->purpose;
1969*0Sstevel@tonic-gate 	ret->trust=s->trust;
1970*0Sstevel@tonic-gate 
1971*0Sstevel@tonic-gate 	/* dup the cipher_list and cipher_list_by_id stacks */
1972*0Sstevel@tonic-gate 	if (s->cipher_list != NULL)
1973*0Sstevel@tonic-gate 		{
1974*0Sstevel@tonic-gate 		if ((ret->cipher_list=sk_SSL_CIPHER_dup(s->cipher_list)) == NULL)
1975*0Sstevel@tonic-gate 			goto err;
1976*0Sstevel@tonic-gate 		}
1977*0Sstevel@tonic-gate 	if (s->cipher_list_by_id != NULL)
1978*0Sstevel@tonic-gate 		if ((ret->cipher_list_by_id=sk_SSL_CIPHER_dup(s->cipher_list_by_id))
1979*0Sstevel@tonic-gate 			== NULL)
1980*0Sstevel@tonic-gate 			goto err;
1981*0Sstevel@tonic-gate 
1982*0Sstevel@tonic-gate 	/* Dup the client_CA list */
1983*0Sstevel@tonic-gate 	if (s->client_CA != NULL)
1984*0Sstevel@tonic-gate 		{
1985*0Sstevel@tonic-gate 		if ((sk=sk_X509_NAME_dup(s->client_CA)) == NULL) goto err;
1986*0Sstevel@tonic-gate 		ret->client_CA=sk;
1987*0Sstevel@tonic-gate 		for (i=0; i<sk_X509_NAME_num(sk); i++)
1988*0Sstevel@tonic-gate 			{
1989*0Sstevel@tonic-gate 			xn=sk_X509_NAME_value(sk,i);
1990*0Sstevel@tonic-gate 			if (sk_X509_NAME_set(sk,i,X509_NAME_dup(xn)) == NULL)
1991*0Sstevel@tonic-gate 				{
1992*0Sstevel@tonic-gate 				X509_NAME_free(xn);
1993*0Sstevel@tonic-gate 				goto err;
1994*0Sstevel@tonic-gate 				}
1995*0Sstevel@tonic-gate 			}
1996*0Sstevel@tonic-gate 		}
1997*0Sstevel@tonic-gate 
1998*0Sstevel@tonic-gate 	if (0)
1999*0Sstevel@tonic-gate 		{
2000*0Sstevel@tonic-gate err:
2001*0Sstevel@tonic-gate 		if (ret != NULL) SSL_free(ret);
2002*0Sstevel@tonic-gate 		ret=NULL;
2003*0Sstevel@tonic-gate 		}
2004*0Sstevel@tonic-gate 	return(ret);
2005*0Sstevel@tonic-gate 	}
2006*0Sstevel@tonic-gate 
2007*0Sstevel@tonic-gate void ssl_clear_cipher_ctx(SSL *s)
2008*0Sstevel@tonic-gate 	{
2009*0Sstevel@tonic-gate 	if (s->enc_read_ctx != NULL)
2010*0Sstevel@tonic-gate 		{
2011*0Sstevel@tonic-gate 		EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
2012*0Sstevel@tonic-gate 		OPENSSL_free(s->enc_read_ctx);
2013*0Sstevel@tonic-gate 		s->enc_read_ctx=NULL;
2014*0Sstevel@tonic-gate 		}
2015*0Sstevel@tonic-gate 	if (s->enc_write_ctx != NULL)
2016*0Sstevel@tonic-gate 		{
2017*0Sstevel@tonic-gate 		EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
2018*0Sstevel@tonic-gate 		OPENSSL_free(s->enc_write_ctx);
2019*0Sstevel@tonic-gate 		s->enc_write_ctx=NULL;
2020*0Sstevel@tonic-gate 		}
2021*0Sstevel@tonic-gate 	if (s->expand != NULL)
2022*0Sstevel@tonic-gate 		{
2023*0Sstevel@tonic-gate 		COMP_CTX_free(s->expand);
2024*0Sstevel@tonic-gate 		s->expand=NULL;
2025*0Sstevel@tonic-gate 		}
2026*0Sstevel@tonic-gate 	if (s->compress != NULL)
2027*0Sstevel@tonic-gate 		{
2028*0Sstevel@tonic-gate 		COMP_CTX_free(s->compress);
2029*0Sstevel@tonic-gate 		s->compress=NULL;
2030*0Sstevel@tonic-gate 		}
2031*0Sstevel@tonic-gate 	}
2032*0Sstevel@tonic-gate 
2033*0Sstevel@tonic-gate /* Fix this function so that it takes an optional type parameter */
2034*0Sstevel@tonic-gate X509 *SSL_get_certificate(SSL *s)
2035*0Sstevel@tonic-gate 	{
2036*0Sstevel@tonic-gate 	if (s->cert != NULL)
2037*0Sstevel@tonic-gate 		return(s->cert->key->x509);
2038*0Sstevel@tonic-gate 	else
2039*0Sstevel@tonic-gate 		return(NULL);
2040*0Sstevel@tonic-gate 	}
2041*0Sstevel@tonic-gate 
2042*0Sstevel@tonic-gate /* Fix this function so that it takes an optional type parameter */
2043*0Sstevel@tonic-gate EVP_PKEY *SSL_get_privatekey(SSL *s)
2044*0Sstevel@tonic-gate 	{
2045*0Sstevel@tonic-gate 	if (s->cert != NULL)
2046*0Sstevel@tonic-gate 		return(s->cert->key->privatekey);
2047*0Sstevel@tonic-gate 	else
2048*0Sstevel@tonic-gate 		return(NULL);
2049*0Sstevel@tonic-gate 	}
2050*0Sstevel@tonic-gate 
2051*0Sstevel@tonic-gate SSL_CIPHER *SSL_get_current_cipher(SSL *s)
2052*0Sstevel@tonic-gate 	{
2053*0Sstevel@tonic-gate 	if ((s->session != NULL) && (s->session->cipher != NULL))
2054*0Sstevel@tonic-gate 		return(s->session->cipher);
2055*0Sstevel@tonic-gate 	return(NULL);
2056*0Sstevel@tonic-gate 	}
2057*0Sstevel@tonic-gate 
2058*0Sstevel@tonic-gate int ssl_init_wbio_buffer(SSL *s,int push)
2059*0Sstevel@tonic-gate 	{
2060*0Sstevel@tonic-gate 	BIO *bbio;
2061*0Sstevel@tonic-gate 
2062*0Sstevel@tonic-gate 	if (s->bbio == NULL)
2063*0Sstevel@tonic-gate 		{
2064*0Sstevel@tonic-gate 		bbio=BIO_new(BIO_f_buffer());
2065*0Sstevel@tonic-gate 		if (bbio == NULL) return(0);
2066*0Sstevel@tonic-gate 		s->bbio=bbio;
2067*0Sstevel@tonic-gate 		}
2068*0Sstevel@tonic-gate 	else
2069*0Sstevel@tonic-gate 		{
2070*0Sstevel@tonic-gate 		bbio=s->bbio;
2071*0Sstevel@tonic-gate 		if (s->bbio == s->wbio)
2072*0Sstevel@tonic-gate 			s->wbio=BIO_pop(s->wbio);
2073*0Sstevel@tonic-gate 		}
2074*0Sstevel@tonic-gate 	(void)BIO_reset(bbio);
2075*0Sstevel@tonic-gate /*	if (!BIO_set_write_buffer_size(bbio,16*1024)) */
2076*0Sstevel@tonic-gate 	if (!BIO_set_read_buffer_size(bbio,1))
2077*0Sstevel@tonic-gate 		{
2078*0Sstevel@tonic-gate 		SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER,ERR_R_BUF_LIB);
2079*0Sstevel@tonic-gate 		return(0);
2080*0Sstevel@tonic-gate 		}
2081*0Sstevel@tonic-gate 	if (push)
2082*0Sstevel@tonic-gate 		{
2083*0Sstevel@tonic-gate 		if (s->wbio != bbio)
2084*0Sstevel@tonic-gate 			s->wbio=BIO_push(bbio,s->wbio);
2085*0Sstevel@tonic-gate 		}
2086*0Sstevel@tonic-gate 	else
2087*0Sstevel@tonic-gate 		{
2088*0Sstevel@tonic-gate 		if (s->wbio == bbio)
2089*0Sstevel@tonic-gate 			s->wbio=BIO_pop(bbio);
2090*0Sstevel@tonic-gate 		}
2091*0Sstevel@tonic-gate 	return(1);
2092*0Sstevel@tonic-gate 	}
2093*0Sstevel@tonic-gate 
2094*0Sstevel@tonic-gate void ssl_free_wbio_buffer(SSL *s)
2095*0Sstevel@tonic-gate 	{
2096*0Sstevel@tonic-gate 	if (s->bbio == NULL) return;
2097*0Sstevel@tonic-gate 
2098*0Sstevel@tonic-gate 	if (s->bbio == s->wbio)
2099*0Sstevel@tonic-gate 		{
2100*0Sstevel@tonic-gate 		/* remove buffering */
2101*0Sstevel@tonic-gate 		s->wbio=BIO_pop(s->wbio);
2102*0Sstevel@tonic-gate #ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
2103*0Sstevel@tonic-gate 		assert(s->wbio != NULL);
2104*0Sstevel@tonic-gate #endif
2105*0Sstevel@tonic-gate 	}
2106*0Sstevel@tonic-gate 	BIO_free(s->bbio);
2107*0Sstevel@tonic-gate 	s->bbio=NULL;
2108*0Sstevel@tonic-gate 	}
2109*0Sstevel@tonic-gate 
2110*0Sstevel@tonic-gate void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
2111*0Sstevel@tonic-gate 	{
2112*0Sstevel@tonic-gate 	ctx->quiet_shutdown=mode;
2113*0Sstevel@tonic-gate 	}
2114*0Sstevel@tonic-gate 
2115*0Sstevel@tonic-gate int SSL_CTX_get_quiet_shutdown(SSL_CTX *ctx)
2116*0Sstevel@tonic-gate 	{
2117*0Sstevel@tonic-gate 	return(ctx->quiet_shutdown);
2118*0Sstevel@tonic-gate 	}
2119*0Sstevel@tonic-gate 
2120*0Sstevel@tonic-gate void SSL_set_quiet_shutdown(SSL *s,int mode)
2121*0Sstevel@tonic-gate 	{
2122*0Sstevel@tonic-gate 	s->quiet_shutdown=mode;
2123*0Sstevel@tonic-gate 	}
2124*0Sstevel@tonic-gate 
2125*0Sstevel@tonic-gate int SSL_get_quiet_shutdown(SSL *s)
2126*0Sstevel@tonic-gate 	{
2127*0Sstevel@tonic-gate 	return(s->quiet_shutdown);
2128*0Sstevel@tonic-gate 	}
2129*0Sstevel@tonic-gate 
2130*0Sstevel@tonic-gate void SSL_set_shutdown(SSL *s,int mode)
2131*0Sstevel@tonic-gate 	{
2132*0Sstevel@tonic-gate 	s->shutdown=mode;
2133*0Sstevel@tonic-gate 	}
2134*0Sstevel@tonic-gate 
2135*0Sstevel@tonic-gate int SSL_get_shutdown(SSL *s)
2136*0Sstevel@tonic-gate 	{
2137*0Sstevel@tonic-gate 	return(s->shutdown);
2138*0Sstevel@tonic-gate 	}
2139*0Sstevel@tonic-gate 
2140*0Sstevel@tonic-gate int SSL_version(SSL *s)
2141*0Sstevel@tonic-gate 	{
2142*0Sstevel@tonic-gate 	return(s->version);
2143*0Sstevel@tonic-gate 	}
2144*0Sstevel@tonic-gate 
2145*0Sstevel@tonic-gate SSL_CTX *SSL_get_SSL_CTX(SSL *ssl)
2146*0Sstevel@tonic-gate 	{
2147*0Sstevel@tonic-gate 	return(ssl->ctx);
2148*0Sstevel@tonic-gate 	}
2149*0Sstevel@tonic-gate 
2150*0Sstevel@tonic-gate #ifndef OPENSSL_NO_STDIO
2151*0Sstevel@tonic-gate int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
2152*0Sstevel@tonic-gate 	{
2153*0Sstevel@tonic-gate 	return(X509_STORE_set_default_paths(ctx->cert_store));
2154*0Sstevel@tonic-gate 	}
2155*0Sstevel@tonic-gate 
2156*0Sstevel@tonic-gate int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
2157*0Sstevel@tonic-gate 		const char *CApath)
2158*0Sstevel@tonic-gate 	{
2159*0Sstevel@tonic-gate 	return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
2160*0Sstevel@tonic-gate 	}
2161*0Sstevel@tonic-gate #endif
2162*0Sstevel@tonic-gate 
2163*0Sstevel@tonic-gate void SSL_set_info_callback(SSL *ssl,
2164*0Sstevel@tonic-gate 			   void (*cb)(const SSL *ssl,int type,int val))
2165*0Sstevel@tonic-gate 	{
2166*0Sstevel@tonic-gate 	ssl->info_callback=cb;
2167*0Sstevel@tonic-gate 	}
2168*0Sstevel@tonic-gate 
2169*0Sstevel@tonic-gate void (*SSL_get_info_callback(SSL *ssl))(const SSL *ssl,int type,int val)
2170*0Sstevel@tonic-gate 	{
2171*0Sstevel@tonic-gate 	return ssl->info_callback;
2172*0Sstevel@tonic-gate 	}
2173*0Sstevel@tonic-gate 
2174*0Sstevel@tonic-gate int SSL_state(SSL *ssl)
2175*0Sstevel@tonic-gate 	{
2176*0Sstevel@tonic-gate 	return(ssl->state);
2177*0Sstevel@tonic-gate 	}
2178*0Sstevel@tonic-gate 
2179*0Sstevel@tonic-gate void SSL_set_verify_result(SSL *ssl,long arg)
2180*0Sstevel@tonic-gate 	{
2181*0Sstevel@tonic-gate 	ssl->verify_result=arg;
2182*0Sstevel@tonic-gate 	}
2183*0Sstevel@tonic-gate 
2184*0Sstevel@tonic-gate long SSL_get_verify_result(SSL *ssl)
2185*0Sstevel@tonic-gate 	{
2186*0Sstevel@tonic-gate 	return(ssl->verify_result);
2187*0Sstevel@tonic-gate 	}
2188*0Sstevel@tonic-gate 
2189*0Sstevel@tonic-gate int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2190*0Sstevel@tonic-gate 			 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2191*0Sstevel@tonic-gate 	{
2192*0Sstevel@tonic-gate 	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
2193*0Sstevel@tonic-gate 				new_func, dup_func, free_func);
2194*0Sstevel@tonic-gate 	}
2195*0Sstevel@tonic-gate 
2196*0Sstevel@tonic-gate int SSL_set_ex_data(SSL *s,int idx,void *arg)
2197*0Sstevel@tonic-gate 	{
2198*0Sstevel@tonic-gate 	return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2199*0Sstevel@tonic-gate 	}
2200*0Sstevel@tonic-gate 
2201*0Sstevel@tonic-gate void *SSL_get_ex_data(SSL *s,int idx)
2202*0Sstevel@tonic-gate 	{
2203*0Sstevel@tonic-gate 	return(CRYPTO_get_ex_data(&s->ex_data,idx));
2204*0Sstevel@tonic-gate 	}
2205*0Sstevel@tonic-gate 
2206*0Sstevel@tonic-gate int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2207*0Sstevel@tonic-gate 			     CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2208*0Sstevel@tonic-gate 	{
2209*0Sstevel@tonic-gate 	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
2210*0Sstevel@tonic-gate 				new_func, dup_func, free_func);
2211*0Sstevel@tonic-gate 	}
2212*0Sstevel@tonic-gate 
2213*0Sstevel@tonic-gate int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
2214*0Sstevel@tonic-gate 	{
2215*0Sstevel@tonic-gate 	return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2216*0Sstevel@tonic-gate 	}
2217*0Sstevel@tonic-gate 
2218*0Sstevel@tonic-gate void *SSL_CTX_get_ex_data(SSL_CTX *s,int idx)
2219*0Sstevel@tonic-gate 	{
2220*0Sstevel@tonic-gate 	return(CRYPTO_get_ex_data(&s->ex_data,idx));
2221*0Sstevel@tonic-gate 	}
2222*0Sstevel@tonic-gate 
2223*0Sstevel@tonic-gate int ssl_ok(SSL *s)
2224*0Sstevel@tonic-gate 	{
2225*0Sstevel@tonic-gate 	return(1);
2226*0Sstevel@tonic-gate 	}
2227*0Sstevel@tonic-gate 
2228*0Sstevel@tonic-gate X509_STORE *SSL_CTX_get_cert_store(SSL_CTX *ctx)
2229*0Sstevel@tonic-gate 	{
2230*0Sstevel@tonic-gate 	return(ctx->cert_store);
2231*0Sstevel@tonic-gate 	}
2232*0Sstevel@tonic-gate 
2233*0Sstevel@tonic-gate void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
2234*0Sstevel@tonic-gate 	{
2235*0Sstevel@tonic-gate 	if (ctx->cert_store != NULL)
2236*0Sstevel@tonic-gate 		X509_STORE_free(ctx->cert_store);
2237*0Sstevel@tonic-gate 	ctx->cert_store=store;
2238*0Sstevel@tonic-gate 	}
2239*0Sstevel@tonic-gate 
2240*0Sstevel@tonic-gate int SSL_want(SSL *s)
2241*0Sstevel@tonic-gate 	{
2242*0Sstevel@tonic-gate 	return(s->rwstate);
2243*0Sstevel@tonic-gate 	}
2244*0Sstevel@tonic-gate 
2245*0Sstevel@tonic-gate /*!
2246*0Sstevel@tonic-gate  * \brief Set the callback for generating temporary RSA keys.
2247*0Sstevel@tonic-gate  * \param ctx the SSL context.
2248*0Sstevel@tonic-gate  * \param cb the callback
2249*0Sstevel@tonic-gate  */
2250*0Sstevel@tonic-gate 
2251*0Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
2252*0Sstevel@tonic-gate void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
2253*0Sstevel@tonic-gate 							  int is_export,
2254*0Sstevel@tonic-gate 							  int keylength))
2255*0Sstevel@tonic-gate     {
2256*0Sstevel@tonic-gate     SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)())cb);
2257*0Sstevel@tonic-gate     }
2258*0Sstevel@tonic-gate 
2259*0Sstevel@tonic-gate void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
2260*0Sstevel@tonic-gate 						  int is_export,
2261*0Sstevel@tonic-gate 						  int keylength))
2262*0Sstevel@tonic-gate     {
2263*0Sstevel@tonic-gate     SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)())cb);
2264*0Sstevel@tonic-gate     }
2265*0Sstevel@tonic-gate #endif
2266*0Sstevel@tonic-gate 
2267*0Sstevel@tonic-gate #ifdef DOXYGEN
2268*0Sstevel@tonic-gate /*!
2269*0Sstevel@tonic-gate  * \brief The RSA temporary key callback function.
2270*0Sstevel@tonic-gate  * \param ssl the SSL session.
2271*0Sstevel@tonic-gate  * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
2272*0Sstevel@tonic-gate  * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
2273*0Sstevel@tonic-gate  * of the required key in bits.
2274*0Sstevel@tonic-gate  * \return the temporary RSA key.
2275*0Sstevel@tonic-gate  * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
2276*0Sstevel@tonic-gate  */
2277*0Sstevel@tonic-gate 
2278*0Sstevel@tonic-gate RSA *cb(SSL *ssl,int is_export,int keylength)
2279*0Sstevel@tonic-gate     {}
2280*0Sstevel@tonic-gate #endif
2281*0Sstevel@tonic-gate 
2282*0Sstevel@tonic-gate /*!
2283*0Sstevel@tonic-gate  * \brief Set the callback for generating temporary DH keys.
2284*0Sstevel@tonic-gate  * \param ctx the SSL context.
2285*0Sstevel@tonic-gate  * \param dh the callback
2286*0Sstevel@tonic-gate  */
2287*0Sstevel@tonic-gate 
2288*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
2289*0Sstevel@tonic-gate void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
2290*0Sstevel@tonic-gate 							int keylength))
2291*0Sstevel@tonic-gate 	{
2292*0Sstevel@tonic-gate 	SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)())dh);
2293*0Sstevel@tonic-gate 	}
2294*0Sstevel@tonic-gate 
2295*0Sstevel@tonic-gate void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
2296*0Sstevel@tonic-gate 						int keylength))
2297*0Sstevel@tonic-gate 	{
2298*0Sstevel@tonic-gate 	SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)())dh);
2299*0Sstevel@tonic-gate 	}
2300*0Sstevel@tonic-gate #endif
2301*0Sstevel@tonic-gate 
2302*0Sstevel@tonic-gate 
2303*0Sstevel@tonic-gate void SSL_CTX_set_msg_callback(SSL_CTX *ctx, void (*cb)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))
2304*0Sstevel@tonic-gate 	{
2305*0Sstevel@tonic-gate 	SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)())cb);
2306*0Sstevel@tonic-gate 	}
2307*0Sstevel@tonic-gate void SSL_set_msg_callback(SSL *ssl, void (*cb)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))
2308*0Sstevel@tonic-gate 	{
2309*0Sstevel@tonic-gate 	SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)())cb);
2310*0Sstevel@tonic-gate 	}
2311*0Sstevel@tonic-gate 
2312*0Sstevel@tonic-gate 
2313*0Sstevel@tonic-gate 
2314*0Sstevel@tonic-gate #if defined(_WINDLL) && defined(OPENSSL_SYS_WIN16)
2315*0Sstevel@tonic-gate #include "../crypto/bio/bss_file.c"
2316*0Sstevel@tonic-gate #endif
2317*0Sstevel@tonic-gate 
2318*0Sstevel@tonic-gate IMPLEMENT_STACK_OF(SSL_CIPHER)
2319*0Sstevel@tonic-gate IMPLEMENT_STACK_OF(SSL_COMP)
2320