verify.c 12 KB

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