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