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