Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/ssl/ssl_lib.c

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