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