2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
14 #include <openssl/bio.h>
15 #include <openssl/err.h>
16 #include <openssl/x509.h>
17 #include <openssl/x509v3.h>
18 #include <openssl/pem.h>
20 static int cb(int ok, X509_STORE_CTX *ctx);
21 static int check(X509_STORE *ctx, const char *file,
22 STACK_OF(X509) *uchain, STACK_OF(X509) *tchain,
23 STACK_OF(X509_CRL) *crls, int show_chain);
24 static int v_verbose = 0, vflags = 0;
26 typedef enum OPTION_choice {
27 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
28 OPT_ENGINE, OPT_CAPATH, OPT_CAFILE, OPT_NOCAPATH, OPT_NOCAFILE,
29 OPT_UNTRUSTED, OPT_TRUSTED, OPT_CRLFILE, OPT_CRL_DOWNLOAD, OPT_SHOW_CHAIN,
30 OPT_V_ENUM, OPT_NAMEOPT,
34 const OPTIONS verify_options[] = {
35 {OPT_HELP_STR, 1, '-', "Usage: %s [options] cert.pem...\n"},
36 {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
37 {"help", OPT_HELP, '-', "Display this summary"},
38 {"verbose", OPT_VERBOSE, '-',
39 "Print extra information about the operations being performed."},
40 {"CApath", OPT_CAPATH, '/', "A directory of trusted certificates"},
41 {"CAfile", OPT_CAFILE, '<', "A file of trusted certificates"},
42 {"no-CAfile", OPT_NOCAFILE, '-',
43 "Do not load the default certificates file"},
44 {"no-CApath", OPT_NOCAPATH, '-',
45 "Do not load certificates from the default certificates directory"},
46 {"untrusted", OPT_UNTRUSTED, '<', "A file of untrusted certificates"},
47 {"trusted", OPT_TRUSTED, '<', "A file of trusted certificates"},
48 {"CRLfile", OPT_CRLFILE, '<',
49 "File containing one or more CRL's (in PEM format) to load"},
50 {"crl_download", OPT_CRL_DOWNLOAD, '-',
51 "Attempt to download CRL information for this certificate"},
52 {"show_chain", OPT_SHOW_CHAIN, '-',
53 "Display information about the certificate chain"},
54 {"nameopt", OPT_NAMEOPT, 's', "Various certificate name options"},
56 #ifndef OPENSSL_NO_ENGINE
57 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
62 int verify_main(int argc, char **argv)
65 STACK_OF(X509) *untrusted = NULL, *trusted = NULL;
66 STACK_OF(X509_CRL) *crls = NULL;
67 X509_STORE *store = NULL;
68 X509_VERIFY_PARAM *vpm = NULL;
69 const char *prog, *CApath = NULL, *CAfile = NULL;
70 int noCApath = 0, noCAfile = 0;
71 int vpmtouched = 0, crl_download = 0, show_chain = 0, i = 0, ret = 1;
74 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
77 prog = opt_init(argc, argv, verify_options);
78 while ((o = opt_next()) != OPT_EOF) {
82 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
85 opt_help(verify_options);
86 BIO_printf(bio_err, "Recognized usages:\n");
87 for (i = 0; i < X509_PURPOSE_get_count(); i++) {
89 ptmp = X509_PURPOSE_get0(i);
90 BIO_printf(bio_err, "\t%-10s\t%s\n",
91 X509_PURPOSE_get0_sname(ptmp),
92 X509_PURPOSE_get0_name(ptmp));
95 BIO_printf(bio_err, "Recognized verify names:\n");
96 for (i = 0; i < X509_VERIFY_PARAM_get_count(); i++) {
97 const X509_VERIFY_PARAM *vptmp;
98 vptmp = X509_VERIFY_PARAM_get0(i);
99 BIO_printf(bio_err, "\t%-10s\n",
100 X509_VERIFY_PARAM_get0_name(vptmp));
105 if (!opt_verify(o, vpm))
122 /* Zero or more times */
123 if (!load_certs(opt_arg(), &untrusted, FORMAT_PEM, NULL,
124 "untrusted certificates"))
128 /* Zero or more times */
131 if (!load_certs(opt_arg(), &trusted, FORMAT_PEM, NULL,
132 "trusted certificates"))
136 /* Zero or more times */
137 if (!load_crls(opt_arg(), &crls, FORMAT_PEM, NULL,
141 case OPT_CRL_DOWNLOAD:
145 if ((e = setup_engine(opt_arg(), 0)) == NULL) {
146 /* Failure message already displayed */
154 if (!set_nameopt(opt_arg()))
162 argc = opt_num_rest();
164 if (trusted != NULL && (CAfile || CApath)) {
166 "%s: Cannot use -trusted with -CAfile or -CApath\n",
171 if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath)) == NULL)
173 X509_STORE_set_verify_cb(store, cb);
176 X509_STORE_set1_param(store, vpm);
181 store_setup_crl_download(store);
185 if (check(store, NULL, untrusted, trusted, crls, show_chain) != 1)
188 for (i = 0; i < argc; i++)
189 if (check(store, argv[i], untrusted, trusted, crls,
195 X509_VERIFY_PARAM_free(vpm);
196 X509_STORE_free(store);
197 sk_X509_pop_free(untrusted, X509_free);
198 sk_X509_pop_free(trusted, X509_free);
199 sk_X509_CRL_pop_free(crls, X509_CRL_free);
201 return (ret < 0 ? 2 : ret);
204 static int check(X509_STORE *ctx, const char *file,
205 STACK_OF(X509) *uchain, STACK_OF(X509) *tchain,
206 STACK_OF(X509_CRL) *crls, int show_chain)
211 STACK_OF(X509) *chain = NULL;
214 x = load_cert(file, FORMAT_PEM, "certificate file");
218 csc = X509_STORE_CTX_new();
220 printf("error %s: X.509 store context allocation failed\n",
221 (file == NULL) ? "stdin" : file);
225 X509_STORE_set_flags(ctx, vflags);
226 if (!X509_STORE_CTX_init(csc, ctx, x, uchain)) {
227 printf("error %s: X.509 store context initialization failed\n",
228 (file == NULL) ? "stdin" : file);
232 X509_STORE_CTX_set0_trusted_stack(csc, tchain);
234 X509_STORE_CTX_set0_crls(csc, crls);
235 i = X509_verify_cert(csc);
236 if (i > 0 && X509_STORE_CTX_get_error(csc) == X509_V_OK) {
237 printf("%s: OK\n", (file == NULL) ? "stdin" : file);
242 chain = X509_STORE_CTX_get1_chain(csc);
243 num_untrusted = X509_STORE_CTX_get_num_untrusted(csc);
245 for (j = 0; j < sk_X509_num(chain); j++) {
246 X509 *cert = sk_X509_value(chain, j);
247 printf("depth=%d: ", j);
248 X509_NAME_print_ex_fp(stdout,
249 X509_get_subject_name(cert),
251 if (j < num_untrusted)
252 printf(" (untrusted)");
255 sk_X509_pop_free(chain, X509_free);
258 printf("error %s: verification failed\n", (file == NULL) ? "stdin" : file);
260 X509_STORE_CTX_free(csc);
264 ERR_print_errors(bio_err);
270 static int cb(int ok, X509_STORE_CTX *ctx)
272 int cert_error = X509_STORE_CTX_get_error(ctx);
273 X509 *current_cert = X509_STORE_CTX_get_current_cert(ctx);
276 if (current_cert != NULL) {
277 X509_NAME_print_ex(bio_err,
278 X509_get_subject_name(current_cert),
280 BIO_printf(bio_err, "\n");
282 BIO_printf(bio_err, "%serror %d at %d depth lookup: %s\n",
283 X509_STORE_CTX_get0_parent_ctx(ctx) ? "[CRL path] " : "",
285 X509_STORE_CTX_get_error_depth(ctx),
286 X509_verify_cert_error_string(cert_error));
287 switch (cert_error) {
288 case X509_V_ERR_NO_EXPLICIT_POLICY:
291 case X509_V_ERR_CERT_HAS_EXPIRED:
294 * since we are just checking the certificates, it is ok if they
295 * are self signed. But we should still warn the user.
297 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
298 /* Continue after extension errors too */
299 case X509_V_ERR_INVALID_CA:
300 case X509_V_ERR_INVALID_NON_CA:
301 case X509_V_ERR_PATH_LENGTH_EXCEEDED:
302 case X509_V_ERR_INVALID_PURPOSE:
303 case X509_V_ERR_CRL_HAS_EXPIRED:
304 case X509_V_ERR_CRL_NOT_YET_VALID:
305 case X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION:
312 if (cert_error == X509_V_OK && ok == 2)