1 /* $OpenBSD: dgst.c,v 1.19 2022/01/14 09:28:07 tb Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
65 #include <openssl/bio.h>
66 #include <openssl/err.h>
67 #include <openssl/evp.h>
68 #include <openssl/hmac.h>
69 #include <openssl/objects.h>
70 #include <openssl/pem.h>
71 #include <openssl/x509.h>
73 #define BUFSIZE 1024*8
76 do_fp(BIO
* out
, unsigned char *buf
, BIO
* bp
, int sep
, int binout
,
77 EVP_PKEY
* key
, unsigned char *sigin
, int siglen
,
78 const char *sig_name
, const char *md_name
,
79 const char *file
, BIO
* bmd
);
90 STACK_OF(OPENSSL_STRING
) *macopts
;
97 STACK_OF(OPENSSL_STRING
) *sigopts
;
102 dgst_opt_macopt(char *arg
)
107 if (dgst_config
.macopts
== NULL
&&
108 (dgst_config
.macopts
= sk_OPENSSL_STRING_new_null()) == NULL
)
111 if (!sk_OPENSSL_STRING_push(dgst_config
.macopts
, arg
))
118 dgst_opt_md(int argc
, char **argv
, int *argsused
)
120 char *name
= argv
[0];
125 if ((dgst_config
.m
= EVP_get_digestbyname(name
)) == NULL
)
128 dgst_config
.md
= dgst_config
.m
;
135 dgst_opt_prverify(char *arg
)
140 dgst_config
.keyfile
= arg
;
141 dgst_config
.do_verify
= 1;
146 dgst_opt_sigopt(char *arg
)
151 if (dgst_config
.sigopts
== NULL
&&
152 (dgst_config
.sigopts
= sk_OPENSSL_STRING_new_null()) == NULL
)
155 if (!sk_OPENSSL_STRING_push(dgst_config
.sigopts
, arg
))
162 dgst_opt_verify(char *arg
)
167 dgst_config
.keyfile
= arg
;
168 dgst_config
.want_pub
= 1;
169 dgst_config
.do_verify
= 1;
173 static const struct option dgst_options
[] = {
176 .desc
= "Output the digest or signature in binary form",
177 .type
= OPTION_VALUE
,
178 .opt
.value
= &dgst_config
.out_bin
,
183 .desc
= "Print the digest in two-digit groups separated by colons",
184 .type
= OPTION_VALUE
,
185 .opt
.value
= &dgst_config
.separator
,
190 .desc
= "Print BIO debugging information",
192 .opt
.flag
= &dgst_config
.debug
,
196 .desc
= "Output as hex dump",
197 .type
= OPTION_VALUE
,
198 .opt
.value
= &dgst_config
.out_bin
,
204 .desc
= "Create hashed MAC with key",
206 .opt
.arg
= &dgst_config
.hmac_key
,
211 .desc
= "Key file format (PEM)",
212 .type
= OPTION_ARG_FORMAT
,
213 .opt
.value
= &dgst_config
.keyform
,
217 .argname
= "algorithm",
218 .desc
= "Create MAC (not necessarily HMAC)",
220 .opt
.arg
= &dgst_config
.mac_name
,
225 .desc
= "MAC algorithm parameters or key",
226 .type
= OPTION_ARG_FUNC
,
227 .opt
.argfunc
= dgst_opt_macopt
,
232 .desc
= "Output to file rather than stdout",
234 .opt
.arg
= &dgst_config
.outfile
,
239 .desc
= "Input file passphrase source",
241 .opt
.arg
= &dgst_config
.passargin
,
246 .desc
= "Verify a signature using private key in file",
247 .type
= OPTION_ARG_FUNC
,
248 .opt
.argfunc
= dgst_opt_prverify
,
252 .desc
= "Output the digest in coreutils format",
253 .type
= OPTION_VALUE
,
254 .opt
.value
= &dgst_config
.separator
,
260 .desc
= "Sign digest using private key in file",
262 .opt
.arg
= &dgst_config
.keyfile
,
267 .desc
= "Signature to verify",
269 .opt
.arg
= &dgst_config
.sigfile
,
274 .desc
= "Signature parameter",
275 .type
= OPTION_ARG_FUNC
,
276 .opt
.argfunc
= dgst_opt_sigopt
,
281 .desc
= "Verify a signature using public key in file",
282 .type
= OPTION_ARG_FUNC
,
283 .opt
.argfunc
= dgst_opt_verify
,
288 .type
= OPTION_ARGV_FUNC
,
289 .opt
.argvfunc
= dgst_opt_md
,
295 list_md_fn(const EVP_MD
* m
, const char *from
, const char *to
, void *arg
)
301 mname
= OBJ_nid2ln(EVP_MD_type(m
));
302 /* Skip shortnames */
303 if (strcmp(from
, mname
))
305 if (strchr(mname
, ' '))
306 mname
= EVP_MD_name(m
);
307 BIO_printf(arg
, " -%-17s To use the %s message digest algorithm\n",
314 fprintf(stderr
, "usage: dgst [-cdr] [-binary] [-digest] [-hex]");
315 fprintf(stderr
, " [-hmac key] [-keyform fmt]\n");
316 fprintf(stderr
, " [-mac algorithm] [-macopt nm:v] [-out file]");
317 fprintf(stderr
, " [-passin arg]\n");
318 fprintf(stderr
, " [-prverify file] [-sign file]");
319 fprintf(stderr
, " [-signature file]\n");
320 fprintf(stderr
, " [-sigopt nm:v] [-verify file] [file ...]\n\n");
321 options_usage(dgst_options
);
322 EVP_MD_do_all_sorted(list_md_fn
, bio_err
);
323 fprintf(stderr
, "\n");
327 dgst_main(int argc
, char **argv
)
329 unsigned char *buf
= NULL
;
331 BIO
*in
= NULL
, *inp
;
334 #define PROG_NAME_SIZE 39
335 char pname
[PROG_NAME_SIZE
+ 1];
336 EVP_PKEY
*sigkey
= NULL
;
337 unsigned char *sigbuf
= NULL
;
341 if (single_execution
) {
342 if (pledge("stdio cpath wpath rpath tty", NULL
) == -1) {
348 if ((buf
= malloc(BUFSIZE
)) == NULL
) {
349 BIO_printf(bio_err
, "out of memory\n");
353 memset(&dgst_config
, 0, sizeof(dgst_config
));
354 dgst_config
.keyform
= FORMAT_PEM
;
355 dgst_config
.out_bin
= -1;
357 /* first check the program name */
358 program_name(argv
[0], pname
, sizeof pname
);
360 dgst_config
.md
= EVP_get_digestbyname(pname
);
362 if (options_parse(argc
, argv
, dgst_options
, NULL
,
363 &dgst_config
.argsused
) != 0) {
367 argc
-= dgst_config
.argsused
;
368 argv
+= dgst_config
.argsused
;
370 if (dgst_config
.do_verify
&& !dgst_config
.sigfile
) {
372 "No signature to verify: use the -signature option\n");
376 in
= BIO_new(BIO_s_file());
377 bmd
= BIO_new(BIO_f_md());
378 if (in
== NULL
|| bmd
== NULL
) {
379 ERR_print_errors(bio_err
);
383 if (dgst_config
.debug
) {
384 BIO_set_callback(in
, BIO_debug_callback
);
385 /* needed for windows 3.1 */
386 BIO_set_callback_arg(in
, (char *) bio_err
);
388 if (!app_passwd(bio_err
, dgst_config
.passargin
, NULL
, &passin
, NULL
)) {
389 BIO_printf(bio_err
, "Error getting password\n");
392 if (dgst_config
.out_bin
== -1) {
393 if (dgst_config
.keyfile
)
394 dgst_config
.out_bin
= 1;
396 dgst_config
.out_bin
= 0;
399 if (dgst_config
.outfile
) {
400 if (dgst_config
.out_bin
)
401 out
= BIO_new_file(dgst_config
.outfile
, "wb");
403 out
= BIO_new_file(dgst_config
.outfile
, "w");
405 out
= BIO_new_fp(stdout
, BIO_NOCLOSE
);
409 BIO_printf(bio_err
, "Error opening output file %s\n",
410 dgst_config
.outfile
? dgst_config
.outfile
: "(stdout)");
411 ERR_print_errors(bio_err
);
414 if ((!!dgst_config
.mac_name
+ !!dgst_config
.keyfile
+
415 !!dgst_config
.hmac_key
) > 1) {
417 "MAC and Signing key cannot both be specified\n");
420 if (dgst_config
.keyfile
) {
421 if (dgst_config
.want_pub
)
422 sigkey
= load_pubkey(bio_err
, dgst_config
.keyfile
,
423 dgst_config
.keyform
, 0, NULL
, "key file");
425 sigkey
= load_key(bio_err
, dgst_config
.keyfile
,
426 dgst_config
.keyform
, 0, passin
, "key file");
429 * load_[pub]key() has already printed an appropriate
435 if (dgst_config
.mac_name
) {
436 EVP_PKEY_CTX
*mac_ctx
= NULL
;
438 if (!init_gen_str(bio_err
, &mac_ctx
, dgst_config
.mac_name
, 0))
440 if (dgst_config
.macopts
) {
442 for (i
= 0; i
< sk_OPENSSL_STRING_num(
443 dgst_config
.macopts
); i
++) {
444 macopt
= sk_OPENSSL_STRING_value(
445 dgst_config
.macopts
, i
);
446 if (pkey_ctrl_string(mac_ctx
, macopt
) <= 0) {
448 "MAC parameter error \"%s\"\n",
450 ERR_print_errors(bio_err
);
455 if (EVP_PKEY_keygen(mac_ctx
, &sigkey
) <= 0) {
456 BIO_puts(bio_err
, "Error generating key\n");
457 ERR_print_errors(bio_err
);
462 EVP_PKEY_CTX_free(mac_ctx
);
466 if (dgst_config
.hmac_key
) {
467 sigkey
= EVP_PKEY_new_mac_key(EVP_PKEY_HMAC
, NULL
,
468 (unsigned char *) dgst_config
.hmac_key
, -1);
473 EVP_MD_CTX
*mctx
= NULL
;
474 EVP_PKEY_CTX
*pctx
= NULL
;
476 if (!BIO_get_md_ctx(bmd
, &mctx
)) {
477 BIO_printf(bio_err
, "Error getting context\n");
478 ERR_print_errors(bio_err
);
481 if (dgst_config
.do_verify
)
482 r
= EVP_DigestVerifyInit(mctx
, &pctx
, dgst_config
.md
,
485 r
= EVP_DigestSignInit(mctx
, &pctx
, dgst_config
.md
,
488 BIO_printf(bio_err
, "Error setting context\n");
489 ERR_print_errors(bio_err
);
492 if (dgst_config
.sigopts
) {
494 for (i
= 0; i
< sk_OPENSSL_STRING_num(
495 dgst_config
.sigopts
); i
++) {
496 sigopt
= sk_OPENSSL_STRING_value(
497 dgst_config
.sigopts
, i
);
498 if (pkey_ctrl_string(pctx
, sigopt
) <= 0) {
500 "parameter error \"%s\"\n",
502 ERR_print_errors(bio_err
);
508 /* we use md as a filter, reading from 'in' */
510 if (dgst_config
.md
== NULL
)
511 dgst_config
.md
= EVP_sha256();
512 if (!BIO_set_md(bmd
, dgst_config
.md
)) {
513 BIO_printf(bio_err
, "Error setting digest %s\n", pname
);
514 ERR_print_errors(bio_err
);
519 if (dgst_config
.sigfile
&& sigkey
) {
521 siglen
= EVP_PKEY_size(sigkey
);
522 sigbuf
= malloc(siglen
);
523 if (sigbuf
== NULL
) {
524 BIO_printf(bio_err
, "out of memory\n");
525 ERR_print_errors(bio_err
);
528 sigbio
= BIO_new_file(dgst_config
.sigfile
, "rb");
530 BIO_printf(bio_err
, "Error opening signature file %s\n",
531 dgst_config
.sigfile
);
532 ERR_print_errors(bio_err
);
535 siglen
= BIO_read(sigbio
, sigbuf
, siglen
);
538 BIO_printf(bio_err
, "Error reading signature file %s\n",
539 dgst_config
.sigfile
);
540 ERR_print_errors(bio_err
);
544 inp
= BIO_push(bmd
, in
);
546 if (dgst_config
.md
== NULL
) {
548 BIO_get_md_ctx(bmd
, &tctx
);
549 dgst_config
.md
= EVP_MD_CTX_md(tctx
);
552 BIO_set_fp(in
, stdin
, BIO_NOCLOSE
);
553 err
= do_fp(out
, buf
, inp
, dgst_config
.separator
,
554 dgst_config
.out_bin
, sigkey
, sigbuf
, siglen
, NULL
, NULL
,
557 const char *md_name
= NULL
, *sig_name
= NULL
;
558 if (!dgst_config
.out_bin
) {
560 const EVP_PKEY_ASN1_METHOD
*ameth
;
561 ameth
= EVP_PKEY_get0_asn1(sigkey
);
563 EVP_PKEY_asn1_get0_info(NULL
, NULL
,
564 NULL
, NULL
, &sig_name
, ameth
);
566 md_name
= EVP_MD_name(dgst_config
.md
);
569 for (i
= 0; i
< argc
; i
++) {
571 if (BIO_read_filename(in
, argv
[i
]) <= 0) {
576 r
= do_fp(out
, buf
, inp
, dgst_config
.separator
,
577 dgst_config
.out_bin
, sigkey
, sigbuf
, siglen
,
578 sig_name
, md_name
, argv
[i
], bmd
);
582 (void) BIO_reset(bmd
);
587 freezero(buf
, BUFSIZE
);
591 EVP_PKEY_free(sigkey
);
592 sk_OPENSSL_STRING_free(dgst_config
.sigopts
);
593 sk_OPENSSL_STRING_free(dgst_config
.macopts
);
601 do_fp(BIO
* out
, unsigned char *buf
, BIO
* bp
, int sep
, int binout
,
602 EVP_PKEY
* key
, unsigned char *sigin
, int siglen
,
603 const char *sig_name
, const char *md_name
,
604 const char *file
, BIO
* bmd
)
610 i
= BIO_read(bp
, (char *) buf
, BUFSIZE
);
612 BIO_printf(bio_err
, "Read Error in %s\n", file
);
613 ERR_print_errors(bio_err
);
621 BIO_get_md_ctx(bp
, &ctx
);
622 i
= EVP_DigestVerifyFinal(ctx
, sigin
, (unsigned int) siglen
);
624 BIO_printf(out
, "Verified OK\n");
626 BIO_printf(out
, "Verification Failure\n");
629 BIO_printf(bio_err
, "Error Verifying Data\n");
630 ERR_print_errors(bio_err
);
637 BIO_get_md_ctx(bp
, &ctx
);
639 if (!EVP_DigestSignFinal(ctx
, buf
, &len
)) {
640 BIO_printf(bio_err
, "Error Signing Data\n");
641 ERR_print_errors(bio_err
);
645 len
= BIO_gets(bp
, (char *) buf
, BUFSIZE
);
647 ERR_print_errors(bio_err
);
653 BIO_write(out
, buf
, len
);
655 for (i
= 0; i
< (int) len
; i
++)
656 BIO_printf(out
, "%02x", buf
[i
]);
657 BIO_printf(out
, " *%s\n", file
);
660 BIO_printf(out
, "%s-%s(%s)= ", sig_name
, md_name
, file
);
662 BIO_printf(out
, "%s(%s)= ", md_name
, file
);
664 BIO_printf(out
, "(%s)= ", file
);
665 for (i
= 0; i
< (int) len
; i
++) {
667 BIO_printf(out
, ":");
668 BIO_printf(out
, "%02x", buf
[i
]);
670 BIO_printf(out
, "\n");