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