Check non-option arguments
[openssl.git] / apps / mac.c
1 /*
2  * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 <string.h>
11
12 #include "apps.h"
13 #include "progs.h"
14 #include <openssl/bio.h>
15 #include <openssl/err.h>
16 #include <openssl/evp.h>
17 #include <openssl/params.h>
18
19 #undef BUFSIZE
20 #define BUFSIZE 1024*8
21
22 typedef enum OPTION_choice {
23     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
24     OPT_MACOPT, OPT_BIN, OPT_IN, OPT_OUT,
25     OPT_PROV_ENUM
26 } OPTION_CHOICE;
27
28 const OPTIONS mac_options[] = {
29     {OPT_HELP_STR, 1, '-', "Usage: %s [options] mac_name\n"},
30
31     OPT_SECTION("General"),
32     {"help", OPT_HELP, '-', "Display this summary"},
33     {"macopt", OPT_MACOPT, 's', "MAC algorithm parameters in n:v form"},
34     {OPT_MORE_STR, 1, '-', "See 'PARAMETER NAMES' in the EVP_MAC_ docs"},
35
36     OPT_SECTION("Input"),
37     {"in", OPT_IN, '<', "Input file to MAC (default is stdin)"},
38
39     OPT_SECTION("Output"),
40     {"out", OPT_OUT, '>', "Output to filename rather than stdout"},
41     {"binary", OPT_BIN, '-',
42         "Output in binary format (default is hexadecimal)"},
43
44     OPT_PROV_OPTIONS,
45
46     OPT_PARAMETERS(),
47     {"mac_name", 0, 0, "MAC algorithm"},
48     {NULL}
49 };
50
51 int mac_main(int argc, char **argv)
52 {
53     int ret = 1;
54     char *prog;
55     EVP_MAC *mac = NULL;
56     OPTION_CHOICE o;
57     EVP_MAC_CTX *ctx = NULL;
58     STACK_OF(OPENSSL_STRING) *opts = NULL;
59     unsigned char *buf = NULL;
60     size_t len;
61     int i;
62     BIO *in = NULL, *out = NULL;
63     const char *outfile = NULL;
64     const char *infile = NULL;
65     int out_bin = 0;
66     int inform = FORMAT_BINARY;
67
68     prog = opt_init(argc, argv, mac_options);
69     buf = app_malloc(BUFSIZE, "I/O buffer");
70     while ((o = opt_next()) != OPT_EOF) {
71         switch (o) {
72         default:
73 opthelp:
74             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
75             goto err;
76         case OPT_HELP:
77             opt_help(mac_options);
78             ret = 0;
79             goto err;
80         case OPT_BIN:
81             out_bin = 1;
82             break;
83         case OPT_IN:
84             infile = opt_arg();
85             break;
86         case OPT_OUT:
87             outfile = opt_arg();
88             break;
89         case OPT_MACOPT:
90             if (opts == NULL)
91                 opts = sk_OPENSSL_STRING_new_null();
92             if (opts == NULL || !sk_OPENSSL_STRING_push(opts, opt_arg()))
93                 goto opthelp;
94             break;
95         case OPT_PROV_CASES:
96             if (!opt_provider(o))
97                 goto err;
98             break;
99         }
100     }
101
102     /* One argument, the MAC name. */
103     argc = opt_num_rest();
104     argv = opt_rest();
105     if (argc != 1)
106         goto opthelp;
107
108     mac = EVP_MAC_fetch(NULL, argv[0], NULL);
109     if (mac == NULL) {
110         BIO_printf(bio_err, "Invalid MAC name %s\n", argv[0]);
111         goto opthelp;
112     }
113
114     ctx = EVP_MAC_CTX_new(mac);
115     if (ctx == NULL)
116         goto err;
117
118     if (opts != NULL) {
119         int ok = 1;
120         OSSL_PARAM *params =
121             app_params_new_from_opts(opts, EVP_MAC_settable_ctx_params(mac));
122
123         if (params == NULL)
124             goto err;
125
126         if (!EVP_MAC_CTX_set_params(ctx, params)) {
127             BIO_printf(bio_err, "MAC parameter error\n");
128             ERR_print_errors(bio_err);
129             ok = 0;
130         }
131         app_params_free(params);
132         if (!ok)
133             goto err;
134     }
135
136     /* Use text mode for stdin */
137     if (infile == NULL || strcmp(infile, "-") == 0)
138         inform = FORMAT_TEXT;
139     in = bio_open_default(infile, 'r', inform);
140     if (in == NULL)
141         goto err;
142
143     out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
144     if (out == NULL)
145         goto err;
146
147     if (!EVP_MAC_init(ctx)) {
148         BIO_printf(bio_err, "EVP_MAC_Init failed\n");
149         goto err;
150     }
151
152     for (;;) {
153         i = BIO_read(in, (char *)buf, BUFSIZE);
154         if (i < 0) {
155             BIO_printf(bio_err, "Read Error in '%s'\n", infile);
156             goto err;
157         }
158         if (i == 0)
159             break;
160         if (!EVP_MAC_update(ctx, buf, i)) {
161             BIO_printf(bio_err, "EVP_MAC_update failed\n");
162             goto err;
163         }
164     }
165
166     if (!EVP_MAC_final(ctx, NULL, &len, 0)) {
167         BIO_printf(bio_err, "EVP_MAC_final failed\n");
168         goto err;
169     }
170     if (len > BUFSIZE) {
171         BIO_printf(bio_err, "output len is too large\n");
172         goto err;
173     }
174
175     if (!EVP_MAC_final(ctx, buf, &len, BUFSIZE)) {
176         BIO_printf(bio_err, "EVP_MAC_final failed\n");
177         goto err;
178     }
179
180     if (out_bin) {
181         BIO_write(out, buf, len);
182     } else {
183         if (outfile == NULL)
184             BIO_printf(out,"\n");
185         for (i = 0; i < (int)len; ++i)
186             BIO_printf(out, "%02X", buf[i]);
187         if (outfile == NULL)
188             BIO_printf(out,"\n");
189     }
190
191     ret = 0;
192 err:
193     if (ret != 0)
194         ERR_print_errors(bio_err);
195     OPENSSL_clear_free(buf, BUFSIZE);
196     sk_OPENSSL_STRING_free(opts);
197     BIO_free(in);
198     BIO_free(out);
199     EVP_MAC_CTX_free(ctx);
200     EVP_MAC_free(mac);
201     return ret;
202 }