2 * Copyright (C) 1998-2000,2003 Werner Koch <werner.koch@guug.de>
3 * Copyright (C) 1999-2003 Thomas Roessler <roessler@does-not-exist.org>
5 * This program is free software; you can redistribute it
6 * and/or modify it under the terms of the GNU General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later
11 * This program is distributed in the hope that it will be
12 * useful, but WITHOUT ANY WARRANTY; without even the implied
13 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public
18 * License along with this program; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
26 * This code used to be the parser for GnuPG's output.
28 * Nowadays, we are using an external pubring lister with PGP which mimics
29 * gpg's output format.
41 #include <sys/types.h>
56 * Read the GNUPG keys. For now we read the complete keyring by
57 * calling gnupg in a special mode.
59 * The output format of gpgm is colon delimited with these fields:
60 * - record type ("pub","uid","sig","rev" etc.)
64 * - 16 hex digits with the long keyid.
65 * - timestamp (1998-02-28)
72 /* decode the backslash-escaped user ids. */
74 static char *_chs = 0;
76 static void fix_uid (char *uid)
81 for (s = d = uid; *s;)
83 if (*s == '\\' && *(s+1) == 'x' && isxdigit ((unsigned char) *(s+2)) && isxdigit ((unsigned char) *(s+3)))
85 *d++ = hexval (*(s+2)) << 4 | hexval (*(s+3));
93 if (_chs && (cd = mutt_iconv_open (_chs, "utf-8", 0)) != (iconv_t)-1)
95 int n = s - uid + 1; /* chars available in original buffer */
101 buf = safe_malloc (n+1);
102 ib = uid, ibl = d - uid + 1, ob = buf, obl = n;
103 iconv (cd, &ib, &ibl, &ob, &obl);
108 memcpy (uid, buf, ob-buf);
111 else if (n >= 0 && ob-buf == n && (buf[n] = 0, strlen (buf) < (size_t)n))
112 memcpy (uid, buf, n);
119 static pgp_key_t parse_pub_line (char *buf, int *is_subkey, pgp_key_t k)
121 pgp_uid_t *uid = NULL;
122 int field = 0, is_uid = 0;
128 struct pgp_keyinfo tmp;
134 /* if we're given a key, merge our parsing results, else
135 * start with a fresh one to work with so that we don't
136 * mess up the real key in case we find parsing errors. */
138 memcpy (&tmp, k, sizeof (tmp));
140 memset (&tmp, 0, sizeof (tmp));
142 dprint (2, (debugfile, "parse_pub_line: buf = `%s'\n", buf));
144 for (p = buf; p; p = pend)
146 if ((pend = strchr (p, ':')))
149 if (!*p && (field != 1) && (field != 10))
152 if (is_fpr && (field != 10))
157 case 1: /* record type */
159 dprint (2, (debugfile, "record type: %s\n", p));
161 if (!mutt_strcmp (p, "pub"))
163 else if (!mutt_strcmp (p, "sub"))
165 else if (!mutt_strcmp (p, "sec"))
167 else if (!mutt_strcmp (p, "ssb"))
169 else if (!mutt_strcmp (p, "uid"))
171 else if (!mutt_strcmp (p, "fpr"))
176 if (!(is_uid || is_fpr || (*is_subkey && option (OPTPGPIGNORESUB))))
177 memset (&tmp, 0, sizeof (tmp));
181 case 2: /* trust info */
183 dprint (2, (debugfile, "trust info: %s\n", p));
186 { /* look only at the first letter */
188 flags |= KEYFLAG_EXPIRED;
191 flags |= KEYFLAG_REVOKED;
194 flags |= KEYFLAG_DISABLED;
210 if (!is_uid && !(*is_subkey && option (OPTPGPIGNORESUB)))
215 case 3: /* key length */
217 dprint (2, (debugfile, "key len: %s\n", p));
219 if (!(*is_subkey && option (OPTPGPIGNORESUB)) &&
220 mutt_atos (p, &tmp.keylen) < 0)
224 case 4: /* pubkey algo */
226 dprint (2, (debugfile, "pubkey algorithm: %s\n", p));
228 if (!(*is_subkey && option (OPTPGPIGNORESUB)))
231 if (mutt_atoi (p, &x) < 0)
234 tmp.algorithm = pgp_pkalgbytype (x);
238 case 5: /* 16 hex digits with the long keyid. */
240 dprint (2, (debugfile, "key id: %s\n", p));
242 if (!(*is_subkey && option (OPTPGPIGNORESUB)))
243 mutt_str_replace (&tmp.keyid, p);
247 case 6: /* timestamp (1998-02-28) */
249 dprint (2, (debugfile, "time stamp: %s\n", p));
251 if (strchr (p, '-')) /* gpg pre-2.0.10 used format (yyyy-mm-dd) */
259 strncpy (tstr, p, 11);
262 if (mutt_atoi (tstr, &time.tm_year) < 0)
267 time.tm_year -= 1900;
268 if (mutt_atoi (tstr+5, &time.tm_mon) < 0)
274 if (mutt_atoi (tstr+8, &time.tm_mday) < 0)
279 tmp.gen_time = mutt_mktime (&time, 0);
281 else /* gpg 2.0.10+ uses seconds since 1970-01-01 */
283 unsigned long long secs;
285 if (mutt_atoull (p, &secs) < 0)
287 tmp.gen_time = (time_t)secs;
291 case 7: /* valid for n days */
293 case 8: /* Local id */
295 case 9: /* ownertrust */
299 /* Empty field or no trailing colon.
300 * We allow an empty field for a pub record type because it is
301 * possible for a primary uid record to have an empty User-ID
302 * field. Without any address records, it is not possible to
303 * use the key in mutt.
305 if (!(pend && (*p || is_pub)))
310 /* don't let a subkey fpr overwrite an existing primary key fpr */
311 if (!tmp.fingerprint)
312 tmp.fingerprint = safe_strdup (p);
316 /* ignore user IDs on subkeys */
317 if (!is_uid && (*is_subkey && option (OPTPGPIGNORESUB)))
320 dprint (2, (debugfile, "user ID: %s\n", NONULL (p)));
322 uid = safe_calloc (sizeof (pgp_uid_t), 1);
324 uid->addr = safe_strdup (p);
327 uid->next = tmp.address;
330 if (strstr (p, "ENCR"))
331 tmp.flags |= KEYFLAG_PREFER_ENCRYPTION;
332 if (strstr (p, "SIGN"))
333 tmp.flags |= KEYFLAG_PREFER_SIGNING;
337 case 11: /* signature class */
339 case 12: /* key capabilities */
340 dprint (2, (debugfile, "capabilities info: %s\n", p));
347 flags |= KEYFLAG_DISABLED;
351 flags |= KEYFLAG_CANENCRYPT;
355 flags |= KEYFLAG_CANSIGN;
361 (!*is_subkey || !option (OPTPGPIGNORESUB)
362 || !((flags & KEYFLAG_DISABLED)
363 || (flags & KEYFLAG_REVOKED)
364 || (flags & KEYFLAG_EXPIRED))))
374 /* merge temp key back into real key */
375 if (!(is_uid || is_fpr || (*is_subkey && option (OPTPGPIGNORESUB))))
376 k = safe_malloc (sizeof (*k));
377 memcpy (k, &tmp, sizeof (*k));
378 /* fixup parentship of uids after mering the temp key into
382 for (uid = k->address; uid; uid = uid->next)
389 dprint(5,(debugfile,"parse_pub_line: invalid number: '%s'\n", p));
393 pgp_key_t pgp_get_candidates (pgp_ring_t keyring, LIST * hints)
397 char buf[LONG_STRING];
398 pgp_key_t db = NULL, *kend, k = NULL, kk, mainkey = NULL;
402 if ((devnull = open ("/dev/null", O_RDWR)) == -1)
405 mutt_str_replace (&_chs, Charset);
407 thepid = pgp_invoke_list_keys (NULL, &fp, NULL, -1, -1, devnull,
417 while (fgets (buf, sizeof (buf) - 1, fp))
419 if (!(kk = parse_pub_line (buf, &is_sub, k)))
422 /* Only append kk to the list if it's new. */
433 k->flags |= KEYFLAG_SUBKEY;
435 for (l = &k->address; *l; l = &(*l)->next)
437 *l = pgp_copy_uids (mainkey->address, k);
445 mutt_perror ("fgets");
448 mutt_wait_filter (thepid);