diff options
| author | markus <> | 2002-09-05 12:51:50 +0000 |
|---|---|---|
| committer | markus <> | 2002-09-05 12:51:50 +0000 |
| commit | 15b5d84f9da2ce4bfae8580e56e34a859f74ad71 (patch) | |
| tree | bf939e82d7fd73cc8a01cf6959002209972091bc /src/lib/libcrypto/x509/x509_cmp.c | |
| parent | 027351f729b9e837200dae6e1520cda6577ab930 (diff) | |
| download | openbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.tar.gz openbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.tar.bz2 openbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.zip | |
import openssl-0.9.7-beta1
Diffstat (limited to 'src/lib/libcrypto/x509/x509_cmp.c')
| -rw-r--r-- | src/lib/libcrypto/x509/x509_cmp.c | 204 |
1 files changed, 130 insertions, 74 deletions
diff --git a/src/lib/libcrypto/x509/x509_cmp.c b/src/lib/libcrypto/x509/x509_cmp.c index f9d9510ac5..cd20b6d66f 100644 --- a/src/lib/libcrypto/x509/x509_cmp.c +++ b/src/lib/libcrypto/x509/x509_cmp.c | |||
| @@ -57,114 +57,121 @@ | |||
| 57 | */ | 57 | */ |
| 58 | 58 | ||
| 59 | #include <stdio.h> | 59 | #include <stdio.h> |
| 60 | #include <sys/types.h> | ||
| 61 | #include <sys/stat.h> | ||
| 62 | #include "cryptlib.h" | 60 | #include "cryptlib.h" |
| 63 | #include "asn1.h" | 61 | #include <openssl/asn1.h> |
| 64 | #include "objects.h" | 62 | #include <openssl/objects.h> |
| 65 | #include "x509.h" | 63 | #include <openssl/x509.h> |
| 64 | #include <openssl/x509v3.h> | ||
| 66 | 65 | ||
| 67 | int X509_issuer_and_serial_cmp(a,b) | 66 | int X509_issuer_and_serial_cmp(const X509 *a, const X509 *b) |
| 68 | X509 *a; | ||
| 69 | X509 *b; | ||
| 70 | { | 67 | { |
| 71 | int i; | 68 | int i; |
| 72 | X509_CINF *ai,*bi; | 69 | X509_CINF *ai,*bi; |
| 73 | 70 | ||
| 74 | ai=a->cert_info; | 71 | ai=a->cert_info; |
| 75 | bi=b->cert_info; | 72 | bi=b->cert_info; |
| 76 | i=ASN1_INTEGER_cmp(ai->serialNumber,bi->serialNumber); | 73 | i=M_ASN1_INTEGER_cmp(ai->serialNumber,bi->serialNumber); |
| 77 | if (i) return(i); | 74 | if (i) return(i); |
| 78 | return(X509_NAME_cmp(ai->issuer,bi->issuer)); | 75 | return(X509_NAME_cmp(ai->issuer,bi->issuer)); |
| 79 | } | 76 | } |
| 80 | 77 | ||
| 81 | #ifndef NO_MD5 | 78 | #ifndef OPENSSL_NO_MD5 |
| 82 | unsigned long X509_issuer_and_serial_hash(a) | 79 | unsigned long X509_issuer_and_serial_hash(X509 *a) |
| 83 | X509 *a; | ||
| 84 | { | 80 | { |
| 85 | unsigned long ret=0; | 81 | unsigned long ret=0; |
| 86 | MD5_CTX ctx; | 82 | EVP_MD_CTX ctx; |
| 87 | unsigned char md[16]; | 83 | unsigned char md[16]; |
| 88 | char str[256]; | 84 | char str[256]; |
| 89 | 85 | ||
| 86 | EVP_MD_CTX_init(&ctx); | ||
| 90 | X509_NAME_oneline(a->cert_info->issuer,str,256); | 87 | X509_NAME_oneline(a->cert_info->issuer,str,256); |
| 91 | ret=strlen(str); | 88 | ret=strlen(str); |
| 92 | MD5_Init(&ctx); | 89 | EVP_DigestInit_ex(&ctx, EVP_md5(), NULL); |
| 93 | MD5_Update(&ctx,(unsigned char *)str,ret); | 90 | EVP_DigestUpdate(&ctx,(unsigned char *)str,ret); |
| 94 | MD5_Update(&ctx,(unsigned char *)a->cert_info->serialNumber->data, | 91 | EVP_DigestUpdate(&ctx,(unsigned char *)a->cert_info->serialNumber->data, |
| 95 | (unsigned long)a->cert_info->serialNumber->length); | 92 | (unsigned long)a->cert_info->serialNumber->length); |
| 96 | MD5_Final(&(md[0]),&ctx); | 93 | EVP_DigestFinal_ex(&ctx,&(md[0]),NULL); |
| 97 | ret=( ((unsigned long)md[0] )|((unsigned long)md[1]<<8L)| | 94 | ret=( ((unsigned long)md[0] )|((unsigned long)md[1]<<8L)| |
| 98 | ((unsigned long)md[2]<<16L)|((unsigned long)md[3]<<24L) | 95 | ((unsigned long)md[2]<<16L)|((unsigned long)md[3]<<24L) |
| 99 | )&0xffffffffL; | 96 | )&0xffffffffL; |
| 97 | EVP_MD_CTX_cleanup(&ctx); | ||
| 100 | return(ret); | 98 | return(ret); |
| 101 | } | 99 | } |
| 102 | #endif | 100 | #endif |
| 103 | 101 | ||
| 104 | int X509_issuer_name_cmp(a, b) | 102 | int X509_issuer_name_cmp(const X509 *a, const X509 *b) |
| 105 | X509 *a; | ||
| 106 | X509 *b; | ||
| 107 | { | 103 | { |
| 108 | return(X509_NAME_cmp(a->cert_info->issuer,b->cert_info->issuer)); | 104 | return(X509_NAME_cmp(a->cert_info->issuer,b->cert_info->issuer)); |
| 109 | } | 105 | } |
| 110 | 106 | ||
| 111 | int X509_subject_name_cmp(a, b) | 107 | int X509_subject_name_cmp(const X509 *a, const X509 *b) |
| 112 | X509 *a; | ||
| 113 | X509 *b; | ||
| 114 | { | 108 | { |
| 115 | return(X509_NAME_cmp(a->cert_info->subject,b->cert_info->subject)); | 109 | return(X509_NAME_cmp(a->cert_info->subject,b->cert_info->subject)); |
| 116 | } | 110 | } |
| 117 | 111 | ||
| 118 | int X509_CRL_cmp(a, b) | 112 | int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b) |
| 119 | X509_CRL *a; | ||
| 120 | X509_CRL *b; | ||
| 121 | { | 113 | { |
| 122 | return(X509_NAME_cmp(a->crl->issuer,b->crl->issuer)); | 114 | return(X509_NAME_cmp(a->crl->issuer,b->crl->issuer)); |
| 123 | } | 115 | } |
| 124 | 116 | ||
| 125 | X509_NAME *X509_get_issuer_name(a) | 117 | X509_NAME *X509_get_issuer_name(X509 *a) |
| 126 | X509 *a; | ||
| 127 | { | 118 | { |
| 128 | return(a->cert_info->issuer); | 119 | return(a->cert_info->issuer); |
| 129 | } | 120 | } |
| 130 | 121 | ||
| 131 | unsigned long X509_issuer_name_hash(x) | 122 | unsigned long X509_issuer_name_hash(X509 *x) |
| 132 | X509 *x; | ||
| 133 | { | 123 | { |
| 134 | return(X509_NAME_hash(x->cert_info->issuer)); | 124 | return(X509_NAME_hash(x->cert_info->issuer)); |
| 135 | } | 125 | } |
| 136 | 126 | ||
| 137 | X509_NAME *X509_get_subject_name(a) | 127 | X509_NAME *X509_get_subject_name(X509 *a) |
| 138 | X509 *a; | ||
| 139 | { | 128 | { |
| 140 | return(a->cert_info->subject); | 129 | return(a->cert_info->subject); |
| 141 | } | 130 | } |
| 142 | 131 | ||
| 143 | ASN1_INTEGER *X509_get_serialNumber(a) | 132 | ASN1_INTEGER *X509_get_serialNumber(X509 *a) |
| 144 | X509 *a; | ||
| 145 | { | 133 | { |
| 146 | return(a->cert_info->serialNumber); | 134 | return(a->cert_info->serialNumber); |
| 147 | } | 135 | } |
| 148 | 136 | ||
| 149 | unsigned long X509_subject_name_hash(x) | 137 | unsigned long X509_subject_name_hash(X509 *x) |
| 150 | X509 *x; | ||
| 151 | { | 138 | { |
| 152 | return(X509_NAME_hash(x->cert_info->subject)); | 139 | return(X509_NAME_hash(x->cert_info->subject)); |
| 153 | } | 140 | } |
| 154 | 141 | ||
| 155 | int X509_NAME_cmp(a, b) | 142 | #ifndef OPENSSL_NO_SHA |
| 156 | X509_NAME *a; | 143 | /* Compare two certificates: they must be identical for |
| 157 | X509_NAME *b; | 144 | * this to work. NB: Although "cmp" operations are generally |
| 145 | * prototyped to take "const" arguments (eg. for use in | ||
| 146 | * STACKs), the way X509 handling is - these operations may | ||
| 147 | * involve ensuring the hashes are up-to-date and ensuring | ||
| 148 | * certain cert information is cached. So this is the point | ||
| 149 | * where the "depth-first" constification tree has to halt | ||
| 150 | * with an evil cast. | ||
| 151 | */ | ||
| 152 | int X509_cmp(const X509 *a, const X509 *b) | ||
| 153 | { | ||
| 154 | /* ensure hash is valid */ | ||
| 155 | X509_check_purpose((X509 *)a, -1, 0); | ||
| 156 | X509_check_purpose((X509 *)b, -1, 0); | ||
| 157 | |||
| 158 | return memcmp(a->sha1_hash, b->sha1_hash, SHA_DIGEST_LENGTH); | ||
| 159 | } | ||
| 160 | #endif | ||
| 161 | |||
| 162 | int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b) | ||
| 158 | { | 163 | { |
| 159 | int i,j; | 164 | int i,j; |
| 160 | X509_NAME_ENTRY *na,*nb; | 165 | X509_NAME_ENTRY *na,*nb; |
| 161 | 166 | ||
| 162 | if (sk_num(a->entries) != sk_num(b->entries)) | 167 | if (sk_X509_NAME_ENTRY_num(a->entries) |
| 163 | return(sk_num(a->entries)-sk_num(b->entries)); | 168 | != sk_X509_NAME_ENTRY_num(b->entries)) |
| 164 | for (i=sk_num(a->entries)-1; i>=0; i--) | 169 | return sk_X509_NAME_ENTRY_num(a->entries) |
| 170 | -sk_X509_NAME_ENTRY_num(b->entries); | ||
| 171 | for (i=sk_X509_NAME_ENTRY_num(a->entries)-1; i>=0; i--) | ||
| 165 | { | 172 | { |
| 166 | na=(X509_NAME_ENTRY *)sk_value(a->entries,i); | 173 | na=sk_X509_NAME_ENTRY_value(a->entries,i); |
| 167 | nb=(X509_NAME_ENTRY *)sk_value(b->entries,i); | 174 | nb=sk_X509_NAME_ENTRY_value(b->entries,i); |
| 168 | j=na->value->length-nb->value->length; | 175 | j=na->value->length-nb->value->length; |
| 169 | if (j) return(j); | 176 | if (j) return(j); |
| 170 | j=memcmp(na->value->data,nb->value->data, | 177 | j=memcmp(na->value->data,nb->value->data, |
| @@ -177,37 +184,27 @@ X509_NAME *b; | |||
| 177 | /* We will check the object types after checking the values | 184 | /* We will check the object types after checking the values |
| 178 | * since the values will more often be different than the object | 185 | * since the values will more often be different than the object |
| 179 | * types. */ | 186 | * types. */ |
| 180 | for (i=sk_num(a->entries)-1; i>=0; i--) | 187 | for (i=sk_X509_NAME_ENTRY_num(a->entries)-1; i>=0; i--) |
| 181 | { | 188 | { |
| 182 | na=(X509_NAME_ENTRY *)sk_value(a->entries,i); | 189 | na=sk_X509_NAME_ENTRY_value(a->entries,i); |
| 183 | nb=(X509_NAME_ENTRY *)sk_value(b->entries,i); | 190 | nb=sk_X509_NAME_ENTRY_value(b->entries,i); |
| 184 | j=OBJ_cmp(na->object,nb->object); | 191 | j=OBJ_cmp(na->object,nb->object); |
| 185 | if (j) return(j); | 192 | if (j) return(j); |
| 186 | } | 193 | } |
| 187 | return(0); | 194 | return(0); |
| 188 | } | 195 | } |
| 189 | 196 | ||
| 190 | #ifndef NO_MD5 | 197 | #ifndef OPENSSL_NO_MD5 |
| 191 | /* I now DER encode the name and hash it. Since I cache the DER encoding, | 198 | /* I now DER encode the name and hash it. Since I cache the DER encoding, |
| 192 | * this is reasonably effiecent. */ | 199 | * this is reasonably efficient. */ |
| 193 | unsigned long X509_NAME_hash(x) | 200 | unsigned long X509_NAME_hash(X509_NAME *x) |
| 194 | X509_NAME *x; | ||
| 195 | { | 201 | { |
| 196 | unsigned long ret=0; | 202 | unsigned long ret=0; |
| 197 | unsigned char md[16]; | 203 | unsigned char md[16]; |
| 198 | unsigned char str[256],*p,*pp; | ||
| 199 | int i; | ||
| 200 | |||
| 201 | i=i2d_X509_NAME(x,NULL); | ||
| 202 | if (i > sizeof(str)) | ||
| 203 | p=Malloc(i); | ||
| 204 | else | ||
| 205 | p=str; | ||
| 206 | 204 | ||
| 207 | pp=p; | 205 | /* Make sure X509_NAME structure contains valid cached encoding */ |
| 208 | i2d_X509_NAME(x,&pp); | 206 | i2d_X509_NAME(x,NULL); |
| 209 | MD5((unsigned char *)p,i,&(md[0])); | 207 | EVP_Digest(x->bytes->data, x->bytes->length, md, NULL, EVP_md5(), NULL); |
| 210 | if (p != str) Free(p); | ||
| 211 | 208 | ||
| 212 | ret=( ((unsigned long)md[0] )|((unsigned long)md[1]<<8L)| | 209 | ret=( ((unsigned long)md[0] )|((unsigned long)md[1]<<8L)| |
| 213 | ((unsigned long)md[2]<<16L)|((unsigned long)md[3]<<24L) | 210 | ((unsigned long)md[2]<<16L)|((unsigned long)md[3]<<24L) |
| @@ -217,41 +214,100 @@ X509_NAME *x; | |||
| 217 | #endif | 214 | #endif |
| 218 | 215 | ||
| 219 | /* Search a stack of X509 for a match */ | 216 | /* Search a stack of X509 for a match */ |
| 220 | X509 *X509_find_by_issuer_and_serial(sk,name,serial) | 217 | X509 *X509_find_by_issuer_and_serial(STACK_OF(X509) *sk, X509_NAME *name, |
| 221 | STACK *sk; | 218 | ASN1_INTEGER *serial) |
| 222 | X509_NAME *name; | ||
| 223 | ASN1_INTEGER *serial; | ||
| 224 | { | 219 | { |
| 225 | int i; | 220 | int i; |
| 226 | X509_CINF cinf; | 221 | X509_CINF cinf; |
| 227 | X509 x,*x509=NULL; | 222 | X509 x,*x509=NULL; |
| 228 | 223 | ||
| 224 | if(!sk) return NULL; | ||
| 225 | |||
| 229 | x.cert_info= &cinf; | 226 | x.cert_info= &cinf; |
| 230 | cinf.serialNumber=serial; | 227 | cinf.serialNumber=serial; |
| 231 | cinf.issuer=name; | 228 | cinf.issuer=name; |
| 232 | 229 | ||
| 233 | for (i=0; i<sk_num(sk); i++) | 230 | for (i=0; i<sk_X509_num(sk); i++) |
| 234 | { | 231 | { |
| 235 | x509=(X509 *)sk_value(sk,i); | 232 | x509=sk_X509_value(sk,i); |
| 236 | if (X509_issuer_and_serial_cmp(x509,&x) == 0) | 233 | if (X509_issuer_and_serial_cmp(x509,&x) == 0) |
| 237 | return(x509); | 234 | return(x509); |
| 238 | } | 235 | } |
| 239 | return(NULL); | 236 | return(NULL); |
| 240 | } | 237 | } |
| 241 | 238 | ||
| 242 | X509 *X509_find_by_subject(sk,name) | 239 | X509 *X509_find_by_subject(STACK_OF(X509) *sk, X509_NAME *name) |
| 243 | STACK *sk; | ||
| 244 | X509_NAME *name; | ||
| 245 | { | 240 | { |
| 246 | X509 *x509; | 241 | X509 *x509; |
| 247 | int i; | 242 | int i; |
| 248 | 243 | ||
| 249 | for (i=0; i<sk_num(sk); i++) | 244 | for (i=0; i<sk_X509_num(sk); i++) |
| 250 | { | 245 | { |
| 251 | x509=(X509 *)sk_value(sk,i); | 246 | x509=sk_X509_value(sk,i); |
| 252 | if (X509_NAME_cmp(X509_get_subject_name(x509),name) == 0) | 247 | if (X509_NAME_cmp(X509_get_subject_name(x509),name) == 0) |
| 253 | return(x509); | 248 | return(x509); |
| 254 | } | 249 | } |
| 255 | return(NULL); | 250 | return(NULL); |
| 256 | } | 251 | } |
| 257 | 252 | ||
| 253 | EVP_PKEY *X509_get_pubkey(X509 *x) | ||
| 254 | { | ||
| 255 | if ((x == NULL) || (x->cert_info == NULL)) | ||
| 256 | return(NULL); | ||
| 257 | return(X509_PUBKEY_get(x->cert_info->key)); | ||
| 258 | } | ||
| 259 | |||
| 260 | ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x) | ||
| 261 | { | ||
| 262 | if(!x) return NULL; | ||
| 263 | return x->cert_info->key->public_key; | ||
| 264 | } | ||
| 265 | |||
| 266 | int X509_check_private_key(X509 *x, EVP_PKEY *k) | ||
| 267 | { | ||
| 268 | EVP_PKEY *xk=NULL; | ||
| 269 | int ok=0; | ||
| 270 | |||
| 271 | xk=X509_get_pubkey(x); | ||
| 272 | if (xk->type != k->type) | ||
| 273 | { | ||
| 274 | X509err(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_KEY_TYPE_MISMATCH); | ||
| 275 | goto err; | ||
| 276 | } | ||
| 277 | switch (k->type) | ||
| 278 | { | ||
| 279 | #ifndef OPENSSL_NO_RSA | ||
| 280 | case EVP_PKEY_RSA: | ||
| 281 | if (BN_cmp(xk->pkey.rsa->n,k->pkey.rsa->n) != 0 | ||
| 282 | || BN_cmp(xk->pkey.rsa->e,k->pkey.rsa->e) != 0) | ||
| 283 | { | ||
| 284 | X509err(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_KEY_VALUES_MISMATCH); | ||
| 285 | goto err; | ||
| 286 | } | ||
| 287 | break; | ||
| 288 | #endif | ||
| 289 | #ifndef OPENSSL_NO_DSA | ||
| 290 | case EVP_PKEY_DSA: | ||
| 291 | if (BN_cmp(xk->pkey.dsa->pub_key,k->pkey.dsa->pub_key) != 0) | ||
| 292 | { | ||
| 293 | X509err(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_KEY_VALUES_MISMATCH); | ||
| 294 | goto err; | ||
| 295 | } | ||
| 296 | break; | ||
| 297 | #endif | ||
| 298 | #ifndef OPENSSL_NO_DH | ||
| 299 | case EVP_PKEY_DH: | ||
| 300 | /* No idea */ | ||
| 301 | X509err(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_CANT_CHECK_DH_KEY); | ||
| 302 | goto err; | ||
| 303 | #endif | ||
| 304 | default: | ||
| 305 | X509err(X509_F_X509_CHECK_PRIVATE_KEY,X509_R_UNKNOWN_KEY_TYPE); | ||
| 306 | goto err; | ||
| 307 | } | ||
| 308 | |||
| 309 | ok=1; | ||
| 310 | err: | ||
| 311 | EVP_PKEY_free(xk); | ||
| 312 | return(ok); | ||
| 313 | } | ||
