~ubuntu-branches/ubuntu/precise/gnupg2/precise-proposed

« back to all changes in this revision

Viewing changes to g10/keygen.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:
92
92
struct output_control_s {
93
93
    int lnr;
94
94
    int dryrun;
 
95
    int ask_passphrase;
95
96
    int use_files;
96
97
    struct {
97
98
        char  *fname;
351
352
              strcat(dummy_string,"H8 ");
352
353
 
353
354
            /* RIPEMD160 */
354
 
            strcat(dummy_string,"H3 ");
 
355
            if (!openpgp_md_test_algo(DIGEST_ALGO_RMD160))
 
356
              strcat(dummy_string,"H3 ");
355
357
 
356
358
            /* ZLIB */
357
359
            strcat(dummy_string,"Z2 ");
630
632
  xfree (buf);
631
633
}
632
634
 
 
635
 
633
636
int
634
 
keygen_upd_std_prefs( PKT_signature *sig, void *opaque )
 
637
keygen_upd_std_prefs (PKT_signature *sig, void *opaque)
635
638
{
636
 
    if (!prefs_initialized)
637
 
        keygen_set_std_prefs (NULL, 0);
638
 
 
639
 
    if (nsym_prefs) 
640
 
        build_sig_subpkt (sig, SIGSUBPKT_PREF_SYM, sym_prefs, nsym_prefs);
641
 
    else
642
 
      {
643
 
        delete_sig_subpkt (sig->hashed, SIGSUBPKT_PREF_SYM);
644
 
        delete_sig_subpkt (sig->unhashed, SIGSUBPKT_PREF_SYM);
645
 
      }
646
 
 
647
 
    if (nhash_prefs)
648
 
        build_sig_subpkt (sig, SIGSUBPKT_PREF_HASH, hash_prefs, nhash_prefs);
649
 
    else
650
 
      {
651
 
        delete_sig_subpkt (sig->hashed, SIGSUBPKT_PREF_HASH);
652
 
        delete_sig_subpkt (sig->unhashed, SIGSUBPKT_PREF_HASH);
653
 
      }
654
 
 
655
 
    if (nzip_prefs)
656
 
        build_sig_subpkt (sig, SIGSUBPKT_PREF_COMPR, zip_prefs, nzip_prefs);
657
 
    else
658
 
      {
659
 
        delete_sig_subpkt (sig->hashed, SIGSUBPKT_PREF_COMPR);
660
 
        delete_sig_subpkt (sig->unhashed, SIGSUBPKT_PREF_COMPR);
661
 
      }
662
 
 
663
 
    /* Make sure that the MDC feature flag is set if needed */
664
 
    add_feature_mdc (sig,mdc_available);
665
 
    add_keyserver_modify (sig,ks_modify);
666
 
    keygen_add_keyserver_url(sig,NULL);
667
 
 
668
 
    return 0;
 
639
  (void)opaque;
 
640
  
 
641
  if (!prefs_initialized)
 
642
    keygen_set_std_prefs (NULL, 0);
 
643
  
 
644
  if (nsym_prefs) 
 
645
    build_sig_subpkt (sig, SIGSUBPKT_PREF_SYM, sym_prefs, nsym_prefs);
 
646
  else
 
647
    {
 
648
      delete_sig_subpkt (sig->hashed, SIGSUBPKT_PREF_SYM);
 
649
      delete_sig_subpkt (sig->unhashed, SIGSUBPKT_PREF_SYM);
 
650
    }
 
651
  
 
652
  if (nhash_prefs)
 
653
    build_sig_subpkt (sig, SIGSUBPKT_PREF_HASH, hash_prefs, nhash_prefs);
 
654
  else
 
655
    {
 
656
      delete_sig_subpkt (sig->hashed, SIGSUBPKT_PREF_HASH);
 
657
      delete_sig_subpkt (sig->unhashed, SIGSUBPKT_PREF_HASH);
 
658
    }
 
659
 
 
660
  if (nzip_prefs)
 
661
    build_sig_subpkt (sig, SIGSUBPKT_PREF_COMPR, zip_prefs, nzip_prefs);
 
662
  else
 
663
    {
 
664
      delete_sig_subpkt (sig->hashed, SIGSUBPKT_PREF_COMPR);
 
665
      delete_sig_subpkt (sig->unhashed, SIGSUBPKT_PREF_COMPR);
 
666
    }
 
667
  
 
668
  /* Make sure that the MDC feature flag is set if needed.  */
 
669
  add_feature_mdc (sig,mdc_available);
 
670
  add_keyserver_modify (sig,ks_modify);
 
671
  keygen_add_keyserver_url(sig,NULL);
 
672
 
 
673
  return 0;
669
674
}
670
675
 
671
676
 
1101
1106
static void
1102
1107
genhelp_factors (gcry_sexp_t misc_key_info, KBNODE sec_root)
1103
1108
{
 
1109
  (void)misc_key_info;
 
1110
  (void)sec_root;
1104
1111
#if 0 /* Not used anymore */
1105
1112
  size_t n;
1106
1113
  char *buf;
1788
1795
u32
1789
1796
parse_expire_string( const char *string )
1790
1797
{
1791
 
    int mult;
1792
 
    u32 seconds,abs_date=0,curtime = make_timestamp();
1793
 
 
1794
 
    if( !*string )
1795
 
      seconds = 0;
1796
 
    else if ( !strncmp (string, "seconds=", 8) )
1797
 
      seconds = atoi (string+8);
1798
 
    else if( (abs_date = scan_isodatestr(string)) && abs_date > curtime )
1799
 
      seconds = abs_date - curtime;
1800
 
    else if( (mult=check_valid_days(string)) )
1801
 
      seconds = atoi(string) * 86400L * mult;
1802
 
    else
1803
 
      seconds=(u32)-1;
1804
 
 
1805
 
    return seconds;
 
1798
  int mult;
 
1799
  u32 seconds;
 
1800
  u32 abs_date = 0;
 
1801
  u32 curtime = make_timestamp ();
 
1802
  
 
1803
  if (!*string)
 
1804
    seconds = 0;
 
1805
  else if (!strncmp (string, "seconds=", 8))
 
1806
    seconds = atoi (string+8);
 
1807
  else if ((abs_date = scan_isodatestr(string)) && abs_date > curtime)
 
1808
    seconds = abs_date - curtime;
 
1809
  else if ((mult = check_valid_days (string)))
 
1810
    seconds = atoi (string) * 86400L * mult;
 
1811
  else
 
1812
    seconds = (u32)(-1);
 
1813
  
 
1814
  return seconds;
1806
1815
}
1807
1816
 
1808
1817
/* Parsean Creation-Date string which is either "1986-04-26" or
1915
1924
              tty_printf (_("Your system can't display dates beyond 2038.\n"
1916
1925
                            "However, it will be correctly handled up to"
1917
1926
                            " 2106.\n"));
 
1927
            else
1918
1928
#endif /*SIZEOF_TIME_T*/
 
1929
              if ( (time_t)((unsigned long)(curtime+interval)) < curtime )
 
1930
                {
 
1931
                  tty_printf (_("invalid value\n"));
 
1932
                  continue;
 
1933
                }
1919
1934
          }
1920
1935
 
1921
1936
        if( cpr_enabled() || cpr_get_answer_is_yes("keygen.valid.okay",
1941
1956
    char *answer;
1942
1957
    char *aname, *acomment, *amail, *uid;
1943
1958
 
1944
 
    if( !mode )
1945
 
        tty_printf( _("\n"
 
1959
    if ( !mode )
 
1960
      {
 
1961
        /* TRANSLATORS: This is the new string telling the user what
 
1962
           gpg is now going to do (i.e. ask for the parts of the user
 
1963
           ID).  Note that if you do not tyranslated this string, a
 
1964
           different string will be used used, which might still have
 
1965
           a correct transaltion.  */
 
1966
        const char *s1 =
 
1967
          N_("\n"
 
1968
             "GnuPG needs to construct a user ID to identify your key.\n"
 
1969
             "\n");
 
1970
        const char *s2 = _(s1);
 
1971
 
 
1972
        if (!strcmp (s1, s2))
 
1973
          {
 
1974
            /* There is no translation for the string thus we to use
 
1975
               the old info text.  gettext has no way to tell whether
 
1976
               a translation is actually available, thus we need to
 
1977
               to compare again. */
 
1978
            /* TRANSLATORS: This string is in general not anymore used
 
1979
               but you should keep your existing translation.  In case
 
1980
               the new string is not translated this old string will
 
1981
               be used. */
 
1982
            const char *s3 = N_("\n"
1946
1983
"You need a user ID to identify your key; "
1947
1984
                                        "the software constructs the user ID\n"
1948
1985
"from the Real Name, Comment and Email Address in this form:\n"
1949
 
"    \"Heinrich Heine (Der Dichter) <heinrichh@duesseldorf.de>\"\n\n") );
 
1986
"    \"Heinrich Heine (Der Dichter) <heinrichh@duesseldorf.de>\"\n\n");
 
1987
            const char *s4 = _(s3);
 
1988
            if (strcmp (s3, s4))
 
1989
              s2 = s3; /* A translation exists - use it. */
 
1990
          }
 
1991
        tty_printf ("%s", s2) ;
 
1992
      }
1950
1993
    uid = aname = acomment = amail = NULL;
1951
1994
    for(;;) {
1952
1995
        char *p;
2254
2297
}
2255
2298
 
2256
2299
/* 
2257
 
 * parse the usage parameter and set the keyflags.  Return true on error.
 
2300
 * Parse the usage parameter and set the keyflags.  Returns -1 on
 
2301
 * error, 0 for no usage given or 1 for usage available.
2258
2302
 */
2259
2303
static int
2260
2304
parse_parameter_usage (const char *fname,
2402
2446
      algo=get_parameter_algo(para,pKEYTYPE);
2403
2447
      if (openpgp_pk_test_algo2 (algo, PUBKEY_USAGE_SIG))
2404
2448
        {
2405
 
          log_error("%s:%d: invalid algorithm\n", fname, r->lnr );
 
2449
          log_error ("%s:%d: invalid algorithm\n", fname, r->lnr );
2406
2450
          return -1;
2407
2451
        }
2408
2452
    }
2409
2453
  else
2410
2454
    {
2411
 
      log_error("%s: no Key-Type specified\n",fname);
 
2455
      log_error ("%s: no Key-Type specified\n",fname);
2412
2456
      return -1;
2413
2457
    }
2414
2458
 
2415
 
  err=parse_parameter_usage (fname, para, pKEYUSAGE);
2416
 
  if(err==0)
 
2459
  err = parse_parameter_usage (fname, para, pKEYUSAGE);
 
2460
  if (!err)
2417
2461
    {
2418
2462
      /* Default to algo capabilities if key-usage is not provided */
2419
 
      r=xmalloc_clear(sizeof(*r));
2420
 
      r->key=pKEYUSAGE;
2421
 
      r->u.usage=openpgp_pk_algo_usage(algo);
2422
 
      r->next=para;
2423
 
      para=r;
 
2463
      r = xmalloc_clear(sizeof(*r));
 
2464
      r->key = pKEYUSAGE;
 
2465
      r->u.usage = openpgp_pk_algo_usage(algo);
 
2466
      r->next = para;
 
2467
      para = r;
2424
2468
    }
2425
 
  else if(err==-1)
 
2469
  else if (err == -1)
2426
2470
    return -1;
 
2471
  else
 
2472
    {
 
2473
      r = get_parameter (para, pKEYUSAGE);
 
2474
      if (r && (r->u.usage & ~openpgp_pk_algo_usage (algo)))
 
2475
        {
 
2476
          log_error ("%s:%d: specified Key-Usage not allowed for algo %d\n",
 
2477
                     fname, r->lnr, algo);
 
2478
          return -1;
 
2479
        }
 
2480
    }
2427
2481
 
2428
2482
  r = get_parameter( para, pSUBKEYTYPE );
2429
2483
  if(r)
2430
2484
    {
2431
 
      algo=get_parameter_algo( para, pSUBKEYTYPE);
 
2485
      algo = get_parameter_algo (para, pSUBKEYTYPE);
2432
2486
      if (openpgp_pk_test_algo (algo))
2433
2487
        {
2434
 
          log_error("%s:%d: invalid algorithm\n", fname, r->lnr );
 
2488
          log_error ("%s:%d: invalid algorithm\n", fname, r->lnr );
2435
2489
          return -1;
2436
2490
        }
2437
2491
 
2438
 
      err=parse_parameter_usage (fname, para, pSUBKEYUSAGE);
2439
 
      if(err==0)
 
2492
      err = parse_parameter_usage (fname, para, pSUBKEYUSAGE);
 
2493
      if (!err)
2440
2494
        {
2441
2495
          /* Default to algo capabilities if subkey-usage is not
2442
2496
             provided */
2443
 
          r=xmalloc_clear(sizeof(*r));
2444
 
          r->key=pSUBKEYUSAGE;
2445
 
          r->u.usage=openpgp_pk_algo_usage(algo);
2446
 
          r->next=para;
2447
 
          para=r;
 
2497
          r = xmalloc_clear (sizeof(*r));
 
2498
          r->key = pSUBKEYUSAGE;
 
2499
          r->u.usage = openpgp_pk_algo_usage (algo);
 
2500
          r->next = para;
 
2501
          para = r;
2448
2502
        }
2449
 
      else if(err==-1)
 
2503
      else if (err == -1)
2450
2504
        return -1;
 
2505
      else
 
2506
        {
 
2507
          r = get_parameter (para, pSUBKEYUSAGE);
 
2508
          if (r && (r->u.usage & ~openpgp_pk_algo_usage (algo)))
 
2509
            {
 
2510
              log_error ("%s:%d: specified Subkey-Usage not allowed"
 
2511
                         " for algo %d\n", fname, r->lnr, algo);
 
2512
              return -1;
 
2513
            }
 
2514
        }
2451
2515
    }
2452
2516
 
 
2517
 
2453
2518
  if( get_parameter_value( para, pUSERID ) )
2454
2519
    have_user_id=1;
2455
2520
  else
2508
2573
  if (parse_revocation_key (fname, para, pREVOKER))
2509
2574
    return -1;
2510
2575
 
2511
 
  /* make DEK and S2K from the Passphrase */
2512
 
  r = get_parameter( para, pPASSPHRASE );
2513
 
  if( r && *r->u.value ) {
2514
 
    /* We have a plain text passphrase - create a DEK from it.
2515
 
     * It is a little bit ridiculous to keep it ih secure memory
2516
 
     * but because we do this always, why not here */
2517
 
    STRING2KEY *s2k;
2518
 
    DEK *dek;
2519
 
 
2520
 
    s2k = xmalloc_secure( sizeof *s2k );
2521
 
    s2k->mode = opt.s2k_mode;
2522
 
    s2k->hash_algo = S2K_DIGEST_ALGO;
2523
 
    set_next_passphrase( r->u.value );
2524
 
    dek = passphrase_to_dek( NULL, 0, opt.s2k_cipher_algo, s2k, 2,
2525
 
                             NULL, NULL);
2526
 
    set_next_passphrase( NULL );
2527
 
    assert( dek );
2528
 
    memset( r->u.value, 0, strlen(r->u.value) );
2529
 
 
2530
 
    r = xmalloc_clear( sizeof *r );
2531
 
    r->key = pPASSPHRASE_S2K;
2532
 
    r->u.s2k = s2k;
2533
 
    r->next = para;
2534
 
    para = r;
2535
 
    r = xmalloc_clear( sizeof *r );
2536
 
    r->key = pPASSPHRASE_DEK;
2537
 
    r->u.dek = dek;
2538
 
    r->next = para;
2539
 
    para = r;
2540
 
  }
 
2576
  /* Make DEK and S2K from the Passphrase. */
 
2577
  if (outctrl->ask_passphrase)
 
2578
    {
 
2579
      /* %ask-passphrase is active - ignore pPASSPRASE and ask.  This
 
2580
         feature is required so that GUIs are able to do a key
 
2581
         creation but have gpg-agent ask for the passphrase.  */
 
2582
      int canceled = 0;
 
2583
      STRING2KEY *s2k;
 
2584
      DEK *dek;
 
2585
 
 
2586
      dek = do_ask_passphrase ( &s2k, &canceled );
 
2587
      if (dek)
 
2588
        {
 
2589
          r = xmalloc_clear( sizeof *r );
 
2590
          r->key = pPASSPHRASE_DEK;
 
2591
          r->u.dek = dek;
 
2592
          r->next = para;
 
2593
          para = r;
 
2594
          r = xmalloc_clear( sizeof *r );
 
2595
          r->key = pPASSPHRASE_S2K;
 
2596
          r->u.s2k = s2k;
 
2597
          r->next = para;
 
2598
          para = r;
 
2599
        }
 
2600
 
 
2601
      if (canceled) 
 
2602
        {
 
2603
          log_error ("%s:%d: key generation canceled\n", fname, r->lnr );
 
2604
          return -1;
 
2605
        }
 
2606
    }
 
2607
  else
 
2608
    {
 
2609
      r = get_parameter( para, pPASSPHRASE );
 
2610
      if ( r && *r->u.value )
 
2611
        {
 
2612
          /* We have a plain text passphrase - create a DEK from it.
 
2613
           * It is a little bit ridiculous to keep it in secure memory
 
2614
           * but because we do this always, why not here.  */
 
2615
          STRING2KEY *s2k;
 
2616
          DEK *dek;
 
2617
          
 
2618
          s2k = xmalloc_secure ( sizeof *s2k );
 
2619
          s2k->mode = opt.s2k_mode;
 
2620
          s2k->hash_algo = S2K_DIGEST_ALGO;
 
2621
          set_next_passphrase ( r->u.value );
 
2622
          dek = passphrase_to_dek (NULL, 0, opt.s2k_cipher_algo, s2k, 2,
 
2623
                                   NULL, NULL);
 
2624
          set_next_passphrase (NULL );
 
2625
          assert (dek);
 
2626
          memset (r->u.value, 0, strlen(r->u.value));
 
2627
          
 
2628
          r = xmalloc_clear (sizeof *r);
 
2629
          r->key = pPASSPHRASE_S2K;
 
2630
          r->u.s2k = s2k;
 
2631
          r->next = para;
 
2632
          para = r;
 
2633
          r = xmalloc_clear (sizeof *r);
 
2634
          r->key = pPASSPHRASE_DEK;
 
2635
          r->u.dek = dek;
 
2636
          r->next = para;
 
2637
          para = r;
 
2638
        }
 
2639
    }
2541
2640
 
2542
2641
  /* Make KEYCREATIONDATE from Creation-Date.  */
2543
2642
  r = get_parameter (para, pCREATIONDATE);
2677
2776
                log_info("%s\n", value );
2678
2777
            else if( !ascii_strcasecmp( keyword, "%dry-run" ) )
2679
2778
                outctrl.dryrun = 1;
 
2779
            else if( !ascii_strcasecmp( keyword, "%ask-passphrase" ) )
 
2780
                outctrl.ask_passphrase = 1;
 
2781
            else if( !ascii_strcasecmp( keyword, "%no-ask-passphrase" ) )
 
2782
                outctrl.ask_passphrase = 0;
2680
2783
            else if( !ascii_strcasecmp( keyword, "%commit" ) ) {
2681
2784
                outctrl.lnr = lnr;
2682
2785
                if (proc_parameter_file( para, fname, &outctrl, 0 ))