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