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/mem.h>
16 #include <lib-lib/str.h>
17 #include <lib-lib/ascii.h>
18 #include <lib-lib/macros.h>
19 #include <lib-lib/file.h>
21 #include <lib-mime/mime.h>
23 #include <lib-ui/curses.h>
24 #include <lib-ui/enter.h>
25 #include <lib-ui/menu.h>
28 #include "recvattach.h"
46 struct pgp_cache *next;
49 static struct pgp_cache *id_defaults = NULL;
51 static char trust_flags[] = "?- +";
53 static char *pgp_key_abilities (int flags)
57 if (!(flags & KEYFLAG_CANENCRYPT))
59 else if (flags & KEYFLAG_PREFER_SIGNING)
64 if (!(flags & KEYFLAG_CANSIGN))
66 else if (flags & KEYFLAG_PREFER_ENCRYPTION)
76 static char pgp_flags (int flags)
78 if (flags & KEYFLAG_REVOKED)
80 else if (flags & KEYFLAG_EXPIRED)
82 else if (flags & KEYFLAG_DISABLED)
84 else if (flags & KEYFLAG_CRITICAL)
90 static pgp_key_t pgp_principal_key (pgp_key_t key)
92 if (key->flags & KEYFLAG_SUBKEY && key->parent)
99 * Format an entry on the PGP key selection menu.
102 * %k key id %K key id of the principal key
104 * %a algorithm %A algorithm of the princ. key
105 * %l length %L length of the princ. key
106 * %f flags %F flags of the princ. key
107 * %c capabilities %C capabilities of the princ. key
108 * %t trust/validity of the key-uid association
109 * %[...] date of key using strftime(3)
112 typedef struct pgp_entry {
118 pgp_entry_fmt (char *dest, ssize_t destlen, char op,
119 const char *src, const char *prefix,
120 const char *ifstring, const char *elsestring,
121 unsigned long data, format_flag flags)
128 int optional = (flags & M_FORMAT_OPTIONAL);
130 entry = (pgp_entry_t *) data;
133 pkey = pgp_principal_key (key);
135 if (isupper ((unsigned char) op))
138 kflags = key->flags | (pkey->flags & KEYFLAG_RESTRICTIONS)
141 switch (ascii_tolower (op)) {
146 char buf2[SHORT_STRING], *p;
162 while (len > 0 && *cp != ']') {
171 break; /* not enough space */
181 if (do_locales && Locale)
182 setlocale (LC_TIME, Locale);
184 tm = localtime (&key->gen_time);
186 strftime (buf2, sizeof (buf2), dest, tm);
189 setlocale (LC_TIME, "C");
191 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
192 snprintf (dest, destlen, fmt, buf2);
199 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
200 snprintf (dest, destlen, fmt, entry->num);
205 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
206 snprintf (dest, destlen, fmt, _pgp_keyid (key));
211 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
212 snprintf (dest, destlen, fmt, uid->addr);
217 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
218 snprintf (dest, destlen, fmt, key->algorithm);
223 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
224 snprintf (dest, destlen, fmt, key->keylen);
229 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
230 snprintf (dest, destlen, fmt, pgp_flags (kflags));
232 else if (!(kflags & (KEYFLAG_RESTRICTIONS)))
237 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
238 snprintf (dest, destlen, fmt, pgp_key_abilities (kflags));
240 else if (!(kflags & (KEYFLAG_ABILITIES)))
245 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
246 snprintf (dest, destlen, fmt, trust_flags[uid->trust & 0x03]);
248 else if (!(uid->trust & 0x03))
249 /* undefined trust */
257 mutt_FormatString (dest, destlen, ifstring, mutt_attach_fmt, data, 0);
258 else if (flags & M_FORMAT_OPTIONAL)
259 mutt_FormatString (dest, destlen, elsestring, mutt_attach_fmt, data, 0);
263 static void pgp_entry (char *s, ssize_t l, MUTTMENU * menu, int num)
265 pgp_uid_t **KeyTable = (pgp_uid_t **) menu->data;
268 entry.uid = KeyTable[num];
271 mutt_FormatString (s, l, NONULL (PgpEntryFormat), pgp_entry_fmt,
272 (unsigned long) &entry, M_FORMAT_ARROWCURSOR);
275 static int _pgp_compare_address (const void *a, const void *b)
279 pgp_uid_t **s = (pgp_uid_t **) a;
280 pgp_uid_t **t = (pgp_uid_t **) b;
282 if ((r = m_strcasecmp((*s)->addr, (*t)->addr)))
285 return (m_strcasecmp(_pgp_keyid ((*s)->parent),
286 _pgp_keyid ((*t)->parent)) > 0);
289 static int pgp_compare_address (const void *a, const void *b)
291 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_address (a, b)
292 : _pgp_compare_address (a, b));
297 static int _pgp_compare_keyid (const void *a, const void *b)
301 pgp_uid_t **s = (pgp_uid_t **) a;
302 pgp_uid_t **t = (pgp_uid_t **) b;
304 if ((r = m_strcasecmp(_pgp_keyid ((*s)->parent),
305 _pgp_keyid ((*t)->parent))))
308 return (m_strcasecmp((*s)->addr, (*t)->addr)) > 0;
311 static int pgp_compare_keyid (const void *a, const void *b)
313 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_keyid (a, b)
314 : _pgp_compare_keyid (a, b));
317 static int _pgp_compare_date (const void *a, const void *b)
320 pgp_uid_t **s = (pgp_uid_t **) a;
321 pgp_uid_t **t = (pgp_uid_t **) b;
323 if ((r = ((*s)->parent->gen_time - (*t)->parent->gen_time)))
325 return (m_strcasecmp((*s)->addr, (*t)->addr)) > 0;
328 static int pgp_compare_date (const void *a, const void *b)
330 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_date (a, b)
331 : _pgp_compare_date (a, b));
334 static int _pgp_compare_trust (const void *a, const void *b)
338 pgp_uid_t **s = (pgp_uid_t **) a;
339 pgp_uid_t **t = (pgp_uid_t **) b;
341 if ((r = (((*s)->parent->flags & (KEYFLAG_RESTRICTIONS))
342 - ((*t)->parent->flags & (KEYFLAG_RESTRICTIONS)))))
344 if ((r = ((*s)->trust - (*t)->trust)))
346 if ((r = ((*s)->parent->keylen - (*t)->parent->keylen)))
348 if ((r = ((*s)->parent->gen_time - (*t)->parent->gen_time)))
350 if ((r = m_strcasecmp((*s)->addr, (*t)->addr)))
352 return (m_strcasecmp(_pgp_keyid ((*s)->parent),
353 _pgp_keyid ((*t)->parent))) > 0;
356 static int pgp_compare_trust (const void *a, const void *b)
358 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_trust (a, b)
359 : _pgp_compare_trust (a, b));
362 static int pgp_key_is_valid (pgp_key_t k)
364 pgp_key_t pk = pgp_principal_key (k);
366 if (k->flags & KEYFLAG_CANTUSE)
368 if (pk->flags & KEYFLAG_CANTUSE)
374 static int pgp_id_is_strong (pgp_uid_t * uid)
376 if ((uid->trust & 3) < 3)
382 static int pgp_id_is_valid (pgp_uid_t * uid)
384 if (!pgp_key_is_valid (uid->parent))
386 if (uid->flags & KEYFLAG_CANTUSE)
392 #define PGP_KV_VALID 1
393 #define PGP_KV_ADDR 2
394 #define PGP_KV_STRING 4
395 #define PGP_KV_STRONGID 8
397 #define PGP_KV_MATCH (PGP_KV_ADDR|PGP_KV_STRING)
399 static int pgp_id_matches_addr (address_t * addr, address_t * u_addr,
404 if (pgp_id_is_valid (uid))
407 if (pgp_id_is_strong (uid))
408 rv |= PGP_KV_STRONGID;
410 if (addr->mailbox && u_addr->mailbox
411 && m_strcasecmp(addr->mailbox, u_addr->mailbox) == 0)
414 if (addr->personal && u_addr->personal
415 && m_strcasecmp(addr->personal, u_addr->personal) == 0)
421 static pgp_key_t pgp_select_key (pgp_key_t keys, address_t * p, const char *s)
424 pgp_uid_t **KeyTable;
427 char helpstr[SHORT_STRING], buf[LONG_STRING], tmpbuf[STRING];
428 char cmd[LONG_STRING], tempfile[_POSIX_PATH_MAX];
433 int (*f) (const void *, const void *);
440 for (i = 0, kp = keys; kp; kp = kp->next) {
441 if (!option (OPTPGPSHOWUNUSABLE) && (kp->flags & KEYFLAG_CANTUSE)) {
446 for (a = kp->address; a; a = a->next) {
447 if (!option (OPTPGPSHOWUNUSABLE) && (a->flags & KEYFLAG_CANTUSE)) {
454 p_realloc(&KeyTable, keymax);
461 if (!i && unusable) {
462 mutt_error _("All matching keys are expired, revoked, or disabled.");
468 switch (PgpSortKeys & SORT_MASK) {
470 f = pgp_compare_date;
473 f = pgp_compare_keyid;
476 f = pgp_compare_address;
480 f = pgp_compare_trust;
483 qsort (KeyTable, i, sizeof (pgp_uid_t *), f);
486 mutt_make_help (buf, sizeof (buf), _("Exit "), MENU_PGP, OP_EXIT);
487 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
488 mutt_make_help (buf, sizeof (buf), _("Select "), MENU_PGP,
489 OP_GENERIC_SELECT_ENTRY);
490 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
491 mutt_make_help (buf, sizeof (buf), _("Check key "), MENU_PGP,
493 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
494 mutt_make_help (buf, sizeof (buf), _("Help"), MENU_PGP, OP_HELP);
495 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
497 menu = mutt_new_menu ();
499 menu->make_entry = pgp_entry;
500 menu->menu = MENU_PGP;
501 menu->help = helpstr;
502 menu->data = KeyTable;
505 snprintf (buf, sizeof (buf), _("PGP keys matching <%s>."), p->mailbox);
507 snprintf (buf, sizeof (buf), _("PGP keys matching \"%s\"."), s);
517 switch (mutt_menuLoop (menu)) {
521 mutt_mktemp (tempfile);
522 if ((devnull = fopen ("/dev/null", "w")) == NULL) { /* __FOPEN_CHECKED__ */
523 mutt_perror (_("Can't open /dev/null"));
527 if ((fp = safe_fopen (tempfile, "w")) == NULL) {
529 mutt_perror (_("Can't create temporary file"));
534 mutt_message _("Invoking PGP...");
536 snprintf (tmpbuf, sizeof (tmpbuf), "0x%s",
537 pgp_keyid (pgp_principal_key
538 (KeyTable[menu->current]->parent)));
540 if ((thepid = pgp_invoke_verify_key (NULL, NULL, NULL, -1,
541 fileno (fp), fileno (devnull),
543 mutt_perror (_("Can't create filter"));
550 mutt_wait_filter (thepid);
554 snprintf (cmd, sizeof (cmd), _("Key ID: 0x%s"),
555 pgp_keyid (pgp_principal_key
556 (KeyTable[menu->current]->parent)));
557 mutt_do_pager (cmd, tempfile, 0, NULL);
558 menu->redraw = REDRAW_FULL;
564 mutt_message ("%s", KeyTable[menu->current]->addr);
567 case OP_GENERIC_SELECT_ENTRY:
570 /* XXX make error reporting more verbose */
572 if (option (OPTPGPCHECKTRUST))
573 if (!pgp_key_is_valid (KeyTable[menu->current]->parent)) {
574 mutt_error _("This key can't be used: expired/disabled/revoked.");
579 if (option (OPTPGPCHECKTRUST) &&
580 (!pgp_id_is_valid (KeyTable[menu->current])
581 || !pgp_id_is_strong (KeyTable[menu->current]))) {
583 char buff[LONG_STRING];
585 if (KeyTable[menu->current]->flags & KEYFLAG_CANTUSE)
586 q = N_("ID is expired/disabled/revoked.");
588 switch (KeyTable[menu->current]->trust & 0x03) {
590 q = N_("ID has undefined validity.");
593 q = N_("ID is not valid.");
596 q = N_("ID is only marginally valid.");
600 snprintf (buff, sizeof (buff),
601 _("%s Do you really want to use the key?"), _(q));
603 if (mutt_yesorno (buff, M_NO) != M_YES) {
610 kp = pgp_principal_key (KeyTable[menu->current]->parent);
612 kp = KeyTable[menu->current]->parent;
625 mutt_menuDestroy (&menu);
628 set_option (OPTNEEDREDRAW);
633 pgp_key_t pgp_ask_for_key (char *tag, char *whatfor,
634 short abilities, pgp_ring_t keyring)
637 char resp[SHORT_STRING];
638 struct pgp_cache *l = NULL;
645 for (l = id_defaults; l; l = l->next)
646 if (!m_strcasecmp(whatfor, l->what)) {
647 m_strcpy(resp, sizeof(resp), NONULL(l->dflt));
655 if (mutt_get_field (tag, resp, sizeof (resp), M_CLEAR) != 0)
660 m_strreplace(&l->dflt, resp);
662 l = p_new(struct pgp_cache, 1);
663 l->next = id_defaults;
665 l->what = m_strdup(whatfor);
666 l->dflt = m_strdup(resp);
670 if ((key = pgp_getkeybystr (resp, abilities, keyring)))
678 /* generate a public key attachment */
680 BODY *pgp_make_key_attachment (char *tempf)
683 char buff[LONG_STRING];
684 char tempfb[_POSIX_PATH_MAX], tmp[STRING];
691 unset_option (OPTPGPCHECKTRUST);
694 pgp_ask_for_key (_("Please enter the key ID: "), NULL, 0, PGP_PUBRING);
699 snprintf (tmp, sizeof (tmp), "0x%s", pgp_keyid (pgp_principal_key (key)));
703 mutt_mktemp (tempfb);
707 if ((tempfp = safe_fopen (tempf, tempf == tempfb ? "w" : "a")) == NULL) {
708 mutt_perror (_("Can't create temporary file"));
713 if ((devnull = fopen ("/dev/null", "w")) == NULL) { /* __FOPEN_CHECKED__ */
714 mutt_perror (_("Can't open /dev/null"));
722 mutt_message _("Invoking pgp...");
726 pgp_invoke_export (NULL, NULL, NULL, -1,
727 fileno (tempfp), fileno (devnull), tmp)) == -1) {
728 mutt_perror (_("Can't create filter"));
736 mutt_wait_filter (thepid);
742 att->filename = m_strdup(tempf);
745 att->type = TYPEAPPLICATION;
746 att->subtype = m_strdup("pgp-keys");
747 snprintf (buff, sizeof (buff), _("PGP Key %s."), tmp);
748 att->description = m_strdup(buff);
749 mutt_update_encoding (att);
752 att->length = sb.st_size;
757 static string_list_t *pgp_add_string_to_hints (string_list_t * hints, const char *str)
762 if ((scratch = m_strdup(str)) == NULL)
765 for (t = strtok (scratch, " ,.:\"()<>\n"); t;
766 t = strtok (NULL, " ,.:\"()<>\n")) {
768 hints = mutt_add_list (hints, t);
775 static pgp_key_t *pgp_get_lastp (pgp_key_t p)
777 for (; p; p = p->next)
784 pgp_key_t pgp_getkeybyaddr (address_t * a, short abilities, pgp_ring_t keyring)
787 string_list_t *hints = NULL;
792 int this_key_has_strong;
793 int this_key_has_weak;
794 int this_key_has_invalid;
797 pgp_key_t keys, k, kn;
798 pgp_key_t the_valid_key = NULL;
799 pgp_key_t matches = NULL;
800 pgp_key_t *last = &matches;
804 hints = pgp_add_string_to_hints (hints, a->mailbox);
805 if (a && a->personal)
806 hints = pgp_add_string_to_hints (hints, a->personal);
808 mutt_message (_("Looking for keys matching \"%s\"..."), a->mailbox);
809 keys = pgp_get_candidates (keyring, hints);
811 string_list_wipe(&hints);
816 for (k = keys; k; k = kn) {
819 if (abilities && !(k->flags & abilities)) {
823 this_key_has_weak = 0; /* weak but valid match */
824 this_key_has_invalid = 0; /* invalid match */
825 this_key_has_strong = 0; /* strong and valid match */
826 match = 0; /* any match */
828 for (q = k->address; q; q = q->next) {
829 r = rfc822_parse_adrlist (NULL, q->addr);
831 for (p = r; p; p = p->next) {
832 int validity = pgp_id_matches_addr (a, p, q);
834 if (validity & PGP_KV_MATCH) /* something matches */
837 /* is this key a strong candidate? */
838 if ((validity & PGP_KV_VALID) && (validity & PGP_KV_STRONGID)
839 && (validity & PGP_KV_ADDR)) {
840 if (the_valid_key && the_valid_key != k)
843 this_key_has_strong = 1;
845 else if ((validity & PGP_KV_MATCH) && !(validity & PGP_KV_VALID))
846 this_key_has_invalid = 1;
847 else if ((validity & PGP_KV_MATCH)
848 && (!(validity & PGP_KV_STRONGID)
849 || !(validity & PGP_KV_ADDR)))
850 this_key_has_weak = 1;
853 address_list_wipe(&r);
856 if (match && !this_key_has_strong && this_key_has_invalid)
858 if (match && !this_key_has_strong && this_key_has_weak)
862 *last = pgp_principal_key (k);
863 kn = pgp_remove_key (&keys, *last);
864 last = pgp_get_lastp (k);
868 pgp_free_key (&keys);
871 if (the_valid_key && !multi /* && !weak
872 && !(invalid && option (OPTPGPSHOWUNUSABLE)) */ ) {
874 * There was precisely one strong match on a valid ID.
876 * Proceed without asking the user.
878 pgp_remove_key (&matches, the_valid_key);
879 pgp_free_key (&matches);
884 * Else: Ask the user.
886 if ((k = pgp_select_key (matches, a, NULL)))
887 pgp_remove_key (&matches, k);
888 pgp_free_key (&matches);
897 pgp_key_t pgp_getkeybystr (char *p, short abilities, pgp_ring_t keyring)
899 string_list_t *hints = NULL;
901 pgp_key_t matches = NULL;
902 pgp_key_t *last = &matches;
907 mutt_message (_("Looking for keys matching \"%s\"..."), p);
909 hints = pgp_add_string_to_hints (hints, p);
910 keys = pgp_get_candidates (keyring, hints);
911 string_list_wipe(&hints);
917 for (k = keys; k; k = kn) {
919 if (abilities && !(k->flags & abilities))
924 for (a = k->address; a; a = a->next) {
925 if (!*p || m_strcasecmp(p, pgp_keyid (k)) == 0
926 || (!m_strncasecmp(p, "0x", 2)
927 && !m_strcasecmp(p + 2, pgp_keyid (k)))
928 || (option (OPTPGPLONGIDS) && !m_strncasecmp(p, "0x", 2)
929 && !m_strcasecmp(p + 2, k->keyid + 8))
930 || m_stristr(a->addr, p)) {
937 *last = pgp_principal_key (k);
938 kn = pgp_remove_key (&keys, *last);
939 last = pgp_get_lastp (k);
943 pgp_free_key (&keys);
946 if ((k = pgp_select_key (matches, NULL, p)))
947 pgp_remove_key (&matches, k);
949 pgp_free_key (&matches);