openssl.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /* apps/openssl.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * openssl-core@openssl.org.
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * (eay@cryptsoft.com). This product includes software written by Tim
  108. * Hudson (tjh@cryptsoft.com).
  109. *
  110. */
  111. #include <stdio.h>
  112. #include <string.h>
  113. #include <stdlib.h>
  114. #define OPENSSL_C /* tells apps.h to use complete
  115. * apps_startup() */
  116. #include "apps.h"
  117. #include <openssl/bio.h>
  118. #include <openssl/crypto.h>
  119. #include <openssl/lhash.h>
  120. #include <openssl/conf.h>
  121. #include <openssl/x509.h>
  122. #include <openssl/pem.h>
  123. #include <openssl/ssl.h>
  124. #ifndef OPENSSL_NO_ENGINE
  125. # include <openssl/engine.h>
  126. #endif
  127. #define USE_SOCKETS /* needed for the _O_BINARY defs in the MS
  128. * world */
  129. #include "progs.h"
  130. #include "s_apps.h"
  131. #include <openssl/err.h>
  132. /*
  133. * The LHASH callbacks ("hash" & "cmp") have been replaced by functions with
  134. * the base prototypes (we cast each variable inside the function to the
  135. * required type of "FUNCTION*"). This removes the necessity for
  136. * macro-generated wrapper functions.
  137. */
  138. /* static unsigned long MS_CALLBACK hash(FUNCTION *a); */
  139. static unsigned long MS_CALLBACK hash(const void *a_void);
  140. /* static int MS_CALLBACK cmp(FUNCTION *a,FUNCTION *b); */
  141. static int MS_CALLBACK cmp(const void *a_void, const void *b_void);
  142. static LHASH *prog_init(void);
  143. static int do_cmd(LHASH *prog, int argc, char *argv[]);
  144. char *default_config_file = NULL;
  145. /* Make sure there is only one when MONOLITH is defined */
  146. #ifdef MONOLITH
  147. CONF *config = NULL;
  148. BIO *bio_err = NULL;
  149. int in_FIPS_mode = 0;
  150. #endif
  151. static void lock_dbg_cb(int mode, int type, const char *file, int line)
  152. {
  153. static int modes[CRYPTO_NUM_LOCKS]; /* = {0, 0, ... } */
  154. const char *errstr = NULL;
  155. int rw;
  156. rw = mode & (CRYPTO_READ | CRYPTO_WRITE);
  157. if (!((rw == CRYPTO_READ) || (rw == CRYPTO_WRITE))) {
  158. errstr = "invalid mode";
  159. goto err;
  160. }
  161. if (type < 0 || type >= CRYPTO_NUM_LOCKS) {
  162. errstr = "type out of bounds";
  163. goto err;
  164. }
  165. if (mode & CRYPTO_LOCK) {
  166. if (modes[type]) {
  167. errstr = "already locked";
  168. /*
  169. * must not happen in a single-threaded program (would deadlock)
  170. */
  171. goto err;
  172. }
  173. modes[type] = rw;
  174. } else if (mode & CRYPTO_UNLOCK) {
  175. if (!modes[type]) {
  176. errstr = "not locked";
  177. goto err;
  178. }
  179. if (modes[type] != rw) {
  180. errstr = (rw == CRYPTO_READ) ?
  181. "CRYPTO_r_unlock on write lock" :
  182. "CRYPTO_w_unlock on read lock";
  183. }
  184. modes[type] = 0;
  185. } else {
  186. errstr = "invalid mode";
  187. goto err;
  188. }
  189. err:
  190. if (errstr) {
  191. /* we cannot use bio_err here */
  192. fprintf(stderr,
  193. "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
  194. errstr, mode, type, file, line);
  195. }
  196. }
  197. int main(int Argc, char *Argv[])
  198. {
  199. ARGS arg;
  200. #define PROG_NAME_SIZE 39
  201. char pname[PROG_NAME_SIZE + 1];
  202. FUNCTION f, *fp;
  203. MS_STATIC const char *prompt;
  204. MS_STATIC char buf[1024];
  205. char *to_free = NULL;
  206. int n, i, ret = 0;
  207. int argc;
  208. char **argv, *p;
  209. LHASH *prog = NULL;
  210. long errline;
  211. arg.data = NULL;
  212. arg.count = 0;
  213. in_FIPS_mode = 0;
  214. if (getenv("OPENSSL_FIPS")) {
  215. #ifdef OPENSSL_FIPS
  216. if (!FIPS_mode_set(1)) {
  217. ERR_load_crypto_strings();
  218. ERR_print_errors(BIO_new_fp(stderr, BIO_NOCLOSE));
  219. EXIT(1);
  220. }
  221. in_FIPS_mode = 1;
  222. #else
  223. fprintf(stderr, "FIPS mode not supported.\n");
  224. EXIT(1);
  225. #endif
  226. }
  227. if (bio_err == NULL)
  228. if ((bio_err = BIO_new(BIO_s_file())) != NULL)
  229. BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  230. if (getenv("OPENSSL_DEBUG_MEMORY") != NULL) { /* if not defined, use
  231. * compiled-in library
  232. * defaults */
  233. if (!(0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))) {
  234. CRYPTO_malloc_debug_init();
  235. CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
  236. } else {
  237. /* OPENSSL_DEBUG_MEMORY=off */
  238. CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
  239. }
  240. }
  241. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  242. #if 0
  243. if (getenv("OPENSSL_DEBUG_LOCKING") != NULL)
  244. #endif
  245. {
  246. CRYPTO_set_locking_callback(lock_dbg_cb);
  247. }
  248. apps_startup();
  249. /* Lets load up our environment a little */
  250. p = getenv("OPENSSL_CONF");
  251. if (p == NULL)
  252. p = getenv("SSLEAY_CONF");
  253. if (p == NULL)
  254. p = to_free = make_config_name();
  255. default_config_file = p;
  256. config = NCONF_new(NULL);
  257. i = NCONF_load(config, p, &errline);
  258. if (i == 0) {
  259. NCONF_free(config);
  260. config = NULL;
  261. ERR_clear_error();
  262. }
  263. prog = prog_init();
  264. /* first check the program name */
  265. program_name(Argv[0], pname, sizeof pname);
  266. f.name = pname;
  267. fp = (FUNCTION *) lh_retrieve(prog, &f);
  268. if (fp != NULL) {
  269. Argv[0] = pname;
  270. ret = fp->func(Argc, Argv);
  271. goto end;
  272. }
  273. /*
  274. * ok, now check that there are not arguments, if there are, run with
  275. * them, shifting the ssleay off the front
  276. */
  277. if (Argc != 1) {
  278. Argc--;
  279. Argv++;
  280. ret = do_cmd(prog, Argc, Argv);
  281. if (ret < 0)
  282. ret = 0;
  283. goto end;
  284. }
  285. /* ok, lets enter the old 'OpenSSL>' mode */
  286. for (;;) {
  287. ret = 0;
  288. p = buf;
  289. n = sizeof buf;
  290. i = 0;
  291. for (;;) {
  292. p[0] = '\0';
  293. if (i++)
  294. prompt = ">";
  295. else
  296. prompt = "OpenSSL> ";
  297. fputs(prompt, stdout);
  298. fflush(stdout);
  299. if (!fgets(p, n, stdin))
  300. goto end;
  301. if (p[0] == '\0')
  302. goto end;
  303. i = strlen(p);
  304. if (i <= 1)
  305. break;
  306. if (p[i - 2] != '\\')
  307. break;
  308. i -= 2;
  309. p += i;
  310. n -= i;
  311. }
  312. if (!chopup_args(&arg, buf, &argc, &argv))
  313. break;
  314. ret = do_cmd(prog, argc, argv);
  315. if (ret < 0) {
  316. ret = 0;
  317. goto end;
  318. }
  319. if (ret != 0)
  320. BIO_printf(bio_err, "error in %s\n", argv[0]);
  321. (void)BIO_flush(bio_err);
  322. }
  323. BIO_printf(bio_err, "bad exit\n");
  324. ret = 1;
  325. end:
  326. if (to_free)
  327. OPENSSL_free(to_free);
  328. if (config != NULL) {
  329. NCONF_free(config);
  330. config = NULL;
  331. }
  332. if (prog != NULL)
  333. lh_free(prog);
  334. if (arg.data != NULL)
  335. OPENSSL_free(arg.data);
  336. apps_shutdown();
  337. CRYPTO_mem_leaks(bio_err);
  338. if (bio_err != NULL) {
  339. BIO_free(bio_err);
  340. bio_err = NULL;
  341. }
  342. OPENSSL_EXIT(ret);
  343. }
  344. #define LIST_STANDARD_COMMANDS "list-standard-commands"
  345. #define LIST_MESSAGE_DIGEST_COMMANDS "list-message-digest-commands"
  346. #define LIST_CIPHER_COMMANDS "list-cipher-commands"
  347. static int do_cmd(LHASH *prog, int argc, char *argv[])
  348. {
  349. FUNCTION f, *fp;
  350. int i, ret = 1, tp, nl;
  351. if ((argc <= 0) || (argv[0] == NULL)) {
  352. ret = 0;
  353. goto end;
  354. }
  355. f.name = argv[0];
  356. fp = (FUNCTION *) lh_retrieve(prog, &f);
  357. if (fp != NULL) {
  358. ret = fp->func(argc, argv);
  359. } else if ((strncmp(argv[0], "no-", 3)) == 0) {
  360. BIO *bio_stdout = BIO_new_fp(stdout, BIO_NOCLOSE);
  361. #ifdef OPENSSL_SYS_VMS
  362. {
  363. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  364. bio_stdout = BIO_push(tmpbio, bio_stdout);
  365. }
  366. #endif
  367. f.name = argv[0] + 3;
  368. ret = (lh_retrieve(prog, &f) != NULL);
  369. if (!ret)
  370. BIO_printf(bio_stdout, "%s\n", argv[0]);
  371. else
  372. BIO_printf(bio_stdout, "%s\n", argv[0] + 3);
  373. BIO_free_all(bio_stdout);
  374. goto end;
  375. } else if ((strcmp(argv[0], "quit") == 0) ||
  376. (strcmp(argv[0], "q") == 0) ||
  377. (strcmp(argv[0], "exit") == 0) ||
  378. (strcmp(argv[0], "bye") == 0)) {
  379. ret = -1;
  380. goto end;
  381. } else if ((strcmp(argv[0], LIST_STANDARD_COMMANDS) == 0) ||
  382. (strcmp(argv[0], LIST_MESSAGE_DIGEST_COMMANDS) == 0) ||
  383. (strcmp(argv[0], LIST_CIPHER_COMMANDS) == 0)) {
  384. int list_type;
  385. BIO *bio_stdout;
  386. if (strcmp(argv[0], LIST_STANDARD_COMMANDS) == 0)
  387. list_type = FUNC_TYPE_GENERAL;
  388. else if (strcmp(argv[0], LIST_MESSAGE_DIGEST_COMMANDS) == 0)
  389. list_type = FUNC_TYPE_MD;
  390. else /* strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0 */
  391. list_type = FUNC_TYPE_CIPHER;
  392. bio_stdout = BIO_new_fp(stdout, BIO_NOCLOSE);
  393. #ifdef OPENSSL_SYS_VMS
  394. {
  395. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  396. bio_stdout = BIO_push(tmpbio, bio_stdout);
  397. }
  398. #endif
  399. for (fp = functions; fp->name != NULL; fp++)
  400. if (fp->type == list_type)
  401. BIO_printf(bio_stdout, "%s\n", fp->name);
  402. BIO_free_all(bio_stdout);
  403. ret = 0;
  404. goto end;
  405. } else {
  406. BIO_printf(bio_err, "openssl:Error: '%s' is an invalid command.\n",
  407. argv[0]);
  408. BIO_printf(bio_err, "\nStandard commands");
  409. i = 0;
  410. tp = 0;
  411. for (fp = functions; fp->name != NULL; fp++) {
  412. nl = 0;
  413. #ifdef OPENSSL_NO_CAMELLIA
  414. if (((i++) % 5) == 0)
  415. #else
  416. if (((i++) % 4) == 0)
  417. #endif
  418. {
  419. BIO_printf(bio_err, "\n");
  420. nl = 1;
  421. }
  422. if (fp->type != tp) {
  423. tp = fp->type;
  424. if (!nl)
  425. BIO_printf(bio_err, "\n");
  426. if (tp == FUNC_TYPE_MD) {
  427. i = 1;
  428. BIO_printf(bio_err,
  429. "\nMessage Digest commands (see the `dgst' command for more details)\n");
  430. } else if (tp == FUNC_TYPE_CIPHER) {
  431. i = 1;
  432. BIO_printf(bio_err,
  433. "\nCipher commands (see the `enc' command for more details)\n");
  434. }
  435. }
  436. #ifdef OPENSSL_NO_CAMELLIA
  437. BIO_printf(bio_err, "%-15s", fp->name);
  438. #else
  439. BIO_printf(bio_err, "%-18s", fp->name);
  440. #endif
  441. }
  442. BIO_printf(bio_err, "\n\n");
  443. ret = 0;
  444. }
  445. end:
  446. return (ret);
  447. }
  448. static int SortFnByName(const void *_f1, const void *_f2)
  449. {
  450. const FUNCTION *f1 = _f1;
  451. const FUNCTION *f2 = _f2;
  452. if (f1->type != f2->type)
  453. return f1->type - f2->type;
  454. return strcmp(f1->name, f2->name);
  455. }
  456. static LHASH *prog_init(void)
  457. {
  458. LHASH *ret;
  459. FUNCTION *f;
  460. size_t i;
  461. /* Purely so it looks nice when the user hits ? */
  462. for (i = 0, f = functions; f->name != NULL; ++f, ++i) ;
  463. qsort(functions, i, sizeof *functions, SortFnByName);
  464. if ((ret = lh_new(hash, cmp)) == NULL)
  465. return (NULL);
  466. for (f = functions; f->name != NULL; f++)
  467. lh_insert(ret, f);
  468. return (ret);
  469. }
  470. /* static int MS_CALLBACK cmp(FUNCTION *a, FUNCTION *b) */
  471. static int MS_CALLBACK cmp(const void *a_void, const void *b_void)
  472. {
  473. return (strncmp(((const FUNCTION *)a_void)->name,
  474. ((const FUNCTION *)b_void)->name, 8));
  475. }
  476. /* static unsigned long MS_CALLBACK hash(FUNCTION *a) */
  477. static unsigned long MS_CALLBACK hash(const void *a_void)
  478. {
  479. return (lh_strhash(((const FUNCTION *)a_void)->name));
  480. }