chiark / gitweb /
Replace use of variable-length-arrays.
[gnupg2.git] / g10 / card-util.c
1 /* card-util.c - Utility functions for the OpenPGP card.
2  * Copyright (C) 2003-2005, 2009 Free Software Foundation, Inc.
3  * Copyright (C) 2003-2005, 2009 Werner Koch
4  *
5  * This file is part of GnuPG.
6  *
7  * GnuPG is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * GnuPG is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <https://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #ifdef HAVE_LIBREADLINE
27 # define GNUPG_LIBREADLINE_H_INCLUDED
28 # include <readline/readline.h>
29 #endif /*HAVE_LIBREADLINE*/
30
31 #if GNUPG_MAJOR_VERSION != 1
32 # include "gpg.h"
33 #endif /*GNUPG_MAJOR_VERSION != 1*/
34 #include "util.h"
35 #include "i18n.h"
36 #include "ttyio.h"
37 #include "status.h"
38 #include "options.h"
39 #include "main.h"
40 #include "keyserver-internal.h"
41
42 #if GNUPG_MAJOR_VERSION == 1
43 # include "cardglue.h"
44 #else /*GNUPG_MAJOR_VERSION!=1*/
45 # include "call-agent.h"
46 #endif /*GNUPG_MAJOR_VERSION!=1*/
47
48 #define CONTROL_D ('D' - 'A' + 1)
49
50
51 static void
52 write_sc_op_status (gpg_error_t err)
53 {
54   switch (gpg_err_code (err))
55     {
56     case 0:
57       write_status (STATUS_SC_OP_SUCCESS);
58       break;
59 #if GNUPG_MAJOR_VERSION != 1
60     case GPG_ERR_CANCELED:
61     case GPG_ERR_FULLY_CANCELED:
62       write_status_text (STATUS_SC_OP_FAILURE, "1");
63       break;
64     case GPG_ERR_BAD_PIN:
65       write_status_text (STATUS_SC_OP_FAILURE, "2");
66       break;
67     default:
68       write_status (STATUS_SC_OP_FAILURE);
69       break;
70 #endif /* GNUPG_MAJOR_VERSION != 1 */
71     }
72 }
73
74
75 /* Change the PIN of a an OpenPGP card.  This is an interactive
76    function. */
77 void
78 change_pin (int unblock_v2, int allow_admin)
79 {
80   struct agent_card_info_s info;
81   int rc;
82
83   rc = agent_scd_learn (&info, 0);
84   if (rc)
85     {
86       log_error (_("OpenPGP card not available: %s\n"),
87                   gpg_strerror (rc));
88       return;
89     }
90
91   log_info (_("OpenPGP card no. %s detected\n"),
92               info.serialno? info.serialno : "[none]");
93
94   agent_clear_pin_cache (info.serialno);
95
96   if (opt.batch)
97     {
98       agent_release_card_info (&info);
99       log_error (_("can't do this in batch mode\n"));
100       return;
101     }
102
103
104   if (unblock_v2)
105     {
106       if (!info.is_v2)
107         log_error (_("This command is only available for version 2 cards\n"));
108       else if (!info.chvretry[1])
109         log_error (_("Reset Code not or not anymore available\n"));
110       else
111         {
112           rc = agent_scd_change_pin (2, info.serialno);
113           write_sc_op_status (rc);
114           if (rc)
115             tty_printf ("Error changing the PIN: %s\n", gpg_strerror (rc));
116           else
117             tty_printf ("PIN changed.\n");
118         }
119     }
120   else if (!allow_admin)
121     {
122       rc = agent_scd_change_pin (1, info.serialno);
123       write_sc_op_status (rc);
124       if (rc)
125         tty_printf ("Error changing the PIN: %s\n", gpg_strerror (rc));
126       else
127         tty_printf ("PIN changed.\n");
128     }
129   else
130     for (;;)
131       {
132         char *answer;
133
134         tty_printf ("\n");
135         tty_printf ("1 - change PIN\n"
136                     "2 - unblock PIN\n"
137                     "3 - change Admin PIN\n"
138                     "4 - set the Reset Code\n"
139                     "Q - quit\n");
140         tty_printf ("\n");
141
142         answer = cpr_get("cardutil.change_pin.menu",_("Your selection? "));
143         cpr_kill_prompt();
144         if (strlen (answer) != 1)
145           continue;
146
147         if (*answer == '1')
148           {
149             /* Change PIN.  */
150             rc = agent_scd_change_pin (1, info.serialno);
151             write_sc_op_status (rc);
152             if (rc)
153               tty_printf ("Error changing the PIN: %s\n", gpg_strerror (rc));
154             else
155               tty_printf ("PIN changed.\n");
156           }
157         else if (*answer == '2')
158           {
159             /* Unblock PIN.  */
160             rc = agent_scd_change_pin (101, info.serialno);
161             write_sc_op_status (rc);
162             if (rc)
163               tty_printf ("Error unblocking the PIN: %s\n", gpg_strerror (rc));
164             else
165               tty_printf ("PIN unblocked and new PIN set.\n");
166           }
167         else if (*answer == '3')
168           {
169             /* Change Admin PIN.  */
170             rc = agent_scd_change_pin (3, info.serialno);
171             write_sc_op_status (rc);
172             if (rc)
173               tty_printf ("Error changing the PIN: %s\n", gpg_strerror (rc));
174             else
175               tty_printf ("PIN changed.\n");
176           }
177         else if (*answer == '4')
178           {
179             /* Set a new Reset Code.  */
180             rc = agent_scd_change_pin (102, info.serialno);
181             write_sc_op_status (rc);
182             if (rc)
183               tty_printf ("Error setting the Reset Code: %s\n",
184                           gpg_strerror (rc));
185             else
186               tty_printf ("Reset Code set.\n");
187           }
188         else if (*answer == 'q' || *answer == 'Q')
189           {
190             break;
191           }
192       }
193
194   agent_release_card_info (&info);
195 }
196
197 static const char *
198 get_manufacturer (unsigned int no)
199 {
200   /* Note:  Make sure that there is no colon or linefeed in the string. */
201   switch (no)
202     {
203     case 0x0001: return "PPC Card Systems";
204     case 0x0002: return "Prism";
205     case 0x0003: return "OpenFortress";
206     case 0x0004: return "Wewid";
207     case 0x0005: return "ZeitControl";
208     case 0x0006: return "Yubico";
209     case 0x0007: return "OpenKMS";
210     case 0x0008: return "LogoEmail";
211     case 0x0009: return "Fidesmo";
212     case 0x000A: return "Dangerous Things";
213
214     case 0x002A: return "Magrathea";
215
216     case 0x1337: return "Warsaw Hackerspace";
217     case 0x2342: return "warpzone"; /* hackerspace Muenster.  */
218     case 0xF517: return "FSIJ";
219
220       /* 0x0000 and 0xFFFF are defined as test cards per spec,
221          0xFF00 to 0xFFFE are assigned for use with randomly created
222          serial numbers.  */
223     case 0x0000:
224     case 0xffff: return "test card";
225     default: return (no & 0xff00) == 0xff00? "unmanaged S/N range":"unknown";
226     }
227 }
228
229
230 static void
231 print_sha1_fpr (estream_t fp, const unsigned char *fpr)
232 {
233   int i;
234
235   if (fpr)
236     {
237       for (i=0; i < 20 ; i+=2, fpr += 2 )
238         {
239           if (i == 10 )
240             tty_fprintf (fp, " ");
241           tty_fprintf (fp, " %02X%02X", *fpr, fpr[1]);
242         }
243     }
244   else
245     tty_fprintf (fp, " [none]");
246   tty_fprintf (fp, "\n");
247 }
248
249
250 static void
251 print_sha1_fpr_colon (estream_t fp, const unsigned char *fpr)
252 {
253   int i;
254
255   if (fpr)
256     {
257       for (i=0; i < 20 ; i++, fpr++)
258         es_fprintf (fp, "%02X", *fpr);
259     }
260   es_putc (':', fp);
261 }
262
263
264 static void
265 print_name (estream_t fp, const char *text, const char *name)
266 {
267   tty_fprintf (fp, "%s", text);
268
269   /* FIXME: tty_printf_utf8_string2 eats everything after and
270      including an @ - e.g. when printing an url. */
271   if (name && *name)
272     {
273       if (fp)
274         print_utf8_buffer2 (fp, name, strlen (name), '\n');
275       else
276         tty_print_utf8_string2 (NULL, name, strlen (name), 0);
277     }
278   else
279     tty_fprintf (fp, _("[not set]"));
280   tty_fprintf (fp, "\n");
281 }
282
283 static void
284 print_isoname (estream_t fp, const char *text,
285                const char *tag, const char *name)
286 {
287   if (opt.with_colons)
288     es_fprintf (fp, "%s:", tag);
289   else
290     tty_fprintf (fp, "%s", text);
291
292   if (name && *name)
293     {
294       char *p, *given, *buf = xstrdup (name);
295
296       given = strstr (buf, "<<");
297       for (p=buf; *p; p++)
298         if (*p == '<')
299           *p = ' ';
300       if (given && given[2])
301         {
302           *given = 0;
303           given += 2;
304           if (opt.with_colons)
305             es_write_sanitized (fp, given, strlen (given), ":", NULL);
306           else if (fp)
307             print_utf8_buffer2 (fp, given, strlen (given), '\n');
308           else
309             tty_print_utf8_string2 (NULL, given, strlen (given), 0);
310
311           if (opt.with_colons)
312             es_putc (':', fp);
313           else if (*buf)
314             tty_fprintf (fp, " ");
315         }
316
317       if (opt.with_colons)
318         es_write_sanitized (fp, buf, strlen (buf), ":", NULL);
319       else if (fp)
320         print_utf8_buffer2 (fp, buf, strlen (buf), '\n');
321       else
322         tty_print_utf8_string2 (NULL, buf, strlen (buf), 0);
323       xfree (buf);
324     }
325   else
326     {
327       if (opt.with_colons)
328         es_putc (':', fp);
329       else
330         tty_fprintf (fp, _("[not set]"));
331     }
332
333   if (opt.with_colons)
334     es_fputs (":\n", fp);
335   else
336     tty_fprintf (fp, "\n");
337 }
338
339 /* Return true if the SHA1 fingerprint FPR consists only of zeroes. */
340 static int
341 fpr_is_zero (const char *fpr)
342 {
343   int i;
344
345   for (i=0; i < 20 && !fpr[i]; i++)
346     ;
347   return (i == 20);
348 }
349
350
351 /* Return true if the SHA1 fingerprint FPR consists only of 0xFF. */
352 static int
353 fpr_is_ff (const char *fpr)
354 {
355   int i;
356
357   for (i=0; i < 20 && fpr[i] == '\xff'; i++)
358     ;
359   return (i == 20);
360 }
361
362
363 /* Print all available information about the current card. */
364 void
365 card_status (estream_t fp, char *serialno, size_t serialnobuflen)
366 {
367   struct agent_card_info_s info;
368   PKT_public_key *pk = xcalloc (1, sizeof *pk);
369   kbnode_t keyblock = NULL;
370   int rc;
371   unsigned int uval;
372   const unsigned char *thefpr;
373   int i;
374
375   if (serialno && serialnobuflen)
376     *serialno = 0;
377
378   rc = agent_scd_learn (&info, 0);
379   if (rc)
380     {
381       if (opt.with_colons)
382         es_fputs ("AID:::\n", fp);
383       log_error (_("OpenPGP card not available: %s\n"), gpg_strerror (rc));
384       xfree (pk);
385       return;
386     }
387
388   if (opt.with_colons)
389     es_fprintf (fp, "Reader:%s:", info.reader? info.reader : "");
390   else
391     tty_fprintf (fp, "Reader ...........: %s\n",
392                  info.reader? info.reader : "[none]");
393   if (opt.with_colons)
394     es_fprintf (fp, "AID:%s:", info.serialno? info.serialno : "");
395   else
396     tty_fprintf (fp, "Application ID ...: %s\n",
397                  info.serialno? info.serialno : "[none]");
398   if (!info.serialno || strncmp (info.serialno, "D27600012401", 12)
399       || strlen (info.serialno) != 32 )
400     {
401       if (info.apptype && !strcmp (info.apptype, "NKS"))
402         {
403           if (opt.with_colons)
404             es_fputs ("netkey-card:\n", fp);
405           log_info ("this is a NetKey card\n");
406         }
407       else if (info.apptype && !strcmp (info.apptype, "DINSIG"))
408         {
409           if (opt.with_colons)
410             es_fputs ("dinsig-card:\n", fp);
411           log_info ("this is a DINSIG compliant card\n");
412         }
413       else if (info.apptype && !strcmp (info.apptype, "P15"))
414         {
415           if (opt.with_colons)
416             es_fputs ("pkcs15-card:\n", fp);
417           log_info ("this is a PKCS#15 compliant card\n");
418         }
419       else if (info.apptype && !strcmp (info.apptype, "GELDKARTE"))
420         {
421           if (opt.with_colons)
422             es_fputs ("geldkarte-card:\n", fp);
423           log_info ("this is a Geldkarte compliant card\n");
424         }
425       else
426         {
427           if (opt.with_colons)
428             es_fputs ("unknown:\n", fp);
429         }
430       log_info ("not an OpenPGP card\n");
431       agent_release_card_info (&info);
432       xfree (pk);
433       return;
434     }
435
436   if (!serialno)
437     ;
438   else if (strlen (serialno)+1 > serialnobuflen)
439     log_error ("serial number longer than expected\n");
440   else
441     strcpy (serialno, info.serialno);
442
443   if (opt.with_colons)
444     es_fputs ("openpgp-card:\n", fp);
445
446
447   if (opt.with_colons)
448     {
449       es_fprintf (fp, "version:%.4s:\n", info.serialno+12);
450       uval = xtoi_2(info.serialno+16)*256 + xtoi_2 (info.serialno+18);
451       es_fprintf (fp, "vendor:%04x:%s:\n", uval, get_manufacturer (uval));
452       es_fprintf (fp, "serial:%.8s:\n", info.serialno+20);
453
454       print_isoname (fp, "Name of cardholder: ", "name", info.disp_name);
455
456       es_fputs ("lang:", fp);
457       if (info.disp_lang)
458         es_write_sanitized (fp, info.disp_lang, strlen (info.disp_lang),
459                             ":", NULL);
460       es_fputs (":\n", fp);
461
462       es_fprintf (fp, "sex:%c:\n", (info.disp_sex == 1? 'm':
463                                  info.disp_sex == 2? 'f' : 'u'));
464
465       es_fputs ("url:", fp);
466       if (info.pubkey_url)
467         es_write_sanitized (fp, info.pubkey_url, strlen (info.pubkey_url),
468                             ":", NULL);
469       es_fputs (":\n", fp);
470
471       es_fputs ("login:", fp);
472       if (info.login_data)
473         es_write_sanitized (fp, info.login_data, strlen (info.login_data),
474                             ":", NULL);
475       es_fputs (":\n", fp);
476
477       es_fprintf (fp, "forcepin:%d:::\n", !info.chv1_cached);
478       for (i=0; i < DIM (info.key_attr); i++)
479         if (info.key_attr[i].algo == PUBKEY_ALGO_RSA)
480           es_fprintf (fp, "keyattr:%d:%d:%u:\n", i+1,
481                       info.key_attr[i].algo, info.key_attr[i].nbits);
482         else if (info.key_attr[i].algo == PUBKEY_ALGO_ECDH
483                  || info.key_attr[i].algo == PUBKEY_ALGO_ECDSA
484                  || info.key_attr[i].algo == PUBKEY_ALGO_EDDSA)
485           es_fprintf (fp, "keyattr:%d:%d:%s:\n", i+1,
486                       info.key_attr[i].algo, info.key_attr[i].curve);
487       es_fprintf (fp, "maxpinlen:%d:%d:%d:\n",
488                   info.chvmaxlen[0], info.chvmaxlen[1], info.chvmaxlen[2]);
489       es_fprintf (fp, "pinretry:%d:%d:%d:\n",
490                   info.chvretry[0], info.chvretry[1], info.chvretry[2]);
491       es_fprintf (fp, "sigcount:%lu:::\n", info.sig_counter);
492
493       for (i=0; i < 4; i++)
494         {
495           if (info.private_do[i])
496             {
497               es_fprintf (fp, "private_do:%d:", i+1);
498               es_write_sanitized (fp, info.private_do[i],
499                                   strlen (info.private_do[i]), ":", NULL);
500               es_fputs (":\n", fp);
501             }
502         }
503
504       es_fputs ("cafpr:", fp);
505       print_sha1_fpr_colon (fp, info.cafpr1valid? info.cafpr1:NULL);
506       print_sha1_fpr_colon (fp, info.cafpr2valid? info.cafpr2:NULL);
507       print_sha1_fpr_colon (fp, info.cafpr3valid? info.cafpr3:NULL);
508       es_putc ('\n', fp);
509       es_fputs ("fpr:", fp);
510       print_sha1_fpr_colon (fp, info.fpr1valid? info.fpr1:NULL);
511       print_sha1_fpr_colon (fp, info.fpr2valid? info.fpr2:NULL);
512       print_sha1_fpr_colon (fp, info.fpr3valid? info.fpr3:NULL);
513       es_putc ('\n', fp);
514       es_fprintf (fp, "fprtime:%lu:%lu:%lu:\n",
515                (unsigned long)info.fpr1time, (unsigned long)info.fpr2time,
516                (unsigned long)info.fpr3time);
517     }
518   else
519     {
520       tty_fprintf (fp, "Version ..........: %.1s%c.%.1s%c\n",
521                    info.serialno[12] == '0'?"":info.serialno+12,
522                    info.serialno[13],
523                    info.serialno[14] == '0'?"":info.serialno+14,
524                    info.serialno[15]);
525       tty_fprintf (fp, "Manufacturer .....: %s\n",
526                    get_manufacturer (xtoi_2(info.serialno+16)*256
527                                      + xtoi_2 (info.serialno+18)));
528       tty_fprintf (fp, "Serial number ....: %.8s\n", info.serialno+20);
529
530       print_isoname (fp, "Name of cardholder: ", "name", info.disp_name);
531       print_name (fp, "Language prefs ...: ", info.disp_lang);
532       tty_fprintf (fp,    "Sex ..............: %s\n",
533                    info.disp_sex == 1? _("male"):
534                    info.disp_sex == 2? _("female") : _("unspecified"));
535       print_name (fp, "URL of public key : ", info.pubkey_url);
536       print_name (fp, "Login data .......: ", info.login_data);
537       if (info.private_do[0])
538         print_name (fp, "Private DO 1 .....: ", info.private_do[0]);
539       if (info.private_do[1])
540         print_name (fp, "Private DO 2 .....: ", info.private_do[1]);
541       if (info.private_do[2])
542         print_name (fp, "Private DO 3 .....: ", info.private_do[2]);
543       if (info.private_do[3])
544         print_name (fp, "Private DO 4 .....: ", info.private_do[3]);
545       if (info.cafpr1valid)
546         {
547           tty_fprintf (fp, "CA fingerprint %d .:", 1);
548           print_sha1_fpr (fp, info.cafpr1);
549         }
550       if (info.cafpr2valid)
551         {
552           tty_fprintf (fp, "CA fingerprint %d .:", 2);
553           print_sha1_fpr (fp, info.cafpr2);
554         }
555       if (info.cafpr3valid)
556         {
557           tty_fprintf (fp, "CA fingerprint %d .:", 3);
558           print_sha1_fpr (fp, info.cafpr3);
559         }
560       tty_fprintf (fp,    "Signature PIN ....: %s\n",
561                    info.chv1_cached? _("not forced"): _("forced"));
562       if (info.key_attr[0].algo)
563         {
564           tty_fprintf (fp,    "Key attributes ...:");
565           for (i=0; i < DIM (info.key_attr); i++)
566             if (info.key_attr[i].algo == PUBKEY_ALGO_RSA)
567               tty_fprintf (fp, " rsa%u", info.key_attr[i].nbits);
568             else if (info.key_attr[i].algo == PUBKEY_ALGO_ECDH
569                      || info.key_attr[i].algo == PUBKEY_ALGO_ECDSA
570                      || info.key_attr[i].algo == PUBKEY_ALGO_EDDSA)
571               {
572                 const char *curve_for_print = "?";
573
574                 if (info.key_attr[i].curve)
575                   {
576                     const char *oid;
577                     oid = openpgp_curve_to_oid (info.key_attr[i].curve, NULL);
578                     if (oid)
579                       curve_for_print = openpgp_oid_to_curve (oid, 0);
580                   }
581                 tty_fprintf (fp, " %s", curve_for_print);
582               }
583           tty_fprintf (fp, "\n");
584         }
585       tty_fprintf (fp,    "Max. PIN lengths .: %d %d %d\n",
586                    info.chvmaxlen[0], info.chvmaxlen[1], info.chvmaxlen[2]);
587       tty_fprintf (fp,    "PIN retry counter : %d %d %d\n",
588                    info.chvretry[0], info.chvretry[1], info.chvretry[2]);
589       tty_fprintf (fp,    "Signature counter : %lu\n", info.sig_counter);
590       tty_fprintf (fp, "Signature key ....:");
591       print_sha1_fpr (fp, info.fpr1valid? info.fpr1:NULL);
592       if (info.fpr1valid && info.fpr1time)
593         tty_fprintf (fp, "      created ....: %s\n",
594                      isotimestamp (info.fpr1time));
595       tty_fprintf (fp, "Encryption key....:");
596       print_sha1_fpr (fp, info.fpr2valid? info.fpr2:NULL);
597       if (info.fpr2valid && info.fpr2time)
598         tty_fprintf (fp, "      created ....: %s\n",
599                      isotimestamp (info.fpr2time));
600       tty_fprintf (fp, "Authentication key:");
601       print_sha1_fpr (fp, info.fpr3valid? info.fpr3:NULL);
602       if (info.fpr3valid && info.fpr3time)
603         tty_fprintf (fp, "      created ....: %s\n",
604                      isotimestamp (info.fpr3time));
605       tty_fprintf (fp, "General key info..: ");
606
607       thefpr = (info.fpr1valid? info.fpr1 : info.fpr2valid? info.fpr2 :
608                 info.fpr3valid? info.fpr3 : NULL);
609       /* If the fingerprint is all 0xff, the key has no asssociated
610          OpenPGP certificate.  */
611       if ( thefpr && !fpr_is_ff (thefpr)
612            && !get_pubkey_byfprint (pk, &keyblock, thefpr, 20))
613         {
614           print_pubkey_info (fp, pk);
615           if (keyblock)
616             print_card_key_info (fp, keyblock);
617         }
618       else
619         tty_fprintf (fp, "[none]\n");
620     }
621
622   release_kbnode (keyblock);
623   free_public_key (pk);
624   agent_release_card_info (&info);
625 }
626
627
628 static char *
629 get_one_name (const char *prompt1, const char *prompt2)
630 {
631   char *name;
632   int i;
633
634   for (;;)
635     {
636       name = cpr_get (prompt1, prompt2);
637       if (!name)
638         return NULL;
639       trim_spaces (name);
640       cpr_kill_prompt ();
641       for (i=0; name[i] && name[i] >= ' ' && name[i] <= 126; i++)
642         ;
643
644       /* The name must be in Latin-1 and not UTF-8 - lacking the code
645          to ensure this we restrict it to ASCII. */
646       if (name[i])
647         tty_printf (_("Error: Only plain ASCII is currently allowed.\n"));
648       else if (strchr (name, '<'))
649         tty_printf (_("Error: The \"<\" character may not be used.\n"));
650       else if (strstr (name, "  "))
651         tty_printf (_("Error: Double spaces are not allowed.\n"));
652       else
653         return name;
654       xfree (name);
655     }
656 }
657
658
659
660 static int
661 change_name (void)
662 {
663   char *surname = NULL, *givenname = NULL;
664   char *isoname, *p;
665   int rc;
666
667   surname = get_one_name ("keygen.smartcard.surname",
668                                     _("Cardholder's surname: "));
669   givenname = get_one_name ("keygen.smartcard.givenname",
670                                        _("Cardholder's given name: "));
671   if (!surname || !givenname || (!*surname && !*givenname))
672     {
673       xfree (surname);
674       xfree (givenname);
675       return -1; /*canceled*/
676     }
677
678   isoname = xmalloc ( strlen (surname) + 2 + strlen (givenname) + 1);
679   strcpy (stpcpy (stpcpy (isoname, surname), "<<"), givenname);
680   xfree (surname);
681   xfree (givenname);
682   for (p=isoname; *p; p++)
683     if (*p == ' ')
684       *p = '<';
685
686   if (strlen (isoname) > 39 )
687     {
688       tty_printf (_("Error: Combined name too long "
689                     "(limit is %d characters).\n"), 39);
690       xfree (isoname);
691       return -1;
692     }
693
694   rc = agent_scd_setattr ("DISP-NAME", isoname, strlen (isoname), NULL );
695   if (rc)
696     log_error ("error setting Name: %s\n", gpg_strerror (rc));
697
698   xfree (isoname);
699   return rc;
700 }
701
702
703 static int
704 change_url (void)
705 {
706   char *url;
707   int rc;
708
709   url = cpr_get ("cardedit.change_url", _("URL to retrieve public key: "));
710   if (!url)
711     return -1;
712   trim_spaces (url);
713   cpr_kill_prompt ();
714
715   if (strlen (url) > 254 )
716     {
717       tty_printf (_("Error: URL too long "
718                     "(limit is %d characters).\n"), 254);
719       xfree (url);
720       return -1;
721     }
722
723   rc = agent_scd_setattr ("PUBKEY-URL", url, strlen (url), NULL );
724   if (rc)
725     log_error ("error setting URL: %s\n", gpg_strerror (rc));
726   xfree (url);
727   write_sc_op_status (rc);
728   return rc;
729 }
730
731
732 /* Fetch the key from the URL given on the card or try to get it from
733    the default keyserver.  */
734 static int
735 fetch_url (ctrl_t ctrl)
736 {
737   int rc;
738   struct agent_card_info_s info;
739
740   memset(&info,0,sizeof(info));
741
742   rc=agent_scd_getattr("PUBKEY-URL",&info);
743   if(rc)
744     log_error("error retrieving URL from card: %s\n",gpg_strerror(rc));
745   else
746     {
747       rc=agent_scd_getattr("KEY-FPR",&info);
748       if(rc)
749         log_error("error retrieving key fingerprint from card: %s\n",
750                   gpg_strerror(rc));
751       else if (info.pubkey_url && *info.pubkey_url)
752         {
753           strlist_t sl = NULL;
754
755           add_to_strlist (&sl, info.pubkey_url);
756           rc = keyserver_fetch (ctrl, sl);
757           free_strlist (sl);
758         }
759       else if (info.fpr1valid)
760         {
761           rc = keyserver_import_fprint (ctrl, info.fpr1, 20, opt.keyserver, 0);
762         }
763     }
764
765   return rc;
766 }
767
768
769 /* Read data from file FNAME up to MAXLEN characters.  On error return
770    -1 and store NULL at R_BUFFER; on success return the number of
771    bytes read and store the address of a newly allocated buffer at
772    R_BUFFER. */
773 static int
774 get_data_from_file (const char *fname, size_t maxlen, char **r_buffer)
775 {
776   estream_t fp;
777   char *data;
778   int n;
779
780   *r_buffer = NULL;
781
782   fp = es_fopen (fname, "rb");
783 #if GNUPG_MAJOR_VERSION == 1
784   if (fp && is_secured_file (fileno (fp)))
785     {
786       fclose (fp);
787       fp = NULL;
788       errno = EPERM;
789     }
790 #endif
791   if (!fp)
792     {
793       tty_printf (_("can't open '%s': %s\n"), fname, strerror (errno));
794       return -1;
795     }
796
797   data = xtrymalloc (maxlen? maxlen:1);
798   if (!data)
799     {
800       tty_printf (_("error allocating enough memory: %s\n"), strerror (errno));
801       es_fclose (fp);
802       return -1;
803     }
804
805   if (maxlen)
806     n = es_fread (data, 1, maxlen, fp);
807   else
808     n = 0;
809   es_fclose (fp);
810   if (n < 0)
811     {
812       tty_printf (_("error reading '%s': %s\n"), fname, strerror (errno));
813       xfree (data);
814       return -1;
815     }
816   *r_buffer = data;
817   return n;
818 }
819
820
821 /* Write LENGTH bytes from BUFFER to file FNAME.  Return 0 on
822    success.  */
823 static int
824 put_data_to_file (const char *fname, const void *buffer, size_t length)
825 {
826   estream_t fp;
827
828   fp = es_fopen (fname, "wb");
829 #if GNUPG_MAJOR_VERSION == 1
830   if (fp && is_secured_file (fileno (fp)))
831     {
832       fclose (fp);
833       fp = NULL;
834       errno = EPERM;
835     }
836 #endif
837   if (!fp)
838     {
839       tty_printf (_("can't create '%s': %s\n"), fname, strerror (errno));
840       return -1;
841     }
842
843   if (length && es_fwrite (buffer, length, 1, fp) != 1)
844     {
845       tty_printf (_("error writing '%s': %s\n"), fname, strerror (errno));
846       es_fclose (fp);
847       return -1;
848     }
849   es_fclose (fp);
850   return 0;
851 }
852
853
854 static int
855 change_login (const char *args)
856 {
857   char *data;
858   int n;
859   int rc;
860
861   if (args && *args == '<')  /* Read it from a file */
862     {
863       for (args++; spacep (args); args++)
864         ;
865       n = get_data_from_file (args, 254, &data);
866       if (n < 0)
867         return -1;
868     }
869   else
870     {
871       data = cpr_get ("cardedit.change_login",
872                       _("Login data (account name): "));
873       if (!data)
874         return -1;
875       trim_spaces (data);
876       cpr_kill_prompt ();
877       n = strlen (data);
878     }
879
880   if (n > 254 )
881     {
882       tty_printf (_("Error: Login data too long "
883                     "(limit is %d characters).\n"), 254);
884       xfree (data);
885       return -1;
886     }
887
888   rc = agent_scd_setattr ("LOGIN-DATA", data, n, NULL );
889   if (rc)
890     log_error ("error setting login data: %s\n", gpg_strerror (rc));
891   xfree (data);
892   write_sc_op_status (rc);
893   return rc;
894 }
895
896 static int
897 change_private_do (const char *args, int nr)
898 {
899   char do_name[] = "PRIVATE-DO-X";
900   char *data;
901   int n;
902   int rc;
903
904   log_assert (nr >= 1 && nr <= 4);
905   do_name[11] = '0' + nr;
906
907   if (args && (args = strchr (args, '<')))  /* Read it from a file */
908     {
909       for (args++; spacep (args); args++)
910         ;
911       n = get_data_from_file (args, 254, &data);
912       if (n < 0)
913         return -1;
914     }
915   else
916     {
917       data = cpr_get ("cardedit.change_private_do",
918                       _("Private DO data: "));
919       if (!data)
920         return -1;
921       trim_spaces (data);
922       cpr_kill_prompt ();
923       n = strlen (data);
924     }
925
926   if (n > 254 )
927     {
928       tty_printf (_("Error: Private DO too long "
929                     "(limit is %d characters).\n"), 254);
930       xfree (data);
931       return -1;
932     }
933
934   rc = agent_scd_setattr (do_name, data, n, NULL );
935   if (rc)
936     log_error ("error setting private DO: %s\n", gpg_strerror (rc));
937   xfree (data);
938   write_sc_op_status (rc);
939   return rc;
940 }
941
942
943 static int
944 change_cert (const char *args)
945 {
946   char *data;
947   int n;
948   int rc;
949
950   if (args && *args == '<')  /* Read it from a file */
951     {
952       for (args++; spacep (args); args++)
953         ;
954       n = get_data_from_file (args, 16384, &data);
955       if (n < 0)
956         return -1;
957     }
958   else
959     {
960       tty_printf ("usage error: redirection to file required\n");
961       return -1;
962     }
963
964   rc = agent_scd_writecert ("OPENPGP.3", data, n);
965   if (rc)
966     log_error ("error writing certificate to card: %s\n", gpg_strerror (rc));
967   xfree (data);
968   write_sc_op_status (rc);
969   return rc;
970 }
971
972
973 static int
974 read_cert (const char *args)
975 {
976   const char *fname;
977   void *buffer;
978   size_t length;
979   int rc;
980
981   if (args && *args == '>')  /* Write it to a file */
982     {
983       for (args++; spacep (args); args++)
984         ;
985       fname = args;
986     }
987   else
988     {
989       tty_printf ("usage error: redirection to file required\n");
990       return -1;
991     }
992
993   rc = agent_scd_readcert ("OPENPGP.3", &buffer, &length);
994   if (rc)
995     log_error ("error reading certificate from card: %s\n", gpg_strerror (rc));
996   else
997     rc = put_data_to_file (fname, buffer, length);
998   xfree (buffer);
999   write_sc_op_status (rc);
1000   return rc;
1001 }
1002
1003
1004 static int
1005 change_lang (void)
1006 {
1007   char *data, *p;
1008   int rc;
1009
1010   data = cpr_get ("cardedit.change_lang",
1011                   _("Language preferences: "));
1012   if (!data)
1013     return -1;
1014   trim_spaces (data);
1015   cpr_kill_prompt ();
1016
1017   if (strlen (data) > 8 || (strlen (data) & 1))
1018     {
1019       tty_printf (_("Error: invalid length of preference string.\n"));
1020       xfree (data);
1021       return -1;
1022     }
1023
1024   for (p=data; *p && *p >= 'a' && *p <= 'z'; p++)
1025     ;
1026   if (*p)
1027     {
1028       tty_printf (_("Error: invalid characters in preference string.\n"));
1029       xfree (data);
1030       return -1;
1031     }
1032
1033   rc = agent_scd_setattr ("DISP-LANG", data, strlen (data), NULL );
1034   if (rc)
1035     log_error ("error setting lang: %s\n", gpg_strerror (rc));
1036   xfree (data);
1037   write_sc_op_status (rc);
1038   return rc;
1039 }
1040
1041
1042 static int
1043 change_sex (void)
1044 {
1045   char *data;
1046   const char *str;
1047   int rc;
1048
1049   data = cpr_get ("cardedit.change_sex",
1050                   _("Sex ((M)ale, (F)emale or space): "));
1051   if (!data)
1052     return -1;
1053   trim_spaces (data);
1054   cpr_kill_prompt ();
1055
1056   if (!*data)
1057     str = "9";
1058   else if ((*data == 'M' || *data == 'm') && !data[1])
1059     str = "1";
1060   else if ((*data == 'F' || *data == 'f') && !data[1])
1061     str = "2";
1062   else
1063     {
1064       tty_printf (_("Error: invalid response.\n"));
1065       xfree (data);
1066       return -1;
1067     }
1068
1069   rc = agent_scd_setattr ("DISP-SEX", str, 1, NULL );
1070   if (rc)
1071     log_error ("error setting sex: %s\n", gpg_strerror (rc));
1072   xfree (data);
1073   write_sc_op_status (rc);
1074   return rc;
1075 }
1076
1077
1078 static int
1079 change_cafpr (int fprno)
1080 {
1081   char *data;
1082   const char *s;
1083   int i, c, rc;
1084   unsigned char fpr[20];
1085
1086   data = cpr_get ("cardedit.change_cafpr", _("CA fingerprint: "));
1087   if (!data)
1088     return -1;
1089   trim_spaces (data);
1090   cpr_kill_prompt ();
1091
1092   for (i=0, s=data; i < 20 && *s; )
1093     {
1094       while (spacep(s))
1095         s++;
1096       if (*s == ':')
1097         s++;
1098       while (spacep(s))
1099         s++;
1100       c = hextobyte (s);
1101       if (c == -1)
1102         break;
1103       fpr[i++] = c;
1104       s += 2;
1105     }
1106   xfree (data);
1107   if (i != 20 || *s)
1108     {
1109       tty_printf (_("Error: invalid formatted fingerprint.\n"));
1110       return -1;
1111     }
1112
1113   rc = agent_scd_setattr (fprno==1?"CA-FPR-1":
1114                           fprno==2?"CA-FPR-2":
1115                           fprno==3?"CA-FPR-3":"x", fpr, 20, NULL );
1116   if (rc)
1117     log_error ("error setting cafpr: %s\n", gpg_strerror (rc));
1118   write_sc_op_status (rc);
1119   return rc;
1120 }
1121
1122
1123
1124 static void
1125 toggle_forcesig (void)
1126 {
1127   struct agent_card_info_s info;
1128   int rc;
1129   int newstate;
1130
1131   memset (&info, 0, sizeof info);
1132   rc = agent_scd_getattr ("CHV-STATUS", &info);
1133   if (rc)
1134     {
1135       log_error ("error getting current status: %s\n", gpg_strerror (rc));
1136       return;
1137     }
1138   newstate = !info.chv1_cached;
1139   agent_release_card_info (&info);
1140
1141   rc = agent_scd_setattr ("CHV-STATUS-1", newstate? "\x01":"", 1, NULL);
1142   if (rc)
1143     log_error ("error toggling signature PIN flag: %s\n", gpg_strerror (rc));
1144   write_sc_op_status (rc);
1145 }
1146
1147
1148 /* Helper for the key generation/edit functions.  */
1149 static int
1150 get_info_for_key_operation (struct agent_card_info_s *info)
1151 {
1152   int rc;
1153
1154   memset (info, 0, sizeof *info);
1155   rc = agent_scd_getattr ("SERIALNO", info);
1156   if (rc || !info->serialno || strncmp (info->serialno, "D27600012401", 12)
1157       || strlen (info->serialno) != 32 )
1158     {
1159       log_error (_("key operation not possible: %s\n"),
1160                  rc ? gpg_strerror (rc) : _("not an OpenPGP card"));
1161       return rc? rc: -1;
1162     }
1163   rc = agent_scd_getattr ("KEY-FPR", info);
1164   if (!rc)
1165     rc = agent_scd_getattr ("CHV-STATUS", info);
1166   if (!rc)
1167     rc = agent_scd_getattr ("DISP-NAME", info);
1168   if (!rc)
1169     rc = agent_scd_getattr ("EXTCAP", info);
1170   if (!rc)
1171     rc = agent_scd_getattr ("KEY-ATTR", info);
1172   if (rc)
1173     log_error (_("error getting current key info: %s\n"), gpg_strerror (rc));
1174   return rc;
1175 }
1176
1177
1178 /* Helper for the key generation/edit functions.  */
1179 static int
1180 check_pin_for_key_operation (struct agent_card_info_s *info, int *forced_chv1)
1181 {
1182   int rc = 0;
1183
1184   agent_clear_pin_cache (info->serialno);
1185
1186   *forced_chv1 = !info->chv1_cached;
1187   if (*forced_chv1)
1188     { /* Switch off the forced mode so that during key generation we
1189          don't get bothered with PIN queries for each
1190          self-signature. */
1191       rc = agent_scd_setattr ("CHV-STATUS-1", "\x01", 1, info->serialno);
1192       if (rc)
1193         {
1194           log_error ("error clearing forced signature PIN flag: %s\n",
1195                      gpg_strerror (rc));
1196           *forced_chv1 = 0;
1197         }
1198     }
1199
1200   if (!rc)
1201     {
1202       /* Check the PIN now, so that we won't get asked later for each
1203          binding signature. */
1204       rc = agent_scd_checkpin (info->serialno);
1205       if (rc)
1206         {
1207           log_error ("error checking the PIN: %s\n", gpg_strerror (rc));
1208           write_sc_op_status (rc);
1209         }
1210   }
1211   return rc;
1212 }
1213
1214 /* Helper for the key generation/edit functions.  */
1215 static void
1216 restore_forced_chv1 (int *forced_chv1)
1217 {
1218   int rc;
1219
1220   if (*forced_chv1)
1221     { /* Switch back to forced state. */
1222       rc = agent_scd_setattr ("CHV-STATUS-1", "", 1, NULL);
1223       if (rc)
1224         {
1225           log_error ("error setting forced signature PIN flag: %s\n",
1226                      gpg_strerror (rc));
1227         }
1228     }
1229 }
1230
1231
1232 /* Helper for the key generation/edit functions.  */
1233 static void
1234 show_card_key_info (struct agent_card_info_s *info)
1235 {
1236   tty_fprintf (NULL, "Signature key ....:");
1237   print_sha1_fpr (NULL, info->fpr1valid? info->fpr1:NULL);
1238   tty_fprintf (NULL, "Encryption key....:");
1239   print_sha1_fpr (NULL, info->fpr2valid? info->fpr2:NULL);
1240   tty_fprintf (NULL, "Authentication key:");
1241   print_sha1_fpr (NULL, info->fpr3valid? info->fpr3:NULL);
1242   tty_printf ("\n");
1243 }
1244
1245
1246 /* Helper for the key generation/edit functions.  */
1247 static int
1248 replace_existing_key_p (struct agent_card_info_s *info, int keyno)
1249 {
1250   log_assert (keyno >= 0 && keyno <= 3);
1251
1252   if ((keyno == 1 && info->fpr1valid)
1253       || (keyno == 2 && info->fpr2valid)
1254       || (keyno == 3 && info->fpr3valid))
1255     {
1256       tty_printf ("\n");
1257       log_info ("WARNING: such a key has already been stored on the card!\n");
1258       tty_printf ("\n");
1259       if ( !cpr_get_answer_is_yes( "cardedit.genkeys.replace_key",
1260                                   _("Replace existing key? (y/N) ")))
1261         return -1;
1262       return 1;
1263     }
1264   return 0;
1265 }
1266
1267
1268 static void
1269 show_keysize_warning (void)
1270 {
1271   static int shown;
1272
1273   if (shown)
1274     return;
1275   shown = 1;
1276   tty_printf
1277     (_("Note: There is no guarantee that the card "
1278        "supports the requested size.\n"
1279        "      If the key generation does not succeed, "
1280        "please check the\n"
1281        "      documentation of your card to see what "
1282        "sizes are allowed.\n"));
1283 }
1284
1285
1286 /* Ask for the size of a card key.  NBITS is the current size
1287    configured for the card.  KEYNO is the number of the key used to
1288    select the prompt.  Returns 0 to use the default size (i.e. NBITS)
1289    or the selected size.  */
1290 static unsigned int
1291 ask_card_rsa_keysize (int keyno, unsigned int nbits)
1292 {
1293   unsigned int min_nbits = 1024;
1294   unsigned int max_nbits = 4096;
1295   char *prompt, *answer;
1296   unsigned int req_nbits;
1297
1298   for (;;)
1299     {
1300       prompt = xasprintf
1301         (keyno == 0?
1302          _("What keysize do you want for the Signature key? (%u) "):
1303          keyno == 1?
1304          _("What keysize do you want for the Encryption key? (%u) "):
1305          _("What keysize do you want for the Authentication key? (%u) "),
1306          nbits);
1307       answer = cpr_get ("cardedit.genkeys.size", prompt);
1308       cpr_kill_prompt ();
1309       req_nbits = *answer? atoi (answer): nbits;
1310       xfree (prompt);
1311       xfree (answer);
1312
1313       if (req_nbits != nbits && (req_nbits % 32) )
1314         {
1315           req_nbits = ((req_nbits + 31) / 32) * 32;
1316           tty_printf (_("rounded up to %u bits\n"), req_nbits);
1317         }
1318
1319       if (req_nbits == nbits)
1320         return 0;  /* Use default.  */
1321
1322       if (req_nbits < min_nbits || req_nbits > max_nbits)
1323         {
1324           tty_printf (_("%s keysizes must be in the range %u-%u\n"),
1325                       "RSA", min_nbits, max_nbits);
1326         }
1327       else
1328         {
1329           tty_printf (_("The card will now be re-configured "
1330                         "to generate a key of %u bits\n"), req_nbits);
1331           show_keysize_warning ();
1332           return req_nbits;
1333         }
1334     }
1335 }
1336
1337
1338 /* Change the size of key KEYNO (0..2) to NBITS and show an error
1339    message if that fails.  */
1340 static gpg_error_t
1341 do_change_rsa_keysize (int keyno, unsigned int nbits)
1342 {
1343   gpg_error_t err;
1344   char args[100];
1345
1346   snprintf (args, sizeof args, "--force %d 1 rsa%u", keyno+1, nbits);
1347   err = agent_scd_setattr ("KEY-ATTR", args, strlen (args), NULL);
1348   if (err)
1349     log_error (_("error changing size of key %d to %u bits: %s\n"),
1350                keyno+1, nbits, gpg_strerror (err));
1351   return err;
1352 }
1353
1354
1355 static void
1356 generate_card_keys (ctrl_t ctrl)
1357 {
1358   struct agent_card_info_s info;
1359   int forced_chv1;
1360   int want_backup;
1361   int keyno;
1362
1363   if (get_info_for_key_operation (&info))
1364     return;
1365
1366   if (info.extcap.ki)
1367     {
1368       char *answer;
1369
1370       /* FIXME: Should be something like cpr_get_bool so that a status
1371          GET_BOOL will be emitted.  */
1372       answer = cpr_get ("cardedit.genkeys.backup_enc",
1373                         _("Make off-card backup of encryption key? (Y/n) "));
1374
1375       want_backup = answer_is_yes_no_default (answer, 1/*(default to Yes)*/);
1376       cpr_kill_prompt ();
1377       xfree (answer);
1378     }
1379   else
1380     want_backup = 0;
1381
1382   if ( (info.fpr1valid && !fpr_is_zero (info.fpr1))
1383        || (info.fpr2valid && !fpr_is_zero (info.fpr2))
1384        || (info.fpr3valid && !fpr_is_zero (info.fpr3)))
1385     {
1386       tty_printf ("\n");
1387       log_info (_("Note: keys are already stored on the card!\n"));
1388       tty_printf ("\n");
1389       if ( !cpr_get_answer_is_yes ("cardedit.genkeys.replace_keys",
1390                                    _("Replace existing keys? (y/N) ")))
1391         {
1392           agent_release_card_info (&info);
1393           return;
1394         }
1395     }
1396
1397   /* If no displayed name has been set, we assume that this is a fresh
1398      card and print a hint about the default PINs.  */
1399   if (!info.disp_name || !*info.disp_name)
1400     {
1401       tty_printf ("\n");
1402       tty_printf (_("Please note that the factory settings of the PINs are\n"
1403                     "   PIN = '%s'     Admin PIN = '%s'\n"
1404                     "You should change them using the command --change-pin\n"),
1405                   "123456", "12345678");
1406       tty_printf ("\n");
1407     }
1408
1409   if (check_pin_for_key_operation (&info, &forced_chv1))
1410     goto leave;
1411
1412   /* If the cards features changeable key attributes, we ask for the
1413      key size.  */
1414   if (info.is_v2 && info.extcap.aac)
1415     {
1416       unsigned int nbits;
1417
1418       for (keyno = 0; keyno < DIM (info.key_attr); keyno++)
1419         {
1420           if (info.key_attr[keyno].algo == PUBKEY_ALGO_RSA)
1421             {
1422               nbits = ask_card_rsa_keysize (keyno, info.key_attr[keyno].nbits);
1423               if (nbits && do_change_rsa_keysize (keyno, nbits))
1424                 {
1425                   /* Error: Better read the default key size again.  */
1426                   agent_release_card_info (&info);
1427                   if (get_info_for_key_operation (&info))
1428                     goto leave;
1429                   /* Ask again for this key size. */
1430                   keyno--;
1431                 }
1432             }
1433         }
1434       /* Note that INFO has not be synced.  However we will only use
1435          the serialnumber and thus it won't harm.  */
1436     }
1437
1438   generate_keypair (ctrl, 1, NULL, info.serialno, want_backup);
1439
1440  leave:
1441   agent_release_card_info (&info);
1442   restore_forced_chv1 (&forced_chv1);
1443 }
1444
1445
1446 /* This function is used by the key edit menu to generate an arbitrary
1447    subkey. */
1448 gpg_error_t
1449 card_generate_subkey (KBNODE pub_keyblock)
1450 {
1451   gpg_error_t err;
1452   struct agent_card_info_s info;
1453   int forced_chv1 = 0;
1454   int keyno;
1455
1456   err = get_info_for_key_operation (&info);
1457   if (err)
1458     return err;
1459
1460   show_card_key_info (&info);
1461
1462   tty_printf (_("Please select the type of key to generate:\n"));
1463
1464   tty_printf (_("   (1) Signature key\n"));
1465   tty_printf (_("   (2) Encryption key\n"));
1466   tty_printf (_("   (3) Authentication key\n"));
1467
1468   for (;;)
1469     {
1470       char *answer = cpr_get ("cardedit.genkeys.subkeytype",
1471                               _("Your selection? "));
1472       cpr_kill_prompt();
1473       if (*answer == CONTROL_D)
1474         {
1475           xfree (answer);
1476           err = gpg_error (GPG_ERR_CANCELED);
1477           goto leave;
1478         }
1479       keyno = *answer? atoi(answer): 0;
1480       xfree(answer);
1481       if (keyno >= 1 && keyno <= 3)
1482         break; /* Okay. */
1483       tty_printf(_("Invalid selection.\n"));
1484     }
1485
1486   if (replace_existing_key_p (&info, keyno) < 0)
1487     {
1488       err = gpg_error (GPG_ERR_CANCELED);
1489       goto leave;
1490     }
1491
1492   err = check_pin_for_key_operation (&info, &forced_chv1);
1493   if (err)
1494     goto leave;
1495
1496   /* If the cards features changeable key attributes, we ask for the
1497      key size.  */
1498   if (info.is_v2 && info.extcap.aac)
1499     {
1500       if (info.key_attr[keyno-1].algo == PUBKEY_ALGO_RSA)
1501         {
1502           unsigned int nbits;
1503
1504         ask_again:
1505           nbits = ask_card_rsa_keysize (keyno-1, info.key_attr[keyno-1].nbits);
1506           if (nbits && do_change_rsa_keysize (keyno-1, nbits))
1507             {
1508               /* Error: Better read the default key size again.  */
1509               agent_release_card_info (&info);
1510               err = get_info_for_key_operation (&info);
1511               if (err)
1512                 goto leave;
1513               goto ask_again;
1514             }
1515         }
1516       /* Note that INFO has not be synced.  However we will only use
1517          the serialnumber and thus it won't harm.  */
1518     }
1519
1520   err = generate_card_subkeypair (pub_keyblock, keyno, info.serialno);
1521
1522  leave:
1523   agent_release_card_info (&info);
1524   restore_forced_chv1 (&forced_chv1);
1525   return err;
1526 }
1527
1528
1529 /* Store the key at NODE into the smartcard and modify NODE to
1530    carry the serialno stuff instead of the actual secret key
1531    parameters.  USE is the usage for that key; 0 means any
1532    usage. */
1533 int
1534 card_store_subkey (KBNODE node, int use)
1535 {
1536   struct agent_card_info_s info;
1537   int okay = 0;
1538   unsigned int nbits;
1539   int allow_keyno[3];
1540   int  keyno;
1541   PKT_public_key *pk;
1542   gpg_error_t err;
1543   char *hexgrip;
1544   int rc;
1545   gnupg_isotime_t timebuf;
1546
1547   log_assert (node->pkt->pkttype == PKT_PUBLIC_KEY
1548               || node->pkt->pkttype == PKT_PUBLIC_SUBKEY);
1549
1550   pk = node->pkt->pkt.public_key;
1551
1552   if (get_info_for_key_operation (&info))
1553     return 0;
1554
1555   if (!info.extcap.ki)
1556     {
1557       tty_printf ("The card does not support the import of keys\n");
1558       tty_printf ("\n");
1559       goto leave;
1560     }
1561
1562   nbits = nbits_from_pk (pk);
1563
1564   if (!info.is_v2 && nbits != 1024)
1565     {
1566       tty_printf ("You may only store a 1024 bit RSA key on the card\n");
1567       tty_printf ("\n");
1568       goto leave;
1569     }
1570
1571   allow_keyno[0] = (!use || (use & (PUBKEY_USAGE_SIG|PUBKEY_USAGE_CERT)));
1572   allow_keyno[1] = (!use || (use & (PUBKEY_USAGE_ENC)));
1573   allow_keyno[2] = (!use || (use & (PUBKEY_USAGE_SIG|PUBKEY_USAGE_AUTH)));
1574
1575   tty_printf (_("Please select where to store the key:\n"));
1576
1577   if (allow_keyno[0])
1578     tty_printf (_("   (1) Signature key\n"));
1579   if (allow_keyno[1])
1580     tty_printf (_("   (2) Encryption key\n"));
1581   if (allow_keyno[2])
1582     tty_printf (_("   (3) Authentication key\n"));
1583
1584   for (;;)
1585     {
1586       char *answer = cpr_get ("cardedit.genkeys.storekeytype",
1587                               _("Your selection? "));
1588       cpr_kill_prompt();
1589       if (*answer == CONTROL_D || !*answer)
1590         {
1591           xfree (answer);
1592           goto leave;
1593         }
1594       keyno = *answer? atoi(answer): 0;
1595       xfree(answer);
1596       if (keyno >= 1 && keyno <= 3 && allow_keyno[keyno-1])
1597         {
1598           if (info.is_v2 && !info.extcap.aac
1599               && info.key_attr[keyno-1].nbits != nbits)
1600             {
1601               tty_printf ("Key does not match the card's capability.\n");
1602             }
1603           else
1604             break; /* Okay. */
1605         }
1606       else
1607         tty_printf(_("Invalid selection.\n"));
1608     }
1609
1610   if ((rc = replace_existing_key_p (&info, keyno)) < 0)
1611     goto leave;
1612
1613   err = hexkeygrip_from_pk (pk, &hexgrip);
1614   if (err)
1615     goto leave;
1616
1617   epoch2isotime (timebuf, (time_t)pk->timestamp);
1618   rc = agent_keytocard (hexgrip, keyno, rc, info.serialno, timebuf);
1619
1620   if (rc)
1621     log_error (_("KEYTOCARD failed: %s\n"), gpg_strerror (rc));
1622   else
1623     okay = 1;
1624   xfree (hexgrip);
1625
1626  leave:
1627   agent_release_card_info (&info);
1628   return okay;
1629 }
1630
1631
1632
1633 /* Direct sending of an hex encoded APDU with error printing.  */
1634 static gpg_error_t
1635 send_apdu (const char *hexapdu, const char *desc, unsigned int ignore)
1636 {
1637   gpg_error_t err;
1638   unsigned int sw;
1639
1640   err = agent_scd_apdu (hexapdu, &sw);
1641   if (err)
1642     tty_printf ("sending card command %s failed: %s\n", desc,
1643                 gpg_strerror (err));
1644   else if (!hexapdu || !strcmp (hexapdu, "undefined"))
1645     ;
1646   else if (ignore == 0xffff)
1647     ; /* Ignore all status words.  */
1648   else if (sw != 0x9000)
1649     {
1650       switch (sw)
1651         {
1652         case 0x6285: err = gpg_error (GPG_ERR_OBJ_TERM_STATE); break;
1653         case 0x6982: err = gpg_error (GPG_ERR_BAD_PIN); break;
1654         case 0x6985: err = gpg_error (GPG_ERR_USE_CONDITIONS); break;
1655         default: err = gpg_error (GPG_ERR_CARD);
1656         }
1657       if (!(ignore && ignore == sw))
1658         tty_printf ("card command %s failed: %s (0x%04x)\n", desc,
1659                     gpg_strerror (err),  sw);
1660     }
1661   return err;
1662 }
1663
1664
1665 /* Do a factory reset after confirmation.  */
1666 static void
1667 factory_reset (void)
1668 {
1669   struct agent_card_info_s info;
1670   gpg_error_t err;
1671   char *answer = NULL;
1672   int termstate = 0;
1673   int i;
1674
1675   /*  The code below basically does the same what this
1676       gpg-connect-agent script does:
1677
1678         scd reset
1679         scd serialno undefined
1680         scd apdu 00 A4 04 00 06 D2 76 00 01 24 01
1681         scd apdu 00 20 00 81 08 40 40 40 40 40 40 40 40
1682         scd apdu 00 20 00 81 08 40 40 40 40 40 40 40 40
1683         scd apdu 00 20 00 81 08 40 40 40 40 40 40 40 40
1684         scd apdu 00 20 00 81 08 40 40 40 40 40 40 40 40
1685         scd apdu 00 20 00 83 08 40 40 40 40 40 40 40 40
1686         scd apdu 00 20 00 83 08 40 40 40 40 40 40 40 40
1687         scd apdu 00 20 00 83 08 40 40 40 40 40 40 40 40
1688         scd apdu 00 20 00 83 08 40 40 40 40 40 40 40 40
1689         scd apdu 00 e6 00 00
1690         scd reset
1691         scd serialno undefined
1692         scd apdu 00 A4 04 00 06 D2 76 00 01 24 01
1693         scd apdu 00 44 00 00
1694         /echo Card has been reset to factory defaults
1695
1696       but tries to find out something about the card first.
1697    */
1698
1699   err = agent_scd_learn (&info, 0);
1700   if (gpg_err_code (err) == GPG_ERR_OBJ_TERM_STATE
1701       && gpg_err_source (err) == GPG_ERR_SOURCE_SCD)
1702     termstate = 1;
1703   else if (err)
1704     {
1705       log_error (_("OpenPGP card not available: %s\n"), gpg_strerror (err));
1706       return;
1707     }
1708
1709   if (!termstate)
1710     {
1711       log_info (_("OpenPGP card no. %s detected\n"),
1712                 info.serialno? info.serialno : "[none]");
1713       if (!(info.status_indicator == 3 || info.status_indicator == 5))
1714         {
1715           /* Note: We won't see status-indicator 3 here because it is not
1716              possible to select a card application in termination state.  */
1717           log_error (_("This command is not supported by this card\n"));
1718           goto leave;
1719         }
1720
1721       tty_printf ("\n");
1722       log_info (_("Note: This command destroys all keys stored on the card!\n"));
1723       tty_printf ("\n");
1724       if (!cpr_get_answer_is_yes ("cardedit.factory-reset.proceed",
1725                                   _("Continue? (y/N) ")))
1726         goto leave;
1727
1728
1729       answer = cpr_get ("cardedit.factory-reset.really",
1730                         _("Really do a factory reset? (enter \"yes\") "));
1731       cpr_kill_prompt ();
1732       trim_spaces (answer);
1733       if (strcmp (answer, "yes"))
1734         goto leave;
1735
1736       /* We need to select a card application before we can send APDUs
1737          to the card without scdaemon doing anything on its own.  */
1738       err = send_apdu (NULL, "RESET", 0);
1739       if (err)
1740         goto leave;
1741       err = send_apdu ("undefined", "dummy select ", 0);
1742       if (err)
1743         goto leave;
1744
1745       /* Select the OpenPGP application.  */
1746       err = send_apdu ("00A4040006D27600012401", "SELECT AID", 0);
1747       if (err)
1748         goto leave;
1749
1750       /* Do some dummy verifies with wrong PINs to set the retry
1751          counter to zero.  We can't easily use the card version 2.1
1752          feature of presenting the admin PIN to allow the terminate
1753          command because there is no machinery in scdaemon to catch
1754          the verify command and ask for the PIN when the "APDU"
1755          command is used. */
1756       for (i=0; i < 4; i++)
1757         send_apdu ("00200081084040404040404040", "VERIFY", 0xffff);
1758       for (i=0; i < 4; i++)
1759         send_apdu ("00200083084040404040404040", "VERIFY", 0xffff);
1760
1761       /* Send terminate datafile command.  */
1762       err = send_apdu ("00e60000", "TERMINATE DF", 0x6985);
1763       if (err)
1764         goto leave;
1765     }
1766
1767   /* The card is in termination state - reset and select again.  */
1768   err = send_apdu (NULL, "RESET", 0);
1769   if (err)
1770     goto leave;
1771   err = send_apdu ("undefined", "dummy select", 0);
1772   if (err)
1773     goto leave;
1774
1775   /* Select the OpenPGP application. (no error checking here). */
1776   send_apdu ("00A4040006D27600012401", "SELECT AID", 0xffff);
1777
1778   /* Send activate datafile command.  This is used without
1779      confirmation if the card is already in termination state.  */
1780   err = send_apdu ("00440000", "ACTIVATE DF", 0);
1781   if (err)
1782     goto leave;
1783
1784   /* Finally we reset the card reader once more.  */
1785   err = send_apdu (NULL, "RESET", 0);
1786   if (err)
1787     goto leave;
1788
1789  leave:
1790   xfree (answer);
1791   agent_release_card_info (&info);
1792 }
1793
1794
1795 \f
1796 /* Data used by the command parser.  This needs to be outside of the
1797    function scope to allow readline based command completion.  */
1798 enum cmdids
1799   {
1800     cmdNOP = 0,
1801     cmdQUIT, cmdADMIN, cmdHELP, cmdLIST, cmdDEBUG, cmdVERIFY,
1802     cmdNAME, cmdURL, cmdFETCH, cmdLOGIN, cmdLANG, cmdSEX, cmdCAFPR,
1803     cmdFORCESIG, cmdGENERATE, cmdPASSWD, cmdPRIVATEDO, cmdWRITECERT,
1804     cmdREADCERT, cmdUNBLOCK, cmdFACTORYRESET,
1805     cmdINVCMD
1806   };
1807
1808 static struct
1809 {
1810   const char *name;
1811   enum cmdids id;
1812   int admin_only;
1813   const char *desc;
1814 } cmds[] =
1815   {
1816     { "quit"    , cmdQUIT  , 0, N_("quit this menu")},
1817     { "q"       , cmdQUIT  , 0, NULL },
1818     { "admin"   , cmdADMIN , 0, N_("show admin commands")},
1819     { "help"    , cmdHELP  , 0, N_("show this help")},
1820     { "?"       , cmdHELP  , 0, NULL },
1821     { "list"    , cmdLIST  , 0, N_("list all available data")},
1822     { "l"       , cmdLIST  , 0, NULL },
1823     { "debug"   , cmdDEBUG , 0, NULL },
1824     { "name"    , cmdNAME  , 1, N_("change card holder's name")},
1825     { "url"     , cmdURL   , 1, N_("change URL to retrieve key")},
1826     { "fetch"   , cmdFETCH , 0, N_("fetch the key specified in the card URL")},
1827     { "login"   , cmdLOGIN , 1, N_("change the login name")},
1828     { "lang"    , cmdLANG  , 1, N_("change the language preferences")},
1829     { "sex"     , cmdSEX   , 1, N_("change card holder's sex")},
1830     { "cafpr"   , cmdCAFPR , 1, N_("change a CA fingerprint")},
1831     { "forcesig", cmdFORCESIG, 1, N_("toggle the signature force PIN flag")},
1832     { "generate", cmdGENERATE, 1, N_("generate new keys")},
1833     { "passwd"  , cmdPASSWD, 0, N_("menu to change or unblock the PIN")},
1834     { "verify"  , cmdVERIFY, 0, N_("verify the PIN and list all data")},
1835     { "unblock" , cmdUNBLOCK,0, N_("unblock the PIN using a Reset Code") },
1836     { "factory-reset", cmdFACTORYRESET, 1, N_("destroy all keys and data")},
1837     /* Note, that we do not announce these command yet. */
1838     { "privatedo", cmdPRIVATEDO, 0, NULL },
1839     { "readcert", cmdREADCERT, 0, NULL },
1840     { "writecert", cmdWRITECERT, 1, NULL },
1841     { NULL, cmdINVCMD, 0, NULL }
1842   };
1843
1844
1845 #ifdef HAVE_LIBREADLINE
1846
1847 /* These two functions are used by readline for command completion. */
1848
1849 static char *
1850 command_generator(const char *text,int state)
1851 {
1852   static int list_index,len;
1853   const char *name;
1854
1855   /* If this is a new word to complete, initialize now.  This includes
1856      saving the length of TEXT for efficiency, and initializing the
1857      index variable to 0. */
1858   if(!state)
1859     {
1860       list_index=0;
1861       len=strlen(text);
1862     }
1863
1864   /* Return the next partial match */
1865   while((name=cmds[list_index].name))
1866     {
1867       /* Only complete commands that have help text */
1868       if(cmds[list_index++].desc && strncmp(name,text,len)==0)
1869         return strdup(name);
1870     }
1871
1872   return NULL;
1873 }
1874
1875 static char **
1876 card_edit_completion(const char *text, int start, int end)
1877 {
1878   (void)end;
1879   /* If we are at the start of a line, we try and command-complete.
1880      If not, just do nothing for now. */
1881
1882   if(start==0)
1883     return rl_completion_matches(text,command_generator);
1884
1885   rl_attempted_completion_over=1;
1886
1887   return NULL;
1888 }
1889 #endif /*HAVE_LIBREADLINE*/
1890
1891 /* Menu to edit all user changeable values on an OpenPGP card.  Only
1892    Key creation is not handled here. */
1893 void
1894 card_edit (ctrl_t ctrl, strlist_t commands)
1895 {
1896   enum cmdids cmd = cmdNOP;
1897   int have_commands = !!commands;
1898   int redisplay = 1;
1899   char *answer = NULL;
1900   int allow_admin=0;
1901   char serialnobuf[50];
1902
1903
1904   if (opt.command_fd != -1)
1905     ;
1906   else if (opt.batch && !have_commands)
1907     {
1908       log_error(_("can't do this in batch mode\n"));
1909       goto leave;
1910     }
1911
1912   for (;;)
1913     {
1914       int arg_number;
1915       const char *arg_string = "";
1916       const char *arg_rest = "";
1917       char *p;
1918       int i;
1919       int cmd_admin_only;
1920
1921       tty_printf("\n");
1922       if (redisplay )
1923         {
1924           if (opt.with_colons)
1925             {
1926               card_status (es_stdout, serialnobuf, DIM (serialnobuf));
1927               fflush (stdout);
1928             }
1929           else
1930             {
1931               card_status (NULL, serialnobuf, DIM (serialnobuf));
1932               tty_printf("\n");
1933             }
1934           redisplay = 0;
1935         }
1936
1937       do
1938         {
1939           xfree (answer);
1940           if (have_commands)
1941             {
1942               if (commands)
1943                 {
1944                   answer = xstrdup (commands->d);
1945                   commands = commands->next;
1946                 }
1947               else if (opt.batch)
1948                 {
1949                   answer = xstrdup ("quit");
1950                 }
1951               else
1952                 have_commands = 0;
1953             }
1954
1955             if (!have_commands)
1956               {
1957                 tty_enable_completion (card_edit_completion);
1958                 answer = cpr_get_no_help("cardedit.prompt", _("gpg/card> "));
1959                 cpr_kill_prompt();
1960                 tty_disable_completion ();
1961               }
1962             trim_spaces(answer);
1963         }
1964       while ( *answer == '#' );
1965
1966       arg_number = 0; /* Yes, here is the init which egcc complains about */
1967       cmd_admin_only = 0;
1968       if (!*answer)
1969         cmd = cmdLIST; /* Default to the list command */
1970       else if (*answer == CONTROL_D)
1971         cmd = cmdQUIT;
1972       else
1973         {
1974           if ((p=strchr (answer,' ')))
1975             {
1976               *p++ = 0;
1977               trim_spaces (answer);
1978               trim_spaces (p);
1979               arg_number = atoi(p);
1980               arg_string = p;
1981               arg_rest = p;
1982               while (digitp (arg_rest))
1983                 arg_rest++;
1984               while (spacep (arg_rest))
1985                 arg_rest++;
1986             }
1987
1988           for (i=0; cmds[i].name; i++ )
1989             if (!ascii_strcasecmp (answer, cmds[i].name ))
1990               break;
1991
1992           cmd = cmds[i].id;
1993           cmd_admin_only = cmds[i].admin_only;
1994         }
1995
1996       if (!allow_admin && cmd_admin_only)
1997         {
1998           tty_printf ("\n");
1999           tty_printf (_("Admin-only command\n"));
2000           continue;
2001         }
2002
2003       switch (cmd)
2004         {
2005         case cmdHELP:
2006           for (i=0; cmds[i].name; i++ )
2007             if(cmds[i].desc
2008                && (!cmds[i].admin_only || (cmds[i].admin_only && allow_admin)))
2009               tty_printf("%-14s %s\n", cmds[i].name, _(cmds[i].desc) );
2010           break;
2011
2012         case cmdADMIN:
2013           if ( !strcmp (arg_string, "on") )
2014             allow_admin = 1;
2015           else if ( !strcmp (arg_string, "off") )
2016             allow_admin = 0;
2017           else if ( !strcmp (arg_string, "verify") )
2018             {
2019               /* Force verification of the Admin Command.  However,
2020                  this is only done if the retry counter is at initial
2021                  state.  */
2022               char *tmp = xmalloc (strlen (serialnobuf) + 6 + 1);
2023               strcpy (stpcpy (tmp, serialnobuf), "[CHV3]");
2024               allow_admin = !agent_scd_checkpin (tmp);
2025               xfree (tmp);
2026             }
2027           else /* Toggle. */
2028             allow_admin=!allow_admin;
2029           if(allow_admin)
2030             tty_printf(_("Admin commands are allowed\n"));
2031           else
2032             tty_printf(_("Admin commands are not allowed\n"));
2033           break;
2034
2035         case cmdVERIFY:
2036           agent_scd_checkpin (serialnobuf);
2037           redisplay = 1;
2038           break;
2039
2040         case cmdLIST:
2041           redisplay = 1;
2042           break;
2043
2044         case cmdNAME:
2045           change_name ();
2046           break;
2047
2048         case cmdURL:
2049           change_url ();
2050           break;
2051
2052         case cmdFETCH:
2053           fetch_url (ctrl);
2054           break;
2055
2056         case cmdLOGIN:
2057           change_login (arg_string);
2058           break;
2059
2060         case cmdLANG:
2061           change_lang ();
2062           break;
2063
2064         case cmdSEX:
2065           change_sex ();
2066           break;
2067
2068         case cmdCAFPR:
2069           if ( arg_number < 1 || arg_number > 3 )
2070             tty_printf ("usage: cafpr N\n"
2071                         "       1 <= N <= 3\n");
2072           else
2073             change_cafpr (arg_number);
2074           break;
2075
2076         case cmdPRIVATEDO:
2077           if ( arg_number < 1 || arg_number > 4 )
2078             tty_printf ("usage: privatedo N\n"
2079                         "       1 <= N <= 4\n");
2080           else
2081             change_private_do (arg_string, arg_number);
2082           break;
2083
2084         case cmdWRITECERT:
2085           if ( arg_number != 3 )
2086             tty_printf ("usage: writecert 3 < FILE\n");
2087           else
2088             change_cert (arg_rest);
2089           break;
2090
2091         case cmdREADCERT:
2092           if ( arg_number != 3 )
2093             tty_printf ("usage: readcert 3 > FILE\n");
2094           else
2095             read_cert (arg_rest);
2096           break;
2097
2098         case cmdFORCESIG:
2099           toggle_forcesig ();
2100           break;
2101
2102         case cmdGENERATE:
2103           generate_card_keys (ctrl);
2104           break;
2105
2106         case cmdPASSWD:
2107           change_pin (0, allow_admin);
2108           break;
2109
2110         case cmdUNBLOCK:
2111           change_pin (1, allow_admin);
2112           break;
2113
2114         case cmdFACTORYRESET:
2115           factory_reset ();
2116           break;
2117
2118         case cmdQUIT:
2119           goto leave;
2120
2121         case cmdNOP:
2122           break;
2123
2124         case cmdINVCMD:
2125         default:
2126           tty_printf ("\n");
2127           tty_printf (_("Invalid command  (try \"help\")\n"));
2128           break;
2129         } /* End command switch. */
2130     } /* End of main menu loop. */
2131
2132  leave:
2133   xfree (answer);
2134 }