storeutl.c 17 KB

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