~ubuntu-branches/ubuntu/natty/gnupg2/natty

« back to all changes in this revision

Viewing changes to sm/sign.c

  • Committer: Bazaar Package Importer
  • Author(s): Eric Dorland
  • Date: 2009-03-08 22:46:47 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20090308224647-gq17gatcl71lrc2k
Tags: 2.0.11-1
* New upstream release. (Closes: #496663)
* debian/control: Make the description a little more distinctive than
  gnupg v1's. Thanks Jari Aalto. (Closes: #496323)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* sign.c - Sign a message
2
 
 *      Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
 
2
 *      Copyright (C) 2001, 2002, 2003, 2008 Free Software Foundation, Inc.
3
3
 *
4
4
 * This file is part of GnuPG.
5
5
 *
396
396
      release_signerlist = 1;
397
397
    }
398
398
 
 
399
  /* Figure out the hash algorithm to use. We do not want to use the
 
400
     one for the certificate but if possible an OID for the plain
 
401
     algorithm.  */
 
402
  for (i=0, cl=signerlist; cl; cl = cl->next, i++)
 
403
    {
 
404
      const char *oid = ksba_cert_get_digest_algo (cl->cert);
 
405
 
 
406
      cl->hash_algo = oid ? gcry_md_map_name (oid) : 0;
 
407
      switch (cl->hash_algo)
 
408
        {
 
409
        case GCRY_MD_SHA1:   oid = "1.3.14.3.2.26"; break;
 
410
        case GCRY_MD_RMD160: oid = "1.3.36.3.2.1"; break;
 
411
        case GCRY_MD_SHA224: oid = "2.16.840.1.101.3.4.2.4"; break;
 
412
        case GCRY_MD_SHA256: oid = "2.16.840.1.101.3.4.2.1"; break;
 
413
        case GCRY_MD_SHA384: oid = "2.16.840.1.101.3.4.2.2"; break;
 
414
        case GCRY_MD_SHA512: oid = "2.16.840.1.101.3.4.2.3"; break;
 
415
/*         case GCRY_MD_WHIRLPOOL: oid = "No OID yet"; break; */
 
416
              
 
417
        case GCRY_MD_MD5:  /* We don't want to use MD5.  */
 
418
        case 0:            /* No algorithm found in cert.  */
 
419
        default:           /* Other algorithms.  */
 
420
          log_info (_("hash algorithm %d (%s) for signer %d not supported;"
 
421
                      " using %s\n"),
 
422
                    cl->hash_algo, oid? oid: "?", i, 
 
423
                    gcry_md_algo_name (GCRY_MD_SHA1));
 
424
          cl->hash_algo = GCRY_MD_SHA1;
 
425
          oid = "1.3.14.3.2.26";
 
426
          break;
 
427
        }
 
428
      cl->hash_algo_oid = oid;
 
429
    }
 
430
  if (opt.verbose)
 
431
    {
 
432
      for (i=0, cl=signerlist; cl; cl = cl->next, i++)
 
433
        log_info (_("hash algorithm used for signer %d: %s (%s)\n"), 
 
434
                  i, gcry_md_algo_name (cl->hash_algo), cl->hash_algo_oid);
 
435
    }
 
436
 
399
437
 
400
438
  /* Gather certificates of signers and store them in the CMS object. */
401
439
  for (cl=signerlist; cl; cl = cl->next)
419
457
          goto leave;
420
458
        }
421
459
      /* Set the hash algorithm we are going to use */
422
 
      err = ksba_cms_add_digest_algo (cms, "1.3.14.3.2.26" /*SHA-1*/);
 
460
      err = ksba_cms_add_digest_algo (cms, cl->hash_algo_oid);
423
461
      if (err)
424
462
        {
425
463
          log_debug ("ksba_cms_add_digest_algo failed: %s\n",
458
496
        }
459
497
    }
460
498
  
461
 
  /* Prepare hashing (actually we are figuring out what we have set above)*/
 
499
  /* Prepare hashing (actually we are figuring out what we have set
 
500
     above). */
462
501
  rc = gcry_md_open (&data_md, 0, 0);
463
502
  if (rc)
464
503
    {
474
513
      if (!algo)
475
514
        {
476
515
          log_error ("unknown hash algorithm `%s'\n", algoid? algoid:"?");
477
 
          if (algoid
478
 
              && (  !strcmp (algoid, "1.2.840.113549.1.1.2")
479
 
                    ||!strcmp (algoid, "1.2.840.113549.2.2")))
480
 
            log_info (_("(this is the MD2 algorithm)\n"));
481
516
          rc = gpg_error (GPG_ERR_BUG);
482
517
          goto leave;
483
518
        }
485
520
    }
486
521
 
487
522
  if (detached)
488
 
    { /* we hash the data right now so that we can store the message
 
523
    { /* We hash the data right now so that we can store the message
489
524
         digest.  ksba_cms_build() takes this as an flag that detached
490
525
         data is expected. */
491
526
      unsigned char *digest;
492
527
      size_t digest_len;
493
 
      /* Fixme do this for all signers and get the algo to use from
494
 
         the signer's certificate - does not make much sense, but we
495
 
         should do this consistent as we have already done it above. */
496
 
      algo = GCRY_MD_SHA1; 
 
528
 
497
529
      hash_data (data_fd, data_md);
498
 
      digest = gcry_md_read (data_md, algo);
499
 
      digest_len = gcry_md_get_algo_dlen (algo);
500
 
      if ( !digest || !digest_len)
501
 
        {
502
 
          log_error ("problem getting the hash of the data\n");
503
 
          rc = gpg_error (GPG_ERR_BUG);
504
 
          goto leave;
505
 
        }
506
530
      for (cl=signerlist,signer=0; cl; cl = cl->next, signer++)
507
531
        {
 
532
          digest = gcry_md_read (data_md, cl->hash_algo);
 
533
          digest_len = gcry_md_get_algo_dlen (cl->hash_algo);
 
534
          if ( !digest || !digest_len )
 
535
            {
 
536
              log_error ("problem getting the hash of the data\n");
 
537
              rc = gpg_error (GPG_ERR_BUG);
 
538
              goto leave;
 
539
            }
508
540
          err = ksba_cms_set_message_digest (cms, signer, digest, digest_len);
509
541
          if (err)
510
542
            {
559
591
        }
560
592
 
561
593
      if (stopreason == KSBA_SR_BEGIN_DATA)
562
 
        { /* hash the data and store the message digest */
 
594
        { 
 
595
          /* Hash the data and store the message digest. */
563
596
          unsigned char *digest;
564
597
          size_t digest_len;
565
598
 
566
599
          assert (!detached);
567
 
          /* Fixme: get the algo to use from the signer's certificate
568
 
             - does not make much sense, but we should do this
569
 
             consistent as we have already done it above.  Code is
570
 
             mostly duplicated above. */
571
600
 
572
 
          algo = GCRY_MD_SHA1; 
573
601
          rc = hash_and_copy_data (data_fd, data_md, writer);
574
602
          if (rc)
575
603
            goto leave;
576
 
          digest = gcry_md_read (data_md, algo);
577
 
          digest_len = gcry_md_get_algo_dlen (algo);
578
 
          if ( !digest || !digest_len)
579
 
            {
580
 
              log_error ("problem getting the hash of the data\n");
581
 
              rc = gpg_error (GPG_ERR_BUG);
582
 
              goto leave;
583
 
            }
584
604
          for (cl=signerlist,signer=0; cl; cl = cl->next, signer++)
585
605
            {
 
606
              digest = gcry_md_read (data_md, cl->hash_algo);
 
607
              digest_len = gcry_md_get_algo_dlen (cl->hash_algo);
 
608
              if ( !digest || !digest_len )
 
609
                {
 
610
                  log_error ("problem getting the hash of the data\n");
 
611
                  rc = gpg_error (GPG_ERR_BUG);
 
612
                  goto leave;
 
613
                }
586
614
              err = ksba_cms_set_message_digest (cms, signer,
587
615
                                                 digest, digest_len);
588
616
              if (err)
595
623
            }
596
624
        }
597
625
      else if (stopreason == KSBA_SR_NEED_SIG)
598
 
        { /* calculate the signature for all signers */
 
626
        { 
 
627
          /* Compute the signature for all signers.  */
599
628
          gcry_md_hd_t md;
600
629
 
601
 
          algo = GCRY_MD_SHA1;
602
 
          rc = gcry_md_open (&md, algo, 0);
 
630
          rc = gcry_md_open (&md, 0, 0);
603
631
          if (rc)
604
632
            {
605
633
              log_error ("md_open failed: %s\n", gpg_strerror (rc));
615
643
 
616
644
              if (signer)
617
645
                gcry_md_reset (md);
 
646
              {
 
647
                certlist_t cl_tmp;
 
648
 
 
649
                for (cl_tmp=signerlist; cl_tmp; cl_tmp = cl_tmp->next)
 
650
                  gcry_md_enable (md, cl_tmp->hash_algo);
 
651
              }
 
652
 
618
653
              rc = ksba_cms_hash_signed_attrs (cms, signer);
619
654
              if (rc)
620
655
                {
625
660
                }
626
661
            
627
662
              rc = gpgsm_create_cms_signature (ctrl, cl->cert,
628
 
                                               md, algo, &sigval);
 
663
                                               md, cl->hash_algo, &sigval);
629
664
              if (rc)
630
665
                {
631
666
                  gcry_md_close (md);
656
691
                rc = asprintf (&buf, "%c %d %d 00 %s %s",
657
692
                               detached? 'D':'S',
658
693
                               pkalgo, 
659
 
                               algo, 
 
694
                               cl->hash_algo, 
660
695
                               signed_at,
661
696
                               fpr);
662
697
              }