2 * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4 * Copyright 2005 Nokia. All rights reserved.
6 * Licensed under the Apache License 2.0 (the "License"). You may not use
7 * this file except in compliance with the License. You can obtain a copy
8 * in the file LICENSE in the source distribution or at
9 * https://www.openssl.org/source/license.html
12 #include "internal/e_os.h"
13 #include "internal/e_winsock.h"
14 #include "ssl_local.h"
16 #include <openssl/objects.h>
17 #include <openssl/x509v3.h>
18 #include <openssl/rand.h>
19 #include <openssl/ocsp.h>
20 #include <openssl/dh.h>
21 #include <openssl/engine.h>
22 #include <openssl/async.h>
23 #include <openssl/ct.h>
24 #include <openssl/trace.h>
25 #include <openssl/core_names.h>
26 #include <openssl/provider.h>
27 #include "internal/cryptlib.h"
28 #include "internal/nelem.h"
29 #include "internal/refcount.h"
30 #include "internal/thread_once.h"
31 #include "internal/ktls.h"
32 #include "internal/to_hex.h"
33 #include "internal/ssl_unwrap.h"
34 #include "quic/quic_local.h"
36 #ifndef OPENSSL_NO_SSLKEYLOG
37 # include <sys/stat.h>
41 static int ssl_undefined_function_3(SSL_CONNECTION
*sc
, unsigned char *r
,
42 unsigned char *s
, size_t t
, size_t *u
)
44 return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc
));
47 static int ssl_undefined_function_4(SSL_CONNECTION
*sc
, int r
)
49 return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc
));
52 static size_t ssl_undefined_function_5(SSL_CONNECTION
*sc
, const char *r
,
53 size_t s
, unsigned char *t
)
55 return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc
));
58 static int ssl_undefined_function_6(int r
)
60 return ssl_undefined_function(NULL
);
63 static int ssl_undefined_function_7(SSL_CONNECTION
*sc
, unsigned char *r
,
64 size_t s
, const char *t
, size_t u
,
65 const unsigned char *v
, size_t w
, int x
)
67 return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc
));
70 static int ssl_undefined_function_8(SSL_CONNECTION
*sc
)
72 return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc
));
75 const SSL3_ENC_METHOD ssl3_undef_enc_method
= {
76 ssl_undefined_function_8
,
77 ssl_undefined_function_3
,
78 ssl_undefined_function_4
,
79 ssl_undefined_function_5
,
80 NULL
, /* client_finished_label */
81 0, /* client_finished_label_len */
82 NULL
, /* server_finished_label */
83 0, /* server_finished_label_len */
84 ssl_undefined_function_6
,
85 ssl_undefined_function_7
,
88 struct ssl_async_args
{
92 enum { READFUNC
, WRITEFUNC
, OTHERFUNC
} type
;
94 int (*func_read
) (SSL
*, void *, size_t, size_t *);
95 int (*func_write
) (SSL
*, const void *, size_t, size_t *);
96 int (*func_other
) (SSL
*);
100 static const struct {
106 DANETLS_MATCHING_FULL
, 0, NID_undef
109 DANETLS_MATCHING_2256
, 1, NID_sha256
112 DANETLS_MATCHING_2512
, 2, NID_sha512
116 static int dane_ctx_enable(struct dane_ctx_st
*dctx
)
118 const EVP_MD
**mdevp
;
120 uint8_t mdmax
= DANETLS_MATCHING_LAST
;
121 int n
= ((int)mdmax
) + 1; /* int to handle PrivMatch(255) */
124 if (dctx
->mdevp
!= NULL
)
127 mdevp
= OPENSSL_zalloc(n
* sizeof(*mdevp
));
128 mdord
= OPENSSL_zalloc(n
* sizeof(*mdord
));
130 if (mdord
== NULL
|| mdevp
== NULL
) {
136 /* Install default entries */
137 for (i
= 0; i
< OSSL_NELEM(dane_mds
); ++i
) {
140 if (dane_mds
[i
].nid
== NID_undef
||
141 (md
= EVP_get_digestbynid(dane_mds
[i
].nid
)) == NULL
)
143 mdevp
[dane_mds
[i
].mtype
] = md
;
144 mdord
[dane_mds
[i
].mtype
] = dane_mds
[i
].ord
;
154 static void dane_ctx_final(struct dane_ctx_st
*dctx
)
156 OPENSSL_free(dctx
->mdevp
);
159 OPENSSL_free(dctx
->mdord
);
164 static void tlsa_free(danetls_record
*t
)
168 OPENSSL_free(t
->data
);
169 EVP_PKEY_free(t
->spki
);
173 static void dane_final(SSL_DANE
*dane
)
175 sk_danetls_record_pop_free(dane
->trecs
, tlsa_free
);
178 OSSL_STACK_OF_X509_free(dane
->certs
);
181 X509_free(dane
->mcert
);
189 * dane_copy - Copy dane configuration, sans verification state.
191 static int ssl_dane_dup(SSL_CONNECTION
*to
, SSL_CONNECTION
*from
)
196 if (!DANETLS_ENABLED(&from
->dane
))
199 num
= sk_danetls_record_num(from
->dane
.trecs
);
200 dane_final(&to
->dane
);
201 to
->dane
.flags
= from
->dane
.flags
;
202 to
->dane
.dctx
= &SSL_CONNECTION_GET_CTX(to
)->dane
;
203 to
->dane
.trecs
= sk_danetls_record_new_reserve(NULL
, num
);
205 if (to
->dane
.trecs
== NULL
) {
206 ERR_raise(ERR_LIB_SSL
, ERR_R_CRYPTO_LIB
);
210 for (i
= 0; i
< num
; ++i
) {
211 danetls_record
*t
= sk_danetls_record_value(from
->dane
.trecs
, i
);
213 if (SSL_dane_tlsa_add(SSL_CONNECTION_GET_SSL(to
), t
->usage
,
214 t
->selector
, t
->mtype
, t
->data
, t
->dlen
) <= 0)
220 static int dane_mtype_set(struct dane_ctx_st
*dctx
,
221 const EVP_MD
*md
, uint8_t mtype
, uint8_t ord
)
225 if (mtype
== DANETLS_MATCHING_FULL
&& md
!= NULL
) {
226 ERR_raise(ERR_LIB_SSL
, SSL_R_DANE_CANNOT_OVERRIDE_MTYPE_FULL
);
230 if (mtype
> dctx
->mdmax
) {
231 const EVP_MD
**mdevp
;
233 int n
= ((int)mtype
) + 1;
235 mdevp
= OPENSSL_realloc(dctx
->mdevp
, n
* sizeof(*mdevp
));
240 mdord
= OPENSSL_realloc(dctx
->mdord
, n
* sizeof(*mdord
));
245 /* Zero-fill any gaps */
246 for (i
= dctx
->mdmax
+ 1; i
< mtype
; ++i
) {
254 dctx
->mdevp
[mtype
] = md
;
255 /* Coerce ordinal of disabled matching types to 0 */
256 dctx
->mdord
[mtype
] = (md
== NULL
) ? 0 : ord
;
261 static const EVP_MD
*tlsa_md_get(SSL_DANE
*dane
, uint8_t mtype
)
263 if (mtype
> dane
->dctx
->mdmax
)
265 return dane
->dctx
->mdevp
[mtype
];
268 static int dane_tlsa_add(SSL_DANE
*dane
,
271 uint8_t mtype
, const unsigned char *data
, size_t dlen
)
274 const EVP_MD
*md
= NULL
;
275 int ilen
= (int)dlen
;
280 if (dane
->trecs
== NULL
) {
281 ERR_raise(ERR_LIB_SSL
, SSL_R_DANE_NOT_ENABLED
);
285 if (ilen
< 0 || dlen
!= (size_t)ilen
) {
286 ERR_raise(ERR_LIB_SSL
, SSL_R_DANE_TLSA_BAD_DATA_LENGTH
);
290 if (usage
> DANETLS_USAGE_LAST
) {
291 ERR_raise(ERR_LIB_SSL
, SSL_R_DANE_TLSA_BAD_CERTIFICATE_USAGE
);
295 if (selector
> DANETLS_SELECTOR_LAST
) {
296 ERR_raise(ERR_LIB_SSL
, SSL_R_DANE_TLSA_BAD_SELECTOR
);
300 if (mtype
!= DANETLS_MATCHING_FULL
) {
301 md
= tlsa_md_get(dane
, mtype
);
303 ERR_raise(ERR_LIB_SSL
, SSL_R_DANE_TLSA_BAD_MATCHING_TYPE
);
309 mdsize
= EVP_MD_get_size(md
);
310 if (mdsize
<= 0 || dlen
!= (size_t)mdsize
) {
311 ERR_raise(ERR_LIB_SSL
, SSL_R_DANE_TLSA_BAD_DIGEST_LENGTH
);
316 ERR_raise(ERR_LIB_SSL
, SSL_R_DANE_TLSA_NULL_DATA
);
320 if ((t
= OPENSSL_zalloc(sizeof(*t
))) == NULL
)
324 t
->selector
= selector
;
326 t
->data
= OPENSSL_malloc(dlen
);
327 if (t
->data
== NULL
) {
331 memcpy(t
->data
, data
, dlen
);
334 /* Validate and cache full certificate or public key */
335 if (mtype
== DANETLS_MATCHING_FULL
) {
336 const unsigned char *p
= data
;
338 EVP_PKEY
*pkey
= NULL
;
341 case DANETLS_SELECTOR_CERT
:
342 if (!d2i_X509(&cert
, &p
, ilen
) || p
< data
||
343 dlen
!= (size_t)(p
- data
)) {
346 ERR_raise(ERR_LIB_SSL
, SSL_R_DANE_TLSA_BAD_CERTIFICATE
);
349 if (X509_get0_pubkey(cert
) == NULL
) {
352 ERR_raise(ERR_LIB_SSL
, SSL_R_DANE_TLSA_BAD_CERTIFICATE
);
356 if ((DANETLS_USAGE_BIT(usage
) & DANETLS_TA_MASK
) == 0) {
358 * The Full(0) certificate decodes to a seemingly valid X.509
359 * object with a plausible key, so the TLSA record is well
360 * formed. However, we don't actually need the certificate for
361 * usages PKIX-EE(1) or DANE-EE(3), because at least the EE
362 * certificate is always presented by the peer. We discard the
363 * certificate, and just use the TLSA data as an opaque blob
364 * for matching the raw presented DER octets.
366 * DO NOT FREE `t` here, it will be added to the TLSA record
374 * For usage DANE-TA(2), we support authentication via "2 0 0" TLSA
375 * records that contain full certificates of trust-anchors that are
376 * not present in the wire chain. For usage PKIX-TA(0), we augment
377 * the chain with untrusted Full(0) certificates from DNS, in case
378 * they are missing from the chain.
380 if ((dane
->certs
== NULL
&&
381 (dane
->certs
= sk_X509_new_null()) == NULL
) ||
382 !sk_X509_push(dane
->certs
, cert
)) {
383 ERR_raise(ERR_LIB_SSL
, ERR_R_CRYPTO_LIB
);
390 case DANETLS_SELECTOR_SPKI
:
391 if (!d2i_PUBKEY(&pkey
, &p
, ilen
) || p
< data
||
392 dlen
!= (size_t)(p
- data
)) {
395 ERR_raise(ERR_LIB_SSL
, SSL_R_DANE_TLSA_BAD_PUBLIC_KEY
);
400 * For usage DANE-TA(2), we support authentication via "2 1 0" TLSA
401 * records that contain full bare keys of trust-anchors that are
402 * not present in the wire chain.
404 if (usage
== DANETLS_USAGE_DANE_TA
)
413 * Find the right insertion point for the new record.
415 * See crypto/x509/x509_vfy.c. We sort DANE-EE(3) records first, so that
416 * they can be processed first, as they require no chain building, and no
417 * expiration or hostname checks. Because DANE-EE(3) is numerically
418 * largest, this is accomplished via descending sort by "usage".
420 * We also sort in descending order by matching ordinal to simplify
421 * the implementation of digest agility in the verification code.
423 * The choice of order for the selector is not significant, so we
424 * use the same descending order for consistency.
426 num
= sk_danetls_record_num(dane
->trecs
);
427 for (i
= 0; i
< num
; ++i
) {
428 danetls_record
*rec
= sk_danetls_record_value(dane
->trecs
, i
);
430 if (rec
->usage
> usage
)
432 if (rec
->usage
< usage
)
434 if (rec
->selector
> selector
)
436 if (rec
->selector
< selector
)
438 if (dane
->dctx
->mdord
[rec
->mtype
] > dane
->dctx
->mdord
[mtype
])
443 if (!sk_danetls_record_insert(dane
->trecs
, t
, i
)) {
445 ERR_raise(ERR_LIB_SSL
, ERR_R_CRYPTO_LIB
);
448 dane
->umask
|= DANETLS_USAGE_BIT(usage
);
454 * Return 0 if there is only one version configured and it was disabled
455 * at configure time. Return 1 otherwise.
457 static int ssl_check_allowed_versions(int min_version
, int max_version
)
459 int minisdtls
= 0, maxisdtls
= 0;
461 /* Figure out if we're doing DTLS versions or TLS versions */
462 if (min_version
== DTLS1_BAD_VER
463 || min_version
>> 8 == DTLS1_VERSION_MAJOR
)
465 if (max_version
== DTLS1_BAD_VER
466 || max_version
>> 8 == DTLS1_VERSION_MAJOR
)
468 /* A wildcard version of 0 could be DTLS or TLS. */
469 if ((minisdtls
&& !maxisdtls
&& max_version
!= 0)
470 || (maxisdtls
&& !minisdtls
&& min_version
!= 0)) {
471 /* Mixing DTLS and TLS versions will lead to sadness; deny it. */
475 if (minisdtls
|| maxisdtls
) {
476 /* Do DTLS version checks. */
477 if (min_version
== 0)
478 /* Ignore DTLS1_BAD_VER */
479 min_version
= DTLS1_VERSION
;
480 if (max_version
== 0)
481 max_version
= DTLS1_2_VERSION
;
482 #ifdef OPENSSL_NO_DTLS1_2
483 if (max_version
== DTLS1_2_VERSION
)
484 max_version
= DTLS1_VERSION
;
486 #ifdef OPENSSL_NO_DTLS1
487 if (min_version
== DTLS1_VERSION
)
488 min_version
= DTLS1_2_VERSION
;
490 /* Done massaging versions; do the check. */
492 #ifdef OPENSSL_NO_DTLS1
493 || (DTLS_VERSION_GE(min_version
, DTLS1_VERSION
)
494 && DTLS_VERSION_GE(DTLS1_VERSION
, max_version
))
496 #ifdef OPENSSL_NO_DTLS1_2
497 || (DTLS_VERSION_GE(min_version
, DTLS1_2_VERSION
)
498 && DTLS_VERSION_GE(DTLS1_2_VERSION
, max_version
))
503 /* Regular TLS version checks. */
504 if (min_version
== 0)
505 min_version
= SSL3_VERSION
;
506 if (max_version
== 0)
507 max_version
= TLS1_3_VERSION
;
508 #ifdef OPENSSL_NO_TLS1_3
509 if (max_version
== TLS1_3_VERSION
)
510 max_version
= TLS1_2_VERSION
;
512 #ifdef OPENSSL_NO_TLS1_2
513 if (max_version
== TLS1_2_VERSION
)
514 max_version
= TLS1_1_VERSION
;
516 #ifdef OPENSSL_NO_TLS1_1
517 if (max_version
== TLS1_1_VERSION
)
518 max_version
= TLS1_VERSION
;
520 #ifdef OPENSSL_NO_TLS1
521 if (max_version
== TLS1_VERSION
)
522 max_version
= SSL3_VERSION
;
524 #ifdef OPENSSL_NO_SSL3
525 if (min_version
== SSL3_VERSION
)
526 min_version
= TLS1_VERSION
;
528 #ifdef OPENSSL_NO_TLS1
529 if (min_version
== TLS1_VERSION
)
530 min_version
= TLS1_1_VERSION
;
532 #ifdef OPENSSL_NO_TLS1_1
533 if (min_version
== TLS1_1_VERSION
)
534 min_version
= TLS1_2_VERSION
;
536 #ifdef OPENSSL_NO_TLS1_2
537 if (min_version
== TLS1_2_VERSION
)
538 min_version
= TLS1_3_VERSION
;
540 /* Done massaging versions; do the check. */
542 #ifdef OPENSSL_NO_SSL3
543 || (min_version
<= SSL3_VERSION
&& SSL3_VERSION
<= max_version
)
545 #ifdef OPENSSL_NO_TLS1
546 || (min_version
<= TLS1_VERSION
&& TLS1_VERSION
<= max_version
)
548 #ifdef OPENSSL_NO_TLS1_1
549 || (min_version
<= TLS1_1_VERSION
&& TLS1_1_VERSION
<= max_version
)
551 #ifdef OPENSSL_NO_TLS1_2
552 || (min_version
<= TLS1_2_VERSION
&& TLS1_2_VERSION
<= max_version
)
554 #ifdef OPENSSL_NO_TLS1_3
555 || (min_version
<= TLS1_3_VERSION
&& TLS1_3_VERSION
<= max_version
)
563 #if defined(__TANDEM) && defined(OPENSSL_VPROC)
565 * Define a VPROC function for HP NonStop build ssl library.
566 * This is used by platform version identification tools.
567 * Do not inline this procedure or make it static.
569 # define OPENSSL_VPROC_STRING_(x) x##_SSL
570 # define OPENSSL_VPROC_STRING(x) OPENSSL_VPROC_STRING_(x)
571 # define OPENSSL_VPROC_FUNC OPENSSL_VPROC_STRING(OPENSSL_VPROC)
572 void OPENSSL_VPROC_FUNC(void) {}
575 int SSL_clear(SSL
*s
)
577 if (s
->method
== NULL
) {
578 ERR_raise(ERR_LIB_SSL
, SSL_R_NO_METHOD_SPECIFIED
);
582 return s
->method
->ssl_reset(s
);
585 int ossl_ssl_connection_reset(SSL
*s
)
587 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
592 if (ssl_clear_bad_session(sc
)) {
593 SSL_SESSION_free(sc
->session
);
596 SSL_SESSION_free(sc
->psksession
);
597 sc
->psksession
= NULL
;
598 OPENSSL_free(sc
->psksession_id
);
599 sc
->psksession_id
= NULL
;
600 sc
->psksession_id_len
= 0;
601 sc
->hello_retry_request
= SSL_HRR_NONE
;
602 sc
->sent_tickets
= 0;
608 if (sc
->renegotiate
) {
609 ERR_raise(ERR_LIB_SSL
, ERR_R_INTERNAL_ERROR
);
613 ossl_statem_clear(sc
);
615 sc
->version
= s
->method
->version
;
616 sc
->client_version
= sc
->version
;
617 sc
->rwstate
= SSL_NOTHING
;
619 BUF_MEM_free(sc
->init_buf
);
621 sc
->first_packet
= 0;
623 sc
->key_update
= SSL_KEY_UPDATE_NONE
;
624 memset(sc
->ext
.compress_certificate_from_peer
, 0,
625 sizeof(sc
->ext
.compress_certificate_from_peer
));
626 sc
->ext
.compress_certificate_sent
= 0;
628 EVP_MD_CTX_free(sc
->pha_dgst
);
631 /* Reset DANE verification result state */
634 X509_free(sc
->dane
.mcert
);
635 sc
->dane
.mcert
= NULL
;
636 sc
->dane
.mtlsa
= NULL
;
638 /* Clear the verification result peername */
639 X509_VERIFY_PARAM_move_peername(sc
->param
, NULL
);
641 /* Clear any shared connection state */
642 OPENSSL_free(sc
->shared_sigalgs
);
643 sc
->shared_sigalgs
= NULL
;
644 sc
->shared_sigalgslen
= 0;
647 * Check to see if we were changed into a different method, if so, revert
650 if (s
->method
!= s
->defltmeth
) {
651 s
->method
->ssl_deinit(s
);
652 s
->method
= s
->defltmeth
;
653 if (!s
->method
->ssl_init(s
))
656 if (!s
->method
->ssl_clear(s
))
660 ossl_quic_tls_clear(sc
->qtls
);
662 if (!RECORD_LAYER_reset(&sc
->rlayer
))
668 #ifndef OPENSSL_NO_DEPRECATED_3_0
669 /** Used to change an SSL_CTXs default SSL method type */
670 int SSL_CTX_set_ssl_version(SSL_CTX
*ctx
, const SSL_METHOD
*meth
)
672 STACK_OF(SSL_CIPHER
) *sk
;
674 if (IS_QUIC_CTX(ctx
)) {
675 ERR_raise(ERR_LIB_SSL
, SSL_R_WRONG_SSL_VERSION
);
681 if (!SSL_CTX_set_ciphersuites(ctx
, OSSL_default_ciphersuites())) {
682 ERR_raise(ERR_LIB_SSL
, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS
);
685 sk
= ssl_create_cipher_list(ctx
,
686 ctx
->tls13_ciphersuites
,
688 &(ctx
->cipher_list_by_id
),
689 OSSL_default_cipher_list(), ctx
->cert
);
690 if ((sk
== NULL
) || (sk_SSL_CIPHER_num(sk
) <= 0)) {
691 ERR_raise(ERR_LIB_SSL
, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS
);
698 SSL
*SSL_new(SSL_CTX
*ctx
)
701 ERR_raise(ERR_LIB_SSL
, SSL_R_NULL_SSL_CTX
);
704 if (ctx
->method
== NULL
) {
705 ERR_raise(ERR_LIB_SSL
, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION
);
708 return ctx
->method
->ssl_new(ctx
);
711 int ossl_ssl_init(SSL
*ssl
, SSL_CTX
*ctx
, const SSL_METHOD
*method
, int type
)
713 if (!SSL_CTX_up_ref(ctx
))
716 ssl
->lock
= CRYPTO_THREAD_lock_new();
718 if (ssl
->lock
== NULL
|| !CRYPTO_NEW_REF(&ssl
->references
, 1))
721 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL
, ssl
, &ssl
->ex_data
)) {
722 CRYPTO_FREE_REF(&ssl
->references
);
728 ssl
->defltmeth
= ssl
->method
= method
;
733 CRYPTO_THREAD_lock_free(ssl
->lock
);
739 SSL
*ossl_ssl_connection_new_int(SSL_CTX
*ctx
, SSL
*user_ssl
,
740 const SSL_METHOD
*method
)
745 s
= OPENSSL_zalloc(sizeof(*s
));
750 s
->user_ssl
= (user_ssl
== NULL
) ? ssl
: user_ssl
;
752 if (!ossl_ssl_init(ssl
, ctx
, method
, SSL_TYPE_SSL_CONNECTION
)) {
759 RECORD_LAYER_init(&s
->rlayer
, s
);
761 s
->options
= ctx
->options
;
763 s
->dane
.flags
= ctx
->dane
.flags
;
764 if (method
->version
== ctx
->method
->version
) {
765 s
->min_proto_version
= ctx
->min_proto_version
;
766 s
->max_proto_version
= ctx
->max_proto_version
;
770 s
->max_cert_list
= ctx
->max_cert_list
;
771 s
->max_early_data
= ctx
->max_early_data
;
772 s
->recv_max_early_data
= ctx
->recv_max_early_data
;
774 s
->num_tickets
= ctx
->num_tickets
;
775 s
->pha_enabled
= ctx
->pha_enabled
;
777 /* Shallow copy of the ciphersuites stack */
778 s
->tls13_ciphersuites
= sk_SSL_CIPHER_dup(ctx
->tls13_ciphersuites
);
779 if (s
->tls13_ciphersuites
== NULL
)
783 * Earlier library versions used to copy the pointer to the CERT, not
784 * its contents; only when setting new parameters for the per-SSL
785 * copy, ssl_cert_new would be called (and the direct reference to
786 * the per-SSL_CTX settings would be lost, but those still were
787 * indirectly accessed for various purposes, and for that reason they
788 * used to be known as s->ctx->default_cert). Now we don't look at the
789 * SSL_CTX's CERT after having duplicated it once.
791 s
->cert
= ssl_cert_dup(ctx
->cert
);
795 RECORD_LAYER_set_read_ahead(&s
->rlayer
, ctx
->read_ahead
);
796 s
->msg_callback
= ctx
->msg_callback
;
797 s
->msg_callback_arg
= ctx
->msg_callback_arg
;
798 s
->verify_mode
= ctx
->verify_mode
;
799 s
->not_resumable_session_cb
= ctx
->not_resumable_session_cb
;
800 s
->rlayer
.record_padding_cb
= ctx
->record_padding_cb
;
801 s
->rlayer
.record_padding_arg
= ctx
->record_padding_arg
;
802 s
->rlayer
.block_padding
= ctx
->block_padding
;
803 s
->rlayer
.hs_padding
= ctx
->hs_padding
;
804 s
->sid_ctx_length
= ctx
->sid_ctx_length
;
805 if (!ossl_assert(s
->sid_ctx_length
<= sizeof(s
->sid_ctx
)))
807 memcpy(&s
->sid_ctx
, &ctx
->sid_ctx
, sizeof(s
->sid_ctx
));
808 s
->verify_callback
= ctx
->default_verify_callback
;
809 s
->generate_session_id
= ctx
->generate_session_id
;
811 s
->param
= X509_VERIFY_PARAM_new();
812 if (s
->param
== NULL
)
814 X509_VERIFY_PARAM_inherit(s
->param
, ctx
->param
);
815 s
->quiet_shutdown
= IS_QUIC_CTX(ctx
) ? 0 : ctx
->quiet_shutdown
;
817 if (!IS_QUIC_CTX(ctx
))
818 s
->ext
.max_fragment_len_mode
= ctx
->ext
.max_fragment_len_mode
;
820 s
->max_send_fragment
= ctx
->max_send_fragment
;
821 s
->split_send_fragment
= ctx
->split_send_fragment
;
822 s
->max_pipelines
= ctx
->max_pipelines
;
823 s
->rlayer
.default_read_buf_len
= ctx
->default_read_buf_len
;
826 s
->ext
.debug_arg
= NULL
;
827 s
->ext
.ticket_expected
= 0;
828 s
->ext
.status_type
= ctx
->ext
.status_type
;
829 s
->ext
.status_expected
= 0;
830 s
->ext
.ocsp
.ids
= NULL
;
831 s
->ext
.ocsp
.exts
= NULL
;
832 s
->ext
.ocsp
.resp
= NULL
;
833 s
->ext
.ocsp
.resp_len
= 0;
835 if (!SSL_CTX_up_ref(ctx
))
838 s
->session_ctx
= ctx
;
839 if (ctx
->ext
.ecpointformats
!= NULL
) {
840 s
->ext
.ecpointformats
=
841 OPENSSL_memdup(ctx
->ext
.ecpointformats
,
842 ctx
->ext
.ecpointformats_len
);
843 if (s
->ext
.ecpointformats
== NULL
) {
844 s
->ext
.ecpointformats_len
= 0;
847 s
->ext
.ecpointformats_len
=
848 ctx
->ext
.ecpointformats_len
;
850 if (ctx
->ext
.supportedgroups
!= NULL
) {
853 if (ctx
->ext
.supportedgroups_len
== 0)
854 /* Add 1 so allocation won't fail */
856 s
->ext
.supportedgroups
=
857 OPENSSL_memdup(ctx
->ext
.supportedgroups
,
858 (ctx
->ext
.supportedgroups_len
+ add
)
859 * sizeof(*ctx
->ext
.supportedgroups
));
860 if (s
->ext
.supportedgroups
== NULL
) {
861 s
->ext
.supportedgroups_len
= 0;
864 s
->ext
.supportedgroups_len
= ctx
->ext
.supportedgroups_len
;
866 if (ctx
->ext
.keyshares
!= NULL
) {
869 if (ctx
->ext
.keyshares_len
== 0)
870 /* Add 1 so allocation won't fail */
873 OPENSSL_memdup(ctx
->ext
.keyshares
,
874 (ctx
->ext
.keyshares_len
+ add
)
875 * sizeof(*ctx
->ext
.keyshares
));
876 if (s
->ext
.keyshares
== NULL
) {
877 s
->ext
.keyshares_len
= 0;
880 s
->ext
.keyshares_len
= ctx
->ext
.keyshares_len
;
882 if (ctx
->ext
.tuples
!= NULL
) {
885 if (ctx
->ext
.tuples_len
== 0)
886 /* Add 1 so allocation won't fail */
889 OPENSSL_memdup(ctx
->ext
.tuples
,
890 (ctx
->ext
.tuples_len
+ add
)
891 * sizeof(*ctx
->ext
.tuples
));
892 if (s
->ext
.tuples
== NULL
) {
893 s
->ext
.tuples_len
= 0;
896 s
->ext
.tuples_len
= ctx
->ext
.tuples_len
;
899 #ifndef OPENSSL_NO_NEXTPROTONEG
903 if (ctx
->ext
.alpn
!= NULL
) {
904 s
->ext
.alpn
= OPENSSL_malloc(ctx
->ext
.alpn_len
);
905 if (s
->ext
.alpn
== NULL
) {
909 memcpy(s
->ext
.alpn
, ctx
->ext
.alpn
, ctx
->ext
.alpn_len
);
910 s
->ext
.alpn_len
= ctx
->ext
.alpn_len
;
913 s
->verified_chain
= NULL
;
914 s
->verify_result
= X509_V_OK
;
916 s
->default_passwd_callback
= ctx
->default_passwd_callback
;
917 s
->default_passwd_callback_userdata
= ctx
->default_passwd_callback_userdata
;
919 s
->key_update
= SSL_KEY_UPDATE_NONE
;
921 if (!IS_QUIC_CTX(ctx
)) {
922 s
->allow_early_data_cb
= ctx
->allow_early_data_cb
;
923 s
->allow_early_data_cb_data
= ctx
->allow_early_data_cb_data
;
926 if (!method
->ssl_init(ssl
))
929 s
->server
= (method
->ssl_accept
== ssl_undefined_function
) ? 0 : 1;
931 if (!method
->ssl_reset(ssl
))
934 #ifndef OPENSSL_NO_PSK
935 s
->psk_client_callback
= ctx
->psk_client_callback
;
936 s
->psk_server_callback
= ctx
->psk_server_callback
;
938 s
->psk_find_session_cb
= ctx
->psk_find_session_cb
;
939 s
->psk_use_session_cb
= ctx
->psk_use_session_cb
;
941 s
->async_cb
= ctx
->async_cb
;
942 s
->async_cb_arg
= ctx
->async_cb_arg
;
946 #ifndef OPENSSL_NO_COMP_ALG
947 memcpy(s
->cert_comp_prefs
, ctx
->cert_comp_prefs
, sizeof(s
->cert_comp_prefs
));
949 if (ctx
->client_cert_type
!= NULL
) {
950 s
->client_cert_type
= OPENSSL_memdup(ctx
->client_cert_type
,
951 ctx
->client_cert_type_len
);
952 if (s
->client_cert_type
== NULL
)
954 s
->client_cert_type_len
= ctx
->client_cert_type_len
;
956 if (ctx
->server_cert_type
!= NULL
) {
957 s
->server_cert_type
= OPENSSL_memdup(ctx
->server_cert_type
,
958 ctx
->server_cert_type_len
);
959 if (s
->server_cert_type
== NULL
)
961 s
->server_cert_type_len
= ctx
->server_cert_type_len
;
964 #ifndef OPENSSL_NO_CT
965 if (!SSL_set_ct_validation_callback(ssl
, ctx
->ct_validation_callback
,
966 ctx
->ct_validation_callback_arg
))
970 s
->ssl_pkey_num
= SSL_PKEY_NUM
+ ctx
->sigalg_list_len
;
973 ERR_raise(ERR_LIB_SSL
, ERR_R_CRYPTO_LIB
);
976 ERR_raise(ERR_LIB_SSL
, ERR_R_ASN1_LIB
);
979 ERR_raise(ERR_LIB_SSL
, ERR_R_SSL_LIB
);
985 SSL
*ossl_ssl_connection_new(SSL_CTX
*ctx
)
987 return ossl_ssl_connection_new_int(ctx
, NULL
, ctx
->method
);
990 int SSL_is_dtls(const SSL
*s
)
992 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
994 #ifndef OPENSSL_NO_QUIC
995 if (s
->type
== SSL_TYPE_QUIC_CONNECTION
|| s
->type
== SSL_TYPE_QUIC_XSO
)
1002 return SSL_CONNECTION_IS_DTLS(sc
) ? 1 : 0;
1005 int SSL_is_tls(const SSL
*s
)
1007 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1009 #ifndef OPENSSL_NO_QUIC
1010 if (s
->type
== SSL_TYPE_QUIC_CONNECTION
|| s
->type
== SSL_TYPE_QUIC_XSO
)
1017 return SSL_CONNECTION_IS_DTLS(sc
) ? 0 : 1;
1020 int SSL_is_quic(const SSL
*s
)
1025 int SSL_up_ref(SSL
*s
)
1029 if (CRYPTO_UP_REF(&s
->references
, &i
) <= 0)
1032 REF_PRINT_COUNT("SSL", i
, s
);
1033 REF_ASSERT_ISNT(i
< 2);
1034 return ((i
> 1) ? 1 : 0);
1037 int SSL_CTX_set_session_id_context(SSL_CTX
*ctx
, const unsigned char *sid_ctx
,
1038 unsigned int sid_ctx_len
)
1040 if (sid_ctx_len
> SSL_MAX_SID_CTX_LENGTH
) {
1041 ERR_raise(ERR_LIB_SSL
, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG
);
1044 ctx
->sid_ctx_length
= sid_ctx_len
;
1045 memcpy(ctx
->sid_ctx
, sid_ctx
, sid_ctx_len
);
1050 int SSL_set_session_id_context(SSL
*ssl
, const unsigned char *sid_ctx
,
1051 unsigned int sid_ctx_len
)
1053 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
1058 if (sid_ctx_len
> SSL_MAX_SID_CTX_LENGTH
) {
1059 ERR_raise(ERR_LIB_SSL
, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG
);
1062 sc
->sid_ctx_length
= sid_ctx_len
;
1063 memcpy(sc
->sid_ctx
, sid_ctx
, sid_ctx_len
);
1068 int SSL_CTX_set_generate_session_id(SSL_CTX
*ctx
, GEN_SESSION_CB cb
)
1070 if (!CRYPTO_THREAD_write_lock(ctx
->lock
))
1072 ctx
->generate_session_id
= cb
;
1073 CRYPTO_THREAD_unlock(ctx
->lock
);
1077 int SSL_set_generate_session_id(SSL
*ssl
, GEN_SESSION_CB cb
)
1079 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
1081 if (sc
== NULL
|| !CRYPTO_THREAD_write_lock(ssl
->lock
))
1083 sc
->generate_session_id
= cb
;
1084 CRYPTO_THREAD_unlock(ssl
->lock
);
1088 int SSL_has_matching_session_id(const SSL
*ssl
, const unsigned char *id
,
1089 unsigned int id_len
)
1092 * A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
1093 * we can "construct" a session to give us the desired check - i.e. to
1094 * find if there's a session in the hash table that would conflict with
1095 * any new session built out of this id/id_len and the ssl_version in use
1099 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(ssl
);
1101 if (sc
== NULL
|| id_len
> sizeof(r
.session_id
))
1104 r
.ssl_version
= sc
->version
;
1105 r
.session_id_length
= id_len
;
1106 memcpy(r
.session_id
, id
, id_len
);
1108 if (!CRYPTO_THREAD_read_lock(sc
->session_ctx
->lock
))
1110 p
= lh_SSL_SESSION_retrieve(sc
->session_ctx
->sessions
, &r
);
1111 CRYPTO_THREAD_unlock(sc
->session_ctx
->lock
);
1115 int SSL_CTX_set_purpose(SSL_CTX
*s
, int purpose
)
1117 return X509_VERIFY_PARAM_set_purpose(s
->param
, purpose
);
1120 int SSL_set_purpose(SSL
*s
, int purpose
)
1122 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1127 return X509_VERIFY_PARAM_set_purpose(sc
->param
, purpose
);
1130 int SSL_CTX_set_trust(SSL_CTX
*s
, int trust
)
1132 return X509_VERIFY_PARAM_set_trust(s
->param
, trust
);
1135 int SSL_set_trust(SSL
*s
, int trust
)
1137 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1142 return X509_VERIFY_PARAM_set_trust(sc
->param
, trust
);
1145 int SSL_set1_host(SSL
*s
, const char *host
)
1147 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1152 /* clear hostname(s) and IP address in any case, also if host parses as an IP address */
1153 (void)X509_VERIFY_PARAM_set1_host(sc
->param
, NULL
, 0);
1154 (void)X509_VERIFY_PARAM_set1_ip(sc
->param
, NULL
, 0);
1158 /* If a host is provided and parses as an IP address, treat it as such. */
1159 return X509_VERIFY_PARAM_set1_ip_asc(sc
->param
, host
)
1160 || X509_VERIFY_PARAM_set1_host(sc
->param
, host
, 0);
1163 int SSL_add1_host(SSL
*s
, const char *host
)
1165 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1170 /* If a host is provided and parses as an IP address, treat it as such. */
1172 ASN1_OCTET_STRING
*ip
;
1175 ip
= a2i_IPADDRESS(host
);
1177 /* We didn't want it; only to check if it *is* an IP address */
1178 ASN1_OCTET_STRING_free(ip
);
1180 old_ip
= X509_VERIFY_PARAM_get1_ip_asc(sc
->param
);
1181 if (old_ip
!= NULL
) {
1182 OPENSSL_free(old_ip
);
1183 /* There can be only one IP address */
1184 ERR_raise_data(ERR_LIB_SSL
, ERR_R_PASSED_INVALID_ARGUMENT
,
1185 "IP address was already set");
1189 return X509_VERIFY_PARAM_set1_ip_asc(sc
->param
, host
);
1193 return X509_VERIFY_PARAM_add1_host(sc
->param
, host
, 0);
1196 void SSL_set_hostflags(SSL
*s
, unsigned int flags
)
1198 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1203 X509_VERIFY_PARAM_set_hostflags(sc
->param
, flags
);
1206 const char *SSL_get0_peername(SSL
*s
)
1208 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1213 return X509_VERIFY_PARAM_get0_peername(sc
->param
);
1216 int SSL_CTX_dane_enable(SSL_CTX
*ctx
)
1218 return dane_ctx_enable(&ctx
->dane
);
1221 unsigned long SSL_CTX_dane_set_flags(SSL_CTX
*ctx
, unsigned long flags
)
1223 unsigned long orig
= ctx
->dane
.flags
;
1225 ctx
->dane
.flags
|= flags
;
1229 unsigned long SSL_CTX_dane_clear_flags(SSL_CTX
*ctx
, unsigned long flags
)
1231 unsigned long orig
= ctx
->dane
.flags
;
1233 ctx
->dane
.flags
&= ~flags
;
1237 int SSL_dane_enable(SSL
*s
, const char *basedomain
)
1240 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1246 if (s
->ctx
->dane
.mdmax
== 0) {
1247 ERR_raise(ERR_LIB_SSL
, SSL_R_CONTEXT_NOT_DANE_ENABLED
);
1250 if (dane
->trecs
!= NULL
) {
1251 ERR_raise(ERR_LIB_SSL
, SSL_R_DANE_ALREADY_ENABLED
);
1256 * Default SNI name. This rejects empty names, while set1_host below
1257 * accepts them and disables hostname checks. To avoid side-effects with
1258 * invalid input, set the SNI name first.
1260 if (sc
->ext
.hostname
== NULL
) {
1261 if (!SSL_set_tlsext_host_name(s
, basedomain
)) {
1262 ERR_raise(ERR_LIB_SSL
, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN
);
1267 /* Primary RFC6125 reference identifier */
1268 if (!X509_VERIFY_PARAM_set1_host(sc
->param
, basedomain
, 0)) {
1269 ERR_raise(ERR_LIB_SSL
, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN
);
1275 dane
->dctx
= &s
->ctx
->dane
;
1276 dane
->trecs
= sk_danetls_record_new_null();
1278 if (dane
->trecs
== NULL
) {
1279 ERR_raise(ERR_LIB_SSL
, ERR_R_CRYPTO_LIB
);
1285 unsigned long SSL_dane_set_flags(SSL
*ssl
, unsigned long flags
)
1288 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
1293 orig
= sc
->dane
.flags
;
1295 sc
->dane
.flags
|= flags
;
1299 unsigned long SSL_dane_clear_flags(SSL
*ssl
, unsigned long flags
)
1302 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
1307 orig
= sc
->dane
.flags
;
1309 sc
->dane
.flags
&= ~flags
;
1313 int SSL_get0_dane_authority(SSL
*s
, X509
**mcert
, EVP_PKEY
**mspki
)
1316 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1323 if (!DANETLS_ENABLED(dane
) || sc
->verify_result
!= X509_V_OK
)
1327 *mcert
= dane
->mcert
;
1329 *mspki
= (dane
->mcert
== NULL
) ? dane
->mtlsa
->spki
: NULL
;
1334 int SSL_get0_dane_tlsa(SSL
*s
, uint8_t *usage
, uint8_t *selector
,
1335 uint8_t *mtype
, const unsigned char **data
, size_t *dlen
)
1338 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1345 if (!DANETLS_ENABLED(dane
) || sc
->verify_result
!= X509_V_OK
)
1349 *usage
= dane
->mtlsa
->usage
;
1351 *selector
= dane
->mtlsa
->selector
;
1353 *mtype
= dane
->mtlsa
->mtype
;
1355 *data
= dane
->mtlsa
->data
;
1357 *dlen
= dane
->mtlsa
->dlen
;
1362 SSL_DANE
*SSL_get0_dane(SSL
*s
)
1364 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1372 int SSL_dane_tlsa_add(SSL
*s
, uint8_t usage
, uint8_t selector
,
1373 uint8_t mtype
, const unsigned char *data
, size_t dlen
)
1375 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1380 return dane_tlsa_add(&sc
->dane
, usage
, selector
, mtype
, data
, dlen
);
1383 int SSL_CTX_dane_mtype_set(SSL_CTX
*ctx
, const EVP_MD
*md
, uint8_t mtype
,
1386 return dane_mtype_set(&ctx
->dane
, md
, mtype
, ord
);
1389 int SSL_CTX_set1_param(SSL_CTX
*ctx
, X509_VERIFY_PARAM
*vpm
)
1391 return X509_VERIFY_PARAM_set1(ctx
->param
, vpm
);
1394 int SSL_set1_param(SSL
*ssl
, X509_VERIFY_PARAM
*vpm
)
1396 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
1401 return X509_VERIFY_PARAM_set1(sc
->param
, vpm
);
1404 X509_VERIFY_PARAM
*SSL_CTX_get0_param(SSL_CTX
*ctx
)
1409 X509_VERIFY_PARAM
*SSL_get0_param(SSL
*ssl
)
1411 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
1419 void SSL_certs_clear(SSL
*s
)
1421 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1426 ssl_cert_clear_certs(sc
->cert
);
1429 void SSL_free(SSL
*s
)
1435 CRYPTO_DOWN_REF(&s
->references
, &i
);
1436 REF_PRINT_COUNT("SSL", i
, s
);
1439 REF_ASSERT_ISNT(i
< 0);
1441 if (s
->method
!= NULL
)
1442 s
->method
->ssl_free(s
);
1444 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL
, s
, &s
->ex_data
);
1445 SSL_CTX_free(s
->ctx
);
1446 CRYPTO_THREAD_lock_free(s
->lock
);
1447 CRYPTO_FREE_REF(&s
->references
);
1452 void ossl_ssl_connection_free(SSL
*ssl
)
1456 s
= SSL_CONNECTION_FROM_SSL_ONLY(ssl
);
1461 * Ignore return values. This could result in user callbacks being called
1462 * e.g. for the QUIC TLS record layer. So we do this early before we have
1463 * freed other things.
1465 ssl_free_wbio_buffer(s
);
1466 RECORD_LAYER_clear(&s
->rlayer
);
1468 X509_VERIFY_PARAM_free(s
->param
);
1469 dane_final(&s
->dane
);
1471 BUF_MEM_free(s
->init_buf
);
1473 /* add extra stuff */
1474 sk_SSL_CIPHER_free(s
->cipher_list
);
1475 sk_SSL_CIPHER_free(s
->cipher_list_by_id
);
1476 sk_SSL_CIPHER_free(s
->tls13_ciphersuites
);
1477 sk_SSL_CIPHER_free(s
->peer_ciphers
);
1479 /* Make the next call work :-) */
1480 if (s
->session
!= NULL
) {
1481 ssl_clear_bad_session(s
);
1482 SSL_SESSION_free(s
->session
);
1484 SSL_SESSION_free(s
->psksession
);
1485 OPENSSL_free(s
->psksession_id
);
1487 ssl_cert_free(s
->cert
);
1488 OPENSSL_free(s
->shared_sigalgs
);
1489 /* Free up if allocated */
1491 OPENSSL_free(s
->ext
.hostname
);
1492 SSL_CTX_free(s
->session_ctx
);
1493 OPENSSL_free(s
->ext
.ecpointformats
);
1494 OPENSSL_free(s
->ext
.peer_ecpointformats
);
1495 OPENSSL_free(s
->ext
.supportedgroups
);
1496 OPENSSL_free(s
->ext
.keyshares
);
1497 OPENSSL_free(s
->ext
.tuples
);
1498 OPENSSL_free(s
->ext
.peer_supportedgroups
);
1499 sk_X509_EXTENSION_pop_free(s
->ext
.ocsp
.exts
, X509_EXTENSION_free
);
1500 #ifndef OPENSSL_NO_OCSP
1501 sk_OCSP_RESPID_pop_free(s
->ext
.ocsp
.ids
, OCSP_RESPID_free
);
1503 #ifndef OPENSSL_NO_CT
1504 SCT_LIST_free(s
->scts
);
1505 OPENSSL_free(s
->ext
.scts
);
1507 OPENSSL_free(s
->ext
.ocsp
.resp
);
1508 OPENSSL_free(s
->ext
.alpn
);
1509 OPENSSL_free(s
->ext
.tls13_cookie
);
1510 if (s
->clienthello
!= NULL
)
1511 OPENSSL_free(s
->clienthello
->pre_proc_exts
);
1512 OPENSSL_free(s
->clienthello
);
1513 OPENSSL_free(s
->pha_context
);
1514 EVP_MD_CTX_free(s
->pha_dgst
);
1516 sk_X509_NAME_pop_free(s
->ca_names
, X509_NAME_free
);
1517 sk_X509_NAME_pop_free(s
->client_ca_names
, X509_NAME_free
);
1519 OPENSSL_free(s
->client_cert_type
);
1520 OPENSSL_free(s
->server_cert_type
);
1522 OSSL_STACK_OF_X509_free(s
->verified_chain
);
1524 if (ssl
->method
!= NULL
)
1525 ssl
->method
->ssl_deinit(ssl
);
1527 ASYNC_WAIT_CTX_free(s
->waitctx
);
1529 #if !defined(OPENSSL_NO_NEXTPROTONEG)
1530 OPENSSL_free(s
->ext
.npn
);
1533 #ifndef OPENSSL_NO_SRTP
1534 sk_SRTP_PROTECTION_PROFILE_free(s
->srtp_profiles
);
1538 * We do this late. We want to ensure that any other references we held to
1539 * these BIOs are freed first *before* we call BIO_free_all(), because
1540 * BIO_free_all() will only free each BIO in the chain if the number of
1541 * references to the first BIO have dropped to 0
1543 BIO_free_all(s
->wbio
);
1545 BIO_free_all(s
->rbio
);
1547 OPENSSL_free(s
->s3
.tmp
.valid_flags
);
1550 void SSL_set0_rbio(SSL
*s
, BIO
*rbio
)
1552 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1554 #ifndef OPENSSL_NO_QUIC
1556 ossl_quic_conn_set0_net_rbio(s
, rbio
);
1564 BIO_free_all(sc
->rbio
);
1566 sc
->rlayer
.rrlmethod
->set1_bio(sc
->rlayer
.rrl
, sc
->rbio
);
1569 void SSL_set0_wbio(SSL
*s
, BIO
*wbio
)
1571 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1573 #ifndef OPENSSL_NO_QUIC
1575 ossl_quic_conn_set0_net_wbio(s
, wbio
);
1584 * If the output buffering BIO is still in place, remove it
1586 if (sc
->bbio
!= NULL
)
1587 sc
->wbio
= BIO_pop(sc
->wbio
);
1589 BIO_free_all(sc
->wbio
);
1592 /* Re-attach |bbio| to the new |wbio|. */
1593 if (sc
->bbio
!= NULL
)
1594 sc
->wbio
= BIO_push(sc
->bbio
, sc
->wbio
);
1596 sc
->rlayer
.wrlmethod
->set1_bio(sc
->rlayer
.wrl
, sc
->wbio
);
1599 void SSL_set_bio(SSL
*s
, BIO
*rbio
, BIO
*wbio
)
1602 * For historical reasons, this function has many different cases in
1603 * ownership handling.
1606 /* If nothing has changed, do nothing */
1607 if (rbio
== SSL_get_rbio(s
) && wbio
== SSL_get_wbio(s
))
1611 * If the two arguments are equal then one fewer reference is granted by the
1612 * caller than we want to take
1614 if (rbio
!= NULL
&& rbio
== wbio
) {
1615 if (!BIO_up_ref(rbio
))
1620 * If only the wbio is changed only adopt one reference.
1622 if (rbio
== SSL_get_rbio(s
)) {
1623 SSL_set0_wbio(s
, wbio
);
1627 * There is an asymmetry here for historical reasons. If only the rbio is
1628 * changed AND the rbio and wbio were originally different, then we only
1629 * adopt one reference.
1631 if (wbio
== SSL_get_wbio(s
) && SSL_get_rbio(s
) != SSL_get_wbio(s
)) {
1632 SSL_set0_rbio(s
, rbio
);
1636 /* Otherwise, adopt both references. */
1637 SSL_set0_rbio(s
, rbio
);
1638 SSL_set0_wbio(s
, wbio
);
1641 BIO
*SSL_get_rbio(const SSL
*s
)
1643 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
1645 #ifndef OPENSSL_NO_QUIC
1647 return ossl_quic_conn_get_net_rbio(s
);
1656 BIO
*SSL_get_wbio(const SSL
*s
)
1658 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
1660 #ifndef OPENSSL_NO_QUIC
1662 return ossl_quic_conn_get_net_wbio(s
);
1668 if (sc
->bbio
!= NULL
) {
1670 * If |bbio| is active, the true caller-configured BIO is its
1673 return BIO_next(sc
->bbio
);
1678 int SSL_get_fd(const SSL
*s
)
1680 return SSL_get_rfd(s
);
1683 int SSL_get_rfd(const SSL
*s
)
1688 b
= SSL_get_rbio(s
);
1689 r
= BIO_find_type(b
, BIO_TYPE_DESCRIPTOR
);
1691 BIO_get_fd(r
, &ret
);
1695 int SSL_get_wfd(const SSL
*s
)
1700 b
= SSL_get_wbio(s
);
1701 r
= BIO_find_type(b
, BIO_TYPE_DESCRIPTOR
);
1703 BIO_get_fd(r
, &ret
);
1707 #ifndef OPENSSL_NO_SOCK
1708 static const BIO_METHOD
*fd_method(SSL
*s
)
1710 #ifndef OPENSSL_NO_DGRAM
1712 return BIO_s_datagram();
1715 return BIO_s_socket();
1718 int SSL_set_fd(SSL
*s
, int fd
)
1723 if (s
->type
== SSL_TYPE_QUIC_XSO
) {
1724 ERR_raise(ERR_LIB_SSL
, SSL_R_CONN_USE_ONLY
);
1728 bio
= BIO_new(fd_method(s
));
1731 ERR_raise(ERR_LIB_SSL
, ERR_R_BUF_LIB
);
1734 BIO_set_fd(bio
, fd
, BIO_NOCLOSE
);
1735 SSL_set_bio(s
, bio
, bio
);
1736 #ifndef OPENSSL_NO_KTLS
1738 * The new socket is created successfully regardless of ktls_enable.
1739 * ktls_enable doesn't change any functionality of the socket, except
1740 * changing the setsockopt to enable the processing of ktls_start.
1741 * Thus, it is not a problem to call it for non-TLS sockets.
1744 #endif /* OPENSSL_NO_KTLS */
1750 int SSL_set_wfd(SSL
*s
, int fd
)
1752 BIO
*rbio
= SSL_get_rbio(s
);
1753 int desired_type
= IS_QUIC(s
) ? BIO_TYPE_DGRAM
: BIO_TYPE_SOCKET
;
1755 if (s
->type
== SSL_TYPE_QUIC_XSO
) {
1756 ERR_raise(ERR_LIB_SSL
, SSL_R_CONN_USE_ONLY
);
1760 if (rbio
== NULL
|| BIO_method_type(rbio
) != desired_type
1761 || (int)BIO_get_fd(rbio
, NULL
) != fd
) {
1762 BIO
*bio
= BIO_new(fd_method(s
));
1765 ERR_raise(ERR_LIB_SSL
, ERR_R_BUF_LIB
);
1768 BIO_set_fd(bio
, fd
, BIO_NOCLOSE
);
1769 SSL_set0_wbio(s
, bio
);
1770 #ifndef OPENSSL_NO_KTLS
1772 * The new socket is created successfully regardless of ktls_enable.
1773 * ktls_enable doesn't change any functionality of the socket, except
1774 * changing the setsockopt to enable the processing of ktls_start.
1775 * Thus, it is not a problem to call it for non-TLS sockets.
1778 #endif /* OPENSSL_NO_KTLS */
1780 if (!BIO_up_ref(rbio
))
1782 SSL_set0_wbio(s
, rbio
);
1787 int SSL_set_rfd(SSL
*s
, int fd
)
1789 BIO
*wbio
= SSL_get_wbio(s
);
1790 int desired_type
= IS_QUIC(s
) ? BIO_TYPE_DGRAM
: BIO_TYPE_SOCKET
;
1792 if (s
->type
== SSL_TYPE_QUIC_XSO
) {
1793 ERR_raise(ERR_LIB_SSL
, SSL_R_CONN_USE_ONLY
);
1797 if (wbio
== NULL
|| BIO_method_type(wbio
) != desired_type
1798 || ((int)BIO_get_fd(wbio
, NULL
) != fd
)) {
1799 BIO
*bio
= BIO_new(fd_method(s
));
1802 ERR_raise(ERR_LIB_SSL
, ERR_R_BUF_LIB
);
1805 BIO_set_fd(bio
, fd
, BIO_NOCLOSE
);
1806 SSL_set0_rbio(s
, bio
);
1808 if (!BIO_up_ref(wbio
))
1810 SSL_set0_rbio(s
, wbio
);
1817 /* return length of latest Finished message we sent, copy to 'buf' */
1818 size_t SSL_get_finished(const SSL
*s
, void *buf
, size_t count
)
1821 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
1826 ret
= sc
->s3
.tmp
.finish_md_len
;
1829 memcpy(buf
, sc
->s3
.tmp
.finish_md
, count
);
1833 /* return length of latest Finished message we expected, copy to 'buf' */
1834 size_t SSL_get_peer_finished(const SSL
*s
, void *buf
, size_t count
)
1837 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
1842 ret
= sc
->s3
.tmp
.peer_finish_md_len
;
1845 memcpy(buf
, sc
->s3
.tmp
.peer_finish_md
, count
);
1849 int SSL_get_verify_mode(const SSL
*s
)
1851 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
1856 return sc
->verify_mode
;
1859 int SSL_get_verify_depth(const SSL
*s
)
1861 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
1866 return X509_VERIFY_PARAM_get_depth(sc
->param
);
1869 int (*SSL_get_verify_callback(const SSL
*s
)) (int, X509_STORE_CTX
*) {
1870 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
1875 return sc
->verify_callback
;
1878 int SSL_CTX_get_verify_mode(const SSL_CTX
*ctx
)
1880 return ctx
->verify_mode
;
1883 int SSL_CTX_get_verify_depth(const SSL_CTX
*ctx
)
1885 return X509_VERIFY_PARAM_get_depth(ctx
->param
);
1888 int (*SSL_CTX_get_verify_callback(const SSL_CTX
*ctx
)) (int, X509_STORE_CTX
*) {
1889 return ctx
->default_verify_callback
;
1892 void SSL_set_verify(SSL
*s
, int mode
,
1893 int (*callback
) (int ok
, X509_STORE_CTX
*ctx
))
1895 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1900 sc
->verify_mode
= mode
;
1901 if (callback
!= NULL
)
1902 sc
->verify_callback
= callback
;
1905 void SSL_set_verify_depth(SSL
*s
, int depth
)
1907 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1912 X509_VERIFY_PARAM_set_depth(sc
->param
, depth
);
1915 void SSL_set_read_ahead(SSL
*s
, int yes
)
1917 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(s
);
1918 OSSL_PARAM options
[2], *opts
= options
;
1923 RECORD_LAYER_set_read_ahead(&sc
->rlayer
, yes
);
1925 *opts
++ = OSSL_PARAM_construct_int(OSSL_LIBSSL_RECORD_LAYER_PARAM_READ_AHEAD
,
1926 &sc
->rlayer
.read_ahead
);
1927 *opts
= OSSL_PARAM_construct_end();
1929 /* Ignore return value */
1930 sc
->rlayer
.rrlmethod
->set_options(sc
->rlayer
.rrl
, options
);
1933 int SSL_get_read_ahead(const SSL
*s
)
1935 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL_ONLY(s
);
1940 return RECORD_LAYER_get_read_ahead(&sc
->rlayer
);
1943 int SSL_pending(const SSL
*s
)
1945 size_t pending
= s
->method
->ssl_pending(s
);
1948 * SSL_pending cannot work properly if read-ahead is enabled
1949 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
1950 * impossible to fix since SSL_pending cannot report errors that may be
1951 * observed while scanning the new data. (Note that SSL_pending() is
1952 * often used as a boolean value, so we'd better not return -1.)
1954 * SSL_pending also cannot work properly if the value >INT_MAX. In that case
1955 * we just return INT_MAX.
1957 return pending
< INT_MAX
? (int)pending
: INT_MAX
;
1960 int SSL_has_pending(const SSL
*s
)
1963 * Similar to SSL_pending() but returns a 1 to indicate that we have
1964 * processed or unprocessed data available or 0 otherwise (as opposed to the
1965 * number of bytes available). Unlike SSL_pending() this will take into
1966 * account read_ahead data. A 1 return simply indicates that we have data.
1967 * That data may not result in any application data, or we may fail to parse
1968 * the records for some reason.
1970 const SSL_CONNECTION
*sc
;
1972 #ifndef OPENSSL_NO_QUIC
1974 return ossl_quic_has_pending(s
);
1977 sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
1979 /* Check buffered app data if any first */
1980 if (SSL_CONNECTION_IS_DTLS(sc
)) {
1984 iter
= pqueue_iterator(sc
->rlayer
.d
->buffered_app_data
);
1985 while ((item
= pqueue_next(&iter
)) != NULL
) {
1987 if (rdata
->length
> 0)
1992 if (RECORD_LAYER_processed_read_pending(&sc
->rlayer
))
1995 return RECORD_LAYER_read_pending(&sc
->rlayer
);
1998 X509
*SSL_get1_peer_certificate(const SSL
*s
)
2000 X509
*r
= SSL_get0_peer_certificate(s
);
2002 if (r
!= NULL
&& !X509_up_ref(r
))
2008 X509
*SSL_get0_peer_certificate(const SSL
*s
)
2010 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
2015 if (sc
->session
== NULL
)
2018 return sc
->session
->peer
;
2021 STACK_OF(X509
) *SSL_get_peer_cert_chain(const SSL
*s
)
2024 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
2029 if (sc
->session
== NULL
)
2032 r
= sc
->session
->peer_chain
;
2035 * If we are a client, cert_chain includes the peer's own certificate; if
2036 * we are a server, it does not.
2043 * Now in theory, since the calling process own 't' it should be safe to
2044 * modify. We need to be able to read f without being hassled
2046 int SSL_copy_session_id(SSL
*t
, const SSL
*f
)
2049 /* TODO(QUIC FUTURE): Not allowed for QUIC currently. */
2050 SSL_CONNECTION
*tsc
= SSL_CONNECTION_FROM_SSL_ONLY(t
);
2051 const SSL_CONNECTION
*fsc
= SSL_CONNECTION_FROM_CONST_SSL_ONLY(f
);
2053 if (tsc
== NULL
|| fsc
== NULL
)
2056 /* Do we need to do SSL locking? */
2057 if (!SSL_set_session(t
, SSL_get_session(f
))) {
2062 * what if we are setup for one protocol version but want to talk another
2064 if (t
->method
!= f
->method
) {
2065 t
->method
->ssl_deinit(t
);
2066 t
->method
= f
->method
;
2067 if (t
->method
->ssl_init(t
) == 0)
2071 CRYPTO_UP_REF(&fsc
->cert
->references
, &i
);
2072 ssl_cert_free(tsc
->cert
);
2073 tsc
->cert
= fsc
->cert
;
2074 if (!SSL_set_session_id_context(t
, fsc
->sid_ctx
, (int)fsc
->sid_ctx_length
)) {
2081 /* Fix this so it checks all the valid key/cert options */
2082 int SSL_CTX_check_private_key(const SSL_CTX
*ctx
)
2084 if ((ctx
== NULL
) || (ctx
->cert
->key
->x509
== NULL
)) {
2085 ERR_raise(ERR_LIB_SSL
, SSL_R_NO_CERTIFICATE_ASSIGNED
);
2088 if (ctx
->cert
->key
->privatekey
== NULL
) {
2089 ERR_raise(ERR_LIB_SSL
, SSL_R_NO_PRIVATE_KEY_ASSIGNED
);
2092 return X509_check_private_key
2093 (ctx
->cert
->key
->x509
, ctx
->cert
->key
->privatekey
);
2096 /* Fix this function so that it takes an optional type parameter */
2097 int SSL_check_private_key(const SSL
*ssl
)
2099 const SSL_CONNECTION
*sc
;
2101 if ((sc
= SSL_CONNECTION_FROM_CONST_SSL(ssl
)) == NULL
) {
2102 ERR_raise(ERR_LIB_SSL
, ERR_R_PASSED_NULL_PARAMETER
);
2105 if (sc
->cert
->key
->x509
== NULL
) {
2106 ERR_raise(ERR_LIB_SSL
, SSL_R_NO_CERTIFICATE_ASSIGNED
);
2109 if (sc
->cert
->key
->privatekey
== NULL
) {
2110 ERR_raise(ERR_LIB_SSL
, SSL_R_NO_PRIVATE_KEY_ASSIGNED
);
2113 return X509_check_private_key(sc
->cert
->key
->x509
,
2114 sc
->cert
->key
->privatekey
);
2117 int SSL_waiting_for_async(SSL
*s
)
2119 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2130 int SSL_get_all_async_fds(SSL
*s
, OSSL_ASYNC_FD
*fds
, size_t *numfds
)
2132 ASYNC_WAIT_CTX
*ctx
;
2133 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2138 if ((ctx
= sc
->waitctx
) == NULL
)
2140 return ASYNC_WAIT_CTX_get_all_fds(ctx
, fds
, numfds
);
2143 int SSL_get_changed_async_fds(SSL
*s
, OSSL_ASYNC_FD
*addfd
, size_t *numaddfds
,
2144 OSSL_ASYNC_FD
*delfd
, size_t *numdelfds
)
2146 ASYNC_WAIT_CTX
*ctx
;
2147 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2152 if ((ctx
= sc
->waitctx
) == NULL
)
2154 return ASYNC_WAIT_CTX_get_changed_fds(ctx
, addfd
, numaddfds
, delfd
,
2158 int SSL_CTX_set_async_callback(SSL_CTX
*ctx
, SSL_async_callback_fn callback
)
2160 ctx
->async_cb
= callback
;
2164 int SSL_CTX_set_async_callback_arg(SSL_CTX
*ctx
, void *arg
)
2166 ctx
->async_cb_arg
= arg
;
2170 int SSL_set_async_callback(SSL
*s
, SSL_async_callback_fn callback
)
2172 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2177 sc
->async_cb
= callback
;
2181 int SSL_set_async_callback_arg(SSL
*s
, void *arg
)
2183 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2188 sc
->async_cb_arg
= arg
;
2192 int SSL_get_async_status(SSL
*s
, int *status
)
2194 ASYNC_WAIT_CTX
*ctx
;
2195 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2200 if ((ctx
= sc
->waitctx
) == NULL
)
2202 *status
= ASYNC_WAIT_CTX_get_status(ctx
);
2206 int SSL_accept(SSL
*s
)
2208 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2210 #ifndef OPENSSL_NO_QUIC
2212 return s
->method
->ssl_accept(s
);
2218 if (sc
->handshake_func
== NULL
) {
2219 /* Not properly initialized yet */
2220 SSL_set_accept_state(s
);
2223 return SSL_do_handshake(s
);
2226 int SSL_connect(SSL
*s
)
2228 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2230 #ifndef OPENSSL_NO_QUIC
2232 return s
->method
->ssl_connect(s
);
2238 if (sc
->handshake_func
== NULL
) {
2239 /* Not properly initialized yet */
2240 SSL_set_connect_state(s
);
2243 return SSL_do_handshake(s
);
2246 long SSL_get_default_timeout(const SSL
*s
)
2248 return (long int)ossl_time2seconds(s
->method
->get_timeout());
2251 static int ssl_async_wait_ctx_cb(void *arg
)
2253 SSL
*s
= (SSL
*)arg
;
2254 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2259 return sc
->async_cb(s
, sc
->async_cb_arg
);
2262 static int ssl_start_async_job(SSL
*s
, struct ssl_async_args
*args
,
2263 int (*func
) (void *))
2266 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2271 if (sc
->waitctx
== NULL
) {
2272 sc
->waitctx
= ASYNC_WAIT_CTX_new();
2273 if (sc
->waitctx
== NULL
)
2275 if (sc
->async_cb
!= NULL
2276 && !ASYNC_WAIT_CTX_set_callback
2277 (sc
->waitctx
, ssl_async_wait_ctx_cb
, s
))
2281 sc
->rwstate
= SSL_NOTHING
;
2282 switch (ASYNC_start_job(&sc
->job
, sc
->waitctx
, &ret
, func
, args
,
2283 sizeof(struct ssl_async_args
))) {
2285 sc
->rwstate
= SSL_NOTHING
;
2286 ERR_raise(ERR_LIB_SSL
, SSL_R_FAILED_TO_INIT_ASYNC
);
2289 sc
->rwstate
= SSL_ASYNC_PAUSED
;
2292 sc
->rwstate
= SSL_ASYNC_NO_JOBS
;
2298 sc
->rwstate
= SSL_NOTHING
;
2299 ERR_raise(ERR_LIB_SSL
, ERR_R_INTERNAL_ERROR
);
2300 /* Shouldn't happen */
2305 static int ssl_io_intern(void *vargs
)
2307 struct ssl_async_args
*args
;
2313 args
= (struct ssl_async_args
*)vargs
;
2317 if ((sc
= SSL_CONNECTION_FROM_SSL(s
)) == NULL
)
2320 switch (args
->type
) {
2322 return args
->f
.func_read(s
, buf
, num
, &sc
->asyncrw
);
2324 return args
->f
.func_write(s
, buf
, num
, &sc
->asyncrw
);
2326 return args
->f
.func_other(s
);
2331 int ssl_read_internal(SSL
*s
, void *buf
, size_t num
, size_t *readbytes
)
2333 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2335 #ifndef OPENSSL_NO_QUIC
2337 return s
->method
->ssl_read(s
, buf
, num
, readbytes
);
2343 if (sc
->handshake_func
== NULL
) {
2344 ERR_raise(ERR_LIB_SSL
, SSL_R_UNINITIALIZED
);
2348 if (sc
->shutdown
& SSL_RECEIVED_SHUTDOWN
) {
2349 sc
->rwstate
= SSL_NOTHING
;
2353 if (sc
->early_data_state
== SSL_EARLY_DATA_CONNECT_RETRY
2354 || sc
->early_data_state
== SSL_EARLY_DATA_ACCEPT_RETRY
) {
2355 ERR_raise(ERR_LIB_SSL
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
2359 * If we are a client and haven't received the ServerHello etc then we
2362 if (!ossl_statem_check_finish_init(sc
, 0))
2365 if ((sc
->mode
& SSL_MODE_ASYNC
) && ASYNC_get_current_job() == NULL
) {
2366 struct ssl_async_args args
;
2372 args
.type
= READFUNC
;
2373 args
.f
.func_read
= s
->method
->ssl_read
;
2375 ret
= ssl_start_async_job(s
, &args
, ssl_io_intern
);
2376 *readbytes
= sc
->asyncrw
;
2379 return s
->method
->ssl_read(s
, buf
, num
, readbytes
);
2383 int SSL_read(SSL
*s
, void *buf
, int num
)
2389 ERR_raise(ERR_LIB_SSL
, SSL_R_BAD_LENGTH
);
2393 ret
= ssl_read_internal(s
, buf
, (size_t)num
, &readbytes
);
2396 * The cast is safe here because ret should be <= INT_MAX because num is
2400 ret
= (int)readbytes
;
2405 int SSL_read_ex(SSL
*s
, void *buf
, size_t num
, size_t *readbytes
)
2407 int ret
= ssl_read_internal(s
, buf
, num
, readbytes
);
2414 int SSL_read_early_data(SSL
*s
, void *buf
, size_t num
, size_t *readbytes
)
2417 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(s
);
2419 /* TODO(QUIC 0RTT): 0-RTT support */
2420 if (sc
== NULL
|| !sc
->server
) {
2421 ERR_raise(ERR_LIB_SSL
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
2422 return SSL_READ_EARLY_DATA_ERROR
;
2425 switch (sc
->early_data_state
) {
2426 case SSL_EARLY_DATA_NONE
:
2427 if (!SSL_in_before(s
)) {
2428 ERR_raise(ERR_LIB_SSL
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
2429 return SSL_READ_EARLY_DATA_ERROR
;
2433 case SSL_EARLY_DATA_ACCEPT_RETRY
:
2434 sc
->early_data_state
= SSL_EARLY_DATA_ACCEPTING
;
2435 ret
= SSL_accept(s
);
2438 sc
->early_data_state
= SSL_EARLY_DATA_ACCEPT_RETRY
;
2439 return SSL_READ_EARLY_DATA_ERROR
;
2443 case SSL_EARLY_DATA_READ_RETRY
:
2444 if (sc
->ext
.early_data
== SSL_EARLY_DATA_ACCEPTED
) {
2445 sc
->early_data_state
= SSL_EARLY_DATA_READING
;
2446 ret
= SSL_read_ex(s
, buf
, num
, readbytes
);
2448 * State machine will update early_data_state to
2449 * SSL_EARLY_DATA_FINISHED_READING if we get an EndOfEarlyData
2452 if (ret
> 0 || (ret
<= 0 && sc
->early_data_state
2453 != SSL_EARLY_DATA_FINISHED_READING
)) {
2454 sc
->early_data_state
= SSL_EARLY_DATA_READ_RETRY
;
2455 return ret
> 0 ? SSL_READ_EARLY_DATA_SUCCESS
2456 : SSL_READ_EARLY_DATA_ERROR
;
2459 sc
->early_data_state
= SSL_EARLY_DATA_FINISHED_READING
;
2462 return SSL_READ_EARLY_DATA_FINISH
;
2465 ERR_raise(ERR_LIB_SSL
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
2466 return SSL_READ_EARLY_DATA_ERROR
;
2470 int SSL_get_early_data_status(const SSL
*s
)
2472 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL_ONLY(s
);
2474 /* TODO(QUIC 0RTT): 0-RTT support */
2478 return sc
->ext
.early_data
;
2481 static int ssl_peek_internal(SSL
*s
, void *buf
, size_t num
, size_t *readbytes
)
2483 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2485 #ifndef OPENSSL_NO_QUIC
2487 return s
->method
->ssl_peek(s
, buf
, num
, readbytes
);
2493 if (sc
->handshake_func
== NULL
) {
2494 ERR_raise(ERR_LIB_SSL
, SSL_R_UNINITIALIZED
);
2498 if (sc
->shutdown
& SSL_RECEIVED_SHUTDOWN
) {
2501 if ((sc
->mode
& SSL_MODE_ASYNC
) && ASYNC_get_current_job() == NULL
) {
2502 struct ssl_async_args args
;
2508 args
.type
= READFUNC
;
2509 args
.f
.func_read
= s
->method
->ssl_peek
;
2511 ret
= ssl_start_async_job(s
, &args
, ssl_io_intern
);
2512 *readbytes
= sc
->asyncrw
;
2515 return s
->method
->ssl_peek(s
, buf
, num
, readbytes
);
2519 int SSL_peek(SSL
*s
, void *buf
, int num
)
2525 ERR_raise(ERR_LIB_SSL
, SSL_R_BAD_LENGTH
);
2529 ret
= ssl_peek_internal(s
, buf
, (size_t)num
, &readbytes
);
2532 * The cast is safe here because ret should be <= INT_MAX because num is
2536 ret
= (int)readbytes
;
2542 int SSL_peek_ex(SSL
*s
, void *buf
, size_t num
, size_t *readbytes
)
2544 int ret
= ssl_peek_internal(s
, buf
, num
, readbytes
);
2551 int ssl_write_internal(SSL
*s
, const void *buf
, size_t num
,
2552 uint64_t flags
, size_t *written
)
2554 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2556 #ifndef OPENSSL_NO_QUIC
2558 return ossl_quic_write_flags(s
, buf
, num
, flags
, written
);
2564 if (sc
->handshake_func
== NULL
) {
2565 ERR_raise(ERR_LIB_SSL
, SSL_R_UNINITIALIZED
);
2569 if (sc
->shutdown
& SSL_SENT_SHUTDOWN
) {
2570 sc
->rwstate
= SSL_NOTHING
;
2571 ERR_raise(ERR_LIB_SSL
, SSL_R_PROTOCOL_IS_SHUTDOWN
);
2576 ERR_raise(ERR_LIB_SSL
, SSL_R_UNSUPPORTED_WRITE_FLAG
);
2580 if (sc
->early_data_state
== SSL_EARLY_DATA_CONNECT_RETRY
2581 || sc
->early_data_state
== SSL_EARLY_DATA_ACCEPT_RETRY
2582 || sc
->early_data_state
== SSL_EARLY_DATA_READ_RETRY
) {
2583 ERR_raise(ERR_LIB_SSL
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
2586 /* If we are a client and haven't sent the Finished we better do that */
2587 if (!ossl_statem_check_finish_init(sc
, 1))
2590 if ((sc
->mode
& SSL_MODE_ASYNC
) && ASYNC_get_current_job() == NULL
) {
2592 struct ssl_async_args args
;
2595 args
.buf
= (void *)buf
;
2597 args
.type
= WRITEFUNC
;
2598 args
.f
.func_write
= s
->method
->ssl_write
;
2600 ret
= ssl_start_async_job(s
, &args
, ssl_io_intern
);
2601 *written
= sc
->asyncrw
;
2604 return s
->method
->ssl_write(s
, buf
, num
, written
);
2608 ossl_ssize_t
SSL_sendfile(SSL
*s
, int fd
, off_t offset
, size_t size
, int flags
)
2611 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(s
);
2616 if (sc
->handshake_func
== NULL
) {
2617 ERR_raise(ERR_LIB_SSL
, SSL_R_UNINITIALIZED
);
2621 if (sc
->shutdown
& SSL_SENT_SHUTDOWN
) {
2622 sc
->rwstate
= SSL_NOTHING
;
2623 ERR_raise(ERR_LIB_SSL
, SSL_R_PROTOCOL_IS_SHUTDOWN
);
2627 if (!BIO_get_ktls_send(sc
->wbio
)) {
2628 ERR_raise(ERR_LIB_SSL
, SSL_R_UNINITIALIZED
);
2632 /* If we have an alert to send, lets send it */
2633 if (sc
->s3
.alert_dispatch
> 0) {
2634 ret
= (ossl_ssize_t
)s
->method
->ssl_dispatch_alert(s
);
2636 /* SSLfatal() already called if appropriate */
2639 /* if it went, fall through and send more stuff */
2642 sc
->rwstate
= SSL_WRITING
;
2643 if (BIO_flush(sc
->wbio
) <= 0) {
2644 if (!BIO_should_retry(sc
->wbio
)) {
2645 sc
->rwstate
= SSL_NOTHING
;
2648 set_sys_error(EAGAIN
);
2654 #ifdef OPENSSL_NO_KTLS
2655 ERR_raise_data(ERR_LIB_SSL
, ERR_R_INTERNAL_ERROR
,
2656 "can't call ktls_sendfile(), ktls disabled");
2659 ret
= ktls_sendfile(SSL_get_wfd(s
), fd
, offset
, size
, flags
);
2661 #if defined(EAGAIN) && defined(EINTR) && defined(EBUSY)
2662 if ((get_last_sys_error() == EAGAIN
) ||
2663 (get_last_sys_error() == EINTR
) ||
2664 (get_last_sys_error() == EBUSY
))
2665 BIO_set_retry_write(sc
->wbio
);
2668 ERR_raise_data(ERR_LIB_SYS
, get_last_sys_error(),
2669 "ktls_sendfile failure");
2672 sc
->rwstate
= SSL_NOTHING
;
2677 int SSL_write(SSL
*s
, const void *buf
, int num
)
2683 ERR_raise(ERR_LIB_SSL
, SSL_R_BAD_LENGTH
);
2687 ret
= ssl_write_internal(s
, buf
, (size_t)num
, 0, &written
);
2690 * The cast is safe here because ret should be <= INT_MAX because num is
2699 int SSL_write_ex(SSL
*s
, const void *buf
, size_t num
, size_t *written
)
2701 return SSL_write_ex2(s
, buf
, num
, 0, written
);
2704 int SSL_write_ex2(SSL
*s
, const void *buf
, size_t num
, uint64_t flags
,
2707 int ret
= ssl_write_internal(s
, buf
, num
, flags
, written
);
2714 int SSL_write_early_data(SSL
*s
, const void *buf
, size_t num
, size_t *written
)
2716 int ret
, early_data_state
;
2718 uint32_t partialwrite
;
2719 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(s
);
2721 /* TODO(QUIC 0RTT): This will need special handling for QUIC */
2725 switch (sc
->early_data_state
) {
2726 case SSL_EARLY_DATA_NONE
:
2728 || !SSL_in_before(s
)
2729 || ((sc
->session
== NULL
|| sc
->session
->ext
.max_early_data
== 0)
2730 && (sc
->psk_use_session_cb
== NULL
))) {
2731 ERR_raise(ERR_LIB_SSL
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
2736 case SSL_EARLY_DATA_CONNECT_RETRY
:
2737 sc
->early_data_state
= SSL_EARLY_DATA_CONNECTING
;
2738 ret
= SSL_connect(s
);
2741 sc
->early_data_state
= SSL_EARLY_DATA_CONNECT_RETRY
;
2746 case SSL_EARLY_DATA_WRITE_RETRY
:
2747 sc
->early_data_state
= SSL_EARLY_DATA_WRITING
;
2749 * We disable partial write for early data because we don't keep track
2750 * of how many bytes we've written between the SSL_write_ex() call and
2751 * the flush if the flush needs to be retried)
2753 partialwrite
= sc
->mode
& SSL_MODE_ENABLE_PARTIAL_WRITE
;
2754 sc
->mode
&= ~SSL_MODE_ENABLE_PARTIAL_WRITE
;
2755 ret
= SSL_write_ex(s
, buf
, num
, &writtmp
);
2756 sc
->mode
|= partialwrite
;
2758 sc
->early_data_state
= SSL_EARLY_DATA_WRITE_RETRY
;
2761 sc
->early_data_state
= SSL_EARLY_DATA_WRITE_FLUSH
;
2764 case SSL_EARLY_DATA_WRITE_FLUSH
:
2765 /* The buffering BIO is still in place so we need to flush it */
2766 if (statem_flush(sc
) != 1)
2769 sc
->early_data_state
= SSL_EARLY_DATA_WRITE_RETRY
;
2772 case SSL_EARLY_DATA_FINISHED_READING
:
2773 case SSL_EARLY_DATA_READ_RETRY
:
2774 early_data_state
= sc
->early_data_state
;
2775 /* We are a server writing to an unauthenticated client */
2776 sc
->early_data_state
= SSL_EARLY_DATA_UNAUTH_WRITING
;
2777 ret
= SSL_write_ex(s
, buf
, num
, written
);
2778 /* The buffering BIO is still in place */
2780 (void)BIO_flush(sc
->wbio
);
2781 sc
->early_data_state
= early_data_state
;
2785 ERR_raise(ERR_LIB_SSL
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
2790 int SSL_shutdown(SSL
*s
)
2793 * Note that this function behaves differently from what one might
2794 * expect. Return values are 0 for no success (yet), 1 for success; but
2795 * calling it once is usually not enough, even if blocking I/O is used
2796 * (see ssl3_shutdown).
2798 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2800 #ifndef OPENSSL_NO_QUIC
2802 return ossl_quic_conn_shutdown(s
, 0, NULL
, 0);
2808 if (sc
->handshake_func
== NULL
) {
2809 ERR_raise(ERR_LIB_SSL
, SSL_R_UNINITIALIZED
);
2813 if (!SSL_in_init(s
)) {
2814 if ((sc
->mode
& SSL_MODE_ASYNC
) && ASYNC_get_current_job() == NULL
) {
2815 struct ssl_async_args args
;
2817 memset(&args
, 0, sizeof(args
));
2819 args
.type
= OTHERFUNC
;
2820 args
.f
.func_other
= s
->method
->ssl_shutdown
;
2822 return ssl_start_async_job(s
, &args
, ssl_io_intern
);
2824 return s
->method
->ssl_shutdown(s
);
2827 ERR_raise(ERR_LIB_SSL
, SSL_R_SHUTDOWN_WHILE_IN_INIT
);
2832 int SSL_key_update(SSL
*s
, int updatetype
)
2834 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2836 #ifndef OPENSSL_NO_QUIC
2838 return ossl_quic_key_update(s
, updatetype
);
2844 if (!SSL_CONNECTION_IS_TLS13(sc
)) {
2845 ERR_raise(ERR_LIB_SSL
, SSL_R_WRONG_SSL_VERSION
);
2849 if (updatetype
!= SSL_KEY_UPDATE_NOT_REQUESTED
2850 && updatetype
!= SSL_KEY_UPDATE_REQUESTED
) {
2851 ERR_raise(ERR_LIB_SSL
, SSL_R_INVALID_KEY_UPDATE_TYPE
);
2855 if (!SSL_is_init_finished(s
)) {
2856 ERR_raise(ERR_LIB_SSL
, SSL_R_STILL_IN_INIT
);
2860 if (RECORD_LAYER_write_pending(&sc
->rlayer
)) {
2861 ERR_raise(ERR_LIB_SSL
, SSL_R_BAD_WRITE_RETRY
);
2865 ossl_statem_set_in_init(sc
, 1);
2866 sc
->key_update
= updatetype
;
2870 int SSL_get_key_update_type(const SSL
*s
)
2872 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
2874 #ifndef OPENSSL_NO_QUIC
2876 return ossl_quic_get_key_update_type(s
);
2882 return sc
->key_update
;
2886 * Can we accept a renegotiation request? If yes, set the flag and
2887 * return 1 if yes. If not, raise error and return 0.
2889 static int can_renegotiate(const SSL_CONNECTION
*sc
)
2891 if (SSL_CONNECTION_IS_TLS13(sc
)) {
2892 ERR_raise(ERR_LIB_SSL
, SSL_R_WRONG_SSL_VERSION
);
2896 if ((sc
->options
& SSL_OP_NO_RENEGOTIATION
) != 0) {
2897 ERR_raise(ERR_LIB_SSL
, SSL_R_NO_RENEGOTIATION
);
2904 int SSL_renegotiate(SSL
*s
)
2906 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(s
);
2911 if (!can_renegotiate(sc
))
2914 sc
->renegotiate
= 1;
2915 sc
->new_session
= 1;
2916 return s
->method
->ssl_renegotiate(s
);
2919 int SSL_renegotiate_abbreviated(SSL
*s
)
2921 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(s
);
2926 if (!can_renegotiate(sc
))
2929 sc
->renegotiate
= 1;
2930 sc
->new_session
= 0;
2931 return s
->method
->ssl_renegotiate(s
);
2934 int SSL_renegotiate_pending(const SSL
*s
)
2936 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(s
);
2942 * becomes true when negotiation is requested; false again once a
2943 * handshake has finished
2945 return (sc
->renegotiate
!= 0);
2948 int SSL_new_session_ticket(SSL
*s
)
2950 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2955 /* If we are in init because we're sending tickets, okay to send more. */
2956 if ((SSL_in_init(s
) && sc
->ext
.extra_tickets_expected
== 0)
2957 || SSL_IS_FIRST_HANDSHAKE(sc
) || !sc
->server
2958 || !SSL_CONNECTION_IS_TLS13(sc
))
2960 sc
->ext
.extra_tickets_expected
++;
2961 if (!RECORD_LAYER_write_pending(&sc
->rlayer
) && !SSL_in_init(s
))
2962 ossl_statem_set_in_init(sc
, 1);
2966 long SSL_ctrl(SSL
*s
, int cmd
, long larg
, void *parg
)
2968 return ossl_ctrl_internal(s
, cmd
, larg
, parg
, /*no_quic=*/0);
2971 long ossl_ctrl_internal(SSL
*s
, int cmd
, long larg
, void *parg
, int no_quic
)
2974 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2977 * Routing of ctrl calls for QUIC is a little counterintuitive:
2979 * - Firstly (no_quic=0), we pass the ctrl directly to our QUIC
2980 * implementation in case it wants to handle the ctrl specially.
2982 * - If our QUIC implementation does not care about the ctrl, it
2983 * will reenter this function with no_quic=1 and we will try to handle
2984 * it directly using the QCSO SSL object stub (not the handshake layer
2985 * SSL object). This is important for e.g. the version configuration
2986 * ctrls below, which must use s->defltmeth (and not sc->defltmeth).
2988 * - If we don't handle a ctrl here specially, then processing is
2989 * redirected to the handshake layer SSL object.
2991 if (!no_quic
&& IS_QUIC(s
))
2992 return s
->method
->ssl_ctrl(s
, cmd
, larg
, parg
);
2998 case SSL_CTRL_GET_READ_AHEAD
:
2999 return RECORD_LAYER_get_read_ahead(&sc
->rlayer
);
3000 case SSL_CTRL_SET_READ_AHEAD
:
3001 l
= RECORD_LAYER_get_read_ahead(&sc
->rlayer
);
3002 RECORD_LAYER_set_read_ahead(&sc
->rlayer
, larg
);
3007 OSSL_PARAM options
[2], *opts
= options
;
3011 *opts
++ = OSSL_PARAM_construct_uint32(OSSL_LIBSSL_RECORD_LAYER_PARAM_MODE
,
3013 *opts
= OSSL_PARAM_construct_end();
3015 /* Ignore return value */
3016 sc
->rlayer
.rrlmethod
->set_options(sc
->rlayer
.rrl
, options
);
3020 case SSL_CTRL_CLEAR_MODE
:
3021 return (sc
->mode
&= ~larg
);
3022 case SSL_CTRL_GET_MAX_CERT_LIST
:
3023 return (long)sc
->max_cert_list
;
3024 case SSL_CTRL_SET_MAX_CERT_LIST
:
3027 l
= (long)sc
->max_cert_list
;
3028 sc
->max_cert_list
= (size_t)larg
;
3030 case SSL_CTRL_SET_MAX_SEND_FRAGMENT
:
3031 if (larg
< 512 || larg
> SSL3_RT_MAX_PLAIN_LENGTH
)
3033 #ifndef OPENSSL_NO_KTLS
3034 if (sc
->wbio
!= NULL
&& BIO_get_ktls_send(sc
->wbio
))
3036 #endif /* OPENSSL_NO_KTLS */
3037 sc
->max_send_fragment
= larg
;
3038 if (sc
->max_send_fragment
< sc
->split_send_fragment
)
3039 sc
->split_send_fragment
= sc
->max_send_fragment
;
3040 sc
->rlayer
.wrlmethod
->set_max_frag_len(sc
->rlayer
.wrl
, larg
);
3042 case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT
:
3043 if ((size_t)larg
> sc
->max_send_fragment
|| larg
== 0)
3045 sc
->split_send_fragment
= larg
;
3047 case SSL_CTRL_SET_MAX_PIPELINES
:
3048 if (larg
< 1 || larg
> SSL_MAX_PIPELINES
)
3050 sc
->max_pipelines
= larg
;
3051 if (sc
->rlayer
.rrlmethod
->set_max_pipelines
!= NULL
)
3052 sc
->rlayer
.rrlmethod
->set_max_pipelines(sc
->rlayer
.rrl
, (size_t)larg
);
3054 case SSL_CTRL_GET_RI_SUPPORT
:
3055 return sc
->s3
.send_connection_binding
;
3056 case SSL_CTRL_SET_RETRY_VERIFY
:
3057 sc
->rwstate
= SSL_RETRY_VERIFY
;
3059 case SSL_CTRL_CERT_FLAGS
:
3060 return (sc
->cert
->cert_flags
|= larg
);
3061 case SSL_CTRL_CLEAR_CERT_FLAGS
:
3062 return (sc
->cert
->cert_flags
&= ~larg
);
3064 case SSL_CTRL_GET_RAW_CIPHERLIST
:
3066 if (sc
->s3
.tmp
.ciphers_raw
== NULL
)
3068 *(unsigned char **)parg
= sc
->s3
.tmp
.ciphers_raw
;
3069 return (int)sc
->s3
.tmp
.ciphers_rawlen
;
3071 return TLS_CIPHER_LEN
;
3073 case SSL_CTRL_GET_EXTMS_SUPPORT
:
3074 if (!sc
->session
|| SSL_in_init(s
) || ossl_statem_get_in_handshake(sc
))
3076 if (sc
->session
->flags
& SSL_SESS_FLAG_EXTMS
)
3080 case SSL_CTRL_SET_MIN_PROTO_VERSION
:
3081 return ssl_check_allowed_versions(larg
, sc
->max_proto_version
)
3082 && ssl_set_version_bound(s
->defltmeth
->version
, (int)larg
,
3083 &sc
->min_proto_version
);
3084 case SSL_CTRL_GET_MIN_PROTO_VERSION
:
3085 return sc
->min_proto_version
;
3086 case SSL_CTRL_SET_MAX_PROTO_VERSION
:
3087 return ssl_check_allowed_versions(sc
->min_proto_version
, larg
)
3088 && ssl_set_version_bound(s
->defltmeth
->version
, (int)larg
,
3089 &sc
->max_proto_version
);
3090 case SSL_CTRL_GET_MAX_PROTO_VERSION
:
3091 return sc
->max_proto_version
;
3094 return SSL_ctrl((SSL
*)sc
, cmd
, larg
, parg
);
3096 return s
->method
->ssl_ctrl(s
, cmd
, larg
, parg
);
3100 long SSL_callback_ctrl(SSL
*s
, int cmd
, void (*fp
) (void))
3102 return s
->method
->ssl_callback_ctrl(s
, cmd
, fp
);
3105 LHASH_OF(SSL_SESSION
) *SSL_CTX_sessions(SSL_CTX
*ctx
)
3107 return ctx
->sessions
;
3110 static int ssl_tsan_load(SSL_CTX
*ctx
, TSAN_QUALIFIER
int *stat
)
3114 if (ssl_tsan_lock(ctx
)) {
3115 res
= tsan_load(stat
);
3116 ssl_tsan_unlock(ctx
);
3121 long SSL_CTX_ctrl(SSL_CTX
*ctx
, int cmd
, long larg
, void *parg
)
3125 /* For some cases with ctx == NULL or larg == 1 perform syntax checks */
3126 if (cmd
== SSL_CTRL_SET_GROUPS_LIST
&& larg
== 1)
3127 return tls1_set_groups_list(ctx
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, parg
);
3130 case SSL_CTRL_SET_SIGALGS_LIST
:
3131 case SSL_CTRL_SET_CLIENT_SIGALGS_LIST
:
3132 return tls1_set_sigalgs_list(ctx
, NULL
, parg
, 0);
3139 case SSL_CTRL_GET_READ_AHEAD
:
3140 return ctx
->read_ahead
;
3141 case SSL_CTRL_SET_READ_AHEAD
:
3142 l
= ctx
->read_ahead
;
3143 ctx
->read_ahead
= larg
;
3146 case SSL_CTRL_SET_MSG_CALLBACK_ARG
:
3147 ctx
->msg_callback_arg
= parg
;
3150 case SSL_CTRL_GET_MAX_CERT_LIST
:
3151 return (long)ctx
->max_cert_list
;
3152 case SSL_CTRL_SET_MAX_CERT_LIST
:
3155 l
= (long)ctx
->max_cert_list
;
3156 ctx
->max_cert_list
= (size_t)larg
;
3159 case SSL_CTRL_SET_SESS_CACHE_SIZE
:
3162 l
= (long)ctx
->session_cache_size
;
3163 ctx
->session_cache_size
= (size_t)larg
;
3165 case SSL_CTRL_GET_SESS_CACHE_SIZE
:
3166 return (long)ctx
->session_cache_size
;
3167 case SSL_CTRL_SET_SESS_CACHE_MODE
:
3168 l
= ctx
->session_cache_mode
;
3169 ctx
->session_cache_mode
= larg
;
3171 case SSL_CTRL_GET_SESS_CACHE_MODE
:
3172 return ctx
->session_cache_mode
;
3174 case SSL_CTRL_SESS_NUMBER
:
3175 return lh_SSL_SESSION_num_items(ctx
->sessions
);
3176 case SSL_CTRL_SESS_CONNECT
:
3177 return ssl_tsan_load(ctx
, &ctx
->stats
.sess_connect
);
3178 case SSL_CTRL_SESS_CONNECT_GOOD
:
3179 return ssl_tsan_load(ctx
, &ctx
->stats
.sess_connect_good
);
3180 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE
:
3181 return ssl_tsan_load(ctx
, &ctx
->stats
.sess_connect_renegotiate
);
3182 case SSL_CTRL_SESS_ACCEPT
:
3183 return ssl_tsan_load(ctx
, &ctx
->stats
.sess_accept
);
3184 case SSL_CTRL_SESS_ACCEPT_GOOD
:
3185 return ssl_tsan_load(ctx
, &ctx
->stats
.sess_accept_good
);
3186 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE
:
3187 return ssl_tsan_load(ctx
, &ctx
->stats
.sess_accept_renegotiate
);
3188 case SSL_CTRL_SESS_HIT
:
3189 return ssl_tsan_load(ctx
, &ctx
->stats
.sess_hit
);
3190 case SSL_CTRL_SESS_CB_HIT
:
3191 return ssl_tsan_load(ctx
, &ctx
->stats
.sess_cb_hit
);
3192 case SSL_CTRL_SESS_MISSES
:
3193 return ssl_tsan_load(ctx
, &ctx
->stats
.sess_miss
);
3194 case SSL_CTRL_SESS_TIMEOUTS
:
3195 return ssl_tsan_load(ctx
, &ctx
->stats
.sess_timeout
);
3196 case SSL_CTRL_SESS_CACHE_FULL
:
3197 return ssl_tsan_load(ctx
, &ctx
->stats
.sess_cache_full
);
3199 return (ctx
->mode
|= larg
);
3200 case SSL_CTRL_CLEAR_MODE
:
3201 return (ctx
->mode
&= ~larg
);
3202 case SSL_CTRL_SET_MAX_SEND_FRAGMENT
:
3203 if (larg
< 512 || larg
> SSL3_RT_MAX_PLAIN_LENGTH
)
3205 ctx
->max_send_fragment
= larg
;
3206 if (ctx
->max_send_fragment
< ctx
->split_send_fragment
)
3207 ctx
->split_send_fragment
= ctx
->max_send_fragment
;
3209 case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT
:
3210 if ((size_t)larg
> ctx
->max_send_fragment
|| larg
== 0)
3212 ctx
->split_send_fragment
= larg
;
3214 case SSL_CTRL_SET_MAX_PIPELINES
:
3215 if (larg
< 1 || larg
> SSL_MAX_PIPELINES
)
3217 ctx
->max_pipelines
= larg
;
3219 case SSL_CTRL_CERT_FLAGS
:
3220 return (ctx
->cert
->cert_flags
|= larg
);
3221 case SSL_CTRL_CLEAR_CERT_FLAGS
:
3222 return (ctx
->cert
->cert_flags
&= ~larg
);
3223 case SSL_CTRL_SET_MIN_PROTO_VERSION
:
3224 return ssl_check_allowed_versions(larg
, ctx
->max_proto_version
)
3225 && ssl_set_version_bound(ctx
->method
->version
, (int)larg
,
3226 &ctx
->min_proto_version
);
3227 case SSL_CTRL_GET_MIN_PROTO_VERSION
:
3228 return ctx
->min_proto_version
;
3229 case SSL_CTRL_SET_MAX_PROTO_VERSION
:
3230 return ssl_check_allowed_versions(ctx
->min_proto_version
, larg
)
3231 && ssl_set_version_bound(ctx
->method
->version
, (int)larg
,
3232 &ctx
->max_proto_version
);
3233 case SSL_CTRL_GET_MAX_PROTO_VERSION
:
3234 return ctx
->max_proto_version
;
3236 return ctx
->method
->ssl_ctx_ctrl(ctx
, cmd
, larg
, parg
);
3240 long SSL_CTX_callback_ctrl(SSL_CTX
*ctx
, int cmd
, void (*fp
) (void))
3243 case SSL_CTRL_SET_MSG_CALLBACK
:
3244 ctx
->msg_callback
= (void (*)
3245 (int write_p
, int version
, int content_type
,
3246 const void *buf
, size_t len
, SSL
*ssl
,
3251 return ctx
->method
->ssl_ctx_callback_ctrl(ctx
, cmd
, fp
);
3255 int ssl_cipher_id_cmp(const SSL_CIPHER
*a
, const SSL_CIPHER
*b
)
3264 int ssl_cipher_ptr_id_cmp(const SSL_CIPHER
*const *ap
,
3265 const SSL_CIPHER
*const *bp
)
3267 if ((*ap
)->id
> (*bp
)->id
)
3269 if ((*ap
)->id
< (*bp
)->id
)
3275 * return a STACK of the ciphers available for the SSL and in order of
3278 STACK_OF(SSL_CIPHER
) *SSL_get_ciphers(const SSL
*s
)
3280 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
3283 if (sc
->cipher_list
!= NULL
) {
3284 return sc
->cipher_list
;
3285 } else if ((s
->ctx
!= NULL
) && (s
->ctx
->cipher_list
!= NULL
)) {
3286 return s
->ctx
->cipher_list
;
3292 STACK_OF(SSL_CIPHER
) *SSL_get_client_ciphers(const SSL
*s
)
3294 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
3296 if (sc
== NULL
|| !sc
->server
)
3298 return sc
->peer_ciphers
;
3301 STACK_OF(SSL_CIPHER
) *SSL_get1_supported_ciphers(SSL
*s
)
3303 STACK_OF(SSL_CIPHER
) *sk
= NULL
, *ciphers
;
3305 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
3310 ciphers
= SSL_get_ciphers(s
);
3313 if (!ssl_set_client_disabled(sc
))
3315 for (i
= 0; i
< sk_SSL_CIPHER_num(ciphers
); i
++) {
3316 const SSL_CIPHER
*c
= sk_SSL_CIPHER_value(ciphers
, i
);
3317 if (!ssl_cipher_disabled(sc
, c
, SSL_SECOP_CIPHER_SUPPORTED
, 0)) {
3319 sk
= sk_SSL_CIPHER_new_null();
3322 if (!sk_SSL_CIPHER_push(sk
, c
)) {
3323 sk_SSL_CIPHER_free(sk
);
3331 /** return a STACK of the ciphers available for the SSL and in order of
3333 STACK_OF(SSL_CIPHER
) *ssl_get_ciphers_by_id(SSL_CONNECTION
*s
)
3336 if (s
->cipher_list_by_id
!= NULL
)
3337 return s
->cipher_list_by_id
;
3338 else if (s
->ssl
.ctx
!= NULL
3339 && s
->ssl
.ctx
->cipher_list_by_id
!= NULL
)
3340 return s
->ssl
.ctx
->cipher_list_by_id
;
3345 /** The old interface to get the same thing as SSL_get_ciphers() */
3346 const char *SSL_get_cipher_list(const SSL
*s
, int n
)
3348 const SSL_CIPHER
*c
;
3349 STACK_OF(SSL_CIPHER
) *sk
;
3353 sk
= SSL_get_ciphers(s
);
3354 if ((sk
== NULL
) || (sk_SSL_CIPHER_num(sk
) <= n
))
3356 c
= sk_SSL_CIPHER_value(sk
, n
);
3362 /** return a STACK of the ciphers available for the SSL_CTX and in order of
3364 STACK_OF(SSL_CIPHER
) *SSL_CTX_get_ciphers(const SSL_CTX
*ctx
)
3367 return ctx
->cipher_list
;
3372 * Distinguish between ciphers controlled by set_ciphersuite() and
3373 * set_cipher_list() when counting.
3375 static int cipher_list_tls12_num(STACK_OF(SSL_CIPHER
) *sk
)
3378 const SSL_CIPHER
*c
;
3382 for (i
= 0; i
< sk_SSL_CIPHER_num(sk
); ++i
) {
3383 c
= sk_SSL_CIPHER_value(sk
, i
);
3384 if (c
->min_tls
>= TLS1_3_VERSION
)
3391 /** specify the ciphers to be used by default by the SSL_CTX */
3392 int SSL_CTX_set_cipher_list(SSL_CTX
*ctx
, const char *str
)
3394 STACK_OF(SSL_CIPHER
) *sk
;
3396 sk
= ssl_create_cipher_list(ctx
, ctx
->tls13_ciphersuites
,
3397 &ctx
->cipher_list
, &ctx
->cipher_list_by_id
, str
,
3400 * ssl_create_cipher_list may return an empty stack if it was unable to
3401 * find a cipher matching the given rule string (for example if the rule
3402 * string specifies a cipher which has been disabled). This is not an
3403 * error as far as ssl_create_cipher_list is concerned, and hence
3404 * ctx->cipher_list and ctx->cipher_list_by_id has been updated.
3408 if (ctx
->method
->num_ciphers() > 0 && cipher_list_tls12_num(sk
) == 0) {
3409 ERR_raise(ERR_LIB_SSL
, SSL_R_NO_CIPHER_MATCH
);
3415 /** specify the ciphers to be used by the SSL */
3416 int SSL_set_cipher_list(SSL
*s
, const char *str
)
3418 STACK_OF(SSL_CIPHER
) *sk
;
3419 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
3426 sk
= ssl_create_cipher_list(ctx
, sc
->tls13_ciphersuites
,
3427 &sc
->cipher_list
, &sc
->cipher_list_by_id
, str
,
3429 /* see comment in SSL_CTX_set_cipher_list */
3432 if (ctx
->method
->num_ciphers() > 0 && cipher_list_tls12_num(sk
) == 0) {
3433 ERR_raise(ERR_LIB_SSL
, SSL_R_NO_CIPHER_MATCH
);
3439 char *SSL_get_shared_ciphers(const SSL
*s
, char *buf
, int size
)
3442 STACK_OF(SSL_CIPHER
) *clntsk
, *srvrsk
;
3443 const SSL_CIPHER
*c
;
3445 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
3451 || sc
->peer_ciphers
== NULL
3456 clntsk
= sc
->peer_ciphers
;
3457 srvrsk
= SSL_get_ciphers(s
);
3458 if (clntsk
== NULL
|| srvrsk
== NULL
)
3461 if (sk_SSL_CIPHER_num(clntsk
) == 0 || sk_SSL_CIPHER_num(srvrsk
) == 0)
3464 for (i
= 0; i
< sk_SSL_CIPHER_num(clntsk
); i
++) {
3467 c
= sk_SSL_CIPHER_value(clntsk
, i
);
3468 if (sk_SSL_CIPHER_find(srvrsk
, c
) < 0)
3471 n
= (int)OPENSSL_strnlen(c
->name
, size
);
3478 memcpy(p
, c
->name
, n
);
3488 * Return the requested servername (SNI) value. Note that the behaviour varies
3490 * - whether this is called by the client or the server,
3491 * - if we are before or during/after the handshake,
3492 * - if a resumption or normal handshake is being attempted/has occurred
3493 * - whether we have negotiated TLSv1.2 (or below) or TLSv1.3
3495 * Note that only the host_name type is defined (RFC 3546).
3497 const char *SSL_get_servername(const SSL
*s
, const int type
)
3499 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
3506 * If we don't know if we are the client or the server yet then we assume
3509 server
= sc
->handshake_func
== NULL
? 0 : sc
->server
;
3511 if (type
!= TLSEXT_NAMETYPE_host_name
)
3517 * In TLSv1.3 on the server SNI is not associated with the session
3518 * but in TLSv1.2 or below it is.
3520 * Before the handshake:
3523 * During/after the handshake (TLSv1.2 or below resumption occurred):
3524 * - If a servername was accepted by the server in the original
3525 * handshake then it will return that servername, or NULL otherwise.
3527 * During/after the handshake (TLSv1.2 or below resumption did not occur):
3528 * - The function will return the servername requested by the client in
3529 * this handshake or NULL if none was requested.
3531 if (sc
->hit
&& !SSL_CONNECTION_IS_TLS13(sc
))
3532 return sc
->session
->ext
.hostname
;
3537 * Before the handshake:
3538 * - If a servername has been set via a call to
3539 * SSL_set_tlsext_host_name() then it will return that servername
3540 * - If one has not been set, but a TLSv1.2 resumption is being
3541 * attempted and the session from the original handshake had a
3542 * servername accepted by the server then it will return that
3544 * - Otherwise it returns NULL
3546 * During/after the handshake (TLSv1.2 or below resumption occurred):
3547 * - If the session from the original handshake had a servername accepted
3548 * by the server then it will return that servername.
3549 * - Otherwise it returns the servername set via
3550 * SSL_set_tlsext_host_name() (or NULL if it was not called).
3552 * During/after the handshake (TLSv1.2 or below resumption did not occur):
3553 * - It will return the servername set via SSL_set_tlsext_host_name()
3554 * (or NULL if it was not called).
3556 if (SSL_in_before(s
)) {
3557 if (sc
->ext
.hostname
== NULL
3558 && sc
->session
!= NULL
3559 && sc
->session
->ssl_version
!= TLS1_3_VERSION
)
3560 return sc
->session
->ext
.hostname
;
3562 if (!SSL_CONNECTION_IS_TLS13(sc
) && sc
->hit
3563 && sc
->session
->ext
.hostname
!= NULL
)
3564 return sc
->session
->ext
.hostname
;
3568 return sc
->ext
.hostname
;
3571 int SSL_get_servername_type(const SSL
*s
)
3573 if (SSL_get_servername(s
, TLSEXT_NAMETYPE_host_name
) != NULL
)
3574 return TLSEXT_NAMETYPE_host_name
;
3579 * SSL_select_next_proto implements the standard protocol selection. It is
3580 * expected that this function is called from the callback set by
3581 * SSL_CTX_set_next_proto_select_cb. The protocol data is assumed to be a
3582 * vector of 8-bit, length prefixed byte strings. The length byte itself is
3583 * not included in the length. A byte string of length 0 is invalid. No byte
3584 * string may be truncated. The current, but experimental algorithm for
3585 * selecting the protocol is: 1) If the server doesn't support NPN then this
3586 * is indicated to the callback. In this case, the client application has to
3587 * abort the connection or have a default application level protocol. 2) If
3588 * the server supports NPN, but advertises an empty list then the client
3589 * selects the first protocol in its list, but indicates via the API that this
3590 * fallback case was enacted. 3) Otherwise, the client finds the first
3591 * protocol in the server's list that it supports and selects this protocol.
3592 * This is because it's assumed that the server has better information about
3593 * which protocol a client should use. 4) If the client doesn't support any
3594 * of the server's advertised protocols, then this is treated the same as
3595 * case 2. It returns either OPENSSL_NPN_NEGOTIATED if a common protocol was
3596 * found, or OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
3598 int SSL_select_next_proto(unsigned char **out
, unsigned char *outlen
,
3599 const unsigned char *server
,
3600 unsigned int server_len
,
3601 const unsigned char *client
, unsigned int client_len
)
3603 PACKET cpkt
, csubpkt
, spkt
, ssubpkt
;
3605 if (!PACKET_buf_init(&cpkt
, client
, client_len
)
3606 || !PACKET_get_length_prefixed_1(&cpkt
, &csubpkt
)
3607 || PACKET_remaining(&csubpkt
) == 0) {
3610 return OPENSSL_NPN_NO_OVERLAP
;
3614 * Set the default opportunistic protocol. Will be overwritten if we find
3617 *out
= (unsigned char *)PACKET_data(&csubpkt
);
3618 *outlen
= (unsigned char)PACKET_remaining(&csubpkt
);
3621 * For each protocol in server preference order, see if we support it.
3623 if (PACKET_buf_init(&spkt
, server
, server_len
)) {
3624 while (PACKET_get_length_prefixed_1(&spkt
, &ssubpkt
)) {
3625 if (PACKET_remaining(&ssubpkt
) == 0)
3626 continue; /* Invalid - ignore it */
3627 if (PACKET_buf_init(&cpkt
, client
, client_len
)) {
3628 while (PACKET_get_length_prefixed_1(&cpkt
, &csubpkt
)) {
3629 if (PACKET_equal(&csubpkt
, PACKET_data(&ssubpkt
),
3630 PACKET_remaining(&ssubpkt
))) {
3631 /* We found a match */
3632 *out
= (unsigned char *)PACKET_data(&ssubpkt
);
3633 *outlen
= (unsigned char)PACKET_remaining(&ssubpkt
);
3634 return OPENSSL_NPN_NEGOTIATED
;
3637 /* Ignore spurious trailing bytes in the client list */
3639 /* This should never happen */
3640 return OPENSSL_NPN_NO_OVERLAP
;
3643 /* Ignore spurious trailing bytes in the server list */
3647 * There's no overlap between our protocols and the server's list. We use
3648 * the default opportunistic protocol selected earlier
3650 return OPENSSL_NPN_NO_OVERLAP
;
3653 #ifndef OPENSSL_NO_NEXTPROTONEG
3655 * SSL_get0_next_proto_negotiated sets *data and *len to point to the
3656 * client's requested protocol for this connection and returns 0. If the
3657 * client didn't request any protocol, then *data is set to NULL. Note that
3658 * the client can request any protocol it chooses. The value returned from
3659 * this function need not be a member of the list of supported protocols
3660 * provided by the callback.
3662 void SSL_get0_next_proto_negotiated(const SSL
*s
, const unsigned char **data
,
3665 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
3668 /* We have no other way to indicate error */
3674 *data
= sc
->ext
.npn
;
3675 if (*data
== NULL
) {
3678 *len
= (unsigned int)sc
->ext
.npn_len
;
3683 * SSL_CTX_set_npn_advertised_cb sets a callback that is called when
3684 * a TLS server needs a list of supported protocols for Next Protocol
3685 * Negotiation. The returned list must be in wire format. The list is
3686 * returned by setting |out| to point to it and |outlen| to its length. This
3687 * memory will not be modified, but one should assume that the SSL* keeps a
3688 * reference to it. The callback should return SSL_TLSEXT_ERR_OK if it
3689 * wishes to advertise. Otherwise, no such extension will be included in the
3692 void SSL_CTX_set_npn_advertised_cb(SSL_CTX
*ctx
,
3693 SSL_CTX_npn_advertised_cb_func cb
,
3696 if (IS_QUIC_CTX(ctx
))
3697 /* NPN not allowed for QUIC */
3700 ctx
->ext
.npn_advertised_cb
= cb
;
3701 ctx
->ext
.npn_advertised_cb_arg
= arg
;
3705 * SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
3706 * client needs to select a protocol from the server's provided list. |out|
3707 * must be set to point to the selected protocol (which may be within |in|).
3708 * The length of the protocol name must be written into |outlen|. The
3709 * server's advertised protocols are provided in |in| and |inlen|. The
3710 * callback can assume that |in| is syntactically valid. The client must
3711 * select a protocol. It is fatal to the connection if this callback returns
3712 * a value other than SSL_TLSEXT_ERR_OK.
3714 void SSL_CTX_set_npn_select_cb(SSL_CTX
*ctx
,
3715 SSL_CTX_npn_select_cb_func cb
,
3718 if (IS_QUIC_CTX(ctx
))
3719 /* NPN not allowed for QUIC */
3722 ctx
->ext
.npn_select_cb
= cb
;
3723 ctx
->ext
.npn_select_cb_arg
= arg
;
3727 static int alpn_value_ok(const unsigned char *protos
, unsigned int protos_len
)
3731 if (protos_len
< 2 || protos
== NULL
)
3734 for (idx
= 0; idx
< protos_len
; idx
+= protos
[idx
] + 1) {
3735 if (protos
[idx
] == 0)
3738 return idx
== protos_len
;
3741 * SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
3742 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
3743 * length-prefixed strings). Returns 0 on success.
3745 int SSL_CTX_set_alpn_protos(SSL_CTX
*ctx
, const unsigned char *protos
,
3746 unsigned int protos_len
)
3748 unsigned char *alpn
;
3750 if (protos_len
== 0 || protos
== NULL
) {
3751 OPENSSL_free(ctx
->ext
.alpn
);
3752 ctx
->ext
.alpn
= NULL
;
3753 ctx
->ext
.alpn_len
= 0;
3756 /* Not valid per RFC */
3757 if (!alpn_value_ok(protos
, protos_len
))
3760 alpn
= OPENSSL_memdup(protos
, protos_len
);
3763 OPENSSL_free(ctx
->ext
.alpn
);
3764 ctx
->ext
.alpn
= alpn
;
3765 ctx
->ext
.alpn_len
= protos_len
;
3771 * SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
3772 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
3773 * length-prefixed strings). Returns 0 on success.
3775 int SSL_set_alpn_protos(SSL
*ssl
, const unsigned char *protos
,
3776 unsigned int protos_len
)
3778 unsigned char *alpn
;
3779 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
3784 if (protos_len
== 0 || protos
== NULL
) {
3785 OPENSSL_free(sc
->ext
.alpn
);
3786 sc
->ext
.alpn
= NULL
;
3787 sc
->ext
.alpn_len
= 0;
3790 /* Not valid per RFC */
3791 if (!alpn_value_ok(protos
, protos_len
))
3794 alpn
= OPENSSL_memdup(protos
, protos_len
);
3797 OPENSSL_free(sc
->ext
.alpn
);
3798 sc
->ext
.alpn
= alpn
;
3799 sc
->ext
.alpn_len
= protos_len
;
3805 * SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is
3806 * called during ClientHello processing in order to select an ALPN protocol
3807 * from the client's list of offered protocols.
3809 void SSL_CTX_set_alpn_select_cb(SSL_CTX
*ctx
,
3810 SSL_CTX_alpn_select_cb_func cb
,
3813 ctx
->ext
.alpn_select_cb
= cb
;
3814 ctx
->ext
.alpn_select_cb_arg
= arg
;
3818 * SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
3819 * On return it sets |*data| to point to |*len| bytes of protocol name
3820 * (not including the leading length-prefix byte). If the server didn't
3821 * respond with a negotiated protocol then |*len| will be zero.
3823 void SSL_get0_alpn_selected(const SSL
*ssl
, const unsigned char **data
,
3826 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(ssl
);
3829 /* We have no other way to indicate error */
3835 *data
= sc
->s3
.alpn_selected
;
3839 *len
= (unsigned int)sc
->s3
.alpn_selected_len
;
3842 int SSL_export_keying_material(SSL
*s
, unsigned char *out
, size_t olen
,
3843 const char *label
, size_t llen
,
3844 const unsigned char *context
, size_t contextlen
,
3847 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
3852 if (sc
->session
== NULL
3853 || (sc
->version
< TLS1_VERSION
&& sc
->version
!= DTLS1_BAD_VER
))
3856 return sc
->ssl
.method
->ssl3_enc
->export_keying_material(sc
, out
, olen
, label
,
3862 int SSL_export_keying_material_early(SSL
*s
, unsigned char *out
, size_t olen
,
3863 const char *label
, size_t llen
,
3864 const unsigned char *context
,
3867 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
3872 if (sc
->version
!= TLS1_3_VERSION
)
3875 return tls13_export_keying_material_early(sc
, out
, olen
, label
, llen
,
3876 context
, contextlen
);
3879 static unsigned long ssl_session_hash(const SSL_SESSION
*a
)
3881 const unsigned char *session_id
= a
->session_id
;
3883 unsigned char tmp_storage
[4];
3885 if (a
->session_id_length
< sizeof(tmp_storage
)) {
3886 memset(tmp_storage
, 0, sizeof(tmp_storage
));
3887 memcpy(tmp_storage
, a
->session_id
, a
->session_id_length
);
3888 session_id
= tmp_storage
;
3892 ((unsigned long)session_id
[0]) |
3893 ((unsigned long)session_id
[1] << 8L) |
3894 ((unsigned long)session_id
[2] << 16L) |
3895 ((unsigned long)session_id
[3] << 24L);
3900 * NB: If this function (or indeed the hash function which uses a sort of
3901 * coarser function than this one) is changed, ensure
3902 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on
3903 * being able to construct an SSL_SESSION that will collide with any existing
3904 * session with a matching session ID.
3906 static int ssl_session_cmp(const SSL_SESSION
*a
, const SSL_SESSION
*b
)
3908 if (a
->ssl_version
!= b
->ssl_version
)
3910 if (a
->session_id_length
!= b
->session_id_length
)
3912 return memcmp(a
->session_id
, b
->session_id
, a
->session_id_length
);
3915 #ifndef OPENSSL_NO_SSLKEYLOG
3917 * @brief Static initialization for a one-time action to initialize the SSL key log.
3919 static CRYPTO_ONCE ssl_keylog_once
= CRYPTO_ONCE_STATIC_INIT
;
3922 * @brief Pointer to a read-write lock used to protect access to the key log.
3924 static CRYPTO_RWLOCK
*keylog_lock
= NULL
;
3927 * @brief Pointer to a BIO structure used for writing the key log information.
3929 static BIO
*keylog_bio
= NULL
;
3932 * @brief Initializes the SSLKEYLOGFILE lock.
3934 * @return 1 on success, 0 on failure.
3936 DEFINE_RUN_ONCE_STATIC(ssl_keylog_init
)
3938 keylog_lock
= CRYPTO_THREAD_lock_new();
3939 if (keylog_lock
== NULL
)
3945 * @brief checks when a BIO refcount has reached zero, and sets
3946 * keylog_cb to NULL if it has
3950 static long check_keylog_bio_free(BIO
*b
, int oper
, const char *argp
,
3951 size_t len
, int argi
, long argl
, int ret
,
3956 * Note we _dont_ take the keylog_lock here
3957 * This is intentional, because we only free the keylog lock
3958 * During SSL_CTX_free, in which we already posess the lock, so
3959 * Theres no need to grab it again here
3961 if (oper
== BIO_CB_FREE
)
3967 * @brief records ssl secrets to a file
3969 static void do_sslkeylogfile(const SSL
*ssl
, const char *line
)
3971 if (keylog_lock
== NULL
)
3974 if (!CRYPTO_THREAD_write_lock(keylog_lock
))
3976 if (keylog_bio
!= NULL
) {
3977 BIO_printf(keylog_bio
, "%s\n", line
);
3978 (void)BIO_flush(keylog_bio
);
3980 CRYPTO_THREAD_unlock(keylog_lock
);
3985 * These wrapper functions should remain rather than redeclaring
3986 * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
3987 * variable. The reason is that the functions aren't static, they're exposed
3991 #ifndef OPENSSL_NO_SSLKEYLOG
3992 static BIO
*get_sslkeylog_bio(const char *keylogfile
)
3994 # ifdef _POSIX_C_SOURCE
3999 fdno
= open(keylogfile
, O_WRONLY
| O_CREAT
| O_APPEND
, 0600);
4003 fp
= fdopen(fdno
, "a");
4009 if ((b
= BIO_new_fp(fp
, BIO_CLOSE
)) == NULL
)
4013 return BIO_new_file(keylogfile
, "a");
4018 SSL_CTX
*SSL_CTX_new_ex(OSSL_LIB_CTX
*libctx
, const char *propq
,
4019 const SSL_METHOD
*meth
)
4021 SSL_CTX
*ret
= NULL
;
4022 #ifndef OPENSSL_NO_SSLKEYLOG
4023 const char *keylogfile
= ossl_safe_getenv("SSLKEYLOGFILE");
4025 #ifndef OPENSSL_NO_COMP_ALG
4030 ERR_raise(ERR_LIB_SSL
, SSL_R_NULL_SSL_METHOD_PASSED
);
4034 if (!OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS
, NULL
))
4037 /* Doing this for the run once effect */
4038 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
4039 ERR_raise(ERR_LIB_SSL
, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS
);
4043 ret
= OPENSSL_zalloc(sizeof(*ret
));
4047 /* Init the reference counting before any call to SSL_CTX_free */
4048 if (!CRYPTO_NEW_REF(&ret
->references
, 1)) {
4053 ret
->lock
= CRYPTO_THREAD_lock_new();
4054 if (ret
->lock
== NULL
) {
4055 ERR_raise(ERR_LIB_SSL
, ERR_R_CRYPTO_LIB
);
4059 #ifdef TSAN_REQUIRES_LOCKING
4060 ret
->tsan_lock
= CRYPTO_THREAD_lock_new();
4061 if (ret
->tsan_lock
== NULL
) {
4062 ERR_raise(ERR_LIB_SSL
, ERR_R_CRYPTO_LIB
);
4067 ret
->libctx
= libctx
;
4068 if (propq
!= NULL
) {
4069 ret
->propq
= OPENSSL_strdup(propq
);
4070 if (ret
->propq
== NULL
)
4075 ret
->min_proto_version
= 0;
4076 ret
->max_proto_version
= 0;
4077 ret
->mode
= SSL_MODE_AUTO_RETRY
;
4078 ret
->session_cache_mode
= SSL_SESS_CACHE_SERVER
;
4079 ret
->session_cache_size
= SSL_SESSION_CACHE_MAX_SIZE_DEFAULT
;
4080 /* We take the system default. */
4081 ret
->session_timeout
= meth
->get_timeout();
4082 ret
->max_cert_list
= SSL_MAX_CERT_LIST_DEFAULT
;
4083 ret
->verify_mode
= SSL_VERIFY_NONE
;
4085 ret
->sessions
= lh_SSL_SESSION_new(ssl_session_hash
, ssl_session_cmp
);
4086 if (ret
->sessions
== NULL
) {
4087 ERR_raise(ERR_LIB_SSL
, ERR_R_CRYPTO_LIB
);
4090 ret
->cert_store
= X509_STORE_new();
4091 if (ret
->cert_store
== NULL
) {
4092 ERR_raise(ERR_LIB_SSL
, ERR_R_X509_LIB
);
4095 #ifndef OPENSSL_NO_CT
4096 ret
->ctlog_store
= CTLOG_STORE_new_ex(libctx
, propq
);
4097 if (ret
->ctlog_store
== NULL
) {
4098 ERR_raise(ERR_LIB_SSL
, ERR_R_CT_LIB
);
4103 /* initialize cipher/digest methods table */
4104 if (!ssl_load_ciphers(ret
)) {
4105 ERR_raise(ERR_LIB_SSL
, ERR_R_SSL_LIB
);
4109 if (!ssl_load_groups(ret
)) {
4110 ERR_raise(ERR_LIB_SSL
, ERR_R_SSL_LIB
);
4114 /* load provider sigalgs */
4115 if (!ssl_load_sigalgs(ret
)) {
4116 ERR_raise(ERR_LIB_SSL
, ERR_R_SSL_LIB
);
4120 /* initialise sig algs */
4121 if (!ssl_setup_sigalgs(ret
)) {
4122 ERR_raise(ERR_LIB_SSL
, ERR_R_SSL_LIB
);
4126 if (!SSL_CTX_set_ciphersuites(ret
, OSSL_default_ciphersuites())) {
4127 ERR_raise(ERR_LIB_SSL
, ERR_R_SSL_LIB
);
4131 if ((ret
->cert
= ssl_cert_new(SSL_PKEY_NUM
+ ret
->sigalg_list_len
)) == NULL
) {
4132 ERR_raise(ERR_LIB_SSL
, ERR_R_SSL_LIB
);
4136 if (!ssl_create_cipher_list(ret
,
4137 ret
->tls13_ciphersuites
,
4138 &ret
->cipher_list
, &ret
->cipher_list_by_id
,
4139 OSSL_default_cipher_list(), ret
->cert
)
4140 || sk_SSL_CIPHER_num(ret
->cipher_list
) <= 0) {
4141 ERR_raise(ERR_LIB_SSL
, SSL_R_LIBRARY_HAS_NO_CIPHERS
);
4145 ret
->param
= X509_VERIFY_PARAM_new();
4146 if (ret
->param
== NULL
) {
4147 ERR_raise(ERR_LIB_SSL
, ERR_R_X509_LIB
);
4152 * If these aren't available from the provider we'll get NULL returns.
4153 * That's fine but will cause errors later if SSLv3 is negotiated
4155 ret
->md5
= ssl_evp_md_fetch(libctx
, NID_md5
, propq
);
4156 ret
->sha1
= ssl_evp_md_fetch(libctx
, NID_sha1
, propq
);
4158 if ((ret
->ca_names
= sk_X509_NAME_new_null()) == NULL
) {
4159 ERR_raise(ERR_LIB_SSL
, ERR_R_CRYPTO_LIB
);
4163 if ((ret
->client_ca_names
= sk_X509_NAME_new_null()) == NULL
) {
4164 ERR_raise(ERR_LIB_SSL
, ERR_R_CRYPTO_LIB
);
4168 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX
, ret
, &ret
->ex_data
)) {
4169 ERR_raise(ERR_LIB_SSL
, ERR_R_CRYPTO_LIB
);
4173 if ((ret
->ext
.secure
= OPENSSL_secure_zalloc(sizeof(*ret
->ext
.secure
))) == NULL
)
4176 /* No compression for DTLS */
4177 if (!(meth
->ssl3_enc
->enc_flags
& SSL_ENC_FLAG_DTLS
))
4178 ret
->comp_methods
= SSL_COMP_get_compression_methods();
4180 ret
->max_send_fragment
= SSL3_RT_MAX_PLAIN_LENGTH
;
4181 ret
->split_send_fragment
= SSL3_RT_MAX_PLAIN_LENGTH
;
4183 /* Setup RFC5077 ticket keys */
4184 if ((RAND_bytes_ex(libctx
, ret
->ext
.tick_key_name
,
4185 sizeof(ret
->ext
.tick_key_name
), 0) <= 0)
4186 || (RAND_priv_bytes_ex(libctx
, ret
->ext
.secure
->tick_hmac_key
,
4187 sizeof(ret
->ext
.secure
->tick_hmac_key
), 0) <= 0)
4188 || (RAND_priv_bytes_ex(libctx
, ret
->ext
.secure
->tick_aes_key
,
4189 sizeof(ret
->ext
.secure
->tick_aes_key
), 0) <= 0))
4190 ret
->options
|= SSL_OP_NO_TICKET
;
4192 if (RAND_priv_bytes_ex(libctx
, ret
->ext
.cookie_hmac_key
,
4193 sizeof(ret
->ext
.cookie_hmac_key
), 0) <= 0) {
4194 ERR_raise(ERR_LIB_SSL
, ERR_R_RAND_LIB
);
4198 #ifndef OPENSSL_NO_SRP
4199 if (!ssl_ctx_srp_ctx_init_intern(ret
)) {
4200 ERR_raise(ERR_LIB_SSL
, ERR_R_SSL_LIB
);
4204 #ifndef OPENSSL_NO_ENGINE
4205 # ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO
4206 # define eng_strx(x) #x
4207 # define eng_str(x) eng_strx(x)
4208 /* Use specific client engine automatically... ignore errors */
4211 eng
= ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO
));
4214 ENGINE_load_builtin_engines();
4215 eng
= ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO
));
4217 if (!eng
|| !SSL_CTX_set_client_cert_engine(ret
, eng
))
4223 #ifndef OPENSSL_NO_COMP_ALG
4225 * Set the default order: brotli, zlib, zstd
4226 * Including only those enabled algorithms
4228 memset(ret
->cert_comp_prefs
, 0, sizeof(ret
->cert_comp_prefs
));
4230 if (ossl_comp_has_alg(TLSEXT_comp_cert_brotli
))
4231 ret
->cert_comp_prefs
[i
++] = TLSEXT_comp_cert_brotli
;
4232 if (ossl_comp_has_alg(TLSEXT_comp_cert_zlib
))
4233 ret
->cert_comp_prefs
[i
++] = TLSEXT_comp_cert_zlib
;
4234 if (ossl_comp_has_alg(TLSEXT_comp_cert_zstd
))
4235 ret
->cert_comp_prefs
[i
++] = TLSEXT_comp_cert_zstd
;
4238 * Disable compression by default to prevent CRIME. Applications can
4239 * re-enable compression by configuring
4240 * SSL_CTX_clear_options(ctx, SSL_OP_NO_COMPRESSION);
4241 * or by using the SSL_CONF library. Similarly we also enable TLSv1.3
4242 * middlebox compatibility by default. This may be disabled by default in
4243 * a later OpenSSL version.
4245 ret
->options
|= SSL_OP_NO_COMPRESSION
| SSL_OP_ENABLE_MIDDLEBOX_COMPAT
;
4247 ret
->ext
.status_type
= TLSEXT_STATUSTYPE_nothing
;
4250 * We cannot usefully set a default max_early_data here (which gets
4251 * propagated in SSL_new(), for the following reason: setting the
4252 * SSL field causes tls_construct_stoc_early_data() to tell the
4253 * client that early data will be accepted when constructing a TLS 1.3
4254 * session ticket, and the client will accordingly send us early data
4255 * when using that ticket (if the client has early data to send).
4256 * However, in order for the early data to actually be consumed by
4257 * the application, the application must also have calls to
4258 * SSL_read_early_data(); otherwise we'll just skip past the early data
4259 * and ignore it. So, since the application must add calls to
4260 * SSL_read_early_data(), we also require them to add
4261 * calls to SSL_CTX_set_max_early_data() in order to use early data,
4262 * eliminating the bandwidth-wasting early data in the case described
4265 ret
->max_early_data
= 0;
4268 * Default recv_max_early_data is a fully loaded single record. Could be
4269 * split across multiple records in practice. We set this differently to
4270 * max_early_data so that, in the default case, we do not advertise any
4271 * support for early_data, but if a client were to send us some (e.g.
4272 * because of an old, stale ticket) then we will tolerate it and skip over
4275 ret
->recv_max_early_data
= SSL3_RT_MAX_PLAIN_LENGTH
;
4277 /* By default we send two session tickets automatically in TLSv1.3 */
4278 ret
->num_tickets
= 2;
4280 # ifndef OPENSSL_NO_QUIC
4281 /* only create a cache for client CTX-es */
4282 if (meth
== OSSL_QUIC_client_method())
4283 if ((ret
->tokencache
= ossl_quic_new_token_store()) == NULL
)
4285 ret
->domain_flags
= 0;
4286 if (IS_QUIC_METHOD(meth
)) {
4287 # if defined(OPENSSL_THREADS)
4288 if (meth
== OSSL_QUIC_client_thread_method())
4290 = SSL_DOMAIN_FLAG_MULTI_THREAD
4291 | SSL_DOMAIN_FLAG_THREAD_ASSISTED
4292 | SSL_DOMAIN_FLAG_BLOCKING
;
4295 = SSL_DOMAIN_FLAG_MULTI_THREAD
4296 | SSL_DOMAIN_FLAG_LEGACY_BLOCKING
;
4299 = SSL_DOMAIN_FLAG_SINGLE_THREAD
4300 | SSL_DOMAIN_FLAG_LEGACY_BLOCKING
;
4305 if (!ssl_ctx_system_config(ret
)) {
4306 ERR_raise(ERR_LIB_SSL
, SSL_R_ERROR_IN_SYSTEM_DEFAULT_CONFIG
);
4310 #ifndef OPENSSL_NO_SSLKEYLOG
4311 if (keylogfile
!= NULL
&& strlen(keylogfile
) != 0) {
4312 /* Make sure we have a global lock allocated */
4313 if (!RUN_ONCE(&ssl_keylog_once
, ssl_keylog_init
)) {
4314 /* use a trace message as a warning */
4315 OSSL_TRACE(TLS
, "Unable to initalize keylog data\n");
4319 /* Grab our global lock */
4320 if (!CRYPTO_THREAD_write_lock(keylog_lock
)) {
4321 OSSL_TRACE(TLS
, "Unable to acquire keylog write lock\n");
4325 * If the bio for the requested keylog file hasn't been
4326 * created yet, go ahead and create it, and set it to append
4327 * if its already there.
4329 if (keylog_bio
== NULL
) {
4330 keylog_bio
= get_sslkeylog_bio(keylogfile
);
4331 if (keylog_bio
== NULL
) {
4332 OSSL_TRACE(TLS
, "Unable to create keylog bio\n");
4335 BIO_set_callback_ex(keylog_bio
, check_keylog_bio_free
);
4337 /* up our refcount for the already-created case */
4338 BIO_up_ref(keylog_bio
);
4340 /* If we have a bio now, assign the callback handler */
4341 if (keylog_bio
!= NULL
)
4342 ret
->do_sslkeylog
= 1;
4343 /* unlock, and we're done */
4344 CRYPTO_THREAD_unlock(keylog_lock
);
4352 #ifndef OPENSSL_NO_SSLKEYLOG
4353 BIO_free(keylog_bio
);
4358 SSL_CTX
*SSL_CTX_new(const SSL_METHOD
*meth
)
4360 return SSL_CTX_new_ex(NULL
, NULL
, meth
);
4363 int SSL_CTX_up_ref(SSL_CTX
*ctx
)
4367 if (CRYPTO_UP_REF(&ctx
->references
, &i
) <= 0)
4370 REF_PRINT_COUNT("SSL_CTX", i
, ctx
);
4371 REF_ASSERT_ISNT(i
< 2);
4372 return ((i
> 1) ? 1 : 0);
4375 void SSL_CTX_free(SSL_CTX
*a
)
4383 CRYPTO_DOWN_REF(&a
->references
, &i
);
4384 REF_PRINT_COUNT("SSL_CTX", i
, a
);
4387 REF_ASSERT_ISNT(i
< 0);
4389 #ifndef OPENSSL_NO_SSLKEYLOG
4390 if (keylog_lock
!= NULL
&& CRYPTO_THREAD_write_lock(keylog_lock
)) {
4391 if (a
->do_sslkeylog
== 1)
4392 BIO_free(keylog_bio
);
4393 a
->do_sslkeylog
= 0;
4394 CRYPTO_THREAD_unlock(keylog_lock
);
4398 X509_VERIFY_PARAM_free(a
->param
);
4399 dane_ctx_final(&a
->dane
);
4402 * Free internal session cache. However: the remove_cb() may reference
4403 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
4404 * after the sessions were flushed.
4405 * As the ex_data handling routines might also touch the session cache,
4406 * the most secure solution seems to be: empty (flush) the cache, then
4407 * free ex_data, then finally free the cache.
4408 * (See ticket [openssl.org #212].)
4410 if (a
->sessions
!= NULL
)
4411 SSL_CTX_flush_sessions_ex(a
, 0);
4413 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX
, a
, &a
->ex_data
);
4414 lh_SSL_SESSION_free(a
->sessions
);
4415 X509_STORE_free(a
->cert_store
);
4416 #ifndef OPENSSL_NO_CT
4417 CTLOG_STORE_free(a
->ctlog_store
);
4419 sk_SSL_CIPHER_free(a
->cipher_list
);
4420 sk_SSL_CIPHER_free(a
->cipher_list_by_id
);
4421 sk_SSL_CIPHER_free(a
->tls13_ciphersuites
);
4422 ssl_cert_free(a
->cert
);
4423 sk_X509_NAME_pop_free(a
->ca_names
, X509_NAME_free
);
4424 sk_X509_NAME_pop_free(a
->client_ca_names
, X509_NAME_free
);
4425 OSSL_STACK_OF_X509_free(a
->extra_certs
);
4426 a
->comp_methods
= NULL
;
4427 #ifndef OPENSSL_NO_SRTP
4428 sk_SRTP_PROTECTION_PROFILE_free(a
->srtp_profiles
);
4430 #ifndef OPENSSL_NO_SRP
4431 ssl_ctx_srp_ctx_free_intern(a
);
4433 #ifndef OPENSSL_NO_ENGINE
4434 tls_engine_finish(a
->client_cert_engine
);
4437 OPENSSL_free(a
->ext
.ecpointformats
);
4438 OPENSSL_free(a
->ext
.supportedgroups
);
4439 OPENSSL_free(a
->ext
.keyshares
);
4440 OPENSSL_free(a
->ext
.tuples
);
4441 OPENSSL_free(a
->ext
.alpn
);
4442 OPENSSL_secure_free(a
->ext
.secure
);
4444 ssl_evp_md_free(a
->md5
);
4445 ssl_evp_md_free(a
->sha1
);
4447 for (j
= 0; j
< SSL_ENC_NUM_IDX
; j
++)
4448 ssl_evp_cipher_free(a
->ssl_cipher_methods
[j
]);
4449 for (j
= 0; j
< SSL_MD_NUM_IDX
; j
++)
4450 ssl_evp_md_free(a
->ssl_digest_methods
[j
]);
4451 for (j
= 0; j
< a
->group_list_len
; j
++) {
4452 OPENSSL_free(a
->group_list
[j
].tlsname
);
4453 OPENSSL_free(a
->group_list
[j
].realname
);
4454 OPENSSL_free(a
->group_list
[j
].algorithm
);
4456 OPENSSL_free(a
->group_list
);
4457 for (j
= 0; j
< a
->sigalg_list_len
; j
++) {
4458 OPENSSL_free(a
->sigalg_list
[j
].name
);
4459 OPENSSL_free(a
->sigalg_list
[j
].sigalg_name
);
4460 OPENSSL_free(a
->sigalg_list
[j
].sigalg_oid
);
4461 OPENSSL_free(a
->sigalg_list
[j
].sig_name
);
4462 OPENSSL_free(a
->sigalg_list
[j
].sig_oid
);
4463 OPENSSL_free(a
->sigalg_list
[j
].hash_name
);
4464 OPENSSL_free(a
->sigalg_list
[j
].hash_oid
);
4465 OPENSSL_free(a
->sigalg_list
[j
].keytype
);
4466 OPENSSL_free(a
->sigalg_list
[j
].keytype_oid
);
4468 OPENSSL_free(a
->sigalg_list
);
4469 OPENSSL_free(a
->ssl_cert_info
);
4471 OPENSSL_free(a
->sigalg_lookup_cache
);
4472 OPENSSL_free(a
->tls12_sigalgs
);
4474 OPENSSL_free(a
->client_cert_type
);
4475 OPENSSL_free(a
->server_cert_type
);
4477 CRYPTO_THREAD_lock_free(a
->lock
);
4478 CRYPTO_FREE_REF(&a
->references
);
4479 #ifdef TSAN_REQUIRES_LOCKING
4480 CRYPTO_THREAD_lock_free(a
->tsan_lock
);
4483 OPENSSL_free(a
->propq
);
4484 #ifndef OPENSSL_NO_QLOG
4485 OPENSSL_free(a
->qlog_title
);
4488 #ifndef OPENSSL_NO_QUIC
4489 ossl_quic_free_token_store(a
->tokencache
);
4495 void SSL_CTX_set_default_passwd_cb(SSL_CTX
*ctx
, pem_password_cb
*cb
)
4497 ctx
->default_passwd_callback
= cb
;
4500 void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX
*ctx
, void *u
)
4502 ctx
->default_passwd_callback_userdata
= u
;
4505 pem_password_cb
*SSL_CTX_get_default_passwd_cb(SSL_CTX
*ctx
)
4507 return ctx
->default_passwd_callback
;
4510 void *SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX
*ctx
)
4512 return ctx
->default_passwd_callback_userdata
;
4515 void SSL_set_default_passwd_cb(SSL
*s
, pem_password_cb
*cb
)
4517 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
4522 sc
->default_passwd_callback
= cb
;
4525 void SSL_set_default_passwd_cb_userdata(SSL
*s
, void *u
)
4527 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
4532 sc
->default_passwd_callback_userdata
= u
;
4535 pem_password_cb
*SSL_get_default_passwd_cb(SSL
*s
)
4537 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
4542 return sc
->default_passwd_callback
;
4545 void *SSL_get_default_passwd_cb_userdata(SSL
*s
)
4547 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
4552 return sc
->default_passwd_callback_userdata
;
4555 void SSL_CTX_set_cert_verify_callback(SSL_CTX
*ctx
,
4556 int (*cb
) (X509_STORE_CTX
*, void *),
4559 ctx
->app_verify_callback
= cb
;
4560 ctx
->app_verify_arg
= arg
;
4563 void SSL_CTX_set_verify(SSL_CTX
*ctx
, int mode
,
4564 int (*cb
) (int, X509_STORE_CTX
*))
4566 ctx
->verify_mode
= mode
;
4567 ctx
->default_verify_callback
= cb
;
4570 void SSL_CTX_set_verify_depth(SSL_CTX
*ctx
, int depth
)
4572 X509_VERIFY_PARAM_set_depth(ctx
->param
, depth
);
4575 void SSL_CTX_set_cert_cb(SSL_CTX
*c
, int (*cb
) (SSL
*ssl
, void *arg
), void *arg
)
4577 ssl_cert_set_cert_cb(c
->cert
, cb
, arg
);
4580 void SSL_set_cert_cb(SSL
*s
, int (*cb
) (SSL
*ssl
, void *arg
), void *arg
)
4582 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
4587 ssl_cert_set_cert_cb(sc
->cert
, cb
, arg
);
4590 void ssl_set_masks(SSL_CONNECTION
*s
)
4593 uint32_t *pvalid
= s
->s3
.tmp
.valid_flags
;
4594 int rsa_enc
, rsa_sign
, dh_tmp
, dsa_sign
;
4595 unsigned long mask_k
, mask_a
;
4596 int have_ecc_cert
, ecdsa_ok
;
4601 dh_tmp
= (c
->dh_tmp
!= NULL
4602 || c
->dh_tmp_cb
!= NULL
4605 rsa_enc
= pvalid
[SSL_PKEY_RSA
] & CERT_PKEY_VALID
;
4606 rsa_sign
= pvalid
[SSL_PKEY_RSA
] & CERT_PKEY_VALID
;
4607 dsa_sign
= pvalid
[SSL_PKEY_DSA_SIGN
] & CERT_PKEY_VALID
;
4608 have_ecc_cert
= pvalid
[SSL_PKEY_ECC
] & CERT_PKEY_VALID
;
4612 OSSL_TRACE4(TLS_CIPHER
, "dh_tmp=%d rsa_enc=%d rsa_sign=%d dsa_sign=%d\n",
4613 dh_tmp
, rsa_enc
, rsa_sign
, dsa_sign
);
4615 #ifndef OPENSSL_NO_GOST
4616 if (ssl_has_cert(s
, SSL_PKEY_GOST12_512
)) {
4617 mask_k
|= SSL_kGOST
| SSL_kGOST18
;
4618 mask_a
|= SSL_aGOST12
;
4620 if (ssl_has_cert(s
, SSL_PKEY_GOST12_256
)) {
4621 mask_k
|= SSL_kGOST
| SSL_kGOST18
;
4622 mask_a
|= SSL_aGOST12
;
4624 if (ssl_has_cert(s
, SSL_PKEY_GOST01
)) {
4625 mask_k
|= SSL_kGOST
;
4626 mask_a
|= SSL_aGOST01
;
4637 * If we only have an RSA-PSS certificate allow RSA authentication
4638 * if TLS 1.2 and peer supports it.
4641 if (rsa_enc
|| rsa_sign
|| (ssl_has_cert(s
, SSL_PKEY_RSA_PSS_SIGN
)
4642 && pvalid
[SSL_PKEY_RSA_PSS_SIGN
] & CERT_PKEY_EXPLICIT_SIGN
4643 && TLS1_get_version(&s
->ssl
) == TLS1_2_VERSION
))
4650 mask_a
|= SSL_aNULL
;
4653 * You can do anything with an RPK key, since there's no cert to restrict it
4654 * But we need to check for private keys
4656 if (pvalid
[SSL_PKEY_RSA
] & CERT_PKEY_RPK
) {
4660 if (pvalid
[SSL_PKEY_ECC
] & CERT_PKEY_RPK
)
4661 mask_a
|= SSL_aECDSA
;
4662 if (TLS1_get_version(&s
->ssl
) == TLS1_2_VERSION
) {
4663 if (pvalid
[SSL_PKEY_RSA_PSS_SIGN
] & CERT_PKEY_RPK
)
4665 if (pvalid
[SSL_PKEY_ED25519
] & CERT_PKEY_RPK
4666 || pvalid
[SSL_PKEY_ED448
] & CERT_PKEY_RPK
)
4667 mask_a
|= SSL_aECDSA
;
4671 * An ECC certificate may be usable for ECDH and/or ECDSA cipher suites
4672 * depending on the key usage extension.
4674 if (have_ecc_cert
) {
4676 ex_kusage
= X509_get_key_usage(c
->pkeys
[SSL_PKEY_ECC
].x509
);
4677 ecdsa_ok
= ex_kusage
& X509v3_KU_DIGITAL_SIGNATURE
;
4678 if (!(pvalid
[SSL_PKEY_ECC
] & CERT_PKEY_SIGN
))
4681 mask_a
|= SSL_aECDSA
;
4683 /* Allow Ed25519 for TLS 1.2 if peer supports it */
4684 if (!(mask_a
& SSL_aECDSA
) && ssl_has_cert(s
, SSL_PKEY_ED25519
)
4685 && pvalid
[SSL_PKEY_ED25519
] & CERT_PKEY_EXPLICIT_SIGN
4686 && TLS1_get_version(&s
->ssl
) == TLS1_2_VERSION
)
4687 mask_a
|= SSL_aECDSA
;
4689 /* Allow Ed448 for TLS 1.2 if peer supports it */
4690 if (!(mask_a
& SSL_aECDSA
) && ssl_has_cert(s
, SSL_PKEY_ED448
)
4691 && pvalid
[SSL_PKEY_ED448
] & CERT_PKEY_EXPLICIT_SIGN
4692 && TLS1_get_version(&s
->ssl
) == TLS1_2_VERSION
)
4693 mask_a
|= SSL_aECDSA
;
4695 mask_k
|= SSL_kECDHE
;
4697 #ifndef OPENSSL_NO_PSK
4700 if (mask_k
& SSL_kRSA
)
4701 mask_k
|= SSL_kRSAPSK
;
4702 if (mask_k
& SSL_kDHE
)
4703 mask_k
|= SSL_kDHEPSK
;
4704 if (mask_k
& SSL_kECDHE
)
4705 mask_k
|= SSL_kECDHEPSK
;
4708 s
->s3
.tmp
.mask_k
= mask_k
;
4709 s
->s3
.tmp
.mask_a
= mask_a
;
4712 int ssl_check_srvr_ecc_cert_and_alg(X509
*x
, SSL_CONNECTION
*s
)
4714 if (s
->s3
.tmp
.new_cipher
->algorithm_auth
& SSL_aECDSA
) {
4715 /* key usage, if present, must allow signing */
4716 if (!(X509_get_key_usage(x
) & X509v3_KU_DIGITAL_SIGNATURE
)) {
4717 ERR_raise(ERR_LIB_SSL
, SSL_R_ECC_CERT_NOT_FOR_SIGNING
);
4721 return 1; /* all checks are ok */
4724 int ssl_get_server_cert_serverinfo(SSL_CONNECTION
*s
,
4725 const unsigned char **serverinfo
,
4726 size_t *serverinfo_length
)
4728 CERT_PKEY
*cpk
= s
->s3
.tmp
.cert
;
4729 *serverinfo_length
= 0;
4731 if (cpk
== NULL
|| cpk
->serverinfo
== NULL
)
4734 *serverinfo
= cpk
->serverinfo
;
4735 *serverinfo_length
= cpk
->serverinfo_length
;
4739 void ssl_update_cache(SSL_CONNECTION
*s
, int mode
)
4744 * If the session_id_length is 0, we are not supposed to cache it, and it
4745 * would be rather hard to do anyway :-). Also if the session has already
4746 * been marked as not_resumable we should not cache it for later reuse.
4748 if (s
->session
->session_id_length
== 0 || s
->session
->not_resumable
)
4752 * If sid_ctx_length is 0 there is no specific application context
4753 * associated with this session, so when we try to resume it and
4754 * SSL_VERIFY_PEER is requested to verify the client identity, we have no
4755 * indication that this is actually a session for the proper application
4756 * context, and the *handshake* will fail, not just the resumption attempt.
4757 * Do not cache (on the server) these sessions that are not resumable
4758 * (clients can set SSL_VERIFY_PEER without needing a sid_ctx set).
4760 if (s
->server
&& s
->session
->sid_ctx_length
== 0
4761 && (s
->verify_mode
& SSL_VERIFY_PEER
) != 0)
4764 i
= s
->session_ctx
->session_cache_mode
;
4766 && (!s
->hit
|| SSL_CONNECTION_IS_TLS13(s
))) {
4768 * Add the session to the internal cache. In server side TLSv1.3 we
4769 * normally don't do this because by default it's a full stateless ticket
4770 * with only a dummy session id so there is no reason to cache it,
4772 * - we are doing early_data, in which case we cache so that we can
4774 * - the application has set a remove_session_cb so needs to know about
4775 * session timeout events
4776 * - SSL_OP_NO_TICKET is set in which case it is a stateful ticket
4778 if ((i
& SSL_SESS_CACHE_NO_INTERNAL_STORE
) == 0
4779 && (!SSL_CONNECTION_IS_TLS13(s
)
4781 || (s
->max_early_data
> 0
4782 && (s
->options
& SSL_OP_NO_ANTI_REPLAY
) == 0)
4783 || s
->session_ctx
->remove_session_cb
!= NULL
4784 || (s
->options
& SSL_OP_NO_TICKET
) != 0))
4785 SSL_CTX_add_session(s
->session_ctx
, s
->session
);
4788 * Add the session to the external cache. We do this even in server side
4789 * TLSv1.3 without early data because some applications just want to
4790 * know about the creation of a session and aren't doing a full cache.
4792 if (s
->session_ctx
->new_session_cb
!= NULL
&& SSL_SESSION_up_ref(s
->session
)) {
4793 if (!s
->session_ctx
->new_session_cb(SSL_CONNECTION_GET_USER_SSL(s
),
4795 SSL_SESSION_free(s
->session
);
4799 /* auto flush every 255 connections */
4800 if ((!(i
& SSL_SESS_CACHE_NO_AUTO_CLEAR
)) && ((i
& mode
) == mode
)) {
4801 TSAN_QUALIFIER
int *stat
;
4803 if (mode
& SSL_SESS_CACHE_CLIENT
)
4804 stat
= &s
->session_ctx
->stats
.sess_connect_good
;
4806 stat
= &s
->session_ctx
->stats
.sess_accept_good
;
4807 if ((ssl_tsan_load(s
->session_ctx
, stat
) & 0xff) == 0xff)
4808 SSL_CTX_flush_sessions_ex(s
->session_ctx
, time(NULL
));
4812 const SSL_METHOD
*SSL_CTX_get_ssl_method(const SSL_CTX
*ctx
)
4817 const SSL_METHOD
*SSL_get_ssl_method(const SSL
*s
)
4822 int SSL_set_ssl_method(SSL
*s
, const SSL_METHOD
*meth
)
4825 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
4827 /* Not allowed for QUIC */
4829 || (s
->type
!= SSL_TYPE_SSL_CONNECTION
&& s
->method
!= meth
)
4830 || (s
->type
== SSL_TYPE_SSL_CONNECTION
&& IS_QUIC_METHOD(meth
)))
4833 if (s
->method
!= meth
) {
4834 const SSL_METHOD
*sm
= s
->method
;
4835 int (*hf
) (SSL
*) = sc
->handshake_func
;
4837 if (sm
->version
== meth
->version
)
4842 ret
= s
->method
->ssl_init(s
);
4845 if (hf
== sm
->ssl_connect
)
4846 sc
->handshake_func
= meth
->ssl_connect
;
4847 else if (hf
== sm
->ssl_accept
)
4848 sc
->handshake_func
= meth
->ssl_accept
;
4853 int SSL_get_error(const SSL
*s
, int i
)
4855 return ossl_ssl_get_error(s
, i
, /*check_err=*/1);
4858 int ossl_ssl_get_error(const SSL
*s
, int i
, int check_err
)
4863 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
4866 return SSL_ERROR_NONE
;
4868 #ifndef OPENSSL_NO_QUIC
4870 reason
= ossl_quic_get_error(s
, i
);
4871 if (reason
!= SSL_ERROR_NONE
)
4877 return SSL_ERROR_SSL
;
4880 * Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
4881 * where we do encode the error
4883 if (check_err
&& (l
= ERR_peek_error()) != 0) {
4884 if (ERR_GET_LIB(l
) == ERR_LIB_SYS
)
4885 return SSL_ERROR_SYSCALL
;
4887 return SSL_ERROR_SSL
;
4890 #ifndef OPENSSL_NO_QUIC
4894 if (SSL_want_read(s
)) {
4895 bio
= SSL_get_rbio(s
);
4896 if (BIO_should_read(bio
))
4897 return SSL_ERROR_WANT_READ
;
4898 else if (BIO_should_write(bio
))
4900 * This one doesn't make too much sense ... We never try to
4901 * write to the rbio, and an application program where rbio and
4902 * wbio are separate couldn't even know what it should wait for.
4903 * However if we ever set s->rwstate incorrectly (so that we
4904 * have SSL_want_read(s) instead of SSL_want_write(s)) and rbio
4905 * and wbio *are* the same, this test works around that bug; so
4906 * it might be safer to keep it.
4908 return SSL_ERROR_WANT_WRITE
;
4909 else if (BIO_should_io_special(bio
)) {
4910 reason
= BIO_get_retry_reason(bio
);
4911 if (reason
== BIO_RR_CONNECT
)
4912 return SSL_ERROR_WANT_CONNECT
;
4913 else if (reason
== BIO_RR_ACCEPT
)
4914 return SSL_ERROR_WANT_ACCEPT
;
4916 return SSL_ERROR_SYSCALL
; /* unknown */
4920 if (SSL_want_write(s
)) {
4922 * Access wbio directly - in order to use the buffered bio if
4926 if (BIO_should_write(bio
))
4927 return SSL_ERROR_WANT_WRITE
;
4928 else if (BIO_should_read(bio
))
4930 * See above (SSL_want_read(s) with BIO_should_write(bio))
4932 return SSL_ERROR_WANT_READ
;
4933 else if (BIO_should_io_special(bio
)) {
4934 reason
= BIO_get_retry_reason(bio
);
4935 if (reason
== BIO_RR_CONNECT
)
4936 return SSL_ERROR_WANT_CONNECT
;
4937 else if (reason
== BIO_RR_ACCEPT
)
4938 return SSL_ERROR_WANT_ACCEPT
;
4940 return SSL_ERROR_SYSCALL
;
4945 if (SSL_want_x509_lookup(s
))
4946 return SSL_ERROR_WANT_X509_LOOKUP
;
4947 if (SSL_want_retry_verify(s
))
4948 return SSL_ERROR_WANT_RETRY_VERIFY
;
4949 if (SSL_want_async(s
))
4950 return SSL_ERROR_WANT_ASYNC
;
4951 if (SSL_want_async_job(s
))
4952 return SSL_ERROR_WANT_ASYNC_JOB
;
4953 if (SSL_want_client_hello_cb(s
))
4954 return SSL_ERROR_WANT_CLIENT_HELLO_CB
;
4956 if ((sc
->shutdown
& SSL_RECEIVED_SHUTDOWN
) &&
4957 (sc
->s3
.warn_alert
== SSL_AD_CLOSE_NOTIFY
))
4958 return SSL_ERROR_ZERO_RETURN
;
4960 return SSL_ERROR_SYSCALL
;
4963 static int ssl_do_handshake_intern(void *vargs
)
4965 struct ssl_async_args
*args
= (struct ssl_async_args
*)vargs
;
4967 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
4972 return sc
->handshake_func(s
);
4975 int SSL_do_handshake(SSL
*s
)
4978 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
4980 #ifndef OPENSSL_NO_QUIC
4982 return ossl_quic_do_handshake(s
);
4988 if (sc
->handshake_func
== NULL
) {
4989 ERR_raise(ERR_LIB_SSL
, SSL_R_CONNECTION_TYPE_NOT_SET
);
4993 if (!ossl_statem_check_finish_init(sc
, -1))
4996 s
->method
->ssl_renegotiate_check(s
, 0);
4998 if (SSL_in_init(s
) || SSL_in_before(s
)) {
4999 if ((sc
->mode
& SSL_MODE_ASYNC
) && ASYNC_get_current_job() == NULL
) {
5000 struct ssl_async_args args
;
5002 memset(&args
, 0, sizeof(args
));
5005 ret
= ssl_start_async_job(s
, &args
, ssl_do_handshake_intern
);
5007 ret
= sc
->handshake_func(s
);
5014 void SSL_set_accept_state(SSL
*s
)
5016 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(s
);
5018 #ifndef OPENSSL_NO_QUIC
5020 /* We suppress errors because this is a void function */
5021 (void)ossl_quic_set_accept_state(s
, 0 /* suppress errors */);
5028 ossl_statem_clear(sc
);
5029 sc
->handshake_func
= s
->method
->ssl_accept
;
5030 /* Ignore return value. Its a void public API function */
5031 RECORD_LAYER_reset(&sc
->rlayer
);
5034 void SSL_set_connect_state(SSL
*s
)
5036 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(s
);
5038 #ifndef OPENSSL_NO_QUIC
5040 /* We suppress errors because this is a void function */
5041 (void)ossl_quic_set_connect_state(s
, 0 /* suppress errors */);
5048 ossl_statem_clear(sc
);
5049 sc
->handshake_func
= s
->method
->ssl_connect
;
5050 /* Ignore return value. Its a void public API function */
5051 RECORD_LAYER_reset(&sc
->rlayer
);
5054 int ssl_undefined_function(SSL
*s
)
5056 ERR_raise(ERR_LIB_SSL
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
5060 int ssl_undefined_void_function(void)
5062 ERR_raise(ERR_LIB_SSL
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
5066 const char *ssl_protocol_to_string(int version
)
5069 case TLS1_3_VERSION
:
5072 case TLS1_2_VERSION
:
5075 case TLS1_1_VERSION
:
5090 case DTLS1_2_VERSION
:
5098 const char *SSL_get_version(const SSL
*s
)
5100 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
5102 #ifndef OPENSSL_NO_QUIC
5103 /* We only support QUICv1 - so if its QUIC its QUICv1 */
5104 if (s
->type
== SSL_TYPE_QUIC_CONNECTION
|| s
->type
== SSL_TYPE_QUIC_XSO
)
5111 return ssl_protocol_to_string(sc
->version
);
5114 __owur
int SSL_get_handshake_rtt(const SSL
*s
, uint64_t *rtt
)
5116 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
5120 if (sc
->ts_msg_write
.t
<= 0 || sc
->ts_msg_read
.t
<= 0)
5121 return 0; /* data not (yet) available */
5122 if (sc
->ts_msg_read
.t
< sc
->ts_msg_write
.t
)
5125 *rtt
= ossl_time2us(ossl_time_subtract(sc
->ts_msg_read
, sc
->ts_msg_write
));
5129 static int dup_ca_names(STACK_OF(X509_NAME
) **dst
, STACK_OF(X509_NAME
) *src
)
5131 STACK_OF(X509_NAME
) *sk
;
5140 if ((sk
= sk_X509_NAME_new_null()) == NULL
)
5142 for (i
= 0; i
< sk_X509_NAME_num(src
); i
++) {
5143 xn
= X509_NAME_dup(sk_X509_NAME_value(src
, i
));
5145 sk_X509_NAME_pop_free(sk
, X509_NAME_free
);
5148 if (sk_X509_NAME_insert(sk
, xn
, i
) == 0) {
5150 sk_X509_NAME_pop_free(sk
, X509_NAME_free
);
5159 SSL
*SSL_dup(SSL
*s
)
5163 /* TODO(QUIC FUTURE): Add an SSL_METHOD function for duplication */
5164 SSL_CONNECTION
*retsc
;
5165 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(s
);
5170 /* If we're not quiescent, just up_ref! */
5171 if (!SSL_in_init(s
) || !SSL_in_before(s
)) {
5172 CRYPTO_UP_REF(&s
->references
, &i
);
5177 * Otherwise, copy configuration state, and session if set.
5179 if ((ret
= SSL_new(SSL_get_SSL_CTX(s
))) == NULL
)
5181 if ((retsc
= SSL_CONNECTION_FROM_SSL_ONLY(ret
)) == NULL
)
5184 if (sc
->session
!= NULL
) {
5186 * Arranges to share the same session via up_ref. This "copies"
5187 * session-id, SSL_METHOD, sid_ctx, and 'cert'
5189 if (!SSL_copy_session_id(ret
, s
))
5193 * No session has been established yet, so we have to expect that
5194 * s->cert or ret->cert will be changed later -- they should not both
5195 * point to the same object, and thus we can't use
5196 * SSL_copy_session_id.
5198 if (!SSL_set_ssl_method(ret
, s
->method
))
5201 if (sc
->cert
!= NULL
) {
5202 ssl_cert_free(retsc
->cert
);
5203 retsc
->cert
= ssl_cert_dup(sc
->cert
);
5204 if (retsc
->cert
== NULL
)
5208 if (!SSL_set_session_id_context(ret
, sc
->sid_ctx
,
5209 (int)sc
->sid_ctx_length
))
5213 if (!ssl_dane_dup(retsc
, sc
))
5215 retsc
->version
= sc
->version
;
5216 retsc
->options
= sc
->options
;
5217 retsc
->min_proto_version
= sc
->min_proto_version
;
5218 retsc
->max_proto_version
= sc
->max_proto_version
;
5219 retsc
->mode
= sc
->mode
;
5220 SSL_set_max_cert_list(ret
, SSL_get_max_cert_list(s
));
5221 SSL_set_read_ahead(ret
, SSL_get_read_ahead(s
));
5222 retsc
->msg_callback
= sc
->msg_callback
;
5223 retsc
->msg_callback_arg
= sc
->msg_callback_arg
;
5224 SSL_set_verify(ret
, SSL_get_verify_mode(s
), SSL_get_verify_callback(s
));
5225 SSL_set_verify_depth(ret
, SSL_get_verify_depth(s
));
5226 retsc
->generate_session_id
= sc
->generate_session_id
;
5228 SSL_set_info_callback(ret
, SSL_get_info_callback(s
));
5230 /* copy app data, a little dangerous perhaps */
5231 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL
, &ret
->ex_data
, &s
->ex_data
))
5234 retsc
->server
= sc
->server
;
5235 if (sc
->handshake_func
) {
5237 SSL_set_accept_state(ret
);
5239 SSL_set_connect_state(ret
);
5241 retsc
->shutdown
= sc
->shutdown
;
5242 retsc
->hit
= sc
->hit
;
5244 retsc
->default_passwd_callback
= sc
->default_passwd_callback
;
5245 retsc
->default_passwd_callback_userdata
= sc
->default_passwd_callback_userdata
;
5247 X509_VERIFY_PARAM_inherit(retsc
->param
, sc
->param
);
5249 /* dup the cipher_list and cipher_list_by_id stacks */
5250 if (sc
->cipher_list
!= NULL
) {
5251 if ((retsc
->cipher_list
= sk_SSL_CIPHER_dup(sc
->cipher_list
)) == NULL
)
5254 if (sc
->cipher_list_by_id
!= NULL
)
5255 if ((retsc
->cipher_list_by_id
= sk_SSL_CIPHER_dup(sc
->cipher_list_by_id
))
5259 /* Dup the client_CA list */
5260 if (!dup_ca_names(&retsc
->ca_names
, sc
->ca_names
)
5261 || !dup_ca_names(&retsc
->client_ca_names
, sc
->client_ca_names
))
5271 X509
*SSL_get_certificate(const SSL
*s
)
5273 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
5278 if (sc
->cert
!= NULL
)
5279 return sc
->cert
->key
->x509
;
5284 EVP_PKEY
*SSL_get_privatekey(const SSL
*s
)
5286 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
5291 if (sc
->cert
!= NULL
)
5292 return sc
->cert
->key
->privatekey
;
5297 X509
*SSL_CTX_get0_certificate(const SSL_CTX
*ctx
)
5299 if (ctx
->cert
!= NULL
)
5300 return ctx
->cert
->key
->x509
;
5305 EVP_PKEY
*SSL_CTX_get0_privatekey(const SSL_CTX
*ctx
)
5307 if (ctx
->cert
!= NULL
)
5308 return ctx
->cert
->key
->privatekey
;
5313 const SSL_CIPHER
*SSL_get_current_cipher(const SSL
*s
)
5315 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
5320 if ((sc
->session
!= NULL
) && (sc
->session
->cipher
!= NULL
))
5321 return sc
->session
->cipher
;
5325 const SSL_CIPHER
*SSL_get_pending_cipher(const SSL
*s
)
5327 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
5332 return sc
->s3
.tmp
.new_cipher
;
5335 const COMP_METHOD
*SSL_get_current_compression(const SSL
*s
)
5337 #ifndef OPENSSL_NO_COMP
5338 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL_ONLY(s
);
5343 return sc
->rlayer
.wrlmethod
->get_compression(sc
->rlayer
.wrl
);
5349 const COMP_METHOD
*SSL_get_current_expansion(const SSL
*s
)
5351 #ifndef OPENSSL_NO_COMP
5352 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL_ONLY(s
);
5357 return sc
->rlayer
.rrlmethod
->get_compression(sc
->rlayer
.rrl
);
5363 int ssl_init_wbio_buffer(SSL_CONNECTION
*s
)
5367 if (s
->bbio
!= NULL
) {
5368 /* Already buffered. */
5372 bbio
= BIO_new(BIO_f_buffer());
5373 if (bbio
== NULL
|| BIO_set_read_buffer_size(bbio
, 1) <= 0) {
5375 ERR_raise(ERR_LIB_SSL
, ERR_R_BUF_LIB
);
5379 s
->wbio
= BIO_push(bbio
, s
->wbio
);
5381 s
->rlayer
.wrlmethod
->set1_bio(s
->rlayer
.wrl
, s
->wbio
);
5386 int ssl_free_wbio_buffer(SSL_CONNECTION
*s
)
5388 /* callers ensure s is never null */
5389 if (s
->bbio
== NULL
)
5392 s
->wbio
= BIO_pop(s
->wbio
);
5393 s
->rlayer
.wrlmethod
->set1_bio(s
->rlayer
.wrl
, s
->wbio
);
5401 void SSL_CTX_set_quiet_shutdown(SSL_CTX
*ctx
, int mode
)
5403 ctx
->quiet_shutdown
= mode
;
5406 int SSL_CTX_get_quiet_shutdown(const SSL_CTX
*ctx
)
5408 return ctx
->quiet_shutdown
;
5411 void SSL_set_quiet_shutdown(SSL
*s
, int mode
)
5413 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(s
);
5415 /* Not supported with QUIC */
5419 sc
->quiet_shutdown
= mode
;
5422 int SSL_get_quiet_shutdown(const SSL
*s
)
5424 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL_ONLY(s
);
5426 /* Not supported with QUIC */
5430 return sc
->quiet_shutdown
;
5433 void SSL_set_shutdown(SSL
*s
, int mode
)
5435 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(s
);
5437 /* Not supported with QUIC */
5441 sc
->shutdown
= mode
;
5444 int SSL_get_shutdown(const SSL
*s
)
5446 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL_ONLY(s
);
5448 #ifndef OPENSSL_NO_QUIC
5449 /* QUIC: Just indicate whether the connection was shutdown cleanly. */
5451 return ossl_quic_get_shutdown(s
);
5457 return sc
->shutdown
;
5460 int SSL_version(const SSL
*s
)
5462 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
5464 #ifndef OPENSSL_NO_QUIC
5465 /* We only support QUICv1 - so if its QUIC its QUICv1 */
5466 if (s
->type
== SSL_TYPE_QUIC_CONNECTION
|| s
->type
== SSL_TYPE_QUIC_XSO
)
5467 return OSSL_QUIC1_VERSION
;
5475 int SSL_client_version(const SSL
*s
)
5477 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
5479 #ifndef OPENSSL_NO_QUIC
5480 /* We only support QUICv1 - so if its QUIC its QUICv1 */
5481 if (s
->type
== SSL_TYPE_QUIC_CONNECTION
|| s
->type
== SSL_TYPE_QUIC_XSO
)
5482 return OSSL_QUIC1_VERSION
;
5487 return sc
->client_version
;
5490 SSL_CTX
*SSL_get_SSL_CTX(const SSL
*ssl
)
5495 SSL_CTX
*SSL_set_SSL_CTX(SSL
*ssl
, SSL_CTX
*ctx
)
5498 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(ssl
);
5500 /* TODO(QUIC FUTURE): Add support for QUIC */
5504 if (ssl
->ctx
== ctx
)
5507 ctx
= sc
->session_ctx
;
5508 new_cert
= ssl_cert_dup(ctx
->cert
);
5509 if (new_cert
== NULL
)
5511 if (!custom_exts_copy_conn(&new_cert
->custext
, &sc
->cert
->custext
))
5513 if (!custom_exts_copy_flags(&new_cert
->custext
, &sc
->cert
->custext
))
5517 * Program invariant: |sid_ctx| has fixed size (SSL_MAX_SID_CTX_LENGTH),
5518 * so setter APIs must prevent invalid lengths from entering the system.
5520 if (!ossl_assert(sc
->sid_ctx_length
<= sizeof(sc
->sid_ctx
)))
5522 if (!SSL_CTX_up_ref(ctx
))
5526 * If the session ID context matches that of the parent SSL_CTX,
5527 * inherit it from the new SSL_CTX as well. If however the context does
5528 * not match (i.e., it was set per-ssl with SSL_set_session_id_context),
5529 * leave it unchanged.
5531 if ((ssl
->ctx
!= NULL
) &&
5532 (sc
->sid_ctx_length
== ssl
->ctx
->sid_ctx_length
) &&
5533 (memcmp(sc
->sid_ctx
, ssl
->ctx
->sid_ctx
, sc
->sid_ctx_length
) == 0)) {
5534 sc
->sid_ctx_length
= ctx
->sid_ctx_length
;
5535 memcpy(&sc
->sid_ctx
, &ctx
->sid_ctx
, sizeof(sc
->sid_ctx
));
5538 ssl_cert_free(sc
->cert
);
5539 sc
->cert
= new_cert
;
5540 SSL_CTX_free(ssl
->ctx
); /* decrement reference count */
5546 ssl_cert_free(new_cert
);
5550 int SSL_CTX_set_default_verify_paths(SSL_CTX
*ctx
)
5552 return X509_STORE_set_default_paths_ex(ctx
->cert_store
, ctx
->libctx
,
5556 int SSL_CTX_set_default_verify_dir(SSL_CTX
*ctx
)
5558 X509_LOOKUP
*lookup
;
5560 lookup
= X509_STORE_add_lookup(ctx
->cert_store
, X509_LOOKUP_hash_dir());
5564 /* We ignore errors, in case the directory doesn't exist */
5567 X509_LOOKUP_add_dir(lookup
, NULL
, X509_FILETYPE_DEFAULT
);
5574 int SSL_CTX_set_default_verify_file(SSL_CTX
*ctx
)
5576 X509_LOOKUP
*lookup
;
5578 lookup
= X509_STORE_add_lookup(ctx
->cert_store
, X509_LOOKUP_file());
5582 /* We ignore errors, in case the file doesn't exist */
5585 X509_LOOKUP_load_file_ex(lookup
, NULL
, X509_FILETYPE_DEFAULT
, ctx
->libctx
,
5593 int SSL_CTX_set_default_verify_store(SSL_CTX
*ctx
)
5595 X509_LOOKUP
*lookup
;
5597 lookup
= X509_STORE_add_lookup(ctx
->cert_store
, X509_LOOKUP_store());
5601 /* We ignore errors, in case the directory doesn't exist */
5604 X509_LOOKUP_add_store_ex(lookup
, NULL
, ctx
->libctx
, ctx
->propq
);
5611 int SSL_CTX_load_verify_file(SSL_CTX
*ctx
, const char *CAfile
)
5613 return X509_STORE_load_file_ex(ctx
->cert_store
, CAfile
, ctx
->libctx
,
5617 int SSL_CTX_load_verify_dir(SSL_CTX
*ctx
, const char *CApath
)
5619 return X509_STORE_load_path(ctx
->cert_store
, CApath
);
5622 int SSL_CTX_load_verify_store(SSL_CTX
*ctx
, const char *CAstore
)
5624 return X509_STORE_load_store_ex(ctx
->cert_store
, CAstore
, ctx
->libctx
,
5628 int SSL_CTX_load_verify_locations(SSL_CTX
*ctx
, const char *CAfile
,
5631 if (CAfile
== NULL
&& CApath
== NULL
)
5633 if (CAfile
!= NULL
&& !SSL_CTX_load_verify_file(ctx
, CAfile
))
5635 if (CApath
!= NULL
&& !SSL_CTX_load_verify_dir(ctx
, CApath
))
5640 void SSL_set_info_callback(SSL
*ssl
,
5641 void (*cb
) (const SSL
*ssl
, int type
, int val
))
5643 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
5648 sc
->info_callback
= cb
;
5652 * One compiler (Diab DCC) doesn't like argument names in returned function
5655 void (*SSL_get_info_callback(const SSL
*ssl
)) (const SSL
* /* ssl */ ,
5658 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(ssl
);
5663 return sc
->info_callback
;
5666 void SSL_set_verify_result(SSL
*ssl
, long arg
)
5668 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
5673 sc
->verify_result
= arg
;
5676 long SSL_get_verify_result(const SSL
*ssl
)
5678 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(ssl
);
5683 return sc
->verify_result
;
5686 size_t SSL_get_client_random(const SSL
*ssl
, unsigned char *out
, size_t outlen
)
5688 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(ssl
);
5694 return sizeof(sc
->s3
.client_random
);
5695 if (outlen
> sizeof(sc
->s3
.client_random
))
5696 outlen
= sizeof(sc
->s3
.client_random
);
5697 memcpy(out
, sc
->s3
.client_random
, outlen
);
5701 size_t SSL_get_server_random(const SSL
*ssl
, unsigned char *out
, size_t outlen
)
5703 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(ssl
);
5709 return sizeof(sc
->s3
.server_random
);
5710 if (outlen
> sizeof(sc
->s3
.server_random
))
5711 outlen
= sizeof(sc
->s3
.server_random
);
5712 memcpy(out
, sc
->s3
.server_random
, outlen
);
5716 size_t SSL_SESSION_get_master_key(const SSL_SESSION
*session
,
5717 unsigned char *out
, size_t outlen
)
5720 return session
->master_key_length
;
5721 if (outlen
> session
->master_key_length
)
5722 outlen
= session
->master_key_length
;
5723 memcpy(out
, session
->master_key
, outlen
);
5727 int SSL_SESSION_set1_master_key(SSL_SESSION
*sess
, const unsigned char *in
,
5730 if (len
> sizeof(sess
->master_key
))
5733 memcpy(sess
->master_key
, in
, len
);
5734 sess
->master_key_length
= len
;
5739 int SSL_set_ex_data(SSL
*s
, int idx
, void *arg
)
5741 return CRYPTO_set_ex_data(&s
->ex_data
, idx
, arg
);
5744 void *SSL_get_ex_data(const SSL
*s
, int idx
)
5746 return CRYPTO_get_ex_data(&s
->ex_data
, idx
);
5749 int SSL_CTX_set_ex_data(SSL_CTX
*s
, int idx
, void *arg
)
5751 return CRYPTO_set_ex_data(&s
->ex_data
, idx
, arg
);
5754 void *SSL_CTX_get_ex_data(const SSL_CTX
*s
, int idx
)
5756 return CRYPTO_get_ex_data(&s
->ex_data
, idx
);
5759 X509_STORE
*SSL_CTX_get_cert_store(const SSL_CTX
*ctx
)
5761 return ctx
->cert_store
;
5764 void SSL_CTX_set_cert_store(SSL_CTX
*ctx
, X509_STORE
*store
)
5766 X509_STORE_free(ctx
->cert_store
);
5767 ctx
->cert_store
= store
;
5770 void SSL_CTX_set1_cert_store(SSL_CTX
*ctx
, X509_STORE
*store
)
5772 if (store
!= NULL
&& !X509_STORE_up_ref(store
))
5775 SSL_CTX_set_cert_store(ctx
, store
);
5778 int SSL_want(const SSL
*s
)
5780 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
5782 #ifndef OPENSSL_NO_QUIC
5784 return ossl_quic_want(s
);
5793 #ifndef OPENSSL_NO_PSK
5794 int SSL_CTX_use_psk_identity_hint(SSL_CTX
*ctx
, const char *identity_hint
)
5796 if (identity_hint
!= NULL
&& strlen(identity_hint
) > PSK_MAX_IDENTITY_LEN
) {
5797 ERR_raise(ERR_LIB_SSL
, SSL_R_DATA_LENGTH_TOO_LONG
);
5800 OPENSSL_free(ctx
->cert
->psk_identity_hint
);
5801 if (identity_hint
!= NULL
) {
5802 ctx
->cert
->psk_identity_hint
= OPENSSL_strdup(identity_hint
);
5803 if (ctx
->cert
->psk_identity_hint
== NULL
)
5806 ctx
->cert
->psk_identity_hint
= NULL
;
5810 int SSL_use_psk_identity_hint(SSL
*s
, const char *identity_hint
)
5812 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
5817 if (identity_hint
!= NULL
&& strlen(identity_hint
) > PSK_MAX_IDENTITY_LEN
) {
5818 ERR_raise(ERR_LIB_SSL
, SSL_R_DATA_LENGTH_TOO_LONG
);
5821 OPENSSL_free(sc
->cert
->psk_identity_hint
);
5822 if (identity_hint
!= NULL
) {
5823 sc
->cert
->psk_identity_hint
= OPENSSL_strdup(identity_hint
);
5824 if (sc
->cert
->psk_identity_hint
== NULL
)
5827 sc
->cert
->psk_identity_hint
= NULL
;
5831 const char *SSL_get_psk_identity_hint(const SSL
*s
)
5833 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
5835 if (sc
== NULL
|| sc
->session
== NULL
)
5838 return sc
->session
->psk_identity_hint
;
5841 const char *SSL_get_psk_identity(const SSL
*s
)
5843 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
5845 if (sc
== NULL
|| sc
->session
== NULL
)
5848 return sc
->session
->psk_identity
;
5851 void SSL_set_psk_client_callback(SSL
*s
, SSL_psk_client_cb_func cb
)
5853 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
5858 sc
->psk_client_callback
= cb
;
5861 void SSL_CTX_set_psk_client_callback(SSL_CTX
*ctx
, SSL_psk_client_cb_func cb
)
5863 ctx
->psk_client_callback
= cb
;
5866 void SSL_set_psk_server_callback(SSL
*s
, SSL_psk_server_cb_func cb
)
5868 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
5873 sc
->psk_server_callback
= cb
;
5876 void SSL_CTX_set_psk_server_callback(SSL_CTX
*ctx
, SSL_psk_server_cb_func cb
)
5878 ctx
->psk_server_callback
= cb
;
5882 void SSL_set_psk_find_session_callback(SSL
*s
, SSL_psk_find_session_cb_func cb
)
5884 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
5889 sc
->psk_find_session_cb
= cb
;
5892 void SSL_CTX_set_psk_find_session_callback(SSL_CTX
*ctx
,
5893 SSL_psk_find_session_cb_func cb
)
5895 ctx
->psk_find_session_cb
= cb
;
5898 void SSL_set_psk_use_session_callback(SSL
*s
, SSL_psk_use_session_cb_func cb
)
5900 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
5905 sc
->psk_use_session_cb
= cb
;
5908 void SSL_CTX_set_psk_use_session_callback(SSL_CTX
*ctx
,
5909 SSL_psk_use_session_cb_func cb
)
5911 ctx
->psk_use_session_cb
= cb
;
5914 void SSL_CTX_set_msg_callback(SSL_CTX
*ctx
,
5915 void (*cb
) (int write_p
, int version
,
5916 int content_type
, const void *buf
,
5917 size_t len
, SSL
*ssl
, void *arg
))
5919 SSL_CTX_callback_ctrl(ctx
, SSL_CTRL_SET_MSG_CALLBACK
, (void (*)(void))cb
);
5922 void SSL_set_msg_callback(SSL
*ssl
,
5923 void (*cb
) (int write_p
, int version
,
5924 int content_type
, const void *buf
,
5925 size_t len
, SSL
*ssl
, void *arg
))
5927 SSL_callback_ctrl(ssl
, SSL_CTRL_SET_MSG_CALLBACK
, (void (*)(void))cb
);
5930 void SSL_CTX_set_not_resumable_session_callback(SSL_CTX
*ctx
,
5931 int (*cb
) (SSL
*ssl
,
5935 SSL_CTX_callback_ctrl(ctx
, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB
,
5936 (void (*)(void))cb
);
5939 void SSL_set_not_resumable_session_callback(SSL
*ssl
,
5940 int (*cb
) (SSL
*ssl
,
5941 int is_forward_secure
))
5943 SSL_callback_ctrl(ssl
, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB
,
5944 (void (*)(void))cb
);
5947 void SSL_CTX_set_record_padding_callback(SSL_CTX
*ctx
,
5948 size_t (*cb
) (SSL
*ssl
, int type
,
5949 size_t len
, void *arg
))
5951 ctx
->record_padding_cb
= cb
;
5954 void SSL_CTX_set_record_padding_callback_arg(SSL_CTX
*ctx
, void *arg
)
5956 ctx
->record_padding_arg
= arg
;
5959 void *SSL_CTX_get_record_padding_callback_arg(const SSL_CTX
*ctx
)
5961 return ctx
->record_padding_arg
;
5964 int SSL_CTX_set_block_padding_ex(SSL_CTX
*ctx
, size_t app_block_size
,
5965 size_t hs_block_size
)
5967 if (IS_QUIC_CTX(ctx
) && (app_block_size
> 1 || hs_block_size
> 1))
5970 /* block size of 0 or 1 is basically no padding */
5971 if (app_block_size
== 1) {
5972 ctx
->block_padding
= 0;
5973 } else if (app_block_size
<= SSL3_RT_MAX_PLAIN_LENGTH
) {
5974 ctx
->block_padding
= app_block_size
;
5978 if (hs_block_size
== 1) {
5979 ctx
->hs_padding
= 0;
5980 } else if (hs_block_size
<= SSL3_RT_MAX_PLAIN_LENGTH
) {
5981 ctx
->hs_padding
= hs_block_size
;
5988 int SSL_CTX_set_block_padding(SSL_CTX
*ctx
, size_t block_size
)
5990 return SSL_CTX_set_block_padding_ex(ctx
, block_size
, block_size
);
5993 int SSL_set_record_padding_callback(SSL
*ssl
,
5994 size_t (*cb
) (SSL
*ssl
, int type
,
5995 size_t len
, void *arg
))
5998 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(ssl
);
6003 b
= SSL_get_wbio(ssl
);
6004 if (b
== NULL
|| !BIO_get_ktls_send(b
)) {
6005 sc
->rlayer
.record_padding_cb
= cb
;
6011 void SSL_set_record_padding_callback_arg(SSL
*ssl
, void *arg
)
6013 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
6018 sc
->rlayer
.record_padding_arg
= arg
;
6021 void *SSL_get_record_padding_callback_arg(const SSL
*ssl
)
6023 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(ssl
);
6028 return sc
->rlayer
.record_padding_arg
;
6031 int SSL_set_block_padding_ex(SSL
*ssl
, size_t app_block_size
,
6032 size_t hs_block_size
)
6034 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
6038 && (app_block_size
> 1 || hs_block_size
> 1)))
6041 /* block size of 0 or 1 is basically no padding */
6042 if (app_block_size
== 1) {
6043 sc
->rlayer
.block_padding
= 0;
6044 } else if (app_block_size
<= SSL3_RT_MAX_PLAIN_LENGTH
) {
6045 sc
->rlayer
.block_padding
= app_block_size
;
6049 if (hs_block_size
== 1) {
6050 sc
->rlayer
.hs_padding
= 0;
6051 } else if (hs_block_size
<= SSL3_RT_MAX_PLAIN_LENGTH
) {
6052 sc
->rlayer
.hs_padding
= hs_block_size
;
6059 int SSL_set_block_padding(SSL
*ssl
, size_t block_size
)
6061 return SSL_set_block_padding_ex(ssl
, block_size
, block_size
);
6064 int SSL_set_num_tickets(SSL
*s
, size_t num_tickets
)
6066 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6071 sc
->num_tickets
= num_tickets
;
6076 size_t SSL_get_num_tickets(const SSL
*s
)
6078 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
6083 return sc
->num_tickets
;
6086 int SSL_CTX_set_num_tickets(SSL_CTX
*ctx
, size_t num_tickets
)
6088 ctx
->num_tickets
= num_tickets
;
6093 size_t SSL_CTX_get_num_tickets(const SSL_CTX
*ctx
)
6095 return ctx
->num_tickets
;
6098 /* Retrieve handshake hashes */
6099 int ssl_handshake_hash(SSL_CONNECTION
*s
,
6100 unsigned char *out
, size_t outlen
,
6103 EVP_MD_CTX
*ctx
= NULL
;
6104 EVP_MD_CTX
*hdgst
= s
->s3
.handshake_dgst
;
6105 int hashleni
= EVP_MD_CTX_get_size(hdgst
);
6108 if (hashleni
< 0 || (size_t)hashleni
> outlen
) {
6109 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
6113 ctx
= EVP_MD_CTX_new();
6115 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
6119 if (!EVP_MD_CTX_copy_ex(ctx
, hdgst
)
6120 || EVP_DigestFinal_ex(ctx
, out
, NULL
) <= 0) {
6121 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
6125 *hashlen
= hashleni
;
6129 EVP_MD_CTX_free(ctx
);
6133 int SSL_session_reused(const SSL
*s
)
6135 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
6143 int SSL_is_server(const SSL
*s
)
6145 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
6153 #ifndef OPENSSL_NO_DEPRECATED_1_1_0
6154 void SSL_set_debug(SSL
*s
, int debug
)
6156 /* Old function was do-nothing anyway... */
6162 void SSL_set_security_level(SSL
*s
, int level
)
6164 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6169 sc
->cert
->sec_level
= level
;
6172 int SSL_get_security_level(const SSL
*s
)
6174 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
6179 return sc
->cert
->sec_level
;
6182 void SSL_set_security_callback(SSL
*s
,
6183 int (*cb
) (const SSL
*s
, const SSL_CTX
*ctx
,
6184 int op
, int bits
, int nid
,
6185 void *other
, void *ex
))
6187 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6192 sc
->cert
->sec_cb
= cb
;
6195 int (*SSL_get_security_callback(const SSL
*s
)) (const SSL
*s
,
6196 const SSL_CTX
*ctx
, int op
,
6197 int bits
, int nid
, void *other
,
6199 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
6204 return sc
->cert
->sec_cb
;
6207 void SSL_set0_security_ex_data(SSL
*s
, void *ex
)
6209 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6214 sc
->cert
->sec_ex
= ex
;
6217 void *SSL_get0_security_ex_data(const SSL
*s
)
6219 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
6224 return sc
->cert
->sec_ex
;
6227 void SSL_CTX_set_security_level(SSL_CTX
*ctx
, int level
)
6229 ctx
->cert
->sec_level
= level
;
6232 int SSL_CTX_get_security_level(const SSL_CTX
*ctx
)
6234 return ctx
->cert
->sec_level
;
6237 void SSL_CTX_set_security_callback(SSL_CTX
*ctx
,
6238 int (*cb
) (const SSL
*s
, const SSL_CTX
*ctx
,
6239 int op
, int bits
, int nid
,
6240 void *other
, void *ex
))
6242 ctx
->cert
->sec_cb
= cb
;
6245 int (*SSL_CTX_get_security_callback(const SSL_CTX
*ctx
)) (const SSL
*s
,
6251 return ctx
->cert
->sec_cb
;
6254 void SSL_CTX_set0_security_ex_data(SSL_CTX
*ctx
, void *ex
)
6256 ctx
->cert
->sec_ex
= ex
;
6259 void *SSL_CTX_get0_security_ex_data(const SSL_CTX
*ctx
)
6261 return ctx
->cert
->sec_ex
;
6264 uint64_t SSL_CTX_get_options(const SSL_CTX
*ctx
)
6266 return ctx
->options
;
6269 uint64_t SSL_get_options(const SSL
*s
)
6271 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
6273 #ifndef OPENSSL_NO_QUIC
6275 return ossl_quic_get_options(s
);
6284 uint64_t SSL_CTX_set_options(SSL_CTX
*ctx
, uint64_t op
)
6286 return ctx
->options
|= op
;
6289 uint64_t SSL_set_options(SSL
*s
, uint64_t op
)
6292 OSSL_PARAM options
[2], *opts
= options
;
6294 #ifndef OPENSSL_NO_QUIC
6296 return ossl_quic_set_options(s
, op
);
6299 sc
= SSL_CONNECTION_FROM_SSL(s
);
6305 *opts
++ = OSSL_PARAM_construct_uint64(OSSL_LIBSSL_RECORD_LAYER_PARAM_OPTIONS
,
6307 *opts
= OSSL_PARAM_construct_end();
6309 /* Ignore return value */
6310 sc
->rlayer
.rrlmethod
->set_options(sc
->rlayer
.rrl
, options
);
6311 sc
->rlayer
.wrlmethod
->set_options(sc
->rlayer
.wrl
, options
);
6316 uint64_t SSL_CTX_clear_options(SSL_CTX
*ctx
, uint64_t op
)
6318 return ctx
->options
&= ~op
;
6321 uint64_t SSL_clear_options(SSL
*s
, uint64_t op
)
6323 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6324 OSSL_PARAM options
[2], *opts
= options
;
6326 #ifndef OPENSSL_NO_QUIC
6328 return ossl_quic_clear_options(s
, op
);
6336 *opts
++ = OSSL_PARAM_construct_uint64(OSSL_LIBSSL_RECORD_LAYER_PARAM_OPTIONS
,
6338 *opts
= OSSL_PARAM_construct_end();
6340 /* Ignore return value */
6341 sc
->rlayer
.rrlmethod
->set_options(sc
->rlayer
.rrl
, options
);
6342 sc
->rlayer
.wrlmethod
->set_options(sc
->rlayer
.wrl
, options
);
6347 STACK_OF(X509
) *SSL_get0_verified_chain(const SSL
*s
)
6349 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
6354 return sc
->verified_chain
;
6357 IMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER
, SSL_CIPHER
, ssl_cipher_id
);
6359 #ifndef OPENSSL_NO_CT
6362 * Moves SCTs from the |src| stack to the |dst| stack.
6363 * The source of each SCT will be set to |origin|.
6364 * If |dst| points to a NULL pointer, a new stack will be created and owned by
6366 * Returns the number of SCTs moved, or a negative integer if an error occurs.
6367 * The |dst| stack is created and possibly partially populated even in case
6368 * of error, likewise the |src| stack may be left in an intermediate state.
6370 static int ct_move_scts(STACK_OF(SCT
) **dst
, STACK_OF(SCT
) *src
,
6371 sct_source_t origin
)
6377 *dst
= sk_SCT_new_null();
6379 ERR_raise(ERR_LIB_SSL
, ERR_R_CRYPTO_LIB
);
6384 while ((sct
= sk_SCT_pop(src
)) != NULL
) {
6385 if (SCT_set_source(sct
, origin
) != 1)
6388 if (!sk_SCT_push(*dst
, sct
))
6400 * Look for data collected during ServerHello and parse if found.
6401 * Returns the number of SCTs extracted.
6403 static int ct_extract_tls_extension_scts(SSL_CONNECTION
*s
)
6405 int scts_extracted
= 0;
6407 if (s
->ext
.scts
!= NULL
) {
6408 const unsigned char *p
= s
->ext
.scts
;
6409 STACK_OF(SCT
) *scts
= o2i_SCT_LIST(NULL
, &p
, s
->ext
.scts_len
);
6411 scts_extracted
= ct_move_scts(&s
->scts
, scts
, SCT_SOURCE_TLS_EXTENSION
);
6413 SCT_LIST_free(scts
);
6416 return scts_extracted
;
6420 * Checks for an OCSP response and then attempts to extract any SCTs found if it
6421 * contains an SCT X509 extension. They will be stored in |s->scts|.
6423 * - The number of SCTs extracted, assuming an OCSP response exists.
6424 * - 0 if no OCSP response exists or it contains no SCTs.
6425 * - A negative integer if an error occurs.
6427 static int ct_extract_ocsp_response_scts(SSL_CONNECTION
*s
)
6429 # ifndef OPENSSL_NO_OCSP
6430 int scts_extracted
= 0;
6431 const unsigned char *p
;
6432 OCSP_BASICRESP
*br
= NULL
;
6433 OCSP_RESPONSE
*rsp
= NULL
;
6434 STACK_OF(SCT
) *scts
= NULL
;
6437 if (s
->ext
.ocsp
.resp
== NULL
|| s
->ext
.ocsp
.resp_len
== 0)
6440 p
= s
->ext
.ocsp
.resp
;
6441 rsp
= d2i_OCSP_RESPONSE(NULL
, &p
, (int)s
->ext
.ocsp
.resp_len
);
6445 br
= OCSP_response_get1_basic(rsp
);
6449 for (i
= 0; i
< OCSP_resp_count(br
); ++i
) {
6450 OCSP_SINGLERESP
*single
= OCSP_resp_get0(br
, i
);
6456 OCSP_SINGLERESP_get1_ext_d2i(single
, NID_ct_cert_scts
, NULL
, NULL
);
6458 ct_move_scts(&s
->scts
, scts
, SCT_SOURCE_OCSP_STAPLED_RESPONSE
);
6459 if (scts_extracted
< 0)
6463 SCT_LIST_free(scts
);
6464 OCSP_BASICRESP_free(br
);
6465 OCSP_RESPONSE_free(rsp
);
6466 return scts_extracted
;
6468 /* Behave as if no OCSP response exists */
6474 * Attempts to extract SCTs from the peer certificate.
6475 * Return the number of SCTs extracted, or a negative integer if an error
6478 static int ct_extract_x509v3_extension_scts(SSL_CONNECTION
*s
)
6480 int scts_extracted
= 0;
6481 X509
*cert
= s
->session
!= NULL
? s
->session
->peer
: NULL
;
6484 STACK_OF(SCT
) *scts
=
6485 X509_get_ext_d2i(cert
, NID_ct_precert_scts
, NULL
, NULL
);
6488 ct_move_scts(&s
->scts
, scts
, SCT_SOURCE_X509V3_EXTENSION
);
6490 SCT_LIST_free(scts
);
6493 return scts_extracted
;
6497 * Attempts to find all received SCTs by checking TLS extensions, the OCSP
6498 * response (if it exists) and X509v3 extensions in the certificate.
6499 * Returns NULL if an error occurs.
6501 const STACK_OF(SCT
) *SSL_get0_peer_scts(SSL
*s
)
6503 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6508 if (!sc
->scts_parsed
) {
6509 if (ct_extract_tls_extension_scts(sc
) < 0 ||
6510 ct_extract_ocsp_response_scts(sc
) < 0 ||
6511 ct_extract_x509v3_extension_scts(sc
) < 0)
6514 sc
->scts_parsed
= 1;
6521 static int ct_permissive(const CT_POLICY_EVAL_CTX
*ctx
,
6522 const STACK_OF(SCT
) *scts
, void *unused_arg
)
6527 static int ct_strict(const CT_POLICY_EVAL_CTX
*ctx
,
6528 const STACK_OF(SCT
) *scts
, void *unused_arg
)
6530 int count
= scts
!= NULL
? sk_SCT_num(scts
) : 0;
6533 for (i
= 0; i
< count
; ++i
) {
6534 SCT
*sct
= sk_SCT_value(scts
, i
);
6535 int status
= SCT_get_validation_status(sct
);
6537 if (status
== SCT_VALIDATION_STATUS_VALID
)
6540 ERR_raise(ERR_LIB_SSL
, SSL_R_NO_VALID_SCTS
);
6544 int SSL_set_ct_validation_callback(SSL
*s
, ssl_ct_validation_cb callback
,
6547 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6553 * Since code exists that uses the custom extension handler for CT, look
6554 * for this and throw an error if they have already registered to use CT.
6556 if (callback
!= NULL
&& SSL_CTX_has_client_custom_ext(s
->ctx
,
6557 TLSEXT_TYPE_signed_certificate_timestamp
))
6559 ERR_raise(ERR_LIB_SSL
, SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED
);
6563 if (callback
!= NULL
) {
6565 * If we are validating CT, then we MUST accept SCTs served via OCSP
6567 if (!SSL_set_tlsext_status_type(s
, TLSEXT_STATUSTYPE_ocsp
))
6571 sc
->ct_validation_callback
= callback
;
6572 sc
->ct_validation_callback_arg
= arg
;
6577 int SSL_CTX_set_ct_validation_callback(SSL_CTX
*ctx
,
6578 ssl_ct_validation_cb callback
, void *arg
)
6581 * Since code exists that uses the custom extension handler for CT, look for
6582 * this and throw an error if they have already registered to use CT.
6584 if (callback
!= NULL
&& SSL_CTX_has_client_custom_ext(ctx
,
6585 TLSEXT_TYPE_signed_certificate_timestamp
))
6587 ERR_raise(ERR_LIB_SSL
, SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED
);
6591 ctx
->ct_validation_callback
= callback
;
6592 ctx
->ct_validation_callback_arg
= arg
;
6596 int SSL_ct_is_enabled(const SSL
*s
)
6598 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
6603 return sc
->ct_validation_callback
!= NULL
;
6606 int SSL_CTX_ct_is_enabled(const SSL_CTX
*ctx
)
6608 return ctx
->ct_validation_callback
!= NULL
;
6611 int ssl_validate_ct(SSL_CONNECTION
*s
)
6614 X509
*cert
= s
->session
!= NULL
? s
->session
->peer
: NULL
;
6616 SSL_DANE
*dane
= &s
->dane
;
6617 CT_POLICY_EVAL_CTX
*ctx
= NULL
;
6618 const STACK_OF(SCT
) *scts
;
6621 * If no callback is set, the peer is anonymous, or its chain is invalid,
6622 * skip SCT validation - just return success. Applications that continue
6623 * handshakes without certificates, with unverified chains, or pinned leaf
6624 * certificates are outside the scope of the WebPKI and CT.
6626 * The above exclusions notwithstanding the vast majority of peers will
6627 * have rather ordinary certificate chains validated by typical
6628 * applications that perform certificate verification and therefore will
6629 * process SCTs when enabled.
6631 if (s
->ct_validation_callback
== NULL
|| cert
== NULL
||
6632 s
->verify_result
!= X509_V_OK
||
6633 s
->verified_chain
== NULL
|| sk_X509_num(s
->verified_chain
) <= 1)
6637 * CT not applicable for chains validated via DANE-TA(2) or DANE-EE(3)
6638 * trust-anchors. See https://tools.ietf.org/html/rfc7671#section-4.2
6640 if (DANETLS_ENABLED(dane
) && dane
->mtlsa
!= NULL
) {
6641 switch (dane
->mtlsa
->usage
) {
6642 case DANETLS_USAGE_DANE_TA
:
6643 case DANETLS_USAGE_DANE_EE
:
6648 ctx
= CT_POLICY_EVAL_CTX_new_ex(SSL_CONNECTION_GET_CTX(s
)->libctx
,
6649 SSL_CONNECTION_GET_CTX(s
)->propq
);
6651 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_CT_LIB
);
6655 issuer
= sk_X509_value(s
->verified_chain
, 1);
6656 CT_POLICY_EVAL_CTX_set1_cert(ctx
, cert
);
6657 CT_POLICY_EVAL_CTX_set1_issuer(ctx
, issuer
);
6658 CT_POLICY_EVAL_CTX_set_shared_CTLOG_STORE(ctx
,
6659 SSL_CONNECTION_GET_CTX(s
)->ctlog_store
);
6660 CT_POLICY_EVAL_CTX_set_time(
6661 ctx
, (uint64_t)SSL_SESSION_get_time_ex(s
->session
) * 1000);
6663 scts
= SSL_get0_peer_scts(SSL_CONNECTION_GET_SSL(s
));
6666 * This function returns success (> 0) only when all the SCTs are valid, 0
6667 * when some are invalid, and < 0 on various internal errors (out of
6668 * memory, etc.). Having some, or even all, invalid SCTs is not sufficient
6669 * reason to abort the handshake, that decision is up to the callback.
6670 * Therefore, we error out only in the unexpected case that the return
6671 * value is negative.
6673 * XXX: One might well argue that the return value of this function is an
6674 * unfortunate design choice. Its job is only to determine the validation
6675 * status of each of the provided SCTs. So long as it correctly separates
6676 * the wheat from the chaff it should return success. Failure in this case
6677 * ought to correspond to an inability to carry out its duties.
6679 if (SCT_LIST_validate(scts
, ctx
) < 0) {
6680 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_SCT_VERIFICATION_FAILED
);
6684 ret
= s
->ct_validation_callback(ctx
, scts
, s
->ct_validation_callback_arg
);
6686 ret
= 0; /* This function returns 0 on failure */
6688 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_CALLBACK_FAILED
);
6691 CT_POLICY_EVAL_CTX_free(ctx
);
6693 * With SSL_VERIFY_NONE the session may be cached and reused despite a
6694 * failure return code here. Also the application may wish the complete
6695 * the handshake, and then disconnect cleanly at a higher layer, after
6696 * checking the verification status of the completed connection.
6698 * We therefore force a certificate verification failure which will be
6699 * visible via SSL_get_verify_result() and cached as part of any resumed
6702 * Note: the permissive callback is for information gathering only, always
6703 * returns success, and does not affect verification status. Only the
6704 * strict callback or a custom application-specified callback can trigger
6705 * connection failure or record a verification error.
6708 s
->verify_result
= X509_V_ERR_NO_VALID_SCTS
;
6712 int SSL_CTX_enable_ct(SSL_CTX
*ctx
, int validation_mode
)
6714 switch (validation_mode
) {
6716 ERR_raise(ERR_LIB_SSL
, SSL_R_INVALID_CT_VALIDATION_TYPE
);
6718 case SSL_CT_VALIDATION_PERMISSIVE
:
6719 return SSL_CTX_set_ct_validation_callback(ctx
, ct_permissive
, NULL
);
6720 case SSL_CT_VALIDATION_STRICT
:
6721 return SSL_CTX_set_ct_validation_callback(ctx
, ct_strict
, NULL
);
6725 int SSL_enable_ct(SSL
*s
, int validation_mode
)
6727 switch (validation_mode
) {
6729 ERR_raise(ERR_LIB_SSL
, SSL_R_INVALID_CT_VALIDATION_TYPE
);
6731 case SSL_CT_VALIDATION_PERMISSIVE
:
6732 return SSL_set_ct_validation_callback(s
, ct_permissive
, NULL
);
6733 case SSL_CT_VALIDATION_STRICT
:
6734 return SSL_set_ct_validation_callback(s
, ct_strict
, NULL
);
6738 int SSL_CTX_set_default_ctlog_list_file(SSL_CTX
*ctx
)
6740 return CTLOG_STORE_load_default_file(ctx
->ctlog_store
);
6743 int SSL_CTX_set_ctlog_list_file(SSL_CTX
*ctx
, const char *path
)
6745 return CTLOG_STORE_load_file(ctx
->ctlog_store
, path
);
6748 void SSL_CTX_set0_ctlog_store(SSL_CTX
*ctx
, CTLOG_STORE
*logs
)
6750 CTLOG_STORE_free(ctx
->ctlog_store
);
6751 ctx
->ctlog_store
= logs
;
6754 const CTLOG_STORE
*SSL_CTX_get0_ctlog_store(const SSL_CTX
*ctx
)
6756 return ctx
->ctlog_store
;
6759 #endif /* OPENSSL_NO_CT */
6761 void SSL_CTX_set_client_hello_cb(SSL_CTX
*c
, SSL_client_hello_cb_fn cb
,
6764 c
->client_hello_cb
= cb
;
6765 c
->client_hello_cb_arg
= arg
;
6768 void SSL_CTX_set_new_pending_conn_cb(SSL_CTX
*c
, SSL_new_pending_conn_cb_fn cb
,
6771 c
->new_pending_conn_cb
= cb
;
6772 c
->new_pending_conn_arg
= arg
;
6775 int SSL_client_hello_isv2(SSL
*s
)
6777 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6782 if (sc
->clienthello
== NULL
)
6784 return sc
->clienthello
->isv2
;
6787 unsigned int SSL_client_hello_get0_legacy_version(SSL
*s
)
6789 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6794 if (sc
->clienthello
== NULL
)
6796 return sc
->clienthello
->legacy_version
;
6799 size_t SSL_client_hello_get0_random(SSL
*s
, const unsigned char **out
)
6801 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6806 if (sc
->clienthello
== NULL
)
6809 *out
= sc
->clienthello
->random
;
6810 return SSL3_RANDOM_SIZE
;
6813 size_t SSL_client_hello_get0_session_id(SSL
*s
, const unsigned char **out
)
6815 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6820 if (sc
->clienthello
== NULL
)
6823 *out
= sc
->clienthello
->session_id
;
6824 return sc
->clienthello
->session_id_len
;
6827 size_t SSL_client_hello_get0_ciphers(SSL
*s
, const unsigned char **out
)
6829 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6834 if (sc
->clienthello
== NULL
)
6837 *out
= PACKET_data(&sc
->clienthello
->ciphersuites
);
6838 return PACKET_remaining(&sc
->clienthello
->ciphersuites
);
6841 size_t SSL_client_hello_get0_compression_methods(SSL
*s
, const unsigned char **out
)
6843 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6848 if (sc
->clienthello
== NULL
)
6851 *out
= sc
->clienthello
->compressions
;
6852 return sc
->clienthello
->compressions_len
;
6855 int SSL_client_hello_get1_extensions_present(SSL
*s
, int **out
, size_t *outlen
)
6860 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6865 if (sc
->clienthello
== NULL
|| out
== NULL
|| outlen
== NULL
)
6867 for (i
= 0; i
< sc
->clienthello
->pre_proc_exts_len
; i
++) {
6868 ext
= sc
->clienthello
->pre_proc_exts
+ i
;
6877 if ((present
= OPENSSL_malloc(sizeof(*present
) * num
)) == NULL
)
6879 for (i
= 0; i
< sc
->clienthello
->pre_proc_exts_len
; i
++) {
6880 ext
= sc
->clienthello
->pre_proc_exts
+ i
;
6882 if (ext
->received_order
>= num
)
6884 present
[ext
->received_order
] = ext
->type
;
6891 OPENSSL_free(present
);
6895 int SSL_client_hello_get_extension_order(SSL
*s
, uint16_t *exts
, size_t *num_exts
)
6899 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6904 if (sc
->clienthello
== NULL
|| num_exts
== NULL
)
6906 for (i
= 0; i
< sc
->clienthello
->pre_proc_exts_len
; i
++) {
6907 ext
= sc
->clienthello
->pre_proc_exts
+ i
;
6919 if (*num_exts
< num
)
6921 for (i
= 0; i
< sc
->clienthello
->pre_proc_exts_len
; i
++) {
6922 ext
= sc
->clienthello
->pre_proc_exts
+ i
;
6924 if (ext
->received_order
>= num
)
6926 exts
[ext
->received_order
] = ext
->type
;
6933 int SSL_client_hello_get0_ext(SSL
*s
, unsigned int type
, const unsigned char **out
,
6938 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6943 if (sc
->clienthello
== NULL
)
6945 for (i
= 0; i
< sc
->clienthello
->pre_proc_exts_len
; ++i
) {
6946 r
= sc
->clienthello
->pre_proc_exts
+ i
;
6947 if (r
->present
&& r
->type
== type
) {
6949 *out
= PACKET_data(&r
->data
);
6951 *outlen
= PACKET_remaining(&r
->data
);
6958 int SSL_free_buffers(SSL
*ssl
)
6961 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(ssl
);
6968 return rl
->rrlmethod
->free_buffers(rl
->rrl
)
6969 && rl
->wrlmethod
->free_buffers(rl
->wrl
);
6972 int SSL_alloc_buffers(SSL
*ssl
)
6975 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
6980 /* QUIC always has buffers allocated. */
6986 return rl
->rrlmethod
->alloc_buffers(rl
->rrl
)
6987 && rl
->wrlmethod
->alloc_buffers(rl
->wrl
);
6990 void SSL_CTX_set_keylog_callback(SSL_CTX
*ctx
, SSL_CTX_keylog_cb_func cb
)
6992 ctx
->keylog_callback
= cb
;
6995 SSL_CTX_keylog_cb_func
SSL_CTX_get_keylog_callback(const SSL_CTX
*ctx
)
6997 return ctx
->keylog_callback
;
7000 static int nss_keylog_int(const char *prefix
,
7002 const uint8_t *parameter_1
,
7003 size_t parameter_1_len
,
7004 const uint8_t *parameter_2
,
7005 size_t parameter_2_len
)
7008 char *cursor
= NULL
;
7009 size_t out_len
= 0, i
, prefix_len
;
7010 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(sc
);
7012 #ifndef OPENSSL_NO_SSLKEYLOG
7013 if (sctx
->keylog_callback
== NULL
&& sctx
->do_sslkeylog
== 0)
7016 if (sctx
->keylog_callback
== NULL
)
7021 * Our output buffer will contain the following strings, rendered with
7022 * space characters in between, terminated by a NULL character: first the
7023 * prefix, then the first parameter, then the second parameter. The
7024 * meaning of each parameter depends on the specific key material being
7025 * logged. Note that the first and second parameters are encoded in
7026 * hexadecimal, so we need a buffer that is twice their lengths.
7028 prefix_len
= strlen(prefix
);
7029 out_len
= prefix_len
+ (2 * parameter_1_len
) + (2 * parameter_2_len
) + 3;
7030 if ((out
= cursor
= OPENSSL_malloc(out_len
)) == NULL
)
7033 memcpy(cursor
, prefix
, prefix_len
);
7034 cursor
+= prefix_len
;
7037 for (i
= 0; i
< parameter_1_len
; ++i
)
7038 cursor
+= ossl_to_lowerhex(cursor
, parameter_1
[i
]);
7041 for (i
= 0; i
< parameter_2_len
; ++i
)
7042 cursor
+= ossl_to_lowerhex(cursor
, parameter_2
[i
]);
7045 #ifndef OPENSSL_NO_SSLKEYLOG
7046 if (sctx
->do_sslkeylog
== 1)
7047 do_sslkeylogfile(SSL_CONNECTION_GET_SSL(sc
), (const char *)out
);
7049 if (sctx
->keylog_callback
!= NULL
)
7050 sctx
->keylog_callback(SSL_CONNECTION_GET_USER_SSL(sc
), (const char *)out
);
7051 OPENSSL_clear_free(out
, out_len
);
7055 int ssl_log_rsa_client_key_exchange(SSL_CONNECTION
*sc
,
7056 const uint8_t *encrypted_premaster
,
7057 size_t encrypted_premaster_len
,
7058 const uint8_t *premaster
,
7059 size_t premaster_len
)
7061 if (encrypted_premaster_len
< 8) {
7062 SSLfatal(sc
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
7066 /* We only want the first 8 bytes of the encrypted premaster as a tag. */
7067 return nss_keylog_int("RSA",
7069 encrypted_premaster
,
7075 int ssl_log_secret(SSL_CONNECTION
*sc
,
7077 const uint8_t *secret
,
7080 return nss_keylog_int(label
,
7082 sc
->s3
.client_random
,
7088 #define SSLV2_CIPHER_LEN 3
7090 int ssl_cache_cipherlist(SSL_CONNECTION
*s
, PACKET
*cipher_suites
, int sslv2format
)
7094 n
= sslv2format
? SSLV2_CIPHER_LEN
: TLS_CIPHER_LEN
;
7096 if (PACKET_remaining(cipher_suites
) == 0) {
7097 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_NO_CIPHERS_SPECIFIED
);
7101 if (PACKET_remaining(cipher_suites
) % n
!= 0) {
7102 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST
);
7106 OPENSSL_free(s
->s3
.tmp
.ciphers_raw
);
7107 s
->s3
.tmp
.ciphers_raw
= NULL
;
7108 s
->s3
.tmp
.ciphers_rawlen
= 0;
7111 size_t numciphers
= PACKET_remaining(cipher_suites
) / n
;
7112 PACKET sslv2ciphers
= *cipher_suites
;
7113 unsigned int leadbyte
;
7117 * We store the raw ciphers list in SSLv3+ format so we need to do some
7118 * preprocessing to convert the list first. If there are any SSLv2 only
7119 * ciphersuites with a non-zero leading byte then we are going to
7120 * slightly over allocate because we won't store those. But that isn't a
7123 raw
= OPENSSL_malloc(numciphers
* TLS_CIPHER_LEN
);
7124 s
->s3
.tmp
.ciphers_raw
= raw
;
7126 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_CRYPTO_LIB
);
7129 for (s
->s3
.tmp
.ciphers_rawlen
= 0;
7130 PACKET_remaining(&sslv2ciphers
) > 0;
7131 raw
+= TLS_CIPHER_LEN
) {
7132 if (!PACKET_get_1(&sslv2ciphers
, &leadbyte
)
7134 && !PACKET_copy_bytes(&sslv2ciphers
, raw
,
7137 && !PACKET_forward(&sslv2ciphers
, TLS_CIPHER_LEN
))) {
7138 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_PACKET
);
7139 OPENSSL_free(s
->s3
.tmp
.ciphers_raw
);
7140 s
->s3
.tmp
.ciphers_raw
= NULL
;
7141 s
->s3
.tmp
.ciphers_rawlen
= 0;
7145 s
->s3
.tmp
.ciphers_rawlen
+= TLS_CIPHER_LEN
;
7147 } else if (!PACKET_memdup(cipher_suites
, &s
->s3
.tmp
.ciphers_raw
,
7148 &s
->s3
.tmp
.ciphers_rawlen
)) {
7149 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
7155 int SSL_bytes_to_cipher_list(SSL
*s
, const unsigned char *bytes
, size_t len
,
7156 int isv2format
, STACK_OF(SSL_CIPHER
) **sk
,
7157 STACK_OF(SSL_CIPHER
) **scsvs
)
7160 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
7165 if (!PACKET_buf_init(&pkt
, bytes
, len
))
7167 return ossl_bytes_to_cipher_list(sc
, &pkt
, sk
, scsvs
, isv2format
, 0);
7170 int ossl_bytes_to_cipher_list(SSL_CONNECTION
*s
, PACKET
*cipher_suites
,
7171 STACK_OF(SSL_CIPHER
) **skp
,
7172 STACK_OF(SSL_CIPHER
) **scsvs_out
,
7173 int sslv2format
, int fatal
)
7175 const SSL_CIPHER
*c
;
7176 STACK_OF(SSL_CIPHER
) *sk
= NULL
;
7177 STACK_OF(SSL_CIPHER
) *scsvs
= NULL
;
7179 /* 3 = SSLV2_CIPHER_LEN > TLS_CIPHER_LEN = 2. */
7180 unsigned char cipher
[SSLV2_CIPHER_LEN
];
7182 n
= sslv2format
? SSLV2_CIPHER_LEN
: TLS_CIPHER_LEN
;
7184 if (PACKET_remaining(cipher_suites
) == 0) {
7186 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_NO_CIPHERS_SPECIFIED
);
7188 ERR_raise(ERR_LIB_SSL
, SSL_R_NO_CIPHERS_SPECIFIED
);
7192 if (PACKET_remaining(cipher_suites
) % n
!= 0) {
7194 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
7195 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST
);
7197 ERR_raise(ERR_LIB_SSL
, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST
);
7201 sk
= sk_SSL_CIPHER_new_null();
7202 scsvs
= sk_SSL_CIPHER_new_null();
7203 if (sk
== NULL
|| scsvs
== NULL
) {
7205 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_CRYPTO_LIB
);
7207 ERR_raise(ERR_LIB_SSL
, ERR_R_CRYPTO_LIB
);
7211 while (PACKET_copy_bytes(cipher_suites
, cipher
, n
)) {
7213 * SSLv3 ciphers wrapped in an SSLv2-compatible ClientHello have the
7214 * first byte set to zero, while true SSLv2 ciphers have a non-zero
7215 * first byte. We don't support any true SSLv2 ciphers, so skip them.
7217 if (sslv2format
&& cipher
[0] != '\0')
7220 /* For SSLv2-compat, ignore leading 0-byte. */
7221 c
= ssl_get_cipher_by_char(s
, sslv2format
? &cipher
[1] : cipher
, 1);
7223 if ((c
->valid
&& !sk_SSL_CIPHER_push(sk
, c
)) ||
7224 (!c
->valid
&& !sk_SSL_CIPHER_push(scsvs
, c
))) {
7226 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_CRYPTO_LIB
);
7228 ERR_raise(ERR_LIB_SSL
, ERR_R_CRYPTO_LIB
);
7233 if (PACKET_remaining(cipher_suites
) > 0) {
7235 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_LENGTH
);
7237 ERR_raise(ERR_LIB_SSL
, SSL_R_BAD_LENGTH
);
7244 sk_SSL_CIPHER_free(sk
);
7245 if (scsvs_out
!= NULL
)
7248 sk_SSL_CIPHER_free(scsvs
);
7251 sk_SSL_CIPHER_free(sk
);
7252 sk_SSL_CIPHER_free(scsvs
);
7256 int SSL_CTX_set_max_early_data(SSL_CTX
*ctx
, uint32_t max_early_data
)
7258 ctx
->max_early_data
= max_early_data
;
7263 uint32_t SSL_CTX_get_max_early_data(const SSL_CTX
*ctx
)
7265 return ctx
->max_early_data
;
7268 int SSL_set_max_early_data(SSL
*s
, uint32_t max_early_data
)
7270 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(s
);
7275 sc
->max_early_data
= max_early_data
;
7280 uint32_t SSL_get_max_early_data(const SSL
*s
)
7282 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
7287 return sc
->max_early_data
;
7290 int SSL_CTX_set_recv_max_early_data(SSL_CTX
*ctx
, uint32_t recv_max_early_data
)
7292 ctx
->recv_max_early_data
= recv_max_early_data
;
7297 uint32_t SSL_CTX_get_recv_max_early_data(const SSL_CTX
*ctx
)
7299 return ctx
->recv_max_early_data
;
7302 int SSL_set_recv_max_early_data(SSL
*s
, uint32_t recv_max_early_data
)
7304 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(s
);
7309 sc
->recv_max_early_data
= recv_max_early_data
;
7314 uint32_t SSL_get_recv_max_early_data(const SSL
*s
)
7316 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
7321 return sc
->recv_max_early_data
;
7324 __owur
unsigned int ssl_get_max_send_fragment(const SSL_CONNECTION
*sc
)
7326 /* Return any active Max Fragment Len extension */
7327 if (sc
->session
!= NULL
&& USE_MAX_FRAGMENT_LENGTH_EXT(sc
->session
))
7328 return GET_MAX_FRAGMENT_LENGTH(sc
->session
);
7330 /* return current SSL connection setting */
7331 return (unsigned int)sc
->max_send_fragment
;
7334 __owur
unsigned int ssl_get_split_send_fragment(const SSL_CONNECTION
*sc
)
7336 /* Return a value regarding an active Max Fragment Len extension */
7337 if (sc
->session
!= NULL
&& USE_MAX_FRAGMENT_LENGTH_EXT(sc
->session
)
7338 && sc
->split_send_fragment
> GET_MAX_FRAGMENT_LENGTH(sc
->session
))
7339 return GET_MAX_FRAGMENT_LENGTH(sc
->session
);
7341 /* else limit |split_send_fragment| to current |max_send_fragment| */
7342 if (sc
->split_send_fragment
> sc
->max_send_fragment
)
7343 return (unsigned int)sc
->max_send_fragment
;
7345 /* return current SSL connection setting */
7346 return (unsigned int)sc
->split_send_fragment
;
7349 int SSL_stateless(SSL
*s
)
7352 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(s
);
7357 /* Ensure there is no state left over from a previous invocation */
7363 sc
->s3
.flags
|= TLS1_FLAGS_STATELESS
;
7364 ret
= SSL_accept(s
);
7365 sc
->s3
.flags
&= ~TLS1_FLAGS_STATELESS
;
7367 if (ret
> 0 && sc
->ext
.cookieok
)
7370 if (sc
->hello_retry_request
== SSL_HRR_PENDING
&& !ossl_statem_in_error(sc
))
7376 void SSL_CTX_set_post_handshake_auth(SSL_CTX
*ctx
, int val
)
7378 ctx
->pha_enabled
= val
;
7381 void SSL_set_post_handshake_auth(SSL
*ssl
, int val
)
7383 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(ssl
);
7388 sc
->pha_enabled
= val
;
7391 int SSL_verify_client_post_handshake(SSL
*ssl
)
7393 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
7395 #ifndef OPENSSL_NO_QUIC
7397 ERR_raise(ERR_LIB_SSL
, SSL_R_WRONG_SSL_VERSION
);
7405 if (!SSL_CONNECTION_IS_TLS13(sc
)) {
7406 ERR_raise(ERR_LIB_SSL
, SSL_R_WRONG_SSL_VERSION
);
7410 ERR_raise(ERR_LIB_SSL
, SSL_R_NOT_SERVER
);
7414 if (!SSL_is_init_finished(ssl
)) {
7415 ERR_raise(ERR_LIB_SSL
, SSL_R_STILL_IN_INIT
);
7419 switch (sc
->post_handshake_auth
) {
7421 ERR_raise(ERR_LIB_SSL
, SSL_R_EXTENSION_NOT_RECEIVED
);
7424 case SSL_PHA_EXT_SENT
:
7425 ERR_raise(ERR_LIB_SSL
, ERR_R_INTERNAL_ERROR
);
7427 case SSL_PHA_EXT_RECEIVED
:
7429 case SSL_PHA_REQUEST_PENDING
:
7430 ERR_raise(ERR_LIB_SSL
, SSL_R_REQUEST_PENDING
);
7432 case SSL_PHA_REQUESTED
:
7433 ERR_raise(ERR_LIB_SSL
, SSL_R_REQUEST_SENT
);
7437 sc
->post_handshake_auth
= SSL_PHA_REQUEST_PENDING
;
7439 /* checks verify_mode and algorithm_auth */
7440 if (!send_certificate_request(sc
)) {
7441 sc
->post_handshake_auth
= SSL_PHA_EXT_RECEIVED
; /* restore on error */
7442 ERR_raise(ERR_LIB_SSL
, SSL_R_INVALID_CONFIG
);
7446 ossl_statem_set_in_init(sc
, 1);
7450 int SSL_CTX_set_session_ticket_cb(SSL_CTX
*ctx
,
7451 SSL_CTX_generate_session_ticket_fn gen_cb
,
7452 SSL_CTX_decrypt_session_ticket_fn dec_cb
,
7455 ctx
->generate_ticket_cb
= gen_cb
;
7456 ctx
->decrypt_ticket_cb
= dec_cb
;
7457 ctx
->ticket_cb_data
= arg
;
7461 void SSL_CTX_set_allow_early_data_cb(SSL_CTX
*ctx
,
7462 SSL_allow_early_data_cb_fn cb
,
7465 ctx
->allow_early_data_cb
= cb
;
7466 ctx
->allow_early_data_cb_data
= arg
;
7469 void SSL_set_allow_early_data_cb(SSL
*s
,
7470 SSL_allow_early_data_cb_fn cb
,
7473 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(s
);
7478 sc
->allow_early_data_cb
= cb
;
7479 sc
->allow_early_data_cb_data
= arg
;
7482 const EVP_CIPHER
*ssl_evp_cipher_fetch(OSSL_LIB_CTX
*libctx
,
7484 const char *properties
)
7486 const EVP_CIPHER
*ciph
;
7488 ciph
= tls_get_cipher_from_engine(nid
);
7493 * If there is no engine cipher then we do an explicit fetch. This may fail
7494 * and that could be ok
7497 ciph
= EVP_CIPHER_fetch(libctx
, OBJ_nid2sn(nid
), properties
);
7499 OSSL_PARAM params
[2];
7500 int decrypt_only
= 0;
7502 params
[0] = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_DECRYPT_ONLY
,
7504 params
[1] = OSSL_PARAM_construct_end();
7505 if (EVP_CIPHER_get_params((EVP_CIPHER
*)ciph
, params
)
7507 /* If a cipher is decrypt-only, it is unusable */
7508 EVP_CIPHER_free((EVP_CIPHER
*)ciph
);
7517 int ssl_evp_cipher_up_ref(const EVP_CIPHER
*cipher
)
7519 /* Don't up-ref an implicit EVP_CIPHER */
7520 if (EVP_CIPHER_get0_provider(cipher
) == NULL
)
7524 * The cipher was explicitly fetched and therefore it is safe to cast
7527 return EVP_CIPHER_up_ref((EVP_CIPHER
*)cipher
);
7530 void ssl_evp_cipher_free(const EVP_CIPHER
*cipher
)
7535 if (EVP_CIPHER_get0_provider(cipher
) != NULL
) {
7537 * The cipher was explicitly fetched and therefore it is safe to cast
7540 EVP_CIPHER_free((EVP_CIPHER
*)cipher
);
7544 const EVP_MD
*ssl_evp_md_fetch(OSSL_LIB_CTX
*libctx
,
7546 const char *properties
)
7550 md
= tls_get_digest_from_engine(nid
);
7554 /* Otherwise we do an explicit fetch */
7556 md
= EVP_MD_fetch(libctx
, OBJ_nid2sn(nid
), properties
);
7561 int ssl_evp_md_up_ref(const EVP_MD
*md
)
7563 /* Don't up-ref an implicit EVP_MD */
7564 if (EVP_MD_get0_provider(md
) == NULL
)
7568 * The digest was explicitly fetched and therefore it is safe to cast
7571 return EVP_MD_up_ref((EVP_MD
*)md
);
7574 void ssl_evp_md_free(const EVP_MD
*md
)
7579 if (EVP_MD_get0_provider(md
) != NULL
) {
7581 * The digest was explicitly fetched and therefore it is safe to cast
7584 EVP_MD_free((EVP_MD
*)md
);
7588 int SSL_set0_tmp_dh_pkey(SSL
*s
, EVP_PKEY
*dhpkey
)
7590 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
7595 if (!ssl_security(sc
, SSL_SECOP_TMP_DH
,
7596 EVP_PKEY_get_security_bits(dhpkey
), 0, dhpkey
)) {
7597 ERR_raise(ERR_LIB_SSL
, SSL_R_DH_KEY_TOO_SMALL
);
7600 EVP_PKEY_free(sc
->cert
->dh_tmp
);
7601 sc
->cert
->dh_tmp
= dhpkey
;
7605 int SSL_CTX_set0_tmp_dh_pkey(SSL_CTX
*ctx
, EVP_PKEY
*dhpkey
)
7607 if (!ssl_ctx_security(ctx
, SSL_SECOP_TMP_DH
,
7608 EVP_PKEY_get_security_bits(dhpkey
), 0, dhpkey
)) {
7609 ERR_raise(ERR_LIB_SSL
, SSL_R_DH_KEY_TOO_SMALL
);
7612 EVP_PKEY_free(ctx
->cert
->dh_tmp
);
7613 ctx
->cert
->dh_tmp
= dhpkey
;
7617 /* QUIC-specific methods which are supported on QUIC connections only. */
7618 int SSL_handle_events(SSL
*s
)
7622 #ifndef OPENSSL_NO_QUIC
7624 return ossl_quic_handle_events(s
);
7627 sc
= SSL_CONNECTION_FROM_SSL_ONLY(s
);
7628 if (sc
!= NULL
&& SSL_CONNECTION_IS_DTLS(sc
))
7630 * DTLSv1_handle_timeout returns 0 if the timer wasn't expired yet,
7631 * which we consider a success case. Theoretically DTLSv1_handle_timeout
7632 * can also return 0 if s is NULL or not a DTLS object, but we've
7633 * already ruled out those possibilities above, so this is not possible
7634 * here. Thus the only failure cases are where DTLSv1_handle_timeout
7637 return DTLSv1_handle_timeout(s
) >= 0;
7642 int SSL_get_event_timeout(SSL
*s
, struct timeval
*tv
, int *is_infinite
)
7646 #ifndef OPENSSL_NO_QUIC
7648 return ossl_quic_get_event_timeout(s
, tv
, is_infinite
);
7651 sc
= SSL_CONNECTION_FROM_SSL_ONLY(s
);
7652 if (sc
!= NULL
&& SSL_CONNECTION_IS_DTLS(sc
)
7653 && DTLSv1_get_timeout(s
, tv
)) {
7658 tv
->tv_sec
= 1000000;
7664 int SSL_get_rpoll_descriptor(SSL
*s
, BIO_POLL_DESCRIPTOR
*desc
)
7666 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
7668 #ifndef OPENSSL_NO_QUIC
7670 return ossl_quic_get_rpoll_descriptor(s
, desc
);
7673 if (sc
== NULL
|| sc
->rbio
== NULL
)
7676 return BIO_get_rpoll_descriptor(sc
->rbio
, desc
);
7679 int SSL_get_wpoll_descriptor(SSL
*s
, BIO_POLL_DESCRIPTOR
*desc
)
7681 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
7683 #ifndef OPENSSL_NO_QUIC
7685 return ossl_quic_get_wpoll_descriptor(s
, desc
);
7688 if (sc
== NULL
|| sc
->wbio
== NULL
)
7691 return BIO_get_wpoll_descriptor(sc
->wbio
, desc
);
7694 int SSL_net_read_desired(SSL
*s
)
7696 #ifndef OPENSSL_NO_QUIC
7698 return SSL_want_read(s
);
7700 return ossl_quic_get_net_read_desired(s
);
7702 return SSL_want_read(s
);
7706 int SSL_net_write_desired(SSL
*s
)
7708 #ifndef OPENSSL_NO_QUIC
7710 return SSL_want_write(s
);
7712 return ossl_quic_get_net_write_desired(s
);
7714 return SSL_want_write(s
);
7718 int SSL_set_blocking_mode(SSL
*s
, int blocking
)
7720 #ifndef OPENSSL_NO_QUIC
7724 return ossl_quic_conn_set_blocking_mode(s
, blocking
);
7730 int SSL_get_blocking_mode(SSL
*s
)
7732 #ifndef OPENSSL_NO_QUIC
7736 return ossl_quic_conn_get_blocking_mode(s
);
7742 int SSL_set1_initial_peer_addr(SSL
*s
, const BIO_ADDR
*peer_addr
)
7744 #ifndef OPENSSL_NO_QUIC
7748 return ossl_quic_conn_set_initial_peer_addr(s
, peer_addr
);
7754 int SSL_shutdown_ex(SSL
*ssl
, uint64_t flags
,
7755 const SSL_SHUTDOWN_EX_ARGS
*args
,
7758 #ifndef OPENSSL_NO_QUIC
7760 return SSL_shutdown(ssl
);
7762 return ossl_quic_conn_shutdown(ssl
, flags
, args
, args_len
);
7764 return SSL_shutdown(ssl
);
7768 int SSL_stream_conclude(SSL
*ssl
, uint64_t flags
)
7770 #ifndef OPENSSL_NO_QUIC
7774 return ossl_quic_conn_stream_conclude(ssl
);
7780 SSL
*SSL_new_stream(SSL
*s
, uint64_t flags
)
7782 #ifndef OPENSSL_NO_QUIC
7786 return ossl_quic_conn_stream_new(s
, flags
);
7792 SSL
*SSL_get0_connection(SSL
*s
)
7794 #ifndef OPENSSL_NO_QUIC
7798 return ossl_quic_get0_connection(s
);
7804 int SSL_is_connection(SSL
*s
)
7806 return SSL_get0_connection(s
) == s
;
7809 SSL
*SSL_get0_listener(SSL
*s
)
7811 #ifndef OPENSSL_NO_QUIC
7815 return ossl_quic_get0_listener(s
);
7821 SSL
*SSL_get0_domain(SSL
*s
)
7823 #ifndef OPENSSL_NO_QUIC
7827 return ossl_quic_get0_domain(s
);
7833 int SSL_is_listener(SSL
*s
)
7835 return SSL_get0_listener(s
) == s
;
7838 int SSL_is_domain(SSL
*s
)
7840 return SSL_get0_domain(s
) == s
;
7843 int SSL_get_stream_type(SSL
*s
)
7845 #ifndef OPENSSL_NO_QUIC
7847 return SSL_STREAM_TYPE_BIDI
;
7849 return ossl_quic_get_stream_type(s
);
7851 return SSL_STREAM_TYPE_BIDI
;
7855 uint64_t SSL_get_stream_id(SSL
*s
)
7857 #ifndef OPENSSL_NO_QUIC
7861 return ossl_quic_get_stream_id(s
);
7867 int SSL_is_stream_local(SSL
*s
)
7869 #ifndef OPENSSL_NO_QUIC
7873 return ossl_quic_is_stream_local(s
);
7879 int SSL_set_default_stream_mode(SSL
*s
, uint32_t mode
)
7881 #ifndef OPENSSL_NO_QUIC
7885 return ossl_quic_set_default_stream_mode(s
, mode
);
7891 int SSL_set_incoming_stream_policy(SSL
*s
, int policy
, uint64_t aec
)
7893 #ifndef OPENSSL_NO_QUIC
7897 return ossl_quic_set_incoming_stream_policy(s
, policy
, aec
);
7903 SSL
*SSL_accept_stream(SSL
*s
, uint64_t flags
)
7905 #ifndef OPENSSL_NO_QUIC
7909 return ossl_quic_accept_stream(s
, flags
);
7915 size_t SSL_get_accept_stream_queue_len(SSL
*s
)
7917 #ifndef OPENSSL_NO_QUIC
7921 return ossl_quic_get_accept_stream_queue_len(s
);
7927 int SSL_stream_reset(SSL
*s
,
7928 const SSL_STREAM_RESET_ARGS
*args
,
7931 #ifndef OPENSSL_NO_QUIC
7935 return ossl_quic_stream_reset(s
, args
, args_len
);
7941 int SSL_get_stream_read_state(SSL
*s
)
7943 #ifndef OPENSSL_NO_QUIC
7945 return SSL_STREAM_STATE_NONE
;
7947 return ossl_quic_get_stream_read_state(s
);
7949 return SSL_STREAM_STATE_NONE
;
7953 int SSL_get_stream_write_state(SSL
*s
)
7955 #ifndef OPENSSL_NO_QUIC
7957 return SSL_STREAM_STATE_NONE
;
7959 return ossl_quic_get_stream_write_state(s
);
7961 return SSL_STREAM_STATE_NONE
;
7965 int SSL_get_stream_read_error_code(SSL
*s
, uint64_t *app_error_code
)
7967 #ifndef OPENSSL_NO_QUIC
7971 return ossl_quic_get_stream_read_error_code(s
, app_error_code
);
7977 int SSL_get_stream_write_error_code(SSL
*s
, uint64_t *app_error_code
)
7979 #ifndef OPENSSL_NO_QUIC
7983 return ossl_quic_get_stream_write_error_code(s
, app_error_code
);
7989 int SSL_get_conn_close_info(SSL
*s
, SSL_CONN_CLOSE_INFO
*info
,
7992 #ifndef OPENSSL_NO_QUIC
7996 return ossl_quic_get_conn_close_info(s
, info
, info_len
);
8002 int SSL_get_value_uint(SSL
*s
, uint32_t class_
, uint32_t id
,
8005 #ifndef OPENSSL_NO_QUIC
8007 return ossl_quic_get_value_uint(s
, class_
, id
, value
);
8010 ERR_raise(ERR_LIB_SSL
, SSL_R_UNSUPPORTED_PROTOCOL
);
8014 int SSL_set_value_uint(SSL
*s
, uint32_t class_
, uint32_t id
,
8017 #ifndef OPENSSL_NO_QUIC
8019 return ossl_quic_set_value_uint(s
, class_
, id
, value
);
8022 ERR_raise(ERR_LIB_SSL
, SSL_R_UNSUPPORTED_PROTOCOL
);
8026 SSL
*SSL_new_listener(SSL_CTX
*ctx
, uint64_t flags
)
8028 #ifndef OPENSSL_NO_QUIC
8029 if (!IS_QUIC_CTX(ctx
))
8032 return ossl_quic_new_listener(ctx
, flags
);
8038 SSL
*SSL_new_listener_from(SSL
*ssl
, uint64_t flags
)
8040 #ifndef OPENSSL_NO_QUIC
8044 return ossl_quic_new_listener_from(ssl
, flags
);
8050 SSL
*SSL_new_from_listener(SSL
*ssl
, uint64_t flags
)
8052 #ifndef OPENSSL_NO_QUIC
8056 return ossl_quic_new_from_listener(ssl
, flags
);
8062 SSL
*SSL_accept_connection(SSL
*ssl
, uint64_t flags
)
8064 #ifndef OPENSSL_NO_QUIC
8068 return ossl_quic_accept_connection(ssl
, flags
);
8074 size_t SSL_get_accept_connection_queue_len(SSL
*ssl
)
8076 #ifndef OPENSSL_NO_QUIC
8080 return ossl_quic_get_accept_connection_queue_len(ssl
);
8086 int SSL_listen(SSL
*ssl
)
8088 #ifndef OPENSSL_NO_QUIC
8092 return ossl_quic_listen(ssl
);
8098 SSL
*SSL_new_domain(SSL_CTX
*ctx
, uint64_t flags
)
8100 #ifndef OPENSSL_NO_QUIC
8101 if (!IS_QUIC_CTX(ctx
))
8104 return ossl_quic_new_domain(ctx
, flags
);
8110 int ossl_adjust_domain_flags(uint64_t domain_flags
, uint64_t *p_domain_flags
)
8112 if ((domain_flags
& ~OSSL_QUIC_SUPPORTED_DOMAIN_FLAGS
) != 0) {
8113 ERR_raise_data(ERR_LIB_SSL
, ERR_R_UNSUPPORTED
,
8114 "unsupported domain flag requested");
8118 if ((domain_flags
& SSL_DOMAIN_FLAG_THREAD_ASSISTED
) != 0)
8119 domain_flags
|= SSL_DOMAIN_FLAG_MULTI_THREAD
;
8121 if ((domain_flags
& (SSL_DOMAIN_FLAG_MULTI_THREAD
8122 | SSL_DOMAIN_FLAG_SINGLE_THREAD
)) == 0)
8123 domain_flags
|= SSL_DOMAIN_FLAG_MULTI_THREAD
;
8125 if ((domain_flags
& SSL_DOMAIN_FLAG_SINGLE_THREAD
) != 0
8126 && (domain_flags
& SSL_DOMAIN_FLAG_MULTI_THREAD
) != 0) {
8127 ERR_raise_data(ERR_LIB_SSL
, ERR_R_PASSED_INVALID_ARGUMENT
,
8128 "mutually exclusive domain flags specified");
8133 * Note: We treat MULTI_THREAD as a no-op in non-threaded builds, but
8134 * not THREAD_ASSISTED.
8136 # ifndef OPENSSL_THREADS
8137 if ((domain_flags
& SSL_DOMAIN_FLAG_THREAD_ASSISTED
) != 0) {
8138 ERR_raise_data(ERR_LIB_SSL
, ERR_R_UNSUPPORTED
,
8139 "thread assisted mode not available in this build");
8144 *p_domain_flags
= domain_flags
;
8148 int SSL_CTX_set_domain_flags(SSL_CTX
*ctx
, uint64_t domain_flags
)
8150 #ifndef OPENSSL_NO_QUIC
8151 if (IS_QUIC_CTX(ctx
)) {
8152 if (!ossl_adjust_domain_flags(domain_flags
, &domain_flags
))
8155 ctx
->domain_flags
= domain_flags
;
8160 ERR_raise_data(ERR_LIB_SSL
, ERR_R_UNSUPPORTED
,
8161 "domain flags unsupported on this kind of SSL_CTX");
8165 int SSL_CTX_get_domain_flags(const SSL_CTX
*ctx
, uint64_t *domain_flags
)
8167 #ifndef OPENSSL_NO_QUIC
8168 if (IS_QUIC_CTX(ctx
)) {
8169 if (domain_flags
!= NULL
)
8170 *domain_flags
= ctx
->domain_flags
;
8176 ERR_raise_data(ERR_LIB_SSL
, ERR_R_UNSUPPORTED
,
8177 "domain flags unsupported on this kind of SSL_CTX");
8181 int SSL_get_domain_flags(const SSL
*ssl
, uint64_t *domain_flags
)
8183 #ifndef OPENSSL_NO_QUIC
8185 return ossl_quic_get_domain_flags(ssl
, domain_flags
);
8191 int SSL_add_expected_rpk(SSL
*s
, EVP_PKEY
*rpk
)
8193 unsigned char *data
= NULL
;
8194 SSL_DANE
*dane
= SSL_get0_dane(s
);
8197 if (dane
== NULL
|| dane
->dctx
== NULL
)
8199 if ((ret
= i2d_PUBKEY(rpk
, &data
)) <= 0)
8202 ret
= SSL_dane_tlsa_add(s
, DANETLS_USAGE_DANE_EE
,
8203 DANETLS_SELECTOR_SPKI
,
8204 DANETLS_MATCHING_FULL
,
8205 data
, (size_t)ret
) > 0;
8210 EVP_PKEY
*SSL_get0_peer_rpk(const SSL
*s
)
8212 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
8214 if (sc
== NULL
|| sc
->session
== NULL
)
8216 return sc
->session
->peer_rpk
;
8219 int SSL_get_negotiated_client_cert_type(const SSL
*s
)
8221 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
8226 return sc
->ext
.client_cert_type
;
8229 int SSL_get_negotiated_server_cert_type(const SSL
*s
)
8231 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
8236 return sc
->ext
.server_cert_type
;
8239 static int validate_cert_type(const unsigned char *val
, size_t len
)
8245 if (val
== NULL
&& len
== 0)
8248 if (val
== NULL
|| len
== 0)
8251 for (i
= 0; i
< len
; i
++) {
8253 case TLSEXT_cert_type_rpk
:
8258 case TLSEXT_cert_type_x509
:
8263 case TLSEXT_cert_type_pgp
:
8264 case TLSEXT_cert_type_1609dot2
:
8272 static int set_cert_type(unsigned char **cert_type
,
8273 size_t *cert_type_len
,
8274 const unsigned char *val
,
8277 unsigned char *tmp
= NULL
;
8279 if (!validate_cert_type(val
, len
))
8282 if (val
!= NULL
&& (tmp
= OPENSSL_memdup(val
, len
)) == NULL
)
8285 OPENSSL_free(*cert_type
);
8287 *cert_type_len
= len
;
8291 int SSL_set1_client_cert_type(SSL
*s
, const unsigned char *val
, size_t len
)
8293 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
8298 return set_cert_type(&sc
->client_cert_type
, &sc
->client_cert_type_len
,
8302 int SSL_set1_server_cert_type(SSL
*s
, const unsigned char *val
, size_t len
)
8304 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
8309 return set_cert_type(&sc
->server_cert_type
, &sc
->server_cert_type_len
,
8313 int SSL_CTX_set1_client_cert_type(SSL_CTX
*ctx
, const unsigned char *val
, size_t len
)
8315 return set_cert_type(&ctx
->client_cert_type
, &ctx
->client_cert_type_len
,
8319 int SSL_CTX_set1_server_cert_type(SSL_CTX
*ctx
, const unsigned char *val
, size_t len
)
8321 return set_cert_type(&ctx
->server_cert_type
, &ctx
->server_cert_type_len
,
8325 int SSL_get0_client_cert_type(const SSL
*s
, unsigned char **t
, size_t *len
)
8327 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
8329 if (t
== NULL
|| len
== NULL
|| sc
== NULL
)
8332 *t
= sc
->client_cert_type
;
8333 *len
= sc
->client_cert_type_len
;
8337 int SSL_get0_server_cert_type(const SSL
*s
, unsigned char **t
, size_t *len
)
8339 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
8341 if (t
== NULL
|| len
== NULL
|| sc
== NULL
)
8344 *t
= sc
->server_cert_type
;
8345 *len
= sc
->server_cert_type_len
;
8349 int SSL_CTX_get0_client_cert_type(const SSL_CTX
*ctx
, unsigned char **t
, size_t *len
)
8351 if (t
== NULL
|| len
== NULL
)
8354 *t
= ctx
->client_cert_type
;
8355 *len
= ctx
->client_cert_type_len
;
8359 int SSL_CTX_get0_server_cert_type(const SSL_CTX
*ctx
, unsigned char **t
, size_t *len
)
8361 if (t
== NULL
|| len
== NULL
)
8364 *t
= ctx
->server_cert_type
;
8365 *len
= ctx
->server_cert_type_len
;