Improve description of CMP untrusted certs and msg 'sender' field
[openssl.git] / apps / openssl.c
1 /*
2  * Copyright 1995-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 <internal/cryptlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <openssl/bio.h>
15 #include <openssl/crypto.h>
16 #include <openssl/trace.h>
17 #include <openssl/lhash.h>
18 #include <openssl/conf.h>
19 #include <openssl/x509.h>
20 #include <openssl/pem.h>
21 #include <openssl/ssl.h>
22 #ifndef OPENSSL_NO_ENGINE
23 # include <openssl/engine.h>
24 #endif
25 #include <openssl/err.h>
26 /* Needed to get the other O_xxx flags. */
27 #ifdef OPENSSL_SYS_VMS
28 # include <unixio.h>
29 #endif
30 #include "apps.h"
31 #include "progs.h"
32
33 /*
34  * The LHASH callbacks ("hash" & "cmp") have been replaced by functions with
35  * the base prototypes (we cast each variable inside the function to the
36  * required type of "FUNCTION*"). This removes the necessity for
37  * macro-generated wrapper functions.
38  */
39 static LHASH_OF(FUNCTION) *prog_init(void);
40 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]);
41 char *default_config_file = NULL;
42
43 BIO *bio_in = NULL;
44 BIO *bio_out = NULL;
45 BIO *bio_err = NULL;
46
47 static void warn_deprecated(const FUNCTION *fp)
48 {
49     if (fp->deprecated_version != NULL)
50         BIO_printf(bio_err, "The command %s was deprecated in version %s.",
51                    fp->name, fp->deprecated_version);
52     else
53         BIO_printf(bio_err, "The command %s is deprecated.", fp->name);
54     if (strcmp(fp->deprecated_alternative, DEPRECATED_NO_ALTERNATIVE) != 0)
55         BIO_printf(bio_err, " Use '%s' instead.", fp->deprecated_alternative);
56     BIO_printf(bio_err, "\n");
57 }
58
59 static int apps_startup(void)
60 {
61 #ifdef SIGPIPE
62     signal(SIGPIPE, SIG_IGN);
63 #endif
64
65     /* Set non-default library initialisation settings */
66     if (!OPENSSL_init_ssl(OPENSSL_INIT_ENGINE_ALL_BUILTIN
67                           | OPENSSL_INIT_LOAD_CONFIG, NULL))
68         return 0;
69
70     setup_ui_method();
71
72     return 1;
73 }
74
75 static void apps_shutdown(void)
76 {
77     destroy_ui_method();
78 }
79
80
81 #ifndef OPENSSL_NO_TRACE
82 typedef struct tracedata_st {
83     BIO *bio;
84     unsigned int ingroup:1;
85 } tracedata;
86
87 static size_t internal_trace_cb(const char *buf, size_t cnt,
88                                 int category, int cmd, void *vdata)
89 {
90     int ret = 0;
91     tracedata *trace_data = vdata;
92     char buffer[256], *hex;
93     CRYPTO_THREAD_ID tid;
94
95     switch (cmd) {
96     case OSSL_TRACE_CTRL_BEGIN:
97         if (!ossl_assert(!trace_data->ingroup))
98             return 0;
99         trace_data->ingroup = 1;
100
101         tid = CRYPTO_THREAD_get_current_id();
102         hex = OPENSSL_buf2hexstr((const unsigned char *)&tid, sizeof(tid));
103         BIO_snprintf(buffer, sizeof(buffer), "TRACE[%s]:%s: ",
104                      hex == NULL ? "<null>" : hex,
105                      OSSL_trace_get_category_name(category));
106         OPENSSL_free(hex);
107         BIO_set_prefix(trace_data->bio, buffer);
108         break;
109     case OSSL_TRACE_CTRL_WRITE:
110         if (!ossl_assert(trace_data->ingroup))
111             return 0;
112
113         ret = BIO_write(trace_data->bio, buf, cnt);
114         break;
115     case OSSL_TRACE_CTRL_END:
116         if (!ossl_assert(trace_data->ingroup))
117             return 0;
118         trace_data->ingroup = 0;
119
120         BIO_set_prefix(trace_data->bio, NULL);
121
122         break;
123     }
124
125     return ret < 0 ? 0 : ret;
126 }
127
128 DEFINE_STACK_OF(tracedata)
129 static STACK_OF(tracedata) *trace_data_stack;
130
131 static void tracedata_free(tracedata *data)
132 {
133     BIO_free_all(data->bio);
134     OPENSSL_free(data);
135 }
136
137 static STACK_OF(tracedata) *trace_data_stack;
138
139 static void cleanup_trace(void)
140 {
141     sk_tracedata_pop_free(trace_data_stack, tracedata_free);
142 }
143
144 static void setup_trace_category(int category)
145 {
146     BIO *channel;
147     tracedata *trace_data;
148
149     if (OSSL_trace_enabled(category))
150         return;
151
152     channel = BIO_push(BIO_new(BIO_f_prefix()), dup_bio_err(FORMAT_TEXT));
153     trace_data = OPENSSL_zalloc(sizeof(*trace_data));
154
155     if (trace_data == NULL
156         || (trace_data->bio = channel) == NULL
157         || OSSL_trace_set_callback(category, internal_trace_cb,
158                                    trace_data) == 0
159         || sk_tracedata_push(trace_data_stack, trace_data) == 0) {
160
161         fprintf(stderr,
162                 "warning: unable to setup trace callback for category '%s'.\n",
163                 OSSL_trace_get_category_name(category));
164
165         OSSL_trace_set_callback(category, NULL, NULL);
166         BIO_free_all(channel);
167     }
168 }
169
170 static void setup_trace(const char *str)
171 {
172     char *val;
173
174     /*
175      * We add this handler as early as possible to ensure it's executed
176      * as late as possible, i.e. after the TRACE code has done its cleanup
177      * (which happens last in OPENSSL_cleanup).
178      */
179     atexit(cleanup_trace);
180
181     trace_data_stack = sk_tracedata_new_null();
182     val = OPENSSL_strdup(str);
183
184     if (val != NULL) {
185         char *valp = val;
186         char *item;
187
188         for (valp = val; (item = strtok(valp, ",")) != NULL; valp = NULL) {
189             int category = OSSL_trace_get_category_num(item);
190
191             if (category == OSSL_TRACE_CATEGORY_ALL) {
192                 while (++category < OSSL_TRACE_CATEGORY_NUM)
193                     setup_trace_category(category);
194                 break;
195             } else if (category > 0) {
196                 setup_trace_category(category);
197             } else {
198                 fprintf(stderr,
199                         "warning: unknown trace category: '%s'.\n", item);
200             }
201         }
202     }
203
204     OPENSSL_free(val);
205 }
206 #endif /* OPENSSL_NO_TRACE */
207
208 int main(int argc, char *argv[])
209 {
210     FUNCTION f, *fp;
211     LHASH_OF(FUNCTION) *prog = NULL;
212     char *pname;
213     ARGS arg;
214     int ret = 0;
215
216     arg.argv = NULL;
217     arg.size = 0;
218
219     /* Set up some of the environment. */
220     bio_in = dup_bio_in(FORMAT_TEXT);
221     bio_out = dup_bio_out(FORMAT_TEXT);
222     bio_err = dup_bio_err(FORMAT_TEXT);
223
224 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
225     argv = copy_argv(&argc, argv);
226 #elif defined(_WIN32)
227     /*
228      * Replace argv[] with UTF-8 encoded strings.
229      */
230     win32_utf8argv(&argc, &argv);
231 #endif
232
233 #ifndef OPENSSL_NO_TRACE
234     setup_trace(getenv("OPENSSL_TRACE"));
235 #endif
236
237     if (!apps_startup()) {
238         BIO_printf(bio_err,
239                    "FATAL: Startup failure (dev note: apps_startup() failed)\n");
240         ERR_print_errors(bio_err);
241         ret = 1;
242         goto end;
243     }
244
245     prog = prog_init();
246     if (prog == NULL) {
247         BIO_printf(bio_err,
248                    "FATAL: Startup failure (dev note: prog_init() failed)\n");
249         ERR_print_errors(bio_err);
250         ret = 1;
251         goto end;
252     }
253     pname = opt_progname(argv[0]);
254
255     default_config_file = CONF_get1_default_config_file();
256     if (default_config_file == NULL)
257         app_bail_out("%s: could not get default config file\n", pname);
258
259     /* first check the program name */
260     f.name = pname;
261     fp = lh_FUNCTION_retrieve(prog, &f);
262     if (fp == NULL) {
263         /* We assume we've been called as 'openssl cmd' */
264         argc--;
265         argv++;
266     }
267
268     /* If there's a command, run with that, otherwise "help". */
269     ret = argc > 0
270         ? do_cmd(prog, argc, argv)
271         : help_main(argc, argv);
272
273  end:
274     app_providers_cleanup();
275     OPENSSL_free(default_config_file);
276     lh_FUNCTION_free(prog);
277     OPENSSL_free(arg.argv);
278     app_RAND_write();
279
280     BIO_free(bio_in);
281     BIO_free_all(bio_out);
282     apps_shutdown();
283     BIO_free(bio_err);
284     EXIT(ret);
285 }
286
287 typedef enum HELP_CHOICE {
288     OPT_hERR = -1, OPT_hEOF = 0, OPT_hHELP
289 } HELP_CHOICE;
290
291 const OPTIONS help_options[] = {
292     {OPT_HELP_STR, 1, '-', "Usage: help [options] [command]\n"},
293
294     OPT_SECTION("General"),
295     {"help", OPT_hHELP, '-', "Display this summary"},
296
297     OPT_PARAMETERS(),
298     {"command", 0, 0, "Name of command to display help (optional)"},
299     {NULL}
300 };
301
302
303 int help_main(int argc, char **argv)
304 {
305     FUNCTION *fp;
306     int i, nl;
307     FUNC_TYPE tp;
308     char *prog;
309     HELP_CHOICE o;
310     DISPLAY_COLUMNS dc;
311
312     prog = opt_init(argc, argv, help_options);
313     while ((o = opt_next()) != OPT_hEOF) {
314         switch (o) {
315         case OPT_hERR:
316         case OPT_hEOF:
317             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
318             return 1;
319         case OPT_hHELP:
320             opt_help(help_options);
321             return 0;
322         }
323     }
324
325     if (opt_num_rest() == 1) {
326         char *new_argv[3];
327
328         new_argv[0] = opt_rest()[0];
329         new_argv[1] = "--help";
330         new_argv[2] = NULL;
331         return do_cmd(prog_init(), 2, new_argv);
332     }
333     if (opt_num_rest() != 0) {
334         BIO_printf(bio_err, "Usage: %s\n", prog);
335         return 1;
336     }
337
338     calculate_columns(functions, &dc);
339     BIO_printf(bio_err, "Standard commands");
340     i = 0;
341     tp = FT_none;
342     for (fp = functions; fp->name != NULL; fp++) {
343         nl = 0;
344         if (i++ % dc.columns == 0) {
345             BIO_printf(bio_err, "\n");
346             nl = 1;
347         }
348         if (fp->type != tp) {
349             tp = fp->type;
350             if (!nl)
351                 BIO_printf(bio_err, "\n");
352             if (tp == FT_md) {
353                 i = 1;
354                 BIO_printf(bio_err,
355                            "\nMessage Digest commands (see the `dgst' command for more details)\n");
356             } else if (tp == FT_cipher) {
357                 i = 1;
358                 BIO_printf(bio_err,
359                            "\nCipher commands (see the `enc' command for more details)\n");
360             }
361         }
362         BIO_printf(bio_err, "%-*s", dc.width, fp->name);
363     }
364     BIO_printf(bio_err, "\n\n");
365     return 0;
366 }
367
368 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
369 {
370     FUNCTION f, *fp;
371
372     if (argc <= 0 || argv[0] == NULL)
373         return 0;
374     f.name = argv[0];
375     fp = lh_FUNCTION_retrieve(prog, &f);
376     if (fp == NULL) {
377         if (EVP_get_digestbyname(argv[0])) {
378             f.type = FT_md;
379             f.func = dgst_main;
380             fp = &f;
381         } else if (EVP_get_cipherbyname(argv[0])) {
382             f.type = FT_cipher;
383             f.func = enc_main;
384             fp = &f;
385         }
386     }
387     if (fp != NULL) {
388         if (fp->deprecated_alternative != NULL)
389             warn_deprecated(fp);
390         return fp->func(argc, argv);
391     }
392     if ((strncmp(argv[0], "no-", 3)) == 0) {
393         /*
394          * User is asking if foo is unsupported, by trying to "run" the
395          * no-foo command.  Strange.
396          */
397         f.name = argv[0] + 3;
398         if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
399             BIO_printf(bio_out, "%s\n", argv[0]);
400             return 0;
401         }
402         BIO_printf(bio_out, "%s\n", argv[0] + 3);
403         return 1;
404     }
405
406     BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
407                argv[0]);
408     return 1;
409 }
410
411 static int function_cmp(const FUNCTION * a, const FUNCTION * b)
412 {
413     return strncmp(a->name, b->name, 8);
414 }
415
416 static unsigned long function_hash(const FUNCTION * a)
417 {
418     return OPENSSL_LH_strhash(a->name);
419 }
420
421 static int SortFnByName(const void *_f1, const void *_f2)
422 {
423     const FUNCTION *f1 = _f1;
424     const FUNCTION *f2 = _f2;
425
426     if (f1->type != f2->type)
427         return f1->type - f2->type;
428     return strcmp(f1->name, f2->name);
429 }
430
431 static LHASH_OF(FUNCTION) *prog_init(void)
432 {
433     static LHASH_OF(FUNCTION) *ret = NULL;
434     static int prog_inited = 0;
435     FUNCTION *f;
436     size_t i;
437
438     if (prog_inited)
439         return ret;
440
441     prog_inited = 1;
442
443     /* Sort alphabetically within category. For nicer help displays. */
444     for (i = 0, f = functions; f->name != NULL; ++f, ++i)
445         ;
446     qsort(functions, i, sizeof(*functions), SortFnByName);
447
448     if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
449         return NULL;
450
451     for (f = functions; f->name != NULL; f++)
452         (void)lh_FUNCTION_insert(ret, f);
453     return ret;
454 }