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