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