VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.5/ssl/ssl_lib.c

Last change on this file was 104078, checked in by vboxsync, 2 months ago

openssl-3.1.5: Applied and adjusted our OpenSSL changes to 3.1.4. bugref:10638

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

© 2023 Oracle
ContactPrivacy policyTerms of Use