sl@0: /* crypto/x509/x509_cmp.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include "cryptlib.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: EXPORT_C int X509_issuer_and_serial_cmp(const X509 *a, const X509 *b) sl@0: { sl@0: int i; sl@0: X509_CINF *ai,*bi; sl@0: sl@0: ai=a->cert_info; sl@0: bi=b->cert_info; sl@0: i=M_ASN1_INTEGER_cmp(ai->serialNumber,bi->serialNumber); sl@0: if (i) return(i); sl@0: return(X509_NAME_cmp(ai->issuer,bi->issuer)); sl@0: } sl@0: sl@0: #ifndef OPENSSL_NO_MD5 sl@0: EXPORT_C unsigned long X509_issuer_and_serial_hash(X509 *a) sl@0: { sl@0: unsigned long ret=0; sl@0: EVP_MD_CTX ctx; sl@0: unsigned char md[16]; sl@0: char *f; sl@0: sl@0: EVP_MD_CTX_init(&ctx); sl@0: f=X509_NAME_oneline(a->cert_info->issuer,NULL,0); sl@0: ret=strlen(f); sl@0: EVP_DigestInit_ex(&ctx, EVP_md5(), NULL); sl@0: EVP_DigestUpdate(&ctx,(unsigned char *)f,ret); sl@0: OPENSSL_free(f); sl@0: EVP_DigestUpdate(&ctx,(unsigned char *)a->cert_info->serialNumber->data, sl@0: (unsigned long)a->cert_info->serialNumber->length); sl@0: EVP_DigestFinal_ex(&ctx,&(md[0]),NULL); sl@0: ret=( ((unsigned long)md[0] )|((unsigned long)md[1]<<8L)| sl@0: ((unsigned long)md[2]<<16L)|((unsigned long)md[3]<<24L) sl@0: )&0xffffffffL; sl@0: EVP_MD_CTX_cleanup(&ctx); sl@0: return(ret); sl@0: } sl@0: #endif sl@0: sl@0: EXPORT_C int X509_issuer_name_cmp(const X509 *a, const X509 *b) sl@0: { sl@0: return(X509_NAME_cmp(a->cert_info->issuer,b->cert_info->issuer)); sl@0: } sl@0: sl@0: EXPORT_C int X509_subject_name_cmp(const X509 *a, const X509 *b) sl@0: { sl@0: return(X509_NAME_cmp(a->cert_info->subject,b->cert_info->subject)); sl@0: } sl@0: sl@0: EXPORT_C int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b) sl@0: { sl@0: return(X509_NAME_cmp(a->crl->issuer,b->crl->issuer)); sl@0: } sl@0: sl@0: EXPORT_C X509_NAME *X509_get_issuer_name(X509 *a) sl@0: { sl@0: return(a->cert_info->issuer); sl@0: } sl@0: sl@0: EXPORT_C unsigned long X509_issuer_name_hash(X509 *x) sl@0: { sl@0: return(X509_NAME_hash(x->cert_info->issuer)); sl@0: } sl@0: sl@0: EXPORT_C X509_NAME *X509_get_subject_name(X509 *a) sl@0: { sl@0: return(a->cert_info->subject); sl@0: } sl@0: sl@0: EXPORT_C ASN1_INTEGER *X509_get_serialNumber(X509 *a) sl@0: { sl@0: return(a->cert_info->serialNumber); sl@0: } sl@0: sl@0: EXPORT_C unsigned long X509_subject_name_hash(X509 *x) sl@0: { sl@0: return(X509_NAME_hash(x->cert_info->subject)); sl@0: } sl@0: sl@0: #ifndef OPENSSL_NO_SHA sl@0: /* Compare two certificates: they must be identical for sl@0: * this to work. NB: Although "cmp" operations are generally sl@0: * prototyped to take "const" arguments (eg. for use in sl@0: * STACKs), the way X509 handling is - these operations may sl@0: * involve ensuring the hashes are up-to-date and ensuring sl@0: * certain cert information is cached. So this is the point sl@0: * where the "depth-first" constification tree has to halt sl@0: * with an evil cast. sl@0: */ sl@0: EXPORT_C int X509_cmp(const X509 *a, const X509 *b) sl@0: { sl@0: /* ensure hash is valid */ sl@0: X509_check_purpose((X509 *)a, -1, 0); sl@0: X509_check_purpose((X509 *)b, -1, 0); sl@0: sl@0: return memcmp(a->sha1_hash, b->sha1_hash, SHA_DIGEST_LENGTH); sl@0: } sl@0: #endif sl@0: sl@0: sl@0: /* Case insensitive string comparision */ sl@0: static int nocase_cmp(const ASN1_STRING *a, const ASN1_STRING *b) sl@0: { sl@0: int i; sl@0: sl@0: if (a->length != b->length) sl@0: return (a->length - b->length); sl@0: sl@0: for (i=0; ilength; i++) sl@0: { sl@0: int ca, cb; sl@0: sl@0: ca = tolower(a->data[i]); sl@0: cb = tolower(b->data[i]); sl@0: sl@0: if (ca != cb) sl@0: return(ca-cb); sl@0: } sl@0: return 0; sl@0: } sl@0: sl@0: /* Case insensitive string comparision with space normalization sl@0: * Space normalization - ignore leading, trailing spaces, sl@0: * multiple spaces between characters are replaced by single space sl@0: */ sl@0: static int nocase_spacenorm_cmp(const ASN1_STRING *a, const ASN1_STRING *b) sl@0: { sl@0: unsigned char *pa = NULL, *pb = NULL; sl@0: int la, lb; sl@0: sl@0: la = a->length; sl@0: lb = b->length; sl@0: pa = a->data; sl@0: pb = b->data; sl@0: sl@0: /* skip leading spaces */ sl@0: while (la > 0 && isspace(*pa)) sl@0: { sl@0: la--; sl@0: pa++; sl@0: } sl@0: while (lb > 0 && isspace(*pb)) sl@0: { sl@0: lb--; sl@0: pb++; sl@0: } sl@0: sl@0: /* skip trailing spaces */ sl@0: while (la > 0 && isspace(pa[la-1])) sl@0: la--; sl@0: while (lb > 0 && isspace(pb[lb-1])) sl@0: lb--; sl@0: sl@0: /* compare strings with space normalization */ sl@0: while (la > 0 && lb > 0) sl@0: { sl@0: int ca, cb; sl@0: sl@0: /* compare character */ sl@0: ca = tolower(*pa); sl@0: cb = tolower(*pb); sl@0: if (ca != cb) sl@0: return (ca - cb); sl@0: sl@0: pa++; pb++; sl@0: la--; lb--; sl@0: sl@0: if (la <= 0 || lb <= 0) sl@0: break; sl@0: sl@0: /* is white space next character ? */ sl@0: if (isspace(*pa) && isspace(*pb)) sl@0: { sl@0: /* skip remaining white spaces */ sl@0: while (la > 0 && isspace(*pa)) sl@0: { sl@0: la--; sl@0: pa++; sl@0: } sl@0: while (lb > 0 && isspace(*pb)) sl@0: { sl@0: lb--; sl@0: pb++; sl@0: } sl@0: } sl@0: } sl@0: if (la > 0 || lb > 0) sl@0: return la - lb; sl@0: sl@0: return 0; sl@0: } sl@0: sl@0: static int asn1_string_memcmp(ASN1_STRING *a, ASN1_STRING *b) sl@0: { sl@0: int j; sl@0: j = a->length - b->length; sl@0: if (j) sl@0: return j; sl@0: return memcmp(a->data, b->data, a->length); sl@0: } sl@0: sl@0: #define STR_TYPE_CMP (B_ASN1_PRINTABLESTRING|B_ASN1_T61STRING|B_ASN1_UTF8STRING) sl@0: sl@0: EXPORT_C int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b) sl@0: { sl@0: int i,j; sl@0: X509_NAME_ENTRY *na,*nb; sl@0: sl@0: unsigned long nabit, nbbit; sl@0: sl@0: j = sk_X509_NAME_ENTRY_num(a->entries) sl@0: - sk_X509_NAME_ENTRY_num(b->entries); sl@0: if (j) sl@0: return j; sl@0: for (i=sk_X509_NAME_ENTRY_num(a->entries)-1; i>=0; i--) sl@0: { sl@0: na=sk_X509_NAME_ENTRY_value(a->entries,i); sl@0: nb=sk_X509_NAME_ENTRY_value(b->entries,i); sl@0: j=na->value->type-nb->value->type; sl@0: if (j) sl@0: { sl@0: nabit = ASN1_tag2bit(na->value->type); sl@0: nbbit = ASN1_tag2bit(nb->value->type); sl@0: if (!(nabit & STR_TYPE_CMP) || sl@0: !(nbbit & STR_TYPE_CMP)) sl@0: return j; sl@0: j = asn1_string_memcmp(na->value, nb->value); sl@0: } sl@0: else if (na->value->type == V_ASN1_PRINTABLESTRING) sl@0: j=nocase_spacenorm_cmp(na->value, nb->value); sl@0: else if (na->value->type == V_ASN1_IA5STRING sl@0: && OBJ_obj2nid(na->object) == NID_pkcs9_emailAddress) sl@0: j=nocase_cmp(na->value, nb->value); sl@0: else sl@0: j = asn1_string_memcmp(na->value, nb->value); sl@0: if (j) return(j); sl@0: j=na->set-nb->set; sl@0: if (j) return(j); sl@0: } sl@0: sl@0: /* We will check the object types after checking the values sl@0: * since the values will more often be different than the object sl@0: * types. */ sl@0: for (i=sk_X509_NAME_ENTRY_num(a->entries)-1; i>=0; i--) sl@0: { sl@0: na=sk_X509_NAME_ENTRY_value(a->entries,i); sl@0: nb=sk_X509_NAME_ENTRY_value(b->entries,i); sl@0: j=OBJ_cmp(na->object,nb->object); sl@0: if (j) return(j); sl@0: } sl@0: return(0); sl@0: } sl@0: sl@0: #ifndef OPENSSL_NO_MD5 sl@0: /* I now DER encode the name and hash it. Since I cache the DER encoding, sl@0: * this is reasonably efficient. */ sl@0: EXPORT_C unsigned long X509_NAME_hash(X509_NAME *x) sl@0: { sl@0: unsigned long ret=0; sl@0: unsigned char md[16]; sl@0: sl@0: /* Make sure X509_NAME structure contains valid cached encoding */ sl@0: i2d_X509_NAME(x,NULL); sl@0: EVP_Digest(x->bytes->data, x->bytes->length, md, NULL, EVP_md5(), NULL); sl@0: sl@0: ret=( ((unsigned long)md[0] )|((unsigned long)md[1]<<8L)| sl@0: ((unsigned long)md[2]<<16L)|((unsigned long)md[3]<<24L) sl@0: )&0xffffffffL; sl@0: return(ret); sl@0: } sl@0: #endif sl@0: sl@0: /* Search a stack of X509 for a match */ sl@0: EXPORT_C X509 *X509_find_by_issuer_and_serial(STACK_OF(X509) *sk, X509_NAME *name, sl@0: ASN1_INTEGER *serial) sl@0: { sl@0: int i; sl@0: X509_CINF cinf; sl@0: X509 x,*x509=NULL; sl@0: sl@0: if(!sk) return NULL; sl@0: sl@0: x.cert_info= &cinf; sl@0: cinf.serialNumber=serial; sl@0: cinf.issuer=name; sl@0: sl@0: for (i=0; icert_info == NULL)) sl@0: return(NULL); sl@0: return(X509_PUBKEY_get(x->cert_info->key)); sl@0: } sl@0: sl@0: EXPORT_C ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x) sl@0: { sl@0: if(!x) return NULL; sl@0: return x->cert_info->key->public_key; sl@0: } sl@0: sl@0: EXPORT_C int X509_check_private_key(X509 *x, EVP_PKEY *k) sl@0: { sl@0: EVP_PKEY *xk=NULL; sl@0: int ok=0; sl@0: sl@0: xk=X509_get_pubkey(x); sl@0: switch (EVP_PKEY_cmp(xk, k)) sl@0: { sl@0: case 1: sl@0: ok=1; sl@0: break; sl@0: case 0: sl@0: X509err(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_KEY_VALUES_MISMATCH); sl@0: break; sl@0: case -1: sl@0: X509err(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_KEY_TYPE_MISMATCH); sl@0: break; sl@0: case -2: sl@0: #ifndef OPENSSL_NO_EC sl@0: if (k->type == EVP_PKEY_EC) sl@0: { sl@0: X509err(X509_F_X509_CHECK_PRIVATE_KEY, ERR_R_EC_LIB); sl@0: break; sl@0: } sl@0: #endif sl@0: #ifndef OPENSSL_NO_DH sl@0: if (k->type == EVP_PKEY_DH) sl@0: { sl@0: /* No idea */ sl@0: X509err(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_CANT_CHECK_DH_KEY); sl@0: break; sl@0: } sl@0: #endif sl@0: X509err(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_UNKNOWN_KEY_TYPE); sl@0: } sl@0: sl@0: EVP_PKEY_free(xk); sl@0: return(ok); sl@0: }