1 /* card-util.c - Utility functions for the OpenPGP card.
2 * Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
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
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
29 #if GNUPG_MAJOR_VERSION != 1
31 #endif /*GNUPG_MAJOR_VERSION != 1*/
38 #include "keyserver-internal.h"
39 #if GNUPG_MAJOR_VERSION == 1
40 #ifdef HAVE_LIBREADLINE
42 #include <readline/readline.h>
43 #endif /*HAVE_LIBREADLINE*/
45 #else /*GNUPG_MAJOR_VERSION!=1*/
46 #include "call-agent.h"
47 #endif /*GNUPG_MAJOR_VERSION!=1*/
49 #define CONTROL_D ('D' - 'A' + 1)
52 /* Change the PIN of a an OpenPGP card. This is an interactive
55 change_pin (int chvno
, int allow_admin
)
57 struct agent_card_info_s info
;
60 rc
= agent_learn (&info
);
63 log_error (_("OpenPGP card not available: %s\n"),
68 log_info (_("OpenPGP card no. %s detected\n"),
69 info
.serialno
? info
.serialno
: "[none]");
71 agent_clear_pin_cache (info
.serialno
);
75 agent_release_card_info (&info
);
76 log_error (_("can't do this in batch mode\n"));
82 rc
= agent_scd_change_pin (1, info
.serialno
);
84 tty_printf ("Error changing the PIN: %s\n", gpg_strerror (rc
));
87 write_status (STATUS_SC_OP_SUCCESS
);
88 tty_printf ("PIN changed.\n");
97 tty_printf ("1 - change PIN\n"
99 "3 - change Admin PIN\n"
103 answer
= cpr_get("cardutil.change_pin.menu",_("Your selection? "));
105 if (strlen (answer
) != 1)
111 rc
= agent_scd_change_pin (1, info
.serialno
);
113 tty_printf ("Error changing the PIN: %s\n", gpg_strerror (rc
));
116 write_status (STATUS_SC_OP_SUCCESS
);
117 tty_printf ("PIN changed.\n");
120 else if (*answer
== '2')
122 rc
= agent_scd_change_pin (101, info
.serialno
);
124 tty_printf ("Error unblocking the PIN: %s\n", gpg_strerror (rc
));
127 write_status (STATUS_SC_OP_SUCCESS
);
128 tty_printf ("PIN unblocked and new PIN set.\n");
131 else if (*answer
== '3')
133 rc
= agent_scd_change_pin (3, info
.serialno
);
135 tty_printf ("Error changing the PIN: %s\n", gpg_strerror (rc
));
138 write_status (STATUS_SC_OP_SUCCESS
);
139 tty_printf ("PIN changed.\n");
142 else if (*answer
== 'q' || *answer
== 'Q')
148 agent_release_card_info (&info
);
152 get_manufacturer (unsigned int no
)
154 /* Note: Make sure that there is no colon or linefeed in the string. */
158 case 0xffff: return "test card";
159 case 0x0001: return "PPC Card Systems";
160 case 0x0002: return "Prism";
161 case 0x0003: return "OpenFortress";
162 default: return "unknown";
168 print_sha1_fpr (FILE *fp
, const unsigned char *fpr
)
174 for (i
=0; i
< 20 ; i
+=2, fpr
+= 2 )
177 tty_fprintf (fp
, " ");
178 tty_fprintf (fp
, " %02X%02X", *fpr
, fpr
[1]);
182 tty_fprintf (fp
, " [none]");
183 tty_fprintf (fp
, "\n");
188 print_sha1_fpr_colon (FILE *fp
, const unsigned char *fpr
)
194 for (i
=0; i
< 20 ; i
++, fpr
++)
195 fprintf (fp
, "%02X", *fpr
);
202 print_name (FILE *fp
, const char *text
, const char *name
)
204 tty_fprintf (fp
, "%s", text
);
206 /* FIXME: tty_printf_utf8_string2 eats everything after and
207 including an @ - e.g. when printing an url. */
211 print_utf8_string2 (fp
, name
, strlen (name
), '\n');
213 tty_print_utf8_string2 (name
, strlen (name
), 0);
216 tty_fprintf (fp
, _("[not set]"));
217 tty_fprintf (fp
, "\n");
221 print_isoname (FILE *fp
, const char *text
, const char *tag
, const char *name
)
224 fprintf (fp
, "%s:", tag
);
226 tty_fprintf (fp
, "%s", text
);
230 char *p
, *given
, *buf
= xstrdup (name
);
232 given
= strstr (buf
, "<<");
236 if (given
&& given
[2])
241 print_string (fp
, given
, strlen (given
), ':');
243 print_utf8_string2 (fp
, given
, strlen (given
), '\n');
245 tty_print_utf8_string2 (given
, strlen (given
), 0);
250 tty_fprintf (fp
, " ");
254 print_string (fp
, buf
, strlen (buf
), ':');
256 print_utf8_string2 (fp
, buf
, strlen (buf
), '\n');
258 tty_print_utf8_string2 (buf
, strlen (buf
), 0);
266 tty_fprintf (fp
, _("[not set]"));
272 tty_fprintf (fp
, "\n");
275 /* Return true if the SHA1 fingerprint FPR consists only of zeroes. */
277 fpr_is_zero (const char *fpr
)
281 for (i
=0; i
< 20 && !fpr
[i
]; i
++)
287 /* Print all available information about the current card. */
289 card_status (FILE *fp
, char *serialno
, size_t serialnobuflen
)
291 struct agent_card_info_s info
;
292 PKT_public_key
*pk
= xcalloc (1, sizeof *pk
);
295 const unsigned char *thefpr
;
298 if (serialno
&& serialnobuflen
)
301 rc
= agent_learn (&info
);
305 fputs ("AID:::\n", fp
);
306 log_error (_("OpenPGP card not available: %s\n"),
313 fprintf (fp
, "AID:%s:", info
.serialno
? info
.serialno
: "");
315 tty_fprintf (fp
, "Application ID ...: %s\n",
316 info
.serialno
? info
.serialno
: "[none]");
317 if (!info
.serialno
|| strncmp (info
.serialno
, "D27600012401", 12)
318 || strlen (info
.serialno
) != 32 )
321 fputs ("unknown:\n", fp
);
322 log_info ("not an OpenPGP card\n");
323 agent_release_card_info (&info
);
330 else if (strlen (serialno
)+1 > serialnobuflen
)
331 log_error ("serial number longer than expected\n");
333 strcpy (serialno
, info
.serialno
);
336 fputs ("openpgp-card:\n", fp
);
341 fprintf (fp
, "version:%.4s:\n", info
.serialno
+12);
342 uval
= xtoi_2(info
.serialno
+16)*256 + xtoi_2 (info
.serialno
+18);
343 fprintf (fp
, "vendor:%04x:%s:\n", uval
, get_manufacturer (uval
));
344 fprintf (fp
, "serial:%.8s:\n", info
.serialno
+20);
346 print_isoname (fp
, "Name of cardholder: ", "name", info
.disp_name
);
350 print_string (fp
, info
.disp_lang
, strlen (info
.disp_lang
), ':');
353 fprintf (fp
, "sex:%c:\n", (info
.disp_sex
== 1? 'm':
354 info
.disp_sex
== 2? 'f' : 'u'));
358 print_string (fp
, info
.pubkey_url
, strlen (info
.pubkey_url
), ':');
361 fputs ("login:", fp
);
363 print_string (fp
, info
.login_data
, strlen (info
.login_data
), ':');
366 fprintf (fp
, "forcepin:%d:::\n", !info
.chv1_cached
);
367 fprintf (fp
, "maxpinlen:%d:%d:%d:\n",
368 info
.chvmaxlen
[0], info
.chvmaxlen
[1], info
.chvmaxlen
[2]);
369 fprintf (fp
, "pinretry:%d:%d:%d:\n",
370 info
.chvretry
[0], info
.chvretry
[1], info
.chvretry
[2]);
371 fprintf (fp
, "sigcount:%lu:::\n", info
.sig_counter
);
373 for (i
=0; i
< 4; i
++)
375 if (info
.private_do
[i
])
377 fprintf (fp
, "private_do:%d:", i
+1);
378 print_string (fp
, info
.private_do
[i
],
379 strlen (info
.private_do
[i
]), ':');
384 fputs ("cafpr:", fp
);
385 print_sha1_fpr_colon (fp
, info
.cafpr1valid
? info
.cafpr1
:NULL
);
386 print_sha1_fpr_colon (fp
, info
.cafpr2valid
? info
.cafpr2
:NULL
);
387 print_sha1_fpr_colon (fp
, info
.cafpr3valid
? info
.cafpr3
:NULL
);
390 print_sha1_fpr_colon (fp
, info
.fpr1valid
? info
.fpr1
:NULL
);
391 print_sha1_fpr_colon (fp
, info
.fpr2valid
? info
.fpr2
:NULL
);
392 print_sha1_fpr_colon (fp
, info
.fpr3valid
? info
.fpr3
:NULL
);
394 fprintf (fp
, "fprtime:%lu:%lu:%lu:\n",
395 (unsigned long)info
.fpr1time
, (unsigned long)info
.fpr2time
,
396 (unsigned long)info
.fpr3time
);
400 tty_fprintf (fp
, "Version ..........: %.1s%c.%.1s%c\n",
401 info
.serialno
[12] == '0'?"":info
.serialno
+12,
403 info
.serialno
[14] == '0'?"":info
.serialno
+14,
405 tty_fprintf (fp
, "Manufacturer .....: %s\n",
406 get_manufacturer (xtoi_2(info
.serialno
+16)*256
407 + xtoi_2 (info
.serialno
+18)));
408 tty_fprintf (fp
, "Serial number ....: %.8s\n", info
.serialno
+20);
410 print_isoname (fp
, "Name of cardholder: ", "name", info
.disp_name
);
411 print_name (fp
, "Language prefs ...: ", info
.disp_lang
);
412 tty_fprintf (fp
, "Sex ..............: %s\n",
413 info
.disp_sex
== 1? _("male"):
414 info
.disp_sex
== 2? _("female") : _("unspecified"));
415 print_name (fp
, "URL of public key : ", info
.pubkey_url
);
416 print_name (fp
, "Login data .......: ", info
.login_data
);
417 if (info
.private_do
[0])
418 print_name (fp
, "Private DO 1 .....: ", info
.private_do
[0]);
419 if (info
.private_do
[1])
420 print_name (fp
, "Private DO 2 .....: ", info
.private_do
[1]);
421 if (info
.private_do
[2])
422 print_name (fp
, "Private DO 3 .....: ", info
.private_do
[2]);
423 if (info
.private_do
[3])
424 print_name (fp
, "Private DO 4 .....: ", info
.private_do
[3]);
425 if (info
.cafpr1valid
)
427 tty_fprintf (fp
, "CA fingerprint %d .:", 1);
428 print_sha1_fpr (fp
, info
.cafpr1
);
430 if (info
.cafpr2valid
)
432 tty_fprintf (fp
, "CA fingerprint %d .:", 2);
433 print_sha1_fpr (fp
, info
.cafpr2
);
435 if (info
.cafpr3valid
)
437 tty_fprintf (fp
, "CA fingerprint %d .:", 3);
438 print_sha1_fpr (fp
, info
.cafpr3
);
440 tty_fprintf (fp
, "Signature PIN ....: %s\n",
441 info
.chv1_cached
? _("not forced"): _("forced"));
442 tty_fprintf (fp
, "Max. PIN lengths .: %d %d %d\n",
443 info
.chvmaxlen
[0], info
.chvmaxlen
[1], info
.chvmaxlen
[2]);
444 tty_fprintf (fp
, "PIN retry counter : %d %d %d\n",
445 info
.chvretry
[0], info
.chvretry
[1], info
.chvretry
[2]);
446 tty_fprintf (fp
, "Signature counter : %lu\n", info
.sig_counter
);
447 tty_fprintf (fp
, "Signature key ....:");
448 print_sha1_fpr (fp
, info
.fpr1valid
? info
.fpr1
:NULL
);
449 if (info
.fpr1valid
&& info
.fpr1time
)
450 tty_fprintf (fp
, " created ....: %s\n",
451 isotimestamp (info
.fpr1time
));
452 tty_fprintf (fp
, "Encryption key....:");
453 print_sha1_fpr (fp
, info
.fpr2valid
? info
.fpr2
:NULL
);
454 if (info
.fpr2valid
&& info
.fpr2time
)
455 tty_fprintf (fp
, " created ....: %s\n",
456 isotimestamp (info
.fpr2time
));
457 tty_fprintf (fp
, "Authentication key:");
458 print_sha1_fpr (fp
, info
.fpr3valid
? info
.fpr3
:NULL
);
459 if (info
.fpr3valid
&& info
.fpr3time
)
460 tty_fprintf (fp
, " created ....: %s\n",
461 isotimestamp (info
.fpr3time
));
462 tty_fprintf (fp
, "General key info..: ");
464 thefpr
= (info
.fpr1valid
? info
.fpr1
: info
.fpr2valid
? info
.fpr2
:
465 info
.fpr3valid
? info
.fpr3
: NULL
);
466 if ( thefpr
&& !get_pubkey_byfprint (pk
, thefpr
, 20))
468 KBNODE keyblock
= NULL
;
470 print_pubkey_info (fp
, pk
);
472 if ( !get_seckeyblock_byfprint (&keyblock
, thefpr
, 20) )
473 print_card_key_info (fp
, keyblock
);
474 else if ( !get_keyblock_byfprint (&keyblock
, thefpr
, 20) )
476 release_kbnode (keyblock
);
479 if (!auto_create_card_key_stub (info
.serialno
,
480 info
.fpr1valid
? info
.fpr1
:NULL
,
481 info
.fpr2valid
? info
.fpr2
:NULL
,
482 info
.fpr3valid
? info
.fpr3
:NULL
))
484 if ( !get_seckeyblock_byfprint (&keyblock
, thefpr
, 20) )
485 print_card_key_info (fp
, keyblock
);
489 release_kbnode (keyblock
);
492 tty_fprintf (fp
, "[none]\n");
495 free_public_key (pk
);
496 agent_release_card_info (&info
);
501 get_one_name (const char *prompt1
, const char *prompt2
)
508 name
= cpr_get (prompt1
, prompt2
);
513 for (i
=0; name
[i
] && name
[i
] >= ' ' && name
[i
] <= 126; i
++)
516 /* The name must be in Latin-1 and not UTF-8 - lacking the code
517 to ensure this we restrict it to ASCII. */
519 tty_printf (_("Error: Only plain ASCII is currently allowed.\n"));
520 else if (strchr (name
, '<'))
521 tty_printf (_("Error: The \"<\" character may not be used.\n"));
522 else if (strstr (name
, " "))
523 tty_printf (_("Error: Double spaces are not allowed.\n"));
535 char *surname
= NULL
, *givenname
= NULL
;
539 surname
= get_one_name ("keygen.smartcard.surname",
540 _("Cardholder's surname: "));
541 givenname
= get_one_name ("keygen.smartcard.givenname",
542 _("Cardholder's given name: "));
543 if (!surname
|| !givenname
|| (!*surname
&& !*givenname
))
547 return -1; /*canceled*/
550 isoname
= xmalloc ( strlen (surname
) + 2 + strlen (givenname
) + 1);
551 strcpy (stpcpy (stpcpy (isoname
, surname
), "<<"), givenname
);
554 for (p
=isoname
; *p
; p
++)
558 if (strlen (isoname
) > 39 )
560 tty_printf (_("Error: Combined name too long "
561 "(limit is %d characters).\n"), 39);
566 rc
= agent_scd_setattr ("DISP-NAME", isoname
, strlen (isoname
), NULL
);
568 log_error ("error setting Name: %s\n", gpg_strerror (rc
));
581 url
= cpr_get ("cardedit.change_url", _("URL to retrieve public key: "));
587 if (strlen (url
) > 254 )
589 tty_printf (_("Error: URL too long "
590 "(limit is %d characters).\n"), 254);
595 rc
= agent_scd_setattr ("PUBKEY-URL", url
, strlen (url
), NULL
);
597 log_error ("error setting URL: %s\n", gpg_strerror (rc
));
603 /* Fetch the key from the URL given on the card or try to get it from
604 the default keyserver. */
608 #if GNUPG_MAJOR_VERSION == 1
610 struct agent_card_info_s info
;
612 memset(&info
,0,sizeof(info
));
614 rc
=agent_scd_getattr("PUBKEY-URL",&info
);
616 log_error("error retrieving URL from card: %s\n",gpg_strerror(rc
));
619 struct keyserver_spec
*spec
=NULL
;
621 rc
=agent_scd_getattr("KEY-FPR",&info
);
623 log_error("error retrieving key fingerprint from card: %s\n",
625 else if (info
.pubkey_url
&& *info
.pubkey_url
)
627 spec
=parse_keyserver_uri(info
.pubkey_url
,1,NULL
,0);
628 if(spec
&& info
.fpr1valid
)
630 /* This is not perfectly right. Currently, all card
631 fingerprints are 20 digits, but what about
632 fingerprints for a future v5 key? We should get the
633 length from somewhere lower in the code. In any
634 event, the fpr/keyid is not meaningful for straight
635 HTTP fetches, but using it allows the card to point
636 to HKP and LDAP servers as well. */
637 rc
=keyserver_import_fprint(info
.fpr1
,20,spec
);
638 free_keyserver_spec(spec
);
641 else if (info
.fpr1valid
)
643 rc
= keyserver_import_fprint (info
.fpr1
, 20, opt
.keyserver
);
655 change_login (const char *args
)
661 if (args
&& *args
== '<') /* Read it from a file */
665 for (args
++; spacep (args
); args
++)
667 fp
= fopen (args
, "rb");
668 #if GNUPG_MAJOR_VERSION == 1
669 if (fp
&& is_secured_file (fileno (fp
)))
678 tty_printf (_("can't open `%s': %s\n"), args
, strerror (errno
));
682 data
= xmalloc (254);
683 n
= fread (data
, 1, 254, fp
);
687 tty_printf (_("error reading `%s': %s\n"), args
, strerror (errno
));
694 data
= cpr_get ("cardedit.change_login",
695 _("Login data (account name): "));
705 tty_printf (_("Error: Login data too long "
706 "(limit is %d characters).\n"), 254);
711 rc
= agent_scd_setattr ("LOGIN-DATA", data
, n
, NULL
);
713 log_error ("error setting login data: %s\n", gpg_strerror (rc
));
719 change_private_do (const char *args
, int nr
)
721 char do_name
[] = "PRIVATE-DO-X";
726 assert (nr
>= 1 && nr
<= 4);
727 do_name
[11] = '0' + nr
;
729 if (args
&& (args
= strchr (args
, '<'))) /* Read it from a file */
733 /* Fixme: Factor this duplicated code out. */
734 for (args
++; spacep (args
); args
++)
736 fp
= fopen (args
, "rb");
737 #if GNUPG_MAJOR_VERSION == 1
738 if (fp
&& is_secured_file (fileno (fp
)))
747 tty_printf (_("can't open `%s': %s\n"), args
, strerror (errno
));
751 data
= xmalloc (254);
752 n
= fread (data
, 1, 254, fp
);
756 tty_printf (_("error reading `%s': %s\n"), args
, strerror (errno
));
763 data
= cpr_get ("cardedit.change_private_do",
764 _("Private DO data: "));
774 tty_printf (_("Error: Private DO too long "
775 "(limit is %d characters).\n"), 254);
780 rc
= agent_scd_setattr (do_name
, data
, n
, NULL
);
782 log_error ("error setting private DO: %s\n", gpg_strerror (rc
));
793 data
= cpr_get ("cardedit.change_lang",
794 _("Language preferences: "));
800 if (strlen (data
) > 8 || (strlen (data
) & 1))
802 tty_printf (_("Error: invalid length of preference string.\n"));
807 for (p
=data
; *p
&& *p
>= 'a' && *p
<= 'z'; p
++)
811 tty_printf (_("Error: invalid characters in preference string.\n"));
816 rc
= agent_scd_setattr ("DISP-LANG", data
, strlen (data
), NULL
);
818 log_error ("error setting lang: %s\n", gpg_strerror (rc
));
831 data
= cpr_get ("cardedit.change_sex",
832 _("Sex ((M)ale, (F)emale or space): "));
840 else if ((*data
== 'M' || *data
== 'm') && !data
[1])
842 else if ((*data
== 'F' || *data
== 'f') && !data
[1])
846 tty_printf (_("Error: invalid response.\n"));
851 rc
= agent_scd_setattr ("DISP-SEX", str
, 1, NULL
);
853 log_error ("error setting sex: %s\n", gpg_strerror (rc
));
860 change_cafpr (int fprno
)
865 unsigned char fpr
[20];
867 data
= cpr_get ("cardedit.change_cafpr", _("CA fingerprint: "));
873 for (i
=0, s
=data
; i
< 20 && *s
; )
890 tty_printf (_("Error: invalid formatted fingerprint.\n"));
894 rc
= agent_scd_setattr (fprno
==1?"CA-FPR-1":
896 fprno
==3?"CA-FPR-3":"x", fpr
, 20, NULL
);
898 log_error ("error setting cafpr: %s\n", gpg_strerror (rc
));
905 toggle_forcesig (void)
907 struct agent_card_info_s info
;
911 memset (&info
, 0, sizeof info
);
912 rc
= agent_scd_getattr ("CHV-STATUS", &info
);
915 log_error ("error getting current status: %s\n", gpg_strerror (rc
));
918 newstate
= !info
.chv1_cached
;
919 agent_release_card_info (&info
);
921 rc
= agent_scd_setattr ("CHV-STATUS-1", newstate
? "\x01":"", 1, NULL
);
923 log_error ("error toggling signature PIN flag: %s\n", gpg_strerror (rc
));
927 /* Helper for the key generation/edit functions. */
929 get_info_for_key_operation (struct agent_card_info_s
*info
)
933 memset (info
, 0, sizeof *info
);
934 rc
= agent_scd_getattr ("SERIALNO", info
);
935 if (rc
|| !info
->serialno
|| strncmp (info
->serialno
, "D27600012401", 12)
936 || strlen (info
->serialno
) != 32 )
938 log_error (_("key operation not possible: %s\n"),
939 rc
? gpg_strerror (rc
) : _("not an OpenPGP card"));
942 rc
= agent_scd_getattr ("KEY-FPR", info
);
944 rc
= agent_scd_getattr ("CHV-STATUS", info
);
946 rc
= agent_scd_getattr ("DISP-NAME", info
);
948 log_error (_("error getting current key info: %s\n"), gpg_strerror (rc
));
953 /* Helper for the key generation/edit functions. */
955 check_pin_for_key_operation (struct agent_card_info_s
*info
, int *forced_chv1
)
959 agent_clear_pin_cache (info
->serialno
);
961 *forced_chv1
= !info
->chv1_cached
;
963 { /* Switch of the forced mode so that during key generation we
964 don't get bothered with PIN queries for each
966 rc
= agent_scd_setattr ("CHV-STATUS-1", "\x01", 1, info
->serialno
);
969 log_error ("error clearing forced signature PIN flag: %s\n",
977 /* Check the PIN now, so that we won't get asked later for each
978 binding signature. */
979 rc
= agent_scd_checkpin (info
->serialno
);
981 log_error ("error checking the PIN: %s\n", gpg_strerror (rc
));
986 /* Helper for the key generation/edit functions. */
988 restore_forced_chv1 (int *forced_chv1
)
993 { /* Switch back to forced state. */
994 rc
= agent_scd_setattr ("CHV-STATUS-1", "", 1, NULL
);
997 log_error ("error setting forced signature PIN flag: %s\n",
1003 #if GNUPG_MAJOR_VERSION == 1
1004 /* Helper for the key generation/edit functions. */
1006 show_card_key_info (struct agent_card_info_s
*info
)
1008 tty_fprintf (NULL
, "Signature key ....:");
1009 print_sha1_fpr (NULL
, info
->fpr1valid
? info
->fpr1
:NULL
);
1010 tty_fprintf (NULL
, "Encryption key....:");
1011 print_sha1_fpr (NULL
, info
->fpr2valid
? info
->fpr2
:NULL
);
1012 tty_fprintf (NULL
, "Authentication key:");
1013 print_sha1_fpr (NULL
, info
->fpr3valid
? info
->fpr3
:NULL
);
1018 #if GNUPG_MAJOR_VERSION == 1
1019 /* Helper for the key generation/edit functions. */
1021 replace_existing_key_p (struct agent_card_info_s
*info
, int keyno
)
1023 assert (keyno
>= 0 && keyno
<= 3);
1025 if ((keyno
== 1 && info
->fpr1valid
)
1026 || (keyno
== 2 && info
->fpr2valid
)
1027 || (keyno
== 3 && info
->fpr3valid
))
1030 log_info ("WARNING: such a key has already been stored on the card!\n");
1032 if ( !cpr_get_answer_is_yes( "cardedit.genkeys.replace_key",
1033 _("Replace existing key? (y/N) ")))
1042 generate_card_keys (const char *serialno
)
1044 struct agent_card_info_s info
;
1048 if (get_info_for_key_operation (&info
))
1051 #if GNUPG_MAJOR_VERSION == 1
1053 char *answer
=cpr_get("cardedit.genkeys.backup_enc",
1054 _("Make off-card backup of encryption key? (Y/n) "));
1056 want_backup
=answer_is_yes_no_default(answer
,1);
1061 want_backup
= cpr_get_answer_is_yes
1062 ( "cardedit.genkeys.backup_enc",
1063 _("Make off-card backup of encryption key? (Y/n) "));
1064 /*FIXME: we need answer_is_yes_no_default()*/
1067 if ( (info
.fpr1valid
&& !fpr_is_zero (info
.fpr1
))
1068 || (info
.fpr2valid
&& !fpr_is_zero (info
.fpr2
))
1069 || (info
.fpr3valid
&& !fpr_is_zero (info
.fpr3
)))
1072 log_info ("NOTE: keys are already stored on the card!\n");
1074 if ( !cpr_get_answer_is_yes( "cardedit.genkeys.replace_keys",
1075 _("Replace existing keys? (y/N) ")))
1077 agent_release_card_info (&info
);
1081 else if (!info
.disp_name
|| !*info
.disp_name
)
1084 tty_printf (_("Please note that the factory settings of the PINs are\n"
1085 " PIN = `%s' Admin PIN = `%s'\n"
1086 "You should change them using the command --change-pin\n"),
1087 "123456", "12345678");
1091 if (check_pin_for_key_operation (&info
, &forced_chv1
))
1094 #if GNUPG_MAJOR_VERSION == 1
1095 generate_keypair (NULL
, info
.serialno
,
1096 want_backup
? opt
.homedir
:NULL
);
1098 generate_keypair (NULL
, info
.serialno
);
1102 agent_release_card_info (&info
);
1103 restore_forced_chv1 (&forced_chv1
);
1107 /* This function is used by the key edit menu to generate an arbitrary
1110 card_generate_subkey (KBNODE pub_keyblock
, KBNODE sec_keyblock
)
1112 #if GNUPG_MAJOR_VERSION == 1
1113 struct agent_card_info_s info
;
1115 int forced_chv1
= 0;
1118 if (get_info_for_key_operation (&info
))
1121 show_card_key_info (&info
);
1123 tty_printf (_("Please select the type of key to generate:\n"));
1125 tty_printf (_(" (1) Signature key\n"));
1126 tty_printf (_(" (2) Encryption key\n"));
1127 tty_printf (_(" (3) Authentication key\n"));
1131 char *answer
= cpr_get ("cardedit.genkeys.subkeytype",
1132 _("Your selection? "));
1134 if (*answer
== CONTROL_D
)
1139 keyno
= *answer
? atoi(answer
): 0;
1141 if (keyno
>= 1 && keyno
<= 3)
1143 tty_printf(_("Invalid selection.\n"));
1146 if (replace_existing_key_p (&info
, keyno
))
1149 if (check_pin_for_key_operation (&info
, &forced_chv1
))
1152 okay
= generate_card_subkeypair (pub_keyblock
, sec_keyblock
,
1153 keyno
, info
.serialno
);
1156 agent_release_card_info (&info
);
1157 restore_forced_chv1 (&forced_chv1
);
1165 /* Store the key at NODE into the smartcard and modify NODE to
1166 carry the serialno stuff instead of the actual secret key
1167 parameters. USE is the usage for that key; 0 means any
1170 card_store_subkey (KBNODE node
, int use
)
1172 #if GNUPG_MAJOR_VERSION == 1
1173 struct agent_card_info_s info
;
1177 PKT_secret_key
*copied_sk
= NULL
;
1183 assert (node
->pkt
->pkttype
== PKT_SECRET_KEY
1184 || node
->pkt
->pkttype
== PKT_SECRET_SUBKEY
);
1185 sk
= node
->pkt
->pkt
.secret_key
;
1187 if (get_info_for_key_operation (&info
))
1190 show_card_key_info (&info
);
1192 if (!is_RSA (sk
->pubkey_algo
) || nbits_from_sk (sk
) != 1024 )
1194 tty_printf ("You may only store a 1024 bit RSA key on the card\n");
1199 allow_keyno
[0] = (!use
|| (use
& (PUBKEY_USAGE_SIG
)));
1200 allow_keyno
[1] = (!use
|| (use
& (PUBKEY_USAGE_ENC
)));
1201 allow_keyno
[2] = (!use
|| (use
& (PUBKEY_USAGE_SIG
|PUBKEY_USAGE_AUTH
)));
1203 tty_printf (_("Please select where to store the key:\n"));
1206 tty_printf (_(" (1) Signature key\n"));
1208 tty_printf (_(" (2) Encryption key\n"));
1210 tty_printf (_(" (3) Authentication key\n"));
1214 char *answer
= cpr_get ("cardedit.genkeys.storekeytype",
1215 _("Your selection? "));
1217 if (*answer
== CONTROL_D
|| !*answer
)
1222 keyno
= *answer
? atoi(answer
): 0;
1224 if (keyno
>= 1 && keyno
<= 3 && allow_keyno
[keyno
-1])
1226 tty_printf(_("Invalid selection.\n"));
1229 if (replace_existing_key_p (&info
, keyno
))
1232 /* Unprotect key. */
1233 switch (is_secret_key_protected (sk
) )
1235 case 0: /* Not protected. */
1238 log_error (_("unknown key protection algorithm\n"));
1241 if (sk
->protect
.s2k
.mode
== 1001)
1243 log_error (_("secret parts of key are not available\n"));
1246 if (sk
->protect
.s2k
.mode
== 1002)
1248 log_error (_("secret key already stored on a card\n"));
1251 /* We better copy the key before we unprotect it. */
1252 copied_sk
= sk
= copy_secret_key (NULL
, sk
);
1253 rc
= check_secret_key (sk
, 0);
1258 rc
= save_unprotected_key_to_card (sk
, keyno
);
1262 /* Get back to the maybe protected original secret key. */
1265 free_secret_key (copied_sk
);
1268 sk
= node
->pkt
->pkt
.secret_key
;
1270 /* Get rid of the secret key parameters and store the serial numer. */
1271 n
= pubkey_get_nskey (sk
->pubkey_algo
);
1272 for (i
=pubkey_get_npkey (sk
->pubkey_algo
); i
< n
; i
++)
1274 mpi_free (sk
->skey
[i
]);
1277 i
= pubkey_get_npkey (sk
->pubkey_algo
);
1278 sk
->skey
[i
] = mpi_set_opaque (NULL
, xstrdup ("dummydata"), 10);
1279 sk
->is_protected
= 1;
1280 sk
->protect
.s2k
.mode
= 1002;
1282 for (sk
->protect
.ivlen
=0; sk
->protect
.ivlen
< 16 && *s
&& s
[1];
1283 sk
->protect
.ivlen
++, s
+= 2)
1284 sk
->protect
.iv
[sk
->protect
.ivlen
] = xtoi_2 (s
);
1290 free_secret_key (copied_sk
);
1291 agent_release_card_info (&info
);
1300 /* Data used by the command parser. This needs to be outside of the
1301 function scope to allow readline based command completion. */
1305 cmdQUIT
, cmdADMIN
, cmdHELP
, cmdLIST
, cmdDEBUG
, cmdVERIFY
,
1306 cmdNAME
, cmdURL
, cmdFETCH
, cmdLOGIN
, cmdLANG
, cmdSEX
, cmdCAFPR
,
1307 cmdFORCESIG
, cmdGENERATE
, cmdPASSWD
, cmdPRIVATEDO
,
1319 { "quit" , cmdQUIT
, 0, N_("quit this menu")},
1320 { "q" , cmdQUIT
, 0, NULL
},
1321 { "admin" , cmdADMIN
, 0, N_("show admin commands")},
1322 { "help" , cmdHELP
, 0, N_("show this help")},
1323 { "?" , cmdHELP
, 0, NULL
},
1324 { "list" , cmdLIST
, 0, N_("list all available data")},
1325 { "l" , cmdLIST
, 0, NULL
},
1326 { "debug" , cmdDEBUG
, 0, NULL
},
1327 { "name" , cmdNAME
, 1, N_("change card holder's name")},
1328 { "url" , cmdURL
, 1, N_("change URL to retrieve key")},
1329 { "fetch" , cmdFETCH
, 0, N_("fetch the key specified in the card URL")},
1330 { "login" , cmdLOGIN
, 1, N_("change the login name")},
1331 { "lang" , cmdLANG
, 1, N_("change the language preferences")},
1332 { "sex" , cmdSEX
, 1, N_("change card holder's sex")},
1333 { "cafpr" , cmdCAFPR
, 1, N_("change a CA fingerprint")},
1334 { "forcesig", cmdFORCESIG
, 1, N_("toggle the signature force PIN flag")},
1335 { "generate", cmdGENERATE
, 1, N_("generate new keys")},
1336 { "passwd" , cmdPASSWD
, 0, N_("menu to change or unblock the PIN")},
1337 { "verify" , cmdVERIFY
, 0, N_("verify the PIN and list all data")},
1338 /* Note, that we do not announce this command yet. */
1339 { "privatedo", cmdPRIVATEDO
, 0, NULL
},
1340 { NULL
, cmdINVCMD
, 0, NULL
}
1344 #if GNUPG_MAJOR_VERSION == 1 && defined (HAVE_LIBREADLINE)
1346 /* These two functions are used by readline for command completion. */
1349 command_generator(const char *text
,int state
)
1351 static int list_index
,len
;
1354 /* If this is a new word to complete, initialize now. This includes
1355 saving the length of TEXT for efficiency, and initializing the
1356 index variable to 0. */
1363 /* Return the next partial match */
1364 while((name
=cmds
[list_index
].name
))
1366 /* Only complete commands that have help text */
1367 if(cmds
[list_index
++].desc
&& strncmp(name
,text
,len
)==0)
1368 return strdup(name
);
1375 card_edit_completion(const char *text
, int start
, int end
)
1377 /* If we are at the start of a line, we try and command-complete.
1378 If not, just do nothing for now. */
1381 return rl_completion_matches(text
,command_generator
);
1383 rl_attempted_completion_over
=1;
1387 #endif /* GNUPG_MAJOR_VERSION == 1 && HAVE_LIBREADLINE */
1389 /* Menu to edit all user changeable values on an OpenPGP card. Only
1390 Key creation is not handled here. */
1392 card_edit (STRLIST commands
)
1394 enum cmdids cmd
= cmdNOP
;
1395 int have_commands
= !!commands
;
1397 char *answer
= NULL
;
1398 int did_checkpin
= 0, allow_admin
=0;
1399 char serialnobuf
[50];
1402 if (opt
.command_fd
!= -1)
1404 else if (opt
.batch
&& !have_commands
)
1406 log_error(_("can't do this in batch mode\n"));
1413 const char *arg_string
= "";
1421 if (opt
.with_colons
)
1423 card_status (stdout
, serialnobuf
, DIM (serialnobuf
));
1428 card_status (NULL
, serialnobuf
, DIM (serialnobuf
));
1441 answer
= xstrdup (commands
->d
);
1442 commands
= commands
->next
;
1446 answer
= xstrdup ("quit");
1454 #if GNUPG_MAJOR_VERSION == 1
1455 tty_enable_completion (card_edit_completion
);
1457 answer
= cpr_get_no_help("cardedit.prompt", _("Command> "));
1459 #if GNUPG_MAJOR_VERSION == 1
1460 tty_disable_completion ();
1463 trim_spaces(answer
);
1465 while ( *answer
== '#' );
1467 arg_number
= 0; /* Yes, here is the init which egcc complains about */
1470 cmd
= cmdLIST
; /* Default to the list command */
1471 else if (*answer
== CONTROL_D
)
1475 if ((p
=strchr (answer
,' ')))
1478 trim_spaces (answer
);
1480 arg_number
= atoi(p
);
1484 for (i
=0; cmds
[i
].name
; i
++ )
1485 if (!ascii_strcasecmp (answer
, cmds
[i
].name
))
1489 cmd_admin_only
= cmds
[i
].admin_only
;
1492 if (!allow_admin
&& cmd_admin_only
)
1495 tty_printf (_("Admin-only command\n"));
1502 for (i
=0; cmds
[i
].name
; i
++ )
1504 && (!cmds
[i
].admin_only
|| (cmds
[i
].admin_only
&& allow_admin
)))
1505 tty_printf("%-10s %s\n", cmds
[i
].name
, _(cmds
[i
].desc
) );
1509 if ( !strcmp (arg_string
, "on") )
1511 else if ( !strcmp (arg_string
, "off") )
1513 else if ( !strcmp (arg_string
, "verify") )
1515 /* Force verification of the Admin Command. However,
1516 this is only done if the retry counter is at initial
1518 char *tmp
= xmalloc (strlen (serialnobuf
) + 6 + 1);
1519 strcpy (stpcpy (tmp
, serialnobuf
), "[CHV3]");
1520 allow_admin
= !agent_scd_checkpin (tmp
);
1524 allow_admin
=!allow_admin
;
1526 tty_printf(_("Admin commands are allowed\n"));
1528 tty_printf(_("Admin commands are not allowed\n"));
1532 agent_scd_checkpin (serialnobuf
);
1553 change_login (arg_string
);
1565 if ( arg_number
< 1 || arg_number
> 3 )
1566 tty_printf ("usage: cafpr N\n"
1569 change_cafpr (arg_number
);
1573 if ( arg_number
< 1 || arg_number
> 4 )
1574 tty_printf ("usage: privatedo N\n"
1577 change_private_do (arg_string
, arg_number
);
1585 generate_card_keys (serialnobuf
);
1589 change_pin (0, allow_admin
);
1590 did_checkpin
= 0; /* Need to reset it of course. */
1602 tty_printf (_("Invalid command (try \"help\")\n"));
1604 } /* End command switch. */
1605 } /* End of main menu loop. */