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