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>
26 /* Needed to get the other O_xxx flags. */
27 #ifdef OPENSSL_SYS_VMS
33 /* Special sentinel to exit the program. */
34 #define EXIT_THE_PROGRAM (-1)
37 * The LHASH callbacks ("hash" & "cmp") have been replaced by functions with
38 * the base prototypes (we cast each variable inside the function to the
39 * required type of "FUNCTION*"). This removes the necessity for
40 * macro-generated wrapper functions.
42 static LHASH_OF(FUNCTION
) *prog_init(void);
43 static int do_cmd(LHASH_OF(FUNCTION
) *prog
, int argc
, char *argv
[]);
44 char *default_config_file
= NULL
;
50 static int apps_startup(void)
53 signal(SIGPIPE
, SIG_IGN
);
56 /* Set non-default library initialisation settings */
57 if (!OPENSSL_init_ssl(OPENSSL_INIT_ENGINE_ALL_BUILTIN
58 | OPENSSL_INIT_LOAD_CONFIG
, NULL
))
66 static void apps_shutdown(void)
71 static char *make_config_name(void)
77 if ((t
= getenv("OPENSSL_CONF")) != NULL
)
78 return OPENSSL_strdup(t
);
80 t
= X509_get_default_cert_area();
81 len
= strlen(t
) + 1 + strlen(OPENSSL_CONF
) + 1;
82 p
= app_malloc(len
, "config filename buffer");
84 #ifndef OPENSSL_SYS_VMS
87 strcat(p
, OPENSSL_CONF
);
93 #ifndef OPENSSL_NO_TRACE
94 typedef struct tracedata_st
{
96 unsigned int ingroup
:1;
99 static size_t internal_trace_cb(const char *buf
, size_t cnt
,
100 int category
, int cmd
, void *vdata
)
103 tracedata
*trace_data
= vdata
;
104 char buffer
[256], *hex
;
105 CRYPTO_THREAD_ID tid
;
108 case OSSL_TRACE_CTRL_BEGIN
:
109 if (!ossl_assert(!trace_data
->ingroup
))
111 trace_data
->ingroup
= 1;
113 tid
= CRYPTO_THREAD_get_current_id();
114 hex
= OPENSSL_buf2hexstr((const unsigned char *)&tid
, sizeof(tid
));
115 BIO_snprintf(buffer
, sizeof(buffer
), "TRACE[%s]:%s: ",
116 hex
, OSSL_trace_get_category_name(category
));
118 BIO_ctrl(trace_data
->bio
, PREFIX_CTRL_SET_PREFIX
,
119 strlen(buffer
), buffer
);
121 case OSSL_TRACE_CTRL_WRITE
:
122 if (!ossl_assert(trace_data
->ingroup
))
125 ret
= BIO_write(trace_data
->bio
, buf
, cnt
);
127 case OSSL_TRACE_CTRL_END
:
128 if (!ossl_assert(trace_data
->ingroup
))
130 trace_data
->ingroup
= 0;
132 BIO_ctrl(trace_data
->bio
, PREFIX_CTRL_SET_PREFIX
, 0, NULL
);
137 return ret
< 0 ? 0 : ret
;
140 DEFINE_STACK_OF(tracedata
)
141 static STACK_OF(tracedata
) *trace_data_stack
;
143 static void tracedata_free(tracedata
*data
)
145 BIO_free_all(data
->bio
);
149 static STACK_OF(tracedata
) *trace_data_stack
;
151 static void cleanup_trace(void)
153 sk_tracedata_pop_free(trace_data_stack
, tracedata_free
);
156 static void setup_trace_category(int category
)
159 tracedata
*trace_data
;
161 if (OSSL_trace_enabled(category
))
164 channel
= BIO_push(BIO_new(apps_bf_prefix()),
165 dup_bio_err(FORMAT_TEXT
));
166 trace_data
= OPENSSL_zalloc(sizeof(*trace_data
));
168 if (trace_data
== NULL
169 || (trace_data
->bio
= channel
) == NULL
170 || OSSL_trace_set_callback(category
, internal_trace_cb
,
172 || sk_tracedata_push(trace_data_stack
, trace_data
) == 0) {
175 "warning: unable to setup trace callback for category '%s'.\n",
176 OSSL_trace_get_category_name(category
));
178 OSSL_trace_set_callback(category
, NULL
, NULL
);
179 BIO_free_all(channel
);
183 static void setup_trace(const char *str
)
188 * We add this handler as early as possible to ensure it's executed
189 * as late as possible, i.e. after the TRACE code has done its cleanup
190 * (which happens last in OPENSSL_cleanup).
192 atexit(cleanup_trace
);
194 trace_data_stack
= sk_tracedata_new_null();
195 val
= OPENSSL_strdup(str
);
201 for (valp
= val
; (item
= strtok(valp
, ",")) != NULL
; valp
= NULL
) {
202 int category
= OSSL_trace_get_category_num(item
);
204 if (category
== OSSL_TRACE_CATEGORY_ALL
) {
205 while (++category
< OSSL_TRACE_CATEGORY_NUM
)
206 setup_trace_category(category
);
208 } else if (category
> 0) {
209 setup_trace_category(category
);
212 "warning: unknown trace category: '%s'.\n", item
);
219 #endif /* OPENSSL_NO_TRACE */
221 int main(int argc
, char *argv
[])
224 LHASH_OF(FUNCTION
) *prog
= NULL
;
229 int first
, n
, i
, ret
= 0;
234 /* Set up some of the environment. */
235 default_config_file
= make_config_name();
236 bio_in
= dup_bio_in(FORMAT_TEXT
);
237 bio_out
= dup_bio_out(FORMAT_TEXT
);
238 bio_err
= dup_bio_err(FORMAT_TEXT
);
240 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
241 argv
= copy_argv(&argc
, argv
);
242 #elif defined(_WIN32)
244 * Replace argv[] with UTF-8 encoded strings.
246 win32_utf8argv(&argc
, &argv
);
250 * We use the prefix method to get the trace output we want. Since some
251 * trace outputs happen with OPENSSL_cleanup(), which is run automatically
252 * after exit(), we need to destroy the prefix method as late as possible.
254 atexit(destroy_prefix_method
);
256 #ifndef OPENSSL_NO_TRACE
257 setup_trace(getenv("OPENSSL_TRACE"));
260 p
= getenv("OPENSSL_DEBUG_MEMORY");
261 if (p
!= NULL
&& strcmp(p
, "on") == 0)
262 CRYPTO_set_mem_debug(1);
263 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON
);
265 if (getenv("OPENSSL_FIPS")) {
266 BIO_printf(bio_err
, "FIPS mode not supported.\n");
270 if (!apps_startup()) {
272 "FATAL: Startup failure (dev note: apps_startup() failed)\n");
273 ERR_print_errors(bio_err
);
279 pname
= opt_progname(argv
[0]);
281 /* first check the program name */
283 fp
= lh_FUNCTION_retrieve(prog
, &f
);
286 ret
= fp
->func(argc
, argv
);
290 /* If there is stuff on the command line, run with that. */
294 ret
= do_cmd(prog
, argc
, argv
);
300 /* ok, lets enter interactive mode */
303 /* Read a line, continue reading if line ends with \ */
304 for (p
= buf
, n
= sizeof(buf
), i
= 0, first
= 1; n
> 0; first
= 0) {
305 prompt
= first
? "OpenSSL> " : "> ";
308 fputs(prompt
, stdout
);
310 if (!fgets(p
, n
, stdin
))
317 if (p
[i
- 2] != '\\')
324 extern char *readline(const char *);
325 extern void add_history(const char *cp
);
328 text
= readline(prompt
);
334 if (text
[i
- 1] != '\\') {
335 p
+= strlen(strcpy(p
, text
));
342 p
+= strlen(strcpy(p
, text
));
349 if (!chopup_args(&arg
, buf
)) {
350 BIO_printf(bio_err
, "Can't parse (no memory?)\n");
354 ret
= do_cmd(prog
, arg
.argc
, arg
.argv
);
355 if (ret
== EXIT_THE_PROGRAM
) {
360 BIO_printf(bio_err
, "error in %s\n", arg
.argv
[0]);
361 (void)BIO_flush(bio_out
);
362 (void)BIO_flush(bio_err
);
366 OPENSSL_free(default_config_file
);
367 lh_FUNCTION_free(prog
);
368 OPENSSL_free(arg
.argv
);
372 BIO_free_all(bio_out
);
374 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
375 if (CRYPTO_mem_leaks(bio_err
) <= 0)
382 typedef enum HELP_CHOICE
{
383 OPT_hERR
= -1, OPT_hEOF
= 0, OPT_hHELP
386 const OPTIONS help_options
[] = {
387 {OPT_HELP_STR
, 1, '-', "Usage: help [options]\n"},
388 {OPT_HELP_STR
, 1, '-', " help [command]\n"},
389 {"help", OPT_hHELP
, '-', "Display this summary"},
394 int help_main(int argc
, char **argv
)
403 prog
= opt_init(argc
, argv
, help_options
);
404 while ((o
= opt_next()) != OPT_hEOF
) {
408 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
411 opt_help(help_options
);
416 if (opt_num_rest() == 1) {
419 new_argv
[0] = opt_rest()[0];
420 new_argv
[1] = "--help";
422 return do_cmd(prog_init(), 2, new_argv
);
424 if (opt_num_rest() != 0) {
425 BIO_printf(bio_err
, "Usage: %s\n", prog
);
429 calculate_columns(functions
, &dc
);
430 BIO_printf(bio_err
, "Standard commands");
433 for (fp
= functions
; fp
->name
!= NULL
; fp
++) {
435 if (i
++ % dc
.columns
== 0) {
436 BIO_printf(bio_err
, "\n");
439 if (fp
->type
!= tp
) {
442 BIO_printf(bio_err
, "\n");
446 "\nMessage Digest commands (see the `dgst' command for more details)\n");
447 } else if (tp
== FT_cipher
) {
450 "\nCipher commands (see the `enc' command for more details)\n");
453 BIO_printf(bio_err
, "%-*s", dc
.width
, fp
->name
);
455 BIO_printf(bio_err
, "\n\n");
459 static int do_cmd(LHASH_OF(FUNCTION
) *prog
, int argc
, char *argv
[])
463 if (argc
<= 0 || argv
[0] == NULL
)
466 fp
= lh_FUNCTION_retrieve(prog
, &f
);
468 if (EVP_get_digestbyname(argv
[0])) {
472 } else if (EVP_get_cipherbyname(argv
[0])) {
479 return fp
->func(argc
, argv
);
481 if ((strncmp(argv
[0], "no-", 3)) == 0) {
483 * User is asking if foo is unsupported, by trying to "run" the
484 * no-foo command. Strange.
486 f
.name
= argv
[0] + 3;
487 if (lh_FUNCTION_retrieve(prog
, &f
) == NULL
) {
488 BIO_printf(bio_out
, "%s\n", argv
[0]);
491 BIO_printf(bio_out
, "%s\n", argv
[0] + 3);
494 if (strcmp(argv
[0], "quit") == 0 || strcmp(argv
[0], "q") == 0 ||
495 strcmp(argv
[0], "exit") == 0 || strcmp(argv
[0], "bye") == 0)
496 /* Special value to mean "exit the program. */
497 return EXIT_THE_PROGRAM
;
499 BIO_printf(bio_err
, "Invalid command '%s'; type \"help\" for a list.\n",
504 static int function_cmp(const FUNCTION
* a
, const FUNCTION
* b
)
506 return strncmp(a
->name
, b
->name
, 8);
509 static unsigned long function_hash(const FUNCTION
* a
)
511 return OPENSSL_LH_strhash(a
->name
);
514 static int SortFnByName(const void *_f1
, const void *_f2
)
516 const FUNCTION
*f1
= _f1
;
517 const FUNCTION
*f2
= _f2
;
519 if (f1
->type
!= f2
->type
)
520 return f1
->type
- f2
->type
;
521 return strcmp(f1
->name
, f2
->name
);
524 static LHASH_OF(FUNCTION
) *prog_init(void)
526 static LHASH_OF(FUNCTION
) *ret
= NULL
;
527 static int prog_inited
= 0;
536 /* Sort alphabetically within category. For nicer help displays. */
537 for (i
= 0, f
= functions
; f
->name
!= NULL
; ++f
, ++i
)
539 qsort(functions
, i
, sizeof(*functions
), SortFnByName
);
541 if ((ret
= lh_FUNCTION_new(function_hash
, function_cmp
)) == NULL
)
544 for (f
= functions
; f
->name
!= NULL
; f
++)
545 (void)lh_FUNCTION_insert(ret
, f
);