1 /* card-util.c - Utility functions for the OpenPGP card.
2 * Copyright (C) 2003, 2004, 2005, 2009 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)
52 write_sc_op_status (gpg_error_t err
)
54 switch (gpg_err_code (err
))
57 write_status (STATUS_SC_OP_SUCCESS
);
59 #if GNUPG_MAJOR_VERSION != 1
60 case GPG_ERR_CANCELED
:
61 write_status_text (STATUS_SC_OP_FAILURE
, "1");
64 write_status_text (STATUS_SC_OP_FAILURE
, "2");
67 write_status (STATUS_SC_OP_FAILURE
);
69 #endif /* GNUPG_MAJOR_VERSION != 1 */
74 /* Change the PIN of a an OpenPGP card. This is an interactive
77 change_pin (int unblock_v2
, int allow_admin
)
79 struct agent_card_info_s info
;
82 rc
= agent_learn (&info
);
85 log_error (_("OpenPGP card not available: %s\n"),
90 log_info (_("OpenPGP card no. %s detected\n"),
91 info
.serialno
? info
.serialno
: "[none]");
93 agent_clear_pin_cache (info
.serialno
);
97 agent_release_card_info (&info
);
98 log_error (_("can't do this in batch mode\n"));
106 log_error (_("This command is only available for version 2 cards\n"));
107 else if (!info
.chvretry
[1])
108 log_error (_("Reset Code not or not anymore available\n"));
111 rc
= agent_scd_change_pin (2, info
.serialno
);
112 write_sc_op_status (rc
);
114 tty_printf ("Error changing the PIN: %s\n", gpg_strerror (rc
));
116 tty_printf ("PIN changed.\n");
119 else if (!allow_admin
)
121 rc
= agent_scd_change_pin (1, info
.serialno
);
122 write_sc_op_status (rc
);
124 tty_printf ("Error changing the PIN: %s\n", gpg_strerror (rc
));
126 tty_printf ("PIN changed.\n");
134 tty_printf ("1 - change PIN\n"
136 "3 - change Admin PIN\n"
137 "4 - set the Reset Code\n"
141 answer
= cpr_get("cardutil.change_pin.menu",_("Your selection? "));
143 if (strlen (answer
) != 1)
150 rc
= agent_scd_change_pin (1, info
.serialno
);
151 write_sc_op_status (rc
);
153 tty_printf ("Error changing the PIN: %s\n", gpg_strerror (rc
));
155 tty_printf ("PIN changed.\n");
157 else if (*answer
== '2')
160 rc
= agent_scd_change_pin (101, info
.serialno
);
161 write_sc_op_status (rc
);
163 tty_printf ("Error unblocking the PIN: %s\n", gpg_strerror (rc
));
165 tty_printf ("PIN unblocked and new PIN set.\n");
167 else if (*answer
== '3')
169 /* Change Admin PIN. */
170 rc
= agent_scd_change_pin (3, info
.serialno
);
171 write_sc_op_status (rc
);
173 tty_printf ("Error changing the PIN: %s\n", gpg_strerror (rc
));
175 tty_printf ("PIN changed.\n");
177 else if (*answer
== '4')
179 /* Set a new Reset Code. */
180 rc
= agent_scd_change_pin (102, info
.serialno
);
181 write_sc_op_status (rc
);
183 tty_printf ("Error setting the Reset Code: %s\n",
186 tty_printf ("Reset Code set.\n");
188 else if (*answer
== 'q' || *answer
== 'Q')
194 agent_release_card_info (&info
);
198 get_manufacturer (unsigned int no
)
200 /* Note: Make sure that there is no colon or linefeed in the string. */
203 case 0x0001: return "PPC Card Systems";
204 case 0x0002: return "Prism";
205 case 0x0003: return "OpenFortress";
206 case 0x0004: return "Wewid AB";
207 case 0x0005: return "ZeitControl";
209 case 0x002A: return "Magrathea";
210 /* 0x00000 and 0xFFFF are defined as test cards per spec,
211 0xFFF00 to 0xFFFE are assigned for use with randomly created
214 case 0xffff: return "test card";
215 default: return (no
& 0xff00) == 0xff00? "unmanaged S/N range":"unknown";
221 print_sha1_fpr (FILE *fp
, const unsigned char *fpr
)
227 for (i
=0; i
< 20 ; i
+=2, fpr
+= 2 )
230 tty_fprintf (fp
, " ");
231 tty_fprintf (fp
, " %02X%02X", *fpr
, fpr
[1]);
235 tty_fprintf (fp
, " [none]");
236 tty_fprintf (fp
, "\n");
241 print_sha1_fpr_colon (FILE *fp
, const unsigned char *fpr
)
247 for (i
=0; i
< 20 ; i
++, fpr
++)
248 fprintf (fp
, "%02X", *fpr
);
255 print_name (FILE *fp
, const char *text
, const char *name
)
257 tty_fprintf (fp
, "%s", text
);
259 /* FIXME: tty_printf_utf8_string2 eats everything after and
260 including an @ - e.g. when printing an url. */
264 print_utf8_string2 (fp
, name
, strlen (name
), '\n');
266 tty_print_utf8_string2 (name
, strlen (name
), 0);
269 tty_fprintf (fp
, _("[not set]"));
270 tty_fprintf (fp
, "\n");
274 print_isoname (FILE *fp
, const char *text
, const char *tag
, const char *name
)
277 fprintf (fp
, "%s:", tag
);
279 tty_fprintf (fp
, "%s", text
);
283 char *p
, *given
, *buf
= xstrdup (name
);
285 given
= strstr (buf
, "<<");
289 if (given
&& given
[2])
294 print_string (fp
, given
, strlen (given
), ':');
296 print_utf8_string2 (fp
, given
, strlen (given
), '\n');
298 tty_print_utf8_string2 (given
, strlen (given
), 0);
303 tty_fprintf (fp
, " ");
307 print_string (fp
, buf
, strlen (buf
), ':');
309 print_utf8_string2 (fp
, buf
, strlen (buf
), '\n');
311 tty_print_utf8_string2 (buf
, strlen (buf
), 0);
319 tty_fprintf (fp
, _("[not set]"));
325 tty_fprintf (fp
, "\n");
328 /* Return true if the SHA1 fingerprint FPR consists only of zeroes. */
330 fpr_is_zero (const char *fpr
)
334 for (i
=0; i
< 20 && !fpr
[i
]; i
++)
340 /* Return true if the SHA1 fingerprint FPR consists only of 0xFF. */
342 fpr_is_ff (const char *fpr
)
346 for (i
=0; i
< 20 && fpr
[i
] == '\xff'; i
++)
352 /* Print all available information about the current card. */
354 card_status (FILE *fp
, char *serialno
, size_t serialnobuflen
)
356 struct agent_card_info_s info
;
357 PKT_public_key
*pk
= xcalloc (1, sizeof *pk
);
360 const unsigned char *thefpr
;
363 if (serialno
&& serialnobuflen
)
366 rc
= agent_learn (&info
);
370 fputs ("AID:::\n", fp
);
371 log_error (_("OpenPGP card not available: %s\n"),
378 fprintf (fp
, "AID:%s:", info
.serialno
? info
.serialno
: "");
380 tty_fprintf (fp
, "Application ID ...: %s\n",
381 info
.serialno
? info
.serialno
: "[none]");
382 if (!info
.serialno
|| strncmp (info
.serialno
, "D27600012401", 12)
383 || strlen (info
.serialno
) != 32 )
385 if (info
.apptype
&& !strcmp (info
.apptype
, "NKS"))
388 fputs ("netkey-card:\n", fp
);
389 log_info ("this is a NetKey card\n");
391 else if (info
.apptype
&& !strcmp (info
.apptype
, "DINSIG"))
394 fputs ("dinsig-card:\n", fp
);
395 log_info ("this is a DINSIG compliant card\n");
397 else if (info
.apptype
&& !strcmp (info
.apptype
, "P15"))
400 fputs ("pkcs15-card:\n", fp
);
401 log_info ("this is a PKCS#15 compliant card\n");
403 else if (info
.apptype
&& !strcmp (info
.apptype
, "GELDKARTE"))
406 fputs ("geldkarte-card:\n", fp
);
407 log_info ("this is a Geldkarte compliant card\n");
412 fputs ("unknown:\n", fp
);
414 log_info ("not an OpenPGP card\n");
415 agent_release_card_info (&info
);
422 else if (strlen (serialno
)+1 > serialnobuflen
)
423 log_error ("serial number longer than expected\n");
425 strcpy (serialno
, info
.serialno
);
428 fputs ("openpgp-card:\n", fp
);
433 fprintf (fp
, "version:%.4s:\n", info
.serialno
+12);
434 uval
= xtoi_2(info
.serialno
+16)*256 + xtoi_2 (info
.serialno
+18);
435 fprintf (fp
, "vendor:%04x:%s:\n", uval
, get_manufacturer (uval
));
436 fprintf (fp
, "serial:%.8s:\n", info
.serialno
+20);
438 print_isoname (fp
, "Name of cardholder: ", "name", info
.disp_name
);
442 print_string (fp
, info
.disp_lang
, strlen (info
.disp_lang
), ':');
445 fprintf (fp
, "sex:%c:\n", (info
.disp_sex
== 1? 'm':
446 info
.disp_sex
== 2? 'f' : 'u'));
450 print_string (fp
, info
.pubkey_url
, strlen (info
.pubkey_url
), ':');
453 fputs ("login:", fp
);
455 print_string (fp
, info
.login_data
, strlen (info
.login_data
), ':');
458 fprintf (fp
, "forcepin:%d:::\n", !info
.chv1_cached
);
459 for (i
=0; i
< DIM (info
.key_attr
); i
++)
460 if (info
.key_attr
[0].algo
)
461 fprintf (fp
, "keyattr:%d:%d:%u:\n", i
+1,
462 info
.key_attr
[i
].algo
, info
.key_attr
[i
].nbits
);
463 fprintf (fp
, "maxpinlen:%d:%d:%d:\n",
464 info
.chvmaxlen
[0], info
.chvmaxlen
[1], info
.chvmaxlen
[2]);
465 fprintf (fp
, "pinretry:%d:%d:%d:\n",
466 info
.chvretry
[0], info
.chvretry
[1], info
.chvretry
[2]);
467 fprintf (fp
, "sigcount:%lu:::\n", info
.sig_counter
);
469 for (i
=0; i
< 4; i
++)
471 if (info
.private_do
[i
])
473 fprintf (fp
, "private_do:%d:", i
+1);
474 print_string (fp
, info
.private_do
[i
],
475 strlen (info
.private_do
[i
]), ':');
480 fputs ("cafpr:", fp
);
481 print_sha1_fpr_colon (fp
, info
.cafpr1valid
? info
.cafpr1
:NULL
);
482 print_sha1_fpr_colon (fp
, info
.cafpr2valid
? info
.cafpr2
:NULL
);
483 print_sha1_fpr_colon (fp
, info
.cafpr3valid
? info
.cafpr3
:NULL
);
486 print_sha1_fpr_colon (fp
, info
.fpr1valid
? info
.fpr1
:NULL
);
487 print_sha1_fpr_colon (fp
, info
.fpr2valid
? info
.fpr2
:NULL
);
488 print_sha1_fpr_colon (fp
, info
.fpr3valid
? info
.fpr3
:NULL
);
490 fprintf (fp
, "fprtime:%lu:%lu:%lu:\n",
491 (unsigned long)info
.fpr1time
, (unsigned long)info
.fpr2time
,
492 (unsigned long)info
.fpr3time
);
496 tty_fprintf (fp
, "Version ..........: %.1s%c.%.1s%c\n",
497 info
.serialno
[12] == '0'?"":info
.serialno
+12,
499 info
.serialno
[14] == '0'?"":info
.serialno
+14,
501 tty_fprintf (fp
, "Manufacturer .....: %s\n",
502 get_manufacturer (xtoi_2(info
.serialno
+16)*256
503 + xtoi_2 (info
.serialno
+18)));
504 tty_fprintf (fp
, "Serial number ....: %.8s\n", info
.serialno
+20);
506 print_isoname (fp
, "Name of cardholder: ", "name", info
.disp_name
);
507 print_name (fp
, "Language prefs ...: ", info
.disp_lang
);
508 tty_fprintf (fp
, "Sex ..............: %s\n",
509 info
.disp_sex
== 1? _("male"):
510 info
.disp_sex
== 2? _("female") : _("unspecified"));
511 print_name (fp
, "URL of public key : ", info
.pubkey_url
);
512 print_name (fp
, "Login data .......: ", info
.login_data
);
513 if (info
.private_do
[0])
514 print_name (fp
, "Private DO 1 .....: ", info
.private_do
[0]);
515 if (info
.private_do
[1])
516 print_name (fp
, "Private DO 2 .....: ", info
.private_do
[1]);
517 if (info
.private_do
[2])
518 print_name (fp
, "Private DO 3 .....: ", info
.private_do
[2]);
519 if (info
.private_do
[3])
520 print_name (fp
, "Private DO 4 .....: ", info
.private_do
[3]);
521 if (info
.cafpr1valid
)
523 tty_fprintf (fp
, "CA fingerprint %d .:", 1);
524 print_sha1_fpr (fp
, info
.cafpr1
);
526 if (info
.cafpr2valid
)
528 tty_fprintf (fp
, "CA fingerprint %d .:", 2);
529 print_sha1_fpr (fp
, info
.cafpr2
);
531 if (info
.cafpr3valid
)
533 tty_fprintf (fp
, "CA fingerprint %d .:", 3);
534 print_sha1_fpr (fp
, info
.cafpr3
);
536 tty_fprintf (fp
, "Signature PIN ....: %s\n",
537 info
.chv1_cached
? _("not forced"): _("forced"));
538 if (info
.key_attr
[0].algo
)
540 tty_fprintf (fp
, "Key attributes ...:");
541 for (i
=0; i
< DIM (info
.key_attr
); i
++)
542 tty_fprintf (fp
, " %u%c",
543 info
.key_attr
[i
].nbits
,
544 info
.key_attr
[i
].algo
== 1? 'R':
545 info
.key_attr
[i
].algo
== 17? 'D': '?');
546 tty_fprintf (fp
, "\n");
548 tty_fprintf (fp
, "Max. PIN lengths .: %d %d %d\n",
549 info
.chvmaxlen
[0], info
.chvmaxlen
[1], info
.chvmaxlen
[2]);
550 tty_fprintf (fp
, "PIN retry counter : %d %d %d\n",
551 info
.chvretry
[0], info
.chvretry
[1], info
.chvretry
[2]);
552 tty_fprintf (fp
, "Signature counter : %lu\n", info
.sig_counter
);
553 tty_fprintf (fp
, "Signature key ....:");
554 print_sha1_fpr (fp
, info
.fpr1valid
? info
.fpr1
:NULL
);
555 if (info
.fpr1valid
&& info
.fpr1time
)
556 tty_fprintf (fp
, " created ....: %s\n",
557 isotimestamp (info
.fpr1time
));
558 tty_fprintf (fp
, "Encryption key....:");
559 print_sha1_fpr (fp
, info
.fpr2valid
? info
.fpr2
:NULL
);
560 if (info
.fpr2valid
&& info
.fpr2time
)
561 tty_fprintf (fp
, " created ....: %s\n",
562 isotimestamp (info
.fpr2time
));
563 tty_fprintf (fp
, "Authentication key:");
564 print_sha1_fpr (fp
, info
.fpr3valid
? info
.fpr3
:NULL
);
565 if (info
.fpr3valid
&& info
.fpr3time
)
566 tty_fprintf (fp
, " created ....: %s\n",
567 isotimestamp (info
.fpr3time
));
568 tty_fprintf (fp
, "General key info..: ");
570 thefpr
= (info
.fpr1valid
? info
.fpr1
: info
.fpr2valid
? info
.fpr2
:
571 info
.fpr3valid
? info
.fpr3
: NULL
);
572 /* If the fingerprint is all 0xff, the key has no asssociated
573 OpenPGP certificate. */
574 if ( thefpr
&& !fpr_is_ff (thefpr
)
575 && !get_pubkey_byfprint (pk
, thefpr
, 20))
577 KBNODE keyblock
= NULL
;
579 print_pubkey_info (fp
, pk
);
581 if ( !get_seckeyblock_byfprint (&keyblock
, thefpr
, 20) )
582 print_card_key_info (fp
, keyblock
);
583 else if ( !get_keyblock_byfprint (&keyblock
, thefpr
, 20) )
585 release_kbnode (keyblock
);
588 if (!auto_create_card_key_stub (info
.serialno
,
589 info
.fpr1valid
? info
.fpr1
:NULL
,
590 info
.fpr2valid
? info
.fpr2
:NULL
,
591 info
.fpr3valid
? info
.fpr3
:NULL
))
593 if ( !get_seckeyblock_byfprint (&keyblock
, thefpr
, 20) )
594 print_card_key_info (fp
, keyblock
);
598 release_kbnode (keyblock
);
601 tty_fprintf (fp
, "[none]\n");
604 free_public_key (pk
);
605 agent_release_card_info (&info
);
610 get_one_name (const char *prompt1
, const char *prompt2
)
617 name
= cpr_get (prompt1
, prompt2
);
622 for (i
=0; name
[i
] && name
[i
] >= ' ' && name
[i
] <= 126; i
++)
625 /* The name must be in Latin-1 and not UTF-8 - lacking the code
626 to ensure this we restrict it to ASCII. */
628 tty_printf (_("Error: Only plain ASCII is currently allowed.\n"));
629 else if (strchr (name
, '<'))
630 tty_printf (_("Error: The \"<\" character may not be used.\n"));
631 else if (strstr (name
, " "))
632 tty_printf (_("Error: Double spaces are not allowed.\n"));
644 char *surname
= NULL
, *givenname
= NULL
;
648 surname
= get_one_name ("keygen.smartcard.surname",
649 _("Cardholder's surname: "));
650 givenname
= get_one_name ("keygen.smartcard.givenname",
651 _("Cardholder's given name: "));
652 if (!surname
|| !givenname
|| (!*surname
&& !*givenname
))
656 return -1; /*canceled*/
659 isoname
= xmalloc ( strlen (surname
) + 2 + strlen (givenname
) + 1);
660 strcpy (stpcpy (stpcpy (isoname
, surname
), "<<"), givenname
);
663 for (p
=isoname
; *p
; p
++)
667 if (strlen (isoname
) > 39 )
669 tty_printf (_("Error: Combined name too long "
670 "(limit is %d characters).\n"), 39);
675 rc
= agent_scd_setattr ("DISP-NAME", isoname
, strlen (isoname
), NULL
);
677 log_error ("error setting Name: %s\n", gpg_strerror (rc
));
690 url
= cpr_get ("cardedit.change_url", _("URL to retrieve public key: "));
696 if (strlen (url
) > 254 )
698 tty_printf (_("Error: URL too long "
699 "(limit is %d characters).\n"), 254);
704 rc
= agent_scd_setattr ("PUBKEY-URL", url
, strlen (url
), NULL
);
706 log_error ("error setting URL: %s\n", gpg_strerror (rc
));
708 write_sc_op_status (rc
);
713 /* Fetch the key from the URL given on the card or try to get it from
714 the default keyserver. */
719 struct agent_card_info_s info
;
721 memset(&info
,0,sizeof(info
));
723 rc
=agent_scd_getattr("PUBKEY-URL",&info
);
725 log_error("error retrieving URL from card: %s\n",gpg_strerror(rc
));
728 struct keyserver_spec
*spec
=NULL
;
730 rc
=agent_scd_getattr("KEY-FPR",&info
);
732 log_error("error retrieving key fingerprint from card: %s\n",
734 else if (info
.pubkey_url
&& *info
.pubkey_url
)
736 spec
=parse_keyserver_uri(info
.pubkey_url
,1,NULL
,0);
737 if(spec
&& info
.fpr1valid
)
739 /* This is not perfectly right. Currently, all card
740 fingerprints are 20 digits, but what about
741 fingerprints for a future v5 key? We should get the
742 length from somewhere lower in the code. In any
743 event, the fpr/keyid is not meaningful for straight
744 HTTP fetches, but using it allows the card to point
745 to HKP and LDAP servers as well. */
746 rc
=keyserver_import_fprint(info
.fpr1
,20,spec
);
747 free_keyserver_spec(spec
);
750 else if (info
.fpr1valid
)
752 rc
= keyserver_import_fprint (info
.fpr1
, 20, opt
.keyserver
);
760 /* Read data from file FNAME up to MAXLEN characters. On error return
761 -1 and store NULL at R_BUFFER; on success return the number of
762 bytes read and store the address of a newly allocated buffer at
765 get_data_from_file (const char *fname
, size_t maxlen
, char **r_buffer
)
773 fp
= fopen (fname
, "rb");
774 #if GNUPG_MAJOR_VERSION == 1
775 if (fp
&& is_secured_file (fileno (fp
)))
784 tty_printf (_("can't open `%s': %s\n"), fname
, strerror (errno
));
788 data
= xtrymalloc (maxlen
? maxlen
:1);
791 tty_printf (_("error allocating enough memory: %s\n"), strerror (errno
));
797 n
= fread (data
, 1, maxlen
, fp
);
803 tty_printf (_("error reading `%s': %s\n"), fname
, strerror (errno
));
812 /* Write LENGTH bytes from BUFFER to file FNAME. Return 0 on
815 put_data_to_file (const char *fname
, const void *buffer
, size_t length
)
819 fp
= fopen (fname
, "wb");
820 #if GNUPG_MAJOR_VERSION == 1
821 if (fp
&& is_secured_file (fileno (fp
)))
830 tty_printf (_("can't create `%s': %s\n"), fname
, strerror (errno
));
834 if (length
&& fwrite (buffer
, length
, 1, fp
) != 1)
836 tty_printf (_("error writing `%s': %s\n"), fname
, strerror (errno
));
846 change_login (const char *args
)
852 if (args
&& *args
== '<') /* Read it from a file */
854 for (args
++; spacep (args
); args
++)
856 n
= get_data_from_file (args
, 254, &data
);
862 data
= cpr_get ("cardedit.change_login",
863 _("Login data (account name): "));
873 tty_printf (_("Error: Login data too long "
874 "(limit is %d characters).\n"), 254);
879 rc
= agent_scd_setattr ("LOGIN-DATA", data
, n
, NULL
);
881 log_error ("error setting login data: %s\n", gpg_strerror (rc
));
883 write_sc_op_status (rc
);
888 change_private_do (const char *args
, int nr
)
890 char do_name
[] = "PRIVATE-DO-X";
895 assert (nr
>= 1 && nr
<= 4);
896 do_name
[11] = '0' + nr
;
898 if (args
&& (args
= strchr (args
, '<'))) /* Read it from a file */
900 for (args
++; spacep (args
); args
++)
902 n
= get_data_from_file (args
, 254, &data
);
908 data
= cpr_get ("cardedit.change_private_do",
909 _("Private DO data: "));
919 tty_printf (_("Error: Private DO too long "
920 "(limit is %d characters).\n"), 254);
925 rc
= agent_scd_setattr (do_name
, data
, n
, NULL
);
927 log_error ("error setting private DO: %s\n", gpg_strerror (rc
));
929 write_sc_op_status (rc
);
935 change_cert (const char *args
)
941 if (args
&& *args
== '<') /* Read it from a file */
943 for (args
++; spacep (args
); args
++)
945 n
= get_data_from_file (args
, 16384, &data
);
951 tty_printf ("usage error: redirectrion to file required\n");
955 rc
= agent_scd_writecert ("OPENPGP.3", data
, n
);
957 log_error ("error writing certificate to card: %s\n", gpg_strerror (rc
));
959 write_sc_op_status (rc
);
965 read_cert (const char *args
)
972 if (args
&& *args
== '>') /* Write it to a file */
974 for (args
++; spacep (args
); args
++)
980 tty_printf ("usage error: redirectrion to file required\n");
984 rc
= agent_scd_readcert ("OPENPGP.3", &buffer
, &length
);
986 log_error ("error reading certificate from card: %s\n", gpg_strerror (rc
));
988 rc
= put_data_to_file (fname
, buffer
, length
);
990 write_sc_op_status (rc
);
1001 data
= cpr_get ("cardedit.change_lang",
1002 _("Language preferences: "));
1008 if (strlen (data
) > 8 || (strlen (data
) & 1))
1010 tty_printf (_("Error: invalid length of preference string.\n"));
1015 for (p
=data
; *p
&& *p
>= 'a' && *p
<= 'z'; p
++)
1019 tty_printf (_("Error: invalid characters in preference string.\n"));
1024 rc
= agent_scd_setattr ("DISP-LANG", data
, strlen (data
), NULL
);
1026 log_error ("error setting lang: %s\n", gpg_strerror (rc
));
1028 write_sc_op_status (rc
);
1040 data
= cpr_get ("cardedit.change_sex",
1041 _("Sex ((M)ale, (F)emale or space): "));
1049 else if ((*data
== 'M' || *data
== 'm') && !data
[1])
1051 else if ((*data
== 'F' || *data
== 'f') && !data
[1])
1055 tty_printf (_("Error: invalid response.\n"));
1060 rc
= agent_scd_setattr ("DISP-SEX", str
, 1, NULL
);
1062 log_error ("error setting sex: %s\n", gpg_strerror (rc
));
1064 write_sc_op_status (rc
);
1070 change_cafpr (int fprno
)
1075 unsigned char fpr
[20];
1077 data
= cpr_get ("cardedit.change_cafpr", _("CA fingerprint: "));
1083 for (i
=0, s
=data
; i
< 20 && *s
; )
1100 tty_printf (_("Error: invalid formatted fingerprint.\n"));
1104 rc
= agent_scd_setattr (fprno
==1?"CA-FPR-1":
1105 fprno
==2?"CA-FPR-2":
1106 fprno
==3?"CA-FPR-3":"x", fpr
, 20, NULL
);
1108 log_error ("error setting cafpr: %s\n", gpg_strerror (rc
));
1109 write_sc_op_status (rc
);
1116 toggle_forcesig (void)
1118 struct agent_card_info_s info
;
1122 memset (&info
, 0, sizeof info
);
1123 rc
= agent_scd_getattr ("CHV-STATUS", &info
);
1126 log_error ("error getting current status: %s\n", gpg_strerror (rc
));
1129 newstate
= !info
.chv1_cached
;
1130 agent_release_card_info (&info
);
1132 rc
= agent_scd_setattr ("CHV-STATUS-1", newstate
? "\x01":"", 1, NULL
);
1134 log_error ("error toggling signature PIN flag: %s\n", gpg_strerror (rc
));
1135 write_sc_op_status (rc
);
1139 /* Helper for the key generation/edit functions. */
1141 get_info_for_key_operation (struct agent_card_info_s
*info
)
1145 memset (info
, 0, sizeof *info
);
1146 rc
= agent_scd_getattr ("SERIALNO", info
);
1147 if (rc
|| !info
->serialno
|| strncmp (info
->serialno
, "D27600012401", 12)
1148 || strlen (info
->serialno
) != 32 )
1150 log_error (_("key operation not possible: %s\n"),
1151 rc
? gpg_strerror (rc
) : _("not an OpenPGP card"));
1154 rc
= agent_scd_getattr ("KEY-FPR", info
);
1156 rc
= agent_scd_getattr ("CHV-STATUS", info
);
1158 rc
= agent_scd_getattr ("DISP-NAME", info
);
1160 log_error (_("error getting current key info: %s\n"), gpg_strerror (rc
));
1165 /* Helper for the key generation/edit functions. */
1167 check_pin_for_key_operation (struct agent_card_info_s
*info
, int *forced_chv1
)
1171 agent_clear_pin_cache (info
->serialno
);
1173 *forced_chv1
= !info
->chv1_cached
;
1175 { /* Switch off the forced mode so that during key generation we
1176 don't get bothered with PIN queries for each
1178 rc
= agent_scd_setattr ("CHV-STATUS-1", "\x01", 1, info
->serialno
);
1181 log_error ("error clearing forced signature PIN flag: %s\n",
1189 /* Check the PIN now, so that we won't get asked later for each
1190 binding signature. */
1191 rc
= agent_scd_checkpin (info
->serialno
);
1194 log_error ("error checking the PIN: %s\n", gpg_strerror (rc
));
1195 write_sc_op_status (rc
);
1201 /* Helper for the key generation/edit functions. */
1203 restore_forced_chv1 (int *forced_chv1
)
1208 { /* Switch back to forced state. */
1209 rc
= agent_scd_setattr ("CHV-STATUS-1", "", 1, NULL
);
1212 log_error ("error setting forced signature PIN flag: %s\n",
1219 /* Helper for the key generation/edit functions. */
1221 show_card_key_info (struct agent_card_info_s
*info
)
1223 tty_fprintf (NULL
, "Signature key ....:");
1224 print_sha1_fpr (NULL
, info
->fpr1valid
? info
->fpr1
:NULL
);
1225 tty_fprintf (NULL
, "Encryption key....:");
1226 print_sha1_fpr (NULL
, info
->fpr2valid
? info
->fpr2
:NULL
);
1227 tty_fprintf (NULL
, "Authentication key:");
1228 print_sha1_fpr (NULL
, info
->fpr3valid
? info
->fpr3
:NULL
);
1233 /* Helper for the key generation/edit functions. */
1235 replace_existing_key_p (struct agent_card_info_s
*info
, int keyno
)
1237 assert (keyno
>= 0 && keyno
<= 3);
1239 if ((keyno
== 1 && info
->fpr1valid
)
1240 || (keyno
== 2 && info
->fpr2valid
)
1241 || (keyno
== 3 && info
->fpr3valid
))
1244 log_info ("WARNING: such a key has already been stored on the card!\n");
1246 if ( !cpr_get_answer_is_yes( "cardedit.genkeys.replace_key",
1247 _("Replace existing key? (y/N) ")))
1255 generate_card_keys (void)
1257 struct agent_card_info_s info
;
1261 if (get_info_for_key_operation (&info
))
1264 #if GNUPG_MAJOR_VERSION == 1
1266 char *answer
=cpr_get("cardedit.genkeys.backup_enc",
1267 _("Make off-card backup of encryption key? (Y/n) "));
1269 want_backup
=answer_is_yes_no_default(answer
,1);
1274 want_backup
= cpr_get_answer_is_yes
1275 ( "cardedit.genkeys.backup_enc",
1276 _("Make off-card backup of encryption key? (Y/n) "));
1277 /*FIXME: we need answer_is_yes_no_default()*/
1280 if ( (info
.fpr1valid
&& !fpr_is_zero (info
.fpr1
))
1281 || (info
.fpr2valid
&& !fpr_is_zero (info
.fpr2
))
1282 || (info
.fpr3valid
&& !fpr_is_zero (info
.fpr3
)))
1285 log_info ("NOTE: keys are already stored on the card!\n");
1287 if ( !cpr_get_answer_is_yes( "cardedit.genkeys.replace_keys",
1288 _("Replace existing keys? (y/N) ")))
1290 agent_release_card_info (&info
);
1294 else if (!info
.disp_name
|| !*info
.disp_name
)
1297 tty_printf (_("Please note that the factory settings of the PINs are\n"
1298 " PIN = `%s' Admin PIN = `%s'\n"
1299 "You should change them using the command --change-pin\n"),
1300 "123456", "12345678");
1304 if (check_pin_for_key_operation (&info
, &forced_chv1
))
1307 generate_keypair (NULL
, info
.serialno
,
1308 want_backup
? opt
.homedir
:NULL
);
1311 agent_release_card_info (&info
);
1312 restore_forced_chv1 (&forced_chv1
);
1316 /* This function is used by the key edit menu to generate an arbitrary
1319 card_generate_subkey (KBNODE pub_keyblock
, KBNODE sec_keyblock
)
1321 struct agent_card_info_s info
;
1323 int forced_chv1
= 0;
1326 if (get_info_for_key_operation (&info
))
1329 show_card_key_info (&info
);
1331 tty_printf (_("Please select the type of key to generate:\n"));
1333 tty_printf (_(" (1) Signature key\n"));
1334 tty_printf (_(" (2) Encryption key\n"));
1335 tty_printf (_(" (3) Authentication key\n"));
1339 char *answer
= cpr_get ("cardedit.genkeys.subkeytype",
1340 _("Your selection? "));
1342 if (*answer
== CONTROL_D
)
1347 keyno
= *answer
? atoi(answer
): 0;
1349 if (keyno
>= 1 && keyno
<= 3)
1351 tty_printf(_("Invalid selection.\n"));
1354 if (replace_existing_key_p (&info
, keyno
))
1357 if (check_pin_for_key_operation (&info
, &forced_chv1
))
1360 okay
= generate_card_subkeypair (pub_keyblock
, sec_keyblock
,
1361 keyno
, info
.serialno
);
1364 agent_release_card_info (&info
);
1365 restore_forced_chv1 (&forced_chv1
);
1370 /* Store the key at NODE into the smartcard and modify NODE to
1371 carry the serialno stuff instead of the actual secret key
1372 parameters. USE is the usage for that key; 0 means any
1375 card_store_subkey (KBNODE node
, int use
)
1377 struct agent_card_info_s info
;
1381 PKT_secret_key
*copied_sk
= NULL
;
1387 assert (node
->pkt
->pkttype
== PKT_SECRET_KEY
1388 || node
->pkt
->pkttype
== PKT_SECRET_SUBKEY
);
1389 sk
= node
->pkt
->pkt
.secret_key
;
1391 if (get_info_for_key_operation (&info
))
1394 show_card_key_info (&info
);
1396 if (!is_RSA (sk
->pubkey_algo
)
1397 || (!info
.is_v2
&& nbits_from_sk (sk
) != 1024) )
1399 tty_printf ("You may only store a 1024 bit RSA key on the card\n");
1404 allow_keyno
[0] = (!use
|| (use
& (PUBKEY_USAGE_SIG
)));
1405 allow_keyno
[1] = (!use
|| (use
& (PUBKEY_USAGE_ENC
)));
1406 allow_keyno
[2] = (!use
|| (use
& (PUBKEY_USAGE_SIG
|PUBKEY_USAGE_AUTH
)));
1408 tty_printf (_("Please select where to store the key:\n"));
1411 tty_printf (_(" (1) Signature key\n"));
1413 tty_printf (_(" (2) Encryption key\n"));
1415 tty_printf (_(" (3) Authentication key\n"));
1419 char *answer
= cpr_get ("cardedit.genkeys.storekeytype",
1420 _("Your selection? "));
1422 if (*answer
== CONTROL_D
|| !*answer
)
1427 keyno
= *answer
? atoi(answer
): 0;
1429 if (keyno
>= 1 && keyno
<= 3 && allow_keyno
[keyno
-1])
1431 tty_printf(_("Invalid selection.\n"));
1434 if (replace_existing_key_p (&info
, keyno
))
1437 /* Unprotect key. */
1438 switch (is_secret_key_protected (sk
) )
1440 case 0: /* Not protected. */
1443 log_error (_("unknown key protection algorithm\n"));
1446 if (sk
->protect
.s2k
.mode
== 1001)
1448 log_error (_("secret parts of key are not available\n"));
1451 if (sk
->protect
.s2k
.mode
== 1002)
1453 log_error (_("secret key already stored on a card\n"));
1456 /* We better copy the key before we unprotect it. */
1457 copied_sk
= sk
= copy_secret_key (NULL
, sk
);
1458 rc
= check_secret_key (sk
, 0);
1463 rc
= save_unprotected_key_to_card (sk
, keyno
);
1466 log_error (_("error writing key to card: %s\n"), gpg_strerror (rc
));
1470 /* Get back to the maybe protected original secret key. */
1473 free_secret_key (copied_sk
);
1476 sk
= node
->pkt
->pkt
.secret_key
;
1478 /* Get rid of the secret key parameters and store the serial numer. */
1479 n
= pubkey_get_nskey (sk
->pubkey_algo
);
1480 for (i
=pubkey_get_npkey (sk
->pubkey_algo
); i
< n
; i
++)
1482 gcry_mpi_release (sk
->skey
[i
]);
1485 i
= pubkey_get_npkey (sk
->pubkey_algo
);
1486 sk
->skey
[i
] = gcry_mpi_set_opaque (NULL
, xstrdup ("dummydata"), 10*8);
1487 sk
->is_protected
= 1;
1488 sk
->protect
.s2k
.mode
= 1002;
1490 for (sk
->protect
.ivlen
=0; sk
->protect
.ivlen
< 16 && *s
&& s
[1];
1491 sk
->protect
.ivlen
++, s
+= 2)
1492 sk
->protect
.iv
[sk
->protect
.ivlen
] = xtoi_2 (s
);
1498 free_secret_key (copied_sk
);
1499 agent_release_card_info (&info
);
1505 /* Data used by the command parser. This needs to be outside of the
1506 function scope to allow readline based command completion. */
1510 cmdQUIT
, cmdADMIN
, cmdHELP
, cmdLIST
, cmdDEBUG
, cmdVERIFY
,
1511 cmdNAME
, cmdURL
, cmdFETCH
, cmdLOGIN
, cmdLANG
, cmdSEX
, cmdCAFPR
,
1512 cmdFORCESIG
, cmdGENERATE
, cmdPASSWD
, cmdPRIVATEDO
, cmdWRITECERT
,
1513 cmdREADCERT
, cmdUNBLOCK
,
1525 { "quit" , cmdQUIT
, 0, N_("quit this menu")},
1526 { "q" , cmdQUIT
, 0, NULL
},
1527 { "admin" , cmdADMIN
, 0, N_("show admin commands")},
1528 { "help" , cmdHELP
, 0, N_("show this help")},
1529 { "?" , cmdHELP
, 0, NULL
},
1530 { "list" , cmdLIST
, 0, N_("list all available data")},
1531 { "l" , cmdLIST
, 0, NULL
},
1532 { "debug" , cmdDEBUG
, 0, NULL
},
1533 { "name" , cmdNAME
, 1, N_("change card holder's name")},
1534 { "url" , cmdURL
, 1, N_("change URL to retrieve key")},
1535 { "fetch" , cmdFETCH
, 0, N_("fetch the key specified in the card URL")},
1536 { "login" , cmdLOGIN
, 1, N_("change the login name")},
1537 { "lang" , cmdLANG
, 1, N_("change the language preferences")},
1538 { "sex" , cmdSEX
, 1, N_("change card holder's sex")},
1539 { "cafpr" , cmdCAFPR
, 1, N_("change a CA fingerprint")},
1540 { "forcesig", cmdFORCESIG
, 1, N_("toggle the signature force PIN flag")},
1541 { "generate", cmdGENERATE
, 1, N_("generate new keys")},
1542 { "passwd" , cmdPASSWD
, 0, N_("menu to change or unblock the PIN")},
1543 { "verify" , cmdVERIFY
, 0, N_("verify the PIN and list all data")},
1544 { "unblock" , cmdUNBLOCK
,0, N_("unblock the PIN using a Reset Code") },
1545 /* Note, that we do not announce these command yet. */
1546 { "privatedo", cmdPRIVATEDO
, 0, NULL
},
1547 { "readcert", cmdREADCERT
, 0, NULL
},
1548 { "writecert", cmdWRITECERT
, 1, NULL
},
1549 { NULL
, cmdINVCMD
, 0, NULL
}
1553 #if GNUPG_MAJOR_VERSION == 1 && defined (HAVE_LIBREADLINE)
1555 /* These two functions are used by readline for command completion. */
1558 command_generator(const char *text
,int state
)
1560 static int list_index
,len
;
1563 /* If this is a new word to complete, initialize now. This includes
1564 saving the length of TEXT for efficiency, and initializing the
1565 index variable to 0. */
1572 /* Return the next partial match */
1573 while((name
=cmds
[list_index
].name
))
1575 /* Only complete commands that have help text */
1576 if(cmds
[list_index
++].desc
&& strncmp(name
,text
,len
)==0)
1577 return strdup(name
);
1584 card_edit_completion(const char *text
, int start
, int end
)
1586 /* If we are at the start of a line, we try and command-complete.
1587 If not, just do nothing for now. */
1590 return rl_completion_matches(text
,command_generator
);
1592 rl_attempted_completion_over
=1;
1596 #endif /* GNUPG_MAJOR_VERSION == 1 && HAVE_LIBREADLINE */
1598 /* Menu to edit all user changeable values on an OpenPGP card. Only
1599 Key creation is not handled here. */
1601 card_edit (strlist_t commands
)
1603 enum cmdids cmd
= cmdNOP
;
1604 int have_commands
= !!commands
;
1606 char *answer
= NULL
;
1608 char serialnobuf
[50];
1611 if (opt
.command_fd
!= -1)
1613 else if (opt
.batch
&& !have_commands
)
1615 log_error(_("can't do this in batch mode\n"));
1622 const char *arg_string
= "";
1623 const char *arg_rest
= "";
1631 if (opt
.with_colons
)
1633 card_status (stdout
, serialnobuf
, DIM (serialnobuf
));
1638 card_status (NULL
, serialnobuf
, DIM (serialnobuf
));
1651 answer
= xstrdup (commands
->d
);
1652 commands
= commands
->next
;
1656 answer
= xstrdup ("quit");
1664 #if GNUPG_MAJOR_VERSION == 1
1665 tty_enable_completion (card_edit_completion
);
1667 answer
= cpr_get_no_help("cardedit.prompt", _("Command> "));
1669 #if GNUPG_MAJOR_VERSION == 1
1670 tty_disable_completion ();
1673 trim_spaces(answer
);
1675 while ( *answer
== '#' );
1677 arg_number
= 0; /* Yes, here is the init which egcc complains about */
1680 cmd
= cmdLIST
; /* Default to the list command */
1681 else if (*answer
== CONTROL_D
)
1685 if ((p
=strchr (answer
,' ')))
1688 trim_spaces (answer
);
1690 arg_number
= atoi(p
);
1693 while (digitp (arg_rest
))
1695 while (spacep (arg_rest
))
1699 for (i
=0; cmds
[i
].name
; i
++ )
1700 if (!ascii_strcasecmp (answer
, cmds
[i
].name
))
1704 cmd_admin_only
= cmds
[i
].admin_only
;
1707 if (!allow_admin
&& cmd_admin_only
)
1710 tty_printf (_("Admin-only command\n"));
1717 for (i
=0; cmds
[i
].name
; i
++ )
1719 && (!cmds
[i
].admin_only
|| (cmds
[i
].admin_only
&& allow_admin
)))
1720 tty_printf("%-10s %s\n", cmds
[i
].name
, _(cmds
[i
].desc
) );
1724 if ( !strcmp (arg_string
, "on") )
1726 else if ( !strcmp (arg_string
, "off") )
1728 else if ( !strcmp (arg_string
, "verify") )
1730 /* Force verification of the Admin Command. However,
1731 this is only done if the retry counter is at initial
1733 char *tmp
= xmalloc (strlen (serialnobuf
) + 6 + 1);
1734 strcpy (stpcpy (tmp
, serialnobuf
), "[CHV3]");
1735 allow_admin
= !agent_scd_checkpin (tmp
);
1739 allow_admin
=!allow_admin
;
1741 tty_printf(_("Admin commands are allowed\n"));
1743 tty_printf(_("Admin commands are not allowed\n"));
1747 agent_scd_checkpin (serialnobuf
);
1768 change_login (arg_string
);
1780 if ( arg_number
< 1 || arg_number
> 3 )
1781 tty_printf ("usage: cafpr N\n"
1784 change_cafpr (arg_number
);
1788 if ( arg_number
< 1 || arg_number
> 4 )
1789 tty_printf ("usage: privatedo N\n"
1792 change_private_do (arg_string
, arg_number
);
1796 if ( arg_number
!= 3 )
1797 tty_printf ("usage: writecert 3 < FILE\n");
1799 change_cert (arg_rest
);
1803 if ( arg_number
!= 3 )
1804 tty_printf ("usage: readcert 3 > FILE\n");
1806 read_cert (arg_rest
);
1814 generate_card_keys ();
1818 change_pin (0, allow_admin
);
1822 change_pin (1, allow_admin
);
1834 tty_printf (_("Invalid command (try \"help\")\n"));
1836 } /* End command switch. */
1837 } /* End of main menu loop. */