1 /* crypto/x509/x509_cmp.c */
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 <stdio.h>
60 #include <ctype.h>
61 #include "cryptlib.h"
62 #include <openssl/asn1.h>
63 #include <openssl/objects.h>
64 #include <openssl/x509.h>
65 #include <openssl/x509v3.h>
66
67 __RCSID("$MirOS: src/lib/libssl/src/crypto/x509/x509_cmp.c,v 1.4 2014/03/23 22:50:59 tg Exp $");
68
69 #define X509_NAME_hash_old X509_NAME_hash
70 unsigned long X509_NAME_hash_new(X509_NAME *x);
71
X509_issuer_and_serial_cmp(const X509 * a,const X509 * b)72 int X509_issuer_and_serial_cmp(const X509 *a, const X509 *b)
73 {
74 int i;
75 X509_CINF *ai,*bi;
76
77 ai=a->cert_info;
78 bi=b->cert_info;
79 i=M_ASN1_INTEGER_cmp(ai->serialNumber,bi->serialNumber);
80 if (i) return(i);
81 return(X509_NAME_cmp(ai->issuer,bi->issuer));
82 }
83
84 #ifndef OPENSSL_NO_MD5
X509_issuer_and_serial_hash(X509 * a)85 unsigned long X509_issuer_and_serial_hash(X509 *a)
86 {
87 unsigned long ret=0;
88 EVP_MD_CTX ctx;
89 unsigned char md[16];
90 char *f;
91
92 EVP_MD_CTX_init(&ctx);
93 f=X509_NAME_oneline(a->cert_info->issuer,NULL,0);
94 ret=strlen(f);
95 EVP_DigestInit_ex(&ctx, EVP_md5(), NULL);
96 EVP_DigestUpdate(&ctx,(unsigned char *)f,ret);
97 OPENSSL_free(f);
98 EVP_DigestUpdate(&ctx,(unsigned char *)a->cert_info->serialNumber->data,
99 (unsigned long)a->cert_info->serialNumber->length);
100 EVP_DigestFinal_ex(&ctx,&(md[0]),NULL);
101 ret=( ((unsigned long)md[0] )|((unsigned long)md[1]<<8L)|
102 ((unsigned long)md[2]<<16L)|((unsigned long)md[3]<<24L)
103 )&0xffffffffL;
104 EVP_MD_CTX_cleanup(&ctx);
105 return(ret);
106 }
107 #endif
108
X509_issuer_name_cmp(const X509 * a,const X509 * b)109 int X509_issuer_name_cmp(const X509 *a, const X509 *b)
110 {
111 return(X509_NAME_cmp(a->cert_info->issuer,b->cert_info->issuer));
112 }
113
X509_subject_name_cmp(const X509 * a,const X509 * b)114 int X509_subject_name_cmp(const X509 *a, const X509 *b)
115 {
116 return(X509_NAME_cmp(a->cert_info->subject,b->cert_info->subject));
117 }
118
X509_CRL_cmp(const X509_CRL * a,const X509_CRL * b)119 int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b)
120 {
121 return(X509_NAME_cmp(a->crl->issuer,b->crl->issuer));
122 }
123
X509_get_issuer_name(X509 * a)124 X509_NAME *X509_get_issuer_name(X509 *a)
125 {
126 return(a->cert_info->issuer);
127 }
128
X509_issuer_name_hash(X509 * x)129 unsigned long X509_issuer_name_hash(X509 *x)
130 {
131 return(X509_NAME_hash(x->cert_info->issuer));
132 }
X509_issuer_name_hash_old(X509 * x)133 unsigned long X509_issuer_name_hash_old(X509 *x)
134 {
135 return(X509_NAME_hash(x->cert_info->issuer));
136 }
X509_issuer_name_hash_new(X509 * x)137 unsigned long X509_issuer_name_hash_new(X509 *x)
138 {
139 return(X509_NAME_hash_new(x->cert_info->issuer));
140 }
141
X509_get_subject_name(X509 * a)142 X509_NAME *X509_get_subject_name(X509 *a)
143 {
144 return(a->cert_info->subject);
145 }
146
X509_get_serialNumber(X509 * a)147 ASN1_INTEGER *X509_get_serialNumber(X509 *a)
148 {
149 return(a->cert_info->serialNumber);
150 }
151
X509_subject_name_hash(X509 * x)152 unsigned long X509_subject_name_hash(X509 *x)
153 {
154 return(X509_NAME_hash(x->cert_info->subject));
155 }
X509_subject_name_hash_old(X509 * x)156 unsigned long X509_subject_name_hash_old(X509 *x)
157 {
158 return(X509_NAME_hash(x->cert_info->subject));
159 }
X509_subject_name_hash_new(X509 * x)160 unsigned long X509_subject_name_hash_new(X509 *x)
161 {
162 return(X509_NAME_hash_new(x->cert_info->subject));
163 }
164
165 #ifndef OPENSSL_NO_SHA
166 /* Compare two certificates: they must be identical for
167 * this to work. NB: Although "cmp" operations are generally
168 * prototyped to take "const" arguments (eg. for use in
169 * STACKs), the way X509 handling is - these operations may
170 * involve ensuring the hashes are up-to-date and ensuring
171 * certain cert information is cached. So this is the point
172 * where the "depth-first" constification tree has to halt
173 * with an evil cast.
174 */
X509_cmp(const X509 * a,const X509 * b)175 int X509_cmp(const X509 *a, const X509 *b)
176 {
177 /* ensure hash is valid */
178 X509_check_purpose((X509 *)a, -1, 0);
179 X509_check_purpose((X509 *)b, -1, 0);
180
181 return memcmp(a->sha1_hash, b->sha1_hash, SHA_DIGEST_LENGTH);
182 }
183 #endif
184
185
186 /* Case insensitive string comparison */
nocase_cmp(const ASN1_STRING * a,const ASN1_STRING * b)187 static int nocase_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
188 {
189 int i;
190
191 if (a->length != b->length)
192 return (a->length - b->length);
193
194 for (i=0; i<a->length; i++)
195 {
196 int ca, cb;
197
198 ca = tolower(a->data[i]);
199 cb = tolower(b->data[i]);
200
201 if (ca != cb)
202 return(ca-cb);
203 }
204 return 0;
205 }
206
207 /* Case insensitive string comparison with space normalization
208 * Space normalization - ignore leading, trailing spaces,
209 * multiple spaces between characters are replaced by single space
210 */
nocase_spacenorm_cmp(const ASN1_STRING * a,const ASN1_STRING * b)211 static int nocase_spacenorm_cmp(const ASN1_STRING *a, const ASN1_STRING *b)
212 {
213 unsigned char *pa = NULL, *pb = NULL;
214 int la, lb;
215
216 la = a->length;
217 lb = b->length;
218 pa = a->data;
219 pb = b->data;
220
221 /* skip leading spaces */
222 while (la > 0 && isspace(*pa))
223 {
224 la--;
225 pa++;
226 }
227 while (lb > 0 && isspace(*pb))
228 {
229 lb--;
230 pb++;
231 }
232
233 /* skip trailing spaces */
234 while (la > 0 && isspace(pa[la-1]))
235 la--;
236 while (lb > 0 && isspace(pb[lb-1]))
237 lb--;
238
239 /* compare strings with space normalization */
240 while (la > 0 && lb > 0)
241 {
242 int ca, cb;
243
244 /* compare character */
245 ca = tolower(*pa);
246 cb = tolower(*pb);
247 if (ca != cb)
248 return (ca - cb);
249
250 pa++; pb++;
251 la--; lb--;
252
253 if (la <= 0 || lb <= 0)
254 break;
255
256 /* is white space next character ? */
257 if (isspace(*pa) && isspace(*pb))
258 {
259 /* skip remaining white spaces */
260 while (la > 0 && isspace(*pa))
261 {
262 la--;
263 pa++;
264 }
265 while (lb > 0 && isspace(*pb))
266 {
267 lb--;
268 pb++;
269 }
270 }
271 }
272 if (la > 0 || lb > 0)
273 return la - lb;
274
275 return 0;
276 }
277
asn1_string_memcmp(ASN1_STRING * a,ASN1_STRING * b)278 static int asn1_string_memcmp(ASN1_STRING *a, ASN1_STRING *b)
279 {
280 int j;
281 j = a->length - b->length;
282 if (j)
283 return j;
284 return memcmp(a->data, b->data, a->length);
285 }
286
287 #define STR_TYPE_CMP (B_ASN1_PRINTABLESTRING|B_ASN1_T61STRING|B_ASN1_UTF8STRING)
288
X509_NAME_cmp(const X509_NAME * a,const X509_NAME * b)289 int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b)
290 {
291 int i,j;
292 X509_NAME_ENTRY *na,*nb;
293
294 unsigned long nabit, nbbit;
295
296 j = sk_X509_NAME_ENTRY_num(a->entries)
297 - sk_X509_NAME_ENTRY_num(b->entries);
298 if (j)
299 return j;
300 for (i=sk_X509_NAME_ENTRY_num(a->entries)-1; i>=0; i--)
301 {
302 na=sk_X509_NAME_ENTRY_value(a->entries,i);
303 nb=sk_X509_NAME_ENTRY_value(b->entries,i);
304 j=na->value->type-nb->value->type;
305 if (j)
306 {
307 nabit = ASN1_tag2bit(na->value->type);
308 nbbit = ASN1_tag2bit(nb->value->type);
309 if (!(nabit & STR_TYPE_CMP) ||
310 !(nbbit & STR_TYPE_CMP))
311 return j;
312 j = asn1_string_memcmp(na->value, nb->value);
313 }
314 else if (na->value->type == V_ASN1_PRINTABLESTRING)
315 j=nocase_spacenorm_cmp(na->value, nb->value);
316 else if (na->value->type == V_ASN1_IA5STRING
317 && OBJ_obj2nid(na->object) == NID_pkcs9_emailAddress)
318 j=nocase_cmp(na->value, nb->value);
319 else
320 j = asn1_string_memcmp(na->value, nb->value);
321 if (j) return(j);
322 j=na->set-nb->set;
323 if (j) return(j);
324 }
325
326 /* We will check the object types after checking the values
327 * since the values will more often be different than the object
328 * types. */
329 for (i=sk_X509_NAME_ENTRY_num(a->entries)-1; i>=0; i--)
330 {
331 na=sk_X509_NAME_ENTRY_value(a->entries,i);
332 nb=sk_X509_NAME_ENTRY_value(b->entries,i);
333 j=OBJ_cmp(na->object,nb->object);
334 if (j) return(j);
335 }
336 return(0);
337 }
338
X509_NAME_hash_new(X509_NAME * x)339 unsigned long X509_NAME_hash_new(X509_NAME *x)
340 {
341 unsigned long ret=0;
342 unsigned char md[SHA_DIGEST_LENGTH];
343
344 /* Make sure X509_NAME structure contains valid cached encoding */
345 i2d_X509_NAME(x,NULL);
346 if (!EVP_Digest(x->canon_enc, x->canon_enclen, md, NULL, EVP_sha1(),
347 NULL))
348 return 0;
349
350 ret=( ((unsigned long)md[0] )|((unsigned long)md[1]<<8L)|
351 ((unsigned long)md[2]<<16L)|((unsigned long)md[3]<<24L)
352 )&0xffffffffL;
353 return(ret);
354 }
355
356 #ifndef OPENSSL_NO_MD5
357 /* I now DER encode the name and hash it. Since I cache the DER encoding,
358 * this is reasonably efficient. */
359
X509_NAME_hash(X509_NAME * x)360 unsigned long X509_NAME_hash(X509_NAME *x)
361 {
362 EVP_MD_CTX md_ctx;
363 unsigned long ret=0;
364 unsigned char md[16];
365
366 /* Make sure X509_NAME structure contains valid cached encoding */
367 i2d_X509_NAME(x,NULL);
368 EVP_MD_CTX_init(&md_ctx);
369 EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
370 if (EVP_DigestInit_ex(&md_ctx, EVP_md5(), NULL)
371 && EVP_DigestUpdate(&md_ctx, x->bytes->data, x->bytes->length)
372 && EVP_DigestFinal_ex(&md_ctx,md,NULL))
373 ret=(((unsigned long)md[0] )|((unsigned long)md[1]<<8L)|
374 ((unsigned long)md[2]<<16L)|((unsigned long)md[3]<<24L)
375 )&0xffffffffL;
376 EVP_MD_CTX_cleanup(&md_ctx);
377
378 return(ret);
379 }
380 #endif
381
382 /* Search a stack of X509 for a match */
X509_find_by_issuer_and_serial(STACK_OF (X509)* sk,X509_NAME * name,ASN1_INTEGER * serial)383 X509 *X509_find_by_issuer_and_serial(STACK_OF(X509) *sk, X509_NAME *name,
384 ASN1_INTEGER *serial)
385 {
386 int i;
387 X509_CINF cinf;
388 X509 x,*x509=NULL;
389
390 if(!sk) return NULL;
391
392 x.cert_info= &cinf;
393 cinf.serialNumber=serial;
394 cinf.issuer=name;
395
396 for (i=0; i<sk_X509_num(sk); i++)
397 {
398 x509=sk_X509_value(sk,i);
399 if (X509_issuer_and_serial_cmp(x509,&x) == 0)
400 return(x509);
401 }
402 return(NULL);
403 }
404
X509_find_by_subject(STACK_OF (X509)* sk,X509_NAME * name)405 X509 *X509_find_by_subject(STACK_OF(X509) *sk, X509_NAME *name)
406 {
407 X509 *x509;
408 int i;
409
410 for (i=0; i<sk_X509_num(sk); i++)
411 {
412 x509=sk_X509_value(sk,i);
413 if (X509_NAME_cmp(X509_get_subject_name(x509),name) == 0)
414 return(x509);
415 }
416 return(NULL);
417 }
418
X509_get_pubkey(X509 * x)419 EVP_PKEY *X509_get_pubkey(X509 *x)
420 {
421 if ((x == NULL) || (x->cert_info == NULL))
422 return(NULL);
423 return(X509_PUBKEY_get(x->cert_info->key));
424 }
425
X509_get0_pubkey_bitstr(const X509 * x)426 ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x)
427 {
428 if(!x) return NULL;
429 return x->cert_info->key->public_key;
430 }
431
X509_check_private_key(X509 * x,EVP_PKEY * k)432 int X509_check_private_key(X509 *x, EVP_PKEY *k)
433 {
434 EVP_PKEY *xk=NULL;
435 int ok=0;
436
437 xk=X509_get_pubkey(x);
438 if (xk->type != k->type)
439 {
440 X509err(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_KEY_TYPE_MISMATCH);
441 goto err;
442 }
443 switch (k->type)
444 {
445 #ifndef OPENSSL_NO_RSA
446 case EVP_PKEY_RSA:
447 if (BN_cmp(xk->pkey.rsa->n,k->pkey.rsa->n) != 0
448 || BN_cmp(xk->pkey.rsa->e,k->pkey.rsa->e) != 0)
449 {
450 X509err(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_KEY_VALUES_MISMATCH);
451 goto err;
452 }
453 break;
454 #endif
455 #ifndef OPENSSL_NO_DSA
456 case EVP_PKEY_DSA:
457 if (BN_cmp(xk->pkey.dsa->pub_key,k->pkey.dsa->pub_key) != 0)
458 {
459 X509err(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_KEY_VALUES_MISMATCH);
460 goto err;
461 }
462 break;
463 #endif
464 #ifndef OPENSSL_NO_DH
465 case EVP_PKEY_DH:
466 /* No idea */
467 X509err(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_CANT_CHECK_DH_KEY);
468 goto err;
469 #endif
470 default:
471 X509err(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_UNKNOWN_KEY_TYPE);
472 goto err;
473 }
474
475 ok=1;
476 err:
477 EVP_PKEY_free(xk);
478 return(ok);
479 }
480