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