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