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