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>
44 * Read the GNUPG keys. For now we read the complete keyring by
45 * calling gnupg in a special mode.
47 * The output format of gpgm is colon delimited with these fields:
48 * - record type ("pub","uid","sig","rev" etc.)
52 * - 16 hex digits with the long keyid.
53 * - timestamp (1998-02-28)
60 /* decode the backslash-escaped user ids. */
62 static char *_chs = 0;
64 static void fix_uid (char *uid)
69 for (s = d = uid; *s;) {
70 if (*s == '\\' && *(s + 1) == 'x' && isxdigit ((unsigned char) *(s + 2))
71 && isxdigit ((unsigned char) *(s + 3))) {
72 *d++ = hexval (*(s + 2)) << 4 | hexval (*(s + 3));
80 if (_chs && (cd = mutt_iconv_open (_chs, "utf-8", 0)) != (iconv_t) - 1) {
81 int n = s - uid + 1; /* chars available in original buffer */
87 buf = safe_malloc (n + 1);
88 ib = uid, ibl = d - uid + 1, ob = buf, obl = n;
89 iconv (cd, &ib, &ibl, &ob, &obl);
92 memcpy (uid, buf, ob - buf);
95 else if (ob - buf == n && (buf[n] = 0, mutt_strlen (buf) < n))
103 static pgp_key_t parse_pub_line (char *buf, int *is_subkey, pgp_key_t k)
105 pgp_uid_t *uid = NULL;
106 int field = 0, is_uid = 0;
115 dprint (2, (debugfile, "parse_pub_line: buf = `%s'\n", buf));
117 for (p = buf; p; p = pend) {
118 if ((pend = strchr (p, ':')))
121 if (field > 1 && !*p)
125 case 1: /* record type */
127 dprint (2, (debugfile, "record type: %s\n", p));
129 if (!mutt_strcmp (p, "pub"));
130 else if (!mutt_strcmp (p, "sub"))
132 else if (!mutt_strcmp (p, "sec"));
133 else if (!mutt_strcmp (p, "ssb"))
135 else if (!mutt_strcmp (p, "uid"))
140 if (!(is_uid || (*is_subkey && option (OPTPGPIGNORESUB))))
141 k = safe_calloc (sizeof *k, 1);
145 case 2: /* trust info */
147 dprint (2, (debugfile, "trust info: %s\n", p));
149 switch (*p) { /* look only at the first letter */
151 flags |= KEYFLAG_EXPIRED;
154 flags |= KEYFLAG_REVOKED;
157 flags |= KEYFLAG_DISABLED;
173 if (!is_uid && !(*is_subkey && option (OPTPGPIGNORESUB)))
178 case 3: /* key length */
181 dprint (2, (debugfile, "key len: %s\n", p));
183 if (!(*is_subkey && option (OPTPGPIGNORESUB)))
184 k->keylen = atoi (p); /* fixme: add validation checks */
187 case 4: /* pubkey algo */
190 dprint (2, (debugfile, "pubkey algorithm: %s\n", p));
192 if (!(*is_subkey && option (OPTPGPIGNORESUB))) {
193 k->numalg = atoi (p);
194 k->algorithm = pgp_pkalgbytype (atoi (p));
198 case 5: /* 16 hex digits with the long keyid. */
200 dprint (2, (debugfile, "key id: %s\n", p));
202 if (!(*is_subkey && option (OPTPGPIGNORESUB)))
203 mutt_str_replace (&k->keyid, p);
207 case 6: /* timestamp (1998-02-28) */
212 dprint (2, (debugfile, "time stamp: %s\n", p));
219 strncpy (tstr, p, 11);
221 time.tm_year = atoi (tstr) - 1900;
223 time.tm_mon = (atoi (tstr + 5)) - 1;
224 time.tm_mday = atoi (tstr + 8);
225 k->gen_time = mutt_mktime (&time, 0);
228 case 7: /* valid for n days */
230 case 8: /* Local id */
232 case 9: /* ownertrust */
237 break; /* empty field or no trailing colon */
239 /* ignore user IDs on subkeys */
240 if (!is_uid && (*is_subkey && option (OPTPGPIGNORESUB)))
243 dprint (2, (debugfile, "user ID: %s\n", p));
245 uid = safe_calloc (sizeof (pgp_uid_t), 1);
247 uid->addr = safe_strdup (p);
251 uid->next = k->address;
254 if (strstr (p, "ENCR"))
255 k->flags |= KEYFLAG_PREFER_ENCRYPTION;
256 if (strstr (p, "SIGN"))
257 k->flags |= KEYFLAG_PREFER_SIGNING;
261 case 11: /* signature class */
263 case 12: /* key capabilities */
264 dprint (2, (debugfile, "capabilities info: %s\n", p));
269 flags |= KEYFLAG_DISABLED;
273 flags |= KEYFLAG_CANENCRYPT;
277 flags |= KEYFLAG_CANSIGN;
282 if (!is_uid && (!*is_subkey || !option (OPTPGPIGNORESUB)
283 || !((flags & KEYFLAG_DISABLED)
284 || (flags & KEYFLAG_REVOKED)
285 || (flags & KEYFLAG_EXPIRED))))
297 pgp_key_t pgp_get_candidates (pgp_ring_t keyring, LIST * hints)
301 char buf[LONG_STRING];
302 pgp_key_t db = NULL, *kend, k = NULL, kk, mainkey = NULL;
306 if ((devnull = open ("/dev/null", O_RDWR)) == -1)
309 mutt_str_replace (&_chs, Charset);
311 thepid = pgp_invoke_list_keys (NULL, &fp, NULL, -1, -1, devnull,
320 while (fgets (buf, sizeof (buf) - 1, fp)) {
321 if (!(kk = parse_pub_line (buf, &is_sub, k)))
324 /* Only append kk to the list if it's new. */
333 k->flags |= KEYFLAG_SUBKEY;
335 for (l = &k->address; *l; l = &(*l)->next);
336 *l = pgp_copy_uids (mainkey->address, k);
344 mutt_perror ("fgets");
347 mutt_wait_filter (thepid);