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