1*e7718adaStb /* $OpenBSD: dgst.c,v 1.21 2023/03/06 14:32:05 tb Exp $ */
2dab3f910Sjsing /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3dab3f910Sjsing * All rights reserved.
4dab3f910Sjsing *
5dab3f910Sjsing * This package is an SSL implementation written
6dab3f910Sjsing * by Eric Young (eay@cryptsoft.com).
7dab3f910Sjsing * The implementation was written so as to conform with Netscapes SSL.
8dab3f910Sjsing *
9dab3f910Sjsing * This library is free for commercial and non-commercial use as long as
10dab3f910Sjsing * the following conditions are aheared to. The following conditions
11dab3f910Sjsing * apply to all code found in this distribution, be it the RC4, RSA,
12dab3f910Sjsing * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13dab3f910Sjsing * included with this distribution is covered by the same copyright terms
14dab3f910Sjsing * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15dab3f910Sjsing *
16dab3f910Sjsing * Copyright remains Eric Young's, and as such any Copyright notices in
17dab3f910Sjsing * the code are not to be removed.
18dab3f910Sjsing * If this package is used in a product, Eric Young should be given attribution
19dab3f910Sjsing * as the author of the parts of the library used.
20dab3f910Sjsing * This can be in the form of a textual message at program startup or
21dab3f910Sjsing * in documentation (online or textual) provided with the package.
22dab3f910Sjsing *
23dab3f910Sjsing * Redistribution and use in source and binary forms, with or without
24dab3f910Sjsing * modification, are permitted provided that the following conditions
25dab3f910Sjsing * are met:
26dab3f910Sjsing * 1. Redistributions of source code must retain the copyright
27dab3f910Sjsing * notice, this list of conditions and the following disclaimer.
28dab3f910Sjsing * 2. Redistributions in binary form must reproduce the above copyright
29dab3f910Sjsing * notice, this list of conditions and the following disclaimer in the
30dab3f910Sjsing * documentation and/or other materials provided with the distribution.
31dab3f910Sjsing * 3. All advertising materials mentioning features or use of this software
32dab3f910Sjsing * must display the following acknowledgement:
33dab3f910Sjsing * "This product includes cryptographic software written by
34dab3f910Sjsing * Eric Young (eay@cryptsoft.com)"
35dab3f910Sjsing * The word 'cryptographic' can be left out if the rouines from the library
36dab3f910Sjsing * being used are not cryptographic related :-).
37dab3f910Sjsing * 4. If you include any Windows specific code (or a derivative thereof) from
38dab3f910Sjsing * the apps directory (application code) you must include an acknowledgement:
39dab3f910Sjsing * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40dab3f910Sjsing *
41dab3f910Sjsing * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42dab3f910Sjsing * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43dab3f910Sjsing * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44dab3f910Sjsing * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45dab3f910Sjsing * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46dab3f910Sjsing * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47dab3f910Sjsing * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48dab3f910Sjsing * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49dab3f910Sjsing * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50dab3f910Sjsing * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51dab3f910Sjsing * SUCH DAMAGE.
52dab3f910Sjsing *
53dab3f910Sjsing * The licence and distribution terms for any publically available version or
54dab3f910Sjsing * derivative of this code cannot be changed. i.e. this code cannot simply be
55dab3f910Sjsing * copied and put under another distribution licence
56dab3f910Sjsing * [including the GNU Public Licence.]
57dab3f910Sjsing */
58dab3f910Sjsing
59dab3f910Sjsing #include <stdio.h>
60dab3f910Sjsing #include <stdlib.h>
61dab3f910Sjsing #include <string.h>
62dab3f910Sjsing
63dab3f910Sjsing #include "apps.h"
64dab3f910Sjsing
65dab3f910Sjsing #include <openssl/bio.h>
66dab3f910Sjsing #include <openssl/err.h>
67dab3f910Sjsing #include <openssl/evp.h>
68dab3f910Sjsing #include <openssl/hmac.h>
69dab3f910Sjsing #include <openssl/objects.h>
70dab3f910Sjsing #include <openssl/pem.h>
71dab3f910Sjsing #include <openssl/x509.h>
72dab3f910Sjsing
73dab3f910Sjsing #define BUFSIZE 1024*8
74dab3f910Sjsing
75dab3f910Sjsing int
76dab3f910Sjsing do_fp(BIO * out, unsigned char *buf, BIO * bp, int sep, int binout,
77dab3f910Sjsing EVP_PKEY * key, unsigned char *sigin, int siglen,
78dab3f910Sjsing const char *sig_name, const char *md_name,
79dab3f910Sjsing const char *file, BIO * bmd);
80dab3f910Sjsing
81ed873533Sinoguchi static struct {
82ed873533Sinoguchi int argsused;
83ed873533Sinoguchi int debug;
84ed873533Sinoguchi int do_verify;
85ed873533Sinoguchi char *hmac_key;
86ed873533Sinoguchi char *keyfile;
87ed873533Sinoguchi int keyform;
88ed873533Sinoguchi const EVP_MD *m;
89ed873533Sinoguchi char *mac_name;
90ed873533Sinoguchi STACK_OF(OPENSSL_STRING) *macopts;
91ed873533Sinoguchi const EVP_MD *md;
92ed873533Sinoguchi int out_bin;
93ed873533Sinoguchi char *outfile;
94ed873533Sinoguchi char *passargin;
95ed873533Sinoguchi int separator;
96ed873533Sinoguchi char *sigfile;
97ed873533Sinoguchi STACK_OF(OPENSSL_STRING) *sigopts;
98ed873533Sinoguchi int want_pub;
99*e7718adaStb } cfg;
100ed873533Sinoguchi
101704010f9Sinoguchi static int
dgst_opt_macopt(char * arg)102704010f9Sinoguchi dgst_opt_macopt(char *arg)
103704010f9Sinoguchi {
104704010f9Sinoguchi if (arg == NULL)
105704010f9Sinoguchi return (1);
106704010f9Sinoguchi
107*e7718adaStb if (cfg.macopts == NULL &&
108*e7718adaStb (cfg.macopts = sk_OPENSSL_STRING_new_null()) == NULL)
109704010f9Sinoguchi return (1);
110704010f9Sinoguchi
111*e7718adaStb if (!sk_OPENSSL_STRING_push(cfg.macopts, arg))
112704010f9Sinoguchi return (1);
113704010f9Sinoguchi
114704010f9Sinoguchi return (0);
115704010f9Sinoguchi }
116704010f9Sinoguchi
117704010f9Sinoguchi static int
dgst_opt_md(int argc,char ** argv,int * argsused)118704010f9Sinoguchi dgst_opt_md(int argc, char **argv, int *argsused)
119704010f9Sinoguchi {
120704010f9Sinoguchi char *name = argv[0];
121704010f9Sinoguchi
122704010f9Sinoguchi if (*name++ != '-')
123704010f9Sinoguchi return (1);
124704010f9Sinoguchi
125*e7718adaStb if ((cfg.m = EVP_get_digestbyname(name)) == NULL)
126704010f9Sinoguchi return (1);
127704010f9Sinoguchi
128*e7718adaStb cfg.md = cfg.m;
129ddf8e1cbSinoguchi
130704010f9Sinoguchi *argsused = 1;
131704010f9Sinoguchi return (0);
132704010f9Sinoguchi }
133704010f9Sinoguchi
134704010f9Sinoguchi static int
dgst_opt_prverify(char * arg)135704010f9Sinoguchi dgst_opt_prverify(char *arg)
136704010f9Sinoguchi {
137704010f9Sinoguchi if (arg == NULL)
138704010f9Sinoguchi return (1);
139704010f9Sinoguchi
140*e7718adaStb cfg.keyfile = arg;
141*e7718adaStb cfg.do_verify = 1;
142704010f9Sinoguchi return (0);
143704010f9Sinoguchi }
144704010f9Sinoguchi
145704010f9Sinoguchi static int
dgst_opt_sigopt(char * arg)146704010f9Sinoguchi dgst_opt_sigopt(char *arg)
147704010f9Sinoguchi {
148704010f9Sinoguchi if (arg == NULL)
149704010f9Sinoguchi return (1);
150704010f9Sinoguchi
151*e7718adaStb if (cfg.sigopts == NULL &&
152*e7718adaStb (cfg.sigopts = sk_OPENSSL_STRING_new_null()) == NULL)
153704010f9Sinoguchi return (1);
154704010f9Sinoguchi
155*e7718adaStb if (!sk_OPENSSL_STRING_push(cfg.sigopts, arg))
156704010f9Sinoguchi return (1);
157704010f9Sinoguchi
158704010f9Sinoguchi return (0);
159704010f9Sinoguchi }
160704010f9Sinoguchi
161704010f9Sinoguchi static int
dgst_opt_verify(char * arg)162704010f9Sinoguchi dgst_opt_verify(char *arg)
163704010f9Sinoguchi {
164704010f9Sinoguchi if (arg == NULL)
165704010f9Sinoguchi return (1);
166704010f9Sinoguchi
167*e7718adaStb cfg.keyfile = arg;
168*e7718adaStb cfg.want_pub = 1;
169*e7718adaStb cfg.do_verify = 1;
170704010f9Sinoguchi return (0);
171704010f9Sinoguchi }
172704010f9Sinoguchi
173704010f9Sinoguchi static const struct option dgst_options[] = {
174704010f9Sinoguchi {
175704010f9Sinoguchi .name = "binary",
176ddf8e1cbSinoguchi .desc = "Output the digest or signature in binary form",
177704010f9Sinoguchi .type = OPTION_VALUE,
178*e7718adaStb .opt.value = &cfg.out_bin,
179704010f9Sinoguchi .value = 1,
180704010f9Sinoguchi },
181704010f9Sinoguchi {
182704010f9Sinoguchi .name = "c",
183ddf8e1cbSinoguchi .desc = "Print the digest in two-digit groups separated by colons",
184704010f9Sinoguchi .type = OPTION_VALUE,
185*e7718adaStb .opt.value = &cfg.separator,
186704010f9Sinoguchi .value = 1,
187704010f9Sinoguchi },
188704010f9Sinoguchi {
189704010f9Sinoguchi .name = "d",
190ddf8e1cbSinoguchi .desc = "Print BIO debugging information",
191704010f9Sinoguchi .type = OPTION_FLAG,
192*e7718adaStb .opt.flag = &cfg.debug,
193704010f9Sinoguchi },
194704010f9Sinoguchi {
195704010f9Sinoguchi .name = "hex",
196704010f9Sinoguchi .desc = "Output as hex dump",
197704010f9Sinoguchi .type = OPTION_VALUE,
198*e7718adaStb .opt.value = &cfg.out_bin,
199704010f9Sinoguchi .value = 0,
200704010f9Sinoguchi },
201704010f9Sinoguchi {
202704010f9Sinoguchi .name = "hmac",
203704010f9Sinoguchi .argname = "key",
204704010f9Sinoguchi .desc = "Create hashed MAC with key",
205704010f9Sinoguchi .type = OPTION_ARG,
206*e7718adaStb .opt.arg = &cfg.hmac_key,
207704010f9Sinoguchi },
208704010f9Sinoguchi {
209704010f9Sinoguchi .name = "keyform",
210704010f9Sinoguchi .argname = "format",
211704010f9Sinoguchi .desc = "Key file format (PEM)",
212704010f9Sinoguchi .type = OPTION_ARG_FORMAT,
213*e7718adaStb .opt.value = &cfg.keyform,
214704010f9Sinoguchi },
215704010f9Sinoguchi {
216704010f9Sinoguchi .name = "mac",
217704010f9Sinoguchi .argname = "algorithm",
218704010f9Sinoguchi .desc = "Create MAC (not necessarily HMAC)",
219704010f9Sinoguchi .type = OPTION_ARG,
220*e7718adaStb .opt.arg = &cfg.mac_name,
221704010f9Sinoguchi },
222704010f9Sinoguchi {
223704010f9Sinoguchi .name = "macopt",
224704010f9Sinoguchi .argname = "nm:v",
225704010f9Sinoguchi .desc = "MAC algorithm parameters or key",
226704010f9Sinoguchi .type = OPTION_ARG_FUNC,
227704010f9Sinoguchi .opt.argfunc = dgst_opt_macopt,
228704010f9Sinoguchi },
229704010f9Sinoguchi {
230704010f9Sinoguchi .name = "out",
231704010f9Sinoguchi .argname = "file",
232704010f9Sinoguchi .desc = "Output to file rather than stdout",
233704010f9Sinoguchi .type = OPTION_ARG,
234*e7718adaStb .opt.arg = &cfg.outfile,
235704010f9Sinoguchi },
236704010f9Sinoguchi {
237704010f9Sinoguchi .name = "passin",
238704010f9Sinoguchi .argname = "arg",
239704010f9Sinoguchi .desc = "Input file passphrase source",
240704010f9Sinoguchi .type = OPTION_ARG,
241*e7718adaStb .opt.arg = &cfg.passargin,
242704010f9Sinoguchi },
243704010f9Sinoguchi {
244704010f9Sinoguchi .name = "prverify",
245704010f9Sinoguchi .argname = "file",
246704010f9Sinoguchi .desc = "Verify a signature using private key in file",
247704010f9Sinoguchi .type = OPTION_ARG_FUNC,
248704010f9Sinoguchi .opt.argfunc = dgst_opt_prverify,
249704010f9Sinoguchi },
250704010f9Sinoguchi {
251704010f9Sinoguchi .name = "r",
252ddf8e1cbSinoguchi .desc = "Output the digest in coreutils format",
253704010f9Sinoguchi .type = OPTION_VALUE,
254*e7718adaStb .opt.value = &cfg.separator,
255704010f9Sinoguchi .value = 2,
256704010f9Sinoguchi },
257704010f9Sinoguchi {
258704010f9Sinoguchi .name = "sign",
259704010f9Sinoguchi .argname = "file",
260704010f9Sinoguchi .desc = "Sign digest using private key in file",
261704010f9Sinoguchi .type = OPTION_ARG,
262*e7718adaStb .opt.arg = &cfg.keyfile,
263704010f9Sinoguchi },
264704010f9Sinoguchi {
265704010f9Sinoguchi .name = "signature",
266704010f9Sinoguchi .argname = "file",
267704010f9Sinoguchi .desc = "Signature to verify",
268704010f9Sinoguchi .type = OPTION_ARG,
269*e7718adaStb .opt.arg = &cfg.sigfile,
270704010f9Sinoguchi },
271704010f9Sinoguchi {
272704010f9Sinoguchi .name = "sigopt",
273704010f9Sinoguchi .argname = "nm:v",
274704010f9Sinoguchi .desc = "Signature parameter",
275704010f9Sinoguchi .type = OPTION_ARG_FUNC,
276704010f9Sinoguchi .opt.argfunc = dgst_opt_sigopt,
277704010f9Sinoguchi },
278704010f9Sinoguchi {
279704010f9Sinoguchi .name = "verify",
280704010f9Sinoguchi .argname = "file",
281704010f9Sinoguchi .desc = "Verify a signature using public key in file",
282704010f9Sinoguchi .type = OPTION_ARG_FUNC,
283704010f9Sinoguchi .opt.argfunc = dgst_opt_verify,
284704010f9Sinoguchi },
285704010f9Sinoguchi {
286704010f9Sinoguchi .name = NULL,
287704010f9Sinoguchi .desc = "",
288704010f9Sinoguchi .type = OPTION_ARGV_FUNC,
289704010f9Sinoguchi .opt.argvfunc = dgst_opt_md,
290704010f9Sinoguchi },
291704010f9Sinoguchi { NULL },
292704010f9Sinoguchi };
293704010f9Sinoguchi
294dab3f910Sjsing static void
list_md_fn(const EVP_MD * m,const char * from,const char * to,void * arg)295dab3f910Sjsing list_md_fn(const EVP_MD * m, const char *from, const char *to, void *arg)
296dab3f910Sjsing {
297dab3f910Sjsing const char *mname;
298dab3f910Sjsing /* Skip aliases */
299dab3f910Sjsing if (!m)
300dab3f910Sjsing return;
301dab3f910Sjsing mname = OBJ_nid2ln(EVP_MD_type(m));
302dab3f910Sjsing /* Skip shortnames */
303dab3f910Sjsing if (strcmp(from, mname))
304dab3f910Sjsing return;
305dab3f910Sjsing if (strchr(mname, ' '))
306dab3f910Sjsing mname = EVP_MD_name(m);
307704010f9Sinoguchi BIO_printf(arg, " -%-17s To use the %s message digest algorithm\n",
308dab3f910Sjsing mname, mname);
309dab3f910Sjsing }
310dab3f910Sjsing
311704010f9Sinoguchi static void
dgst_usage(void)312704010f9Sinoguchi dgst_usage(void)
313704010f9Sinoguchi {
314704010f9Sinoguchi fprintf(stderr, "usage: dgst [-cdr] [-binary] [-digest] [-hex]");
315704010f9Sinoguchi fprintf(stderr, " [-hmac key] [-keyform fmt]\n");
316704010f9Sinoguchi fprintf(stderr, " [-mac algorithm] [-macopt nm:v] [-out file]");
317704010f9Sinoguchi fprintf(stderr, " [-passin arg]\n");
318704010f9Sinoguchi fprintf(stderr, " [-prverify file] [-sign file]");
319704010f9Sinoguchi fprintf(stderr, " [-signature file]\n");
320704010f9Sinoguchi fprintf(stderr, " [-sigopt nm:v] [-verify file] [file ...]\n\n");
321704010f9Sinoguchi options_usage(dgst_options);
322704010f9Sinoguchi EVP_MD_do_all_sorted(list_md_fn, bio_err);
323704010f9Sinoguchi fprintf(stderr, "\n");
324704010f9Sinoguchi }
325704010f9Sinoguchi
326dab3f910Sjsing int
dgst_main(int argc,char ** argv)327dab3f910Sjsing dgst_main(int argc, char **argv)
328dab3f910Sjsing {
329dab3f910Sjsing unsigned char *buf = NULL;
330dab3f910Sjsing int i, err = 1;
331dab3f910Sjsing BIO *in = NULL, *inp;
332dab3f910Sjsing BIO *bmd = NULL;
333dab3f910Sjsing BIO *out = NULL;
334dab3f910Sjsing #define PROG_NAME_SIZE 39
335dab3f910Sjsing char pname[PROG_NAME_SIZE + 1];
336dab3f910Sjsing EVP_PKEY *sigkey = NULL;
337dab3f910Sjsing unsigned char *sigbuf = NULL;
338dab3f910Sjsing int siglen = 0;
339ed873533Sinoguchi char *passin = NULL;
340dab3f910Sjsing
34151811eadSderaadt if (pledge("stdio cpath wpath rpath tty", NULL) == -1) {
3429bc487adSdoug perror("pledge");
343e370f0eeSdoug exit(1);
344e370f0eeSdoug }
3459bc487adSdoug
346dab3f910Sjsing if ((buf = malloc(BUFSIZE)) == NULL) {
347dab3f910Sjsing BIO_printf(bio_err, "out of memory\n");
348dab3f910Sjsing goto end;
349dab3f910Sjsing }
350dab3f910Sjsing
351*e7718adaStb memset(&cfg, 0, sizeof(cfg));
352*e7718adaStb cfg.keyform = FORMAT_PEM;
353*e7718adaStb cfg.out_bin = -1;
354ed873533Sinoguchi
355dab3f910Sjsing /* first check the program name */
356dab3f910Sjsing program_name(argv[0], pname, sizeof pname);
357dab3f910Sjsing
358*e7718adaStb cfg.md = EVP_get_digestbyname(pname);
359dab3f910Sjsing
360704010f9Sinoguchi if (options_parse(argc, argv, dgst_options, NULL,
361*e7718adaStb &cfg.argsused) != 0) {
362704010f9Sinoguchi dgst_usage();
363704010f9Sinoguchi goto end;
364dab3f910Sjsing }
365*e7718adaStb argc -= cfg.argsused;
366*e7718adaStb argv += cfg.argsused;
367dab3f910Sjsing
368*e7718adaStb if (cfg.do_verify && !cfg.sigfile) {
369b43c161cSinoguchi BIO_printf(bio_err,
370b43c161cSinoguchi "No signature to verify: use the -signature option\n");
371dab3f910Sjsing goto end;
372dab3f910Sjsing }
373e8456fe5Sdoug
374dab3f910Sjsing in = BIO_new(BIO_s_file());
375dab3f910Sjsing bmd = BIO_new(BIO_f_md());
376e8456fe5Sdoug if (in == NULL || bmd == NULL) {
377e8456fe5Sdoug ERR_print_errors(bio_err);
378e8456fe5Sdoug goto end;
379e8456fe5Sdoug }
380e8456fe5Sdoug
381*e7718adaStb if (cfg.debug) {
382dab3f910Sjsing BIO_set_callback(in, BIO_debug_callback);
383dab3f910Sjsing /* needed for windows 3.1 */
384dab3f910Sjsing BIO_set_callback_arg(in, (char *) bio_err);
385dab3f910Sjsing }
386*e7718adaStb if (!app_passwd(bio_err, cfg.passargin, NULL, &passin, NULL)) {
387dab3f910Sjsing BIO_printf(bio_err, "Error getting password\n");
388dab3f910Sjsing goto end;
389dab3f910Sjsing }
390*e7718adaStb if (cfg.out_bin == -1) {
391*e7718adaStb if (cfg.keyfile)
392*e7718adaStb cfg.out_bin = 1;
393dab3f910Sjsing else
394*e7718adaStb cfg.out_bin = 0;
395dab3f910Sjsing }
396dab3f910Sjsing
397*e7718adaStb if (cfg.outfile) {
398*e7718adaStb if (cfg.out_bin)
399*e7718adaStb out = BIO_new_file(cfg.outfile, "wb");
400dab3f910Sjsing else
401*e7718adaStb out = BIO_new_file(cfg.outfile, "w");
402dab3f910Sjsing } else {
403dab3f910Sjsing out = BIO_new_fp(stdout, BIO_NOCLOSE);
404dab3f910Sjsing }
405dab3f910Sjsing
406dab3f910Sjsing if (!out) {
407dab3f910Sjsing BIO_printf(bio_err, "Error opening output file %s\n",
408*e7718adaStb cfg.outfile ? cfg.outfile : "(stdout)");
409dab3f910Sjsing ERR_print_errors(bio_err);
410dab3f910Sjsing goto end;
411dab3f910Sjsing }
412*e7718adaStb if ((!!cfg.mac_name + !!cfg.keyfile +
413*e7718adaStb !!cfg.hmac_key) > 1) {
414b43c161cSinoguchi BIO_printf(bio_err,
415b43c161cSinoguchi "MAC and Signing key cannot both be specified\n");
416dab3f910Sjsing goto end;
417dab3f910Sjsing }
418*e7718adaStb if (cfg.keyfile) {
419*e7718adaStb if (cfg.want_pub)
420*e7718adaStb sigkey = load_pubkey(bio_err, cfg.keyfile,
421*e7718adaStb cfg.keyform, 0, NULL, "key file");
422dab3f910Sjsing else
423*e7718adaStb sigkey = load_key(bio_err, cfg.keyfile,
424*e7718adaStb cfg.keyform, 0, passin, "key file");
425dab3f910Sjsing if (!sigkey) {
426dab3f910Sjsing /*
427dab3f910Sjsing * load_[pub]key() has already printed an appropriate
428dab3f910Sjsing * message
429dab3f910Sjsing */
430dab3f910Sjsing goto end;
431dab3f910Sjsing }
432dab3f910Sjsing }
433*e7718adaStb if (cfg.mac_name) {
434dab3f910Sjsing EVP_PKEY_CTX *mac_ctx = NULL;
435dab3f910Sjsing int r = 0;
436*e7718adaStb if (!init_gen_str(bio_err, &mac_ctx, cfg.mac_name, 0))
437dab3f910Sjsing goto mac_end;
438*e7718adaStb if (cfg.macopts) {
439dab3f910Sjsing char *macopt;
440b43c161cSinoguchi for (i = 0; i < sk_OPENSSL_STRING_num(
441*e7718adaStb cfg.macopts); i++) {
442b43c161cSinoguchi macopt = sk_OPENSSL_STRING_value(
443*e7718adaStb cfg.macopts, i);
444dab3f910Sjsing if (pkey_ctrl_string(mac_ctx, macopt) <= 0) {
445dab3f910Sjsing BIO_printf(bio_err,
446dab3f910Sjsing "MAC parameter error \"%s\"\n",
447dab3f910Sjsing macopt);
448dab3f910Sjsing ERR_print_errors(bio_err);
449dab3f910Sjsing goto mac_end;
450dab3f910Sjsing }
451dab3f910Sjsing }
452dab3f910Sjsing }
453dab3f910Sjsing if (EVP_PKEY_keygen(mac_ctx, &sigkey) <= 0) {
454dab3f910Sjsing BIO_puts(bio_err, "Error generating key\n");
455dab3f910Sjsing ERR_print_errors(bio_err);
456dab3f910Sjsing goto mac_end;
457dab3f910Sjsing }
458dab3f910Sjsing r = 1;
459dab3f910Sjsing mac_end:
460dab3f910Sjsing EVP_PKEY_CTX_free(mac_ctx);
461dab3f910Sjsing if (r == 0)
462dab3f910Sjsing goto end;
463dab3f910Sjsing }
464*e7718adaStb if (cfg.hmac_key) {
4655284dfeaSbcook sigkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
466*e7718adaStb (unsigned char *) cfg.hmac_key, -1);
467dab3f910Sjsing if (!sigkey)
468dab3f910Sjsing goto end;
469dab3f910Sjsing }
470dab3f910Sjsing if (sigkey) {
471dab3f910Sjsing EVP_MD_CTX *mctx = NULL;
472dab3f910Sjsing EVP_PKEY_CTX *pctx = NULL;
473dab3f910Sjsing int r;
474dab3f910Sjsing if (!BIO_get_md_ctx(bmd, &mctx)) {
475dab3f910Sjsing BIO_printf(bio_err, "Error getting context\n");
476dab3f910Sjsing ERR_print_errors(bio_err);
477dab3f910Sjsing goto end;
478dab3f910Sjsing }
479*e7718adaStb if (cfg.do_verify)
480*e7718adaStb r = EVP_DigestVerifyInit(mctx, &pctx, cfg.md,
481b43c161cSinoguchi NULL, sigkey);
482dab3f910Sjsing else
483*e7718adaStb r = EVP_DigestSignInit(mctx, &pctx, cfg.md,
484b43c161cSinoguchi NULL, sigkey);
485dab3f910Sjsing if (!r) {
486dab3f910Sjsing BIO_printf(bio_err, "Error setting context\n");
487dab3f910Sjsing ERR_print_errors(bio_err);
488dab3f910Sjsing goto end;
489dab3f910Sjsing }
490*e7718adaStb if (cfg.sigopts) {
491dab3f910Sjsing char *sigopt;
492b43c161cSinoguchi for (i = 0; i < sk_OPENSSL_STRING_num(
493*e7718adaStb cfg.sigopts); i++) {
494b43c161cSinoguchi sigopt = sk_OPENSSL_STRING_value(
495*e7718adaStb cfg.sigopts, i);
496dab3f910Sjsing if (pkey_ctrl_string(pctx, sigopt) <= 0) {
497dab3f910Sjsing BIO_printf(bio_err,
498dab3f910Sjsing "parameter error \"%s\"\n",
499dab3f910Sjsing sigopt);
500dab3f910Sjsing ERR_print_errors(bio_err);
501dab3f910Sjsing goto end;
502dab3f910Sjsing }
503dab3f910Sjsing }
504dab3f910Sjsing }
505dab3f910Sjsing }
506dab3f910Sjsing /* we use md as a filter, reading from 'in' */
507dab3f910Sjsing else {
508*e7718adaStb if (cfg.md == NULL)
509*e7718adaStb cfg.md = EVP_sha256();
510*e7718adaStb if (!BIO_set_md(bmd, cfg.md)) {
511dab3f910Sjsing BIO_printf(bio_err, "Error setting digest %s\n", pname);
512dab3f910Sjsing ERR_print_errors(bio_err);
513dab3f910Sjsing goto end;
514dab3f910Sjsing }
515dab3f910Sjsing }
516dab3f910Sjsing
517*e7718adaStb if (cfg.sigfile && sigkey) {
518dab3f910Sjsing BIO *sigbio;
519dab3f910Sjsing siglen = EVP_PKEY_size(sigkey);
520dab3f910Sjsing sigbuf = malloc(siglen);
5210a71567aSrpointel if (sigbuf == NULL) {
5220a71567aSrpointel BIO_printf(bio_err, "out of memory\n");
5230a71567aSrpointel ERR_print_errors(bio_err);
5240a71567aSrpointel goto end;
5250a71567aSrpointel }
526*e7718adaStb sigbio = BIO_new_file(cfg.sigfile, "rb");
527dab3f910Sjsing if (!sigbio) {
528dab3f910Sjsing BIO_printf(bio_err, "Error opening signature file %s\n",
529*e7718adaStb cfg.sigfile);
530dab3f910Sjsing ERR_print_errors(bio_err);
531dab3f910Sjsing goto end;
532dab3f910Sjsing }
533dab3f910Sjsing siglen = BIO_read(sigbio, sigbuf, siglen);
534dab3f910Sjsing BIO_free(sigbio);
535dab3f910Sjsing if (siglen <= 0) {
536dab3f910Sjsing BIO_printf(bio_err, "Error reading signature file %s\n",
537*e7718adaStb cfg.sigfile);
538dab3f910Sjsing ERR_print_errors(bio_err);
539dab3f910Sjsing goto end;
540dab3f910Sjsing }
541dab3f910Sjsing }
542dab3f910Sjsing inp = BIO_push(bmd, in);
543dab3f910Sjsing
544*e7718adaStb if (cfg.md == NULL) {
545dab3f910Sjsing EVP_MD_CTX *tctx;
546dab3f910Sjsing BIO_get_md_ctx(bmd, &tctx);
547*e7718adaStb cfg.md = EVP_MD_CTX_md(tctx);
548dab3f910Sjsing }
549dab3f910Sjsing if (argc == 0) {
550dab3f910Sjsing BIO_set_fp(in, stdin, BIO_NOCLOSE);
551*e7718adaStb err = do_fp(out, buf, inp, cfg.separator,
552*e7718adaStb cfg.out_bin, sigkey, sigbuf, siglen, NULL, NULL,
553b43c161cSinoguchi "stdin", bmd);
554dab3f910Sjsing } else {
555dab3f910Sjsing const char *md_name = NULL, *sig_name = NULL;
556*e7718adaStb if (!cfg.out_bin) {
557dab3f910Sjsing if (sigkey) {
558dab3f910Sjsing const EVP_PKEY_ASN1_METHOD *ameth;
559dab3f910Sjsing ameth = EVP_PKEY_get0_asn1(sigkey);
560dab3f910Sjsing if (ameth)
561dab3f910Sjsing EVP_PKEY_asn1_get0_info(NULL, NULL,
562dab3f910Sjsing NULL, NULL, &sig_name, ameth);
563dab3f910Sjsing }
564*e7718adaStb md_name = EVP_MD_name(cfg.md);
565dab3f910Sjsing }
566dab3f910Sjsing err = 0;
567dab3f910Sjsing for (i = 0; i < argc; i++) {
568dab3f910Sjsing int r;
569dab3f910Sjsing if (BIO_read_filename(in, argv[i]) <= 0) {
570dab3f910Sjsing perror(argv[i]);
571dab3f910Sjsing err++;
572dab3f910Sjsing continue;
573dab3f910Sjsing } else {
574*e7718adaStb r = do_fp(out, buf, inp, cfg.separator,
575*e7718adaStb cfg.out_bin, sigkey, sigbuf, siglen,
576b43c161cSinoguchi sig_name, md_name, argv[i], bmd);
577dab3f910Sjsing }
578dab3f910Sjsing if (r)
579dab3f910Sjsing err = r;
580dab3f910Sjsing (void) BIO_reset(bmd);
581dab3f910Sjsing }
582dab3f910Sjsing }
583dab3f910Sjsing
584dab3f910Sjsing end:
585cf4db30dSderaadt freezero(buf, BUFSIZE);
586dab3f910Sjsing BIO_free(in);
587dab3f910Sjsing free(passin);
588dab3f910Sjsing BIO_free_all(out);
589dab3f910Sjsing EVP_PKEY_free(sigkey);
590*e7718adaStb sk_OPENSSL_STRING_free(cfg.sigopts);
591*e7718adaStb sk_OPENSSL_STRING_free(cfg.macopts);
592dab3f910Sjsing free(sigbuf);
593dab3f910Sjsing BIO_free(bmd);
594dab3f910Sjsing
595dab3f910Sjsing return (err);
596dab3f910Sjsing }
597dab3f910Sjsing
598dab3f910Sjsing int
do_fp(BIO * out,unsigned char * buf,BIO * bp,int sep,int binout,EVP_PKEY * key,unsigned char * sigin,int siglen,const char * sig_name,const char * md_name,const char * file,BIO * bmd)599dab3f910Sjsing do_fp(BIO * out, unsigned char *buf, BIO * bp, int sep, int binout,
600dab3f910Sjsing EVP_PKEY * key, unsigned char *sigin, int siglen,
601dab3f910Sjsing const char *sig_name, const char *md_name,
602dab3f910Sjsing const char *file, BIO * bmd)
603dab3f910Sjsing {
604dab3f910Sjsing size_t len;
605dab3f910Sjsing int i;
606dab3f910Sjsing
607dab3f910Sjsing for (;;) {
608dab3f910Sjsing i = BIO_read(bp, (char *) buf, BUFSIZE);
609dab3f910Sjsing if (i < 0) {
610dab3f910Sjsing BIO_printf(bio_err, "Read Error in %s\n", file);
611dab3f910Sjsing ERR_print_errors(bio_err);
612dab3f910Sjsing return 1;
613dab3f910Sjsing }
614dab3f910Sjsing if (i == 0)
615dab3f910Sjsing break;
616dab3f910Sjsing }
617dab3f910Sjsing if (sigin) {
618dab3f910Sjsing EVP_MD_CTX *ctx;
619dab3f910Sjsing BIO_get_md_ctx(bp, &ctx);
620dab3f910Sjsing i = EVP_DigestVerifyFinal(ctx, sigin, (unsigned int) siglen);
621dab3f910Sjsing if (i > 0)
622dab3f910Sjsing BIO_printf(out, "Verified OK\n");
623dab3f910Sjsing else if (i == 0) {
624dab3f910Sjsing BIO_printf(out, "Verification Failure\n");
625dab3f910Sjsing return 1;
626dab3f910Sjsing } else {
627dab3f910Sjsing BIO_printf(bio_err, "Error Verifying Data\n");
628dab3f910Sjsing ERR_print_errors(bio_err);
629dab3f910Sjsing return 1;
630dab3f910Sjsing }
631dab3f910Sjsing return 0;
632dab3f910Sjsing }
633dab3f910Sjsing if (key) {
634dab3f910Sjsing EVP_MD_CTX *ctx;
635dab3f910Sjsing BIO_get_md_ctx(bp, &ctx);
636dab3f910Sjsing len = BUFSIZE;
637dab3f910Sjsing if (!EVP_DigestSignFinal(ctx, buf, &len)) {
638dab3f910Sjsing BIO_printf(bio_err, "Error Signing Data\n");
639dab3f910Sjsing ERR_print_errors(bio_err);
640dab3f910Sjsing return 1;
641dab3f910Sjsing }
642dab3f910Sjsing } else {
643dab3f910Sjsing len = BIO_gets(bp, (char *) buf, BUFSIZE);
644dab3f910Sjsing if ((int) len < 0) {
645dab3f910Sjsing ERR_print_errors(bio_err);
646dab3f910Sjsing return 1;
647dab3f910Sjsing }
648dab3f910Sjsing }
649dab3f910Sjsing
650dab3f910Sjsing if (binout)
651dab3f910Sjsing BIO_write(out, buf, len);
652dab3f910Sjsing else if (sep == 2) {
653dab3f910Sjsing for (i = 0; i < (int) len; i++)
654dab3f910Sjsing BIO_printf(out, "%02x", buf[i]);
655dab3f910Sjsing BIO_printf(out, " *%s\n", file);
656dab3f910Sjsing } else {
657dab3f910Sjsing if (sig_name)
658dab3f910Sjsing BIO_printf(out, "%s-%s(%s)= ", sig_name, md_name, file);
659dab3f910Sjsing else if (md_name)
660dab3f910Sjsing BIO_printf(out, "%s(%s)= ", md_name, file);
661dab3f910Sjsing else
662dab3f910Sjsing BIO_printf(out, "(%s)= ", file);
663dab3f910Sjsing for (i = 0; i < (int) len; i++) {
664dab3f910Sjsing if (sep && (i != 0))
665dab3f910Sjsing BIO_printf(out, ":");
666dab3f910Sjsing BIO_printf(out, "%02x", buf[i]);
667dab3f910Sjsing }
668dab3f910Sjsing BIO_printf(out, "\n");
669dab3f910Sjsing }
670dab3f910Sjsing return 0;
671dab3f910Sjsing }
672