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 (ob-buf == n && (buf[n] = 0, strlen (buf) < 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;
131 dprint (2, (debugfile, "parse_pub_line: buf = `%s'\n", buf));
133 for (p = buf; p; p = pend)
135 if ((pend = strchr (p, ':')))
138 if (field > 1 && !*p)
143 case 1: /* record type */
145 dprint (2, (debugfile, "record type: %s\n", p));
147 if (!mutt_strcmp (p, "pub"))
149 else if (!mutt_strcmp (p, "sub"))
151 else if (!mutt_strcmp (p, "sec"))
153 else if (!mutt_strcmp (p, "ssb"))
155 else if (!mutt_strcmp (p, "uid"))
160 if (!(is_uid || (*is_subkey && option (OPTPGPIGNORESUB))))
161 k = safe_calloc (sizeof *k, 1);
165 case 2: /* trust info */
167 dprint (2, (debugfile, "trust info: %s\n", p));
170 { /* look only at the first letter */
172 flags |= KEYFLAG_EXPIRED;
175 flags |= KEYFLAG_REVOKED;
178 flags |= KEYFLAG_DISABLED;
194 if (!is_uid && !(*is_subkey && option (OPTPGPIGNORESUB)))
199 case 3: /* key length */
202 dprint (2, (debugfile, "key len: %s\n", p));
204 if (!(*is_subkey && option (OPTPGPIGNORESUB)))
205 k->keylen = atoi (p); /* fixme: add validation checks */
208 case 4: /* pubkey algo */
211 dprint (2, (debugfile, "pubkey algorithm: %s\n", p));
213 if (!(*is_subkey && option (OPTPGPIGNORESUB)))
215 k->numalg = atoi (p);
216 k->algorithm = pgp_pkalgbytype (atoi (p));
220 case 5: /* 16 hex digits with the long keyid. */
222 dprint (2, (debugfile, "key id: %s\n", p));
224 if (!(*is_subkey && option (OPTPGPIGNORESUB)))
225 mutt_str_replace (&k->keyid, p);
229 case 6: /* timestamp (1998-02-28) */
234 dprint (2, (debugfile, "time stamp: %s\n", p));
241 strncpy (tstr, p, 11);
243 time.tm_year = atoi (tstr)-1900;
245 time.tm_mon = (atoi (tstr+5))-1;
246 time.tm_mday = atoi (tstr+8);
247 k->gen_time = mutt_mktime (&time, 0);
250 case 7: /* valid for n days */
252 case 8: /* Local id */
254 case 9: /* ownertrust */
259 break; /* empty field or no trailing colon */
261 /* ignore user IDs on subkeys */
262 if (!is_uid && (*is_subkey && option (OPTPGPIGNORESUB)))
265 dprint (2, (debugfile, "user ID: %s\n", p));
267 uid = safe_calloc (sizeof (pgp_uid_t), 1);
269 uid->addr = safe_strdup (p);
273 uid->next = k->address;
276 if (strstr (p, "ENCR"))
277 k->flags |= KEYFLAG_PREFER_ENCRYPTION;
278 if (strstr (p, "SIGN"))
279 k->flags |= KEYFLAG_PREFER_SIGNING;
283 case 11: /* signature class */
285 case 12: /* key capabilities */
286 dprint (2, (debugfile, "capabilities info: %s\n", p));
293 flags |= KEYFLAG_DISABLED;
297 flags |= KEYFLAG_CANENCRYPT;
301 flags |= KEYFLAG_CANSIGN;
307 (!*is_subkey || !option (OPTPGPIGNORESUB)
308 || !((flags & KEYFLAG_DISABLED)
309 || (flags & KEYFLAG_REVOKED)
310 || (flags & KEYFLAG_EXPIRED))))
322 pgp_key_t pgp_get_candidates (pgp_ring_t keyring, LIST * hints)
326 char buf[LONG_STRING];
327 pgp_key_t db = NULL, *kend, k = NULL, kk, mainkey = NULL;
331 if ((devnull = open ("/dev/null", O_RDWR)) == -1)
334 mutt_str_replace (&_chs, Charset);
336 thepid = pgp_invoke_list_keys (NULL, &fp, NULL, -1, -1, devnull,
346 while (fgets (buf, sizeof (buf) - 1, fp))
348 if (!(kk = parse_pub_line (buf, &is_sub, k)))
351 /* Only append kk to the list if it's new. */
362 k->flags |= KEYFLAG_SUBKEY;
364 for (l = &k->address; *l; l = &(*l)->next)
366 *l = pgp_copy_uids (mainkey->address, k);
374 mutt_perror ("fgets");
377 mutt_wait_filter (thepid);