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