/src/openssl30/crypto/x509/x509_cmp.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (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 "internal/cryptlib.h" |
12 | | #include <openssl/asn1.h> |
13 | | #include <openssl/objects.h> |
14 | | #include <openssl/x509.h> |
15 | | #include <openssl/x509v3.h> |
16 | | #include <openssl/core_names.h> |
17 | | #include "crypto/x509.h" |
18 | | |
19 | | int X509_issuer_and_serial_cmp(const X509 *a, const X509 *b) |
20 | 0 | { |
21 | 0 | int i; |
22 | 0 | const X509_CINF *ai, *bi; |
23 | |
|
24 | 0 | if (b == NULL) |
25 | 0 | return a != NULL; |
26 | 0 | if (a == NULL) |
27 | 0 | return -1; |
28 | 0 | ai = &a->cert_info; |
29 | 0 | bi = &b->cert_info; |
30 | 0 | i = ASN1_INTEGER_cmp(&ai->serialNumber, &bi->serialNumber); |
31 | 0 | if (i != 0) |
32 | 0 | return i < 0 ? -1 : 1; |
33 | 0 | return X509_NAME_cmp(ai->issuer, bi->issuer); |
34 | 0 | } |
35 | | |
36 | | #ifndef OPENSSL_NO_MD5 |
37 | | unsigned long X509_issuer_and_serial_hash(X509 *a) |
38 | 0 | { |
39 | 0 | unsigned long ret = 0; |
40 | 0 | EVP_MD_CTX *ctx = EVP_MD_CTX_new(); |
41 | 0 | unsigned char md[16]; |
42 | 0 | char *f = NULL; |
43 | 0 | EVP_MD *digest = NULL; |
44 | |
|
45 | 0 | if (ctx == NULL) |
46 | 0 | goto err; |
47 | 0 | f = X509_NAME_oneline(a->cert_info.issuer, NULL, 0); |
48 | 0 | if (f == NULL) |
49 | 0 | goto err; |
50 | 0 | digest = EVP_MD_fetch(a->libctx, SN_md5, a->propq); |
51 | 0 | if (digest == NULL) |
52 | 0 | goto err; |
53 | | |
54 | 0 | if (!EVP_DigestInit_ex(ctx, digest, NULL)) |
55 | 0 | goto err; |
56 | 0 | if (!EVP_DigestUpdate(ctx, (unsigned char *)f, strlen(f))) |
57 | 0 | goto err; |
58 | 0 | if (!EVP_DigestUpdate |
59 | 0 | (ctx, (unsigned char *)a->cert_info.serialNumber.data, |
60 | 0 | (unsigned long)a->cert_info.serialNumber.length)) |
61 | 0 | goto err; |
62 | 0 | if (!EVP_DigestFinal_ex(ctx, &(md[0]), NULL)) |
63 | 0 | goto err; |
64 | 0 | ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) | |
65 | 0 | ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L) |
66 | 0 | ) & 0xffffffffL; |
67 | 0 | err: |
68 | 0 | OPENSSL_free(f); |
69 | 0 | EVP_MD_free(digest); |
70 | 0 | EVP_MD_CTX_free(ctx); |
71 | 0 | return ret; |
72 | 0 | } |
73 | | #endif |
74 | | |
75 | | int X509_issuer_name_cmp(const X509 *a, const X509 *b) |
76 | 0 | { |
77 | 0 | return X509_NAME_cmp(a->cert_info.issuer, b->cert_info.issuer); |
78 | 0 | } |
79 | | |
80 | | int X509_subject_name_cmp(const X509 *a, const X509 *b) |
81 | 0 | { |
82 | 0 | return X509_NAME_cmp(a->cert_info.subject, b->cert_info.subject); |
83 | 0 | } |
84 | | |
85 | | int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b) |
86 | 0 | { |
87 | 0 | return X509_NAME_cmp(a->crl.issuer, b->crl.issuer); |
88 | 0 | } |
89 | | |
90 | | int X509_CRL_match(const X509_CRL *a, const X509_CRL *b) |
91 | 0 | { |
92 | 0 | int rv; |
93 | |
|
94 | 0 | if ((a->flags & EXFLAG_NO_FINGERPRINT) == 0 |
95 | 0 | && (b->flags & EXFLAG_NO_FINGERPRINT) == 0) |
96 | 0 | rv = memcmp(a->sha1_hash, b->sha1_hash, SHA_DIGEST_LENGTH); |
97 | 0 | else |
98 | 0 | return -2; |
99 | | |
100 | 0 | return rv < 0 ? -1 : rv > 0; |
101 | 0 | } |
102 | | |
103 | | X509_NAME *X509_get_issuer_name(const X509 *a) |
104 | 1.28k | { |
105 | 1.28k | return a->cert_info.issuer; |
106 | 1.28k | } |
107 | | |
108 | | unsigned long X509_issuer_name_hash(X509 *x) |
109 | 0 | { |
110 | 0 | return X509_NAME_hash_ex(x->cert_info.issuer, NULL, NULL, NULL); |
111 | 0 | } |
112 | | |
113 | | #ifndef OPENSSL_NO_MD5 |
114 | | unsigned long X509_issuer_name_hash_old(X509 *x) |
115 | 0 | { |
116 | 0 | return X509_NAME_hash_old(x->cert_info.issuer); |
117 | 0 | } |
118 | | #endif |
119 | | |
120 | | X509_NAME *X509_get_subject_name(const X509 *a) |
121 | 1.25k | { |
122 | 1.25k | return a->cert_info.subject; |
123 | 1.25k | } |
124 | | |
125 | | ASN1_INTEGER *X509_get_serialNumber(X509 *a) |
126 | 0 | { |
127 | 0 | return &a->cert_info.serialNumber; |
128 | 0 | } |
129 | | |
130 | | const ASN1_INTEGER *X509_get0_serialNumber(const X509 *a) |
131 | 49 | { |
132 | 49 | return &a->cert_info.serialNumber; |
133 | 49 | } |
134 | | |
135 | | unsigned long X509_subject_name_hash(X509 *x) |
136 | 0 | { |
137 | 0 | return X509_NAME_hash_ex(x->cert_info.subject, NULL, NULL, NULL); |
138 | 0 | } |
139 | | |
140 | | #ifndef OPENSSL_NO_MD5 |
141 | | unsigned long X509_subject_name_hash_old(X509 *x) |
142 | 0 | { |
143 | 0 | return X509_NAME_hash_old(x->cert_info.subject); |
144 | 0 | } |
145 | | #endif |
146 | | |
147 | | /* |
148 | | * Compare two certificates: they must be identical for this to work. NB: |
149 | | * Although "cmp" operations are generally prototyped to take "const" |
150 | | * arguments (eg. for use in STACKs), the way X509 handling is - these |
151 | | * operations may involve ensuring the hashes are up-to-date and ensuring |
152 | | * certain cert information is cached. So this is the point where the |
153 | | * "depth-first" constification tree has to halt with an evil cast. |
154 | | */ |
155 | | int X509_cmp(const X509 *a, const X509 *b) |
156 | 5.15k | { |
157 | 5.15k | int rv = 0; |
158 | | |
159 | 5.15k | if (a == b) /* for efficiency */ |
160 | 1.06k | return 0; |
161 | | |
162 | | /* attempt to compute cert hash */ |
163 | 4.09k | (void)X509_check_purpose((X509 *)a, -1, 0); |
164 | 4.09k | (void)X509_check_purpose((X509 *)b, -1, 0); |
165 | | |
166 | 4.09k | if ((a->ex_flags & EXFLAG_NO_FINGERPRINT) == 0 |
167 | 4.09k | && (b->ex_flags & EXFLAG_NO_FINGERPRINT) == 0) |
168 | 4.09k | rv = memcmp(a->sha1_hash, b->sha1_hash, SHA_DIGEST_LENGTH); |
169 | 4.09k | if (rv != 0) |
170 | 4.09k | return rv < 0 ? -1 : 1; |
171 | | |
172 | | /* Check for match against stored encoding too */ |
173 | 0 | if (!a->cert_info.enc.modified && !b->cert_info.enc.modified) { |
174 | 0 | if (a->cert_info.enc.len < b->cert_info.enc.len) |
175 | 0 | return -1; |
176 | 0 | if (a->cert_info.enc.len > b->cert_info.enc.len) |
177 | 0 | return 1; |
178 | 0 | rv = memcmp(a->cert_info.enc.enc, |
179 | 0 | b->cert_info.enc.enc, a->cert_info.enc.len); |
180 | 0 | } |
181 | 0 | return rv < 0 ? -1 : rv > 0; |
182 | 0 | } |
183 | | |
184 | | int ossl_x509_add_cert_new(STACK_OF(X509) **p_sk, X509 *cert, int flags) |
185 | 2.12k | { |
186 | 2.12k | if (*p_sk == NULL && (*p_sk = sk_X509_new_null()) == NULL) { |
187 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); |
188 | 0 | return 0; |
189 | 0 | } |
190 | 2.12k | return X509_add_cert(*p_sk, cert, flags); |
191 | 2.12k | } |
192 | | |
193 | | int X509_add_cert(STACK_OF(X509) *sk, X509 *cert, int flags) |
194 | 2.12k | { |
195 | 2.12k | if (sk == NULL) { |
196 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER); |
197 | 0 | return 0; |
198 | 0 | } |
199 | 2.12k | if ((flags & X509_ADD_FLAG_NO_DUP) != 0) { |
200 | | /* |
201 | | * not using sk_X509_set_cmp_func() and sk_X509_find() |
202 | | * because this re-orders the certs on the stack |
203 | | */ |
204 | 2.12k | int i; |
205 | | |
206 | 6.21k | for (i = 0; i < sk_X509_num(sk); i++) { |
207 | 5.15k | if (X509_cmp(sk_X509_value(sk, i), cert) == 0) |
208 | 1.06k | return 1; |
209 | 5.15k | } |
210 | 2.12k | } |
211 | 1.06k | if ((flags & X509_ADD_FLAG_NO_SS) != 0) { |
212 | 0 | int ret = X509_self_signed(cert, 0); |
213 | |
|
214 | 0 | if (ret != 0) |
215 | 0 | return ret > 0 ? 1 : 0; |
216 | 0 | } |
217 | 1.06k | if (!sk_X509_insert(sk, cert, |
218 | 1.06k | (flags & X509_ADD_FLAG_PREPEND) != 0 ? 0 : -1)) { |
219 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); |
220 | 0 | return 0; |
221 | 0 | } |
222 | 1.06k | if ((flags & X509_ADD_FLAG_UP_REF) != 0) |
223 | 1.06k | (void)X509_up_ref(cert); |
224 | 1.06k | return 1; |
225 | 1.06k | } |
226 | | |
227 | | int X509_add_certs(STACK_OF(X509) *sk, STACK_OF(X509) *certs, int flags) |
228 | | /* compiler would allow 'const' for the certs, yet they may get up-ref'ed */ |
229 | 9.78k | { |
230 | 9.78k | if (sk == NULL) { |
231 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER); |
232 | 0 | return 0; |
233 | 0 | } |
234 | 9.78k | return ossl_x509_add_certs_new(&sk, certs, flags); |
235 | 9.78k | } |
236 | | |
237 | | int ossl_x509_add_certs_new(STACK_OF(X509) **p_sk, STACK_OF(X509) *certs, |
238 | | int flags) |
239 | | /* compiler would allow 'const' for the certs, yet they may get up-ref'ed */ |
240 | 10.1k | { |
241 | 10.1k | int n = sk_X509_num(certs /* may be NULL */); |
242 | 10.1k | int i; |
243 | | |
244 | 12.2k | for (i = 0; i < n; i++) { |
245 | 2.12k | int j = (flags & X509_ADD_FLAG_PREPEND) == 0 ? i : n - 1 - i; |
246 | | /* if prepend, add certs in reverse order to keep original order */ |
247 | | |
248 | 2.12k | if (!ossl_x509_add_cert_new(p_sk, sk_X509_value(certs, j), flags)) |
249 | 0 | return 0; |
250 | 2.12k | } |
251 | 10.1k | return 1; |
252 | 10.1k | } |
253 | | |
254 | | int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b) |
255 | 813 | { |
256 | 813 | int ret; |
257 | | |
258 | 813 | if (b == NULL) |
259 | 0 | return a != NULL; |
260 | 813 | if (a == NULL) |
261 | 0 | return -1; |
262 | | |
263 | | /* Ensure canonical encoding is present and up to date */ |
264 | 813 | if (a->canon_enc == NULL || a->modified) { |
265 | 0 | ret = i2d_X509_NAME((X509_NAME *)a, NULL); |
266 | 0 | if (ret < 0) |
267 | 0 | return -2; |
268 | 0 | } |
269 | | |
270 | 813 | if (b->canon_enc == NULL || b->modified) { |
271 | 0 | ret = i2d_X509_NAME((X509_NAME *)b, NULL); |
272 | 0 | if (ret < 0) |
273 | 0 | return -2; |
274 | 0 | } |
275 | | |
276 | 813 | ret = a->canon_enclen - b->canon_enclen; |
277 | 813 | if (ret == 0 && a->canon_enclen == 0) |
278 | 0 | return 0; |
279 | | |
280 | 813 | if (a->canon_enc == NULL || b->canon_enc == NULL) |
281 | 0 | return -2; |
282 | | |
283 | 813 | if (ret == 0) |
284 | 394 | ret = memcmp(a->canon_enc, b->canon_enc, a->canon_enclen); |
285 | | |
286 | 813 | return ret < 0 ? -1 : ret > 0; |
287 | 813 | } |
288 | | |
289 | | unsigned long X509_NAME_hash_ex(const X509_NAME *x, OSSL_LIB_CTX *libctx, |
290 | | const char *propq, int *ok) |
291 | 0 | { |
292 | 0 | unsigned long ret = 0; |
293 | 0 | unsigned char md[SHA_DIGEST_LENGTH]; |
294 | 0 | EVP_MD *sha1 = EVP_MD_fetch(libctx, "SHA1", propq); |
295 | | |
296 | | /* Make sure X509_NAME structure contains valid cached encoding */ |
297 | 0 | i2d_X509_NAME(x, NULL); |
298 | 0 | if (ok != NULL) |
299 | 0 | *ok = 0; |
300 | 0 | if (sha1 != NULL |
301 | 0 | && EVP_Digest(x->canon_enc, x->canon_enclen, md, NULL, sha1, NULL)) { |
302 | 0 | ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) | |
303 | 0 | ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L) |
304 | 0 | ) & 0xffffffffL; |
305 | 0 | if (ok != NULL) |
306 | 0 | *ok = 1; |
307 | 0 | } |
308 | 0 | EVP_MD_free(sha1); |
309 | 0 | return ret; |
310 | 0 | } |
311 | | |
312 | | #ifndef OPENSSL_NO_MD5 |
313 | | /* |
314 | | * I now DER encode the name and hash it. Since I cache the DER encoding, |
315 | | * this is reasonably efficient. |
316 | | */ |
317 | | unsigned long X509_NAME_hash_old(const X509_NAME *x) |
318 | 0 | { |
319 | 0 | EVP_MD *md5 = EVP_MD_fetch(NULL, OSSL_DIGEST_NAME_MD5, "-fips"); |
320 | 0 | EVP_MD_CTX *md_ctx = EVP_MD_CTX_new(); |
321 | 0 | unsigned long ret = 0; |
322 | 0 | unsigned char md[16]; |
323 | |
|
324 | 0 | if (md5 == NULL || md_ctx == NULL) |
325 | 0 | goto end; |
326 | | |
327 | | /* Make sure X509_NAME structure contains valid cached encoding */ |
328 | 0 | i2d_X509_NAME(x, NULL); |
329 | 0 | if (EVP_DigestInit_ex(md_ctx, md5, NULL) |
330 | 0 | && EVP_DigestUpdate(md_ctx, x->bytes->data, x->bytes->length) |
331 | 0 | && EVP_DigestFinal_ex(md_ctx, md, NULL)) |
332 | 0 | ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) | |
333 | 0 | ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L) |
334 | 0 | ) & 0xffffffffL; |
335 | |
|
336 | 0 | end: |
337 | 0 | EVP_MD_CTX_free(md_ctx); |
338 | 0 | EVP_MD_free(md5); |
339 | |
|
340 | 0 | return ret; |
341 | 0 | } |
342 | | #endif |
343 | | |
344 | | /* Search a stack of X509 for a match */ |
345 | | X509 *X509_find_by_issuer_and_serial(STACK_OF(X509) *sk, const X509_NAME *name, |
346 | | const ASN1_INTEGER *serial) |
347 | 0 | { |
348 | 0 | int i; |
349 | 0 | X509 x, *x509 = NULL; |
350 | |
|
351 | 0 | if (!sk) |
352 | 0 | return NULL; |
353 | | |
354 | 0 | x.cert_info.serialNumber = *serial; |
355 | 0 | x.cert_info.issuer = (X509_NAME *)name; /* won't modify it */ |
356 | |
|
357 | 0 | for (i = 0; i < sk_X509_num(sk); i++) { |
358 | 0 | x509 = sk_X509_value(sk, i); |
359 | 0 | if (X509_issuer_and_serial_cmp(x509, &x) == 0) |
360 | 0 | return x509; |
361 | 0 | } |
362 | 0 | return NULL; |
363 | 0 | } |
364 | | |
365 | | X509 *X509_find_by_subject(STACK_OF(X509) *sk, const X509_NAME *name) |
366 | 0 | { |
367 | 0 | X509 *x509; |
368 | 0 | int i; |
369 | |
|
370 | 0 | for (i = 0; i < sk_X509_num(sk); i++) { |
371 | 0 | x509 = sk_X509_value(sk, i); |
372 | 0 | if (X509_NAME_cmp(X509_get_subject_name(x509), name) == 0) |
373 | 0 | return x509; |
374 | 0 | } |
375 | 0 | return NULL; |
376 | 0 | } |
377 | | |
378 | | EVP_PKEY *X509_get0_pubkey(const X509 *x) |
379 | 227 | { |
380 | 227 | if (x == NULL) |
381 | 0 | return NULL; |
382 | 227 | return X509_PUBKEY_get0(x->cert_info.key); |
383 | 227 | } |
384 | | |
385 | | EVP_PKEY *X509_get_pubkey(X509 *x) |
386 | 0 | { |
387 | 0 | if (x == NULL) |
388 | 0 | return NULL; |
389 | 0 | return X509_PUBKEY_get(x->cert_info.key); |
390 | 0 | } |
391 | | |
392 | | int X509_check_private_key(const X509 *x, const EVP_PKEY *k) |
393 | 0 | { |
394 | 0 | const EVP_PKEY *xk; |
395 | 0 | int ret; |
396 | |
|
397 | 0 | xk = X509_get0_pubkey(x); |
398 | 0 | if (xk == NULL) { |
399 | 0 | ERR_raise(ERR_LIB_X509, X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY); |
400 | 0 | return 0; |
401 | 0 | } |
402 | | |
403 | 0 | switch (ret = EVP_PKEY_eq(xk, k)) { |
404 | 0 | case 0: |
405 | 0 | ERR_raise(ERR_LIB_X509, X509_R_KEY_VALUES_MISMATCH); |
406 | 0 | break; |
407 | 0 | case -1: |
408 | 0 | ERR_raise(ERR_LIB_X509, X509_R_KEY_TYPE_MISMATCH); |
409 | 0 | break; |
410 | 0 | case -2: |
411 | 0 | ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_KEY_TYPE); |
412 | 0 | break; |
413 | 0 | } |
414 | | |
415 | 0 | return ret > 0; |
416 | 0 | } |
417 | | |
418 | | /* |
419 | | * Check a suite B algorithm is permitted: pass in a public key and the NID |
420 | | * of its signature (or 0 if no signature). The pflags is a pointer to a |
421 | | * flags field which must contain the suite B verification flags. |
422 | | */ |
423 | | |
424 | | #ifndef OPENSSL_NO_EC |
425 | | |
426 | | static int check_suite_b(EVP_PKEY *pkey, int sign_nid, unsigned long *pflags) |
427 | 0 | { |
428 | 0 | char curve_name[80]; |
429 | 0 | size_t curve_name_len; |
430 | 0 | int curve_nid; |
431 | |
|
432 | 0 | if (pkey == NULL || !EVP_PKEY_is_a(pkey, "EC")) |
433 | 0 | return X509_V_ERR_SUITE_B_INVALID_ALGORITHM; |
434 | | |
435 | 0 | if (!EVP_PKEY_get_group_name(pkey, curve_name, sizeof(curve_name), |
436 | 0 | &curve_name_len)) |
437 | 0 | return X509_V_ERR_SUITE_B_INVALID_CURVE; |
438 | | |
439 | 0 | curve_nid = OBJ_txt2nid(curve_name); |
440 | | /* Check curve is consistent with LOS */ |
441 | 0 | if (curve_nid == NID_secp384r1) { /* P-384 */ |
442 | | /* |
443 | | * Check signature algorithm is consistent with curve. |
444 | | */ |
445 | 0 | if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA384) |
446 | 0 | return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM; |
447 | 0 | if (!(*pflags & X509_V_FLAG_SUITEB_192_LOS)) |
448 | 0 | return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED; |
449 | | /* If we encounter P-384 we cannot use P-256 later */ |
450 | 0 | *pflags &= ~X509_V_FLAG_SUITEB_128_LOS_ONLY; |
451 | 0 | } else if (curve_nid == NID_X9_62_prime256v1) { /* P-256 */ |
452 | 0 | if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA256) |
453 | 0 | return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM; |
454 | 0 | if (!(*pflags & X509_V_FLAG_SUITEB_128_LOS_ONLY)) |
455 | 0 | return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED; |
456 | 0 | } else { |
457 | 0 | return X509_V_ERR_SUITE_B_INVALID_CURVE; |
458 | 0 | } |
459 | 0 | return X509_V_OK; |
460 | 0 | } |
461 | | |
462 | | int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain, |
463 | | unsigned long flags) |
464 | 0 | { |
465 | 0 | int rv, i, sign_nid; |
466 | 0 | EVP_PKEY *pk; |
467 | 0 | unsigned long tflags = flags; |
468 | |
|
469 | 0 | if (!(flags & X509_V_FLAG_SUITEB_128_LOS)) |
470 | 0 | return X509_V_OK; |
471 | | |
472 | | /* If no EE certificate passed in must be first in chain */ |
473 | 0 | if (x == NULL) { |
474 | 0 | x = sk_X509_value(chain, 0); |
475 | 0 | i = 1; |
476 | 0 | } else { |
477 | 0 | i = 0; |
478 | 0 | } |
479 | 0 | pk = X509_get0_pubkey(x); |
480 | | |
481 | | /* |
482 | | * With DANE-EE(3) success, or DANE-EE(3)/PKIX-EE(1) failure we don't build |
483 | | * a chain all, just report trust success or failure, but must also report |
484 | | * Suite-B errors if applicable. This is indicated via a NULL chain |
485 | | * pointer. All we need to do is check the leaf key algorithm. |
486 | | */ |
487 | 0 | if (chain == NULL) |
488 | 0 | return check_suite_b(pk, -1, &tflags); |
489 | | |
490 | 0 | if (X509_get_version(x) != X509_VERSION_3) { |
491 | 0 | rv = X509_V_ERR_SUITE_B_INVALID_VERSION; |
492 | | /* Correct error depth */ |
493 | 0 | i = 0; |
494 | 0 | goto end; |
495 | 0 | } |
496 | | |
497 | | /* Check EE key only */ |
498 | 0 | rv = check_suite_b(pk, -1, &tflags); |
499 | 0 | if (rv != X509_V_OK) { |
500 | | /* Correct error depth */ |
501 | 0 | i = 0; |
502 | 0 | goto end; |
503 | 0 | } |
504 | 0 | for (; i < sk_X509_num(chain); i++) { |
505 | 0 | sign_nid = X509_get_signature_nid(x); |
506 | 0 | x = sk_X509_value(chain, i); |
507 | 0 | if (X509_get_version(x) != X509_VERSION_3) { |
508 | 0 | rv = X509_V_ERR_SUITE_B_INVALID_VERSION; |
509 | 0 | goto end; |
510 | 0 | } |
511 | 0 | pk = X509_get0_pubkey(x); |
512 | 0 | rv = check_suite_b(pk, sign_nid, &tflags); |
513 | 0 | if (rv != X509_V_OK) |
514 | 0 | goto end; |
515 | 0 | } |
516 | | |
517 | | /* Final check: root CA signature */ |
518 | 0 | rv = check_suite_b(pk, X509_get_signature_nid(x), &tflags); |
519 | 0 | end: |
520 | 0 | if (rv != X509_V_OK) { |
521 | | /* Invalid signature or LOS errors are for previous cert */ |
522 | 0 | if ((rv == X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM |
523 | 0 | || rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED) && i) |
524 | 0 | i--; |
525 | | /* |
526 | | * If we have LOS error and flags changed then we are signing P-384 |
527 | | * with P-256. Use more meaningful error. |
528 | | */ |
529 | 0 | if (rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED && flags != tflags) |
530 | 0 | rv = X509_V_ERR_SUITE_B_CANNOT_SIGN_P_384_WITH_P_256; |
531 | 0 | if (perror_depth) |
532 | 0 | *perror_depth = i; |
533 | 0 | } |
534 | 0 | return rv; |
535 | 0 | } |
536 | | |
537 | | int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags) |
538 | 0 | { |
539 | 0 | int sign_nid; |
540 | 0 | if (!(flags & X509_V_FLAG_SUITEB_128_LOS)) |
541 | 0 | return X509_V_OK; |
542 | 0 | sign_nid = OBJ_obj2nid(crl->crl.sig_alg.algorithm); |
543 | 0 | return check_suite_b(pk, sign_nid, &flags); |
544 | 0 | } |
545 | | |
546 | | #else |
547 | | int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain, |
548 | | unsigned long flags) |
549 | | { |
550 | | return 0; |
551 | | } |
552 | | |
553 | | int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags) |
554 | | { |
555 | | return 0; |
556 | | } |
557 | | |
558 | | #endif |
559 | | |
560 | | /* |
561 | | * Not strictly speaking an "up_ref" as a STACK doesn't have a reference |
562 | | * count but it has the same effect by duping the STACK and upping the ref of |
563 | | * each X509 structure. |
564 | | */ |
565 | | STACK_OF(X509) *X509_chain_up_ref(STACK_OF(X509) *chain) |
566 | 0 | { |
567 | 0 | STACK_OF(X509) *ret = sk_X509_dup(chain); |
568 | 0 | int i; |
569 | |
|
570 | 0 | if (ret == NULL) |
571 | 0 | return NULL; |
572 | 0 | for (i = 0; i < sk_X509_num(ret); i++) { |
573 | 0 | X509 *x = sk_X509_value(ret, i); |
574 | |
|
575 | 0 | if (!X509_up_ref(x)) |
576 | 0 | goto err; |
577 | 0 | } |
578 | 0 | return ret; |
579 | | |
580 | 0 | err: |
581 | 0 | while (i-- > 0) |
582 | 0 | X509_free(sk_X509_value(ret, i)); |
583 | 0 | sk_X509_free(ret); |
584 | 0 | return NULL; |
585 | 0 | } |