cmdline app: add provider commandline options.
[openssl.git] / apps / storeutl.c
1 /*
2  * Copyright 2016-2018 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);
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;
85     OSSL_STORE_SEARCH *search = NULL;
86     const EVP_MD *digest = NULL;
87
88     while ((o = opt_next()) != OPT_EOF) {
89         switch (o) {
90         case OPT_EOF:
91         case OPT_ERR:
92  opthelp:
93             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
94             goto end;
95         case OPT_HELP:
96             opt_help(storeutl_options);
97             ret = 0;
98             goto end;
99         case OPT_OUT:
100             outfile = opt_arg();
101             break;
102         case OPT_PASSIN:
103             passinarg = opt_arg();
104             break;
105         case OPT_NOOUT:
106             noout = 1;
107             break;
108         case OPT_TEXT:
109             text = 1;
110             break;
111         case OPT_RECURSIVE:
112             recursive = 1;
113             break;
114         case OPT_SEARCHFOR_CERTS:
115         case OPT_SEARCHFOR_KEYS:
116         case OPT_SEARCHFOR_CRLS:
117             if (expected != 0) {
118                 BIO_printf(bio_err, "%s: only one search type can be given.\n",
119                            prog);
120                 goto end;
121             }
122             {
123                 static const struct {
124                     enum OPTION_choice choice;
125                     int type;
126                 } map[] = {
127                     {OPT_SEARCHFOR_CERTS, OSSL_STORE_INFO_CERT},
128                     {OPT_SEARCHFOR_KEYS, OSSL_STORE_INFO_PKEY},
129                     {OPT_SEARCHFOR_CRLS, OSSL_STORE_INFO_CRL},
130                 };
131                 size_t i;
132
133                 for (i = 0; i < OSSL_NELEM(map); i++) {
134                     if (o == map[i].choice) {
135                         expected = map[i].type;
136                         break;
137                     }
138                 }
139                 /*
140                  * If expected wasn't set at this point, it means the map
141                  * isn't synchronised with the possible options leading here.
142                  */
143                 OPENSSL_assert(expected != 0);
144             }
145             break;
146         case OPT_CRITERION_SUBJECT:
147             if (criterion != 0) {
148                 BIO_printf(bio_err, "%s: criterion already given.\n",
149                            prog);
150                 goto end;
151             }
152             criterion = OSSL_STORE_SEARCH_BY_NAME;
153             if (subject != NULL) {
154                 BIO_printf(bio_err, "%s: subject already given.\n",
155                            prog);
156                 goto end;
157             }
158             if ((subject = parse_name(opt_arg(), MBSTRING_UTF8, 1)) == NULL) {
159                 BIO_printf(bio_err, "%s: can't parse subject argument.\n",
160                            prog);
161                 goto end;
162             }
163             break;
164         case OPT_CRITERION_ISSUER:
165             if (criterion != 0
166                 || (criterion == OSSL_STORE_SEARCH_BY_ISSUER_SERIAL
167                     && issuer != NULL)) {
168                 BIO_printf(bio_err, "%s: criterion already given.\n",
169                            prog);
170                 goto end;
171             }
172             criterion = OSSL_STORE_SEARCH_BY_ISSUER_SERIAL;
173             if (issuer != NULL) {
174                 BIO_printf(bio_err, "%s: issuer already given.\n",
175                            prog);
176                 goto end;
177             }
178             if ((issuer = parse_name(opt_arg(), MBSTRING_UTF8, 1)) == NULL) {
179                 BIO_printf(bio_err, "%s: can't parse issuer argument.\n",
180                            prog);
181                 goto end;
182             }
183             break;
184         case OPT_CRITERION_SERIAL:
185             if (criterion != 0
186                 || (criterion == OSSL_STORE_SEARCH_BY_ISSUER_SERIAL
187                     && serial != NULL)) {
188                 BIO_printf(bio_err, "%s: criterion already given.\n",
189                            prog);
190                 goto end;
191             }
192             criterion = OSSL_STORE_SEARCH_BY_ISSUER_SERIAL;
193             if (serial != NULL) {
194                 BIO_printf(bio_err, "%s: serial number already given.\n",
195                            prog);
196                 goto end;
197             }
198             if ((serial = s2i_ASN1_INTEGER(NULL, opt_arg())) == NULL) {
199                 BIO_printf(bio_err, "%s: can't parse serial number argument.\n",
200                            prog);
201                 goto end;
202             }
203             break;
204         case OPT_CRITERION_FINGERPRINT:
205             if (criterion != 0
206                 || (criterion == OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT
207                     && fingerprint != NULL)) {
208                 BIO_printf(bio_err, "%s: criterion already given.\n",
209                            prog);
210                 goto end;
211             }
212             criterion = OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT;
213             if (fingerprint != NULL) {
214                 BIO_printf(bio_err, "%s: fingerprint already given.\n",
215                            prog);
216                 goto end;
217             }
218             {
219                 long tmplen = 0;
220
221                 if ((fingerprint = OPENSSL_hexstr2buf(opt_arg(), &tmplen))
222                     == NULL) {
223                     BIO_printf(bio_err,
224                                "%s: can't parse fingerprint argument.\n",
225                                prog);
226                     goto end;
227                 }
228                 fingerprintlen = (size_t)tmplen;
229             }
230             break;
231         case OPT_CRITERION_ALIAS:
232             if (criterion != 0) {
233                 BIO_printf(bio_err, "%s: criterion already given.\n",
234                            prog);
235                 goto end;
236             }
237             criterion = OSSL_STORE_SEARCH_BY_ALIAS;
238             if (alias != NULL) {
239                 BIO_printf(bio_err, "%s: alias already given.\n",
240                            prog);
241                 goto end;
242             }
243             if ((alias = OPENSSL_strdup(opt_arg())) == NULL) {
244                 BIO_printf(bio_err, "%s: can't parse alias argument.\n",
245                            prog);
246                 goto end;
247             }
248             break;
249         case OPT_ENGINE:
250             e = setup_engine(opt_arg(), 0);
251             break;
252         case OPT_MD:
253             if (!opt_md(opt_unknown(), &digest))
254                 goto opthelp;
255         case OPT_PROV_CASES:
256             if (!opt_provider(o))
257                 goto end;
258             break;
259         }
260     }
261     argc = opt_num_rest();
262     argv = opt_rest();
263
264     if (argc == 0) {
265         BIO_printf(bio_err, "%s: No URI given, nothing to do...\n", prog);
266         goto opthelp;
267     }
268     if (argc > 1) {
269         BIO_printf(bio_err, "%s: Unknown extra parameters after URI\n", prog);
270         goto opthelp;
271     }
272
273     if (criterion != 0) {
274         switch (criterion) {
275         case OSSL_STORE_SEARCH_BY_NAME:
276             if ((search = OSSL_STORE_SEARCH_by_name(subject)) == NULL) {
277                 ERR_print_errors(bio_err);
278                 goto end;
279             }
280             break;
281         case OSSL_STORE_SEARCH_BY_ISSUER_SERIAL:
282             if (issuer == NULL || serial == NULL) {
283                 BIO_printf(bio_err,
284                            "%s: both -issuer and -serial must be given.\n",
285                            prog);
286                 goto end;
287             }
288             if ((search = OSSL_STORE_SEARCH_by_issuer_serial(issuer, serial))
289                 == NULL) {
290                 ERR_print_errors(bio_err);
291                 goto end;
292             }
293             break;
294         case OSSL_STORE_SEARCH_BY_KEY_FINGERPRINT:
295             if ((search = OSSL_STORE_SEARCH_by_key_fingerprint(digest,
296                                                                fingerprint,
297                                                                fingerprintlen))
298                 == NULL) {
299                 ERR_print_errors(bio_err);
300                 goto end;
301             }
302             break;
303         case OSSL_STORE_SEARCH_BY_ALIAS:
304             if ((search = OSSL_STORE_SEARCH_by_alias(alias)) == NULL) {
305                 ERR_print_errors(bio_err);
306                 goto end;
307             }
308             break;
309         }
310     }
311
312     if (!app_passwd(passinarg, NULL, &passin, NULL)) {
313         BIO_printf(bio_err, "Error getting passwords\n");
314         goto end;
315     }
316     pw_cb_data.password = passin;
317     pw_cb_data.prompt_info = argv[0];
318
319     out = bio_open_default(outfile, 'w', FORMAT_TEXT);
320     if (out == NULL)
321         goto end;
322
323     ret = process(argv[0], get_ui_method(), &pw_cb_data,
324                   expected, criterion, search,
325                   text, noout, recursive, 0, out, prog);
326
327  end:
328     OPENSSL_free(fingerprint);
329     OPENSSL_free(alias);
330     ASN1_INTEGER_free(serial);
331     X509_NAME_free(subject);
332     X509_NAME_free(issuer);
333     OSSL_STORE_SEARCH_free(search);
334     BIO_free_all(out);
335     OPENSSL_free(passin);
336     release_engine(e);
337     return ret;
338 }
339
340 static int indent_printf(int indent, BIO *bio, const char *format, ...)
341 {
342     va_list args;
343     int ret;
344
345     va_start(args, format);
346
347     ret = BIO_printf(bio, "%*s", indent, "") + BIO_vprintf(bio, format, args);
348
349     va_end(args);
350     return ret;
351 }
352
353 static int process(const char *uri, const UI_METHOD *uimeth, PW_CB_DATA *uidata,
354                    int expected, int criterion, OSSL_STORE_SEARCH *search,
355                    int text, int noout, int recursive, int indent, BIO *out,
356                    const char *prog)
357 {
358     OSSL_STORE_CTX *store_ctx = NULL;
359     int ret = 1, items = 0;
360
361     if ((store_ctx = OSSL_STORE_open(uri, uimeth, uidata, 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_eof(store_ctx))
400                 break;
401
402             if (OSSL_STORE_error(store_ctx)) {
403                 if (recursive)
404                     ERR_clear_error();
405                 else
406                     ERR_print_errors(bio_err);
407                 ret++;
408                 continue;
409             }
410
411             BIO_printf(bio_err,
412                        "ERROR: OSSL_STORE_load() returned NULL without "
413                        "eof or error indications\n");
414             BIO_printf(bio_err, "       This is an error in the loader\n");
415             ERR_print_errors(bio_err);
416             ret++;
417             break;
418         }
419
420         if (type == OSSL_STORE_INFO_NAME) {
421             const char *name = OSSL_STORE_INFO_get0_NAME(info);
422             const char *desc = OSSL_STORE_INFO_get0_NAME_description(info);
423             indent_printf(indent, bio_out, "%d: %s: %s\n", items, infostr,
424                           name);
425             if (desc != NULL)
426                 indent_printf(indent, bio_out, "%s\n", desc);
427         } else {
428             indent_printf(indent, bio_out, "%d: %s\n", items, infostr);
429         }
430
431         /*
432          * Unfortunately, PEM_X509_INFO_write_bio() is sorely lacking in
433          * functionality, so we must figure out how exactly to write things
434          * ourselves...
435          */
436         switch (type) {
437         case OSSL_STORE_INFO_NAME:
438             if (recursive) {
439                 const char *suburi = OSSL_STORE_INFO_get0_NAME(info);
440                 ret += process(suburi, uimeth, uidata,
441                                expected, criterion, search,
442                                text, noout, recursive, indent + 2, out, prog);
443             }
444             break;
445         case OSSL_STORE_INFO_PARAMS:
446             if (text)
447                 EVP_PKEY_print_params(out, OSSL_STORE_INFO_get0_PARAMS(info),
448                                       0, NULL);
449             if (!noout)
450                 PEM_write_bio_Parameters(out,
451                                          OSSL_STORE_INFO_get0_PARAMS(info));
452             break;
453         case OSSL_STORE_INFO_PKEY:
454             if (text)
455                 EVP_PKEY_print_private(out, OSSL_STORE_INFO_get0_PKEY(info),
456                                        0, NULL);
457             if (!noout)
458                 PEM_write_bio_PrivateKey(out, OSSL_STORE_INFO_get0_PKEY(info),
459                                          NULL, NULL, 0, NULL, NULL);
460             break;
461         case OSSL_STORE_INFO_CERT:
462             if (text)
463                 X509_print(out, OSSL_STORE_INFO_get0_CERT(info));
464             if (!noout)
465                 PEM_write_bio_X509(out, OSSL_STORE_INFO_get0_CERT(info));
466             break;
467         case OSSL_STORE_INFO_CRL:
468             if (text)
469                 X509_CRL_print(out, OSSL_STORE_INFO_get0_CRL(info));
470             if (!noout)
471                 PEM_write_bio_X509_CRL(out, OSSL_STORE_INFO_get0_CRL(info));
472             break;
473         default:
474             BIO_printf(bio_err, "!!! Unknown code\n");
475             ret++;
476             break;
477         }
478         items++;
479         OSSL_STORE_INFO_free(info);
480     }
481     indent_printf(indent, out, "Total found: %d\n", items);
482
483  end2:
484     if (!OSSL_STORE_close(store_ctx)) {
485         ERR_print_errors(bio_err);
486         ret++;
487     }
488
489     return ret;
490 }