~ubuntu-branches/ubuntu/karmic/gnupg2/karmic-security

« back to all changes in this revision

Viewing changes to scd/app-openpgp.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* app-openpgp.c - The OpenPGP card application.
2
 
 *      Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
 
2
 * Copyright (C) 2003, 2004, 2005, 2007, 2008 Free Software Foundation, Inc.
3
3
 *
4
4
 * This file is part of GnuPG.
5
5
 *
6
6
 * GnuPG is free software; you can redistribute it and/or modify
7
7
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * the Free Software Foundation; either version 3 of the License, or
9
9
 * (at your option) any later version.
10
10
 *
11
11
 * GnuPG is distributed in the hope that it will be useful,
14
14
 * GNU General Public License for more details.
15
15
 *
16
16
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
17
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19
18
 *
20
 
 * $Id: app-openpgp.c,v 1.9.2.38 2005/06/16 08:11:59 wk Exp $
 
19
 * $Id: app-openpgp.c 4719 2008-03-26 09:20:40Z wk $
21
20
 */
22
21
 
23
22
#include <config.h>
112
111
                           encoded S-expression encoding a public
113
112
                           key. Might be NULL if key is not
114
113
                           available.  */
115
 
    size_t keylen;      /* The length of the above S-expression.  Thsi
116
 
                           is usullay only required for corss checks
 
114
    size_t keylen;      /* The length of the above S-expression.  This
 
115
                           is usullay only required for cross checks
117
116
                           because the length of an S-expression is
118
117
                           implicitly available.  */
119
118
  } pk[3];
141
140
static unsigned long convert_sig_counter_value (const unsigned char *value,
142
141
                                                size_t valuelen);
143
142
static unsigned long get_sig_counter (app_t app);
 
143
static gpg_error_t do_auth (app_t app, const char *keyidstr,
 
144
                            gpg_error_t (*pincb)(void*, const char *, char **),
 
145
                            void *pincb_arg,
 
146
                            const void *indata, size_t indatalen,
 
147
                            unsigned char **outdata, size_t *outdatalen);
144
148
 
145
149
 
146
150
 
484
488
   Everything up to a LF is considered a mailbox or account name.  If
485
489
   the first LF is followed by DC4 (0x14) control sequence are
486
490
   expected up to the next LF.  Control sequences are separated by FS
487
 
   (0x28) and consist of key=value pairs.  There is one key defined:
 
491
   (0x18) and consist of key=value pairs.  There is one key defined:
488
492
 
489
493
    F=<flags>
490
494
 
565
569
  n = 6 + 2 + mlen + 2 + elen;
566
570
  p = buffer = xtrymalloc (3 + n);
567
571
  if (!buffer)
568
 
    return gpg_error_from_errno (errno);
 
572
    return gpg_error_from_syserror ();
569
573
  
570
574
  *p++ = 0x99;     /* ctb */
571
575
  *p++ = n >> 8;   /* 2 byte length header */
879
883
        break; /* EOF. */
880
884
      if (i < 0)
881
885
        {
882
 
          err = gpg_error_from_errno (errno);
 
886
          err = gpg_error_from_syserror ();
883
887
          goto leave; /* Error. */
884
888
        }
885
889
      if (!max_length)
1035
1039
          mbuf = xtrymalloc ( mlen + 1);
1036
1040
          if (!mbuf)
1037
1041
            {
1038
 
              err = gpg_error_from_errno (errno);
 
1042
              err = gpg_error_from_syserror ();
1039
1043
              goto leave;
1040
1044
            }
1041
1045
          *mbuf = 0;
1048
1052
          ebuf = xtrymalloc ( elen + 1);
1049
1053
          if (!ebuf)
1050
1054
            {
1051
 
              err = gpg_error_from_errno (errno);
 
1055
              err = gpg_error_from_syserror ();
1052
1056
              goto leave;
1053
1057
            }
1054
1058
          *ebuf = 0;
1090
1094
                      fpr);
1091
1095
      if (ret < 0)
1092
1096
        {
1093
 
          err = gpg_error_from_errno (errno);
 
1097
          err = gpg_error_from_syserror ();
1094
1098
          goto leave;
1095
1099
        }
1096
1100
 
1098
1102
      free (command);
1099
1103
      if (!fp)
1100
1104
        {
1101
 
          err = gpg_error_from_errno (errno);
 
1105
          err = gpg_error_from_syserror ();
1102
1106
          log_error ("running gpg failed: %s\n", gpg_strerror (err));
1103
1107
          goto leave;
1104
1108
        }
1119
1123
  keybuf = xtrymalloc (50 + 2*35 + mlen + elen + 1);
1120
1124
  if (!keybuf)
1121
1125
    {
1122
 
      err = gpg_error_from_errno (errno);
 
1126
      err = gpg_error_from_syserror ();
1123
1127
      goto leave;
1124
1128
    }
1125
1129
  
1259
1263
  *pk = xtrymalloc (*pklen);
1260
1264
  if (!*pk)
1261
1265
    {
1262
 
      err = gpg_error_from_errno (errno);
 
1266
      err = gpg_error_from_syserror ();
1263
1267
      *pklen = 0;
1264
1268
      return err;
1265
1269
    }
1271
1275
}
1272
1276
 
1273
1277
 
 
1278
/* Verify a CHV either using using the pinentry or if possibile by
 
1279
   using a keypad.  PINCB and PINCB_ARG describe the usual callback
 
1280
   for the pinentry.  CHVNO must be either 1 or 2. SIGCOUNT is only
 
1281
   used with CHV1.  PINVALUE is the address of a pointer which will
 
1282
   receive a newly allocated block with the actual PIN (this is useful
 
1283
   in case that PIN shall be used for another verifiy operation).  The
 
1284
   caller needs to free this value.  If the function returns with
 
1285
   success and NULL is stored at PINVALUE, the caller should take this
 
1286
   as an indication that the keypad has been used.
 
1287
   */
 
1288
static gpg_error_t
 
1289
verify_a_chv (app_t app,
 
1290
              gpg_error_t (*pincb)(void*, const char *, char **),
 
1291
              void *pincb_arg,
 
1292
              int chvno, unsigned long sigcount, char **pinvalue)
 
1293
{
 
1294
  int rc = 0;
 
1295
  char *prompt;
 
1296
  iso7816_pininfo_t pininfo;
 
1297
  int minlen = 6;
 
1298
 
 
1299
  assert (chvno == 1 || chvno == 2);
 
1300
 
 
1301
  *pinvalue = NULL;
 
1302
 
 
1303
  memset (&pininfo, 0, sizeof pininfo);
 
1304
  pininfo.mode = 1;
 
1305
  pininfo.minlen = minlen;
 
1306
  
 
1307
  if (!opt.disable_keypad
 
1308
      && !iso7816_check_keypad (app->slot, ISO7816_VERIFY, &pininfo) )
 
1309
    {
 
1310
      /* The reader supports the verify command through the keypad. */
 
1311
 
 
1312
      if (chvno == 1)
 
1313
        {
 
1314
#define PROMPTSTRING  _("||Please enter your PIN at the reader's keypad%%0A" \
 
1315
                        "[sigs done: %lu]")
 
1316
          size_t promptsize = strlen (PROMPTSTRING) + 50;
 
1317
 
 
1318
          prompt = xmalloc (promptsize);
 
1319
          if (!prompt)
 
1320
            return gpg_error_from_syserror ();
 
1321
          snprintf (prompt, promptsize-1, PROMPTSTRING, sigcount);
 
1322
          rc = pincb (pincb_arg, prompt, NULL); 
 
1323
          xfree (prompt);
 
1324
#undef PROMPTSTRING
 
1325
        }
 
1326
      else
 
1327
        rc = pincb (pincb_arg,
 
1328
                    _("||Please enter your PIN at the reader's keypad"),
 
1329
                    NULL);
 
1330
      if (rc)
 
1331
        {
 
1332
          log_info (_("PIN callback returned error: %s\n"),
 
1333
                    gpg_strerror (rc));
 
1334
          return rc;
 
1335
        }
 
1336
      rc = iso7816_verify_kp (app->slot, 0x80+chvno, "", 0, &pininfo); 
 
1337
      /* Dismiss the prompt. */
 
1338
      pincb (pincb_arg, NULL, NULL);
 
1339
 
 
1340
      assert (!*pinvalue);
 
1341
    }
 
1342
  else
 
1343
    {
 
1344
      /* The reader has no keypad or we don't want to use it. */
 
1345
 
 
1346
      if (chvno == 1)
 
1347
        {
 
1348
#define PROMPTSTRING  _("||Please enter the PIN%%0A[sigs done: %lu]")
 
1349
          size_t promptsize = strlen (PROMPTSTRING) + 50;
 
1350
 
 
1351
          prompt = xmalloc (promptsize);
 
1352
          if (!prompt)
 
1353
            return gpg_error_from_syserror ();
 
1354
          snprintf (prompt, promptsize-1, PROMPTSTRING, sigcount);
 
1355
          rc = pincb (pincb_arg, prompt, pinvalue); 
 
1356
          xfree (prompt);
 
1357
#undef PROMPTSTRING
 
1358
        }
 
1359
      else
 
1360
        rc = pincb (pincb_arg, "PIN", pinvalue); 
 
1361
 
 
1362
      if (rc)
 
1363
        {
 
1364
          log_info (_("PIN callback returned error: %s\n"),
 
1365
                    gpg_strerror (rc));
 
1366
          return rc;
 
1367
        }
 
1368
      
 
1369
      if (strlen (*pinvalue) < minlen)
 
1370
        {
 
1371
          log_error (_("PIN for CHV%d is too short;"
 
1372
                       " minimum length is %d\n"), chvno, minlen);
 
1373
          xfree (*pinvalue);
 
1374
          *pinvalue = NULL;
 
1375
          return gpg_error (GPG_ERR_BAD_PIN);
 
1376
        }
 
1377
 
 
1378
      rc = iso7816_verify (app->slot, 0x80+chvno,
 
1379
                           *pinvalue, strlen (*pinvalue));
 
1380
    }
 
1381
  
 
1382
  if (rc)
 
1383
    {
 
1384
      log_error (_("verify CHV%d failed: %s\n"), chvno, gpg_strerror (rc));
 
1385
      xfree (*pinvalue);
 
1386
      *pinvalue = NULL;
 
1387
      flush_cache_after_error (app);
 
1388
    }
 
1389
 
 
1390
  return rc;
 
1391
}
 
1392
 
1274
1393
 
1275
1394
/* Verify CHV2 if required.  Depending on the configuration of the
1276
1395
   card CHV1 will also be verified. */
1279
1398
             gpg_error_t (*pincb)(void*, const char *, char **),
1280
1399
             void *pincb_arg)
1281
1400
{
1282
 
  int rc = 0;
1283
 
 
1284
 
  if (!app->did_chv2) 
 
1401
  int rc;
 
1402
  char *pinvalue;
 
1403
 
 
1404
  if (app->did_chv2) 
 
1405
    return 0;  /* We already verified CHV2.  */
 
1406
 
 
1407
  rc = verify_a_chv (app, pincb, pincb_arg, 2, 0, &pinvalue);
 
1408
  if (rc)
 
1409
    return rc;
 
1410
 
 
1411
  app->did_chv2 = 1;
 
1412
  
 
1413
  if (!app->did_chv1 && !app->force_chv1 && pinvalue)
1285
1414
    {
1286
 
      char *pinvalue;
1287
 
      iso7816_pininfo_t pininfo;
1288
 
 
1289
 
      memset (&pininfo, 0, sizeof pininfo);
1290
 
      pininfo.mode = 1;
1291
 
      pininfo.minlen = 6;
1292
 
 
1293
 
      rc = pincb (pincb_arg, "PIN", &pinvalue); 
1294
 
      if (rc)
1295
 
        {
1296
 
          log_info (_("PIN callback returned error: %s\n"), gpg_strerror (rc));
1297
 
          return rc;
1298
 
        }
1299
 
 
1300
 
      if (strlen (pinvalue) < 6)
1301
 
        {
1302
 
          log_error (_("PIN for CHV%d is too short;"
1303
 
                       " minimum length is %d\n"), 2, 6);
1304
 
          xfree (pinvalue);
1305
 
          return gpg_error (GPG_ERR_BAD_PIN);
1306
 
        }
1307
 
 
1308
 
      rc = iso7816_verify (app->slot, 0x82, pinvalue, strlen (pinvalue));
1309
 
      if (rc)
1310
 
        {
1311
 
          log_error (_("verify CHV%d failed: %s\n"), 2, gpg_strerror (rc));
1312
 
          xfree (pinvalue);
 
1415
      /* For convenience we verify CHV1 here too.  We do this only if
 
1416
         the card is not configured to require a verification before
 
1417
         each CHV1 controlled operation (force_chv1) and if we are not
 
1418
         using the keypad (PINVALUE == NULL). */
 
1419
      rc = iso7816_verify (app->slot, 0x81, pinvalue, strlen (pinvalue));
 
1420
      if (gpg_err_code (rc) == GPG_ERR_BAD_PIN)
 
1421
        rc = gpg_error (GPG_ERR_PIN_NOT_SYNCED);
 
1422
      if (rc)
 
1423
        {
 
1424
          log_error (_("verify CHV%d failed: %s\n"), 1, gpg_strerror (rc));
1313
1425
          flush_cache_after_error (app);
1314
 
          return rc;
1315
 
        }
1316
 
      app->did_chv2 = 1;
1317
 
 
1318
 
      if (!app->did_chv1 && !app->force_chv1)
1319
 
        {
1320
 
          rc = iso7816_verify (app->slot, 0x81, pinvalue, strlen (pinvalue));
1321
 
          if (gpg_err_code (rc) == GPG_ERR_BAD_PIN)
1322
 
            rc = gpg_error (GPG_ERR_PIN_NOT_SYNCED);
1323
 
          if (rc)
1324
 
            {
1325
 
              log_error (_("verify CHV%d failed: %s\n"), 1, gpg_strerror (rc));
1326
 
              xfree (pinvalue);
1327
 
              flush_cache_after_error (app);
1328
 
              return rc;
1329
 
            }
1330
 
          app->did_chv1 = 1;
1331
 
        }
1332
 
      xfree (pinvalue);
 
1426
        }
 
1427
      else
 
1428
        app->did_chv1 = 1;
1333
1429
    }
 
1430
  xfree (pinvalue);
 
1431
 
1334
1432
  return rc;
1335
1433
}
1336
1434
 
 
1435
 
1337
1436
/* Verify CHV3 if required. */
1338
1437
static gpg_error_t
1339
1438
verify_chv3 (app_t app,
1352
1451
      
1353
1452
  if (!app->did_chv3) 
1354
1453
    {
1355
 
      char *pinvalue;
1356
1454
      void *relptr;
1357
1455
      unsigned char *value;
1358
1456
      size_t valuelen;
 
1457
      iso7816_pininfo_t pininfo;
 
1458
      int minlen = 8;
 
1459
      int remaining;
 
1460
 
 
1461
      memset (&pininfo, 0, sizeof pininfo);
 
1462
      pininfo.mode = 1;
 
1463
      pininfo.minlen = minlen;
1359
1464
 
1360
1465
      relptr = get_one_do (app, 0x00C4, &value, &valuelen, NULL);
1361
1466
      if (!relptr || valuelen < 7)
1370
1475
          xfree (relptr);
1371
1476
          return gpg_error (GPG_ERR_BAD_PIN);
1372
1477
        }
 
1478
      remaining = value[6];
 
1479
      xfree (relptr);
1373
1480
 
1374
1481
      log_info(_("%d Admin PIN attempts remaining before card"
1375
 
                 " is permanently locked\n"), value[6]);
1376
 
      xfree (relptr);
 
1482
                 " is permanently locked\n"), remaining);
1377
1483
 
1378
 
      /* TRANSLATORS: Do not translate the "|A|" prefix but
1379
 
         keep it at the start of the string.  We need this elsewhere
1380
 
         to get some infos on the string. */
1381
 
      rc = pincb (pincb_arg, _("|A|Admin PIN"), &pinvalue); 
1382
 
      if (rc)
 
1484
      if (!opt.disable_keypad
 
1485
          && !iso7816_check_keypad (app->slot, ISO7816_VERIFY, &pininfo) )
1383
1486
        {
1384
 
          log_info (_("PIN callback returned error: %s\n"), gpg_strerror (rc));
1385
 
          return rc;
 
1487
          /* The reader supports the verify command through the keypad. */
 
1488
          
 
1489
          if (remaining < 3)
 
1490
            {
 
1491
#define PROMPTSTRING  _("|A|Please enter the Admin PIN" \
 
1492
                        " at the reader's keypad%%0A"   \
 
1493
                        "[remaining attempts: %d]")
 
1494
              size_t promptsize = strlen (PROMPTSTRING) + 50;
 
1495
              char *prompt;
 
1496
              
 
1497
              prompt = xmalloc (promptsize);
 
1498
              if (!prompt)
 
1499
                return gpg_error_from_syserror ();
 
1500
              snprintf (prompt, promptsize-1, PROMPTSTRING, remaining);
 
1501
              rc = pincb (pincb_arg, prompt, NULL); 
 
1502
              xfree (prompt);
 
1503
#undef PROMPTSTRING
 
1504
            }
 
1505
          else
 
1506
            rc = pincb (pincb_arg, _("|A|Please enter the Admin PIN"
 
1507
                                     " at the reader's keypad"),   NULL);
 
1508
 
 
1509
          if (rc)
 
1510
            {
 
1511
              log_info (_("PIN callback returned error: %s\n"),
 
1512
                        gpg_strerror (rc));
 
1513
              return rc;
 
1514
            }
 
1515
          rc = iso7816_verify_kp (app->slot, 0x83, "", 0, &pininfo); 
 
1516
          /* Dismiss the prompt. */
 
1517
          pincb (pincb_arg, NULL, NULL);
1386
1518
        }
 
1519
      else
 
1520
        {
 
1521
          char *pinvalue;
1387
1522
 
1388
 
      if (strlen (pinvalue) < 8)
1389
 
        {
1390
 
          log_error (_("PIN for CHV%d is too short;"
1391
 
                       " minimum length is %d\n"), 3, 8);
 
1523
          /* TRANSLATORS: Do not translate the "|A|" prefix but keep
 
1524
             it at the start of the string.  We need this elsewhere to
 
1525
             get some infos on the string. */
 
1526
          rc = pincb (pincb_arg, _("|A|Admin PIN"), &pinvalue); 
 
1527
          if (rc)
 
1528
            {
 
1529
              log_info (_("PIN callback returned error: %s\n"),
 
1530
                        gpg_strerror (rc));
 
1531
              return rc;
 
1532
            }
 
1533
          
 
1534
          if (strlen (pinvalue) < minlen)
 
1535
            {
 
1536
              log_error (_("PIN for CHV%d is too short;"
 
1537
                           " minimum length is %d\n"), 3, minlen);
 
1538
              xfree (pinvalue);
 
1539
              return gpg_error (GPG_ERR_BAD_PIN);
 
1540
            }
 
1541
          
 
1542
          rc = iso7816_verify (app->slot, 0x83, pinvalue, strlen (pinvalue));
1392
1543
          xfree (pinvalue);
1393
 
          return gpg_error (GPG_ERR_BAD_PIN);
1394
1544
        }
1395
 
 
1396
 
      rc = iso7816_verify (app->slot, 0x83, pinvalue, strlen (pinvalue));
1397
 
      xfree (pinvalue);
 
1545
      
1398
1546
      if (rc)
1399
1547
        {
1400
1548
          log_error (_("verify CHV%d failed: %s\n"), 3, gpg_strerror (rc));
1401
1549
          flush_cache_after_error (app);
1402
1550
          return rc;
1403
1551
        }
1404
 
      app->did_chv3 = 1;
1405
1552
    }
1406
1553
  return rc;
1407
1554
}
1818
1965
  template = tp = xtrymalloc_secure (template_len);
1819
1966
  if (!template)
1820
1967
    {
1821
 
      err = gpg_error_from_errno (errno);
 
1968
      err = gpg_error_from_syserror ();
1822
1969
      goto leave;
1823
1970
    }
1824
1971
  *tp++ = 0xC0;
1827
1974
  if (rsa_e_len < 4)
1828
1975
    {
1829
1976
      /* Right justify E. */
1830
 
      memmove (tp+4-rsa_e_len, tp, 4-rsa_e_len);
 
1977
      memmove (tp+4-rsa_e_len, tp, rsa_e_len);
1831
1978
      memset (tp, 0, 4-rsa_e_len);
1832
1979
    }                 
1833
1980
  tp += 4;
1882
2029
/* Handle the GENKEY command. */
1883
2030
static gpg_error_t 
1884
2031
do_genkey (app_t app, ctrl_t ctrl,  const char *keynostr, unsigned int flags,
1885
 
          gpg_error_t (*pincb)(void*, const char *, char **),
1886
 
          void *pincb_arg)
 
2032
           time_t createtime,
 
2033
           gpg_error_t (*pincb)(void*, const char *, char **),
 
2034
           void *pincb_arg)
1887
2035
{
1888
2036
  int rc;
1889
2037
  char numbuf[30];
1915
2063
  if (rc)
1916
2064
    return rc;
1917
2065
 
1918
 
  /* Prepare for key generation by verifying the ADmin PIN.  */
 
2066
  /* Prepare for key generation by verifying the Admin PIN.  */
1919
2067
  rc = verify_chv3 (app, pincb, pincb_arg);
1920
2068
  if (rc)
1921
2069
    goto leave;
1925
2073
  start_at = time (NULL);
1926
2074
  rc = iso7816_generate_keypair 
1927
2075
#else
1928
 
#warning key generation temporary replaced by reading an existing key.
 
2076
# warning key generation temporary replaced by reading an existing key.
1929
2077
  rc = iso7816_read_public_key
1930
2078
#endif
1931
2079
    (app->slot, (const unsigned char*)(keyno == 0? "\xB6" :
1968
2116
/*    log_printhex ("RSA e:", e, elen); */
1969
2117
  send_key_data (ctrl, "e", e, elen);
1970
2118
 
1971
 
  created_at = gnupg_get_time ();
 
2119
  created_at = createtime? createtime : gnupg_get_time ();
1972
2120
  sprintf (numbuf, "%lu", (unsigned long)created_at);
1973
2121
  send_status_info (ctrl, "KEY-CREATED-AT",
1974
2122
                    numbuf, (size_t)strlen(numbuf), NULL, 0);
2045
2193
    if (sha1fpr[i] != fpr[i])
2046
2194
      {
2047
2195
        xfree (buffer);
 
2196
        log_info (_("fingerprint on card does not match requested one\n"));
2048
2197
        return gpg_error (GPG_ERR_WRONG_SECKEY);
2049
2198
      }
2050
2199
  xfree (buffer);
2084
2233
   raw message digest. For this application the KEYIDSTR consists of
2085
2234
   the serialnumber and the fingerprint delimited by a slash.
2086
2235
 
2087
 
   Note that this fucntion may return the error code
 
2236
   Note that this function may return the error code
2088
2237
   GPG_ERR_WRONG_CARD to indicate that the card currently present does
2089
2238
   not match the one required for the requested action (e.g. the
2090
 
   serial number does not match). */
 
2239
   serial number does not match). 
 
2240
   
 
2241
   As a special feature a KEYIDSTR of "OPENPGP.3" redirects the
 
2242
   operation to the auth command.
 
2243
*/
2091
2244
static gpg_error_t 
2092
2245
do_sign (app_t app, const char *keyidstr, int hashalgo,
2093
2246
         gpg_error_t (*pincb)(void*, const char *, char **),
2108
2261
  int n;
2109
2262
  const char *fpr = NULL;
2110
2263
  unsigned long sigcount;
 
2264
  int use_auth = 0;
2111
2265
 
2112
2266
  if (!keyidstr || !*keyidstr)
2113
2267
    return gpg_error (GPG_ERR_INV_VALUE);
2115
2269
    ;
2116
2270
  else if (indatalen == (15 + 20) && hashalgo == GCRY_MD_SHA1
2117
2271
           && !memcmp (indata, sha1_prefix, 15))
2118
 
    ;
 
2272
    {
 
2273
      indata = (const char*)indata + 15;
 
2274
      indatalen -= 15;
 
2275
    }
2119
2276
  else if (indatalen == (15 + 20) && hashalgo == GCRY_MD_RMD160
2120
2277
           && !memcmp (indata, rmd160_prefix, 15))
2121
 
    ;
 
2278
    {
 
2279
      indata = (const char*)indata + 15;
 
2280
      indatalen -= 15;
 
2281
    }
2122
2282
  else
2123
 
    return gpg_error (GPG_ERR_INV_VALUE);
 
2283
    {
 
2284
      log_error (_("card does not support digest algorithm %s\n"),
 
2285
                 gcry_md_algo_name (hashalgo));
 
2286
      return gpg_error (GPG_ERR_INV_VALUE);
 
2287
    }
2124
2288
 
2125
2289
  /* Check whether an OpenPGP card of any version has been requested. */
2126
 
  if (strlen (keyidstr) < 32 || strncmp (keyidstr, "D27600012401", 12))
2127
 
    return gpg_error (GPG_ERR_INV_ID);
2128
 
  
2129
 
  for (s=keyidstr, n=0; hexdigitp (s); s++, n++)
 
2290
  if (!strcmp (keyidstr, "OPENPGP.1"))
2130
2291
    ;
2131
 
  if (n != 32)
 
2292
  else if (!strcmp (keyidstr, "OPENPGP.3"))
 
2293
    use_auth = 1;
 
2294
  else if (strlen (keyidstr) < 32 || strncmp (keyidstr, "D27600012401", 12))
2132
2295
    return gpg_error (GPG_ERR_INV_ID);
2133
 
  else if (!*s)
2134
 
    ; /* no fingerprint given: we allow this for now. */
2135
 
  else if (*s == '/')
2136
 
    fpr = s + 1; 
2137
2296
  else
2138
 
    return gpg_error (GPG_ERR_INV_ID);
2139
 
 
2140
 
  for (s=keyidstr, n=0; n < 16; s += 2, n++)
2141
 
    tmp_sn[n] = xtoi_2 (s);
2142
 
 
2143
 
  if (app->serialnolen != 16)
2144
 
    return gpg_error (GPG_ERR_INV_CARD);
2145
 
  if (memcmp (app->serialno, tmp_sn, 16))
2146
 
    return gpg_error (GPG_ERR_WRONG_CARD);
 
2297
    {
 
2298
      for (s=keyidstr, n=0; hexdigitp (s); s++, n++)
 
2299
        ;
 
2300
      if (n != 32)
 
2301
        return gpg_error (GPG_ERR_INV_ID);
 
2302
      else if (!*s)
 
2303
        ; /* no fingerprint given: we allow this for now. */
 
2304
      else if (*s == '/')
 
2305
        fpr = s + 1; 
 
2306
      else
 
2307
        return gpg_error (GPG_ERR_INV_ID);
 
2308
 
 
2309
      for (s=keyidstr, n=0; n < 16; s += 2, n++)
 
2310
        tmp_sn[n] = xtoi_2 (s);
 
2311
 
 
2312
      if (app->serialnolen != 16)
 
2313
        return gpg_error (GPG_ERR_INV_CARD);
 
2314
      if (memcmp (app->serialno, tmp_sn, 16))
 
2315
        return gpg_error (GPG_ERR_WRONG_CARD);
 
2316
    }
2147
2317
 
2148
2318
  /* If a fingerprint has been specified check it against the one on
2149
2319
     the card.  This is allows for a meaningful error message in case
2163
2333
    return gpg_error (GPG_ERR_UNSUPPORTED_ALGORITHM);
2164
2334
  memcpy (data+15, indata, indatalen);
2165
2335
 
 
2336
  if (use_auth)
 
2337
    {
 
2338
      /* This is a hack to redirect to the internal authenticate command.  */
 
2339
      return do_auth (app, "OPENPGP.3", pincb, pincb_arg,
 
2340
                      data, 35,
 
2341
                      outdata, outdatalen);
 
2342
    }
 
2343
 
2166
2344
  sigcount = get_sig_counter (app);
2167
2345
  log_info (_("signatures created so far: %lu\n"), sigcount);
2168
2346
 
2170
2348
    {
2171
2349
      char *pinvalue;
2172
2350
 
2173
 
      {
2174
 
        char *prompt;
2175
 
#define PROMPTSTRING  _("||Please enter the PIN%%0A[sigs done: %lu]")
2176
 
 
2177
 
        prompt = malloc (strlen (PROMPTSTRING) + 50);
2178
 
        if (!prompt)
2179
 
          return gpg_error_from_errno (errno);
2180
 
        sprintf (prompt, PROMPTSTRING, sigcount);
2181
 
        rc = pincb (pincb_arg, prompt, &pinvalue); 
2182
 
        free (prompt);
2183
 
#undef PROMPTSTRING
2184
 
      }
2185
 
      if (rc)
2186
 
        {
2187
 
          log_info (_("PIN callback returned error: %s\n"), gpg_strerror (rc));
2188
 
          return rc;
2189
 
        }
2190
 
 
2191
 
      if (strlen (pinvalue) < 6)
2192
 
        {
2193
 
          log_error (_("PIN for CHV%d is too short;"
2194
 
                       " minimum length is %d\n"), 1, 6);
2195
 
          xfree (pinvalue);
2196
 
          return gpg_error (GPG_ERR_BAD_PIN);
2197
 
        }
2198
 
 
2199
 
      rc = iso7816_verify (app->slot, 0x81, pinvalue, strlen (pinvalue));
2200
 
      if (rc)
2201
 
        {
2202
 
          log_error (_("verify CHV%d failed: %s\n"), 1, gpg_strerror (rc));
2203
 
          xfree (pinvalue);
2204
 
          flush_cache_after_error (app);
2205
 
          return rc;
2206
 
        }
 
2351
      rc = verify_a_chv (app, pincb, pincb_arg, 1, sigcount, &pinvalue);
 
2352
      if (rc)
 
2353
        return rc;
 
2354
 
2207
2355
      app->did_chv1 = 1;
2208
 
      if (!app->did_chv2)
 
2356
 
 
2357
      if (!app->did_chv2 && pinvalue)
2209
2358
        {
2210
 
          /* We should also verify CHV2. */
 
2359
          /* We should also verify CHV2.  Note, that we can't do that
 
2360
             if the keypad has been used. */
2211
2361
          rc = iso7816_verify (app->slot, 0x82, pinvalue, strlen (pinvalue));
2212
2362
          if (gpg_err_code (rc) == GPG_ERR_BAD_PIN)
2213
2363
            rc = gpg_error (GPG_ERR_PIN_NOT_SYNCED);
2317
2467
    return gpg_error (GPG_ERR_INV_VALUE);
2318
2468
 
2319
2469
  /* Check whether an OpenPGP card of any version has been requested. */
2320
 
  if (strlen (keyidstr) < 32 || strncmp (keyidstr, "D27600012401", 12))
2321
 
    return gpg_error (GPG_ERR_INV_ID);
2322
 
  
2323
 
  for (s=keyidstr, n=0; hexdigitp (s); s++, n++)
 
2470
  if (!strcmp (keyidstr, "OPENPGP.2"))
2324
2471
    ;
2325
 
  if (n != 32)
 
2472
  else if (strlen (keyidstr) < 32 || strncmp (keyidstr, "D27600012401", 12))
2326
2473
    return gpg_error (GPG_ERR_INV_ID);
2327
 
  else if (!*s)
2328
 
    ; /* no fingerprint given: we allow this for now. */
2329
 
  else if (*s == '/')
2330
 
    fpr = s + 1; 
2331
2474
  else
2332
 
    return gpg_error (GPG_ERR_INV_ID);
2333
 
 
2334
 
  for (s=keyidstr, n=0; n < 16; s += 2, n++)
2335
 
    tmp_sn[n] = xtoi_2 (s);
2336
 
 
2337
 
  if (app->serialnolen != 16)
2338
 
    return gpg_error (GPG_ERR_INV_CARD);
2339
 
  if (memcmp (app->serialno, tmp_sn, 16))
2340
 
    return gpg_error (GPG_ERR_WRONG_CARD);
 
2475
    {
 
2476
      for (s=keyidstr, n=0; hexdigitp (s); s++, n++)
 
2477
        ;
 
2478
      if (n != 32)
 
2479
        return gpg_error (GPG_ERR_INV_ID);
 
2480
      else if (!*s)
 
2481
        ; /* no fingerprint given: we allow this for now. */
 
2482
      else if (*s == '/')
 
2483
        fpr = s + 1; 
 
2484
      else
 
2485
        return gpg_error (GPG_ERR_INV_ID);
 
2486
      
 
2487
      for (s=keyidstr, n=0; n < 16; s += 2, n++)
 
2488
        tmp_sn[n] = xtoi_2 (s);
 
2489
      
 
2490
      if (app->serialnolen != 16)
 
2491
        return gpg_error (GPG_ERR_INV_CARD);
 
2492
      if (memcmp (app->serialno, tmp_sn, 16))
 
2493
        return gpg_error (GPG_ERR_WRONG_CARD);
 
2494
    }
2341
2495
 
2342
2496
  /* If a fingerprint has been specified check it against the one on
2343
2497
     the card.  This is allows for a meaningful error message in case
2350
2504
 
2351
2505
  rc = verify_chv2 (app, pincb, pincb_arg);
2352
2506
  if (!rc)
2353
 
    rc = iso7816_decipher (app->slot, indata, indatalen, 0,
2354
 
                           outdata, outdatalen);
 
2507
    {
 
2508
      size_t fixuplen;
 
2509
 
 
2510
      /* We might encounter a couple of leading zeroes in the
 
2511
         cryptogram.  Due to internal use of MPIs thease leading
 
2512
         zeroes are stripped.  However the OpenPGP card expects
 
2513
         exactly 128 bytes for the cryptogram (for a 1k key).  Thus we
 
2514
         need to fix it up.  We do this for up to 16 leading zero
 
2515
         bytes; a cryptogram with more than this is with a very high
 
2516
         probability anyway broken.  */
 
2517
      if (indatalen >= (128-16) && indatalen < 128)      /* 1024 bit key.  */
 
2518
        fixuplen = 128 - indatalen;
 
2519
      else if (indatalen >= (256-16) && indatalen < 256) /* 2048 bit key.  */
 
2520
        fixuplen = 256 - indatalen;
 
2521
      else if (indatalen >= (192-16) && indatalen < 192) /* 1536 bit key.  */
 
2522
        fixuplen = 192 - indatalen;
 
2523
      else
 
2524
        fixuplen = 0;
 
2525
      if (fixuplen)
 
2526
        {
 
2527
          unsigned char *fixbuf;
 
2528
 
 
2529
          /* While we have to prepend stuff anyway, we can also
 
2530
             include the padding byte here so that iso1816_decipher
 
2531
             does not need to do yet another data mangling.  */
 
2532
          fixuplen++;
 
2533
          fixbuf = xtrymalloc (fixuplen + indatalen);
 
2534
          if (!fixbuf)
 
2535
            rc = gpg_error_from_syserror ();
 
2536
          else
 
2537
            {
 
2538
              memset (fixbuf, 0, fixuplen);
 
2539
              memcpy (fixbuf+fixuplen, indata, indatalen);
 
2540
              rc = iso7816_decipher (app->slot, fixbuf, fixuplen+indatalen, -1,
 
2541
                                     outdata, outdatalen);
 
2542
              xfree (fixbuf);
 
2543
            }
 
2544
 
 
2545
        }
 
2546
      else
 
2547
        rc = iso7816_decipher (app->slot, indata, indatalen, 0,
 
2548
                               outdata, outdatalen);
 
2549
    }
2355
2550
  return rc;
2356
2551
}
2357
2552