storeutl.c 17 KB

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