2
0

verify.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. unsigned char *sm2id, size_t sm2idlen);
  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_NOCAPATH, OPT_NOCAFILE,
  28. OPT_UNTRUSTED, OPT_TRUSTED, OPT_CRLFILE, OPT_CRL_DOWNLOAD, OPT_SHOW_CHAIN,
  29. OPT_V_ENUM, OPT_NAMEOPT,
  30. OPT_VERBOSE, OPT_SM2ID, OPT_SM2HEXID
  31. } OPTION_CHOICE;
  32. const OPTIONS verify_options[] = {
  33. {OPT_HELP_STR, 1, '-', "Usage: %s [options] cert.pem...\n"},
  34. {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
  35. {"help", OPT_HELP, '-', "Display this summary"},
  36. {"verbose", OPT_VERBOSE, '-',
  37. "Print extra information about the operations being performed."},
  38. {"CApath", OPT_CAPATH, '/', "A directory of trusted certificates"},
  39. {"CAfile", OPT_CAFILE, '<', "A file of trusted certificates"},
  40. {"no-CAfile", OPT_NOCAFILE, '-',
  41. "Do not load the default certificates file"},
  42. {"no-CApath", OPT_NOCAPATH, '-',
  43. "Do not load certificates from the default certificates directory"},
  44. {"untrusted", OPT_UNTRUSTED, '<', "A file of untrusted certificates"},
  45. {"trusted", OPT_TRUSTED, '<', "A file of trusted certificates"},
  46. {"CRLfile", OPT_CRLFILE, '<',
  47. "File containing one or more CRL's (in PEM format) to load"},
  48. {"crl_download", OPT_CRL_DOWNLOAD, '-',
  49. "Attempt to download CRL information for this certificate"},
  50. {"show_chain", OPT_SHOW_CHAIN, '-',
  51. "Display information about the certificate chain"},
  52. {"nameopt", OPT_NAMEOPT, 's', "Various certificate name options"},
  53. OPT_V_OPTIONS,
  54. #ifndef OPENSSL_NO_ENGINE
  55. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  56. #endif
  57. #ifndef OPENSSL_NO_SM2
  58. {"sm2-id", OPT_SM2ID, 's',
  59. "Specify an ID string to verify an SM2 certificate"},
  60. {"sm2-hex-id", OPT_SM2HEXID, 's',
  61. "Specify a hex ID string to verify an SM2 certificate"},
  62. #endif
  63. {NULL}
  64. };
  65. int verify_main(int argc, char **argv)
  66. {
  67. ENGINE *e = NULL;
  68. STACK_OF(X509) *untrusted = NULL, *trusted = NULL;
  69. STACK_OF(X509_CRL) *crls = NULL;
  70. X509_STORE *store = NULL;
  71. X509_VERIFY_PARAM *vpm = NULL;
  72. const char *prog, *CApath = NULL, *CAfile = NULL;
  73. int noCApath = 0, noCAfile = 0;
  74. int vpmtouched = 0, crl_download = 0, show_chain = 0, i = 0, ret = 1;
  75. OPTION_CHOICE o;
  76. unsigned char *sm2_id = NULL;
  77. size_t sm2_idlen = 0;
  78. int sm2_free = 0;
  79. if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
  80. goto end;
  81. prog = opt_init(argc, argv, verify_options);
  82. while ((o = opt_next()) != OPT_EOF) {
  83. switch (o) {
  84. case OPT_EOF:
  85. case OPT_ERR:
  86. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  87. goto end;
  88. case OPT_HELP:
  89. opt_help(verify_options);
  90. BIO_printf(bio_err, "Recognized usages:\n");
  91. for (i = 0; i < X509_PURPOSE_get_count(); i++) {
  92. X509_PURPOSE *ptmp;
  93. ptmp = X509_PURPOSE_get0(i);
  94. BIO_printf(bio_err, "\t%-10s\t%s\n",
  95. X509_PURPOSE_get0_sname(ptmp),
  96. X509_PURPOSE_get0_name(ptmp));
  97. }
  98. BIO_printf(bio_err, "Recognized verify names:\n");
  99. for (i = 0; i < X509_VERIFY_PARAM_get_count(); i++) {
  100. const X509_VERIFY_PARAM *vptmp;
  101. vptmp = X509_VERIFY_PARAM_get0(i);
  102. BIO_printf(bio_err, "\t%-10s\n",
  103. X509_VERIFY_PARAM_get0_name(vptmp));
  104. }
  105. ret = 0;
  106. goto end;
  107. case OPT_V_CASES:
  108. if (!opt_verify(o, vpm))
  109. goto end;
  110. vpmtouched++;
  111. break;
  112. case OPT_CAPATH:
  113. CApath = opt_arg();
  114. break;
  115. case OPT_CAFILE:
  116. CAfile = opt_arg();
  117. break;
  118. case OPT_NOCAPATH:
  119. noCApath = 1;
  120. break;
  121. case OPT_NOCAFILE:
  122. noCAfile = 1;
  123. break;
  124. case OPT_UNTRUSTED:
  125. /* Zero or more times */
  126. if (!load_certs(opt_arg(), &untrusted, FORMAT_PEM, NULL,
  127. "untrusted certificates"))
  128. goto end;
  129. break;
  130. case OPT_TRUSTED:
  131. /* Zero or more times */
  132. noCAfile = 1;
  133. noCApath = 1;
  134. if (!load_certs(opt_arg(), &trusted, FORMAT_PEM, NULL,
  135. "trusted certificates"))
  136. goto end;
  137. break;
  138. case OPT_CRLFILE:
  139. /* Zero or more times */
  140. if (!load_crls(opt_arg(), &crls, FORMAT_PEM, NULL,
  141. "other CRLs"))
  142. goto end;
  143. break;
  144. case OPT_CRL_DOWNLOAD:
  145. crl_download = 1;
  146. break;
  147. case OPT_ENGINE:
  148. if ((e = setup_engine(opt_arg(), 0)) == NULL) {
  149. /* Failure message already displayed */
  150. goto end;
  151. }
  152. break;
  153. case OPT_SHOW_CHAIN:
  154. show_chain = 1;
  155. break;
  156. case OPT_NAMEOPT:
  157. if (!set_nameopt(opt_arg()))
  158. goto end;
  159. break;
  160. case OPT_VERBOSE:
  161. v_verbose = 1;
  162. break;
  163. case OPT_SM2ID:
  164. if (sm2_id != NULL) {
  165. BIO_printf(bio_err,
  166. "Use one of the options 'sm2-hex-id' or 'sm2-id' \n");
  167. goto end;
  168. }
  169. sm2_id = (unsigned char *)opt_arg();
  170. sm2_idlen = strlen((const char *)sm2_id);
  171. break;
  172. case OPT_SM2HEXID:
  173. if (sm2_id != NULL) {
  174. BIO_printf(bio_err,
  175. "Use one of the options 'sm2-hex-id' or 'sm2-id' \n");
  176. goto end;
  177. }
  178. /* try to parse the input as hex string first */
  179. sm2_free = 1;
  180. sm2_id = OPENSSL_hexstr2buf(opt_arg(), (long *)&sm2_idlen);
  181. if (sm2_id == NULL) {
  182. BIO_printf(bio_err, "Invalid hex string input\n");
  183. goto end;
  184. }
  185. break;
  186. }
  187. }
  188. argc = opt_num_rest();
  189. argv = opt_rest();
  190. if (trusted != NULL && (CAfile || CApath)) {
  191. BIO_printf(bio_err,
  192. "%s: Cannot use -trusted with -CAfile or -CApath\n",
  193. prog);
  194. goto end;
  195. }
  196. if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath)) == NULL)
  197. goto end;
  198. X509_STORE_set_verify_cb(store, cb);
  199. if (vpmtouched)
  200. X509_STORE_set1_param(store, vpm);
  201. ERR_clear_error();
  202. if (crl_download)
  203. store_setup_crl_download(store);
  204. ret = 0;
  205. if (argc < 1) {
  206. if (check(store, NULL, untrusted, trusted, crls, show_chain,
  207. sm2_id, sm2_idlen) != 1)
  208. ret = -1;
  209. } else {
  210. for (i = 0; i < argc; i++)
  211. if (check(store, argv[i], untrusted, trusted, crls,
  212. show_chain, sm2_id, sm2_idlen) != 1)
  213. ret = -1;
  214. }
  215. end:
  216. if (sm2_free)
  217. OPENSSL_free(sm2_id);
  218. X509_VERIFY_PARAM_free(vpm);
  219. X509_STORE_free(store);
  220. sk_X509_pop_free(untrusted, X509_free);
  221. sk_X509_pop_free(trusted, X509_free);
  222. sk_X509_CRL_pop_free(crls, X509_CRL_free);
  223. release_engine(e);
  224. return (ret < 0 ? 2 : ret);
  225. }
  226. static int check(X509_STORE *ctx, const char *file,
  227. STACK_OF(X509) *uchain, STACK_OF(X509) *tchain,
  228. STACK_OF(X509_CRL) *crls, int show_chain,
  229. unsigned char *sm2id, size_t sm2idlen)
  230. {
  231. X509 *x = NULL;
  232. int i = 0, ret = 0;
  233. X509_STORE_CTX *csc;
  234. STACK_OF(X509) *chain = NULL;
  235. int num_untrusted;
  236. x = load_cert(file, FORMAT_PEM, "certificate file");
  237. if (x == NULL)
  238. goto end;
  239. if (sm2id != NULL) {
  240. #ifndef OPENSSL_NO_SM2
  241. ASN1_OCTET_STRING *v;
  242. v = ASN1_OCTET_STRING_new();
  243. if (v == NULL) {
  244. BIO_printf(bio_err, "error: SM2 ID allocation failed\n");
  245. goto end;
  246. }
  247. if (!ASN1_OCTET_STRING_set(v, sm2id, sm2idlen)) {
  248. BIO_printf(bio_err, "error: setting SM2 ID failed\n");
  249. ASN1_OCTET_STRING_free(v);
  250. goto end;
  251. }
  252. X509_set0_sm2_id(x, v);
  253. #endif
  254. }
  255. csc = X509_STORE_CTX_new();
  256. if (csc == NULL) {
  257. BIO_printf(bio_err, "error %s: X.509 store context allocation failed\n",
  258. (file == NULL) ? "stdin" : file);
  259. goto end;
  260. }
  261. X509_STORE_set_flags(ctx, vflags);
  262. if (!X509_STORE_CTX_init(csc, ctx, x, uchain)) {
  263. X509_STORE_CTX_free(csc);
  264. BIO_printf(bio_err,
  265. "error %s: X.509 store context initialization failed\n",
  266. (file == NULL) ? "stdin" : file);
  267. goto end;
  268. }
  269. if (tchain != NULL)
  270. X509_STORE_CTX_set0_trusted_stack(csc, tchain);
  271. if (crls != NULL)
  272. X509_STORE_CTX_set0_crls(csc, crls);
  273. i = X509_verify_cert(csc);
  274. if (i > 0 && X509_STORE_CTX_get_error(csc) == X509_V_OK) {
  275. BIO_printf(bio_out, "%s: OK\n", (file == NULL) ? "stdin" : file);
  276. ret = 1;
  277. if (show_chain) {
  278. int j;
  279. chain = X509_STORE_CTX_get1_chain(csc);
  280. num_untrusted = X509_STORE_CTX_get_num_untrusted(csc);
  281. BIO_printf(bio_out, "Chain:\n");
  282. for (j = 0; j < sk_X509_num(chain); j++) {
  283. X509 *cert = sk_X509_value(chain, j);
  284. BIO_printf(bio_out, "depth=%d: ", j);
  285. X509_NAME_print_ex_fp(stdout,
  286. X509_get_subject_name(cert),
  287. 0, get_nameopt());
  288. if (j < num_untrusted)
  289. BIO_printf(bio_out, " (untrusted)");
  290. BIO_printf(bio_out, "\n");
  291. }
  292. sk_X509_pop_free(chain, X509_free);
  293. }
  294. } else {
  295. BIO_printf(bio_err,
  296. "error %s: verification failed\n",
  297. (file == NULL) ? "stdin" : file);
  298. }
  299. X509_STORE_CTX_free(csc);
  300. end:
  301. if (i <= 0)
  302. ERR_print_errors(bio_err);
  303. X509_free(x);
  304. return ret;
  305. }
  306. static int cb(int ok, X509_STORE_CTX *ctx)
  307. {
  308. int cert_error = X509_STORE_CTX_get_error(ctx);
  309. X509 *current_cert = X509_STORE_CTX_get_current_cert(ctx);
  310. if (!ok) {
  311. if (current_cert != NULL) {
  312. X509_NAME_print_ex(bio_err,
  313. X509_get_subject_name(current_cert),
  314. 0, get_nameopt());
  315. BIO_printf(bio_err, "\n");
  316. }
  317. BIO_printf(bio_err, "%serror %d at %d depth lookup: %s\n",
  318. X509_STORE_CTX_get0_parent_ctx(ctx) ? "[CRL path] " : "",
  319. cert_error,
  320. X509_STORE_CTX_get_error_depth(ctx),
  321. X509_verify_cert_error_string(cert_error));
  322. /*
  323. * Pretend that some errors are ok, so they don't stop further
  324. * processing of the certificate chain. Setting ok = 1 does this.
  325. * After X509_verify_cert() is done, we verify that there were
  326. * no actual errors, even if the returned value was positive.
  327. */
  328. switch (cert_error) {
  329. case X509_V_ERR_NO_EXPLICIT_POLICY:
  330. policies_print(ctx);
  331. /* fall thru */
  332. case X509_V_ERR_CERT_HAS_EXPIRED:
  333. /* Continue even if the leaf is a self signed cert */
  334. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  335. /* Continue after extension errors too */
  336. case X509_V_ERR_INVALID_CA:
  337. case X509_V_ERR_INVALID_NON_CA:
  338. case X509_V_ERR_PATH_LENGTH_EXCEEDED:
  339. case X509_V_ERR_INVALID_PURPOSE:
  340. case X509_V_ERR_CRL_HAS_EXPIRED:
  341. case X509_V_ERR_CRL_NOT_YET_VALID:
  342. case X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION:
  343. ok = 1;
  344. }
  345. return ok;
  346. }
  347. if (cert_error == X509_V_OK && ok == 2)
  348. policies_print(ctx);
  349. if (!v_verbose)
  350. ERR_clear_error();
  351. return ok;
  352. }