os/ossrv/ssl/tsrc/topenssl/src/verify.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* apps/verify.c */
sl@0
     2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
sl@0
     3
 * All rights reserved.
sl@0
     4
 *
sl@0
     5
 * This package is an SSL implementation written
sl@0
     6
 * by Eric Young (eay@cryptsoft.com).
sl@0
     7
 * The implementation was written so as to conform with Netscapes SSL.
sl@0
     8
 * 
sl@0
     9
 * This library is free for commercial and non-commercial use as long as
sl@0
    10
 * the following conditions are aheared to.  The following conditions
sl@0
    11
 * apply to all code found in this distribution, be it the RC4, RSA,
sl@0
    12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
sl@0
    13
 * included with this distribution is covered by the same copyright terms
sl@0
    14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
sl@0
    15
 * 
sl@0
    16
 * Copyright remains Eric Young's, and as such any Copyright notices in
sl@0
    17
 * the code are not to be removed.
sl@0
    18
 * If this package is used in a product, Eric Young should be given attribution
sl@0
    19
 * as the author of the parts of the library used.
sl@0
    20
 * This can be in the form of a textual message at program startup or
sl@0
    21
 * in documentation (online or textual) provided with the package.
sl@0
    22
 * 
sl@0
    23
 * Redistribution and use in source and binary forms, with or without
sl@0
    24
 * modification, are permitted provided that the following conditions
sl@0
    25
 * are met:
sl@0
    26
 * 1. Redistributions of source code must retain the copyright
sl@0
    27
 *    notice, this list of conditions and the following disclaimer.
sl@0
    28
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    29
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    30
 *    documentation and/or other materials provided with the distribution.
sl@0
    31
 * 3. All advertising materials mentioning features or use of this software
sl@0
    32
 *    must display the following acknowledgement:
sl@0
    33
 *    "This product includes cryptographic software written by
sl@0
    34
 *     Eric Young (eay@cryptsoft.com)"
sl@0
    35
 *    The word 'cryptographic' can be left out if the rouines from the library
sl@0
    36
 *    being used are not cryptographic related :-).
sl@0
    37
 * 4. If you include any Windows specific code (or a derivative thereof) from 
sl@0
    38
 *    the apps directory (application code) you must include an acknowledgement:
sl@0
    39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
sl@0
    40
 * 
sl@0
    41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
sl@0
    42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
sl@0
    45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0
    46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0
    47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0
    49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0
    50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0
    51
 * SUCH DAMAGE.
sl@0
    52
 * 
sl@0
    53
 * The licence and distribution terms for any publically available version or
sl@0
    54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
sl@0
    55
 * copied and put under another distribution licence
sl@0
    56
 * [including the GNU Public Licence.]
sl@0
    57
 */
sl@0
    58
sl@0
    59
#include <stdio.h>
sl@0
    60
#include <stdlib.h>
sl@0
    61
#include <string.h>
sl@0
    62
#include "apps.h"
sl@0
    63
#include <openssl/bio.h>
sl@0
    64
#include <openssl/err.h>
sl@0
    65
#include <openssl/x509.h>
sl@0
    66
#include <openssl/x509v3.h>
sl@0
    67
#include <openssl/pem.h>
sl@0
    68
sl@0
    69
#undef PROG
sl@0
    70
#define PROG	verify_main
sl@0
    71
sl@0
    72
static int MS_CALLBACK cb(int ok, X509_STORE_CTX *ctx);
sl@0
    73
static int check(X509_STORE *ctx, char *file, STACK_OF(X509) *uchain, STACK_OF(X509) *tchain, int purpose, ENGINE *e);
sl@0
    74
static STACK_OF(X509) *load_untrusted(char *file);
sl@0
    75
static int v_verbose=0, vflags = 0;
sl@0
    76
sl@0
    77
sl@0
    78
sl@0
    79
int MAIN(int, char **);
sl@0
    80
sl@0
    81
int MAIN(int argc, char **argv)
sl@0
    82
	{
sl@0
    83
	ENGINE *e = NULL;
sl@0
    84
	int i,ret=1, badarg = 0;
sl@0
    85
	int purpose = -1;
sl@0
    86
	char *CApath=NULL,*CAfile=NULL;
sl@0
    87
	char *untfile = NULL, *trustfile = NULL;
sl@0
    88
	STACK_OF(X509) *untrusted = NULL, *trusted = NULL;
sl@0
    89
	X509_STORE *cert_ctx=NULL;
sl@0
    90
	X509_LOOKUP *lookup=NULL;
sl@0
    91
	X509_VERIFY_PARAM *vpm = NULL;
sl@0
    92
#ifndef OPENSSL_NO_ENGINE
sl@0
    93
	char *engine=NULL;
sl@0
    94
#endif
sl@0
    95
sl@0
    96
	cert_ctx=X509_STORE_new();
sl@0
    97
	if (cert_ctx == NULL) goto end;
sl@0
    98
	X509_STORE_set_verify_cb_func(cert_ctx,cb);
sl@0
    99
sl@0
   100
	ERR_load_crypto_strings();
sl@0
   101
sl@0
   102
	apps_startup();
sl@0
   103
sl@0
   104
	if (bio_err == NULL)
sl@0
   105
		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
sl@0
   106
			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
sl@0
   107
		
sl@0
   108
sl@0
   109
	if (!load_config(bio_err, NULL))
sl@0
   110
		goto end;
sl@0
   111
sl@0
   112
	argc--;
sl@0
   113
	argv++;
sl@0
   114
	for (;;)
sl@0
   115
		{
sl@0
   116
		if (argc >= 1)
sl@0
   117
			{
sl@0
   118
			if (strcmp(*argv,"-CApath") == 0)
sl@0
   119
				{
sl@0
   120
				if (argc-- < 1) goto end;
sl@0
   121
				CApath= *(++argv);
sl@0
   122
				}
sl@0
   123
			else if (strcmp(*argv,"-CAfile") == 0)
sl@0
   124
				{
sl@0
   125
				if (argc-- < 1) goto end;
sl@0
   126
				CAfile= *(++argv);
sl@0
   127
				}
sl@0
   128
			else if (args_verify(&argv, &argc, &badarg, bio_err,
sl@0
   129
									&vpm))
sl@0
   130
				{
sl@0
   131
				if (badarg)
sl@0
   132
					goto end;
sl@0
   133
				continue;
sl@0
   134
				}
sl@0
   135
			else if (strcmp(*argv,"-untrusted") == 0)
sl@0
   136
				{
sl@0
   137
				if (argc-- < 1) goto end;
sl@0
   138
				untfile= *(++argv);
sl@0
   139
				}
sl@0
   140
			else if (strcmp(*argv,"-trusted") == 0)
sl@0
   141
				{
sl@0
   142
				if (argc-- < 1) goto end;
sl@0
   143
				trustfile= *(++argv);
sl@0
   144
				}
sl@0
   145
#ifndef OPENSSL_NO_ENGINE
sl@0
   146
			else if (strcmp(*argv,"-engine") == 0)
sl@0
   147
				{
sl@0
   148
				if (--argc < 1) goto end;
sl@0
   149
				engine= *(++argv);
sl@0
   150
				}
sl@0
   151
#endif
sl@0
   152
			else if (strcmp(*argv,"-help") == 0)
sl@0
   153
				goto end;
sl@0
   154
			else if (strcmp(*argv,"-verbose") == 0)
sl@0
   155
				v_verbose=1;
sl@0
   156
			else if (argv[0][0] == '-')
sl@0
   157
				goto end;
sl@0
   158
			else
sl@0
   159
				break;
sl@0
   160
			argc--;
sl@0
   161
			argv++;
sl@0
   162
			}
sl@0
   163
		else
sl@0
   164
			break;
sl@0
   165
		}
sl@0
   166
sl@0
   167
#ifndef OPENSSL_NO_ENGINE
sl@0
   168
        e = setup_engine(bio_err, engine, 0);
sl@0
   169
#endif
sl@0
   170
sl@0
   171
	if (vpm)
sl@0
   172
		X509_STORE_set1_param(cert_ctx, vpm);
sl@0
   173
sl@0
   174
	lookup=X509_STORE_add_lookup(cert_ctx,X509_LOOKUP_file());
sl@0
   175
	if (lookup == NULL) abort();
sl@0
   176
	if (CAfile) {
sl@0
   177
		i=X509_LOOKUP_load_file(lookup,CAfile,X509_FILETYPE_PEM);
sl@0
   178
		if(!i) {
sl@0
   179
			BIO_printf(bio_err, "Error loading file %s\n", CAfile);
sl@0
   180
			ERR_print_errors(bio_err);
sl@0
   181
			goto end;
sl@0
   182
		}
sl@0
   183
	} else X509_LOOKUP_load_file(lookup,NULL,X509_FILETYPE_DEFAULT);
sl@0
   184
		
sl@0
   185
	lookup=X509_STORE_add_lookup(cert_ctx,X509_LOOKUP_hash_dir());
sl@0
   186
	if (lookup == NULL) abort();
sl@0
   187
	if (CApath) {
sl@0
   188
		i=X509_LOOKUP_add_dir(lookup,CApath,X509_FILETYPE_PEM);
sl@0
   189
		if(!i) {
sl@0
   190
			BIO_printf(bio_err, "Error loading directory %s\n", CApath);
sl@0
   191
			ERR_print_errors(bio_err);
sl@0
   192
			goto end;
sl@0
   193
		}
sl@0
   194
	} else X509_LOOKUP_add_dir(lookup,NULL,X509_FILETYPE_DEFAULT);
sl@0
   195
sl@0
   196
	ERR_clear_error();
sl@0
   197
sl@0
   198
	if(untfile) {
sl@0
   199
		if(!(untrusted = load_untrusted(untfile))) {
sl@0
   200
			BIO_printf(bio_err, "Error loading untrusted file %s\n", untfile);
sl@0
   201
			ERR_print_errors(bio_err);
sl@0
   202
			goto end;
sl@0
   203
		}
sl@0
   204
	}
sl@0
   205
sl@0
   206
	if(trustfile) {
sl@0
   207
		if(!(trusted = load_untrusted(trustfile))) {
sl@0
   208
			BIO_printf(bio_err, "Error loading untrusted file %s\n", trustfile);
sl@0
   209
			ERR_print_errors(bio_err);
sl@0
   210
			goto end;
sl@0
   211
		}
sl@0
   212
	}
sl@0
   213
sl@0
   214
	if (argc < 1) check(cert_ctx, NULL, untrusted, trusted, purpose, e);
sl@0
   215
	else
sl@0
   216
		for (i=0; i<argc; i++)
sl@0
   217
			check(cert_ctx,argv[i], untrusted, trusted, purpose, e);
sl@0
   218
	ret=0;
sl@0
   219
end:
sl@0
   220
	if (ret == 1) {
sl@0
   221
		BIO_printf(bio_err,"usage: verify [-verbose] [-CApath path] [-CAfile file] [-purpose purpose] [-crl_check]");
sl@0
   222
#ifndef OPENSSL_NO_ENGINE
sl@0
   223
		BIO_printf(bio_err," [-engine e]");
sl@0
   224
#endif
sl@0
   225
		BIO_printf(bio_err," cert1 cert2 ...\n");
sl@0
   226
		BIO_printf(bio_err,"recognized usages:\n");
sl@0
   227
		for(i = 0; i < X509_PURPOSE_get_count(); i++) {
sl@0
   228
			X509_PURPOSE *ptmp;
sl@0
   229
			ptmp = X509_PURPOSE_get0(i);
sl@0
   230
			BIO_printf(bio_err, "\t%-10s\t%s\n", X509_PURPOSE_get0_sname(ptmp),
sl@0
   231
								X509_PURPOSE_get0_name(ptmp));
sl@0
   232
		}
sl@0
   233
	}
sl@0
   234
	if (vpm) X509_VERIFY_PARAM_free(vpm);
sl@0
   235
	if (cert_ctx != NULL) X509_STORE_free(cert_ctx);
sl@0
   236
	sk_X509_pop_free(untrusted, X509_free);
sl@0
   237
	sk_X509_pop_free(trusted, X509_free);
sl@0
   238
	apps_shutdown();
sl@0
   239
	OPENSSL_EXIT(ret);
sl@0
   240
	}
sl@0
   241
sl@0
   242
static int check(X509_STORE *ctx, char *file, STACK_OF(X509) *uchain, STACK_OF(X509) *tchain, int purpose, ENGINE *e)
sl@0
   243
	{
sl@0
   244
	X509 *x=NULL;
sl@0
   245
	int i=0,ret=0;
sl@0
   246
	X509_STORE_CTX *csc;
sl@0
   247
sl@0
   248
	x = load_cert(bio_err, file, FORMAT_PEM, NULL, e, "certificate file");
sl@0
   249
	if (x == NULL)
sl@0
   250
		goto end;
sl@0
   251
	fprintf(stdout,"%s: ",(file == NULL)?"stdin":file);
sl@0
   252
sl@0
   253
	csc = X509_STORE_CTX_new();
sl@0
   254
	if (csc == NULL)
sl@0
   255
		{
sl@0
   256
		ERR_print_errors(bio_err);
sl@0
   257
		goto end;
sl@0
   258
		}
sl@0
   259
	X509_STORE_set_flags(ctx, vflags);
sl@0
   260
	if(!X509_STORE_CTX_init(csc,ctx,x,uchain))
sl@0
   261
		{
sl@0
   262
		ERR_print_errors(bio_err);
sl@0
   263
		goto end;
sl@0
   264
		}
sl@0
   265
	if(tchain) X509_STORE_CTX_trusted_stack(csc, tchain);
sl@0
   266
	if(purpose >= 0) X509_STORE_CTX_set_purpose(csc, purpose);
sl@0
   267
	i=X509_verify_cert(csc);
sl@0
   268
	X509_STORE_CTX_free(csc);
sl@0
   269
sl@0
   270
	ret=0;
sl@0
   271
end:
sl@0
   272
	if (i)
sl@0
   273
		{
sl@0
   274
		fprintf(stdout,"OK\n");
sl@0
   275
  	ret=1;
sl@0
   276
		}
sl@0
   277
	else
sl@0
   278
		ERR_print_errors(bio_err);
sl@0
   279
	if (x != NULL) X509_free(x);
sl@0
   280
sl@0
   281
	return(ret);
sl@0
   282
	}
sl@0
   283
sl@0
   284
static STACK_OF(X509) *load_untrusted(char *certfile)
sl@0
   285
{
sl@0
   286
	STACK_OF(X509_INFO) *sk=NULL;
sl@0
   287
	STACK_OF(X509) *stack=NULL, *ret=NULL;
sl@0
   288
	BIO *in=NULL;
sl@0
   289
	X509_INFO *xi;
sl@0
   290
sl@0
   291
	if(!(stack = sk_X509_new_null())) {
sl@0
   292
		BIO_printf(bio_err,"memory allocation failure\n");
sl@0
   293
		goto end;
sl@0
   294
	}
sl@0
   295
sl@0
   296
	if(!(in=BIO_new_file(certfile, "r"))) {
sl@0
   297
		BIO_printf(bio_err,"error opening the file, %s\n",certfile);
sl@0
   298
		goto end;
sl@0
   299
	}
sl@0
   300
sl@0
   301
	/* This loads from a file, a stack of x509/crl/pkey sets */
sl@0
   302
	if(!(sk=PEM_X509_INFO_read_bio(in,NULL,NULL,NULL))) {
sl@0
   303
		BIO_printf(bio_err,"error reading the file, %s\n",certfile);
sl@0
   304
		goto end;
sl@0
   305
	}
sl@0
   306
sl@0
   307
	/* scan over it and pull out the certs */
sl@0
   308
	while (sk_X509_INFO_num(sk))
sl@0
   309
		{
sl@0
   310
		xi=sk_X509_INFO_shift(sk);
sl@0
   311
		if (xi->x509 != NULL)
sl@0
   312
			{
sl@0
   313
			sk_X509_push(stack,xi->x509);
sl@0
   314
			xi->x509=NULL;
sl@0
   315
			}
sl@0
   316
		X509_INFO_free(xi);
sl@0
   317
		}
sl@0
   318
	if(!sk_X509_num(stack)) {
sl@0
   319
		BIO_printf(bio_err,"no certificates in file, %s\n",certfile);
sl@0
   320
		sk_X509_free(stack);
sl@0
   321
		goto end;
sl@0
   322
	}
sl@0
   323
	ret=stack;
sl@0
   324
end:
sl@0
   325
	BIO_free(in);
sl@0
   326
	sk_X509_INFO_free(sk);
sl@0
   327
	return(ret);
sl@0
   328
	}
sl@0
   329
sl@0
   330
static int MS_CALLBACK cb(int ok, X509_STORE_CTX *ctx)
sl@0
   331
	{
sl@0
   332
	char buf[256];
sl@0
   333
sl@0
   334
	if (!ok)
sl@0
   335
		{
sl@0
   336
		if (ctx->current_cert)
sl@0
   337
			{
sl@0
   338
			X509_NAME_oneline(
sl@0
   339
				X509_get_subject_name(ctx->current_cert),buf,
sl@0
   340
				sizeof buf);
sl@0
   341
			printf("%s\n",buf);
sl@0
   342
			}
sl@0
   343
		printf("error %d at %d depth lookup:%s\n",ctx->error,
sl@0
   344
			ctx->error_depth,
sl@0
   345
			X509_verify_cert_error_string(ctx->error));
sl@0
   346
		if (ctx->error == X509_V_ERR_CERT_HAS_EXPIRED) ok=1;
sl@0
   347
		/* since we are just checking the certificates, it is
sl@0
   348
		 * ok if they are self signed. But we should still warn
sl@0
   349
		 * the user.
sl@0
   350
 		 */
sl@0
   351
		if (ctx->error == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) ok=1;
sl@0
   352
		/* Continue after extension errors too */
sl@0
   353
		if (ctx->error == X509_V_ERR_INVALID_CA) ok=1;
sl@0
   354
		if (ctx->error == X509_V_ERR_INVALID_NON_CA) ok=1;
sl@0
   355
		if (ctx->error == X509_V_ERR_PATH_LENGTH_EXCEEDED) ok=1;
sl@0
   356
		if (ctx->error == X509_V_ERR_INVALID_PURPOSE) ok=1;
sl@0
   357
		if (ctx->error == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) ok=1;
sl@0
   358
		if (ctx->error == X509_V_ERR_CRL_HAS_EXPIRED) ok=1;
sl@0
   359
		if (ctx->error == X509_V_ERR_CRL_NOT_YET_VALID) ok=1;
sl@0
   360
		if (ctx->error == X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION) ok=1;
sl@0
   361
sl@0
   362
		if (ctx->error == X509_V_ERR_NO_EXPLICIT_POLICY)
sl@0
   363
			policies_print(NULL, ctx);
sl@0
   364
		return ok;
sl@0
   365
sl@0
   366
		}
sl@0
   367
	if ((ctx->error == X509_V_OK) && (ok == 2))
sl@0
   368
		policies_print(NULL, ctx);
sl@0
   369
	if (!v_verbose)
sl@0
   370
		ERR_clear_error();
sl@0
   371
	return(ok);
sl@0
   372
	}
sl@0
   373