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 __RCSID("$MirOS: src/lib/libssl/src/crypto/x509/x509_vfy.c,v 1.5 2011/11/20 23:39:46 tg Exp $");
74 
75 static int null_callback(int ok,X509_STORE_CTX *e);
76 static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer);
77 static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x);
78 static int check_chain_extensions(X509_STORE_CTX *ctx);
79 static int check_trust(X509_STORE_CTX *ctx);
80 static int check_revocation(X509_STORE_CTX *ctx);
81 static int check_cert(X509_STORE_CTX *ctx);
82 static int check_ca_blacklist(X509_STORE_CTX *ctx);
83 static int internal_verify(X509_STORE_CTX *ctx);
84 const char X509_version[]="X.509" OPENSSL_VERSION_PTEXT;
85 
86 
null_callback(int ok,X509_STORE_CTX * e)87 static int null_callback(int ok, X509_STORE_CTX *e)
88 	{
89 	return ok;
90 	}
91 
92 #if 0
93 static int x509_subject_cmp(X509 **a, X509 **b)
94 	{
95 	return X509_subject_name_cmp(*a,*b);
96 	}
97 #endif
98 
X509_verify_cert(X509_STORE_CTX * ctx)99 int X509_verify_cert(X509_STORE_CTX *ctx)
100 	{
101 	X509 *x,*xtmp,*chain_ss=NULL;
102 	X509_NAME *xn;
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->verify_cb;
115 
116 	/* first we make sure the chain we are going to build is
117 	 * present and that the first entry is in place */
118 	if (ctx->chain == NULL)
119 		{
120 		if (	((ctx->chain=sk_X509_new_null()) == NULL) ||
121 			(!sk_X509_push(ctx->chain,ctx->cert)))
122 			{
123 			X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
124 			goto end;
125 			}
126 		CRYPTO_add(&ctx->cert->references,1,CRYPTO_LOCK_X509);
127 		ctx->last_untrusted=1;
128 		}
129 
130 	/* We use a temporary STACK so we can chop and hack at it */
131 	if (ctx->untrusted != NULL
132 	    && (sktmp=sk_X509_dup(ctx->untrusted)) == NULL)
133 		{
134 		X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
135 		goto end;
136 		}
137 
138 	num=sk_X509_num(ctx->chain);
139 	x=sk_X509_value(ctx->chain,num-1);
140 	depth=ctx->depth;
141 
142 
143 	for (;;)
144 		{
145 		/* If we have enough, we break */
146 		if (depth < num) break; /* FIXME: If this happens, we should take
147 		                         * note of it and, if appropriate, use the
148 		                         * X509_V_ERR_CERT_CHAIN_TOO_LONG error
149 		                         * code later.
150 		                         */
151 
152 		/* If we are self signed, we break */
153 		xn=X509_get_issuer_name(x);
154 		if (ctx->check_issued(ctx, x,x)) break;
155 
156 		/* If we were passed a cert chain, use it first */
157 		if (ctx->untrusted != NULL)
158 			{
159 			xtmp=find_issuer(ctx, sktmp,x);
160 			if (xtmp != NULL)
161 				{
162 				if (!sk_X509_push(ctx->chain,xtmp))
163 					{
164 					X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
165 					goto end;
166 					}
167 				CRYPTO_add(&xtmp->references,1,CRYPTO_LOCK_X509);
168 				sk_X509_delete_ptr(sktmp,xtmp);
169 				ctx->last_untrusted++;
170 				x=xtmp;
171 				num++;
172 				/* reparse the full chain for
173 				 * the next one */
174 				continue;
175 				}
176 			}
177 		break;
178 		}
179 
180 	/* at this point, chain should contain a list of untrusted
181 	 * certificates.  We now need to add at least one trusted one,
182 	 * if possible, otherwise we complain. */
183 
184 	/* Examine last certificate in chain and see if it
185  	 * is self signed.
186  	 */
187 
188 	i=sk_X509_num(ctx->chain);
189 	x=sk_X509_value(ctx->chain,i-1);
190 	xn = X509_get_subject_name(x);
191 	if (ctx->check_issued(ctx, x, x))
192 		{
193 		/* we have a self signed certificate */
194 		if (sk_X509_num(ctx->chain) == 1)
195 			{
196 			/* We have a single self signed certificate: see if
197 			 * we can find it in the store. We must have an exact
198 			 * match to avoid possible impersonation.
199 			 */
200 			ok = ctx->get_issuer(&xtmp, ctx, x);
201 			if ((ok <= 0) || X509_cmp(x, xtmp))
202 				{
203 				ctx->error=X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
204 				ctx->current_cert=x;
205 				ctx->error_depth=i-1;
206 				if (ok == 1) X509_free(xtmp);
207 				ok=cb(0,ctx);
208 				if (!ok) goto end;
209 				}
210 			else
211 				{
212 				/* We have a match: replace certificate with store version
213 				 * so we get any trust settings.
214 				 */
215 				X509_free(x);
216 				x = xtmp;
217 				sk_X509_set(ctx->chain, i - 1, x);
218 				ctx->last_untrusted=0;
219 				}
220 			}
221 		else
222 			{
223 			/* extract and save self signed certificate for later use */
224 			chain_ss=sk_X509_pop(ctx->chain);
225 			ctx->last_untrusted--;
226 			num--;
227 			x=sk_X509_value(ctx->chain,num-1);
228 			}
229 		}
230 
231 	/* We now lookup certs from the certificate store */
232 	for (;;)
233 		{
234 		/* If we have enough, we break */
235 		if (depth < num) break;
236 
237 		/* If we are self signed, we break */
238 		xn=X509_get_issuer_name(x);
239 		if (ctx->check_issued(ctx,x,x)) break;
240 
241 		ok = ctx->get_issuer(&xtmp, ctx, x);
242 
243 		if (ok < 0) return ok;
244 		if (ok == 0) break;
245 
246 		x = xtmp;
247 		if (!sk_X509_push(ctx->chain,x))
248 			{
249 			X509_free(xtmp);
250 			X509err(X509_F_X509_VERIFY_CERT,ERR_R_MALLOC_FAILURE);
251 			return 0;
252 			}
253 		num++;
254 		}
255 
256 	/* we now have our chain, lets check it... */
257 	xn=X509_get_issuer_name(x);
258 
259 	/* Is last certificate looked up self signed? */
260 	if (!ctx->check_issued(ctx,x,x))
261 		{
262 		if ((chain_ss == NULL) || !ctx->check_issued(ctx, x, chain_ss))
263 			{
264 			if (ctx->last_untrusted >= num)
265 				ctx->error=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
266 			else
267 				ctx->error=X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;
268 			ctx->current_cert=x;
269 			}
270 		else
271 			{
272 
273 			sk_X509_push(ctx->chain,chain_ss);
274 			num++;
275 			ctx->last_untrusted=num;
276 			ctx->current_cert=chain_ss;
277 			ctx->error=X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
278 			chain_ss=NULL;
279 			}
280 
281 		ctx->error_depth=num-1;
282 		ok=cb(0,ctx);
283 		if (!ok) goto end;
284 		}
285 
286 	/* We have the chain complete: now we need to check its purpose */
287 	ok = check_chain_extensions(ctx);
288 
289 	if (!ok) goto end;
290 
291 	/* The chain extensions are OK: check trust */
292 
293 	if (ctx->trust > 0) ok = check_trust(ctx);
294 
295 	if (!ok) goto end;
296 
297 	/* We may as well copy down any DSA parameters that are required */
298 	X509_get_pubkey_parameters(NULL,ctx->chain);
299 
300 	/* Check revocation status: we do this after copying parameters
301 	 * because they may be needed for CRL signature verification.
302 	 */
303 
304 	ok = ctx->check_revocation(ctx);
305 	if(!ok) goto end;
306 
307 	/* At this point, we have a chain and just need to verify it */
308 	if (ctx->verify != NULL)
309 		ok=ctx->verify(ctx);
310 	else
311 		ok=internal_verify(ctx);
312 
313 	if(!ok) goto end;
314 	ok = check_ca_blacklist(ctx);
315 	if(!ok) goto end;
316 
317 	if (0)
318 		{
319 end:
320 		X509_get_pubkey_parameters(NULL,ctx->chain);
321 		}
322 	if (sktmp != NULL) sk_X509_free(sktmp);
323 	if (chain_ss != NULL) X509_free(chain_ss);
324 	return ok;
325 	}
326 
327 
328 /* Given a STACK_OF(X509) find the issuer of cert (if any)
329  */
330 
find_issuer(X509_STORE_CTX * ctx,STACK_OF (X509)* sk,X509 * x)331 static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x)
332 {
333 	int i;
334 	X509 *issuer;
335 	for (i = 0; i < sk_X509_num(sk); i++)
336 		{
337 		issuer = sk_X509_value(sk, i);
338 		if (ctx->check_issued(ctx, x, issuer))
339 			return issuer;
340 		}
341 	return NULL;
342 }
343 
344 /* Given a possible certificate and issuer check them */
345 
check_issued(X509_STORE_CTX * ctx,X509 * x,X509 * issuer)346 static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer)
347 {
348 	int ret;
349 	ret = X509_check_issued(issuer, x);
350 	if (ret == X509_V_OK)
351 		return 1;
352 	/* If we haven't asked for issuer errors don't set ctx */
353 	if (!(ctx->flags & X509_V_FLAG_CB_ISSUER_CHECK))
354 		return 0;
355 
356 	ctx->error = ret;
357 	ctx->current_cert = x;
358 	ctx->current_issuer = issuer;
359 	return ctx->verify_cb(0, ctx);
360 	return 0;
361 }
362 
363 /* Alternative lookup method: look from a STACK stored in other_ctx */
364 
get_issuer_sk(X509 ** issuer,X509_STORE_CTX * ctx,X509 * x)365 static int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
366 {
367 	*issuer = find_issuer(ctx, ctx->other_ctx, x);
368 	if (*issuer)
369 		{
370 		CRYPTO_add(&(*issuer)->references,1,CRYPTO_LOCK_X509);
371 		return 1;
372 		}
373 	else
374 		return 0;
375 }
376 
377 
378 /* Check a certificate chains extensions for consistency
379  * with the supplied purpose
380  */
381 
check_chain_extensions(X509_STORE_CTX * ctx)382 static int check_chain_extensions(X509_STORE_CTX *ctx)
383 {
384 #ifdef OPENSSL_NO_CHAIN_VERIFY
385 	return 1;
386 #else
387 	int i, ok=0, must_be_ca;
388 	X509 *x;
389 	int (*cb)();
390 	int proxy_path_length = 0;
391 	int allow_proxy_certs = !!(ctx->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);
392 	cb=ctx->verify_cb;
393 
394 	/* must_be_ca can have 1 of 3 values:
395 	   -1: we accept both CA and non-CA certificates, to allow direct
396 	       use of self-signed certificates (which are marked as CA).
397 	   0:  we only accept non-CA certificates.  This is currently not
398 	       used, but the possibility is present for future extensions.
399 	   1:  we only accept CA certificates.  This is currently used for
400 	       all certificates in the chain except the leaf certificate.
401 	*/
402 	must_be_ca = -1;
403 
404 	/* A hack to keep people who don't want to modify their software
405 	   happy */
406 	if (getenv("OPENSSL_ALLOW_PROXY_CERTS"))
407 		allow_proxy_certs = 1;
408 
409 	/* Check all untrusted certificates */
410 	for (i = 0; i < ctx->last_untrusted; i++)
411 		{
412 		int ret;
413 		x = sk_X509_value(ctx->chain, i);
414 		if (!(ctx->flags & X509_V_FLAG_IGNORE_CRITICAL)
415 			&& (x->ex_flags & EXFLAG_CRITICAL))
416 			{
417 			ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
418 			ctx->error_depth = i;
419 			ctx->current_cert = x;
420 			ok=cb(0,ctx);
421 			if (!ok) goto end;
422 			}
423 		if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY))
424 			{
425 			ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;
426 			ctx->error_depth = i;
427 			ctx->current_cert = x;
428 			ok=cb(0,ctx);
429 			if (!ok) goto end;
430 			}
431 		ret = X509_check_ca(x);
432 		switch(must_be_ca)
433 			{
434 		case -1:
435 			if ((ctx->flags & X509_V_FLAG_X509_STRICT)
436 				&& (ret != 1) && (ret != 0))
437 				{
438 				ret = 0;
439 				ctx->error = X509_V_ERR_INVALID_CA;
440 				}
441 			else
442 				ret = 1;
443 			break;
444 		case 0:
445 			if (ret != 0)
446 				{
447 				ret = 0;
448 				ctx->error = X509_V_ERR_INVALID_NON_CA;
449 				}
450 			else
451 				ret = 1;
452 			break;
453 		default:
454 			if ((ret == 0)
455 				|| ((ctx->flags & X509_V_FLAG_X509_STRICT)
456 					&& (ret != 1)))
457 				{
458 				ret = 0;
459 				ctx->error = X509_V_ERR_INVALID_CA;
460 				}
461 			else
462 				ret = 1;
463 			break;
464 			}
465 		if (ret == 0)
466 			{
467 			ctx->error_depth = i;
468 			ctx->current_cert = x;
469 			ok=cb(0,ctx);
470 			if (!ok) goto end;
471 			}
472 		if (ctx->purpose > 0)
473 			{
474 			ret = X509_check_purpose(x, ctx->purpose,
475 				must_be_ca > 0);
476 			if ((ret == 0)
477 				|| ((ctx->flags & X509_V_FLAG_X509_STRICT)
478 					&& (ret != 1)))
479 				{
480 				ctx->error = X509_V_ERR_INVALID_PURPOSE;
481 				ctx->error_depth = i;
482 				ctx->current_cert = x;
483 				ok=cb(0,ctx);
484 				if (!ok) goto end;
485 				}
486 			}
487 		/* Check pathlen */
488 		if ((i > 1) && (x->ex_pathlen != -1)
489 			   && (i > (x->ex_pathlen + proxy_path_length + 1)))
490 			{
491 			ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
492 			ctx->error_depth = i;
493 			ctx->current_cert = x;
494 			ok=cb(0,ctx);
495 			if (!ok) goto end;
496 			}
497 		/* If this certificate is a proxy certificate, the next
498 		   certificate must be another proxy certificate or a EE
499 		   certificate.  If not, the next certificate must be a
500 		   CA certificate.  */
501 		if (x->ex_flags & EXFLAG_PROXY)
502 			{
503 			PROXY_CERT_INFO_EXTENSION *pci =
504 				X509_get_ext_d2i(x, NID_proxyCertInfo,
505 					NULL, NULL);
506 			if (pci->pcPathLengthConstraint &&
507 				ASN1_INTEGER_get(pci->pcPathLengthConstraint)
508 				< i)
509 				{
510 				PROXY_CERT_INFO_EXTENSION_free(pci);
511 				ctx->error = X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;
512 				ctx->error_depth = i;
513 				ctx->current_cert = x;
514 				ok=cb(0,ctx);
515 				if (!ok) goto end;
516 				}
517 			PROXY_CERT_INFO_EXTENSION_free(pci);
518 			proxy_path_length++;
519 			must_be_ca = 0;
520 			}
521 		else
522 			must_be_ca = 1;
523 		}
524 	ok = 1;
525  end:
526 	return ok;
527 #endif
528 }
529 
check_trust(X509_STORE_CTX * ctx)530 static int check_trust(X509_STORE_CTX *ctx)
531 {
532 #ifdef OPENSSL_NO_CHAIN_VERIFY
533 	return 1;
534 #else
535 	int i, ok;
536 	X509 *x;
537 	int (*cb)();
538 	cb=ctx->verify_cb;
539 /* For now just check the last certificate in the chain */
540 	i = sk_X509_num(ctx->chain) - 1;
541 	x = sk_X509_value(ctx->chain, i);
542 	ok = X509_check_trust(x, ctx->trust, 0);
543 	if (ok == X509_TRUST_TRUSTED)
544 		return 1;
545 	ctx->error_depth = i;
546 	ctx->current_cert = x;
547 	if (ok == X509_TRUST_REJECTED)
548 		ctx->error = X509_V_ERR_CERT_REJECTED;
549 	else
550 		ctx->error = X509_V_ERR_CERT_UNTRUSTED;
551 	ok = cb(0, ctx);
552 	return ok;
553 #endif
554 }
555 
check_revocation(X509_STORE_CTX * ctx)556 static int check_revocation(X509_STORE_CTX *ctx)
557 	{
558 	int i, last, ok;
559 	if (!(ctx->flags & X509_V_FLAG_CRL_CHECK))
560 		return 1;
561 	if (ctx->flags & X509_V_FLAG_CRL_CHECK_ALL)
562 		last = sk_X509_num(ctx->chain) - 1;
563 	else
564 		last = 0;
565 	for(i = 0; i <= last; i++)
566 		{
567 		ctx->error_depth = i;
568 		ok = check_cert(ctx);
569 		if (!ok) return ok;
570 		}
571 	return 1;
572 	}
573 
check_cert(X509_STORE_CTX * ctx)574 static int check_cert(X509_STORE_CTX *ctx)
575 	{
576 	X509_CRL *crl = NULL;
577 	X509 *x;
578 	int ok, cnum;
579 	cnum = ctx->error_depth;
580 	x = sk_X509_value(ctx->chain, cnum);
581 	ctx->current_cert = x;
582 	/* Try to retrieve relevant CRL */
583 	ok = ctx->get_crl(ctx, &crl, x);
584 	/* If error looking up CRL, nothing we can do except
585 	 * notify callback
586 	 */
587 	if(!ok)
588 		{
589 		ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
590 		ok = ctx->verify_cb(0, ctx);
591 		goto err;
592 		}
593 	ctx->current_crl = crl;
594 	ok = ctx->check_crl(ctx, crl);
595 	if (!ok) goto err;
596 	ok = ctx->cert_crl(ctx, crl, x);
597 	err:
598 	ctx->current_crl = NULL;
599 	X509_CRL_free(crl);
600 	return ok;
601 
602 	}
603 
604 
605 #ifdef notyet
606 #include "blaklist.inc"
607 #endif
608 static int
check_ca_blacklist(X509_STORE_CTX * ctx)609 check_ca_blacklist(X509_STORE_CTX *ctx)
610 {
611 	int i, j;
612 	X509 *x;
613 /*	unsigned char curmd4[MD4_DIGEST_LENGTH];*/
614 
615 	/* Check all certificates against the blacklist */
616 	for (i = sk_X509_num(ctx->chain) - 1; i >= 0; i--) {
617 		x = sk_X509_value(ctx->chain, i);
618 		/*
619 		 * Mark certificates containing the following names as
620 		 * revoked, no matter where in the chain they are.
621 		 */
622 		if (x->name && (
623 		    strstr(x->name, "DigiNotar") ||
624 		    strstr(x->name, "Digicert Sdn. Bhd."))) {
625 			ctx->error = X509_V_ERR_CERT_DISTRUSTED_BY_NAME;
626 /* mark_as_distrusted:*/
627 			ctx->error_depth = i;
628 			ctx->current_cert = x;
629 			if (!ctx->verify_cb(0, ctx))
630 				return (0);
631 		}
632 #ifdef notyet
633 		/*
634 		 * Match against blacklist of MD4 fingerprints
635 		 */
636 /* this is broken, X509_pubkey_digest() must be used */
637 		X509_digest(x, EVP_md4(), curmd4, NULL);
638 		j = 0;
639 		while (fp_blacklist[j] != NULL) {
640 			if (!memcmp(fp_blacklist[j], curmd4, sizeof(curmd4))) {
641 				ctx->error = X509_V_ERR_CERT_DISTRUSTED_BY_HASH;
642 				goto mark_as_distrusted;
643 			}
644 			++j;
645 		}
646 #endif
647 	}
648 	return (1);
649 }
650 
651 
652 /* Retrieve CRL corresponding to certificate: currently just a
653  * subject lookup: maybe use AKID later...
654  * Also might look up any included CRLs too (e.g PKCS#7 signedData).
655  */
get_crl(X509_STORE_CTX * ctx,X509_CRL ** crl,X509 * x)656 static int get_crl(X509_STORE_CTX *ctx, X509_CRL **crl, X509 *x)
657 	{
658 	int ok;
659 	X509_OBJECT xobj;
660 	ok = X509_STORE_get_by_subject(ctx, X509_LU_CRL, X509_get_issuer_name(x), &xobj);
661 	if (!ok) return 0;
662 	*crl = xobj.data.crl;
663 	return 1;
664 	}
665 
666 /* Check CRL validity */
check_crl(X509_STORE_CTX * ctx,X509_CRL * crl)667 static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
668 	{
669 	X509 *issuer = NULL;
670 	EVP_PKEY *ikey = NULL;
671 	int ok = 0, chnum, cnum, i;
672 	time_t *ptime;
673 	cnum = ctx->error_depth;
674 	chnum = sk_X509_num(ctx->chain) - 1;
675 	/* Find CRL issuer: if not last certificate then issuer
676 	 * is next certificate in chain.
677 	 */
678 	if(cnum < chnum)
679 		issuer = sk_X509_value(ctx->chain, cnum + 1);
680 	else
681 		{
682 		issuer = sk_X509_value(ctx->chain, chnum);
683 		/* If not self signed, can't check signature */
684 		if(!ctx->check_issued(ctx, issuer, issuer))
685 			{
686 			ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER;
687 			ok = ctx->verify_cb(0, ctx);
688 			if(!ok) goto err;
689 			}
690 		}
691 
692 	if(issuer)
693 		{
694 		/* Check for cRLSign bit if keyUsage present */
695 		if ((issuer->ex_flags & EXFLAG_KUSAGE) &&
696 			!(issuer->ex_kusage & KU_CRL_SIGN))
697 			{
698 			ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN;
699 			ok = ctx->verify_cb(0, ctx);
700 			if(!ok) goto err;
701 			}
702 
703 		/* Attempt to get issuer certificate public key */
704 		ikey = X509_get_pubkey(issuer);
705 
706 		if(!ikey)
707 			{
708 			ctx->error=X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
709 			ok = ctx->verify_cb(0, ctx);
710 			if (!ok) goto err;
711 			}
712 		else
713 			{
714 			/* Verify CRL signature */
715 			if(X509_CRL_verify(crl, ikey) <= 0)
716 				{
717 				ctx->error=X509_V_ERR_CRL_SIGNATURE_FAILURE;
718 				ok = ctx->verify_cb(0, ctx);
719 				if (!ok) goto err;
720 				}
721 			}
722 		}
723 
724 	/* OK, CRL signature valid check times */
725 	if (ctx->flags & X509_V_FLAG_USE_CHECK_TIME)
726 		ptime = &ctx->check_time;
727 	else
728 		ptime = NULL;
729 
730 	i=X509_cmp_time(X509_CRL_get_lastUpdate(crl), ptime);
731 	if (i == 0)
732 		{
733 		ctx->error=X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD;
734 		ok = ctx->verify_cb(0, ctx);
735 		if (!ok) goto err;
736 		}
737 
738 	if (i > 0)
739 		{
740 		ctx->error=X509_V_ERR_CRL_NOT_YET_VALID;
741 		ok = ctx->verify_cb(0, ctx);
742 		if (!ok) goto err;
743 		}
744 
745 	if(X509_CRL_get_nextUpdate(crl))
746 		{
747 		i=X509_cmp_time(X509_CRL_get_nextUpdate(crl), ptime);
748 
749 		if (i == 0)
750 			{
751 			ctx->error=X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD;
752 			ok = ctx->verify_cb(0, ctx);
753 			if (!ok) goto err;
754 			}
755 
756 		if (i < 0)
757 			{
758 			ctx->error=X509_V_ERR_CRL_HAS_EXPIRED;
759 			ok = ctx->verify_cb(0, ctx);
760 			if (!ok) goto err;
761 			}
762 		}
763 
764 	ok = 1;
765 
766 	err:
767 	EVP_PKEY_free(ikey);
768 	return ok;
769 	}
770 
771 /* Check certificate against CRL */
cert_crl(X509_STORE_CTX * ctx,X509_CRL * crl,X509 * x)772 static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x)
773 	{
774 	int idx, ok;
775 	X509_REVOKED rtmp;
776 	STACK_OF(X509_EXTENSION) *exts;
777 	X509_EXTENSION *ext;
778 	/* Look for serial number of certificate in CRL */
779 	rtmp.serialNumber = X509_get_serialNumber(x);
780 	/* Sort revoked into serial number order if not already sorted.
781 	 * Do this under a lock to avoid race condition.
782  	 */
783 	if (!sk_X509_REVOKED_is_sorted(crl->crl->revoked))
784 		{
785 		CRYPTO_w_lock(CRYPTO_LOCK_X509_CRL);
786 		sk_X509_REVOKED_sort(crl->crl->revoked);
787 		CRYPTO_w_unlock(CRYPTO_LOCK_X509_CRL);
788 		}
789 	idx = sk_X509_REVOKED_find(crl->crl->revoked, &rtmp);
790 	/* If found assume revoked: want something cleverer than
791 	 * this to handle entry extensions in V2 CRLs.
792 	 */
793 	if(idx >= 0)
794 		{
795 		ctx->error = X509_V_ERR_CERT_REVOKED;
796 		ok = ctx->verify_cb(0, ctx);
797 		if (!ok) return 0;
798 		}
799 
800 	if (ctx->flags & X509_V_FLAG_IGNORE_CRITICAL)
801 		return 1;
802 
803 	/* See if we have any critical CRL extensions: since we
804 	 * currently don't handle any CRL extensions the CRL must be
805 	 * rejected.
806 	 * This code accesses the X509_CRL structure directly: applications
807 	 * shouldn't do this.
808 	 */
809 
810 	exts = crl->crl->extensions;
811 
812 	for (idx = 0; idx < sk_X509_EXTENSION_num(exts); idx++)
813 		{
814 		ext = sk_X509_EXTENSION_value(exts, idx);
815 		if (ext->critical > 0)
816 			{
817 			ctx->error =
818 				X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION;
819 			ok = ctx->verify_cb(0, ctx);
820 			if(!ok) return 0;
821 			break;
822 			}
823 		}
824 	return 1;
825 	}
826 
internal_verify(X509_STORE_CTX * ctx)827 static int internal_verify(X509_STORE_CTX *ctx)
828 	{
829 	int i,ok=0,n;
830 	X509 *xs,*xi;
831 	EVP_PKEY *pkey=NULL;
832 	time_t *ptime;
833 	int (*cb)();
834 
835 	cb=ctx->verify_cb;
836 
837 	n=sk_X509_num(ctx->chain);
838 	ctx->error_depth=n-1;
839 	n--;
840 	xi=sk_X509_value(ctx->chain,n);
841 	if (ctx->flags & X509_V_FLAG_USE_CHECK_TIME)
842 		ptime = &ctx->check_time;
843 	else
844 		ptime = NULL;
845 	if (ctx->check_issued(ctx, xi, xi))
846 		xs=xi;
847 	else
848 		{
849 		if (n <= 0)
850 			{
851 			ctx->error=X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
852 			ctx->current_cert=xi;
853 			ok=cb(0,ctx);
854 			goto end;
855 			}
856 		else
857 			{
858 			n--;
859 			ctx->error_depth=n;
860 			xs=sk_X509_value(ctx->chain,n);
861 			}
862 		}
863 
864 /*	ctx->error=0;  not needed */
865 	while (n >= 0)
866 		{
867 		ctx->error_depth=n;
868 
869 		/* Skip signature check for self signed certificates. It
870 		 * doesn't add any security and just wastes time.
871 		 */
872 		if (!xs->valid && xs != xi)
873 			{
874 			if ((pkey=X509_get_pubkey(xi)) == NULL)
875 				{
876 				ctx->error=X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
877 				ctx->current_cert=xi;
878 				ok=(*cb)(0,ctx);
879 				if (!ok) goto end;
880 				}
881 			else if (X509_verify(xs,pkey) <= 0)
882 				{
883 				ctx->error=X509_V_ERR_CERT_SIGNATURE_FAILURE;
884 				ctx->current_cert=xs;
885 				ok=(*cb)(0,ctx);
886 				if (!ok)
887 					{
888 					EVP_PKEY_free(pkey);
889 					goto end;
890 					}
891 				}
892 			EVP_PKEY_free(pkey);
893 			pkey=NULL;
894 
895 			i=X509_cmp_time(X509_get_notBefore(xs), ptime);
896 			if (i == 0)
897 				{
898 				ctx->error=X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
899 				ctx->current_cert=xs;
900 				ok=(*cb)(0,ctx);
901 				if (!ok) goto end;
902 				}
903 			if (i > 0)
904 				{
905 				ctx->error=X509_V_ERR_CERT_NOT_YET_VALID;
906 				ctx->current_cert=xs;
907 				ok=(*cb)(0,ctx);
908 				if (!ok) goto end;
909 				}
910 			xs->valid=1;
911 			}
912 
913 		i=X509_cmp_time(X509_get_notAfter(xs), ptime);
914 		if (i == 0)
915 			{
916 			ctx->error=X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
917 			ctx->current_cert=xs;
918 			ok=(*cb)(0,ctx);
919 			if (!ok) goto end;
920 			}
921 
922 		if (i < 0)
923 			{
924 			ctx->error=X509_V_ERR_CERT_HAS_EXPIRED;
925 			ctx->current_cert=xs;
926 			ok=(*cb)(0,ctx);
927 			if (!ok) goto end;
928 			}
929 
930 		/* The last error (if any) is still in the error value */
931 		ctx->current_issuer=xi;
932 		ctx->current_cert=xs;
933 		ok=(*cb)(1,ctx);
934 		if (!ok) goto end;
935 
936 		n--;
937 		if (n >= 0)
938 			{
939 			xi=xs;
940 			xs=sk_X509_value(ctx->chain,n);
941 			}
942 		}
943 	ok=1;
944 end:
945 	return ok;
946 	}
947 
X509_cmp_current_time(ASN1_TIME * ctm)948 int X509_cmp_current_time(ASN1_TIME *ctm)
949 {
950 	return X509_cmp_time(ctm, NULL);
951 }
952 
X509_cmp_time(ASN1_TIME * ctm,time_t * cmp_time)953 int X509_cmp_time(ASN1_TIME *ctm, time_t *cmp_time)
954 	{
955 	char *str;
956 	ASN1_TIME atm;
957 	long offset;
958 	char buff1[24],buff2[24],*p;
959 	int i,j;
960 
961 	p=buff1;
962 	i=ctm->length;
963 	str=(char *)ctm->data;
964 	if (ctm->type == V_ASN1_UTCTIME)
965 		{
966 		if ((i < 11) || (i > 17)) return 0;
967 		memcpy(p,str,10);
968 		p+=10;
969 		str+=10;
970 		}
971 	else
972 		{
973 		if (i < 13) return 0;
974 		memcpy(p,str,12);
975 		p+=12;
976 		str+=12;
977 		}
978 
979 	if ((*str == 'Z') || (*str == '-') || (*str == '+'))
980 		{ *(p++)='0'; *(p++)='0'; }
981 	else
982 		{
983 		*(p++)= *(str++);
984 		*(p++)= *(str++);
985 		/* Skip any fractional seconds... */
986 		if (*str == '.')
987 			{
988 			str++;
989 			while ((*str >= '0') && (*str <= '9')) str++;
990 			}
991 
992 		}
993 	*(p++)='Z';
994 	*(p++)='\0';
995 
996 	if (*str == 'Z')
997 		offset=0;
998 	else
999 		{
1000 		if ((*str != '+') && (*str != '-'))
1001 			return 0;
1002 		offset=((str[1]-'0')*10+(str[2]-'0'))*60;
1003 		offset+=(str[3]-'0')*10+(str[4]-'0');
1004 		if (*str == '-')
1005 			offset= -offset;
1006 		}
1007 	atm.type=ctm->type;
1008 	atm.length=sizeof(buff2);
1009 	atm.data=(unsigned char *)buff2;
1010 
1011 	if (X509_time_adj(&atm,-offset*60, cmp_time) == NULL)
1012 		return 0;
1013 
1014 	if (ctm->type == V_ASN1_UTCTIME)
1015 		{
1016 		i=(buff1[0]-'0')*10+(buff1[1]-'0');
1017 		if (i < 50) i+=100; /* cf. RFC 2459 */
1018 		j=(buff2[0]-'0')*10+(buff2[1]-'0');
1019 		if (j < 50) j+=100;
1020 
1021 		if (i < j) return -1;
1022 		if (i > j) return 1;
1023 		}
1024 	i=strcmp(buff1,buff2);
1025 	if (i == 0) /* wait a second then return younger :-) */
1026 		return -1;
1027 	else
1028 		return i;
1029 	}
1030 
X509_gmtime_adj(ASN1_TIME * s,long adj)1031 ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long adj)
1032 {
1033 	return X509_time_adj(s, adj, NULL);
1034 }
1035 
X509_time_adj(ASN1_TIME * s,long adj,time_t * in_tm)1036 ASN1_TIME *X509_time_adj(ASN1_TIME *s, long adj, time_t *in_tm)
1037 	{
1038 	time_t t;
1039 	int type = -1;
1040 
1041 	if (in_tm) t = *in_tm;
1042 	else time(&t);
1043 
1044 	t+=adj;
1045 	if (s) type = s->type;
1046 	if (type == V_ASN1_UTCTIME) return ASN1_UTCTIME_set(s,t);
1047 	if (type == V_ASN1_GENERALIZEDTIME) return ASN1_GENERALIZEDTIME_set(s, t);
1048 	return ASN1_TIME_set(s, t);
1049 	}
1050 
X509_get_pubkey_parameters(EVP_PKEY * pkey,STACK_OF (X509)* chain)1051 int X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK_OF(X509) *chain)
1052 	{
1053 	EVP_PKEY *ktmp=NULL,*ktmp2;
1054 	int i,j;
1055 
1056 	if ((pkey != NULL) && !EVP_PKEY_missing_parameters(pkey)) return 1;
1057 
1058 	for (i=0; i<sk_X509_num(chain); i++)
1059 		{
1060 		ktmp=X509_get_pubkey(sk_X509_value(chain,i));
1061 		if (ktmp == NULL)
1062 			{
1063 			X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY);
1064 			return 0;
1065 			}
1066 		if (!EVP_PKEY_missing_parameters(ktmp))
1067 			break;
1068 		else
1069 			{
1070 			EVP_PKEY_free(ktmp);
1071 			ktmp=NULL;
1072 			}
1073 		}
1074 	if (ktmp == NULL)
1075 		{
1076 		X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN);
1077 		return 0;
1078 		}
1079 
1080 	/* first, populate the other certs */
1081 	for (j=i-1; j >= 0; j--)
1082 		{
1083 		ktmp2=X509_get_pubkey(sk_X509_value(chain,j));
1084 		EVP_PKEY_copy_parameters(ktmp2,ktmp);
1085 		EVP_PKEY_free(ktmp2);
1086 		}
1087 
1088 	if (pkey != NULL) EVP_PKEY_copy_parameters(pkey,ktmp);
1089 	EVP_PKEY_free(ktmp);
1090 	return 1;
1091 	}
1092 
X509_STORE_CTX_get_ex_new_index(long argl,void * argp,CRYPTO_EX_new * new_func,CRYPTO_EX_dup * dup_func,CRYPTO_EX_free * free_func)1093 int X509_STORE_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
1094 	     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
1095 	{
1096 	/* This function is (usually) called only once, by
1097 	 * SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c). */
1098 	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509_STORE_CTX, argl, argp,
1099 			new_func, dup_func, free_func);
1100 	}
1101 
X509_STORE_CTX_set_ex_data(X509_STORE_CTX * ctx,int idx,void * data)1102 int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data)
1103 	{
1104 	return CRYPTO_set_ex_data(&ctx->ex_data,idx,data);
1105 	}
1106 
X509_STORE_CTX_get_ex_data(X509_STORE_CTX * ctx,int idx)1107 void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx)
1108 	{
1109 	return CRYPTO_get_ex_data(&ctx->ex_data,idx);
1110 	}
1111 
X509_STORE_CTX_get_error(X509_STORE_CTX * ctx)1112 int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx)
1113 	{
1114 	return ctx->error;
1115 	}
1116 
X509_STORE_CTX_set_error(X509_STORE_CTX * ctx,int err)1117 void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err)
1118 	{
1119 	ctx->error=err;
1120 	}
1121 
X509_STORE_CTX_get_error_depth(X509_STORE_CTX * ctx)1122 int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx)
1123 	{
1124 	return ctx->error_depth;
1125 	}
1126 
X509_STORE_CTX_get_current_cert(X509_STORE_CTX * ctx)1127 X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx)
1128 	{
1129 	return ctx->current_cert;
1130 	}
1131 
STACK_OF(X509)1132 STACK_OF(X509) *X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx)
1133 	{
1134 	return ctx->chain;
1135 	}
1136 
STACK_OF(X509)1137 STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx)
1138 	{
1139 	int i;
1140 	X509 *x;
1141 	STACK_OF(X509) *chain;
1142 	if (!ctx->chain || !(chain = sk_X509_dup(ctx->chain))) return NULL;
1143 	for (i = 0; i < sk_X509_num(chain); i++)
1144 		{
1145 		x = sk_X509_value(chain, i);
1146 		CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1147 		}
1148 	return chain;
1149 	}
1150 
X509_STORE_CTX_set_cert(X509_STORE_CTX * ctx,X509 * x)1151 void X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x)
1152 	{
1153 	ctx->cert=x;
1154 	}
1155 
X509_STORE_CTX_set_chain(X509_STORE_CTX * ctx,STACK_OF (X509)* sk)1156 void X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
1157 	{
1158 	ctx->untrusted=sk;
1159 	}
1160 
X509_STORE_CTX_set_purpose(X509_STORE_CTX * ctx,int purpose)1161 int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose)
1162 	{
1163 	return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0);
1164 	}
1165 
X509_STORE_CTX_set_trust(X509_STORE_CTX * ctx,int trust)1166 int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust)
1167 	{
1168 	return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust);
1169 	}
1170 
1171 /* This function is used to set the X509_STORE_CTX purpose and trust
1172  * values. This is intended to be used when another structure has its
1173  * own trust and purpose values which (if set) will be inherited by
1174  * the ctx. If they aren't set then we will usually have a default
1175  * purpose in mind which should then be used to set the trust value.
1176  * An example of this is SSL use: an SSL structure will have its own
1177  * purpose and trust settings which the application can set: if they
1178  * aren't set then we use the default of SSL client/server.
1179  */
1180 
X509_STORE_CTX_purpose_inherit(X509_STORE_CTX * ctx,int def_purpose,int purpose,int trust)1181 int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,
1182 				int purpose, int trust)
1183 {
1184 	int idx;
1185 	/* If purpose not set use default */
1186 	if (!purpose) purpose = def_purpose;
1187 	/* If we have a purpose then check it is valid */
1188 	if (purpose)
1189 		{
1190 		X509_PURPOSE *ptmp;
1191 		idx = X509_PURPOSE_get_by_id(purpose);
1192 		if (idx == -1)
1193 			{
1194 			X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
1195 						X509_R_UNKNOWN_PURPOSE_ID);
1196 			return 0;
1197 			}
1198 		ptmp = X509_PURPOSE_get0(idx);
1199 		if (ptmp->trust == X509_TRUST_DEFAULT)
1200 			{
1201 			idx = X509_PURPOSE_get_by_id(def_purpose);
1202 			if (idx == -1)
1203 				{
1204 				X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
1205 						X509_R_UNKNOWN_PURPOSE_ID);
1206 				return 0;
1207 				}
1208 			ptmp = X509_PURPOSE_get0(idx);
1209 			}
1210 		/* If trust not set then get from purpose default */
1211 		if (!trust) trust = ptmp->trust;
1212 		}
1213 	if (trust)
1214 		{
1215 		idx = X509_TRUST_get_by_id(trust);
1216 		if (idx == -1)
1217 			{
1218 			X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
1219 						X509_R_UNKNOWN_TRUST_ID);
1220 			return 0;
1221 			}
1222 		}
1223 
1224 	if (purpose && !ctx->purpose) ctx->purpose = purpose;
1225 	if (trust && !ctx->trust) ctx->trust = trust;
1226 	return 1;
1227 }
1228 
X509_STORE_CTX_new(void)1229 X509_STORE_CTX *X509_STORE_CTX_new(void)
1230 {
1231 	X509_STORE_CTX *ctx;
1232 	ctx = (X509_STORE_CTX *)OPENSSL_malloc(sizeof(X509_STORE_CTX));
1233 	if (!ctx)
1234 		{
1235 		X509err(X509_F_X509_STORE_CTX_NEW,ERR_R_MALLOC_FAILURE);
1236 		return NULL;
1237 		}
1238 	memset(ctx, 0, sizeof(X509_STORE_CTX));
1239 	return ctx;
1240 }
1241 
X509_STORE_CTX_free(X509_STORE_CTX * ctx)1242 void X509_STORE_CTX_free(X509_STORE_CTX *ctx)
1243 {
1244 	X509_STORE_CTX_cleanup(ctx);
1245 	OPENSSL_free(ctx);
1246 }
1247 
X509_STORE_CTX_init(X509_STORE_CTX * ctx,X509_STORE * store,X509 * x509,STACK_OF (X509)* chain)1248 int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
1249 	     STACK_OF(X509) *chain)
1250 	{
1251 	ctx->ctx=store;
1252 	ctx->current_method=0;
1253 	ctx->cert=x509;
1254 	ctx->untrusted=chain;
1255 	ctx->last_untrusted=0;
1256 	ctx->check_time=0;
1257 	ctx->other_ctx=NULL;
1258 	ctx->valid=0;
1259 	ctx->chain=NULL;
1260 	ctx->depth=9;
1261 	ctx->error=0;
1262 	ctx->error_depth=0;
1263 	ctx->current_cert=NULL;
1264 	ctx->current_issuer=NULL;
1265 
1266 	/* Inherit callbacks and flags from X509_STORE if not set
1267 	 * use defaults.
1268 	 */
1269 
1270 
1271 	if (store)
1272 		{
1273 		ctx->purpose=store->purpose;
1274 		ctx->trust=store->trust;
1275 		ctx->flags = store->flags;
1276 		ctx->cleanup = store->cleanup;
1277 		}
1278 	else
1279 		{
1280 		ctx->purpose = 0;
1281 		ctx->trust = 0;
1282 		ctx->flags = 0;
1283 		ctx->cleanup = 0;
1284 		}
1285 
1286 	if (store && store->check_issued)
1287 		ctx->check_issued = store->check_issued;
1288 	else
1289 		ctx->check_issued = check_issued;
1290 
1291 	if (store && store->get_issuer)
1292 		ctx->get_issuer = store->get_issuer;
1293 	else
1294 		ctx->get_issuer = X509_STORE_CTX_get1_issuer;
1295 
1296 	if (store && store->verify_cb)
1297 		ctx->verify_cb = store->verify_cb;
1298 	else
1299 		ctx->verify_cb = null_callback;
1300 
1301 	if (store && store->verify)
1302 		ctx->verify = store->verify;
1303 	else
1304 		ctx->verify = internal_verify;
1305 
1306 	if (store && store->check_revocation)
1307 		ctx->check_revocation = store->check_revocation;
1308 	else
1309 		ctx->check_revocation = check_revocation;
1310 
1311 	if (store && store->get_crl)
1312 		ctx->get_crl = store->get_crl;
1313 	else
1314 		ctx->get_crl = get_crl;
1315 
1316 	if (store && store->check_crl)
1317 		ctx->check_crl = store->check_crl;
1318 	else
1319 		ctx->check_crl = check_crl;
1320 
1321 	if (store && store->cert_crl)
1322 		ctx->cert_crl = store->cert_crl;
1323 	else
1324 		ctx->cert_crl = cert_crl;
1325 
1326 
1327 	/* This memset() can't make any sense anyway, so it's removed. As
1328 	 * X509_STORE_CTX_cleanup does a proper "free" on the ex_data, we put a
1329 	 * corresponding "new" here and remove this bogus initialisation. */
1330 	/* memset(&(ctx->ex_data),0,sizeof(CRYPTO_EX_DATA)); */
1331 	if(!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx,
1332 				&(ctx->ex_data)))
1333 		{
1334 		OPENSSL_free(ctx);
1335 		X509err(X509_F_X509_STORE_CTX_INIT,ERR_R_MALLOC_FAILURE);
1336 		return 0;
1337 		}
1338 	return 1;
1339 	}
1340 
1341 /* Set alternative lookup method: just a STACK of trusted certificates.
1342  * This avoids X509_STORE nastiness where it isn't needed.
1343  */
1344 
X509_STORE_CTX_trusted_stack(X509_STORE_CTX * ctx,STACK_OF (X509)* sk)1345 void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
1346 {
1347 	ctx->other_ctx = sk;
1348 	ctx->get_issuer = get_issuer_sk;
1349 }
1350 
X509_STORE_CTX_cleanup(X509_STORE_CTX * ctx)1351 void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx)
1352 	{
1353 	if (ctx->cleanup) ctx->cleanup(ctx);
1354 	if (ctx->chain != NULL)
1355 		{
1356 		sk_X509_pop_free(ctx->chain,X509_free);
1357 		ctx->chain=NULL;
1358 		}
1359 	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx, &(ctx->ex_data));
1360 	memset(&ctx->ex_data,0,sizeof(CRYPTO_EX_DATA));
1361 	}
1362 
X509_STORE_CTX_set_flags(X509_STORE_CTX * ctx,long flags)1363 void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, long flags)
1364 	{
1365 	ctx->flags |= flags;
1366 	}
1367 
X509_STORE_CTX_set_time(X509_STORE_CTX * ctx,long flags,time_t t)1368 void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, long flags, time_t t)
1369 	{
1370 	ctx->check_time = t;
1371 	ctx->flags |= X509_V_FLAG_USE_CHECK_TIME;
1372 	}
1373 
X509_STORE_CTX_set_verify_cb(X509_STORE_CTX * ctx,int (* verify_cb)(int,X509_STORE_CTX *))1374 void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
1375 				  int (*verify_cb)(int, X509_STORE_CTX *))
1376 	{
1377 	ctx->verify_cb=verify_cb;
1378 	}
1379 
1380 IMPLEMENT_STACK_OF(X509)
1381 IMPLEMENT_ASN1_SET_OF(X509)
1382 
1383 IMPLEMENT_STACK_OF(X509_NAME)
1384 
1385 IMPLEMENT_STACK_OF(X509_ATTRIBUTE)
1386 IMPLEMENT_ASN1_SET_OF(X509_ATTRIBUTE)
1387