1 /* keyedit.c - keyedit stuff
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3 * 2008, 2009, 2010 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
28 #ifdef HAVE_LIBREADLINE
29 # define GNUPG_LIBREADLINE_H_INCLUDED
30 # include <readline/readline.h>
47 #include "keyserver-internal.h"
48 #include "call-agent.h"
50 static void show_prefs (PKT_user_id * uid, PKT_signature * selfsig,
52 static void show_names (KBNODE keyblock, PKT_public_key * pk,
53 unsigned int flag, int with_prefs);
54 static void show_key_with_all_names (KBNODE keyblock, int only_marked,
55 int with_revoker, int with_fpr,
56 int with_subkeys, int with_prefs);
57 static void show_key_and_fingerprint (KBNODE keyblock);
58 static int menu_adduid (KBNODE keyblock, int photo, const char *photo_name);
59 static void menu_deluid (KBNODE pub_keyblock);
60 static int menu_delsig (KBNODE pub_keyblock);
61 static int menu_clean (KBNODE keyblock, int self_only);
62 static void menu_delkey (KBNODE pub_keyblock);
63 static int menu_addrevoker (KBNODE pub_keyblock, int sensitive);
64 static int menu_expire (KBNODE pub_keyblock);
65 static int menu_backsign (KBNODE pub_keyblock);
66 static int menu_set_primary_uid (KBNODE pub_keyblock);
67 static int menu_set_preferences (KBNODE pub_keyblock);
68 static int menu_set_keyserver_url (const char *url, KBNODE pub_keyblock);
69 static int menu_set_notation (const char *string, KBNODE pub_keyblock);
70 static int menu_select_uid (KBNODE keyblock, int idx);
71 static int menu_select_uid_namehash (KBNODE keyblock, const char *namehash);
72 static int menu_select_key (KBNODE keyblock, int idx);
73 static int count_uids (KBNODE keyblock);
74 static int count_uids_with_flag (KBNODE keyblock, unsigned flag);
75 static int count_keys_with_flag (KBNODE keyblock, unsigned flag);
76 static int count_selected_uids (KBNODE keyblock);
77 static int real_uids_left (KBNODE keyblock);
78 static int count_selected_keys (KBNODE keyblock);
79 static int menu_revsig (KBNODE keyblock);
80 static int menu_revuid (KBNODE keyblock);
81 static int menu_revkey (KBNODE pub_keyblock);
82 static int menu_revsubkey (KBNODE pub_keyblock);
83 static int enable_disable_key (KBNODE keyblock, int disable);
84 static void menu_showphoto (KBNODE keyblock);
86 static int update_trust = 0;
88 #define CONTROL_D ('D' - 'A' + 1)
90 #define NODFLG_BADSIG (1<<0) /* Bad signature. */
91 #define NODFLG_NOKEY (1<<1) /* No public key. */
92 #define NODFLG_SIGERR (1<<2) /* Other sig error. */
94 #define NODFLG_MARK_A (1<<4) /* Temporary mark. */
95 #define NODFLG_DELSIG (1<<5) /* To be deleted. */
97 #define NODFLG_SELUID (1<<8) /* Indicate the selected userid. */
98 #define NODFLG_SELKEY (1<<9) /* Indicate the selected key. */
99 #define NODFLG_SELSIG (1<<10) /* Indicate a selected signature. */
103 int non_exportable, non_revocable;
104 struct revocation_reason_info *reason;
105 byte trust_depth, trust_value;
110 #ifdef ENABLE_CARD_SUPPORT
111 /* Given a node SEC_NODE with a secret key or subkey, locate the
112 corresponding public key from pub_keyblock. */
113 static PKT_public_key *
114 find_pk_from_sknode (KBNODE pub_keyblock, KBNODE sec_node)
116 KBNODE node = pub_keyblock;
120 #warning: This is not anymore needed.
121 if (sec_node->pkt->pkttype == PKT_SECRET_KEY
122 && node->pkt->pkttype == PKT_PUBLIC_KEY)
123 return node->pkt->pkt.public_key;
124 if (sec_node->pkt->pkttype != PKT_SECRET_SUBKEY)
126 sk = sec_node->pkt->pkt.secret_key;
127 for (; node; node = node->next)
128 if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
130 pk = node->pkt->pkt.public_key;
131 if (pk->keyid[0] == sk->keyid[0] && pk->keyid[1] == sk->keyid[1])
137 #endif /* ENABLE_CARD_SUPPORT */
140 /* TODO: Fix duplicated code between here and the check-sigs/list-sigs
141 code in keylist.c. */
143 print_and_check_one_sig_colon (KBNODE keyblock, KBNODE node,
144 int *inv_sigs, int *no_key, int *oth_err,
145 int *is_selfsig, int print_without_key)
147 PKT_signature *sig = node->pkt->pkt.signature;
150 /* TODO: Make sure a cached sig record here still has the pk that
151 issued it. See also keylist.c:list_keyblock_print */
153 switch ((rc = check_key_signature (keyblock, node, is_selfsig)))
156 node->flag &= ~(NODFLG_BADSIG | NODFLG_NOKEY | NODFLG_SIGERR);
159 case G10ERR_BAD_SIGN:
160 node->flag = NODFLG_BADSIG;
165 case G10ERR_NO_PUBKEY:
166 case G10ERR_UNU_PUBKEY:
167 node->flag = NODFLG_NOKEY;
173 node->flag = NODFLG_SIGERR;
180 if (sigrc != '?' || print_without_key)
182 printf ("sig:%c::%d:%08lX%08lX:%lu:%lu:",
183 sigrc, sig->pubkey_algo, (ulong) sig->keyid[0],
184 (ulong) sig->keyid[1], (ulong) sig->timestamp,
185 (ulong) sig->expiredate);
187 if (sig->trust_depth || sig->trust_value)
188 printf ("%d %d", sig->trust_depth, sig->trust_value);
192 if (sig->trust_regexp)
193 es_write_sanitized (es_stdout,
194 sig->trust_regexp, strlen (sig->trust_regexp),
197 printf ("::%02x%c\n", sig->sig_class,
198 sig->flags.exportable ? 'x' : 'l');
200 if (opt.show_subpackets)
201 print_subpackets_colon (sig);
204 return (sigrc == '!');
209 * Print information about a signature, check it and return true
210 * if the signature is okay. NODE must be a signature packet.
213 print_and_check_one_sig (KBNODE keyblock, KBNODE node,
214 int *inv_sigs, int *no_key, int *oth_err,
215 int *is_selfsig, int print_without_key)
217 PKT_signature *sig = node->pkt->pkt.signature;
219 int is_rev = sig->sig_class == 0x30;
221 /* TODO: Make sure a cached sig record here still has the pk that
222 issued it. See also keylist.c:list_keyblock_print */
224 switch ((rc = check_key_signature (keyblock, node, is_selfsig)))
227 node->flag &= ~(NODFLG_BADSIG | NODFLG_NOKEY | NODFLG_SIGERR);
230 case G10ERR_BAD_SIGN:
231 node->flag = NODFLG_BADSIG;
236 case G10ERR_NO_PUBKEY:
237 case G10ERR_UNU_PUBKEY:
238 node->flag = NODFLG_NOKEY;
244 node->flag = NODFLG_SIGERR;
250 if (sigrc != '?' || print_without_key)
252 tty_printf ("%s%c%c %c%c%c%c%c%c %s %s",
253 is_rev ? "rev" : "sig", sigrc,
254 (sig->sig_class - 0x10 > 0 &&
255 sig->sig_class - 0x10 <
256 4) ? '0' + sig->sig_class - 0x10 : ' ',
257 sig->flags.exportable ? ' ' : 'L',
258 sig->flags.revocable ? ' ' : 'R',
259 sig->flags.policy_url ? 'P' : ' ',
260 sig->flags.notation ? 'N' : ' ',
261 sig->flags.expired ? 'X' : ' ',
262 (sig->trust_depth > 9) ? 'T' : (sig->trust_depth >
264 sig->trust_depth : ' ', keystr (sig->keyid),
265 datestr_from_sig (sig));
266 if (opt.list_options & LIST_SHOW_SIG_EXPIRE)
267 tty_printf (" %s", expirestr_from_sig (sig));
270 tty_printf ("[%s] ", g10_errstr (rc));
271 else if (sigrc == '?')
273 else if (*is_selfsig)
275 tty_printf (is_rev ? _("[revocation]") : _("[self-signature]"));
280 char *p = get_user_id (sig->keyid, &n);
281 tty_print_utf8_string2 (p, n,
282 opt.screen_columns - keystrlen () - 26 -
284 list_options & LIST_SHOW_SIG_EXPIRE) ? 11
290 if (sig->flags.policy_url && (opt.list_options & LIST_SHOW_POLICY_URLS))
291 show_policy_url (sig, 3, 0);
293 if (sig->flags.notation && (opt.list_options & LIST_SHOW_NOTATIONS))
294 show_notation (sig, 3, 0,
296 list_options & LIST_SHOW_STD_NOTATIONS) ? 1 : 0) +
298 list_options & LIST_SHOW_USER_NOTATIONS) ? 2 : 0));
300 if (sig->flags.pref_ks && (opt.list_options & LIST_SHOW_KEYSERVER_URLS))
301 show_keyserver_url (sig, 3, 0);
304 return (sigrc == '!');
310 * Check the keysigs and set the flags to indicate errors.
311 * Returns true if error found.
314 check_all_keysigs (KBNODE keyblock, int only_selected)
323 int selected = !only_selected;
326 for (kbctx = NULL; (node = walk_kbnode (keyblock, &kbctx, 0));)
328 if (node->pkt->pkttype == PKT_USER_ID)
330 PKT_user_id *uid = node->pkt->pkt.user_id;
333 selected = (node->flag & NODFLG_SELUID);
337 tty_print_utf8_string (uid->name, uid->len);
339 if (anyuid && !has_selfsig)
345 else if (selected && node->pkt->pkttype == PKT_SIGNATURE
346 && ((node->pkt->pkt.signature->sig_class & ~3) == 0x10
347 || node->pkt->pkt.signature->sig_class == 0x30))
351 if (print_and_check_one_sig (keyblock, node, &inv_sigs,
352 &no_key, &oth_err, &selfsig, 0))
357 /* Hmmm: should we update the trustdb here? */
363 tty_printf (_("1 bad signature\n"));
365 tty_printf (_("%d bad signatures\n"), inv_sigs);
367 tty_printf (_("1 signature not checked due to a missing key\n"));
369 tty_printf (_("%d signatures not checked due to missing keys\n"), no_key);
371 tty_printf (_("1 signature not checked due to an error\n"));
373 tty_printf (_("%d signatures not checked due to errors\n"), oth_err);
374 if (mis_selfsig == 1)
375 tty_printf (_("1 user ID without valid self-signature detected\n"));
376 else if (mis_selfsig)
377 tty_printf (_("%d user IDs without valid self-signatures detected\n"),
380 return inv_sigs || no_key || oth_err || mis_selfsig;
385 sign_mk_attrib (PKT_signature * sig, void *opaque)
387 struct sign_attrib *attrib = opaque;
390 if (attrib->non_exportable)
392 buf[0] = 0; /* not exportable */
393 build_sig_subpkt (sig, SIGSUBPKT_EXPORTABLE, buf, 1);
396 if (attrib->non_revocable)
398 buf[0] = 0; /* not revocable */
399 build_sig_subpkt (sig, SIGSUBPKT_REVOCABLE, buf, 1);
403 revocation_reason_build_cb (sig, attrib->reason);
405 if (attrib->trust_depth)
407 /* Not critical. If someone doesn't understand trust sigs,
408 this can still be a valid regular signature. */
409 buf[0] = attrib->trust_depth;
410 buf[1] = attrib->trust_value;
411 build_sig_subpkt (sig, SIGSUBPKT_TRUST, buf, 2);
413 /* Critical. If someone doesn't understands regexps, this
414 whole sig should be invalid. Note the +1 for the length -
415 regexps are null terminated. */
416 if (attrib->trust_regexp)
417 build_sig_subpkt (sig, SIGSUBPKT_FLAG_CRITICAL | SIGSUBPKT_REGEXP,
418 attrib->trust_regexp,
419 strlen (attrib->trust_regexp) + 1);
427 trustsig_prompt (byte * trust_value, byte * trust_depth, char **regexp)
435 /* Same string as pkclist.c:do_edit_ownertrust */
437 ("Please decide how far you trust this user to correctly verify"
438 " other users' keys\n(by looking at passports, checking"
439 " fingerprints from different sources, etc.)\n"));
441 tty_printf (_(" %d = I trust marginally\n"), 1);
442 tty_printf (_(" %d = I trust fully\n"), 2);
445 while (*trust_value == 0)
447 p = cpr_get ("trustsig_prompt.trust_value", _("Your selection? "));
450 /* 60 and 120 are as per RFC2440 */
451 if (p[0] == '1' && !p[1])
453 else if (p[0] == '2' && !p[1])
460 tty_printf (_("Please enter the depth of this trust signature.\n"
461 "A depth greater than 1 allows the key you are signing to make\n"
462 "trust signatures on your behalf.\n"));
465 while (*trust_depth == 0)
467 p = cpr_get ("trustsig_prompt.trust_depth", _("Your selection? "));
470 *trust_depth = atoi (p);
476 tty_printf (_("Please enter a domain to restrict this signature, "
477 "or enter for none.\n"));
481 p = cpr_get ("trustsig_prompt.trust_regexp", _("Your selection? "));
488 int regexplen = 100, ind;
490 *regexp = xmalloc (regexplen);
492 /* Now mangle the domain the user entered into a regexp. To do
493 this, \-escape everything that isn't alphanumeric, and attach
494 "<[^>]+[@.]" to the front, and ">$" to the end. */
496 strcpy (*regexp, "<[^>]+[@.]");
497 ind = strlen (*regexp);
501 if (!((*q >= 'A' && *q <= 'Z')
502 || (*q >= 'a' && *q <= 'z') || (*q >= '0' && *q <= '9')))
503 (*regexp)[ind++] = '\\';
505 (*regexp)[ind++] = *q;
507 if ((regexplen - ind) < 3)
510 *regexp = xrealloc (*regexp, regexplen);
516 (*regexp)[ind] = '\0';
517 strcat (*regexp, ">$");
526 * Loop over all LOCUSR and and sign the uids after asking.
527 * If no user id is marked, all user ids will be signed;
528 * if some user_ids are marked those will be signed.
531 sign_uids (KBNODE keyblock, strlist_t locusr, int *ret_modified,
532 int local, int nonrevocable, int trust, int interactive)
535 SK_LIST sk_list = NULL;
536 SK_LIST sk_rover = NULL;
537 PKT_public_key *pk = NULL;
538 KBNODE node, uidnode;
539 PKT_public_key *primary_pk = NULL;
540 int select_all = !count_selected_uids (keyblock) || interactive;
543 /* Are there any non-v3 sigs on this key already? */
545 for (node = keyblock; node; node = node->next)
546 if (node->pkt->pkttype == PKT_SIGNATURE &&
547 node->pkt->pkt.signature->version > 3)
553 /* Build a list of all signators.
555 * We use the CERT flag to request the primary which must always
556 * be one which is capable of signing keys. I can't see a reason
557 * why to sign keys using a subkey. Implementation of USAGE_CERT
558 * is just a hack in getkey.c and does not mean that a subkey
559 * marked as certification capable will be used. */
560 rc = build_sk_list (locusr, &sk_list, PUBKEY_USAGE_CERT);
564 /* Loop over all signators. */
565 for (sk_rover = sk_list; sk_rover; sk_rover = sk_rover->next)
567 u32 sk_keyid[2], pk_keyid[2];
568 char *p, *trust_regexp = NULL;
569 int force_v4 = 0, class = 0, selfsig = 0;
570 u32 duration = 0, timestamp = 0;
571 byte trust_depth = 0, trust_value = 0;
573 if (local || nonrevocable || trust
574 || opt.cert_policy_url || opt.cert_notations)
578 keyid_from_pk (pk, sk_keyid);
580 /* Set mark A for all selected user ids. */
581 for (node = keyblock; node; node = node->next)
583 if (select_all || (node->flag & NODFLG_SELUID))
584 node->flag |= NODFLG_MARK_A;
586 node->flag &= ~NODFLG_MARK_A;
589 /* Reset mark for uids which are already signed. */
591 for (node = keyblock; node; node = node->next)
593 if (node->pkt->pkttype == PKT_PUBLIC_KEY)
595 primary_pk = node->pkt->pkt.public_key;
596 keyid_from_pk (primary_pk, pk_keyid);
598 /* Is this a self-sig? */
599 if (pk_keyid[0] == sk_keyid[0] && pk_keyid[1] == sk_keyid[1])
602 /* Do not force a v4 sig here, otherwise it would
603 be difficult to remake a v3 selfsig. If this
604 is a v3->v4 promotion case, then we set
605 force_v4 later anyway. */
609 else if (node->pkt->pkttype == PKT_USER_ID)
611 uidnode = (node->flag & NODFLG_MARK_A) ? node : NULL;
617 user = utf8_to_native (uidnode->pkt->pkt.user_id->name,
618 uidnode->pkt->pkt.user_id->len, 0);
620 if (uidnode->pkt->pkt.user_id->is_revoked)
622 tty_printf (_("User ID \"%s\" is revoked."), user);
629 /* No, so remove the mark and continue */
630 if (!cpr_get_answer_is_yes ("sign_uid.revoke_okay",
631 _("Are you sure you "
632 "still want to sign "
635 uidnode->flag &= ~NODFLG_MARK_A;
638 else if (interactive)
643 uidnode->flag &= ~NODFLG_MARK_A;
645 tty_printf (_(" Unable to sign.\n"));
648 else if (uidnode->pkt->pkt.user_id->is_expired)
650 tty_printf (_("User ID \"%s\" is expired."), user);
657 /* No, so remove the mark and continue */
658 if (!cpr_get_answer_is_yes ("sign_uid.expire_okay",
659 _("Are you sure you "
660 "still want to sign "
663 uidnode->flag &= ~NODFLG_MARK_A;
666 else if (interactive)
671 uidnode->flag &= ~NODFLG_MARK_A;
673 tty_printf (_(" Unable to sign.\n"));
676 else if (!uidnode->pkt->pkt.user_id->created && !selfsig)
678 tty_printf (_("User ID \"%s\" is not self-signed."),
684 /* No, so remove the mark and continue */
685 if (!cpr_get_answer_is_yes ("sign_uid.nosig_okay",
686 _("Are you sure you "
687 "still want to sign "
690 uidnode->flag &= ~NODFLG_MARK_A;
693 else if (interactive)
698 uidnode->flag &= ~NODFLG_MARK_A;
700 tty_printf (_(" Unable to sign.\n"));
704 if (uidnode && interactive && !yesreally)
706 tty_printf (_("User ID \"%s\" is signable. "), user);
707 if (!cpr_get_answer_is_yes ("sign_uid.sign_okay",
708 _("Sign it? (y/N) ")))
710 uidnode->flag &= ~NODFLG_MARK_A;
718 else if (uidnode && node->pkt->pkttype == PKT_SIGNATURE
719 && (node->pkt->pkt.signature->sig_class & ~3) == 0x10)
721 if (sk_keyid[0] == node->pkt->pkt.signature->keyid[0]
722 && sk_keyid[1] == node->pkt->pkt.signature->keyid[1])
727 user = utf8_to_native (uidnode->pkt->pkt.user_id->name,
728 uidnode->pkt->pkt.user_id->len, 0);
730 /* It's a v3 self-sig. Make it into a v4 self-sig? */
731 if (node->pkt->pkt.signature->version < 4 && selfsig)
733 tty_printf (_("The self-signature on \"%s\"\n"
734 "is a PGP 2.x-style signature.\n"), user);
736 /* Note that the regular PGP2 warning below
737 still applies if there are no v4 sigs on
741 if (cpr_get_answer_is_yes ("sign_uid.v4_promote_okay",
742 _("Do you want to promote "
743 "it to an OpenPGP self-"
744 "signature? (y/N) ")))
747 node->flag |= NODFLG_DELSIG;
753 /* Is the current signature expired? */
754 if (node->pkt->pkt.signature->flags.expired)
756 tty_printf (_("Your current signature on \"%s\"\n"
757 "has expired.\n"), user);
759 if (cpr_get_answer_is_yes
760 ("sign_uid.replace_expired_okay",
761 _("Do you want to issue a "
762 "new signature to replace "
763 "the expired one? (y/N) ")))
765 /* Mark these for later deletion. We
766 don't want to delete them here, just in
767 case the replacement signature doesn't
768 happen for some reason. We only delete
769 these after the replacement is already
772 node->flag |= NODFLG_DELSIG;
778 if (!node->pkt->pkt.signature->flags.exportable && !local)
780 /* It's a local sig, and we want to make a
782 tty_printf (_("Your current signature on \"%s\"\n"
783 "is a local signature.\n"), user);
785 if (cpr_get_answer_is_yes
786 ("sign_uid.local_promote_okay",
787 _("Do you want to promote "
788 "it to a full exportable " "signature? (y/N) ")))
790 /* Mark these for later deletion. We
791 don't want to delete them here, just in
792 case the replacement signature doesn't
793 happen for some reason. We only delete
794 these after the replacement is already
797 node->flag |= NODFLG_DELSIG;
803 /* Fixme: see whether there is a revocation in which
804 * case we should allow to sign it again. */
805 if (!node->pkt->pkt.signature->flags.exportable && local)
807 (_("\"%s\" was already locally signed by key %s\n"),
808 user, keystr_from_pk (pk));
810 tty_printf (_("\"%s\" was already signed by key %s\n"),
811 user, keystr_from_pk (pk));
814 && cpr_get_answer_is_yes ("sign_uid.dupe_okay",
815 _("Do you want to sign it "
816 "again anyway? (y/N) ")))
818 /* Don't delete the old sig here since this is
819 an --expert thing. */
824 snprintf (buf, sizeof buf, "%08lX%08lX",
825 (ulong) pk->keyid[0], (ulong) pk->keyid[1]);
826 write_status_text (STATUS_ALREADY_SIGNED, buf);
827 uidnode->flag &= ~NODFLG_MARK_A; /* remove mark */
834 /* Check whether any uids are left for signing. */
835 if (!count_uids_with_flag (keyblock, NODFLG_MARK_A))
837 tty_printf (_("Nothing to sign with key %s\n"),
838 keystr_from_pk (pk));
842 /* Ask whether we really should sign these user id(s). */
844 show_key_with_all_names (keyblock, 1, 0, 1, 0, 0);
847 if (primary_pk->expiredate && !selfsig)
849 u32 now = make_timestamp ();
851 if (primary_pk->expiredate <= now)
853 tty_printf (_("This key has expired!"));
858 if (!cpr_get_answer_is_yes ("sign_uid.expired_okay",
859 _("Are you sure you still "
860 "want to sign it? (y/N) ")))
865 tty_printf (_(" Unable to sign.\n"));
871 tty_printf (_("This key is due to expire on %s.\n"),
872 expirestr_from_pk (primary_pk));
874 if (opt.ask_cert_expire)
876 char *answer = cpr_get ("sign_uid.expire",
877 _("Do you want your signature to "
878 "expire at the same time? (Y/n) "));
879 if (answer_is_yes_no_default (answer, 1))
881 /* This fixes the signature timestamp we're
882 going to make as now. This is so the
883 expiration date is exactly correct, and not
884 a few seconds off (due to the time it takes
885 to answer the questions, enter the
888 duration = primary_pk->expiredate - now;
898 /* Only ask for duration if we haven't already set it to match
899 the expiration of the pk */
900 if (!duration && !selfsig)
902 if (opt.ask_cert_expire)
903 duration = ask_expire_interval (1, opt.def_cert_expire);
905 duration = parse_expire_string (opt.def_cert_expire);
911 /* Is --pgp2 on, it's a v3 key, all the sigs on the key are
912 currently v3 and we're about to sign it with a v4 sig? If
914 if (PGP2 && all_v3 &&
915 (pk->version > 3 || force_v4) && primary_pk->version <= 3)
917 tty_printf (_("You may not make an OpenPGP signature on a "
918 "PGP 2.x key while in --pgp2 mode.\n"));
919 tty_printf (_("This would make the key unusable in PGP 2.x.\n"));
923 if (!cpr_get_answer_is_yes ("sign_uid.v4_on_v3_okay",
924 _("Are you sure you still "
925 "want to sign it? (y/N) ")))
938 if (opt.batch || !opt.ask_cert_level)
939 class = 0x10 + opt.def_cert_level;
944 tty_printf (_("How carefully have you verified the key you are "
945 "about to sign actually belongs\nto the person "
946 "named above? If you don't know what to "
947 "answer, enter \"0\".\n"));
949 tty_printf (_(" (0) I will not answer.%s\n"),
950 opt.def_cert_level == 0 ? " (default)" : "");
951 tty_printf (_(" (1) I have not checked at all.%s\n"),
952 opt.def_cert_level == 1 ? " (default)" : "");
953 tty_printf (_(" (2) I have done casual checking.%s\n"),
954 opt.def_cert_level == 2 ? " (default)" : "");
955 tty_printf (_(" (3) I have done very careful checking.%s\n"),
956 opt.def_cert_level == 3 ? " (default)" : "");
961 answer = cpr_get ("sign_uid.class",
963 "(enter `?' for more information): "));
964 if (answer[0] == '\0')
965 class = 0x10 + opt.def_cert_level; /* Default */
966 else if (ascii_strcasecmp (answer, "0") == 0)
967 class = 0x10; /* Generic */
968 else if (ascii_strcasecmp (answer, "1") == 0)
969 class = 0x11; /* Persona */
970 else if (ascii_strcasecmp (answer, "2") == 0)
971 class = 0x12; /* Casual */
972 else if (ascii_strcasecmp (answer, "3") == 0)
973 class = 0x13; /* Positive */
975 tty_printf (_("Invalid selection.\n"));
982 trustsig_prompt (&trust_value, &trust_depth, &trust_regexp);
985 p = get_user_id_native (sk_keyid);
986 tty_printf (_("Are you sure that you want to sign this key with your\n"
987 "key \"%s\" (%s)\n"), p, keystr_from_pk (pk));
993 tty_printf (_("This will be a self-signature.\n"));
998 tty_printf (_("WARNING: the signature will not be marked "
999 "as non-exportable.\n"));
1005 tty_printf (_("WARNING: the signature will not be marked "
1006 "as non-revocable.\n"));
1015 (_("The signature will be marked as non-exportable.\n"));
1022 (_("The signature will be marked as non-revocable.\n"));
1029 tty_printf (_("I have not checked this key at all.\n"));
1034 tty_printf (_("I have checked this key casually.\n"));
1039 tty_printf (_("I have checked this key very carefully.\n"));
1046 if (opt.batch && opt.answer_yes)
1048 else if (!cpr_get_answer_is_yes ("sign_uid.okay",
1049 _("Really sign? (y/N) ")))
1052 /* Now we can sign the user ids. */
1053 reloop: /* (Must use this, because we are modifing the list.) */
1055 for (node = keyblock; node; node = node->next)
1057 if (node->pkt->pkttype == PKT_PUBLIC_KEY)
1058 primary_pk = node->pkt->pkt.public_key;
1059 else if (node->pkt->pkttype == PKT_USER_ID
1060 && (node->flag & NODFLG_MARK_A))
1064 struct sign_attrib attrib;
1066 assert (primary_pk);
1067 memset (&attrib, 0, sizeof attrib);
1068 attrib.non_exportable = local;
1069 attrib.non_revocable = nonrevocable;
1070 attrib.trust_depth = trust_depth;
1071 attrib.trust_value = trust_value;
1072 attrib.trust_regexp = trust_regexp;
1073 node->flag &= ~NODFLG_MARK_A;
1075 /* We force creation of a v4 signature for local
1076 * signatures, otherwise we would not generate the
1077 * subpacket with v3 keys and the signature becomes
1081 rc = make_keysig_packet (&sig, primary_pk,
1082 node->pkt->pkt.user_id,
1085 0x13, 0, force_v4 ? 4 : 0, 0, 0,
1086 keygen_add_std_prefs, primary_pk);
1088 rc = make_keysig_packet (&sig, primary_pk,
1089 node->pkt->pkt.user_id,
1092 class, 0, force_v4 ? 4 : 0,
1093 timestamp, duration,
1094 sign_mk_attrib, &attrib);
1097 log_error (_("signing failed: %s\n"), g10_errstr (rc));
1101 *ret_modified = 1; /* We changed the keyblock. */
1104 pkt = xmalloc_clear (sizeof *pkt);
1105 pkt->pkttype = PKT_SIGNATURE;
1106 pkt->pkt.signature = sig;
1107 insert_kbnode (node, new_kbnode (pkt), PKT_SIGNATURE);
1112 /* Delete any sigs that got promoted */
1113 for (node = keyblock; node; node = node->next)
1114 if (node->flag & NODFLG_DELSIG)
1115 delete_kbnode (node);
1116 } /* End loop over signators. */
1119 release_sk_list (sk_list);
1125 * Change the passphrase of the primary and all secondary keys.
1126 * We use only one passphrase for all keys.
1129 change_passphrase (KBNODE keyblock, int *r_err)
1134 PKT_public_key *pksk;
1135 char *passphrase = NULL;
1136 int no_primary_secrets = 0;
1139 node = find_kbnode (keyblock, PKT_PUBLIC_KEY);
1142 log_error ("Oops; public key missing!\n");
1145 pksk = node->pkt->pkt.public_key;
1147 for (any = 0, node = keyblock; node; node = node->next)
1149 if (node->pkt->pkttype == PKT_PUBLIC_KEY
1150 || node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
1152 log_debug ("FIXME\n");
1153 /* PKT_public_key *tmpsk = node->pkt->pkt.public_key; */
1154 /* if (!(tmpsk->is_protected */
1155 /* && (tmpsk->protect.s2k.mode == 1001 */
1156 /* || tmpsk->protect.s2k.mode == 1002))) */
1165 tty_printf (_("Key has only stub or on-card key items - "
1166 "no passphrase to change.\n"));
1170 log_debug ("FIXME\n");
1171 /* See how to handle this key. */
1172 /* switch (is_secret_key_protected (pksk)) */
1175 /* rc = G10ERR_PUBKEY_ALGO; */
1178 /* tty_printf (_("This key is not protected.\n")); */
1181 /* if (sk->protect.s2k.mode == 1001) */
1183 /* tty_printf (_("Secret parts of key are not available.\n")); */
1184 /* no_primary_secrets = 1; */
1186 /* else if (sk->protect.s2k.mode == 1002) */
1188 /* tty_printf (_("Secret parts of key are stored on-card.\n")); */
1189 /* no_primary_secrets = 1; */
1195 /* tty_printf (_("Key is protected.\n")); */
1197 /* /\* Clear the passphrase cache so that the user is required */
1198 /* to enter the old passphrase. *\/ */
1199 /* keyid_from_pk (pksk, keyid); */
1200 /* passphrase_clear_cache (keyid, NULL, 0); */
1202 /* /\* rc = check_secret_key( sk, 0 ); *\/ */
1203 /* /\* if( !rc ) *\/ */
1204 /* /\* passphrase = get_last_passphrase(); *\/ */
1209 /* Unprotect all subkeys (use the supplied passphrase or ask) */
1210 for (node = keyblock; !rc && node; node = node->next)
1212 if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
1214 log_debug ("FIXME\n");
1215 /* PKT_pubic_key *subsk = node->pkt->pkt.public_key; */
1216 /* if (!(subsk->is_protected */
1217 /* && (subsk->protect.s2k.mode == 1001 */
1218 /* || subsk->protect.s2k.mode == 1002))) */
1220 /* set_next_passphrase (passphrase); */
1221 /* /\* rc = check_secret_key( subsk, 0 ); *\/ */
1222 /* /\* if( !rc && !passphrase ) *\/ */
1223 /* /\* passphrase = get_last_passphrase(); *\/ */
1229 tty_printf (_("Can't edit this key: %s\n"), g10_errstr (rc));
1233 STRING2KEY *s2k = xmalloc_secure (sizeof *s2k);
1234 const char *errtext = NULL;
1236 tty_printf (_("Enter the new passphrase for this secret key.\n\n"));
1238 set_next_passphrase (NULL);
1243 s2k->mode = opt.s2k_mode;
1244 s2k->hash_algo = S2K_DIGEST_ALGO;
1245 dek = passphrase_to_dek (NULL, 0, opt.s2k_cipher_algo,
1246 s2k, 2, errtext, &canceled);
1247 if (!dek && canceled)
1249 rc = GPG_ERR_CANCELED;
1254 errtext = N_("passphrase not correctly repeated; try again");
1255 tty_printf ("%s.\n", _(errtext));
1257 else if (!dek->keylen)
1260 tty_printf (_("You don't want a passphrase -"
1261 " this is probably a *bad* idea!\n\n"));
1262 if (cpr_get_answer_is_yes
1263 ("change_passwd.empty.okay",
1264 _("Do you really want to do this? (y/N) ")))
1273 if (!no_primary_secrets)
1275 /* sk->protect.algo = dek->algo; */
1276 /* sk->protect.s2k = *s2k; */
1278 /* rc = protect_secret_key( sk, dek ); */
1280 for (node = keyblock; !rc && node; node = node->next)
1282 if (node->pkt->pkttype == PKT_SECRET_SUBKEY)
1284 log_debug ("FIXME\n");
1285 /* PKT_secret_key *subsk = node->pkt->pkt.secret_key; */
1286 /* if (!(subsk->is_protected */
1287 /* && (subsk->protect.s2k.mode == 1001 */
1288 /* || subsk->protect.s2k.mode == 1002))) */
1290 /* subsk->protect.algo = dek->algo; */
1291 /* subsk->protect.s2k = *s2k; */
1292 /* #warning fixme */
1294 /* /\* rc = protect_secret_key( subsk, dek ); *\/ */
1299 log_error ("protect_secret_key failed: %s\n",
1305 /* Clear the cahce again so that the user is
1306 required to enter the new passphrase at the
1308 /* FIXME keyid_from_sk (sk, keyid); */
1309 passphrase_clear_cache (keyid, NULL, 0);
1322 set_next_passphrase (NULL);
1325 return changed && !rc;
1331 * There are some keys out (due to a bug in gnupg), where the sequence
1332 * of the packets is wrong. This function fixes that.
1333 * Returns: true if the keyblock has been fixed.
1335 * Note: This function does not work if there is more than one user ID.
1338 fix_keyblock (KBNODE keyblock)
1340 KBNODE node, last, subkey;
1343 /* Locate key signatures of class 0x10..0x13 behind sub key packets. */
1344 for (subkey = last = NULL, node = keyblock; node;
1345 last = node, node = node->next)
1347 switch (node->pkt->pkttype)
1349 case PKT_PUBLIC_SUBKEY:
1350 case PKT_SECRET_SUBKEY:
1352 subkey = last; /* Actually it is the one before the subkey. */
1357 PKT_signature *sig = node->pkt->pkt.signature;
1358 if (sig->sig_class >= 0x10 && sig->sig_class <= 0x13)
1360 log_info (_("moving a key signature to the correct place\n"));
1361 last->next = node->next;
1362 node->next = subkey->next;
1363 subkey->next = node;
1379 parse_sign_type (const char *str, int *localsig, int *nonrevokesig,
1382 const char *p = str;
1386 if (ascii_strncasecmp (p, "l", 1) == 0)
1391 else if (ascii_strncasecmp (p, "nr", 2) == 0)
1396 else if (ascii_strncasecmp (p, "t", 1) == 0)
1411 * Menu driven key editor. If seckey_check is true, then a secret key
1412 * that matches username will be looked for. If it is false, not all
1413 * commands will be available.
1415 * Note: to keep track of certain selections we use node->mark MARKBIT_xxxx.
1418 /* Need an SK for this command */
1419 #define KEYEDIT_NEED_SK 1
1420 /* Cannot be viewing the SK for this command */
1421 #define KEYEDIT_NOT_SK 2
1422 /* Must be viewing the SK for this command */
1423 #define KEYEDIT_ONLY_SK 4
1424 /* Match the tail of the string */
1425 #define KEYEDIT_TAIL_MATCH 8
1430 cmdQUIT, cmdHELP, cmdFPR, cmdLIST, cmdSELUID, cmdCHECK, cmdSIGN,
1431 cmdREVSIG, cmdREVKEY, cmdREVUID, cmdDELSIG, cmdPRIMARY, cmdDEBUG,
1432 cmdSAVE, cmdADDUID, cmdADDPHOTO, cmdDELUID, cmdADDKEY, cmdDELKEY,
1433 cmdADDREVOKER, cmdTOGGLE, cmdSELKEY, cmdPASSWD, cmdTRUST, cmdPREF,
1434 cmdEXPIRE, cmdBACKSIGN, cmdENABLEKEY, cmdDISABLEKEY, cmdSHOWPREF,
1435 cmdSETPREF, cmdPREFKS, cmdNOTATION, cmdINVCMD, cmdSHOWPHOTO, cmdUPDTRUST,
1436 cmdCHKTRUST, cmdADDCARDKEY, cmdKEYTOCARD, cmdBKUPTOCARD, cmdCHECKBKUPKEY,
1437 cmdCLEAN, cmdMINIMIZE, cmdNOP
1448 { "quit", cmdQUIT, 0, N_("quit this menu")},
1449 { "q", cmdQUIT, 0, NULL},
1450 { "save", cmdSAVE, 0, N_("save and quit")},
1451 { "help", cmdHELP, 0, N_("show this help")},
1452 { "?", cmdHELP, 0, NULL},
1453 { "fpr", cmdFPR, 0, N_("show key fingerprint")},
1454 { "list", cmdLIST, 0, N_("list key and user IDs")},
1455 { "l", cmdLIST, 0, NULL},
1456 { "uid", cmdSELUID, 0, N_("select user ID N")},
1457 { "key", cmdSELKEY, 0, N_("select subkey N")},
1458 { "check", cmdCHECK, 0, N_("check signatures")},
1459 { "c", cmdCHECK, 0, NULL},
1460 { "cross-certify", cmdBACKSIGN, KEYEDIT_NOT_SK | KEYEDIT_NEED_SK, NULL},
1461 { "backsign", cmdBACKSIGN, KEYEDIT_NOT_SK | KEYEDIT_NEED_SK, NULL},
1462 { "sign", cmdSIGN, KEYEDIT_NOT_SK | KEYEDIT_TAIL_MATCH,
1463 N_("sign selected user IDs [* see below for related commands]")},
1464 { "s", cmdSIGN, KEYEDIT_NOT_SK, NULL},
1465 /* "lsign" and friends will never match since "sign" comes first
1466 and it is a tail match. They are just here so they show up in
1468 { "lsign", cmdNOP, 0, N_("sign selected user IDs locally")},
1469 { "tsign", cmdNOP, 0, N_("sign selected user IDs with a trust signature")},
1470 { "nrsign", cmdNOP, 0,
1471 N_("sign selected user IDs with a non-revocable signature")},
1472 { "debug", cmdDEBUG, 0, NULL},
1473 { "adduid", cmdADDUID, KEYEDIT_NOT_SK | KEYEDIT_NEED_SK, N_("add a user ID")},
1474 { "addphoto", cmdADDPHOTO, KEYEDIT_NOT_SK | KEYEDIT_NEED_SK,
1475 N_("add a photo ID")},
1476 { "deluid", cmdDELUID, KEYEDIT_NOT_SK, N_("delete selected user IDs")},
1477 /* delphoto is really deluid in disguise */
1478 { "delphoto", cmdDELUID, KEYEDIT_NOT_SK, NULL},
1479 { "addkey", cmdADDKEY, KEYEDIT_NOT_SK | KEYEDIT_NEED_SK, N_("add a subkey")},
1480 #ifdef ENABLE_CARD_SUPPORT
1481 { "addcardkey", cmdADDCARDKEY, KEYEDIT_NOT_SK | KEYEDIT_NEED_SK,
1482 N_("add a key to a smartcard")},
1483 { "keytocard", cmdKEYTOCARD, KEYEDIT_NEED_SK | KEYEDIT_ONLY_SK,
1484 N_("move a key to a smartcard")},
1485 { "bkuptocard", cmdBKUPTOCARD, KEYEDIT_NEED_SK | KEYEDIT_ONLY_SK,
1486 N_("move a backup key to a smartcard")},
1487 { "checkbkupkey", cmdCHECKBKUPKEY, KEYEDIT_NEED_SK | KEYEDIT_ONLY_SK, NULL},
1488 #endif /*ENABLE_CARD_SUPPORT */
1489 { "delkey", cmdDELKEY, KEYEDIT_NOT_SK, N_("delete selected subkeys")},
1490 { "addrevoker", cmdADDREVOKER, KEYEDIT_NOT_SK | KEYEDIT_NEED_SK,
1491 N_("add a revocation key")},
1492 { "delsig", cmdDELSIG, KEYEDIT_NOT_SK,
1493 N_("delete signatures from the selected user IDs")},
1494 { "expire", cmdEXPIRE, KEYEDIT_NOT_SK | KEYEDIT_NEED_SK,
1495 N_("change the expiration date for the key or selected subkeys")},
1496 { "primary", cmdPRIMARY, KEYEDIT_NOT_SK | KEYEDIT_NEED_SK,
1497 N_("flag the selected user ID as primary")},
1498 { "toggle", cmdTOGGLE, KEYEDIT_NEED_SK,
1499 N_("toggle between the secret and public key listings")},
1500 { "t", cmdTOGGLE, KEYEDIT_NEED_SK, NULL},
1501 { "pref", cmdPREF, KEYEDIT_NOT_SK, N_("list preferences (expert)")},
1502 { "showpref", cmdSHOWPREF, KEYEDIT_NOT_SK, N_("list preferences (verbose)")},
1503 { "setpref", cmdSETPREF, KEYEDIT_NOT_SK | KEYEDIT_NEED_SK,
1504 N_("set preference list for the selected user IDs")},
1505 { "updpref", cmdSETPREF, KEYEDIT_NOT_SK | KEYEDIT_NEED_SK, NULL},
1506 { "keyserver", cmdPREFKS, KEYEDIT_NOT_SK | KEYEDIT_NEED_SK,
1507 N_("set the preferred keyserver URL for the selected user IDs")},
1508 { "notation", cmdNOTATION, KEYEDIT_NOT_SK | KEYEDIT_NEED_SK,
1509 N_("set a notation for the selected user IDs")},
1510 { "passwd", cmdPASSWD, KEYEDIT_NOT_SK | KEYEDIT_NEED_SK,
1511 N_("change the passphrase")},
1512 { "password", cmdPASSWD, KEYEDIT_NOT_SK | KEYEDIT_NEED_SK, NULL},
1513 { "trust", cmdTRUST, KEYEDIT_NOT_SK, N_("change the ownertrust")},
1514 { "revsig", cmdREVSIG, KEYEDIT_NOT_SK,
1515 N_("revoke signatures on the selected user IDs")},
1516 { "revuid", cmdREVUID, KEYEDIT_NOT_SK | KEYEDIT_NEED_SK,
1517 N_("revoke selected user IDs")},
1518 { "revphoto", cmdREVUID, KEYEDIT_NOT_SK | KEYEDIT_NEED_SK, NULL},
1519 { "revkey", cmdREVKEY, KEYEDIT_NOT_SK | KEYEDIT_NEED_SK,
1520 N_("revoke key or selected subkeys")},
1521 { "enable", cmdENABLEKEY, KEYEDIT_NOT_SK, N_("enable key")},
1522 { "disable", cmdDISABLEKEY, KEYEDIT_NOT_SK, N_("disable key")},
1523 { "showphoto", cmdSHOWPHOTO, 0, N_("show selected photo IDs")},
1524 { "clean", cmdCLEAN, KEYEDIT_NOT_SK,
1525 N_("compact unusable user IDs and remove unusable signatures from key")},
1526 { "minimize", cmdMINIMIZE, KEYEDIT_NOT_SK,
1527 N_("compact unusable user IDs and remove all signatures from key")},
1529 { NULL, cmdNONE, 0, NULL}
1534 #ifdef HAVE_LIBREADLINE
1537 These two functions are used by readline for command completion.
1541 command_generator (const char *text, int state)
1543 static int list_index, len;
1546 /* If this is a new word to complete, initialize now. This includes
1547 saving the length of TEXT for efficiency, and initializing the
1548 index variable to 0. */
1552 len = strlen (text);
1555 /* Return the next partial match */
1556 while ((name = cmds[list_index].name))
1558 /* Only complete commands that have help text */
1559 if (cmds[list_index++].desc && strncmp (name, text, len) == 0)
1560 return strdup (name);
1567 keyedit_completion (const char *text, int start, int end)
1569 /* If we are at the start of a line, we try and command-complete.
1570 If not, just do nothing for now. */
1575 return rl_completion_matches (text, command_generator);
1577 rl_attempted_completion_over = 1;
1581 #endif /* HAVE_LIBREADLINE */
1585 /* Main function of the menu driven key editor. */
1587 keyedit_menu (const char *username, strlist_t locusr,
1588 strlist_t commands, int quiet, int seckey_check)
1590 enum cmdids cmd = 0;
1591 gpg_error_t err = 0;
1592 KBNODE keyblock = NULL;
1593 KEYDB_HANDLE kdbhd = NULL;
1594 int have_seckey = 0;
1595 char *answer = NULL;
1599 int have_commands = !!commands;
1600 ctrl_t ctrl = NULL; /* Dummy for now. */
1602 if (opt.command_fd != -1)
1604 else if (opt.batch && !have_commands)
1606 log_error (_("can't do this in batch mode\n"));
1610 #ifdef HAVE_W32_SYSTEM
1611 /* Due to Windows peculiarities we need to make sure that the
1612 trustdb stale check is done before we open another file
1613 (i.e. by searching for a key). In theory we could make sure
1614 that the files are closed after use but the open/close caches
1615 inhibits that and flushing the cache right before the stale
1616 check is not easy to implement. Thus we take the easy way out
1617 and run the stale check as early as possible. Note, that for
1618 non- W32 platforms it is run indirectly trough a call to
1620 check_trustdb_stale ();
1623 /* Get the public key */
1624 err = get_pubkey_byname (NULL, NULL, username, &keyblock, &kdbhd, 1, 1);
1627 if (fix_keyblock (keyblock))
1629 if (collapse_uids (&keyblock))
1631 reorder_keyblock (keyblock);
1632 /* We modified the keyblock, so let's make sure the flags are
1635 merge_keys_and_selfsig (keyblock);
1637 /* See whether we have a matching secret key. */
1640 have_seckey = have_any_secret_key (ctrl, keyblock);
1641 if (have_seckey && !quiet)
1642 tty_printf (_("Secret key is available.\n"));
1647 /* Main command loop. */
1650 int i, arg_number, photo;
1651 const char *arg_string = "";
1653 PKT_public_key *pk = keyblock->pkt->pkt.public_key;
1657 if (redisplay && !quiet)
1659 show_key_with_all_names (keyblock, 0, 1, 0, 1, 0);
1670 answer = xstrdup (commands->d);
1671 commands = commands->next;
1675 answer = xstrdup ("quit");
1682 #ifdef HAVE_LIBREADLINE
1683 tty_enable_completion (keyedit_completion);
1685 answer = cpr_get_no_help ("keyedit.prompt", "gpg> ");
1687 tty_disable_completion ();
1689 trim_spaces (answer);
1691 while (*answer == '#');
1693 arg_number = 0; /* Here is the init which egcc complains about. */
1694 photo = 0; /* Same here. */
1697 else if (*answer == CONTROL_D)
1699 else if (digitp (answer))
1702 arg_number = atoi (answer);
1706 if ((p = strchr (answer, ' ')))
1709 trim_spaces (answer);
1711 arg_number = atoi (p);
1715 for (i = 0; cmds[i].name; i++)
1717 if (cmds[i].flags & KEYEDIT_TAIL_MATCH)
1719 size_t l = strlen (cmds[i].name);
1720 size_t a = strlen (answer);
1723 if (!ascii_strcasecmp (&answer[a - l], cmds[i].name))
1725 answer[a - l] = '\0';
1730 else if (!ascii_strcasecmp (answer, cmds[i].name))
1733 if ((cmds[i].flags & KEYEDIT_NEED_SK) && !have_seckey)
1735 tty_printf (_("Need the secret key to do this.\n"));
1738 else if (((cmds[i].flags & KEYEDIT_NOT_SK) && have_seckey && toggle)
1739 || ((cmds[i].flags & KEYEDIT_ONLY_SK) && have_seckey
1742 tty_printf (_("Please use the command \"toggle\" first.\n"));
1749 /* Dispatch the command. */
1753 for (i = 0; cmds[i].name; i++)
1755 if ((cmds[i].flags & KEYEDIT_NEED_SK) && !have_seckey)
1756 ; /* Skip those item if we do not have the secret key. */
1757 else if (cmds[i].desc)
1758 tty_printf ("%-11s %s\n", cmds[i].name, _(cmds[i].desc));
1763 (_("* The `sign' command may be prefixed with an `l' for local "
1764 "signatures (lsign),\n"
1765 " a `t' for trust signatures (tsign), an `nr' for "
1766 "non-revocable signatures\n"
1767 " (nrsign), or any combination thereof (ltsign, "
1768 "tnrsign, etc.).\n"));
1776 show_key_and_fingerprint (keyblock);
1780 if (strlen (arg_string) == NAMEHASH_LEN * 2)
1781 redisplay = menu_select_uid_namehash (keyblock, arg_string);
1784 if (*arg_string == '*'
1785 && (!arg_string[1] || spacep (arg_string + 1)))
1786 arg_number = -1; /* Select all. */
1787 redisplay = menu_select_uid (keyblock, arg_number);
1793 if (*arg_string == '*'
1794 && (!arg_string[1] || spacep (arg_string + 1)))
1795 arg_number = -1; /* Select all. */
1796 if (menu_select_key (keyblock, arg_number))
1802 check_all_keysigs (keyblock, count_selected_uids (keyblock));
1807 int localsig = 0, nonrevokesig = 0, trustsig = 0, interactive = 0;
1811 tty_printf (_("Key is revoked."));
1816 if (!cpr_get_answer_is_yes
1817 ("keyedit.sign_revoked.okay",
1818 _("Are you sure you still want to sign it? (y/N) ")))
1823 tty_printf (_(" Unable to sign.\n"));
1828 if (count_uids (keyblock) > 1 && !count_selected_uids (keyblock)
1829 && !cpr_get_answer_is_yes ("keyedit.sign_all.okay",
1830 _("Really sign all user IDs?"
1833 if (opt.interactive)
1837 tty_printf (_("Hint: Select the user IDs to sign\n"));
1843 /* What sort of signing are we doing? */
1844 if (!parse_sign_type
1845 (answer, &localsig, &nonrevokesig, &trustsig))
1847 tty_printf (_("Unknown signature type `%s'\n"), answer);
1851 sign_uids (keyblock, locusr, &modified,
1852 localsig, nonrevokesig, trustsig, interactive);
1857 dump_kbnode (keyblock);
1861 /* The toggle command is a leftover from old gpg versions
1862 where we worked with a secret and a public keyring. It
1863 is not necessary anymore but we keep this command for the
1864 sake of scripts using it. */
1870 if (RFC2440 || RFC1991 || PGP2)
1872 tty_printf (_("This command is not allowed while in %s mode.\n"),
1873 compliance_option_string ());
1879 if (menu_adduid (keyblock, photo, arg_string))
1884 merge_keys_and_selfsig (keyblock);
1892 if (!(n1 = count_selected_uids (keyblock)))
1893 tty_printf (_("You must select at least one user ID.\n"));
1894 else if (real_uids_left (keyblock) < 1)
1895 tty_printf (_("You can't delete the last user ID!\n"));
1896 else if (cpr_get_answer_is_yes
1897 ("keyedit.remove.uid.okay",
1898 n1 > 1 ? _("Really remove all selected user IDs? (y/N) ")
1899 : _("Really remove this user ID? (y/N) ")))
1901 menu_deluid (keyblock);
1912 if (!(n1 = count_selected_uids (keyblock)))
1913 tty_printf (_("You must select at least one user ID.\n"));
1914 else if (menu_delsig (keyblock))
1916 /* No redisplay here, because it may scroll away some
1917 * of the status output of this command. */
1924 if (!generate_subkeypair (keyblock))
1928 merge_keys_and_selfsig (keyblock);
1932 #ifdef ENABLE_CARD_SUPPORT
1934 if (card_generate_subkey (keyblock))
1938 merge_keys_and_selfsig (keyblock);
1945 switch (count_selected_keys (keyblock))
1948 if (cpr_get_answer_is_yes
1949 ("keyedit.keytocard.use_primary",
1950 /* TRANSLATORS: Please take care: This is about
1951 moving the key and not about removing it. */
1952 _("Really move the primary key? (y/N) ")))
1956 for (node = keyblock; node; node = node->next)
1958 if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY
1959 && node->flag & NODFLG_SELKEY)
1964 tty_printf (_("You must select exactly one key.\n"));
1969 PKT_public_key *xxpk = find_pk_from_sknode (keyblock, node);
1970 if (card_store_subkey (node, xxpk ? xxpk->pubkey_usage : 0))
1973 /* Only the secret key has been modified; thus
1974 there is no need to set the modified flag. */
1981 case cmdCHECKBKUPKEY:
1982 log_debug ("FIXME: This needs to be changed\n");
1984 /* Ask for a filename, check whether this is really a
1985 backup key as generated by the card generation, parse
1986 that key and store it on card. */
1995 tty_printf (_("Command expects a filename argument\n"));
1999 /* Open that file. */
2000 a = iobuf_open (fname);
2001 if (a && is_secured_file (iobuf_get_fd (a)))
2005 gpg_err_set_errno (EPERM);
2009 tty_printf (_("Can't open `%s': %s\n"),
2010 fname, strerror (errno));
2014 /* Parse and check that file. */
2015 pkt = xmalloc (sizeof *pkt);
2017 err = parse_packet (a, pkt);
2019 iobuf_ioctl (NULL, IOBUF_IOCTL_INVALIDATE_CACHE, 0, (char *) fname);
2020 if (!err && pkt->pkttype != PKT_SECRET_KEY
2021 && pkt->pkttype != PKT_SECRET_SUBKEY)
2022 err = G10ERR_NO_SECKEY;
2025 tty_printf (_("Error reading backup key from `%s': %s\n"),
2026 fname, g10_errstr (err));
2031 node = new_kbnode (pkt);
2033 if (cmd == cmdCHECKBKUPKEY)
2035 PKT_secret_key *sk = node->pkt->pkt.secret_key;
2036 switch (is_secret_key_protected (sk))
2038 case 0: /* Not protected. */
2039 tty_printf (_("This key is not protected.\n"));
2042 log_error (_("unknown key protection algorithm\n"));
2045 if (sk->protect.s2k.mode == 1001)
2046 tty_printf (_("Secret parts of key"
2047 " are not available.\n"));
2048 if (sk->protect.s2k.mode == 1002)
2049 tty_printf (_("Secret parts of key"
2050 " are stored on-card.\n"));
2052 /* check_secret_key (sk, 0); */
2055 else /* Store it. */
2057 if (card_store_subkey (node, 0))
2060 /* FIXME:sec_modified = 1;*/
2063 release_kbnode (node);
2067 #endif /* ENABLE_CARD_SUPPORT */
2073 if (!(n1 = count_selected_keys (keyblock)))
2074 tty_printf (_("You must select at least one key.\n"));
2075 else if (!cpr_get_answer_is_yes
2076 ("keyedit.remove.subkey.okay",
2077 n1 > 1 ? _("Do you really want to delete the "
2078 "selected keys? (y/N) ")
2079 : _("Do you really want to delete this key? (y/N) ")))
2083 menu_delkey (keyblock);
2094 if (ascii_strcasecmp (arg_string, "sensitive") == 0)
2096 if (menu_addrevoker (keyblock, sensitive))
2100 merge_keys_and_selfsig (keyblock);
2109 if (!(n1 = count_selected_uids (keyblock)))
2110 tty_printf (_("You must select at least one user ID.\n"));
2111 else if (cpr_get_answer_is_yes
2112 ("keyedit.revoke.uid.okay",
2113 n1 > 1 ? _("Really revoke all selected user IDs? (y/N) ")
2114 : _("Really revoke this user ID? (y/N) ")))
2116 if (menu_revuid (keyblock))
2129 if (!(n1 = count_selected_keys (keyblock)))
2131 if (cpr_get_answer_is_yes ("keyedit.revoke.subkey.okay",
2132 _("Do you really want to revoke"
2133 " the entire key? (y/N) ")))
2135 if (menu_revkey (keyblock))
2141 else if (cpr_get_answer_is_yes ("keyedit.revoke.subkey.okay",
2143 _("Do you really want to revoke"
2144 " the selected subkeys? (y/N) ")
2145 : _("Do you really want to revoke"
2146 " this subkey? (y/N) ")))
2148 if (menu_revsubkey (keyblock))
2155 merge_keys_and_selfsig (keyblock);
2160 if (menu_expire (keyblock))
2162 merge_keys_and_selfsig (keyblock);
2169 if (menu_backsign (keyblock))
2177 if (menu_set_primary_uid (keyblock))
2179 merge_keys_and_selfsig (keyblock);
2186 change_passphrase (keyblock, NULL);
2190 if (opt.trust_model == TM_EXTERNAL)
2192 tty_printf (_("Owner trust may not be set while "
2193 "using a user provided trust database\n"));
2197 show_key_with_all_names (keyblock, 0, 0, 0, 1, 0);
2199 if (edit_ownertrust (find_kbnode (keyblock,
2200 PKT_PUBLIC_KEY)->pkt->pkt.
2204 /* No real need to set update_trust here as
2205 edit_ownertrust() calls revalidation_mark()
2213 int count = count_selected_uids (keyblock);
2214 assert (keyblock->pkt->pkttype == PKT_PUBLIC_KEY);
2215 show_names (keyblock, keyblock->pkt->pkt.public_key,
2216 count ? NODFLG_SELUID : 0, 1);
2222 int count = count_selected_uids (keyblock);
2223 assert (keyblock->pkt->pkttype == PKT_PUBLIC_KEY);
2224 show_names (keyblock, keyblock->pkt->pkt.public_key,
2225 count ? NODFLG_SELUID : 0, 2);
2231 PKT_user_id *tempuid;
2233 keygen_set_std_prefs (!*arg_string ? "default" : arg_string, 0);
2235 tempuid = keygen_get_std_prefs ();
2236 tty_printf (_("Set preference list to:\n"));
2237 show_prefs (tempuid, NULL, 1);
2238 free_user_id (tempuid);
2240 if (cpr_get_answer_is_yes
2241 ("keyedit.setpref.okay",
2242 count_selected_uids (keyblock) ?
2243 _("Really update the preferences"
2244 " for the selected user IDs? (y/N) ")
2245 : _("Really update the preferences? (y/N) ")))
2247 if (menu_set_preferences (keyblock))
2249 merge_keys_and_selfsig (keyblock);
2258 if (menu_set_keyserver_url (*arg_string ? arg_string : NULL,
2261 merge_keys_and_selfsig (keyblock);
2268 if (menu_set_notation (*arg_string ? arg_string : NULL,
2271 merge_keys_and_selfsig (keyblock);
2281 if (menu_revsig (keyblock))
2290 if (enable_disable_key (keyblock, cmd == cmdDISABLEKEY))
2298 menu_showphoto (keyblock);
2302 if (menu_clean (keyblock, 0))
2303 redisplay = modified = 1;
2307 if (menu_clean (keyblock, 1))
2308 redisplay = modified = 1;
2316 if (!cpr_get_answer_is_yes ("keyedit.save.okay",
2317 _("Save changes? (y/N) ")))
2320 || cpr_get_answer_is_yes ("keyedit.cancel.okay",
2321 _("Quit without saving? (y/N) ")))
2329 err = keydb_update_keyblock (kdbhd, keyblock);
2332 log_error (_("update failed: %s\n"), g10_errstr (err));
2337 tty_printf (_("Key not changed so no update needed.\n"));
2341 revalidation_mark ();
2349 tty_printf (_("Invalid command (try \"help\")\n"));
2352 } /* End of the main command loop. */
2355 release_kbnode (keyblock);
2356 keydb_release (kdbhd);
2361 /* Change the passphrase of the secret key identified by USERNAME. */
2363 keyedit_passwd (const char *username)
2367 unsigned char fpr[MAX_FINGERPRINT_LEN];
2369 KEYDB_HANDLE kdh = NULL;
2370 kbnode_t keyblock = NULL;
2372 pk = xtrycalloc (1, sizeof *pk);
2375 err = gpg_error_from_syserror ();
2378 err = getkey_byname (NULL, pk, username, 1, NULL);
2381 fingerprint_from_pk (pk, fpr, &fprlen);
2382 while (fprlen < MAX_FINGERPRINT_LEN)
2385 /* FIXME: Call an agent function instead. */
2387 kdh = NULL /*keydb_new (1)*/;
2390 err = gpg_error (GPG_ERR_GENERAL);
2394 err = keydb_search_fpr (kdh, fpr);
2395 if (err == -1 || gpg_err_code (err) == GPG_ERR_EOF)
2396 err = gpg_error (GPG_ERR_NO_SECKEY);
2400 err = keydb_get_keyblock (kdh, &keyblock);
2404 if (!change_passphrase (keyblock, &err))
2407 err = keydb_update_keyblock (kdh, keyblock);
2409 log_error (_("update secret failed: %s\n"), gpg_strerror (err));
2412 release_kbnode (keyblock);
2414 free_public_key (pk);
2415 keydb_release (kdh);
2418 log_info ("error changing the passphrase for `%s': %s\n",
2419 username, gpg_strerror (err));
2420 write_status_error ("keyedit.passwd", err);
2423 write_status_text (STATUS_SUCCESS, "keyedit.passwd");
2428 tty_print_notations (int indent, PKT_signature * sig)
2431 struct notation *notation, *nd;
2439 notation = sig_to_notation (sig);
2441 for (nd = notation; nd; nd = nd->next)
2444 tty_printf ("%*s", indent, "");
2448 tty_print_utf8_string (nd->name, strlen (nd->name));
2450 tty_print_utf8_string (nd->value, strlen (nd->value));
2454 free_notation (notation);
2459 * Show preferences of a public keyblock.
2462 show_prefs (PKT_user_id * uid, PKT_signature * selfsig, int verbose)
2464 const prefitem_t fake = { 0, 0 };
2465 const prefitem_t *prefs;
2480 int any, des_seen = 0, sha1_seen = 0, uncomp_seen = 0;
2483 tty_printf (_("Cipher: "));
2484 for (i = any = 0; prefs[i].type; i++)
2486 if (prefs[i].type == PREFTYPE_SYM)
2491 /* We don't want to display strings for experimental algos */
2492 if (!openpgp_cipher_test_algo (prefs[i].value)
2493 && prefs[i].value < 100)
2494 tty_printf ("%s", openpgp_cipher_algo_name (prefs[i].value));
2496 tty_printf ("[%d]", prefs[i].value);
2497 if (prefs[i].value == CIPHER_ALGO_3DES)
2505 tty_printf ("%s", openpgp_cipher_algo_name (CIPHER_ALGO_3DES));
2508 tty_printf (_("Digest: "));
2509 for (i = any = 0; prefs[i].type; i++)
2511 if (prefs[i].type == PREFTYPE_HASH)
2516 /* We don't want to display strings for experimental algos */
2517 if (!gcry_md_test_algo (prefs[i].value) && prefs[i].value < 100)
2518 tty_printf ("%s", gcry_md_algo_name (prefs[i].value));
2520 tty_printf ("[%d]", prefs[i].value);
2521 if (prefs[i].value == DIGEST_ALGO_SHA1)
2529 tty_printf ("%s", gcry_md_algo_name (DIGEST_ALGO_SHA1));
2532 tty_printf (_("Compression: "));
2533 for (i = any = 0; prefs[i].type; i++)
2535 if (prefs[i].type == PREFTYPE_ZIP)
2537 const char *s = compress_algo_to_string (prefs[i].value);
2542 /* We don't want to display strings for experimental algos */
2543 if (s && prefs[i].value < 100)
2544 tty_printf ("%s", s);
2546 tty_printf ("[%d]", prefs[i].value);
2547 if (prefs[i].value == COMPRESS_ALGO_NONE)
2557 tty_printf ("%s", compress_algo_to_string (COMPRESS_ALGO_ZIP));
2560 tty_printf ("%s", compress_algo_to_string (COMPRESS_ALGO_NONE));
2562 if (uid->flags.mdc || !uid->flags.ks_modify)
2565 tty_printf (_("Features: "));
2572 if (!uid->flags.ks_modify)
2576 tty_printf (_("Keyserver no-modify"));
2583 const byte *pref_ks;
2586 pref_ks = parse_sig_subpkt (selfsig->hashed,
2587 SIGSUBPKT_PREF_KS, &pref_ks_len);
2588 if (pref_ks && pref_ks_len)
2591 tty_printf (_("Preferred keyserver: "));
2592 tty_print_utf8_string (pref_ks, pref_ks_len);
2596 if (selfsig->flags.notation)
2599 tty_printf (_("Notations: "));
2600 tty_print_notations (5 + strlen (_("Notations: ")), selfsig);
2607 for (i = 0; prefs[i].type; i++)
2609 tty_printf (" %c%d", prefs[i].type == PREFTYPE_SYM ? 'S' :
2610 prefs[i].type == PREFTYPE_HASH ? 'H' :
2611 prefs[i].type == PREFTYPE_ZIP ? 'Z' : '?',
2615 tty_printf (" [mdc]");
2616 if (!uid->flags.ks_modify)
2617 tty_printf (" [no-ks-modify]");
2623 /* This is the version of show_key_with_all_names used when
2624 opt.with_colons is used. It prints all available data in a easy to
2625 parse format and does not translate utf8 */
2627 show_key_with_all_names_colon (KBNODE keyblock)
2630 int i, j, ulti_hack = 0;
2631 byte pk_version = 0;
2632 PKT_public_key *primary = NULL;
2635 for (node = keyblock; node; node = node->next)
2637 if (node->pkt->pkttype == PKT_PUBLIC_KEY
2638 || (node->pkt->pkttype == PKT_PUBLIC_SUBKEY))
2640 PKT_public_key *pk = node->pkt->pkt.public_key;
2643 if (node->pkt->pkttype == PKT_PUBLIC_KEY)
2645 pk_version = pk->version;
2649 keyid_from_pk (pk, keyid);
2651 fputs (node->pkt->pkttype == PKT_PUBLIC_KEY ? "pub:" : "sub:",
2655 else if (pk->is_revoked)
2657 else if (pk->has_expired)
2659 else if (!(opt.fast_list_mode || opt.no_expensive_trust_checks))
2661 int trust = get_validity_info (pk, NULL);
2667 printf (":%u:%d:%08lX%08lX:%lu:%lu::",
2670 (ulong) keyid[0], (ulong) keyid[1],
2671 (ulong) pk->timestamp, (ulong) pk->expiredate);
2672 if (node->pkt->pkttype == PKT_PUBLIC_KEY
2673 && !(opt.fast_list_mode || opt.no_expensive_trust_checks))
2674 putchar (get_ownertrust_info (pk));
2678 /* Print capabilities. */
2679 if ((pk->pubkey_usage & PUBKEY_USAGE_ENC))
2681 if ((pk->pubkey_usage & PUBKEY_USAGE_SIG))
2683 if ((pk->pubkey_usage & PUBKEY_USAGE_CERT))
2685 if ((pk->pubkey_usage & PUBKEY_USAGE_AUTH))
2689 print_fingerprint (pk, 0);
2690 print_revokers (pk);
2696 for (node = keyblock; node; node = node->next)
2698 if (node->pkt->pkttype == PKT_USER_ID)
2700 PKT_user_id *uid = node->pkt->pkt.user_id;
2704 if (uid->attrib_data)
2709 if (uid->is_revoked)
2710 printf ("r::::::::");
2711 else if (uid->is_expired)
2712 printf ("e::::::::");
2713 else if (opt.fast_list_mode || opt.no_expensive_trust_checks)
2714 printf ("::::::::");
2719 if (primary && !ulti_hack)
2720 uid_validity = get_validity_info (primary, uid);
2723 printf ("%c::::::::", uid_validity);
2726 if (uid->attrib_data)
2727 printf ("%u %lu", uid->numattribs, uid->attrib_len);
2729 es_write_sanitized (es_stdout, uid->name, uid->len, ":", NULL);
2732 /* signature class */
2737 if (pk_version > 3 || uid->selfsigversion > 3)
2739 const prefitem_t *prefs = uid->prefs;
2741 for (j = 0; prefs && prefs[j].type; j++)
2745 printf ("%c%d", prefs[j].type == PREFTYPE_SYM ? 'S' :
2746 prefs[j].type == PREFTYPE_HASH ? 'H' :
2747 prefs[j].type == PREFTYPE_ZIP ? 'Z' : '?',
2752 if (!uid->flags.ks_modify)
2753 printf (",no-ks-modify");
2758 if (uid->is_primary)
2760 if (uid->is_revoked)
2762 if (uid->is_expired)
2764 if ((node->flag & NODFLG_SELUID))
2766 if ((node->flag & NODFLG_MARK_A))
2775 show_names (KBNODE keyblock, PKT_public_key * pk, unsigned int flag,
2781 for (node = keyblock; node; node = node->next)
2783 if (node->pkt->pkttype == PKT_USER_ID && !is_deleted_kbnode (node))
2785 PKT_user_id *uid = node->pkt->pkt.user_id;
2787 if (!flag || (flag && (node->flag & flag)))
2789 if (!(flag & NODFLG_MARK_A) && pk)
2790 tty_printf ("%s ", uid_trust_string_fixed (pk, uid));
2792 if (flag & NODFLG_MARK_A)
2794 else if (node->flag & NODFLG_SELUID)
2795 tty_printf ("(%d)* ", i);
2796 else if (uid->is_primary)
2797 tty_printf ("(%d). ", i);
2799 tty_printf ("(%d) ", i);
2800 tty_print_utf8_string (uid->name, uid->len);
2802 if (with_prefs && pk)
2804 if (pk->version > 3 || uid->selfsigversion > 3)
2806 PKT_signature *selfsig = NULL;
2809 for (signode = node->next;
2810 signode && signode->pkt->pkttype == PKT_SIGNATURE;
2811 signode = signode->next)
2813 if (signode->pkt->pkt.signature->
2814 flags.chosen_selfsig)
2816 selfsig = signode->pkt->pkt.signature;
2821 show_prefs (uid, selfsig, with_prefs == 2);
2824 tty_printf (_("There are no preferences on a"
2825 " PGP 2.x-style user ID.\n"));
2834 * Display the key a the user ids, if only_marked is true, do only
2835 * so for user ids with mark A flag set and dont display the index number
2838 show_key_with_all_names (KBNODE keyblock, int only_marked, int with_revoker,
2839 int with_fpr, int with_subkeys, int with_prefs)
2844 byte pk_version = 0;
2845 PKT_public_key *primary = NULL;
2847 if (opt.with_colons)
2849 show_key_with_all_names_colon (keyblock);
2854 for (node = keyblock; node; node = node->next)
2856 if (node->pkt->pkttype == PKT_PUBLIC_KEY
2857 || (with_subkeys && node->pkt->pkttype == PKT_PUBLIC_SUBKEY
2858 && !is_deleted_kbnode (node)))
2860 PKT_public_key *pk = node->pkt->pkt.public_key;
2861 const char *otrust = "err", *trust = "err";
2863 if (node->pkt->pkttype == PKT_PUBLIC_KEY)
2865 /* do it here, so that debug messages don't clutter the
2867 static int did_warn = 0;
2869 trust = get_validity_string (pk, NULL);
2870 otrust = get_ownertrust_string (pk);
2872 /* Show a warning once */
2874 && (get_validity (pk, NULL) & TRUST_FLAG_PENDING_CHECK))
2880 pk_version = pk->version;
2886 char *user = get_user_id_string_native (pk->revoked.keyid);
2887 const char *algo = gcry_pk_algo_name (pk->revoked.algo);
2888 tty_printf (_("This key was revoked on %s by %s key %s\n"),
2889 revokestr_from_pk (pk), algo ? algo : "?", user);
2895 if (!pk->revkey && pk->numrevkeys)
2898 for (i = 0; i < pk->numrevkeys; i++)
2904 algo = gcry_pk_algo_name (pk->revkey[i].algid);
2905 keyid_from_fingerprint (pk->revkey[i].fpr,
2906 MAX_FINGERPRINT_LEN, r_keyid);
2908 user = get_user_id_string_native (r_keyid);
2909 tty_printf (_("This key may be revoked by %s key %s"),
2910 algo ? algo : "?", user);
2912 if (pk->revkey[i].class & 0x40)
2915 tty_printf (_("(sensitive)"));
2923 keyid_from_pk (pk, NULL);
2924 tty_printf ("%s%c %4u%c/%s ",
2925 node->pkt->pkttype == PKT_PUBLIC_KEY ? "pub" : "sub",
2926 (node->flag & NODFLG_SELKEY) ? '*' : ' ',
2928 pubkey_letter (pk->pubkey_algo), keystr (pk->keyid));
2930 tty_printf (_("created: %s"), datestr_from_pk (pk));
2933 tty_printf (_("revoked: %s"), revokestr_from_pk (pk));
2934 else if (pk->has_expired)
2935 tty_printf (_("expired: %s"), expirestr_from_pk (pk));
2937 tty_printf (_("expires: %s"), expirestr_from_pk (pk));
2939 tty_printf (_("usage: %s"), usagestr_from_pk (pk));
2942 if (node->pkt->pkttype == PKT_PUBLIC_KEY)
2944 if (opt.trust_model != TM_ALWAYS)
2946 tty_printf ("%*s", (int) keystrlen () + 13, "");
2947 /* Ownertrust is only meaningful for the PGP or
2948 classic trust models */
2949 if (opt.trust_model == TM_PGP
2950 || opt.trust_model == TM_CLASSIC)
2952 int width = 14 - strlen (otrust);
2955 tty_printf (_("trust: %s"), otrust);
2956 tty_printf ("%*s", width, "");
2959 tty_printf (_("validity: %s"), trust);
2962 if (node->pkt->pkttype == PKT_PUBLIC_KEY
2963 && (get_ownertrust (pk) & TRUST_FLAG_DISABLED))
2965 tty_printf ("*** ");
2966 tty_printf (_("This key has been disabled"));
2971 if (node->pkt->pkttype == PKT_PUBLIC_KEY && with_fpr)
2973 print_fingerprint (pk, 2);
2977 else if (node->pkt->pkttype == PKT_SECRET_KEY
2978 || (with_subkeys && node->pkt->pkttype == PKT_SECRET_SUBKEY))
2980 PKT_secret_key *sk = node->pkt->pkt.secret_key;
2981 tty_printf ("%s%c %4u%c/%s ",
2982 node->pkt->pkttype == PKT_SECRET_KEY ? "sec" : "ssb",
2983 (node->flag & NODFLG_SELKEY) ? '*' : ' ',
2985 pubkey_letter (sk->pubkey_algo), keystr_from_sk (sk));
2986 tty_printf (_("created: %s"), datestr_from_sk (sk));
2988 tty_printf (_("expires: %s"), expirestr_from_sk (sk));
2990 if (sk->is_protected && sk->protect.s2k.mode == 1002)
2993 tty_printf (_("card-no: "));
2994 if (sk->protect.ivlen == 16
2995 && !memcmp (sk->protect.iv, "\xD2\x76\x00\x01\x24\x01", 6))
2996 { /* This is an OpenPGP card. */
2997 for (i = 8; i < 14; i++)
3001 tty_printf ("%02X", sk->protect.iv[i]);
3005 { /* Something is wrong: Print all. */
3006 for (i = 0; i < sk->protect.ivlen; i++)
3007 tty_printf ("%02X", sk->protect.iv[i]);
3014 show_names (keyblock, primary, only_marked ? NODFLG_MARK_A : 0, with_prefs);
3017 tty_printf (_("Please note that the shown key validity"
3018 " is not necessarily correct\n"
3019 "unless you restart the program.\n"));
3023 /* Display basic key information. This function is suitable to show
3024 information on the key without any dependencies on the trustdb or
3025 any other internal GnuPG stuff. KEYBLOCK may either be a public or
3028 show_basic_key_info (KBNODE keyblock)
3033 /* The primary key */
3034 for (node = keyblock; node; node = node->next)
3036 if (node->pkt->pkttype == PKT_PUBLIC_KEY)
3038 PKT_public_key *pk = node->pkt->pkt.public_key;
3040 /* Note, we use the same format string as in other show
3041 functions to make the translation job easier. */
3042 tty_printf ("%s %4u%c/%s ",
3043 node->pkt->pkttype == PKT_PUBLIC_KEY ? "pub" : "sub",
3045 pubkey_letter (pk->pubkey_algo), keystr_from_pk (pk));
3046 tty_printf (_("created: %s"), datestr_from_pk (pk));
3048 tty_printf (_("expires: %s"), expirestr_from_pk (pk));
3050 print_fingerprint (pk, 3);
3053 else if (node->pkt->pkttype == PKT_SECRET_KEY)
3055 PKT_secret_key *sk = node->pkt->pkt.secret_key;
3056 tty_printf ("%s %4u%c/%s",
3057 node->pkt->pkttype == PKT_SECRET_KEY ? "sec" : "ssb",
3059 pubkey_letter (sk->pubkey_algo), keystr_from_sk (sk));
3060 tty_printf (_("created: %s"), datestr_from_sk (sk));
3062 tty_printf (_("expires: %s"), expirestr_from_sk (sk));
3064 log_debug ("FIXME\n");
3065 /* print_fingerprint (NULL, sk, 3); */
3071 for (i = 0, node = keyblock; node; node = node->next)
3073 if (node->pkt->pkttype == PKT_USER_ID)
3075 PKT_user_id *uid = node->pkt->pkt.user_id;
3079 if (uid->is_revoked)
3080 tty_printf ("[%s] ", _("revoked"));
3081 else if (uid->is_expired)
3082 tty_printf ("[%s] ", _("expired"));
3083 tty_print_utf8_string (uid->name, uid->len);
3090 show_key_and_fingerprint (KBNODE keyblock)
3093 PKT_public_key *pk = NULL;
3095 for (node = keyblock; node; node = node->next)
3097 if (node->pkt->pkttype == PKT_PUBLIC_KEY)
3099 pk = node->pkt->pkt.public_key;
3100 tty_printf ("pub %4u%c/%s %s ",
3102 pubkey_letter (pk->pubkey_algo),
3103 keystr_from_pk (pk), datestr_from_pk (pk));
3105 else if (node->pkt->pkttype == PKT_USER_ID)
3107 PKT_user_id *uid = node->pkt->pkt.user_id;
3108 tty_print_utf8_string (uid->name, uid->len);
3114 print_fingerprint (pk, 2);
3118 /* Show a warning if no uids on the key have the primary uid flag
3121 no_primary_warning (KBNODE keyblock)
3124 int have_primary = 0, uid_count = 0;
3126 /* TODO: if we ever start behaving differently with a primary or
3127 non-primary attribute ID, we will need to check for attributes
3130 for (node = keyblock; node; node = node->next)
3132 if (node->pkt->pkttype == PKT_USER_ID
3133 && node->pkt->pkt.user_id->attrib_data == NULL)
3137 if (node->pkt->pkt.user_id->is_primary == 2)
3145 if (uid_count > 1 && !have_primary)
3147 ("WARNING: no user ID has been marked as primary. This command"
3148 " may\n cause a different user ID to become"
3149 " the assumed primary.\n"));
3154 * Ask for a new user id, add the self-signature and update the keyblock.
3155 * Return true if there is a new user id
3158 menu_adduid (KBNODE pub_keyblock, int photo, const char *photo_name)
3161 PKT_public_key *pk = NULL;
3162 PKT_signature *sig = NULL;
3165 KBNODE pub_where = NULL;
3168 for (node = pub_keyblock; node; pub_where = node, node = node->next)
3170 if (node->pkt->pkttype == PKT_PUBLIC_KEY)
3171 pk = node->pkt->pkt.public_key;
3172 else if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
3175 if (!node) /* No subkey. */
3183 for (node = pub_keyblock; node; node = node->next)
3184 if (node->pkt->pkttype == PKT_USER_ID &&
3185 node->pkt->pkt.user_id->attrib_data != NULL)
3191 /* It is legal but bad for compatibility to add a photo ID to a
3192 v3 key as it means that PGP2 will not be able to use that key
3193 anymore. Also, PGP may not expect a photo on a v3 key.
3194 Don't bother to ask this if the key already has a photo - any
3195 damage has already been done at that point. -dms */
3196 if (pk->version == 3 && !hasattrib)
3200 tty_printf (_("WARNING: This is a PGP2-style key. "
3201 "Adding a photo ID may cause some versions\n"
3202 " of PGP to reject this key.\n"));
3204 if (!cpr_get_answer_is_yes ("keyedit.v3_photo.okay",
3205 _("Are you sure you still want "
3206 "to add it? (y/N) ")))
3211 tty_printf (_("You may not add a photo ID to "
3212 "a PGP2-style key.\n"));
3217 uid = generate_photo_id (pk, photo_name);
3220 uid = generate_user_id (pub_keyblock);
3224 err = make_keysig_packet (&sig, pk, uid, NULL, pk, 0x13, 0, 0, 0, 0,
3225 keygen_add_std_prefs, pk);
3228 log_error ("signing failed: %s\n", g10_errstr (err));
3233 /* Insert/append to public keyblock */
3234 pkt = xmalloc_clear (sizeof *pkt);
3235 pkt->pkttype = PKT_USER_ID;
3236 pkt->pkt.user_id = uid;
3237 node = new_kbnode (pkt);
3239 insert_kbnode (pub_where, node, 0);
3241 add_kbnode (pub_keyblock, node);
3242 pkt = xmalloc_clear (sizeof *pkt);
3243 pkt->pkttype = PKT_SIGNATURE;
3244 pkt->pkt.signature = copy_signature (NULL, sig);
3246 insert_kbnode (node, new_kbnode (pkt), 0);
3248 add_kbnode (pub_keyblock, new_kbnode (pkt));
3254 * Remove all selected userids from the keyring
3257 menu_deluid (KBNODE pub_keyblock)
3262 for (node = pub_keyblock; node; node = node->next)
3264 if (node->pkt->pkttype == PKT_USER_ID)
3266 selected = node->flag & NODFLG_SELUID;
3269 /* Only cause a trust update if we delete a
3270 non-revoked user id */
3271 if (!node->pkt->pkt.user_id->is_revoked)
3273 delete_kbnode (node);
3276 else if (selected && node->pkt->pkttype == PKT_SIGNATURE)
3277 delete_kbnode (node);
3278 else if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
3281 commit_kbnode (&pub_keyblock);
3286 menu_delsig (KBNODE pub_keyblock)
3289 PKT_user_id *uid = NULL;
3292 for (node = pub_keyblock; node; node = node->next)
3294 if (node->pkt->pkttype == PKT_USER_ID)
3296 uid = (node->flag & NODFLG_SELUID) ? node->pkt->pkt.user_id : NULL;
3298 else if (uid && node->pkt->pkttype == PKT_SIGNATURE)
3300 int okay, valid, selfsig, inv_sig, no_key, other_err;
3302 tty_printf ("uid ");
3303 tty_print_utf8_string (uid->name, uid->len);
3306 okay = inv_sig = no_key = other_err = 0;
3307 if (opt.with_colons)
3308 valid = print_and_check_one_sig_colon (pub_keyblock, node,
3310 &other_err, &selfsig, 1);
3312 valid = print_and_check_one_sig (pub_keyblock, node,
3313 &inv_sig, &no_key, &other_err,
3318 okay = cpr_get_answer_yes_no_quit
3319 ("keyedit.delsig.valid",
3320 _("Delete this good signature? (y/N/q)"));
3322 /* Only update trust if we delete a good signature.
3323 The other two cases do not affect trust. */
3327 else if (inv_sig || other_err)
3328 okay = cpr_get_answer_yes_no_quit
3329 ("keyedit.delsig.invalid",
3330 _("Delete this invalid signature? (y/N/q)"));
3332 okay = cpr_get_answer_yes_no_quit
3333 ("keyedit.delsig.unknown",
3334 _("Delete this unknown signature? (y/N/q)"));
3339 && !cpr_get_answer_is_yes
3340 ("keyedit.delsig.selfsig",
3341 _("Really delete this self-signature? (y/N)")))
3345 delete_kbnode (node);