1/* $OpenBSD: ssl_lib.c,v 1.323 2024/04/15 16:00:05 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 <limits.h>
148#include <stdio.h>
149
150#include <openssl/dh.h>
151#include <openssl/lhash.h>
152#include <openssl/objects.h>
153#include <openssl/ocsp.h>
154#include <openssl/opensslconf.h>
155#include <openssl/x509v3.h>
156
157#include "bytestring.h"
158#include "dtls_local.h"
159#include "ssl_local.h"
160#include "ssl_sigalgs.h"
161#include "ssl_tlsext.h"
162#include "tls12_internal.h"
163
164int
165SSL_clear(SSL *s)
166{
167	if (s->method == NULL) {
168		SSLerror(s, SSL_R_NO_METHOD_SPECIFIED);
169		return (0);
170	}
171
172	if (ssl_clear_bad_session(s)) {
173		SSL_SESSION_free(s->session);
174		s->session = NULL;
175	}
176
177	s->error = 0;
178	s->hit = 0;
179	s->shutdown = 0;
180
181	if (s->renegotiate) {
182		SSLerror(s, ERR_R_INTERNAL_ERROR);
183		return (0);
184	}
185
186	s->version = s->method->version;
187	s->client_version = s->version;
188	s->rwstate = SSL_NOTHING;
189	s->rstate = SSL_ST_READ_HEADER;
190
191	tls13_ctx_free(s->tls13);
192	s->tls13 = NULL;
193
194	ssl3_release_init_buffer(s);
195
196	ssl_clear_cipher_state(s);
197
198	s->first_packet = 0;
199
200	/*
201	 * Check to see if we were changed into a different method, if
202	 * so, revert back if we are not doing session-id reuse.
203	 */
204	if (!s->in_handshake && (s->session == NULL) &&
205	    (s->method != s->ctx->method)) {
206		s->method->ssl_free(s);
207		s->method = s->ctx->method;
208		if (!s->method->ssl_new(s))
209			return (0);
210	} else
211		s->method->ssl_clear(s);
212
213	return (1);
214}
215LSSL_ALIAS(SSL_clear);
216
217/* Used to change an SSL_CTXs default SSL method type */
218int
219SSL_CTX_set_ssl_version(SSL_CTX *ctx, const SSL_METHOD *meth)
220{
221	STACK_OF(SSL_CIPHER) *ciphers;
222
223	ctx->method = meth;
224
225	ciphers = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
226	    ctx->cipher_list_tls13, SSL_DEFAULT_CIPHER_LIST,
227	    ctx->cert);
228	if (ciphers == NULL || sk_SSL_CIPHER_num(ciphers) <= 0) {
229		SSLerrorx(SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
230		return (0);
231	}
232	return (1);
233}
234LSSL_ALIAS(SSL_CTX_set_ssl_version);
235
236SSL *
237SSL_new(SSL_CTX *ctx)
238{
239	SSL *s;
240	CBS cbs;
241
242	if (ctx == NULL) {
243		SSLerrorx(SSL_R_NULL_SSL_CTX);
244		return (NULL);
245	}
246	if (ctx->method == NULL) {
247		SSLerrorx(SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
248		return (NULL);
249	}
250
251	if ((s = calloc(1, sizeof(*s))) == NULL)
252		goto err;
253
254	if ((s->rl = tls12_record_layer_new()) == NULL)
255		goto err;
256
257	s->min_tls_version = ctx->min_tls_version;
258	s->max_tls_version = ctx->max_tls_version;
259	s->min_proto_version = ctx->min_proto_version;
260	s->max_proto_version = ctx->max_proto_version;
261
262	s->options = ctx->options;
263	s->mode = ctx->mode;
264	s->max_cert_list = ctx->max_cert_list;
265	s->num_tickets = ctx->num_tickets;
266
267	if ((s->cert = ssl_cert_dup(ctx->cert)) == NULL)
268		goto err;
269
270	s->read_ahead = ctx->read_ahead;
271	s->msg_callback = ctx->msg_callback;
272	s->msg_callback_arg = ctx->msg_callback_arg;
273	s->verify_mode = ctx->verify_mode;
274	s->sid_ctx_length = ctx->sid_ctx_length;
275	OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx);
276	memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
277	s->verify_callback = ctx->default_verify_callback;
278	s->generate_session_id = ctx->generate_session_id;
279
280	s->param = X509_VERIFY_PARAM_new();
281	if (!s->param)
282		goto err;
283	X509_VERIFY_PARAM_inherit(s->param, ctx->param);
284	s->quiet_shutdown = ctx->quiet_shutdown;
285	s->max_send_fragment = ctx->max_send_fragment;
286
287	CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
288	s->ctx = ctx;
289	s->tlsext_debug_cb = NULL;
290	s->tlsext_debug_arg = NULL;
291	s->tlsext_ticket_expected = 0;
292	s->tlsext_status_type = -1;
293	s->tlsext_status_expected = 0;
294	s->tlsext_ocsp_ids = NULL;
295	s->tlsext_ocsp_exts = NULL;
296	s->tlsext_ocsp_resp = NULL;
297	s->tlsext_ocsp_resp_len = 0;
298	CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
299	s->initial_ctx = ctx;
300
301	if (!tlsext_randomize_build_order(s))
302		goto err;
303
304	if (ctx->tlsext_ecpointformatlist != NULL) {
305		s->tlsext_ecpointformatlist =
306		    calloc(ctx->tlsext_ecpointformatlist_length,
307			sizeof(ctx->tlsext_ecpointformatlist[0]));
308		if (s->tlsext_ecpointformatlist == NULL)
309			goto err;
310		memcpy(s->tlsext_ecpointformatlist,
311		    ctx->tlsext_ecpointformatlist,
312		    ctx->tlsext_ecpointformatlist_length *
313		    sizeof(ctx->tlsext_ecpointformatlist[0]));
314		s->tlsext_ecpointformatlist_length =
315		    ctx->tlsext_ecpointformatlist_length;
316	}
317	if (ctx->tlsext_supportedgroups != NULL) {
318		s->tlsext_supportedgroups =
319		    calloc(ctx->tlsext_supportedgroups_length,
320			sizeof(ctx->tlsext_supportedgroups[0]));
321		if (s->tlsext_supportedgroups == NULL)
322			goto err;
323		memcpy(s->tlsext_supportedgroups,
324		    ctx->tlsext_supportedgroups,
325		    ctx->tlsext_supportedgroups_length *
326		    sizeof(ctx->tlsext_supportedgroups[0]));
327		s->tlsext_supportedgroups_length =
328		    ctx->tlsext_supportedgroups_length;
329	}
330
331	CBS_init(&cbs, ctx->alpn_client_proto_list,
332	    ctx->alpn_client_proto_list_len);
333	if (!CBS_stow(&cbs, &s->alpn_client_proto_list,
334	    &s->alpn_client_proto_list_len))
335		goto err;
336
337	s->verify_result = X509_V_OK;
338
339	s->method = ctx->method;
340	s->quic_method = ctx->quic_method;
341
342	if (!s->method->ssl_new(s))
343		goto err;
344
345	s->references = 1;
346	s->server = ctx->method->server;
347
348	SSL_clear(s);
349
350	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
351
352	return (s);
353
354 err:
355	SSL_free(s);
356	SSLerrorx(ERR_R_MALLOC_FAILURE);
357	return (NULL);
358}
359LSSL_ALIAS(SSL_new);
360
361int
362SSL_CTX_set_session_id_context(SSL_CTX *ctx, const unsigned char *sid_ctx,
363    unsigned int sid_ctx_len)
364{
365	if (sid_ctx_len > sizeof ctx->sid_ctx) {
366		SSLerrorx(SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
367		return (0);
368	}
369	ctx->sid_ctx_length = sid_ctx_len;
370	memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
371
372	return (1);
373}
374LSSL_ALIAS(SSL_CTX_set_session_id_context);
375
376int
377SSL_set_session_id_context(SSL *ssl, const unsigned char *sid_ctx,
378    unsigned int sid_ctx_len)
379{
380	if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
381		SSLerror(ssl, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
382		return (0);
383	}
384	ssl->sid_ctx_length = sid_ctx_len;
385	memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
386
387	return (1);
388}
389LSSL_ALIAS(SSL_set_session_id_context);
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->generate_session_id = cb;
396	CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
397	return (1);
398}
399LSSL_ALIAS(SSL_CTX_set_generate_session_id);
400
401int
402SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
403{
404	CRYPTO_w_lock(CRYPTO_LOCK_SSL);
405	ssl->generate_session_id = cb;
406	CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
407	return (1);
408}
409LSSL_ALIAS(SSL_set_generate_session_id);
410
411int
412SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
413    unsigned int id_len)
414{
415	/*
416	 * A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp
417	 * shows how we can "construct" a session to give us the desired
418	 * check - ie. to find if there's a session in the hash table
419	 * that would conflict with any new session built out of this
420	 * id/id_len and the ssl_version in use by this SSL.
421	 */
422	SSL_SESSION r, *p;
423
424	if (id_len > sizeof r.session_id)
425		return (0);
426
427	r.ssl_version = ssl->version;
428	r.session_id_length = id_len;
429	memcpy(r.session_id, id, id_len);
430
431	CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
432	p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
433	CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
434	return (p != NULL);
435}
436LSSL_ALIAS(SSL_has_matching_session_id);
437
438int
439SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
440{
441	return (X509_VERIFY_PARAM_set_purpose(s->param, purpose));
442}
443LSSL_ALIAS(SSL_CTX_set_purpose);
444
445int
446SSL_set_purpose(SSL *s, int purpose)
447{
448	return (X509_VERIFY_PARAM_set_purpose(s->param, purpose));
449}
450LSSL_ALIAS(SSL_set_purpose);
451
452int
453SSL_CTX_set_trust(SSL_CTX *s, int trust)
454{
455	return (X509_VERIFY_PARAM_set_trust(s->param, trust));
456}
457LSSL_ALIAS(SSL_CTX_set_trust);
458
459int
460SSL_set_trust(SSL *s, int trust)
461{
462	return (X509_VERIFY_PARAM_set_trust(s->param, trust));
463}
464LSSL_ALIAS(SSL_set_trust);
465
466int
467SSL_set1_host(SSL *s, const char *hostname)
468{
469	struct in_addr ina;
470	struct in6_addr in6a;
471
472	if (hostname != NULL && *hostname != '\0' &&
473	    (inet_pton(AF_INET, hostname, &ina) == 1 ||
474	    inet_pton(AF_INET6, hostname, &in6a) == 1))
475		return X509_VERIFY_PARAM_set1_ip_asc(s->param, hostname);
476	else
477		return X509_VERIFY_PARAM_set1_host(s->param, hostname, 0);
478}
479LSSL_ALIAS(SSL_set1_host);
480
481void
482SSL_set_hostflags(SSL *s, unsigned int flags)
483{
484	X509_VERIFY_PARAM_set_hostflags(s->param, flags);
485}
486LSSL_ALIAS(SSL_set_hostflags);
487
488const char *
489SSL_get0_peername(SSL *s)
490{
491	return X509_VERIFY_PARAM_get0_peername(s->param);
492}
493LSSL_ALIAS(SSL_get0_peername);
494
495X509_VERIFY_PARAM *
496SSL_CTX_get0_param(SSL_CTX *ctx)
497{
498	return (ctx->param);
499}
500LSSL_ALIAS(SSL_CTX_get0_param);
501
502int
503SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
504{
505	return (X509_VERIFY_PARAM_set1(ctx->param, vpm));
506}
507LSSL_ALIAS(SSL_CTX_set1_param);
508
509X509_VERIFY_PARAM *
510SSL_get0_param(SSL *ssl)
511{
512	return (ssl->param);
513}
514LSSL_ALIAS(SSL_get0_param);
515
516int
517SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
518{
519	return (X509_VERIFY_PARAM_set1(ssl->param, vpm));
520}
521LSSL_ALIAS(SSL_set1_param);
522
523void
524SSL_free(SSL *s)
525{
526	int	i;
527
528	if (s == NULL)
529		return;
530
531	i = CRYPTO_add(&s->references, -1, CRYPTO_LOCK_SSL);
532	if (i > 0)
533		return;
534
535	X509_VERIFY_PARAM_free(s->param);
536
537	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
538
539	if (s->bbio != NULL) {
540		/* If the buffering BIO is in place, pop it off */
541		if (s->bbio == s->wbio) {
542			s->wbio = BIO_pop(s->wbio);
543		}
544		BIO_free(s->bbio);
545		s->bbio = NULL;
546	}
547
548	if (s->rbio != s->wbio)
549		BIO_free_all(s->rbio);
550	BIO_free_all(s->wbio);
551
552	tls13_ctx_free(s->tls13);
553
554	ssl3_release_init_buffer(s);
555
556	sk_SSL_CIPHER_free(s->cipher_list);
557	sk_SSL_CIPHER_free(s->cipher_list_tls13);
558
559	/* Make the next call work :-) */
560	if (s->session != NULL) {
561		ssl_clear_bad_session(s);
562		SSL_SESSION_free(s->session);
563	}
564
565	ssl_clear_cipher_state(s);
566
567	ssl_cert_free(s->cert);
568
569	free(s->tlsext_build_order);
570
571	free(s->tlsext_hostname);
572	SSL_CTX_free(s->initial_ctx);
573
574	free(s->tlsext_ecpointformatlist);
575	free(s->tlsext_supportedgroups);
576
577	sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
578	    X509_EXTENSION_free);
579	sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free);
580	free(s->tlsext_ocsp_resp);
581
582	sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free);
583
584	if (s->method != NULL)
585		s->method->ssl_free(s);
586
587	SSL_CTX_free(s->ctx);
588
589	free(s->alpn_client_proto_list);
590
591	free(s->quic_transport_params);
592
593#ifndef OPENSSL_NO_SRTP
594	sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
595#endif
596
597	tls12_record_layer_free(s->rl);
598
599	free(s);
600}
601LSSL_ALIAS(SSL_free);
602
603int
604SSL_up_ref(SSL *s)
605{
606	return CRYPTO_add(&s->references, 1, CRYPTO_LOCK_SSL) > 1;
607}
608LSSL_ALIAS(SSL_up_ref);
609
610void
611SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio)
612{
613	/* If the output buffering BIO is still in place, remove it */
614	if (s->bbio != NULL) {
615		if (s->wbio == s->bbio) {
616			s->wbio = BIO_next(s->wbio);
617			BIO_set_next(s->bbio, NULL);
618		}
619	}
620
621	if (s->rbio != rbio && s->rbio != s->wbio)
622		BIO_free_all(s->rbio);
623	if (s->wbio != wbio)
624		BIO_free_all(s->wbio);
625	s->rbio = rbio;
626	s->wbio = wbio;
627}
628LSSL_ALIAS(SSL_set_bio);
629
630BIO *
631SSL_get_rbio(const SSL *s)
632{
633	return (s->rbio);
634}
635LSSL_ALIAS(SSL_get_rbio);
636
637void
638SSL_set0_rbio(SSL *s, BIO *rbio)
639{
640	BIO_free_all(s->rbio);
641	s->rbio = rbio;
642}
643LSSL_ALIAS(SSL_set0_rbio);
644
645BIO *
646SSL_get_wbio(const SSL *s)
647{
648	return (s->wbio);
649}
650LSSL_ALIAS(SSL_get_wbio);
651
652int
653SSL_get_fd(const SSL *s)
654{
655	return (SSL_get_rfd(s));
656}
657LSSL_ALIAS(SSL_get_fd);
658
659int
660SSL_get_rfd(const SSL *s)
661{
662	int	 ret = -1;
663	BIO	*b, *r;
664
665	b = SSL_get_rbio(s);
666	r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
667	if (r != NULL)
668		BIO_get_fd(r, &ret);
669	return (ret);
670}
671LSSL_ALIAS(SSL_get_rfd);
672
673int
674SSL_get_wfd(const SSL *s)
675{
676	int	 ret = -1;
677	BIO	*b, *r;
678
679	b = SSL_get_wbio(s);
680	r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
681	if (r != NULL)
682		BIO_get_fd(r, &ret);
683	return (ret);
684}
685LSSL_ALIAS(SSL_get_wfd);
686
687int
688SSL_set_fd(SSL *s, int fd)
689{
690	int	 ret = 0;
691	BIO	*bio = NULL;
692
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, bio);
701	ret = 1;
702 err:
703	return (ret);
704}
705LSSL_ALIAS(SSL_set_fd);
706
707int
708SSL_set_wfd(SSL *s, int fd)
709{
710	int	 ret = 0;
711	BIO	*bio = NULL;
712
713	if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET)
714	    || ((int)BIO_get_fd(s->rbio, NULL) != fd)) {
715		bio = BIO_new(BIO_s_socket());
716
717		if (bio == NULL) {
718			SSLerror(s, ERR_R_BUF_LIB);
719			goto err;
720		}
721		BIO_set_fd(bio, fd, BIO_NOCLOSE);
722		SSL_set_bio(s, SSL_get_rbio(s), bio);
723	} else
724		SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
725	ret = 1;
726 err:
727	return (ret);
728}
729LSSL_ALIAS(SSL_set_wfd);
730
731int
732SSL_set_rfd(SSL *s, int fd)
733{
734	int	 ret = 0;
735	BIO	*bio = NULL;
736
737	if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET)
738	    || ((int)BIO_get_fd(s->wbio, NULL) != fd)) {
739		bio = BIO_new(BIO_s_socket());
740
741		if (bio == NULL) {
742			SSLerror(s, ERR_R_BUF_LIB);
743			goto err;
744		}
745		BIO_set_fd(bio, fd, BIO_NOCLOSE);
746		SSL_set_bio(s, bio, SSL_get_wbio(s));
747	} else
748		SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
749	ret = 1;
750 err:
751	return (ret);
752}
753LSSL_ALIAS(SSL_set_rfd);
754
755
756/* return length of latest Finished message we sent, copy to 'buf' */
757size_t
758SSL_get_finished(const SSL *s, void *buf, size_t count)
759{
760	size_t	ret;
761
762	ret = s->s3->hs.finished_len;
763	if (count > ret)
764		count = ret;
765	memcpy(buf, s->s3->hs.finished, count);
766	return (ret);
767}
768LSSL_ALIAS(SSL_get_finished);
769
770/* return length of latest Finished message we expected, copy to 'buf' */
771size_t
772SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
773{
774	size_t	ret;
775
776	ret = s->s3->hs.peer_finished_len;
777	if (count > ret)
778		count = ret;
779	memcpy(buf, s->s3->hs.peer_finished, count);
780	return (ret);
781}
782LSSL_ALIAS(SSL_get_peer_finished);
783
784
785int
786SSL_get_verify_mode(const SSL *s)
787{
788	return (s->verify_mode);
789}
790LSSL_ALIAS(SSL_get_verify_mode);
791
792int
793SSL_get_verify_depth(const SSL *s)
794{
795	return (X509_VERIFY_PARAM_get_depth(s->param));
796}
797LSSL_ALIAS(SSL_get_verify_depth);
798
799int
800(*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *)
801{
802	return (s->verify_callback);
803}
804LSSL_ALIAS(SSL_get_verify_callback);
805
806void
807SSL_CTX_set_keylog_callback(SSL_CTX *ctx, SSL_CTX_keylog_cb_func cb)
808{
809	ctx->keylog_callback = cb;
810}
811LSSL_ALIAS(SSL_CTX_set_keylog_callback);
812
813SSL_CTX_keylog_cb_func
814SSL_CTX_get_keylog_callback(const SSL_CTX *ctx)
815{
816	return (ctx->keylog_callback);
817}
818LSSL_ALIAS(SSL_CTX_get_keylog_callback);
819
820int
821SSL_set_num_tickets(SSL *s, size_t num_tickets)
822{
823	s->num_tickets = num_tickets;
824
825	return 1;
826}
827LSSL_ALIAS(SSL_set_num_tickets);
828
829size_t
830SSL_get_num_tickets(const SSL *s)
831{
832	return s->num_tickets;
833}
834LSSL_ALIAS(SSL_get_num_tickets);
835
836int
837SSL_CTX_set_num_tickets(SSL_CTX *ctx, size_t num_tickets)
838{
839	ctx->num_tickets = num_tickets;
840
841	return 1;
842}
843LSSL_ALIAS(SSL_CTX_set_num_tickets);
844
845size_t
846SSL_CTX_get_num_tickets(const SSL_CTX *ctx)
847{
848	return ctx->num_tickets;
849}
850LSSL_ALIAS(SSL_CTX_get_num_tickets);
851
852int
853SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
854{
855	return (ctx->verify_mode);
856}
857LSSL_ALIAS(SSL_CTX_get_verify_mode);
858
859int
860SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
861{
862	return (X509_VERIFY_PARAM_get_depth(ctx->param));
863}
864LSSL_ALIAS(SSL_CTX_get_verify_depth);
865
866int
867(*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *)
868{
869	return (ctx->default_verify_callback);
870}
871LSSL_ALIAS(SSL_CTX_get_verify_callback);
872
873void
874SSL_set_verify(SSL *s, int mode,
875    int (*callback)(int ok, X509_STORE_CTX *ctx))
876{
877	s->verify_mode = mode;
878	if (callback != NULL)
879		s->verify_callback = callback;
880}
881LSSL_ALIAS(SSL_set_verify);
882
883void
884SSL_set_verify_depth(SSL *s, int depth)
885{
886	X509_VERIFY_PARAM_set_depth(s->param, depth);
887}
888LSSL_ALIAS(SSL_set_verify_depth);
889
890void
891SSL_set_read_ahead(SSL *s, int yes)
892{
893	s->read_ahead = yes;
894}
895LSSL_ALIAS(SSL_set_read_ahead);
896
897int
898SSL_get_read_ahead(const SSL *s)
899{
900	return (s->read_ahead);
901}
902LSSL_ALIAS(SSL_get_read_ahead);
903
904int
905SSL_pending(const SSL *s)
906{
907	return (s->method->ssl_pending(s));
908}
909LSSL_ALIAS(SSL_pending);
910
911X509 *
912SSL_get_peer_certificate(const SSL *s)
913{
914	X509 *cert;
915
916	if (s == NULL || s->session == NULL)
917		return NULL;
918
919	if ((cert = s->session->peer_cert) == NULL)
920		return NULL;
921
922	X509_up_ref(cert);
923
924	return cert;
925}
926LSSL_ALIAS(SSL_get_peer_certificate);
927
928STACK_OF(X509) *
929SSL_get_peer_cert_chain(const SSL *s)
930{
931	if (s == NULL)
932		return NULL;
933
934	/*
935	 * Achtung! Due to API inconsistency, a client includes the peer's leaf
936	 * certificate in the peer certificate chain, while a server does not.
937	 */
938	if (!s->server)
939		return s->s3->hs.peer_certs;
940
941	return s->s3->hs.peer_certs_no_leaf;
942}
943LSSL_ALIAS(SSL_get_peer_cert_chain);
944
945STACK_OF(X509) *
946SSL_get0_verified_chain(const SSL *s)
947{
948	if (s->s3 == NULL)
949		return NULL;
950	return s->s3->hs.verified_chain;
951}
952LSSL_ALIAS(SSL_get0_verified_chain);
953
954/*
955 * Now in theory, since the calling process own 't' it should be safe to
956 * modify.  We need to be able to read f without being hassled
957 */
958int
959SSL_copy_session_id(SSL *t, const SSL *f)
960{
961	SSL_CERT *tmp;
962
963	/* Do we need to do SSL locking? */
964	if (!SSL_set_session(t, SSL_get_session(f)))
965		return 0;
966
967	/* What if we are set up for one protocol but want to talk another? */
968	if (t->method != f->method) {
969		t->method->ssl_free(t);
970		t->method = f->method;
971		if (!t->method->ssl_new(t))
972			return 0;
973	}
974
975	tmp = t->cert;
976	if (f->cert != NULL) {
977		CRYPTO_add(&f->cert->references, 1, CRYPTO_LOCK_SSL_CERT);
978		t->cert = f->cert;
979	} else
980		t->cert = NULL;
981	ssl_cert_free(tmp);
982
983	if (!SSL_set_session_id_context(t, f->sid_ctx, f->sid_ctx_length))
984		return 0;
985
986	return 1;
987}
988LSSL_ALIAS(SSL_copy_session_id);
989
990/* Fix this so it checks all the valid key/cert options */
991int
992SSL_CTX_check_private_key(const SSL_CTX *ctx)
993{
994	if ((ctx == NULL) || (ctx->cert == NULL) ||
995	    (ctx->cert->key->x509 == NULL)) {
996		SSLerrorx(SSL_R_NO_CERTIFICATE_ASSIGNED);
997		return (0);
998	}
999	if (ctx->cert->key->privatekey == NULL) {
1000		SSLerrorx(SSL_R_NO_PRIVATE_KEY_ASSIGNED);
1001		return (0);
1002	}
1003	return (X509_check_private_key(ctx->cert->key->x509,
1004	    ctx->cert->key->privatekey));
1005}
1006LSSL_ALIAS(SSL_CTX_check_private_key);
1007
1008/* Fix this function so that it takes an optional type parameter */
1009int
1010SSL_check_private_key(const SSL *ssl)
1011{
1012	if (ssl == NULL) {
1013		SSLerrorx(ERR_R_PASSED_NULL_PARAMETER);
1014		return (0);
1015	}
1016	if (ssl->cert == NULL) {
1017		SSLerror(ssl, SSL_R_NO_CERTIFICATE_ASSIGNED);
1018		return (0);
1019	}
1020	if (ssl->cert->key->x509 == NULL) {
1021		SSLerror(ssl, SSL_R_NO_CERTIFICATE_ASSIGNED);
1022		return (0);
1023	}
1024	if (ssl->cert->key->privatekey == NULL) {
1025		SSLerror(ssl, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
1026		return (0);
1027	}
1028	return (X509_check_private_key(ssl->cert->key->x509,
1029	    ssl->cert->key->privatekey));
1030}
1031LSSL_ALIAS(SSL_check_private_key);
1032
1033int
1034SSL_accept(SSL *s)
1035{
1036	if (s->handshake_func == NULL)
1037		SSL_set_accept_state(s); /* Not properly initialized yet */
1038
1039	return (s->method->ssl_accept(s));
1040}
1041LSSL_ALIAS(SSL_accept);
1042
1043int
1044SSL_connect(SSL *s)
1045{
1046	if (s->handshake_func == NULL)
1047		SSL_set_connect_state(s); /* Not properly initialized yet */
1048
1049	return (s->method->ssl_connect(s));
1050}
1051LSSL_ALIAS(SSL_connect);
1052
1053int
1054SSL_is_dtls(const SSL *s)
1055{
1056	return s->method->dtls;
1057}
1058LSSL_ALIAS(SSL_is_dtls);
1059
1060int
1061SSL_is_server(const SSL *s)
1062{
1063	return s->server;
1064}
1065LSSL_ALIAS(SSL_is_server);
1066
1067static long
1068ssl_get_default_timeout(void)
1069{
1070	/*
1071	 * 2 hours, the 24 hours mentioned in the TLSv1 spec
1072	 * is way too long for http, the cache would over fill.
1073	 */
1074	return (2 * 60 * 60);
1075}
1076
1077long
1078SSL_get_default_timeout(const SSL *s)
1079{
1080	return (ssl_get_default_timeout());
1081}
1082LSSL_ALIAS(SSL_get_default_timeout);
1083
1084int
1085SSL_read(SSL *s, void *buf, int num)
1086{
1087	if (num < 0) {
1088		SSLerror(s, SSL_R_BAD_LENGTH);
1089		return -1;
1090	}
1091
1092	if (SSL_is_quic(s)) {
1093		SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1094		return (-1);
1095	}
1096
1097	if (s->handshake_func == NULL) {
1098		SSLerror(s, SSL_R_UNINITIALIZED);
1099		return (-1);
1100	}
1101
1102	if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
1103		s->rwstate = SSL_NOTHING;
1104		return (0);
1105	}
1106	return ssl3_read(s, buf, num);
1107}
1108LSSL_ALIAS(SSL_read);
1109
1110int
1111SSL_read_ex(SSL *s, void *buf, size_t num, size_t *bytes_read)
1112{
1113	int ret;
1114
1115	/* We simply don't bother supporting enormous reads */
1116	if (num > INT_MAX) {
1117		SSLerror(s, SSL_R_BAD_LENGTH);
1118		return 0;
1119	}
1120
1121	ret = SSL_read(s, buf, (int)num);
1122	if (ret < 0)
1123		ret = 0;
1124	*bytes_read = ret;
1125
1126	return ret > 0;
1127}
1128LSSL_ALIAS(SSL_read_ex);
1129
1130int
1131SSL_peek(SSL *s, void *buf, int num)
1132{
1133	if (num < 0) {
1134		SSLerror(s, SSL_R_BAD_LENGTH);
1135		return -1;
1136	}
1137
1138	if (SSL_is_quic(s)) {
1139		SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1140		return (-1);
1141	}
1142
1143	if (s->handshake_func == NULL) {
1144		SSLerror(s, SSL_R_UNINITIALIZED);
1145		return (-1);
1146	}
1147
1148	if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
1149		return (0);
1150	}
1151	return ssl3_peek(s, buf, num);
1152}
1153LSSL_ALIAS(SSL_peek);
1154
1155int
1156SSL_peek_ex(SSL *s, void *buf, size_t num, size_t *bytes_peeked)
1157{
1158	int ret;
1159
1160	/* We simply don't bother supporting enormous peeks */
1161	if (num > INT_MAX) {
1162		SSLerror(s, SSL_R_BAD_LENGTH);
1163		return 0;
1164	}
1165
1166	ret = SSL_peek(s, buf, (int)num);
1167	if (ret < 0)
1168		ret = 0;
1169	*bytes_peeked = ret;
1170
1171	return ret > 0;
1172}
1173LSSL_ALIAS(SSL_peek_ex);
1174
1175int
1176SSL_write(SSL *s, const void *buf, int num)
1177{
1178	if (num < 0) {
1179		SSLerror(s, SSL_R_BAD_LENGTH);
1180		return -1;
1181	}
1182
1183	if (SSL_is_quic(s)) {
1184		SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1185		return (-1);
1186	}
1187
1188	if (s->handshake_func == NULL) {
1189		SSLerror(s, SSL_R_UNINITIALIZED);
1190		return (-1);
1191	}
1192
1193	if (s->shutdown & SSL_SENT_SHUTDOWN) {
1194		s->rwstate = SSL_NOTHING;
1195		SSLerror(s, SSL_R_PROTOCOL_IS_SHUTDOWN);
1196		return (-1);
1197	}
1198	return ssl3_write(s, buf, num);
1199}
1200LSSL_ALIAS(SSL_write);
1201
1202int
1203SSL_write_ex(SSL *s, const void *buf, size_t num, size_t *bytes_written)
1204{
1205	int ret;
1206
1207	/* We simply don't bother supporting enormous writes */
1208	if (num > INT_MAX) {
1209		SSLerror(s, SSL_R_BAD_LENGTH);
1210		return 0;
1211	}
1212
1213	if (num == 0) {
1214		/* This API is special */
1215		bytes_written = 0;
1216		return 1;
1217	}
1218
1219	ret = SSL_write(s, buf, (int)num);
1220	if (ret < 0)
1221		ret = 0;
1222	*bytes_written = ret;
1223
1224	return ret > 0;
1225}
1226LSSL_ALIAS(SSL_write_ex);
1227
1228uint32_t
1229SSL_CTX_get_max_early_data(const SSL_CTX *ctx)
1230{
1231	return 0;
1232}
1233LSSL_ALIAS(SSL_CTX_get_max_early_data);
1234
1235int
1236SSL_CTX_set_max_early_data(SSL_CTX *ctx, uint32_t max_early_data)
1237{
1238	return 1;
1239}
1240LSSL_ALIAS(SSL_CTX_set_max_early_data);
1241
1242uint32_t
1243SSL_get_max_early_data(const SSL *s)
1244{
1245	return 0;
1246}
1247LSSL_ALIAS(SSL_get_max_early_data);
1248
1249int
1250SSL_set_max_early_data(SSL *s, uint32_t max_early_data)
1251{
1252	return 1;
1253}
1254LSSL_ALIAS(SSL_set_max_early_data);
1255
1256int
1257SSL_get_early_data_status(const SSL *s)
1258{
1259	return SSL_EARLY_DATA_REJECTED;
1260}
1261LSSL_ALIAS(SSL_get_early_data_status);
1262
1263int
1264SSL_read_early_data(SSL *s, void *buf, size_t num, size_t *readbytes)
1265{
1266	*readbytes = 0;
1267
1268	if (!s->server) {
1269		SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1270		return SSL_READ_EARLY_DATA_ERROR;
1271	}
1272
1273	return SSL_READ_EARLY_DATA_FINISH;
1274}
1275LSSL_ALIAS(SSL_read_early_data);
1276
1277int
1278SSL_write_early_data(SSL *s, const void *buf, size_t num, size_t *written)
1279{
1280	*written = 0;
1281	SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1282	return 0;
1283}
1284LSSL_ALIAS(SSL_write_early_data);
1285
1286int
1287SSL_shutdown(SSL *s)
1288{
1289	/*
1290	 * Note that this function behaves differently from what one might
1291	 * expect.  Return values are 0 for no success (yet),
1292	 * 1 for success; but calling it once is usually not enough,
1293	 * even if blocking I/O is used (see ssl3_shutdown).
1294	 */
1295
1296	if (s->handshake_func == NULL) {
1297		SSLerror(s, SSL_R_UNINITIALIZED);
1298		return (-1);
1299	}
1300
1301	if (s != NULL && !SSL_in_init(s))
1302		return (s->method->ssl_shutdown(s));
1303
1304	return (1);
1305}
1306LSSL_ALIAS(SSL_shutdown);
1307
1308int
1309SSL_renegotiate(SSL *s)
1310{
1311	if (s->renegotiate == 0)
1312		s->renegotiate = 1;
1313
1314	s->new_session = 1;
1315
1316	return (s->method->ssl_renegotiate(s));
1317}
1318LSSL_ALIAS(SSL_renegotiate);
1319
1320int
1321SSL_renegotiate_abbreviated(SSL *s)
1322{
1323	if (s->renegotiate == 0)
1324		s->renegotiate = 1;
1325
1326	s->new_session = 0;
1327
1328	return (s->method->ssl_renegotiate(s));
1329}
1330LSSL_ALIAS(SSL_renegotiate_abbreviated);
1331
1332int
1333SSL_renegotiate_pending(SSL *s)
1334{
1335	/*
1336	 * Becomes true when negotiation is requested;
1337	 * false again once a handshake has finished.
1338	 */
1339	return (s->renegotiate != 0);
1340}
1341LSSL_ALIAS(SSL_renegotiate_pending);
1342
1343long
1344SSL_ctrl(SSL *s, int cmd, long larg, void *parg)
1345{
1346	long	l;
1347
1348	switch (cmd) {
1349	case SSL_CTRL_GET_READ_AHEAD:
1350		return (s->read_ahead);
1351	case SSL_CTRL_SET_READ_AHEAD:
1352		l = s->read_ahead;
1353		s->read_ahead = larg;
1354		return (l);
1355
1356	case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1357		s->msg_callback_arg = parg;
1358		return (1);
1359
1360	case SSL_CTRL_OPTIONS:
1361		return (s->options|=larg);
1362	case SSL_CTRL_CLEAR_OPTIONS:
1363		return (s->options&=~larg);
1364	case SSL_CTRL_MODE:
1365		return (s->mode|=larg);
1366	case SSL_CTRL_CLEAR_MODE:
1367		return (s->mode &=~larg);
1368	case SSL_CTRL_GET_MAX_CERT_LIST:
1369		return (s->max_cert_list);
1370	case SSL_CTRL_SET_MAX_CERT_LIST:
1371		l = s->max_cert_list;
1372		s->max_cert_list = larg;
1373		return (l);
1374	case SSL_CTRL_SET_MTU:
1375#ifndef OPENSSL_NO_DTLS1
1376		if (larg < (long)dtls1_min_mtu())
1377			return (0);
1378#endif
1379		if (SSL_is_dtls(s)) {
1380			s->d1->mtu = larg;
1381			return (larg);
1382		}
1383		return (0);
1384	case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1385		if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1386			return (0);
1387		s->max_send_fragment = larg;
1388		return (1);
1389	case SSL_CTRL_GET_RI_SUPPORT:
1390		if (s->s3)
1391			return (s->s3->send_connection_binding);
1392		else return (0);
1393	default:
1394		if (SSL_is_dtls(s))
1395			return dtls1_ctrl(s, cmd, larg, parg);
1396		return ssl3_ctrl(s, cmd, larg, parg);
1397	}
1398}
1399LSSL_ALIAS(SSL_ctrl);
1400
1401long
1402SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
1403{
1404	switch (cmd) {
1405	case SSL_CTRL_SET_MSG_CALLBACK:
1406		s->msg_callback = (ssl_msg_callback_fn *)(fp);
1407		return (1);
1408
1409	default:
1410		return (ssl3_callback_ctrl(s, cmd, fp));
1411	}
1412}
1413LSSL_ALIAS(SSL_callback_ctrl);
1414
1415struct lhash_st_SSL_SESSION *
1416SSL_CTX_sessions(SSL_CTX *ctx)
1417{
1418	return (ctx->sessions);
1419}
1420LSSL_ALIAS(SSL_CTX_sessions);
1421
1422long
1423SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
1424{
1425	long	l;
1426
1427	switch (cmd) {
1428	case SSL_CTRL_GET_READ_AHEAD:
1429		return (ctx->read_ahead);
1430	case SSL_CTRL_SET_READ_AHEAD:
1431		l = ctx->read_ahead;
1432		ctx->read_ahead = larg;
1433		return (l);
1434
1435	case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1436		ctx->msg_callback_arg = parg;
1437		return (1);
1438
1439	case SSL_CTRL_GET_MAX_CERT_LIST:
1440		return (ctx->max_cert_list);
1441	case SSL_CTRL_SET_MAX_CERT_LIST:
1442		l = ctx->max_cert_list;
1443		ctx->max_cert_list = larg;
1444		return (l);
1445
1446	case SSL_CTRL_SET_SESS_CACHE_SIZE:
1447		l = ctx->session_cache_size;
1448		ctx->session_cache_size = larg;
1449		return (l);
1450	case SSL_CTRL_GET_SESS_CACHE_SIZE:
1451		return (ctx->session_cache_size);
1452	case SSL_CTRL_SET_SESS_CACHE_MODE:
1453		l = ctx->session_cache_mode;
1454		ctx->session_cache_mode = larg;
1455		return (l);
1456	case SSL_CTRL_GET_SESS_CACHE_MODE:
1457		return (ctx->session_cache_mode);
1458
1459	case SSL_CTRL_SESS_NUMBER:
1460		return (lh_SSL_SESSION_num_items(ctx->sessions));
1461	case SSL_CTRL_SESS_CONNECT:
1462		return (ctx->stats.sess_connect);
1463	case SSL_CTRL_SESS_CONNECT_GOOD:
1464		return (ctx->stats.sess_connect_good);
1465	case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1466		return (ctx->stats.sess_connect_renegotiate);
1467	case SSL_CTRL_SESS_ACCEPT:
1468		return (ctx->stats.sess_accept);
1469	case SSL_CTRL_SESS_ACCEPT_GOOD:
1470		return (ctx->stats.sess_accept_good);
1471	case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1472		return (ctx->stats.sess_accept_renegotiate);
1473	case SSL_CTRL_SESS_HIT:
1474		return (ctx->stats.sess_hit);
1475	case SSL_CTRL_SESS_CB_HIT:
1476		return (ctx->stats.sess_cb_hit);
1477	case SSL_CTRL_SESS_MISSES:
1478		return (ctx->stats.sess_miss);
1479	case SSL_CTRL_SESS_TIMEOUTS:
1480		return (ctx->stats.sess_timeout);
1481	case SSL_CTRL_SESS_CACHE_FULL:
1482		return (ctx->stats.sess_cache_full);
1483	case SSL_CTRL_OPTIONS:
1484		return (ctx->options|=larg);
1485	case SSL_CTRL_CLEAR_OPTIONS:
1486		return (ctx->options&=~larg);
1487	case SSL_CTRL_MODE:
1488		return (ctx->mode|=larg);
1489	case SSL_CTRL_CLEAR_MODE:
1490		return (ctx->mode&=~larg);
1491	case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1492		if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1493			return (0);
1494		ctx->max_send_fragment = larg;
1495		return (1);
1496	default:
1497		return (ssl3_ctx_ctrl(ctx, cmd, larg, parg));
1498	}
1499}
1500LSSL_ALIAS(SSL_CTX_ctrl);
1501
1502long
1503SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
1504{
1505	switch (cmd) {
1506	case SSL_CTRL_SET_MSG_CALLBACK:
1507		ctx->msg_callback = (ssl_msg_callback_fn *)fp;
1508		return (1);
1509
1510	default:
1511		return (ssl3_ctx_callback_ctrl(ctx, cmd, fp));
1512	}
1513}
1514LSSL_ALIAS(SSL_CTX_callback_ctrl);
1515
1516STACK_OF(SSL_CIPHER) *
1517SSL_get_ciphers(const SSL *s)
1518{
1519	if (s == NULL)
1520		return (NULL);
1521	if (s->cipher_list != NULL)
1522		return (s->cipher_list);
1523
1524	return (s->ctx->cipher_list);
1525}
1526LSSL_ALIAS(SSL_get_ciphers);
1527
1528STACK_OF(SSL_CIPHER) *
1529SSL_get_client_ciphers(const SSL *s)
1530{
1531	if (s == NULL || s->session == NULL || !s->server)
1532		return NULL;
1533	return s->session->ciphers;
1534}
1535LSSL_ALIAS(SSL_get_client_ciphers);
1536
1537STACK_OF(SSL_CIPHER) *
1538SSL_get1_supported_ciphers(SSL *s)
1539{
1540	STACK_OF(SSL_CIPHER) *supported_ciphers = NULL, *ciphers;
1541	SSL_CIPHER *cipher;
1542	uint16_t min_vers, max_vers;
1543	int i;
1544
1545	if (s == NULL)
1546		return NULL;
1547	if (!ssl_supported_tls_version_range(s, &min_vers, &max_vers))
1548		return NULL;
1549	if ((ciphers = SSL_get_ciphers(s)) == NULL)
1550		return NULL;
1551	if ((supported_ciphers = sk_SSL_CIPHER_new_null()) == NULL)
1552		return NULL;
1553
1554	for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1555		if ((cipher = sk_SSL_CIPHER_value(ciphers, i)) == NULL)
1556			goto err;
1557		if (!ssl_cipher_allowed_in_tls_version_range(cipher, min_vers,
1558		    max_vers))
1559			continue;
1560		if (!ssl_security_supported_cipher(s, cipher))
1561			continue;
1562		if (!sk_SSL_CIPHER_push(supported_ciphers, cipher))
1563			goto err;
1564	}
1565
1566	if (sk_SSL_CIPHER_num(supported_ciphers) > 0)
1567		return supported_ciphers;
1568
1569 err:
1570	sk_SSL_CIPHER_free(supported_ciphers);
1571	return NULL;
1572}
1573LSSL_ALIAS(SSL_get1_supported_ciphers);
1574
1575/* See if we have any ECC cipher suites. */
1576int
1577ssl_has_ecc_ciphers(SSL *s)
1578{
1579	STACK_OF(SSL_CIPHER) *ciphers;
1580	unsigned long alg_k, alg_a;
1581	SSL_CIPHER *cipher;
1582	int i;
1583
1584	if ((ciphers = SSL_get_ciphers(s)) == NULL)
1585		return 0;
1586
1587	for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1588		cipher = sk_SSL_CIPHER_value(ciphers, i);
1589
1590		alg_k = cipher->algorithm_mkey;
1591		alg_a = cipher->algorithm_auth;
1592
1593		if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))
1594			return 1;
1595	}
1596
1597	return 0;
1598}
1599
1600/* The old interface to get the same thing as SSL_get_ciphers(). */
1601const char *
1602SSL_get_cipher_list(const SSL *s, int n)
1603{
1604	STACK_OF(SSL_CIPHER) *ciphers;
1605	const SSL_CIPHER *cipher;
1606
1607	if ((ciphers = SSL_get_ciphers(s)) == NULL)
1608		return (NULL);
1609	if ((cipher = sk_SSL_CIPHER_value(ciphers, n)) == NULL)
1610		return (NULL);
1611
1612	return (cipher->name);
1613}
1614LSSL_ALIAS(SSL_get_cipher_list);
1615
1616STACK_OF(SSL_CIPHER) *
1617SSL_CTX_get_ciphers(const SSL_CTX *ctx)
1618{
1619	if (ctx == NULL)
1620		return NULL;
1621	return ctx->cipher_list;
1622}
1623LSSL_ALIAS(SSL_CTX_get_ciphers);
1624
1625/* Specify the ciphers to be used by default by the SSL_CTX. */
1626int
1627SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
1628{
1629	STACK_OF(SSL_CIPHER) *ciphers;
1630
1631	/*
1632	 * ssl_create_cipher_list may return an empty stack if it was unable to
1633	 * find a cipher matching the given rule string (for example if the
1634	 * rule string specifies a cipher which has been disabled). This is not
1635	 * an error as far as ssl_create_cipher_list is concerned, and hence
1636	 * ctx->cipher_list has been updated.
1637	 */
1638	ciphers = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
1639	    ctx->cipher_list_tls13, str, ctx->cert);
1640	if (ciphers == NULL) {
1641		return (0);
1642	} else if (sk_SSL_CIPHER_num(ciphers) == 0) {
1643		SSLerrorx(SSL_R_NO_CIPHER_MATCH);
1644		return (0);
1645	}
1646	return (1);
1647}
1648LSSL_ALIAS(SSL_CTX_set_cipher_list);
1649
1650int
1651SSL_CTX_set_ciphersuites(SSL_CTX *ctx, const char *str)
1652{
1653	if (!ssl_parse_ciphersuites(&ctx->cipher_list_tls13, str)) {
1654		SSLerrorx(SSL_R_NO_CIPHER_MATCH);
1655		return 0;
1656	}
1657	if (!ssl_merge_cipherlists(ctx->cipher_list,
1658	    ctx->cipher_list_tls13, &ctx->cipher_list))
1659		return 0;
1660
1661	return 1;
1662}
1663LSSL_ALIAS(SSL_CTX_set_ciphersuites);
1664
1665/* Specify the ciphers to be used by the SSL. */
1666int
1667SSL_set_cipher_list(SSL *s, const char *str)
1668{
1669	STACK_OF(SSL_CIPHER) *ciphers, *ciphers_tls13;
1670
1671	if ((ciphers_tls13 = s->cipher_list_tls13) == NULL)
1672		ciphers_tls13 = s->ctx->cipher_list_tls13;
1673
1674	/* See comment in SSL_CTX_set_cipher_list. */
1675	ciphers = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
1676	    ciphers_tls13, str, s->cert);
1677	if (ciphers == NULL) {
1678		return (0);
1679	} else if (sk_SSL_CIPHER_num(ciphers) == 0) {
1680		SSLerror(s, SSL_R_NO_CIPHER_MATCH);
1681		return (0);
1682	}
1683	return (1);
1684}
1685LSSL_ALIAS(SSL_set_cipher_list);
1686
1687int
1688SSL_set_ciphersuites(SSL *s, const char *str)
1689{
1690	STACK_OF(SSL_CIPHER) *ciphers;
1691
1692	if ((ciphers = s->cipher_list) == NULL)
1693		ciphers = s->ctx->cipher_list;
1694
1695	if (!ssl_parse_ciphersuites(&s->cipher_list_tls13, str)) {
1696		SSLerrorx(SSL_R_NO_CIPHER_MATCH);
1697		return (0);
1698	}
1699	if (!ssl_merge_cipherlists(ciphers, s->cipher_list_tls13,
1700	    &s->cipher_list))
1701		return 0;
1702
1703	return 1;
1704}
1705LSSL_ALIAS(SSL_set_ciphersuites);
1706
1707char *
1708SSL_get_shared_ciphers(const SSL *s, char *buf, int len)
1709{
1710	STACK_OF(SSL_CIPHER) *client_ciphers, *server_ciphers;
1711	const SSL_CIPHER *cipher;
1712	size_t curlen = 0;
1713	char *end;
1714	int i;
1715
1716	if (!s->server || s->session == NULL || len < 2)
1717		return NULL;
1718
1719	if ((client_ciphers = s->session->ciphers) == NULL)
1720		return NULL;
1721	if ((server_ciphers = SSL_get_ciphers(s)) == NULL)
1722		return NULL;
1723	if (sk_SSL_CIPHER_num(client_ciphers) == 0 ||
1724	    sk_SSL_CIPHER_num(server_ciphers) == 0)
1725		return NULL;
1726
1727	buf[0] = '\0';
1728	for (i = 0; i < sk_SSL_CIPHER_num(client_ciphers); i++) {
1729		cipher = sk_SSL_CIPHER_value(client_ciphers, i);
1730
1731		if (sk_SSL_CIPHER_find(server_ciphers, cipher) < 0)
1732			continue;
1733
1734		end = buf + curlen;
1735		if (strlcat(buf, cipher->name, len) >= len ||
1736		    (curlen = strlcat(buf, ":", len)) >= len) {
1737			/* remove truncated cipher from list */
1738			*end = '\0';
1739			break;
1740		}
1741	}
1742	/* remove trailing colon */
1743	if ((end = strrchr(buf, ':')) != NULL)
1744		*end = '\0';
1745	return buf;
1746}
1747LSSL_ALIAS(SSL_get_shared_ciphers);
1748
1749/*
1750 * Return a servername extension value if provided in Client Hello, or NULL.
1751 * So far, only host_name types are defined (RFC 3546).
1752 */
1753const char *
1754SSL_get_servername(const SSL *s, const int type)
1755{
1756	if (type != TLSEXT_NAMETYPE_host_name)
1757		return (NULL);
1758
1759	return (s->session && !s->tlsext_hostname ?
1760	    s->session->tlsext_hostname :
1761	    s->tlsext_hostname);
1762}
1763LSSL_ALIAS(SSL_get_servername);
1764
1765int
1766SSL_get_servername_type(const SSL *s)
1767{
1768	if (s->session &&
1769	    (!s->tlsext_hostname ?
1770	    s->session->tlsext_hostname : s->tlsext_hostname))
1771		return (TLSEXT_NAMETYPE_host_name);
1772	return (-1);
1773}
1774LSSL_ALIAS(SSL_get_servername_type);
1775
1776/*
1777 * SSL_select_next_proto implements standard protocol selection. It is
1778 * expected that this function is called from the callback set by
1779 * SSL_CTX_set_alpn_select_cb.
1780 *
1781 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1782 * strings. The length byte itself is not included in the length. A byte
1783 * string of length 0 is invalid. No byte string may be truncated.
1784 *
1785 * It returns either:
1786 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1787 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1788 */
1789int
1790SSL_select_next_proto(unsigned char **out, unsigned char *outlen,
1791    const unsigned char *server, unsigned int server_len,
1792    const unsigned char *client, unsigned int client_len)
1793{
1794	unsigned int		 i, j;
1795	const unsigned char	*result;
1796	int			 status = OPENSSL_NPN_UNSUPPORTED;
1797
1798	/*
1799	 * For each protocol in server preference order,
1800	 * see if we support it.
1801	 */
1802	for (i = 0; i < server_len; ) {
1803		for (j = 0; j < client_len; ) {
1804			if (server[i] == client[j] &&
1805			    memcmp(&server[i + 1],
1806			    &client[j + 1], server[i]) == 0) {
1807				/* We found a match */
1808				result = &server[i];
1809				status = OPENSSL_NPN_NEGOTIATED;
1810				goto found;
1811			}
1812			j += client[j];
1813			j++;
1814		}
1815		i += server[i];
1816		i++;
1817	}
1818
1819	/* There's no overlap between our protocols and the server's list. */
1820	result = client;
1821	status = OPENSSL_NPN_NO_OVERLAP;
1822
1823 found:
1824	*out = (unsigned char *) result + 1;
1825	*outlen = result[0];
1826	return (status);
1827}
1828LSSL_ALIAS(SSL_select_next_proto);
1829
1830/* SSL_get0_next_proto_negotiated is deprecated. */
1831void
1832SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data,
1833    unsigned int *len)
1834{
1835	*data = NULL;
1836	*len = 0;
1837}
1838LSSL_ALIAS(SSL_get0_next_proto_negotiated);
1839
1840/* SSL_CTX_set_next_protos_advertised_cb is deprecated. */
1841void
1842SSL_CTX_set_next_protos_advertised_cb(SSL_CTX *ctx, int (*cb) (SSL *ssl,
1843    const unsigned char **out, unsigned int *outlen, void *arg), void *arg)
1844{
1845}
1846LSSL_ALIAS(SSL_CTX_set_next_protos_advertised_cb);
1847
1848/* SSL_CTX_set_next_proto_select_cb is deprecated. */
1849void
1850SSL_CTX_set_next_proto_select_cb(SSL_CTX *ctx, int (*cb) (SSL *s,
1851    unsigned char **out, unsigned char *outlen, const unsigned char *in,
1852    unsigned int inlen, void *arg), void *arg)
1853{
1854}
1855LSSL_ALIAS(SSL_CTX_set_next_proto_select_cb);
1856
1857/*
1858 * SSL_CTX_set_alpn_protos sets the ALPN protocol list to the specified
1859 * protocols, which must be in wire-format (i.e. a series of non-empty,
1860 * 8-bit length-prefixed strings). Returns 0 on success.
1861 */
1862int
1863SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char *protos,
1864    unsigned int protos_len)
1865{
1866	CBS cbs;
1867	int failed = 1;
1868
1869	if (protos == NULL)
1870		protos_len = 0;
1871
1872	CBS_init(&cbs, protos, protos_len);
1873
1874	if (protos_len > 0) {
1875		if (!tlsext_alpn_check_format(&cbs))
1876			goto err;
1877	}
1878
1879	if (!CBS_stow(&cbs, &ctx->alpn_client_proto_list,
1880	    &ctx->alpn_client_proto_list_len))
1881		goto err;
1882
1883	failed = 0;
1884
1885 err:
1886	/* NOTE: Return values are the reverse of what you expect. */
1887	return failed;
1888}
1889LSSL_ALIAS(SSL_CTX_set_alpn_protos);
1890
1891/*
1892 * SSL_set_alpn_protos sets the ALPN protocol list to the specified
1893 * protocols, which must be in wire-format (i.e. a series of non-empty,
1894 * 8-bit length-prefixed strings). Returns 0 on success.
1895 */
1896int
1897SSL_set_alpn_protos(SSL *ssl, const unsigned char *protos,
1898    unsigned int protos_len)
1899{
1900	CBS cbs;
1901	int failed = 1;
1902
1903	if (protos == NULL)
1904		protos_len = 0;
1905
1906	CBS_init(&cbs, protos, protos_len);
1907
1908	if (protos_len > 0) {
1909		if (!tlsext_alpn_check_format(&cbs))
1910			goto err;
1911	}
1912
1913	if (!CBS_stow(&cbs, &ssl->alpn_client_proto_list,
1914	    &ssl->alpn_client_proto_list_len))
1915		goto err;
1916
1917	failed = 0;
1918
1919 err:
1920	/* NOTE: Return values are the reverse of what you expect. */
1921	return failed;
1922}
1923LSSL_ALIAS(SSL_set_alpn_protos);
1924
1925/*
1926 * SSL_CTX_set_alpn_select_cb sets a callback function that is called during
1927 * ClientHello processing in order to select an ALPN protocol from the
1928 * client's list of offered protocols.
1929 */
1930void
1931SSL_CTX_set_alpn_select_cb(SSL_CTX* ctx,
1932    int (*cb) (SSL *ssl, const unsigned char **out, unsigned char *outlen,
1933    const unsigned char *in, unsigned int inlen, void *arg), void *arg)
1934{
1935	ctx->alpn_select_cb = cb;
1936	ctx->alpn_select_cb_arg = arg;
1937}
1938LSSL_ALIAS(SSL_CTX_set_alpn_select_cb);
1939
1940/*
1941 * SSL_get0_alpn_selected gets the selected ALPN protocol (if any). On return
1942 * it sets data to point to len bytes of protocol name (not including the
1943 * leading length-prefix byte). If the server didn't respond with* a negotiated
1944 * protocol then len will be zero.
1945 */
1946void
1947SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
1948    unsigned int *len)
1949{
1950	*data = ssl->s3->alpn_selected;
1951	*len = ssl->s3->alpn_selected_len;
1952}
1953LSSL_ALIAS(SSL_get0_alpn_selected);
1954
1955void
1956SSL_set_psk_use_session_callback(SSL *s, SSL_psk_use_session_cb_func cb)
1957{
1958	return;
1959}
1960LSSL_ALIAS(SSL_set_psk_use_session_callback);
1961
1962int
1963SSL_export_keying_material(SSL *s, unsigned char *out, size_t out_len,
1964    const char *label, size_t label_len, const unsigned char *context,
1965    size_t context_len, int use_context)
1966{
1967	if (s->tls13 != NULL && s->version == TLS1_3_VERSION) {
1968		if (!use_context) {
1969			context = NULL;
1970			context_len = 0;
1971		}
1972		return tls13_exporter(s->tls13, label, label_len, context,
1973		    context_len, out, out_len);
1974	}
1975
1976	return tls12_exporter(s, label, label_len, context, context_len,
1977	    use_context, out, out_len);
1978}
1979LSSL_ALIAS(SSL_export_keying_material);
1980
1981static unsigned long
1982ssl_session_hash(const SSL_SESSION *a)
1983{
1984	unsigned long	l;
1985
1986	l = (unsigned long)
1987	    ((unsigned int) a->session_id[0]     )|
1988	    ((unsigned int) a->session_id[1]<< 8L)|
1989	    ((unsigned long)a->session_id[2]<<16L)|
1990	    ((unsigned long)a->session_id[3]<<24L);
1991	return (l);
1992}
1993
1994/*
1995 * NB: If this function (or indeed the hash function which uses a sort of
1996 * coarser function than this one) is changed, ensure
1997 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1998 * able to construct an SSL_SESSION that will collide with any existing session
1999 * with a matching session ID.
2000 */
2001static int
2002ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
2003{
2004	if (a->ssl_version != b->ssl_version)
2005		return (1);
2006	if (a->session_id_length != b->session_id_length)
2007		return (1);
2008	if (timingsafe_memcmp(a->session_id, b->session_id, a->session_id_length) != 0)
2009		return (1);
2010	return (0);
2011}
2012
2013/*
2014 * These wrapper functions should remain rather than redeclaring
2015 * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
2016 * variable. The reason is that the functions aren't static, they're exposed via
2017 * ssl.h.
2018 */
2019static unsigned long
2020ssl_session_LHASH_HASH(const void *arg)
2021{
2022	const SSL_SESSION *a = arg;
2023
2024	return ssl_session_hash(a);
2025}
2026
2027static int
2028ssl_session_LHASH_COMP(const void *arg1, const void *arg2)
2029{
2030	const SSL_SESSION *a = arg1;
2031	const SSL_SESSION *b = arg2;
2032
2033	return ssl_session_cmp(a, b);
2034}
2035
2036SSL_CTX *
2037SSL_CTX_new(const SSL_METHOD *meth)
2038{
2039	SSL_CTX	*ret;
2040
2041	if (!OPENSSL_init_ssl(0, NULL)) {
2042		SSLerrorx(SSL_R_LIBRARY_BUG);
2043		return (NULL);
2044	}
2045
2046	if (meth == NULL) {
2047		SSLerrorx(SSL_R_NULL_SSL_METHOD_PASSED);
2048		return (NULL);
2049	}
2050
2051	if ((ret = calloc(1, sizeof(*ret))) == NULL) {
2052		SSLerrorx(ERR_R_MALLOC_FAILURE);
2053		return (NULL);
2054	}
2055
2056	if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
2057		SSLerrorx(SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
2058		goto err;
2059	}
2060
2061	ret->method = meth;
2062	ret->min_tls_version = meth->min_tls_version;
2063	ret->max_tls_version = meth->max_tls_version;
2064	ret->min_proto_version = 0;
2065	ret->max_proto_version = 0;
2066	ret->mode = SSL_MODE_AUTO_RETRY;
2067
2068	ret->cert_store = NULL;
2069	ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
2070	ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
2071	ret->session_cache_head = NULL;
2072	ret->session_cache_tail = NULL;
2073
2074	/* We take the system default */
2075	ret->session_timeout = ssl_get_default_timeout();
2076
2077	ret->new_session_cb = NULL;
2078	ret->remove_session_cb = NULL;
2079	ret->get_session_cb = NULL;
2080	ret->generate_session_id = NULL;
2081
2082	memset((char *)&ret->stats, 0, sizeof(ret->stats));
2083
2084	ret->references = 1;
2085	ret->quiet_shutdown = 0;
2086
2087	ret->info_callback = NULL;
2088
2089	ret->app_verify_callback = NULL;
2090	ret->app_verify_arg = NULL;
2091
2092	ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
2093	ret->read_ahead = 0;
2094	ret->msg_callback = NULL;
2095	ret->msg_callback_arg = NULL;
2096	ret->verify_mode = SSL_VERIFY_NONE;
2097	ret->sid_ctx_length = 0;
2098	ret->default_verify_callback = NULL;
2099
2100	if ((ret->cert = ssl_cert_new()) == NULL)
2101		goto err;
2102
2103	ret->default_passwd_callback = NULL;
2104	ret->default_passwd_callback_userdata = NULL;
2105	ret->client_cert_cb = NULL;
2106	ret->app_gen_cookie_cb = NULL;
2107	ret->app_verify_cookie_cb = NULL;
2108
2109	ret->sessions = lh_SSL_SESSION_new();
2110	if (ret->sessions == NULL)
2111		goto err;
2112	ret->cert_store = X509_STORE_new();
2113	if (ret->cert_store == NULL)
2114		goto err;
2115
2116	ssl_create_cipher_list(ret->method, &ret->cipher_list,
2117	    NULL, SSL_DEFAULT_CIPHER_LIST, ret->cert);
2118	if (ret->cipher_list == NULL ||
2119	    sk_SSL_CIPHER_num(ret->cipher_list) <= 0) {
2120		SSLerrorx(SSL_R_LIBRARY_HAS_NO_CIPHERS);
2121		goto err2;
2122	}
2123
2124	ret->param = X509_VERIFY_PARAM_new();
2125	if (!ret->param)
2126		goto err;
2127
2128	if ((ret->client_CA = sk_X509_NAME_new_null()) == NULL)
2129		goto err;
2130
2131	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
2132
2133	ret->extra_certs = NULL;
2134
2135	ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
2136
2137	ret->tlsext_servername_callback = 0;
2138	ret->tlsext_servername_arg = NULL;
2139
2140	/* Setup RFC4507 ticket keys */
2141	arc4random_buf(ret->tlsext_tick_key_name, 16);
2142	arc4random_buf(ret->tlsext_tick_hmac_key, 16);
2143	arc4random_buf(ret->tlsext_tick_aes_key, 16);
2144
2145	ret->tlsext_status_cb = 0;
2146	ret->tlsext_status_arg = NULL;
2147
2148	/*
2149	 * Default is to connect to non-RI servers. When RI is more widely
2150	 * deployed might change this.
2151	 */
2152	ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
2153
2154	return (ret);
2155 err:
2156	SSLerrorx(ERR_R_MALLOC_FAILURE);
2157 err2:
2158	SSL_CTX_free(ret);
2159	return (NULL);
2160}
2161LSSL_ALIAS(SSL_CTX_new);
2162
2163void
2164SSL_CTX_free(SSL_CTX *ctx)
2165{
2166	int	i;
2167
2168	if (ctx == NULL)
2169		return;
2170
2171	i = CRYPTO_add(&ctx->references, -1, CRYPTO_LOCK_SSL_CTX);
2172	if (i > 0)
2173		return;
2174
2175	X509_VERIFY_PARAM_free(ctx->param);
2176
2177	/*
2178	 * Free internal session cache. However: the remove_cb() may reference
2179	 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
2180	 * after the sessions were flushed.
2181	 * As the ex_data handling routines might also touch the session cache,
2182	 * the most secure solution seems to be: empty (flush) the cache, then
2183	 * free ex_data, then finally free the cache.
2184	 * (See ticket [openssl.org #212].)
2185	 */
2186	if (ctx->sessions != NULL)
2187		SSL_CTX_flush_sessions(ctx, 0);
2188
2189	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ctx, &ctx->ex_data);
2190
2191	lh_SSL_SESSION_free(ctx->sessions);
2192
2193	X509_STORE_free(ctx->cert_store);
2194	sk_SSL_CIPHER_free(ctx->cipher_list);
2195	sk_SSL_CIPHER_free(ctx->cipher_list_tls13);
2196	ssl_cert_free(ctx->cert);
2197	sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
2198	sk_X509_pop_free(ctx->extra_certs, X509_free);
2199
2200#ifndef OPENSSL_NO_SRTP
2201	if (ctx->srtp_profiles)
2202		sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
2203#endif
2204
2205	free(ctx->tlsext_ecpointformatlist);
2206	free(ctx->tlsext_supportedgroups);
2207
2208	free(ctx->alpn_client_proto_list);
2209
2210	free(ctx);
2211}
2212LSSL_ALIAS(SSL_CTX_free);
2213
2214int
2215SSL_CTX_up_ref(SSL_CTX *ctx)
2216{
2217	return CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX) > 1;
2218}
2219LSSL_ALIAS(SSL_CTX_up_ref);
2220
2221pem_password_cb *
2222SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx)
2223{
2224	return (ctx->default_passwd_callback);
2225}
2226LSSL_ALIAS(SSL_CTX_get_default_passwd_cb);
2227
2228void
2229SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
2230{
2231	ctx->default_passwd_callback = cb;
2232}
2233LSSL_ALIAS(SSL_CTX_set_default_passwd_cb);
2234
2235void *
2236SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx)
2237{
2238	return ctx->default_passwd_callback_userdata;
2239}
2240LSSL_ALIAS(SSL_CTX_get_default_passwd_cb_userdata);
2241
2242void
2243SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u)
2244{
2245	ctx->default_passwd_callback_userdata = u;
2246}
2247LSSL_ALIAS(SSL_CTX_set_default_passwd_cb_userdata);
2248
2249void
2250SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
2251    int (*cb)(X509_STORE_CTX *, void *), void *arg)
2252{
2253	ctx->app_verify_callback = cb;
2254	ctx->app_verify_arg = arg;
2255}
2256LSSL_ALIAS(SSL_CTX_set_cert_verify_callback);
2257
2258void
2259SSL_CTX_set_verify(SSL_CTX *ctx, int mode, int (*cb)(int, X509_STORE_CTX *))
2260{
2261	ctx->verify_mode = mode;
2262	ctx->default_verify_callback = cb;
2263}
2264LSSL_ALIAS(SSL_CTX_set_verify);
2265
2266void
2267SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth)
2268{
2269	X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2270}
2271LSSL_ALIAS(SSL_CTX_set_verify_depth);
2272
2273void
2274ssl_set_cert_masks(SSL_CERT *c, const SSL_CIPHER *cipher)
2275{
2276	unsigned long mask_a, mask_k;
2277	SSL_CERT_PKEY *cpk;
2278
2279	if (c == NULL)
2280		return;
2281
2282	mask_a = SSL_aNULL | SSL_aTLS1_3;
2283	mask_k = SSL_kECDHE | SSL_kTLS1_3;
2284
2285	if (c->dhe_params != NULL || c->dhe_params_cb != NULL ||
2286	    c->dhe_params_auto != 0)
2287		mask_k |= SSL_kDHE;
2288
2289	cpk = &(c->pkeys[SSL_PKEY_ECC]);
2290	if (cpk->x509 != NULL && cpk->privatekey != NULL) {
2291		/* Key usage, if present, must allow signing. */
2292		if (X509_get_key_usage(cpk->x509) & X509v3_KU_DIGITAL_SIGNATURE)
2293			mask_a |= SSL_aECDSA;
2294	}
2295
2296	cpk = &(c->pkeys[SSL_PKEY_RSA]);
2297	if (cpk->x509 != NULL && cpk->privatekey != NULL) {
2298		mask_a |= SSL_aRSA;
2299		mask_k |= SSL_kRSA;
2300	}
2301
2302	c->mask_k = mask_k;
2303	c->mask_a = mask_a;
2304	c->valid = 1;
2305}
2306
2307/* See if this handshake is using an ECC cipher suite. */
2308int
2309ssl_using_ecc_cipher(SSL *s)
2310{
2311	unsigned long alg_a, alg_k;
2312
2313	alg_a = s->s3->hs.cipher->algorithm_auth;
2314	alg_k = s->s3->hs.cipher->algorithm_mkey;
2315
2316	return s->session->tlsext_ecpointformatlist != NULL &&
2317	    s->session->tlsext_ecpointformatlist_length > 0 &&
2318	    ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA));
2319}
2320
2321int
2322ssl_check_srvr_ecc_cert_and_alg(SSL *s, X509 *x)
2323{
2324	const SSL_CIPHER *cs = s->s3->hs.cipher;
2325	unsigned long alg_a;
2326
2327	alg_a = cs->algorithm_auth;
2328
2329	if (alg_a & SSL_aECDSA) {
2330		/* Key usage, if present, must allow signing. */
2331		if (!(X509_get_key_usage(x) & X509v3_KU_DIGITAL_SIGNATURE)) {
2332			SSLerror(s, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2333			return (0);
2334		}
2335	}
2336
2337	return (1);
2338}
2339
2340SSL_CERT_PKEY *
2341ssl_get_server_send_pkey(const SSL *s)
2342{
2343	unsigned long alg_a;
2344	SSL_CERT *c;
2345	int i;
2346
2347	c = s->cert;
2348	ssl_set_cert_masks(c, s->s3->hs.cipher);
2349
2350	alg_a = s->s3->hs.cipher->algorithm_auth;
2351
2352	if (alg_a & SSL_aECDSA) {
2353		i = SSL_PKEY_ECC;
2354	} else if (alg_a & SSL_aRSA) {
2355		i = SSL_PKEY_RSA;
2356	} else { /* if (alg_a & SSL_aNULL) */
2357		SSLerror(s, ERR_R_INTERNAL_ERROR);
2358		return (NULL);
2359	}
2360
2361	return (c->pkeys + i);
2362}
2363
2364EVP_PKEY *
2365ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher, const EVP_MD **pmd,
2366    const struct ssl_sigalg **sap)
2367{
2368	const struct ssl_sigalg *sigalg = NULL;
2369	EVP_PKEY *pkey = NULL;
2370	unsigned long alg_a;
2371	SSL_CERT *c;
2372	int idx = -1;
2373
2374	alg_a = cipher->algorithm_auth;
2375	c = s->cert;
2376
2377	if (alg_a & SSL_aRSA) {
2378		idx = SSL_PKEY_RSA;
2379	} else if ((alg_a & SSL_aECDSA) &&
2380	    (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2381		idx = SSL_PKEY_ECC;
2382	if (idx == -1) {
2383		SSLerror(s, ERR_R_INTERNAL_ERROR);
2384		return (NULL);
2385	}
2386
2387	pkey = c->pkeys[idx].privatekey;
2388	if ((sigalg = ssl_sigalg_select(s, pkey)) == NULL) {
2389		SSLerror(s, SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2390		return (NULL);
2391	}
2392	*pmd = sigalg->md();
2393	*sap = sigalg;
2394
2395	return (pkey);
2396}
2397
2398size_t
2399ssl_dhe_params_auto_key_bits(SSL *s)
2400{
2401	SSL_CERT_PKEY *cpk;
2402	int key_bits;
2403
2404	if (s->cert->dhe_params_auto == 2) {
2405		key_bits = 1024;
2406	} else if (s->s3->hs.cipher->algorithm_auth & SSL_aNULL) {
2407		key_bits = 1024;
2408		if (s->s3->hs.cipher->strength_bits == 256)
2409			key_bits = 3072;
2410	} else {
2411		if ((cpk = ssl_get_server_send_pkey(s)) == NULL)
2412			return 0;
2413		if (cpk->privatekey == NULL ||
2414		    EVP_PKEY_get0_RSA(cpk->privatekey) == NULL)
2415			return 0;
2416		if ((key_bits = EVP_PKEY_bits(cpk->privatekey)) <= 0)
2417			return 0;
2418	}
2419
2420	return key_bits;
2421}
2422
2423static int
2424ssl_should_update_external_cache(SSL *s, int mode)
2425{
2426	int cache_mode;
2427
2428	cache_mode = s->session_ctx->session_cache_mode;
2429
2430	/* Don't cache if mode says not to */
2431	if ((cache_mode & mode) == 0)
2432		return 0;
2433
2434	/* if it is not already cached, cache it */
2435	if (!s->hit)
2436		return 1;
2437
2438	/* If it's TLS 1.3, do it to match OpenSSL */
2439	if (s->s3->hs.negotiated_tls_version >= TLS1_3_VERSION)
2440		return 1;
2441
2442	return 0;
2443}
2444
2445static int
2446ssl_should_update_internal_cache(SSL *s, int mode)
2447{
2448	int cache_mode;
2449
2450	cache_mode = s->session_ctx->session_cache_mode;
2451
2452	/* Don't cache if mode says not to */
2453	if ((cache_mode & mode) == 0)
2454		return 0;
2455
2456	/* If it is already cached, don't cache it again */
2457	if (s->hit)
2458		return 0;
2459
2460	if ((cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) != 0)
2461		return 0;
2462
2463	/* If we are lesser than TLS 1.3, Cache it. */
2464	if (s->s3->hs.negotiated_tls_version < TLS1_3_VERSION)
2465		return 1;
2466
2467	/* Below this we consider TLS 1.3 or later */
2468
2469	/* If it's not a server, add it? OpenSSL does this. */
2470	if (!s->server)
2471		return 1;
2472
2473	/* XXX if we support early data / PSK need to add */
2474
2475	/*
2476	 * If we have the remove session callback, we will want
2477	 * to know about this even if it's a stateless ticket
2478	 * from 1.3 so we can know when it is removed.
2479	 */
2480	if (s->session_ctx->remove_session_cb != NULL)
2481		return 1;
2482
2483	/* If we have set OP_NO_TICKET, cache it. */
2484	if ((s->options & SSL_OP_NO_TICKET) != 0)
2485		return 1;
2486
2487	/* Otherwise do not cache */
2488	return 0;
2489}
2490
2491void
2492ssl_update_cache(SSL *s, int mode)
2493{
2494	int cache_mode, do_callback;
2495
2496	if (s->session->session_id_length == 0)
2497		return;
2498
2499	cache_mode = s->session_ctx->session_cache_mode;
2500	do_callback = ssl_should_update_external_cache(s, mode);
2501
2502	if (ssl_should_update_internal_cache(s, mode)) {
2503		/*
2504		 * XXX should we fail if the add to the internal cache
2505		 * fails? OpenSSL doesn't care..
2506		 */
2507		(void) SSL_CTX_add_session(s->session_ctx, s->session);
2508	}
2509
2510	/*
2511	 * Update the "external cache" by calling the new session
2512	 * callback if present, even with TLS 1.3 without early data
2513	 * "because some application just want to know about the
2514	 * creation of a session and aren't doing a full cache".
2515	 * Apparently, if they are doing a full cache, they'll have
2516	 * some fun, but we endeavour to give application writers the
2517	 * same glorious experience they expect from OpenSSL which
2518	 * does it this way.
2519	 */
2520	if (do_callback && s->session_ctx->new_session_cb != NULL) {
2521		    CRYPTO_add(&s->session->references, 1, CRYPTO_LOCK_SSL_SESSION);
2522		    if (!s->session_ctx->new_session_cb(s, s->session))
2523			    SSL_SESSION_free(s->session);
2524	}
2525
2526	/* Auto flush every 255 connections. */
2527	if (!(cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
2528	    (cache_mode & mode) != 0) {
2529		int connections;
2530		if (mode & SSL_SESS_CACHE_CLIENT)
2531			connections = s->session_ctx->stats.sess_connect_good;
2532		else
2533			connections = s->session_ctx->stats.sess_accept_good;
2534		if ((connections & 0xff) == 0xff)
2535			SSL_CTX_flush_sessions(s->session_ctx, time(NULL));
2536	}
2537}
2538
2539const SSL_METHOD *
2540SSL_get_ssl_method(SSL *s)
2541{
2542	return (s->method);
2543}
2544LSSL_ALIAS(SSL_get_ssl_method);
2545
2546int
2547SSL_set_ssl_method(SSL *s, const SSL_METHOD *method)
2548{
2549	int (*handshake_func)(SSL *) = NULL;
2550	int ret = 1;
2551
2552	if (s->method == method)
2553		return (ret);
2554
2555	if (s->handshake_func == s->method->ssl_connect)
2556		handshake_func = method->ssl_connect;
2557	else if (s->handshake_func == s->method->ssl_accept)
2558		handshake_func = method->ssl_accept;
2559
2560	if (s->method->version == method->version) {
2561		s->method = method;
2562	} else {
2563		s->method->ssl_free(s);
2564		s->method = method;
2565		ret = s->method->ssl_new(s);
2566	}
2567	s->handshake_func = handshake_func;
2568
2569	return (ret);
2570}
2571LSSL_ALIAS(SSL_set_ssl_method);
2572
2573int
2574SSL_get_error(const SSL *s, int i)
2575{
2576	unsigned long l;
2577	int reason;
2578	BIO *bio;
2579
2580	if (i > 0)
2581		return (SSL_ERROR_NONE);
2582
2583	/*
2584	 * Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2585	 * etc, where we do encode the error.
2586	 */
2587	if ((l = ERR_peek_error()) != 0) {
2588		if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2589			return (SSL_ERROR_SYSCALL);
2590		else
2591			return (SSL_ERROR_SSL);
2592	}
2593
2594	if (SSL_want_read(s)) {
2595		bio = SSL_get_rbio(s);
2596		if (BIO_should_read(bio)) {
2597			return (SSL_ERROR_WANT_READ);
2598		} else if (BIO_should_write(bio)) {
2599			/*
2600			 * This one doesn't make too much sense...  We never
2601			 * try to write to the rbio, and an application
2602			 * program where rbio and wbio are separate couldn't
2603			 * even know what it should wait for.  However if we
2604			 * ever set s->rwstate incorrectly (so that we have
2605			 * SSL_want_read(s) instead of SSL_want_write(s))
2606			 * and rbio and wbio *are* the same, this test works
2607			 * around that bug; so it might be safer to keep it.
2608			 */
2609			return (SSL_ERROR_WANT_WRITE);
2610		} else if (BIO_should_io_special(bio)) {
2611			reason = BIO_get_retry_reason(bio);
2612			if (reason == BIO_RR_CONNECT)
2613				return (SSL_ERROR_WANT_CONNECT);
2614			else if (reason == BIO_RR_ACCEPT)
2615				return (SSL_ERROR_WANT_ACCEPT);
2616			else
2617				return (SSL_ERROR_SYSCALL); /* unknown */
2618		}
2619	}
2620
2621	if (SSL_want_write(s)) {
2622		bio = SSL_get_wbio(s);
2623		if (BIO_should_write(bio)) {
2624			return (SSL_ERROR_WANT_WRITE);
2625		} else if (BIO_should_read(bio)) {
2626			/*
2627			 * See above (SSL_want_read(s) with
2628			 * BIO_should_write(bio))
2629			 */
2630			return (SSL_ERROR_WANT_READ);
2631		} else if (BIO_should_io_special(bio)) {
2632			reason = BIO_get_retry_reason(bio);
2633			if (reason == BIO_RR_CONNECT)
2634				return (SSL_ERROR_WANT_CONNECT);
2635			else if (reason == BIO_RR_ACCEPT)
2636				return (SSL_ERROR_WANT_ACCEPT);
2637			else
2638				return (SSL_ERROR_SYSCALL);
2639		}
2640	}
2641
2642	if (SSL_want_x509_lookup(s))
2643		return (SSL_ERROR_WANT_X509_LOOKUP);
2644
2645	if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2646	    (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
2647		return (SSL_ERROR_ZERO_RETURN);
2648
2649	return (SSL_ERROR_SYSCALL);
2650}
2651LSSL_ALIAS(SSL_get_error);
2652
2653int
2654SSL_CTX_set_quic_method(SSL_CTX *ctx, const SSL_QUIC_METHOD *quic_method)
2655{
2656	if (ctx->method->dtls)
2657		return 0;
2658
2659	ctx->quic_method = quic_method;
2660
2661	return 1;
2662}
2663LSSL_ALIAS(SSL_CTX_set_quic_method);
2664
2665int
2666SSL_set_quic_method(SSL *ssl, const SSL_QUIC_METHOD *quic_method)
2667{
2668	if (ssl->method->dtls)
2669		return 0;
2670
2671	ssl->quic_method = quic_method;
2672
2673	return 1;
2674}
2675LSSL_ALIAS(SSL_set_quic_method);
2676
2677size_t
2678SSL_quic_max_handshake_flight_len(const SSL *ssl,
2679    enum ssl_encryption_level_t level)
2680{
2681	size_t flight_len;
2682
2683	/* Limit flights to 16K when there are no large certificate messages. */
2684	flight_len = 16384;
2685
2686	switch (level) {
2687	case ssl_encryption_initial:
2688		return flight_len;
2689
2690	case ssl_encryption_early_data:
2691		/* QUIC does not send EndOfEarlyData. */
2692		return 0;
2693
2694	case ssl_encryption_handshake:
2695		if (ssl->server) {
2696			/*
2697			 * Servers may receive Certificate message if configured
2698			 * to request client certificates.
2699			 */
2700			if ((SSL_get_verify_mode(ssl) & SSL_VERIFY_PEER) != 0 &&
2701			    ssl->max_cert_list > flight_len)
2702				flight_len = ssl->max_cert_list;
2703		} else {
2704			/*
2705			 * Clients may receive both Certificate message and a
2706			 * CertificateRequest message.
2707			 */
2708			if (ssl->max_cert_list * 2 > flight_len)
2709				flight_len = ssl->max_cert_list * 2;
2710		}
2711		return flight_len;
2712	case ssl_encryption_application:
2713		/*
2714		 * Note there is not actually a bound on the number of
2715		 * NewSessionTickets one may send in a row. This level may need
2716		 * more involved flow control.
2717		 */
2718		return flight_len;
2719	}
2720
2721	return 0;
2722}
2723LSSL_ALIAS(SSL_quic_max_handshake_flight_len);
2724
2725enum ssl_encryption_level_t
2726SSL_quic_read_level(const SSL *ssl)
2727{
2728	return ssl->s3->hs.tls13.quic_read_level;
2729}
2730LSSL_ALIAS(SSL_quic_read_level);
2731
2732enum ssl_encryption_level_t
2733SSL_quic_write_level(const SSL *ssl)
2734{
2735	return ssl->s3->hs.tls13.quic_write_level;
2736}
2737LSSL_ALIAS(SSL_quic_write_level);
2738
2739int
2740SSL_provide_quic_data(SSL *ssl, enum ssl_encryption_level_t level,
2741    const uint8_t *data, size_t len)
2742{
2743	if (!SSL_is_quic(ssl)) {
2744		SSLerror(ssl, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2745		return 0;
2746	}
2747
2748	if (level != SSL_quic_read_level(ssl)) {
2749		SSLerror(ssl, SSL_R_WRONG_ENCRYPTION_LEVEL_RECEIVED);
2750		return 0;
2751	}
2752
2753	if (ssl->s3->hs.tls13.quic_read_buffer == NULL) {
2754		ssl->s3->hs.tls13.quic_read_buffer = tls_buffer_new(0);
2755		if (ssl->s3->hs.tls13.quic_read_buffer == NULL) {
2756			SSLerror(ssl, ERR_R_MALLOC_FAILURE);
2757			return 0;
2758		}
2759	}
2760
2761	/* XXX - note that this does not currently downsize. */
2762	tls_buffer_set_capacity_limit(ssl->s3->hs.tls13.quic_read_buffer,
2763	    SSL_quic_max_handshake_flight_len(ssl, level));
2764
2765	/*
2766	 * XXX - an append that fails due to exceeding capacity should set
2767	 * SSL_R_EXCESSIVE_MESSAGE_SIZE.
2768	 */
2769	return tls_buffer_append(ssl->s3->hs.tls13.quic_read_buffer, data, len);
2770}
2771LSSL_ALIAS(SSL_provide_quic_data);
2772
2773int
2774SSL_process_quic_post_handshake(SSL *ssl)
2775{
2776	/* XXX - this needs to run PHH received. */
2777	return 1;
2778}
2779LSSL_ALIAS(SSL_process_quic_post_handshake);
2780
2781int
2782SSL_do_handshake(SSL *s)
2783{
2784	if (s->handshake_func == NULL) {
2785		SSLerror(s, SSL_R_CONNECTION_TYPE_NOT_SET);
2786		return (-1);
2787	}
2788
2789	s->method->ssl_renegotiate_check(s);
2790
2791	if (!SSL_in_init(s) && !SSL_in_before(s))
2792		return 1;
2793
2794	return s->handshake_func(s);
2795}
2796LSSL_ALIAS(SSL_do_handshake);
2797
2798/*
2799 * For the next 2 functions, SSL_clear() sets shutdown and so
2800 * one of these calls will reset it
2801 */
2802void
2803SSL_set_accept_state(SSL *s)
2804{
2805	s->server = 1;
2806	s->shutdown = 0;
2807	s->s3->hs.state = SSL_ST_ACCEPT|SSL_ST_BEFORE;
2808	s->handshake_func = s->method->ssl_accept;
2809	ssl_clear_cipher_state(s);
2810}
2811LSSL_ALIAS(SSL_set_accept_state);
2812
2813void
2814SSL_set_connect_state(SSL *s)
2815{
2816	s->server = 0;
2817	s->shutdown = 0;
2818	s->s3->hs.state = SSL_ST_CONNECT|SSL_ST_BEFORE;
2819	s->handshake_func = s->method->ssl_connect;
2820	ssl_clear_cipher_state(s);
2821}
2822LSSL_ALIAS(SSL_set_connect_state);
2823
2824int
2825ssl_undefined_function(SSL *s)
2826{
2827	SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2828	return (0);
2829}
2830
2831int
2832ssl_undefined_void_function(void)
2833{
2834	SSLerrorx(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2835	return (0);
2836}
2837
2838int
2839ssl_undefined_const_function(const SSL *s)
2840{
2841	SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2842	return (0);
2843}
2844
2845const char *
2846ssl_version_string(int ver)
2847{
2848	switch (ver) {
2849	case TLS1_VERSION:
2850		return (SSL_TXT_TLSV1);
2851	case TLS1_1_VERSION:
2852		return (SSL_TXT_TLSV1_1);
2853	case TLS1_2_VERSION:
2854		return (SSL_TXT_TLSV1_2);
2855	case TLS1_3_VERSION:
2856		return (SSL_TXT_TLSV1_3);
2857	case DTLS1_VERSION:
2858		return (SSL_TXT_DTLS1);
2859	case DTLS1_2_VERSION:
2860		return (SSL_TXT_DTLS1_2);
2861	default:
2862		return ("unknown");
2863	}
2864}
2865
2866const char *
2867SSL_get_version(const SSL *s)
2868{
2869	return ssl_version_string(s->version);
2870}
2871LSSL_ALIAS(SSL_get_version);
2872
2873SSL *
2874SSL_dup(SSL *s)
2875{
2876	STACK_OF(X509_NAME) *sk;
2877	X509_NAME *xn;
2878	SSL *ret;
2879	int i;
2880
2881	if ((ret = SSL_new(SSL_get_SSL_CTX(s))) == NULL)
2882		goto err;
2883
2884	ret->version = s->version;
2885	ret->method = s->method;
2886
2887	if (s->session != NULL) {
2888		if (!SSL_copy_session_id(ret, s))
2889			goto err;
2890	} else {
2891		/*
2892		 * No session has been established yet, so we have to expect
2893		 * that s->cert or ret->cert will be changed later --
2894		 * they should not both point to the same object,
2895		 * and thus we can't use SSL_copy_session_id.
2896		 */
2897
2898		ret->method->ssl_free(ret);
2899		ret->method = s->method;
2900		ret->method->ssl_new(ret);
2901
2902		ssl_cert_free(ret->cert);
2903		if ((ret->cert = ssl_cert_dup(s->cert)) == NULL)
2904			goto err;
2905
2906		if (!SSL_set_session_id_context(ret, s->sid_ctx,
2907		    s->sid_ctx_length))
2908			goto err;
2909	}
2910
2911	ret->options = s->options;
2912	ret->mode = s->mode;
2913	SSL_set_max_cert_list(ret, SSL_get_max_cert_list(s));
2914	SSL_set_read_ahead(ret, SSL_get_read_ahead(s));
2915	ret->msg_callback = s->msg_callback;
2916	ret->msg_callback_arg = s->msg_callback_arg;
2917	SSL_set_verify(ret, SSL_get_verify_mode(s),
2918	SSL_get_verify_callback(s));
2919	SSL_set_verify_depth(ret, SSL_get_verify_depth(s));
2920	ret->generate_session_id = s->generate_session_id;
2921
2922	SSL_set_info_callback(ret, SSL_get_info_callback(s));
2923
2924	/* copy app data, a little dangerous perhaps */
2925	if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL,
2926	    &ret->ex_data, &s->ex_data))
2927		goto err;
2928
2929	/* setup rbio, and wbio */
2930	if (s->rbio != NULL) {
2931		if (!BIO_dup_state(s->rbio,(char *)&ret->rbio))
2932			goto err;
2933	}
2934	if (s->wbio != NULL) {
2935		if (s->wbio != s->rbio) {
2936			if (!BIO_dup_state(s->wbio,(char *)&ret->wbio))
2937				goto err;
2938		} else
2939			ret->wbio = ret->rbio;
2940	}
2941	ret->rwstate = s->rwstate;
2942	ret->in_handshake = s->in_handshake;
2943	ret->handshake_func = s->handshake_func;
2944	ret->server = s->server;
2945	ret->renegotiate = s->renegotiate;
2946	ret->new_session = s->new_session;
2947	ret->quiet_shutdown = s->quiet_shutdown;
2948	ret->shutdown = s->shutdown;
2949	/* SSL_dup does not really work at any state, though */
2950	ret->s3->hs.state = s->s3->hs.state;
2951	ret->rstate = s->rstate;
2952
2953	/*
2954	 * Would have to copy ret->init_buf, ret->init_msg, ret->init_num,
2955	 * ret->init_off
2956	 */
2957	ret->init_num = 0;
2958
2959	ret->hit = s->hit;
2960
2961	X509_VERIFY_PARAM_inherit(ret->param, s->param);
2962
2963	if (s->cipher_list != NULL) {
2964		if ((ret->cipher_list =
2965		    sk_SSL_CIPHER_dup(s->cipher_list)) == NULL)
2966			goto err;
2967	}
2968	if (s->cipher_list_tls13 != NULL) {
2969		if ((ret->cipher_list_tls13 =
2970		    sk_SSL_CIPHER_dup(s->cipher_list_tls13)) == NULL)
2971			goto err;
2972	}
2973
2974	/* Dup the client_CA list */
2975	if (s->client_CA != NULL) {
2976		if ((sk = sk_X509_NAME_dup(s->client_CA)) == NULL) goto err;
2977			ret->client_CA = sk;
2978		for (i = 0; i < sk_X509_NAME_num(sk); i++) {
2979			xn = sk_X509_NAME_value(sk, i);
2980			if (sk_X509_NAME_set(sk, i,
2981			    X509_NAME_dup(xn)) == NULL) {
2982				X509_NAME_free(xn);
2983				goto err;
2984			}
2985		}
2986	}
2987
2988	return ret;
2989 err:
2990	SSL_free(ret);
2991	return NULL;
2992}
2993LSSL_ALIAS(SSL_dup);
2994
2995void
2996ssl_clear_cipher_state(SSL *s)
2997{
2998	tls12_record_layer_clear_read_state(s->rl);
2999	tls12_record_layer_clear_write_state(s->rl);
3000}
3001
3002void
3003ssl_info_callback(const SSL *s, int type, int value)
3004{
3005	ssl_info_callback_fn *cb;
3006
3007	if ((cb = s->info_callback) == NULL)
3008		cb = s->ctx->info_callback;
3009	if (cb != NULL)
3010		cb(s, type, value);
3011}
3012
3013void
3014ssl_msg_callback(SSL *s, int is_write, int content_type,
3015    const void *msg_buf, size_t msg_len)
3016{
3017	if (s->msg_callback == NULL)
3018		return;
3019
3020	s->msg_callback(is_write, s->version, content_type,
3021	    msg_buf, msg_len, s, s->msg_callback_arg);
3022}
3023
3024void
3025ssl_msg_callback_cbs(SSL *s, int is_write, int content_type, CBS *cbs)
3026{
3027	ssl_msg_callback(s, is_write, content_type, CBS_data(cbs), CBS_len(cbs));
3028}
3029
3030/* Fix this function so that it takes an optional type parameter */
3031X509 *
3032SSL_get_certificate(const SSL *s)
3033{
3034	return (s->cert->key->x509);
3035}
3036LSSL_ALIAS(SSL_get_certificate);
3037
3038/* Fix this function so that it takes an optional type parameter */
3039EVP_PKEY *
3040SSL_get_privatekey(const SSL *s)
3041{
3042	return (s->cert->key->privatekey);
3043}
3044LSSL_ALIAS(SSL_get_privatekey);
3045
3046const SSL_CIPHER *
3047SSL_get_current_cipher(const SSL *s)
3048{
3049	if ((s->session != NULL) && (s->session->cipher != NULL))
3050		return (s->session->cipher);
3051	return (NULL);
3052}
3053LSSL_ALIAS(SSL_get_current_cipher);
3054const void *
3055SSL_get_current_compression(SSL *s)
3056{
3057	return (NULL);
3058}
3059LSSL_ALIAS(SSL_get_current_compression);
3060
3061const void *
3062SSL_get_current_expansion(SSL *s)
3063{
3064	return (NULL);
3065}
3066LSSL_ALIAS(SSL_get_current_expansion);
3067
3068size_t
3069SSL_get_client_random(const SSL *s, unsigned char *out, size_t max_out)
3070{
3071	size_t len = sizeof(s->s3->client_random);
3072
3073	if (out == NULL)
3074		return len;
3075
3076	if (len > max_out)
3077		len = max_out;
3078
3079	memcpy(out, s->s3->client_random, len);
3080
3081	return len;
3082}
3083LSSL_ALIAS(SSL_get_client_random);
3084
3085size_t
3086SSL_get_server_random(const SSL *s, unsigned char *out, size_t max_out)
3087{
3088	size_t len = sizeof(s->s3->server_random);
3089
3090	if (out == NULL)
3091		return len;
3092
3093	if (len > max_out)
3094		len = max_out;
3095
3096	memcpy(out, s->s3->server_random, len);
3097
3098	return len;
3099}
3100LSSL_ALIAS(SSL_get_server_random);
3101
3102int
3103ssl_init_wbio_buffer(SSL *s, int push)
3104{
3105	BIO	*bbio;
3106
3107	if (s->bbio == NULL) {
3108		bbio = BIO_new(BIO_f_buffer());
3109		if (bbio == NULL)
3110			return (0);
3111		s->bbio = bbio;
3112	} else {
3113		bbio = s->bbio;
3114		if (s->bbio == s->wbio)
3115			s->wbio = BIO_pop(s->wbio);
3116	}
3117	(void)BIO_reset(bbio);
3118/*	if (!BIO_set_write_buffer_size(bbio,16*1024)) */
3119	if (!BIO_set_read_buffer_size(bbio, 1)) {
3120		SSLerror(s, ERR_R_BUF_LIB);
3121		return (0);
3122	}
3123	if (push) {
3124		if (s->wbio != bbio)
3125			s->wbio = BIO_push(bbio, s->wbio);
3126	} else {
3127		if (s->wbio == bbio)
3128			s->wbio = BIO_pop(bbio);
3129	}
3130	return (1);
3131}
3132
3133void
3134ssl_free_wbio_buffer(SSL *s)
3135{
3136	if (s == NULL)
3137		return;
3138
3139	if (s->bbio == NULL)
3140		return;
3141
3142	if (s->bbio == s->wbio) {
3143		/* remove buffering */
3144		s->wbio = BIO_pop(s->wbio);
3145	}
3146	BIO_free(s->bbio);
3147	s->bbio = NULL;
3148}
3149
3150void
3151SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode)
3152{
3153	ctx->quiet_shutdown = mode;
3154}
3155LSSL_ALIAS(SSL_CTX_set_quiet_shutdown);
3156
3157int
3158SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
3159{
3160	return (ctx->quiet_shutdown);
3161}
3162LSSL_ALIAS(SSL_CTX_get_quiet_shutdown);
3163
3164void
3165SSL_set_quiet_shutdown(SSL *s, int mode)
3166{
3167	s->quiet_shutdown = mode;
3168}
3169LSSL_ALIAS(SSL_set_quiet_shutdown);
3170
3171int
3172SSL_get_quiet_shutdown(const SSL *s)
3173{
3174	return (s->quiet_shutdown);
3175}
3176LSSL_ALIAS(SSL_get_quiet_shutdown);
3177
3178void
3179SSL_set_shutdown(SSL *s, int mode)
3180{
3181	s->shutdown = mode;
3182}
3183LSSL_ALIAS(SSL_set_shutdown);
3184
3185int
3186SSL_get_shutdown(const SSL *s)
3187{
3188	return (s->shutdown);
3189}
3190LSSL_ALIAS(SSL_get_shutdown);
3191
3192int
3193SSL_version(const SSL *s)
3194{
3195	return (s->version);
3196}
3197LSSL_ALIAS(SSL_version);
3198
3199SSL_CTX *
3200SSL_get_SSL_CTX(const SSL *ssl)
3201{
3202	return (ssl->ctx);
3203}
3204LSSL_ALIAS(SSL_get_SSL_CTX);
3205
3206SSL_CTX *
3207SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
3208{
3209	SSL_CERT *new_cert;
3210
3211	if (ctx == NULL)
3212		ctx = ssl->initial_ctx;
3213	if (ssl->ctx == ctx)
3214		return (ssl->ctx);
3215
3216	if ((new_cert = ssl_cert_dup(ctx->cert)) == NULL)
3217		return NULL;
3218	ssl_cert_free(ssl->cert);
3219	ssl->cert = new_cert;
3220
3221	SSL_CTX_up_ref(ctx);
3222	SSL_CTX_free(ssl->ctx); /* decrement reference count */
3223	ssl->ctx = ctx;
3224
3225	return (ssl->ctx);
3226}
3227LSSL_ALIAS(SSL_set_SSL_CTX);
3228
3229int
3230SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
3231{
3232	return (X509_STORE_set_default_paths(ctx->cert_store));
3233}
3234LSSL_ALIAS(SSL_CTX_set_default_verify_paths);
3235
3236int
3237SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
3238    const char *CApath)
3239{
3240	return (X509_STORE_load_locations(ctx->cert_store, CAfile, CApath));
3241}
3242LSSL_ALIAS(SSL_CTX_load_verify_locations);
3243
3244int
3245SSL_CTX_load_verify_mem(SSL_CTX *ctx, void *buf, int len)
3246{
3247	return (X509_STORE_load_mem(ctx->cert_store, buf, len));
3248}
3249LSSL_ALIAS(SSL_CTX_load_verify_mem);
3250
3251void
3252SSL_set_info_callback(SSL *ssl, void (*cb)(const SSL *ssl, int type, int val))
3253{
3254	ssl->info_callback = cb;
3255}
3256LSSL_ALIAS(SSL_set_info_callback);
3257
3258void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type, int val)
3259{
3260	return (ssl->info_callback);
3261}
3262LSSL_ALIAS(SSL_get_info_callback);
3263
3264int
3265SSL_state(const SSL *ssl)
3266{
3267	return (ssl->s3->hs.state);
3268}
3269LSSL_ALIAS(SSL_state);
3270
3271void
3272SSL_set_state(SSL *ssl, int state)
3273{
3274	ssl->s3->hs.state = state;
3275}
3276LSSL_ALIAS(SSL_set_state);
3277
3278void
3279SSL_set_verify_result(SSL *ssl, long arg)
3280{
3281	ssl->verify_result = arg;
3282}
3283LSSL_ALIAS(SSL_set_verify_result);
3284
3285long
3286SSL_get_verify_result(const SSL *ssl)
3287{
3288	return (ssl->verify_result);
3289}
3290LSSL_ALIAS(SSL_get_verify_result);
3291
3292int
3293SSL_verify_client_post_handshake(SSL *ssl)
3294{
3295	return 0;
3296}
3297LSSL_ALIAS(SSL_verify_client_post_handshake);
3298
3299void
3300SSL_CTX_set_post_handshake_auth(SSL_CTX *ctx, int val)
3301{
3302	return;
3303}
3304LSSL_ALIAS(SSL_CTX_set_post_handshake_auth);
3305
3306void
3307SSL_set_post_handshake_auth(SSL *ssl, int val)
3308{
3309	return;
3310}
3311LSSL_ALIAS(SSL_set_post_handshake_auth);
3312
3313int
3314SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
3315    CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
3316{
3317	return (CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
3318	    new_func, dup_func, free_func));
3319}
3320LSSL_ALIAS(SSL_get_ex_new_index);
3321
3322int
3323SSL_set_ex_data(SSL *s, int idx, void *arg)
3324{
3325	return (CRYPTO_set_ex_data(&s->ex_data, idx, arg));
3326}
3327LSSL_ALIAS(SSL_set_ex_data);
3328
3329void *
3330SSL_get_ex_data(const SSL *s, int idx)
3331{
3332	return (CRYPTO_get_ex_data(&s->ex_data, idx));
3333}
3334LSSL_ALIAS(SSL_get_ex_data);
3335
3336int
3337SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
3338    CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
3339{
3340	return (CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
3341	    new_func, dup_func, free_func));
3342}
3343LSSL_ALIAS(SSL_CTX_get_ex_new_index);
3344
3345int
3346SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg)
3347{
3348	return (CRYPTO_set_ex_data(&s->ex_data, idx, arg));
3349}
3350LSSL_ALIAS(SSL_CTX_set_ex_data);
3351
3352void *
3353SSL_CTX_get_ex_data(const SSL_CTX *s, int idx)
3354{
3355	return (CRYPTO_get_ex_data(&s->ex_data, idx));
3356}
3357LSSL_ALIAS(SSL_CTX_get_ex_data);
3358
3359int
3360ssl_ok(SSL *s)
3361{
3362	return (1);
3363}
3364
3365X509_STORE *
3366SSL_CTX_get_cert_store(const SSL_CTX *ctx)
3367{
3368	return (ctx->cert_store);
3369}
3370LSSL_ALIAS(SSL_CTX_get_cert_store);
3371
3372void
3373SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store)
3374{
3375	X509_STORE_free(ctx->cert_store);
3376	ctx->cert_store = store;
3377}
3378LSSL_ALIAS(SSL_CTX_set_cert_store);
3379
3380X509 *
3381SSL_CTX_get0_certificate(const SSL_CTX *ctx)
3382{
3383	if (ctx->cert == NULL)
3384		return NULL;
3385
3386	return ctx->cert->key->x509;
3387}
3388LSSL_ALIAS(SSL_CTX_get0_certificate);
3389
3390EVP_PKEY *
3391SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
3392{
3393	if (ctx->cert == NULL)
3394		return NULL;
3395
3396	return ctx->cert->key->privatekey;
3397}
3398LSSL_ALIAS(SSL_CTX_get0_privatekey);
3399
3400int
3401SSL_want(const SSL *s)
3402{
3403	return (s->rwstate);
3404}
3405LSSL_ALIAS(SSL_want);
3406
3407void
3408SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx, RSA *(*cb)(SSL *ssl, int is_export,
3409    int keylength))
3410{
3411	SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
3412}
3413LSSL_ALIAS(SSL_CTX_set_tmp_rsa_callback);
3414
3415void
3416SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
3417    int keylength))
3418{
3419	SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
3420}
3421LSSL_ALIAS(SSL_set_tmp_rsa_callback);
3422
3423void
3424SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx, DH *(*dh)(SSL *ssl, int is_export,
3425    int keylength))
3426{
3427	SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
3428}
3429LSSL_ALIAS(SSL_CTX_set_tmp_dh_callback);
3430
3431void
3432SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh)(SSL *ssl, int is_export,
3433    int keylength))
3434{
3435	SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
3436}
3437LSSL_ALIAS(SSL_set_tmp_dh_callback);
3438
3439void
3440SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx, EC_KEY *(*ecdh)(SSL *ssl,
3441    int is_export, int keylength))
3442{
3443	SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB,
3444	    (void (*)(void))ecdh);
3445}
3446LSSL_ALIAS(SSL_CTX_set_tmp_ecdh_callback);
3447
3448void
3449SSL_set_tmp_ecdh_callback(SSL *ssl, EC_KEY *(*ecdh)(SSL *ssl, int is_export,
3450    int keylength))
3451{
3452	SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
3453}
3454LSSL_ALIAS(SSL_set_tmp_ecdh_callback);
3455
3456
3457void
3458SSL_CTX_set_msg_callback(SSL_CTX *ctx, void (*cb)(int write_p, int version,
3459    int content_type, const void *buf, size_t len, SSL *ssl, void *arg))
3460{
3461	SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK,
3462	    (void (*)(void))cb);
3463}
3464LSSL_ALIAS(SSL_CTX_set_msg_callback);
3465
3466void
3467SSL_set_msg_callback(SSL *ssl, void (*cb)(int write_p, int version,
3468    int content_type, const void *buf, size_t len, SSL *ssl, void *arg))
3469{
3470	SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3471}
3472LSSL_ALIAS(SSL_set_msg_callback);
3473
3474int
3475SSL_cache_hit(SSL *s)
3476{
3477	return (s->hit);
3478}
3479LSSL_ALIAS(SSL_cache_hit);
3480
3481int
3482SSL_CTX_get_min_proto_version(SSL_CTX *ctx)
3483{
3484	return ctx->min_proto_version;
3485}
3486LSSL_ALIAS(SSL_CTX_get_min_proto_version);
3487
3488int
3489SSL_CTX_set_min_proto_version(SSL_CTX *ctx, uint16_t version)
3490{
3491	return ssl_version_set_min(ctx->method, version,
3492	    ctx->max_tls_version, &ctx->min_tls_version,
3493	    &ctx->min_proto_version);
3494}
3495LSSL_ALIAS(SSL_CTX_set_min_proto_version);
3496
3497int
3498SSL_CTX_get_max_proto_version(SSL_CTX *ctx)
3499{
3500	return ctx->max_proto_version;
3501}
3502LSSL_ALIAS(SSL_CTX_get_max_proto_version);
3503
3504int
3505SSL_CTX_set_max_proto_version(SSL_CTX *ctx, uint16_t version)
3506{
3507	return ssl_version_set_max(ctx->method, version,
3508	    ctx->min_tls_version, &ctx->max_tls_version,
3509	    &ctx->max_proto_version);
3510}
3511LSSL_ALIAS(SSL_CTX_set_max_proto_version);
3512
3513int
3514SSL_get_min_proto_version(SSL *ssl)
3515{
3516	return ssl->min_proto_version;
3517}
3518LSSL_ALIAS(SSL_get_min_proto_version);
3519
3520int
3521SSL_set_min_proto_version(SSL *ssl, uint16_t version)
3522{
3523	return ssl_version_set_min(ssl->method, version,
3524	    ssl->max_tls_version, &ssl->min_tls_version,
3525	    &ssl->min_proto_version);
3526}
3527LSSL_ALIAS(SSL_set_min_proto_version);
3528int
3529SSL_get_max_proto_version(SSL *ssl)
3530{
3531	return ssl->max_proto_version;
3532}
3533LSSL_ALIAS(SSL_get_max_proto_version);
3534
3535int
3536SSL_set_max_proto_version(SSL *ssl, uint16_t version)
3537{
3538	return ssl_version_set_max(ssl->method, version,
3539	    ssl->min_tls_version, &ssl->max_tls_version,
3540	    &ssl->max_proto_version);
3541}
3542LSSL_ALIAS(SSL_set_max_proto_version);
3543
3544const SSL_METHOD *
3545SSL_CTX_get_ssl_method(const SSL_CTX *ctx)
3546{
3547	return ctx->method;
3548}
3549LSSL_ALIAS(SSL_CTX_get_ssl_method);
3550
3551int
3552SSL_CTX_get_security_level(const SSL_CTX *ctx)
3553{
3554	return ctx->cert->security_level;
3555}
3556LSSL_ALIAS(SSL_CTX_get_security_level);
3557
3558void
3559SSL_CTX_set_security_level(SSL_CTX *ctx, int level)
3560{
3561	ctx->cert->security_level = level;
3562}
3563LSSL_ALIAS(SSL_CTX_set_security_level);
3564
3565int
3566SSL_get_security_level(const SSL *ssl)
3567{
3568	return ssl->cert->security_level;
3569}
3570LSSL_ALIAS(SSL_get_security_level);
3571
3572void
3573SSL_set_security_level(SSL *ssl, int level)
3574{
3575	ssl->cert->security_level = level;
3576}
3577LSSL_ALIAS(SSL_set_security_level);
3578
3579int
3580SSL_is_quic(const SSL *ssl)
3581{
3582	return ssl->quic_method != NULL;
3583}
3584LSSL_ALIAS(SSL_is_quic);
3585
3586int
3587SSL_set_quic_transport_params(SSL *ssl, const uint8_t *params,
3588    size_t params_len)
3589{
3590	freezero(ssl->quic_transport_params,
3591	    ssl->quic_transport_params_len);
3592	ssl->quic_transport_params = NULL;
3593	ssl->quic_transport_params_len = 0;
3594
3595	if ((ssl->quic_transport_params = malloc(params_len)) == NULL)
3596		return 0;
3597
3598	memcpy(ssl->quic_transport_params, params, params_len);
3599	ssl->quic_transport_params_len = params_len;
3600
3601	return 1;
3602}
3603LSSL_ALIAS(SSL_set_quic_transport_params);
3604
3605void
3606SSL_get_peer_quic_transport_params(const SSL *ssl, const uint8_t **out_params,
3607    size_t *out_params_len)
3608{
3609	*out_params = ssl->s3->peer_quic_transport_params;
3610	*out_params_len = ssl->s3->peer_quic_transport_params_len;
3611}
3612LSSL_ALIAS(SSL_get_peer_quic_transport_params);
3613
3614void
3615SSL_set_quic_use_legacy_codepoint(SSL *ssl, int use_legacy)
3616{
3617	/* Not supported. */
3618}
3619LSSL_ALIAS(SSL_set_quic_use_legacy_codepoint);
3620