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>
47 * Read the GNUPG keys. For now we read the complete keyring by
48 * calling gnupg in a special mode.
50 * The output format of gpgm is colon delimited with these fields:
51 * - record type ("pub","uid","sig","rev" etc.)
55 * - 16 hex digits with the long keyid.
56 * - timestamp (1998-02-28)
63 /* decode the backslash-escaped user ids. */
65 static char *_chs = 0;
67 static void fix_uid (char *uid)
72 for (s = d = uid; *s;) {
73 if (*s == '\\' && *(s + 1) == 'x' && isxdigit ((unsigned char) *(s + 2))
74 && isxdigit ((unsigned char) *(s + 3))) {
75 *d++ = hexval (*(s + 2)) << 4 | hexval (*(s + 3));
83 if (_chs && (cd = mutt_iconv_open (_chs, "utf-8", 0)) != (iconv_t) - 1) {
84 int n = s - uid + 1; /* chars available in original buffer */
90 buf = safe_malloc (n + 1);
91 ib = uid, ibl = d - uid + 1, ob = buf, obl = n;
92 iconv (cd, &ib, &ibl, &ob, &obl);
95 memcpy (uid, buf, ob - buf);
98 else if (ob - buf == n && (buf[n] = 0, mutt_strlen (buf) < n))
106 static pgp_key_t parse_pub_line (char *buf, int *is_subkey, pgp_key_t k)
108 pgp_uid_t *uid = NULL;
109 int field = 0, is_uid = 0;
118 dprint (2, (debugfile, "parse_pub_line: buf = `%s'\n", buf));
120 for (p = buf; p; p = pend) {
121 if ((pend = strchr (p, ':')))
124 if (field > 1 && !*p)
128 case 1: /* record type */
130 dprint (2, (debugfile, "record type: %s\n", p));
132 if (!mutt_strcmp (p, "pub"));
133 else if (!mutt_strcmp (p, "sub"))
135 else if (!mutt_strcmp (p, "sec"));
136 else if (!mutt_strcmp (p, "ssb"))
138 else if (!mutt_strcmp (p, "uid"))
143 if (!(is_uid || (*is_subkey && option (OPTPGPIGNORESUB))))
144 k = safe_calloc (sizeof *k, 1);
148 case 2: /* trust info */
150 dprint (2, (debugfile, "trust info: %s\n", p));
152 switch (*p) { /* look only at the first letter */
154 flags |= KEYFLAG_EXPIRED;
157 flags |= KEYFLAG_REVOKED;
160 flags |= KEYFLAG_DISABLED;
176 if (!is_uid && !(*is_subkey && option (OPTPGPIGNORESUB)))
181 case 3: /* key length */
184 dprint (2, (debugfile, "key len: %s\n", p));
186 if (!(*is_subkey && option (OPTPGPIGNORESUB)))
187 k->keylen = atoi (p); /* fixme: add validation checks */
190 case 4: /* pubkey algo */
193 dprint (2, (debugfile, "pubkey algorithm: %s\n", p));
195 if (!(*is_subkey && option (OPTPGPIGNORESUB))) {
196 k->numalg = atoi (p);
197 k->algorithm = pgp_pkalgbytype (atoi (p));
201 case 5: /* 16 hex digits with the long keyid. */
203 dprint (2, (debugfile, "key id: %s\n", p));
205 if (!(*is_subkey && option (OPTPGPIGNORESUB)))
206 mutt_str_replace (&k->keyid, p);
210 case 6: /* timestamp (1998-02-28) */
215 dprint (2, (debugfile, "time stamp: %s\n", p));
222 strncpy (tstr, p, 11);
224 time.tm_year = atoi (tstr) - 1900;
226 time.tm_mon = (atoi (tstr + 5)) - 1;
227 time.tm_mday = atoi (tstr + 8);
228 k->gen_time = mutt_mktime (&time, 0);
231 case 7: /* valid for n days */
233 case 8: /* Local id */
235 case 9: /* ownertrust */
240 break; /* empty field or no trailing colon */
242 /* ignore user IDs on subkeys */
243 if (!is_uid && (*is_subkey && option (OPTPGPIGNORESUB)))
246 dprint (2, (debugfile, "user ID: %s\n", p));
248 uid = safe_calloc (sizeof (pgp_uid_t), 1);
250 uid->addr = safe_strdup (p);
254 uid->next = k->address;
257 if (strstr (p, "ENCR"))
258 k->flags |= KEYFLAG_PREFER_ENCRYPTION;
259 if (strstr (p, "SIGN"))
260 k->flags |= KEYFLAG_PREFER_SIGNING;
264 case 11: /* signature class */
266 case 12: /* key capabilities */
267 dprint (2, (debugfile, "capabilities info: %s\n", p));
272 flags |= KEYFLAG_DISABLED;
276 flags |= KEYFLAG_CANENCRYPT;
280 flags |= KEYFLAG_CANSIGN;
285 if (!is_uid && (!*is_subkey || !option (OPTPGPIGNORESUB)
286 || !((flags & KEYFLAG_DISABLED)
287 || (flags & KEYFLAG_REVOKED)
288 || (flags & KEYFLAG_EXPIRED))))
300 pgp_key_t pgp_get_candidates (pgp_ring_t keyring, LIST * hints)
304 char buf[LONG_STRING];
305 pgp_key_t db = NULL, *kend, k = NULL, kk, mainkey = NULL;
309 if ((devnull = open ("/dev/null", O_RDWR)) == -1)
312 mutt_str_replace (&_chs, Charset);
314 thepid = pgp_invoke_list_keys (NULL, &fp, NULL, -1, -1, devnull,
323 while (fgets (buf, sizeof (buf) - 1, fp)) {
324 if (!(kk = parse_pub_line (buf, &is_sub, k)))
327 /* Only append kk to the list if it's new. */
336 k->flags |= KEYFLAG_SUBKEY;
338 for (l = &k->address; *l; l = &(*l)->next);
339 *l = pgp_copy_uids (mainkey->address, k);
347 mutt_perror ("fgets");
350 mutt_wait_filter (thepid);