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