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.
11 #include <lib-lib/lib-lib.h>
13 #include <lib-mime/mime.h>
14 #include <lib-sys/unix.h>
16 #include <lib-ui/curses.h>
17 #include <lib-ui/enter.h>
18 #include <lib-ui/menu.h>
20 #include "recvattach.h"
28 struct pgp_cache *next;
31 static struct pgp_cache *id_defaults = NULL;
33 static char trust_flags[] = "?- +";
35 static char *pgp_key_abilities (int flags)
39 if (!(flags & KEYFLAG_CANENCRYPT))
41 else if (flags & KEYFLAG_PREFER_SIGNING)
46 if (!(flags & KEYFLAG_CANSIGN))
48 else if (flags & KEYFLAG_PREFER_ENCRYPTION)
58 static char pgp_flags (int flags)
60 if (flags & KEYFLAG_REVOKED)
62 else if (flags & KEYFLAG_EXPIRED)
64 else if (flags & KEYFLAG_DISABLED)
66 else if (flags & KEYFLAG_CRITICAL)
72 static pgp_key_t pgp_principal_key (pgp_key_t key)
74 if (key->flags & KEYFLAG_SUBKEY && key->parent)
81 * Format an entry on the PGP key selection menu.
84 * %k key id %K key id of the principal key
86 * %a algorithm %A algorithm of the princ. key
87 * %l length %L length of the princ. key
88 * %f flags %F flags of the princ. key
89 * %c capabilities %C capabilities of the princ. key
90 * %t trust/validity of the key-uid association
91 * %[...] date of key using strftime(3)
94 typedef struct pgp_entry {
100 pgp_entry_fmt (char *dest, ssize_t destlen, char op,
101 const char *src, const char *prefix,
102 const char *ifstring, const char *elsestring,
103 unsigned long data, format_flag flags)
110 int optional = (flags & M_FORMAT_OPTIONAL);
112 entry = (pgp_entry_t *) data;
115 pkey = pgp_principal_key (key);
117 if (isupper ((unsigned char) op))
120 kflags = key->flags | (pkey->flags & KEYFLAG_RESTRICTIONS)
123 switch (ascii_tolower (op)) {
128 char buf2[SHORT_STRING], *p;
144 while (len > 0 && *cp != ']') {
153 break; /* not enough space */
163 if (do_locales && Locale)
164 setlocale (LC_TIME, Locale);
166 tm = localtime (&key->gen_time);
168 strftime (buf2, sizeof (buf2), dest, tm);
171 setlocale (LC_TIME, "C");
173 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
174 snprintf (dest, destlen, fmt, buf2);
181 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
182 snprintf (dest, destlen, fmt, entry->num);
187 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
188 snprintf (dest, destlen, fmt, _pgp_keyid (key));
193 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
194 snprintf (dest, destlen, fmt, uid->addr);
199 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
200 snprintf (dest, destlen, fmt, key->algorithm);
205 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
206 snprintf (dest, destlen, fmt, key->keylen);
211 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
212 snprintf (dest, destlen, fmt, pgp_flags (kflags));
214 else if (!(kflags & (KEYFLAG_RESTRICTIONS)))
219 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
220 snprintf (dest, destlen, fmt, pgp_key_abilities (kflags));
222 else if (!(kflags & (KEYFLAG_ABILITIES)))
227 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
228 snprintf (dest, destlen, fmt, trust_flags[uid->trust & 0x03]);
230 else if (!(uid->trust & 0x03))
231 /* undefined trust */
239 mutt_FormatString (dest, destlen, ifstring, mutt_attach_fmt, data, 0);
240 else if (flags & M_FORMAT_OPTIONAL)
241 mutt_FormatString (dest, destlen, elsestring, mutt_attach_fmt, data, 0);
245 static void pgp_entry (char *s, ssize_t l, MUTTMENU * menu, int num)
247 pgp_uid_t **KeyTable = (pgp_uid_t **) menu->data;
250 entry.uid = KeyTable[num];
253 mutt_FormatString (s, l, NONULL (PgpEntryFormat), pgp_entry_fmt,
254 (unsigned long) &entry, M_FORMAT_ARROWCURSOR);
257 static int _pgp_compare_address (const void *a, const void *b)
261 pgp_uid_t **s = (pgp_uid_t **) a;
262 pgp_uid_t **t = (pgp_uid_t **) b;
264 if ((r = m_strcasecmp((*s)->addr, (*t)->addr)))
267 return (m_strcasecmp(_pgp_keyid ((*s)->parent),
268 _pgp_keyid ((*t)->parent)) > 0);
271 static int pgp_compare_address (const void *a, const void *b)
273 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_address (a, b)
274 : _pgp_compare_address (a, b));
279 static int _pgp_compare_keyid (const void *a, const void *b)
283 pgp_uid_t **s = (pgp_uid_t **) a;
284 pgp_uid_t **t = (pgp_uid_t **) b;
286 if ((r = m_strcasecmp(_pgp_keyid ((*s)->parent),
287 _pgp_keyid ((*t)->parent))))
290 return (m_strcasecmp((*s)->addr, (*t)->addr)) > 0;
293 static int pgp_compare_keyid (const void *a, const void *b)
295 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_keyid (a, b)
296 : _pgp_compare_keyid (a, b));
299 static int _pgp_compare_date (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 = ((*s)->parent->gen_time - (*t)->parent->gen_time)))
307 return (m_strcasecmp((*s)->addr, (*t)->addr)) > 0;
310 static int pgp_compare_date (const void *a, const void *b)
312 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_date (a, b)
313 : _pgp_compare_date (a, b));
316 static int _pgp_compare_trust (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->flags & (KEYFLAG_RESTRICTIONS))
324 - ((*t)->parent->flags & (KEYFLAG_RESTRICTIONS)))))
326 if ((r = ((*s)->trust - (*t)->trust)))
328 if ((r = ((*s)->parent->keylen - (*t)->parent->keylen)))
330 if ((r = ((*s)->parent->gen_time - (*t)->parent->gen_time)))
332 if ((r = m_strcasecmp((*s)->addr, (*t)->addr)))
334 return (m_strcasecmp(_pgp_keyid ((*s)->parent),
335 _pgp_keyid ((*t)->parent))) > 0;
338 static int pgp_compare_trust (const void *a, const void *b)
340 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_trust (a, b)
341 : _pgp_compare_trust (a, b));
344 static int pgp_key_is_valid (pgp_key_t k)
346 pgp_key_t pk = pgp_principal_key (k);
348 if (k->flags & KEYFLAG_CANTUSE)
350 if (pk->flags & KEYFLAG_CANTUSE)
356 static int pgp_id_is_strong (pgp_uid_t * uid)
358 if ((uid->trust & 3) < 3)
364 static int pgp_id_is_valid (pgp_uid_t * uid)
366 if (!pgp_key_is_valid (uid->parent))
368 if (uid->flags & KEYFLAG_CANTUSE)
374 #define PGP_KV_VALID 1
375 #define PGP_KV_ADDR 2
376 #define PGP_KV_STRING 4
377 #define PGP_KV_STRONGID 8
379 #define PGP_KV_MATCH (PGP_KV_ADDR|PGP_KV_STRING)
381 static int pgp_id_matches_addr (address_t * addr, address_t * u_addr,
386 if (pgp_id_is_valid (uid))
389 if (pgp_id_is_strong (uid))
390 rv |= PGP_KV_STRONGID;
392 if (addr->mailbox && u_addr->mailbox
393 && m_strcasecmp(addr->mailbox, u_addr->mailbox) == 0)
396 if (addr->personal && u_addr->personal
397 && m_strcasecmp(addr->personal, u_addr->personal) == 0)
403 static pgp_key_t pgp_select_key (pgp_key_t keys, address_t * p, const char *s)
406 pgp_uid_t **KeyTable;
409 char helpstr[SHORT_STRING], buf[LONG_STRING], tmpbuf[STRING];
410 char cmd[LONG_STRING], tempfile[_POSIX_PATH_MAX];
415 int (*f) (const void *, const void *);
422 for (i = 0, kp = keys; kp; kp = kp->next) {
423 if (!option (OPTPGPSHOWUNUSABLE) && (kp->flags & KEYFLAG_CANTUSE)) {
428 for (a = kp->address; a; a = a->next) {
429 if (!option (OPTPGPSHOWUNUSABLE) && (a->flags & KEYFLAG_CANTUSE)) {
436 p_realloc(&KeyTable, keymax);
443 if (!i && unusable) {
444 mutt_error _("All matching keys are expired, revoked, or disabled.");
450 switch (PgpSortKeys & SORT_MASK) {
452 f = pgp_compare_date;
455 f = pgp_compare_keyid;
458 f = pgp_compare_address;
462 f = pgp_compare_trust;
465 qsort (KeyTable, i, sizeof (pgp_uid_t *), f);
468 mutt_make_help (buf, sizeof (buf), _("Exit "), MENU_PGP, OP_EXIT);
469 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
470 mutt_make_help (buf, sizeof (buf), _("Select "), MENU_PGP,
471 OP_GENERIC_SELECT_ENTRY);
472 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
473 mutt_make_help (buf, sizeof (buf), _("Check key "), MENU_PGP,
475 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
476 mutt_make_help (buf, sizeof (buf), _("Help"), MENU_PGP, OP_HELP);
477 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
479 menu = mutt_new_menu ();
481 menu->make_entry = pgp_entry;
482 menu->menu = MENU_PGP;
483 menu->help = helpstr;
484 menu->data = KeyTable;
487 snprintf (buf, sizeof (buf), _("PGP keys matching <%s>."), p->mailbox);
489 snprintf (buf, sizeof (buf), _("PGP keys matching \"%s\"."), s);
499 switch (mutt_menuLoop (menu)) {
503 mutt_mktemp (tempfile);
504 if ((devnull = fopen ("/dev/null", "w")) == NULL) { /* __FOPEN_CHECKED__ */
505 mutt_perror (_("Can't open /dev/null"));
509 if ((fp = safe_fopen (tempfile, "w")) == NULL) {
511 mutt_perror (_("Can't create temporary file"));
516 mutt_message _("Invoking PGP...");
518 snprintf (tmpbuf, sizeof (tmpbuf), "0x%s",
519 pgp_keyid (pgp_principal_key
520 (KeyTable[menu->current]->parent)));
522 if ((thepid = pgp_invoke_verify_key (NULL, NULL, NULL, -1,
523 fileno (fp), fileno (devnull),
525 mutt_perror (_("Can't create filter"));
532 mutt_wait_filter (thepid);
536 snprintf (cmd, sizeof (cmd), _("Key ID: 0x%s"),
537 pgp_keyid (pgp_principal_key
538 (KeyTable[menu->current]->parent)));
539 mutt_do_pager (cmd, tempfile, 0, NULL);
540 menu->redraw = REDRAW_FULL;
546 mutt_message ("%s", KeyTable[menu->current]->addr);
549 case OP_GENERIC_SELECT_ENTRY:
552 /* XXX make error reporting more verbose */
554 if (option (OPTPGPCHECKTRUST))
555 if (!pgp_key_is_valid (KeyTable[menu->current]->parent)) {
556 mutt_error _("This key can't be used: expired/disabled/revoked.");
561 if (option (OPTPGPCHECKTRUST) &&
562 (!pgp_id_is_valid (KeyTable[menu->current])
563 || !pgp_id_is_strong (KeyTable[menu->current]))) {
565 char buff[LONG_STRING];
567 if (KeyTable[menu->current]->flags & KEYFLAG_CANTUSE)
568 q = N_("ID is expired/disabled/revoked.");
570 switch (KeyTable[menu->current]->trust & 0x03) {
572 q = N_("ID has undefined validity.");
575 q = N_("ID is not valid.");
578 q = N_("ID is only marginally valid.");
582 snprintf (buff, sizeof (buff),
583 _("%s Do you really want to use the key?"), _(q));
585 if (mutt_yesorno (buff, M_NO) != M_YES) {
592 kp = pgp_principal_key (KeyTable[menu->current]->parent);
594 kp = KeyTable[menu->current]->parent;
607 mutt_menuDestroy (&menu);
610 set_option (OPTNEEDREDRAW);
615 pgp_key_t pgp_ask_for_key (char *tag, char *whatfor,
616 short abilities, pgp_ring_t keyring)
619 char resp[SHORT_STRING];
620 struct pgp_cache *l = NULL;
627 for (l = id_defaults; l; l = l->next)
628 if (!m_strcasecmp(whatfor, l->what)) {
629 m_strcpy(resp, sizeof(resp), NONULL(l->dflt));
637 if (mutt_get_field (tag, resp, sizeof (resp), M_CLEAR) != 0)
642 m_strreplace(&l->dflt, resp);
644 l = p_new(struct pgp_cache, 1);
645 l->next = id_defaults;
647 l->what = m_strdup(whatfor);
648 l->dflt = m_strdup(resp);
652 if ((key = pgp_getkeybystr (resp, abilities, keyring)))
660 /* generate a public key attachment */
662 BODY *pgp_make_key_attachment (char *tempf)
665 char buff[LONG_STRING];
666 char tempfb[_POSIX_PATH_MAX], tmp[STRING];
673 unset_option (OPTPGPCHECKTRUST);
676 pgp_ask_for_key (_("Please enter the key ID: "), NULL, 0, PGP_PUBRING);
681 snprintf (tmp, sizeof (tmp), "0x%s", pgp_keyid (pgp_principal_key (key)));
685 mutt_mktemp (tempfb);
689 if ((tempfp = safe_fopen (tempf, tempf == tempfb ? "w" : "a")) == NULL) {
690 mutt_perror (_("Can't create temporary file"));
695 if ((devnull = fopen ("/dev/null", "w")) == NULL) { /* __FOPEN_CHECKED__ */
696 mutt_perror (_("Can't open /dev/null"));
704 mutt_message _("Invoking pgp...");
708 pgp_invoke_export (NULL, NULL, NULL, -1,
709 fileno (tempfp), fileno (devnull), tmp)) == -1) {
710 mutt_perror (_("Can't create filter"));
718 mutt_wait_filter (thepid);
724 att->filename = m_strdup(tempf);
727 att->type = TYPEAPPLICATION;
728 att->subtype = m_strdup("pgp-keys");
729 snprintf (buff, sizeof (buff), _("PGP Key %s."), tmp);
730 att->description = m_strdup(buff);
731 mutt_update_encoding (att);
734 att->length = sb.st_size;
739 static string_list_t *pgp_add_string_to_hints (string_list_t * hints, const char *str)
744 if ((scratch = m_strdup(str)) == NULL)
747 for (t = strtok (scratch, " ,.:\"()<>\n"); t;
748 t = strtok (NULL, " ,.:\"()<>\n")) {
750 hints = mutt_add_list (hints, t);
757 static pgp_key_t *pgp_get_lastp (pgp_key_t p)
759 for (; p; p = p->next)
766 pgp_key_t pgp_getkeybyaddr (address_t * a, short abilities, pgp_ring_t keyring)
769 string_list_t *hints = NULL;
774 int this_key_has_strong;
775 int this_key_has_weak;
776 int this_key_has_invalid;
779 pgp_key_t keys, k, kn;
780 pgp_key_t the_valid_key = NULL;
781 pgp_key_t matches = NULL;
782 pgp_key_t *last = &matches;
786 hints = pgp_add_string_to_hints (hints, a->mailbox);
787 if (a && a->personal)
788 hints = pgp_add_string_to_hints (hints, a->personal);
790 mutt_message (_("Looking for keys matching \"%s\"..."), a->mailbox);
791 keys = pgp_get_candidates (keyring, hints);
793 string_list_wipe(&hints);
798 for (k = keys; k; k = kn) {
801 if (abilities && !(k->flags & abilities)) {
805 this_key_has_weak = 0; /* weak but valid match */
806 this_key_has_invalid = 0; /* invalid match */
807 this_key_has_strong = 0; /* strong and valid match */
808 match = 0; /* any match */
810 for (q = k->address; q; q = q->next) {
811 r = rfc822_parse_adrlist (NULL, q->addr);
813 for (p = r; p; p = p->next) {
814 int validity = pgp_id_matches_addr (a, p, q);
816 if (validity & PGP_KV_MATCH) /* something matches */
819 /* is this key a strong candidate? */
820 if ((validity & PGP_KV_VALID) && (validity & PGP_KV_STRONGID)
821 && (validity & PGP_KV_ADDR)) {
822 if (the_valid_key && the_valid_key != k)
825 this_key_has_strong = 1;
827 else if ((validity & PGP_KV_MATCH) && !(validity & PGP_KV_VALID))
828 this_key_has_invalid = 1;
829 else if ((validity & PGP_KV_MATCH)
830 && (!(validity & PGP_KV_STRONGID)
831 || !(validity & PGP_KV_ADDR)))
832 this_key_has_weak = 1;
835 address_list_wipe(&r);
838 if (match && !this_key_has_strong && this_key_has_invalid)
840 if (match && !this_key_has_strong && this_key_has_weak)
844 *last = pgp_principal_key (k);
845 kn = pgp_remove_key (&keys, *last);
846 last = pgp_get_lastp (k);
850 pgp_free_key (&keys);
853 if (the_valid_key && !multi /* && !weak
854 && !(invalid && option (OPTPGPSHOWUNUSABLE)) */ ) {
856 * There was precisely one strong match on a valid ID.
858 * Proceed without asking the user.
860 pgp_remove_key (&matches, the_valid_key);
861 pgp_free_key (&matches);
866 * Else: Ask the user.
868 if ((k = pgp_select_key (matches, a, NULL)))
869 pgp_remove_key (&matches, k);
870 pgp_free_key (&matches);
879 pgp_key_t pgp_getkeybystr (const char *p, short abilities, pgp_ring_t keyring)
881 string_list_t *hints = NULL;
883 pgp_key_t matches = NULL;
884 pgp_key_t *last = &matches;
889 mutt_message (_("Looking for keys matching \"%s\"..."), p);
891 hints = pgp_add_string_to_hints (hints, p);
892 keys = pgp_get_candidates (keyring, hints);
893 string_list_wipe(&hints);
899 for (k = keys; k; k = kn) {
901 if (abilities && !(k->flags & abilities))
906 for (a = k->address; a; a = a->next) {
907 if (!*p || m_strcasecmp(p, pgp_keyid (k)) == 0
908 || (!m_strncasecmp(p, "0x", 2)
909 && !m_strcasecmp(p + 2, pgp_keyid (k)))
910 || (option (OPTPGPLONGIDS) && !m_strncasecmp(p, "0x", 2)
911 && !m_strcasecmp(p + 2, k->keyid + 8))
912 || m_stristr(a->addr, p)) {
919 *last = pgp_principal_key (k);
920 kn = pgp_remove_key (&keys, *last);
921 last = pgp_get_lastp (k);
925 pgp_free_key (&keys);
928 if ((k = pgp_select_key (matches, NULL, p)))
929 pgp_remove_key (&matches, k);
931 pgp_free_key (&matches);