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