verify.c revision 306195
1189251Ssam/* apps/verify.c */ 2189251Ssam/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3252726Srpaulo * All rights reserved. 4189251Ssam * 5252726Srpaulo * This package is an SSL implementation written 6252726Srpaulo * by Eric Young (eay@cryptsoft.com). 7189251Ssam * The implementation was written so as to conform with Netscapes SSL. 8189251Ssam * 9189251Ssam * This library is free for commercial and non-commercial use as long as 10189251Ssam * the following conditions are aheared to. The following conditions 11189251Ssam * apply to all code found in this distribution, be it the RC4, RSA, 12189251Ssam * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13189251Ssam * included with this distribution is covered by the same copyright terms 14189251Ssam * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15189251Ssam * 16252726Srpaulo * Copyright remains Eric Young's, and as such any Copyright notices in 17252726Srpaulo * the code are not to be removed. 18189251Ssam * If this package is used in a product, Eric Young should be given attribution 19189251Ssam * as the author of the parts of the library used. 20214734Srpaulo * This can be in the form of a textual message at program startup or 21214734Srpaulo * in documentation (online or textual) provided with the package. 22189251Ssam * 23189251Ssam * Redistribution and use in source and binary forms, with or without 24252726Srpaulo * modification, are permitted provided that the following conditions 25189251Ssam * are met: 26189251Ssam * 1. Redistributions of source code must retain the copyright 27214734Srpaulo * notice, this list of conditions and the following disclaimer. 28189251Ssam * 2. Redistributions in binary form must reproduce the above copyright 29189251Ssam * notice, this list of conditions and the following disclaimer in the 30214734Srpaulo * documentation and/or other materials provided with the distribution. 31214734Srpaulo * 3. All advertising materials mentioning features or use of this software 32214734Srpaulo * must display the following acknowledgement: 33214734Srpaulo * "This product includes cryptographic software written by 34214734Srpaulo * Eric Young (eay@cryptsoft.com)" 35252726Srpaulo * The word 'cryptographic' can be left out if the rouines from the library 36189251Ssam * being used are not cryptographic related :-). 37189251Ssam * 4. If you include any Windows specific code (or a derivative thereof) from 38189251Ssam * the apps directory (application code) you must include an acknowledgement: 39214734Srpaulo * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40214734Srpaulo * 41252726Srpaulo * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42214734Srpaulo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43252726Srpaulo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44252726Srpaulo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45214734Srpaulo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46214734Srpaulo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47252726Srpaulo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48214734Srpaulo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49214734Srpaulo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50252726Srpaulo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51252726Srpaulo * SUCH DAMAGE. 52189251Ssam * 53189251Ssam * The licence and distribution terms for any publically available version or 54189251Ssam * derivative of this code cannot be changed. i.e. this code cannot simply be 55252726Srpaulo * copied and put under another distribution licence 56189251Ssam * [including the GNU Public Licence.] 57189251Ssam */ 58252726Srpaulo 59252726Srpaulo#include <stdio.h> 60189251Ssam#include <stdlib.h> 61189251Ssam#include <string.h> 62189251Ssam#include "apps.h" 63189251Ssam#include <openssl/bio.h> 64189251Ssam#include <openssl/err.h> 65189251Ssam#include <openssl/x509.h> 66189251Ssam#include <openssl/x509v3.h> 67189251Ssam#include <openssl/pem.h> 68189251Ssam 69252726Srpaulo#undef PROG 70189251Ssam#define PROG verify_main 71252726Srpaulo 72189251Ssamstatic int MS_CALLBACK cb(int ok, X509_STORE_CTX *ctx); 73189251Ssamstatic int check(X509_STORE *ctx, char *file, 74189251Ssam STACK_OF(X509) *uchain, STACK_OF(X509) *tchain, 75189251Ssam STACK_OF(X509_CRL) *crls, ENGINE *e); 76189251Ssamstatic int v_verbose = 0, vflags = 0; 77189251Ssam 78189251Ssamint MAIN(int, char **); 79189251Ssam 80189251Ssamint MAIN(int argc, char **argv) 81189251Ssam{ 82189251Ssam ENGINE *e = NULL; 83189251Ssam int i, ret = 1, badarg = 0; 84189251Ssam char *CApath = NULL, *CAfile = NULL; 85189251Ssam char *untfile = NULL, *trustfile = NULL, *crlfile = NULL; 86189251Ssam STACK_OF(X509) *untrusted = NULL, *trusted = NULL; 87189251Ssam STACK_OF(X509_CRL) *crls = NULL; 88189251Ssam X509_STORE *cert_ctx = NULL; 89189251Ssam X509_LOOKUP *lookup = NULL; 90189251Ssam X509_VERIFY_PARAM *vpm = NULL; 91189251Ssam int crl_download = 0; 92189251Ssam#ifndef OPENSSL_NO_ENGINE 93189251Ssam char *engine = NULL; 94189251Ssam#endif 95189251Ssam 96189251Ssam cert_ctx = X509_STORE_new(); 97189251Ssam if (cert_ctx == NULL) 98189251Ssam goto end; 99189251Ssam X509_STORE_set_verify_cb(cert_ctx, cb); 100189251Ssam 101189251Ssam ERR_load_crypto_strings(); 102189251Ssam 103189251Ssam apps_startup(); 104189251Ssam 105189251Ssam if (bio_err == NULL) 106189251Ssam if ((bio_err = BIO_new(BIO_s_file())) != NULL) 107189251Ssam BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT); 108214734Srpaulo 109189251Ssam if (!load_config(bio_err, NULL)) 110189251Ssam goto end; 111214734Srpaulo 112189251Ssam argc--; 113189251Ssam argv++; 114189251Ssam for (;;) { 115189251Ssam if (argc >= 1) { 116189251Ssam if (strcmp(*argv, "-CApath") == 0) { 117189251Ssam if (argc-- < 1) 118189251Ssam goto usage; 119189251Ssam CApath = *(++argv); 120252726Srpaulo } else if (strcmp(*argv, "-CAfile") == 0) { 121252726Srpaulo if (argc-- < 1) 122189251Ssam goto usage; 123189251Ssam CAfile = *(++argv); 124189251Ssam } else if (args_verify(&argv, &argc, &badarg, bio_err, &vpm)) { 125189251Ssam if (badarg) 126189251Ssam goto usage; 127189251Ssam continue; 128189251Ssam } else if (strcmp(*argv, "-untrusted") == 0) { 129189251Ssam if (argc-- < 1) 130189251Ssam goto usage; 131189251Ssam untfile = *(++argv); 132189251Ssam } else if (strcmp(*argv, "-trusted") == 0) { 133189251Ssam if (argc-- < 1) 134214734Srpaulo goto usage; 135189251Ssam trustfile = *(++argv); 136189251Ssam } else if (strcmp(*argv, "-CRLfile") == 0) { 137189251Ssam if (argc-- < 1) 138189251Ssam goto usage; 139189251Ssam crlfile = *(++argv); 140214734Srpaulo } else if (strcmp(*argv, "-crl_download") == 0) 141252726Srpaulo crl_download = 1; 142252726Srpaulo#ifndef OPENSSL_NO_ENGINE 143189251Ssam else if (strcmp(*argv, "-engine") == 0) { 144189251Ssam if (--argc < 1) 145189251Ssam goto usage; 146189251Ssam engine = *(++argv); 147252726Srpaulo } 148252726Srpaulo#endif 149189251Ssam else if (strcmp(*argv, "-help") == 0) 150189251Ssam goto usage; 151189251Ssam else if (strcmp(*argv, "-verbose") == 0) 152189251Ssam v_verbose = 1; 153189251Ssam else if (argv[0][0] == '-') 154189251Ssam goto usage; 155189251Ssam else 156189251Ssam break; 157189251Ssam argc--; 158252726Srpaulo argv++; 159252726Srpaulo } else 160252726Srpaulo break; 161252726Srpaulo } 162252726Srpaulo 163189251Ssam#ifndef OPENSSL_NO_ENGINE 164189251Ssam e = setup_engine(bio_err, engine, 0); 165189251Ssam#endif 166189251Ssam 167189251Ssam if (vpm) 168189251Ssam X509_STORE_set1_param(cert_ctx, vpm); 169189251Ssam 170189251Ssam lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_file()); 171252726Srpaulo if (lookup == NULL) 172252726Srpaulo abort(); 173189251Ssam if (CAfile) { 174189251Ssam i = X509_LOOKUP_load_file(lookup, CAfile, X509_FILETYPE_PEM); 175189251Ssam if (!i) { 176189251Ssam BIO_printf(bio_err, "Error loading file %s\n", CAfile); 177189251Ssam ERR_print_errors(bio_err); 178189251Ssam goto end; 179252726Srpaulo } 180189251Ssam } else 181189251Ssam X509_LOOKUP_load_file(lookup, NULL, X509_FILETYPE_DEFAULT); 182189251Ssam 183189251Ssam lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_hash_dir()); 184189251Ssam if (lookup == NULL) 185189251Ssam abort(); 186189251Ssam if (CApath) { 187189251Ssam i = X509_LOOKUP_add_dir(lookup, CApath, X509_FILETYPE_PEM); 188189251Ssam if (!i) { 189189251Ssam BIO_printf(bio_err, "Error loading directory %s\n", CApath); 190189251Ssam ERR_print_errors(bio_err); 191189251Ssam goto end; 192189251Ssam } 193252726Srpaulo } else 194189251Ssam X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT); 195252726Srpaulo 196252726Srpaulo ERR_clear_error(); 197252726Srpaulo 198252726Srpaulo if (untfile) { 199252726Srpaulo untrusted = load_certs(bio_err, untfile, FORMAT_PEM, 200252726Srpaulo NULL, e, "untrusted certificates"); 201252726Srpaulo if (!untrusted) 202252726Srpaulo goto end; 203252726Srpaulo } 204252726Srpaulo 205252726Srpaulo if (trustfile) { 206252726Srpaulo trusted = load_certs(bio_err, trustfile, FORMAT_PEM, 207252726Srpaulo NULL, e, "trusted certificates"); 208252726Srpaulo if (!trusted) 209252726Srpaulo goto end; 210252726Srpaulo } 211252726Srpaulo 212189251Ssam if (crlfile) { 213189251Ssam crls = load_crls(bio_err, crlfile, FORMAT_PEM, NULL, e, "other CRLs"); 214189251Ssam if (!crls) 215189251Ssam goto end; 216189251Ssam } 217189251Ssam 218189251Ssam ret = 0; 219189251Ssam 220189251Ssam if (crl_download) 221189251Ssam store_setup_crl_download(cert_ctx); 222189251Ssam if (argc < 1) { 223189251Ssam if (1 != check(cert_ctx, NULL, untrusted, trusted, crls, e)) 224189251Ssam ret = -1; 225189251Ssam } else { 226189251Ssam for (i = 0; i < argc; i++) 227189251Ssam if (1 != check(cert_ctx, argv[i], untrusted, trusted, crls, e)) 228214734Srpaulo ret = -1; 229189251Ssam } 230189251Ssam 231252726Srpaulo usage: 232189251Ssam if (ret == 1) { 233189251Ssam BIO_printf(bio_err, 234189251Ssam "usage: verify [-verbose] [-CApath path] [-CAfile file] [-purpose purpose] [-crl_check]"); 235189251Ssam BIO_printf(bio_err, " [-no_alt_chains] [-attime timestamp]"); 236189251Ssam#ifndef OPENSSL_NO_ENGINE 237189251Ssam BIO_printf(bio_err, " [-engine e]"); 238189251Ssam#endif 239189251Ssam BIO_printf(bio_err, " cert1 cert2 ...\n"); 240189251Ssam 241189251Ssam BIO_printf(bio_err, "recognized usages:\n"); 242189251Ssam for (i = 0; i < X509_PURPOSE_get_count(); i++) { 243189251Ssam X509_PURPOSE *ptmp; 244189251Ssam ptmp = X509_PURPOSE_get0(i); 245189251Ssam BIO_printf(bio_err, "\t%-10s\t%s\n", 246189251Ssam X509_PURPOSE_get0_sname(ptmp), 247189251Ssam X509_PURPOSE_get0_name(ptmp)); 248252726Srpaulo } 249189251Ssam } 250189251Ssam end: 251189251Ssam if (vpm) 252189251Ssam X509_VERIFY_PARAM_free(vpm); 253189251Ssam if (cert_ctx != NULL) 254189251Ssam X509_STORE_free(cert_ctx); 255189251Ssam sk_X509_pop_free(untrusted, X509_free); 256189251Ssam sk_X509_pop_free(trusted, X509_free); 257189251Ssam sk_X509_CRL_pop_free(crls, X509_CRL_free); 258189251Ssam apps_shutdown(); 259189251Ssam OPENSSL_EXIT(ret < 0 ? 2 : ret); 260189251Ssam} 261189251Ssam 262189251Ssamstatic int check(X509_STORE *ctx, char *file, 263189251Ssam STACK_OF(X509) *uchain, STACK_OF(X509) *tchain, 264189251Ssam STACK_OF(X509_CRL) *crls, ENGINE *e) 265189251Ssam{ 266189251Ssam X509 *x = NULL; 267214734Srpaulo int i = 0, ret = 0; 268214734Srpaulo X509_STORE_CTX *csc; 269214734Srpaulo 270214734Srpaulo x = load_cert(bio_err, file, FORMAT_PEM, NULL, e, "certificate file"); 271214734Srpaulo if (x == NULL) 272214734Srpaulo goto end; 273214734Srpaulo fprintf(stdout, "%s: ", (file == NULL) ? "stdin" : file); 274214734Srpaulo 275214734Srpaulo csc = X509_STORE_CTX_new(); 276214734Srpaulo if (csc == NULL) { 277214734Srpaulo ERR_print_errors(bio_err); 278214734Srpaulo goto end; 279214734Srpaulo } 280214734Srpaulo X509_STORE_set_flags(ctx, vflags); 281214734Srpaulo if (!X509_STORE_CTX_init(csc, ctx, x, uchain)) { 282189251Ssam ERR_print_errors(bio_err); 283189251Ssam goto end; 284189251Ssam } 285189251Ssam if (tchain) 286189251Ssam X509_STORE_CTX_trusted_stack(csc, tchain); 287189251Ssam if (crls) 288189251Ssam X509_STORE_CTX_set0_crls(csc, crls); 289189251Ssam i = X509_verify_cert(csc); 290189251Ssam X509_STORE_CTX_free(csc); 291189251Ssam 292189251Ssam ret = 0; 293189251Ssam end: 294189251Ssam if (i > 0) { 295189251Ssam fprintf(stdout, "OK\n"); 296189251Ssam ret = 1; 297189251Ssam } else 298189251Ssam ERR_print_errors(bio_err); 299189251Ssam if (x != NULL) 300189251Ssam X509_free(x); 301189251Ssam 302189251Ssam return (ret); 303214734Srpaulo} 304189251Ssam 305189251Ssamstatic int MS_CALLBACK cb(int ok, X509_STORE_CTX *ctx) 306189251Ssam{ 307189251Ssam int cert_error = X509_STORE_CTX_get_error(ctx); 308189251Ssam X509 *current_cert = X509_STORE_CTX_get_current_cert(ctx); 309189251Ssam 310189251Ssam if (!ok) { 311189251Ssam if (current_cert) { 312189251Ssam X509_NAME_print_ex_fp(stdout, 313189251Ssam X509_get_subject_name(current_cert), 314189251Ssam 0, XN_FLAG_ONELINE); 315189251Ssam printf("\n"); 316189251Ssam } 317189251Ssam printf("%serror %d at %d depth lookup:%s\n", 318189251Ssam X509_STORE_CTX_get0_parent_ctx(ctx) ? "[CRL path]" : "", 319189251Ssam cert_error, 320189251Ssam X509_STORE_CTX_get_error_depth(ctx), 321189251Ssam X509_verify_cert_error_string(cert_error)); 322189251Ssam switch (cert_error) { 323189251Ssam case X509_V_ERR_NO_EXPLICIT_POLICY: 324189251Ssam policies_print(NULL, ctx); 325189251Ssam case X509_V_ERR_CERT_HAS_EXPIRED: 326189251Ssam 327189251Ssam /* 328189251Ssam * since we are just checking the certificates, it is ok if they 329189251Ssam * are self signed. But we should still warn the user. 330189251Ssam */ 331189251Ssam 332189251Ssam case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: 333189251Ssam /* Continue after extension errors too */ 334189251Ssam case X509_V_ERR_INVALID_CA: 335189251Ssam case X509_V_ERR_INVALID_NON_CA: 336189251Ssam case X509_V_ERR_PATH_LENGTH_EXCEEDED: 337189251Ssam case X509_V_ERR_INVALID_PURPOSE: 338189251Ssam case X509_V_ERR_CRL_HAS_EXPIRED: 339189251Ssam case X509_V_ERR_CRL_NOT_YET_VALID: 340189251Ssam case X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION: 341189251Ssam ok = 1; 342189251Ssam 343189251Ssam } 344189251Ssam 345189251Ssam return ok; 346189251Ssam 347189251Ssam } 348189251Ssam if (cert_error == X509_V_OK && ok == 2) 349189251Ssam policies_print(NULL, ctx); 350189251Ssam if (!v_verbose) 351189251Ssam ERR_clear_error(); 352189251Ssam return (ok); 353189251Ssam} 354189251Ssam