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