openssl.c 13 KB

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