2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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>
22 DEFINE_STACK_OF(X509_CRL)
23 DEFINE_STACK_OF_STRING()
25 static int cb(int ok, X509_STORE_CTX *ctx);
26 static int check(X509_STORE *ctx, const char *file,
27 STACK_OF(X509) *uchain, STACK_OF(X509) *tchain,
28 STACK_OF(X509_CRL) *crls, int show_chain,
29 STACK_OF(OPENSSL_STRING) *opts);
30 static int v_verbose = 0, vflags = 0;
32 typedef enum OPTION_choice {
33 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
34 OPT_ENGINE, OPT_CAPATH, OPT_CAFILE, OPT_CASTORE,
35 OPT_NOCAPATH, OPT_NOCAFILE, OPT_NOCASTORE,
36 OPT_UNTRUSTED, OPT_TRUSTED, OPT_CRLFILE, OPT_CRL_DOWNLOAD, OPT_SHOW_CHAIN,
37 OPT_V_ENUM, OPT_NAMEOPT, OPT_VFYOPT,
42 const OPTIONS verify_options[] = {
43 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [cert...]\n"},
45 OPT_SECTION("General"),
46 {"help", OPT_HELP, '-', "Display this summary"},
47 #ifndef OPENSSL_NO_ENGINE
48 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
50 {"verbose", OPT_VERBOSE, '-',
51 "Print extra information about the operations being performed."},
52 {"nameopt", OPT_NAMEOPT, 's', "Certificate subject/issuer name printing options"},
54 OPT_SECTION("Certificate chain"),
55 {"trusted", OPT_TRUSTED, '<', "A file of trusted certificates"},
56 {"CAfile", OPT_CAFILE, '<', "A file of trusted certificates"},
57 {"CApath", OPT_CAPATH, '/', "A directory of files with trusted certificates"},
58 {"CAstore", OPT_CASTORE, ':', "URI to a store of trusted certificates"},
59 {"no-CAfile", OPT_NOCAFILE, '-',
60 "Do not load the default trusted certificates file"},
61 {"no-CApath", OPT_NOCAPATH, '-',
62 "Do not load trusted certificates from the default directory"},
63 {"no-CAstore", OPT_NOCAPATH, '-',
64 "Do not load trusted certificates from the default certificates store"},
65 {"untrusted", OPT_UNTRUSTED, '<', "A file of untrusted certificates"},
66 {"CRLfile", OPT_CRLFILE, '<',
67 "File containing one or more CRL's (in PEM format) to load"},
68 {"crl_download", OPT_CRL_DOWNLOAD, '-',
69 "Try downloading CRL information for certificates via their CDP entries"},
70 {"show_chain", OPT_SHOW_CHAIN, '-',
71 "Display information about the certificate chain"},
74 {"vfyopt", OPT_VFYOPT, 's', "Verification parameter in n:v form"},
79 {"cert", 0, 0, "Certificate(s) to verify (optional; stdin used otherwise)"},
83 int verify_main(int argc, char **argv)
86 STACK_OF(X509) *untrusted = NULL, *trusted = NULL;
87 STACK_OF(X509_CRL) *crls = NULL;
88 STACK_OF(OPENSSL_STRING) *vfyopts = NULL;
89 X509_STORE *store = NULL;
90 X509_VERIFY_PARAM *vpm = NULL;
91 const char *prog, *CApath = NULL, *CAfile = NULL, *CAstore = NULL;
92 int noCApath = 0, noCAfile = 0, noCAstore = 0;
93 int vpmtouched = 0, crl_download = 0, show_chain = 0, i = 0, ret = 1;
96 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
99 prog = opt_init(argc, argv, verify_options);
100 while ((o = opt_next()) != OPT_EOF) {
105 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
108 opt_help(verify_options);
109 BIO_printf(bio_err, "\nRecognized certificate chain purposes:\n");
110 for (i = 0; i < X509_PURPOSE_get_count(); i++) {
111 X509_PURPOSE *ptmp = X509_PURPOSE_get0(i);
113 BIO_printf(bio_err, " %-15s %s\n",
114 X509_PURPOSE_get0_sname(ptmp),
115 X509_PURPOSE_get0_name(ptmp));
118 BIO_printf(bio_err, "Recognized certificate policy names:\n");
119 for (i = 0; i < X509_VERIFY_PARAM_get_count(); i++) {
120 const X509_VERIFY_PARAM *vptmp = X509_VERIFY_PARAM_get0(i);
122 BIO_printf(bio_err, " %s\n",
123 X509_VERIFY_PARAM_get0_name(vptmp));
128 if (!opt_verify(o, vpm))
151 /* Zero or more times */
152 if (!load_certs(opt_arg(), &untrusted, FORMAT_PEM, NULL,
153 "untrusted certificates"))
157 /* Zero or more times */
161 if (!load_certs(opt_arg(), &trusted, FORMAT_PEM, NULL,
162 "trusted certificates"))
166 /* Zero or more times */
167 if (!load_crls(opt_arg(), &crls, FORMAT_PEM, NULL,
171 case OPT_CRL_DOWNLOAD:
175 if ((e = setup_engine(opt_arg(), 0)) == NULL) {
176 /* Failure message already displayed */
184 if (!set_nameopt(opt_arg()))
189 vfyopts = sk_OPENSSL_STRING_new_null();
190 if (!vfyopts || !sk_OPENSSL_STRING_push(vfyopts, opt_arg()))
197 if (!opt_provider(o))
202 argc = opt_num_rest();
205 && (CAfile != NULL || CApath != NULL || CAstore != NULL)) {
207 "%s: Cannot use -trusted with -CAfile, -CApath or -CAstore\n",
212 if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
213 CAstore, noCAstore)) == NULL)
215 X509_STORE_set_verify_cb(store, cb);
218 X509_STORE_set1_param(store, vpm);
223 store_setup_crl_download(store);
227 if (check(store, NULL, untrusted, trusted, crls, show_chain,
231 for (i = 0; i < argc; i++)
232 if (check(store, argv[i], untrusted, trusted, crls, show_chain,
238 X509_VERIFY_PARAM_free(vpm);
239 X509_STORE_free(store);
240 sk_X509_pop_free(untrusted, X509_free);
241 sk_X509_pop_free(trusted, X509_free);
242 sk_X509_CRL_pop_free(crls, X509_CRL_free);
243 sk_OPENSSL_STRING_free(vfyopts);
245 return (ret < 0 ? 2 : ret);
248 static int check(X509_STORE *ctx, const char *file,
249 STACK_OF(X509) *uchain, STACK_OF(X509) *tchain,
250 STACK_OF(X509_CRL) *crls, int show_chain,
251 STACK_OF(OPENSSL_STRING) *opts)
256 STACK_OF(X509) *chain = NULL;
259 x = load_cert(file, FORMAT_UNDEF, "certificate file");
264 for (i = 0; i < sk_OPENSSL_STRING_num(opts); i++) {
265 char *opt = sk_OPENSSL_STRING_value(opts, i);
266 if (x509_ctrl_string(x, opt) <= 0) {
267 BIO_printf(bio_err, "parameter error \"%s\"\n", opt);
268 ERR_print_errors(bio_err);
274 csc = X509_STORE_CTX_new();
276 BIO_printf(bio_err, "error %s: X.509 store context allocation failed\n",
277 (file == NULL) ? "stdin" : file);
281 X509_STORE_set_flags(ctx, vflags);
282 if (!X509_STORE_CTX_init(csc, ctx, x, uchain)) {
283 X509_STORE_CTX_free(csc);
285 "error %s: X.509 store context initialization failed\n",
286 (file == NULL) ? "stdin" : file);
290 X509_STORE_CTX_set0_trusted_stack(csc, tchain);
292 X509_STORE_CTX_set0_crls(csc, crls);
293 i = X509_verify_cert(csc);
294 if (i > 0 && X509_STORE_CTX_get_error(csc) == X509_V_OK) {
295 BIO_printf(bio_out, "%s: OK\n", (file == NULL) ? "stdin" : file);
300 chain = X509_STORE_CTX_get1_chain(csc);
301 num_untrusted = X509_STORE_CTX_get_num_untrusted(csc);
302 BIO_printf(bio_out, "Chain:\n");
303 for (j = 0; j < sk_X509_num(chain); j++) {
304 X509 *cert = sk_X509_value(chain, j);
305 BIO_printf(bio_out, "depth=%d: ", j);
306 X509_NAME_print_ex_fp(stdout,
307 X509_get_subject_name(cert),
309 if (j < num_untrusted)
310 BIO_printf(bio_out, " (untrusted)");
311 BIO_printf(bio_out, "\n");
313 sk_X509_pop_free(chain, X509_free);
317 "error %s: verification failed\n",
318 (file == NULL) ? "stdin" : file);
320 X509_STORE_CTX_free(csc);
324 ERR_print_errors(bio_err);
330 static int cb(int ok, X509_STORE_CTX *ctx)
332 int cert_error = X509_STORE_CTX_get_error(ctx);
333 X509 *current_cert = X509_STORE_CTX_get_current_cert(ctx);
336 if (current_cert != NULL) {
337 X509_NAME_print_ex(bio_err,
338 X509_get_subject_name(current_cert),
340 BIO_printf(bio_err, "\n");
342 BIO_printf(bio_err, "%serror %d at %d depth lookup: %s\n",
343 X509_STORE_CTX_get0_parent_ctx(ctx) ? "[CRL path] " : "",
345 X509_STORE_CTX_get_error_depth(ctx),
346 X509_verify_cert_error_string(cert_error));
349 * Pretend that some errors are ok, so they don't stop further
350 * processing of the certificate chain. Setting ok = 1 does this.
351 * After X509_verify_cert() is done, we verify that there were
352 * no actual errors, even if the returned value was positive.
354 switch (cert_error) {
355 case X509_V_ERR_NO_EXPLICIT_POLICY:
358 case X509_V_ERR_CERT_HAS_EXPIRED:
359 /* Continue even if the leaf is a self signed cert */
360 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
361 /* Continue after extension errors too */
362 case X509_V_ERR_INVALID_CA:
363 case X509_V_ERR_INVALID_NON_CA:
364 case X509_V_ERR_PATH_LENGTH_EXCEEDED:
365 case X509_V_ERR_INVALID_PURPOSE:
366 case X509_V_ERR_CRL_HAS_EXPIRED:
367 case X509_V_ERR_CRL_NOT_YET_VALID:
368 case X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION:
375 if (cert_error == X509_V_OK && ok == 2)