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