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