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
49 struct pgp_cache *next;
52 static struct pgp_cache *id_defaults = NULL;
54 static char trust_flags[] = "?- +";
56 static char *pgp_key_abilities (int flags)
60 if (!(flags & KEYFLAG_CANENCRYPT))
62 else if (flags & KEYFLAG_PREFER_SIGNING)
67 if (!(flags & KEYFLAG_CANSIGN))
69 else if (flags & KEYFLAG_PREFER_ENCRYPTION)
79 static char pgp_flags (int flags)
81 if (flags & KEYFLAG_REVOKED)
83 else if (flags & KEYFLAG_EXPIRED)
85 else if (flags & KEYFLAG_DISABLED)
87 else if (flags & KEYFLAG_CRITICAL)
93 static pgp_key_t pgp_principal_key (pgp_key_t key)
95 if (key->flags & KEYFLAG_SUBKEY && key->parent)
102 * Format an entry on the PGP key selection menu.
105 * %k key id %K key id of the principal key
107 * %a algorithm %A algorithm of the princ. key
108 * %l length %L length of the princ. key
109 * %f flags %F flags of the princ. key
110 * %c capabilities %C capabilities of the princ. key
111 * %t trust/validity of the key-uid association
112 * %[...] date of key using strftime(3)
115 typedef struct pgp_entry {
120 static const char *pgp_entry_fmt (char *dest,
125 const char *ifstring,
126 const char *elsestring,
127 unsigned long data, format_flag flags)
134 int optional = (flags & M_FORMAT_OPTIONAL);
136 entry = (pgp_entry_t *) data;
139 pkey = pgp_principal_key (key);
141 if (isupper ((unsigned char) op))
144 kflags = key->flags | (pkey->flags & KEYFLAG_RESTRICTIONS)
147 switch (ascii_tolower (op)) {
152 char buf2[SHORT_STRING], *p;
168 while (len > 0 && *cp != ']') {
177 break; /* not enough space */
187 if (do_locales && Locale)
188 setlocale (LC_TIME, Locale);
190 tm = localtime (&key->gen_time);
192 strftime (buf2, sizeof (buf2), dest, tm);
195 setlocale (LC_TIME, "C");
197 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
198 snprintf (dest, destlen, fmt, buf2);
205 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
206 snprintf (dest, destlen, fmt, entry->num);
211 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
212 snprintf (dest, destlen, fmt, _pgp_keyid (key));
217 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
218 snprintf (dest, destlen, fmt, uid->addr);
223 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
224 snprintf (dest, destlen, fmt, key->algorithm);
229 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
230 snprintf (dest, destlen, fmt, key->keylen);
235 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
236 snprintf (dest, destlen, fmt, pgp_flags (kflags));
238 else if (!(kflags & (KEYFLAG_RESTRICTIONS)))
243 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
244 snprintf (dest, destlen, fmt, pgp_key_abilities (kflags));
246 else if (!(kflags & (KEYFLAG_ABILITIES)))
251 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
252 snprintf (dest, destlen, fmt, trust_flags[uid->trust & 0x03]);
254 else if (!(uid->trust & 0x03))
255 /* undefined trust */
263 mutt_FormatString (dest, destlen, ifstring, mutt_attach_fmt, data, 0);
264 else if (flags & M_FORMAT_OPTIONAL)
265 mutt_FormatString (dest, destlen, elsestring, mutt_attach_fmt, data, 0);
269 static void pgp_entry (char *s, size_t l, MUTTMENU * menu, int num)
271 pgp_uid_t **KeyTable = (pgp_uid_t **) menu->data;
274 entry.uid = KeyTable[num];
277 mutt_FormatString (s, l, NONULL (PgpEntryFormat), pgp_entry_fmt,
278 (unsigned long) &entry, M_FORMAT_ARROWCURSOR);
281 static int _pgp_compare_address (const void *a, const void *b)
285 pgp_uid_t **s = (pgp_uid_t **) a;
286 pgp_uid_t **t = (pgp_uid_t **) b;
288 if ((r = mutt_strcasecmp ((*s)->addr, (*t)->addr)))
291 return (mutt_strcasecmp (_pgp_keyid ((*s)->parent),
292 _pgp_keyid ((*t)->parent)) > 0);
295 static int pgp_compare_address (const void *a, const void *b)
297 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_address (a, b)
298 : _pgp_compare_address (a, b));
303 static int _pgp_compare_keyid (const void *a, const void *b)
307 pgp_uid_t **s = (pgp_uid_t **) a;
308 pgp_uid_t **t = (pgp_uid_t **) b;
310 if ((r = mutt_strcasecmp (_pgp_keyid ((*s)->parent),
311 _pgp_keyid ((*t)->parent))))
314 return (mutt_strcasecmp ((*s)->addr, (*t)->addr)) > 0;
317 static int pgp_compare_keyid (const void *a, const void *b)
319 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_keyid (a, b)
320 : _pgp_compare_keyid (a, b));
323 static int _pgp_compare_date (const void *a, const void *b)
326 pgp_uid_t **s = (pgp_uid_t **) a;
327 pgp_uid_t **t = (pgp_uid_t **) b;
329 if ((r = ((*s)->parent->gen_time - (*t)->parent->gen_time)))
331 return (mutt_strcasecmp ((*s)->addr, (*t)->addr)) > 0;
334 static int pgp_compare_date (const void *a, const void *b)
336 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_date (a, b)
337 : _pgp_compare_date (a, b));
340 static int _pgp_compare_trust (const void *a, const void *b)
344 pgp_uid_t **s = (pgp_uid_t **) a;
345 pgp_uid_t **t = (pgp_uid_t **) b;
347 if ((r = (((*s)->parent->flags & (KEYFLAG_RESTRICTIONS))
348 - ((*t)->parent->flags & (KEYFLAG_RESTRICTIONS)))))
350 if ((r = ((*s)->trust - (*t)->trust)))
352 if ((r = ((*s)->parent->keylen - (*t)->parent->keylen)))
354 if ((r = ((*s)->parent->gen_time - (*t)->parent->gen_time)))
356 if ((r = mutt_strcasecmp ((*s)->addr, (*t)->addr)))
358 return (mutt_strcasecmp (_pgp_keyid ((*s)->parent),
359 _pgp_keyid ((*t)->parent))) > 0;
362 static int pgp_compare_trust (const void *a, const void *b)
364 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_trust (a, b)
365 : _pgp_compare_trust (a, b));
368 static int pgp_key_is_valid (pgp_key_t k)
370 pgp_key_t pk = pgp_principal_key (k);
372 if (k->flags & KEYFLAG_CANTUSE)
374 if (pk->flags & KEYFLAG_CANTUSE)
380 static int pgp_id_is_strong (pgp_uid_t * uid)
382 if ((uid->trust & 3) < 3)
388 static int pgp_id_is_valid (pgp_uid_t * uid)
390 if (!pgp_key_is_valid (uid->parent))
392 if (uid->flags & KEYFLAG_CANTUSE)
398 #define PGP_KV_VALID 1
399 #define PGP_KV_ADDR 2
400 #define PGP_KV_STRING 4
401 #define PGP_KV_STRONGID 8
403 #define PGP_KV_MATCH (PGP_KV_ADDR|PGP_KV_STRING)
405 static int pgp_id_matches_addr (ADDRESS * addr, ADDRESS * u_addr,
410 if (pgp_id_is_valid (uid))
413 if (pgp_id_is_strong (uid))
414 rv |= PGP_KV_STRONGID;
416 if (addr->mailbox && u_addr->mailbox
417 && mutt_strcasecmp (addr->mailbox, u_addr->mailbox) == 0)
420 if (addr->personal && u_addr->personal
421 && mutt_strcasecmp (addr->personal, u_addr->personal) == 0)
427 static pgp_key_t pgp_select_key (pgp_key_t keys, ADDRESS * p, const char *s)
430 pgp_uid_t **KeyTable;
433 char helpstr[SHORT_STRING], buf[LONG_STRING], tmpbuf[STRING];
434 char cmd[LONG_STRING], tempfile[_POSIX_PATH_MAX];
439 int (*f) (const void *, const void *);
446 for (i = 0, kp = keys; kp; kp = kp->next) {
447 if (!option (OPTPGPSHOWUNUSABLE) && (kp->flags & KEYFLAG_CANTUSE)) {
452 for (a = kp->address; a; a = a->next) {
453 if (!option (OPTPGPSHOWUNUSABLE) && (a->flags & KEYFLAG_CANTUSE)) {
460 safe_realloc (&KeyTable, sizeof (pgp_uid_t *) * keymax);
467 if (!i && unusable) {
468 mutt_error _("All matching keys are expired, revoked, or disabled.");
474 switch (PgpSortKeys & SORT_MASK) {
476 f = pgp_compare_date;
479 f = pgp_compare_keyid;
482 f = pgp_compare_address;
486 f = pgp_compare_trust;
489 qsort (KeyTable, i, sizeof (pgp_uid_t *), f);
492 mutt_make_help (buf, sizeof (buf), _("Exit "), MENU_PGP, OP_EXIT);
493 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
494 mutt_make_help (buf, sizeof (buf), _("Select "), MENU_PGP,
495 OP_GENERIC_SELECT_ENTRY);
496 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
497 mutt_make_help (buf, sizeof (buf), _("Check key "), MENU_PGP,
499 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
500 mutt_make_help (buf, sizeof (buf), _("Help"), MENU_PGP, OP_HELP);
501 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
503 menu = mutt_new_menu ();
505 menu->make_entry = pgp_entry;
506 menu->menu = MENU_PGP;
507 menu->help = helpstr;
508 menu->data = KeyTable;
511 snprintf (buf, sizeof (buf), _("PGP keys matching <%s>."), p->mailbox);
513 snprintf (buf, sizeof (buf), _("PGP keys matching \"%s\"."), s);
523 switch (mutt_menuLoop (menu)) {
527 mutt_mktemp (tempfile);
528 if ((devnull = fopen ("/dev/null", "w")) == NULL) { /* __FOPEN_CHECKED__ */
529 mutt_perror _("Can't open /dev/null");
533 if ((fp = safe_fopen (tempfile, "w")) == NULL) {
535 mutt_perror _("Can't create temporary file");
540 mutt_message _("Invoking PGP...");
542 snprintf (tmpbuf, sizeof (tmpbuf), "0x%s",
543 pgp_keyid (pgp_principal_key
544 (KeyTable[menu->current]->parent)));
546 if ((thepid = pgp_invoke_verify_key (NULL, NULL, NULL, -1,
547 fileno (fp), fileno (devnull),
549 mutt_perror _("Can't create filter");
556 mutt_wait_filter (thepid);
560 snprintf (cmd, sizeof (cmd), _("Key ID: 0x%s"),
561 pgp_keyid (pgp_principal_key
562 (KeyTable[menu->current]->parent)));
563 mutt_do_pager (cmd, tempfile, 0, NULL);
564 menu->redraw = REDRAW_FULL;
570 mutt_message ("%s", KeyTable[menu->current]->addr);
573 case OP_GENERIC_SELECT_ENTRY:
576 /* XXX make error reporting more verbose */
578 if (option (OPTPGPCHECKTRUST))
579 if (!pgp_key_is_valid (KeyTable[menu->current]->parent)) {
580 mutt_error _("This key can't be used: expired/disabled/revoked.");
585 if (option (OPTPGPCHECKTRUST) &&
586 (!pgp_id_is_valid (KeyTable[menu->current])
587 || !pgp_id_is_strong (KeyTable[menu->current]))) {
589 char buff[LONG_STRING];
591 if (KeyTable[menu->current]->flags & KEYFLAG_CANTUSE)
592 s = N_("ID is expired/disabled/revoked.");
594 switch (KeyTable[menu->current]->trust & 0x03) {
596 s = N_("ID has undefined validity.");
599 s = N_("ID is not valid.");
602 s = N_("ID is only marginally valid.");
606 snprintf (buff, sizeof (buff),
607 _("%s Do you really want to use the key?"), _(s));
609 if (mutt_yesorno (buff, M_NO) != M_YES) {
616 kp = pgp_principal_key (KeyTable[menu->current]->parent);
618 kp = KeyTable[menu->current]->parent;
631 mutt_menuDestroy (&menu);
634 set_option (OPTNEEDREDRAW);
639 pgp_key_t pgp_ask_for_key (char *tag, char *whatfor,
640 short abilities, pgp_ring_t keyring)
643 char resp[SHORT_STRING];
644 struct pgp_cache *l = NULL;
651 for (l = id_defaults; l; l = l->next)
652 if (!mutt_strcasecmp (whatfor, l->what)) {
653 strfcpy (resp, NONULL (l->dflt), sizeof (resp));
661 if (mutt_get_field (tag, resp, sizeof (resp), M_CLEAR) != 0)
666 mutt_str_replace (&l->dflt, resp);
668 l = safe_malloc (sizeof (struct pgp_cache));
669 l->next = id_defaults;
671 l->what = safe_strdup (whatfor);
672 l->dflt = safe_strdup (resp);
676 if ((key = pgp_getkeybystr (resp, abilities, keyring)))
684 /* generate a public key attachment */
686 BODY *pgp_make_key_attachment (char *tempf)
689 char buff[LONG_STRING];
690 char tempfb[_POSIX_PATH_MAX], tmp[STRING];
697 unset_option (OPTPGPCHECKTRUST);
700 pgp_ask_for_key (_("Please enter the key ID: "), NULL, 0, PGP_PUBRING);
705 snprintf (tmp, sizeof (tmp), "0x%s", pgp_keyid (pgp_principal_key (key)));
709 mutt_mktemp (tempfb);
713 if ((tempfp = safe_fopen (tempf, tempf == tempfb ? "w" : "a")) == NULL) {
714 mutt_perror _("Can't create temporary file");
719 if ((devnull = fopen ("/dev/null", "w")) == NULL) { /* __FOPEN_CHECKED__ */
720 mutt_perror _("Can't open /dev/null");
728 mutt_message _("Invoking pgp...");
732 pgp_invoke_export (NULL, NULL, NULL, -1,
733 fileno (tempfp), fileno (devnull), tmp)) == -1) {
734 mutt_perror _("Can't create filter");
742 mutt_wait_filter (thepid);
747 att = mutt_new_body ();
748 att->filename = safe_strdup (tempf);
751 att->type = TYPEAPPLICATION;
752 att->subtype = safe_strdup ("pgp-keys");
753 snprintf (buff, sizeof (buff), _("PGP Key %s."), tmp);
754 att->description = safe_strdup (buff);
755 mutt_update_encoding (att);
758 att->length = sb.st_size;
763 static LIST *pgp_add_string_to_hints (LIST * hints, const char *str)
768 if ((scratch = safe_strdup (str)) == NULL)
771 for (t = strtok (scratch, " ,.:\"()<>\n"); t;
772 t = strtok (NULL, " ,.:\"()<>\n")) {
774 hints = mutt_add_list (hints, t);
781 static pgp_key_t *pgp_get_lastp (pgp_key_t p)
783 for (; p; p = p->next)
790 pgp_key_t pgp_getkeybyaddr (ADDRESS * a, short abilities, pgp_ring_t keyring)
798 int this_key_has_strong;
799 int this_key_has_weak;
800 int this_key_has_invalid;
803 pgp_key_t keys, k, kn;
804 pgp_key_t the_valid_key = NULL;
805 pgp_key_t matches = NULL;
806 pgp_key_t *last = &matches;
810 hints = pgp_add_string_to_hints (hints, a->mailbox);
811 if (a && a->personal)
812 hints = pgp_add_string_to_hints (hints, a->personal);
814 mutt_message (_("Looking for keys matching \"%s\"..."), a->mailbox);
815 keys = pgp_get_candidates (keyring, hints);
817 mutt_free_list (&hints);
822 dprint (5, (debugfile, "pgp_getkeybyaddr: looking for %s <%s>.",
823 a->personal, a->mailbox));
826 for (k = keys; k; k = kn) {
829 dprint (5, (debugfile, " looking at key: %s\n", pgp_keyid (k)));
831 if (abilities && !(k->flags & abilities)) {
832 dprint (5, (debugfile, " insufficient abilities: Has %x, want %x\n",
833 k->flags, abilities));
837 this_key_has_weak = 0; /* weak but valid match */
838 this_key_has_invalid = 0; /* invalid match */
839 this_key_has_strong = 0; /* strong and valid match */
840 match = 0; /* any match */
842 for (q = k->address; q; q = q->next) {
843 r = rfc822_parse_adrlist (NULL, q->addr);
845 for (p = r; p; p = p->next) {
846 int validity = pgp_id_matches_addr (a, p, q);
848 if (validity & PGP_KV_MATCH) /* something matches */
851 /* is this key a strong candidate? */
852 if ((validity & PGP_KV_VALID) && (validity & PGP_KV_STRONGID)
853 && (validity & PGP_KV_ADDR)) {
854 if (the_valid_key && the_valid_key != k)
857 this_key_has_strong = 1;
859 else if ((validity & PGP_KV_MATCH) && !(validity & PGP_KV_VALID))
860 this_key_has_invalid = 1;
861 else if ((validity & PGP_KV_MATCH)
862 && (!(validity & PGP_KV_STRONGID)
863 || !(validity & PGP_KV_ADDR)))
864 this_key_has_weak = 1;
867 rfc822_free_address (&r);
870 if (match && !this_key_has_strong && this_key_has_invalid)
872 if (match && !this_key_has_strong && this_key_has_weak)
876 *last = pgp_principal_key (k);
877 kn = pgp_remove_key (&keys, *last);
878 last = pgp_get_lastp (k);
882 pgp_free_key (&keys);
885 if (the_valid_key && !multi /* && !weak
886 && !(invalid && option (OPTPGPSHOWUNUSABLE)) */ ) {
888 * There was precisely one strong match on a valid ID.
890 * Proceed without asking the user.
892 pgp_remove_key (&matches, the_valid_key);
893 pgp_free_key (&matches);
898 * Else: Ask the user.
900 if ((k = pgp_select_key (matches, a, NULL)))
901 pgp_remove_key (&matches, k);
902 pgp_free_key (&matches);
911 pgp_key_t pgp_getkeybystr (char *p, short abilities, pgp_ring_t keyring)
915 pgp_key_t matches = NULL;
916 pgp_key_t *last = &matches;
921 mutt_message (_("Looking for keys matching \"%s\"..."), p);
923 hints = pgp_add_string_to_hints (hints, p);
924 keys = pgp_get_candidates (keyring, hints);
925 mutt_free_list (&hints);
931 for (k = keys; k; k = kn) {
933 if (abilities && !(k->flags & abilities))
938 for (a = k->address; a; a = a->next) {
941 "pgp_getkeybystr: matching \"%s\" against key %s, \"%s\": ", p,
942 pgp_keyid (k), a->addr));
943 if (!*p || mutt_strcasecmp (p, pgp_keyid (k)) == 0
944 || (!mutt_strncasecmp (p, "0x", 2)
945 && !mutt_strcasecmp (p + 2, pgp_keyid (k)))
946 || (option (OPTPGPLONGIDS) && !mutt_strncasecmp (p, "0x", 2)
947 && !mutt_strcasecmp (p + 2, k->keyid + 8))
948 || mutt_stristr (a->addr, p)) {
949 dprint (5, (debugfile, "match.\n"));
956 *last = pgp_principal_key (k);
957 kn = pgp_remove_key (&keys, *last);
958 last = pgp_get_lastp (k);
962 pgp_free_key (&keys);
965 if ((k = pgp_select_key (matches, NULL, p)))
966 pgp_remove_key (&matches, k);
968 pgp_free_key (&matches);
977 #endif /* CRYPT_BACKEND_CLASSIC_PGP */