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