jcs's openbsd hax
openbsd
1/* $OpenBSD: t1_lib.c,v 1.207 2025/12/04 21:16:17 beck Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58/* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 *
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 *
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
71 * distribution.
72 *
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 *
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
82 *
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
86 *
87 * 6. Redistributions of any form whatsoever must retain the following
88 * acknowledgment:
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 *
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
105 *
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
109 *
110 */
111
112#include <stdio.h>
113
114#include <openssl/evp.h>
115#include <openssl/hmac.h>
116#include <openssl/objects.h>
117#include <openssl/ocsp.h>
118
119#include "bytestring.h"
120#include "ssl_local.h"
121#include "ssl_sigalgs.h"
122#include "ssl_tlsext.h"
123
124static int tls_decrypt_ticket(SSL *s, CBS *ticket, int *alert,
125 SSL_SESSION **psess);
126
127int
128tls1_new(SSL *s)
129{
130 if (!ssl3_new(s))
131 return 0;
132 s->method->ssl_clear(s);
133 return 1;
134}
135
136void
137tls1_free(SSL *s)
138{
139 if (s == NULL)
140 return;
141
142 free(s->tlsext_session_ticket);
143 ssl3_free(s);
144}
145
146void
147tls1_clear(SSL *s)
148{
149 ssl3_clear(s);
150 s->version = s->method->version;
151}
152
153struct supported_group {
154 uint16_t group_id;
155 int nid;
156 int bits;
157};
158
159/*
160 * Supported groups (formerly known as named curves)
161 * https://www.iana.org/assignments/tls-parameters/#tls-parameters-8
162 */
163static const struct supported_group nid_list[] = {
164 {
165 .group_id = 1,
166 .nid = NID_sect163k1,
167 .bits = 80,
168 },
169 {
170 .group_id = 2,
171 .nid = NID_sect163r1,
172 .bits = 80,
173 },
174 {
175 .group_id = 3,
176 .nid = NID_sect163r2,
177 .bits = 80,
178 },
179 {
180 .group_id = 4,
181 .nid = NID_sect193r1,
182 .bits = 80,
183 },
184 {
185 .group_id = 5,
186 .nid = NID_sect193r2,
187 .bits = 80,
188 },
189 {
190 .group_id = 6,
191 .nid = NID_sect233k1,
192 .bits = 112,
193 },
194 {
195 .group_id = 7,
196 .nid = NID_sect233r1,
197 .bits = 112,
198 },
199 {
200 .group_id = 8,
201 .nid = NID_sect239k1,
202 .bits = 112,
203 },
204 {
205 .group_id = 9,
206 .nid = NID_sect283k1,
207 .bits = 128,
208 },
209 {
210 .group_id = 10,
211 .nid = NID_sect283r1,
212 .bits = 128,
213 },
214 {
215 .group_id = 11,
216 .nid = NID_sect409k1,
217 .bits = 192,
218 },
219 {
220 .group_id = 12,
221 .nid = NID_sect409r1,
222 .bits = 192,
223 },
224 {
225 .group_id = 13,
226 .nid = NID_sect571k1,
227 .bits = 256,
228 },
229 {
230 .group_id = 14,
231 .nid = NID_sect571r1,
232 .bits = 256,
233 },
234 {
235 .group_id = 15,
236 .nid = NID_secp160k1,
237 .bits = 80,
238 },
239 {
240 .group_id = 16,
241 .nid = NID_secp160r1,
242 .bits = 80,
243 },
244 {
245 .group_id = 17,
246 .nid = NID_secp160r2,
247 .bits = 80,
248 },
249 {
250 .group_id = 18,
251 .nid = NID_secp192k1,
252 .bits = 80,
253 },
254 {
255 .group_id = 19,
256 .nid = NID_X9_62_prime192v1, /* aka secp192r1 */
257 .bits = 80,
258 },
259 {
260 .group_id = 20,
261 .nid = NID_secp224k1,
262 .bits = 112,
263 },
264 {
265 .group_id = 21,
266 .nid = NID_secp224r1,
267 .bits = 112,
268 },
269 {
270 .group_id = 22,
271 .nid = NID_secp256k1,
272 .bits = 128,
273 },
274 {
275 .group_id = 23,
276 .nid = NID_X9_62_prime256v1, /* aka secp256r1 */
277 .bits = 128,
278 },
279 {
280 .group_id = 24,
281 .nid = NID_secp384r1,
282 .bits = 192,
283 },
284 {
285 .group_id = 25,
286 .nid = NID_secp521r1,
287 .bits = 256,
288 },
289 {
290 .group_id = 26,
291 .nid = NID_brainpoolP256r1,
292 .bits = 128,
293 },
294 {
295 .group_id = 27,
296 .nid = NID_brainpoolP384r1,
297 .bits = 192,
298 },
299 {
300 .group_id = 28,
301 .nid = NID_brainpoolP512r1,
302 .bits = 256,
303 },
304 {
305 .group_id = 29,
306 .nid = NID_X25519,
307 .bits = 128,
308 },
309 {
310 .group_id = 4588,
311 .nid = NID_X25519MLKEM768,
312 .bits = 128,
313 },
314};
315
316#define NID_LIST_LEN (sizeof(nid_list) / sizeof(nid_list[0]))
317
318#if 0
319static const uint8_t ecformats_list[] = {
320 TLSEXT_ECPOINTFORMAT_uncompressed,
321 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,
322 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
323};
324#endif
325
326static const uint8_t ecformats_default[] = {
327 TLSEXT_ECPOINTFORMAT_uncompressed,
328};
329
330static const uint16_t ecgroups_tls12_client_default[] = {
331 29, /* X25519 (29) */
332 23, /* secp256r1 (23) */
333 24, /* secp384r1 (24) */
334 25, /* secp521r1 (25) */
335};
336
337static const uint16_t ecgroups_tls12_server_default[] = {
338 29, /* X25519 (29) */
339 23, /* secp256r1 (23) */
340 24, /* secp384r1 (24) */
341};
342
343static const uint16_t ecgroups_client_default[] = {
344 4588, /* X25519MLKEM768 (4588) */
345 29, /* X25519 (29) */
346 23, /* secp256r1 (23) */
347 24, /* secp384r1 (24) */
348 25, /* secp521r1 (25) */
349};
350
351static const uint16_t ecgroups_server_default[] = {
352 4588, /* X25519MLKEM768 (4588) */
353 29, /* X25519 (29) */
354 23, /* secp256r1 (23) */
355 24, /* secp384r1 (24) */
356};
357
358static const struct supported_group *
359tls1_supported_group_by_id(uint16_t group_id)
360{
361 int i;
362
363 for (i = 0; i < NID_LIST_LEN; i++) {
364 if (group_id == nid_list[i].group_id)
365 return &nid_list[i];
366 }
367
368 return NULL;
369}
370
371static const struct supported_group *
372tls1_supported_group_by_nid(int nid)
373{
374 int i;
375
376 for (i = 0; i < NID_LIST_LEN; i++) {
377 if (nid == nid_list[i].nid)
378 return &nid_list[i];
379 }
380
381 return NULL;
382}
383
384int
385tls1_ec_group_id2nid(uint16_t group_id, int *out_nid)
386{
387 const struct supported_group *sg;
388
389 if ((sg = tls1_supported_group_by_id(group_id)) == NULL)
390 return 0;
391
392 *out_nid = sg->nid;
393
394 return 1;
395}
396
397int
398tls1_ec_group_id2bits(uint16_t group_id, int *out_bits)
399{
400 const struct supported_group *sg;
401
402 if ((sg = tls1_supported_group_by_id(group_id)) == NULL)
403 return 0;
404
405 *out_bits = sg->bits;
406
407 return 1;
408}
409
410int
411tls1_ec_nid2group_id(int nid, uint16_t *out_group_id)
412{
413 const struct supported_group *sg;
414
415 if ((sg = tls1_supported_group_by_nid(nid)) == NULL)
416 return 0;
417
418 *out_group_id = sg->group_id;
419
420 return 1;
421}
422
423/*
424 * Return the appropriate format list. If client_formats is non-zero, return
425 * the client/session formats. Otherwise return the custom format list if one
426 * exists, or the default formats if a custom list has not been specified.
427 */
428void
429tls1_get_formatlist(const SSL *s, int client_formats, const uint8_t **pformats,
430 size_t *pformatslen)
431{
432 if (client_formats != 0) {
433 *pformats = s->session->tlsext_ecpointformatlist;
434 *pformatslen = s->session->tlsext_ecpointformatlist_length;
435 return;
436 }
437
438 *pformats = s->tlsext_ecpointformatlist;
439 *pformatslen = s->tlsext_ecpointformatlist_length;
440 if (*pformats == NULL) {
441 *pformats = ecformats_default;
442 *pformatslen = sizeof(ecformats_default);
443 }
444}
445
446/*
447 * Return the appropriate group list. If client_groups is non-zero, return
448 * the client/session groups. Otherwise return the custom group list if one
449 * exists, or the default groups if a custom list has not been specified.
450 */
451void
452tls1_get_group_list(const SSL *s, int client_groups, const uint16_t **pgroups,
453 size_t *pgroupslen)
454{
455 if (client_groups != 0) {
456 *pgroups = s->session->tlsext_supportedgroups;
457 *pgroupslen = s->session->tlsext_supportedgroups_length;
458 return;
459 }
460
461 *pgroups = s->tlsext_supportedgroups;
462 *pgroupslen = s->tlsext_supportedgroups_length;
463 if (*pgroups != NULL)
464 return;
465
466 if (!s->server) {
467 if (s->s3->hs.our_max_tls_version >= TLS1_3_VERSION) {
468 *pgroups = ecgroups_client_default;
469 *pgroupslen = sizeof(ecgroups_client_default) / 2;
470 } else {
471 *pgroups = ecgroups_tls12_client_default;
472 *pgroupslen = sizeof(ecgroups_tls12_client_default) / 2;
473 }
474 } else {
475 if (s->s3->hs.our_max_tls_version >= TLS1_3_VERSION) {
476 *pgroups = ecgroups_server_default;
477 *pgroupslen = sizeof(ecgroups_server_default) / 2;
478 } else {
479 *pgroups = ecgroups_tls12_server_default;
480 *pgroupslen = sizeof(ecgroups_tls12_server_default) / 2;
481 }
482 }
483}
484
485static int
486tls1_get_group_lists(const SSL *ssl, const uint16_t **pref, size_t *preflen,
487 const uint16_t **supp, size_t *supplen)
488{
489 unsigned long server_pref;
490
491 /* Cannot do anything on the client side. */
492 if (!ssl->server)
493 return 0;
494
495 server_pref = (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE);
496 tls1_get_group_list(ssl, (server_pref == 0), pref, preflen);
497 tls1_get_group_list(ssl, (server_pref != 0), supp, supplen);
498
499 return 1;
500}
501
502static int
503tls1_group_id_present(uint16_t group_id, const uint16_t *list, size_t list_len)
504{
505 size_t i;
506
507 for (i = 0; i < list_len; i++) {
508 if (group_id == list[i])
509 return 1;
510 }
511
512 return 0;
513}
514
515int
516tls1_count_shared_groups(const SSL *ssl, size_t *out_count)
517{
518 size_t count, preflen, supplen, i;
519 const uint16_t *pref, *supp;
520
521 if (!tls1_get_group_lists(ssl, &pref, &preflen, &supp, &supplen))
522 return 0;
523
524 count = 0;
525 for (i = 0; i < preflen; i++) {
526 if (!tls1_group_id_present(pref[i], supp, supplen))
527 continue;
528
529 if (!ssl_security_shared_group(ssl, pref[i]))
530 continue;
531
532 count++;
533 }
534
535 *out_count = count;
536
537 return 1;
538}
539
540static int
541tls1_group_by_index(const SSL *ssl, size_t n, int *out_nid,
542 int (*ssl_security_fn)(const SSL *, uint16_t))
543{
544 size_t count, preflen, supplen, i;
545 const uint16_t *pref, *supp;
546
547 if (!tls1_get_group_lists(ssl, &pref, &preflen, &supp, &supplen))
548 return 0;
549
550 count = 0;
551 for (i = 0; i < preflen; i++) {
552 if (!tls1_group_id_present(pref[i], supp, supplen))
553 continue;
554
555 if (!ssl_security_fn(ssl, pref[i]))
556 continue;
557
558 if (count++ == n)
559 return tls1_ec_group_id2nid(pref[i], out_nid);
560 }
561
562 return 0;
563}
564
565int
566tls1_get_shared_group_by_index(const SSL *ssl, size_t index, int *out_nid)
567{
568 return tls1_group_by_index(ssl, index, out_nid,
569 ssl_security_shared_group);
570}
571
572int
573tls1_get_supported_group(const SSL *ssl, int *out_nid)
574{
575 return tls1_group_by_index(ssl, 0, out_nid,
576 ssl_security_supported_group);
577}
578
579int
580tls1_set_groups(uint16_t **out_group_ids, size_t *out_group_ids_len,
581 const int *groups, size_t ngroups)
582{
583 uint16_t *group_ids;
584 size_t i;
585
586 if ((group_ids = calloc(ngroups, sizeof(uint16_t))) == NULL)
587 return 0;
588
589 for (i = 0; i < ngroups; i++) {
590 if (!tls1_ec_nid2group_id(groups[i], &group_ids[i])) {
591 free(group_ids);
592 return 0;
593 }
594 }
595
596 free(*out_group_ids);
597 *out_group_ids = group_ids;
598 *out_group_ids_len = ngroups;
599
600 return 1;
601}
602
603int
604tls1_set_group_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
605 const char *groups)
606{
607 uint16_t *new_group_ids, *group_ids = NULL;
608 size_t ngroups = 0;
609 char *gs, *p, *q;
610 int nid;
611
612 if ((gs = strdup(groups)) == NULL)
613 return 0;
614
615 q = gs;
616 while ((p = strsep(&q, ":")) != NULL) {
617 nid = OBJ_sn2nid(p);
618 if (nid == NID_undef)
619 nid = OBJ_ln2nid(p);
620 if (nid == NID_undef)
621 nid = EC_curve_nist2nid(p);
622 if (nid == NID_undef)
623 goto err;
624
625 if ((new_group_ids = reallocarray(group_ids, ngroups + 1,
626 sizeof(uint16_t))) == NULL)
627 goto err;
628 group_ids = new_group_ids;
629
630 if (!tls1_ec_nid2group_id(nid, &group_ids[ngroups]))
631 goto err;
632
633 ngroups++;
634 }
635
636 free(gs);
637 free(*out_group_ids);
638 *out_group_ids = group_ids;
639 *out_group_ids_len = ngroups;
640
641 return 1;
642
643 err:
644 free(gs);
645 free(group_ids);
646
647 return 0;
648}
649
650/* Check that a group is one of our preferences. */
651int
652tls1_check_group(SSL *s, uint16_t group_id)
653{
654 const uint16_t *groups;
655 size_t groupslen, i;
656
657 tls1_get_group_list(s, 0, &groups, &groupslen);
658
659 for (i = 0; i < groupslen; i++) {
660 if (!ssl_security_supported_group(s, groups[i]))
661 continue;
662 if (groups[i] == group_id)
663 return 1;
664 }
665 return 0;
666}
667
668/* For an EC key set TLS ID and required compression based on parameters. */
669static int
670tls1_set_ec_id(uint16_t *group_id, uint8_t *comp_id, EC_KEY *ec)
671{
672 const EC_GROUP *group;
673 int nid;
674
675 if ((group = EC_KEY_get0_group(ec)) == NULL)
676 return 0;
677
678 /* Determine group ID. */
679 nid = EC_GROUP_get_curve_name(group);
680 if (!tls1_ec_nid2group_id(nid, group_id))
681 return 0;
682
683 /* Specify the compression identifier. */
684 if (EC_KEY_get0_public_key(ec) == NULL)
685 return 0;
686 *comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
687 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) {
688 *comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
689 }
690
691 return 1;
692}
693
694/* Check that an EC key is compatible with extensions. */
695static int
696tls1_check_ec_key(SSL *s, const uint16_t group_id, const uint8_t comp_id)
697{
698 size_t groupslen, formatslen, i;
699 const uint16_t *groups;
700 const uint8_t *formats;
701
702 /*
703 * Check point formats extension if present, otherwise everything
704 * is supported (see RFC4492).
705 */
706 tls1_get_formatlist(s, 1, &formats, &formatslen);
707 if (formats != NULL) {
708 for (i = 0; i < formatslen; i++) {
709 if (formats[i] == comp_id)
710 break;
711 }
712 if (i == formatslen)
713 return 0;
714 }
715
716 /*
717 * Check group list if present, otherwise everything is supported.
718 */
719 tls1_get_group_list(s, 1, &groups, &groupslen);
720 if (groups != NULL) {
721 for (i = 0; i < groupslen; i++) {
722 if (groups[i] == group_id)
723 break;
724 }
725 if (i == groupslen)
726 return 0;
727 }
728
729 return 1;
730}
731
732/* Check EC server key is compatible with client extensions. */
733int
734tls1_check_ec_server_key(SSL *s)
735{
736 SSL_CERT_PKEY *cpk = s->cert->pkeys + SSL_PKEY_ECC;
737 uint16_t group_id;
738 uint8_t comp_id;
739 EC_KEY *eckey;
740 EVP_PKEY *pkey;
741
742 if (cpk->x509 == NULL || cpk->privatekey == NULL)
743 return 0;
744 if ((pkey = X509_get0_pubkey(cpk->x509)) == NULL)
745 return 0;
746 if ((eckey = EVP_PKEY_get0_EC_KEY(pkey)) == NULL)
747 return 0;
748 if (!tls1_set_ec_id(&group_id, &comp_id, eckey))
749 return 0;
750
751 return tls1_check_ec_key(s, group_id, comp_id);
752}
753
754int
755ssl_check_clienthello_tlsext_early(SSL *s)
756{
757 int ret = SSL_TLSEXT_ERR_NOACK;
758 int al = SSL_AD_UNRECOGNIZED_NAME;
759
760 /* The handling of the ECPointFormats extension is done elsewhere, namely in
761 * ssl3_choose_cipher in s3_lib.c.
762 */
763 /* The handling of the EllipticCurves extension is done elsewhere, namely in
764 * ssl3_choose_cipher in s3_lib.c.
765 */
766
767 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
768 ret = s->ctx->tlsext_servername_callback(s, &al,
769 s->ctx->tlsext_servername_arg);
770 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
771 ret = s->initial_ctx->tlsext_servername_callback(s, &al,
772 s->initial_ctx->tlsext_servername_arg);
773
774 switch (ret) {
775 case SSL_TLSEXT_ERR_ALERT_FATAL:
776 ssl3_send_alert(s, SSL3_AL_FATAL, al);
777 return -1;
778 case SSL_TLSEXT_ERR_ALERT_WARNING:
779 ssl3_send_alert(s, SSL3_AL_WARNING, al);
780 return 1;
781 case SSL_TLSEXT_ERR_NOACK:
782 default:
783 return 1;
784 }
785}
786
787int
788ssl_check_clienthello_tlsext_late(SSL *s)
789{
790 int ret = SSL_TLSEXT_ERR_OK;
791 int al = 0; /* XXX gcc3 */
792
793 /* If status request then ask callback what to do.
794 * Note: this must be called after servername callbacks in case
795 * the certificate has changed, and must be called after the cipher
796 * has been chosen because this may influence which certificate is sent
797 */
798 if ((s->tlsext_status_type != -1) &&
799 s->ctx && s->ctx->tlsext_status_cb) {
800 int r;
801 SSL_CERT_PKEY *certpkey;
802 certpkey = ssl_get_server_send_pkey(s);
803 /* If no certificate can't return certificate status */
804 if (certpkey == NULL) {
805 s->tlsext_status_expected = 0;
806 return 1;
807 }
808 /* Set current certificate to one we will use so
809 * SSL_get_certificate et al can pick it up.
810 */
811 s->cert->key = certpkey;
812 r = s->ctx->tlsext_status_cb(s,
813 s->ctx->tlsext_status_arg);
814 switch (r) {
815 /* We don't want to send a status request response */
816 case SSL_TLSEXT_ERR_NOACK:
817 s->tlsext_status_expected = 0;
818 break;
819 /* status request response should be sent */
820 case SSL_TLSEXT_ERR_OK:
821 if (s->tlsext_ocsp_resp)
822 s->tlsext_status_expected = 1;
823 else
824 s->tlsext_status_expected = 0;
825 break;
826 /* something bad happened */
827 case SSL_TLSEXT_ERR_ALERT_FATAL:
828 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
829 al = SSL_AD_INTERNAL_ERROR;
830 goto err;
831 }
832 } else
833 s->tlsext_status_expected = 0;
834
835 err:
836 switch (ret) {
837 case SSL_TLSEXT_ERR_ALERT_FATAL:
838 ssl3_send_alert(s, SSL3_AL_FATAL, al);
839 return -1;
840 case SSL_TLSEXT_ERR_ALERT_WARNING:
841 ssl3_send_alert(s, SSL3_AL_WARNING, al);
842 return 1;
843 default:
844 return 1;
845 }
846}
847
848int
849ssl_check_serverhello_tlsext(SSL *s)
850{
851 int ret = SSL_TLSEXT_ERR_NOACK;
852 int al = SSL_AD_UNRECOGNIZED_NAME;
853
854 ret = SSL_TLSEXT_ERR_OK;
855
856 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
857 ret = s->ctx->tlsext_servername_callback(s, &al,
858 s->ctx->tlsext_servername_arg);
859 else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0)
860 ret = s->initial_ctx->tlsext_servername_callback(s, &al,
861 s->initial_ctx->tlsext_servername_arg);
862
863 /* If we've requested certificate status and we wont get one
864 * tell the callback
865 */
866 if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected) &&
867 s->ctx && s->ctx->tlsext_status_cb) {
868 int r;
869
870 free(s->tlsext_ocsp_resp);
871 s->tlsext_ocsp_resp = NULL;
872 s->tlsext_ocsp_resp_len = 0;
873
874 r = s->ctx->tlsext_status_cb(s,
875 s->ctx->tlsext_status_arg);
876 if (r == 0) {
877 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
878 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
879 }
880 if (r < 0) {
881 al = SSL_AD_INTERNAL_ERROR;
882 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
883 }
884 }
885
886 switch (ret) {
887 case SSL_TLSEXT_ERR_ALERT_FATAL:
888 ssl3_send_alert(s, SSL3_AL_FATAL, al);
889 return -1;
890 case SSL_TLSEXT_ERR_ALERT_WARNING:
891 ssl3_send_alert(s, SSL3_AL_WARNING, al);
892 return 1;
893 case SSL_TLSEXT_ERR_NOACK:
894 default:
895 return 1;
896 }
897}
898
899/* Since the server cache lookup is done early on in the processing of the
900 * ClientHello, and other operations depend on the result, we need to handle
901 * any TLS session ticket extension at the same time.
902 *
903 * ext_block: a CBS for the ClientHello extensions block.
904 * ret: (output) on return, if a ticket was decrypted, then this is set to
905 * point to the resulting session.
906 *
907 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
908 * ciphersuite, in which case we have no use for session tickets and one will
909 * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
910 *
911 * Returns:
912 * TLS1_TICKET_FATAL_ERROR: error from parsing or decrypting the ticket.
913 * TLS1_TICKET_NONE: no ticket was found (or was ignored, based on settings).
914 * TLS1_TICKET_EMPTY: a zero length extension was found, indicating that the
915 * client supports session tickets but doesn't currently have one to offer.
916 * TLS1_TICKET_NOT_DECRYPTED: either s->tls_session_secret_cb was
917 * set, or a ticket was offered but couldn't be decrypted because of a
918 * non-fatal error.
919 * TLS1_TICKET_DECRYPTED: a ticket was successfully decrypted and *ret was set.
920 *
921 * Side effects:
922 * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
923 * a new session ticket to the client because the client indicated support
924 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
925 * a session ticket or we couldn't use the one it gave us, or if
926 * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
927 * Otherwise, s->tlsext_ticket_expected is set to 0.
928 */
929int
930tls1_process_ticket(SSL *s, CBS *ext_block, int *alert, SSL_SESSION **ret)
931{
932 CBS extensions, ext_data;
933 uint16_t ext_type = 0;
934
935 s->tlsext_ticket_expected = 0;
936 *ret = NULL;
937
938 /*
939 * If tickets disabled behave as if no ticket present to permit stateful
940 * resumption.
941 */
942 if (SSL_get_options(s) & SSL_OP_NO_TICKET)
943 return TLS1_TICKET_NONE;
944
945 /*
946 * An empty extensions block is valid, but obviously does not contain
947 * a session ticket.
948 */
949 if (CBS_len(ext_block) == 0)
950 return TLS1_TICKET_NONE;
951
952 if (!CBS_get_u16_length_prefixed(ext_block, &extensions)) {
953 *alert = SSL_AD_DECODE_ERROR;
954 return TLS1_TICKET_FATAL_ERROR;
955 }
956
957 while (CBS_len(&extensions) > 0) {
958 if (!CBS_get_u16(&extensions, &ext_type) ||
959 !CBS_get_u16_length_prefixed(&extensions, &ext_data)) {
960 *alert = SSL_AD_DECODE_ERROR;
961 return TLS1_TICKET_FATAL_ERROR;
962 }
963
964 if (ext_type == TLSEXT_TYPE_session_ticket)
965 break;
966 }
967
968 if (ext_type != TLSEXT_TYPE_session_ticket)
969 return TLS1_TICKET_NONE;
970
971 if (CBS_len(&ext_data) == 0) {
972 /*
973 * The client will accept a ticket but does not currently
974 * have one.
975 */
976 s->tlsext_ticket_expected = 1;
977 return TLS1_TICKET_EMPTY;
978 }
979
980 if (s->tls_session_secret_cb != NULL) {
981 /*
982 * Indicate that the ticket could not be decrypted rather than
983 * generating the session from ticket now, trigger abbreviated
984 * handshake based on external mechanism to calculate the master
985 * secret later.
986 */
987 return TLS1_TICKET_NOT_DECRYPTED;
988 }
989
990 return tls_decrypt_ticket(s, &ext_data, alert, ret);
991}
992
993/* tls_decrypt_ticket attempts to decrypt a session ticket.
994 *
995 * ticket: a CBS containing the body of the session ticket extension.
996 * psess: (output) on return, if a ticket was decrypted, then this is set to
997 * point to the resulting session.
998 *
999 * Returns:
1000 * TLS1_TICKET_FATAL_ERROR: error from parsing or decrypting the ticket.
1001 * TLS1_TICKET_NOT_DECRYPTED: the ticket couldn't be decrypted.
1002 * TLS1_TICKET_DECRYPTED: a ticket was decrypted and *psess was set.
1003 */
1004static int
1005tls_decrypt_ticket(SSL *s, CBS *ticket, int *alert, SSL_SESSION **psess)
1006{
1007 CBS ticket_name, ticket_iv, ticket_encdata, ticket_hmac;
1008 SSL_SESSION *sess = NULL;
1009 unsigned char *sdec = NULL;
1010 size_t sdec_len = 0;
1011 const unsigned char *p;
1012 unsigned char hmac[EVP_MAX_MD_SIZE];
1013 HMAC_CTX *hctx = NULL;
1014 EVP_CIPHER_CTX *cctx = NULL;
1015 SSL_CTX *tctx = s->initial_ctx;
1016 int slen, hlen, iv_len;
1017 int alert_desc = SSL_AD_INTERNAL_ERROR;
1018 int ret = TLS1_TICKET_FATAL_ERROR;
1019
1020 *psess = NULL;
1021
1022 if (!CBS_get_bytes(ticket, &ticket_name, 16))
1023 goto derr;
1024
1025 /*
1026 * Initialize session ticket encryption and HMAC contexts.
1027 */
1028 if ((cctx = EVP_CIPHER_CTX_new()) == NULL)
1029 goto err;
1030 if ((hctx = HMAC_CTX_new()) == NULL)
1031 goto err;
1032
1033 if (tctx->tlsext_ticket_key_cb != NULL) {
1034 int rv;
1035
1036 /*
1037 * The API guarantees EVP_MAX_IV_LENGTH bytes of space for
1038 * the iv to tlsext_ticket_key_cb(). Since the total space
1039 * required for a session cookie is never less than this,
1040 * this check isn't too strict. The exact check comes later.
1041 */
1042 if (CBS_len(ticket) < EVP_MAX_IV_LENGTH)
1043 goto derr;
1044
1045 if ((rv = tctx->tlsext_ticket_key_cb(s,
1046 (unsigned char *)CBS_data(&ticket_name),
1047 (unsigned char *)CBS_data(ticket), cctx, hctx, 0)) < 0)
1048 goto err;
1049 if (rv == 0)
1050 goto derr;
1051 if (rv == 2) {
1052 /* Renew ticket. */
1053 s->tlsext_ticket_expected = 1;
1054 }
1055
1056 if ((iv_len = EVP_CIPHER_CTX_iv_length(cctx)) < 0)
1057 goto err;
1058 /*
1059 * Now that the cipher context is initialised, we can extract
1060 * the IV since its length is known.
1061 */
1062 if (!CBS_get_bytes(ticket, &ticket_iv, iv_len))
1063 goto derr;
1064 } else {
1065 /* Check that the key name matches. */
1066 if (!CBS_mem_equal(&ticket_name,
1067 tctx->tlsext_tick_key_name,
1068 sizeof(tctx->tlsext_tick_key_name)))
1069 goto derr;
1070 if ((iv_len = EVP_CIPHER_iv_length(EVP_aes_128_cbc())) < 0)
1071 goto err;
1072 if (!CBS_get_bytes(ticket, &ticket_iv, iv_len))
1073 goto derr;
1074 if (!EVP_DecryptInit_ex(cctx, EVP_aes_128_cbc(), NULL,
1075 tctx->tlsext_tick_aes_key, CBS_data(&ticket_iv)))
1076 goto err;
1077 if (!HMAC_Init_ex(hctx, tctx->tlsext_tick_hmac_key,
1078 sizeof(tctx->tlsext_tick_hmac_key), EVP_sha256(),
1079 NULL))
1080 goto err;
1081 }
1082
1083 /*
1084 * Attempt to process session ticket.
1085 */
1086
1087 if ((hlen = HMAC_size(hctx)) < 0)
1088 goto err;
1089
1090 if (hlen > CBS_len(ticket))
1091 goto derr;
1092 if (!CBS_get_bytes(ticket, &ticket_encdata, CBS_len(ticket) - hlen))
1093 goto derr;
1094 if (!CBS_get_bytes(ticket, &ticket_hmac, hlen))
1095 goto derr;
1096 if (CBS_len(ticket) != 0) {
1097 alert_desc = SSL_AD_DECODE_ERROR;
1098 goto err;
1099 }
1100
1101 /* Check HMAC of encrypted ticket. */
1102 if (HMAC_Update(hctx, CBS_data(&ticket_name),
1103 CBS_len(&ticket_name)) <= 0)
1104 goto err;
1105 if (HMAC_Update(hctx, CBS_data(&ticket_iv),
1106 CBS_len(&ticket_iv)) <= 0)
1107 goto err;
1108 if (HMAC_Update(hctx, CBS_data(&ticket_encdata),
1109 CBS_len(&ticket_encdata)) <= 0)
1110 goto err;
1111 if (HMAC_Final(hctx, hmac, &hlen) <= 0)
1112 goto err;
1113
1114 if (!CBS_mem_equal(&ticket_hmac, hmac, hlen))
1115 goto derr;
1116
1117 /* Attempt to decrypt session data. */
1118 sdec_len = CBS_len(&ticket_encdata);
1119 if ((sdec = calloc(1, sdec_len)) == NULL)
1120 goto err;
1121 if (EVP_DecryptUpdate(cctx, sdec, &slen, CBS_data(&ticket_encdata),
1122 CBS_len(&ticket_encdata)) <= 0)
1123 goto derr;
1124 if (EVP_DecryptFinal_ex(cctx, sdec + slen, &hlen) <= 0)
1125 goto derr;
1126
1127 slen += hlen;
1128
1129 /*
1130 * For session parse failures, indicate that we need to send a new
1131 * ticket.
1132 */
1133 p = sdec;
1134 if ((sess = d2i_SSL_SESSION(NULL, &p, slen)) == NULL)
1135 goto derr;
1136 *psess = sess;
1137 sess = NULL;
1138
1139 ret = TLS1_TICKET_DECRYPTED;
1140 goto done;
1141
1142 derr:
1143 ERR_clear_error();
1144 s->tlsext_ticket_expected = 1;
1145 ret = TLS1_TICKET_NOT_DECRYPTED;
1146 goto done;
1147
1148 err:
1149 *alert = alert_desc;
1150 ret = TLS1_TICKET_FATAL_ERROR;
1151 goto done;
1152
1153 done:
1154 freezero(sdec, sdec_len);
1155 EVP_CIPHER_CTX_free(cctx);
1156 HMAC_CTX_free(hctx);
1157 SSL_SESSION_free(sess);
1158
1159 return ret;
1160}