~ubuntu-branches/ubuntu/maverick/openssl/maverick

« back to all changes in this revision

Viewing changes to apps/dgst.c

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Martin
  • Date: 2004-12-16 18:41:29 UTC
  • mto: (11.1.1 lenny)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20041216184129-z7xjkul57mh1jiha
Tags: upstream-0.9.7e
ImportĀ upstreamĀ versionĀ 0.9.7e

Show diffs side-by-side

added added

removed removed

Lines of Context:
66
66
#include <openssl/objects.h>
67
67
#include <openssl/x509.h>
68
68
#include <openssl/pem.h>
 
69
#include <openssl/hmac.h>
69
70
 
70
71
#undef BUFSIZE
71
72
#define BUFSIZE 1024*8
73
74
#undef PROG
74
75
#define PROG    dgst_main
75
76
 
 
77
static HMAC_CTX hmac_ctx;
 
78
 
76
79
int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout,
77
80
          EVP_PKEY *key, unsigned char *sigin, int siglen, const char *title,
78
 
          const char *file);
 
81
          const char *file,BIO *bmd,const char *hmac_key);
79
82
 
80
83
int MAIN(int, char **);
81
84
 
103
106
#ifndef OPENSSL_NO_ENGINE
104
107
        char *engine=NULL;
105
108
#endif
 
109
        char *hmac_key=NULL;
106
110
 
107
111
        apps_startup();
108
112
 
181
185
                        out_bin = 1;
182
186
                else if (strcmp(*argv,"-d") == 0)
183
187
                        debug=1;
 
188
                else if (!strcmp(*argv,"-hmac"))
 
189
                        {
 
190
                        if (--argc < 1)
 
191
                                break;
 
192
                        hmac_key=*++argv;
 
193
                        }
184
194
                else if ((m=EVP_get_digestbyname(&((*argv)[1]))) != NULL)
185
195
                        md=m;
186
196
                else
235
245
                }
236
246
 
237
247
#ifndef OPENSSL_NO_ENGINE
238
 
        e = setup_engine(bio_err, engine, 0);
 
248
        e = setup_engine(bio_err, engine, 0);
239
249
#endif
240
250
 
241
251
        in=BIO_new(BIO_s_file());
318
328
                        goto end;
319
329
                }
320
330
        }
 
331
 
 
332
        /* we use md as a filter, reading from 'in' */
 
333
        if (!BIO_set_md(bmd,md))
 
334
                {
 
335
                BIO_printf(bio_err, "Error setting digest %s\n", pname);
 
336
                ERR_print_errors(bio_err);
 
337
                goto end;
 
338
                }
321
339
                
322
 
 
323
 
 
324
 
        /* we use md as a filter, reading from 'in' */
325
 
        BIO_set_md(bmd,md);
326
340
        inp=BIO_push(bmd,in);
327
341
 
328
342
        if (argc == 0)
329
343
                {
330
344
                BIO_set_fp(in,stdin,BIO_NOCLOSE);
331
345
                err=do_fp(out, buf,inp,separator, out_bin, sigkey, sigbuf,
332
 
                          siglen,"","(stdin)");
 
346
                          siglen,"","(stdin)",bmd,hmac_key);
333
347
                }
334
348
        else
335
349
                {
347
361
                                }
348
362
                        if(!out_bin)
349
363
                                {
350
 
                                size_t len = strlen(name)+strlen(argv[i])+5;
 
364
                                size_t len = strlen(name)+strlen(argv[i])+(hmac_key ? 5 : 0)+5;
351
365
                                tmp=tofree=OPENSSL_malloc(len);
352
 
                                BIO_snprintf(tmp,len,"%s(%s)= ",name,argv[i]);
 
366
                                BIO_snprintf(tmp,len,"%s%s(%s)= ",
 
367
                                                         hmac_key ? "HMAC-" : "",name,argv[i]);
353
368
                                }
354
369
                        else
355
370
                                tmp="";
356
371
                        r=do_fp(out,buf,inp,separator,out_bin,sigkey,sigbuf,
357
 
                                siglen,tmp,argv[i]);
 
372
                                siglen,tmp,argv[i],bmd,hmac_key);
358
373
                        if(r)
359
374
                            err=r;
360
375
                        if(tofree)
379
394
 
380
395
int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout,
381
396
          EVP_PKEY *key, unsigned char *sigin, int siglen, const char *title,
382
 
          const char *file)
 
397
          const char *file,BIO *bmd,const char *hmac_key)
383
398
        {
384
 
        int len;
 
399
        unsigned int len;
385
400
        int i;
386
 
 
 
401
        EVP_MD_CTX *md_ctx;
 
402
 
 
403
        if (hmac_key)
 
404
                {
 
405
                EVP_MD *md;
 
406
 
 
407
                BIO_get_md(bmd,&md);
 
408
                HMAC_Init(&hmac_ctx,hmac_key,strlen(hmac_key),md);
 
409
                BIO_get_md_ctx(bmd,&md_ctx);
 
410
                BIO_set_md_ctx(bmd,&hmac_ctx.md_ctx);
 
411
                }
387
412
        for (;;)
388
413
                {
389
414
                i=BIO_read(bp,(char *)buf,BUFSIZE);
426
451
                        return 1;
427
452
                        }
428
453
                }
 
454
        else if(hmac_key)
 
455
                {
 
456
                HMAC_Final(&hmac_ctx,buf,&len);
 
457
                HMAC_CTX_cleanup(&hmac_ctx);
 
458
                }
429
459
        else
430
460
                len=BIO_gets(bp,(char *)buf,BUFSIZE);
431
461
 
433
463
        else 
434
464
                {
435
465
                BIO_write(out,title,strlen(title));
436
 
                for (i=0; i<len; i++)
 
466
                for (i=0; (unsigned int)i<len; i++)
437
467
                        {
438
468
                        if (sep && (i != 0))
439
469
                                BIO_printf(out, ":");
441
471
                        }
442
472
                BIO_printf(out, "\n");
443
473
                }
 
474
        if (hmac_key)
 
475
                {
 
476
                BIO_set_md_ctx(bmd,md_ctx);
 
477
                }
444
478
        return 0;
445
479
        }
446
480