xref: /aosp_15_r20/external/cronet/third_party/boringssl/src/crypto/x509/x509_cmp.c (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 /* Copyright (C) 1995-1998 Eric Young ([email protected])
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young ([email protected]).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson ([email protected]).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young ([email protected])"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson ([email protected])"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.] */
56 
57 #include <string.h>
58 
59 #include <openssl/asn1.h>
60 #include <openssl/digest.h>
61 #include <openssl/err.h>
62 #include <openssl/mem.h>
63 #include <openssl/md5.h>
64 #include <openssl/obj.h>
65 #include <openssl/sha.h>
66 #include <openssl/stack.h>
67 #include <openssl/x509.h>
68 
69 #include "../internal.h"
70 #include "internal.h"
71 
72 
X509_issuer_name_cmp(const X509 * a,const X509 * b)73 int X509_issuer_name_cmp(const X509 *a, const X509 *b) {
74   return (X509_NAME_cmp(a->cert_info->issuer, b->cert_info->issuer));
75 }
76 
X509_subject_name_cmp(const X509 * a,const X509 * b)77 int X509_subject_name_cmp(const X509 *a, const X509 *b) {
78   return (X509_NAME_cmp(a->cert_info->subject, b->cert_info->subject));
79 }
80 
X509_CRL_cmp(const X509_CRL * a,const X509_CRL * b)81 int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b) {
82   return (X509_NAME_cmp(a->crl->issuer, b->crl->issuer));
83 }
84 
X509_CRL_match(const X509_CRL * a,const X509_CRL * b)85 int X509_CRL_match(const X509_CRL *a, const X509_CRL *b) {
86   return OPENSSL_memcmp(a->crl_hash, b->crl_hash, SHA256_DIGEST_LENGTH);
87 }
88 
X509_get_issuer_name(const X509 * a)89 X509_NAME *X509_get_issuer_name(const X509 *a) {
90   return a->cert_info->issuer;
91 }
92 
X509_issuer_name_hash(X509 * x)93 uint32_t X509_issuer_name_hash(X509 *x) {
94   return X509_NAME_hash(x->cert_info->issuer);
95 }
96 
X509_issuer_name_hash_old(X509 * x)97 uint32_t X509_issuer_name_hash_old(X509 *x) {
98   return (X509_NAME_hash_old(x->cert_info->issuer));
99 }
100 
X509_get_subject_name(const X509 * a)101 X509_NAME *X509_get_subject_name(const X509 *a) {
102   return a->cert_info->subject;
103 }
104 
X509_get_serialNumber(X509 * a)105 ASN1_INTEGER *X509_get_serialNumber(X509 *a) {
106   return a->cert_info->serialNumber;
107 }
108 
X509_get0_serialNumber(const X509 * x509)109 const ASN1_INTEGER *X509_get0_serialNumber(const X509 *x509) {
110   return x509->cert_info->serialNumber;
111 }
112 
X509_subject_name_hash(X509 * x)113 uint32_t X509_subject_name_hash(X509 *x) {
114   return X509_NAME_hash(x->cert_info->subject);
115 }
116 
X509_subject_name_hash_old(X509 * x)117 uint32_t X509_subject_name_hash_old(X509 *x) {
118   return X509_NAME_hash_old(x->cert_info->subject);
119 }
120 
121 // Compare two certificates: they must be identical for this to work. NB:
122 // Although "cmp" operations are generally prototyped to take "const"
123 // arguments (eg. for use in STACKs), the way X509 handling is - these
124 // operations may involve ensuring the hashes are up-to-date and ensuring
125 // certain cert information is cached. So this is the point where the
126 // "depth-first" constification tree has to halt with an evil cast.
X509_cmp(const X509 * a,const X509 * b)127 int X509_cmp(const X509 *a, const X509 *b) {
128   // Fill in the |cert_hash| fields.
129   //
130   // TODO(davidben): This may fail, in which case the the hash will be all
131   // zeros. This produces a consistent comparison (failures are sticky), but
132   // not a good one. OpenSSL now returns -2, but this is not a consistent
133   // comparison and may cause misbehaving sorts by transitivity. For now, we
134   // retain the old OpenSSL behavior, which was to ignore the error. See
135   // https://crbug.com/boringssl/355.
136   x509v3_cache_extensions((X509 *)a);
137   x509v3_cache_extensions((X509 *)b);
138 
139   return OPENSSL_memcmp(a->cert_hash, b->cert_hash, SHA256_DIGEST_LENGTH);
140 }
141 
X509_NAME_cmp(const X509_NAME * a,const X509_NAME * b)142 int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b) {
143   int ret;
144 
145   // Ensure canonical encoding is present and up to date
146 
147   if (!a->canon_enc || a->modified) {
148     ret = i2d_X509_NAME((X509_NAME *)a, NULL);
149     if (ret < 0) {
150       return -2;
151     }
152   }
153 
154   if (!b->canon_enc || b->modified) {
155     ret = i2d_X509_NAME((X509_NAME *)b, NULL);
156     if (ret < 0) {
157       return -2;
158     }
159   }
160 
161   ret = a->canon_enclen - b->canon_enclen;
162 
163   if (ret) {
164     return ret;
165   }
166 
167   return OPENSSL_memcmp(a->canon_enc, b->canon_enc, a->canon_enclen);
168 }
169 
X509_NAME_hash(X509_NAME * x)170 uint32_t X509_NAME_hash(X509_NAME *x) {
171   // Make sure the X509_NAME structure contains a valid cached encoding.
172   if (i2d_X509_NAME(x, NULL) < 0) {
173     return 0;
174   }
175 
176   uint8_t md[SHA_DIGEST_LENGTH];
177   SHA1(x->canon_enc, x->canon_enclen, md);
178   return CRYPTO_load_u32_le(md);
179 }
180 
181 // I now DER encode the name and hash it.  Since I cache the DER encoding,
182 // this is reasonably efficient.
183 
X509_NAME_hash_old(X509_NAME * x)184 uint32_t X509_NAME_hash_old(X509_NAME *x) {
185   // Make sure the X509_NAME structure contains a valid cached encoding.
186   if (i2d_X509_NAME(x, NULL) < 0) {
187     return 0;
188   }
189 
190   uint8_t md[SHA_DIGEST_LENGTH];
191   MD5((const uint8_t *)x->bytes->data, x->bytes->length, md);
192   return CRYPTO_load_u32_le(md);
193 }
194 
X509_find_by_issuer_and_serial(const STACK_OF (X509)* sk,X509_NAME * name,const ASN1_INTEGER * serial)195 X509 *X509_find_by_issuer_and_serial(const STACK_OF(X509) *sk, X509_NAME *name,
196                                      const ASN1_INTEGER *serial) {
197   if (serial->type != V_ASN1_INTEGER && serial->type != V_ASN1_NEG_INTEGER) {
198     return NULL;
199   }
200 
201   for (size_t i = 0; i < sk_X509_num(sk); i++) {
202     X509 *x509 = sk_X509_value(sk, i);
203     if (ASN1_INTEGER_cmp(X509_get0_serialNumber(x509), serial) == 0 &&
204         X509_NAME_cmp(X509_get_issuer_name(x509), name) == 0) {
205       return x509;
206     }
207   }
208   return NULL;
209 }
210 
X509_find_by_subject(const STACK_OF (X509)* sk,X509_NAME * name)211 X509 *X509_find_by_subject(const STACK_OF(X509) *sk, X509_NAME *name) {
212   for (size_t i = 0; i < sk_X509_num(sk); i++) {
213     X509 *x509 = sk_X509_value(sk, i);
214     if (X509_NAME_cmp(X509_get_subject_name(x509), name) == 0) {
215       return x509;
216     }
217   }
218   return NULL;
219 }
220 
X509_get0_pubkey(const X509 * x)221 EVP_PKEY *X509_get0_pubkey(const X509 *x) {
222   if (x == NULL) {
223     return NULL;
224   }
225   return X509_PUBKEY_get0(x->cert_info->key);
226 }
227 
X509_get_pubkey(const X509 * x)228 EVP_PKEY *X509_get_pubkey(const X509 *x) {
229   if (x == NULL) {
230     return NULL;
231   }
232   return X509_PUBKEY_get(x->cert_info->key);
233 }
234 
X509_get0_pubkey_bitstr(const X509 * x)235 ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x) {
236   if (!x) {
237     return NULL;
238   }
239   return x->cert_info->key->public_key;
240 }
241 
X509_check_private_key(const X509 * x,const EVP_PKEY * k)242 int X509_check_private_key(const X509 *x, const EVP_PKEY *k) {
243   const EVP_PKEY *xk = X509_get0_pubkey(x);
244   if (xk == NULL) {
245     return 0;
246   }
247 
248   int ret = EVP_PKEY_cmp(xk, k);
249   if (ret > 0) {
250     return 1;
251   }
252 
253   switch (ret) {
254     case 0:
255       OPENSSL_PUT_ERROR(X509, X509_R_KEY_VALUES_MISMATCH);
256       return 0;
257     case -1:
258       OPENSSL_PUT_ERROR(X509, X509_R_KEY_TYPE_MISMATCH);
259       return 0;
260     case -2:
261       OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
262       return 0;
263   }
264 
265   return 0;
266 }
267 
268 // Not strictly speaking an "up_ref" as a STACK doesn't have a reference
269 // count but it has the same effect by duping the STACK and upping the ref of
270 // each X509 structure.
STACK_OF(X509)271 STACK_OF(X509) *X509_chain_up_ref(STACK_OF(X509) *chain) {
272   STACK_OF(X509) *ret = sk_X509_dup(chain);
273   if (ret == NULL) {
274     return NULL;
275   }
276   for (size_t i = 0; i < sk_X509_num(ret); i++) {
277     X509_up_ref(sk_X509_value(ret, i));
278   }
279   return ret;
280 }
281