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