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 3 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, see <http://www.gnu.org/licenses/>.
27 #if GNUPG_MAJOR_VERSION != 1
29 #endif /*GNUPG_MAJOR_VERSION != 1*/
36 #include "keyserver-internal.h"
37 #if GNUPG_MAJOR_VERSION == 1
38 # ifdef HAVE_LIBREADLINE
39 # define GNUPG_LIBREADLINE_H_INCLUDED
41 # include <readline/readline.h>
42 # endif /*HAVE_LIBREADLINE*/
43 # include "cardglue.h"
44 #else /*GNUPG_MAJOR_VERSION!=1*/
45 # include "call-agent.h"
46 #endif /*GNUPG_MAJOR_VERSION!=1*/
48 #define CONTROL_D ('D' - 'A' + 1)
51 /* Change the PIN of a an OpenPGP card. This is an interactive
54 change_pin (int unblock_v2
, int allow_admin
)
56 struct agent_card_info_s info
;
59 rc
= agent_learn (&info
);
62 log_error (_("OpenPGP card not available: %s\n"),
67 log_info (_("OpenPGP card no. %s detected\n"),
68 info
.serialno
? info
.serialno
: "[none]");
70 agent_clear_pin_cache (info
.serialno
);
74 agent_release_card_info (&info
);
75 log_error (_("can't do this in batch mode\n"));
83 log_error (_("This command is only available for version 2 cards\n"));
84 else if (!info
.chvretry
[1])
85 log_error (_("Reset Code not or not anymore available\n"));
88 rc
= agent_scd_change_pin (2, info
.serialno
);
90 tty_printf ("Error changing the PIN: %s\n", gpg_strerror (rc
));
93 write_status (STATUS_SC_OP_SUCCESS
);
94 tty_printf ("PIN changed.\n");
98 else if (!allow_admin
)
100 rc
= agent_scd_change_pin (1, info
.serialno
);
102 tty_printf ("Error changing the PIN: %s\n", gpg_strerror (rc
));
105 write_status (STATUS_SC_OP_SUCCESS
);
106 tty_printf ("PIN changed.\n");
115 tty_printf ("1 - change PIN\n"
117 "3 - change Admin PIN\n"
118 "4 - set the Reset Code\n"
122 answer
= cpr_get("cardutil.change_pin.menu",_("Your selection? "));
124 if (strlen (answer
) != 1)
131 rc
= agent_scd_change_pin (1, info
.serialno
);
133 tty_printf ("Error changing the PIN: %s\n", gpg_strerror (rc
));
136 write_status (STATUS_SC_OP_SUCCESS
);
137 tty_printf ("PIN changed.\n");
140 else if (*answer
== '2')
143 rc
= agent_scd_change_pin (101, info
.serialno
);
145 tty_printf ("Error unblocking the PIN: %s\n", gpg_strerror (rc
));
148 write_status (STATUS_SC_OP_SUCCESS
);
149 tty_printf ("PIN unblocked and new PIN set.\n");
152 else if (*answer
== '3')
154 /* Change Admin PIN. */
155 rc
= agent_scd_change_pin (3, info
.serialno
);
157 tty_printf ("Error changing the PIN: %s\n", gpg_strerror (rc
));
160 write_status (STATUS_SC_OP_SUCCESS
);
161 tty_printf ("PIN changed.\n");
164 else if (*answer
== '4')
166 /* Set a new Reset Code. */
167 rc
= agent_scd_change_pin (102, info
.serialno
);
169 tty_printf ("Error setting the Reset Code: %s\n",
173 write_status (STATUS_SC_OP_SUCCESS
);
174 tty_printf ("Reset Code set.\n");
177 else if (*answer
== 'q' || *answer
== 'Q')
183 agent_release_card_info (&info
);
187 get_manufacturer (unsigned int no
)
189 /* Note: Make sure that there is no colon or linefeed in the string. */
192 case 0x0001: return "PPC Card Systems";
193 case 0x0002: return "Prism";
194 case 0x0003: return "OpenFortress";
195 case 0x0004: return "Wewid AB";
197 /* 0x00000 and 0xFFFF are defined as test cards per spec,
198 0xFFF00 to 0xFFFE are assigned for use with randomly created
201 case 0xffff: return "test card";
202 default: return (no
& 0xff00) == 0xff00? "unmanaged S/N range":"unknown";
208 print_sha1_fpr (FILE *fp
, const unsigned char *fpr
)
214 for (i
=0; i
< 20 ; i
+=2, fpr
+= 2 )
217 tty_fprintf (fp
, " ");
218 tty_fprintf (fp
, " %02X%02X", *fpr
, fpr
[1]);
222 tty_fprintf (fp
, " [none]");
223 tty_fprintf (fp
, "\n");
228 print_sha1_fpr_colon (FILE *fp
, const unsigned char *fpr
)
234 for (i
=0; i
< 20 ; i
++, fpr
++)
235 fprintf (fp
, "%02X", *fpr
);
242 print_name (FILE *fp
, const char *text
, const char *name
)
244 tty_fprintf (fp
, "%s", text
);
246 /* FIXME: tty_printf_utf8_string2 eats everything after and
247 including an @ - e.g. when printing an url. */
251 print_utf8_string2 (fp
, name
, strlen (name
), '\n');
253 tty_print_utf8_string2 (name
, strlen (name
), 0);
256 tty_fprintf (fp
, _("[not set]"));
257 tty_fprintf (fp
, "\n");
261 print_isoname (FILE *fp
, const char *text
, const char *tag
, const char *name
)
264 fprintf (fp
, "%s:", tag
);
266 tty_fprintf (fp
, "%s", text
);
270 char *p
, *given
, *buf
= xstrdup (name
);
272 given
= strstr (buf
, "<<");
276 if (given
&& given
[2])
281 print_string (fp
, given
, strlen (given
), ':');
283 print_utf8_string2 (fp
, given
, strlen (given
), '\n');
285 tty_print_utf8_string2 (given
, strlen (given
), 0);
290 tty_fprintf (fp
, " ");
294 print_string (fp
, buf
, strlen (buf
), ':');
296 print_utf8_string2 (fp
, buf
, strlen (buf
), '\n');
298 tty_print_utf8_string2 (buf
, strlen (buf
), 0);
306 tty_fprintf (fp
, _("[not set]"));
312 tty_fprintf (fp
, "\n");
315 /* Return true if the SHA1 fingerprint FPR consists only of zeroes. */
317 fpr_is_zero (const char *fpr
)
321 for (i
=0; i
< 20 && !fpr
[i
]; i
++)
327 /* Return true if the SHA1 fingerprint FPR consists only of 0xFF. */
329 fpr_is_ff (const char *fpr
)
333 for (i
=0; i
< 20 && fpr
[i
] == '\xff'; i
++)
339 /* Print all available information about the current card. */
341 card_status (FILE *fp
, char *serialno
, size_t serialnobuflen
)
343 struct agent_card_info_s info
;
344 PKT_public_key
*pk
= xcalloc (1, sizeof *pk
);
347 const unsigned char *thefpr
;
350 if (serialno
&& serialnobuflen
)
353 rc
= agent_learn (&info
);
357 fputs ("AID:::\n", fp
);
358 log_error (_("OpenPGP card not available: %s\n"),
365 fprintf (fp
, "AID:%s:", info
.serialno
? info
.serialno
: "");
367 tty_fprintf (fp
, "Application ID ...: %s\n",
368 info
.serialno
? info
.serialno
: "[none]");
369 if (!info
.serialno
|| strncmp (info
.serialno
, "D27600012401", 12)
370 || strlen (info
.serialno
) != 32 )
373 fputs ("unknown:\n", fp
);
374 log_info ("not an OpenPGP card\n");
375 agent_release_card_info (&info
);
382 else if (strlen (serialno
)+1 > serialnobuflen
)
383 log_error ("serial number longer than expected\n");
385 strcpy (serialno
, info
.serialno
);
388 fputs ("openpgp-card:\n", fp
);
393 fprintf (fp
, "version:%.4s:\n", info
.serialno
+12);
394 uval
= xtoi_2(info
.serialno
+16)*256 + xtoi_2 (info
.serialno
+18);
395 fprintf (fp
, "vendor:%04x:%s:\n", uval
, get_manufacturer (uval
));
396 fprintf (fp
, "serial:%.8s:\n", info
.serialno
+20);
398 print_isoname (fp
, "Name of cardholder: ", "name", info
.disp_name
);
402 print_string (fp
, info
.disp_lang
, strlen (info
.disp_lang
), ':');
405 fprintf (fp
, "sex:%c:\n", (info
.disp_sex
== 1? 'm':
406 info
.disp_sex
== 2? 'f' : 'u'));
410 print_string (fp
, info
.pubkey_url
, strlen (info
.pubkey_url
), ':');
413 fputs ("login:", fp
);
415 print_string (fp
, info
.login_data
, strlen (info
.login_data
), ':');
418 fprintf (fp
, "forcepin:%d:::\n", !info
.chv1_cached
);
419 fprintf (fp
, "maxpinlen:%d:%d:%d:\n",
420 info
.chvmaxlen
[0], info
.chvmaxlen
[1], info
.chvmaxlen
[2]);
421 fprintf (fp
, "pinretry:%d:%d:%d:\n",
422 info
.chvretry
[0], info
.chvretry
[1], info
.chvretry
[2]);
423 fprintf (fp
, "sigcount:%lu:::\n", info
.sig_counter
);
425 for (i
=0; i
< 4; i
++)
427 if (info
.private_do
[i
])
429 fprintf (fp
, "private_do:%d:", i
+1);
430 print_string (fp
, info
.private_do
[i
],
431 strlen (info
.private_do
[i
]), ':');
436 fputs ("cafpr:", fp
);
437 print_sha1_fpr_colon (fp
, info
.cafpr1valid
? info
.cafpr1
:NULL
);
438 print_sha1_fpr_colon (fp
, info
.cafpr2valid
? info
.cafpr2
:NULL
);
439 print_sha1_fpr_colon (fp
, info
.cafpr3valid
? info
.cafpr3
:NULL
);
442 print_sha1_fpr_colon (fp
, info
.fpr1valid
? info
.fpr1
:NULL
);
443 print_sha1_fpr_colon (fp
, info
.fpr2valid
? info
.fpr2
:NULL
);
444 print_sha1_fpr_colon (fp
, info
.fpr3valid
? info
.fpr3
:NULL
);
446 fprintf (fp
, "fprtime:%lu:%lu:%lu:\n",
447 (unsigned long)info
.fpr1time
, (unsigned long)info
.fpr2time
,
448 (unsigned long)info
.fpr3time
);
452 tty_fprintf (fp
, "Version ..........: %.1s%c.%.1s%c\n",
453 info
.serialno
[12] == '0'?"":info
.serialno
+12,
455 info
.serialno
[14] == '0'?"":info
.serialno
+14,
457 tty_fprintf (fp
, "Manufacturer .....: %s\n",
458 get_manufacturer (xtoi_2(info
.serialno
+16)*256
459 + xtoi_2 (info
.serialno
+18)));
460 tty_fprintf (fp
, "Serial number ....: %.8s\n", info
.serialno
+20);
462 print_isoname (fp
, "Name of cardholder: ", "name", info
.disp_name
);
463 print_name (fp
, "Language prefs ...: ", info
.disp_lang
);
464 tty_fprintf (fp
, "Sex ..............: %s\n",
465 info
.disp_sex
== 1? _("male"):
466 info
.disp_sex
== 2? _("female") : _("unspecified"));
467 print_name (fp
, "URL of public key : ", info
.pubkey_url
);
468 print_name (fp
, "Login data .......: ", info
.login_data
);
469 if (info
.private_do
[0])
470 print_name (fp
, "Private DO 1 .....: ", info
.private_do
[0]);
471 if (info
.private_do
[1])
472 print_name (fp
, "Private DO 2 .....: ", info
.private_do
[1]);
473 if (info
.private_do
[2])
474 print_name (fp
, "Private DO 3 .....: ", info
.private_do
[2]);
475 if (info
.private_do
[3])
476 print_name (fp
, "Private DO 4 .....: ", info
.private_do
[3]);
477 if (info
.cafpr1valid
)
479 tty_fprintf (fp
, "CA fingerprint %d .:", 1);
480 print_sha1_fpr (fp
, info
.cafpr1
);
482 if (info
.cafpr2valid
)
484 tty_fprintf (fp
, "CA fingerprint %d .:", 2);
485 print_sha1_fpr (fp
, info
.cafpr2
);
487 if (info
.cafpr3valid
)
489 tty_fprintf (fp
, "CA fingerprint %d .:", 3);
490 print_sha1_fpr (fp
, info
.cafpr3
);
492 tty_fprintf (fp
, "Signature PIN ....: %s\n",
493 info
.chv1_cached
? _("not forced"): _("forced"));
494 tty_fprintf (fp
, "Max. PIN lengths .: %d %d %d\n",
495 info
.chvmaxlen
[0], info
.chvmaxlen
[1], info
.chvmaxlen
[2]);
496 tty_fprintf (fp
, "PIN retry counter : %d %d %d\n",
497 info
.chvretry
[0], info
.chvretry
[1], info
.chvretry
[2]);
498 tty_fprintf (fp
, "Signature counter : %lu\n", info
.sig_counter
);
499 tty_fprintf (fp
, "Signature key ....:");
500 print_sha1_fpr (fp
, info
.fpr1valid
? info
.fpr1
:NULL
);
501 if (info
.fpr1valid
&& info
.fpr1time
)
502 tty_fprintf (fp
, " created ....: %s\n",
503 isotimestamp (info
.fpr1time
));
504 tty_fprintf (fp
, "Encryption key....:");
505 print_sha1_fpr (fp
, info
.fpr2valid
? info
.fpr2
:NULL
);
506 if (info
.fpr2valid
&& info
.fpr2time
)
507 tty_fprintf (fp
, " created ....: %s\n",
508 isotimestamp (info
.fpr2time
));
509 tty_fprintf (fp
, "Authentication key:");
510 print_sha1_fpr (fp
, info
.fpr3valid
? info
.fpr3
:NULL
);
511 if (info
.fpr3valid
&& info
.fpr3time
)
512 tty_fprintf (fp
, " created ....: %s\n",
513 isotimestamp (info
.fpr3time
));
514 tty_fprintf (fp
, "General key info..: ");
516 thefpr
= (info
.fpr1valid
? info
.fpr1
: info
.fpr2valid
? info
.fpr2
:
517 info
.fpr3valid
? info
.fpr3
: NULL
);
518 /* If the fingerprint is all 0xff, the key has no asssociated
519 OpenPGP certificate. */
520 if ( thefpr
&& !fpr_is_ff (thefpr
)
521 && !get_pubkey_byfprint (pk
, thefpr
, 20))
523 KBNODE keyblock
= NULL
;
525 print_pubkey_info (fp
, pk
);
527 if ( !get_seckeyblock_byfprint (&keyblock
, thefpr
, 20) )
528 print_card_key_info (fp
, keyblock
);
529 else if ( !get_keyblock_byfprint (&keyblock
, thefpr
, 20) )
531 release_kbnode (keyblock
);
534 if (!auto_create_card_key_stub (info
.serialno
,
535 info
.fpr1valid
? info
.fpr1
:NULL
,
536 info
.fpr2valid
? info
.fpr2
:NULL
,
537 info
.fpr3valid
? info
.fpr3
:NULL
))
539 if ( !get_seckeyblock_byfprint (&keyblock
, thefpr
, 20) )
540 print_card_key_info (fp
, keyblock
);
544 release_kbnode (keyblock
);
547 tty_fprintf (fp
, "[none]\n");
550 free_public_key (pk
);
551 agent_release_card_info (&info
);
556 get_one_name (const char *prompt1
, const char *prompt2
)
563 name
= cpr_get (prompt1
, prompt2
);
568 for (i
=0; name
[i
] && name
[i
] >= ' ' && name
[i
] <= 126; i
++)
571 /* The name must be in Latin-1 and not UTF-8 - lacking the code
572 to ensure this we restrict it to ASCII. */
574 tty_printf (_("Error: Only plain ASCII is currently allowed.\n"));
575 else if (strchr (name
, '<'))
576 tty_printf (_("Error: The \"<\" character may not be used.\n"));
577 else if (strstr (name
, " "))
578 tty_printf (_("Error: Double spaces are not allowed.\n"));
590 char *surname
= NULL
, *givenname
= NULL
;
594 surname
= get_one_name ("keygen.smartcard.surname",
595 _("Cardholder's surname: "));
596 givenname
= get_one_name ("keygen.smartcard.givenname",
597 _("Cardholder's given name: "));
598 if (!surname
|| !givenname
|| (!*surname
&& !*givenname
))
602 return -1; /*canceled*/
605 isoname
= xmalloc ( strlen (surname
) + 2 + strlen (givenname
) + 1);
606 strcpy (stpcpy (stpcpy (isoname
, surname
), "<<"), givenname
);
609 for (p
=isoname
; *p
; p
++)
613 if (strlen (isoname
) > 39 )
615 tty_printf (_("Error: Combined name too long "
616 "(limit is %d characters).\n"), 39);
621 rc
= agent_scd_setattr ("DISP-NAME", isoname
, strlen (isoname
), NULL
);
623 log_error ("error setting Name: %s\n", gpg_strerror (rc
));
636 url
= cpr_get ("cardedit.change_url", _("URL to retrieve public key: "));
642 if (strlen (url
) > 254 )
644 tty_printf (_("Error: URL too long "
645 "(limit is %d characters).\n"), 254);
650 rc
= agent_scd_setattr ("PUBKEY-URL", url
, strlen (url
), NULL
);
652 log_error ("error setting URL: %s\n", gpg_strerror (rc
));
658 /* Fetch the key from the URL given on the card or try to get it from
659 the default keyserver. */
663 #if GNUPG_MAJOR_VERSION == 1
665 struct agent_card_info_s info
;
667 memset(&info
,0,sizeof(info
));
669 rc
=agent_scd_getattr("PUBKEY-URL",&info
);
671 log_error("error retrieving URL from card: %s\n",gpg_strerror(rc
));
674 struct keyserver_spec
*spec
=NULL
;
676 rc
=agent_scd_getattr("KEY-FPR",&info
);
678 log_error("error retrieving key fingerprint from card: %s\n",
680 else if (info
.pubkey_url
&& *info
.pubkey_url
)
682 spec
=parse_keyserver_uri(info
.pubkey_url
,1,NULL
,0);
683 if(spec
&& info
.fpr1valid
)
685 /* This is not perfectly right. Currently, all card
686 fingerprints are 20 digits, but what about
687 fingerprints for a future v5 key? We should get the
688 length from somewhere lower in the code. In any
689 event, the fpr/keyid is not meaningful for straight
690 HTTP fetches, but using it allows the card to point
691 to HKP and LDAP servers as well. */
692 rc
=keyserver_import_fprint(info
.fpr1
,20,spec
);
693 free_keyserver_spec(spec
);
696 else if (info
.fpr1valid
)
698 rc
= keyserver_import_fprint (info
.fpr1
, 20, opt
.keyserver
);
709 /* Read data from file FNAME up to MAXLEN characters. On error return
710 -1 and store NULl at R_BUFFER; on success return the number of
711 bytes read and store the address of a newly allocated buffer at
714 get_data_from_file (const char *fname
, size_t maxlen
, char **r_buffer
)
722 fp
= fopen (fname
, "rb");
723 #if GNUPG_MAJOR_VERSION == 1
724 if (fp
&& is_secured_file (fileno (fp
)))
733 tty_printf (_("can't open `%s': %s\n"), fname
, strerror (errno
));
737 data
= xtrymalloc (maxlen
? maxlen
:1);
740 tty_printf (_("error allocating enough memory: %s\n"), strerror (errno
));
746 n
= fread (data
, 1, maxlen
, fp
);
752 tty_printf (_("error reading `%s': %s\n"), fname
, strerror (errno
));
762 change_login (const char *args
)
768 if (args
&& *args
== '<') /* Read it from a file */
770 for (args
++; spacep (args
); args
++)
772 n
= get_data_from_file (args
, 254, &data
);
778 data
= cpr_get ("cardedit.change_login",
779 _("Login data (account name): "));
789 tty_printf (_("Error: Login data too long "
790 "(limit is %d characters).\n"), 254);
795 rc
= agent_scd_setattr ("LOGIN-DATA", data
, n
, NULL
);
797 log_error ("error setting login data: %s\n", gpg_strerror (rc
));
803 change_private_do (const char *args
, int nr
)
805 char do_name
[] = "PRIVATE-DO-X";
810 assert (nr
>= 1 && nr
<= 4);
811 do_name
[11] = '0' + nr
;
813 if (args
&& (args
= strchr (args
, '<'))) /* Read it from a file */
815 for (args
++; spacep (args
); args
++)
817 n
= get_data_from_file (args
, 254, &data
);
823 data
= cpr_get ("cardedit.change_private_do",
824 _("Private DO data: "));
834 tty_printf (_("Error: Private DO too long "
835 "(limit is %d characters).\n"), 254);
840 rc
= agent_scd_setattr (do_name
, data
, n
, NULL
);
842 log_error ("error setting private DO: %s\n", gpg_strerror (rc
));
849 change_cert (const char *args
)
855 if (args
&& *args
== '<') /* Read it from a file */
857 for (args
++; spacep (args
); args
++)
859 n
= get_data_from_file (args
, 16384, &data
);
865 tty_printf ("usage error: redirectrion to file required\n");
869 rc
= agent_scd_writecert ("OPENPGP.3", data
, n
);
871 log_error ("error writing certificate to card: %s\n", gpg_strerror (rc
));
883 data
= cpr_get ("cardedit.change_lang",
884 _("Language preferences: "));
890 if (strlen (data
) > 8 || (strlen (data
) & 1))
892 tty_printf (_("Error: invalid length of preference string.\n"));
897 for (p
=data
; *p
&& *p
>= 'a' && *p
<= 'z'; p
++)
901 tty_printf (_("Error: invalid characters in preference string.\n"));
906 rc
= agent_scd_setattr ("DISP-LANG", data
, strlen (data
), NULL
);
908 log_error ("error setting lang: %s\n", gpg_strerror (rc
));
921 data
= cpr_get ("cardedit.change_sex",
922 _("Sex ((M)ale, (F)emale or space): "));
930 else if ((*data
== 'M' || *data
== 'm') && !data
[1])
932 else if ((*data
== 'F' || *data
== 'f') && !data
[1])
936 tty_printf (_("Error: invalid response.\n"));
941 rc
= agent_scd_setattr ("DISP-SEX", str
, 1, NULL
);
943 log_error ("error setting sex: %s\n", gpg_strerror (rc
));
950 change_cafpr (int fprno
)
955 unsigned char fpr
[20];
957 data
= cpr_get ("cardedit.change_cafpr", _("CA fingerprint: "));
963 for (i
=0, s
=data
; i
< 20 && *s
; )
980 tty_printf (_("Error: invalid formatted fingerprint.\n"));
984 rc
= agent_scd_setattr (fprno
==1?"CA-FPR-1":
986 fprno
==3?"CA-FPR-3":"x", fpr
, 20, NULL
);
988 log_error ("error setting cafpr: %s\n", gpg_strerror (rc
));
995 toggle_forcesig (void)
997 struct agent_card_info_s info
;
1001 memset (&info
, 0, sizeof info
);
1002 rc
= agent_scd_getattr ("CHV-STATUS", &info
);
1005 log_error ("error getting current status: %s\n", gpg_strerror (rc
));
1008 newstate
= !info
.chv1_cached
;
1009 agent_release_card_info (&info
);
1011 rc
= agent_scd_setattr ("CHV-STATUS-1", newstate
? "\x01":"", 1, NULL
);
1013 log_error ("error toggling signature PIN flag: %s\n", gpg_strerror (rc
));
1017 /* Helper for the key generation/edit functions. */
1019 get_info_for_key_operation (struct agent_card_info_s
*info
)
1023 memset (info
, 0, sizeof *info
);
1024 rc
= agent_scd_getattr ("SERIALNO", info
);
1025 if (rc
|| !info
->serialno
|| strncmp (info
->serialno
, "D27600012401", 12)
1026 || strlen (info
->serialno
) != 32 )
1028 log_error (_("key operation not possible: %s\n"),
1029 rc
? gpg_strerror (rc
) : _("not an OpenPGP card"));
1032 rc
= agent_scd_getattr ("KEY-FPR", info
);
1034 rc
= agent_scd_getattr ("CHV-STATUS", info
);
1036 rc
= agent_scd_getattr ("DISP-NAME", info
);
1038 log_error (_("error getting current key info: %s\n"), gpg_strerror (rc
));
1043 /* Helper for the key generation/edit functions. */
1045 check_pin_for_key_operation (struct agent_card_info_s
*info
, int *forced_chv1
)
1049 agent_clear_pin_cache (info
->serialno
);
1051 *forced_chv1
= !info
->chv1_cached
;
1053 { /* Switch of the forced mode so that during key generation we
1054 don't get bothered with PIN queries for each
1056 rc
= agent_scd_setattr ("CHV-STATUS-1", "\x01", 1, info
->serialno
);
1059 log_error ("error clearing forced signature PIN flag: %s\n",
1067 /* Check the PIN now, so that we won't get asked later for each
1068 binding signature. */
1069 rc
= agent_scd_checkpin (info
->serialno
);
1071 log_error ("error checking the PIN: %s\n", gpg_strerror (rc
));
1076 /* Helper for the key generation/edit functions. */
1078 restore_forced_chv1 (int *forced_chv1
)
1083 { /* Switch back to forced state. */
1084 rc
= agent_scd_setattr ("CHV-STATUS-1", "", 1, NULL
);
1087 log_error ("error setting forced signature PIN flag: %s\n",
1094 /* Helper for the key generation/edit functions. */
1096 show_card_key_info (struct agent_card_info_s
*info
)
1098 tty_fprintf (NULL
, "Signature key ....:");
1099 print_sha1_fpr (NULL
, info
->fpr1valid
? info
->fpr1
:NULL
);
1100 tty_fprintf (NULL
, "Encryption key....:");
1101 print_sha1_fpr (NULL
, info
->fpr2valid
? info
->fpr2
:NULL
);
1102 tty_fprintf (NULL
, "Authentication key:");
1103 print_sha1_fpr (NULL
, info
->fpr3valid
? info
->fpr3
:NULL
);
1108 /* Helper for the key generation/edit functions. */
1110 replace_existing_key_p (struct agent_card_info_s
*info
, int keyno
)
1112 assert (keyno
>= 0 && keyno
<= 3);
1114 if ((keyno
== 1 && info
->fpr1valid
)
1115 || (keyno
== 2 && info
->fpr2valid
)
1116 || (keyno
== 3 && info
->fpr3valid
))
1119 log_info ("WARNING: such a key has already been stored on the card!\n");
1121 if ( !cpr_get_answer_is_yes( "cardedit.genkeys.replace_key",
1122 _("Replace existing key? (y/N) ")))
1130 generate_card_keys (void)
1132 struct agent_card_info_s info
;
1136 if (get_info_for_key_operation (&info
))
1139 #if GNUPG_MAJOR_VERSION == 1
1141 char *answer
=cpr_get("cardedit.genkeys.backup_enc",
1142 _("Make off-card backup of encryption key? (Y/n) "));
1144 want_backup
=answer_is_yes_no_default(answer
,1);
1149 want_backup
= cpr_get_answer_is_yes
1150 ( "cardedit.genkeys.backup_enc",
1151 _("Make off-card backup of encryption key? (Y/n) "));
1152 /*FIXME: we need answer_is_yes_no_default()*/
1155 if ( (info
.fpr1valid
&& !fpr_is_zero (info
.fpr1
))
1156 || (info
.fpr2valid
&& !fpr_is_zero (info
.fpr2
))
1157 || (info
.fpr3valid
&& !fpr_is_zero (info
.fpr3
)))
1160 log_info ("NOTE: keys are already stored on the card!\n");
1162 if ( !cpr_get_answer_is_yes( "cardedit.genkeys.replace_keys",
1163 _("Replace existing keys? (y/N) ")))
1165 agent_release_card_info (&info
);
1169 else if (!info
.disp_name
|| !*info
.disp_name
)
1172 tty_printf (_("Please note that the factory settings of the PINs are\n"
1173 " PIN = `%s' Admin PIN = `%s'\n"
1174 "You should change them using the command --change-pin\n"),
1175 "123456", "12345678");
1179 if (check_pin_for_key_operation (&info
, &forced_chv1
))
1182 generate_keypair (NULL
, info
.serialno
,
1183 want_backup
? opt
.homedir
:NULL
);
1186 agent_release_card_info (&info
);
1187 restore_forced_chv1 (&forced_chv1
);
1191 /* This function is used by the key edit menu to generate an arbitrary
1194 card_generate_subkey (KBNODE pub_keyblock
, KBNODE sec_keyblock
)
1196 struct agent_card_info_s info
;
1198 int forced_chv1
= 0;
1201 if (get_info_for_key_operation (&info
))
1204 show_card_key_info (&info
);
1206 tty_printf (_("Please select the type of key to generate:\n"));
1208 tty_printf (_(" (1) Signature key\n"));
1209 tty_printf (_(" (2) Encryption key\n"));
1210 tty_printf (_(" (3) Authentication key\n"));
1214 char *answer
= cpr_get ("cardedit.genkeys.subkeytype",
1215 _("Your selection? "));
1217 if (*answer
== CONTROL_D
)
1222 keyno
= *answer
? atoi(answer
): 0;
1224 if (keyno
>= 1 && keyno
<= 3)
1226 tty_printf(_("Invalid selection.\n"));
1229 if (replace_existing_key_p (&info
, keyno
))
1232 if (check_pin_for_key_operation (&info
, &forced_chv1
))
1235 okay
= generate_card_subkeypair (pub_keyblock
, sec_keyblock
,
1236 keyno
, info
.serialno
);
1239 agent_release_card_info (&info
);
1240 restore_forced_chv1 (&forced_chv1
);
1245 /* Store the key at NODE into the smartcard and modify NODE to
1246 carry the serialno stuff instead of the actual secret key
1247 parameters. USE is the usage for that key; 0 means any
1250 card_store_subkey (KBNODE node
, int use
)
1252 struct agent_card_info_s info
;
1256 PKT_secret_key
*copied_sk
= NULL
;
1262 assert (node
->pkt
->pkttype
== PKT_SECRET_KEY
1263 || node
->pkt
->pkttype
== PKT_SECRET_SUBKEY
);
1264 sk
= node
->pkt
->pkt
.secret_key
;
1266 if (get_info_for_key_operation (&info
))
1269 show_card_key_info (&info
);
1271 if (!is_RSA (sk
->pubkey_algo
) || nbits_from_sk (sk
) != 1024 )
1273 tty_printf ("You may only store a 1024 bit RSA key on the card\n");
1278 allow_keyno
[0] = (!use
|| (use
& (PUBKEY_USAGE_SIG
)));
1279 allow_keyno
[1] = (!use
|| (use
& (PUBKEY_USAGE_ENC
)));
1280 allow_keyno
[2] = (!use
|| (use
& (PUBKEY_USAGE_SIG
|PUBKEY_USAGE_AUTH
)));
1282 tty_printf (_("Please select where to store the key:\n"));
1285 tty_printf (_(" (1) Signature key\n"));
1287 tty_printf (_(" (2) Encryption key\n"));
1289 tty_printf (_(" (3) Authentication key\n"));
1293 char *answer
= cpr_get ("cardedit.genkeys.storekeytype",
1294 _("Your selection? "));
1296 if (*answer
== CONTROL_D
|| !*answer
)
1301 keyno
= *answer
? atoi(answer
): 0;
1303 if (keyno
>= 1 && keyno
<= 3 && allow_keyno
[keyno
-1])
1305 tty_printf(_("Invalid selection.\n"));
1308 if (replace_existing_key_p (&info
, keyno
))
1311 /* Unprotect key. */
1312 switch (is_secret_key_protected (sk
) )
1314 case 0: /* Not protected. */
1317 log_error (_("unknown key protection algorithm\n"));
1320 if (sk
->protect
.s2k
.mode
== 1001)
1322 log_error (_("secret parts of key are not available\n"));
1325 if (sk
->protect
.s2k
.mode
== 1002)
1327 log_error (_("secret key already stored on a card\n"));
1330 /* We better copy the key before we unprotect it. */
1331 copied_sk
= sk
= copy_secret_key (NULL
, sk
);
1332 rc
= check_secret_key (sk
, 0);
1337 rc
= save_unprotected_key_to_card (sk
, keyno
);
1341 /* Get back to the maybe protected original secret key. */
1344 free_secret_key (copied_sk
);
1347 sk
= node
->pkt
->pkt
.secret_key
;
1349 /* Get rid of the secret key parameters and store the serial numer. */
1350 n
= pubkey_get_nskey (sk
->pubkey_algo
);
1351 for (i
=pubkey_get_npkey (sk
->pubkey_algo
); i
< n
; i
++)
1353 gcry_mpi_release (sk
->skey
[i
]);
1356 i
= pubkey_get_npkey (sk
->pubkey_algo
);
1357 sk
->skey
[i
] = gcry_mpi_set_opaque (NULL
, xstrdup ("dummydata"), 10*8);
1358 sk
->is_protected
= 1;
1359 sk
->protect
.s2k
.mode
= 1002;
1361 for (sk
->protect
.ivlen
=0; sk
->protect
.ivlen
< 16 && *s
&& s
[1];
1362 sk
->protect
.ivlen
++, s
+= 2)
1363 sk
->protect
.iv
[sk
->protect
.ivlen
] = xtoi_2 (s
);
1369 free_secret_key (copied_sk
);
1370 agent_release_card_info (&info
);
1376 /* Data used by the command parser. This needs to be outside of the
1377 function scope to allow readline based command completion. */
1381 cmdQUIT
, cmdADMIN
, cmdHELP
, cmdLIST
, cmdDEBUG
, cmdVERIFY
,
1382 cmdNAME
, cmdURL
, cmdFETCH
, cmdLOGIN
, cmdLANG
, cmdSEX
, cmdCAFPR
,
1383 cmdFORCESIG
, cmdGENERATE
, cmdPASSWD
, cmdPRIVATEDO
, cmdWRITECERT
,
1396 { "quit" , cmdQUIT
, 0, N_("quit this menu")},
1397 { "q" , cmdQUIT
, 0, NULL
},
1398 { "admin" , cmdADMIN
, 0, N_("show admin commands")},
1399 { "help" , cmdHELP
, 0, N_("show this help")},
1400 { "?" , cmdHELP
, 0, NULL
},
1401 { "list" , cmdLIST
, 0, N_("list all available data")},
1402 { "l" , cmdLIST
, 0, NULL
},
1403 { "debug" , cmdDEBUG
, 0, NULL
},
1404 { "name" , cmdNAME
, 1, N_("change card holder's name")},
1405 { "url" , cmdURL
, 1, N_("change URL to retrieve key")},
1406 { "fetch" , cmdFETCH
, 0, N_("fetch the key specified in the card URL")},
1407 { "login" , cmdLOGIN
, 1, N_("change the login name")},
1408 { "lang" , cmdLANG
, 1, N_("change the language preferences")},
1409 { "sex" , cmdSEX
, 1, N_("change card holder's sex")},
1410 { "cafpr" , cmdCAFPR
, 1, N_("change a CA fingerprint")},
1411 { "forcesig", cmdFORCESIG
, 1, N_("toggle the signature force PIN flag")},
1412 { "generate", cmdGENERATE
, 1, N_("generate new keys")},
1413 { "passwd" , cmdPASSWD
, 0, N_("menu to change or unblock the PIN")},
1414 { "verify" , cmdVERIFY
, 0, N_("verify the PIN and list all data")},
1415 { "unblock" , cmdUNBLOCK
,0, N_("unblock the PIN using a Reset Code") },
1416 /* Note, that we do not announce these command yet. */
1417 { "privatedo", cmdPRIVATEDO
, 0, NULL
},
1418 { "writecert", cmdWRITECERT
, 1, NULL
},
1419 { NULL
, cmdINVCMD
, 0, NULL
}
1423 #if GNUPG_MAJOR_VERSION == 1 && defined (HAVE_LIBREADLINE)
1425 /* These two functions are used by readline for command completion. */
1428 command_generator(const char *text
,int state
)
1430 static int list_index
,len
;
1433 /* If this is a new word to complete, initialize now. This includes
1434 saving the length of TEXT for efficiency, and initializing the
1435 index variable to 0. */
1442 /* Return the next partial match */
1443 while((name
=cmds
[list_index
].name
))
1445 /* Only complete commands that have help text */
1446 if(cmds
[list_index
++].desc
&& strncmp(name
,text
,len
)==0)
1447 return strdup(name
);
1454 card_edit_completion(const char *text
, int start
, int end
)
1456 /* If we are at the start of a line, we try and command-complete.
1457 If not, just do nothing for now. */
1460 return rl_completion_matches(text
,command_generator
);
1462 rl_attempted_completion_over
=1;
1466 #endif /* GNUPG_MAJOR_VERSION == 1 && HAVE_LIBREADLINE */
1468 /* Menu to edit all user changeable values on an OpenPGP card. Only
1469 Key creation is not handled here. */
1471 card_edit (strlist_t commands
)
1473 enum cmdids cmd
= cmdNOP
;
1474 int have_commands
= !!commands
;
1476 char *answer
= NULL
;
1477 int did_checkpin
= 0, allow_admin
=0;
1478 char serialnobuf
[50];
1481 if (opt
.command_fd
!= -1)
1483 else if (opt
.batch
&& !have_commands
)
1485 log_error(_("can't do this in batch mode\n"));
1492 const char *arg_string
= "";
1493 const char *arg_rest
= "";
1501 if (opt
.with_colons
)
1503 card_status (stdout
, serialnobuf
, DIM (serialnobuf
));
1508 card_status (NULL
, serialnobuf
, DIM (serialnobuf
));
1521 answer
= xstrdup (commands
->d
);
1522 commands
= commands
->next
;
1526 answer
= xstrdup ("quit");
1534 #if GNUPG_MAJOR_VERSION == 1
1535 tty_enable_completion (card_edit_completion
);
1537 answer
= cpr_get_no_help("cardedit.prompt", _("Command> "));
1539 #if GNUPG_MAJOR_VERSION == 1
1540 tty_disable_completion ();
1543 trim_spaces(answer
);
1545 while ( *answer
== '#' );
1547 arg_number
= 0; /* Yes, here is the init which egcc complains about */
1550 cmd
= cmdLIST
; /* Default to the list command */
1551 else if (*answer
== CONTROL_D
)
1555 if ((p
=strchr (answer
,' ')))
1558 trim_spaces (answer
);
1560 arg_number
= atoi(p
);
1563 while (digitp (arg_rest
))
1565 while (spacep (arg_rest
))
1569 for (i
=0; cmds
[i
].name
; i
++ )
1570 if (!ascii_strcasecmp (answer
, cmds
[i
].name
))
1574 cmd_admin_only
= cmds
[i
].admin_only
;
1577 if (!allow_admin
&& cmd_admin_only
)
1580 tty_printf (_("Admin-only command\n"));
1587 for (i
=0; cmds
[i
].name
; i
++ )
1589 && (!cmds
[i
].admin_only
|| (cmds
[i
].admin_only
&& allow_admin
)))
1590 tty_printf("%-10s %s\n", cmds
[i
].name
, _(cmds
[i
].desc
) );
1594 if ( !strcmp (arg_string
, "on") )
1596 else if ( !strcmp (arg_string
, "off") )
1598 else if ( !strcmp (arg_string
, "verify") )
1600 /* Force verification of the Admin Command. However,
1601 this is only done if the retry counter is at initial
1603 char *tmp
= xmalloc (strlen (serialnobuf
) + 6 + 1);
1604 strcpy (stpcpy (tmp
, serialnobuf
), "[CHV3]");
1605 allow_admin
= !agent_scd_checkpin (tmp
);
1609 allow_admin
=!allow_admin
;
1611 tty_printf(_("Admin commands are allowed\n"));
1613 tty_printf(_("Admin commands are not allowed\n"));
1617 agent_scd_checkpin (serialnobuf
);
1638 change_login (arg_string
);
1650 if ( arg_number
< 1 || arg_number
> 3 )
1651 tty_printf ("usage: cafpr N\n"
1654 change_cafpr (arg_number
);
1658 if ( arg_number
< 1 || arg_number
> 4 )
1659 tty_printf ("usage: privatedo N\n"
1662 change_private_do (arg_string
, arg_number
);
1666 if ( arg_number
!= 3 )
1667 tty_printf ("usage: writecert 3 < FILE\n");
1669 change_cert (arg_rest
);
1677 generate_card_keys ();
1681 change_pin (0, allow_admin
);
1682 did_checkpin
= 0; /* Need to reset it of course. */
1686 change_pin (1, allow_admin
);
1687 did_checkpin
= 0; /* Need to reset it of course. */
1699 tty_printf (_("Invalid command (try \"help\")\n"));
1701 } /* End command switch. */
1702 } /* End of main menu loop. */