2
0

verify.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Copyright 1995-2022 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. OSSL_STACK_OF_X509_free(untrusted);
  215. OSSL_STACK_OF_X509_free(trusted);
  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. X509_free(x);
  241. return 0;
  242. }
  243. }
  244. }
  245. csc = X509_STORE_CTX_new();
  246. if (csc == NULL) {
  247. BIO_printf(bio_err, "error %s: X.509 store context allocation failed\n",
  248. (file == NULL) ? "stdin" : file);
  249. goto end;
  250. }
  251. X509_STORE_set_flags(ctx, vflags);
  252. if (!X509_STORE_CTX_init(csc, ctx, x, uchain)) {
  253. X509_STORE_CTX_free(csc);
  254. BIO_printf(bio_err,
  255. "error %s: X.509 store context initialization failed\n",
  256. (file == NULL) ? "stdin" : file);
  257. goto end;
  258. }
  259. if (tchain != NULL)
  260. X509_STORE_CTX_set0_trusted_stack(csc, tchain);
  261. if (crls != NULL)
  262. X509_STORE_CTX_set0_crls(csc, crls);
  263. i = X509_verify_cert(csc);
  264. if (i > 0 && X509_STORE_CTX_get_error(csc) == X509_V_OK) {
  265. BIO_printf(bio_out, "%s: OK\n", (file == NULL) ? "stdin" : file);
  266. ret = 1;
  267. if (show_chain) {
  268. int j;
  269. chain = X509_STORE_CTX_get1_chain(csc);
  270. num_untrusted = X509_STORE_CTX_get_num_untrusted(csc);
  271. BIO_printf(bio_out, "Chain:\n");
  272. for (j = 0; j < sk_X509_num(chain); j++) {
  273. X509 *cert = sk_X509_value(chain, j);
  274. BIO_printf(bio_out, "depth=%d: ", j);
  275. X509_NAME_print_ex_fp(stdout,
  276. X509_get_subject_name(cert),
  277. 0, get_nameopt());
  278. if (j < num_untrusted)
  279. BIO_printf(bio_out, " (untrusted)");
  280. BIO_printf(bio_out, "\n");
  281. }
  282. OSSL_STACK_OF_X509_free(chain);
  283. }
  284. } else {
  285. BIO_printf(bio_err,
  286. "error %s: verification failed\n",
  287. (file == NULL) ? "stdin" : file);
  288. }
  289. X509_STORE_CTX_free(csc);
  290. end:
  291. if (i <= 0)
  292. ERR_print_errors(bio_err);
  293. X509_free(x);
  294. return ret;
  295. }
  296. static int cb(int ok, X509_STORE_CTX *ctx)
  297. {
  298. int cert_error = X509_STORE_CTX_get_error(ctx);
  299. X509 *current_cert = X509_STORE_CTX_get_current_cert(ctx);
  300. if (!ok) {
  301. if (current_cert != NULL) {
  302. X509_NAME_print_ex(bio_err,
  303. X509_get_subject_name(current_cert),
  304. 0, get_nameopt());
  305. BIO_printf(bio_err, "\n");
  306. }
  307. BIO_printf(bio_err, "%serror %d at %d depth lookup: %s\n",
  308. X509_STORE_CTX_get0_parent_ctx(ctx) ? "[CRL path] " : "",
  309. cert_error,
  310. X509_STORE_CTX_get_error_depth(ctx),
  311. X509_verify_cert_error_string(cert_error));
  312. /*
  313. * Pretend that some errors are ok, so they don't stop further
  314. * processing of the certificate chain. Setting ok = 1 does this.
  315. * After X509_verify_cert() is done, we verify that there were
  316. * no actual errors, even if the returned value was positive.
  317. */
  318. switch (cert_error) {
  319. case X509_V_ERR_NO_EXPLICIT_POLICY:
  320. policies_print(ctx);
  321. /* fall through */
  322. case X509_V_ERR_CERT_HAS_EXPIRED:
  323. /* Continue even if the leaf is a self-signed cert */
  324. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  325. /* Continue after extension errors too */
  326. case X509_V_ERR_INVALID_CA:
  327. case X509_V_ERR_INVALID_NON_CA:
  328. case X509_V_ERR_PATH_LENGTH_EXCEEDED:
  329. case X509_V_ERR_CRL_HAS_EXPIRED:
  330. case X509_V_ERR_CRL_NOT_YET_VALID:
  331. case X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION:
  332. /* errors due to strict conformance checking (-x509_strict) */
  333. case X509_V_ERR_INVALID_PURPOSE:
  334. case X509_V_ERR_PATHLEN_INVALID_FOR_NON_CA:
  335. case X509_V_ERR_PATHLEN_WITHOUT_KU_KEY_CERT_SIGN:
  336. case X509_V_ERR_CA_BCONS_NOT_CRITICAL:
  337. case X509_V_ERR_CA_CERT_MISSING_KEY_USAGE:
  338. case X509_V_ERR_KU_KEY_CERT_SIGN_INVALID_FOR_NON_CA:
  339. case X509_V_ERR_ISSUER_NAME_EMPTY:
  340. case X509_V_ERR_SUBJECT_NAME_EMPTY:
  341. case X509_V_ERR_EMPTY_SUBJECT_SAN_NOT_CRITICAL:
  342. case X509_V_ERR_EMPTY_SUBJECT_ALT_NAME:
  343. case X509_V_ERR_SIGNATURE_ALGORITHM_INCONSISTENCY:
  344. case X509_V_ERR_AUTHORITY_KEY_IDENTIFIER_CRITICAL:
  345. case X509_V_ERR_SUBJECT_KEY_IDENTIFIER_CRITICAL:
  346. case X509_V_ERR_MISSING_AUTHORITY_KEY_IDENTIFIER:
  347. case X509_V_ERR_MISSING_SUBJECT_KEY_IDENTIFIER:
  348. case X509_V_ERR_EXTENSIONS_REQUIRE_VERSION_3:
  349. ok = 1;
  350. }
  351. return ok;
  352. }
  353. if (cert_error == X509_V_OK && ok == 2)
  354. policies_print(ctx);
  355. if (!v_verbose)
  356. ERR_clear_error();
  357. return ok;
  358. }