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