2 * Copyright 1995-2022 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>
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 STACK_OF(OPENSSL_STRING) *opts);
26 static int v_verbose = 0, vflags = 0;
28 typedef enum OPTION_choice {
30 OPT_ENGINE, OPT_CAPATH, OPT_CAFILE, OPT_CASTORE,
31 OPT_NOCAPATH, OPT_NOCAFILE, OPT_NOCASTORE,
32 OPT_UNTRUSTED, OPT_TRUSTED, OPT_CRLFILE, OPT_CRL_DOWNLOAD, OPT_SHOW_CHAIN,
33 OPT_V_ENUM, OPT_NAMEOPT, OPT_VFYOPT,
38 const OPTIONS verify_options[] = {
39 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [cert...]\n"},
41 OPT_SECTION("General"),
42 {"help", OPT_HELP, '-', "Display this summary"},
43 #ifndef OPENSSL_NO_ENGINE
44 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
46 {"verbose", OPT_VERBOSE, '-',
47 "Print extra information about the operations being performed."},
48 {"nameopt", OPT_NAMEOPT, 's', "Certificate subject/issuer name printing options"},
50 OPT_SECTION("Certificate chain"),
51 {"trusted", OPT_TRUSTED, '<', "A file of trusted certificates"},
52 {"CAfile", OPT_CAFILE, '<', "A file of trusted certificates"},
53 {"CApath", OPT_CAPATH, '/', "A directory of files with trusted certificates"},
54 {"CAstore", OPT_CASTORE, ':', "URI to a store of trusted certificates"},
55 {"no-CAfile", OPT_NOCAFILE, '-',
56 "Do not load the default trusted certificates file"},
57 {"no-CApath", OPT_NOCAPATH, '-',
58 "Do not load trusted certificates from the default directory"},
59 {"no-CAstore", OPT_NOCASTORE, '-',
60 "Do not load trusted certificates from the default certificates store"},
61 {"untrusted", OPT_UNTRUSTED, '<', "A file of untrusted certificates"},
62 {"CRLfile", OPT_CRLFILE, '<',
63 "File containing one or more CRL's (in PEM format) to load"},
64 {"crl_download", OPT_CRL_DOWNLOAD, '-',
65 "Try downloading CRL information for certificates via their CDP entries"},
66 {"show_chain", OPT_SHOW_CHAIN, '-',
67 "Display information about the certificate chain"},
70 {"vfyopt", OPT_VFYOPT, 's', "Verification parameter in n:v form"},
75 {"cert", 0, 0, "Certificate(s) to verify (optional; stdin used otherwise)"},
79 int verify_main(int argc, char **argv)
82 STACK_OF(X509) *untrusted = NULL, *trusted = NULL;
83 STACK_OF(X509_CRL) *crls = NULL;
84 STACK_OF(OPENSSL_STRING) *vfyopts = NULL;
85 X509_STORE *store = NULL;
86 X509_VERIFY_PARAM *vpm = NULL;
87 const char *prog, *CApath = NULL, *CAfile = NULL, *CAstore = NULL;
88 int noCApath = 0, noCAfile = 0, noCAstore = 0;
89 int vpmtouched = 0, crl_download = 0, show_chain = 0, i = 0, ret = 1;
92 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
95 prog = opt_init(argc, argv, verify_options);
96 while ((o = opt_next()) != OPT_EOF) {
101 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
104 opt_help(verify_options);
105 BIO_printf(bio_err, "\nRecognized certificate chain purposes:\n");
106 for (i = 0; i < X509_PURPOSE_get_count(); i++) {
107 X509_PURPOSE *ptmp = X509_PURPOSE_get0(i);
109 BIO_printf(bio_err, " %-15s %s\n",
110 X509_PURPOSE_get0_sname(ptmp),
111 X509_PURPOSE_get0_name(ptmp));
114 BIO_printf(bio_err, "Recognized certificate policy names:\n");
115 for (i = 0; i < X509_VERIFY_PARAM_get_count(); i++) {
116 const X509_VERIFY_PARAM *vptmp = X509_VERIFY_PARAM_get0(i);
118 BIO_printf(bio_err, " %s\n",
119 X509_VERIFY_PARAM_get0_name(vptmp));
124 if (!opt_verify(o, vpm))
147 /* Zero or more times */
148 if (!load_certs(opt_arg(), 0, &untrusted, NULL,
149 "untrusted certificates"))
153 /* Zero or more times */
157 if (!load_certs(opt_arg(), 0, &trusted, NULL, "trusted certificates"))
161 /* Zero or more times */
162 if (!load_crls(opt_arg(), &crls, NULL, "other CRLs"))
165 case OPT_CRL_DOWNLOAD:
169 if ((e = setup_engine(opt_arg(), 0)) == NULL) {
170 /* Failure message already displayed */
178 if (!set_nameopt(opt_arg()))
183 vfyopts = sk_OPENSSL_STRING_new_null();
184 if (!vfyopts || !sk_OPENSSL_STRING_push(vfyopts, opt_arg()))
191 if (!opt_provider(o))
197 /* Extra arguments are certificates to verify. */
198 argc = opt_num_rest();
202 && (CAfile != NULL || CApath != NULL || CAstore != NULL)) {
204 "%s: Cannot use -trusted with -CAfile, -CApath or -CAstore\n",
209 if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
210 CAstore, noCAstore)) == NULL)
212 X509_STORE_set_verify_cb(store, cb);
215 X509_STORE_set1_param(store, vpm);
220 store_setup_crl_download(store);
224 if (check(store, NULL, untrusted, trusted, crls, show_chain,
228 for (i = 0; i < argc; i++)
229 if (check(store, argv[i], untrusted, trusted, crls, show_chain,
235 X509_VERIFY_PARAM_free(vpm);
236 X509_STORE_free(store);
237 OSSL_STACK_OF_X509_free(untrusted);
238 OSSL_STACK_OF_X509_free(trusted);
239 sk_X509_CRL_pop_free(crls, X509_CRL_free);
240 sk_OPENSSL_STRING_free(vfyopts);
242 return (ret < 0 ? 2 : ret);
245 static int check(X509_STORE *ctx, const char *file,
246 STACK_OF(X509) *uchain, STACK_OF(X509) *tchain,
247 STACK_OF(X509_CRL) *crls, int show_chain,
248 STACK_OF(OPENSSL_STRING) *opts)
253 STACK_OF(X509) *chain = NULL;
256 x = load_cert(file, FORMAT_UNDEF, "certificate file");
261 for (i = 0; i < sk_OPENSSL_STRING_num(opts); i++) {
262 char *opt = sk_OPENSSL_STRING_value(opts, i);
263 if (x509_ctrl_string(x, opt) <= 0) {
264 BIO_printf(bio_err, "parameter error \"%s\"\n", opt);
265 ERR_print_errors(bio_err);
272 csc = X509_STORE_CTX_new();
274 BIO_printf(bio_err, "error %s: X.509 store context allocation failed\n",
275 (file == NULL) ? "stdin" : file);
279 X509_STORE_set_flags(ctx, vflags);
280 if (!X509_STORE_CTX_init(csc, ctx, x, uchain)) {
281 X509_STORE_CTX_free(csc);
283 "error %s: X.509 store context initialization failed\n",
284 (file == NULL) ? "stdin" : file);
288 X509_STORE_CTX_set0_trusted_stack(csc, tchain);
290 X509_STORE_CTX_set0_crls(csc, crls);
291 i = X509_verify_cert(csc);
292 if (i > 0 && X509_STORE_CTX_get_error(csc) == X509_V_OK) {
293 BIO_printf(bio_out, "%s: OK\n", (file == NULL) ? "stdin" : file);
298 chain = X509_STORE_CTX_get1_chain(csc);
299 num_untrusted = X509_STORE_CTX_get_num_untrusted(csc);
300 BIO_printf(bio_out, "Chain:\n");
301 for (j = 0; j < sk_X509_num(chain); j++) {
302 X509 *cert = sk_X509_value(chain, j);
303 BIO_printf(bio_out, "depth=%d: ", j);
304 X509_NAME_print_ex_fp(stdout,
305 X509_get_subject_name(cert),
307 if (j < num_untrusted)
308 BIO_printf(bio_out, " (untrusted)");
309 BIO_printf(bio_out, "\n");
311 OSSL_STACK_OF_X509_free(chain);
315 "error %s: verification failed\n",
316 (file == NULL) ? "stdin" : file);
318 X509_STORE_CTX_free(csc);
322 ERR_print_errors(bio_err);
328 static int cb(int ok, X509_STORE_CTX *ctx)
330 int cert_error = X509_STORE_CTX_get_error(ctx);
331 X509 *current_cert = X509_STORE_CTX_get_current_cert(ctx);
334 if (current_cert != NULL) {
335 X509_NAME_print_ex(bio_err,
336 X509_get_subject_name(current_cert),
338 BIO_printf(bio_err, "\n");
340 BIO_printf(bio_err, "%serror %d at %d depth lookup: %s\n",
341 X509_STORE_CTX_get0_parent_ctx(ctx) ? "[CRL path] " : "",
343 X509_STORE_CTX_get_error_depth(ctx),
344 X509_verify_cert_error_string(cert_error));
347 * Pretend that some errors are ok, so they don't stop further
348 * processing of the certificate chain. Setting ok = 1 does this.
349 * After X509_verify_cert() is done, we verify that there were
350 * no actual errors, even if the returned value was positive.
352 switch (cert_error) {
353 case X509_V_ERR_NO_EXPLICIT_POLICY:
356 case X509_V_ERR_CERT_HAS_EXPIRED:
357 /* Continue even if the leaf is a self-signed cert */
358 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
359 /* Continue after extension errors too */
360 case X509_V_ERR_INVALID_CA:
361 case X509_V_ERR_INVALID_NON_CA:
362 case X509_V_ERR_PATH_LENGTH_EXCEEDED:
363 case X509_V_ERR_CRL_HAS_EXPIRED:
364 case X509_V_ERR_CRL_NOT_YET_VALID:
365 case X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION:
366 /* errors due to strict conformance checking (-x509_strict) */
367 case X509_V_ERR_INVALID_PURPOSE:
368 case X509_V_ERR_PATHLEN_INVALID_FOR_NON_CA:
369 case X509_V_ERR_PATHLEN_WITHOUT_KU_KEY_CERT_SIGN:
370 case X509_V_ERR_CA_BCONS_NOT_CRITICAL:
371 case X509_V_ERR_CA_CERT_MISSING_KEY_USAGE:
372 case X509_V_ERR_KU_KEY_CERT_SIGN_INVALID_FOR_NON_CA:
373 case X509_V_ERR_ISSUER_NAME_EMPTY:
374 case X509_V_ERR_SUBJECT_NAME_EMPTY:
375 case X509_V_ERR_EMPTY_SUBJECT_SAN_NOT_CRITICAL:
376 case X509_V_ERR_EMPTY_SUBJECT_ALT_NAME:
377 case X509_V_ERR_SIGNATURE_ALGORITHM_INCONSISTENCY:
378 case X509_V_ERR_AUTHORITY_KEY_IDENTIFIER_CRITICAL:
379 case X509_V_ERR_SUBJECT_KEY_IDENTIFIER_CRITICAL:
380 case X509_V_ERR_MISSING_AUTHORITY_KEY_IDENTIFIER:
381 case X509_V_ERR_MISSING_SUBJECT_KEY_IDENTIFIER:
382 case X509_V_ERR_EXTENSIONS_REQUIRE_VERSION_3:
388 if (cert_error == X509_V_OK && ok == 2)