2 * Copyright notice from original mutt:
3 * Copyright (C) 1996,1997 Michael R. Elkins <me@mutt.org>
4 * Copyright (c) 1998,1999 Thomas Roessler <roessler@does-not-exist.org>
6 * This file is part of mutt-ng, see http://www.muttng.org/.
7 * It's licensed under the GNU General Public License,
8 * please see the file GPL in the top level source directory.
16 #include "mutt_curses.h"
17 #include "mutt_menu.h"
26 #include "lib/debug.h"
37 #ifdef CRYPT_BACKEND_CLASSIC_PGP
42 struct pgp_cache *next;
45 static struct pgp_cache *id_defaults = NULL;
47 static char trust_flags[] = "?- +";
49 static char *pgp_key_abilities (int flags)
53 if (!(flags & KEYFLAG_CANENCRYPT))
55 else if (flags & KEYFLAG_PREFER_SIGNING)
60 if (!(flags & KEYFLAG_CANSIGN))
62 else if (flags & KEYFLAG_PREFER_ENCRYPTION)
72 static char pgp_flags (int flags)
74 if (flags & KEYFLAG_REVOKED)
76 else if (flags & KEYFLAG_EXPIRED)
78 else if (flags & KEYFLAG_DISABLED)
80 else if (flags & KEYFLAG_CRITICAL)
86 static pgp_key_t pgp_principal_key (pgp_key_t key)
88 if (key->flags & KEYFLAG_SUBKEY && key->parent)
95 * Format an entry on the PGP key selection menu.
98 * %k key id %K key id of the principal key
100 * %a algorithm %A algorithm of the princ. key
101 * %l length %L length of the princ. key
102 * %f flags %F flags of the princ. key
103 * %c capabilities %C capabilities of the princ. key
104 * %t trust/validity of the key-uid association
105 * %[...] date of key using strftime(3)
108 typedef struct pgp_entry {
113 static const char *pgp_entry_fmt (char *dest,
118 const char *ifstring,
119 const char *elsestring,
120 unsigned long data, format_flag flags)
127 int optional = (flags & M_FORMAT_OPTIONAL);
129 entry = (pgp_entry_t *) data;
132 pkey = pgp_principal_key (key);
134 if (isupper ((unsigned char) op))
137 kflags = key->flags | (pkey->flags & KEYFLAG_RESTRICTIONS)
140 switch (ascii_tolower (op)) {
145 char buf2[SHORT_STRING], *p;
161 while (len > 0 && *cp != ']') {
170 break; /* not enough space */
180 if (do_locales && Locale)
181 setlocale (LC_TIME, Locale);
183 tm = localtime (&key->gen_time);
185 strftime (buf2, sizeof (buf2), dest, tm);
188 setlocale (LC_TIME, "C");
190 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
191 snprintf (dest, destlen, fmt, buf2);
198 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
199 snprintf (dest, destlen, fmt, entry->num);
204 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
205 snprintf (dest, destlen, fmt, _pgp_keyid (key));
210 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
211 snprintf (dest, destlen, fmt, uid->addr);
216 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
217 snprintf (dest, destlen, fmt, key->algorithm);
222 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
223 snprintf (dest, destlen, fmt, key->keylen);
228 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
229 snprintf (dest, destlen, fmt, pgp_flags (kflags));
231 else if (!(kflags & (KEYFLAG_RESTRICTIONS)))
236 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
237 snprintf (dest, destlen, fmt, pgp_key_abilities (kflags));
239 else if (!(kflags & (KEYFLAG_ABILITIES)))
244 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
245 snprintf (dest, destlen, fmt, trust_flags[uid->trust & 0x03]);
247 else if (!(uid->trust & 0x03))
248 /* undefined trust */
256 mutt_FormatString (dest, destlen, ifstring, mutt_attach_fmt, data, 0);
257 else if (flags & M_FORMAT_OPTIONAL)
258 mutt_FormatString (dest, destlen, elsestring, mutt_attach_fmt, data, 0);
262 static void pgp_entry (char *s, size_t l, MUTTMENU * menu, int num)
264 pgp_uid_t **KeyTable = (pgp_uid_t **) menu->data;
267 entry.uid = KeyTable[num];
270 mutt_FormatString (s, l, NONULL (PgpEntryFormat), pgp_entry_fmt,
271 (unsigned long) &entry, M_FORMAT_ARROWCURSOR);
274 static int _pgp_compare_address (const void *a, const void *b)
278 pgp_uid_t **s = (pgp_uid_t **) a;
279 pgp_uid_t **t = (pgp_uid_t **) b;
281 if ((r = safe_strcasecmp ((*s)->addr, (*t)->addr)))
284 return (safe_strcasecmp (_pgp_keyid ((*s)->parent),
285 _pgp_keyid ((*t)->parent)) > 0);
288 static int pgp_compare_address (const void *a, const void *b)
290 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_address (a, b)
291 : _pgp_compare_address (a, b));
296 static int _pgp_compare_keyid (const void *a, const void *b)
300 pgp_uid_t **s = (pgp_uid_t **) a;
301 pgp_uid_t **t = (pgp_uid_t **) b;
303 if ((r = safe_strcasecmp (_pgp_keyid ((*s)->parent),
304 _pgp_keyid ((*t)->parent))))
307 return (safe_strcasecmp ((*s)->addr, (*t)->addr)) > 0;
310 static int pgp_compare_keyid (const void *a, const void *b)
312 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_keyid (a, b)
313 : _pgp_compare_keyid (a, b));
316 static int _pgp_compare_date (const void *a, const void *b)
319 pgp_uid_t **s = (pgp_uid_t **) a;
320 pgp_uid_t **t = (pgp_uid_t **) b;
322 if ((r = ((*s)->parent->gen_time - (*t)->parent->gen_time)))
324 return (safe_strcasecmp ((*s)->addr, (*t)->addr)) > 0;
327 static int pgp_compare_date (const void *a, const void *b)
329 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_date (a, b)
330 : _pgp_compare_date (a, b));
333 static int _pgp_compare_trust (const void *a, const void *b)
337 pgp_uid_t **s = (pgp_uid_t **) a;
338 pgp_uid_t **t = (pgp_uid_t **) b;
340 if ((r = (((*s)->parent->flags & (KEYFLAG_RESTRICTIONS))
341 - ((*t)->parent->flags & (KEYFLAG_RESTRICTIONS)))))
343 if ((r = ((*s)->trust - (*t)->trust)))
345 if ((r = ((*s)->parent->keylen - (*t)->parent->keylen)))
347 if ((r = ((*s)->parent->gen_time - (*t)->parent->gen_time)))
349 if ((r = safe_strcasecmp ((*s)->addr, (*t)->addr)))
351 return (safe_strcasecmp (_pgp_keyid ((*s)->parent),
352 _pgp_keyid ((*t)->parent))) > 0;
355 static int pgp_compare_trust (const void *a, const void *b)
357 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_trust (a, b)
358 : _pgp_compare_trust (a, b));
361 static int pgp_key_is_valid (pgp_key_t k)
363 pgp_key_t pk = pgp_principal_key (k);
365 if (k->flags & KEYFLAG_CANTUSE)
367 if (pk->flags & KEYFLAG_CANTUSE)
373 static int pgp_id_is_strong (pgp_uid_t * uid)
375 if ((uid->trust & 3) < 3)
381 static int pgp_id_is_valid (pgp_uid_t * uid)
383 if (!pgp_key_is_valid (uid->parent))
385 if (uid->flags & KEYFLAG_CANTUSE)
391 #define PGP_KV_VALID 1
392 #define PGP_KV_ADDR 2
393 #define PGP_KV_STRING 4
394 #define PGP_KV_STRONGID 8
396 #define PGP_KV_MATCH (PGP_KV_ADDR|PGP_KV_STRING)
398 static int pgp_id_matches_addr (ADDRESS * addr, ADDRESS * u_addr,
403 if (pgp_id_is_valid (uid))
406 if (pgp_id_is_strong (uid))
407 rv |= PGP_KV_STRONGID;
409 if (addr->mailbox && u_addr->mailbox
410 && safe_strcasecmp (addr->mailbox, u_addr->mailbox) == 0)
413 if (addr->personal && u_addr->personal
414 && safe_strcasecmp (addr->personal, u_addr->personal) == 0)
420 static pgp_key_t pgp_select_key (pgp_key_t keys, ADDRESS * p, const char *s)
423 pgp_uid_t **KeyTable;
426 char helpstr[SHORT_STRING], buf[LONG_STRING], tmpbuf[STRING];
427 char cmd[LONG_STRING], tempfile[_POSIX_PATH_MAX];
432 int (*f) (const void *, const void *);
439 for (i = 0, kp = keys; kp; kp = kp->next) {
440 if (!option (OPTPGPSHOWUNUSABLE) && (kp->flags & KEYFLAG_CANTUSE)) {
445 for (a = kp->address; a; a = a->next) {
446 if (!option (OPTPGPSHOWUNUSABLE) && (a->flags & KEYFLAG_CANTUSE)) {
453 safe_realloc (&KeyTable, sizeof (pgp_uid_t *) * keymax);
460 if (!i && unusable) {
461 mutt_error _("All matching keys are expired, revoked, or disabled.");
467 switch (PgpSortKeys & SORT_MASK) {
469 f = pgp_compare_date;
472 f = pgp_compare_keyid;
475 f = pgp_compare_address;
479 f = pgp_compare_trust;
482 qsort (KeyTable, i, sizeof (pgp_uid_t *), f);
485 mutt_make_help (buf, sizeof (buf), _("Exit "), MENU_PGP, OP_EXIT);
486 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
487 mutt_make_help (buf, sizeof (buf), _("Select "), MENU_PGP,
488 OP_GENERIC_SELECT_ENTRY);
489 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
490 mutt_make_help (buf, sizeof (buf), _("Check key "), MENU_PGP,
492 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
493 mutt_make_help (buf, sizeof (buf), _("Help"), MENU_PGP, OP_HELP);
494 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
496 menu = mutt_new_menu ();
498 menu->make_entry = pgp_entry;
499 menu->menu = MENU_PGP;
500 menu->help = helpstr;
501 menu->data = KeyTable;
504 snprintf (buf, sizeof (buf), _("PGP keys matching <%s>."), p->mailbox);
506 snprintf (buf, sizeof (buf), _("PGP keys matching \"%s\"."), s);
516 switch (mutt_menuLoop (menu)) {
520 mutt_mktemp (tempfile);
521 if ((devnull = fopen ("/dev/null", "w")) == NULL) { /* __FOPEN_CHECKED__ */
522 mutt_perror (_("Can't open /dev/null"));
526 if ((fp = safe_fopen (tempfile, "w")) == NULL) {
528 mutt_perror (_("Can't create temporary file"));
533 mutt_message _("Invoking PGP...");
535 snprintf (tmpbuf, sizeof (tmpbuf), "0x%s",
536 pgp_keyid (pgp_principal_key
537 (KeyTable[menu->current]->parent)));
539 if ((thepid = pgp_invoke_verify_key (NULL, NULL, NULL, -1,
540 fileno (fp), fileno (devnull),
542 mutt_perror (_("Can't create filter"));
549 mutt_wait_filter (thepid);
553 snprintf (cmd, sizeof (cmd), _("Key ID: 0x%s"),
554 pgp_keyid (pgp_principal_key
555 (KeyTable[menu->current]->parent)));
556 mutt_do_pager (cmd, tempfile, 0, NULL);
557 menu->redraw = REDRAW_FULL;
563 mutt_message ("%s", KeyTable[menu->current]->addr);
566 case OP_GENERIC_SELECT_ENTRY:
569 /* XXX make error reporting more verbose */
571 if (option (OPTPGPCHECKTRUST))
572 if (!pgp_key_is_valid (KeyTable[menu->current]->parent)) {
573 mutt_error _("This key can't be used: expired/disabled/revoked.");
578 if (option (OPTPGPCHECKTRUST) &&
579 (!pgp_id_is_valid (KeyTable[menu->current])
580 || !pgp_id_is_strong (KeyTable[menu->current]))) {
582 char buff[LONG_STRING];
584 if (KeyTable[menu->current]->flags & KEYFLAG_CANTUSE)
585 s = N_("ID is expired/disabled/revoked.");
587 switch (KeyTable[menu->current]->trust & 0x03) {
589 s = N_("ID has undefined validity.");
592 s = N_("ID is not valid.");
595 s = N_("ID is only marginally valid.");
599 snprintf (buff, sizeof (buff),
600 _("%s Do you really want to use the key?"), _(s));
602 if (mutt_yesorno (buff, M_NO) != M_YES) {
609 kp = pgp_principal_key (KeyTable[menu->current]->parent);
611 kp = KeyTable[menu->current]->parent;
624 mutt_menuDestroy (&menu);
627 set_option (OPTNEEDREDRAW);
632 pgp_key_t pgp_ask_for_key (char *tag, char *whatfor,
633 short abilities, pgp_ring_t keyring)
636 char resp[SHORT_STRING];
637 struct pgp_cache *l = NULL;
644 for (l = id_defaults; l; l = l->next)
645 if (!safe_strcasecmp (whatfor, l->what)) {
646 strfcpy (resp, NONULL (l->dflt), sizeof (resp));
654 if (mutt_get_field (tag, resp, sizeof (resp), M_CLEAR) != 0)
659 str_replace (&l->dflt, resp);
661 l = safe_malloc (sizeof (struct pgp_cache));
662 l->next = id_defaults;
664 l->what = safe_strdup (whatfor);
665 l->dflt = safe_strdup (resp);
669 if ((key = pgp_getkeybystr (resp, abilities, keyring)))
677 /* generate a public key attachment */
679 BODY *pgp_make_key_attachment (char *tempf)
682 char buff[LONG_STRING];
683 char tempfb[_POSIX_PATH_MAX], tmp[STRING];
690 unset_option (OPTPGPCHECKTRUST);
693 pgp_ask_for_key (_("Please enter the key ID: "), NULL, 0, PGP_PUBRING);
698 snprintf (tmp, sizeof (tmp), "0x%s", pgp_keyid (pgp_principal_key (key)));
702 mutt_mktemp (tempfb);
706 if ((tempfp = safe_fopen (tempf, tempf == tempfb ? "w" : "a")) == NULL) {
707 mutt_perror (_("Can't create temporary file"));
712 if ((devnull = fopen ("/dev/null", "w")) == NULL) { /* __FOPEN_CHECKED__ */
713 mutt_perror (_("Can't open /dev/null"));
721 mutt_message _("Invoking pgp...");
725 pgp_invoke_export (NULL, NULL, NULL, -1,
726 fileno (tempfp), fileno (devnull), tmp)) == -1) {
727 mutt_perror (_("Can't create filter"));
735 mutt_wait_filter (thepid);
740 att = mutt_new_body ();
741 att->filename = safe_strdup (tempf);
744 att->type = TYPEAPPLICATION;
745 att->subtype = safe_strdup ("pgp-keys");
746 snprintf (buff, sizeof (buff), _("PGP Key %s."), tmp);
747 att->description = safe_strdup (buff);
748 mutt_update_encoding (att);
751 att->length = sb.st_size;
756 static LIST *pgp_add_string_to_hints (LIST * hints, const char *str)
761 if ((scratch = safe_strdup (str)) == NULL)
764 for (t = strtok (scratch, " ,.:\"()<>\n"); t;
765 t = strtok (NULL, " ,.:\"()<>\n")) {
766 if (safe_strlen (t) > 3)
767 hints = mutt_add_list (hints, t);
774 static pgp_key_t *pgp_get_lastp (pgp_key_t p)
776 for (; p; p = p->next)
783 pgp_key_t pgp_getkeybyaddr (ADDRESS * a, short abilities, pgp_ring_t keyring)
791 int this_key_has_strong;
792 int this_key_has_weak;
793 int this_key_has_invalid;
796 pgp_key_t keys, k, kn;
797 pgp_key_t the_valid_key = NULL;
798 pgp_key_t matches = NULL;
799 pgp_key_t *last = &matches;
803 hints = pgp_add_string_to_hints (hints, a->mailbox);
804 if (a && a->personal)
805 hints = pgp_add_string_to_hints (hints, a->personal);
807 mutt_message (_("Looking for keys matching \"%s\"..."), a->mailbox);
808 keys = pgp_get_candidates (keyring, hints);
810 mutt_free_list (&hints);
815 debug_print (5, ("looking for %s <%s>\n", a->personal, a->mailbox));
817 for (k = keys; k; k = kn) {
820 debug_print (5, (" looking at key: %s\n", pgp_keyid (k)));
822 if (abilities && !(k->flags & abilities)) {
823 debug_print (5, (" insufficient abilities: Has %x, want %x\n",
824 k->flags, abilities));
828 this_key_has_weak = 0; /* weak but valid match */
829 this_key_has_invalid = 0; /* invalid match */
830 this_key_has_strong = 0; /* strong and valid match */
831 match = 0; /* any match */
833 for (q = k->address; q; q = q->next) {
834 r = rfc822_parse_adrlist (NULL, q->addr);
836 for (p = r; p; p = p->next) {
837 int validity = pgp_id_matches_addr (a, p, q);
839 if (validity & PGP_KV_MATCH) /* something matches */
842 /* is this key a strong candidate? */
843 if ((validity & PGP_KV_VALID) && (validity & PGP_KV_STRONGID)
844 && (validity & PGP_KV_ADDR)) {
845 if (the_valid_key && the_valid_key != k)
848 this_key_has_strong = 1;
850 else if ((validity & PGP_KV_MATCH) && !(validity & PGP_KV_VALID))
851 this_key_has_invalid = 1;
852 else if ((validity & PGP_KV_MATCH)
853 && (!(validity & PGP_KV_STRONGID)
854 || !(validity & PGP_KV_ADDR)))
855 this_key_has_weak = 1;
858 rfc822_free_address (&r);
861 if (match && !this_key_has_strong && this_key_has_invalid)
863 if (match && !this_key_has_strong && this_key_has_weak)
867 *last = pgp_principal_key (k);
868 kn = pgp_remove_key (&keys, *last);
869 last = pgp_get_lastp (k);
873 pgp_free_key (&keys);
876 if (the_valid_key && !multi /* && !weak
877 && !(invalid && option (OPTPGPSHOWUNUSABLE)) */ ) {
879 * There was precisely one strong match on a valid ID.
881 * Proceed without asking the user.
883 pgp_remove_key (&matches, the_valid_key);
884 pgp_free_key (&matches);
889 * Else: Ask the user.
891 if ((k = pgp_select_key (matches, a, NULL)))
892 pgp_remove_key (&matches, k);
893 pgp_free_key (&matches);
902 pgp_key_t pgp_getkeybystr (char *p, short abilities, pgp_ring_t keyring)
906 pgp_key_t matches = NULL;
907 pgp_key_t *last = &matches;
912 mutt_message (_("Looking for keys matching \"%s\"..."), p);
914 hints = pgp_add_string_to_hints (hints, p);
915 keys = pgp_get_candidates (keyring, hints);
916 mutt_free_list (&hints);
922 for (k = keys; k; k = kn) {
924 if (abilities && !(k->flags & abilities))
929 for (a = k->address; a; a = a->next) {
930 debug_print (5, ("matching \"%s\" against key %s, \"%s\":\n", p, pgp_keyid (k), a->addr));
931 if (!*p || safe_strcasecmp (p, pgp_keyid (k)) == 0
932 || (!safe_strncasecmp (p, "0x", 2)
933 && !safe_strcasecmp (p + 2, pgp_keyid (k)))
934 || (option (OPTPGPLONGIDS) && !safe_strncasecmp (p, "0x", 2)
935 && !safe_strcasecmp (p + 2, k->keyid + 8))
936 || str_isstr (a->addr, p)) {
937 debug_print (5, ("match.\n"));
944 *last = pgp_principal_key (k);
945 kn = pgp_remove_key (&keys, *last);
946 last = pgp_get_lastp (k);
950 pgp_free_key (&keys);
953 if ((k = pgp_select_key (matches, NULL, p)))
954 pgp_remove_key (&matches, k);
956 pgp_free_key (&matches);
965 #endif /* CRYPT_BACKEND_CLASSIC_PGP */