verify.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. int vpmtouched = 0, crl_download = 0, show_chain = 0, i = 0, ret = 1;
  114. OPTION_CHOICE o;
  115. if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
  116. goto end;
  117. prog = opt_init(argc, argv, verify_options);
  118. while ((o = opt_next()) != OPT_EOF) {
  119. switch (o) {
  120. case OPT_EOF:
  121. case OPT_ERR:
  122. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  123. goto end;
  124. case OPT_HELP:
  125. opt_help(verify_options);
  126. BIO_printf(bio_err, "Recognized usages:\n");
  127. for (i = 0; i < X509_PURPOSE_get_count(); i++) {
  128. X509_PURPOSE *ptmp;
  129. ptmp = X509_PURPOSE_get0(i);
  130. BIO_printf(bio_err, "\t%-10s\t%s\n",
  131. X509_PURPOSE_get0_sname(ptmp),
  132. X509_PURPOSE_get0_name(ptmp));
  133. }
  134. BIO_printf(bio_err, "Recognized verify names:\n");
  135. for (i = 0; i < X509_VERIFY_PARAM_get_count(); i++) {
  136. const X509_VERIFY_PARAM *vptmp;
  137. vptmp = X509_VERIFY_PARAM_get0(i);
  138. BIO_printf(bio_err, "\t%-10s\n",
  139. X509_VERIFY_PARAM_get0_name(vptmp));
  140. }
  141. ret = 0;
  142. goto end;
  143. case OPT_V_CASES:
  144. if (!opt_verify(o, vpm))
  145. goto end;
  146. vpmtouched++;
  147. break;
  148. case OPT_CAPATH:
  149. CApath = opt_arg();
  150. break;
  151. case OPT_CAFILE:
  152. CAfile = opt_arg();
  153. break;
  154. case OPT_NOCAPATH:
  155. noCApath = 1;
  156. break;
  157. case OPT_NOCAFILE:
  158. noCAfile = 1;
  159. break;
  160. case OPT_UNTRUSTED:
  161. /* Zero or more times */
  162. if (!load_certs(opt_arg(), &untrusted, FORMAT_PEM, NULL, e,
  163. "untrusted certificates"))
  164. goto end;
  165. break;
  166. case OPT_TRUSTED:
  167. /* Zero or more times */
  168. noCAfile = 1;
  169. noCApath = 1;
  170. if (!load_certs(opt_arg(), &trusted, FORMAT_PEM, NULL, e,
  171. "trusted certificates"))
  172. goto end;
  173. break;
  174. case OPT_CRLFILE:
  175. /* Zero or more times */
  176. if (!load_crls(opt_arg(), &crls, FORMAT_PEM, NULL, e,
  177. "other CRLs"))
  178. goto end;
  179. break;
  180. case OPT_CRL_DOWNLOAD:
  181. crl_download = 1;
  182. break;
  183. case OPT_SHOW_CHAIN:
  184. show_chain = 1;
  185. break;
  186. case OPT_ENGINE:
  187. /* Specify *before* -trusted/-untrusted/-CRLfile */
  188. e = setup_engine(opt_arg(), 0);
  189. break;
  190. case OPT_VERBOSE:
  191. v_verbose = 1;
  192. break;
  193. }
  194. }
  195. argc = opt_num_rest();
  196. argv = opt_rest();
  197. if (trusted != NULL && (CAfile || CApath)) {
  198. BIO_printf(bio_err,
  199. "%s: Cannot use -trusted with -CAfile or -CApath\n",
  200. prog);
  201. goto end;
  202. }
  203. if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath)) == NULL)
  204. goto end;
  205. X509_STORE_set_verify_cb(store, cb);
  206. if (vpmtouched)
  207. X509_STORE_set1_param(store, vpm);
  208. ERR_clear_error();
  209. if (crl_download)
  210. store_setup_crl_download(store);
  211. ret = 0;
  212. if (argc < 1) {
  213. if (check(store, NULL, untrusted, trusted, crls, e, show_chain) != 1)
  214. ret = -1;
  215. } else {
  216. for (i = 0; i < argc; i++)
  217. if (check(store, argv[i], untrusted, trusted, crls, e,
  218. show_chain) != 1)
  219. ret = -1;
  220. }
  221. end:
  222. X509_VERIFY_PARAM_free(vpm);
  223. X509_STORE_free(store);
  224. sk_X509_pop_free(untrusted, X509_free);
  225. sk_X509_pop_free(trusted, X509_free);
  226. sk_X509_CRL_pop_free(crls, X509_CRL_free);
  227. return (ret < 0 ? 2 : ret);
  228. }
  229. static int check(X509_STORE *ctx, char *file,
  230. STACK_OF(X509) *uchain, STACK_OF(X509) *tchain,
  231. STACK_OF(X509_CRL) *crls, ENGINE *e, int show_chain)
  232. {
  233. X509 *x = NULL;
  234. int i = 0, ret = 0;
  235. X509_STORE_CTX *csc;
  236. STACK_OF(X509) *chain = NULL;
  237. int num_untrusted;
  238. x = load_cert(file, FORMAT_PEM, NULL, e, "certificate file");
  239. if (x == NULL)
  240. goto end;
  241. csc = X509_STORE_CTX_new();
  242. if (csc == NULL) {
  243. printf("error %s: X.509 store context allocation failed\n",
  244. (file == NULL) ? "stdin" : file);
  245. goto end;
  246. }
  247. X509_STORE_set_flags(ctx, vflags);
  248. if (!X509_STORE_CTX_init(csc, ctx, x, uchain)) {
  249. printf("error %s: X.509 store context initialization failed\n",
  250. (file == NULL) ? "stdin" : file);
  251. goto end;
  252. }
  253. if (tchain)
  254. X509_STORE_CTX_trusted_stack(csc, tchain);
  255. if (crls)
  256. X509_STORE_CTX_set0_crls(csc, crls);
  257. i = X509_verify_cert(csc);
  258. if (i > 0 && X509_STORE_CTX_get_error(csc) == X509_V_OK) {
  259. printf("%s: OK\n", (file == NULL) ? "stdin" : file);
  260. ret = 1;
  261. if (show_chain) {
  262. int j;
  263. chain = X509_STORE_CTX_get1_chain(csc);
  264. num_untrusted = X509_STORE_CTX_get_num_untrusted(csc);
  265. printf("Chain:\n");
  266. for (j = 0; j < sk_X509_num(chain); j++) {
  267. X509 *cert = sk_X509_value(chain, j);
  268. printf("depth=%d: ", j);
  269. X509_NAME_print_ex_fp(stdout,
  270. X509_get_subject_name(cert),
  271. 0, XN_FLAG_ONELINE);
  272. if (j < num_untrusted)
  273. printf(" (untrusted)");
  274. printf("\n");
  275. }
  276. sk_X509_pop_free(chain, X509_free);
  277. }
  278. } else {
  279. printf("error %s: verification failed\n", (file == NULL) ? "stdin" : file);
  280. }
  281. X509_STORE_CTX_free(csc);
  282. end:
  283. if (i <= 0)
  284. ERR_print_errors(bio_err);
  285. X509_free(x);
  286. return ret;
  287. }
  288. static int cb(int ok, X509_STORE_CTX *ctx)
  289. {
  290. int cert_error = X509_STORE_CTX_get_error(ctx);
  291. X509 *current_cert = X509_STORE_CTX_get_current_cert(ctx);
  292. if (!ok) {
  293. if (current_cert) {
  294. X509_NAME_print_ex(bio_err,
  295. X509_get_subject_name(current_cert),
  296. 0, XN_FLAG_ONELINE);
  297. BIO_printf(bio_err, "\n");
  298. }
  299. BIO_printf(bio_err, "%serror %d at %d depth lookup: %s\n",
  300. X509_STORE_CTX_get0_parent_ctx(ctx) ? "[CRL path] " : "",
  301. cert_error,
  302. X509_STORE_CTX_get_error_depth(ctx),
  303. X509_verify_cert_error_string(cert_error));
  304. switch (cert_error) {
  305. case X509_V_ERR_NO_EXPLICIT_POLICY:
  306. policies_print(ctx);
  307. case X509_V_ERR_CERT_HAS_EXPIRED:
  308. /*
  309. * since we are just checking the certificates, it is ok if they
  310. * are self signed. But we should still warn the user.
  311. */
  312. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  313. /* Continue after extension errors too */
  314. case X509_V_ERR_INVALID_CA:
  315. case X509_V_ERR_INVALID_NON_CA:
  316. case X509_V_ERR_PATH_LENGTH_EXCEEDED:
  317. case X509_V_ERR_INVALID_PURPOSE:
  318. case X509_V_ERR_CRL_HAS_EXPIRED:
  319. case X509_V_ERR_CRL_NOT_YET_VALID:
  320. case X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION:
  321. ok = 1;
  322. }
  323. return ok;
  324. }
  325. if (cert_error == X509_V_OK && ok == 2)
  326. policies_print(ctx);
  327. if (!v_verbose)
  328. ERR_clear_error();
  329. return (ok);
  330. }