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;
26 i = ASN1_INTEGER_cmp(&ai->serialNumber, &bi->serialNumber);
29 return X509_NAME_cmp(ai->issuer, bi->issuer);
32 #ifndef OPENSSL_NO_MD5
33 unsigned long X509_issuer_and_serial_hash(X509 *a)
35 unsigned long ret = 0;
36 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
42 f = X509_NAME_oneline(a->cert_info.issuer, NULL, 0);
43 if (!EVP_DigestInit_ex(ctx, EVP_md5(), NULL))
45 if (!EVP_DigestUpdate(ctx, (unsigned char *)f, strlen(f)))
49 (ctx, (unsigned char *)a->cert_info.serialNumber.data,
50 (unsigned long)a->cert_info.serialNumber.length))
52 if (!EVP_DigestFinal_ex(ctx, &(md[0]), NULL))
54 ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
55 ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
63 int X509_issuer_name_cmp(const X509 *a, const X509 *b)
65 return X509_NAME_cmp(a->cert_info.issuer, b->cert_info.issuer);
68 int X509_subject_name_cmp(const X509 *a, const X509 *b)
70 return X509_NAME_cmp(a->cert_info.subject, b->cert_info.subject);
73 int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b)
75 return X509_NAME_cmp(a->crl.issuer, b->crl.issuer);
78 int X509_CRL_match(const X509_CRL *a, const X509_CRL *b)
80 return memcmp(a->sha1_hash, b->sha1_hash, 20);
83 X509_NAME *X509_get_issuer_name(const X509 *a)
85 return a->cert_info.issuer;
88 unsigned long X509_issuer_name_hash(X509 *x)
90 return X509_NAME_hash(x->cert_info.issuer);
93 #ifndef OPENSSL_NO_MD5
94 unsigned long X509_issuer_name_hash_old(X509 *x)
96 return X509_NAME_hash_old(x->cert_info.issuer);
100 X509_NAME *X509_get_subject_name(const X509 *a)
102 return a->cert_info.subject;
105 ASN1_INTEGER *X509_get_serialNumber(X509 *a)
107 return &a->cert_info.serialNumber;
110 const ASN1_INTEGER *X509_get0_serialNumber(const X509 *a)
112 return &a->cert_info.serialNumber;
115 unsigned long X509_subject_name_hash(X509 *x)
117 return X509_NAME_hash(x->cert_info.subject);
120 #ifndef OPENSSL_NO_MD5
121 unsigned long X509_subject_name_hash_old(X509 *x)
123 return X509_NAME_hash_old(x->cert_info.subject);
128 * Compare two certificates: they must be identical for this to work. NB:
129 * Although "cmp" operations are generally prototyped to take "const"
130 * arguments (eg. for use in STACKs), the way X509 handling is - these
131 * operations may involve ensuring the hashes are up-to-date and ensuring
132 * certain cert information is cached. So this is the point where the
133 * "depth-first" constification tree has to halt with an evil cast.
135 int X509_cmp(const X509 *a, const X509 *b)
139 /* ensure hash is valid */
140 if (X509_check_purpose((X509 *)a, -1, 0) != 1)
142 if (X509_check_purpose((X509 *)b, -1, 0) != 1)
145 rv = memcmp(a->sha1_hash, b->sha1_hash, SHA_DIGEST_LENGTH);
148 /* Check for match against stored encoding too */
149 if (!a->cert_info.enc.modified && !b->cert_info.enc.modified) {
150 if (a->cert_info.enc.len < b->cert_info.enc.len)
152 if (a->cert_info.enc.len > b->cert_info.enc.len)
154 return memcmp(a->cert_info.enc.enc, b->cert_info.enc.enc,
155 a->cert_info.enc.len);
160 int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b)
164 /* Ensure canonical encoding is present and up to date */
166 if (!a->canon_enc || a->modified) {
167 ret = i2d_X509_NAME((X509_NAME *)a, NULL);
172 if (!b->canon_enc || b->modified) {
173 ret = i2d_X509_NAME((X509_NAME *)b, NULL);
178 ret = a->canon_enclen - b->canon_enclen;
180 if (ret != 0 || a->canon_enclen == 0)
183 return memcmp(a->canon_enc, b->canon_enc, a->canon_enclen);
187 unsigned long X509_NAME_hash(const X509_NAME *x)
189 unsigned long ret = 0;
190 unsigned char md[SHA_DIGEST_LENGTH];
192 /* Make sure X509_NAME structure contains valid cached encoding */
193 i2d_X509_NAME(x, NULL);
194 if (!EVP_Digest(x->canon_enc, x->canon_enclen, md, NULL, EVP_sha1(),
198 ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
199 ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
204 #ifndef OPENSSL_NO_MD5
206 * I now DER encode the name and hash it. Since I cache the DER encoding,
207 * this is reasonably efficient.
210 unsigned long X509_NAME_hash_old(const X509_NAME *x)
212 EVP_MD *md5 = EVP_MD_fetch(NULL, OSSL_DIGEST_NAME_MD5, "-fips");
213 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
214 unsigned long ret = 0;
215 unsigned char md[16];
217 if (md5 == NULL || md_ctx == NULL)
220 /* Make sure X509_NAME structure contains valid cached encoding */
221 i2d_X509_NAME(x, NULL);
222 if (EVP_DigestInit_ex(md_ctx, md5, NULL)
223 && EVP_DigestUpdate(md_ctx, x->bytes->data, x->bytes->length)
224 && EVP_DigestFinal_ex(md_ctx, md, NULL))
225 ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
226 ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
230 EVP_MD_CTX_free(md_ctx);
237 /* Search a stack of X509 for a match */
238 X509 *X509_find_by_issuer_and_serial(STACK_OF(X509) *sk, const X509_NAME *name,
239 const ASN1_INTEGER *serial)
242 X509 x, *x509 = NULL;
247 x.cert_info.serialNumber = *serial;
248 x.cert_info.issuer = (X509_NAME *)name; /* won't modify it */
250 for (i = 0; i < sk_X509_num(sk); i++) {
251 x509 = sk_X509_value(sk, i);
252 if (X509_issuer_and_serial_cmp(x509, &x) == 0)
258 X509 *X509_find_by_subject(STACK_OF(X509) *sk, const X509_NAME *name)
263 for (i = 0; i < sk_X509_num(sk); i++) {
264 x509 = sk_X509_value(sk, i);
265 if (X509_NAME_cmp(X509_get_subject_name(x509), name) == 0)
271 EVP_PKEY *X509_get0_pubkey(const X509 *x)
275 return X509_PUBKEY_get0(x->cert_info.key);
278 EVP_PKEY *X509_get_pubkey(X509 *x)
282 return X509_PUBKEY_get(x->cert_info.key);
285 int X509_check_private_key(const X509 *x, const EVP_PKEY *k)
290 xk = X509_get0_pubkey(x);
293 ret = EVP_PKEY_cmp(xk, k);
301 X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_KEY_VALUES_MISMATCH);
304 X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_KEY_TYPE_MISMATCH);
307 X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_UNKNOWN_KEY_TYPE);
315 * Check a suite B algorithm is permitted: pass in a public key and the NID
316 * of its signature (or 0 if no signature). The pflags is a pointer to a
317 * flags field which must contain the suite B verification flags.
320 #ifndef OPENSSL_NO_EC
322 static int check_suite_b(EVP_PKEY *pkey, int sign_nid, unsigned long *pflags)
324 const EC_GROUP *grp = NULL;
326 if (pkey && EVP_PKEY_id(pkey) == EVP_PKEY_EC)
327 grp = EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey));
329 return X509_V_ERR_SUITE_B_INVALID_ALGORITHM;
330 curve_nid = EC_GROUP_get_curve_name(grp);
331 /* Check curve is consistent with LOS */
332 if (curve_nid == NID_secp384r1) { /* P-384 */
334 * Check signature algorithm is consistent with curve.
336 if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA384)
337 return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM;
338 if (!(*pflags & X509_V_FLAG_SUITEB_192_LOS))
339 return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED;
340 /* If we encounter P-384 we cannot use P-256 later */
341 *pflags &= ~X509_V_FLAG_SUITEB_128_LOS_ONLY;
342 } else if (curve_nid == NID_X9_62_prime256v1) { /* P-256 */
343 if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA256)
344 return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM;
345 if (!(*pflags & X509_V_FLAG_SUITEB_128_LOS_ONLY))
346 return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED;
348 return X509_V_ERR_SUITE_B_INVALID_CURVE;
353 int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain,
358 unsigned long tflags = flags;
360 if (!(flags & X509_V_FLAG_SUITEB_128_LOS))
363 /* If no EE certificate passed in must be first in chain */
365 x = sk_X509_value(chain, 0);
370 pk = X509_get0_pubkey(x);
373 * With DANE-EE(3) success, or DANE-EE(3)/PKIX-EE(1) failure we don't build
374 * a chain all, just report trust success or failure, but must also report
375 * Suite-B errors if applicable. This is indicated via a NULL chain
376 * pointer. All we need to do is check the leaf key algorithm.
379 return check_suite_b(pk, -1, &tflags);
381 if (X509_get_version(x) != 2) {
382 rv = X509_V_ERR_SUITE_B_INVALID_VERSION;
383 /* Correct error depth */
388 /* Check EE key only */
389 rv = check_suite_b(pk, -1, &tflags);
390 if (rv != X509_V_OK) {
391 /* Correct error depth */
395 for (; i < sk_X509_num(chain); i++) {
396 sign_nid = X509_get_signature_nid(x);
397 x = sk_X509_value(chain, i);
398 if (X509_get_version(x) != 2) {
399 rv = X509_V_ERR_SUITE_B_INVALID_VERSION;
402 pk = X509_get0_pubkey(x);
403 rv = check_suite_b(pk, sign_nid, &tflags);
408 /* Final check: root CA signature */
409 rv = check_suite_b(pk, X509_get_signature_nid(x), &tflags);
411 if (rv != X509_V_OK) {
412 /* Invalid signature or LOS errors are for previous cert */
413 if ((rv == X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM
414 || rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED) && i)
417 * If we have LOS error and flags changed then we are signing P-384
418 * with P-256. Use more meaningful error.
420 if (rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED && flags != tflags)
421 rv = X509_V_ERR_SUITE_B_CANNOT_SIGN_P_384_WITH_P_256;
428 int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags)
431 if (!(flags & X509_V_FLAG_SUITEB_128_LOS))
433 sign_nid = OBJ_obj2nid(crl->crl.sig_alg.algorithm);
434 return check_suite_b(pk, sign_nid, &flags);
438 int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain,
444 int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags)
451 * Not strictly speaking an "up_ref" as a STACK doesn't have a reference
452 * count but it has the same effect by duping the STACK and upping the ref of
453 * each X509 structure.
455 STACK_OF(X509) *X509_chain_up_ref(STACK_OF(X509) *chain)
459 ret = sk_X509_dup(chain);
462 for (i = 0; i < sk_X509_num(ret); i++) {
463 X509 *x = sk_X509_value(ret, i);
470 X509_free (sk_X509_value(ret, i));