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