]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/ssl_lib.c
Fix unconstrained session cache growth in TLSv1.3
[thirdparty/openssl.git] / ssl / ssl_lib.c
CommitLineData
0f113f3e 1/*
3c95ef22 2 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
aa8f3d76 3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
c80149d9 4 * Copyright 2005 Nokia. All rights reserved.
bf21446a 5 *
2c18d164 6 * Licensed under the Apache License 2.0 (the "License"). You may not use
846e33c7
RS
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
bf21446a 10 */
846e33c7 11
d02b48c6 12#include <stdio.h>
706457b7 13#include "ssl_local.h"
d5f9166b 14#include "internal/e_os.h"
ec577822 15#include <openssl/objects.h>
bb7cd4e3 16#include <openssl/x509v3.h>
6434abbf 17#include <openssl/rand.h>
67c8e7f4 18#include <openssl/ocsp.h>
3c27208f
RS
19#include <openssl/dh.h>
20#include <openssl/engine.h>
07bbc92c 21#include <openssl/async.h>
3c27208f 22#include <openssl/ct.h>
77359d22 23#include <openssl/trace.h>
4566dae7 24#include <openssl/core_names.h>
67dc995e 25#include "internal/cryptlib.h"
f2a6f838 26#include "internal/nelem.h"
cd420b0b 27#include "internal/refcount.h"
50ec7505 28#include "internal/ktls.h"
03bacce8 29#include "quic/quic_local.h"
0f113f3e 30
38b051a1 31static int ssl_undefined_function_3(SSL_CONNECTION *sc, unsigned char *r,
fce78bd4
BE
32 unsigned char *s, size_t t, size_t *u)
33{
38b051a1 34 return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc));
fce78bd4
BE
35}
36
38b051a1 37static int ssl_undefined_function_4(SSL_CONNECTION *sc, int r)
fce78bd4 38{
38b051a1 39 return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc));
fce78bd4
BE
40}
41
38b051a1
TM
42static size_t ssl_undefined_function_5(SSL_CONNECTION *sc, const char *r,
43 size_t s, unsigned char *t)
fce78bd4 44{
38b051a1 45 return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc));
fce78bd4
BE
46}
47
48static int ssl_undefined_function_6(int r)
49{
fce78bd4
BE
50 return ssl_undefined_function(NULL);
51}
52
38b051a1
TM
53static int ssl_undefined_function_7(SSL_CONNECTION *sc, unsigned char *r,
54 size_t s, const char *t, size_t u,
fce78bd4
BE
55 const unsigned char *v, size_t w, int x)
56{
38b051a1
TM
57 return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc));
58}
59
60static int ssl_undefined_function_8(SSL_CONNECTION *sc)
61{
62 return ssl_undefined_function(SSL_CONNECTION_GET_SSL(sc));
fce78bd4
BE
63}
64
89dd87e1 65const SSL3_ENC_METHOD ssl3_undef_enc_method = {
38b051a1 66 ssl_undefined_function_8,
fce78bd4
BE
67 ssl_undefined_function_3,
68 ssl_undefined_function_4,
69 ssl_undefined_function_5,
0f113f3e
MC
70 NULL, /* client_finished_label */
71 0, /* client_finished_label_len */
72 NULL, /* server_finished_label */
73 0, /* server_finished_label_len */
fce78bd4
BE
74 ssl_undefined_function_6,
75 ssl_undefined_function_7,
0f113f3e 76};
d02b48c6 77
07bbc92c
MC
78struct ssl_async_args {
79 SSL *s;
80 void *buf;
348240c6 81 size_t num;
a230b26e 82 enum { READFUNC, WRITEFUNC, OTHERFUNC } type;
add2f5ca 83 union {
eda75751 84 int (*func_read) (SSL *, void *, size_t, size_t *);
7ee8627f 85 int (*func_write) (SSL *, const void *, size_t, size_t *);
a230b26e 86 int (*func_other) (SSL *);
add2f5ca 87 } f;
07bbc92c
MC
88};
89
919ba009
VD
90static const struct {
91 uint8_t mtype;
92 uint8_t ord;
a230b26e 93 int nid;
919ba009 94} dane_mds[] = {
a230b26e
EK
95 {
96 DANETLS_MATCHING_FULL, 0, NID_undef
97 },
98 {
99 DANETLS_MATCHING_2256, 1, NID_sha256
100 },
101 {
102 DANETLS_MATCHING_2512, 2, NID_sha512
103 },
919ba009
VD
104};
105
106static int dane_ctx_enable(struct dane_ctx_st *dctx)
107{
108 const EVP_MD **mdevp;
109 uint8_t *mdord;
110 uint8_t mdmax = DANETLS_MATCHING_LAST;
a230b26e 111 int n = ((int)mdmax) + 1; /* int to handle PrivMatch(255) */
919ba009
VD
112 size_t i;
113
5ae4ceb9
VD
114 if (dctx->mdevp != NULL)
115 return 1;
116
919ba009
VD
117 mdevp = OPENSSL_zalloc(n * sizeof(*mdevp));
118 mdord = OPENSSL_zalloc(n * sizeof(*mdord));
119
120 if (mdord == NULL || mdevp == NULL) {
b3bd3d5a 121 OPENSSL_free(mdord);
919ba009 122 OPENSSL_free(mdevp);
919ba009
VD
123 return 0;
124 }
125
126 /* Install default entries */
127 for (i = 0; i < OSSL_NELEM(dane_mds); ++i) {
128 const EVP_MD *md;
129
130 if (dane_mds[i].nid == NID_undef ||
131 (md = EVP_get_digestbynid(dane_mds[i].nid)) == NULL)
132 continue;
133 mdevp[dane_mds[i].mtype] = md;
134 mdord[dane_mds[i].mtype] = dane_mds[i].ord;
135 }
136
137 dctx->mdevp = mdevp;
138 dctx->mdord = mdord;
139 dctx->mdmax = mdmax;
140
141 return 1;
142}
143
144static void dane_ctx_final(struct dane_ctx_st *dctx)
145{
146 OPENSSL_free(dctx->mdevp);
147 dctx->mdevp = NULL;
148
149 OPENSSL_free(dctx->mdord);
150 dctx->mdord = NULL;
151 dctx->mdmax = 0;
152}
153
154static void tlsa_free(danetls_record *t)
155{
156 if (t == NULL)
157 return;
158 OPENSSL_free(t->data);
159 EVP_PKEY_free(t->spki);
160 OPENSSL_free(t);
161}
162
b9aec69a 163static void dane_final(SSL_DANE *dane)
919ba009
VD
164{
165 sk_danetls_record_pop_free(dane->trecs, tlsa_free);
166 dane->trecs = NULL;
167
79b2a2f2 168 OSSL_STACK_OF_X509_free(dane->certs);
919ba009
VD
169 dane->certs = NULL;
170
171 X509_free(dane->mcert);
172 dane->mcert = NULL;
173 dane->mtlsa = NULL;
174 dane->mdpth = -1;
175 dane->pdpth = -1;
176}
177
178/*
179 * dane_copy - Copy dane configuration, sans verification state.
180 */
38b051a1 181static int ssl_dane_dup(SSL_CONNECTION *to, SSL_CONNECTION *from)
919ba009
VD
182{
183 int num;
184 int i;
185
186 if (!DANETLS_ENABLED(&from->dane))
187 return 1;
188
e431363f 189 num = sk_danetls_record_num(from->dane.trecs);
919ba009 190 dane_final(&to->dane);
5ae4ceb9 191 to->dane.flags = from->dane.flags;
38b051a1 192 to->dane.dctx = &SSL_CONNECTION_GET_CTX(to)->dane;
7a908204 193 to->dane.trecs = sk_danetls_record_new_reserve(NULL, num);
9f6b22b8
VD
194
195 if (to->dane.trecs == NULL) {
e077455e 196 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
9f6b22b8
VD
197 return 0;
198 }
919ba009 199
919ba009
VD
200 for (i = 0; i < num; ++i) {
201 danetls_record *t = sk_danetls_record_value(from->dane.trecs, i);
9f6b22b8 202
38b051a1
TM
203 if (SSL_dane_tlsa_add(SSL_CONNECTION_GET_SSL(to), t->usage,
204 t->selector, t->mtype, t->data, t->dlen) <= 0)
919ba009
VD
205 return 0;
206 }
207 return 1;
208}
209
a230b26e
EK
210static int dane_mtype_set(struct dane_ctx_st *dctx,
211 const EVP_MD *md, uint8_t mtype, uint8_t ord)
919ba009
VD
212{
213 int i;
214
215 if (mtype == DANETLS_MATCHING_FULL && md != NULL) {
6849b73c 216 ERR_raise(ERR_LIB_SSL, SSL_R_DANE_CANNOT_OVERRIDE_MTYPE_FULL);
919ba009
VD
217 return 0;
218 }
219
220 if (mtype > dctx->mdmax) {
221 const EVP_MD **mdevp;
222 uint8_t *mdord;
a230b26e 223 int n = ((int)mtype) + 1;
919ba009
VD
224
225 mdevp = OPENSSL_realloc(dctx->mdevp, n * sizeof(*mdevp));
e077455e 226 if (mdevp == NULL)
919ba009 227 return -1;
919ba009
VD
228 dctx->mdevp = mdevp;
229
230 mdord = OPENSSL_realloc(dctx->mdord, n * sizeof(*mdord));
e077455e 231 if (mdord == NULL)
919ba009 232 return -1;
919ba009
VD
233 dctx->mdord = mdord;
234
235 /* Zero-fill any gaps */
a230b26e 236 for (i = dctx->mdmax + 1; i < mtype; ++i) {
919ba009
VD
237 mdevp[i] = NULL;
238 mdord[i] = 0;
239 }
240
241 dctx->mdmax = mtype;
242 }
243
244 dctx->mdevp[mtype] = md;
245 /* Coerce ordinal of disabled matching types to 0 */
246 dctx->mdord[mtype] = (md == NULL) ? 0 : ord;
247
248 return 1;
249}
250
b9aec69a 251static const EVP_MD *tlsa_md_get(SSL_DANE *dane, uint8_t mtype)
919ba009
VD
252{
253 if (mtype > dane->dctx->mdmax)
254 return NULL;
255 return dane->dctx->mdevp[mtype];
256}
257
a230b26e
EK
258static int dane_tlsa_add(SSL_DANE *dane,
259 uint8_t usage,
260 uint8_t selector,
6d4313f0 261 uint8_t mtype, const unsigned char *data, size_t dlen)
919ba009
VD
262{
263 danetls_record *t;
264 const EVP_MD *md = NULL;
265 int ilen = (int)dlen;
266 int i;
9f6b22b8 267 int num;
165797c7 268 int mdsize;
919ba009
VD
269
270 if (dane->trecs == NULL) {
6849b73c 271 ERR_raise(ERR_LIB_SSL, SSL_R_DANE_NOT_ENABLED);
919ba009
VD
272 return -1;
273 }
274
275 if (ilen < 0 || dlen != (size_t)ilen) {
6849b73c 276 ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_DATA_LENGTH);
919ba009
VD
277 return 0;
278 }
279
280 if (usage > DANETLS_USAGE_LAST) {
6849b73c 281 ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_CERTIFICATE_USAGE);
919ba009
VD
282 return 0;
283 }
284
285 if (selector > DANETLS_SELECTOR_LAST) {
6849b73c 286 ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_SELECTOR);
919ba009
VD
287 return 0;
288 }
289
290 if (mtype != DANETLS_MATCHING_FULL) {
291 md = tlsa_md_get(dane, mtype);
292 if (md == NULL) {
6849b73c 293 ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_MATCHING_TYPE);
919ba009
VD
294 return 0;
295 }
296 }
297
165797c7
JJ
298 if (md != NULL) {
299 mdsize = EVP_MD_get_size(md);
e53a7ccd 300 if (mdsize <= 0 || dlen != (size_t)mdsize) {
165797c7
JJ
301 ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_DIGEST_LENGTH);
302 return 0;
303 }
919ba009
VD
304 }
305 if (!data) {
6849b73c 306 ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_NULL_DATA);
919ba009
VD
307 return 0;
308 }
309
e077455e 310 if ((t = OPENSSL_zalloc(sizeof(*t))) == NULL)
919ba009 311 return -1;
919ba009
VD
312
313 t->usage = usage;
314 t->selector = selector;
315 t->mtype = mtype;
348240c6 316 t->data = OPENSSL_malloc(dlen);
919ba009
VD
317 if (t->data == NULL) {
318 tlsa_free(t);
919ba009
VD
319 return -1;
320 }
348240c6
MC
321 memcpy(t->data, data, dlen);
322 t->dlen = dlen;
919ba009
VD
323
324 /* Validate and cache full certificate or public key */
325 if (mtype == DANETLS_MATCHING_FULL) {
326 const unsigned char *p = data;
327 X509 *cert = NULL;
328 EVP_PKEY *pkey = NULL;
329
330 switch (selector) {
331 case DANETLS_SELECTOR_CERT:
348240c6 332 if (!d2i_X509(&cert, &p, ilen) || p < data ||
919ba009 333 dlen != (size_t)(p - data)) {
e4a94bcc 334 X509_free(cert);
919ba009 335 tlsa_free(t);
6849b73c 336 ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_CERTIFICATE);
919ba009
VD
337 return 0;
338 }
339 if (X509_get0_pubkey(cert) == NULL) {
e4a94bcc 340 X509_free(cert);
919ba009 341 tlsa_free(t);
6849b73c 342 ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_CERTIFICATE);
919ba009
VD
343 return 0;
344 }
345
346 if ((DANETLS_USAGE_BIT(usage) & DANETLS_TA_MASK) == 0) {
f636e7e6
VD
347 /*
348 * The Full(0) certificate decodes to a seemingly valid X.509
349 * object with a plausible key, so the TLSA record is well
164a541b 350 * formed. However, we don't actually need the certificate for
f636e7e6
VD
351 * usages PKIX-EE(1) or DANE-EE(3), because at least the EE
352 * certificate is always presented by the peer. We discard the
353 * certificate, and just use the TLSA data as an opaque blob
354 * for matching the raw presented DER octets.
355 *
356 * DO NOT FREE `t` here, it will be added to the TLSA record
357 * list below!
358 */
919ba009
VD
359 X509_free(cert);
360 break;
361 }
362
363 /*
364 * For usage DANE-TA(2), we support authentication via "2 0 0" TLSA
365 * records that contain full certificates of trust-anchors that are
366 * not present in the wire chain. For usage PKIX-TA(0), we augment
367 * the chain with untrusted Full(0) certificates from DNS, in case
368 * they are missing from the chain.
369 */
370 if ((dane->certs == NULL &&
371 (dane->certs = sk_X509_new_null()) == NULL) ||
372 !sk_X509_push(dane->certs, cert)) {
e077455e 373 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
919ba009
VD
374 X509_free(cert);
375 tlsa_free(t);
376 return -1;
377 }
378 break;
379
380 case DANETLS_SELECTOR_SPKI:
348240c6 381 if (!d2i_PUBKEY(&pkey, &p, ilen) || p < data ||
919ba009 382 dlen != (size_t)(p - data)) {
e4a94bcc 383 EVP_PKEY_free(pkey);
919ba009 384 tlsa_free(t);
6849b73c 385 ERR_raise(ERR_LIB_SSL, SSL_R_DANE_TLSA_BAD_PUBLIC_KEY);
919ba009
VD
386 return 0;
387 }
388
389 /*
390 * For usage DANE-TA(2), we support authentication via "2 1 0" TLSA
391 * records that contain full bare keys of trust-anchors that are
392 * not present in the wire chain.
393 */
394 if (usage == DANETLS_USAGE_DANE_TA)
395 t->spki = pkey;
396 else
397 EVP_PKEY_free(pkey);
398 break;
399 }
400 }
401
402 /*-
403 * Find the right insertion point for the new record.
404 *
405 * See crypto/x509/x509_vfy.c. We sort DANE-EE(3) records first, so that
406 * they can be processed first, as they require no chain building, and no
407 * expiration or hostname checks. Because DANE-EE(3) is numerically
408 * largest, this is accomplished via descending sort by "usage".
409 *
410 * We also sort in descending order by matching ordinal to simplify
411 * the implementation of digest agility in the verification code.
412 *
413 * The choice of order for the selector is not significant, so we
414 * use the same descending order for consistency.
415 */
9f6b22b8
VD
416 num = sk_danetls_record_num(dane->trecs);
417 for (i = 0; i < num; ++i) {
919ba009 418 danetls_record *rec = sk_danetls_record_value(dane->trecs, i);
9f6b22b8 419
919ba009
VD
420 if (rec->usage > usage)
421 continue;
422 if (rec->usage < usage)
423 break;
424 if (rec->selector > selector)
425 continue;
426 if (rec->selector < selector)
427 break;
428 if (dane->dctx->mdord[rec->mtype] > dane->dctx->mdord[mtype])
429 continue;
430 break;
431 }
432
433 if (!sk_danetls_record_insert(dane->trecs, t, i)) {
434 tlsa_free(t);
e077455e 435 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
919ba009
VD
436 return -1;
437 }
438 dane->umask |= DANETLS_USAGE_BIT(usage);
439
440 return 1;
441}
442
c8feba72
BK
443/*
444 * Return 0 if there is only one version configured and it was disabled
445 * at configure time. Return 1 otherwise.
446 */
d6e7ebba 447static int ssl_check_allowed_versions(int min_version, int max_version)
c8feba72
BK
448{
449 int minisdtls = 0, maxisdtls = 0;
450
451 /* Figure out if we're doing DTLS versions or TLS versions */
452 if (min_version == DTLS1_BAD_VER
453 || min_version >> 8 == DTLS1_VERSION_MAJOR)
454 minisdtls = 1;
455 if (max_version == DTLS1_BAD_VER
456 || max_version >> 8 == DTLS1_VERSION_MAJOR)
457 maxisdtls = 1;
458 /* A wildcard version of 0 could be DTLS or TLS. */
459 if ((minisdtls && !maxisdtls && max_version != 0)
460 || (maxisdtls && !minisdtls && min_version != 0)) {
461 /* Mixing DTLS and TLS versions will lead to sadness; deny it. */
462 return 0;
463 }
464
465 if (minisdtls || maxisdtls) {
466 /* Do DTLS version checks. */
467 if (min_version == 0)
468 /* Ignore DTLS1_BAD_VER */
469 min_version = DTLS1_VERSION;
470 if (max_version == 0)
471 max_version = DTLS1_2_VERSION;
472#ifdef OPENSSL_NO_DTLS1_2
473 if (max_version == DTLS1_2_VERSION)
474 max_version = DTLS1_VERSION;
475#endif
476#ifdef OPENSSL_NO_DTLS1
477 if (min_version == DTLS1_VERSION)
478 min_version = DTLS1_2_VERSION;
479#endif
79b4444d
DMSP
480 /* Done massaging versions; do the check. */
481 if (0
c8feba72
BK
482#ifdef OPENSSL_NO_DTLS1
483 || (DTLS_VERSION_GE(min_version, DTLS1_VERSION)
484 && DTLS_VERSION_GE(DTLS1_VERSION, max_version))
485#endif
486#ifdef OPENSSL_NO_DTLS1_2
487 || (DTLS_VERSION_GE(min_version, DTLS1_2_VERSION)
488 && DTLS_VERSION_GE(DTLS1_2_VERSION, max_version))
489#endif
490 )
491 return 0;
492 } else {
493 /* Regular TLS version checks. */
79b4444d 494 if (min_version == 0)
d6e7ebba 495 min_version = SSL3_VERSION;
79b4444d
DMSP
496 if (max_version == 0)
497 max_version = TLS1_3_VERSION;
c8feba72 498#ifdef OPENSSL_NO_TLS1_3
79b4444d
DMSP
499 if (max_version == TLS1_3_VERSION)
500 max_version = TLS1_2_VERSION;
c8feba72
BK
501#endif
502#ifdef OPENSSL_NO_TLS1_2
79b4444d
DMSP
503 if (max_version == TLS1_2_VERSION)
504 max_version = TLS1_1_VERSION;
c8feba72
BK
505#endif
506#ifdef OPENSSL_NO_TLS1_1
79b4444d
DMSP
507 if (max_version == TLS1_1_VERSION)
508 max_version = TLS1_VERSION;
c8feba72
BK
509#endif
510#ifdef OPENSSL_NO_TLS1
79b4444d
DMSP
511 if (max_version == TLS1_VERSION)
512 max_version = SSL3_VERSION;
c8feba72
BK
513#endif
514#ifdef OPENSSL_NO_SSL3
79b4444d
DMSP
515 if (min_version == SSL3_VERSION)
516 min_version = TLS1_VERSION;
c8feba72
BK
517#endif
518#ifdef OPENSSL_NO_TLS1
79b4444d
DMSP
519 if (min_version == TLS1_VERSION)
520 min_version = TLS1_1_VERSION;
c8feba72
BK
521#endif
522#ifdef OPENSSL_NO_TLS1_1
79b4444d
DMSP
523 if (min_version == TLS1_1_VERSION)
524 min_version = TLS1_2_VERSION;
c8feba72
BK
525#endif
526#ifdef OPENSSL_NO_TLS1_2
79b4444d
DMSP
527 if (min_version == TLS1_2_VERSION)
528 min_version = TLS1_3_VERSION;
c8feba72 529#endif
79b4444d
DMSP
530 /* Done massaging versions; do the check. */
531 if (0
c8feba72
BK
532#ifdef OPENSSL_NO_SSL3
533 || (min_version <= SSL3_VERSION && SSL3_VERSION <= max_version)
534#endif
535#ifdef OPENSSL_NO_TLS1
536 || (min_version <= TLS1_VERSION && TLS1_VERSION <= max_version)
537#endif
538#ifdef OPENSSL_NO_TLS1_1
539 || (min_version <= TLS1_1_VERSION && TLS1_1_VERSION <= max_version)
540#endif
541#ifdef OPENSSL_NO_TLS1_2
542 || (min_version <= TLS1_2_VERSION && TLS1_2_VERSION <= max_version)
543#endif
544#ifdef OPENSSL_NO_TLS1_3
545 || (min_version <= TLS1_3_VERSION && TLS1_3_VERSION <= max_version)
546#endif
547 )
548 return 0;
549 }
550 return 1;
551}
552
08073700
RB
553#if defined(__TANDEM) && defined(OPENSSL_VPROC)
554/*
555 * Define a VPROC function for HP NonStop build ssl library.
556 * This is used by platform version identification tools.
557 * Do not inline this procedure or make it static.
558 */
559# define OPENSSL_VPROC_STRING_(x) x##_SSL
560# define OPENSSL_VPROC_STRING(x) OPENSSL_VPROC_STRING_(x)
561# define OPENSSL_VPROC_FUNC OPENSSL_VPROC_STRING(OPENSSL_VPROC)
562void OPENSSL_VPROC_FUNC(void) {}
563#endif
564
4f43d0e7 565int SSL_clear(SSL *s)
0f113f3e 566{
0f113f3e 567 if (s->method == NULL) {
6849b73c 568 ERR_raise(ERR_LIB_SSL, SSL_R_NO_METHOD_SPECIFIED);
a89325e4 569 return 0;
0f113f3e 570 }
d02b48c6 571
38b051a1
TM
572 return s->method->ssl_reset(s);
573}
574
575int ossl_ssl_connection_reset(SSL *s)
576{
577 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
578
579 if (sc == NULL)
580 return 0;
581
582 if (ssl_clear_bad_session(sc)) {
583 SSL_SESSION_free(sc->session);
584 sc->session = NULL;
0f113f3e 585 }
38b051a1
TM
586 SSL_SESSION_free(sc->psksession);
587 sc->psksession = NULL;
588 OPENSSL_free(sc->psksession_id);
589 sc->psksession_id = NULL;
590 sc->psksession_id_len = 0;
5ac7ee4d 591 sc->hello_retry_request = SSL_HRR_NONE;
38b051a1 592 sc->sent_tickets = 0;
d62bfb39 593
38b051a1
TM
594 sc->error = 0;
595 sc->hit = 0;
596 sc->shutdown = 0;
d02b48c6 597
38b051a1 598 if (sc->renegotiate) {
6849b73c 599 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
0f113f3e
MC
600 return 0;
601 }
d02b48c6 602
38b051a1 603 ossl_statem_clear(sc);
413c4f45 604
38b051a1
TM
605 sc->version = s->method->version;
606 sc->client_version = sc->version;
607 sc->rwstate = SSL_NOTHING;
d02b48c6 608
38b051a1
TM
609 BUF_MEM_free(sc->init_buf);
610 sc->init_buf = NULL;
38b051a1 611 sc->first_packet = 0;
d02b48c6 612
38b051a1 613 sc->key_update = SSL_KEY_UPDATE_NONE;
b67cb09f
TS
614 memset(sc->ext.compress_certificate_from_peer, 0,
615 sizeof(sc->ext.compress_certificate_from_peer));
616 sc->ext.compress_certificate_sent = 0;
44c04a2e 617
38b051a1
TM
618 EVP_MD_CTX_free(sc->pha_dgst);
619 sc->pha_dgst = NULL;
88834998 620
919ba009 621 /* Reset DANE verification result state */
38b051a1
TM
622 sc->dane.mdpth = -1;
623 sc->dane.pdpth = -1;
624 X509_free(sc->dane.mcert);
625 sc->dane.mcert = NULL;
626 sc->dane.mtlsa = NULL;
919ba009
VD
627
628 /* Clear the verification result peername */
38b051a1 629 X509_VERIFY_PARAM_move_peername(sc->param, NULL);
919ba009 630
29948ac8 631 /* Clear any shared connection state */
38b051a1
TM
632 OPENSSL_free(sc->shared_sigalgs);
633 sc->shared_sigalgs = NULL;
634 sc->shared_sigalgslen = 0;
29948ac8 635
0f113f3e
MC
636 /*
637 * Check to see if we were changed into a different method, if so, revert
24252537 638 * back.
0f113f3e 639 */
a7f41885 640 if (s->method != s->defltmeth) {
38b051a1 641 s->method->ssl_deinit(s);
a7f41885 642 s->method = s->defltmeth;
38b051a1 643 if (!s->method->ssl_init(s))
a89325e4 644 return 0;
b77f3ed1
MC
645 } else {
646 if (!s->method->ssl_clear(s))
647 return 0;
648 }
33d23b87 649
4a0e4849 650 if (!RECORD_LAYER_reset(&sc->rlayer))
2b71b042 651 return 0;
aedbb71b 652
a89325e4 653 return 1;
0f113f3e 654}
d02b48c6 655
dd0164e7 656#ifndef OPENSSL_NO_DEPRECATED_3_0
4f43d0e7 657/** Used to change an SSL_CTXs default SSL method type */
0f113f3e
MC
658int SSL_CTX_set_ssl_version(SSL_CTX *ctx, const SSL_METHOD *meth)
659{
660 STACK_OF(SSL_CIPHER) *sk;
661
3ea30e76
HL
662 if (IS_QUIC_CTX(ctx)) {
663 ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION);
664 return 0;
665 }
666
0f113f3e
MC
667 ctx->method = meth;
668
5d120511 669 if (!SSL_CTX_set_ciphersuites(ctx, OSSL_default_ciphersuites())) {
6849b73c 670 ERR_raise(ERR_LIB_SSL, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
2340ed27
BK
671 return 0;
672 }
a68eee67 673 sk = ssl_create_cipher_list(ctx,
f865b081
MC
674 ctx->tls13_ciphersuites,
675 &(ctx->cipher_list),
0f113f3e 676 &(ctx->cipher_list_by_id),
5d120511 677 OSSL_default_cipher_list(), ctx->cert);
0f113f3e 678 if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0)) {
6849b73c 679 ERR_raise(ERR_LIB_SSL, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
26a7d938 680 return 0;
0f113f3e 681 }
208fb891 682 return 1;
0f113f3e 683}
dd0164e7 684#endif
d02b48c6 685
4f43d0e7 686SSL *SSL_new(SSL_CTX *ctx)
0f113f3e 687{
0f113f3e 688 if (ctx == NULL) {
6849b73c 689 ERR_raise(ERR_LIB_SSL, SSL_R_NULL_SSL_CTX);
26a7d938 690 return NULL;
0f113f3e
MC
691 }
692 if (ctx->method == NULL) {
6849b73c 693 ERR_raise(ERR_LIB_SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
26a7d938 694 return NULL;
0f113f3e 695 }
38b051a1
TM
696 return ctx->method->ssl_new(ctx);
697}
698
a7f41885 699int ossl_ssl_init(SSL *ssl, SSL_CTX *ctx, const SSL_METHOD *method, int type)
38b051a1
TM
700{
701 ssl->type = type;
702
38b051a1
TM
703 ssl->lock = CRYPTO_THREAD_lock_new();
704 if (ssl->lock == NULL)
705 return 0;
706
43a07d6d
P
707 if (!CRYPTO_NEW_REF(&ssl->references, 1)) {
708 CRYPTO_THREAD_lock_free(ssl->lock);
709 return 0;
710 }
711
c10ded8c
TS
712 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, ssl, &ssl->ex_data)) {
713 CRYPTO_THREAD_lock_free(ssl->lock);
43a07d6d 714 CRYPTO_FREE_REF(&ssl->references);
c10ded8c
TS
715 ssl->lock = NULL;
716 return 0;
717 }
718
38b051a1
TM
719 SSL_CTX_up_ref(ctx);
720 ssl->ctx = ctx;
721
a7f41885 722 ssl->defltmeth = ssl->method = method;
38b051a1 723
38b051a1
TM
724 return 1;
725}
726
a7f41885 727SSL *ossl_ssl_connection_new_int(SSL_CTX *ctx, const SSL_METHOD *method)
38b051a1
TM
728{
729 SSL_CONNECTION *s;
730 SSL *ssl;
0f113f3e 731
b51bce94 732 s = OPENSSL_zalloc(sizeof(*s));
0f113f3e 733 if (s == NULL)
38b051a1 734 return NULL;
0f113f3e 735
38b051a1 736 ssl = &s->ssl;
a7f41885 737 if (!ossl_ssl_init(ssl, ctx, method, SSL_TYPE_SSL_CONNECTION)) {
e6b10c34
BE
738 OPENSSL_free(s);
739 s = NULL;
c4a44e7b 740 ssl = NULL;
e077455e 741 goto sslerr;
e6b10c34 742 }
ae3947de 743
c036e210 744 RECORD_LAYER_init(&s->rlayer, s);
28d59af8 745
0f113f3e 746 s->options = ctx->options;
f0d9757c 747
5ae4ceb9 748 s->dane.flags = ctx->dane.flags;
4f373a97
TM
749 if (method->version == ctx->method->version) {
750 s->min_proto_version = ctx->min_proto_version;
751 s->max_proto_version = ctx->max_proto_version;
752 }
0eecf841 753
0f113f3e
MC
754 s->mode = ctx->mode;
755 s->max_cert_list = ctx->max_cert_list;
6e5550a1
HL
756 s->max_early_data = ctx->max_early_data;
757 s->recv_max_early_data = ctx->recv_max_early_data;
82a2beca 758
9d0a8bb7 759 s->num_tickets = ctx->num_tickets;
e97be718 760 s->pha_enabled = ctx->pha_enabled;
0f113f3e 761
f865b081
MC
762 /* Shallow copy of the ciphersuites stack */
763 s->tls13_ciphersuites = sk_SSL_CIPHER_dup(ctx->tls13_ciphersuites);
764 if (s->tls13_ciphersuites == NULL)
e077455e 765 goto cerr;
f865b081 766
2c382349
KR
767 /*
768 * Earlier library versions used to copy the pointer to the CERT, not
769 * its contents; only when setting new parameters for the per-SSL
770 * copy, ssl_cert_new would be called (and the direct reference to
771 * the per-SSL_CTX settings would be lost, but those still were
772 * indirectly accessed for various purposes, and for that reason they
773 * used to be known as s->ctx->default_cert). Now we don't look at the
774 * SSL_CTX's CERT after having duplicated it once.
775 */
776 s->cert = ssl_cert_dup(ctx->cert);
777 if (s->cert == NULL)
e077455e 778 goto sslerr;
0f113f3e 779
52e1d7b1 780 RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead);
0f113f3e
MC
781 s->msg_callback = ctx->msg_callback;
782 s->msg_callback_arg = ctx->msg_callback_arg;
783 s->verify_mode = ctx->verify_mode;
784 s->not_resumable_session_cb = ctx->not_resumable_session_cb;
d6e7ebba
HL
785 s->rlayer.record_padding_cb = ctx->record_padding_cb;
786 s->rlayer.record_padding_arg = ctx->record_padding_arg;
787 s->rlayer.block_padding = ctx->block_padding;
0f113f3e 788 s->sid_ctx_length = ctx->sid_ctx_length;
cbe29648 789 if (!ossl_assert(s->sid_ctx_length <= sizeof(s->sid_ctx)))
380a522f 790 goto err;
0f113f3e
MC
791 memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
792 s->verify_callback = ctx->default_verify_callback;
793 s->generate_session_id = ctx->generate_session_id;
794
795 s->param = X509_VERIFY_PARAM_new();
a71edf3b 796 if (s->param == NULL)
e077455e 797 goto asn1err;
0f113f3e 798 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
f66f0d3c 799 s->quiet_shutdown = IS_QUIC_CTX(ctx) ? 0 : ctx->quiet_shutdown;
cf72c757 800
82a2beca 801 if (!IS_QUIC_CTX(ctx))
d0638fd5
HL
802 s->ext.max_fragment_len_mode = ctx->ext.max_fragment_len_mode;
803
0f113f3e 804 s->max_send_fragment = ctx->max_send_fragment;
d102d9df
MC
805 s->split_send_fragment = ctx->split_send_fragment;
806 s->max_pipelines = ctx->max_pipelines;
cffafb5f 807 s->rlayer.default_read_buf_len = ctx->default_read_buf_len;
bf21446a 808
aff8c126
RS
809 s->ext.debug_cb = 0;
810 s->ext.debug_arg = NULL;
811 s->ext.ticket_expected = 0;
812 s->ext.status_type = ctx->ext.status_type;
813 s->ext.status_expected = 0;
814 s->ext.ocsp.ids = NULL;
815 s->ext.ocsp.exts = NULL;
816 s->ext.ocsp.resp = NULL;
817 s->ext.ocsp.resp_len = 0;
16203f7b 818 SSL_CTX_up_ref(ctx);
222da979 819 s->session_ctx = ctx;
aff8c126
RS
820 if (ctx->ext.ecpointformats) {
821 s->ext.ecpointformats =
822 OPENSSL_memdup(ctx->ext.ecpointformats,
823 ctx->ext.ecpointformats_len);
39a14059
MC
824 if (!s->ext.ecpointformats) {
825 s->ext.ecpointformats_len = 0;
0f113f3e 826 goto err;
39a14059 827 }
aff8c126
RS
828 s->ext.ecpointformats_len =
829 ctx->ext.ecpointformats_len;
830 }
831 if (ctx->ext.supportedgroups) {
832 s->ext.supportedgroups =
833 OPENSSL_memdup(ctx->ext.supportedgroups,
9e84a42d 834 ctx->ext.supportedgroups_len
b92d7b62 835 * sizeof(*ctx->ext.supportedgroups));
39a14059
MC
836 if (!s->ext.supportedgroups) {
837 s->ext.supportedgroups_len = 0;
0f113f3e 838 goto err;
39a14059 839 }
aff8c126 840 s->ext.supportedgroups_len = ctx->ext.supportedgroups_len;
0f113f3e 841 }
dbc6268f 842
a230b26e 843#ifndef OPENSSL_NO_NEXTPROTONEG
aff8c126 844 s->ext.npn = NULL;
a230b26e 845#endif
6f017a8f 846
38b051a1
TM
847 if (ctx->ext.alpn != NULL) {
848 s->ext.alpn = OPENSSL_malloc(ctx->ext.alpn_len);
39a14059
MC
849 if (s->ext.alpn == NULL) {
850 s->ext.alpn_len = 0;
0f113f3e 851 goto err;
39a14059 852 }
38b051a1
TM
853 memcpy(s->ext.alpn, ctx->ext.alpn, ctx->ext.alpn_len);
854 s->ext.alpn_len = ctx->ext.alpn_len;
0f113f3e 855 }
d02b48c6 856
696178ed 857 s->verified_chain = NULL;
0f113f3e 858 s->verify_result = X509_V_OK;
d02b48c6 859
a974e64a
MC
860 s->default_passwd_callback = ctx->default_passwd_callback;
861 s->default_passwd_callback_userdata = ctx->default_passwd_callback_userdata;
862
44c04a2e
MC
863 s->key_update = SSL_KEY_UPDATE_NONE;
864
82a2beca
HL
865 if (!IS_QUIC_CTX(ctx)) {
866 s->allow_early_data_cb = ctx->allow_early_data_cb;
867 s->allow_early_data_cb_data = ctx->allow_early_data_cb_data;
868 }
c9598459 869
a7f41885 870 if (!method->ssl_init(ssl))
e077455e 871 goto sslerr;
d02b48c6 872
a7f41885 873 s->server = (method->ssl_accept == ssl_undefined_function) ? 0 : 1;
bf21446a 874
a7f41885 875 if (!method->ssl_reset(ssl))
e077455e 876 goto sslerr;
58964a49 877
ddac1974 878#ifndef OPENSSL_NO_PSK
0f113f3e
MC
879 s->psk_client_callback = ctx->psk_client_callback;
880 s->psk_server_callback = ctx->psk_server_callback;
ddac1974 881#endif
f46184bd
MC
882 s->psk_find_session_cb = ctx->psk_find_session_cb;
883 s->psk_use_session_cb = ctx->psk_use_session_cb;
ddac1974 884
9f5a87fd
PY
885 s->async_cb = ctx->async_cb;
886 s->async_cb_arg = ctx->async_cb_arg;
887
07bbc92c
MC
888 s->job = NULL;
889
b67cb09f
TS
890#ifndef OPENSSL_NO_COMP_ALG
891 memcpy(s->cert_comp_prefs, ctx->cert_comp_prefs, sizeof(s->cert_comp_prefs));
892#endif
3c95ef22
TS
893 if (ctx->client_cert_type != NULL) {
894 s->client_cert_type = OPENSSL_memdup(ctx->client_cert_type,
895 ctx->client_cert_type_len);
896 if (s->client_cert_type == NULL)
897 goto sslerr;
898 s->client_cert_type_len = ctx->client_cert_type_len;
899 }
900 if (ctx->server_cert_type != NULL) {
901 s->server_cert_type = OPENSSL_memdup(ctx->server_cert_type,
902 ctx->server_cert_type_len);
903 if (s->server_cert_type == NULL)
904 goto sslerr;
905 s->server_cert_type_len = ctx->server_cert_type_len;
906 }
b67cb09f 907
ed29e82a 908#ifndef OPENSSL_NO_CT
38b051a1 909 if (!SSL_set_ct_validation_callback(ssl, ctx->ct_validation_callback,
a230b26e 910 ctx->ct_validation_callback_arg))
e077455e 911 goto sslerr;
ed29e82a
RP
912#endif
913
ee58915c 914 s->ssl_pkey_num = SSL_PKEY_NUM + ctx->sigalg_list_len;
38b051a1 915 return ssl;
e077455e
RL
916 cerr:
917 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
918 goto err;
919 asn1err:
920 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
921 goto err;
922 sslerr:
923 ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
0f113f3e 924 err:
38b051a1 925 SSL_free(ssl);
16203f7b 926 return NULL;
0f113f3e 927}
d02b48c6 928
a7f41885
MC
929SSL *ossl_ssl_connection_new(SSL_CTX *ctx)
930{
931 return ossl_ssl_connection_new_int(ctx, ctx->method);
932}
933
e417070c
RS
934int SSL_is_dtls(const SSL *s)
935{
38b051a1
TM
936 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
937
50769b15 938#ifndef OPENSSL_NO_QUIC
f8636c7e 939 if (s->type == SSL_TYPE_QUIC_CONNECTION || s->type == SSL_TYPE_QUIC_XSO)
50769b15
MC
940 return 0;
941#endif
942
38b051a1
TM
943 if (sc == NULL)
944 return 0;
945
946 return SSL_CONNECTION_IS_DTLS(sc) ? 1 : 0;
e417070c
RS
947}
948
50769b15
MC
949int SSL_is_tls(const SSL *s)
950{
951 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
952
953#ifndef OPENSSL_NO_QUIC
f8636c7e 954 if (s->type == SSL_TYPE_QUIC_CONNECTION || s->type == SSL_TYPE_QUIC_XSO)
50769b15
MC
955 return 0;
956#endif
957
958 if (sc == NULL)
959 return 0;
960
961 return SSL_CONNECTION_IS_DTLS(sc) ? 0 : 1;
962}
963
964int SSL_is_quic(const SSL *s)
965{
966#ifndef OPENSSL_NO_QUIC
f8636c7e 967 if (s->type == SSL_TYPE_QUIC_CONNECTION || s->type == SSL_TYPE_QUIC_XSO)
50769b15
MC
968 return 1;
969#endif
970 return 0;
971}
972
c5ebfcab 973int SSL_up_ref(SSL *s)
a18a31e4 974{
16203f7b 975 int i;
c5ebfcab 976
43a07d6d 977 if (CRYPTO_UP_REF(&s->references, &i) <= 0)
c5ebfcab
F
978 return 0;
979
980 REF_PRINT_COUNT("SSL", s);
981 REF_ASSERT_ISNT(i < 2);
982 return ((i > 1) ? 1 : 0);
a18a31e4
MC
983}
984
0f113f3e
MC
985int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const unsigned char *sid_ctx,
986 unsigned int sid_ctx_len)
987{
fe9edc9d 988 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
6849b73c 989 ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
0f113f3e
MC
990 return 0;
991 }
992 ctx->sid_ctx_length = sid_ctx_len;
993 memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
4eb77b26
BM
994
995 return 1;
0f113f3e 996}
4eb77b26 997
0f113f3e
MC
998int SSL_set_session_id_context(SSL *ssl, const unsigned char *sid_ctx,
999 unsigned int sid_ctx_len)
1000{
38b051a1
TM
1001 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
1002
1003 if (sc == NULL)
1004 return 0;
1005
0f113f3e 1006 if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
6849b73c 1007 ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
0f113f3e
MC
1008 return 0;
1009 }
38b051a1
TM
1010 sc->sid_ctx_length = sid_ctx_len;
1011 memcpy(sc->sid_ctx, sid_ctx, sid_ctx_len);
b4cadc6e
BL
1012
1013 return 1;
0f113f3e 1014}
b4cadc6e 1015
dc644fe2 1016int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
0f113f3e 1017{
cd3f8c1b
RS
1018 if (!CRYPTO_THREAD_write_lock(ctx->lock))
1019 return 0;
0f113f3e 1020 ctx->generate_session_id = cb;
16203f7b 1021 CRYPTO_THREAD_unlock(ctx->lock);
0f113f3e
MC
1022 return 1;
1023}
dc644fe2
GT
1024
1025int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
0f113f3e 1026{
38b051a1
TM
1027 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
1028
1029 if (sc == NULL || !CRYPTO_THREAD_write_lock(ssl->lock))
cd3f8c1b 1030 return 0;
38b051a1 1031 sc->generate_session_id = cb;
16203f7b 1032 CRYPTO_THREAD_unlock(ssl->lock);
0f113f3e
MC
1033 return 1;
1034}
dc644fe2 1035
f85c9904 1036int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
0f113f3e
MC
1037 unsigned int id_len)
1038{
1039 /*
1040 * A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
69687aa8 1041 * we can "construct" a session to give us the desired check - i.e. to
0f113f3e
MC
1042 * find if there's a session in the hash table that would conflict with
1043 * any new session built out of this id/id_len and the ssl_version in use
1044 * by this SSL.
1045 */
1046 SSL_SESSION r, *p;
38b051a1 1047 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
0f113f3e 1048
38b051a1 1049 if (sc == NULL || id_len > sizeof(r.session_id))
0f113f3e
MC
1050 return 0;
1051
38b051a1 1052 r.ssl_version = sc->version;
0f113f3e
MC
1053 r.session_id_length = id_len;
1054 memcpy(r.session_id, id, id_len);
1055
38b051a1 1056 if (!CRYPTO_THREAD_read_lock(sc->session_ctx->lock))
cd3f8c1b 1057 return 0;
38b051a1
TM
1058 p = lh_SSL_SESSION_retrieve(sc->session_ctx->sessions, &r);
1059 CRYPTO_THREAD_unlock(sc->session_ctx->lock);
0f113f3e
MC
1060 return (p != NULL);
1061}
dc644fe2 1062
bb7cd4e3 1063int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
0f113f3e
MC
1064{
1065 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
1066}
bb7cd4e3
DSH
1067
1068int SSL_set_purpose(SSL *s, int purpose)
0f113f3e 1069{
38b051a1
TM
1070 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1071
1072 if (sc == NULL)
1073 return 0;
1074
1075 return X509_VERIFY_PARAM_set_purpose(sc->param, purpose);
0f113f3e 1076}
926a56bf 1077
bb7cd4e3 1078int SSL_CTX_set_trust(SSL_CTX *s, int trust)
0f113f3e
MC
1079{
1080 return X509_VERIFY_PARAM_set_trust(s->param, trust);
1081}
bb7cd4e3
DSH
1082
1083int SSL_set_trust(SSL *s, int trust)
0f113f3e 1084{
38b051a1
TM
1085 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1086
1087 if (sc == NULL)
1088 return 0;
1089
1090 return X509_VERIFY_PARAM_set_trust(sc->param, trust);
0f113f3e 1091}
bb7cd4e3 1092
919ba009
VD
1093int SSL_set1_host(SSL *s, const char *hostname)
1094{
38b051a1
TM
1095 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1096
1097 if (sc == NULL)
1098 return 0;
1099
c832840e
DW
1100 /* If a hostname is provided and parses as an IP address,
1101 * treat it as such. */
38b051a1
TM
1102 if (hostname != NULL
1103 && X509_VERIFY_PARAM_set1_ip_asc(sc->param, hostname) == 1)
c832840e
DW
1104 return 1;
1105
38b051a1 1106 return X509_VERIFY_PARAM_set1_host(sc->param, hostname, 0);
919ba009
VD
1107}
1108
1109int SSL_add1_host(SSL *s, const char *hostname)
1110{
38b051a1
TM
1111 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1112
1113 if (sc == NULL)
1114 return 0;
1115
c832840e
DW
1116 /* If a hostname is provided and parses as an IP address,
1117 * treat it as such. */
892a9e4c
DW
1118 if (hostname)
1119 {
1120 ASN1_OCTET_STRING *ip;
1121 char *old_ip;
1122
1123 ip = a2i_IPADDRESS(hostname);
1124 if (ip) {
1125 /* We didn't want it; only to check if it *is* an IP address */
1126 ASN1_OCTET_STRING_free(ip);
1127
38b051a1 1128 old_ip = X509_VERIFY_PARAM_get1_ip_asc(sc->param);
892a9e4c
DW
1129 if (old_ip)
1130 {
f2bfc53b 1131 OPENSSL_free(old_ip);
892a9e4c
DW
1132 /* There can be only one IP address */
1133 return 0;
1134 }
1135
38b051a1 1136 return X509_VERIFY_PARAM_set1_ip_asc(sc->param, hostname);
892a9e4c
DW
1137 }
1138 }
c832840e 1139
38b051a1 1140 return X509_VERIFY_PARAM_add1_host(sc->param, hostname, 0);
919ba009
VD
1141}
1142
1143void SSL_set_hostflags(SSL *s, unsigned int flags)
1144{
38b051a1
TM
1145 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1146
1147 if (sc == NULL)
1148 return;
1149
1150 X509_VERIFY_PARAM_set_hostflags(sc->param, flags);
919ba009
VD
1151}
1152
4588cb44 1153const char *SSL_get0_peername(SSL *s)
919ba009 1154{
38b051a1
TM
1155 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1156
1157 if (sc == NULL)
1158 return NULL;
1159
1160 return X509_VERIFY_PARAM_get0_peername(sc->param);
919ba009
VD
1161}
1162
1163int SSL_CTX_dane_enable(SSL_CTX *ctx)
1164{
1165 return dane_ctx_enable(&ctx->dane);
1166}
1167
5ae4ceb9
VD
1168unsigned long SSL_CTX_dane_set_flags(SSL_CTX *ctx, unsigned long flags)
1169{
1170 unsigned long orig = ctx->dane.flags;
1171
1172 ctx->dane.flags |= flags;
1173 return orig;
1174}
1175
1176unsigned long SSL_CTX_dane_clear_flags(SSL_CTX *ctx, unsigned long flags)
1177{
1178 unsigned long orig = ctx->dane.flags;
1179
1180 ctx->dane.flags &= ~flags;
1181 return orig;
1182}
1183
919ba009
VD
1184int SSL_dane_enable(SSL *s, const char *basedomain)
1185{
38b051a1
TM
1186 SSL_DANE *dane;
1187 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
919ba009 1188
38b051a1
TM
1189 if (sc == NULL)
1190 return 0;
1191
1192 dane = &sc->dane;
919ba009 1193 if (s->ctx->dane.mdmax == 0) {
6849b73c 1194 ERR_raise(ERR_LIB_SSL, SSL_R_CONTEXT_NOT_DANE_ENABLED);
919ba009
VD
1195 return 0;
1196 }
1197 if (dane->trecs != NULL) {
6849b73c 1198 ERR_raise(ERR_LIB_SSL, SSL_R_DANE_ALREADY_ENABLED);
919ba009
VD
1199 return 0;
1200 }
1201
8d887efa
VD
1202 /*
1203 * Default SNI name. This rejects empty names, while set1_host below
9929c817 1204 * accepts them and disables hostname checks. To avoid side-effects with
8d887efa
VD
1205 * invalid input, set the SNI name first.
1206 */
38b051a1 1207 if (sc->ext.hostname == NULL) {
dccd20d1 1208 if (!SSL_set_tlsext_host_name(s, basedomain)) {
6849b73c 1209 ERR_raise(ERR_LIB_SSL, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN);
dccd20d1 1210 return -1;
8d887efa
VD
1211 }
1212 }
1213
919ba009 1214 /* Primary RFC6125 reference identifier */
38b051a1 1215 if (!X509_VERIFY_PARAM_set1_host(sc->param, basedomain, 0)) {
6849b73c 1216 ERR_raise(ERR_LIB_SSL, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN);
919ba009
VD
1217 return -1;
1218 }
1219
919ba009
VD
1220 dane->mdpth = -1;
1221 dane->pdpth = -1;
1222 dane->dctx = &s->ctx->dane;
1223 dane->trecs = sk_danetls_record_new_null();
1224
1225 if (dane->trecs == NULL) {
e077455e 1226 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
919ba009
VD
1227 return -1;
1228 }
1229 return 1;
1230}
1231
5ae4ceb9
VD
1232unsigned long SSL_dane_set_flags(SSL *ssl, unsigned long flags)
1233{
38b051a1
TM
1234 unsigned long orig;
1235 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
1236
1237 if (sc == NULL)
1238 return 0;
1239
1240 orig = sc->dane.flags;
5ae4ceb9 1241
38b051a1 1242 sc->dane.flags |= flags;
5ae4ceb9
VD
1243 return orig;
1244}
1245
1246unsigned long SSL_dane_clear_flags(SSL *ssl, unsigned long flags)
1247{
38b051a1
TM
1248 unsigned long orig;
1249 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
5ae4ceb9 1250
38b051a1
TM
1251 if (sc == NULL)
1252 return 0;
1253
1254 orig = sc->dane.flags;
1255
1256 sc->dane.flags &= ~flags;
5ae4ceb9
VD
1257 return orig;
1258}
1259
919ba009
VD
1260int SSL_get0_dane_authority(SSL *s, X509 **mcert, EVP_PKEY **mspki)
1261{
38b051a1
TM
1262 SSL_DANE *dane;
1263 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
919ba009 1264
38b051a1
TM
1265 if (sc == NULL)
1266 return -1;
1267
1268 dane = &sc->dane;
1269
1270 if (!DANETLS_ENABLED(dane) || sc->verify_result != X509_V_OK)
919ba009
VD
1271 return -1;
1272 if (dane->mtlsa) {
1273 if (mcert)
1274 *mcert = dane->mcert;
1275 if (mspki)
1276 *mspki = (dane->mcert == NULL) ? dane->mtlsa->spki : NULL;
1277 }
1278 return dane->mdpth;
1279}
1280
1281int SSL_get0_dane_tlsa(SSL *s, uint8_t *usage, uint8_t *selector,
6d4313f0 1282 uint8_t *mtype, const unsigned char **data, size_t *dlen)
919ba009 1283{
38b051a1
TM
1284 SSL_DANE *dane;
1285 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
919ba009 1286
38b051a1
TM
1287 if (sc == NULL)
1288 return -1;
1289
1290 dane = &sc->dane;
1291
1292 if (!DANETLS_ENABLED(dane) || sc->verify_result != X509_V_OK)
919ba009
VD
1293 return -1;
1294 if (dane->mtlsa) {
1295 if (usage)
1296 *usage = dane->mtlsa->usage;
1297 if (selector)
1298 *selector = dane->mtlsa->selector;
1299 if (mtype)
1300 *mtype = dane->mtlsa->mtype;
1301 if (data)
1302 *data = dane->mtlsa->data;
1303 if (dlen)
1304 *dlen = dane->mtlsa->dlen;
1305 }
1306 return dane->mdpth;
1307}
1308
b9aec69a 1309SSL_DANE *SSL_get0_dane(SSL *s)
919ba009 1310{
38b051a1
TM
1311 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1312
1313 if (sc == NULL)
1314 return NULL;
1315
1316 return &sc->dane;
919ba009
VD
1317}
1318
1319int SSL_dane_tlsa_add(SSL *s, uint8_t usage, uint8_t selector,
6d4313f0 1320 uint8_t mtype, const unsigned char *data, size_t dlen)
919ba009 1321{
38b051a1
TM
1322 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1323
1324 if (sc == NULL)
1325 return 0;
1326
1327 return dane_tlsa_add(&sc->dane, usage, selector, mtype, data, dlen);
919ba009
VD
1328}
1329
a230b26e
EK
1330int SSL_CTX_dane_mtype_set(SSL_CTX *ctx, const EVP_MD *md, uint8_t mtype,
1331 uint8_t ord)
919ba009
VD
1332{
1333 return dane_mtype_set(&ctx->dane, md, mtype, ord);
1334}
1335
ccf11751 1336int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
0f113f3e
MC
1337{
1338 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
1339}
ccf11751
DSH
1340
1341int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
0f113f3e 1342{
38b051a1
TM
1343 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
1344
1345 if (sc == NULL)
1346 return 0;
1347
1348 return X509_VERIFY_PARAM_set1(sc->param, vpm);
0f113f3e 1349}
ccf11751 1350
7af31968 1351X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx)
0f113f3e
MC
1352{
1353 return ctx->param;
1354}
7af31968
DSH
1355
1356X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl)
0f113f3e 1357{
38b051a1
TM
1358 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
1359
1360 if (sc == NULL)
1361 return NULL;
1362
1363 return sc->param;
0f113f3e 1364}
7af31968 1365
a5ee80b9 1366void SSL_certs_clear(SSL *s)
0f113f3e 1367{
38b051a1
TM
1368 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1369
1370 if (sc == NULL)
1371 return;
1372
1373 ssl_cert_clear_certs(sc->cert);
0f113f3e 1374}
a5ee80b9 1375
4f43d0e7 1376void SSL_free(SSL *s)
0f113f3e
MC
1377{
1378 int i;
58964a49 1379
e6e9170d
RS
1380 if (s == NULL)
1381 return;
43a07d6d 1382 CRYPTO_DOWN_REF(&s->references, &i);
f3f1cf84 1383 REF_PRINT_COUNT("SSL", s);
0f113f3e
MC
1384 if (i > 0)
1385 return;
f3f1cf84 1386 REF_ASSERT_ISNT(i < 0);
d02b48c6 1387
38b051a1
TM
1388 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
1389
1390 if (s->method != NULL)
1391 s->method->ssl_free(s);
1392
1393 SSL_CTX_free(s->ctx);
1394 CRYPTO_THREAD_lock_free(s->lock);
43a07d6d 1395 CRYPTO_FREE_REF(&s->references);
38b051a1
TM
1396
1397 OPENSSL_free(s);
1398}
1399
1400void ossl_ssl_connection_free(SSL *ssl)
1401{
1402 SSL_CONNECTION *s;
1403
1404 s = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
1405 if (s == NULL)
1406 return;
1407
222561fe 1408 X509_VERIFY_PARAM_free(s->param);
919ba009 1409 dane_final(&s->dane);
0f113f3e 1410
b77f3ed1 1411 /* Ignore return value */
2e7dc7cd
MC
1412 ssl_free_wbio_buffer(s);
1413
4a0e4849 1414 /* Ignore return value */
9ff51954
MC
1415 RECORD_LAYER_clear(&s->rlayer);
1416
25aaa98a 1417 BUF_MEM_free(s->init_buf);
0f113f3e
MC
1418
1419 /* add extra stuff */
25aaa98a
RS
1420 sk_SSL_CIPHER_free(s->cipher_list);
1421 sk_SSL_CIPHER_free(s->cipher_list_by_id);
f865b081 1422 sk_SSL_CIPHER_free(s->tls13_ciphersuites);
eee2a6a7 1423 sk_SSL_CIPHER_free(s->peer_ciphers);
0f113f3e
MC
1424
1425 /* Make the next call work :-) */
1426 if (s->session != NULL) {
1427 ssl_clear_bad_session(s);
1428 SSL_SESSION_free(s->session);
1429 }
9368f865 1430 SSL_SESSION_free(s->psksession);
add8d0e9 1431 OPENSSL_free(s->psksession_id);
0f113f3e 1432
e0e920b1 1433 ssl_cert_free(s->cert);
29948ac8 1434 OPENSSL_free(s->shared_sigalgs);
0f113f3e 1435 /* Free up if allocated */
d02b48c6 1436
aff8c126 1437 OPENSSL_free(s->ext.hostname);
222da979 1438 SSL_CTX_free(s->session_ctx);
aff8c126 1439 OPENSSL_free(s->ext.ecpointformats);
cd0fb43c 1440 OPENSSL_free(s->ext.peer_ecpointformats);
aff8c126 1441 OPENSSL_free(s->ext.supportedgroups);
45436e61 1442 OPENSSL_free(s->ext.peer_supportedgroups);
aff8c126 1443 sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts, X509_EXTENSION_free);
3e41ac35 1444#ifndef OPENSSL_NO_OCSP
aff8c126 1445 sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);
3e41ac35 1446#endif
ed29e82a
RP
1447#ifndef OPENSSL_NO_CT
1448 SCT_LIST_free(s->scts);
aff8c126 1449 OPENSSL_free(s->ext.scts);
ed29e82a 1450#endif
aff8c126
RS
1451 OPENSSL_free(s->ext.ocsp.resp);
1452 OPENSSL_free(s->ext.alpn);
cfef5027 1453 OPENSSL_free(s->ext.tls13_cookie);
94941cad
MK
1454 if (s->clienthello != NULL)
1455 OPENSSL_free(s->clienthello->pre_proc_exts);
6b1bb98f 1456 OPENSSL_free(s->clienthello);
9d75dce3
TS
1457 OPENSSL_free(s->pha_context);
1458 EVP_MD_CTX_free(s->pha_dgst);
0f113f3e 1459
fa7c2637 1460 sk_X509_NAME_pop_free(s->ca_names, X509_NAME_free);
98732979 1461 sk_X509_NAME_pop_free(s->client_ca_names, X509_NAME_free);
0f113f3e 1462
3c95ef22
TS
1463 OPENSSL_free(s->client_cert_type);
1464 OPENSSL_free(s->server_cert_type);
1465
79b2a2f2 1466 OSSL_STACK_OF_X509_free(s->verified_chain);
696178ed 1467
38b051a1
TM
1468 if (ssl->method != NULL)
1469 ssl->method->ssl_deinit(ssl);
7c3908dd 1470
ff75a257
MC
1471 ASYNC_WAIT_CTX_free(s->waitctx);
1472
e481f9b9 1473#if !defined(OPENSSL_NO_NEXTPROTONEG)
aff8c126 1474 OPENSSL_free(s->ext.npn);
ee2ffc27
BL
1475#endif
1476
e783bae2 1477#ifndef OPENSSL_NO_SRTP
25aaa98a 1478 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
0f113f3e 1479#endif
cd6e89b6
MC
1480
1481 /*
1482 * We do this late. We want to ensure that any other references we held to
1483 * these BIOs are freed first *before* we call BIO_free_all(), because
1484 * BIO_free_all() will only free each BIO in the chain if the number of
1485 * references to the first BIO have dropped to 0
1486 */
1487 BIO_free_all(s->wbio);
1488 s->wbio = NULL;
1489 BIO_free_all(s->rbio);
1490 s->rbio = NULL;
ee58915c 1491 OPENSSL_free(s->s3.tmp.valid_flags);
0f113f3e
MC
1492}
1493
65e2d672 1494void SSL_set0_rbio(SSL *s, BIO *rbio)
3ffbe008 1495{
38b051a1 1496 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
03bacce8 1497
6d495cc4
HL
1498#ifndef OPENSSL_NO_QUIC
1499 if (IS_QUIC(s)) {
1500 ossl_quic_conn_set0_net_rbio(s, rbio);
03bacce8
HL
1501 return;
1502 }
1503#endif
38b051a1
TM
1504
1505 if (sc == NULL)
1506 return;
1507
1508 BIO_free_all(sc->rbio);
1509 sc->rbio = rbio;
cffafb5f 1510 sc->rlayer.rrlmethod->set1_bio(sc->rlayer.rrl, sc->rbio);
3ffbe008
MC
1511}
1512
65e2d672 1513void SSL_set0_wbio(SSL *s, BIO *wbio)
0f113f3e 1514{
38b051a1 1515 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
03bacce8 1516
6d495cc4
HL
1517#ifndef OPENSSL_NO_QUIC
1518 if (IS_QUIC(s)) {
1519 ossl_quic_conn_set0_net_wbio(s, wbio);
03bacce8
HL
1520 return;
1521 }
1522#endif
38b051a1
TM
1523
1524 if (sc == NULL)
1525 return;
1526
0f113f3e
MC
1527 /*
1528 * If the output buffering BIO is still in place, remove it
1529 */
38b051a1
TM
1530 if (sc->bbio != NULL)
1531 sc->wbio = BIO_pop(sc->wbio);
2e7dc7cd 1532
38b051a1
TM
1533 BIO_free_all(sc->wbio);
1534 sc->wbio = wbio;
2e7dc7cd
MC
1535
1536 /* Re-attach |bbio| to the new |wbio|. */
38b051a1
TM
1537 if (sc->bbio != NULL)
1538 sc->wbio = BIO_push(sc->bbio, sc->wbio);
b5cf81f7
MC
1539
1540 sc->rlayer.wrlmethod->set1_bio(sc->rlayer.wrl, sc->wbio);
0f113f3e 1541}
d02b48c6 1542
3ffbe008
MC
1543void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio)
1544{
65e2d672
MC
1545 /*
1546 * For historical reasons, this function has many different cases in
1547 * ownership handling.
1548 */
1549
1550 /* If nothing has changed, do nothing */
1551 if (rbio == SSL_get_rbio(s) && wbio == SSL_get_wbio(s))
1552 return;
1553
1554 /*
1555 * If the two arguments are equal then one fewer reference is granted by the
1556 * caller than we want to take
1557 */
1558 if (rbio != NULL && rbio == wbio)
1559 BIO_up_ref(rbio);
1560
1561 /*
1562 * If only the wbio is changed only adopt one reference.
1563 */
1564 if (rbio == SSL_get_rbio(s)) {
1565 SSL_set0_wbio(s, wbio);
1566 return;
1567 }
1568 /*
1569 * There is an asymmetry here for historical reasons. If only the rbio is
1570 * changed AND the rbio and wbio were originally different, then we only
1571 * adopt one reference.
1572 */
1573 if (wbio == SSL_get_wbio(s) && SSL_get_rbio(s) != SSL_get_wbio(s)) {
1574 SSL_set0_rbio(s, rbio);
1575 return;
1576 }
1577
1578 /* Otherwise, adopt both references. */
1579 SSL_set0_rbio(s, rbio);
1580 SSL_set0_wbio(s, wbio);
3ffbe008
MC
1581}
1582
0821bcd4 1583BIO *SSL_get_rbio(const SSL *s)
0f113f3e 1584{
38b051a1 1585 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
03bacce8 1586
6d495cc4
HL
1587#ifndef OPENSSL_NO_QUIC
1588 if (IS_QUIC(s))
1589 return ossl_quic_conn_get_net_rbio(s);
03bacce8 1590#endif
38b051a1
TM
1591
1592 if (sc == NULL)
1593 return NULL;
1594
1595 return sc->rbio;
0f113f3e 1596}
d02b48c6 1597
0821bcd4 1598BIO *SSL_get_wbio(const SSL *s)
0f113f3e 1599{
38b051a1 1600 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
03bacce8 1601
6d495cc4
HL
1602#ifndef OPENSSL_NO_QUIC
1603 if (IS_QUIC(s))
1604 return ossl_quic_conn_get_net_wbio(s);
03bacce8 1605#endif
38b051a1
TM
1606
1607 if (sc == NULL)
1608 return NULL;
1609
1610 if (sc->bbio != NULL) {
2e7dc7cd
MC
1611 /*
1612 * If |bbio| is active, the true caller-configured BIO is its
1613 * |next_bio|.
1614 */
38b051a1 1615 return BIO_next(sc->bbio);
2e7dc7cd 1616 }
38b051a1 1617 return sc->wbio;
0f113f3e 1618}
d02b48c6 1619
0821bcd4 1620int SSL_get_fd(const SSL *s)
0f113f3e 1621{
2e7dc7cd 1622 return SSL_get_rfd(s);
0f113f3e 1623}
24cbf3ef 1624
0821bcd4 1625int SSL_get_rfd(const SSL *s)
0f113f3e
MC
1626{
1627 int ret = -1;
1628 BIO *b, *r;
1629
1630 b = SSL_get_rbio(s);
1631 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
1632 if (r != NULL)
1633 BIO_get_fd(r, &ret);
26a7d938 1634 return ret;
0f113f3e 1635}
d02b48c6 1636
0821bcd4 1637int SSL_get_wfd(const SSL *s)
0f113f3e
MC
1638{
1639 int ret = -1;
1640 BIO *b, *r;
1641
1642 b = SSL_get_wbio(s);
1643 r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
1644 if (r != NULL)
1645 BIO_get_fd(r, &ret);
26a7d938 1646 return ret;
0f113f3e 1647}
24cbf3ef 1648
bc36ee62 1649#ifndef OPENSSL_NO_SOCK
d6e7ebba
HL
1650static const BIO_METHOD *fd_method(SSL *s)
1651{
1652#ifndef OPENSSL_NO_DGRAM
1653 if (IS_QUIC(s))
1654 return BIO_s_datagram();
1655#endif
1656
1657 return BIO_s_socket();
1658}
1659
0f113f3e
MC
1660int SSL_set_fd(SSL *s, int fd)
1661{
1662 int ret = 0;
1663 BIO *bio = NULL;
1664
d6e7ebba
HL
1665 if (s->type == SSL_TYPE_QUIC_XSO) {
1666 ERR_raise(ERR_LIB_SSL, SSL_R_CONN_USE_ONLY);
1667 goto err;
1668 }
1669
1670 bio = BIO_new(fd_method(s));
0f113f3e
MC
1671
1672 if (bio == NULL) {
6849b73c 1673 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
0f113f3e
MC
1674 goto err;
1675 }
1676 BIO_set_fd(bio, fd, BIO_NOCLOSE);
1677 SSL_set_bio(s, bio, bio);
50ec7505
BP
1678#ifndef OPENSSL_NO_KTLS
1679 /*
1680 * The new socket is created successfully regardless of ktls_enable.
1681 * ktls_enable doesn't change any functionality of the socket, except
1682 * changing the setsockopt to enable the processing of ktls_start.
1683 * Thus, it is not a problem to call it for non-TLS sockets.
1684 */
1685 ktls_enable(fd);
1686#endif /* OPENSSL_NO_KTLS */
0f113f3e
MC
1687 ret = 1;
1688 err:
26a7d938 1689 return ret;
0f113f3e 1690}
d02b48c6 1691
0f113f3e
MC
1692int SSL_set_wfd(SSL *s, int fd)
1693{
2e7dc7cd 1694 BIO *rbio = SSL_get_rbio(s);
d6e7ebba
HL
1695 int desired_type = IS_QUIC(s) ? BIO_TYPE_DGRAM : BIO_TYPE_SOCKET;
1696
1697 if (s->type == SSL_TYPE_QUIC_XSO) {
1698 ERR_raise(ERR_LIB_SSL, SSL_R_CONN_USE_ONLY);
1699 return 0;
1700 }
0f113f3e 1701
5e6015af 1702 if (rbio == NULL || BIO_method_type(rbio) != desired_type
2e7dc7cd 1703 || (int)BIO_get_fd(rbio, NULL) != fd) {
d6e7ebba 1704 BIO *bio = BIO_new(fd_method(s));
0f113f3e
MC
1705
1706 if (bio == NULL) {
6849b73c 1707 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
2e7dc7cd 1708 return 0;
0f113f3e
MC
1709 }
1710 BIO_set_fd(bio, fd, BIO_NOCLOSE);
65e2d672 1711 SSL_set0_wbio(s, bio);
50ec7505
BP
1712#ifndef OPENSSL_NO_KTLS
1713 /*
1714 * The new socket is created successfully regardless of ktls_enable.
1715 * ktls_enable doesn't change any functionality of the socket, except
1716 * changing the setsockopt to enable the processing of ktls_start.
1717 * Thus, it is not a problem to call it for non-TLS sockets.
1718 */
1719 ktls_enable(fd);
1720#endif /* OPENSSL_NO_KTLS */
2e7dc7cd 1721 } else {
65e2d672
MC
1722 BIO_up_ref(rbio);
1723 SSL_set0_wbio(s, rbio);
2e7dc7cd
MC
1724 }
1725 return 1;
0f113f3e
MC
1726}
1727
1728int SSL_set_rfd(SSL *s, int fd)
1729{
2e7dc7cd 1730 BIO *wbio = SSL_get_wbio(s);
d6e7ebba
HL
1731 int desired_type = IS_QUIC(s) ? BIO_TYPE_DGRAM : BIO_TYPE_SOCKET;
1732
1733 if (s->type == SSL_TYPE_QUIC_XSO) {
1734 ERR_raise(ERR_LIB_SSL, SSL_R_CONN_USE_ONLY);
1735 return 0;
1736 }
0f113f3e 1737
5e6015af 1738 if (wbio == NULL || BIO_method_type(wbio) != desired_type
2e7dc7cd 1739 || ((int)BIO_get_fd(wbio, NULL) != fd)) {
d6e7ebba 1740 BIO *bio = BIO_new(fd_method(s));
0f113f3e
MC
1741
1742 if (bio == NULL) {
6849b73c 1743 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
2e7dc7cd 1744 return 0;
0f113f3e
MC
1745 }
1746 BIO_set_fd(bio, fd, BIO_NOCLOSE);
65e2d672 1747 SSL_set0_rbio(s, bio);
2e7dc7cd 1748 } else {
65e2d672
MC
1749 BIO_up_ref(wbio);
1750 SSL_set0_rbio(s, wbio);
2e7dc7cd
MC
1751 }
1752
1753 return 1;
0f113f3e
MC
1754}
1755#endif
ca03109c
BM
1756
1757/* return length of latest Finished message we sent, copy to 'buf' */
0821bcd4 1758size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
0f113f3e
MC
1759{
1760 size_t ret = 0;
38b051a1
TM
1761 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
1762
1763 if (sc == NULL)
1764 return 0;
0f113f3e 1765
38b051a1 1766 ret = sc->s3.tmp.finish_md_len;
555cbb32
TS
1767 if (count > ret)
1768 count = ret;
38b051a1 1769 memcpy(buf, sc->s3.tmp.finish_md, count);
0f113f3e
MC
1770 return ret;
1771}
ca03109c
BM
1772
1773/* return length of latest Finished message we expected, copy to 'buf' */
0821bcd4 1774size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
0f113f3e
MC
1775{
1776 size_t ret = 0;
38b051a1 1777 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
ca03109c 1778
38b051a1
TM
1779 if (sc == NULL)
1780 return 0;
1781
1782 ret = sc->s3.tmp.peer_finish_md_len;
555cbb32
TS
1783 if (count > ret)
1784 count = ret;
38b051a1 1785 memcpy(buf, sc->s3.tmp.peer_finish_md, count);
0f113f3e
MC
1786 return ret;
1787}
ca03109c 1788
0821bcd4 1789int SSL_get_verify_mode(const SSL *s)
0f113f3e 1790{
38b051a1
TM
1791 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
1792
1793 if (sc == NULL)
1794 return 0;
1795
1796 return sc->verify_mode;
0f113f3e 1797}
d02b48c6 1798
0821bcd4 1799int SSL_get_verify_depth(const SSL *s)
0f113f3e 1800{
38b051a1
TM
1801 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
1802
1803 if (sc == NULL)
1804 return 0;
1805
1806 return X509_VERIFY_PARAM_get_depth(sc->param);
0f113f3e 1807}
7f89714e 1808
0f113f3e 1809int (*SSL_get_verify_callback(const SSL *s)) (int, X509_STORE_CTX *) {
38b051a1
TM
1810 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
1811
1812 if (sc == NULL)
1813 return NULL;
1814
1815 return sc->verify_callback;
0f113f3e 1816}
d02b48c6 1817
0821bcd4 1818int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
0f113f3e 1819{
26a7d938 1820 return ctx->verify_mode;
0f113f3e 1821}
d02b48c6 1822
0821bcd4 1823int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
0f113f3e
MC
1824{
1825 return X509_VERIFY_PARAM_get_depth(ctx->param);
1826}
1827
1828int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx)) (int, X509_STORE_CTX *) {
26a7d938 1829 return ctx->default_verify_callback;
0f113f3e
MC
1830}
1831
1832void SSL_set_verify(SSL *s, int mode,
1833 int (*callback) (int ok, X509_STORE_CTX *ctx))
1834{
38b051a1
TM
1835 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1836
1837 if (sc == NULL)
1838 return;
1839
1840 sc->verify_mode = mode;
0f113f3e 1841 if (callback != NULL)
38b051a1 1842 sc->verify_callback = callback;
0f113f3e
MC
1843}
1844
1845void SSL_set_verify_depth(SSL *s, int depth)
1846{
38b051a1
TM
1847 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
1848
1849 if (sc == NULL)
1850 return;
1851
1852 X509_VERIFY_PARAM_set_depth(sc->param, depth);
0f113f3e
MC
1853}
1854
1855void SSL_set_read_ahead(SSL *s, int yes)
1856{
9562842b 1857 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
4566dae7 1858 OSSL_PARAM options[2], *opts = options;
38b051a1 1859
9562842b 1860 if (sc == NULL)
38b051a1
TM
1861 return;
1862
1863 RECORD_LAYER_set_read_ahead(&sc->rlayer, yes);
4566dae7
MC
1864
1865 *opts++ = OSSL_PARAM_construct_int(OSSL_LIBSSL_RECORD_LAYER_PARAM_READ_AHEAD,
1866 &sc->rlayer.read_ahead);
1867 *opts = OSSL_PARAM_construct_end();
1868
1869 /* Ignore return value */
1870 sc->rlayer.rrlmethod->set_options(sc->rlayer.rrl, options);
0f113f3e 1871}
d02b48c6 1872
0821bcd4 1873int SSL_get_read_ahead(const SSL *s)
0f113f3e 1874{
9562842b 1875 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s);
38b051a1 1876
9562842b 1877 if (sc == NULL)
38b051a1
TM
1878 return 0;
1879
1880 return RECORD_LAYER_get_read_ahead(&sc->rlayer);
0f113f3e 1881}
d02b48c6 1882
0821bcd4 1883int SSL_pending(const SSL *s)
0f113f3e 1884{
8b0e934a
MC
1885 size_t pending = s->method->ssl_pending(s);
1886
0f113f3e
MC
1887 /*
1888 * SSL_pending cannot work properly if read-ahead is enabled
1889 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
1890 * impossible to fix since SSL_pending cannot report errors that may be
1891 * observed while scanning the new data. (Note that SSL_pending() is
1892 * often used as a boolean value, so we'd better not return -1.)
8b0e934a
MC
1893 *
1894 * SSL_pending also cannot work properly if the value >INT_MAX. In that case
1895 * we just return INT_MAX.
0f113f3e 1896 */
348240c6 1897 return pending < INT_MAX ? (int)pending : INT_MAX;
0f113f3e 1898}
d02b48c6 1899
49580f25
MC
1900int SSL_has_pending(const SSL *s)
1901{
1902 /*
1903 * Similar to SSL_pending() but returns a 1 to indicate that we have
6d6b295a
MC
1904 * processed or unprocessed data available or 0 otherwise (as opposed to the
1905 * number of bytes available). Unlike SSL_pending() this will take into
1906 * account read_ahead data. A 1 return simply indicates that we have data.
1907 * That data may not result in any application data, or we may fail to parse
1908 * the records for some reason.
49580f25 1909 */
560470b5 1910 const SSL_CONNECTION *sc;
560470b5 1911
22b1a96f
HL
1912#ifndef OPENSSL_NO_QUIC
1913 if (IS_QUIC(s))
6d495cc4 1914 return ossl_quic_has_pending(s);
560470b5
MC
1915#endif
1916
560470b5 1917 sc = SSL_CONNECTION_FROM_CONST_SSL(s);
38b051a1 1918
6d6b295a
MC
1919 /* Check buffered app data if any first */
1920 if (SSL_CONNECTION_IS_DTLS(sc)) {
eddb067e 1921 TLS_RECORD *rdata;
6d6b295a
MC
1922 pitem *item, *iter;
1923
715a74a6 1924 iter = pqueue_iterator(sc->rlayer.d->buffered_app_data);
6d6b295a
MC
1925 while ((item = pqueue_next(&iter)) != NULL) {
1926 rdata = item->data;
eddb067e 1927 if (rdata->length > 0)
6d6b295a
MC
1928 return 1;
1929 }
1930 }
38b051a1
TM
1931
1932 if (RECORD_LAYER_processed_read_pending(&sc->rlayer))
49580f25
MC
1933 return 1;
1934
38b051a1 1935 return RECORD_LAYER_read_pending(&sc->rlayer);
49580f25
MC
1936}
1937
8c2bfd25 1938X509 *SSL_get1_peer_certificate(const SSL *s)
0f113f3e 1939{
8c2bfd25 1940 X509 *r = SSL_get0_peer_certificate(s);
d02b48c6 1941
8c2bfd25
TS
1942 if (r != NULL)
1943 X509_up_ref(r);
0f113f3e 1944
26a7d938 1945 return r;
0f113f3e 1946}
d02b48c6 1947
8c2bfd25
TS
1948X509 *SSL_get0_peer_certificate(const SSL *s)
1949{
38b051a1
TM
1950 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
1951
1952 if (sc == NULL)
1953 return NULL;
1954
1955 if (sc->session == NULL)
8c2bfd25
TS
1956 return NULL;
1957 else
38b051a1 1958 return sc->session->peer;
8c2bfd25
TS
1959}
1960
0821bcd4 1961STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
0f113f3e
MC
1962{
1963 STACK_OF(X509) *r;
38b051a1 1964 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
0f113f3e 1965
38b051a1
TM
1966 if (sc == NULL)
1967 return NULL;
1968
1969 if (sc->session == NULL)
0f113f3e
MC
1970 r = NULL;
1971 else
38b051a1 1972 r = sc->session->peer_chain;
0f113f3e
MC
1973
1974 /*
1975 * If we are a client, cert_chain includes the peer's own certificate; if
1976 * we are a server, it does not.
1977 */
1978
26a7d938 1979 return r;
0f113f3e
MC
1980}
1981
1982/*
1983 * Now in theory, since the calling process own 't' it should be safe to
1984 * modify. We need to be able to read f without being hassled
1985 */
17dd65e6 1986int SSL_copy_session_id(SSL *t, const SSL *f)
0f113f3e 1987{
16203f7b 1988 int i;
44cb36d0 1989 /* TODO(QUIC FUTURE): Not allowed for QUIC currently. */
38b051a1
TM
1990 SSL_CONNECTION *tsc = SSL_CONNECTION_FROM_SSL_ONLY(t);
1991 const SSL_CONNECTION *fsc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(f);
1992
1993 if (tsc == NULL || fsc == NULL)
1994 return 0;
1995
3e6a0d57 1996 /* Do we need to do SSL locking? */
61986d32 1997 if (!SSL_set_session(t, SSL_get_session(f))) {
17dd65e6 1998 return 0;
69f68237 1999 }
0f113f3e
MC
2000
2001 /*
87d9cafa 2002 * what if we are setup for one protocol version but want to talk another
0f113f3e
MC
2003 */
2004 if (t->method != f->method) {
38b051a1 2005 t->method->ssl_deinit(t);
919ba009 2006 t->method = f->method;
38b051a1 2007 if (t->method->ssl_init(t) == 0)
919ba009 2008 return 0;
0f113f3e
MC
2009 }
2010
43a07d6d 2011 CRYPTO_UP_REF(&fsc->cert->references, &i);
38b051a1
TM
2012 ssl_cert_free(tsc->cert);
2013 tsc->cert = fsc->cert;
2014 if (!SSL_set_session_id_context(t, fsc->sid_ctx, (int)fsc->sid_ctx_length)) {
17dd65e6 2015 return 0;
69f68237 2016 }
17dd65e6
MC
2017
2018 return 1;
0f113f3e 2019}
d02b48c6 2020
58964a49 2021/* Fix this so it checks all the valid key/cert options */
0821bcd4 2022int SSL_CTX_check_private_key(const SSL_CTX *ctx)
0f113f3e 2023{
a230b26e 2024 if ((ctx == NULL) || (ctx->cert->key->x509 == NULL)) {
6849b73c 2025 ERR_raise(ERR_LIB_SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
26a7d938 2026 return 0;
0f113f3e
MC
2027 }
2028 if (ctx->cert->key->privatekey == NULL) {
6849b73c 2029 ERR_raise(ERR_LIB_SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
26a7d938 2030 return 0;
0f113f3e 2031 }
26a7d938
K
2032 return X509_check_private_key
2033 (ctx->cert->key->x509, ctx->cert->key->privatekey);
0f113f3e 2034}
d02b48c6 2035
58964a49 2036/* Fix this function so that it takes an optional type parameter */
0821bcd4 2037int SSL_check_private_key(const SSL *ssl)
0f113f3e 2038{
38b051a1
TM
2039 const SSL_CONNECTION *sc;
2040
2041 if ((sc = SSL_CONNECTION_FROM_CONST_SSL(ssl)) == NULL) {
6849b73c 2042 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
26a7d938 2043 return 0;
0f113f3e 2044 }
38b051a1 2045 if (sc->cert->key->x509 == NULL) {
6849b73c 2046 ERR_raise(ERR_LIB_SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
26a7d938 2047 return 0;
0f113f3e 2048 }
38b051a1 2049 if (sc->cert->key->privatekey == NULL) {
6849b73c 2050 ERR_raise(ERR_LIB_SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
26a7d938 2051 return 0;
0f113f3e 2052 }
38b051a1
TM
2053 return X509_check_private_key(sc->cert->key->x509,
2054 sc->cert->key->privatekey);
0f113f3e 2055}
d02b48c6 2056
07bbc92c
MC
2057int SSL_waiting_for_async(SSL *s)
2058{
38b051a1
TM
2059 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2060
2061 if (sc == NULL)
2062 return 0;
2063
2064 if (sc->job)
82676094
MC
2065 return 1;
2066
07bbc92c
MC
2067 return 0;
2068}
2069
ff75a257 2070int SSL_get_all_async_fds(SSL *s, OSSL_ASYNC_FD *fds, size_t *numfds)
f4da39d2 2071{
38b051a1
TM
2072 ASYNC_WAIT_CTX *ctx;
2073 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
ff75a257 2074
38b051a1
TM
2075 if (sc == NULL)
2076 return 0;
2077
2078 if ((ctx = sc->waitctx) == NULL)
ff75a257
MC
2079 return 0;
2080 return ASYNC_WAIT_CTX_get_all_fds(ctx, fds, numfds);
2081}
f4da39d2 2082
ff75a257
MC
2083int SSL_get_changed_async_fds(SSL *s, OSSL_ASYNC_FD *addfd, size_t *numaddfds,
2084 OSSL_ASYNC_FD *delfd, size_t *numdelfds)
2085{
38b051a1
TM
2086 ASYNC_WAIT_CTX *ctx;
2087 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
ff75a257 2088
38b051a1
TM
2089 if (sc == NULL)
2090 return 0;
2091
2092 if ((ctx = sc->waitctx) == NULL)
ff75a257
MC
2093 return 0;
2094 return ASYNC_WAIT_CTX_get_changed_fds(ctx, addfd, numaddfds, delfd,
2095 numdelfds);
f4da39d2
MC
2096}
2097
9f5a87fd
PY
2098int SSL_CTX_set_async_callback(SSL_CTX *ctx, SSL_async_callback_fn callback)
2099{
2100 ctx->async_cb = callback;
2101 return 1;
2102}
2103
2104int SSL_CTX_set_async_callback_arg(SSL_CTX *ctx, void *arg)
2105{
2106 ctx->async_cb_arg = arg;
2107 return 1;
2108}
2109
2110int SSL_set_async_callback(SSL *s, SSL_async_callback_fn callback)
2111{
38b051a1
TM
2112 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2113
2114 if (sc == NULL)
2115 return 0;
2116
2117 sc->async_cb = callback;
9f5a87fd
PY
2118 return 1;
2119}
2120
2121int SSL_set_async_callback_arg(SSL *s, void *arg)
2122{
38b051a1
TM
2123 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2124
2125 if (sc == NULL)
2126 return 0;
2127
2128 sc->async_cb_arg = arg;
9f5a87fd
PY
2129 return 1;
2130}
2131
2132int SSL_get_async_status(SSL *s, int *status)
2133{
38b051a1
TM
2134 ASYNC_WAIT_CTX *ctx;
2135 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
9f5a87fd 2136
38b051a1
TM
2137 if (sc == NULL)
2138 return 0;
2139
2140 if ((ctx = sc->waitctx) == NULL)
9f5a87fd
PY
2141 return 0;
2142 *status = ASYNC_WAIT_CTX_get_status(ctx);
2143 return 1;
2144}
2145
4f43d0e7 2146int SSL_accept(SSL *s)
0f113f3e 2147{
38b051a1 2148 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
03bacce8 2149
6d495cc4
HL
2150#ifndef OPENSSL_NO_QUIC
2151 if (IS_QUIC(s))
03bacce8 2152 return s->method->ssl_accept(s);
6292519c 2153#endif
38b051a1
TM
2154
2155 if (sc == NULL)
2156 return 0;
2157
2158 if (sc->handshake_func == NULL) {
0f113f3e
MC
2159 /* Not properly initialized yet */
2160 SSL_set_accept_state(s);
07bbc92c 2161 }
add2f5ca
MC
2162
2163 return SSL_do_handshake(s);
0f113f3e 2164}
d02b48c6 2165
4f43d0e7 2166int SSL_connect(SSL *s)
0f113f3e 2167{
38b051a1 2168 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
03bacce8 2169
6d495cc4
HL
2170#ifndef OPENSSL_NO_QUIC
2171 if (IS_QUIC(s))
03bacce8 2172 return s->method->ssl_connect(s);
6292519c 2173#endif
38b051a1
TM
2174
2175 if (sc == NULL)
2176 return 0;
2177
2178 if (sc->handshake_func == NULL) {
0f113f3e
MC
2179 /* Not properly initialized yet */
2180 SSL_set_connect_state(s);
add2f5ca 2181 }
b31b04d9 2182
add2f5ca 2183 return SSL_do_handshake(s);
0f113f3e 2184}
d02b48c6 2185
0821bcd4 2186long SSL_get_default_timeout(const SSL *s)
0f113f3e 2187{
f0131dc0 2188 return (long int)ossl_time2seconds(s->method->get_timeout());
0f113f3e
MC
2189}
2190
9f5a87fd
PY
2191static int ssl_async_wait_ctx_cb(void *arg)
2192{
2193 SSL *s = (SSL *)arg;
38b051a1 2194 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
9f5a87fd 2195
38b051a1
TM
2196 if (sc == NULL)
2197 return 0;
2198
2199 return sc->async_cb(s, sc->async_cb_arg);
9f5a87fd
PY
2200}
2201
7fecbf6f 2202static int ssl_start_async_job(SSL *s, struct ssl_async_args *args,
a230b26e
EK
2203 int (*func) (void *))
2204{
add2f5ca 2205 int ret;
38b051a1
TM
2206 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2207
2208 if (sc == NULL)
2209 return 0;
2210
2211 if (sc->waitctx == NULL) {
2212 sc->waitctx = ASYNC_WAIT_CTX_new();
2213 if (sc->waitctx == NULL)
ff75a257 2214 return -1;
38b051a1 2215 if (sc->async_cb != NULL
9f5a87fd 2216 && !ASYNC_WAIT_CTX_set_callback
38b051a1 2217 (sc->waitctx, ssl_async_wait_ctx_cb, s))
9f5a87fd 2218 return -1;
ff75a257 2219 }
07f620e3 2220
38b051a1
TM
2221 sc->rwstate = SSL_NOTHING;
2222 switch (ASYNC_start_job(&sc->job, sc->waitctx, &ret, func, args,
a230b26e 2223 sizeof(struct ssl_async_args))) {
add2f5ca 2224 case ASYNC_ERR:
38b051a1 2225 sc->rwstate = SSL_NOTHING;
6849b73c 2226 ERR_raise(ERR_LIB_SSL, SSL_R_FAILED_TO_INIT_ASYNC);
add2f5ca
MC
2227 return -1;
2228 case ASYNC_PAUSE:
38b051a1 2229 sc->rwstate = SSL_ASYNC_PAUSED;
add2f5ca 2230 return -1;
fc7f190c 2231 case ASYNC_NO_JOBS:
38b051a1 2232 sc->rwstate = SSL_ASYNC_NO_JOBS;
fc7f190c 2233 return -1;
add2f5ca 2234 case ASYNC_FINISH:
38b051a1 2235 sc->job = NULL;
add2f5ca
MC
2236 return ret;
2237 default:
38b051a1 2238 sc->rwstate = SSL_NOTHING;
6849b73c 2239 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
add2f5ca
MC
2240 /* Shouldn't happen */
2241 return -1;
2242 }
2243}
07bbc92c 2244
add2f5ca 2245static int ssl_io_intern(void *vargs)
07bbc92c
MC
2246{
2247 struct ssl_async_args *args;
2248 SSL *s;
2249 void *buf;
348240c6 2250 size_t num;
38b051a1 2251 SSL_CONNECTION *sc;
07bbc92c
MC
2252
2253 args = (struct ssl_async_args *)vargs;
2254 s = args->s;
2255 buf = args->buf;
2256 num = args->num;
38b051a1
TM
2257 if ((sc = SSL_CONNECTION_FROM_SSL(s)) == NULL)
2258 return -1;
2259
ec447924
MC
2260 switch (args->type) {
2261 case READFUNC:
38b051a1 2262 return args->f.func_read(s, buf, num, &sc->asyncrw);
ec447924 2263 case WRITEFUNC:
38b051a1 2264 return args->f.func_write(s, buf, num, &sc->asyncrw);
ec447924
MC
2265 case OTHERFUNC:
2266 return args->f.func_other(s);
2267 }
2268 return -1;
07bbc92c
MC
2269}
2270
4ee7d3f9 2271int ssl_read_internal(SSL *s, void *buf, size_t num, size_t *readbytes)
0f113f3e 2272{
38b051a1 2273 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
03bacce8 2274
6d495cc4
HL
2275#ifndef OPENSSL_NO_QUIC
2276 if (IS_QUIC(s))
03bacce8 2277 return s->method->ssl_read(s, buf, num, readbytes);
6292519c 2278#endif
38b051a1
TM
2279
2280 if (sc == NULL)
2281 return -1;
2282
2283 if (sc->handshake_func == NULL) {
6849b73c 2284 ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
0f113f3e
MC
2285 return -1;
2286 }
2287
38b051a1
TM
2288 if (sc->shutdown & SSL_RECEIVED_SHUTDOWN) {
2289 sc->rwstate = SSL_NOTHING;
4ee7d3f9 2290 return 0;
0f113f3e 2291 }
07bbc92c 2292
38b051a1
TM
2293 if (sc->early_data_state == SSL_EARLY_DATA_CONNECT_RETRY
2294 || sc->early_data_state == SSL_EARLY_DATA_ACCEPT_RETRY) {
6849b73c 2295 ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
0a5ece5b
MC
2296 return 0;
2297 }
564547e4
MC
2298 /*
2299 * If we are a client and haven't received the ServerHello etc then we
2300 * better do that
2301 */
38b051a1 2302 ossl_statem_check_finish_init(sc, 0);
0a5ece5b 2303
38b051a1 2304 if ((sc->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
add2f5ca 2305 struct ssl_async_args args;
eda75751 2306 int ret;
add2f5ca
MC
2307
2308 args.s = s;
2309 args.buf = buf;
2310 args.num = num;
ec447924
MC
2311 args.type = READFUNC;
2312 args.f.func_read = s->method->ssl_read;
add2f5ca 2313
eda75751 2314 ret = ssl_start_async_job(s, &args, ssl_io_intern);
38b051a1 2315 *readbytes = sc->asyncrw;
eda75751 2316 return ret;
07bbc92c 2317 } else {
54105ddd 2318 return s->method->ssl_read(s, buf, num, readbytes);
07bbc92c 2319 }
0f113f3e
MC
2320}
2321
4ee7d3f9 2322int SSL_read(SSL *s, void *buf, int num)
eda75751
MC
2323{
2324 int ret;
54105ddd 2325 size_t readbytes;
eda75751
MC
2326
2327 if (num < 0) {
6849b73c 2328 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);
eda75751
MC
2329 return -1;
2330 }
2331
4ee7d3f9 2332 ret = ssl_read_internal(s, buf, (size_t)num, &readbytes);
eda75751
MC
2333
2334 /*
2335 * The cast is safe here because ret should be <= INT_MAX because num is
2336 * <= INT_MAX
2337 */
2338 if (ret > 0)
54105ddd 2339 ret = (int)readbytes;
eda75751
MC
2340
2341 return ret;
2342}
2343
4ee7d3f9
KR
2344int SSL_read_ex(SSL *s, void *buf, size_t num, size_t *readbytes)
2345{
2346 int ret = ssl_read_internal(s, buf, num, readbytes);
2347
2348 if (ret < 0)
2349 ret = 0;
2350 return ret;
2351}
2352
f533fbd4 2353int SSL_read_early_data(SSL *s, void *buf, size_t num, size_t *readbytes)
d781d247
MC
2354{
2355 int ret;
38b051a1
TM
2356 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
2357
d6e7ebba
HL
2358 /* TODO(QUIC 0RTT): 0-RTT support */
2359 if (sc == NULL || !sc->server) {
6849b73c 2360 ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
f533fbd4 2361 return SSL_READ_EARLY_DATA_ERROR;
d781d247
MC
2362 }
2363
38b051a1 2364 switch (sc->early_data_state) {
d781d247
MC
2365 case SSL_EARLY_DATA_NONE:
2366 if (!SSL_in_before(s)) {
6849b73c 2367 ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
f533fbd4 2368 return SSL_READ_EARLY_DATA_ERROR;
d781d247
MC
2369 }
2370 /* fall through */
2371
2372 case SSL_EARLY_DATA_ACCEPT_RETRY:
38b051a1 2373 sc->early_data_state = SSL_EARLY_DATA_ACCEPTING;
d781d247
MC
2374 ret = SSL_accept(s);
2375 if (ret <= 0) {
2376 /* NBIO or error */
38b051a1 2377 sc->early_data_state = SSL_EARLY_DATA_ACCEPT_RETRY;
f533fbd4 2378 return SSL_READ_EARLY_DATA_ERROR;
d781d247
MC
2379 }
2380 /* fall through */
2381
2382 case SSL_EARLY_DATA_READ_RETRY:
38b051a1
TM
2383 if (sc->ext.early_data == SSL_EARLY_DATA_ACCEPTED) {
2384 sc->early_data_state = SSL_EARLY_DATA_READING;
d781d247
MC
2385 ret = SSL_read_ex(s, buf, num, readbytes);
2386 /*
ef6c191b
MC
2387 * State machine will update early_data_state to
2388 * SSL_EARLY_DATA_FINISHED_READING if we get an EndOfEarlyData
2389 * message
d781d247 2390 */
38b051a1 2391 if (ret > 0 || (ret <= 0 && sc->early_data_state
d781d247 2392 != SSL_EARLY_DATA_FINISHED_READING)) {
38b051a1 2393 sc->early_data_state = SSL_EARLY_DATA_READ_RETRY;
f533fbd4
MC
2394 return ret > 0 ? SSL_READ_EARLY_DATA_SUCCESS
2395 : SSL_READ_EARLY_DATA_ERROR;
d781d247
MC
2396 }
2397 } else {
38b051a1 2398 sc->early_data_state = SSL_EARLY_DATA_FINISHED_READING;
d781d247
MC
2399 }
2400 *readbytes = 0;
f533fbd4 2401 return SSL_READ_EARLY_DATA_FINISH;
d781d247
MC
2402
2403 default:
6849b73c 2404 ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
f533fbd4 2405 return SSL_READ_EARLY_DATA_ERROR;
d781d247
MC
2406 }
2407}
2408
f5b519c4 2409int SSL_get_early_data_status(const SSL *s)
1ea4d09a 2410{
38b051a1
TM
2411 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s);
2412
d6e7ebba 2413 /* TODO(QUIC 0RTT): 0-RTT support */
38b051a1
TM
2414 if (sc == NULL)
2415 return 0;
2416
2417 return sc->ext.early_data;
1ea4d09a
MC
2418}
2419
4ee7d3f9 2420static int ssl_peek_internal(SSL *s, void *buf, size_t num, size_t *readbytes)
0f113f3e 2421{
38b051a1 2422 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
03bacce8 2423
6d495cc4
HL
2424#ifndef OPENSSL_NO_QUIC
2425 if (IS_QUIC(s))
03bacce8 2426 return s->method->ssl_peek(s, buf, num, readbytes);
6292519c 2427#endif
38b051a1
TM
2428
2429 if (sc == NULL)
2430 return 0;
2431
2432 if (sc->handshake_func == NULL) {
6849b73c 2433 ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
0f113f3e
MC
2434 return -1;
2435 }
2436
38b051a1 2437 if (sc->shutdown & SSL_RECEIVED_SHUTDOWN) {
4ee7d3f9 2438 return 0;
0f113f3e 2439 }
38b051a1 2440 if ((sc->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
add2f5ca 2441 struct ssl_async_args args;
eda75751 2442 int ret;
0f113f3e 2443
add2f5ca
MC
2444 args.s = s;
2445 args.buf = buf;
2446 args.num = num;
ec447924
MC
2447 args.type = READFUNC;
2448 args.f.func_read = s->method->ssl_peek;
07bbc92c 2449
eda75751 2450 ret = ssl_start_async_job(s, &args, ssl_io_intern);
38b051a1 2451 *readbytes = sc->asyncrw;
eda75751 2452 return ret;
add2f5ca 2453 } else {
54105ddd 2454 return s->method->ssl_peek(s, buf, num, readbytes);
add2f5ca 2455 }
07bbc92c
MC
2456}
2457
4ee7d3f9 2458int SSL_peek(SSL *s, void *buf, int num)
7ee8627f
MC
2459{
2460 int ret;
4ee7d3f9 2461 size_t readbytes;
7ee8627f
MC
2462
2463 if (num < 0) {
6849b73c 2464 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);
7ee8627f
MC
2465 return -1;
2466 }
2467
4ee7d3f9 2468 ret = ssl_peek_internal(s, buf, (size_t)num, &readbytes);
7ee8627f
MC
2469
2470 /*
2471 * The cast is safe here because ret should be <= INT_MAX because num is
2472 * <= INT_MAX
2473 */
2474 if (ret > 0)
4ee7d3f9 2475 ret = (int)readbytes;
7ee8627f
MC
2476
2477 return ret;
2478}
2479
4ee7d3f9
KR
2480
2481int SSL_peek_ex(SSL *s, void *buf, size_t num, size_t *readbytes)
2482{
2483 int ret = ssl_peek_internal(s, buf, num, readbytes);
2484
2485 if (ret < 0)
2486 ret = 0;
2487 return ret;
2488}
2489
113be15a
HL
2490int ssl_write_internal(SSL *s, const void *buf, size_t num,
2491 uint64_t flags, size_t *written)
0f113f3e 2492{
38b051a1 2493 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
03bacce8 2494
6d495cc4
HL
2495#ifndef OPENSSL_NO_QUIC
2496 if (IS_QUIC(s))
113be15a 2497 return ossl_quic_write_flags(s, buf, num, flags, written);
6292519c 2498#endif
38b051a1
TM
2499
2500 if (sc == NULL)
2501 return 0;
2502
2503 if (sc->handshake_func == NULL) {
6849b73c 2504 ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
0f113f3e
MC
2505 return -1;
2506 }
2507
38b051a1
TM
2508 if (sc->shutdown & SSL_SENT_SHUTDOWN) {
2509 sc->rwstate = SSL_NOTHING;
6849b73c 2510 ERR_raise(ERR_LIB_SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
4ee7d3f9 2511 return -1;
0f113f3e 2512 }
07bbc92c 2513
113be15a
HL
2514 if (flags != 0) {
2515 ERR_raise(ERR_LIB_SSL, SSL_R_UNSUPPORTED_WRITE_FLAG);
2516 return -1;
2517 }
2518
38b051a1
TM
2519 if (sc->early_data_state == SSL_EARLY_DATA_CONNECT_RETRY
2520 || sc->early_data_state == SSL_EARLY_DATA_ACCEPT_RETRY
2521 || sc->early_data_state == SSL_EARLY_DATA_READ_RETRY) {
6849b73c 2522 ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
49e7fe12 2523 return 0;
0a5ece5b 2524 }
564547e4 2525 /* If we are a client and haven't sent the Finished we better do that */
38b051a1 2526 ossl_statem_check_finish_init(sc, 1);
49e7fe12 2527
38b051a1 2528 if ((sc->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
7ee8627f 2529 int ret;
add2f5ca
MC
2530 struct ssl_async_args args;
2531
2532 args.s = s;
2533 args.buf = (void *)buf;
2534 args.num = num;
ec447924
MC
2535 args.type = WRITEFUNC;
2536 args.f.func_write = s->method->ssl_write;
add2f5ca 2537
7ee8627f 2538 ret = ssl_start_async_job(s, &args, ssl_io_intern);
38b051a1 2539 *written = sc->asyncrw;
7ee8627f 2540 return ret;
07bbc92c 2541 } else {
7ee8627f 2542 return s->method->ssl_write(s, buf, num, written);
07bbc92c 2543 }
0f113f3e 2544}
d02b48c6 2545
7c3a7561
BP
2546ossl_ssize_t SSL_sendfile(SSL *s, int fd, off_t offset, size_t size, int flags)
2547{
2548 ossl_ssize_t ret;
38b051a1 2549 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
7c3a7561 2550
38b051a1
TM
2551 if (sc == NULL)
2552 return 0;
2553
2554 if (sc->handshake_func == NULL) {
6849b73c 2555 ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
7c3a7561
BP
2556 return -1;
2557 }
2558
38b051a1
TM
2559 if (sc->shutdown & SSL_SENT_SHUTDOWN) {
2560 sc->rwstate = SSL_NOTHING;
6849b73c 2561 ERR_raise(ERR_LIB_SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
7c3a7561
BP
2562 return -1;
2563 }
2564
38b051a1 2565 if (!BIO_get_ktls_send(sc->wbio)) {
6849b73c 2566 ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
7c3a7561
BP
2567 return -1;
2568 }
2569
2570 /* If we have an alert to send, lets send it */
73243502 2571 if (sc->s3.alert_dispatch > 0) {
7c3a7561
BP
2572 ret = (ossl_ssize_t)s->method->ssl_dispatch_alert(s);
2573 if (ret <= 0) {
2574 /* SSLfatal() already called if appropriate */
2575 return ret;
2576 }
2577 /* if it went, fall through and send more stuff */
2578 }
2579
38b051a1
TM
2580 sc->rwstate = SSL_WRITING;
2581 if (BIO_flush(sc->wbio) <= 0) {
2582 if (!BIO_should_retry(sc->wbio)) {
2583 sc->rwstate = SSL_NOTHING;
7c3a7561
BP
2584 } else {
2585#ifdef EAGAIN
2586 set_sys_error(EAGAIN);
2587#endif
2588 }
2589 return -1;
2590 }
2591
712c0942 2592#ifdef OPENSSL_NO_KTLS
fa7a8074
RL
2593 ERR_raise_data(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR,
2594 "can't call ktls_sendfile(), ktls disabled");
712c0942 2595 return -1;
7c3a7561 2596#else
712c0942 2597 ret = ktls_sendfile(SSL_get_wfd(s), fd, offset, size, flags);
7c3a7561
BP
2598 if (ret < 0) {
2599#if defined(EAGAIN) && defined(EINTR) && defined(EBUSY)
2600 if ((get_last_sys_error() == EAGAIN) ||
2601 (get_last_sys_error() == EINTR) ||
2602 (get_last_sys_error() == EBUSY))
38b051a1 2603 BIO_set_retry_write(sc->wbio);
7c3a7561
BP
2604 else
2605#endif
6849b73c 2606 ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
7c3a7561
BP
2607 return ret;
2608 }
38b051a1 2609 sc->rwstate = SSL_NOTHING;
7c3a7561 2610 return ret;
712c0942 2611#endif
7c3a7561
BP
2612}
2613
4ee7d3f9
KR
2614int SSL_write(SSL *s, const void *buf, int num)
2615{
2616 int ret;
2617 size_t written;
2618
2619 if (num < 0) {
6849b73c 2620 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);
4ee7d3f9
KR
2621 return -1;
2622 }
2623
113be15a 2624 ret = ssl_write_internal(s, buf, (size_t)num, 0, &written);
4ee7d3f9
KR
2625
2626 /*
2627 * The cast is safe here because ret should be <= INT_MAX because num is
2628 * <= INT_MAX
2629 */
2630 if (ret > 0)
2631 ret = (int)written;
2632
2633 return ret;
2634}
2635
2636int SSL_write_ex(SSL *s, const void *buf, size_t num, size_t *written)
2637{
113be15a
HL
2638 return SSL_write_ex2(s, buf, num, 0, written);
2639}
2640
2641int SSL_write_ex2(SSL *s, const void *buf, size_t num, uint64_t flags,
2642 size_t *written)
2643{
2644 int ret = ssl_write_internal(s, buf, num, flags, written);
4ee7d3f9
KR
2645
2646 if (ret < 0)
2647 ret = 0;
2648 return ret;
2649}
2650
0665b4ed 2651int SSL_write_early_data(SSL *s, const void *buf, size_t num, size_t *written)
49e7fe12 2652{
a0cb628b 2653 int ret, early_data_state;
2a8db717 2654 size_t writtmp;
f7414b08 2655 uint32_t partialwrite;
38b051a1
TM
2656 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
2657
44cb36d0 2658 /* TODO(QUIC 0RTT): This will need special handling for QUIC */
38b051a1
TM
2659 if (sc == NULL)
2660 return 0;
49e7fe12 2661
38b051a1 2662 switch (sc->early_data_state) {
49e7fe12 2663 case SSL_EARLY_DATA_NONE:
38b051a1 2664 if (sc->server
09f28874 2665 || !SSL_in_before(s)
38b051a1
TM
2666 || ((sc->session == NULL || sc->session->ext.max_early_data == 0)
2667 && (sc->psk_use_session_cb == NULL))) {
6849b73c 2668 ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
49e7fe12
MC
2669 return 0;
2670 }
2671 /* fall through */
2672
2673 case SSL_EARLY_DATA_CONNECT_RETRY:
38b051a1 2674 sc->early_data_state = SSL_EARLY_DATA_CONNECTING;
49e7fe12
MC
2675 ret = SSL_connect(s);
2676 if (ret <= 0) {
2677 /* NBIO or error */
38b051a1 2678 sc->early_data_state = SSL_EARLY_DATA_CONNECT_RETRY;
49e7fe12
MC
2679 return 0;
2680 }
2681 /* fall through */
2682
2683 case SSL_EARLY_DATA_WRITE_RETRY:
38b051a1 2684 sc->early_data_state = SSL_EARLY_DATA_WRITING;
f7414b08
MC
2685 /*
2686 * We disable partial write for early data because we don't keep track
2687 * of how many bytes we've written between the SSL_write_ex() call and
2688 * the flush if the flush needs to be retried)
2689 */
38b051a1
TM
2690 partialwrite = sc->mode & SSL_MODE_ENABLE_PARTIAL_WRITE;
2691 sc->mode &= ~SSL_MODE_ENABLE_PARTIAL_WRITE;
2a8db717 2692 ret = SSL_write_ex(s, buf, num, &writtmp);
38b051a1 2693 sc->mode |= partialwrite;
2a8db717 2694 if (!ret) {
38b051a1 2695 sc->early_data_state = SSL_EARLY_DATA_WRITE_RETRY;
2a8db717
MC
2696 return ret;
2697 }
38b051a1 2698 sc->early_data_state = SSL_EARLY_DATA_WRITE_FLUSH;
2a8db717
MC
2699 /* fall through */
2700
2701 case SSL_EARLY_DATA_WRITE_FLUSH:
2702 /* The buffering BIO is still in place so we need to flush it */
38b051a1 2703 if (statem_flush(sc) != 1)
2a8db717 2704 return 0;
2a8db717 2705 *written = num;
38b051a1 2706 sc->early_data_state = SSL_EARLY_DATA_WRITE_RETRY;
2a8db717 2707 return 1;
49e7fe12 2708
116d0da5 2709 case SSL_EARLY_DATA_FINISHED_READING:
a0cb628b 2710 case SSL_EARLY_DATA_READ_RETRY:
38b051a1 2711 early_data_state = sc->early_data_state;
09f28874 2712 /* We are a server writing to an unauthenticated client */
38b051a1 2713 sc->early_data_state = SSL_EARLY_DATA_UNAUTH_WRITING;
09f28874 2714 ret = SSL_write_ex(s, buf, num, written);
5fe37157
MC
2715 /* The buffering BIO is still in place */
2716 if (ret)
38b051a1
TM
2717 (void)BIO_flush(sc->wbio);
2718 sc->early_data_state = early_data_state;
09f28874
MC
2719 return ret;
2720
49e7fe12 2721 default:
6849b73c 2722 ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
49e7fe12
MC
2723 return 0;
2724 }
2725}
2726
4f43d0e7 2727int SSL_shutdown(SSL *s)
0f113f3e
MC
2728{
2729 /*
2730 * Note that this function behaves differently from what one might
2731 * expect. Return values are 0 for no success (yet), 1 for success; but
2732 * calling it once is usually not enough, even if blocking I/O is used
2733 * (see ssl3_shutdown).
2734 */
38b051a1 2735 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
e8043229 2736
22b1a96f
HL
2737#ifndef OPENSSL_NO_QUIC
2738 if (IS_QUIC(s))
6d495cc4 2739 return ossl_quic_conn_shutdown(s, 0, NULL, 0);
e8043229 2740#endif
0f113f3e 2741
38b051a1
TM
2742 if (sc == NULL)
2743 return -1;
2744
2745 if (sc->handshake_func == NULL) {
6849b73c 2746 ERR_raise(ERR_LIB_SSL, SSL_R_UNINITIALIZED);
0f113f3e
MC
2747 return -1;
2748 }
2749
64f9f406 2750 if (!SSL_in_init(s)) {
38b051a1 2751 if ((sc->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
64f9f406 2752 struct ssl_async_args args;
ec447924 2753
09134f18 2754 memset(&args, 0, sizeof(args));
64f9f406
MC
2755 args.s = s;
2756 args.type = OTHERFUNC;
2757 args.f.func_other = s->method->ssl_shutdown;
ec447924 2758
64f9f406
MC
2759 return ssl_start_async_job(s, &args, ssl_io_intern);
2760 } else {
2761 return s->method->ssl_shutdown(s);
2762 }
ec447924 2763 } else {
6849b73c 2764 ERR_raise(ERR_LIB_SSL, SSL_R_SHUTDOWN_WHILE_IN_INIT);
64f9f406 2765 return -1;
ec447924 2766 }
0f113f3e 2767}
d02b48c6 2768
4fbfe86a 2769int SSL_key_update(SSL *s, int updatetype)
44c04a2e 2770{
38b051a1
TM
2771 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2772
2525109f
HL
2773#ifndef OPENSSL_NO_QUIC
2774 if (IS_QUIC(s))
2775 return ossl_quic_key_update(s, updatetype);
2776#endif
2777
38b051a1
TM
2778 if (sc == NULL)
2779 return 0;
2780
2781 if (!SSL_CONNECTION_IS_TLS13(sc)) {
6849b73c 2782 ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION);
44c04a2e
MC
2783 return 0;
2784 }
2785
2786 if (updatetype != SSL_KEY_UPDATE_NOT_REQUESTED
2787 && updatetype != SSL_KEY_UPDATE_REQUESTED) {
6849b73c 2788 ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_KEY_UPDATE_TYPE);
44c04a2e
MC
2789 return 0;
2790 }
2791
2792 if (!SSL_is_init_finished(s)) {
6849b73c 2793 ERR_raise(ERR_LIB_SSL, SSL_R_STILL_IN_INIT);
44c04a2e
MC
2794 return 0;
2795 }
2796
38b051a1 2797 if (RECORD_LAYER_write_pending(&sc->rlayer)) {
3bec4851
MC
2798 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_WRITE_RETRY);
2799 return 0;
2800 }
2801
38b051a1
TM
2802 ossl_statem_set_in_init(sc, 1);
2803 sc->key_update = updatetype;
44c04a2e
MC
2804 return 1;
2805}
2806
3499327b 2807int SSL_get_key_update_type(const SSL *s)
53d1d07d 2808{
38b051a1
TM
2809 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
2810
2525109f
HL
2811#ifndef OPENSSL_NO_QUIC
2812 if (IS_QUIC(s))
2813 return ossl_quic_get_key_update_type(s);
2814#endif
2815
38b051a1
TM
2816 if (sc == NULL)
2817 return 0;
2818
2819 return sc->key_update;
53d1d07d
MC
2820}
2821
55373bfd
RS
2822/*
2823 * Can we accept a renegotiation request? If yes, set the flag and
2824 * return 1 if yes. If not, raise error and return 0.
2825 */
38b051a1 2826static int can_renegotiate(const SSL_CONNECTION *sc)
0f113f3e 2827{
38b051a1 2828 if (SSL_CONNECTION_IS_TLS13(sc)) {
6849b73c 2829 ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION);
2c0980d2 2830 return 0;
44c04a2e 2831 }
cda6b998 2832
38b051a1 2833 if ((sc->options & SSL_OP_NO_RENEGOTIATION) != 0) {
6849b73c 2834 ERR_raise(ERR_LIB_SSL, SSL_R_NO_RENEGOTIATION);
db0f35dd
TS
2835 return 0;
2836 }
44959ee4 2837
55373bfd
RS
2838 return 1;
2839}
2840
2841int SSL_renegotiate(SSL *s)
2842{
38b051a1
TM
2843 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
2844
2845 if (sc == NULL)
55373bfd
RS
2846 return 0;
2847
38b051a1
TM
2848 if (!can_renegotiate(sc))
2849 return 0;
2850
2851 sc->renegotiate = 1;
2852 sc->new_session = 1;
26a7d938 2853 return s->method->ssl_renegotiate(s);
0f113f3e 2854}
d02b48c6 2855
44959ee4 2856int SSL_renegotiate_abbreviated(SSL *s)
0f113f3e 2857{
38b051a1
TM
2858 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
2859
2860 if (sc == NULL)
2861 return 0;
2862
2863 if (!can_renegotiate(sc))
2c0980d2 2864 return 0;
c519e89f 2865
38b051a1
TM
2866 sc->renegotiate = 1;
2867 sc->new_session = 0;
26a7d938 2868 return s->method->ssl_renegotiate(s);
0f113f3e 2869}
44959ee4 2870
3499327b 2871int SSL_renegotiate_pending(const SSL *s)
0f113f3e 2872{
38b051a1
TM
2873 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
2874
2875 if (sc == NULL)
2876 return 0;
2877
0f113f3e
MC
2878 /*
2879 * becomes true when negotiation is requested; false again once a
2880 * handshake has finished
2881 */
38b051a1 2882 return (sc->renegotiate != 0);
0f113f3e
MC
2883}
2884
3bfacb5f
BK
2885int SSL_new_session_ticket(SSL *s)
2886{
38b051a1
TM
2887 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
2888
2889 if (sc == NULL)
2890 return 0;
2891
7c73fefe 2892 /* If we are in init because we're sending tickets, okay to send more. */
38b051a1
TM
2893 if ((SSL_in_init(s) && sc->ext.extra_tickets_expected == 0)
2894 || SSL_IS_FIRST_HANDSHAKE(sc) || !sc->server
2895 || !SSL_CONNECTION_IS_TLS13(sc))
35774d55 2896 return 0;
38b051a1
TM
2897 sc->ext.extra_tickets_expected++;
2898 if (!RECORD_LAYER_write_pending(&sc->rlayer) && !SSL_in_init(s))
2899 ossl_statem_set_in_init(sc, 1);
35774d55 2900 return 1;
3bfacb5f
BK
2901}
2902
0f113f3e 2903long SSL_ctrl(SSL *s, int cmd, long larg, void *parg)
c5b882a8
HL
2904{
2905 return ossl_ctrl_internal(s, cmd, larg, parg, /*no_quic=*/0);
2906}
2907
2908long ossl_ctrl_internal(SSL *s, int cmd, long larg, void *parg, int no_quic)
0f113f3e
MC
2909{
2910 long l;
8dc82c02 2911 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
38b051a1 2912
c5b882a8
HL
2913 /*
2914 * Routing of ctrl calls for QUIC is a little counterintuitive:
2915 *
2916 * - Firstly (no_quic=0), we pass the ctrl directly to our QUIC
2917 * implementation in case it wants to handle the ctrl specially.
2918 *
2919 * - If our QUIC implementation does not care about the ctrl, it
2920 * will reenter this function with no_quic=1 and we will try to handle
2921 * it directly using the QCSO SSL object stub (not the handshake layer
2922 * SSL object). This is important for e.g. the version configuration
2923 * ctrls below, which must use s->defltmeth (and not sc->defltmeth).
2924 *
2925 * - If we don't handle a ctrl here specially, then processing is
2926 * redirected to the handshake layer SSL object.
2927 */
2928 if (!no_quic && IS_QUIC(s))
2929 return s->method->ssl_ctrl(s, cmd, larg, parg);
0f113f3e 2930
5c16e9d3
HL
2931 if (sc == NULL)
2932 return 0;
2933
0f113f3e
MC
2934 switch (cmd) {
2935 case SSL_CTRL_GET_READ_AHEAD:
38b051a1 2936 return RECORD_LAYER_get_read_ahead(&sc->rlayer);
0f113f3e 2937 case SSL_CTRL_SET_READ_AHEAD:
38b051a1
TM
2938 l = RECORD_LAYER_get_read_ahead(&sc->rlayer);
2939 RECORD_LAYER_set_read_ahead(&sc->rlayer, larg);
26a7d938 2940 return l;
0f113f3e 2941
0f113f3e 2942 case SSL_CTRL_MODE:
4566dae7
MC
2943 {
2944 OSSL_PARAM options[2], *opts = options;
2945
2946 sc->mode |= larg;
2947
2948 *opts++ = OSSL_PARAM_construct_uint32(OSSL_LIBSSL_RECORD_LAYER_PARAM_MODE,
2949 &sc->mode);
2950 *opts = OSSL_PARAM_construct_end();
2951
2952 /* Ignore return value */
2953 sc->rlayer.rrlmethod->set_options(sc->rlayer.rrl, options);
2954
2955 return sc->mode;
2956 }
0f113f3e 2957 case SSL_CTRL_CLEAR_MODE:
38b051a1 2958 return (sc->mode &= ~larg);
0f113f3e 2959 case SSL_CTRL_GET_MAX_CERT_LIST:
38b051a1 2960 return (long)sc->max_cert_list;
0f113f3e 2961 case SSL_CTRL_SET_MAX_CERT_LIST:
348240c6
MC
2962 if (larg < 0)
2963 return 0;
38b051a1
TM
2964 l = (long)sc->max_cert_list;
2965 sc->max_cert_list = (size_t)larg;
348240c6 2966 return l;
0f113f3e 2967 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
c5b882a8 2968 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
0f113f3e 2969 return 0;
50ec7505 2970#ifndef OPENSSL_NO_KTLS
38b051a1 2971 if (sc->wbio != NULL && BIO_get_ktls_send(sc->wbio))
50ec7505
BP
2972 return 0;
2973#endif /* OPENSSL_NO_KTLS */
38b051a1
TM
2974 sc->max_send_fragment = larg;
2975 if (sc->max_send_fragment < sc->split_send_fragment)
2976 sc->split_send_fragment = sc->max_send_fragment;
435d88d7 2977 sc->rlayer.wrlmethod->set_max_frag_len(sc->rlayer.wrl, larg);
d102d9df
MC
2978 return 1;
2979 case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT:
c5b882a8 2980 if ((size_t)larg > sc->max_send_fragment || larg == 0)
d102d9df 2981 return 0;
38b051a1 2982 sc->split_send_fragment = larg;
0f113f3e 2983 return 1;
d102d9df 2984 case SSL_CTRL_SET_MAX_PIPELINES:
c5b882a8 2985 if (larg < 1 || larg > SSL_MAX_PIPELINES)
d102d9df 2986 return 0;
38b051a1 2987 sc->max_pipelines = larg;
cffafb5f
MC
2988 if (sc->rlayer.rrlmethod->set_max_pipelines != NULL)
2989 sc->rlayer.rrlmethod->set_max_pipelines(sc->rlayer.rrl, (size_t)larg);
07077415 2990 return 1;
0f113f3e 2991 case SSL_CTRL_GET_RI_SUPPORT:
38b051a1 2992 return sc->s3.send_connection_binding;
dfb39f73 2993 case SSL_CTRL_SET_RETRY_VERIFY:
38b051a1 2994 sc->rwstate = SSL_RETRY_VERIFY;
dfb39f73 2995 return 1;
0f113f3e 2996 case SSL_CTRL_CERT_FLAGS:
38b051a1 2997 return (sc->cert->cert_flags |= larg);
0f113f3e 2998 case SSL_CTRL_CLEAR_CERT_FLAGS:
38b051a1 2999 return (sc->cert->cert_flags &= ~larg);
0f113f3e
MC
3000
3001 case SSL_CTRL_GET_RAW_CIPHERLIST:
3002 if (parg) {
38b051a1 3003 if (sc->s3.tmp.ciphers_raw == NULL)
0f113f3e 3004 return 0;
38b051a1
TM
3005 *(unsigned char **)parg = sc->s3.tmp.ciphers_raw;
3006 return (int)sc->s3.tmp.ciphers_rawlen;
e9fa092e
EK
3007 } else {
3008 return TLS_CIPHER_LEN;
3009 }
c5364614 3010 case SSL_CTRL_GET_EXTMS_SUPPORT:
38b051a1 3011 if (!sc->session || SSL_in_init(s) || ossl_statem_get_in_handshake(sc))
a230b26e 3012 return -1;
38b051a1 3013 if (sc->session->flags & SSL_SESS_FLAG_EXTMS)
c5364614
DSH
3014 return 1;
3015 else
3016 return 0;
7946ab33 3017 case SSL_CTRL_SET_MIN_PROTO_VERSION:
d6e7ebba 3018 return ssl_check_allowed_versions(larg, sc->max_proto_version)
a7f41885 3019 && ssl_set_version_bound(s->defltmeth->version, (int)larg,
38b051a1 3020 &sc->min_proto_version);
3edabd3c 3021 case SSL_CTRL_GET_MIN_PROTO_VERSION:
38b051a1 3022 return sc->min_proto_version;
7946ab33 3023 case SSL_CTRL_SET_MAX_PROTO_VERSION:
d6e7ebba 3024 return ssl_check_allowed_versions(sc->min_proto_version, larg)
a7f41885 3025 && ssl_set_version_bound(s->defltmeth->version, (int)larg,
38b051a1 3026 &sc->max_proto_version);
3edabd3c 3027 case SSL_CTRL_GET_MAX_PROTO_VERSION:
38b051a1 3028 return sc->max_proto_version;
0f113f3e 3029 default:
c5b882a8
HL
3030 if (IS_QUIC(s))
3031 return SSL_ctrl((SSL *)sc, cmd, larg, parg);
3032 else
3033 return s->method->ssl_ctrl(s, cmd, larg, parg);
0f113f3e
MC
3034 }
3035}
3036
3037long SSL_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
3038{
63dfde87 3039 return s->method->ssl_callback_ctrl(s, cmd, fp);
0f113f3e 3040}
d3442bc7 3041
3c1d6bbc 3042LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
0f113f3e
MC
3043{
3044 return ctx->sessions;
3045}
3046
acce0557
P
3047static int ssl_tsan_load(SSL_CTX *ctx, TSAN_QUALIFIER int *stat)
3048{
3049 int res = 0;
3050
3051 if (ssl_tsan_lock(ctx)) {
3052 res = tsan_load(stat);
3053 ssl_tsan_unlock(ctx);
3054 }
3055 return res;
3056}
3057
0f113f3e
MC
3058long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
3059{
3060 long l;
3061 /* For some cases with ctx == NULL perform syntax checks */
3062 if (ctx == NULL) {
3063 switch (cmd) {
de4d764e 3064 case SSL_CTRL_SET_GROUPS_LIST:
260009d8 3065 return tls1_set_groups_list(ctx, NULL, NULL, parg);
0f113f3e
MC
3066 case SSL_CTRL_SET_SIGALGS_LIST:
3067 case SSL_CTRL_SET_CLIENT_SIGALGS_LIST:
4169d58c 3068 return tls1_set_sigalgs_list(ctx, NULL, parg, 0);
0f113f3e
MC
3069 default:
3070 return 0;
3071 }
3072 }
3073
3074 switch (cmd) {
3075 case SSL_CTRL_GET_READ_AHEAD:
26a7d938 3076 return ctx->read_ahead;
0f113f3e
MC
3077 case SSL_CTRL_SET_READ_AHEAD:
3078 l = ctx->read_ahead;
3079 ctx->read_ahead = larg;
26a7d938 3080 return l;
0f113f3e
MC
3081
3082 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
3083 ctx->msg_callback_arg = parg;
3084 return 1;
3085
3086 case SSL_CTRL_GET_MAX_CERT_LIST:
26a7d938 3087 return (long)ctx->max_cert_list;
0f113f3e 3088 case SSL_CTRL_SET_MAX_CERT_LIST:
348240c6
MC
3089 if (larg < 0)
3090 return 0;
3091 l = (long)ctx->max_cert_list;
3092 ctx->max_cert_list = (size_t)larg;
3093 return l;
0f113f3e
MC
3094
3095 case SSL_CTRL_SET_SESS_CACHE_SIZE:
348240c6
MC
3096 if (larg < 0)
3097 return 0;
3098 l = (long)ctx->session_cache_size;
3099 ctx->session_cache_size = (size_t)larg;
3100 return l;
0f113f3e 3101 case SSL_CTRL_GET_SESS_CACHE_SIZE:
26a7d938 3102 return (long)ctx->session_cache_size;
0f113f3e
MC
3103 case SSL_CTRL_SET_SESS_CACHE_MODE:
3104 l = ctx->session_cache_mode;
3105 ctx->session_cache_mode = larg;
26a7d938 3106 return l;
0f113f3e 3107 case SSL_CTRL_GET_SESS_CACHE_MODE:
26a7d938 3108 return ctx->session_cache_mode;
0f113f3e
MC
3109
3110 case SSL_CTRL_SESS_NUMBER:
26a7d938 3111 return lh_SSL_SESSION_num_items(ctx->sessions);
0f113f3e 3112 case SSL_CTRL_SESS_CONNECT:
acce0557 3113 return ssl_tsan_load(ctx, &ctx->stats.sess_connect);
0f113f3e 3114 case SSL_CTRL_SESS_CONNECT_GOOD:
acce0557 3115 return ssl_tsan_load(ctx, &ctx->stats.sess_connect_good);
0f113f3e 3116 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
acce0557 3117 return ssl_tsan_load(ctx, &ctx->stats.sess_connect_renegotiate);
0f113f3e 3118 case SSL_CTRL_SESS_ACCEPT:
acce0557 3119 return ssl_tsan_load(ctx, &ctx->stats.sess_accept);
0f113f3e 3120 case SSL_CTRL_SESS_ACCEPT_GOOD:
acce0557 3121 return ssl_tsan_load(ctx, &ctx->stats.sess_accept_good);
0f113f3e 3122 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
acce0557 3123 return ssl_tsan_load(ctx, &ctx->stats.sess_accept_renegotiate);
0f113f3e 3124 case SSL_CTRL_SESS_HIT:
acce0557 3125 return ssl_tsan_load(ctx, &ctx->stats.sess_hit);
0f113f3e 3126 case SSL_CTRL_SESS_CB_HIT:
acce0557 3127 return ssl_tsan_load(ctx, &ctx->stats.sess_cb_hit);
0f113f3e 3128 case SSL_CTRL_SESS_MISSES:
acce0557 3129 return ssl_tsan_load(ctx, &ctx->stats.sess_miss);
0f113f3e 3130 case SSL_CTRL_SESS_TIMEOUTS:
acce0557 3131 return ssl_tsan_load(ctx, &ctx->stats.sess_timeout);
0f113f3e 3132 case SSL_CTRL_SESS_CACHE_FULL:
acce0557 3133 return ssl_tsan_load(ctx, &ctx->stats.sess_cache_full);
0f113f3e
MC
3134 case SSL_CTRL_MODE:
3135 return (ctx->mode |= larg);
3136 case SSL_CTRL_CLEAR_MODE:
3137 return (ctx->mode &= ~larg);
3138 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
3139 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
3140 return 0;
3141 ctx->max_send_fragment = larg;
d102d9df 3142 if (ctx->max_send_fragment < ctx->split_send_fragment)
bfb155c1 3143 ctx->split_send_fragment = ctx->max_send_fragment;
0f113f3e 3144 return 1;
d102d9df 3145 case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT:
7ee8627f 3146 if ((size_t)larg > ctx->max_send_fragment || larg == 0)
d102d9df
MC
3147 return 0;
3148 ctx->split_send_fragment = larg;
3149 return 1;
3150 case SSL_CTRL_SET_MAX_PIPELINES:
3151 if (larg < 1 || larg > SSL_MAX_PIPELINES)
3152 return 0;
3153 ctx->max_pipelines = larg;
07077415 3154 return 1;
0f113f3e
MC
3155 case SSL_CTRL_CERT_FLAGS:
3156 return (ctx->cert->cert_flags |= larg);
3157 case SSL_CTRL_CLEAR_CERT_FLAGS:
3158 return (ctx->cert->cert_flags &= ~larg);
7946ab33 3159 case SSL_CTRL_SET_MIN_PROTO_VERSION:
d6e7ebba 3160 return ssl_check_allowed_versions(larg, ctx->max_proto_version)
c8feba72
BK
3161 && ssl_set_version_bound(ctx->method->version, (int)larg,
3162 &ctx->min_proto_version);
3edabd3c
CH
3163 case SSL_CTRL_GET_MIN_PROTO_VERSION:
3164 return ctx->min_proto_version;
7946ab33 3165 case SSL_CTRL_SET_MAX_PROTO_VERSION:
d6e7ebba 3166 return ssl_check_allowed_versions(ctx->min_proto_version, larg)
c8feba72
BK
3167 && ssl_set_version_bound(ctx->method->version, (int)larg,
3168 &ctx->max_proto_version);
3edabd3c
CH
3169 case SSL_CTRL_GET_MAX_PROTO_VERSION:
3170 return ctx->max_proto_version;
0f113f3e 3171 default:
26a7d938 3172 return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
0f113f3e
MC
3173 }
3174}
3175
3176long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
3177{
3178 switch (cmd) {
3179 case SSL_CTRL_SET_MSG_CALLBACK:
3180 ctx->msg_callback = (void (*)
3181 (int write_p, int version, int content_type,
3182 const void *buf, size_t len, SSL *ssl,
3183 void *arg))(fp);
3184 return 1;
3185
3186 default:
26a7d938 3187 return ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp);
0f113f3e
MC
3188 }
3189}
d3442bc7 3190
ccd86b68 3191int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b)
0f113f3e 3192{
90d9e49a
DSH
3193 if (a->id > b->id)
3194 return 1;
3195 if (a->id < b->id)
3196 return -1;
3197 return 0;
0f113f3e
MC
3198}
3199
3200int ssl_cipher_ptr_id_cmp(const SSL_CIPHER *const *ap,
3201 const SSL_CIPHER *const *bp)
3202{
90d9e49a
DSH
3203 if ((*ap)->id > (*bp)->id)
3204 return 1;
3205 if ((*ap)->id < (*bp)->id)
3206 return -1;
3207 return 0;
0f113f3e 3208}
d02b48c6 3209
38b051a1
TM
3210/*
3211 * return a STACK of the ciphers available for the SSL and in order of
3212 * preference
3213 */
0821bcd4 3214STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
0f113f3e 3215{
38b051a1
TM
3216 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
3217
3218 if (sc != NULL) {
3219 if (sc->cipher_list != NULL) {
3220 return sc->cipher_list;
0f113f3e 3221 } else if ((s->ctx != NULL) && (s->ctx->cipher_list != NULL)) {
26a7d938 3222 return s->ctx->cipher_list;
0f113f3e
MC
3223 }
3224 }
26a7d938 3225 return NULL;
0f113f3e
MC
3226}
3227
831eef2c
NM
3228STACK_OF(SSL_CIPHER) *SSL_get_client_ciphers(const SSL *s)
3229{
38b051a1
TM
3230 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
3231
3232 if (sc == NULL || !sc->server)
831eef2c 3233 return NULL;
38b051a1 3234 return sc->peer_ciphers;
831eef2c
NM
3235}
3236
8b8e5bed 3237STACK_OF(SSL_CIPHER) *SSL_get1_supported_ciphers(SSL *s)
0f113f3e
MC
3238{
3239 STACK_OF(SSL_CIPHER) *sk = NULL, *ciphers;
3240 int i;
38b051a1
TM
3241 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
3242
3243 if (sc == NULL)
3244 return NULL;
1d0c08b4 3245
0f113f3e
MC
3246 ciphers = SSL_get_ciphers(s);
3247 if (!ciphers)
3248 return NULL;
38b051a1 3249 if (!ssl_set_client_disabled(sc))
1d0c08b4 3250 return NULL;
0f113f3e
MC
3251 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
3252 const SSL_CIPHER *c = sk_SSL_CIPHER_value(ciphers, i);
38b051a1 3253 if (!ssl_cipher_disabled(sc, c, SSL_SECOP_CIPHER_SUPPORTED, 0)) {
0f113f3e
MC
3254 if (!sk)
3255 sk = sk_SSL_CIPHER_new_null();
3256 if (!sk)
3257 return NULL;
3258 if (!sk_SSL_CIPHER_push(sk, c)) {
3259 sk_SSL_CIPHER_free(sk);
3260 return NULL;
3261 }
3262 }
3263 }
3264 return sk;
3265}
8b8e5bed 3266
4f43d0e7 3267/** return a STACK of the ciphers available for the SSL and in order of
d02b48c6 3268 * algorithm id */
38b051a1 3269STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL_CONNECTION *s)
0f113f3e
MC
3270{
3271 if (s != NULL) {
38b051a1 3272 if (s->cipher_list_by_id != NULL)
26a7d938 3273 return s->cipher_list_by_id;
38b051a1
TM
3274 else if (s->ssl.ctx != NULL
3275 && s->ssl.ctx->cipher_list_by_id != NULL)
3276 return s->ssl.ctx->cipher_list_by_id;
0f113f3e 3277 }
26a7d938 3278 return NULL;
0f113f3e 3279}
d02b48c6 3280
4f43d0e7 3281/** The old interface to get the same thing as SSL_get_ciphers() */
0f113f3e
MC
3282const char *SSL_get_cipher_list(const SSL *s, int n)
3283{
4a640fb6 3284 const SSL_CIPHER *c;
0f113f3e
MC
3285 STACK_OF(SSL_CIPHER) *sk;
3286
3287 if (s == NULL)
26a7d938 3288 return NULL;
0f113f3e
MC
3289 sk = SSL_get_ciphers(s);
3290 if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n))
26a7d938 3291 return NULL;
0f113f3e
MC
3292 c = sk_SSL_CIPHER_value(sk, n);
3293 if (c == NULL)
26a7d938
K
3294 return NULL;
3295 return c->name;
0f113f3e 3296}
d02b48c6 3297
9d5ac953
KY
3298/** return a STACK of the ciphers available for the SSL_CTX and in order of
3299 * preference */
3300STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx)
3301{
3302 if (ctx != NULL)
3303 return ctx->cipher_list;
3304 return NULL;
3305}
3306
3c83c5ba
SR
3307/*
3308 * Distinguish between ciphers controlled by set_ciphersuite() and
3309 * set_cipher_list() when counting.
3310 */
3311static int cipher_list_tls12_num(STACK_OF(SSL_CIPHER) *sk)
3312{
3313 int i, num = 0;
3314 const SSL_CIPHER *c;
3315
3316 if (sk == NULL)
3317 return 0;
3318 for (i = 0; i < sk_SSL_CIPHER_num(sk); ++i) {
3319 c = sk_SSL_CIPHER_value(sk, i);
3320 if (c->min_tls >= TLS1_3_VERSION)
3321 continue;
3322 num++;
3323 }
3324 return num;
3325}
3326
25f923dd 3327/** specify the ciphers to be used by default by the SSL_CTX */
018e57c7 3328int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
0f113f3e
MC
3329{
3330 STACK_OF(SSL_CIPHER) *sk;
3331
a68eee67 3332 sk = ssl_create_cipher_list(ctx, ctx->tls13_ciphersuites,
f865b081
MC
3333 &ctx->cipher_list, &ctx->cipher_list_by_id, str,
3334 ctx->cert);
0f113f3e
MC
3335 /*
3336 * ssl_create_cipher_list may return an empty stack if it was unable to
3337 * find a cipher matching the given rule string (for example if the rule
3338 * string specifies a cipher which has been disabled). This is not an
3339 * error as far as ssl_create_cipher_list is concerned, and hence
3340 * ctx->cipher_list and ctx->cipher_list_by_id has been updated.
3341 */
3342 if (sk == NULL)
3343 return 0;
3c83c5ba 3344 else if (cipher_list_tls12_num(sk) == 0) {
6849b73c 3345 ERR_raise(ERR_LIB_SSL, SSL_R_NO_CIPHER_MATCH);
0f113f3e
MC
3346 return 0;
3347 }
3348 return 1;
3349}
d02b48c6 3350
4f43d0e7 3351/** specify the ciphers to be used by the SSL */
0f113f3e
MC
3352int SSL_set_cipher_list(SSL *s, const char *str)
3353{
3354 STACK_OF(SSL_CIPHER) *sk;
38b051a1 3355 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
0f113f3e 3356
38b051a1
TM
3357 if (sc == NULL)
3358 return 0;
3359
3360 sk = ssl_create_cipher_list(s->ctx, sc->tls13_ciphersuites,
3361 &sc->cipher_list, &sc->cipher_list_by_id, str,
3362 sc->cert);
0f113f3e
MC
3363 /* see comment in SSL_CTX_set_cipher_list */
3364 if (sk == NULL)
3365 return 0;
3c83c5ba 3366 else if (cipher_list_tls12_num(sk) == 0) {
6849b73c 3367 ERR_raise(ERR_LIB_SSL, SSL_R_NO_CIPHER_MATCH);
0f113f3e
MC
3368 return 0;
3369 }
3370 return 1;
3371}
d02b48c6 3372
a216df59 3373char *SSL_get_shared_ciphers(const SSL *s, char *buf, int size)
0f113f3e
MC
3374{
3375 char *p;
a216df59 3376 STACK_OF(SSL_CIPHER) *clntsk, *srvrsk;
4a640fb6 3377 const SSL_CIPHER *c;
0f113f3e 3378 int i;
38b051a1
TM
3379 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
3380
3381 if (sc == NULL)
3382 return NULL;
0f113f3e 3383
38b051a1
TM
3384 if (!sc->server
3385 || sc->peer_ciphers == NULL
a216df59 3386 || size < 2)
26a7d938 3387 return NULL;
0f113f3e
MC
3388
3389 p = buf;
38b051a1 3390 clntsk = sc->peer_ciphers;
a216df59
MC
3391 srvrsk = SSL_get_ciphers(s);
3392 if (clntsk == NULL || srvrsk == NULL)
3393 return NULL;
0f113f3e 3394
a216df59 3395 if (sk_SSL_CIPHER_num(clntsk) == 0 || sk_SSL_CIPHER_num(srvrsk) == 0)
0f113f3e
MC
3396 return NULL;
3397
a216df59 3398 for (i = 0; i < sk_SSL_CIPHER_num(clntsk); i++) {
0f113f3e
MC
3399 int n;
3400
a216df59
MC
3401 c = sk_SSL_CIPHER_value(clntsk, i);
3402 if (sk_SSL_CIPHER_find(srvrsk, c) < 0)
3403 continue;
3404
2743594d
SS
3405 n = OPENSSL_strnlen(c->name, size);
3406 if (n >= size) {
0f113f3e
MC
3407 if (p != buf)
3408 --p;
3409 *p = '\0';
3410 return buf;
3411 }
2743594d 3412 memcpy(p, c->name, n);
0f113f3e
MC
3413 p += n;
3414 *(p++) = ':';
a216df59 3415 size -= n + 1;
0f113f3e
MC
3416 }
3417 p[-1] = '\0';
26a7d938 3418 return buf;
0f113f3e
MC
3419}
3420
7955c1f1
MC
3421/**
3422 * Return the requested servername (SNI) value. Note that the behaviour varies
3423 * depending on:
3424 * - whether this is called by the client or the server,
3425 * - if we are before or during/after the handshake,
3426 * - if a resumption or normal handshake is being attempted/has occurred
3427 * - whether we have negotiated TLSv1.2 (or below) or TLSv1.3
38b051a1 3428 *
7955c1f1 3429 * Note that only the host_name type is defined (RFC 3546).
ed3883d2 3430 */
f1fd4544 3431const char *SSL_get_servername(const SSL *s, const int type)
0f113f3e 3432{
38b051a1
TM
3433 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
3434 int server;
3435
3436 if (sc == NULL)
3437 return NULL;
3438
7955c1f1
MC
3439 /*
3440 * If we don't know if we are the client or the server yet then we assume
3441 * client.
3442 */
38b051a1
TM
3443 server = sc->handshake_func == NULL ? 0 : sc->server;
3444
0f113f3e
MC
3445 if (type != TLSEXT_NAMETYPE_host_name)
3446 return NULL;
a13c20f6 3447
7955c1f1
MC
3448 if (server) {
3449 /**
3450 * Server side
3451 * In TLSv1.3 on the server SNI is not associated with the session
3452 * but in TLSv1.2 or below it is.
3453 *
3454 * Before the handshake:
3455 * - return NULL
3456 *
3457 * During/after the handshake (TLSv1.2 or below resumption occurred):
3458 * - If a servername was accepted by the server in the original
3459 * handshake then it will return that servername, or NULL otherwise.
3460 *
3461 * During/after the handshake (TLSv1.2 or below resumption did not occur):
3462 * - The function will return the servername requested by the client in
3463 * this handshake or NULL if none was requested.
3464 */
38b051a1
TM
3465 if (sc->hit && !SSL_CONNECTION_IS_TLS13(sc))
3466 return sc->session->ext.hostname;
7955c1f1
MC
3467 } else {
3468 /**
3469 * Client side
3470 *
3471 * Before the handshake:
3472 * - If a servername has been set via a call to
3473 * SSL_set_tlsext_host_name() then it will return that servername
3474 * - If one has not been set, but a TLSv1.2 resumption is being
3475 * attempted and the session from the original handshake had a
3476 * servername accepted by the server then it will return that
3477 * servername
3478 * - Otherwise it returns NULL
3479 *
3480 * During/after the handshake (TLSv1.2 or below resumption occurred):
9f7505ab 3481 * - If the session from the original handshake had a servername accepted
7955c1f1
MC
3482 * by the server then it will return that servername.
3483 * - Otherwise it returns the servername set via
3484 * SSL_set_tlsext_host_name() (or NULL if it was not called).
3485 *
3486 * During/after the handshake (TLSv1.2 or below resumption did not occur):
3487 * - It will return the servername set via SSL_set_tlsext_host_name()
3488 * (or NULL if it was not called).
3489 */
3490 if (SSL_in_before(s)) {
38b051a1
TM
3491 if (sc->ext.hostname == NULL
3492 && sc->session != NULL
3493 && sc->session->ssl_version != TLS1_3_VERSION)
3494 return sc->session->ext.hostname;
7955c1f1 3495 } else {
38b051a1
TM
3496 if (!SSL_CONNECTION_IS_TLS13(sc) && sc->hit
3497 && sc->session->ext.hostname != NULL)
3498 return sc->session->ext.hostname;
7955c1f1
MC
3499 }
3500 }
3501
38b051a1 3502 return sc->ext.hostname;
0f113f3e 3503}
ed3883d2 3504
f1fd4544 3505int SSL_get_servername_type(const SSL *s)
0f113f3e 3506{
7955c1f1 3507 if (SSL_get_servername(s, TLSEXT_NAMETYPE_host_name) != NULL)
0f113f3e
MC
3508 return TLSEXT_NAMETYPE_host_name;
3509 return -1;
3510}
ee2ffc27 3511
0f113f3e
MC
3512/*
3513 * SSL_select_next_proto implements the standard protocol selection. It is
ee2ffc27 3514 * expected that this function is called from the callback set by
0f113f3e
MC
3515 * SSL_CTX_set_next_proto_select_cb. The protocol data is assumed to be a
3516 * vector of 8-bit, length prefixed byte strings. The length byte itself is
3517 * not included in the length. A byte string of length 0 is invalid. No byte
3518 * string may be truncated. The current, but experimental algorithm for
3519 * selecting the protocol is: 1) If the server doesn't support NPN then this
3520 * is indicated to the callback. In this case, the client application has to
3521 * abort the connection or have a default application level protocol. 2) If
3522 * the server supports NPN, but advertises an empty list then the client
f430ba31 3523 * selects the first protocol in its list, but indicates via the API that this
0f113f3e
MC
3524 * fallback case was enacted. 3) Otherwise, the client finds the first
3525 * protocol in the server's list that it supports and selects this protocol.
3526 * This is because it's assumed that the server has better information about
3527 * which protocol a client should use. 4) If the client doesn't support any
3528 * of the server's advertised protocols, then this is treated the same as
3529 * case 2. It returns either OPENSSL_NPN_NEGOTIATED if a common protocol was
3530 * found, or OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
ee2ffc27 3531 */
0f113f3e
MC
3532int SSL_select_next_proto(unsigned char **out, unsigned char *outlen,
3533 const unsigned char *server,
3534 unsigned int server_len,
a230b26e 3535 const unsigned char *client, unsigned int client_len)
0f113f3e
MC
3536{
3537 unsigned int i, j;
3538 const unsigned char *result;
3539 int status = OPENSSL_NPN_UNSUPPORTED;
3540
3541 /*
3542 * For each protocol in server preference order, see if we support it.
3543 */
3544 for (i = 0; i < server_len;) {
3545 for (j = 0; j < client_len;) {
3546 if (server[i] == client[j] &&
3547 memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
3548 /* We found a match */
3549 result = &server[i];
3550 status = OPENSSL_NPN_NEGOTIATED;
3551 goto found;
3552 }
3553 j += client[j];
3554 j++;
3555 }
3556 i += server[i];
3557 i++;
3558 }
3559
3560 /* There's no overlap between our protocols and the server's list. */
3561 result = client;
3562 status = OPENSSL_NPN_NO_OVERLAP;
3563
3564 found:
3565 *out = (unsigned char *)result + 1;
3566 *outlen = result[0];
3567 return status;
3568}
ee2ffc27 3569
e481f9b9 3570#ifndef OPENSSL_NO_NEXTPROTONEG
0f113f3e
MC
3571/*
3572 * SSL_get0_next_proto_negotiated sets *data and *len to point to the
3573 * client's requested protocol for this connection and returns 0. If the
3574 * client didn't request any protocol, then *data is set to NULL. Note that
3575 * the client can request any protocol it chooses. The value returned from
3576 * this function need not be a member of the list of supported protocols
ee2ffc27
BL
3577 * provided by the callback.
3578 */
0f113f3e
MC
3579void SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data,
3580 unsigned *len)
3581{
38b051a1
TM
3582 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
3583
3584 if (sc == NULL) {
3585 /* We have no other way to indicate error */
3586 *data = NULL;
3587 *len = 0;
3588 return;
3589 }
3590
3591 *data = sc->ext.npn;
12a765a5 3592 if (*data == NULL) {
0f113f3e
MC
3593 *len = 0;
3594 } else {
38b051a1 3595 *len = (unsigned int)sc->ext.npn_len;
0f113f3e
MC
3596 }
3597}
3598
3599/*
aff8c126 3600 * SSL_CTX_set_npn_advertised_cb sets a callback that is called when
0f113f3e
MC
3601 * a TLS server needs a list of supported protocols for Next Protocol
3602 * Negotiation. The returned list must be in wire format. The list is
3603 * returned by setting |out| to point to it and |outlen| to its length. This
3604 * memory will not be modified, but one should assume that the SSL* keeps a
3605 * reference to it. The callback should return SSL_TLSEXT_ERR_OK if it
3606 * wishes to advertise. Otherwise, no such extension will be included in the
3607 * ServerHello.
3608 */
aff8c126 3609void SSL_CTX_set_npn_advertised_cb(SSL_CTX *ctx,
8cbfcc70 3610 SSL_CTX_npn_advertised_cb_func cb,
aff8c126 3611 void *arg)
0f113f3e 3612{
68dbff4c
HL
3613 if (IS_QUIC_CTX(ctx))
3614 /* NPN not allowed for QUIC */
3615 return;
3616
aff8c126
RS
3617 ctx->ext.npn_advertised_cb = cb;
3618 ctx->ext.npn_advertised_cb_arg = arg;
0f113f3e
MC
3619}
3620
3621/*
3622 * SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
ee2ffc27
BL
3623 * client needs to select a protocol from the server's provided list. |out|
3624 * must be set to point to the selected protocol (which may be within |in|).
0f113f3e
MC
3625 * The length of the protocol name must be written into |outlen|. The
3626 * server's advertised protocols are provided in |in| and |inlen|. The
3627 * callback can assume that |in| is syntactically valid. The client must
3628 * select a protocol. It is fatal to the connection if this callback returns
3629 * a value other than SSL_TLSEXT_ERR_OK.
ee2ffc27 3630 */
aff8c126 3631void SSL_CTX_set_npn_select_cb(SSL_CTX *ctx,
8cbfcc70 3632 SSL_CTX_npn_select_cb_func cb,
aff8c126 3633 void *arg)
0f113f3e 3634{
68dbff4c
HL
3635 if (IS_QUIC_CTX(ctx))
3636 /* NPN not allowed for QUIC */
3637 return;
3638
aff8c126
RS
3639 ctx->ext.npn_select_cb = cb;
3640 ctx->ext.npn_select_cb_arg = arg;
0f113f3e 3641}
e481f9b9 3642#endif
a398f821 3643
feba11cf
TS
3644static int alpn_value_ok(const unsigned char *protos, unsigned int protos_len)
3645{
3646 unsigned int idx;
3647
3648 if (protos_len < 2 || protos == NULL)
3649 return 0;
3650
3651 for (idx = 0; idx < protos_len; idx += protos[idx] + 1) {
3652 if (protos[idx] == 0)
3653 return 0;
3654 }
3655 return idx == protos_len;
3656}
0f113f3e
MC
3657/*
3658 * SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
6f017a8f 3659 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
0f113f3e
MC
3660 * length-prefixed strings). Returns 0 on success.
3661 */
3662int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char *protos,
817cd0d5 3663 unsigned int protos_len)
0f113f3e 3664{
feba11cf
TS
3665 unsigned char *alpn;
3666
3667 if (protos_len == 0 || protos == NULL) {
3668 OPENSSL_free(ctx->ext.alpn);
3669 ctx->ext.alpn = NULL;
39a14059 3670 ctx->ext.alpn_len = 0;
feba11cf
TS
3671 return 0;
3672 }
3673 /* Not valid per RFC */
3674 if (!alpn_value_ok(protos, protos_len))
3675 return 1;
3676
3677 alpn = OPENSSL_memdup(protos, protos_len);
e077455e 3678 if (alpn == NULL)
0f113f3e 3679 return 1;
feba11cf
TS
3680 OPENSSL_free(ctx->ext.alpn);
3681 ctx->ext.alpn = alpn;
aff8c126 3682 ctx->ext.alpn_len = protos_len;
0f113f3e
MC
3683
3684 return 0;
3685}
3686
3687/*
3688 * SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
6f017a8f 3689 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
0f113f3e
MC
3690 * length-prefixed strings). Returns 0 on success.
3691 */
3692int SSL_set_alpn_protos(SSL *ssl, const unsigned char *protos,
817cd0d5 3693 unsigned int protos_len)
0f113f3e 3694{
feba11cf 3695 unsigned char *alpn;
38b051a1
TM
3696 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
3697
3698 if (sc == NULL)
3699 return 1;
feba11cf
TS
3700
3701 if (protos_len == 0 || protos == NULL) {
38b051a1
TM
3702 OPENSSL_free(sc->ext.alpn);
3703 sc->ext.alpn = NULL;
3704 sc->ext.alpn_len = 0;
feba11cf
TS
3705 return 0;
3706 }
3707 /* Not valid per RFC */
3708 if (!alpn_value_ok(protos, protos_len))
3709 return 1;
3710
3711 alpn = OPENSSL_memdup(protos, protos_len);
e077455e 3712 if (alpn == NULL)
0f113f3e 3713 return 1;
38b051a1
TM
3714 OPENSSL_free(sc->ext.alpn);
3715 sc->ext.alpn = alpn;
3716 sc->ext.alpn_len = protos_len;
0f113f3e
MC
3717
3718 return 0;
3719}
3720
3721/*
3722 * SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is
3723 * called during ClientHello processing in order to select an ALPN protocol
3724 * from the client's list of offered protocols.
3725 */
3726void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
8cbfcc70
RS
3727 SSL_CTX_alpn_select_cb_func cb,
3728 void *arg)
0f113f3e 3729{
aff8c126
RS
3730 ctx->ext.alpn_select_cb = cb;
3731 ctx->ext.alpn_select_cb_arg = arg;
0f113f3e
MC
3732}
3733
3734/*
69687aa8
F
3735 * SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
3736 * On return it sets |*data| to point to |*len| bytes of protocol name
0f113f3e
MC
3737 * (not including the leading length-prefix byte). If the server didn't
3738 * respond with a negotiated protocol then |*len| will be zero.
3739 */
6f017a8f 3740void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
817cd0d5 3741 unsigned int *len)
0f113f3e 3742{
38b051a1
TM
3743 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
3744
3745 if (sc == NULL) {
3746 /* We have no other way to indicate error */
3747 *data = NULL;
3748 *len = 0;
3749 return;
3750 }
3751
3752 *data = sc->s3.alpn_selected;
0f113f3e
MC
3753 if (*data == NULL)
3754 *len = 0;
3755 else
38b051a1 3756 *len = (unsigned int)sc->s3.alpn_selected_len;
0f113f3e
MC
3757}
3758
74b4b494 3759int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
0f113f3e 3760 const char *label, size_t llen,
23cec1f4 3761 const unsigned char *context, size_t contextlen,
0f113f3e
MC
3762 int use_context)
3763{
38b051a1
TM
3764 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
3765
3766 if (sc == NULL)
3767 return -1;
3768
3769 if (sc->session == NULL
3770 || (sc->version < TLS1_VERSION && sc->version != DTLS1_BAD_VER))
0f113f3e 3771 return -1;
e0af0405 3772
12c0d72c
HL
3773 return sc->ssl.method->ssl3_enc->export_keying_material(sc, out, olen, label,
3774 llen, context,
3775 contextlen,
3776 use_context);
0f113f3e 3777}
e0af0405 3778
b38ede80
TT
3779int SSL_export_keying_material_early(SSL *s, unsigned char *out, size_t olen,
3780 const char *label, size_t llen,
3781 const unsigned char *context,
3782 size_t contextlen)
3783{
38b051a1
TM
3784 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
3785
3786 if (sc == NULL)
3787 return -1;
3788
3789 if (sc->version != TLS1_3_VERSION)
b38ede80
TT
3790 return 0;
3791
38b051a1 3792 return tls13_export_keying_material_early(sc, out, olen, label, llen,
b38ede80
TT
3793 context, contextlen);
3794}
3795
3c1d6bbc 3796static unsigned long ssl_session_hash(const SSL_SESSION *a)
0f113f3e 3797{
bd5d27c1 3798 const unsigned char *session_id = a->session_id;
0f113f3e 3799 unsigned long l;
bd5d27c1
DB
3800 unsigned char tmp_storage[4];
3801
3802 if (a->session_id_length < sizeof(tmp_storage)) {
3803 memset(tmp_storage, 0, sizeof(tmp_storage));
3804 memcpy(tmp_storage, a->session_id, a->session_id_length);
3805 session_id = tmp_storage;
3806 }
0f113f3e
MC
3807
3808 l = (unsigned long)
bd5d27c1
DB
3809 ((unsigned long)session_id[0]) |
3810 ((unsigned long)session_id[1] << 8L) |
3811 ((unsigned long)session_id[2] << 16L) |
3812 ((unsigned long)session_id[3] << 24L);
26a7d938 3813 return l;
0f113f3e
MC
3814}
3815
3816/*
3817 * NB: If this function (or indeed the hash function which uses a sort of
dc644fe2 3818 * coarser function than this one) is changed, ensure
0f113f3e
MC
3819 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on
3820 * being able to construct an SSL_SESSION that will collide with any existing
3821 * session with a matching session ID.
3822 */
3823static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
3824{
3825 if (a->ssl_version != b->ssl_version)
208fb891 3826 return 1;
0f113f3e 3827 if (a->session_id_length != b->session_id_length)
208fb891 3828 return 1;
26a7d938 3829 return memcmp(a->session_id, b->session_id, a->session_id_length);
0f113f3e
MC
3830}
3831
3832/*
3833 * These wrapper functions should remain rather than redeclaring
d0fa136c 3834 * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
0f113f3e
MC
3835 * variable. The reason is that the functions aren't static, they're exposed
3836 * via ssl.h.
3837 */
97b17195 3838
b4250010 3839SSL_CTX *SSL_CTX_new_ex(OSSL_LIB_CTX *libctx, const char *propq,
d8652be0 3840 const SSL_METHOD *meth)
0f113f3e
MC
3841{
3842 SSL_CTX *ret = NULL;
b67cb09f
TS
3843#ifndef OPENSSL_NO_COMP_ALG
3844 int i;
3845#endif
0f113f3e
MC
3846
3847 if (meth == NULL) {
6849b73c 3848 ERR_raise(ERR_LIB_SSL, SSL_R_NULL_SSL_METHOD_PASSED);
26a7d938 3849 return NULL;
0f113f3e
MC
3850 }
3851
0fc32b07
MC
3852 if (!OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL))
3853 return NULL;
7fa792d1 3854
97beb77f 3855 /* Doing this for the run once effect */
0f113f3e 3856 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
6849b73c 3857 ERR_raise(ERR_LIB_SSL, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
0f113f3e
MC
3858 goto err;
3859 }
43a07d6d 3860
b51bce94 3861 ret = OPENSSL_zalloc(sizeof(*ret));
0f113f3e 3862 if (ret == NULL)
97beb77f 3863 return NULL;
0f113f3e 3864
045a8930 3865 /* Init the reference counting before any call to SSL_CTX_free */
97beb77f
P
3866 if (!CRYPTO_NEW_REF(&ret->references, 1)) {
3867 OPENSSL_free(ret);
3868 return NULL;
3869 }
43a07d6d 3870
045a8930
F
3871 ret->lock = CRYPTO_THREAD_lock_new();
3872 if (ret->lock == NULL) {
e077455e
RL
3873 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
3874 goto err;
045a8930
F
3875 }
3876
acce0557
P
3877#ifdef TSAN_REQUIRES_LOCKING
3878 ret->tsan_lock = CRYPTO_THREAD_lock_new();
3879 if (ret->tsan_lock == NULL) {
e077455e 3880 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
acce0557
P
3881 goto err;
3882 }
3883#endif
3884
ba18627e
MC
3885 ret->libctx = libctx;
3886 if (propq != NULL) {
3887 ret->propq = OPENSSL_strdup(propq);
3888 if (ret->propq == NULL)
3889 goto err;
3890 }
3891
0f113f3e 3892 ret->method = meth;
7946ab33
KR
3893 ret->min_proto_version = 0;
3894 ret->max_proto_version = 0;
693cf80c 3895 ret->mode = SSL_MODE_AUTO_RETRY;
0f113f3e
MC
3896 ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
3897 ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
64b25758 3898 /* We take the system default. */
0f113f3e 3899 ret->session_timeout = meth->get_timeout();
0f113f3e 3900 ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
0f113f3e 3901 ret->verify_mode = SSL_VERIFY_NONE;
0f113f3e 3902
62d0577e 3903 ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
e077455e
RL
3904 if (ret->sessions == NULL) {
3905 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
0f113f3e 3906 goto err;
e077455e 3907 }
0f113f3e 3908 ret->cert_store = X509_STORE_new();
e077455e
RL
3909 if (ret->cert_store == NULL) {
3910 ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
0f113f3e 3911 goto err;
e077455e 3912 }
ed29e82a 3913#ifndef OPENSSL_NO_CT
d8652be0 3914 ret->ctlog_store = CTLOG_STORE_new_ex(libctx, propq);
e077455e
RL
3915 if (ret->ctlog_store == NULL) {
3916 ERR_raise(ERR_LIB_SSL, ERR_R_CT_LIB);
ed29e82a 3917 goto err;
e077455e 3918 }
ed29e82a 3919#endif
f865b081 3920
c8f6c28a 3921 /* initialize cipher/digest methods table */
ee58915c
MB
3922 if (!ssl_load_ciphers(ret)) {
3923 ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
e077455e 3924 goto err;
ee58915c
MB
3925 }
3926
3927 if (!ssl_load_groups(ret)) {
3928 ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
3929 goto err;
3930 }
3931
3932 /* load provider sigalgs */
3933 if (!ssl_load_sigalgs(ret)) {
3934 ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
e077455e 3935 goto err;
ee58915c 3936 }
c8f6c28a 3937
ee58915c
MB
3938 /* initialise sig algs */
3939 if (!ssl_setup_sigalgs(ret)) {
3940 ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
e077455e 3941 goto err;
ee58915c 3942 }
9d2d857f 3943
e077455e
RL
3944 if (!SSL_CTX_set_ciphersuites(ret, OSSL_default_ciphersuites())) {
3945 ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
f865b081 3946 goto err;
e077455e 3947 }
f865b081 3948
ee58915c
MB
3949 if ((ret->cert = ssl_cert_new(SSL_PKEY_NUM + ret->sigalg_list_len)) == NULL) {
3950 ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
3951 goto err;
3952 }
3953
a68eee67 3954 if (!ssl_create_cipher_list(ret,
f865b081 3955 ret->tls13_ciphersuites,
a230b26e 3956 &ret->cipher_list, &ret->cipher_list_by_id,
5d120511 3957 OSSL_default_cipher_list(), ret->cert)
a230b26e 3958 || sk_SSL_CIPHER_num(ret->cipher_list) <= 0) {
6849b73c 3959 ERR_raise(ERR_LIB_SSL, SSL_R_LIBRARY_HAS_NO_CIPHERS);
e077455e 3960 goto err;
0f113f3e
MC
3961 }
3962
3963 ret->param = X509_VERIFY_PARAM_new();
e077455e
RL
3964 if (ret->param == NULL) {
3965 ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
0f113f3e 3966 goto err;
e077455e 3967 }
0f113f3e 3968
c8f6c28a
MC
3969 /*
3970 * If these aren't available from the provider we'll get NULL returns.
3971 * That's fine but will cause errors later if SSLv3 is negotiated
3972 */
3973 ret->md5 = ssl_evp_md_fetch(libctx, NID_md5, propq);
3974 ret->sha1 = ssl_evp_md_fetch(libctx, NID_sha1, propq);
0f113f3e 3975
e077455e
RL
3976 if ((ret->ca_names = sk_X509_NAME_new_null()) == NULL) {
3977 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
0f113f3e 3978 goto err;
e077455e 3979 }
0f113f3e 3980
e077455e
RL
3981 if ((ret->client_ca_names = sk_X509_NAME_new_null()) == NULL) {
3982 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
98732979 3983 goto err;
e077455e 3984 }
98732979 3985
e077455e
RL
3986 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data)) {
3987 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
25a807bc 3988 goto err;
e077455e 3989 }
0f113f3e 3990
4bfb96f2
TS
3991 if ((ret->ext.secure = OPENSSL_secure_zalloc(sizeof(*ret->ext.secure))) == NULL)
3992 goto err;
3993
0f113f3e
MC
3994 /* No compression for DTLS */
3995 if (!(meth->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS))
3996 ret->comp_methods = SSL_COMP_get_compression_methods();
3997
3998 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
d102d9df 3999 ret->split_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
566dda07 4000
4e2e1ec9 4001 /* Setup RFC5077 ticket keys */
8f21260b 4002 if ((RAND_bytes_ex(libctx, ret->ext.tick_key_name,
0f8815aa 4003 sizeof(ret->ext.tick_key_name), 0) <= 0)
8f21260b 4004 || (RAND_priv_bytes_ex(libctx, ret->ext.secure->tick_hmac_key,
0f8815aa 4005 sizeof(ret->ext.secure->tick_hmac_key), 0) <= 0)
8f21260b 4006 || (RAND_priv_bytes_ex(libctx, ret->ext.secure->tick_aes_key,
0f8815aa 4007 sizeof(ret->ext.secure->tick_aes_key), 0) <= 0))
0f113f3e 4008 ret->options |= SSL_OP_NO_TICKET;
6434abbf 4009
8f21260b 4010 if (RAND_priv_bytes_ex(libctx, ret->ext.cookie_hmac_key,
e077455e
RL
4011 sizeof(ret->ext.cookie_hmac_key), 0) <= 0) {
4012 ERR_raise(ERR_LIB_SSL, ERR_R_RAND_LIB);
43054d3d 4013 goto err;
e077455e 4014 }
43054d3d 4015
edc032b5 4016#ifndef OPENSSL_NO_SRP
e077455e
RL
4017 if (!ssl_ctx_srp_ctx_init_intern(ret)) {
4018 ERR_raise(ERR_LIB_SSL, ERR_R_SSL_LIB);
69f68237 4019 goto err;
e077455e 4020 }
edc032b5 4021#endif
4db9677b 4022#ifndef OPENSSL_NO_ENGINE
0f113f3e
MC
4023# ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO
4024# define eng_strx(x) #x
4025# define eng_str(x) eng_strx(x)
4026 /* Use specific client engine automatically... ignore errors */
4027 {
4028 ENGINE *eng;
4029 eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
4030 if (!eng) {
4031 ERR_clear_error();
4032 ENGINE_load_builtin_engines();
4033 eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
4034 }
4035 if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng))
4036 ERR_clear_error();
4037 }
4038# endif
b67cb09f
TS
4039#endif
4040
4041#ifndef OPENSSL_NO_COMP_ALG
4042 /*
4043 * Set the default order: brotli, zlib, zstd
4044 * Including only those enabled algorithms
4045 */
4046 memset(ret->cert_comp_prefs, 0, sizeof(ret->cert_comp_prefs));
4047 i = 0;
4048 if (ossl_comp_has_alg(TLSEXT_comp_cert_brotli))
4049 ret->cert_comp_prefs[i++] = TLSEXT_comp_cert_brotli;
4050 if (ossl_comp_has_alg(TLSEXT_comp_cert_zlib))
4051 ret->cert_comp_prefs[i++] = TLSEXT_comp_cert_zlib;
4052 if (ossl_comp_has_alg(TLSEXT_comp_cert_zstd))
4053 ret->cert_comp_prefs[i++] = TLSEXT_comp_cert_zstd;
0f113f3e 4054#endif
dc5744cb
EK
4055 /*
4056 * Disable compression by default to prevent CRIME. Applications can
4057 * re-enable compression by configuring
4058 * SSL_CTX_clear_options(ctx, SSL_OP_NO_COMPRESSION);
a5816a5a
MC
4059 * or by using the SSL_CONF library. Similarly we also enable TLSv1.3
4060 * middlebox compatibility by default. This may be disabled by default in
4061 * a later OpenSSL version.
dc5744cb 4062 */
a5816a5a 4063 ret->options |= SSL_OP_NO_COMPRESSION | SSL_OP_ENABLE_MIDDLEBOX_COMPAT;
0f113f3e 4064
aff8c126 4065 ret->ext.status_type = TLSEXT_STATUSTYPE_nothing;
ba261f71 4066
bfa9a9af 4067 /*
c39e4048
BK
4068 * We cannot usefully set a default max_early_data here (which gets
4069 * propagated in SSL_new(), for the following reason: setting the
4070 * SSL field causes tls_construct_stoc_early_data() to tell the
4071 * client that early data will be accepted when constructing a TLS 1.3
4072 * session ticket, and the client will accordingly send us early data
4073 * when using that ticket (if the client has early data to send).
4074 * However, in order for the early data to actually be consumed by
4075 * the application, the application must also have calls to
4076 * SSL_read_early_data(); otherwise we'll just skip past the early data
4077 * and ignore it. So, since the application must add calls to
4078 * SSL_read_early_data(), we also require them to add
4079 * calls to SSL_CTX_set_max_early_data() in order to use early data,
4080 * eliminating the bandwidth-wasting early data in the case described
4081 * above.
bfa9a9af 4082 */
c39e4048 4083 ret->max_early_data = 0;
bfa9a9af 4084
4e8548e8
MC
4085 /*
4086 * Default recv_max_early_data is a fully loaded single record. Could be
4087 * split across multiple records in practice. We set this differently to
4088 * max_early_data so that, in the default case, we do not advertise any
4089 * support for early_data, but if a client were to send us some (e.g.
4090 * because of an old, stale ticket) then we will tolerate it and skip over
4091 * it.
4092 */
4093 ret->recv_max_early_data = SSL3_RT_MAX_PLAIN_LENGTH;
4094
36ff232c
MC
4095 /* By default we send two session tickets automatically in TLSv1.3 */
4096 ret->num_tickets = 2;
9d0a8bb7 4097
8a5ed9dc
TM
4098 ssl_ctx_system_config(ret);
4099
16203f7b 4100 return ret;
0f113f3e 4101 err:
e0e920b1 4102 SSL_CTX_free(ret);
16203f7b 4103 return NULL;
0f113f3e 4104}
d02b48c6 4105
ba18627e
MC
4106SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
4107{
d8652be0 4108 return SSL_CTX_new_ex(NULL, NULL, meth);
ba18627e
MC
4109}
4110
c5ebfcab 4111int SSL_CTX_up_ref(SSL_CTX *ctx)
a18a31e4 4112{
16203f7b 4113 int i;
c5ebfcab 4114
43a07d6d 4115 if (CRYPTO_UP_REF(&ctx->references, &i) <= 0)
c5ebfcab
F
4116 return 0;
4117
4118 REF_PRINT_COUNT("SSL_CTX", ctx);
4119 REF_ASSERT_ISNT(i < 2);
4120 return ((i > 1) ? 1 : 0);
a18a31e4
MC
4121}
4122
4f43d0e7 4123void SSL_CTX_free(SSL_CTX *a)
0f113f3e
MC
4124{
4125 int i;
9d2d857f 4126 size_t j;
d02b48c6 4127
0f113f3e
MC
4128 if (a == NULL)
4129 return;
d02b48c6 4130
43a07d6d 4131 CRYPTO_DOWN_REF(&a->references, &i);
f3f1cf84 4132 REF_PRINT_COUNT("SSL_CTX", a);
0f113f3e
MC
4133 if (i > 0)
4134 return;
f3f1cf84 4135 REF_ASSERT_ISNT(i < 0);
0f113f3e 4136
222561fe 4137 X509_VERIFY_PARAM_free(a->param);
919ba009 4138 dane_ctx_final(&a->dane);
0f113f3e
MC
4139
4140 /*
4141 * Free internal session cache. However: the remove_cb() may reference
4142 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
4143 * after the sessions were flushed.
4144 * As the ex_data handling routines might also touch the session cache,
4145 * the most secure solution seems to be: empty (flush) the cache, then
4146 * free ex_data, then finally free the cache.
4147 * (See ticket [openssl.org #212].)
4148 */
4149 if (a->sessions != NULL)
4150 SSL_CTX_flush_sessions(a, 0);
4151
4152 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
25aaa98a 4153 lh_SSL_SESSION_free(a->sessions);
222561fe 4154 X509_STORE_free(a->cert_store);
ed29e82a
RP
4155#ifndef OPENSSL_NO_CT
4156 CTLOG_STORE_free(a->ctlog_store);
4157#endif
25aaa98a
RS
4158 sk_SSL_CIPHER_free(a->cipher_list);
4159 sk_SSL_CIPHER_free(a->cipher_list_by_id);
f865b081 4160 sk_SSL_CIPHER_free(a->tls13_ciphersuites);
e0e920b1 4161 ssl_cert_free(a->cert);
fa7c2637 4162 sk_X509_NAME_pop_free(a->ca_names, X509_NAME_free);
98732979 4163 sk_X509_NAME_pop_free(a->client_ca_names, X509_NAME_free);
79b2a2f2 4164 OSSL_STACK_OF_X509_free(a->extra_certs);
0f113f3e 4165 a->comp_methods = NULL;
e783bae2 4166#ifndef OPENSSL_NO_SRTP
25aaa98a 4167 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
e783bae2 4168#endif
edc032b5 4169#ifndef OPENSSL_NO_SRP
76cb077f 4170 ssl_ctx_srp_ctx_free_intern(a);
edc032b5 4171#endif
bdfe932d 4172#ifndef OPENSSL_NO_ENGINE
301fcb28 4173 tls_engine_finish(a->client_cert_engine);
ddac1974 4174#endif
8671b898 4175
aff8c126 4176 OPENSSL_free(a->ext.ecpointformats);
187753e0 4177 OPENSSL_free(a->ext.supportedgroups);
ddf8f1ce 4178 OPENSSL_free(a->ext.supported_groups_default);
aff8c126 4179 OPENSSL_free(a->ext.alpn);
4bfb96f2 4180 OPENSSL_secure_free(a->ext.secure);
8671b898 4181
c8f6c28a
MC
4182 ssl_evp_md_free(a->md5);
4183 ssl_evp_md_free(a->sha1);
4184
9d2d857f
MC
4185 for (j = 0; j < SSL_ENC_NUM_IDX; j++)
4186 ssl_evp_cipher_free(a->ssl_cipher_methods[j]);
4187 for (j = 0; j < SSL_MD_NUM_IDX; j++)
4188 ssl_evp_md_free(a->ssl_digest_methods[j]);
4189 for (j = 0; j < a->group_list_len; j++) {
4190 OPENSSL_free(a->group_list[j].tlsname);
4191 OPENSSL_free(a->group_list[j].realname);
4192 OPENSSL_free(a->group_list[j].algorithm);
4193 }
4194 OPENSSL_free(a->group_list);
ee58915c
MB
4195 for (j = 0; j < a->sigalg_list_len; j++) {
4196 OPENSSL_free(a->sigalg_list[j].name);
4197 OPENSSL_free(a->sigalg_list[j].sigalg_name);
4198 OPENSSL_free(a->sigalg_list[j].sigalg_oid);
4199 OPENSSL_free(a->sigalg_list[j].sig_name);
4200 OPENSSL_free(a->sigalg_list[j].sig_oid);
4201 OPENSSL_free(a->sigalg_list[j].hash_name);
4202 OPENSSL_free(a->sigalg_list[j].hash_oid);
4203 OPENSSL_free(a->sigalg_list[j].keytype);
4204 OPENSSL_free(a->sigalg_list[j].keytype_oid);
4205 }
4206 OPENSSL_free(a->sigalg_list);
4207 OPENSSL_free(a->ssl_cert_info);
c8f6c28a 4208
263ff2c9 4209 OPENSSL_free(a->sigalg_lookup_cache);
ee58915c 4210 OPENSSL_free(a->tls12_sigalgs);
263ff2c9 4211
3c95ef22
TS
4212 OPENSSL_free(a->client_cert_type);
4213 OPENSSL_free(a->server_cert_type);
4214
16203f7b 4215 CRYPTO_THREAD_lock_free(a->lock);
43a07d6d 4216 CRYPTO_FREE_REF(&a->references);
acce0557
P
4217#ifdef TSAN_REQUIRES_LOCKING
4218 CRYPTO_THREAD_lock_free(a->tsan_lock);
4219#endif
16203f7b 4220
ba18627e 4221 OPENSSL_free(a->propq);
fb1a0bb9
HL
4222#ifndef OPENSSL_NO_QLOG
4223 OPENSSL_free(a->qlog_title);
4224#endif
ba18627e 4225
0f113f3e
MC
4226 OPENSSL_free(a);
4227}
d02b48c6 4228
3ae76679 4229void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
0f113f3e
MC
4230{
4231 ctx->default_passwd_callback = cb;
4232}
4233
4234void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u)
4235{
4236 ctx->default_passwd_callback_userdata = u;
4237}
4238
0c452abc
CH
4239pem_password_cb *SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx)
4240{
4241 return ctx->default_passwd_callback;
4242}
4243
4244void *SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx)
4245{
4246 return ctx->default_passwd_callback_userdata;
4247}
4248
a974e64a
MC
4249void SSL_set_default_passwd_cb(SSL *s, pem_password_cb *cb)
4250{
38b051a1
TM
4251 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
4252
4253 if (sc == NULL)
4254 return;
4255
4256 sc->default_passwd_callback = cb;
a974e64a
MC
4257}
4258
4259void SSL_set_default_passwd_cb_userdata(SSL *s, void *u)
4260{
38b051a1
TM
4261 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
4262
4263 if (sc == NULL)
4264 return;
4265
4266 sc->default_passwd_callback_userdata = u;
a974e64a
MC
4267}
4268
0c452abc
CH
4269pem_password_cb *SSL_get_default_passwd_cb(SSL *s)
4270{
38b051a1
TM
4271 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
4272
4273 if (sc == NULL)
4274 return NULL;
4275
4276 return sc->default_passwd_callback;
0c452abc
CH
4277}
4278
4279void *SSL_get_default_passwd_cb_userdata(SSL *s)
4280{
38b051a1
TM
4281 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
4282
4283 if (sc == NULL)
4284 return NULL;
4285
4286 return sc->default_passwd_callback_userdata;
0c452abc
CH
4287}
4288
0f113f3e
MC
4289void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
4290 int (*cb) (X509_STORE_CTX *, void *),
4291 void *arg)
4292{
4293 ctx->app_verify_callback = cb;
4294 ctx->app_verify_arg = arg;
4295}
4296
4297void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
4298 int (*cb) (int, X509_STORE_CTX *))
4299{
4300 ctx->verify_mode = mode;
4301 ctx->default_verify_callback = cb;
4302}
4303
4304void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth)
4305{
4306 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
4307}
4308
a230b26e 4309void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb) (SSL *ssl, void *arg), void *arg)
0f113f3e
MC
4310{
4311 ssl_cert_set_cert_cb(c->cert, cb, arg);
4312}
4313
4314void SSL_set_cert_cb(SSL *s, int (*cb) (SSL *ssl, void *arg), void *arg)
4315{
38b051a1
TM
4316 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
4317
4318 if (sc == NULL)
4319 return;
4320
4321 ssl_cert_set_cert_cb(sc->cert, cb, arg);
0f113f3e 4322}
18d71588 4323
38b051a1 4324void ssl_set_masks(SSL_CONNECTION *s)
0f113f3e 4325{
6383d316 4326 CERT *c = s->cert;
555cbb32 4327 uint32_t *pvalid = s->s3.tmp.valid_flags;
bc71f910 4328 int rsa_enc, rsa_sign, dh_tmp, dsa_sign;
361a1191 4329 unsigned long mask_k, mask_a;
361a1191 4330 int have_ecc_cert, ecdsa_ok;
462f4f4b 4331
0f113f3e
MC
4332 if (c == NULL)
4333 return;
d02b48c6 4334
13c45372 4335 dh_tmp = (c->dh_tmp != NULL
13c45372 4336 || c->dh_tmp_cb != NULL
13c45372 4337 || c->dh_tmp_auto);
d02b48c6 4338
d0ff28f8 4339 rsa_enc = pvalid[SSL_PKEY_RSA] & CERT_PKEY_VALID;
38e8f3cd
DSH
4340 rsa_sign = pvalid[SSL_PKEY_RSA] & CERT_PKEY_VALID;
4341 dsa_sign = pvalid[SSL_PKEY_DSA_SIGN] & CERT_PKEY_VALID;
6383d316 4342 have_ecc_cert = pvalid[SSL_PKEY_ECC] & CERT_PKEY_VALID;
0f113f3e
MC
4343 mask_k = 0;
4344 mask_a = 0;
0e1dba93 4345
77359d22
RL
4346 OSSL_TRACE4(TLS_CIPHER, "dh_tmp=%d rsa_enc=%d rsa_sign=%d dsa_sign=%d\n",
4347 dh_tmp, rsa_enc, rsa_sign, dsa_sign);
0f113f3e 4348
2a9b9654 4349#ifndef OPENSSL_NO_GOST
4020c0b3 4350 if (ssl_has_cert(s, SSL_PKEY_GOST12_512)) {
5a5530a2 4351 mask_k |= SSL_kGOST | SSL_kGOST18;
e44380a9
DB
4352 mask_a |= SSL_aGOST12;
4353 }
4020c0b3 4354 if (ssl_has_cert(s, SSL_PKEY_GOST12_256)) {
5a5530a2 4355 mask_k |= SSL_kGOST | SSL_kGOST18;
e44380a9
DB
4356 mask_a |= SSL_aGOST12;
4357 }
4020c0b3 4358 if (ssl_has_cert(s, SSL_PKEY_GOST01)) {
0f113f3e
MC
4359 mask_k |= SSL_kGOST;
4360 mask_a |= SSL_aGOST01;
4361 }
2a9b9654 4362#endif
0f113f3e 4363
361a1191 4364 if (rsa_enc)
0f113f3e 4365 mask_k |= SSL_kRSA;
d02b48c6 4366
0f113f3e
MC
4367 if (dh_tmp)
4368 mask_k |= SSL_kDHE;
d02b48c6 4369
6aaa29fb
DSH
4370 /*
4371 * If we only have an RSA-PSS certificate allow RSA authentication
4372 * if TLS 1.2 and peer supports it.
4373 */
4374
4375 if (rsa_enc || rsa_sign || (ssl_has_cert(s, SSL_PKEY_RSA_PSS_SIGN)
4376 && pvalid[SSL_PKEY_RSA_PSS_SIGN] & CERT_PKEY_EXPLICIT_SIGN
38b051a1 4377 && TLS1_get_version(&s->ssl) == TLS1_2_VERSION))
0f113f3e 4378 mask_a |= SSL_aRSA;
d02b48c6 4379
0f113f3e
MC
4380 if (dsa_sign) {
4381 mask_a |= SSL_aDSS;
0f113f3e 4382 }
d02b48c6 4383
0f113f3e 4384 mask_a |= SSL_aNULL;
d02b48c6 4385
3c95ef22
TS
4386 /*
4387 * You can do anything with an RPK key, since there's no cert to restrict it
4388 * But we need to check for private keys
4389 */
4390 if (pvalid[SSL_PKEY_RSA] & CERT_PKEY_RPK) {
4391 mask_a |= SSL_aRSA;
4392 mask_k |= SSL_kRSA;
4393 }
4394 if (pvalid[SSL_PKEY_ECC] & CERT_PKEY_RPK)
4395 mask_a |= SSL_aECDSA;
4396 if (TLS1_get_version(&s->ssl) == TLS1_2_VERSION) {
4397 if (pvalid[SSL_PKEY_RSA_PSS_SIGN] & CERT_PKEY_RPK)
4398 mask_a |= SSL_aRSA;
4399 if (pvalid[SSL_PKEY_ED25519] & CERT_PKEY_RPK
4400 || pvalid[SSL_PKEY_ED448] & CERT_PKEY_RPK)
4401 mask_a |= SSL_aECDSA;
4402 }
4403
0f113f3e
MC
4404 /*
4405 * An ECC certificate may be usable for ECDH and/or ECDSA cipher suites
4406 * depending on the key usage extension.
4407 */
0f113f3e 4408 if (have_ecc_cert) {
a8d8e06b 4409 uint32_t ex_kusage;
4020c0b3 4410 ex_kusage = X509_get_key_usage(c->pkeys[SSL_PKEY_ECC].x509);
a8d8e06b 4411 ecdsa_ok = ex_kusage & X509v3_KU_DIGITAL_SIGNATURE;
6383d316 4412 if (!(pvalid[SSL_PKEY_ECC] & CERT_PKEY_SIGN))
0f113f3e 4413 ecdsa_ok = 0;
c7c46256 4414 if (ecdsa_ok)
0f113f3e 4415 mask_a |= SSL_aECDSA;
0f113f3e 4416 }
b2021556
DSH
4417 /* Allow Ed25519 for TLS 1.2 if peer supports it */
4418 if (!(mask_a & SSL_aECDSA) && ssl_has_cert(s, SSL_PKEY_ED25519)
4419 && pvalid[SSL_PKEY_ED25519] & CERT_PKEY_EXPLICIT_SIGN
38b051a1 4420 && TLS1_get_version(&s->ssl) == TLS1_2_VERSION)
b2021556 4421 mask_a |= SSL_aECDSA;
0e1d6ecf
MC
4422
4423 /* Allow Ed448 for TLS 1.2 if peer supports it */
4424 if (!(mask_a & SSL_aECDSA) && ssl_has_cert(s, SSL_PKEY_ED448)
4425 && pvalid[SSL_PKEY_ED448] & CERT_PKEY_EXPLICIT_SIGN
38b051a1 4426 && TLS1_get_version(&s->ssl) == TLS1_2_VERSION)
0e1d6ecf 4427 mask_a |= SSL_aECDSA;
ea262260 4428
fe6ef247 4429 mask_k |= SSL_kECDHE;
ddac1974
NL
4430
4431#ifndef OPENSSL_NO_PSK
0f113f3e
MC
4432 mask_k |= SSL_kPSK;
4433 mask_a |= SSL_aPSK;
526f94ad
DSH
4434 if (mask_k & SSL_kRSA)
4435 mask_k |= SSL_kRSAPSK;
4436 if (mask_k & SSL_kDHE)
4437 mask_k |= SSL_kDHEPSK;
4438 if (mask_k & SSL_kECDHE)
4439 mask_k |= SSL_kECDHEPSK;
ddac1974
NL
4440#endif
4441
555cbb32
TS
4442 s->s3.tmp.mask_k = mask_k;
4443 s->s3.tmp.mask_a = mask_a;
0f113f3e 4444}
d02b48c6 4445
38b051a1 4446int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL_CONNECTION *s)
0f113f3e 4447{
555cbb32 4448 if (s->s3.tmp.new_cipher->algorithm_auth & SSL_aECDSA) {
0f113f3e 4449 /* key usage, if present, must allow signing */
ce0c1f2b 4450 if (!(X509_get_key_usage(x) & X509v3_KU_DIGITAL_SIGNATURE)) {
6849b73c 4451 ERR_raise(ERR_LIB_SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
0f113f3e
MC
4452 return 0;
4453 }
4454 }
0f113f3e
MC
4455 return 1; /* all checks are ok */
4456}
ea262260 4457
38b051a1
TM
4458int ssl_get_server_cert_serverinfo(SSL_CONNECTION *s,
4459 const unsigned char **serverinfo,
0f113f3e
MC
4460 size_t *serverinfo_length)
4461{
555cbb32 4462 CERT_PKEY *cpk = s->s3.tmp.cert;
0f113f3e
MC
4463 *serverinfo_length = 0;
4464
a497cf25 4465 if (cpk == NULL || cpk->serverinfo == NULL)
0f113f3e
MC
4466 return 0;
4467
a497cf25
DSH
4468 *serverinfo = cpk->serverinfo;
4469 *serverinfo_length = cpk->serverinfo_length;
0f113f3e
MC
4470 return 1;
4471}
0f113f3e 4472
38b051a1 4473void ssl_update_cache(SSL_CONNECTION *s, int mode)
0f113f3e
MC
4474{
4475 int i;
4476
4477 /*
4478 * If the session_id_length is 0, we are not supposed to cache it, and it
7984fa68
MC
4479 * would be rather hard to do anyway :-). Also if the session has already
4480 * been marked as not_resumable we should not cache it for later reuse.
0f113f3e 4481 */
7984fa68 4482 if (s->session->session_id_length == 0 || s->session->not_resumable)
0f113f3e
MC
4483 return;
4484
d316cdcf
BK
4485 /*
4486 * If sid_ctx_length is 0 there is no specific application context
4487 * associated with this session, so when we try to resume it and
c4fa1f7f
BK
4488 * SSL_VERIFY_PEER is requested to verify the client identity, we have no
4489 * indication that this is actually a session for the proper application
4490 * context, and the *handshake* will fail, not just the resumption attempt.
4491 * Do not cache (on the server) these sessions that are not resumable
4492 * (clients can set SSL_VERIFY_PEER without needing a sid_ctx set).
d316cdcf 4493 */
c4fa1f7f 4494 if (s->server && s->session->sid_ctx_length == 0
d316cdcf
BK
4495 && (s->verify_mode & SSL_VERIFY_PEER) != 0)
4496 return;
4497
0f113f3e 4498 i = s->session_ctx->session_cache_mode;
5d61491c 4499 if ((i & mode) != 0
38b051a1 4500 && (!s->hit || SSL_CONNECTION_IS_TLS13(s))) {
ee94ec2e
MC
4501 /*
4502 * Add the session to the internal cache. In server side TLSv1.3 we
6cc0b3c2
MC
4503 * normally don't do this because by default it's a full stateless ticket
4504 * with only a dummy session id so there is no reason to cache it,
4505 * unless:
ee94ec2e
MC
4506 * - we are doing early_data, in which case we cache so that we can
4507 * detect replays
4508 * - the application has set a remove_session_cb so needs to know about
4509 * session timeout events
6cc0b3c2 4510 * - SSL_OP_NO_TICKET is set in which case it is a stateful ticket
ee94ec2e
MC
4511 */
4512 if ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE) == 0
38b051a1 4513 && (!SSL_CONNECTION_IS_TLS13(s)
ee94ec2e 4514 || !s->server
5d263fb7
MC
4515 || (s->max_early_data > 0
4516 && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0)
6cc0b3c2
MC
4517 || s->session_ctx->remove_session_cb != NULL
4518 || (s->options & SSL_OP_NO_TICKET) != 0))
ee94ec2e
MC
4519 SSL_CTX_add_session(s->session_ctx, s->session);
4520
4521 /*
4522 * Add the session to the external cache. We do this even in server side
4523 * TLSv1.3 without early data because some applications just want to
4524 * know about the creation of a session and aren't doing a full cache.
4525 */
4526 if (s->session_ctx->new_session_cb != NULL) {
4527 SSL_SESSION_up_ref(s->session);
38b051a1
TM
4528 if (!s->session_ctx->new_session_cb(SSL_CONNECTION_GET_SSL(s),
4529 s->session))
ee94ec2e
MC
4530 SSL_SESSION_free(s->session);
4531 }
0f113f3e
MC
4532 }
4533
4534 /* auto flush every 255 connections */
4535 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && ((i & mode) == mode)) {
9ef9088c 4536 TSAN_QUALIFIER int *stat;
acce0557 4537
1fcb4e4d
BK
4538 if (mode & SSL_SESS_CACHE_CLIENT)
4539 stat = &s->session_ctx->stats.sess_connect_good;
4540 else
4541 stat = &s->session_ctx->stats.sess_accept_good;
acce0557 4542 if ((ssl_tsan_load(s->session_ctx, stat) & 0xff) == 0xff)
0f113f3e 4543 SSL_CTX_flush_sessions(s->session_ctx, (unsigned long)time(NULL));
0f113f3e
MC
4544 }
4545}
d02b48c6 4546
3499327b 4547const SSL_METHOD *SSL_CTX_get_ssl_method(const SSL_CTX *ctx)
0f113f3e
MC
4548{
4549 return ctx->method;
4550}
ba168244 4551
3499327b 4552const SSL_METHOD *SSL_get_ssl_method(const SSL *s)
0f113f3e 4553{
26a7d938 4554 return s->method;
0f113f3e 4555}
d02b48c6 4556
4ebb342f 4557int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
0f113f3e 4558{
0f113f3e 4559 int ret = 1;
38b051a1
TM
4560 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
4561
3ea30e76 4562 /* Not allowed for QUIC */
38b051a1 4563 if (sc == NULL
3ea30e76
HL
4564 || (s->type != SSL_TYPE_SSL_CONNECTION && s->method != meth)
4565 || (s->type == SSL_TYPE_SSL_CONNECTION && IS_QUIC_METHOD(meth)))
38b051a1 4566 return 0;
0f113f3e
MC
4567
4568 if (s->method != meth) {
919ba009 4569 const SSL_METHOD *sm = s->method;
38b051a1 4570 int (*hf) (SSL *) = sc->handshake_func;
0f113f3e 4571
919ba009 4572 if (sm->version == meth->version)
0f113f3e
MC
4573 s->method = meth;
4574 else {
38b051a1 4575 sm->ssl_deinit(s);
0f113f3e 4576 s->method = meth;
38b051a1 4577 ret = s->method->ssl_init(s);
0f113f3e
MC
4578 }
4579
919ba009 4580 if (hf == sm->ssl_connect)
38b051a1 4581 sc->handshake_func = meth->ssl_connect;
919ba009 4582 else if (hf == sm->ssl_accept)
38b051a1 4583 sc->handshake_func = meth->ssl_accept;
0f113f3e 4584 }
26a7d938 4585 return ret;
0f113f3e
MC
4586}
4587
4588int SSL_get_error(const SSL *s, int i)
7a2bb210
HL
4589{
4590 return ossl_ssl_get_error(s, i, /*check_err=*/1);
4591}
4592
4593int ossl_ssl_get_error(const SSL *s, int i, int check_err)
0f113f3e
MC
4594{
4595 int reason;
4596 unsigned long l;
4597 BIO *bio;
38b051a1 4598 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
0f113f3e
MC
4599
4600 if (i > 0)
26a7d938 4601 return SSL_ERROR_NONE;
0f113f3e 4602
e30c502a 4603#ifndef OPENSSL_NO_QUIC
6d495cc4
HL
4604 if (IS_QUIC(s)) {
4605 reason = ossl_quic_get_error(s, i);
e30c502a
HL
4606 if (reason != SSL_ERROR_NONE)
4607 return reason;
4608 }
4609#endif
4610
38b051a1
TM
4611 if (sc == NULL)
4612 return SSL_ERROR_SSL;
4613
0f113f3e
MC
4614 /*
4615 * Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
4616 * where we do encode the error
4617 */
7a2bb210 4618 if (check_err && (l = ERR_peek_error()) != 0) {
0f113f3e 4619 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
26a7d938 4620 return SSL_ERROR_SYSCALL;
0f113f3e 4621 else
26a7d938 4622 return SSL_ERROR_SSL;
0f113f3e
MC
4623 }
4624
03bacce8 4625#ifndef OPENSSL_NO_QUIC
6d495cc4 4626 if (!IS_QUIC(s))
03bacce8
HL
4627#endif
4628 {
4629 if (SSL_want_read(s)) {
4630 bio = SSL_get_rbio(s);
4631 if (BIO_should_read(bio))
4632 return SSL_ERROR_WANT_READ;
4633 else if (BIO_should_write(bio))
4634 /*
4635 * This one doesn't make too much sense ... We never try to
4636 * write to the rbio, and an application program where rbio and
4637 * wbio are separate couldn't even know what it should wait for.
4638 * However if we ever set s->rwstate incorrectly (so that we
4639 * have SSL_want_read(s) instead of SSL_want_write(s)) and rbio
4640 * and wbio *are* the same, this test works around that bug; so
4641 * it might be safer to keep it.
4642 */
4643 return SSL_ERROR_WANT_WRITE;
4644 else if (BIO_should_io_special(bio)) {
4645 reason = BIO_get_retry_reason(bio);
4646 if (reason == BIO_RR_CONNECT)
4647 return SSL_ERROR_WANT_CONNECT;
4648 else if (reason == BIO_RR_ACCEPT)
4649 return SSL_ERROR_WANT_ACCEPT;
4650 else
4651 return SSL_ERROR_SYSCALL; /* unknown */
4652 }
4653 }
4654
4655 if (SSL_want_write(s)) {
2e7dc7cd 4656 /*
03bacce8
HL
4657 * Access wbio directly - in order to use the buffered bio if
4658 * present
2e7dc7cd 4659 */
03bacce8
HL
4660 bio = sc->wbio;
4661 if (BIO_should_write(bio))
4662 return SSL_ERROR_WANT_WRITE;
4663 else if (BIO_should_read(bio))
4664 /*
4665 * See above (SSL_want_read(s) with BIO_should_write(bio))
4666 */
4667 return SSL_ERROR_WANT_READ;
4668 else if (BIO_should_io_special(bio)) {
4669 reason = BIO_get_retry_reason(bio);
4670 if (reason == BIO_RR_CONNECT)
4671 return SSL_ERROR_WANT_CONNECT;
4672 else if (reason == BIO_RR_ACCEPT)
4673 return SSL_ERROR_WANT_ACCEPT;
4674 else
4675 return SSL_ERROR_SYSCALL;
4676 }
0f113f3e 4677 }
07bbc92c 4678 }
03bacce8 4679
6b1bb98f 4680 if (SSL_want_x509_lookup(s))
26a7d938 4681 return SSL_ERROR_WANT_X509_LOOKUP;
0c3eb279
DDO
4682 if (SSL_want_retry_verify(s))
4683 return SSL_ERROR_WANT_RETRY_VERIFY;
6b1bb98f 4684 if (SSL_want_async(s))
8051ab2b 4685 return SSL_ERROR_WANT_ASYNC;
6b1bb98f 4686 if (SSL_want_async_job(s))
8051ab2b 4687 return SSL_ERROR_WANT_ASYNC_JOB;
a9c0d8be
DB
4688 if (SSL_want_client_hello_cb(s))
4689 return SSL_ERROR_WANT_CLIENT_HELLO_CB;
8051ab2b 4690
38b051a1
TM
4691 if ((sc->shutdown & SSL_RECEIVED_SHUTDOWN) &&
4692 (sc->s3.warn_alert == SSL_AD_CLOSE_NOTIFY))
26a7d938 4693 return SSL_ERROR_ZERO_RETURN;
8051ab2b 4694
26a7d938 4695 return SSL_ERROR_SYSCALL;
0f113f3e 4696}
d02b48c6 4697
add2f5ca
MC
4698static int ssl_do_handshake_intern(void *vargs)
4699{
38b051a1
TM
4700 struct ssl_async_args *args = (struct ssl_async_args *)vargs;
4701 SSL *s = args->s;
4702 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
add2f5ca 4703
38b051a1
TM
4704 if (sc == NULL)
4705 return -1;
add2f5ca 4706
38b051a1 4707 return sc->handshake_func(s);
add2f5ca
MC
4708}
4709
4f43d0e7 4710int SSL_do_handshake(SSL *s)
0f113f3e
MC
4711{
4712 int ret = 1;
38b051a1
TM
4713 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
4714
6d495cc4
HL
4715#ifndef OPENSSL_NO_QUIC
4716 if (IS_QUIC(s))
4717 return ossl_quic_do_handshake(s);
03bacce8 4718#endif
0f113f3e 4719
38b051a1 4720 if (sc->handshake_func == NULL) {
6849b73c 4721 ERR_raise(ERR_LIB_SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
add2f5ca 4722 return -1;
0f113f3e
MC
4723 }
4724
38b051a1 4725 ossl_statem_check_finish_init(sc, -1);
49e7fe12 4726
c7f47786 4727 s->method->ssl_renegotiate_check(s, 0);
0f113f3e
MC
4728
4729 if (SSL_in_init(s) || SSL_in_before(s)) {
38b051a1 4730 if ((sc->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
add2f5ca
MC
4731 struct ssl_async_args args;
4732
09134f18 4733 memset(&args, 0, sizeof(args));
add2f5ca
MC
4734 args.s = s;
4735
7fecbf6f 4736 ret = ssl_start_async_job(s, &args, ssl_do_handshake_intern);
add2f5ca 4737 } else {
38b051a1 4738 ret = sc->handshake_func(s);
add2f5ca 4739 }
0f113f3e 4740 }
add2f5ca 4741 return ret;
0f113f3e
MC
4742}
4743
4f43d0e7 4744void SSL_set_accept_state(SSL *s)
0f113f3e 4745{
03bacce8 4746 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
38b051a1 4747
6d495cc4
HL
4748#ifndef OPENSSL_NO_QUIC
4749 if (IS_QUIC(s)) {
4750 ossl_quic_set_accept_state(s);
38b051a1 4751 return;
03bacce8
HL
4752 }
4753#endif
38b051a1
TM
4754
4755 sc->server = 1;
4756 sc->shutdown = 0;
4757 ossl_statem_clear(sc);
4758 sc->handshake_func = s->method->ssl_accept;
6d814fd6 4759 /* Ignore return value. Its a void public API function */
4a0e4849 4760 RECORD_LAYER_reset(&sc->rlayer);
0f113f3e 4761}
d02b48c6 4762
4f43d0e7 4763void SSL_set_connect_state(SSL *s)
0f113f3e 4764{
03bacce8 4765 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
38b051a1 4766
6d495cc4
HL
4767#ifndef OPENSSL_NO_QUIC
4768 if (IS_QUIC(s)) {
4769 ossl_quic_set_connect_state(s);
38b051a1 4770 return;
03bacce8
HL
4771 }
4772#endif
38b051a1
TM
4773
4774 sc->server = 0;
4775 sc->shutdown = 0;
4776 ossl_statem_clear(sc);
4777 sc->handshake_func = s->method->ssl_connect;
6d814fd6 4778 /* Ignore return value. Its a void public API function */
4a0e4849 4779 RECORD_LAYER_reset(&sc->rlayer);
0f113f3e 4780}
d02b48c6 4781
4f43d0e7 4782int ssl_undefined_function(SSL *s)
0f113f3e 4783{
6849b73c 4784 ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
26a7d938 4785 return 0;
0f113f3e 4786}
d02b48c6 4787
41a15c4f 4788int ssl_undefined_void_function(void)
0f113f3e 4789{
6849b73c 4790 ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
26a7d938 4791 return 0;
0f113f3e 4792}
41a15c4f 4793
0821bcd4 4794int ssl_undefined_const_function(const SSL *s)
0f113f3e 4795{
26a7d938 4796 return 0;
0f113f3e 4797}
0821bcd4 4798
3eb2aff4 4799const char *ssl_protocol_to_string(int version)
7d650072 4800{
1287dabd 4801 switch (version)
2abacef1
MC
4802 {
4803 case TLS1_3_VERSION:
582a17d6 4804 return "TLSv1.3";
2abacef1
MC
4805
4806 case TLS1_2_VERSION:
7d650072 4807 return "TLSv1.2";
2abacef1
MC
4808
4809 case TLS1_1_VERSION:
7d650072 4810 return "TLSv1.1";
2abacef1
MC
4811
4812 case TLS1_VERSION:
ee3a6c64 4813 return "TLSv1";
2abacef1
MC
4814
4815 case SSL3_VERSION:
7d650072 4816 return "SSLv3";
2abacef1
MC
4817
4818 case DTLS1_BAD_VER:
7d650072 4819 return "DTLSv0.9";
2abacef1
MC
4820
4821 case DTLS1_VERSION:
7d650072 4822 return "DTLSv1";
2abacef1
MC
4823
4824 case DTLS1_2_VERSION:
7d650072 4825 return "DTLSv1.2";
2abacef1
MC
4826
4827 default:
4828 return "unknown";
4829 }
0f113f3e 4830}
d02b48c6 4831
7d650072
KR
4832const char *SSL_get_version(const SSL *s)
4833{
38b051a1
TM
4834 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
4835
50769b15
MC
4836#ifndef OPENSSL_NO_QUIC
4837 /* We only support QUICv1 - so if its QUIC its QUICv1 */
f8636c7e 4838 if (s->type == SSL_TYPE_QUIC_CONNECTION || s->type == SSL_TYPE_QUIC_XSO)
50769b15
MC
4839 return "QUICv1";
4840#endif
4841
38b051a1
TM
4842 if (sc == NULL)
4843 return NULL;
4844
4845 return ssl_protocol_to_string(sc->version);
7d650072
KR
4846}
4847
cee0628e
JC
4848__owur int SSL_get_handshake_rtt(const SSL *s, uint64_t *rtt)
4849{
4850 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
4851
4852 if (sc == NULL)
4853 return -1;
4854 if (sc->ts_msg_write.t <= 0 || sc->ts_msg_read.t <= 0)
4855 return 0; /* data not (yet) available */
4856 if (sc->ts_msg_read.t < sc->ts_msg_write.t)
4857 return -1;
4858
4859 *rtt = ossl_time2us(ossl_time_subtract(sc->ts_msg_read, sc->ts_msg_write));
4860 return 1;
4861}
4862
98732979 4863static int dup_ca_names(STACK_OF(X509_NAME) **dst, STACK_OF(X509_NAME) *src)
0f113f3e
MC
4864{
4865 STACK_OF(X509_NAME) *sk;
4866 X509_NAME *xn;
98732979
MC
4867 int i;
4868
4869 if (src == NULL) {
4870 *dst = NULL;
4871 return 1;
4872 }
4873
4874 if ((sk = sk_X509_NAME_new_null()) == NULL)
4875 return 0;
4876 for (i = 0; i < sk_X509_NAME_num(src); i++) {
4877 xn = X509_NAME_dup(sk_X509_NAME_value(src, i));
4878 if (xn == NULL) {
4879 sk_X509_NAME_pop_free(sk, X509_NAME_free);
4880 return 0;
4881 }
4882 if (sk_X509_NAME_insert(sk, xn, i) == 0) {
4883 X509_NAME_free(xn);
4884 sk_X509_NAME_pop_free(sk, X509_NAME_free);
4885 return 0;
4886 }
4887 }
4888 *dst = sk;
4889
4890 return 1;
4891}
4892
4893SSL *SSL_dup(SSL *s)
4894{
0f113f3e
MC
4895 SSL *ret;
4896 int i;
44cb36d0 4897 /* TODO(QUIC FUTURE): Add a SSL_METHOD function for duplication */
38b051a1
TM
4898 SSL_CONNECTION *retsc;
4899 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
4900
4901 if (sc == NULL)
4902 return NULL;
0f113f3e 4903
919ba009
VD
4904 /* If we're not quiescent, just up_ref! */
4905 if (!SSL_in_init(s) || !SSL_in_before(s)) {
43a07d6d 4906 CRYPTO_UP_REF(&s->references, &i);
919ba009
VD
4907 return s;
4908 }
4909
4910 /*
4911 * Otherwise, copy configuration state, and session if set.
4912 */
0f113f3e 4913 if ((ret = SSL_new(SSL_get_SSL_CTX(s))) == NULL)
26a7d938 4914 return NULL;
38b051a1
TM
4915 if ((retsc = SSL_CONNECTION_FROM_SSL_ONLY(ret)) == NULL)
4916 goto err;
0f113f3e 4917
38b051a1 4918 if (sc->session != NULL) {
919ba009
VD
4919 /*
4920 * Arranges to share the same session via up_ref. This "copies"
4921 * session-id, SSL_METHOD, sid_ctx, and 'cert'
4922 */
61986d32 4923 if (!SSL_copy_session_id(ret, s))
17dd65e6 4924 goto err;
0f113f3e
MC
4925 } else {
4926 /*
4927 * No session has been established yet, so we have to expect that
4928 * s->cert or ret->cert will be changed later -- they should not both
4929 * point to the same object, and thus we can't use
4930 * SSL_copy_session_id.
4931 */
919ba009
VD
4932 if (!SSL_set_ssl_method(ret, s->method))
4933 goto err;
0f113f3e 4934
38b051a1
TM
4935 if (sc->cert != NULL) {
4936 ssl_cert_free(retsc->cert);
4937 retsc->cert = ssl_cert_dup(sc->cert);
4938 if (retsc->cert == NULL)
0f113f3e
MC
4939 goto err;
4940 }
4941
38b051a1
TM
4942 if (!SSL_set_session_id_context(ret, sc->sid_ctx,
4943 (int)sc->sid_ctx_length))
69f68237 4944 goto err;
0f113f3e
MC
4945 }
4946
38b051a1 4947 if (!ssl_dane_dup(retsc, sc))
9f6b22b8 4948 goto err;
38b051a1
TM
4949 retsc->version = sc->version;
4950 retsc->options = sc->options;
4951 retsc->min_proto_version = sc->min_proto_version;
4952 retsc->max_proto_version = sc->max_proto_version;
4953 retsc->mode = sc->mode;
0f113f3e
MC
4954 SSL_set_max_cert_list(ret, SSL_get_max_cert_list(s));
4955 SSL_set_read_ahead(ret, SSL_get_read_ahead(s));
38b051a1
TM
4956 retsc->msg_callback = sc->msg_callback;
4957 retsc->msg_callback_arg = sc->msg_callback_arg;
0f113f3e
MC
4958 SSL_set_verify(ret, SSL_get_verify_mode(s), SSL_get_verify_callback(s));
4959 SSL_set_verify_depth(ret, SSL_get_verify_depth(s));
38b051a1 4960 retsc->generate_session_id = sc->generate_session_id;
0f113f3e
MC
4961
4962 SSL_set_info_callback(ret, SSL_get_info_callback(s));
4963
0f113f3e
MC
4964 /* copy app data, a little dangerous perhaps */
4965 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data))
4966 goto err;
4967
38b051a1
TM
4968 retsc->server = sc->server;
4969 if (sc->handshake_func) {
4970 if (sc->server)
919ba009
VD
4971 SSL_set_accept_state(ret);
4972 else
4973 SSL_set_connect_state(ret);
4974 }
38b051a1
TM
4975 retsc->shutdown = sc->shutdown;
4976 retsc->hit = sc->hit;
0f113f3e 4977
38b051a1
TM
4978 retsc->default_passwd_callback = sc->default_passwd_callback;
4979 retsc->default_passwd_callback_userdata = sc->default_passwd_callback_userdata;
a974e64a 4980
38b051a1 4981 X509_VERIFY_PARAM_inherit(retsc->param, sc->param);
0f113f3e
MC
4982
4983 /* dup the cipher_list and cipher_list_by_id stacks */
38b051a1
TM
4984 if (sc->cipher_list != NULL) {
4985 if ((retsc->cipher_list = sk_SSL_CIPHER_dup(sc->cipher_list)) == NULL)
0f113f3e
MC
4986 goto err;
4987 }
38b051a1
TM
4988 if (sc->cipher_list_by_id != NULL)
4989 if ((retsc->cipher_list_by_id = sk_SSL_CIPHER_dup(sc->cipher_list_by_id))
0f113f3e
MC
4990 == NULL)
4991 goto err;
4992
4993 /* Dup the client_CA list */
38b051a1
TM
4994 if (!dup_ca_names(&retsc->ca_names, sc->ca_names)
4995 || !dup_ca_names(&retsc->client_ca_names, sc->client_ca_names))
98732979
MC
4996 goto err;
4997
66696478 4998 return ret;
0f113f3e 4999
0f113f3e 5000 err:
66696478
RS
5001 SSL_free(ret);
5002 return NULL;
0f113f3e 5003}
d02b48c6 5004
0821bcd4 5005X509 *SSL_get_certificate(const SSL *s)
0f113f3e 5006{
38b051a1
TM
5007 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
5008
5009 if (sc == NULL)
5010 return NULL;
5011
5012 if (sc->cert != NULL)
5013 return sc->cert->key->x509;
0f113f3e 5014 else
26a7d938 5015 return NULL;
0f113f3e 5016}
d02b48c6 5017
a25f9adc 5018EVP_PKEY *SSL_get_privatekey(const SSL *s)
0f113f3e 5019{
38b051a1
TM
5020 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5021
5022 if (sc == NULL)
5023 return NULL;
5024
5025 if (sc->cert != NULL)
5026 return sc->cert->key->privatekey;
0f113f3e 5027 else
26a7d938 5028 return NULL;
0f113f3e 5029}
d02b48c6 5030
a25f9adc 5031X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
0f113f3e
MC
5032{
5033 if (ctx->cert != NULL)
5034 return ctx->cert->key->x509;
5035 else
5036 return NULL;
5037}
a25f9adc
DSH
5038
5039EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
0f113f3e
MC
5040{
5041 if (ctx->cert != NULL)
5042 return ctx->cert->key->privatekey;
5043 else
5044 return NULL;
5045}
a25f9adc 5046
babb3798 5047const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
0f113f3e 5048{
38b051a1
TM
5049 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5050
5051 if (sc == NULL)
5052 return NULL;
5053
5054 if ((sc->session != NULL) && (sc->session->cipher != NULL))
5055 return sc->session->cipher;
26a7d938 5056 return NULL;
0f113f3e
MC
5057}
5058
0aed6e44
BK
5059const SSL_CIPHER *SSL_get_pending_cipher(const SSL *s)
5060{
38b051a1
TM
5061 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5062
5063 if (sc == NULL)
5064 return NULL;
5065
5066 return sc->s3.tmp.new_cipher;
0aed6e44
BK
5067}
5068
3499327b 5069const COMP_METHOD *SSL_get_current_compression(const SSL *s)
0f113f3e 5070{
9a555706 5071#ifndef OPENSSL_NO_COMP
38b051a1
TM
5072 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s);
5073
5074 if (sc == NULL)
5075 return NULL;
5076
1e76110b 5077 return sc->rlayer.wrlmethod->get_compression(sc->rlayer.wrl);
9a555706
RS
5078#else
5079 return NULL;
5080#endif
0f113f3e 5081}
377dcdba 5082
3499327b 5083const COMP_METHOD *SSL_get_current_expansion(const SSL *s)
0f113f3e 5084{
9a555706 5085#ifndef OPENSSL_NO_COMP
38b051a1
TM
5086 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s);
5087
5088 if (sc == NULL)
5089 return NULL;
5090
1e76110b 5091 return sc->rlayer.rrlmethod->get_compression(sc->rlayer.rrl);
9a555706
RS
5092#else
5093 return NULL;
0f113f3e 5094#endif
9a555706 5095}
0f113f3e 5096
38b051a1 5097int ssl_init_wbio_buffer(SSL_CONNECTION *s)
0f113f3e
MC
5098{
5099 BIO *bbio;
5100
2e7dc7cd
MC
5101 if (s->bbio != NULL) {
5102 /* Already buffered. */
5103 return 1;
0f113f3e 5104 }
46417569 5105
2e7dc7cd 5106 bbio = BIO_new(BIO_f_buffer());
25d02f33 5107 if (bbio == NULL || BIO_set_read_buffer_size(bbio, 1) <= 0) {
2e7dc7cd 5108 BIO_free(bbio);
6849b73c 5109 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
46417569 5110 return 0;
0f113f3e 5111 }
2e7dc7cd
MC
5112 s->bbio = bbio;
5113 s->wbio = BIO_push(bbio, s->wbio);
46417569 5114
b5cf81f7
MC
5115 s->rlayer.wrlmethod->set1_bio(s->rlayer.wrl, s->wbio);
5116
46417569 5117 return 1;
0f113f3e 5118}
413c4f45 5119
38b051a1 5120int ssl_free_wbio_buffer(SSL_CONNECTION *s)
0f113f3e 5121{
62adbcee 5122 /* callers ensure s is never null */
0f113f3e 5123 if (s->bbio == NULL)
b77f3ed1 5124 return 1;
0f113f3e 5125
2e7dc7cd 5126 s->wbio = BIO_pop(s->wbio);
b5cf81f7
MC
5127 s->rlayer.wrlmethod->set1_bio(s->rlayer.wrl, s->wbio);
5128
0f113f3e
MC
5129 BIO_free(s->bbio);
5130 s->bbio = NULL;
b77f3ed1
MC
5131
5132 return 1;
0f113f3e
MC
5133}
5134
5135void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode)
5136{
5137 ctx->quiet_shutdown = mode;
5138}
58964a49 5139
0821bcd4 5140int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
0f113f3e 5141{
26a7d938 5142 return ctx->quiet_shutdown;
0f113f3e 5143}
58964a49 5144
0f113f3e
MC
5145void SSL_set_quiet_shutdown(SSL *s, int mode)
5146{
38b051a1
TM
5147 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
5148
7757f5ef 5149 /* Not supported with QUIC */
38b051a1
TM
5150 if (sc == NULL)
5151 return;
5152
5153 sc->quiet_shutdown = mode;
0f113f3e 5154}
58964a49 5155
0821bcd4 5156int SSL_get_quiet_shutdown(const SSL *s)
0f113f3e 5157{
38b051a1
TM
5158 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s);
5159
7757f5ef 5160 /* Not supported with QUIC */
38b051a1
TM
5161 if (sc == NULL)
5162 return 0;
5163
5164 return sc->quiet_shutdown;
0f113f3e 5165}
58964a49 5166
0f113f3e
MC
5167void SSL_set_shutdown(SSL *s, int mode)
5168{
38b051a1
TM
5169 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
5170
7757f5ef 5171 /* Not supported with QUIC */
38b051a1
TM
5172 if (sc == NULL)
5173 return;
5174
5175 sc->shutdown = mode;
0f113f3e 5176}
58964a49 5177
0821bcd4 5178int SSL_get_shutdown(const SSL *s)
0f113f3e 5179{
38b051a1
TM
5180 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL_ONLY(s);
5181
7757f5ef
TM
5182#ifndef OPENSSL_NO_QUIC
5183 /* QUIC: Just indicate whether the connection was shutdown cleanly. */
5184 if (IS_QUIC(s))
5185 return ossl_quic_get_shutdown(s);
5186#endif
5187
38b051a1
TM
5188 if (sc == NULL)
5189 return 0;
5190
5191 return sc->shutdown;
0f113f3e 5192}
58964a49 5193
0821bcd4 5194int SSL_version(const SSL *s)
0f113f3e 5195{
38b051a1
TM
5196 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5197
50769b15
MC
5198#ifndef OPENSSL_NO_QUIC
5199 /* We only support QUICv1 - so if its QUIC its QUICv1 */
f8636c7e 5200 if (s->type == SSL_TYPE_QUIC_CONNECTION || s->type == SSL_TYPE_QUIC_XSO)
50769b15
MC
5201 return OSSL_QUIC1_VERSION;
5202#endif
38b051a1
TM
5203 if (sc == NULL)
5204 return 0;
5205
5206 return sc->version;
6546e9b2
AG
5207}
5208
5209int SSL_client_version(const SSL *s)
5210{
38b051a1
TM
5211 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5212
44cb36d0
TM
5213#ifndef OPENSSL_NO_QUIC
5214 /* We only support QUICv1 - so if its QUIC its QUICv1 */
5215 if (s->type == SSL_TYPE_QUIC_CONNECTION || s->type == SSL_TYPE_QUIC_XSO)
5216 return OSSL_QUIC1_VERSION;
5217#endif
38b051a1
TM
5218 if (sc == NULL)
5219 return 0;
5220
5221 return sc->client_version;
0f113f3e 5222}
58964a49 5223
0821bcd4 5224SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
0f113f3e 5225{
6546e9b2 5226 return ssl->ctx;
0f113f3e
MC
5227}
5228
5229SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx)
5230{
24a0d393 5231 CERT *new_cert;
38b051a1
TM
5232 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
5233
44cb36d0 5234 /* TODO(QUIC FUTURE): Add support for QUIC */
38b051a1
TM
5235 if (sc == NULL)
5236 return NULL;
5237
0f113f3e
MC
5238 if (ssl->ctx == ctx)
5239 return ssl->ctx;
0f113f3e 5240 if (ctx == NULL)
38b051a1 5241 ctx = sc->session_ctx;
24a0d393
KR
5242 new_cert = ssl_cert_dup(ctx->cert);
5243 if (new_cert == NULL) {
5244 return NULL;
0f113f3e 5245 }
21181889 5246
38b051a1 5247 if (!custom_exts_copy_flags(&new_cert->custext, &sc->cert->custext)) {
21181889
MC
5248 ssl_cert_free(new_cert);
5249 return NULL;
5250 }
5251
38b051a1
TM
5252 ssl_cert_free(sc->cert);
5253 sc->cert = new_cert;
0f113f3e
MC
5254
5255 /*
5256 * Program invariant: |sid_ctx| has fixed size (SSL_MAX_SID_CTX_LENGTH),
5257 * so setter APIs must prevent invalid lengths from entering the system.
5258 */
38b051a1 5259 if (!ossl_assert(sc->sid_ctx_length <= sizeof(sc->sid_ctx)))
380a522f 5260 return NULL;
0f113f3e
MC
5261
5262 /*
5263 * If the session ID context matches that of the parent SSL_CTX,
5264 * inherit it from the new SSL_CTX as well. If however the context does
5265 * not match (i.e., it was set per-ssl with SSL_set_session_id_context),
5266 * leave it unchanged.
5267 */
5268 if ((ssl->ctx != NULL) &&
38b051a1
TM
5269 (sc->sid_ctx_length == ssl->ctx->sid_ctx_length) &&
5270 (memcmp(sc->sid_ctx, ssl->ctx->sid_ctx, sc->sid_ctx_length) == 0)) {
5271 sc->sid_ctx_length = ctx->sid_ctx_length;
5272 memcpy(&sc->sid_ctx, &ctx->sid_ctx, sizeof(sc->sid_ctx));
0f113f3e
MC
5273 }
5274
16203f7b 5275 SSL_CTX_up_ref(ctx);
a230b26e 5276 SSL_CTX_free(ssl->ctx); /* decrement reference count */
0f113f3e
MC
5277 ssl->ctx = ctx;
5278
16203f7b 5279 return ssl->ctx;
0f113f3e 5280}
ed3883d2 5281
4f43d0e7 5282int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
0f113f3e 5283{
d8652be0
MC
5284 return X509_STORE_set_default_paths_ex(ctx->cert_store, ctx->libctx,
5285 ctx->propq);
0f113f3e 5286}
58964a49 5287
d84a7b20
MC
5288int SSL_CTX_set_default_verify_dir(SSL_CTX *ctx)
5289{
5290 X509_LOOKUP *lookup;
5291
5292 lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_hash_dir());
5293 if (lookup == NULL)
5294 return 0;
6dcb100f
RL
5295
5296 /* We ignore errors, in case the directory doesn't exist */
5297 ERR_set_mark();
5298
d84a7b20
MC
5299 X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT);
5300
6dcb100f 5301 ERR_pop_to_mark();
d84a7b20
MC
5302
5303 return 1;
5304}
5305
5306int SSL_CTX_set_default_verify_file(SSL_CTX *ctx)
5307{
5308 X509_LOOKUP *lookup;
5309
5310 lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_file());
5311 if (lookup == NULL)
5312 return 0;
5313
492bc359 5314 /* We ignore errors, in case the file doesn't exist */
6dcb100f
RL
5315 ERR_set_mark();
5316
d8652be0
MC
5317 X509_LOOKUP_load_file_ex(lookup, NULL, X509_FILETYPE_DEFAULT, ctx->libctx,
5318 ctx->propq);
d84a7b20 5319
6dcb100f
RL
5320 ERR_pop_to_mark();
5321
5322 return 1;
5323}
5324
5325int SSL_CTX_set_default_verify_store(SSL_CTX *ctx)
5326{
5327 X509_LOOKUP *lookup;
5328
5329 lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_store());
5330 if (lookup == NULL)
5331 return 0;
5332
5333 /* We ignore errors, in case the directory doesn't exist */
5334 ERR_set_mark();
5335
d8652be0 5336 X509_LOOKUP_add_store_ex(lookup, NULL, ctx->libctx, ctx->propq);
6dcb100f
RL
5337
5338 ERR_pop_to_mark();
d84a7b20
MC
5339
5340 return 1;
5341}
5342
6dcb100f
RL
5343int SSL_CTX_load_verify_file(SSL_CTX *ctx, const char *CAfile)
5344{
d8652be0
MC
5345 return X509_STORE_load_file_ex(ctx->cert_store, CAfile, ctx->libctx,
5346 ctx->propq);
6dcb100f
RL
5347}
5348
5349int SSL_CTX_load_verify_dir(SSL_CTX *ctx, const char *CApath)
5350{
5351 return X509_STORE_load_path(ctx->cert_store, CApath);
5352}
5353
5354int SSL_CTX_load_verify_store(SSL_CTX *ctx, const char *CAstore)
5355{
d8652be0
MC
5356 return X509_STORE_load_store_ex(ctx->cert_store, CAstore, ctx->libctx,
5357 ctx->propq);
6dcb100f
RL
5358}
5359
303c0028 5360int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
0f113f3e
MC
5361 const char *CApath)
5362{
6dcb100f
RL
5363 if (CAfile == NULL && CApath == NULL)
5364 return 0;
5365 if (CAfile != NULL && !SSL_CTX_load_verify_file(ctx, CAfile))
5366 return 0;
5367 if (CApath != NULL && !SSL_CTX_load_verify_dir(ctx, CApath))
5368 return 0;
5369 return 1;
0f113f3e 5370}
58964a49 5371
45d87a1f 5372void SSL_set_info_callback(SSL *ssl,
0f113f3e
MC
5373 void (*cb) (const SSL *ssl, int type, int val))
5374{
38b051a1
TM
5375 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
5376
5377 if (sc == NULL)
5378 return;
5379
5380 sc->info_callback = cb;
0f113f3e
MC
5381}
5382
5383/*
5384 * One compiler (Diab DCC) doesn't like argument names in returned function
5385 * pointer.
5386 */
5387void (*SSL_get_info_callback(const SSL *ssl)) (const SSL * /* ssl */ ,
5388 int /* type */ ,
5389 int /* val */ ) {
38b051a1
TM
5390 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
5391
5392 if (sc == NULL)
5393 return NULL;
5394
5395 return sc->info_callback;
0f113f3e 5396}
58964a49 5397
0f113f3e
MC
5398void SSL_set_verify_result(SSL *ssl, long arg)
5399{
38b051a1
TM
5400 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
5401
5402 if (sc == NULL)
5403 return;
5404
5405 sc->verify_result = arg;
0f113f3e 5406}
58964a49 5407
0821bcd4 5408long SSL_get_verify_result(const SSL *ssl)
0f113f3e 5409{
38b051a1
TM
5410 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
5411
5412 if (sc == NULL)
5413 return 0;
5414
5415 return sc->verify_result;
0f113f3e
MC
5416}
5417
d9f1c639 5418size_t SSL_get_client_random(const SSL *ssl, unsigned char *out, size_t outlen)
858618e7 5419{
38b051a1
TM
5420 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
5421
5422 if (sc == NULL)
5423 return 0;
5424
6b8f5d0d 5425 if (outlen == 0)
38b051a1
TM
5426 return sizeof(sc->s3.client_random);
5427 if (outlen > sizeof(sc->s3.client_random))
5428 outlen = sizeof(sc->s3.client_random);
5429 memcpy(out, sc->s3.client_random, outlen);
d9f1c639 5430 return outlen;
858618e7
NM
5431}
5432
d9f1c639 5433size_t SSL_get_server_random(const SSL *ssl, unsigned char *out, size_t outlen)
858618e7 5434{
38b051a1
TM
5435 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
5436
5437 if (sc == NULL)
5438 return 0;
5439
6b8f5d0d 5440 if (outlen == 0)
38b051a1
TM
5441 return sizeof(sc->s3.server_random);
5442 if (outlen > sizeof(sc->s3.server_random))
5443 outlen = sizeof(sc->s3.server_random);
5444 memcpy(out, sc->s3.server_random, outlen);
d9f1c639 5445 return outlen;
858618e7
NM
5446}
5447
d9f1c639 5448size_t SSL_SESSION_get_master_key(const SSL_SESSION *session,
a230b26e 5449 unsigned char *out, size_t outlen)
858618e7 5450{
d9f1c639
MC
5451 if (outlen == 0)
5452 return session->master_key_length;
8c1a5343 5453 if (outlen > session->master_key_length)
858618e7
NM
5454 outlen = session->master_key_length;
5455 memcpy(out, session->master_key, outlen);
d9f1c639 5456 return outlen;
858618e7
NM
5457}
5458
725b0f1e 5459int SSL_SESSION_set1_master_key(SSL_SESSION *sess, const unsigned char *in,
911d63f2
MC
5460 size_t len)
5461{
5462 if (len > sizeof(sess->master_key))
5463 return 0;
5464
5465 memcpy(sess->master_key, in, len);
5466 sess->master_key_length = len;
911d63f2
MC
5467 return 1;
5468}
5469
5470
0f113f3e
MC
5471int SSL_set_ex_data(SSL *s, int idx, void *arg)
5472{
26a7d938 5473 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
0f113f3e
MC
5474}
5475
5476void *SSL_get_ex_data(const SSL *s, int idx)
5477{
26a7d938 5478 return CRYPTO_get_ex_data(&s->ex_data, idx);
0f113f3e
MC
5479}
5480
0f113f3e
MC
5481int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg)
5482{
26a7d938 5483 return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
0f113f3e
MC
5484}
5485
5486void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx)
5487{
26a7d938 5488 return CRYPTO_get_ex_data(&s->ex_data, idx);
0f113f3e 5489}
58964a49 5490
0821bcd4 5491X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
0f113f3e 5492{
26a7d938 5493 return ctx->cert_store;
0f113f3e 5494}
413c4f45 5495
0f113f3e
MC
5496void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store)
5497{
222561fe 5498 X509_STORE_free(ctx->cert_store);
0f113f3e
MC
5499 ctx->cert_store = store;
5500}
413c4f45 5501
b50052db
TS
5502void SSL_CTX_set1_cert_store(SSL_CTX *ctx, X509_STORE *store)
5503{
5504 if (store != NULL)
5505 X509_STORE_up_ref(store);
5506 SSL_CTX_set_cert_store(ctx, store);
5507}
5508
0821bcd4 5509int SSL_want(const SSL *s)
0f113f3e 5510{
38b051a1
TM
5511 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5512
5debf070
HL
5513#ifndef OPENSSL_NO_QUIC
5514 if (IS_QUIC(s))
5515 return ossl_quic_want(s);
5516#endif
5517
38b051a1
TM
5518 if (sc == NULL)
5519 return SSL_NOTHING;
5520
5521 return sc->rwstate;
0f113f3e 5522}
413c4f45 5523
ddac1974
NL
5524#ifndef OPENSSL_NO_PSK
5525int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
0f113f3e
MC
5526{
5527 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
6849b73c 5528 ERR_raise(ERR_LIB_SSL, SSL_R_DATA_LENGTH_TOO_LONG);
0f113f3e
MC
5529 return 0;
5530 }
df6da24b 5531 OPENSSL_free(ctx->cert->psk_identity_hint);
0f113f3e 5532 if (identity_hint != NULL) {
7644a9ae 5533 ctx->cert->psk_identity_hint = OPENSSL_strdup(identity_hint);
df6da24b 5534 if (ctx->cert->psk_identity_hint == NULL)
0f113f3e
MC
5535 return 0;
5536 } else
df6da24b 5537 ctx->cert->psk_identity_hint = NULL;
0f113f3e
MC
5538 return 1;
5539}
ddac1974
NL
5540
5541int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
0f113f3e 5542{
38b051a1
TM
5543 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
5544
5545 if (sc == NULL)
0f113f3e
MC
5546 return 0;
5547
0f113f3e 5548 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
6849b73c 5549 ERR_raise(ERR_LIB_SSL, SSL_R_DATA_LENGTH_TOO_LONG);
0f113f3e
MC
5550 return 0;
5551 }
38b051a1 5552 OPENSSL_free(sc->cert->psk_identity_hint);
0f113f3e 5553 if (identity_hint != NULL) {
38b051a1
TM
5554 sc->cert->psk_identity_hint = OPENSSL_strdup(identity_hint);
5555 if (sc->cert->psk_identity_hint == NULL)
0f113f3e
MC
5556 return 0;
5557 } else
38b051a1 5558 sc->cert->psk_identity_hint = NULL;
0f113f3e
MC
5559 return 1;
5560}
ddac1974
NL
5561
5562const char *SSL_get_psk_identity_hint(const SSL *s)
0f113f3e 5563{
38b051a1
TM
5564 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5565
5566 if (sc == NULL || sc->session == NULL)
0f113f3e 5567 return NULL;
38b051a1
TM
5568
5569 return sc->session->psk_identity_hint;
0f113f3e 5570}
ddac1974
NL
5571
5572const char *SSL_get_psk_identity(const SSL *s)
0f113f3e 5573{
38b051a1
TM
5574 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5575
5576 if (sc == NULL || sc->session == NULL)
0f113f3e 5577 return NULL;
38b051a1
TM
5578
5579 return sc->session->psk_identity;
0f113f3e 5580}
7806f3dd 5581
8cbfcc70 5582void SSL_set_psk_client_callback(SSL *s, SSL_psk_client_cb_func cb)
0f113f3e 5583{
38b051a1
TM
5584 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
5585
5586 if (sc == NULL)
5587 return;
5588
5589 sc->psk_client_callback = cb;
0f113f3e 5590}
7806f3dd 5591
8cbfcc70 5592void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx, SSL_psk_client_cb_func cb)
0f113f3e
MC
5593{
5594 ctx->psk_client_callback = cb;
5595}
7806f3dd 5596
8cbfcc70 5597void SSL_set_psk_server_callback(SSL *s, SSL_psk_server_cb_func cb)
0f113f3e 5598{
38b051a1
TM
5599 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
5600
5601 if (sc == NULL)
5602 return;
5603
5604 sc->psk_server_callback = cb;
0f113f3e 5605}
7806f3dd 5606
8cbfcc70 5607void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx, SSL_psk_server_cb_func cb)
0f113f3e
MC
5608{
5609 ctx->psk_server_callback = cb;
5610}
5611#endif
5612
f46184bd
MC
5613void SSL_set_psk_find_session_callback(SSL *s, SSL_psk_find_session_cb_func cb)
5614{
38b051a1
TM
5615 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
5616
5617 if (sc == NULL)
5618 return;
5619
5620 sc->psk_find_session_cb = cb;
f46184bd
MC
5621}
5622
5623void SSL_CTX_set_psk_find_session_callback(SSL_CTX *ctx,
5624 SSL_psk_find_session_cb_func cb)
5625{
5626 ctx->psk_find_session_cb = cb;
5627}
5628
5629void SSL_set_psk_use_session_callback(SSL *s, SSL_psk_use_session_cb_func cb)
5630{
38b051a1
TM
5631 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
5632
5633 if (sc == NULL)
5634 return;
5635
5636 sc->psk_use_session_cb = cb;
f46184bd
MC
5637}
5638
5639void SSL_CTX_set_psk_use_session_callback(SSL_CTX *ctx,
5640 SSL_psk_use_session_cb_func cb)
5641{
5642 ctx->psk_use_session_cb = cb;
5643}
5644
0f113f3e
MC
5645void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
5646 void (*cb) (int write_p, int version,
5647 int content_type, const void *buf,
5648 size_t len, SSL *ssl, void *arg))
5649{
5650 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
5651}
5652
5653void SSL_set_msg_callback(SSL *ssl,
5654 void (*cb) (int write_p, int version,
5655 int content_type, const void *buf,
5656 size_t len, SSL *ssl, void *arg))
5657{
5658 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
5659}
a661b653 5660
7c2d4fee 5661void SSL_CTX_set_not_resumable_session_callback(SSL_CTX *ctx,
0f113f3e
MC
5662 int (*cb) (SSL *ssl,
5663 int
5664 is_forward_secure))
5665{
5666 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB,
5667 (void (*)(void))cb);
5668}
5669
7c2d4fee 5670void SSL_set_not_resumable_session_callback(SSL *ssl,
0f113f3e
MC
5671 int (*cb) (SSL *ssl,
5672 int is_forward_secure))
5673{
5674 SSL_callback_ctrl(ssl, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB,
5675 (void (*)(void))cb);
5676}
5677
c649d10d
TS
5678void SSL_CTX_set_record_padding_callback(SSL_CTX *ctx,
5679 size_t (*cb) (SSL *ssl, int type,
5680 size_t len, void *arg))
5681{
5682 ctx->record_padding_cb = cb;
5683}
5684
5685void SSL_CTX_set_record_padding_callback_arg(SSL_CTX *ctx, void *arg)
5686{
5687 ctx->record_padding_arg = arg;
5688}
5689
3499327b 5690void *SSL_CTX_get_record_padding_callback_arg(const SSL_CTX *ctx)
c649d10d
TS
5691{
5692 return ctx->record_padding_arg;
5693}
5694
5695int SSL_CTX_set_block_padding(SSL_CTX *ctx, size_t block_size)
5696{
6e5550a1
HL
5697 if (IS_QUIC_CTX(ctx) && block_size > 1)
5698 return 0;
5699
c649d10d
TS
5700 /* block size of 0 or 1 is basically no padding */
5701 if (block_size == 1)
5702 ctx->block_padding = 0;
5703 else if (block_size <= SSL3_RT_MAX_PLAIN_LENGTH)
5704 ctx->block_padding = block_size;
5705 else
5706 return 0;
5707 return 1;
5708}
5709
a6d36303 5710int SSL_set_record_padding_callback(SSL *ssl,
c649d10d
TS
5711 size_t (*cb) (SSL *ssl, int type,
5712 size_t len, void *arg))
5713{
a6d36303 5714 BIO *b;
9562842b 5715 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
38b051a1 5716
9562842b 5717 if (sc == NULL)
38b051a1 5718 return 0;
a6d36303
VF
5719
5720 b = SSL_get_wbio(ssl);
5721 if (b == NULL || !BIO_get_ktls_send(b)) {
eb7d6c2a 5722 sc->rlayer.record_padding_cb = cb;
a6d36303
VF
5723 return 1;
5724 }
5725 return 0;
c649d10d
TS
5726}
5727
5728void SSL_set_record_padding_callback_arg(SSL *ssl, void *arg)
5729{
38b051a1
TM
5730 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
5731
5732 if (sc == NULL)
5733 return;
5734
eb7d6c2a 5735 sc->rlayer.record_padding_arg = arg;
c649d10d
TS
5736}
5737
3499327b 5738void *SSL_get_record_padding_callback_arg(const SSL *ssl)
c649d10d 5739{
38b051a1
TM
5740 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
5741
5742 if (sc == NULL)
5743 return NULL;
5744
eb7d6c2a 5745 return sc->rlayer.record_padding_arg;
c649d10d
TS
5746}
5747
5748int SSL_set_block_padding(SSL *ssl, size_t block_size)
5749{
38b051a1
TM
5750 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
5751
d6e7ebba 5752 if (sc == NULL || (IS_QUIC(ssl) && block_size > 1))
38b051a1
TM
5753 return 0;
5754
c649d10d
TS
5755 /* block size of 0 or 1 is basically no padding */
5756 if (block_size == 1)
eb7d6c2a 5757 sc->rlayer.block_padding = 0;
c649d10d 5758 else if (block_size <= SSL3_RT_MAX_PLAIN_LENGTH)
eb7d6c2a 5759 sc->rlayer.block_padding = block_size;
c649d10d
TS
5760 else
5761 return 0;
5762 return 1;
5763}
5764
9d0a8bb7
MC
5765int SSL_set_num_tickets(SSL *s, size_t num_tickets)
5766{
38b051a1
TM
5767 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
5768
5769 if (sc == NULL)
5770 return 0;
5771
5772 sc->num_tickets = num_tickets;
9d0a8bb7
MC
5773
5774 return 1;
5775}
5776
3499327b 5777size_t SSL_get_num_tickets(const SSL *s)
9d0a8bb7 5778{
38b051a1
TM
5779 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5780
5781 if (sc == NULL)
5782 return 0;
5783
5784 return sc->num_tickets;
9d0a8bb7
MC
5785}
5786
5787int SSL_CTX_set_num_tickets(SSL_CTX *ctx, size_t num_tickets)
5788{
5789 ctx->num_tickets = num_tickets;
5790
5791 return 1;
5792}
5793
3499327b 5794size_t SSL_CTX_get_num_tickets(const SSL_CTX *ctx)
9d0a8bb7
MC
5795{
5796 return ctx->num_tickets;
5797}
5798
48fbcbac 5799/* Retrieve handshake hashes */
38b051a1
TM
5800int ssl_handshake_hash(SSL_CONNECTION *s,
5801 unsigned char *out, size_t outlen,
8c1a5343 5802 size_t *hashlen)
48fbcbac 5803{
6e59a892 5804 EVP_MD_CTX *ctx = NULL;
555cbb32 5805 EVP_MD_CTX *hdgst = s->s3.handshake_dgst;
ed576acd 5806 int hashleni = EVP_MD_CTX_get_size(hdgst);
8c1a5343
MC
5807 int ret = 0;
5808
f63a17d6 5809 if (hashleni < 0 || (size_t)hashleni > outlen) {
c48ffbcc 5810 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
28ba2541 5811 goto err;
f63a17d6 5812 }
8c1a5343 5813
bfb0641f 5814 ctx = EVP_MD_CTX_new();
147ed5f9
TL
5815 if (ctx == NULL) {
5816 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
6e59a892 5817 goto err;
147ed5f9 5818 }
8c1a5343 5819
6e59a892 5820 if (!EVP_MD_CTX_copy_ex(ctx, hdgst)
f63a17d6 5821 || EVP_DigestFinal_ex(ctx, out, NULL) <= 0) {
c48ffbcc 5822 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
8c1a5343 5823 goto err;
f63a17d6 5824 }
8c1a5343
MC
5825
5826 *hashlen = hashleni;
5827
5828 ret = 1;
48fbcbac 5829 err:
bfb0641f 5830 EVP_MD_CTX_free(ctx);
48fbcbac
DSH
5831 return ret;
5832}
5833
c04b66b1 5834int SSL_session_reused(const SSL *s)
0f113f3e 5835{
38b051a1
TM
5836 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5837
5838 if (sc == NULL)
5839 return 0;
5840
5841 return sc->hit;
0f113f3e 5842}
08557cf2 5843
69443116 5844int SSL_is_server(const SSL *s)
0f113f3e 5845{
38b051a1
TM
5846 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5847
5848 if (sc == NULL)
5849 return 0;
5850
5851 return sc->server;
0f113f3e 5852}
87adf1fa 5853
00db8c60 5854#ifndef OPENSSL_NO_DEPRECATED_1_1_0
47153c72
RS
5855void SSL_set_debug(SSL *s, int debug)
5856{
5857 /* Old function was do-nothing anyway... */
5858 (void)s;
5859 (void)debug;
5860}
5861#endif
5862
b362ccab 5863void SSL_set_security_level(SSL *s, int level)
0f113f3e 5864{
38b051a1
TM
5865 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
5866
5867 if (sc == NULL)
5868 return;
5869
5870 sc->cert->sec_level = level;
0f113f3e 5871}
b362ccab
DSH
5872
5873int SSL_get_security_level(const SSL *s)
0f113f3e 5874{
38b051a1
TM
5875 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5876
5877 if (sc == NULL)
5878 return 0;
5879
5880 return sc->cert->sec_level;
0f113f3e 5881}
b362ccab 5882
0f113f3e 5883void SSL_set_security_callback(SSL *s,
a230b26e
EK
5884 int (*cb) (const SSL *s, const SSL_CTX *ctx,
5885 int op, int bits, int nid,
5886 void *other, void *ex))
0f113f3e 5887{
38b051a1
TM
5888 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
5889
5890 if (sc == NULL)
5891 return;
5892
5893 sc->cert->sec_cb = cb;
0f113f3e 5894}
b362ccab 5895
a230b26e
EK
5896int (*SSL_get_security_callback(const SSL *s)) (const SSL *s,
5897 const SSL_CTX *ctx, int op,
5898 int bits, int nid, void *other,
5899 void *ex) {
38b051a1
TM
5900 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5901
5902 if (sc == NULL)
5903 return NULL;
5904
5905 return sc->cert->sec_cb;
0f113f3e 5906}
b362ccab
DSH
5907
5908void SSL_set0_security_ex_data(SSL *s, void *ex)
0f113f3e 5909{
38b051a1
TM
5910 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
5911
5912 if (sc == NULL)
5913 return;
5914
5915 sc->cert->sec_ex = ex;
0f113f3e 5916}
b362ccab
DSH
5917
5918void *SSL_get0_security_ex_data(const SSL *s)
0f113f3e 5919{
38b051a1
TM
5920 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5921
5922 if (sc == NULL)
5923 return NULL;
5924
5925 return sc->cert->sec_ex;
0f113f3e 5926}
b362ccab
DSH
5927
5928void SSL_CTX_set_security_level(SSL_CTX *ctx, int level)
0f113f3e
MC
5929{
5930 ctx->cert->sec_level = level;
5931}
b362ccab
DSH
5932
5933int SSL_CTX_get_security_level(const SSL_CTX *ctx)
0f113f3e
MC
5934{
5935 return ctx->cert->sec_level;
5936}
b362ccab 5937
0f113f3e 5938void SSL_CTX_set_security_callback(SSL_CTX *ctx,
a230b26e
EK
5939 int (*cb) (const SSL *s, const SSL_CTX *ctx,
5940 int op, int bits, int nid,
5941 void *other, void *ex))
0f113f3e
MC
5942{
5943 ctx->cert->sec_cb = cb;
5944}
b362ccab 5945
e4646a89
KR
5946int (*SSL_CTX_get_security_callback(const SSL_CTX *ctx)) (const SSL *s,
5947 const SSL_CTX *ctx,
0f113f3e
MC
5948 int op, int bits,
5949 int nid,
5950 void *other,
5951 void *ex) {
5952 return ctx->cert->sec_cb;
5953}
b362ccab
DSH
5954
5955void SSL_CTX_set0_security_ex_data(SSL_CTX *ctx, void *ex)
0f113f3e
MC
5956{
5957 ctx->cert->sec_ex = ex;
5958}
b362ccab
DSH
5959
5960void *SSL_CTX_get0_security_ex_data(const SSL_CTX *ctx)
0f113f3e
MC
5961{
5962 return ctx->cert->sec_ex;
5963}
b362ccab 5964
56bd1783 5965uint64_t SSL_CTX_get_options(const SSL_CTX *ctx)
8106cb8b
VD
5966{
5967 return ctx->options;
5968}
a230b26e 5969
56bd1783 5970uint64_t SSL_get_options(const SSL *s)
8106cb8b 5971{
38b051a1
TM
5972 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
5973
f0d9757c
HL
5974#ifndef OPENSSL_NO_QUIC
5975 if (IS_QUIC(s))
5976 return ossl_quic_get_options(s);
5977#endif
5978
38b051a1
TM
5979 if (sc == NULL)
5980 return 0;
5981
5982 return sc->options;
8106cb8b 5983}
a230b26e 5984
56bd1783 5985uint64_t SSL_CTX_set_options(SSL_CTX *ctx, uint64_t op)
8106cb8b
VD
5986{
5987 return ctx->options |= op;
5988}
a230b26e 5989
56bd1783 5990uint64_t SSL_set_options(SSL *s, uint64_t op)
8106cb8b 5991{
a02571a0 5992 SSL_CONNECTION *sc;
4566dae7 5993 OSSL_PARAM options[2], *opts = options;
38b051a1 5994
a02571a0 5995#ifndef OPENSSL_NO_QUIC
f0d9757c
HL
5996 if (IS_QUIC(s))
5997 return ossl_quic_set_options(s, op);
a02571a0
TM
5998#endif
5999
f0d9757c
HL
6000 sc = SSL_CONNECTION_FROM_SSL(s);
6001 if (sc == NULL)
38b051a1
TM
6002 return 0;
6003
4566dae7
MC
6004 sc->options |= op;
6005
6006 *opts++ = OSSL_PARAM_construct_uint64(OSSL_LIBSSL_RECORD_LAYER_PARAM_OPTIONS,
6007 &sc->options);
6008 *opts = OSSL_PARAM_construct_end();
6009
6010 /* Ignore return value */
6011 sc->rlayer.rrlmethod->set_options(sc->rlayer.rrl, options);
e8e95f20 6012 sc->rlayer.wrlmethod->set_options(sc->rlayer.wrl, options);
4566dae7
MC
6013
6014 return sc->options;
8106cb8b 6015}
a230b26e 6016
56bd1783 6017uint64_t SSL_CTX_clear_options(SSL_CTX *ctx, uint64_t op)
8106cb8b
VD
6018{
6019 return ctx->options &= ~op;
6020}
a230b26e 6021
56bd1783 6022uint64_t SSL_clear_options(SSL *s, uint64_t op)
8106cb8b 6023{
38b051a1 6024 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
e8e95f20 6025 OSSL_PARAM options[2], *opts = options;
38b051a1 6026
f0d9757c
HL
6027#ifndef OPENSSL_NO_QUIC
6028 if (IS_QUIC(s))
6029 return ossl_quic_clear_options(s, op);
6030#endif
6031
38b051a1
TM
6032 if (sc == NULL)
6033 return 0;
6034
e8e95f20 6035 sc->options &= ~op;
6036
6037 *opts++ = OSSL_PARAM_construct_uint64(OSSL_LIBSSL_RECORD_LAYER_PARAM_OPTIONS,
6038 &sc->options);
6039 *opts = OSSL_PARAM_construct_end();
6040
6041 /* Ignore return value */
6042 sc->rlayer.rrlmethod->set_options(sc->rlayer.rrl, options);
6043 sc->rlayer.wrlmethod->set_options(sc->rlayer.wrl, options);
6044
6045 return sc->options;
8106cb8b
VD
6046}
6047
696178ed
DSH
6048STACK_OF(X509) *SSL_get0_verified_chain(const SSL *s)
6049{
38b051a1
TM
6050 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
6051
6052 if (sc == NULL)
6053 return NULL;
6054
6055 return sc->verified_chain;
696178ed
DSH
6056}
6057
0f113f3e 6058IMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER, ssl_cipher_id);
ed29e82a
RP
6059
6060#ifndef OPENSSL_NO_CT
6061
6062/*
6063 * Moves SCTs from the |src| stack to the |dst| stack.
6064 * The source of each SCT will be set to |origin|.
6065 * If |dst| points to a NULL pointer, a new stack will be created and owned by
6066 * the caller.
6067 * Returns the number of SCTs moved, or a negative integer if an error occurs.
a435d786
BE
6068 * The |dst| stack is created and possibly partially populated even in case
6069 * of error, likewise the |src| stack may be left in an intermediate state.
ed29e82a 6070 */
a230b26e
EK
6071static int ct_move_scts(STACK_OF(SCT) **dst, STACK_OF(SCT) *src,
6072 sct_source_t origin)
ed29e82a
RP
6073{
6074 int scts_moved = 0;
6075 SCT *sct = NULL;
6076
6077 if (*dst == NULL) {
6078 *dst = sk_SCT_new_null();
6079 if (*dst == NULL) {
e077455e 6080 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
ed29e82a
RP
6081 goto err;
6082 }
6083 }
6084
a8086e6b 6085 while ((sct = sk_SCT_pop(src)) != NULL) {
ed29e82a
RP
6086 if (SCT_set_source(sct, origin) != 1)
6087 goto err;
6088
a435d786 6089 if (!sk_SCT_push(*dst, sct))
ed29e82a
RP
6090 goto err;
6091 scts_moved += 1;
6092 }
6093
6094 return scts_moved;
a230b26e 6095 err:
a435d786 6096 SCT_free(sct);
cc7113e8 6097 return -1;
ed29e82a
RP
6098}
6099
6100/*
a230b26e 6101 * Look for data collected during ServerHello and parse if found.
6b13bd1d 6102 * Returns the number of SCTs extracted.
a230b26e 6103 */
38b051a1 6104static int ct_extract_tls_extension_scts(SSL_CONNECTION *s)
ed29e82a
RP
6105{
6106 int scts_extracted = 0;
6107
aff8c126
RS
6108 if (s->ext.scts != NULL) {
6109 const unsigned char *p = s->ext.scts;
6110 STACK_OF(SCT) *scts = o2i_SCT_LIST(NULL, &p, s->ext.scts_len);
ed29e82a
RP
6111
6112 scts_extracted = ct_move_scts(&s->scts, scts, SCT_SOURCE_TLS_EXTENSION);
6113
6114 SCT_LIST_free(scts);
6115 }
6116
6117 return scts_extracted;
6118}
6119
6120/*
6121 * Checks for an OCSP response and then attempts to extract any SCTs found if it
6122 * contains an SCT X509 extension. They will be stored in |s->scts|.
6123 * Returns:
6124 * - The number of SCTs extracted, assuming an OCSP response exists.
6125 * - 0 if no OCSP response exists or it contains no SCTs.
6126 * - A negative integer if an error occurs.
6127 */
38b051a1 6128static int ct_extract_ocsp_response_scts(SSL_CONNECTION *s)
ed29e82a 6129{
a230b26e 6130# ifndef OPENSSL_NO_OCSP
ed29e82a
RP
6131 int scts_extracted = 0;
6132 const unsigned char *p;
6133 OCSP_BASICRESP *br = NULL;
6134 OCSP_RESPONSE *rsp = NULL;
6135 STACK_OF(SCT) *scts = NULL;
6136 int i;
6137
aff8c126 6138 if (s->ext.ocsp.resp == NULL || s->ext.ocsp.resp_len == 0)
ed29e82a
RP
6139 goto err;
6140
aff8c126
RS
6141 p = s->ext.ocsp.resp;
6142 rsp = d2i_OCSP_RESPONSE(NULL, &p, (int)s->ext.ocsp.resp_len);
ed29e82a
RP
6143 if (rsp == NULL)
6144 goto err;
6145
6146 br = OCSP_response_get1_basic(rsp);
6147 if (br == NULL)
6148 goto err;
6149
6150 for (i = 0; i < OCSP_resp_count(br); ++i) {
6151 OCSP_SINGLERESP *single = OCSP_resp_get0(br, i);
6152
6153 if (single == NULL)
6154 continue;
6155
a230b26e
EK
6156 scts =
6157 OCSP_SINGLERESP_get1_ext_d2i(single, NID_ct_cert_scts, NULL, NULL);
6158 scts_extracted =
6159 ct_move_scts(&s->scts, scts, SCT_SOURCE_OCSP_STAPLED_RESPONSE);
ed29e82a
RP
6160 if (scts_extracted < 0)
6161 goto err;
6162 }
a230b26e 6163 err:
ed29e82a
RP
6164 SCT_LIST_free(scts);
6165 OCSP_BASICRESP_free(br);
6166 OCSP_RESPONSE_free(rsp);
6167 return scts_extracted;
a230b26e 6168# else
3e41ac35
MC
6169 /* Behave as if no OCSP response exists */
6170 return 0;
a230b26e 6171# endif
ed29e82a
RP
6172}
6173
6174/*
6175 * Attempts to extract SCTs from the peer certificate.
6176 * Return the number of SCTs extracted, or a negative integer if an error
6177 * occurs.
6178 */
38b051a1 6179static int ct_extract_x509v3_extension_scts(SSL_CONNECTION *s)
ed29e82a
RP
6180{
6181 int scts_extracted = 0;
3f3c7d26 6182 X509 *cert = s->session != NULL ? s->session->peer : NULL;
ed29e82a
RP
6183
6184 if (cert != NULL) {
6185 STACK_OF(SCT) *scts =
6186 X509_get_ext_d2i(cert, NID_ct_precert_scts, NULL, NULL);
6187
6188 scts_extracted =
6189 ct_move_scts(&s->scts, scts, SCT_SOURCE_X509V3_EXTENSION);
6190
6191 SCT_LIST_free(scts);
6192 }
6193
6194 return scts_extracted;
6195}
6196
6197/*
6198 * Attempts to find all received SCTs by checking TLS extensions, the OCSP
6199 * response (if it exists) and X509v3 extensions in the certificate.
6200 * Returns NULL if an error occurs.
6201 */
6202const STACK_OF(SCT) *SSL_get0_peer_scts(SSL *s)
6203{
38b051a1
TM
6204 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6205
6206 if (sc == NULL)
6207 return NULL;
6208
6209 if (!sc->scts_parsed) {
6210 if (ct_extract_tls_extension_scts(sc) < 0 ||
6211 ct_extract_ocsp_response_scts(sc) < 0 ||
6212 ct_extract_x509v3_extension_scts(sc) < 0)
ed29e82a
RP
6213 goto err;
6214
38b051a1 6215 sc->scts_parsed = 1;
ed29e82a 6216 }
38b051a1 6217 return sc->scts;
a230b26e 6218 err:
ed29e82a
RP
6219 return NULL;
6220}
6221
bbaeadb0 6222static int ct_permissive(const CT_POLICY_EVAL_CTX *ctx,
43341433 6223 const STACK_OF(SCT) *scts, void *unused_arg)
ed29e82a 6224{
43341433
VD
6225 return 1;
6226}
6227
bbaeadb0 6228static int ct_strict(const CT_POLICY_EVAL_CTX *ctx,
43341433
VD
6229 const STACK_OF(SCT) *scts, void *unused_arg)
6230{
6231 int count = scts != NULL ? sk_SCT_num(scts) : 0;
6232 int i;
ed29e82a 6233
43341433
VD
6234 for (i = 0; i < count; ++i) {
6235 SCT *sct = sk_SCT_value(scts, i);
6236 int status = SCT_get_validation_status(sct);
6237
6238 if (status == SCT_VALIDATION_STATUS_VALID)
6239 return 1;
6240 }
6849b73c 6241 ERR_raise(ERR_LIB_SSL, SSL_R_NO_VALID_SCTS);
43341433
VD
6242 return 0;
6243}
6244
6245int SSL_set_ct_validation_callback(SSL *s, ssl_ct_validation_cb callback,
6246 void *arg)
6247{
38b051a1
TM
6248 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6249
6250 if (sc == NULL)
6251 return 0;
6252
ed29e82a
RP
6253 /*
6254 * Since code exists that uses the custom extension handler for CT, look
6255 * for this and throw an error if they have already registered to use CT.
6256 */
6257 if (callback != NULL && SSL_CTX_has_client_custom_ext(s->ctx,
a230b26e
EK
6258 TLSEXT_TYPE_signed_certificate_timestamp))
6259 {
6849b73c 6260 ERR_raise(ERR_LIB_SSL, SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED);
43341433 6261 return 0;
ed29e82a
RP
6262 }
6263
ed29e82a 6264 if (callback != NULL) {
a230b26e
EK
6265 /*
6266 * If we are validating CT, then we MUST accept SCTs served via OCSP
6267 */
ed29e82a 6268 if (!SSL_set_tlsext_status_type(s, TLSEXT_STATUSTYPE_ocsp))
43341433 6269 return 0;
ed29e82a
RP
6270 }
6271
38b051a1
TM
6272 sc->ct_validation_callback = callback;
6273 sc->ct_validation_callback_arg = arg;
43341433
VD
6274
6275 return 1;
ed29e82a
RP
6276}
6277
43341433 6278int SSL_CTX_set_ct_validation_callback(SSL_CTX *ctx,
a230b26e 6279 ssl_ct_validation_cb callback, void *arg)
ed29e82a 6280{
ed29e82a
RP
6281 /*
6282 * Since code exists that uses the custom extension handler for CT, look for
6283 * this and throw an error if they have already registered to use CT.
6284 */
6285 if (callback != NULL && SSL_CTX_has_client_custom_ext(ctx,
a230b26e
EK
6286 TLSEXT_TYPE_signed_certificate_timestamp))
6287 {
6849b73c 6288 ERR_raise(ERR_LIB_SSL, SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED);
43341433 6289 return 0;
ed29e82a
RP
6290 }
6291
6292 ctx->ct_validation_callback = callback;
6293 ctx->ct_validation_callback_arg = arg;
43341433 6294 return 1;
ed29e82a
RP
6295}
6296
43341433 6297int SSL_ct_is_enabled(const SSL *s)
ed29e82a 6298{
38b051a1
TM
6299 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
6300
6301 if (sc == NULL)
6302 return 0;
6303
6304 return sc->ct_validation_callback != NULL;
ed29e82a
RP
6305}
6306
43341433 6307int SSL_CTX_ct_is_enabled(const SSL_CTX *ctx)
ed29e82a 6308{
43341433 6309 return ctx->ct_validation_callback != NULL;
ed29e82a
RP
6310}
6311
38b051a1 6312int ssl_validate_ct(SSL_CONNECTION *s)
ed29e82a
RP
6313{
6314 int ret = 0;
3f3c7d26 6315 X509 *cert = s->session != NULL ? s->session->peer : NULL;
43341433 6316 X509 *issuer;
b9aec69a 6317 SSL_DANE *dane = &s->dane;
ed29e82a
RP
6318 CT_POLICY_EVAL_CTX *ctx = NULL;
6319 const STACK_OF(SCT) *scts;
6320
43341433
VD
6321 /*
6322 * If no callback is set, the peer is anonymous, or its chain is invalid,
6323 * skip SCT validation - just return success. Applications that continue
6324 * handshakes without certificates, with unverified chains, or pinned leaf
6325 * certificates are outside the scope of the WebPKI and CT.
6326 *
6327 * The above exclusions notwithstanding the vast majority of peers will
6328 * have rather ordinary certificate chains validated by typical
6329 * applications that perform certificate verification and therefore will
6330 * process SCTs when enabled.
6331 */
6332 if (s->ct_validation_callback == NULL || cert == NULL ||
6333 s->verify_result != X509_V_OK ||
a230b26e 6334 s->verified_chain == NULL || sk_X509_num(s->verified_chain) <= 1)
ed29e82a
RP
6335 return 1;
6336
43341433
VD
6337 /*
6338 * CT not applicable for chains validated via DANE-TA(2) or DANE-EE(3)
6339 * trust-anchors. See https://tools.ietf.org/html/rfc7671#section-4.2
6340 */
6341 if (DANETLS_ENABLED(dane) && dane->mtlsa != NULL) {
6342 switch (dane->mtlsa->usage) {
6343 case DANETLS_USAGE_DANE_TA:
6344 case DANETLS_USAGE_DANE_EE:
6345 return 1;
6346 }
ed29e82a
RP
6347 }
6348
38b051a1
TM
6349 ctx = CT_POLICY_EVAL_CTX_new_ex(SSL_CONNECTION_GET_CTX(s)->libctx,
6350 SSL_CONNECTION_GET_CTX(s)->propq);
ed29e82a 6351 if (ctx == NULL) {
e077455e 6352 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CT_LIB);
ed29e82a
RP
6353 goto end;
6354 }
6355
43341433 6356 issuer = sk_X509_value(s->verified_chain, 1);
a1bb7708
RP
6357 CT_POLICY_EVAL_CTX_set1_cert(ctx, cert);
6358 CT_POLICY_EVAL_CTX_set1_issuer(ctx, issuer);
38b051a1
TM
6359 CT_POLICY_EVAL_CTX_set_shared_CTLOG_STORE(ctx,
6360 SSL_CONNECTION_GET_CTX(s)->ctlog_store);
6a71e06d 6361 CT_POLICY_EVAL_CTX_set_time(
38b051a1 6362 ctx, (uint64_t)SSL_SESSION_get_time(s->session) * 1000);
ed29e82a 6363
38b051a1 6364 scts = SSL_get0_peer_scts(SSL_CONNECTION_GET_SSL(s));
ed29e82a 6365
43341433
VD
6366 /*
6367 * This function returns success (> 0) only when all the SCTs are valid, 0
6368 * when some are invalid, and < 0 on various internal errors (out of
6369 * memory, etc.). Having some, or even all, invalid SCTs is not sufficient
6370 * reason to abort the handshake, that decision is up to the callback.
6371 * Therefore, we error out only in the unexpected case that the return
6372 * value is negative.
6373 *
6374 * XXX: One might well argue that the return value of this function is an
f430ba31 6375 * unfortunate design choice. Its job is only to determine the validation
43341433
VD
6376 * status of each of the provided SCTs. So long as it correctly separates
6377 * the wheat from the chaff it should return success. Failure in this case
6378 * ought to correspond to an inability to carry out its duties.
6379 */
6380 if (SCT_LIST_validate(scts, ctx) < 0) {
c48ffbcc 6381 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_SCT_VERIFICATION_FAILED);
ed29e82a
RP
6382 goto end;
6383 }
6384
6385 ret = s->ct_validation_callback(ctx, scts, s->ct_validation_callback_arg);
6386 if (ret < 0)
a230b26e 6387 ret = 0; /* This function returns 0 on failure */
f63a17d6 6388 if (!ret)
c48ffbcc 6389 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_CALLBACK_FAILED);
ed29e82a 6390
a230b26e 6391 end:
ed29e82a 6392 CT_POLICY_EVAL_CTX_free(ctx);
f75b34c8
VD
6393 /*
6394 * With SSL_VERIFY_NONE the session may be cached and re-used despite a
6395 * failure return code here. Also the application may wish the complete
6396 * the handshake, and then disconnect cleanly at a higher layer, after
6397 * checking the verification status of the completed connection.
6398 *
6399 * We therefore force a certificate verification failure which will be
6400 * visible via SSL_get_verify_result() and cached as part of any resumed
6401 * session.
6402 *
6403 * Note: the permissive callback is for information gathering only, always
6404 * returns success, and does not affect verification status. Only the
6405 * strict callback or a custom application-specified callback can trigger
6406 * connection failure or record a verification error.
6407 */
6408 if (ret <= 0)
6409 s->verify_result = X509_V_ERR_NO_VALID_SCTS;
ed29e82a
RP
6410 return ret;
6411}
6412
43341433
VD
6413int SSL_CTX_enable_ct(SSL_CTX *ctx, int validation_mode)
6414{
6415 switch (validation_mode) {
6416 default:
6849b73c 6417 ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_CT_VALIDATION_TYPE);
43341433
VD
6418 return 0;
6419 case SSL_CT_VALIDATION_PERMISSIVE:
6420 return SSL_CTX_set_ct_validation_callback(ctx, ct_permissive, NULL);
6421 case SSL_CT_VALIDATION_STRICT:
6422 return SSL_CTX_set_ct_validation_callback(ctx, ct_strict, NULL);
6423 }
6424}
6425
6426int SSL_enable_ct(SSL *s, int validation_mode)
6427{
6428 switch (validation_mode) {
6429 default:
6849b73c 6430 ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_CT_VALIDATION_TYPE);
43341433
VD
6431 return 0;
6432 case SSL_CT_VALIDATION_PERMISSIVE:
6433 return SSL_set_ct_validation_callback(s, ct_permissive, NULL);
6434 case SSL_CT_VALIDATION_STRICT:
6435 return SSL_set_ct_validation_callback(s, ct_strict, NULL);
6436 }
6437}
6438
ed29e82a
RP
6439int SSL_CTX_set_default_ctlog_list_file(SSL_CTX *ctx)
6440{
328f36c5 6441 return CTLOG_STORE_load_default_file(ctx->ctlog_store);
ed29e82a
RP
6442}
6443
6444int SSL_CTX_set_ctlog_list_file(SSL_CTX *ctx, const char *path)
6445{
6446 return CTLOG_STORE_load_file(ctx->ctlog_store, path);
6447}
6448
bbaeadb0 6449void SSL_CTX_set0_ctlog_store(SSL_CTX *ctx, CTLOG_STORE *logs)
8359b57f
RP
6450{
6451 CTLOG_STORE_free(ctx->ctlog_store);
6452 ctx->ctlog_store = logs;
6453}
6454
6455const CTLOG_STORE *SSL_CTX_get0_ctlog_store(const SSL_CTX *ctx)
6456{
6457 return ctx->ctlog_store;
6458}
6459
6b1bb98f
BK
6460#endif /* OPENSSL_NO_CT */
6461
a9c0d8be
DB
6462void SSL_CTX_set_client_hello_cb(SSL_CTX *c, SSL_client_hello_cb_fn cb,
6463 void *arg)
6b1bb98f 6464{
a9c0d8be
DB
6465 c->client_hello_cb = cb;
6466 c->client_hello_cb_arg = arg;
6b1bb98f
BK
6467}
6468
a9c0d8be 6469int SSL_client_hello_isv2(SSL *s)
6b1bb98f 6470{
38b051a1
TM
6471 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6472
6473 if (sc == NULL)
6474 return 0;
6475
6476 if (sc->clienthello == NULL)
6b1bb98f 6477 return 0;
38b051a1 6478 return sc->clienthello->isv2;
6b1bb98f
BK
6479}
6480
a9c0d8be 6481unsigned int SSL_client_hello_get0_legacy_version(SSL *s)
6b1bb98f 6482{
38b051a1
TM
6483 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6484
6485 if (sc == NULL)
6486 return 0;
6487
6488 if (sc->clienthello == NULL)
6b1bb98f 6489 return 0;
38b051a1 6490 return sc->clienthello->legacy_version;
6b1bb98f
BK
6491}
6492
a9c0d8be 6493size_t SSL_client_hello_get0_random(SSL *s, const unsigned char **out)
6b1bb98f 6494{
38b051a1
TM
6495 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6496
6497 if (sc == NULL)
6498 return 0;
6499
6500 if (sc->clienthello == NULL)
6b1bb98f
BK
6501 return 0;
6502 if (out != NULL)
38b051a1 6503 *out = sc->clienthello->random;
6b1bb98f
BK
6504 return SSL3_RANDOM_SIZE;
6505}
6506
a9c0d8be 6507size_t SSL_client_hello_get0_session_id(SSL *s, const unsigned char **out)
6b1bb98f 6508{
38b051a1
TM
6509 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6510
6511 if (sc == NULL)
6512 return 0;
6513
6514 if (sc->clienthello == NULL)
6b1bb98f
BK
6515 return 0;
6516 if (out != NULL)
38b051a1
TM
6517 *out = sc->clienthello->session_id;
6518 return sc->clienthello->session_id_len;
6b1bb98f
BK
6519}
6520
a9c0d8be 6521size_t SSL_client_hello_get0_ciphers(SSL *s, const unsigned char **out)
6b1bb98f 6522{
38b051a1
TM
6523 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6524
6525 if (sc == NULL)
6526 return 0;
6527
6528 if (sc->clienthello == NULL)
6b1bb98f
BK
6529 return 0;
6530 if (out != NULL)
38b051a1
TM
6531 *out = PACKET_data(&sc->clienthello->ciphersuites);
6532 return PACKET_remaining(&sc->clienthello->ciphersuites);
6b1bb98f
BK
6533}
6534
a9c0d8be 6535size_t SSL_client_hello_get0_compression_methods(SSL *s, const unsigned char **out)
6b1bb98f 6536{
38b051a1
TM
6537 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6538
6539 if (sc == NULL)
6540 return 0;
6541
6542 if (sc->clienthello == NULL)
6b1bb98f
BK
6543 return 0;
6544 if (out != NULL)
38b051a1
TM
6545 *out = sc->clienthello->compressions;
6546 return sc->clienthello->compressions_len;
6b1bb98f
BK
6547}
6548
a9c0d8be 6549int SSL_client_hello_get1_extensions_present(SSL *s, int **out, size_t *outlen)
193b5d76
BK
6550{
6551 RAW_EXTENSION *ext;
6552 int *present;
6553 size_t num = 0, i;
38b051a1 6554 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
193b5d76 6555
38b051a1 6556 if (sc == NULL)
193b5d76 6557 return 0;
38b051a1
TM
6558
6559 if (sc->clienthello == NULL || out == NULL || outlen == NULL)
6560 return 0;
6561 for (i = 0; i < sc->clienthello->pre_proc_exts_len; i++) {
6562 ext = sc->clienthello->pre_proc_exts + i;
193b5d76
BK
6563 if (ext->present)
6564 num++;
6565 }
6fda11ae 6566 if (num == 0) {
6567 *out = NULL;
6568 *outlen = 0;
6569 return 1;
6570 }
e077455e 6571 if ((present = OPENSSL_malloc(sizeof(*present) * num)) == NULL)
193b5d76 6572 return 0;
38b051a1
TM
6573 for (i = 0; i < sc->clienthello->pre_proc_exts_len; i++) {
6574 ext = sc->clienthello->pre_proc_exts + i;
193b5d76
BK
6575 if (ext->present) {
6576 if (ext->received_order >= num)
6577 goto err;
6578 present[ext->received_order] = ext->type;
6579 }
6580 }
6581 *out = present;
6582 *outlen = num;
6583 return 1;
6584 err:
6585 OPENSSL_free(present);
6586 return 0;
6587}
6588
13a53fbf
PL
6589int SSL_client_hello_get_extension_order(SSL *s, uint16_t *exts, size_t *num_exts)
6590{
6591 RAW_EXTENSION *ext;
6592 size_t num = 0, i;
38b051a1
TM
6593 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6594
6595 if (sc == NULL)
6596 return 0;
13a53fbf 6597
38b051a1 6598 if (sc->clienthello == NULL || num_exts == NULL)
13a53fbf 6599 return 0;
38b051a1
TM
6600 for (i = 0; i < sc->clienthello->pre_proc_exts_len; i++) {
6601 ext = sc->clienthello->pre_proc_exts + i;
13a53fbf
PL
6602 if (ext->present)
6603 num++;
6604 }
6605 if (num == 0) {
6606 *num_exts = 0;
6607 return 1;
6608 }
6609 if (exts == NULL) {
6610 *num_exts = num;
6611 return 1;
6612 }
6613 if (*num_exts < num)
6614 return 0;
38b051a1
TM
6615 for (i = 0; i < sc->clienthello->pre_proc_exts_len; i++) {
6616 ext = sc->clienthello->pre_proc_exts + i;
13a53fbf
PL
6617 if (ext->present) {
6618 if (ext->received_order >= num)
6619 return 0;
6620 exts[ext->received_order] = ext->type;
6621 }
6622 }
6623 *num_exts = num;
6624 return 1;
6625}
6626
a9c0d8be 6627int SSL_client_hello_get0_ext(SSL *s, unsigned int type, const unsigned char **out,
6b1bb98f
BK
6628 size_t *outlen)
6629{
6630 size_t i;
6631 RAW_EXTENSION *r;
38b051a1 6632 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6b1bb98f 6633
38b051a1 6634 if (sc == NULL)
6b1bb98f 6635 return 0;
38b051a1
TM
6636
6637 if (sc->clienthello == NULL)
6638 return 0;
6639 for (i = 0; i < sc->clienthello->pre_proc_exts_len; ++i) {
6640 r = sc->clienthello->pre_proc_exts + i;
6b1bb98f
BK
6641 if (r->present && r->type == type) {
6642 if (out != NULL)
6643 *out = PACKET_data(&r->data);
6644 if (outlen != NULL)
6645 *outlen = PACKET_remaining(&r->data);
6646 return 1;
6647 }
6648 }
6649 return 0;
6650}
2faa1b48 6651
a58eb06d
TS
6652int SSL_free_buffers(SSL *ssl)
6653{
38b051a1 6654 RECORD_LAYER *rl;
9562842b 6655 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
38b051a1
TM
6656
6657 if (sc == NULL)
6658 return 0;
6659
6660 rl = &sc->rlayer;
a58eb06d 6661
7eb39ecb
MC
6662 return rl->rrlmethod->free_buffers(rl->rrl)
6663 && rl->wrlmethod->free_buffers(rl->wrl);
a58eb06d
TS
6664}
6665
6666int SSL_alloc_buffers(SSL *ssl)
6667{
7eb39ecb 6668 RECORD_LAYER *rl;
38b051a1
TM
6669 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
6670
6671 if (sc == NULL)
6672 return 0;
6673
fe33e2c8 6674 /* QUIC always has buffers allocated. */
d6e7ebba 6675 if (IS_QUIC(ssl))
fe33e2c8
HL
6676 return 1;
6677
7eb39ecb
MC
6678 rl = &sc->rlayer;
6679
6680 return rl->rrlmethod->alloc_buffers(rl->rrl)
6681 && rl->wrlmethod->alloc_buffers(rl->wrl);
a58eb06d
TS
6682}
6683
2faa1b48
CB
6684void SSL_CTX_set_keylog_callback(SSL_CTX *ctx, SSL_CTX_keylog_cb_func cb)
6685{
6686 ctx->keylog_callback = cb;
6687}
6688
6689SSL_CTX_keylog_cb_func SSL_CTX_get_keylog_callback(const SSL_CTX *ctx)
6690{
6691 return ctx->keylog_callback;
6692}
6693
6694static int nss_keylog_int(const char *prefix,
38b051a1 6695 SSL_CONNECTION *sc,
2faa1b48
CB
6696 const uint8_t *parameter_1,
6697 size_t parameter_1_len,
6698 const uint8_t *parameter_2,
6699 size_t parameter_2_len)
6700{
6701 char *out = NULL;
6702 char *cursor = NULL;
6703 size_t out_len = 0;
6704 size_t i;
6705 size_t prefix_len;
38b051a1 6706 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(sc);
2faa1b48 6707
38b051a1 6708 if (sctx->keylog_callback == NULL)
20870286 6709 return 1;
2faa1b48
CB
6710
6711 /*
6712 * Our output buffer will contain the following strings, rendered with
6713 * space characters in between, terminated by a NULL character: first the
6714 * prefix, then the first parameter, then the second parameter. The
6715 * meaning of each parameter depends on the specific key material being
6716 * logged. Note that the first and second parameters are encoded in
6717 * hexadecimal, so we need a buffer that is twice their lengths.
6718 */
6719 prefix_len = strlen(prefix);
e931f370 6720 out_len = prefix_len + (2 * parameter_1_len) + (2 * parameter_2_len) + 3;
e077455e 6721 if ((out = cursor = OPENSSL_malloc(out_len)) == NULL)
2faa1b48 6722 return 0;
2faa1b48
CB
6723
6724 strcpy(cursor, prefix);
6725 cursor += prefix_len;
6726 *cursor++ = ' ';
6727
6728 for (i = 0; i < parameter_1_len; i++) {
6729 sprintf(cursor, "%02x", parameter_1[i]);
6730 cursor += 2;
6731 }
6732 *cursor++ = ' ';
6733
6734 for (i = 0; i < parameter_2_len; i++) {
6735 sprintf(cursor, "%02x", parameter_2[i]);
6736 cursor += 2;
6737 }
6738 *cursor = '\0';
6739
38b051a1 6740 sctx->keylog_callback(SSL_CONNECTION_GET_SSL(sc), (const char *)out);
e931f370 6741 OPENSSL_clear_free(out, out_len);
2faa1b48
CB
6742 return 1;
6743
6744}
6745
38b051a1 6746int ssl_log_rsa_client_key_exchange(SSL_CONNECTION *sc,
2faa1b48
CB
6747 const uint8_t *encrypted_premaster,
6748 size_t encrypted_premaster_len,
6749 const uint8_t *premaster,
6750 size_t premaster_len)
6751{
6752 if (encrypted_premaster_len < 8) {
38b051a1 6753 SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
2faa1b48
CB
6754 return 0;
6755 }
6756
f0deb4d3 6757 /* We only want the first 8 bytes of the encrypted premaster as a tag. */
2faa1b48 6758 return nss_keylog_int("RSA",
38b051a1 6759 sc,
2faa1b48 6760 encrypted_premaster,
f0deb4d3 6761 8,
2faa1b48
CB
6762 premaster,
6763 premaster_len);
6764}
6765
38b051a1 6766int ssl_log_secret(SSL_CONNECTION *sc,
2c7bd692
CB
6767 const char *label,
6768 const uint8_t *secret,
6769 size_t secret_len)
2faa1b48 6770{
2c7bd692 6771 return nss_keylog_int(label,
38b051a1
TM
6772 sc,
6773 sc->s3.client_random,
2c7bd692
CB
6774 SSL3_RANDOM_SIZE,
6775 secret,
6776 secret_len);
2faa1b48
CB
6777}
6778
ccb8e6e0
BK
6779#define SSLV2_CIPHER_LEN 3
6780
38b051a1 6781int ssl_cache_cipherlist(SSL_CONNECTION *s, PACKET *cipher_suites, int sslv2format)
ccb8e6e0 6782{
ccb8e6e0 6783 int n;
ccb8e6e0
BK
6784
6785 n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN;
6786
6787 if (PACKET_remaining(cipher_suites) == 0) {
c48ffbcc 6788 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_NO_CIPHERS_SPECIFIED);
90134d98 6789 return 0;
ccb8e6e0
BK
6790 }
6791
6792 if (PACKET_remaining(cipher_suites) % n != 0) {
c48ffbcc 6793 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
90134d98 6794 return 0;
ccb8e6e0
BK
6795 }
6796
555cbb32
TS
6797 OPENSSL_free(s->s3.tmp.ciphers_raw);
6798 s->s3.tmp.ciphers_raw = NULL;
6799 s->s3.tmp.ciphers_rawlen = 0;
ccb8e6e0
BK
6800
6801 if (sslv2format) {
6802 size_t numciphers = PACKET_remaining(cipher_suites) / n;
6803 PACKET sslv2ciphers = *cipher_suites;
6804 unsigned int leadbyte;
6805 unsigned char *raw;
6806
6807 /*
6808 * We store the raw ciphers list in SSLv3+ format so we need to do some
6809 * preprocessing to convert the list first. If there are any SSLv2 only
6810 * ciphersuites with a non-zero leading byte then we are going to
6811 * slightly over allocate because we won't store those. But that isn't a
6812 * problem.
6813 */
6814 raw = OPENSSL_malloc(numciphers * TLS_CIPHER_LEN);
555cbb32 6815 s->s3.tmp.ciphers_raw = raw;
ccb8e6e0 6816 if (raw == NULL) {
e077455e 6817 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
f63a17d6 6818 return 0;
ccb8e6e0 6819 }
555cbb32 6820 for (s->s3.tmp.ciphers_rawlen = 0;
ccb8e6e0
BK
6821 PACKET_remaining(&sslv2ciphers) > 0;
6822 raw += TLS_CIPHER_LEN) {
6823 if (!PACKET_get_1(&sslv2ciphers, &leadbyte)
6824 || (leadbyte == 0
6825 && !PACKET_copy_bytes(&sslv2ciphers, raw,
6826 TLS_CIPHER_LEN))
6827 || (leadbyte != 0
6828 && !PACKET_forward(&sslv2ciphers, TLS_CIPHER_LEN))) {
c48ffbcc 6829 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_PACKET);
555cbb32
TS
6830 OPENSSL_free(s->s3.tmp.ciphers_raw);
6831 s->s3.tmp.ciphers_raw = NULL;
6832 s->s3.tmp.ciphers_rawlen = 0;
f63a17d6 6833 return 0;
ccb8e6e0
BK
6834 }
6835 if (leadbyte == 0)
555cbb32 6836 s->s3.tmp.ciphers_rawlen += TLS_CIPHER_LEN;
ccb8e6e0 6837 }
555cbb32
TS
6838 } else if (!PACKET_memdup(cipher_suites, &s->s3.tmp.ciphers_raw,
6839 &s->s3.tmp.ciphers_rawlen)) {
c48ffbcc 6840 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
f63a17d6 6841 return 0;
ccb8e6e0 6842 }
90134d98 6843 return 1;
90134d98
BK
6844}
6845
6846int SSL_bytes_to_cipher_list(SSL *s, const unsigned char *bytes, size_t len,
6847 int isv2format, STACK_OF(SSL_CIPHER) **sk,
6848 STACK_OF(SSL_CIPHER) **scsvs)
6849{
90134d98 6850 PACKET pkt;
38b051a1
TM
6851 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
6852
6853 if (sc == NULL)
6854 return 0;
90134d98
BK
6855
6856 if (!PACKET_buf_init(&pkt, bytes, len))
6857 return 0;
38b051a1 6858 return ossl_bytes_to_cipher_list(sc, &pkt, sk, scsvs, isv2format, 0);
90134d98
BK
6859}
6860
38b051a1
TM
6861int ossl_bytes_to_cipher_list(SSL_CONNECTION *s, PACKET *cipher_suites,
6862 STACK_OF(SSL_CIPHER) **skp,
6863 STACK_OF(SSL_CIPHER) **scsvs_out,
6864 int sslv2format, int fatal)
90134d98
BK
6865{
6866 const SSL_CIPHER *c;
6867 STACK_OF(SSL_CIPHER) *sk = NULL;
6868 STACK_OF(SSL_CIPHER) *scsvs = NULL;
6869 int n;
6870 /* 3 = SSLV2_CIPHER_LEN > TLS_CIPHER_LEN = 2. */
6871 unsigned char cipher[SSLV2_CIPHER_LEN];
6872
6873 n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN;
6874
6875 if (PACKET_remaining(cipher_suites) == 0) {
f63a17d6 6876 if (fatal)
c48ffbcc 6877 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_NO_CIPHERS_SPECIFIED);
f63a17d6 6878 else
6849b73c 6879 ERR_raise(ERR_LIB_SSL, SSL_R_NO_CIPHERS_SPECIFIED);
90134d98
BK
6880 return 0;
6881 }
6882
6883 if (PACKET_remaining(cipher_suites) % n != 0) {
f63a17d6 6884 if (fatal)
c48ffbcc 6885 SSLfatal(s, SSL_AD_DECODE_ERROR,
f63a17d6
MC
6886 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
6887 else
6849b73c 6888 ERR_raise(ERR_LIB_SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
90134d98
BK
6889 return 0;
6890 }
6891
6892 sk = sk_SSL_CIPHER_new_null();
6893 scsvs = sk_SSL_CIPHER_new_null();
6894 if (sk == NULL || scsvs == NULL) {
f63a17d6 6895 if (fatal)
e077455e 6896 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
f63a17d6 6897 else
e077455e 6898 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
90134d98
BK
6899 goto err;
6900 }
ccb8e6e0
BK
6901
6902 while (PACKET_copy_bytes(cipher_suites, cipher, n)) {
6903 /*
6904 * SSLv3 ciphers wrapped in an SSLv2-compatible ClientHello have the
6905 * first byte set to zero, while true SSLv2 ciphers have a non-zero
6906 * first byte. We don't support any true SSLv2 ciphers, so skip them.
6907 */
6908 if (sslv2format && cipher[0] != '\0')
6909 continue;
6910
ccb8e6e0
BK
6911 /* For SSLv2-compat, ignore leading 0-byte. */
6912 c = ssl_get_cipher_by_char(s, sslv2format ? &cipher[1] : cipher, 1);
6913 if (c != NULL) {
90134d98
BK
6914 if ((c->valid && !sk_SSL_CIPHER_push(sk, c)) ||
6915 (!c->valid && !sk_SSL_CIPHER_push(scsvs, c))) {
f63a17d6 6916 if (fatal)
e077455e 6917 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
f63a17d6 6918 else
e077455e 6919 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
ccb8e6e0
BK
6920 goto err;
6921 }
6922 }
6923 }
6924 if (PACKET_remaining(cipher_suites) > 0) {
f63a17d6 6925 if (fatal)
c48ffbcc 6926 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH);
f63a17d6 6927 else
6849b73c 6928 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_LENGTH);
ccb8e6e0
BK
6929 goto err;
6930 }
6931
90134d98
BK
6932 if (skp != NULL)
6933 *skp = sk;
6934 else
6935 sk_SSL_CIPHER_free(sk);
6936 if (scsvs_out != NULL)
6937 *scsvs_out = scsvs;
6938 else
6939 sk_SSL_CIPHER_free(scsvs);
6940 return 1;
ccb8e6e0
BK
6941 err:
6942 sk_SSL_CIPHER_free(sk);
90134d98
BK
6943 sk_SSL_CIPHER_free(scsvs);
6944 return 0;
ccb8e6e0 6945}
3fc8d856
MC
6946
6947int SSL_CTX_set_max_early_data(SSL_CTX *ctx, uint32_t max_early_data)
6948{
6949 ctx->max_early_data = max_early_data;
6950
6951 return 1;
6952}
6953
46dcb945 6954uint32_t SSL_CTX_get_max_early_data(const SSL_CTX *ctx)
3fc8d856
MC
6955{
6956 return ctx->max_early_data;
6957}
6958
6959int SSL_set_max_early_data(SSL *s, uint32_t max_early_data)
6960{
9562842b 6961 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
38b051a1 6962
9562842b 6963 if (sc == NULL)
38b051a1
TM
6964 return 0;
6965
6966 sc->max_early_data = max_early_data;
3fc8d856
MC
6967
6968 return 1;
6969}
6970
a8e75d56 6971uint32_t SSL_get_max_early_data(const SSL *s)
3fc8d856 6972{
38b051a1
TM
6973 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
6974
6975 if (sc == NULL)
6976 return 0;
6977
6978 return sc->max_early_data;
3fc8d856 6979}
ae3947de 6980
4e8548e8
MC
6981int SSL_CTX_set_recv_max_early_data(SSL_CTX *ctx, uint32_t recv_max_early_data)
6982{
6983 ctx->recv_max_early_data = recv_max_early_data;
6984
6985 return 1;
6986}
6987
6988uint32_t SSL_CTX_get_recv_max_early_data(const SSL_CTX *ctx)
6989{
6990 return ctx->recv_max_early_data;
6991}
6992
6993int SSL_set_recv_max_early_data(SSL *s, uint32_t recv_max_early_data)
6994{
9562842b 6995 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
38b051a1 6996
9562842b 6997 if (sc == NULL)
38b051a1
TM
6998 return 0;
6999
7000 sc->recv_max_early_data = recv_max_early_data;
4e8548e8
MC
7001
7002 return 1;
7003}
7004
7005uint32_t SSL_get_recv_max_early_data(const SSL *s)
7006{
38b051a1
TM
7007 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
7008
7009 if (sc == NULL)
7010 return 0;
7011
7012 return sc->recv_max_early_data;
4e8548e8
MC
7013}
7014
38b051a1 7015__owur unsigned int ssl_get_max_send_fragment(const SSL_CONNECTION *sc)
cf72c757
F
7016{
7017 /* Return any active Max Fragment Len extension */
38b051a1
TM
7018 if (sc->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(sc->session))
7019 return GET_MAX_FRAGMENT_LENGTH(sc->session);
cf72c757
F
7020
7021 /* return current SSL connection setting */
38b051a1 7022 return sc->max_send_fragment;
cf72c757
F
7023}
7024
38b051a1 7025__owur unsigned int ssl_get_split_send_fragment(const SSL_CONNECTION *sc)
cf72c757
F
7026{
7027 /* Return a value regarding an active Max Fragment Len extension */
38b051a1
TM
7028 if (sc->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(sc->session)
7029 && sc->split_send_fragment > GET_MAX_FRAGMENT_LENGTH(sc->session))
7030 return GET_MAX_FRAGMENT_LENGTH(sc->session);
cf72c757
F
7031
7032 /* else limit |split_send_fragment| to current |max_send_fragment| */
38b051a1
TM
7033 if (sc->split_send_fragment > sc->max_send_fragment)
7034 return sc->max_send_fragment;
cf72c757
F
7035
7036 /* return current SSL connection setting */
38b051a1 7037 return sc->split_send_fragment;
cf72c757 7038}
042c5753
MC
7039
7040int SSL_stateless(SSL *s)
7041{
7042 int ret;
9562842b 7043 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
38b051a1 7044
9562842b 7045 if (sc == NULL)
38b051a1 7046 return 0;
042c5753
MC
7047
7048 /* Ensure there is no state left over from a previous invocation */
7049 if (!SSL_clear(s))
d6bb50a5 7050 return 0;
042c5753
MC
7051
7052 ERR_clear_error();
7053
38b051a1 7054 sc->s3.flags |= TLS1_FLAGS_STATELESS;
042c5753 7055 ret = SSL_accept(s);
38b051a1 7056 sc->s3.flags &= ~TLS1_FLAGS_STATELESS;
042c5753 7057
38b051a1 7058 if (ret > 0 && sc->ext.cookieok)
c36001c3
MC
7059 return 1;
7060
38b051a1 7061 if (sc->hello_retry_request == SSL_HRR_PENDING && !ossl_statem_in_error(sc))
e440f513
MC
7062 return 0;
7063
7064 return -1;
042c5753 7065}
9d75dce3 7066
e97be718
MC
7067void SSL_CTX_set_post_handshake_auth(SSL_CTX *ctx, int val)
7068{
7069 ctx->pha_enabled = val;
7070}
7071
32097b33 7072void SSL_set_post_handshake_auth(SSL *ssl, int val)
9d75dce3 7073{
9562842b 7074 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
38b051a1
TM
7075
7076 if (sc == NULL)
7077 return;
7078
7079 sc->pha_enabled = val;
9d75dce3
TS
7080}
7081
7082int SSL_verify_client_post_handshake(SSL *ssl)
7083{
38b051a1 7084 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
9ea0e729 7085
d6e7ebba
HL
7086#ifndef OPENSSL_NO_QUIC
7087 if (IS_QUIC(ssl)) {
9ea0e729
HL
7088 ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION);
7089 return 0;
7090 }
7091#endif
38b051a1
TM
7092
7093 if (sc == NULL)
7094 return 0;
7095
7096 if (!SSL_CONNECTION_IS_TLS13(sc)) {
6849b73c 7097 ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_SSL_VERSION);
9d75dce3
TS
7098 return 0;
7099 }
38b051a1 7100 if (!sc->server) {
6849b73c 7101 ERR_raise(ERR_LIB_SSL, SSL_R_NOT_SERVER);
9d75dce3
TS
7102 return 0;
7103 }
7104
7105 if (!SSL_is_init_finished(ssl)) {
6849b73c 7106 ERR_raise(ERR_LIB_SSL, SSL_R_STILL_IN_INIT);
9d75dce3
TS
7107 return 0;
7108 }
7109
38b051a1 7110 switch (sc->post_handshake_auth) {
9d75dce3 7111 case SSL_PHA_NONE:
6849b73c 7112 ERR_raise(ERR_LIB_SSL, SSL_R_EXTENSION_NOT_RECEIVED);
9d75dce3
TS
7113 return 0;
7114 default:
7115 case SSL_PHA_EXT_SENT:
6849b73c 7116 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
9d75dce3
TS
7117 return 0;
7118 case SSL_PHA_EXT_RECEIVED:
7119 break;
7120 case SSL_PHA_REQUEST_PENDING:
6849b73c 7121 ERR_raise(ERR_LIB_SSL, SSL_R_REQUEST_PENDING);
9d75dce3
TS
7122 return 0;
7123 case SSL_PHA_REQUESTED:
6849b73c 7124 ERR_raise(ERR_LIB_SSL, SSL_R_REQUEST_SENT);
9d75dce3
TS
7125 return 0;
7126 }
7127
38b051a1 7128 sc->post_handshake_auth = SSL_PHA_REQUEST_PENDING;
9d75dce3
TS
7129
7130 /* checks verify_mode and algorithm_auth */
38b051a1
TM
7131 if (!send_certificate_request(sc)) {
7132 sc->post_handshake_auth = SSL_PHA_EXT_RECEIVED; /* restore on error */
6849b73c 7133 ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_CONFIG);
9d75dce3
TS
7134 return 0;
7135 }
7136
38b051a1 7137 ossl_statem_set_in_init(sc, 1);
9d75dce3
TS
7138 return 1;
7139}
df0fed9a
TS
7140
7141int SSL_CTX_set_session_ticket_cb(SSL_CTX *ctx,
7142 SSL_CTX_generate_session_ticket_fn gen_cb,
7143 SSL_CTX_decrypt_session_ticket_fn dec_cb,
7144 void *arg)
7145{
7146 ctx->generate_ticket_cb = gen_cb;
7147 ctx->decrypt_ticket_cb = dec_cb;
7148 ctx->ticket_cb_data = arg;
7149 return 1;
7150}
c9598459
MC
7151
7152void SSL_CTX_set_allow_early_data_cb(SSL_CTX *ctx,
7153 SSL_allow_early_data_cb_fn cb,
7154 void *arg)
7155{
7156 ctx->allow_early_data_cb = cb;
7157 ctx->allow_early_data_cb_data = arg;
7158}
7159
7160void SSL_set_allow_early_data_cb(SSL *s,
7161 SSL_allow_early_data_cb_fn cb,
7162 void *arg)
7163{
9562842b 7164 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
38b051a1 7165
9562842b 7166 if (sc == NULL)
38b051a1
TM
7167 return;
7168
7169 sc->allow_early_data_cb = cb;
7170 sc->allow_early_data_cb_data = arg;
c9598459 7171}
c8f6c28a 7172
b4250010 7173const EVP_CIPHER *ssl_evp_cipher_fetch(OSSL_LIB_CTX *libctx,
c8f6c28a
MC
7174 int nid,
7175 const char *properties)
7176{
301fcb28 7177 const EVP_CIPHER *ciph;
5fcb97c6 7178
301fcb28
MC
7179 ciph = tls_get_cipher_from_engine(nid);
7180 if (ciph != NULL)
7181 return ciph;
0618b62c 7182
c8f6c28a 7183 /*
301fcb28
MC
7184 * If there is no engine cipher then we do an explicit fetch. This may fail
7185 * and that could be ok
c8f6c28a 7186 */
5fcb97c6
MC
7187 ERR_set_mark();
7188 ciph = EVP_CIPHER_fetch(libctx, OBJ_nid2sn(nid), properties);
7189 ERR_pop_to_mark();
7190 return ciph;
c8f6c28a
MC
7191}
7192
7193
7194int ssl_evp_cipher_up_ref(const EVP_CIPHER *cipher)
7195{
7196 /* Don't up-ref an implicit EVP_CIPHER */
ed576acd 7197 if (EVP_CIPHER_get0_provider(cipher) == NULL)
c8f6c28a
MC
7198 return 1;
7199
7200 /*
7201 * The cipher was explicitly fetched and therefore it is safe to cast
7202 * away the const
7203 */
7204 return EVP_CIPHER_up_ref((EVP_CIPHER *)cipher);
7205}
7206
7207void ssl_evp_cipher_free(const EVP_CIPHER *cipher)
7208{
7209 if (cipher == NULL)
7210 return;
7211
ed576acd 7212 if (EVP_CIPHER_get0_provider(cipher) != NULL) {
c8f6c28a
MC
7213 /*
7214 * The cipher was explicitly fetched and therefore it is safe to cast
7215 * away the const
7216 */
7217 EVP_CIPHER_free((EVP_CIPHER *)cipher);
7218 }
7219}
7220
b4250010 7221const EVP_MD *ssl_evp_md_fetch(OSSL_LIB_CTX *libctx,
c8f6c28a
MC
7222 int nid,
7223 const char *properties)
7224{
301fcb28 7225 const EVP_MD *md;
5fcb97c6 7226
301fcb28
MC
7227 md = tls_get_digest_from_engine(nid);
7228 if (md != NULL)
7229 return md;
c8f6c28a
MC
7230
7231 /* Otherwise we do an explicit fetch */
5fcb97c6
MC
7232 ERR_set_mark();
7233 md = EVP_MD_fetch(libctx, OBJ_nid2sn(nid), properties);
7234 ERR_pop_to_mark();
7235 return md;
c8f6c28a
MC
7236}
7237
7238int ssl_evp_md_up_ref(const EVP_MD *md)
7239{
7240 /* Don't up-ref an implicit EVP_MD */
ed576acd 7241 if (EVP_MD_get0_provider(md) == NULL)
c8f6c28a
MC
7242 return 1;
7243
7244 /*
7245 * The digest was explicitly fetched and therefore it is safe to cast
7246 * away the const
7247 */
7248 return EVP_MD_up_ref((EVP_MD *)md);
7249}
7250
7251void ssl_evp_md_free(const EVP_MD *md)
7252{
7253 if (md == NULL)
7254 return;
7255
ed576acd 7256 if (EVP_MD_get0_provider(md) != NULL) {
c8f6c28a
MC
7257 /*
7258 * The digest was explicitly fetched and therefore it is safe to cast
7259 * away the const
7260 */
7261 EVP_MD_free((EVP_MD *)md);
7262 }
7263}
163f6dc1
MC
7264
7265int SSL_set0_tmp_dh_pkey(SSL *s, EVP_PKEY *dhpkey)
7266{
38b051a1
TM
7267 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
7268
7269 if (sc == NULL)
7270 return 0;
7271
7272 if (!ssl_security(sc, SSL_SECOP_TMP_DH,
ed576acd 7273 EVP_PKEY_get_security_bits(dhpkey), 0, dhpkey)) {
276d6c68 7274 ERR_raise(ERR_LIB_SSL, SSL_R_DH_KEY_TOO_SMALL);
163f6dc1
MC
7275 return 0;
7276 }
38b051a1
TM
7277 EVP_PKEY_free(sc->cert->dh_tmp);
7278 sc->cert->dh_tmp = dhpkey;
163f6dc1
MC
7279 return 1;
7280}
7281
7282int SSL_CTX_set0_tmp_dh_pkey(SSL_CTX *ctx, EVP_PKEY *dhpkey)
7283{
7284 if (!ssl_ctx_security(ctx, SSL_SECOP_TMP_DH,
ed576acd 7285 EVP_PKEY_get_security_bits(dhpkey), 0, dhpkey)) {
276d6c68 7286 ERR_raise(ERR_LIB_SSL, SSL_R_DH_KEY_TOO_SMALL);
163f6dc1
MC
7287 return 0;
7288 }
7289 EVP_PKEY_free(ctx->cert->dh_tmp);
7290 ctx->cert->dh_tmp = dhpkey;
7291 return 1;
7292}
68801bcb 7293
03bacce8 7294/* QUIC-specific methods which are supported on QUIC connections only. */
6084e04b 7295int SSL_handle_events(SSL *s)
03bacce8
HL
7296{
7297 SSL_CONNECTION *sc;
03bacce8 7298
6d495cc4
HL
7299#ifndef OPENSSL_NO_QUIC
7300 if (IS_QUIC(s))
6084e04b 7301 return ossl_quic_handle_events(s);
03bacce8
HL
7302#endif
7303
7304 sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
7305 if (sc != NULL && SSL_CONNECTION_IS_DTLS(sc))
fbe2573d
HL
7306 /*
7307 * DTLSv1_handle_timeout returns 0 if the timer wasn't expired yet,
7308 * which we consider a success case. Theoretically DTLSv1_handle_timeout
7309 * can also return 0 if s is NULL or not a DTLS object, but we've
7310 * already ruled out those possibilities above, so this is not possible
7311 * here. Thus the only failure cases are where DTLSv1_handle_timeout
7312 * returns -1.
7313 */
7314 return DTLSv1_handle_timeout(s) >= 0;
03bacce8 7315
fbe2573d 7316 return 1;
03bacce8
HL
7317}
7318
7ea49713 7319int SSL_get_event_timeout(SSL *s, struct timeval *tv, int *is_infinite)
03bacce8
HL
7320{
7321 SSL_CONNECTION *sc;
03bacce8 7322
6d495cc4
HL
7323#ifndef OPENSSL_NO_QUIC
7324 if (IS_QUIC(s))
7ea49713 7325 return ossl_quic_get_event_timeout(s, tv, is_infinite);
03bacce8
HL
7326#endif
7327
7328 sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
fbe2573d 7329 if (sc != NULL && SSL_CONNECTION_IS_DTLS(sc)
7ea49713
HL
7330 && DTLSv1_get_timeout(s, tv)) {
7331 *is_infinite = 0;
03bacce8 7332 return 1;
7ea49713 7333 }
03bacce8 7334
7ea49713 7335 tv->tv_sec = 1000000;
fbe2573d 7336 tv->tv_usec = 0;
7ea49713 7337 *is_infinite = 1;
fbe2573d 7338 return 1;
03bacce8
HL
7339}
7340
68801bcb
HL
7341int SSL_get_rpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
7342{
f2624433 7343 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
68801bcb 7344
f2624433
HL
7345#ifndef OPENSSL_NO_QUIC
7346 if (IS_QUIC(s))
7347 return ossl_quic_get_rpoll_descriptor(s, desc);
68801bcb 7348#endif
f2624433
HL
7349
7350 if (sc == NULL || sc->rbio == NULL)
7351 return 0;
7352
7353 return BIO_get_rpoll_descriptor(sc->rbio, desc);
68801bcb
HL
7354}
7355
7356int SSL_get_wpoll_descriptor(SSL *s, BIO_POLL_DESCRIPTOR *desc)
7357{
f2624433 7358 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
68801bcb 7359
f2624433
HL
7360#ifndef OPENSSL_NO_QUIC
7361 if (IS_QUIC(s))
7362 return ossl_quic_get_wpoll_descriptor(s, desc);
03bacce8 7363#endif
f2624433
HL
7364
7365 if (sc == NULL || sc->wbio == NULL)
7366 return 0;
7367
7368 return BIO_get_wpoll_descriptor(sc->wbio, desc);
03bacce8
HL
7369}
7370
b639475a 7371int SSL_net_read_desired(SSL *s)
03bacce8
HL
7372{
7373#ifndef OPENSSL_NO_QUIC
6d495cc4 7374 if (!IS_QUIC(s))
3432157b 7375 return SSL_want_read(s);
03bacce8 7376
6d495cc4 7377 return ossl_quic_get_net_read_desired(s);
03bacce8 7378#else
3432157b 7379 return SSL_want_read(s);
03bacce8
HL
7380#endif
7381}
7382
b639475a 7383int SSL_net_write_desired(SSL *s)
03bacce8
HL
7384{
7385#ifndef OPENSSL_NO_QUIC
6d495cc4 7386 if (!IS_QUIC(s))
3432157b 7387 return SSL_want_write(s);
03bacce8 7388
6d495cc4 7389 return ossl_quic_get_net_write_desired(s);
03bacce8 7390#else
3432157b 7391 return SSL_want_write(s);
03bacce8
HL
7392#endif
7393}
7394
7395int SSL_set_blocking_mode(SSL *s, int blocking)
7396{
7397#ifndef OPENSSL_NO_QUIC
6d495cc4 7398 if (!IS_QUIC(s))
03bacce8
HL
7399 return 0;
7400
6d495cc4 7401 return ossl_quic_conn_set_blocking_mode(s, blocking);
03bacce8
HL
7402#else
7403 return 0;
7404#endif
7405}
7406
7407int SSL_get_blocking_mode(SSL *s)
7408{
7409#ifndef OPENSSL_NO_QUIC
6d495cc4 7410 if (!IS_QUIC(s))
03bacce8
HL
7411 return -1;
7412
6d495cc4 7413 return ossl_quic_conn_get_blocking_mode(s);
03bacce8
HL
7414#else
7415 return -1;
7416#endif
7417}
7418
ce7a9e23 7419int SSL_set1_initial_peer_addr(SSL *s, const BIO_ADDR *peer_addr)
03bacce8
HL
7420{
7421#ifndef OPENSSL_NO_QUIC
6d495cc4 7422 if (!IS_QUIC(s))
e8043229 7423 return 0;
03bacce8 7424
6d495cc4 7425 return ossl_quic_conn_set_initial_peer_addr(s, peer_addr);
68801bcb 7426#else
e8043229
HL
7427 return 0;
7428#endif
7429}
7430
7431int SSL_shutdown_ex(SSL *ssl, uint64_t flags,
7432 const SSL_SHUTDOWN_EX_ARGS *args,
7433 size_t args_len)
7434{
7435#ifndef OPENSSL_NO_QUIC
6d495cc4 7436 if (!IS_QUIC(ssl))
e8043229
HL
7437 return SSL_shutdown(ssl);
7438
6d495cc4 7439 return ossl_quic_conn_shutdown(ssl, flags, args, args_len);
e8043229
HL
7440#else
7441 return SSL_shutdown(ssl);
68801bcb
HL
7442#endif
7443}
a9979965
HL
7444
7445int SSL_stream_conclude(SSL *ssl, uint64_t flags)
7446{
7447#ifndef OPENSSL_NO_QUIC
6d495cc4 7448 if (!IS_QUIC(ssl))
a9979965
HL
7449 return 0;
7450
6d495cc4 7451 return ossl_quic_conn_stream_conclude(ssl);
a9979965
HL
7452#else
7453 return 0;
7454#endif
7455}
3c95ef22 7456
cb5c208b
HL
7457SSL *SSL_new_stream(SSL *s, uint64_t flags)
7458{
7459#ifndef OPENSSL_NO_QUIC
7460 if (!IS_QUIC(s))
7461 return NULL;
7462
7463 return ossl_quic_conn_stream_new(s, flags);
7464#else
7465 return NULL;
7466#endif
7467}
7468
020d0389
HL
7469SSL *SSL_get0_connection(SSL *s)
7470{
7471#ifndef OPENSSL_NO_QUIC
7472 if (!IS_QUIC(s))
7473 return s;
7474
7475 return ossl_quic_get0_connection(s);
7476#else
7477 return s;
7478#endif
7479}
7480
e1dee2e3
HL
7481int SSL_is_connection(SSL *s)
7482{
7483 return SSL_get0_connection(s) == s;
7484}
7485
1bca3f1b
HL
7486int SSL_get_stream_type(SSL *s)
7487{
7488#ifndef OPENSSL_NO_QUIC
7489 if (!IS_QUIC(s))
7490 return SSL_STREAM_TYPE_BIDI;
7491
7492 return ossl_quic_get_stream_type(s);
7493#else
7494 return SSL_STREAM_TYPE_BIDI;
7495#endif
7496}
7497
19cb0887
HL
7498uint64_t SSL_get_stream_id(SSL *s)
7499{
7500#ifndef OPENSSL_NO_QUIC
7501 if (!IS_QUIC(s))
7502 return UINT64_MAX;
7503
7504 return ossl_quic_get_stream_id(s);
7505#else
7506 return UINT64_MAX;
7507#endif
7508}
7509
d2e9e12b
HL
7510int SSL_is_stream_local(SSL *s)
7511{
7512#ifndef OPENSSL_NO_QUIC
7513 if (!IS_QUIC(s))
7514 return -1;
7515
7516 return ossl_quic_is_stream_local(s);
7517#else
7518 return -1;
7519#endif
7520}
7521
8b7be3aa
HL
7522int SSL_set_default_stream_mode(SSL *s, uint32_t mode)
7523{
7524#ifndef OPENSSL_NO_QUIC
7525 if (!IS_QUIC(s))
7526 return 0;
7527
7528 return ossl_quic_set_default_stream_mode(s, mode);
7529#else
7530 return 0;
7531#endif
7532}
7533
83df44ae 7534int SSL_set_incoming_stream_policy(SSL *s, int policy, uint64_t aec)
8a90df34
HL
7535{
7536#ifndef OPENSSL_NO_QUIC
7537 if (!IS_QUIC(s))
7538 return 0;
7539
83df44ae 7540 return ossl_quic_set_incoming_stream_policy(s, policy, aec);
8a90df34
HL
7541#else
7542 return 0;
7543#endif
7544}
7545
cb68ce9f
HL
7546SSL *SSL_accept_stream(SSL *s, uint64_t flags)
7547{
7548#ifndef OPENSSL_NO_QUIC
7549 if (!IS_QUIC(s))
7550 return NULL;
7551
7552 return ossl_quic_accept_stream(s, flags);
7553#else
7554 return NULL;
7555#endif
7556}
7557
7558size_t SSL_get_accept_stream_queue_len(SSL *s)
7559{
7560#ifndef OPENSSL_NO_QUIC
7561 if (!IS_QUIC(s))
7562 return 0;
7563
7564 return ossl_quic_get_accept_stream_queue_len(s);
7565#else
7566 return 0;
7567#endif
7568}
7569
c3a04ea2
HL
7570int SSL_stream_reset(SSL *s,
7571 const SSL_STREAM_RESET_ARGS *args,
7572 size_t args_len)
7573{
7574#ifndef OPENSSL_NO_QUIC
7575 if (!IS_QUIC(s))
7576 return 0;
7577
7578 return ossl_quic_stream_reset(s, args, args_len);
7579#else
7580 return 0;
7581#endif
7582}
7583
7584int SSL_get_stream_read_state(SSL *s)
7585{
7586#ifndef OPENSSL_NO_QUIC
7587 if (!IS_QUIC(s))
7588 return SSL_STREAM_STATE_NONE;
7589
7590 return ossl_quic_get_stream_read_state(s);
7591#else
7592 return SSL_STREAM_STATE_NONE;
7593#endif
7594}
7595
7596int SSL_get_stream_write_state(SSL *s)
7597{
7598#ifndef OPENSSL_NO_QUIC
7599 if (!IS_QUIC(s))
7600 return SSL_STREAM_STATE_NONE;
7601
7602 return ossl_quic_get_stream_write_state(s);
7603#else
7604 return SSL_STREAM_STATE_NONE;
7605#endif
7606}
7607
7608int SSL_get_stream_read_error_code(SSL *s, uint64_t *app_error_code)
7609{
7610#ifndef OPENSSL_NO_QUIC
7611 if (!IS_QUIC(s))
7612 return -1;
7613
7614 return ossl_quic_get_stream_read_error_code(s, app_error_code);
7615#else
7616 return -1;
7617#endif
7618}
7619
7620int SSL_get_stream_write_error_code(SSL *s, uint64_t *app_error_code)
7621{
7622#ifndef OPENSSL_NO_QUIC
7623 if (!IS_QUIC(s))
7624 return -1;
7625
7626 return ossl_quic_get_stream_write_error_code(s, app_error_code);
7627#else
7628 return -1;
7629#endif
7630}
7631
7632int SSL_get_conn_close_info(SSL *s, SSL_CONN_CLOSE_INFO *info,
7633 size_t info_len)
7634{
7635#ifndef OPENSSL_NO_QUIC
7636 if (!IS_QUIC(s))
7637 return -1;
7638
7639 return ossl_quic_get_conn_close_info(s, info, info_len);
7640#else
7641 return -1;
7642#endif
7643}
7644
e203d1b5
HL
7645int SSL_get_value_uint(SSL *s, uint32_t class_, uint32_t id,
7646 uint64_t *value)
7647{
e203d1b5
HL
7648#ifndef OPENSSL_NO_QUIC
7649 if (IS_QUIC(s))
7650 return ossl_quic_get_value_uint(s, class_, id, value);
7651#endif
7652
7653 ERR_raise(ERR_LIB_SSL, SSL_R_UNSUPPORTED_PROTOCOL);
7654 return 0;
7655}
7656
7657int SSL_set_value_uint(SSL *s, uint32_t class_, uint32_t id,
7658 uint64_t value)
7659{
7660#ifndef OPENSSL_NO_QUIC
7661 if (IS_QUIC(s))
7662 return ossl_quic_set_value_uint(s, class_, id, value);
7663#endif
7664
7665 ERR_raise(ERR_LIB_SSL, SSL_R_UNSUPPORTED_PROTOCOL);
7666 return 0;
7667}
7668
3c95ef22
TS
7669int SSL_add_expected_rpk(SSL *s, EVP_PKEY *rpk)
7670{
7671 unsigned char *data = NULL;
7672 SSL_DANE *dane = SSL_get0_dane(s);
7673 int ret;
7674
7675 if (dane == NULL || dane->dctx == NULL)
7676 return 0;
7677 if ((ret = i2d_PUBKEY(rpk, &data)) <= 0)
7678 return 0;
7679
7680 ret = SSL_dane_tlsa_add(s, DANETLS_USAGE_DANE_EE,
7681 DANETLS_SELECTOR_SPKI,
7682 DANETLS_MATCHING_FULL,
7683 data, (size_t)ret) > 0;
7684 OPENSSL_free(data);
7685 return ret;
7686}
7687
7688EVP_PKEY *SSL_get0_peer_rpk(const SSL *s)
7689{
7690 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
7691
7692 if (sc == NULL || sc->session == NULL)
7693 return NULL;
7694 return sc->session->peer_rpk;
7695}
7696
7697int SSL_get_negotiated_client_cert_type(const SSL *s)
7698{
7699 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
7700
7701 if (sc == NULL)
7702 return 0;
7703
7704 return sc->ext.client_cert_type;
7705}
7706
7707int SSL_get_negotiated_server_cert_type(const SSL *s)
7708{
7709 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
7710
7711 if (sc == NULL)
7712 return 0;
7713
7714 return sc->ext.server_cert_type;
7715}
7716
7717static int validate_cert_type(const unsigned char *val, size_t len)
7718{
7719 size_t i;
7720 int saw_rpk = 0;
7721 int saw_x509 = 0;
7722
7723 if (val == NULL && len == 0)
7724 return 1;
7725
7726 if (val == NULL || len == 0)
7727 return 0;
7728
7729 for (i = 0; i < len; i++) {
7730 switch (val[i]) {
7731 case TLSEXT_cert_type_rpk:
7732 if (saw_rpk)
7733 return 0;
7734 saw_rpk = 1;
7735 break;
7736 case TLSEXT_cert_type_x509:
7737 if (saw_x509)
7738 return 0;
7739 saw_x509 = 1;
7740 break;
7741 case TLSEXT_cert_type_pgp:
7742 case TLSEXT_cert_type_1609dot2:
7743 default:
7744 return 0;
7745 }
7746 }
7747 return 1;
7748}
7749
7750static int set_cert_type(unsigned char **cert_type,
7751 size_t *cert_type_len,
7752 const unsigned char *val,
7753 size_t len)
7754{
7755 unsigned char *tmp = NULL;
7756
7757 if (!validate_cert_type(val, len))
7758 return 0;
7759
7760 if (val != NULL && (tmp = OPENSSL_memdup(val, len)) == NULL)
7761 return 0;
7762
7763 OPENSSL_free(*cert_type);
7764 *cert_type = tmp;
7765 *cert_type_len = len;
7766 return 1;
7767}
7768
7769int SSL_set1_client_cert_type(SSL *s, const unsigned char *val, size_t len)
7770{
7771 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
7772
7773 return set_cert_type(&sc->client_cert_type, &sc->client_cert_type_len,
7774 val, len);
7775}
7776
7777int SSL_set1_server_cert_type(SSL *s, const unsigned char *val, size_t len)
7778{
7779 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
7780
7781 return set_cert_type(&sc->server_cert_type, &sc->server_cert_type_len,
7782 val, len);
7783}
7784
7785int SSL_CTX_set1_client_cert_type(SSL_CTX *ctx, const unsigned char *val, size_t len)
7786{
7787 return set_cert_type(&ctx->client_cert_type, &ctx->client_cert_type_len,
7788 val, len);
7789}
7790
7791int SSL_CTX_set1_server_cert_type(SSL_CTX *ctx, const unsigned char *val, size_t len)
7792{
7793 return set_cert_type(&ctx->server_cert_type, &ctx->server_cert_type_len,
7794 val, len);
7795}
7796
7797int SSL_get0_client_cert_type(const SSL *s, unsigned char **t, size_t *len)
7798{
7799 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
7800
7801 if (t == NULL || len == NULL)
7802 return 0;
7803
7804 *t = sc->client_cert_type;
7805 *len = sc->client_cert_type_len;
7806 return 1;
7807}
7808
7809int SSL_get0_server_cert_type(const SSL *s, unsigned char **t, size_t *len)
7810{
7811 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
7812
7813 if (t == NULL || len == NULL)
7814 return 0;
7815
7816 *t = sc->server_cert_type;
7817 *len = sc->server_cert_type_len;
7818 return 1;
7819}
7820
7821int SSL_CTX_get0_client_cert_type(const SSL_CTX *ctx, unsigned char **t, size_t *len)
7822{
7823 if (t == NULL || len == NULL)
7824 return 0;
7825
7826 *t = ctx->client_cert_type;
7827 *len = ctx->client_cert_type_len;
7828 return 1;
7829}
7830
7831int SSL_CTX_get0_server_cert_type(const SSL_CTX *ctx, unsigned char **t, size_t *len)
7832{
7833 if (t == NULL || len == NULL)
7834 return 0;
7835
7836 *t = ctx->server_cert_type;
7837 *len = ctx->server_cert_type_len;
7838 return 1;
7839}