storeutl.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * Copyright 2016-2025 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. BIO_printf(bio_err, "%s: criterion already given.\n",
  191. prog);
  192. goto end;
  193. }
  194. criterion = OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT;
  195. if (fingerprint != NULL) {
  196. BIO_printf(bio_err, "%s: fingerprint already given.\n",
  197. prog);
  198. goto end;
  199. }
  200. {
  201. long tmplen = 0;
  202. if ((fingerprint = OPENSSL_hexstr2buf(opt_arg(), &tmplen))
  203. == NULL) {
  204. BIO_printf(bio_err,
  205. "%s: can't parse fingerprint argument.\n",
  206. prog);
  207. goto end;
  208. }
  209. fingerprintlen = (size_t)tmplen;
  210. }
  211. break;
  212. case OPT_CRITERION_ALIAS:
  213. if (criterion != 0) {
  214. BIO_printf(bio_err, "%s: criterion already given.\n",
  215. prog);
  216. goto end;
  217. }
  218. criterion = OSSL_STORE_SEARCH_BY_ALIAS;
  219. if (alias != NULL) {
  220. BIO_printf(bio_err, "%s: alias already given.\n",
  221. prog);
  222. goto end;
  223. }
  224. if ((alias = OPENSSL_strdup(opt_arg())) == NULL) {
  225. BIO_printf(bio_err, "%s: can't parse alias argument.\n",
  226. prog);
  227. goto end;
  228. }
  229. break;
  230. case OPT_ENGINE:
  231. e = setup_engine(opt_arg(), 0);
  232. break;
  233. case OPT_MD:
  234. digestname = opt_unknown();
  235. break;
  236. case OPT_PROV_CASES:
  237. if (!opt_provider(o))
  238. goto end;
  239. break;
  240. }
  241. }
  242. /* One argument, the URI */
  243. if (!opt_check_rest_arg("URI"))
  244. goto opthelp;
  245. argv = opt_rest();
  246. if (!opt_md(digestname, &digest))
  247. goto opthelp;
  248. if (criterion != 0) {
  249. switch (criterion) {
  250. case OSSL_STORE_SEARCH_BY_NAME:
  251. if ((search = OSSL_STORE_SEARCH_by_name(subject)) == NULL) {
  252. ERR_print_errors(bio_err);
  253. goto end;
  254. }
  255. break;
  256. case OSSL_STORE_SEARCH_BY_ISSUER_SERIAL:
  257. if (issuer == NULL || serial == NULL) {
  258. BIO_printf(bio_err,
  259. "%s: both -issuer and -serial must be given.\n",
  260. prog);
  261. goto end;
  262. }
  263. if ((search = OSSL_STORE_SEARCH_by_issuer_serial(issuer, serial))
  264. == NULL) {
  265. ERR_print_errors(bio_err);
  266. goto end;
  267. }
  268. break;
  269. case OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT:
  270. if ((search = OSSL_STORE_SEARCH_by_key_fingerprint(digest,
  271. fingerprint,
  272. fingerprintlen))
  273. == NULL) {
  274. ERR_print_errors(bio_err);
  275. goto end;
  276. }
  277. break;
  278. case OSSL_STORE_SEARCH_BY_ALIAS:
  279. if ((search = OSSL_STORE_SEARCH_by_alias(alias)) == NULL) {
  280. ERR_print_errors(bio_err);
  281. goto end;
  282. }
  283. break;
  284. }
  285. }
  286. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  287. BIO_printf(bio_err, "Error getting passwords\n");
  288. goto end;
  289. }
  290. pw_cb_data.password = passin;
  291. pw_cb_data.prompt_info = argv[0];
  292. out = bio_open_default(outfile, 'w', FORMAT_TEXT);
  293. if (out == NULL)
  294. goto end;
  295. ret = process(argv[0], get_ui_method(), &pw_cb_data,
  296. expected, criterion, search,
  297. text, noout, recursive, 0, out, prog, libctx);
  298. end:
  299. EVP_MD_free(digest);
  300. OPENSSL_free(fingerprint);
  301. OPENSSL_free(alias);
  302. ASN1_INTEGER_free(serial);
  303. X509_NAME_free(subject);
  304. X509_NAME_free(issuer);
  305. OSSL_STORE_SEARCH_free(search);
  306. BIO_free_all(out);
  307. OPENSSL_free(passin);
  308. release_engine(e);
  309. return ret;
  310. }
  311. static int indent_printf(int indent, BIO *bio, const char *format, ...)
  312. {
  313. va_list args;
  314. int ret, vret;
  315. ret = BIO_printf(bio, "%*s", indent, "");
  316. if (ret < 0)
  317. return ret;
  318. va_start(args, format);
  319. vret = BIO_vprintf(bio, format, args);
  320. va_end(args);
  321. if (vret < 0)
  322. return vret;
  323. if (vret > INT_MAX - ret)
  324. return INT_MAX;
  325. return ret + vret;
  326. }
  327. static int process(const char *uri, const UI_METHOD *uimeth, PW_CB_DATA *uidata,
  328. int expected, int criterion, OSSL_STORE_SEARCH *search,
  329. int text, int noout, int recursive, int indent, BIO *out,
  330. const char *prog, OSSL_LIB_CTX *libctx)
  331. {
  332. OSSL_STORE_CTX *store_ctx = NULL;
  333. int ret = 1, items = 0;
  334. if ((store_ctx = OSSL_STORE_open_ex(uri, libctx, app_get0_propq(), uimeth, uidata,
  335. NULL, NULL, NULL))
  336. == NULL) {
  337. BIO_printf(bio_err, "Couldn't open file or uri %s\n", uri);
  338. ERR_print_errors(bio_err);
  339. return ret;
  340. }
  341. if (expected != 0) {
  342. if (!OSSL_STORE_expect(store_ctx, expected)) {
  343. ERR_print_errors(bio_err);
  344. goto end2;
  345. }
  346. }
  347. if (criterion != 0) {
  348. if (!OSSL_STORE_supports_search(store_ctx, criterion)) {
  349. BIO_printf(bio_err,
  350. "%s: the store scheme doesn't support the given search criteria.\n",
  351. prog);
  352. goto end2;
  353. }
  354. if (!OSSL_STORE_find(store_ctx, search)) {
  355. ERR_print_errors(bio_err);
  356. goto end2;
  357. }
  358. }
  359. /* From here on, we count errors, and we'll return the count at the end */
  360. ret = 0;
  361. for (;;) {
  362. OSSL_STORE_INFO *info = OSSL_STORE_load(store_ctx);
  363. int type = info == NULL ? 0 : OSSL_STORE_INFO_get_type(info);
  364. const char *infostr =
  365. info == NULL ? NULL : OSSL_STORE_INFO_type_string(type);
  366. if (info == NULL) {
  367. if (OSSL_STORE_error(store_ctx)) {
  368. if (recursive)
  369. ERR_clear_error();
  370. else
  371. ERR_print_errors(bio_err);
  372. if (OSSL_STORE_eof(store_ctx))
  373. break;
  374. ret++;
  375. continue;
  376. }
  377. if (OSSL_STORE_eof(store_ctx))
  378. break;
  379. BIO_printf(bio_err,
  380. "ERROR: OSSL_STORE_load() returned NULL without "
  381. "eof or error indications\n");
  382. BIO_printf(bio_err, " This is an error in the loader\n");
  383. ERR_print_errors(bio_err);
  384. ret++;
  385. break;
  386. }
  387. if (type == OSSL_STORE_INFO_NAME) {
  388. const char *name = OSSL_STORE_INFO_get0_NAME(info);
  389. const char *desc = OSSL_STORE_INFO_get0_NAME_description(info);
  390. indent_printf(indent, bio_out, "%d: %s: %s\n", items, infostr,
  391. name);
  392. if (desc != NULL)
  393. indent_printf(indent, bio_out, "%s\n", desc);
  394. } else {
  395. indent_printf(indent, bio_out, "%d: %s\n", items, infostr);
  396. }
  397. /*
  398. * Unfortunately, PEM_X509_INFO_write_bio() is sorely lacking in
  399. * functionality, so we must figure out how exactly to write things
  400. * ourselves...
  401. */
  402. switch (type) {
  403. case OSSL_STORE_INFO_NAME:
  404. if (recursive) {
  405. const char *suburi = OSSL_STORE_INFO_get0_NAME(info);
  406. ret += process(suburi, uimeth, uidata,
  407. expected, criterion, search,
  408. text, noout, recursive, indent + 2, out, prog,
  409. libctx);
  410. }
  411. break;
  412. case OSSL_STORE_INFO_PARAMS:
  413. if (text)
  414. EVP_PKEY_print_params(out, OSSL_STORE_INFO_get0_PARAMS(info),
  415. 0, NULL);
  416. if (!noout)
  417. PEM_write_bio_Parameters(out,
  418. OSSL_STORE_INFO_get0_PARAMS(info));
  419. break;
  420. case OSSL_STORE_INFO_PUBKEY:
  421. if (text)
  422. EVP_PKEY_print_public(out, OSSL_STORE_INFO_get0_PUBKEY(info),
  423. 0, NULL);
  424. if (!noout)
  425. PEM_write_bio_PUBKEY(out, OSSL_STORE_INFO_get0_PUBKEY(info));
  426. break;
  427. case OSSL_STORE_INFO_PKEY:
  428. if (text)
  429. EVP_PKEY_print_private(out, OSSL_STORE_INFO_get0_PKEY(info),
  430. 0, NULL);
  431. if (!noout)
  432. PEM_write_bio_PrivateKey(out, OSSL_STORE_INFO_get0_PKEY(info),
  433. NULL, NULL, 0, NULL, NULL);
  434. break;
  435. case OSSL_STORE_INFO_CERT:
  436. if (text)
  437. X509_print(out, OSSL_STORE_INFO_get0_CERT(info));
  438. if (!noout)
  439. PEM_write_bio_X509(out, OSSL_STORE_INFO_get0_CERT(info));
  440. break;
  441. case OSSL_STORE_INFO_CRL:
  442. if (text)
  443. X509_CRL_print(out, OSSL_STORE_INFO_get0_CRL(info));
  444. if (!noout)
  445. PEM_write_bio_X509_CRL(out, OSSL_STORE_INFO_get0_CRL(info));
  446. break;
  447. default:
  448. BIO_printf(bio_err, "!!! Unknown code\n");
  449. ret++;
  450. break;
  451. }
  452. items++;
  453. OSSL_STORE_INFO_free(info);
  454. }
  455. indent_printf(indent, out, "Total found: %d\n", items);
  456. end2:
  457. if (!OSSL_STORE_close(store_ctx)) {
  458. ERR_print_errors(bio_err);
  459. ret++;
  460. }
  461. return ret;
  462. }