Add all publicly avaiable asn1 types to the asn1 fuzzer.
[openssl.git] / apps / openssl.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <openssl/bio.h>
14 #include <openssl/crypto.h>
15 #include <openssl/lhash.h>
16 #include <openssl/conf.h>
17 #include <openssl/x509.h>
18 #include <openssl/pem.h>
19 #include <openssl/ssl.h>
20 #ifndef OPENSSL_NO_ENGINE
21 # include <openssl/engine.h>
22 #endif
23 #include <openssl/err.h>
24 #ifdef OPENSSL_FIPS
25 # include <openssl/fips.h>
26 #endif
27 #define USE_SOCKETS /* needed for the _O_BINARY defs in the MS world */
28 #include "s_apps.h"
29 /* Needed to get the other O_xxx flags. */
30 #ifdef OPENSSL_SYS_VMS
31 # include <unixio.h>
32 #endif
33 #define INCLUDE_FUNCTION_TABLE
34 #include "apps.h"
35
36
37 #ifdef OPENSSL_NO_CAMELLIA
38 # define FORMAT "%-15s"
39 # define COLUMNS 5
40 #else
41 # define FORMAT "%-18s"
42 # define COLUMNS 4
43 #endif
44
45 /* Special sentinel to exit the program. */
46 #define EXIT_THE_PROGRAM (-1)
47
48 /*
49  * The LHASH callbacks ("hash" & "cmp") have been replaced by functions with
50  * the base prototypes (we cast each variable inside the function to the
51  * required type of "FUNCTION*"). This removes the necessity for
52  * macro-generated wrapper functions.
53  */
54 static LHASH_OF(FUNCTION) *prog_init(void);
55 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]);
56 static void list_pkey(void);
57 static void list_type(FUNC_TYPE ft);
58 static void list_disabled(void);
59 char *default_config_file = NULL;
60
61 static CONF *config = NULL;
62 BIO *bio_in = NULL;
63 BIO *bio_out = NULL;
64 BIO *bio_err = NULL;
65
66 static int apps_startup()
67 {
68 #ifdef SIGPIPE
69     signal(SIGPIPE, SIG_IGN);
70 #endif
71
72     /* Set non-default library initialisation settings */
73     if (!OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_ALL_BUILTIN
74                              | OPENSSL_INIT_LOAD_CONFIG, NULL))
75         return 0;
76
77 #ifndef OPENSSL_NO_UI
78     setup_ui_method();
79 #endif
80
81     return 1;
82 }
83
84 static void apps_shutdown()
85 {
86 #ifndef OPENSSL_NO_UI
87     destroy_ui_method();
88 #endif
89 }
90
91 static char *make_config_name()
92 {
93     const char *t;
94     size_t len;
95     char *p;
96
97     if ((t = getenv("OPENSSL_CONF")) != NULL)
98         return OPENSSL_strdup(t);
99
100     t = X509_get_default_cert_area();
101     len = strlen(t) + 1 + strlen(OPENSSL_CONF) + 1;
102     p = app_malloc(len, "config filename buffer");
103     strcpy(p, t);
104 #ifndef OPENSSL_SYS_VMS
105     strcat(p, "/");
106 #endif
107     strcat(p, OPENSSL_CONF);
108
109     return p;
110 }
111
112 int main(int argc, char *argv[])
113 {
114     FUNCTION f, *fp;
115     LHASH_OF(FUNCTION) *prog = NULL;
116     char **copied_argv = NULL;
117     char *p, *pname;
118     char buf[1024];
119     const char *prompt;
120     ARGS arg;
121     int first, n, i, ret = 0;
122
123     arg.argv = NULL;
124     arg.size = 0;
125
126     /* Set up some of the environment. */
127     default_config_file = make_config_name();
128     bio_in = dup_bio_in(FORMAT_TEXT);
129     bio_out = dup_bio_out(FORMAT_TEXT);
130     bio_err = dup_bio_err(FORMAT_TEXT);
131
132 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
133     copied_argv = argv = copy_argv(&argc, argv);
134 #endif
135
136     p = getenv("OPENSSL_DEBUG_MEMORY");
137     if (p != NULL && strcmp(p, "on") == 0)
138         CRYPTO_set_mem_debug(1);
139     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
140
141     if (getenv("OPENSSL_FIPS")) {
142 #ifdef OPENSSL_FIPS
143         if (!FIPS_mode_set(1)) {
144             ERR_print_errors(bio_err);
145             return 1;
146         }
147 #else
148         BIO_printf(bio_err, "FIPS mode not supported.\n");
149         return 1;
150 #endif
151     }
152
153     if (!apps_startup())
154         goto end;
155
156     prog = prog_init();
157     pname = opt_progname(argv[0]);
158
159     /* first check the program name */
160     f.name = pname;
161     fp = lh_FUNCTION_retrieve(prog, &f);
162     if (fp != NULL) {
163         argv[0] = pname;
164         ret = fp->func(argc, argv);
165         goto end;
166     }
167
168     /* If there is stuff on the command line, run with that. */
169     if (argc != 1) {
170         argc--;
171         argv++;
172         ret = do_cmd(prog, argc, argv);
173         if (ret < 0)
174             ret = 0;
175         goto end;
176     }
177
178     /* ok, lets enter interactive mode */
179     for (;;) {
180         ret = 0;
181         /* Read a line, continue reading if line ends with \ */
182         for (p = buf, n = sizeof buf, i = 0, first = 1; n > 0; first = 0) {
183             prompt = first ? "OpenSSL> " : "> ";
184             p[0] = '\0';
185 #ifndef READLINE
186             fputs(prompt, stdout);
187             fflush(stdout);
188             if (!fgets(p, n, stdin))
189                 goto end;
190             if (p[0] == '\0')
191                 goto end;
192             i = strlen(p);
193             if (i <= 1)
194                 break;
195             if (p[i - 2] != '\\')
196                 break;
197             i -= 2;
198             p += i;
199             n -= i;
200 #else
201             {
202                 extern char *readline(const char *);
203                 extern void add_history(const char *cp);
204                 char *text;
205
206                 text = readline(prompt);
207                 if (text == NULL)
208                     goto end;
209                 i = strlen(text);
210                 if (i == 0 || i > n)
211                     break;
212                 if (text[i - 1] != '\\') {
213                     p += strlen(strcpy(p, text));
214                     free(text);
215                     add_history(buf);
216                     break;
217                 }
218
219                 text[i - 1] = '\0';
220                 p += strlen(strcpy(p, text));
221                 free(text);
222                 n -= i;
223             }
224 #endif
225         }
226
227         if (!chopup_args(&arg, buf)) {
228             BIO_printf(bio_err, "Can't parse (no memory?)\n");
229             break;
230         }
231
232         ret = do_cmd(prog, arg.argc, arg.argv);
233         if (ret == EXIT_THE_PROGRAM) {
234             ret = 0;
235             goto end;
236         }
237         if (ret != 0)
238             BIO_printf(bio_err, "error in %s\n", arg.argv[0]);
239         (void)BIO_flush(bio_out);
240         (void)BIO_flush(bio_err);
241     }
242     ret = 1;
243  end:
244     OPENSSL_free(copied_argv);
245     OPENSSL_free(default_config_file);
246     NCONF_free(config);
247     config = NULL;
248     lh_FUNCTION_free(prog);
249     OPENSSL_free(arg.argv);
250
251     BIO_free(bio_in);
252     BIO_free_all(bio_out);
253     apps_shutdown();
254 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
255     if (CRYPTO_mem_leaks(bio_err) <= 0)
256         ret = 1;
257 #endif
258     BIO_free(bio_err);
259     EXIT(ret);
260 }
261
262 OPTIONS exit_options[] = {
263     {NULL}
264 };
265
266 static void list_cipher_fn(const EVP_CIPHER *c,
267                            const char *from, const char *to, void *arg)
268 {
269     if (c)
270         BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
271     else {
272         if (!from)
273             from = "<undefined>";
274         if (!to)
275             to = "<undefined>";
276         BIO_printf(arg, "%s => %s\n", from, to);
277     }
278 }
279
280 static void list_md_fn(const EVP_MD *m,
281                        const char *from, const char *to, void *arg)
282 {
283     if (m)
284         BIO_printf(arg, "%s\n", EVP_MD_name(m));
285     else {
286         if (!from)
287             from = "<undefined>";
288         if (!to)
289             to = "<undefined>";
290         BIO_printf((BIO *)arg, "%s => %s\n", from, to);
291     }
292 }
293
294 /* Unified enum for help and list commands. */
295 typedef enum HELPLIST_CHOICE {
296     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
297     OPT_COMMANDS, OPT_DIGEST_COMMANDS,
298     OPT_DIGEST_ALGORITHMS, OPT_CIPHER_COMMANDS, OPT_CIPHER_ALGORITHMS,
299     OPT_PK_ALGORITHMS, OPT_DISABLED
300 } HELPLIST_CHOICE;
301
302 OPTIONS list_options[] = {
303     {"help", OPT_HELP, '-', "Display this summary"},
304     {"commands", OPT_COMMANDS, '-', "List of standard commands"},
305     {"digest-commands", OPT_DIGEST_COMMANDS, '-',
306      "List of message digest commands"},
307     {"digest-algorithms", OPT_DIGEST_ALGORITHMS, '-',
308      "List of message digest algorithms"},
309     {"cipher-commands", OPT_CIPHER_COMMANDS, '-', "List of cipher commands"},
310     {"cipher-algorithms", OPT_CIPHER_ALGORITHMS, '-',
311      "List of cipher algorithms"},
312     {"public-key-algorithms", OPT_PK_ALGORITHMS, '-',
313      "List of public key algorithms"},
314     {"disabled", OPT_DISABLED, '-',
315      "List of disabled features"},
316     {NULL}
317 };
318
319 int list_main(int argc, char **argv)
320 {
321     char *prog;
322     HELPLIST_CHOICE o;
323     int done = 0;
324
325     prog = opt_init(argc, argv, list_options);
326     while ((o = opt_next()) != OPT_EOF) {
327         switch (o) {
328         case OPT_EOF:  /* Never hit, but suppresses warning */
329         case OPT_ERR:
330             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
331             return 1;
332         case OPT_HELP:
333             opt_help(list_options);
334             break;
335         case OPT_COMMANDS:
336             list_type(FT_general);
337             break;
338         case OPT_DIGEST_COMMANDS:
339             list_type(FT_md);
340             break;
341         case OPT_DIGEST_ALGORITHMS:
342             EVP_MD_do_all_sorted(list_md_fn, bio_out);
343             break;
344         case OPT_CIPHER_COMMANDS:
345             list_type(FT_cipher);
346             break;
347         case OPT_CIPHER_ALGORITHMS:
348             EVP_CIPHER_do_all_sorted(list_cipher_fn, bio_out);
349             break;
350         case OPT_PK_ALGORITHMS:
351             list_pkey();
352             break;
353         case OPT_DISABLED:
354             list_disabled();
355             break;
356         }
357         done = 1;
358     }
359
360     if (!done) {
361         BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
362         return 1;
363     }
364
365     return 0;
366 }
367
368 OPTIONS help_options[] = {
369     {"help", OPT_HELP, '-', "Display this summary"},
370     {NULL}
371 };
372
373 int help_main(int argc, char **argv)
374 {
375     FUNCTION *fp;
376     int i, nl;
377     FUNC_TYPE tp;
378     char *prog;
379     HELPLIST_CHOICE o;
380
381     prog = opt_init(argc, argv, help_options);
382     while ((o = opt_next()) != OPT_EOF) {
383         switch (o) {
384         default:
385             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
386             return 1;
387         case OPT_HELP:
388             opt_help(help_options);
389             return 0;
390         }
391     }
392
393     if (opt_num_rest() != 0) {
394         BIO_printf(bio_err, "Usage: %s\n", prog);
395         return 1;
396     }
397
398     BIO_printf(bio_err, "\nStandard commands");
399     i = 0;
400     tp = FT_none;
401     for (fp = functions; fp->name != NULL; fp++) {
402         nl = 0;
403         if (((i++) % COLUMNS) == 0) {
404             BIO_printf(bio_err, "\n");
405             nl = 1;
406         }
407         if (fp->type != tp) {
408             tp = fp->type;
409             if (!nl)
410                 BIO_printf(bio_err, "\n");
411             if (tp == FT_md) {
412                 i = 1;
413                 BIO_printf(bio_err,
414                            "\nMessage Digest commands (see the `dgst' command for more details)\n");
415             } else if (tp == FT_cipher) {
416                 i = 1;
417                 BIO_printf(bio_err,
418                            "\nCipher commands (see the `enc' command for more details)\n");
419             }
420         }
421         BIO_printf(bio_err, FORMAT, fp->name);
422     }
423     BIO_printf(bio_err, "\n\n");
424     return 0;
425 }
426
427 int exit_main(int argc, char **argv)
428 {
429     return EXIT_THE_PROGRAM;
430 }
431
432 static void list_type(FUNC_TYPE ft)
433 {
434     FUNCTION *fp;
435     int i = 0;
436
437     for (fp = functions; fp->name != NULL; fp++)
438         if (fp->type == ft) {
439             if ((i++ % COLUMNS) == 0)
440                 BIO_printf(bio_out, "\n");
441             BIO_printf(bio_out, FORMAT, fp->name);
442         }
443     BIO_printf(bio_out, "\n");
444 }
445
446 static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[])
447 {
448     FUNCTION f, *fp;
449
450     if (argc <= 0 || argv[0] == NULL)
451         return (0);
452     f.name = argv[0];
453     fp = lh_FUNCTION_retrieve(prog, &f);
454     if (fp == NULL) {
455         if (EVP_get_digestbyname(argv[0])) {
456             f.type = FT_md;
457             f.func = dgst_main;
458             fp = &f;
459         } else if (EVP_get_cipherbyname(argv[0])) {
460             f.type = FT_cipher;
461             f.func = enc_main;
462             fp = &f;
463         }
464     }
465     if (fp != NULL) {
466         return (fp->func(argc, argv));
467     }
468     if ((strncmp(argv[0], "no-", 3)) == 0) {
469         /*
470          * User is asking if foo is unsupported, by trying to "run" the
471          * no-foo command.  Strange.
472          */
473         f.name = argv[0] + 3;
474         if (lh_FUNCTION_retrieve(prog, &f) == NULL) {
475             BIO_printf(bio_out, "%s\n", argv[0]);
476             return (0);
477         }
478         BIO_printf(bio_out, "%s\n", argv[0] + 3);
479         return 1;
480     }
481     if (strcmp(argv[0], "quit") == 0 || strcmp(argv[0], "q") == 0 ||
482         strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "bye") == 0)
483         /* Special value to mean "exit the program. */
484         return EXIT_THE_PROGRAM;
485
486     BIO_printf(bio_err, "Invalid command '%s'; type \"help\" for a list.\n",
487                argv[0]);
488     return (1);
489 }
490
491 static void list_pkey(void)
492 {
493     int i;
494
495     for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
496         const EVP_PKEY_ASN1_METHOD *ameth;
497         int pkey_id, pkey_base_id, pkey_flags;
498         const char *pinfo, *pem_str;
499         ameth = EVP_PKEY_asn1_get0(i);
500         EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
501                                 &pinfo, &pem_str, ameth);
502         if (pkey_flags & ASN1_PKEY_ALIAS) {
503             BIO_printf(bio_out, "Name: %s\n", OBJ_nid2ln(pkey_id));
504             BIO_printf(bio_out, "\tAlias for: %s\n",
505                        OBJ_nid2ln(pkey_base_id));
506         } else {
507             BIO_printf(bio_out, "Name: %s\n", pinfo);
508             BIO_printf(bio_out, "\tType: %s Algorithm\n",
509                        pkey_flags & ASN1_PKEY_DYNAMIC ?
510                        "External" : "Builtin");
511             BIO_printf(bio_out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
512             if (pem_str == NULL)
513                 pem_str = "(none)";
514             BIO_printf(bio_out, "\tPEM string: %s\n", pem_str);
515         }
516
517     }
518 }
519
520 static int function_cmp(const FUNCTION * a, const FUNCTION * b)
521 {
522     return strncmp(a->name, b->name, 8);
523 }
524
525 static unsigned long function_hash(const FUNCTION * a)
526 {
527     return OPENSSL_LH_strhash(a->name);
528 }
529
530 static int SortFnByName(const void *_f1, const void *_f2)
531 {
532     const FUNCTION *f1 = _f1;
533     const FUNCTION *f2 = _f2;
534
535     if (f1->type != f2->type)
536         return f1->type - f2->type;
537     return strcmp(f1->name, f2->name);
538 }
539
540 static void list_disabled(void)
541 {
542     BIO_puts(bio_out, "Disabled algorithms:\n");
543 #ifdef OPENSSL_NO_BF
544     BIO_puts(bio_out, "BF\n");
545 #endif
546 #ifndef OPENSSL_NO_BLAKE2
547     BIO_puts(bio_out, "BLAKE2\n");
548 #endif
549 #ifdef OPENSSL_NO_CAMELLIA
550     BIO_puts(bio_out, "CAMELLIA\n");
551 #endif
552 #ifdef OPENSSL_NO_CAST
553     BIO_puts(bio_out, "CAST\n");
554 #endif
555 #ifdef OPENSSL_NO_CMAC
556     BIO_puts(bio_out, "CMAC\n");
557 #endif
558 #ifdef OPENSSL_NO_CMS
559     BIO_puts(bio_out, "CMS\n");
560 #endif
561 #ifdef OPENSSL_NO_COMP
562     BIO_puts(bio_out, "COMP\n");
563 #endif
564 #ifdef OPENSSL_NO_DES
565     BIO_puts(bio_out, "DES\n");
566 #endif
567 #ifdef OPENSSL_NO_DGRAM
568     BIO_puts(bio_out, "DGRAM\n");
569 #endif
570 #ifdef OPENSSL_NO_DH
571     BIO_puts(bio_out, "DH\n");
572 #endif
573 #ifdef OPENSSL_NO_DSA
574     BIO_puts(bio_out, "DSA\n");
575 #endif
576 #if defined(OPENSSL_NO_DTLS)
577     BIO_puts(bio_out, "DTLS\n");
578 #endif
579 #if defined(OPENSSL_NO_DTLS1)
580     BIO_puts(bio_out, "DTLS1\n");
581 #endif
582 #if defined(OPENSSL_NO_DTLS1_2)
583     BIO_puts(bio_out, "DTLS1_2\n");
584 #endif
585 #ifdef OPENSSL_NO_EC
586     BIO_puts(bio_out, "EC\n");
587 #endif
588 #ifdef OPENSSL_NO_EC2M
589     BIO_puts(bio_out, "EC2M\n");
590 #endif
591 #ifdef OPENSSL_NO_ENGINE
592     BIO_puts(bio_out, "ENGINE\n");
593 #endif
594 #ifdef OPENSSL_NO_GOST
595     BIO_puts(bio_out, "GOST\n");
596 #endif
597 #ifdef OPENSSL_NO_HEARTBEATS
598     BIO_puts(bio_out, "HEARTBEATS\n");
599 #endif
600 #ifdef OPENSSL_NO_IDEA
601     BIO_puts(bio_out, "IDEA\n");
602 #endif
603 #ifdef OPENSSL_NO_MD2
604     BIO_puts(bio_out, "MD2\n");
605 #endif
606 #ifdef OPENSSL_NO_MD4
607     BIO_puts(bio_out, "MD4\n");
608 #endif
609 #ifdef OPENSSL_NO_MD5
610     BIO_puts(bio_out, "MD5\n");
611 #endif
612 #ifdef OPENSSL_NO_MDC2
613     BIO_puts(bio_out, "MDC2\n");
614 #endif
615 #ifdef OPENSSL_NO_OCB
616     BIO_puts(bio_out, "OCB\n");
617 #endif
618 #ifdef OPENSSL_NO_OCSP
619     BIO_puts(bio_out, "OCSP\n");
620 #endif
621 #ifdef OPENSSL_NO_PSK
622     BIO_puts(bio_out, "PSK\n");
623 #endif
624 #ifdef OPENSSL_NO_RC2
625     BIO_puts(bio_out, "RC2\n");
626 #endif
627 #ifdef OPENSSL_NO_RC4
628     BIO_puts(bio_out, "RC4\n");
629 #endif
630 #ifdef OPENSSL_NO_RC5
631     BIO_puts(bio_out, "RC5\n");
632 #endif
633 #ifdef OPENSSL_NO_RMD160
634     BIO_puts(bio_out, "RMD160\n");
635 #endif
636 #ifdef OPENSSL_NO_RSA
637     BIO_puts(bio_out, "RSA\n");
638 #endif
639 #ifdef OPENSSL_NO_SCRYPT
640     BIO_puts(bio_out, "SCRYPT\n");
641 #endif
642 #ifdef OPENSSL_NO_SCTP
643     BIO_puts(bio_out, "SCTP\n");
644 #endif
645 #ifdef OPENSSL_NO_SEED
646     BIO_puts(bio_out, "SEED\n");
647 #endif
648 #ifdef OPENSSL_NO_SOCK
649     BIO_puts(bio_out, "SOCK\n");
650 #endif
651 #ifdef OPENSSL_NO_SRP
652     BIO_puts(bio_out, "SRP\n");
653 #endif
654 #ifdef OPENSSL_NO_SRTP
655     BIO_puts(bio_out, "SRTP\n");
656 #endif
657 #ifdef OPENSSL_NO_SSL3
658     BIO_puts(bio_out, "SSL3\n");
659 #endif
660 #ifdef OPENSSL_NO_TLS1
661     BIO_puts(bio_out, "TLS1\n");
662 #endif
663 #ifdef OPENSSL_NO_TLS1_1
664     BIO_puts(bio_out, "TLS1_1\n");
665 #endif
666 #ifdef OPENSSL_NO_TLS1_2
667     BIO_puts(bio_out, "TLS1_2\n");
668 #endif
669 #ifdef OPENSSL_NO_WHIRLPOOL
670     BIO_puts(bio_out, "WHIRLPOOL\n");
671 #endif
672 #ifndef ZLIB
673     BIO_puts(bio_out, "ZLIB\n");
674 #endif
675 }
676
677 static LHASH_OF(FUNCTION) *prog_init(void)
678 {
679     LHASH_OF(FUNCTION) *ret;
680     FUNCTION *f;
681     size_t i;
682
683     /* Sort alphabetically within category. For nicer help displays. */
684     for (i = 0, f = functions; f->name != NULL; ++f, ++i) ;
685     qsort(functions, i, sizeof(*functions), SortFnByName);
686
687     if ((ret = lh_FUNCTION_new(function_hash, function_cmp)) == NULL)
688         return (NULL);
689
690     for (f = functions; f->name != NULL; f++)
691         (void)lh_FUNCTION_insert(ret, f);
692     return (ret);
693 }