]>
Commit | Line | Data |
---|---|---|
58964a49 | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
d02b48c6 RE |
2 | * All rights reserved. |
3 | * | |
4 | * This package is an SSL implementation written | |
5 | * by Eric Young (eay@cryptsoft.com). | |
6 | * The implementation was written so as to conform with Netscapes SSL. | |
0f113f3e | 7 | * |
d02b48c6 RE |
8 | * This library is free for commercial and non-commercial use as long as |
9 | * the following conditions are aheared to. The following conditions | |
10 | * apply to all code found in this distribution, be it the RC4, RSA, | |
11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | |
12 | * included with this distribution is covered by the same copyright terms | |
13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | |
0f113f3e | 14 | * |
d02b48c6 RE |
15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
16 | * the code are not to be removed. | |
17 | * If this package is used in a product, Eric Young should be given attribution | |
18 | * as the author of the parts of the library used. | |
19 | * This can be in the form of a textual message at program startup or | |
20 | * in documentation (online or textual) provided with the package. | |
0f113f3e | 21 | * |
d02b48c6 RE |
22 | * Redistribution and use in source and binary forms, with or without |
23 | * modification, are permitted provided that the following conditions | |
24 | * are met: | |
25 | * 1. Redistributions of source code must retain the copyright | |
26 | * notice, this list of conditions and the following disclaimer. | |
27 | * 2. Redistributions in binary form must reproduce the above copyright | |
28 | * notice, this list of conditions and the following disclaimer in the | |
29 | * documentation and/or other materials provided with the distribution. | |
30 | * 3. All advertising materials mentioning features or use of this software | |
31 | * must display the following acknowledgement: | |
32 | * "This product includes cryptographic software written by | |
33 | * Eric Young (eay@cryptsoft.com)" | |
34 | * The word 'cryptographic' can be left out if the rouines from the library | |
35 | * being used are not cryptographic related :-). | |
0f113f3e | 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
d02b48c6 RE |
37 | * the apps directory (application code) you must include an acknowledgement: |
38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
0f113f3e | 39 | * |
d02b48c6 RE |
40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
50 | * SUCH DAMAGE. | |
0f113f3e | 51 | * |
d02b48c6 RE |
52 | * The licence and distribution terms for any publically available version or |
53 | * derivative of this code cannot be changed. i.e. this code cannot simply be | |
54 | * copied and put under another distribution licence | |
55 | * [including the GNU Public Licence.] | |
56 | */ | |
57 | ||
58 | #include <stdio.h> | |
59 | #include <string.h> | |
60 | #include <stdlib.h> | |
61 | #include "apps.h" | |
ec577822 BM |
62 | #include <openssl/bio.h> |
63 | #include <openssl/err.h> | |
64 | #include <openssl/evp.h> | |
65 | #include <openssl/objects.h> | |
66 | #include <openssl/x509.h> | |
67 | #include <openssl/pem.h> | |
52cfa397 | 68 | #include <openssl/hmac.h> |
d02b48c6 RE |
69 | |
70 | #undef BUFSIZE | |
0f113f3e | 71 | #define BUFSIZE 1024*8 |
d02b48c6 | 72 | |
d15711ef | 73 | int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout, |
0f113f3e MC |
74 | EVP_PKEY *key, unsigned char *sigin, int siglen, |
75 | const char *sig_name, const char *md_name, | |
76 | const char *file, BIO *bmd); | |
667ac4ec | 77 | |
7e1b7485 RS |
78 | typedef enum OPTION_choice { |
79 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
80 | OPT_C, OPT_R, OPT_RAND, OPT_OUT, OPT_SIGN, OPT_PASSIN, OPT_VERIFY, | |
81 | OPT_PRVERIFY, OPT_SIGNATURE, OPT_KEYFORM, OPT_ENGINE, OPT_ENGINE_IMPL, | |
82 | OPT_HEX, OPT_BINARY, OPT_DEBUG, OPT_FIPS_FINGERPRINT, | |
700b4a4a | 83 | OPT_HMAC, OPT_MAC, OPT_SIGOPT, OPT_MACOPT, |
7e1b7485 RS |
84 | OPT_DIGEST |
85 | } OPTION_CHOICE; | |
86 | ||
87 | OPTIONS dgst_options[] = { | |
88 | {OPT_HELP_STR, 1, '-', "Usage: %s [options] [file...]\n"}, | |
89 | {OPT_HELP_STR, 1, '-', | |
90 | " file... files to digest (default is stdin)\n"}, | |
91 | {"help", OPT_HELP, '-', "Display this summary"}, | |
92 | {"c", OPT_C, '-', "Print the digest with separating colons"}, | |
93 | {"r", OPT_R, '-', "Print the digest in coreutils format"}, | |
94 | {"rand", OPT_RAND, 's'}, | |
95 | {"out", OPT_OUT, '>', "Output to filename rather than stdout"}, | |
96 | {"passin", OPT_PASSIN, 's'}, | |
97 | {"sign", OPT_SIGN, '<', "Sign digest using private key in file"}, | |
98 | {"verify", OPT_VERIFY, '<', | |
99 | "Verify a signature using public key in file"}, | |
100 | {"prverify", OPT_PRVERIFY, '<', | |
101 | "Verify a signature using private key in file"}, | |
102 | {"signature", OPT_SIGNATURE, '<', "File with signature to verify"}, | |
103 | {"keyform", OPT_KEYFORM, 'f', "Key file format (PEM or ENGINE)"}, | |
7e1b7485 RS |
104 | {"hex", OPT_HEX, '-', "Print as hex dump"}, |
105 | {"binary", OPT_BINARY, '-', "Print in binary form"}, | |
106 | {"d", OPT_DEBUG, '-', "Print debug info"}, | |
107 | {"debug", OPT_DEBUG, '-'}, | |
108 | {"fips-fingerprint", OPT_FIPS_FINGERPRINT, '-'}, | |
7e1b7485 RS |
109 | {"hmac", OPT_HMAC, 's', "Create hashed MAC with key"}, |
110 | {"mac", OPT_MAC, 's', "Create MAC (not neccessarily HMAC)"}, | |
d175e8a6 MC |
111 | {"sigopt", OPT_SIGOPT, 's', "Signature parameter in n:v form"}, |
112 | {"macopt", OPT_MACOPT, 's', "MAC algorithm parameters in n:v form or key"}, | |
9c3bcfa0 | 113 | {"", OPT_DIGEST, '-', "Any supported digest"}, |
333b070e RS |
114 | #ifndef OPENSSL_NO_ENGINE |
115 | {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"}, | |
116 | {"engine_impl", OPT_ENGINE_IMPL, '-'}, | |
117 | #endif | |
7e1b7485 RS |
118 | {NULL} |
119 | }; | |
120 | ||
121 | int dgst_main(int argc, char **argv) | |
0f113f3e | 122 | { |
7e1b7485 | 123 | BIO *in = NULL, *inp, *bmd = NULL, *out = NULL; |
0f113f3e | 124 | ENGINE *e = NULL, *impl = NULL; |
7e1b7485 RS |
125 | EVP_PKEY *sigkey = NULL; |
126 | STACK_OF(OPENSSL_STRING) *sigopts = NULL, *macopts = NULL; | |
127 | char *hmac_key = NULL; | |
128 | char *mac_name = NULL; | |
129 | char *passinarg = NULL, *passin = NULL; | |
0f113f3e | 130 | const EVP_MD *md = NULL, *m; |
7e1b7485 | 131 | const char *outfile = NULL, *keyfile = NULL, *prog = NULL; |
0f113f3e | 132 | const char *sigfile = NULL, *randfile = NULL; |
7e1b7485 RS |
133 | OPTION_CHOICE o; |
134 | int separator = 0, debug = 0, keyform = FORMAT_PEM, siglen = 0; | |
700b4a4a | 135 | int i, ret = 1, out_bin = -1, want_pub = 0, do_verify = 0; |
7e1b7485 | 136 | unsigned char *buf = NULL, *sigbuf = NULL; |
0f113f3e | 137 | int engine_impl = 0; |
0f113f3e | 138 | |
7e1b7485 | 139 | prog = opt_progname(argv[0]); |
68dc6824 | 140 | buf = app_malloc(BUFSIZE, "I/O buffer"); |
7e1b7485 RS |
141 | md = EVP_get_digestbyname(prog); |
142 | ||
143 | prog = opt_init(argc, argv, dgst_options); | |
144 | while ((o = opt_next()) != OPT_EOF) { | |
145 | switch (o) { | |
146 | case OPT_EOF: | |
147 | case OPT_ERR: | |
148 | opthelp: | |
149 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
150 | goto end; | |
151 | case OPT_HELP: | |
152 | opt_help(dgst_options); | |
153 | ret = 0; | |
154 | goto end; | |
155 | case OPT_C: | |
0f113f3e | 156 | separator = 1; |
7e1b7485 RS |
157 | break; |
158 | case OPT_R: | |
0f113f3e | 159 | separator = 2; |
7e1b7485 RS |
160 | break; |
161 | case OPT_RAND: | |
162 | randfile = opt_arg(); | |
163 | break; | |
164 | case OPT_OUT: | |
165 | outfile = opt_arg(); | |
166 | break; | |
167 | case OPT_SIGN: | |
168 | keyfile = opt_arg(); | |
169 | break; | |
170 | case OPT_PASSIN: | |
171 | passinarg = opt_arg(); | |
172 | break; | |
173 | case OPT_VERIFY: | |
174 | keyfile = opt_arg(); | |
175 | want_pub = do_verify = 1; | |
176 | break; | |
177 | case OPT_PRVERIFY: | |
178 | keyfile = opt_arg(); | |
0f113f3e | 179 | do_verify = 1; |
7e1b7485 RS |
180 | break; |
181 | case OPT_SIGNATURE: | |
182 | sigfile = opt_arg(); | |
183 | break; | |
184 | case OPT_KEYFORM: | |
185 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform)) | |
186 | goto opthelp; | |
187 | break; | |
7e1b7485 | 188 | case OPT_ENGINE: |
333b070e | 189 | e = setup_engine(opt_arg(), 0); |
7e1b7485 RS |
190 | break; |
191 | case OPT_ENGINE_IMPL: | |
0f113f3e | 192 | engine_impl = 1; |
7e1b7485 | 193 | break; |
7e1b7485 | 194 | case OPT_HEX: |
0f113f3e | 195 | out_bin = 0; |
7e1b7485 RS |
196 | break; |
197 | case OPT_BINARY: | |
0f113f3e | 198 | out_bin = 1; |
7e1b7485 RS |
199 | break; |
200 | case OPT_DEBUG: | |
0f113f3e | 201 | debug = 1; |
7e1b7485 RS |
202 | break; |
203 | case OPT_FIPS_FINGERPRINT: | |
0f113f3e | 204 | hmac_key = "etaonrishdlcupfm"; |
7e1b7485 | 205 | break; |
7e1b7485 RS |
206 | case OPT_HMAC: |
207 | hmac_key = opt_arg(); | |
208 | break; | |
209 | case OPT_MAC: | |
210 | mac_name = opt_arg(); | |
211 | break; | |
212 | case OPT_SIGOPT: | |
0f113f3e MC |
213 | if (!sigopts) |
214 | sigopts = sk_OPENSSL_STRING_new_null(); | |
7e1b7485 RS |
215 | if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, opt_arg())) |
216 | goto opthelp; | |
217 | break; | |
218 | case OPT_MACOPT: | |
0f113f3e MC |
219 | if (!macopts) |
220 | macopts = sk_OPENSSL_STRING_new_null(); | |
7e1b7485 RS |
221 | if (!macopts || !sk_OPENSSL_STRING_push(macopts, opt_arg())) |
222 | goto opthelp; | |
223 | break; | |
224 | case OPT_DIGEST: | |
225 | if (!opt_md(opt_unknown(), &m)) | |
226 | goto opthelp; | |
0f113f3e | 227 | md = m; |
0f113f3e | 228 | break; |
7e1b7485 | 229 | } |
0f113f3e | 230 | } |
7e1b7485 RS |
231 | argc = opt_num_rest(); |
232 | argv = opt_rest(); | |
0f113f3e MC |
233 | |
234 | if (do_verify && !sigfile) { | |
235 | BIO_printf(bio_err, | |
236 | "No signature to verify: use the -signature option\n"); | |
237 | goto end; | |
238 | } | |
0f113f3e MC |
239 | if (engine_impl) |
240 | impl = e; | |
bb845ee0 | 241 | |
0f113f3e MC |
242 | in = BIO_new(BIO_s_file()); |
243 | bmd = BIO_new(BIO_f_md()); | |
be1477ad MC |
244 | if ((in == NULL) || (bmd == NULL)) { |
245 | ERR_print_errors(bio_err); | |
246 | goto end; | |
247 | } | |
248 | ||
0f113f3e MC |
249 | if (debug) { |
250 | BIO_set_callback(in, BIO_debug_callback); | |
251 | /* needed for windows 3.1 */ | |
252 | BIO_set_callback_arg(in, (char *)bio_err); | |
253 | } | |
254 | ||
7e1b7485 | 255 | if (!app_passwd(passinarg, NULL, &passin, NULL)) { |
0f113f3e MC |
256 | BIO_printf(bio_err, "Error getting password\n"); |
257 | goto end; | |
258 | } | |
259 | ||
0f113f3e MC |
260 | if (out_bin == -1) { |
261 | if (keyfile) | |
262 | out_bin = 1; | |
263 | else | |
264 | out_bin = 0; | |
265 | } | |
266 | ||
267 | if (randfile) | |
7e1b7485 | 268 | app_RAND_load_file(randfile, 0); |
0f113f3e | 269 | |
bdd58d98 | 270 | out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT); |
7e1b7485 | 271 | if (out == NULL) |
0f113f3e | 272 | goto end; |
7e1b7485 | 273 | |
0f113f3e MC |
274 | if ((! !mac_name + ! !keyfile + ! !hmac_key) > 1) { |
275 | BIO_printf(bio_err, "MAC and Signing key cannot both be specified\n"); | |
276 | goto end; | |
277 | } | |
278 | ||
279 | if (keyfile) { | |
280 | if (want_pub) | |
7e1b7485 | 281 | sigkey = load_pubkey(keyfile, keyform, 0, NULL, e, "key file"); |
0f113f3e | 282 | else |
7e1b7485 | 283 | sigkey = load_key(keyfile, keyform, 0, passin, e, "key file"); |
0f113f3e MC |
284 | if (!sigkey) { |
285 | /* | |
286 | * load_[pub]key() has already printed an appropriate message | |
287 | */ | |
288 | goto end; | |
289 | } | |
290 | } | |
291 | ||
292 | if (mac_name) { | |
293 | EVP_PKEY_CTX *mac_ctx = NULL; | |
294 | int r = 0; | |
7e1b7485 | 295 | if (!init_gen_str(&mac_ctx, mac_name, impl, 0)) |
0f113f3e MC |
296 | goto mac_end; |
297 | if (macopts) { | |
298 | char *macopt; | |
299 | for (i = 0; i < sk_OPENSSL_STRING_num(macopts); i++) { | |
300 | macopt = sk_OPENSSL_STRING_value(macopts, i); | |
301 | if (pkey_ctrl_string(mac_ctx, macopt) <= 0) { | |
302 | BIO_printf(bio_err, | |
303 | "MAC parameter error \"%s\"\n", macopt); | |
304 | ERR_print_errors(bio_err); | |
305 | goto mac_end; | |
306 | } | |
307 | } | |
308 | } | |
309 | if (EVP_PKEY_keygen(mac_ctx, &sigkey) <= 0) { | |
310 | BIO_puts(bio_err, "Error generating key\n"); | |
311 | ERR_print_errors(bio_err); | |
312 | goto mac_end; | |
313 | } | |
314 | r = 1; | |
315 | mac_end: | |
c5ba2d99 | 316 | EVP_PKEY_CTX_free(mac_ctx); |
0f113f3e MC |
317 | if (r == 0) |
318 | goto end; | |
319 | } | |
320 | ||
0f113f3e MC |
321 | if (hmac_key) { |
322 | sigkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, impl, | |
323 | (unsigned char *)hmac_key, -1); | |
324 | if (!sigkey) | |
325 | goto end; | |
326 | } | |
327 | ||
328 | if (sigkey) { | |
329 | EVP_MD_CTX *mctx = NULL; | |
330 | EVP_PKEY_CTX *pctx = NULL; | |
331 | int r; | |
332 | if (!BIO_get_md_ctx(bmd, &mctx)) { | |
333 | BIO_printf(bio_err, "Error getting context\n"); | |
334 | ERR_print_errors(bio_err); | |
335 | goto end; | |
336 | } | |
337 | if (do_verify) | |
338 | r = EVP_DigestVerifyInit(mctx, &pctx, md, impl, sigkey); | |
339 | else | |
340 | r = EVP_DigestSignInit(mctx, &pctx, md, impl, sigkey); | |
341 | if (!r) { | |
342 | BIO_printf(bio_err, "Error setting context\n"); | |
343 | ERR_print_errors(bio_err); | |
344 | goto end; | |
345 | } | |
346 | if (sigopts) { | |
347 | char *sigopt; | |
348 | for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++) { | |
349 | sigopt = sk_OPENSSL_STRING_value(sigopts, i); | |
350 | if (pkey_ctrl_string(pctx, sigopt) <= 0) { | |
351 | BIO_printf(bio_err, "parameter error \"%s\"\n", sigopt); | |
352 | ERR_print_errors(bio_err); | |
353 | goto end; | |
354 | } | |
355 | } | |
356 | } | |
357 | } | |
358 | /* we use md as a filter, reading from 'in' */ | |
359 | else { | |
360 | EVP_MD_CTX *mctx = NULL; | |
361 | if (!BIO_get_md_ctx(bmd, &mctx)) { | |
362 | BIO_printf(bio_err, "Error getting context\n"); | |
363 | ERR_print_errors(bio_err); | |
364 | goto end; | |
365 | } | |
366 | if (md == NULL) | |
f8547f62 | 367 | md = EVP_sha256(); |
0f113f3e | 368 | if (!EVP_DigestInit_ex(mctx, md, impl)) { |
7e1b7485 | 369 | BIO_printf(bio_err, "Error setting digest\n"); |
0f113f3e MC |
370 | ERR_print_errors(bio_err); |
371 | goto end; | |
372 | } | |
373 | } | |
374 | ||
375 | if (sigfile && sigkey) { | |
7e1b7485 | 376 | BIO *sigbio = BIO_new_file(sigfile, "rb"); |
0f113f3e MC |
377 | if (!sigbio) { |
378 | BIO_printf(bio_err, "Error opening signature file %s\n", sigfile); | |
379 | ERR_print_errors(bio_err); | |
380 | goto end; | |
381 | } | |
7e1b7485 | 382 | siglen = EVP_PKEY_size(sigkey); |
68dc6824 | 383 | sigbuf = app_malloc(siglen, "signature buffer"); |
0f113f3e MC |
384 | siglen = BIO_read(sigbio, sigbuf, siglen); |
385 | BIO_free(sigbio); | |
386 | if (siglen <= 0) { | |
387 | BIO_printf(bio_err, "Error reading signature file %s\n", sigfile); | |
388 | ERR_print_errors(bio_err); | |
389 | goto end; | |
390 | } | |
391 | } | |
392 | inp = BIO_push(bmd, in); | |
393 | ||
394 | if (md == NULL) { | |
395 | EVP_MD_CTX *tctx; | |
396 | BIO_get_md_ctx(bmd, &tctx); | |
397 | md = EVP_MD_CTX_md(tctx); | |
398 | } | |
399 | ||
400 | if (argc == 0) { | |
401 | BIO_set_fp(in, stdin, BIO_NOCLOSE); | |
7e1b7485 | 402 | ret = do_fp(out, buf, inp, separator, out_bin, sigkey, sigbuf, |
0f113f3e MC |
403 | siglen, NULL, NULL, "stdin", bmd); |
404 | } else { | |
405 | const char *md_name = NULL, *sig_name = NULL; | |
406 | if (!out_bin) { | |
407 | if (sigkey) { | |
408 | const EVP_PKEY_ASN1_METHOD *ameth; | |
409 | ameth = EVP_PKEY_get0_asn1(sigkey); | |
410 | if (ameth) | |
411 | EVP_PKEY_asn1_get0_info(NULL, NULL, | |
412 | NULL, NULL, &sig_name, ameth); | |
413 | } | |
414 | if (md) | |
415 | md_name = EVP_MD_name(md); | |
416 | } | |
7e1b7485 | 417 | ret = 0; |
0f113f3e MC |
418 | for (i = 0; i < argc; i++) { |
419 | int r; | |
420 | if (BIO_read_filename(in, argv[i]) <= 0) { | |
421 | perror(argv[i]); | |
7e1b7485 | 422 | ret++; |
0f113f3e MC |
423 | continue; |
424 | } else | |
425 | r = do_fp(out, buf, inp, separator, out_bin, sigkey, sigbuf, | |
426 | siglen, sig_name, md_name, argv[i], bmd); | |
427 | if (r) | |
7e1b7485 | 428 | ret = r; |
0f113f3e MC |
429 | (void)BIO_reset(bmd); |
430 | } | |
431 | } | |
432 | end: | |
4b45c6e5 | 433 | OPENSSL_clear_free(buf, BUFSIZE); |
ca3a82c3 | 434 | BIO_free(in); |
25aaa98a | 435 | OPENSSL_free(passin); |
0f113f3e MC |
436 | BIO_free_all(out); |
437 | EVP_PKEY_free(sigkey); | |
25aaa98a RS |
438 | sk_OPENSSL_STRING_free(sigopts); |
439 | sk_OPENSSL_STRING_free(macopts); | |
b548a1f1 | 440 | OPENSSL_free(sigbuf); |
ca3a82c3 | 441 | BIO_free(bmd); |
7e1b7485 | 442 | return (ret); |
0f113f3e | 443 | } |
d02b48c6 | 444 | |
d15711ef | 445 | int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout, |
0f113f3e MC |
446 | EVP_PKEY *key, unsigned char *sigin, int siglen, |
447 | const char *sig_name, const char *md_name, | |
448 | const char *file, BIO *bmd) | |
449 | { | |
450 | size_t len; | |
451 | int i; | |
452 | ||
453 | for (;;) { | |
454 | i = BIO_read(bp, (char *)buf, BUFSIZE); | |
455 | if (i < 0) { | |
456 | BIO_printf(bio_err, "Read Error in %s\n", file); | |
457 | ERR_print_errors(bio_err); | |
458 | return 1; | |
459 | } | |
460 | if (i == 0) | |
461 | break; | |
462 | } | |
463 | if (sigin) { | |
464 | EVP_MD_CTX *ctx; | |
465 | BIO_get_md_ctx(bp, &ctx); | |
466 | i = EVP_DigestVerifyFinal(ctx, sigin, (unsigned int)siglen); | |
467 | if (i > 0) | |
468 | BIO_printf(out, "Verified OK\n"); | |
469 | else if (i == 0) { | |
470 | BIO_printf(out, "Verification Failure\n"); | |
471 | return 1; | |
472 | } else { | |
473 | BIO_printf(bio_err, "Error Verifying Data\n"); | |
474 | ERR_print_errors(bio_err); | |
475 | return 1; | |
476 | } | |
477 | return 0; | |
478 | } | |
479 | if (key) { | |
480 | EVP_MD_CTX *ctx; | |
481 | BIO_get_md_ctx(bp, &ctx); | |
482 | len = BUFSIZE; | |
483 | if (!EVP_DigestSignFinal(ctx, buf, &len)) { | |
484 | BIO_printf(bio_err, "Error Signing Data\n"); | |
485 | ERR_print_errors(bio_err); | |
486 | return 1; | |
487 | } | |
488 | } else { | |
489 | len = BIO_gets(bp, (char *)buf, BUFSIZE); | |
490 | if ((int)len < 0) { | |
491 | ERR_print_errors(bio_err); | |
492 | return 1; | |
493 | } | |
494 | } | |
495 | ||
496 | if (binout) | |
497 | BIO_write(out, buf, len); | |
498 | else if (sep == 2) { | |
499 | for (i = 0; i < (int)len; i++) | |
500 | BIO_printf(out, "%02x", buf[i]); | |
501 | BIO_printf(out, " *%s\n", file); | |
502 | } else { | |
503 | if (sig_name) { | |
504 | BIO_puts(out, sig_name); | |
505 | if (md_name) | |
506 | BIO_printf(out, "-%s", md_name); | |
507 | BIO_printf(out, "(%s)= ", file); | |
508 | } else if (md_name) | |
509 | BIO_printf(out, "%s(%s)= ", md_name, file); | |
510 | else | |
511 | BIO_printf(out, "(%s)= ", file); | |
512 | for (i = 0; i < (int)len; i++) { | |
513 | if (sep && (i != 0)) | |
514 | BIO_printf(out, ":"); | |
515 | BIO_printf(out, "%02x", buf[i]); | |
516 | } | |
517 | BIO_printf(out, "\n"); | |
518 | } | |
519 | return 0; | |
520 | } |