~ubuntu-branches/ubuntu/precise/gnupg2/precise-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-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:
51
51
/* Change the PIN of a an OpenPGP card.  This is an interactive
52
52
   function. */
53
53
void
54
 
change_pin (int chvno, int allow_admin)
 
54
change_pin (int unblock_v2, int allow_admin)
55
55
{
56
56
  struct agent_card_info_s info;
57
57
  int rc;
76
76
      return;
77
77
    }
78
78
 
79
 
  if(!allow_admin)
 
79
 
 
80
  if (unblock_v2)
 
81
    {
 
82
      if (!info.is_v2)
 
83
        log_error (_("This command is only available for version 2 cards\n"));
 
84
      else if (!info.chvretry[1])
 
85
        log_error (_("Reset Code not or not anymore available\n"));
 
86
      else
 
87
        {
 
88
          rc = agent_scd_change_pin (2, info.serialno);
 
89
          if (rc)
 
90
            tty_printf ("Error changing the PIN: %s\n", gpg_strerror (rc));
 
91
          else
 
92
            {
 
93
              write_status (STATUS_SC_OP_SUCCESS);
 
94
              tty_printf ("PIN changed.\n");
 
95
            }
 
96
        }
 
97
    }
 
98
  else if (!allow_admin)
80
99
    {
81
100
      rc = agent_scd_change_pin (1, info.serialno);
82
101
      if (rc)
96
115
        tty_printf ("1 - change PIN\n"
97
116
                    "2 - unblock PIN\n"
98
117
                    "3 - change Admin PIN\n"
 
118
                    "4 - set the Reset Code\n"
99
119
                    "Q - quit\n");
100
120
        tty_printf ("\n");
101
121
 
107
127
        rc = 0;
108
128
        if (*answer == '1')
109
129
          {
 
130
            /* Change PIN.  */
110
131
            rc = agent_scd_change_pin (1, info.serialno);
111
132
            if (rc)
112
133
              tty_printf ("Error changing the PIN: %s\n", gpg_strerror (rc));
118
139
          }
119
140
        else if (*answer == '2')
120
141
          {
 
142
            /* Unblock PIN.  */
121
143
            rc = agent_scd_change_pin (101, info.serialno);
122
144
            if (rc)
123
145
              tty_printf ("Error unblocking the PIN: %s\n", gpg_strerror (rc));
129
151
          }
130
152
        else if (*answer == '3')
131
153
          {
 
154
            /* Change Admin PIN.  */
132
155
            rc = agent_scd_change_pin (3, info.serialno);
133
156
            if (rc)
134
157
              tty_printf ("Error changing the PIN: %s\n", gpg_strerror (rc));
138
161
                tty_printf ("PIN changed.\n");
139
162
              }
140
163
          }
 
164
        else if (*answer == '4')
 
165
          {
 
166
            /* Set a new Reset Code.  */
 
167
            rc = agent_scd_change_pin (102, info.serialno);
 
168
            if (rc)
 
169
              tty_printf ("Error setting the Reset Code: %s\n", 
 
170
                          gpg_strerror (rc));
 
171
            else
 
172
              {
 
173
                write_status (STATUS_SC_OP_SUCCESS);
 
174
                tty_printf ("Reset Code set.\n");
 
175
              }
 
176
          }
141
177
        else if (*answer == 'q' || *answer == 'Q')
142
178
          {
143
179
            break;
288
324
}
289
325
 
290
326
 
 
327
/* Return true if the SHA1 fingerprint FPR consists only of 0xFF. */
 
328
static int
 
329
fpr_is_ff (const char *fpr)
 
330
{
 
331
  int i;
 
332
 
 
333
  for (i=0; i < 20 && fpr[i] == '\xff'; i++)
 
334
    ;
 
335
  return (i == 20);
 
336
}
 
337
 
 
338
 
291
339
/* Print all available information about the current card. */
292
340
void
293
341
card_status (FILE *fp, char *serialno, size_t serialnobuflen)
321
369
  if (!info.serialno || strncmp (info.serialno, "D27600012401", 12) 
322
370
      || strlen (info.serialno) != 32 )
323
371
    {
324
 
      if (opt.with_colons)
325
 
        fputs ("unknown:\n", fp);
 
372
      if (info.apptype && !strcmp (info.apptype, "NKS"))
 
373
        {
 
374
          if (opt.with_colons)
 
375
            fputs ("netkey-card:\n", fp);
 
376
          log_info ("this is a NetKey card\n");
 
377
        }
 
378
      else if (info.apptype && !strcmp (info.apptype, "DINSIG"))
 
379
        {
 
380
          if (opt.with_colons)
 
381
            fputs ("dinsig-card:\n", fp);
 
382
          log_info ("this is a DINSIG compliant card\n");
 
383
        }
 
384
      else if (info.apptype && !strcmp (info.apptype, "P15"))
 
385
        {
 
386
          if (opt.with_colons)
 
387
            fputs ("pkcs15-card:\n", fp);
 
388
          log_info ("this is a PKCS#15 compliant card\n");
 
389
        }
 
390
      else if (info.apptype && !strcmp (info.apptype, "GELDKARTE"))
 
391
        {
 
392
          if (opt.with_colons)
 
393
            fputs ("geldkarte-card:\n", fp);
 
394
          log_info ("this is a Geldkarte compliant card\n");
 
395
        }
 
396
      else
 
397
        {
 
398
          if (opt.with_colons)
 
399
            fputs ("unknown:\n", fp);
 
400
        }
326
401
      log_info ("not an OpenPGP card\n");
327
402
      agent_release_card_info (&info);
328
403
      xfree (pk);
467
542
 
468
543
      thefpr = (info.fpr1valid? info.fpr1 : info.fpr2valid? info.fpr2 : 
469
544
                info.fpr3valid? info.fpr3 : NULL);
470
 
      if ( thefpr && !get_pubkey_byfprint (pk, thefpr, 20))
 
545
      /* If the fingerprint is all 0xff, the key has no asssociated
 
546
         OpenPGP certificate.  */
 
547
      if ( thefpr && !fpr_is_ff (thefpr) 
 
548
           && !get_pubkey_byfprint (pk, thefpr, 20))
471
549
        {
472
550
          KBNODE keyblock = NULL;
473
551
 
655
733
}
656
734
 
657
735
 
 
736
/* Read data from file FNAME up to MAXLEN characters.  On error return
 
737
   -1 and store NULl at R_BUFFER; on success return the number of
 
738
   bytes read and store the address of a newly allocated buffer at
 
739
   R_BUFFER. */
 
740
static int
 
741
get_data_from_file (const char *fname, size_t maxlen, char **r_buffer)
 
742
{
 
743
  FILE *fp;
 
744
  char *data;
 
745
  int n;
 
746
  
 
747
  *r_buffer = NULL;
 
748
 
 
749
  fp = fopen (fname, "rb");
 
750
#if GNUPG_MAJOR_VERSION == 1
 
751
  if (fp && is_secured_file (fileno (fp)))
 
752
    {
 
753
      fclose (fp);
 
754
      fp = NULL;
 
755
      errno = EPERM;
 
756
    }
 
757
#endif
 
758
  if (!fp)
 
759
    {
 
760
      tty_printf (_("can't open `%s': %s\n"), fname, strerror (errno));
 
761
      return -1;
 
762
    }
 
763
          
 
764
  data = xtrymalloc (maxlen? maxlen:1);
 
765
  if (!data)
 
766
    {
 
767
      tty_printf (_("error allocating enough memory: %s\n"), strerror (errno));
 
768
      fclose (fp);
 
769
      return -1;
 
770
    }
 
771
 
 
772
  if (maxlen)
 
773
    n = fread (data, 1, maxlen, fp);
 
774
  else
 
775
    n = 0;
 
776
  fclose (fp);
 
777
  if (n < 0)
 
778
    {
 
779
      tty_printf (_("error reading `%s': %s\n"), fname, strerror (errno));
 
780
      xfree (data);
 
781
      return -1;
 
782
    }
 
783
  *r_buffer = data;
 
784
  return n;
 
785
}
 
786
 
 
787
 
658
788
static int
659
789
change_login (const char *args)
660
790
{
664
794
 
665
795
  if (args && *args == '<')  /* Read it from a file */
666
796
    {
667
 
      FILE *fp;
668
 
 
669
797
      for (args++; spacep (args); args++)
670
798
        ;
671
 
      fp = fopen (args, "rb");
672
 
#if GNUPG_MAJOR_VERSION == 1
673
 
      if (fp && is_secured_file (fileno (fp)))
674
 
        {
675
 
          fclose (fp);
676
 
          fp = NULL;
677
 
          errno = EPERM;
678
 
        }
679
 
#endif
680
 
      if (!fp)
681
 
        {
682
 
          tty_printf (_("can't open `%s': %s\n"), args, strerror (errno));
683
 
          return -1;
684
 
        }
685
 
          
686
 
      data = xmalloc (254);
687
 
      n = fread (data, 1, 254, fp);
688
 
      fclose (fp);
 
799
      n = get_data_from_file (args, 254, &data);
689
800
      if (n < 0)
690
 
        {
691
 
          tty_printf (_("error reading `%s': %s\n"), args, strerror (errno));
692
 
          xfree (data);
693
 
          return -1;
694
 
        }
 
801
        return -1;
695
802
    }
696
803
  else
697
804
    {
732
839
 
733
840
  if (args && (args = strchr (args, '<')))  /* Read it from a file */
734
841
    {
735
 
      FILE *fp;
736
 
 
737
 
      /* Fixme: Factor this duplicated code out. */
738
842
      for (args++; spacep (args); args++)
739
843
        ;
740
 
      fp = fopen (args, "rb");
741
 
#if GNUPG_MAJOR_VERSION == 1
742
 
      if (fp && is_secured_file (fileno (fp)))
743
 
        {
744
 
          fclose (fp);
745
 
          fp = NULL;
746
 
          errno = EPERM;
747
 
        }
748
 
#endif
749
 
      if (!fp)
750
 
        {
751
 
          tty_printf (_("can't open `%s': %s\n"), args, strerror (errno));
752
 
          return -1;
753
 
        }
754
 
          
755
 
      data = xmalloc (254);
756
 
      n = fread (data, 1, 254, fp);
757
 
      fclose (fp);
 
844
      n = get_data_from_file (args, 254, &data);
758
845
      if (n < 0)
759
 
        {
760
 
          tty_printf (_("error reading `%s': %s\n"), args, strerror (errno));
761
 
          xfree (data);
762
 
          return -1;
763
 
        }
 
846
        return -1;
764
847
    }
765
848
  else
766
849
    {
788
871
  return rc;
789
872
}
790
873
 
 
874
 
 
875
static int
 
876
change_cert (const char *args)
 
877
{
 
878
  char *data;
 
879
  int n;
 
880
  int rc;
 
881
 
 
882
  if (args && *args == '<')  /* Read it from a file */
 
883
    {
 
884
      for (args++; spacep (args); args++)
 
885
        ;
 
886
      n = get_data_from_file (args, 16384, &data);
 
887
      if (n < 0)
 
888
        return -1;
 
889
    }
 
890
  else
 
891
    {
 
892
      tty_printf ("usage error: redirectrion to file required\n");
 
893
      return -1;
 
894
    }
 
895
 
 
896
  rc = agent_scd_writecert ("OPENPGP.3", data, n);
 
897
  if (rc)
 
898
    log_error ("error writing certificate to card: %s\n", gpg_strerror (rc));
 
899
  xfree (data);
 
900
  return rc;
 
901
}
 
902
 
 
903
 
791
904
static int
792
905
change_lang (void)
793
906
{
1041
1154
 
1042
1155
 
1043
1156
static void
1044
 
generate_card_keys (const char *serialno)
 
1157
generate_card_keys (void)
1045
1158
{
1046
1159
  struct agent_card_info_s info;
1047
1160
  int forced_chv1;
1294
1407
    cmdNOP = 0,
1295
1408
    cmdQUIT, cmdADMIN, cmdHELP, cmdLIST, cmdDEBUG, cmdVERIFY,
1296
1409
    cmdNAME, cmdURL, cmdFETCH, cmdLOGIN, cmdLANG, cmdSEX, cmdCAFPR,
1297
 
    cmdFORCESIG, cmdGENERATE, cmdPASSWD, cmdPRIVATEDO,
 
1410
    cmdFORCESIG, cmdGENERATE, cmdPASSWD, cmdPRIVATEDO, cmdWRITECERT,
 
1411
    cmdUNBLOCK,
1298
1412
    cmdINVCMD
1299
1413
  };
1300
1414
 
1325
1439
    { "generate", cmdGENERATE, 1, N_("generate new keys")},
1326
1440
    { "passwd"  , cmdPASSWD, 0, N_("menu to change or unblock the PIN")},
1327
1441
    { "verify"  , cmdVERIFY, 0, N_("verify the PIN and list all data")},
1328
 
    /* Note, that we do not announce this command yet. */
 
1442
    { "unblock" , cmdUNBLOCK,0, N_("unblock the PIN using a Reset Code") },
 
1443
    /* Note, that we do not announce these command yet. */
1329
1444
    { "privatedo", cmdPRIVATEDO, 0, NULL },
 
1445
    { "writecert", cmdWRITECERT, 1, NULL },
1330
1446
    { NULL, cmdINVCMD, 0, NULL } 
1331
1447
  };
1332
1448
 
1401
1517
    {
1402
1518
      int arg_number;
1403
1519
      const char *arg_string = "";
 
1520
      const char *arg_rest = "";
1404
1521
      char *p;
1405
1522
      int i;
1406
1523
      int cmd_admin_only;
1469
1586
              trim_spaces (p);
1470
1587
              arg_number = atoi(p);
1471
1588
              arg_string = p;
 
1589
              arg_rest = p;
 
1590
              while (digitp (arg_rest))
 
1591
                arg_rest++;
 
1592
              while (spacep (arg_rest))
 
1593
                arg_rest++;
1472
1594
            }
1473
1595
          
1474
1596
          for (i=0; cmds[i].name; i++ )
1567
1689
            change_private_do (arg_string, arg_number);
1568
1690
          break;
1569
1691
 
 
1692
        case cmdWRITECERT:
 
1693
          if ( arg_number != 3 )
 
1694
            tty_printf ("usage: writecert 3 < FILE\n");
 
1695
          else
 
1696
            change_cert (arg_rest);
 
1697
          break;
 
1698
 
1570
1699
        case cmdFORCESIG:
1571
1700
          toggle_forcesig ();
1572
1701
          break;
1573
1702
 
1574
1703
        case cmdGENERATE:
1575
 
          generate_card_keys (serialnobuf);
 
1704
          generate_card_keys ();
1576
1705
          break;
1577
1706
 
1578
1707
        case cmdPASSWD:
1580
1709
          did_checkpin = 0; /* Need to reset it of course. */
1581
1710
          break;
1582
1711
 
 
1712
        case cmdUNBLOCK:
 
1713
          change_pin (1, allow_admin);
 
1714
          did_checkpin = 0; /* Need to reset it of course. */
 
1715
          break;
 
1716
 
1583
1717
        case cmdQUIT:
1584
1718
          goto leave;
1585
1719