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