Annotation of src/usr.bin/openssl/openssl.c, Revision 1.6
1.6 ! jsing 1: /* $OpenBSD: openssl.c,v 1.5 2015/08/22 16:36:05 jsing Exp $ */
1.1 jsing 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:
112: #include <err.h>
113: #include <signal.h>
114: #include <stdio.h>
115: #include <string.h>
116: #include <stdlib.h>
117:
118: #include "apps.h"
119:
120: #include <openssl/bio.h>
121: #include <openssl/conf.h>
122: #include <openssl/crypto.h>
123: #include <openssl/err.h>
124: #include <openssl/lhash.h>
125: #include <openssl/pem.h>
126: #include <openssl/ssl.h>
127: #include <openssl/x509.h>
128:
129: #ifndef OPENSSL_NO_ENGINE
130: #include <openssl/engine.h>
131: #endif
132:
133: #include "progs.h"
134: #include "s_apps.h"
1.5 jsing 135:
136: #define FUNC_TYPE_GENERAL 1
137: #define FUNC_TYPE_MD 2
138: #define FUNC_TYPE_CIPHER 3
139: #define FUNC_TYPE_PKEY 4
140: #define FUNC_TYPE_MD_ALG 5
141: #define FUNC_TYPE_CIPHER_ALG 6
142:
143: typedef struct {
144: int type;
145: const char *name;
146: int (*func)(int argc, char **argv);
147: } FUNCTION;
148:
149: DECLARE_LHASH_OF(FUNCTION);
1.4 deraadt 150:
151: FUNCTION functions[] = {
152:
153: /* General functions. */
154: { FUNC_TYPE_GENERAL, "asn1parse", asn1parse_main },
155: { FUNC_TYPE_GENERAL, "ca", ca_main },
156: { FUNC_TYPE_GENERAL, "certhash", certhash_main },
157: { FUNC_TYPE_GENERAL, "ciphers", ciphers_main },
158: #ifndef OPENSSL_NO_CMS
159: { FUNC_TYPE_GENERAL, "cms", cms_main },
160: #endif
161: { FUNC_TYPE_GENERAL, "crl2pkcs7", crl2pkcs7_main },
162: { FUNC_TYPE_GENERAL, "crl", crl_main },
163: { FUNC_TYPE_GENERAL, "dgst", dgst_main },
164: { FUNC_TYPE_GENERAL, "enc", enc_main },
165: #ifndef OPENSSL_NO_ENGINE
166: { FUNC_TYPE_GENERAL, "engine", engine_main },
167: #endif
168: { FUNC_TYPE_GENERAL, "errstr", errstr_main },
169: { FUNC_TYPE_GENERAL, "genpkey", genpkey_main },
170: { FUNC_TYPE_GENERAL, "nseq", nseq_main },
171: #ifndef OPENSSL_NO_OCSP
172: { FUNC_TYPE_GENERAL, "ocsp", ocsp_main },
173: #endif
174: { FUNC_TYPE_GENERAL, "passwd", passwd_main },
175: { FUNC_TYPE_GENERAL, "pkcs7", pkcs7_main },
176: { FUNC_TYPE_GENERAL, "pkcs8", pkcs8_main },
177: #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_SHA1)
178: { FUNC_TYPE_GENERAL, "pkcs12", pkcs12_main },
179: #endif
180: { FUNC_TYPE_GENERAL, "pkey", pkey_main },
181: { FUNC_TYPE_GENERAL, "pkeyparam", pkeyparam_main },
182: { FUNC_TYPE_GENERAL, "pkeyutl", pkeyutl_main },
183: { FUNC_TYPE_GENERAL, "prime", prime_main },
184: { FUNC_TYPE_GENERAL, "rand", rand_main },
185: { FUNC_TYPE_GENERAL, "req", req_main },
186: { FUNC_TYPE_GENERAL, "s_client", s_client_main },
187: { FUNC_TYPE_GENERAL, "s_server", s_server_main },
188: { FUNC_TYPE_GENERAL, "s_time", s_time_main },
189: { FUNC_TYPE_GENERAL, "sess_id", sess_id_main },
190: { FUNC_TYPE_GENERAL, "smime", smime_main },
191: #ifndef OPENSSL_NO_SPEED
192: { FUNC_TYPE_GENERAL, "speed", speed_main },
193: #endif
194: { FUNC_TYPE_GENERAL, "spkac", spkac_main },
195: { FUNC_TYPE_GENERAL, "ts", ts_main },
196: { FUNC_TYPE_GENERAL, "verify", verify_main },
197: { FUNC_TYPE_GENERAL, "version", version_main },
198: { FUNC_TYPE_GENERAL, "x509", x509_main },
199:
200: #ifndef OPENSSL_NO_DH
201: { FUNC_TYPE_GENERAL, "dh", dh_main },
202: { FUNC_TYPE_GENERAL, "dhparam", dhparam_main },
203: { FUNC_TYPE_GENERAL, "gendh", gendh_main },
204: #endif
205: #ifndef OPENSSL_NO_DSA
206: { FUNC_TYPE_GENERAL, "dsa", dsa_main },
207: { FUNC_TYPE_GENERAL, "dsaparam", dsaparam_main },
208: { FUNC_TYPE_GENERAL, "gendsa", gendsa_main },
209: #endif
210: #ifndef OPENSSL_NO_EC
211: { FUNC_TYPE_GENERAL, "ec", ec_main },
212: { FUNC_TYPE_GENERAL, "ecparam", ecparam_main },
213: #endif
214: #ifndef OPENSSL_NO_RSA
215: { FUNC_TYPE_GENERAL, "genrsa", genrsa_main },
216: { FUNC_TYPE_GENERAL, "rsa", rsa_main },
217: { FUNC_TYPE_GENERAL, "rsautl", rsautl_main },
218: #endif
219:
220: /* Message Digests. */
221: #ifndef OPENSSL_NO_GOST
222: { FUNC_TYPE_MD, "gost-mac", dgst_main },
223: { FUNC_TYPE_MD, "md_gost94", dgst_main },
224: { FUNC_TYPE_MD, "streebog256", dgst_main },
225: { FUNC_TYPE_MD, "streebog512", dgst_main },
226: #endif
227: #ifndef OPENSSL_NO_MD4
228: { FUNC_TYPE_MD, "md4", dgst_main },
229: #endif
230: #ifndef OPENSSL_NO_MD5
231: { FUNC_TYPE_MD, "md5", dgst_main },
232: #endif
233: #ifndef OPENSSL_NO_RIPEMD160
234: { FUNC_TYPE_MD, "ripemd160", dgst_main },
235: #endif
236: #ifndef OPENSSL_NO_SHA
237: { FUNC_TYPE_MD, "sha", dgst_main },
238: #endif
239: #ifndef OPENSSL_NO_SHA1
240: { FUNC_TYPE_MD, "sha1", dgst_main },
241: #endif
242: #ifndef OPENSSL_NO_SHA224
243: { FUNC_TYPE_MD, "sha224", dgst_main },
244: #endif
245: #ifndef OPENSSL_NO_SHA256
246: { FUNC_TYPE_MD, "sha256", dgst_main },
247: #endif
248: #ifndef OPENSSL_NO_SHA384
249: { FUNC_TYPE_MD, "sha384", dgst_main },
250: #endif
251: #ifndef OPENSSL_NO_SHA512
252: { FUNC_TYPE_MD, "sha512", dgst_main },
253: #endif
254: #ifndef OPENSSL_NO_WHIRLPOOL
255: { FUNC_TYPE_MD, "whirlpool", dgst_main },
256: #endif
257:
258: /* Ciphers. */
259: { FUNC_TYPE_CIPHER, "base64", enc_main },
260: #ifndef OPENSSL_NO_AES
261: { FUNC_TYPE_CIPHER, "aes-128-cbc", enc_main },
262: { FUNC_TYPE_CIPHER, "aes-128-ecb", enc_main },
263: { FUNC_TYPE_CIPHER, "aes-192-cbc", enc_main },
264: { FUNC_TYPE_CIPHER, "aes-192-ecb", enc_main },
265: { FUNC_TYPE_CIPHER, "aes-256-cbc", enc_main },
266: { FUNC_TYPE_CIPHER, "aes-256-ecb", enc_main },
267: #endif
268: #ifndef OPENSSL_NO_BF
269: { FUNC_TYPE_CIPHER, "bf", enc_main },
270: { FUNC_TYPE_CIPHER, "bf-cbc", enc_main },
271: { FUNC_TYPE_CIPHER, "bf-ecb", enc_main },
272: { FUNC_TYPE_CIPHER, "bf-cfb", enc_main },
273: { FUNC_TYPE_CIPHER, "bf-ofb", enc_main },
274: #endif
275: #ifndef OPENSSL_NO_CAMELLIA
276: { FUNC_TYPE_CIPHER, "camellia-128-cbc", enc_main },
277: { FUNC_TYPE_CIPHER, "camellia-128-ecb", enc_main },
278: { FUNC_TYPE_CIPHER, "camellia-192-cbc", enc_main },
279: { FUNC_TYPE_CIPHER, "camellia-192-ecb", enc_main },
280: { FUNC_TYPE_CIPHER, "camellia-256-cbc", enc_main },
281: { FUNC_TYPE_CIPHER, "camellia-256-ecb", enc_main },
282: #endif
283: #ifndef OPENSSL_NO_CAST
284: { FUNC_TYPE_CIPHER, "cast", enc_main },
285: { FUNC_TYPE_CIPHER, "cast5-cbc", enc_main },
286: { FUNC_TYPE_CIPHER, "cast5-ecb", enc_main },
287: { FUNC_TYPE_CIPHER, "cast5-cfb", enc_main },
288: { FUNC_TYPE_CIPHER, "cast5-ofb", enc_main },
289: { FUNC_TYPE_CIPHER, "cast-cbc", enc_main },
290: #endif
291: #ifndef OPENSSL_NO_CHACHA
292: { FUNC_TYPE_CIPHER, "chacha", enc_main },
293: #endif
294: #ifndef OPENSSL_NO_DES
295: { FUNC_TYPE_CIPHER, "des", enc_main },
296: { FUNC_TYPE_CIPHER, "des3", enc_main },
297: { FUNC_TYPE_CIPHER, "desx", enc_main },
298: { FUNC_TYPE_CIPHER, "des-ecb", enc_main },
299: { FUNC_TYPE_CIPHER, "des-ede", enc_main },
300: { FUNC_TYPE_CIPHER, "des-ede3", enc_main },
301: { FUNC_TYPE_CIPHER, "des-cbc", enc_main },
302: { FUNC_TYPE_CIPHER, "des-ede-cbc", enc_main },
303: { FUNC_TYPE_CIPHER, "des-ede3-cbc", enc_main },
304: { FUNC_TYPE_CIPHER, "des-cfb", enc_main },
305: { FUNC_TYPE_CIPHER, "des-ede-cfb", enc_main },
306: { FUNC_TYPE_CIPHER, "des-ede3-cfb", enc_main },
307: { FUNC_TYPE_CIPHER, "des-ofb", enc_main },
308: { FUNC_TYPE_CIPHER, "des-ede-ofb", enc_main },
309: { FUNC_TYPE_CIPHER, "des-ede3-ofb", enc_main },
310: #endif
311: #ifndef OPENSSL_NO_IDEA
312: { FUNC_TYPE_CIPHER, "idea", enc_main },
313: { FUNC_TYPE_CIPHER, "idea-cbc", enc_main },
314: { FUNC_TYPE_CIPHER, "idea-ecb", enc_main },
315: { FUNC_TYPE_CIPHER, "idea-cfb", enc_main },
316: { FUNC_TYPE_CIPHER, "idea-ofb", enc_main },
317: #endif
318: #ifndef OPENSSL_NO_RC2
319: { FUNC_TYPE_CIPHER, "rc2", enc_main },
320: { FUNC_TYPE_CIPHER, "rc2-cbc", enc_main },
321: { FUNC_TYPE_CIPHER, "rc2-ecb", enc_main },
322: { FUNC_TYPE_CIPHER, "rc2-cfb", enc_main },
323: { FUNC_TYPE_CIPHER, "rc2-ofb", enc_main },
324: { FUNC_TYPE_CIPHER, "rc2-64-cbc", enc_main },
325: { FUNC_TYPE_CIPHER, "rc2-40-cbc", enc_main },
326: #endif
327: #ifndef OPENSSL_NO_RC4
328: { FUNC_TYPE_CIPHER, "rc4", enc_main },
329: { FUNC_TYPE_CIPHER, "rc4-40", enc_main },
330: #endif
331: #ifndef OPENSSL_NO_RC5
332: { FUNC_TYPE_CIPHER, "rc5", enc_main },
333: { FUNC_TYPE_CIPHER, "rc5-cbc", enc_main },
334: { FUNC_TYPE_CIPHER, "rc5-ecb", enc_main },
335: { FUNC_TYPE_CIPHER, "rc5-cfb", enc_main },
336: { FUNC_TYPE_CIPHER, "rc5-ofb", enc_main },
337: #endif
338: #ifdef ZLIB
339: { FUNC_TYPE_CIPHER, "zlib", enc_main },
340: #endif
341:
342: { 0, NULL, NULL }
343: };
1.1 jsing 344:
345: static void openssl_startup(void);
346: static void openssl_shutdown(void);
347:
348: /* The LHASH callbacks ("hash" & "cmp") have been replaced by functions with the
349: * base prototypes (we cast each variable inside the function to the required
350: * type of "FUNCTION*"). This removes the necessity for macro-generated wrapper
351: * functions. */
352:
353: static LHASH_OF(FUNCTION) *prog_init(void);
354: static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]);
355: static void list_pkey(BIO * out);
356: static void list_cipher(BIO * out);
357: static void list_md(BIO * out);
358: char *default_config_file = NULL;
359:
360: CONF *config = NULL;
361: BIO *bio_err = NULL;
362:
363: static void
364: lock_dbg_cb(int mode, int type, const char *file, int line)
365: {
366: static int modes[CRYPTO_NUM_LOCKS]; /* = {0, 0, ... } */
367: const char *errstr = NULL;
368: int rw;
369:
370: rw = mode & (CRYPTO_READ | CRYPTO_WRITE);
371: if (!((rw == CRYPTO_READ) || (rw == CRYPTO_WRITE))) {
372: errstr = "invalid mode";
373: goto err;
374: }
375: if (type < 0 || type >= CRYPTO_NUM_LOCKS) {
376: errstr = "type out of bounds";
377: goto err;
378: }
379: if (mode & CRYPTO_LOCK) {
380: if (modes[type]) {
381: errstr = "already locked";
382: /*
383: * must not happen in a single-threaded program
384: * (would deadlock)
385: */
386: goto err;
387: }
388: modes[type] = rw;
389: } else if (mode & CRYPTO_UNLOCK) {
390: if (!modes[type]) {
391: errstr = "not locked";
392: goto err;
393: }
394: if (modes[type] != rw) {
395: errstr = (rw == CRYPTO_READ) ?
396: "CRYPTO_r_unlock on write lock" :
397: "CRYPTO_w_unlock on read lock";
398: }
399: modes[type] = 0;
400: } else {
401: errstr = "invalid mode";
402: goto err;
403: }
404:
405: err:
406: if (errstr) {
407: /* we cannot use bio_err here */
408: fprintf(stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
409: errstr, mode, type, file, line);
410: }
411: }
412:
413: static void
414: openssl_startup(void)
415: {
416: signal(SIGPIPE, SIG_IGN);
417:
418: OpenSSL_add_all_algorithms();
419: SSL_library_init();
420: SSL_load_error_strings();
421:
422: #ifndef OPENSSL_NO_ENGINE
423: ENGINE_load_builtin_engines();
424: #endif
425:
426: setup_ui_method();
427: }
428:
429: static void
430: openssl_shutdown(void)
431: {
432: CONF_modules_unload(1);
433: destroy_ui_method();
434: OBJ_cleanup();
435: EVP_cleanup();
436:
437: #ifndef OPENSSL_NO_ENGINE
438: ENGINE_cleanup();
439: #endif
440:
441: CRYPTO_cleanup_all_ex_data();
442: ERR_remove_thread_state(NULL);
443: ERR_free_strings();
444: }
445:
446: int
447: main(int argc, char **argv)
448: {
449: ARGS arg;
450: #define PROG_NAME_SIZE 39
451: char pname[PROG_NAME_SIZE + 1];
452: FUNCTION f, *fp;
453: const char *prompt;
454: char buf[1024];
455: char *to_free = NULL;
456: int n, i, ret = 0;
457: char *p;
458: LHASH_OF(FUNCTION) * prog = NULL;
459: long errline;
460:
461: arg.data = NULL;
462: arg.count = 0;
463:
464: bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
465: if (bio_err == NULL) {
466: fprintf(stderr, "openssl: failed to initialise bio_err\n");
1.3 bcook 467: exit(1);
468: }
469:
470: if (BIO_sock_init() != 1) {
471: BIO_printf(bio_err, "BIO_sock_init failed\n");
1.1 jsing 472: exit(1);
473: }
474:
475: CRYPTO_set_locking_callback(lock_dbg_cb);
476:
477: openssl_startup();
478:
479: /* Lets load up our environment a little */
480: p = getenv("OPENSSL_CONF");
481: if (p == NULL)
482: p = getenv("SSLEAY_CONF");
483: if (p == NULL) {
484: p = to_free = make_config_name();
485: if (p == NULL) {
486: BIO_printf(bio_err, "error making config file name\n");
487: goto end;
488: }
489: }
490:
491: default_config_file = p;
492:
493: config = NCONF_new(NULL);
494: i = NCONF_load(config, p, &errline);
495: if (i == 0) {
496: if (ERR_GET_REASON(ERR_peek_last_error()) ==
497: CONF_R_NO_SUCH_FILE) {
498: BIO_printf(bio_err,
499: "WARNING: can't open config file: %s\n", p);
500: ERR_clear_error();
501: NCONF_free(config);
502: config = NULL;
503: } else {
504: ERR_print_errors(bio_err);
505: NCONF_free(config);
506: exit(1);
507: }
508: }
509:
510: if (!load_config(bio_err, NULL)) {
511: BIO_printf(bio_err, "failed to load configuration\n");
512: goto end;
513: }
514:
515: prog = prog_init();
516:
517: /* first check the program name */
518: program_name(argv[0], pname, sizeof pname);
519:
520: f.name = pname;
521: fp = lh_FUNCTION_retrieve(prog, &f);
522: if (fp != NULL) {
523: argv[0] = pname;
524: ret = fp->func(argc, argv);
525: goto end;
526: }
527: /*
528: * ok, now check that there are not arguments, if there are, run with
529: * them, shifting the ssleay off the front
530: */
531: if (argc != 1) {
532: argc--;
533: argv++;
534: ret = do_cmd(prog, argc, argv);
535: if (ret < 0)
536: ret = 0;
537: goto end;
538: }
539: /* ok, lets enter the old 'OpenSSL>' mode */
540:
541: for (;;) {
542: ret = 0;
543: p = buf;
544: n = sizeof buf;
545: i = 0;
546: for (;;) {
547: p[0] = '\0';
548: if (i++)
549: prompt = ">";
550: else
551: prompt = "OpenSSL> ";
552: fputs(prompt, stdout);
553: fflush(stdout);
554: if (!fgets(p, n, stdin))
555: goto end;
556: if (p[0] == '\0')
557: goto end;
558: i = strlen(p);
559: if (i <= 1)
560: break;
561: if (p[i - 2] != '\\')
562: break;
563: i -= 2;
564: p += i;
565: n -= i;
566: }
567: if (!chopup_args(&arg, buf, &argc, &argv))
568: break;
569:
570: ret = do_cmd(prog, argc, argv);
571: if (ret < 0) {
572: ret = 0;
573: goto end;
574: }
575: if (ret != 0)
576: BIO_printf(bio_err, "error in %s\n", argv[0]);
577: (void) BIO_flush(bio_err);
578: }
579: BIO_printf(bio_err, "bad exit\n");
580: ret = 1;
581:
582: end:
583: free(to_free);
584:
585: if (config != NULL) {
586: NCONF_free(config);
587: config = NULL;
588: }
589: if (prog != NULL)
590: lh_FUNCTION_free(prog);
591: free(arg.data);
592:
593: openssl_shutdown();
594:
595: if (bio_err != NULL) {
596: BIO_free(bio_err);
597: bio_err = NULL;
598: }
599: return (ret);
600: }
601:
602: #define LIST_STANDARD_COMMANDS "list-standard-commands"
603: #define LIST_MESSAGE_DIGEST_COMMANDS "list-message-digest-commands"
604: #define LIST_MESSAGE_DIGEST_ALGORITHMS "list-message-digest-algorithms"
605: #define LIST_CIPHER_COMMANDS "list-cipher-commands"
606: #define LIST_CIPHER_ALGORITHMS "list-cipher-algorithms"
607: #define LIST_PUBLIC_KEY_ALGORITHMS "list-public-key-algorithms"
608:
609:
610: static int
611: do_cmd(LHASH_OF(FUNCTION) * prog, int argc, char *argv[])
612: {
613: FUNCTION f, *fp;
614: int i, ret = 1, tp, nl;
615:
616: if ((argc <= 0) || (argv[0] == NULL)) {
617: ret = 0;
618: goto end;
619: }
620: f.name = argv[0];
621: fp = lh_FUNCTION_retrieve(prog, &f);
622: if (fp == NULL) {
623: if (EVP_get_digestbyname(argv[0])) {
624: f.type = FUNC_TYPE_MD;
625: f.func = dgst_main;
626: fp = &f;
627: } else if (EVP_get_cipherbyname(argv[0])) {
628: f.type = FUNC_TYPE_CIPHER;
629: f.func = enc_main;
630: fp = &f;
631: }
632: }
633: if (fp != NULL) {
634: ret = fp->func(argc, argv);
635: } else if ((strncmp(argv[0], "no-", 3)) == 0) {
636: BIO *bio_stdout = BIO_new_fp(stdout, BIO_NOCLOSE);
637: f.name = argv[0] + 3;
638: ret = (lh_FUNCTION_retrieve(prog, &f) != NULL);
639: if (!ret)
640: BIO_printf(bio_stdout, "%s\n", argv[0]);
641: else
642: BIO_printf(bio_stdout, "%s\n", argv[0] + 3);
643: BIO_free_all(bio_stdout);
644: goto end;
645: } else if ((strcmp(argv[0], "quit") == 0) ||
646: (strcmp(argv[0], "q") == 0) ||
647: (strcmp(argv[0], "exit") == 0) ||
648: (strcmp(argv[0], "bye") == 0)) {
649: ret = -1;
650: goto end;
651: } else if ((strcmp(argv[0], LIST_STANDARD_COMMANDS) == 0) ||
652: (strcmp(argv[0], LIST_MESSAGE_DIGEST_COMMANDS) == 0) ||
653: (strcmp(argv[0], LIST_MESSAGE_DIGEST_ALGORITHMS) == 0) ||
654: (strcmp(argv[0], LIST_CIPHER_COMMANDS) == 0) ||
655: (strcmp(argv[0], LIST_CIPHER_ALGORITHMS) == 0) ||
656: (strcmp(argv[0], LIST_PUBLIC_KEY_ALGORITHMS) == 0)) {
657: int list_type;
658: BIO *bio_stdout;
659:
660: if (strcmp(argv[0], LIST_STANDARD_COMMANDS) == 0)
661: list_type = FUNC_TYPE_GENERAL;
662: else if (strcmp(argv[0], LIST_MESSAGE_DIGEST_COMMANDS) == 0)
663: list_type = FUNC_TYPE_MD;
664: else if (strcmp(argv[0], LIST_MESSAGE_DIGEST_ALGORITHMS) == 0)
665: list_type = FUNC_TYPE_MD_ALG;
666: else if (strcmp(argv[0], LIST_PUBLIC_KEY_ALGORITHMS) == 0)
667: list_type = FUNC_TYPE_PKEY;
668: else if (strcmp(argv[0], LIST_CIPHER_ALGORITHMS) == 0)
669: list_type = FUNC_TYPE_CIPHER_ALG;
670: else /* strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0 */
671: list_type = FUNC_TYPE_CIPHER;
672: bio_stdout = BIO_new_fp(stdout, BIO_NOCLOSE);
673:
674: if (list_type == FUNC_TYPE_PKEY)
675: list_pkey(bio_stdout);
676: if (list_type == FUNC_TYPE_MD_ALG)
677: list_md(bio_stdout);
678: if (list_type == FUNC_TYPE_CIPHER_ALG)
679: list_cipher(bio_stdout);
680: else {
681: for (fp = functions; fp->name != NULL; fp++)
682: if (fp->type == list_type)
683: BIO_printf(bio_stdout, "%s\n",
684: fp->name);
685: }
686: BIO_free_all(bio_stdout);
687: ret = 0;
688: goto end;
689: } else {
690: BIO_printf(bio_err,
691: "openssl:Error: '%s' is an invalid command.\n",
692: argv[0]);
693: BIO_printf(bio_err, "\nStandard commands");
694: i = 0;
695: tp = 0;
696: for (fp = functions; fp->name != NULL; fp++) {
697: nl = 0;
698: #ifdef OPENSSL_NO_CAMELLIA
699: if (((i++) % 5) == 0)
700: #else
701: if (((i++) % 4) == 0)
702: #endif
703: {
704: BIO_printf(bio_err, "\n");
705: nl = 1;
706: }
707: if (fp->type != tp) {
708: tp = fp->type;
709: if (!nl)
710: BIO_printf(bio_err, "\n");
711: if (tp == FUNC_TYPE_MD) {
712: i = 1;
713: BIO_printf(bio_err,
714: "\nMessage Digest commands (see the `dgst' command for more details)\n");
715: } else if (tp == FUNC_TYPE_CIPHER) {
716: i = 1;
717: BIO_printf(bio_err, "\nCipher commands (see the `enc' command for more details)\n");
718: }
719: }
720: #ifdef OPENSSL_NO_CAMELLIA
721: BIO_printf(bio_err, "%-15s", fp->name);
722: #else
723: BIO_printf(bio_err, "%-18s", fp->name);
724: #endif
725: }
726: BIO_printf(bio_err, "\n\n");
727: ret = 0;
728: }
729: end:
730: return (ret);
731: }
732:
733: static int
734: SortFnByName(const void *_f1, const void *_f2)
735: {
736: const FUNCTION *f1 = _f1;
737: const FUNCTION *f2 = _f2;
738:
739: if (f1->type != f2->type)
740: return f1->type - f2->type;
741: return strcmp(f1->name, f2->name);
742: }
743:
744: static void
745: list_pkey(BIO * out)
746: {
747: int i;
748:
749: for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
750: const EVP_PKEY_ASN1_METHOD *ameth;
751: int pkey_id, pkey_base_id, pkey_flags;
752: const char *pinfo, *pem_str;
753: ameth = EVP_PKEY_asn1_get0(i);
754: EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
755: &pinfo, &pem_str, ameth);
756: if (pkey_flags & ASN1_PKEY_ALIAS) {
757: BIO_printf(out, "Name: %s\n",
758: OBJ_nid2ln(pkey_id));
759: BIO_printf(out, "\tType: Alias to %s\n",
760: OBJ_nid2ln(pkey_base_id));
761: } else {
762: BIO_printf(out, "Name: %s\n", pinfo);
763: BIO_printf(out, "\tType: %s Algorithm\n",
764: pkey_flags & ASN1_PKEY_DYNAMIC ?
765: "External" : "Builtin");
766: BIO_printf(out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
767: if (pem_str == NULL)
768: pem_str = "(none)";
769: BIO_printf(out, "\tPEM string: %s\n", pem_str);
770: }
771:
772: }
773: }
774:
775: static void
776: list_cipher_fn(const EVP_CIPHER * c, const char *from, const char *to,
777: void *arg)
778: {
779: if (c)
780: BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
781: else {
782: if (!from)
783: from = "<undefined>";
784: if (!to)
785: to = "<undefined>";
786: BIO_printf(arg, "%s => %s\n", from, to);
787: }
788: }
789:
790: static void
791: list_cipher(BIO * out)
792: {
793: EVP_CIPHER_do_all_sorted(list_cipher_fn, out);
794: }
795:
796: static void
797: list_md_fn(const EVP_MD * m, const char *from, const char *to, void *arg)
798: {
799: if (m)
800: BIO_printf(arg, "%s\n", EVP_MD_name(m));
801: else {
802: if (!from)
803: from = "<undefined>";
804: if (!to)
805: to = "<undefined>";
806: BIO_printf(arg, "%s => %s\n", from, to);
807: }
808: }
809:
810: static void
811: list_md(BIO * out)
812: {
813: EVP_MD_do_all_sorted(list_md_fn, out);
814: }
815:
816: static int
817: function_cmp(const FUNCTION * a, const FUNCTION * b)
818: {
819: return strncmp(a->name, b->name, 8);
820: }
821:
822: static IMPLEMENT_LHASH_COMP_FN(function, FUNCTION)
823:
824: static unsigned long
825: function_hash(const FUNCTION * a)
826: {
827: return lh_strhash(a->name);
828: }
829:
830: static IMPLEMENT_LHASH_HASH_FN(function, FUNCTION)
831:
832: static LHASH_OF(FUNCTION) *
833: prog_init(void)
834: {
835: LHASH_OF(FUNCTION) * ret;
836: FUNCTION *f;
837: size_t i;
838:
839: /* Purely so it looks nice when the user hits ? */
840: for (i = 0, f = functions; f->name != NULL; ++f, ++i)
841: ;
842: qsort(functions, i, sizeof *functions, SortFnByName);
843:
844: if ((ret = lh_FUNCTION_new()) == NULL)
845: return (NULL);
846:
847: for (f = functions; f->name != NULL; f++)
848: (void) lh_FUNCTION_insert(ret, f);
849: return (ret);
850: }