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