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>
25 #include "recvattach.h"
26 #include "mutt_curses.h"
27 #include "mutt_menu.h"
32 #include "lib/debug.h"
43 #ifdef CRYPT_BACKEND_CLASSIC_PGP
48 struct pgp_cache *next;
51 static struct pgp_cache *id_defaults = NULL;
53 static char trust_flags[] = "?- +";
55 static char *pgp_key_abilities (int flags)
59 if (!(flags & KEYFLAG_CANENCRYPT))
61 else if (flags & KEYFLAG_PREFER_SIGNING)
66 if (!(flags & KEYFLAG_CANSIGN))
68 else if (flags & KEYFLAG_PREFER_ENCRYPTION)
78 static char pgp_flags (int flags)
80 if (flags & KEYFLAG_REVOKED)
82 else if (flags & KEYFLAG_EXPIRED)
84 else if (flags & KEYFLAG_DISABLED)
86 else if (flags & KEYFLAG_CRITICAL)
92 static pgp_key_t pgp_principal_key (pgp_key_t key)
94 if (key->flags & KEYFLAG_SUBKEY && key->parent)
101 * Format an entry on the PGP key selection menu.
104 * %k key id %K key id of the principal key
106 * %a algorithm %A algorithm of the princ. key
107 * %l length %L length of the princ. key
108 * %f flags %F flags of the princ. key
109 * %c capabilities %C capabilities of the princ. key
110 * %t trust/validity of the key-uid association
111 * %[...] date of key using strftime(3)
114 typedef struct pgp_entry {
119 static const char *pgp_entry_fmt (char *dest,
124 const char *ifstring,
125 const char *elsestring,
126 unsigned long data, format_flag flags)
133 int optional = (flags & M_FORMAT_OPTIONAL);
135 entry = (pgp_entry_t *) data;
138 pkey = pgp_principal_key (key);
140 if (isupper ((unsigned char) op))
143 kflags = key->flags | (pkey->flags & KEYFLAG_RESTRICTIONS)
146 switch (ascii_tolower (op)) {
151 char buf2[SHORT_STRING], *p;
167 while (len > 0 && *cp != ']') {
176 break; /* not enough space */
186 if (do_locales && Locale)
187 setlocale (LC_TIME, Locale);
189 tm = localtime (&key->gen_time);
191 strftime (buf2, sizeof (buf2), dest, tm);
194 setlocale (LC_TIME, "C");
196 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
197 snprintf (dest, destlen, fmt, buf2);
204 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
205 snprintf (dest, destlen, fmt, entry->num);
210 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
211 snprintf (dest, destlen, fmt, _pgp_keyid (key));
216 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
217 snprintf (dest, destlen, fmt, uid->addr);
222 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
223 snprintf (dest, destlen, fmt, key->algorithm);
228 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
229 snprintf (dest, destlen, fmt, key->keylen);
234 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
235 snprintf (dest, destlen, fmt, pgp_flags (kflags));
237 else if (!(kflags & (KEYFLAG_RESTRICTIONS)))
242 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
243 snprintf (dest, destlen, fmt, pgp_key_abilities (kflags));
245 else if (!(kflags & (KEYFLAG_ABILITIES)))
250 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
251 snprintf (dest, destlen, fmt, trust_flags[uid->trust & 0x03]);
253 else if (!(uid->trust & 0x03))
254 /* undefined trust */
262 mutt_FormatString (dest, destlen, ifstring, mutt_attach_fmt, data, 0);
263 else if (flags & M_FORMAT_OPTIONAL)
264 mutt_FormatString (dest, destlen, elsestring, mutt_attach_fmt, data, 0);
268 static void pgp_entry (char *s, size_t l, MUTTMENU * menu, int num)
270 pgp_uid_t **KeyTable = (pgp_uid_t **) menu->data;
273 entry.uid = KeyTable[num];
276 mutt_FormatString (s, l, NONULL (PgpEntryFormat), pgp_entry_fmt,
277 (unsigned long) &entry, M_FORMAT_ARROWCURSOR);
280 static int _pgp_compare_address (const void *a, const void *b)
284 pgp_uid_t **s = (pgp_uid_t **) a;
285 pgp_uid_t **t = (pgp_uid_t **) b;
287 if ((r = m_strcasecmp((*s)->addr, (*t)->addr)))
290 return (m_strcasecmp(_pgp_keyid ((*s)->parent),
291 _pgp_keyid ((*t)->parent)) > 0);
294 static int pgp_compare_address (const void *a, const void *b)
296 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_address (a, b)
297 : _pgp_compare_address (a, b));
302 static int _pgp_compare_keyid (const void *a, const void *b)
306 pgp_uid_t **s = (pgp_uid_t **) a;
307 pgp_uid_t **t = (pgp_uid_t **) b;
309 if ((r = m_strcasecmp(_pgp_keyid ((*s)->parent),
310 _pgp_keyid ((*t)->parent))))
313 return (m_strcasecmp((*s)->addr, (*t)->addr)) > 0;
316 static int pgp_compare_keyid (const void *a, const void *b)
318 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_keyid (a, b)
319 : _pgp_compare_keyid (a, b));
322 static int _pgp_compare_date (const void *a, const void *b)
325 pgp_uid_t **s = (pgp_uid_t **) a;
326 pgp_uid_t **t = (pgp_uid_t **) b;
328 if ((r = ((*s)->parent->gen_time - (*t)->parent->gen_time)))
330 return (m_strcasecmp((*s)->addr, (*t)->addr)) > 0;
333 static int pgp_compare_date (const void *a, const void *b)
335 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_date (a, b)
336 : _pgp_compare_date (a, b));
339 static int _pgp_compare_trust (const void *a, const void *b)
343 pgp_uid_t **s = (pgp_uid_t **) a;
344 pgp_uid_t **t = (pgp_uid_t **) b;
346 if ((r = (((*s)->parent->flags & (KEYFLAG_RESTRICTIONS))
347 - ((*t)->parent->flags & (KEYFLAG_RESTRICTIONS)))))
349 if ((r = ((*s)->trust - (*t)->trust)))
351 if ((r = ((*s)->parent->keylen - (*t)->parent->keylen)))
353 if ((r = ((*s)->parent->gen_time - (*t)->parent->gen_time)))
355 if ((r = m_strcasecmp((*s)->addr, (*t)->addr)))
357 return (m_strcasecmp(_pgp_keyid ((*s)->parent),
358 _pgp_keyid ((*t)->parent))) > 0;
361 static int pgp_compare_trust (const void *a, const void *b)
363 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_trust (a, b)
364 : _pgp_compare_trust (a, b));
367 static int pgp_key_is_valid (pgp_key_t k)
369 pgp_key_t pk = pgp_principal_key (k);
371 if (k->flags & KEYFLAG_CANTUSE)
373 if (pk->flags & KEYFLAG_CANTUSE)
379 static int pgp_id_is_strong (pgp_uid_t * uid)
381 if ((uid->trust & 3) < 3)
387 static int pgp_id_is_valid (pgp_uid_t * uid)
389 if (!pgp_key_is_valid (uid->parent))
391 if (uid->flags & KEYFLAG_CANTUSE)
397 #define PGP_KV_VALID 1
398 #define PGP_KV_ADDR 2
399 #define PGP_KV_STRING 4
400 #define PGP_KV_STRONGID 8
402 #define PGP_KV_MATCH (PGP_KV_ADDR|PGP_KV_STRING)
404 static int pgp_id_matches_addr (address_t * addr, address_t * u_addr,
409 if (pgp_id_is_valid (uid))
412 if (pgp_id_is_strong (uid))
413 rv |= PGP_KV_STRONGID;
415 if (addr->mailbox && u_addr->mailbox
416 && m_strcasecmp(addr->mailbox, u_addr->mailbox) == 0)
419 if (addr->personal && u_addr->personal
420 && m_strcasecmp(addr->personal, u_addr->personal) == 0)
426 static pgp_key_t pgp_select_key (pgp_key_t keys, address_t * p, const char *s)
429 pgp_uid_t **KeyTable;
432 char helpstr[SHORT_STRING], buf[LONG_STRING], tmpbuf[STRING];
433 char cmd[LONG_STRING], tempfile[_POSIX_PATH_MAX];
438 int (*f) (const void *, const void *);
445 for (i = 0, kp = keys; kp; kp = kp->next) {
446 if (!option (OPTPGPSHOWUNUSABLE) && (kp->flags & KEYFLAG_CANTUSE)) {
451 for (a = kp->address; a; a = a->next) {
452 if (!option (OPTPGPSHOWUNUSABLE) && (a->flags & KEYFLAG_CANTUSE)) {
459 p_realloc(&KeyTable, keymax);
466 if (!i && unusable) {
467 mutt_error _("All matching keys are expired, revoked, or disabled.");
473 switch (PgpSortKeys & SORT_MASK) {
475 f = pgp_compare_date;
478 f = pgp_compare_keyid;
481 f = pgp_compare_address;
485 f = pgp_compare_trust;
488 qsort (KeyTable, i, sizeof (pgp_uid_t *), f);
491 mutt_make_help (buf, sizeof (buf), _("Exit "), MENU_PGP, OP_EXIT);
492 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
493 mutt_make_help (buf, sizeof (buf), _("Select "), MENU_PGP,
494 OP_GENERIC_SELECT_ENTRY);
495 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
496 mutt_make_help (buf, sizeof (buf), _("Check key "), MENU_PGP,
498 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
499 mutt_make_help (buf, sizeof (buf), _("Help"), MENU_PGP, OP_HELP);
500 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
502 menu = mutt_new_menu ();
504 menu->make_entry = pgp_entry;
505 menu->menu = MENU_PGP;
506 menu->help = helpstr;
507 menu->data = KeyTable;
510 snprintf (buf, sizeof (buf), _("PGP keys matching <%s>."), p->mailbox);
512 snprintf (buf, sizeof (buf), _("PGP keys matching \"%s\"."), s);
522 switch (mutt_menuLoop (menu)) {
526 mutt_mktemp (tempfile);
527 if ((devnull = fopen ("/dev/null", "w")) == NULL) { /* __FOPEN_CHECKED__ */
528 mutt_perror (_("Can't open /dev/null"));
532 if ((fp = safe_fopen (tempfile, "w")) == NULL) {
534 mutt_perror (_("Can't create temporary file"));
539 mutt_message _("Invoking PGP...");
541 snprintf (tmpbuf, sizeof (tmpbuf), "0x%s",
542 pgp_keyid (pgp_principal_key
543 (KeyTable[menu->current]->parent)));
545 if ((thepid = pgp_invoke_verify_key (NULL, NULL, NULL, -1,
546 fileno (fp), fileno (devnull),
548 mutt_perror (_("Can't create filter"));
555 mutt_wait_filter (thepid);
559 snprintf (cmd, sizeof (cmd), _("Key ID: 0x%s"),
560 pgp_keyid (pgp_principal_key
561 (KeyTable[menu->current]->parent)));
562 mutt_do_pager (cmd, tempfile, 0, NULL);
563 menu->redraw = REDRAW_FULL;
569 mutt_message ("%s", KeyTable[menu->current]->addr);
572 case OP_GENERIC_SELECT_ENTRY:
575 /* XXX make error reporting more verbose */
577 if (option (OPTPGPCHECKTRUST))
578 if (!pgp_key_is_valid (KeyTable[menu->current]->parent)) {
579 mutt_error _("This key can't be used: expired/disabled/revoked.");
584 if (option (OPTPGPCHECKTRUST) &&
585 (!pgp_id_is_valid (KeyTable[menu->current])
586 || !pgp_id_is_strong (KeyTable[menu->current]))) {
588 char buff[LONG_STRING];
590 if (KeyTable[menu->current]->flags & KEYFLAG_CANTUSE)
591 s = N_("ID is expired/disabled/revoked.");
593 switch (KeyTable[menu->current]->trust & 0x03) {
595 s = N_("ID has undefined validity.");
598 s = N_("ID is not valid.");
601 s = N_("ID is only marginally valid.");
605 snprintf (buff, sizeof (buff),
606 _("%s Do you really want to use the key?"), _(s));
608 if (mutt_yesorno (buff, M_NO) != M_YES) {
615 kp = pgp_principal_key (KeyTable[menu->current]->parent);
617 kp = KeyTable[menu->current]->parent;
630 mutt_menuDestroy (&menu);
633 set_option (OPTNEEDREDRAW);
638 pgp_key_t pgp_ask_for_key (char *tag, char *whatfor,
639 short abilities, pgp_ring_t keyring)
642 char resp[SHORT_STRING];
643 struct pgp_cache *l = NULL;
650 for (l = id_defaults; l; l = l->next)
651 if (!m_strcasecmp(whatfor, l->what)) {
652 m_strcpy(resp, sizeof(resp), NONULL(l->dflt));
660 if (mutt_get_field (tag, resp, sizeof (resp), M_CLEAR) != 0)
665 m_strreplace(&l->dflt, resp);
667 l = p_new(struct pgp_cache, 1);
668 l->next = id_defaults;
670 l->what = m_strdup(whatfor);
671 l->dflt = m_strdup(resp);
675 if ((key = pgp_getkeybystr (resp, abilities, keyring)))
683 /* generate a public key attachment */
685 BODY *pgp_make_key_attachment (char *tempf)
688 char buff[LONG_STRING];
689 char tempfb[_POSIX_PATH_MAX], tmp[STRING];
696 unset_option (OPTPGPCHECKTRUST);
699 pgp_ask_for_key (_("Please enter the key ID: "), NULL, 0, PGP_PUBRING);
704 snprintf (tmp, sizeof (tmp), "0x%s", pgp_keyid (pgp_principal_key (key)));
708 mutt_mktemp (tempfb);
712 if ((tempfp = safe_fopen (tempf, tempf == tempfb ? "w" : "a")) == NULL) {
713 mutt_perror (_("Can't create temporary file"));
718 if ((devnull = fopen ("/dev/null", "w")) == NULL) { /* __FOPEN_CHECKED__ */
719 mutt_perror (_("Can't open /dev/null"));
727 mutt_message _("Invoking pgp...");
731 pgp_invoke_export (NULL, NULL, NULL, -1,
732 fileno (tempfp), fileno (devnull), tmp)) == -1) {
733 mutt_perror (_("Can't create filter"));
741 mutt_wait_filter (thepid);
746 att = mutt_new_body ();
747 att->filename = m_strdup(tempf);
750 att->type = TYPEAPPLICATION;
751 att->subtype = m_strdup("pgp-keys");
752 snprintf (buff, sizeof (buff), _("PGP Key %s."), tmp);
753 att->description = m_strdup(buff);
754 mutt_update_encoding (att);
757 att->length = sb.st_size;
762 static LIST *pgp_add_string_to_hints (LIST * hints, const char *str)
767 if ((scratch = m_strdup(str)) == NULL)
770 for (t = strtok (scratch, " ,.:\"()<>\n"); t;
771 t = strtok (NULL, " ,.:\"()<>\n")) {
773 hints = mutt_add_list (hints, t);
780 static pgp_key_t *pgp_get_lastp (pgp_key_t p)
782 for (; p; p = p->next)
789 pgp_key_t pgp_getkeybyaddr (address_t * a, short abilities, pgp_ring_t keyring)
797 int this_key_has_strong;
798 int this_key_has_weak;
799 int this_key_has_invalid;
802 pgp_key_t keys, k, kn;
803 pgp_key_t the_valid_key = NULL;
804 pgp_key_t matches = NULL;
805 pgp_key_t *last = &matches;
809 hints = pgp_add_string_to_hints (hints, a->mailbox);
810 if (a && a->personal)
811 hints = pgp_add_string_to_hints (hints, a->personal);
813 mutt_message (_("Looking for keys matching \"%s\"..."), a->mailbox);
814 keys = pgp_get_candidates (keyring, hints);
816 mutt_free_list (&hints);
821 debug_print (5, ("looking for %s <%s>\n", a->personal, a->mailbox));
823 for (k = keys; k; k = kn) {
826 debug_print (5, (" looking at key: %s\n", pgp_keyid (k)));
828 if (abilities && !(k->flags & abilities)) {
829 debug_print (5, (" insufficient abilities: Has %x, want %x\n",
830 k->flags, abilities));
834 this_key_has_weak = 0; /* weak but valid match */
835 this_key_has_invalid = 0; /* invalid match */
836 this_key_has_strong = 0; /* strong and valid match */
837 match = 0; /* any match */
839 for (q = k->address; q; q = q->next) {
840 r = rfc822_parse_adrlist (NULL, q->addr);
842 for (p = r; p; p = p->next) {
843 int validity = pgp_id_matches_addr (a, p, q);
845 if (validity & PGP_KV_MATCH) /* something matches */
848 /* is this key a strong candidate? */
849 if ((validity & PGP_KV_VALID) && (validity & PGP_KV_STRONGID)
850 && (validity & PGP_KV_ADDR)) {
851 if (the_valid_key && the_valid_key != k)
854 this_key_has_strong = 1;
856 else if ((validity & PGP_KV_MATCH) && !(validity & PGP_KV_VALID))
857 this_key_has_invalid = 1;
858 else if ((validity & PGP_KV_MATCH)
859 && (!(validity & PGP_KV_STRONGID)
860 || !(validity & PGP_KV_ADDR)))
861 this_key_has_weak = 1;
867 if (match && !this_key_has_strong && this_key_has_invalid)
869 if (match && !this_key_has_strong && this_key_has_weak)
873 *last = pgp_principal_key (k);
874 kn = pgp_remove_key (&keys, *last);
875 last = pgp_get_lastp (k);
879 pgp_free_key (&keys);
882 if (the_valid_key && !multi /* && !weak
883 && !(invalid && option (OPTPGPSHOWUNUSABLE)) */ ) {
885 * There was precisely one strong match on a valid ID.
887 * Proceed without asking the user.
889 pgp_remove_key (&matches, the_valid_key);
890 pgp_free_key (&matches);
895 * Else: Ask the user.
897 if ((k = pgp_select_key (matches, a, NULL)))
898 pgp_remove_key (&matches, k);
899 pgp_free_key (&matches);
908 pgp_key_t pgp_getkeybystr (char *p, short abilities, pgp_ring_t keyring)
912 pgp_key_t matches = NULL;
913 pgp_key_t *last = &matches;
918 mutt_message (_("Looking for keys matching \"%s\"..."), p);
920 hints = pgp_add_string_to_hints (hints, p);
921 keys = pgp_get_candidates (keyring, hints);
922 mutt_free_list (&hints);
928 for (k = keys; k; k = kn) {
930 if (abilities && !(k->flags & abilities))
935 for (a = k->address; a; a = a->next) {
936 debug_print (5, ("matching \"%s\" against key %s, \"%s\":\n", p, pgp_keyid (k), a->addr));
937 if (!*p || m_strcasecmp(p, pgp_keyid (k)) == 0
938 || (!m_strncasecmp(p, "0x", 2)
939 && !m_strcasecmp(p + 2, pgp_keyid (k)))
940 || (option (OPTPGPLONGIDS) && !m_strncasecmp(p, "0x", 2)
941 && !m_strcasecmp(p + 2, k->keyid + 8))
942 || m_stristr(a->addr, p)) {
943 debug_print (5, ("match.\n"));
950 *last = pgp_principal_key (k);
951 kn = pgp_remove_key (&keys, *last);
952 last = pgp_get_lastp (k);
956 pgp_free_key (&keys);
959 if ((k = pgp_select_key (matches, NULL, p)))
960 pgp_remove_key (&matches, k);
962 pgp_free_key (&matches);
971 #endif /* CRYPT_BACKEND_CLASSIC_PGP */