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.
21 #include <lib-lib/lib-lib.h>
23 #include <lib-mime/mime.h>
24 #include <lib-sys/unix.h>
25 #include <lib-ui/curses.h>
32 * Read the GNUPG keys. For now we read the complete keyring by
33 * calling gnupg in a special mode.
35 * The output format of gpgm is colon delimited with these fields:
36 * - record type ("pub","uid","sig","rev" etc.)
40 * - 16 hex digits with the long keyid.
41 * - timestamp (1998-02-28)
48 /* decode the backslash-escaped user ids. */
50 static char *_chs = 0;
52 static void fix_uid (char *uid)
57 for (s = d = uid; *s;) {
58 if (*s == '\\' && *(s + 1) == 'x' && isxdigit ((unsigned char) *(s + 2))
59 && isxdigit ((unsigned char) *(s + 3))) {
60 *d++ = hexval (*(s + 2)) << 4 | hexval (*(s + 3));
68 if (_chs && (cd = mutt_iconv_open (_chs, "utf-8", 0)) != MUTT_ICONV_ERROR) {
69 int n = s - uid + 1; /* chars available in original buffer */
75 buf = p_new(char, n + 1);
76 ib = uid, ibl = d - uid + 1, ob = buf, obl = n;
77 my_iconv(cd, &ib, &ibl, &ob, &obl);
80 memcpy (uid, buf, ob - buf);
83 else if (ob - buf == n && (buf[n] = 0, m_strlen(buf) < n))
91 static pgp_key_t parse_pub_line (char *buf, int *is_subkey, pgp_key_t k)
93 pgp_uid_t *uid = NULL;
94 int field = 0, is_uid = 0;
103 for (p = buf; p; p = pend) {
104 if ((pend = strchr (p, ':')))
107 if (field > 1 && !*p)
111 case 1: /* record type */
113 if (!m_strcmp(p, "pub"));
114 else if (!m_strcmp(p, "sub"))
116 else if (!m_strcmp(p, "sec"));
117 else if (!m_strcmp(p, "ssb"))
119 else if (!m_strcmp(p, "uid"))
124 if (!(is_uid || (*is_subkey && option (OPTPGPIGNORESUB))))
125 k = pgp_new_keyinfo();
129 case 2: /* trust info */
131 switch (*p) { /* look only at the first letter */
133 flags |= KEYFLAG_EXPIRED;
136 flags |= KEYFLAG_REVOKED;
139 flags |= KEYFLAG_DISABLED;
155 if (!is_uid && !(*is_subkey && option (OPTPGPIGNORESUB)))
160 case 3: /* key length */
162 if (!(*is_subkey && option (OPTPGPIGNORESUB)))
163 k->keylen = atoi (p); /* fixme: add validation checks */
166 case 4: /* pubkey algo */
168 if (!(*is_subkey && option (OPTPGPIGNORESUB))) {
169 k->numalg = atoi (p);
170 k->algorithm = pgp_pkalgbytype (atoi (p));
174 case 5: /* 16 hex digits with the long keyid. */
176 if (!(*is_subkey && option (OPTPGPIGNORESUB)))
177 m_strreplace(&k->keyid, p);
181 case 6: /* timestamp (1998-02-28) */
190 st_time.tm_hour = 12;
191 m_strcpy(tstr, sizeof(tstr), p);
193 st_time.tm_year = atoi (tstr) - 1900;
195 st_time.tm_mon = (atoi (tstr + 5)) - 1;
196 st_time.tm_mday = atoi (tstr + 8);
197 k->gen_time = mutt_mktime (&st_time, 0);
200 case 7: /* valid for n days */
202 case 8: /* Local id */
204 case 9: /* ownertrust */
209 break; /* empty field or no trailing colon */
211 /* ignore user IDs on subkeys */
212 if (!is_uid && (*is_subkey && option (OPTPGPIGNORESUB)))
215 uid = p_new(pgp_uid_t, 1);
217 uid->addr = m_strdup(p);
221 uid->next = k->address;
224 if (strstr (p, "ENCR"))
225 k->flags |= KEYFLAG_PREFER_ENCRYPTION;
226 if (strstr (p, "SIGN"))
227 k->flags |= KEYFLAG_PREFER_SIGNING;
231 case 11: /* signature class */
233 case 12: /* key capabilities */
237 flags |= KEYFLAG_DISABLED;
241 flags |= KEYFLAG_CANENCRYPT;
245 flags |= KEYFLAG_CANSIGN;
250 if (!is_uid && (!*is_subkey || !option (OPTPGPIGNORESUB)
251 || !((flags & KEYFLAG_DISABLED)
252 || (flags & KEYFLAG_REVOKED)
253 || (flags & KEYFLAG_EXPIRED))))
265 pgp_key_t pgp_get_candidates (pgp_ring_t keyring, string_list_t * hints)
269 char buf[LONG_STRING];
270 pgp_key_t db = NULL, *kend, k = NULL, kk, mainkey = NULL;
274 if ((devnull = open ("/dev/null", O_RDWR)) == -1)
277 m_strreplace(&_chs, Charset);
279 thepid = pgp_invoke_list_keys (NULL, &fp, NULL, -1, -1, devnull,
288 while (fgets (buf, sizeof (buf) - 1, fp)) {
289 if (!(kk = parse_pub_line (buf, &is_sub, k)))
292 /* Only append kk to the list if it's new. */
301 k->flags |= KEYFLAG_SUBKEY;
303 for (l = &k->address; *l; l = &(*l)->next);
304 *l = pgp_copy_uids (mainkey->address, k);
312 mutt_perror ("fgets");
315 mutt_wait_filter (thepid);