xref: /openbsd-src/lib/libssl/ssl_lib.c (revision 68dd5bb1859285b71cb62a10bf107b8ad54064d9)
1 /* $OpenBSD: ssl_lib.c,v 1.318 2023/12/29 12:24:33 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()
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_GOST01]);
2301 	if (cpk->x509 != NULL && cpk->privatekey != NULL) {
2302 		mask_k |= SSL_kGOST;
2303 		mask_a |= SSL_aGOST01;
2304 	}
2305 
2306 	cpk = &(c->pkeys[SSL_PKEY_RSA]);
2307 	if (cpk->x509 != NULL && cpk->privatekey != NULL) {
2308 		mask_a |= SSL_aRSA;
2309 		mask_k |= SSL_kRSA;
2310 	}
2311 
2312 	c->mask_k = mask_k;
2313 	c->mask_a = mask_a;
2314 	c->valid = 1;
2315 }
2316 
2317 /* See if this handshake is using an ECC cipher suite. */
2318 int
2319 ssl_using_ecc_cipher(SSL *s)
2320 {
2321 	unsigned long alg_a, alg_k;
2322 
2323 	alg_a = s->s3->hs.cipher->algorithm_auth;
2324 	alg_k = s->s3->hs.cipher->algorithm_mkey;
2325 
2326 	return s->session->tlsext_ecpointformatlist != NULL &&
2327 	    s->session->tlsext_ecpointformatlist_length > 0 &&
2328 	    ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA));
2329 }
2330 
2331 int
2332 ssl_check_srvr_ecc_cert_and_alg(SSL *s, X509 *x)
2333 {
2334 	const SSL_CIPHER *cs = s->s3->hs.cipher;
2335 	unsigned long alg_a;
2336 
2337 	alg_a = cs->algorithm_auth;
2338 
2339 	if (alg_a & SSL_aECDSA) {
2340 		/* Key usage, if present, must allow signing. */
2341 		if (!(X509_get_key_usage(x) & X509v3_KU_DIGITAL_SIGNATURE)) {
2342 			SSLerror(s, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2343 			return (0);
2344 		}
2345 	}
2346 
2347 	return (1);
2348 }
2349 
2350 SSL_CERT_PKEY *
2351 ssl_get_server_send_pkey(const SSL *s)
2352 {
2353 	unsigned long alg_a;
2354 	SSL_CERT *c;
2355 	int i;
2356 
2357 	c = s->cert;
2358 	ssl_set_cert_masks(c, s->s3->hs.cipher);
2359 
2360 	alg_a = s->s3->hs.cipher->algorithm_auth;
2361 
2362 	if (alg_a & SSL_aECDSA) {
2363 		i = SSL_PKEY_ECC;
2364 	} else if (alg_a & SSL_aRSA) {
2365 		i = SSL_PKEY_RSA;
2366 	} else if (alg_a & SSL_aGOST01) {
2367 		i = SSL_PKEY_GOST01;
2368 	} else { /* if (alg_a & SSL_aNULL) */
2369 		SSLerror(s, ERR_R_INTERNAL_ERROR);
2370 		return (NULL);
2371 	}
2372 
2373 	return (c->pkeys + i);
2374 }
2375 
2376 EVP_PKEY *
2377 ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher, const EVP_MD **pmd,
2378     const struct ssl_sigalg **sap)
2379 {
2380 	const struct ssl_sigalg *sigalg = NULL;
2381 	EVP_PKEY *pkey = NULL;
2382 	unsigned long alg_a;
2383 	SSL_CERT *c;
2384 	int idx = -1;
2385 
2386 	alg_a = cipher->algorithm_auth;
2387 	c = s->cert;
2388 
2389 	if (alg_a & SSL_aRSA) {
2390 		idx = SSL_PKEY_RSA;
2391 	} else if ((alg_a & SSL_aECDSA) &&
2392 	    (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2393 		idx = SSL_PKEY_ECC;
2394 	if (idx == -1) {
2395 		SSLerror(s, ERR_R_INTERNAL_ERROR);
2396 		return (NULL);
2397 	}
2398 
2399 	pkey = c->pkeys[idx].privatekey;
2400 	if ((sigalg = ssl_sigalg_select(s, pkey)) == NULL) {
2401 		SSLerror(s, SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2402 		return (NULL);
2403 	}
2404 	*pmd = sigalg->md();
2405 	*sap = sigalg;
2406 
2407 	return (pkey);
2408 }
2409 
2410 size_t
2411 ssl_dhe_params_auto_key_bits(SSL *s)
2412 {
2413 	SSL_CERT_PKEY *cpk;
2414 	int key_bits;
2415 
2416 	if (s->cert->dhe_params_auto == 2) {
2417 		key_bits = 1024;
2418 	} else if (s->s3->hs.cipher->algorithm_auth & SSL_aNULL) {
2419 		key_bits = 1024;
2420 		if (s->s3->hs.cipher->strength_bits == 256)
2421 			key_bits = 3072;
2422 	} else {
2423 		if ((cpk = ssl_get_server_send_pkey(s)) == NULL)
2424 			return 0;
2425 		if (cpk->privatekey == NULL ||
2426 		    EVP_PKEY_get0_RSA(cpk->privatekey) == NULL)
2427 			return 0;
2428 		if ((key_bits = EVP_PKEY_bits(cpk->privatekey)) <= 0)
2429 			return 0;
2430 	}
2431 
2432 	return key_bits;
2433 }
2434 
2435 static int
2436 ssl_should_update_external_cache(SSL *s, int mode)
2437 {
2438 	int cache_mode;
2439 
2440 	cache_mode = s->session_ctx->session_cache_mode;
2441 
2442 	/* Don't cache if mode says not to */
2443 	if ((cache_mode & mode) == 0)
2444 		return 0;
2445 
2446 	/* if it is not already cached, cache it */
2447 	if (!s->hit)
2448 		return 1;
2449 
2450 	/* If it's TLS 1.3, do it to match OpenSSL */
2451 	if (s->s3->hs.negotiated_tls_version >= TLS1_3_VERSION)
2452 		return 1;
2453 
2454 	return 0;
2455 }
2456 
2457 static int
2458 ssl_should_update_internal_cache(SSL *s, int mode)
2459 {
2460 	int cache_mode;
2461 
2462 	cache_mode = s->session_ctx->session_cache_mode;
2463 
2464 	/* Don't cache if mode says not to */
2465 	if ((cache_mode & mode) == 0)
2466 		return 0;
2467 
2468 	/* If it is already cached, don't cache it again */
2469 	if (s->hit)
2470 		return 0;
2471 
2472 	if ((cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) != 0)
2473 		return 0;
2474 
2475 	/* If we are lesser than TLS 1.3, Cache it. */
2476 	if (s->s3->hs.negotiated_tls_version < TLS1_3_VERSION)
2477 		return 1;
2478 
2479 	/* Below this we consider TLS 1.3 or later */
2480 
2481 	/* If it's not a server, add it? OpenSSL does this. */
2482 	if (!s->server)
2483 		return 1;
2484 
2485 	/* XXX if we support early data / PSK need to add */
2486 
2487 	/*
2488 	 * If we have the remove session callback, we will want
2489 	 * to know about this even if it's a stateless ticket
2490 	 * from 1.3 so we can know when it is removed.
2491 	 */
2492 	if (s->session_ctx->remove_session_cb != NULL)
2493 		return 1;
2494 
2495 	/* If we have set OP_NO_TICKET, cache it. */
2496 	if ((s->options & SSL_OP_NO_TICKET) != 0)
2497 		return 1;
2498 
2499 	/* Otherwise do not cache */
2500 	return 0;
2501 }
2502 
2503 void
2504 ssl_update_cache(SSL *s, int mode)
2505 {
2506 	int cache_mode, do_callback;
2507 
2508 	if (s->session->session_id_length == 0)
2509 		return;
2510 
2511 	cache_mode = s->session_ctx->session_cache_mode;
2512 	do_callback = ssl_should_update_external_cache(s, mode);
2513 
2514 	if (ssl_should_update_internal_cache(s, mode)) {
2515 		/*
2516 		 * XXX should we fail if the add to the internal cache
2517 		 * fails? OpenSSL doesn't care..
2518 		 */
2519 		(void) SSL_CTX_add_session(s->session_ctx, s->session);
2520 	}
2521 
2522 	/*
2523 	 * Update the "external cache" by calling the new session
2524 	 * callback if present, even with TLS 1.3 without early data
2525 	 * "because some application just want to know about the
2526 	 * creation of a session and aren't doing a full cache".
2527 	 * Apparently, if they are doing a full cache, they'll have
2528 	 * some fun, but we endeavour to give application writers the
2529 	 * same glorious experience they expect from OpenSSL which
2530 	 * does it this way.
2531 	 */
2532 	if (do_callback && s->session_ctx->new_session_cb != NULL) {
2533 		    CRYPTO_add(&s->session->references, 1, CRYPTO_LOCK_SSL_SESSION);
2534 		    if (!s->session_ctx->new_session_cb(s, s->session))
2535 			    SSL_SESSION_free(s->session);
2536 	}
2537 
2538 	/* Auto flush every 255 connections. */
2539 	if (!(cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
2540 	    (cache_mode & mode) != 0) {
2541 		int connections;
2542 		if (mode & SSL_SESS_CACHE_CLIENT)
2543 			connections = s->session_ctx->stats.sess_connect_good;
2544 		else
2545 			connections = s->session_ctx->stats.sess_accept_good;
2546 		if ((connections & 0xff) == 0xff)
2547 			SSL_CTX_flush_sessions(s->session_ctx, time(NULL));
2548 	}
2549 }
2550 
2551 const SSL_METHOD *
2552 SSL_get_ssl_method(SSL *s)
2553 {
2554 	return (s->method);
2555 }
2556 LSSL_ALIAS(SSL_get_ssl_method);
2557 
2558 int
2559 SSL_set_ssl_method(SSL *s, const SSL_METHOD *method)
2560 {
2561 	int (*handshake_func)(SSL *) = NULL;
2562 	int ret = 1;
2563 
2564 	if (s->method == method)
2565 		return (ret);
2566 
2567 	if (s->handshake_func == s->method->ssl_connect)
2568 		handshake_func = method->ssl_connect;
2569 	else if (s->handshake_func == s->method->ssl_accept)
2570 		handshake_func = method->ssl_accept;
2571 
2572 	if (s->method->version == method->version) {
2573 		s->method = method;
2574 	} else {
2575 		s->method->ssl_free(s);
2576 		s->method = method;
2577 		ret = s->method->ssl_new(s);
2578 	}
2579 	s->handshake_func = handshake_func;
2580 
2581 	return (ret);
2582 }
2583 LSSL_ALIAS(SSL_set_ssl_method);
2584 
2585 int
2586 SSL_get_error(const SSL *s, int i)
2587 {
2588 	unsigned long l;
2589 	int reason;
2590 	BIO *bio;
2591 
2592 	if (i > 0)
2593 		return (SSL_ERROR_NONE);
2594 
2595 	/*
2596 	 * Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2597 	 * etc, where we do encode the error.
2598 	 */
2599 	if ((l = ERR_peek_error()) != 0) {
2600 		if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2601 			return (SSL_ERROR_SYSCALL);
2602 		else
2603 			return (SSL_ERROR_SSL);
2604 	}
2605 
2606 	if (SSL_want_read(s)) {
2607 		bio = SSL_get_rbio(s);
2608 		if (BIO_should_read(bio)) {
2609 			return (SSL_ERROR_WANT_READ);
2610 		} else if (BIO_should_write(bio)) {
2611 			/*
2612 			 * This one doesn't make too much sense...  We never
2613 			 * try to write to the rbio, and an application
2614 			 * program where rbio and wbio are separate couldn't
2615 			 * even know what it should wait for.  However if we
2616 			 * ever set s->rwstate incorrectly (so that we have
2617 			 * SSL_want_read(s) instead of SSL_want_write(s))
2618 			 * and rbio and wbio *are* the same, this test works
2619 			 * around that bug; so it might be safer to keep it.
2620 			 */
2621 			return (SSL_ERROR_WANT_WRITE);
2622 		} else if (BIO_should_io_special(bio)) {
2623 			reason = BIO_get_retry_reason(bio);
2624 			if (reason == BIO_RR_CONNECT)
2625 				return (SSL_ERROR_WANT_CONNECT);
2626 			else if (reason == BIO_RR_ACCEPT)
2627 				return (SSL_ERROR_WANT_ACCEPT);
2628 			else
2629 				return (SSL_ERROR_SYSCALL); /* unknown */
2630 		}
2631 	}
2632 
2633 	if (SSL_want_write(s)) {
2634 		bio = SSL_get_wbio(s);
2635 		if (BIO_should_write(bio)) {
2636 			return (SSL_ERROR_WANT_WRITE);
2637 		} else if (BIO_should_read(bio)) {
2638 			/*
2639 			 * See above (SSL_want_read(s) with
2640 			 * BIO_should_write(bio))
2641 			 */
2642 			return (SSL_ERROR_WANT_READ);
2643 		} else if (BIO_should_io_special(bio)) {
2644 			reason = BIO_get_retry_reason(bio);
2645 			if (reason == BIO_RR_CONNECT)
2646 				return (SSL_ERROR_WANT_CONNECT);
2647 			else if (reason == BIO_RR_ACCEPT)
2648 				return (SSL_ERROR_WANT_ACCEPT);
2649 			else
2650 				return (SSL_ERROR_SYSCALL);
2651 		}
2652 	}
2653 
2654 	if (SSL_want_x509_lookup(s))
2655 		return (SSL_ERROR_WANT_X509_LOOKUP);
2656 
2657 	if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2658 	    (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
2659 		return (SSL_ERROR_ZERO_RETURN);
2660 
2661 	return (SSL_ERROR_SYSCALL);
2662 }
2663 LSSL_ALIAS(SSL_get_error);
2664 
2665 int
2666 SSL_CTX_set_quic_method(SSL_CTX *ctx, const SSL_QUIC_METHOD *quic_method)
2667 {
2668 	if (ctx->method->dtls)
2669 		return 0;
2670 
2671 	ctx->quic_method = quic_method;
2672 
2673 	return 1;
2674 }
2675 LSSL_ALIAS(SSL_CTX_set_quic_method);
2676 
2677 int
2678 SSL_set_quic_method(SSL *ssl, const SSL_QUIC_METHOD *quic_method)
2679 {
2680 	if (ssl->method->dtls)
2681 		return 0;
2682 
2683 	ssl->quic_method = quic_method;
2684 
2685 	return 1;
2686 }
2687 LSSL_ALIAS(SSL_set_quic_method);
2688 
2689 size_t
2690 SSL_quic_max_handshake_flight_len(const SSL *ssl,
2691     enum ssl_encryption_level_t level)
2692 {
2693 	size_t flight_len;
2694 
2695 	/* Limit flights to 16K when there are no large certificate messages. */
2696 	flight_len = 16384;
2697 
2698 	switch (level) {
2699 	case ssl_encryption_initial:
2700 		return flight_len;
2701 
2702 	case ssl_encryption_early_data:
2703 		/* QUIC does not send EndOfEarlyData. */
2704 		return 0;
2705 
2706 	case ssl_encryption_handshake:
2707 		if (ssl->server) {
2708 			/*
2709 			 * Servers may receive Certificate message if configured
2710 			 * to request client certificates.
2711 			 */
2712 			if ((SSL_get_verify_mode(ssl) & SSL_VERIFY_PEER) != 0 &&
2713 			    ssl->max_cert_list > flight_len)
2714 				flight_len = ssl->max_cert_list;
2715 		} else {
2716 			/*
2717 			 * Clients may receive both Certificate message and a
2718 			 * CertificateRequest message.
2719 			 */
2720 			if (ssl->max_cert_list * 2 > flight_len)
2721 				flight_len = ssl->max_cert_list * 2;
2722 		}
2723 		return flight_len;
2724 	case ssl_encryption_application:
2725 		/*
2726 		 * Note there is not actually a bound on the number of
2727 		 * NewSessionTickets one may send in a row. This level may need
2728 		 * more involved flow control.
2729 		 */
2730 		return flight_len;
2731 	}
2732 
2733 	return 0;
2734 }
2735 LSSL_ALIAS(SSL_quic_max_handshake_flight_len);
2736 
2737 enum ssl_encryption_level_t
2738 SSL_quic_read_level(const SSL *ssl)
2739 {
2740 	return ssl->s3->hs.tls13.quic_read_level;
2741 }
2742 LSSL_ALIAS(SSL_quic_read_level);
2743 
2744 enum ssl_encryption_level_t
2745 SSL_quic_write_level(const SSL *ssl)
2746 {
2747 	return ssl->s3->hs.tls13.quic_write_level;
2748 }
2749 LSSL_ALIAS(SSL_quic_write_level);
2750 
2751 int
2752 SSL_provide_quic_data(SSL *ssl, enum ssl_encryption_level_t level,
2753     const uint8_t *data, size_t len)
2754 {
2755 	if (!SSL_is_quic(ssl)) {
2756 		SSLerror(ssl, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2757 		return 0;
2758 	}
2759 
2760 	if (level != SSL_quic_read_level(ssl)) {
2761 		SSLerror(ssl, SSL_R_WRONG_ENCRYPTION_LEVEL_RECEIVED);
2762 		return 0;
2763 	}
2764 
2765 	if (ssl->s3->hs.tls13.quic_read_buffer == NULL) {
2766 		ssl->s3->hs.tls13.quic_read_buffer = tls_buffer_new(0);
2767 		if (ssl->s3->hs.tls13.quic_read_buffer == NULL) {
2768 			SSLerror(ssl, ERR_R_MALLOC_FAILURE);
2769 			return 0;
2770 		}
2771 	}
2772 
2773 	/* XXX - note that this does not currently downsize. */
2774 	tls_buffer_set_capacity_limit(ssl->s3->hs.tls13.quic_read_buffer,
2775 	    SSL_quic_max_handshake_flight_len(ssl, level));
2776 
2777 	/*
2778 	 * XXX - an append that fails due to exceeding capacity should set
2779 	 * SSL_R_EXCESSIVE_MESSAGE_SIZE.
2780 	 */
2781 	return tls_buffer_append(ssl->s3->hs.tls13.quic_read_buffer, data, len);
2782 }
2783 LSSL_ALIAS(SSL_provide_quic_data);
2784 
2785 int
2786 SSL_process_quic_post_handshake(SSL *ssl)
2787 {
2788 	/* XXX - this needs to run PHH received. */
2789 	return 1;
2790 }
2791 LSSL_ALIAS(SSL_process_quic_post_handshake);
2792 
2793 int
2794 SSL_do_handshake(SSL *s)
2795 {
2796 	if (s->handshake_func == NULL) {
2797 		SSLerror(s, SSL_R_CONNECTION_TYPE_NOT_SET);
2798 		return (-1);
2799 	}
2800 
2801 	s->method->ssl_renegotiate_check(s);
2802 
2803 	if (!SSL_in_init(s) && !SSL_in_before(s))
2804 		return 1;
2805 
2806 	return s->handshake_func(s);
2807 }
2808 LSSL_ALIAS(SSL_do_handshake);
2809 
2810 /*
2811  * For the next 2 functions, SSL_clear() sets shutdown and so
2812  * one of these calls will reset it
2813  */
2814 void
2815 SSL_set_accept_state(SSL *s)
2816 {
2817 	s->server = 1;
2818 	s->shutdown = 0;
2819 	s->s3->hs.state = SSL_ST_ACCEPT|SSL_ST_BEFORE;
2820 	s->handshake_func = s->method->ssl_accept;
2821 	ssl_clear_cipher_state(s);
2822 }
2823 LSSL_ALIAS(SSL_set_accept_state);
2824 
2825 void
2826 SSL_set_connect_state(SSL *s)
2827 {
2828 	s->server = 0;
2829 	s->shutdown = 0;
2830 	s->s3->hs.state = SSL_ST_CONNECT|SSL_ST_BEFORE;
2831 	s->handshake_func = s->method->ssl_connect;
2832 	ssl_clear_cipher_state(s);
2833 }
2834 LSSL_ALIAS(SSL_set_connect_state);
2835 
2836 int
2837 ssl_undefined_function(SSL *s)
2838 {
2839 	SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2840 	return (0);
2841 }
2842 
2843 int
2844 ssl_undefined_void_function(void)
2845 {
2846 	SSLerrorx(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2847 	return (0);
2848 }
2849 
2850 int
2851 ssl_undefined_const_function(const SSL *s)
2852 {
2853 	SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2854 	return (0);
2855 }
2856 
2857 const char *
2858 ssl_version_string(int ver)
2859 {
2860 	switch (ver) {
2861 	case TLS1_VERSION:
2862 		return (SSL_TXT_TLSV1);
2863 	case TLS1_1_VERSION:
2864 		return (SSL_TXT_TLSV1_1);
2865 	case TLS1_2_VERSION:
2866 		return (SSL_TXT_TLSV1_2);
2867 	case TLS1_3_VERSION:
2868 		return (SSL_TXT_TLSV1_3);
2869 	case DTLS1_VERSION:
2870 		return (SSL_TXT_DTLS1);
2871 	case DTLS1_2_VERSION:
2872 		return (SSL_TXT_DTLS1_2);
2873 	default:
2874 		return ("unknown");
2875 	}
2876 }
2877 
2878 const char *
2879 SSL_get_version(const SSL *s)
2880 {
2881 	return ssl_version_string(s->version);
2882 }
2883 LSSL_ALIAS(SSL_get_version);
2884 
2885 SSL *
2886 SSL_dup(SSL *s)
2887 {
2888 	STACK_OF(X509_NAME) *sk;
2889 	X509_NAME *xn;
2890 	SSL *ret;
2891 	int i;
2892 
2893 	if ((ret = SSL_new(SSL_get_SSL_CTX(s))) == NULL)
2894 		goto err;
2895 
2896 	ret->version = s->version;
2897 	ret->method = s->method;
2898 
2899 	if (s->session != NULL) {
2900 		if (!SSL_copy_session_id(ret, s))
2901 			goto err;
2902 	} else {
2903 		/*
2904 		 * No session has been established yet, so we have to expect
2905 		 * that s->cert or ret->cert will be changed later --
2906 		 * they should not both point to the same object,
2907 		 * and thus we can't use SSL_copy_session_id.
2908 		 */
2909 
2910 		ret->method->ssl_free(ret);
2911 		ret->method = s->method;
2912 		ret->method->ssl_new(ret);
2913 
2914 		ssl_cert_free(ret->cert);
2915 		if ((ret->cert = ssl_cert_dup(s->cert)) == NULL)
2916 			goto err;
2917 
2918 		if (!SSL_set_session_id_context(ret, s->sid_ctx,
2919 		    s->sid_ctx_length))
2920 			goto err;
2921 	}
2922 
2923 	ret->options = s->options;
2924 	ret->mode = s->mode;
2925 	SSL_set_max_cert_list(ret, SSL_get_max_cert_list(s));
2926 	SSL_set_read_ahead(ret, SSL_get_read_ahead(s));
2927 	ret->msg_callback = s->msg_callback;
2928 	ret->msg_callback_arg = s->msg_callback_arg;
2929 	SSL_set_verify(ret, SSL_get_verify_mode(s),
2930 	SSL_get_verify_callback(s));
2931 	SSL_set_verify_depth(ret, SSL_get_verify_depth(s));
2932 	ret->generate_session_id = s->generate_session_id;
2933 
2934 	SSL_set_info_callback(ret, SSL_get_info_callback(s));
2935 
2936 	/* copy app data, a little dangerous perhaps */
2937 	if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL,
2938 	    &ret->ex_data, &s->ex_data))
2939 		goto err;
2940 
2941 	/* setup rbio, and wbio */
2942 	if (s->rbio != NULL) {
2943 		if (!BIO_dup_state(s->rbio,(char *)&ret->rbio))
2944 			goto err;
2945 	}
2946 	if (s->wbio != NULL) {
2947 		if (s->wbio != s->rbio) {
2948 			if (!BIO_dup_state(s->wbio,(char *)&ret->wbio))
2949 				goto err;
2950 		} else
2951 			ret->wbio = ret->rbio;
2952 	}
2953 	ret->rwstate = s->rwstate;
2954 	ret->in_handshake = s->in_handshake;
2955 	ret->handshake_func = s->handshake_func;
2956 	ret->server = s->server;
2957 	ret->renegotiate = s->renegotiate;
2958 	ret->new_session = s->new_session;
2959 	ret->quiet_shutdown = s->quiet_shutdown;
2960 	ret->shutdown = s->shutdown;
2961 	/* SSL_dup does not really work at any state, though */
2962 	ret->s3->hs.state = s->s3->hs.state;
2963 	ret->rstate = s->rstate;
2964 
2965 	/*
2966 	 * Would have to copy ret->init_buf, ret->init_msg, ret->init_num,
2967 	 * ret->init_off
2968 	 */
2969 	ret->init_num = 0;
2970 
2971 	ret->hit = s->hit;
2972 
2973 	X509_VERIFY_PARAM_inherit(ret->param, s->param);
2974 
2975 	if (s->cipher_list != NULL) {
2976 		if ((ret->cipher_list =
2977 		    sk_SSL_CIPHER_dup(s->cipher_list)) == NULL)
2978 			goto err;
2979 	}
2980 	if (s->cipher_list_tls13 != NULL) {
2981 		if ((ret->cipher_list_tls13 =
2982 		    sk_SSL_CIPHER_dup(s->cipher_list_tls13)) == NULL)
2983 			goto err;
2984 	}
2985 
2986 	/* Dup the client_CA list */
2987 	if (s->client_CA != NULL) {
2988 		if ((sk = sk_X509_NAME_dup(s->client_CA)) == NULL) goto err;
2989 			ret->client_CA = sk;
2990 		for (i = 0; i < sk_X509_NAME_num(sk); i++) {
2991 			xn = sk_X509_NAME_value(sk, i);
2992 			if (sk_X509_NAME_set(sk, i,
2993 			    X509_NAME_dup(xn)) == NULL) {
2994 				X509_NAME_free(xn);
2995 				goto err;
2996 			}
2997 		}
2998 	}
2999 
3000 	return ret;
3001  err:
3002 	SSL_free(ret);
3003 	return NULL;
3004 }
3005 LSSL_ALIAS(SSL_dup);
3006 
3007 void
3008 ssl_clear_cipher_state(SSL *s)
3009 {
3010 	tls12_record_layer_clear_read_state(s->rl);
3011 	tls12_record_layer_clear_write_state(s->rl);
3012 }
3013 
3014 void
3015 ssl_info_callback(const SSL *s, int type, int value)
3016 {
3017 	ssl_info_callback_fn *cb;
3018 
3019 	if ((cb = s->info_callback) == NULL)
3020 		cb = s->ctx->info_callback;
3021 	if (cb != NULL)
3022 		cb(s, type, value);
3023 }
3024 
3025 void
3026 ssl_msg_callback(SSL *s, int is_write, int content_type,
3027     const void *msg_buf, size_t msg_len)
3028 {
3029 	if (s->msg_callback == NULL)
3030 		return;
3031 
3032 	s->msg_callback(is_write, s->version, content_type,
3033 	    msg_buf, msg_len, s, s->msg_callback_arg);
3034 }
3035 
3036 void
3037 ssl_msg_callback_cbs(SSL *s, int is_write, int content_type, CBS *cbs)
3038 {
3039 	ssl_msg_callback(s, is_write, content_type, CBS_data(cbs), CBS_len(cbs));
3040 }
3041 
3042 /* Fix this function so that it takes an optional type parameter */
3043 X509 *
3044 SSL_get_certificate(const SSL *s)
3045 {
3046 	return (s->cert->key->x509);
3047 }
3048 LSSL_ALIAS(SSL_get_certificate);
3049 
3050 /* Fix this function so that it takes an optional type parameter */
3051 EVP_PKEY *
3052 SSL_get_privatekey(const SSL *s)
3053 {
3054 	return (s->cert->key->privatekey);
3055 }
3056 LSSL_ALIAS(SSL_get_privatekey);
3057 
3058 const SSL_CIPHER *
3059 SSL_get_current_cipher(const SSL *s)
3060 {
3061 	if ((s->session != NULL) && (s->session->cipher != NULL))
3062 		return (s->session->cipher);
3063 	return (NULL);
3064 }
3065 LSSL_ALIAS(SSL_get_current_cipher);
3066 const void *
3067 SSL_get_current_compression(SSL *s)
3068 {
3069 	return (NULL);
3070 }
3071 LSSL_ALIAS(SSL_get_current_compression);
3072 
3073 const void *
3074 SSL_get_current_expansion(SSL *s)
3075 {
3076 	return (NULL);
3077 }
3078 LSSL_ALIAS(SSL_get_current_expansion);
3079 
3080 size_t
3081 SSL_get_client_random(const SSL *s, unsigned char *out, size_t max_out)
3082 {
3083 	size_t len = sizeof(s->s3->client_random);
3084 
3085 	if (out == NULL)
3086 		return len;
3087 
3088 	if (len > max_out)
3089 		len = max_out;
3090 
3091 	memcpy(out, s->s3->client_random, len);
3092 
3093 	return len;
3094 }
3095 LSSL_ALIAS(SSL_get_client_random);
3096 
3097 size_t
3098 SSL_get_server_random(const SSL *s, unsigned char *out, size_t max_out)
3099 {
3100 	size_t len = sizeof(s->s3->server_random);
3101 
3102 	if (out == NULL)
3103 		return len;
3104 
3105 	if (len > max_out)
3106 		len = max_out;
3107 
3108 	memcpy(out, s->s3->server_random, len);
3109 
3110 	return len;
3111 }
3112 LSSL_ALIAS(SSL_get_server_random);
3113 
3114 int
3115 ssl_init_wbio_buffer(SSL *s, int push)
3116 {
3117 	BIO	*bbio;
3118 
3119 	if (s->bbio == NULL) {
3120 		bbio = BIO_new(BIO_f_buffer());
3121 		if (bbio == NULL)
3122 			return (0);
3123 		s->bbio = bbio;
3124 	} else {
3125 		bbio = s->bbio;
3126 		if (s->bbio == s->wbio)
3127 			s->wbio = BIO_pop(s->wbio);
3128 	}
3129 	(void)BIO_reset(bbio);
3130 /*	if (!BIO_set_write_buffer_size(bbio,16*1024)) */
3131 	if (!BIO_set_read_buffer_size(bbio, 1)) {
3132 		SSLerror(s, ERR_R_BUF_LIB);
3133 		return (0);
3134 	}
3135 	if (push) {
3136 		if (s->wbio != bbio)
3137 			s->wbio = BIO_push(bbio, s->wbio);
3138 	} else {
3139 		if (s->wbio == bbio)
3140 			s->wbio = BIO_pop(bbio);
3141 	}
3142 	return (1);
3143 }
3144 
3145 void
3146 ssl_free_wbio_buffer(SSL *s)
3147 {
3148 	if (s == NULL)
3149 		return;
3150 
3151 	if (s->bbio == NULL)
3152 		return;
3153 
3154 	if (s->bbio == s->wbio) {
3155 		/* remove buffering */
3156 		s->wbio = BIO_pop(s->wbio);
3157 	}
3158 	BIO_free(s->bbio);
3159 	s->bbio = NULL;
3160 }
3161 
3162 void
3163 SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode)
3164 {
3165 	ctx->quiet_shutdown = mode;
3166 }
3167 LSSL_ALIAS(SSL_CTX_set_quiet_shutdown);
3168 
3169 int
3170 SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
3171 {
3172 	return (ctx->quiet_shutdown);
3173 }
3174 LSSL_ALIAS(SSL_CTX_get_quiet_shutdown);
3175 
3176 void
3177 SSL_set_quiet_shutdown(SSL *s, int mode)
3178 {
3179 	s->quiet_shutdown = mode;
3180 }
3181 LSSL_ALIAS(SSL_set_quiet_shutdown);
3182 
3183 int
3184 SSL_get_quiet_shutdown(const SSL *s)
3185 {
3186 	return (s->quiet_shutdown);
3187 }
3188 LSSL_ALIAS(SSL_get_quiet_shutdown);
3189 
3190 void
3191 SSL_set_shutdown(SSL *s, int mode)
3192 {
3193 	s->shutdown = mode;
3194 }
3195 LSSL_ALIAS(SSL_set_shutdown);
3196 
3197 int
3198 SSL_get_shutdown(const SSL *s)
3199 {
3200 	return (s->shutdown);
3201 }
3202 LSSL_ALIAS(SSL_get_shutdown);
3203 
3204 int
3205 SSL_version(const SSL *s)
3206 {
3207 	return (s->version);
3208 }
3209 LSSL_ALIAS(SSL_version);
3210 
3211 SSL_CTX *
3212 SSL_get_SSL_CTX(const SSL *ssl)
3213 {
3214 	return (ssl->ctx);
3215 }
3216 LSSL_ALIAS(SSL_get_SSL_CTX);
3217 
3218 SSL_CTX *
3219 SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
3220 {
3221 	SSL_CERT *new_cert;
3222 
3223 	if (ctx == NULL)
3224 		ctx = ssl->initial_ctx;
3225 	if (ssl->ctx == ctx)
3226 		return (ssl->ctx);
3227 
3228 	if ((new_cert = ssl_cert_dup(ctx->cert)) == NULL)
3229 		return NULL;
3230 	ssl_cert_free(ssl->cert);
3231 	ssl->cert = new_cert;
3232 
3233 	SSL_CTX_up_ref(ctx);
3234 	SSL_CTX_free(ssl->ctx); /* decrement reference count */
3235 	ssl->ctx = ctx;
3236 
3237 	return (ssl->ctx);
3238 }
3239 LSSL_ALIAS(SSL_set_SSL_CTX);
3240 
3241 int
3242 SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
3243 {
3244 	return (X509_STORE_set_default_paths(ctx->cert_store));
3245 }
3246 LSSL_ALIAS(SSL_CTX_set_default_verify_paths);
3247 
3248 int
3249 SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
3250     const char *CApath)
3251 {
3252 	return (X509_STORE_load_locations(ctx->cert_store, CAfile, CApath));
3253 }
3254 LSSL_ALIAS(SSL_CTX_load_verify_locations);
3255 
3256 int
3257 SSL_CTX_load_verify_mem(SSL_CTX *ctx, void *buf, int len)
3258 {
3259 	return (X509_STORE_load_mem(ctx->cert_store, buf, len));
3260 }
3261 LSSL_ALIAS(SSL_CTX_load_verify_mem);
3262 
3263 void
3264 SSL_set_info_callback(SSL *ssl, void (*cb)(const SSL *ssl, int type, int val))
3265 {
3266 	ssl->info_callback = cb;
3267 }
3268 LSSL_ALIAS(SSL_set_info_callback);
3269 
3270 void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type, int val)
3271 {
3272 	return (ssl->info_callback);
3273 }
3274 LSSL_ALIAS(SSL_get_info_callback);
3275 
3276 int
3277 SSL_state(const SSL *ssl)
3278 {
3279 	return (ssl->s3->hs.state);
3280 }
3281 LSSL_ALIAS(SSL_state);
3282 
3283 void
3284 SSL_set_state(SSL *ssl, int state)
3285 {
3286 	ssl->s3->hs.state = state;
3287 }
3288 LSSL_ALIAS(SSL_set_state);
3289 
3290 void
3291 SSL_set_verify_result(SSL *ssl, long arg)
3292 {
3293 	ssl->verify_result = arg;
3294 }
3295 LSSL_ALIAS(SSL_set_verify_result);
3296 
3297 long
3298 SSL_get_verify_result(const SSL *ssl)
3299 {
3300 	return (ssl->verify_result);
3301 }
3302 LSSL_ALIAS(SSL_get_verify_result);
3303 
3304 int
3305 SSL_verify_client_post_handshake(SSL *ssl)
3306 {
3307 	return 0;
3308 }
3309 LSSL_ALIAS(SSL_verify_client_post_handshake);
3310 
3311 void
3312 SSL_CTX_set_post_handshake_auth(SSL_CTX *ctx, int val)
3313 {
3314 	return;
3315 }
3316 LSSL_ALIAS(SSL_CTX_set_post_handshake_auth);
3317 
3318 void
3319 SSL_set_post_handshake_auth(SSL *ssl, int val)
3320 {
3321 	return;
3322 }
3323 LSSL_ALIAS(SSL_set_post_handshake_auth);
3324 
3325 int
3326 SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
3327     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
3328 {
3329 	return (CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
3330 	    new_func, dup_func, free_func));
3331 }
3332 LSSL_ALIAS(SSL_get_ex_new_index);
3333 
3334 int
3335 SSL_set_ex_data(SSL *s, int idx, void *arg)
3336 {
3337 	return (CRYPTO_set_ex_data(&s->ex_data, idx, arg));
3338 }
3339 LSSL_ALIAS(SSL_set_ex_data);
3340 
3341 void *
3342 SSL_get_ex_data(const SSL *s, int idx)
3343 {
3344 	return (CRYPTO_get_ex_data(&s->ex_data, idx));
3345 }
3346 LSSL_ALIAS(SSL_get_ex_data);
3347 
3348 int
3349 SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
3350     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
3351 {
3352 	return (CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
3353 	    new_func, dup_func, free_func));
3354 }
3355 LSSL_ALIAS(SSL_CTX_get_ex_new_index);
3356 
3357 int
3358 SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg)
3359 {
3360 	return (CRYPTO_set_ex_data(&s->ex_data, idx, arg));
3361 }
3362 LSSL_ALIAS(SSL_CTX_set_ex_data);
3363 
3364 void *
3365 SSL_CTX_get_ex_data(const SSL_CTX *s, int idx)
3366 {
3367 	return (CRYPTO_get_ex_data(&s->ex_data, idx));
3368 }
3369 LSSL_ALIAS(SSL_CTX_get_ex_data);
3370 
3371 int
3372 ssl_ok(SSL *s)
3373 {
3374 	return (1);
3375 }
3376 
3377 X509_STORE *
3378 SSL_CTX_get_cert_store(const SSL_CTX *ctx)
3379 {
3380 	return (ctx->cert_store);
3381 }
3382 LSSL_ALIAS(SSL_CTX_get_cert_store);
3383 
3384 void
3385 SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store)
3386 {
3387 	X509_STORE_free(ctx->cert_store);
3388 	ctx->cert_store = store;
3389 }
3390 LSSL_ALIAS(SSL_CTX_set_cert_store);
3391 
3392 X509 *
3393 SSL_CTX_get0_certificate(const SSL_CTX *ctx)
3394 {
3395 	if (ctx->cert == NULL)
3396 		return NULL;
3397 
3398 	return ctx->cert->key->x509;
3399 }
3400 LSSL_ALIAS(SSL_CTX_get0_certificate);
3401 
3402 EVP_PKEY *
3403 SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
3404 {
3405 	if (ctx->cert == NULL)
3406 		return NULL;
3407 
3408 	return ctx->cert->key->privatekey;
3409 }
3410 LSSL_ALIAS(SSL_CTX_get0_privatekey);
3411 
3412 int
3413 SSL_want(const SSL *s)
3414 {
3415 	return (s->rwstate);
3416 }
3417 LSSL_ALIAS(SSL_want);
3418 
3419 void
3420 SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx, RSA *(*cb)(SSL *ssl, int is_export,
3421     int keylength))
3422 {
3423 	SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
3424 }
3425 LSSL_ALIAS(SSL_CTX_set_tmp_rsa_callback);
3426 
3427 void
3428 SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
3429     int keylength))
3430 {
3431 	SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
3432 }
3433 LSSL_ALIAS(SSL_set_tmp_rsa_callback);
3434 
3435 void
3436 SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx, DH *(*dh)(SSL *ssl, int is_export,
3437     int keylength))
3438 {
3439 	SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
3440 }
3441 LSSL_ALIAS(SSL_CTX_set_tmp_dh_callback);
3442 
3443 void
3444 SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh)(SSL *ssl, int is_export,
3445     int keylength))
3446 {
3447 	SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
3448 }
3449 LSSL_ALIAS(SSL_set_tmp_dh_callback);
3450 
3451 void
3452 SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx, EC_KEY *(*ecdh)(SSL *ssl,
3453     int is_export, int keylength))
3454 {
3455 	SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB,
3456 	    (void (*)(void))ecdh);
3457 }
3458 LSSL_ALIAS(SSL_CTX_set_tmp_ecdh_callback);
3459 
3460 void
3461 SSL_set_tmp_ecdh_callback(SSL *ssl, EC_KEY *(*ecdh)(SSL *ssl, int is_export,
3462     int keylength))
3463 {
3464 	SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
3465 }
3466 LSSL_ALIAS(SSL_set_tmp_ecdh_callback);
3467 
3468 
3469 void
3470 SSL_CTX_set_msg_callback(SSL_CTX *ctx, void (*cb)(int write_p, int version,
3471     int content_type, const void *buf, size_t len, SSL *ssl, void *arg))
3472 {
3473 	SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK,
3474 	    (void (*)(void))cb);
3475 }
3476 LSSL_ALIAS(SSL_CTX_set_msg_callback);
3477 
3478 void
3479 SSL_set_msg_callback(SSL *ssl, void (*cb)(int write_p, int version,
3480     int content_type, const void *buf, size_t len, SSL *ssl, void *arg))
3481 {
3482 	SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3483 }
3484 LSSL_ALIAS(SSL_set_msg_callback);
3485 
3486 void
3487 SSL_set_debug(SSL *s, int debug)
3488 {
3489 	SSLerror(s, ERR_R_DISABLED);
3490 }
3491 LSSL_ALIAS(SSL_set_debug);
3492 
3493 int
3494 SSL_cache_hit(SSL *s)
3495 {
3496 	return (s->hit);
3497 }
3498 LSSL_ALIAS(SSL_cache_hit);
3499 
3500 int
3501 SSL_CTX_get_min_proto_version(SSL_CTX *ctx)
3502 {
3503 	return ctx->min_proto_version;
3504 }
3505 LSSL_ALIAS(SSL_CTX_get_min_proto_version);
3506 
3507 int
3508 SSL_CTX_set_min_proto_version(SSL_CTX *ctx, uint16_t version)
3509 {
3510 	return ssl_version_set_min(ctx->method, version,
3511 	    ctx->max_tls_version, &ctx->min_tls_version,
3512 	    &ctx->min_proto_version);
3513 }
3514 LSSL_ALIAS(SSL_CTX_set_min_proto_version);
3515 
3516 int
3517 SSL_CTX_get_max_proto_version(SSL_CTX *ctx)
3518 {
3519 	return ctx->max_proto_version;
3520 }
3521 LSSL_ALIAS(SSL_CTX_get_max_proto_version);
3522 
3523 int
3524 SSL_CTX_set_max_proto_version(SSL_CTX *ctx, uint16_t version)
3525 {
3526 	return ssl_version_set_max(ctx->method, version,
3527 	    ctx->min_tls_version, &ctx->max_tls_version,
3528 	    &ctx->max_proto_version);
3529 }
3530 LSSL_ALIAS(SSL_CTX_set_max_proto_version);
3531 
3532 int
3533 SSL_get_min_proto_version(SSL *ssl)
3534 {
3535 	return ssl->min_proto_version;
3536 }
3537 LSSL_ALIAS(SSL_get_min_proto_version);
3538 
3539 int
3540 SSL_set_min_proto_version(SSL *ssl, uint16_t version)
3541 {
3542 	return ssl_version_set_min(ssl->method, version,
3543 	    ssl->max_tls_version, &ssl->min_tls_version,
3544 	    &ssl->min_proto_version);
3545 }
3546 LSSL_ALIAS(SSL_set_min_proto_version);
3547 int
3548 SSL_get_max_proto_version(SSL *ssl)
3549 {
3550 	return ssl->max_proto_version;
3551 }
3552 LSSL_ALIAS(SSL_get_max_proto_version);
3553 
3554 int
3555 SSL_set_max_proto_version(SSL *ssl, uint16_t version)
3556 {
3557 	return ssl_version_set_max(ssl->method, version,
3558 	    ssl->min_tls_version, &ssl->max_tls_version,
3559 	    &ssl->max_proto_version);
3560 }
3561 LSSL_ALIAS(SSL_set_max_proto_version);
3562 
3563 const SSL_METHOD *
3564 SSL_CTX_get_ssl_method(const SSL_CTX *ctx)
3565 {
3566 	return ctx->method;
3567 }
3568 LSSL_ALIAS(SSL_CTX_get_ssl_method);
3569 
3570 int
3571 SSL_CTX_get_security_level(const SSL_CTX *ctx)
3572 {
3573 	return ctx->cert->security_level;
3574 }
3575 LSSL_ALIAS(SSL_CTX_get_security_level);
3576 
3577 void
3578 SSL_CTX_set_security_level(SSL_CTX *ctx, int level)
3579 {
3580 	ctx->cert->security_level = level;
3581 }
3582 LSSL_ALIAS(SSL_CTX_set_security_level);
3583 
3584 int
3585 SSL_get_security_level(const SSL *ssl)
3586 {
3587 	return ssl->cert->security_level;
3588 }
3589 LSSL_ALIAS(SSL_get_security_level);
3590 
3591 void
3592 SSL_set_security_level(SSL *ssl, int level)
3593 {
3594 	ssl->cert->security_level = level;
3595 }
3596 LSSL_ALIAS(SSL_set_security_level);
3597 
3598 int
3599 SSL_is_quic(const SSL *ssl)
3600 {
3601 	return ssl->quic_method != NULL;
3602 }
3603 LSSL_ALIAS(SSL_is_quic);
3604 
3605 int
3606 SSL_set_quic_transport_params(SSL *ssl, const uint8_t *params,
3607     size_t params_len)
3608 {
3609 	freezero(ssl->quic_transport_params,
3610 	    ssl->quic_transport_params_len);
3611 	ssl->quic_transport_params = NULL;
3612 	ssl->quic_transport_params_len = 0;
3613 
3614 	if ((ssl->quic_transport_params = malloc(params_len)) == NULL)
3615 		return 0;
3616 
3617 	memcpy(ssl->quic_transport_params, params, params_len);
3618 	ssl->quic_transport_params_len = params_len;
3619 
3620 	return 1;
3621 }
3622 LSSL_ALIAS(SSL_set_quic_transport_params);
3623 
3624 void
3625 SSL_get_peer_quic_transport_params(const SSL *ssl, const uint8_t **out_params,
3626     size_t *out_params_len)
3627 {
3628 	*out_params = ssl->s3->peer_quic_transport_params;
3629 	*out_params_len = ssl->s3->peer_quic_transport_params_len;
3630 }
3631 LSSL_ALIAS(SSL_get_peer_quic_transport_params);
3632 
3633 void
3634 SSL_set_quic_use_legacy_codepoint(SSL *ssl, int use_legacy)
3635 {
3636 	/* Not supported. */
3637 }
3638 LSSL_ALIAS(SSL_set_quic_use_legacy_codepoint);
3639