]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/mac.c
Fix error reporting glitch in X509_STORE_CTX_print_verify_cb() in t_x509.c
[thirdparty/openssl.git] / apps / mac.c
CommitLineData
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
22typedef 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
28const 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
51int 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
73opthelp:
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 }
101 argc = opt_num_rest();
102 argv = opt_rest();
103
104 if (argc != 1) {
105 BIO_printf(bio_err, "Invalid number of extra arguments\n");
106 goto opthelp;
107 }
108
d747fb2e 109 mac = EVP_MAC_fetch(NULL, argv[0], NULL);
4d768e96
SL
110 if (mac == NULL) {
111 BIO_printf(bio_err, "Invalid MAC name %s\n", argv[0]);
112 goto opthelp;
113 }
114
115 ctx = EVP_MAC_CTX_new(mac);
116 if (ctx == NULL)
117 goto err;
118
119 if (opts != NULL) {
d747fb2e 120 int ok = 1;
95214b43 121 OSSL_PARAM *params =
41f7ecf3 122 app_params_new_from_opts(opts, EVP_MAC_settable_ctx_params(mac));
d747fb2e 123
95214b43
SL
124 if (params == NULL)
125 goto err;
126
127 if (!EVP_MAC_CTX_set_params(ctx, params)) {
128 BIO_printf(bio_err, "MAC parameter error\n");
129 ERR_print_errors(bio_err);
130 ok = 0;
d747fb2e 131 }
95214b43 132 app_params_free(params);
d747fb2e
RL
133 if (!ok)
134 goto err;
4d768e96
SL
135 }
136
137 /* Use text mode for stdin */
138 if (infile == NULL || strcmp(infile, "-") == 0)
139 inform = FORMAT_TEXT;
140 in = bio_open_default(infile, 'r', inform);
141 if (in == NULL)
142 goto err;
143
144 out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
145 if (out == NULL)
146 goto err;
147
148 if (!EVP_MAC_init(ctx)) {
149 BIO_printf(bio_err, "EVP_MAC_Init failed\n");
150 goto err;
151 }
152
4d768e96
SL
153 for (;;) {
154 i = BIO_read(in, (char *)buf, BUFSIZE);
155 if (i < 0) {
156 BIO_printf(bio_err, "Read Error in '%s'\n", infile);
157 goto err;
158 }
159 if (i == 0)
160 break;
161 if (!EVP_MAC_update(ctx, buf, i)) {
162 BIO_printf(bio_err, "EVP_MAC_update failed\n");
163 goto err;
164 }
165 }
166
d747fb2e 167 if (!EVP_MAC_final(ctx, NULL, &len, 0)) {
4d768e96
SL
168 BIO_printf(bio_err, "EVP_MAC_final failed\n");
169 goto err;
170 }
171 if (len > BUFSIZE) {
172 BIO_printf(bio_err, "output len is too large\n");
173 goto err;
174 }
175
d747fb2e 176 if (!EVP_MAC_final(ctx, buf, &len, BUFSIZE)) {
4d768e96
SL
177 BIO_printf(bio_err, "EVP_MAC_final failed\n");
178 goto err;
179 }
180
181 if (out_bin) {
182 BIO_write(out, buf, len);
183 } else {
184 if (outfile == NULL)
185 BIO_printf(out,"\n");
186 for (i = 0; i < (int)len; ++i)
187 BIO_printf(out, "%02X", buf[i]);
188 if (outfile == NULL)
189 BIO_printf(out,"\n");
190 }
191
192 ret = 0;
193err:
194 if (ret != 0)
195 ERR_print_errors(bio_err);
196 OPENSSL_clear_free(buf, BUFSIZE);
197 sk_OPENSSL_STRING_free(opts);
198 BIO_free(in);
199 BIO_free(out);
200 EVP_MAC_CTX_free(ctx);
d747fb2e 201 EVP_MAC_free(mac);
4d768e96
SL
202 return ret;
203}