xref: /onnv-gate/usr/src/common/openssl/apps/dgst.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* apps/dgst.c */
2*0Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3*0Sstevel@tonic-gate  * All rights reserved.
4*0Sstevel@tonic-gate  *
5*0Sstevel@tonic-gate  * This package is an SSL implementation written
6*0Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
7*0Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
10*0Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
11*0Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
12*0Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13*0Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
14*0Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15*0Sstevel@tonic-gate  *
16*0Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
17*0Sstevel@tonic-gate  * the code are not to be removed.
18*0Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
19*0Sstevel@tonic-gate  * as the author of the parts of the library used.
20*0Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
21*0Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
22*0Sstevel@tonic-gate  *
23*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
24*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
25*0Sstevel@tonic-gate  * are met:
26*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
27*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
28*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
29*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
30*0Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
31*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
32*0Sstevel@tonic-gate  *    must display the following acknowledgement:
33*0Sstevel@tonic-gate  *    "This product includes cryptographic software written by
34*0Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
35*0Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
36*0Sstevel@tonic-gate  *    being used are not cryptographic related :-).
37*0Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
38*0Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
39*0Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42*0Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44*0Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45*0Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46*0Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47*0Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49*0Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50*0Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51*0Sstevel@tonic-gate  * SUCH DAMAGE.
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
54*0Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55*0Sstevel@tonic-gate  * copied and put under another distribution licence
56*0Sstevel@tonic-gate  * [including the GNU Public Licence.]
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #include <stdio.h>
60*0Sstevel@tonic-gate #include <string.h>
61*0Sstevel@tonic-gate #include <stdlib.h>
62*0Sstevel@tonic-gate #include "apps.h"
63*0Sstevel@tonic-gate #include <openssl/bio.h>
64*0Sstevel@tonic-gate #include <openssl/err.h>
65*0Sstevel@tonic-gate #include <openssl/evp.h>
66*0Sstevel@tonic-gate #include <openssl/objects.h>
67*0Sstevel@tonic-gate #include <openssl/x509.h>
68*0Sstevel@tonic-gate #include <openssl/pem.h>
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate #undef BUFSIZE
71*0Sstevel@tonic-gate #define BUFSIZE	1024*8
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate #undef PROG
74*0Sstevel@tonic-gate #define PROG	dgst_main
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout,
77*0Sstevel@tonic-gate 	  EVP_PKEY *key, unsigned char *sigin, int siglen, const char *title,
78*0Sstevel@tonic-gate 	  const char *file);
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate int MAIN(int, char **);
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate int MAIN(int argc, char **argv)
83*0Sstevel@tonic-gate 	{
84*0Sstevel@tonic-gate 	ENGINE *e = NULL;
85*0Sstevel@tonic-gate 	unsigned char *buf=NULL;
86*0Sstevel@tonic-gate 	int i,err=0;
87*0Sstevel@tonic-gate 	const EVP_MD *md=NULL,*m;
88*0Sstevel@tonic-gate 	BIO *in=NULL,*inp;
89*0Sstevel@tonic-gate 	BIO *bmd=NULL;
90*0Sstevel@tonic-gate 	BIO *out = NULL;
91*0Sstevel@tonic-gate 	const char *name;
92*0Sstevel@tonic-gate #define PROG_NAME_SIZE  39
93*0Sstevel@tonic-gate 	char pname[PROG_NAME_SIZE+1];
94*0Sstevel@tonic-gate 	int separator=0;
95*0Sstevel@tonic-gate 	int debug=0;
96*0Sstevel@tonic-gate 	int keyform=FORMAT_PEM;
97*0Sstevel@tonic-gate 	const char *outfile = NULL, *keyfile = NULL;
98*0Sstevel@tonic-gate 	const char *sigfile = NULL, *randfile = NULL;
99*0Sstevel@tonic-gate 	int out_bin = -1, want_pub = 0, do_verify = 0;
100*0Sstevel@tonic-gate 	EVP_PKEY *sigkey = NULL;
101*0Sstevel@tonic-gate 	unsigned char *sigbuf = NULL;
102*0Sstevel@tonic-gate 	int siglen = 0;
103*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
104*0Sstevel@tonic-gate 	char *engine=NULL;
105*0Sstevel@tonic-gate #endif
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	apps_startup();
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	if ((buf=(unsigned char *)OPENSSL_malloc(BUFSIZE)) == NULL)
110*0Sstevel@tonic-gate 		{
111*0Sstevel@tonic-gate 		BIO_printf(bio_err,"out of memory\n");
112*0Sstevel@tonic-gate 		goto end;
113*0Sstevel@tonic-gate 		}
114*0Sstevel@tonic-gate 	if (bio_err == NULL)
115*0Sstevel@tonic-gate 		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
116*0Sstevel@tonic-gate 			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 	if (!load_config(bio_err, NULL))
119*0Sstevel@tonic-gate 		goto end;
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	/* first check the program name */
122*0Sstevel@tonic-gate 	program_name(argv[0],pname,sizeof pname);
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 	md=EVP_get_digestbyname(pname);
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	argc--;
127*0Sstevel@tonic-gate 	argv++;
128*0Sstevel@tonic-gate 	while (argc > 0)
129*0Sstevel@tonic-gate 		{
130*0Sstevel@tonic-gate 		if ((*argv)[0] != '-') break;
131*0Sstevel@tonic-gate 		if (strcmp(*argv,"-c") == 0)
132*0Sstevel@tonic-gate 			separator=1;
133*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-rand") == 0)
134*0Sstevel@tonic-gate 			{
135*0Sstevel@tonic-gate 			if (--argc < 1) break;
136*0Sstevel@tonic-gate 			randfile=*(++argv);
137*0Sstevel@tonic-gate 			}
138*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-out") == 0)
139*0Sstevel@tonic-gate 			{
140*0Sstevel@tonic-gate 			if (--argc < 1) break;
141*0Sstevel@tonic-gate 			outfile=*(++argv);
142*0Sstevel@tonic-gate 			}
143*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-sign") == 0)
144*0Sstevel@tonic-gate 			{
145*0Sstevel@tonic-gate 			if (--argc < 1) break;
146*0Sstevel@tonic-gate 			keyfile=*(++argv);
147*0Sstevel@tonic-gate 			}
148*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-verify") == 0)
149*0Sstevel@tonic-gate 			{
150*0Sstevel@tonic-gate 			if (--argc < 1) break;
151*0Sstevel@tonic-gate 			keyfile=*(++argv);
152*0Sstevel@tonic-gate 			want_pub = 1;
153*0Sstevel@tonic-gate 			do_verify = 1;
154*0Sstevel@tonic-gate 			}
155*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-prverify") == 0)
156*0Sstevel@tonic-gate 			{
157*0Sstevel@tonic-gate 			if (--argc < 1) break;
158*0Sstevel@tonic-gate 			keyfile=*(++argv);
159*0Sstevel@tonic-gate 			do_verify = 1;
160*0Sstevel@tonic-gate 			}
161*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-signature") == 0)
162*0Sstevel@tonic-gate 			{
163*0Sstevel@tonic-gate 			if (--argc < 1) break;
164*0Sstevel@tonic-gate 			sigfile=*(++argv);
165*0Sstevel@tonic-gate 			}
166*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-keyform") == 0)
167*0Sstevel@tonic-gate 			{
168*0Sstevel@tonic-gate 			if (--argc < 1) break;
169*0Sstevel@tonic-gate 			keyform=str2fmt(*(++argv));
170*0Sstevel@tonic-gate 			}
171*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
172*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-engine") == 0)
173*0Sstevel@tonic-gate 			{
174*0Sstevel@tonic-gate 			if (--argc < 1) break;
175*0Sstevel@tonic-gate 			engine= *(++argv);
176*0Sstevel@tonic-gate 			}
177*0Sstevel@tonic-gate #endif
178*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-hex") == 0)
179*0Sstevel@tonic-gate 			out_bin = 0;
180*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-binary") == 0)
181*0Sstevel@tonic-gate 			out_bin = 1;
182*0Sstevel@tonic-gate 		else if (strcmp(*argv,"-d") == 0)
183*0Sstevel@tonic-gate 			debug=1;
184*0Sstevel@tonic-gate 		else if ((m=EVP_get_digestbyname(&((*argv)[1]))) != NULL)
185*0Sstevel@tonic-gate 			md=m;
186*0Sstevel@tonic-gate 		else
187*0Sstevel@tonic-gate 			break;
188*0Sstevel@tonic-gate 		argc--;
189*0Sstevel@tonic-gate 		argv++;
190*0Sstevel@tonic-gate 		}
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 	if (md == NULL)
193*0Sstevel@tonic-gate 		md=EVP_md5();
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	if(do_verify && !sigfile) {
196*0Sstevel@tonic-gate 		BIO_printf(bio_err, "No signature to verify: use the -signature option\n");
197*0Sstevel@tonic-gate 		err = 1;
198*0Sstevel@tonic-gate 		goto end;
199*0Sstevel@tonic-gate 	}
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	if ((argc > 0) && (argv[0][0] == '-')) /* bad option */
202*0Sstevel@tonic-gate 		{
203*0Sstevel@tonic-gate 		BIO_printf(bio_err,"unknown option '%s'\n",*argv);
204*0Sstevel@tonic-gate 		BIO_printf(bio_err,"options are\n");
205*0Sstevel@tonic-gate 		BIO_printf(bio_err,"-c              to output the digest with separating colons\n");
206*0Sstevel@tonic-gate 		BIO_printf(bio_err,"-d              to output debug info\n");
207*0Sstevel@tonic-gate 		BIO_printf(bio_err,"-hex            output as hex dump\n");
208*0Sstevel@tonic-gate 		BIO_printf(bio_err,"-binary         output in binary form\n");
209*0Sstevel@tonic-gate 		BIO_printf(bio_err,"-sign   file    sign digest using private key in file\n");
210*0Sstevel@tonic-gate 		BIO_printf(bio_err,"-verify file    verify a signature using public key in file\n");
211*0Sstevel@tonic-gate 		BIO_printf(bio_err,"-prverify file  verify a signature using private key in file\n");
212*0Sstevel@tonic-gate 		BIO_printf(bio_err,"-keyform arg    key file format (PEM or ENGINE)\n");
213*0Sstevel@tonic-gate 		BIO_printf(bio_err,"-signature file signature to verify\n");
214*0Sstevel@tonic-gate 		BIO_printf(bio_err,"-binary         output in binary form\n");
215*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
216*0Sstevel@tonic-gate 		BIO_printf(bio_err,"-engine e       use engine e, possibly a hardware device.\n");
217*0Sstevel@tonic-gate #endif
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 		BIO_printf(bio_err,"-%3s to use the %s message digest algorithm (default)\n",
220*0Sstevel@tonic-gate 			LN_md5,LN_md5);
221*0Sstevel@tonic-gate 		BIO_printf(bio_err,"-%3s to use the %s message digest algorithm\n",
222*0Sstevel@tonic-gate 			LN_md4,LN_md4);
223*0Sstevel@tonic-gate 		BIO_printf(bio_err,"-%3s to use the %s message digest algorithm\n",
224*0Sstevel@tonic-gate 			LN_md2,LN_md2);
225*0Sstevel@tonic-gate 		BIO_printf(bio_err,"-%3s to use the %s message digest algorithm\n",
226*0Sstevel@tonic-gate 			LN_sha1,LN_sha1);
227*0Sstevel@tonic-gate 		BIO_printf(bio_err,"-%3s to use the %s message digest algorithm\n",
228*0Sstevel@tonic-gate 			LN_sha,LN_sha);
229*0Sstevel@tonic-gate 		BIO_printf(bio_err,"-%3s to use the %s message digest algorithm\n",
230*0Sstevel@tonic-gate 			LN_mdc2,LN_mdc2);
231*0Sstevel@tonic-gate 		BIO_printf(bio_err,"-%3s to use the %s message digest algorithm\n",
232*0Sstevel@tonic-gate 			LN_ripemd160,LN_ripemd160);
233*0Sstevel@tonic-gate 		err=1;
234*0Sstevel@tonic-gate 		goto end;
235*0Sstevel@tonic-gate 		}
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
238*0Sstevel@tonic-gate         e = setup_engine(bio_err, engine, 0);
239*0Sstevel@tonic-gate #endif
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 	in=BIO_new(BIO_s_file());
242*0Sstevel@tonic-gate 	bmd=BIO_new(BIO_f_md());
243*0Sstevel@tonic-gate 	if (debug)
244*0Sstevel@tonic-gate 		{
245*0Sstevel@tonic-gate 		BIO_set_callback(in,BIO_debug_callback);
246*0Sstevel@tonic-gate 		/* needed for windows 3.1 */
247*0Sstevel@tonic-gate 		BIO_set_callback_arg(in,bio_err);
248*0Sstevel@tonic-gate 		}
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 	if ((in == NULL) || (bmd == NULL))
251*0Sstevel@tonic-gate 		{
252*0Sstevel@tonic-gate 		ERR_print_errors(bio_err);
253*0Sstevel@tonic-gate 		goto end;
254*0Sstevel@tonic-gate 		}
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 	if(out_bin == -1) {
257*0Sstevel@tonic-gate 		if(keyfile) out_bin = 1;
258*0Sstevel@tonic-gate 		else out_bin = 0;
259*0Sstevel@tonic-gate 	}
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 	if(randfile)
262*0Sstevel@tonic-gate 		app_RAND_load_file(randfile, bio_err, 0);
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 	if(outfile) {
265*0Sstevel@tonic-gate 		if(out_bin)
266*0Sstevel@tonic-gate 			out = BIO_new_file(outfile, "wb");
267*0Sstevel@tonic-gate 		else    out = BIO_new_file(outfile, "w");
268*0Sstevel@tonic-gate 	} else {
269*0Sstevel@tonic-gate 		out = BIO_new_fp(stdout, BIO_NOCLOSE);
270*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
271*0Sstevel@tonic-gate 		{
272*0Sstevel@tonic-gate 		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
273*0Sstevel@tonic-gate 		out = BIO_push(tmpbio, out);
274*0Sstevel@tonic-gate 		}
275*0Sstevel@tonic-gate #endif
276*0Sstevel@tonic-gate 	}
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 	if(!out) {
279*0Sstevel@tonic-gate 		BIO_printf(bio_err, "Error opening output file %s\n",
280*0Sstevel@tonic-gate 					outfile ? outfile : "(stdout)");
281*0Sstevel@tonic-gate 		ERR_print_errors(bio_err);
282*0Sstevel@tonic-gate 		goto end;
283*0Sstevel@tonic-gate 	}
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate 	if(keyfile)
286*0Sstevel@tonic-gate 		{
287*0Sstevel@tonic-gate 		if (want_pub)
288*0Sstevel@tonic-gate 			sigkey = load_pubkey(bio_err, keyfile, keyform, 0, NULL,
289*0Sstevel@tonic-gate 				e, "key file");
290*0Sstevel@tonic-gate 		else
291*0Sstevel@tonic-gate 			sigkey = load_key(bio_err, keyfile, keyform, 0, NULL,
292*0Sstevel@tonic-gate 				e, "key file");
293*0Sstevel@tonic-gate 		if (!sigkey)
294*0Sstevel@tonic-gate 			{
295*0Sstevel@tonic-gate 			/* load_[pub]key() has already printed an appropriate
296*0Sstevel@tonic-gate 			   message */
297*0Sstevel@tonic-gate 			goto end;
298*0Sstevel@tonic-gate 			}
299*0Sstevel@tonic-gate 		}
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate 	if(sigfile && sigkey) {
302*0Sstevel@tonic-gate 		BIO *sigbio;
303*0Sstevel@tonic-gate 		sigbio = BIO_new_file(sigfile, "rb");
304*0Sstevel@tonic-gate 		siglen = EVP_PKEY_size(sigkey);
305*0Sstevel@tonic-gate 		sigbuf = OPENSSL_malloc(siglen);
306*0Sstevel@tonic-gate 		if(!sigbio) {
307*0Sstevel@tonic-gate 			BIO_printf(bio_err, "Error opening signature file %s\n",
308*0Sstevel@tonic-gate 								sigfile);
309*0Sstevel@tonic-gate 			ERR_print_errors(bio_err);
310*0Sstevel@tonic-gate 			goto end;
311*0Sstevel@tonic-gate 		}
312*0Sstevel@tonic-gate 		siglen = BIO_read(sigbio, sigbuf, siglen);
313*0Sstevel@tonic-gate 		BIO_free(sigbio);
314*0Sstevel@tonic-gate 		if(siglen <= 0) {
315*0Sstevel@tonic-gate 			BIO_printf(bio_err, "Error reading signature file %s\n",
316*0Sstevel@tonic-gate 								sigfile);
317*0Sstevel@tonic-gate 			ERR_print_errors(bio_err);
318*0Sstevel@tonic-gate 			goto end;
319*0Sstevel@tonic-gate 		}
320*0Sstevel@tonic-gate 	}
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 
324*0Sstevel@tonic-gate 	/* we use md as a filter, reading from 'in' */
325*0Sstevel@tonic-gate 	BIO_set_md(bmd,md);
326*0Sstevel@tonic-gate 	inp=BIO_push(bmd,in);
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 	if (argc == 0)
329*0Sstevel@tonic-gate 		{
330*0Sstevel@tonic-gate 		BIO_set_fp(in,stdin,BIO_NOCLOSE);
331*0Sstevel@tonic-gate 		err=do_fp(out, buf,inp,separator, out_bin, sigkey, sigbuf,
332*0Sstevel@tonic-gate 			  siglen,"","(stdin)");
333*0Sstevel@tonic-gate 		}
334*0Sstevel@tonic-gate 	else
335*0Sstevel@tonic-gate 		{
336*0Sstevel@tonic-gate 		name=OBJ_nid2sn(md->type);
337*0Sstevel@tonic-gate 		for (i=0; i<argc; i++)
338*0Sstevel@tonic-gate 			{
339*0Sstevel@tonic-gate 			char *tmp,*tofree=NULL;
340*0Sstevel@tonic-gate 			int r;
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate 			if (BIO_read_filename(in,argv[i]) <= 0)
343*0Sstevel@tonic-gate 				{
344*0Sstevel@tonic-gate 				perror(argv[i]);
345*0Sstevel@tonic-gate 				err++;
346*0Sstevel@tonic-gate 				continue;
347*0Sstevel@tonic-gate 				}
348*0Sstevel@tonic-gate 			if(!out_bin)
349*0Sstevel@tonic-gate 				{
350*0Sstevel@tonic-gate 				size_t len = strlen(name)+strlen(argv[i])+5;
351*0Sstevel@tonic-gate 				tmp=tofree=OPENSSL_malloc(len);
352*0Sstevel@tonic-gate 				BIO_snprintf(tmp,len,"%s(%s)= ",name,argv[i]);
353*0Sstevel@tonic-gate 				}
354*0Sstevel@tonic-gate 			else
355*0Sstevel@tonic-gate 				tmp="";
356*0Sstevel@tonic-gate 			r=do_fp(out,buf,inp,separator,out_bin,sigkey,sigbuf,
357*0Sstevel@tonic-gate 				siglen,tmp,argv[i]);
358*0Sstevel@tonic-gate 			if(r)
359*0Sstevel@tonic-gate 			    err=r;
360*0Sstevel@tonic-gate 			if(tofree)
361*0Sstevel@tonic-gate 				OPENSSL_free(tofree);
362*0Sstevel@tonic-gate 			(void)BIO_reset(bmd);
363*0Sstevel@tonic-gate 			}
364*0Sstevel@tonic-gate 		}
365*0Sstevel@tonic-gate end:
366*0Sstevel@tonic-gate 	if (buf != NULL)
367*0Sstevel@tonic-gate 		{
368*0Sstevel@tonic-gate 		OPENSSL_cleanse(buf,BUFSIZE);
369*0Sstevel@tonic-gate 		OPENSSL_free(buf);
370*0Sstevel@tonic-gate 		}
371*0Sstevel@tonic-gate 	if (in != NULL) BIO_free(in);
372*0Sstevel@tonic-gate 	BIO_free_all(out);
373*0Sstevel@tonic-gate 	EVP_PKEY_free(sigkey);
374*0Sstevel@tonic-gate 	if(sigbuf) OPENSSL_free(sigbuf);
375*0Sstevel@tonic-gate 	if (bmd != NULL) BIO_free(bmd);
376*0Sstevel@tonic-gate 	apps_shutdown();
377*0Sstevel@tonic-gate 	OPENSSL_EXIT(err);
378*0Sstevel@tonic-gate 	}
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout,
381*0Sstevel@tonic-gate 	  EVP_PKEY *key, unsigned char *sigin, int siglen, const char *title,
382*0Sstevel@tonic-gate 	  const char *file)
383*0Sstevel@tonic-gate 	{
384*0Sstevel@tonic-gate 	int len;
385*0Sstevel@tonic-gate 	int i;
386*0Sstevel@tonic-gate 
387*0Sstevel@tonic-gate 	for (;;)
388*0Sstevel@tonic-gate 		{
389*0Sstevel@tonic-gate 		i=BIO_read(bp,(char *)buf,BUFSIZE);
390*0Sstevel@tonic-gate 		if(i < 0)
391*0Sstevel@tonic-gate 			{
392*0Sstevel@tonic-gate 			BIO_printf(bio_err, "Read Error in %s\n",file);
393*0Sstevel@tonic-gate 			ERR_print_errors(bio_err);
394*0Sstevel@tonic-gate 			return 1;
395*0Sstevel@tonic-gate 			}
396*0Sstevel@tonic-gate 		if (i == 0) break;
397*0Sstevel@tonic-gate 		}
398*0Sstevel@tonic-gate 	if(sigin)
399*0Sstevel@tonic-gate 		{
400*0Sstevel@tonic-gate 		EVP_MD_CTX *ctx;
401*0Sstevel@tonic-gate 		BIO_get_md_ctx(bp, &ctx);
402*0Sstevel@tonic-gate 		i = EVP_VerifyFinal(ctx, sigin, (unsigned int)siglen, key);
403*0Sstevel@tonic-gate 		if(i > 0)
404*0Sstevel@tonic-gate 			BIO_printf(out, "Verified OK\n");
405*0Sstevel@tonic-gate 		else if(i == 0)
406*0Sstevel@tonic-gate 			{
407*0Sstevel@tonic-gate 			BIO_printf(out, "Verification Failure\n");
408*0Sstevel@tonic-gate 			return 1;
409*0Sstevel@tonic-gate 			}
410*0Sstevel@tonic-gate 		else
411*0Sstevel@tonic-gate 			{
412*0Sstevel@tonic-gate 			BIO_printf(bio_err, "Error Verifying Data\n");
413*0Sstevel@tonic-gate 			ERR_print_errors(bio_err);
414*0Sstevel@tonic-gate 			return 1;
415*0Sstevel@tonic-gate 			}
416*0Sstevel@tonic-gate 		return 0;
417*0Sstevel@tonic-gate 		}
418*0Sstevel@tonic-gate 	if(key)
419*0Sstevel@tonic-gate 		{
420*0Sstevel@tonic-gate 		EVP_MD_CTX *ctx;
421*0Sstevel@tonic-gate 		BIO_get_md_ctx(bp, &ctx);
422*0Sstevel@tonic-gate 		if(!EVP_SignFinal(ctx, buf, (unsigned int *)&len, key))
423*0Sstevel@tonic-gate 			{
424*0Sstevel@tonic-gate 			BIO_printf(bio_err, "Error Signing Data\n");
425*0Sstevel@tonic-gate 			ERR_print_errors(bio_err);
426*0Sstevel@tonic-gate 			return 1;
427*0Sstevel@tonic-gate 			}
428*0Sstevel@tonic-gate 		}
429*0Sstevel@tonic-gate 	else
430*0Sstevel@tonic-gate 		len=BIO_gets(bp,(char *)buf,BUFSIZE);
431*0Sstevel@tonic-gate 
432*0Sstevel@tonic-gate 	if(binout) BIO_write(out, buf, len);
433*0Sstevel@tonic-gate 	else
434*0Sstevel@tonic-gate 		{
435*0Sstevel@tonic-gate 		BIO_write(out,title,strlen(title));
436*0Sstevel@tonic-gate 		for (i=0; i<len; i++)
437*0Sstevel@tonic-gate 			{
438*0Sstevel@tonic-gate 			if (sep && (i != 0))
439*0Sstevel@tonic-gate 				BIO_printf(out, ":");
440*0Sstevel@tonic-gate 			BIO_printf(out, "%02x",buf[i]);
441*0Sstevel@tonic-gate 			}
442*0Sstevel@tonic-gate 		BIO_printf(out, "\n");
443*0Sstevel@tonic-gate 		}
444*0Sstevel@tonic-gate 	return 0;
445*0Sstevel@tonic-gate 	}
446*0Sstevel@tonic-gate 
447