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;
126 struct pgp_keyinfo tmp;
132 /* if we're given a key, merge our parsing results, else
133 * start with a fresh one to work with so that we don't
134 * mess up the real key in case we find parsing errors. */
136 memcpy (&tmp, k, sizeof (tmp));
138 memset (&tmp, 0, sizeof (tmp));
140 dprint (2, (debugfile, "parse_pub_line: buf = `%s'\n", buf));
142 for (p = buf; p; p = pend)
144 if ((pend = strchr (p, ':')))
147 if (field > 1 && !*p)
152 case 1: /* record type */
154 dprint (2, (debugfile, "record type: %s\n", p));
156 if (!mutt_strcmp (p, "pub"))
158 else if (!mutt_strcmp (p, "sub"))
160 else if (!mutt_strcmp (p, "sec"))
162 else if (!mutt_strcmp (p, "ssb"))
164 else if (!mutt_strcmp (p, "uid"))
169 if (!(is_uid || (*is_subkey && option (OPTPGPIGNORESUB))))
170 memset (&tmp, 0, sizeof (tmp));
174 case 2: /* trust info */
176 dprint (2, (debugfile, "trust info: %s\n", p));
179 { /* look only at the first letter */
181 flags |= KEYFLAG_EXPIRED;
184 flags |= KEYFLAG_REVOKED;
187 flags |= KEYFLAG_DISABLED;
203 if (!is_uid && !(*is_subkey && option (OPTPGPIGNORESUB)))
208 case 3: /* key length */
210 dprint (2, (debugfile, "key len: %s\n", p));
212 if (!(*is_subkey && option (OPTPGPIGNORESUB)) &&
213 mutt_atos (p, &tmp.keylen) < 0)
217 case 4: /* pubkey algo */
219 dprint (2, (debugfile, "pubkey algorithm: %s\n", p));
221 if (!(*is_subkey && option (OPTPGPIGNORESUB)))
224 if (mutt_atoi (p, &x) < 0)
227 tmp.algorithm = pgp_pkalgbytype (x);
231 case 5: /* 16 hex digits with the long keyid. */
233 dprint (2, (debugfile, "key id: %s\n", p));
235 if (!(*is_subkey && option (OPTPGPIGNORESUB)))
236 mutt_str_replace (&tmp.keyid, p);
240 case 6: /* timestamp (1998-02-28) */
245 dprint (2, (debugfile, "time stamp: %s\n", p));
252 strncpy (tstr, p, 11);
255 if (mutt_atoi (tstr, &time.tm_year) < 0)
260 time.tm_year -= 1900;
261 if (mutt_atoi (tstr+5, &time.tm_mon) < 0)
267 if (mutt_atoi (tstr+8, &time.tm_mday) < 0)
272 tmp.gen_time = mutt_mktime (&time, 0);
275 case 7: /* valid for n days */
277 case 8: /* Local id */
279 case 9: /* ownertrust */
284 break; /* empty field or no trailing colon */
286 /* ignore user IDs on subkeys */
287 if (!is_uid && (*is_subkey && option (OPTPGPIGNORESUB)))
290 dprint (2, (debugfile, "user ID: %s\n", p));
292 uid = safe_calloc (sizeof (pgp_uid_t), 1);
294 uid->addr = safe_strdup (p);
297 uid->next = tmp.address;
300 if (strstr (p, "ENCR"))
301 tmp.flags |= KEYFLAG_PREFER_ENCRYPTION;
302 if (strstr (p, "SIGN"))
303 tmp.flags |= KEYFLAG_PREFER_SIGNING;
307 case 11: /* signature class */
309 case 12: /* key capabilities */
310 dprint (2, (debugfile, "capabilities info: %s\n", p));
317 flags |= KEYFLAG_DISABLED;
321 flags |= KEYFLAG_CANENCRYPT;
325 flags |= KEYFLAG_CANSIGN;
331 (!*is_subkey || !option (OPTPGPIGNORESUB)
332 || !((flags & KEYFLAG_DISABLED)
333 || (flags & KEYFLAG_REVOKED)
334 || (flags & KEYFLAG_EXPIRED))))
344 /* merge temp key back into real key */
345 if (!(is_uid || (*is_subkey && option (OPTPGPIGNORESUB))))
346 k = safe_malloc (sizeof (*k));
347 memcpy (k, &tmp, sizeof (*k));
348 /* fixup parentship of uids after mering the temp key into
352 for (uid = k->address; uid; uid = uid->next)
359 dprint(5,(debugfile,"parse_pub_line: invalid number: '%s'\n", p));
363 pgp_key_t pgp_get_candidates (pgp_ring_t keyring, LIST * hints)
367 char buf[LONG_STRING];
368 pgp_key_t db = NULL, *kend, k = NULL, kk, mainkey = NULL;
372 if ((devnull = open ("/dev/null", O_RDWR)) == -1)
375 mutt_str_replace (&_chs, Charset);
377 thepid = pgp_invoke_list_keys (NULL, &fp, NULL, -1, -1, devnull,
387 while (fgets (buf, sizeof (buf) - 1, fp))
389 if (!(kk = parse_pub_line (buf, &is_sub, k)))
392 /* Only append kk to the list if it's new. */
403 k->flags |= KEYFLAG_SUBKEY;
405 for (l = &k->address; *l; l = &(*l)->next)
407 *l = pgp_copy_uids (mainkey->address, k);
415 mutt_perror ("fgets");
418 mutt_wait_filter (thepid);