2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
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
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"
18 int X509_issuer_and_serial_cmp(const X509 *a, const X509 *b)
21 const X509_CINF *ai, *bi;
25 i = ASN1_INTEGER_cmp(&ai->serialNumber, &bi->serialNumber);
28 return X509_NAME_cmp(ai->issuer, bi->issuer);
31 #ifndef OPENSSL_NO_MD5
32 unsigned long X509_issuer_and_serial_hash(X509 *a)
34 unsigned long ret = 0;
35 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
41 f = X509_NAME_oneline(a->cert_info.issuer, NULL, 0);
44 if (!EVP_DigestInit_ex(ctx, EVP_md5(), NULL))
46 if (!EVP_DigestUpdate(ctx, (unsigned char *)f, strlen(f)))
50 (ctx, (unsigned char *)a->cert_info.serialNumber.data,
51 (unsigned long)a->cert_info.serialNumber.length))
53 if (!EVP_DigestFinal_ex(ctx, &(md[0]), NULL))
55 ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
56 ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
64 int X509_issuer_name_cmp(const X509 *a, const X509 *b)
66 return X509_NAME_cmp(a->cert_info.issuer, b->cert_info.issuer);
69 int X509_subject_name_cmp(const X509 *a, const X509 *b)
71 return X509_NAME_cmp(a->cert_info.subject, b->cert_info.subject);
74 int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b)
76 return X509_NAME_cmp(a->crl.issuer, b->crl.issuer);
79 int X509_CRL_match(const X509_CRL *a, const X509_CRL *b)
81 return memcmp(a->sha1_hash, b->sha1_hash, 20);
84 X509_NAME *X509_get_issuer_name(const X509 *a)
86 return a->cert_info.issuer;
89 unsigned long X509_issuer_name_hash(X509 *x)
91 return X509_NAME_hash(x->cert_info.issuer);
94 #ifndef OPENSSL_NO_MD5
95 unsigned long X509_issuer_name_hash_old(X509 *x)
97 return X509_NAME_hash_old(x->cert_info.issuer);
101 X509_NAME *X509_get_subject_name(const X509 *a)
103 return a->cert_info.subject;
106 ASN1_INTEGER *X509_get_serialNumber(X509 *a)
108 return &a->cert_info.serialNumber;
111 const ASN1_INTEGER *X509_get0_serialNumber(const X509 *a)
113 return &a->cert_info.serialNumber;
116 unsigned long X509_subject_name_hash(X509 *x)
118 return X509_NAME_hash(x->cert_info.subject);
121 #ifndef OPENSSL_NO_MD5
122 unsigned long X509_subject_name_hash_old(X509 *x)
124 return X509_NAME_hash_old(x->cert_info.subject);
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.
136 int X509_cmp(const X509 *a, const X509 *b)
140 if (a == b) /* for efficiency */
143 /* try to make sure hash is valid */
144 (void)X509_check_purpose((X509 *)a, -1, 0);
145 (void)X509_check_purpose((X509 *)b, -1, 0);
147 if ((a->ex_flags & EXFLAG_NO_FINGERPRINT) == 0
148 && (b->ex_flags & EXFLAG_NO_FINGERPRINT) == 0)
149 rv = memcmp(a->sha1_hash, b->sha1_hash, SHA_DIGEST_LENGTH);
153 /* Check for match against stored encoding too */
154 if (!a->cert_info.enc.modified && !b->cert_info.enc.modified) {
155 if (a->cert_info.enc.len < b->cert_info.enc.len)
157 if (a->cert_info.enc.len > b->cert_info.enc.len)
159 return memcmp(a->cert_info.enc.enc, b->cert_info.enc.enc,
160 a->cert_info.enc.len);
165 int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b)
169 /* Ensure canonical encoding is present and up to date */
171 if (!a->canon_enc || a->modified) {
172 ret = i2d_X509_NAME((X509_NAME *)a, NULL);
177 if (!b->canon_enc || b->modified) {
178 ret = i2d_X509_NAME((X509_NAME *)b, NULL);
183 ret = a->canon_enclen - b->canon_enclen;
185 if (ret != 0 || a->canon_enclen == 0)
188 return memcmp(a->canon_enc, b->canon_enc, a->canon_enclen);
192 unsigned long X509_NAME_hash(X509_NAME *x)
194 unsigned long ret = 0;
195 unsigned char md[SHA_DIGEST_LENGTH];
197 /* Make sure X509_NAME structure contains valid cached encoding */
198 i2d_X509_NAME(x, NULL);
199 if (!EVP_Digest(x->canon_enc, x->canon_enclen, md, NULL, EVP_sha1(),
203 ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
204 ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
209 #ifndef OPENSSL_NO_MD5
211 * I now DER encode the name and hash it. Since I cache the DER encoding,
212 * this is reasonably efficient.
215 unsigned long X509_NAME_hash_old(X509_NAME *x)
217 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
218 unsigned long ret = 0;
219 unsigned char md[16];
224 /* Make sure X509_NAME structure contains valid cached encoding */
225 i2d_X509_NAME(x, NULL);
226 EVP_MD_CTX_set_flags(md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
227 if (EVP_DigestInit_ex(md_ctx, EVP_md5(), NULL)
228 && EVP_DigestUpdate(md_ctx, x->bytes->data, x->bytes->length)
229 && EVP_DigestFinal_ex(md_ctx, md, NULL))
230 ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
231 ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
233 EVP_MD_CTX_free(md_ctx);
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)
244 X509 x, *x509 = NULL;
249 x.cert_info.serialNumber = *serial;
250 x.cert_info.issuer = name;
252 for (i = 0; i < sk_X509_num(sk); i++) {
253 x509 = sk_X509_value(sk, i);
254 if (X509_issuer_and_serial_cmp(x509, &x) == 0)
260 X509 *X509_find_by_subject(STACK_OF(X509) *sk, X509_NAME *name)
265 for (i = 0; i < sk_X509_num(sk); i++) {
266 x509 = sk_X509_value(sk, i);
267 if (X509_NAME_cmp(X509_get_subject_name(x509), name) == 0)
273 EVP_PKEY *X509_get0_pubkey(const X509 *x)
277 return X509_PUBKEY_get0(x->cert_info.key);
280 EVP_PKEY *X509_get_pubkey(X509 *x)
284 return X509_PUBKEY_get(x->cert_info.key);
287 int X509_check_private_key(const X509 *x, const EVP_PKEY *k)
292 xk = X509_get0_pubkey(x);
295 ret = EVP_PKEY_cmp(xk, k);
303 X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_KEY_VALUES_MISMATCH);
306 X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_KEY_TYPE_MISMATCH);
309 X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_UNKNOWN_KEY_TYPE);
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.
322 #ifndef OPENSSL_NO_EC
324 static int check_suite_b(EVP_PKEY *pkey, int sign_nid, unsigned long *pflags)
326 const EC_GROUP *grp = NULL;
328 if (pkey && EVP_PKEY_id(pkey) == EVP_PKEY_EC)
329 grp = EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey));
331 return X509_V_ERR_SUITE_B_INVALID_ALGORITHM;
332 curve_nid = EC_GROUP_get_curve_name(grp);
333 /* Check curve is consistent with LOS */
334 if (curve_nid == NID_secp384r1) { /* P-384 */
336 * Check signature algorithm is consistent with curve.
338 if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA384)
339 return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM;
340 if (!(*pflags & X509_V_FLAG_SUITEB_192_LOS))
341 return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED;
342 /* If we encounter P-384 we cannot use P-256 later */
343 *pflags &= ~X509_V_FLAG_SUITEB_128_LOS_ONLY;
344 } else if (curve_nid == NID_X9_62_prime256v1) { /* P-256 */
345 if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA256)
346 return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM;
347 if (!(*pflags & X509_V_FLAG_SUITEB_128_LOS_ONLY))
348 return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED;
350 return X509_V_ERR_SUITE_B_INVALID_CURVE;
355 int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain,
360 unsigned long tflags = flags;
362 if (!(flags & X509_V_FLAG_SUITEB_128_LOS))
365 /* If no EE certificate passed in must be first in chain */
367 x = sk_X509_value(chain, 0);
372 pk = X509_get0_pubkey(x);
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.
381 return check_suite_b(pk, -1, &tflags);
383 if (X509_get_version(x) != 2) {
384 rv = X509_V_ERR_SUITE_B_INVALID_VERSION;
385 /* Correct error depth */
390 /* Check EE key only */
391 rv = check_suite_b(pk, -1, &tflags);
392 if (rv != X509_V_OK) {
393 /* Correct error depth */
397 for (; i < sk_X509_num(chain); i++) {
398 sign_nid = X509_get_signature_nid(x);
399 x = sk_X509_value(chain, i);
400 if (X509_get_version(x) != 2) {
401 rv = X509_V_ERR_SUITE_B_INVALID_VERSION;
404 pk = X509_get0_pubkey(x);
405 rv = check_suite_b(pk, sign_nid, &tflags);
410 /* Final check: root CA signature */
411 rv = check_suite_b(pk, X509_get_signature_nid(x), &tflags);
413 if (rv != X509_V_OK) {
414 /* Invalid signature or LOS errors are for previous cert */
415 if ((rv == X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM
416 || rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED) && i)
419 * If we have LOS error and flags changed then we are signing P-384
420 * with P-256. Use more meaningful error.
422 if (rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED && flags != tflags)
423 rv = X509_V_ERR_SUITE_B_CANNOT_SIGN_P_384_WITH_P_256;
430 int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags)
433 if (!(flags & X509_V_FLAG_SUITEB_128_LOS))
435 sign_nid = OBJ_obj2nid(crl->crl.sig_alg.algorithm);
436 return check_suite_b(pk, sign_nid, &flags);
440 int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain,
446 int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags)
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.
457 STACK_OF(X509) *X509_chain_up_ref(STACK_OF(X509) *chain)
461 ret = sk_X509_dup(chain);
464 for (i = 0; i < sk_X509_num(ret); i++) {
465 X509 *x = sk_X509_value(ret, i);
472 X509_free (sk_X509_value(ret, i));