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/macros.h>
22 #include "recvattach.h"
23 #include "mutt_curses.h"
24 #include "mutt_menu.h"
30 #include "lib/debug.h"
41 #ifdef CRYPT_BACKEND_CLASSIC_PGP
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 {
117 static const char *pgp_entry_fmt (char *dest,
122 const char *ifstring,
123 const char *elsestring,
124 unsigned long data, format_flag flags)
131 int optional = (flags & M_FORMAT_OPTIONAL);
133 entry = (pgp_entry_t *) data;
136 pkey = pgp_principal_key (key);
138 if (isupper ((unsigned char) op))
141 kflags = key->flags | (pkey->flags & KEYFLAG_RESTRICTIONS)
144 switch (ascii_tolower (op)) {
149 char buf2[SHORT_STRING], *p;
165 while (len > 0 && *cp != ']') {
174 break; /* not enough space */
184 if (do_locales && Locale)
185 setlocale (LC_TIME, Locale);
187 tm = localtime (&key->gen_time);
189 strftime (buf2, sizeof (buf2), dest, tm);
192 setlocale (LC_TIME, "C");
194 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
195 snprintf (dest, destlen, fmt, buf2);
202 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
203 snprintf (dest, destlen, fmt, entry->num);
208 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
209 snprintf (dest, destlen, fmt, _pgp_keyid (key));
214 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
215 snprintf (dest, destlen, fmt, uid->addr);
220 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
221 snprintf (dest, destlen, fmt, key->algorithm);
226 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
227 snprintf (dest, destlen, fmt, key->keylen);
232 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
233 snprintf (dest, destlen, fmt, pgp_flags (kflags));
235 else if (!(kflags & (KEYFLAG_RESTRICTIONS)))
240 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
241 snprintf (dest, destlen, fmt, pgp_key_abilities (kflags));
243 else if (!(kflags & (KEYFLAG_ABILITIES)))
248 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
249 snprintf (dest, destlen, fmt, trust_flags[uid->trust & 0x03]);
251 else if (!(uid->trust & 0x03))
252 /* undefined trust */
260 mutt_FormatString (dest, destlen, ifstring, mutt_attach_fmt, data, 0);
261 else if (flags & M_FORMAT_OPTIONAL)
262 mutt_FormatString (dest, destlen, elsestring, mutt_attach_fmt, data, 0);
266 static void pgp_entry (char *s, size_t l, MUTTMENU * menu, int num)
268 pgp_uid_t **KeyTable = (pgp_uid_t **) menu->data;
271 entry.uid = KeyTable[num];
274 mutt_FormatString (s, l, NONULL (PgpEntryFormat), pgp_entry_fmt,
275 (unsigned long) &entry, M_FORMAT_ARROWCURSOR);
278 static int _pgp_compare_address (const void *a, const void *b)
282 pgp_uid_t **s = (pgp_uid_t **) a;
283 pgp_uid_t **t = (pgp_uid_t **) b;
285 if ((r = str_casecmp ((*s)->addr, (*t)->addr)))
288 return (str_casecmp (_pgp_keyid ((*s)->parent),
289 _pgp_keyid ((*t)->parent)) > 0);
292 static int pgp_compare_address (const void *a, const void *b)
294 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_address (a, b)
295 : _pgp_compare_address (a, b));
300 static int _pgp_compare_keyid (const void *a, const void *b)
304 pgp_uid_t **s = (pgp_uid_t **) a;
305 pgp_uid_t **t = (pgp_uid_t **) b;
307 if ((r = str_casecmp (_pgp_keyid ((*s)->parent),
308 _pgp_keyid ((*t)->parent))))
311 return (str_casecmp ((*s)->addr, (*t)->addr)) > 0;
314 static int pgp_compare_keyid (const void *a, const void *b)
316 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_keyid (a, b)
317 : _pgp_compare_keyid (a, b));
320 static int _pgp_compare_date (const void *a, const void *b)
323 pgp_uid_t **s = (pgp_uid_t **) a;
324 pgp_uid_t **t = (pgp_uid_t **) b;
326 if ((r = ((*s)->parent->gen_time - (*t)->parent->gen_time)))
328 return (str_casecmp ((*s)->addr, (*t)->addr)) > 0;
331 static int pgp_compare_date (const void *a, const void *b)
333 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_date (a, b)
334 : _pgp_compare_date (a, b));
337 static int _pgp_compare_trust (const void *a, const void *b)
341 pgp_uid_t **s = (pgp_uid_t **) a;
342 pgp_uid_t **t = (pgp_uid_t **) b;
344 if ((r = (((*s)->parent->flags & (KEYFLAG_RESTRICTIONS))
345 - ((*t)->parent->flags & (KEYFLAG_RESTRICTIONS)))))
347 if ((r = ((*s)->trust - (*t)->trust)))
349 if ((r = ((*s)->parent->keylen - (*t)->parent->keylen)))
351 if ((r = ((*s)->parent->gen_time - (*t)->parent->gen_time)))
353 if ((r = str_casecmp ((*s)->addr, (*t)->addr)))
355 return (str_casecmp (_pgp_keyid ((*s)->parent),
356 _pgp_keyid ((*t)->parent))) > 0;
359 static int pgp_compare_trust (const void *a, const void *b)
361 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_trust (a, b)
362 : _pgp_compare_trust (a, b));
365 static int pgp_key_is_valid (pgp_key_t k)
367 pgp_key_t pk = pgp_principal_key (k);
369 if (k->flags & KEYFLAG_CANTUSE)
371 if (pk->flags & KEYFLAG_CANTUSE)
377 static int pgp_id_is_strong (pgp_uid_t * uid)
379 if ((uid->trust & 3) < 3)
385 static int pgp_id_is_valid (pgp_uid_t * uid)
387 if (!pgp_key_is_valid (uid->parent))
389 if (uid->flags & KEYFLAG_CANTUSE)
395 #define PGP_KV_VALID 1
396 #define PGP_KV_ADDR 2
397 #define PGP_KV_STRING 4
398 #define PGP_KV_STRONGID 8
400 #define PGP_KV_MATCH (PGP_KV_ADDR|PGP_KV_STRING)
402 static int pgp_id_matches_addr (ADDRESS * addr, ADDRESS * u_addr,
407 if (pgp_id_is_valid (uid))
410 if (pgp_id_is_strong (uid))
411 rv |= PGP_KV_STRONGID;
413 if (addr->mailbox && u_addr->mailbox
414 && str_casecmp (addr->mailbox, u_addr->mailbox) == 0)
417 if (addr->personal && u_addr->personal
418 && str_casecmp (addr->personal, u_addr->personal) == 0)
424 static pgp_key_t pgp_select_key (pgp_key_t keys, ADDRESS * p, const char *s)
427 pgp_uid_t **KeyTable;
430 char helpstr[SHORT_STRING], buf[LONG_STRING], tmpbuf[STRING];
431 char cmd[LONG_STRING], tempfile[_POSIX_PATH_MAX];
436 int (*f) (const void *, const void *);
443 for (i = 0, kp = keys; kp; kp = kp->next) {
444 if (!option (OPTPGPSHOWUNUSABLE) && (kp->flags & KEYFLAG_CANTUSE)) {
449 for (a = kp->address; a; a = a->next) {
450 if (!option (OPTPGPSHOWUNUSABLE) && (a->flags & KEYFLAG_CANTUSE)) {
457 p_realloc(&KeyTable, keymax);
464 if (!i && unusable) {
465 mutt_error _("All matching keys are expired, revoked, or disabled.");
471 switch (PgpSortKeys & SORT_MASK) {
473 f = pgp_compare_date;
476 f = pgp_compare_keyid;
479 f = pgp_compare_address;
483 f = pgp_compare_trust;
486 qsort (KeyTable, i, sizeof (pgp_uid_t *), f);
489 mutt_make_help (buf, sizeof (buf), _("Exit "), MENU_PGP, OP_EXIT);
490 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
491 mutt_make_help (buf, sizeof (buf), _("Select "), MENU_PGP,
492 OP_GENERIC_SELECT_ENTRY);
493 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
494 mutt_make_help (buf, sizeof (buf), _("Check key "), MENU_PGP,
496 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
497 mutt_make_help (buf, sizeof (buf), _("Help"), MENU_PGP, OP_HELP);
498 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
500 menu = mutt_new_menu ();
502 menu->make_entry = pgp_entry;
503 menu->menu = MENU_PGP;
504 menu->help = helpstr;
505 menu->data = KeyTable;
508 snprintf (buf, sizeof (buf), _("PGP keys matching <%s>."), p->mailbox);
510 snprintf (buf, sizeof (buf), _("PGP keys matching \"%s\"."), s);
520 switch (mutt_menuLoop (menu)) {
524 mutt_mktemp (tempfile);
525 if ((devnull = fopen ("/dev/null", "w")) == NULL) { /* __FOPEN_CHECKED__ */
526 mutt_perror (_("Can't open /dev/null"));
530 if ((fp = safe_fopen (tempfile, "w")) == NULL) {
532 mutt_perror (_("Can't create temporary file"));
537 mutt_message _("Invoking PGP...");
539 snprintf (tmpbuf, sizeof (tmpbuf), "0x%s",
540 pgp_keyid (pgp_principal_key
541 (KeyTable[menu->current]->parent)));
543 if ((thepid = pgp_invoke_verify_key (NULL, NULL, NULL, -1,
544 fileno (fp), fileno (devnull),
546 mutt_perror (_("Can't create filter"));
553 mutt_wait_filter (thepid);
557 snprintf (cmd, sizeof (cmd), _("Key ID: 0x%s"),
558 pgp_keyid (pgp_principal_key
559 (KeyTable[menu->current]->parent)));
560 mutt_do_pager (cmd, tempfile, 0, NULL);
561 menu->redraw = REDRAW_FULL;
567 mutt_message ("%s", KeyTable[menu->current]->addr);
570 case OP_GENERIC_SELECT_ENTRY:
573 /* XXX make error reporting more verbose */
575 if (option (OPTPGPCHECKTRUST))
576 if (!pgp_key_is_valid (KeyTable[menu->current]->parent)) {
577 mutt_error _("This key can't be used: expired/disabled/revoked.");
582 if (option (OPTPGPCHECKTRUST) &&
583 (!pgp_id_is_valid (KeyTable[menu->current])
584 || !pgp_id_is_strong (KeyTable[menu->current]))) {
586 char buff[LONG_STRING];
588 if (KeyTable[menu->current]->flags & KEYFLAG_CANTUSE)
589 s = N_("ID is expired/disabled/revoked.");
591 switch (KeyTable[menu->current]->trust & 0x03) {
593 s = N_("ID has undefined validity.");
596 s = N_("ID is not valid.");
599 s = N_("ID is only marginally valid.");
603 snprintf (buff, sizeof (buff),
604 _("%s Do you really want to use the key?"), _(s));
606 if (mutt_yesorno (buff, M_NO) != M_YES) {
613 kp = pgp_principal_key (KeyTable[menu->current]->parent);
615 kp = KeyTable[menu->current]->parent;
628 mutt_menuDestroy (&menu);
631 set_option (OPTNEEDREDRAW);
636 pgp_key_t pgp_ask_for_key (char *tag, char *whatfor,
637 short abilities, pgp_ring_t keyring)
640 char resp[SHORT_STRING];
641 struct pgp_cache *l = NULL;
648 for (l = id_defaults; l; l = l->next)
649 if (!str_casecmp (whatfor, l->what)) {
650 strfcpy (resp, NONULL (l->dflt), sizeof (resp));
658 if (mutt_get_field (tag, resp, sizeof (resp), M_CLEAR) != 0)
663 str_replace (&l->dflt, resp);
665 l = p_new(struct pgp_cache, 1);
666 l->next = id_defaults;
668 l->what = m_strdup(whatfor);
669 l->dflt = m_strdup(resp);
673 if ((key = pgp_getkeybystr (resp, abilities, keyring)))
681 /* generate a public key attachment */
683 BODY *pgp_make_key_attachment (char *tempf)
686 char buff[LONG_STRING];
687 char tempfb[_POSIX_PATH_MAX], tmp[STRING];
694 unset_option (OPTPGPCHECKTRUST);
697 pgp_ask_for_key (_("Please enter the key ID: "), NULL, 0, PGP_PUBRING);
702 snprintf (tmp, sizeof (tmp), "0x%s", pgp_keyid (pgp_principal_key (key)));
706 mutt_mktemp (tempfb);
710 if ((tempfp = safe_fopen (tempf, tempf == tempfb ? "w" : "a")) == NULL) {
711 mutt_perror (_("Can't create temporary file"));
716 if ((devnull = fopen ("/dev/null", "w")) == NULL) { /* __FOPEN_CHECKED__ */
717 mutt_perror (_("Can't open /dev/null"));
725 mutt_message _("Invoking pgp...");
729 pgp_invoke_export (NULL, NULL, NULL, -1,
730 fileno (tempfp), fileno (devnull), tmp)) == -1) {
731 mutt_perror (_("Can't create filter"));
739 mutt_wait_filter (thepid);
744 att = mutt_new_body ();
745 att->filename = m_strdup(tempf);
748 att->type = TYPEAPPLICATION;
749 att->subtype = m_strdup("pgp-keys");
750 snprintf (buff, sizeof (buff), _("PGP Key %s."), tmp);
751 att->description = m_strdup(buff);
752 mutt_update_encoding (att);
755 att->length = sb.st_size;
760 static LIST *pgp_add_string_to_hints (LIST * hints, const char *str)
765 if ((scratch = m_strdup(str)) == NULL)
768 for (t = strtok (scratch, " ,.:\"()<>\n"); t;
769 t = strtok (NULL, " ,.:\"()<>\n")) {
771 hints = mutt_add_list (hints, t);
778 static pgp_key_t *pgp_get_lastp (pgp_key_t p)
780 for (; p; p = p->next)
787 pgp_key_t pgp_getkeybyaddr (ADDRESS * a, short abilities, pgp_ring_t keyring)
795 int this_key_has_strong;
796 int this_key_has_weak;
797 int this_key_has_invalid;
800 pgp_key_t keys, k, kn;
801 pgp_key_t the_valid_key = NULL;
802 pgp_key_t matches = NULL;
803 pgp_key_t *last = &matches;
807 hints = pgp_add_string_to_hints (hints, a->mailbox);
808 if (a && a->personal)
809 hints = pgp_add_string_to_hints (hints, a->personal);
811 mutt_message (_("Looking for keys matching \"%s\"..."), a->mailbox);
812 keys = pgp_get_candidates (keyring, hints);
814 mutt_free_list (&hints);
819 debug_print (5, ("looking for %s <%s>\n", a->personal, a->mailbox));
821 for (k = keys; k; k = kn) {
824 debug_print (5, (" looking at key: %s\n", pgp_keyid (k)));
826 if (abilities && !(k->flags & abilities)) {
827 debug_print (5, (" insufficient abilities: Has %x, want %x\n",
828 k->flags, abilities));
832 this_key_has_weak = 0; /* weak but valid match */
833 this_key_has_invalid = 0; /* invalid match */
834 this_key_has_strong = 0; /* strong and valid match */
835 match = 0; /* any match */
837 for (q = k->address; q; q = q->next) {
838 r = rfc822_parse_adrlist (NULL, q->addr);
840 for (p = r; p; p = p->next) {
841 int validity = pgp_id_matches_addr (a, p, q);
843 if (validity & PGP_KV_MATCH) /* something matches */
846 /* is this key a strong candidate? */
847 if ((validity & PGP_KV_VALID) && (validity & PGP_KV_STRONGID)
848 && (validity & PGP_KV_ADDR)) {
849 if (the_valid_key && the_valid_key != k)
852 this_key_has_strong = 1;
854 else if ((validity & PGP_KV_MATCH) && !(validity & PGP_KV_VALID))
855 this_key_has_invalid = 1;
856 else if ((validity & PGP_KV_MATCH)
857 && (!(validity & PGP_KV_STRONGID)
858 || !(validity & PGP_KV_ADDR)))
859 this_key_has_weak = 1;
862 rfc822_free_address (&r);
865 if (match && !this_key_has_strong && this_key_has_invalid)
867 if (match && !this_key_has_strong && this_key_has_weak)
871 *last = pgp_principal_key (k);
872 kn = pgp_remove_key (&keys, *last);
873 last = pgp_get_lastp (k);
877 pgp_free_key (&keys);
880 if (the_valid_key && !multi /* && !weak
881 && !(invalid && option (OPTPGPSHOWUNUSABLE)) */ ) {
883 * There was precisely one strong match on a valid ID.
885 * Proceed without asking the user.
887 pgp_remove_key (&matches, the_valid_key);
888 pgp_free_key (&matches);
893 * Else: Ask the user.
895 if ((k = pgp_select_key (matches, a, NULL)))
896 pgp_remove_key (&matches, k);
897 pgp_free_key (&matches);
906 pgp_key_t pgp_getkeybystr (char *p, short abilities, pgp_ring_t keyring)
910 pgp_key_t matches = NULL;
911 pgp_key_t *last = &matches;
916 mutt_message (_("Looking for keys matching \"%s\"..."), p);
918 hints = pgp_add_string_to_hints (hints, p);
919 keys = pgp_get_candidates (keyring, hints);
920 mutt_free_list (&hints);
926 for (k = keys; k; k = kn) {
928 if (abilities && !(k->flags & abilities))
933 for (a = k->address; a; a = a->next) {
934 debug_print (5, ("matching \"%s\" against key %s, \"%s\":\n", p, pgp_keyid (k), a->addr));
935 if (!*p || str_casecmp (p, pgp_keyid (k)) == 0
936 || (!str_ncasecmp (p, "0x", 2)
937 && !str_casecmp (p + 2, pgp_keyid (k)))
938 || (option (OPTPGPLONGIDS) && !str_ncasecmp (p, "0x", 2)
939 && !str_casecmp (p + 2, k->keyid + 8))
940 || str_isstr (a->addr, p)) {
941 debug_print (5, ("match.\n"));
948 *last = pgp_principal_key (k);
949 kn = pgp_remove_key (&keys, *last);
950 last = pgp_get_lastp (k);
954 pgp_free_key (&keys);
957 if ((k = pgp_select_key (matches, NULL, p)))
958 pgp_remove_key (&matches, k);
960 pgp_free_key (&matches);
969 #endif /* CRYPT_BACKEND_CLASSIC_PGP */