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