]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/x509/x509_cmp.c
Constify various mostly X509-related parameter types in crypto/ and apps/
[thirdparty/openssl.git] / crypto / x509 / x509_cmp.c
CommitLineData
b1322259 1/*
7e06a675 2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
3e4b43b9 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
b1322259
RS
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
d02b48c6
RE
8 */
9
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
ec577822
BM
12#include <openssl/asn1.h>
13#include <openssl/objects.h>
14#include <openssl/x509.h>
e947f396 15#include <openssl/x509v3.h>
47b4ccea 16#include <openssl/core_names.h>
25f2138b 17#include "crypto/x509.h"
d02b48c6 18
ccd86b68 19int X509_issuer_and_serial_cmp(const X509 *a, const X509 *b)
0f113f3e
MC
20{
21 int i;
5cf6abd8 22 const X509_CINF *ai, *bi;
0f113f3e 23
5cf6abd8
DSH
24 ai = &a->cert_info;
25 bi = &b->cert_info;
81e49438 26 i = ASN1_INTEGER_cmp(&ai->serialNumber, &bi->serialNumber);
0f113f3e 27 if (i)
26a7d938
K
28 return i;
29 return X509_NAME_cmp(ai->issuer, bi->issuer);
0f113f3e 30}
d02b48c6 31
cf1b7d96 32#ifndef OPENSSL_NO_MD5
6b691a5c 33unsigned long X509_issuer_and_serial_hash(X509 *a)
0f113f3e
MC
34{
35 unsigned long ret = 0;
bfb0641f 36 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
0f113f3e
MC
37 unsigned char md[16];
38 char *f;
39
6e59a892
RL
40 if (ctx == NULL)
41 goto err;
5cf6abd8 42 f = X509_NAME_oneline(a->cert_info.issuer, NULL, 0);
6e59a892 43 if (!EVP_DigestInit_ex(ctx, EVP_md5(), NULL))
0f113f3e 44 goto err;
6e59a892 45 if (!EVP_DigestUpdate(ctx, (unsigned char *)f, strlen(f)))
0f113f3e
MC
46 goto err;
47 OPENSSL_free(f);
48 if (!EVP_DigestUpdate
6e59a892 49 (ctx, (unsigned char *)a->cert_info.serialNumber.data,
81e49438 50 (unsigned long)a->cert_info.serialNumber.length))
0f113f3e 51 goto err;
6e59a892 52 if (!EVP_DigestFinal_ex(ctx, &(md[0]), NULL))
0f113f3e
MC
53 goto err;
54 ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
55 ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
56 ) & 0xffffffffL;
57 err:
bfb0641f 58 EVP_MD_CTX_free(ctx);
26a7d938 59 return ret;
0f113f3e 60}
d02b48c6 61#endif
0f113f3e 62
ccd86b68 63int X509_issuer_name_cmp(const X509 *a, const X509 *b)
0f113f3e 64{
26a7d938 65 return X509_NAME_cmp(a->cert_info.issuer, b->cert_info.issuer);
0f113f3e 66}
d02b48c6 67
ccd86b68 68int X509_subject_name_cmp(const X509 *a, const X509 *b)
0f113f3e 69{
26a7d938 70 return X509_NAME_cmp(a->cert_info.subject, b->cert_info.subject);
0f113f3e 71}
d02b48c6 72
ccd86b68 73int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b)
0f113f3e 74{
26a7d938 75 return X509_NAME_cmp(a->crl.issuer, b->crl.issuer);
0f113f3e 76}
d02b48c6 77
edc54021 78int X509_CRL_match(const X509_CRL *a, const X509_CRL *b)
0f113f3e
MC
79{
80 return memcmp(a->sha1_hash, b->sha1_hash, 20);
81}
edc54021 82
1421aead 83X509_NAME *X509_get_issuer_name(const X509 *a)
0f113f3e 84{
26a7d938 85 return a->cert_info.issuer;
0f113f3e 86}
d02b48c6 87
6b691a5c 88unsigned long X509_issuer_name_hash(X509 *x)
0f113f3e 89{
26a7d938 90 return X509_NAME_hash(x->cert_info.issuer);
0f113f3e 91}
d02b48c6 92
0e0c6821
DSH
93#ifndef OPENSSL_NO_MD5
94unsigned long X509_issuer_name_hash_old(X509 *x)
0f113f3e 95{
26a7d938 96 return X509_NAME_hash_old(x->cert_info.issuer);
0f113f3e 97}
0e0c6821
DSH
98#endif
99
1421aead 100X509_NAME *X509_get_subject_name(const X509 *a)
0f113f3e 101{
26a7d938 102 return a->cert_info.subject;
0f113f3e 103}
d02b48c6 104
22293ea1 105ASN1_INTEGER *X509_get_serialNumber(X509 *a)
0f113f3e 106{
81e49438 107 return &a->cert_info.serialNumber;
0f113f3e 108}
d02b48c6 109
68c12bfc
DSH
110const ASN1_INTEGER *X509_get0_serialNumber(const X509 *a)
111{
112 return &a->cert_info.serialNumber;
113}
114
6b691a5c 115unsigned long X509_subject_name_hash(X509 *x)
0f113f3e 116{
26a7d938 117 return X509_NAME_hash(x->cert_info.subject);
0f113f3e 118}
731d9c5f 119
0e0c6821
DSH
120#ifndef OPENSSL_NO_MD5
121unsigned long X509_subject_name_hash_old(X509 *x)
0f113f3e 122{
26a7d938 123 return X509_NAME_hash_old(x->cert_info.subject);
0f113f3e 124}
0e0c6821
DSH
125#endif
126
0f113f3e
MC
127/*
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.
e947f396 134 */
ccd86b68 135int X509_cmp(const X509 *a, const X509 *b)
e947f396 136{
0f113f3e 137 int rv;
7e06a675 138
0f113f3e 139 /* ensure hash is valid */
7e06a675
BE
140 if (X509_check_purpose((X509 *)a, -1, 0) != 1)
141 return -2;
142 if (X509_check_purpose((X509 *)b, -1, 0) != 1)
143 return -2;
0f113f3e
MC
144
145 rv = memcmp(a->sha1_hash, b->sha1_hash, SHA_DIGEST_LENGTH);
146 if (rv)
147 return rv;
148 /* Check for match against stored encoding too */
5cf6abd8 149 if (!a->cert_info.enc.modified && !b->cert_info.enc.modified) {
87a8405b
DB
150 if (a->cert_info.enc.len < b->cert_info.enc.len)
151 return -1;
152 if (a->cert_info.enc.len > b->cert_info.enc.len)
153 return 1;
5cf6abd8
DSH
154 return memcmp(a->cert_info.enc.enc, b->cert_info.enc.enc,
155 a->cert_info.enc.len);
0f113f3e
MC
156 }
157 return rv;
e947f396 158}
d02b48c6 159
450ea834 160int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b)
0f113f3e
MC
161{
162 int ret;
c81a1509 163
0f113f3e 164 /* Ensure canonical encoding is present and up to date */
c81a1509 165
0f113f3e
MC
166 if (!a->canon_enc || a->modified) {
167 ret = i2d_X509_NAME((X509_NAME *)a, NULL);
168 if (ret < 0)
169 return -2;
170 }
1862dae8 171
0f113f3e
MC
172 if (!b->canon_enc || b->modified) {
173 ret = i2d_X509_NAME((X509_NAME *)b, NULL);
174 if (ret < 0)
175 return -2;
176 }
1862dae8 177
0f113f3e 178 ret = a->canon_enclen - b->canon_enclen;
d02b48c6 179
511190b6 180 if (ret != 0 || a->canon_enclen == 0)
0f113f3e 181 return ret;
1862dae8 182
0f113f3e 183 return memcmp(a->canon_enc, b->canon_enc, a->canon_enclen);
d02b48c6 184
0f113f3e 185}
d02b48c6 186
8cc86b81 187unsigned long X509_NAME_hash(const X509_NAME *x)
0f113f3e
MC
188{
189 unsigned long ret = 0;
190 unsigned char md[SHA_DIGEST_LENGTH];
191
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(),
195 NULL))
196 return 0;
197
198 ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
199 ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
200 ) & 0xffffffffL;
26a7d938 201 return ret;
0f113f3e 202}
450ea834 203
cf1b7d96 204#ifndef OPENSSL_NO_MD5
0f113f3e
MC
205/*
206 * I now DER encode the name and hash it. Since I cache the DER encoding,
207 * this is reasonably efficient.
208 */
c2c99e28 209
8cc86b81 210unsigned long X509_NAME_hash_old(const X509_NAME *x)
0f113f3e 211{
47b4ccea 212 EVP_MD *md5 = EVP_MD_fetch(NULL, OSSL_DIGEST_NAME_MD5, "-fips");
bfb0641f 213 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
0f113f3e
MC
214 unsigned long ret = 0;
215 unsigned char md[16];
216
47b4ccea
RL
217 if (md5 == NULL || md_ctx == NULL)
218 goto end;
6e59a892 219
0f113f3e
MC
220 /* Make sure X509_NAME structure contains valid cached encoding */
221 i2d_X509_NAME(x, NULL);
47b4ccea 222 if (EVP_DigestInit_ex(md_ctx, md5, NULL)
6e59a892
RL
223 && EVP_DigestUpdate(md_ctx, x->bytes->data, x->bytes->length)
224 && EVP_DigestFinal_ex(md_ctx, md, NULL))
0f113f3e
MC
225 ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
226 ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
227 ) & 0xffffffffL;
47b4ccea
RL
228
229 end:
bfb0641f 230 EVP_MD_CTX_free(md_ctx);
47b4ccea 231 EVP_MD_free(md5);
0f113f3e 232
26a7d938 233 return ret;
0f113f3e 234}
d02b48c6
RE
235#endif
236
237/* Search a stack of X509 for a match */
8cc86b81
DDO
238X509 *X509_find_by_issuer_and_serial(STACK_OF(X509) *sk, const X509_NAME *name,
239 const ASN1_INTEGER *serial)
0f113f3e
MC
240{
241 int i;
0f113f3e
MC
242 X509 x, *x509 = NULL;
243
244 if (!sk)
245 return NULL;
246
81e49438 247 x.cert_info.serialNumber = *serial;
8cc86b81 248 x.cert_info.issuer = (X509_NAME *)name; /* won't modify it */
0f113f3e
MC
249
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)
26a7d938 253 return x509;
0f113f3e 254 }
26a7d938 255 return NULL;
0f113f3e 256}
d02b48c6 257
8cc86b81 258X509 *X509_find_by_subject(STACK_OF(X509) *sk, const X509_NAME *name)
0f113f3e
MC
259{
260 X509 *x509;
261 int i;
262
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)
26a7d938 266 return x509;
0f113f3e 267 }
26a7d938 268 return NULL;
0f113f3e 269}
d02b48c6 270
fdaf7bee 271EVP_PKEY *X509_get0_pubkey(const X509 *x)
c01ff880
DSH
272{
273 if (x == NULL)
274 return NULL;
275 return X509_PUBKEY_get0(x->cert_info.key);
276}
277
6b691a5c 278EVP_PKEY *X509_get_pubkey(X509 *x)
0f113f3e 279{
5cf6abd8 280 if (x == NULL)
c01ff880
DSH
281 return NULL;
282 return X509_PUBKEY_get(x->cert_info.key);
0f113f3e 283}
dfeab068 284
fdaf7bee 285int X509_check_private_key(const X509 *x, const EVP_PKEY *k)
0f113f3e 286{
fdaf7bee 287 const EVP_PKEY *xk;
0f113f3e
MC
288 int ret;
289
c01ff880 290 xk = X509_get0_pubkey(x);
0f113f3e
MC
291
292 if (xk)
293 ret = EVP_PKEY_cmp(xk, k);
294 else
295 ret = -2;
296
297 switch (ret) {
298 case 1:
299 break;
300 case 0:
301 X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_KEY_VALUES_MISMATCH);
302 break;
303 case -1:
304 X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_KEY_TYPE_MISMATCH);
305 break;
306 case -2:
307 X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_UNKNOWN_KEY_TYPE);
308 }
0f113f3e
MC
309 if (ret > 0)
310 return 1;
311 return 0;
312}
313
314/*
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.
3ad344a5
DSH
318 */
319
14536c8c
DSH
320#ifndef OPENSSL_NO_EC
321
3ad344a5 322static int check_suite_b(EVP_PKEY *pkey, int sign_nid, unsigned long *pflags)
0f113f3e
MC
323{
324 const EC_GROUP *grp = NULL;
325 int curve_nid;
3aeb9348
DSH
326 if (pkey && EVP_PKEY_id(pkey) == EVP_PKEY_EC)
327 grp = EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey));
0f113f3e
MC
328 if (!grp)
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 */
333 /*
334 * Check signature algorithm is consistent with curve.
335 */
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;
347 } else
348 return X509_V_ERR_SUITE_B_INVALID_CURVE;
349
350 return X509_V_OK;
351}
3ad344a5 352
3b0648eb 353int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain,
0f113f3e
MC
354 unsigned long flags)
355{
356 int rv, i, sign_nid;
6e328256
VD
357 EVP_PKEY *pk;
358 unsigned long tflags = flags;
359
0f113f3e
MC
360 if (!(flags & X509_V_FLAG_SUITEB_128_LOS))
361 return X509_V_OK;
6e328256 362
0f113f3e
MC
363 /* If no EE certificate passed in must be first in chain */
364 if (x == NULL) {
365 x = sk_X509_value(chain, 0);
366 i = 1;
367 } else
368 i = 0;
369
6e328256
VD
370 pk = X509_get0_pubkey(x);
371
372 /*
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.
377 */
378 if (chain == NULL)
379 return check_suite_b(pk, -1, &tflags);
380
0f113f3e
MC
381 if (X509_get_version(x) != 2) {
382 rv = X509_V_ERR_SUITE_B_INVALID_VERSION;
383 /* Correct error depth */
384 i = 0;
385 goto end;
386 }
387
0f113f3e
MC
388 /* Check EE key only */
389 rv = check_suite_b(pk, -1, &tflags);
390 if (rv != X509_V_OK) {
391 /* Correct error depth */
392 i = 0;
393 goto end;
394 }
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;
400 goto end;
401 }
8382fd3a 402 pk = X509_get0_pubkey(x);
0f113f3e
MC
403 rv = check_suite_b(pk, sign_nid, &tflags);
404 if (rv != X509_V_OK)
405 goto end;
406 }
407
408 /* Final check: root CA signature */
409 rv = check_suite_b(pk, X509_get_signature_nid(x), &tflags);
410 end:
0f113f3e
MC
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)
415 i--;
416 /*
417 * If we have LOS error and flags changed then we are signing P-384
0d4fb843 418 * with P-256. Use more meaningful error.
0f113f3e
MC
419 */
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;
422 if (perror_depth)
423 *perror_depth = i;
424 }
425 return rv;
426}
3ad344a5 427
3b0648eb 428int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags)
0f113f3e
MC
429{
430 int sign_nid;
431 if (!(flags & X509_V_FLAG_SUITEB_128_LOS))
432 return X509_V_OK;
6e63c142 433 sign_nid = OBJ_obj2nid(crl->crl.sig_alg.algorithm);
0f113f3e
MC
434 return check_suite_b(pk, sign_nid, &flags);
435}
14536c8c
DSH
436
437#else
438int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain,
0f113f3e
MC
439 unsigned long flags)
440{
441 return 0;
442}
14536c8c
DSH
443
444int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags)
0f113f3e
MC
445{
446 return 0;
447}
14536c8c
DSH
448
449#endif
0f113f3e
MC
450/*
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.
3b0648eb
DSH
454 */
455STACK_OF(X509) *X509_chain_up_ref(STACK_OF(X509) *chain)
0f113f3e
MC
456{
457 STACK_OF(X509) *ret;
458 int i;
459 ret = sk_X509_dup(chain);
cae665df
BE
460 if (ret == NULL)
461 return NULL;
0f113f3e
MC
462 for (i = 0; i < sk_X509_num(ret); i++) {
463 X509 *x = sk_X509_value(ret, i);
cae665df
BE
464 if (!X509_up_ref(x))
465 goto err;
0f113f3e
MC
466 }
467 return ret;
cae665df
BE
468 err:
469 while (i-- > 0)
470 X509_free (sk_X509_value(ret, i));
471 sk_X509_free(ret);
472 return NULL;
0f113f3e 473}