xref: /openbsd/lib/libssl/ssl_lib.c (revision 6eec31fb)
1 /* $OpenBSD: ssl_lib.c,v 1.330 2024/09/22 14:59:48 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 		if (larg < (long)dtls1_min_mtu())
1376 			return (0);
1377 		if (SSL_is_dtls(s)) {
1378 			s->d1->mtu = larg;
1379 			return (larg);
1380 		}
1381 		return (0);
1382 	case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1383 		if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1384 			return (0);
1385 		s->max_send_fragment = larg;
1386 		return (1);
1387 	case SSL_CTRL_GET_RI_SUPPORT:
1388 		if (s->s3)
1389 			return (s->s3->send_connection_binding);
1390 		else return (0);
1391 	default:
1392 		if (SSL_is_dtls(s))
1393 			return dtls1_ctrl(s, cmd, larg, parg);
1394 		return ssl3_ctrl(s, cmd, larg, parg);
1395 	}
1396 }
1397 LSSL_ALIAS(SSL_ctrl);
1398 
1399 long
SSL_callback_ctrl(SSL * s,int cmd,void (* fp)(void))1400 SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
1401 {
1402 	switch (cmd) {
1403 	case SSL_CTRL_SET_MSG_CALLBACK:
1404 		s->msg_callback = (ssl_msg_callback_fn *)(fp);
1405 		return (1);
1406 
1407 	default:
1408 		return (ssl3_callback_ctrl(s, cmd, fp));
1409 	}
1410 }
1411 LSSL_ALIAS(SSL_callback_ctrl);
1412 
1413 struct lhash_st_SSL_SESSION *
SSL_CTX_sessions(SSL_CTX * ctx)1414 SSL_CTX_sessions(SSL_CTX *ctx)
1415 {
1416 	return (ctx->sessions);
1417 }
1418 LSSL_ALIAS(SSL_CTX_sessions);
1419 
1420 long
SSL_CTX_ctrl(SSL_CTX * ctx,int cmd,long larg,void * parg)1421 SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
1422 {
1423 	long	l;
1424 
1425 	switch (cmd) {
1426 	case SSL_CTRL_GET_READ_AHEAD:
1427 		return (ctx->read_ahead);
1428 	case SSL_CTRL_SET_READ_AHEAD:
1429 		l = ctx->read_ahead;
1430 		ctx->read_ahead = larg;
1431 		return (l);
1432 
1433 	case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1434 		ctx->msg_callback_arg = parg;
1435 		return (1);
1436 
1437 	case SSL_CTRL_GET_MAX_CERT_LIST:
1438 		return (ctx->max_cert_list);
1439 	case SSL_CTRL_SET_MAX_CERT_LIST:
1440 		l = ctx->max_cert_list;
1441 		ctx->max_cert_list = larg;
1442 		return (l);
1443 
1444 	case SSL_CTRL_SET_SESS_CACHE_SIZE:
1445 		l = ctx->session_cache_size;
1446 		ctx->session_cache_size = larg;
1447 		return (l);
1448 	case SSL_CTRL_GET_SESS_CACHE_SIZE:
1449 		return (ctx->session_cache_size);
1450 	case SSL_CTRL_SET_SESS_CACHE_MODE:
1451 		l = ctx->session_cache_mode;
1452 		ctx->session_cache_mode = larg;
1453 		return (l);
1454 	case SSL_CTRL_GET_SESS_CACHE_MODE:
1455 		return (ctx->session_cache_mode);
1456 
1457 	case SSL_CTRL_SESS_NUMBER:
1458 		return (lh_SSL_SESSION_num_items(ctx->sessions));
1459 	case SSL_CTRL_SESS_CONNECT:
1460 		return (ctx->stats.sess_connect);
1461 	case SSL_CTRL_SESS_CONNECT_GOOD:
1462 		return (ctx->stats.sess_connect_good);
1463 	case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1464 		return (ctx->stats.sess_connect_renegotiate);
1465 	case SSL_CTRL_SESS_ACCEPT:
1466 		return (ctx->stats.sess_accept);
1467 	case SSL_CTRL_SESS_ACCEPT_GOOD:
1468 		return (ctx->stats.sess_accept_good);
1469 	case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1470 		return (ctx->stats.sess_accept_renegotiate);
1471 	case SSL_CTRL_SESS_HIT:
1472 		return (ctx->stats.sess_hit);
1473 	case SSL_CTRL_SESS_CB_HIT:
1474 		return (ctx->stats.sess_cb_hit);
1475 	case SSL_CTRL_SESS_MISSES:
1476 		return (ctx->stats.sess_miss);
1477 	case SSL_CTRL_SESS_TIMEOUTS:
1478 		return (ctx->stats.sess_timeout);
1479 	case SSL_CTRL_SESS_CACHE_FULL:
1480 		return (ctx->stats.sess_cache_full);
1481 	case SSL_CTRL_OPTIONS:
1482 		return (ctx->options|=larg);
1483 	case SSL_CTRL_CLEAR_OPTIONS:
1484 		return (ctx->options&=~larg);
1485 	case SSL_CTRL_MODE:
1486 		return (ctx->mode|=larg);
1487 	case SSL_CTRL_CLEAR_MODE:
1488 		return (ctx->mode&=~larg);
1489 	case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1490 		if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1491 			return (0);
1492 		ctx->max_send_fragment = larg;
1493 		return (1);
1494 	default:
1495 		return (ssl3_ctx_ctrl(ctx, cmd, larg, parg));
1496 	}
1497 }
1498 LSSL_ALIAS(SSL_CTX_ctrl);
1499 
1500 long
SSL_CTX_callback_ctrl(SSL_CTX * ctx,int cmd,void (* fp)(void))1501 SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
1502 {
1503 	switch (cmd) {
1504 	case SSL_CTRL_SET_MSG_CALLBACK:
1505 		ctx->msg_callback = (ssl_msg_callback_fn *)fp;
1506 		return (1);
1507 
1508 	default:
1509 		return (ssl3_ctx_callback_ctrl(ctx, cmd, fp));
1510 	}
1511 }
1512 LSSL_ALIAS(SSL_CTX_callback_ctrl);
1513 
STACK_OF(SSL_CIPHER)1514 STACK_OF(SSL_CIPHER) *
1515 SSL_get_ciphers(const SSL *s)
1516 {
1517 	if (s == NULL)
1518 		return (NULL);
1519 	if (s->cipher_list != NULL)
1520 		return (s->cipher_list);
1521 
1522 	return (s->ctx->cipher_list);
1523 }
1524 LSSL_ALIAS(SSL_get_ciphers);
1525 
STACK_OF(SSL_CIPHER)1526 STACK_OF(SSL_CIPHER) *
1527 SSL_get_client_ciphers(const SSL *s)
1528 {
1529 	if (s == NULL || !s->server)
1530 		return NULL;
1531 	return s->s3->hs.client_ciphers;
1532 }
1533 LSSL_ALIAS(SSL_get_client_ciphers);
1534 
STACK_OF(SSL_CIPHER)1535 STACK_OF(SSL_CIPHER) *
1536 SSL_get1_supported_ciphers(SSL *s)
1537 {
1538 	STACK_OF(SSL_CIPHER) *supported_ciphers = NULL, *ciphers;
1539 	SSL_CIPHER *cipher;
1540 	uint16_t min_vers, max_vers;
1541 	int i;
1542 
1543 	if (s == NULL)
1544 		return NULL;
1545 	if (!ssl_supported_tls_version_range(s, &min_vers, &max_vers))
1546 		return NULL;
1547 	if ((ciphers = SSL_get_ciphers(s)) == NULL)
1548 		return NULL;
1549 	if ((supported_ciphers = sk_SSL_CIPHER_new_null()) == NULL)
1550 		return NULL;
1551 
1552 	for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1553 		if ((cipher = sk_SSL_CIPHER_value(ciphers, i)) == NULL)
1554 			goto err;
1555 		if (!ssl_cipher_allowed_in_tls_version_range(cipher, min_vers,
1556 		    max_vers))
1557 			continue;
1558 		if (!ssl_security_supported_cipher(s, cipher))
1559 			continue;
1560 		if (!sk_SSL_CIPHER_push(supported_ciphers, cipher))
1561 			goto err;
1562 	}
1563 
1564 	if (sk_SSL_CIPHER_num(supported_ciphers) > 0)
1565 		return supported_ciphers;
1566 
1567  err:
1568 	sk_SSL_CIPHER_free(supported_ciphers);
1569 	return NULL;
1570 }
1571 LSSL_ALIAS(SSL_get1_supported_ciphers);
1572 
1573 /* See if we have any ECC cipher suites. */
1574 int
ssl_has_ecc_ciphers(SSL * s)1575 ssl_has_ecc_ciphers(SSL *s)
1576 {
1577 	STACK_OF(SSL_CIPHER) *ciphers;
1578 	unsigned long alg_k, alg_a;
1579 	SSL_CIPHER *cipher;
1580 	int i;
1581 
1582 	if ((ciphers = SSL_get_ciphers(s)) == NULL)
1583 		return 0;
1584 
1585 	for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1586 		cipher = sk_SSL_CIPHER_value(ciphers, i);
1587 
1588 		alg_k = cipher->algorithm_mkey;
1589 		alg_a = cipher->algorithm_auth;
1590 
1591 		if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))
1592 			return 1;
1593 	}
1594 
1595 	return 0;
1596 }
1597 
1598 /* The old interface to get the same thing as SSL_get_ciphers(). */
1599 const char *
SSL_get_cipher_list(const SSL * s,int n)1600 SSL_get_cipher_list(const SSL *s, int n)
1601 {
1602 	STACK_OF(SSL_CIPHER) *ciphers;
1603 	const SSL_CIPHER *cipher;
1604 
1605 	if ((ciphers = SSL_get_ciphers(s)) == NULL)
1606 		return (NULL);
1607 	if ((cipher = sk_SSL_CIPHER_value(ciphers, n)) == NULL)
1608 		return (NULL);
1609 
1610 	return (cipher->name);
1611 }
1612 LSSL_ALIAS(SSL_get_cipher_list);
1613 
STACK_OF(SSL_CIPHER)1614 STACK_OF(SSL_CIPHER) *
1615 SSL_CTX_get_ciphers(const SSL_CTX *ctx)
1616 {
1617 	if (ctx == NULL)
1618 		return NULL;
1619 	return ctx->cipher_list;
1620 }
1621 LSSL_ALIAS(SSL_CTX_get_ciphers);
1622 
1623 /* Specify the ciphers to be used by default by the SSL_CTX. */
1624 int
SSL_CTX_set_cipher_list(SSL_CTX * ctx,const char * str)1625 SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
1626 {
1627 	STACK_OF(SSL_CIPHER) *ciphers;
1628 
1629 	/*
1630 	 * ssl_create_cipher_list may return an empty stack if it was unable to
1631 	 * find a cipher matching the given rule string (for example if the
1632 	 * rule string specifies a cipher which has been disabled). This is not
1633 	 * an error as far as ssl_create_cipher_list is concerned, and hence
1634 	 * ctx->cipher_list has been updated.
1635 	 */
1636 	ciphers = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
1637 	    ctx->cipher_list_tls13, str, ctx->cert);
1638 	if (ciphers == NULL) {
1639 		return (0);
1640 	} else if (sk_SSL_CIPHER_num(ciphers) == 0) {
1641 		SSLerrorx(SSL_R_NO_CIPHER_MATCH);
1642 		return (0);
1643 	}
1644 	return (1);
1645 }
1646 LSSL_ALIAS(SSL_CTX_set_cipher_list);
1647 
1648 int
SSL_CTX_set_ciphersuites(SSL_CTX * ctx,const char * str)1649 SSL_CTX_set_ciphersuites(SSL_CTX *ctx, const char *str)
1650 {
1651 	if (!ssl_parse_ciphersuites(&ctx->cipher_list_tls13, str)) {
1652 		SSLerrorx(SSL_R_NO_CIPHER_MATCH);
1653 		return 0;
1654 	}
1655 	if (!ssl_merge_cipherlists(ctx->cipher_list,
1656 	    ctx->cipher_list_tls13, &ctx->cipher_list))
1657 		return 0;
1658 
1659 	return 1;
1660 }
1661 LSSL_ALIAS(SSL_CTX_set_ciphersuites);
1662 
1663 /* Specify the ciphers to be used by the SSL. */
1664 int
SSL_set_cipher_list(SSL * s,const char * str)1665 SSL_set_cipher_list(SSL *s, const char *str)
1666 {
1667 	STACK_OF(SSL_CIPHER) *ciphers, *ciphers_tls13;
1668 
1669 	if ((ciphers_tls13 = s->cipher_list_tls13) == NULL)
1670 		ciphers_tls13 = s->ctx->cipher_list_tls13;
1671 
1672 	/* See comment in SSL_CTX_set_cipher_list. */
1673 	ciphers = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
1674 	    ciphers_tls13, str, s->cert);
1675 	if (ciphers == NULL) {
1676 		return (0);
1677 	} else if (sk_SSL_CIPHER_num(ciphers) == 0) {
1678 		SSLerror(s, SSL_R_NO_CIPHER_MATCH);
1679 		return (0);
1680 	}
1681 	return (1);
1682 }
1683 LSSL_ALIAS(SSL_set_cipher_list);
1684 
1685 int
SSL_set_ciphersuites(SSL * s,const char * str)1686 SSL_set_ciphersuites(SSL *s, const char *str)
1687 {
1688 	STACK_OF(SSL_CIPHER) *ciphers;
1689 
1690 	if ((ciphers = s->cipher_list) == NULL)
1691 		ciphers = s->ctx->cipher_list;
1692 
1693 	if (!ssl_parse_ciphersuites(&s->cipher_list_tls13, str)) {
1694 		SSLerrorx(SSL_R_NO_CIPHER_MATCH);
1695 		return (0);
1696 	}
1697 	if (!ssl_merge_cipherlists(ciphers, s->cipher_list_tls13,
1698 	    &s->cipher_list))
1699 		return 0;
1700 
1701 	return 1;
1702 }
1703 LSSL_ALIAS(SSL_set_ciphersuites);
1704 
1705 char *
SSL_get_shared_ciphers(const SSL * s,char * buf,int len)1706 SSL_get_shared_ciphers(const SSL *s, char *buf, int len)
1707 {
1708 	STACK_OF(SSL_CIPHER) *client_ciphers, *server_ciphers;
1709 	const SSL_CIPHER *cipher;
1710 	size_t curlen = 0;
1711 	char *end;
1712 	int i;
1713 
1714 	if (!s->server || len < 2)
1715 		return NULL;
1716 
1717 	if ((client_ciphers = s->s3->hs.client_ciphers) == NULL)
1718 		return NULL;
1719 	if ((server_ciphers = SSL_get_ciphers(s)) == NULL)
1720 		return NULL;
1721 	if (sk_SSL_CIPHER_num(client_ciphers) == 0 ||
1722 	    sk_SSL_CIPHER_num(server_ciphers) == 0)
1723 		return NULL;
1724 
1725 	buf[0] = '\0';
1726 	for (i = 0; i < sk_SSL_CIPHER_num(client_ciphers); i++) {
1727 		cipher = sk_SSL_CIPHER_value(client_ciphers, i);
1728 
1729 		if (sk_SSL_CIPHER_find(server_ciphers, cipher) < 0)
1730 			continue;
1731 
1732 		end = buf + curlen;
1733 		if (strlcat(buf, cipher->name, len) >= len ||
1734 		    (curlen = strlcat(buf, ":", len)) >= len) {
1735 			/* remove truncated cipher from list */
1736 			*end = '\0';
1737 			break;
1738 		}
1739 	}
1740 	/* remove trailing colon */
1741 	if ((end = strrchr(buf, ':')) != NULL)
1742 		*end = '\0';
1743 	return buf;
1744 }
1745 LSSL_ALIAS(SSL_get_shared_ciphers);
1746 
1747 /*
1748  * Return a servername extension value if provided in Client Hello, or NULL.
1749  * So far, only host_name types are defined (RFC 3546).
1750  */
1751 const char *
SSL_get_servername(const SSL * s,const int type)1752 SSL_get_servername(const SSL *s, const int type)
1753 {
1754 	if (type != TLSEXT_NAMETYPE_host_name)
1755 		return (NULL);
1756 
1757 	return (s->session && !s->tlsext_hostname ?
1758 	    s->session->tlsext_hostname :
1759 	    s->tlsext_hostname);
1760 }
1761 LSSL_ALIAS(SSL_get_servername);
1762 
1763 int
SSL_get_servername_type(const SSL * s)1764 SSL_get_servername_type(const SSL *s)
1765 {
1766 	if (s->session &&
1767 	    (!s->tlsext_hostname ?
1768 	    s->session->tlsext_hostname : s->tlsext_hostname))
1769 		return (TLSEXT_NAMETYPE_host_name);
1770 	return (-1);
1771 }
1772 LSSL_ALIAS(SSL_get_servername_type);
1773 
1774 /*
1775  * SSL_select_next_proto implements standard protocol selection. It is
1776  * expected that this function is called from the callback set by
1777  * SSL_CTX_set_alpn_select_cb.
1778  *
1779  * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1780  * strings. The length byte itself is not included in the length. A byte
1781  * string of length 0 is invalid. No byte string may be truncated.
1782  *
1783  * It returns either:
1784  * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1785  * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1786  *
1787  * XXX - the out argument points into server_list or client_list and should
1788  * therefore really be const. We can't fix that without breaking the callers.
1789  */
1790 int
SSL_select_next_proto(unsigned char ** out,unsigned char * outlen,const unsigned char * peer_list,unsigned int peer_list_len,const unsigned char * supported_list,unsigned int supported_list_len)1791 SSL_select_next_proto(unsigned char **out, unsigned char *outlen,
1792     const unsigned char *peer_list, unsigned int peer_list_len,
1793     const unsigned char *supported_list, unsigned int supported_list_len)
1794 {
1795 	CBS peer, peer_proto, supported, supported_proto;
1796 
1797 	*out = NULL;
1798 	*outlen = 0;
1799 
1800 	/* First check that the supported list is well-formed. */
1801 	CBS_init(&supported, supported_list, supported_list_len);
1802 	if (!tlsext_alpn_check_format(&supported))
1803 		goto err;
1804 
1805 	/*
1806 	 * Use first supported protocol as fallback. This is one way of doing
1807 	 * NPN's "opportunistic" protocol selection (see security considerations
1808 	 * in draft-agl-tls-nextprotoneg-04), and it is the documented behavior
1809 	 * of this API. For ALPN it's the callback's responsibility to fail on
1810 	 * OPENSSL_NPN_NO_OVERLAP.
1811 	 */
1812 
1813 	if (!CBS_get_u8_length_prefixed(&supported, &supported_proto))
1814 		goto err;
1815 
1816 	*out = (unsigned char *)CBS_data(&supported_proto);
1817 	*outlen = CBS_len(&supported_proto);
1818 
1819 	/* Now check that the peer list is well-formed. */
1820 	CBS_init(&peer, peer_list, peer_list_len);
1821 	if (!tlsext_alpn_check_format(&peer))
1822 		goto err;
1823 
1824 	/*
1825 	 * Walk the peer list and select the first protocol that appears in
1826 	 * the supported list. Thus we honor peer preference rather than local
1827 	 * preference contrary to a SHOULD in RFC 7301, section 3.2.
1828 	 */
1829 	while (CBS_len(&peer) > 0) {
1830 		if (!CBS_get_u8_length_prefixed(&peer, &peer_proto))
1831 			goto err;
1832 
1833 		CBS_init(&supported, supported_list, supported_list_len);
1834 
1835 		while (CBS_len(&supported) > 0) {
1836 			if (!CBS_get_u8_length_prefixed(&supported,
1837 			    &supported_proto))
1838 				goto err;
1839 
1840 			if (CBS_mem_equal(&supported_proto,
1841 			    CBS_data(&peer_proto), CBS_len(&peer_proto))) {
1842 				*out = (unsigned char *)CBS_data(&peer_proto);
1843 				*outlen = CBS_len(&peer_proto);
1844 
1845 				return OPENSSL_NPN_NEGOTIATED;
1846 			}
1847 		}
1848 	}
1849 
1850  err:
1851 	return OPENSSL_NPN_NO_OVERLAP;
1852 }
1853 LSSL_ALIAS(SSL_select_next_proto);
1854 
1855 /* SSL_get0_next_proto_negotiated is deprecated. */
1856 void
SSL_get0_next_proto_negotiated(const SSL * s,const unsigned char ** data,unsigned int * len)1857 SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data,
1858     unsigned int *len)
1859 {
1860 	*data = NULL;
1861 	*len = 0;
1862 }
1863 LSSL_ALIAS(SSL_get0_next_proto_negotiated);
1864 
1865 /* SSL_CTX_set_next_protos_advertised_cb is deprecated. */
1866 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)1867 SSL_CTX_set_next_protos_advertised_cb(SSL_CTX *ctx, int (*cb) (SSL *ssl,
1868     const unsigned char **out, unsigned int *outlen, void *arg), void *arg)
1869 {
1870 }
1871 LSSL_ALIAS(SSL_CTX_set_next_protos_advertised_cb);
1872 
1873 /* SSL_CTX_set_next_proto_select_cb is deprecated. */
1874 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)1875 SSL_CTX_set_next_proto_select_cb(SSL_CTX *ctx, int (*cb) (SSL *s,
1876     unsigned char **out, unsigned char *outlen, const unsigned char *in,
1877     unsigned int inlen, void *arg), void *arg)
1878 {
1879 }
1880 LSSL_ALIAS(SSL_CTX_set_next_proto_select_cb);
1881 
1882 /*
1883  * SSL_CTX_set_alpn_protos sets the ALPN protocol list to the specified
1884  * protocols, which must be in wire-format (i.e. a series of non-empty,
1885  * 8-bit length-prefixed strings). Returns 0 on success.
1886  */
1887 int
SSL_CTX_set_alpn_protos(SSL_CTX * ctx,const unsigned char * protos,unsigned int protos_len)1888 SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char *protos,
1889     unsigned int protos_len)
1890 {
1891 	CBS cbs;
1892 	int failed = 1;
1893 
1894 	if (protos == NULL)
1895 		protos_len = 0;
1896 
1897 	CBS_init(&cbs, protos, protos_len);
1898 
1899 	if (protos_len > 0) {
1900 		if (!tlsext_alpn_check_format(&cbs))
1901 			goto err;
1902 	}
1903 
1904 	if (!CBS_stow(&cbs, &ctx->alpn_client_proto_list,
1905 	    &ctx->alpn_client_proto_list_len))
1906 		goto err;
1907 
1908 	failed = 0;
1909 
1910  err:
1911 	/* NOTE: Return values are the reverse of what you expect. */
1912 	return failed;
1913 }
1914 LSSL_ALIAS(SSL_CTX_set_alpn_protos);
1915 
1916 /*
1917  * SSL_set_alpn_protos sets the ALPN protocol list to the specified
1918  * protocols, which must be in wire-format (i.e. a series of non-empty,
1919  * 8-bit length-prefixed strings). Returns 0 on success.
1920  */
1921 int
SSL_set_alpn_protos(SSL * ssl,const unsigned char * protos,unsigned int protos_len)1922 SSL_set_alpn_protos(SSL *ssl, const unsigned char *protos,
1923     unsigned int protos_len)
1924 {
1925 	CBS cbs;
1926 	int failed = 1;
1927 
1928 	if (protos == NULL)
1929 		protos_len = 0;
1930 
1931 	CBS_init(&cbs, protos, protos_len);
1932 
1933 	if (protos_len > 0) {
1934 		if (!tlsext_alpn_check_format(&cbs))
1935 			goto err;
1936 	}
1937 
1938 	if (!CBS_stow(&cbs, &ssl->alpn_client_proto_list,
1939 	    &ssl->alpn_client_proto_list_len))
1940 		goto err;
1941 
1942 	failed = 0;
1943 
1944  err:
1945 	/* NOTE: Return values are the reverse of what you expect. */
1946 	return failed;
1947 }
1948 LSSL_ALIAS(SSL_set_alpn_protos);
1949 
1950 /*
1951  * SSL_CTX_set_alpn_select_cb sets a callback function that is called during
1952  * ClientHello processing in order to select an ALPN protocol from the
1953  * client's list of offered protocols.
1954  */
1955 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)1956 SSL_CTX_set_alpn_select_cb(SSL_CTX* ctx,
1957     int (*cb) (SSL *ssl, const unsigned char **out, unsigned char *outlen,
1958     const unsigned char *in, unsigned int inlen, void *arg), void *arg)
1959 {
1960 	ctx->alpn_select_cb = cb;
1961 	ctx->alpn_select_cb_arg = arg;
1962 }
1963 LSSL_ALIAS(SSL_CTX_set_alpn_select_cb);
1964 
1965 /*
1966  * SSL_get0_alpn_selected gets the selected ALPN protocol (if any). On return
1967  * it sets data to point to len bytes of protocol name (not including the
1968  * leading length-prefix byte). If the server didn't respond with* a negotiated
1969  * protocol then len will be zero.
1970  */
1971 void
SSL_get0_alpn_selected(const SSL * ssl,const unsigned char ** data,unsigned int * len)1972 SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
1973     unsigned int *len)
1974 {
1975 	*data = ssl->s3->alpn_selected;
1976 	*len = ssl->s3->alpn_selected_len;
1977 }
1978 LSSL_ALIAS(SSL_get0_alpn_selected);
1979 
1980 void
SSL_set_psk_use_session_callback(SSL * s,SSL_psk_use_session_cb_func cb)1981 SSL_set_psk_use_session_callback(SSL *s, SSL_psk_use_session_cb_func cb)
1982 {
1983 	return;
1984 }
1985 LSSL_ALIAS(SSL_set_psk_use_session_callback);
1986 
1987 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)1988 SSL_export_keying_material(SSL *s, unsigned char *out, size_t out_len,
1989     const char *label, size_t label_len, const unsigned char *context,
1990     size_t context_len, int use_context)
1991 {
1992 	if (s->tls13 != NULL && s->version == TLS1_3_VERSION) {
1993 		if (!use_context) {
1994 			context = NULL;
1995 			context_len = 0;
1996 		}
1997 		return tls13_exporter(s->tls13, label, label_len, context,
1998 		    context_len, out, out_len);
1999 	}
2000 
2001 	return tls12_exporter(s, label, label_len, context, context_len,
2002 	    use_context, out, out_len);
2003 }
2004 LSSL_ALIAS(SSL_export_keying_material);
2005 
2006 static unsigned long
ssl_session_hash(const SSL_SESSION * a)2007 ssl_session_hash(const SSL_SESSION *a)
2008 {
2009 	unsigned long	l;
2010 
2011 	l = (unsigned long)
2012 	    ((unsigned int) a->session_id[0]     )|
2013 	    ((unsigned int) a->session_id[1]<< 8L)|
2014 	    ((unsigned long)a->session_id[2]<<16L)|
2015 	    ((unsigned long)a->session_id[3]<<24L);
2016 	return (l);
2017 }
2018 
2019 /*
2020  * NB: If this function (or indeed the hash function which uses a sort of
2021  * coarser function than this one) is changed, ensure
2022  * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
2023  * able to construct an SSL_SESSION that will collide with any existing session
2024  * with a matching session ID.
2025  */
2026 static int
ssl_session_cmp(const SSL_SESSION * a,const SSL_SESSION * b)2027 ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
2028 {
2029 	if (a->ssl_version != b->ssl_version)
2030 		return (1);
2031 	if (a->session_id_length != b->session_id_length)
2032 		return (1);
2033 	if (timingsafe_memcmp(a->session_id, b->session_id, a->session_id_length) != 0)
2034 		return (1);
2035 	return (0);
2036 }
2037 
2038 /*
2039  * These wrapper functions should remain rather than redeclaring
2040  * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
2041  * variable. The reason is that the functions aren't static, they're exposed via
2042  * ssl.h.
2043  */
2044 static unsigned long
ssl_session_LHASH_HASH(const void * arg)2045 ssl_session_LHASH_HASH(const void *arg)
2046 {
2047 	const SSL_SESSION *a = arg;
2048 
2049 	return ssl_session_hash(a);
2050 }
2051 
2052 static int
ssl_session_LHASH_COMP(const void * arg1,const void * arg2)2053 ssl_session_LHASH_COMP(const void *arg1, const void *arg2)
2054 {
2055 	const SSL_SESSION *a = arg1;
2056 	const SSL_SESSION *b = arg2;
2057 
2058 	return ssl_session_cmp(a, b);
2059 }
2060 
2061 SSL_CTX *
SSL_CTX_new(const SSL_METHOD * meth)2062 SSL_CTX_new(const SSL_METHOD *meth)
2063 {
2064 	SSL_CTX	*ret;
2065 
2066 	if (!OPENSSL_init_ssl(0, NULL)) {
2067 		SSLerrorx(SSL_R_LIBRARY_BUG);
2068 		return (NULL);
2069 	}
2070 
2071 	if (meth == NULL) {
2072 		SSLerrorx(SSL_R_NULL_SSL_METHOD_PASSED);
2073 		return (NULL);
2074 	}
2075 
2076 	if ((ret = calloc(1, sizeof(*ret))) == NULL) {
2077 		SSLerrorx(ERR_R_MALLOC_FAILURE);
2078 		return (NULL);
2079 	}
2080 
2081 	if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
2082 		SSLerrorx(SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
2083 		goto err;
2084 	}
2085 
2086 	ret->method = meth;
2087 	ret->min_tls_version = meth->min_tls_version;
2088 	ret->max_tls_version = meth->max_tls_version;
2089 	ret->min_proto_version = 0;
2090 	ret->max_proto_version = 0;
2091 	ret->mode = SSL_MODE_AUTO_RETRY;
2092 
2093 	ret->cert_store = NULL;
2094 	ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
2095 	ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
2096 	ret->session_cache_head = NULL;
2097 	ret->session_cache_tail = NULL;
2098 
2099 	/* We take the system default */
2100 	ret->session_timeout = ssl_get_default_timeout();
2101 
2102 	ret->new_session_cb = NULL;
2103 	ret->remove_session_cb = NULL;
2104 	ret->get_session_cb = NULL;
2105 	ret->generate_session_id = NULL;
2106 
2107 	memset((char *)&ret->stats, 0, sizeof(ret->stats));
2108 
2109 	ret->references = 1;
2110 	ret->quiet_shutdown = 0;
2111 
2112 	ret->info_callback = NULL;
2113 
2114 	ret->app_verify_callback = NULL;
2115 	ret->app_verify_arg = NULL;
2116 
2117 	ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
2118 	ret->read_ahead = 0;
2119 	ret->msg_callback = NULL;
2120 	ret->msg_callback_arg = NULL;
2121 	ret->verify_mode = SSL_VERIFY_NONE;
2122 	ret->sid_ctx_length = 0;
2123 	ret->default_verify_callback = NULL;
2124 
2125 	if ((ret->cert = ssl_cert_new()) == NULL)
2126 		goto err;
2127 
2128 	ret->default_passwd_callback = NULL;
2129 	ret->default_passwd_callback_userdata = NULL;
2130 	ret->client_cert_cb = NULL;
2131 	ret->app_gen_cookie_cb = NULL;
2132 	ret->app_verify_cookie_cb = NULL;
2133 
2134 	ret->sessions = lh_SSL_SESSION_new();
2135 	if (ret->sessions == NULL)
2136 		goto err;
2137 	ret->cert_store = X509_STORE_new();
2138 	if (ret->cert_store == NULL)
2139 		goto err;
2140 
2141 	ssl_create_cipher_list(ret->method, &ret->cipher_list,
2142 	    NULL, SSL_DEFAULT_CIPHER_LIST, ret->cert);
2143 	if (ret->cipher_list == NULL ||
2144 	    sk_SSL_CIPHER_num(ret->cipher_list) <= 0) {
2145 		SSLerrorx(SSL_R_LIBRARY_HAS_NO_CIPHERS);
2146 		goto err2;
2147 	}
2148 
2149 	ret->param = X509_VERIFY_PARAM_new();
2150 	if (!ret->param)
2151 		goto err;
2152 
2153 	if ((ret->client_CA = sk_X509_NAME_new_null()) == NULL)
2154 		goto err;
2155 
2156 	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
2157 
2158 	ret->extra_certs = NULL;
2159 
2160 	ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
2161 
2162 	ret->tlsext_servername_callback = 0;
2163 	ret->tlsext_servername_arg = NULL;
2164 
2165 	/* Setup RFC4507 ticket keys */
2166 	arc4random_buf(ret->tlsext_tick_key_name, 16);
2167 	arc4random_buf(ret->tlsext_tick_hmac_key, 16);
2168 	arc4random_buf(ret->tlsext_tick_aes_key, 16);
2169 
2170 	ret->tlsext_status_cb = 0;
2171 	ret->tlsext_status_arg = NULL;
2172 
2173 	/*
2174 	 * Default is to connect to non-RI servers. When RI is more widely
2175 	 * deployed might change this.
2176 	 */
2177 	ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
2178 
2179 	return (ret);
2180  err:
2181 	SSLerrorx(ERR_R_MALLOC_FAILURE);
2182  err2:
2183 	SSL_CTX_free(ret);
2184 	return (NULL);
2185 }
2186 LSSL_ALIAS(SSL_CTX_new);
2187 
2188 void
SSL_CTX_free(SSL_CTX * ctx)2189 SSL_CTX_free(SSL_CTX *ctx)
2190 {
2191 	int	i;
2192 
2193 	if (ctx == NULL)
2194 		return;
2195 
2196 	i = CRYPTO_add(&ctx->references, -1, CRYPTO_LOCK_SSL_CTX);
2197 	if (i > 0)
2198 		return;
2199 
2200 	X509_VERIFY_PARAM_free(ctx->param);
2201 
2202 	/*
2203 	 * Free internal session cache. However: the remove_cb() may reference
2204 	 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
2205 	 * after the sessions were flushed.
2206 	 * As the ex_data handling routines might also touch the session cache,
2207 	 * the most secure solution seems to be: empty (flush) the cache, then
2208 	 * free ex_data, then finally free the cache.
2209 	 * (See ticket [openssl.org #212].)
2210 	 */
2211 	if (ctx->sessions != NULL)
2212 		SSL_CTX_flush_sessions(ctx, 0);
2213 
2214 	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ctx, &ctx->ex_data);
2215 
2216 	lh_SSL_SESSION_free(ctx->sessions);
2217 
2218 	X509_STORE_free(ctx->cert_store);
2219 	sk_SSL_CIPHER_free(ctx->cipher_list);
2220 	sk_SSL_CIPHER_free(ctx->cipher_list_tls13);
2221 	ssl_cert_free(ctx->cert);
2222 	sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
2223 	sk_X509_pop_free(ctx->extra_certs, X509_free);
2224 
2225 #ifndef OPENSSL_NO_SRTP
2226 	if (ctx->srtp_profiles)
2227 		sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
2228 #endif
2229 
2230 	free(ctx->tlsext_ecpointformatlist);
2231 	free(ctx->tlsext_supportedgroups);
2232 
2233 	free(ctx->alpn_client_proto_list);
2234 
2235 	free(ctx);
2236 }
2237 LSSL_ALIAS(SSL_CTX_free);
2238 
2239 int
SSL_CTX_up_ref(SSL_CTX * ctx)2240 SSL_CTX_up_ref(SSL_CTX *ctx)
2241 {
2242 	return CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX) > 1;
2243 }
2244 LSSL_ALIAS(SSL_CTX_up_ref);
2245 
2246 pem_password_cb *
SSL_CTX_get_default_passwd_cb(SSL_CTX * ctx)2247 SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx)
2248 {
2249 	return (ctx->default_passwd_callback);
2250 }
2251 LSSL_ALIAS(SSL_CTX_get_default_passwd_cb);
2252 
2253 void
SSL_CTX_set_default_passwd_cb(SSL_CTX * ctx,pem_password_cb * cb)2254 SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
2255 {
2256 	ctx->default_passwd_callback = cb;
2257 }
2258 LSSL_ALIAS(SSL_CTX_set_default_passwd_cb);
2259 
2260 void *
SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX * ctx)2261 SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx)
2262 {
2263 	return ctx->default_passwd_callback_userdata;
2264 }
2265 LSSL_ALIAS(SSL_CTX_get_default_passwd_cb_userdata);
2266 
2267 void
SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX * ctx,void * u)2268 SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u)
2269 {
2270 	ctx->default_passwd_callback_userdata = u;
2271 }
2272 LSSL_ALIAS(SSL_CTX_set_default_passwd_cb_userdata);
2273 
2274 void
SSL_CTX_set_cert_verify_callback(SSL_CTX * ctx,int (* cb)(X509_STORE_CTX *,void *),void * arg)2275 SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
2276     int (*cb)(X509_STORE_CTX *, void *), void *arg)
2277 {
2278 	ctx->app_verify_callback = cb;
2279 	ctx->app_verify_arg = arg;
2280 }
2281 LSSL_ALIAS(SSL_CTX_set_cert_verify_callback);
2282 
2283 void
SSL_CTX_set_verify(SSL_CTX * ctx,int mode,int (* cb)(int,X509_STORE_CTX *))2284 SSL_CTX_set_verify(SSL_CTX *ctx, int mode, int (*cb)(int, X509_STORE_CTX *))
2285 {
2286 	ctx->verify_mode = mode;
2287 	ctx->default_verify_callback = cb;
2288 }
2289 LSSL_ALIAS(SSL_CTX_set_verify);
2290 
2291 void
SSL_CTX_set_verify_depth(SSL_CTX * ctx,int depth)2292 SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth)
2293 {
2294 	X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2295 }
2296 LSSL_ALIAS(SSL_CTX_set_verify_depth);
2297 
2298 void
ssl_set_cert_masks(SSL_CERT * c,const SSL_CIPHER * cipher)2299 ssl_set_cert_masks(SSL_CERT *c, const SSL_CIPHER *cipher)
2300 {
2301 	unsigned long mask_a, mask_k;
2302 	SSL_CERT_PKEY *cpk;
2303 
2304 	if (c == NULL)
2305 		return;
2306 
2307 	mask_a = SSL_aNULL | SSL_aTLS1_3;
2308 	mask_k = SSL_kECDHE | SSL_kTLS1_3;
2309 
2310 	if (c->dhe_params != NULL || c->dhe_params_cb != NULL ||
2311 	    c->dhe_params_auto != 0)
2312 		mask_k |= SSL_kDHE;
2313 
2314 	cpk = &(c->pkeys[SSL_PKEY_ECC]);
2315 	if (cpk->x509 != NULL && cpk->privatekey != NULL) {
2316 		/* Key usage, if present, must allow signing. */
2317 		if (X509_get_key_usage(cpk->x509) & X509v3_KU_DIGITAL_SIGNATURE)
2318 			mask_a |= SSL_aECDSA;
2319 	}
2320 
2321 	cpk = &(c->pkeys[SSL_PKEY_RSA]);
2322 	if (cpk->x509 != NULL && cpk->privatekey != NULL) {
2323 		mask_a |= SSL_aRSA;
2324 		mask_k |= SSL_kRSA;
2325 	}
2326 
2327 	c->mask_k = mask_k;
2328 	c->mask_a = mask_a;
2329 	c->valid = 1;
2330 }
2331 
2332 /* See if this handshake is using an ECC cipher suite. */
2333 int
ssl_using_ecc_cipher(SSL * s)2334 ssl_using_ecc_cipher(SSL *s)
2335 {
2336 	unsigned long alg_a, alg_k;
2337 
2338 	alg_a = s->s3->hs.cipher->algorithm_auth;
2339 	alg_k = s->s3->hs.cipher->algorithm_mkey;
2340 
2341 	return s->session->tlsext_ecpointformatlist != NULL &&
2342 	    s->session->tlsext_ecpointformatlist_length > 0 &&
2343 	    ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA));
2344 }
2345 
2346 int
ssl_check_srvr_ecc_cert_and_alg(SSL * s,X509 * x)2347 ssl_check_srvr_ecc_cert_and_alg(SSL *s, X509 *x)
2348 {
2349 	const SSL_CIPHER *cs = s->s3->hs.cipher;
2350 	unsigned long alg_a;
2351 
2352 	alg_a = cs->algorithm_auth;
2353 
2354 	if (alg_a & SSL_aECDSA) {
2355 		/* Key usage, if present, must allow signing. */
2356 		if (!(X509_get_key_usage(x) & X509v3_KU_DIGITAL_SIGNATURE)) {
2357 			SSLerror(s, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2358 			return (0);
2359 		}
2360 	}
2361 
2362 	return (1);
2363 }
2364 
2365 SSL_CERT_PKEY *
ssl_get_server_send_pkey(const SSL * s)2366 ssl_get_server_send_pkey(const SSL *s)
2367 {
2368 	unsigned long alg_a;
2369 	SSL_CERT *c;
2370 	int i;
2371 
2372 	c = s->cert;
2373 	ssl_set_cert_masks(c, s->s3->hs.cipher);
2374 
2375 	alg_a = s->s3->hs.cipher->algorithm_auth;
2376 
2377 	if (alg_a & SSL_aECDSA) {
2378 		i = SSL_PKEY_ECC;
2379 	} else if (alg_a & SSL_aRSA) {
2380 		i = SSL_PKEY_RSA;
2381 	} else { /* if (alg_a & SSL_aNULL) */
2382 		SSLerror(s, ERR_R_INTERNAL_ERROR);
2383 		return (NULL);
2384 	}
2385 
2386 	return (c->pkeys + i);
2387 }
2388 
2389 EVP_PKEY *
ssl_get_sign_pkey(SSL * s,const SSL_CIPHER * cipher,const EVP_MD ** pmd,const struct ssl_sigalg ** sap)2390 ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher, const EVP_MD **pmd,
2391     const struct ssl_sigalg **sap)
2392 {
2393 	const struct ssl_sigalg *sigalg = NULL;
2394 	EVP_PKEY *pkey = NULL;
2395 	unsigned long alg_a;
2396 	SSL_CERT *c;
2397 	int idx = -1;
2398 
2399 	alg_a = cipher->algorithm_auth;
2400 	c = s->cert;
2401 
2402 	if (alg_a & SSL_aRSA) {
2403 		idx = SSL_PKEY_RSA;
2404 	} else if ((alg_a & SSL_aECDSA) &&
2405 	    (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2406 		idx = SSL_PKEY_ECC;
2407 	if (idx == -1) {
2408 		SSLerror(s, ERR_R_INTERNAL_ERROR);
2409 		return (NULL);
2410 	}
2411 
2412 	pkey = c->pkeys[idx].privatekey;
2413 	if ((sigalg = ssl_sigalg_select(s, pkey)) == NULL) {
2414 		SSLerror(s, SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2415 		return (NULL);
2416 	}
2417 	*pmd = sigalg->md();
2418 	*sap = sigalg;
2419 
2420 	return (pkey);
2421 }
2422 
2423 size_t
ssl_dhe_params_auto_key_bits(SSL * s)2424 ssl_dhe_params_auto_key_bits(SSL *s)
2425 {
2426 	SSL_CERT_PKEY *cpk;
2427 	int key_bits;
2428 
2429 	if (s->cert->dhe_params_auto == 2) {
2430 		key_bits = 1024;
2431 	} else if (s->s3->hs.cipher->algorithm_auth & SSL_aNULL) {
2432 		key_bits = 1024;
2433 		if (s->s3->hs.cipher->strength_bits == 256)
2434 			key_bits = 3072;
2435 	} else {
2436 		if ((cpk = ssl_get_server_send_pkey(s)) == NULL)
2437 			return 0;
2438 		if (cpk->privatekey == NULL ||
2439 		    EVP_PKEY_get0_RSA(cpk->privatekey) == NULL)
2440 			return 0;
2441 		if ((key_bits = EVP_PKEY_bits(cpk->privatekey)) <= 0)
2442 			return 0;
2443 	}
2444 
2445 	return key_bits;
2446 }
2447 
2448 static int
ssl_should_update_external_cache(SSL * s,int mode)2449 ssl_should_update_external_cache(SSL *s, int mode)
2450 {
2451 	int cache_mode;
2452 
2453 	cache_mode = s->session_ctx->session_cache_mode;
2454 
2455 	/* Don't cache if mode says not to */
2456 	if ((cache_mode & mode) == 0)
2457 		return 0;
2458 
2459 	/* if it is not already cached, cache it */
2460 	if (!s->hit)
2461 		return 1;
2462 
2463 	/* If it's TLS 1.3, do it to match OpenSSL */
2464 	if (s->s3->hs.negotiated_tls_version >= TLS1_3_VERSION)
2465 		return 1;
2466 
2467 	return 0;
2468 }
2469 
2470 static int
ssl_should_update_internal_cache(SSL * s,int mode)2471 ssl_should_update_internal_cache(SSL *s, int mode)
2472 {
2473 	int cache_mode;
2474 
2475 	cache_mode = s->session_ctx->session_cache_mode;
2476 
2477 	/* Don't cache if mode says not to */
2478 	if ((cache_mode & mode) == 0)
2479 		return 0;
2480 
2481 	/* If it is already cached, don't cache it again */
2482 	if (s->hit)
2483 		return 0;
2484 
2485 	if ((cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) != 0)
2486 		return 0;
2487 
2488 	/* If we are lesser than TLS 1.3, Cache it. */
2489 	if (s->s3->hs.negotiated_tls_version < TLS1_3_VERSION)
2490 		return 1;
2491 
2492 	/* Below this we consider TLS 1.3 or later */
2493 
2494 	/* If it's not a server, add it? OpenSSL does this. */
2495 	if (!s->server)
2496 		return 1;
2497 
2498 	/* XXX if we support early data / PSK need to add */
2499 
2500 	/*
2501 	 * If we have the remove session callback, we will want
2502 	 * to know about this even if it's a stateless ticket
2503 	 * from 1.3 so we can know when it is removed.
2504 	 */
2505 	if (s->session_ctx->remove_session_cb != NULL)
2506 		return 1;
2507 
2508 	/* If we have set OP_NO_TICKET, cache it. */
2509 	if ((s->options & SSL_OP_NO_TICKET) != 0)
2510 		return 1;
2511 
2512 	/* Otherwise do not cache */
2513 	return 0;
2514 }
2515 
2516 void
ssl_update_cache(SSL * s,int mode)2517 ssl_update_cache(SSL *s, int mode)
2518 {
2519 	int cache_mode, do_callback;
2520 
2521 	if (s->session->session_id_length == 0)
2522 		return;
2523 
2524 	cache_mode = s->session_ctx->session_cache_mode;
2525 	do_callback = ssl_should_update_external_cache(s, mode);
2526 
2527 	if (ssl_should_update_internal_cache(s, mode)) {
2528 		/*
2529 		 * XXX should we fail if the add to the internal cache
2530 		 * fails? OpenSSL doesn't care..
2531 		 */
2532 		(void) SSL_CTX_add_session(s->session_ctx, s->session);
2533 	}
2534 
2535 	/*
2536 	 * Update the "external cache" by calling the new session
2537 	 * callback if present, even with TLS 1.3 without early data
2538 	 * "because some application just want to know about the
2539 	 * creation of a session and aren't doing a full cache".
2540 	 * Apparently, if they are doing a full cache, they'll have
2541 	 * some fun, but we endeavour to give application writers the
2542 	 * same glorious experience they expect from OpenSSL which
2543 	 * does it this way.
2544 	 */
2545 	if (do_callback && s->session_ctx->new_session_cb != NULL) {
2546 		    CRYPTO_add(&s->session->references, 1, CRYPTO_LOCK_SSL_SESSION);
2547 		    if (!s->session_ctx->new_session_cb(s, s->session))
2548 			    SSL_SESSION_free(s->session);
2549 	}
2550 
2551 	/* Auto flush every 255 connections. */
2552 	if (!(cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
2553 	    (cache_mode & mode) != 0) {
2554 		int connections;
2555 		if (mode & SSL_SESS_CACHE_CLIENT)
2556 			connections = s->session_ctx->stats.sess_connect_good;
2557 		else
2558 			connections = s->session_ctx->stats.sess_accept_good;
2559 		if ((connections & 0xff) == 0xff)
2560 			SSL_CTX_flush_sessions(s->session_ctx, time(NULL));
2561 	}
2562 }
2563 
2564 const SSL_METHOD *
SSL_get_ssl_method(SSL * s)2565 SSL_get_ssl_method(SSL *s)
2566 {
2567 	return (s->method);
2568 }
2569 LSSL_ALIAS(SSL_get_ssl_method);
2570 
2571 int
SSL_set_ssl_method(SSL * s,const SSL_METHOD * method)2572 SSL_set_ssl_method(SSL *s, const SSL_METHOD *method)
2573 {
2574 	int (*handshake_func)(SSL *) = NULL;
2575 	int ret = 1;
2576 
2577 	if (s->method == method)
2578 		return (ret);
2579 
2580 	if (s->handshake_func == s->method->ssl_connect)
2581 		handshake_func = method->ssl_connect;
2582 	else if (s->handshake_func == s->method->ssl_accept)
2583 		handshake_func = method->ssl_accept;
2584 
2585 	if (s->method->version == method->version) {
2586 		s->method = method;
2587 	} else {
2588 		s->method->ssl_free(s);
2589 		s->method = method;
2590 		ret = s->method->ssl_new(s);
2591 	}
2592 	s->handshake_func = handshake_func;
2593 
2594 	return (ret);
2595 }
2596 LSSL_ALIAS(SSL_set_ssl_method);
2597 
2598 int
SSL_get_error(const SSL * s,int i)2599 SSL_get_error(const SSL *s, int i)
2600 {
2601 	unsigned long l;
2602 	int reason;
2603 	BIO *bio;
2604 
2605 	if (i > 0)
2606 		return (SSL_ERROR_NONE);
2607 
2608 	/*
2609 	 * Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2610 	 * etc, where we do encode the error.
2611 	 */
2612 	if ((l = ERR_peek_error()) != 0) {
2613 		if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2614 			return (SSL_ERROR_SYSCALL);
2615 		else
2616 			return (SSL_ERROR_SSL);
2617 	}
2618 
2619 	if (SSL_want_read(s)) {
2620 		bio = SSL_get_rbio(s);
2621 		if (BIO_should_read(bio)) {
2622 			return (SSL_ERROR_WANT_READ);
2623 		} else if (BIO_should_write(bio)) {
2624 			/*
2625 			 * This one doesn't make too much sense...  We never
2626 			 * try to write to the rbio, and an application
2627 			 * program where rbio and wbio are separate couldn't
2628 			 * even know what it should wait for.  However if we
2629 			 * ever set s->rwstate incorrectly (so that we have
2630 			 * SSL_want_read(s) instead of SSL_want_write(s))
2631 			 * and rbio and wbio *are* the same, this test works
2632 			 * around that bug; so it might be safer to keep it.
2633 			 */
2634 			return (SSL_ERROR_WANT_WRITE);
2635 		} else if (BIO_should_io_special(bio)) {
2636 			reason = BIO_get_retry_reason(bio);
2637 			if (reason == BIO_RR_CONNECT)
2638 				return (SSL_ERROR_WANT_CONNECT);
2639 			else if (reason == BIO_RR_ACCEPT)
2640 				return (SSL_ERROR_WANT_ACCEPT);
2641 			else
2642 				return (SSL_ERROR_SYSCALL); /* unknown */
2643 		}
2644 	}
2645 
2646 	if (SSL_want_write(s)) {
2647 		bio = SSL_get_wbio(s);
2648 		if (BIO_should_write(bio)) {
2649 			return (SSL_ERROR_WANT_WRITE);
2650 		} else if (BIO_should_read(bio)) {
2651 			/*
2652 			 * See above (SSL_want_read(s) with
2653 			 * BIO_should_write(bio))
2654 			 */
2655 			return (SSL_ERROR_WANT_READ);
2656 		} else if (BIO_should_io_special(bio)) {
2657 			reason = BIO_get_retry_reason(bio);
2658 			if (reason == BIO_RR_CONNECT)
2659 				return (SSL_ERROR_WANT_CONNECT);
2660 			else if (reason == BIO_RR_ACCEPT)
2661 				return (SSL_ERROR_WANT_ACCEPT);
2662 			else
2663 				return (SSL_ERROR_SYSCALL);
2664 		}
2665 	}
2666 
2667 	if (SSL_want_x509_lookup(s))
2668 		return (SSL_ERROR_WANT_X509_LOOKUP);
2669 
2670 	if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2671 	    (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
2672 		return (SSL_ERROR_ZERO_RETURN);
2673 
2674 	return (SSL_ERROR_SYSCALL);
2675 }
2676 LSSL_ALIAS(SSL_get_error);
2677 
2678 int
SSL_CTX_set_quic_method(SSL_CTX * ctx,const SSL_QUIC_METHOD * quic_method)2679 SSL_CTX_set_quic_method(SSL_CTX *ctx, const SSL_QUIC_METHOD *quic_method)
2680 {
2681 	if (ctx->method->dtls)
2682 		return 0;
2683 
2684 	ctx->quic_method = quic_method;
2685 
2686 	return 1;
2687 }
2688 LSSL_ALIAS(SSL_CTX_set_quic_method);
2689 
2690 int
SSL_set_quic_method(SSL * ssl,const SSL_QUIC_METHOD * quic_method)2691 SSL_set_quic_method(SSL *ssl, const SSL_QUIC_METHOD *quic_method)
2692 {
2693 	if (ssl->method->dtls)
2694 		return 0;
2695 
2696 	ssl->quic_method = quic_method;
2697 
2698 	return 1;
2699 }
2700 LSSL_ALIAS(SSL_set_quic_method);
2701 
2702 size_t
SSL_quic_max_handshake_flight_len(const SSL * ssl,enum ssl_encryption_level_t level)2703 SSL_quic_max_handshake_flight_len(const SSL *ssl,
2704     enum ssl_encryption_level_t level)
2705 {
2706 	size_t flight_len;
2707 
2708 	/* Limit flights to 16K when there are no large certificate messages. */
2709 	flight_len = 16384;
2710 
2711 	switch (level) {
2712 	case ssl_encryption_initial:
2713 		return flight_len;
2714 
2715 	case ssl_encryption_early_data:
2716 		/* QUIC does not send EndOfEarlyData. */
2717 		return 0;
2718 
2719 	case ssl_encryption_handshake:
2720 		if (ssl->server) {
2721 			/*
2722 			 * Servers may receive Certificate message if configured
2723 			 * to request client certificates.
2724 			 */
2725 			if ((SSL_get_verify_mode(ssl) & SSL_VERIFY_PEER) != 0 &&
2726 			    ssl->max_cert_list > flight_len)
2727 				flight_len = ssl->max_cert_list;
2728 		} else {
2729 			/*
2730 			 * Clients may receive both Certificate message and a
2731 			 * CertificateRequest message.
2732 			 */
2733 			if (ssl->max_cert_list * 2 > flight_len)
2734 				flight_len = ssl->max_cert_list * 2;
2735 		}
2736 		return flight_len;
2737 	case ssl_encryption_application:
2738 		/*
2739 		 * Note there is not actually a bound on the number of
2740 		 * NewSessionTickets one may send in a row. This level may need
2741 		 * more involved flow control.
2742 		 */
2743 		return flight_len;
2744 	}
2745 
2746 	return 0;
2747 }
2748 LSSL_ALIAS(SSL_quic_max_handshake_flight_len);
2749 
2750 enum ssl_encryption_level_t
SSL_quic_read_level(const SSL * ssl)2751 SSL_quic_read_level(const SSL *ssl)
2752 {
2753 	return ssl->s3->hs.tls13.quic_read_level;
2754 }
2755 LSSL_ALIAS(SSL_quic_read_level);
2756 
2757 enum ssl_encryption_level_t
SSL_quic_write_level(const SSL * ssl)2758 SSL_quic_write_level(const SSL *ssl)
2759 {
2760 	return ssl->s3->hs.tls13.quic_write_level;
2761 }
2762 LSSL_ALIAS(SSL_quic_write_level);
2763 
2764 int
SSL_provide_quic_data(SSL * ssl,enum ssl_encryption_level_t level,const uint8_t * data,size_t len)2765 SSL_provide_quic_data(SSL *ssl, enum ssl_encryption_level_t level,
2766     const uint8_t *data, size_t len)
2767 {
2768 	if (!SSL_is_quic(ssl)) {
2769 		SSLerror(ssl, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2770 		return 0;
2771 	}
2772 
2773 	if (level != SSL_quic_read_level(ssl)) {
2774 		SSLerror(ssl, SSL_R_WRONG_ENCRYPTION_LEVEL_RECEIVED);
2775 		return 0;
2776 	}
2777 
2778 	if (ssl->s3->hs.tls13.quic_read_buffer == NULL) {
2779 		ssl->s3->hs.tls13.quic_read_buffer = tls_buffer_new(0);
2780 		if (ssl->s3->hs.tls13.quic_read_buffer == NULL) {
2781 			SSLerror(ssl, ERR_R_MALLOC_FAILURE);
2782 			return 0;
2783 		}
2784 	}
2785 
2786 	/* XXX - note that this does not currently downsize. */
2787 	tls_buffer_set_capacity_limit(ssl->s3->hs.tls13.quic_read_buffer,
2788 	    SSL_quic_max_handshake_flight_len(ssl, level));
2789 
2790 	/*
2791 	 * XXX - an append that fails due to exceeding capacity should set
2792 	 * SSL_R_EXCESSIVE_MESSAGE_SIZE.
2793 	 */
2794 	return tls_buffer_append(ssl->s3->hs.tls13.quic_read_buffer, data, len);
2795 }
2796 LSSL_ALIAS(SSL_provide_quic_data);
2797 
2798 int
SSL_process_quic_post_handshake(SSL * ssl)2799 SSL_process_quic_post_handshake(SSL *ssl)
2800 {
2801 	/* XXX - this needs to run PHH received. */
2802 	return 1;
2803 }
2804 LSSL_ALIAS(SSL_process_quic_post_handshake);
2805 
2806 int
SSL_do_handshake(SSL * s)2807 SSL_do_handshake(SSL *s)
2808 {
2809 	if (s->handshake_func == NULL) {
2810 		SSLerror(s, SSL_R_CONNECTION_TYPE_NOT_SET);
2811 		return (-1);
2812 	}
2813 
2814 	s->method->ssl_renegotiate_check(s);
2815 
2816 	if (!SSL_in_init(s) && !SSL_in_before(s))
2817 		return 1;
2818 
2819 	return s->handshake_func(s);
2820 }
2821 LSSL_ALIAS(SSL_do_handshake);
2822 
2823 /*
2824  * For the next 2 functions, SSL_clear() sets shutdown and so
2825  * one of these calls will reset it
2826  */
2827 void
SSL_set_accept_state(SSL * s)2828 SSL_set_accept_state(SSL *s)
2829 {
2830 	s->server = 1;
2831 	s->shutdown = 0;
2832 	s->s3->hs.state = SSL_ST_ACCEPT|SSL_ST_BEFORE;
2833 	s->handshake_func = s->method->ssl_accept;
2834 	ssl_clear_cipher_state(s);
2835 }
2836 LSSL_ALIAS(SSL_set_accept_state);
2837 
2838 void
SSL_set_connect_state(SSL * s)2839 SSL_set_connect_state(SSL *s)
2840 {
2841 	s->server = 0;
2842 	s->shutdown = 0;
2843 	s->s3->hs.state = SSL_ST_CONNECT|SSL_ST_BEFORE;
2844 	s->handshake_func = s->method->ssl_connect;
2845 	ssl_clear_cipher_state(s);
2846 }
2847 LSSL_ALIAS(SSL_set_connect_state);
2848 
2849 int
ssl_undefined_function(SSL * s)2850 ssl_undefined_function(SSL *s)
2851 {
2852 	SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2853 	return (0);
2854 }
2855 
2856 int
ssl_undefined_void_function(void)2857 ssl_undefined_void_function(void)
2858 {
2859 	SSLerrorx(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2860 	return (0);
2861 }
2862 
2863 int
ssl_undefined_const_function(const SSL * s)2864 ssl_undefined_const_function(const SSL *s)
2865 {
2866 	SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2867 	return (0);
2868 }
2869 
2870 const char *
ssl_version_string(int ver)2871 ssl_version_string(int ver)
2872 {
2873 	switch (ver) {
2874 	case TLS1_VERSION:
2875 		return (SSL_TXT_TLSV1);
2876 	case TLS1_1_VERSION:
2877 		return (SSL_TXT_TLSV1_1);
2878 	case TLS1_2_VERSION:
2879 		return (SSL_TXT_TLSV1_2);
2880 	case TLS1_3_VERSION:
2881 		return (SSL_TXT_TLSV1_3);
2882 	case DTLS1_VERSION:
2883 		return (SSL_TXT_DTLS1);
2884 	case DTLS1_2_VERSION:
2885 		return (SSL_TXT_DTLS1_2);
2886 	default:
2887 		return ("unknown");
2888 	}
2889 }
2890 
2891 const char *
SSL_get_version(const SSL * s)2892 SSL_get_version(const SSL *s)
2893 {
2894 	return ssl_version_string(s->version);
2895 }
2896 LSSL_ALIAS(SSL_get_version);
2897 
2898 SSL *
SSL_dup(SSL * s)2899 SSL_dup(SSL *s)
2900 {
2901 	STACK_OF(X509_NAME) *sk;
2902 	X509_NAME *xn;
2903 	SSL *ret;
2904 	int i;
2905 
2906 	if ((ret = SSL_new(SSL_get_SSL_CTX(s))) == NULL)
2907 		goto err;
2908 
2909 	ret->version = s->version;
2910 	ret->method = s->method;
2911 
2912 	if (s->session != NULL) {
2913 		if (!SSL_copy_session_id(ret, s))
2914 			goto err;
2915 	} else {
2916 		/*
2917 		 * No session has been established yet, so we have to expect
2918 		 * that s->cert or ret->cert will be changed later --
2919 		 * they should not both point to the same object,
2920 		 * and thus we can't use SSL_copy_session_id.
2921 		 */
2922 
2923 		ret->method->ssl_free(ret);
2924 		ret->method = s->method;
2925 		ret->method->ssl_new(ret);
2926 
2927 		ssl_cert_free(ret->cert);
2928 		if ((ret->cert = ssl_cert_dup(s->cert)) == NULL)
2929 			goto err;
2930 
2931 		if (!SSL_set_session_id_context(ret, s->sid_ctx,
2932 		    s->sid_ctx_length))
2933 			goto err;
2934 	}
2935 
2936 	ret->options = s->options;
2937 	ret->mode = s->mode;
2938 	SSL_set_max_cert_list(ret, SSL_get_max_cert_list(s));
2939 	SSL_set_read_ahead(ret, SSL_get_read_ahead(s));
2940 	ret->msg_callback = s->msg_callback;
2941 	ret->msg_callback_arg = s->msg_callback_arg;
2942 	SSL_set_verify(ret, SSL_get_verify_mode(s),
2943 	SSL_get_verify_callback(s));
2944 	SSL_set_verify_depth(ret, SSL_get_verify_depth(s));
2945 	ret->generate_session_id = s->generate_session_id;
2946 
2947 	SSL_set_info_callback(ret, SSL_get_info_callback(s));
2948 
2949 	/* copy app data, a little dangerous perhaps */
2950 	if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL,
2951 	    &ret->ex_data, &s->ex_data))
2952 		goto err;
2953 
2954 	/* setup rbio, and wbio */
2955 	if (s->rbio != NULL) {
2956 		if (!BIO_dup_state(s->rbio,(char *)&ret->rbio))
2957 			goto err;
2958 	}
2959 	if (s->wbio != NULL) {
2960 		if (s->wbio != s->rbio) {
2961 			if (!BIO_dup_state(s->wbio,(char *)&ret->wbio))
2962 				goto err;
2963 		} else
2964 			ret->wbio = ret->rbio;
2965 	}
2966 	ret->rwstate = s->rwstate;
2967 	ret->in_handshake = s->in_handshake;
2968 	ret->handshake_func = s->handshake_func;
2969 	ret->server = s->server;
2970 	ret->renegotiate = s->renegotiate;
2971 	ret->new_session = s->new_session;
2972 	ret->quiet_shutdown = s->quiet_shutdown;
2973 	ret->shutdown = s->shutdown;
2974 	/* SSL_dup does not really work at any state, though */
2975 	ret->s3->hs.state = s->s3->hs.state;
2976 	ret->rstate = s->rstate;
2977 
2978 	/*
2979 	 * Would have to copy ret->init_buf, ret->init_msg, ret->init_num,
2980 	 * ret->init_off
2981 	 */
2982 	ret->init_num = 0;
2983 
2984 	ret->hit = s->hit;
2985 
2986 	X509_VERIFY_PARAM_inherit(ret->param, s->param);
2987 
2988 	if (s->cipher_list != NULL) {
2989 		if ((ret->cipher_list =
2990 		    sk_SSL_CIPHER_dup(s->cipher_list)) == NULL)
2991 			goto err;
2992 	}
2993 	if (s->cipher_list_tls13 != NULL) {
2994 		if ((ret->cipher_list_tls13 =
2995 		    sk_SSL_CIPHER_dup(s->cipher_list_tls13)) == NULL)
2996 			goto err;
2997 	}
2998 
2999 	/* Dup the client_CA list */
3000 	if (s->client_CA != NULL) {
3001 		if ((sk = sk_X509_NAME_dup(s->client_CA)) == NULL) goto err;
3002 			ret->client_CA = sk;
3003 		for (i = 0; i < sk_X509_NAME_num(sk); i++) {
3004 			xn = sk_X509_NAME_value(sk, i);
3005 			if (sk_X509_NAME_set(sk, i,
3006 			    X509_NAME_dup(xn)) == NULL) {
3007 				X509_NAME_free(xn);
3008 				goto err;
3009 			}
3010 		}
3011 	}
3012 
3013 	return ret;
3014  err:
3015 	SSL_free(ret);
3016 	return NULL;
3017 }
3018 LSSL_ALIAS(SSL_dup);
3019 
3020 void
ssl_clear_cipher_state(SSL * s)3021 ssl_clear_cipher_state(SSL *s)
3022 {
3023 	tls12_record_layer_clear_read_state(s->rl);
3024 	tls12_record_layer_clear_write_state(s->rl);
3025 }
3026 
3027 void
ssl_info_callback(const SSL * s,int type,int value)3028 ssl_info_callback(const SSL *s, int type, int value)
3029 {
3030 	ssl_info_callback_fn *cb;
3031 
3032 	if ((cb = s->info_callback) == NULL)
3033 		cb = s->ctx->info_callback;
3034 	if (cb != NULL)
3035 		cb(s, type, value);
3036 }
3037 
3038 void
ssl_msg_callback(SSL * s,int is_write,int content_type,const void * msg_buf,size_t msg_len)3039 ssl_msg_callback(SSL *s, int is_write, int content_type,
3040     const void *msg_buf, size_t msg_len)
3041 {
3042 	if (s->msg_callback == NULL)
3043 		return;
3044 
3045 	s->msg_callback(is_write, s->version, content_type,
3046 	    msg_buf, msg_len, s, s->msg_callback_arg);
3047 }
3048 
3049 void
ssl_msg_callback_cbs(SSL * s,int is_write,int content_type,CBS * cbs)3050 ssl_msg_callback_cbs(SSL *s, int is_write, int content_type, CBS *cbs)
3051 {
3052 	ssl_msg_callback(s, is_write, content_type, CBS_data(cbs), CBS_len(cbs));
3053 }
3054 
3055 /* Fix this function so that it takes an optional type parameter */
3056 X509 *
SSL_get_certificate(const SSL * s)3057 SSL_get_certificate(const SSL *s)
3058 {
3059 	return (s->cert->key->x509);
3060 }
3061 LSSL_ALIAS(SSL_get_certificate);
3062 
3063 /* Fix this function so that it takes an optional type parameter */
3064 EVP_PKEY *
SSL_get_privatekey(const SSL * s)3065 SSL_get_privatekey(const SSL *s)
3066 {
3067 	return (s->cert->key->privatekey);
3068 }
3069 LSSL_ALIAS(SSL_get_privatekey);
3070 
3071 const SSL_CIPHER *
SSL_get_current_cipher(const SSL * s)3072 SSL_get_current_cipher(const SSL *s)
3073 {
3074 	return s->s3->hs.cipher;
3075 }
3076 LSSL_ALIAS(SSL_get_current_cipher);
3077 
3078 const void *
SSL_get_current_compression(SSL * s)3079 SSL_get_current_compression(SSL *s)
3080 {
3081 	return (NULL);
3082 }
3083 LSSL_ALIAS(SSL_get_current_compression);
3084 
3085 const void *
SSL_get_current_expansion(SSL * s)3086 SSL_get_current_expansion(SSL *s)
3087 {
3088 	return (NULL);
3089 }
3090 LSSL_ALIAS(SSL_get_current_expansion);
3091 
3092 size_t
SSL_get_client_random(const SSL * s,unsigned char * out,size_t max_out)3093 SSL_get_client_random(const SSL *s, unsigned char *out, size_t max_out)
3094 {
3095 	size_t len = sizeof(s->s3->client_random);
3096 
3097 	if (out == NULL)
3098 		return len;
3099 
3100 	if (len > max_out)
3101 		len = max_out;
3102 
3103 	memcpy(out, s->s3->client_random, len);
3104 
3105 	return len;
3106 }
3107 LSSL_ALIAS(SSL_get_client_random);
3108 
3109 size_t
SSL_get_server_random(const SSL * s,unsigned char * out,size_t max_out)3110 SSL_get_server_random(const SSL *s, unsigned char *out, size_t max_out)
3111 {
3112 	size_t len = sizeof(s->s3->server_random);
3113 
3114 	if (out == NULL)
3115 		return len;
3116 
3117 	if (len > max_out)
3118 		len = max_out;
3119 
3120 	memcpy(out, s->s3->server_random, len);
3121 
3122 	return len;
3123 }
3124 LSSL_ALIAS(SSL_get_server_random);
3125 
3126 int
ssl_init_wbio_buffer(SSL * s,int push)3127 ssl_init_wbio_buffer(SSL *s, int push)
3128 {
3129 	BIO	*bbio;
3130 
3131 	if (s->bbio == NULL) {
3132 		bbio = BIO_new(BIO_f_buffer());
3133 		if (bbio == NULL)
3134 			return (0);
3135 		s->bbio = bbio;
3136 	} else {
3137 		bbio = s->bbio;
3138 		if (s->bbio == s->wbio)
3139 			s->wbio = BIO_pop(s->wbio);
3140 	}
3141 	(void)BIO_reset(bbio);
3142 /*	if (!BIO_set_write_buffer_size(bbio,16*1024)) */
3143 	if (!BIO_set_read_buffer_size(bbio, 1)) {
3144 		SSLerror(s, ERR_R_BUF_LIB);
3145 		return (0);
3146 	}
3147 	if (push) {
3148 		if (s->wbio != bbio)
3149 			s->wbio = BIO_push(bbio, s->wbio);
3150 	} else {
3151 		if (s->wbio == bbio)
3152 			s->wbio = BIO_pop(bbio);
3153 	}
3154 	return (1);
3155 }
3156 
3157 void
ssl_free_wbio_buffer(SSL * s)3158 ssl_free_wbio_buffer(SSL *s)
3159 {
3160 	if (s == NULL)
3161 		return;
3162 
3163 	if (s->bbio == NULL)
3164 		return;
3165 
3166 	if (s->bbio == s->wbio) {
3167 		/* remove buffering */
3168 		s->wbio = BIO_pop(s->wbio);
3169 	}
3170 	BIO_free(s->bbio);
3171 	s->bbio = NULL;
3172 }
3173 
3174 void
SSL_CTX_set_quiet_shutdown(SSL_CTX * ctx,int mode)3175 SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode)
3176 {
3177 	ctx->quiet_shutdown = mode;
3178 }
3179 LSSL_ALIAS(SSL_CTX_set_quiet_shutdown);
3180 
3181 int
SSL_CTX_get_quiet_shutdown(const SSL_CTX * ctx)3182 SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
3183 {
3184 	return (ctx->quiet_shutdown);
3185 }
3186 LSSL_ALIAS(SSL_CTX_get_quiet_shutdown);
3187 
3188 void
SSL_set_quiet_shutdown(SSL * s,int mode)3189 SSL_set_quiet_shutdown(SSL *s, int mode)
3190 {
3191 	s->quiet_shutdown = mode;
3192 }
3193 LSSL_ALIAS(SSL_set_quiet_shutdown);
3194 
3195 int
SSL_get_quiet_shutdown(const SSL * s)3196 SSL_get_quiet_shutdown(const SSL *s)
3197 {
3198 	return (s->quiet_shutdown);
3199 }
3200 LSSL_ALIAS(SSL_get_quiet_shutdown);
3201 
3202 void
SSL_set_shutdown(SSL * s,int mode)3203 SSL_set_shutdown(SSL *s, int mode)
3204 {
3205 	s->shutdown = mode;
3206 }
3207 LSSL_ALIAS(SSL_set_shutdown);
3208 
3209 int
SSL_get_shutdown(const SSL * s)3210 SSL_get_shutdown(const SSL *s)
3211 {
3212 	return (s->shutdown);
3213 }
3214 LSSL_ALIAS(SSL_get_shutdown);
3215 
3216 int
SSL_version(const SSL * s)3217 SSL_version(const SSL *s)
3218 {
3219 	return (s->version);
3220 }
3221 LSSL_ALIAS(SSL_version);
3222 
3223 SSL_CTX *
SSL_get_SSL_CTX(const SSL * ssl)3224 SSL_get_SSL_CTX(const SSL *ssl)
3225 {
3226 	return (ssl->ctx);
3227 }
3228 LSSL_ALIAS(SSL_get_SSL_CTX);
3229 
3230 SSL_CTX *
SSL_set_SSL_CTX(SSL * ssl,SSL_CTX * ctx)3231 SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
3232 {
3233 	SSL_CERT *new_cert;
3234 
3235 	if (ctx == NULL)
3236 		ctx = ssl->initial_ctx;
3237 	if (ssl->ctx == ctx)
3238 		return (ssl->ctx);
3239 
3240 	if ((new_cert = ssl_cert_dup(ctx->cert)) == NULL)
3241 		return NULL;
3242 	ssl_cert_free(ssl->cert);
3243 	ssl->cert = new_cert;
3244 
3245 	SSL_CTX_up_ref(ctx);
3246 	SSL_CTX_free(ssl->ctx); /* decrement reference count */
3247 	ssl->ctx = ctx;
3248 
3249 	return (ssl->ctx);
3250 }
3251 LSSL_ALIAS(SSL_set_SSL_CTX);
3252 
3253 int
SSL_CTX_set_default_verify_paths(SSL_CTX * ctx)3254 SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
3255 {
3256 	return (X509_STORE_set_default_paths(ctx->cert_store));
3257 }
3258 LSSL_ALIAS(SSL_CTX_set_default_verify_paths);
3259 
3260 int
SSL_CTX_load_verify_locations(SSL_CTX * ctx,const char * CAfile,const char * CApath)3261 SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
3262     const char *CApath)
3263 {
3264 	return (X509_STORE_load_locations(ctx->cert_store, CAfile, CApath));
3265 }
3266 LSSL_ALIAS(SSL_CTX_load_verify_locations);
3267 
3268 int
SSL_CTX_load_verify_mem(SSL_CTX * ctx,void * buf,int len)3269 SSL_CTX_load_verify_mem(SSL_CTX *ctx, void *buf, int len)
3270 {
3271 	return (X509_STORE_load_mem(ctx->cert_store, buf, len));
3272 }
3273 LSSL_ALIAS(SSL_CTX_load_verify_mem);
3274 
3275 void
SSL_set_info_callback(SSL * ssl,void (* cb)(const SSL * ssl,int type,int val))3276 SSL_set_info_callback(SSL *ssl, void (*cb)(const SSL *ssl, int type, int val))
3277 {
3278 	ssl->info_callback = cb;
3279 }
3280 LSSL_ALIAS(SSL_set_info_callback);
3281 
SSL_get_info_callback(const SSL * ssl)3282 void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type, int val)
3283 {
3284 	return (ssl->info_callback);
3285 }
3286 LSSL_ALIAS(SSL_get_info_callback);
3287 
3288 int
SSL_state(const SSL * ssl)3289 SSL_state(const SSL *ssl)
3290 {
3291 	return (ssl->s3->hs.state);
3292 }
3293 LSSL_ALIAS(SSL_state);
3294 
3295 void
SSL_set_state(SSL * ssl,int state)3296 SSL_set_state(SSL *ssl, int state)
3297 {
3298 	ssl->s3->hs.state = state;
3299 }
3300 LSSL_ALIAS(SSL_set_state);
3301 
3302 void
SSL_set_verify_result(SSL * ssl,long arg)3303 SSL_set_verify_result(SSL *ssl, long arg)
3304 {
3305 	ssl->verify_result = arg;
3306 }
3307 LSSL_ALIAS(SSL_set_verify_result);
3308 
3309 long
SSL_get_verify_result(const SSL * ssl)3310 SSL_get_verify_result(const SSL *ssl)
3311 {
3312 	return (ssl->verify_result);
3313 }
3314 LSSL_ALIAS(SSL_get_verify_result);
3315 
3316 int
SSL_verify_client_post_handshake(SSL * ssl)3317 SSL_verify_client_post_handshake(SSL *ssl)
3318 {
3319 	return 0;
3320 }
3321 LSSL_ALIAS(SSL_verify_client_post_handshake);
3322 
3323 void
SSL_CTX_set_post_handshake_auth(SSL_CTX * ctx,int val)3324 SSL_CTX_set_post_handshake_auth(SSL_CTX *ctx, int val)
3325 {
3326 	return;
3327 }
3328 LSSL_ALIAS(SSL_CTX_set_post_handshake_auth);
3329 
3330 void
SSL_set_post_handshake_auth(SSL * ssl,int val)3331 SSL_set_post_handshake_auth(SSL *ssl, int val)
3332 {
3333 	return;
3334 }
3335 LSSL_ALIAS(SSL_set_post_handshake_auth);
3336 
3337 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)3338 SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
3339     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
3340 {
3341 	return (CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
3342 	    new_func, dup_func, free_func));
3343 }
3344 LSSL_ALIAS(SSL_get_ex_new_index);
3345 
3346 int
SSL_set_ex_data(SSL * s,int idx,void * arg)3347 SSL_set_ex_data(SSL *s, int idx, void *arg)
3348 {
3349 	return (CRYPTO_set_ex_data(&s->ex_data, idx, arg));
3350 }
3351 LSSL_ALIAS(SSL_set_ex_data);
3352 
3353 void *
SSL_get_ex_data(const SSL * s,int idx)3354 SSL_get_ex_data(const SSL *s, int idx)
3355 {
3356 	return (CRYPTO_get_ex_data(&s->ex_data, idx));
3357 }
3358 LSSL_ALIAS(SSL_get_ex_data);
3359 
3360 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)3361 SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
3362     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
3363 {
3364 	return (CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
3365 	    new_func, dup_func, free_func));
3366 }
3367 LSSL_ALIAS(SSL_CTX_get_ex_new_index);
3368 
3369 int
SSL_CTX_set_ex_data(SSL_CTX * s,int idx,void * arg)3370 SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg)
3371 {
3372 	return (CRYPTO_set_ex_data(&s->ex_data, idx, arg));
3373 }
3374 LSSL_ALIAS(SSL_CTX_set_ex_data);
3375 
3376 void *
SSL_CTX_get_ex_data(const SSL_CTX * s,int idx)3377 SSL_CTX_get_ex_data(const SSL_CTX *s, int idx)
3378 {
3379 	return (CRYPTO_get_ex_data(&s->ex_data, idx));
3380 }
3381 LSSL_ALIAS(SSL_CTX_get_ex_data);
3382 
3383 int
ssl_ok(SSL * s)3384 ssl_ok(SSL *s)
3385 {
3386 	return (1);
3387 }
3388 
3389 X509_STORE *
SSL_CTX_get_cert_store(const SSL_CTX * ctx)3390 SSL_CTX_get_cert_store(const SSL_CTX *ctx)
3391 {
3392 	return (ctx->cert_store);
3393 }
3394 LSSL_ALIAS(SSL_CTX_get_cert_store);
3395 
3396 void
SSL_CTX_set_cert_store(SSL_CTX * ctx,X509_STORE * store)3397 SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store)
3398 {
3399 	X509_STORE_free(ctx->cert_store);
3400 	ctx->cert_store = store;
3401 }
3402 LSSL_ALIAS(SSL_CTX_set_cert_store);
3403 
3404 void
SSL_CTX_set1_cert_store(SSL_CTX * ctx,X509_STORE * store)3405 SSL_CTX_set1_cert_store(SSL_CTX *ctx, X509_STORE *store)
3406 {
3407 	if (store != NULL)
3408 		X509_STORE_up_ref(store);
3409 
3410 	SSL_CTX_set_cert_store(ctx, store);
3411 }
3412 LSSL_ALIAS(SSL_CTX_set1_cert_store);
3413 
3414 X509 *
SSL_CTX_get0_certificate(const SSL_CTX * ctx)3415 SSL_CTX_get0_certificate(const SSL_CTX *ctx)
3416 {
3417 	if (ctx->cert == NULL)
3418 		return NULL;
3419 
3420 	return ctx->cert->key->x509;
3421 }
3422 LSSL_ALIAS(SSL_CTX_get0_certificate);
3423 
3424 EVP_PKEY *
SSL_CTX_get0_privatekey(const SSL_CTX * ctx)3425 SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
3426 {
3427 	if (ctx->cert == NULL)
3428 		return NULL;
3429 
3430 	return ctx->cert->key->privatekey;
3431 }
3432 LSSL_ALIAS(SSL_CTX_get0_privatekey);
3433 
3434 int
SSL_want(const SSL * s)3435 SSL_want(const SSL *s)
3436 {
3437 	return (s->rwstate);
3438 }
3439 LSSL_ALIAS(SSL_want);
3440 
3441 void
SSL_CTX_set_tmp_rsa_callback(SSL_CTX * ctx,RSA * (* cb)(SSL * ssl,int is_export,int keylength))3442 SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx, RSA *(*cb)(SSL *ssl, int is_export,
3443     int keylength))
3444 {
3445 	SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
3446 }
3447 LSSL_ALIAS(SSL_CTX_set_tmp_rsa_callback);
3448 
3449 void
SSL_set_tmp_rsa_callback(SSL * ssl,RSA * (* cb)(SSL * ssl,int is_export,int keylength))3450 SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
3451     int keylength))
3452 {
3453 	SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
3454 }
3455 LSSL_ALIAS(SSL_set_tmp_rsa_callback);
3456 
3457 void
SSL_CTX_set_tmp_dh_callback(SSL_CTX * ctx,DH * (* dh)(SSL * ssl,int is_export,int keylength))3458 SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx, DH *(*dh)(SSL *ssl, int is_export,
3459     int keylength))
3460 {
3461 	SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
3462 }
3463 LSSL_ALIAS(SSL_CTX_set_tmp_dh_callback);
3464 
3465 void
SSL_set_tmp_dh_callback(SSL * ssl,DH * (* dh)(SSL * ssl,int is_export,int keylength))3466 SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh)(SSL *ssl, int is_export,
3467     int keylength))
3468 {
3469 	SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
3470 }
3471 LSSL_ALIAS(SSL_set_tmp_dh_callback);
3472 
3473 void
SSL_CTX_set_tmp_ecdh_callback(SSL_CTX * ctx,EC_KEY * (* ecdh)(SSL * ssl,int is_export,int keylength))3474 SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx, EC_KEY *(*ecdh)(SSL *ssl,
3475     int is_export, int keylength))
3476 {
3477 	SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB,
3478 	    (void (*)(void))ecdh);
3479 }
3480 LSSL_ALIAS(SSL_CTX_set_tmp_ecdh_callback);
3481 
3482 void
SSL_set_tmp_ecdh_callback(SSL * ssl,EC_KEY * (* ecdh)(SSL * ssl,int is_export,int keylength))3483 SSL_set_tmp_ecdh_callback(SSL *ssl, EC_KEY *(*ecdh)(SSL *ssl, int is_export,
3484     int keylength))
3485 {
3486 	SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
3487 }
3488 LSSL_ALIAS(SSL_set_tmp_ecdh_callback);
3489 
3490 
3491 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))3492 SSL_CTX_set_msg_callback(SSL_CTX *ctx, void (*cb)(int write_p, int version,
3493     int content_type, const void *buf, size_t len, SSL *ssl, void *arg))
3494 {
3495 	SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK,
3496 	    (void (*)(void))cb);
3497 }
3498 LSSL_ALIAS(SSL_CTX_set_msg_callback);
3499 
3500 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))3501 SSL_set_msg_callback(SSL *ssl, void (*cb)(int write_p, int version,
3502     int content_type, const void *buf, size_t len, SSL *ssl, void *arg))
3503 {
3504 	SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3505 }
3506 LSSL_ALIAS(SSL_set_msg_callback);
3507 
3508 int
SSL_cache_hit(SSL * s)3509 SSL_cache_hit(SSL *s)
3510 {
3511 	return (s->hit);
3512 }
3513 LSSL_ALIAS(SSL_cache_hit);
3514 
3515 int
SSL_CTX_get_min_proto_version(SSL_CTX * ctx)3516 SSL_CTX_get_min_proto_version(SSL_CTX *ctx)
3517 {
3518 	return ctx->min_proto_version;
3519 }
3520 LSSL_ALIAS(SSL_CTX_get_min_proto_version);
3521 
3522 int
SSL_CTX_set_min_proto_version(SSL_CTX * ctx,uint16_t version)3523 SSL_CTX_set_min_proto_version(SSL_CTX *ctx, uint16_t version)
3524 {
3525 	return ssl_version_set_min(ctx->method, version,
3526 	    ctx->max_tls_version, &ctx->min_tls_version,
3527 	    &ctx->min_proto_version);
3528 }
3529 LSSL_ALIAS(SSL_CTX_set_min_proto_version);
3530 
3531 int
SSL_CTX_get_max_proto_version(SSL_CTX * ctx)3532 SSL_CTX_get_max_proto_version(SSL_CTX *ctx)
3533 {
3534 	return ctx->max_proto_version;
3535 }
3536 LSSL_ALIAS(SSL_CTX_get_max_proto_version);
3537 
3538 int
SSL_CTX_set_max_proto_version(SSL_CTX * ctx,uint16_t version)3539 SSL_CTX_set_max_proto_version(SSL_CTX *ctx, uint16_t version)
3540 {
3541 	return ssl_version_set_max(ctx->method, version,
3542 	    ctx->min_tls_version, &ctx->max_tls_version,
3543 	    &ctx->max_proto_version);
3544 }
3545 LSSL_ALIAS(SSL_CTX_set_max_proto_version);
3546 
3547 int
SSL_get_min_proto_version(SSL * ssl)3548 SSL_get_min_proto_version(SSL *ssl)
3549 {
3550 	return ssl->min_proto_version;
3551 }
3552 LSSL_ALIAS(SSL_get_min_proto_version);
3553 
3554 int
SSL_set_min_proto_version(SSL * ssl,uint16_t version)3555 SSL_set_min_proto_version(SSL *ssl, uint16_t version)
3556 {
3557 	return ssl_version_set_min(ssl->method, version,
3558 	    ssl->max_tls_version, &ssl->min_tls_version,
3559 	    &ssl->min_proto_version);
3560 }
3561 LSSL_ALIAS(SSL_set_min_proto_version);
3562 int
SSL_get_max_proto_version(SSL * ssl)3563 SSL_get_max_proto_version(SSL *ssl)
3564 {
3565 	return ssl->max_proto_version;
3566 }
3567 LSSL_ALIAS(SSL_get_max_proto_version);
3568 
3569 int
SSL_set_max_proto_version(SSL * ssl,uint16_t version)3570 SSL_set_max_proto_version(SSL *ssl, uint16_t version)
3571 {
3572 	return ssl_version_set_max(ssl->method, version,
3573 	    ssl->min_tls_version, &ssl->max_tls_version,
3574 	    &ssl->max_proto_version);
3575 }
3576 LSSL_ALIAS(SSL_set_max_proto_version);
3577 
3578 const SSL_METHOD *
SSL_CTX_get_ssl_method(const SSL_CTX * ctx)3579 SSL_CTX_get_ssl_method(const SSL_CTX *ctx)
3580 {
3581 	return ctx->method;
3582 }
3583 LSSL_ALIAS(SSL_CTX_get_ssl_method);
3584 
3585 int
SSL_CTX_get_security_level(const SSL_CTX * ctx)3586 SSL_CTX_get_security_level(const SSL_CTX *ctx)
3587 {
3588 	return ctx->cert->security_level;
3589 }
3590 LSSL_ALIAS(SSL_CTX_get_security_level);
3591 
3592 void
SSL_CTX_set_security_level(SSL_CTX * ctx,int level)3593 SSL_CTX_set_security_level(SSL_CTX *ctx, int level)
3594 {
3595 	ctx->cert->security_level = level;
3596 }
3597 LSSL_ALIAS(SSL_CTX_set_security_level);
3598 
3599 int
SSL_get_security_level(const SSL * ssl)3600 SSL_get_security_level(const SSL *ssl)
3601 {
3602 	return ssl->cert->security_level;
3603 }
3604 LSSL_ALIAS(SSL_get_security_level);
3605 
3606 void
SSL_set_security_level(SSL * ssl,int level)3607 SSL_set_security_level(SSL *ssl, int level)
3608 {
3609 	ssl->cert->security_level = level;
3610 }
3611 LSSL_ALIAS(SSL_set_security_level);
3612 
3613 int
SSL_is_quic(const SSL * ssl)3614 SSL_is_quic(const SSL *ssl)
3615 {
3616 	return ssl->quic_method != NULL;
3617 }
3618 LSSL_ALIAS(SSL_is_quic);
3619 
3620 int
SSL_set_quic_transport_params(SSL * ssl,const uint8_t * params,size_t params_len)3621 SSL_set_quic_transport_params(SSL *ssl, const uint8_t *params,
3622     size_t params_len)
3623 {
3624 	freezero(ssl->quic_transport_params,
3625 	    ssl->quic_transport_params_len);
3626 	ssl->quic_transport_params = NULL;
3627 	ssl->quic_transport_params_len = 0;
3628 
3629 	if ((ssl->quic_transport_params = malloc(params_len)) == NULL)
3630 		return 0;
3631 
3632 	memcpy(ssl->quic_transport_params, params, params_len);
3633 	ssl->quic_transport_params_len = params_len;
3634 
3635 	return 1;
3636 }
3637 LSSL_ALIAS(SSL_set_quic_transport_params);
3638 
3639 void
SSL_get_peer_quic_transport_params(const SSL * ssl,const uint8_t ** out_params,size_t * out_params_len)3640 SSL_get_peer_quic_transport_params(const SSL *ssl, const uint8_t **out_params,
3641     size_t *out_params_len)
3642 {
3643 	*out_params = ssl->s3->peer_quic_transport_params;
3644 	*out_params_len = ssl->s3->peer_quic_transport_params_len;
3645 }
3646 LSSL_ALIAS(SSL_get_peer_quic_transport_params);
3647 
3648 void
SSL_set_quic_use_legacy_codepoint(SSL * ssl,int use_legacy)3649 SSL_set_quic_use_legacy_codepoint(SSL *ssl, int use_legacy)
3650 {
3651 	/* Not supported. */
3652 }
3653 LSSL_ALIAS(SSL_set_quic_use_legacy_codepoint);
3654