verify.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. #include <string.h>
  60. #include "apps.h"
  61. #include <openssl/bio.h>
  62. #include <openssl/err.h>
  63. #include <openssl/x509.h>
  64. #include <openssl/x509v3.h>
  65. #include <openssl/pem.h>
  66. static int cb(int ok, X509_STORE_CTX *ctx);
  67. static int check(X509_STORE *ctx, char *file,
  68. STACK_OF(X509) *uchain, STACK_OF(X509) *tchain,
  69. STACK_OF(X509_CRL) *crls, ENGINE *e, int show_chain);
  70. static int v_verbose = 0, vflags = 0;
  71. typedef enum OPTION_choice {
  72. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  73. OPT_ENGINE, OPT_CAPATH, OPT_CAFILE, OPT_NOCAPATH, OPT_NOCAFILE,
  74. OPT_UNTRUSTED, OPT_TRUSTED, OPT_CRLFILE, OPT_CRL_DOWNLOAD, OPT_SHOW_CHAIN,
  75. OPT_V_ENUM,
  76. OPT_VERBOSE
  77. } OPTION_CHOICE;
  78. OPTIONS verify_options[] = {
  79. {OPT_HELP_STR, 1, '-', "Usage: %s [options] cert.pem...\n"},
  80. {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
  81. {"help", OPT_HELP, '-', "Display this summary"},
  82. {"verbose", OPT_VERBOSE, '-',
  83. "Print extra information about the operations being performed."},
  84. {"CApath", OPT_CAPATH, '/', "A directory of trusted certificates"},
  85. {"CAfile", OPT_CAFILE, '<', "A file of trusted certificates"},
  86. {"no-CAfile", OPT_NOCAFILE, '-',
  87. "Do not load the default certificates file"},
  88. {"no-CApath", OPT_NOCAPATH, '-',
  89. "Do not load certificates from the default certificates directory"},
  90. {"untrusted", OPT_UNTRUSTED, '<', "A file of untrusted certificates"},
  91. {"trusted", OPT_TRUSTED, '<', "A file of trusted certificates"},
  92. {"CRLfile", OPT_CRLFILE, '<',
  93. "File containing one or more CRL's (in PEM format) to load"},
  94. {"crl_download", OPT_CRL_DOWNLOAD, '-',
  95. "Attempt to download CRL information for this certificate"},
  96. {"show_chain", OPT_SHOW_CHAIN, '-',
  97. "Display information about the certificate chain"},
  98. OPT_V_OPTIONS,
  99. #ifndef OPENSSL_NO_ENGINE
  100. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  101. #endif
  102. {NULL}
  103. };
  104. int verify_main(int argc, char **argv)
  105. {
  106. ENGINE *e = NULL;
  107. STACK_OF(X509) *untrusted = NULL, *trusted = NULL;
  108. STACK_OF(X509_CRL) *crls = NULL;
  109. X509_STORE *store = NULL;
  110. X509_VERIFY_PARAM *vpm = NULL;
  111. char *prog, *CApath = NULL, *CAfile = NULL;
  112. int noCApath = 0, noCAfile = 0;
  113. char *untfile = NULL, *trustfile = NULL, *crlfile = NULL;
  114. int vpmtouched = 0, crl_download = 0, show_chain = 0, i = 0, ret = 1;
  115. OPTION_CHOICE o;
  116. if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
  117. goto end;
  118. prog = opt_init(argc, argv, verify_options);
  119. while ((o = opt_next()) != OPT_EOF) {
  120. switch (o) {
  121. case OPT_EOF:
  122. case OPT_ERR:
  123. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  124. goto end;
  125. case OPT_HELP:
  126. opt_help(verify_options);
  127. BIO_printf(bio_err, "Recognized usages:\n");
  128. for (i = 0; i < X509_PURPOSE_get_count(); i++) {
  129. X509_PURPOSE *ptmp;
  130. ptmp = X509_PURPOSE_get0(i);
  131. BIO_printf(bio_err, "\t%-10s\t%s\n",
  132. X509_PURPOSE_get0_sname(ptmp),
  133. X509_PURPOSE_get0_name(ptmp));
  134. }
  135. BIO_printf(bio_err, "Recognized verify names:\n");
  136. for (i = 0; i < X509_VERIFY_PARAM_get_count(); i++) {
  137. const X509_VERIFY_PARAM *vptmp;
  138. vptmp = X509_VERIFY_PARAM_get0(i);
  139. BIO_printf(bio_err, "\t%-10s\n",
  140. X509_VERIFY_PARAM_get0_name(vptmp));
  141. }
  142. ret = 0;
  143. goto end;
  144. case OPT_V_CASES:
  145. if (!opt_verify(o, vpm))
  146. goto end;
  147. vpmtouched++;
  148. break;
  149. case OPT_CAPATH:
  150. CApath = opt_arg();
  151. break;
  152. case OPT_CAFILE:
  153. CAfile = opt_arg();
  154. break;
  155. case OPT_NOCAPATH:
  156. noCApath = 1;
  157. break;
  158. case OPT_NOCAFILE:
  159. noCAfile = 1;
  160. break;
  161. case OPT_UNTRUSTED:
  162. untfile = opt_arg();
  163. break;
  164. case OPT_TRUSTED:
  165. trustfile = opt_arg();
  166. break;
  167. case OPT_CRLFILE:
  168. crlfile = opt_arg();
  169. break;
  170. case OPT_CRL_DOWNLOAD:
  171. crl_download = 1;
  172. break;
  173. case OPT_SHOW_CHAIN:
  174. show_chain = 1;
  175. break;
  176. case OPT_ENGINE:
  177. e = setup_engine(opt_arg(), 0);
  178. break;
  179. case OPT_VERBOSE:
  180. v_verbose = 1;
  181. break;
  182. }
  183. }
  184. argc = opt_num_rest();
  185. argv = opt_rest();
  186. if (trustfile && (CAfile || CApath)) {
  187. BIO_printf(bio_err,
  188. "%s: Cannot use -trusted with -CAfile or -CApath\n",
  189. prog);
  190. goto end;
  191. }
  192. if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath)) == 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 (untfile) {
  199. untrusted = load_certs(untfile, FORMAT_PEM,
  200. NULL, e, "untrusted certificates");
  201. if (!untrusted)
  202. goto end;
  203. }
  204. if (trustfile) {
  205. trusted = load_certs(trustfile, FORMAT_PEM,
  206. NULL, e, "trusted certificates");
  207. if (!trusted)
  208. goto end;
  209. }
  210. if (crlfile) {
  211. crls = load_crls(crlfile, FORMAT_PEM, NULL, e, "other CRLs");
  212. if (!crls)
  213. goto end;
  214. }
  215. if (crl_download)
  216. store_setup_crl_download(store);
  217. ret = 0;
  218. if (argc < 1) {
  219. if (check(store, NULL, untrusted, trusted, crls, e, show_chain) != 1)
  220. ret = -1;
  221. } else {
  222. for (i = 0; i < argc; i++)
  223. if (check(store, argv[i], untrusted, trusted, crls, e,
  224. show_chain) != 1)
  225. ret = -1;
  226. }
  227. end:
  228. X509_VERIFY_PARAM_free(vpm);
  229. X509_STORE_free(store);
  230. sk_X509_pop_free(untrusted, X509_free);
  231. sk_X509_pop_free(trusted, X509_free);
  232. sk_X509_CRL_pop_free(crls, X509_CRL_free);
  233. return (ret < 0 ? 2 : ret);
  234. }
  235. static int check(X509_STORE *ctx, char *file,
  236. STACK_OF(X509) *uchain, STACK_OF(X509) *tchain,
  237. STACK_OF(X509_CRL) *crls, ENGINE *e, int show_chain)
  238. {
  239. X509 *x = NULL;
  240. int i = 0, ret = 0;
  241. X509_STORE_CTX *csc;
  242. STACK_OF(X509) *chain = NULL;
  243. int num_untrusted;
  244. x = load_cert(file, FORMAT_PEM, NULL, e, "certificate file");
  245. if (x == NULL)
  246. goto end;
  247. csc = X509_STORE_CTX_new();
  248. if (csc == NULL) {
  249. printf("error %s: X.509 store context allocation failed\n",
  250. (file == NULL) ? "stdin" : file);
  251. goto end;
  252. }
  253. X509_STORE_set_flags(ctx, vflags);
  254. if (!X509_STORE_CTX_init(csc, ctx, x, uchain)) {
  255. printf("error %s: X.509 store context initialization failed\n",
  256. (file == NULL) ? "stdin" : file);
  257. goto end;
  258. }
  259. if (tchain)
  260. X509_STORE_CTX_trusted_stack(csc, tchain);
  261. if (crls)
  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. printf("%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. printf("Chain:\n");
  272. for (j = 0; j < sk_X509_num(chain); j++) {
  273. X509 *cert = sk_X509_value(chain, j);
  274. printf("depth=%d: ", j);
  275. X509_NAME_print_ex_fp(stdout,
  276. X509_get_subject_name(cert),
  277. 0, XN_FLAG_ONELINE);
  278. if (j < num_untrusted)
  279. printf(" (untrusted)");
  280. printf("\n");
  281. }
  282. sk_X509_pop_free(chain, X509_free);
  283. }
  284. } else {
  285. printf("error %s: verification failed\n", (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) {
  300. X509_NAME_print_ex(bio_err,
  301. X509_get_subject_name(current_cert),
  302. 0, XN_FLAG_ONELINE);
  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. switch (cert_error) {
  311. case X509_V_ERR_NO_EXPLICIT_POLICY:
  312. policies_print(ctx);
  313. case X509_V_ERR_CERT_HAS_EXPIRED:
  314. /*
  315. * since we are just checking the certificates, it is ok if they
  316. * are self signed. But we should still warn the user.
  317. */
  318. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  319. /* Continue after extension errors too */
  320. case X509_V_ERR_INVALID_CA:
  321. case X509_V_ERR_INVALID_NON_CA:
  322. case X509_V_ERR_PATH_LENGTH_EXCEEDED:
  323. case X509_V_ERR_INVALID_PURPOSE:
  324. case X509_V_ERR_CRL_HAS_EXPIRED:
  325. case X509_V_ERR_CRL_NOT_YET_VALID:
  326. case X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION:
  327. ok = 1;
  328. }
  329. return ok;
  330. }
  331. if (cert_error == X509_V_OK && ok == 2)
  332. policies_print(ctx);
  333. if (!v_verbose)
  334. ERR_clear_error();
  335. return (ok);
  336. }