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