2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
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
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"
19 int X509_issuer_and_serial_cmp(const X509 *a, const X509 *b)
22 const X509_CINF *ai, *bi;
30 i = ASN1_INTEGER_cmp(&ai->serialNumber, &bi->serialNumber);
32 return i < 0 ? -1 : 1;
33 return X509_NAME_cmp(ai->issuer, bi->issuer);
36 #ifndef OPENSSL_NO_MD5
37 unsigned long X509_issuer_and_serial_hash(X509 *a)
39 unsigned long ret = 0;
40 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
46 f = X509_NAME_oneline(a->cert_info.issuer, NULL, 0);
47 if (!EVP_DigestInit_ex(ctx, EVP_md5(), NULL))
49 if (!EVP_DigestUpdate(ctx, (unsigned char *)f, strlen(f)))
53 (ctx, (unsigned char *)a->cert_info.serialNumber.data,
54 (unsigned long)a->cert_info.serialNumber.length))
56 if (!EVP_DigestFinal_ex(ctx, &(md[0]), NULL))
58 ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
59 ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
67 int X509_issuer_name_cmp(const X509 *a, const X509 *b)
69 return X509_NAME_cmp(a->cert_info.issuer, b->cert_info.issuer);
72 int X509_subject_name_cmp(const X509 *a, const X509 *b)
74 return X509_NAME_cmp(a->cert_info.subject, b->cert_info.subject);
77 int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b)
79 return X509_NAME_cmp(a->crl.issuer, b->crl.issuer);
82 int X509_CRL_match(const X509_CRL *a, const X509_CRL *b)
84 int rv = memcmp(a->sha1_hash, b->sha1_hash, 20);
86 return rv < 0 ? -1 : rv > 0;
89 X509_NAME *X509_get_issuer_name(const X509 *a)
91 return a->cert_info.issuer;
94 unsigned long X509_issuer_name_hash(X509 *x)
96 return X509_NAME_hash(x->cert_info.issuer);
99 #ifndef OPENSSL_NO_MD5
100 unsigned long X509_issuer_name_hash_old(X509 *x)
102 return X509_NAME_hash_old(x->cert_info.issuer);
106 X509_NAME *X509_get_subject_name(const X509 *a)
108 return a->cert_info.subject;
111 ASN1_INTEGER *X509_get_serialNumber(X509 *a)
113 return &a->cert_info.serialNumber;
116 const ASN1_INTEGER *X509_get0_serialNumber(const X509 *a)
118 return &a->cert_info.serialNumber;
121 unsigned long X509_subject_name_hash(X509 *x)
123 return X509_NAME_hash(x->cert_info.subject);
126 #ifndef OPENSSL_NO_MD5
127 unsigned long X509_subject_name_hash_old(X509 *x)
129 return X509_NAME_hash_old(x->cert_info.subject);
134 * Compare two certificates: they must be identical for this to work. NB:
135 * Although "cmp" operations are generally prototyped to take "const"
136 * arguments (eg. for use in STACKs), the way X509 handling is - these
137 * operations may involve ensuring the hashes are up-to-date and ensuring
138 * certain cert information is cached. So this is the point where the
139 * "depth-first" constification tree has to halt with an evil cast.
141 int X509_cmp(const X509 *a, const X509 *b)
145 if (a == b) /* for efficiency */
147 /* ensure hash is valid */
148 if (X509_check_purpose((X509 *)a, -1, 0) != 1)
150 if (X509_check_purpose((X509 *)b, -1, 0) != 1)
153 rv = memcmp(a->sha1_hash, b->sha1_hash, SHA_DIGEST_LENGTH);
155 return rv < 0 ? -1 : 1;
156 /* Check for match against stored encoding too */
157 if (!a->cert_info.enc.modified && !b->cert_info.enc.modified) {
158 if (a->cert_info.enc.len < b->cert_info.enc.len)
160 if (a->cert_info.enc.len > b->cert_info.enc.len)
162 rv = memcmp(a->cert_info.enc.enc,
163 b->cert_info.enc.enc, a->cert_info.enc.len);
165 return rv < 0 ? -1 : rv > 0;
168 int X509_add_cert_new(STACK_OF(X509) **sk, X509 *cert, int flags)
171 && (*sk = sk_X509_new_null()) == NULL) {
172 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
175 return X509_add_cert(*sk, cert, flags);
178 int X509_add_cert(STACK_OF(X509) *sk, X509 *cert, int flags)
181 ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
184 if ((flags & X509_ADD_FLAG_NO_DUP) != 0) {
186 * not using sk_X509_set_cmp_func() and sk_X509_find()
187 * because this re-orders the certs on the stack
191 for (i = 0; i < sk_X509_num(sk); i++) {
192 if (X509_cmp(sk_X509_value(sk, i), cert) == 0)
196 if ((flags & X509_ADD_FLAG_NO_SS) != 0 && X509_self_signed(cert, 0))
198 if (!sk_X509_insert(sk, cert,
199 (flags & X509_ADD_FLAG_PREPEND) != 0 ? 0 : -1)) {
200 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
203 if ((flags & X509_ADD_FLAG_UP_REF) != 0)
204 (void)X509_up_ref(cert);
208 int X509_add_certs(STACK_OF(X509) *sk, STACK_OF(X509) *certs, int flags)
209 /* compiler would allow 'const' for the list of certs, yet they are up-ref'ed */
211 int n = sk_X509_num(certs); /* certs may be NULL */
214 for (i = 0; i < n; i++) {
215 int j = (flags & X509_ADD_FLAG_PREPEND) == 0 ? i : n - 1 - i;
216 /* if prepend, add certs in reverse order to keep original order */
218 if (!X509_add_cert(sk, sk_X509_value(certs, j), flags))
224 int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b)
233 /* Ensure canonical encoding is present and up to date */
234 if (!a->canon_enc || a->modified) {
235 ret = i2d_X509_NAME((X509_NAME *)a, NULL);
240 if (!b->canon_enc || b->modified) {
241 ret = i2d_X509_NAME((X509_NAME *)b, NULL);
246 ret = a->canon_enclen - b->canon_enclen;
247 if (ret == 0 && a->canon_enclen != 0)
248 ret = memcmp(a->canon_enc, b->canon_enc, a->canon_enclen);
250 return ret < 0 ? -1 : ret > 0;
253 unsigned long X509_NAME_hash(const X509_NAME *x)
255 unsigned long ret = 0;
256 unsigned char md[SHA_DIGEST_LENGTH];
258 /* Make sure X509_NAME structure contains valid cached encoding */
259 i2d_X509_NAME(x, NULL);
260 if (!EVP_Digest(x->canon_enc, x->canon_enclen, md, NULL, EVP_sha1(),
264 ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
265 ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
270 #ifndef OPENSSL_NO_MD5
272 * I now DER encode the name and hash it. Since I cache the DER encoding,
273 * this is reasonably efficient.
276 unsigned long X509_NAME_hash_old(const X509_NAME *x)
278 EVP_MD *md5 = EVP_MD_fetch(NULL, OSSL_DIGEST_NAME_MD5, "-fips");
279 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
280 unsigned long ret = 0;
281 unsigned char md[16];
283 if (md5 == NULL || md_ctx == NULL)
286 /* Make sure X509_NAME structure contains valid cached encoding */
287 i2d_X509_NAME(x, NULL);
288 if (EVP_DigestInit_ex(md_ctx, md5, NULL)
289 && EVP_DigestUpdate(md_ctx, x->bytes->data, x->bytes->length)
290 && EVP_DigestFinal_ex(md_ctx, md, NULL))
291 ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
292 ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
296 EVP_MD_CTX_free(md_ctx);
303 /* Search a stack of X509 for a match */
304 X509 *X509_find_by_issuer_and_serial(STACK_OF(X509) *sk, const X509_NAME *name,
305 const ASN1_INTEGER *serial)
308 X509 x, *x509 = NULL;
313 x.cert_info.serialNumber = *serial;
314 x.cert_info.issuer = (X509_NAME *)name; /* won't modify it */
316 for (i = 0; i < sk_X509_num(sk); i++) {
317 x509 = sk_X509_value(sk, i);
318 if (X509_issuer_and_serial_cmp(x509, &x) == 0)
324 X509 *X509_find_by_subject(STACK_OF(X509) *sk, const X509_NAME *name)
329 for (i = 0; i < sk_X509_num(sk); i++) {
330 x509 = sk_X509_value(sk, i);
331 if (X509_NAME_cmp(X509_get_subject_name(x509), name) == 0)
337 EVP_PKEY *X509_get0_pubkey(const X509 *x)
341 return X509_PUBKEY_get0(x->cert_info.key);
344 EVP_PKEY *X509_get_pubkey(X509 *x)
348 return X509_PUBKEY_get(x->cert_info.key);
351 int X509_check_private_key(const X509 *x, const EVP_PKEY *k)
356 xk = X509_get0_pubkey(x);
359 ret = EVP_PKEY_eq(xk, k);
367 ERR_raise(ERR_LIB_X509, X509_R_KEY_VALUES_MISMATCH);
370 ERR_raise(ERR_LIB_X509, X509_R_KEY_TYPE_MISMATCH);
373 ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_KEY_TYPE);
381 * Check a suite B algorithm is permitted: pass in a public key and the NID
382 * of its signature (or 0 if no signature). The pflags is a pointer to a
383 * flags field which must contain the suite B verification flags.
386 #ifndef OPENSSL_NO_EC
388 static int check_suite_b(EVP_PKEY *pkey, int sign_nid, unsigned long *pflags)
390 const EC_GROUP *grp = NULL;
392 if (pkey && EVP_PKEY_id(pkey) == EVP_PKEY_EC)
393 grp = EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey));
395 return X509_V_ERR_SUITE_B_INVALID_ALGORITHM;
396 curve_nid = EC_GROUP_get_curve_name(grp);
397 /* Check curve is consistent with LOS */
398 if (curve_nid == NID_secp384r1) { /* P-384 */
400 * Check signature algorithm is consistent with curve.
402 if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA384)
403 return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM;
404 if (!(*pflags & X509_V_FLAG_SUITEB_192_LOS))
405 return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED;
406 /* If we encounter P-384 we cannot use P-256 later */
407 *pflags &= ~X509_V_FLAG_SUITEB_128_LOS_ONLY;
408 } else if (curve_nid == NID_X9_62_prime256v1) { /* P-256 */
409 if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA256)
410 return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM;
411 if (!(*pflags & X509_V_FLAG_SUITEB_128_LOS_ONLY))
412 return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED;
414 return X509_V_ERR_SUITE_B_INVALID_CURVE;
419 int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain,
424 unsigned long tflags = flags;
426 if (!(flags & X509_V_FLAG_SUITEB_128_LOS))
429 /* If no EE certificate passed in must be first in chain */
431 x = sk_X509_value(chain, 0);
436 pk = X509_get0_pubkey(x);
439 * With DANE-EE(3) success, or DANE-EE(3)/PKIX-EE(1) failure we don't build
440 * a chain all, just report trust success or failure, but must also report
441 * Suite-B errors if applicable. This is indicated via a NULL chain
442 * pointer. All we need to do is check the leaf key algorithm.
445 return check_suite_b(pk, -1, &tflags);
447 if (X509_get_version(x) != 2) {
448 rv = X509_V_ERR_SUITE_B_INVALID_VERSION;
449 /* Correct error depth */
454 /* Check EE key only */
455 rv = check_suite_b(pk, -1, &tflags);
456 if (rv != X509_V_OK) {
457 /* Correct error depth */
461 for (; i < sk_X509_num(chain); i++) {
462 sign_nid = X509_get_signature_nid(x);
463 x = sk_X509_value(chain, i);
464 if (X509_get_version(x) != 2) {
465 rv = X509_V_ERR_SUITE_B_INVALID_VERSION;
468 pk = X509_get0_pubkey(x);
469 rv = check_suite_b(pk, sign_nid, &tflags);
474 /* Final check: root CA signature */
475 rv = check_suite_b(pk, X509_get_signature_nid(x), &tflags);
477 if (rv != X509_V_OK) {
478 /* Invalid signature or LOS errors are for previous cert */
479 if ((rv == X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM
480 || rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED) && i)
483 * If we have LOS error and flags changed then we are signing P-384
484 * with P-256. Use more meaningful error.
486 if (rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED && flags != tflags)
487 rv = X509_V_ERR_SUITE_B_CANNOT_SIGN_P_384_WITH_P_256;
494 int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags)
497 if (!(flags & X509_V_FLAG_SUITEB_128_LOS))
499 sign_nid = OBJ_obj2nid(crl->crl.sig_alg.algorithm);
500 return check_suite_b(pk, sign_nid, &flags);
504 int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain,
510 int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags)
517 * Not strictly speaking an "up_ref" as a STACK doesn't have a reference
518 * count but it has the same effect by duping the STACK and upping the ref of
519 * each X509 structure.
521 STACK_OF(X509) *X509_chain_up_ref(STACK_OF(X509) *chain)
525 ret = sk_X509_dup(chain);
528 for (i = 0; i < sk_X509_num(ret); i++) {
529 X509 *x = sk_X509_value(ret, i);
536 X509_free(sk_X509_value(ret, i));