Home | History | Annotate | Download | only in x509
      1 /* Copyright (C) 1995-1998 Eric Young (eay (at) cryptsoft.com)
      2  * All rights reserved.
      3  *
      4  * This package is an SSL implementation written
      5  * by Eric Young (eay (at) cryptsoft.com).
      6  * The implementation was written so as to conform with Netscapes SSL.
      7  *
      8  * This library is free for commercial and non-commercial use as long as
      9  * the following conditions are aheared to.  The following conditions
     10  * apply to all code found in this distribution, be it the RC4, RSA,
     11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
     12  * included with this distribution is covered by the same copyright terms
     13  * except that the holder is Tim Hudson (tjh (at) cryptsoft.com).
     14  *
     15  * Copyright remains Eric Young's, and as such any Copyright notices in
     16  * the code are not to be removed.
     17  * If this package is used in a product, Eric Young should be given attribution
     18  * as the author of the parts of the library used.
     19  * This can be in the form of a textual message at program startup or
     20  * in documentation (online or textual) provided with the package.
     21  *
     22  * Redistribution and use in source and binary forms, with or without
     23  * modification, are permitted provided that the following conditions
     24  * are met:
     25  * 1. Redistributions of source code must retain the copyright
     26  *    notice, this list of conditions and the following disclaimer.
     27  * 2. Redistributions in binary form must reproduce the above copyright
     28  *    notice, this list of conditions and the following disclaimer in the
     29  *    documentation and/or other materials provided with the distribution.
     30  * 3. All advertising materials mentioning features or use of this software
     31  *    must display the following acknowledgement:
     32  *    "This product includes cryptographic software written by
     33  *     Eric Young (eay (at) cryptsoft.com)"
     34  *    The word 'cryptographic' can be left out if the rouines from the library
     35  *    being used are not cryptographic related :-).
     36  * 4. If you include any Windows specific code (or a derivative thereof) from
     37  *    the apps directory (application code) you must include an acknowledgement:
     38  *    "This product includes software written by Tim Hudson (tjh (at) cryptsoft.com)"
     39  *
     40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
     41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     50  * SUCH DAMAGE.
     51  *
     52  * The licence and distribution terms for any publically available version or
     53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
     54  * copied and put under another distribution licence
     55  * [including the GNU Public Licence.] */
     56 
     57 #include <string.h>
     58 #include <time.h>
     59 
     60 #include <openssl/asn1.h>
     61 #include <openssl/buf.h>
     62 #include <openssl/err.h>
     63 #include <openssl/evp.h>
     64 #include <openssl/lhash.h>
     65 #include <openssl/mem.h>
     66 #include <openssl/obj.h>
     67 #include <openssl/thread.h>
     68 #include <openssl/x509.h>
     69 #include <openssl/x509v3.h>
     70 
     71 #include "vpm_int.h"
     72 #include "../internal.h"
     73 
     74 
     75 static CRYPTO_EX_DATA_CLASS g_ex_data_class =
     76 	CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
     77 
     78 /* CRL score values */
     79 
     80 /* No unhandled critical extensions */
     81 
     82 #define CRL_SCORE_NOCRITICAL	0x100
     83 
     84 /* certificate is within CRL scope */
     85 
     86 #define CRL_SCORE_SCOPE		0x080
     87 
     88 /* CRL times valid */
     89 
     90 #define CRL_SCORE_TIME		0x040
     91 
     92 /* Issuer name matches certificate */
     93 
     94 #define CRL_SCORE_ISSUER_NAME	0x020
     95 
     96 /* If this score or above CRL is probably valid */
     97 
     98 #define CRL_SCORE_VALID (CRL_SCORE_NOCRITICAL|CRL_SCORE_TIME|CRL_SCORE_SCOPE)
     99 
    100 /* CRL issuer is certificate issuer */
    101 
    102 #define CRL_SCORE_ISSUER_CERT	0x018
    103 
    104 /* CRL issuer is on certificate path */
    105 
    106 #define CRL_SCORE_SAME_PATH	0x008
    107 
    108 /* CRL issuer matches CRL AKID */
    109 
    110 #define CRL_SCORE_AKID		0x004
    111 
    112 /* Have a delta CRL with valid times */
    113 
    114 #define CRL_SCORE_TIME_DELTA	0x002
    115 
    116 static int null_callback(int ok,X509_STORE_CTX *e);
    117 static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer);
    118 static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x);
    119 static int check_chain_extensions(X509_STORE_CTX *ctx);
    120 static int check_name_constraints(X509_STORE_CTX *ctx);
    121 static int check_id(X509_STORE_CTX *ctx);
    122 static int check_trust(X509_STORE_CTX *ctx);
    123 static int check_revocation(X509_STORE_CTX *ctx);
    124 static int check_cert(X509_STORE_CTX *ctx);
    125 static int check_policy(X509_STORE_CTX *ctx);
    126 
    127 static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
    128 			unsigned int *preasons,
    129 			X509_CRL *crl, X509 *x);
    130 static int get_crl_delta(X509_STORE_CTX *ctx,
    131 				X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x);
    132 static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, int *pcrl_score,
    133 			X509_CRL *base, STACK_OF(X509_CRL) *crls);
    134 static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl,
    135 				X509 **pissuer, int *pcrl_score);
    136 static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
    137 				unsigned int *preasons);
    138 static int check_crl_path(X509_STORE_CTX *ctx, X509 *x);
    139 static int check_crl_chain(X509_STORE_CTX *ctx,
    140 			STACK_OF(X509) *cert_path,
    141 			STACK_OF(X509) *crl_path);
    142 
    143 static int internal_verify(X509_STORE_CTX *ctx);
    144 
    145 
    146 static int null_callback(int ok, X509_STORE_CTX *e)
    147 	{
    148 	return ok;
    149 	}
    150 
    151 #if 0
    152 static int x509_subject_cmp(X509 **a, X509 **b)
    153 	{
    154 	return X509_subject_name_cmp(*a,*b);
    155 	}
    156 #endif
    157 /* Return 1 is a certificate is self signed */
    158 static int cert_self_signed(X509 *x)
    159 	{
    160 	X509_check_purpose(x, -1, 0);
    161 	if (x->ex_flags & EXFLAG_SS)
    162 		return 1;
    163 	else
    164 		return 0;
    165 	}
    166 
    167 /* Given a certificate try and find an exact match in the store */
    168 
    169 static X509 *lookup_cert_match(X509_STORE_CTX *ctx, X509 *x)
    170 	{
    171 	STACK_OF(X509) *certs;
    172 	X509 *xtmp = NULL;
    173 	size_t i;
    174 	/* Lookup all certs with matching subject name */
    175 	certs = ctx->lookup_certs(ctx, X509_get_subject_name(x));
    176 	if (certs == NULL)
    177 		return NULL;
    178 	/* Look for exact match */
    179 	for (i = 0; i < sk_X509_num(certs); i++)
    180 		{
    181 		xtmp = sk_X509_value(certs, i);
    182 		if (!X509_cmp(xtmp, x))
    183 			break;
    184 		}
    185 	if (i < sk_X509_num(certs))
    186 		X509_up_ref(xtmp);
    187 	else
    188 		xtmp = NULL;
    189 	sk_X509_pop_free(certs, X509_free);
    190 	return xtmp;
    191 	}
    192 
    193 int X509_verify_cert(X509_STORE_CTX *ctx)
    194 	{
    195 	X509 *x,*xtmp,*chain_ss=NULL;
    196 	int bad_chain = 0;
    197 	X509_VERIFY_PARAM *param = ctx->param;
    198 	int depth,i,ok=0;
    199 	int num;
    200 	int (*cb)(int xok,X509_STORE_CTX *xctx);
    201 	STACK_OF(X509) *sktmp=NULL;
    202 	if (ctx->cert == NULL)
    203 		{
    204 		OPENSSL_PUT_ERROR(X509, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);
    205 		return -1;
    206 		}
    207 	if (ctx->chain != NULL)
    208 		{
    209 		/* This X509_STORE_CTX has already been used to verify a
    210 		 * cert. We cannot do another one. */
    211 		OPENSSL_PUT_ERROR(X509, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
    212 		return -1;
    213 		}
    214 
    215 	cb=ctx->verify_cb;
    216 
    217 	/* first we make sure the chain we are going to build is
    218 	 * present and that the first entry is in place */
    219 	ctx->chain = sk_X509_new_null();
    220 	if (ctx->chain == NULL || !sk_X509_push(ctx->chain, ctx->cert))
    221 		{
    222 		OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
    223 		goto end;
    224 		}
    225 	X509_up_ref(ctx->cert);
    226 	ctx->last_untrusted = 1;
    227 
    228 	/* We use a temporary STACK so we can chop and hack at it */
    229 	if (ctx->untrusted != NULL
    230 	    && (sktmp=sk_X509_dup(ctx->untrusted)) == NULL)
    231 		{
    232 		OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
    233 		goto end;
    234 		}
    235 
    236 	num=sk_X509_num(ctx->chain);
    237 	x=sk_X509_value(ctx->chain,num-1);
    238 	depth=param->depth;
    239 
    240 
    241 	for (;;)
    242 		{
    243 		/* If we have enough, we break */
    244 		if (depth < num) break; /* FIXME: If this happens, we should take
    245 		                         * note of it and, if appropriate, use the
    246 		                         * X509_V_ERR_CERT_CHAIN_TOO_LONG error
    247 		                         * code later.
    248 		                         */
    249 
    250 		/* If we are self signed, we break */
    251 		if (cert_self_signed(x))
    252 			break;
    253 		/* If asked see if we can find issuer in trusted store first */
    254 		if (ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST)
    255 			{
    256 			ok = ctx->get_issuer(&xtmp, ctx, x);
    257 			if (ok < 0)
    258 				goto end;
    259 			/* If successful for now free up cert so it
    260 			 * will be picked up again later.
    261 			 */
    262 			if (ok > 0)
    263 				{
    264 				X509_free(xtmp);
    265 				break;
    266 				}
    267 			}
    268 
    269 		/* If we were passed a cert chain, use it first */
    270 		if (ctx->untrusted != NULL)
    271 			{
    272 			xtmp=find_issuer(ctx, sktmp,x);
    273 			if (xtmp != NULL)
    274 				{
    275 				if (!sk_X509_push(ctx->chain,xtmp))
    276 					{
    277 					OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
    278 					goto end;
    279 					}
    280 				X509_up_ref(xtmp);
    281 				(void)sk_X509_delete_ptr(sktmp,xtmp);
    282 				ctx->last_untrusted++;
    283 				x=xtmp;
    284 				num++;
    285 				/* reparse the full chain for
    286 				 * the next one */
    287 				continue;
    288 				}
    289 			}
    290 		break;
    291 		}
    292 
    293 	/* at this point, chain should contain a list of untrusted
    294 	 * certificates.  We now need to add at least one trusted one,
    295 	 * if possible, otherwise we complain. */
    296 
    297 	/* Examine last certificate in chain and see if it
    298  	 * is self signed.
    299  	 */
    300 
    301 	i=sk_X509_num(ctx->chain);
    302 	x=sk_X509_value(ctx->chain,i-1);
    303 	if (cert_self_signed(x))
    304 		{
    305 		/* we have a self signed certificate */
    306 		if (sk_X509_num(ctx->chain) == 1)
    307 			{
    308 			/* We have a single self signed certificate: see if
    309 			 * we can find it in the store. We must have an exact
    310 			 * match to avoid possible impersonation.
    311 			 */
    312 			ok = ctx->get_issuer(&xtmp, ctx, x);
    313 			if ((ok <= 0) || X509_cmp(x, xtmp))
    314 				{
    315 				ctx->error=X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
    316 				ctx->current_cert=x;
    317 				ctx->error_depth=i-1;
    318 				if (ok == 1) X509_free(xtmp);
    319 				bad_chain = 1;
    320 				ok=cb(0,ctx);
    321 				if (!ok) goto end;
    322 				}
    323 			else
    324 				{
    325 				/* We have a match: replace certificate with store version
    326 				 * so we get any trust settings.
    327 				 */
    328 				X509_free(x);
    329 				x = xtmp;
    330 				(void)sk_X509_set(ctx->chain, i - 1, x);
    331 				ctx->last_untrusted=0;
    332 				}
    333 			}
    334 		else
    335 			{
    336 			/* extract and save self signed certificate for later use */
    337 			chain_ss=sk_X509_pop(ctx->chain);
    338 			ctx->last_untrusted--;
    339 			num--;
    340 			x=sk_X509_value(ctx->chain,num-1);
    341 			}
    342 		}
    343 
    344 	/* We now lookup certs from the certificate store */
    345 	for (;;)
    346 		{
    347 		/* If we have enough, we break */
    348 		if (depth < num) break;
    349 
    350 		/* If we are self signed, we break */
    351 		if (cert_self_signed(x))
    352 			break;
    353 
    354 		ok = ctx->get_issuer(&xtmp, ctx, x);
    355 
    356 		if (ok < 0) goto end;
    357 		if (ok == 0) break;
    358 
    359 		x = xtmp;
    360 		if (!sk_X509_push(ctx->chain,x))
    361 			{
    362 			X509_free(xtmp);
    363 			OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
    364 			ok = 0;
    365 			goto end;
    366 			}
    367 		num++;
    368 		}
    369 
    370 	/* we now have our chain, lets check it... */
    371 
    372 	i = check_trust(ctx);
    373 
    374 	/* If explicitly rejected error */
    375 	if (i == X509_TRUST_REJECTED)
    376 		goto end;
    377 	/* If not explicitly trusted then indicate error unless it's
    378 	 * a single self signed certificate in which case we've indicated
    379 	 * an error already and set bad_chain == 1
    380 	 */
    381 	if (i != X509_TRUST_TRUSTED && !bad_chain)
    382 		{
    383 		if ((chain_ss == NULL) || !ctx->check_issued(ctx, x, chain_ss))
    384 			{
    385 			if (ctx->last_untrusted >= num)
    386 				ctx->error=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
    387 			else
    388 				ctx->error=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;
    389 			ctx->current_cert=x;
    390 			}
    391 		else
    392 			{
    393 
    394 			sk_X509_push(ctx->chain,chain_ss);
    395 			num++;
    396 			ctx->last_untrusted=num;
    397 			ctx->current_cert=chain_ss;
    398 			ctx->error=X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
    399 			chain_ss=NULL;
    400 			}
    401 
    402 		ctx->error_depth=num-1;
    403 		bad_chain = 1;
    404 		ok=cb(0,ctx);
    405 		if (!ok) goto end;
    406 		}
    407 
    408 	/* We have the chain complete: now we need to check its purpose */
    409 	ok = check_chain_extensions(ctx);
    410 
    411 	if (!ok) goto end;
    412 
    413 	/* Check name constraints */
    414 
    415 	ok = check_name_constraints(ctx);
    416 
    417 	if (!ok) goto end;
    418 
    419 	ok = check_id(ctx);
    420 
    421 	if (!ok) goto end;
    422 
    423 	/* Check revocation status: we do this after copying parameters
    424 	 * because they may be needed for CRL signature verification.
    425 	 */
    426 
    427 	ok = ctx->check_revocation(ctx);
    428 	if(!ok) goto end;
    429 
    430 	i = X509_chain_check_suiteb(&ctx->error_depth, NULL, ctx->chain,
    431 							ctx->param->flags);
    432 	if (i != X509_V_OK)
    433 		{
    434 		ctx->error = i;
    435 		ctx->current_cert = sk_X509_value(ctx->chain, ctx->error_depth);
    436 		ok = cb(0, ctx);
    437 		if (!ok)
    438 			goto end;
    439 		}
    440 
    441 	/* At this point, we have a chain and need to verify it */
    442 	if (ctx->verify != NULL)
    443 		ok=ctx->verify(ctx);
    444 	else
    445 		ok=internal_verify(ctx);
    446 	if(!ok) goto end;
    447 
    448 	/* If we get this far evaluate policies */
    449 	if (!bad_chain && (ctx->param->flags & X509_V_FLAG_POLICY_CHECK))
    450 		ok = ctx->check_policy(ctx);
    451 
    452 end:
    453 	if (sktmp != NULL) sk_X509_free(sktmp);
    454 	if (chain_ss != NULL) X509_free(chain_ss);
    455 	return ok;
    456 	}
    457 
    458 
    459 /* Given a STACK_OF(X509) find the issuer of cert (if any)
    460  */
    461 
    462 static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x)
    463 {
    464 	size_t i;
    465 	X509 *issuer;
    466 	for (i = 0; i < sk_X509_num(sk); i++)
    467 		{
    468 		issuer = sk_X509_value(sk, i);
    469 		if (ctx->check_issued(ctx, x, issuer))
    470 			return issuer;
    471 		}
    472 	return NULL;
    473 }
    474 
    475 /* Given a possible certificate and issuer check them */
    476 
    477 static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer)
    478 {
    479 	int ret;
    480 	ret = X509_check_issued(issuer, x);
    481 	if (ret == X509_V_OK)
    482 		return 1;
    483 	/* If we haven't asked for issuer errors don't set ctx */
    484 	if (!(ctx->param->flags & X509_V_FLAG_CB_ISSUER_CHECK))
    485 		return 0;
    486 
    487 	ctx->error = ret;
    488 	ctx->current_cert = x;
    489 	ctx->current_issuer = issuer;
    490 	return ctx->verify_cb(0, ctx);
    491 }
    492 
    493 /* Alternative lookup method: look from a STACK stored in other_ctx */
    494 
    495 static int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
    496 {
    497 	*issuer = find_issuer(ctx, ctx->other_ctx, x);
    498 	if (*issuer)
    499 		{
    500 		X509_up_ref(*issuer);
    501 		return 1;
    502 		}
    503 	else
    504 		return 0;
    505 }
    506 
    507 
    508 /* Check a certificate chains extensions for consistency
    509  * with the supplied purpose
    510  */
    511 
    512 static int check_chain_extensions(X509_STORE_CTX *ctx)
    513 {
    514 	int i, ok=0, must_be_ca, plen = 0;
    515 	X509 *x;
    516 	int (*cb)(int xok,X509_STORE_CTX *xctx);
    517 	int proxy_path_length = 0;
    518 	int purpose;
    519 	int allow_proxy_certs;
    520 	cb=ctx->verify_cb;
    521 
    522 	/* must_be_ca can have 1 of 3 values:
    523 	   -1: we accept both CA and non-CA certificates, to allow direct
    524 	       use of self-signed certificates (which are marked as CA).
    525 	   0:  we only accept non-CA certificates.  This is currently not
    526 	       used, but the possibility is present for future extensions.
    527 	   1:  we only accept CA certificates.  This is currently used for
    528 	       all certificates in the chain except the leaf certificate.
    529 	*/
    530 	must_be_ca = -1;
    531 
    532 	/* CRL path validation */
    533 	if (ctx->parent)
    534 		{
    535 		allow_proxy_certs = 0;
    536 		purpose = X509_PURPOSE_CRL_SIGN;
    537 		}
    538 	else
    539 		{
    540 		allow_proxy_certs =
    541 			!!(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);
    542 		/* A hack to keep people who don't want to modify their
    543 		   software happy */
    544 		if (getenv("OPENSSL_ALLOW_PROXY_CERTS"))
    545 			allow_proxy_certs = 1;
    546 		purpose = ctx->param->purpose;
    547 		}
    548 
    549 	/* Check all untrusted certificates */
    550 	for (i = 0; i < ctx->last_untrusted; i++)
    551 		{
    552 		int ret;
    553 		x = sk_X509_value(ctx->chain, i);
    554 		if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
    555 			&& (x->ex_flags & EXFLAG_CRITICAL))
    556 			{
    557 			ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
    558 			ctx->error_depth = i;
    559 			ctx->current_cert = x;
    560 			ok=cb(0,ctx);
    561 			if (!ok) goto end;
    562 			}
    563 		if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY))
    564 			{
    565 			ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;
    566 			ctx->error_depth = i;
    567 			ctx->current_cert = x;
    568 			ok=cb(0,ctx);
    569 			if (!ok) goto end;
    570 			}
    571 		ret = X509_check_ca(x);
    572 		switch(must_be_ca)
    573 			{
    574 		case -1:
    575 			if ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
    576 				&& (ret != 1) && (ret != 0))
    577 				{
    578 				ret = 0;
    579 				ctx->error = X509_V_ERR_INVALID_CA;
    580 				}
    581 			else
    582 				ret = 1;
    583 			break;
    584 		case 0:
    585 			if (ret != 0)
    586 				{
    587 				ret = 0;
    588 				ctx->error = X509_V_ERR_INVALID_NON_CA;
    589 				}
    590 			else
    591 				ret = 1;
    592 			break;
    593 		default:
    594 			if ((ret == 0)
    595 				|| ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
    596 					&& (ret != 1)))
    597 				{
    598 				ret = 0;
    599 				ctx->error = X509_V_ERR_INVALID_CA;
    600 				}
    601 			else
    602 				ret = 1;
    603 			break;
    604 			}
    605 		if (ret == 0)
    606 			{
    607 			ctx->error_depth = i;
    608 			ctx->current_cert = x;
    609 			ok=cb(0,ctx);
    610 			if (!ok) goto end;
    611 			}
    612 		if (ctx->param->purpose > 0)
    613 			{
    614 			ret = X509_check_purpose(x, purpose, must_be_ca > 0);
    615 			if ((ret == 0)
    616 				|| ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
    617 					&& (ret != 1)))
    618 				{
    619 				ctx->error = X509_V_ERR_INVALID_PURPOSE;
    620 				ctx->error_depth = i;
    621 				ctx->current_cert = x;
    622 				ok=cb(0,ctx);
    623 				if (!ok) goto end;
    624 				}
    625 			}
    626 		/* Check pathlen if not self issued */
    627 		if ((i > 1) && !(x->ex_flags & EXFLAG_SI)
    628 			   && (x->ex_pathlen != -1)
    629 			   && (plen > (x->ex_pathlen + proxy_path_length + 1)))
    630 			{
    631 			ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
    632 			ctx->error_depth = i;
    633 			ctx->current_cert = x;
    634 			ok=cb(0,ctx);
    635 			if (!ok) goto end;
    636 			}
    637 		/* Increment path length if not self issued */
    638 		if (!(x->ex_flags & EXFLAG_SI))
    639 			plen++;
    640 		/* If this certificate is a proxy certificate, the next
    641 		   certificate must be another proxy certificate or a EE
    642 		   certificate.  If not, the next certificate must be a
    643 		   CA certificate.  */
    644 		if (x->ex_flags & EXFLAG_PROXY)
    645 			{
    646 			if (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen)
    647 				{
    648 				ctx->error =
    649 					X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;
    650 				ctx->error_depth = i;
    651 				ctx->current_cert = x;
    652 				ok=cb(0,ctx);
    653 				if (!ok) goto end;
    654 				}
    655 			proxy_path_length++;
    656 			must_be_ca = 0;
    657 			}
    658 		else
    659 			must_be_ca = 1;
    660 		}
    661 	ok = 1;
    662  end:
    663 	return ok;
    664 }
    665 
    666 static int check_name_constraints(X509_STORE_CTX *ctx)
    667 	{
    668 	X509 *x;
    669 	int i, j, rv;
    670 	/* Check name constraints for all certificates */
    671 	for (i = sk_X509_num(ctx->chain) - 1; i >= 0; i--)
    672 		{
    673 		x = sk_X509_value(ctx->chain, i);
    674 		/* Ignore self issued certs unless last in chain */
    675 		if (i && (x->ex_flags & EXFLAG_SI))
    676 			continue;
    677 		/* Check against constraints for all certificates higher in
    678 		 * chain including trust anchor. Trust anchor not strictly
    679 		 * speaking needed but if it includes constraints it is to be
    680 		 * assumed it expects them to be obeyed.
    681 		 */
    682 		for (j = sk_X509_num(ctx->chain) - 1; j > i; j--)
    683 			{
    684 			NAME_CONSTRAINTS *nc = sk_X509_value(ctx->chain, j)->nc;
    685 			if (nc)
    686 				{
    687 				rv = NAME_CONSTRAINTS_check(x, nc);
    688 				if (rv != X509_V_OK)
    689 					{
    690 					ctx->error = rv;
    691 					ctx->error_depth = i;
    692 					ctx->current_cert = x;
    693 					if (!ctx->verify_cb(0,ctx))
    694 						return 0;
    695 					}
    696 				}
    697 			}
    698 		}
    699 	return 1;
    700 	}
    701 
    702 static int check_id_error(X509_STORE_CTX *ctx, int errcode)
    703 	{
    704 	ctx->error = errcode;
    705 	ctx->current_cert = ctx->cert;
    706 	ctx->error_depth = 0;
    707 	return ctx->verify_cb(0, ctx);
    708 	}
    709 
    710 static int check_hosts(X509 *x, X509_VERIFY_PARAM_ID *id)
    711 	{
    712 	size_t i;
    713 	size_t n = sk_OPENSSL_STRING_num(id->hosts);
    714 	char *name;
    715 
    716 	for (i = 0; i < n; ++i)
    717 		{
    718 		name = sk_OPENSSL_STRING_value(id->hosts, i);
    719 		if (X509_check_host(x, name, strlen(name), id->hostflags,
    720 				    &id->peername) > 0)
    721 			return 1;
    722 		}
    723 	return n == 0;
    724 	}
    725 
    726 static int check_id(X509_STORE_CTX *ctx)
    727 	{
    728 	X509_VERIFY_PARAM *vpm = ctx->param;
    729 	X509_VERIFY_PARAM_ID *id = vpm->id;
    730 	X509 *x = ctx->cert;
    731 	if (id->hosts && check_hosts(x, id) <= 0)
    732 		{
    733 		if (!check_id_error(ctx, X509_V_ERR_HOSTNAME_MISMATCH))
    734 			return 0;
    735 		}
    736 	if (id->email && X509_check_email(x, id->email, id->emaillen, 0) <= 0)
    737 		{
    738 		if (!check_id_error(ctx, X509_V_ERR_EMAIL_MISMATCH))
    739 			return 0;
    740 		}
    741 	if (id->ip && X509_check_ip(x, id->ip, id->iplen, 0) <= 0)
    742 		{
    743 		if (!check_id_error(ctx, X509_V_ERR_IP_ADDRESS_MISMATCH))
    744 			return 0;
    745 		}
    746 	return 1;
    747 	}
    748 
    749 static int check_trust(X509_STORE_CTX *ctx)
    750 {
    751 	size_t i;
    752 	int ok;
    753 	X509 *x = NULL;
    754 	int (*cb)(int xok,X509_STORE_CTX *xctx);
    755 	cb=ctx->verify_cb;
    756 	/* Check all trusted certificates in chain */
    757 	for (i = ctx->last_untrusted; i < sk_X509_num(ctx->chain); i++)
    758 		{
    759 		x = sk_X509_value(ctx->chain, i);
    760 		ok = X509_check_trust(x, ctx->param->trust, 0);
    761 		/* If explicitly trusted return trusted */
    762 		if (ok == X509_TRUST_TRUSTED)
    763 			return X509_TRUST_TRUSTED;
    764 		/* If explicitly rejected notify callback and reject if
    765 		 * not overridden.
    766 		 */
    767 		if (ok == X509_TRUST_REJECTED)
    768 			{
    769 			ctx->error_depth = i;
    770 			ctx->current_cert = x;
    771 			ctx->error = X509_V_ERR_CERT_REJECTED;
    772 			ok = cb(0, ctx);
    773 			if (!ok)
    774 				return X509_TRUST_REJECTED;
    775 			}
    776 		}
    777 	/* If we accept partial chains and have at least one trusted
    778 	 * certificate return success.
    779 	 */
    780 	if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN)
    781 		{
    782 		X509 *mx;
    783 		if (ctx->last_untrusted < (int) sk_X509_num(ctx->chain))
    784 			return X509_TRUST_TRUSTED;
    785 		x = sk_X509_value(ctx->chain, 0);
    786 		mx = lookup_cert_match(ctx, x);
    787 		if (mx)
    788 			{
    789 			(void)sk_X509_set(ctx->chain, 0, mx);
    790 			X509_free(x);
    791 			ctx->last_untrusted = 0;
    792 			return X509_TRUST_TRUSTED;
    793 			}
    794 		}
    795 
    796 	/* If no trusted certs in chain at all return untrusted and
    797 	 * allow standard (no issuer cert) etc errors to be indicated.
    798 	 */
    799 	return X509_TRUST_UNTRUSTED;
    800 }
    801 
    802 static int check_revocation(X509_STORE_CTX *ctx)
    803 	{
    804 	int i, last, ok;
    805 	if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK))
    806 		return 1;
    807 	if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL)
    808 		last = sk_X509_num(ctx->chain) - 1;
    809 	else
    810 		{
    811 		/* If checking CRL paths this isn't the EE certificate */
    812 		if (ctx->parent)
    813 			return 1;
    814 		last = 0;
    815 		}
    816 	for(i = 0; i <= last; i++)
    817 		{
    818 		ctx->error_depth = i;
    819 		ok = check_cert(ctx);
    820 		if (!ok) return ok;
    821 		}
    822 	return 1;
    823 	}
    824 
    825 static int check_cert(X509_STORE_CTX *ctx)
    826                       OPENSSL_SUPPRESS_POTENTIALLY_UNINITIALIZED_WARNINGS
    827 	{
    828 	X509_CRL *crl = NULL, *dcrl = NULL;
    829 	X509 *x;
    830 	int ok, cnum;
    831 	unsigned int last_reasons;
    832 	cnum = ctx->error_depth;
    833 	x = sk_X509_value(ctx->chain, cnum);
    834 	ctx->current_cert = x;
    835 	ctx->current_issuer = NULL;
    836 	ctx->current_crl_score = 0;
    837 	ctx->current_reasons = 0;
    838 	while (ctx->current_reasons != CRLDP_ALL_REASONS)
    839 		{
    840 		last_reasons = ctx->current_reasons;
    841 		/* Try to retrieve relevant CRL */
    842 		if (ctx->get_crl)
    843 			ok = ctx->get_crl(ctx, &crl, x);
    844 		else
    845 			ok = get_crl_delta(ctx, &crl, &dcrl, x);
    846 		/* If error looking up CRL, nothing we can do except
    847 		 * notify callback
    848 		 */
    849 		if(!ok)
    850 			{
    851 			ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
    852 			ok = ctx->verify_cb(0, ctx);
    853 			goto err;
    854 			}
    855 		ctx->current_crl = crl;
    856 		ok = ctx->check_crl(ctx, crl);
    857 		if (!ok)
    858 			goto err;
    859 
    860 		if (dcrl)
    861 			{
    862 			ok = ctx->check_crl(ctx, dcrl);
    863 			if (!ok)
    864 				goto err;
    865 			ok = ctx->cert_crl(ctx, dcrl, x);
    866 			if (!ok)
    867 				goto err;
    868 			}
    869 		else
    870 			ok = 1;
    871 
    872 		/* Don't look in full CRL if delta reason is removefromCRL */
    873 		if (ok != 2)
    874 			{
    875 			ok = ctx->cert_crl(ctx, crl, x);
    876 			if (!ok)
    877 				goto err;
    878 			}
    879 
    880 		X509_CRL_free(crl);
    881 		X509_CRL_free(dcrl);
    882 		crl = NULL;
    883 		dcrl = NULL;
    884 		/* If reasons not updated we wont get anywhere by
    885 		 * another iteration, so exit loop.
    886 		 */
    887 		if (last_reasons == ctx->current_reasons)
    888 			{
    889 			ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
    890 			ok = ctx->verify_cb(0, ctx);
    891 			goto err;
    892 			}
    893 		}
    894 	err:
    895 	X509_CRL_free(crl);
    896 	X509_CRL_free(dcrl);
    897 
    898 	ctx->current_crl = NULL;
    899 	return ok;
    900 
    901 	}
    902 
    903 /* Check CRL times against values in X509_STORE_CTX */
    904 
    905 static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify)
    906 	{
    907 	time_t *ptime;
    908 	int i;
    909 	if (notify)
    910 		ctx->current_crl = crl;
    911 	if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
    912 		ptime = &ctx->param->check_time;
    913 	else
    914 		ptime = NULL;
    915 
    916 	i=X509_cmp_time(X509_CRL_get_lastUpdate(crl), ptime);
    917 	if (i == 0)
    918 		{
    919 		if (!notify)
    920 			return 0;
    921 		ctx->error=X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD;
    922 		if (!ctx->verify_cb(0, ctx))
    923 			return 0;
    924 		}
    925 
    926 	if (i > 0)
    927 		{
    928 		if (!notify)
    929 			return 0;
    930 		ctx->error=X509_V_ERR_CRL_NOT_YET_VALID;
    931 		if (!ctx->verify_cb(0, ctx))
    932 			return 0;
    933 		}
    934 
    935 	if(X509_CRL_get_nextUpdate(crl))
    936 		{
    937 		i=X509_cmp_time(X509_CRL_get_nextUpdate(crl), ptime);
    938 
    939 		if (i == 0)
    940 			{
    941 			if (!notify)
    942 				return 0;
    943 			ctx->error=X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD;
    944 			if (!ctx->verify_cb(0, ctx))
    945 				return 0;
    946 			}
    947 		/* Ignore expiry of base CRL is delta is valid */
    948 		if ((i < 0) && !(ctx->current_crl_score & CRL_SCORE_TIME_DELTA))
    949 			{
    950 			if (!notify)
    951 				return 0;
    952 			ctx->error=X509_V_ERR_CRL_HAS_EXPIRED;
    953 			if (!ctx->verify_cb(0, ctx))
    954 				return 0;
    955 			}
    956 		}
    957 
    958 	if (notify)
    959 		ctx->current_crl = NULL;
    960 
    961 	return 1;
    962 	}
    963 
    964 static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509_CRL **pdcrl,
    965 			X509 **pissuer, int *pscore, unsigned int *preasons,
    966 			STACK_OF(X509_CRL) *crls)
    967 	{
    968 	int crl_score, best_score = *pscore;
    969 	size_t i;
    970 	unsigned int reasons, best_reasons = 0;
    971 	X509 *x = ctx->current_cert;
    972 	X509_CRL *crl, *best_crl = NULL;
    973 	X509 *crl_issuer = NULL, *best_crl_issuer = NULL;
    974 
    975 	for (i = 0; i < sk_X509_CRL_num(crls); i++)
    976 		{
    977 		crl = sk_X509_CRL_value(crls, i);
    978 		reasons = *preasons;
    979 		crl_score = get_crl_score(ctx, &crl_issuer, &reasons, crl, x);
    980 
    981 		if (crl_score > best_score)
    982 			{
    983 			best_crl = crl;
    984 			best_crl_issuer = crl_issuer;
    985 			best_score = crl_score;
    986 			best_reasons = reasons;
    987 			}
    988 		}
    989 
    990 	if (best_crl)
    991 		{
    992 		if (*pcrl)
    993 			X509_CRL_free(*pcrl);
    994 		*pcrl = best_crl;
    995 		*pissuer = best_crl_issuer;
    996 		*pscore = best_score;
    997 		*preasons = best_reasons;
    998 		X509_CRL_up_ref(best_crl);
    999 		if (*pdcrl)
   1000 			{
   1001 			X509_CRL_free(*pdcrl);
   1002 			*pdcrl = NULL;
   1003 			}
   1004 		get_delta_sk(ctx, pdcrl, pscore, best_crl, crls);
   1005 		}
   1006 
   1007 	if (best_score >= CRL_SCORE_VALID)
   1008 		return 1;
   1009 
   1010 	return 0;
   1011 	}
   1012 
   1013 /* Compare two CRL extensions for delta checking purposes. They should be
   1014  * both present or both absent. If both present all fields must be identical.
   1015  */
   1016 
   1017 static int crl_extension_match(X509_CRL *a, X509_CRL *b, int nid)
   1018 	{
   1019 	ASN1_OCTET_STRING *exta, *extb;
   1020 	int i;
   1021 	i = X509_CRL_get_ext_by_NID(a, nid, -1);
   1022 	if (i >= 0)
   1023 		{
   1024 		/* Can't have multiple occurrences */
   1025 		if (X509_CRL_get_ext_by_NID(a, nid, i) != -1)
   1026 			return 0;
   1027 		exta = X509_EXTENSION_get_data(X509_CRL_get_ext(a, i));
   1028 		}
   1029 	else
   1030 		exta = NULL;
   1031 
   1032 	i = X509_CRL_get_ext_by_NID(b, nid, -1);
   1033 
   1034 	if (i >= 0)
   1035 		{
   1036 
   1037 		if (X509_CRL_get_ext_by_NID(b, nid, i) != -1)
   1038 			return 0;
   1039 		extb = X509_EXTENSION_get_data(X509_CRL_get_ext(b, i));
   1040 		}
   1041 	else
   1042 		extb = NULL;
   1043 
   1044 	if (!exta && !extb)
   1045 		return 1;
   1046 
   1047 	if (!exta || !extb)
   1048 		return 0;
   1049 
   1050 
   1051 	if (ASN1_OCTET_STRING_cmp(exta, extb))
   1052 		return 0;
   1053 
   1054 	return 1;
   1055 	}
   1056 
   1057 /* See if a base and delta are compatible */
   1058 
   1059 static int check_delta_base(X509_CRL *delta, X509_CRL *base)
   1060 	{
   1061 	/* Delta CRL must be a delta */
   1062 	if (!delta->base_crl_number)
   1063 			return 0;
   1064 	/* Base must have a CRL number */
   1065 	if (!base->crl_number)
   1066 			return 0;
   1067 	/* Issuer names must match */
   1068 	if (X509_NAME_cmp(X509_CRL_get_issuer(base),
   1069 				X509_CRL_get_issuer(delta)))
   1070 		return 0;
   1071 	/* AKID and IDP must match */
   1072 	if (!crl_extension_match(delta, base, NID_authority_key_identifier))
   1073 			return 0;
   1074 	if (!crl_extension_match(delta, base, NID_issuing_distribution_point))
   1075 			return 0;
   1076 	/* Delta CRL base number must not exceed Full CRL number. */
   1077 	if (ASN1_INTEGER_cmp(delta->base_crl_number, base->crl_number) > 0)
   1078 			return 0;
   1079 	/* Delta CRL number must exceed full CRL number */
   1080 	if (ASN1_INTEGER_cmp(delta->crl_number, base->crl_number) > 0)
   1081 			return 1;
   1082 	return 0;
   1083 	}
   1084 
   1085 /* For a given base CRL find a delta... maybe extend to delta scoring
   1086  * or retrieve a chain of deltas...
   1087  */
   1088 
   1089 static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, int *pscore,
   1090 			X509_CRL *base, STACK_OF(X509_CRL) *crls)
   1091 	{
   1092 	X509_CRL *delta;
   1093 	size_t i;
   1094 	if (!(ctx->param->flags & X509_V_FLAG_USE_DELTAS))
   1095 		return;
   1096 	if (!((ctx->current_cert->ex_flags | base->flags) & EXFLAG_FRESHEST))
   1097 		return;
   1098 	for (i = 0; i < sk_X509_CRL_num(crls); i++)
   1099 		{
   1100 		delta = sk_X509_CRL_value(crls, i);
   1101 		if (check_delta_base(delta, base))
   1102 			{
   1103 			if (check_crl_time(ctx, delta, 0))
   1104 				*pscore |= CRL_SCORE_TIME_DELTA;
   1105 			X509_CRL_up_ref(delta);
   1106 			*dcrl = delta;
   1107 			return;
   1108 			}
   1109 		}
   1110 	*dcrl = NULL;
   1111 	}
   1112 
   1113 /* For a given CRL return how suitable it is for the supplied certificate 'x'.
   1114  * The return value is a mask of several criteria.
   1115  * If the issuer is not the certificate issuer this is returned in *pissuer.
   1116  * The reasons mask is also used to determine if the CRL is suitable: if
   1117  * no new reasons the CRL is rejected, otherwise reasons is updated.
   1118  */
   1119 
   1120 static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
   1121 			unsigned int *preasons,
   1122 			X509_CRL *crl, X509 *x)
   1123 	{
   1124 
   1125 	int crl_score = 0;
   1126 	unsigned int tmp_reasons = *preasons, crl_reasons;
   1127 
   1128 	/* First see if we can reject CRL straight away */
   1129 
   1130 	/* Invalid IDP cannot be processed */
   1131 	if (crl->idp_flags & IDP_INVALID)
   1132 		return 0;
   1133 	/* Reason codes or indirect CRLs need extended CRL support */
   1134 	if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT))
   1135 		{
   1136 		if (crl->idp_flags & (IDP_INDIRECT | IDP_REASONS))
   1137 			return 0;
   1138 		}
   1139 	else if (crl->idp_flags & IDP_REASONS)
   1140 		{
   1141 		/* If no new reasons reject */
   1142 		if (!(crl->idp_reasons & ~tmp_reasons))
   1143 			return 0;
   1144 		}
   1145 	/* Don't process deltas at this stage */
   1146 	else if (crl->base_crl_number)
   1147 		return 0;
   1148 	/* If issuer name doesn't match certificate need indirect CRL */
   1149 	if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl)))
   1150 		{
   1151 		if (!(crl->idp_flags & IDP_INDIRECT))
   1152 			return 0;
   1153 		}
   1154 	else
   1155 		crl_score |= CRL_SCORE_ISSUER_NAME;
   1156 
   1157 	if (!(crl->flags & EXFLAG_CRITICAL))
   1158 		crl_score |= CRL_SCORE_NOCRITICAL;
   1159 
   1160 	/* Check expiry */
   1161 	if (check_crl_time(ctx, crl, 0))
   1162 		crl_score |= CRL_SCORE_TIME;
   1163 
   1164 	/* Check authority key ID and locate certificate issuer */
   1165 	crl_akid_check(ctx, crl, pissuer, &crl_score);
   1166 
   1167 	/* If we can't locate certificate issuer at this point forget it */
   1168 
   1169 	if (!(crl_score & CRL_SCORE_AKID))
   1170 		return 0;
   1171 
   1172 	/* Check cert for matching CRL distribution points */
   1173 
   1174 	if (crl_crldp_check(x, crl, crl_score, &crl_reasons))
   1175 		{
   1176 		/* If no new reasons reject */
   1177 		if (!(crl_reasons & ~tmp_reasons))
   1178 			return 0;
   1179 		tmp_reasons |= crl_reasons;
   1180 		crl_score |= CRL_SCORE_SCOPE;
   1181 		}
   1182 
   1183 	*preasons = tmp_reasons;
   1184 
   1185 	return crl_score;
   1186 
   1187 	}
   1188 
   1189 static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl,
   1190 				X509 **pissuer, int *pcrl_score)
   1191 	{
   1192 	X509 *crl_issuer = NULL;
   1193 	X509_NAME *cnm = X509_CRL_get_issuer(crl);
   1194 	int cidx = ctx->error_depth;
   1195 	size_t i;
   1196 
   1197 	if ((size_t) cidx != sk_X509_num(ctx->chain) - 1)
   1198 		cidx++;
   1199 
   1200 	crl_issuer = sk_X509_value(ctx->chain, cidx);
   1201 
   1202 	if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK)
   1203 		{
   1204 		if (*pcrl_score & CRL_SCORE_ISSUER_NAME)
   1205 			{
   1206 			*pcrl_score |= CRL_SCORE_AKID|CRL_SCORE_ISSUER_CERT;
   1207 			*pissuer = crl_issuer;
   1208 			return;
   1209 			}
   1210 		}
   1211 
   1212 	for (cidx++; cidx < (int) sk_X509_num(ctx->chain); cidx++)
   1213 		{
   1214 		crl_issuer = sk_X509_value(ctx->chain, cidx);
   1215 		if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
   1216 			continue;
   1217 		if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK)
   1218 			{
   1219 			*pcrl_score |= CRL_SCORE_AKID|CRL_SCORE_SAME_PATH;
   1220 			*pissuer = crl_issuer;
   1221 			return;
   1222 			}
   1223 		}
   1224 
   1225 	/* Anything else needs extended CRL support */
   1226 
   1227 	if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT))
   1228 		return;
   1229 
   1230 	/* Otherwise the CRL issuer is not on the path. Look for it in the
   1231 	 * set of untrusted certificates.
   1232 	 */
   1233 	for (i = 0; i < sk_X509_num(ctx->untrusted); i++)
   1234 		{
   1235 		crl_issuer = sk_X509_value(ctx->untrusted, i);
   1236 		if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
   1237 			continue;
   1238 		if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK)
   1239 			{
   1240 			*pissuer = crl_issuer;
   1241 			*pcrl_score |= CRL_SCORE_AKID;
   1242 			return;
   1243 			}
   1244 		}
   1245 	}
   1246 
   1247 /* Check the path of a CRL issuer certificate. This creates a new
   1248  * X509_STORE_CTX and populates it with most of the parameters from the
   1249  * parent. This could be optimised somewhat since a lot of path checking
   1250  * will be duplicated by the parent, but this will rarely be used in
   1251  * practice.
   1252  */
   1253 
   1254 static int check_crl_path(X509_STORE_CTX *ctx, X509 *x)
   1255 	{
   1256 	X509_STORE_CTX crl_ctx;
   1257 	int ret;
   1258 	/* Don't allow recursive CRL path validation */
   1259 	if (ctx->parent)
   1260 		return 0;
   1261 	if (!X509_STORE_CTX_init(&crl_ctx, ctx->ctx, x, ctx->untrusted))
   1262 		return -1;
   1263 
   1264 	crl_ctx.crls = ctx->crls;
   1265 	/* Copy verify params across */
   1266 	X509_STORE_CTX_set0_param(&crl_ctx, ctx->param);
   1267 
   1268 	crl_ctx.parent = ctx;
   1269 	crl_ctx.verify_cb = ctx->verify_cb;
   1270 
   1271 	/* Verify CRL issuer */
   1272 	ret = X509_verify_cert(&crl_ctx);
   1273 
   1274 	if (ret <= 0)
   1275 		goto err;
   1276 
   1277 	/* Check chain is acceptable */
   1278 
   1279 	ret = check_crl_chain(ctx, ctx->chain, crl_ctx.chain);
   1280 	err:
   1281 	X509_STORE_CTX_cleanup(&crl_ctx);
   1282 	return ret;
   1283 	}
   1284 
   1285 /* RFC3280 says nothing about the relationship between CRL path
   1286  * and certificate path, which could lead to situations where a
   1287  * certificate could be revoked or validated by a CA not authorised
   1288  * to do so. RFC5280 is more strict and states that the two paths must
   1289  * end in the same trust anchor, though some discussions remain...
   1290  * until this is resolved we use the RFC5280 version
   1291  */
   1292 
   1293 static int check_crl_chain(X509_STORE_CTX *ctx,
   1294 			STACK_OF(X509) *cert_path,
   1295 			STACK_OF(X509) *crl_path)
   1296 	{
   1297 	X509 *cert_ta, *crl_ta;
   1298 	cert_ta = sk_X509_value(cert_path, sk_X509_num(cert_path) - 1);
   1299 	crl_ta = sk_X509_value(crl_path, sk_X509_num(crl_path) - 1);
   1300 	if (!X509_cmp(cert_ta, crl_ta))
   1301 		return 1;
   1302 	return 0;
   1303 	}
   1304 
   1305 /* Check for match between two dist point names: three separate cases.
   1306  * 1. Both are relative names and compare X509_NAME types.
   1307  * 2. One full, one relative. Compare X509_NAME to GENERAL_NAMES.
   1308  * 3. Both are full names and compare two GENERAL_NAMES.
   1309  * 4. One is NULL: automatic match.
   1310  */
   1311 
   1312 
   1313 static int idp_check_dp(DIST_POINT_NAME *a, DIST_POINT_NAME *b)
   1314 	{
   1315 	X509_NAME *nm = NULL;
   1316 	GENERAL_NAMES *gens = NULL;
   1317 	GENERAL_NAME *gena, *genb;
   1318 	size_t i, j;
   1319 	if (!a || !b)
   1320 		return 1;
   1321 	if (a->type == 1)
   1322 		{
   1323 		if (!a->dpname)
   1324 			return 0;
   1325 		/* Case 1: two X509_NAME */
   1326 		if (b->type == 1)
   1327 			{
   1328 			if (!b->dpname)
   1329 				return 0;
   1330 			if (!X509_NAME_cmp(a->dpname, b->dpname))
   1331 				return 1;
   1332 			else
   1333 				return 0;
   1334 			}
   1335 		/* Case 2: set name and GENERAL_NAMES appropriately */
   1336 		nm = a->dpname;
   1337 		gens = b->name.fullname;
   1338 		}
   1339 	else if (b->type == 1)
   1340 		{
   1341 		if (!b->dpname)
   1342 			return 0;
   1343 		/* Case 2: set name and GENERAL_NAMES appropriately */
   1344 		gens = a->name.fullname;
   1345 		nm = b->dpname;
   1346 		}
   1347 
   1348 	/* Handle case 2 with one GENERAL_NAMES and one X509_NAME */
   1349 	if (nm)
   1350 		{
   1351 		for (i = 0; i < sk_GENERAL_NAME_num(gens); i++)
   1352 			{
   1353 			gena = sk_GENERAL_NAME_value(gens, i);
   1354 			if (gena->type != GEN_DIRNAME)
   1355 				continue;
   1356 			if (!X509_NAME_cmp(nm, gena->d.directoryName))
   1357 				return 1;
   1358 			}
   1359 		return 0;
   1360 		}
   1361 
   1362 	/* Else case 3: two GENERAL_NAMES */
   1363 
   1364 	for (i = 0; i < sk_GENERAL_NAME_num(a->name.fullname); i++)
   1365 		{
   1366 		gena = sk_GENERAL_NAME_value(a->name.fullname, i);
   1367 		for (j = 0; j < sk_GENERAL_NAME_num(b->name.fullname); j++)
   1368 			{
   1369 			genb = sk_GENERAL_NAME_value(b->name.fullname, j);
   1370 			if (!GENERAL_NAME_cmp(gena, genb))
   1371 				return 1;
   1372 			}
   1373 		}
   1374 
   1375 	return 0;
   1376 
   1377 	}
   1378 
   1379 static int crldp_check_crlissuer(DIST_POINT *dp, X509_CRL *crl, int crl_score)
   1380 	{
   1381 	size_t i;
   1382 	X509_NAME *nm = X509_CRL_get_issuer(crl);
   1383 	/* If no CRLissuer return is successful iff don't need a match */
   1384 	if (!dp->CRLissuer)
   1385 		return !!(crl_score & CRL_SCORE_ISSUER_NAME);
   1386 	for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++)
   1387 		{
   1388 		GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
   1389 		if (gen->type != GEN_DIRNAME)
   1390 			continue;
   1391 		if (!X509_NAME_cmp(gen->d.directoryName, nm))
   1392 			return 1;
   1393 		}
   1394 	return 0;
   1395 	}
   1396 
   1397 /* Check CRLDP and IDP */
   1398 
   1399 static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
   1400 				unsigned int *preasons)
   1401 	{
   1402 	size_t i;
   1403 	if (crl->idp_flags & IDP_ONLYATTR)
   1404 		return 0;
   1405 	if (x->ex_flags & EXFLAG_CA)
   1406 		{
   1407 		if (crl->idp_flags & IDP_ONLYUSER)
   1408 			return 0;
   1409 		}
   1410 	else
   1411 		{
   1412 		if (crl->idp_flags & IDP_ONLYCA)
   1413 			return 0;
   1414 		}
   1415 	*preasons = crl->idp_reasons;
   1416 	for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++)
   1417 		{
   1418 		DIST_POINT *dp = sk_DIST_POINT_value(x->crldp, i);
   1419 		if (crldp_check_crlissuer(dp, crl, crl_score))
   1420 			{
   1421 			if (!crl->idp ||
   1422 			     idp_check_dp(dp->distpoint, crl->idp->distpoint))
   1423 				{
   1424 				*preasons &= dp->dp_reasons;
   1425 				return 1;
   1426 				}
   1427 			}
   1428 		}
   1429 	if ((!crl->idp || !crl->idp->distpoint) && (crl_score & CRL_SCORE_ISSUER_NAME))
   1430 		return 1;
   1431 	return 0;
   1432 	}
   1433 
   1434 /* Retrieve CRL corresponding to current certificate.
   1435  * If deltas enabled try to find a delta CRL too
   1436  */
   1437 
   1438 static int get_crl_delta(X509_STORE_CTX *ctx,
   1439 				X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x)
   1440 	{
   1441 	int ok;
   1442 	X509 *issuer = NULL;
   1443 	int crl_score = 0;
   1444 	unsigned int reasons;
   1445 	X509_CRL *crl = NULL, *dcrl = NULL;
   1446 	STACK_OF(X509_CRL) *skcrl;
   1447 	X509_NAME *nm = X509_get_issuer_name(x);
   1448 	reasons = ctx->current_reasons;
   1449 	ok = get_crl_sk(ctx, &crl, &dcrl,
   1450 				&issuer, &crl_score, &reasons, ctx->crls);
   1451 
   1452 	if (ok)
   1453 		goto done;
   1454 
   1455 	/* Lookup CRLs from store */
   1456 
   1457 	skcrl = ctx->lookup_crls(ctx, nm);
   1458 
   1459 	/* If no CRLs found and a near match from get_crl_sk use that */
   1460 	if (!skcrl && crl)
   1461 		goto done;
   1462 
   1463 	get_crl_sk(ctx, &crl, &dcrl, &issuer, &crl_score, &reasons, skcrl);
   1464 
   1465 	sk_X509_CRL_pop_free(skcrl, X509_CRL_free);
   1466 
   1467 	done:
   1468 
   1469 	/* If we got any kind of CRL use it and return success */
   1470 	if (crl)
   1471 		{
   1472 		ctx->current_issuer = issuer;
   1473 		ctx->current_crl_score = crl_score;
   1474 		ctx->current_reasons = reasons;
   1475 		*pcrl = crl;
   1476 		*pdcrl = dcrl;
   1477 		return 1;
   1478 		}
   1479 
   1480 	return 0;
   1481 	}
   1482 
   1483 /* Check CRL validity */
   1484 static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
   1485 	{
   1486 	X509 *issuer = NULL;
   1487 	EVP_PKEY *ikey = NULL;
   1488 	int ok = 0, chnum, cnum;
   1489 	cnum = ctx->error_depth;
   1490 	chnum = sk_X509_num(ctx->chain) - 1;
   1491 	/* if we have an alternative CRL issuer cert use that */
   1492 	if (ctx->current_issuer)
   1493 		issuer = ctx->current_issuer;
   1494 
   1495 	/* Else find CRL issuer: if not last certificate then issuer
   1496 	 * is next certificate in chain.
   1497 	 */
   1498 	else if (cnum < chnum)
   1499 		issuer = sk_X509_value(ctx->chain, cnum + 1);
   1500 	else
   1501 		{
   1502 		issuer = sk_X509_value(ctx->chain, chnum);
   1503 		/* If not self signed, can't check signature */
   1504 		if(!ctx->check_issued(ctx, issuer, issuer))
   1505 			{
   1506 			ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER;
   1507 			ok = ctx->verify_cb(0, ctx);
   1508 			if(!ok) goto err;
   1509 			}
   1510 		}
   1511 
   1512 	if(issuer)
   1513 		{
   1514 		/* Skip most tests for deltas because they have already
   1515 		 * been done
   1516 		 */
   1517 		if (!crl->base_crl_number)
   1518 			{
   1519 			/* Check for cRLSign bit if keyUsage present */
   1520 			if ((issuer->ex_flags & EXFLAG_KUSAGE) &&
   1521 				!(issuer->ex_kusage & KU_CRL_SIGN))
   1522 				{
   1523 				ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN;
   1524 				ok = ctx->verify_cb(0, ctx);
   1525 				if(!ok) goto err;
   1526 				}
   1527 
   1528 			if (!(ctx->current_crl_score & CRL_SCORE_SCOPE))
   1529 				{
   1530 				ctx->error = X509_V_ERR_DIFFERENT_CRL_SCOPE;
   1531 				ok = ctx->verify_cb(0, ctx);
   1532 				if(!ok) goto err;
   1533 				}
   1534 
   1535 			if (!(ctx->current_crl_score & CRL_SCORE_SAME_PATH))
   1536 				{
   1537 				if (check_crl_path(ctx, ctx->current_issuer) <= 0)
   1538 					{
   1539 					ctx->error = X509_V_ERR_CRL_PATH_VALIDATION_ERROR;
   1540 					ok = ctx->verify_cb(0, ctx);
   1541 					if(!ok) goto err;
   1542 					}
   1543 				}
   1544 
   1545 			if (crl->idp_flags & IDP_INVALID)
   1546 				{
   1547 				ctx->error = X509_V_ERR_INVALID_EXTENSION;
   1548 				ok = ctx->verify_cb(0, ctx);
   1549 				if(!ok) goto err;
   1550 				}
   1551 
   1552 
   1553 			}
   1554 
   1555 		if (!(ctx->current_crl_score & CRL_SCORE_TIME))
   1556 			{
   1557 			ok = check_crl_time(ctx, crl, 1);
   1558 			if (!ok)
   1559 				goto err;
   1560 			}
   1561 
   1562 		/* Attempt to get issuer certificate public key */
   1563 		ikey = X509_get_pubkey(issuer);
   1564 
   1565 		if(!ikey)
   1566 			{
   1567 			ctx->error=X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
   1568 			ok = ctx->verify_cb(0, ctx);
   1569 			if (!ok) goto err;
   1570 			}
   1571 		else
   1572 			{
   1573 			int rv;
   1574 			rv = X509_CRL_check_suiteb(crl, ikey, ctx->param->flags);
   1575 			if (rv != X509_V_OK)
   1576 				{
   1577 				ctx->error=rv;
   1578 				ok = ctx->verify_cb(0, ctx);
   1579 				if (!ok)
   1580 					goto err;
   1581 				}
   1582 			/* Verify CRL signature */
   1583 			if(X509_CRL_verify(crl, ikey) <= 0)
   1584 				{
   1585 				ctx->error=X509_V_ERR_CRL_SIGNATURE_FAILURE;
   1586 				ok = ctx->verify_cb(0, ctx);
   1587 				if (!ok) goto err;
   1588 				}
   1589 			}
   1590 		}
   1591 
   1592 	ok = 1;
   1593 
   1594 	err:
   1595 	EVP_PKEY_free(ikey);
   1596 	return ok;
   1597 	}
   1598 
   1599 /* Check certificate against CRL */
   1600 static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x)
   1601 	{
   1602 	int ok;
   1603 	X509_REVOKED *rev;
   1604 	/* The rules changed for this... previously if a CRL contained
   1605 	 * unhandled critical extensions it could still be used to indicate
   1606 	 * a certificate was revoked. This has since been changed since
   1607 	 * critical extension can change the meaning of CRL entries.
   1608 	 */
   1609 	if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
   1610 		&& (crl->flags & EXFLAG_CRITICAL))
   1611 		{
   1612 		ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION;
   1613 		ok = ctx->verify_cb(0, ctx);
   1614 		if(!ok)
   1615 			return 0;
   1616 		}
   1617 	/* Look for serial number of certificate in CRL
   1618 	 * If found make sure reason is not removeFromCRL.
   1619 	 */
   1620 	if (X509_CRL_get0_by_cert(crl, &rev, x))
   1621 		{
   1622 		if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
   1623 			return 2;
   1624 		ctx->error = X509_V_ERR_CERT_REVOKED;
   1625 		ok = ctx->verify_cb(0, ctx);
   1626 		if (!ok)
   1627 			return 0;
   1628 		}
   1629 
   1630 	return 1;
   1631 	}
   1632 
   1633 static int check_policy(X509_STORE_CTX *ctx)
   1634 	{
   1635 	int ret;
   1636 	if (ctx->parent)
   1637 		return 1;
   1638 	ret = X509_policy_check(&ctx->tree, &ctx->explicit_policy, ctx->chain,
   1639 				ctx->param->policies, ctx->param->flags);
   1640 	if (ret == 0)
   1641 		{
   1642 		OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
   1643 		return 0;
   1644 		}
   1645 	/* Invalid or inconsistent extensions */
   1646 	if (ret == -1)
   1647 		{
   1648 		/* Locate certificates with bad extensions and notify
   1649 		 * callback.
   1650 		 */
   1651 		X509 *x;
   1652 		size_t i;
   1653 		for (i = 1; i < sk_X509_num(ctx->chain); i++)
   1654 			{
   1655 			x = sk_X509_value(ctx->chain, i);
   1656 			if (!(x->ex_flags & EXFLAG_INVALID_POLICY))
   1657 				continue;
   1658 			ctx->current_cert = x;
   1659 			ctx->error = X509_V_ERR_INVALID_POLICY_EXTENSION;
   1660 			if(!ctx->verify_cb(0, ctx))
   1661 				return 0;
   1662 			}
   1663 		return 1;
   1664 		}
   1665 	if (ret == -2)
   1666 		{
   1667 		ctx->current_cert = NULL;
   1668 		ctx->error = X509_V_ERR_NO_EXPLICIT_POLICY;
   1669 		return ctx->verify_cb(0, ctx);
   1670 		}
   1671 
   1672 	if (ctx->param->flags & X509_V_FLAG_NOTIFY_POLICY)
   1673 		{
   1674 		ctx->current_cert = NULL;
   1675 		ctx->error = X509_V_OK;
   1676 		if (!ctx->verify_cb(2, ctx))
   1677 			return 0;
   1678 		}
   1679 
   1680 	return 1;
   1681 	}
   1682 
   1683 static int check_cert_time(X509_STORE_CTX *ctx, X509 *x)
   1684 	{
   1685 	time_t *ptime;
   1686 	int i;
   1687 
   1688 	if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
   1689 		ptime = &ctx->param->check_time;
   1690 	else
   1691 		ptime = NULL;
   1692 
   1693 	i=X509_cmp_time(X509_get_notBefore(x), ptime);
   1694 	if (i == 0)
   1695 		{
   1696 		ctx->error=X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
   1697 		ctx->current_cert=x;
   1698 		if (!ctx->verify_cb(0, ctx))
   1699 			return 0;
   1700 		}
   1701 
   1702 	if (i > 0)
   1703 		{
   1704 		ctx->error=X509_V_ERR_CERT_NOT_YET_VALID;
   1705 		ctx->current_cert=x;
   1706 		if (!ctx->verify_cb(0, ctx))
   1707 			return 0;
   1708 		}
   1709 
   1710 	i=X509_cmp_time(X509_get_notAfter(x), ptime);
   1711 	if (i == 0)
   1712 		{
   1713 		ctx->error=X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
   1714 		ctx->current_cert=x;
   1715 		if (!ctx->verify_cb(0, ctx))
   1716 			return 0;
   1717 		}
   1718 
   1719 	if (i < 0)
   1720 		{
   1721 		ctx->error=X509_V_ERR_CERT_HAS_EXPIRED;
   1722 		ctx->current_cert=x;
   1723 		if (!ctx->verify_cb(0, ctx))
   1724 			return 0;
   1725 		}
   1726 
   1727 	return 1;
   1728 	}
   1729 
   1730 static int internal_verify(X509_STORE_CTX *ctx)
   1731 	{
   1732 	int ok=0,n;
   1733 	X509 *xs,*xi;
   1734 	EVP_PKEY *pkey=NULL;
   1735 	int (*cb)(int xok,X509_STORE_CTX *xctx);
   1736 
   1737 	cb=ctx->verify_cb;
   1738 
   1739 	n=sk_X509_num(ctx->chain);
   1740 	ctx->error_depth=n-1;
   1741 	n--;
   1742 	xi=sk_X509_value(ctx->chain,n);
   1743 
   1744 	if (ctx->check_issued(ctx, xi, xi))
   1745 		xs=xi;
   1746 	else
   1747 		{
   1748 		if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN)
   1749 			{
   1750 			xs = xi;
   1751 			goto check_cert;
   1752 			}
   1753 		if (n <= 0)
   1754 			{
   1755 			ctx->error=X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
   1756 			ctx->current_cert=xi;
   1757 			ok=cb(0,ctx);
   1758 			goto end;
   1759 			}
   1760 		else
   1761 			{
   1762 			n--;
   1763 			ctx->error_depth=n;
   1764 			xs=sk_X509_value(ctx->chain,n);
   1765 			}
   1766 		}
   1767 
   1768 /*	ctx->error=0;  not needed */
   1769 	while (n >= 0)
   1770 		{
   1771 		ctx->error_depth=n;
   1772 
   1773 		/* Skip signature check for self signed certificates unless
   1774 		 * explicitly asked for. It doesn't add any security and
   1775 		 * just wastes time.
   1776 		 */
   1777 		if (!xs->valid && (xs != xi || (ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE)))
   1778 			{
   1779 			if ((pkey=X509_get_pubkey(xi)) == NULL)
   1780 				{
   1781 				ctx->error=X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
   1782 				ctx->current_cert=xi;
   1783 				ok=(*cb)(0,ctx);
   1784 				if (!ok) goto end;
   1785 				}
   1786 			else if (X509_verify(xs,pkey) <= 0)
   1787 				{
   1788 				ctx->error=X509_V_ERR_CERT_SIGNATURE_FAILURE;
   1789 				ctx->current_cert=xs;
   1790 				ok=(*cb)(0,ctx);
   1791 				if (!ok)
   1792 					{
   1793 					EVP_PKEY_free(pkey);
   1794 					goto end;
   1795 					}
   1796 				}
   1797 			EVP_PKEY_free(pkey);
   1798 			pkey=NULL;
   1799 			}
   1800 
   1801 		xs->valid = 1;
   1802 
   1803 		check_cert:
   1804 		ok = check_cert_time(ctx, xs);
   1805 		if (!ok)
   1806 			goto end;
   1807 
   1808 		/* The last error (if any) is still in the error value */
   1809 		ctx->current_issuer=xi;
   1810 		ctx->current_cert=xs;
   1811 		ok=(*cb)(1,ctx);
   1812 		if (!ok) goto end;
   1813 
   1814 		n--;
   1815 		if (n >= 0)
   1816 			{
   1817 			xi=xs;
   1818 			xs=sk_X509_value(ctx->chain,n);
   1819 			}
   1820 		}
   1821 	ok=1;
   1822 end:
   1823 	return ok;
   1824 	}
   1825 
   1826 int X509_cmp_current_time(const ASN1_TIME *ctm)
   1827 {
   1828 	return X509_cmp_time(ctm, NULL);
   1829 }
   1830 
   1831 int X509_cmp_time(const ASN1_TIME *ctm, time_t *cmp_time)
   1832 	{
   1833 	char *str;
   1834 	ASN1_TIME atm;
   1835 	long offset;
   1836 	char buff1[24],buff2[24],*p;
   1837 	int i, j, remaining;
   1838 
   1839 	p=buff1;
   1840 	remaining = ctm->length;
   1841 	str=(char *)ctm->data;
   1842 	/* Note that the following (historical) code allows much more slack in
   1843 	 * the time format than RFC5280. In RFC5280, the representation is
   1844 	 * fixed:
   1845 	 * UTCTime: YYMMDDHHMMSSZ
   1846 	 * GeneralizedTime: YYYYMMDDHHMMSSZ */
   1847 	if (ctm->type == V_ASN1_UTCTIME)
   1848 		{
   1849 		/* YYMMDDHHMM[SS]Z or YYMMDDHHMM[SS](+-)hhmm */
   1850 		int min_length = sizeof("YYMMDDHHMMZ") - 1;
   1851 		int max_length = sizeof("YYMMDDHHMMSS+hhmm") - 1;
   1852 		if (remaining < min_length || remaining > max_length)
   1853 			return 0;
   1854 		memcpy(p,str,10);
   1855 		p+=10;
   1856 		str+=10;
   1857 		remaining -= 10;
   1858 		}
   1859 	else
   1860 		{
   1861 		/* YYYYMMDDHHMM[SS[.fff]]Z or YYYYMMDDHHMM[SS[.f[f[f]]]](+-)hhmm */
   1862 		int min_length = sizeof("YYYYMMDDHHMMZ") - 1;
   1863 		int max_length = sizeof("YYYYMMDDHHMMSS.fff+hhmm") - 1;
   1864 		if (remaining < min_length || remaining > max_length)
   1865 			return 0;
   1866 		memcpy(p,str,12);
   1867 		p+=12;
   1868 		str+=12;
   1869 		remaining -= 12;
   1870 		}
   1871 
   1872 	if ((*str == 'Z') || (*str == '-') || (*str == '+'))
   1873 		{ *(p++)='0'; *(p++)='0'; }
   1874 	else
   1875 		{
   1876 		/* SS (seconds) */
   1877 		if (remaining < 2)
   1878 			return 0;
   1879 		*(p++)= *(str++);
   1880 		*(p++)= *(str++);
   1881 		remaining -= 2;
   1882 		/* Skip any (up to three) fractional seconds...
   1883 		 * TODO(emilia): in RFC5280, fractional seconds are forbidden.
   1884 		 * Can we just kill them altogether? */
   1885 		if (remaining && *str == '.')
   1886 			{
   1887 			str++;
   1888 			remaining--;
   1889 			for (i = 0; i < 3 && remaining; i++, str++, remaining--)
   1890 				{
   1891 				if (*str < '0' || *str > '9')
   1892 					break;
   1893 				}
   1894 			}
   1895 
   1896 		}
   1897 	*(p++)='Z';
   1898 	*(p++)='\0';
   1899 
   1900 	/* We now need either a terminating 'Z' or an offset. */
   1901 	if (!remaining)
   1902 		return 0;
   1903 	if (*str == 'Z')
   1904 		{
   1905 		if (remaining != 1)
   1906 			return 0;
   1907 		offset=0;
   1908 		}
   1909 	else
   1910 		{
   1911 		/* (+-)HHMM */
   1912 		if ((*str != '+') && (*str != '-'))
   1913 			return 0;
   1914 		/* Historical behaviour: the (+-)hhmm offset is forbidden in RFC5280. */
   1915 		if (remaining != 5)
   1916 			return 0;
   1917 		if (str[1] < '0' || str[1] > '9' || str[2] < '0' || str[2] > '9' ||
   1918 			str[3] < '0' || str[3] > '9' || str[4] < '0' || str[4] > '9')
   1919 			return 0;
   1920 		offset=((str[1]-'0')*10+(str[2]-'0'))*60;
   1921 		offset+=(str[3]-'0')*10+(str[4]-'0');
   1922 		if (*str == '-')
   1923 			offset= -offset;
   1924 		}
   1925 	atm.type=ctm->type;
   1926 	atm.flags = 0;
   1927 	atm.length=sizeof(buff2);
   1928 	atm.data=(unsigned char *)buff2;
   1929 
   1930 	if (X509_time_adj(&atm, offset*60, cmp_time) == NULL)
   1931 		return 0;
   1932 
   1933 	if (ctm->type == V_ASN1_UTCTIME)
   1934 		{
   1935 		i=(buff1[0]-'0')*10+(buff1[1]-'0');
   1936 		if (i < 50) i+=100; /* cf. RFC 2459 */
   1937 		j=(buff2[0]-'0')*10+(buff2[1]-'0');
   1938 		if (j < 50) j+=100;
   1939 
   1940 		if (i < j) return -1;
   1941 		if (i > j) return 1;
   1942 		}
   1943 	i=strcmp(buff1,buff2);
   1944 	if (i == 0) /* wait a second then return younger :-) */
   1945 		return -1;
   1946 	else
   1947 		return i;
   1948 	}
   1949 
   1950 ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long adj)
   1951 {
   1952 	return X509_time_adj(s, adj, NULL);
   1953 }
   1954 
   1955 ASN1_TIME *X509_time_adj(ASN1_TIME *s, long offset_sec, time_t *in_tm)
   1956 	{
   1957 	return X509_time_adj_ex(s, 0, offset_sec, in_tm);
   1958 	}
   1959 
   1960 ASN1_TIME *X509_time_adj_ex(ASN1_TIME *s,
   1961 				int offset_day, long offset_sec, time_t *in_tm)
   1962 	{
   1963 	time_t t = 0;
   1964 
   1965 	if (in_tm) t = *in_tm;
   1966 	else time(&t);
   1967 
   1968 	if (s && !(s->flags & ASN1_STRING_FLAG_MSTRING))
   1969 		{
   1970 		if (s->type == V_ASN1_UTCTIME)
   1971 			return ASN1_UTCTIME_adj(s,t, offset_day, offset_sec);
   1972 		if (s->type == V_ASN1_GENERALIZEDTIME)
   1973 			return ASN1_GENERALIZEDTIME_adj(s, t, offset_day,
   1974 								offset_sec);
   1975 		}
   1976 	return ASN1_TIME_adj(s, t, offset_day, offset_sec);
   1977 	}
   1978 
   1979 /* Make a delta CRL as the diff between two full CRLs */
   1980 
   1981 X509_CRL *X509_CRL_diff(X509_CRL *base, X509_CRL *newer,
   1982 			EVP_PKEY *skey, const EVP_MD *md, unsigned int flags)
   1983 	{
   1984 	X509_CRL *crl = NULL;
   1985 	int i;
   1986 	size_t j;
   1987 	STACK_OF(X509_REVOKED) *revs = NULL;
   1988 	/* CRLs can't be delta already */
   1989 	if (base->base_crl_number || newer->base_crl_number)
   1990 			{
   1991 			OPENSSL_PUT_ERROR(X509, X509_R_CRL_ALREADY_DELTA);
   1992 			return NULL;
   1993 			}
   1994 	/* Base and new CRL must have a CRL number */
   1995 	if (!base->crl_number || !newer->crl_number)
   1996 			{
   1997 			OPENSSL_PUT_ERROR(X509, X509_R_NO_CRL_NUMBER);
   1998 			return NULL;
   1999 			}
   2000 	/* Issuer names must match */
   2001 	if (X509_NAME_cmp(X509_CRL_get_issuer(base),
   2002 				X509_CRL_get_issuer(newer)))
   2003 			{
   2004 			OPENSSL_PUT_ERROR(X509, X509_R_ISSUER_MISMATCH);
   2005 			return NULL;
   2006 			}
   2007 	/* AKID and IDP must match */
   2008 	if (!crl_extension_match(base, newer, NID_authority_key_identifier))
   2009 			{
   2010 			OPENSSL_PUT_ERROR(X509, X509_R_AKID_MISMATCH);
   2011 			return NULL;
   2012 			}
   2013 	if (!crl_extension_match(base, newer, NID_issuing_distribution_point))
   2014 			{
   2015 			OPENSSL_PUT_ERROR(X509, X509_R_IDP_MISMATCH);
   2016 			return NULL;
   2017 			}
   2018 	/* Newer CRL number must exceed full CRL number */
   2019 	if (ASN1_INTEGER_cmp(newer->crl_number, base->crl_number) <= 0)
   2020 			{
   2021 			OPENSSL_PUT_ERROR(X509, X509_R_NEWER_CRL_NOT_NEWER);
   2022 			return NULL;
   2023 			}
   2024 	/* CRLs must verify */
   2025 	if (skey && (X509_CRL_verify(base, skey) <= 0 ||
   2026 			X509_CRL_verify(newer, skey) <= 0))
   2027 		{
   2028 		OPENSSL_PUT_ERROR(X509, X509_R_CRL_VERIFY_FAILURE);
   2029 		return NULL;
   2030 		}
   2031 	/* Create new CRL */
   2032 	crl = X509_CRL_new();
   2033 	if (!crl || !X509_CRL_set_version(crl, 1))
   2034 		goto memerr;
   2035 	/* Set issuer name */
   2036 	if (!X509_CRL_set_issuer_name(crl, X509_CRL_get_issuer(newer)))
   2037 		goto memerr;
   2038 
   2039 	if (!X509_CRL_set_lastUpdate(crl, X509_CRL_get_lastUpdate(newer)))
   2040 		goto memerr;
   2041 	if (!X509_CRL_set_nextUpdate(crl, X509_CRL_get_nextUpdate(newer)))
   2042 		goto memerr;
   2043 
   2044 	/* Set base CRL number: must be critical */
   2045 
   2046 	if (!X509_CRL_add1_ext_i2d(crl, NID_delta_crl, base->crl_number, 1, 0))
   2047 		goto memerr;
   2048 
   2049 	/* Copy extensions across from newest CRL to delta: this will set
   2050 	 * CRL number to correct value too.
   2051 	 */
   2052 
   2053 	for (i = 0; i < X509_CRL_get_ext_count(newer); i++)
   2054 		{
   2055 		X509_EXTENSION *ext;
   2056 		ext = X509_CRL_get_ext(newer, i);
   2057 		if (!X509_CRL_add_ext(crl, ext, -1))
   2058 			goto memerr;
   2059 		}
   2060 
   2061 	/* Go through revoked entries, copying as needed */
   2062 
   2063 	revs = X509_CRL_get_REVOKED(newer);
   2064 
   2065 	for (j = 0; j < sk_X509_REVOKED_num(revs); j++)
   2066 		{
   2067 		X509_REVOKED *rvn, *rvtmp;
   2068 		rvn = sk_X509_REVOKED_value(revs, j);
   2069 		/* Add only if not also in base.
   2070 		 * TODO: need something cleverer here for some more complex
   2071 		 * CRLs covering multiple CAs.
   2072 		 */
   2073 		if (!X509_CRL_get0_by_serial(base, &rvtmp, rvn->serialNumber))
   2074 			{
   2075 			rvtmp = X509_REVOKED_dup(rvn);
   2076 			if (!rvtmp)
   2077 				goto memerr;
   2078 			if (!X509_CRL_add0_revoked(crl, rvtmp))
   2079 				{
   2080 				X509_REVOKED_free(rvtmp);
   2081 				goto memerr;
   2082 				}
   2083 			}
   2084 		}
   2085 	/* TODO: optionally prune deleted entries */
   2086 
   2087 	if (skey && md && !X509_CRL_sign(crl, skey, md))
   2088 		goto memerr;
   2089 
   2090 	return crl;
   2091 
   2092 	memerr:
   2093 	OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
   2094 	if (crl)
   2095 		X509_CRL_free(crl);
   2096 	return NULL;
   2097 	}
   2098 
   2099 int X509_STORE_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
   2100 	     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
   2101 	{
   2102 	/* This function is (usually) called only once, by
   2103 	 * SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c). */
   2104 	int index;
   2105 	if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp,
   2106 			dup_func, free_func))
   2107 		{
   2108 		return -1;
   2109 		}
   2110 	return index;
   2111 	}
   2112 
   2113 int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data)
   2114 	{
   2115 	return CRYPTO_set_ex_data(&ctx->ex_data,idx,data);
   2116 	}
   2117 
   2118 void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx)
   2119 	{
   2120 	return CRYPTO_get_ex_data(&ctx->ex_data,idx);
   2121 	}
   2122 
   2123 int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx)
   2124 	{
   2125 	return ctx->error;
   2126 	}
   2127 
   2128 void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err)
   2129 	{
   2130 	ctx->error=err;
   2131 	}
   2132 
   2133 int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx)
   2134 	{
   2135 	return ctx->error_depth;
   2136 	}
   2137 
   2138 X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx)
   2139 	{
   2140 	return ctx->current_cert;
   2141 	}
   2142 
   2143 STACK_OF(X509) *X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx)
   2144 	{
   2145 	return ctx->chain;
   2146 	}
   2147 
   2148 STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx)
   2149 	{
   2150 	if (!ctx->chain)
   2151 		return NULL;
   2152 	return X509_chain_up_ref(ctx->chain);
   2153 	}
   2154 
   2155 X509 *X509_STORE_CTX_get0_current_issuer(X509_STORE_CTX *ctx)
   2156 	{
   2157 	return ctx->current_issuer;
   2158 	}
   2159 
   2160 X509_CRL *X509_STORE_CTX_get0_current_crl(X509_STORE_CTX *ctx)
   2161 	{
   2162 	return ctx->current_crl;
   2163 	}
   2164 
   2165 X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(X509_STORE_CTX *ctx)
   2166 	{
   2167 	return ctx->parent;
   2168 	}
   2169 
   2170 void X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x)
   2171 	{
   2172 	ctx->cert=x;
   2173 	}
   2174 
   2175 void X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
   2176 	{
   2177 	ctx->untrusted=sk;
   2178 	}
   2179 
   2180 void X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk)
   2181 	{
   2182 	ctx->crls=sk;
   2183 	}
   2184 
   2185 int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose)
   2186 	{
   2187 	return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0);
   2188 	}
   2189 
   2190 int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust)
   2191 	{
   2192 	return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust);
   2193 	}
   2194 
   2195 /* This function is used to set the X509_STORE_CTX purpose and trust
   2196  * values. This is intended to be used when another structure has its
   2197  * own trust and purpose values which (if set) will be inherited by
   2198  * the ctx. If they aren't set then we will usually have a default
   2199  * purpose in mind which should then be used to set the trust value.
   2200  * An example of this is SSL use: an SSL structure will have its own
   2201  * purpose and trust settings which the application can set: if they
   2202  * aren't set then we use the default of SSL client/server.
   2203  */
   2204 
   2205 int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,
   2206 				int purpose, int trust)
   2207 {
   2208 	int idx;
   2209 	/* If purpose not set use default */
   2210 	if (!purpose) purpose = def_purpose;
   2211 	/* If we have a purpose then check it is valid */
   2212 	if (purpose)
   2213 		{
   2214 		X509_PURPOSE *ptmp;
   2215 		idx = X509_PURPOSE_get_by_id(purpose);
   2216 		if (idx == -1)
   2217 			{
   2218 			OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_PURPOSE_ID);
   2219 			return 0;
   2220 			}
   2221 		ptmp = X509_PURPOSE_get0(idx);
   2222 		if (ptmp->trust == X509_TRUST_DEFAULT)
   2223 			{
   2224 			idx = X509_PURPOSE_get_by_id(def_purpose);
   2225 			if (idx == -1)
   2226 				{
   2227 				OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_PURPOSE_ID);
   2228 				return 0;
   2229 				}
   2230 			ptmp = X509_PURPOSE_get0(idx);
   2231 			}
   2232 		/* If trust not set then get from purpose default */
   2233 		if (!trust) trust = ptmp->trust;
   2234 		}
   2235 	if (trust)
   2236 		{
   2237 		idx = X509_TRUST_get_by_id(trust);
   2238 		if (idx == -1)
   2239 			{
   2240 			OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_TRUST_ID);
   2241 			return 0;
   2242 			}
   2243 		}
   2244 
   2245 	if (purpose && !ctx->param->purpose) ctx->param->purpose = purpose;
   2246 	if (trust && !ctx->param->trust) ctx->param->trust = trust;
   2247 	return 1;
   2248 }
   2249 
   2250 X509_STORE_CTX *X509_STORE_CTX_new(void)
   2251 {
   2252 	X509_STORE_CTX *ctx;
   2253 	ctx = (X509_STORE_CTX *)OPENSSL_malloc(sizeof(X509_STORE_CTX));
   2254 	if (!ctx)
   2255 		{
   2256 		OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
   2257 		return NULL;
   2258 		}
   2259 	memset(ctx, 0, sizeof(X509_STORE_CTX));
   2260 	return ctx;
   2261 }
   2262 
   2263 void X509_STORE_CTX_free(X509_STORE_CTX *ctx)
   2264 {
   2265 	X509_STORE_CTX_cleanup(ctx);
   2266 	OPENSSL_free(ctx);
   2267 }
   2268 
   2269 int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
   2270 	     STACK_OF(X509) *chain)
   2271 	{
   2272 	int ret = 1;
   2273 
   2274 	memset(ctx, 0, sizeof(X509_STORE_CTX));
   2275 	ctx->ctx=store;
   2276 	ctx->cert=x509;
   2277 	ctx->untrusted=chain;
   2278 
   2279 	CRYPTO_new_ex_data(&ctx->ex_data);
   2280 
   2281 	ctx->param = X509_VERIFY_PARAM_new();
   2282 	if (!ctx->param)
   2283 		goto err;
   2284 
   2285 	/* Inherit callbacks and flags from X509_STORE if not set
   2286 	 * use defaults. */
   2287 
   2288 	if (store)
   2289 		ret = X509_VERIFY_PARAM_inherit(ctx->param, store->param);
   2290 	else
   2291 		ctx->param->inh_flags |= X509_VP_FLAG_DEFAULT|X509_VP_FLAG_ONCE;
   2292 
   2293 	if (store)
   2294 		{
   2295 		ctx->verify_cb = store->verify_cb;
   2296 		ctx->cleanup = store->cleanup;
   2297 		}
   2298 	else
   2299 		ctx->cleanup = 0;
   2300 
   2301 	if (ret)
   2302 		ret = X509_VERIFY_PARAM_inherit(ctx->param,
   2303 					X509_VERIFY_PARAM_lookup("default"));
   2304 
   2305 	if (ret == 0)
   2306 		goto err;
   2307 
   2308 	if (store && store->check_issued)
   2309 		ctx->check_issued = store->check_issued;
   2310 	else
   2311 		ctx->check_issued = check_issued;
   2312 
   2313 	if (store && store->get_issuer)
   2314 		ctx->get_issuer = store->get_issuer;
   2315 	else
   2316 		ctx->get_issuer = X509_STORE_CTX_get1_issuer;
   2317 
   2318 	if (store && store->verify_cb)
   2319 		ctx->verify_cb = store->verify_cb;
   2320 	else
   2321 		ctx->verify_cb = null_callback;
   2322 
   2323 	if (store && store->verify)
   2324 		ctx->verify = store->verify;
   2325 	else
   2326 		ctx->verify = internal_verify;
   2327 
   2328 	if (store && store->check_revocation)
   2329 		ctx->check_revocation = store->check_revocation;
   2330 	else
   2331 		ctx->check_revocation = check_revocation;
   2332 
   2333 	if (store && store->get_crl)
   2334 		ctx->get_crl = store->get_crl;
   2335 	else
   2336 		ctx->get_crl = NULL;
   2337 
   2338 	if (store && store->check_crl)
   2339 		ctx->check_crl = store->check_crl;
   2340 	else
   2341 		ctx->check_crl = check_crl;
   2342 
   2343 	if (store && store->cert_crl)
   2344 		ctx->cert_crl = store->cert_crl;
   2345 	else
   2346 		ctx->cert_crl = cert_crl;
   2347 
   2348 	if (store && store->lookup_certs)
   2349 		ctx->lookup_certs = store->lookup_certs;
   2350 	else
   2351 		ctx->lookup_certs = X509_STORE_get1_certs;
   2352 
   2353 	if (store && store->lookup_crls)
   2354 		ctx->lookup_crls = store->lookup_crls;
   2355 	else
   2356 		ctx->lookup_crls = X509_STORE_get1_crls;
   2357 
   2358 	ctx->check_policy = check_policy;
   2359 
   2360 	return 1;
   2361 
   2362 err:
   2363 	CRYPTO_free_ex_data(&g_ex_data_class, ctx, &ctx->ex_data);
   2364 	if (ctx->param != NULL)
   2365 		{
   2366 		X509_VERIFY_PARAM_free(ctx->param);
   2367 		}
   2368 
   2369 	memset(ctx, 0, sizeof(X509_STORE_CTX));
   2370 	OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
   2371 	return 0;
   2372 	}
   2373 
   2374 /* Set alternative lookup method: just a STACK of trusted certificates.
   2375  * This avoids X509_STORE nastiness where it isn't needed.
   2376  */
   2377 
   2378 void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
   2379 {
   2380 	ctx->other_ctx = sk;
   2381 	ctx->get_issuer = get_issuer_sk;
   2382 }
   2383 
   2384 void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx)
   2385 	{
   2386 	if (ctx->cleanup) ctx->cleanup(ctx);
   2387 	if (ctx->param != NULL)
   2388 		{
   2389 		if (ctx->parent == NULL)
   2390 			X509_VERIFY_PARAM_free(ctx->param);
   2391 		ctx->param=NULL;
   2392 		}
   2393 	if (ctx->tree != NULL)
   2394 		{
   2395 		X509_policy_tree_free(ctx->tree);
   2396 		ctx->tree=NULL;
   2397 		}
   2398 	if (ctx->chain != NULL)
   2399 		{
   2400 		sk_X509_pop_free(ctx->chain,X509_free);
   2401 		ctx->chain=NULL;
   2402 		}
   2403 	CRYPTO_free_ex_data(&g_ex_data_class, ctx, &(ctx->ex_data));
   2404 	memset(&ctx->ex_data,0,sizeof(CRYPTO_EX_DATA));
   2405 	}
   2406 
   2407 void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth)
   2408 	{
   2409 	X509_VERIFY_PARAM_set_depth(ctx->param, depth);
   2410 	}
   2411 
   2412 void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags)
   2413 	{
   2414 	X509_VERIFY_PARAM_set_flags(ctx->param, flags);
   2415 	}
   2416 
   2417 void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags, time_t t)
   2418 	{
   2419 	X509_VERIFY_PARAM_set_time(ctx->param, t);
   2420 	}
   2421 
   2422 void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
   2423 				  int (*verify_cb)(int, X509_STORE_CTX *))
   2424 	{
   2425 	ctx->verify_cb=verify_cb;
   2426 	}
   2427 
   2428 X509_POLICY_TREE *X509_STORE_CTX_get0_policy_tree(X509_STORE_CTX *ctx)
   2429 	{
   2430 	return ctx->tree;
   2431 	}
   2432 
   2433 int X509_STORE_CTX_get_explicit_policy(X509_STORE_CTX *ctx)
   2434 	{
   2435 	return ctx->explicit_policy;
   2436 	}
   2437 
   2438 int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name)
   2439 	{
   2440 	const X509_VERIFY_PARAM *param;
   2441 	param = X509_VERIFY_PARAM_lookup(name);
   2442 	if (!param)
   2443 		return 0;
   2444 	return X509_VERIFY_PARAM_inherit(ctx->param, param);
   2445 	}
   2446 
   2447 X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(X509_STORE_CTX *ctx)
   2448 	{
   2449 	return ctx->param;
   2450 	}
   2451 
   2452 void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param)
   2453 	{
   2454 	if (ctx->param)
   2455 		X509_VERIFY_PARAM_free(ctx->param);
   2456 	ctx->param = param;
   2457 	}
   2458 
   2459 IMPLEMENT_ASN1_SET_OF(X509)
   2460 IMPLEMENT_ASN1_SET_OF(X509_ATTRIBUTE)
   2461