1 /*
2  * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
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 "crypto/x509.h"
17 
X509_issuer_and_serial_cmp(const X509 * a,const X509 * b)18 int X509_issuer_and_serial_cmp(const X509 *a, const X509 *b)
19 {
20     int i;
21     const X509_CINF *ai, *bi;
22 
23     ai = &a->cert_info;
24     bi = &b->cert_info;
25     i = ASN1_INTEGER_cmp(&ai->serialNumber, &bi->serialNumber);
26     if (i)
27         return i;
28     return X509_NAME_cmp(ai->issuer, bi->issuer);
29 }
30 
31 #ifndef OPENSSL_NO_MD5
X509_issuer_and_serial_hash(X509 * a)32 unsigned long X509_issuer_and_serial_hash(X509 *a)
33 {
34     unsigned long ret = 0;
35     EVP_MD_CTX *ctx = EVP_MD_CTX_new();
36     unsigned char md[16];
37     char *f;
38 
39     if (ctx == NULL)
40         goto err;
41     f = X509_NAME_oneline(a->cert_info.issuer, NULL, 0);
42     if (f == NULL)
43         goto err;
44     if (!EVP_DigestInit_ex(ctx, EVP_md5(), NULL))
45         goto err;
46     if (!EVP_DigestUpdate(ctx, (unsigned char *)f, strlen(f)))
47         goto err;
48     OPENSSL_free(f);
49     if (!EVP_DigestUpdate
50         (ctx, (unsigned char *)a->cert_info.serialNumber.data,
51          (unsigned long)a->cert_info.serialNumber.length))
52         goto err;
53     if (!EVP_DigestFinal_ex(ctx, &(md[0]), NULL))
54         goto err;
55     ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
56            ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
57         ) & 0xffffffffL;
58  err:
59     EVP_MD_CTX_free(ctx);
60     return ret;
61 }
62 #endif
63 
X509_issuer_name_cmp(const X509 * a,const X509 * b)64 int X509_issuer_name_cmp(const X509 *a, const X509 *b)
65 {
66     return X509_NAME_cmp(a->cert_info.issuer, b->cert_info.issuer);
67 }
68 
X509_subject_name_cmp(const X509 * a,const X509 * b)69 int X509_subject_name_cmp(const X509 *a, const X509 *b)
70 {
71     return X509_NAME_cmp(a->cert_info.subject, b->cert_info.subject);
72 }
73 
X509_CRL_cmp(const X509_CRL * a,const X509_CRL * b)74 int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b)
75 {
76     return X509_NAME_cmp(a->crl.issuer, b->crl.issuer);
77 }
78 
X509_CRL_match(const X509_CRL * a,const X509_CRL * b)79 int X509_CRL_match(const X509_CRL *a, const X509_CRL *b)
80 {
81     return memcmp(a->sha1_hash, b->sha1_hash, 20);
82 }
83 
X509_get_issuer_name(const X509 * a)84 X509_NAME *X509_get_issuer_name(const X509 *a)
85 {
86     return a->cert_info.issuer;
87 }
88 
X509_issuer_name_hash(X509 * x)89 unsigned long X509_issuer_name_hash(X509 *x)
90 {
91     return X509_NAME_hash(x->cert_info.issuer);
92 }
93 
94 #ifndef OPENSSL_NO_MD5
X509_issuer_name_hash_old(X509 * x)95 unsigned long X509_issuer_name_hash_old(X509 *x)
96 {
97     return X509_NAME_hash_old(x->cert_info.issuer);
98 }
99 #endif
100 
X509_get_subject_name(const X509 * a)101 X509_NAME *X509_get_subject_name(const X509 *a)
102 {
103     return a->cert_info.subject;
104 }
105 
X509_get_serialNumber(X509 * a)106 ASN1_INTEGER *X509_get_serialNumber(X509 *a)
107 {
108     return &a->cert_info.serialNumber;
109 }
110 
X509_get0_serialNumber(const X509 * a)111 const ASN1_INTEGER *X509_get0_serialNumber(const X509 *a)
112 {
113     return &a->cert_info.serialNumber;
114 }
115 
X509_subject_name_hash(X509 * x)116 unsigned long X509_subject_name_hash(X509 *x)
117 {
118     return X509_NAME_hash(x->cert_info.subject);
119 }
120 
121 #ifndef OPENSSL_NO_MD5
X509_subject_name_hash_old(X509 * x)122 unsigned long X509_subject_name_hash_old(X509 *x)
123 {
124     return X509_NAME_hash_old(x->cert_info.subject);
125 }
126 #endif
127 
128 /*
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.
135  */
X509_cmp(const X509 * a,const X509 * b)136 int X509_cmp(const X509 *a, const X509 *b)
137 {
138     int rv = 0;
139 
140     if (a == b) /* for efficiency */
141         return 0;
142 
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);
146 
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);
150     if (rv != 0)
151         return rv;
152 
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)
156             return -1;
157         if (a->cert_info.enc.len > b->cert_info.enc.len)
158             return 1;
159         return memcmp(a->cert_info.enc.enc, b->cert_info.enc.enc,
160                       a->cert_info.enc.len);
161     }
162     return rv;
163 }
164 
X509_NAME_cmp(const X509_NAME * a,const X509_NAME * b)165 int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b)
166 {
167     int ret;
168 
169     /* Ensure canonical encoding is present and up to date */
170 
171     if (!a->canon_enc || a->modified) {
172         ret = i2d_X509_NAME((X509_NAME *)a, NULL);
173         if (ret < 0)
174             return -2;
175     }
176 
177     if (!b->canon_enc || b->modified) {
178         ret = i2d_X509_NAME((X509_NAME *)b, NULL);
179         if (ret < 0)
180             return -2;
181     }
182 
183     ret = a->canon_enclen - b->canon_enclen;
184 
185     if (ret != 0 || a->canon_enclen == 0)
186         return ret;
187 
188     return memcmp(a->canon_enc, b->canon_enc, a->canon_enclen);
189 
190 }
191 
X509_NAME_hash(X509_NAME * x)192 unsigned long X509_NAME_hash(X509_NAME *x)
193 {
194     unsigned long ret = 0;
195     unsigned char md[SHA_DIGEST_LENGTH];
196 
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(),
200                     NULL))
201         return 0;
202 
203     ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
204            ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
205         ) & 0xffffffffL;
206     return ret;
207 }
208 
209 #ifndef OPENSSL_NO_MD5
210 /*
211  * I now DER encode the name and hash it.  Since I cache the DER encoding,
212  * this is reasonably efficient.
213  */
214 
X509_NAME_hash_old(X509_NAME * x)215 unsigned long X509_NAME_hash_old(X509_NAME *x)
216 {
217     EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
218     unsigned long ret = 0;
219     unsigned char md[16];
220 
221     if (md_ctx == NULL)
222         return ret;
223 
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)
232             ) & 0xffffffffL;
233     EVP_MD_CTX_free(md_ctx);
234 
235     return ret;
236 }
237 #endif
238 
239 /* Search a stack of X509 for a match */
X509_find_by_issuer_and_serial(STACK_OF (X509)* sk,X509_NAME * name,ASN1_INTEGER * serial)240 X509 *X509_find_by_issuer_and_serial(STACK_OF(X509) *sk, X509_NAME *name,
241                                      ASN1_INTEGER *serial)
242 {
243     int i;
244     X509 x, *x509 = NULL;
245 
246     if (!sk)
247         return NULL;
248 
249     x.cert_info.serialNumber = *serial;
250     x.cert_info.issuer = name;
251 
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)
255             return x509;
256     }
257     return NULL;
258 }
259 
X509_find_by_subject(STACK_OF (X509)* sk,X509_NAME * name)260 X509 *X509_find_by_subject(STACK_OF(X509) *sk, X509_NAME *name)
261 {
262     X509 *x509;
263     int i;
264 
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)
268             return x509;
269     }
270     return NULL;
271 }
272 
X509_get0_pubkey(const X509 * x)273 EVP_PKEY *X509_get0_pubkey(const X509 *x)
274 {
275     if (x == NULL)
276         return NULL;
277     return X509_PUBKEY_get0(x->cert_info.key);
278 }
279 
X509_get_pubkey(X509 * x)280 EVP_PKEY *X509_get_pubkey(X509 *x)
281 {
282     if (x == NULL)
283         return NULL;
284     return X509_PUBKEY_get(x->cert_info.key);
285 }
286 
X509_check_private_key(const X509 * x,const EVP_PKEY * k)287 int X509_check_private_key(const X509 *x, const EVP_PKEY *k)
288 {
289     const EVP_PKEY *xk;
290     int ret;
291 
292     xk = X509_get0_pubkey(x);
293 
294     if (xk)
295         ret = EVP_PKEY_cmp(xk, k);
296     else
297         ret = -2;
298 
299     switch (ret) {
300     case 1:
301         break;
302     case 0:
303         X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_KEY_VALUES_MISMATCH);
304         break;
305     case -1:
306         X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_KEY_TYPE_MISMATCH);
307         break;
308     case -2:
309         X509err(X509_F_X509_CHECK_PRIVATE_KEY, X509_R_UNKNOWN_KEY_TYPE);
310     }
311     if (ret > 0)
312         return 1;
313     return 0;
314 }
315 
316 /*
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.
320  */
321 
322 #ifndef OPENSSL_NO_EC
323 
check_suite_b(EVP_PKEY * pkey,int sign_nid,unsigned long * pflags)324 static int check_suite_b(EVP_PKEY *pkey, int sign_nid, unsigned long *pflags)
325 {
326     const EC_GROUP *grp = NULL;
327     int curve_nid;
328     if (pkey && EVP_PKEY_id(pkey) == EVP_PKEY_EC)
329         grp = EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey));
330     if (!grp)
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 */
335         /*
336          * Check signature algorithm is consistent with curve.
337          */
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;
349     } else
350         return X509_V_ERR_SUITE_B_INVALID_CURVE;
351 
352     return X509_V_OK;
353 }
354 
X509_chain_check_suiteb(int * perror_depth,X509 * x,STACK_OF (X509)* chain,unsigned long flags)355 int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain,
356                             unsigned long flags)
357 {
358     int rv, i, sign_nid;
359     EVP_PKEY *pk;
360     unsigned long tflags = flags;
361 
362     if (!(flags & X509_V_FLAG_SUITEB_128_LOS))
363         return X509_V_OK;
364 
365     /* If no EE certificate passed in must be first in chain */
366     if (x == NULL) {
367         x = sk_X509_value(chain, 0);
368         i = 1;
369     } else
370         i = 0;
371 
372     pk = X509_get0_pubkey(x);
373 
374     /*
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.
379      */
380     if (chain == NULL)
381         return check_suite_b(pk, -1, &tflags);
382 
383     if (X509_get_version(x) != 2) {
384         rv = X509_V_ERR_SUITE_B_INVALID_VERSION;
385         /* Correct error depth */
386         i = 0;
387         goto end;
388     }
389 
390     /* Check EE key only */
391     rv = check_suite_b(pk, -1, &tflags);
392     if (rv != X509_V_OK) {
393         /* Correct error depth */
394         i = 0;
395         goto end;
396     }
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;
402             goto end;
403         }
404         pk = X509_get0_pubkey(x);
405         rv = check_suite_b(pk, sign_nid, &tflags);
406         if (rv != X509_V_OK)
407             goto end;
408     }
409 
410     /* Final check: root CA signature */
411     rv = check_suite_b(pk, X509_get_signature_nid(x), &tflags);
412  end:
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)
417             i--;
418         /*
419          * If we have LOS error and flags changed then we are signing P-384
420          * with P-256. Use more meaningful error.
421          */
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;
424         if (perror_depth)
425             *perror_depth = i;
426     }
427     return rv;
428 }
429 
X509_CRL_check_suiteb(X509_CRL * crl,EVP_PKEY * pk,unsigned long flags)430 int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags)
431 {
432     int sign_nid;
433     if (!(flags & X509_V_FLAG_SUITEB_128_LOS))
434         return X509_V_OK;
435     sign_nid = OBJ_obj2nid(crl->crl.sig_alg.algorithm);
436     return check_suite_b(pk, sign_nid, &flags);
437 }
438 
439 #else
X509_chain_check_suiteb(int * perror_depth,X509 * x,STACK_OF (X509)* chain,unsigned long flags)440 int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain,
441                             unsigned long flags)
442 {
443     return 0;
444 }
445 
X509_CRL_check_suiteb(X509_CRL * crl,EVP_PKEY * pk,unsigned long flags)446 int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags)
447 {
448     return 0;
449 }
450 
451 #endif
452 /*
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.
456  */
STACK_OF(X509)457 STACK_OF(X509) *X509_chain_up_ref(STACK_OF(X509) *chain)
458 {
459     STACK_OF(X509) *ret;
460     int i;
461     ret = sk_X509_dup(chain);
462     if (ret == NULL)
463         return NULL;
464     for (i = 0; i < sk_X509_num(ret); i++) {
465         X509 *x = sk_X509_value(ret, i);
466         if (!X509_up_ref(x))
467             goto err;
468     }
469     return ret;
470  err:
471     while (i-- > 0)
472         X509_free (sk_X509_value(ret, i));
473     sk_X509_free(ret);
474     return NULL;
475 }
476