2 * Copyright 1995-2022 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
13 #include "ssl_local.h"
14 #include "internal/e_os.h"
15 #include <openssl/objects.h>
16 #include <openssl/x509v3.h>
17 #include <openssl/rand.h>
18 #include <openssl/ocsp.h>
19 #include <openssl/dh.h>
20 #include <openssl/engine.h>
21 #include <openssl/async.h>
22 #include <openssl/ct.h>
23 #include <openssl/trace.h>
24 #include "internal/cryptlib.h"
25 #include "internal/refcount.h"
26 #include "internal/ktls.h"
28 static int ssl_undefined_function_1(SSL_CONNECTION
*sc
, SSL3_RECORD
*r
, size_t s
,
29 int t
, SSL_MAC_BUF
*mac
, size_t macsize
)
31 return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc
));
34 static int ssl_undefined_function_2(SSL_CONNECTION
*sc
, SSL3_RECORD
*r
,
35 unsigned char *s
, int t
)
37 return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc
));
40 static int ssl_undefined_function_3(SSL_CONNECTION
*sc
, unsigned char *r
,
41 unsigned char *s
, size_t t
, size_t *u
)
43 return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc
));
46 static int ssl_undefined_function_4(SSL_CONNECTION
*sc
, int r
)
48 return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc
));
51 static size_t ssl_undefined_function_5(SSL_CONNECTION
*sc
, const char *r
,
52 size_t s
, unsigned char *t
)
54 return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc
));
57 static int ssl_undefined_function_6(int r
)
59 return ssl_undefined_function(NULL
);
62 static int ssl_undefined_function_7(SSL_CONNECTION
*sc
, unsigned char *r
,
63 size_t s
, const char *t
, size_t u
,
64 const unsigned char *v
, size_t w
, int x
)
66 return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc
));
69 static int ssl_undefined_function_8(SSL_CONNECTION
*sc
)
71 return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc
));
74 SSL3_ENC_METHOD ssl3_undef_enc_method
= {
75 ssl_undefined_function_1
,
76 ssl_undefined_function_2
,
77 ssl_undefined_function_8
,
78 ssl_undefined_function_3
,
79 ssl_undefined_function_4
,
80 ssl_undefined_function_5
,
81 NULL
, /* client_finished_label */
82 0, /* client_finished_label_len */
83 NULL
, /* server_finished_label */
84 0, /* server_finished_label_len */
85 ssl_undefined_function_6
,
86 ssl_undefined_function_7
,
89 struct ssl_async_args
{
93 enum { READFUNC
, WRITEFUNC
, OTHERFUNC
} type
;
95 int (*func_read
) (SSL
*, void *, size_t, size_t *);
96 int (*func_write
) (SSL
*, const void *, size_t, size_t *);
97 int (*func_other
) (SSL
*);
101 static const struct {
107 DANETLS_MATCHING_FULL
, 0, NID_undef
110 DANETLS_MATCHING_2256
, 1, NID_sha256
113 DANETLS_MATCHING_2512
, 2, NID_sha512
117 static int dane_ctx_enable(struct dane_ctx_st
*dctx
)
119 const EVP_MD
**mdevp
;
121 uint8_t mdmax
= DANETLS_MATCHING_LAST
;
122 int n
= ((int)mdmax
) + 1; /* int to handle PrivMatch(255) */
125 if (dctx
->mdevp
!= NULL
)
128 mdevp
= OPENSSL_zalloc(n
* sizeof(*mdevp
));
129 mdord
= OPENSSL_zalloc(n
* sizeof(*mdord
));
131 if (mdord
== NULL
|| mdevp
== NULL
) {
134 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
138 /* Install default entries */
139 for (i
= 0; i
< OSSL_NELEM(dane_mds
); ++i
) {
142 if (dane_mds
[i
].nid
== NID_undef
||
143 (md
= EVP_get_digestbynid(dane_mds
[i
].nid
)) == NULL
)
145 mdevp
[dane_mds
[i
].mtype
] = md
;
146 mdord
[dane_mds
[i
].mtype
] = dane_mds
[i
].ord
;
156 static void dane_ctx_final(struct dane_ctx_st
*dctx
)
158 OPENSSL_free(dctx
->mdevp
);
161 OPENSSL_free(dctx
->mdord
);
166 static void tlsa_free(danetls_record
*t
)
170 OPENSSL_free(t
->data
);
171 EVP_PKEY_free(t
->spki
);
175 static void dane_final(SSL_DANE
*dane
)
177 sk_danetls_record_pop_free(dane
->trecs
, tlsa_free
);
180 OSSL_STACK_OF_X509_free(dane
->certs
);
183 X509_free(dane
->mcert
);
191 * dane_copy - Copy dane configuration, sans verification state.
193 static int ssl_dane_dup(SSL_CONNECTION
*to
, SSL_CONNECTION
*from
)
198 if (!DANETLS_ENABLED(&from
->dane
))
201 num
= sk_danetls_record_num(from
->dane
.trecs
);
202 dane_final(&to
->dane
);
203 to
->dane
.flags
= from
->dane
.flags
;
204 to
->dane
.dctx
= &SSL_CONNECTION_GET_CTX(to
)->dane
;
205 to
->dane
.trecs
= sk_danetls_record_new_reserve(NULL
, num
);
207 if (to
->dane
.trecs
== NULL
) {
208 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
212 for (i
= 0; i
< num
; ++i
) {
213 danetls_record
*t
= sk_danetls_record_value(from
->dane
.trecs
, i
);
215 if (SSL_dane_tlsa_add(SSL_CONNECTION_GET_SSL(to
), t
->usage
,
216 t
->selector
, t
->mtype
, t
->data
, t
->dlen
) <= 0)
222 static int dane_mtype_set(struct dane_ctx_st
*dctx
,
223 const EVP_MD
*md
, uint8_t mtype
, uint8_t ord
)
227 if (mtype
== DANETLS_MATCHING_FULL
&& md
!= NULL
) {
228 ERR_raise(ERR_LIB_SSL
, SSL_R_DANE_CANNOT_OVERRIDE_MTYPE_FULL
);
232 if (mtype
> dctx
->mdmax
) {
233 const EVP_MD
**mdevp
;
235 int n
= ((int)mtype
) + 1;
237 mdevp
= OPENSSL_realloc(dctx
->mdevp
, n
* sizeof(*mdevp
));
239 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
244 mdord
= OPENSSL_realloc(dctx
->mdord
, n
* sizeof(*mdord
));
246 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
251 /* Zero-fill any gaps */
252 for (i
= dctx
->mdmax
+ 1; i
< mtype
; ++i
) {
260 dctx
->mdevp
[mtype
] = md
;
261 /* Coerce ordinal of disabled matching types to 0 */
262 dctx
->mdord
[mtype
] = (md
== NULL
) ? 0 : ord
;
267 static const EVP_MD
*tlsa_md_get(SSL_DANE
*dane
, uint8_t mtype
)
269 if (mtype
> dane
->dctx
->mdmax
)
271 return dane
->dctx
->mdevp
[mtype
];
274 static int dane_tlsa_add(SSL_DANE
*dane
,
277 uint8_t mtype
, const unsigned char *data
, size_t dlen
)
280 const EVP_MD
*md
= NULL
;
281 int ilen
= (int)dlen
;
285 if (dane
->trecs
== NULL
) {
286 ERR_raise(ERR_LIB_SSL
, SSL_R_DANE_NOT_ENABLED
);
290 if (ilen
< 0 || dlen
!= (size_t)ilen
) {
291 ERR_raise(ERR_LIB_SSL
, SSL_R_DANE_TLSA_BAD_DATA_LENGTH
);
295 if (usage
> DANETLS_USAGE_LAST
) {
296 ERR_raise(ERR_LIB_SSL
, SSL_R_DANE_TLSA_BAD_CERTIFICATE_USAGE
);
300 if (selector
> DANETLS_SELECTOR_LAST
) {
301 ERR_raise(ERR_LIB_SSL
, SSL_R_DANE_TLSA_BAD_SELECTOR
);
305 if (mtype
!= DANETLS_MATCHING_FULL
) {
306 md
= tlsa_md_get(dane
, mtype
);
308 ERR_raise(ERR_LIB_SSL
, SSL_R_DANE_TLSA_BAD_MATCHING_TYPE
);
313 if (md
!= NULL
&& dlen
!= (size_t)EVP_MD_get_size(md
)) {
314 ERR_raise(ERR_LIB_SSL
, SSL_R_DANE_TLSA_BAD_DIGEST_LENGTH
);
318 ERR_raise(ERR_LIB_SSL
, SSL_R_DANE_TLSA_NULL_DATA
);
322 if ((t
= OPENSSL_zalloc(sizeof(*t
))) == NULL
) {
323 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
328 t
->selector
= selector
;
330 t
->data
= OPENSSL_malloc(dlen
);
331 if (t
->data
== NULL
) {
333 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
336 memcpy(t
->data
, data
, dlen
);
339 /* Validate and cache full certificate or public key */
340 if (mtype
== DANETLS_MATCHING_FULL
) {
341 const unsigned char *p
= data
;
343 EVP_PKEY
*pkey
= NULL
;
346 case DANETLS_SELECTOR_CERT
:
347 if (!d2i_X509(&cert
, &p
, ilen
) || p
< data
||
348 dlen
!= (size_t)(p
- data
)) {
350 ERR_raise(ERR_LIB_SSL
, SSL_R_DANE_TLSA_BAD_CERTIFICATE
);
353 if (X509_get0_pubkey(cert
) == NULL
) {
355 ERR_raise(ERR_LIB_SSL
, SSL_R_DANE_TLSA_BAD_CERTIFICATE
);
359 if ((DANETLS_USAGE_BIT(usage
) & DANETLS_TA_MASK
) == 0) {
365 * For usage DANE-TA(2), we support authentication via "2 0 0" TLSA
366 * records that contain full certificates of trust-anchors that are
367 * not present in the wire chain. For usage PKIX-TA(0), we augment
368 * the chain with untrusted Full(0) certificates from DNS, in case
369 * they are missing from the chain.
371 if ((dane
->certs
== NULL
&&
372 (dane
->certs
= sk_X509_new_null()) == NULL
) ||
373 !sk_X509_push(dane
->certs
, cert
)) {
374 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
381 case DANETLS_SELECTOR_SPKI
:
382 if (!d2i_PUBKEY(&pkey
, &p
, ilen
) || p
< data
||
383 dlen
!= (size_t)(p
- data
)) {
385 ERR_raise(ERR_LIB_SSL
, SSL_R_DANE_TLSA_BAD_PUBLIC_KEY
);
390 * For usage DANE-TA(2), we support authentication via "2 1 0" TLSA
391 * records that contain full bare keys of trust-anchors that are
392 * not present in the wire chain.
394 if (usage
== DANETLS_USAGE_DANE_TA
)
403 * Find the right insertion point for the new record.
405 * See crypto/x509/x509_vfy.c. We sort DANE-EE(3) records first, so that
406 * they can be processed first, as they require no chain building, and no
407 * expiration or hostname checks. Because DANE-EE(3) is numerically
408 * largest, this is accomplished via descending sort by "usage".
410 * We also sort in descending order by matching ordinal to simplify
411 * the implementation of digest agility in the verification code.
413 * The choice of order for the selector is not significant, so we
414 * use the same descending order for consistency.
416 num
= sk_danetls_record_num(dane
->trecs
);
417 for (i
= 0; i
< num
; ++i
) {
418 danetls_record
*rec
= sk_danetls_record_value(dane
->trecs
, i
);
420 if (rec
->usage
> usage
)
422 if (rec
->usage
< usage
)
424 if (rec
->selector
> selector
)
426 if (rec
->selector
< selector
)
428 if (dane
->dctx
->mdord
[rec
->mtype
] > dane
->dctx
->mdord
[mtype
])
433 if (!sk_danetls_record_insert(dane
->trecs
, t
, i
)) {
435 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
438 dane
->umask
|= DANETLS_USAGE_BIT(usage
);
444 * Return 0 if there is only one version configured and it was disabled
445 * at configure time. Return 1 otherwise.
447 static int ssl_check_allowed_versions(int min_version
, int max_version
)
449 int minisdtls
= 0, maxisdtls
= 0;
451 /* Figure out if we're doing DTLS versions or TLS versions */
452 if (min_version
== DTLS1_BAD_VER
453 || min_version
>> 8 == DTLS1_VERSION_MAJOR
)
455 if (max_version
== DTLS1_BAD_VER
456 || max_version
>> 8 == DTLS1_VERSION_MAJOR
)
458 /* A wildcard version of 0 could be DTLS or TLS. */
459 if ((minisdtls
&& !maxisdtls
&& max_version
!= 0)
460 || (maxisdtls
&& !minisdtls
&& min_version
!= 0)) {
461 /* Mixing DTLS and TLS versions will lead to sadness; deny it. */
465 if (minisdtls
|| maxisdtls
) {
466 /* Do DTLS version checks. */
467 if (min_version
== 0)
468 /* Ignore DTLS1_BAD_VER */
469 min_version
= DTLS1_VERSION
;
470 if (max_version
== 0)
471 max_version
= DTLS1_2_VERSION
;
472 #ifdef OPENSSL_NO_DTLS1_2
473 if (max_version
== DTLS1_2_VERSION
)
474 max_version
= DTLS1_VERSION
;
476 #ifdef OPENSSL_NO_DTLS1
477 if (min_version
== DTLS1_VERSION
)
478 min_version
= DTLS1_2_VERSION
;
480 /* Done massaging versions; do the check. */
482 #ifdef OPENSSL_NO_DTLS1
483 || (DTLS_VERSION_GE(min_version
, DTLS1_VERSION
)
484 && DTLS_VERSION_GE(DTLS1_VERSION
, max_version
))
486 #ifdef OPENSSL_NO_DTLS1_2
487 || (DTLS_VERSION_GE(min_version
, DTLS1_2_VERSION
)
488 && DTLS_VERSION_GE(DTLS1_2_VERSION
, max_version
))
493 /* Regular TLS version checks. */
494 if (min_version
== 0)
495 min_version
= SSL3_VERSION
;
496 if (max_version
== 0)
497 max_version
= TLS1_3_VERSION
;
498 #ifdef OPENSSL_NO_TLS1_3
499 if (max_version
== TLS1_3_VERSION
)
500 max_version
= TLS1_2_VERSION
;
502 #ifdef OPENSSL_NO_TLS1_2
503 if (max_version
== TLS1_2_VERSION
)
504 max_version
= TLS1_1_VERSION
;
506 #ifdef OPENSSL_NO_TLS1_1
507 if (max_version
== TLS1_1_VERSION
)
508 max_version
= TLS1_VERSION
;
510 #ifdef OPENSSL_NO_TLS1
511 if (max_version
== TLS1_VERSION
)
512 max_version
= SSL3_VERSION
;
514 #ifdef OPENSSL_NO_SSL3
515 if (min_version
== SSL3_VERSION
)
516 min_version
= TLS1_VERSION
;
518 #ifdef OPENSSL_NO_TLS1
519 if (min_version
== TLS1_VERSION
)
520 min_version
= TLS1_1_VERSION
;
522 #ifdef OPENSSL_NO_TLS1_1
523 if (min_version
== TLS1_1_VERSION
)
524 min_version
= TLS1_2_VERSION
;
526 #ifdef OPENSSL_NO_TLS1_2
527 if (min_version
== TLS1_2_VERSION
)
528 min_version
= TLS1_3_VERSION
;
530 /* Done massaging versions; do the check. */
532 #ifdef OPENSSL_NO_SSL3
533 || (min_version
<= SSL3_VERSION
&& SSL3_VERSION
<= max_version
)
535 #ifdef OPENSSL_NO_TLS1
536 || (min_version
<= TLS1_VERSION
&& TLS1_VERSION
<= max_version
)
538 #ifdef OPENSSL_NO_TLS1_1
539 || (min_version
<= TLS1_1_VERSION
&& TLS1_1_VERSION
<= max_version
)
541 #ifdef OPENSSL_NO_TLS1_2
542 || (min_version
<= TLS1_2_VERSION
&& TLS1_2_VERSION
<= max_version
)
544 #ifdef OPENSSL_NO_TLS1_3
545 || (min_version
<= TLS1_3_VERSION
&& TLS1_3_VERSION
<= max_version
)
553 #if defined(__TANDEM) && defined(OPENSSL_VPROC)
555 * Define a VPROC function for HP NonStop build ssl library.
556 * This is used by platform version identification tools.
557 * Do not inline this procedure or make it static.
559 # define OPENSSL_VPROC_STRING_(x) x##_SSL
560 # define OPENSSL_VPROC_STRING(x) OPENSSL_VPROC_STRING_(x)
561 # define OPENSSL_VPROC_FUNC OPENSSL_VPROC_STRING(OPENSSL_VPROC)
562 void OPENSSL_VPROC_FUNC(void) {}
566 static void clear_ciphers(SSL_CONNECTION
*s
)
568 /* clear the current cipher */
569 ssl_clear_cipher_ctx(s
);
570 ssl_clear_hash_ctx(&s
->read_hash
);
571 ssl_clear_hash_ctx(&s
->write_hash
);
574 int SSL_clear(SSL
*s
)
576 if (s
->method
== NULL
) {
577 ERR_raise(ERR_LIB_SSL
, SSL_R_NO_METHOD_SPECIFIED
);
581 return s
->method
->ssl_reset(s
);
584 int ossl_ssl_connection_reset(SSL
*s
)
586 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
591 if (ssl_clear_bad_session(sc
)) {
592 SSL_SESSION_free(sc
->session
);
595 SSL_SESSION_free(sc
->psksession
);
596 sc
->psksession
= NULL
;
597 OPENSSL_free(sc
->psksession_id
);
598 sc
->psksession_id
= NULL
;
599 sc
->psksession_id_len
= 0;
600 sc
->hello_retry_request
= 0;
601 sc
->sent_tickets
= 0;
607 if (sc
->renegotiate
) {
608 ERR_raise(ERR_LIB_SSL
, ERR_R_INTERNAL_ERROR
);
612 ossl_statem_clear(sc
);
614 /* TODO(QUIC): Version handling not yet clear */
615 sc
->version
= s
->method
->version
;
616 sc
->client_version
= sc
->version
;
617 sc
->rwstate
= SSL_NOTHING
;
619 BUF_MEM_free(sc
->init_buf
);
622 sc
->first_packet
= 0;
624 sc
->key_update
= SSL_KEY_UPDATE_NONE
;
626 EVP_MD_CTX_free(sc
->pha_dgst
);
629 /* Reset DANE verification result state */
632 X509_free(sc
->dane
.mcert
);
633 sc
->dane
.mcert
= NULL
;
634 sc
->dane
.mtlsa
= NULL
;
636 /* Clear the verification result peername */
637 X509_VERIFY_PARAM_move_peername(sc
->param
, NULL
);
639 /* Clear any shared connection state */
640 OPENSSL_free(sc
->shared_sigalgs
);
641 sc
->shared_sigalgs
= NULL
;
642 sc
->shared_sigalgslen
= 0;
645 * Check to see if we were changed into a different method, if so, revert
648 if (s
->method
!= SSL_CONNECTION_GET_CTX(sc
)->method
) {
649 s
->method
->ssl_deinit(s
);
650 s
->method
= SSL_CONNECTION_GET_CTX(sc
)->method
;
651 if (!s
->method
->ssl_init(s
))
654 if (!s
->method
->ssl_clear(s
))
658 RECORD_LAYER_clear(&sc
->rlayer
);
661 * TODO(RECLAYER): The record method should probably initialy come from the
662 * SSL_METHOD, and potentially be updated later. For now though we just
665 if (!ssl_set_new_record_layer(sc
,
666 SSL_CONNECTION_IS_DTLS(sc
) ? &ossl_dtls_record_method
667 : &ossl_tls_record_method
,
669 OSSL_RECORD_DIRECTION_READ
,
670 OSSL_RECORD_PROTECTION_LEVEL_NONE
,
671 NULL
, 0, NULL
, 0, NULL
, 0, NULL
, 0,
672 NID_undef
, NULL
, NULL
)) {
673 /* SSLfatal already called */
680 #ifndef OPENSSL_NO_DEPRECATED_3_0
681 /** Used to change an SSL_CTXs default SSL method type */
682 int SSL_CTX_set_ssl_version(SSL_CTX
*ctx
, const SSL_METHOD
*meth
)
684 STACK_OF(SSL_CIPHER
) *sk
;
688 if (!SSL_CTX_set_ciphersuites(ctx
, OSSL_default_ciphersuites())) {
689 ERR_raise(ERR_LIB_SSL
, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS
);
692 sk
= ssl_create_cipher_list(ctx
,
693 ctx
->tls13_ciphersuites
,
695 &(ctx
->cipher_list_by_id
),
696 OSSL_default_cipher_list(), ctx
->cert
);
697 if ((sk
== NULL
) || (sk_SSL_CIPHER_num(sk
) <= 0)) {
698 ERR_raise(ERR_LIB_SSL
, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS
);
705 SSL
*SSL_new(SSL_CTX
*ctx
)
708 ERR_raise(ERR_LIB_SSL
, SSL_R_NULL_SSL_CTX
);
711 if (ctx
->method
== NULL
) {
712 ERR_raise(ERR_LIB_SSL
, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION
);
715 return ctx
->method
->ssl_new(ctx
);
718 int ossl_ssl_init(SSL
*ssl
, SSL_CTX
*ctx
, int type
)
723 ssl
->lock
= CRYPTO_THREAD_lock_new();
724 if (ssl
->lock
== NULL
)
730 ssl
->method
= ctx
->method
;
732 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL
, ssl
, &ssl
->ex_data
))
738 SSL
*ossl_ssl_connection_new(SSL_CTX
*ctx
)
743 s
= OPENSSL_zalloc(sizeof(*s
));
748 if (!ossl_ssl_init(ssl
, ctx
, SSL_TYPE_SSL_CONNECTION
)) {
754 #ifndef OPENSSL_NO_QUIC
755 /* set the parent (user visible) ssl to self */
759 RECORD_LAYER_init(&s
->rlayer
, s
);
761 s
->options
= ctx
->options
;
762 s
->dane
.flags
= ctx
->dane
.flags
;
763 s
->min_proto_version
= ctx
->min_proto_version
;
764 s
->max_proto_version
= ctx
->max_proto_version
;
766 s
->max_cert_list
= ctx
->max_cert_list
;
767 s
->max_early_data
= ctx
->max_early_data
;
768 s
->recv_max_early_data
= ctx
->recv_max_early_data
;
769 s
->num_tickets
= ctx
->num_tickets
;
770 s
->pha_enabled
= ctx
->pha_enabled
;
772 /* Shallow copy of the ciphersuites stack */
773 s
->tls13_ciphersuites
= sk_SSL_CIPHER_dup(ctx
->tls13_ciphersuites
);
774 if (s
->tls13_ciphersuites
== NULL
)
778 * Earlier library versions used to copy the pointer to the CERT, not
779 * its contents; only when setting new parameters for the per-SSL
780 * copy, ssl_cert_new would be called (and the direct reference to
781 * the per-SSL_CTX settings would be lost, but those still were
782 * indirectly accessed for various purposes, and for that reason they
783 * used to be known as s->ctx->default_cert). Now we don't look at the
784 * SSL_CTX's CERT after having duplicated it once.
786 s
->cert
= ssl_cert_dup(ctx
->cert
);
790 RECORD_LAYER_set_read_ahead(&s
->rlayer
, ctx
->read_ahead
);
791 s
->msg_callback
= ctx
->msg_callback
;
792 s
->msg_callback_arg
= ctx
->msg_callback_arg
;
793 s
->verify_mode
= ctx
->verify_mode
;
794 s
->not_resumable_session_cb
= ctx
->not_resumable_session_cb
;
795 s
->record_padding_cb
= ctx
->record_padding_cb
;
796 s
->record_padding_arg
= ctx
->record_padding_arg
;
797 s
->block_padding
= ctx
->block_padding
;
798 s
->sid_ctx_length
= ctx
->sid_ctx_length
;
799 if (!ossl_assert(s
->sid_ctx_length
<= sizeof(s
->sid_ctx
)))
801 memcpy(&s
->sid_ctx
, &ctx
->sid_ctx
, sizeof(s
->sid_ctx
));
802 s
->verify_callback
= ctx
->default_verify_callback
;
803 s
->generate_session_id
= ctx
->generate_session_id
;
805 s
->param
= X509_VERIFY_PARAM_new();
806 if (s
->param
== NULL
)
808 X509_VERIFY_PARAM_inherit(s
->param
, ctx
->param
);
809 s
->quiet_shutdown
= ctx
->quiet_shutdown
;
811 s
->ext
.max_fragment_len_mode
= ctx
->ext
.max_fragment_len_mode
;
812 s
->max_send_fragment
= ctx
->max_send_fragment
;
813 s
->split_send_fragment
= ctx
->split_send_fragment
;
814 s
->max_pipelines
= ctx
->max_pipelines
;
815 if (s
->max_pipelines
> 1)
816 RECORD_LAYER_set_read_ahead(&s
->rlayer
, 1);
817 if (ctx
->default_read_buf_len
> 0)
818 SSL_set_default_read_buffer_len(ssl
, ctx
->default_read_buf_len
);
821 s
->ext
.debug_arg
= NULL
;
822 s
->ext
.ticket_expected
= 0;
823 s
->ext
.status_type
= ctx
->ext
.status_type
;
824 s
->ext
.status_expected
= 0;
825 s
->ext
.ocsp
.ids
= NULL
;
826 s
->ext
.ocsp
.exts
= NULL
;
827 s
->ext
.ocsp
.resp
= NULL
;
828 s
->ext
.ocsp
.resp_len
= 0;
830 s
->session_ctx
= ctx
;
831 if (ctx
->ext
.ecpointformats
) {
832 s
->ext
.ecpointformats
=
833 OPENSSL_memdup(ctx
->ext
.ecpointformats
,
834 ctx
->ext
.ecpointformats_len
);
835 if (!s
->ext
.ecpointformats
) {
836 s
->ext
.ecpointformats_len
= 0;
839 s
->ext
.ecpointformats_len
=
840 ctx
->ext
.ecpointformats_len
;
842 if (ctx
->ext
.supportedgroups
) {
843 s
->ext
.supportedgroups
=
844 OPENSSL_memdup(ctx
->ext
.supportedgroups
,
845 ctx
->ext
.supportedgroups_len
846 * sizeof(*ctx
->ext
.supportedgroups
));
847 if (!s
->ext
.supportedgroups
) {
848 s
->ext
.supportedgroups_len
= 0;
851 s
->ext
.supportedgroups_len
= ctx
->ext
.supportedgroups_len
;
854 #ifndef OPENSSL_NO_NEXTPROTONEG
858 if (ctx
->ext
.alpn
!= NULL
) {
859 s
->ext
.alpn
= OPENSSL_malloc(ctx
->ext
.alpn_len
);
860 if (s
->ext
.alpn
== NULL
) {
864 memcpy(s
->ext
.alpn
, ctx
->ext
.alpn
, ctx
->ext
.alpn_len
);
865 s
->ext
.alpn_len
= ctx
->ext
.alpn_len
;
868 s
->verified_chain
= NULL
;
869 s
->verify_result
= X509_V_OK
;
871 s
->default_passwd_callback
= ctx
->default_passwd_callback
;
872 s
->default_passwd_callback_userdata
= ctx
->default_passwd_callback_userdata
;
874 s
->key_update
= SSL_KEY_UPDATE_NONE
;
876 s
->allow_early_data_cb
= ctx
->allow_early_data_cb
;
877 s
->allow_early_data_cb_data
= ctx
->allow_early_data_cb_data
;
879 if (!ssl
->method
->ssl_init(ssl
))
882 s
->server
= (ctx
->method
->ssl_accept
== ssl_undefined_function
) ? 0 : 1;
887 #ifndef OPENSSL_NO_PSK
888 s
->psk_client_callback
= ctx
->psk_client_callback
;
889 s
->psk_server_callback
= ctx
->psk_server_callback
;
891 s
->psk_find_session_cb
= ctx
->psk_find_session_cb
;
892 s
->psk_use_session_cb
= ctx
->psk_use_session_cb
;
894 s
->async_cb
= ctx
->async_cb
;
895 s
->async_cb_arg
= ctx
->async_cb_arg
;
899 #ifndef OPENSSL_NO_CT
900 if (!SSL_set_ct_validation_callback(ssl
, ctx
->ct_validation_callback
,
901 ctx
->ct_validation_callback_arg
))
908 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
912 int SSL_is_dtls(const SSL
*s
)
914 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
919 return SSL_CONNECTION_IS_DTLS(sc
) ? 1 : 0;
922 int SSL_up_ref(SSL
*s
)
926 if (CRYPTO_UP_REF(&s
->references
, &i
, s
->lock
) <= 0)
929 REF_PRINT_COUNT("SSL", s
);
930 REF_ASSERT_ISNT(i
< 2);
931 return ((i
> 1) ? 1 : 0);
934 int SSL_CTX_set_session_id_context(SSL_CTX
*ctx
, const unsigned char *sid_ctx
,
935 unsigned int sid_ctx_len
)
937 if (sid_ctx_len
> SSL_MAX_SID_CTX_LENGTH
) {
938 ERR_raise(ERR_LIB_SSL
, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG
);
941 ctx
->sid_ctx_length
= sid_ctx_len
;
942 memcpy(ctx
->sid_ctx
, sid_ctx
, sid_ctx_len
);
947 int SSL_set_session_id_context(SSL
*ssl
, const unsigned char *sid_ctx
,
948 unsigned int sid_ctx_len
)
950 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
955 if (sid_ctx_len
> SSL_MAX_SID_CTX_LENGTH
) {
956 ERR_raise(ERR_LIB_SSL
, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG
);
959 sc
->sid_ctx_length
= sid_ctx_len
;
960 memcpy(sc
->sid_ctx
, sid_ctx
, sid_ctx_len
);
965 int SSL_CTX_set_generate_session_id(SSL_CTX
*ctx
, GEN_SESSION_CB cb
)
967 if (!CRYPTO_THREAD_write_lock(ctx
->lock
))
969 ctx
->generate_session_id
= cb
;
970 CRYPTO_THREAD_unlock(ctx
->lock
);
974 int SSL_set_generate_session_id(SSL
*ssl
, GEN_SESSION_CB cb
)
976 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
978 if (sc
== NULL
|| !CRYPTO_THREAD_write_lock(ssl
->lock
))
980 sc
->generate_session_id
= cb
;
981 CRYPTO_THREAD_unlock(ssl
->lock
);
985 int SSL_has_matching_session_id(const SSL
*ssl
, const unsigned char *id
,
989 * A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
990 * we can "construct" a session to give us the desired check - i.e. to
991 * find if there's a session in the hash table that would conflict with
992 * any new session built out of this id/id_len and the ssl_version in use
996 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(ssl
);
998 if (sc
== NULL
|| id_len
> sizeof(r
.session_id
))
1001 r
.ssl_version
= sc
->version
;
1002 r
.session_id_length
= id_len
;
1003 memcpy(r
.session_id
, id
, id_len
);
1005 if (!CRYPTO_THREAD_read_lock(sc
->session_ctx
->lock
))
1007 p
= lh_SSL_SESSION_retrieve(sc
->session_ctx
->sessions
, &r
);
1008 CRYPTO_THREAD_unlock(sc
->session_ctx
->lock
);
1012 int SSL_CTX_set_purpose(SSL_CTX
*s
, int purpose
)
1014 return X509_VERIFY_PARAM_set_purpose(s
->param
, purpose
);
1017 int SSL_set_purpose(SSL
*s
, int purpose
)
1019 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1024 return X509_VERIFY_PARAM_set_purpose(sc
->param
, purpose
);
1027 int SSL_CTX_set_trust(SSL_CTX
*s
, int trust
)
1029 return X509_VERIFY_PARAM_set_trust(s
->param
, trust
);
1032 int SSL_set_trust(SSL
*s
, int trust
)
1034 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1039 return X509_VERIFY_PARAM_set_trust(sc
->param
, trust
);
1042 int SSL_set1_host(SSL
*s
, const char *hostname
)
1044 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1049 /* If a hostname is provided and parses as an IP address,
1050 * treat it as such. */
1051 if (hostname
!= NULL
1052 && X509_VERIFY_PARAM_set1_ip_asc(sc
->param
, hostname
) == 1)
1055 return X509_VERIFY_PARAM_set1_host(sc
->param
, hostname
, 0);
1058 int SSL_add1_host(SSL
*s
, const char *hostname
)
1060 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1065 /* If a hostname is provided and parses as an IP address,
1066 * treat it as such. */
1069 ASN1_OCTET_STRING
*ip
;
1072 ip
= a2i_IPADDRESS(hostname
);
1074 /* We didn't want it; only to check if it *is* an IP address */
1075 ASN1_OCTET_STRING_free(ip
);
1077 old_ip
= X509_VERIFY_PARAM_get1_ip_asc(sc
->param
);
1080 OPENSSL_free(old_ip
);
1081 /* There can be only one IP address */
1085 return X509_VERIFY_PARAM_set1_ip_asc(sc
->param
, hostname
);
1089 return X509_VERIFY_PARAM_add1_host(sc
->param
, hostname
, 0);
1092 void SSL_set_hostflags(SSL
*s
, unsigned int flags
)
1094 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1099 X509_VERIFY_PARAM_set_hostflags(sc
->param
, flags
);
1102 const char *SSL_get0_peername(SSL
*s
)
1104 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1109 return X509_VERIFY_PARAM_get0_peername(sc
->param
);
1112 int SSL_CTX_dane_enable(SSL_CTX
*ctx
)
1114 return dane_ctx_enable(&ctx
->dane
);
1117 unsigned long SSL_CTX_dane_set_flags(SSL_CTX
*ctx
, unsigned long flags
)
1119 unsigned long orig
= ctx
->dane
.flags
;
1121 ctx
->dane
.flags
|= flags
;
1125 unsigned long SSL_CTX_dane_clear_flags(SSL_CTX
*ctx
, unsigned long flags
)
1127 unsigned long orig
= ctx
->dane
.flags
;
1129 ctx
->dane
.flags
&= ~flags
;
1133 int SSL_dane_enable(SSL
*s
, const char *basedomain
)
1136 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1142 if (s
->ctx
->dane
.mdmax
== 0) {
1143 ERR_raise(ERR_LIB_SSL
, SSL_R_CONTEXT_NOT_DANE_ENABLED
);
1146 if (dane
->trecs
!= NULL
) {
1147 ERR_raise(ERR_LIB_SSL
, SSL_R_DANE_ALREADY_ENABLED
);
1152 * Default SNI name. This rejects empty names, while set1_host below
1153 * accepts them and disables host name checks. To avoid side-effects with
1154 * invalid input, set the SNI name first.
1156 if (sc
->ext
.hostname
== NULL
) {
1157 if (!SSL_set_tlsext_host_name(s
, basedomain
)) {
1158 ERR_raise(ERR_LIB_SSL
, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN
);
1163 /* Primary RFC6125 reference identifier */
1164 if (!X509_VERIFY_PARAM_set1_host(sc
->param
, basedomain
, 0)) {
1165 ERR_raise(ERR_LIB_SSL
, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN
);
1171 dane
->dctx
= &s
->ctx
->dane
;
1172 dane
->trecs
= sk_danetls_record_new_null();
1174 if (dane
->trecs
== NULL
) {
1175 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
1181 unsigned long SSL_dane_set_flags(SSL
*ssl
, unsigned long flags
)
1184 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
1189 orig
= sc
->dane
.flags
;
1191 sc
->dane
.flags
|= flags
;
1195 unsigned long SSL_dane_clear_flags(SSL
*ssl
, unsigned long flags
)
1198 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
1203 orig
= sc
->dane
.flags
;
1205 sc
->dane
.flags
&= ~flags
;
1209 int SSL_get0_dane_authority(SSL
*s
, X509
**mcert
, EVP_PKEY
**mspki
)
1212 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1219 if (!DANETLS_ENABLED(dane
) || sc
->verify_result
!= X509_V_OK
)
1223 *mcert
= dane
->mcert
;
1225 *mspki
= (dane
->mcert
== NULL
) ? dane
->mtlsa
->spki
: NULL
;
1230 int SSL_get0_dane_tlsa(SSL
*s
, uint8_t *usage
, uint8_t *selector
,
1231 uint8_t *mtype
, const unsigned char **data
, size_t *dlen
)
1234 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1241 if (!DANETLS_ENABLED(dane
) || sc
->verify_result
!= X509_V_OK
)
1245 *usage
= dane
->mtlsa
->usage
;
1247 *selector
= dane
->mtlsa
->selector
;
1249 *mtype
= dane
->mtlsa
->mtype
;
1251 *data
= dane
->mtlsa
->data
;
1253 *dlen
= dane
->mtlsa
->dlen
;
1258 SSL_DANE
*SSL_get0_dane(SSL
*s
)
1260 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1268 int SSL_dane_tlsa_add(SSL
*s
, uint8_t usage
, uint8_t selector
,
1269 uint8_t mtype
, const unsigned char *data
, size_t dlen
)
1271 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1276 return dane_tlsa_add(&sc
->dane
, usage
, selector
, mtype
, data
, dlen
);
1279 int SSL_CTX_dane_mtype_set(SSL_CTX
*ctx
, const EVP_MD
*md
, uint8_t mtype
,
1282 return dane_mtype_set(&ctx
->dane
, md
, mtype
, ord
);
1285 int SSL_CTX_set1_param(SSL_CTX
*ctx
, X509_VERIFY_PARAM
*vpm
)
1287 return X509_VERIFY_PARAM_set1(ctx
->param
, vpm
);
1290 int SSL_set1_param(SSL
*ssl
, X509_VERIFY_PARAM
*vpm
)
1292 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
1297 return X509_VERIFY_PARAM_set1(sc
->param
, vpm
);
1300 X509_VERIFY_PARAM
*SSL_CTX_get0_param(SSL_CTX
*ctx
)
1305 X509_VERIFY_PARAM
*SSL_get0_param(SSL
*ssl
)
1307 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
1315 void SSL_certs_clear(SSL
*s
)
1317 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1322 ssl_cert_clear_certs(sc
->cert
);
1325 void SSL_free(SSL
*s
)
1331 CRYPTO_DOWN_REF(&s
->references
, &i
, s
->lock
);
1332 REF_PRINT_COUNT("SSL", s
);
1335 REF_ASSERT_ISNT(i
< 0);
1337 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL
, s
, &s
->ex_data
);
1339 if (s
->method
!= NULL
)
1340 s
->method
->ssl_free(s
);
1342 SSL_CTX_free(s
->ctx
);
1343 CRYPTO_THREAD_lock_free(s
->lock
);
1348 void ossl_ssl_connection_free(SSL
*ssl
)
1352 s
= SSL_CONNECTION_FROM_SSL_ONLY(ssl
);
1356 X509_VERIFY_PARAM_free(s
->param
);
1357 dane_final(&s
->dane
);
1359 RECORD_LAYER_release(&s
->rlayer
);
1361 /* Ignore return value */
1362 ssl_free_wbio_buffer(s
);
1364 BIO_free_all(s
->wbio
);
1366 BIO_free_all(s
->rbio
);
1369 BUF_MEM_free(s
->init_buf
);
1371 /* add extra stuff */
1372 sk_SSL_CIPHER_free(s
->cipher_list
);
1373 sk_SSL_CIPHER_free(s
->cipher_list_by_id
);
1374 sk_SSL_CIPHER_free(s
->tls13_ciphersuites
);
1375 sk_SSL_CIPHER_free(s
->peer_ciphers
);
1377 /* Make the next call work :-) */
1378 if (s
->session
!= NULL
) {
1379 ssl_clear_bad_session(s
);
1380 SSL_SESSION_free(s
->session
);
1382 SSL_SESSION_free(s
->psksession
);
1383 OPENSSL_free(s
->psksession_id
);
1387 ssl_cert_free(s
->cert
);
1388 OPENSSL_free(s
->shared_sigalgs
);
1389 /* Free up if allocated */
1391 OPENSSL_free(s
->ext
.hostname
);
1392 SSL_CTX_free(s
->session_ctx
);
1393 OPENSSL_free(s
->ext
.ecpointformats
);
1394 OPENSSL_free(s
->ext
.peer_ecpointformats
);
1395 OPENSSL_free(s
->ext
.supportedgroups
);
1396 OPENSSL_free(s
->ext
.peer_supportedgroups
);
1397 sk_X509_EXTENSION_pop_free(s
->ext
.ocsp
.exts
, X509_EXTENSION_free
);
1398 #ifndef OPENSSL_NO_OCSP
1399 sk_OCSP_RESPID_pop_free(s
->ext
.ocsp
.ids
, OCSP_RESPID_free
);
1401 #ifndef OPENSSL_NO_CT
1402 SCT_LIST_free(s
->scts
);
1403 OPENSSL_free(s
->ext
.scts
);
1405 OPENSSL_free(s
->ext
.ocsp
.resp
);
1406 OPENSSL_free(s
->ext
.alpn
);
1407 OPENSSL_free(s
->ext
.tls13_cookie
);
1408 if (s
->clienthello
!= NULL
)
1409 OPENSSL_free(s
->clienthello
->pre_proc_exts
);
1410 OPENSSL_free(s
->clienthello
);
1411 OPENSSL_free(s
->pha_context
);
1412 EVP_MD_CTX_free(s
->pha_dgst
);
1414 sk_X509_NAME_pop_free(s
->ca_names
, X509_NAME_free
);
1415 sk_X509_NAME_pop_free(s
->client_ca_names
, X509_NAME_free
);
1417 OSSL_STACK_OF_X509_free(s
->verified_chain
);
1419 if (ssl
->method
!= NULL
)
1420 ssl
->method
->ssl_deinit(ssl
);
1422 ASYNC_WAIT_CTX_free(s
->waitctx
);
1424 #if !defined(OPENSSL_NO_NEXTPROTONEG)
1425 OPENSSL_free(s
->ext
.npn
);
1428 #ifndef OPENSSL_NO_SRTP
1429 sk_SRTP_PROTECTION_PROFILE_free(s
->srtp_profiles
);
1433 void SSL_set0_rbio(SSL
*s
, BIO
*rbio
)
1435 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1440 BIO_free_all(sc
->rbio
);
1442 sc
->rrlmethod
->set1_bio(sc
->rrl
, sc
->rbio
);
1445 void SSL_set0_wbio(SSL
*s
, BIO
*wbio
)
1447 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1453 * If the output buffering BIO is still in place, remove it
1455 if (sc
->bbio
!= NULL
)
1456 sc
->wbio
= BIO_pop(sc
->wbio
);
1458 BIO_free_all(sc
->wbio
);
1461 /* Re-attach |bbio| to the new |wbio|. */
1462 if (sc
->bbio
!= NULL
)
1463 sc
->wbio
= BIO_push(sc
->bbio
, sc
->wbio
);
1466 void SSL_set_bio(SSL
*s
, BIO
*rbio
, BIO
*wbio
)
1469 * For historical reasons, this function has many different cases in
1470 * ownership handling.
1473 /* If nothing has changed, do nothing */
1474 if (rbio
== SSL_get_rbio(s
) && wbio
== SSL_get_wbio(s
))
1478 * If the two arguments are equal then one fewer reference is granted by the
1479 * caller than we want to take
1481 if (rbio
!= NULL
&& rbio
== wbio
)
1485 * If only the wbio is changed only adopt one reference.
1487 if (rbio
== SSL_get_rbio(s
)) {
1488 SSL_set0_wbio(s
, wbio
);
1492 * There is an asymmetry here for historical reasons. If only the rbio is
1493 * changed AND the rbio and wbio were originally different, then we only
1494 * adopt one reference.
1496 if (wbio
== SSL_get_wbio(s
) && SSL_get_rbio(s
) != SSL_get_wbio(s
)) {
1497 SSL_set0_rbio(s
, rbio
);
1501 /* Otherwise, adopt both references. */
1502 SSL_set0_rbio(s
, rbio
);
1503 SSL_set0_wbio(s
, wbio
);
1506 BIO
*SSL_get_rbio(const SSL
*s
)
1508 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
1516 BIO
*SSL_get_wbio(const SSL
*s
)
1518 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
1523 if (sc
->bbio
!= NULL
) {
1525 * If |bbio| is active, the true caller-configured BIO is its
1528 return BIO_next(sc
->bbio
);
1533 int SSL_get_fd(const SSL
*s
)
1535 return SSL_get_rfd(s
);
1538 int SSL_get_rfd(const SSL
*s
)
1543 b
= SSL_get_rbio(s
);
1544 r
= BIO_find_type(b
, BIO_TYPE_DESCRIPTOR
);
1546 BIO_get_fd(r
, &ret
);
1550 int SSL_get_wfd(const SSL
*s
)
1555 b
= SSL_get_wbio(s
);
1556 r
= BIO_find_type(b
, BIO_TYPE_DESCRIPTOR
);
1558 BIO_get_fd(r
, &ret
);
1562 #ifndef OPENSSL_NO_SOCK
1563 int SSL_set_fd(SSL
*s
, int fd
)
1568 bio
= BIO_new(BIO_s_socket());
1571 ERR_raise(ERR_LIB_SSL
, ERR_R_BUF_LIB
);
1574 BIO_set_fd(bio
, fd
, BIO_NOCLOSE
);
1575 SSL_set_bio(s
, bio
, bio
);
1576 #ifndef OPENSSL_NO_KTLS
1578 * The new socket is created successfully regardless of ktls_enable.
1579 * ktls_enable doesn't change any functionality of the socket, except
1580 * changing the setsockopt to enable the processing of ktls_start.
1581 * Thus, it is not a problem to call it for non-TLS sockets.
1584 #endif /* OPENSSL_NO_KTLS */
1590 int SSL_set_wfd(SSL
*s
, int fd
)
1592 BIO
*rbio
= SSL_get_rbio(s
);
1594 if (rbio
== NULL
|| BIO_method_type(rbio
) != BIO_TYPE_SOCKET
1595 || (int)BIO_get_fd(rbio
, NULL
) != fd
) {
1596 BIO
*bio
= BIO_new(BIO_s_socket());
1599 ERR_raise(ERR_LIB_SSL
, ERR_R_BUF_LIB
);
1602 BIO_set_fd(bio
, fd
, BIO_NOCLOSE
);
1603 SSL_set0_wbio(s
, bio
);
1604 #ifndef OPENSSL_NO_KTLS
1606 * The new socket is created successfully regardless of ktls_enable.
1607 * ktls_enable doesn't change any functionality of the socket, except
1608 * changing the setsockopt to enable the processing of ktls_start.
1609 * Thus, it is not a problem to call it for non-TLS sockets.
1612 #endif /* OPENSSL_NO_KTLS */
1615 SSL_set0_wbio(s
, rbio
);
1620 int SSL_set_rfd(SSL
*s
, int fd
)
1622 BIO
*wbio
= SSL_get_wbio(s
);
1624 if (wbio
== NULL
|| BIO_method_type(wbio
) != BIO_TYPE_SOCKET
1625 || ((int)BIO_get_fd(wbio
, NULL
) != fd
)) {
1626 BIO
*bio
= BIO_new(BIO_s_socket());
1629 ERR_raise(ERR_LIB_SSL
, ERR_R_BUF_LIB
);
1632 BIO_set_fd(bio
, fd
, BIO_NOCLOSE
);
1633 SSL_set0_rbio(s
, bio
);
1636 SSL_set0_rbio(s
, wbio
);
1643 /* return length of latest Finished message we sent, copy to 'buf' */
1644 size_t SSL_get_finished(const SSL
*s
, void *buf
, size_t count
)
1647 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
1652 ret
= sc
->s3
.tmp
.finish_md_len
;
1655 memcpy(buf
, sc
->s3
.tmp
.finish_md
, count
);
1659 /* return length of latest Finished message we expected, copy to 'buf' */
1660 size_t SSL_get_peer_finished(const SSL
*s
, void *buf
, size_t count
)
1663 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
1668 ret
= sc
->s3
.tmp
.peer_finish_md_len
;
1671 memcpy(buf
, sc
->s3
.tmp
.peer_finish_md
, count
);
1675 int SSL_get_verify_mode(const SSL
*s
)
1677 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
1682 return sc
->verify_mode
;
1685 int SSL_get_verify_depth(const SSL
*s
)
1687 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
1692 return X509_VERIFY_PARAM_get_depth(sc
->param
);
1695 int (*SSL_get_verify_callback(const SSL
*s
)) (int, X509_STORE_CTX
*) {
1696 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
1701 return sc
->verify_callback
;
1704 int SSL_CTX_get_verify_mode(const SSL_CTX
*ctx
)
1706 return ctx
->verify_mode
;
1709 int SSL_CTX_get_verify_depth(const SSL_CTX
*ctx
)
1711 return X509_VERIFY_PARAM_get_depth(ctx
->param
);
1714 int (*SSL_CTX_get_verify_callback(const SSL_CTX
*ctx
)) (int, X509_STORE_CTX
*) {
1715 return ctx
->default_verify_callback
;
1718 void SSL_set_verify(SSL
*s
, int mode
,
1719 int (*callback
) (int ok
, X509_STORE_CTX
*ctx
))
1721 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1726 sc
->verify_mode
= mode
;
1727 if (callback
!= NULL
)
1728 sc
->verify_callback
= callback
;
1731 void SSL_set_verify_depth(SSL
*s
, int depth
)
1733 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1738 X509_VERIFY_PARAM_set_depth(sc
->param
, depth
);
1741 void SSL_set_read_ahead(SSL
*s
, int yes
)
1743 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1748 RECORD_LAYER_set_read_ahead(&sc
->rlayer
, yes
);
1751 int SSL_get_read_ahead(const SSL
*s
)
1753 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
1758 return RECORD_LAYER_get_read_ahead(&sc
->rlayer
);
1761 int SSL_pending(const SSL
*s
)
1763 size_t pending
= s
->method
->ssl_pending(s
);
1766 * SSL_pending cannot work properly if read-ahead is enabled
1767 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
1768 * impossible to fix since SSL_pending cannot report errors that may be
1769 * observed while scanning the new data. (Note that SSL_pending() is
1770 * often used as a boolean value, so we'd better not return -1.)
1772 * SSL_pending also cannot work properly if the value >INT_MAX. In that case
1773 * we just return INT_MAX.
1775 return pending
< INT_MAX
? (int)pending
: INT_MAX
;
1778 int SSL_has_pending(const SSL
*s
)
1781 * Similar to SSL_pending() but returns a 1 to indicate that we have
1782 * processed or unprocessed data available or 0 otherwise (as opposed to the
1783 * number of bytes available). Unlike SSL_pending() this will take into
1784 * account read_ahead data. A 1 return simply indicates that we have data.
1785 * That data may not result in any application data, or we may fail to parse
1786 * the records for some reason.
1788 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
1790 /* Check buffered app data if any first */
1791 if (SSL_CONNECTION_IS_DTLS(sc
)) {
1792 DTLS1_RECORD_DATA
*rdata
;
1795 iter
= pqueue_iterator(sc
->rlayer
.d
->buffered_app_data
.q
);
1796 while ((item
= pqueue_next(&iter
)) != NULL
) {
1798 if (rdata
->rrec
.length
> 0)
1803 if (RECORD_LAYER_processed_read_pending(&sc
->rlayer
))
1806 return RECORD_LAYER_read_pending(&sc
->rlayer
);
1809 X509
*SSL_get1_peer_certificate(const SSL
*s
)
1811 X509
*r
= SSL_get0_peer_certificate(s
);
1819 X509
*SSL_get0_peer_certificate(const SSL
*s
)
1821 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
1826 if (sc
->session
== NULL
)
1829 return sc
->session
->peer
;
1832 STACK_OF(X509
) *SSL_get_peer_cert_chain(const SSL
*s
)
1835 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
1840 if (sc
->session
== NULL
)
1843 r
= sc
->session
->peer_chain
;
1846 * If we are a client, cert_chain includes the peer's own certificate; if
1847 * we are a server, it does not.
1854 * Now in theory, since the calling process own 't' it should be safe to
1855 * modify. We need to be able to read f without being hassled
1857 int SSL_copy_session_id(SSL
*t
, const SSL
*f
)
1860 /* TODO(QUIC): Do we want to support this for QUIC connections? */
1861 SSL_CONNECTION
*tsc
= SSL_CONNECTION_FROM_SSL_ONLY(t
);
1862 const SSL_CONNECTION
*fsc
= SSL_CONNECTION_FROM_CONST_SSL_ONLY(f
);
1864 if (tsc
== NULL
|| fsc
== NULL
)
1867 /* Do we need to do SSL locking? */
1868 if (!SSL_set_session(t
, SSL_get_session(f
))) {
1873 * what if we are setup for one protocol version but want to talk another
1875 if (t
->method
!= f
->method
) {
1876 t
->method
->ssl_deinit(t
);
1877 t
->method
= f
->method
;
1878 if (t
->method
->ssl_init(t
) == 0)
1882 CRYPTO_UP_REF(&fsc
->cert
->references
, &i
, fsc
->cert
->lock
);
1883 ssl_cert_free(tsc
->cert
);
1884 tsc
->cert
= fsc
->cert
;
1885 if (!SSL_set_session_id_context(t
, fsc
->sid_ctx
, (int)fsc
->sid_ctx_length
)) {
1892 /* Fix this so it checks all the valid key/cert options */
1893 int SSL_CTX_check_private_key(const SSL_CTX
*ctx
)
1895 if ((ctx
== NULL
) || (ctx
->cert
->key
->x509
== NULL
)) {
1896 ERR_raise(ERR_LIB_SSL
, SSL_R_NO_CERTIFICATE_ASSIGNED
);
1899 if (ctx
->cert
->key
->privatekey
== NULL
) {
1900 ERR_raise(ERR_LIB_SSL
, SSL_R_NO_PRIVATE_KEY_ASSIGNED
);
1903 return X509_check_private_key
1904 (ctx
->cert
->key
->x509
, ctx
->cert
->key
->privatekey
);
1907 /* Fix this function so that it takes an optional type parameter */
1908 int SSL_check_private_key(const SSL
*ssl
)
1910 const SSL_CONNECTION
*sc
;
1912 if ((sc
= SSL_CONNECTION_FROM_CONST_SSL(ssl
)) == NULL
) {
1913 ERR_raise(ERR_LIB_SSL
, ERR_R_PASSED_NULL_PARAMETER
);
1916 if (sc
->cert
->key
->x509
== NULL
) {
1917 ERR_raise(ERR_LIB_SSL
, SSL_R_NO_CERTIFICATE_ASSIGNED
);
1920 if (sc
->cert
->key
->privatekey
== NULL
) {
1921 ERR_raise(ERR_LIB_SSL
, SSL_R_NO_PRIVATE_KEY_ASSIGNED
);
1924 return X509_check_private_key(sc
->cert
->key
->x509
,
1925 sc
->cert
->key
->privatekey
);
1928 int SSL_waiting_for_async(SSL
*s
)
1930 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1941 int SSL_get_all_async_fds(SSL
*s
, OSSL_ASYNC_FD
*fds
, size_t *numfds
)
1943 ASYNC_WAIT_CTX
*ctx
;
1944 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1949 if ((ctx
= sc
->waitctx
) == NULL
)
1951 return ASYNC_WAIT_CTX_get_all_fds(ctx
, fds
, numfds
);
1954 int SSL_get_changed_async_fds(SSL
*s
, OSSL_ASYNC_FD
*addfd
, size_t *numaddfds
,
1955 OSSL_ASYNC_FD
*delfd
, size_t *numdelfds
)
1957 ASYNC_WAIT_CTX
*ctx
;
1958 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1963 if ((ctx
= sc
->waitctx
) == NULL
)
1965 return ASYNC_WAIT_CTX_get_changed_fds(ctx
, addfd
, numaddfds
, delfd
,
1969 int SSL_CTX_set_async_callback(SSL_CTX
*ctx
, SSL_async_callback_fn callback
)
1971 ctx
->async_cb
= callback
;
1975 int SSL_CTX_set_async_callback_arg(SSL_CTX
*ctx
, void *arg
)
1977 ctx
->async_cb_arg
= arg
;
1981 int SSL_set_async_callback(SSL
*s
, SSL_async_callback_fn callback
)
1983 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1988 sc
->async_cb
= callback
;
1992 int SSL_set_async_callback_arg(SSL
*s
, void *arg
)
1994 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
1999 sc
->async_cb_arg
= arg
;
2003 int SSL_get_async_status(SSL
*s
, int *status
)
2005 ASYNC_WAIT_CTX
*ctx
;
2006 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2011 if ((ctx
= sc
->waitctx
) == NULL
)
2013 *status
= ASYNC_WAIT_CTX_get_status(ctx
);
2017 int SSL_accept(SSL
*s
)
2019 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2024 if (sc
->handshake_func
== NULL
) {
2025 /* Not properly initialized yet */
2026 SSL_set_accept_state(s
);
2029 return SSL_do_handshake(s
);
2032 int SSL_connect(SSL
*s
)
2034 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2039 if (sc
->handshake_func
== NULL
) {
2040 /* Not properly initialized yet */
2041 SSL_set_connect_state(s
);
2044 return SSL_do_handshake(s
);
2047 long SSL_get_default_timeout(const SSL
*s
)
2049 return s
->method
->get_timeout();
2052 static int ssl_async_wait_ctx_cb(void *arg
)
2054 SSL
*s
= (SSL
*)arg
;
2055 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2060 return sc
->async_cb(s
, sc
->async_cb_arg
);
2063 static int ssl_start_async_job(SSL
*s
, struct ssl_async_args
*args
,
2064 int (*func
) (void *))
2067 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2072 if (sc
->waitctx
== NULL
) {
2073 sc
->waitctx
= ASYNC_WAIT_CTX_new();
2074 if (sc
->waitctx
== NULL
)
2076 if (sc
->async_cb
!= NULL
2077 && !ASYNC_WAIT_CTX_set_callback
2078 (sc
->waitctx
, ssl_async_wait_ctx_cb
, s
))
2082 sc
->rwstate
= SSL_NOTHING
;
2083 switch (ASYNC_start_job(&sc
->job
, sc
->waitctx
, &ret
, func
, args
,
2084 sizeof(struct ssl_async_args
))) {
2086 sc
->rwstate
= SSL_NOTHING
;
2087 ERR_raise(ERR_LIB_SSL
, SSL_R_FAILED_TO_INIT_ASYNC
);
2090 sc
->rwstate
= SSL_ASYNC_PAUSED
;
2093 sc
->rwstate
= SSL_ASYNC_NO_JOBS
;
2099 sc
->rwstate
= SSL_NOTHING
;
2100 ERR_raise(ERR_LIB_SSL
, ERR_R_INTERNAL_ERROR
);
2101 /* Shouldn't happen */
2106 static int ssl_io_intern(void *vargs
)
2108 struct ssl_async_args
*args
;
2114 args
= (struct ssl_async_args
*)vargs
;
2118 if ((sc
= SSL_CONNECTION_FROM_SSL(s
)) == NULL
)
2121 switch (args
->type
) {
2123 return args
->f
.func_read(s
, buf
, num
, &sc
->asyncrw
);
2125 return args
->f
.func_write(s
, buf
, num
, &sc
->asyncrw
);
2127 return args
->f
.func_other(s
);
2132 int ssl_read_internal(SSL
*s
, void *buf
, size_t num
, size_t *readbytes
)
2134 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2139 if (sc
->handshake_func
== NULL
) {
2140 ERR_raise(ERR_LIB_SSL
, SSL_R_UNINITIALIZED
);
2144 if (sc
->shutdown
& SSL_RECEIVED_SHUTDOWN
) {
2145 sc
->rwstate
= SSL_NOTHING
;
2149 if (sc
->early_data_state
== SSL_EARLY_DATA_CONNECT_RETRY
2150 || sc
->early_data_state
== SSL_EARLY_DATA_ACCEPT_RETRY
) {
2151 ERR_raise(ERR_LIB_SSL
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
2155 * If we are a client and haven't received the ServerHello etc then we
2158 ossl_statem_check_finish_init(sc
, 0);
2160 if ((sc
->mode
& SSL_MODE_ASYNC
) && ASYNC_get_current_job() == NULL
) {
2161 struct ssl_async_args args
;
2167 args
.type
= READFUNC
;
2168 args
.f
.func_read
= s
->method
->ssl_read
;
2170 ret
= ssl_start_async_job(s
, &args
, ssl_io_intern
);
2171 *readbytes
= sc
->asyncrw
;
2174 return s
->method
->ssl_read(s
, buf
, num
, readbytes
);
2178 int SSL_read(SSL
*s
, void *buf
, int num
)
2184 ERR_raise(ERR_LIB_SSL
, SSL_R_BAD_LENGTH
);
2188 ret
= ssl_read_internal(s
, buf
, (size_t)num
, &readbytes
);
2191 * The cast is safe here because ret should be <= INT_MAX because num is
2195 ret
= (int)readbytes
;
2200 int SSL_read_ex(SSL
*s
, void *buf
, size_t num
, size_t *readbytes
)
2202 int ret
= ssl_read_internal(s
, buf
, num
, readbytes
);
2209 int SSL_read_early_data(SSL
*s
, void *buf
, size_t num
, size_t *readbytes
)
2212 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(s
);
2214 /* TODO(QUIC): This will need special handling for QUIC */
2219 ERR_raise(ERR_LIB_SSL
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
2220 return SSL_READ_EARLY_DATA_ERROR
;
2223 switch (sc
->early_data_state
) {
2224 case SSL_EARLY_DATA_NONE
:
2225 if (!SSL_in_before(s
)) {
2226 ERR_raise(ERR_LIB_SSL
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
2227 return SSL_READ_EARLY_DATA_ERROR
;
2231 case SSL_EARLY_DATA_ACCEPT_RETRY
:
2232 sc
->early_data_state
= SSL_EARLY_DATA_ACCEPTING
;
2233 ret
= SSL_accept(s
);
2236 sc
->early_data_state
= SSL_EARLY_DATA_ACCEPT_RETRY
;
2237 return SSL_READ_EARLY_DATA_ERROR
;
2241 case SSL_EARLY_DATA_READ_RETRY
:
2242 if (sc
->ext
.early_data
== SSL_EARLY_DATA_ACCEPTED
) {
2243 sc
->early_data_state
= SSL_EARLY_DATA_READING
;
2244 ret
= SSL_read_ex(s
, buf
, num
, readbytes
);
2246 * State machine will update early_data_state to
2247 * SSL_EARLY_DATA_FINISHED_READING if we get an EndOfEarlyData
2250 if (ret
> 0 || (ret
<= 0 && sc
->early_data_state
2251 != SSL_EARLY_DATA_FINISHED_READING
)) {
2252 sc
->early_data_state
= SSL_EARLY_DATA_READ_RETRY
;
2253 return ret
> 0 ? SSL_READ_EARLY_DATA_SUCCESS
2254 : SSL_READ_EARLY_DATA_ERROR
;
2257 sc
->early_data_state
= SSL_EARLY_DATA_FINISHED_READING
;
2260 return SSL_READ_EARLY_DATA_FINISH
;
2263 ERR_raise(ERR_LIB_SSL
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
2264 return SSL_READ_EARLY_DATA_ERROR
;
2268 int SSL_get_early_data_status(const SSL
*s
)
2270 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL_ONLY(s
);
2272 /* TODO(QUIC): This will need special handling for QUIC */
2276 return sc
->ext
.early_data
;
2279 static int ssl_peek_internal(SSL
*s
, void *buf
, size_t num
, size_t *readbytes
)
2281 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2286 if (sc
->handshake_func
== NULL
) {
2287 ERR_raise(ERR_LIB_SSL
, SSL_R_UNINITIALIZED
);
2291 if (sc
->shutdown
& SSL_RECEIVED_SHUTDOWN
) {
2294 if ((sc
->mode
& SSL_MODE_ASYNC
) && ASYNC_get_current_job() == NULL
) {
2295 struct ssl_async_args args
;
2301 args
.type
= READFUNC
;
2302 args
.f
.func_read
= s
->method
->ssl_peek
;
2304 ret
= ssl_start_async_job(s
, &args
, ssl_io_intern
);
2305 *readbytes
= sc
->asyncrw
;
2308 return s
->method
->ssl_peek(s
, buf
, num
, readbytes
);
2312 int SSL_peek(SSL
*s
, void *buf
, int num
)
2318 ERR_raise(ERR_LIB_SSL
, SSL_R_BAD_LENGTH
);
2322 ret
= ssl_peek_internal(s
, buf
, (size_t)num
, &readbytes
);
2325 * The cast is safe here because ret should be <= INT_MAX because num is
2329 ret
= (int)readbytes
;
2335 int SSL_peek_ex(SSL
*s
, void *buf
, size_t num
, size_t *readbytes
)
2337 int ret
= ssl_peek_internal(s
, buf
, num
, readbytes
);
2344 int ssl_write_internal(SSL
*s
, const void *buf
, size_t num
, size_t *written
)
2346 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2351 if (sc
->handshake_func
== NULL
) {
2352 ERR_raise(ERR_LIB_SSL
, SSL_R_UNINITIALIZED
);
2356 if (sc
->shutdown
& SSL_SENT_SHUTDOWN
) {
2357 sc
->rwstate
= SSL_NOTHING
;
2358 ERR_raise(ERR_LIB_SSL
, SSL_R_PROTOCOL_IS_SHUTDOWN
);
2362 if (sc
->early_data_state
== SSL_EARLY_DATA_CONNECT_RETRY
2363 || sc
->early_data_state
== SSL_EARLY_DATA_ACCEPT_RETRY
2364 || sc
->early_data_state
== SSL_EARLY_DATA_READ_RETRY
) {
2365 ERR_raise(ERR_LIB_SSL
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
2368 /* If we are a client and haven't sent the Finished we better do that */
2369 ossl_statem_check_finish_init(sc
, 1);
2371 if ((sc
->mode
& SSL_MODE_ASYNC
) && ASYNC_get_current_job() == NULL
) {
2373 struct ssl_async_args args
;
2376 args
.buf
= (void *)buf
;
2378 args
.type
= WRITEFUNC
;
2379 args
.f
.func_write
= s
->method
->ssl_write
;
2381 ret
= ssl_start_async_job(s
, &args
, ssl_io_intern
);
2382 *written
= sc
->asyncrw
;
2385 return s
->method
->ssl_write(s
, buf
, num
, written
);
2389 ossl_ssize_t
SSL_sendfile(SSL
*s
, int fd
, off_t offset
, size_t size
, int flags
)
2392 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(s
);
2397 if (sc
->handshake_func
== NULL
) {
2398 ERR_raise(ERR_LIB_SSL
, SSL_R_UNINITIALIZED
);
2402 if (sc
->shutdown
& SSL_SENT_SHUTDOWN
) {
2403 sc
->rwstate
= SSL_NOTHING
;
2404 ERR_raise(ERR_LIB_SSL
, SSL_R_PROTOCOL_IS_SHUTDOWN
);
2408 if (!BIO_get_ktls_send(sc
->wbio
)) {
2409 ERR_raise(ERR_LIB_SSL
, SSL_R_UNINITIALIZED
);
2413 /* If we have an alert to send, lets send it */
2414 if (sc
->s3
.alert_dispatch
) {
2415 ret
= (ossl_ssize_t
)s
->method
->ssl_dispatch_alert(s
);
2417 /* SSLfatal() already called if appropriate */
2420 /* if it went, fall through and send more stuff */
2423 sc
->rwstate
= SSL_WRITING
;
2424 if (BIO_flush(sc
->wbio
) <= 0) {
2425 if (!BIO_should_retry(sc
->wbio
)) {
2426 sc
->rwstate
= SSL_NOTHING
;
2429 set_sys_error(EAGAIN
);
2435 #ifdef OPENSSL_NO_KTLS
2436 ERR_raise_data(ERR_LIB_SSL
, ERR_R_INTERNAL_ERROR
,
2437 "can't call ktls_sendfile(), ktls disabled");
2440 ret
= ktls_sendfile(SSL_get_wfd(s
), fd
, offset
, size
, flags
);
2442 #if defined(EAGAIN) && defined(EINTR) && defined(EBUSY)
2443 if ((get_last_sys_error() == EAGAIN
) ||
2444 (get_last_sys_error() == EINTR
) ||
2445 (get_last_sys_error() == EBUSY
))
2446 BIO_set_retry_write(sc
->wbio
);
2449 ERR_raise(ERR_LIB_SSL
, SSL_R_UNINITIALIZED
);
2452 sc
->rwstate
= SSL_NOTHING
;
2457 int SSL_write(SSL
*s
, const void *buf
, int num
)
2463 ERR_raise(ERR_LIB_SSL
, SSL_R_BAD_LENGTH
);
2467 ret
= ssl_write_internal(s
, buf
, (size_t)num
, &written
);
2470 * The cast is safe here because ret should be <= INT_MAX because num is
2479 int SSL_write_ex(SSL
*s
, const void *buf
, size_t num
, size_t *written
)
2481 int ret
= ssl_write_internal(s
, buf
, num
, written
);
2488 int SSL_write_early_data(SSL
*s
, const void *buf
, size_t num
, size_t *written
)
2490 int ret
, early_data_state
;
2492 uint32_t partialwrite
;
2493 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(s
);
2495 /* TODO(QUIC): This will need special handling for QUIC */
2499 switch (sc
->early_data_state
) {
2500 case SSL_EARLY_DATA_NONE
:
2502 || !SSL_in_before(s
)
2503 || ((sc
->session
== NULL
|| sc
->session
->ext
.max_early_data
== 0)
2504 && (sc
->psk_use_session_cb
== NULL
))) {
2505 ERR_raise(ERR_LIB_SSL
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
2510 case SSL_EARLY_DATA_CONNECT_RETRY
:
2511 sc
->early_data_state
= SSL_EARLY_DATA_CONNECTING
;
2512 ret
= SSL_connect(s
);
2515 sc
->early_data_state
= SSL_EARLY_DATA_CONNECT_RETRY
;
2520 case SSL_EARLY_DATA_WRITE_RETRY
:
2521 sc
->early_data_state
= SSL_EARLY_DATA_WRITING
;
2523 * We disable partial write for early data because we don't keep track
2524 * of how many bytes we've written between the SSL_write_ex() call and
2525 * the flush if the flush needs to be retried)
2527 partialwrite
= sc
->mode
& SSL_MODE_ENABLE_PARTIAL_WRITE
;
2528 sc
->mode
&= ~SSL_MODE_ENABLE_PARTIAL_WRITE
;
2529 ret
= SSL_write_ex(s
, buf
, num
, &writtmp
);
2530 sc
->mode
|= partialwrite
;
2532 sc
->early_data_state
= SSL_EARLY_DATA_WRITE_RETRY
;
2535 sc
->early_data_state
= SSL_EARLY_DATA_WRITE_FLUSH
;
2538 case SSL_EARLY_DATA_WRITE_FLUSH
:
2539 /* The buffering BIO is still in place so we need to flush it */
2540 if (statem_flush(sc
) != 1)
2543 sc
->early_data_state
= SSL_EARLY_DATA_WRITE_RETRY
;
2546 case SSL_EARLY_DATA_FINISHED_READING
:
2547 case SSL_EARLY_DATA_READ_RETRY
:
2548 early_data_state
= sc
->early_data_state
;
2549 /* We are a server writing to an unauthenticated client */
2550 sc
->early_data_state
= SSL_EARLY_DATA_UNAUTH_WRITING
;
2551 ret
= SSL_write_ex(s
, buf
, num
, written
);
2552 /* The buffering BIO is still in place */
2554 (void)BIO_flush(sc
->wbio
);
2555 sc
->early_data_state
= early_data_state
;
2559 ERR_raise(ERR_LIB_SSL
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
2564 int SSL_shutdown(SSL
*s
)
2567 * Note that this function behaves differently from what one might
2568 * expect. Return values are 0 for no success (yet), 1 for success; but
2569 * calling it once is usually not enough, even if blocking I/O is used
2570 * (see ssl3_shutdown).
2572 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2577 if (sc
->handshake_func
== NULL
) {
2578 ERR_raise(ERR_LIB_SSL
, SSL_R_UNINITIALIZED
);
2582 if (!SSL_in_init(s
)) {
2583 if ((sc
->mode
& SSL_MODE_ASYNC
) && ASYNC_get_current_job() == NULL
) {
2584 struct ssl_async_args args
;
2586 memset(&args
, 0, sizeof(args
));
2588 args
.type
= OTHERFUNC
;
2589 args
.f
.func_other
= s
->method
->ssl_shutdown
;
2591 return ssl_start_async_job(s
, &args
, ssl_io_intern
);
2593 return s
->method
->ssl_shutdown(s
);
2596 ERR_raise(ERR_LIB_SSL
, SSL_R_SHUTDOWN_WHILE_IN_INIT
);
2601 int SSL_key_update(SSL
*s
, int updatetype
)
2603 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2608 if (!SSL_CONNECTION_IS_TLS13(sc
)) {
2609 ERR_raise(ERR_LIB_SSL
, SSL_R_WRONG_SSL_VERSION
);
2613 if (updatetype
!= SSL_KEY_UPDATE_NOT_REQUESTED
2614 && updatetype
!= SSL_KEY_UPDATE_REQUESTED
) {
2615 ERR_raise(ERR_LIB_SSL
, SSL_R_INVALID_KEY_UPDATE_TYPE
);
2619 if (!SSL_is_init_finished(s
)) {
2620 ERR_raise(ERR_LIB_SSL
, SSL_R_STILL_IN_INIT
);
2624 if (RECORD_LAYER_write_pending(&sc
->rlayer
)) {
2625 ERR_raise(ERR_LIB_SSL
, SSL_R_BAD_WRITE_RETRY
);
2629 ossl_statem_set_in_init(sc
, 1);
2630 sc
->key_update
= updatetype
;
2634 int SSL_get_key_update_type(const SSL
*s
)
2636 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
2641 return sc
->key_update
;
2645 * Can we accept a renegotiation request? If yes, set the flag and
2646 * return 1 if yes. If not, raise error and return 0.
2648 static int can_renegotiate(const SSL_CONNECTION
*sc
)
2650 if (SSL_CONNECTION_IS_TLS13(sc
)) {
2651 ERR_raise(ERR_LIB_SSL
, SSL_R_WRONG_SSL_VERSION
);
2655 if ((sc
->options
& SSL_OP_NO_RENEGOTIATION
) != 0) {
2656 ERR_raise(ERR_LIB_SSL
, SSL_R_NO_RENEGOTIATION
);
2663 int SSL_renegotiate(SSL
*s
)
2665 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(s
);
2670 if (!can_renegotiate(sc
))
2673 sc
->renegotiate
= 1;
2674 sc
->new_session
= 1;
2675 return s
->method
->ssl_renegotiate(s
);
2678 int SSL_renegotiate_abbreviated(SSL
*s
)
2680 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(s
);
2685 if (!can_renegotiate(sc
))
2688 sc
->renegotiate
= 1;
2689 sc
->new_session
= 0;
2690 return s
->method
->ssl_renegotiate(s
);
2693 int SSL_renegotiate_pending(const SSL
*s
)
2695 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(s
);
2701 * becomes true when negotiation is requested; false again once a
2702 * handshake has finished
2704 return (sc
->renegotiate
!= 0);
2707 int SSL_new_session_ticket(SSL
*s
)
2709 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2714 /* If we are in init because we're sending tickets, okay to send more. */
2715 if ((SSL_in_init(s
) && sc
->ext
.extra_tickets_expected
== 0)
2716 || SSL_IS_FIRST_HANDSHAKE(sc
) || !sc
->server
2717 || !SSL_CONNECTION_IS_TLS13(sc
))
2719 sc
->ext
.extra_tickets_expected
++;
2720 if (!RECORD_LAYER_write_pending(&sc
->rlayer
) && !SSL_in_init(s
))
2721 ossl_statem_set_in_init(sc
, 1);
2725 long SSL_ctrl(SSL
*s
, int cmd
, long larg
, void *parg
)
2728 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2730 /* TODO(QUIC): Special handling for some ctrls will be needed */
2735 case SSL_CTRL_GET_READ_AHEAD
:
2736 return RECORD_LAYER_get_read_ahead(&sc
->rlayer
);
2737 case SSL_CTRL_SET_READ_AHEAD
:
2738 l
= RECORD_LAYER_get_read_ahead(&sc
->rlayer
);
2739 RECORD_LAYER_set_read_ahead(&sc
->rlayer
, larg
);
2742 case SSL_CTRL_SET_MSG_CALLBACK_ARG
:
2743 sc
->msg_callback_arg
= parg
;
2747 return (sc
->mode
|= larg
);
2748 case SSL_CTRL_CLEAR_MODE
:
2749 return (sc
->mode
&= ~larg
);
2750 case SSL_CTRL_GET_MAX_CERT_LIST
:
2751 return (long)sc
->max_cert_list
;
2752 case SSL_CTRL_SET_MAX_CERT_LIST
:
2755 l
= (long)sc
->max_cert_list
;
2756 sc
->max_cert_list
= (size_t)larg
;
2758 case SSL_CTRL_SET_MAX_SEND_FRAGMENT
:
2759 if (larg
< 512 || larg
> SSL3_RT_MAX_PLAIN_LENGTH
)
2761 #ifndef OPENSSL_NO_KTLS
2762 if (sc
->wbio
!= NULL
&& BIO_get_ktls_send(sc
->wbio
))
2764 #endif /* OPENSSL_NO_KTLS */
2765 sc
->max_send_fragment
= larg
;
2766 if (sc
->max_send_fragment
< sc
->split_send_fragment
)
2767 sc
->split_send_fragment
= sc
->max_send_fragment
;
2769 case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT
:
2770 if ((size_t)larg
> sc
->max_send_fragment
|| larg
== 0)
2772 sc
->split_send_fragment
= larg
;
2774 case SSL_CTRL_SET_MAX_PIPELINES
:
2775 if (larg
< 1 || larg
> SSL_MAX_PIPELINES
)
2777 sc
->max_pipelines
= larg
;
2779 RECORD_LAYER_set_read_ahead(&sc
->rlayer
, 1);
2781 case SSL_CTRL_GET_RI_SUPPORT
:
2782 return sc
->s3
.send_connection_binding
;
2783 case SSL_CTRL_SET_RETRY_VERIFY
:
2784 sc
->rwstate
= SSL_RETRY_VERIFY
;
2786 case SSL_CTRL_CERT_FLAGS
:
2787 return (sc
->cert
->cert_flags
|= larg
);
2788 case SSL_CTRL_CLEAR_CERT_FLAGS
:
2789 return (sc
->cert
->cert_flags
&= ~larg
);
2791 case SSL_CTRL_GET_RAW_CIPHERLIST
:
2793 if (sc
->s3
.tmp
.ciphers_raw
== NULL
)
2795 *(unsigned char **)parg
= sc
->s3
.tmp
.ciphers_raw
;
2796 return (int)sc
->s3
.tmp
.ciphers_rawlen
;
2798 return TLS_CIPHER_LEN
;
2800 case SSL_CTRL_GET_EXTMS_SUPPORT
:
2801 if (!sc
->session
|| SSL_in_init(s
) || ossl_statem_get_in_handshake(sc
))
2803 if (sc
->session
->flags
& SSL_SESS_FLAG_EXTMS
)
2807 case SSL_CTRL_SET_MIN_PROTO_VERSION
:
2808 return ssl_check_allowed_versions(larg
, sc
->max_proto_version
)
2809 && ssl_set_version_bound(s
->ctx
->method
->version
, (int)larg
,
2810 &sc
->min_proto_version
);
2811 case SSL_CTRL_GET_MIN_PROTO_VERSION
:
2812 return sc
->min_proto_version
;
2813 case SSL_CTRL_SET_MAX_PROTO_VERSION
:
2814 return ssl_check_allowed_versions(sc
->min_proto_version
, larg
)
2815 && ssl_set_version_bound(s
->ctx
->method
->version
, (int)larg
,
2816 &sc
->max_proto_version
);
2817 case SSL_CTRL_GET_MAX_PROTO_VERSION
:
2818 return sc
->max_proto_version
;
2820 return s
->method
->ssl_ctrl(s
, cmd
, larg
, parg
);
2824 long SSL_callback_ctrl(SSL
*s
, int cmd
, void (*fp
) (void))
2826 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
2832 case SSL_CTRL_SET_MSG_CALLBACK
:
2833 sc
->msg_callback
= (void (*)
2834 (int write_p
, int version
, int content_type
,
2835 const void *buf
, size_t len
, SSL
*ssl
,
2840 return s
->method
->ssl_callback_ctrl(s
, cmd
, fp
);
2844 LHASH_OF(SSL_SESSION
) *SSL_CTX_sessions(SSL_CTX
*ctx
)
2846 return ctx
->sessions
;
2849 static int ssl_tsan_load(SSL_CTX
*ctx
, TSAN_QUALIFIER
int *stat
)
2853 if (ssl_tsan_lock(ctx
)) {
2854 res
= tsan_load(stat
);
2855 ssl_tsan_unlock(ctx
);
2860 long SSL_CTX_ctrl(SSL_CTX
*ctx
, int cmd
, long larg
, void *parg
)
2863 /* For some cases with ctx == NULL perform syntax checks */
2866 case SSL_CTRL_SET_GROUPS_LIST
:
2867 return tls1_set_groups_list(ctx
, NULL
, NULL
, parg
);
2868 case SSL_CTRL_SET_SIGALGS_LIST
:
2869 case SSL_CTRL_SET_CLIENT_SIGALGS_LIST
:
2870 return tls1_set_sigalgs_list(NULL
, parg
, 0);
2877 case SSL_CTRL_GET_READ_AHEAD
:
2878 return ctx
->read_ahead
;
2879 case SSL_CTRL_SET_READ_AHEAD
:
2880 l
= ctx
->read_ahead
;
2881 ctx
->read_ahead
= larg
;
2884 case SSL_CTRL_SET_MSG_CALLBACK_ARG
:
2885 ctx
->msg_callback_arg
= parg
;
2888 case SSL_CTRL_GET_MAX_CERT_LIST
:
2889 return (long)ctx
->max_cert_list
;
2890 case SSL_CTRL_SET_MAX_CERT_LIST
:
2893 l
= (long)ctx
->max_cert_list
;
2894 ctx
->max_cert_list
= (size_t)larg
;
2897 case SSL_CTRL_SET_SESS_CACHE_SIZE
:
2900 l
= (long)ctx
->session_cache_size
;
2901 ctx
->session_cache_size
= (size_t)larg
;
2903 case SSL_CTRL_GET_SESS_CACHE_SIZE
:
2904 return (long)ctx
->session_cache_size
;
2905 case SSL_CTRL_SET_SESS_CACHE_MODE
:
2906 l
= ctx
->session_cache_mode
;
2907 ctx
->session_cache_mode
= larg
;
2909 case SSL_CTRL_GET_SESS_CACHE_MODE
:
2910 return ctx
->session_cache_mode
;
2912 case SSL_CTRL_SESS_NUMBER
:
2913 return lh_SSL_SESSION_num_items(ctx
->sessions
);
2914 case SSL_CTRL_SESS_CONNECT
:
2915 return ssl_tsan_load(ctx
, &ctx
->stats
.sess_connect
);
2916 case SSL_CTRL_SESS_CONNECT_GOOD
:
2917 return ssl_tsan_load(ctx
, &ctx
->stats
.sess_connect_good
);
2918 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE
:
2919 return ssl_tsan_load(ctx
, &ctx
->stats
.sess_connect_renegotiate
);
2920 case SSL_CTRL_SESS_ACCEPT
:
2921 return ssl_tsan_load(ctx
, &ctx
->stats
.sess_accept
);
2922 case SSL_CTRL_SESS_ACCEPT_GOOD
:
2923 return ssl_tsan_load(ctx
, &ctx
->stats
.sess_accept_good
);
2924 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE
:
2925 return ssl_tsan_load(ctx
, &ctx
->stats
.sess_accept_renegotiate
);
2926 case SSL_CTRL_SESS_HIT
:
2927 return ssl_tsan_load(ctx
, &ctx
->stats
.sess_hit
);
2928 case SSL_CTRL_SESS_CB_HIT
:
2929 return ssl_tsan_load(ctx
, &ctx
->stats
.sess_cb_hit
);
2930 case SSL_CTRL_SESS_MISSES
:
2931 return ssl_tsan_load(ctx
, &ctx
->stats
.sess_miss
);
2932 case SSL_CTRL_SESS_TIMEOUTS
:
2933 return ssl_tsan_load(ctx
, &ctx
->stats
.sess_timeout
);
2934 case SSL_CTRL_SESS_CACHE_FULL
:
2935 return ssl_tsan_load(ctx
, &ctx
->stats
.sess_cache_full
);
2937 return (ctx
->mode
|= larg
);
2938 case SSL_CTRL_CLEAR_MODE
:
2939 return (ctx
->mode
&= ~larg
);
2940 case SSL_CTRL_SET_MAX_SEND_FRAGMENT
:
2941 if (larg
< 512 || larg
> SSL3_RT_MAX_PLAIN_LENGTH
)
2943 ctx
->max_send_fragment
= larg
;
2944 if (ctx
->max_send_fragment
< ctx
->split_send_fragment
)
2945 ctx
->split_send_fragment
= ctx
->max_send_fragment
;
2947 case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT
:
2948 if ((size_t)larg
> ctx
->max_send_fragment
|| larg
== 0)
2950 ctx
->split_send_fragment
= larg
;
2952 case SSL_CTRL_SET_MAX_PIPELINES
:
2953 if (larg
< 1 || larg
> SSL_MAX_PIPELINES
)
2955 ctx
->max_pipelines
= larg
;
2957 case SSL_CTRL_CERT_FLAGS
:
2958 return (ctx
->cert
->cert_flags
|= larg
);
2959 case SSL_CTRL_CLEAR_CERT_FLAGS
:
2960 return (ctx
->cert
->cert_flags
&= ~larg
);
2961 case SSL_CTRL_SET_MIN_PROTO_VERSION
:
2962 return ssl_check_allowed_versions(larg
, ctx
->max_proto_version
)
2963 && ssl_set_version_bound(ctx
->method
->version
, (int)larg
,
2964 &ctx
->min_proto_version
);
2965 case SSL_CTRL_GET_MIN_PROTO_VERSION
:
2966 return ctx
->min_proto_version
;
2967 case SSL_CTRL_SET_MAX_PROTO_VERSION
:
2968 return ssl_check_allowed_versions(ctx
->min_proto_version
, larg
)
2969 && ssl_set_version_bound(ctx
->method
->version
, (int)larg
,
2970 &ctx
->max_proto_version
);
2971 case SSL_CTRL_GET_MAX_PROTO_VERSION
:
2972 return ctx
->max_proto_version
;
2974 return ctx
->method
->ssl_ctx_ctrl(ctx
, cmd
, larg
, parg
);
2978 long SSL_CTX_callback_ctrl(SSL_CTX
*ctx
, int cmd
, void (*fp
) (void))
2981 case SSL_CTRL_SET_MSG_CALLBACK
:
2982 ctx
->msg_callback
= (void (*)
2983 (int write_p
, int version
, int content_type
,
2984 const void *buf
, size_t len
, SSL
*ssl
,
2989 return ctx
->method
->ssl_ctx_callback_ctrl(ctx
, cmd
, fp
);
2993 int ssl_cipher_id_cmp(const SSL_CIPHER
*a
, const SSL_CIPHER
*b
)
3002 int ssl_cipher_ptr_id_cmp(const SSL_CIPHER
*const *ap
,
3003 const SSL_CIPHER
*const *bp
)
3005 if ((*ap
)->id
> (*bp
)->id
)
3007 if ((*ap
)->id
< (*bp
)->id
)
3013 * return a STACK of the ciphers available for the SSL and in order of
3016 STACK_OF(SSL_CIPHER
) *SSL_get_ciphers(const SSL
*s
)
3018 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
3021 if (sc
->cipher_list
!= NULL
) {
3022 return sc
->cipher_list
;
3023 } else if ((s
->ctx
!= NULL
) && (s
->ctx
->cipher_list
!= NULL
)) {
3024 return s
->ctx
->cipher_list
;
3030 STACK_OF(SSL_CIPHER
) *SSL_get_client_ciphers(const SSL
*s
)
3032 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
3034 if (sc
== NULL
|| !sc
->server
)
3036 return sc
->peer_ciphers
;
3039 STACK_OF(SSL_CIPHER
) *SSL_get1_supported_ciphers(SSL
*s
)
3041 STACK_OF(SSL_CIPHER
) *sk
= NULL
, *ciphers
;
3043 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
3048 ciphers
= SSL_get_ciphers(s
);
3051 if (!ssl_set_client_disabled(sc
))
3053 for (i
= 0; i
< sk_SSL_CIPHER_num(ciphers
); i
++) {
3054 const SSL_CIPHER
*c
= sk_SSL_CIPHER_value(ciphers
, i
);
3055 if (!ssl_cipher_disabled(sc
, c
, SSL_SECOP_CIPHER_SUPPORTED
, 0)) {
3057 sk
= sk_SSL_CIPHER_new_null();
3060 if (!sk_SSL_CIPHER_push(sk
, c
)) {
3061 sk_SSL_CIPHER_free(sk
);
3069 /** return a STACK of the ciphers available for the SSL and in order of
3071 STACK_OF(SSL_CIPHER
) *ssl_get_ciphers_by_id(SSL_CONNECTION
*s
)
3074 if (s
->cipher_list_by_id
!= NULL
)
3075 return s
->cipher_list_by_id
;
3076 else if (s
->ssl
.ctx
!= NULL
3077 && s
->ssl
.ctx
->cipher_list_by_id
!= NULL
)
3078 return s
->ssl
.ctx
->cipher_list_by_id
;
3083 /** The old interface to get the same thing as SSL_get_ciphers() */
3084 const char *SSL_get_cipher_list(const SSL
*s
, int n
)
3086 const SSL_CIPHER
*c
;
3087 STACK_OF(SSL_CIPHER
) *sk
;
3091 sk
= SSL_get_ciphers(s
);
3092 if ((sk
== NULL
) || (sk_SSL_CIPHER_num(sk
) <= n
))
3094 c
= sk_SSL_CIPHER_value(sk
, n
);
3100 /** return a STACK of the ciphers available for the SSL_CTX and in order of
3102 STACK_OF(SSL_CIPHER
) *SSL_CTX_get_ciphers(const SSL_CTX
*ctx
)
3105 return ctx
->cipher_list
;
3110 * Distinguish between ciphers controlled by set_ciphersuite() and
3111 * set_cipher_list() when counting.
3113 static int cipher_list_tls12_num(STACK_OF(SSL_CIPHER
) *sk
)
3116 const SSL_CIPHER
*c
;
3120 for (i
= 0; i
< sk_SSL_CIPHER_num(sk
); ++i
) {
3121 c
= sk_SSL_CIPHER_value(sk
, i
);
3122 if (c
->min_tls
>= TLS1_3_VERSION
)
3129 /** specify the ciphers to be used by default by the SSL_CTX */
3130 int SSL_CTX_set_cipher_list(SSL_CTX
*ctx
, const char *str
)
3132 STACK_OF(SSL_CIPHER
) *sk
;
3134 sk
= ssl_create_cipher_list(ctx
, ctx
->tls13_ciphersuites
,
3135 &ctx
->cipher_list
, &ctx
->cipher_list_by_id
, str
,
3138 * ssl_create_cipher_list may return an empty stack if it was unable to
3139 * find a cipher matching the given rule string (for example if the rule
3140 * string specifies a cipher which has been disabled). This is not an
3141 * error as far as ssl_create_cipher_list is concerned, and hence
3142 * ctx->cipher_list and ctx->cipher_list_by_id has been updated.
3146 else if (cipher_list_tls12_num(sk
) == 0) {
3147 ERR_raise(ERR_LIB_SSL
, SSL_R_NO_CIPHER_MATCH
);
3153 /** specify the ciphers to be used by the SSL */
3154 int SSL_set_cipher_list(SSL
*s
, const char *str
)
3156 STACK_OF(SSL_CIPHER
) *sk
;
3157 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
3162 sk
= ssl_create_cipher_list(s
->ctx
, sc
->tls13_ciphersuites
,
3163 &sc
->cipher_list
, &sc
->cipher_list_by_id
, str
,
3165 /* see comment in SSL_CTX_set_cipher_list */
3168 else if (cipher_list_tls12_num(sk
) == 0) {
3169 ERR_raise(ERR_LIB_SSL
, SSL_R_NO_CIPHER_MATCH
);
3175 char *SSL_get_shared_ciphers(const SSL
*s
, char *buf
, int size
)
3178 STACK_OF(SSL_CIPHER
) *clntsk
, *srvrsk
;
3179 const SSL_CIPHER
*c
;
3181 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
3187 || sc
->peer_ciphers
== NULL
3192 clntsk
= sc
->peer_ciphers
;
3193 srvrsk
= SSL_get_ciphers(s
);
3194 if (clntsk
== NULL
|| srvrsk
== NULL
)
3197 if (sk_SSL_CIPHER_num(clntsk
) == 0 || sk_SSL_CIPHER_num(srvrsk
) == 0)
3200 for (i
= 0; i
< sk_SSL_CIPHER_num(clntsk
); i
++) {
3203 c
= sk_SSL_CIPHER_value(clntsk
, i
);
3204 if (sk_SSL_CIPHER_find(srvrsk
, c
) < 0)
3207 n
= strlen(c
->name
);
3224 * Return the requested servername (SNI) value. Note that the behaviour varies
3226 * - whether this is called by the client or the server,
3227 * - if we are before or during/after the handshake,
3228 * - if a resumption or normal handshake is being attempted/has occurred
3229 * - whether we have negotiated TLSv1.2 (or below) or TLSv1.3
3231 * Note that only the host_name type is defined (RFC 3546).
3233 const char *SSL_get_servername(const SSL
*s
, const int type
)
3235 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
3242 * If we don't know if we are the client or the server yet then we assume
3245 server
= sc
->handshake_func
== NULL
? 0 : sc
->server
;
3247 if (type
!= TLSEXT_NAMETYPE_host_name
)
3253 * In TLSv1.3 on the server SNI is not associated with the session
3254 * but in TLSv1.2 or below it is.
3256 * Before the handshake:
3259 * During/after the handshake (TLSv1.2 or below resumption occurred):
3260 * - If a servername was accepted by the server in the original
3261 * handshake then it will return that servername, or NULL otherwise.
3263 * During/after the handshake (TLSv1.2 or below resumption did not occur):
3264 * - The function will return the servername requested by the client in
3265 * this handshake or NULL if none was requested.
3267 if (sc
->hit
&& !SSL_CONNECTION_IS_TLS13(sc
))
3268 return sc
->session
->ext
.hostname
;
3273 * Before the handshake:
3274 * - If a servername has been set via a call to
3275 * SSL_set_tlsext_host_name() then it will return that servername
3276 * - If one has not been set, but a TLSv1.2 resumption is being
3277 * attempted and the session from the original handshake had a
3278 * servername accepted by the server then it will return that
3280 * - Otherwise it returns NULL
3282 * During/after the handshake (TLSv1.2 or below resumption occurred):
3283 * - If the session from the original handshake had a servername accepted
3284 * by the server then it will return that servername.
3285 * - Otherwise it returns the servername set via
3286 * SSL_set_tlsext_host_name() (or NULL if it was not called).
3288 * During/after the handshake (TLSv1.2 or below resumption did not occur):
3289 * - It will return the servername set via SSL_set_tlsext_host_name()
3290 * (or NULL if it was not called).
3292 if (SSL_in_before(s
)) {
3293 if (sc
->ext
.hostname
== NULL
3294 && sc
->session
!= NULL
3295 && sc
->session
->ssl_version
!= TLS1_3_VERSION
)
3296 return sc
->session
->ext
.hostname
;
3298 if (!SSL_CONNECTION_IS_TLS13(sc
) && sc
->hit
3299 && sc
->session
->ext
.hostname
!= NULL
)
3300 return sc
->session
->ext
.hostname
;
3304 return sc
->ext
.hostname
;
3307 int SSL_get_servername_type(const SSL
*s
)
3309 if (SSL_get_servername(s
, TLSEXT_NAMETYPE_host_name
) != NULL
)
3310 return TLSEXT_NAMETYPE_host_name
;
3315 * SSL_select_next_proto implements the standard protocol selection. It is
3316 * expected that this function is called from the callback set by
3317 * SSL_CTX_set_next_proto_select_cb. The protocol data is assumed to be a
3318 * vector of 8-bit, length prefixed byte strings. The length byte itself is
3319 * not included in the length. A byte string of length 0 is invalid. No byte
3320 * string may be truncated. The current, but experimental algorithm for
3321 * selecting the protocol is: 1) If the server doesn't support NPN then this
3322 * is indicated to the callback. In this case, the client application has to
3323 * abort the connection or have a default application level protocol. 2) If
3324 * the server supports NPN, but advertises an empty list then the client
3325 * selects the first protocol in its list, but indicates via the API that this
3326 * fallback case was enacted. 3) Otherwise, the client finds the first
3327 * protocol in the server's list that it supports and selects this protocol.
3328 * This is because it's assumed that the server has better information about
3329 * which protocol a client should use. 4) If the client doesn't support any
3330 * of the server's advertised protocols, then this is treated the same as
3331 * case 2. It returns either OPENSSL_NPN_NEGOTIATED if a common protocol was
3332 * found, or OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
3334 int SSL_select_next_proto(unsigned char **out
, unsigned char *outlen
,
3335 const unsigned char *server
,
3336 unsigned int server_len
,
3337 const unsigned char *client
, unsigned int client_len
)
3340 const unsigned char *result
;
3341 int status
= OPENSSL_NPN_UNSUPPORTED
;
3344 * For each protocol in server preference order, see if we support it.
3346 for (i
= 0; i
< server_len
;) {
3347 for (j
= 0; j
< client_len
;) {
3348 if (server
[i
] == client
[j
] &&
3349 memcmp(&server
[i
+ 1], &client
[j
+ 1], server
[i
]) == 0) {
3350 /* We found a match */
3351 result
= &server
[i
];
3352 status
= OPENSSL_NPN_NEGOTIATED
;
3362 /* There's no overlap between our protocols and the server's list. */
3364 status
= OPENSSL_NPN_NO_OVERLAP
;
3367 *out
= (unsigned char *)result
+ 1;
3368 *outlen
= result
[0];
3372 #ifndef OPENSSL_NO_NEXTPROTONEG
3374 * SSL_get0_next_proto_negotiated sets *data and *len to point to the
3375 * client's requested protocol for this connection and returns 0. If the
3376 * client didn't request any protocol, then *data is set to NULL. Note that
3377 * the client can request any protocol it chooses. The value returned from
3378 * this function need not be a member of the list of supported protocols
3379 * provided by the callback.
3381 void SSL_get0_next_proto_negotiated(const SSL
*s
, const unsigned char **data
,
3384 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
3387 /* We have no other way to indicate error */
3393 *data
= sc
->ext
.npn
;
3394 if (*data
== NULL
) {
3397 *len
= (unsigned int)sc
->ext
.npn_len
;
3402 * SSL_CTX_set_npn_advertised_cb sets a callback that is called when
3403 * a TLS server needs a list of supported protocols for Next Protocol
3404 * Negotiation. The returned list must be in wire format. The list is
3405 * returned by setting |out| to point to it and |outlen| to its length. This
3406 * memory will not be modified, but one should assume that the SSL* keeps a
3407 * reference to it. The callback should return SSL_TLSEXT_ERR_OK if it
3408 * wishes to advertise. Otherwise, no such extension will be included in the
3411 void SSL_CTX_set_npn_advertised_cb(SSL_CTX
*ctx
,
3412 SSL_CTX_npn_advertised_cb_func cb
,
3415 ctx
->ext
.npn_advertised_cb
= cb
;
3416 ctx
->ext
.npn_advertised_cb_arg
= arg
;
3420 * SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
3421 * client needs to select a protocol from the server's provided list. |out|
3422 * must be set to point to the selected protocol (which may be within |in|).
3423 * The length of the protocol name must be written into |outlen|. The
3424 * server's advertised protocols are provided in |in| and |inlen|. The
3425 * callback can assume that |in| is syntactically valid. The client must
3426 * select a protocol. It is fatal to the connection if this callback returns
3427 * a value other than SSL_TLSEXT_ERR_OK.
3429 void SSL_CTX_set_npn_select_cb(SSL_CTX
*ctx
,
3430 SSL_CTX_npn_select_cb_func cb
,
3433 ctx
->ext
.npn_select_cb
= cb
;
3434 ctx
->ext
.npn_select_cb_arg
= arg
;
3438 static int alpn_value_ok(const unsigned char *protos
, unsigned int protos_len
)
3442 if (protos_len
< 2 || protos
== NULL
)
3445 for (idx
= 0; idx
< protos_len
; idx
+= protos
[idx
] + 1) {
3446 if (protos
[idx
] == 0)
3449 return idx
== protos_len
;
3452 * SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
3453 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
3454 * length-prefixed strings). Returns 0 on success.
3456 int SSL_CTX_set_alpn_protos(SSL_CTX
*ctx
, const unsigned char *protos
,
3457 unsigned int protos_len
)
3459 unsigned char *alpn
;
3461 if (protos_len
== 0 || protos
== NULL
) {
3462 OPENSSL_free(ctx
->ext
.alpn
);
3463 ctx
->ext
.alpn
= NULL
;
3464 ctx
->ext
.alpn_len
= 0;
3467 /* Not valid per RFC */
3468 if (!alpn_value_ok(protos
, protos_len
))
3471 alpn
= OPENSSL_memdup(protos
, protos_len
);
3473 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
3476 OPENSSL_free(ctx
->ext
.alpn
);
3477 ctx
->ext
.alpn
= alpn
;
3478 ctx
->ext
.alpn_len
= protos_len
;
3484 * SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
3485 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
3486 * length-prefixed strings). Returns 0 on success.
3488 int SSL_set_alpn_protos(SSL
*ssl
, const unsigned char *protos
,
3489 unsigned int protos_len
)
3491 unsigned char *alpn
;
3492 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
3497 if (protos_len
== 0 || protos
== NULL
) {
3498 OPENSSL_free(sc
->ext
.alpn
);
3499 sc
->ext
.alpn
= NULL
;
3500 sc
->ext
.alpn_len
= 0;
3503 /* Not valid per RFC */
3504 if (!alpn_value_ok(protos
, protos_len
))
3507 alpn
= OPENSSL_memdup(protos
, protos_len
);
3509 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
3512 OPENSSL_free(sc
->ext
.alpn
);
3513 sc
->ext
.alpn
= alpn
;
3514 sc
->ext
.alpn_len
= protos_len
;
3520 * SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is
3521 * called during ClientHello processing in order to select an ALPN protocol
3522 * from the client's list of offered protocols.
3524 void SSL_CTX_set_alpn_select_cb(SSL_CTX
*ctx
,
3525 SSL_CTX_alpn_select_cb_func cb
,
3528 ctx
->ext
.alpn_select_cb
= cb
;
3529 ctx
->ext
.alpn_select_cb_arg
= arg
;
3533 * SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
3534 * On return it sets |*data| to point to |*len| bytes of protocol name
3535 * (not including the leading length-prefix byte). If the server didn't
3536 * respond with a negotiated protocol then |*len| will be zero.
3538 void SSL_get0_alpn_selected(const SSL
*ssl
, const unsigned char **data
,
3541 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(ssl
);
3544 /* We have no other way to indicate error */
3550 *data
= sc
->s3
.alpn_selected
;
3554 *len
= (unsigned int)sc
->s3
.alpn_selected_len
;
3557 int SSL_export_keying_material(SSL
*s
, unsigned char *out
, size_t olen
,
3558 const char *label
, size_t llen
,
3559 const unsigned char *context
, size_t contextlen
,
3562 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
3567 if (sc
->session
== NULL
3568 || (sc
->version
< TLS1_VERSION
&& sc
->version
!= DTLS1_BAD_VER
))
3571 return s
->method
->ssl3_enc
->export_keying_material(sc
, out
, olen
, label
,
3573 contextlen
, use_context
);
3576 int SSL_export_keying_material_early(SSL
*s
, unsigned char *out
, size_t olen
,
3577 const char *label
, size_t llen
,
3578 const unsigned char *context
,
3581 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
3586 if (sc
->version
!= TLS1_3_VERSION
)
3589 return tls13_export_keying_material_early(sc
, out
, olen
, label
, llen
,
3590 context
, contextlen
);
3593 static unsigned long ssl_session_hash(const SSL_SESSION
*a
)
3595 const unsigned char *session_id
= a
->session_id
;
3597 unsigned char tmp_storage
[4];
3599 if (a
->session_id_length
< sizeof(tmp_storage
)) {
3600 memset(tmp_storage
, 0, sizeof(tmp_storage
));
3601 memcpy(tmp_storage
, a
->session_id
, a
->session_id_length
);
3602 session_id
= tmp_storage
;
3606 ((unsigned long)session_id
[0]) |
3607 ((unsigned long)session_id
[1] << 8L) |
3608 ((unsigned long)session_id
[2] << 16L) |
3609 ((unsigned long)session_id
[3] << 24L);
3614 * NB: If this function (or indeed the hash function which uses a sort of
3615 * coarser function than this one) is changed, ensure
3616 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on
3617 * being able to construct an SSL_SESSION that will collide with any existing
3618 * session with a matching session ID.
3620 static int ssl_session_cmp(const SSL_SESSION
*a
, const SSL_SESSION
*b
)
3622 if (a
->ssl_version
!= b
->ssl_version
)
3624 if (a
->session_id_length
!= b
->session_id_length
)
3626 return memcmp(a
->session_id
, b
->session_id
, a
->session_id_length
);
3630 * These wrapper functions should remain rather than redeclaring
3631 * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
3632 * variable. The reason is that the functions aren't static, they're exposed
3636 SSL_CTX
*SSL_CTX_new_ex(OSSL_LIB_CTX
*libctx
, const char *propq
,
3637 const SSL_METHOD
*meth
)
3639 SSL_CTX
*ret
= NULL
;
3642 ERR_raise(ERR_LIB_SSL
, SSL_R_NULL_SSL_METHOD_PASSED
);
3646 if (!OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS
, NULL
))
3649 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
3650 ERR_raise(ERR_LIB_SSL
, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS
);
3653 ret
= OPENSSL_zalloc(sizeof(*ret
));
3657 /* Init the reference counting before any call to SSL_CTX_free */
3658 ret
->references
= 1;
3659 ret
->lock
= CRYPTO_THREAD_lock_new();
3660 if (ret
->lock
== NULL
) {
3661 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
3666 #ifdef TSAN_REQUIRES_LOCKING
3667 ret
->tsan_lock
= CRYPTO_THREAD_lock_new();
3668 if (ret
->tsan_lock
== NULL
) {
3669 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
3674 ret
->libctx
= libctx
;
3675 if (propq
!= NULL
) {
3676 ret
->propq
= OPENSSL_strdup(propq
);
3677 if (ret
->propq
== NULL
)
3682 ret
->min_proto_version
= 0;
3683 ret
->max_proto_version
= 0;
3684 ret
->mode
= SSL_MODE_AUTO_RETRY
;
3685 ret
->session_cache_mode
= SSL_SESS_CACHE_SERVER
;
3686 ret
->session_cache_size
= SSL_SESSION_CACHE_MAX_SIZE_DEFAULT
;
3687 /* We take the system default. */
3688 ret
->session_timeout
= meth
->get_timeout();
3689 ret
->max_cert_list
= SSL_MAX_CERT_LIST_DEFAULT
;
3690 ret
->verify_mode
= SSL_VERIFY_NONE
;
3691 if ((ret
->cert
= ssl_cert_new()) == NULL
)
3694 ret
->sessions
= lh_SSL_SESSION_new(ssl_session_hash
, ssl_session_cmp
);
3695 if (ret
->sessions
== NULL
)
3697 ret
->cert_store
= X509_STORE_new();
3698 if (ret
->cert_store
== NULL
)
3700 #ifndef OPENSSL_NO_CT
3701 ret
->ctlog_store
= CTLOG_STORE_new_ex(libctx
, propq
);
3702 if (ret
->ctlog_store
== NULL
)
3706 /* initialize cipher/digest methods table */
3707 if (!ssl_load_ciphers(ret
))
3709 /* initialise sig algs */
3710 if (!ssl_setup_sig_algs(ret
))
3714 if (!ssl_load_groups(ret
))
3717 if (!SSL_CTX_set_ciphersuites(ret
, OSSL_default_ciphersuites()))
3720 if (!ssl_create_cipher_list(ret
,
3721 ret
->tls13_ciphersuites
,
3722 &ret
->cipher_list
, &ret
->cipher_list_by_id
,
3723 OSSL_default_cipher_list(), ret
->cert
)
3724 || sk_SSL_CIPHER_num(ret
->cipher_list
) <= 0) {
3725 ERR_raise(ERR_LIB_SSL
, SSL_R_LIBRARY_HAS_NO_CIPHERS
);
3729 ret
->param
= X509_VERIFY_PARAM_new();
3730 if (ret
->param
== NULL
)
3734 * If these aren't available from the provider we'll get NULL returns.
3735 * That's fine but will cause errors later if SSLv3 is negotiated
3737 ret
->md5
= ssl_evp_md_fetch(libctx
, NID_md5
, propq
);
3738 ret
->sha1
= ssl_evp_md_fetch(libctx
, NID_sha1
, propq
);
3740 if ((ret
->ca_names
= sk_X509_NAME_new_null()) == NULL
)
3743 if ((ret
->client_ca_names
= sk_X509_NAME_new_null()) == NULL
)
3746 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX
, ret
, &ret
->ex_data
))
3749 if ((ret
->ext
.secure
= OPENSSL_secure_zalloc(sizeof(*ret
->ext
.secure
))) == NULL
)
3752 /* No compression for DTLS */
3753 if (!(meth
->ssl3_enc
->enc_flags
& SSL_ENC_FLAG_DTLS
))
3754 ret
->comp_methods
= SSL_COMP_get_compression_methods();
3756 ret
->max_send_fragment
= SSL3_RT_MAX_PLAIN_LENGTH
;
3757 ret
->split_send_fragment
= SSL3_RT_MAX_PLAIN_LENGTH
;
3759 /* Setup RFC5077 ticket keys */
3760 if ((RAND_bytes_ex(libctx
, ret
->ext
.tick_key_name
,
3761 sizeof(ret
->ext
.tick_key_name
), 0) <= 0)
3762 || (RAND_priv_bytes_ex(libctx
, ret
->ext
.secure
->tick_hmac_key
,
3763 sizeof(ret
->ext
.secure
->tick_hmac_key
), 0) <= 0)
3764 || (RAND_priv_bytes_ex(libctx
, ret
->ext
.secure
->tick_aes_key
,
3765 sizeof(ret
->ext
.secure
->tick_aes_key
), 0) <= 0))
3766 ret
->options
|= SSL_OP_NO_TICKET
;
3768 if (RAND_priv_bytes_ex(libctx
, ret
->ext
.cookie_hmac_key
,
3769 sizeof(ret
->ext
.cookie_hmac_key
), 0) <= 0)
3772 #ifndef OPENSSL_NO_SRP
3773 if (!ssl_ctx_srp_ctx_init_intern(ret
))
3776 #ifndef OPENSSL_NO_ENGINE
3777 # ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO
3778 # define eng_strx(x) #x
3779 # define eng_str(x) eng_strx(x)
3780 /* Use specific client engine automatically... ignore errors */
3783 eng
= ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO
));
3786 ENGINE_load_builtin_engines();
3787 eng
= ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO
));
3789 if (!eng
|| !SSL_CTX_set_client_cert_engine(ret
, eng
))
3795 * Disable compression by default to prevent CRIME. Applications can
3796 * re-enable compression by configuring
3797 * SSL_CTX_clear_options(ctx, SSL_OP_NO_COMPRESSION);
3798 * or by using the SSL_CONF library. Similarly we also enable TLSv1.3
3799 * middlebox compatibility by default. This may be disabled by default in
3800 * a later OpenSSL version.
3802 ret
->options
|= SSL_OP_NO_COMPRESSION
| SSL_OP_ENABLE_MIDDLEBOX_COMPAT
;
3804 ret
->ext
.status_type
= TLSEXT_STATUSTYPE_nothing
;
3807 * We cannot usefully set a default max_early_data here (which gets
3808 * propagated in SSL_new(), for the following reason: setting the
3809 * SSL field causes tls_construct_stoc_early_data() to tell the
3810 * client that early data will be accepted when constructing a TLS 1.3
3811 * session ticket, and the client will accordingly send us early data
3812 * when using that ticket (if the client has early data to send).
3813 * However, in order for the early data to actually be consumed by
3814 * the application, the application must also have calls to
3815 * SSL_read_early_data(); otherwise we'll just skip past the early data
3816 * and ignore it. So, since the application must add calls to
3817 * SSL_read_early_data(), we also require them to add
3818 * calls to SSL_CTX_set_max_early_data() in order to use early data,
3819 * eliminating the bandwidth-wasting early data in the case described
3822 ret
->max_early_data
= 0;
3825 * Default recv_max_early_data is a fully loaded single record. Could be
3826 * split across multiple records in practice. We set this differently to
3827 * max_early_data so that, in the default case, we do not advertise any
3828 * support for early_data, but if a client were to send us some (e.g.
3829 * because of an old, stale ticket) then we will tolerate it and skip over
3832 ret
->recv_max_early_data
= SSL3_RT_MAX_PLAIN_LENGTH
;
3834 /* By default we send two session tickets automatically in TLSv1.3 */
3835 ret
->num_tickets
= 2;
3837 ssl_ctx_system_config(ret
);
3841 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
3847 SSL_CTX
*SSL_CTX_new(const SSL_METHOD
*meth
)
3849 return SSL_CTX_new_ex(NULL
, NULL
, meth
);
3852 int SSL_CTX_up_ref(SSL_CTX
*ctx
)
3856 if (CRYPTO_UP_REF(&ctx
->references
, &i
, ctx
->lock
) <= 0)
3859 REF_PRINT_COUNT("SSL_CTX", ctx
);
3860 REF_ASSERT_ISNT(i
< 2);
3861 return ((i
> 1) ? 1 : 0);
3864 void SSL_CTX_free(SSL_CTX
*a
)
3872 CRYPTO_DOWN_REF(&a
->references
, &i
, a
->lock
);
3873 REF_PRINT_COUNT("SSL_CTX", a
);
3876 REF_ASSERT_ISNT(i
< 0);
3878 X509_VERIFY_PARAM_free(a
->param
);
3879 dane_ctx_final(&a
->dane
);
3882 * Free internal session cache. However: the remove_cb() may reference
3883 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
3884 * after the sessions were flushed.
3885 * As the ex_data handling routines might also touch the session cache,
3886 * the most secure solution seems to be: empty (flush) the cache, then
3887 * free ex_data, then finally free the cache.
3888 * (See ticket [openssl.org #212].)
3890 if (a
->sessions
!= NULL
)
3891 SSL_CTX_flush_sessions(a
, 0);
3893 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX
, a
, &a
->ex_data
);
3894 lh_SSL_SESSION_free(a
->sessions
);
3895 X509_STORE_free(a
->cert_store
);
3896 #ifndef OPENSSL_NO_CT
3897 CTLOG_STORE_free(a
->ctlog_store
);
3899 sk_SSL_CIPHER_free(a
->cipher_list
);
3900 sk_SSL_CIPHER_free(a
->cipher_list_by_id
);
3901 sk_SSL_CIPHER_free(a
->tls13_ciphersuites
);
3902 ssl_cert_free(a
->cert
);
3903 sk_X509_NAME_pop_free(a
->ca_names
, X509_NAME_free
);
3904 sk_X509_NAME_pop_free(a
->client_ca_names
, X509_NAME_free
);
3905 OSSL_STACK_OF_X509_free(a
->extra_certs
);
3906 a
->comp_methods
= NULL
;
3907 #ifndef OPENSSL_NO_SRTP
3908 sk_SRTP_PROTECTION_PROFILE_free(a
->srtp_profiles
);
3910 #ifndef OPENSSL_NO_SRP
3911 ssl_ctx_srp_ctx_free_intern(a
);
3913 #ifndef OPENSSL_NO_ENGINE
3914 tls_engine_finish(a
->client_cert_engine
);
3917 OPENSSL_free(a
->ext
.ecpointformats
);
3918 OPENSSL_free(a
->ext
.supportedgroups
);
3919 OPENSSL_free(a
->ext
.supported_groups_default
);
3920 OPENSSL_free(a
->ext
.alpn
);
3921 OPENSSL_secure_free(a
->ext
.secure
);
3923 ssl_evp_md_free(a
->md5
);
3924 ssl_evp_md_free(a
->sha1
);
3926 for (j
= 0; j
< SSL_ENC_NUM_IDX
; j
++)
3927 ssl_evp_cipher_free(a
->ssl_cipher_methods
[j
]);
3928 for (j
= 0; j
< SSL_MD_NUM_IDX
; j
++)
3929 ssl_evp_md_free(a
->ssl_digest_methods
[j
]);
3930 for (j
= 0; j
< a
->group_list_len
; j
++) {
3931 OPENSSL_free(a
->group_list
[j
].tlsname
);
3932 OPENSSL_free(a
->group_list
[j
].realname
);
3933 OPENSSL_free(a
->group_list
[j
].algorithm
);
3935 OPENSSL_free(a
->group_list
);
3937 OPENSSL_free(a
->sigalg_lookup_cache
);
3939 CRYPTO_THREAD_lock_free(a
->lock
);
3940 #ifdef TSAN_REQUIRES_LOCKING
3941 CRYPTO_THREAD_lock_free(a
->tsan_lock
);
3944 OPENSSL_free(a
->propq
);
3949 void SSL_CTX_set_default_passwd_cb(SSL_CTX
*ctx
, pem_password_cb
*cb
)
3951 ctx
->default_passwd_callback
= cb
;
3954 void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX
*ctx
, void *u
)
3956 ctx
->default_passwd_callback_userdata
= u
;
3959 pem_password_cb
*SSL_CTX_get_default_passwd_cb(SSL_CTX
*ctx
)
3961 return ctx
->default_passwd_callback
;
3964 void *SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX
*ctx
)
3966 return ctx
->default_passwd_callback_userdata
;
3969 void SSL_set_default_passwd_cb(SSL
*s
, pem_password_cb
*cb
)
3971 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
3976 sc
->default_passwd_callback
= cb
;
3979 void SSL_set_default_passwd_cb_userdata(SSL
*s
, void *u
)
3981 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
3986 sc
->default_passwd_callback_userdata
= u
;
3989 pem_password_cb
*SSL_get_default_passwd_cb(SSL
*s
)
3991 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
3996 return sc
->default_passwd_callback
;
3999 void *SSL_get_default_passwd_cb_userdata(SSL
*s
)
4001 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
4006 return sc
->default_passwd_callback_userdata
;
4009 void SSL_CTX_set_cert_verify_callback(SSL_CTX
*ctx
,
4010 int (*cb
) (X509_STORE_CTX
*, void *),
4013 ctx
->app_verify_callback
= cb
;
4014 ctx
->app_verify_arg
= arg
;
4017 void SSL_CTX_set_verify(SSL_CTX
*ctx
, int mode
,
4018 int (*cb
) (int, X509_STORE_CTX
*))
4020 ctx
->verify_mode
= mode
;
4021 ctx
->default_verify_callback
= cb
;
4024 void SSL_CTX_set_verify_depth(SSL_CTX
*ctx
, int depth
)
4026 X509_VERIFY_PARAM_set_depth(ctx
->param
, depth
);
4029 void SSL_CTX_set_cert_cb(SSL_CTX
*c
, int (*cb
) (SSL
*ssl
, void *arg
), void *arg
)
4031 ssl_cert_set_cert_cb(c
->cert
, cb
, arg
);
4034 void SSL_set_cert_cb(SSL
*s
, int (*cb
) (SSL
*ssl
, void *arg
), void *arg
)
4036 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
4041 ssl_cert_set_cert_cb(sc
->cert
, cb
, arg
);
4044 void ssl_set_masks(SSL_CONNECTION
*s
)
4047 uint32_t *pvalid
= s
->s3
.tmp
.valid_flags
;
4048 int rsa_enc
, rsa_sign
, dh_tmp
, dsa_sign
;
4049 unsigned long mask_k
, mask_a
;
4050 int have_ecc_cert
, ecdsa_ok
;
4055 dh_tmp
= (c
->dh_tmp
!= NULL
4056 || c
->dh_tmp_cb
!= NULL
4059 rsa_enc
= pvalid
[SSL_PKEY_RSA
] & CERT_PKEY_VALID
;
4060 rsa_sign
= pvalid
[SSL_PKEY_RSA
] & CERT_PKEY_VALID
;
4061 dsa_sign
= pvalid
[SSL_PKEY_DSA_SIGN
] & CERT_PKEY_VALID
;
4062 have_ecc_cert
= pvalid
[SSL_PKEY_ECC
] & CERT_PKEY_VALID
;
4066 OSSL_TRACE4(TLS_CIPHER
, "dh_tmp=%d rsa_enc=%d rsa_sign=%d dsa_sign=%d\n",
4067 dh_tmp
, rsa_enc
, rsa_sign
, dsa_sign
);
4069 #ifndef OPENSSL_NO_GOST
4070 if (ssl_has_cert(s
, SSL_PKEY_GOST12_512
)) {
4071 mask_k
|= SSL_kGOST
| SSL_kGOST18
;
4072 mask_a
|= SSL_aGOST12
;
4074 if (ssl_has_cert(s
, SSL_PKEY_GOST12_256
)) {
4075 mask_k
|= SSL_kGOST
| SSL_kGOST18
;
4076 mask_a
|= SSL_aGOST12
;
4078 if (ssl_has_cert(s
, SSL_PKEY_GOST01
)) {
4079 mask_k
|= SSL_kGOST
;
4080 mask_a
|= SSL_aGOST01
;
4091 * If we only have an RSA-PSS certificate allow RSA authentication
4092 * if TLS 1.2 and peer supports it.
4095 if (rsa_enc
|| rsa_sign
|| (ssl_has_cert(s
, SSL_PKEY_RSA_PSS_SIGN
)
4096 && pvalid
[SSL_PKEY_RSA_PSS_SIGN
] & CERT_PKEY_EXPLICIT_SIGN
4097 && TLS1_get_version(&s
->ssl
) == TLS1_2_VERSION
))
4104 mask_a
|= SSL_aNULL
;
4107 * An ECC certificate may be usable for ECDH and/or ECDSA cipher suites
4108 * depending on the key usage extension.
4110 if (have_ecc_cert
) {
4112 ex_kusage
= X509_get_key_usage(c
->pkeys
[SSL_PKEY_ECC
].x509
);
4113 ecdsa_ok
= ex_kusage
& X509v3_KU_DIGITAL_SIGNATURE
;
4114 if (!(pvalid
[SSL_PKEY_ECC
] & CERT_PKEY_SIGN
))
4117 mask_a
|= SSL_aECDSA
;
4119 /* Allow Ed25519 for TLS 1.2 if peer supports it */
4120 if (!(mask_a
& SSL_aECDSA
) && ssl_has_cert(s
, SSL_PKEY_ED25519
)
4121 && pvalid
[SSL_PKEY_ED25519
] & CERT_PKEY_EXPLICIT_SIGN
4122 && TLS1_get_version(&s
->ssl
) == TLS1_2_VERSION
)
4123 mask_a
|= SSL_aECDSA
;
4125 /* Allow Ed448 for TLS 1.2 if peer supports it */
4126 if (!(mask_a
& SSL_aECDSA
) && ssl_has_cert(s
, SSL_PKEY_ED448
)
4127 && pvalid
[SSL_PKEY_ED448
] & CERT_PKEY_EXPLICIT_SIGN
4128 && TLS1_get_version(&s
->ssl
) == TLS1_2_VERSION
)
4129 mask_a
|= SSL_aECDSA
;
4131 mask_k
|= SSL_kECDHE
;
4133 #ifndef OPENSSL_NO_PSK
4136 if (mask_k
& SSL_kRSA
)
4137 mask_k
|= SSL_kRSAPSK
;
4138 if (mask_k
& SSL_kDHE
)
4139 mask_k
|= SSL_kDHEPSK
;
4140 if (mask_k
& SSL_kECDHE
)
4141 mask_k
|= SSL_kECDHEPSK
;
4144 s
->s3
.tmp
.mask_k
= mask_k
;
4145 s
->s3
.tmp
.mask_a
= mask_a
;
4148 int ssl_check_srvr_ecc_cert_and_alg(X509
*x
, SSL_CONNECTION
*s
)
4150 if (s
->s3
.tmp
.new_cipher
->algorithm_auth
& SSL_aECDSA
) {
4151 /* key usage, if present, must allow signing */
4152 if (!(X509_get_key_usage(x
) & X509v3_KU_DIGITAL_SIGNATURE
)) {
4153 ERR_raise(ERR_LIB_SSL
, SSL_R_ECC_CERT_NOT_FOR_SIGNING
);
4157 return 1; /* all checks are ok */
4160 int ssl_get_server_cert_serverinfo(SSL_CONNECTION
*s
,
4161 const unsigned char **serverinfo
,
4162 size_t *serverinfo_length
)
4164 CERT_PKEY
*cpk
= s
->s3
.tmp
.cert
;
4165 *serverinfo_length
= 0;
4167 if (cpk
== NULL
|| cpk
->serverinfo
== NULL
)
4170 *serverinfo
= cpk
->serverinfo
;
4171 *serverinfo_length
= cpk
->serverinfo_length
;
4175 void ssl_update_cache(SSL_CONNECTION
*s
, int mode
)
4180 * If the session_id_length is 0, we are not supposed to cache it, and it
4181 * would be rather hard to do anyway :-)
4183 if (s
->session
->session_id_length
== 0)
4187 * If sid_ctx_length is 0 there is no specific application context
4188 * associated with this session, so when we try to resume it and
4189 * SSL_VERIFY_PEER is requested to verify the client identity, we have no
4190 * indication that this is actually a session for the proper application
4191 * context, and the *handshake* will fail, not just the resumption attempt.
4192 * Do not cache (on the server) these sessions that are not resumable
4193 * (clients can set SSL_VERIFY_PEER without needing a sid_ctx set).
4195 if (s
->server
&& s
->session
->sid_ctx_length
== 0
4196 && (s
->verify_mode
& SSL_VERIFY_PEER
) != 0)
4199 i
= s
->session_ctx
->session_cache_mode
;
4201 && (!s
->hit
|| SSL_CONNECTION_IS_TLS13(s
))) {
4203 * Add the session to the internal cache. In server side TLSv1.3 we
4204 * normally don't do this because by default it's a full stateless ticket
4205 * with only a dummy session id so there is no reason to cache it,
4207 * - we are doing early_data, in which case we cache so that we can
4209 * - the application has set a remove_session_cb so needs to know about
4210 * session timeout events
4211 * - SSL_OP_NO_TICKET is set in which case it is a stateful ticket
4213 if ((i
& SSL_SESS_CACHE_NO_INTERNAL_STORE
) == 0
4214 && (!SSL_CONNECTION_IS_TLS13(s
)
4216 || (s
->max_early_data
> 0
4217 && (s
->options
& SSL_OP_NO_ANTI_REPLAY
) == 0)
4218 || s
->session_ctx
->remove_session_cb
!= NULL
4219 || (s
->options
& SSL_OP_NO_TICKET
) != 0))
4220 SSL_CTX_add_session(s
->session_ctx
, s
->session
);
4223 * Add the session to the external cache. We do this even in server side
4224 * TLSv1.3 without early data because some applications just want to
4225 * know about the creation of a session and aren't doing a full cache.
4227 if (s
->session_ctx
->new_session_cb
!= NULL
) {
4228 SSL_SESSION_up_ref(s
->session
);
4229 if (!s
->session_ctx
->new_session_cb(SSL_CONNECTION_GET_SSL(s
),
4231 SSL_SESSION_free(s
->session
);
4235 /* auto flush every 255 connections */
4236 if ((!(i
& SSL_SESS_CACHE_NO_AUTO_CLEAR
)) && ((i
& mode
) == mode
)) {
4237 TSAN_QUALIFIER
int *stat
;
4239 if (mode
& SSL_SESS_CACHE_CLIENT
)
4240 stat
= &s
->session_ctx
->stats
.sess_connect_good
;
4242 stat
= &s
->session_ctx
->stats
.sess_accept_good
;
4243 if ((ssl_tsan_load(s
->session_ctx
, stat
) & 0xff) == 0xff)
4244 SSL_CTX_flush_sessions(s
->session_ctx
, (unsigned long)time(NULL
));
4248 const SSL_METHOD
*SSL_CTX_get_ssl_method(const SSL_CTX
*ctx
)
4253 const SSL_METHOD
*SSL_get_ssl_method(const SSL
*s
)
4258 int SSL_set_ssl_method(SSL
*s
, const SSL_METHOD
*meth
)
4261 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
4263 /* TODO(QUIC): Do we want this for QUIC? */
4265 || (s
->type
!= SSL_TYPE_SSL_CONNECTION
&& s
->method
!= meth
))
4268 if (s
->method
!= meth
) {
4269 const SSL_METHOD
*sm
= s
->method
;
4270 int (*hf
) (SSL
*) = sc
->handshake_func
;
4272 if (sm
->version
== meth
->version
)
4277 ret
= s
->method
->ssl_init(s
);
4280 if (hf
== sm
->ssl_connect
)
4281 sc
->handshake_func
= meth
->ssl_connect
;
4282 else if (hf
== sm
->ssl_accept
)
4283 sc
->handshake_func
= meth
->ssl_accept
;
4288 int SSL_get_error(const SSL
*s
, int i
)
4293 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
4296 return SSL_ERROR_NONE
;
4298 /* TODO(QUIC): This will need more handling for QUIC_CONNECTIONs */
4300 return SSL_ERROR_SSL
;
4303 * Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
4304 * where we do encode the error
4306 if ((l
= ERR_peek_error()) != 0) {
4307 if (ERR_GET_LIB(l
) == ERR_LIB_SYS
)
4308 return SSL_ERROR_SYSCALL
;
4310 return SSL_ERROR_SSL
;
4313 if (SSL_want_read(s
)) {
4314 bio
= SSL_get_rbio(s
);
4315 if (BIO_should_read(bio
))
4316 return SSL_ERROR_WANT_READ
;
4317 else if (BIO_should_write(bio
))
4319 * This one doesn't make too much sense ... We never try to write
4320 * to the rbio, and an application program where rbio and wbio
4321 * are separate couldn't even know what it should wait for.
4322 * However if we ever set s->rwstate incorrectly (so that we have
4323 * SSL_want_read(s) instead of SSL_want_write(s)) and rbio and
4324 * wbio *are* the same, this test works around that bug; so it
4325 * might be safer to keep it.
4327 return SSL_ERROR_WANT_WRITE
;
4328 else if (BIO_should_io_special(bio
)) {
4329 reason
= BIO_get_retry_reason(bio
);
4330 if (reason
== BIO_RR_CONNECT
)
4331 return SSL_ERROR_WANT_CONNECT
;
4332 else if (reason
== BIO_RR_ACCEPT
)
4333 return SSL_ERROR_WANT_ACCEPT
;
4335 return SSL_ERROR_SYSCALL
; /* unknown */
4339 if (SSL_want_write(s
)) {
4340 /* Access wbio directly - in order to use the buffered bio if present */
4342 if (BIO_should_write(bio
))
4343 return SSL_ERROR_WANT_WRITE
;
4344 else if (BIO_should_read(bio
))
4346 * See above (SSL_want_read(s) with BIO_should_write(bio))
4348 return SSL_ERROR_WANT_READ
;
4349 else if (BIO_should_io_special(bio
)) {
4350 reason
= BIO_get_retry_reason(bio
);
4351 if (reason
== BIO_RR_CONNECT
)
4352 return SSL_ERROR_WANT_CONNECT
;
4353 else if (reason
== BIO_RR_ACCEPT
)
4354 return SSL_ERROR_WANT_ACCEPT
;
4356 return SSL_ERROR_SYSCALL
;
4359 if (SSL_want_x509_lookup(s
))
4360 return SSL_ERROR_WANT_X509_LOOKUP
;
4361 if (SSL_want_retry_verify(s
))
4362 return SSL_ERROR_WANT_RETRY_VERIFY
;
4363 if (SSL_want_async(s
))
4364 return SSL_ERROR_WANT_ASYNC
;
4365 if (SSL_want_async_job(s
))
4366 return SSL_ERROR_WANT_ASYNC_JOB
;
4367 if (SSL_want_client_hello_cb(s
))
4368 return SSL_ERROR_WANT_CLIENT_HELLO_CB
;
4370 if ((sc
->shutdown
& SSL_RECEIVED_SHUTDOWN
) &&
4371 (sc
->s3
.warn_alert
== SSL_AD_CLOSE_NOTIFY
))
4372 return SSL_ERROR_ZERO_RETURN
;
4374 return SSL_ERROR_SYSCALL
;
4377 static int ssl_do_handshake_intern(void *vargs
)
4379 struct ssl_async_args
*args
= (struct ssl_async_args
*)vargs
;
4381 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
4386 return sc
->handshake_func(s
);
4389 int SSL_do_handshake(SSL
*s
)
4392 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
4394 /* TODO(QUIC): Special handling for QUIC will be needed */
4398 if (sc
->handshake_func
== NULL
) {
4399 ERR_raise(ERR_LIB_SSL
, SSL_R_CONNECTION_TYPE_NOT_SET
);
4403 ossl_statem_check_finish_init(sc
, -1);
4405 s
->method
->ssl_renegotiate_check(s
, 0);
4407 if (SSL_in_init(s
) || SSL_in_before(s
)) {
4408 if ((sc
->mode
& SSL_MODE_ASYNC
) && ASYNC_get_current_job() == NULL
) {
4409 struct ssl_async_args args
;
4411 memset(&args
, 0, sizeof(args
));
4414 ret
= ssl_start_async_job(s
, &args
, ssl_do_handshake_intern
);
4416 ret
= sc
->handshake_func(s
);
4422 void SSL_set_accept_state(SSL
*s
)
4424 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
4426 /* TODO(QUIC): Special handling for QUIC will be needed */
4432 ossl_statem_clear(sc
);
4433 sc
->handshake_func
= s
->method
->ssl_accept
;
4437 void SSL_set_connect_state(SSL
*s
)
4439 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
4441 /* TODO(QUIC): Special handling for QUIC will be needed */
4447 ossl_statem_clear(sc
);
4448 sc
->handshake_func
= s
->method
->ssl_connect
;
4452 int ssl_undefined_function(SSL
*s
)
4454 ERR_raise(ERR_LIB_SSL
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
4458 int ssl_undefined_void_function(void)
4460 ERR_raise(ERR_LIB_SSL
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
4464 int ssl_undefined_const_function(const SSL
*s
)
4469 const SSL_METHOD
*ssl_bad_method(int ver
)
4471 ERR_raise(ERR_LIB_SSL
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
4475 const char *ssl_protocol_to_string(int version
)
4479 case TLS1_3_VERSION
:
4482 case TLS1_2_VERSION
:
4485 case TLS1_1_VERSION
:
4500 case DTLS1_2_VERSION
:
4508 const char *SSL_get_version(const SSL
*s
)
4510 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
4512 /* TODO(QUIC): Should QUIC return QUIC or TLSv1.3? */
4516 return ssl_protocol_to_string(sc
->version
);
4519 static int dup_ca_names(STACK_OF(X509_NAME
) **dst
, STACK_OF(X509_NAME
) *src
)
4521 STACK_OF(X509_NAME
) *sk
;
4530 if ((sk
= sk_X509_NAME_new_null()) == NULL
)
4532 for (i
= 0; i
< sk_X509_NAME_num(src
); i
++) {
4533 xn
= X509_NAME_dup(sk_X509_NAME_value(src
, i
));
4535 sk_X509_NAME_pop_free(sk
, X509_NAME_free
);
4538 if (sk_X509_NAME_insert(sk
, xn
, i
) == 0) {
4540 sk_X509_NAME_pop_free(sk
, X509_NAME_free
);
4549 SSL
*SSL_dup(SSL
*s
)
4553 /* TODO(QUIC): Add a SSL_METHOD function for duplication */
4554 SSL_CONNECTION
*retsc
;
4555 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(s
);
4560 /* If we're not quiescent, just up_ref! */
4561 if (!SSL_in_init(s
) || !SSL_in_before(s
)) {
4562 CRYPTO_UP_REF(&s
->references
, &i
, s
->lock
);
4567 * Otherwise, copy configuration state, and session if set.
4569 if ((ret
= SSL_new(SSL_get_SSL_CTX(s
))) == NULL
)
4571 if ((retsc
= SSL_CONNECTION_FROM_SSL_ONLY(ret
)) == NULL
)
4574 if (sc
->session
!= NULL
) {
4576 * Arranges to share the same session via up_ref. This "copies"
4577 * session-id, SSL_METHOD, sid_ctx, and 'cert'
4579 if (!SSL_copy_session_id(ret
, s
))
4583 * No session has been established yet, so we have to expect that
4584 * s->cert or ret->cert will be changed later -- they should not both
4585 * point to the same object, and thus we can't use
4586 * SSL_copy_session_id.
4588 if (!SSL_set_ssl_method(ret
, s
->method
))
4591 if (sc
->cert
!= NULL
) {
4592 ssl_cert_free(retsc
->cert
);
4593 retsc
->cert
= ssl_cert_dup(sc
->cert
);
4594 if (retsc
->cert
== NULL
)
4598 if (!SSL_set_session_id_context(ret
, sc
->sid_ctx
,
4599 (int)sc
->sid_ctx_length
))
4603 if (!ssl_dane_dup(retsc
, sc
))
4605 retsc
->version
= sc
->version
;
4606 retsc
->options
= sc
->options
;
4607 retsc
->min_proto_version
= sc
->min_proto_version
;
4608 retsc
->max_proto_version
= sc
->max_proto_version
;
4609 retsc
->mode
= sc
->mode
;
4610 SSL_set_max_cert_list(ret
, SSL_get_max_cert_list(s
));
4611 SSL_set_read_ahead(ret
, SSL_get_read_ahead(s
));
4612 retsc
->msg_callback
= sc
->msg_callback
;
4613 retsc
->msg_callback_arg
= sc
->msg_callback_arg
;
4614 SSL_set_verify(ret
, SSL_get_verify_mode(s
), SSL_get_verify_callback(s
));
4615 SSL_set_verify_depth(ret
, SSL_get_verify_depth(s
));
4616 retsc
->generate_session_id
= sc
->generate_session_id
;
4618 SSL_set_info_callback(ret
, SSL_get_info_callback(s
));
4620 /* copy app data, a little dangerous perhaps */
4621 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL
, &ret
->ex_data
, &s
->ex_data
))
4624 retsc
->server
= sc
->server
;
4625 if (sc
->handshake_func
) {
4627 SSL_set_accept_state(ret
);
4629 SSL_set_connect_state(ret
);
4631 retsc
->shutdown
= sc
->shutdown
;
4632 retsc
->hit
= sc
->hit
;
4634 retsc
->default_passwd_callback
= sc
->default_passwd_callback
;
4635 retsc
->default_passwd_callback_userdata
= sc
->default_passwd_callback_userdata
;
4637 X509_VERIFY_PARAM_inherit(retsc
->param
, sc
->param
);
4639 /* dup the cipher_list and cipher_list_by_id stacks */
4640 if (sc
->cipher_list
!= NULL
) {
4641 if ((retsc
->cipher_list
= sk_SSL_CIPHER_dup(sc
->cipher_list
)) == NULL
)
4644 if (sc
->cipher_list_by_id
!= NULL
)
4645 if ((retsc
->cipher_list_by_id
= sk_SSL_CIPHER_dup(sc
->cipher_list_by_id
))
4649 /* Dup the client_CA list */
4650 if (!dup_ca_names(&retsc
->ca_names
, sc
->ca_names
)
4651 || !dup_ca_names(&retsc
->client_ca_names
, sc
->client_ca_names
))
4661 void ssl_clear_cipher_ctx(SSL_CONNECTION
*s
)
4663 if (s
->enc_read_ctx
!= NULL
) {
4664 EVP_CIPHER_CTX_free(s
->enc_read_ctx
);
4665 s
->enc_read_ctx
= NULL
;
4667 if (s
->enc_write_ctx
!= NULL
) {
4668 EVP_CIPHER_CTX_free(s
->enc_write_ctx
);
4669 s
->enc_write_ctx
= NULL
;
4671 #ifndef OPENSSL_NO_COMP
4672 COMP_CTX_free(s
->expand
);
4674 COMP_CTX_free(s
->compress
);
4679 X509
*SSL_get_certificate(const SSL
*s
)
4681 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
4686 if (sc
->cert
!= NULL
)
4687 return sc
->cert
->key
->x509
;
4692 EVP_PKEY
*SSL_get_privatekey(const SSL
*s
)
4694 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
4699 if (sc
->cert
!= NULL
)
4700 return sc
->cert
->key
->privatekey
;
4705 X509
*SSL_CTX_get0_certificate(const SSL_CTX
*ctx
)
4707 if (ctx
->cert
!= NULL
)
4708 return ctx
->cert
->key
->x509
;
4713 EVP_PKEY
*SSL_CTX_get0_privatekey(const SSL_CTX
*ctx
)
4715 if (ctx
->cert
!= NULL
)
4716 return ctx
->cert
->key
->privatekey
;
4721 const SSL_CIPHER
*SSL_get_current_cipher(const SSL
*s
)
4723 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
4728 if ((sc
->session
!= NULL
) && (sc
->session
->cipher
!= NULL
))
4729 return sc
->session
->cipher
;
4733 const SSL_CIPHER
*SSL_get_pending_cipher(const SSL
*s
)
4735 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
4740 return sc
->s3
.tmp
.new_cipher
;
4743 const COMP_METHOD
*SSL_get_current_compression(const SSL
*s
)
4745 #ifndef OPENSSL_NO_COMP
4746 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL_ONLY(s
);
4751 return sc
->compress
? COMP_CTX_get_method(sc
->compress
) : NULL
;
4757 const COMP_METHOD
*SSL_get_current_expansion(const SSL
*s
)
4759 #ifndef OPENSSL_NO_COMP
4760 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL_ONLY(s
);
4765 return sc
->expand
? COMP_CTX_get_method(sc
->expand
) : NULL
;
4771 int ssl_init_wbio_buffer(SSL_CONNECTION
*s
)
4775 if (s
->bbio
!= NULL
) {
4776 /* Already buffered. */
4780 bbio
= BIO_new(BIO_f_buffer());
4781 if (bbio
== NULL
|| !BIO_set_read_buffer_size(bbio
, 1)) {
4783 ERR_raise(ERR_LIB_SSL
, ERR_R_BUF_LIB
);
4787 s
->wbio
= BIO_push(bbio
, s
->wbio
);
4792 int ssl_free_wbio_buffer(SSL_CONNECTION
*s
)
4794 /* callers ensure s is never null */
4795 if (s
->bbio
== NULL
)
4798 s
->wbio
= BIO_pop(s
->wbio
);
4805 void SSL_CTX_set_quiet_shutdown(SSL_CTX
*ctx
, int mode
)
4807 ctx
->quiet_shutdown
= mode
;
4810 int SSL_CTX_get_quiet_shutdown(const SSL_CTX
*ctx
)
4812 return ctx
->quiet_shutdown
;
4815 void SSL_set_quiet_shutdown(SSL
*s
, int mode
)
4817 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(s
);
4819 /* TODO(QUIC): Do we want this for QUIC? */
4823 sc
->quiet_shutdown
= mode
;
4826 int SSL_get_quiet_shutdown(const SSL
*s
)
4828 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL_ONLY(s
);
4830 /* TODO(QUIC): Do we want this for QUIC? */
4834 return sc
->quiet_shutdown
;
4837 void SSL_set_shutdown(SSL
*s
, int mode
)
4839 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(s
);
4841 /* TODO(QUIC): Do we want this for QUIC? */
4845 sc
->shutdown
= mode
;
4848 int SSL_get_shutdown(const SSL
*s
)
4850 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL_ONLY(s
);
4852 /* TODO(QUIC): Do we want this for QUIC? */
4856 return sc
->shutdown
;
4859 int SSL_version(const SSL
*s
)
4861 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
4863 /* TODO(QUIC): Do we want to report QUIC version this way instead? */
4870 int SSL_client_version(const SSL
*s
)
4872 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
4874 /* TODO(QUIC): Do we want to report QUIC version this way instead? */
4878 return sc
->client_version
;
4881 SSL_CTX
*SSL_get_SSL_CTX(const SSL
*ssl
)
4886 SSL_CTX
*SSL_set_SSL_CTX(SSL
*ssl
, SSL_CTX
*ctx
)
4889 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL_ONLY(ssl
);
4891 /* TODO(QUIC): Do we need this for QUIC support? */
4895 if (ssl
->ctx
== ctx
)
4898 ctx
= sc
->session_ctx
;
4899 new_cert
= ssl_cert_dup(ctx
->cert
);
4900 if (new_cert
== NULL
) {
4904 if (!custom_exts_copy_flags(&new_cert
->custext
, &sc
->cert
->custext
)) {
4905 ssl_cert_free(new_cert
);
4909 ssl_cert_free(sc
->cert
);
4910 sc
->cert
= new_cert
;
4913 * Program invariant: |sid_ctx| has fixed size (SSL_MAX_SID_CTX_LENGTH),
4914 * so setter APIs must prevent invalid lengths from entering the system.
4916 if (!ossl_assert(sc
->sid_ctx_length
<= sizeof(sc
->sid_ctx
)))
4920 * If the session ID context matches that of the parent SSL_CTX,
4921 * inherit it from the new SSL_CTX as well. If however the context does
4922 * not match (i.e., it was set per-ssl with SSL_set_session_id_context),
4923 * leave it unchanged.
4925 if ((ssl
->ctx
!= NULL
) &&
4926 (sc
->sid_ctx_length
== ssl
->ctx
->sid_ctx_length
) &&
4927 (memcmp(sc
->sid_ctx
, ssl
->ctx
->sid_ctx
, sc
->sid_ctx_length
) == 0)) {
4928 sc
->sid_ctx_length
= ctx
->sid_ctx_length
;
4929 memcpy(&sc
->sid_ctx
, &ctx
->sid_ctx
, sizeof(sc
->sid_ctx
));
4932 SSL_CTX_up_ref(ctx
);
4933 SSL_CTX_free(ssl
->ctx
); /* decrement reference count */
4939 int SSL_CTX_set_default_verify_paths(SSL_CTX
*ctx
)
4941 return X509_STORE_set_default_paths_ex(ctx
->cert_store
, ctx
->libctx
,
4945 int SSL_CTX_set_default_verify_dir(SSL_CTX
*ctx
)
4947 X509_LOOKUP
*lookup
;
4949 lookup
= X509_STORE_add_lookup(ctx
->cert_store
, X509_LOOKUP_hash_dir());
4953 /* We ignore errors, in case the directory doesn't exist */
4956 X509_LOOKUP_add_dir(lookup
, NULL
, X509_FILETYPE_DEFAULT
);
4963 int SSL_CTX_set_default_verify_file(SSL_CTX
*ctx
)
4965 X509_LOOKUP
*lookup
;
4967 lookup
= X509_STORE_add_lookup(ctx
->cert_store
, X509_LOOKUP_file());
4971 /* We ignore errors, in case the file doesn't exist */
4974 X509_LOOKUP_load_file_ex(lookup
, NULL
, X509_FILETYPE_DEFAULT
, ctx
->libctx
,
4982 int SSL_CTX_set_default_verify_store(SSL_CTX
*ctx
)
4984 X509_LOOKUP
*lookup
;
4986 lookup
= X509_STORE_add_lookup(ctx
->cert_store
, X509_LOOKUP_store());
4990 /* We ignore errors, in case the directory doesn't exist */
4993 X509_LOOKUP_add_store_ex(lookup
, NULL
, ctx
->libctx
, ctx
->propq
);
5000 int SSL_CTX_load_verify_file(SSL_CTX
*ctx
, const char *CAfile
)
5002 return X509_STORE_load_file_ex(ctx
->cert_store
, CAfile
, ctx
->libctx
,
5006 int SSL_CTX_load_verify_dir(SSL_CTX
*ctx
, const char *CApath
)
5008 return X509_STORE_load_path(ctx
->cert_store
, CApath
);
5011 int SSL_CTX_load_verify_store(SSL_CTX
*ctx
, const char *CAstore
)
5013 return X509_STORE_load_store_ex(ctx
->cert_store
, CAstore
, ctx
->libctx
,
5017 int SSL_CTX_load_verify_locations(SSL_CTX
*ctx
, const char *CAfile
,
5020 if (CAfile
== NULL
&& CApath
== NULL
)
5022 if (CAfile
!= NULL
&& !SSL_CTX_load_verify_file(ctx
, CAfile
))
5024 if (CApath
!= NULL
&& !SSL_CTX_load_verify_dir(ctx
, CApath
))
5029 void SSL_set_info_callback(SSL
*ssl
,
5030 void (*cb
) (const SSL
*ssl
, int type
, int val
))
5032 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
5037 sc
->info_callback
= cb
;
5041 * One compiler (Diab DCC) doesn't like argument names in returned function
5044 void (*SSL_get_info_callback(const SSL
*ssl
)) (const SSL
* /* ssl */ ,
5047 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(ssl
);
5052 return sc
->info_callback
;
5055 void SSL_set_verify_result(SSL
*ssl
, long arg
)
5057 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
5062 sc
->verify_result
= arg
;
5065 long SSL_get_verify_result(const SSL
*ssl
)
5067 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(ssl
);
5072 return sc
->verify_result
;
5075 size_t SSL_get_client_random(const SSL
*ssl
, unsigned char *out
, size_t outlen
)
5077 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(ssl
);
5083 return sizeof(sc
->s3
.client_random
);
5084 if (outlen
> sizeof(sc
->s3
.client_random
))
5085 outlen
= sizeof(sc
->s3
.client_random
);
5086 memcpy(out
, sc
->s3
.client_random
, outlen
);
5090 size_t SSL_get_server_random(const SSL
*ssl
, unsigned char *out
, size_t outlen
)
5092 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(ssl
);
5098 return sizeof(sc
->s3
.server_random
);
5099 if (outlen
> sizeof(sc
->s3
.server_random
))
5100 outlen
= sizeof(sc
->s3
.server_random
);
5101 memcpy(out
, sc
->s3
.server_random
, outlen
);
5105 size_t SSL_SESSION_get_master_key(const SSL_SESSION
*session
,
5106 unsigned char *out
, size_t outlen
)
5109 return session
->master_key_length
;
5110 if (outlen
> session
->master_key_length
)
5111 outlen
= session
->master_key_length
;
5112 memcpy(out
, session
->master_key
, outlen
);
5116 int SSL_SESSION_set1_master_key(SSL_SESSION
*sess
, const unsigned char *in
,
5119 if (len
> sizeof(sess
->master_key
))
5122 memcpy(sess
->master_key
, in
, len
);
5123 sess
->master_key_length
= len
;
5128 int SSL_set_ex_data(SSL
*s
, int idx
, void *arg
)
5130 return CRYPTO_set_ex_data(&s
->ex_data
, idx
, arg
);
5133 void *SSL_get_ex_data(const SSL
*s
, int idx
)
5135 return CRYPTO_get_ex_data(&s
->ex_data
, idx
);
5138 int SSL_CTX_set_ex_data(SSL_CTX
*s
, int idx
, void *arg
)
5140 return CRYPTO_set_ex_data(&s
->ex_data
, idx
, arg
);
5143 void *SSL_CTX_get_ex_data(const SSL_CTX
*s
, int idx
)
5145 return CRYPTO_get_ex_data(&s
->ex_data
, idx
);
5148 X509_STORE
*SSL_CTX_get_cert_store(const SSL_CTX
*ctx
)
5150 return ctx
->cert_store
;
5153 void SSL_CTX_set_cert_store(SSL_CTX
*ctx
, X509_STORE
*store
)
5155 X509_STORE_free(ctx
->cert_store
);
5156 ctx
->cert_store
= store
;
5159 void SSL_CTX_set1_cert_store(SSL_CTX
*ctx
, X509_STORE
*store
)
5162 X509_STORE_up_ref(store
);
5163 SSL_CTX_set_cert_store(ctx
, store
);
5166 int SSL_want(const SSL
*s
)
5168 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
5176 #ifndef OPENSSL_NO_PSK
5177 int SSL_CTX_use_psk_identity_hint(SSL_CTX
*ctx
, const char *identity_hint
)
5179 if (identity_hint
!= NULL
&& strlen(identity_hint
) > PSK_MAX_IDENTITY_LEN
) {
5180 ERR_raise(ERR_LIB_SSL
, SSL_R_DATA_LENGTH_TOO_LONG
);
5183 OPENSSL_free(ctx
->cert
->psk_identity_hint
);
5184 if (identity_hint
!= NULL
) {
5185 ctx
->cert
->psk_identity_hint
= OPENSSL_strdup(identity_hint
);
5186 if (ctx
->cert
->psk_identity_hint
== NULL
)
5189 ctx
->cert
->psk_identity_hint
= NULL
;
5193 int SSL_use_psk_identity_hint(SSL
*s
, const char *identity_hint
)
5195 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
5200 if (identity_hint
!= NULL
&& strlen(identity_hint
) > PSK_MAX_IDENTITY_LEN
) {
5201 ERR_raise(ERR_LIB_SSL
, SSL_R_DATA_LENGTH_TOO_LONG
);
5204 OPENSSL_free(sc
->cert
->psk_identity_hint
);
5205 if (identity_hint
!= NULL
) {
5206 sc
->cert
->psk_identity_hint
= OPENSSL_strdup(identity_hint
);
5207 if (sc
->cert
->psk_identity_hint
== NULL
)
5210 sc
->cert
->psk_identity_hint
= NULL
;
5214 const char *SSL_get_psk_identity_hint(const SSL
*s
)
5216 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
5218 if (sc
== NULL
|| sc
->session
== NULL
)
5221 return sc
->session
->psk_identity_hint
;
5224 const char *SSL_get_psk_identity(const SSL
*s
)
5226 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
5228 if (sc
== NULL
|| sc
->session
== NULL
)
5231 return sc
->session
->psk_identity
;
5234 void SSL_set_psk_client_callback(SSL
*s
, SSL_psk_client_cb_func cb
)
5236 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
5241 sc
->psk_client_callback
= cb
;
5244 void SSL_CTX_set_psk_client_callback(SSL_CTX
*ctx
, SSL_psk_client_cb_func cb
)
5246 ctx
->psk_client_callback
= cb
;
5249 void SSL_set_psk_server_callback(SSL
*s
, SSL_psk_server_cb_func cb
)
5251 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
5256 sc
->psk_server_callback
= cb
;
5259 void SSL_CTX_set_psk_server_callback(SSL_CTX
*ctx
, SSL_psk_server_cb_func cb
)
5261 ctx
->psk_server_callback
= cb
;
5265 void SSL_set_psk_find_session_callback(SSL
*s
, SSL_psk_find_session_cb_func cb
)
5267 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
5272 sc
->psk_find_session_cb
= cb
;
5275 void SSL_CTX_set_psk_find_session_callback(SSL_CTX
*ctx
,
5276 SSL_psk_find_session_cb_func cb
)
5278 ctx
->psk_find_session_cb
= cb
;
5281 void SSL_set_psk_use_session_callback(SSL
*s
, SSL_psk_use_session_cb_func cb
)
5283 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
5288 sc
->psk_use_session_cb
= cb
;
5291 void SSL_CTX_set_psk_use_session_callback(SSL_CTX
*ctx
,
5292 SSL_psk_use_session_cb_func cb
)
5294 ctx
->psk_use_session_cb
= cb
;
5297 void SSL_CTX_set_msg_callback(SSL_CTX
*ctx
,
5298 void (*cb
) (int write_p
, int version
,
5299 int content_type
, const void *buf
,
5300 size_t len
, SSL
*ssl
, void *arg
))
5302 SSL_CTX_callback_ctrl(ctx
, SSL_CTRL_SET_MSG_CALLBACK
, (void (*)(void))cb
);
5305 void SSL_set_msg_callback(SSL
*ssl
,
5306 void (*cb
) (int write_p
, int version
,
5307 int content_type
, const void *buf
,
5308 size_t len
, SSL
*ssl
, void *arg
))
5310 SSL_callback_ctrl(ssl
, SSL_CTRL_SET_MSG_CALLBACK
, (void (*)(void))cb
);
5313 void SSL_CTX_set_not_resumable_session_callback(SSL_CTX
*ctx
,
5314 int (*cb
) (SSL
*ssl
,
5318 SSL_CTX_callback_ctrl(ctx
, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB
,
5319 (void (*)(void))cb
);
5322 void SSL_set_not_resumable_session_callback(SSL
*ssl
,
5323 int (*cb
) (SSL
*ssl
,
5324 int is_forward_secure
))
5326 SSL_callback_ctrl(ssl
, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB
,
5327 (void (*)(void))cb
);
5330 void SSL_CTX_set_record_padding_callback(SSL_CTX
*ctx
,
5331 size_t (*cb
) (SSL
*ssl
, int type
,
5332 size_t len
, void *arg
))
5334 ctx
->record_padding_cb
= cb
;
5337 void SSL_CTX_set_record_padding_callback_arg(SSL_CTX
*ctx
, void *arg
)
5339 ctx
->record_padding_arg
= arg
;
5342 void *SSL_CTX_get_record_padding_callback_arg(const SSL_CTX
*ctx
)
5344 return ctx
->record_padding_arg
;
5347 int SSL_CTX_set_block_padding(SSL_CTX
*ctx
, size_t block_size
)
5349 /* block size of 0 or 1 is basically no padding */
5350 if (block_size
== 1)
5351 ctx
->block_padding
= 0;
5352 else if (block_size
<= SSL3_RT_MAX_PLAIN_LENGTH
)
5353 ctx
->block_padding
= block_size
;
5359 int SSL_set_record_padding_callback(SSL
*ssl
,
5360 size_t (*cb
) (SSL
*ssl
, int type
,
5361 size_t len
, void *arg
))
5364 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
5369 b
= SSL_get_wbio(ssl
);
5370 if (b
== NULL
|| !BIO_get_ktls_send(b
)) {
5371 sc
->record_padding_cb
= cb
;
5377 void SSL_set_record_padding_callback_arg(SSL
*ssl
, void *arg
)
5379 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
5384 sc
->record_padding_arg
= arg
;
5387 void *SSL_get_record_padding_callback_arg(const SSL
*ssl
)
5389 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(ssl
);
5394 return sc
->record_padding_arg
;
5397 int SSL_set_block_padding(SSL
*ssl
, size_t block_size
)
5399 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
5404 /* block size of 0 or 1 is basically no padding */
5405 if (block_size
== 1)
5406 sc
->block_padding
= 0;
5407 else if (block_size
<= SSL3_RT_MAX_PLAIN_LENGTH
)
5408 sc
->block_padding
= block_size
;
5414 int SSL_set_num_tickets(SSL
*s
, size_t num_tickets
)
5416 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
5421 sc
->num_tickets
= num_tickets
;
5426 size_t SSL_get_num_tickets(const SSL
*s
)
5428 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
5433 return sc
->num_tickets
;
5436 int SSL_CTX_set_num_tickets(SSL_CTX
*ctx
, size_t num_tickets
)
5438 ctx
->num_tickets
= num_tickets
;
5443 size_t SSL_CTX_get_num_tickets(const SSL_CTX
*ctx
)
5445 return ctx
->num_tickets
;
5449 * Allocates new EVP_MD_CTX and sets pointer to it into given pointer
5450 * variable, freeing EVP_MD_CTX previously stored in that variable, if any.
5451 * If EVP_MD pointer is passed, initializes ctx with this |md|.
5452 * Returns the newly allocated ctx;
5455 EVP_MD_CTX
*ssl_replace_hash(EVP_MD_CTX
**hash
, const EVP_MD
*md
)
5457 ssl_clear_hash_ctx(hash
);
5458 *hash
= EVP_MD_CTX_new();
5459 if (*hash
== NULL
|| (md
&& EVP_DigestInit_ex(*hash
, md
, NULL
) <= 0)) {
5460 EVP_MD_CTX_free(*hash
);
5467 void ssl_clear_hash_ctx(EVP_MD_CTX
**hash
)
5470 EVP_MD_CTX_free(*hash
);
5474 /* Retrieve handshake hashes */
5475 int ssl_handshake_hash(SSL_CONNECTION
*s
,
5476 unsigned char *out
, size_t outlen
,
5479 EVP_MD_CTX
*ctx
= NULL
;
5480 EVP_MD_CTX
*hdgst
= s
->s3
.handshake_dgst
;
5481 int hashleni
= EVP_MD_CTX_get_size(hdgst
);
5484 if (hashleni
< 0 || (size_t)hashleni
> outlen
) {
5485 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
5489 ctx
= EVP_MD_CTX_new();
5491 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
5495 if (!EVP_MD_CTX_copy_ex(ctx
, hdgst
)
5496 || EVP_DigestFinal_ex(ctx
, out
, NULL
) <= 0) {
5497 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
5501 *hashlen
= hashleni
;
5505 EVP_MD_CTX_free(ctx
);
5509 int SSL_session_reused(const SSL
*s
)
5511 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
5519 int SSL_is_server(const SSL
*s
)
5521 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
5529 #ifndef OPENSSL_NO_DEPRECATED_1_1_0
5530 void SSL_set_debug(SSL
*s
, int debug
)
5532 /* Old function was do-nothing anyway... */
5538 void SSL_set_security_level(SSL
*s
, int level
)
5540 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
5545 sc
->cert
->sec_level
= level
;
5548 int SSL_get_security_level(const SSL
*s
)
5550 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
5555 return sc
->cert
->sec_level
;
5558 void SSL_set_security_callback(SSL
*s
,
5559 int (*cb
) (const SSL
*s
, const SSL_CTX
*ctx
,
5560 int op
, int bits
, int nid
,
5561 void *other
, void *ex
))
5563 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
5568 sc
->cert
->sec_cb
= cb
;
5571 int (*SSL_get_security_callback(const SSL
*s
)) (const SSL
*s
,
5572 const SSL_CTX
*ctx
, int op
,
5573 int bits
, int nid
, void *other
,
5575 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
5580 return sc
->cert
->sec_cb
;
5583 void SSL_set0_security_ex_data(SSL
*s
, void *ex
)
5585 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
5590 sc
->cert
->sec_ex
= ex
;
5593 void *SSL_get0_security_ex_data(const SSL
*s
)
5595 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
5600 return sc
->cert
->sec_ex
;
5603 void SSL_CTX_set_security_level(SSL_CTX
*ctx
, int level
)
5605 ctx
->cert
->sec_level
= level
;
5608 int SSL_CTX_get_security_level(const SSL_CTX
*ctx
)
5610 return ctx
->cert
->sec_level
;
5613 void SSL_CTX_set_security_callback(SSL_CTX
*ctx
,
5614 int (*cb
) (const SSL
*s
, const SSL_CTX
*ctx
,
5615 int op
, int bits
, int nid
,
5616 void *other
, void *ex
))
5618 ctx
->cert
->sec_cb
= cb
;
5621 int (*SSL_CTX_get_security_callback(const SSL_CTX
*ctx
)) (const SSL
*s
,
5627 return ctx
->cert
->sec_cb
;
5630 void SSL_CTX_set0_security_ex_data(SSL_CTX
*ctx
, void *ex
)
5632 ctx
->cert
->sec_ex
= ex
;
5635 void *SSL_CTX_get0_security_ex_data(const SSL_CTX
*ctx
)
5637 return ctx
->cert
->sec_ex
;
5640 uint64_t SSL_CTX_get_options(const SSL_CTX
*ctx
)
5642 return ctx
->options
;
5645 uint64_t SSL_get_options(const SSL
*s
)
5647 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
5655 uint64_t SSL_CTX_set_options(SSL_CTX
*ctx
, uint64_t op
)
5657 return ctx
->options
|= op
;
5660 uint64_t SSL_set_options(SSL
*s
, uint64_t op
)
5662 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
5667 return sc
->options
|= op
;
5670 uint64_t SSL_CTX_clear_options(SSL_CTX
*ctx
, uint64_t op
)
5672 return ctx
->options
&= ~op
;
5675 uint64_t SSL_clear_options(SSL
*s
, uint64_t op
)
5677 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
5682 return sc
->options
&= ~op
;
5685 STACK_OF(X509
) *SSL_get0_verified_chain(const SSL
*s
)
5687 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
5692 return sc
->verified_chain
;
5695 IMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER
, SSL_CIPHER
, ssl_cipher_id
);
5697 #ifndef OPENSSL_NO_CT
5700 * Moves SCTs from the |src| stack to the |dst| stack.
5701 * The source of each SCT will be set to |origin|.
5702 * If |dst| points to a NULL pointer, a new stack will be created and owned by
5704 * Returns the number of SCTs moved, or a negative integer if an error occurs.
5706 static int ct_move_scts(STACK_OF(SCT
) **dst
, STACK_OF(SCT
) *src
,
5707 sct_source_t origin
)
5713 *dst
= sk_SCT_new_null();
5715 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
5720 while ((sct
= sk_SCT_pop(src
)) != NULL
) {
5721 if (SCT_set_source(sct
, origin
) != 1)
5724 if (sk_SCT_push(*dst
, sct
) <= 0)
5732 sk_SCT_push(src
, sct
); /* Put the SCT back */
5737 * Look for data collected during ServerHello and parse if found.
5738 * Returns the number of SCTs extracted.
5740 static int ct_extract_tls_extension_scts(SSL_CONNECTION
*s
)
5742 int scts_extracted
= 0;
5744 if (s
->ext
.scts
!= NULL
) {
5745 const unsigned char *p
= s
->ext
.scts
;
5746 STACK_OF(SCT
) *scts
= o2i_SCT_LIST(NULL
, &p
, s
->ext
.scts_len
);
5748 scts_extracted
= ct_move_scts(&s
->scts
, scts
, SCT_SOURCE_TLS_EXTENSION
);
5750 SCT_LIST_free(scts
);
5753 return scts_extracted
;
5757 * Checks for an OCSP response and then attempts to extract any SCTs found if it
5758 * contains an SCT X509 extension. They will be stored in |s->scts|.
5760 * - The number of SCTs extracted, assuming an OCSP response exists.
5761 * - 0 if no OCSP response exists or it contains no SCTs.
5762 * - A negative integer if an error occurs.
5764 static int ct_extract_ocsp_response_scts(SSL_CONNECTION
*s
)
5766 # ifndef OPENSSL_NO_OCSP
5767 int scts_extracted
= 0;
5768 const unsigned char *p
;
5769 OCSP_BASICRESP
*br
= NULL
;
5770 OCSP_RESPONSE
*rsp
= NULL
;
5771 STACK_OF(SCT
) *scts
= NULL
;
5774 if (s
->ext
.ocsp
.resp
== NULL
|| s
->ext
.ocsp
.resp_len
== 0)
5777 p
= s
->ext
.ocsp
.resp
;
5778 rsp
= d2i_OCSP_RESPONSE(NULL
, &p
, (int)s
->ext
.ocsp
.resp_len
);
5782 br
= OCSP_response_get1_basic(rsp
);
5786 for (i
= 0; i
< OCSP_resp_count(br
); ++i
) {
5787 OCSP_SINGLERESP
*single
= OCSP_resp_get0(br
, i
);
5793 OCSP_SINGLERESP_get1_ext_d2i(single
, NID_ct_cert_scts
, NULL
, NULL
);
5795 ct_move_scts(&s
->scts
, scts
, SCT_SOURCE_OCSP_STAPLED_RESPONSE
);
5796 if (scts_extracted
< 0)
5800 SCT_LIST_free(scts
);
5801 OCSP_BASICRESP_free(br
);
5802 OCSP_RESPONSE_free(rsp
);
5803 return scts_extracted
;
5805 /* Behave as if no OCSP response exists */
5811 * Attempts to extract SCTs from the peer certificate.
5812 * Return the number of SCTs extracted, or a negative integer if an error
5815 static int ct_extract_x509v3_extension_scts(SSL_CONNECTION
*s
)
5817 int scts_extracted
= 0;
5818 X509
*cert
= s
->session
!= NULL
? s
->session
->peer
: NULL
;
5821 STACK_OF(SCT
) *scts
=
5822 X509_get_ext_d2i(cert
, NID_ct_precert_scts
, NULL
, NULL
);
5825 ct_move_scts(&s
->scts
, scts
, SCT_SOURCE_X509V3_EXTENSION
);
5827 SCT_LIST_free(scts
);
5830 return scts_extracted
;
5834 * Attempts to find all received SCTs by checking TLS extensions, the OCSP
5835 * response (if it exists) and X509v3 extensions in the certificate.
5836 * Returns NULL if an error occurs.
5838 const STACK_OF(SCT
) *SSL_get0_peer_scts(SSL
*s
)
5840 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
5845 if (!sc
->scts_parsed
) {
5846 if (ct_extract_tls_extension_scts(sc
) < 0 ||
5847 ct_extract_ocsp_response_scts(sc
) < 0 ||
5848 ct_extract_x509v3_extension_scts(sc
) < 0)
5851 sc
->scts_parsed
= 1;
5858 static int ct_permissive(const CT_POLICY_EVAL_CTX
* ctx
,
5859 const STACK_OF(SCT
) *scts
, void *unused_arg
)
5864 static int ct_strict(const CT_POLICY_EVAL_CTX
* ctx
,
5865 const STACK_OF(SCT
) *scts
, void *unused_arg
)
5867 int count
= scts
!= NULL
? sk_SCT_num(scts
) : 0;
5870 for (i
= 0; i
< count
; ++i
) {
5871 SCT
*sct
= sk_SCT_value(scts
, i
);
5872 int status
= SCT_get_validation_status(sct
);
5874 if (status
== SCT_VALIDATION_STATUS_VALID
)
5877 ERR_raise(ERR_LIB_SSL
, SSL_R_NO_VALID_SCTS
);
5881 int SSL_set_ct_validation_callback(SSL
*s
, ssl_ct_validation_cb callback
,
5884 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
5890 * Since code exists that uses the custom extension handler for CT, look
5891 * for this and throw an error if they have already registered to use CT.
5893 if (callback
!= NULL
&& SSL_CTX_has_client_custom_ext(s
->ctx
,
5894 TLSEXT_TYPE_signed_certificate_timestamp
))
5896 ERR_raise(ERR_LIB_SSL
, SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED
);
5900 if (callback
!= NULL
) {
5902 * If we are validating CT, then we MUST accept SCTs served via OCSP
5904 if (!SSL_set_tlsext_status_type(s
, TLSEXT_STATUSTYPE_ocsp
))
5908 sc
->ct_validation_callback
= callback
;
5909 sc
->ct_validation_callback_arg
= arg
;
5914 int SSL_CTX_set_ct_validation_callback(SSL_CTX
*ctx
,
5915 ssl_ct_validation_cb callback
, void *arg
)
5918 * Since code exists that uses the custom extension handler for CT, look for
5919 * this and throw an error if they have already registered to use CT.
5921 if (callback
!= NULL
&& SSL_CTX_has_client_custom_ext(ctx
,
5922 TLSEXT_TYPE_signed_certificate_timestamp
))
5924 ERR_raise(ERR_LIB_SSL
, SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED
);
5928 ctx
->ct_validation_callback
= callback
;
5929 ctx
->ct_validation_callback_arg
= arg
;
5933 int SSL_ct_is_enabled(const SSL
*s
)
5935 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
5940 return sc
->ct_validation_callback
!= NULL
;
5943 int SSL_CTX_ct_is_enabled(const SSL_CTX
*ctx
)
5945 return ctx
->ct_validation_callback
!= NULL
;
5948 int ssl_validate_ct(SSL_CONNECTION
*s
)
5951 X509
*cert
= s
->session
!= NULL
? s
->session
->peer
: NULL
;
5953 SSL_DANE
*dane
= &s
->dane
;
5954 CT_POLICY_EVAL_CTX
*ctx
= NULL
;
5955 const STACK_OF(SCT
) *scts
;
5958 * If no callback is set, the peer is anonymous, or its chain is invalid,
5959 * skip SCT validation - just return success. Applications that continue
5960 * handshakes without certificates, with unverified chains, or pinned leaf
5961 * certificates are outside the scope of the WebPKI and CT.
5963 * The above exclusions notwithstanding the vast majority of peers will
5964 * have rather ordinary certificate chains validated by typical
5965 * applications that perform certificate verification and therefore will
5966 * process SCTs when enabled.
5968 if (s
->ct_validation_callback
== NULL
|| cert
== NULL
||
5969 s
->verify_result
!= X509_V_OK
||
5970 s
->verified_chain
== NULL
|| sk_X509_num(s
->verified_chain
) <= 1)
5974 * CT not applicable for chains validated via DANE-TA(2) or DANE-EE(3)
5975 * trust-anchors. See https://tools.ietf.org/html/rfc7671#section-4.2
5977 if (DANETLS_ENABLED(dane
) && dane
->mtlsa
!= NULL
) {
5978 switch (dane
->mtlsa
->usage
) {
5979 case DANETLS_USAGE_DANE_TA
:
5980 case DANETLS_USAGE_DANE_EE
:
5985 ctx
= CT_POLICY_EVAL_CTX_new_ex(SSL_CONNECTION_GET_CTX(s
)->libctx
,
5986 SSL_CONNECTION_GET_CTX(s
)->propq
);
5988 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
5992 issuer
= sk_X509_value(s
->verified_chain
, 1);
5993 CT_POLICY_EVAL_CTX_set1_cert(ctx
, cert
);
5994 CT_POLICY_EVAL_CTX_set1_issuer(ctx
, issuer
);
5995 CT_POLICY_EVAL_CTX_set_shared_CTLOG_STORE(ctx
,
5996 SSL_CONNECTION_GET_CTX(s
)->ctlog_store
);
5997 CT_POLICY_EVAL_CTX_set_time(
5998 ctx
, (uint64_t)SSL_SESSION_get_time(s
->session
) * 1000);
6000 scts
= SSL_get0_peer_scts(SSL_CONNECTION_GET_SSL(s
));
6003 * This function returns success (> 0) only when all the SCTs are valid, 0
6004 * when some are invalid, and < 0 on various internal errors (out of
6005 * memory, etc.). Having some, or even all, invalid SCTs is not sufficient
6006 * reason to abort the handshake, that decision is up to the callback.
6007 * Therefore, we error out only in the unexpected case that the return
6008 * value is negative.
6010 * XXX: One might well argue that the return value of this function is an
6011 * unfortunate design choice. Its job is only to determine the validation
6012 * status of each of the provided SCTs. So long as it correctly separates
6013 * the wheat from the chaff it should return success. Failure in this case
6014 * ought to correspond to an inability to carry out its duties.
6016 if (SCT_LIST_validate(scts
, ctx
) < 0) {
6017 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_SCT_VERIFICATION_FAILED
);
6021 ret
= s
->ct_validation_callback(ctx
, scts
, s
->ct_validation_callback_arg
);
6023 ret
= 0; /* This function returns 0 on failure */
6025 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_CALLBACK_FAILED
);
6028 CT_POLICY_EVAL_CTX_free(ctx
);
6030 * With SSL_VERIFY_NONE the session may be cached and re-used despite a
6031 * failure return code here. Also the application may wish the complete
6032 * the handshake, and then disconnect cleanly at a higher layer, after
6033 * checking the verification status of the completed connection.
6035 * We therefore force a certificate verification failure which will be
6036 * visible via SSL_get_verify_result() and cached as part of any resumed
6039 * Note: the permissive callback is for information gathering only, always
6040 * returns success, and does not affect verification status. Only the
6041 * strict callback or a custom application-specified callback can trigger
6042 * connection failure or record a verification error.
6045 s
->verify_result
= X509_V_ERR_NO_VALID_SCTS
;
6049 int SSL_CTX_enable_ct(SSL_CTX
*ctx
, int validation_mode
)
6051 switch (validation_mode
) {
6053 ERR_raise(ERR_LIB_SSL
, SSL_R_INVALID_CT_VALIDATION_TYPE
);
6055 case SSL_CT_VALIDATION_PERMISSIVE
:
6056 return SSL_CTX_set_ct_validation_callback(ctx
, ct_permissive
, NULL
);
6057 case SSL_CT_VALIDATION_STRICT
:
6058 return SSL_CTX_set_ct_validation_callback(ctx
, ct_strict
, NULL
);
6062 int SSL_enable_ct(SSL
*s
, int validation_mode
)
6064 switch (validation_mode
) {
6066 ERR_raise(ERR_LIB_SSL
, SSL_R_INVALID_CT_VALIDATION_TYPE
);
6068 case SSL_CT_VALIDATION_PERMISSIVE
:
6069 return SSL_set_ct_validation_callback(s
, ct_permissive
, NULL
);
6070 case SSL_CT_VALIDATION_STRICT
:
6071 return SSL_set_ct_validation_callback(s
, ct_strict
, NULL
);
6075 int SSL_CTX_set_default_ctlog_list_file(SSL_CTX
*ctx
)
6077 return CTLOG_STORE_load_default_file(ctx
->ctlog_store
);
6080 int SSL_CTX_set_ctlog_list_file(SSL_CTX
*ctx
, const char *path
)
6082 return CTLOG_STORE_load_file(ctx
->ctlog_store
, path
);
6085 void SSL_CTX_set0_ctlog_store(SSL_CTX
*ctx
, CTLOG_STORE
* logs
)
6087 CTLOG_STORE_free(ctx
->ctlog_store
);
6088 ctx
->ctlog_store
= logs
;
6091 const CTLOG_STORE
*SSL_CTX_get0_ctlog_store(const SSL_CTX
*ctx
)
6093 return ctx
->ctlog_store
;
6096 #endif /* OPENSSL_NO_CT */
6098 void SSL_CTX_set_client_hello_cb(SSL_CTX
*c
, SSL_client_hello_cb_fn cb
,
6101 c
->client_hello_cb
= cb
;
6102 c
->client_hello_cb_arg
= arg
;
6105 int SSL_client_hello_isv2(SSL
*s
)
6107 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6112 if (sc
->clienthello
== NULL
)
6114 return sc
->clienthello
->isv2
;
6117 unsigned int SSL_client_hello_get0_legacy_version(SSL
*s
)
6119 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6124 if (sc
->clienthello
== NULL
)
6126 return sc
->clienthello
->legacy_version
;
6129 size_t SSL_client_hello_get0_random(SSL
*s
, const unsigned char **out
)
6131 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6136 if (sc
->clienthello
== NULL
)
6139 *out
= sc
->clienthello
->random
;
6140 return SSL3_RANDOM_SIZE
;
6143 size_t SSL_client_hello_get0_session_id(SSL
*s
, const unsigned char **out
)
6145 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6150 if (sc
->clienthello
== NULL
)
6153 *out
= sc
->clienthello
->session_id
;
6154 return sc
->clienthello
->session_id_len
;
6157 size_t SSL_client_hello_get0_ciphers(SSL
*s
, const unsigned char **out
)
6159 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6164 if (sc
->clienthello
== NULL
)
6167 *out
= PACKET_data(&sc
->clienthello
->ciphersuites
);
6168 return PACKET_remaining(&sc
->clienthello
->ciphersuites
);
6171 size_t SSL_client_hello_get0_compression_methods(SSL
*s
, const unsigned char **out
)
6173 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6178 if (sc
->clienthello
== NULL
)
6181 *out
= sc
->clienthello
->compressions
;
6182 return sc
->clienthello
->compressions_len
;
6185 int SSL_client_hello_get1_extensions_present(SSL
*s
, int **out
, size_t *outlen
)
6190 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6195 if (sc
->clienthello
== NULL
|| out
== NULL
|| outlen
== NULL
)
6197 for (i
= 0; i
< sc
->clienthello
->pre_proc_exts_len
; i
++) {
6198 ext
= sc
->clienthello
->pre_proc_exts
+ i
;
6207 if ((present
= OPENSSL_malloc(sizeof(*present
) * num
)) == NULL
) {
6208 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
6211 for (i
= 0; i
< sc
->clienthello
->pre_proc_exts_len
; i
++) {
6212 ext
= sc
->clienthello
->pre_proc_exts
+ i
;
6214 if (ext
->received_order
>= num
)
6216 present
[ext
->received_order
] = ext
->type
;
6223 OPENSSL_free(present
);
6227 int SSL_client_hello_get_extension_order(SSL
*s
, uint16_t *exts
, size_t *num_exts
)
6231 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6236 if (sc
->clienthello
== NULL
|| num_exts
== NULL
)
6238 for (i
= 0; i
< sc
->clienthello
->pre_proc_exts_len
; i
++) {
6239 ext
= sc
->clienthello
->pre_proc_exts
+ i
;
6251 if (*num_exts
< num
)
6253 for (i
= 0; i
< sc
->clienthello
->pre_proc_exts_len
; i
++) {
6254 ext
= sc
->clienthello
->pre_proc_exts
+ i
;
6256 if (ext
->received_order
>= num
)
6258 exts
[ext
->received_order
] = ext
->type
;
6265 int SSL_client_hello_get0_ext(SSL
*s
, unsigned int type
, const unsigned char **out
,
6270 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6275 if (sc
->clienthello
== NULL
)
6277 for (i
= 0; i
< sc
->clienthello
->pre_proc_exts_len
; ++i
) {
6278 r
= sc
->clienthello
->pre_proc_exts
+ i
;
6279 if (r
->present
&& r
->type
== type
) {
6281 *out
= PACKET_data(&r
->data
);
6283 *outlen
= PACKET_remaining(&r
->data
);
6290 int SSL_free_buffers(SSL
*ssl
)
6293 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
6300 if (RECORD_LAYER_read_pending(rl
) || RECORD_LAYER_write_pending(rl
))
6303 RECORD_LAYER_release(rl
);
6307 int SSL_alloc_buffers(SSL
*ssl
)
6309 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
6314 return ssl3_setup_buffers(sc
);
6317 void SSL_CTX_set_keylog_callback(SSL_CTX
*ctx
, SSL_CTX_keylog_cb_func cb
)
6319 ctx
->keylog_callback
= cb
;
6322 SSL_CTX_keylog_cb_func
SSL_CTX_get_keylog_callback(const SSL_CTX
*ctx
)
6324 return ctx
->keylog_callback
;
6327 static int nss_keylog_int(const char *prefix
,
6329 const uint8_t *parameter_1
,
6330 size_t parameter_1_len
,
6331 const uint8_t *parameter_2
,
6332 size_t parameter_2_len
)
6335 char *cursor
= NULL
;
6339 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(sc
);
6341 if (sctx
->keylog_callback
== NULL
)
6345 * Our output buffer will contain the following strings, rendered with
6346 * space characters in between, terminated by a NULL character: first the
6347 * prefix, then the first parameter, then the second parameter. The
6348 * meaning of each parameter depends on the specific key material being
6349 * logged. Note that the first and second parameters are encoded in
6350 * hexadecimal, so we need a buffer that is twice their lengths.
6352 prefix_len
= strlen(prefix
);
6353 out_len
= prefix_len
+ (2 * parameter_1_len
) + (2 * parameter_2_len
) + 3;
6354 if ((out
= cursor
= OPENSSL_malloc(out_len
)) == NULL
) {
6355 SSLfatal(sc
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
6359 strcpy(cursor
, prefix
);
6360 cursor
+= prefix_len
;
6363 for (i
= 0; i
< parameter_1_len
; i
++) {
6364 sprintf(cursor
, "%02x", parameter_1
[i
]);
6369 for (i
= 0; i
< parameter_2_len
; i
++) {
6370 sprintf(cursor
, "%02x", parameter_2
[i
]);
6375 sctx
->keylog_callback(SSL_CONNECTION_GET_SSL(sc
), (const char *)out
);
6376 OPENSSL_clear_free(out
, out_len
);
6381 int ssl_log_rsa_client_key_exchange(SSL_CONNECTION
*sc
,
6382 const uint8_t *encrypted_premaster
,
6383 size_t encrypted_premaster_len
,
6384 const uint8_t *premaster
,
6385 size_t premaster_len
)
6387 if (encrypted_premaster_len
< 8) {
6388 SSLfatal(sc
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
6392 /* We only want the first 8 bytes of the encrypted premaster as a tag. */
6393 return nss_keylog_int("RSA",
6395 encrypted_premaster
,
6401 int ssl_log_secret(SSL_CONNECTION
*sc
,
6403 const uint8_t *secret
,
6406 return nss_keylog_int(label
,
6408 sc
->s3
.client_random
,
6414 #define SSLV2_CIPHER_LEN 3
6416 int ssl_cache_cipherlist(SSL_CONNECTION
*s
, PACKET
*cipher_suites
, int sslv2format
)
6420 n
= sslv2format
? SSLV2_CIPHER_LEN
: TLS_CIPHER_LEN
;
6422 if (PACKET_remaining(cipher_suites
) == 0) {
6423 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_NO_CIPHERS_SPECIFIED
);
6427 if (PACKET_remaining(cipher_suites
) % n
!= 0) {
6428 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST
);
6432 OPENSSL_free(s
->s3
.tmp
.ciphers_raw
);
6433 s
->s3
.tmp
.ciphers_raw
= NULL
;
6434 s
->s3
.tmp
.ciphers_rawlen
= 0;
6437 size_t numciphers
= PACKET_remaining(cipher_suites
) / n
;
6438 PACKET sslv2ciphers
= *cipher_suites
;
6439 unsigned int leadbyte
;
6443 * We store the raw ciphers list in SSLv3+ format so we need to do some
6444 * preprocessing to convert the list first. If there are any SSLv2 only
6445 * ciphersuites with a non-zero leading byte then we are going to
6446 * slightly over allocate because we won't store those. But that isn't a
6449 raw
= OPENSSL_malloc(numciphers
* TLS_CIPHER_LEN
);
6450 s
->s3
.tmp
.ciphers_raw
= raw
;
6452 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
6455 for (s
->s3
.tmp
.ciphers_rawlen
= 0;
6456 PACKET_remaining(&sslv2ciphers
) > 0;
6457 raw
+= TLS_CIPHER_LEN
) {
6458 if (!PACKET_get_1(&sslv2ciphers
, &leadbyte
)
6460 && !PACKET_copy_bytes(&sslv2ciphers
, raw
,
6463 && !PACKET_forward(&sslv2ciphers
, TLS_CIPHER_LEN
))) {
6464 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_PACKET
);
6465 OPENSSL_free(s
->s3
.tmp
.ciphers_raw
);
6466 s
->s3
.tmp
.ciphers_raw
= NULL
;
6467 s
->s3
.tmp
.ciphers_rawlen
= 0;
6471 s
->s3
.tmp
.ciphers_rawlen
+= TLS_CIPHER_LEN
;
6473 } else if (!PACKET_memdup(cipher_suites
, &s
->s3
.tmp
.ciphers_raw
,
6474 &s
->s3
.tmp
.ciphers_rawlen
)) {
6475 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
6481 int SSL_bytes_to_cipher_list(SSL
*s
, const unsigned char *bytes
, size_t len
,
6482 int isv2format
, STACK_OF(SSL_CIPHER
) **sk
,
6483 STACK_OF(SSL_CIPHER
) **scsvs
)
6486 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6491 if (!PACKET_buf_init(&pkt
, bytes
, len
))
6493 return ossl_bytes_to_cipher_list(sc
, &pkt
, sk
, scsvs
, isv2format
, 0);
6496 int ossl_bytes_to_cipher_list(SSL_CONNECTION
*s
, PACKET
*cipher_suites
,
6497 STACK_OF(SSL_CIPHER
) **skp
,
6498 STACK_OF(SSL_CIPHER
) **scsvs_out
,
6499 int sslv2format
, int fatal
)
6501 const SSL_CIPHER
*c
;
6502 STACK_OF(SSL_CIPHER
) *sk
= NULL
;
6503 STACK_OF(SSL_CIPHER
) *scsvs
= NULL
;
6505 /* 3 = SSLV2_CIPHER_LEN > TLS_CIPHER_LEN = 2. */
6506 unsigned char cipher
[SSLV2_CIPHER_LEN
];
6508 n
= sslv2format
? SSLV2_CIPHER_LEN
: TLS_CIPHER_LEN
;
6510 if (PACKET_remaining(cipher_suites
) == 0) {
6512 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_NO_CIPHERS_SPECIFIED
);
6514 ERR_raise(ERR_LIB_SSL
, SSL_R_NO_CIPHERS_SPECIFIED
);
6518 if (PACKET_remaining(cipher_suites
) % n
!= 0) {
6520 SSLfatal(s
, SSL_AD_DECODE_ERROR
,
6521 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST
);
6523 ERR_raise(ERR_LIB_SSL
, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST
);
6527 sk
= sk_SSL_CIPHER_new_null();
6528 scsvs
= sk_SSL_CIPHER_new_null();
6529 if (sk
== NULL
|| scsvs
== NULL
) {
6531 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
6533 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
6537 while (PACKET_copy_bytes(cipher_suites
, cipher
, n
)) {
6539 * SSLv3 ciphers wrapped in an SSLv2-compatible ClientHello have the
6540 * first byte set to zero, while true SSLv2 ciphers have a non-zero
6541 * first byte. We don't support any true SSLv2 ciphers, so skip them.
6543 if (sslv2format
&& cipher
[0] != '\0')
6546 /* For SSLv2-compat, ignore leading 0-byte. */
6547 c
= ssl_get_cipher_by_char(s
, sslv2format
? &cipher
[1] : cipher
, 1);
6549 if ((c
->valid
&& !sk_SSL_CIPHER_push(sk
, c
)) ||
6550 (!c
->valid
&& !sk_SSL_CIPHER_push(scsvs
, c
))) {
6552 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
6554 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
6559 if (PACKET_remaining(cipher_suites
) > 0) {
6561 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_LENGTH
);
6563 ERR_raise(ERR_LIB_SSL
, SSL_R_BAD_LENGTH
);
6570 sk_SSL_CIPHER_free(sk
);
6571 if (scsvs_out
!= NULL
)
6574 sk_SSL_CIPHER_free(scsvs
);
6577 sk_SSL_CIPHER_free(sk
);
6578 sk_SSL_CIPHER_free(scsvs
);
6582 int SSL_CTX_set_max_early_data(SSL_CTX
*ctx
, uint32_t max_early_data
)
6584 ctx
->max_early_data
= max_early_data
;
6589 uint32_t SSL_CTX_get_max_early_data(const SSL_CTX
*ctx
)
6591 return ctx
->max_early_data
;
6594 int SSL_set_max_early_data(SSL
*s
, uint32_t max_early_data
)
6596 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6601 sc
->max_early_data
= max_early_data
;
6606 uint32_t SSL_get_max_early_data(const SSL
*s
)
6608 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
6613 return sc
->max_early_data
;
6616 int SSL_CTX_set_recv_max_early_data(SSL_CTX
*ctx
, uint32_t recv_max_early_data
)
6618 ctx
->recv_max_early_data
= recv_max_early_data
;
6623 uint32_t SSL_CTX_get_recv_max_early_data(const SSL_CTX
*ctx
)
6625 return ctx
->recv_max_early_data
;
6628 int SSL_set_recv_max_early_data(SSL
*s
, uint32_t recv_max_early_data
)
6630 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6635 sc
->recv_max_early_data
= recv_max_early_data
;
6640 uint32_t SSL_get_recv_max_early_data(const SSL
*s
)
6642 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
6647 return sc
->recv_max_early_data
;
6650 __owur
unsigned int ssl_get_max_send_fragment(const SSL_CONNECTION
*sc
)
6652 /* Return any active Max Fragment Len extension */
6653 if (sc
->session
!= NULL
&& USE_MAX_FRAGMENT_LENGTH_EXT(sc
->session
))
6654 return GET_MAX_FRAGMENT_LENGTH(sc
->session
);
6656 /* return current SSL connection setting */
6657 return sc
->max_send_fragment
;
6660 __owur
unsigned int ssl_get_split_send_fragment(const SSL_CONNECTION
*sc
)
6662 /* Return a value regarding an active Max Fragment Len extension */
6663 if (sc
->session
!= NULL
&& USE_MAX_FRAGMENT_LENGTH_EXT(sc
->session
)
6664 && sc
->split_send_fragment
> GET_MAX_FRAGMENT_LENGTH(sc
->session
))
6665 return GET_MAX_FRAGMENT_LENGTH(sc
->session
);
6667 /* else limit |split_send_fragment| to current |max_send_fragment| */
6668 if (sc
->split_send_fragment
> sc
->max_send_fragment
)
6669 return sc
->max_send_fragment
;
6671 /* return current SSL connection setting */
6672 return sc
->split_send_fragment
;
6675 int SSL_stateless(SSL
*s
)
6678 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6680 /* TODO(QUIC): This will need further work. */
6684 /* Ensure there is no state left over from a previous invocation */
6690 sc
->s3
.flags
|= TLS1_FLAGS_STATELESS
;
6691 ret
= SSL_accept(s
);
6692 sc
->s3
.flags
&= ~TLS1_FLAGS_STATELESS
;
6694 if (ret
> 0 && sc
->ext
.cookieok
)
6697 if (sc
->hello_retry_request
== SSL_HRR_PENDING
&& !ossl_statem_in_error(sc
))
6703 void SSL_CTX_set_post_handshake_auth(SSL_CTX
*ctx
, int val
)
6705 ctx
->pha_enabled
= val
;
6708 void SSL_set_post_handshake_auth(SSL
*ssl
, int val
)
6710 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
6715 sc
->pha_enabled
= val
;
6718 int SSL_verify_client_post_handshake(SSL
*ssl
)
6720 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(ssl
);
6725 if (!SSL_CONNECTION_IS_TLS13(sc
)) {
6726 ERR_raise(ERR_LIB_SSL
, SSL_R_WRONG_SSL_VERSION
);
6730 ERR_raise(ERR_LIB_SSL
, SSL_R_NOT_SERVER
);
6734 if (!SSL_is_init_finished(ssl
)) {
6735 ERR_raise(ERR_LIB_SSL
, SSL_R_STILL_IN_INIT
);
6739 switch (sc
->post_handshake_auth
) {
6741 ERR_raise(ERR_LIB_SSL
, SSL_R_EXTENSION_NOT_RECEIVED
);
6744 case SSL_PHA_EXT_SENT
:
6745 ERR_raise(ERR_LIB_SSL
, ERR_R_INTERNAL_ERROR
);
6747 case SSL_PHA_EXT_RECEIVED
:
6749 case SSL_PHA_REQUEST_PENDING
:
6750 ERR_raise(ERR_LIB_SSL
, SSL_R_REQUEST_PENDING
);
6752 case SSL_PHA_REQUESTED
:
6753 ERR_raise(ERR_LIB_SSL
, SSL_R_REQUEST_SENT
);
6757 sc
->post_handshake_auth
= SSL_PHA_REQUEST_PENDING
;
6759 /* checks verify_mode and algorithm_auth */
6760 if (!send_certificate_request(sc
)) {
6761 sc
->post_handshake_auth
= SSL_PHA_EXT_RECEIVED
; /* restore on error */
6762 ERR_raise(ERR_LIB_SSL
, SSL_R_INVALID_CONFIG
);
6766 ossl_statem_set_in_init(sc
, 1);
6770 int SSL_CTX_set_session_ticket_cb(SSL_CTX
*ctx
,
6771 SSL_CTX_generate_session_ticket_fn gen_cb
,
6772 SSL_CTX_decrypt_session_ticket_fn dec_cb
,
6775 ctx
->generate_ticket_cb
= gen_cb
;
6776 ctx
->decrypt_ticket_cb
= dec_cb
;
6777 ctx
->ticket_cb_data
= arg
;
6781 void SSL_CTX_set_allow_early_data_cb(SSL_CTX
*ctx
,
6782 SSL_allow_early_data_cb_fn cb
,
6785 ctx
->allow_early_data_cb
= cb
;
6786 ctx
->allow_early_data_cb_data
= arg
;
6789 void SSL_set_allow_early_data_cb(SSL
*s
,
6790 SSL_allow_early_data_cb_fn cb
,
6793 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6798 sc
->allow_early_data_cb
= cb
;
6799 sc
->allow_early_data_cb_data
= arg
;
6802 const EVP_CIPHER
*ssl_evp_cipher_fetch(OSSL_LIB_CTX
*libctx
,
6804 const char *properties
)
6806 const EVP_CIPHER
*ciph
;
6808 ciph
= tls_get_cipher_from_engine(nid
);
6813 * If there is no engine cipher then we do an explicit fetch. This may fail
6814 * and that could be ok
6817 ciph
= EVP_CIPHER_fetch(libctx
, OBJ_nid2sn(nid
), properties
);
6823 int ssl_evp_cipher_up_ref(const EVP_CIPHER
*cipher
)
6825 /* Don't up-ref an implicit EVP_CIPHER */
6826 if (EVP_CIPHER_get0_provider(cipher
) == NULL
)
6830 * The cipher was explicitly fetched and therefore it is safe to cast
6833 return EVP_CIPHER_up_ref((EVP_CIPHER
*)cipher
);
6836 void ssl_evp_cipher_free(const EVP_CIPHER
*cipher
)
6841 if (EVP_CIPHER_get0_provider(cipher
) != NULL
) {
6843 * The cipher was explicitly fetched and therefore it is safe to cast
6846 EVP_CIPHER_free((EVP_CIPHER
*)cipher
);
6850 const EVP_MD
*ssl_evp_md_fetch(OSSL_LIB_CTX
*libctx
,
6852 const char *properties
)
6856 md
= tls_get_digest_from_engine(nid
);
6860 /* Otherwise we do an explicit fetch */
6862 md
= EVP_MD_fetch(libctx
, OBJ_nid2sn(nid
), properties
);
6867 int ssl_evp_md_up_ref(const EVP_MD
*md
)
6869 /* Don't up-ref an implicit EVP_MD */
6870 if (EVP_MD_get0_provider(md
) == NULL
)
6874 * The digest was explicitly fetched and therefore it is safe to cast
6877 return EVP_MD_up_ref((EVP_MD
*)md
);
6880 void ssl_evp_md_free(const EVP_MD
*md
)
6885 if (EVP_MD_get0_provider(md
) != NULL
) {
6887 * The digest was explicitly fetched and therefore it is safe to cast
6890 EVP_MD_free((EVP_MD
*)md
);
6894 int SSL_set0_tmp_dh_pkey(SSL
*s
, EVP_PKEY
*dhpkey
)
6896 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
6901 if (!ssl_security(sc
, SSL_SECOP_TMP_DH
,
6902 EVP_PKEY_get_security_bits(dhpkey
), 0, dhpkey
)) {
6903 ERR_raise(ERR_LIB_SSL
, SSL_R_DH_KEY_TOO_SMALL
);
6906 EVP_PKEY_free(sc
->cert
->dh_tmp
);
6907 sc
->cert
->dh_tmp
= dhpkey
;
6911 int SSL_CTX_set0_tmp_dh_pkey(SSL_CTX
*ctx
, EVP_PKEY
*dhpkey
)
6913 if (!ssl_ctx_security(ctx
, SSL_SECOP_TMP_DH
,
6914 EVP_PKEY_get_security_bits(dhpkey
), 0, dhpkey
)) {
6915 ERR_raise(ERR_LIB_SSL
, SSL_R_DH_KEY_TOO_SMALL
);
6918 EVP_PKEY_free(ctx
->cert
->dh_tmp
);
6919 ctx
->cert
->dh_tmp
= dhpkey
;