diff options
| author | cvs2svn <admin@example.com> | 2000-04-30 04:58:23 +0000 |
|---|---|---|
| committer | cvs2svn <admin@example.com> | 2000-04-30 04:58:23 +0000 |
| commit | 9cc1eacd8366faaa248a9aca9120fb7427c2a58e (patch) | |
| tree | 9304b7a65ac1d9eeb42cdd518d69fd21eb2b47af /src/lib/libcrypto/x509/x509_vfy.c | |
| parent | e71289f71fad3f153975fd8fbf1f0f35dbda7f28 (diff) | |
| download | openbsd-OPENBSD_2_7_BASE.tar.gz openbsd-OPENBSD_2_7_BASE.tar.bz2 openbsd-OPENBSD_2_7_BASE.zip | |
This commit was manufactured by cvs2git to create tag 'OPENBSD_2_7_BASE'.OPENBSD_2_7_BASE
Diffstat (limited to 'src/lib/libcrypto/x509/x509_vfy.c')
| -rw-r--r-- | src/lib/libcrypto/x509/x509_vfy.c | 815 |
1 files changed, 0 insertions, 815 deletions
diff --git a/src/lib/libcrypto/x509/x509_vfy.c b/src/lib/libcrypto/x509/x509_vfy.c deleted file mode 100644 index 3ddb2303d3..0000000000 --- a/src/lib/libcrypto/x509/x509_vfy.c +++ /dev/null | |||
| @@ -1,815 +0,0 @@ | |||
| 1 | /* crypto/x509/x509_vfy.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 <time.h> | ||
| 61 | #include <errno.h> | ||
| 62 | |||
| 63 | #include "cryptlib.h" | ||
| 64 | #include <openssl/crypto.h> | ||
| 65 | #include <openssl/lhash.h> | ||
| 66 | #include <openssl/buffer.h> | ||
| 67 | #include <openssl/evp.h> | ||
| 68 | #include <openssl/asn1.h> | ||
| 69 | #include <openssl/x509.h> | ||
| 70 | #include <openssl/x509v3.h> | ||
| 71 | #include <openssl/objects.h> | ||
| 72 | |||
| 73 | static int null_callback(int ok,X509_STORE_CTX *e); | ||
| 74 | static int check_chain_purpose(X509_STORE_CTX *ctx); | ||
| 75 | static int check_trust(X509_STORE_CTX *ctx); | ||
| 76 | static int internal_verify(X509_STORE_CTX *ctx); | ||
| 77 | const char *X509_version="X.509" OPENSSL_VERSION_PTEXT; | ||
| 78 | |||
| 79 | static STACK_OF(CRYPTO_EX_DATA_FUNCS) *x509_store_ctx_method=NULL; | ||
| 80 | static int x509_store_ctx_num=0; | ||
| 81 | #if 0 | ||
| 82 | static int x509_store_num=1; | ||
| 83 | static STACK *x509_store_method=NULL; | ||
| 84 | #endif | ||
| 85 | |||
| 86 | static int null_callback(int ok, X509_STORE_CTX *e) | ||
| 87 | { | ||
| 88 | return(ok); | ||
| 89 | } | ||
| 90 | |||
| 91 | #if 0 | ||
| 92 | static int x509_subject_cmp(X509 **a, X509 **b) | ||
| 93 | { | ||
| 94 | return(X509_subject_name_cmp(*a,*b)); | ||
| 95 | } | ||
| 96 | #endif | ||
| 97 | |||
| 98 | int X509_verify_cert(X509_STORE_CTX *ctx) | ||
| 99 | { | ||
| 100 | X509 *x,*xtmp,*chain_ss=NULL; | ||
| 101 | X509_NAME *xn; | ||
| 102 | X509_OBJECT obj; | ||
| 103 | int depth,i,ok=0; | ||
| 104 | int num; | ||
| 105 | int (*cb)(); | ||
| 106 | STACK_OF(X509) *sktmp=NULL; | ||
| 107 | |||
| 108 | if (ctx->cert == NULL) | ||
| 109 | { | ||
| 110 | X509err(X509_F_X509_VERIFY_CERT,X509_R_NO_CERT_SET_FOR_US_TO_VERIFY); | ||
| 111 | return(-1); | ||
| 112 | } | ||
| 113 | |||
| 114 | cb=ctx->ctx->verify_cb; | ||
| 115 | if (cb == NULL) cb=null_callback; | ||
| 116 | |||
| 117 | /* first we make sure the chain we are going to build is | ||
| 118 | * present and that the first entry is in place */ | ||
| 119 | if (ctx->chain == NULL) | ||
| 120 | { | ||
| 121 | if ( ((ctx->chain=sk_X509_new_null()) == NULL) || | ||
| 122 | (!sk_X509_push(ctx->chain,ctx->cert))) | ||
| 123 | { | ||
| 124 | X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE); | ||
| 125 | goto end; | ||
| 126 | } | ||
| 127 | CRYPTO_add(&ctx->cert->references,1,CRYPTO_LOCK_X509); | ||
| 128 | ctx->last_untrusted=1; | ||
| 129 | } | ||
| 130 | |||
| 131 | /* We use a temporary STACK so we can chop and hack at it */ | ||
| 132 | if (ctx->untrusted != NULL | ||
| 133 | && (sktmp=sk_X509_dup(ctx->untrusted)) == NULL) | ||
| 134 | { | ||
| 135 | X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE); | ||
| 136 | goto end; | ||
| 137 | } | ||
| 138 | |||
| 139 | num=sk_X509_num(ctx->chain); | ||
| 140 | x=sk_X509_value(ctx->chain,num-1); | ||
| 141 | depth=ctx->depth; | ||
| 142 | |||
| 143 | |||
| 144 | for (;;) | ||
| 145 | { | ||
| 146 | /* If we have enough, we break */ | ||
| 147 | if (depth < num) break; /* FIXME: If this happens, we should take | ||
| 148 | * note of it and, if appropriate, use the | ||
| 149 | * X509_V_ERR_CERT_CHAIN_TOO_LONG error | ||
| 150 | * code later. | ||
| 151 | */ | ||
| 152 | |||
| 153 | /* If we are self signed, we break */ | ||
| 154 | xn=X509_get_issuer_name(x); | ||
| 155 | if (X509_NAME_cmp(X509_get_subject_name(x),xn) == 0) | ||
| 156 | break; | ||
| 157 | |||
| 158 | /* If we were passed a cert chain, use it first */ | ||
| 159 | if (ctx->untrusted != NULL) | ||
| 160 | { | ||
| 161 | xtmp=X509_find_by_subject(sktmp,xn); | ||
| 162 | if (xtmp != NULL) | ||
| 163 | { | ||
| 164 | if (!sk_X509_push(ctx->chain,xtmp)) | ||
| 165 | { | ||
| 166 | X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE); | ||
| 167 | goto end; | ||
| 168 | } | ||
| 169 | CRYPTO_add(&xtmp->references,1,CRYPTO_LOCK_X509); | ||
| 170 | sk_X509_delete_ptr(sktmp,xtmp); | ||
| 171 | ctx->last_untrusted++; | ||
| 172 | x=xtmp; | ||
| 173 | num++; | ||
| 174 | /* reparse the full chain for | ||
| 175 | * the next one */ | ||
| 176 | continue; | ||
| 177 | } | ||
| 178 | } | ||
| 179 | break; | ||
| 180 | } | ||
| 181 | |||
| 182 | /* at this point, chain should contain a list of untrusted | ||
| 183 | * certificates. We now need to add at least one trusted one, | ||
| 184 | * if possible, otherwise we complain. */ | ||
| 185 | |||
| 186 | i=sk_X509_num(ctx->chain); | ||
| 187 | x=sk_X509_value(ctx->chain,i-1); | ||
| 188 | xn = X509_get_subject_name(x); | ||
| 189 | if (X509_NAME_cmp(xn,X509_get_issuer_name(x)) | ||
| 190 | == 0) | ||
| 191 | { | ||
| 192 | /* we have a self signed certificate */ | ||
| 193 | if (sk_X509_num(ctx->chain) == 1) | ||
| 194 | { | ||
| 195 | /* We have a single self signed certificate: see if | ||
| 196 | * we can find it in the store. We must have an exact | ||
| 197 | * match to avoid possible impersonation. | ||
| 198 | */ | ||
| 199 | ok=X509_STORE_get_by_subject(ctx,X509_LU_X509,xn,&obj); | ||
| 200 | if ((ok != X509_LU_X509) || X509_cmp(x, obj.data.x509)) | ||
| 201 | { | ||
| 202 | ctx->error=X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT; | ||
| 203 | ctx->current_cert=x; | ||
| 204 | ctx->error_depth=i-1; | ||
| 205 | if(ok == X509_LU_X509) X509_OBJECT_free_contents(&obj); | ||
| 206 | ok=cb(0,ctx); | ||
| 207 | if (!ok) goto end; | ||
| 208 | } | ||
| 209 | else | ||
| 210 | { | ||
| 211 | /* We have a match: replace certificate with store version | ||
| 212 | * so we get any trust settings. | ||
| 213 | */ | ||
| 214 | X509_free(x); | ||
| 215 | x = obj.data.x509; | ||
| 216 | sk_X509_set(ctx->chain, i - 1, x); | ||
| 217 | ctx->last_untrusted=0; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | else | ||
| 221 | { | ||
| 222 | /* worry more about this one elsewhere */ | ||
| 223 | chain_ss=sk_X509_pop(ctx->chain); | ||
| 224 | ctx->last_untrusted--; | ||
| 225 | num--; | ||
| 226 | x=sk_X509_value(ctx->chain,num-1); | ||
| 227 | } | ||
| 228 | } | ||
| 229 | |||
| 230 | /* We now lookup certs from the certificate store */ | ||
| 231 | for (;;) | ||
| 232 | { | ||
| 233 | /* If we have enough, we break */ | ||
| 234 | if (depth < num) break; | ||
| 235 | |||
| 236 | /* If we are self signed, we break */ | ||
| 237 | xn=X509_get_issuer_name(x); | ||
| 238 | if (X509_NAME_cmp(X509_get_subject_name(x),xn) == 0) | ||
| 239 | break; | ||
| 240 | |||
| 241 | ok=X509_STORE_get_by_subject(ctx,X509_LU_X509,xn,&obj); | ||
| 242 | if (ok != X509_LU_X509) | ||
| 243 | { | ||
| 244 | if (ok == X509_LU_RETRY) | ||
| 245 | { | ||
| 246 | X509_OBJECT_free_contents(&obj); | ||
| 247 | X509err(X509_F_X509_VERIFY_CERT,X509_R_SHOULD_RETRY); | ||
| 248 | return(ok); | ||
| 249 | } | ||
| 250 | else if (ok != X509_LU_FAIL) | ||
| 251 | { | ||
| 252 | X509_OBJECT_free_contents(&obj); | ||
| 253 | /* not good :-(, break anyway */ | ||
| 254 | return(ok); | ||
| 255 | } | ||
| 256 | break; | ||
| 257 | } | ||
| 258 | x=obj.data.x509; | ||
| 259 | if (!sk_X509_push(ctx->chain,obj.data.x509)) | ||
| 260 | { | ||
| 261 | X509_OBJECT_free_contents(&obj); | ||
| 262 | X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE); | ||
| 263 | return(0); | ||
| 264 | } | ||
| 265 | num++; | ||
| 266 | } | ||
| 267 | |||
| 268 | /* we now have our chain, lets check it... */ | ||
| 269 | xn=X509_get_issuer_name(x); | ||
| 270 | if (X509_NAME_cmp(X509_get_subject_name(x),xn) != 0) | ||
| 271 | { | ||
| 272 | if ((chain_ss == NULL) || (X509_NAME_cmp(X509_get_subject_name(chain_ss),xn) != 0)) | ||
| 273 | { | ||
| 274 | if (ctx->last_untrusted >= num) | ||
| 275 | ctx->error=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY; | ||
| 276 | else | ||
| 277 | ctx->error=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT; | ||
| 278 | ctx->current_cert=x; | ||
| 279 | } | ||
| 280 | else | ||
| 281 | { | ||
| 282 | |||
| 283 | sk_X509_push(ctx->chain,chain_ss); | ||
| 284 | num++; | ||
| 285 | ctx->last_untrusted=num; | ||
| 286 | ctx->current_cert=chain_ss; | ||
| 287 | ctx->error=X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN; | ||
| 288 | chain_ss=NULL; | ||
| 289 | } | ||
| 290 | |||
| 291 | ctx->error_depth=num-1; | ||
| 292 | ok=cb(0,ctx); | ||
| 293 | if (!ok) goto end; | ||
| 294 | } | ||
| 295 | |||
| 296 | /* We have the chain complete: now we need to check its purpose */ | ||
| 297 | if(ctx->purpose > 0) ok = check_chain_purpose(ctx); | ||
| 298 | |||
| 299 | if(!ok) goto end; | ||
| 300 | |||
| 301 | /* The chain extensions are OK: check trust */ | ||
| 302 | |||
| 303 | if(ctx->trust > 0) ok = check_trust(ctx); | ||
| 304 | |||
| 305 | if(!ok) goto end; | ||
| 306 | |||
| 307 | /* We may as well copy down any DSA parameters that are required */ | ||
| 308 | X509_get_pubkey_parameters(NULL,ctx->chain); | ||
| 309 | |||
| 310 | /* At this point, we have a chain and just need to verify it */ | ||
| 311 | if (ctx->ctx->verify != NULL) | ||
| 312 | ok=ctx->ctx->verify(ctx); | ||
| 313 | else | ||
| 314 | ok=internal_verify(ctx); | ||
| 315 | if (0) | ||
| 316 | { | ||
| 317 | end: | ||
| 318 | X509_get_pubkey_parameters(NULL,ctx->chain); | ||
| 319 | } | ||
| 320 | if (sktmp != NULL) sk_X509_free(sktmp); | ||
| 321 | if (chain_ss != NULL) X509_free(chain_ss); | ||
| 322 | return(ok); | ||
| 323 | } | ||
| 324 | |||
| 325 | /* Check a certificate chains extensions for consistency | ||
| 326 | * with the supplied purpose | ||
| 327 | */ | ||
| 328 | |||
| 329 | static int check_chain_purpose(X509_STORE_CTX *ctx) | ||
| 330 | { | ||
| 331 | #ifdef NO_CHAIN_VERIFY | ||
| 332 | return 1; | ||
| 333 | #else | ||
| 334 | int i, ok=0; | ||
| 335 | X509 *x; | ||
| 336 | int (*cb)(); | ||
| 337 | cb=ctx->ctx->verify_cb; | ||
| 338 | if (cb == NULL) cb=null_callback; | ||
| 339 | /* Check all untrusted certificates */ | ||
| 340 | for(i = 0; i < ctx->last_untrusted; i++) { | ||
| 341 | x = sk_X509_value(ctx->chain, i); | ||
| 342 | if(!X509_check_purpose(x, ctx->purpose, i)) { | ||
| 343 | if(i) ctx->error = X509_V_ERR_INVALID_CA; | ||
| 344 | else ctx->error = X509_V_ERR_INVALID_PURPOSE; | ||
| 345 | ctx->error_depth = i; | ||
| 346 | ctx->current_cert = x; | ||
| 347 | ok=cb(0,ctx); | ||
| 348 | if(!ok) goto end; | ||
| 349 | } | ||
| 350 | /* Check pathlen */ | ||
| 351 | if((i > 1) && (x->ex_pathlen != -1) | ||
| 352 | && (i > (x->ex_pathlen + 1))) { | ||
| 353 | ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED; | ||
| 354 | ctx->error_depth = i; | ||
| 355 | ctx->current_cert = x; | ||
| 356 | ok=cb(0,ctx); | ||
| 357 | if(!ok) goto end; | ||
| 358 | } | ||
| 359 | } | ||
| 360 | ok = 1; | ||
| 361 | end: | ||
| 362 | return(ok); | ||
| 363 | #endif | ||
| 364 | } | ||
| 365 | |||
| 366 | static int check_trust(X509_STORE_CTX *ctx) | ||
| 367 | { | ||
| 368 | #ifdef NO_CHAIN_VERIFY | ||
| 369 | return 1; | ||
| 370 | #else | ||
| 371 | int i, ok; | ||
| 372 | X509 *x; | ||
| 373 | int (*cb)(); | ||
| 374 | cb=ctx->ctx->verify_cb; | ||
| 375 | if (cb == NULL) cb=null_callback; | ||
| 376 | /* For now just check the last certificate in the chain */ | ||
| 377 | i = sk_X509_num(ctx->chain) - 1; | ||
| 378 | x = sk_X509_value(ctx->chain, i); | ||
| 379 | ok = X509_check_trust(x, ctx->trust, 0); | ||
| 380 | if(ok == X509_TRUST_TRUSTED) return 1; | ||
| 381 | ctx->error_depth = sk_X509_num(ctx->chain) - 1; | ||
| 382 | ctx->current_cert = x; | ||
| 383 | if(ok == X509_TRUST_REJECTED) ctx->error = X509_V_ERR_CERT_REJECTED; | ||
| 384 | else ctx->error = X509_V_ERR_CERT_UNTRUSTED; | ||
| 385 | ok = cb(0, ctx); | ||
| 386 | return(ok); | ||
| 387 | #endif | ||
| 388 | } | ||
| 389 | |||
| 390 | static int internal_verify(X509_STORE_CTX *ctx) | ||
| 391 | { | ||
| 392 | int i,ok=0,n; | ||
| 393 | X509 *xs,*xi; | ||
| 394 | EVP_PKEY *pkey=NULL; | ||
| 395 | int (*cb)(); | ||
| 396 | |||
| 397 | cb=ctx->ctx->verify_cb; | ||
| 398 | if (cb == NULL) cb=null_callback; | ||
| 399 | |||
| 400 | n=sk_X509_num(ctx->chain); | ||
| 401 | ctx->error_depth=n-1; | ||
| 402 | n--; | ||
| 403 | xi=sk_X509_value(ctx->chain,n); | ||
| 404 | if (X509_NAME_cmp(X509_get_subject_name(xi), | ||
| 405 | X509_get_issuer_name(xi)) == 0) | ||
| 406 | xs=xi; | ||
| 407 | else | ||
| 408 | { | ||
| 409 | if (n <= 0) | ||
| 410 | { | ||
| 411 | ctx->error=X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE; | ||
| 412 | ctx->current_cert=xi; | ||
| 413 | ok=cb(0,ctx); | ||
| 414 | goto end; | ||
| 415 | } | ||
| 416 | else | ||
| 417 | { | ||
| 418 | n--; | ||
| 419 | ctx->error_depth=n; | ||
| 420 | xs=sk_X509_value(ctx->chain,n); | ||
| 421 | } | ||
| 422 | } | ||
| 423 | |||
| 424 | /* ctx->error=0; not needed */ | ||
| 425 | while (n >= 0) | ||
| 426 | { | ||
| 427 | ctx->error_depth=n; | ||
| 428 | if (!xs->valid) | ||
| 429 | { | ||
| 430 | if ((pkey=X509_get_pubkey(xi)) == NULL) | ||
| 431 | { | ||
| 432 | ctx->error=X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY; | ||
| 433 | ctx->current_cert=xi; | ||
| 434 | ok=(*cb)(0,ctx); | ||
| 435 | if (!ok) goto end; | ||
| 436 | } | ||
| 437 | if (X509_verify(xs,pkey) <= 0) | ||
| 438 | { | ||
| 439 | ctx->error=X509_V_ERR_CERT_SIGNATURE_FAILURE; | ||
| 440 | ctx->current_cert=xs; | ||
| 441 | ok=(*cb)(0,ctx); | ||
| 442 | if (!ok) | ||
| 443 | { | ||
| 444 | EVP_PKEY_free(pkey); | ||
| 445 | goto end; | ||
| 446 | } | ||
| 447 | } | ||
| 448 | EVP_PKEY_free(pkey); | ||
| 449 | pkey=NULL; | ||
| 450 | |||
| 451 | i=X509_cmp_current_time(X509_get_notBefore(xs)); | ||
| 452 | if (i == 0) | ||
| 453 | { | ||
| 454 | ctx->error=X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD; | ||
| 455 | ctx->current_cert=xs; | ||
| 456 | ok=(*cb)(0,ctx); | ||
| 457 | if (!ok) goto end; | ||
| 458 | } | ||
| 459 | if (i > 0) | ||
| 460 | { | ||
| 461 | ctx->error=X509_V_ERR_CERT_NOT_YET_VALID; | ||
| 462 | ctx->current_cert=xs; | ||
| 463 | ok=(*cb)(0,ctx); | ||
| 464 | if (!ok) goto end; | ||
| 465 | } | ||
| 466 | xs->valid=1; | ||
| 467 | } | ||
| 468 | |||
| 469 | i=X509_cmp_current_time(X509_get_notAfter(xs)); | ||
| 470 | if (i == 0) | ||
| 471 | { | ||
| 472 | ctx->error=X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD; | ||
| 473 | ctx->current_cert=xs; | ||
| 474 | ok=(*cb)(0,ctx); | ||
| 475 | if (!ok) goto end; | ||
| 476 | } | ||
| 477 | |||
| 478 | if (i < 0) | ||
| 479 | { | ||
| 480 | ctx->error=X509_V_ERR_CERT_HAS_EXPIRED; | ||
| 481 | ctx->current_cert=xs; | ||
| 482 | ok=(*cb)(0,ctx); | ||
| 483 | if (!ok) goto end; | ||
| 484 | } | ||
| 485 | |||
| 486 | /* CRL CHECK */ | ||
| 487 | |||
| 488 | /* The last error (if any) is still in the error value */ | ||
| 489 | ctx->current_cert=xs; | ||
| 490 | ok=(*cb)(1,ctx); | ||
| 491 | if (!ok) goto end; | ||
| 492 | |||
| 493 | n--; | ||
| 494 | if (n >= 0) | ||
| 495 | { | ||
| 496 | xi=xs; | ||
| 497 | xs=sk_X509_value(ctx->chain,n); | ||
| 498 | } | ||
| 499 | } | ||
| 500 | ok=1; | ||
| 501 | end: | ||
| 502 | return(ok); | ||
| 503 | } | ||
| 504 | |||
| 505 | int X509_cmp_current_time(ASN1_UTCTIME *ctm) | ||
| 506 | { | ||
| 507 | char *str; | ||
| 508 | ASN1_UTCTIME atm; | ||
| 509 | time_t offset; | ||
| 510 | char buff1[24],buff2[24],*p; | ||
| 511 | int i,j; | ||
| 512 | |||
| 513 | p=buff1; | ||
| 514 | i=ctm->length; | ||
| 515 | str=(char *)ctm->data; | ||
| 516 | if ((i < 11) || (i > 17)) return(0); | ||
| 517 | memcpy(p,str,10); | ||
| 518 | p+=10; | ||
| 519 | str+=10; | ||
| 520 | |||
| 521 | if ((*str == 'Z') || (*str == '-') || (*str == '+')) | ||
| 522 | { *(p++)='0'; *(p++)='0'; } | ||
| 523 | else { *(p++)= *(str++); *(p++)= *(str++); } | ||
| 524 | *(p++)='Z'; | ||
| 525 | *(p++)='\0'; | ||
| 526 | |||
| 527 | if (*str == 'Z') | ||
| 528 | offset=0; | ||
| 529 | else | ||
| 530 | { | ||
| 531 | if ((*str != '+') && (str[5] != '-')) | ||
| 532 | return(0); | ||
| 533 | offset=((str[1]-'0')*10+(str[2]-'0'))*60; | ||
| 534 | offset+=(str[3]-'0')*10+(str[4]-'0'); | ||
| 535 | if (*str == '-') | ||
| 536 | offset= -offset; | ||
| 537 | } | ||
| 538 | atm.type=V_ASN1_UTCTIME; | ||
| 539 | atm.length=sizeof(buff2); | ||
| 540 | atm.data=(unsigned char *)buff2; | ||
| 541 | |||
| 542 | X509_gmtime_adj(&atm,-offset*60); | ||
| 543 | |||
| 544 | i=(buff1[0]-'0')*10+(buff1[1]-'0'); | ||
| 545 | if (i < 50) i+=100; /* cf. RFC 2459 */ | ||
| 546 | j=(buff2[0]-'0')*10+(buff2[1]-'0'); | ||
| 547 | if (j < 50) j+=100; | ||
| 548 | |||
| 549 | if (i < j) return (-1); | ||
| 550 | if (i > j) return (1); | ||
| 551 | i=strcmp(buff1,buff2); | ||
| 552 | if (i == 0) /* wait a second then return younger :-) */ | ||
| 553 | return(-1); | ||
| 554 | else | ||
| 555 | return(i); | ||
| 556 | } | ||
| 557 | |||
| 558 | ASN1_UTCTIME *X509_gmtime_adj(ASN1_UTCTIME *s, long adj) | ||
| 559 | { | ||
| 560 | time_t t; | ||
| 561 | |||
| 562 | time(&t); | ||
| 563 | t+=adj; | ||
| 564 | return(ASN1_UTCTIME_set(s,t)); | ||
| 565 | } | ||
| 566 | |||
| 567 | int X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK_OF(X509) *chain) | ||
| 568 | { | ||
| 569 | EVP_PKEY *ktmp=NULL,*ktmp2; | ||
| 570 | int i,j; | ||
| 571 | |||
| 572 | if ((pkey != NULL) && !EVP_PKEY_missing_parameters(pkey)) return(1); | ||
| 573 | |||
| 574 | for (i=0; i<sk_X509_num(chain); i++) | ||
| 575 | { | ||
| 576 | ktmp=X509_get_pubkey(sk_X509_value(chain,i)); | ||
| 577 | if (ktmp == NULL) | ||
| 578 | { | ||
| 579 | X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY); | ||
| 580 | return(0); | ||
| 581 | } | ||
| 582 | if (!EVP_PKEY_missing_parameters(ktmp)) | ||
| 583 | break; | ||
| 584 | else | ||
| 585 | { | ||
| 586 | EVP_PKEY_free(ktmp); | ||
| 587 | ktmp=NULL; | ||
| 588 | } | ||
| 589 | } | ||
| 590 | if (ktmp == NULL) | ||
| 591 | { | ||
| 592 | X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN); | ||
| 593 | return(0); | ||
| 594 | } | ||
| 595 | |||
| 596 | /* first, populate the other certs */ | ||
| 597 | for (j=i-1; j >= 0; j--) | ||
| 598 | { | ||
| 599 | ktmp2=X509_get_pubkey(sk_X509_value(chain,j)); | ||
| 600 | EVP_PKEY_copy_parameters(ktmp2,ktmp); | ||
| 601 | EVP_PKEY_free(ktmp2); | ||
| 602 | } | ||
| 603 | |||
| 604 | if (pkey != NULL) EVP_PKEY_copy_parameters(pkey,ktmp); | ||
| 605 | EVP_PKEY_free(ktmp); | ||
| 606 | return(1); | ||
| 607 | } | ||
| 608 | |||
| 609 | int X509_STORE_add_cert(X509_STORE *ctx, X509 *x) | ||
| 610 | { | ||
| 611 | X509_OBJECT *obj,*r; | ||
| 612 | int ret=1; | ||
| 613 | |||
| 614 | if (x == NULL) return(0); | ||
| 615 | obj=(X509_OBJECT *)Malloc(sizeof(X509_OBJECT)); | ||
| 616 | if (obj == NULL) | ||
| 617 | { | ||
| 618 | X509err(X509_F_X509_STORE_ADD_CERT,ERR_R_MALLOC_FAILURE); | ||
| 619 | return(0); | ||
| 620 | } | ||
| 621 | obj->type=X509_LU_X509; | ||
| 622 | obj->data.x509=x; | ||
| 623 | |||
| 624 | CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE); | ||
| 625 | |||
| 626 | X509_OBJECT_up_ref_count(obj); | ||
| 627 | |||
| 628 | r=(X509_OBJECT *)lh_insert(ctx->certs,obj); | ||
| 629 | if (r != NULL) | ||
| 630 | { /* oops, put it back */ | ||
| 631 | lh_delete(ctx->certs,obj); | ||
| 632 | X509_OBJECT_free_contents(obj); | ||
| 633 | Free(obj); | ||
| 634 | lh_insert(ctx->certs,r); | ||
| 635 | X509err(X509_F_X509_STORE_ADD_CERT,X509_R_CERT_ALREADY_IN_HASH_TABLE); | ||
| 636 | ret=0; | ||
| 637 | } | ||
| 638 | |||
| 639 | CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE); | ||
| 640 | |||
| 641 | return(ret); | ||
| 642 | } | ||
| 643 | |||
| 644 | int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x) | ||
| 645 | { | ||
| 646 | X509_OBJECT *obj,*r; | ||
| 647 | int ret=1; | ||
| 648 | |||
| 649 | if (x == NULL) return(0); | ||
| 650 | obj=(X509_OBJECT *)Malloc(sizeof(X509_OBJECT)); | ||
| 651 | if (obj == NULL) | ||
| 652 | { | ||
| 653 | X509err(X509_F_X509_STORE_ADD_CRL,ERR_R_MALLOC_FAILURE); | ||
| 654 | return(0); | ||
| 655 | } | ||
| 656 | obj->type=X509_LU_CRL; | ||
| 657 | obj->data.crl=x; | ||
| 658 | |||
| 659 | CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE); | ||
| 660 | |||
| 661 | X509_OBJECT_up_ref_count(obj); | ||
| 662 | |||
| 663 | r=(X509_OBJECT *)lh_insert(ctx->certs,obj); | ||
| 664 | if (r != NULL) | ||
| 665 | { /* oops, put it back */ | ||
| 666 | lh_delete(ctx->certs,obj); | ||
| 667 | X509_OBJECT_free_contents(obj); | ||
| 668 | Free(obj); | ||
| 669 | lh_insert(ctx->certs,r); | ||
| 670 | X509err(X509_F_X509_STORE_ADD_CRL,X509_R_CERT_ALREADY_IN_HASH_TABLE); | ||
| 671 | ret=0; | ||
| 672 | } | ||
| 673 | |||
| 674 | CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE); | ||
| 675 | |||
| 676 | return(ret); | ||
| 677 | } | ||
| 678 | |||
| 679 | int X509_STORE_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, | ||
| 680 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) | ||
| 681 | { | ||
| 682 | x509_store_ctx_num++; | ||
| 683 | return(CRYPTO_get_ex_new_index(x509_store_ctx_num-1, | ||
| 684 | &x509_store_ctx_method, | ||
| 685 | argl,argp,new_func,dup_func,free_func)); | ||
| 686 | } | ||
| 687 | |||
| 688 | int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data) | ||
| 689 | { | ||
| 690 | return(CRYPTO_set_ex_data(&ctx->ex_data,idx,data)); | ||
| 691 | } | ||
| 692 | |||
| 693 | void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx) | ||
| 694 | { | ||
| 695 | return(CRYPTO_get_ex_data(&ctx->ex_data,idx)); | ||
| 696 | } | ||
| 697 | |||
| 698 | int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx) | ||
| 699 | { | ||
| 700 | return(ctx->error); | ||
| 701 | } | ||
| 702 | |||
| 703 | void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err) | ||
| 704 | { | ||
| 705 | ctx->error=err; | ||
| 706 | } | ||
| 707 | |||
| 708 | int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx) | ||
| 709 | { | ||
| 710 | return(ctx->error_depth); | ||
| 711 | } | ||
| 712 | |||
| 713 | X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx) | ||
| 714 | { | ||
| 715 | return(ctx->current_cert); | ||
| 716 | } | ||
| 717 | |||
| 718 | STACK_OF(X509) *X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx) | ||
| 719 | { | ||
| 720 | return(ctx->chain); | ||
| 721 | } | ||
| 722 | |||
| 723 | STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx) | ||
| 724 | { | ||
| 725 | int i; | ||
| 726 | X509 *x; | ||
| 727 | STACK_OF(X509) *chain; | ||
| 728 | if(!ctx->chain || !(chain = sk_X509_dup(ctx->chain))) return NULL; | ||
| 729 | for(i = 0; i < sk_X509_num(chain); i++) { | ||
| 730 | x = sk_X509_value(chain, i); | ||
| 731 | CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); | ||
| 732 | } | ||
| 733 | return(chain); | ||
| 734 | } | ||
| 735 | |||
| 736 | void X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x) | ||
| 737 | { | ||
| 738 | ctx->cert=x; | ||
| 739 | } | ||
| 740 | |||
| 741 | void X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk) | ||
| 742 | { | ||
| 743 | ctx->untrusted=sk; | ||
| 744 | } | ||
| 745 | |||
| 746 | int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose) | ||
| 747 | { | ||
| 748 | return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0); | ||
| 749 | } | ||
| 750 | |||
| 751 | int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust) | ||
| 752 | { | ||
| 753 | return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust); | ||
| 754 | } | ||
| 755 | |||
| 756 | /* This function is used to set the X509_STORE_CTX purpose and trust | ||
| 757 | * values. This is intended to be used when another structure has its | ||
| 758 | * own trust and purpose values which (if set) will be inherited by | ||
| 759 | * the ctx. If they aren't set then we will usually have a default | ||
| 760 | * purpose in mind which should then be used to set the trust value. | ||
| 761 | * An example of this is SSL use: an SSL structure will have its own | ||
| 762 | * purpose and trust settings which the application can set: if they | ||
| 763 | * aren't set then we use the default of SSL client/server. | ||
| 764 | */ | ||
| 765 | |||
| 766 | int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose, | ||
| 767 | int purpose, int trust) | ||
| 768 | { | ||
| 769 | int idx; | ||
| 770 | /* If purpose not set use default */ | ||
| 771 | if(!purpose) purpose = def_purpose; | ||
| 772 | /* If we have a purpose then check it is valid */ | ||
| 773 | if(purpose) { | ||
| 774 | X509_PURPOSE *ptmp; | ||
| 775 | idx = X509_PURPOSE_get_by_id(purpose); | ||
| 776 | if(idx == -1) { | ||
| 777 | X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT, | ||
| 778 | X509_R_UNKNOWN_PURPOSE_ID); | ||
| 779 | return 0; | ||
| 780 | } | ||
| 781 | ptmp = X509_PURPOSE_get0(idx); | ||
| 782 | if(ptmp->trust == X509_TRUST_DEFAULT) { | ||
| 783 | idx = X509_PURPOSE_get_by_id(def_purpose); | ||
| 784 | if(idx == -1) { | ||
| 785 | X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT, | ||
| 786 | X509_R_UNKNOWN_PURPOSE_ID); | ||
| 787 | return 0; | ||
| 788 | } | ||
| 789 | ptmp = X509_PURPOSE_get0(idx); | ||
| 790 | } | ||
| 791 | /* If trust not set then get from purpose default */ | ||
| 792 | if(!trust) trust = ptmp->trust; | ||
| 793 | } | ||
| 794 | if(trust) { | ||
| 795 | idx = X509_TRUST_get_by_id(trust); | ||
| 796 | if(idx == -1) { | ||
| 797 | X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT, | ||
| 798 | X509_R_UNKNOWN_TRUST_ID); | ||
| 799 | return 0; | ||
| 800 | } | ||
| 801 | } | ||
| 802 | |||
| 803 | if(purpose) ctx->purpose = purpose; | ||
| 804 | if(trust) ctx->trust = trust; | ||
| 805 | return 1; | ||
| 806 | } | ||
| 807 | |||
| 808 | |||
| 809 | IMPLEMENT_STACK_OF(X509) | ||
| 810 | IMPLEMENT_ASN1_SET_OF(X509) | ||
| 811 | |||
| 812 | IMPLEMENT_STACK_OF(X509_NAME) | ||
| 813 | |||
| 814 | IMPLEMENT_STACK_OF(X509_ATTRIBUTE) | ||
| 815 | IMPLEMENT_ASN1_SET_OF(X509_ATTRIBUTE) | ||
