2 * Copyright (C) 1996,1997 Michael R. Elkins <me@mutt.org>
3 * Copyright (c) 1998,1999 Thomas Roessler <roessler@does-not-exist.org>
5 * This program is free software; you can redistribute it
6 * and/or modify it under the terms of the GNU General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later
11 * This program is distributed in the hope that it will be
12 * useful, but WITHOUT ANY WARRANTY; without even the implied
13 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public
18 * License along with this program; if not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111, USA.
28 #include "mutt_curses.h"
29 #include "mutt_menu.h"
44 #ifdef CRYPT_BACKEND_CLASSIC_PGP
50 struct pgp_cache *next;
53 static struct pgp_cache *id_defaults = NULL;
55 static char trust_flags[] = "?- +";
57 static char *pgp_key_abilities (int flags)
61 if (!(flags & KEYFLAG_CANENCRYPT))
63 else if (flags & KEYFLAG_PREFER_SIGNING)
68 if (!(flags & KEYFLAG_CANSIGN))
70 else if (flags & KEYFLAG_PREFER_ENCRYPTION)
80 static char pgp_flags (int flags)
82 if (flags & KEYFLAG_REVOKED)
84 else if (flags & KEYFLAG_EXPIRED)
86 else if (flags & KEYFLAG_DISABLED)
88 else if (flags & KEYFLAG_CRITICAL)
94 static pgp_key_t pgp_principal_key (pgp_key_t key)
96 if (key->flags & KEYFLAG_SUBKEY && key->parent)
103 * Format an entry on the PGP key selection menu.
106 * %k key id %K key id of the principal key
108 * %a algorithm %A algorithm of the princ. key
109 * %l length %L length of the princ. key
110 * %f flags %F flags of the princ. key
111 * %c capabilities %C capabilities of the princ. key
112 * %t trust/validity of the key-uid association
113 * %[...] date of key using strftime(3)
116 typedef struct pgp_entry
122 static const char *pgp_entry_fmt (char *dest,
127 const char *ifstring,
128 const char *elsestring,
137 int optional = (flags & M_FORMAT_OPTIONAL);
139 entry = (pgp_entry_t *) data;
142 pkey = pgp_principal_key (key);
144 if (isupper ((unsigned char) op))
147 kflags = key->flags | (pkey->flags & KEYFLAG_RESTRICTIONS)
150 switch (ascii_tolower (op))
156 char buf2[SHORT_STRING], *p;
173 while (len > 0 && *cp != ']')
185 break; /* not enough space */
196 if (do_locales && Locale)
197 setlocale (LC_TIME, Locale);
199 tm = localtime (&key->gen_time);
201 strftime (buf2, sizeof (buf2), dest, tm);
204 setlocale (LC_TIME, "C");
206 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
207 snprintf (dest, destlen, fmt, buf2);
215 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
216 snprintf (dest, destlen, fmt, entry->num);
222 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
223 snprintf (dest, destlen, fmt, _pgp_keyid (key));
229 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
230 snprintf (dest, destlen, fmt, uid->addr);
236 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
237 snprintf (dest, destlen, fmt, key->algorithm);
243 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
244 snprintf (dest, destlen, fmt, key->keylen);
250 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
251 snprintf (dest, destlen, fmt, pgp_flags (kflags));
253 else if (!(kflags & (KEYFLAG_RESTRICTIONS)))
259 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
260 snprintf (dest, destlen, fmt, pgp_key_abilities (kflags));
262 else if (!(kflags & (KEYFLAG_ABILITIES)))
268 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
269 snprintf (dest, destlen, fmt, trust_flags[uid->trust & 0x03]);
271 else if (!(uid->trust & 0x03))
272 /* undefined trust */
280 mutt_FormatString (dest, destlen, ifstring, mutt_attach_fmt, data, 0);
281 else if (flags & M_FORMAT_OPTIONAL)
282 mutt_FormatString (dest, destlen, elsestring, mutt_attach_fmt, data, 0);
286 static void pgp_entry (char *s, size_t l, MUTTMENU * menu, int num)
288 pgp_uid_t **KeyTable = (pgp_uid_t **) menu->data;
291 entry.uid = KeyTable[num];
294 mutt_FormatString (s, l, NONULL (PgpEntryFormat), pgp_entry_fmt,
295 (unsigned long) &entry, M_FORMAT_ARROWCURSOR);
298 static int _pgp_compare_address (const void *a, const void *b)
302 pgp_uid_t **s = (pgp_uid_t **) a;
303 pgp_uid_t **t = (pgp_uid_t **) b;
305 if ((r = mutt_strcasecmp ((*s)->addr, (*t)->addr)))
308 return (mutt_strcasecmp (_pgp_keyid ((*s)->parent),
309 _pgp_keyid ((*t)->parent)) > 0);
312 static int pgp_compare_address (const void *a, const void *b)
314 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_address (a, b)
315 : _pgp_compare_address (a, b));
320 static int _pgp_compare_keyid (const void *a, const void *b)
324 pgp_uid_t **s = (pgp_uid_t **) a;
325 pgp_uid_t **t = (pgp_uid_t **) b;
327 if ((r = mutt_strcasecmp (_pgp_keyid ((*s)->parent),
328 _pgp_keyid ((*t)->parent))))
331 return (mutt_strcasecmp ((*s)->addr, (*t)->addr)) > 0;
334 static int pgp_compare_keyid (const void *a, const void *b)
336 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_keyid (a, b)
337 : _pgp_compare_keyid (a, b));
340 static int _pgp_compare_date (const void *a, const void *b)
343 pgp_uid_t **s = (pgp_uid_t **) a;
344 pgp_uid_t **t = (pgp_uid_t **) b;
346 if ((r = ((*s)->parent->gen_time - (*t)->parent->gen_time)))
348 return (mutt_strcasecmp ((*s)->addr, (*t)->addr)) > 0;
351 static int pgp_compare_date (const void *a, const void *b)
353 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_date (a, b)
354 : _pgp_compare_date (a, b));
357 static int _pgp_compare_trust (const void *a, const void *b)
361 pgp_uid_t **s = (pgp_uid_t **) a;
362 pgp_uid_t **t = (pgp_uid_t **) b;
364 if ((r = (((*s)->parent->flags & (KEYFLAG_RESTRICTIONS))
365 - ((*t)->parent->flags & (KEYFLAG_RESTRICTIONS)))))
367 if ((r = ((*s)->trust - (*t)->trust)))
369 if ((r = ((*s)->parent->keylen - (*t)->parent->keylen)))
371 if ((r = ((*s)->parent->gen_time - (*t)->parent->gen_time)))
373 if ((r = mutt_strcasecmp ((*s)->addr, (*t)->addr)))
375 return (mutt_strcasecmp (_pgp_keyid ((*s)->parent),
376 _pgp_keyid ((*t)->parent))) > 0;
379 static int pgp_compare_trust (const void *a, const void *b)
381 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_trust (a, b)
382 : _pgp_compare_trust (a, b));
385 static int pgp_key_is_valid (pgp_key_t k)
387 pgp_key_t pk = pgp_principal_key (k);
388 if (k->flags & KEYFLAG_CANTUSE)
390 if (pk->flags & KEYFLAG_CANTUSE)
396 static int pgp_id_is_strong (pgp_uid_t *uid)
398 if ((uid->trust & 3) < 3)
404 static int pgp_id_is_valid (pgp_uid_t *uid)
406 if (!pgp_key_is_valid (uid->parent))
408 if (uid->flags & KEYFLAG_CANTUSE)
414 #define PGP_KV_VALID 1
415 #define PGP_KV_ADDR 2
416 #define PGP_KV_STRING 4
417 #define PGP_KV_STRONGID 8
419 #define PGP_KV_MATCH (PGP_KV_ADDR|PGP_KV_STRING)
421 static int pgp_id_matches_addr (ADDRESS *addr, ADDRESS *u_addr, pgp_uid_t *uid)
425 if (pgp_id_is_valid (uid))
428 if (pgp_id_is_strong (uid))
429 rv |= PGP_KV_STRONGID;
431 if (addr->mailbox && u_addr->mailbox
432 && mutt_strcasecmp (addr->mailbox, u_addr->mailbox) == 0)
435 if (addr->personal && u_addr->personal
436 && mutt_strcasecmp (addr->personal, u_addr->personal) == 0)
442 static pgp_key_t pgp_select_key (pgp_key_t keys,
443 ADDRESS * p, const char *s)
446 pgp_uid_t **KeyTable;
449 char helpstr[SHORT_STRING], buf[LONG_STRING], tmpbuf[STRING];
450 char cmd[LONG_STRING], tempfile[_POSIX_PATH_MAX];
455 int (*f) (const void *, const void *);
462 for (i = 0, kp = keys; kp; kp = kp->next)
464 if (!option (OPTPGPSHOWUNUSABLE) && (kp->flags & KEYFLAG_CANTUSE))
470 for (a = kp->address; a; a = a->next)
472 if (!option (OPTPGPSHOWUNUSABLE) && (a->flags & KEYFLAG_CANTUSE))
481 safe_realloc (&KeyTable, sizeof (pgp_uid_t *) * keymax);
490 mutt_error _("All matching keys are expired, revoked, or disabled.");
495 switch (PgpSortKeys & SORT_MASK)
498 f = pgp_compare_date;
501 f = pgp_compare_keyid;
504 f = pgp_compare_address;
508 f = pgp_compare_trust;
511 qsort (KeyTable, i, sizeof (pgp_uid_t *), f);
514 mutt_make_help (buf, sizeof (buf), _("Exit "), MENU_PGP, OP_EXIT);
515 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
516 mutt_make_help (buf, sizeof (buf), _("Select "), MENU_PGP,
517 OP_GENERIC_SELECT_ENTRY);
518 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
519 mutt_make_help (buf, sizeof (buf), _("Check key "), MENU_PGP, OP_VERIFY_KEY);
520 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
521 mutt_make_help (buf, sizeof (buf), _("Help"), MENU_PGP, OP_HELP);
522 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
524 menu = mutt_new_menu ();
526 menu->make_entry = pgp_entry;
527 menu->menu = MENU_PGP;
528 menu->help = helpstr;
529 menu->data = KeyTable;
532 snprintf (buf, sizeof (buf), _("PGP keys matching <%s>."), p->mailbox);
534 snprintf (buf, sizeof (buf), _("PGP keys matching \"%s\"."), s);
545 switch (mutt_menuLoop (menu))
550 mutt_mktemp (tempfile);
551 if ((devnull = fopen ("/dev/null", "w")) == NULL) /* __FOPEN_CHECKED__ */
553 mutt_perror _("Can't open /dev/null");
556 if ((fp = safe_fopen (tempfile, "w")) == NULL)
559 mutt_perror _("Can't create temporary file");
563 mutt_message _("Invoking PGP...");
565 snprintf (tmpbuf, sizeof (tmpbuf), "0x%s", pgp_keyid (pgp_principal_key (KeyTable[menu->current]->parent)));
567 if ((thepid = pgp_invoke_verify_key (NULL, NULL, NULL, -1,
568 fileno (fp), fileno (devnull), tmpbuf)) == -1)
570 mutt_perror _("Can't create filter");
576 mutt_wait_filter (thepid);
580 snprintf (cmd, sizeof (cmd), _("Key ID: 0x%s"),
581 pgp_keyid (pgp_principal_key (KeyTable[menu->current]->parent)));
582 mutt_do_pager (cmd, tempfile, 0, NULL);
583 menu->redraw = REDRAW_FULL;
589 mutt_message ("%s", KeyTable[menu->current]->addr);
592 case OP_GENERIC_SELECT_ENTRY:
595 /* XXX make error reporting more verbose */
597 if (option (OPTPGPCHECKTRUST))
598 if (!pgp_key_is_valid (KeyTable[menu->current]->parent))
600 mutt_error _("This key can't be used: expired/disabled/revoked.");
604 if (option (OPTPGPCHECKTRUST) &&
605 (!pgp_id_is_valid (KeyTable[menu->current])
606 || !pgp_id_is_strong (KeyTable[menu->current])))
609 char buff[LONG_STRING];
611 if (KeyTable[menu->current]->flags & KEYFLAG_CANTUSE)
612 s = N_("ID is expired/disabled/revoked.");
613 else switch (KeyTable[menu->current]->trust & 0x03)
616 s = N_("ID has undefined validity.");
619 s = N_("ID is not valid.");
622 s = N_("ID is only marginally valid.");
626 snprintf (buff, sizeof (buff), _("%s Do you really want to use the key?"),
629 if (mutt_yesorno (buff, M_NO) != M_YES)
637 kp = pgp_principal_key (KeyTable[menu->current]->parent);
639 kp = KeyTable[menu->current]->parent;
652 mutt_menuDestroy (&menu);
655 set_option (OPTNEEDREDRAW);
660 pgp_key_t pgp_ask_for_key (char *tag, char *whatfor,
661 short abilities, pgp_ring_t keyring)
664 char resp[SHORT_STRING];
665 struct pgp_cache *l = NULL;
673 for (l = id_defaults; l; l = l->next)
674 if (!mutt_strcasecmp (whatfor, l->what))
676 strfcpy (resp, NONULL (l->dflt), sizeof (resp));
685 if (mutt_get_field (tag, resp, sizeof (resp), M_CLEAR) != 0)
691 mutt_str_replace (&l->dflt, resp);
694 l = safe_malloc (sizeof (struct pgp_cache));
695 l->next = id_defaults;
697 l->what = safe_strdup (whatfor);
698 l->dflt = safe_strdup (resp);
702 if ((key = pgp_getkeybystr (resp, abilities, keyring)))
710 /* generate a public key attachment */
712 BODY *pgp_make_key_attachment (char *tempf)
715 char buff[LONG_STRING];
716 char tempfb[_POSIX_PATH_MAX], tmp[STRING];
722 unset_option (OPTPGPCHECKTRUST);
724 key = pgp_ask_for_key (_("Please enter the key ID: "), NULL, 0, PGP_PUBRING);
726 if (!key) return NULL;
728 snprintf (tmp, sizeof (tmp), "0x%s", pgp_keyid (pgp_principal_key (key)));
733 mutt_mktemp (tempfb);
737 if ((tempfp = safe_fopen (tempf, tempf == tempfb ? "w" : "a")) == NULL)
739 mutt_perror _("Can't create temporary file");
743 if ((devnull = fopen ("/dev/null", "w")) == NULL) /* __FOPEN_CHECKED__ */
745 mutt_perror _("Can't open /dev/null");
752 mutt_message _("Invoking pgp...");
756 pgp_invoke_export (NULL, NULL, NULL, -1,
757 fileno (tempfp), fileno (devnull), tmp)) == -1)
759 mutt_perror _("Can't create filter");
766 mutt_wait_filter (thepid);
771 att = mutt_new_body ();
772 att->filename = safe_strdup (tempf);
775 att->type = TYPEAPPLICATION;
776 att->subtype = safe_strdup ("pgp-keys");
777 snprintf (buff, sizeof (buff), _("PGP Key %s."), tmp);
778 att->description = safe_strdup (buff);
779 mutt_update_encoding (att);
782 att->length = sb.st_size;
787 static LIST *pgp_add_string_to_hints (LIST *hints, const char *str)
792 if ((scratch = safe_strdup (str)) == NULL)
795 for (t = strtok (scratch, " ,.:\"()<>\n"); t;
796 t = strtok (NULL, " ,.:\"()<>\n"))
799 hints = mutt_add_list (hints, t);
806 static pgp_key_t *pgp_get_lastp (pgp_key_t p)
808 for (; p; p = p->next)
815 pgp_key_t pgp_getkeybyaddr (ADDRESS * a, short abilities, pgp_ring_t keyring)
823 int this_key_has_strong;
824 int this_key_has_weak;
825 int this_key_has_invalid;
828 pgp_key_t keys, k, kn;
829 pgp_key_t the_valid_key = NULL;
830 pgp_key_t matches = NULL;
831 pgp_key_t *last = &matches;
835 hints = pgp_add_string_to_hints (hints, a->mailbox);
836 if (a && a->personal)
837 hints = pgp_add_string_to_hints (hints, a->personal);
839 mutt_message (_("Looking for keys matching \"%s\"..."), a->mailbox);
840 keys = pgp_get_candidates (keyring, hints);
842 mutt_free_list (&hints);
847 dprint (5, (debugfile, "pgp_getkeybyaddr: looking for %s <%s>.",
848 a->personal, a->mailbox));
851 for (k = keys; k; k = kn)
855 dprint (5, (debugfile, " looking at key: %s\n",
858 if (abilities && !(k->flags & abilities))
860 dprint (5, (debugfile, " insufficient abilities: Has %x, want %x\n",
861 k->flags, abilities));
865 this_key_has_weak = 0; /* weak but valid match */
866 this_key_has_invalid = 0; /* invalid match */
867 this_key_has_strong = 0; /* strong and valid match */
868 match = 0; /* any match */
870 for (q = k->address; q; q = q->next)
872 r = rfc822_parse_adrlist (NULL, q->addr);
874 for (p = r; p; p = p->next)
876 int validity = pgp_id_matches_addr (a, p, q);
878 if (validity & PGP_KV_MATCH) /* something matches */
881 /* is this key a strong candidate? */
882 if ((validity & PGP_KV_VALID) && (validity & PGP_KV_STRONGID)
883 && (validity & PGP_KV_ADDR))
885 if (the_valid_key && the_valid_key != k)
888 this_key_has_strong = 1;
890 else if ((validity & PGP_KV_MATCH) && !(validity & PGP_KV_VALID))
891 this_key_has_invalid = 1;
892 else if ((validity & PGP_KV_MATCH)
893 && (!(validity & PGP_KV_STRONGID) || !(validity & PGP_KV_ADDR)))
894 this_key_has_weak = 1;
897 rfc822_free_address (&r);
900 if (match && !this_key_has_strong && this_key_has_invalid)
902 if (match && !this_key_has_strong && this_key_has_weak)
907 *last = pgp_principal_key (k);
908 kn = pgp_remove_key (&keys, *last);
909 last = pgp_get_lastp (k);
913 pgp_free_key (&keys);
917 if (the_valid_key && !multi /* && !weak
918 && !(invalid && option (OPTPGPSHOWUNUSABLE)) */)
921 * There was precisely one strong match on a valid ID.
923 * Proceed without asking the user.
925 pgp_remove_key (&matches, the_valid_key);
926 pgp_free_key (&matches);
932 * Else: Ask the user.
934 if ((k = pgp_select_key (matches, a, NULL)))
935 pgp_remove_key (&matches, k);
936 pgp_free_key (&matches);
945 pgp_key_t pgp_getkeybystr (char *p, short abilities, pgp_ring_t keyring)
949 pgp_key_t matches = NULL;
950 pgp_key_t *last = &matches;
955 mutt_message (_("Looking for keys matching \"%s\"..."), p);
957 hints = pgp_add_string_to_hints (hints, p);
958 keys = pgp_get_candidates (keyring, hints);
959 mutt_free_list (&hints);
965 for (k = keys; k; k = kn)
968 if (abilities && !(k->flags & abilities))
973 for (a = k->address; a; a = a->next)
975 dprint (5, (debugfile, "pgp_getkeybystr: matching \"%s\" against key %s, \"%s\": ",
976 p, pgp_keyid (k), a->addr));
977 if (!*p || mutt_strcasecmp (p, pgp_keyid (k)) == 0 ||
978 (!mutt_strncasecmp (p, "0x", 2) && !mutt_strcasecmp (p + 2, pgp_keyid (k))) ||
979 (option (OPTPGPLONGIDS) && !mutt_strncasecmp (p, "0x", 2) &&
980 !mutt_strcasecmp (p + 2, k->keyid + 8)) ||
981 mutt_stristr (a->addr, p))
983 dprint (5, (debugfile, "match.\n"));
991 *last = pgp_principal_key (k);
992 kn = pgp_remove_key (&keys, *last);
993 last = pgp_get_lastp (k);
997 pgp_free_key (&keys);
1001 if ((k = pgp_select_key (matches, NULL, p)))
1002 pgp_remove_key (&matches, k);
1004 pgp_free_key (&matches);
1013 #endif /* CRYPT_BACKEND_CLASSIC_PGP */