openssl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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-2000 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 apps_startup() */
  115. #include <openssl/bio.h>
  116. #include <openssl/crypto.h>
  117. #include <openssl/lhash.h>
  118. #include <openssl/conf.h>
  119. #include <openssl/x509.h>
  120. #include <openssl/pem.h>
  121. #include <openssl/ssl.h>
  122. #define USE_SOCKETS /* needed for the _O_BINARY defs in the MS world */
  123. #include "apps.h"
  124. #include "progs.h"
  125. #include "s_apps.h"
  126. #include <openssl/err.h>
  127. /* The LHASH callbacks ("hash" & "cmp") have been replaced by functions with the
  128. * base prototypes (we cast each variable inside the function to the required
  129. * type of "FUNCTION*"). This removes the necessity for macro-generated wrapper
  130. * functions. */
  131. /* static unsigned long MS_CALLBACK hash(FUNCTION *a); */
  132. static unsigned long MS_CALLBACK hash(const void *a_void);
  133. /* static int MS_CALLBACK cmp(FUNCTION *a,FUNCTION *b); */
  134. static int MS_CALLBACK cmp(const void *a_void,const void *b_void);
  135. static LHASH *prog_init(void );
  136. static int do_cmd(LHASH *prog,int argc,char *argv[]);
  137. LHASH *config=NULL;
  138. char *default_config_file=NULL;
  139. /* Make sure there is only one when MONOLITH is defined */
  140. #ifdef MONOLITH
  141. BIO *bio_err=NULL;
  142. #endif
  143. static void lock_dbg_cb(int mode, int type, const char *file, int line)
  144. {
  145. static int modes[CRYPTO_NUM_LOCKS]; /* = {0, 0, ... } */
  146. const char *errstr = NULL;
  147. int rw;
  148. rw = mode & (CRYPTO_READ|CRYPTO_WRITE);
  149. if (!((rw == CRYPTO_READ) || (rw == CRYPTO_WRITE)))
  150. {
  151. errstr = "invalid mode";
  152. goto err;
  153. }
  154. if (type < 0 || type > CRYPTO_NUM_LOCKS)
  155. {
  156. errstr = "type out of bounds";
  157. goto err;
  158. }
  159. if (mode & CRYPTO_LOCK)
  160. {
  161. if (modes[type])
  162. {
  163. errstr = "already locked";
  164. /* must not happen in a single-threaded program
  165. * (would deadlock) */
  166. goto err;
  167. }
  168. modes[type] = rw;
  169. }
  170. else if (mode & CRYPTO_UNLOCK)
  171. {
  172. if (!modes[type])
  173. {
  174. errstr = "not locked";
  175. goto err;
  176. }
  177. if (modes[type] != rw)
  178. {
  179. errstr = (rw == CRYPTO_READ) ?
  180. "CRYPTO_r_unlock on write lock" :
  181. "CRYPTO_w_unlock on read lock";
  182. }
  183. modes[type] = 0;
  184. }
  185. else
  186. {
  187. errstr = "invalid mode";
  188. goto err;
  189. }
  190. err:
  191. if (errstr)
  192. {
  193. /* we cannot use bio_err here */
  194. fprintf(stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
  195. errstr, mode, type, file, line);
  196. }
  197. }
  198. int main(int Argc, char *Argv[])
  199. {
  200. ARGS arg;
  201. #define PROG_NAME_SIZE 16
  202. char pname[PROG_NAME_SIZE];
  203. FUNCTION f,*fp;
  204. MS_STATIC char *prompt,buf[1024],config_name[256];
  205. int n,i,ret=0;
  206. int argc;
  207. char **argv,*p;
  208. LHASH *prog=NULL;
  209. long errline;
  210. arg.data=NULL;
  211. arg.count=0;
  212. if (getenv("OPENSSL_DEBUG_MEMORY") != NULL)
  213. CRYPTO_malloc_debug_init();
  214. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  215. #if 0
  216. if (getenv("OPENSSL_DEBUG_LOCKING") != NULL)
  217. #endif
  218. {
  219. CRYPTO_set_locking_callback(lock_dbg_cb);
  220. }
  221. apps_startup();
  222. if (bio_err == NULL)
  223. if ((bio_err=BIO_new(BIO_s_file())) != NULL)
  224. BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
  225. ERR_load_crypto_strings();
  226. ENGINE_load_builtin_engines();
  227. /* Lets load up our environment a little */
  228. p=getenv("OPENSSL_CONF");
  229. if (p == NULL)
  230. p=getenv("SSLEAY_CONF");
  231. if (p == NULL)
  232. {
  233. strcpy(config_name,X509_get_default_cert_area());
  234. #ifndef OPENSSL_SYS_VMS
  235. strcat(config_name,"/");
  236. #endif
  237. strcat(config_name,OPENSSL_CONF);
  238. p=config_name;
  239. }
  240. default_config_file=p;
  241. config=CONF_load(config,p,&errline);
  242. if (config == NULL) ERR_clear_error();
  243. prog=prog_init();
  244. /* first check the program name */
  245. program_name(Argv[0],pname,PROG_NAME_SIZE);
  246. f.name=pname;
  247. fp=(FUNCTION *)lh_retrieve(prog,&f);
  248. if (fp != NULL)
  249. {
  250. Argv[0]=pname;
  251. ret=fp->func(Argc,Argv);
  252. goto end;
  253. }
  254. /* ok, now check that there are not arguments, if there are,
  255. * run with them, shifting the ssleay off the front */
  256. if (Argc != 1)
  257. {
  258. Argc--;
  259. Argv++;
  260. ret=do_cmd(prog,Argc,Argv);
  261. if (ret < 0) ret=0;
  262. goto end;
  263. }
  264. /* ok, lets enter the old 'OpenSSL>' mode */
  265. for (;;)
  266. {
  267. ret=0;
  268. p=buf;
  269. n=1024;
  270. i=0;
  271. for (;;)
  272. {
  273. p[0]='\0';
  274. if (i++)
  275. prompt=">";
  276. else prompt="OpenSSL> ";
  277. fputs(prompt,stdout);
  278. fflush(stdout);
  279. fgets(p,n,stdin);
  280. if (p[0] == '\0') goto end;
  281. i=strlen(p);
  282. if (i <= 1) break;
  283. if (p[i-2] != '\\') break;
  284. i-=2;
  285. p+=i;
  286. n-=i;
  287. }
  288. if (!chopup_args(&arg,buf,&argc,&argv)) break;
  289. ret=do_cmd(prog,argc,argv);
  290. if (ret < 0)
  291. {
  292. ret=0;
  293. goto end;
  294. }
  295. if (ret != 0)
  296. BIO_printf(bio_err,"error in %s\n",argv[0]);
  297. (void)BIO_flush(bio_err);
  298. }
  299. BIO_printf(bio_err,"bad exit\n");
  300. ret=1;
  301. end:
  302. if (config != NULL)
  303. {
  304. CONF_free(config);
  305. config=NULL;
  306. }
  307. if (prog != NULL) lh_free(prog);
  308. if (arg.data != NULL) OPENSSL_free(arg.data);
  309. ERR_remove_state(0);
  310. EVP_cleanup();
  311. ERR_free_strings();
  312. CRYPTO_mem_leaks(bio_err);
  313. if (bio_err != NULL)
  314. {
  315. BIO_free(bio_err);
  316. bio_err=NULL;
  317. }
  318. EXIT(ret);
  319. }
  320. #define LIST_STANDARD_COMMANDS "list-standard-commands"
  321. #define LIST_MESSAGE_DIGEST_COMMANDS "list-message-digest-commands"
  322. #define LIST_CIPHER_COMMANDS "list-cipher-commands"
  323. static int do_cmd(LHASH *prog, int argc, char *argv[])
  324. {
  325. FUNCTION f,*fp;
  326. int i,ret=1,tp,nl;
  327. if ((argc <= 0) || (argv[0] == NULL))
  328. { ret=0; goto end; }
  329. f.name=argv[0];
  330. fp=(FUNCTION *)lh_retrieve(prog,&f);
  331. if (fp != NULL)
  332. {
  333. ret=fp->func(argc,argv);
  334. }
  335. else if ((strncmp(argv[0],"no-",3)) == 0)
  336. {
  337. BIO *bio_stdout = BIO_new_fp(stdout,BIO_NOCLOSE);
  338. #ifdef OPENSSL_SYS_VMS
  339. {
  340. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  341. bio_stdout = BIO_push(tmpbio, bio_stdout);
  342. }
  343. #endif
  344. f.name=argv[0]+3;
  345. ret = (lh_retrieve(prog,&f) != NULL);
  346. if (!ret)
  347. BIO_printf(bio_stdout, "%s\n", argv[0]);
  348. else
  349. BIO_printf(bio_stdout, "%s\n", argv[0]+3);
  350. BIO_free_all(bio_stdout);
  351. goto end;
  352. }
  353. else if ((strcmp(argv[0],"quit") == 0) ||
  354. (strcmp(argv[0],"q") == 0) ||
  355. (strcmp(argv[0],"exit") == 0) ||
  356. (strcmp(argv[0],"bye") == 0))
  357. {
  358. ret= -1;
  359. goto end;
  360. }
  361. else if ((strcmp(argv[0],LIST_STANDARD_COMMANDS) == 0) ||
  362. (strcmp(argv[0],LIST_MESSAGE_DIGEST_COMMANDS) == 0) ||
  363. (strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0))
  364. {
  365. int list_type;
  366. BIO *bio_stdout;
  367. if (strcmp(argv[0],LIST_STANDARD_COMMANDS) == 0)
  368. list_type = FUNC_TYPE_GENERAL;
  369. else if (strcmp(argv[0],LIST_MESSAGE_DIGEST_COMMANDS) == 0)
  370. list_type = FUNC_TYPE_MD;
  371. else /* strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0 */
  372. list_type = FUNC_TYPE_CIPHER;
  373. bio_stdout = BIO_new_fp(stdout,BIO_NOCLOSE);
  374. #ifdef OPENSSL_SYS_VMS
  375. {
  376. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  377. bio_stdout = BIO_push(tmpbio, bio_stdout);
  378. }
  379. #endif
  380. for (fp=functions; fp->name != NULL; fp++)
  381. if (fp->type == list_type)
  382. BIO_printf(bio_stdout, "%s\n", fp->name);
  383. BIO_free_all(bio_stdout);
  384. ret=0;
  385. goto end;
  386. }
  387. else
  388. {
  389. BIO_printf(bio_err,"openssl:Error: '%s' is an invalid command.\n",
  390. argv[0]);
  391. BIO_printf(bio_err, "\nStandard commands");
  392. i=0;
  393. tp=0;
  394. for (fp=functions; fp->name != NULL; fp++)
  395. {
  396. nl=0;
  397. if (((i++) % 5) == 0)
  398. {
  399. BIO_printf(bio_err,"\n");
  400. nl=1;
  401. }
  402. if (fp->type != tp)
  403. {
  404. tp=fp->type;
  405. if (!nl) BIO_printf(bio_err,"\n");
  406. if (tp == FUNC_TYPE_MD)
  407. {
  408. i=1;
  409. BIO_printf(bio_err,
  410. "\nMessage Digest commands (see the `dgst' command for more details)\n");
  411. }
  412. else if (tp == FUNC_TYPE_CIPHER)
  413. {
  414. i=1;
  415. BIO_printf(bio_err,"\nCipher commands (see the `enc' command for more details)\n");
  416. }
  417. }
  418. BIO_printf(bio_err,"%-15s",fp->name);
  419. }
  420. BIO_printf(bio_err,"\n\n");
  421. ret=0;
  422. }
  423. end:
  424. return(ret);
  425. }
  426. static int SortFnByName(const void *_f1,const void *_f2)
  427. {
  428. const FUNCTION *f1=_f1;
  429. const FUNCTION *f2=_f2;
  430. if(f1->type != f2->type)
  431. return f1->type-f2->type;
  432. return strcmp(f1->name,f2->name);
  433. }
  434. static LHASH *prog_init(void)
  435. {
  436. LHASH *ret;
  437. FUNCTION *f;
  438. int i;
  439. /* Purely so it looks nice when the user hits ? */
  440. for(i=0,f=functions ; f->name != NULL ; ++f,++i)
  441. ;
  442. qsort(functions,i,sizeof *functions,SortFnByName);
  443. if ((ret=lh_new(hash, cmp)) == NULL)
  444. return(NULL);
  445. for (f=functions; f->name != NULL; f++)
  446. lh_insert(ret,f);
  447. return(ret);
  448. }
  449. /* static int MS_CALLBACK cmp(FUNCTION *a, FUNCTION *b) */
  450. static int MS_CALLBACK cmp(const void *a_void, const void *b_void)
  451. {
  452. return(strncmp(((FUNCTION *)a_void)->name,
  453. ((FUNCTION *)b_void)->name,8));
  454. }
  455. /* static unsigned long MS_CALLBACK hash(FUNCTION *a) */
  456. static unsigned long MS_CALLBACK hash(const void *a_void)
  457. {
  458. return(lh_strhash(((FUNCTION *)a_void)->name));
  459. }