2 * Copyright notice from original mutt:
3 * Copyright (C) 1998-2000 Werner Koch <werner.koch@guug.de>
4 * Copyright (C) 1999-2000 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.
14 * This code used to be the parser for GnuPG's output.
16 * Nowadays, we are using an external pubring lister with PGP which mimics
17 * gpg's output format.
29 #include <sys/types.h>
45 #include "lib/debug.h"
48 * Read the GNUPG keys. For now we read the complete keyring by
49 * calling gnupg in a special mode.
51 * The output format of gpgm is colon delimited with these fields:
52 * - record type ("pub","uid","sig","rev" etc.)
56 * - 16 hex digits with the long keyid.
57 * - timestamp (1998-02-28)
64 /* decode the backslash-escaped user ids. */
66 static char *_chs = 0;
68 static void fix_uid (char *uid)
73 for (s = d = uid; *s;) {
74 if (*s == '\\' && *(s + 1) == 'x' && isxdigit ((unsigned char) *(s + 2))
75 && isxdigit ((unsigned char) *(s + 3))) {
76 *d++ = hexval (*(s + 2)) << 4 | hexval (*(s + 3));
84 if (_chs && (cd = mutt_iconv_open (_chs, "utf-8", 0)) != (iconv_t) - 1) {
85 int n = s - uid + 1; /* chars available in original buffer */
91 buf = mem_malloc (n + 1);
92 ib = uid, ibl = d - uid + 1, ob = buf, obl = n;
93 iconv (cd, &ib, &ibl, &ob, &obl);
96 memcpy (uid, buf, ob - buf);
99 else if (ob - buf == n && (buf[n] = 0, str_len (buf) < n))
100 memcpy (uid, buf, n);
107 static pgp_key_t parse_pub_line (char *buf, int *is_subkey, pgp_key_t k)
109 pgp_uid_t *uid = NULL;
110 int field = 0, is_uid = 0;
119 debug_print (2, ("buf = `%s'\n", buf));
121 for (p = buf; p; p = pend) {
122 if ((pend = strchr (p, ':')))
125 if (field > 1 && !*p)
129 case 1: /* record type */
131 debug_print (2, ("record type: %s\n", p));
133 if (!str_cmp (p, "pub"));
134 else if (!str_cmp (p, "sub"))
136 else if (!str_cmp (p, "sec"));
137 else if (!str_cmp (p, "ssb"))
139 else if (!str_cmp (p, "uid"))
144 if (!(is_uid || (*is_subkey && option (OPTPGPIGNORESUB))))
145 k = mem_calloc (sizeof *k, 1);
149 case 2: /* trust info */
151 debug_print (2, ("trust info: %s\n", p));
153 switch (*p) { /* look only at the first letter */
155 flags |= KEYFLAG_EXPIRED;
158 flags |= KEYFLAG_REVOKED;
161 flags |= KEYFLAG_DISABLED;
177 if (!is_uid && !(*is_subkey && option (OPTPGPIGNORESUB)))
182 case 3: /* key length */
185 debug_print (2, ("key len: %s\n", p));
187 if (!(*is_subkey && option (OPTPGPIGNORESUB)))
188 k->keylen = atoi (p); /* fixme: add validation checks */
191 case 4: /* pubkey algo */
194 debug_print (2, ("pubkey algorithm: %s\n", p));
196 if (!(*is_subkey && option (OPTPGPIGNORESUB))) {
197 k->numalg = atoi (p);
198 k->algorithm = pgp_pkalgbytype (atoi (p));
202 case 5: /* 16 hex digits with the long keyid. */
204 debug_print (2, ("key id: %s\n", p));
206 if (!(*is_subkey && option (OPTPGPIGNORESUB)))
207 str_replace (&k->keyid, p);
211 case 6: /* timestamp (1998-02-28) */
216 debug_print (2, ("time stamp: %s\n", p));
223 strncpy (tstr, p, 11);
225 time.tm_year = atoi (tstr) - 1900;
227 time.tm_mon = (atoi (tstr + 5)) - 1;
228 time.tm_mday = atoi (tstr + 8);
229 k->gen_time = mutt_mktime (&time, 0);
232 case 7: /* valid for n days */
234 case 8: /* Local id */
236 case 9: /* ownertrust */
241 break; /* empty field or no trailing colon */
243 /* ignore user IDs on subkeys */
244 if (!is_uid && (*is_subkey && option (OPTPGPIGNORESUB)))
247 debug_print (2, ("user ID: %s\n", p));
249 uid = mem_calloc (sizeof (pgp_uid_t), 1);
251 uid->addr = str_dup (p);
255 uid->next = k->address;
258 if (strstr (p, "ENCR"))
259 k->flags |= KEYFLAG_PREFER_ENCRYPTION;
260 if (strstr (p, "SIGN"))
261 k->flags |= KEYFLAG_PREFER_SIGNING;
265 case 11: /* signature class */
267 case 12: /* key capabilities */
268 debug_print (2, ("capabilities info: %s\n", p));
273 flags |= KEYFLAG_DISABLED;
277 flags |= KEYFLAG_CANENCRYPT;
281 flags |= KEYFLAG_CANSIGN;
286 if (!is_uid && (!*is_subkey || !option (OPTPGPIGNORESUB)
287 || !((flags & KEYFLAG_DISABLED)
288 || (flags & KEYFLAG_REVOKED)
289 || (flags & KEYFLAG_EXPIRED))))
301 pgp_key_t pgp_get_candidates (pgp_ring_t keyring, LIST * hints)
305 char buf[LONG_STRING];
306 pgp_key_t db = NULL, *kend, k = NULL, kk, mainkey = NULL;
310 if ((devnull = open ("/dev/null", O_RDWR)) == -1)
313 str_replace (&_chs, Charset);
315 thepid = pgp_invoke_list_keys (NULL, &fp, NULL, -1, -1, devnull,
324 while (fgets (buf, sizeof (buf) - 1, fp)) {
325 if (!(kk = parse_pub_line (buf, &is_sub, k)))
328 /* Only append kk to the list if it's new. */
337 k->flags |= KEYFLAG_SUBKEY;
339 for (l = &k->address; *l; l = &(*l)->next);
340 *l = pgp_copy_uids (mainkey->address, k);
348 mutt_perror ("fgets");
351 mutt_wait_filter (thepid);