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