2 * Copyright 1995-2023 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 <openssl/core_names.h>
25 #include "internal/cryptlib.h"
26 #include "internal/nelem.h"
27 #include "internal/refcount.h"
28 #include "internal/ktls.h"
29 #include "quic/quic_local.h"
31 static int ssl_undefined_function_3(SSL_CONNECTION *sc, unsigned char *r,
32 unsigned char *s, size_t t, size_t *u)
34 return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc));
37 static int ssl_undefined_function_4(SSL_CONNECTION *sc, int r)
39 return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc));
42 static size_t ssl_undefined_function_5(SSL_CONNECTION *sc, const char *r,
43 size_t s, unsigned char *t)
45 return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc));
48 static int ssl_undefined_function_6(int r)
50 return ssl_undefined_function(NULL);
53 static int ssl_undefined_function_7(SSL_CONNECTION *sc, unsigned char *r,
54 size_t s, const char *t, size_t u,
55 const unsigned char *v, size_t w, int x)
57 return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc));
60 static int ssl_undefined_function_8(SSL_CONNECTION *sc)
62 return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc));
65 SSL3_ENC_METHOD ssl3_undef_enc_method = {
66 ssl_undefined_function_8,
67 ssl_undefined_function_3,
68 ssl_undefined_function_4,
69 ssl_undefined_function_5,
70 NULL, /* client_finished_label */
71 0, /* client_finished_label_len */
72 NULL, /* server_finished_label */
73 0, /* server_finished_label_len */
74 ssl_undefined_function_6,
75 ssl_undefined_function_7,
78 struct ssl_async_args {
82 enum { READFUNC, WRITEFUNC, OTHERFUNC } type;
84 int (*func_read) (SSL *, void *, size_t, size_t *);
85 int (*func_write) (SSL *, const void *, size_t, size_t *);
86 int (*func_other) (SSL *);
96 DANETLS_MATCHING_FULL, 0, NID_undef
99 DANETLS_MATCHING_2256, 1, NID_sha256
102 DANETLS_MATCHING_2512, 2, NID_sha512
106 static int dane_ctx_enable(struct dane_ctx_st *dctx)
108 const EVP_MD **mdevp;
110 uint8_t mdmax = DANETLS_MATCHING_LAST;
111 int n = ((int)mdmax) + 1; /* int to handle PrivMatch(255) */
114 if (dctx->mdevp != NULL)
117 mdevp = OPENSSL_zalloc(n * sizeof(*mdevp));
118 mdord = OPENSSL_zalloc(n * sizeof(*mdord));
120 if (mdord == NULL || mdevp == NULL) {
126 /* Install default entries */
127 for (i = 0; i < OSSL_NELEM(dane_mds); ++i) {
130 if (dane_mds[i].nid == NID_undef ||
131 (md = EVP_get_digestbynid(dane_mds[i].nid)) == NULL)
133 mdevp[dane_mds[i].mtype] = md;
134 mdord[dane_mds[i].mtype] = dane_mds[i].ord;
144 static void dane_ctx_final(struct dane_ctx_st *dctx)
146 OPENSSL_free(dctx->mdevp);
149 OPENSSL_free(dctx->mdord);
154 static void tlsa_free(danetls_record *t)
158 OPENSSL_free(t->data);
159 EVP_PKEY_free(t->spki);
163 static void dane_final(SSL_DANE *dane)
165 sk_danetls_record_pop_free(dane->trecs, tlsa_free);
168 OSSL_STACK_OF_X509_free(dane->certs);
171 X509_free(dane->mcert);
179 * dane_copy - Copy dane configuration, sans verification state.
181 static int ssl_dane_dup(SSL_CONNECTION *to, SSL_CONNECTION *from)
186 if (!DANETLS_ENABLED(&from->dane))
189 num = sk_danetls_record_num(from->dane.trecs);
190 dane_final(&to->dane);
191 to->dane.flags = from->dane.flags;
192 to->dane.dctx = &SSL_CONNECTION_GET_CTX(to)->dane;
193 to->dane.trecs = sk_danetls_record_new_reserve(NULL, num);
195 if (to->dane.trecs == NULL) {
196 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
200 for (i = 0; i < num; ++i) {
201 danetls_record *t = sk_danetls_record_value(from->dane.trecs, i);
203 if (SSL_dane_tlsa_add(SSL_CONNECTION_GET_SSL(to), t->usage,
204 t->selector, t->mtype, t->data, t->dlen) <= 0)
210 static int dane_mtype_set(struct dane_ctx_st *dctx,
211 const EVP_MD *md, uint8_t mtype, uint8_t ord)
215 if (mtype == DANETLS_MATCHING_FULL && md != NULL) {
216 ERR_raise(ERR_LIB_SSL, SSL_R_DANE_CANNOT_OVERRIDE_MTYPE_FULL);
220 if (mtype > dctx->mdmax) {
221 const EVP_MD **mdevp;
223 int n = ((int)mtype) + 1;
225 mdevp = OPENSSL_realloc(dctx->mdevp, n * sizeof(*mdevp));
230 mdord = OPENSSL_realloc(dctx->mdord, n * sizeof(*mdord));
235 /* Zero-fill any gaps */
236 for (i = dctx->mdmax + 1; i < mtype; ++i) {
244 dctx->mdevp[mtype] = md;
245 /* Coerce ordinal of disabled matching types to 0 */
246 dctx->mdord[mtype] = (md == NULL) ? 0 : ord;
251 static const EVP_MD *tlsa_md_get(SSL_DANE *dane, uint8_t mtype)
253 if (mtype > dane->dctx->mdmax)
255 return dane->dctx->mdevp[mtype];
258 static int dane_tlsa_add(SSL_DANE *dane,
261 uint8_t mtype, const unsigned char *data, size_t dlen)
264 const EVP_MD *md = NULL;
265 int ilen = (int)dlen;
269 if (dane->trecs == NULL) {
270 ERR_raise(ERR_LIB_SSL, SSL_R_DANE_NOT_ENABLED);
274 if (ilen < 0 || dlen != (size_t)ilen) {
275 ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_DATA_LENGTH);
279 if (usage > DANETLS_USAGE_LAST) {
280 ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_CERTIFICATE_USAGE);
284 if (selector > DANETLS_SELECTOR_LAST) {
285 ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_SELECTOR);
289 if (mtype != DANETLS_MATCHING_FULL) {
290 md = tlsa_md_get(dane, mtype);
292 ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_MATCHING_TYPE);
297 if (md != NULL && dlen != (size_t)EVP_MD_get_size(md)) {
298 ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_DIGEST_LENGTH);
302 ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_NULL_DATA);
306 if ((t = OPENSSL_zalloc(sizeof(*t))) == NULL)
310 t->selector = selector;
312 t->data = OPENSSL_malloc(dlen);
313 if (t->data == NULL) {
317 memcpy(t->data, data, dlen);
320 /* Validate and cache full certificate or public key */
321 if (mtype == DANETLS_MATCHING_FULL) {
322 const unsigned char *p = data;
324 EVP_PKEY *pkey = NULL;
327 case DANETLS_SELECTOR_CERT:
328 if (!d2i_X509(&cert, &p, ilen) || p < data ||
329 dlen != (size_t)(p - data)) {
332 ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_CERTIFICATE);
335 if (X509_get0_pubkey(cert) == NULL) {
338 ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_CERTIFICATE);
342 if ((DANETLS_USAGE_BIT(usage) & DANETLS_TA_MASK) == 0) {
344 * The Full(0) certificate decodes to a seemingly valid X.509
345 * object with a plausible key, so the TLSA record is well
346 * formed. However, we don't actually need the certifiate for
347 * usages PKIX-EE(1) or DANE-EE(3), because at least the EE
348 * certificate is always presented by the peer. We discard the
349 * certificate, and just use the TLSA data as an opaque blob
350 * for matching the raw presented DER octets.
352 * DO NOT FREE `t` here, it will be added to the TLSA record
360 * For usage DANE-TA(2), we support authentication via "2 0 0" TLSA
361 * records that contain full certificates of trust-anchors that are
362 * not present in the wire chain. For usage PKIX-TA(0), we augment
363 * the chain with untrusted Full(0) certificates from DNS, in case
364 * they are missing from the chain.
366 if ((dane->certs == NULL &&
367 (dane->certs = sk_X509_new_null()) == NULL) ||
368 !sk_X509_push(dane->certs, cert)) {
369 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
376 case DANETLS_SELECTOR_SPKI:
377 if (!d2i_PUBKEY(&pkey, &p, ilen) || p < data ||
378 dlen != (size_t)(p - data)) {
381 ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_PUBLIC_KEY);
386 * For usage DANE-TA(2), we support authentication via "2 1 0" TLSA
387 * records that contain full bare keys of trust-anchors that are
388 * not present in the wire chain.
390 if (usage == DANETLS_USAGE_DANE_TA)
399 * Find the right insertion point for the new record.
401 * See crypto/x509/x509_vfy.c. We sort DANE-EE(3) records first, so that
402 * they can be processed first, as they require no chain building, and no
403 * expiration or hostname checks. Because DANE-EE(3) is numerically
404 * largest, this is accomplished via descending sort by "usage".
406 * We also sort in descending order by matching ordinal to simplify
407 * the implementation of digest agility in the verification code.
409 * The choice of order for the selector is not significant, so we
410 * use the same descending order for consistency.
412 num = sk_danetls_record_num(dane->trecs);
413 for (i = 0; i < num; ++i) {
414 danetls_record *rec = sk_danetls_record_value(dane->trecs, i);
416 if (rec->usage > usage)
418 if (rec->usage < usage)
420 if (rec->selector > selector)
422 if (rec->selector < selector)
424 if (dane->dctx->mdord[rec->mtype] > dane->dctx->mdord[mtype])
429 if (!sk_danetls_record_insert(dane->trecs, t, i)) {
431 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
434 dane->umask |= DANETLS_USAGE_BIT(usage);
440 * Return 0 if there is only one version configured and it was disabled
441 * at configure time. Return 1 otherwise.
443 static int ssl_check_allowed_versions(int min_version, int max_version)
445 int minisdtls = 0, maxisdtls = 0;
447 /* Figure out if we're doing DTLS versions or TLS versions */
448 if (min_version == DTLS1_BAD_VER
449 || min_version >> 8 == DTLS1_VERSION_MAJOR)
451 if (max_version == DTLS1_BAD_VER
452 || max_version >> 8 == DTLS1_VERSION_MAJOR)
454 /* A wildcard version of 0 could be DTLS or TLS. */
455 if ((minisdtls && !maxisdtls && max_version != 0)
456 || (maxisdtls && !minisdtls && min_version != 0)) {
457 /* Mixing DTLS and TLS versions will lead to sadness; deny it. */
461 if (minisdtls || maxisdtls) {
462 /* Do DTLS version checks. */
463 if (min_version == 0)
464 /* Ignore DTLS1_BAD_VER */
465 min_version = DTLS1_VERSION;
466 if (max_version == 0)
467 max_version = DTLS1_2_VERSION;
468 #ifdef OPENSSL_NO_DTLS1_2
469 if (max_version == DTLS1_2_VERSION)
470 max_version = DTLS1_VERSION;
472 #ifdef OPENSSL_NO_DTLS1
473 if (min_version == DTLS1_VERSION)
474 min_version = DTLS1_2_VERSION;
476 /* Done massaging versions; do the check. */
478 #ifdef OPENSSL_NO_DTLS1
479 || (DTLS_VERSION_GE(min_version, DTLS1_VERSION)
480 && DTLS_VERSION_GE(DTLS1_VERSION, max_version))
482 #ifdef OPENSSL_NO_DTLS1_2
483 || (DTLS_VERSION_GE(min_version, DTLS1_2_VERSION)
484 && DTLS_VERSION_GE(DTLS1_2_VERSION, max_version))
489 /* Regular TLS version checks. */
490 if (min_version == 0)
491 min_version = SSL3_VERSION;
492 if (max_version == 0)
493 max_version = TLS1_3_VERSION;
494 #ifdef OPENSSL_NO_TLS1_3
495 if (max_version == TLS1_3_VERSION)
496 max_version = TLS1_2_VERSION;
498 #ifdef OPENSSL_NO_TLS1_2
499 if (max_version == TLS1_2_VERSION)
500 max_version = TLS1_1_VERSION;
502 #ifdef OPENSSL_NO_TLS1_1
503 if (max_version == TLS1_1_VERSION)
504 max_version = TLS1_VERSION;
506 #ifdef OPENSSL_NO_TLS1
507 if (max_version == TLS1_VERSION)
508 max_version = SSL3_VERSION;
510 #ifdef OPENSSL_NO_SSL3
511 if (min_version == SSL3_VERSION)
512 min_version = TLS1_VERSION;
514 #ifdef OPENSSL_NO_TLS1
515 if (min_version == TLS1_VERSION)
516 min_version = TLS1_1_VERSION;
518 #ifdef OPENSSL_NO_TLS1_1
519 if (min_version == TLS1_1_VERSION)
520 min_version = TLS1_2_VERSION;
522 #ifdef OPENSSL_NO_TLS1_2
523 if (min_version == TLS1_2_VERSION)
524 min_version = TLS1_3_VERSION;
526 /* Done massaging versions; do the check. */
528 #ifdef OPENSSL_NO_SSL3
529 || (min_version <= SSL3_VERSION && SSL3_VERSION <= max_version)
531 #ifdef OPENSSL_NO_TLS1
532 || (min_version <= TLS1_VERSION && TLS1_VERSION <= max_version)
534 #ifdef OPENSSL_NO_TLS1_1
535 || (min_version <= TLS1_1_VERSION && TLS1_1_VERSION <= max_version)
537 #ifdef OPENSSL_NO_TLS1_2
538 || (min_version <= TLS1_2_VERSION && TLS1_2_VERSION <= max_version)
540 #ifdef OPENSSL_NO_TLS1_3
541 || (min_version <= TLS1_3_VERSION && TLS1_3_VERSION <= max_version)
549 #if defined(__TANDEM) && defined(OPENSSL_VPROC)
551 * Define a VPROC function for HP NonStop build ssl library.
552 * This is used by platform version identification tools.
553 * Do not inline this procedure or make it static.
555 # define OPENSSL_VPROC_STRING_(x) x##_SSL
556 # define OPENSSL_VPROC_STRING(x) OPENSSL_VPROC_STRING_(x)
557 # define OPENSSL_VPROC_FUNC OPENSSL_VPROC_STRING(OPENSSL_VPROC)
558 void OPENSSL_VPROC_FUNC(void) {}
561 static int clear_record_layer(SSL_CONNECTION *s)
565 /* We try and reset both record layers even if one fails */
567 ret = ssl_set_new_record_layer(s,
568 SSL_CONNECTION_IS_DTLS(s) ? DTLS_ANY_VERSION
570 OSSL_RECORD_DIRECTION_READ,
571 OSSL_RECORD_PROTECTION_LEVEL_NONE, NULL, 0,
572 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
573 NID_undef, NULL, NULL, NULL);
575 ret &= ssl_set_new_record_layer(s,
576 SSL_CONNECTION_IS_DTLS(s) ? DTLS_ANY_VERSION
578 OSSL_RECORD_DIRECTION_WRITE,
579 OSSL_RECORD_PROTECTION_LEVEL_NONE, NULL, 0,
580 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
581 NID_undef, NULL, NULL, NULL);
583 /* SSLfatal already called in the event of failure */
587 int SSL_clear(SSL *s)
589 if (s->method == NULL) {
590 ERR_raise(ERR_LIB_SSL, SSL_R_NO_METHOD_SPECIFIED);
594 return s->method->ssl_reset(s);
597 int ossl_ssl_connection_reset(SSL *s)
599 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
604 if (ssl_clear_bad_session(sc)) {
605 SSL_SESSION_free(sc->session);
608 SSL_SESSION_free(sc->psksession);
609 sc->psksession = NULL;
610 OPENSSL_free(sc->psksession_id);
611 sc->psksession_id = NULL;
612 sc->psksession_id_len = 0;
613 sc->hello_retry_request = SSL_HRR_NONE;
614 sc->sent_tickets = 0;
620 if (sc->renegotiate) {
621 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
625 ossl_statem_clear(sc);
627 sc->version = s->method->version;
628 sc->client_version = sc->version;
629 sc->rwstate = SSL_NOTHING;
631 BUF_MEM_free(sc->init_buf);
633 sc->first_packet = 0;
635 sc->key_update = SSL_KEY_UPDATE_NONE;
636 memset(sc->ext.compress_certificate_from_peer, 0,
637 sizeof(sc->ext.compress_certificate_from_peer));
638 sc->ext.compress_certificate_sent = 0;
640 EVP_MD_CTX_free(sc->pha_dgst);
643 /* Reset DANE verification result state */
646 X509_free(sc->dane.mcert);
647 sc->dane.mcert = NULL;
648 sc->dane.mtlsa = NULL;
650 /* Clear the verification result peername */
651 X509_VERIFY_PARAM_move_peername(sc->param, NULL);
653 /* Clear any shared connection state */
654 OPENSSL_free(sc->shared_sigalgs);
655 sc->shared_sigalgs = NULL;
656 sc->shared_sigalgslen = 0;
659 * Check to see if we were changed into a different method, if so, revert
662 if (s->method != s->defltmeth) {
663 s->method->ssl_deinit(s);
664 s->method = s->defltmeth;
665 if (!s->method->ssl_init(s))
668 if (!s->method->ssl_clear(s))
672 RECORD_LAYER_clear(&sc->rlayer);
673 BIO_free(sc->rlayer.rrlnext);
674 sc->rlayer.rrlnext = NULL;
676 if (!clear_record_layer(sc))
682 #ifndef OPENSSL_NO_DEPRECATED_3_0
683 /** Used to change an SSL_CTXs default SSL method type */
684 int SSL_CTX_set_ssl_version(SSL_CTX *ctx, const SSL_METHOD *meth)
686 STACK_OF(SSL_CIPHER) *sk;
688 if (IS_QUIC_CTX(ctx)) {
689 ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION);
695 if (!SSL_CTX_set_ciphersuites(ctx, OSSL_default_ciphersuites())) {
696 ERR_raise(ERR_LIB_SSL, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
699 sk = ssl_create_cipher_list(ctx,
700 ctx->tls13_ciphersuites,
702 &(ctx->cipher_list_by_id),
703 OSSL_default_cipher_list(), ctx->cert);
704 if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0)) {
705 ERR_raise(ERR_LIB_SSL, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
712 SSL *SSL_new(SSL_CTX *ctx)
715 ERR_raise(ERR_LIB_SSL, SSL_R_NULL_SSL_CTX);
718 if (ctx->method == NULL) {
719 ERR_raise(ERR_LIB_SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
722 return ctx->method->ssl_new(ctx);
725 int ossl_ssl_init(SSL *ssl, SSL_CTX *ctx, const SSL_METHOD *method, int type)
729 ssl->lock = CRYPTO_THREAD_lock_new();
730 if (ssl->lock == NULL)
733 if (!CRYPTO_NEW_REF(&ssl->references, 1)) {
734 CRYPTO_THREAD_lock_free(ssl->lock);
738 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, ssl, &ssl->ex_data)) {
739 CRYPTO_THREAD_lock_free(ssl->lock);
740 CRYPTO_FREE_REF(&ssl->references);
748 ssl->defltmeth = ssl->method = method;
753 SSL *ossl_ssl_connection_new_int(SSL_CTX *ctx, const SSL_METHOD *method)
758 s = OPENSSL_zalloc(sizeof(*s));
763 if (!ossl_ssl_init(ssl, ctx, method, SSL_TYPE_SSL_CONNECTION)) {
770 RECORD_LAYER_init(&s->rlayer, s);
772 s->options = ctx->options;
774 s->dane.flags = ctx->dane.flags;
775 if (method->version == ctx->method->version) {
776 s->min_proto_version = ctx->min_proto_version;
777 s->max_proto_version = ctx->max_proto_version;
781 s->max_cert_list = ctx->max_cert_list;
782 s->max_early_data = ctx->max_early_data;
783 s->recv_max_early_data = ctx->recv_max_early_data;
785 s->num_tickets = ctx->num_tickets;
786 s->pha_enabled = ctx->pha_enabled;
788 /* Shallow copy of the ciphersuites stack */
789 s->tls13_ciphersuites = sk_SSL_CIPHER_dup(ctx->tls13_ciphersuites);
790 if (s->tls13_ciphersuites == NULL)
794 * Earlier library versions used to copy the pointer to the CERT, not
795 * its contents; only when setting new parameters for the per-SSL
796 * copy, ssl_cert_new would be called (and the direct reference to
797 * the per-SSL_CTX settings would be lost, but those still were
798 * indirectly accessed for various purposes, and for that reason they
799 * used to be known as s->ctx->default_cert). Now we don't look at the
800 * SSL_CTX's CERT after having duplicated it once.
802 s->cert = ssl_cert_dup(ctx->cert);
806 RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead);
807 s->msg_callback = ctx->msg_callback;
808 s->msg_callback_arg = ctx->msg_callback_arg;
809 s->verify_mode = ctx->verify_mode;
810 s->not_resumable_session_cb = ctx->not_resumable_session_cb;
811 s->rlayer.record_padding_cb = ctx->record_padding_cb;
812 s->rlayer.record_padding_arg = ctx->record_padding_arg;
813 s->rlayer.block_padding = ctx->block_padding;
814 s->sid_ctx_length = ctx->sid_ctx_length;
815 if (!ossl_assert(s->sid_ctx_length <= sizeof(s->sid_ctx)))
817 memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
818 s->verify_callback = ctx->default_verify_callback;
819 s->generate_session_id = ctx->generate_session_id;
821 s->param = X509_VERIFY_PARAM_new();
822 if (s->param == NULL)
824 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
825 s->quiet_shutdown = IS_QUIC_CTX(ctx) ? 0 : ctx->quiet_shutdown;
827 if (!IS_QUIC_CTX(ctx))
828 s->ext.max_fragment_len_mode = ctx->ext.max_fragment_len_mode;
830 s->max_send_fragment = ctx->max_send_fragment;
831 s->split_send_fragment = ctx->split_send_fragment;
832 s->max_pipelines = ctx->max_pipelines;
833 s->rlayer.default_read_buf_len = ctx->default_read_buf_len;
836 s->ext.debug_arg = NULL;
837 s->ext.ticket_expected = 0;
838 s->ext.status_type = ctx->ext.status_type;
839 s->ext.status_expected = 0;
840 s->ext.ocsp.ids = NULL;
841 s->ext.ocsp.exts = NULL;
842 s->ext.ocsp.resp = NULL;
843 s->ext.ocsp.resp_len = 0;
845 s->session_ctx = ctx;
846 if (ctx->ext.ecpointformats) {
847 s->ext.ecpointformats =
848 OPENSSL_memdup(ctx->ext.ecpointformats,
849 ctx->ext.ecpointformats_len);
850 if (!s->ext.ecpointformats) {
851 s->ext.ecpointformats_len = 0;
854 s->ext.ecpointformats_len =
855 ctx->ext.ecpointformats_len;
857 if (ctx->ext.supportedgroups) {
858 s->ext.supportedgroups =
859 OPENSSL_memdup(ctx->ext.supportedgroups,
860 ctx->ext.supportedgroups_len
861 * sizeof(*ctx->ext.supportedgroups));
862 if (!s->ext.supportedgroups) {
863 s->ext.supportedgroups_len = 0;
866 s->ext.supportedgroups_len = ctx->ext.supportedgroups_len;
869 #ifndef OPENSSL_NO_NEXTPROTONEG
873 if (ctx->ext.alpn != NULL) {
874 s->ext.alpn = OPENSSL_malloc(ctx->ext.alpn_len);
875 if (s->ext.alpn == NULL) {
879 memcpy(s->ext.alpn, ctx->ext.alpn, ctx->ext.alpn_len);
880 s->ext.alpn_len = ctx->ext.alpn_len;
883 s->verified_chain = NULL;
884 s->verify_result = X509_V_OK;
886 s->default_passwd_callback = ctx->default_passwd_callback;
887 s->default_passwd_callback_userdata = ctx->default_passwd_callback_userdata;
889 s->key_update = SSL_KEY_UPDATE_NONE;
891 if (!IS_QUIC_CTX(ctx)) {
892 s->allow_early_data_cb = ctx->allow_early_data_cb;
893 s->allow_early_data_cb_data = ctx->allow_early_data_cb_data;
896 if (!method->ssl_init(ssl))
899 s->server = (method->ssl_accept == ssl_undefined_function) ? 0 : 1;
901 if (!method->ssl_reset(ssl))
904 #ifndef OPENSSL_NO_PSK
905 s->psk_client_callback = ctx->psk_client_callback;
906 s->psk_server_callback = ctx->psk_server_callback;
908 s->psk_find_session_cb = ctx->psk_find_session_cb;
909 s->psk_use_session_cb = ctx->psk_use_session_cb;
911 s->async_cb = ctx->async_cb;
912 s->async_cb_arg = ctx->async_cb_arg;
916 #ifndef OPENSSL_NO_COMP_ALG
917 memcpy(s->cert_comp_prefs, ctx->cert_comp_prefs, sizeof(s->cert_comp_prefs));
919 if (ctx->client_cert_type != NULL) {
920 s->client_cert_type = OPENSSL_memdup(ctx->client_cert_type,
921 ctx->client_cert_type_len);
922 if (s->client_cert_type == NULL)
924 s->client_cert_type_len = ctx->client_cert_type_len;
926 if (ctx->server_cert_type != NULL) {
927 s->server_cert_type = OPENSSL_memdup(ctx->server_cert_type,
928 ctx->server_cert_type_len);
929 if (s->server_cert_type == NULL)
931 s->server_cert_type_len = ctx->server_cert_type_len;
934 #ifndef OPENSSL_NO_CT
935 if (!SSL_set_ct_validation_callback(ssl, ctx->ct_validation_callback,
936 ctx->ct_validation_callback_arg))
940 s->ssl_pkey_num = SSL_PKEY_NUM + ctx->sigalg_list_len;
943 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
946 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
949 ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
955 SSL *ossl_ssl_connection_new(SSL_CTX *ctx)
957 return ossl_ssl_connection_new_int(ctx, ctx->method);
960 int SSL_is_dtls(const SSL *s)
962 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
964 #ifndef OPENSSL_NO_QUIC
965 if (s->type == SSL_TYPE_QUIC_CONNECTION || s->type == SSL_TYPE_QUIC_XSO)
972 return SSL_CONNECTION_IS_DTLS(sc) ? 1 : 0;
975 int SSL_is_tls(const SSL *s)
977 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
979 #ifndef OPENSSL_NO_QUIC
980 if (s->type == SSL_TYPE_QUIC_CONNECTION || s->type == SSL_TYPE_QUIC_XSO)
987 return SSL_CONNECTION_IS_DTLS(sc) ? 0 : 1;
990 int SSL_is_quic(const SSL *s)
992 #ifndef OPENSSL_NO_QUIC
993 if (s->type == SSL_TYPE_QUIC_CONNECTION || s->type == SSL_TYPE_QUIC_XSO)
999 int SSL_up_ref(SSL *s)
1003 if (CRYPTO_UP_REF(&s->references, &i) <= 0)
1006 REF_PRINT_COUNT("SSL", s);
1007 REF_ASSERT_ISNT(i < 2);
1008 return ((i > 1) ? 1 : 0);
1011 int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const unsigned char *sid_ctx,
1012 unsigned int sid_ctx_len)
1014 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
1015 ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
1018 ctx->sid_ctx_length = sid_ctx_len;
1019 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
1024 int SSL_set_session_id_context(SSL *ssl, const unsigned char *sid_ctx,
1025 unsigned int sid_ctx_len)
1027 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
1032 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
1033 ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
1036 sc->sid_ctx_length = sid_ctx_len;
1037 memcpy(sc->sid_ctx, sid_ctx, sid_ctx_len);
1042 int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
1044 if (!CRYPTO_THREAD_write_lock(ctx->lock))
1046 ctx->generate_session_id = cb;
1047 CRYPTO_THREAD_unlock(ctx->lock);
1051 int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
1053 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
1055 if (sc == NULL || !CRYPTO_THREAD_write_lock(ssl->lock))
1057 sc->generate_session_id = cb;
1058 CRYPTO_THREAD_unlock(ssl->lock);
1062 int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
1063 unsigned int id_len)
1066 * A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
1067 * we can "construct" a session to give us the desired check - i.e. to
1068 * find if there's a session in the hash table that would conflict with
1069 * any new session built out of this id/id_len and the ssl_version in use
1073 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
1075 if (sc == NULL || id_len > sizeof(r.session_id))
1078 r.ssl_version = sc->version;
1079 r.session_id_length = id_len;
1080 memcpy(r.session_id, id, id_len);
1082 if (!CRYPTO_THREAD_read_lock(sc->session_ctx->lock))
1084 p = lh_SSL_SESSION_retrieve(sc->session_ctx->sessions, &r);
1085 CRYPTO_THREAD_unlock(sc->session_ctx->lock);
1089 int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
1091 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
1094 int SSL_set_purpose(SSL *s, int purpose)
1096 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1101 return X509_VERIFY_PARAM_set_purpose(sc->param, purpose);
1104 int SSL_CTX_set_trust(SSL_CTX *s, int trust)
1106 return X509_VERIFY_PARAM_set_trust(s->param, trust);
1109 int SSL_set_trust(SSL *s, int trust)
1111 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1116 return X509_VERIFY_PARAM_set_trust(sc->param, trust);
1119 int SSL_set1_host(SSL *s, const char *hostname)
1121 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1126 /* If a hostname is provided and parses as an IP address,
1127 * treat it as such. */
1128 if (hostname != NULL
1129 && X509_VERIFY_PARAM_set1_ip_asc(sc->param, hostname) == 1)
1132 return X509_VERIFY_PARAM_set1_host(sc->param, hostname, 0);
1135 int SSL_add1_host(SSL *s, const char *hostname)
1137 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1142 /* If a hostname is provided and parses as an IP address,
1143 * treat it as such. */
1146 ASN1_OCTET_STRING *ip;
1149 ip = a2i_IPADDRESS(hostname);
1151 /* We didn't want it; only to check if it *is* an IP address */
1152 ASN1_OCTET_STRING_free(ip);
1154 old_ip = X509_VERIFY_PARAM_get1_ip_asc(sc->param);
1157 OPENSSL_free(old_ip);
1158 /* There can be only one IP address */
1162 return X509_VERIFY_PARAM_set1_ip_asc(sc->param, hostname);
1166 return X509_VERIFY_PARAM_add1_host(sc->param, hostname, 0);
1169 void SSL_set_hostflags(SSL *s, unsigned int flags)
1171 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1176 X509_VERIFY_PARAM_set_hostflags(sc->param, flags);
1179 const char *SSL_get0_peername(SSL *s)
1181 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1186 return X509_VERIFY_PARAM_get0_peername(sc->param);
1189 int SSL_CTX_dane_enable(SSL_CTX *ctx)
1191 return dane_ctx_enable(&ctx->dane);
1194 unsigned long SSL_CTX_dane_set_flags(SSL_CTX *ctx, unsigned long flags)
1196 unsigned long orig = ctx->dane.flags;
1198 ctx->dane.flags |= flags;
1202 unsigned long SSL_CTX_dane_clear_flags(SSL_CTX *ctx, unsigned long flags)
1204 unsigned long orig = ctx->dane.flags;
1206 ctx->dane.flags &= ~flags;
1210 int SSL_dane_enable(SSL *s, const char *basedomain)
1213 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1219 if (s->ctx->dane.mdmax == 0) {
1220 ERR_raise(ERR_LIB_SSL, SSL_R_CONTEXT_NOT_DANE_ENABLED);
1223 if (dane->trecs != NULL) {
1224 ERR_raise(ERR_LIB_SSL, SSL_R_DANE_ALREADY_ENABLED);
1229 * Default SNI name. This rejects empty names, while set1_host below
1230 * accepts them and disables hostname checks. To avoid side-effects with
1231 * invalid input, set the SNI name first.
1233 if (sc->ext.hostname == NULL) {
1234 if (!SSL_set_tlsext_host_name(s, basedomain)) {
1235 ERR_raise(ERR_LIB_SSL, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN);
1240 /* Primary RFC6125 reference identifier */
1241 if (!X509_VERIFY_PARAM_set1_host(sc->param, basedomain, 0)) {
1242 ERR_raise(ERR_LIB_SSL, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN);
1248 dane->dctx = &s->ctx->dane;
1249 dane->trecs = sk_danetls_record_new_null();
1251 if (dane->trecs == NULL) {
1252 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
1258 unsigned long SSL_dane_set_flags(SSL *ssl, unsigned long flags)
1261 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
1266 orig = sc->dane.flags;
1268 sc->dane.flags |= flags;
1272 unsigned long SSL_dane_clear_flags(SSL *ssl, unsigned long flags)
1275 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
1280 orig = sc->dane.flags;
1282 sc->dane.flags &= ~flags;
1286 int SSL_get0_dane_authority(SSL *s, X509 **mcert, EVP_PKEY **mspki)
1289 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1296 if (!DANETLS_ENABLED(dane) || sc->verify_result != X509_V_OK)
1300 *mcert = dane->mcert;
1302 *mspki = (dane->mcert == NULL) ? dane->mtlsa->spki : NULL;
1307 int SSL_get0_dane_tlsa(SSL *s, uint8_t *usage, uint8_t *selector,
1308 uint8_t *mtype, const unsigned char **data, size_t *dlen)
1311 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1318 if (!DANETLS_ENABLED(dane) || sc->verify_result != X509_V_OK)
1322 *usage = dane->mtlsa->usage;
1324 *selector = dane->mtlsa->selector;
1326 *mtype = dane->mtlsa->mtype;
1328 *data = dane->mtlsa->data;
1330 *dlen = dane->mtlsa->dlen;
1335 SSL_DANE *SSL_get0_dane(SSL *s)
1337 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1345 int SSL_dane_tlsa_add(SSL *s, uint8_t usage, uint8_t selector,
1346 uint8_t mtype, const unsigned char *data, size_t dlen)
1348 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1353 return dane_tlsa_add(&sc->dane, usage, selector, mtype, data, dlen);
1356 int SSL_CTX_dane_mtype_set(SSL_CTX *ctx, const EVP_MD *md, uint8_t mtype,
1359 return dane_mtype_set(&ctx->dane, md, mtype, ord);
1362 int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
1364 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
1367 int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
1369 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
1374 return X509_VERIFY_PARAM_set1(sc->param, vpm);
1377 X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx)
1382 X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl)
1384 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
1392 void SSL_certs_clear(SSL *s)
1394 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1399 ssl_cert_clear_certs(sc->cert);
1402 void SSL_free(SSL *s)
1408 CRYPTO_DOWN_REF(&s->references, &i);
1409 REF_PRINT_COUNT("SSL", s);
1412 REF_ASSERT_ISNT(i < 0);
1414 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
1416 if (s->method != NULL)
1417 s->method->ssl_free(s);
1419 SSL_CTX_free(s->ctx);
1420 CRYPTO_THREAD_lock_free(s->lock);
1421 CRYPTO_FREE_REF(&s->references);
1426 void ossl_ssl_connection_free(SSL *ssl)
1430 s = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
1434 X509_VERIFY_PARAM_free(s->param);
1435 dane_final(&s->dane);
1437 /* Ignore return value */
1438 ssl_free_wbio_buffer(s);
1440 RECORD_LAYER_clear(&s->rlayer);
1442 BUF_MEM_free(s->init_buf);
1444 /* add extra stuff */
1445 sk_SSL_CIPHER_free(s->cipher_list);
1446 sk_SSL_CIPHER_free(s->cipher_list_by_id);
1447 sk_SSL_CIPHER_free(s->tls13_ciphersuites);
1448 sk_SSL_CIPHER_free(s->peer_ciphers);
1450 /* Make the next call work :-) */
1451 if (s->session != NULL) {
1452 ssl_clear_bad_session(s);
1453 SSL_SESSION_free(s->session);
1455 SSL_SESSION_free(s->psksession);
1456 OPENSSL_free(s->psksession_id);
1458 ssl_cert_free(s->cert);
1459 OPENSSL_free(s->shared_sigalgs);
1460 /* Free up if allocated */
1462 OPENSSL_free(s->ext.hostname);
1463 SSL_CTX_free(s->session_ctx);
1464 OPENSSL_free(s->ext.ecpointformats);
1465 OPENSSL_free(s->ext.peer_ecpointformats);
1466 OPENSSL_free(s->ext.supportedgroups);
1467 OPENSSL_free(s->ext.peer_supportedgroups);
1468 sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts, X509_EXTENSION_free);
1469 #ifndef OPENSSL_NO_OCSP
1470 sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);
1472 #ifndef OPENSSL_NO_CT
1473 SCT_LIST_free(s->scts);
1474 OPENSSL_free(s->ext.scts);
1476 OPENSSL_free(s->ext.ocsp.resp);
1477 OPENSSL_free(s->ext.alpn);
1478 OPENSSL_free(s->ext.tls13_cookie);
1479 if (s->clienthello != NULL)
1480 OPENSSL_free(s->clienthello->pre_proc_exts);
1481 OPENSSL_free(s->clienthello);
1482 OPENSSL_free(s->pha_context);
1483 EVP_MD_CTX_free(s->pha_dgst);
1485 sk_X509_NAME_pop_free(s->ca_names, X509_NAME_free);
1486 sk_X509_NAME_pop_free(s->client_ca_names, X509_NAME_free);
1488 OPENSSL_free(s->client_cert_type);
1489 OPENSSL_free(s->server_cert_type);
1491 OSSL_STACK_OF_X509_free(s->verified_chain);
1493 if (ssl->method != NULL)
1494 ssl->method->ssl_deinit(ssl);
1496 ASYNC_WAIT_CTX_free(s->waitctx);
1498 #if !defined(OPENSSL_NO_NEXTPROTONEG)
1499 OPENSSL_free(s->ext.npn);
1502 #ifndef OPENSSL_NO_SRTP
1503 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
1507 * We do this late. We want to ensure that any other references we held to
1508 * these BIOs are freed first *before* we call BIO_free_all(), because
1509 * BIO_free_all() will only free each BIO in the chain if the number of
1510 * references to the first BIO have dropped to 0
1512 BIO_free_all(s->wbio);
1514 BIO_free_all(s->rbio);
1516 OPENSSL_free(s->s3.tmp.valid_flags);
1519 void SSL_set0_rbio(SSL *s, BIO *rbio)
1521 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1523 #ifndef OPENSSL_NO_QUIC
1525 ossl_quic_conn_set0_net_rbio(s, rbio);
1533 BIO_free_all(sc->rbio);
1535 sc->rlayer.rrlmethod->set1_bio(sc->rlayer.rrl, sc->rbio);
1538 void SSL_set0_wbio(SSL *s, BIO *wbio)
1540 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1542 #ifndef OPENSSL_NO_QUIC
1544 ossl_quic_conn_set0_net_wbio(s, wbio);
1553 * If the output buffering BIO is still in place, remove it
1555 if (sc->bbio != NULL)
1556 sc->wbio = BIO_pop(sc->wbio);
1558 BIO_free_all(sc->wbio);
1561 /* Re-attach |bbio| to the new |wbio|. */
1562 if (sc->bbio != NULL)
1563 sc->wbio = BIO_push(sc->bbio, sc->wbio);
1565 sc->rlayer.wrlmethod->set1_bio(sc->rlayer.wrl, sc->wbio);
1568 void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio)
1571 * For historical reasons, this function has many different cases in
1572 * ownership handling.
1575 /* If nothing has changed, do nothing */
1576 if (rbio == SSL_get_rbio(s) && wbio == SSL_get_wbio(s))
1580 * If the two arguments are equal then one fewer reference is granted by the
1581 * caller than we want to take
1583 if (rbio != NULL && rbio == wbio)
1587 * If only the wbio is changed only adopt one reference.
1589 if (rbio == SSL_get_rbio(s)) {
1590 SSL_set0_wbio(s, wbio);
1594 * There is an asymmetry here for historical reasons. If only the rbio is
1595 * changed AND the rbio and wbio were originally different, then we only
1596 * adopt one reference.
1598 if (wbio == SSL_get_wbio(s) && SSL_get_rbio(s) != SSL_get_wbio(s)) {
1599 SSL_set0_rbio(s, rbio);
1603 /* Otherwise, adopt both references. */
1604 SSL_set0_rbio(s, rbio);
1605 SSL_set0_wbio(s, wbio);
1608 BIO *SSL_get_rbio(const SSL *s)
1610 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
1612 #ifndef OPENSSL_NO_QUIC
1614 return ossl_quic_conn_get_net_rbio(s);
1623 BIO *SSL_get_wbio(const SSL *s)
1625 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
1627 #ifndef OPENSSL_NO_QUIC
1629 return ossl_quic_conn_get_net_wbio(s);
1635 if (sc->bbio != NULL) {
1637 * If |bbio| is active, the true caller-configured BIO is its
1640 return BIO_next(sc->bbio);
1645 int SSL_get_fd(const SSL *s)
1647 return SSL_get_rfd(s);
1650 int SSL_get_rfd(const SSL *s)
1655 b = SSL_get_rbio(s);
1656 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
1658 BIO_get_fd(r, &ret);
1662 int SSL_get_wfd(const SSL *s)
1667 b = SSL_get_wbio(s);
1668 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
1670 BIO_get_fd(r, &ret);
1674 #ifndef OPENSSL_NO_SOCK
1675 static const BIO_METHOD *fd_method(SSL *s)
1677 #ifndef OPENSSL_NO_DGRAM
1679 return BIO_s_datagram();
1682 return BIO_s_socket();
1685 int SSL_set_fd(SSL *s, int fd)
1690 if (s->type == SSL_TYPE_QUIC_XSO) {
1691 ERR_raise(ERR_LIB_SSL, SSL_R_CONN_USE_ONLY);
1695 bio = BIO_new(fd_method(s));
1698 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
1701 BIO_set_fd(bio, fd, BIO_NOCLOSE);
1702 SSL_set_bio(s, bio, bio);
1703 #ifndef OPENSSL_NO_KTLS
1705 * The new socket is created successfully regardless of ktls_enable.
1706 * ktls_enable doesn't change any functionality of the socket, except
1707 * changing the setsockopt to enable the processing of ktls_start.
1708 * Thus, it is not a problem to call it for non-TLS sockets.
1711 #endif /* OPENSSL_NO_KTLS */
1717 int SSL_set_wfd(SSL *s, int fd)
1719 BIO *rbio = SSL_get_rbio(s);
1720 int desired_type = IS_QUIC(s) ? BIO_TYPE_DGRAM : BIO_TYPE_SOCKET;
1722 if (s->type == SSL_TYPE_QUIC_XSO) {
1723 ERR_raise(ERR_LIB_SSL, SSL_R_CONN_USE_ONLY);
1727 if (rbio == NULL || BIO_method_type(rbio) != desired_type
1728 || (int)BIO_get_fd(rbio, NULL) != fd) {
1729 BIO *bio = BIO_new(fd_method(s));
1732 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
1735 BIO_set_fd(bio, fd, BIO_NOCLOSE);
1736 SSL_set0_wbio(s, bio);
1737 #ifndef OPENSSL_NO_KTLS
1739 * The new socket is created successfully regardless of ktls_enable.
1740 * ktls_enable doesn't change any functionality of the socket, except
1741 * changing the setsockopt to enable the processing of ktls_start.
1742 * Thus, it is not a problem to call it for non-TLS sockets.
1745 #endif /* OPENSSL_NO_KTLS */
1748 SSL_set0_wbio(s, rbio);
1753 int SSL_set_rfd(SSL *s, int fd)
1755 BIO *wbio = SSL_get_wbio(s);
1756 int desired_type = IS_QUIC(s) ? BIO_TYPE_DGRAM : BIO_TYPE_SOCKET;
1758 if (s->type == SSL_TYPE_QUIC_XSO) {
1759 ERR_raise(ERR_LIB_SSL, SSL_R_CONN_USE_ONLY);
1763 if (wbio == NULL || BIO_method_type(wbio) != desired_type
1764 || ((int)BIO_get_fd(wbio, NULL) != fd)) {
1765 BIO *bio = BIO_new(fd_method(s));
1768 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
1771 BIO_set_fd(bio, fd, BIO_NOCLOSE);
1772 SSL_set0_rbio(s, bio);
1775 SSL_set0_rbio(s, wbio);
1782 /* return length of latest Finished message we sent, copy to 'buf' */
1783 size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
1786 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
1791 ret = sc->s3.tmp.finish_md_len;
1794 memcpy(buf, sc->s3.tmp.finish_md, count);
1798 /* return length of latest Finished message we expected, copy to 'buf' */
1799 size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
1802 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
1807 ret = sc->s3.tmp.peer_finish_md_len;
1810 memcpy(buf, sc->s3.tmp.peer_finish_md, count);
1814 int SSL_get_verify_mode(const SSL *s)
1816 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
1821 return sc->verify_mode;
1824 int SSL_get_verify_depth(const SSL *s)
1826 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
1831 return X509_VERIFY_PARAM_get_depth(sc->param);
1834 int (*SSL_get_verify_callback(const SSL *s)) (int, X509_STORE_CTX *) {
1835 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
1840 return sc->verify_callback;
1843 int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
1845 return ctx->verify_mode;
1848 int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
1850 return X509_VERIFY_PARAM_get_depth(ctx->param);
1853 int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx)) (int, X509_STORE_CTX *) {
1854 return ctx->default_verify_callback;
1857 void SSL_set_verify(SSL *s, int mode,
1858 int (*callback) (int ok, X509_STORE_CTX *ctx))
1860 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1865 sc->verify_mode = mode;
1866 if (callback != NULL)
1867 sc->verify_callback = callback;
1870 void SSL_set_verify_depth(SSL *s, int depth)
1872 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1877 X509_VERIFY_PARAM_set_depth(sc->param, depth);
1880 void SSL_set_read_ahead(SSL *s, int yes)
1882 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
1883 OSSL_PARAM options[2], *opts = options;
1888 RECORD_LAYER_set_read_ahead(&sc->rlayer, yes);
1890 *opts++ = OSSL_PARAM_construct_int(OSSL_LIBSSL_RECORD_LAYER_PARAM_READ_AHEAD,
1891 &sc->rlayer.read_ahead);
1892 *opts = OSSL_PARAM_construct_end();
1894 /* Ignore return value */
1895 sc->rlayer.rrlmethod->set_options(sc->rlayer.rrl, options);
1898 int SSL_get_read_ahead(const SSL *s)
1900 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s);
1905 return RECORD_LAYER_get_read_ahead(&sc->rlayer);
1908 int SSL_pending(const SSL *s)
1910 size_t pending = s->method->ssl_pending(s);
1913 * SSL_pending cannot work properly if read-ahead is enabled
1914 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
1915 * impossible to fix since SSL_pending cannot report errors that may be
1916 * observed while scanning the new data. (Note that SSL_pending() is
1917 * often used as a boolean value, so we'd better not return -1.)
1919 * SSL_pending also cannot work properly if the value >INT_MAX. In that case
1920 * we just return INT_MAX.
1922 return pending < INT_MAX ? (int)pending : INT_MAX;
1925 int SSL_has_pending(const SSL *s)
1928 * Similar to SSL_pending() but returns a 1 to indicate that we have
1929 * processed or unprocessed data available or 0 otherwise (as opposed to the
1930 * number of bytes available). Unlike SSL_pending() this will take into
1931 * account read_ahead data. A 1 return simply indicates that we have data.
1932 * That data may not result in any application data, or we may fail to parse
1933 * the records for some reason.
1935 const SSL_CONNECTION *sc;
1937 #ifndef OPENSSL_NO_QUIC
1939 return ossl_quic_has_pending(s);
1942 sc = SSL_CONNECTION_FROM_CONST_SSL(s);
1944 /* Check buffered app data if any first */
1945 if (SSL_CONNECTION_IS_DTLS(sc)) {
1949 iter = pqueue_iterator(sc->rlayer.d->buffered_app_data.q);
1950 while ((item = pqueue_next(&iter)) != NULL) {
1952 if (rdata->length > 0)
1957 if (RECORD_LAYER_processed_read_pending(&sc->rlayer))
1960 return RECORD_LAYER_read_pending(&sc->rlayer);
1963 X509 *SSL_get1_peer_certificate(const SSL *s)
1965 X509 *r = SSL_get0_peer_certificate(s);
1973 X509 *SSL_get0_peer_certificate(const SSL *s)
1975 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
1980 if (sc->session == NULL)
1983 return sc->session->peer;
1986 STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
1989 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
1994 if (sc->session == NULL)
1997 r = sc->session->peer_chain;
2000 * If we are a client, cert_chain includes the peer's own certificate; if
2001 * we are a server, it does not.
2008 * Now in theory, since the calling process own 't' it should be safe to
2009 * modify. We need to be able to read f without being hassled
2011 int SSL_copy_session_id(SSL *t, const SSL *f)
2014 /* TODO(QUIC FUTURE): Not allowed for QUIC currently. */
2015 SSL_CONNECTION *tsc = SSL_CONNECTION_FROM_SSL_ONLY(t);
2016 const SSL_CONNECTION *fsc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(f);
2018 if (tsc == NULL || fsc == NULL)
2021 /* Do we need to do SSL locking? */
2022 if (!SSL_set_session(t, SSL_get_session(f))) {
2027 * what if we are setup for one protocol version but want to talk another
2029 if (t->method != f->method) {
2030 t->method->ssl_deinit(t);
2031 t->method = f->method;
2032 if (t->method->ssl_init(t) == 0)
2036 CRYPTO_UP_REF(&fsc->cert->references, &i);
2037 ssl_cert_free(tsc->cert);
2038 tsc->cert = fsc->cert;
2039 if (!SSL_set_session_id_context(t, fsc->sid_ctx, (int)fsc->sid_ctx_length)) {
2046 /* Fix this so it checks all the valid key/cert options */
2047 int SSL_CTX_check_private_key(const SSL_CTX *ctx)
2049 if ((ctx == NULL) || (ctx->cert->key->x509 == NULL)) {
2050 ERR_raise(ERR_LIB_SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
2053 if (ctx->cert->key->privatekey == NULL) {
2054 ERR_raise(ERR_LIB_SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
2057 return X509_check_private_key
2058 (ctx->cert->key->x509, ctx->cert->key->privatekey);
2061 /* Fix this function so that it takes an optional type parameter */
2062 int SSL_check_private_key(const SSL *ssl)
2064 const SSL_CONNECTION *sc;
2066 if ((sc = SSL_CONNECTION_FROM_CONST_SSL(ssl)) == NULL) {
2067 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
2070 if (sc->cert->key->x509 == NULL) {
2071 ERR_raise(ERR_LIB_SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
2074 if (sc->cert->key->privatekey == NULL) {
2075 ERR_raise(ERR_LIB_SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
2078 return X509_check_private_key(sc->cert->key->x509,
2079 sc->cert->key->privatekey);
2082 int SSL_waiting_for_async(SSL *s)
2084 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2095 int SSL_get_all_async_fds(SSL *s, OSSL_ASYNC_FD *fds, size_t *numfds)
2097 ASYNC_WAIT_CTX *ctx;
2098 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2103 if ((ctx = sc->waitctx) == NULL)
2105 return ASYNC_WAIT_CTX_get_all_fds(ctx, fds, numfds);
2108 int SSL_get_changed_async_fds(SSL *s, OSSL_ASYNC_FD *addfd, size_t *numaddfds,
2109 OSSL_ASYNC_FD *delfd, size_t *numdelfds)
2111 ASYNC_WAIT_CTX *ctx;
2112 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2117 if ((ctx = sc->waitctx) == NULL)
2119 return ASYNC_WAIT_CTX_get_changed_fds(ctx, addfd, numaddfds, delfd,
2123 int SSL_CTX_set_async_callback(SSL_CTX *ctx, SSL_async_callback_fn callback)
2125 ctx->async_cb = callback;
2129 int SSL_CTX_set_async_callback_arg(SSL_CTX *ctx, void *arg)
2131 ctx->async_cb_arg = arg;
2135 int SSL_set_async_callback(SSL *s, SSL_async_callback_fn callback)
2137 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2142 sc->async_cb = callback;
2146 int SSL_set_async_callback_arg(SSL *s, void *arg)
2148 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2153 sc->async_cb_arg = arg;
2157 int SSL_get_async_status(SSL *s, int *status)
2159 ASYNC_WAIT_CTX *ctx;
2160 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2165 if ((ctx = sc->waitctx) == NULL)
2167 *status = ASYNC_WAIT_CTX_get_status(ctx);
2171 int SSL_accept(SSL *s)
2173 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2175 #ifndef OPENSSL_NO_QUIC
2177 return s->method->ssl_accept(s);
2183 if (sc->handshake_func == NULL) {
2184 /* Not properly initialized yet */
2185 SSL_set_accept_state(s);
2188 return SSL_do_handshake(s);
2191 int SSL_connect(SSL *s)
2193 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2195 #ifndef OPENSSL_NO_QUIC
2197 return s->method->ssl_connect(s);
2203 if (sc->handshake_func == NULL) {
2204 /* Not properly initialized yet */
2205 SSL_set_connect_state(s);
2208 return SSL_do_handshake(s);
2211 long SSL_get_default_timeout(const SSL *s)
2213 return (long int)ossl_time2seconds(s->method->get_timeout());
2216 static int ssl_async_wait_ctx_cb(void *arg)
2218 SSL *s = (SSL *)arg;
2219 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2224 return sc->async_cb(s, sc->async_cb_arg);
2227 static int ssl_start_async_job(SSL *s, struct ssl_async_args *args,
2228 int (*func) (void *))
2231 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2236 if (sc->waitctx == NULL) {
2237 sc->waitctx = ASYNC_WAIT_CTX_new();
2238 if (sc->waitctx == NULL)
2240 if (sc->async_cb != NULL
2241 && !ASYNC_WAIT_CTX_set_callback
2242 (sc->waitctx, ssl_async_wait_ctx_cb, s))
2246 sc->rwstate = SSL_NOTHING;
2247 switch (ASYNC_start_job(&sc->job, sc->waitctx, &ret, func, args,
2248 sizeof(struct ssl_async_args))) {
2250 sc->rwstate = SSL_NOTHING;
2251 ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_INIT_ASYNC);
2254 sc->rwstate = SSL_ASYNC_PAUSED;
2257 sc->rwstate = SSL_ASYNC_NO_JOBS;
2263 sc->rwstate = SSL_NOTHING;
2264 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
2265 /* Shouldn't happen */
2270 static int ssl_io_intern(void *vargs)
2272 struct ssl_async_args *args;
2278 args = (struct ssl_async_args *)vargs;
2282 if ((sc = SSL_CONNECTION_FROM_SSL(s)) == NULL)
2285 switch (args->type) {
2287 return args->f.func_read(s, buf, num, &sc->asyncrw);
2289 return args->f.func_write(s, buf, num, &sc->asyncrw);
2291 return args->f.func_other(s);
2296 int ssl_read_internal(SSL *s, void *buf, size_t num, size_t *readbytes)
2298 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2300 #ifndef OPENSSL_NO_QUIC
2302 return s->method->ssl_read(s, buf, num, readbytes);
2308 if (sc->handshake_func == NULL) {
2309 ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
2313 if (sc->shutdown & SSL_RECEIVED_SHUTDOWN) {
2314 sc->rwstate = SSL_NOTHING;
2318 if (sc->early_data_state == SSL_EARLY_DATA_CONNECT_RETRY
2319 || sc->early_data_state == SSL_EARLY_DATA_ACCEPT_RETRY) {
2320 ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2324 * If we are a client and haven't received the ServerHello etc then we
2327 ossl_statem_check_finish_init(sc, 0);
2329 if ((sc->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
2330 struct ssl_async_args args;
2336 args.type = READFUNC;
2337 args.f.func_read = s->method->ssl_read;
2339 ret = ssl_start_async_job(s, &args, ssl_io_intern);
2340 *readbytes = sc->asyncrw;
2343 return s->method->ssl_read(s, buf, num, readbytes);
2347 int SSL_read(SSL *s, void *buf, int num)
2353 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);
2357 ret = ssl_read_internal(s, buf, (size_t)num, &readbytes);
2360 * The cast is safe here because ret should be <= INT_MAX because num is
2364 ret = (int)readbytes;
2369 int SSL_read_ex(SSL *s, void *buf, size_t num, size_t *readbytes)
2371 int ret = ssl_read_internal(s, buf, num, readbytes);
2378 int SSL_read_early_data(SSL *s, void *buf, size_t num, size_t *readbytes)
2381 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
2383 /* TODO(QUIC 0RTT): 0-RTT support */
2384 if (sc == NULL || !sc->server) {
2385 ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2386 return SSL_READ_EARLY_DATA_ERROR;
2389 switch (sc->early_data_state) {
2390 case SSL_EARLY_DATA_NONE:
2391 if (!SSL_in_before(s)) {
2392 ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2393 return SSL_READ_EARLY_DATA_ERROR;
2397 case SSL_EARLY_DATA_ACCEPT_RETRY:
2398 sc->early_data_state = SSL_EARLY_DATA_ACCEPTING;
2399 ret = SSL_accept(s);
2402 sc->early_data_state = SSL_EARLY_DATA_ACCEPT_RETRY;
2403 return SSL_READ_EARLY_DATA_ERROR;
2407 case SSL_EARLY_DATA_READ_RETRY:
2408 if (sc->ext.early_data == SSL_EARLY_DATA_ACCEPTED) {
2409 sc->early_data_state = SSL_EARLY_DATA_READING;
2410 ret = SSL_read_ex(s, buf, num, readbytes);
2412 * State machine will update early_data_state to
2413 * SSL_EARLY_DATA_FINISHED_READING if we get an EndOfEarlyData
2416 if (ret > 0 || (ret <= 0 && sc->early_data_state
2417 != SSL_EARLY_DATA_FINISHED_READING)) {
2418 sc->early_data_state = SSL_EARLY_DATA_READ_RETRY;
2419 return ret > 0 ? SSL_READ_EARLY_DATA_SUCCESS
2420 : SSL_READ_EARLY_DATA_ERROR;
2423 sc->early_data_state = SSL_EARLY_DATA_FINISHED_READING;
2426 return SSL_READ_EARLY_DATA_FINISH;
2429 ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2430 return SSL_READ_EARLY_DATA_ERROR;
2434 int SSL_get_early_data_status(const SSL *s)
2436 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s);
2438 /* TODO(QUIC 0RTT): 0-RTT support */
2442 return sc->ext.early_data;
2445 static int ssl_peek_internal(SSL *s, void *buf, size_t num, size_t *readbytes)
2447 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2449 #ifndef OPENSSL_NO_QUIC
2451 return s->method->ssl_peek(s, buf, num, readbytes);
2457 if (sc->handshake_func == NULL) {
2458 ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
2462 if (sc->shutdown & SSL_RECEIVED_SHUTDOWN) {
2465 if ((sc->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
2466 struct ssl_async_args args;
2472 args.type = READFUNC;
2473 args.f.func_read = s->method->ssl_peek;
2475 ret = ssl_start_async_job(s, &args, ssl_io_intern);
2476 *readbytes = sc->asyncrw;
2479 return s->method->ssl_peek(s, buf, num, readbytes);
2483 int SSL_peek(SSL *s, void *buf, int num)
2489 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);
2493 ret = ssl_peek_internal(s, buf, (size_t)num, &readbytes);
2496 * The cast is safe here because ret should be <= INT_MAX because num is
2500 ret = (int)readbytes;
2506 int SSL_peek_ex(SSL *s, void *buf, size_t num, size_t *readbytes)
2508 int ret = ssl_peek_internal(s, buf, num, readbytes);
2515 int ssl_write_internal(SSL *s, const void *buf, size_t num, size_t *written)
2517 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2519 #ifndef OPENSSL_NO_QUIC
2521 return s->method->ssl_write(s, buf, num, written);
2527 if (sc->handshake_func == NULL) {
2528 ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
2532 if (sc->shutdown & SSL_SENT_SHUTDOWN) {
2533 sc->rwstate = SSL_NOTHING;
2534 ERR_raise(ERR_LIB_SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
2538 if (sc->early_data_state == SSL_EARLY_DATA_CONNECT_RETRY
2539 || sc->early_data_state == SSL_EARLY_DATA_ACCEPT_RETRY
2540 || sc->early_data_state == SSL_EARLY_DATA_READ_RETRY) {
2541 ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2544 /* If we are a client and haven't sent the Finished we better do that */
2545 ossl_statem_check_finish_init(sc, 1);
2547 if ((sc->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
2549 struct ssl_async_args args;
2552 args.buf = (void *)buf;
2554 args.type = WRITEFUNC;
2555 args.f.func_write = s->method->ssl_write;
2557 ret = ssl_start_async_job(s, &args, ssl_io_intern);
2558 *written = sc->asyncrw;
2561 return s->method->ssl_write(s, buf, num, written);
2565 ossl_ssize_t SSL_sendfile(SSL *s, int fd, off_t offset, size_t size, int flags)
2568 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
2573 if (sc->handshake_func == NULL) {
2574 ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
2578 if (sc->shutdown & SSL_SENT_SHUTDOWN) {
2579 sc->rwstate = SSL_NOTHING;
2580 ERR_raise(ERR_LIB_SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
2584 if (!BIO_get_ktls_send(sc->wbio)) {
2585 ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
2589 /* If we have an alert to send, lets send it */
2590 if (sc->s3.alert_dispatch > 0) {
2591 ret = (ossl_ssize_t)s->method->ssl_dispatch_alert(s);
2593 /* SSLfatal() already called if appropriate */
2596 /* if it went, fall through and send more stuff */
2599 sc->rwstate = SSL_WRITING;
2600 if (BIO_flush(sc->wbio) <= 0) {
2601 if (!BIO_should_retry(sc->wbio)) {
2602 sc->rwstate = SSL_NOTHING;
2605 set_sys_error(EAGAIN);
2611 #ifdef OPENSSL_NO_KTLS
2612 ERR_raise_data(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR,
2613 "can't call ktls_sendfile(), ktls disabled");
2616 ret = ktls_sendfile(SSL_get_wfd(s), fd, offset, size, flags);
2618 #if defined(EAGAIN) && defined(EINTR) && defined(EBUSY)
2619 if ((get_last_sys_error() == EAGAIN) ||
2620 (get_last_sys_error() == EINTR) ||
2621 (get_last_sys_error() == EBUSY))
2622 BIO_set_retry_write(sc->wbio);
2625 ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
2628 sc->rwstate = SSL_NOTHING;
2633 int SSL_write(SSL *s, const void *buf, int num)
2639 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);
2643 ret = ssl_write_internal(s, buf, (size_t)num, &written);
2646 * The cast is safe here because ret should be <= INT_MAX because num is
2655 int SSL_write_ex(SSL *s, const void *buf, size_t num, size_t *written)
2657 int ret = ssl_write_internal(s, buf, num, written);
2664 int SSL_write_early_data(SSL *s, const void *buf, size_t num, size_t *written)
2666 int ret, early_data_state;
2668 uint32_t partialwrite;
2669 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
2671 /* TODO(QUIC 0RTT): This will need special handling for QUIC */
2675 switch (sc->early_data_state) {
2676 case SSL_EARLY_DATA_NONE:
2678 || !SSL_in_before(s)
2679 || ((sc->session == NULL || sc->session->ext.max_early_data == 0)
2680 && (sc->psk_use_session_cb == NULL))) {
2681 ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2686 case SSL_EARLY_DATA_CONNECT_RETRY:
2687 sc->early_data_state = SSL_EARLY_DATA_CONNECTING;
2688 ret = SSL_connect(s);
2691 sc->early_data_state = SSL_EARLY_DATA_CONNECT_RETRY;
2696 case SSL_EARLY_DATA_WRITE_RETRY:
2697 sc->early_data_state = SSL_EARLY_DATA_WRITING;
2699 * We disable partial write for early data because we don't keep track
2700 * of how many bytes we've written between the SSL_write_ex() call and
2701 * the flush if the flush needs to be retried)
2703 partialwrite = sc->mode & SSL_MODE_ENABLE_PARTIAL_WRITE;
2704 sc->mode &= ~SSL_MODE_ENABLE_PARTIAL_WRITE;
2705 ret = SSL_write_ex(s, buf, num, &writtmp);
2706 sc->mode |= partialwrite;
2708 sc->early_data_state = SSL_EARLY_DATA_WRITE_RETRY;
2711 sc->early_data_state = SSL_EARLY_DATA_WRITE_FLUSH;
2714 case SSL_EARLY_DATA_WRITE_FLUSH:
2715 /* The buffering BIO is still in place so we need to flush it */
2716 if (statem_flush(sc) != 1)
2719 sc->early_data_state = SSL_EARLY_DATA_WRITE_RETRY;
2722 case SSL_EARLY_DATA_FINISHED_READING:
2723 case SSL_EARLY_DATA_READ_RETRY:
2724 early_data_state = sc->early_data_state;
2725 /* We are a server writing to an unauthenticated client */
2726 sc->early_data_state = SSL_EARLY_DATA_UNAUTH_WRITING;
2727 ret = SSL_write_ex(s, buf, num, written);
2728 /* The buffering BIO is still in place */
2730 (void)BIO_flush(sc->wbio);
2731 sc->early_data_state = early_data_state;
2735 ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2740 int SSL_shutdown(SSL *s)
2743 * Note that this function behaves differently from what one might
2744 * expect. Return values are 0 for no success (yet), 1 for success; but
2745 * calling it once is usually not enough, even if blocking I/O is used
2746 * (see ssl3_shutdown).
2748 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2750 #ifndef OPENSSL_NO_QUIC
2752 return ossl_quic_conn_shutdown(s, 0, NULL, 0);
2758 if (sc->handshake_func == NULL) {
2759 ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
2763 if (!SSL_in_init(s)) {
2764 if ((sc->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
2765 struct ssl_async_args args;
2767 memset(&args, 0, sizeof(args));
2769 args.type = OTHERFUNC;
2770 args.f.func_other = s->method->ssl_shutdown;
2772 return ssl_start_async_job(s, &args, ssl_io_intern);
2774 return s->method->ssl_shutdown(s);
2777 ERR_raise(ERR_LIB_SSL, SSL_R_SHUTDOWN_WHILE_IN_INIT);
2782 int SSL_key_update(SSL *s, int updatetype)
2784 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2786 #ifndef OPENSSL_NO_QUIC
2788 return ossl_quic_key_update(s, updatetype);
2794 if (!SSL_CONNECTION_IS_TLS13(sc)) {
2795 ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION);
2799 if (updatetype != SSL_KEY_UPDATE_NOT_REQUESTED
2800 && updatetype != SSL_KEY_UPDATE_REQUESTED) {
2801 ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_KEY_UPDATE_TYPE);
2805 if (!SSL_is_init_finished(s)) {
2806 ERR_raise(ERR_LIB_SSL, SSL_R_STILL_IN_INIT);
2810 if (RECORD_LAYER_write_pending(&sc->rlayer)) {
2811 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_WRITE_RETRY);
2815 ossl_statem_set_in_init(sc, 1);
2816 sc->key_update = updatetype;
2820 int SSL_get_key_update_type(const SSL *s)
2822 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
2824 #ifndef OPENSSL_NO_QUIC
2826 return ossl_quic_get_key_update_type(s);
2832 return sc->key_update;
2836 * Can we accept a renegotiation request? If yes, set the flag and
2837 * return 1 if yes. If not, raise error and return 0.
2839 static int can_renegotiate(const SSL_CONNECTION *sc)
2841 if (SSL_CONNECTION_IS_TLS13(sc)) {
2842 ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION);
2846 if ((sc->options & SSL_OP_NO_RENEGOTIATION) != 0) {
2847 ERR_raise(ERR_LIB_SSL, SSL_R_NO_RENEGOTIATION);
2854 int SSL_renegotiate(SSL *s)
2856 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
2861 if (!can_renegotiate(sc))
2864 sc->renegotiate = 1;
2865 sc->new_session = 1;
2866 return s->method->ssl_renegotiate(s);
2869 int SSL_renegotiate_abbreviated(SSL *s)
2871 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
2876 if (!can_renegotiate(sc))
2879 sc->renegotiate = 1;
2880 sc->new_session = 0;
2881 return s->method->ssl_renegotiate(s);
2884 int SSL_renegotiate_pending(const SSL *s)
2886 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
2892 * becomes true when negotiation is requested; false again once a
2893 * handshake has finished
2895 return (sc->renegotiate != 0);
2898 int SSL_new_session_ticket(SSL *s)
2900 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2905 /* If we are in init because we're sending tickets, okay to send more. */
2906 if ((SSL_in_init(s) && sc->ext.extra_tickets_expected == 0)
2907 || SSL_IS_FIRST_HANDSHAKE(sc) || !sc->server
2908 || !SSL_CONNECTION_IS_TLS13(sc))
2910 sc->ext.extra_tickets_expected++;
2911 if (!RECORD_LAYER_write_pending(&sc->rlayer) && !SSL_in_init(s))
2912 ossl_statem_set_in_init(sc, 1);
2916 long SSL_ctrl(SSL *s, int cmd, long larg, void *parg)
2918 return ossl_ctrl_internal(s, cmd, larg, parg, /*no_quic=*/0);
2921 long ossl_ctrl_internal(SSL *s, int cmd, long larg, void *parg, int no_quic)
2924 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2930 * Routing of ctrl calls for QUIC is a little counterintuitive:
2932 * - Firstly (no_quic=0), we pass the ctrl directly to our QUIC
2933 * implementation in case it wants to handle the ctrl specially.
2935 * - If our QUIC implementation does not care about the ctrl, it
2936 * will reenter this function with no_quic=1 and we will try to handle
2937 * it directly using the QCSO SSL object stub (not the handshake layer
2938 * SSL object). This is important for e.g. the version configuration
2939 * ctrls below, which must use s->defltmeth (and not sc->defltmeth).
2941 * - If we don't handle a ctrl here specially, then processing is
2942 * redirected to the handshake layer SSL object.
2944 if (!no_quic && IS_QUIC(s))
2945 return s->method->ssl_ctrl(s, cmd, larg, parg);
2948 case SSL_CTRL_GET_READ_AHEAD:
2949 return RECORD_LAYER_get_read_ahead(&sc->rlayer);
2950 case SSL_CTRL_SET_READ_AHEAD:
2951 l = RECORD_LAYER_get_read_ahead(&sc->rlayer);
2952 RECORD_LAYER_set_read_ahead(&sc->rlayer, larg);
2957 OSSL_PARAM options[2], *opts = options;
2961 *opts++ = OSSL_PARAM_construct_uint32(OSSL_LIBSSL_RECORD_LAYER_PARAM_MODE,
2963 *opts = OSSL_PARAM_construct_end();
2965 /* Ignore return value */
2966 sc->rlayer.rrlmethod->set_options(sc->rlayer.rrl, options);
2970 case SSL_CTRL_CLEAR_MODE:
2971 return (sc->mode &= ~larg);
2972 case SSL_CTRL_GET_MAX_CERT_LIST:
2973 return (long)sc->max_cert_list;
2974 case SSL_CTRL_SET_MAX_CERT_LIST:
2977 l = (long)sc->max_cert_list;
2978 sc->max_cert_list = (size_t)larg;
2980 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
2981 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
2983 #ifndef OPENSSL_NO_KTLS
2984 if (sc->wbio != NULL && BIO_get_ktls_send(sc->wbio))
2986 #endif /* OPENSSL_NO_KTLS */
2987 sc->max_send_fragment = larg;
2988 if (sc->max_send_fragment < sc->split_send_fragment)
2989 sc->split_send_fragment = sc->max_send_fragment;
2990 sc->rlayer.wrlmethod->set_max_frag_len(sc->rlayer.wrl, larg);
2992 case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT:
2993 if ((size_t)larg > sc->max_send_fragment || larg == 0)
2995 sc->split_send_fragment = larg;
2997 case SSL_CTRL_SET_MAX_PIPELINES:
2998 if (larg < 1 || larg > SSL_MAX_PIPELINES)
3000 sc->max_pipelines = larg;
3001 if (sc->rlayer.rrlmethod->set_max_pipelines != NULL)
3002 sc->rlayer.rrlmethod->set_max_pipelines(sc->rlayer.rrl, (size_t)larg);
3004 case SSL_CTRL_GET_RI_SUPPORT:
3005 return sc->s3.send_connection_binding;
3006 case SSL_CTRL_SET_RETRY_VERIFY:
3007 sc->rwstate = SSL_RETRY_VERIFY;
3009 case SSL_CTRL_CERT_FLAGS:
3010 return (sc->cert->cert_flags |= larg);
3011 case SSL_CTRL_CLEAR_CERT_FLAGS:
3012 return (sc->cert->cert_flags &= ~larg);
3014 case SSL_CTRL_GET_RAW_CIPHERLIST:
3016 if (sc->s3.tmp.ciphers_raw == NULL)
3018 *(unsigned char **)parg = sc->s3.tmp.ciphers_raw;
3019 return (int)sc->s3.tmp.ciphers_rawlen;
3021 return TLS_CIPHER_LEN;
3023 case SSL_CTRL_GET_EXTMS_SUPPORT:
3024 if (!sc->session || SSL_in_init(s) || ossl_statem_get_in_handshake(sc))
3026 if (sc->session->flags & SSL_SESS_FLAG_EXTMS)
3030 case SSL_CTRL_SET_MIN_PROTO_VERSION:
3031 return ssl_check_allowed_versions(larg, sc->max_proto_version)
3032 && ssl_set_version_bound(s->defltmeth->version, (int)larg,
3033 &sc->min_proto_version);
3034 case SSL_CTRL_GET_MIN_PROTO_VERSION:
3035 return sc->min_proto_version;
3036 case SSL_CTRL_SET_MAX_PROTO_VERSION:
3037 return ssl_check_allowed_versions(sc->min_proto_version, larg)
3038 && ssl_set_version_bound(s->defltmeth->version, (int)larg,
3039 &sc->max_proto_version);
3040 case SSL_CTRL_GET_MAX_PROTO_VERSION:
3041 return sc->max_proto_version;
3044 return SSL_ctrl((SSL *)sc, cmd, larg, parg);
3046 return s->method->ssl_ctrl(s, cmd, larg, parg);
3050 long SSL_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
3052 return s->method->ssl_callback_ctrl(s, cmd, fp);
3055 LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
3057 return ctx->sessions;
3060 static int ssl_tsan_load(SSL_CTX *ctx, TSAN_QUALIFIER int *stat)
3064 if (ssl_tsan_lock(ctx)) {
3065 res = tsan_load(stat);
3066 ssl_tsan_unlock(ctx);
3071 long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
3074 /* For some cases with ctx == NULL perform syntax checks */
3077 case SSL_CTRL_SET_GROUPS_LIST:
3078 return tls1_set_groups_list(ctx, NULL, NULL, parg);
3079 case SSL_CTRL_SET_SIGALGS_LIST:
3080 case SSL_CTRL_SET_CLIENT_SIGALGS_LIST:
3081 return tls1_set_sigalgs_list(NULL, parg, 0);
3088 case SSL_CTRL_GET_READ_AHEAD:
3089 return ctx->read_ahead;
3090 case SSL_CTRL_SET_READ_AHEAD:
3091 l = ctx->read_ahead;
3092 ctx->read_ahead = larg;
3095 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
3096 ctx->msg_callback_arg = parg;
3099 case SSL_CTRL_GET_MAX_CERT_LIST:
3100 return (long)ctx->max_cert_list;
3101 case SSL_CTRL_SET_MAX_CERT_LIST:
3104 l = (long)ctx->max_cert_list;
3105 ctx->max_cert_list = (size_t)larg;
3108 case SSL_CTRL_SET_SESS_CACHE_SIZE:
3111 l = (long)ctx->session_cache_size;
3112 ctx->session_cache_size = (size_t)larg;
3114 case SSL_CTRL_GET_SESS_CACHE_SIZE:
3115 return (long)ctx->session_cache_size;
3116 case SSL_CTRL_SET_SESS_CACHE_MODE:
3117 l = ctx->session_cache_mode;
3118 ctx->session_cache_mode = larg;
3120 case SSL_CTRL_GET_SESS_CACHE_MODE:
3121 return ctx->session_cache_mode;
3123 case SSL_CTRL_SESS_NUMBER:
3124 return lh_SSL_SESSION_num_items(ctx->sessions);
3125 case SSL_CTRL_SESS_CONNECT:
3126 return ssl_tsan_load(ctx, &ctx->stats.sess_connect);
3127 case SSL_CTRL_SESS_CONNECT_GOOD:
3128 return ssl_tsan_load(ctx, &ctx->stats.sess_connect_good);
3129 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
3130 return ssl_tsan_load(ctx, &ctx->stats.sess_connect_renegotiate);
3131 case SSL_CTRL_SESS_ACCEPT:
3132 return ssl_tsan_load(ctx, &ctx->stats.sess_accept);
3133 case SSL_CTRL_SESS_ACCEPT_GOOD:
3134 return ssl_tsan_load(ctx, &ctx->stats.sess_accept_good);
3135 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
3136 return ssl_tsan_load(ctx, &ctx->stats.sess_accept_renegotiate);
3137 case SSL_CTRL_SESS_HIT:
3138 return ssl_tsan_load(ctx, &ctx->stats.sess_hit);
3139 case SSL_CTRL_SESS_CB_HIT:
3140 return ssl_tsan_load(ctx, &ctx->stats.sess_cb_hit);
3141 case SSL_CTRL_SESS_MISSES:
3142 return ssl_tsan_load(ctx, &ctx->stats.sess_miss);
3143 case SSL_CTRL_SESS_TIMEOUTS:
3144 return ssl_tsan_load(ctx, &ctx->stats.sess_timeout);
3145 case SSL_CTRL_SESS_CACHE_FULL:
3146 return ssl_tsan_load(ctx, &ctx->stats.sess_cache_full);
3148 return (ctx->mode |= larg);
3149 case SSL_CTRL_CLEAR_MODE:
3150 return (ctx->mode &= ~larg);
3151 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
3152 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
3154 ctx->max_send_fragment = larg;
3155 if (ctx->max_send_fragment < ctx->split_send_fragment)
3156 ctx->split_send_fragment = ctx->max_send_fragment;
3158 case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT:
3159 if ((size_t)larg > ctx->max_send_fragment || larg == 0)
3161 ctx->split_send_fragment = larg;
3163 case SSL_CTRL_SET_MAX_PIPELINES:
3164 if (larg < 1 || larg > SSL_MAX_PIPELINES)
3166 ctx->max_pipelines = larg;
3168 case SSL_CTRL_CERT_FLAGS:
3169 return (ctx->cert->cert_flags |= larg);
3170 case SSL_CTRL_CLEAR_CERT_FLAGS:
3171 return (ctx->cert->cert_flags &= ~larg);
3172 case SSL_CTRL_SET_MIN_PROTO_VERSION:
3173 return ssl_check_allowed_versions(larg, ctx->max_proto_version)
3174 && ssl_set_version_bound(ctx->method->version, (int)larg,
3175 &ctx->min_proto_version);
3176 case SSL_CTRL_GET_MIN_PROTO_VERSION:
3177 return ctx->min_proto_version;
3178 case SSL_CTRL_SET_MAX_PROTO_VERSION:
3179 return ssl_check_allowed_versions(ctx->min_proto_version, larg)
3180 && ssl_set_version_bound(ctx->method->version, (int)larg,
3181 &ctx->max_proto_version);
3182 case SSL_CTRL_GET_MAX_PROTO_VERSION:
3183 return ctx->max_proto_version;
3185 return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
3189 long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
3192 case SSL_CTRL_SET_MSG_CALLBACK:
3193 ctx->msg_callback = (void (*)
3194 (int write_p, int version, int content_type,
3195 const void *buf, size_t len, SSL *ssl,
3200 return ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp);
3204 int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b)
3213 int ssl_cipher_ptr_id_cmp(const SSL_CIPHER *const *ap,
3214 const SSL_CIPHER *const *bp)
3216 if ((*ap)->id > (*bp)->id)
3218 if ((*ap)->id < (*bp)->id)
3224 * return a STACK of the ciphers available for the SSL and in order of
3227 STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
3229 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
3232 if (sc->cipher_list != NULL) {
3233 return sc->cipher_list;
3234 } else if ((s->ctx != NULL) && (s->ctx->cipher_list != NULL)) {
3235 return s->ctx->cipher_list;
3241 STACK_OF(SSL_CIPHER) *SSL_get_client_ciphers(const SSL *s)
3243 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
3245 if (sc == NULL || !sc->server)
3247 return sc->peer_ciphers;
3250 STACK_OF(SSL_CIPHER) *SSL_get1_supported_ciphers(SSL *s)
3252 STACK_OF(SSL_CIPHER) *sk = NULL, *ciphers;
3254 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
3259 ciphers = SSL_get_ciphers(s);
3262 if (!ssl_set_client_disabled(sc))
3264 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
3265 const SSL_CIPHER *c = sk_SSL_CIPHER_value(ciphers, i);
3266 if (!ssl_cipher_disabled(sc, c, SSL_SECOP_CIPHER_SUPPORTED, 0)) {
3268 sk = sk_SSL_CIPHER_new_null();
3271 if (!sk_SSL_CIPHER_push(sk, c)) {
3272 sk_SSL_CIPHER_free(sk);
3280 /** return a STACK of the ciphers available for the SSL and in order of
3282 STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL_CONNECTION *s)
3285 if (s->cipher_list_by_id != NULL)
3286 return s->cipher_list_by_id;
3287 else if (s->ssl.ctx != NULL
3288 && s->ssl.ctx->cipher_list_by_id != NULL)
3289 return s->ssl.ctx->cipher_list_by_id;
3294 /** The old interface to get the same thing as SSL_get_ciphers() */
3295 const char *SSL_get_cipher_list(const SSL *s, int n)
3297 const SSL_CIPHER *c;
3298 STACK_OF(SSL_CIPHER) *sk;
3302 sk = SSL_get_ciphers(s);
3303 if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n))
3305 c = sk_SSL_CIPHER_value(sk, n);
3311 /** return a STACK of the ciphers available for the SSL_CTX and in order of
3313 STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx)
3316 return ctx->cipher_list;
3321 * Distinguish between ciphers controlled by set_ciphersuite() and
3322 * set_cipher_list() when counting.
3324 static int cipher_list_tls12_num(STACK_OF(SSL_CIPHER) *sk)
3327 const SSL_CIPHER *c;
3331 for (i = 0; i < sk_SSL_CIPHER_num(sk); ++i) {
3332 c = sk_SSL_CIPHER_value(sk, i);
3333 if (c->min_tls >= TLS1_3_VERSION)
3340 /** specify the ciphers to be used by default by the SSL_CTX */
3341 int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
3343 STACK_OF(SSL_CIPHER) *sk;
3345 sk = ssl_create_cipher_list(ctx, ctx->tls13_ciphersuites,
3346 &ctx->cipher_list, &ctx->cipher_list_by_id, str,
3349 * ssl_create_cipher_list may return an empty stack if it was unable to
3350 * find a cipher matching the given rule string (for example if the rule
3351 * string specifies a cipher which has been disabled). This is not an
3352 * error as far as ssl_create_cipher_list is concerned, and hence
3353 * ctx->cipher_list and ctx->cipher_list_by_id has been updated.
3357 else if (cipher_list_tls12_num(sk) == 0) {
3358 ERR_raise(ERR_LIB_SSL, SSL_R_NO_CIPHER_MATCH);
3364 /** specify the ciphers to be used by the SSL */
3365 int SSL_set_cipher_list(SSL *s, const char *str)
3367 STACK_OF(SSL_CIPHER) *sk;
3368 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
3373 sk = ssl_create_cipher_list(s->ctx, sc->tls13_ciphersuites,
3374 &sc->cipher_list, &sc->cipher_list_by_id, str,
3376 /* see comment in SSL_CTX_set_cipher_list */
3379 else if (cipher_list_tls12_num(sk) == 0) {
3380 ERR_raise(ERR_LIB_SSL, SSL_R_NO_CIPHER_MATCH);
3386 char *SSL_get_shared_ciphers(const SSL *s, char *buf, int size)
3389 STACK_OF(SSL_CIPHER) *clntsk, *srvrsk;
3390 const SSL_CIPHER *c;
3392 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
3398 || sc->peer_ciphers == NULL
3403 clntsk = sc->peer_ciphers;
3404 srvrsk = SSL_get_ciphers(s);
3405 if (clntsk == NULL || srvrsk == NULL)
3408 if (sk_SSL_CIPHER_num(clntsk) == 0 || sk_SSL_CIPHER_num(srvrsk) == 0)
3411 for (i = 0; i < sk_SSL_CIPHER_num(clntsk); i++) {
3414 c = sk_SSL_CIPHER_value(clntsk, i);
3415 if (sk_SSL_CIPHER_find(srvrsk, c) < 0)
3418 n = OPENSSL_strnlen(c->name, size);
3425 memcpy(p, c->name, n);
3435 * Return the requested servername (SNI) value. Note that the behaviour varies
3437 * - whether this is called by the client or the server,
3438 * - if we are before or during/after the handshake,
3439 * - if a resumption or normal handshake is being attempted/has occurred
3440 * - whether we have negotiated TLSv1.2 (or below) or TLSv1.3
3442 * Note that only the host_name type is defined (RFC 3546).
3444 const char *SSL_get_servername(const SSL *s, const int type)
3446 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
3453 * If we don't know if we are the client or the server yet then we assume
3456 server = sc->handshake_func == NULL ? 0 : sc->server;
3458 if (type != TLSEXT_NAMETYPE_host_name)
3464 * In TLSv1.3 on the server SNI is not associated with the session
3465 * but in TLSv1.2 or below it is.
3467 * Before the handshake:
3470 * During/after the handshake (TLSv1.2 or below resumption occurred):
3471 * - If a servername was accepted by the server in the original
3472 * handshake then it will return that servername, or NULL otherwise.
3474 * During/after the handshake (TLSv1.2 or below resumption did not occur):
3475 * - The function will return the servername requested by the client in
3476 * this handshake or NULL if none was requested.
3478 if (sc->hit && !SSL_CONNECTION_IS_TLS13(sc))
3479 return sc->session->ext.hostname;
3484 * Before the handshake:
3485 * - If a servername has been set via a call to
3486 * SSL_set_tlsext_host_name() then it will return that servername
3487 * - If one has not been set, but a TLSv1.2 resumption is being
3488 * attempted and the session from the original handshake had a
3489 * servername accepted by the server then it will return that
3491 * - Otherwise it returns NULL
3493 * During/after the handshake (TLSv1.2 or below resumption occurred):
3494 * - If the session from the original handshake had a servername accepted
3495 * by the server then it will return that servername.
3496 * - Otherwise it returns the servername set via
3497 * SSL_set_tlsext_host_name() (or NULL if it was not called).
3499 * During/after the handshake (TLSv1.2 or below resumption did not occur):
3500 * - It will return the servername set via SSL_set_tlsext_host_name()
3501 * (or NULL if it was not called).
3503 if (SSL_in_before(s)) {
3504 if (sc->ext.hostname == NULL
3505 && sc->session != NULL
3506 && sc->session->ssl_version != TLS1_3_VERSION)
3507 return sc->session->ext.hostname;
3509 if (!SSL_CONNECTION_IS_TLS13(sc) && sc->hit
3510 && sc->session->ext.hostname != NULL)
3511 return sc->session->ext.hostname;
3515 return sc->ext.hostname;
3518 int SSL_get_servername_type(const SSL *s)
3520 if (SSL_get_servername(s, TLSEXT_NAMETYPE_host_name) != NULL)
3521 return TLSEXT_NAMETYPE_host_name;
3526 * SSL_select_next_proto implements the standard protocol selection. It is
3527 * expected that this function is called from the callback set by
3528 * SSL_CTX_set_next_proto_select_cb. The protocol data is assumed to be a
3529 * vector of 8-bit, length prefixed byte strings. The length byte itself is
3530 * not included in the length. A byte string of length 0 is invalid. No byte
3531 * string may be truncated. The current, but experimental algorithm for
3532 * selecting the protocol is: 1) If the server doesn't support NPN then this
3533 * is indicated to the callback. In this case, the client application has to
3534 * abort the connection or have a default application level protocol. 2) If
3535 * the server supports NPN, but advertises an empty list then the client
3536 * selects the first protocol in its list, but indicates via the API that this
3537 * fallback case was enacted. 3) Otherwise, the client finds the first
3538 * protocol in the server's list that it supports and selects this protocol.
3539 * This is because it's assumed that the server has better information about
3540 * which protocol a client should use. 4) If the client doesn't support any
3541 * of the server's advertised protocols, then this is treated the same as
3542 * case 2. It returns either OPENSSL_NPN_NEGOTIATED if a common protocol was
3543 * found, or OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
3545 int SSL_select_next_proto(unsigned char **out, unsigned char *outlen,
3546 const unsigned char *server,
3547 unsigned int server_len,
3548 const unsigned char *client, unsigned int client_len)
3551 const unsigned char *result;
3552 int status = OPENSSL_NPN_UNSUPPORTED;
3555 * For each protocol in server preference order, see if we support it.
3557 for (i = 0; i < server_len;) {
3558 for (j = 0; j < client_len;) {
3559 if (server[i] == client[j] &&
3560 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
3561 /* We found a match */
3562 result = &server[i];
3563 status = OPENSSL_NPN_NEGOTIATED;
3573 /* There's no overlap between our protocols and the server's list. */
3575 status = OPENSSL_NPN_NO_OVERLAP;
3578 *out = (unsigned char *)result + 1;
3579 *outlen = result[0];
3583 #ifndef OPENSSL_NO_NEXTPROTONEG
3585 * SSL_get0_next_proto_negotiated sets *data and *len to point to the
3586 * client's requested protocol for this connection and returns 0. If the
3587 * client didn't request any protocol, then *data is set to NULL. Note that
3588 * the client can request any protocol it chooses. The value returned from
3589 * this function need not be a member of the list of supported protocols
3590 * provided by the callback.
3592 void SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data,
3595 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
3598 /* We have no other way to indicate error */
3604 *data = sc->ext.npn;
3605 if (*data == NULL) {
3608 *len = (unsigned int)sc->ext.npn_len;
3613 * SSL_CTX_set_npn_advertised_cb sets a callback that is called when
3614 * a TLS server needs a list of supported protocols for Next Protocol
3615 * Negotiation. The returned list must be in wire format. The list is
3616 * returned by setting |out| to point to it and |outlen| to its length. This
3617 * memory will not be modified, but one should assume that the SSL* keeps a
3618 * reference to it. The callback should return SSL_TLSEXT_ERR_OK if it
3619 * wishes to advertise. Otherwise, no such extension will be included in the
3622 void SSL_CTX_set_npn_advertised_cb(SSL_CTX *ctx,
3623 SSL_CTX_npn_advertised_cb_func cb,
3626 if (IS_QUIC_CTX(ctx))
3627 /* NPN not allowed for QUIC */
3630 ctx->ext.npn_advertised_cb = cb;
3631 ctx->ext.npn_advertised_cb_arg = arg;
3635 * SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
3636 * client needs to select a protocol from the server's provided list. |out|
3637 * must be set to point to the selected protocol (which may be within |in|).
3638 * The length of the protocol name must be written into |outlen|. The
3639 * server's advertised protocols are provided in |in| and |inlen|. The
3640 * callback can assume that |in| is syntactically valid. The client must
3641 * select a protocol. It is fatal to the connection if this callback returns
3642 * a value other than SSL_TLSEXT_ERR_OK.
3644 void SSL_CTX_set_npn_select_cb(SSL_CTX *ctx,
3645 SSL_CTX_npn_select_cb_func cb,
3648 if (IS_QUIC_CTX(ctx))
3649 /* NPN not allowed for QUIC */
3652 ctx->ext.npn_select_cb = cb;
3653 ctx->ext.npn_select_cb_arg = arg;
3657 static int alpn_value_ok(const unsigned char *protos, unsigned int protos_len)
3661 if (protos_len < 2 || protos == NULL)
3664 for (idx = 0; idx < protos_len; idx += protos[idx] + 1) {
3665 if (protos[idx] == 0)
3668 return idx == protos_len;
3671 * SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
3672 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
3673 * length-prefixed strings). Returns 0 on success.
3675 int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char *protos,
3676 unsigned int protos_len)
3678 unsigned char *alpn;
3680 if (protos_len == 0 || protos == NULL) {
3681 OPENSSL_free(ctx->ext.alpn);
3682 ctx->ext.alpn = NULL;
3683 ctx->ext.alpn_len = 0;
3686 /* Not valid per RFC */
3687 if (!alpn_value_ok(protos, protos_len))
3690 alpn = OPENSSL_memdup(protos, protos_len);
3693 OPENSSL_free(ctx->ext.alpn);
3694 ctx->ext.alpn = alpn;
3695 ctx->ext.alpn_len = protos_len;
3701 * SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
3702 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
3703 * length-prefixed strings). Returns 0 on success.
3705 int SSL_set_alpn_protos(SSL *ssl, const unsigned char *protos,
3706 unsigned int protos_len)
3708 unsigned char *alpn;
3709 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
3714 if (protos_len == 0 || protos == NULL) {
3715 OPENSSL_free(sc->ext.alpn);
3716 sc->ext.alpn = NULL;
3717 sc->ext.alpn_len = 0;
3720 /* Not valid per RFC */
3721 if (!alpn_value_ok(protos, protos_len))
3724 alpn = OPENSSL_memdup(protos, protos_len);
3727 OPENSSL_free(sc->ext.alpn);
3728 sc->ext.alpn = alpn;
3729 sc->ext.alpn_len = protos_len;
3735 * SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is
3736 * called during ClientHello processing in order to select an ALPN protocol
3737 * from the client's list of offered protocols.
3739 void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
3740 SSL_CTX_alpn_select_cb_func cb,
3743 ctx->ext.alpn_select_cb = cb;
3744 ctx->ext.alpn_select_cb_arg = arg;
3748 * SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
3749 * On return it sets |*data| to point to |*len| bytes of protocol name
3750 * (not including the leading length-prefix byte). If the server didn't
3751 * respond with a negotiated protocol then |*len| will be zero.
3753 void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
3756 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
3759 /* We have no other way to indicate error */
3765 *data = sc->s3.alpn_selected;
3769 *len = (unsigned int)sc->s3.alpn_selected_len;
3772 int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
3773 const char *label, size_t llen,
3774 const unsigned char *context, size_t contextlen,
3777 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
3782 if (sc->session == NULL
3783 || (sc->version < TLS1_VERSION && sc->version != DTLS1_BAD_VER))
3786 return s->method->ssl3_enc->export_keying_material(sc, out, olen, label,
3788 contextlen, use_context);
3791 int SSL_export_keying_material_early(SSL *s, unsigned char *out, size_t olen,
3792 const char *label, size_t llen,
3793 const unsigned char *context,
3796 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
3801 if (sc->version != TLS1_3_VERSION)
3804 return tls13_export_keying_material_early(sc, out, olen, label, llen,
3805 context, contextlen);
3808 static unsigned long ssl_session_hash(const SSL_SESSION *a)
3810 const unsigned char *session_id = a->session_id;
3812 unsigned char tmp_storage[4];
3814 if (a->session_id_length < sizeof(tmp_storage)) {
3815 memset(tmp_storage, 0, sizeof(tmp_storage));
3816 memcpy(tmp_storage, a->session_id, a->session_id_length);
3817 session_id = tmp_storage;
3821 ((unsigned long)session_id[0]) |
3822 ((unsigned long)session_id[1] << 8L) |
3823 ((unsigned long)session_id[2] << 16L) |
3824 ((unsigned long)session_id[3] << 24L);
3829 * NB: If this function (or indeed the hash function which uses a sort of
3830 * coarser function than this one) is changed, ensure
3831 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on
3832 * being able to construct an SSL_SESSION that will collide with any existing
3833 * session with a matching session ID.
3835 static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
3837 if (a->ssl_version != b->ssl_version)
3839 if (a->session_id_length != b->session_id_length)
3841 return memcmp(a->session_id, b->session_id, a->session_id_length);
3845 * These wrapper functions should remain rather than redeclaring
3846 * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
3847 * variable. The reason is that the functions aren't static, they're exposed
3851 SSL_CTX *SSL_CTX_new_ex(OSSL_LIB_CTX *libctx, const char *propq,
3852 const SSL_METHOD *meth)
3854 SSL_CTX *ret = NULL;
3855 #ifndef OPENSSL_NO_COMP_ALG
3860 ERR_raise(ERR_LIB_SSL, SSL_R_NULL_SSL_METHOD_PASSED);
3864 if (!OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL))
3867 /* Doing this for the run once effect */
3868 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
3869 ERR_raise(ERR_LIB_SSL, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
3873 ret = OPENSSL_zalloc(sizeof(*ret));
3877 /* Init the reference counting before any call to SSL_CTX_free */
3878 if (!CRYPTO_NEW_REF(&ret->references, 1)) {
3883 ret->lock = CRYPTO_THREAD_lock_new();
3884 if (ret->lock == NULL) {
3885 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
3889 #ifdef TSAN_REQUIRES_LOCKING
3890 ret->tsan_lock = CRYPTO_THREAD_lock_new();
3891 if (ret->tsan_lock == NULL) {
3892 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
3897 ret->libctx = libctx;
3898 if (propq != NULL) {
3899 ret->propq = OPENSSL_strdup(propq);
3900 if (ret->propq == NULL)
3905 ret->min_proto_version = 0;
3906 ret->max_proto_version = 0;
3907 ret->mode = SSL_MODE_AUTO_RETRY;
3908 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
3909 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
3910 /* We take the system default. */
3911 ret->session_timeout = meth->get_timeout();
3912 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
3913 ret->verify_mode = SSL_VERIFY_NONE;
3915 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
3916 if (ret->sessions == NULL) {
3917 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
3920 ret->cert_store = X509_STORE_new();
3921 if (ret->cert_store == NULL) {
3922 ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
3925 #ifndef OPENSSL_NO_CT
3926 ret->ctlog_store = CTLOG_STORE_new_ex(libctx, propq);
3927 if (ret->ctlog_store == NULL) {
3928 ERR_raise(ERR_LIB_SSL, ERR_R_CT_LIB);
3933 /* initialize cipher/digest methods table */
3934 if (!ssl_load_ciphers(ret)) {
3935 ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
3939 if (!ssl_load_groups(ret)) {
3940 ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
3944 /* load provider sigalgs */
3945 if (!ssl_load_sigalgs(ret)) {
3946 ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
3950 /* initialise sig algs */
3951 if (!ssl_setup_sigalgs(ret)) {
3952 ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
3956 if (!SSL_CTX_set_ciphersuites(ret, OSSL_default_ciphersuites())) {
3957 ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
3961 if ((ret->cert = ssl_cert_new(SSL_PKEY_NUM + ret->sigalg_list_len)) == NULL) {
3962 ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
3966 if (!ssl_create_cipher_list(ret,
3967 ret->tls13_ciphersuites,
3968 &ret->cipher_list, &ret->cipher_list_by_id,
3969 OSSL_default_cipher_list(), ret->cert)
3970 || sk_SSL_CIPHER_num(ret->cipher_list) <= 0) {
3971 ERR_raise(ERR_LIB_SSL, SSL_R_LIBRARY_HAS_NO_CIPHERS);
3975 ret->param = X509_VERIFY_PARAM_new();
3976 if (ret->param == NULL) {
3977 ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
3982 * If these aren't available from the provider we'll get NULL returns.
3983 * That's fine but will cause errors later if SSLv3 is negotiated
3985 ret->md5 = ssl_evp_md_fetch(libctx, NID_md5, propq);
3986 ret->sha1 = ssl_evp_md_fetch(libctx, NID_sha1, propq);
3988 if ((ret->ca_names = sk_X509_NAME_new_null()) == NULL) {
3989 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
3993 if ((ret->client_ca_names = sk_X509_NAME_new_null()) == NULL) {
3994 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
3998 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data)) {
3999 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
4003 if ((ret->ext.secure = OPENSSL_secure_zalloc(sizeof(*ret->ext.secure))) == NULL)
4006 /* No compression for DTLS */
4007 if (!(meth->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS))
4008 ret->comp_methods = SSL_COMP_get_compression_methods();
4010 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
4011 ret->split_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
4013 /* Setup RFC5077 ticket keys */
4014 if ((RAND_bytes_ex(libctx, ret->ext.tick_key_name,
4015 sizeof(ret->ext.tick_key_name), 0) <= 0)
4016 || (RAND_priv_bytes_ex(libctx, ret->ext.secure->tick_hmac_key,
4017 sizeof(ret->ext.secure->tick_hmac_key), 0) <= 0)
4018 || (RAND_priv_bytes_ex(libctx, ret->ext.secure->tick_aes_key,
4019 sizeof(ret->ext.secure->tick_aes_key), 0) <= 0))
4020 ret->options |= SSL_OP_NO_TICKET;
4022 if (RAND_priv_bytes_ex(libctx, ret->ext.cookie_hmac_key,
4023 sizeof(ret->ext.cookie_hmac_key), 0) <= 0) {
4024 ERR_raise(ERR_LIB_SSL, ERR_R_RAND_LIB);
4028 #ifndef OPENSSL_NO_SRP
4029 if (!ssl_ctx_srp_ctx_init_intern(ret)) {
4030 ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
4034 #ifndef OPENSSL_NO_ENGINE
4035 # ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO
4036 # define eng_strx(x) #x
4037 # define eng_str(x) eng_strx(x)
4038 /* Use specific client engine automatically... ignore errors */
4041 eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
4044 ENGINE_load_builtin_engines();
4045 eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
4047 if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng))
4053 #ifndef OPENSSL_NO_COMP_ALG
4055 * Set the default order: brotli, zlib, zstd
4056 * Including only those enabled algorithms
4058 memset(ret->cert_comp_prefs, 0, sizeof(ret->cert_comp_prefs));
4060 if (ossl_comp_has_alg(TLSEXT_comp_cert_brotli))
4061 ret->cert_comp_prefs[i++] = TLSEXT_comp_cert_brotli;
4062 if (ossl_comp_has_alg(TLSEXT_comp_cert_zlib))
4063 ret->cert_comp_prefs[i++] = TLSEXT_comp_cert_zlib;
4064 if (ossl_comp_has_alg(TLSEXT_comp_cert_zstd))
4065 ret->cert_comp_prefs[i++] = TLSEXT_comp_cert_zstd;
4068 * Disable compression by default to prevent CRIME. Applications can
4069 * re-enable compression by configuring
4070 * SSL_CTX_clear_options(ctx, SSL_OP_NO_COMPRESSION);
4071 * or by using the SSL_CONF library. Similarly we also enable TLSv1.3
4072 * middlebox compatibility by default. This may be disabled by default in
4073 * a later OpenSSL version.
4075 ret->options |= SSL_OP_NO_COMPRESSION | SSL_OP_ENABLE_MIDDLEBOX_COMPAT;
4077 ret->ext.status_type = TLSEXT_STATUSTYPE_nothing;
4080 * We cannot usefully set a default max_early_data here (which gets
4081 * propagated in SSL_new(), for the following reason: setting the
4082 * SSL field causes tls_construct_stoc_early_data() to tell the
4083 * client that early data will be accepted when constructing a TLS 1.3
4084 * session ticket, and the client will accordingly send us early data
4085 * when using that ticket (if the client has early data to send).
4086 * However, in order for the early data to actually be consumed by
4087 * the application, the application must also have calls to
4088 * SSL_read_early_data(); otherwise we'll just skip past the early data
4089 * and ignore it. So, since the application must add calls to
4090 * SSL_read_early_data(), we also require them to add
4091 * calls to SSL_CTX_set_max_early_data() in order to use early data,
4092 * eliminating the bandwidth-wasting early data in the case described
4095 ret->max_early_data = 0;
4098 * Default recv_max_early_data is a fully loaded single record. Could be
4099 * split across multiple records in practice. We set this differently to
4100 * max_early_data so that, in the default case, we do not advertise any
4101 * support for early_data, but if a client were to send us some (e.g.
4102 * because of an old, stale ticket) then we will tolerate it and skip over
4105 ret->recv_max_early_data = SSL3_RT_MAX_PLAIN_LENGTH;
4107 /* By default we send two session tickets automatically in TLSv1.3 */
4108 ret->num_tickets = 2;
4110 ssl_ctx_system_config(ret);
4118 SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
4120 return SSL_CTX_new_ex(NULL, NULL, meth);
4123 int SSL_CTX_up_ref(SSL_CTX *ctx)
4127 if (CRYPTO_UP_REF(&ctx->references, &i) <= 0)
4130 REF_PRINT_COUNT("SSL_CTX", ctx);
4131 REF_ASSERT_ISNT(i < 2);
4132 return ((i > 1) ? 1 : 0);
4135 void SSL_CTX_free(SSL_CTX *a)
4143 CRYPTO_DOWN_REF(&a->references, &i);
4144 REF_PRINT_COUNT("SSL_CTX", a);
4147 REF_ASSERT_ISNT(i < 0);
4149 X509_VERIFY_PARAM_free(a->param);
4150 dane_ctx_final(&a->dane);
4153 * Free internal session cache. However: the remove_cb() may reference
4154 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
4155 * after the sessions were flushed.
4156 * As the ex_data handling routines might also touch the session cache,
4157 * the most secure solution seems to be: empty (flush) the cache, then
4158 * free ex_data, then finally free the cache.
4159 * (See ticket [openssl.org #212].)
4161 if (a->sessions != NULL)
4162 SSL_CTX_flush_sessions(a, 0);
4164 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
4165 lh_SSL_SESSION_free(a->sessions);
4166 X509_STORE_free(a->cert_store);
4167 #ifndef OPENSSL_NO_CT
4168 CTLOG_STORE_free(a->ctlog_store);
4170 sk_SSL_CIPHER_free(a->cipher_list);
4171 sk_SSL_CIPHER_free(a->cipher_list_by_id);
4172 sk_SSL_CIPHER_free(a->tls13_ciphersuites);
4173 ssl_cert_free(a->cert);
4174 sk_X509_NAME_pop_free(a->ca_names, X509_NAME_free);
4175 sk_X509_NAME_pop_free(a->client_ca_names, X509_NAME_free);
4176 OSSL_STACK_OF_X509_free(a->extra_certs);
4177 a->comp_methods = NULL;
4178 #ifndef OPENSSL_NO_SRTP
4179 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
4181 #ifndef OPENSSL_NO_SRP
4182 ssl_ctx_srp_ctx_free_intern(a);
4184 #ifndef OPENSSL_NO_ENGINE
4185 tls_engine_finish(a->client_cert_engine);
4188 OPENSSL_free(a->ext.ecpointformats);
4189 OPENSSL_free(a->ext.supportedgroups);
4190 OPENSSL_free(a->ext.supported_groups_default);
4191 OPENSSL_free(a->ext.alpn);
4192 OPENSSL_secure_free(a->ext.secure);
4194 ssl_evp_md_free(a->md5);
4195 ssl_evp_md_free(a->sha1);
4197 for (j = 0; j < SSL_ENC_NUM_IDX; j++)
4198 ssl_evp_cipher_free(a->ssl_cipher_methods[j]);
4199 for (j = 0; j < SSL_MD_NUM_IDX; j++)
4200 ssl_evp_md_free(a->ssl_digest_methods[j]);
4201 for (j = 0; j < a->group_list_len; j++) {
4202 OPENSSL_free(a->group_list[j].tlsname);
4203 OPENSSL_free(a->group_list[j].realname);
4204 OPENSSL_free(a->group_list[j].algorithm);
4206 OPENSSL_free(a->group_list);
4207 for (j = 0; j < a->sigalg_list_len; j++) {
4208 OPENSSL_free(a->sigalg_list[j].name);
4209 OPENSSL_free(a->sigalg_list[j].sigalg_name);
4210 OPENSSL_free(a->sigalg_list[j].sigalg_oid);
4211 OPENSSL_free(a->sigalg_list[j].sig_name);
4212 OPENSSL_free(a->sigalg_list[j].sig_oid);
4213 OPENSSL_free(a->sigalg_list[j].hash_name);
4214 OPENSSL_free(a->sigalg_list[j].hash_oid);
4215 OPENSSL_free(a->sigalg_list[j].keytype);
4216 OPENSSL_free(a->sigalg_list[j].keytype_oid);
4218 OPENSSL_free(a->sigalg_list);
4219 OPENSSL_free(a->ssl_cert_info);
4221 OPENSSL_free(a->sigalg_lookup_cache);
4222 OPENSSL_free(a->tls12_sigalgs);
4224 OPENSSL_free(a->client_cert_type);
4225 OPENSSL_free(a->server_cert_type);
4227 CRYPTO_THREAD_lock_free(a->lock);
4228 CRYPTO_FREE_REF(&a->references);
4229 #ifdef TSAN_REQUIRES_LOCKING
4230 CRYPTO_THREAD_lock_free(a->tsan_lock);
4233 OPENSSL_free(a->propq);
4238 void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
4240 ctx->default_passwd_callback = cb;
4243 void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u)
4245 ctx->default_passwd_callback_userdata = u;
4248 pem_password_cb *SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx)
4250 return ctx->default_passwd_callback;
4253 void *SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx)
4255 return ctx->default_passwd_callback_userdata;
4258 void SSL_set_default_passwd_cb(SSL *s, pem_password_cb *cb)
4260 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
4265 sc->default_passwd_callback = cb;
4268 void SSL_set_default_passwd_cb_userdata(SSL *s, void *u)
4270 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
4275 sc->default_passwd_callback_userdata = u;
4278 pem_password_cb *SSL_get_default_passwd_cb(SSL *s)
4280 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
4285 return sc->default_passwd_callback;
4288 void *SSL_get_default_passwd_cb_userdata(SSL *s)
4290 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
4295 return sc->default_passwd_callback_userdata;
4298 void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
4299 int (*cb) (X509_STORE_CTX *, void *),
4302 ctx->app_verify_callback = cb;
4303 ctx->app_verify_arg = arg;
4306 void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
4307 int (*cb) (int, X509_STORE_CTX *))
4309 ctx->verify_mode = mode;
4310 ctx->default_verify_callback = cb;
4313 void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth)
4315 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
4318 void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb) (SSL *ssl, void *arg), void *arg)
4320 ssl_cert_set_cert_cb(c->cert, cb, arg);
4323 void SSL_set_cert_cb(SSL *s, int (*cb) (SSL *ssl, void *arg), void *arg)
4325 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
4330 ssl_cert_set_cert_cb(sc->cert, cb, arg);
4333 void ssl_set_masks(SSL_CONNECTION *s)
4336 uint32_t *pvalid = s->s3.tmp.valid_flags;
4337 int rsa_enc, rsa_sign, dh_tmp, dsa_sign;
4338 unsigned long mask_k, mask_a;
4339 int have_ecc_cert, ecdsa_ok;
4344 dh_tmp = (c->dh_tmp != NULL
4345 || c->dh_tmp_cb != NULL
4348 rsa_enc = pvalid[SSL_PKEY_RSA] & CERT_PKEY_VALID;
4349 rsa_sign = pvalid[SSL_PKEY_RSA] & CERT_PKEY_VALID;
4350 dsa_sign = pvalid[SSL_PKEY_DSA_SIGN] & CERT_PKEY_VALID;
4351 have_ecc_cert = pvalid[SSL_PKEY_ECC] & CERT_PKEY_VALID;
4355 OSSL_TRACE4(TLS_CIPHER, "dh_tmp=%d rsa_enc=%d rsa_sign=%d dsa_sign=%d\n",
4356 dh_tmp, rsa_enc, rsa_sign, dsa_sign);
4358 #ifndef OPENSSL_NO_GOST
4359 if (ssl_has_cert(s, SSL_PKEY_GOST12_512)) {
4360 mask_k |= SSL_kGOST | SSL_kGOST18;
4361 mask_a |= SSL_aGOST12;
4363 if (ssl_has_cert(s, SSL_PKEY_GOST12_256)) {
4364 mask_k |= SSL_kGOST | SSL_kGOST18;
4365 mask_a |= SSL_aGOST12;
4367 if (ssl_has_cert(s, SSL_PKEY_GOST01)) {
4368 mask_k |= SSL_kGOST;
4369 mask_a |= SSL_aGOST01;
4380 * If we only have an RSA-PSS certificate allow RSA authentication
4381 * if TLS 1.2 and peer supports it.
4384 if (rsa_enc || rsa_sign || (ssl_has_cert(s, SSL_PKEY_RSA_PSS_SIGN)
4385 && pvalid[SSL_PKEY_RSA_PSS_SIGN] & CERT_PKEY_EXPLICIT_SIGN
4386 && TLS1_get_version(&s->ssl) == TLS1_2_VERSION))
4393 mask_a |= SSL_aNULL;
4396 * You can do anything with an RPK key, since there's no cert to restrict it
4397 * But we need to check for private keys
4399 if (pvalid[SSL_PKEY_RSA] & CERT_PKEY_RPK) {
4403 if (pvalid[SSL_PKEY_ECC] & CERT_PKEY_RPK)
4404 mask_a |= SSL_aECDSA;
4405 if (TLS1_get_version(&s->ssl) == TLS1_2_VERSION) {
4406 if (pvalid[SSL_PKEY_RSA_PSS_SIGN] & CERT_PKEY_RPK)
4408 if (pvalid[SSL_PKEY_ED25519] & CERT_PKEY_RPK
4409 || pvalid[SSL_PKEY_ED448] & CERT_PKEY_RPK)
4410 mask_a |= SSL_aECDSA;
4414 * An ECC certificate may be usable for ECDH and/or ECDSA cipher suites
4415 * depending on the key usage extension.
4417 if (have_ecc_cert) {
4419 ex_kusage = X509_get_key_usage(c->pkeys[SSL_PKEY_ECC].x509);
4420 ecdsa_ok = ex_kusage & X509v3_KU_DIGITAL_SIGNATURE;
4421 if (!(pvalid[SSL_PKEY_ECC] & CERT_PKEY_SIGN))
4424 mask_a |= SSL_aECDSA;
4426 /* Allow Ed25519 for TLS 1.2 if peer supports it */
4427 if (!(mask_a & SSL_aECDSA) && ssl_has_cert(s, SSL_PKEY_ED25519)
4428 && pvalid[SSL_PKEY_ED25519] & CERT_PKEY_EXPLICIT_SIGN
4429 && TLS1_get_version(&s->ssl) == TLS1_2_VERSION)
4430 mask_a |= SSL_aECDSA;
4432 /* Allow Ed448 for TLS 1.2 if peer supports it */
4433 if (!(mask_a & SSL_aECDSA) && ssl_has_cert(s, SSL_PKEY_ED448)
4434 && pvalid[SSL_PKEY_ED448] & CERT_PKEY_EXPLICIT_SIGN
4435 && TLS1_get_version(&s->ssl) == TLS1_2_VERSION)
4436 mask_a |= SSL_aECDSA;
4438 mask_k |= SSL_kECDHE;
4440 #ifndef OPENSSL_NO_PSK
4443 if (mask_k & SSL_kRSA)
4444 mask_k |= SSL_kRSAPSK;
4445 if (mask_k & SSL_kDHE)
4446 mask_k |= SSL_kDHEPSK;
4447 if (mask_k & SSL_kECDHE)
4448 mask_k |= SSL_kECDHEPSK;
4451 s->s3.tmp.mask_k = mask_k;
4452 s->s3.tmp.mask_a = mask_a;
4455 int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL_CONNECTION *s)
4457 if (s->s3.tmp.new_cipher->algorithm_auth & SSL_aECDSA) {
4458 /* key usage, if present, must allow signing */
4459 if (!(X509_get_key_usage(x) & X509v3_KU_DIGITAL_SIGNATURE)) {
4460 ERR_raise(ERR_LIB_SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
4464 return 1; /* all checks are ok */
4467 int ssl_get_server_cert_serverinfo(SSL_CONNECTION *s,
4468 const unsigned char **serverinfo,
4469 size_t *serverinfo_length)
4471 CERT_PKEY *cpk = s->s3.tmp.cert;
4472 *serverinfo_length = 0;
4474 if (cpk == NULL || cpk->serverinfo == NULL)
4477 *serverinfo = cpk->serverinfo;
4478 *serverinfo_length = cpk->serverinfo_length;
4482 void ssl_update_cache(SSL_CONNECTION *s, int mode)
4487 * If the session_id_length is 0, we are not supposed to cache it, and it
4488 * would be rather hard to do anyway :-)
4490 if (s->session->session_id_length == 0)
4494 * If sid_ctx_length is 0 there is no specific application context
4495 * associated with this session, so when we try to resume it and
4496 * SSL_VERIFY_PEER is requested to verify the client identity, we have no
4497 * indication that this is actually a session for the proper application
4498 * context, and the *handshake* will fail, not just the resumption attempt.
4499 * Do not cache (on the server) these sessions that are not resumable
4500 * (clients can set SSL_VERIFY_PEER without needing a sid_ctx set).
4502 if (s->server && s->session->sid_ctx_length == 0
4503 && (s->verify_mode & SSL_VERIFY_PEER) != 0)
4506 i = s->session_ctx->session_cache_mode;
4508 && (!s->hit || SSL_CONNECTION_IS_TLS13(s))) {
4510 * Add the session to the internal cache. In server side TLSv1.3 we
4511 * normally don't do this because by default it's a full stateless ticket
4512 * with only a dummy session id so there is no reason to cache it,
4514 * - we are doing early_data, in which case we cache so that we can
4516 * - the application has set a remove_session_cb so needs to know about
4517 * session timeout events
4518 * - SSL_OP_NO_TICKET is set in which case it is a stateful ticket
4520 if ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE) == 0
4521 && (!SSL_CONNECTION_IS_TLS13(s)
4523 || (s->max_early_data > 0
4524 && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0)
4525 || s->session_ctx->remove_session_cb != NULL
4526 || (s->options & SSL_OP_NO_TICKET) != 0))
4527 SSL_CTX_add_session(s->session_ctx, s->session);
4530 * Add the session to the external cache. We do this even in server side
4531 * TLSv1.3 without early data because some applications just want to
4532 * know about the creation of a session and aren't doing a full cache.
4534 if (s->session_ctx->new_session_cb != NULL) {
4535 SSL_SESSION_up_ref(s->session);
4536 if (!s->session_ctx->new_session_cb(SSL_CONNECTION_GET_SSL(s),
4538 SSL_SESSION_free(s->session);
4542 /* auto flush every 255 connections */
4543 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && ((i & mode) == mode)) {
4544 TSAN_QUALIFIER int *stat;
4546 if (mode & SSL_SESS_CACHE_CLIENT)
4547 stat = &s->session_ctx->stats.sess_connect_good;
4549 stat = &s->session_ctx->stats.sess_accept_good;
4550 if ((ssl_tsan_load(s->session_ctx, stat) & 0xff) == 0xff)
4551 SSL_CTX_flush_sessions(s->session_ctx, (unsigned long)time(NULL));
4555 const SSL_METHOD *SSL_CTX_get_ssl_method(const SSL_CTX *ctx)
4560 const SSL_METHOD *SSL_get_ssl_method(const SSL *s)
4565 int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
4568 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
4570 /* Not allowed for QUIC */
4572 || (s->type != SSL_TYPE_SSL_CONNECTION && s->method != meth)
4573 || (s->type == SSL_TYPE_SSL_CONNECTION && IS_QUIC_METHOD(meth)))
4576 if (s->method != meth) {
4577 const SSL_METHOD *sm = s->method;
4578 int (*hf) (SSL *) = sc->handshake_func;
4580 if (sm->version == meth->version)
4585 ret = s->method->ssl_init(s);
4588 if (hf == sm->ssl_connect)
4589 sc->handshake_func = meth->ssl_connect;
4590 else if (hf == sm->ssl_accept)
4591 sc->handshake_func = meth->ssl_accept;
4596 int SSL_get_error(const SSL *s, int i)
4598 return ossl_ssl_get_error(s, i, /*check_err=*/1);
4601 int ossl_ssl_get_error(const SSL *s, int i, int check_err)
4606 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
4609 return SSL_ERROR_NONE;
4611 #ifndef OPENSSL_NO_QUIC
4613 reason = ossl_quic_get_error(s, i);
4614 if (reason != SSL_ERROR_NONE)
4620 return SSL_ERROR_SSL;
4623 * Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
4624 * where we do encode the error
4626 if (check_err && (l = ERR_peek_error()) != 0) {
4627 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
4628 return SSL_ERROR_SYSCALL;
4630 return SSL_ERROR_SSL;
4633 #ifndef OPENSSL_NO_QUIC
4637 if (SSL_want_read(s)) {
4638 bio = SSL_get_rbio(s);
4639 if (BIO_should_read(bio))
4640 return SSL_ERROR_WANT_READ;
4641 else if (BIO_should_write(bio))
4643 * This one doesn't make too much sense ... We never try to
4644 * write to the rbio, and an application program where rbio and
4645 * wbio are separate couldn't even know what it should wait for.
4646 * However if we ever set s->rwstate incorrectly (so that we
4647 * have SSL_want_read(s) instead of SSL_want_write(s)) and rbio
4648 * and wbio *are* the same, this test works around that bug; so
4649 * it might be safer to keep it.
4651 return SSL_ERROR_WANT_WRITE;
4652 else if (BIO_should_io_special(bio)) {
4653 reason = BIO_get_retry_reason(bio);
4654 if (reason == BIO_RR_CONNECT)
4655 return SSL_ERROR_WANT_CONNECT;
4656 else if (reason == BIO_RR_ACCEPT)
4657 return SSL_ERROR_WANT_ACCEPT;
4659 return SSL_ERROR_SYSCALL; /* unknown */
4663 if (SSL_want_write(s)) {
4665 * Access wbio directly - in order to use the buffered bio if
4669 if (BIO_should_write(bio))
4670 return SSL_ERROR_WANT_WRITE;
4671 else if (BIO_should_read(bio))
4673 * See above (SSL_want_read(s) with BIO_should_write(bio))
4675 return SSL_ERROR_WANT_READ;
4676 else if (BIO_should_io_special(bio)) {
4677 reason = BIO_get_retry_reason(bio);
4678 if (reason == BIO_RR_CONNECT)
4679 return SSL_ERROR_WANT_CONNECT;
4680 else if (reason == BIO_RR_ACCEPT)
4681 return SSL_ERROR_WANT_ACCEPT;
4683 return SSL_ERROR_SYSCALL;
4688 if (SSL_want_x509_lookup(s))
4689 return SSL_ERROR_WANT_X509_LOOKUP;
4690 if (SSL_want_retry_verify(s))
4691 return SSL_ERROR_WANT_RETRY_VERIFY;
4692 if (SSL_want_async(s))
4693 return SSL_ERROR_WANT_ASYNC;
4694 if (SSL_want_async_job(s))
4695 return SSL_ERROR_WANT_ASYNC_JOB;
4696 if (SSL_want_client_hello_cb(s))
4697 return SSL_ERROR_WANT_CLIENT_HELLO_CB;
4699 if ((sc->shutdown & SSL_RECEIVED_SHUTDOWN) &&
4700 (sc->s3.warn_alert == SSL_AD_CLOSE_NOTIFY))
4701 return SSL_ERROR_ZERO_RETURN;
4703 return SSL_ERROR_SYSCALL;
4706 static int ssl_do_handshake_intern(void *vargs)
4708 struct ssl_async_args *args = (struct ssl_async_args *)vargs;
4710 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
4715 return sc->handshake_func(s);
4718 int SSL_do_handshake(SSL *s)
4721 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
4723 #ifndef OPENSSL_NO_QUIC
4725 return ossl_quic_do_handshake(s);
4728 if (sc->handshake_func == NULL) {
4729 ERR_raise(ERR_LIB_SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
4733 ossl_statem_check_finish_init(sc, -1);
4735 s->method->ssl_renegotiate_check(s, 0);
4737 if (SSL_in_init(s) || SSL_in_before(s)) {
4738 if ((sc->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
4739 struct ssl_async_args args;
4741 memset(&args, 0, sizeof(args));
4744 ret = ssl_start_async_job(s, &args, ssl_do_handshake_intern);
4746 ret = sc->handshake_func(s);
4752 void SSL_set_accept_state(SSL *s)
4754 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
4756 #ifndef OPENSSL_NO_QUIC
4758 ossl_quic_set_accept_state(s);
4765 ossl_statem_clear(sc);
4766 sc->handshake_func = s->method->ssl_accept;
4767 /* Ignore return value. Its a void public API function */
4768 clear_record_layer(sc);
4771 void SSL_set_connect_state(SSL *s)
4773 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
4775 #ifndef OPENSSL_NO_QUIC
4777 ossl_quic_set_connect_state(s);
4784 ossl_statem_clear(sc);
4785 sc->handshake_func = s->method->ssl_connect;
4786 /* Ignore return value. Its a void public API function */
4787 clear_record_layer(sc);
4790 int ssl_undefined_function(SSL *s)
4792 ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
4796 int ssl_undefined_void_function(void)
4798 ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
4802 int ssl_undefined_const_function(const SSL *s)
4807 const SSL_METHOD *ssl_bad_method(int ver)
4809 ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
4813 const char *ssl_protocol_to_string(int version)
4817 case TLS1_3_VERSION:
4820 case TLS1_2_VERSION:
4823 case TLS1_1_VERSION:
4838 case DTLS1_2_VERSION:
4846 const char *SSL_get_version(const SSL *s)
4848 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
4850 #ifndef OPENSSL_NO_QUIC
4851 /* We only support QUICv1 - so if its QUIC its QUICv1 */
4852 if (s->type == SSL_TYPE_QUIC_CONNECTION || s->type == SSL_TYPE_QUIC_XSO)
4859 return ssl_protocol_to_string(sc->version);
4862 __owur int SSL_get_handshake_rtt(const SSL *s, uint64_t *rtt)
4864 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
4868 if (sc->ts_msg_write.t <= 0 || sc->ts_msg_read.t <= 0)
4869 return 0; /* data not (yet) available */
4870 if (sc->ts_msg_read.t < sc->ts_msg_write.t)
4873 *rtt = ossl_time2us(ossl_time_subtract(sc->ts_msg_read, sc->ts_msg_write));
4877 static int dup_ca_names(STACK_OF(X509_NAME) **dst, STACK_OF(X509_NAME) *src)
4879 STACK_OF(X509_NAME) *sk;
4888 if ((sk = sk_X509_NAME_new_null()) == NULL)
4890 for (i = 0; i < sk_X509_NAME_num(src); i++) {
4891 xn = X509_NAME_dup(sk_X509_NAME_value(src, i));
4893 sk_X509_NAME_pop_free(sk, X509_NAME_free);
4896 if (sk_X509_NAME_insert(sk, xn, i) == 0) {
4898 sk_X509_NAME_pop_free(sk, X509_NAME_free);
4907 SSL *SSL_dup(SSL *s)
4911 /* TODO(QUIC FUTURE): Add a SSL_METHOD function for duplication */
4912 SSL_CONNECTION *retsc;
4913 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
4918 /* If we're not quiescent, just up_ref! */
4919 if (!SSL_in_init(s) || !SSL_in_before(s)) {
4920 CRYPTO_UP_REF(&s->references, &i);
4925 * Otherwise, copy configuration state, and session if set.
4927 if ((ret = SSL_new(SSL_get_SSL_CTX(s))) == NULL)
4929 if ((retsc = SSL_CONNECTION_FROM_SSL_ONLY(ret)) == NULL)
4932 if (sc->session != NULL) {
4934 * Arranges to share the same session via up_ref. This "copies"
4935 * session-id, SSL_METHOD, sid_ctx, and 'cert'
4937 if (!SSL_copy_session_id(ret, s))
4941 * No session has been established yet, so we have to expect that
4942 * s->cert or ret->cert will be changed later -- they should not both
4943 * point to the same object, and thus we can't use
4944 * SSL_copy_session_id.
4946 if (!SSL_set_ssl_method(ret, s->method))
4949 if (sc->cert != NULL) {
4950 ssl_cert_free(retsc->cert);
4951 retsc->cert = ssl_cert_dup(sc->cert);
4952 if (retsc->cert == NULL)
4956 if (!SSL_set_session_id_context(ret, sc->sid_ctx,
4957 (int)sc->sid_ctx_length))
4961 if (!ssl_dane_dup(retsc, sc))
4963 retsc->version = sc->version;
4964 retsc->options = sc->options;
4965 retsc->min_proto_version = sc->min_proto_version;
4966 retsc->max_proto_version = sc->max_proto_version;
4967 retsc->mode = sc->mode;
4968 SSL_set_max_cert_list(ret, SSL_get_max_cert_list(s));
4969 SSL_set_read_ahead(ret, SSL_get_read_ahead(s));
4970 retsc->msg_callback = sc->msg_callback;
4971 retsc->msg_callback_arg = sc->msg_callback_arg;
4972 SSL_set_verify(ret, SSL_get_verify_mode(s), SSL_get_verify_callback(s));
4973 SSL_set_verify_depth(ret, SSL_get_verify_depth(s));
4974 retsc->generate_session_id = sc->generate_session_id;
4976 SSL_set_info_callback(ret, SSL_get_info_callback(s));
4978 /* copy app data, a little dangerous perhaps */
4979 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data))
4982 retsc->server = sc->server;
4983 if (sc->handshake_func) {
4985 SSL_set_accept_state(ret);
4987 SSL_set_connect_state(ret);
4989 retsc->shutdown = sc->shutdown;
4990 retsc->hit = sc->hit;
4992 retsc->default_passwd_callback = sc->default_passwd_callback;
4993 retsc->default_passwd_callback_userdata = sc->default_passwd_callback_userdata;
4995 X509_VERIFY_PARAM_inherit(retsc->param, sc->param);
4997 /* dup the cipher_list and cipher_list_by_id stacks */
4998 if (sc->cipher_list != NULL) {
4999 if ((retsc->cipher_list = sk_SSL_CIPHER_dup(sc->cipher_list)) == NULL)
5002 if (sc->cipher_list_by_id != NULL)
5003 if ((retsc->cipher_list_by_id = sk_SSL_CIPHER_dup(sc->cipher_list_by_id))
5007 /* Dup the client_CA list */
5008 if (!dup_ca_names(&retsc->ca_names, sc->ca_names)
5009 || !dup_ca_names(&retsc->client_ca_names, sc->client_ca_names))
5019 X509 *SSL_get_certificate(const SSL *s)
5021 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
5026 if (sc->cert != NULL)
5027 return sc->cert->key->x509;
5032 EVP_PKEY *SSL_get_privatekey(const SSL *s)
5034 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5039 if (sc->cert != NULL)
5040 return sc->cert->key->privatekey;
5045 X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
5047 if (ctx->cert != NULL)
5048 return ctx->cert->key->x509;
5053 EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
5055 if (ctx->cert != NULL)
5056 return ctx->cert->key->privatekey;
5061 const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
5063 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5068 if ((sc->session != NULL) && (sc->session->cipher != NULL))
5069 return sc->session->cipher;
5073 const SSL_CIPHER *SSL_get_pending_cipher(const SSL *s)
5075 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5080 return sc->s3.tmp.new_cipher;
5083 const COMP_METHOD *SSL_get_current_compression(const SSL *s)
5085 #ifndef OPENSSL_NO_COMP
5086 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s);
5091 return sc->rlayer.wrlmethod->get_compression(sc->rlayer.wrl);
5097 const COMP_METHOD *SSL_get_current_expansion(const SSL *s)
5099 #ifndef OPENSSL_NO_COMP
5100 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s);
5105 return sc->rlayer.rrlmethod->get_compression(sc->rlayer.rrl);
5111 int ssl_init_wbio_buffer(SSL_CONNECTION *s)
5115 if (s->bbio != NULL) {
5116 /* Already buffered. */
5120 bbio = BIO_new(BIO_f_buffer());
5121 if (bbio == NULL || BIO_set_read_buffer_size(bbio, 1) <= 0) {
5123 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
5127 s->wbio = BIO_push(bbio, s->wbio);
5129 s->rlayer.wrlmethod->set1_bio(s->rlayer.wrl, s->wbio);
5134 int ssl_free_wbio_buffer(SSL_CONNECTION *s)
5136 /* callers ensure s is never null */
5137 if (s->bbio == NULL)
5140 s->wbio = BIO_pop(s->wbio);
5141 s->rlayer.wrlmethod->set1_bio(s->rlayer.wrl, s->wbio);
5149 void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode)
5151 ctx->quiet_shutdown = mode;
5154 int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
5156 return ctx->quiet_shutdown;
5159 void SSL_set_quiet_shutdown(SSL *s, int mode)
5161 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
5163 /* Not supported with QUIC */
5167 sc->quiet_shutdown = mode;
5170 int SSL_get_quiet_shutdown(const SSL *s)
5172 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s);
5174 /* Not supported with QUIC */
5178 return sc->quiet_shutdown;
5181 void SSL_set_shutdown(SSL *s, int mode)
5183 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
5185 /* Not supported with QUIC */
5189 sc->shutdown = mode;
5192 int SSL_get_shutdown(const SSL *s)
5194 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s);
5196 #ifndef OPENSSL_NO_QUIC
5197 /* QUIC: Just indicate whether the connection was shutdown cleanly. */
5199 return ossl_quic_get_shutdown(s);
5205 return sc->shutdown;
5208 int SSL_version(const SSL *s)
5210 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5212 #ifndef OPENSSL_NO_QUIC
5213 /* We only support QUICv1 - so if its QUIC its QUICv1 */
5214 if (s->type == SSL_TYPE_QUIC_CONNECTION || s->type == SSL_TYPE_QUIC_XSO)
5215 return OSSL_QUIC1_VERSION;
5223 int SSL_client_version(const SSL *s)
5225 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5227 #ifndef OPENSSL_NO_QUIC
5228 /* We only support QUICv1 - so if its QUIC its QUICv1 */
5229 if (s->type == SSL_TYPE_QUIC_CONNECTION || s->type == SSL_TYPE_QUIC_XSO)
5230 return OSSL_QUIC1_VERSION;
5235 return sc->client_version;
5238 SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
5243 SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx)
5246 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
5248 /* TODO(QUIC FUTURE): Add support for QUIC */
5252 if (ssl->ctx == ctx)
5255 ctx = sc->session_ctx;
5256 new_cert = ssl_cert_dup(ctx->cert);
5257 if (new_cert == NULL) {
5261 if (!custom_exts_copy_flags(&new_cert->custext, &sc->cert->custext)) {
5262 ssl_cert_free(new_cert);
5266 ssl_cert_free(sc->cert);
5267 sc->cert = new_cert;
5270 * Program invariant: |sid_ctx| has fixed size (SSL_MAX_SID_CTX_LENGTH),
5271 * so setter APIs must prevent invalid lengths from entering the system.
5273 if (!ossl_assert(sc->sid_ctx_length <= sizeof(sc->sid_ctx)))
5277 * If the session ID context matches that of the parent SSL_CTX,
5278 * inherit it from the new SSL_CTX as well. If however the context does
5279 * not match (i.e., it was set per-ssl with SSL_set_session_id_context),
5280 * leave it unchanged.
5282 if ((ssl->ctx != NULL) &&
5283 (sc->sid_ctx_length == ssl->ctx->sid_ctx_length) &&
5284 (memcmp(sc->sid_ctx, ssl->ctx->sid_ctx, sc->sid_ctx_length) == 0)) {
5285 sc->sid_ctx_length = ctx->sid_ctx_length;
5286 memcpy(&sc->sid_ctx, &ctx->sid_ctx, sizeof(sc->sid_ctx));
5289 SSL_CTX_up_ref(ctx);
5290 SSL_CTX_free(ssl->ctx); /* decrement reference count */
5296 int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
5298 return X509_STORE_set_default_paths_ex(ctx->cert_store, ctx->libctx,
5302 int SSL_CTX_set_default_verify_dir(SSL_CTX *ctx)
5304 X509_LOOKUP *lookup;
5306 lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_hash_dir());
5310 /* We ignore errors, in case the directory doesn't exist */
5313 X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT);
5320 int SSL_CTX_set_default_verify_file(SSL_CTX *ctx)
5322 X509_LOOKUP *lookup;
5324 lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_file());
5328 /* We ignore errors, in case the file doesn't exist */
5331 X509_LOOKUP_load_file_ex(lookup, NULL, X509_FILETYPE_DEFAULT, ctx->libctx,
5339 int SSL_CTX_set_default_verify_store(SSL_CTX *ctx)
5341 X509_LOOKUP *lookup;
5343 lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_store());
5347 /* We ignore errors, in case the directory doesn't exist */
5350 X509_LOOKUP_add_store_ex(lookup, NULL, ctx->libctx, ctx->propq);
5357 int SSL_CTX_load_verify_file(SSL_CTX *ctx, const char *CAfile)
5359 return X509_STORE_load_file_ex(ctx->cert_store, CAfile, ctx->libctx,
5363 int SSL_CTX_load_verify_dir(SSL_CTX *ctx, const char *CApath)
5365 return X509_STORE_load_path(ctx->cert_store, CApath);
5368 int SSL_CTX_load_verify_store(SSL_CTX *ctx, const char *CAstore)
5370 return X509_STORE_load_store_ex(ctx->cert_store, CAstore, ctx->libctx,
5374 int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
5377 if (CAfile == NULL && CApath == NULL)
5379 if (CAfile != NULL && !SSL_CTX_load_verify_file(ctx, CAfile))
5381 if (CApath != NULL && !SSL_CTX_load_verify_dir(ctx, CApath))
5386 void SSL_set_info_callback(SSL *ssl,
5387 void (*cb) (const SSL *ssl, int type, int val))
5389 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
5394 sc->info_callback = cb;
5398 * One compiler (Diab DCC) doesn't like argument names in returned function
5401 void (*SSL_get_info_callback(const SSL *ssl)) (const SSL * /* ssl */ ,
5404 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
5409 return sc->info_callback;
5412 void SSL_set_verify_result(SSL *ssl, long arg)
5414 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
5419 sc->verify_result = arg;
5422 long SSL_get_verify_result(const SSL *ssl)
5424 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
5429 return sc->verify_result;
5432 size_t SSL_get_client_random(const SSL *ssl, unsigned char *out, size_t outlen)
5434 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
5440 return sizeof(sc->s3.client_random);
5441 if (outlen > sizeof(sc->s3.client_random))
5442 outlen = sizeof(sc->s3.client_random);
5443 memcpy(out, sc->s3.client_random, outlen);
5447 size_t SSL_get_server_random(const SSL *ssl, unsigned char *out, size_t outlen)
5449 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
5455 return sizeof(sc->s3.server_random);
5456 if (outlen > sizeof(sc->s3.server_random))
5457 outlen = sizeof(sc->s3.server_random);
5458 memcpy(out, sc->s3.server_random, outlen);
5462 size_t SSL_SESSION_get_master_key(const SSL_SESSION *session,
5463 unsigned char *out, size_t outlen)
5466 return session->master_key_length;
5467 if (outlen > session->master_key_length)
5468 outlen = session->master_key_length;
5469 memcpy(out, session->master_key, outlen);
5473 int SSL_SESSION_set1_master_key(SSL_SESSION *sess, const unsigned char *in,
5476 if (len > sizeof(sess->master_key))
5479 memcpy(sess->master_key, in, len);
5480 sess->master_key_length = len;
5485 int SSL_set_ex_data(SSL *s, int idx, void *arg)
5487 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
5490 void *SSL_get_ex_data(const SSL *s, int idx)
5492 return CRYPTO_get_ex_data(&s->ex_data, idx);
5495 int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg)
5497 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
5500 void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx)
5502 return CRYPTO_get_ex_data(&s->ex_data, idx);
5505 X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
5507 return ctx->cert_store;
5510 void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store)
5512 X509_STORE_free(ctx->cert_store);
5513 ctx->cert_store = store;
5516 void SSL_CTX_set1_cert_store(SSL_CTX *ctx, X509_STORE *store)
5519 X509_STORE_up_ref(store);
5520 SSL_CTX_set_cert_store(ctx, store);
5523 int SSL_want(const SSL *s)
5525 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5527 #ifndef OPENSSL_NO_QUIC
5529 return ossl_quic_want(s);
5538 #ifndef OPENSSL_NO_PSK
5539 int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
5541 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
5542 ERR_raise(ERR_LIB_SSL, SSL_R_DATA_LENGTH_TOO_LONG);
5545 OPENSSL_free(ctx->cert->psk_identity_hint);
5546 if (identity_hint != NULL) {
5547 ctx->cert->psk_identity_hint = OPENSSL_strdup(identity_hint);
5548 if (ctx->cert->psk_identity_hint == NULL)
5551 ctx->cert->psk_identity_hint = NULL;
5555 int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
5557 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
5562 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
5563 ERR_raise(ERR_LIB_SSL, SSL_R_DATA_LENGTH_TOO_LONG);
5566 OPENSSL_free(sc->cert->psk_identity_hint);
5567 if (identity_hint != NULL) {
5568 sc->cert->psk_identity_hint = OPENSSL_strdup(identity_hint);
5569 if (sc->cert->psk_identity_hint == NULL)
5572 sc->cert->psk_identity_hint = NULL;
5576 const char *SSL_get_psk_identity_hint(const SSL *s)
5578 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5580 if (sc == NULL || sc->session == NULL)
5583 return sc->session->psk_identity_hint;
5586 const char *SSL_get_psk_identity(const SSL *s)
5588 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5590 if (sc == NULL || sc->session == NULL)
5593 return sc->session->psk_identity;
5596 void SSL_set_psk_client_callback(SSL *s, SSL_psk_client_cb_func cb)
5598 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
5603 sc->psk_client_callback = cb;
5606 void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx, SSL_psk_client_cb_func cb)
5608 ctx->psk_client_callback = cb;
5611 void SSL_set_psk_server_callback(SSL *s, SSL_psk_server_cb_func cb)
5613 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
5618 sc->psk_server_callback = cb;
5621 void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx, SSL_psk_server_cb_func cb)
5623 ctx->psk_server_callback = cb;
5627 void SSL_set_psk_find_session_callback(SSL *s, SSL_psk_find_session_cb_func cb)
5629 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
5634 sc->psk_find_session_cb = cb;
5637 void SSL_CTX_set_psk_find_session_callback(SSL_CTX *ctx,
5638 SSL_psk_find_session_cb_func cb)
5640 ctx->psk_find_session_cb = cb;
5643 void SSL_set_psk_use_session_callback(SSL *s, SSL_psk_use_session_cb_func cb)
5645 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
5650 sc->psk_use_session_cb = cb;
5653 void SSL_CTX_set_psk_use_session_callback(SSL_CTX *ctx,
5654 SSL_psk_use_session_cb_func cb)
5656 ctx->psk_use_session_cb = cb;
5659 void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
5660 void (*cb) (int write_p, int version,
5661 int content_type, const void *buf,
5662 size_t len, SSL *ssl, void *arg))
5664 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
5667 void SSL_set_msg_callback(SSL *ssl,
5668 void (*cb) (int write_p, int version,
5669 int content_type, const void *buf,
5670 size_t len, SSL *ssl, void *arg))
5672 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
5675 void SSL_CTX_set_not_resumable_session_callback(SSL_CTX *ctx,
5676 int (*cb) (SSL *ssl,
5680 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB,
5681 (void (*)(void))cb);
5684 void SSL_set_not_resumable_session_callback(SSL *ssl,
5685 int (*cb) (SSL *ssl,
5686 int is_forward_secure))
5688 SSL_callback_ctrl(ssl, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB,
5689 (void (*)(void))cb);
5692 void SSL_CTX_set_record_padding_callback(SSL_CTX *ctx,
5693 size_t (*cb) (SSL *ssl, int type,
5694 size_t len, void *arg))
5696 ctx->record_padding_cb = cb;
5699 void SSL_CTX_set_record_padding_callback_arg(SSL_CTX *ctx, void *arg)
5701 ctx->record_padding_arg = arg;
5704 void *SSL_CTX_get_record_padding_callback_arg(const SSL_CTX *ctx)
5706 return ctx->record_padding_arg;
5709 int SSL_CTX_set_block_padding(SSL_CTX *ctx, size_t block_size)
5711 if (IS_QUIC_CTX(ctx) && block_size > 1)
5714 /* block size of 0 or 1 is basically no padding */
5715 if (block_size == 1)
5716 ctx->block_padding = 0;
5717 else if (block_size <= SSL3_RT_MAX_PLAIN_LENGTH)
5718 ctx->block_padding = block_size;
5724 int SSL_set_record_padding_callback(SSL *ssl,
5725 size_t (*cb) (SSL *ssl, int type,
5726 size_t len, void *arg))
5729 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
5734 b = SSL_get_wbio(ssl);
5735 if (b == NULL || !BIO_get_ktls_send(b)) {
5736 sc->rlayer.record_padding_cb = cb;
5742 void SSL_set_record_padding_callback_arg(SSL *ssl, void *arg)
5744 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
5749 sc->rlayer.record_padding_arg = arg;
5752 void *SSL_get_record_padding_callback_arg(const SSL *ssl)
5754 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
5759 return sc->rlayer.record_padding_arg;
5762 int SSL_set_block_padding(SSL *ssl, size_t block_size)
5764 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
5766 if (sc == NULL || (IS_QUIC(ssl) && block_size > 1))
5769 /* block size of 0 or 1 is basically no padding */
5770 if (block_size == 1)
5771 sc->rlayer.block_padding = 0;
5772 else if (block_size <= SSL3_RT_MAX_PLAIN_LENGTH)
5773 sc->rlayer.block_padding = block_size;
5779 int SSL_set_num_tickets(SSL *s, size_t num_tickets)
5781 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
5786 sc->num_tickets = num_tickets;
5791 size_t SSL_get_num_tickets(const SSL *s)
5793 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5798 return sc->num_tickets;
5801 int SSL_CTX_set_num_tickets(SSL_CTX *ctx, size_t num_tickets)
5803 ctx->num_tickets = num_tickets;
5808 size_t SSL_CTX_get_num_tickets(const SSL_CTX *ctx)
5810 return ctx->num_tickets;
5813 /* Retrieve handshake hashes */
5814 int ssl_handshake_hash(SSL_CONNECTION *s,
5815 unsigned char *out, size_t outlen,
5818 EVP_MD_CTX *ctx = NULL;
5819 EVP_MD_CTX *hdgst = s->s3.handshake_dgst;
5820 int hashleni = EVP_MD_CTX_get_size(hdgst);
5823 if (hashleni < 0 || (size_t)hashleni > outlen) {
5824 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
5828 ctx = EVP_MD_CTX_new();
5830 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
5834 if (!EVP_MD_CTX_copy_ex(ctx, hdgst)
5835 || EVP_DigestFinal_ex(ctx, out, NULL) <= 0) {
5836 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
5840 *hashlen = hashleni;
5844 EVP_MD_CTX_free(ctx);
5848 int SSL_session_reused(const SSL *s)
5850 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5858 int SSL_is_server(const SSL *s)
5860 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5868 #ifndef OPENSSL_NO_DEPRECATED_1_1_0
5869 void SSL_set_debug(SSL *s, int debug)
5871 /* Old function was do-nothing anyway... */
5877 void SSL_set_security_level(SSL *s, int level)
5879 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
5884 sc->cert->sec_level = level;
5887 int SSL_get_security_level(const SSL *s)
5889 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5894 return sc->cert->sec_level;
5897 void SSL_set_security_callback(SSL *s,
5898 int (*cb) (const SSL *s, const SSL_CTX *ctx,
5899 int op, int bits, int nid,
5900 void *other, void *ex))
5902 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
5907 sc->cert->sec_cb = cb;
5910 int (*SSL_get_security_callback(const SSL *s)) (const SSL *s,
5911 const SSL_CTX *ctx, int op,
5912 int bits, int nid, void *other,
5914 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5919 return sc->cert->sec_cb;
5922 void SSL_set0_security_ex_data(SSL *s, void *ex)
5924 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
5929 sc->cert->sec_ex = ex;
5932 void *SSL_get0_security_ex_data(const SSL *s)
5934 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5939 return sc->cert->sec_ex;
5942 void SSL_CTX_set_security_level(SSL_CTX *ctx, int level)
5944 ctx->cert->sec_level = level;
5947 int SSL_CTX_get_security_level(const SSL_CTX *ctx)
5949 return ctx->cert->sec_level;
5952 void SSL_CTX_set_security_callback(SSL_CTX *ctx,
5953 int (*cb) (const SSL *s, const SSL_CTX *ctx,
5954 int op, int bits, int nid,
5955 void *other, void *ex))
5957 ctx->cert->sec_cb = cb;
5960 int (*SSL_CTX_get_security_callback(const SSL_CTX *ctx)) (const SSL *s,
5966 return ctx->cert->sec_cb;
5969 void SSL_CTX_set0_security_ex_data(SSL_CTX *ctx, void *ex)
5971 ctx->cert->sec_ex = ex;
5974 void *SSL_CTX_get0_security_ex_data(const SSL_CTX *ctx)
5976 return ctx->cert->sec_ex;
5979 uint64_t SSL_CTX_get_options(const SSL_CTX *ctx)
5981 return ctx->options;
5984 uint64_t SSL_get_options(const SSL *s)
5986 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5988 #ifndef OPENSSL_NO_QUIC
5990 return ossl_quic_get_options(s);
5999 uint64_t SSL_CTX_set_options(SSL_CTX *ctx, uint64_t op)
6001 return ctx->options |= op;
6004 uint64_t SSL_set_options(SSL *s, uint64_t op)
6007 OSSL_PARAM options[2], *opts = options;
6009 #ifndef OPENSSL_NO_QUIC
6011 return ossl_quic_set_options(s, op);
6014 sc = SSL_CONNECTION_FROM_SSL(s);
6020 *opts++ = OSSL_PARAM_construct_uint64(OSSL_LIBSSL_RECORD_LAYER_PARAM_OPTIONS,
6022 *opts = OSSL_PARAM_construct_end();
6024 /* Ignore return value */
6025 sc->rlayer.rrlmethod->set_options(sc->rlayer.rrl, options);
6026 sc->rlayer.wrlmethod->set_options(sc->rlayer.wrl, options);
6031 uint64_t SSL_CTX_clear_options(SSL_CTX *ctx, uint64_t op)
6033 return ctx->options &= ~op;
6036 uint64_t SSL_clear_options(SSL *s, uint64_t op)
6038 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6039 OSSL_PARAM options[2], *opts = options;
6041 #ifndef OPENSSL_NO_QUIC
6043 return ossl_quic_clear_options(s, op);
6051 *opts++ = OSSL_PARAM_construct_uint64(OSSL_LIBSSL_RECORD_LAYER_PARAM_OPTIONS,
6053 *opts = OSSL_PARAM_construct_end();
6055 /* Ignore return value */
6056 sc->rlayer.rrlmethod->set_options(sc->rlayer.rrl, options);
6057 sc->rlayer.wrlmethod->set_options(sc->rlayer.wrl, options);
6062 STACK_OF(X509) *SSL_get0_verified_chain(const SSL *s)
6064 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
6069 return sc->verified_chain;
6072 IMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER, ssl_cipher_id);
6074 #ifndef OPENSSL_NO_CT
6077 * Moves SCTs from the |src| stack to the |dst| stack.
6078 * The source of each SCT will be set to |origin|.
6079 * If |dst| points to a NULL pointer, a new stack will be created and owned by
6081 * Returns the number of SCTs moved, or a negative integer if an error occurs.
6082 * The |dst| stack is created and possibly partially populated even in case
6083 * of error, likewise the |src| stack may be left in an intermediate state.
6085 static int ct_move_scts(STACK_OF(SCT) **dst, STACK_OF(SCT) *src,
6086 sct_source_t origin)
6092 *dst = sk_SCT_new_null();
6094 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
6099 while ((sct = sk_SCT_pop(src)) != NULL) {
6100 if (SCT_set_source(sct, origin) != 1)
6103 if (!sk_SCT_push(*dst, sct))
6115 * Look for data collected during ServerHello and parse if found.
6116 * Returns the number of SCTs extracted.
6118 static int ct_extract_tls_extension_scts(SSL_CONNECTION *s)
6120 int scts_extracted = 0;
6122 if (s->ext.scts != NULL) {
6123 const unsigned char *p = s->ext.scts;
6124 STACK_OF(SCT) *scts = o2i_SCT_LIST(NULL, &p, s->ext.scts_len);
6126 scts_extracted = ct_move_scts(&s->scts, scts, SCT_SOURCE_TLS_EXTENSION);
6128 SCT_LIST_free(scts);
6131 return scts_extracted;
6135 * Checks for an OCSP response and then attempts to extract any SCTs found if it
6136 * contains an SCT X509 extension. They will be stored in |s->scts|.
6138 * - The number of SCTs extracted, assuming an OCSP response exists.
6139 * - 0 if no OCSP response exists or it contains no SCTs.
6140 * - A negative integer if an error occurs.
6142 static int ct_extract_ocsp_response_scts(SSL_CONNECTION *s)
6144 # ifndef OPENSSL_NO_OCSP
6145 int scts_extracted = 0;
6146 const unsigned char *p;
6147 OCSP_BASICRESP *br = NULL;
6148 OCSP_RESPONSE *rsp = NULL;
6149 STACK_OF(SCT) *scts = NULL;
6152 if (s->ext.ocsp.resp == NULL || s->ext.ocsp.resp_len == 0)
6155 p = s->ext.ocsp.resp;
6156 rsp = d2i_OCSP_RESPONSE(NULL, &p, (int)s->ext.ocsp.resp_len);
6160 br = OCSP_response_get1_basic(rsp);
6164 for (i = 0; i < OCSP_resp_count(br); ++i) {
6165 OCSP_SINGLERESP *single = OCSP_resp_get0(br, i);
6171 OCSP_SINGLERESP_get1_ext_d2i(single, NID_ct_cert_scts, NULL, NULL);
6173 ct_move_scts(&s->scts, scts, SCT_SOURCE_OCSP_STAPLED_RESPONSE);
6174 if (scts_extracted < 0)
6178 SCT_LIST_free(scts);
6179 OCSP_BASICRESP_free(br);
6180 OCSP_RESPONSE_free(rsp);
6181 return scts_extracted;
6183 /* Behave as if no OCSP response exists */
6189 * Attempts to extract SCTs from the peer certificate.
6190 * Return the number of SCTs extracted, or a negative integer if an error
6193 static int ct_extract_x509v3_extension_scts(SSL_CONNECTION *s)
6195 int scts_extracted = 0;
6196 X509 *cert = s->session != NULL ? s->session->peer : NULL;
6199 STACK_OF(SCT) *scts =
6200 X509_get_ext_d2i(cert, NID_ct_precert_scts, NULL, NULL);
6203 ct_move_scts(&s->scts, scts, SCT_SOURCE_X509V3_EXTENSION);
6205 SCT_LIST_free(scts);
6208 return scts_extracted;
6212 * Attempts to find all received SCTs by checking TLS extensions, the OCSP
6213 * response (if it exists) and X509v3 extensions in the certificate.
6214 * Returns NULL if an error occurs.
6216 const STACK_OF(SCT) *SSL_get0_peer_scts(SSL *s)
6218 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6223 if (!sc->scts_parsed) {
6224 if (ct_extract_tls_extension_scts(sc) < 0 ||
6225 ct_extract_ocsp_response_scts(sc) < 0 ||
6226 ct_extract_x509v3_extension_scts(sc) < 0)
6229 sc->scts_parsed = 1;
6236 static int ct_permissive(const CT_POLICY_EVAL_CTX *ctx,
6237 const STACK_OF(SCT) *scts, void *unused_arg)
6242 static int ct_strict(const CT_POLICY_EVAL_CTX *ctx,
6243 const STACK_OF(SCT) *scts, void *unused_arg)
6245 int count = scts != NULL ? sk_SCT_num(scts) : 0;
6248 for (i = 0; i < count; ++i) {
6249 SCT *sct = sk_SCT_value(scts, i);
6250 int status = SCT_get_validation_status(sct);
6252 if (status == SCT_VALIDATION_STATUS_VALID)
6255 ERR_raise(ERR_LIB_SSL, SSL_R_NO_VALID_SCTS);
6259 int SSL_set_ct_validation_callback(SSL *s, ssl_ct_validation_cb callback,
6262 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6268 * Since code exists that uses the custom extension handler for CT, look
6269 * for this and throw an error if they have already registered to use CT.
6271 if (callback != NULL && SSL_CTX_has_client_custom_ext(s->ctx,
6272 TLSEXT_TYPE_signed_certificate_timestamp))
6274 ERR_raise(ERR_LIB_SSL, SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED);
6278 if (callback != NULL) {
6280 * If we are validating CT, then we MUST accept SCTs served via OCSP
6282 if (!SSL_set_tlsext_status_type(s, TLSEXT_STATUSTYPE_ocsp))
6286 sc->ct_validation_callback = callback;
6287 sc->ct_validation_callback_arg = arg;
6292 int SSL_CTX_set_ct_validation_callback(SSL_CTX *ctx,
6293 ssl_ct_validation_cb callback, void *arg)
6296 * Since code exists that uses the custom extension handler for CT, look for
6297 * this and throw an error if they have already registered to use CT.
6299 if (callback != NULL && SSL_CTX_has_client_custom_ext(ctx,
6300 TLSEXT_TYPE_signed_certificate_timestamp))
6302 ERR_raise(ERR_LIB_SSL, SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED);
6306 ctx->ct_validation_callback = callback;
6307 ctx->ct_validation_callback_arg = arg;
6311 int SSL_ct_is_enabled(const SSL *s)
6313 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
6318 return sc->ct_validation_callback != NULL;
6321 int SSL_CTX_ct_is_enabled(const SSL_CTX *ctx)
6323 return ctx->ct_validation_callback != NULL;
6326 int ssl_validate_ct(SSL_CONNECTION *s)
6329 X509 *cert = s->session != NULL ? s->session->peer : NULL;
6331 SSL_DANE *dane = &s->dane;
6332 CT_POLICY_EVAL_CTX *ctx = NULL;
6333 const STACK_OF(SCT) *scts;
6336 * If no callback is set, the peer is anonymous, or its chain is invalid,
6337 * skip SCT validation - just return success. Applications that continue
6338 * handshakes without certificates, with unverified chains, or pinned leaf
6339 * certificates are outside the scope of the WebPKI and CT.
6341 * The above exclusions notwithstanding the vast majority of peers will
6342 * have rather ordinary certificate chains validated by typical
6343 * applications that perform certificate verification and therefore will
6344 * process SCTs when enabled.
6346 if (s->ct_validation_callback == NULL || cert == NULL ||
6347 s->verify_result != X509_V_OK ||
6348 s->verified_chain == NULL || sk_X509_num(s->verified_chain) <= 1)
6352 * CT not applicable for chains validated via DANE-TA(2) or DANE-EE(3)
6353 * trust-anchors. See https://tools.ietf.org/html/rfc7671#section-4.2
6355 if (DANETLS_ENABLED(dane) && dane->mtlsa != NULL) {
6356 switch (dane->mtlsa->usage) {
6357 case DANETLS_USAGE_DANE_TA:
6358 case DANETLS_USAGE_DANE_EE:
6363 ctx = CT_POLICY_EVAL_CTX_new_ex(SSL_CONNECTION_GET_CTX(s)->libctx,
6364 SSL_CONNECTION_GET_CTX(s)->propq);
6366 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CT_LIB);
6370 issuer = sk_X509_value(s->verified_chain, 1);
6371 CT_POLICY_EVAL_CTX_set1_cert(ctx, cert);
6372 CT_POLICY_EVAL_CTX_set1_issuer(ctx, issuer);
6373 CT_POLICY_EVAL_CTX_set_shared_CTLOG_STORE(ctx,
6374 SSL_CONNECTION_GET_CTX(s)->ctlog_store);
6375 CT_POLICY_EVAL_CTX_set_time(
6376 ctx, (uint64_t)SSL_SESSION_get_time(s->session) * 1000);
6378 scts = SSL_get0_peer_scts(SSL_CONNECTION_GET_SSL(s));
6381 * This function returns success (> 0) only when all the SCTs are valid, 0
6382 * when some are invalid, and < 0 on various internal errors (out of
6383 * memory, etc.). Having some, or even all, invalid SCTs is not sufficient
6384 * reason to abort the handshake, that decision is up to the callback.
6385 * Therefore, we error out only in the unexpected case that the return
6386 * value is negative.
6388 * XXX: One might well argue that the return value of this function is an
6389 * unfortunate design choice. Its job is only to determine the validation
6390 * status of each of the provided SCTs. So long as it correctly separates
6391 * the wheat from the chaff it should return success. Failure in this case
6392 * ought to correspond to an inability to carry out its duties.
6394 if (SCT_LIST_validate(scts, ctx) < 0) {
6395 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_SCT_VERIFICATION_FAILED);
6399 ret = s->ct_validation_callback(ctx, scts, s->ct_validation_callback_arg);
6401 ret = 0; /* This function returns 0 on failure */
6403 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_CALLBACK_FAILED);
6406 CT_POLICY_EVAL_CTX_free(ctx);
6408 * With SSL_VERIFY_NONE the session may be cached and re-used despite a
6409 * failure return code here. Also the application may wish the complete
6410 * the handshake, and then disconnect cleanly at a higher layer, after
6411 * checking the verification status of the completed connection.
6413 * We therefore force a certificate verification failure which will be
6414 * visible via SSL_get_verify_result() and cached as part of any resumed
6417 * Note: the permissive callback is for information gathering only, always
6418 * returns success, and does not affect verification status. Only the
6419 * strict callback or a custom application-specified callback can trigger
6420 * connection failure or record a verification error.
6423 s->verify_result = X509_V_ERR_NO_VALID_SCTS;
6427 int SSL_CTX_enable_ct(SSL_CTX *ctx, int validation_mode)
6429 switch (validation_mode) {
6431 ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_CT_VALIDATION_TYPE);
6433 case SSL_CT_VALIDATION_PERMISSIVE:
6434 return SSL_CTX_set_ct_validation_callback(ctx, ct_permissive, NULL);
6435 case SSL_CT_VALIDATION_STRICT:
6436 return SSL_CTX_set_ct_validation_callback(ctx, ct_strict, NULL);
6440 int SSL_enable_ct(SSL *s, int validation_mode)
6442 switch (validation_mode) {
6444 ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_CT_VALIDATION_TYPE);
6446 case SSL_CT_VALIDATION_PERMISSIVE:
6447 return SSL_set_ct_validation_callback(s, ct_permissive, NULL);
6448 case SSL_CT_VALIDATION_STRICT:
6449 return SSL_set_ct_validation_callback(s, ct_strict, NULL);
6453 int SSL_CTX_set_default_ctlog_list_file(SSL_CTX *ctx)
6455 return CTLOG_STORE_load_default_file(ctx->ctlog_store);
6458 int SSL_CTX_set_ctlog_list_file(SSL_CTX *ctx, const char *path)
6460 return CTLOG_STORE_load_file(ctx->ctlog_store, path);
6463 void SSL_CTX_set0_ctlog_store(SSL_CTX *ctx, CTLOG_STORE *logs)
6465 CTLOG_STORE_free(ctx->ctlog_store);
6466 ctx->ctlog_store = logs;
6469 const CTLOG_STORE *SSL_CTX_get0_ctlog_store(const SSL_CTX *ctx)
6471 return ctx->ctlog_store;
6474 #endif /* OPENSSL_NO_CT */
6476 void SSL_CTX_set_client_hello_cb(SSL_CTX *c, SSL_client_hello_cb_fn cb,
6479 c->client_hello_cb = cb;
6480 c->client_hello_cb_arg = arg;
6483 int SSL_client_hello_isv2(SSL *s)
6485 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6490 if (sc->clienthello == NULL)
6492 return sc->clienthello->isv2;
6495 unsigned int SSL_client_hello_get0_legacy_version(SSL *s)
6497 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6502 if (sc->clienthello == NULL)
6504 return sc->clienthello->legacy_version;
6507 size_t SSL_client_hello_get0_random(SSL *s, const unsigned char **out)
6509 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6514 if (sc->clienthello == NULL)
6517 *out = sc->clienthello->random;
6518 return SSL3_RANDOM_SIZE;
6521 size_t SSL_client_hello_get0_session_id(SSL *s, const unsigned char **out)
6523 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6528 if (sc->clienthello == NULL)
6531 *out = sc->clienthello->session_id;
6532 return sc->clienthello->session_id_len;
6535 size_t SSL_client_hello_get0_ciphers(SSL *s, const unsigned char **out)
6537 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6542 if (sc->clienthello == NULL)
6545 *out = PACKET_data(&sc->clienthello->ciphersuites);
6546 return PACKET_remaining(&sc->clienthello->ciphersuites);
6549 size_t SSL_client_hello_get0_compression_methods(SSL *s, const unsigned char **out)
6551 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6556 if (sc->clienthello == NULL)
6559 *out = sc->clienthello->compressions;
6560 return sc->clienthello->compressions_len;
6563 int SSL_client_hello_get1_extensions_present(SSL *s, int **out, size_t *outlen)
6568 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6573 if (sc->clienthello == NULL || out == NULL || outlen == NULL)
6575 for (i = 0; i < sc->clienthello->pre_proc_exts_len; i++) {
6576 ext = sc->clienthello->pre_proc_exts + i;
6585 if ((present = OPENSSL_malloc(sizeof(*present) * num)) == NULL)
6587 for (i = 0; i < sc->clienthello->pre_proc_exts_len; i++) {
6588 ext = sc->clienthello->pre_proc_exts + i;
6590 if (ext->received_order >= num)
6592 present[ext->received_order] = ext->type;
6599 OPENSSL_free(present);
6603 int SSL_client_hello_get_extension_order(SSL *s, uint16_t *exts, size_t *num_exts)
6607 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6612 if (sc->clienthello == NULL || num_exts == NULL)
6614 for (i = 0; i < sc->clienthello->pre_proc_exts_len; i++) {
6615 ext = sc->clienthello->pre_proc_exts + i;
6627 if (*num_exts < num)
6629 for (i = 0; i < sc->clienthello->pre_proc_exts_len; i++) {
6630 ext = sc->clienthello->pre_proc_exts + i;
6632 if (ext->received_order >= num)
6634 exts[ext->received_order] = ext->type;
6641 int SSL_client_hello_get0_ext(SSL *s, unsigned int type, const unsigned char **out,
6646 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6651 if (sc->clienthello == NULL)
6653 for (i = 0; i < sc->clienthello->pre_proc_exts_len; ++i) {
6654 r = sc->clienthello->pre_proc_exts + i;
6655 if (r->present && r->type == type) {
6657 *out = PACKET_data(&r->data);
6659 *outlen = PACKET_remaining(&r->data);
6666 int SSL_free_buffers(SSL *ssl)
6669 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
6676 return rl->rrlmethod->free_buffers(rl->rrl)
6677 && rl->wrlmethod->free_buffers(rl->wrl);
6680 int SSL_alloc_buffers(SSL *ssl)
6683 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
6688 /* QUIC always has buffers allocated. */
6694 return rl->rrlmethod->alloc_buffers(rl->rrl)
6695 && rl->wrlmethod->alloc_buffers(rl->wrl);
6698 void SSL_CTX_set_keylog_callback(SSL_CTX *ctx, SSL_CTX_keylog_cb_func cb)
6700 ctx->keylog_callback = cb;
6703 SSL_CTX_keylog_cb_func SSL_CTX_get_keylog_callback(const SSL_CTX *ctx)
6705 return ctx->keylog_callback;
6708 static int nss_keylog_int(const char *prefix,
6710 const uint8_t *parameter_1,
6711 size_t parameter_1_len,
6712 const uint8_t *parameter_2,
6713 size_t parameter_2_len)
6716 char *cursor = NULL;
6720 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(sc);
6722 if (sctx->keylog_callback == NULL)
6726 * Our output buffer will contain the following strings, rendered with
6727 * space characters in between, terminated by a NULL character: first the
6728 * prefix, then the first parameter, then the second parameter. The
6729 * meaning of each parameter depends on the specific key material being
6730 * logged. Note that the first and second parameters are encoded in
6731 * hexadecimal, so we need a buffer that is twice their lengths.
6733 prefix_len = strlen(prefix);
6734 out_len = prefix_len + (2 * parameter_1_len) + (2 * parameter_2_len) + 3;
6735 if ((out = cursor = OPENSSL_malloc(out_len)) == NULL)
6738 strcpy(cursor, prefix);
6739 cursor += prefix_len;
6742 for (i = 0; i < parameter_1_len; i++) {
6743 sprintf(cursor, "%02x", parameter_1[i]);
6748 for (i = 0; i < parameter_2_len; i++) {
6749 sprintf(cursor, "%02x", parameter_2[i]);
6754 sctx->keylog_callback(SSL_CONNECTION_GET_SSL(sc), (const char *)out);
6755 OPENSSL_clear_free(out, out_len);
6760 int ssl_log_rsa_client_key_exchange(SSL_CONNECTION *sc,
6761 const uint8_t *encrypted_premaster,
6762 size_t encrypted_premaster_len,
6763 const uint8_t *premaster,
6764 size_t premaster_len)
6766 if (encrypted_premaster_len < 8) {
6767 SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
6771 /* We only want the first 8 bytes of the encrypted premaster as a tag. */
6772 return nss_keylog_int("RSA",
6774 encrypted_premaster,
6780 int ssl_log_secret(SSL_CONNECTION *sc,
6782 const uint8_t *secret,
6785 return nss_keylog_int(label,
6787 sc->s3.client_random,
6793 #define SSLV2_CIPHER_LEN 3
6795 int ssl_cache_cipherlist(SSL_CONNECTION *s, PACKET *cipher_suites, int sslv2format)
6799 n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN;
6801 if (PACKET_remaining(cipher_suites) == 0) {
6802 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_NO_CIPHERS_SPECIFIED);
6806 if (PACKET_remaining(cipher_suites) % n != 0) {
6807 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
6811 OPENSSL_free(s->s3.tmp.ciphers_raw);
6812 s->s3.tmp.ciphers_raw = NULL;
6813 s->s3.tmp.ciphers_rawlen = 0;
6816 size_t numciphers = PACKET_remaining(cipher_suites) / n;
6817 PACKET sslv2ciphers = *cipher_suites;
6818 unsigned int leadbyte;
6822 * We store the raw ciphers list in SSLv3+ format so we need to do some
6823 * preprocessing to convert the list first. If there are any SSLv2 only
6824 * ciphersuites with a non-zero leading byte then we are going to
6825 * slightly over allocate because we won't store those. But that isn't a
6828 raw = OPENSSL_malloc(numciphers * TLS_CIPHER_LEN);
6829 s->s3.tmp.ciphers_raw = raw;
6831 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
6834 for (s->s3.tmp.ciphers_rawlen = 0;
6835 PACKET_remaining(&sslv2ciphers) > 0;
6836 raw += TLS_CIPHER_LEN) {
6837 if (!PACKET_get_1(&sslv2ciphers, &leadbyte)
6839 && !PACKET_copy_bytes(&sslv2ciphers, raw,
6842 && !PACKET_forward(&sslv2ciphers, TLS_CIPHER_LEN))) {
6843 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_PACKET);
6844 OPENSSL_free(s->s3.tmp.ciphers_raw);
6845 s->s3.tmp.ciphers_raw = NULL;
6846 s->s3.tmp.ciphers_rawlen = 0;
6850 s->s3.tmp.ciphers_rawlen += TLS_CIPHER_LEN;
6852 } else if (!PACKET_memdup(cipher_suites, &s->s3.tmp.ciphers_raw,
6853 &s->s3.tmp.ciphers_rawlen)) {
6854 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
6860 int SSL_bytes_to_cipher_list(SSL *s, const unsigned char *bytes, size_t len,
6861 int isv2format, STACK_OF(SSL_CIPHER) **sk,
6862 STACK_OF(SSL_CIPHER) **scsvs)
6865 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6870 if (!PACKET_buf_init(&pkt, bytes, len))
6872 return ossl_bytes_to_cipher_list(sc, &pkt, sk, scsvs, isv2format, 0);
6875 int ossl_bytes_to_cipher_list(SSL_CONNECTION *s, PACKET *cipher_suites,
6876 STACK_OF(SSL_CIPHER) **skp,
6877 STACK_OF(SSL_CIPHER) **scsvs_out,
6878 int sslv2format, int fatal)
6880 const SSL_CIPHER *c;
6881 STACK_OF(SSL_CIPHER) *sk = NULL;
6882 STACK_OF(SSL_CIPHER) *scsvs = NULL;
6884 /* 3 = SSLV2_CIPHER_LEN > TLS_CIPHER_LEN = 2. */
6885 unsigned char cipher[SSLV2_CIPHER_LEN];
6887 n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN;
6889 if (PACKET_remaining(cipher_suites) == 0) {
6891 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_NO_CIPHERS_SPECIFIED);
6893 ERR_raise(ERR_LIB_SSL, SSL_R_NO_CIPHERS_SPECIFIED);
6897 if (PACKET_remaining(cipher_suites) % n != 0) {
6899 SSLfatal(s, SSL_AD_DECODE_ERROR,
6900 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
6902 ERR_raise(ERR_LIB_SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
6906 sk = sk_SSL_CIPHER_new_null();
6907 scsvs = sk_SSL_CIPHER_new_null();
6908 if (sk == NULL || scsvs == NULL) {
6910 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
6912 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
6916 while (PACKET_copy_bytes(cipher_suites, cipher, n)) {
6918 * SSLv3 ciphers wrapped in an SSLv2-compatible ClientHello have the
6919 * first byte set to zero, while true SSLv2 ciphers have a non-zero
6920 * first byte. We don't support any true SSLv2 ciphers, so skip them.
6922 if (sslv2format && cipher[0] != '\0')
6925 /* For SSLv2-compat, ignore leading 0-byte. */
6926 c = ssl_get_cipher_by_char(s, sslv2format ? &cipher[1] : cipher, 1);
6928 if ((c->valid && !sk_SSL_CIPHER_push(sk, c)) ||
6929 (!c->valid && !sk_SSL_CIPHER_push(scsvs, c))) {
6931 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
6933 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
6938 if (PACKET_remaining(cipher_suites) > 0) {
6940 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
6942 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);
6949 sk_SSL_CIPHER_free(sk);
6950 if (scsvs_out != NULL)
6953 sk_SSL_CIPHER_free(scsvs);
6956 sk_SSL_CIPHER_free(sk);
6957 sk_SSL_CIPHER_free(scsvs);
6961 int SSL_CTX_set_max_early_data(SSL_CTX *ctx, uint32_t max_early_data)
6963 ctx->max_early_data = max_early_data;
6968 uint32_t SSL_CTX_get_max_early_data(const SSL_CTX *ctx)
6970 return ctx->max_early_data;
6973 int SSL_set_max_early_data(SSL *s, uint32_t max_early_data)
6975 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
6980 sc->max_early_data = max_early_data;
6985 uint32_t SSL_get_max_early_data(const SSL *s)
6987 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
6992 return sc->max_early_data;
6995 int SSL_CTX_set_recv_max_early_data(SSL_CTX *ctx, uint32_t recv_max_early_data)
6997 ctx->recv_max_early_data = recv_max_early_data;
7002 uint32_t SSL_CTX_get_recv_max_early_data(const SSL_CTX *ctx)
7004 return ctx->recv_max_early_data;
7007 int SSL_set_recv_max_early_data(SSL *s, uint32_t recv_max_early_data)
7009 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
7014 sc->recv_max_early_data = recv_max_early_data;
7019 uint32_t SSL_get_recv_max_early_data(const SSL *s)
7021 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
7026 return sc->recv_max_early_data;
7029 __owur unsigned int ssl_get_max_send_fragment(const SSL_CONNECTION *sc)
7031 /* Return any active Max Fragment Len extension */
7032 if (sc->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(sc->session))
7033 return GET_MAX_FRAGMENT_LENGTH(sc->session);
7035 /* return current SSL connection setting */
7036 return sc->max_send_fragment;
7039 __owur unsigned int ssl_get_split_send_fragment(const SSL_CONNECTION *sc)
7041 /* Return a value regarding an active Max Fragment Len extension */
7042 if (sc->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(sc->session)
7043 && sc->split_send_fragment > GET_MAX_FRAGMENT_LENGTH(sc->session))
7044 return GET_MAX_FRAGMENT_LENGTH(sc->session);
7046 /* else limit |split_send_fragment| to current |max_send_fragment| */
7047 if (sc->split_send_fragment > sc->max_send_fragment)
7048 return sc->max_send_fragment;
7050 /* return current SSL connection setting */
7051 return sc->split_send_fragment;
7054 int SSL_stateless(SSL *s)
7057 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
7062 /* Ensure there is no state left over from a previous invocation */
7068 sc->s3.flags |= TLS1_FLAGS_STATELESS;
7069 ret = SSL_accept(s);
7070 sc->s3.flags &= ~TLS1_FLAGS_STATELESS;
7072 if (ret > 0 && sc->ext.cookieok)
7075 if (sc->hello_retry_request == SSL_HRR_PENDING && !ossl_statem_in_error(sc))
7081 void SSL_CTX_set_post_handshake_auth(SSL_CTX *ctx, int val)
7083 ctx->pha_enabled = val;
7086 void SSL_set_post_handshake_auth(SSL *ssl, int val)
7088 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
7093 sc->pha_enabled = val;
7096 int SSL_verify_client_post_handshake(SSL *ssl)
7098 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
7100 #ifndef OPENSSL_NO_QUIC
7102 ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION);
7110 if (!SSL_CONNECTION_IS_TLS13(sc)) {
7111 ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION);
7115 ERR_raise(ERR_LIB_SSL, SSL_R_NOT_SERVER);
7119 if (!SSL_is_init_finished(ssl)) {
7120 ERR_raise(ERR_LIB_SSL, SSL_R_STILL_IN_INIT);
7124 switch (sc->post_handshake_auth) {
7126 ERR_raise(ERR_LIB_SSL, SSL_R_EXTENSION_NOT_RECEIVED);
7129 case SSL_PHA_EXT_SENT:
7130 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
7132 case SSL_PHA_EXT_RECEIVED:
7134 case SSL_PHA_REQUEST_PENDING:
7135 ERR_raise(ERR_LIB_SSL, SSL_R_REQUEST_PENDING);
7137 case SSL_PHA_REQUESTED:
7138 ERR_raise(ERR_LIB_SSL, SSL_R_REQUEST_SENT);
7142 sc->post_handshake_auth = SSL_PHA_REQUEST_PENDING;
7144 /* checks verify_mode and algorithm_auth */
7145 if (!send_certificate_request(sc)) {
7146 sc->post_handshake_auth = SSL_PHA_EXT_RECEIVED; /* restore on error */
7147 ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_CONFIG);
7151 ossl_statem_set_in_init(sc, 1);
7155 int SSL_CTX_set_session_ticket_cb(SSL_CTX *ctx,
7156 SSL_CTX_generate_session_ticket_fn gen_cb,
7157 SSL_CTX_decrypt_session_ticket_fn dec_cb,
7160 ctx->generate_ticket_cb = gen_cb;
7161 ctx->decrypt_ticket_cb = dec_cb;
7162 ctx->ticket_cb_data = arg;
7166 void SSL_CTX_set_allow_early_data_cb(SSL_CTX *ctx,
7167 SSL_allow_early_data_cb_fn cb,
7170 ctx->allow_early_data_cb = cb;
7171 ctx->allow_early_data_cb_data = arg;
7174 void SSL_set_allow_early_data_cb(SSL *s,
7175 SSL_allow_early_data_cb_fn cb,
7178 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
7183 sc->allow_early_data_cb = cb;
7184 sc->allow_early_data_cb_data = arg;
7187 const EVP_CIPHER *ssl_evp_cipher_fetch(OSSL_LIB_CTX *libctx,
7189 const char *properties)
7191 const EVP_CIPHER *ciph;
7193 ciph = tls_get_cipher_from_engine(nid);
7198 * If there is no engine cipher then we do an explicit fetch. This may fail
7199 * and that could be ok
7202 ciph = EVP_CIPHER_fetch(libctx, OBJ_nid2sn(nid), properties);
7208 int ssl_evp_cipher_up_ref(const EVP_CIPHER *cipher)
7210 /* Don't up-ref an implicit EVP_CIPHER */
7211 if (EVP_CIPHER_get0_provider(cipher) == NULL)
7215 * The cipher was explicitly fetched and therefore it is safe to cast
7218 return EVP_CIPHER_up_ref((EVP_CIPHER *)cipher);
7221 void ssl_evp_cipher_free(const EVP_CIPHER *cipher)
7226 if (EVP_CIPHER_get0_provider(cipher) != NULL) {
7228 * The cipher was explicitly fetched and therefore it is safe to cast
7231 EVP_CIPHER_free((EVP_CIPHER *)cipher);
7235 const EVP_MD *ssl_evp_md_fetch(OSSL_LIB_CTX *libctx,
7237 const char *properties)
7241 md = tls_get_digest_from_engine(nid);
7245 /* Otherwise we do an explicit fetch */
7247 md = EVP_MD_fetch(libctx, OBJ_nid2sn(nid), properties);
7252 int ssl_evp_md_up_ref(const EVP_MD *md)
7254 /* Don't up-ref an implicit EVP_MD */
7255 if (EVP_MD_get0_provider(md) == NULL)
7259 * The digest was explicitly fetched and therefore it is safe to cast
7262 return EVP_MD_up_ref((EVP_MD *)md);
7265 void ssl_evp_md_free(const EVP_MD *md)
7270 if (EVP_MD_get0_provider(md) != NULL) {
7272 * The digest was explicitly fetched and therefore it is safe to cast
7275 EVP_MD_free((EVP_MD *)md);
7279 int SSL_set0_tmp_dh_pkey(SSL *s, EVP_PKEY *dhpkey)
7281 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
7286 if (!ssl_security(sc, SSL_SECOP_TMP_DH,
7287 EVP_PKEY_get_security_bits(dhpkey), 0, dhpkey)) {
7288 ERR_raise(ERR_LIB_SSL, SSL_R_DH_KEY_TOO_SMALL);
7291 EVP_PKEY_free(sc->cert->dh_tmp);
7292 sc->cert->dh_tmp = dhpkey;
7296 int SSL_CTX_set0_tmp_dh_pkey(SSL_CTX *ctx, EVP_PKEY *dhpkey)
7298 if (!ssl_ctx_security(ctx, SSL_SECOP_TMP_DH,
7299 EVP_PKEY_get_security_bits(dhpkey), 0, dhpkey)) {
7300 ERR_raise(ERR_LIB_SSL, SSL_R_DH_KEY_TOO_SMALL);
7303 EVP_PKEY_free(ctx->cert->dh_tmp);
7304 ctx->cert->dh_tmp = dhpkey;
7308 /* QUIC-specific methods which are supported on QUIC connections only. */
7309 int SSL_handle_events(SSL *s)
7313 #ifndef OPENSSL_NO_QUIC
7315 return ossl_quic_handle_events(s);
7318 sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
7319 if (sc != NULL && SSL_CONNECTION_IS_DTLS(sc))
7321 * DTLSv1_handle_timeout returns 0 if the timer wasn't expired yet,
7322 * which we consider a success case. Theoretically DTLSv1_handle_timeout
7323 * can also return 0 if s is NULL or not a DTLS object, but we've
7324 * already ruled out those possibilities above, so this is not possible
7325 * here. Thus the only failure cases are where DTLSv1_handle_timeout
7328 return DTLSv1_handle_timeout(s) >= 0;
7333 int SSL_get_event_timeout(SSL *s, struct timeval *tv, int *is_infinite)
7337 #ifndef OPENSSL_NO_QUIC
7339 return ossl_quic_get_event_timeout(s, tv, is_infinite);
7342 sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
7343 if (sc != NULL && SSL_CONNECTION_IS_DTLS(sc)
7344 && DTLSv1_get_timeout(s, tv)) {
7349 tv->tv_sec = 1000000;
7355 int SSL_get_rpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
7357 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
7359 #ifndef OPENSSL_NO_QUIC
7361 return ossl_quic_get_rpoll_descriptor(s, desc);
7364 if (sc == NULL || sc->rbio == NULL)
7367 return BIO_get_rpoll_descriptor(sc->rbio, desc);
7370 int SSL_get_wpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
7372 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
7374 #ifndef OPENSSL_NO_QUIC
7376 return ossl_quic_get_wpoll_descriptor(s, desc);
7379 if (sc == NULL || sc->wbio == NULL)
7382 return BIO_get_wpoll_descriptor(sc->wbio, desc);
7385 int SSL_net_read_desired(SSL *s)
7387 #ifndef OPENSSL_NO_QUIC
7389 return SSL_want_read(s);
7391 return ossl_quic_get_net_read_desired(s);
7393 return SSL_want_read(s);
7397 int SSL_net_write_desired(SSL *s)
7399 #ifndef OPENSSL_NO_QUIC
7401 return SSL_want_write(s);
7403 return ossl_quic_get_net_write_desired(s);
7405 return SSL_want_write(s);
7409 int SSL_set_blocking_mode(SSL *s, int blocking)
7411 #ifndef OPENSSL_NO_QUIC
7415 return ossl_quic_conn_set_blocking_mode(s, blocking);
7421 int SSL_get_blocking_mode(SSL *s)
7423 #ifndef OPENSSL_NO_QUIC
7427 return ossl_quic_conn_get_blocking_mode(s);
7433 int SSL_set1_initial_peer_addr(SSL *s, const BIO_ADDR *peer_addr)
7435 #ifndef OPENSSL_NO_QUIC
7439 return ossl_quic_conn_set_initial_peer_addr(s, peer_addr);
7445 int SSL_shutdown_ex(SSL *ssl, uint64_t flags,
7446 const SSL_SHUTDOWN_EX_ARGS *args,
7449 #ifndef OPENSSL_NO_QUIC
7451 return SSL_shutdown(ssl);
7453 return ossl_quic_conn_shutdown(ssl, flags, args, args_len);
7455 return SSL_shutdown(ssl);
7459 int SSL_stream_conclude(SSL *ssl, uint64_t flags)
7461 #ifndef OPENSSL_NO_QUIC
7465 return ossl_quic_conn_stream_conclude(ssl);
7471 SSL *SSL_new_stream(SSL *s, uint64_t flags)
7473 #ifndef OPENSSL_NO_QUIC
7477 return ossl_quic_conn_stream_new(s, flags);
7483 SSL *SSL_get0_connection(SSL *s)
7485 #ifndef OPENSSL_NO_QUIC
7489 return ossl_quic_get0_connection(s);
7495 int SSL_is_connection(SSL *s)
7497 return SSL_get0_connection(s) == s;
7500 int SSL_get_stream_type(SSL *s)
7502 #ifndef OPENSSL_NO_QUIC
7504 return SSL_STREAM_TYPE_BIDI;
7506 return ossl_quic_get_stream_type(s);
7508 return SSL_STREAM_TYPE_BIDI;
7512 uint64_t SSL_get_stream_id(SSL *s)
7514 #ifndef OPENSSL_NO_QUIC
7518 return ossl_quic_get_stream_id(s);
7524 int SSL_is_stream_local(SSL *s)
7526 #ifndef OPENSSL_NO_QUIC
7530 return ossl_quic_is_stream_local(s);
7536 int SSL_set_default_stream_mode(SSL *s, uint32_t mode)
7538 #ifndef OPENSSL_NO_QUIC
7542 return ossl_quic_set_default_stream_mode(s, mode);
7548 int SSL_set_incoming_stream_policy(SSL *s, int policy, uint64_t aec)
7550 #ifndef OPENSSL_NO_QUIC
7554 return ossl_quic_set_incoming_stream_policy(s, policy, aec);
7560 SSL *SSL_accept_stream(SSL *s, uint64_t flags)
7562 #ifndef OPENSSL_NO_QUIC
7566 return ossl_quic_accept_stream(s, flags);
7572 size_t SSL_get_accept_stream_queue_len(SSL *s)
7574 #ifndef OPENSSL_NO_QUIC
7578 return ossl_quic_get_accept_stream_queue_len(s);
7584 int SSL_stream_reset(SSL *s,
7585 const SSL_STREAM_RESET_ARGS *args,
7588 #ifndef OPENSSL_NO_QUIC
7592 return ossl_quic_stream_reset(s, args, args_len);
7598 int SSL_get_stream_read_state(SSL *s)
7600 #ifndef OPENSSL_NO_QUIC
7602 return SSL_STREAM_STATE_NONE;
7604 return ossl_quic_get_stream_read_state(s);
7606 return SSL_STREAM_STATE_NONE;
7610 int SSL_get_stream_write_state(SSL *s)
7612 #ifndef OPENSSL_NO_QUIC
7614 return SSL_STREAM_STATE_NONE;
7616 return ossl_quic_get_stream_write_state(s);
7618 return SSL_STREAM_STATE_NONE;
7622 int SSL_get_stream_read_error_code(SSL *s, uint64_t *app_error_code)
7624 #ifndef OPENSSL_NO_QUIC
7628 return ossl_quic_get_stream_read_error_code(s, app_error_code);
7634 int SSL_get_stream_write_error_code(SSL *s, uint64_t *app_error_code)
7636 #ifndef OPENSSL_NO_QUIC
7640 return ossl_quic_get_stream_write_error_code(s, app_error_code);
7646 int SSL_get_conn_close_info(SSL *s, SSL_CONN_CLOSE_INFO *info,
7649 #ifndef OPENSSL_NO_QUIC
7653 return ossl_quic_get_conn_close_info(s, info, info_len);
7659 int SSL_add_expected_rpk(SSL *s, EVP_PKEY *rpk)
7661 unsigned char *data = NULL;
7662 SSL_DANE *dane = SSL_get0_dane(s);
7665 if (dane == NULL || dane->dctx == NULL)
7667 if ((ret = i2d_PUBKEY(rpk, &data)) <= 0)
7670 ret = SSL_dane_tlsa_add(s, DANETLS_USAGE_DANE_EE,
7671 DANETLS_SELECTOR_SPKI,
7672 DANETLS_MATCHING_FULL,
7673 data, (size_t)ret) > 0;
7678 EVP_PKEY *SSL_get0_peer_rpk(const SSL *s)
7680 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
7682 if (sc == NULL || sc->session == NULL)
7684 return sc->session->peer_rpk;
7687 int SSL_get_negotiated_client_cert_type(const SSL *s)
7689 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
7694 return sc->ext.client_cert_type;
7697 int SSL_get_negotiated_server_cert_type(const SSL *s)
7699 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
7704 return sc->ext.server_cert_type;
7707 static int validate_cert_type(const unsigned char *val, size_t len)
7713 if (val == NULL && len == 0)
7716 if (val == NULL || len == 0)
7719 for (i = 0; i < len; i++) {
7721 case TLSEXT_cert_type_rpk:
7726 case TLSEXT_cert_type_x509:
7731 case TLSEXT_cert_type_pgp:
7732 case TLSEXT_cert_type_1609dot2:
7740 static int set_cert_type(unsigned char **cert_type,
7741 size_t *cert_type_len,
7742 const unsigned char *val,
7745 unsigned char *tmp = NULL;
7747 if (!validate_cert_type(val, len))
7750 if (val != NULL && (tmp = OPENSSL_memdup(val, len)) == NULL)
7753 OPENSSL_free(*cert_type);
7755 *cert_type_len = len;
7759 int SSL_set1_client_cert_type(SSL *s, const unsigned char *val, size_t len)
7761 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
7763 return set_cert_type(&sc->client_cert_type, &sc->client_cert_type_len,
7767 int SSL_set1_server_cert_type(SSL *s, const unsigned char *val, size_t len)
7769 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
7771 return set_cert_type(&sc->server_cert_type, &sc->server_cert_type_len,
7775 int SSL_CTX_set1_client_cert_type(SSL_CTX *ctx, const unsigned char *val, size_t len)
7777 return set_cert_type(&ctx->client_cert_type, &ctx->client_cert_type_len,
7781 int SSL_CTX_set1_server_cert_type(SSL_CTX *ctx, const unsigned char *val, size_t len)
7783 return set_cert_type(&ctx->server_cert_type, &ctx->server_cert_type_len,
7787 int SSL_get0_client_cert_type(const SSL *s, unsigned char **t, size_t *len)
7789 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
7791 if (t == NULL || len == NULL)
7794 *t = sc->client_cert_type;
7795 *len = sc->client_cert_type_len;
7799 int SSL_get0_server_cert_type(const SSL *s, unsigned char **t, size_t *len)
7801 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
7803 if (t == NULL || len == NULL)
7806 *t = sc->server_cert_type;
7807 *len = sc->server_cert_type_len;
7811 int SSL_CTX_get0_client_cert_type(const SSL_CTX *ctx, unsigned char **t, size_t *len)
7813 if (t == NULL || len == NULL)
7816 *t = ctx->client_cert_type;
7817 *len = ctx->client_cert_type_len;
7821 int SSL_CTX_get0_server_cert_type(const SSL_CTX *ctx, unsigned char **t, size_t *len)
7823 if (t == NULL || len == NULL)
7826 *t = ctx->server_cert_type;
7827 *len = ctx->server_cert_type_len;