2 * Copyright 1995-2019 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>
27 /* Needed to get the other O_xxx flags. */
28 #ifdef OPENSSL_SYS_VMS
34 /* Special sentinel to exit the program. */
35 #define EXIT_THE_PROGRAM (-1)
38 * The LHASH callbacks ("hash" & "cmp") have been replaced by functions with
39 * the base prototypes (we cast each variable inside the function to the
40 * required type of "FUNCTION*"). This removes the necessity for
41 * macro-generated wrapper functions.
43 static LHASH_OF(FUNCTION) *prog_init(void);
44 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]);
45 char *default_config_file = NULL;
51 static int apps_startup(void)
54 signal(SIGPIPE, SIG_IGN);
57 /* Set non-default library initialisation settings */
58 if (!OPENSSL_init_ssl(OPENSSL_INIT_ENGINE_ALL_BUILTIN
59 | OPENSSL_INIT_LOAD_CONFIG, NULL))
67 static void apps_shutdown(void)
72 static char *make_config_name(void)
78 if ((t = getenv("OPENSSL_CONF")) != NULL)
79 return OPENSSL_strdup(t);
81 t = X509_get_default_cert_area();
82 len = strlen(t) + 1 + strlen(OPENSSL_CONF) + 1;
83 p = app_malloc(len, "config filename buffer");
85 #ifndef OPENSSL_SYS_VMS
88 strcat(p, OPENSSL_CONF);
94 #ifndef OPENSSL_NO_TRACE
95 typedef struct tracedata_st {
97 unsigned int ingroup:1;
100 static size_t internal_trace_cb(const char *buf, size_t cnt,
101 int category, int cmd, void *vdata)
104 tracedata *trace_data = vdata;
105 char buffer[256], *hex;
106 CRYPTO_THREAD_ID tid;
109 case OSSL_TRACE_CTRL_BEGIN:
110 if (!ossl_assert(!trace_data->ingroup))
112 trace_data->ingroup = 1;
114 tid = CRYPTO_THREAD_get_current_id();
115 hex = OPENSSL_buf2hexstr((const unsigned char *)&tid, sizeof(tid));
116 BIO_snprintf(buffer, sizeof(buffer), "TRACE[%s]:%s: ",
117 hex, OSSL_trace_get_category_name(category));
119 BIO_ctrl(trace_data->bio, PREFIX_CTRL_SET_PREFIX,
120 strlen(buffer), buffer);
122 case OSSL_TRACE_CTRL_WRITE:
123 if (!ossl_assert(trace_data->ingroup))
126 ret = BIO_write(trace_data->bio, buf, cnt);
128 case OSSL_TRACE_CTRL_END:
129 if (!ossl_assert(trace_data->ingroup))
131 trace_data->ingroup = 0;
133 BIO_ctrl(trace_data->bio, PREFIX_CTRL_SET_PREFIX, 0, NULL);
138 return ret < 0 ? 0 : ret;
141 DEFINE_STACK_OF(tracedata)
142 static STACK_OF(tracedata) *trace_data_stack;
144 static void tracedata_free(tracedata *data)
146 BIO_free_all(data->bio);
150 static STACK_OF(tracedata) *trace_data_stack;
152 static void cleanup_trace(void)
154 sk_tracedata_pop_free(trace_data_stack, tracedata_free);
157 static void setup_trace_category(int category)
160 tracedata *trace_data;
162 if (OSSL_trace_enabled(category))
165 channel = BIO_push(BIO_new(apps_bf_prefix()),
166 dup_bio_err(FORMAT_TEXT));
167 trace_data = OPENSSL_zalloc(sizeof(*trace_data));
169 if (trace_data == NULL
170 || (trace_data->bio = channel) == NULL
171 || OSSL_trace_set_callback(category, internal_trace_cb,
173 || sk_tracedata_push(trace_data_stack, trace_data) == 0) {
176 "warning: unable to setup trace callback for category '%s'.\n",
177 OSSL_trace_get_category_name(category));
179 OSSL_trace_set_callback(category, NULL, NULL);
180 BIO_free_all(channel);
184 static void setup_trace(const char *str)
189 * We add this handler as early as possible to ensure it's executed
190 * as late as possible, i.e. after the TRACE code has done its cleanup
191 * (which happens last in OPENSSL_cleanup).
193 atexit(cleanup_trace);
195 trace_data_stack = sk_tracedata_new_null();
196 val = OPENSSL_strdup(str);
202 for (valp = val; (item = strtok(valp, ",")) != NULL; valp = NULL) {
203 int category = OSSL_trace_get_category_num(item);
205 if (category == OSSL_TRACE_CATEGORY_ALL) {
206 while (++category < OSSL_TRACE_CATEGORY_NUM)
207 setup_trace_category(category);
209 } else if (category > 0) {
210 setup_trace_category(category);
213 "warning: unknown trace category: '%s'.\n", item);
220 #endif /* OPENSSL_NO_TRACE */
222 int main(int argc, char *argv[])
225 LHASH_OF(FUNCTION) *prog = NULL;
230 int first, n, i, ret = 0;
235 /* Set up some of the environment. */
236 default_config_file = make_config_name();
237 bio_in = dup_bio_in(FORMAT_TEXT);
238 bio_out = dup_bio_out(FORMAT_TEXT);
239 bio_err = dup_bio_err(FORMAT_TEXT);
241 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
242 argv = copy_argv(&argc, argv);
243 #elif defined(_WIN32)
245 * Replace argv[] with UTF-8 encoded strings.
247 win32_utf8argv(&argc, &argv);
251 * We use the prefix method to get the trace output we want. Since some
252 * trace outputs happen with OPENSSL_cleanup(), which is run automatically
253 * after exit(), we need to destroy the prefix method as late as possible.
255 atexit(destroy_prefix_method);
257 #ifndef OPENSSL_NO_TRACE
258 setup_trace(getenv("OPENSSL_TRACE"));
261 p = getenv("OPENSSL_DEBUG_MEMORY");
262 if (p != NULL && strcmp(p, "on") == 0)
263 CRYPTO_set_mem_debug(1);
264 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
266 if (getenv("OPENSSL_FIPS")) {
267 BIO_printf(bio_err, "FIPS mode not supported.\n");
271 if (!apps_startup()) {
273 "FATAL: Startup failure (dev note: apps_startup() failed)\n");
274 ERR_print_errors(bio_err);
280 pname = opt_progname(argv[0]);
282 /* first check the program name */
284 fp = lh_FUNCTION_retrieve(prog, &f);
287 ret = fp->func(argc, argv);
291 /* If there is stuff on the command line, run with that. */
295 ret = do_cmd(prog, argc, argv);
301 /* ok, lets enter interactive mode */
304 /* Read a line, continue reading if line ends with \ */
305 for (p = buf, n = sizeof(buf), i = 0, first = 1; n > 0; first = 0) {
306 prompt = first ? "OpenSSL> " : "> ";
309 fputs(prompt, stdout);
311 if (!fgets(p, n, stdin))
318 if (p[i - 2] != '\\')
325 extern char *readline(const char *);
326 extern void add_history(const char *cp);
329 text = readline(prompt);
335 if (text[i - 1] != '\\') {
336 p += strlen(strcpy(p, text));
343 p += strlen(strcpy(p, text));
350 if (!chopup_args(&arg, buf)) {
351 BIO_printf(bio_err, "Can't parse (no memory?)\n");
355 ret = do_cmd(prog, arg.argc, arg.argv);
356 if (ret == EXIT_THE_PROGRAM) {
361 BIO_printf(bio_err, "error in %s\n", arg.argv[0]);
362 (void)BIO_flush(bio_out);
363 (void)BIO_flush(bio_err);
367 OPENSSL_free(default_config_file);
368 lh_FUNCTION_free(prog);
369 OPENSSL_free(arg.argv);
373 BIO_free_all(bio_out);
375 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
376 if (CRYPTO_mem_leaks(bio_err) <= 0)
383 typedef enum HELP_CHOICE {
384 OPT_hERR = -1, OPT_hEOF = 0, OPT_hHELP
387 const OPTIONS help_options[] = {
388 {OPT_HELP_STR, 1, '-', "Usage: help [options]\n"},
389 {OPT_HELP_STR, 1, '-', " help [command]\n"},
390 {"help", OPT_hHELP, '-', "Display this summary"},
395 int help_main(int argc, char **argv)
404 prog = opt_init(argc, argv, help_options);
405 while ((o = opt_next()) != OPT_hEOF) {
409 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
412 opt_help(help_options);
417 if (opt_num_rest() == 1) {
420 new_argv[0] = opt_rest()[0];
421 new_argv[1] = "--help";
423 return do_cmd(prog_init(), 2, new_argv);
425 if (opt_num_rest() != 0) {
426 BIO_printf(bio_err, "Usage: %s\n", prog);
430 calculate_columns(functions, &dc);
431 BIO_printf(bio_err, "Standard commands");
434 for (fp = functions; fp->name != NULL; fp++) {
436 if (i++ % dc.columns == 0) {
437 BIO_printf(bio_err, "\n");
440 if (fp->type != tp) {
443 BIO_printf(bio_err, "\n");
447 "\nMessage Digest commands (see the `dgst' command for more details)\n");
448 } else if (tp == FT_cipher) {
451 "\nCipher commands (see the `enc' command for more details)\n");
454 BIO_printf(bio_err, "%-*s", dc.width, fp->name);
456 BIO_printf(bio_err, "\n\n");
460 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
464 if (argc <= 0 || argv[0] == NULL)
467 fp = lh_FUNCTION_retrieve(prog, &f);
469 if (EVP_get_digestbyname(argv[0])) {
473 } else if (EVP_get_cipherbyname(argv[0])) {
480 return fp->func(argc, argv);
482 if ((strncmp(argv[0], "no-", 3)) == 0) {
484 * User is asking if foo is unsupported, by trying to "run" the
485 * no-foo command. Strange.
487 f.name = argv[0] + 3;
488 if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
489 BIO_printf(bio_out, "%s\n", argv[0]);
492 BIO_printf(bio_out, "%s\n", argv[0] + 3);
495 if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 ||
496 strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0)
497 /* Special value to mean "exit the program. */
498 return EXIT_THE_PROGRAM;
500 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
505 static int function_cmp(const FUNCTION * a, const FUNCTION * b)
507 return strncmp(a->name, b->name, 8);
510 static unsigned long function_hash(const FUNCTION * a)
512 return OPENSSL_LH_strhash(a->name);
515 static int SortFnByName(const void *_f1, const void *_f2)
517 const FUNCTION *f1 = _f1;
518 const FUNCTION *f2 = _f2;
520 if (f1->type != f2->type)
521 return f1->type - f2->type;
522 return strcmp(f1->name, f2->name);
525 static LHASH_OF(FUNCTION) *prog_init(void)
527 static LHASH_OF(FUNCTION) *ret = NULL;
528 static int prog_inited = 0;
537 /* Sort alphabetically within category. For nicer help displays. */
538 for (i = 0, f = functions; f->name != NULL; ++f, ++i)
540 qsort(functions, i, sizeof(*functions), SortFnByName);
542 if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
545 for (f = functions; f->name != NULL; f++)
546 (void)lh_FUNCTION_insert(ret, f);