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