]>
Commit | Line | Data |
---|---|---|
4d768e96 | 1 | /* |
a28d06f3 | 2 | * Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved. |
4d768e96 | 3 | * |
a6ed19dc | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
4d768e96 SL |
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> | |
d747fb2e | 17 | #include <openssl/params.h> |
4d768e96 SL |
18 | |
19 | #undef BUFSIZE | |
20 | #define BUFSIZE 1024*8 | |
21 | ||
22 | typedef enum OPTION_choice { | |
23 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
6bd4e3f2 P |
24 | OPT_MACOPT, OPT_BIN, OPT_IN, OPT_OUT, |
25 | OPT_PROV_ENUM | |
4d768e96 SL |
26 | } OPTION_CHOICE; |
27 | ||
28 | const OPTIONS mac_options[] = { | |
29 | {OPT_HELP_STR, 1, '-', "Usage: %s [options] mac_name\n"}, | |
5388f986 RS |
30 | |
31 | OPT_SECTION("General"), | |
4d768e96 | 32 | {"help", OPT_HELP, '-', "Display this summary"}, |
92de469f RS |
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"}, | |
5388f986 RS |
35 | |
36 | OPT_SECTION("Input"), | |
4d768e96 | 37 | {"in", OPT_IN, '<', "Input file to MAC (default is stdin)"}, |
5388f986 RS |
38 | |
39 | OPT_SECTION("Output"), | |
4d768e96 | 40 | {"out", OPT_OUT, '>', "Output to filename rather than stdout"}, |
92de469f RS |
41 | {"binary", OPT_BIN, '-', |
42 | "Output in binary format (default is hexadecimal)"}, | |
43 | ||
6bd4e3f2 P |
44 | OPT_PROV_OPTIONS, |
45 | ||
92de469f RS |
46 | OPT_PARAMETERS(), |
47 | {"mac_name", 0, 0, "MAC algorithm"}, | |
4d768e96 SL |
48 | {NULL} |
49 | }; | |
50 | ||
4d768e96 SL |
51 | int mac_main(int argc, char **argv) |
52 | { | |
53 | int ret = 1; | |
54 | char *prog; | |
d747fb2e | 55 | EVP_MAC *mac = NULL; |
4d768e96 SL |
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) { | |
a3c62426 | 72 | default: |
4d768e96 SL |
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; | |
6bd4e3f2 P |
95 | case OPT_PROV_CASES: |
96 | if (!opt_provider(o)) | |
97 | goto err; | |
98 | break; | |
4d768e96 SL |
99 | } |
100 | } | |
021410ea RS |
101 | |
102 | /* One argument, the MAC name. */ | |
4d768e96 SL |
103 | argc = opt_num_rest(); |
104 | argv = opt_rest(); | |
021410ea | 105 | if (argc != 1) |
4d768e96 | 106 | goto opthelp; |
4d768e96 | 107 | |
7dc67708 | 108 | mac = EVP_MAC_fetch(app_get0_libctx(), argv[0], app_get0_propq()); |
4d768e96 SL |
109 | if (mac == NULL) { |
110 | BIO_printf(bio_err, "Invalid MAC name %s\n", argv[0]); | |
111 | goto opthelp; | |
112 | } | |
113 | ||
865adf97 | 114 | ctx = EVP_MAC_CTX_new(mac); |
4d768e96 SL |
115 | if (ctx == NULL) |
116 | goto err; | |
117 | ||
118 | if (opts != NULL) { | |
d747fb2e | 119 | int ok = 1; |
95214b43 | 120 | OSSL_PARAM *params = |
41f7ecf3 | 121 | app_params_new_from_opts(opts, EVP_MAC_settable_ctx_params(mac)); |
d747fb2e | 122 | |
95214b43 SL |
123 | if (params == NULL) |
124 | goto err; | |
125 | ||
865adf97 | 126 | if (!EVP_MAC_CTX_set_params(ctx, params)) { |
95214b43 SL |
127 | BIO_printf(bio_err, "MAC parameter error\n"); |
128 | ERR_print_errors(bio_err); | |
129 | ok = 0; | |
d747fb2e | 130 | } |
95214b43 | 131 | app_params_free(params); |
d747fb2e RL |
132 | if (!ok) |
133 | goto err; | |
4d768e96 SL |
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 | ||
4d768e96 SL |
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 | ||
d747fb2e | 166 | if (!EVP_MAC_final(ctx, NULL, &len, 0)) { |
4d768e96 SL |
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 | ||
d747fb2e | 175 | if (!EVP_MAC_final(ctx, buf, &len, BUFSIZE)) { |
4d768e96 SL |
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); | |
865adf97 | 199 | EVP_MAC_CTX_free(ctx); |
d747fb2e | 200 | EVP_MAC_free(mac); |
4d768e96 SL |
201 | return ret; |
202 | } |