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