~ubuntu-branches/ubuntu/raring/gnupg2/raring-proposed

« back to all changes in this revision

Viewing changes to scd/app-openpgp.c

  • Committer: Bazaar Package Importer
  • Author(s): Eric Dorland
  • Date: 2009-09-07 20:38:23 UTC
  • mfrom: (1.1.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20090907203823-d7hsk7lnfqoc4yom
Tags: 2.0.13-1
* New upstream release.
* debian/control: Depend instead of Recommend gnupg-agent. (Closes:
  #538947)

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 *
17
17
 * You should have received a copy of the GNU General Public License
18
18
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19
 
 *
20
 
 * $Id: app-openpgp.c 5043 2009-06-17 09:45:50Z wk $
21
19
 */
22
20
 
23
21
/* Some notes:
213
211
                            void *pincb_arg,
214
212
                            const void *indata, size_t indatalen,
215
213
                            unsigned char **outdata, size_t *outdatalen);
 
214
static void parse_algorithm_attribute (app_t app, int keyno);
 
215
static gpg_error_t change_keyattr_from_string
 
216
                           (app_t app, 
 
217
                            gpg_error_t (*pincb)(void*, const char *, char **),
 
218
                            void *pincb_arg,
 
219
                            const void *value, size_t valuelen);
216
220
 
217
221
 
218
222
 
1118
1122
 
1119
1123
  if (app->card_version > 0x0100)
1120
1124
    {
 
1125
      int exmode, le_value;
 
1126
 
1121
1127
      /* We may simply read the public key out of these cards.  */
 
1128
      if (app->app_local->cardcap.ext_lc_le)
 
1129
        {
 
1130
          exmode = 1;    /* Use extended length.  */
 
1131
          le_value = app->app_local->extcap.max_rsp_data;
 
1132
        }
 
1133
      else
 
1134
        {
 
1135
          exmode = 0;
 
1136
          le_value = 256; /* Use legacy value. */
 
1137
        }
 
1138
 
1122
1139
      err = iso7816_read_public_key 
1123
 
        (app->slot, 0, (const unsigned char*)(keyno == 0? "\xB6" :
1124
 
                                              keyno == 1? "\xB8" : "\xA4"), 2,  
1125
 
         0,
 
1140
        (app->slot, exmode,
 
1141
         (const unsigned char*)(keyno == 0? "\xB6" :
 
1142
                                keyno == 1? "\xB8" : "\xA4"), 2,  
 
1143
         le_value,
1126
1144
         &buffer, &buflen);
1127
1145
      if (err)
1128
1146
        {
1579
1597
  char *pinvalue;
1580
1598
 
1581
1599
  if (app->did_chv2) 
1582
 
    return 0;  /* We already verified CHV2 (PW1 for v2 cards).  */
 
1600
    return 0;  /* We already verified CHV2.  */
1583
1601
 
1584
 
  if (app->app_local->extcap.is_v2)
1585
 
    {
1586
 
      /* Version two cards don't have a CHV2 anymore.  We need to
1587
 
         verify CHV1 (now called PW1) instead.  */
1588
 
      rc = verify_a_chv (app, pincb, pincb_arg, 1, 0, &pinvalue);
1589
 
      if (rc)
1590
 
        return rc;
1591
 
      app->did_chv2 = 1;
1592
 
    }
1593
 
  else
1594
 
    {
1595
 
      /* Version 1 cards only.  */
1596
 
      rc = verify_a_chv (app, pincb, pincb_arg, 2, 0, &pinvalue);
1597
 
      if (rc)
1598
 
        return rc;
1599
 
      app->did_chv2 = 1;
 
1602
  rc = verify_a_chv (app, pincb, pincb_arg, 2, 0, &pinvalue);
 
1603
  if (rc)
 
1604
    return rc;
 
1605
  app->did_chv2 = 1;
1600
1606
  
1601
 
      if (!app->did_chv1 && !app->force_chv1 && pinvalue)
 
1607
  if (!app->did_chv1 && !app->force_chv1 && pinvalue)
 
1608
    {
 
1609
      /* For convenience we verify CHV1 here too.  We do this only if
 
1610
         the card is not configured to require a verification before
 
1611
         each CHV1 controlled operation (force_chv1) and if we are not
 
1612
         using the keypad (PINVALUE == NULL). */
 
1613
      rc = iso7816_verify (app->slot, 0x81, pinvalue, strlen (pinvalue));
 
1614
      if (gpg_err_code (rc) == GPG_ERR_BAD_PIN)
 
1615
        rc = gpg_error (GPG_ERR_PIN_NOT_SYNCED);
 
1616
      if (rc)
1602
1617
        {
1603
 
          /* For convenience we verify CHV1 here too.  We do this only
1604
 
             if the card is not configured to require a verification
1605
 
             before each CHV1 controlled operation (force_chv1) and if
1606
 
             we are not using the keypad (PINVALUE == NULL). */
1607
 
          rc = iso7816_verify (app->slot, 0x81, pinvalue, strlen (pinvalue));
1608
 
          if (gpg_err_code (rc) == GPG_ERR_BAD_PIN)
1609
 
            rc = gpg_error (GPG_ERR_PIN_NOT_SYNCED);
1610
 
          if (rc)
1611
 
            {
1612
 
              log_error (_("verify CHV%d failed: %s\n"), 1, gpg_strerror (rc));
1613
 
              flush_cache_after_error (app);
1614
 
            }
1615
 
          else
1616
 
            app->did_chv1 = 1;
 
1618
          log_error (_("verify CHV%d failed: %s\n"), 1, gpg_strerror (rc));
 
1619
          flush_cache_after_error (app);
1617
1620
        }
 
1621
      else
 
1622
        app->did_chv1 = 1;
1618
1623
    }
 
1624
 
1619
1625
  xfree (pinvalue);
1620
1626
 
1621
1627
  return rc;
1790
1796
    { "CERT-3",       0x7F21, 3, 0, 1 },
1791
1797
    { "SM-KEY-ENC",   0x00D1, 3, 0, 1 },
1792
1798
    { "SM-KEY-MAC",   0x00D2, 3, 0, 1 },
 
1799
    { "KEY-ATTR",     0,      0, 3, 1 },
1793
1800
    { NULL, 0 }
1794
1801
  };
1795
1802
  int exmode;
1801
1808
  if (table[idx].need_v2 && !app->app_local->extcap.is_v2)
1802
1809
    return gpg_error (GPG_ERR_NOT_SUPPORTED); /* Not yet supported.  */
1803
1810
 
 
1811
  if (table[idx].special == 3)
 
1812
    return change_keyattr_from_string (app, pincb, pincb_arg, value, valuelen);
 
1813
 
1804
1814
  switch (table[idx].need_chv)
1805
1815
    {
1806
1816
    case 2:
2142
2152
 
2143
2153
/* Check whether a key already exists.  KEYIDX is the index of the key
2144
2154
   (0..2).  If FORCE is TRUE a diagnositic will be printed but no
2145
 
   error returned if the key already exists. */
 
2155
   error returned if the key already exists.  The flag GENERATING is
 
2156
   only used to print correct messages. */
2146
2157
static gpg_error_t
2147
 
does_key_exist (app_t app, int keyidx, int force)
 
2158
does_key_exist (app_t app, int keyidx, int generating, int force)
2148
2159
{
2149
2160
  const unsigned char *fpr;
2150
2161
  unsigned char *buffer;
2176
2187
    }
2177
2188
  else if (i!=20)
2178
2189
    log_info (_("existing key will be replaced\n"));
2179
 
  else
 
2190
  else if (generating)
2180
2191
    log_info (_("generating new key\n"));
 
2192
  else
 
2193
    log_info (_("writing new key\n"));
2181
2194
  return 0;
2182
2195
}
2183
2196
 
2338
2351
}
2339
2352
 
2340
2353
 
 
2354
/* Helper for do_writekley to change the size of a key.  Not ethat
 
2355
   this deletes the entire key without asking.  */
 
2356
static gpg_error_t
 
2357
change_keyattr (app_t app, int keyno, unsigned int nbits,
 
2358
                gpg_error_t (*pincb)(void*, const char *, char **),
 
2359
                void *pincb_arg)
 
2360
{
 
2361
  gpg_error_t err;
 
2362
  unsigned char *buffer;
 
2363
  size_t buflen;
 
2364
  void *relptr;
 
2365
 
 
2366
  assert (keyno >=0 && keyno <= 2);
 
2367
 
 
2368
  if (nbits > 3072)
 
2369
    return gpg_error (GPG_ERR_TOO_LARGE);
 
2370
 
 
2371
  /* Read the current attributes into a buffer.  */
 
2372
  relptr = get_one_do (app, 0xC1+keyno, &buffer, &buflen, NULL);
 
2373
  if (!relptr)
 
2374
    return gpg_error (GPG_ERR_CARD);
 
2375
  if (buflen < 6 || buffer[0] != 1)
 
2376
    {
 
2377
      /* Attriutes too short or not an RSA key.  */
 
2378
      xfree (relptr);
 
2379
      return gpg_error (GPG_ERR_CARD);
 
2380
    }
 
2381
  
 
2382
  /* We only change n_bits and don't touch anything else.  Before we
 
2383
     do so, we round up NBITS to a sensible way in the same way as
 
2384
     gpg's key generation does it.  This may help to sort out problems
 
2385
     with a few bits too short keys.  */
 
2386
  nbits = ((nbits + 31) / 32) * 32;
 
2387
  buffer[1] = (nbits >> 8);
 
2388
  buffer[2] = nbits;
 
2389
 
 
2390
  /* Prepare for storing the key.  */
 
2391
  err = verify_chv3 (app, pincb, pincb_arg);
 
2392
  if (err)
 
2393
    {
 
2394
      xfree (relptr);
 
2395
      return err;
 
2396
    }
 
2397
 
 
2398
  /* Change the attribute.  */
 
2399
  err = iso7816_put_data (app->slot, 0, 0xC1+keyno, buffer, buflen);
 
2400
  xfree (relptr);
 
2401
  if (err)
 
2402
    log_error ("error changing size of key %d to %u bits\n", keyno+1, nbits);
 
2403
  else
 
2404
    log_info ("size of key %d changed to %u bits\n", keyno+1, nbits);
 
2405
  flush_cache (app);
 
2406
  parse_algorithm_attribute (app, keyno);
 
2407
  app->did_chv1 = 0;
 
2408
  app->did_chv2 = 0;
 
2409
  app->did_chv3 = 0;
 
2410
  return err;
 
2411
}
 
2412
 
 
2413
 
 
2414
/* Helper to process an setattr command for name KEY-ATTR.  It expects
 
2415
   a string "--force <keyno> <algo> <nbits>" in (VALUE,VALUELEN).  */
 
2416
static gpg_error_t 
 
2417
change_keyattr_from_string (app_t app, 
 
2418
                            gpg_error_t (*pincb)(void*, const char *, char **),
 
2419
                            void *pincb_arg,
 
2420
                            const void *value, size_t valuelen)
 
2421
{
 
2422
  gpg_error_t err;
 
2423
  char *string;
 
2424
  int keyno, algo;
 
2425
  unsigned int nbits;
 
2426
 
 
2427
  /* VALUE is expected to be a string but not guaranteed to be
 
2428
     terminated.  Thus copy it to an allocated buffer first. */
 
2429
  string = xtrymalloc (valuelen+1);
 
2430
  if (!string)
 
2431
    return gpg_error_from_syserror ();
 
2432
  memcpy (string, value, valuelen);
 
2433
  string[valuelen] = 0;
 
2434
 
 
2435
  /* Because this function deletes the key we require the string
 
2436
     "--force" in the data to make clear that something serious might
 
2437
     happen.  */
 
2438
  if (sscanf (string, " --force %d %d %u", &keyno, &algo, &nbits) != 3)
 
2439
    err = gpg_error (GPG_ERR_INV_DATA);
 
2440
  else if (keyno < 1 || keyno > 3)
 
2441
    err = gpg_error (GPG_ERR_INV_ID);
 
2442
  else if (algo != 1)
 
2443
    err = gpg_error (GPG_ERR_PUBKEY_ALGO); /* Not RSA.  */
 
2444
  else if (nbits < 1024)
 
2445
    err = gpg_error (GPG_ERR_TOO_SHORT);
 
2446
  else
 
2447
    err = change_keyattr (app, keyno-1, nbits, pincb, pincb_arg);
 
2448
 
 
2449
  xfree (string);
 
2450
  return err;
 
2451
}
 
2452
 
2341
2453
 
2342
2454
/* Handle the WRITEKEY command for OpenPGP.  This function expects a
2343
2455
   canonical encoded S-expression with the secret key in KEYDATA and
2383
2495
  else
2384
2496
    return gpg_error (GPG_ERR_INV_ID);
2385
2497
  
2386
 
  err = does_key_exist (app, keyno, force);
 
2498
  err = does_key_exist (app, keyno, 0, force);
2387
2499
  if (err)
2388
2500
    return err;
2389
2501
 
2513
2625
  if (opt.verbose)
2514
2626
    log_info ("RSA modulus size is %u bits (%u bytes)\n", 
2515
2627
              nbits, (unsigned int)rsa_n_len);
 
2628
  if (nbits && nbits != maxbits
 
2629
      && app->app_local->extcap.algo_attr_change)
 
2630
    {
 
2631
      /* Try to switch the key to a new length.  */
 
2632
      err = change_keyattr (app, keyno, nbits, pincb, pincb_arg);
 
2633
      if (!err)
 
2634
        maxbits = app->app_local->keyattr[keyno].n_bits;
 
2635
    }
2516
2636
  if (nbits != maxbits)
2517
2637
    {
2518
2638
      log_error (_("RSA modulus missing or not of size %d bits\n"), 
2694
2814
  app->app_local->pk[keyno].read_done = 0;
2695
2815
 
2696
2816
  /* Check whether a key already exists.  */
2697
 
  rc = does_key_exist (app, keyno, force);
 
2817
  rc = does_key_exist (app, keyno, 1, force);
2698
2818
  if (rc)
2699
2819
    return rc;
2700
2820
 
2937
3057
  const char *fpr = NULL;
2938
3058
  unsigned long sigcount;
2939
3059
  int use_auth = 0;
 
3060
  int exmode, le_value;
2940
3061
 
2941
3062
  if (!keyidstr || !*keyidstr)
2942
3063
    return gpg_error (GPG_ERR_INV_VALUE);
3077
3198
      xfree (pinvalue);
3078
3199
    }
3079
3200
 
3080
 
  rc = iso7816_compute_ds (app->slot, data, datalen, outdata, outdatalen);
 
3201
 
 
3202
  if (app->app_local->cardcap.ext_lc_le)
 
3203
    {
 
3204
      exmode = 1;    /* Use extended length.  */
 
3205
      le_value = app->app_local->extcap.max_rsp_data;
 
3206
    }
 
3207
  else
 
3208
    {
 
3209
      exmode = 0;
 
3210
      le_value = 0; 
 
3211
    }
 
3212
  rc = iso7816_compute_ds (app->slot, exmode, data, datalen, le_value,
 
3213
                           outdata, outdatalen);
3081
3214
  return rc;
3082
3215
}
3083
3216
 
3148
3281
 
3149
3282
  rc = verify_chv2 (app, pincb, pincb_arg);
3150
3283
  if (!rc)
3151
 
    rc = iso7816_internal_authenticate (app->slot, indata, indatalen,
3152
 
                                        outdata, outdatalen);
 
3284
    {
 
3285
      int exmode, le_value;
 
3286
 
 
3287
      if (app->app_local->cardcap.ext_lc_le)
 
3288
        {
 
3289
          exmode = 1;    /* Use extended length.  */
 
3290
          le_value = app->app_local->extcap.max_rsp_data;
 
3291
        }
 
3292
      else
 
3293
        {
 
3294
          exmode = 0;
 
3295
          le_value = 0; 
 
3296
        }
 
3297
      rc = iso7816_internal_authenticate (app->slot, exmode,
 
3298
                                          indata, indatalen, le_value,
 
3299
                                          outdata, outdatalen);
 
3300
    }
3153
3301
  return rc;
3154
3302
}
3155
3303
 
3166
3314
  const char *s;
3167
3315
  int n;
3168
3316
  const char *fpr = NULL;
3169
 
  int exmode;
 
3317
  int exmode, le_value;
3170
3318
 
3171
3319
  if (!keyidstr || !*keyidstr || !indatalen)
3172
3320
    return gpg_error (GPG_ERR_INV_VALUE);
3249
3397
          indatalen = fixuplen + indatalen;
3250
3398
          padind = -1; /* Already padded.  */
3251
3399
        }
3252
 
      
 
3400
 
3253
3401
      if (app->app_local->cardcap.ext_lc_le && indatalen > 254 )
3254
 
        exmode = 1;    /* Extended length w/o a limit.  */
 
3402
        {
 
3403
          exmode = 1;    /* Extended length w/o a limit.  */
 
3404
          le_value = app->app_local->extcap.max_rsp_data;
 
3405
        }
3255
3406
      else if (app->app_local->cardcap.cmd_chaining && indatalen > 254)
3256
 
        exmode = -254; /* Command chaining with max. 254 bytes.  */
 
3407
        {
 
3408
          exmode = -254; /* Command chaining with max. 254 bytes.  */
 
3409
          le_value = 0;
 
3410
        }
3257
3411
      else
3258
 
        exmode = 0;    
 
3412
        exmode = le_value = 0;    
3259
3413
 
3260
3414
      rc = iso7816_decipher (app->slot, exmode, 
3261
 
                             indata, indatalen, padind,
 
3415
                             indata, indatalen, le_value, padind,
3262
3416
                             outdata, outdatalen);
3263
3417
      xfree (fixbuf);
3264
3418
    }