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