Annotation of src/usr.bin/openssl/openssl.c, Revision 1.3
1.3 ! bcook 1: /* $OpenBSD: openssl.c,v 1.2 2014/10/22 13:54:03 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"
135:
136: static void openssl_startup(void);
137: static void openssl_shutdown(void);
138:
139: /* The LHASH callbacks ("hash" & "cmp") have been replaced by functions with the
140: * base prototypes (we cast each variable inside the function to the required
141: * type of "FUNCTION*"). This removes the necessity for macro-generated wrapper
142: * functions. */
143:
144: static LHASH_OF(FUNCTION) *prog_init(void);
145: static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]);
146: static void list_pkey(BIO * out);
147: static void list_cipher(BIO * out);
148: static void list_md(BIO * out);
149: char *default_config_file = NULL;
150:
151: CONF *config = NULL;
152: BIO *bio_err = NULL;
153:
154: static void
155: lock_dbg_cb(int mode, int type, const char *file, int line)
156: {
157: static int modes[CRYPTO_NUM_LOCKS]; /* = {0, 0, ... } */
158: const char *errstr = NULL;
159: int rw;
160:
161: rw = mode & (CRYPTO_READ | CRYPTO_WRITE);
162: if (!((rw == CRYPTO_READ) || (rw == CRYPTO_WRITE))) {
163: errstr = "invalid mode";
164: goto err;
165: }
166: if (type < 0 || type >= CRYPTO_NUM_LOCKS) {
167: errstr = "type out of bounds";
168: goto err;
169: }
170: if (mode & CRYPTO_LOCK) {
171: if (modes[type]) {
172: errstr = "already locked";
173: /*
174: * must not happen in a single-threaded program
175: * (would deadlock)
176: */
177: goto err;
178: }
179: modes[type] = rw;
180: } else if (mode & CRYPTO_UNLOCK) {
181: if (!modes[type]) {
182: errstr = "not locked";
183: goto err;
184: }
185: if (modes[type] != rw) {
186: errstr = (rw == CRYPTO_READ) ?
187: "CRYPTO_r_unlock on write lock" :
188: "CRYPTO_w_unlock on read lock";
189: }
190: modes[type] = 0;
191: } else {
192: errstr = "invalid mode";
193: goto err;
194: }
195:
196: err:
197: if (errstr) {
198: /* we cannot use bio_err here */
199: fprintf(stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
200: errstr, mode, type, file, line);
201: }
202: }
203:
204: static void
205: openssl_startup(void)
206: {
207: signal(SIGPIPE, SIG_IGN);
208:
209: CRYPTO_malloc_init();
210: OpenSSL_add_all_algorithms();
211: SSL_library_init();
212: SSL_load_error_strings();
213:
214: #ifndef OPENSSL_NO_ENGINE
215: ENGINE_load_builtin_engines();
216: #endif
217:
218: setup_ui_method();
219: }
220:
221: static void
222: openssl_shutdown(void)
223: {
224: CONF_modules_unload(1);
225: destroy_ui_method();
226: OBJ_cleanup();
227: EVP_cleanup();
228:
229: #ifndef OPENSSL_NO_ENGINE
230: ENGINE_cleanup();
231: #endif
232:
233: CRYPTO_cleanup_all_ex_data();
234: ERR_remove_thread_state(NULL);
235: ERR_free_strings();
236: }
237:
238: int
239: main(int argc, char **argv)
240: {
241: ARGS arg;
242: #define PROG_NAME_SIZE 39
243: char pname[PROG_NAME_SIZE + 1];
244: FUNCTION f, *fp;
245: const char *prompt;
246: char buf[1024];
247: char *to_free = NULL;
248: int n, i, ret = 0;
249: char *p;
250: LHASH_OF(FUNCTION) * prog = NULL;
251: long errline;
252:
253: arg.data = NULL;
254: arg.count = 0;
255:
256: bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
257: if (bio_err == NULL) {
258: fprintf(stderr, "openssl: failed to initialise bio_err\n");
1.3 ! bcook 259: exit(1);
! 260: }
! 261:
! 262: if (BIO_sock_init() != 1) {
! 263: BIO_printf(bio_err, "BIO_sock_init failed\n");
1.1 jsing 264: exit(1);
265: }
266:
267: CRYPTO_set_locking_callback(lock_dbg_cb);
268:
269: openssl_startup();
270:
271: /* Lets load up our environment a little */
272: p = getenv("OPENSSL_CONF");
273: if (p == NULL)
274: p = getenv("SSLEAY_CONF");
275: if (p == NULL) {
276: p = to_free = make_config_name();
277: if (p == NULL) {
278: BIO_printf(bio_err, "error making config file name\n");
279: goto end;
280: }
281: }
282:
283: default_config_file = p;
284:
285: config = NCONF_new(NULL);
286: i = NCONF_load(config, p, &errline);
287: if (i == 0) {
288: if (ERR_GET_REASON(ERR_peek_last_error()) ==
289: CONF_R_NO_SUCH_FILE) {
290: BIO_printf(bio_err,
291: "WARNING: can't open config file: %s\n", p);
292: ERR_clear_error();
293: NCONF_free(config);
294: config = NULL;
295: } else {
296: ERR_print_errors(bio_err);
297: NCONF_free(config);
298: exit(1);
299: }
300: }
301:
302: if (!load_config(bio_err, NULL)) {
303: BIO_printf(bio_err, "failed to load configuration\n");
304: goto end;
305: }
306:
307: prog = prog_init();
308:
309: /* first check the program name */
310: program_name(argv[0], pname, sizeof pname);
311:
312: f.name = pname;
313: fp = lh_FUNCTION_retrieve(prog, &f);
314: if (fp != NULL) {
315: argv[0] = pname;
316: ret = fp->func(argc, argv);
317: goto end;
318: }
319: /*
320: * ok, now check that there are not arguments, if there are, run with
321: * them, shifting the ssleay off the front
322: */
323: if (argc != 1) {
324: argc--;
325: argv++;
326: ret = do_cmd(prog, argc, argv);
327: if (ret < 0)
328: ret = 0;
329: goto end;
330: }
331: /* ok, lets enter the old 'OpenSSL>' mode */
332:
333: for (;;) {
334: ret = 0;
335: p = buf;
336: n = sizeof buf;
337: i = 0;
338: for (;;) {
339: p[0] = '\0';
340: if (i++)
341: prompt = ">";
342: else
343: prompt = "OpenSSL> ";
344: fputs(prompt, stdout);
345: fflush(stdout);
346: if (!fgets(p, n, stdin))
347: goto end;
348: if (p[0] == '\0')
349: goto end;
350: i = strlen(p);
351: if (i <= 1)
352: break;
353: if (p[i - 2] != '\\')
354: break;
355: i -= 2;
356: p += i;
357: n -= i;
358: }
359: if (!chopup_args(&arg, buf, &argc, &argv))
360: break;
361:
362: ret = do_cmd(prog, argc, argv);
363: if (ret < 0) {
364: ret = 0;
365: goto end;
366: }
367: if (ret != 0)
368: BIO_printf(bio_err, "error in %s\n", argv[0]);
369: (void) BIO_flush(bio_err);
370: }
371: BIO_printf(bio_err, "bad exit\n");
372: ret = 1;
373:
374: end:
375: free(to_free);
376:
377: if (config != NULL) {
378: NCONF_free(config);
379: config = NULL;
380: }
381: if (prog != NULL)
382: lh_FUNCTION_free(prog);
383: free(arg.data);
384:
385: openssl_shutdown();
386:
387: if (bio_err != NULL) {
388: BIO_free(bio_err);
389: bio_err = NULL;
390: }
391: return (ret);
392: }
393:
394: #define LIST_STANDARD_COMMANDS "list-standard-commands"
395: #define LIST_MESSAGE_DIGEST_COMMANDS "list-message-digest-commands"
396: #define LIST_MESSAGE_DIGEST_ALGORITHMS "list-message-digest-algorithms"
397: #define LIST_CIPHER_COMMANDS "list-cipher-commands"
398: #define LIST_CIPHER_ALGORITHMS "list-cipher-algorithms"
399: #define LIST_PUBLIC_KEY_ALGORITHMS "list-public-key-algorithms"
400:
401:
402: static int
403: do_cmd(LHASH_OF(FUNCTION) * prog, int argc, char *argv[])
404: {
405: FUNCTION f, *fp;
406: int i, ret = 1, tp, nl;
407:
408: if ((argc <= 0) || (argv[0] == NULL)) {
409: ret = 0;
410: goto end;
411: }
412: f.name = argv[0];
413: fp = lh_FUNCTION_retrieve(prog, &f);
414: if (fp == NULL) {
415: if (EVP_get_digestbyname(argv[0])) {
416: f.type = FUNC_TYPE_MD;
417: f.func = dgst_main;
418: fp = &f;
419: } else if (EVP_get_cipherbyname(argv[0])) {
420: f.type = FUNC_TYPE_CIPHER;
421: f.func = enc_main;
422: fp = &f;
423: }
424: }
425: if (fp != NULL) {
426: ret = fp->func(argc, argv);
427: } else if ((strncmp(argv[0], "no-", 3)) == 0) {
428: BIO *bio_stdout = BIO_new_fp(stdout, BIO_NOCLOSE);
429: f.name = argv[0] + 3;
430: ret = (lh_FUNCTION_retrieve(prog, &f) != NULL);
431: if (!ret)
432: BIO_printf(bio_stdout, "%s\n", argv[0]);
433: else
434: BIO_printf(bio_stdout, "%s\n", argv[0] + 3);
435: BIO_free_all(bio_stdout);
436: goto end;
437: } else if ((strcmp(argv[0], "quit") == 0) ||
438: (strcmp(argv[0], "q") == 0) ||
439: (strcmp(argv[0], "exit") == 0) ||
440: (strcmp(argv[0], "bye") == 0)) {
441: ret = -1;
442: goto end;
443: } else if ((strcmp(argv[0], LIST_STANDARD_COMMANDS) == 0) ||
444: (strcmp(argv[0], LIST_MESSAGE_DIGEST_COMMANDS) == 0) ||
445: (strcmp(argv[0], LIST_MESSAGE_DIGEST_ALGORITHMS) == 0) ||
446: (strcmp(argv[0], LIST_CIPHER_COMMANDS) == 0) ||
447: (strcmp(argv[0], LIST_CIPHER_ALGORITHMS) == 0) ||
448: (strcmp(argv[0], LIST_PUBLIC_KEY_ALGORITHMS) == 0)) {
449: int list_type;
450: BIO *bio_stdout;
451:
452: if (strcmp(argv[0], LIST_STANDARD_COMMANDS) == 0)
453: list_type = FUNC_TYPE_GENERAL;
454: else if (strcmp(argv[0], LIST_MESSAGE_DIGEST_COMMANDS) == 0)
455: list_type = FUNC_TYPE_MD;
456: else if (strcmp(argv[0], LIST_MESSAGE_DIGEST_ALGORITHMS) == 0)
457: list_type = FUNC_TYPE_MD_ALG;
458: else if (strcmp(argv[0], LIST_PUBLIC_KEY_ALGORITHMS) == 0)
459: list_type = FUNC_TYPE_PKEY;
460: else if (strcmp(argv[0], LIST_CIPHER_ALGORITHMS) == 0)
461: list_type = FUNC_TYPE_CIPHER_ALG;
462: else /* strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0 */
463: list_type = FUNC_TYPE_CIPHER;
464: bio_stdout = BIO_new_fp(stdout, BIO_NOCLOSE);
465:
466: if (list_type == FUNC_TYPE_PKEY)
467: list_pkey(bio_stdout);
468: if (list_type == FUNC_TYPE_MD_ALG)
469: list_md(bio_stdout);
470: if (list_type == FUNC_TYPE_CIPHER_ALG)
471: list_cipher(bio_stdout);
472: else {
473: for (fp = functions; fp->name != NULL; fp++)
474: if (fp->type == list_type)
475: BIO_printf(bio_stdout, "%s\n",
476: fp->name);
477: }
478: BIO_free_all(bio_stdout);
479: ret = 0;
480: goto end;
481: } else {
482: BIO_printf(bio_err,
483: "openssl:Error: '%s' is an invalid command.\n",
484: argv[0]);
485: BIO_printf(bio_err, "\nStandard commands");
486: i = 0;
487: tp = 0;
488: for (fp = functions; fp->name != NULL; fp++) {
489: nl = 0;
490: #ifdef OPENSSL_NO_CAMELLIA
491: if (((i++) % 5) == 0)
492: #else
493: if (((i++) % 4) == 0)
494: #endif
495: {
496: BIO_printf(bio_err, "\n");
497: nl = 1;
498: }
499: if (fp->type != tp) {
500: tp = fp->type;
501: if (!nl)
502: BIO_printf(bio_err, "\n");
503: if (tp == FUNC_TYPE_MD) {
504: i = 1;
505: BIO_printf(bio_err,
506: "\nMessage Digest commands (see the `dgst' command for more details)\n");
507: } else if (tp == FUNC_TYPE_CIPHER) {
508: i = 1;
509: BIO_printf(bio_err, "\nCipher commands (see the `enc' command for more details)\n");
510: }
511: }
512: #ifdef OPENSSL_NO_CAMELLIA
513: BIO_printf(bio_err, "%-15s", fp->name);
514: #else
515: BIO_printf(bio_err, "%-18s", fp->name);
516: #endif
517: }
518: BIO_printf(bio_err, "\n\n");
519: ret = 0;
520: }
521: end:
522: return (ret);
523: }
524:
525: static int
526: SortFnByName(const void *_f1, const void *_f2)
527: {
528: const FUNCTION *f1 = _f1;
529: const FUNCTION *f2 = _f2;
530:
531: if (f1->type != f2->type)
532: return f1->type - f2->type;
533: return strcmp(f1->name, f2->name);
534: }
535:
536: static void
537: list_pkey(BIO * out)
538: {
539: int i;
540:
541: for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
542: const EVP_PKEY_ASN1_METHOD *ameth;
543: int pkey_id, pkey_base_id, pkey_flags;
544: const char *pinfo, *pem_str;
545: ameth = EVP_PKEY_asn1_get0(i);
546: EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
547: &pinfo, &pem_str, ameth);
548: if (pkey_flags & ASN1_PKEY_ALIAS) {
549: BIO_printf(out, "Name: %s\n",
550: OBJ_nid2ln(pkey_id));
551: BIO_printf(out, "\tType: Alias to %s\n",
552: OBJ_nid2ln(pkey_base_id));
553: } else {
554: BIO_printf(out, "Name: %s\n", pinfo);
555: BIO_printf(out, "\tType: %s Algorithm\n",
556: pkey_flags & ASN1_PKEY_DYNAMIC ?
557: "External" : "Builtin");
558: BIO_printf(out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
559: if (pem_str == NULL)
560: pem_str = "(none)";
561: BIO_printf(out, "\tPEM string: %s\n", pem_str);
562: }
563:
564: }
565: }
566:
567: static void
568: list_cipher_fn(const EVP_CIPHER * c, const char *from, const char *to,
569: void *arg)
570: {
571: if (c)
572: BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
573: else {
574: if (!from)
575: from = "<undefined>";
576: if (!to)
577: to = "<undefined>";
578: BIO_printf(arg, "%s => %s\n", from, to);
579: }
580: }
581:
582: static void
583: list_cipher(BIO * out)
584: {
585: EVP_CIPHER_do_all_sorted(list_cipher_fn, out);
586: }
587:
588: static void
589: list_md_fn(const EVP_MD * m, const char *from, const char *to, void *arg)
590: {
591: if (m)
592: BIO_printf(arg, "%s\n", EVP_MD_name(m));
593: else {
594: if (!from)
595: from = "<undefined>";
596: if (!to)
597: to = "<undefined>";
598: BIO_printf(arg, "%s => %s\n", from, to);
599: }
600: }
601:
602: static void
603: list_md(BIO * out)
604: {
605: EVP_MD_do_all_sorted(list_md_fn, out);
606: }
607:
608: static int
609: function_cmp(const FUNCTION * a, const FUNCTION * b)
610: {
611: return strncmp(a->name, b->name, 8);
612: }
613:
614: static IMPLEMENT_LHASH_COMP_FN(function, FUNCTION)
615:
616: static unsigned long
617: function_hash(const FUNCTION * a)
618: {
619: return lh_strhash(a->name);
620: }
621:
622: static IMPLEMENT_LHASH_HASH_FN(function, FUNCTION)
623:
624: static LHASH_OF(FUNCTION) *
625: prog_init(void)
626: {
627: LHASH_OF(FUNCTION) * ret;
628: FUNCTION *f;
629: size_t i;
630:
631: /* Purely so it looks nice when the user hits ? */
632: for (i = 0, f = functions; f->name != NULL; ++f, ++i)
633: ;
634: qsort(functions, i, sizeof *functions, SortFnByName);
635:
636: if ((ret = lh_FUNCTION_new()) == NULL)
637: return (NULL);
638:
639: for (f = functions; f->name != NULL; f++)
640: (void) lh_FUNCTION_insert(ret, f);
641: return (ret);
642: }