1/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
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 (tjh@cryptsoft.com).
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 (eay@cryptsoft.com)"
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 (tjh@cryptsoft.com)"
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/obj.h>
64#include <openssl/stack.h>
65#include <openssl/x509.h>
66#include <openssl/x509v3.h>
67
68#include "../internal.h"
69#include "../x509v3/internal.h"
70#include "internal.h"
71
72
73int X509_issuer_name_cmp(const X509 *a, const X509 *b) {
74 return (X509_NAME_cmp(a: a->cert_info->issuer, b: b->cert_info->issuer));
75}
76
77int X509_subject_name_cmp(const X509 *a, const X509 *b) {
78 return (X509_NAME_cmp(a: a->cert_info->subject, b: b->cert_info->subject));
79}
80
81int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b) {
82 return (X509_NAME_cmp(a: a->crl->issuer, b: b->crl->issuer));
83}
84
85int X509_CRL_match(const X509_CRL *a, const X509_CRL *b) {
86 return OPENSSL_memcmp(s1: a->crl_hash, s2: b->crl_hash, SHA256_DIGEST_LENGTH);
87}
88
89X509_NAME *X509_get_issuer_name(const X509 *a) {
90 return a->cert_info->issuer;
91}
92
93unsigned long X509_issuer_name_hash(X509 *x) {
94 return (X509_NAME_hash(x: x->cert_info->issuer));
95}
96
97unsigned long X509_issuer_name_hash_old(X509 *x) {
98 return (X509_NAME_hash_old(x: x->cert_info->issuer));
99}
100
101X509_NAME *X509_get_subject_name(const X509 *a) {
102 return a->cert_info->subject;
103}
104
105ASN1_INTEGER *X509_get_serialNumber(X509 *a) {
106 return a->cert_info->serialNumber;
107}
108
109const ASN1_INTEGER *X509_get0_serialNumber(const X509 *x509) {
110 return x509->cert_info->serialNumber;
111}
112
113unsigned long X509_subject_name_hash(X509 *x) {
114 return (X509_NAME_hash(x: x->cert_info->subject));
115}
116
117unsigned long X509_subject_name_hash_old(X509 *x) {
118 return (X509_NAME_hash_old(x: 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.
127int 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(x: (X509 *)a);
137 x509v3_cache_extensions(x: (X509 *)b);
138
139 return OPENSSL_memcmp(s1: a->cert_hash, s2: b->cert_hash, SHA256_DIGEST_LENGTH);
140}
141
142int 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(in: (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(in: (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(s1: a->canon_enc, s2: b->canon_enc, n: a->canon_enclen);
168}
169
170unsigned long X509_NAME_hash(X509_NAME *x) {
171 unsigned long ret = 0;
172 unsigned char md[SHA_DIGEST_LENGTH];
173
174 // Make sure X509_NAME structure contains valid cached encoding
175 i2d_X509_NAME(in: x, NULL);
176 if (!EVP_Digest(data: x->canon_enc, len: x->canon_enclen, md_out: md, NULL, type: EVP_sha1(), NULL)) {
177 return 0;
178 }
179
180 ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
181 ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)) &
182 0xffffffffL;
183 return ret;
184}
185
186// I now DER encode the name and hash it. Since I cache the DER encoding,
187// this is reasonably efficient.
188
189unsigned long X509_NAME_hash_old(X509_NAME *x) {
190 EVP_MD_CTX md_ctx;
191 unsigned long ret = 0;
192 unsigned char md[16];
193
194 // Make sure X509_NAME structure contains valid cached encoding
195 i2d_X509_NAME(in: x, NULL);
196 EVP_MD_CTX_init(ctx: &md_ctx);
197 // EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
198 if (EVP_DigestInit_ex(ctx: &md_ctx, type: EVP_md5(), NULL) &&
199 EVP_DigestUpdate(ctx: &md_ctx, data: x->bytes->data, len: x->bytes->length) &&
200 EVP_DigestFinal_ex(ctx: &md_ctx, md_out: md, NULL)) {
201 ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
202 ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)) &
203 0xffffffffL;
204 }
205 EVP_MD_CTX_cleanup(ctx: &md_ctx);
206
207 return ret;
208}
209
210X509 *X509_find_by_issuer_and_serial(const STACK_OF(X509) *sk, X509_NAME *name,
211 const ASN1_INTEGER *serial) {
212 if (serial->type != V_ASN1_INTEGER && serial->type != V_ASN1_NEG_INTEGER) {
213 return NULL;
214 }
215
216 for (size_t i = 0; i < sk_X509_num(sk); i++) {
217 X509 *x509 = sk_X509_value(sk, i);
218 if (ASN1_INTEGER_cmp(x: X509_get0_serialNumber(x509), y: serial) == 0 &&
219 X509_NAME_cmp(a: X509_get_issuer_name(a: x509), b: name) == 0) {
220 return x509;
221 }
222 }
223 return NULL;
224}
225
226X509 *X509_find_by_subject(const STACK_OF(X509) *sk, X509_NAME *name) {
227 for (size_t i = 0; i < sk_X509_num(sk); i++) {
228 X509 *x509 = sk_X509_value(sk, i);
229 if (X509_NAME_cmp(a: X509_get_subject_name(a: x509), b: name) == 0) {
230 return x509;
231 }
232 }
233 return NULL;
234}
235
236EVP_PKEY *X509_get_pubkey(X509 *x) {
237 if ((x == NULL) || (x->cert_info == NULL)) {
238 return NULL;
239 }
240 return (X509_PUBKEY_get(key: x->cert_info->key));
241}
242
243ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x) {
244 if (!x) {
245 return NULL;
246 }
247 return x->cert_info->key->public_key;
248}
249
250int X509_check_private_key(X509 *x, const EVP_PKEY *k) {
251 EVP_PKEY *xk;
252 int ret;
253
254 xk = X509_get_pubkey(x);
255
256 if (xk) {
257 ret = EVP_PKEY_cmp(a: xk, b: k);
258 } else {
259 ret = -2;
260 }
261
262 switch (ret) {
263 case 1:
264 break;
265 case 0:
266 OPENSSL_PUT_ERROR(X509, X509_R_KEY_VALUES_MISMATCH);
267 break;
268 case -1:
269 OPENSSL_PUT_ERROR(X509, X509_R_KEY_TYPE_MISMATCH);
270 break;
271 case -2:
272 OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE);
273 }
274 if (xk) {
275 EVP_PKEY_free(pkey: xk);
276 }
277 if (ret > 0) {
278 return 1;
279 }
280 return 0;
281}
282
283// Not strictly speaking an "up_ref" as a STACK doesn't have a reference
284// count but it has the same effect by duping the STACK and upping the ref of
285// each X509 structure.
286STACK_OF(X509) *X509_chain_up_ref(STACK_OF(X509) *chain) {
287 STACK_OF(X509) *ret = sk_X509_dup(sk: chain);
288 if (ret == NULL) {
289 return NULL;
290 }
291 for (size_t i = 0; i < sk_X509_num(sk: ret); i++) {
292 X509_up_ref(x509: sk_X509_value(sk: ret, i));
293 }
294 return ret;
295}
296

source code of dart_sdk/third_party/boringssl/src/crypto/x509/x509_cmp.c