2 * Copyright 1995-2019 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
15 #include <openssl/bio.h>
16 #include <openssl/err.h>
17 #include <openssl/x509.h>
18 #include <openssl/x509v3.h>
19 #include <openssl/pem.h>
21 static int cb(int ok, X509_STORE_CTX *ctx);
22 static int check(X509_STORE *ctx, const char *file,
23 STACK_OF(X509) *uchain, STACK_OF(X509) *tchain,
24 STACK_OF(X509_CRL) *crls, int show_chain);
25 static int v_verbose = 0, vflags = 0;
27 typedef enum OPTION_choice {
28 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
29 OPT_ENGINE, OPT_CAPATH, OPT_CAFILE, OPT_NOCAPATH, OPT_NOCAFILE,
30 OPT_UNTRUSTED, OPT_TRUSTED, OPT_CRLFILE, OPT_CRL_DOWNLOAD, OPT_SHOW_CHAIN,
31 OPT_V_ENUM, OPT_NAMEOPT,
35 const OPTIONS verify_options[] = {
36 {OPT_HELP_STR, 1, '-', "Usage: %s [options] cert.pem...\n"},
37 {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
38 {"help", OPT_HELP, '-', "Display this summary"},
39 {"verbose", OPT_VERBOSE, '-',
40 "Print extra information about the operations being performed."},
41 {"CApath", OPT_CAPATH, '/', "A directory of trusted certificates"},
42 {"CAfile", OPT_CAFILE, '<', "A file of trusted certificates"},
43 {"no-CAfile", OPT_NOCAFILE, '-',
44 "Do not load the default certificates file"},
45 {"no-CApath", OPT_NOCAPATH, '-',
46 "Do not load certificates from the default certificates directory"},
47 {"untrusted", OPT_UNTRUSTED, '<', "A file of untrusted certificates"},
48 {"trusted", OPT_TRUSTED, '<', "A file of trusted certificates"},
49 {"CRLfile", OPT_CRLFILE, '<',
50 "File containing one or more CRL's (in PEM format) to load"},
51 {"crl_download", OPT_CRL_DOWNLOAD, '-',
52 "Attempt to download CRL information for this certificate"},
53 {"show_chain", OPT_SHOW_CHAIN, '-',
54 "Display information about the certificate chain"},
55 {"nameopt", OPT_NAMEOPT, 's', "Various certificate name options"},
57 #ifndef OPENSSL_NO_ENGINE
58 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
63 int verify_main(int argc, char **argv)
66 STACK_OF(X509) *untrusted = NULL, *trusted = NULL;
67 STACK_OF(X509_CRL) *crls = NULL;
68 X509_STORE *store = NULL;
69 X509_VERIFY_PARAM *vpm = NULL;
70 const char *prog, *CApath = NULL, *CAfile = NULL;
71 int noCApath = 0, noCAfile = 0;
72 int vpmtouched = 0, crl_download = 0, show_chain = 0, i = 0, ret = 1;
75 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
78 prog = opt_init(argc, argv, verify_options);
79 while ((o = opt_next()) != OPT_EOF) {
83 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
86 opt_help(verify_options);
87 BIO_printf(bio_err, "Recognized usages:\n");
88 for (i = 0; i < X509_PURPOSE_get_count(); i++) {
90 ptmp = X509_PURPOSE_get0(i);
91 BIO_printf(bio_err, "\t%-10s\t%s\n",
92 X509_PURPOSE_get0_sname(ptmp),
93 X509_PURPOSE_get0_name(ptmp));
96 BIO_printf(bio_err, "Recognized verify names:\n");
97 for (i = 0; i < X509_VERIFY_PARAM_get_count(); i++) {
98 const X509_VERIFY_PARAM *vptmp;
99 vptmp = X509_VERIFY_PARAM_get0(i);
100 BIO_printf(bio_err, "\t%-10s\n",
101 X509_VERIFY_PARAM_get0_name(vptmp));
106 if (!opt_verify(o, vpm))
123 /* Zero or more times */
124 if (!load_certs(opt_arg(), &untrusted, FORMAT_PEM, NULL,
125 "untrusted certificates"))
129 /* Zero or more times */
132 if (!load_certs(opt_arg(), &trusted, FORMAT_PEM, NULL,
133 "trusted certificates"))
137 /* Zero or more times */
138 if (!load_crls(opt_arg(), &crls, FORMAT_PEM, NULL,
142 case OPT_CRL_DOWNLOAD:
146 if ((e = setup_engine(opt_arg(), 0)) == NULL) {
147 /* Failure message already displayed */
155 if (!set_nameopt(opt_arg()))
163 argc = opt_num_rest();
165 if (trusted != NULL && (CAfile || CApath)) {
167 "%s: Cannot use -trusted with -CAfile or -CApath\n",
172 if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath)) == NULL)
174 X509_STORE_set_verify_cb(store, cb);
177 X509_STORE_set1_param(store, vpm);
182 store_setup_crl_download(store);
186 if (check(store, NULL, untrusted, trusted, crls, show_chain) != 1)
189 for (i = 0; i < argc; i++)
190 if (check(store, argv[i], untrusted, trusted, crls,
196 X509_VERIFY_PARAM_free(vpm);
197 X509_STORE_free(store);
198 sk_X509_pop_free(untrusted, X509_free);
199 sk_X509_pop_free(trusted, X509_free);
200 sk_X509_CRL_pop_free(crls, X509_CRL_free);
202 return (ret < 0 ? 2 : ret);
205 static int check(X509_STORE *ctx, const char *file,
206 STACK_OF(X509) *uchain, STACK_OF(X509) *tchain,
207 STACK_OF(X509_CRL) *crls, int show_chain)
212 STACK_OF(X509) *chain = NULL;
215 x = load_cert(file, FORMAT_PEM, "certificate file");
219 csc = X509_STORE_CTX_new();
221 printf("error %s: X.509 store context allocation failed\n",
222 (file == NULL) ? "stdin" : file);
226 X509_STORE_set_flags(ctx, vflags);
227 if (!X509_STORE_CTX_init(csc, ctx, x, uchain)) {
228 X509_STORE_CTX_free(csc);
229 printf("error %s: X.509 store context initialization failed\n",
230 (file == NULL) ? "stdin" : file);
234 X509_STORE_CTX_set0_trusted_stack(csc, tchain);
236 X509_STORE_CTX_set0_crls(csc, crls);
237 i = X509_verify_cert(csc);
238 if (i > 0 && X509_STORE_CTX_get_error(csc) == X509_V_OK) {
239 printf("%s: OK\n", (file == NULL) ? "stdin" : file);
244 chain = X509_STORE_CTX_get1_chain(csc);
245 num_untrusted = X509_STORE_CTX_get_num_untrusted(csc);
247 for (j = 0; j < sk_X509_num(chain); j++) {
248 X509 *cert = sk_X509_value(chain, j);
249 printf("depth=%d: ", j);
250 X509_NAME_print_ex_fp(stdout,
251 X509_get_subject_name(cert),
253 if (j < num_untrusted)
254 printf(" (untrusted)");
257 sk_X509_pop_free(chain, X509_free);
260 printf("error %s: verification failed\n", (file == NULL) ? "stdin" : file);
262 X509_STORE_CTX_free(csc);
266 ERR_print_errors(bio_err);
272 static int cb(int ok, X509_STORE_CTX *ctx)
274 int cert_error = X509_STORE_CTX_get_error(ctx);
275 X509 *current_cert = X509_STORE_CTX_get_current_cert(ctx);
278 if (current_cert != NULL) {
279 X509_NAME_print_ex(bio_err,
280 X509_get_subject_name(current_cert),
282 BIO_printf(bio_err, "\n");
284 BIO_printf(bio_err, "%serror %d at %d depth lookup: %s\n",
285 X509_STORE_CTX_get0_parent_ctx(ctx) ? "[CRL path] " : "",
287 X509_STORE_CTX_get_error_depth(ctx),
288 X509_verify_cert_error_string(cert_error));
291 * Pretend that some errors are ok, so they don't stop further
292 * processing of the certificate chain. Setting ok = 1 does this.
293 * After X509_verify_cert() is done, we verify that there were
294 * no actual errors, even if the returned value was positive.
296 switch (cert_error) {
297 case X509_V_ERR_NO_EXPLICIT_POLICY:
300 case X509_V_ERR_CERT_HAS_EXPIRED:
301 /* Continue even if the leaf is a self signed cert */
302 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
303 /* Continue after extension errors too */
304 case X509_V_ERR_INVALID_CA:
305 case X509_V_ERR_INVALID_NON_CA:
306 case X509_V_ERR_PATH_LENGTH_EXCEEDED:
307 case X509_V_ERR_INVALID_PURPOSE:
308 case X509_V_ERR_CRL_HAS_EXPIRED:
309 case X509_V_ERR_CRL_NOT_YET_VALID:
310 case X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION:
317 if (cert_error == X509_V_OK && ok == 2)