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>
20 #include <lib-lib/debug.h>
22 #include <lib-mime/mime.h>
24 #include <lib-ui/curses.h>
25 #include <lib-ui/enter.h>
26 #include <lib-ui/menu.h>
29 #include "recvattach.h"
47 struct pgp_cache *next;
50 static struct pgp_cache *id_defaults = NULL;
52 static char trust_flags[] = "?- +";
54 static char *pgp_key_abilities (int flags)
58 if (!(flags & KEYFLAG_CANENCRYPT))
60 else if (flags & KEYFLAG_PREFER_SIGNING)
65 if (!(flags & KEYFLAG_CANSIGN))
67 else if (flags & KEYFLAG_PREFER_ENCRYPTION)
77 static char pgp_flags (int flags)
79 if (flags & KEYFLAG_REVOKED)
81 else if (flags & KEYFLAG_EXPIRED)
83 else if (flags & KEYFLAG_DISABLED)
85 else if (flags & KEYFLAG_CRITICAL)
91 static pgp_key_t pgp_principal_key (pgp_key_t key)
93 if (key->flags & KEYFLAG_SUBKEY && key->parent)
100 * Format an entry on the PGP key selection menu.
103 * %k key id %K key id of the principal key
105 * %a algorithm %A algorithm of the princ. key
106 * %l length %L length of the princ. key
107 * %f flags %F flags of the princ. key
108 * %c capabilities %C capabilities of the princ. key
109 * %t trust/validity of the key-uid association
110 * %[...] date of key using strftime(3)
113 typedef struct pgp_entry {
119 pgp_entry_fmt (char *dest, ssize_t destlen, char op,
120 const char *src, const char *prefix,
121 const char *ifstring, const char *elsestring,
122 unsigned long data, format_flag flags)
129 int optional = (flags & M_FORMAT_OPTIONAL);
131 entry = (pgp_entry_t *) data;
134 pkey = pgp_principal_key (key);
136 if (isupper ((unsigned char) op))
139 kflags = key->flags | (pkey->flags & KEYFLAG_RESTRICTIONS)
142 switch (ascii_tolower (op)) {
147 char buf2[SHORT_STRING], *p;
163 while (len > 0 && *cp != ']') {
172 break; /* not enough space */
182 if (do_locales && Locale)
183 setlocale (LC_TIME, Locale);
185 tm = localtime (&key->gen_time);
187 strftime (buf2, sizeof (buf2), dest, tm);
190 setlocale (LC_TIME, "C");
192 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
193 snprintf (dest, destlen, fmt, buf2);
200 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
201 snprintf (dest, destlen, fmt, entry->num);
206 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
207 snprintf (dest, destlen, fmt, _pgp_keyid (key));
212 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
213 snprintf (dest, destlen, fmt, uid->addr);
218 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
219 snprintf (dest, destlen, fmt, key->algorithm);
224 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
225 snprintf (dest, destlen, fmt, key->keylen);
230 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
231 snprintf (dest, destlen, fmt, pgp_flags (kflags));
233 else if (!(kflags & (KEYFLAG_RESTRICTIONS)))
238 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
239 snprintf (dest, destlen, fmt, pgp_key_abilities (kflags));
241 else if (!(kflags & (KEYFLAG_ABILITIES)))
246 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
247 snprintf (dest, destlen, fmt, trust_flags[uid->trust & 0x03]);
249 else if (!(uid->trust & 0x03))
250 /* undefined trust */
258 mutt_FormatString (dest, destlen, ifstring, mutt_attach_fmt, data, 0);
259 else if (flags & M_FORMAT_OPTIONAL)
260 mutt_FormatString (dest, destlen, elsestring, mutt_attach_fmt, data, 0);
264 static void pgp_entry (char *s, ssize_t l, MUTTMENU * menu, int num)
266 pgp_uid_t **KeyTable = (pgp_uid_t **) menu->data;
269 entry.uid = KeyTable[num];
272 mutt_FormatString (s, l, NONULL (PgpEntryFormat), pgp_entry_fmt,
273 (unsigned long) &entry, M_FORMAT_ARROWCURSOR);
276 static int _pgp_compare_address (const void *a, const void *b)
280 pgp_uid_t **s = (pgp_uid_t **) a;
281 pgp_uid_t **t = (pgp_uid_t **) b;
283 if ((r = m_strcasecmp((*s)->addr, (*t)->addr)))
286 return (m_strcasecmp(_pgp_keyid ((*s)->parent),
287 _pgp_keyid ((*t)->parent)) > 0);
290 static int pgp_compare_address (const void *a, const void *b)
292 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_address (a, b)
293 : _pgp_compare_address (a, b));
298 static int _pgp_compare_keyid (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 = m_strcasecmp(_pgp_keyid ((*s)->parent),
306 _pgp_keyid ((*t)->parent))))
309 return (m_strcasecmp((*s)->addr, (*t)->addr)) > 0;
312 static int pgp_compare_keyid (const void *a, const void *b)
314 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_keyid (a, b)
315 : _pgp_compare_keyid (a, b));
318 static int _pgp_compare_date (const void *a, const void *b)
321 pgp_uid_t **s = (pgp_uid_t **) a;
322 pgp_uid_t **t = (pgp_uid_t **) b;
324 if ((r = ((*s)->parent->gen_time - (*t)->parent->gen_time)))
326 return (m_strcasecmp((*s)->addr, (*t)->addr)) > 0;
329 static int pgp_compare_date (const void *a, const void *b)
331 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_date (a, b)
332 : _pgp_compare_date (a, b));
335 static int _pgp_compare_trust (const void *a, const void *b)
339 pgp_uid_t **s = (pgp_uid_t **) a;
340 pgp_uid_t **t = (pgp_uid_t **) b;
342 if ((r = (((*s)->parent->flags & (KEYFLAG_RESTRICTIONS))
343 - ((*t)->parent->flags & (KEYFLAG_RESTRICTIONS)))))
345 if ((r = ((*s)->trust - (*t)->trust)))
347 if ((r = ((*s)->parent->keylen - (*t)->parent->keylen)))
349 if ((r = ((*s)->parent->gen_time - (*t)->parent->gen_time)))
351 if ((r = m_strcasecmp((*s)->addr, (*t)->addr)))
353 return (m_strcasecmp(_pgp_keyid ((*s)->parent),
354 _pgp_keyid ((*t)->parent))) > 0;
357 static int pgp_compare_trust (const void *a, const void *b)
359 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_trust (a, b)
360 : _pgp_compare_trust (a, b));
363 static int pgp_key_is_valid (pgp_key_t k)
365 pgp_key_t pk = pgp_principal_key (k);
367 if (k->flags & KEYFLAG_CANTUSE)
369 if (pk->flags & KEYFLAG_CANTUSE)
375 static int pgp_id_is_strong (pgp_uid_t * uid)
377 if ((uid->trust & 3) < 3)
383 static int pgp_id_is_valid (pgp_uid_t * uid)
385 if (!pgp_key_is_valid (uid->parent))
387 if (uid->flags & KEYFLAG_CANTUSE)
393 #define PGP_KV_VALID 1
394 #define PGP_KV_ADDR 2
395 #define PGP_KV_STRING 4
396 #define PGP_KV_STRONGID 8
398 #define PGP_KV_MATCH (PGP_KV_ADDR|PGP_KV_STRING)
400 static int pgp_id_matches_addr (address_t * addr, address_t * u_addr,
405 if (pgp_id_is_valid (uid))
408 if (pgp_id_is_strong (uid))
409 rv |= PGP_KV_STRONGID;
411 if (addr->mailbox && u_addr->mailbox
412 && m_strcasecmp(addr->mailbox, u_addr->mailbox) == 0)
415 if (addr->personal && u_addr->personal
416 && m_strcasecmp(addr->personal, u_addr->personal) == 0)
422 static pgp_key_t pgp_select_key (pgp_key_t keys, address_t * p, const char *s)
425 pgp_uid_t **KeyTable;
428 char helpstr[SHORT_STRING], buf[LONG_STRING], tmpbuf[STRING];
429 char cmd[LONG_STRING], tempfile[_POSIX_PATH_MAX];
434 int (*f) (const void *, const void *);
441 for (i = 0, kp = keys; kp; kp = kp->next) {
442 if (!option (OPTPGPSHOWUNUSABLE) && (kp->flags & KEYFLAG_CANTUSE)) {
447 for (a = kp->address; a; a = a->next) {
448 if (!option (OPTPGPSHOWUNUSABLE) && (a->flags & KEYFLAG_CANTUSE)) {
455 p_realloc(&KeyTable, keymax);
462 if (!i && unusable) {
463 mutt_error _("All matching keys are expired, revoked, or disabled.");
469 switch (PgpSortKeys & SORT_MASK) {
471 f = pgp_compare_date;
474 f = pgp_compare_keyid;
477 f = pgp_compare_address;
481 f = pgp_compare_trust;
484 qsort (KeyTable, i, sizeof (pgp_uid_t *), f);
487 mutt_make_help (buf, sizeof (buf), _("Exit "), MENU_PGP, OP_EXIT);
488 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
489 mutt_make_help (buf, sizeof (buf), _("Select "), MENU_PGP,
490 OP_GENERIC_SELECT_ENTRY);
491 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
492 mutt_make_help (buf, sizeof (buf), _("Check key "), MENU_PGP,
494 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
495 mutt_make_help (buf, sizeof (buf), _("Help"), MENU_PGP, OP_HELP);
496 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
498 menu = mutt_new_menu ();
500 menu->make_entry = pgp_entry;
501 menu->menu = MENU_PGP;
502 menu->help = helpstr;
503 menu->data = KeyTable;
506 snprintf (buf, sizeof (buf), _("PGP keys matching <%s>."), p->mailbox);
508 snprintf (buf, sizeof (buf), _("PGP keys matching \"%s\"."), s);
518 switch (mutt_menuLoop (menu)) {
522 mutt_mktemp (tempfile);
523 if ((devnull = fopen ("/dev/null", "w")) == NULL) { /* __FOPEN_CHECKED__ */
524 mutt_perror (_("Can't open /dev/null"));
528 if ((fp = safe_fopen (tempfile, "w")) == NULL) {
530 mutt_perror (_("Can't create temporary file"));
535 mutt_message _("Invoking PGP...");
537 snprintf (tmpbuf, sizeof (tmpbuf), "0x%s",
538 pgp_keyid (pgp_principal_key
539 (KeyTable[menu->current]->parent)));
541 if ((thepid = pgp_invoke_verify_key (NULL, NULL, NULL, -1,
542 fileno (fp), fileno (devnull),
544 mutt_perror (_("Can't create filter"));
551 mutt_wait_filter (thepid);
555 snprintf (cmd, sizeof (cmd), _("Key ID: 0x%s"),
556 pgp_keyid (pgp_principal_key
557 (KeyTable[menu->current]->parent)));
558 mutt_do_pager (cmd, tempfile, 0, NULL);
559 menu->redraw = REDRAW_FULL;
565 mutt_message ("%s", KeyTable[menu->current]->addr);
568 case OP_GENERIC_SELECT_ENTRY:
571 /* XXX make error reporting more verbose */
573 if (option (OPTPGPCHECKTRUST))
574 if (!pgp_key_is_valid (KeyTable[menu->current]->parent)) {
575 mutt_error _("This key can't be used: expired/disabled/revoked.");
580 if (option (OPTPGPCHECKTRUST) &&
581 (!pgp_id_is_valid (KeyTable[menu->current])
582 || !pgp_id_is_strong (KeyTable[menu->current]))) {
584 char buff[LONG_STRING];
586 if (KeyTable[menu->current]->flags & KEYFLAG_CANTUSE)
587 q = N_("ID is expired/disabled/revoked.");
589 switch (KeyTable[menu->current]->trust & 0x03) {
591 q = N_("ID has undefined validity.");
594 q = N_("ID is not valid.");
597 q = N_("ID is only marginally valid.");
601 snprintf (buff, sizeof (buff),
602 _("%s Do you really want to use the key?"), _(q));
604 if (mutt_yesorno (buff, M_NO) != M_YES) {
611 kp = pgp_principal_key (KeyTable[menu->current]->parent);
613 kp = KeyTable[menu->current]->parent;
626 mutt_menuDestroy (&menu);
629 set_option (OPTNEEDREDRAW);
634 pgp_key_t pgp_ask_for_key (char *tag, char *whatfor,
635 short abilities, pgp_ring_t keyring)
638 char resp[SHORT_STRING];
639 struct pgp_cache *l = NULL;
646 for (l = id_defaults; l; l = l->next)
647 if (!m_strcasecmp(whatfor, l->what)) {
648 m_strcpy(resp, sizeof(resp), NONULL(l->dflt));
656 if (mutt_get_field (tag, resp, sizeof (resp), M_CLEAR) != 0)
661 m_strreplace(&l->dflt, resp);
663 l = p_new(struct pgp_cache, 1);
664 l->next = id_defaults;
666 l->what = m_strdup(whatfor);
667 l->dflt = m_strdup(resp);
671 if ((key = pgp_getkeybystr (resp, abilities, keyring)))
679 /* generate a public key attachment */
681 BODY *pgp_make_key_attachment (char *tempf)
684 char buff[LONG_STRING];
685 char tempfb[_POSIX_PATH_MAX], tmp[STRING];
692 unset_option (OPTPGPCHECKTRUST);
695 pgp_ask_for_key (_("Please enter the key ID: "), NULL, 0, PGP_PUBRING);
700 snprintf (tmp, sizeof (tmp), "0x%s", pgp_keyid (pgp_principal_key (key)));
704 mutt_mktemp (tempfb);
708 if ((tempfp = safe_fopen (tempf, tempf == tempfb ? "w" : "a")) == NULL) {
709 mutt_perror (_("Can't create temporary file"));
714 if ((devnull = fopen ("/dev/null", "w")) == NULL) { /* __FOPEN_CHECKED__ */
715 mutt_perror (_("Can't open /dev/null"));
723 mutt_message _("Invoking pgp...");
727 pgp_invoke_export (NULL, NULL, NULL, -1,
728 fileno (tempfp), fileno (devnull), tmp)) == -1) {
729 mutt_perror (_("Can't create filter"));
737 mutt_wait_filter (thepid);
742 att = mutt_new_body ();
743 att->filename = m_strdup(tempf);
746 att->type = TYPEAPPLICATION;
747 att->subtype = m_strdup("pgp-keys");
748 snprintf (buff, sizeof (buff), _("PGP Key %s."), tmp);
749 att->description = m_strdup(buff);
750 mutt_update_encoding (att);
753 att->length = sb.st_size;
758 static string_list_t *pgp_add_string_to_hints (string_list_t * hints, const char *str)
763 if ((scratch = m_strdup(str)) == NULL)
766 for (t = strtok (scratch, " ,.:\"()<>\n"); t;
767 t = strtok (NULL, " ,.:\"()<>\n")) {
769 hints = mutt_add_list (hints, t);
776 static pgp_key_t *pgp_get_lastp (pgp_key_t p)
778 for (; p; p = p->next)
785 pgp_key_t pgp_getkeybyaddr (address_t * a, short abilities, pgp_ring_t keyring)
788 string_list_t *hints = NULL;
793 int this_key_has_strong;
794 int this_key_has_weak;
795 int this_key_has_invalid;
798 pgp_key_t keys, k, kn;
799 pgp_key_t the_valid_key = NULL;
800 pgp_key_t matches = NULL;
801 pgp_key_t *last = &matches;
805 hints = pgp_add_string_to_hints (hints, a->mailbox);
806 if (a && a->personal)
807 hints = pgp_add_string_to_hints (hints, a->personal);
809 mutt_message (_("Looking for keys matching \"%s\"..."), a->mailbox);
810 keys = pgp_get_candidates (keyring, hints);
812 string_list_wipe(&hints);
817 debug_print (5, ("looking for %s <%s>\n", a->personal, a->mailbox));
819 for (k = keys; k; k = kn) {
822 debug_print (5, (" looking at key: %s\n", pgp_keyid (k)));
824 if (abilities && !(k->flags & abilities)) {
825 debug_print (5, (" insufficient abilities: Has %x, want %x\n",
826 k->flags, abilities));
830 this_key_has_weak = 0; /* weak but valid match */
831 this_key_has_invalid = 0; /* invalid match */
832 this_key_has_strong = 0; /* strong and valid match */
833 match = 0; /* any match */
835 for (q = k->address; q; q = q->next) {
836 r = rfc822_parse_adrlist (NULL, q->addr);
838 for (p = r; p; p = p->next) {
839 int validity = pgp_id_matches_addr (a, p, q);
841 if (validity & PGP_KV_MATCH) /* something matches */
844 /* is this key a strong candidate? */
845 if ((validity & PGP_KV_VALID) && (validity & PGP_KV_STRONGID)
846 && (validity & PGP_KV_ADDR)) {
847 if (the_valid_key && the_valid_key != k)
850 this_key_has_strong = 1;
852 else if ((validity & PGP_KV_MATCH) && !(validity & PGP_KV_VALID))
853 this_key_has_invalid = 1;
854 else if ((validity & PGP_KV_MATCH)
855 && (!(validity & PGP_KV_STRONGID)
856 || !(validity & PGP_KV_ADDR)))
857 this_key_has_weak = 1;
860 address_list_wipe(&r);
863 if (match && !this_key_has_strong && this_key_has_invalid)
865 if (match && !this_key_has_strong && this_key_has_weak)
869 *last = pgp_principal_key (k);
870 kn = pgp_remove_key (&keys, *last);
871 last = pgp_get_lastp (k);
875 pgp_free_key (&keys);
878 if (the_valid_key && !multi /* && !weak
879 && !(invalid && option (OPTPGPSHOWUNUSABLE)) */ ) {
881 * There was precisely one strong match on a valid ID.
883 * Proceed without asking the user.
885 pgp_remove_key (&matches, the_valid_key);
886 pgp_free_key (&matches);
891 * Else: Ask the user.
893 if ((k = pgp_select_key (matches, a, NULL)))
894 pgp_remove_key (&matches, k);
895 pgp_free_key (&matches);
904 pgp_key_t pgp_getkeybystr (char *p, short abilities, pgp_ring_t keyring)
906 string_list_t *hints = NULL;
908 pgp_key_t matches = NULL;
909 pgp_key_t *last = &matches;
914 mutt_message (_("Looking for keys matching \"%s\"..."), p);
916 hints = pgp_add_string_to_hints (hints, p);
917 keys = pgp_get_candidates (keyring, hints);
918 string_list_wipe(&hints);
924 for (k = keys; k; k = kn) {
926 if (abilities && !(k->flags & abilities))
931 for (a = k->address; a; a = a->next) {
932 debug_print (5, ("matching \"%s\" against key %s, \"%s\":\n", p, pgp_keyid (k), a->addr));
933 if (!*p || m_strcasecmp(p, pgp_keyid (k)) == 0
934 || (!m_strncasecmp(p, "0x", 2)
935 && !m_strcasecmp(p + 2, pgp_keyid (k)))
936 || (option (OPTPGPLONGIDS) && !m_strncasecmp(p, "0x", 2)
937 && !m_strcasecmp(p + 2, k->keyid + 8))
938 || m_stristr(a->addr, p)) {
939 debug_print (5, ("match.\n"));
946 *last = pgp_principal_key (k);
947 kn = pgp_remove_key (&keys, *last);
948 last = pgp_get_lastp (k);
952 pgp_free_key (&keys);
955 if ((k = pgp_select_key (matches, NULL, p)))
956 pgp_remove_key (&matches, k);
958 pgp_free_key (&matches);