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