storeutl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * Copyright 2016-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 <openssl/opensslconf.h>
  10. #include "apps.h"
  11. #include "progs.h"
  12. #include <openssl/err.h>
  13. #include <openssl/pem.h>
  14. #include <openssl/store.h>
  15. #include <openssl/x509v3.h> /* s2i_ASN1_INTEGER */
  16. static int process(const char *uri, const UI_METHOD *uimeth, PW_CB_DATA *uidata,
  17. int expected, int criterion, OSSL_STORE_SEARCH *search,
  18. int text, int noout, int recursive, int indent, BIO *out,
  19. const char *prog);
  20. typedef enum OPTION_choice {
  21. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, OPT_ENGINE, OPT_OUT, OPT_PASSIN,
  22. OPT_NOOUT, OPT_TEXT, OPT_RECURSIVE,
  23. OPT_SEARCHFOR_CERTS, OPT_SEARCHFOR_KEYS, OPT_SEARCHFOR_CRLS,
  24. OPT_CRITERION_SUBJECT, OPT_CRITERION_ISSUER, OPT_CRITERION_SERIAL,
  25. OPT_CRITERION_FINGERPRINT, OPT_CRITERION_ALIAS,
  26. OPT_MD
  27. } OPTION_CHOICE;
  28. const OPTIONS storeutl_options[] = {
  29. {OPT_HELP_STR, 1, '-', "Usage: %s [options] uri\nValid options are:\n"},
  30. {"help", OPT_HELP, '-', "Display this summary"},
  31. {"out", OPT_OUT, '>', "Output file - default stdout"},
  32. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  33. {"text", OPT_TEXT, '-', "Print a text form of the objects"},
  34. {"noout", OPT_NOOUT, '-', "No PEM output, just status"},
  35. {"certs", OPT_SEARCHFOR_CERTS, '-', "Search for certificates only"},
  36. {"keys", OPT_SEARCHFOR_KEYS, '-', "Search for keys only"},
  37. {"crls", OPT_SEARCHFOR_CRLS, '-', "Search for CRLs only"},
  38. {"subject", OPT_CRITERION_SUBJECT, 's', "Search by subject"},
  39. {"issuer", OPT_CRITERION_ISSUER, 's', "Search by issuer and serial, issuer name"},
  40. {"serial", OPT_CRITERION_SERIAL, 's', "Search by issuer and serial, serial number"},
  41. {"fingerprint", OPT_CRITERION_FINGERPRINT, 's', "Search by public key fingerprint, given in hex"},
  42. {"alias", OPT_CRITERION_ALIAS, 's', "Search by alias"},
  43. {"", OPT_MD, '-', "Any supported digest"},
  44. #ifndef OPENSSL_NO_ENGINE
  45. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  46. #endif
  47. {"r", OPT_RECURSIVE, '-', "Recurse through names"},
  48. {NULL}
  49. };
  50. int storeutl_main(int argc, char *argv[])
  51. {
  52. int ret = 1, noout = 0, text = 0, recursive = 0;
  53. char *outfile = NULL, *passin = NULL, *passinarg = NULL;
  54. BIO *out = NULL;
  55. ENGINE *e = NULL;
  56. OPTION_CHOICE o;
  57. char *prog = opt_init(argc, argv, storeutl_options);
  58. PW_CB_DATA pw_cb_data;
  59. int expected = 0;
  60. int criterion = 0;
  61. X509_NAME *subject = NULL, *issuer = NULL;
  62. ASN1_INTEGER *serial = NULL;
  63. unsigned char *fingerprint = NULL;
  64. size_t fingerprintlen = 0;
  65. char *alias = NULL;
  66. OSSL_STORE_SEARCH *search = NULL;
  67. const EVP_MD *digest = NULL;
  68. while ((o = opt_next()) != OPT_EOF) {
  69. switch (o) {
  70. case OPT_EOF:
  71. case OPT_ERR:
  72. opthelp:
  73. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  74. goto end;
  75. case OPT_HELP:
  76. opt_help(storeutl_options);
  77. ret = 0;
  78. goto end;
  79. case OPT_OUT:
  80. outfile = opt_arg();
  81. break;
  82. case OPT_PASSIN:
  83. passinarg = opt_arg();
  84. break;
  85. case OPT_NOOUT:
  86. noout = 1;
  87. break;
  88. case OPT_TEXT:
  89. text = 1;
  90. break;
  91. case OPT_RECURSIVE:
  92. recursive = 1;
  93. break;
  94. case OPT_SEARCHFOR_CERTS:
  95. case OPT_SEARCHFOR_KEYS:
  96. case OPT_SEARCHFOR_CRLS:
  97. if (expected != 0) {
  98. BIO_printf(bio_err, "%s: only one search type can be given.\n",
  99. prog);
  100. goto end;
  101. }
  102. {
  103. static const struct {
  104. enum OPTION_choice choice;
  105. int type;
  106. } map[] = {
  107. {OPT_SEARCHFOR_CERTS, OSSL_STORE_INFO_CERT},
  108. {OPT_SEARCHFOR_KEYS, OSSL_STORE_INFO_PKEY},
  109. {OPT_SEARCHFOR_CRLS, OSSL_STORE_INFO_CRL},
  110. };
  111. size_t i;
  112. for (i = 0; i < OSSL_NELEM(map); i++) {
  113. if (o == map[i].choice) {
  114. expected = map[i].type;
  115. break;
  116. }
  117. }
  118. /*
  119. * If expected wasn't set at this point, it means the map
  120. * isn't syncronised with the possible options leading here.
  121. */
  122. OPENSSL_assert(expected != 0);
  123. }
  124. break;
  125. case OPT_CRITERION_SUBJECT:
  126. if (criterion != 0) {
  127. BIO_printf(bio_err, "%s: criterion already given.\n",
  128. prog);
  129. goto end;
  130. }
  131. criterion = OSSL_STORE_SEARCH_BY_NAME;
  132. if (subject != NULL) {
  133. BIO_printf(bio_err, "%s: subject already given.\n",
  134. prog);
  135. goto end;
  136. }
  137. if ((subject = parse_name(opt_arg(), MBSTRING_UTF8, 1)) == NULL) {
  138. BIO_printf(bio_err, "%s: can't parse subject argument.\n",
  139. prog);
  140. goto end;
  141. }
  142. break;
  143. case OPT_CRITERION_ISSUER:
  144. if (criterion != 0
  145. || (criterion == OSSL_STORE_SEARCH_BY_ISSUER_SERIAL
  146. && issuer != NULL)) {
  147. BIO_printf(bio_err, "%s: criterion already given.\n",
  148. prog);
  149. goto end;
  150. }
  151. criterion = OSSL_STORE_SEARCH_BY_ISSUER_SERIAL;
  152. if (issuer != NULL) {
  153. BIO_printf(bio_err, "%s: issuer already given.\n",
  154. prog);
  155. goto end;
  156. }
  157. if ((issuer = parse_name(opt_arg(), MBSTRING_UTF8, 1)) == NULL) {
  158. BIO_printf(bio_err, "%s: can't parse issuer argument.\n",
  159. prog);
  160. goto end;
  161. }
  162. break;
  163. case OPT_CRITERION_SERIAL:
  164. if (criterion != 0
  165. || (criterion == OSSL_STORE_SEARCH_BY_ISSUER_SERIAL
  166. && serial != NULL)) {
  167. BIO_printf(bio_err, "%s: criterion already given.\n",
  168. prog);
  169. goto end;
  170. }
  171. criterion = OSSL_STORE_SEARCH_BY_ISSUER_SERIAL;
  172. if (serial != NULL) {
  173. BIO_printf(bio_err, "%s: serial number already given.\n",
  174. prog);
  175. goto end;
  176. }
  177. if ((serial = s2i_ASN1_INTEGER(NULL, opt_arg())) == NULL) {
  178. BIO_printf(bio_err, "%s: can't parse serial number argument.\n",
  179. prog);
  180. goto end;
  181. }
  182. break;
  183. case OPT_CRITERION_FINGERPRINT:
  184. if (criterion != 0
  185. || (criterion == OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT
  186. && fingerprint != NULL)) {
  187. BIO_printf(bio_err, "%s: criterion already given.\n",
  188. prog);
  189. goto end;
  190. }
  191. criterion = OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT;
  192. if (fingerprint != NULL) {
  193. BIO_printf(bio_err, "%s: fingerprint already given.\n",
  194. prog);
  195. goto end;
  196. }
  197. {
  198. long tmplen = 0;
  199. if ((fingerprint = OPENSSL_hexstr2buf(opt_arg(), &tmplen))
  200. == NULL) {
  201. BIO_printf(bio_err,
  202. "%s: can't parse fingerprint argument.\n",
  203. prog);
  204. goto end;
  205. }
  206. fingerprintlen = (size_t)tmplen;
  207. }
  208. break;
  209. case OPT_CRITERION_ALIAS:
  210. if (criterion != 0) {
  211. BIO_printf(bio_err, "%s: criterion already given.\n",
  212. prog);
  213. goto end;
  214. }
  215. criterion = OSSL_STORE_SEARCH_BY_ALIAS;
  216. if (alias != NULL) {
  217. BIO_printf(bio_err, "%s: alias already given.\n",
  218. prog);
  219. goto end;
  220. }
  221. if ((alias = OPENSSL_strdup(opt_arg())) == NULL) {
  222. BIO_printf(bio_err, "%s: can't parse alias argument.\n",
  223. prog);
  224. goto end;
  225. }
  226. break;
  227. case OPT_ENGINE:
  228. e = setup_engine(opt_arg(), 0);
  229. break;
  230. case OPT_MD:
  231. if (!opt_md(opt_unknown(), &digest))
  232. goto opthelp;
  233. }
  234. }
  235. argc = opt_num_rest();
  236. argv = opt_rest();
  237. if (argc == 0) {
  238. BIO_printf(bio_err, "%s: No URI given, nothing to do...\n", prog);
  239. goto opthelp;
  240. }
  241. if (argc > 1) {
  242. BIO_printf(bio_err, "%s: Unknown extra parameters after URI\n", prog);
  243. goto opthelp;
  244. }
  245. if (criterion != 0) {
  246. switch (criterion) {
  247. case OSSL_STORE_SEARCH_BY_NAME:
  248. if ((search = OSSL_STORE_SEARCH_by_name(subject)) == NULL) {
  249. ERR_print_errors(bio_err);
  250. goto end;
  251. }
  252. break;
  253. case OSSL_STORE_SEARCH_BY_ISSUER_SERIAL:
  254. if (issuer == NULL || serial == NULL) {
  255. BIO_printf(bio_err,
  256. "%s: both -issuer and -serial must be given.\n",
  257. prog);
  258. goto end;
  259. }
  260. if ((search = OSSL_STORE_SEARCH_by_issuer_serial(issuer, serial))
  261. == NULL) {
  262. ERR_print_errors(bio_err);
  263. goto end;
  264. }
  265. break;
  266. case OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT:
  267. if ((search = OSSL_STORE_SEARCH_by_key_fingerprint(digest,
  268. fingerprint,
  269. fingerprintlen))
  270. == NULL) {
  271. ERR_print_errors(bio_err);
  272. goto end;
  273. }
  274. break;
  275. case OSSL_STORE_SEARCH_BY_ALIAS:
  276. if ((search = OSSL_STORE_SEARCH_by_alias(alias)) == NULL) {
  277. ERR_print_errors(bio_err);
  278. goto end;
  279. }
  280. break;
  281. }
  282. }
  283. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  284. BIO_printf(bio_err, "Error getting passwords\n");
  285. goto end;
  286. }
  287. pw_cb_data.password = passin;
  288. pw_cb_data.prompt_info = argv[0];
  289. out = bio_open_default(outfile, 'w', FORMAT_TEXT);
  290. if (out == NULL)
  291. goto end;
  292. ret = process(argv[0], get_ui_method(), &pw_cb_data,
  293. expected, criterion, search,
  294. text, noout, recursive, 0, out, prog);
  295. end:
  296. OPENSSL_free(fingerprint);
  297. OPENSSL_free(alias);
  298. ASN1_INTEGER_free(serial);
  299. X509_NAME_free(subject);
  300. X509_NAME_free(issuer);
  301. OSSL_STORE_SEARCH_free(search);
  302. BIO_free_all(out);
  303. OPENSSL_free(passin);
  304. release_engine(e);
  305. return ret;
  306. }
  307. static int indent_printf(int indent, BIO *bio, const char *format, ...)
  308. {
  309. va_list args;
  310. int ret;
  311. va_start(args, format);
  312. ret = BIO_printf(bio, "%*s", indent, "") + BIO_vprintf(bio, format, args);
  313. va_end(args);
  314. return ret;
  315. }
  316. static int process(const char *uri, const UI_METHOD *uimeth, PW_CB_DATA *uidata,
  317. int expected, int criterion, OSSL_STORE_SEARCH *search,
  318. int text, int noout, int recursive, int indent, BIO *out,
  319. const char *prog)
  320. {
  321. OSSL_STORE_CTX *store_ctx = NULL;
  322. int ret = 1, items = 0;
  323. if ((store_ctx = OSSL_STORE_open(uri, uimeth, uidata, NULL, NULL))
  324. == NULL) {
  325. BIO_printf(bio_err, "Couldn't open file or uri %s\n", uri);
  326. ERR_print_errors(bio_err);
  327. return ret;
  328. }
  329. if (expected != 0) {
  330. if (!OSSL_STORE_expect(store_ctx, expected)) {
  331. ERR_print_errors(bio_err);
  332. goto end2;
  333. }
  334. }
  335. if (criterion != 0) {
  336. if (!OSSL_STORE_supports_search(store_ctx, criterion)) {
  337. BIO_printf(bio_err,
  338. "%s: the store scheme doesn't support the given search criteria.\n",
  339. prog);
  340. goto end2;
  341. }
  342. if (!OSSL_STORE_find(store_ctx, search)) {
  343. ERR_print_errors(bio_err);
  344. goto end2;
  345. }
  346. }
  347. /* From here on, we count errors, and we'll return the count at the end */
  348. ret = 0;
  349. for (;;) {
  350. OSSL_STORE_INFO *info = OSSL_STORE_load(store_ctx);
  351. int type = info == NULL ? 0 : OSSL_STORE_INFO_get_type(info);
  352. const char *infostr =
  353. info == NULL ? NULL : OSSL_STORE_INFO_type_string(type);
  354. if (info == NULL) {
  355. if (OSSL_STORE_eof(store_ctx))
  356. break;
  357. if (OSSL_STORE_error(store_ctx)) {
  358. if (recursive)
  359. ERR_clear_error();
  360. else
  361. ERR_print_errors(bio_err);
  362. ret++;
  363. continue;
  364. }
  365. BIO_printf(bio_err,
  366. "ERROR: OSSL_STORE_load() returned NULL without "
  367. "eof or error indications\n");
  368. BIO_printf(bio_err, " This is an error in the loader\n");
  369. ERR_print_errors(bio_err);
  370. ret++;
  371. break;
  372. }
  373. if (type == OSSL_STORE_INFO_NAME) {
  374. const char *name = OSSL_STORE_INFO_get0_NAME(info);
  375. const char *desc = OSSL_STORE_INFO_get0_NAME_description(info);
  376. indent_printf(indent, bio_out, "%d: %s: %s\n", items, infostr,
  377. name);
  378. if (desc != NULL)
  379. indent_printf(indent, bio_out, "%s\n", desc);
  380. } else {
  381. indent_printf(indent, bio_out, "%d: %s\n", items, infostr);
  382. }
  383. /*
  384. * Unfortunately, PEM_X509_INFO_write_bio() is sorely lacking in
  385. * functionality, so we must figure out how exactly to write things
  386. * ourselves...
  387. */
  388. switch (type) {
  389. case OSSL_STORE_INFO_NAME:
  390. if (recursive) {
  391. const char *suburi = OSSL_STORE_INFO_get0_NAME(info);
  392. ret += process(suburi, uimeth, uidata,
  393. expected, criterion, search,
  394. text, noout, recursive, indent + 2, out, prog);
  395. }
  396. break;
  397. case OSSL_STORE_INFO_PARAMS:
  398. if (text)
  399. EVP_PKEY_print_params(out, OSSL_STORE_INFO_get0_PARAMS(info),
  400. 0, NULL);
  401. if (!noout)
  402. PEM_write_bio_Parameters(out,
  403. OSSL_STORE_INFO_get0_PARAMS(info));
  404. break;
  405. case OSSL_STORE_INFO_PKEY:
  406. if (text)
  407. EVP_PKEY_print_private(out, OSSL_STORE_INFO_get0_PKEY(info),
  408. 0, NULL);
  409. if (!noout)
  410. PEM_write_bio_PrivateKey(out, OSSL_STORE_INFO_get0_PKEY(info),
  411. NULL, NULL, 0, NULL, NULL);
  412. break;
  413. case OSSL_STORE_INFO_CERT:
  414. if (text)
  415. X509_print(out, OSSL_STORE_INFO_get0_CERT(info));
  416. if (!noout)
  417. PEM_write_bio_X509(out, OSSL_STORE_INFO_get0_CERT(info));
  418. break;
  419. case OSSL_STORE_INFO_CRL:
  420. if (text)
  421. X509_CRL_print(out, OSSL_STORE_INFO_get0_CRL(info));
  422. if (!noout)
  423. PEM_write_bio_X509_CRL(out, OSSL_STORE_INFO_get0_CRL(info));
  424. break;
  425. default:
  426. BIO_printf(bio_err, "!!! Unknown code\n");
  427. ret++;
  428. break;
  429. }
  430. items++;
  431. OSSL_STORE_INFO_free(info);
  432. }
  433. indent_printf(indent, out, "Total found: %d\n", items);
  434. end2:
  435. if (!OSSL_STORE_close(store_ctx)) {
  436. ERR_print_errors(bio_err);
  437. ret++;
  438. }
  439. return ret;
  440. }