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