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>
36 #include <lib-lib/mem.h>
37 #include <lib-lib/str.h>
39 #include <lib-mime/mime.h>
47 #include "lib/debug.h"
50 * Read the GNUPG keys. For now we read the complete keyring by
51 * calling gnupg in a special mode.
53 * The output format of gpgm is colon delimited with these fields:
54 * - record type ("pub","uid","sig","rev" etc.)
58 * - 16 hex digits with the long keyid.
59 * - timestamp (1998-02-28)
66 /* decode the backslash-escaped user ids. */
68 static char *_chs = 0;
70 static void fix_uid (char *uid)
75 for (s = d = uid; *s;) {
76 if (*s == '\\' && *(s + 1) == 'x' && isxdigit ((unsigned char) *(s + 2))
77 && isxdigit ((unsigned char) *(s + 3))) {
78 *d++ = hexval (*(s + 2)) << 4 | hexval (*(s + 3));
86 if (_chs && (cd = mutt_iconv_open (_chs, "utf-8", 0)) != (iconv_t) - 1) {
87 int n = s - uid + 1; /* chars available in original buffer */
93 buf = p_new(char, n + 1);
94 ib = uid, ibl = d - uid + 1, ob = buf, obl = n;
95 my_iconv(cd, &ib, &ibl, &ob, &obl);
98 memcpy (uid, buf, ob - buf);
101 else if (ob - buf == n && (buf[n] = 0, m_strlen(buf) < n))
102 memcpy (uid, buf, n);
109 static pgp_key_t parse_pub_line (char *buf, int *is_subkey, pgp_key_t k)
111 pgp_uid_t *uid = NULL;
112 int field = 0, is_uid = 0;
121 debug_print (2, ("buf = `%s'\n", buf));
123 for (p = buf; p; p = pend) {
124 if ((pend = strchr (p, ':')))
127 if (field > 1 && !*p)
131 case 1: /* record type */
133 debug_print (2, ("record type: %s\n", p));
135 if (!m_strcmp(p, "pub"));
136 else if (!m_strcmp(p, "sub"))
138 else if (!m_strcmp(p, "sec"));
139 else if (!m_strcmp(p, "ssb"))
141 else if (!m_strcmp(p, "uid"))
146 if (!(is_uid || (*is_subkey && option (OPTPGPIGNORESUB))))
147 k = pgp_new_keyinfo();
151 case 2: /* trust info */
153 debug_print (2, ("trust info: %s\n", p));
155 switch (*p) { /* look only at the first letter */
157 flags |= KEYFLAG_EXPIRED;
160 flags |= KEYFLAG_REVOKED;
163 flags |= KEYFLAG_DISABLED;
179 if (!is_uid && !(*is_subkey && option (OPTPGPIGNORESUB)))
184 case 3: /* key length */
187 debug_print (2, ("key len: %s\n", p));
189 if (!(*is_subkey && option (OPTPGPIGNORESUB)))
190 k->keylen = atoi (p); /* fixme: add validation checks */
193 case 4: /* pubkey algo */
196 debug_print (2, ("pubkey algorithm: %s\n", p));
198 if (!(*is_subkey && option (OPTPGPIGNORESUB))) {
199 k->numalg = atoi (p);
200 k->algorithm = pgp_pkalgbytype (atoi (p));
204 case 5: /* 16 hex digits with the long keyid. */
206 debug_print (2, ("key id: %s\n", p));
208 if (!(*is_subkey && option (OPTPGPIGNORESUB)))
209 str_replace (&k->keyid, p);
213 case 6: /* timestamp (1998-02-28) */
218 debug_print (2, ("time stamp: %s\n", p));
225 m_strcpy(tstr, sizeof(tstr), p);
227 time.tm_year = atoi (tstr) - 1900;
229 time.tm_mon = (atoi (tstr + 5)) - 1;
230 time.tm_mday = atoi (tstr + 8);
231 k->gen_time = mutt_mktime (&time, 0);
234 case 7: /* valid for n days */
236 case 8: /* Local id */
238 case 9: /* ownertrust */
243 break; /* empty field or no trailing colon */
245 /* ignore user IDs on subkeys */
246 if (!is_uid && (*is_subkey && option (OPTPGPIGNORESUB)))
249 debug_print (2, ("user ID: %s\n", p));
251 uid = p_new(pgp_uid_t, 1);
253 uid->addr = m_strdup(p);
257 uid->next = k->address;
260 if (strstr (p, "ENCR"))
261 k->flags |= KEYFLAG_PREFER_ENCRYPTION;
262 if (strstr (p, "SIGN"))
263 k->flags |= KEYFLAG_PREFER_SIGNING;
267 case 11: /* signature class */
269 case 12: /* key capabilities */
270 debug_print (2, ("capabilities info: %s\n", p));
275 flags |= KEYFLAG_DISABLED;
279 flags |= KEYFLAG_CANENCRYPT;
283 flags |= KEYFLAG_CANSIGN;
288 if (!is_uid && (!*is_subkey || !option (OPTPGPIGNORESUB)
289 || !((flags & KEYFLAG_DISABLED)
290 || (flags & KEYFLAG_REVOKED)
291 || (flags & KEYFLAG_EXPIRED))))
303 pgp_key_t pgp_get_candidates (pgp_ring_t keyring, LIST * hints)
307 char buf[LONG_STRING];
308 pgp_key_t db = NULL, *kend, k = NULL, kk, mainkey = NULL;
312 if ((devnull = open ("/dev/null", O_RDWR)) == -1)
315 str_replace (&_chs, Charset);
317 thepid = pgp_invoke_list_keys (NULL, &fp, NULL, -1, -1, devnull,
326 while (fgets (buf, sizeof (buf) - 1, fp)) {
327 if (!(kk = parse_pub_line (buf, &is_sub, k)))
330 /* Only append kk to the list if it's new. */
339 k->flags |= KEYFLAG_SUBKEY;
341 for (l = &k->address; *l; l = &(*l)->next);
342 *l = pgp_copy_uids (mainkey->address, k);
350 mutt_perror ("fgets");
353 mutt_wait_filter (thepid);