]> git.ipfire.org Git - thirdparty/openssl.git/blob - ssl/t1_lib.c
Add support for the TLS 1.3 signature_algorithms_cert extension
[thirdparty/openssl.git] / ssl / t1_lib.c
1 /*
2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <openssl/objects.h>
13 #include <openssl/evp.h>
14 #include <openssl/hmac.h>
15 #include <openssl/ocsp.h>
16 #include <openssl/conf.h>
17 #include <openssl/x509v3.h>
18 #include <openssl/dh.h>
19 #include <openssl/bn.h>
20 #include "internal/nelem.h"
21 #include "ssl_locl.h"
22 #include <openssl/ct.h>
23
24 SSL3_ENC_METHOD const TLSv1_enc_data = {
25 tls1_enc,
26 tls1_mac,
27 tls1_setup_key_block,
28 tls1_generate_master_secret,
29 tls1_change_cipher_state,
30 tls1_final_finish_mac,
31 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
32 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
33 tls1_alert_code,
34 tls1_export_keying_material,
35 0,
36 ssl3_set_handshake_header,
37 tls_close_construct_packet,
38 ssl3_handshake_write
39 };
40
41 SSL3_ENC_METHOD const TLSv1_1_enc_data = {
42 tls1_enc,
43 tls1_mac,
44 tls1_setup_key_block,
45 tls1_generate_master_secret,
46 tls1_change_cipher_state,
47 tls1_final_finish_mac,
48 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
49 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
50 tls1_alert_code,
51 tls1_export_keying_material,
52 SSL_ENC_FLAG_EXPLICIT_IV,
53 ssl3_set_handshake_header,
54 tls_close_construct_packet,
55 ssl3_handshake_write
56 };
57
58 SSL3_ENC_METHOD const TLSv1_2_enc_data = {
59 tls1_enc,
60 tls1_mac,
61 tls1_setup_key_block,
62 tls1_generate_master_secret,
63 tls1_change_cipher_state,
64 tls1_final_finish_mac,
65 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
66 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
67 tls1_alert_code,
68 tls1_export_keying_material,
69 SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF
70 | SSL_ENC_FLAG_TLS1_2_CIPHERS,
71 ssl3_set_handshake_header,
72 tls_close_construct_packet,
73 ssl3_handshake_write
74 };
75
76 SSL3_ENC_METHOD const TLSv1_3_enc_data = {
77 tls13_enc,
78 tls1_mac,
79 tls13_setup_key_block,
80 tls13_generate_master_secret,
81 tls13_change_cipher_state,
82 tls13_final_finish_mac,
83 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
84 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
85 tls13_alert_code,
86 tls13_export_keying_material,
87 SSL_ENC_FLAG_SIGALGS | SSL_ENC_FLAG_SHA256_PRF,
88 ssl3_set_handshake_header,
89 tls_close_construct_packet,
90 ssl3_handshake_write
91 };
92
93 long tls1_default_timeout(void)
94 {
95 /*
96 * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for
97 * http, the cache would over fill
98 */
99 return (60 * 60 * 2);
100 }
101
102 int tls1_new(SSL *s)
103 {
104 if (!ssl3_new(s))
105 return 0;
106 if (!s->method->ssl_clear(s))
107 return 0;
108
109 return 1;
110 }
111
112 void tls1_free(SSL *s)
113 {
114 OPENSSL_free(s->ext.session_ticket);
115 ssl3_free(s);
116 }
117
118 int tls1_clear(SSL *s)
119 {
120 if (!ssl3_clear(s))
121 return 0;
122
123 if (s->method->version == TLS_ANY_VERSION)
124 s->version = TLS_MAX_VERSION;
125 else
126 s->version = s->method->version;
127
128 return 1;
129 }
130
131 #ifndef OPENSSL_NO_EC
132
133 /*
134 * Table of curve information.
135 * Do not delete entries or reorder this array! It is used as a lookup
136 * table: the index of each entry is one less than the TLS curve id.
137 */
138 static const TLS_GROUP_INFO nid_list[] = {
139 {NID_sect163k1, 80, TLS_CURVE_CHAR2}, /* sect163k1 (1) */
140 {NID_sect163r1, 80, TLS_CURVE_CHAR2}, /* sect163r1 (2) */
141 {NID_sect163r2, 80, TLS_CURVE_CHAR2}, /* sect163r2 (3) */
142 {NID_sect193r1, 80, TLS_CURVE_CHAR2}, /* sect193r1 (4) */
143 {NID_sect193r2, 80, TLS_CURVE_CHAR2}, /* sect193r2 (5) */
144 {NID_sect233k1, 112, TLS_CURVE_CHAR2}, /* sect233k1 (6) */
145 {NID_sect233r1, 112, TLS_CURVE_CHAR2}, /* sect233r1 (7) */
146 {NID_sect239k1, 112, TLS_CURVE_CHAR2}, /* sect239k1 (8) */
147 {NID_sect283k1, 128, TLS_CURVE_CHAR2}, /* sect283k1 (9) */
148 {NID_sect283r1, 128, TLS_CURVE_CHAR2}, /* sect283r1 (10) */
149 {NID_sect409k1, 192, TLS_CURVE_CHAR2}, /* sect409k1 (11) */
150 {NID_sect409r1, 192, TLS_CURVE_CHAR2}, /* sect409r1 (12) */
151 {NID_sect571k1, 256, TLS_CURVE_CHAR2}, /* sect571k1 (13) */
152 {NID_sect571r1, 256, TLS_CURVE_CHAR2}, /* sect571r1 (14) */
153 {NID_secp160k1, 80, TLS_CURVE_PRIME}, /* secp160k1 (15) */
154 {NID_secp160r1, 80, TLS_CURVE_PRIME}, /* secp160r1 (16) */
155 {NID_secp160r2, 80, TLS_CURVE_PRIME}, /* secp160r2 (17) */
156 {NID_secp192k1, 80, TLS_CURVE_PRIME}, /* secp192k1 (18) */
157 {NID_X9_62_prime192v1, 80, TLS_CURVE_PRIME}, /* secp192r1 (19) */
158 {NID_secp224k1, 112, TLS_CURVE_PRIME}, /* secp224k1 (20) */
159 {NID_secp224r1, 112, TLS_CURVE_PRIME}, /* secp224r1 (21) */
160 {NID_secp256k1, 128, TLS_CURVE_PRIME}, /* secp256k1 (22) */
161 {NID_X9_62_prime256v1, 128, TLS_CURVE_PRIME}, /* secp256r1 (23) */
162 {NID_secp384r1, 192, TLS_CURVE_PRIME}, /* secp384r1 (24) */
163 {NID_secp521r1, 256, TLS_CURVE_PRIME}, /* secp521r1 (25) */
164 {NID_brainpoolP256r1, 128, TLS_CURVE_PRIME}, /* brainpoolP256r1 (26) */
165 {NID_brainpoolP384r1, 192, TLS_CURVE_PRIME}, /* brainpoolP384r1 (27) */
166 {NID_brainpoolP512r1, 256, TLS_CURVE_PRIME}, /* brainpool512r1 (28) */
167 {EVP_PKEY_X25519, 128, TLS_CURVE_CUSTOM}, /* X25519 (29) */
168 };
169
170 static const unsigned char ecformats_default[] = {
171 TLSEXT_ECPOINTFORMAT_uncompressed,
172 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime,
173 TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2
174 };
175
176 /* The default curves */
177 static const uint16_t eccurves_default[] = {
178 29, /* X25519 (29) */
179 23, /* secp256r1 (23) */
180 25, /* secp521r1 (25) */
181 24, /* secp384r1 (24) */
182 };
183
184 static const uint16_t suiteb_curves[] = {
185 TLSEXT_curve_P_256,
186 TLSEXT_curve_P_384
187 };
188
189 const TLS_GROUP_INFO *tls1_group_id_lookup(uint16_t group_id)
190 {
191 /* ECC curves from RFC 4492 and RFC 7027 */
192 if (group_id < 1 || group_id > OSSL_NELEM(nid_list))
193 return NULL;
194 return &nid_list[group_id - 1];
195 }
196
197 static uint16_t tls1_nid2group_id(int nid)
198 {
199 size_t i;
200 for (i = 0; i < OSSL_NELEM(nid_list); i++) {
201 if (nid_list[i].nid == nid)
202 return (uint16_t)(i + 1);
203 }
204 return 0;
205 }
206
207 /*
208 * Set *pgroups to the supported groups list and *pgroupslen to
209 * the number of groups supported.
210 */
211 void tls1_get_supported_groups(SSL *s, const uint16_t **pgroups,
212 size_t *pgroupslen)
213 {
214
215 /* For Suite B mode only include P-256, P-384 */
216 switch (tls1_suiteb(s)) {
217 case SSL_CERT_FLAG_SUITEB_128_LOS:
218 *pgroups = suiteb_curves;
219 *pgroupslen = OSSL_NELEM(suiteb_curves);
220 break;
221
222 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
223 *pgroups = suiteb_curves;
224 *pgroupslen = 1;
225 break;
226
227 case SSL_CERT_FLAG_SUITEB_192_LOS:
228 *pgroups = suiteb_curves + 1;
229 *pgroupslen = 1;
230 break;
231
232 default:
233 if (s->ext.supportedgroups == NULL) {
234 *pgroups = eccurves_default;
235 *pgroupslen = OSSL_NELEM(eccurves_default);
236 } else {
237 *pgroups = s->ext.supportedgroups;
238 *pgroupslen = s->ext.supportedgroups_len;
239 }
240 break;
241 }
242 }
243
244 /* See if curve is allowed by security callback */
245 int tls_curve_allowed(SSL *s, uint16_t curve, int op)
246 {
247 const TLS_GROUP_INFO *cinfo = tls1_group_id_lookup(curve);
248 unsigned char ctmp[2];
249
250 if (cinfo == NULL)
251 return 0;
252 # ifdef OPENSSL_NO_EC2M
253 if (cinfo->flags & TLS_CURVE_CHAR2)
254 return 0;
255 # endif
256 ctmp[0] = curve >> 8;
257 ctmp[1] = curve & 0xff;
258 return ssl_security(s, op, cinfo->secbits, cinfo->nid, (void *)ctmp);
259 }
260
261 /* Return 1 if "id" is in "list" */
262 static int tls1_in_list(uint16_t id, const uint16_t *list, size_t listlen)
263 {
264 size_t i;
265 for (i = 0; i < listlen; i++)
266 if (list[i] == id)
267 return 1;
268 return 0;
269 }
270
271 /*-
272 * For nmatch >= 0, return the id of the |nmatch|th shared group or 0
273 * if there is no match.
274 * For nmatch == -1, return number of matches
275 * For nmatch == -2, return the id of the group to use for
276 * a tmp key, or 0 if there is no match.
277 */
278 uint16_t tls1_shared_group(SSL *s, int nmatch)
279 {
280 const uint16_t *pref, *supp;
281 size_t num_pref, num_supp, i;
282 int k;
283
284 /* Can't do anything on client side */
285 if (s->server == 0)
286 return 0;
287 if (nmatch == -2) {
288 if (tls1_suiteb(s)) {
289 /*
290 * For Suite B ciphersuite determines curve: we already know
291 * these are acceptable due to previous checks.
292 */
293 unsigned long cid = s->s3->tmp.new_cipher->id;
294
295 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
296 return TLSEXT_curve_P_256;
297 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
298 return TLSEXT_curve_P_384;
299 /* Should never happen */
300 return 0;
301 }
302 /* If not Suite B just return first preference shared curve */
303 nmatch = 0;
304 }
305 /*
306 * If server preference set, our groups are the preference order
307 * otherwise peer decides.
308 */
309 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
310 tls1_get_supported_groups(s, &pref, &num_pref);
311 tls1_get_peer_groups(s, &supp, &num_supp);
312 } else {
313 tls1_get_peer_groups(s, &pref, &num_pref);
314 tls1_get_supported_groups(s, &supp, &num_supp);
315 }
316
317 for (k = 0, i = 0; i < num_pref; i++) {
318 uint16_t id = pref[i];
319
320 if (!tls1_in_list(id, supp, num_supp)
321 || !tls_curve_allowed(s, id, SSL_SECOP_CURVE_SHARED))
322 continue;
323 if (nmatch == k)
324 return id;
325 k++;
326 }
327 if (nmatch == -1)
328 return k;
329 /* Out of range (nmatch > k). */
330 return 0;
331 }
332
333 int tls1_set_groups(uint16_t **pext, size_t *pextlen,
334 int *groups, size_t ngroups)
335 {
336 uint16_t *glist;
337 size_t i;
338 /*
339 * Bitmap of groups included to detect duplicates: only works while group
340 * ids < 32
341 */
342 unsigned long dup_list = 0;
343 glist = OPENSSL_malloc(ngroups * sizeof(*glist));
344 if (glist == NULL)
345 return 0;
346 for (i = 0; i < ngroups; i++) {
347 unsigned long idmask;
348 uint16_t id;
349 /* TODO(TLS1.3): Convert for DH groups */
350 id = tls1_nid2group_id(groups[i]);
351 idmask = 1L << id;
352 if (!id || (dup_list & idmask)) {
353 OPENSSL_free(glist);
354 return 0;
355 }
356 dup_list |= idmask;
357 glist[i] = id;
358 }
359 OPENSSL_free(*pext);
360 *pext = glist;
361 *pextlen = ngroups;
362 return 1;
363 }
364
365 # define MAX_CURVELIST 28
366
367 typedef struct {
368 size_t nidcnt;
369 int nid_arr[MAX_CURVELIST];
370 } nid_cb_st;
371
372 static int nid_cb(const char *elem, int len, void *arg)
373 {
374 nid_cb_st *narg = arg;
375 size_t i;
376 int nid;
377 char etmp[20];
378 if (elem == NULL)
379 return 0;
380 if (narg->nidcnt == MAX_CURVELIST)
381 return 0;
382 if (len > (int)(sizeof(etmp) - 1))
383 return 0;
384 memcpy(etmp, elem, len);
385 etmp[len] = 0;
386 nid = EC_curve_nist2nid(etmp);
387 if (nid == NID_undef)
388 nid = OBJ_sn2nid(etmp);
389 if (nid == NID_undef)
390 nid = OBJ_ln2nid(etmp);
391 if (nid == NID_undef)
392 return 0;
393 for (i = 0; i < narg->nidcnt; i++)
394 if (narg->nid_arr[i] == nid)
395 return 0;
396 narg->nid_arr[narg->nidcnt++] = nid;
397 return 1;
398 }
399
400 /* Set groups based on a colon separate list */
401 int tls1_set_groups_list(uint16_t **pext, size_t *pextlen, const char *str)
402 {
403 nid_cb_st ncb;
404 ncb.nidcnt = 0;
405 if (!CONF_parse_list(str, ':', 1, nid_cb, &ncb))
406 return 0;
407 if (pext == NULL)
408 return 1;
409 return tls1_set_groups(pext, pextlen, ncb.nid_arr, ncb.nidcnt);
410 }
411 /* Return group id of a key */
412 static uint16_t tls1_get_group_id(EVP_PKEY *pkey)
413 {
414 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
415 const EC_GROUP *grp;
416
417 if (ec == NULL)
418 return 0;
419 grp = EC_KEY_get0_group(ec);
420 return tls1_nid2group_id(EC_GROUP_get_curve_name(grp));
421 }
422
423 /* Check a key is compatible with compression extension */
424 static int tls1_check_pkey_comp(SSL *s, EVP_PKEY *pkey)
425 {
426 const EC_KEY *ec;
427 const EC_GROUP *grp;
428 unsigned char comp_id;
429 size_t i;
430
431 /* If not an EC key nothing to check */
432 if (EVP_PKEY_id(pkey) != EVP_PKEY_EC)
433 return 1;
434 ec = EVP_PKEY_get0_EC_KEY(pkey);
435 grp = EC_KEY_get0_group(ec);
436
437 /* Get required compression id */
438 if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_UNCOMPRESSED) {
439 comp_id = TLSEXT_ECPOINTFORMAT_uncompressed;
440 } else if (SSL_IS_TLS13(s)) {
441 /* Compression not allowed in TLS 1.3 */
442 return 0;
443 } else {
444 int field_type = EC_METHOD_get_field_type(EC_GROUP_method_of(grp));
445
446 if (field_type == NID_X9_62_prime_field)
447 comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
448 else if (field_type == NID_X9_62_characteristic_two_field)
449 comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
450 else
451 return 0;
452 }
453 /*
454 * If point formats extension present check it, otherwise everything is
455 * supported (see RFC4492).
456 */
457 if (s->session->ext.ecpointformats == NULL)
458 return 1;
459
460 for (i = 0; i < s->session->ext.ecpointformats_len; i++) {
461 if (s->session->ext.ecpointformats[i] == comp_id)
462 return 1;
463 }
464 return 0;
465 }
466
467 /* Check a group id matches preferences */
468 int tls1_check_group_id(SSL *s, uint16_t group_id)
469 {
470 const uint16_t *groups;
471 size_t groups_len;
472
473 if (group_id == 0)
474 return 0;
475
476 /* Check for Suite B compliance */
477 if (tls1_suiteb(s) && s->s3->tmp.new_cipher != NULL) {
478 unsigned long cid = s->s3->tmp.new_cipher->id;
479
480 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256) {
481 if (group_id != TLSEXT_curve_P_256)
482 return 0;
483 } else if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384) {
484 if (group_id != TLSEXT_curve_P_384)
485 return 0;
486 } else {
487 /* Should never happen */
488 return 0;
489 }
490 }
491
492 /* Check group is one of our preferences */
493 tls1_get_supported_groups(s, &groups, &groups_len);
494 if (!tls1_in_list(group_id, groups, groups_len))
495 return 0;
496
497 if (!tls_curve_allowed(s, group_id, SSL_SECOP_CURVE_CHECK))
498 return 0;
499
500 /* For clients, nothing more to check */
501 if (!s->server)
502 return 1;
503
504 /* Check group is one of peers preferences */
505 tls1_get_peer_groups(s, &groups, &groups_len);
506
507 /*
508 * RFC 4492 does not require the supported elliptic curves extension
509 * so if it is not sent we can just choose any curve.
510 * It is invalid to send an empty list in the supported groups
511 * extension, so groups_len == 0 always means no extension.
512 */
513 if (groups_len == 0)
514 return 1;
515 return tls1_in_list(group_id, groups, groups_len);
516 }
517
518 void tls1_get_formatlist(SSL *s, const unsigned char **pformats,
519 size_t *num_formats)
520 {
521 /*
522 * If we have a custom point format list use it otherwise use default
523 */
524 if (s->ext.ecpointformats) {
525 *pformats = s->ext.ecpointformats;
526 *num_formats = s->ext.ecpointformats_len;
527 } else {
528 *pformats = ecformats_default;
529 /* For Suite B we don't support char2 fields */
530 if (tls1_suiteb(s))
531 *num_formats = sizeof(ecformats_default) - 1;
532 else
533 *num_formats = sizeof(ecformats_default);
534 }
535 }
536
537 /*
538 * Check cert parameters compatible with extensions: currently just checks EC
539 * certificates have compatible curves and compression.
540 */
541 static int tls1_check_cert_param(SSL *s, X509 *x, int check_ee_md)
542 {
543 uint16_t group_id;
544 EVP_PKEY *pkey;
545 pkey = X509_get0_pubkey(x);
546 if (pkey == NULL)
547 return 0;
548 /* If not EC nothing to do */
549 if (EVP_PKEY_id(pkey) != EVP_PKEY_EC)
550 return 1;
551 /* Check compression */
552 if (!tls1_check_pkey_comp(s, pkey))
553 return 0;
554 group_id = tls1_get_group_id(pkey);
555 if (!tls1_check_group_id(s, group_id))
556 return 0;
557 /*
558 * Special case for suite B. We *MUST* sign using SHA256+P-256 or
559 * SHA384+P-384.
560 */
561 if (check_ee_md && tls1_suiteb(s)) {
562 int check_md;
563 size_t i;
564 CERT *c = s->cert;
565
566 /* Check to see we have necessary signing algorithm */
567 if (group_id == TLSEXT_curve_P_256)
568 check_md = NID_ecdsa_with_SHA256;
569 else if (group_id == TLSEXT_curve_P_384)
570 check_md = NID_ecdsa_with_SHA384;
571 else
572 return 0; /* Should never happen */
573 for (i = 0; i < c->shared_sigalgslen; i++) {
574 if (check_md == c->shared_sigalgs[i]->sigandhash)
575 return 1;;
576 }
577 return 0;
578 }
579 return 1;
580 }
581
582 /*
583 * tls1_check_ec_tmp_key - Check EC temporary key compatibility
584 * @s: SSL connection
585 * @cid: Cipher ID we're considering using
586 *
587 * Checks that the kECDHE cipher suite we're considering using
588 * is compatible with the client extensions.
589 *
590 * Returns 0 when the cipher can't be used or 1 when it can.
591 */
592 int tls1_check_ec_tmp_key(SSL *s, unsigned long cid)
593 {
594 /* If not Suite B just need a shared group */
595 if (!tls1_suiteb(s))
596 return tls1_shared_group(s, 0) != 0;
597 /*
598 * If Suite B, AES128 MUST use P-256 and AES256 MUST use P-384, no other
599 * curves permitted.
600 */
601 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256)
602 return tls1_check_group_id(s, TLSEXT_curve_P_256);
603 if (cid == TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
604 return tls1_check_group_id(s, TLSEXT_curve_P_384);
605
606 return 0;
607 }
608
609 #else
610
611 static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md)
612 {
613 return 1;
614 }
615
616 #endif /* OPENSSL_NO_EC */
617
618 /* Default sigalg schemes */
619 static const uint16_t tls12_sigalgs[] = {
620 #ifndef OPENSSL_NO_EC
621 TLSEXT_SIGALG_ecdsa_secp256r1_sha256,
622 TLSEXT_SIGALG_ecdsa_secp384r1_sha384,
623 TLSEXT_SIGALG_ecdsa_secp521r1_sha512,
624 TLSEXT_SIGALG_ed25519,
625 #endif
626
627 TLSEXT_SIGALG_rsa_pss_pss_sha256,
628 TLSEXT_SIGALG_rsa_pss_pss_sha384,
629 TLSEXT_SIGALG_rsa_pss_pss_sha512,
630 TLSEXT_SIGALG_rsa_pss_rsae_sha256,
631 TLSEXT_SIGALG_rsa_pss_rsae_sha384,
632 TLSEXT_SIGALG_rsa_pss_rsae_sha512,
633
634 TLSEXT_SIGALG_rsa_pkcs1_sha256,
635 TLSEXT_SIGALG_rsa_pkcs1_sha384,
636 TLSEXT_SIGALG_rsa_pkcs1_sha512,
637
638 #ifndef OPENSSL_NO_EC
639 TLSEXT_SIGALG_ecdsa_sha224,
640 TLSEXT_SIGALG_ecdsa_sha1,
641 #endif
642 TLSEXT_SIGALG_rsa_pkcs1_sha224,
643 TLSEXT_SIGALG_rsa_pkcs1_sha1,
644 #ifndef OPENSSL_NO_DSA
645 TLSEXT_SIGALG_dsa_sha224,
646 TLSEXT_SIGALG_dsa_sha1,
647
648 TLSEXT_SIGALG_dsa_sha256,
649 TLSEXT_SIGALG_dsa_sha384,
650 TLSEXT_SIGALG_dsa_sha512
651 #endif
652 };
653
654 #ifndef OPENSSL_NO_EC
655 static const uint16_t suiteb_sigalgs[] = {
656 TLSEXT_SIGALG_ecdsa_secp256r1_sha256,
657 TLSEXT_SIGALG_ecdsa_secp384r1_sha384
658 };
659 #endif
660
661 static const SIGALG_LOOKUP sigalg_lookup_tbl[] = {
662 #ifndef OPENSSL_NO_EC
663 {"ecdsa_secp256r1_sha256", TLSEXT_SIGALG_ecdsa_secp256r1_sha256,
664 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
665 NID_ecdsa_with_SHA256, NID_X9_62_prime256v1},
666 {"ecdsa_secp384r1_sha384", TLSEXT_SIGALG_ecdsa_secp384r1_sha384,
667 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
668 NID_ecdsa_with_SHA384, NID_secp384r1},
669 {"ecdsa_secp521r1_sha512", TLSEXT_SIGALG_ecdsa_secp521r1_sha512,
670 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
671 NID_ecdsa_with_SHA512, NID_secp521r1},
672 {"ed25519", TLSEXT_SIGALG_ed25519,
673 NID_undef, -1, EVP_PKEY_ED25519, SSL_PKEY_ED25519,
674 NID_undef, NID_undef},
675 {NULL, TLSEXT_SIGALG_ecdsa_sha224,
676 NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
677 NID_ecdsa_with_SHA224, NID_undef},
678 {NULL, TLSEXT_SIGALG_ecdsa_sha1,
679 NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_EC, SSL_PKEY_ECC,
680 NID_ecdsa_with_SHA1, NID_undef},
681 #endif
682 {"rsa_pss_rsae_sha256", TLSEXT_SIGALG_rsa_pss_rsae_sha256,
683 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA,
684 NID_undef, NID_undef},
685 {"rsa_pss_rsae_sha384", TLSEXT_SIGALG_rsa_pss_rsae_sha384,
686 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA,
687 NID_undef, NID_undef},
688 {"rsa_pss_rsae_sha512", TLSEXT_SIGALG_rsa_pss_rsae_sha512,
689 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA,
690 NID_undef, NID_undef},
691 {"rsa_pss_pss_sha256", TLSEXT_SIGALG_rsa_pss_pss_sha256,
692 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,
693 NID_undef, NID_undef},
694 {"rsa_pss_pss_sha384", TLSEXT_SIGALG_rsa_pss_pss_sha384,
695 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,
696 NID_undef, NID_undef},
697 {"rsa_pss_pss_sha512", TLSEXT_SIGALG_rsa_pss_pss_sha512,
698 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA_PSS, SSL_PKEY_RSA_PSS_SIGN,
699 NID_undef, NID_undef},
700 {"rsa_pkcs1_sha256", TLSEXT_SIGALG_rsa_pkcs1_sha256,
701 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
702 NID_sha256WithRSAEncryption, NID_undef},
703 {"rsa_pkcs1_sha384", TLSEXT_SIGALG_rsa_pkcs1_sha384,
704 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
705 NID_sha384WithRSAEncryption, NID_undef},
706 {"rsa_pkcs1_sha512", TLSEXT_SIGALG_rsa_pkcs1_sha512,
707 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
708 NID_sha512WithRSAEncryption, NID_undef},
709 {"rsa_pkcs1_sha224", TLSEXT_SIGALG_rsa_pkcs1_sha224,
710 NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
711 NID_sha224WithRSAEncryption, NID_undef},
712 {"rsa_pkcs1_sha1", TLSEXT_SIGALG_rsa_pkcs1_sha1,
713 NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_RSA, SSL_PKEY_RSA,
714 NID_sha1WithRSAEncryption, NID_undef},
715 #ifndef OPENSSL_NO_DSA
716 {NULL, TLSEXT_SIGALG_dsa_sha256,
717 NID_sha256, SSL_MD_SHA256_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
718 NID_dsa_with_SHA256, NID_undef},
719 {NULL, TLSEXT_SIGALG_dsa_sha384,
720 NID_sha384, SSL_MD_SHA384_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
721 NID_undef, NID_undef},
722 {NULL, TLSEXT_SIGALG_dsa_sha512,
723 NID_sha512, SSL_MD_SHA512_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
724 NID_undef, NID_undef},
725 {NULL, TLSEXT_SIGALG_dsa_sha224,
726 NID_sha224, SSL_MD_SHA224_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
727 NID_undef, NID_undef},
728 {NULL, TLSEXT_SIGALG_dsa_sha1,
729 NID_sha1, SSL_MD_SHA1_IDX, EVP_PKEY_DSA, SSL_PKEY_DSA_SIGN,
730 NID_dsaWithSHA1, NID_undef},
731 #endif
732 #ifndef OPENSSL_NO_GOST
733 {NULL, TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256,
734 NID_id_GostR3411_2012_256, SSL_MD_GOST12_256_IDX,
735 NID_id_GostR3410_2012_256, SSL_PKEY_GOST12_256,
736 NID_undef, NID_undef},
737 {NULL, TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512,
738 NID_id_GostR3411_2012_512, SSL_MD_GOST12_512_IDX,
739 NID_id_GostR3410_2012_512, SSL_PKEY_GOST12_512,
740 NID_undef, NID_undef},
741 {NULL, TLSEXT_SIGALG_gostr34102001_gostr3411,
742 NID_id_GostR3411_94, SSL_MD_GOST94_IDX,
743 NID_id_GostR3410_2001, SSL_PKEY_GOST01,
744 NID_undef, NID_undef}
745 #endif
746 };
747 /* Legacy sigalgs for TLS < 1.2 RSA TLS signatures */
748 static const SIGALG_LOOKUP legacy_rsa_sigalg = {
749 "rsa_pkcs1_md5_sha1", 0,
750 NID_md5_sha1, SSL_MD_MD5_SHA1_IDX,
751 EVP_PKEY_RSA, SSL_PKEY_RSA,
752 NID_undef, NID_undef
753 };
754
755 /*
756 * Default signature algorithm values used if signature algorithms not present.
757 * From RFC5246. Note: order must match certificate index order.
758 */
759 static const uint16_t tls_default_sigalg[] = {
760 TLSEXT_SIGALG_rsa_pkcs1_sha1, /* SSL_PKEY_RSA */
761 0, /* SSL_PKEY_RSA_PSS_SIGN */
762 TLSEXT_SIGALG_dsa_sha1, /* SSL_PKEY_DSA_SIGN */
763 TLSEXT_SIGALG_ecdsa_sha1, /* SSL_PKEY_ECC */
764 TLSEXT_SIGALG_gostr34102001_gostr3411, /* SSL_PKEY_GOST01 */
765 TLSEXT_SIGALG_gostr34102012_256_gostr34112012_256, /* SSL_PKEY_GOST12_256 */
766 TLSEXT_SIGALG_gostr34102012_512_gostr34112012_512, /* SSL_PKEY_GOST12_512 */
767 0 /* SSL_PKEY_ED25519 */
768 };
769
770 /* Lookup TLS signature algorithm */
771 static const SIGALG_LOOKUP *tls1_lookup_sigalg(uint16_t sigalg)
772 {
773 size_t i;
774 const SIGALG_LOOKUP *s;
775
776 for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl);
777 i++, s++) {
778 if (s->sigalg == sigalg)
779 return s;
780 }
781 return NULL;
782 }
783 /* Lookup hash: return 0 if invalid or not enabled */
784 int tls1_lookup_md(const SIGALG_LOOKUP *lu, const EVP_MD **pmd)
785 {
786 const EVP_MD *md;
787 if (lu == NULL)
788 return 0;
789 /* lu->hash == NID_undef means no associated digest */
790 if (lu->hash == NID_undef) {
791 md = NULL;
792 } else {
793 md = ssl_md(lu->hash_idx);
794 if (md == NULL)
795 return 0;
796 }
797 if (pmd)
798 *pmd = md;
799 return 1;
800 }
801
802 /*
803 * Check if key is large enough to generate RSA-PSS signature.
804 *
805 * The key must greater than or equal to 2 * hash length + 2.
806 * SHA512 has a hash length of 64 bytes, which is incompatible
807 * with a 128 byte (1024 bit) key.
808 */
809 #define RSA_PSS_MINIMUM_KEY_SIZE(md) (2 * EVP_MD_size(md) + 2)
810 static int rsa_pss_check_min_key_size(const RSA *rsa, const SIGALG_LOOKUP *lu)
811 {
812 const EVP_MD *md;
813
814 if (rsa == NULL)
815 return 0;
816 if (!tls1_lookup_md(lu, &md) || md == NULL)
817 return 0;
818 if (RSA_size(rsa) < RSA_PSS_MINIMUM_KEY_SIZE(md))
819 return 0;
820 return 1;
821 }
822
823 /*
824 * Return a signature algorithm for TLS < 1.2 where the signature type
825 * is fixed by the certificate type.
826 */
827 static const SIGALG_LOOKUP *tls1_get_legacy_sigalg(const SSL *s, int idx)
828 {
829 if (idx == -1) {
830 if (s->server) {
831 size_t i;
832
833 /* Work out index corresponding to ciphersuite */
834 for (i = 0; i < SSL_PKEY_NUM; i++) {
835 const SSL_CERT_LOOKUP *clu = ssl_cert_lookup_by_idx(i);
836
837 if (clu->amask & s->s3->tmp.new_cipher->algorithm_auth) {
838 idx = i;
839 break;
840 }
841 }
842 } else {
843 idx = s->cert->key - s->cert->pkeys;
844 }
845 }
846 if (idx < 0 || idx >= (int)OSSL_NELEM(tls_default_sigalg))
847 return NULL;
848 if (SSL_USE_SIGALGS(s) || idx != SSL_PKEY_RSA) {
849 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(tls_default_sigalg[idx]);
850
851 if (!tls1_lookup_md(lu, NULL))
852 return NULL;
853 return lu;
854 }
855 return &legacy_rsa_sigalg;
856 }
857 /* Set peer sigalg based key type */
858 int tls1_set_peer_legacy_sigalg(SSL *s, const EVP_PKEY *pkey)
859 {
860 size_t idx;
861 const SIGALG_LOOKUP *lu;
862
863 if (ssl_cert_lookup_by_pkey(pkey, &idx) == NULL)
864 return 0;
865 lu = tls1_get_legacy_sigalg(s, idx);
866 if (lu == NULL)
867 return 0;
868 s->s3->tmp.peer_sigalg = lu;
869 return 1;
870 }
871
872 size_t tls12_get_psigalgs(SSL *s, int sent, const uint16_t **psigs)
873 {
874 /*
875 * If Suite B mode use Suite B sigalgs only, ignore any other
876 * preferences.
877 */
878 #ifndef OPENSSL_NO_EC
879 switch (tls1_suiteb(s)) {
880 case SSL_CERT_FLAG_SUITEB_128_LOS:
881 *psigs = suiteb_sigalgs;
882 return OSSL_NELEM(suiteb_sigalgs);
883
884 case SSL_CERT_FLAG_SUITEB_128_LOS_ONLY:
885 *psigs = suiteb_sigalgs;
886 return 1;
887
888 case SSL_CERT_FLAG_SUITEB_192_LOS:
889 *psigs = suiteb_sigalgs + 1;
890 return 1;
891 }
892 #endif
893 /*
894 * We use client_sigalgs (if not NULL) if we're a server
895 * and sending a certificate request or if we're a client and
896 * determining which shared algorithm to use.
897 */
898 if ((s->server == sent) && s->cert->client_sigalgs != NULL) {
899 *psigs = s->cert->client_sigalgs;
900 return s->cert->client_sigalgslen;
901 } else if (s->cert->conf_sigalgs) {
902 *psigs = s->cert->conf_sigalgs;
903 return s->cert->conf_sigalgslen;
904 } else {
905 *psigs = tls12_sigalgs;
906 return OSSL_NELEM(tls12_sigalgs);
907 }
908 }
909
910 /*
911 * Check signature algorithm is consistent with sent supported signature
912 * algorithms and if so set relevant digest and signature scheme in
913 * s.
914 */
915 int tls12_check_peer_sigalg(SSL *s, uint16_t sig, EVP_PKEY *pkey)
916 {
917 const uint16_t *sent_sigs;
918 const EVP_MD *md = NULL;
919 char sigalgstr[2];
920 size_t sent_sigslen, i;
921 int pkeyid = EVP_PKEY_id(pkey);
922 const SIGALG_LOOKUP *lu;
923
924 /* Should never happen */
925 if (pkeyid == -1)
926 return -1;
927 if (SSL_IS_TLS13(s)) {
928 /* Disallow DSA for TLS 1.3 */
929 if (pkeyid == EVP_PKEY_DSA) {
930 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS12_CHECK_PEER_SIGALG,
931 SSL_R_WRONG_SIGNATURE_TYPE);
932 return 0;
933 }
934 /* Only allow PSS for TLS 1.3 */
935 if (pkeyid == EVP_PKEY_RSA)
936 pkeyid = EVP_PKEY_RSA_PSS;
937 }
938 lu = tls1_lookup_sigalg(sig);
939 /*
940 * Check sigalgs is known. Disallow SHA1/SHA224 with TLS 1.3. Check key type
941 * is consistent with signature: RSA keys can be used for RSA-PSS
942 */
943 if (lu == NULL
944 || (SSL_IS_TLS13(s) && (lu->hash == NID_sha1 || lu->hash == NID_sha224))
945 || (pkeyid != lu->sig
946 && (lu->sig != EVP_PKEY_RSA_PSS || pkeyid != EVP_PKEY_RSA))) {
947 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS12_CHECK_PEER_SIGALG,
948 SSL_R_WRONG_SIGNATURE_TYPE);
949 return 0;
950 }
951 #ifndef OPENSSL_NO_EC
952 if (pkeyid == EVP_PKEY_EC) {
953
954 /* Check point compression is permitted */
955 if (!tls1_check_pkey_comp(s, pkey)) {
956 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
957 SSL_F_TLS12_CHECK_PEER_SIGALG,
958 SSL_R_ILLEGAL_POINT_COMPRESSION);
959 return 0;
960 }
961
962 /* For TLS 1.3 or Suite B check curve matches signature algorithm */
963 if (SSL_IS_TLS13(s) || tls1_suiteb(s)) {
964 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
965 int curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
966
967 if (lu->curve != NID_undef && curve != lu->curve) {
968 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
969 SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
970 return 0;
971 }
972 }
973 if (!SSL_IS_TLS13(s)) {
974 /* Check curve matches extensions */
975 if (!tls1_check_group_id(s, tls1_get_group_id(pkey))) {
976 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
977 SSL_F_TLS12_CHECK_PEER_SIGALG, SSL_R_WRONG_CURVE);
978 return 0;
979 }
980 if (tls1_suiteb(s)) {
981 /* Check sigalg matches a permissible Suite B value */
982 if (sig != TLSEXT_SIGALG_ecdsa_secp256r1_sha256
983 && sig != TLSEXT_SIGALG_ecdsa_secp384r1_sha384) {
984 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
985 SSL_F_TLS12_CHECK_PEER_SIGALG,
986 SSL_R_WRONG_SIGNATURE_TYPE);
987 return 0;
988 }
989 }
990 }
991 } else if (tls1_suiteb(s)) {
992 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS12_CHECK_PEER_SIGALG,
993 SSL_R_WRONG_SIGNATURE_TYPE);
994 return 0;
995 }
996 #endif
997
998 /* Check signature matches a type we sent */
999 sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);
1000 for (i = 0; i < sent_sigslen; i++, sent_sigs++) {
1001 if (sig == *sent_sigs)
1002 break;
1003 }
1004 /* Allow fallback to SHA1 if not strict mode */
1005 if (i == sent_sigslen && (lu->hash != NID_sha1
1006 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) {
1007 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS12_CHECK_PEER_SIGALG,
1008 SSL_R_WRONG_SIGNATURE_TYPE);
1009 return 0;
1010 }
1011 if (!tls1_lookup_md(lu, &md)) {
1012 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS12_CHECK_PEER_SIGALG,
1013 SSL_R_UNKNOWN_DIGEST);
1014 return 0;
1015 }
1016 if (md != NULL) {
1017 /*
1018 * Make sure security callback allows algorithm. For historical
1019 * reasons we have to pass the sigalg as a two byte char array.
1020 */
1021 sigalgstr[0] = (sig >> 8) & 0xff;
1022 sigalgstr[1] = sig & 0xff;
1023 if (!ssl_security(s, SSL_SECOP_SIGALG_CHECK,
1024 EVP_MD_size(md) * 4, EVP_MD_type(md),
1025 (void *)sigalgstr)) {
1026 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS12_CHECK_PEER_SIGALG,
1027 SSL_R_WRONG_SIGNATURE_TYPE);
1028 return 0;
1029 }
1030 }
1031 /* Store the sigalg the peer uses */
1032 s->s3->tmp.peer_sigalg = lu;
1033 return 1;
1034 }
1035
1036 int SSL_get_peer_signature_type_nid(const SSL *s, int *pnid)
1037 {
1038 if (s->s3->tmp.peer_sigalg == NULL)
1039 return 0;
1040 *pnid = s->s3->tmp.peer_sigalg->sig;
1041 return 1;
1042 }
1043
1044 /*
1045 * Set a mask of disabled algorithms: an algorithm is disabled if it isn't
1046 * supported, doesn't appear in supported signature algorithms, isn't supported
1047 * by the enabled protocol versions or by the security level.
1048 *
1049 * This function should only be used for checking which ciphers are supported
1050 * by the client.
1051 *
1052 * Call ssl_cipher_disabled() to check that it's enabled or not.
1053 */
1054 void ssl_set_client_disabled(SSL *s)
1055 {
1056 s->s3->tmp.mask_a = 0;
1057 s->s3->tmp.mask_k = 0;
1058 ssl_set_sig_mask(&s->s3->tmp.mask_a, s, SSL_SECOP_SIGALG_MASK);
1059 ssl_get_min_max_version(s, &s->s3->tmp.min_ver, &s->s3->tmp.max_ver);
1060 #ifndef OPENSSL_NO_PSK
1061 /* with PSK there must be client callback set */
1062 if (!s->psk_client_callback) {
1063 s->s3->tmp.mask_a |= SSL_aPSK;
1064 s->s3->tmp.mask_k |= SSL_PSK;
1065 }
1066 #endif /* OPENSSL_NO_PSK */
1067 #ifndef OPENSSL_NO_SRP
1068 if (!(s->srp_ctx.srp_Mask & SSL_kSRP)) {
1069 s->s3->tmp.mask_a |= SSL_aSRP;
1070 s->s3->tmp.mask_k |= SSL_kSRP;
1071 }
1072 #endif
1073 }
1074
1075 /*
1076 * ssl_cipher_disabled - check that a cipher is disabled or not
1077 * @s: SSL connection that you want to use the cipher on
1078 * @c: cipher to check
1079 * @op: Security check that you want to do
1080 * @ecdhe: If set to 1 then TLSv1 ECDHE ciphers are also allowed in SSLv3
1081 *
1082 * Returns 1 when it's disabled, 0 when enabled.
1083 */
1084 int ssl_cipher_disabled(SSL *s, const SSL_CIPHER *c, int op, int ecdhe)
1085 {
1086 if (c->algorithm_mkey & s->s3->tmp.mask_k
1087 || c->algorithm_auth & s->s3->tmp.mask_a)
1088 return 1;
1089 if (s->s3->tmp.max_ver == 0)
1090 return 1;
1091 if (!SSL_IS_DTLS(s)) {
1092 int min_tls = c->min_tls;
1093
1094 /*
1095 * For historical reasons we will allow ECHDE to be selected by a server
1096 * in SSLv3 if we are a client
1097 */
1098 if (min_tls == TLS1_VERSION && ecdhe
1099 && (c->algorithm_mkey & (SSL_kECDHE | SSL_kECDHEPSK)) != 0)
1100 min_tls = SSL3_VERSION;
1101
1102 if ((min_tls > s->s3->tmp.max_ver) || (c->max_tls < s->s3->tmp.min_ver))
1103 return 1;
1104 }
1105 if (SSL_IS_DTLS(s) && (DTLS_VERSION_GT(c->min_dtls, s->s3->tmp.max_ver)
1106 || DTLS_VERSION_LT(c->max_dtls, s->s3->tmp.min_ver)))
1107 return 1;
1108
1109 return !ssl_security(s, op, c->strength_bits, 0, (void *)c);
1110 }
1111
1112 int tls_use_ticket(SSL *s)
1113 {
1114 if ((s->options & SSL_OP_NO_TICKET))
1115 return 0;
1116 return ssl_security(s, SSL_SECOP_TICKET, 0, 0, NULL);
1117 }
1118
1119 int tls1_set_server_sigalgs(SSL *s)
1120 {
1121 size_t i;
1122
1123 /* Clear any shared signature algorithms */
1124 OPENSSL_free(s->cert->shared_sigalgs);
1125 s->cert->shared_sigalgs = NULL;
1126 s->cert->shared_sigalgslen = 0;
1127 /* Clear certificate validity flags */
1128 for (i = 0; i < SSL_PKEY_NUM; i++)
1129 s->s3->tmp.valid_flags[i] = 0;
1130 /*
1131 * If peer sent no signature algorithms check to see if we support
1132 * the default algorithm for each certificate type
1133 */
1134 if (s->s3->tmp.peer_cert_sigalgs == NULL
1135 && s->s3->tmp.peer_sigalgs == NULL) {
1136 const uint16_t *sent_sigs;
1137 size_t sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);
1138
1139 for (i = 0; i < SSL_PKEY_NUM; i++) {
1140 const SIGALG_LOOKUP *lu = tls1_get_legacy_sigalg(s, i);
1141 size_t j;
1142
1143 if (lu == NULL)
1144 continue;
1145 /* Check default matches a type we sent */
1146 for (j = 0; j < sent_sigslen; j++) {
1147 if (lu->sigalg == sent_sigs[j]) {
1148 s->s3->tmp.valid_flags[i] = CERT_PKEY_SIGN;
1149 break;
1150 }
1151 }
1152 }
1153 return 1;
1154 }
1155
1156 if (!tls1_process_sigalgs(s)) {
1157 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
1158 SSL_F_TLS1_SET_SERVER_SIGALGS, ERR_R_INTERNAL_ERROR);
1159 return 0;
1160 }
1161 if (s->cert->shared_sigalgs != NULL)
1162 return 1;
1163
1164 /* Fatal error if no shared signature algorithms */
1165 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS1_SET_SERVER_SIGALGS,
1166 SSL_R_NO_SHARED_SIGNATURE_ALGORITHMS);
1167 return 0;
1168 }
1169
1170 /*-
1171 * Gets the ticket information supplied by the client if any.
1172 *
1173 * hello: The parsed ClientHello data
1174 * ret: (output) on return, if a ticket was decrypted, then this is set to
1175 * point to the resulting session.
1176 *
1177 * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
1178 * ciphersuite, in which case we have no use for session tickets and one will
1179 * never be decrypted, nor will s->ext.ticket_expected be set to 1.
1180 *
1181 * Returns:
1182 * -1: fatal error, either from parsing or decrypting the ticket.
1183 * 0: no ticket was found (or was ignored, based on settings).
1184 * 1: a zero length extension was found, indicating that the client supports
1185 * session tickets but doesn't currently have one to offer.
1186 * 2: either s->tls_session_secret_cb was set, or a ticket was offered but
1187 * couldn't be decrypted because of a non-fatal error.
1188 * 3: a ticket was successfully decrypted and *ret was set.
1189 *
1190 * Side effects:
1191 * Sets s->ext.ticket_expected to 1 if the server will have to issue
1192 * a new session ticket to the client because the client indicated support
1193 * (and s->tls_session_secret_cb is NULL) but the client either doesn't have
1194 * a session ticket or we couldn't use the one it gave us, or if
1195 * s->ctx->ext.ticket_key_cb asked to renew the client's ticket.
1196 * Otherwise, s->ext.ticket_expected is set to 0.
1197 */
1198 TICKET_RETURN tls_get_ticket_from_client(SSL *s, CLIENTHELLO_MSG *hello,
1199 SSL_SESSION **ret)
1200 {
1201 int retv;
1202 size_t size;
1203 RAW_EXTENSION *ticketext;
1204
1205 *ret = NULL;
1206 s->ext.ticket_expected = 0;
1207
1208 /*
1209 * If tickets disabled or not supported by the protocol version
1210 * (e.g. TLSv1.3) behave as if no ticket present to permit stateful
1211 * resumption.
1212 */
1213 if (s->version <= SSL3_VERSION || !tls_use_ticket(s))
1214 return TICKET_NONE;
1215
1216 ticketext = &hello->pre_proc_exts[TLSEXT_IDX_session_ticket];
1217 if (!ticketext->present)
1218 return TICKET_NONE;
1219
1220 size = PACKET_remaining(&ticketext->data);
1221 if (size == 0) {
1222 /*
1223 * The client will accept a ticket but doesn't currently have
1224 * one.
1225 */
1226 s->ext.ticket_expected = 1;
1227 return TICKET_EMPTY;
1228 }
1229 if (s->ext.session_secret_cb) {
1230 /*
1231 * Indicate that the ticket couldn't be decrypted rather than
1232 * generating the session from ticket now, trigger
1233 * abbreviated handshake based on external mechanism to
1234 * calculate the master secret later.
1235 */
1236 return TICKET_NO_DECRYPT;
1237 }
1238
1239 retv = tls_decrypt_ticket(s, PACKET_data(&ticketext->data), size,
1240 hello->session_id, hello->session_id_len, ret);
1241 switch (retv) {
1242 case TICKET_NO_DECRYPT:
1243 s->ext.ticket_expected = 1;
1244 return TICKET_NO_DECRYPT;
1245
1246 case TICKET_SUCCESS:
1247 return TICKET_SUCCESS;
1248
1249 case TICKET_SUCCESS_RENEW:
1250 s->ext.ticket_expected = 1;
1251 return TICKET_SUCCESS;
1252
1253 default:
1254 return TICKET_FATAL_ERR_OTHER;
1255 }
1256 }
1257
1258 /*-
1259 * tls_decrypt_ticket attempts to decrypt a session ticket.
1260 *
1261 * etick: points to the body of the session ticket extension.
1262 * eticklen: the length of the session tickets extension.
1263 * sess_id: points at the session ID.
1264 * sesslen: the length of the session ID.
1265 * psess: (output) on return, if a ticket was decrypted, then this is set to
1266 * point to the resulting session.
1267 */
1268 TICKET_RETURN tls_decrypt_ticket(SSL *s, const unsigned char *etick,
1269 size_t eticklen, const unsigned char *sess_id,
1270 size_t sesslen, SSL_SESSION **psess)
1271 {
1272 SSL_SESSION *sess;
1273 unsigned char *sdec;
1274 const unsigned char *p;
1275 int slen, renew_ticket = 0, declen;
1276 TICKET_RETURN ret = TICKET_FATAL_ERR_OTHER;
1277 size_t mlen;
1278 unsigned char tick_hmac[EVP_MAX_MD_SIZE];
1279 HMAC_CTX *hctx = NULL;
1280 EVP_CIPHER_CTX *ctx;
1281 SSL_CTX *tctx = s->session_ctx;
1282
1283 /* Initialize session ticket encryption and HMAC contexts */
1284 hctx = HMAC_CTX_new();
1285 if (hctx == NULL)
1286 return TICKET_FATAL_ERR_MALLOC;
1287 ctx = EVP_CIPHER_CTX_new();
1288 if (ctx == NULL) {
1289 ret = TICKET_FATAL_ERR_MALLOC;
1290 goto err;
1291 }
1292 if (tctx->ext.ticket_key_cb) {
1293 unsigned char *nctick = (unsigned char *)etick;
1294 int rv = tctx->ext.ticket_key_cb(s, nctick, nctick + 16,
1295 ctx, hctx, 0);
1296 if (rv < 0)
1297 goto err;
1298 if (rv == 0) {
1299 ret = TICKET_NO_DECRYPT;
1300 goto err;
1301 }
1302 if (rv == 2)
1303 renew_ticket = 1;
1304 } else {
1305 /* Check key name matches */
1306 if (memcmp(etick, tctx->ext.tick_key_name,
1307 sizeof(tctx->ext.tick_key_name)) != 0) {
1308 ret = TICKET_NO_DECRYPT;
1309 goto err;
1310 }
1311 if (HMAC_Init_ex(hctx, tctx->ext.tick_hmac_key,
1312 sizeof(tctx->ext.tick_hmac_key),
1313 EVP_sha256(), NULL) <= 0
1314 || EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL,
1315 tctx->ext.tick_aes_key,
1316 etick
1317 + sizeof(tctx->ext.tick_key_name)) <= 0) {
1318 goto err;
1319 }
1320 }
1321 /*
1322 * Attempt to process session ticket, first conduct sanity and integrity
1323 * checks on ticket.
1324 */
1325 mlen = HMAC_size(hctx);
1326 if (mlen == 0) {
1327 goto err;
1328 }
1329 /* Sanity check ticket length: must exceed keyname + IV + HMAC */
1330 if (eticklen <=
1331 TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx) + mlen) {
1332 ret = TICKET_NO_DECRYPT;
1333 goto err;
1334 }
1335 eticklen -= mlen;
1336 /* Check HMAC of encrypted ticket */
1337 if (HMAC_Update(hctx, etick, eticklen) <= 0
1338 || HMAC_Final(hctx, tick_hmac, NULL) <= 0) {
1339 goto err;
1340 }
1341 HMAC_CTX_free(hctx);
1342 if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
1343 EVP_CIPHER_CTX_free(ctx);
1344 return TICKET_NO_DECRYPT;
1345 }
1346 /* Attempt to decrypt session data */
1347 /* Move p after IV to start of encrypted ticket, update length */
1348 p = etick + TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx);
1349 eticklen -= TLSEXT_KEYNAME_LENGTH + EVP_CIPHER_CTX_iv_length(ctx);
1350 sdec = OPENSSL_malloc(eticklen);
1351 if (sdec == NULL || EVP_DecryptUpdate(ctx, sdec, &slen, p,
1352 (int)eticklen) <= 0) {
1353 EVP_CIPHER_CTX_free(ctx);
1354 OPENSSL_free(sdec);
1355 return TICKET_FATAL_ERR_OTHER;
1356 }
1357 if (EVP_DecryptFinal(ctx, sdec + slen, &declen) <= 0) {
1358 EVP_CIPHER_CTX_free(ctx);
1359 OPENSSL_free(sdec);
1360 return TICKET_NO_DECRYPT;
1361 }
1362 slen += declen;
1363 EVP_CIPHER_CTX_free(ctx);
1364 ctx = NULL;
1365 p = sdec;
1366
1367 sess = d2i_SSL_SESSION(NULL, &p, slen);
1368 slen -= p - sdec;
1369 OPENSSL_free(sdec);
1370 if (sess) {
1371 /* Some additional consistency checks */
1372 if (slen != 0 || sess->session_id_length != 0) {
1373 SSL_SESSION_free(sess);
1374 return TICKET_NO_DECRYPT;
1375 }
1376 /*
1377 * The session ID, if non-empty, is used by some clients to detect
1378 * that the ticket has been accepted. So we copy it to the session
1379 * structure. If it is empty set length to zero as required by
1380 * standard.
1381 */
1382 if (sesslen)
1383 memcpy(sess->session_id, sess_id, sesslen);
1384 sess->session_id_length = sesslen;
1385 *psess = sess;
1386 if (renew_ticket)
1387 return TICKET_SUCCESS_RENEW;
1388 else
1389 return TICKET_SUCCESS;
1390 }
1391 ERR_clear_error();
1392 /*
1393 * For session parse failure, indicate that we need to send a new ticket.
1394 */
1395 return TICKET_NO_DECRYPT;
1396 err:
1397 EVP_CIPHER_CTX_free(ctx);
1398 HMAC_CTX_free(hctx);
1399 return ret;
1400 }
1401
1402 /* Check to see if a signature algorithm is allowed */
1403 static int tls12_sigalg_allowed(SSL *s, int op, const SIGALG_LOOKUP *lu)
1404 {
1405 unsigned char sigalgstr[2];
1406 int secbits;
1407
1408 /* See if sigalgs is recognised and if hash is enabled */
1409 if (!tls1_lookup_md(lu, NULL))
1410 return 0;
1411 /* DSA is not allowed in TLS 1.3 */
1412 if (SSL_IS_TLS13(s) && lu->sig == EVP_PKEY_DSA)
1413 return 0;
1414 /* TODO(OpenSSL1.2) fully axe DSA/etc. in ClientHello per TLS 1.3 spec */
1415 if (!s->server && !SSL_IS_DTLS(s) && s->s3->tmp.min_ver >= TLS1_3_VERSION
1416 && (lu->sig == EVP_PKEY_DSA || lu->hash_idx == SSL_MD_SHA1_IDX
1417 || lu->hash_idx == SSL_MD_MD5_IDX
1418 || lu->hash_idx == SSL_MD_SHA224_IDX))
1419 return 0;
1420 /* See if public key algorithm allowed */
1421 if (ssl_cert_is_disabled(lu->sig_idx))
1422 return 0;
1423 if (lu->hash == NID_undef)
1424 return 1;
1425 /* Security bits: half digest bits */
1426 secbits = EVP_MD_size(ssl_md(lu->hash_idx)) * 4;
1427 /* Finally see if security callback allows it */
1428 sigalgstr[0] = (lu->sigalg >> 8) & 0xff;
1429 sigalgstr[1] = lu->sigalg & 0xff;
1430 return ssl_security(s, op, secbits, lu->hash, (void *)sigalgstr);
1431 }
1432
1433 /*
1434 * Get a mask of disabled public key algorithms based on supported signature
1435 * algorithms. For example if no signature algorithm supports RSA then RSA is
1436 * disabled.
1437 */
1438
1439 void ssl_set_sig_mask(uint32_t *pmask_a, SSL *s, int op)
1440 {
1441 const uint16_t *sigalgs;
1442 size_t i, sigalgslen;
1443 uint32_t disabled_mask = SSL_aRSA | SSL_aDSS | SSL_aECDSA;
1444 /*
1445 * Go through all signature algorithms seeing if we support any
1446 * in disabled_mask.
1447 */
1448 sigalgslen = tls12_get_psigalgs(s, 1, &sigalgs);
1449 for (i = 0; i < sigalgslen; i++, sigalgs++) {
1450 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*sigalgs);
1451 const SSL_CERT_LOOKUP *clu;
1452
1453 if (lu == NULL)
1454 continue;
1455
1456 clu = ssl_cert_lookup_by_idx(lu->sig_idx);
1457 if (clu == NULL)
1458 continue;
1459
1460 /* If algorithm is disabled see if we can enable it */
1461 if ((clu->amask & disabled_mask) != 0
1462 && tls12_sigalg_allowed(s, op, lu))
1463 disabled_mask &= ~clu->amask;
1464 }
1465 *pmask_a |= disabled_mask;
1466 }
1467
1468 int tls12_copy_sigalgs(SSL *s, WPACKET *pkt,
1469 const uint16_t *psig, size_t psiglen)
1470 {
1471 size_t i;
1472 int rv = 0;
1473
1474 for (i = 0; i < psiglen; i++, psig++) {
1475 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*psig);
1476
1477 if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SUPPORTED, lu))
1478 continue;
1479 if (!WPACKET_put_bytes_u16(pkt, *psig))
1480 return 0;
1481 /*
1482 * If TLS 1.3 must have at least one valid TLS 1.3 message
1483 * signing algorithm: i.e. neither RSA nor SHA1/SHA224
1484 */
1485 if (rv == 0 && (!SSL_IS_TLS13(s)
1486 || (lu->sig != EVP_PKEY_RSA
1487 && lu->hash != NID_sha1
1488 && lu->hash != NID_sha224)))
1489 rv = 1;
1490 }
1491 if (rv == 0)
1492 SSLerr(SSL_F_TLS12_COPY_SIGALGS, SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
1493 return rv;
1494 }
1495
1496 /* Given preference and allowed sigalgs set shared sigalgs */
1497 static size_t tls12_shared_sigalgs(SSL *s, const SIGALG_LOOKUP **shsig,
1498 const uint16_t *pref, size_t preflen,
1499 const uint16_t *allow, size_t allowlen)
1500 {
1501 const uint16_t *ptmp, *atmp;
1502 size_t i, j, nmatch = 0;
1503 for (i = 0, ptmp = pref; i < preflen; i++, ptmp++) {
1504 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*ptmp);
1505
1506 /* Skip disabled hashes or signature algorithms */
1507 if (!tls12_sigalg_allowed(s, SSL_SECOP_SIGALG_SHARED, lu))
1508 continue;
1509 for (j = 0, atmp = allow; j < allowlen; j++, atmp++) {
1510 if (*ptmp == *atmp) {
1511 nmatch++;
1512 if (shsig)
1513 *shsig++ = lu;
1514 break;
1515 }
1516 }
1517 }
1518 return nmatch;
1519 }
1520
1521 /* Set shared signature algorithms for SSL structures */
1522 static int tls1_set_shared_sigalgs(SSL *s)
1523 {
1524 const uint16_t *pref, *allow, *conf;
1525 size_t preflen, allowlen, conflen;
1526 size_t nmatch;
1527 const SIGALG_LOOKUP **salgs = NULL;
1528 CERT *c = s->cert;
1529 unsigned int is_suiteb = tls1_suiteb(s);
1530
1531 OPENSSL_free(c->shared_sigalgs);
1532 c->shared_sigalgs = NULL;
1533 c->shared_sigalgslen = 0;
1534 /* If client use client signature algorithms if not NULL */
1535 if (!s->server && c->client_sigalgs && !is_suiteb) {
1536 conf = c->client_sigalgs;
1537 conflen = c->client_sigalgslen;
1538 } else if (c->conf_sigalgs && !is_suiteb) {
1539 conf = c->conf_sigalgs;
1540 conflen = c->conf_sigalgslen;
1541 } else
1542 conflen = tls12_get_psigalgs(s, 0, &conf);
1543 if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE || is_suiteb) {
1544 pref = conf;
1545 preflen = conflen;
1546 allow = s->s3->tmp.peer_sigalgs;
1547 allowlen = s->s3->tmp.peer_sigalgslen;
1548 } else {
1549 allow = conf;
1550 allowlen = conflen;
1551 pref = s->s3->tmp.peer_sigalgs;
1552 preflen = s->s3->tmp.peer_sigalgslen;
1553 }
1554 nmatch = tls12_shared_sigalgs(s, NULL, pref, preflen, allow, allowlen);
1555 if (nmatch) {
1556 salgs = OPENSSL_malloc(nmatch * sizeof(*salgs));
1557 if (salgs == NULL)
1558 return 0;
1559 nmatch = tls12_shared_sigalgs(s, salgs, pref, preflen, allow, allowlen);
1560 } else {
1561 salgs = NULL;
1562 }
1563 c->shared_sigalgs = salgs;
1564 c->shared_sigalgslen = nmatch;
1565 return 1;
1566 }
1567
1568 int tls1_save_u16(PACKET *pkt, uint16_t **pdest, size_t *pdestlen)
1569 {
1570 unsigned int stmp;
1571 size_t size, i;
1572 uint16_t *buf;
1573
1574 size = PACKET_remaining(pkt);
1575
1576 /* Invalid data length */
1577 if (size == 0 || (size & 1) != 0)
1578 return 0;
1579
1580 size >>= 1;
1581
1582 buf = OPENSSL_malloc(size * sizeof(*buf));
1583 if (buf == NULL)
1584 return 0;
1585 for (i = 0; i < size && PACKET_get_net_2(pkt, &stmp); i++)
1586 buf[i] = stmp;
1587
1588 if (i != size) {
1589 OPENSSL_free(buf);
1590 return 0;
1591 }
1592
1593 OPENSSL_free(*pdest);
1594 *pdest = buf;
1595 *pdestlen = size;
1596
1597 return 1;
1598 }
1599
1600 int tls1_save_sigalgs(SSL *s, PACKET *pkt, int cert)
1601 {
1602 /* Extension ignored for inappropriate versions */
1603 if (!SSL_USE_SIGALGS(s))
1604 return 1;
1605 /* Should never happen */
1606 if (s->cert == NULL)
1607 return 0;
1608
1609 if (cert)
1610 return tls1_save_u16(pkt, &s->s3->tmp.peer_cert_sigalgs,
1611 &s->s3->tmp.peer_cert_sigalgslen);
1612 else
1613 return tls1_save_u16(pkt, &s->s3->tmp.peer_sigalgs,
1614 &s->s3->tmp.peer_sigalgslen);
1615
1616 }
1617
1618 /* Set preferred digest for each key type */
1619
1620 int tls1_process_sigalgs(SSL *s)
1621 {
1622 size_t i;
1623 uint32_t *pvalid = s->s3->tmp.valid_flags;
1624 CERT *c = s->cert;
1625
1626 if (!tls1_set_shared_sigalgs(s))
1627 return 0;
1628
1629 for (i = 0; i < SSL_PKEY_NUM; i++)
1630 pvalid[i] = 0;
1631
1632 for (i = 0; i < c->shared_sigalgslen; i++) {
1633 const SIGALG_LOOKUP *sigptr = c->shared_sigalgs[i];
1634 int idx = sigptr->sig_idx;
1635
1636 /* Ignore PKCS1 based sig algs in TLSv1.3 */
1637 if (SSL_IS_TLS13(s) && sigptr->sig == EVP_PKEY_RSA)
1638 continue;
1639 /* If not disabled indicate we can explicitly sign */
1640 if (pvalid[idx] == 0 && !ssl_cert_is_disabled(idx))
1641 pvalid[idx] = CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
1642 }
1643 return 1;
1644 }
1645
1646 int SSL_get_sigalgs(SSL *s, int idx,
1647 int *psign, int *phash, int *psignhash,
1648 unsigned char *rsig, unsigned char *rhash)
1649 {
1650 uint16_t *psig = s->s3->tmp.peer_sigalgs;
1651 size_t numsigalgs = s->s3->tmp.peer_sigalgslen;
1652 if (psig == NULL || numsigalgs > INT_MAX)
1653 return 0;
1654 if (idx >= 0) {
1655 const SIGALG_LOOKUP *lu;
1656
1657 if (idx >= (int)numsigalgs)
1658 return 0;
1659 psig += idx;
1660 if (rhash != NULL)
1661 *rhash = (unsigned char)((*psig >> 8) & 0xff);
1662 if (rsig != NULL)
1663 *rsig = (unsigned char)(*psig & 0xff);
1664 lu = tls1_lookup_sigalg(*psig);
1665 if (psign != NULL)
1666 *psign = lu != NULL ? lu->sig : NID_undef;
1667 if (phash != NULL)
1668 *phash = lu != NULL ? lu->hash : NID_undef;
1669 if (psignhash != NULL)
1670 *psignhash = lu != NULL ? lu->sigandhash : NID_undef;
1671 }
1672 return (int)numsigalgs;
1673 }
1674
1675 int SSL_get_shared_sigalgs(SSL *s, int idx,
1676 int *psign, int *phash, int *psignhash,
1677 unsigned char *rsig, unsigned char *rhash)
1678 {
1679 const SIGALG_LOOKUP *shsigalgs;
1680 if (s->cert->shared_sigalgs == NULL
1681 || idx < 0
1682 || idx >= (int)s->cert->shared_sigalgslen
1683 || s->cert->shared_sigalgslen > INT_MAX)
1684 return 0;
1685 shsigalgs = s->cert->shared_sigalgs[idx];
1686 if (phash != NULL)
1687 *phash = shsigalgs->hash;
1688 if (psign != NULL)
1689 *psign = shsigalgs->sig;
1690 if (psignhash != NULL)
1691 *psignhash = shsigalgs->sigandhash;
1692 if (rsig != NULL)
1693 *rsig = (unsigned char)(shsigalgs->sigalg & 0xff);
1694 if (rhash != NULL)
1695 *rhash = (unsigned char)((shsigalgs->sigalg >> 8) & 0xff);
1696 return (int)s->cert->shared_sigalgslen;
1697 }
1698
1699 /* Maximum possible number of unique entries in sigalgs array */
1700 #define TLS_MAX_SIGALGCNT (OSSL_NELEM(sigalg_lookup_tbl) * 2)
1701
1702 typedef struct {
1703 size_t sigalgcnt;
1704 /* TLSEXT_SIGALG_XXX values */
1705 uint16_t sigalgs[TLS_MAX_SIGALGCNT];
1706 } sig_cb_st;
1707
1708 static void get_sigorhash(int *psig, int *phash, const char *str)
1709 {
1710 if (strcmp(str, "RSA") == 0) {
1711 *psig = EVP_PKEY_RSA;
1712 } else if (strcmp(str, "RSA-PSS") == 0 || strcmp(str, "PSS") == 0) {
1713 *psig = EVP_PKEY_RSA_PSS;
1714 } else if (strcmp(str, "DSA") == 0) {
1715 *psig = EVP_PKEY_DSA;
1716 } else if (strcmp(str, "ECDSA") == 0) {
1717 *psig = EVP_PKEY_EC;
1718 } else {
1719 *phash = OBJ_sn2nid(str);
1720 if (*phash == NID_undef)
1721 *phash = OBJ_ln2nid(str);
1722 }
1723 }
1724 /* Maximum length of a signature algorithm string component */
1725 #define TLS_MAX_SIGSTRING_LEN 40
1726
1727 static int sig_cb(const char *elem, int len, void *arg)
1728 {
1729 sig_cb_st *sarg = arg;
1730 size_t i;
1731 const SIGALG_LOOKUP *s;
1732 char etmp[TLS_MAX_SIGSTRING_LEN], *p;
1733 int sig_alg = NID_undef, hash_alg = NID_undef;
1734 if (elem == NULL)
1735 return 0;
1736 if (sarg->sigalgcnt == TLS_MAX_SIGALGCNT)
1737 return 0;
1738 if (len > (int)(sizeof(etmp) - 1))
1739 return 0;
1740 memcpy(etmp, elem, len);
1741 etmp[len] = 0;
1742 p = strchr(etmp, '+');
1743 /*
1744 * We only allow SignatureSchemes listed in the sigalg_lookup_tbl;
1745 * if there's no '+' in the provided name, look for the new-style combined
1746 * name. If not, match both sig+hash to find the needed SIGALG_LOOKUP.
1747 * Just sig+hash is not unique since TLS 1.3 adds rsa_pss_pss_* and
1748 * rsa_pss_rsae_* that differ only by public key OID; in such cases
1749 * we will pick the _rsae_ variant, by virtue of them appearing earlier
1750 * in the table.
1751 */
1752 if (p == NULL) {
1753 for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl);
1754 i++, s++) {
1755 if (s->name != NULL && strcmp(etmp, s->name) == 0) {
1756 sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg;
1757 break;
1758 }
1759 }
1760 if (i == OSSL_NELEM(sigalg_lookup_tbl))
1761 return 0;
1762 } else {
1763 *p = 0;
1764 p++;
1765 if (*p == 0)
1766 return 0;
1767 get_sigorhash(&sig_alg, &hash_alg, etmp);
1768 get_sigorhash(&sig_alg, &hash_alg, p);
1769 if (sig_alg == NID_undef || hash_alg == NID_undef)
1770 return 0;
1771 for (i = 0, s = sigalg_lookup_tbl; i < OSSL_NELEM(sigalg_lookup_tbl);
1772 i++, s++) {
1773 if (s->hash == hash_alg && s->sig == sig_alg) {
1774 sarg->sigalgs[sarg->sigalgcnt++] = s->sigalg;
1775 break;
1776 }
1777 }
1778 if (i == OSSL_NELEM(sigalg_lookup_tbl))
1779 return 0;
1780 }
1781
1782 /* Reject duplicates */
1783 for (i = 0; i < sarg->sigalgcnt - 1; i++) {
1784 if (sarg->sigalgs[i] == sarg->sigalgs[sarg->sigalgcnt]) {
1785 sarg->sigalgcnt--;
1786 return 0;
1787 }
1788 }
1789 return 1;
1790 }
1791
1792 /*
1793 * Set supported signature algorithms based on a colon separated list of the
1794 * form sig+hash e.g. RSA+SHA512:DSA+SHA512
1795 */
1796 int tls1_set_sigalgs_list(CERT *c, const char *str, int client)
1797 {
1798 sig_cb_st sig;
1799 sig.sigalgcnt = 0;
1800 if (!CONF_parse_list(str, ':', 1, sig_cb, &sig))
1801 return 0;
1802 if (c == NULL)
1803 return 1;
1804 return tls1_set_raw_sigalgs(c, sig.sigalgs, sig.sigalgcnt, client);
1805 }
1806
1807 int tls1_set_raw_sigalgs(CERT *c, const uint16_t *psigs, size_t salglen,
1808 int client)
1809 {
1810 uint16_t *sigalgs;
1811
1812 sigalgs = OPENSSL_malloc(salglen * sizeof(*sigalgs));
1813 if (sigalgs == NULL)
1814 return 0;
1815 memcpy(sigalgs, psigs, salglen * sizeof(*sigalgs));
1816
1817 if (client) {
1818 OPENSSL_free(c->client_sigalgs);
1819 c->client_sigalgs = sigalgs;
1820 c->client_sigalgslen = salglen;
1821 } else {
1822 OPENSSL_free(c->conf_sigalgs);
1823 c->conf_sigalgs = sigalgs;
1824 c->conf_sigalgslen = salglen;
1825 }
1826
1827 return 1;
1828 }
1829
1830 int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client)
1831 {
1832 uint16_t *sigalgs, *sptr;
1833 size_t i;
1834
1835 if (salglen & 1)
1836 return 0;
1837 sigalgs = OPENSSL_malloc((salglen / 2) * sizeof(*sigalgs));
1838 if (sigalgs == NULL)
1839 return 0;
1840 for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
1841 size_t j;
1842 const SIGALG_LOOKUP *curr;
1843 int md_id = *psig_nids++;
1844 int sig_id = *psig_nids++;
1845
1846 for (j = 0, curr = sigalg_lookup_tbl; j < OSSL_NELEM(sigalg_lookup_tbl);
1847 j++, curr++) {
1848 if (curr->hash == md_id && curr->sig == sig_id) {
1849 *sptr++ = curr->sigalg;
1850 break;
1851 }
1852 }
1853
1854 if (j == OSSL_NELEM(sigalg_lookup_tbl))
1855 goto err;
1856 }
1857
1858 if (client) {
1859 OPENSSL_free(c->client_sigalgs);
1860 c->client_sigalgs = sigalgs;
1861 c->client_sigalgslen = salglen / 2;
1862 } else {
1863 OPENSSL_free(c->conf_sigalgs);
1864 c->conf_sigalgs = sigalgs;
1865 c->conf_sigalgslen = salglen / 2;
1866 }
1867
1868 return 1;
1869
1870 err:
1871 OPENSSL_free(sigalgs);
1872 return 0;
1873 }
1874
1875 static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid)
1876 {
1877 int sig_nid;
1878 size_t i;
1879 if (default_nid == -1)
1880 return 1;
1881 sig_nid = X509_get_signature_nid(x);
1882 if (default_nid)
1883 return sig_nid == default_nid ? 1 : 0;
1884 for (i = 0; i < c->shared_sigalgslen; i++)
1885 if (sig_nid == c->shared_sigalgs[i]->sigandhash)
1886 return 1;
1887 return 0;
1888 }
1889
1890 /* Check to see if a certificate issuer name matches list of CA names */
1891 static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x)
1892 {
1893 X509_NAME *nm;
1894 int i;
1895 nm = X509_get_issuer_name(x);
1896 for (i = 0; i < sk_X509_NAME_num(names); i++) {
1897 if (!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i)))
1898 return 1;
1899 }
1900 return 0;
1901 }
1902
1903 /*
1904 * Check certificate chain is consistent with TLS extensions and is usable by
1905 * server. This servers two purposes: it allows users to check chains before
1906 * passing them to the server and it allows the server to check chains before
1907 * attempting to use them.
1908 */
1909
1910 /* Flags which need to be set for a certificate when strict mode not set */
1911
1912 #define CERT_PKEY_VALID_FLAGS \
1913 (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM)
1914 /* Strict mode flags */
1915 #define CERT_PKEY_STRICT_FLAGS \
1916 (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \
1917 | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE)
1918
1919 int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain,
1920 int idx)
1921 {
1922 int i;
1923 int rv = 0;
1924 int check_flags = 0, strict_mode;
1925 CERT_PKEY *cpk = NULL;
1926 CERT *c = s->cert;
1927 uint32_t *pvalid;
1928 unsigned int suiteb_flags = tls1_suiteb(s);
1929 /* idx == -1 means checking server chains */
1930 if (idx != -1) {
1931 /* idx == -2 means checking client certificate chains */
1932 if (idx == -2) {
1933 cpk = c->key;
1934 idx = (int)(cpk - c->pkeys);
1935 } else
1936 cpk = c->pkeys + idx;
1937 pvalid = s->s3->tmp.valid_flags + idx;
1938 x = cpk->x509;
1939 pk = cpk->privatekey;
1940 chain = cpk->chain;
1941 strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT;
1942 /* If no cert or key, forget it */
1943 if (!x || !pk)
1944 goto end;
1945 } else {
1946 size_t certidx;
1947
1948 if (!x || !pk)
1949 return 0;
1950
1951 if (ssl_cert_lookup_by_pkey(pk, &certidx) == NULL)
1952 return 0;
1953 idx = certidx;
1954 pvalid = s->s3->tmp.valid_flags + idx;
1955
1956 if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)
1957 check_flags = CERT_PKEY_STRICT_FLAGS;
1958 else
1959 check_flags = CERT_PKEY_VALID_FLAGS;
1960 strict_mode = 1;
1961 }
1962
1963 if (suiteb_flags) {
1964 int ok;
1965 if (check_flags)
1966 check_flags |= CERT_PKEY_SUITEB;
1967 ok = X509_chain_check_suiteb(NULL, x, chain, suiteb_flags);
1968 if (ok == X509_V_OK)
1969 rv |= CERT_PKEY_SUITEB;
1970 else if (!check_flags)
1971 goto end;
1972 }
1973
1974 /*
1975 * Check all signature algorithms are consistent with signature
1976 * algorithms extension if TLS 1.2 or later and strict mode.
1977 */
1978 if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) {
1979 int default_nid;
1980 int rsign = 0;
1981 if (s->s3->tmp.peer_cert_sigalgs != NULL
1982 || s->s3->tmp.peer_sigalgs != NULL) {
1983 default_nid = 0;
1984 /* If no sigalgs extension use defaults from RFC5246 */
1985 } else {
1986 switch (idx) {
1987 case SSL_PKEY_RSA:
1988 rsign = EVP_PKEY_RSA;
1989 default_nid = NID_sha1WithRSAEncryption;
1990 break;
1991
1992 case SSL_PKEY_DSA_SIGN:
1993 rsign = EVP_PKEY_DSA;
1994 default_nid = NID_dsaWithSHA1;
1995 break;
1996
1997 case SSL_PKEY_ECC:
1998 rsign = EVP_PKEY_EC;
1999 default_nid = NID_ecdsa_with_SHA1;
2000 break;
2001
2002 case SSL_PKEY_GOST01:
2003 rsign = NID_id_GostR3410_2001;
2004 default_nid = NID_id_GostR3411_94_with_GostR3410_2001;
2005 break;
2006
2007 case SSL_PKEY_GOST12_256:
2008 rsign = NID_id_GostR3410_2012_256;
2009 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_256;
2010 break;
2011
2012 case SSL_PKEY_GOST12_512:
2013 rsign = NID_id_GostR3410_2012_512;
2014 default_nid = NID_id_tc26_signwithdigest_gost3410_2012_512;
2015 break;
2016
2017 default:
2018 default_nid = -1;
2019 break;
2020 }
2021 }
2022 /*
2023 * If peer sent no signature algorithms extension and we have set
2024 * preferred signature algorithms check we support sha1.
2025 */
2026 if (default_nid > 0 && c->conf_sigalgs) {
2027 size_t j;
2028 const uint16_t *p = c->conf_sigalgs;
2029 for (j = 0; j < c->conf_sigalgslen; j++, p++) {
2030 const SIGALG_LOOKUP *lu = tls1_lookup_sigalg(*p);
2031
2032 if (lu != NULL && lu->hash == NID_sha1 && lu->sig == rsign)
2033 break;
2034 }
2035 if (j == c->conf_sigalgslen) {
2036 if (check_flags)
2037 goto skip_sigs;
2038 else
2039 goto end;
2040 }
2041 }
2042 /* Check signature algorithm of each cert in chain */
2043 if (!tls1_check_sig_alg(c, x, default_nid)) {
2044 if (!check_flags)
2045 goto end;
2046 } else
2047 rv |= CERT_PKEY_EE_SIGNATURE;
2048 rv |= CERT_PKEY_CA_SIGNATURE;
2049 for (i = 0; i < sk_X509_num(chain); i++) {
2050 if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), default_nid)) {
2051 if (check_flags) {
2052 rv &= ~CERT_PKEY_CA_SIGNATURE;
2053 break;
2054 } else
2055 goto end;
2056 }
2057 }
2058 }
2059 /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */
2060 else if (check_flags)
2061 rv |= CERT_PKEY_EE_SIGNATURE | CERT_PKEY_CA_SIGNATURE;
2062 skip_sigs:
2063 /* Check cert parameters are consistent */
2064 if (tls1_check_cert_param(s, x, 1))
2065 rv |= CERT_PKEY_EE_PARAM;
2066 else if (!check_flags)
2067 goto end;
2068 if (!s->server)
2069 rv |= CERT_PKEY_CA_PARAM;
2070 /* In strict mode check rest of chain too */
2071 else if (strict_mode) {
2072 rv |= CERT_PKEY_CA_PARAM;
2073 for (i = 0; i < sk_X509_num(chain); i++) {
2074 X509 *ca = sk_X509_value(chain, i);
2075 if (!tls1_check_cert_param(s, ca, 0)) {
2076 if (check_flags) {
2077 rv &= ~CERT_PKEY_CA_PARAM;
2078 break;
2079 } else
2080 goto end;
2081 }
2082 }
2083 }
2084 if (!s->server && strict_mode) {
2085 STACK_OF(X509_NAME) *ca_dn;
2086 int check_type = 0;
2087 switch (EVP_PKEY_id(pk)) {
2088 case EVP_PKEY_RSA:
2089 check_type = TLS_CT_RSA_SIGN;
2090 break;
2091 case EVP_PKEY_DSA:
2092 check_type = TLS_CT_DSS_SIGN;
2093 break;
2094 case EVP_PKEY_EC:
2095 check_type = TLS_CT_ECDSA_SIGN;
2096 break;
2097 }
2098 if (check_type) {
2099 const uint8_t *ctypes = s->s3->tmp.ctype;
2100 size_t j;
2101
2102 for (j = 0; j < s->s3->tmp.ctype_len; j++, ctypes++) {
2103 if (*ctypes == check_type) {
2104 rv |= CERT_PKEY_CERT_TYPE;
2105 break;
2106 }
2107 }
2108 if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags)
2109 goto end;
2110 } else {
2111 rv |= CERT_PKEY_CERT_TYPE;
2112 }
2113
2114 ca_dn = s->s3->tmp.peer_ca_names;
2115
2116 if (!sk_X509_NAME_num(ca_dn))
2117 rv |= CERT_PKEY_ISSUER_NAME;
2118
2119 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
2120 if (ssl_check_ca_name(ca_dn, x))
2121 rv |= CERT_PKEY_ISSUER_NAME;
2122 }
2123 if (!(rv & CERT_PKEY_ISSUER_NAME)) {
2124 for (i = 0; i < sk_X509_num(chain); i++) {
2125 X509 *xtmp = sk_X509_value(chain, i);
2126 if (ssl_check_ca_name(ca_dn, xtmp)) {
2127 rv |= CERT_PKEY_ISSUER_NAME;
2128 break;
2129 }
2130 }
2131 }
2132 if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME))
2133 goto end;
2134 } else
2135 rv |= CERT_PKEY_ISSUER_NAME | CERT_PKEY_CERT_TYPE;
2136
2137 if (!check_flags || (rv & check_flags) == check_flags)
2138 rv |= CERT_PKEY_VALID;
2139
2140 end:
2141
2142 if (TLS1_get_version(s) >= TLS1_2_VERSION)
2143 rv |= *pvalid & (CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN);
2144 else
2145 rv |= CERT_PKEY_SIGN | CERT_PKEY_EXPLICIT_SIGN;
2146
2147 /*
2148 * When checking a CERT_PKEY structure all flags are irrelevant if the
2149 * chain is invalid.
2150 */
2151 if (!check_flags) {
2152 if (rv & CERT_PKEY_VALID) {
2153 *pvalid = rv;
2154 } else {
2155 /* Preserve sign and explicit sign flag, clear rest */
2156 *pvalid &= CERT_PKEY_EXPLICIT_SIGN | CERT_PKEY_SIGN;
2157 return 0;
2158 }
2159 }
2160 return rv;
2161 }
2162
2163 /* Set validity of certificates in an SSL structure */
2164 void tls1_set_cert_validity(SSL *s)
2165 {
2166 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA);
2167 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_PSS_SIGN);
2168 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_DSA_SIGN);
2169 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC);
2170 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST01);
2171 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_256);
2172 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_GOST12_512);
2173 tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ED25519);
2174 }
2175
2176 /* User level utility function to check a chain is suitable */
2177 int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain)
2178 {
2179 return tls1_check_chain(s, x, pk, chain, -1);
2180 }
2181
2182 #ifndef OPENSSL_NO_DH
2183 DH *ssl_get_auto_dh(SSL *s)
2184 {
2185 int dh_secbits = 80;
2186 if (s->cert->dh_tmp_auto == 2)
2187 return DH_get_1024_160();
2188 if (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aPSK)) {
2189 if (s->s3->tmp.new_cipher->strength_bits == 256)
2190 dh_secbits = 128;
2191 else
2192 dh_secbits = 80;
2193 } else {
2194 if (s->s3->tmp.cert == NULL)
2195 return NULL;
2196 dh_secbits = EVP_PKEY_security_bits(s->s3->tmp.cert->privatekey);
2197 }
2198
2199 if (dh_secbits >= 128) {
2200 DH *dhp = DH_new();
2201 BIGNUM *p, *g;
2202 if (dhp == NULL)
2203 return NULL;
2204 g = BN_new();
2205 if (g != NULL)
2206 BN_set_word(g, 2);
2207 if (dh_secbits >= 192)
2208 p = BN_get_rfc3526_prime_8192(NULL);
2209 else
2210 p = BN_get_rfc3526_prime_3072(NULL);
2211 if (p == NULL || g == NULL || !DH_set0_pqg(dhp, p, NULL, g)) {
2212 DH_free(dhp);
2213 BN_free(p);
2214 BN_free(g);
2215 return NULL;
2216 }
2217 return dhp;
2218 }
2219 if (dh_secbits >= 112)
2220 return DH_get_2048_224();
2221 return DH_get_1024_160();
2222 }
2223 #endif
2224
2225 static int ssl_security_cert_key(SSL *s, SSL_CTX *ctx, X509 *x, int op)
2226 {
2227 int secbits = -1;
2228 EVP_PKEY *pkey = X509_get0_pubkey(x);
2229 if (pkey) {
2230 /*
2231 * If no parameters this will return -1 and fail using the default
2232 * security callback for any non-zero security level. This will
2233 * reject keys which omit parameters but this only affects DSA and
2234 * omission of parameters is never (?) done in practice.
2235 */
2236 secbits = EVP_PKEY_security_bits(pkey);
2237 }
2238 if (s)
2239 return ssl_security(s, op, secbits, 0, x);
2240 else
2241 return ssl_ctx_security(ctx, op, secbits, 0, x);
2242 }
2243
2244 static int ssl_security_cert_sig(SSL *s, SSL_CTX *ctx, X509 *x, int op)
2245 {
2246 /* Lookup signature algorithm digest */
2247 int secbits, nid, pknid;
2248 /* Don't check signature if self signed */
2249 if ((X509_get_extension_flags(x) & EXFLAG_SS) != 0)
2250 return 1;
2251 if (!X509_get_signature_info(x, &nid, &pknid, &secbits, NULL))
2252 secbits = -1;
2253 /* If digest NID not defined use signature NID */
2254 if (nid == NID_undef)
2255 nid = pknid;
2256 if (s)
2257 return ssl_security(s, op, secbits, nid, x);
2258 else
2259 return ssl_ctx_security(ctx, op, secbits, nid, x);
2260 }
2261
2262 int ssl_security_cert(SSL *s, SSL_CTX *ctx, X509 *x, int vfy, int is_ee)
2263 {
2264 if (vfy)
2265 vfy = SSL_SECOP_PEER;
2266 if (is_ee) {
2267 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_EE_KEY | vfy))
2268 return SSL_R_EE_KEY_TOO_SMALL;
2269 } else {
2270 if (!ssl_security_cert_key(s, ctx, x, SSL_SECOP_CA_KEY | vfy))
2271 return SSL_R_CA_KEY_TOO_SMALL;
2272 }
2273 if (!ssl_security_cert_sig(s, ctx, x, SSL_SECOP_CA_MD | vfy))
2274 return SSL_R_CA_MD_TOO_WEAK;
2275 return 1;
2276 }
2277
2278 /*
2279 * Check security of a chain, if |sk| includes the end entity certificate then
2280 * |x| is NULL. If |vfy| is 1 then we are verifying a peer chain and not sending
2281 * one to the peer. Return values: 1 if ok otherwise error code to use
2282 */
2283
2284 int ssl_security_cert_chain(SSL *s, STACK_OF(X509) *sk, X509 *x, int vfy)
2285 {
2286 int rv, start_idx, i;
2287 if (x == NULL) {
2288 x = sk_X509_value(sk, 0);
2289 start_idx = 1;
2290 } else
2291 start_idx = 0;
2292
2293 rv = ssl_security_cert(s, NULL, x, vfy, 1);
2294 if (rv != 1)
2295 return rv;
2296
2297 for (i = start_idx; i < sk_X509_num(sk); i++) {
2298 x = sk_X509_value(sk, i);
2299 rv = ssl_security_cert(s, NULL, x, vfy, 0);
2300 if (rv != 1)
2301 return rv;
2302 }
2303 return 1;
2304 }
2305
2306 /*
2307 * For TLS 1.2 servers check if we have a certificate which can be used
2308 * with the signature algorithm "lu" and return index of certificate.
2309 */
2310
2311 static int tls12_get_cert_sigalg_idx(const SSL *s, const SIGALG_LOOKUP *lu)
2312 {
2313 int sig_idx = lu->sig_idx;
2314 const SSL_CERT_LOOKUP *clu = ssl_cert_lookup_by_idx(sig_idx);
2315
2316 /* If not recognised or not supported by cipher mask it is not suitable */
2317 if (clu == NULL || !(clu->amask & s->s3->tmp.new_cipher->algorithm_auth))
2318 return -1;
2319
2320 return s->s3->tmp.valid_flags[sig_idx] & CERT_PKEY_VALID ? sig_idx : -1;
2321 }
2322
2323 /*
2324 * Returns true if |s| has a usable certificate configured for use
2325 * with signature scheme |sig|.
2326 * "Usable" includes a check for presence as well as applying
2327 * the signature_algorithm_cert restrictions sent by the peer (if any).
2328 * Returns false if no usable certificate is found.
2329 */
2330 static int has_usable_cert(SSL *s, const SIGALG_LOOKUP *sig, int idx)
2331 {
2332 const SIGALG_LOOKUP *lu;
2333 int mdnid, pknid;
2334 size_t i;
2335
2336 /* TLS 1.2 callers can override lu->sig_idx, but not TLS 1.3 callers. */
2337 if (idx == -1)
2338 idx = sig->sig_idx;
2339 if (!ssl_has_cert(s, idx))
2340 return 0;
2341 if (s->s3->tmp.peer_cert_sigalgs != NULL) {
2342 for (i = 0; i < s->s3->tmp.peer_cert_sigalgslen; i++) {
2343 lu = tls1_lookup_sigalg(s->s3->tmp.peer_cert_sigalgs[i]);
2344 if (lu == NULL
2345 || !X509_get_signature_info(s->cert->pkeys[idx].x509, &mdnid,
2346 &pknid, NULL, NULL))
2347 continue;
2348 /*
2349 * TODO this does not differentiate between the
2350 * rsa_pss_pss_* and rsa_pss_rsae_* schemes since we do not
2351 * have a chain here that lets us look at the key OID in the
2352 * signing certificate.
2353 */
2354 if (mdnid == lu->hash && pknid == lu->sig)
2355 return 1;
2356 }
2357 return 0;
2358 }
2359 return 1;
2360 }
2361
2362 /*
2363 * Choose an appropriate signature algorithm based on available certificates
2364 * Sets chosen certificate and signature algorithm.
2365 *
2366 * For servers if we fail to find a required certificate it is a fatal error,
2367 * an appropriate error code is set and a TLS alert is sent.
2368 *
2369 * For clients fatalerrs is set to 0. If a certificate is not suitable it is not
2370 * a fatal error: we will either try another certificate or not present one
2371 * to the server. In this case no error is set.
2372 */
2373 int tls_choose_sigalg(SSL *s, int fatalerrs)
2374 {
2375 const SIGALG_LOOKUP *lu = NULL;
2376 int sig_idx = -1;
2377
2378 s->s3->tmp.cert = NULL;
2379 s->s3->tmp.sigalg = NULL;
2380
2381 if (SSL_IS_TLS13(s)) {
2382 size_t i;
2383 #ifndef OPENSSL_NO_EC
2384 int curve = -1, skip_ec = 0;
2385 #endif
2386
2387 /* Look for a certificate matching shared sigalgs */
2388 for (i = 0; i < s->cert->shared_sigalgslen; i++) {
2389 lu = s->cert->shared_sigalgs[i];
2390 sig_idx = -1;
2391
2392 /* Skip SHA1, SHA224, DSA and RSA if not PSS */
2393 if (lu->hash == NID_sha1
2394 || lu->hash == NID_sha224
2395 || lu->sig == EVP_PKEY_DSA
2396 || lu->sig == EVP_PKEY_RSA)
2397 continue;
2398 /* Check that we have a cert, and signature_algorithms_cert */
2399 if (!tls1_lookup_md(lu, NULL) || !has_usable_cert(s, lu, -1))
2400 continue;
2401 if (lu->sig == EVP_PKEY_EC) {
2402 #ifndef OPENSSL_NO_EC
2403 if (curve == -1) {
2404 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(s->cert->pkeys[SSL_PKEY_ECC].privatekey);
2405
2406 curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
2407 if (EC_KEY_get_conv_form(ec)
2408 != POINT_CONVERSION_UNCOMPRESSED)
2409 skip_ec = 1;
2410 }
2411 if (skip_ec || (lu->curve != NID_undef && curve != lu->curve))
2412 continue;
2413 #else
2414 continue;
2415 #endif
2416 } else if (lu->sig == EVP_PKEY_RSA_PSS) {
2417 /* validate that key is large enough for the signature algorithm */
2418 EVP_PKEY *pkey;
2419
2420 pkey = s->cert->pkeys[lu->sig_idx].privatekey;
2421 if (!rsa_pss_check_min_key_size(EVP_PKEY_get0(pkey), lu))
2422 continue;
2423 }
2424 break;
2425 }
2426 if (i == s->cert->shared_sigalgslen) {
2427 if (!fatalerrs)
2428 return 1;
2429 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_CHOOSE_SIGALG,
2430 SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
2431 return 0;
2432 }
2433 } else {
2434 /* If ciphersuite doesn't require a cert nothing to do */
2435 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aCERT))
2436 return 1;
2437 if (!s->server && !ssl_has_cert(s, s->cert->key - s->cert->pkeys))
2438 return 1;
2439
2440 if (SSL_USE_SIGALGS(s)) {
2441 size_t i;
2442 if (s->s3->tmp.peer_sigalgs != NULL) {
2443 #ifndef OPENSSL_NO_EC
2444 int curve;
2445
2446 /* For Suite B need to match signature algorithm to curve */
2447 if (tls1_suiteb(s)) {
2448 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(s->cert->pkeys[SSL_PKEY_ECC].privatekey);
2449 curve = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
2450 } else {
2451 curve = -1;
2452 }
2453 #endif
2454
2455 /*
2456 * Find highest preference signature algorithm matching
2457 * cert type
2458 */
2459 for (i = 0; i < s->cert->shared_sigalgslen; i++) {
2460 lu = s->cert->shared_sigalgs[i];
2461
2462 if (s->server) {
2463 if ((sig_idx = tls12_get_cert_sigalg_idx(s, lu)) == -1)
2464 continue;
2465 } else {
2466 int cc_idx = s->cert->key - s->cert->pkeys;
2467
2468 sig_idx = lu->sig_idx;
2469 if (cc_idx != sig_idx)
2470 continue;
2471 }
2472 /* Check that we have a cert, and sig_algs_cert */
2473 if (!has_usable_cert(s, lu, sig_idx))
2474 continue;
2475 if (lu->sig == EVP_PKEY_RSA_PSS) {
2476 /* validate that key is large enough for the signature algorithm */
2477 EVP_PKEY *pkey = s->cert->pkeys[sig_idx].privatekey;
2478
2479 if (!rsa_pss_check_min_key_size(EVP_PKEY_get0(pkey), lu))
2480 continue;
2481 }
2482 #ifndef OPENSSL_NO_EC
2483 if (curve == -1 || lu->curve == curve)
2484 #endif
2485 break;
2486 }
2487 if (i == s->cert->shared_sigalgslen) {
2488 if (!fatalerrs)
2489 return 1;
2490 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CHOOSE_SIGALG,
2491 ERR_R_INTERNAL_ERROR);
2492 return 0;
2493 }
2494 } else {
2495 /*
2496 * If we have no sigalg use defaults
2497 */
2498 const uint16_t *sent_sigs;
2499 size_t sent_sigslen;
2500
2501 if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) {
2502 if (!fatalerrs)
2503 return 1;
2504 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CHOOSE_SIGALG,
2505 ERR_R_INTERNAL_ERROR);
2506 return 0;
2507 }
2508
2509 /* Check signature matches a type we sent */
2510 sent_sigslen = tls12_get_psigalgs(s, 1, &sent_sigs);
2511 for (i = 0; i < sent_sigslen; i++, sent_sigs++) {
2512 if (lu->sigalg == *sent_sigs
2513 && has_usable_cert(s, lu, lu->sig_idx))
2514 break;
2515 }
2516 if (i == sent_sigslen) {
2517 if (!fatalerrs)
2518 return 1;
2519 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
2520 SSL_F_TLS_CHOOSE_SIGALG,
2521 SSL_R_WRONG_SIGNATURE_TYPE);
2522 return 0;
2523 }
2524 }
2525 } else {
2526 if ((lu = tls1_get_legacy_sigalg(s, -1)) == NULL) {
2527 if (!fatalerrs)
2528 return 1;
2529 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CHOOSE_SIGALG,
2530 ERR_R_INTERNAL_ERROR);
2531 return 0;
2532 }
2533 }
2534 }
2535 if (sig_idx == -1)
2536 sig_idx = lu->sig_idx;
2537 s->s3->tmp.cert = &s->cert->pkeys[sig_idx];
2538 s->cert->key = s->s3->tmp.cert;
2539 s->s3->tmp.sigalg = lu;
2540 return 1;
2541 }
2542
2543 int SSL_CTX_set_tlsext_max_fragment_length(SSL_CTX *ctx, uint8_t mode)
2544 {
2545 if (mode != TLSEXT_max_fragment_length_DISABLED
2546 && !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode)) {
2547 SSLerr(SSL_F_SSL_CTX_SET_TLSEXT_MAX_FRAGMENT_LENGTH,
2548 SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
2549 return 0;
2550 }
2551
2552 ctx->ext.max_fragment_len_mode = mode;
2553 return 1;
2554 }
2555
2556 int SSL_set_tlsext_max_fragment_length(SSL *ssl, uint8_t mode)
2557 {
2558 if (mode != TLSEXT_max_fragment_length_DISABLED
2559 && !IS_MAX_FRAGMENT_LENGTH_EXT_VALID(mode)) {
2560 SSLerr(SSL_F_SSL_SET_TLSEXT_MAX_FRAGMENT_LENGTH,
2561 SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH);
2562 return 0;
2563 }
2564
2565 ssl->ext.max_fragment_len_mode = mode;
2566 return 1;
2567 }
2568
2569 uint8_t SSL_SESSION_get_max_fragment_length(const SSL_SESSION *session)
2570 {
2571 return session->ext.max_fragment_len_mode;
2572 }