174664626SKris Kennaway /* apps/verify.c */ 274664626SKris Kennaway /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 374664626SKris Kennaway * All rights reserved. 474664626SKris Kennaway * 574664626SKris Kennaway * This package is an SSL implementation written 674664626SKris Kennaway * by Eric Young (eay@cryptsoft.com). 774664626SKris Kennaway * The implementation was written so as to conform with Netscapes SSL. 874664626SKris Kennaway * 974664626SKris Kennaway * This library is free for commercial and non-commercial use as long as 1074664626SKris Kennaway * the following conditions are aheared to. The following conditions 1174664626SKris Kennaway * apply to all code found in this distribution, be it the RC4, RSA, 1274664626SKris Kennaway * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1374664626SKris Kennaway * included with this distribution is covered by the same copyright terms 1474664626SKris Kennaway * except that the holder is Tim Hudson (tjh@cryptsoft.com). 1574664626SKris Kennaway * 1674664626SKris Kennaway * Copyright remains Eric Young's, and as such any Copyright notices in 1774664626SKris Kennaway * the code are not to be removed. 1874664626SKris Kennaway * If this package is used in a product, Eric Young should be given attribution 1974664626SKris Kennaway * as the author of the parts of the library used. 2074664626SKris Kennaway * This can be in the form of a textual message at program startup or 2174664626SKris Kennaway * in documentation (online or textual) provided with the package. 2274664626SKris Kennaway * 2374664626SKris Kennaway * Redistribution and use in source and binary forms, with or without 2474664626SKris Kennaway * modification, are permitted provided that the following conditions 2574664626SKris Kennaway * are met: 2674664626SKris Kennaway * 1. Redistributions of source code must retain the copyright 2774664626SKris Kennaway * notice, this list of conditions and the following disclaimer. 2874664626SKris Kennaway * 2. Redistributions in binary form must reproduce the above copyright 2974664626SKris Kennaway * notice, this list of conditions and the following disclaimer in the 3074664626SKris Kennaway * documentation and/or other materials provided with the distribution. 3174664626SKris Kennaway * 3. All advertising materials mentioning features or use of this software 3274664626SKris Kennaway * must display the following acknowledgement: 3374664626SKris Kennaway * "This product includes cryptographic software written by 3474664626SKris Kennaway * Eric Young (eay@cryptsoft.com)" 3574664626SKris Kennaway * The word 'cryptographic' can be left out if the rouines from the library 3674664626SKris Kennaway * being used are not cryptographic related :-). 3774664626SKris Kennaway * 4. If you include any Windows specific code (or a derivative thereof) from 3874664626SKris Kennaway * the apps directory (application code) you must include an acknowledgement: 3974664626SKris Kennaway * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 4074664626SKris Kennaway * 4174664626SKris Kennaway * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4274664626SKris Kennaway * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4374664626SKris Kennaway * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4474664626SKris Kennaway * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4574664626SKris Kennaway * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4674664626SKris Kennaway * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4774664626SKris Kennaway * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4874664626SKris Kennaway * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4974664626SKris Kennaway * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5074664626SKris Kennaway * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5174664626SKris Kennaway * SUCH DAMAGE. 5274664626SKris Kennaway * 5374664626SKris Kennaway * The licence and distribution terms for any publically available version or 5474664626SKris Kennaway * derivative of this code cannot be changed. i.e. this code cannot simply be 5574664626SKris Kennaway * copied and put under another distribution licence 5674664626SKris Kennaway * [including the GNU Public Licence.] 5774664626SKris Kennaway */ 5874664626SKris Kennaway 5974664626SKris Kennaway #include <stdio.h> 6074664626SKris Kennaway #include <stdlib.h> 6174664626SKris Kennaway #include <string.h> 6274664626SKris Kennaway #include "apps.h" 6374664626SKris Kennaway #include <openssl/bio.h> 6474664626SKris Kennaway #include <openssl/err.h> 6574664626SKris Kennaway #include <openssl/x509.h> 66f579bf8eSKris Kennaway #include <openssl/x509v3.h> 6774664626SKris Kennaway #include <openssl/pem.h> 6874664626SKris Kennaway 6974664626SKris Kennaway #undef PROG 7074664626SKris Kennaway #define PROG verify_main 7174664626SKris Kennaway 7274664626SKris Kennaway static int MS_CALLBACK cb(int ok, X509_STORE_CTX *ctx); 73f579bf8eSKris Kennaway static int check(X509_STORE *ctx,char *file, STACK_OF(X509)*other, int purpose); 74f579bf8eSKris Kennaway static STACK_OF(X509) *load_untrusted(char *file); 7574664626SKris Kennaway static int v_verbose=0; 7674664626SKris Kennaway 77f579bf8eSKris Kennaway int MAIN(int, char **); 78f579bf8eSKris Kennaway 7974664626SKris Kennaway int MAIN(int argc, char **argv) 8074664626SKris Kennaway { 8174664626SKris Kennaway int i,ret=1; 82f579bf8eSKris Kennaway int purpose = -1; 8374664626SKris Kennaway char *CApath=NULL,*CAfile=NULL; 84f579bf8eSKris Kennaway char *untfile = NULL; 85f579bf8eSKris Kennaway STACK_OF(X509) *untrusted = NULL; 8674664626SKris Kennaway X509_STORE *cert_ctx=NULL; 8774664626SKris Kennaway X509_LOOKUP *lookup=NULL; 8874664626SKris Kennaway 8974664626SKris Kennaway cert_ctx=X509_STORE_new(); 9074664626SKris Kennaway if (cert_ctx == NULL) goto end; 9174664626SKris Kennaway X509_STORE_set_verify_cb_func(cert_ctx,cb); 9274664626SKris Kennaway 9374664626SKris Kennaway ERR_load_crypto_strings(); 9474664626SKris Kennaway 9574664626SKris Kennaway apps_startup(); 9674664626SKris Kennaway 9774664626SKris Kennaway if (bio_err == NULL) 9874664626SKris Kennaway if ((bio_err=BIO_new(BIO_s_file())) != NULL) 9974664626SKris Kennaway BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); 10074664626SKris Kennaway 10174664626SKris Kennaway argc--; 10274664626SKris Kennaway argv++; 10374664626SKris Kennaway for (;;) 10474664626SKris Kennaway { 10574664626SKris Kennaway if (argc >= 1) 10674664626SKris Kennaway { 10774664626SKris Kennaway if (strcmp(*argv,"-CApath") == 0) 10874664626SKris Kennaway { 10974664626SKris Kennaway if (argc-- < 1) goto end; 11074664626SKris Kennaway CApath= *(++argv); 11174664626SKris Kennaway } 11274664626SKris Kennaway else if (strcmp(*argv,"-CAfile") == 0) 11374664626SKris Kennaway { 11474664626SKris Kennaway if (argc-- < 1) goto end; 11574664626SKris Kennaway CAfile= *(++argv); 11674664626SKris Kennaway } 117f579bf8eSKris Kennaway else if (strcmp(*argv,"-purpose") == 0) 118f579bf8eSKris Kennaway { 119f579bf8eSKris Kennaway X509_PURPOSE *xptmp; 120f579bf8eSKris Kennaway if (argc-- < 1) goto end; 121f579bf8eSKris Kennaway i = X509_PURPOSE_get_by_sname(*(++argv)); 122f579bf8eSKris Kennaway if(i < 0) 123f579bf8eSKris Kennaway { 124f579bf8eSKris Kennaway BIO_printf(bio_err, "unrecognized purpose\n"); 125f579bf8eSKris Kennaway goto end; 126f579bf8eSKris Kennaway } 127f579bf8eSKris Kennaway xptmp = X509_PURPOSE_get0(i); 128f579bf8eSKris Kennaway purpose = X509_PURPOSE_get_id(xptmp); 129f579bf8eSKris Kennaway } 130f579bf8eSKris Kennaway else if (strcmp(*argv,"-untrusted") == 0) 131f579bf8eSKris Kennaway { 132f579bf8eSKris Kennaway if (argc-- < 1) goto end; 133f579bf8eSKris Kennaway untfile= *(++argv); 134f579bf8eSKris Kennaway } 13574664626SKris Kennaway else if (strcmp(*argv,"-help") == 0) 13674664626SKris Kennaway goto end; 13774664626SKris Kennaway else if (strcmp(*argv,"-verbose") == 0) 13874664626SKris Kennaway v_verbose=1; 13974664626SKris Kennaway else if (argv[0][0] == '-') 14074664626SKris Kennaway goto end; 14174664626SKris Kennaway else 14274664626SKris Kennaway break; 14374664626SKris Kennaway argc--; 14474664626SKris Kennaway argv++; 14574664626SKris Kennaway } 14674664626SKris Kennaway else 14774664626SKris Kennaway break; 14874664626SKris Kennaway } 14974664626SKris Kennaway 15074664626SKris Kennaway lookup=X509_STORE_add_lookup(cert_ctx,X509_LOOKUP_file()); 15174664626SKris Kennaway if (lookup == NULL) abort(); 152f579bf8eSKris Kennaway if (CAfile) { 153f579bf8eSKris Kennaway i=X509_LOOKUP_load_file(lookup,CAfile,X509_FILETYPE_PEM); 154f579bf8eSKris Kennaway if(!i) { 155f579bf8eSKris Kennaway BIO_printf(bio_err, "Error loading file %s\n", CAfile); 156f579bf8eSKris Kennaway ERR_print_errors(bio_err); 157f579bf8eSKris Kennaway goto end; 158f579bf8eSKris Kennaway } 159f579bf8eSKris Kennaway } else X509_LOOKUP_load_file(lookup,NULL,X509_FILETYPE_DEFAULT); 16074664626SKris Kennaway 16174664626SKris Kennaway lookup=X509_STORE_add_lookup(cert_ctx,X509_LOOKUP_hash_dir()); 16274664626SKris Kennaway if (lookup == NULL) abort(); 163f579bf8eSKris Kennaway if (CApath) { 164f579bf8eSKris Kennaway i=X509_LOOKUP_add_dir(lookup,CApath,X509_FILETYPE_PEM); 165f579bf8eSKris Kennaway if(!i) { 166f579bf8eSKris Kennaway BIO_printf(bio_err, "Error loading directory %s\n", CApath); 167f579bf8eSKris Kennaway ERR_print_errors(bio_err); 168f579bf8eSKris Kennaway goto end; 169f579bf8eSKris Kennaway } 170f579bf8eSKris Kennaway } else X509_LOOKUP_add_dir(lookup,NULL,X509_FILETYPE_DEFAULT); 17174664626SKris Kennaway 17274664626SKris Kennaway ERR_clear_error(); 173f579bf8eSKris Kennaway 174f579bf8eSKris Kennaway if(untfile) { 175f579bf8eSKris Kennaway if(!(untrusted = load_untrusted(untfile))) { 176f579bf8eSKris Kennaway BIO_printf(bio_err, "Error loading untrusted file %s\n", untfile); 177f579bf8eSKris Kennaway ERR_print_errors(bio_err); 178f579bf8eSKris Kennaway goto end; 179f579bf8eSKris Kennaway } 180f579bf8eSKris Kennaway } 181f579bf8eSKris Kennaway 182f579bf8eSKris Kennaway if (argc < 1) check(cert_ctx, NULL, untrusted, purpose); 18374664626SKris Kennaway else 18474664626SKris Kennaway for (i=0; i<argc; i++) 185f579bf8eSKris Kennaway check(cert_ctx,argv[i], untrusted, purpose); 18674664626SKris Kennaway ret=0; 18774664626SKris Kennaway end: 188f579bf8eSKris Kennaway if (ret == 1) { 18974664626SKris Kennaway BIO_printf(bio_err,"usage: verify [-verbose] [-CApath path] [-CAfile file] cert1 cert2 ...\n"); 190f579bf8eSKris Kennaway BIO_printf(bio_err,"recognized usages:\n"); 191f579bf8eSKris Kennaway for(i = 0; i < X509_PURPOSE_get_count(); i++) { 192f579bf8eSKris Kennaway X509_PURPOSE *ptmp; 193f579bf8eSKris Kennaway ptmp = X509_PURPOSE_get0(i); 194f579bf8eSKris Kennaway BIO_printf(bio_err, "\t%-10s\t%s\n", X509_PURPOSE_get0_sname(ptmp), 195f579bf8eSKris Kennaway X509_PURPOSE_get0_name(ptmp)); 196f579bf8eSKris Kennaway } 197f579bf8eSKris Kennaway } 19874664626SKris Kennaway if (cert_ctx != NULL) X509_STORE_free(cert_ctx); 199f579bf8eSKris Kennaway sk_X509_pop_free(untrusted, X509_free); 20074664626SKris Kennaway EXIT(ret); 20174664626SKris Kennaway } 20274664626SKris Kennaway 203f579bf8eSKris Kennaway static int check(X509_STORE *ctx, char *file, STACK_OF(X509) *uchain, int purpose) 20474664626SKris Kennaway { 20574664626SKris Kennaway X509 *x=NULL; 20674664626SKris Kennaway BIO *in=NULL; 20774664626SKris Kennaway int i=0,ret=0; 208f579bf8eSKris Kennaway X509_STORE_CTX *csc; 20974664626SKris Kennaway 21074664626SKris Kennaway in=BIO_new(BIO_s_file()); 21174664626SKris Kennaway if (in == NULL) 21274664626SKris Kennaway { 21374664626SKris Kennaway ERR_print_errors(bio_err); 21474664626SKris Kennaway goto end; 21574664626SKris Kennaway } 21674664626SKris Kennaway 21774664626SKris Kennaway if (file == NULL) 21874664626SKris Kennaway BIO_set_fp(in,stdin,BIO_NOCLOSE); 21974664626SKris Kennaway else 22074664626SKris Kennaway { 22174664626SKris Kennaway if (BIO_read_filename(in,file) <= 0) 22274664626SKris Kennaway { 22374664626SKris Kennaway perror(file); 22474664626SKris Kennaway goto end; 22574664626SKris Kennaway } 22674664626SKris Kennaway } 22774664626SKris Kennaway 22874664626SKris Kennaway x=PEM_read_bio_X509(in,NULL,NULL,NULL); 22974664626SKris Kennaway if (x == NULL) 23074664626SKris Kennaway { 23174664626SKris Kennaway fprintf(stdout,"%s: unable to load certificate file\n", 23274664626SKris Kennaway (file == NULL)?"stdin":file); 23374664626SKris Kennaway ERR_print_errors(bio_err); 23474664626SKris Kennaway goto end; 23574664626SKris Kennaway } 23674664626SKris Kennaway fprintf(stdout,"%s: ",(file == NULL)?"stdin":file); 23774664626SKris Kennaway 238f579bf8eSKris Kennaway csc = X509_STORE_CTX_new(); 239f579bf8eSKris Kennaway if (csc == NULL) 240f579bf8eSKris Kennaway { 241f579bf8eSKris Kennaway ERR_print_errors(bio_err); 242f579bf8eSKris Kennaway goto end; 243f579bf8eSKris Kennaway } 244f579bf8eSKris Kennaway X509_STORE_CTX_init(csc,ctx,x,uchain); 245f579bf8eSKris Kennaway if(purpose >= 0) X509_STORE_CTX_set_purpose(csc, purpose); 246f579bf8eSKris Kennaway i=X509_verify_cert(csc); 247f579bf8eSKris Kennaway X509_STORE_CTX_free(csc); 24874664626SKris Kennaway 24974664626SKris Kennaway ret=0; 25074664626SKris Kennaway end: 25174664626SKris Kennaway if (i) 25274664626SKris Kennaway { 25374664626SKris Kennaway fprintf(stdout,"OK\n"); 25474664626SKris Kennaway ret=1; 25574664626SKris Kennaway } 25674664626SKris Kennaway else 25774664626SKris Kennaway ERR_print_errors(bio_err); 25874664626SKris Kennaway if (x != NULL) X509_free(x); 25974664626SKris Kennaway if (in != NULL) BIO_free(in); 26074664626SKris Kennaway 26174664626SKris Kennaway return(ret); 26274664626SKris Kennaway } 26374664626SKris Kennaway 264f579bf8eSKris Kennaway static STACK_OF(X509) *load_untrusted(char *certfile) 265f579bf8eSKris Kennaway { 266f579bf8eSKris Kennaway STACK_OF(X509_INFO) *sk=NULL; 267f579bf8eSKris Kennaway STACK_OF(X509) *stack=NULL, *ret=NULL; 268f579bf8eSKris Kennaway BIO *in=NULL; 269f579bf8eSKris Kennaway X509_INFO *xi; 270f579bf8eSKris Kennaway 271f579bf8eSKris Kennaway if(!(stack = sk_X509_new_null())) { 272f579bf8eSKris Kennaway BIO_printf(bio_err,"memory allocation failure\n"); 273f579bf8eSKris Kennaway goto end; 274f579bf8eSKris Kennaway } 275f579bf8eSKris Kennaway 276f579bf8eSKris Kennaway if(!(in=BIO_new_file(certfile, "r"))) { 277f579bf8eSKris Kennaway BIO_printf(bio_err,"error opening the file, %s\n",certfile); 278f579bf8eSKris Kennaway goto end; 279f579bf8eSKris Kennaway } 280f579bf8eSKris Kennaway 281f579bf8eSKris Kennaway /* This loads from a file, a stack of x509/crl/pkey sets */ 282f579bf8eSKris Kennaway if(!(sk=PEM_X509_INFO_read_bio(in,NULL,NULL,NULL))) { 283f579bf8eSKris Kennaway BIO_printf(bio_err,"error reading the file, %s\n",certfile); 284f579bf8eSKris Kennaway goto end; 285f579bf8eSKris Kennaway } 286f579bf8eSKris Kennaway 287f579bf8eSKris Kennaway /* scan over it and pull out the certs */ 288f579bf8eSKris Kennaway while (sk_X509_INFO_num(sk)) 289f579bf8eSKris Kennaway { 290f579bf8eSKris Kennaway xi=sk_X509_INFO_shift(sk); 291f579bf8eSKris Kennaway if (xi->x509 != NULL) 292f579bf8eSKris Kennaway { 293f579bf8eSKris Kennaway sk_X509_push(stack,xi->x509); 294f579bf8eSKris Kennaway xi->x509=NULL; 295f579bf8eSKris Kennaway } 296f579bf8eSKris Kennaway X509_INFO_free(xi); 297f579bf8eSKris Kennaway } 298f579bf8eSKris Kennaway if(!sk_X509_num(stack)) { 299f579bf8eSKris Kennaway BIO_printf(bio_err,"no certificates in file, %s\n",certfile); 300f579bf8eSKris Kennaway sk_X509_free(stack); 301f579bf8eSKris Kennaway goto end; 302f579bf8eSKris Kennaway } 303f579bf8eSKris Kennaway ret=stack; 304f579bf8eSKris Kennaway end: 305f579bf8eSKris Kennaway BIO_free(in); 306f579bf8eSKris Kennaway sk_X509_INFO_free(sk); 307f579bf8eSKris Kennaway return(ret); 308f579bf8eSKris Kennaway } 309f579bf8eSKris Kennaway 31074664626SKris Kennaway static int MS_CALLBACK cb(int ok, X509_STORE_CTX *ctx) 31174664626SKris Kennaway { 31274664626SKris Kennaway char buf[256]; 31374664626SKris Kennaway 31474664626SKris Kennaway if (!ok) 31574664626SKris Kennaway { 31674664626SKris Kennaway X509_NAME_oneline( 31774664626SKris Kennaway X509_get_subject_name(ctx->current_cert),buf,256); 31874664626SKris Kennaway printf("%s\n",buf); 31974664626SKris Kennaway printf("error %d at %d depth lookup:%s\n",ctx->error, 32074664626SKris Kennaway ctx->error_depth, 32174664626SKris Kennaway X509_verify_cert_error_string(ctx->error)); 322f579bf8eSKris Kennaway if (ctx->error == X509_V_ERR_CERT_HAS_EXPIRED) ok=1; 323f579bf8eSKris Kennaway /* since we are just checking the certificates, it is 324f579bf8eSKris Kennaway * ok if they are self signed. But we should still warn 325f579bf8eSKris Kennaway * the user. 326f579bf8eSKris Kennaway */ 327f579bf8eSKris Kennaway if (ctx->error == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) ok=1; 328f579bf8eSKris Kennaway /* Continue after extension errors too */ 329f579bf8eSKris Kennaway if (ctx->error == X509_V_ERR_INVALID_CA) ok=1; 330f579bf8eSKris Kennaway if (ctx->error == X509_V_ERR_PATH_LENGTH_EXCEEDED) ok=1; 331f579bf8eSKris Kennaway if (ctx->error == X509_V_ERR_INVALID_PURPOSE) ok=1; 332f579bf8eSKris Kennaway if (ctx->error == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) ok=1; 33374664626SKris Kennaway } 33474664626SKris Kennaway if (!v_verbose) 33574664626SKris Kennaway ERR_clear_error(); 33674664626SKris Kennaway return(ok); 33774664626SKris Kennaway } 33874664626SKris Kennaway 339