xref: /openbsd-src/lib/libcrypto/x509/x509_cmp.c (revision 88e5d4476be93cdd38d69b681fe8cc963fa2a4cd)
1 /* $OpenBSD: x509_cmp.c,v 1.37 2021/11/04 23:52:34 beck Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 
59 #include <ctype.h>
60 #include <stdio.h>
61 #include <string.h>
62 
63 #include <openssl/opensslconf.h>
64 
65 #include <openssl/asn1.h>
66 #include <openssl/err.h>
67 #include <openssl/objects.h>
68 #include <openssl/x509.h>
69 #include <openssl/x509v3.h>
70 
71 #include "x509_lcl.h"
72 
73 int
74 X509_issuer_and_serial_cmp(const X509 *a, const X509 *b)
75 {
76 	int i;
77 	X509_CINF *ai, *bi;
78 
79 	ai = a->cert_info;
80 	bi = b->cert_info;
81 	i = ASN1_INTEGER_cmp(ai->serialNumber, bi->serialNumber);
82 	if (i)
83 		return (i);
84 	return (X509_NAME_cmp(ai->issuer, bi->issuer));
85 }
86 
87 #ifndef OPENSSL_NO_MD5
88 unsigned long
89 X509_issuer_and_serial_hash(X509 *a)
90 {
91 	unsigned long ret = 0;
92 	EVP_MD_CTX ctx;
93 	unsigned char md[16];
94 	char *f;
95 
96 	EVP_MD_CTX_init(&ctx);
97 	f = X509_NAME_oneline(a->cert_info->issuer, NULL, 0);
98 	if (f == NULL)
99 		goto err;
100 	if (!EVP_DigestInit_ex(&ctx, EVP_md5(), NULL))
101 		goto err;
102 	if (!EVP_DigestUpdate(&ctx, (unsigned char *)f, strlen(f)))
103 		goto err;
104 	free(f);
105 	f = NULL;
106 	if (!EVP_DigestUpdate(&ctx,
107 	    (unsigned char *)a->cert_info->serialNumber->data,
108 	    (unsigned long)a->cert_info->serialNumber->length))
109 		goto err;
110 	if (!EVP_DigestFinal_ex(&ctx, &(md[0]), NULL))
111 		goto err;
112 	ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
113 	    ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)) &
114 	    0xffffffffL;
115 
116 err:
117 	EVP_MD_CTX_cleanup(&ctx);
118 	free(f);
119 	return (ret);
120 }
121 #endif
122 
123 int
124 X509_issuer_name_cmp(const X509 *a, const X509 *b)
125 {
126 	return (X509_NAME_cmp(a->cert_info->issuer, b->cert_info->issuer));
127 }
128 
129 int
130 X509_subject_name_cmp(const X509 *a, const X509 *b)
131 {
132 	return (X509_NAME_cmp(a->cert_info->subject, b->cert_info->subject));
133 }
134 
135 int
136 X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b)
137 {
138 	return (X509_NAME_cmp(a->crl->issuer, b->crl->issuer));
139 }
140 
141 #ifndef OPENSSL_NO_SHA
142 int
143 X509_CRL_match(const X509_CRL *a, const X509_CRL *b)
144 {
145 	return memcmp(a->sha1_hash, b->sha1_hash, 20);
146 }
147 #endif
148 
149 X509_NAME *
150 X509_get_issuer_name(const X509 *a)
151 {
152 	return (a->cert_info->issuer);
153 }
154 
155 unsigned long
156 X509_issuer_name_hash(X509 *x)
157 {
158 	return (X509_NAME_hash(x->cert_info->issuer));
159 }
160 
161 #ifndef OPENSSL_NO_MD5
162 unsigned long
163 X509_issuer_name_hash_old(X509 *x)
164 {
165 	return (X509_NAME_hash_old(x->cert_info->issuer));
166 }
167 #endif
168 
169 X509_NAME *
170 X509_get_subject_name(const X509 *a)
171 {
172 	return (a->cert_info->subject);
173 }
174 
175 ASN1_INTEGER *
176 X509_get_serialNumber(X509 *a)
177 {
178 	return (a->cert_info->serialNumber);
179 }
180 
181 const ASN1_INTEGER *
182 X509_get0_serialNumber(const X509 *a)
183 {
184 	return (a->cert_info->serialNumber);
185 }
186 
187 unsigned long
188 X509_subject_name_hash(X509 *x)
189 {
190 	return (X509_NAME_hash(x->cert_info->subject));
191 }
192 
193 #ifndef OPENSSL_NO_MD5
194 unsigned long
195 X509_subject_name_hash_old(X509 *x)
196 {
197 	return (X509_NAME_hash_old(x->cert_info->subject));
198 }
199 #endif
200 
201 #ifndef OPENSSL_NO_SHA
202 /* Compare two certificates: they must be identical for
203  * this to work. NB: Although "cmp" operations are generally
204  * prototyped to take "const" arguments (eg. for use in
205  * STACKs), the way X509 handling is - these operations may
206  * involve ensuring the hashes are up-to-date and ensuring
207  * certain cert information is cached. So this is the point
208  * where the "depth-first" constification tree has to halt
209  * with an evil cast.
210  */
211 int
212 X509_cmp(const X509 *a, const X509 *b)
213 {
214 	/* ensure hash is valid */
215 	X509_check_purpose((X509 *)a, -1, 0);
216 	X509_check_purpose((X509 *)b, -1, 0);
217 
218 	return memcmp(a->hash, b->hash, X509_CERT_HASH_LEN);
219 }
220 #endif
221 
222 int
223 X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b)
224 {
225 	int ret;
226 
227 	/* Ensure canonical encoding is present and up to date */
228 	if (!a->canon_enc || a->modified) {
229 		ret = i2d_X509_NAME((X509_NAME *)a, NULL);
230 		if (ret < 0)
231 			return -2;
232 	}
233 	if (!b->canon_enc || b->modified) {
234 		ret = i2d_X509_NAME((X509_NAME *)b, NULL);
235 		if (ret < 0)
236 			return -2;
237 	}
238 	ret = a->canon_enclen - b->canon_enclen;
239 	if (ret)
240 		return ret;
241 	return memcmp(a->canon_enc, b->canon_enc, a->canon_enclen);
242 }
243 
244 unsigned long
245 X509_NAME_hash(X509_NAME *x)
246 {
247 	unsigned long ret = 0;
248 	unsigned char md[SHA_DIGEST_LENGTH];
249 
250 	/* Make sure X509_NAME structure contains valid cached encoding */
251 	i2d_X509_NAME(x, NULL);
252 	if (!EVP_Digest(x->canon_enc, x->canon_enclen, md, NULL, EVP_sha1(),
253 	    NULL))
254 		return 0;
255 
256 	ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
257 	    ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)) &
258 	    0xffffffffL;
259 	return (ret);
260 }
261 
262 
263 #ifndef OPENSSL_NO_MD5
264 /* I now DER encode the name and hash it.  Since I cache the DER encoding,
265  * this is reasonably efficient. */
266 
267 unsigned long
268 X509_NAME_hash_old(X509_NAME *x)
269 {
270 	EVP_MD_CTX md_ctx;
271 	unsigned long ret = 0;
272 	unsigned char md[16];
273 
274 	/* Make sure X509_NAME structure contains valid cached encoding */
275 	i2d_X509_NAME(x, NULL);
276 	EVP_MD_CTX_init(&md_ctx);
277 	if (EVP_DigestInit_ex(&md_ctx, EVP_md5(), NULL) &&
278 	    EVP_DigestUpdate(&md_ctx, x->bytes->data, x->bytes->length) &&
279 	    EVP_DigestFinal_ex(&md_ctx, md, NULL))
280 		ret = (((unsigned long)md[0]) |
281 		    ((unsigned long)md[1] << 8L) |
282 		    ((unsigned long)md[2] << 16L) |
283 		    ((unsigned long)md[3] << 24L)) &
284 		    0xffffffffL;
285 	EVP_MD_CTX_cleanup(&md_ctx);
286 
287 	return (ret);
288 }
289 #endif
290 
291 /* Search a stack of X509 for a match */
292 X509 *
293 X509_find_by_issuer_and_serial(STACK_OF(X509) *sk, X509_NAME *name,
294     ASN1_INTEGER *serial)
295 {
296 	int i;
297 	X509_CINF cinf;
298 	X509 x, *x509 = NULL;
299 
300 	if (!sk)
301 		return NULL;
302 
303 	x.cert_info = &cinf;
304 	cinf.serialNumber = serial;
305 	cinf.issuer = name;
306 
307 	for (i = 0; i < sk_X509_num(sk); i++) {
308 		x509 = sk_X509_value(sk, i);
309 		if (X509_issuer_and_serial_cmp(x509, &x) == 0)
310 			return (x509);
311 	}
312 	return (NULL);
313 }
314 
315 X509 *
316 X509_find_by_subject(STACK_OF(X509) *sk, X509_NAME *name)
317 {
318 	X509 *x509;
319 	int i;
320 
321 	for (i = 0; i < sk_X509_num(sk); i++) {
322 		x509 = sk_X509_value(sk, i);
323 		if (X509_NAME_cmp(X509_get_subject_name(x509), name) == 0)
324 			return (x509);
325 	}
326 	return (NULL);
327 }
328 
329 EVP_PKEY *
330 X509_get_pubkey(X509 *x)
331 {
332 	if (x == NULL || x->cert_info == NULL)
333 		return (NULL);
334 	return (X509_PUBKEY_get(x->cert_info->key));
335 }
336 
337 EVP_PKEY *
338 X509_get0_pubkey(const X509 *x)
339 {
340 	if (x == NULL || x->cert_info == NULL)
341 		return (NULL);
342 	return (X509_PUBKEY_get0(x->cert_info->key));
343 }
344 
345 ASN1_BIT_STRING *
346 X509_get0_pubkey_bitstr(const X509 *x)
347 {
348 	if (!x)
349 		return NULL;
350 	return x->cert_info->key->public_key;
351 }
352 
353 int
354 X509_check_private_key(const X509 *x, const EVP_PKEY *k)
355 {
356 	const EVP_PKEY *xk;
357 	int ret;
358 
359 	xk = X509_get0_pubkey(x);
360 
361 	if (xk)
362 		ret = EVP_PKEY_cmp(xk, k);
363 	else
364 		ret = -2;
365 
366 	switch (ret) {
367 	case 1:
368 		break;
369 	case 0:
370 		X509error(X509_R_KEY_VALUES_MISMATCH);
371 		break;
372 	case -1:
373 		X509error(X509_R_KEY_TYPE_MISMATCH);
374 		break;
375 	case -2:
376 		X509error(X509_R_UNKNOWN_KEY_TYPE);
377 	}
378 	if (ret > 0)
379 		return 1;
380 	return 0;
381 }
382 
383 /*
384  * Not strictly speaking an "up_ref" as a STACK doesn't have a reference
385  * count but it has the same effect by duping the STACK and upping the ref of
386  * each X509 structure.
387  */
388 STACK_OF(X509) *
389 X509_chain_up_ref(STACK_OF(X509) *chain)
390 {
391 	STACK_OF(X509) *ret;
392 	size_t i;
393 
394 	ret = sk_X509_dup(chain);
395 	for (i = 0; i < sk_X509_num(ret); i++)
396 		X509_up_ref(sk_X509_value(ret, i));
397 
398 	return ret;
399 }
400