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

« back to all changes in this revision

Viewing changes to g10/card-util.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:
1
1
/* card-util.c - Utility functions for the OpenPGP card.
2
 
 *      Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
 
2
 * Copyright (C) 2003, 2004, 2005, 2009 Free Software Foundation, Inc.
3
3
 *
4
4
 * This file is part of GnuPG.
5
5
 *
23
23
#include <string.h>
24
24
#include <errno.h>
25
25
#include <assert.h>
 
26
#ifdef HAVE_LIBREADLINE
 
27
# define GNUPG_LIBREADLINE_H_INCLUDED
 
28
# include <readline/readline.h>
 
29
#endif /*HAVE_LIBREADLINE*/
26
30
 
27
31
#if GNUPG_MAJOR_VERSION != 1
28
32
# include "gpg.h"
34
38
#include "options.h"
35
39
#include "main.h"
36
40
#include "keyserver-internal.h"
 
41
 
37
42
#if GNUPG_MAJOR_VERSION == 1
38
 
# ifdef HAVE_LIBREADLINE
39
 
# define GNUPG_LIBREADLINE_H_INCLUDED
40
 
# include <stdio.h>
41
 
# include <readline/readline.h>
42
 
# endif /*HAVE_LIBREADLINE*/
43
43
# include "cardglue.h"
44
44
#else /*GNUPG_MAJOR_VERSION!=1*/
45
45
# include "call-agent.h"
1156
1156
    rc = agent_scd_getattr ("CHV-STATUS", info);
1157
1157
  if (!rc)
1158
1158
    rc = agent_scd_getattr ("DISP-NAME", info);
 
1159
  if (!rc)
 
1160
    rc = agent_scd_getattr ("EXTCAP", info);
 
1161
  if (!rc)
 
1162
    rc = agent_scd_getattr ("KEY-ATTR", info);
1159
1163
  if (rc)
1160
1164
    log_error (_("error getting current key info: %s\n"), gpg_strerror (rc));
1161
1165
  return rc;
1252
1256
 
1253
1257
 
1254
1258
static void
 
1259
show_keysize_warning (void)
 
1260
{
 
1261
  static int shown;
 
1262
 
 
1263
  if (shown)
 
1264
    return;
 
1265
  shown = 1;
 
1266
  tty_printf
 
1267
    (_("NOTE: There is no guarantee that the card "
 
1268
       "supports the requested size.\n"
 
1269
       "      If the key generation does not succeed, "
 
1270
       "please check the\n"
 
1271
       "      documentation of your card to see what "
 
1272
       "sizes are allowed.\n"));
 
1273
}
 
1274
 
 
1275
 
 
1276
/* Ask for the size of a card key.  NBITS is the current size
 
1277
   configured for the card.  KEYNO is the number of the key used to
 
1278
   select the prompt.  Returns 0 to use the default size (i.e. NBITS)
 
1279
   or the selected size.  */
 
1280
static unsigned int
 
1281
ask_card_keysize (int keyno, unsigned int nbits)
 
1282
{
 
1283
  unsigned int min_nbits = 1024;
 
1284
  unsigned int max_nbits = 3072; /* GnuPG limit due to Assuan.  */
 
1285
  char *prompt, *answer;
 
1286
  unsigned int req_nbits;
 
1287
 
 
1288
  for (;;)
 
1289
    {
 
1290
      prompt = xasprintf 
 
1291
        (keyno == 0?
 
1292
         _("What keysize do you want for the Signature key? (%u) "):
 
1293
         keyno == 1?
 
1294
         _("What keysize do you want for the Encryption key? (%u) "):
 
1295
         _("What keysize do you want for the Authentication key? (%u) "),
 
1296
         nbits);
 
1297
      answer = cpr_get ("cardedit.genkeys.size", prompt);
 
1298
      cpr_kill_prompt ();
 
1299
      req_nbits = *answer? atoi (answer): nbits;
 
1300
      xfree (prompt);
 
1301
      xfree (answer);
 
1302
      
 
1303
      if (req_nbits != nbits && (req_nbits % 32) )
 
1304
        {
 
1305
          req_nbits = ((req_nbits + 31) / 32) * 32;
 
1306
          tty_printf (_("rounded up to %u bits\n"), req_nbits);
 
1307
        }
 
1308
  
 
1309
      if (req_nbits == nbits)
 
1310
        return 0;  /* Use default.  */
 
1311
      
 
1312
      if (req_nbits < min_nbits || req_nbits > max_nbits)
 
1313
        {
 
1314
          tty_printf (_("%s keysizes must be in the range %u-%u\n"),
 
1315
                      "RSA", min_nbits, max_nbits);
 
1316
        }
 
1317
      else
 
1318
        {
 
1319
          tty_printf (_("The card will now be re-configured "
 
1320
                        "to generate a key of %u bits\n"), req_nbits);
 
1321
          show_keysize_warning ();
 
1322
          return req_nbits;
 
1323
        }
 
1324
    }
 
1325
}
 
1326
 
 
1327
 
 
1328
/* Change the size of key KEYNO (0..2) to NBITS and show an error
 
1329
   message if that fails.  */
 
1330
static gpg_error_t
 
1331
do_change_keysize (int keyno, unsigned int nbits) 
 
1332
{
 
1333
  gpg_error_t err;
 
1334
  char args[100];
 
1335
  
 
1336
  snprintf (args, sizeof args, "--force %d 1 %u", keyno+1, nbits);
 
1337
  err = agent_scd_setattr ("KEY-ATTR", args, strlen (args), NULL);
 
1338
  if (err)
 
1339
    log_error (_("error changing size of key %d to %u bits: %s\n"), 
 
1340
               keyno+1, nbits, gpg_strerror (err));
 
1341
  return err;
 
1342
}
 
1343
 
 
1344
 
 
1345
static void
1255
1346
generate_card_keys (void)
1256
1347
{
1257
1348
  struct agent_card_info_s info;
1258
1349
  int forced_chv1;
1259
1350
  int want_backup;
 
1351
  int keyno;
1260
1352
 
1261
1353
  if (get_info_for_key_operation (&info))
1262
1354
    return;
1263
1355
 
1264
 
#if GNUPG_MAJOR_VERSION == 1
1265
 
  {
1266
 
    char *answer=cpr_get("cardedit.genkeys.backup_enc",
1267
 
                         _("Make off-card backup of encryption key? (Y/n) "));
1268
 
 
1269
 
    want_backup=answer_is_yes_no_default(answer,1);
1270
 
    cpr_kill_prompt();
1271
 
    xfree(answer);
1272
 
  }
1273
 
#else
1274
 
  want_backup = cpr_get_answer_is_yes 
1275
 
                  ( "cardedit.genkeys.backup_enc",
1276
 
                    _("Make off-card backup of encryption key? (Y/n) "));
1277
 
  /*FIXME: we need answer_is_yes_no_default()*/
1278
 
#endif
 
1356
  if (info.extcap.ki)
 
1357
    {
 
1358
      char *answer;
 
1359
 
 
1360
      answer = cpr_get ("cardedit.genkeys.backup_enc",
 
1361
                        _("Make off-card backup of encryption key? (Y/n) "));
 
1362
 
 
1363
      want_backup = answer_is_yes_no_default (answer, 1/*(default to Yes)*/);
 
1364
      cpr_kill_prompt ();
 
1365
      xfree (answer);
 
1366
    }
 
1367
  else
 
1368
    want_backup = 0;
1279
1369
 
1280
1370
  if ( (info.fpr1valid && !fpr_is_zero (info.fpr1))
1281
1371
       || (info.fpr2valid && !fpr_is_zero (info.fpr2))
1282
1372
       || (info.fpr3valid && !fpr_is_zero (info.fpr3)))
1283
1373
    {
1284
1374
      tty_printf ("\n");
1285
 
      log_info ("NOTE: keys are already stored on the card!\n");
 
1375
      log_info (_("NOTE: keys are already stored on the card!\n"));
1286
1376
      tty_printf ("\n");
1287
 
      if ( !cpr_get_answer_is_yes( "cardedit.genkeys.replace_keys",
1288
 
                                  _("Replace existing keys? (y/N) ")))
 
1377
      if ( !cpr_get_answer_is_yes ("cardedit.genkeys.replace_keys",
 
1378
                                   _("Replace existing keys? (y/N) ")))
1289
1379
        {
1290
1380
          agent_release_card_info (&info);
1291
1381
          return;
1292
1382
        }
1293
1383
    }
1294
 
  else if (!info.disp_name || !*info.disp_name)
 
1384
 
 
1385
  /* If no displayed name has been set, we assume that this is a fresh
 
1386
     card and print a hint about the default PINs.  */
 
1387
  if (!info.disp_name || !*info.disp_name)
1295
1388
    {
1296
1389
      tty_printf ("\n");
1297
1390
      tty_printf (_("Please note that the factory settings of the PINs are\n"
1303
1396
 
1304
1397
  if (check_pin_for_key_operation (&info, &forced_chv1))
1305
1398
    goto leave;
1306
 
  
1307
 
  generate_keypair (NULL, info.serialno,
1308
 
                    want_backup? opt.homedir:NULL);
 
1399
 
 
1400
  /* If the cards features changeable key attributes, we ask for the
 
1401
     key size.  */
 
1402
  if (info.is_v2 && info.extcap.aac)
 
1403
    {
 
1404
      unsigned int nbits;
 
1405
 
 
1406
      for (keyno = 0; keyno < DIM (info.key_attr); keyno++)
 
1407
        {
 
1408
          nbits = ask_card_keysize (keyno, info.key_attr[keyno].nbits);
 
1409
          if (nbits && do_change_keysize (keyno, nbits))
 
1410
            {
 
1411
              /* Error: Better read the default key size again.  */
 
1412
              agent_release_card_info (&info);
 
1413
              if (get_info_for_key_operation (&info))
 
1414
                goto leave;
 
1415
              /* Ask again for this key size. */
 
1416
              keyno--;
 
1417
            }
 
1418
        }
 
1419
      /* Note that INFO has not be synced.  However we will only use
 
1420
         the serialnumber and thus it won't harm.  */
 
1421
    }
 
1422
     
 
1423
  generate_keypair (NULL, info.serialno, want_backup? opt.homedir:NULL);
1309
1424
 
1310
1425
 leave:
1311
1426
  agent_release_card_info (&info);
1357
1472
  if (check_pin_for_key_operation (&info, &forced_chv1))
1358
1473
    goto leave;
1359
1474
 
 
1475
  /* If the cards features changeable key attributes, we ask for the
 
1476
     key size.  */
 
1477
  if (info.is_v2 && info.extcap.aac)
 
1478
    {
 
1479
      unsigned int nbits;
 
1480
 
 
1481
    ask_again:
 
1482
      nbits = ask_card_keysize (keyno-1, info.key_attr[keyno-1].nbits);
 
1483
      if (nbits && do_change_keysize (keyno-1, nbits))
 
1484
        {
 
1485
          /* Error: Better read the default key size again.  */
 
1486
          agent_release_card_info (&info);
 
1487
          if (get_info_for_key_operation (&info))
 
1488
            goto leave;
 
1489
          goto ask_again;
 
1490
        }
 
1491
      /* Note that INFO has not be synced.  However we will only use
 
1492
         the serialnumber and thus it won't harm.  */
 
1493
    }
 
1494
 
1360
1495
  okay = generate_card_subkeypair (pub_keyblock, sec_keyblock,
1361
1496
                                   keyno, info.serialno);
1362
1497
 
1383
1518
  size_t n;
1384
1519
  const char *s;
1385
1520
  int allow_keyno[3];
 
1521
  unsigned int nbits;
 
1522
 
1386
1523
 
1387
1524
  assert (node->pkt->pkttype == PKT_SECRET_KEY
1388
1525
          || node->pkt->pkttype == PKT_SECRET_SUBKEY);
1391
1528
  if (get_info_for_key_operation (&info))
1392
1529
    return 0;
1393
1530
 
 
1531
  if (!info.extcap.ki)
 
1532
    {
 
1533
      tty_printf ("The card does not support the import of keys\n");
 
1534
      tty_printf ("\n");
 
1535
      goto leave;
 
1536
    }
 
1537
 
1394
1538
  show_card_key_info (&info);
1395
1539
 
1396
 
  if (!is_RSA (sk->pubkey_algo) || nbits_from_sk (sk) != 1024 )
 
1540
  nbits = nbits_from_sk (sk);
 
1541
 
 
1542
  if (!is_RSA (sk->pubkey_algo) || (!info.is_v2 && nbits != 1024) )
1397
1543
    {
1398
1544
      tty_printf ("You may only store a 1024 bit RSA key on the card\n");
1399
1545
      tty_printf ("\n");
1426
1572
      keyno = *answer? atoi(answer): 0;
1427
1573
      xfree(answer);
1428
1574
      if (keyno >= 1 && keyno <= 3 && allow_keyno[keyno-1])
1429
 
        break; /* Okay. */
1430
 
      tty_printf(_("Invalid selection.\n"));
 
1575
        {
 
1576
          if (info.is_v2 && !info.extcap.aac 
 
1577
              && info.key_attr[keyno-1].nbits != nbits)
 
1578
            {
 
1579
              tty_printf ("Key does not match the card's capability.\n");
 
1580
            }
 
1581
          else
 
1582
            break; /* Okay. */
 
1583
        }
 
1584
      else
 
1585
        tty_printf(_("Invalid selection.\n"));
1431
1586
    }
1432
1587
 
1433
1588
  if (replace_existing_key_p (&info, keyno))
1461
1616
 
1462
1617
  rc = save_unprotected_key_to_card (sk, keyno);
1463
1618
  if (rc)
1464
 
    goto leave;
 
1619
    {
 
1620
      log_error (_("error writing key to card: %s\n"), gpg_strerror (rc));
 
1621
      goto leave;
 
1622
    }
1465
1623
 
1466
1624
  /* Get back to the maybe protected original secret key.  */
1467
1625
  if (copied_sk)
1546
1704
  };
1547
1705
 
1548
1706
 
1549
 
#if GNUPG_MAJOR_VERSION == 1 && defined (HAVE_LIBREADLINE)
 
1707
#ifdef HAVE_LIBREADLINE
1550
1708
 
1551
1709
/* These two functions are used by readline for command completion. */
1552
1710
 
1579
1737
static char **
1580
1738
card_edit_completion(const char *text, int start, int end)
1581
1739
{
 
1740
  (void)end;
1582
1741
  /* If we are at the start of a line, we try and command-complete.
1583
1742
     If not, just do nothing for now. */
1584
1743
 
1589
1748
 
1590
1749
  return NULL;
1591
1750
}
1592
 
#endif /* GNUPG_MAJOR_VERSION == 1 && HAVE_LIBREADLINE */
 
1751
#endif /*HAVE_LIBREADLINE*/
1593
1752
 
1594
1753
/* Menu to edit all user changeable values on an OpenPGP card.  Only
1595
1754
   Key creation is not handled here. */
1600
1759
  int have_commands = !!commands;
1601
1760
  int redisplay = 1;
1602
1761
  char *answer = NULL;
1603
 
  int did_checkpin = 0, allow_admin=0;
 
1762
  int allow_admin=0;
1604
1763
  char serialnobuf[50];
1605
1764
 
1606
1765
 
1657
1816
 
1658
1817
            if (!have_commands)
1659
1818
              {
1660
 
#if GNUPG_MAJOR_VERSION == 1
1661
1819
                tty_enable_completion (card_edit_completion);
1662
 
#endif
1663
1820
                answer = cpr_get_no_help("cardedit.prompt", _("Command> "));
1664
1821
                cpr_kill_prompt();
1665
 
#if GNUPG_MAJOR_VERSION == 1
1666
1822
                tty_disable_completion ();
1667
 
#endif
1668
 
            }
 
1823
              }
1669
1824
            trim_spaces(answer);
1670
1825
        }
1671
1826
      while ( *answer == '#' );
1812
1967
 
1813
1968
        case cmdPASSWD:
1814
1969
          change_pin (0, allow_admin);
1815
 
          did_checkpin = 0; /* Need to reset it of course. */
1816
1970
          break;
1817
1971
 
1818
1972
        case cmdUNBLOCK:
1819
1973
          change_pin (1, allow_admin);
1820
 
          did_checkpin = 0; /* Need to reset it of course. */
1821
1974
          break;
1822
1975
 
1823
1976
        case cmdQUIT: