2
0

verify.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  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
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "apps.h"
  13. #include "progs.h"
  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>
  19. static int cb(int ok, X509_STORE_CTX *ctx);
  20. static int check(X509_STORE *ctx, const char *file,
  21. STACK_OF(X509) *uchain, STACK_OF(X509) *tchain,
  22. STACK_OF(X509_CRL) *crls, int show_chain,
  23. STACK_OF(OPENSSL_STRING) *opts);
  24. static int v_verbose = 0, vflags = 0;
  25. typedef enum OPTION_choice {
  26. OPT_COMMON,
  27. OPT_ENGINE, OPT_CAPATH, OPT_CAFILE, OPT_CASTORE,
  28. OPT_NOCAPATH, OPT_NOCAFILE, OPT_NOCASTORE,
  29. OPT_UNTRUSTED, OPT_TRUSTED, OPT_CRLFILE, OPT_CRL_DOWNLOAD, OPT_SHOW_CHAIN,
  30. OPT_V_ENUM, OPT_NAMEOPT, OPT_VFYOPT,
  31. OPT_VERBOSE,
  32. OPT_PROV_ENUM
  33. } OPTION_CHOICE;
  34. const OPTIONS verify_options[] = {
  35. {OPT_HELP_STR, 1, '-', "Usage: %s [options] [cert...]\n"},
  36. OPT_SECTION("General"),
  37. {"help", OPT_HELP, '-', "Display this summary"},
  38. #ifndef OPENSSL_NO_ENGINE
  39. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  40. #endif
  41. {"verbose", OPT_VERBOSE, '-',
  42. "Print extra information about the operations being performed."},
  43. {"nameopt", OPT_NAMEOPT, 's', "Certificate subject/issuer name printing options"},
  44. OPT_SECTION("Certificate chain"),
  45. {"trusted", OPT_TRUSTED, '<', "A file of trusted certificates"},
  46. {"CAfile", OPT_CAFILE, '<', "A file of trusted certificates"},
  47. {"CApath", OPT_CAPATH, '/', "A directory of files with trusted certificates"},
  48. {"CAstore", OPT_CASTORE, ':', "URI to a store of trusted certificates"},
  49. {"no-CAfile", OPT_NOCAFILE, '-',
  50. "Do not load the default trusted certificates file"},
  51. {"no-CApath", OPT_NOCAPATH, '-',
  52. "Do not load trusted certificates from the default directory"},
  53. {"no-CAstore", OPT_NOCASTORE, '-',
  54. "Do not load trusted certificates from the default certificates store"},
  55. {"untrusted", OPT_UNTRUSTED, '<', "A file of untrusted certificates"},
  56. {"CRLfile", OPT_CRLFILE, '<',
  57. "File containing one or more CRL's (in PEM format) to load"},
  58. {"crl_download", OPT_CRL_DOWNLOAD, '-',
  59. "Try downloading CRL information for certificates via their CDP entries"},
  60. {"show_chain", OPT_SHOW_CHAIN, '-',
  61. "Display information about the certificate chain"},
  62. OPT_V_OPTIONS,
  63. {"vfyopt", OPT_VFYOPT, 's', "Verification parameter in n:v form"},
  64. OPT_PROV_OPTIONS,
  65. OPT_PARAMETERS(),
  66. {"cert", 0, 0, "Certificate(s) to verify (optional; stdin used otherwise)"},
  67. {NULL}
  68. };
  69. int verify_main(int argc, char **argv)
  70. {
  71. ENGINE *e = NULL;
  72. STACK_OF(X509) *untrusted = NULL, *trusted = NULL;
  73. STACK_OF(X509_CRL) *crls = NULL;
  74. STACK_OF(OPENSSL_STRING) *vfyopts = NULL;
  75. X509_STORE *store = NULL;
  76. X509_VERIFY_PARAM *vpm = NULL;
  77. const char *prog, *CApath = NULL, *CAfile = NULL, *CAstore = NULL;
  78. int noCApath = 0, noCAfile = 0, noCAstore = 0;
  79. int vpmtouched = 0, crl_download = 0, show_chain = 0, i = 0, ret = 1;
  80. OPTION_CHOICE o;
  81. if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
  82. goto end;
  83. prog = opt_init(argc, argv, verify_options);
  84. while ((o = opt_next()) != OPT_EOF) {
  85. switch (o) {
  86. case OPT_EOF:
  87. case OPT_ERR:
  88. opthelp:
  89. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  90. goto end;
  91. case OPT_HELP:
  92. opt_help(verify_options);
  93. BIO_printf(bio_err, "\nRecognized certificate chain purposes:\n");
  94. for (i = 0; i < X509_PURPOSE_get_count(); i++) {
  95. X509_PURPOSE *ptmp = X509_PURPOSE_get0(i);
  96. BIO_printf(bio_err, " %-15s %s\n",
  97. X509_PURPOSE_get0_sname(ptmp),
  98. X509_PURPOSE_get0_name(ptmp));
  99. }
  100. BIO_printf(bio_err, "Recognized certificate policy names:\n");
  101. for (i = 0; i < X509_VERIFY_PARAM_get_count(); i++) {
  102. const X509_VERIFY_PARAM *vptmp = X509_VERIFY_PARAM_get0(i);
  103. BIO_printf(bio_err, " %s\n",
  104. X509_VERIFY_PARAM_get0_name(vptmp));
  105. }
  106. ret = 0;
  107. goto end;
  108. case OPT_V_CASES:
  109. if (!opt_verify(o, vpm))
  110. goto end;
  111. vpmtouched++;
  112. break;
  113. case OPT_CAPATH:
  114. CApath = opt_arg();
  115. break;
  116. case OPT_CAFILE:
  117. CAfile = opt_arg();
  118. break;
  119. case OPT_CASTORE:
  120. CAstore = opt_arg();
  121. break;
  122. case OPT_NOCAPATH:
  123. noCApath = 1;
  124. break;
  125. case OPT_NOCAFILE:
  126. noCAfile = 1;
  127. break;
  128. case OPT_NOCASTORE:
  129. noCAstore = 1;
  130. break;
  131. case OPT_UNTRUSTED:
  132. /* Zero or more times */
  133. if (!load_certs(opt_arg(), 0, &untrusted, NULL,
  134. "untrusted certificates"))
  135. goto end;
  136. break;
  137. case OPT_TRUSTED:
  138. /* Zero or more times */
  139. noCAfile = 1;
  140. noCApath = 1;
  141. noCAstore = 1;
  142. if (!load_certs(opt_arg(), 0, &trusted, NULL, "trusted certificates"))
  143. goto end;
  144. break;
  145. case OPT_CRLFILE:
  146. /* Zero or more times */
  147. if (!load_crls(opt_arg(), &crls, NULL, "other CRLs"))
  148. goto end;
  149. break;
  150. case OPT_CRL_DOWNLOAD:
  151. crl_download = 1;
  152. break;
  153. case OPT_ENGINE:
  154. if ((e = setup_engine(opt_arg(), 0)) == NULL) {
  155. /* Failure message already displayed */
  156. goto end;
  157. }
  158. break;
  159. case OPT_SHOW_CHAIN:
  160. show_chain = 1;
  161. break;
  162. case OPT_NAMEOPT:
  163. if (!set_nameopt(opt_arg()))
  164. goto end;
  165. break;
  166. case OPT_VFYOPT:
  167. if (!vfyopts)
  168. vfyopts = sk_OPENSSL_STRING_new_null();
  169. if (!vfyopts || !sk_OPENSSL_STRING_push(vfyopts, opt_arg()))
  170. goto opthelp;
  171. break;
  172. case OPT_VERBOSE:
  173. v_verbose = 1;
  174. break;
  175. case OPT_PROV_CASES:
  176. if (!opt_provider(o))
  177. goto end;
  178. break;
  179. }
  180. }
  181. /* Extra arguments are certificates to verify. */
  182. argc = opt_num_rest();
  183. argv = opt_rest();
  184. if (trusted != NULL
  185. && (CAfile != NULL || CApath != NULL || CAstore != NULL)) {
  186. BIO_printf(bio_err,
  187. "%s: Cannot use -trusted with -CAfile, -CApath or -CAstore\n",
  188. prog);
  189. goto end;
  190. }
  191. if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
  192. CAstore, noCAstore)) == NULL)
  193. goto end;
  194. X509_STORE_set_verify_cb(store, cb);
  195. if (vpmtouched)
  196. X509_STORE_set1_param(store, vpm);
  197. ERR_clear_error();
  198. if (crl_download)
  199. store_setup_crl_download(store);
  200. ret = 0;
  201. if (argc < 1) {
  202. if (check(store, NULL, untrusted, trusted, crls, show_chain,
  203. vfyopts) != 1)
  204. ret = -1;
  205. } else {
  206. for (i = 0; i < argc; i++)
  207. if (check(store, argv[i], untrusted, trusted, crls, show_chain,
  208. vfyopts) != 1)
  209. ret = -1;
  210. }
  211. end:
  212. X509_VERIFY_PARAM_free(vpm);
  213. X509_STORE_free(store);
  214. sk_X509_pop_free(untrusted, X509_free);
  215. sk_X509_pop_free(trusted, X509_free);
  216. sk_X509_CRL_pop_free(crls, X509_CRL_free);
  217. sk_OPENSSL_STRING_free(vfyopts);
  218. release_engine(e);
  219. return (ret < 0 ? 2 : ret);
  220. }
  221. static int check(X509_STORE *ctx, const char *file,
  222. STACK_OF(X509) *uchain, STACK_OF(X509) *tchain,
  223. STACK_OF(X509_CRL) *crls, int show_chain,
  224. STACK_OF(OPENSSL_STRING) *opts)
  225. {
  226. X509 *x = NULL;
  227. int i = 0, ret = 0;
  228. X509_STORE_CTX *csc;
  229. STACK_OF(X509) *chain = NULL;
  230. int num_untrusted;
  231. x = load_cert(file, FORMAT_UNDEF, "certificate file");
  232. if (x == NULL)
  233. goto end;
  234. if (opts != NULL) {
  235. for (i = 0; i < sk_OPENSSL_STRING_num(opts); i++) {
  236. char *opt = sk_OPENSSL_STRING_value(opts, i);
  237. if (x509_ctrl_string(x, opt) <= 0) {
  238. BIO_printf(bio_err, "parameter error \"%s\"\n", opt);
  239. ERR_print_errors(bio_err);
  240. return 0;
  241. }
  242. }
  243. }
  244. csc = X509_STORE_CTX_new();
  245. if (csc == NULL) {
  246. BIO_printf(bio_err, "error %s: X.509 store context allocation failed\n",
  247. (file == NULL) ? "stdin" : file);
  248. goto end;
  249. }
  250. X509_STORE_set_flags(ctx, vflags);
  251. if (!X509_STORE_CTX_init(csc, ctx, x, uchain)) {
  252. X509_STORE_CTX_free(csc);
  253. BIO_printf(bio_err,
  254. "error %s: X.509 store context initialization failed\n",
  255. (file == NULL) ? "stdin" : file);
  256. goto end;
  257. }
  258. if (tchain != NULL)
  259. X509_STORE_CTX_set0_trusted_stack(csc, tchain);
  260. if (crls != NULL)
  261. X509_STORE_CTX_set0_crls(csc, crls);
  262. i = X509_verify_cert(csc);
  263. if (i > 0 && X509_STORE_CTX_get_error(csc) == X509_V_OK) {
  264. BIO_printf(bio_out, "%s: OK\n", (file == NULL) ? "stdin" : file);
  265. ret = 1;
  266. if (show_chain) {
  267. int j;
  268. chain = X509_STORE_CTX_get1_chain(csc);
  269. num_untrusted = X509_STORE_CTX_get_num_untrusted(csc);
  270. BIO_printf(bio_out, "Chain:\n");
  271. for (j = 0; j < sk_X509_num(chain); j++) {
  272. X509 *cert = sk_X509_value(chain, j);
  273. BIO_printf(bio_out, "depth=%d: ", j);
  274. X509_NAME_print_ex_fp(stdout,
  275. X509_get_subject_name(cert),
  276. 0, get_nameopt());
  277. if (j < num_untrusted)
  278. BIO_printf(bio_out, " (untrusted)");
  279. BIO_printf(bio_out, "\n");
  280. }
  281. sk_X509_pop_free(chain, X509_free);
  282. }
  283. } else {
  284. BIO_printf(bio_err,
  285. "error %s: verification failed\n",
  286. (file == NULL) ? "stdin" : file);
  287. }
  288. X509_STORE_CTX_free(csc);
  289. end:
  290. if (i <= 0)
  291. ERR_print_errors(bio_err);
  292. X509_free(x);
  293. return ret;
  294. }
  295. static int cb(int ok, X509_STORE_CTX *ctx)
  296. {
  297. int cert_error = X509_STORE_CTX_get_error(ctx);
  298. X509 *current_cert = X509_STORE_CTX_get_current_cert(ctx);
  299. if (!ok) {
  300. if (current_cert != NULL) {
  301. X509_NAME_print_ex(bio_err,
  302. X509_get_subject_name(current_cert),
  303. 0, get_nameopt());
  304. BIO_printf(bio_err, "\n");
  305. }
  306. BIO_printf(bio_err, "%serror %d at %d depth lookup: %s\n",
  307. X509_STORE_CTX_get0_parent_ctx(ctx) ? "[CRL path] " : "",
  308. cert_error,
  309. X509_STORE_CTX_get_error_depth(ctx),
  310. X509_verify_cert_error_string(cert_error));
  311. /*
  312. * Pretend that some errors are ok, so they don't stop further
  313. * processing of the certificate chain. Setting ok = 1 does this.
  314. * After X509_verify_cert() is done, we verify that there were
  315. * no actual errors, even if the returned value was positive.
  316. */
  317. switch (cert_error) {
  318. case X509_V_ERR_NO_EXPLICIT_POLICY:
  319. policies_print(ctx);
  320. /* fall thru */
  321. case X509_V_ERR_CERT_HAS_EXPIRED:
  322. /* Continue even if the leaf is a self-signed cert */
  323. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  324. /* Continue after extension errors too */
  325. case X509_V_ERR_INVALID_CA:
  326. case X509_V_ERR_INVALID_NON_CA:
  327. case X509_V_ERR_PATH_LENGTH_EXCEEDED:
  328. case X509_V_ERR_CRL_HAS_EXPIRED:
  329. case X509_V_ERR_CRL_NOT_YET_VALID:
  330. case X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION:
  331. /* errors due to strict conformance checking (-x509_strict) */
  332. case X509_V_ERR_INVALID_PURPOSE:
  333. case X509_V_ERR_PATHLEN_INVALID_FOR_NON_CA:
  334. case X509_V_ERR_PATHLEN_WITHOUT_KU_KEY_CERT_SIGN:
  335. case X509_V_ERR_CA_BCONS_NOT_CRITICAL:
  336. case X509_V_ERR_CA_CERT_MISSING_KEY_USAGE:
  337. case X509_V_ERR_KU_KEY_CERT_SIGN_INVALID_FOR_NON_CA:
  338. case X509_V_ERR_ISSUER_NAME_EMPTY:
  339. case X509_V_ERR_SUBJECT_NAME_EMPTY:
  340. case X509_V_ERR_EMPTY_SUBJECT_SAN_NOT_CRITICAL:
  341. case X509_V_ERR_EMPTY_SUBJECT_ALT_NAME:
  342. case X509_V_ERR_SIGNATURE_ALGORITHM_INCONSISTENCY:
  343. case X509_V_ERR_AUTHORITY_KEY_IDENTIFIER_CRITICAL:
  344. case X509_V_ERR_SUBJECT_KEY_IDENTIFIER_CRITICAL:
  345. case X509_V_ERR_MISSING_AUTHORITY_KEY_IDENTIFIER:
  346. case X509_V_ERR_MISSING_SUBJECT_KEY_IDENTIFIER:
  347. case X509_V_ERR_EXTENSIONS_REQUIRE_VERSION_3:
  348. ok = 1;
  349. }
  350. return ok;
  351. }
  352. if (cert_error == X509_V_OK && ok == 2)
  353. policies_print(ctx);
  354. if (!v_verbose)
  355. ERR_clear_error();
  356. return ok;
  357. }