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