os/ossrv/ssl/tsrc/topenssl/src/verify.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ssl/tsrc/topenssl/src/verify.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,373 @@
     1.4 +/* apps/verify.c */
     1.5 +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
     1.6 + * All rights reserved.
     1.7 + *
     1.8 + * This package is an SSL implementation written
     1.9 + * by Eric Young (eay@cryptsoft.com).
    1.10 + * The implementation was written so as to conform with Netscapes SSL.
    1.11 + * 
    1.12 + * This library is free for commercial and non-commercial use as long as
    1.13 + * the following conditions are aheared to.  The following conditions
    1.14 + * apply to all code found in this distribution, be it the RC4, RSA,
    1.15 + * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
    1.16 + * included with this distribution is covered by the same copyright terms
    1.17 + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
    1.18 + * 
    1.19 + * Copyright remains Eric Young's, and as such any Copyright notices in
    1.20 + * the code are not to be removed.
    1.21 + * If this package is used in a product, Eric Young should be given attribution
    1.22 + * as the author of the parts of the library used.
    1.23 + * This can be in the form of a textual message at program startup or
    1.24 + * in documentation (online or textual) provided with the package.
    1.25 + * 
    1.26 + * Redistribution and use in source and binary forms, with or without
    1.27 + * modification, are permitted provided that the following conditions
    1.28 + * are met:
    1.29 + * 1. Redistributions of source code must retain the copyright
    1.30 + *    notice, this list of conditions and the following disclaimer.
    1.31 + * 2. Redistributions in binary form must reproduce the above copyright
    1.32 + *    notice, this list of conditions and the following disclaimer in the
    1.33 + *    documentation and/or other materials provided with the distribution.
    1.34 + * 3. All advertising materials mentioning features or use of this software
    1.35 + *    must display the following acknowledgement:
    1.36 + *    "This product includes cryptographic software written by
    1.37 + *     Eric Young (eay@cryptsoft.com)"
    1.38 + *    The word 'cryptographic' can be left out if the rouines from the library
    1.39 + *    being used are not cryptographic related :-).
    1.40 + * 4. If you include any Windows specific code (or a derivative thereof) from 
    1.41 + *    the apps directory (application code) you must include an acknowledgement:
    1.42 + *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
    1.43 + * 
    1.44 + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
    1.45 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.46 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.47 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
    1.48 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    1.49 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    1.50 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.51 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    1.52 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    1.53 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    1.54 + * SUCH DAMAGE.
    1.55 + * 
    1.56 + * The licence and distribution terms for any publically available version or
    1.57 + * derivative of this code cannot be changed.  i.e. this code cannot simply be
    1.58 + * copied and put under another distribution licence
    1.59 + * [including the GNU Public Licence.]
    1.60 + */
    1.61 +
    1.62 +#include <stdio.h>
    1.63 +#include <stdlib.h>
    1.64 +#include <string.h>
    1.65 +#include "apps.h"
    1.66 +#include <openssl/bio.h>
    1.67 +#include <openssl/err.h>
    1.68 +#include <openssl/x509.h>
    1.69 +#include <openssl/x509v3.h>
    1.70 +#include <openssl/pem.h>
    1.71 +
    1.72 +#undef PROG
    1.73 +#define PROG	verify_main
    1.74 +
    1.75 +static int MS_CALLBACK cb(int ok, X509_STORE_CTX *ctx);
    1.76 +static int check(X509_STORE *ctx, char *file, STACK_OF(X509) *uchain, STACK_OF(X509) *tchain, int purpose, ENGINE *e);
    1.77 +static STACK_OF(X509) *load_untrusted(char *file);
    1.78 +static int v_verbose=0, vflags = 0;
    1.79 +
    1.80 +
    1.81 +
    1.82 +int MAIN(int, char **);
    1.83 +
    1.84 +int MAIN(int argc, char **argv)
    1.85 +	{
    1.86 +	ENGINE *e = NULL;
    1.87 +	int i,ret=1, badarg = 0;
    1.88 +	int purpose = -1;
    1.89 +	char *CApath=NULL,*CAfile=NULL;
    1.90 +	char *untfile = NULL, *trustfile = NULL;
    1.91 +	STACK_OF(X509) *untrusted = NULL, *trusted = NULL;
    1.92 +	X509_STORE *cert_ctx=NULL;
    1.93 +	X509_LOOKUP *lookup=NULL;
    1.94 +	X509_VERIFY_PARAM *vpm = NULL;
    1.95 +#ifndef OPENSSL_NO_ENGINE
    1.96 +	char *engine=NULL;
    1.97 +#endif
    1.98 +
    1.99 +	cert_ctx=X509_STORE_new();
   1.100 +	if (cert_ctx == NULL) goto end;
   1.101 +	X509_STORE_set_verify_cb_func(cert_ctx,cb);
   1.102 +
   1.103 +	ERR_load_crypto_strings();
   1.104 +
   1.105 +	apps_startup();
   1.106 +
   1.107 +	if (bio_err == NULL)
   1.108 +		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
   1.109 +			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
   1.110 +		
   1.111 +
   1.112 +	if (!load_config(bio_err, NULL))
   1.113 +		goto end;
   1.114 +
   1.115 +	argc--;
   1.116 +	argv++;
   1.117 +	for (;;)
   1.118 +		{
   1.119 +		if (argc >= 1)
   1.120 +			{
   1.121 +			if (strcmp(*argv,"-CApath") == 0)
   1.122 +				{
   1.123 +				if (argc-- < 1) goto end;
   1.124 +				CApath= *(++argv);
   1.125 +				}
   1.126 +			else if (strcmp(*argv,"-CAfile") == 0)
   1.127 +				{
   1.128 +				if (argc-- < 1) goto end;
   1.129 +				CAfile= *(++argv);
   1.130 +				}
   1.131 +			else if (args_verify(&argv, &argc, &badarg, bio_err,
   1.132 +									&vpm))
   1.133 +				{
   1.134 +				if (badarg)
   1.135 +					goto end;
   1.136 +				continue;
   1.137 +				}
   1.138 +			else if (strcmp(*argv,"-untrusted") == 0)
   1.139 +				{
   1.140 +				if (argc-- < 1) goto end;
   1.141 +				untfile= *(++argv);
   1.142 +				}
   1.143 +			else if (strcmp(*argv,"-trusted") == 0)
   1.144 +				{
   1.145 +				if (argc-- < 1) goto end;
   1.146 +				trustfile= *(++argv);
   1.147 +				}
   1.148 +#ifndef OPENSSL_NO_ENGINE
   1.149 +			else if (strcmp(*argv,"-engine") == 0)
   1.150 +				{
   1.151 +				if (--argc < 1) goto end;
   1.152 +				engine= *(++argv);
   1.153 +				}
   1.154 +#endif
   1.155 +			else if (strcmp(*argv,"-help") == 0)
   1.156 +				goto end;
   1.157 +			else if (strcmp(*argv,"-verbose") == 0)
   1.158 +				v_verbose=1;
   1.159 +			else if (argv[0][0] == '-')
   1.160 +				goto end;
   1.161 +			else
   1.162 +				break;
   1.163 +			argc--;
   1.164 +			argv++;
   1.165 +			}
   1.166 +		else
   1.167 +			break;
   1.168 +		}
   1.169 +
   1.170 +#ifndef OPENSSL_NO_ENGINE
   1.171 +        e = setup_engine(bio_err, engine, 0);
   1.172 +#endif
   1.173 +
   1.174 +	if (vpm)
   1.175 +		X509_STORE_set1_param(cert_ctx, vpm);
   1.176 +
   1.177 +	lookup=X509_STORE_add_lookup(cert_ctx,X509_LOOKUP_file());
   1.178 +	if (lookup == NULL) abort();
   1.179 +	if (CAfile) {
   1.180 +		i=X509_LOOKUP_load_file(lookup,CAfile,X509_FILETYPE_PEM);
   1.181 +		if(!i) {
   1.182 +			BIO_printf(bio_err, "Error loading file %s\n", CAfile);
   1.183 +			ERR_print_errors(bio_err);
   1.184 +			goto end;
   1.185 +		}
   1.186 +	} else X509_LOOKUP_load_file(lookup,NULL,X509_FILETYPE_DEFAULT);
   1.187 +		
   1.188 +	lookup=X509_STORE_add_lookup(cert_ctx,X509_LOOKUP_hash_dir());
   1.189 +	if (lookup == NULL) abort();
   1.190 +	if (CApath) {
   1.191 +		i=X509_LOOKUP_add_dir(lookup,CApath,X509_FILETYPE_PEM);
   1.192 +		if(!i) {
   1.193 +			BIO_printf(bio_err, "Error loading directory %s\n", CApath);
   1.194 +			ERR_print_errors(bio_err);
   1.195 +			goto end;
   1.196 +		}
   1.197 +	} else X509_LOOKUP_add_dir(lookup,NULL,X509_FILETYPE_DEFAULT);
   1.198 +
   1.199 +	ERR_clear_error();
   1.200 +
   1.201 +	if(untfile) {
   1.202 +		if(!(untrusted = load_untrusted(untfile))) {
   1.203 +			BIO_printf(bio_err, "Error loading untrusted file %s\n", untfile);
   1.204 +			ERR_print_errors(bio_err);
   1.205 +			goto end;
   1.206 +		}
   1.207 +	}
   1.208 +
   1.209 +	if(trustfile) {
   1.210 +		if(!(trusted = load_untrusted(trustfile))) {
   1.211 +			BIO_printf(bio_err, "Error loading untrusted file %s\n", trustfile);
   1.212 +			ERR_print_errors(bio_err);
   1.213 +			goto end;
   1.214 +		}
   1.215 +	}
   1.216 +
   1.217 +	if (argc < 1) check(cert_ctx, NULL, untrusted, trusted, purpose, e);
   1.218 +	else
   1.219 +		for (i=0; i<argc; i++)
   1.220 +			check(cert_ctx,argv[i], untrusted, trusted, purpose, e);
   1.221 +	ret=0;
   1.222 +end:
   1.223 +	if (ret == 1) {
   1.224 +		BIO_printf(bio_err,"usage: verify [-verbose] [-CApath path] [-CAfile file] [-purpose purpose] [-crl_check]");
   1.225 +#ifndef OPENSSL_NO_ENGINE
   1.226 +		BIO_printf(bio_err," [-engine e]");
   1.227 +#endif
   1.228 +		BIO_printf(bio_err," cert1 cert2 ...\n");
   1.229 +		BIO_printf(bio_err,"recognized usages:\n");
   1.230 +		for(i = 0; i < X509_PURPOSE_get_count(); i++) {
   1.231 +			X509_PURPOSE *ptmp;
   1.232 +			ptmp = X509_PURPOSE_get0(i);
   1.233 +			BIO_printf(bio_err, "\t%-10s\t%s\n", X509_PURPOSE_get0_sname(ptmp),
   1.234 +								X509_PURPOSE_get0_name(ptmp));
   1.235 +		}
   1.236 +	}
   1.237 +	if (vpm) X509_VERIFY_PARAM_free(vpm);
   1.238 +	if (cert_ctx != NULL) X509_STORE_free(cert_ctx);
   1.239 +	sk_X509_pop_free(untrusted, X509_free);
   1.240 +	sk_X509_pop_free(trusted, X509_free);
   1.241 +	apps_shutdown();
   1.242 +	OPENSSL_EXIT(ret);
   1.243 +	}
   1.244 +
   1.245 +static int check(X509_STORE *ctx, char *file, STACK_OF(X509) *uchain, STACK_OF(X509) *tchain, int purpose, ENGINE *e)
   1.246 +	{
   1.247 +	X509 *x=NULL;
   1.248 +	int i=0,ret=0;
   1.249 +	X509_STORE_CTX *csc;
   1.250 +
   1.251 +	x = load_cert(bio_err, file, FORMAT_PEM, NULL, e, "certificate file");
   1.252 +	if (x == NULL)
   1.253 +		goto end;
   1.254 +	fprintf(stdout,"%s: ",(file == NULL)?"stdin":file);
   1.255 +
   1.256 +	csc = X509_STORE_CTX_new();
   1.257 +	if (csc == NULL)
   1.258 +		{
   1.259 +		ERR_print_errors(bio_err);
   1.260 +		goto end;
   1.261 +		}
   1.262 +	X509_STORE_set_flags(ctx, vflags);
   1.263 +	if(!X509_STORE_CTX_init(csc,ctx,x,uchain))
   1.264 +		{
   1.265 +		ERR_print_errors(bio_err);
   1.266 +		goto end;
   1.267 +		}
   1.268 +	if(tchain) X509_STORE_CTX_trusted_stack(csc, tchain);
   1.269 +	if(purpose >= 0) X509_STORE_CTX_set_purpose(csc, purpose);
   1.270 +	i=X509_verify_cert(csc);
   1.271 +	X509_STORE_CTX_free(csc);
   1.272 +
   1.273 +	ret=0;
   1.274 +end:
   1.275 +	if (i)
   1.276 +		{
   1.277 +		fprintf(stdout,"OK\n");
   1.278 +  	ret=1;
   1.279 +		}
   1.280 +	else
   1.281 +		ERR_print_errors(bio_err);
   1.282 +	if (x != NULL) X509_free(x);
   1.283 +
   1.284 +	return(ret);
   1.285 +	}
   1.286 +
   1.287 +static STACK_OF(X509) *load_untrusted(char *certfile)
   1.288 +{
   1.289 +	STACK_OF(X509_INFO) *sk=NULL;
   1.290 +	STACK_OF(X509) *stack=NULL, *ret=NULL;
   1.291 +	BIO *in=NULL;
   1.292 +	X509_INFO *xi;
   1.293 +
   1.294 +	if(!(stack = sk_X509_new_null())) {
   1.295 +		BIO_printf(bio_err,"memory allocation failure\n");
   1.296 +		goto end;
   1.297 +	}
   1.298 +
   1.299 +	if(!(in=BIO_new_file(certfile, "r"))) {
   1.300 +		BIO_printf(bio_err,"error opening the file, %s\n",certfile);
   1.301 +		goto end;
   1.302 +	}
   1.303 +
   1.304 +	/* This loads from a file, a stack of x509/crl/pkey sets */
   1.305 +	if(!(sk=PEM_X509_INFO_read_bio(in,NULL,NULL,NULL))) {
   1.306 +		BIO_printf(bio_err,"error reading the file, %s\n",certfile);
   1.307 +		goto end;
   1.308 +	}
   1.309 +
   1.310 +	/* scan over it and pull out the certs */
   1.311 +	while (sk_X509_INFO_num(sk))
   1.312 +		{
   1.313 +		xi=sk_X509_INFO_shift(sk);
   1.314 +		if (xi->x509 != NULL)
   1.315 +			{
   1.316 +			sk_X509_push(stack,xi->x509);
   1.317 +			xi->x509=NULL;
   1.318 +			}
   1.319 +		X509_INFO_free(xi);
   1.320 +		}
   1.321 +	if(!sk_X509_num(stack)) {
   1.322 +		BIO_printf(bio_err,"no certificates in file, %s\n",certfile);
   1.323 +		sk_X509_free(stack);
   1.324 +		goto end;
   1.325 +	}
   1.326 +	ret=stack;
   1.327 +end:
   1.328 +	BIO_free(in);
   1.329 +	sk_X509_INFO_free(sk);
   1.330 +	return(ret);
   1.331 +	}
   1.332 +
   1.333 +static int MS_CALLBACK cb(int ok, X509_STORE_CTX *ctx)
   1.334 +	{
   1.335 +	char buf[256];
   1.336 +
   1.337 +	if (!ok)
   1.338 +		{
   1.339 +		if (ctx->current_cert)
   1.340 +			{
   1.341 +			X509_NAME_oneline(
   1.342 +				X509_get_subject_name(ctx->current_cert),buf,
   1.343 +				sizeof buf);
   1.344 +			printf("%s\n",buf);
   1.345 +			}
   1.346 +		printf("error %d at %d depth lookup:%s\n",ctx->error,
   1.347 +			ctx->error_depth,
   1.348 +			X509_verify_cert_error_string(ctx->error));
   1.349 +		if (ctx->error == X509_V_ERR_CERT_HAS_EXPIRED) ok=1;
   1.350 +		/* since we are just checking the certificates, it is
   1.351 +		 * ok if they are self signed. But we should still warn
   1.352 +		 * the user.
   1.353 + 		 */
   1.354 +		if (ctx->error == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) ok=1;
   1.355 +		/* Continue after extension errors too */
   1.356 +		if (ctx->error == X509_V_ERR_INVALID_CA) ok=1;
   1.357 +		if (ctx->error == X509_V_ERR_INVALID_NON_CA) ok=1;
   1.358 +		if (ctx->error == X509_V_ERR_PATH_LENGTH_EXCEEDED) ok=1;
   1.359 +		if (ctx->error == X509_V_ERR_INVALID_PURPOSE) ok=1;
   1.360 +		if (ctx->error == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) ok=1;
   1.361 +		if (ctx->error == X509_V_ERR_CRL_HAS_EXPIRED) ok=1;
   1.362 +		if (ctx->error == X509_V_ERR_CRL_NOT_YET_VALID) ok=1;
   1.363 +		if (ctx->error == X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION) ok=1;
   1.364 +
   1.365 +		if (ctx->error == X509_V_ERR_NO_EXPLICIT_POLICY)
   1.366 +			policies_print(NULL, ctx);
   1.367 +		return ok;
   1.368 +
   1.369 +		}
   1.370 +	if ((ctx->error == X509_V_OK) && (ok == 2))
   1.371 +		policies_print(NULL, ctx);
   1.372 +	if (!v_verbose)
   1.373 +		ERR_clear_error();
   1.374 +	return(ok);
   1.375 +	}
   1.376 +