2 * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/opensslconf.h>
14 #include <openssl/err.h>
15 #include <openssl/pem.h>
16 #include <openssl/store.h>
17 #include <openssl/x509v3.h> /* s2i_ASN1_INTEGER */
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
, BIO
*out
,
22 const char *prog
, OPENSSL_CTX
*libctx
, const char *propq
);
24 typedef enum OPTION_choice
{
25 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
, OPT_ENGINE
, OPT_OUT
, OPT_PASSIN
,
26 OPT_NOOUT
, OPT_TEXT
, OPT_RECURSIVE
,
27 OPT_SEARCHFOR_CERTS
, OPT_SEARCHFOR_KEYS
, OPT_SEARCHFOR_CRLS
,
28 OPT_CRITERION_SUBJECT
, OPT_CRITERION_ISSUER
, OPT_CRITERION_SERIAL
,
29 OPT_CRITERION_FINGERPRINT
, OPT_CRITERION_ALIAS
,
33 const OPTIONS storeutl_options
[] = {
34 {OPT_HELP_STR
, 1, '-', "Usage: %s [options] uri\n"},
36 OPT_SECTION("General"),
37 {"help", OPT_HELP
, '-', "Display this summary"},
38 {"", OPT_MD
, '-', "Any supported digest"},
39 #ifndef OPENSSL_NO_ENGINE
40 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
43 OPT_SECTION("Search"),
44 {"certs", OPT_SEARCHFOR_CERTS
, '-', "Search for certificates only"},
45 {"keys", OPT_SEARCHFOR_KEYS
, '-', "Search for keys only"},
46 {"crls", OPT_SEARCHFOR_CRLS
, '-', "Search for CRLs only"},
47 {"subject", OPT_CRITERION_SUBJECT
, 's', "Search by subject"},
48 {"issuer", OPT_CRITERION_ISSUER
, 's', "Search by issuer and serial, issuer name"},
49 {"serial", OPT_CRITERION_SERIAL
, 's', "Search by issuer and serial, serial number"},
50 {"fingerprint", OPT_CRITERION_FINGERPRINT
, 's', "Search by public key fingerprint, given in hex"},
51 {"alias", OPT_CRITERION_ALIAS
, 's', "Search by alias"},
52 {"r", OPT_RECURSIVE
, '-', "Recurse through names"},
55 {"passin", OPT_PASSIN
, 's', "Input file pass phrase source"},
57 OPT_SECTION("Output"),
58 {"out", OPT_OUT
, '>', "Output file - default stdout"},
59 {"text", OPT_TEXT
, '-', "Print a text form of the objects"},
60 {"noout", OPT_NOOUT
, '-', "No PEM output, just status"},
65 {"uri", 0, 0, "URI of the store object"},
69 int storeutl_main(int argc
, char *argv
[])
71 int ret
= 1, noout
= 0, text
= 0, recursive
= 0;
72 char *outfile
= NULL
, *passin
= NULL
, *passinarg
= NULL
;
76 char *prog
= opt_init(argc
, argv
, storeutl_options
);
77 PW_CB_DATA pw_cb_data
;
80 X509_NAME
*subject
= NULL
, *issuer
= NULL
;
81 ASN1_INTEGER
*serial
= NULL
;
82 unsigned char *fingerprint
= NULL
;
83 size_t fingerprintlen
= 0;
85 OSSL_STORE_SEARCH
*search
= NULL
;
86 const EVP_MD
*digest
= NULL
;
87 OPENSSL_CTX
*libctx
= app_get0_libctx();
88 const char *propq
= app_get0_propq();
90 while ((o
= opt_next()) != OPT_EOF
) {
95 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
98 opt_help(storeutl_options
);
105 passinarg
= opt_arg();
116 case OPT_SEARCHFOR_CERTS
:
117 case OPT_SEARCHFOR_KEYS
:
118 case OPT_SEARCHFOR_CRLS
:
120 BIO_printf(bio_err
, "%s: only one search type can be given.\n",
125 static const struct {
126 enum OPTION_choice choice
;
129 {OPT_SEARCHFOR_CERTS
, OSSL_STORE_INFO_CERT
},
130 {OPT_SEARCHFOR_KEYS
, OSSL_STORE_INFO_PKEY
},
131 {OPT_SEARCHFOR_CRLS
, OSSL_STORE_INFO_CRL
},
135 for (i
= 0; i
< OSSL_NELEM(map
); i
++) {
136 if (o
== map
[i
].choice
) {
137 expected
= map
[i
].type
;
142 * If expected wasn't set at this point, it means the map
143 * isn't synchronised with the possible options leading here.
145 OPENSSL_assert(expected
!= 0);
148 case OPT_CRITERION_SUBJECT
:
149 if (criterion
!= 0) {
150 BIO_printf(bio_err
, "%s: criterion already given.\n",
154 criterion
= OSSL_STORE_SEARCH_BY_NAME
;
155 if (subject
!= NULL
) {
156 BIO_printf(bio_err
, "%s: subject already given.\n",
160 subject
= parse_name(opt_arg(), MBSTRING_UTF8
, 1, "subject");
164 case OPT_CRITERION_ISSUER
:
166 || (criterion
== OSSL_STORE_SEARCH_BY_ISSUER_SERIAL
167 && issuer
!= NULL
)) {
168 BIO_printf(bio_err
, "%s: criterion already given.\n",
172 criterion
= OSSL_STORE_SEARCH_BY_ISSUER_SERIAL
;
173 if (issuer
!= NULL
) {
174 BIO_printf(bio_err
, "%s: issuer already given.\n",
178 issuer
= parse_name(opt_arg(), MBSTRING_UTF8
, 1, "issuer");
182 case OPT_CRITERION_SERIAL
:
184 || (criterion
== OSSL_STORE_SEARCH_BY_ISSUER_SERIAL
185 && serial
!= NULL
)) {
186 BIO_printf(bio_err
, "%s: criterion already given.\n",
190 criterion
= OSSL_STORE_SEARCH_BY_ISSUER_SERIAL
;
191 if (serial
!= NULL
) {
192 BIO_printf(bio_err
, "%s: serial number already given.\n",
196 if ((serial
= s2i_ASN1_INTEGER(NULL
, opt_arg())) == NULL
) {
197 BIO_printf(bio_err
, "%s: can't parse serial number argument.\n",
202 case OPT_CRITERION_FINGERPRINT
:
204 || (criterion
== OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT
205 && fingerprint
!= NULL
)) {
206 BIO_printf(bio_err
, "%s: criterion already given.\n",
210 criterion
= OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT
;
211 if (fingerprint
!= NULL
) {
212 BIO_printf(bio_err
, "%s: fingerprint already given.\n",
219 if ((fingerprint
= OPENSSL_hexstr2buf(opt_arg(), &tmplen
))
222 "%s: can't parse fingerprint argument.\n",
226 fingerprintlen
= (size_t)tmplen
;
229 case OPT_CRITERION_ALIAS
:
230 if (criterion
!= 0) {
231 BIO_printf(bio_err
, "%s: criterion already given.\n",
235 criterion
= OSSL_STORE_SEARCH_BY_ALIAS
;
237 BIO_printf(bio_err
, "%s: alias already given.\n",
241 if ((alias
= OPENSSL_strdup(opt_arg())) == NULL
) {
242 BIO_printf(bio_err
, "%s: can't parse alias argument.\n",
248 e
= setup_engine(opt_arg(), 0);
251 if (!opt_md(opt_unknown(), &digest
))
254 if (!opt_provider(o
))
259 argc
= opt_num_rest();
263 BIO_printf(bio_err
, "%s: No URI given, nothing to do...\n", prog
);
267 BIO_printf(bio_err
, "%s: Unknown extra parameters after URI\n", prog
);
271 if (criterion
!= 0) {
273 case OSSL_STORE_SEARCH_BY_NAME
:
274 if ((search
= OSSL_STORE_SEARCH_by_name(subject
)) == NULL
) {
275 ERR_print_errors(bio_err
);
279 case OSSL_STORE_SEARCH_BY_ISSUER_SERIAL
:
280 if (issuer
== NULL
|| serial
== NULL
) {
282 "%s: both -issuer and -serial must be given.\n",
286 if ((search
= OSSL_STORE_SEARCH_by_issuer_serial(issuer
, serial
))
288 ERR_print_errors(bio_err
);
292 case OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT
:
293 if ((search
= OSSL_STORE_SEARCH_by_key_fingerprint(digest
,
297 ERR_print_errors(bio_err
);
301 case OSSL_STORE_SEARCH_BY_ALIAS
:
302 if ((search
= OSSL_STORE_SEARCH_by_alias(alias
)) == NULL
) {
303 ERR_print_errors(bio_err
);
310 if (!app_passwd(passinarg
, NULL
, &passin
, NULL
)) {
311 BIO_printf(bio_err
, "Error getting passwords\n");
314 pw_cb_data
.password
= passin
;
315 pw_cb_data
.prompt_info
= argv
[0];
317 out
= bio_open_default(outfile
, 'w', FORMAT_TEXT
);
321 ret
= process(argv
[0], get_ui_method(), &pw_cb_data
,
322 expected
, criterion
, search
,
323 text
, noout
, recursive
, 0, out
, prog
, libctx
, propq
);
326 OPENSSL_free(fingerprint
);
328 ASN1_INTEGER_free(serial
);
329 X509_NAME_free(subject
);
330 X509_NAME_free(issuer
);
331 OSSL_STORE_SEARCH_free(search
);
333 OPENSSL_free(passin
);
338 static int indent_printf(int indent
, BIO
*bio
, const char *format
, ...)
343 va_start(args
, format
);
345 ret
= BIO_printf(bio
, "%*s", indent
, "") + BIO_vprintf(bio
, format
, args
);
351 static int process(const char *uri
, const UI_METHOD
*uimeth
, PW_CB_DATA
*uidata
,
352 int expected
, int criterion
, OSSL_STORE_SEARCH
*search
,
353 int text
, int noout
, int recursive
, int indent
, BIO
*out
,
354 const char *prog
, OPENSSL_CTX
*libctx
, const char *propq
)
356 OSSL_STORE_CTX
*store_ctx
= NULL
;
357 int ret
= 1, items
= 0;
359 if ((store_ctx
= OSSL_STORE_open_with_libctx(uri
, libctx
, propq
,
360 uimeth
, uidata
, NULL
, NULL
))
362 BIO_printf(bio_err
, "Couldn't open file or uri %s\n", uri
);
363 ERR_print_errors(bio_err
);
368 if (!OSSL_STORE_expect(store_ctx
, expected
)) {
369 ERR_print_errors(bio_err
);
374 if (criterion
!= 0) {
375 if (!OSSL_STORE_supports_search(store_ctx
, criterion
)) {
377 "%s: the store scheme doesn't support the given search criteria.\n",
382 if (!OSSL_STORE_find(store_ctx
, search
)) {
383 ERR_print_errors(bio_err
);
388 /* From here on, we count errors, and we'll return the count at the end */
392 OSSL_STORE_INFO
*info
= OSSL_STORE_load(store_ctx
);
393 int type
= info
== NULL
? 0 : OSSL_STORE_INFO_get_type(info
);
394 const char *infostr
=
395 info
== NULL
? NULL
: OSSL_STORE_INFO_type_string(type
);
398 if (OSSL_STORE_eof(store_ctx
))
401 if (OSSL_STORE_error(store_ctx
)) {
405 ERR_print_errors(bio_err
);
411 "ERROR: OSSL_STORE_load() returned NULL without "
412 "eof or error indications\n");
413 BIO_printf(bio_err
, " This is an error in the loader\n");
414 ERR_print_errors(bio_err
);
419 if (type
== OSSL_STORE_INFO_NAME
) {
420 const char *name
= OSSL_STORE_INFO_get0_NAME(info
);
421 const char *desc
= OSSL_STORE_INFO_get0_NAME_description(info
);
422 indent_printf(indent
, bio_out
, "%d: %s: %s\n", items
, infostr
,
425 indent_printf(indent
, bio_out
, "%s\n", desc
);
427 indent_printf(indent
, bio_out
, "%d: %s\n", items
, infostr
);
431 * Unfortunately, PEM_X509_INFO_write_bio() is sorely lacking in
432 * functionality, so we must figure out how exactly to write things
436 case OSSL_STORE_INFO_NAME
:
438 const char *suburi
= OSSL_STORE_INFO_get0_NAME(info
);
439 ret
+= process(suburi
, uimeth
, uidata
,
440 expected
, criterion
, search
,
441 text
, noout
, recursive
, indent
+ 2, out
, prog
,
445 case OSSL_STORE_INFO_PARAMS
:
447 EVP_PKEY_print_params(out
, OSSL_STORE_INFO_get0_PARAMS(info
),
450 PEM_write_bio_Parameters(out
,
451 OSSL_STORE_INFO_get0_PARAMS(info
));
453 case OSSL_STORE_INFO_PKEY
:
455 EVP_PKEY_print_private(out
, OSSL_STORE_INFO_get0_PKEY(info
),
458 PEM_write_bio_PrivateKey(out
, OSSL_STORE_INFO_get0_PKEY(info
),
459 NULL
, NULL
, 0, NULL
, NULL
);
461 case OSSL_STORE_INFO_CERT
:
463 X509_print(out
, OSSL_STORE_INFO_get0_CERT(info
));
465 PEM_write_bio_X509(out
, OSSL_STORE_INFO_get0_CERT(info
));
467 case OSSL_STORE_INFO_CRL
:
469 X509_CRL_print(out
, OSSL_STORE_INFO_get0_CRL(info
));
471 PEM_write_bio_X509_CRL(out
, OSSL_STORE_INFO_get0_CRL(info
));
474 BIO_printf(bio_err
, "!!! Unknown code\n");
479 OSSL_STORE_INFO_free(info
);
481 indent_printf(indent
, out
, "Total found: %d\n", items
);
484 if (!OSSL_STORE_close(store_ctx
)) {
485 ERR_print_errors(bio_err
);