~ubuntu-branches/ubuntu/vivid/gnupg2/vivid-proposed

« back to all changes in this revision

Viewing changes to g10/passphrase.c

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2014-06-26 14:50:31 UTC
  • mfrom: (14.1.12 sid)
  • Revision ID: package-import@ubuntu.com-20140626145031-tpg2xmbtvomb8erd
Tags: 2.0.24-1ubuntu1
* Merge from Debian, remaining changes:
  - Drop sh prefix from openpgp test environment as it leads to exec
    invocations of sh /bin/bash leading to syntax errors from sh.  Fixes
    FTBFS detected in Ubuntu saucy archive rebuild.
  - Add udev rules to give gpg access to some smartcard readers;
    Debian #543217.
  - debian/gnupg2.udev: udev rules to set ACLs on SCM smartcard readers.
  - Add upstart user job for gpg-agent.

Show diffs side-by-side

added added

removed removed

Lines of Context:
74
74
          if (err && gpg_err_code (err) != GPG_ERR_ASS_PARAMETER)
75
75
            log_error (_("problem with the agent: %s\n"), gpg_strerror (err));
76
76
          /* Default to 65536 which we used up to 2.0.13.  */
77
 
          return 96; 
 
77
          return 96;
78
78
        }
79
79
      else if (mycnt >= 65011712)
80
80
        return 255; /* Largest possible value.  */
87
87
 
88
88
  if (iterations >= 65011712)
89
89
    return 255;
90
 
  
 
90
 
91
91
  /* Need count to be in the range 16-31 */
92
92
  for (count=iterations>>6; count>=32; count>>=1)
93
93
    c++;
96
96
 
97
97
  if (S2K_DECODE_COUNT(result) < iterations)
98
98
    result++;
99
 
  
 
99
 
100
100
  return result;
101
101
}
102
102
 
103
103
 
104
104
 
105
 
/* Hash a passphrase using the supplied s2k. 
 
105
/* Hash a passphrase using the supplied s2k.
106
106
   Always needs: dek->algo, s2k->mode, s2k->hash_algo.  */
107
107
static void
108
108
hash_passphrase ( DEK *dek, char *pw, STRING2KEY *s2k)
119
119
 
120
120
  if (gcry_md_open (&md, s2k->hash_algo, 1))
121
121
    BUG ();
122
 
  for (pass=0; used < dek->keylen ; pass++ ) 
 
122
  for (pass=0; used < dek->keylen ; pass++ )
123
123
    {
124
 
      if ( pass ) 
 
124
      if ( pass )
125
125
        {
126
126
          gcry_md_reset (md);
127
127
          for (i=0; i < pass; i++ ) /* Preset the hash context.  */
128
128
            gcry_md_putc (md, 0 );
129
129
        }
130
130
 
131
 
      if ( s2k->mode == 1 || s2k->mode == 3 ) 
 
131
      if ( s2k->mode == 1 || s2k->mode == 3 )
132
132
        {
133
133
          int len2 = pwlen + 8;
134
134
          ulong count = len2;
135
 
          
 
135
 
136
136
          if ( s2k->mode == 3 )
137
137
            {
138
138
              count = S2K_DECODE_COUNT(s2k->count);
146
146
 
147
147
          /* A little bit complicated because we need a ulong for count. */
148
148
          while ( count > len2 )  /* maybe iterated+salted */
149
 
            { 
 
149
            {
150
150
              gcry_md_write ( md, s2k->salt, 8 );
151
151
              gcry_md_write ( md, pw, pwlen );
152
152
              count -= len2;
242
242
  int i, len;
243
243
  char *pw;
244
244
 
245
 
  if ( !opt.batch ) 
 
245
  if ( !opt.batch )
246
246
    { /* Not used but we have to do a dummy read, so that it won't end
247
247
         up at the begin of the message if the quite usual trick to
248
248
         prepend the passphtrase to the message is used. */
251
251
      while (!(read (fd, buf, 1) != 1 || *buf == '\n' ))
252
252
        ;
253
253
      *buf = 0;
254
 
      return; 
 
254
      return;
255
255
    }
256
256
 
257
 
  for (pw = NULL, i = len = 100; ; i++ ) 
 
257
  for (pw = NULL, i = len = 100; ; i++ )
258
258
    {
259
 
      if (i >= len-1 ) 
 
259
      if (i >= len-1 )
260
260
        {
261
261
          char *pw2 = pw;
262
262
          len += 100;
322
322
  if( keyid && get_pubkey( pk, keyid ) )
323
323
    {
324
324
      if (pk)
325
 
        free_public_key( pk );      
 
325
        free_public_key( pk );
326
326
      pk = NULL; /* oops: no key for some reason */
327
327
    }
328
 
  
 
328
 
329
329
  orig_codeset = i18n_switchto_utf8 ();
330
330
 
331
331
  if (custom_description)
332
332
    atext = native_to_utf8 (custom_description);
333
333
  else if ( !mode && pk && keyid )
334
 
    { 
 
334
    {
335
335
      char *uid;
336
336
      size_t uidlen;
337
 
      const char *algo_name = gcry_pk_algo_name ( pk->pubkey_algo );
 
337
      const char *algo_name = openpgp_pk_algo_name (pk->pubkey_algo);
338
338
      const char *timestr;
339
339
      char *maink;
340
 
      
 
340
 
341
341
      if ( !algo_name )
342
342
        algo_name = "?";
343
343
 
344
344
#define KEYIDSTRING _(" (main key ID %s)")
345
345
 
346
346
      maink = xmalloc ( strlen (KEYIDSTRING) + keystrlen() + 20 );
347
 
      if( keyid[2] && keyid[3] && keyid[0] != keyid[2] 
 
347
      if( keyid[2] && keyid[3] && keyid[0] != keyid[2]
348
348
          && keyid[1] != keyid[3] )
349
349
        sprintf( maink, KEYIDSTRING, keystr(&keyid[2]) );
350
350
      else
351
351
        *maink = 0;
352
 
      
353
 
      uid = get_user_id ( keyid, &uidlen ); 
 
352
 
 
353
      uid = get_user_id ( keyid, &uidlen );
354
354
      timestr = strtimestamp (pk->timestamp);
355
355
 
356
356
#undef KEYIDSTRING
361
361
                       "%u-bit %s key, ID %s,\n" \
362
362
                       "created %s%s.\n" )
363
363
 
364
 
      atext = xmalloc ( 100 + strlen (PROMPTSTRING)  
 
364
      atext = xmalloc ( 100 + strlen (PROMPTSTRING)
365
365
                        + uidlen + 15 + strlen(algo_name) + keystrlen()
366
366
                        + strlen (timestr) + strlen (maink) );
367
367
      sprintf (atext, PROMPTSTRING,
373
373
 
374
374
#undef PROMPTSTRING
375
375
 
376
 
      { 
 
376
      {
377
377
        size_t dummy;
378
378
        fingerprint_from_pk( pk, fpr, &dummy );
379
379
        have_fpr = 1;
380
380
      }
381
 
      
 
381
 
382
382
    }
383
383
  else
384
384
    atext = xstrdup ( _("Enter passphrase\n") );
385
 
                
 
385
 
386
386
 
387
387
  if (!mode && cacheid)
388
388
    my_cacheid = cacheid;
398
398
 
399
399
  rc = agent_get_passphrase (my_cacheid, tryagain_text, my_prompt, atext,
400
400
                             repeat, check, &pw);
401
 
  
 
401
 
402
402
  xfree (my_prompt);
403
403
  xfree (atext); atext = NULL;
404
404
 
413
413
      if (canceled)
414
414
        *canceled = 1;
415
415
    }
416
 
  else 
 
416
  else
417
417
    {
418
418
      log_error (_("problem with the agent: %s\n"), gpg_strerror (rc));
419
419
      /* Due to limitations in the API of the upper layers they
422
422
         definitely not happen and let it continue without requiring a
423
423
         passphrase.  Given that now all the upper layers handle a
424
424
         cancel correctly, we simply set the cancel flag now for all
425
 
         errors from the agent.  */ 
 
425
         errors from the agent.  */
426
426
      if (canceled)
427
427
        *canceled = 1;
428
428
 
450
450
  int rc;
451
451
 
452
452
  (void)algo;
453
 
    
 
453
 
454
454
  if (!cacheid)
455
455
    {
456
456
      PKT_public_key *pk;
460
460
      byte fpr[MAX_FINGERPRINT_LEN];
461
461
      char hexfprbuf[2*20+1];
462
462
      size_t dummy;
463
 
      
 
463
 
464
464
      pk = xcalloc (1, sizeof *pk);
465
465
      if ( !keyid || get_pubkey( pk, keyid ) )
466
466
        {
488
488
   NULL, sets it to true.
489
489
 
490
490
   MODE 0:  Allow cached passphrase
491
 
        1:  Ignore cached passphrase 
 
491
        1:  Ignore cached passphrase
492
492
        2:  Ditto, but create a new key
493
493
        3:  Allow cached passphrase; use the S2K salt as the cache ID
494
494
        4:  Ditto, but create a new key
496
496
DEK *
497
497
passphrase_to_dek_ext (u32 *keyid, int pubkey_algo,
498
498
                       int cipher_algo, STRING2KEY *s2k, int mode,
499
 
                       const char *tryagain_text, 
 
499
                       const char *tryagain_text,
500
500
                       const char *custdesc, const char *custprompt,
501
501
                       int *canceled)
502
502
{
509
509
  if (!canceled)
510
510
    canceled = &dummy_canceled;
511
511
  *canceled = 0;
512
 
  
 
512
 
513
513
  if ( !s2k )
514
514
    {
515
515
      assert (mode != 3 && mode != 4);
516
 
      /* This is used for the old rfc1991 mode 
 
516
      /* This is used for the old rfc1991 mode
517
517
       * Note: This must match the code in encode.c with opt.rfc1991 set */
518
518
      s2k = &help_s2k;
519
519
      s2k->mode = 0;
539
539
 
540
540
  /* If we do not have a passphrase available in NEXT_PW and status
541
541
     information are request, we print them now. */
542
 
  if ( !next_pw && is_status_enabled() ) 
 
542
  if ( !next_pw && is_status_enabled() )
543
543
    {
544
544
      char buf[50];
545
 
      
 
545
 
546
546
      if ( keyid )
547
547
        {
548
548
          u32 used_kid[2];
549
549
          char *us;
550
 
          
551
 
          if ( keyid[2] && keyid[3] ) 
 
550
 
 
551
          if ( keyid[2] && keyid[3] )
552
552
            {
553
553
              used_kid[0] = keyid[2];
554
554
              used_kid[1] = keyid[3];
558
558
              used_kid[0] = keyid[0];
559
559
              used_kid[1] = keyid[1];
560
560
            }
561
 
          
 
561
 
562
562
          us = get_long_user_id_string ( keyid );
563
563
          write_status_text ( STATUS_USERID_HINT, us );
564
564
          xfree(us);
565
 
          
 
565
 
566
566
          snprintf (buf, sizeof buf -1, "%08lX%08lX %08lX%08lX %d 0",
567
567
                    (ulong)keyid[0], (ulong)keyid[1],
568
568
                    (ulong)used_kid[0], (ulong)used_kid[1],
569
569
                    pubkey_algo );
570
 
          
 
570
 
571
571
          write_status_text ( STATUS_NEED_PASSPHRASE, buf );
572
572
        }
573
573
      else
586
586
    {
587
587
      PKT_public_key *pk = xmalloc_clear( sizeof *pk );
588
588
      char *p;
589
 
      
 
589
 
590
590
      p = get_user_id_native(keyid);
591
591
      tty_printf ("\n");
592
592
      tty_printf (_("You need a passphrase to unlock the secret key for\n"
595
595
 
596
596
      if ( !get_pubkey( pk, keyid ) )
597
597
        {
598
 
          const char *s = gcry_pk_algo_name ( pk->pubkey_algo );
599
 
          
 
598
          const char *s = openpgp_pk_algo_name (pk->pubkey_algo);
 
599
 
600
600
          tty_printf (_("%u-bit %s key, ID %s, created %s"),
601
601
                      nbits_from_pk( pk ), s?s:"?", keystr(keyid),
602
602
                      strtimestamp(pk->timestamp) );
620
620
        free_public_key( pk );
621
621
    }
622
622
 
623
 
  if ( next_pw ) 
 
623
  if ( next_pw )
624
624
    {
625
625
      /* Simply return the passphrase we already have in NEXT_PW. */
626
626
      pw = next_pw;
627
627
      next_pw = NULL;
628
628
    }
629
 
  else if ( have_static_passphrase () ) 
 
629
  else if ( have_static_passphrase () )
630
630
    {
631
631
      /* Return the passphrase we have stored in FD_PASSWD. */
632
632
      pw = xmalloc_secure ( strlen(fd_passwd)+1 );
633
633
      strcpy ( pw, fd_passwd );
634
634
    }
635
 
  else 
 
635
  else
636
636
    {
637
637
      if ((mode == 3 || mode == 4) && (s2k->mode == 1 || s2k->mode == 3))
638
638
        {
653
653
          return NULL;
654
654
        }
655
655
    }
656
 
    
 
656
 
657
657
  if ( !pw || !*pw )
658
658
    write_status( STATUS_MISSING_PASSPHRASE );
659
659