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