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