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