2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
10 #include <openssl/opensslconf.h>
14 #include <openssl/err.h>
15 #include <openssl/pem.h>
16 #include <openssl/store.h>
18 static int process(const char *uri, const UI_METHOD *uimeth, PW_CB_DATA *uidata,
19 int expected, int criterion, OSSL_STORE_SEARCH *search,
20 int text, int noout, int recursive, int indent, BIO *out,
23 typedef enum OPTION_choice {
24 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, OPT_ENGINE, OPT_OUT, OPT_PASSIN,
25 OPT_NOOUT, OPT_TEXT, OPT_RECURSIVE,
26 OPT_SEARCHFOR_CERTS, OPT_SEARCHFOR_KEYS, OPT_SEARCHFOR_CRLS,
27 OPT_CRITERION_SUBJECT, OPT_CRITERION_ISSUER, OPT_CRITERION_SERIAL,
28 OPT_CRITERION_FINGERPRINT, OPT_CRITERION_ALIAS,
32 const OPTIONS storeutl_options[] = {
33 {OPT_HELP_STR, 1, '-', "Usage: %s [options] uri\nValid options are:\n"},
34 {"help", OPT_HELP, '-', "Display this summary"},
35 {"out", OPT_OUT, '>', "Output file - default stdout"},
36 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
37 {"text", OPT_TEXT, '-', "Print a text form of the objects"},
38 {"noout", OPT_NOOUT, '-', "No PEM output, just status"},
39 {"certs", OPT_SEARCHFOR_CERTS, '-', "Search for certificates only"},
40 {"keys", OPT_SEARCHFOR_KEYS, '-', "Search for keys only"},
41 {"crls", OPT_SEARCHFOR_CRLS, '-', "Search for CRLs only"},
42 {"subject", OPT_CRITERION_SUBJECT, 's', "Search by subject"},
43 {"issuer", OPT_CRITERION_ISSUER, 's', "Search by issuer and serial, issuer name"},
44 {"serial", OPT_CRITERION_SERIAL, 's', "Search by issuer and serial, serial number"},
45 {"fingerprint", OPT_CRITERION_FINGERPRINT, 's', "Search by public key fingerprint, given in hex"},
46 {"alias", OPT_CRITERION_ALIAS, 's', "Search by alias"},
47 {"", OPT_MD, '-', "Any supported digest"},
48 #ifndef OPENSSL_NO_ENGINE
49 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
51 {"r", OPT_RECURSIVE, '-', "Recurse through names"},
55 int storeutl_main(int argc, char *argv[])
57 int ret = 1, noout = 0, text = 0, recursive = 0;
58 char *outfile = NULL, *passin = NULL, *passinarg = NULL;
62 char *prog = opt_init(argc, argv, storeutl_options);
63 PW_CB_DATA pw_cb_data;
66 X509_NAME *subject = NULL, *issuer = NULL;
67 ASN1_INTEGER *serial = NULL;
68 unsigned char *fingerprint = NULL;
69 size_t fingerprintlen = 0;
71 OSSL_STORE_SEARCH *search = NULL;
72 const EVP_MD *digest = NULL;
74 while ((o = opt_next()) != OPT_EOF) {
79 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
82 opt_help(storeutl_options);
89 passinarg = opt_arg();
100 case OPT_SEARCHFOR_CERTS:
101 case OPT_SEARCHFOR_KEYS:
102 case OPT_SEARCHFOR_CRLS:
104 BIO_printf(bio_err, "%s: only one search type can be given.\n",
109 static const struct {
110 enum OPTION_choice choice;
113 {OPT_SEARCHFOR_CERTS, OSSL_STORE_INFO_CERT},
114 {OPT_SEARCHFOR_KEYS, OSSL_STORE_INFO_PKEY},
115 {OPT_SEARCHFOR_CRLS, OSSL_STORE_INFO_CRL},
119 for (i = 0; i < OSSL_NELEM(map); i++) {
120 if (o == map[i].choice) {
121 expected = map[i].type;
126 * If expected wasn't set at this point, it means the map
127 * isn't syncronised with the possible options leading here.
129 OPENSSL_assert(expected != 0);
132 case OPT_CRITERION_SUBJECT:
133 if (criterion != 0) {
134 BIO_printf(bio_err, "%s: criterion already given.\n",
138 criterion = OSSL_STORE_SEARCH_BY_NAME;
139 if (subject != NULL) {
140 BIO_printf(bio_err, "%s: subject already given.\n",
144 if ((subject = parse_name(opt_arg(), MBSTRING_UTF8, 1)) == NULL) {
145 BIO_printf(bio_err, "%s: can't parse subject argument.\n",
150 case OPT_CRITERION_ISSUER:
152 || (criterion == OSSL_STORE_SEARCH_BY_ISSUER_SERIAL
153 && issuer != NULL)) {
154 BIO_printf(bio_err, "%s: criterion already given.\n",
158 criterion = OSSL_STORE_SEARCH_BY_ISSUER_SERIAL;
159 if (issuer != NULL) {
160 BIO_printf(bio_err, "%s: issuer already given.\n",
164 if ((issuer = parse_name(opt_arg(), MBSTRING_UTF8, 1)) == NULL) {
165 BIO_printf(bio_err, "%s: can't parse issuer argument.\n",
170 case OPT_CRITERION_SERIAL:
172 || (criterion == OSSL_STORE_SEARCH_BY_ISSUER_SERIAL
173 && serial != NULL)) {
174 BIO_printf(bio_err, "%s: criterion already given.\n",
178 criterion = OSSL_STORE_SEARCH_BY_ISSUER_SERIAL;
179 if (serial != NULL) {
180 BIO_printf(bio_err, "%s: serial number already given.\n",
184 if ((serial = s2i_ASN1_INTEGER(NULL, opt_arg())) == NULL) {
185 BIO_printf(bio_err, "%s: can't parse serial number argument.\n",
190 case OPT_CRITERION_FINGERPRINT:
192 || (criterion == OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT
193 && fingerprint != NULL)) {
194 BIO_printf(bio_err, "%s: criterion already given.\n",
198 criterion = OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT;
199 if (fingerprint != NULL) {
200 BIO_printf(bio_err, "%s: fingerprint already given.\n",
207 if ((fingerprint = OPENSSL_hexstr2buf(opt_arg(), &tmplen))
210 "%s: can't parse fingerprint argument.\n",
214 fingerprintlen = (size_t)tmplen;
217 case OPT_CRITERION_ALIAS:
218 if (criterion != 0) {
219 BIO_printf(bio_err, "%s: criterion already given.\n",
223 criterion = OSSL_STORE_SEARCH_BY_ALIAS;
225 BIO_printf(bio_err, "%s: alias already given.\n",
229 if ((alias = OPENSSL_strdup(opt_arg())) == NULL) {
230 BIO_printf(bio_err, "%s: can't parse alias argument.\n",
236 e = setup_engine(opt_arg(), 0);
239 if (!opt_md(opt_unknown(), &digest))
243 argc = opt_num_rest();
247 BIO_printf(bio_err, "%s: No URI given, nothing to do...\n", prog);
251 BIO_printf(bio_err, "%s: Unknown extra parameters after URI\n", prog);
255 if (criterion != 0) {
257 case OSSL_STORE_SEARCH_BY_NAME:
258 if ((search = OSSL_STORE_SEARCH_by_name(subject)) == NULL) {
259 ERR_print_errors(bio_err);
263 case OSSL_STORE_SEARCH_BY_ISSUER_SERIAL:
264 if (issuer == NULL || serial == NULL) {
266 "%s: both -issuer and -serial must be given.\n",
270 if ((search = OSSL_STORE_SEARCH_by_issuer_serial(issuer, serial))
272 ERR_print_errors(bio_err);
276 case OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT:
277 if ((search = OSSL_STORE_SEARCH_by_key_fingerprint(digest,
281 ERR_print_errors(bio_err);
285 case OSSL_STORE_SEARCH_BY_ALIAS:
286 if ((search = OSSL_STORE_SEARCH_by_alias(alias)) == NULL) {
287 ERR_print_errors(bio_err);
294 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
295 BIO_printf(bio_err, "Error getting passwords\n");
298 pw_cb_data.password = passin;
299 pw_cb_data.prompt_info = argv[0];
301 out = bio_open_default(outfile, 'w', FORMAT_TEXT);
305 ret = process(argv[0], get_ui_method(), &pw_cb_data,
306 expected, criterion, search,
307 text, noout, recursive, 0, out, prog);
311 ASN1_INTEGER_free(serial);
312 X509_NAME_free(subject);
313 X509_NAME_free(issuer);
314 OSSL_STORE_SEARCH_free(search);
316 OPENSSL_free(passin);
321 static int indent_printf(int indent, BIO *bio, const char *format, ...)
326 va_start(args, format);
328 ret = BIO_printf(bio, "%*s", indent, "") + BIO_vprintf(bio, format, args);
334 static int process(const char *uri, const UI_METHOD *uimeth, PW_CB_DATA *uidata,
335 int expected, int criterion, OSSL_STORE_SEARCH *search,
336 int text, int noout, int recursive, int indent, BIO *out,
339 OSSL_STORE_CTX *store_ctx = NULL;
340 int ret = 1, items = 0;
342 if ((store_ctx = OSSL_STORE_open(uri, uimeth, uidata, NULL, NULL))
344 BIO_printf(bio_err, "Couldn't open file or uri %s\n", uri);
345 ERR_print_errors(bio_err);
350 if (!OSSL_STORE_expect(store_ctx, expected)) {
351 ERR_print_errors(bio_err);
356 if (criterion != 0) {
357 if (!OSSL_STORE_supports_search(store_ctx, criterion)) {
359 "%s: the store scheme doesn't support the given search criteria.\n",
364 if (!OSSL_STORE_find(store_ctx, search)) {
365 ERR_print_errors(bio_err);
370 /* From here on, we count errors, and we'll return the count at the end */
374 OSSL_STORE_INFO *info = OSSL_STORE_load(store_ctx);
375 int type = info == NULL ? 0 : OSSL_STORE_INFO_get_type(info);
376 const char *infostr =
377 info == NULL ? NULL : OSSL_STORE_INFO_type_string(type);
380 if (OSSL_STORE_eof(store_ctx))
383 if (OSSL_STORE_error(store_ctx)) {
387 ERR_print_errors(bio_err);
393 "ERROR: OSSL_STORE_load() returned NULL without "
394 "eof or error indications\n");
395 BIO_printf(bio_err, " This is an error in the loader\n");
396 ERR_print_errors(bio_err);
401 if (type == OSSL_STORE_INFO_NAME) {
402 const char *name = OSSL_STORE_INFO_get0_NAME(info);
403 const char *desc = OSSL_STORE_INFO_get0_NAME_description(info);
404 indent_printf(indent, bio_out, "%d: %s: %s\n", items, infostr,
407 indent_printf(indent, bio_out, "%s\n", desc);
409 indent_printf(indent, bio_out, "%d: %s\n", items, infostr);
413 * Unfortunately, PEM_X509_INFO_write_bio() is sorely lacking in
414 * functionality, so we must figure out how exactly to write things
418 case OSSL_STORE_INFO_NAME:
420 const char *suburi = OSSL_STORE_INFO_get0_NAME(info);
421 ret += process(suburi, uimeth, uidata,
422 expected, criterion, search,
423 text, noout, recursive, indent + 2, out, prog);
426 case OSSL_STORE_INFO_PARAMS:
428 EVP_PKEY_print_params(out, OSSL_STORE_INFO_get0_PARAMS(info),
431 PEM_write_bio_Parameters(out,
432 OSSL_STORE_INFO_get0_PARAMS(info));
434 case OSSL_STORE_INFO_PKEY:
436 EVP_PKEY_print_private(out, OSSL_STORE_INFO_get0_PKEY(info),
439 PEM_write_bio_PrivateKey(out, OSSL_STORE_INFO_get0_PKEY(info),
440 NULL, NULL, 0, NULL, NULL);
442 case OSSL_STORE_INFO_CERT:
444 X509_print(out, OSSL_STORE_INFO_get0_CERT(info));
446 PEM_write_bio_X509(out, OSSL_STORE_INFO_get0_CERT(info));
448 case OSSL_STORE_INFO_CRL:
450 X509_CRL_print(out, OSSL_STORE_INFO_get0_CRL(info));
452 PEM_write_bio_X509_CRL(out, OSSL_STORE_INFO_get0_CRL(info));
455 BIO_printf(bio_err, "!!! Unknown code\n");
460 OSSL_STORE_INFO_free(info);
462 indent_printf(indent, out, "Total found: %d\n", items);
465 if (!OSSL_STORE_close(store_ctx)) {
466 ERR_print_errors(bio_err);