~ubuntu-branches/ubuntu/feisty/gnupg2/feisty

« back to all changes in this revision

Viewing changes to g10/sign.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2006-11-24 18:48:23 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20061124184823-17ir9m46tl09n9k4
Tags: 2.0.0-4ubuntu1
* Synchronize to Debian, reapply remaining Ubuntu changes to pristine Debian
  version:
  - Remove libpcsclite-dev, libopensc2-dev build dependencies (they are in
    universe).

Show diffs side-by-side

added added

removed removed

Lines of Context:
66
66
{
67
67
    const char *string;
68
68
    char *s=NULL;
69
 
    STRLIST pu=NULL;
 
69
    strlist_t pu=NULL;
70
70
    struct notation *nd=NULL;
71
71
    struct expando_args args;
72
72
 
320
320
      }
321
321
    else 
322
322
      {
323
 
        /* If it's a DSA key, and q is 160 bits, it might be an
324
 
           old-style DSA key.  If the hash doesn't match the q, fail
325
 
           unless --enable-dsa2 is set.  If the q isn't 160 bits, then
326
 
           allow any hash since it must be a DSA2 key (if the hash is
327
 
           too small, we'll fail in encode_md_value). */
328
 
        if (sk->pubkey_algo==PUBKEY_ALGO_DSA
329
 
            && (gcry_mpi_get_nbits (sk->skey[1])/8)==20
330
 
            && !opt.flags.dsa2
331
 
            && gcry_md_get_algo_dlen (digest_algo)!=20)
332
 
          {
333
 
            log_error(_("DSA requires the use of a 160 bit hash algorithm\n"));
334
 
            return G10ERR_GENERAL;
335
 
          }
336
 
 
337
323
        frame = encode_md_value( NULL, sk, md, digest_algo );
338
324
        if (!frame)
339
325
          return G10ERR_GENERAL;
749
735
 * uncompressed, non-armored and in binary mode.
750
736
 */
751
737
int
752
 
sign_file( STRLIST filenames, int detached, STRLIST locusr,
753
 
           int encryptflag, STRLIST remusr, const char *outfile )
 
738
sign_file( strlist_t filenames, int detached, strlist_t locusr,
 
739
           int encryptflag, strlist_t remusr, const char *outfile )
754
740
{
755
741
    const char *fname;
756
742
    armor_filter_context_t afx;
822
808
        }
823
809
      if( !inp ) 
824
810
        {
825
 
          rc = gpg_error_from_errno (errno);
 
811
          rc = gpg_error_from_syserror ();
826
812
          log_error (_("can't open `%s': %s\n"), fname? fname: "[stdin]",
827
813
                     strerror(errno) );
828
814
          goto leave;
840
826
            out = iobuf_create( outfile );
841
827
        if( !out )
842
828
          {
843
 
            rc = gpg_error_from_errno (errno);
 
829
            rc = gpg_error_from_syserror ();
844
830
            log_error(_("can't create `%s': %s\n"), outfile, strerror(errno) );
845
831
            goto leave;
846
832
          }
862
848
    if (DBG_HASHING)
863
849
      gcry_md_start_debug (mfx.md, "sign");
864
850
 
865
 
   /* If we're encrypting and signing, it is reasonable to pick the
866
 
       hash algorithm to use out of the recepient key prefs. */
 
851
    /* If we're encrypting and signing, it is reasonable to pick the
 
852
       hash algorithm to use out of the recepient key prefs.  This is
 
853
       best effort only, as in a DSA2 and smartcard world there are
 
854
       cases where we cannot please everyone with a single hash (DSA2
 
855
       wants >160 and smartcards want =160).  In the future this could
 
856
       be more complex with different hashes for each sk, but the
 
857
       current design requires a single hash for all SKs. */
867
858
    if(pk_list)
868
859
      {
869
860
        if(opt.def_digest_algo)
879
870
          }
880
871
        else
881
872
          {
882
 
            int hashlen=0,algo;
 
873
            int algo, smartcard=0;
 
874
            union pref_hint hint;
 
875
 
 
876
            hint.digest_length = 0;
883
877
 
884
878
            /* Of course, if the recipient asks for something
885
 
               unreasonable (like a non-160-bit hash for DSA, for
886
 
               example), then don't do it.  Check all sk's - if any
887
 
               are DSA, then the hash must be 160-bit.  In the future
888
 
               this can be more complex with different hashes for each
889
 
               sk, but so long as there is only one signing algorithm
890
 
               with hash restrictions, this is ok. -dms */
891
 
 
892
 
            /* Current smartcards only do 160-bit hashes as well.
893
 
               Note that this may well have to change as the cards add
894
 
               algorithms. */
895
 
 
896
 
            for( sk_rover = sk_list; sk_rover; sk_rover = sk_rover->next )
897
 
              if(sk_rover->sk->pubkey_algo==PUBKEY_ALGO_DSA
898
 
                 || (sk_rover->sk->is_protected
899
 
                     && sk_rover->sk->protect.s2k.mode==1002))
900
 
                hashlen=20;
901
 
 
902
 
            if((algo=
903
 
                select_algo_from_prefs(pk_list,PREFTYPE_HASH,-1,
904
 
                                       hashlen?&hashlen:NULL))>0)
905
 
              recipient_digest_algo=algo;
 
879
               unreasonable (like the wrong hash for a DSA key) then
 
880
               don't do it.  Check all sk's - if any are DSA or live
 
881
               on a smartcard, then the hash has restrictions and we
 
882
               may not be able to give the recipient what they want.
 
883
               For DSA, pass a hint for the largest q we have.  Note
 
884
               that this means that a q>160 key will override a q=160
 
885
               key and force the use of truncation for the q=160 key.
 
886
               The alternative would be to ignore the recipient prefs
 
887
               completely and get a different hash for each DSA key in
 
888
               hash_for().  The override behavior here is more or less
 
889
               reasonable as it is under the control of the user which
 
890
               keys they sign with for a given message and the fact
 
891
               that the message with multiple signatures won't be
 
892
               usable on an implementation that doesn't understand
 
893
               DSA2 anyway. */
 
894
 
 
895
            for (sk_rover = sk_list; sk_rover; sk_rover = sk_rover->next )
 
896
              {
 
897
                if (sk_rover->sk->pubkey_algo == PUBKEY_ALGO_DSA)
 
898
                  {
 
899
                    int temp_hashlen = gcry_mpi_get_nbits
 
900
                      (sk_rover->sk->skey[1])+7/8;
 
901
 
 
902
                    /* Pick a hash that is large enough for our
 
903
                       largest q */
 
904
 
 
905
                    if (hint.digest_length<temp_hashlen)
 
906
                      hint.digest_length=temp_hashlen;
 
907
                  }
 
908
                else if (sk_rover->sk->is_protected
 
909
                         && sk_rover->sk->protect.s2k.mode == 1002)
 
910
                  smartcard = 1;
 
911
              }
 
912
 
 
913
            /* Current smartcards only do 160-bit hashes.  If we have
 
914
               to have a >160-bit hash, then we can't use the
 
915
               recipient prefs as we'd need both =160 and >160 at the
 
916
               same time and recipient prefs currently require a
 
917
               single hash for all signatures.  All this may well have
 
918
               to change as the cards add algorithms. */
 
919
 
 
920
            if (!smartcard || (smartcard && hint.digest_length==20))
 
921
              if ( (algo=
 
922
                   select_algo_from_prefs(pk_list,PREFTYPE_HASH,-1,&hint)) > 0)
 
923
                recipient_digest_algo=algo;
906
924
          }
907
925
      }
908
926
 
969
987
    /* Setup the inner packet. */
970
988
    if( detached ) {
971
989
        if( multifile ) {
972
 
            STRLIST sl;
 
990
            strlist_t sl;
973
991
 
974
992
            if( opt.verbose )
975
993
                log_info(_("signing:") );
985
1003
                  }
986
1004
                if( !inp )
987
1005
                  {
988
 
                    rc = gpg_error_from_errno (errno);
 
1006
                    rc = gpg_error_from_syserror ();
989
1007
                    log_error(_("can't open `%s': %s\n"),
990
1008
                              sl->d,strerror(errno));
991
1009
                    goto leave;
1051
1069
 * make a clear signature. note that opt.armor is not needed
1052
1070
 */
1053
1071
int
1054
 
clearsign_file( const char *fname, STRLIST locusr, const char *outfile )
 
1072
clearsign_file( const char *fname, strlist_t locusr, const char *outfile )
1055
1073
{
1056
1074
    armor_filter_context_t afx;
1057
1075
    progress_filter_context_t pfx;
1098
1116
        errno = EPERM;
1099
1117
      }
1100
1118
    if( !inp ) {
1101
 
        rc = gpg_error_from_errno (errno);
 
1119
        rc = gpg_error_from_syserror ();
1102
1120
        log_error (_("can't open `%s': %s\n"), 
1103
1121
                   fname? fname: "[stdin]", strerror(errno) );
1104
1122
        goto leave;
1114
1132
            out = iobuf_create( outfile );
1115
1133
        if( !out )
1116
1134
          {
1117
 
            rc = gpg_error_from_errno (errno);
 
1135
            rc = gpg_error_from_syserror ();
1118
1136
            log_error(_("can't create `%s': %s\n"), outfile, strerror(errno) );
1119
1137
            goto leave;
1120
1138
          }
1205
1223
 * FIXME: Far too much code is duplicated - revamp the whole file.
1206
1224
 */
1207
1225
int
1208
 
sign_symencrypt_file (const char *fname, STRLIST locusr)
 
1226
sign_symencrypt_file (const char *fname, strlist_t locusr)
1209
1227
{
1210
1228
    armor_filter_context_t afx;
1211
1229
    progress_filter_context_t pfx;
1250
1268
        errno = EPERM;
1251
1269
      }
1252
1270
    if( !inp ) {
1253
 
        rc = gpg_error_from_errno (errno);
 
1271
        rc = gpg_error_from_syserror ();
1254
1272
        log_error (_("can't open `%s': %s\n"), 
1255
1273
                   fname? fname: "[stdin]", strerror(errno) );
1256
1274
        goto leave;
1539
1557
      }
1540
1558
 
1541
1559
    /* Note that already expired sigs will remain expired (with a
1542
 
       duration of 0) since build-packet.c:build_sig_subpkt_from_sig
 
1560
       duration of 1) since build-packet.c:build_sig_subpkt_from_sig
1543
1561
       detects this case. */
1544
1562
 
1545
1563
    if( sig->version >= 4 )