]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/x509/x509_cmp.c
Fix error handling in x509v3_cache_extensions and related functions
[thirdparty/openssl.git] / crypto / x509 / x509_cmp.c
CommitLineData
b1322259 1/*
ba4356ae 2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
b1322259
RS
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
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>
0c994d54 16#include "crypto/x509.h"
d02b48c6 17
ccd86b68 18int X509_issuer_and_serial_cmp(const X509 *a, const X509 *b)
0f113f3e
MC
19{
20 int i;
5cf6abd8 21 const X509_CINF *ai, *bi;
0f113f3e 22
5cf6abd8
DSH
23 ai = &a->cert_info;
24 bi = &b->cert_info;
81e49438 25 i = ASN1_INTEGER_cmp(&ai->serialNumber, &bi->serialNumber);
0f113f3e 26 if (i)
26a7d938
K
27 return i;
28 return X509_NAME_cmp(ai->issuer, bi->issuer);
0f113f3e 29}
d02b48c6 30
cf1b7d96 31#ifndef OPENSSL_NO_MD5
6b691a5c 32unsigned long X509_issuer_and_serial_hash(X509 *a)
0f113f3e
MC
33{
34 unsigned long ret = 0;
bfb0641f 35 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
0f113f3e
MC
36 unsigned char md[16];
37 char *f;
38
6e59a892
RL
39 if (ctx == NULL)
40 goto err;
5cf6abd8 41 f = X509_NAME_oneline(a->cert_info.issuer, NULL, 0);
6e59a892 42 if (!EVP_DigestInit_ex(ctx, EVP_md5(), NULL))
0f113f3e 43 goto err;
6e59a892 44 if (!EVP_DigestUpdate(ctx, (unsigned char *)f, strlen(f)))
0f113f3e
MC
45 goto err;
46 OPENSSL_free(f);
47 if (!EVP_DigestUpdate
6e59a892 48 (ctx, (unsigned char *)a->cert_info.serialNumber.data,
81e49438 49 (unsigned long)a->cert_info.serialNumber.length))
0f113f3e 50 goto err;
6e59a892 51 if (!EVP_DigestFinal_ex(ctx, &(md[0]), NULL))
0f113f3e
MC
52 goto err;
53 ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
54 ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
55 ) & 0xffffffffL;
56 err:
bfb0641f 57 EVP_MD_CTX_free(ctx);
26a7d938 58 return ret;
0f113f3e 59}
d02b48c6 60#endif
0f113f3e 61
ccd86b68 62int X509_issuer_name_cmp(const X509 *a, const X509 *b)
0f113f3e 63{
26a7d938 64 return X509_NAME_cmp(a->cert_info.issuer, b->cert_info.issuer);
0f113f3e 65}
d02b48c6 66
ccd86b68 67int X509_subject_name_cmp(const X509 *a, const X509 *b)
0f113f3e 68{
26a7d938 69 return X509_NAME_cmp(a->cert_info.subject, b->cert_info.subject);
0f113f3e 70}
d02b48c6 71
ccd86b68 72int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b)
0f113f3e 73{
26a7d938 74 return X509_NAME_cmp(a->crl.issuer, b->crl.issuer);
0f113f3e 75}
d02b48c6 76
edc54021 77int X509_CRL_match(const X509_CRL *a, const X509_CRL *b)
0f113f3e
MC
78{
79 return memcmp(a->sha1_hash, b->sha1_hash, 20);
80}
edc54021 81
1421aead 82X509_NAME *X509_get_issuer_name(const X509 *a)
0f113f3e 83{
26a7d938 84 return a->cert_info.issuer;
0f113f3e 85}
d02b48c6 86
6b691a5c 87unsigned long X509_issuer_name_hash(X509 *x)
0f113f3e 88{
26a7d938 89 return X509_NAME_hash(x->cert_info.issuer);
0f113f3e 90}
d02b48c6 91
0e0c6821
DSH
92#ifndef OPENSSL_NO_MD5
93unsigned long X509_issuer_name_hash_old(X509 *x)
0f113f3e 94{
26a7d938 95 return X509_NAME_hash_old(x->cert_info.issuer);
0f113f3e 96}
0e0c6821
DSH
97#endif
98
1421aead 99X509_NAME *X509_get_subject_name(const X509 *a)
0f113f3e 100{
26a7d938 101 return a->cert_info.subject;
0f113f3e 102}
d02b48c6 103
22293ea1 104ASN1_INTEGER *X509_get_serialNumber(X509 *a)
0f113f3e 105{
81e49438 106 return &a->cert_info.serialNumber;
0f113f3e 107}
d02b48c6 108
68c12bfc
DSH
109const ASN1_INTEGER *X509_get0_serialNumber(const X509 *a)
110{
111 return &a->cert_info.serialNumber;
112}
113
6b691a5c 114unsigned long X509_subject_name_hash(X509 *x)
0f113f3e 115{
26a7d938 116 return X509_NAME_hash(x->cert_info.subject);
0f113f3e 117}
731d9c5f 118
0e0c6821
DSH
119#ifndef OPENSSL_NO_MD5
120unsigned long X509_subject_name_hash_old(X509 *x)
0f113f3e 121{
26a7d938 122 return X509_NAME_hash_old(x->cert_info.subject);
0f113f3e 123}
0e0c6821
DSH
124#endif
125
0f113f3e
MC
126/*
127 * Compare two certificates: they must be identical for this to work. NB:
128 * Although "cmp" operations are generally prototyped to take "const"
129 * arguments (eg. for use in STACKs), the way X509 handling is - these
130 * operations may involve ensuring the hashes are up-to-date and ensuring
131 * certain cert information is cached. So this is the point where the
132 * "depth-first" constification tree has to halt with an evil cast.
e947f396 133 */
ccd86b68 134int X509_cmp(const X509 *a, const X509 *b)
e947f396 135{
0f113f3e 136 int rv;
ba4356ae 137
0f113f3e 138 /* ensure hash is valid */
ba4356ae
BE
139 if (X509_check_purpose((X509 *)a, -1, 0) != 1)
140 return -2;
141 if (X509_check_purpose((X509 *)b, -1, 0) != 1)
142 return -2;
0f113f3e
MC
143
144 rv = memcmp(a->sha1_hash, b->sha1_hash, SHA_DIGEST_LENGTH);
145 if (rv)
146 return rv;
147 /* Check for match against stored encoding too */
5cf6abd8 148 if (!a->cert_info.enc.modified && !b->cert_info.enc.modified) {
87a8405b
DB
149 if (a->cert_info.enc.len < b->cert_info.enc.len)
150 return -1;
151 if (a->cert_info.enc.len > b->cert_info.enc.len)
152 return 1;
5cf6abd8
DSH
153 return memcmp(a->cert_info.enc.enc, b->cert_info.enc.enc,
154 a->cert_info.enc.len);
0f113f3e
MC
155 }
156 return rv;
e947f396 157}
d02b48c6 158
450ea834 159int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b)
0f113f3e
MC
160{
161 int ret;
c81a1509 162
0f113f3e 163 /* Ensure canonical encoding is present and up to date */
c81a1509 164
0f113f3e
MC
165 if (!a->canon_enc || a->modified) {
166 ret = i2d_X509_NAME((X509_NAME *)a, NULL);
167 if (ret < 0)
168 return -2;
169 }
1862dae8 170
0f113f3e
MC
171 if (!b->canon_enc || b->modified) {
172 ret = i2d_X509_NAME((X509_NAME *)b, NULL);
173 if (ret < 0)
174 return -2;
175 }
1862dae8 176
0f113f3e 177 ret = a->canon_enclen - b->canon_enclen;
d02b48c6 178
511190b6 179 if (ret != 0 || a->canon_enclen == 0)
0f113f3e 180 return ret;
1862dae8 181
0f113f3e 182 return memcmp(a->canon_enc, b->canon_enc, a->canon_enclen);
d02b48c6 183
0f113f3e 184}
d02b48c6 185
c2c99e28 186unsigned long X509_NAME_hash(X509_NAME *x)
0f113f3e
MC
187{
188 unsigned long ret = 0;
189 unsigned char md[SHA_DIGEST_LENGTH];
190
191 /* Make sure X509_NAME structure contains valid cached encoding */
192 i2d_X509_NAME(x, NULL);
193 if (!EVP_Digest(x->canon_enc, x->canon_enclen, md, NULL, EVP_sha1(),
194 NULL))
195 return 0;
196
197 ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
198 ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
199 ) & 0xffffffffL;
26a7d938 200 return ret;
0f113f3e 201}
450ea834 202
cf1b7d96 203#ifndef OPENSSL_NO_MD5
0f113f3e
MC
204/*
205 * I now DER encode the name and hash it. Since I cache the DER encoding,
206 * this is reasonably efficient.
207 */
c2c99e28
DSH
208
209unsigned long X509_NAME_hash_old(X509_NAME *x)
0f113f3e 210{
bfb0641f 211 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
0f113f3e
MC
212 unsigned long ret = 0;
213 unsigned char md[16];
214
6e59a892
RL
215 if (md_ctx == NULL)
216 return ret;
217
0f113f3e
MC
218 /* Make sure X509_NAME structure contains valid cached encoding */
219 i2d_X509_NAME(x, NULL);
6e59a892
RL
220 EVP_MD_CTX_set_flags(md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
221 if (EVP_DigestInit_ex(md_ctx, EVP_md5(), NULL)
222 && EVP_DigestUpdate(md_ctx, x->bytes->data, x->bytes->length)
223 && EVP_DigestFinal_ex(md_ctx, md, NULL))
0f113f3e
MC
224 ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
225 ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
226 ) & 0xffffffffL;
bfb0641f 227 EVP_MD_CTX_free(md_ctx);
0f113f3e 228
26a7d938 229 return ret;
0f113f3e 230}
d02b48c6
RE
231#endif
232
233/* Search a stack of X509 for a match */
6b691a5c 234X509 *X509_find_by_issuer_and_serial(STACK_OF(X509) *sk, X509_NAME *name,
0f113f3e
MC
235 ASN1_INTEGER *serial)
236{
237 int i;
0f113f3e
MC
238 X509 x, *x509 = NULL;
239
240 if (!sk)
241 return NULL;
242
81e49438 243 x.cert_info.serialNumber = *serial;
5cf6abd8 244 x.cert_info.issuer = name;
0f113f3e
MC
245
246 for (i = 0; i < sk_X509_num(sk); i++) {
247 x509 = sk_X509_value(sk, i);
248 if (X509_issuer_and_serial_cmp(x509, &x) == 0)
26a7d938 249 return x509;
0f113f3e 250 }
26a7d938 251 return NULL;
0f113f3e 252}
d02b48c6 253
6b691a5c 254X509 *X509_find_by_subject(STACK_OF(X509) *sk, X509_NAME *name)
0f113f3e
MC
255{
256 X509 *x509;
257 int i;
258
259 for (i = 0; i < sk_X509_num(sk); i++) {
260 x509 = sk_X509_value(sk, i);
261 if (X509_NAME_cmp(X509_get_subject_name(x509), name) == 0)
26a7d938 262 return x509;
0f113f3e 263 }
26a7d938 264 return NULL;
0f113f3e 265}
d02b48c6 266
fdaf7bee 267EVP_PKEY *X509_get0_pubkey(const X509 *x)
c01ff880
DSH
268{
269 if (x == NULL)
270 return NULL;
271 return X509_PUBKEY_get0(x->cert_info.key);
272}
273
6b691a5c 274EVP_PKEY *X509_get_pubkey(X509 *x)
0f113f3e 275{
5cf6abd8 276 if (x == NULL)
c01ff880
DSH
277 return NULL;
278 return X509_PUBKEY_get(x->cert_info.key);
0f113f3e 279}
dfeab068 280
fdaf7bee 281int X509_check_private_key(const X509 *x, const EVP_PKEY *k)
0f113f3e 282{
fdaf7bee 283 const EVP_PKEY *xk;
0f113f3e
MC
284 int ret;
285
c01ff880 286 xk = X509_get0_pubkey(x);
0f113f3e
MC
287
288 if (xk)
289 ret = EVP_PKEY_cmp(xk, k);
290 else
291 ret = -2;
292
293 switch (ret) {
294 case 1:
295 break;
296 case 0:
297 X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_KEY_VALUES_MISMATCH);
298 break;
299 case -1:
300 X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_KEY_TYPE_MISMATCH);
301 break;
302 case -2:
303 X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_UNKNOWN_KEY_TYPE);
304 }
0f113f3e
MC
305 if (ret > 0)
306 return 1;
307 return 0;
308}
309
310/*
311 * Check a suite B algorithm is permitted: pass in a public key and the NID
312 * of its signature (or 0 if no signature). The pflags is a pointer to a
313 * flags field which must contain the suite B verification flags.
3ad344a5
DSH
314 */
315
14536c8c
DSH
316#ifndef OPENSSL_NO_EC
317
3ad344a5 318static int check_suite_b(EVP_PKEY *pkey, int sign_nid, unsigned long *pflags)
0f113f3e
MC
319{
320 const EC_GROUP *grp = NULL;
321 int curve_nid;
3aeb9348
DSH
322 if (pkey && EVP_PKEY_id(pkey) == EVP_PKEY_EC)
323 grp = EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey));
0f113f3e
MC
324 if (!grp)
325 return X509_V_ERR_SUITE_B_INVALID_ALGORITHM;
326 curve_nid = EC_GROUP_get_curve_name(grp);
327 /* Check curve is consistent with LOS */
328 if (curve_nid == NID_secp384r1) { /* P-384 */
329 /*
330 * Check signature algorithm is consistent with curve.
331 */
332 if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA384)
333 return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM;
334 if (!(*pflags & X509_V_FLAG_SUITEB_192_LOS))
335 return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED;
336 /* If we encounter P-384 we cannot use P-256 later */
337 *pflags &= ~X509_V_FLAG_SUITEB_128_LOS_ONLY;
338 } else if (curve_nid == NID_X9_62_prime256v1) { /* P-256 */
339 if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA256)
340 return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM;
341 if (!(*pflags & X509_V_FLAG_SUITEB_128_LOS_ONLY))
342 return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED;
343 } else
344 return X509_V_ERR_SUITE_B_INVALID_CURVE;
345
346 return X509_V_OK;
347}
3ad344a5 348
3b0648eb 349int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain,
0f113f3e
MC
350 unsigned long flags)
351{
352 int rv, i, sign_nid;
6e328256
VD
353 EVP_PKEY *pk;
354 unsigned long tflags = flags;
355
0f113f3e
MC
356 if (!(flags & X509_V_FLAG_SUITEB_128_LOS))
357 return X509_V_OK;
6e328256 358
0f113f3e
MC
359 /* If no EE certificate passed in must be first in chain */
360 if (x == NULL) {
361 x = sk_X509_value(chain, 0);
362 i = 1;
363 } else
364 i = 0;
365
6e328256
VD
366 pk = X509_get0_pubkey(x);
367
368 /*
369 * With DANE-EE(3) success, or DANE-EE(3)/PKIX-EE(1) failure we don't build
370 * a chain all, just report trust success or failure, but must also report
371 * Suite-B errors if applicable. This is indicated via a NULL chain
372 * pointer. All we need to do is check the leaf key algorithm.
373 */
374 if (chain == NULL)
375 return check_suite_b(pk, -1, &tflags);
376
0f113f3e
MC
377 if (X509_get_version(x) != 2) {
378 rv = X509_V_ERR_SUITE_B_INVALID_VERSION;
379 /* Correct error depth */
380 i = 0;
381 goto end;
382 }
383
0f113f3e
MC
384 /* Check EE key only */
385 rv = check_suite_b(pk, -1, &tflags);
386 if (rv != X509_V_OK) {
387 /* Correct error depth */
388 i = 0;
389 goto end;
390 }
391 for (; i < sk_X509_num(chain); i++) {
392 sign_nid = X509_get_signature_nid(x);
393 x = sk_X509_value(chain, i);
394 if (X509_get_version(x) != 2) {
395 rv = X509_V_ERR_SUITE_B_INVALID_VERSION;
396 goto end;
397 }
8382fd3a 398 pk = X509_get0_pubkey(x);
0f113f3e
MC
399 rv = check_suite_b(pk, sign_nid, &tflags);
400 if (rv != X509_V_OK)
401 goto end;
402 }
403
404 /* Final check: root CA signature */
405 rv = check_suite_b(pk, X509_get_signature_nid(x), &tflags);
406 end:
0f113f3e
MC
407 if (rv != X509_V_OK) {
408 /* Invalid signature or LOS errors are for previous cert */
409 if ((rv == X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM
410 || rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED) && i)
411 i--;
412 /*
413 * If we have LOS error and flags changed then we are signing P-384
0d4fb843 414 * with P-256. Use more meaningful error.
0f113f3e
MC
415 */
416 if (rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED && flags != tflags)
417 rv = X509_V_ERR_SUITE_B_CANNOT_SIGN_P_384_WITH_P_256;
418 if (perror_depth)
419 *perror_depth = i;
420 }
421 return rv;
422}
3ad344a5 423
3b0648eb 424int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags)
0f113f3e
MC
425{
426 int sign_nid;
427 if (!(flags & X509_V_FLAG_SUITEB_128_LOS))
428 return X509_V_OK;
6e63c142 429 sign_nid = OBJ_obj2nid(crl->crl.sig_alg.algorithm);
0f113f3e
MC
430 return check_suite_b(pk, sign_nid, &flags);
431}
14536c8c
DSH
432
433#else
434int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain,
0f113f3e
MC
435 unsigned long flags)
436{
437 return 0;
438}
14536c8c
DSH
439
440int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags)
0f113f3e
MC
441{
442 return 0;
443}
14536c8c
DSH
444
445#endif
0f113f3e
MC
446/*
447 * Not strictly speaking an "up_ref" as a STACK doesn't have a reference
448 * count but it has the same effect by duping the STACK and upping the ref of
449 * each X509 structure.
3b0648eb
DSH
450 */
451STACK_OF(X509) *X509_chain_up_ref(STACK_OF(X509) *chain)
0f113f3e
MC
452{
453 STACK_OF(X509) *ret;
454 int i;
455 ret = sk_X509_dup(chain);
2403153c
BE
456 if (ret == NULL)
457 return NULL;
0f113f3e
MC
458 for (i = 0; i < sk_X509_num(ret); i++) {
459 X509 *x = sk_X509_value(ret, i);
2403153c
BE
460 if (!X509_up_ref(x))
461 goto err;
0f113f3e
MC
462 }
463 return ret;
2403153c
BE
464 err:
465 while (i-- > 0)
466 X509_free (sk_X509_value(ret, i));
467 sk_X509_free(ret);
468 return NULL;
0f113f3e 469}