verify.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. 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_ERR = -1, OPT_EOF = 0, OPT_HELP,
  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(), &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(), &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. argc = opt_num_rest();
  182. argv = opt_rest();
  183. if (trusted != NULL
  184. && (CAfile != NULL || CApath != NULL || CAstore != NULL)) {
  185. BIO_printf(bio_err,
  186. "%s: Cannot use -trusted with -CAfile, -CApath or -CAstore\n",
  187. prog);
  188. goto end;
  189. }
  190. if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
  191. CAstore, noCAstore)) == NULL)
  192. goto end;
  193. X509_STORE_set_verify_cb(store, cb);
  194. if (vpmtouched)
  195. X509_STORE_set1_param(store, vpm);
  196. ERR_clear_error();
  197. if (crl_download)
  198. store_setup_crl_download(store);
  199. ret = 0;
  200. if (argc < 1) {
  201. if (check(store, NULL, untrusted, trusted, crls, show_chain,
  202. vfyopts) != 1)
  203. ret = -1;
  204. } else {
  205. for (i = 0; i < argc; i++)
  206. if (check(store, argv[i], untrusted, trusted, crls, show_chain,
  207. vfyopts) != 1)
  208. ret = -1;
  209. }
  210. end:
  211. X509_VERIFY_PARAM_free(vpm);
  212. X509_STORE_free(store);
  213. sk_X509_pop_free(untrusted, X509_free);
  214. sk_X509_pop_free(trusted, X509_free);
  215. sk_X509_CRL_pop_free(crls, X509_CRL_free);
  216. sk_OPENSSL_STRING_free(vfyopts);
  217. release_engine(e);
  218. return (ret < 0 ? 2 : ret);
  219. }
  220. static int check(X509_STORE *ctx, const char *file,
  221. STACK_OF(X509) *uchain, STACK_OF(X509) *tchain,
  222. STACK_OF(X509_CRL) *crls, int show_chain,
  223. STACK_OF(OPENSSL_STRING) *opts)
  224. {
  225. X509 *x = NULL;
  226. int i = 0, ret = 0;
  227. X509_STORE_CTX *csc;
  228. STACK_OF(X509) *chain = NULL;
  229. int num_untrusted;
  230. x = load_cert(file, "certificate file");
  231. if (x == NULL)
  232. goto end;
  233. if (opts != NULL) {
  234. for (i = 0; i < sk_OPENSSL_STRING_num(opts); i++) {
  235. char *opt = sk_OPENSSL_STRING_value(opts, i);
  236. if (x509_ctrl_string(x, opt) <= 0) {
  237. BIO_printf(bio_err, "parameter error \"%s\"\n", opt);
  238. ERR_print_errors(bio_err);
  239. return 0;
  240. }
  241. }
  242. }
  243. csc = X509_STORE_CTX_new();
  244. if (csc == NULL) {
  245. BIO_printf(bio_err, "error %s: X.509 store context allocation failed\n",
  246. (file == NULL) ? "stdin" : file);
  247. goto end;
  248. }
  249. X509_STORE_set_flags(ctx, vflags);
  250. if (!X509_STORE_CTX_init(csc, ctx, x, uchain)) {
  251. X509_STORE_CTX_free(csc);
  252. BIO_printf(bio_err,
  253. "error %s: X.509 store context initialization failed\n",
  254. (file == NULL) ? "stdin" : file);
  255. goto end;
  256. }
  257. if (tchain != NULL)
  258. X509_STORE_CTX_set0_trusted_stack(csc, tchain);
  259. if (crls != NULL)
  260. X509_STORE_CTX_set0_crls(csc, crls);
  261. i = X509_verify_cert(csc);
  262. if (i > 0 && X509_STORE_CTX_get_error(csc) == X509_V_OK) {
  263. BIO_printf(bio_out, "%s: OK\n", (file == NULL) ? "stdin" : file);
  264. ret = 1;
  265. if (show_chain) {
  266. int j;
  267. chain = X509_STORE_CTX_get1_chain(csc);
  268. num_untrusted = X509_STORE_CTX_get_num_untrusted(csc);
  269. BIO_printf(bio_out, "Chain:\n");
  270. for (j = 0; j < sk_X509_num(chain); j++) {
  271. X509 *cert = sk_X509_value(chain, j);
  272. BIO_printf(bio_out, "depth=%d: ", j);
  273. X509_NAME_print_ex_fp(stdout,
  274. X509_get_subject_name(cert),
  275. 0, get_nameopt());
  276. if (j < num_untrusted)
  277. BIO_printf(bio_out, " (untrusted)");
  278. BIO_printf(bio_out, "\n");
  279. }
  280. sk_X509_pop_free(chain, X509_free);
  281. }
  282. } else {
  283. BIO_printf(bio_err,
  284. "error %s: verification failed\n",
  285. (file == NULL) ? "stdin" : file);
  286. }
  287. X509_STORE_CTX_free(csc);
  288. end:
  289. if (i <= 0)
  290. ERR_print_errors(bio_err);
  291. X509_free(x);
  292. return ret;
  293. }
  294. static int cb(int ok, X509_STORE_CTX *ctx)
  295. {
  296. int cert_error = X509_STORE_CTX_get_error(ctx);
  297. X509 *current_cert = X509_STORE_CTX_get_current_cert(ctx);
  298. if (!ok) {
  299. if (current_cert != NULL) {
  300. X509_NAME_print_ex(bio_err,
  301. X509_get_subject_name(current_cert),
  302. 0, get_nameopt());
  303. BIO_printf(bio_err, "\n");
  304. }
  305. BIO_printf(bio_err, "%serror %d at %d depth lookup: %s\n",
  306. X509_STORE_CTX_get0_parent_ctx(ctx) ? "[CRL path] " : "",
  307. cert_error,
  308. X509_STORE_CTX_get_error_depth(ctx),
  309. X509_verify_cert_error_string(cert_error));
  310. /*
  311. * Pretend that some errors are ok, so they don't stop further
  312. * processing of the certificate chain. Setting ok = 1 does this.
  313. * After X509_verify_cert() is done, we verify that there were
  314. * no actual errors, even if the returned value was positive.
  315. */
  316. switch (cert_error) {
  317. case X509_V_ERR_NO_EXPLICIT_POLICY:
  318. policies_print(ctx);
  319. /* fall thru */
  320. case X509_V_ERR_CERT_HAS_EXPIRED:
  321. /* Continue even if the leaf is a self-signed cert */
  322. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  323. /* Continue after extension errors too */
  324. case X509_V_ERR_INVALID_CA:
  325. case X509_V_ERR_INVALID_NON_CA:
  326. case X509_V_ERR_PATH_LENGTH_EXCEEDED:
  327. case X509_V_ERR_INVALID_PURPOSE:
  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. ok = 1;
  332. }
  333. return ok;
  334. }
  335. if (cert_error == X509_V_OK && ok == 2)
  336. policies_print(ctx);
  337. if (!v_verbose)
  338. ERR_clear_error();
  339. return ok;
  340. }