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