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