Fetch and free cipher and md's
[openssl.git] / apps / storeutl.c
1 /*
2  * Copyright 2016-2021 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, BIO *out,
22                    const char *prog, OSSL_LIB_CTX *libctx);
23
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,
30     OPT_MD, OPT_PROV_ENUM
31 } OPTION_CHOICE;
32
33 const OPTIONS storeutl_options[] = {
34     {OPT_HELP_STR, 1, '-', "Usage: %s [options] uri\n"},
35
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"},
41 #endif
42
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"},
53
54     OPT_SECTION("Input"),
55     {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
56
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"},
61
62     OPT_PROV_OPTIONS,
63
64     OPT_PARAMETERS(),
65     {"uri", 0, 0, "URI of the store object"},
66     {NULL}
67 };
68
69 int storeutl_main(int argc, char *argv[])
70 {
71     int ret = 1, noout = 0, text = 0, recursive = 0;
72     char *outfile = NULL, *passin = NULL, *passinarg = NULL;
73     BIO *out = NULL;
74     ENGINE *e = NULL;
75     OPTION_CHOICE o;
76     char *prog = opt_init(argc, argv, storeutl_options);
77     PW_CB_DATA pw_cb_data;
78     int expected = 0;
79     int criterion = 0;
80     X509_NAME *subject = NULL, *issuer = NULL;
81     ASN1_INTEGER *serial = NULL;
82     unsigned char *fingerprint = NULL;
83     size_t fingerprintlen = 0;
84     char *alias = NULL, *digestname = NULL;
85     OSSL_STORE_SEARCH *search = NULL;
86     EVP_MD *digest = NULL;
87     OSSL_LIB_CTX *libctx = app_get0_libctx();
88
89     while ((o = opt_next()) != OPT_EOF) {
90         switch (o) {
91         case OPT_EOF:
92         case OPT_ERR:
93  opthelp:
94             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
95             goto end;
96         case OPT_HELP:
97             opt_help(storeutl_options);
98             ret = 0;
99             goto end;
100         case OPT_OUT:
101             outfile = opt_arg();
102             break;
103         case OPT_PASSIN:
104             passinarg = opt_arg();
105             break;
106         case OPT_NOOUT:
107             noout = 1;
108             break;
109         case OPT_TEXT:
110             text = 1;
111             break;
112         case OPT_RECURSIVE:
113             recursive = 1;
114             break;
115         case OPT_SEARCHFOR_CERTS:
116         case OPT_SEARCHFOR_KEYS:
117         case OPT_SEARCHFOR_CRLS:
118             if (expected != 0) {
119                 BIO_printf(bio_err, "%s: only one search type can be given.\n",
120                            prog);
121                 goto end;
122             }
123             {
124                 static const struct {
125                     enum OPTION_choice choice;
126                     int type;
127                 } map[] = {
128                     {OPT_SEARCHFOR_CERTS, OSSL_STORE_INFO_CERT},
129                     {OPT_SEARCHFOR_KEYS, OSSL_STORE_INFO_PKEY},
130                     {OPT_SEARCHFOR_CRLS, OSSL_STORE_INFO_CRL},
131                 };
132                 size_t i;
133
134                 for (i = 0; i < OSSL_NELEM(map); i++) {
135                     if (o == map[i].choice) {
136                         expected = map[i].type;
137                         break;
138                     }
139                 }
140                 /*
141                  * If expected wasn't set at this point, it means the map
142                  * isn't synchronised with the possible options leading here.
143                  */
144                 OPENSSL_assert(expected != 0);
145             }
146             break;
147         case OPT_CRITERION_SUBJECT:
148             if (criterion != 0) {
149                 BIO_printf(bio_err, "%s: criterion already given.\n",
150                            prog);
151                 goto end;
152             }
153             criterion = OSSL_STORE_SEARCH_BY_NAME;
154             if (subject != NULL) {
155                 BIO_printf(bio_err, "%s: subject already given.\n",
156                            prog);
157                 goto end;
158             }
159             subject = parse_name(opt_arg(), MBSTRING_UTF8, 1, "subject");
160             if (subject == NULL)
161                 goto end;
162             break;
163         case OPT_CRITERION_ISSUER:
164             if (criterion != 0
165                 || (criterion == OSSL_STORE_SEARCH_BY_ISSUER_SERIAL
166                     && issuer != NULL)) {
167                 BIO_printf(bio_err, "%s: criterion already given.\n",
168                            prog);
169                 goto end;
170             }
171             criterion = OSSL_STORE_SEARCH_BY_ISSUER_SERIAL;
172             if (issuer != NULL) {
173                 BIO_printf(bio_err, "%s: issuer already given.\n",
174                            prog);
175                 goto end;
176             }
177             issuer = parse_name(opt_arg(), MBSTRING_UTF8, 1, "issuer");
178             if (issuer == NULL)
179                 goto end;
180             break;
181         case OPT_CRITERION_SERIAL:
182             if (criterion != 0
183                 || (criterion == OSSL_STORE_SEARCH_BY_ISSUER_SERIAL
184                     && serial != NULL)) {
185                 BIO_printf(bio_err, "%s: criterion already given.\n",
186                            prog);
187                 goto end;
188             }
189             criterion = OSSL_STORE_SEARCH_BY_ISSUER_SERIAL;
190             if (serial != NULL) {
191                 BIO_printf(bio_err, "%s: serial number already given.\n",
192                            prog);
193                 goto end;
194             }
195             if ((serial = s2i_ASN1_INTEGER(NULL, opt_arg())) == NULL) {
196                 BIO_printf(bio_err, "%s: can't parse serial number argument.\n",
197                            prog);
198                 goto end;
199             }
200             break;
201         case OPT_CRITERION_FINGERPRINT:
202             if (criterion != 0
203                 || (criterion == OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT
204                     && fingerprint != NULL)) {
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     argc = opt_num_rest();
261     argv = opt_rest();
262     if (argc != 1)
263         goto opthelp;
264
265     if (digestname != NULL) {
266         if (!opt_md(digestname, &digest))
267             goto opthelp;
268     }
269
270     if (criterion != 0) {
271         switch (criterion) {
272         case OSSL_STORE_SEARCH_BY_NAME:
273             if ((search = OSSL_STORE_SEARCH_by_name(subject)) == NULL) {
274                 ERR_print_errors(bio_err);
275                 goto end;
276             }
277             break;
278         case OSSL_STORE_SEARCH_BY_ISSUER_SERIAL:
279             if (issuer == NULL || serial == NULL) {
280                 BIO_printf(bio_err,
281                            "%s: both -issuer and -serial must be given.\n",
282                            prog);
283                 goto end;
284             }
285             if ((search = OSSL_STORE_SEARCH_by_issuer_serial(issuer, serial))
286                 == NULL) {
287                 ERR_print_errors(bio_err);
288                 goto end;
289             }
290             break;
291         case OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT:
292             if ((search = OSSL_STORE_SEARCH_by_key_fingerprint(digest,
293                                                                fingerprint,
294                                                                fingerprintlen))
295                 == NULL) {
296                 ERR_print_errors(bio_err);
297                 goto end;
298             }
299             break;
300         case OSSL_STORE_SEARCH_BY_ALIAS:
301             if ((search = OSSL_STORE_SEARCH_by_alias(alias)) == NULL) {
302                 ERR_print_errors(bio_err);
303                 goto end;
304             }
305             break;
306         }
307     }
308
309     if (!app_passwd(passinarg, NULL, &passin, NULL)) {
310         BIO_printf(bio_err, "Error getting passwords\n");
311         goto end;
312     }
313     pw_cb_data.password = passin;
314     pw_cb_data.prompt_info = argv[0];
315
316     out = bio_open_default(outfile, 'w', FORMAT_TEXT);
317     if (out == NULL)
318         goto end;
319
320     ret = process(argv[0], get_ui_method(), &pw_cb_data,
321                   expected, criterion, search,
322                   text, noout, recursive, 0, out, prog, libctx);
323
324  end:
325     EVP_MD_free(digest);
326     OPENSSL_free(fingerprint);
327     OPENSSL_free(alias);
328     ASN1_INTEGER_free(serial);
329     X509_NAME_free(subject);
330     X509_NAME_free(issuer);
331     OSSL_STORE_SEARCH_free(search);
332     BIO_free_all(out);
333     OPENSSL_free(passin);
334     release_engine(e);
335     return ret;
336 }
337
338 static int indent_printf(int indent, BIO *bio, const char *format, ...)
339 {
340     va_list args;
341     int ret;
342
343     va_start(args, format);
344
345     ret = BIO_printf(bio, "%*s", indent, "") + BIO_vprintf(bio, format, args);
346
347     va_end(args);
348     return ret;
349 }
350
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, OSSL_LIB_CTX *libctx)
355 {
356     OSSL_STORE_CTX *store_ctx = NULL;
357     int ret = 1, items = 0;
358
359     if ((store_ctx = OSSL_STORE_open_ex(uri, libctx, app_get0_propq(), uimeth, uidata,
360                                         NULL, NULL))
361         == NULL) {
362         BIO_printf(bio_err, "Couldn't open file or uri %s\n", uri);
363         ERR_print_errors(bio_err);
364         return ret;
365     }
366
367     if (expected != 0) {
368         if (!OSSL_STORE_expect(store_ctx, expected)) {
369             ERR_print_errors(bio_err);
370             goto end2;
371         }
372     }
373
374     if (criterion != 0) {
375         if (!OSSL_STORE_supports_search(store_ctx, criterion)) {
376             BIO_printf(bio_err,
377                        "%s: the store scheme doesn't support the given search criteria.\n",
378                        prog);
379             goto end2;
380         }
381
382         if (!OSSL_STORE_find(store_ctx, search)) {
383             ERR_print_errors(bio_err);
384             goto end2;
385         }
386     }
387
388     /* From here on, we count errors, and we'll return the count at the end */
389     ret = 0;
390
391     for (;;) {
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);
396
397         if (info == NULL) {
398             if (OSSL_STORE_error(store_ctx)) {
399                 if (recursive)
400                     ERR_clear_error();
401                 else
402                     ERR_print_errors(bio_err);
403                 if (OSSL_STORE_eof(store_ctx))
404                     break;
405                 ret++;
406                 continue;
407             }
408
409             if (OSSL_STORE_eof(store_ctx))
410                 break;
411
412             BIO_printf(bio_err,
413                        "ERROR: OSSL_STORE_load() returned NULL without "
414                        "eof or error indications\n");
415             BIO_printf(bio_err, "       This is an error in the loader\n");
416             ERR_print_errors(bio_err);
417             ret++;
418             break;
419         }
420
421         if (type == OSSL_STORE_INFO_NAME) {
422             const char *name = OSSL_STORE_INFO_get0_NAME(info);
423             const char *desc = OSSL_STORE_INFO_get0_NAME_description(info);
424             indent_printf(indent, bio_out, "%d: %s: %s\n", items, infostr,
425                           name);
426             if (desc != NULL)
427                 indent_printf(indent, bio_out, "%s\n", desc);
428         } else {
429             indent_printf(indent, bio_out, "%d: %s\n", items, infostr);
430         }
431
432         /*
433          * Unfortunately, PEM_X509_INFO_write_bio() is sorely lacking in
434          * functionality, so we must figure out how exactly to write things
435          * ourselves...
436          */
437         switch (type) {
438         case OSSL_STORE_INFO_NAME:
439             if (recursive) {
440                 const char *suburi = OSSL_STORE_INFO_get0_NAME(info);
441                 ret += process(suburi, uimeth, uidata,
442                                expected, criterion, search,
443                                text, noout, recursive, indent + 2, out, prog,
444                                libctx);
445             }
446             break;
447         case OSSL_STORE_INFO_PARAMS:
448             if (text)
449                 EVP_PKEY_print_params(out, OSSL_STORE_INFO_get0_PARAMS(info),
450                                       0, NULL);
451             if (!noout)
452                 PEM_write_bio_Parameters(out,
453                                          OSSL_STORE_INFO_get0_PARAMS(info));
454             break;
455         case OSSL_STORE_INFO_PUBKEY:
456             if (text)
457                 EVP_PKEY_print_public(out, OSSL_STORE_INFO_get0_PUBKEY(info),
458                                       0, NULL);
459             if (!noout)
460                 PEM_write_bio_PUBKEY(out, OSSL_STORE_INFO_get0_PUBKEY(info));
461             break;
462         case OSSL_STORE_INFO_PKEY:
463             if (text)
464                 EVP_PKEY_print_private(out, OSSL_STORE_INFO_get0_PKEY(info),
465                                        0, NULL);
466             if (!noout)
467                 PEM_write_bio_PrivateKey(out, OSSL_STORE_INFO_get0_PKEY(info),
468                                          NULL, NULL, 0, NULL, NULL);
469             break;
470         case OSSL_STORE_INFO_CERT:
471             if (text)
472                 X509_print(out, OSSL_STORE_INFO_get0_CERT(info));
473             if (!noout)
474                 PEM_write_bio_X509(out, OSSL_STORE_INFO_get0_CERT(info));
475             break;
476         case OSSL_STORE_INFO_CRL:
477             if (text)
478                 X509_CRL_print(out, OSSL_STORE_INFO_get0_CRL(info));
479             if (!noout)
480                 PEM_write_bio_X509_CRL(out, OSSL_STORE_INFO_get0_CRL(info));
481             break;
482         default:
483             BIO_printf(bio_err, "!!! Unknown code\n");
484             ret++;
485             break;
486         }
487         items++;
488         OSSL_STORE_INFO_free(info);
489     }
490     indent_printf(indent, out, "Total found: %d\n", items);
491
492  end2:
493     if (!OSSL_STORE_close(store_ctx)) {
494         ERR_print_errors(bio_err);
495         ret++;
496     }
497
498     return ret;
499 }