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.
16 #include "mutt_curses.h"
17 #include "mutt_menu.h"
36 #ifdef CRYPT_BACKEND_CLASSIC_PGP
41 struct pgp_cache *next;
44 static struct pgp_cache *id_defaults = NULL;
46 static char trust_flags[] = "?- +";
48 static char *pgp_key_abilities (int flags)
52 if (!(flags & KEYFLAG_CANENCRYPT))
54 else if (flags & KEYFLAG_PREFER_SIGNING)
59 if (!(flags & KEYFLAG_CANSIGN))
61 else if (flags & KEYFLAG_PREFER_ENCRYPTION)
71 static char pgp_flags (int flags)
73 if (flags & KEYFLAG_REVOKED)
75 else if (flags & KEYFLAG_EXPIRED)
77 else if (flags & KEYFLAG_DISABLED)
79 else if (flags & KEYFLAG_CRITICAL)
85 static pgp_key_t pgp_principal_key (pgp_key_t key)
87 if (key->flags & KEYFLAG_SUBKEY && key->parent)
94 * Format an entry on the PGP key selection menu.
97 * %k key id %K key id of the principal key
99 * %a algorithm %A algorithm of the princ. key
100 * %l length %L length of the princ. key
101 * %f flags %F flags of the princ. key
102 * %c capabilities %C capabilities of the princ. key
103 * %t trust/validity of the key-uid association
104 * %[...] date of key using strftime(3)
107 typedef struct pgp_entry {
112 static const char *pgp_entry_fmt (char *dest,
117 const char *ifstring,
118 const char *elsestring,
119 unsigned long data, format_flag flags)
126 int optional = (flags & M_FORMAT_OPTIONAL);
128 entry = (pgp_entry_t *) data;
131 pkey = pgp_principal_key (key);
133 if (isupper ((unsigned char) op))
136 kflags = key->flags | (pkey->flags & KEYFLAG_RESTRICTIONS)
139 switch (ascii_tolower (op)) {
144 char buf2[SHORT_STRING], *p;
160 while (len > 0 && *cp != ']') {
169 break; /* not enough space */
179 if (do_locales && Locale)
180 setlocale (LC_TIME, Locale);
182 tm = localtime (&key->gen_time);
184 strftime (buf2, sizeof (buf2), dest, tm);
187 setlocale (LC_TIME, "C");
189 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
190 snprintf (dest, destlen, fmt, buf2);
197 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
198 snprintf (dest, destlen, fmt, entry->num);
203 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
204 snprintf (dest, destlen, fmt, _pgp_keyid (key));
209 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
210 snprintf (dest, destlen, fmt, uid->addr);
215 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
216 snprintf (dest, destlen, fmt, key->algorithm);
221 snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
222 snprintf (dest, destlen, fmt, key->keylen);
227 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
228 snprintf (dest, destlen, fmt, pgp_flags (kflags));
230 else if (!(kflags & (KEYFLAG_RESTRICTIONS)))
235 snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
236 snprintf (dest, destlen, fmt, pgp_key_abilities (kflags));
238 else if (!(kflags & (KEYFLAG_ABILITIES)))
243 snprintf (fmt, sizeof (fmt), "%%%sc", prefix);
244 snprintf (dest, destlen, fmt, trust_flags[uid->trust & 0x03]);
246 else if (!(uid->trust & 0x03))
247 /* undefined trust */
255 mutt_FormatString (dest, destlen, ifstring, mutt_attach_fmt, data, 0);
256 else if (flags & M_FORMAT_OPTIONAL)
257 mutt_FormatString (dest, destlen, elsestring, mutt_attach_fmt, data, 0);
261 static void pgp_entry (char *s, size_t l, MUTTMENU * menu, int num)
263 pgp_uid_t **KeyTable = (pgp_uid_t **) menu->data;
266 entry.uid = KeyTable[num];
269 mutt_FormatString (s, l, NONULL (PgpEntryFormat), pgp_entry_fmt,
270 (unsigned long) &entry, M_FORMAT_ARROWCURSOR);
273 static int _pgp_compare_address (const void *a, const void *b)
277 pgp_uid_t **s = (pgp_uid_t **) a;
278 pgp_uid_t **t = (pgp_uid_t **) b;
280 if ((r = mutt_strcasecmp ((*s)->addr, (*t)->addr)))
283 return (mutt_strcasecmp (_pgp_keyid ((*s)->parent),
284 _pgp_keyid ((*t)->parent)) > 0);
287 static int pgp_compare_address (const void *a, const void *b)
289 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_address (a, b)
290 : _pgp_compare_address (a, b));
295 static int _pgp_compare_keyid (const void *a, const void *b)
299 pgp_uid_t **s = (pgp_uid_t **) a;
300 pgp_uid_t **t = (pgp_uid_t **) b;
302 if ((r = mutt_strcasecmp (_pgp_keyid ((*s)->parent),
303 _pgp_keyid ((*t)->parent))))
306 return (mutt_strcasecmp ((*s)->addr, (*t)->addr)) > 0;
309 static int pgp_compare_keyid (const void *a, const void *b)
311 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_keyid (a, b)
312 : _pgp_compare_keyid (a, b));
315 static int _pgp_compare_date (const void *a, const void *b)
318 pgp_uid_t **s = (pgp_uid_t **) a;
319 pgp_uid_t **t = (pgp_uid_t **) b;
321 if ((r = ((*s)->parent->gen_time - (*t)->parent->gen_time)))
323 return (mutt_strcasecmp ((*s)->addr, (*t)->addr)) > 0;
326 static int pgp_compare_date (const void *a, const void *b)
328 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_date (a, b)
329 : _pgp_compare_date (a, b));
332 static int _pgp_compare_trust (const void *a, const void *b)
336 pgp_uid_t **s = (pgp_uid_t **) a;
337 pgp_uid_t **t = (pgp_uid_t **) b;
339 if ((r = (((*s)->parent->flags & (KEYFLAG_RESTRICTIONS))
340 - ((*t)->parent->flags & (KEYFLAG_RESTRICTIONS)))))
342 if ((r = ((*s)->trust - (*t)->trust)))
344 if ((r = ((*s)->parent->keylen - (*t)->parent->keylen)))
346 if ((r = ((*s)->parent->gen_time - (*t)->parent->gen_time)))
348 if ((r = mutt_strcasecmp ((*s)->addr, (*t)->addr)))
350 return (mutt_strcasecmp (_pgp_keyid ((*s)->parent),
351 _pgp_keyid ((*t)->parent))) > 0;
354 static int pgp_compare_trust (const void *a, const void *b)
356 return ((PgpSortKeys & SORT_REVERSE) ? !_pgp_compare_trust (a, b)
357 : _pgp_compare_trust (a, b));
360 static int pgp_key_is_valid (pgp_key_t k)
362 pgp_key_t pk = pgp_principal_key (k);
364 if (k->flags & KEYFLAG_CANTUSE)
366 if (pk->flags & KEYFLAG_CANTUSE)
372 static int pgp_id_is_strong (pgp_uid_t * uid)
374 if ((uid->trust & 3) < 3)
380 static int pgp_id_is_valid (pgp_uid_t * uid)
382 if (!pgp_key_is_valid (uid->parent))
384 if (uid->flags & KEYFLAG_CANTUSE)
390 #define PGP_KV_VALID 1
391 #define PGP_KV_ADDR 2
392 #define PGP_KV_STRING 4
393 #define PGP_KV_STRONGID 8
395 #define PGP_KV_MATCH (PGP_KV_ADDR|PGP_KV_STRING)
397 static int pgp_id_matches_addr (ADDRESS * addr, ADDRESS * u_addr,
402 if (pgp_id_is_valid (uid))
405 if (pgp_id_is_strong (uid))
406 rv |= PGP_KV_STRONGID;
408 if (addr->mailbox && u_addr->mailbox
409 && mutt_strcasecmp (addr->mailbox, u_addr->mailbox) == 0)
412 if (addr->personal && u_addr->personal
413 && mutt_strcasecmp (addr->personal, u_addr->personal) == 0)
419 static pgp_key_t pgp_select_key (pgp_key_t keys, ADDRESS * p, const char *s)
422 pgp_uid_t **KeyTable;
425 char helpstr[SHORT_STRING], buf[LONG_STRING], tmpbuf[STRING];
426 char cmd[LONG_STRING], tempfile[_POSIX_PATH_MAX];
431 int (*f) (const void *, const void *);
438 for (i = 0, kp = keys; kp; kp = kp->next) {
439 if (!option (OPTPGPSHOWUNUSABLE) && (kp->flags & KEYFLAG_CANTUSE)) {
444 for (a = kp->address; a; a = a->next) {
445 if (!option (OPTPGPSHOWUNUSABLE) && (a->flags & KEYFLAG_CANTUSE)) {
452 safe_realloc (&KeyTable, sizeof (pgp_uid_t *) * keymax);
459 if (!i && unusable) {
460 mutt_error _("All matching keys are expired, revoked, or disabled.");
466 switch (PgpSortKeys & SORT_MASK) {
468 f = pgp_compare_date;
471 f = pgp_compare_keyid;
474 f = pgp_compare_address;
478 f = pgp_compare_trust;
481 qsort (KeyTable, i, sizeof (pgp_uid_t *), f);
484 mutt_make_help (buf, sizeof (buf), _("Exit "), MENU_PGP, OP_EXIT);
485 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
486 mutt_make_help (buf, sizeof (buf), _("Select "), MENU_PGP,
487 OP_GENERIC_SELECT_ENTRY);
488 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
489 mutt_make_help (buf, sizeof (buf), _("Check key "), MENU_PGP,
491 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
492 mutt_make_help (buf, sizeof (buf), _("Help"), MENU_PGP, OP_HELP);
493 strcat (helpstr, buf); /* __STRCAT_CHECKED__ */
495 menu = mutt_new_menu ();
497 menu->make_entry = pgp_entry;
498 menu->menu = MENU_PGP;
499 menu->help = helpstr;
500 menu->data = KeyTable;
503 snprintf (buf, sizeof (buf), _("PGP keys matching <%s>."), p->mailbox);
505 snprintf (buf, sizeof (buf), _("PGP keys matching \"%s\"."), s);
515 switch (mutt_menuLoop (menu)) {
519 mutt_mktemp (tempfile);
520 if ((devnull = fopen ("/dev/null", "w")) == NULL) { /* __FOPEN_CHECKED__ */
521 mutt_perror _("Can't open /dev/null");
525 if ((fp = safe_fopen (tempfile, "w")) == NULL) {
527 mutt_perror _("Can't create temporary file");
532 mutt_message _("Invoking PGP...");
534 snprintf (tmpbuf, sizeof (tmpbuf), "0x%s",
535 pgp_keyid (pgp_principal_key
536 (KeyTable[menu->current]->parent)));
538 if ((thepid = pgp_invoke_verify_key (NULL, NULL, NULL, -1,
539 fileno (fp), fileno (devnull),
541 mutt_perror _("Can't create filter");
548 mutt_wait_filter (thepid);
552 snprintf (cmd, sizeof (cmd), _("Key ID: 0x%s"),
553 pgp_keyid (pgp_principal_key
554 (KeyTable[menu->current]->parent)));
555 mutt_do_pager (cmd, tempfile, 0, NULL);
556 menu->redraw = REDRAW_FULL;
562 mutt_message ("%s", KeyTable[menu->current]->addr);
565 case OP_GENERIC_SELECT_ENTRY:
568 /* XXX make error reporting more verbose */
570 if (option (OPTPGPCHECKTRUST))
571 if (!pgp_key_is_valid (KeyTable[menu->current]->parent)) {
572 mutt_error _("This key can't be used: expired/disabled/revoked.");
577 if (option (OPTPGPCHECKTRUST) &&
578 (!pgp_id_is_valid (KeyTable[menu->current])
579 || !pgp_id_is_strong (KeyTable[menu->current]))) {
581 char buff[LONG_STRING];
583 if (KeyTable[menu->current]->flags & KEYFLAG_CANTUSE)
584 s = N_("ID is expired/disabled/revoked.");
586 switch (KeyTable[menu->current]->trust & 0x03) {
588 s = N_("ID has undefined validity.");
591 s = N_("ID is not valid.");
594 s = N_("ID is only marginally valid.");
598 snprintf (buff, sizeof (buff),
599 _("%s Do you really want to use the key?"), _(s));
601 if (mutt_yesorno (buff, M_NO) != M_YES) {
608 kp = pgp_principal_key (KeyTable[menu->current]->parent);
610 kp = KeyTable[menu->current]->parent;
623 mutt_menuDestroy (&menu);
626 set_option (OPTNEEDREDRAW);
631 pgp_key_t pgp_ask_for_key (char *tag, char *whatfor,
632 short abilities, pgp_ring_t keyring)
635 char resp[SHORT_STRING];
636 struct pgp_cache *l = NULL;
643 for (l = id_defaults; l; l = l->next)
644 if (!mutt_strcasecmp (whatfor, l->what)) {
645 strfcpy (resp, NONULL (l->dflt), sizeof (resp));
653 if (mutt_get_field (tag, resp, sizeof (resp), M_CLEAR) != 0)
658 mutt_str_replace (&l->dflt, resp);
660 l = safe_malloc (sizeof (struct pgp_cache));
661 l->next = id_defaults;
663 l->what = safe_strdup (whatfor);
664 l->dflt = safe_strdup (resp);
668 if ((key = pgp_getkeybystr (resp, abilities, keyring)))
676 /* generate a public key attachment */
678 BODY *pgp_make_key_attachment (char *tempf)
681 char buff[LONG_STRING];
682 char tempfb[_POSIX_PATH_MAX], tmp[STRING];
689 unset_option (OPTPGPCHECKTRUST);
692 pgp_ask_for_key (_("Please enter the key ID: "), NULL, 0, PGP_PUBRING);
697 snprintf (tmp, sizeof (tmp), "0x%s", pgp_keyid (pgp_principal_key (key)));
701 mutt_mktemp (tempfb);
705 if ((tempfp = safe_fopen (tempf, tempf == tempfb ? "w" : "a")) == NULL) {
706 mutt_perror _("Can't create temporary file");
711 if ((devnull = fopen ("/dev/null", "w")) == NULL) { /* __FOPEN_CHECKED__ */
712 mutt_perror _("Can't open /dev/null");
720 mutt_message _("Invoking pgp...");
724 pgp_invoke_export (NULL, NULL, NULL, -1,
725 fileno (tempfp), fileno (devnull), tmp)) == -1) {
726 mutt_perror _("Can't create filter");
734 mutt_wait_filter (thepid);
739 att = mutt_new_body ();
740 att->filename = safe_strdup (tempf);
743 att->type = TYPEAPPLICATION;
744 att->subtype = safe_strdup ("pgp-keys");
745 snprintf (buff, sizeof (buff), _("PGP Key %s."), tmp);
746 att->description = safe_strdup (buff);
747 mutt_update_encoding (att);
750 att->length = sb.st_size;
755 static LIST *pgp_add_string_to_hints (LIST * hints, const char *str)
760 if ((scratch = safe_strdup (str)) == NULL)
763 for (t = strtok (scratch, " ,.:\"()<>\n"); t;
764 t = strtok (NULL, " ,.:\"()<>\n")) {
765 if (mutt_strlen (t) > 3)
766 hints = mutt_add_list (hints, t);
773 static pgp_key_t *pgp_get_lastp (pgp_key_t p)
775 for (; p; p = p->next)
782 pgp_key_t pgp_getkeybyaddr (ADDRESS * a, short abilities, pgp_ring_t keyring)
790 int this_key_has_strong;
791 int this_key_has_weak;
792 int this_key_has_invalid;
795 pgp_key_t keys, k, kn;
796 pgp_key_t the_valid_key = NULL;
797 pgp_key_t matches = NULL;
798 pgp_key_t *last = &matches;
802 hints = pgp_add_string_to_hints (hints, a->mailbox);
803 if (a && a->personal)
804 hints = pgp_add_string_to_hints (hints, a->personal);
806 mutt_message (_("Looking for keys matching \"%s\"..."), a->mailbox);
807 keys = pgp_get_candidates (keyring, hints);
809 mutt_free_list (&hints);
814 dprint (5, (debugfile, "pgp_getkeybyaddr: looking for %s <%s>.",
815 a->personal, a->mailbox));
818 for (k = keys; k; k = kn) {
821 dprint (5, (debugfile, " looking at key: %s\n", pgp_keyid (k)));
823 if (abilities && !(k->flags & abilities)) {
824 dprint (5, (debugfile, " insufficient abilities: Has %x, want %x\n",
825 k->flags, abilities));
829 this_key_has_weak = 0; /* weak but valid match */
830 this_key_has_invalid = 0; /* invalid match */
831 this_key_has_strong = 0; /* strong and valid match */
832 match = 0; /* any match */
834 for (q = k->address; q; q = q->next) {
835 r = rfc822_parse_adrlist (NULL, q->addr);
837 for (p = r; p; p = p->next) {
838 int validity = pgp_id_matches_addr (a, p, q);
840 if (validity & PGP_KV_MATCH) /* something matches */
843 /* is this key a strong candidate? */
844 if ((validity & PGP_KV_VALID) && (validity & PGP_KV_STRONGID)
845 && (validity & PGP_KV_ADDR)) {
846 if (the_valid_key && the_valid_key != k)
849 this_key_has_strong = 1;
851 else if ((validity & PGP_KV_MATCH) && !(validity & PGP_KV_VALID))
852 this_key_has_invalid = 1;
853 else if ((validity & PGP_KV_MATCH)
854 && (!(validity & PGP_KV_STRONGID)
855 || !(validity & PGP_KV_ADDR)))
856 this_key_has_weak = 1;
859 rfc822_free_address (&r);
862 if (match && !this_key_has_strong && this_key_has_invalid)
864 if (match && !this_key_has_strong && this_key_has_weak)
868 *last = pgp_principal_key (k);
869 kn = pgp_remove_key (&keys, *last);
870 last = pgp_get_lastp (k);
874 pgp_free_key (&keys);
877 if (the_valid_key && !multi /* && !weak
878 && !(invalid && option (OPTPGPSHOWUNUSABLE)) */ ) {
880 * There was precisely one strong match on a valid ID.
882 * Proceed without asking the user.
884 pgp_remove_key (&matches, the_valid_key);
885 pgp_free_key (&matches);
890 * Else: Ask the user.
892 if ((k = pgp_select_key (matches, a, NULL)))
893 pgp_remove_key (&matches, k);
894 pgp_free_key (&matches);
903 pgp_key_t pgp_getkeybystr (char *p, short abilities, pgp_ring_t keyring)
907 pgp_key_t matches = NULL;
908 pgp_key_t *last = &matches;
913 mutt_message (_("Looking for keys matching \"%s\"..."), p);
915 hints = pgp_add_string_to_hints (hints, p);
916 keys = pgp_get_candidates (keyring, hints);
917 mutt_free_list (&hints);
923 for (k = keys; k; k = kn) {
925 if (abilities && !(k->flags & abilities))
930 for (a = k->address; a; a = a->next) {
933 "pgp_getkeybystr: matching \"%s\" against key %s, \"%s\": ", p,
934 pgp_keyid (k), a->addr));
935 if (!*p || mutt_strcasecmp (p, pgp_keyid (k)) == 0
936 || (!mutt_strncasecmp (p, "0x", 2)
937 && !mutt_strcasecmp (p + 2, pgp_keyid (k)))
938 || (option (OPTPGPLONGIDS) && !mutt_strncasecmp (p, "0x", 2)
939 && !mutt_strcasecmp (p + 2, k->keyid + 8))
940 || mutt_stristr (a->addr, p)) {
941 dprint (5, (debugfile, "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 */