]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/openssl.c
rand: remove unimplemented librandom stub code
[thirdparty/openssl.git] / apps / openssl.c
1 /*
2 * Copyright 1995-2023 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 <stdlib.h>
12 #include "internal/common.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
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
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
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
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
154 static void tracedata_free(tracedata *data)
155 {
156 BIO_free_all(data->bio);
157 OPENSSL_free(data);
158 }
159
160 static void cleanup_trace(void)
161 {
162 sk_tracedata_pop_free(trace_data_stack, tracedata_free);
163 }
164
165 static void setup_trace_category(int category)
166 {
167 BIO *channel;
168 tracedata *trace_data;
169 BIO *bio = NULL;
170
171 if (OSSL_trace_enabled(category))
172 return;
173
174 bio = BIO_new(BIO_f_prefix());
175 channel = BIO_push(bio, dup_bio_err(FORMAT_TEXT));
176 trace_data = OPENSSL_zalloc(sizeof(*trace_data));
177
178 if (trace_data == NULL
179 || bio == NULL
180 || (trace_data->bio = channel) == NULL
181 || OSSL_trace_set_callback(category, internal_trace_cb,
182 trace_data) == 0
183 || sk_tracedata_push(trace_data_stack, trace_data) == 0) {
184
185 fprintf(stderr,
186 "warning: unable to setup trace callback for category '%s'.\n",
187 OSSL_trace_get_category_name(category));
188
189 OSSL_trace_set_callback(category, NULL, NULL);
190 BIO_free_all(channel);
191 }
192 }
193
194 static void setup_trace(const char *str)
195 {
196 char *val;
197
198 /*
199 * We add this handler as early as possible to ensure it's executed
200 * as late as possible, i.e. after the TRACE code has done its cleanup
201 * (which happens last in OPENSSL_cleanup).
202 */
203 atexit(cleanup_trace);
204
205 trace_data_stack = sk_tracedata_new_null();
206 val = OPENSSL_strdup(str);
207
208 if (val != NULL) {
209 char *valp = val;
210 char *item;
211
212 for (valp = val; (item = strtok(valp, ",")) != NULL; valp = NULL) {
213 int category = OSSL_trace_get_category_num(item);
214
215 if (category == OSSL_TRACE_CATEGORY_ALL) {
216 while (++category < OSSL_TRACE_CATEGORY_NUM)
217 setup_trace_category(category);
218 break;
219 } else if (category > 0) {
220 setup_trace_category(category);
221 } else {
222 fprintf(stderr,
223 "warning: unknown trace category: '%s'.\n", item);
224 }
225 }
226 }
227
228 OPENSSL_free(val);
229 }
230 #endif /* OPENSSL_NO_TRACE */
231
232 static char *help_argv[] = { "help", NULL };
233 static char *version_argv[] = { "version", NULL };
234
235 int main(int argc, char *argv[])
236 {
237 FUNCTION f, *fp;
238 LHASH_OF(FUNCTION) *prog = NULL;
239 char *pname;
240 const char *fname;
241 ARGS arg;
242 int global_help = 0;
243 int global_version = 0;
244 int ret = 0;
245
246 arg.argv = NULL;
247 arg.size = 0;
248
249 /* Set up some of the environment. */
250 bio_in = dup_bio_in(FORMAT_TEXT);
251 bio_out = dup_bio_out(FORMAT_TEXT);
252 bio_err = dup_bio_err(FORMAT_TEXT);
253
254 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
255 argv = copy_argv(&argc, argv);
256 #elif defined(_WIN32)
257 /* Replace argv[] with UTF-8 encoded strings. */
258 win32_utf8argv(&argc, &argv);
259 #endif
260
261 #ifndef OPENSSL_NO_TRACE
262 setup_trace(getenv("OPENSSL_TRACE"));
263 #endif
264
265 if ((fname = "apps_startup", !apps_startup())
266 || (fname = "prog_init", (prog = prog_init()) == NULL)) {
267 BIO_printf(bio_err,
268 "FATAL: Startup failure (dev note: %s()) for %s\n",
269 fname, argv[0]);
270 ERR_print_errors(bio_err);
271 ret = 1;
272 goto end;
273 }
274 pname = opt_progname(argv[0]);
275
276 default_config_file = CONF_get1_default_config_file();
277 if (default_config_file == NULL)
278 app_bail_out("%s: could not get default config file\n", pname);
279
280 /* first check the program name */
281 f.name = pname;
282 fp = lh_FUNCTION_retrieve(prog, &f);
283 if (fp == NULL) {
284 /* We assume we've been called as 'openssl ...' */
285 global_help = argc > 1
286 && (strcmp(argv[1], "-help") == 0 || strcmp(argv[1], "--help") == 0
287 || strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--h") == 0);
288 global_version = argc > 1
289 && (strcmp(argv[1], "-version") == 0 || strcmp(argv[1], "--version") == 0
290 || strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--v") == 0);
291
292 argc--;
293 argv++;
294 opt_appname(argc == 1 || global_help ? "help" : global_version ? "version" : argv[0]);
295 } else {
296 argv[0] = pname;
297 }
298
299 /*
300 * If there's no command, assume "help". If there's an override for help
301 * or version run those, otherwise run the command given.
302 */
303 ret = (argc == 0) || global_help
304 ? do_cmd(prog, 1, help_argv)
305 : global_version
306 ? do_cmd(prog, 1, version_argv)
307 : do_cmd(prog, argc, argv);
308
309 end:
310 OPENSSL_free(default_config_file);
311 lh_FUNCTION_free(prog);
312 OPENSSL_free(arg.argv);
313 if (!app_RAND_write())
314 ret = EXIT_FAILURE;
315
316 BIO_free(bio_in);
317 BIO_free_all(bio_out);
318 apps_shutdown();
319 BIO_free_all(bio_err);
320 EXIT(ret);
321 }
322
323 typedef enum HELP_CHOICE {
324 OPT_hERR = -1, OPT_hEOF = 0, OPT_hHELP
325 } HELP_CHOICE;
326
327 const OPTIONS help_options[] = {
328 {OPT_HELP_STR, 1, '-', "Usage: help [options] [command]\n"},
329
330 OPT_SECTION("General"),
331 {"help", OPT_hHELP, '-', "Display this summary"},
332
333 OPT_PARAMETERS(),
334 {"command", 0, 0, "Name of command to display help (optional)"},
335 {NULL}
336 };
337
338 int help_main(int argc, char **argv)
339 {
340 FUNCTION *fp;
341 int i, nl;
342 FUNC_TYPE tp;
343 char *prog;
344 HELP_CHOICE o;
345 DISPLAY_COLUMNS dc;
346 char *new_argv[3];
347
348 prog = opt_init(argc, argv, help_options);
349 while ((o = opt_next()) != OPT_hEOF) {
350 switch (o) {
351 case OPT_hERR:
352 case OPT_hEOF:
353 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
354 return 1;
355 case OPT_hHELP:
356 opt_help(help_options);
357 return 0;
358 }
359 }
360
361 /* One optional argument, the command to get help for. */
362 if (opt_num_rest() == 1) {
363 new_argv[0] = opt_rest()[0];
364 new_argv[1] = "--help";
365 new_argv[2] = NULL;
366 return do_cmd(prog_init(), 2, new_argv);
367 }
368 if (!opt_check_rest_arg(NULL)) {
369 BIO_printf(bio_err, "Usage: %s\n", prog);
370 return 1;
371 }
372
373 calculate_columns(functions, &dc);
374 BIO_printf(bio_err, "%s:\n\nStandard commands", prog);
375 i = 0;
376 tp = FT_none;
377 for (fp = functions; fp->name != NULL; fp++) {
378 nl = 0;
379 if (i++ % dc.columns == 0) {
380 BIO_printf(bio_err, "\n");
381 nl = 1;
382 }
383 if (fp->type != tp) {
384 tp = fp->type;
385 if (!nl)
386 BIO_printf(bio_err, "\n");
387 if (tp == FT_md) {
388 i = 1;
389 BIO_printf(bio_err,
390 "\nMessage Digest commands (see the `dgst' command for more details)\n");
391 } else if (tp == FT_cipher) {
392 i = 1;
393 BIO_printf(bio_err,
394 "\nCipher commands (see the `enc' command for more details)\n");
395 }
396 }
397 BIO_printf(bio_err, "%-*s", dc.width, fp->name);
398 }
399 BIO_printf(bio_err, "\n\n");
400 return 0;
401 }
402
403 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
404 {
405 FUNCTION f, *fp;
406
407 if (argc <= 0 || argv[0] == NULL)
408 return 0;
409 memset(&f, 0, sizeof(f));
410 f.name = argv[0];
411 fp = lh_FUNCTION_retrieve(prog, &f);
412 if (fp == NULL) {
413 if (EVP_get_digestbyname(argv[0])) {
414 f.type = FT_md;
415 f.func = dgst_main;
416 fp = &f;
417 } else if (EVP_get_cipherbyname(argv[0])) {
418 f.type = FT_cipher;
419 f.func = enc_main;
420 fp = &f;
421 }
422 }
423 if (fp != NULL) {
424 if (fp->deprecated_alternative != NULL)
425 warn_deprecated(fp);
426 return fp->func(argc, argv);
427 }
428 f.name = argv[0];
429 if (CHECK_AND_SKIP_PREFIX(f.name, "no-")) {
430 /*
431 * User is asking if foo is unsupported, by trying to "run" the
432 * no-foo command. Strange.
433 */
434 if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
435 BIO_printf(bio_out, "%s\n", argv[0]);
436 return 0;
437 }
438 BIO_printf(bio_out, "%s\n", argv[0] + 3);
439 return 1;
440 }
441
442 BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
443 argv[0]);
444 return 1;
445 }
446
447 static int function_cmp(const FUNCTION *a, const FUNCTION *b)
448 {
449 return strncmp(a->name, b->name, 8);
450 }
451
452 static unsigned long function_hash(const FUNCTION *a)
453 {
454 return OPENSSL_LH_strhash(a->name);
455 }
456
457 static int SortFnByName(const void *_f1, const void *_f2)
458 {
459 const FUNCTION *f1 = _f1;
460 const FUNCTION *f2 = _f2;
461
462 if (f1->type != f2->type)
463 return f1->type - f2->type;
464 return strcmp(f1->name, f2->name);
465 }
466
467 static LHASH_OF(FUNCTION) *prog_init(void)
468 {
469 static LHASH_OF(FUNCTION) *ret = NULL;
470 static int prog_inited = 0;
471 FUNCTION *f;
472 size_t i;
473
474 if (prog_inited)
475 return ret;
476
477 prog_inited = 1;
478
479 /* Sort alphabetically within category. For nicer help displays. */
480 for (i = 0, f = functions; f->name != NULL; ++f, ++i)
481 ;
482 qsort(functions, i, sizeof(*functions), SortFnByName);
483
484 if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
485 return NULL;
486
487 for (f = functions; f->name != NULL; f++)
488 (void)lh_FUNCTION_insert(ret, f);
489 return ret;
490 }