openssl.c 13 KB

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