/src/openssl111/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 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 "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 "crypto/x509.h" |
17 | | |
18 | | int X509_issuer_and_serial_cmp(const X509 *a, const X509 *b) |
19 | 0 | { |
20 | 0 | int i; |
21 | 0 | const X509_CINF *ai, *bi; |
22 | |
|
23 | 0 | ai = &a->cert_info; |
24 | 0 | bi = &b->cert_info; |
25 | 0 | i = ASN1_INTEGER_cmp(&ai->serialNumber, &bi->serialNumber); |
26 | 0 | if (i) |
27 | 0 | return i; |
28 | 0 | return X509_NAME_cmp(ai->issuer, bi->issuer); |
29 | 0 | } |
30 | | |
31 | | #ifndef OPENSSL_NO_MD5 |
32 | | unsigned long X509_issuer_and_serial_hash(X509 *a) |
33 | 5.11k | { |
34 | 5.11k | unsigned long ret = 0; |
35 | 5.11k | EVP_MD_CTX *ctx = EVP_MD_CTX_new(); |
36 | 5.11k | unsigned char md[16]; |
37 | 5.11k | char *f = NULL; |
38 | | |
39 | 5.11k | if (ctx == NULL) |
40 | 0 | goto err; |
41 | 5.11k | f = X509_NAME_oneline(a->cert_info.issuer, NULL, 0); |
42 | 5.11k | if (f == NULL) |
43 | 29 | goto err; |
44 | 5.08k | if (!EVP_DigestInit_ex(ctx, EVP_md5(), NULL)) |
45 | 0 | goto err; |
46 | 5.08k | if (!EVP_DigestUpdate(ctx, (unsigned char *)f, strlen(f))) |
47 | 0 | goto err; |
48 | 5.08k | if (!EVP_DigestUpdate |
49 | 5.08k | (ctx, (unsigned char *)a->cert_info.serialNumber.data, |
50 | 5.08k | (unsigned long)a->cert_info.serialNumber.length)) |
51 | 0 | goto err; |
52 | 5.08k | if (!EVP_DigestFinal_ex(ctx, &(md[0]), NULL)) |
53 | 0 | goto err; |
54 | 5.08k | ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) | |
55 | 5.08k | ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L) |
56 | 5.08k | ) & 0xffffffffL; |
57 | 5.11k | err: |
58 | 5.11k | OPENSSL_free(f); |
59 | 5.11k | EVP_MD_CTX_free(ctx); |
60 | 5.11k | return ret; |
61 | 5.08k | } |
62 | | #endif |
63 | | |
64 | | int X509_issuer_name_cmp(const X509 *a, const X509 *b) |
65 | 0 | { |
66 | 0 | return X509_NAME_cmp(a->cert_info.issuer, b->cert_info.issuer); |
67 | 0 | } |
68 | | |
69 | | int X509_subject_name_cmp(const X509 *a, const X509 *b) |
70 | 0 | { |
71 | 0 | return X509_NAME_cmp(a->cert_info.subject, b->cert_info.subject); |
72 | 0 | } |
73 | | |
74 | | int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b) |
75 | 0 | { |
76 | 0 | return X509_NAME_cmp(a->crl.issuer, b->crl.issuer); |
77 | 0 | } |
78 | | |
79 | | int X509_CRL_match(const X509_CRL *a, const X509_CRL *b) |
80 | 0 | { |
81 | 0 | return memcmp(a->sha1_hash, b->sha1_hash, 20); |
82 | 0 | } |
83 | | |
84 | | X509_NAME *X509_get_issuer_name(const X509 *a) |
85 | 5.11k | { |
86 | 5.11k | return a->cert_info.issuer; |
87 | 5.11k | } |
88 | | |
89 | | unsigned long X509_issuer_name_hash(X509 *x) |
90 | 0 | { |
91 | 0 | return X509_NAME_hash(x->cert_info.issuer); |
92 | 0 | } |
93 | | |
94 | | #ifndef OPENSSL_NO_MD5 |
95 | | unsigned long X509_issuer_name_hash_old(X509 *x) |
96 | 0 | { |
97 | 0 | return X509_NAME_hash_old(x->cert_info.issuer); |
98 | 0 | } |
99 | | #endif |
100 | | |
101 | | X509_NAME *X509_get_subject_name(const X509 *a) |
102 | 3.62k | { |
103 | 3.62k | return a->cert_info.subject; |
104 | 3.62k | } |
105 | | |
106 | | ASN1_INTEGER *X509_get_serialNumber(X509 *a) |
107 | 5.11k | { |
108 | 5.11k | return &a->cert_info.serialNumber; |
109 | 5.11k | } |
110 | | |
111 | | const ASN1_INTEGER *X509_get0_serialNumber(const X509 *a) |
112 | 0 | { |
113 | 0 | return &a->cert_info.serialNumber; |
114 | 0 | } |
115 | | |
116 | | unsigned long X509_subject_name_hash(X509 *x) |
117 | 0 | { |
118 | 0 | return X509_NAME_hash(x->cert_info.subject); |
119 | 0 | } |
120 | | |
121 | | #ifndef OPENSSL_NO_MD5 |
122 | | unsigned long X509_subject_name_hash_old(X509 *x) |
123 | 0 | { |
124 | 0 | return X509_NAME_hash_old(x->cert_info.subject); |
125 | 0 | } |
126 | | #endif |
127 | | |
128 | | /* |
129 | | * Compare two certificates: they must be identical for this to work. NB: |
130 | | * Although "cmp" operations are generally prototyped to take "const" |
131 | | * arguments (eg. for use in STACKs), the way X509 handling is - these |
132 | | * operations may involve ensuring the hashes are up-to-date and ensuring |
133 | | * certain cert information is cached. So this is the point where the |
134 | | * "depth-first" constification tree has to halt with an evil cast. |
135 | | */ |
136 | | int X509_cmp(const X509 *a, const X509 *b) |
137 | 0 | { |
138 | 0 | int rv = 0; |
139 | |
|
140 | 0 | if (a == b) /* for efficiency */ |
141 | 0 | return 0; |
142 | | |
143 | | /* try to make sure hash is valid */ |
144 | 0 | (void)X509_check_purpose((X509 *)a, -1, 0); |
145 | 0 | (void)X509_check_purpose((X509 *)b, -1, 0); |
146 | |
|
147 | 0 | if ((a->ex_flags & EXFLAG_NO_FINGERPRINT) == 0 |
148 | 0 | && (b->ex_flags & EXFLAG_NO_FINGERPRINT) == 0) |
149 | 0 | rv = memcmp(a->sha1_hash, b->sha1_hash, SHA_DIGEST_LENGTH); |
150 | 0 | if (rv != 0) |
151 | 0 | return rv; |
152 | | |
153 | | /* Check for match against stored encoding too */ |
154 | 0 | if (!a->cert_info.enc.modified && !b->cert_info.enc.modified) { |
155 | 0 | if (a->cert_info.enc.len < b->cert_info.enc.len) |
156 | 0 | return -1; |
157 | 0 | if (a->cert_info.enc.len > b->cert_info.enc.len) |
158 | 0 | return 1; |
159 | 0 | return memcmp(a->cert_info.enc.enc, b->cert_info.enc.enc, |
160 | 0 | a->cert_info.enc.len); |
161 | 0 | } |
162 | 0 | return rv; |
163 | 0 | } |
164 | | |
165 | | int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b) |
166 | 0 | { |
167 | 0 | int ret; |
168 | | |
169 | | /* Ensure canonical encoding is present and up to date */ |
170 | |
|
171 | 0 | if (!a->canon_enc || a->modified) { |
172 | 0 | ret = i2d_X509_NAME((X509_NAME *)a, NULL); |
173 | 0 | if (ret < 0) |
174 | 0 | return -2; |
175 | 0 | } |
176 | | |
177 | 0 | if (!b->canon_enc || b->modified) { |
178 | 0 | ret = i2d_X509_NAME((X509_NAME *)b, NULL); |
179 | 0 | if (ret < 0) |
180 | 0 | return -2; |
181 | 0 | } |
182 | | |
183 | 0 | ret = a->canon_enclen - b->canon_enclen; |
184 | |
|
185 | 0 | if (ret != 0 || a->canon_enclen == 0) |
186 | 0 | return ret; |
187 | | |
188 | 0 | return memcmp(a->canon_enc, b->canon_enc, a->canon_enclen); |
189 | |
|
190 | 0 | } |
191 | | |
192 | | unsigned long X509_NAME_hash(X509_NAME *x) |
193 | 0 | { |
194 | 0 | unsigned long ret = 0; |
195 | 0 | unsigned char md[SHA_DIGEST_LENGTH]; |
196 | | |
197 | | /* Make sure X509_NAME structure contains valid cached encoding */ |
198 | 0 | i2d_X509_NAME(x, NULL); |
199 | 0 | if (!EVP_Digest(x->canon_enc, x->canon_enclen, md, NULL, EVP_sha1(), |
200 | 0 | NULL)) |
201 | 0 | return 0; |
202 | | |
203 | 0 | ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) | |
204 | 0 | ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L) |
205 | 0 | ) & 0xffffffffL; |
206 | 0 | return ret; |
207 | 0 | } |
208 | | |
209 | | #ifndef OPENSSL_NO_MD5 |
210 | | /* |
211 | | * I now DER encode the name and hash it. Since I cache the DER encoding, |
212 | | * this is reasonably efficient. |
213 | | */ |
214 | | |
215 | | unsigned long X509_NAME_hash_old(X509_NAME *x) |
216 | 0 | { |
217 | 0 | EVP_MD_CTX *md_ctx = EVP_MD_CTX_new(); |
218 | 0 | unsigned long ret = 0; |
219 | 0 | unsigned char md[16]; |
220 | |
|
221 | 0 | if (md_ctx == NULL) |
222 | 0 | return ret; |
223 | | |
224 | | /* Make sure X509_NAME structure contains valid cached encoding */ |
225 | 0 | i2d_X509_NAME(x, NULL); |
226 | 0 | EVP_MD_CTX_set_flags(md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW); |
227 | 0 | if (EVP_DigestInit_ex(md_ctx, EVP_md5(), NULL) |
228 | 0 | && EVP_DigestUpdate(md_ctx, x->bytes->data, x->bytes->length) |
229 | 0 | && EVP_DigestFinal_ex(md_ctx, md, NULL)) |
230 | 0 | ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) | |
231 | 0 | ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L) |
232 | 0 | ) & 0xffffffffL; |
233 | 0 | EVP_MD_CTX_free(md_ctx); |
234 | |
|
235 | 0 | return ret; |
236 | 0 | } |
237 | | #endif |
238 | | |
239 | | /* Search a stack of X509 for a match */ |
240 | | X509 *X509_find_by_issuer_and_serial(STACK_OF(X509) *sk, X509_NAME *name, |
241 | | ASN1_INTEGER *serial) |
242 | 0 | { |
243 | 0 | int i; |
244 | 0 | X509 x, *x509 = NULL; |
245 | |
|
246 | 0 | if (!sk) |
247 | 0 | return NULL; |
248 | | |
249 | 0 | x.cert_info.serialNumber = *serial; |
250 | 0 | x.cert_info.issuer = name; |
251 | |
|
252 | 0 | for (i = 0; i < sk_X509_num(sk); i++) { |
253 | 0 | x509 = sk_X509_value(sk, i); |
254 | 0 | if (X509_issuer_and_serial_cmp(x509, &x) == 0) |
255 | 0 | return x509; |
256 | 0 | } |
257 | 0 | return NULL; |
258 | 0 | } |
259 | | |
260 | | X509 *X509_find_by_subject(STACK_OF(X509) *sk, X509_NAME *name) |
261 | 0 | { |
262 | 0 | X509 *x509; |
263 | 0 | int i; |
264 | |
|
265 | 0 | for (i = 0; i < sk_X509_num(sk); i++) { |
266 | 0 | x509 = sk_X509_value(sk, i); |
267 | 0 | if (X509_NAME_cmp(X509_get_subject_name(x509), name) == 0) |
268 | 0 | return x509; |
269 | 0 | } |
270 | 0 | return NULL; |
271 | 0 | } |
272 | | |
273 | | EVP_PKEY *X509_get0_pubkey(const X509 *x) |
274 | 3.62k | { |
275 | 3.62k | if (x == NULL) |
276 | 0 | return NULL; |
277 | 3.62k | return X509_PUBKEY_get0(x->cert_info.key); |
278 | 3.62k | } |
279 | | |
280 | | EVP_PKEY *X509_get_pubkey(X509 *x) |
281 | 0 | { |
282 | 0 | if (x == NULL) |
283 | 0 | return NULL; |
284 | 0 | return X509_PUBKEY_get(x->cert_info.key); |
285 | 0 | } |
286 | | |
287 | | int X509_check_private_key(const X509 *x, const EVP_PKEY *k) |
288 | 0 | { |
289 | 0 | const EVP_PKEY *xk; |
290 | 0 | int ret; |
291 | |
|
292 | 0 | xk = X509_get0_pubkey(x); |
293 | |
|
294 | 0 | if (xk) |
295 | 0 | ret = EVP_PKEY_cmp(xk, k); |
296 | 0 | else |
297 | 0 | ret = -2; |
298 | |
|
299 | 0 | switch (ret) { |
300 | 0 | case 1: |
301 | 0 | break; |
302 | 0 | case 0: |
303 | 0 | X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_KEY_VALUES_MISMATCH); |
304 | 0 | break; |
305 | 0 | case -1: |
306 | 0 | X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_KEY_TYPE_MISMATCH); |
307 | 0 | break; |
308 | 0 | case -2: |
309 | 0 | X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_UNKNOWN_KEY_TYPE); |
310 | 0 | } |
311 | 0 | if (ret > 0) |
312 | 0 | return 1; |
313 | 0 | return 0; |
314 | 0 | } |
315 | | |
316 | | /* |
317 | | * Check a suite B algorithm is permitted: pass in a public key and the NID |
318 | | * of its signature (or 0 if no signature). The pflags is a pointer to a |
319 | | * flags field which must contain the suite B verification flags. |
320 | | */ |
321 | | |
322 | | #ifndef OPENSSL_NO_EC |
323 | | |
324 | | static int check_suite_b(EVP_PKEY *pkey, int sign_nid, unsigned long *pflags) |
325 | 0 | { |
326 | 0 | const EC_GROUP *grp = NULL; |
327 | 0 | int curve_nid; |
328 | 0 | if (pkey && EVP_PKEY_id(pkey) == EVP_PKEY_EC) |
329 | 0 | grp = EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey)); |
330 | 0 | if (!grp) |
331 | 0 | return X509_V_ERR_SUITE_B_INVALID_ALGORITHM; |
332 | 0 | curve_nid = EC_GROUP_get_curve_name(grp); |
333 | | /* Check curve is consistent with LOS */ |
334 | 0 | if (curve_nid == NID_secp384r1) { /* P-384 */ |
335 | | /* |
336 | | * Check signature algorithm is consistent with curve. |
337 | | */ |
338 | 0 | if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA384) |
339 | 0 | return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM; |
340 | 0 | if (!(*pflags & X509_V_FLAG_SUITEB_192_LOS)) |
341 | 0 | return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED; |
342 | | /* If we encounter P-384 we cannot use P-256 later */ |
343 | 0 | *pflags &= ~X509_V_FLAG_SUITEB_128_LOS_ONLY; |
344 | 0 | } else if (curve_nid == NID_X9_62_prime256v1) { /* P-256 */ |
345 | 0 | if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA256) |
346 | 0 | return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM; |
347 | 0 | if (!(*pflags & X509_V_FLAG_SUITEB_128_LOS_ONLY)) |
348 | 0 | return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED; |
349 | 0 | } else |
350 | 0 | return X509_V_ERR_SUITE_B_INVALID_CURVE; |
351 | | |
352 | 0 | return X509_V_OK; |
353 | 0 | } |
354 | | |
355 | | int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain, |
356 | | unsigned long flags) |
357 | 0 | { |
358 | 0 | int rv, i, sign_nid; |
359 | 0 | EVP_PKEY *pk; |
360 | 0 | unsigned long tflags = flags; |
361 | |
|
362 | 0 | if (!(flags & X509_V_FLAG_SUITEB_128_LOS)) |
363 | 0 | return X509_V_OK; |
364 | | |
365 | | /* If no EE certificate passed in must be first in chain */ |
366 | 0 | if (x == NULL) { |
367 | 0 | x = sk_X509_value(chain, 0); |
368 | 0 | i = 1; |
369 | 0 | } else |
370 | 0 | i = 0; |
371 | |
|
372 | 0 | pk = X509_get0_pubkey(x); |
373 | | |
374 | | /* |
375 | | * With DANE-EE(3) success, or DANE-EE(3)/PKIX-EE(1) failure we don't build |
376 | | * a chain all, just report trust success or failure, but must also report |
377 | | * Suite-B errors if applicable. This is indicated via a NULL chain |
378 | | * pointer. All we need to do is check the leaf key algorithm. |
379 | | */ |
380 | 0 | if (chain == NULL) |
381 | 0 | return check_suite_b(pk, -1, &tflags); |
382 | | |
383 | 0 | if (X509_get_version(x) != 2) { |
384 | 0 | rv = X509_V_ERR_SUITE_B_INVALID_VERSION; |
385 | | /* Correct error depth */ |
386 | 0 | i = 0; |
387 | 0 | goto end; |
388 | 0 | } |
389 | | |
390 | | /* Check EE key only */ |
391 | 0 | rv = check_suite_b(pk, -1, &tflags); |
392 | 0 | if (rv != X509_V_OK) { |
393 | | /* Correct error depth */ |
394 | 0 | i = 0; |
395 | 0 | goto end; |
396 | 0 | } |
397 | 0 | for (; i < sk_X509_num(chain); i++) { |
398 | 0 | sign_nid = X509_get_signature_nid(x); |
399 | 0 | x = sk_X509_value(chain, i); |
400 | 0 | if (X509_get_version(x) != 2) { |
401 | 0 | rv = X509_V_ERR_SUITE_B_INVALID_VERSION; |
402 | 0 | goto end; |
403 | 0 | } |
404 | 0 | pk = X509_get0_pubkey(x); |
405 | 0 | rv = check_suite_b(pk, sign_nid, &tflags); |
406 | 0 | if (rv != X509_V_OK) |
407 | 0 | goto end; |
408 | 0 | } |
409 | | |
410 | | /* Final check: root CA signature */ |
411 | 0 | rv = check_suite_b(pk, X509_get_signature_nid(x), &tflags); |
412 | 0 | end: |
413 | 0 | if (rv != X509_V_OK) { |
414 | | /* Invalid signature or LOS errors are for previous cert */ |
415 | 0 | if ((rv == X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM |
416 | 0 | || rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED) && i) |
417 | 0 | i--; |
418 | | /* |
419 | | * If we have LOS error and flags changed then we are signing P-384 |
420 | | * with P-256. Use more meaningful error. |
421 | | */ |
422 | 0 | if (rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED && flags != tflags) |
423 | 0 | rv = X509_V_ERR_SUITE_B_CANNOT_SIGN_P_384_WITH_P_256; |
424 | 0 | if (perror_depth) |
425 | 0 | *perror_depth = i; |
426 | 0 | } |
427 | 0 | return rv; |
428 | 0 | } |
429 | | |
430 | | int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags) |
431 | 0 | { |
432 | 0 | int sign_nid; |
433 | 0 | if (!(flags & X509_V_FLAG_SUITEB_128_LOS)) |
434 | 0 | return X509_V_OK; |
435 | 0 | sign_nid = OBJ_obj2nid(crl->crl.sig_alg.algorithm); |
436 | 0 | return check_suite_b(pk, sign_nid, &flags); |
437 | 0 | } |
438 | | |
439 | | #else |
440 | | int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain, |
441 | | unsigned long flags) |
442 | | { |
443 | | return 0; |
444 | | } |
445 | | |
446 | | int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags) |
447 | | { |
448 | | return 0; |
449 | | } |
450 | | |
451 | | #endif |
452 | | /* |
453 | | * Not strictly speaking an "up_ref" as a STACK doesn't have a reference |
454 | | * count but it has the same effect by duping the STACK and upping the ref of |
455 | | * each X509 structure. |
456 | | */ |
457 | | STACK_OF(X509) *X509_chain_up_ref(STACK_OF(X509) *chain) |
458 | 0 | { |
459 | 0 | STACK_OF(X509) *ret; |
460 | 0 | int i; |
461 | 0 | ret = sk_X509_dup(chain); |
462 | 0 | if (ret == NULL) |
463 | 0 | return NULL; |
464 | 0 | for (i = 0; i < sk_X509_num(ret); i++) { |
465 | 0 | X509 *x = sk_X509_value(ret, i); |
466 | 0 | if (!X509_up_ref(x)) |
467 | 0 | goto err; |
468 | 0 | } |
469 | 0 | return ret; |
470 | 0 | err: |
471 | 0 | while (i-- > 0) |
472 | 0 | X509_free (sk_X509_value(ret, i)); |
473 | 0 | sk_X509_free(ret); |
474 | 0 | return NULL; |
475 | 0 | } |