2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <internal/cryptlib.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>
25 #include <openssl/err.h>
26 /* Needed to get the other O_xxx flags. */
27 #ifdef OPENSSL_SYS_VMS
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.
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;
47 static void warn_deprecated(const FUNCTION *fp)
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);
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");
59 static int apps_startup(void)
62 signal(SIGPIPE, SIG_IGN);
65 /* Set non-default library initialisation settings */
66 if (!OPENSSL_init_ssl(OPENSSL_INIT_ENGINE_ALL_BUILTIN
67 | OPENSSL_INIT_LOAD_CONFIG, NULL))
75 static void apps_shutdown(void)
81 #ifndef OPENSSL_NO_TRACE
82 typedef struct tracedata_st {
84 unsigned int ingroup:1;
87 static size_t internal_trace_cb(const char *buf, size_t cnt,
88 int category, int cmd, void *vdata)
91 tracedata *trace_data = vdata;
92 char buffer[256], *hex;
96 case OSSL_TRACE_CTRL_BEGIN:
97 if (!ossl_assert(!trace_data->ingroup))
99 trace_data->ingroup = 1;
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));
107 BIO_set_prefix(trace_data->bio, buffer);
109 case OSSL_TRACE_CTRL_WRITE:
110 if (!ossl_assert(trace_data->ingroup))
113 ret = BIO_write(trace_data->bio, buf, cnt);
115 case OSSL_TRACE_CTRL_END:
116 if (!ossl_assert(trace_data->ingroup))
118 trace_data->ingroup = 0;
120 BIO_set_prefix(trace_data->bio, NULL);
125 return ret < 0 ? 0 : ret;
128 DEFINE_STACK_OF(tracedata)
129 static STACK_OF(tracedata) *trace_data_stack;
131 static void tracedata_free(tracedata *data)
133 BIO_free_all(data->bio);
137 static STACK_OF(tracedata) *trace_data_stack;
139 static void cleanup_trace(void)
141 sk_tracedata_pop_free(trace_data_stack, tracedata_free);
144 static void setup_trace_category(int category)
147 tracedata *trace_data;
149 if (OSSL_trace_enabled(category))
152 channel = BIO_push(BIO_new(BIO_f_prefix()), dup_bio_err(FORMAT_TEXT));
153 trace_data = OPENSSL_zalloc(sizeof(*trace_data));
155 if (trace_data == NULL
156 || (trace_data->bio = channel) == NULL
157 || OSSL_trace_set_callback(category, internal_trace_cb,
159 || sk_tracedata_push(trace_data_stack, trace_data) == 0) {
162 "warning: unable to setup trace callback for category '%s'.\n",
163 OSSL_trace_get_category_name(category));
165 OSSL_trace_set_callback(category, NULL, NULL);
166 BIO_free_all(channel);
170 static void setup_trace(const char *str)
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).
179 atexit(cleanup_trace);
181 trace_data_stack = sk_tracedata_new_null();
182 val = OPENSSL_strdup(str);
188 for (valp = val; (item = strtok(valp, ",")) != NULL; valp = NULL) {
189 int category = OSSL_trace_get_category_num(item);
191 if (category == OSSL_TRACE_CATEGORY_ALL) {
192 while (++category < OSSL_TRACE_CATEGORY_NUM)
193 setup_trace_category(category);
195 } else if (category > 0) {
196 setup_trace_category(category);
199 "warning: unknown trace category: '%s'.\n", item);
206 #endif /* OPENSSL_NO_TRACE */
208 int main(int argc, char *argv[])
211 LHASH_OF(FUNCTION) *prog = NULL;
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);
224 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
225 argv = copy_argv(&argc, argv);
226 #elif defined(_WIN32)
228 * Replace argv[] with UTF-8 encoded strings.
230 win32_utf8argv(&argc, &argv);
233 #ifndef OPENSSL_NO_TRACE
234 setup_trace(getenv("OPENSSL_TRACE"));
237 if (!apps_startup()) {
239 "FATAL: Startup failure (dev note: apps_startup() failed)\n");
240 ERR_print_errors(bio_err);
248 "FATAL: Startup failure (dev note: prog_init() failed)\n");
249 ERR_print_errors(bio_err);
253 pname = opt_progname(argv[0]);
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);
259 /* first check the program name */
261 fp = lh_FUNCTION_retrieve(prog, &f);
263 /* We assume we've been called as 'openssl cmd' */
268 /* If there's a command, run with that, otherwise "help". */
270 ? do_cmd(prog, argc, argv)
271 : help_main(argc, argv);
274 app_providers_cleanup();
275 OPENSSL_free(default_config_file);
276 lh_FUNCTION_free(prog);
277 OPENSSL_free(arg.argv);
281 BIO_free_all(bio_out);
287 typedef enum HELP_CHOICE {
288 OPT_hERR = -1, OPT_hEOF = 0, OPT_hHELP
291 const OPTIONS help_options[] = {
292 {OPT_HELP_STR, 1, '-', "Usage: help [options] [command]\n"},
294 OPT_SECTION("General"),
295 {"help", OPT_hHELP, '-', "Display this summary"},
298 {"command", 0, 0, "Name of command to display help (optional)"},
303 int help_main(int argc, char **argv)
312 prog = opt_init(argc, argv, help_options);
313 while ((o = opt_next()) != OPT_hEOF) {
317 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
320 opt_help(help_options);
325 if (opt_num_rest() == 1) {
328 new_argv[0] = opt_rest()[0];
329 new_argv[1] = "--help";
331 return do_cmd(prog_init(), 2, new_argv);
333 if (opt_num_rest() != 0) {
334 BIO_printf(bio_err, "Usage: %s\n", prog);
338 calculate_columns(functions, &dc);
339 BIO_printf(bio_err, "Standard commands");
342 for (fp = functions; fp->name != NULL; fp++) {
344 if (i++ % dc.columns == 0) {
345 BIO_printf(bio_err, "\n");
348 if (fp->type != tp) {
351 BIO_printf(bio_err, "\n");
355 "\nMessage Digest commands (see the `dgst' command for more details)\n");
356 } else if (tp == FT_cipher) {
359 "\nCipher commands (see the `enc' command for more details)\n");
362 BIO_printf(bio_err, "%-*s", dc.width, fp->name);
364 BIO_printf(bio_err, "\n\n");
368 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
372 if (argc <= 0 || argv[0] == NULL)
375 fp = lh_FUNCTION_retrieve(prog, &f);
377 if (EVP_get_digestbyname(argv[0])) {
381 } else if (EVP_get_cipherbyname(argv[0])) {
388 if (fp->deprecated_alternative != NULL)
390 return fp->func(argc, argv);
392 if ((strncmp(argv[0], "no-", 3)) == 0) {
394 * User is asking if foo is unsupported, by trying to "run" the
395 * no-foo command. Strange.
397 f.name = argv[0] + 3;
398 if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
399 BIO_printf(bio_out, "%s\n", argv[0]);
402 BIO_printf(bio_out, "%s\n", argv[0] + 3);
406 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
411 static int function_cmp(const FUNCTION * a, const FUNCTION * b)
413 return strncmp(a->name, b->name, 8);
416 static unsigned long function_hash(const FUNCTION * a)
418 return OPENSSL_LH_strhash(a->name);
421 static int SortFnByName(const void *_f1, const void *_f2)
423 const FUNCTION *f1 = _f1;
424 const FUNCTION *f2 = _f2;
426 if (f1->type != f2->type)
427 return f1->type - f2->type;
428 return strcmp(f1->name, f2->name);
431 static LHASH_OF(FUNCTION) *prog_init(void)
433 static LHASH_OF(FUNCTION) *ret = NULL;
434 static int prog_inited = 0;
443 /* Sort alphabetically within category. For nicer help displays. */
444 for (i = 0, f = functions; f->name != NULL; ++f, ++i)
446 qsort(functions, i, sizeof(*functions), SortFnByName);
448 if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
451 for (f = functions; f->name != NULL; f++)
452 (void)lh_FUNCTION_insert(ret, f);