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