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