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