verify.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Copyright 1995-2018 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. static int v_verbose = 0, vflags = 0;
  24. typedef enum OPTION_choice {
  25. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  26. OPT_ENGINE, OPT_CAPATH, OPT_CAFILE, OPT_NOCAPATH, OPT_NOCAFILE,
  27. OPT_UNTRUSTED, OPT_TRUSTED, OPT_CRLFILE, OPT_CRL_DOWNLOAD, OPT_SHOW_CHAIN,
  28. OPT_V_ENUM, OPT_NAMEOPT,
  29. OPT_VERBOSE
  30. } OPTION_CHOICE;
  31. const OPTIONS verify_options[] = {
  32. {OPT_HELP_STR, 1, '-', "Usage: %s [options] cert.pem...\n"},
  33. {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
  34. {"help", OPT_HELP, '-', "Display this summary"},
  35. {"verbose", OPT_VERBOSE, '-',
  36. "Print extra information about the operations being performed."},
  37. {"CApath", OPT_CAPATH, '/', "A directory of trusted certificates"},
  38. {"CAfile", OPT_CAFILE, '<', "A file of trusted certificates"},
  39. {"no-CAfile", OPT_NOCAFILE, '-',
  40. "Do not load the default certificates file"},
  41. {"no-CApath", OPT_NOCAPATH, '-',
  42. "Do not load certificates from the default certificates directory"},
  43. {"untrusted", OPT_UNTRUSTED, '<', "A file of untrusted certificates"},
  44. {"trusted", OPT_TRUSTED, '<', "A file of trusted certificates"},
  45. {"CRLfile", OPT_CRLFILE, '<',
  46. "File containing one or more CRL's (in PEM format) to load"},
  47. {"crl_download", OPT_CRL_DOWNLOAD, '-',
  48. "Attempt to download CRL information for this certificate"},
  49. {"show_chain", OPT_SHOW_CHAIN, '-',
  50. "Display information about the certificate chain"},
  51. {"nameopt", OPT_NAMEOPT, 's', "Various certificate name options"},
  52. OPT_V_OPTIONS,
  53. #ifndef OPENSSL_NO_ENGINE
  54. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  55. #endif
  56. {NULL}
  57. };
  58. int verify_main(int argc, char **argv)
  59. {
  60. ENGINE *e = NULL;
  61. STACK_OF(X509) *untrusted = NULL, *trusted = NULL;
  62. STACK_OF(X509_CRL) *crls = NULL;
  63. X509_STORE *store = NULL;
  64. X509_VERIFY_PARAM *vpm = NULL;
  65. const char *prog, *CApath = NULL, *CAfile = NULL;
  66. int noCApath = 0, noCAfile = 0;
  67. int vpmtouched = 0, crl_download = 0, show_chain = 0, i = 0, ret = 1;
  68. OPTION_CHOICE o;
  69. if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
  70. goto end;
  71. prog = opt_init(argc, argv, verify_options);
  72. while ((o = opt_next()) != OPT_EOF) {
  73. switch (o) {
  74. case OPT_EOF:
  75. case OPT_ERR:
  76. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  77. goto end;
  78. case OPT_HELP:
  79. opt_help(verify_options);
  80. BIO_printf(bio_err, "Recognized usages:\n");
  81. for (i = 0; i < X509_PURPOSE_get_count(); i++) {
  82. X509_PURPOSE *ptmp;
  83. ptmp = X509_PURPOSE_get0(i);
  84. BIO_printf(bio_err, "\t%-10s\t%s\n",
  85. X509_PURPOSE_get0_sname(ptmp),
  86. X509_PURPOSE_get0_name(ptmp));
  87. }
  88. BIO_printf(bio_err, "Recognized verify names:\n");
  89. for (i = 0; i < X509_VERIFY_PARAM_get_count(); i++) {
  90. const X509_VERIFY_PARAM *vptmp;
  91. vptmp = X509_VERIFY_PARAM_get0(i);
  92. BIO_printf(bio_err, "\t%-10s\n",
  93. X509_VERIFY_PARAM_get0_name(vptmp));
  94. }
  95. ret = 0;
  96. goto end;
  97. case OPT_V_CASES:
  98. if (!opt_verify(o, vpm))
  99. goto end;
  100. vpmtouched++;
  101. break;
  102. case OPT_CAPATH:
  103. CApath = opt_arg();
  104. break;
  105. case OPT_CAFILE:
  106. CAfile = opt_arg();
  107. break;
  108. case OPT_NOCAPATH:
  109. noCApath = 1;
  110. break;
  111. case OPT_NOCAFILE:
  112. noCAfile = 1;
  113. break;
  114. case OPT_UNTRUSTED:
  115. /* Zero or more times */
  116. if (!load_certs(opt_arg(), &untrusted, FORMAT_PEM, NULL,
  117. "untrusted certificates"))
  118. goto end;
  119. break;
  120. case OPT_TRUSTED:
  121. /* Zero or more times */
  122. noCAfile = 1;
  123. noCApath = 1;
  124. if (!load_certs(opt_arg(), &trusted, FORMAT_PEM, NULL,
  125. "trusted certificates"))
  126. goto end;
  127. break;
  128. case OPT_CRLFILE:
  129. /* Zero or more times */
  130. if (!load_crls(opt_arg(), &crls, FORMAT_PEM, NULL,
  131. "other CRLs"))
  132. goto end;
  133. break;
  134. case OPT_CRL_DOWNLOAD:
  135. crl_download = 1;
  136. break;
  137. case OPT_ENGINE:
  138. if ((e = setup_engine(opt_arg(), 0)) == NULL) {
  139. /* Failure message already displayed */
  140. goto end;
  141. }
  142. break;
  143. case OPT_SHOW_CHAIN:
  144. show_chain = 1;
  145. break;
  146. case OPT_NAMEOPT:
  147. if (!set_nameopt(opt_arg()))
  148. goto end;
  149. break;
  150. case OPT_VERBOSE:
  151. v_verbose = 1;
  152. break;
  153. }
  154. }
  155. argc = opt_num_rest();
  156. argv = opt_rest();
  157. if (trusted != NULL && (CAfile || CApath)) {
  158. BIO_printf(bio_err,
  159. "%s: Cannot use -trusted with -CAfile or -CApath\n",
  160. prog);
  161. goto end;
  162. }
  163. if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath)) == NULL)
  164. goto end;
  165. X509_STORE_set_verify_cb(store, cb);
  166. if (vpmtouched)
  167. X509_STORE_set1_param(store, vpm);
  168. ERR_clear_error();
  169. if (crl_download)
  170. store_setup_crl_download(store);
  171. ret = 0;
  172. if (argc < 1) {
  173. if (check(store, NULL, untrusted, trusted, crls, show_chain) != 1)
  174. ret = -1;
  175. } else {
  176. for (i = 0; i < argc; i++)
  177. if (check(store, argv[i], untrusted, trusted, crls,
  178. show_chain) != 1)
  179. ret = -1;
  180. }
  181. end:
  182. X509_VERIFY_PARAM_free(vpm);
  183. X509_STORE_free(store);
  184. sk_X509_pop_free(untrusted, X509_free);
  185. sk_X509_pop_free(trusted, X509_free);
  186. sk_X509_CRL_pop_free(crls, X509_CRL_free);
  187. release_engine(e);
  188. return (ret < 0 ? 2 : ret);
  189. }
  190. static int check(X509_STORE *ctx, const char *file,
  191. STACK_OF(X509) *uchain, STACK_OF(X509) *tchain,
  192. STACK_OF(X509_CRL) *crls, int show_chain)
  193. {
  194. X509 *x = NULL;
  195. int i = 0, ret = 0;
  196. X509_STORE_CTX *csc;
  197. STACK_OF(X509) *chain = NULL;
  198. int num_untrusted;
  199. x = load_cert(file, FORMAT_PEM, "certificate file");
  200. if (x == NULL)
  201. goto end;
  202. csc = X509_STORE_CTX_new();
  203. if (csc == NULL) {
  204. printf("error %s: X.509 store context allocation failed\n",
  205. (file == NULL) ? "stdin" : file);
  206. goto end;
  207. }
  208. X509_STORE_set_flags(ctx, vflags);
  209. if (!X509_STORE_CTX_init(csc, ctx, x, uchain)) {
  210. X509_STORE_CTX_free(csc);
  211. printf("error %s: X.509 store context initialization failed\n",
  212. (file == NULL) ? "stdin" : file);
  213. goto end;
  214. }
  215. if (tchain != NULL)
  216. X509_STORE_CTX_set0_trusted_stack(csc, tchain);
  217. if (crls != NULL)
  218. X509_STORE_CTX_set0_crls(csc, crls);
  219. i = X509_verify_cert(csc);
  220. if (i > 0 && X509_STORE_CTX_get_error(csc) == X509_V_OK) {
  221. printf("%s: OK\n", (file == NULL) ? "stdin" : file);
  222. ret = 1;
  223. if (show_chain) {
  224. int j;
  225. chain = X509_STORE_CTX_get1_chain(csc);
  226. num_untrusted = X509_STORE_CTX_get_num_untrusted(csc);
  227. printf("Chain:\n");
  228. for (j = 0; j < sk_X509_num(chain); j++) {
  229. X509 *cert = sk_X509_value(chain, j);
  230. printf("depth=%d: ", j);
  231. X509_NAME_print_ex_fp(stdout,
  232. X509_get_subject_name(cert),
  233. 0, get_nameopt());
  234. if (j < num_untrusted)
  235. printf(" (untrusted)");
  236. printf("\n");
  237. }
  238. sk_X509_pop_free(chain, X509_free);
  239. }
  240. } else {
  241. printf("error %s: verification failed\n", (file == NULL) ? "stdin" : file);
  242. }
  243. X509_STORE_CTX_free(csc);
  244. end:
  245. if (i <= 0)
  246. ERR_print_errors(bio_err);
  247. X509_free(x);
  248. return ret;
  249. }
  250. static int cb(int ok, X509_STORE_CTX *ctx)
  251. {
  252. int cert_error = X509_STORE_CTX_get_error(ctx);
  253. X509 *current_cert = X509_STORE_CTX_get_current_cert(ctx);
  254. if (!ok) {
  255. if (current_cert != NULL) {
  256. X509_NAME_print_ex(bio_err,
  257. X509_get_subject_name(current_cert),
  258. 0, get_nameopt());
  259. BIO_printf(bio_err, "\n");
  260. }
  261. BIO_printf(bio_err, "%serror %d at %d depth lookup: %s\n",
  262. X509_STORE_CTX_get0_parent_ctx(ctx) ? "[CRL path] " : "",
  263. cert_error,
  264. X509_STORE_CTX_get_error_depth(ctx),
  265. X509_verify_cert_error_string(cert_error));
  266. switch (cert_error) {
  267. case X509_V_ERR_NO_EXPLICIT_POLICY:
  268. policies_print(ctx);
  269. /* fall thru */
  270. case X509_V_ERR_CERT_HAS_EXPIRED:
  271. /*
  272. * since we are just checking the certificates, it is ok if they
  273. * are self signed. But we should still warn the user.
  274. */
  275. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  276. /* Continue after extension errors too */
  277. case X509_V_ERR_INVALID_CA:
  278. case X509_V_ERR_INVALID_NON_CA:
  279. case X509_V_ERR_PATH_LENGTH_EXCEEDED:
  280. case X509_V_ERR_INVALID_PURPOSE:
  281. case X509_V_ERR_CRL_HAS_EXPIRED:
  282. case X509_V_ERR_CRL_NOT_YET_VALID:
  283. case X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION:
  284. ok = 1;
  285. }
  286. return ok;
  287. }
  288. if (cert_error == X509_V_OK && ok == 2)
  289. policies_print(ctx);
  290. if (!v_verbose)
  291. ERR_clear_error();
  292. return ok;
  293. }