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.
15 #include <lib-lib/lib-lib.h>
17 #include <lib-mime/mime.h>
19 #include <lib-ui/curses.h>
20 #include <lib-ui/enter.h>
21 #include <lib-ui/menu.h>
24 #include "recvattach.h"
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 {
114 pgp_entry_fmt (char *dest, ssize_t destlen, char op,
115 const char *src, const char *prefix,
116 const char *ifstring, const char *elsestring,
117 unsigned long data, format_flag flags)
124 int optional = (flags & M_FORMAT_OPTIONAL);
126 entry = (pgp_entry_t *) data;
129 pkey = pgp_principal_key (key);
131 if (isupper ((unsigned char) op))
134 kflags = key->flags | (pkey->flags & KEYFLAG_RESTRICTIONS)
137 switch (ascii_tolower (op)) {
142 char buf2[SHORT_STRING], *p;
158 while (len > 0 && *cp != ']') {
167 break; /* not enough space */
177 if (do_locales && Locale)
178 setlocale (LC_TIME, Locale);
180 tm = localtime (&key->gen_time);
182 strftime (buf2, sizeof (buf2), dest, tm);
185 setlocale (LC_TIME, "C");
187 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
188 snprintf (dest, destlen, fmt, buf2);
195 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
196 snprintf (dest, destlen, fmt, entry->num);
201 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
202 snprintf (dest, destlen, fmt, _pgp_keyid (key));
207 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
208 snprintf (dest, destlen, fmt, uid->addr);
213 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
214 snprintf (dest, destlen, fmt, key->algorithm);
219 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
220 snprintf (dest, destlen, fmt, key->keylen);
225 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
226 snprintf (dest, destlen, fmt, pgp_flags (kflags));
228 else if (!(kflags & (KEYFLAG_RESTRICTIONS)))
233 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
234 snprintf (dest, destlen, fmt, pgp_key_abilities (kflags));
236 else if (!(kflags & (KEYFLAG_ABILITIES)))
241 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
242 snprintf (dest, destlen, fmt, trust_flags[uid->trust & 0x03]);
244 else if (!(uid->trust & 0x03))
245 /* undefined trust */
253 mutt_FormatString (dest, destlen, ifstring, mutt_attach_fmt, data, 0);
254 else if (flags & M_FORMAT_OPTIONAL)
255 mutt_FormatString (dest, destlen, elsestring, mutt_attach_fmt, data, 0);
259 static void pgp_entry (char *s, ssize_t l, MUTTMENU * menu, int num)
261 pgp_uid_t **KeyTable = (pgp_uid_t **) menu->data;
264 entry.uid = KeyTable[num];
267 mutt_FormatString (s, l, NONULL (PgpEntryFormat), pgp_entry_fmt,
268 (unsigned long) &entry, M_FORMAT_ARROWCURSOR);
271 static int _pgp_compare_address (const void *a, const void *b)
275 pgp_uid_t **s = (pgp_uid_t **) a;
276 pgp_uid_t **t = (pgp_uid_t **) b;
278 if ((r = m_strcasecmp((*s)->addr, (*t)->addr)))
281 return (m_strcasecmp(_pgp_keyid ((*s)->parent),
282 _pgp_keyid ((*t)->parent)) > 0);
285 static int pgp_compare_address (const void *a, const void *b)
287 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_address (a, b)
288 : _pgp_compare_address (a, b));
293 static int _pgp_compare_keyid (const void *a, const void *b)
297 pgp_uid_t **s = (pgp_uid_t **) a;
298 pgp_uid_t **t = (pgp_uid_t **) b;
300 if ((r = m_strcasecmp(_pgp_keyid ((*s)->parent),
301 _pgp_keyid ((*t)->parent))))
304 return (m_strcasecmp((*s)->addr, (*t)->addr)) > 0;
307 static int pgp_compare_keyid (const void *a, const void *b)
309 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_keyid (a, b)
310 : _pgp_compare_keyid (a, b));
313 static int _pgp_compare_date (const void *a, const void *b)
316 pgp_uid_t **s = (pgp_uid_t **) a;
317 pgp_uid_t **t = (pgp_uid_t **) b;
319 if ((r = ((*s)->parent->gen_time - (*t)->parent->gen_time)))
321 return (m_strcasecmp((*s)->addr, (*t)->addr)) > 0;
324 static int pgp_compare_date (const void *a, const void *b)
326 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_date (a, b)
327 : _pgp_compare_date (a, b));
330 static int _pgp_compare_trust (const void *a, const void *b)
334 pgp_uid_t **s = (pgp_uid_t **) a;
335 pgp_uid_t **t = (pgp_uid_t **) b;
337 if ((r = (((*s)->parent->flags & (KEYFLAG_RESTRICTIONS))
338 - ((*t)->parent->flags & (KEYFLAG_RESTRICTIONS)))))
340 if ((r = ((*s)->trust - (*t)->trust)))
342 if ((r = ((*s)->parent->keylen - (*t)->parent->keylen)))
344 if ((r = ((*s)->parent->gen_time - (*t)->parent->gen_time)))
346 if ((r = m_strcasecmp((*s)->addr, (*t)->addr)))
348 return (m_strcasecmp(_pgp_keyid ((*s)->parent),
349 _pgp_keyid ((*t)->parent))) > 0;
352 static int pgp_compare_trust (const void *a, const void *b)
354 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_trust (a, b)
355 : _pgp_compare_trust (a, b));
358 static int pgp_key_is_valid (pgp_key_t k)
360 pgp_key_t pk = pgp_principal_key (k);
362 if (k->flags & KEYFLAG_CANTUSE)
364 if (pk->flags & KEYFLAG_CANTUSE)
370 static int pgp_id_is_strong (pgp_uid_t * uid)
372 if ((uid->trust & 3) < 3)
378 static int pgp_id_is_valid (pgp_uid_t * uid)
380 if (!pgp_key_is_valid (uid->parent))
382 if (uid->flags & KEYFLAG_CANTUSE)
388 #define PGP_KV_VALID 1
389 #define PGP_KV_ADDR 2
390 #define PGP_KV_STRING 4
391 #define PGP_KV_STRONGID 8
393 #define PGP_KV_MATCH (PGP_KV_ADDR|PGP_KV_STRING)
395 static int pgp_id_matches_addr (address_t * addr, address_t * u_addr,
400 if (pgp_id_is_valid (uid))
403 if (pgp_id_is_strong (uid))
404 rv |= PGP_KV_STRONGID;
406 if (addr->mailbox && u_addr->mailbox
407 && m_strcasecmp(addr->mailbox, u_addr->mailbox) == 0)
410 if (addr->personal && u_addr->personal
411 && m_strcasecmp(addr->personal, u_addr->personal) == 0)
417 static pgp_key_t pgp_select_key (pgp_key_t keys, address_t * p, const char *s)
420 pgp_uid_t **KeyTable;
423 char helpstr[SHORT_STRING], buf[LONG_STRING], tmpbuf[STRING];
424 char cmd[LONG_STRING], tempfile[_POSIX_PATH_MAX];
429 int (*f) (const void *, const void *);
436 for (i = 0, kp = keys; kp; kp = kp->next) {
437 if (!option (OPTPGPSHOWUNUSABLE) && (kp->flags & KEYFLAG_CANTUSE)) {
442 for (a = kp->address; a; a = a->next) {
443 if (!option (OPTPGPSHOWUNUSABLE) && (a->flags & KEYFLAG_CANTUSE)) {
450 p_realloc(&KeyTable, keymax);
457 if (!i && unusable) {
458 mutt_error _("All matching keys are expired, revoked, or disabled.");
464 switch (PgpSortKeys & SORT_MASK) {
466 f = pgp_compare_date;
469 f = pgp_compare_keyid;
472 f = pgp_compare_address;
476 f = pgp_compare_trust;
479 qsort (KeyTable, i, sizeof (pgp_uid_t *), f);
482 mutt_make_help (buf, sizeof (buf), _("Exit "), MENU_PGP, OP_EXIT);
483 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
484 mutt_make_help (buf, sizeof (buf), _("Select "), MENU_PGP,
485 OP_GENERIC_SELECT_ENTRY);
486 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
487 mutt_make_help (buf, sizeof (buf), _("Check key "), MENU_PGP,
489 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
490 mutt_make_help (buf, sizeof (buf), _("Help"), MENU_PGP, OP_HELP);
491 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
493 menu = mutt_new_menu ();
495 menu->make_entry = pgp_entry;
496 menu->menu = MENU_PGP;
497 menu->help = helpstr;
498 menu->data = KeyTable;
501 snprintf (buf, sizeof (buf), _("PGP keys matching <%s>."), p->mailbox);
503 snprintf (buf, sizeof (buf), _("PGP keys matching \"%s\"."), s);
513 switch (mutt_menuLoop (menu)) {
517 mutt_mktemp (tempfile);
518 if ((devnull = fopen ("/dev/null", "w")) == NULL) { /* __FOPEN_CHECKED__ */
519 mutt_perror (_("Can't open /dev/null"));
523 if ((fp = safe_fopen (tempfile, "w")) == NULL) {
525 mutt_perror (_("Can't create temporary file"));
530 mutt_message _("Invoking PGP...");
532 snprintf (tmpbuf, sizeof (tmpbuf), "0x%s",
533 pgp_keyid (pgp_principal_key
534 (KeyTable[menu->current]->parent)));
536 if ((thepid = pgp_invoke_verify_key (NULL, NULL, NULL, -1,
537 fileno (fp), fileno (devnull),
539 mutt_perror (_("Can't create filter"));
546 mutt_wait_filter (thepid);
550 snprintf (cmd, sizeof (cmd), _("Key ID: 0x%s"),
551 pgp_keyid (pgp_principal_key
552 (KeyTable[menu->current]->parent)));
553 mutt_do_pager (cmd, tempfile, 0, NULL);
554 menu->redraw = REDRAW_FULL;
560 mutt_message ("%s", KeyTable[menu->current]->addr);
563 case OP_GENERIC_SELECT_ENTRY:
566 /* XXX make error reporting more verbose */
568 if (option (OPTPGPCHECKTRUST))
569 if (!pgp_key_is_valid (KeyTable[menu->current]->parent)) {
570 mutt_error _("This key can't be used: expired/disabled/revoked.");
575 if (option (OPTPGPCHECKTRUST) &&
576 (!pgp_id_is_valid (KeyTable[menu->current])
577 || !pgp_id_is_strong (KeyTable[menu->current]))) {
579 char buff[LONG_STRING];
581 if (KeyTable[menu->current]->flags & KEYFLAG_CANTUSE)
582 q = N_("ID is expired/disabled/revoked.");
584 switch (KeyTable[menu->current]->trust & 0x03) {
586 q = N_("ID has undefined validity.");
589 q = N_("ID is not valid.");
592 q = N_("ID is only marginally valid.");
596 snprintf (buff, sizeof (buff),
597 _("%s Do you really want to use the key?"), _(q));
599 if (mutt_yesorno (buff, M_NO) != M_YES) {
606 kp = pgp_principal_key (KeyTable[menu->current]->parent);
608 kp = KeyTable[menu->current]->parent;
621 mutt_menuDestroy (&menu);
624 set_option (OPTNEEDREDRAW);
629 pgp_key_t pgp_ask_for_key (char *tag, char *whatfor,
630 short abilities, pgp_ring_t keyring)
633 char resp[SHORT_STRING];
634 struct pgp_cache *l = NULL;
641 for (l = id_defaults; l; l = l->next)
642 if (!m_strcasecmp(whatfor, l->what)) {
643 m_strcpy(resp, sizeof(resp), NONULL(l->dflt));
651 if (mutt_get_field (tag, resp, sizeof (resp), M_CLEAR) != 0)
656 m_strreplace(&l->dflt, resp);
658 l = p_new(struct pgp_cache, 1);
659 l->next = id_defaults;
661 l->what = m_strdup(whatfor);
662 l->dflt = m_strdup(resp);
666 if ((key = pgp_getkeybystr (resp, abilities, keyring)))
674 /* generate a public key attachment */
676 BODY *pgp_make_key_attachment (char *tempf)
679 char buff[LONG_STRING];
680 char tempfb[_POSIX_PATH_MAX], tmp[STRING];
687 unset_option (OPTPGPCHECKTRUST);
690 pgp_ask_for_key (_("Please enter the key ID: "), NULL, 0, PGP_PUBRING);
695 snprintf (tmp, sizeof (tmp), "0x%s", pgp_keyid (pgp_principal_key (key)));
699 mutt_mktemp (tempfb);
703 if ((tempfp = safe_fopen (tempf, tempf == tempfb ? "w" : "a")) == NULL) {
704 mutt_perror (_("Can't create temporary file"));
709 if ((devnull = fopen ("/dev/null", "w")) == NULL) { /* __FOPEN_CHECKED__ */
710 mutt_perror (_("Can't open /dev/null"));
718 mutt_message _("Invoking pgp...");
722 pgp_invoke_export (NULL, NULL, NULL, -1,
723 fileno (tempfp), fileno (devnull), tmp)) == -1) {
724 mutt_perror (_("Can't create filter"));
732 mutt_wait_filter (thepid);
738 att->filename = m_strdup(tempf);
741 att->type = TYPEAPPLICATION;
742 att->subtype = m_strdup("pgp-keys");
743 snprintf (buff, sizeof (buff), _("PGP Key %s."), tmp);
744 att->description = m_strdup(buff);
745 mutt_update_encoding (att);
748 att->length = sb.st_size;
753 static string_list_t *pgp_add_string_to_hints (string_list_t * hints, const char *str)
758 if ((scratch = m_strdup(str)) == NULL)
761 for (t = strtok (scratch, " ,.:\"()<>\n"); t;
762 t = strtok (NULL, " ,.:\"()<>\n")) {
764 hints = mutt_add_list (hints, t);
771 static pgp_key_t *pgp_get_lastp (pgp_key_t p)
773 for (; p; p = p->next)
780 pgp_key_t pgp_getkeybyaddr (address_t * a, short abilities, pgp_ring_t keyring)
783 string_list_t *hints = NULL;
788 int this_key_has_strong;
789 int this_key_has_weak;
790 int this_key_has_invalid;
793 pgp_key_t keys, k, kn;
794 pgp_key_t the_valid_key = NULL;
795 pgp_key_t matches = NULL;
796 pgp_key_t *last = &matches;
800 hints = pgp_add_string_to_hints (hints, a->mailbox);
801 if (a && a->personal)
802 hints = pgp_add_string_to_hints (hints, a->personal);
804 mutt_message (_("Looking for keys matching \"%s\"..."), a->mailbox);
805 keys = pgp_get_candidates (keyring, hints);
807 string_list_wipe(&hints);
812 for (k = keys; k; k = kn) {
815 if (abilities && !(k->flags & abilities)) {
819 this_key_has_weak = 0; /* weak but valid match */
820 this_key_has_invalid = 0; /* invalid match */
821 this_key_has_strong = 0; /* strong and valid match */
822 match = 0; /* any match */
824 for (q = k->address; q; q = q->next) {
825 r = rfc822_parse_adrlist (NULL, q->addr);
827 for (p = r; p; p = p->next) {
828 int validity = pgp_id_matches_addr (a, p, q);
830 if (validity & PGP_KV_MATCH) /* something matches */
833 /* is this key a strong candidate? */
834 if ((validity & PGP_KV_VALID) && (validity & PGP_KV_STRONGID)
835 && (validity & PGP_KV_ADDR)) {
836 if (the_valid_key && the_valid_key != k)
839 this_key_has_strong = 1;
841 else if ((validity & PGP_KV_MATCH) && !(validity & PGP_KV_VALID))
842 this_key_has_invalid = 1;
843 else if ((validity & PGP_KV_MATCH)
844 && (!(validity & PGP_KV_STRONGID)
845 || !(validity & PGP_KV_ADDR)))
846 this_key_has_weak = 1;
849 address_list_wipe(&r);
852 if (match && !this_key_has_strong && this_key_has_invalid)
854 if (match && !this_key_has_strong && this_key_has_weak)
858 *last = pgp_principal_key (k);
859 kn = pgp_remove_key (&keys, *last);
860 last = pgp_get_lastp (k);
864 pgp_free_key (&keys);
867 if (the_valid_key && !multi /* && !weak
868 && !(invalid && option (OPTPGPSHOWUNUSABLE)) */ ) {
870 * There was precisely one strong match on a valid ID.
872 * Proceed without asking the user.
874 pgp_remove_key (&matches, the_valid_key);
875 pgp_free_key (&matches);
880 * Else: Ask the user.
882 if ((k = pgp_select_key (matches, a, NULL)))
883 pgp_remove_key (&matches, k);
884 pgp_free_key (&matches);
893 pgp_key_t pgp_getkeybystr (char *p, short abilities, pgp_ring_t keyring)
895 string_list_t *hints = NULL;
897 pgp_key_t matches = NULL;
898 pgp_key_t *last = &matches;
903 mutt_message (_("Looking for keys matching \"%s\"..."), p);
905 hints = pgp_add_string_to_hints (hints, p);
906 keys = pgp_get_candidates (keyring, hints);
907 string_list_wipe(&hints);
913 for (k = keys; k; k = kn) {
915 if (abilities && !(k->flags & abilities))
920 for (a = k->address; a; a = a->next) {
921 if (!*p || m_strcasecmp(p, pgp_keyid (k)) == 0
922 || (!m_strncasecmp(p, "0x", 2)
923 && !m_strcasecmp(p + 2, pgp_keyid (k)))
924 || (option (OPTPGPLONGIDS) && !m_strncasecmp(p, "0x", 2)
925 && !m_strcasecmp(p + 2, k->keyid + 8))
926 || m_stristr(a->addr, p)) {
933 *last = pgp_principal_key (k);
934 kn = pgp_remove_key (&keys, *last);
935 last = pgp_get_lastp (k);
939 pgp_free_key (&keys);
942 if ((k = pgp_select_key (matches, NULL, p)))
943 pgp_remove_key (&matches, k);
945 pgp_free_key (&matches);