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