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>
31 * Read the GNUPG keys. For now we read the complete keyring by
32 * calling gnupg in a special mode.
34 * The output format of gpgm is colon delimited with these fields:
35 * - record type ("pub","uid","sig","rev" etc.)
39 * - 16 hex digits with the long keyid.
40 * - timestamp (1998-02-28)
47 /* decode the backslash-escaped user ids. */
49 static char *_chs = 0;
51 static void fix_uid (char *uid)
56 for (s = d = uid; *s;) {
57 if (*s == '\\' && *(s + 1) == 'x' && isxdigit ((unsigned char) *(s + 2))
58 && isxdigit ((unsigned char) *(s + 3))) {
59 *d++ = hexval (*(s + 2)) << 4 | hexval (*(s + 3));
67 if (_chs && (cd = mutt_iconv_open (_chs, "utf-8", 0)) != MUTT_ICONV_ERROR) {
68 int n = s - uid + 1; /* chars available in original buffer */
74 buf = p_new(char, n + 1);
75 ib = uid, ibl = d - uid + 1, ob = buf, obl = n;
76 my_iconv(cd, &ib, &ibl, &ob, &obl);
79 memcpy (uid, buf, ob - buf);
82 else if (ob - buf == n && (buf[n] = 0, m_strlen(buf) < n))
90 static pgp_key_t parse_pub_line (char *buf, int *is_subkey, pgp_key_t k)
92 pgp_uid_t *uid = NULL;
93 int field = 0, is_uid = 0;
102 for (p = buf; p; p = pend) {
103 if ((pend = strchr (p, ':')))
106 if (field > 1 && !*p)
110 case 1: /* record type */
112 if (!m_strcmp(p, "pub"));
113 else if (!m_strcmp(p, "sub"))
115 else if (!m_strcmp(p, "sec"));
116 else if (!m_strcmp(p, "ssb"))
118 else if (!m_strcmp(p, "uid"))
123 if (!(is_uid || (*is_subkey && option (OPTPGPIGNORESUB))))
124 k = pgp_new_keyinfo();
128 case 2: /* trust info */
130 switch (*p) { /* look only at the first letter */
132 flags |= KEYFLAG_EXPIRED;
135 flags |= KEYFLAG_REVOKED;
138 flags |= KEYFLAG_DISABLED;
154 if (!is_uid && !(*is_subkey && option (OPTPGPIGNORESUB)))
159 case 3: /* key length */
161 if (!(*is_subkey && option (OPTPGPIGNORESUB)))
162 k->keylen = atoi (p); /* fixme: add validation checks */
165 case 4: /* pubkey algo */
167 if (!(*is_subkey && option (OPTPGPIGNORESUB))) {
168 k->numalg = atoi (p);
169 k->algorithm = pgp_pkalgbytype (atoi (p));
173 case 5: /* 16 hex digits with the long keyid. */
175 if (!(*is_subkey && option (OPTPGPIGNORESUB)))
176 m_strreplace(&k->keyid, p);
180 case 6: /* timestamp (1998-02-28) */
189 st_time.tm_hour = 12;
190 m_strcpy(tstr, sizeof(tstr), p);
192 st_time.tm_year = atoi (tstr) - 1900;
194 st_time.tm_mon = (atoi (tstr + 5)) - 1;
195 st_time.tm_mday = atoi (tstr + 8);
196 k->gen_time = mutt_mktime (&st_time, 0);
199 case 7: /* valid for n days */
201 case 8: /* Local id */
203 case 9: /* ownertrust */
208 break; /* empty field or no trailing colon */
210 /* ignore user IDs on subkeys */
211 if (!is_uid && (*is_subkey && option (OPTPGPIGNORESUB)))
214 uid = p_new(pgp_uid_t, 1);
216 uid->addr = m_strdup(p);
220 uid->next = k->address;
223 if (strstr (p, "ENCR"))
224 k->flags |= KEYFLAG_PREFER_ENCRYPTION;
225 if (strstr (p, "SIGN"))
226 k->flags |= KEYFLAG_PREFER_SIGNING;
230 case 11: /* signature class */
232 case 12: /* key capabilities */
236 flags |= KEYFLAG_DISABLED;
240 flags |= KEYFLAG_CANENCRYPT;
244 flags |= KEYFLAG_CANSIGN;
249 if (!is_uid && (!*is_subkey || !option (OPTPGPIGNORESUB)
250 || !((flags & KEYFLAG_DISABLED)
251 || (flags & KEYFLAG_REVOKED)
252 || (flags & KEYFLAG_EXPIRED))))
264 pgp_key_t pgp_get_candidates (pgp_ring_t keyring, string_list_t * hints)
268 char buf[LONG_STRING];
269 pgp_key_t db = NULL, *kend, k = NULL, kk, mainkey = NULL;
273 if ((devnull = open ("/dev/null", O_RDWR)) == -1)
276 m_strreplace(&_chs, Charset);
278 thepid = pgp_invoke_list_keys (NULL, &fp, NULL, -1, -1, devnull,
287 while (fgets (buf, sizeof (buf) - 1, fp)) {
288 if (!(kk = parse_pub_line (buf, &is_sub, k)))
291 /* Only append kk to the list if it's new. */
300 k->flags |= KEYFLAG_SUBKEY;
302 for (l = &k->address; *l; l = &(*l)->next);
303 *l = pgp_copy_uids (mainkey->address, k);
311 mutt_perror ("fgets");
314 mutt_wait_filter (thepid);