2 * Copyright notice from original mutt:
3 * Copyright (C) 1997-2001 Thomas Roessler <roessler@does-not-exist.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
11 * This is a "simple" PGP key ring dumper.
13 * The output format is supposed to be compatible to the one GnuPG
14 * emits and Mutt expects.
16 * Note that the code of this program could be considerably less
17 * complex, but most of it was taken from mutt's second generation
20 * You can actually use this to put together some fairly general
21 * PGP key management applications.
29 #include <lib-lib/mem.h>
30 #include <lib-lib/str.h>
31 #include <lib-hash/hash.h>
46 short Umask; /* dirty hack because we need Umask in lib.c but don't want globals.h there */
50 #include "pgppacket.h"
52 #define MD5_DIGEST_LENGTH 16
55 #define FGETPOS(fp,pos) fgetpos((fp),&(pos))
56 #define FSETPOS(fp,pos) fsetpos((fp),&(pos))
58 #define FGETPOS(fp,pos) pos=ftello((fp));
59 #define FSETPOS(fp,pos) fseeko((fp),(pos),SEEK_SET)
63 static short dump_signatures = 0;
64 static short dump_fingerprints = 0;
67 static void pgpring_find_candidates (char *ringfile, const char *hints[],
69 static void pgpring_dump_keyblock (pgp_key_t p);
71 int main (int argc, char *const argv[])
78 const char *_kring = NULL;
79 char *env_pgppath, *env_home;
81 char pgppath[_POSIX_PATH_MAX];
82 char kring[_POSIX_PATH_MAX];
84 while ((c = getopt (argc, argv, "f25sk:S")) != EOF) {
94 dump_fingerprints = 1;
120 "usage: %s [-k <key ring> | [-2 | -5] [ -s] [-S] [-f]] [hints]\n",
128 m_strcpy(kring, sizeof(kring), _kring);
130 if ((env_pgppath = getenv ("PGPPATH")))
131 m_strcpy(pgppath, sizeof(pgppath), env_pgppath);
132 else if ((env_home = getenv ("HOME")))
133 snprintf (pgppath, sizeof (pgppath), "%s/.pgp", env_home);
135 fprintf (stderr, "%s: Can't determine your PGPPATH.\n", argv[0]);
140 snprintf (kring, sizeof (kring), "%s/secring.%s", pgppath,
141 version == 2 ? "pgp" : "skr");
143 snprintf (kring, sizeof (kring), "%s/pubring.%s", pgppath,
144 version == 2 ? "pgp" : "pkr");
147 pgpring_find_candidates (kring, (const char **) argv + optind,
154 /* The actual key ring parser */
156 static void pgp_make_pgp2_fingerprint (unsigned char *buff,
157 unsigned char *digest)
161 unsigned int size = 0;
166 size = (buff[0] << 8) + buff[1];
167 size = ((size + 7) / 8);
170 MD5Update (&context, buff, size);
173 size = (buff[0] << 8) + buff[1];
174 size = ((size + 7) / 8);
177 MD5Update (&context, buff, size);
179 MD5Final (digest, &context);
181 } /* pgp_make_pgp2_fingerprint() */
183 static pgp_key_t pgp_parse_pgp2_key (unsigned char *buff, size_t l)
187 unsigned char digest[MD5_DIGEST_LENGTH];
191 unsigned short exp_days = 0;
194 unsigned char scratch[LONG_STRING];
199 p = pgp_new_keyinfo ();
201 for (i = 0, j = 2; i < 4; i++)
202 gen_time = (gen_time << 8) + buff[j++];
204 p->gen_time = gen_time;
206 for (i = 0; i < 2; i++)
207 exp_days = (exp_days << 8) + buff[j++];
209 if (exp_days && time (NULL) > gen_time + exp_days * 24 * 3600)
210 p->flags |= KEYFLAG_EXPIRED;
215 p->algorithm = pgp_pkalgbytype (alg);
216 p->flags |= pgp_get_abilities (alg);
218 if (dump_fingerprints) {
219 /* j now points to the key material, which we need for the fingerprint */
220 p->fp_len = MD5_DIGEST_LENGTH;
221 pgp_make_pgp2_fingerprint (&buff[j], digest);
222 memcpy (p->fingerprint, digest, MD5_DIGEST_LENGTH);
224 else /* just to be usre */
225 p_clear(p->fingerprint, MD5_DIGEST_LENGTH);
228 for (i = 0; i < 2; i++)
229 expl = (expl << 8) + buff[j++];
233 expl = (expl + 7) / 8;
240 for (k = 0; k < 2; k++) {
241 for (id = 0, i = 0; i < 4; i++)
242 id = (id << 8) + buff[j++];
244 snprintf ((char *) scratch + k * 8, sizeof (scratch) - k * 8,
248 p->keyid = m_strdup((char *) scratch);
258 static void pgp_make_pgp3_fingerprint (unsigned char *buff, size_t l,
259 unsigned char *digest)
264 SHA1_Init (&context);
266 dummy = buff[0] & 0x3f;
268 if (dummy == PT_SUBSECKEY || dummy == PT_SUBKEY || dummy == PT_SECKEY)
271 dummy = (dummy << 2) | 0x81;
272 SHA1_Update (&context, &dummy, 1);
273 dummy = ((l - 1) >> 8) & 0xff;
274 SHA1_Update (&context, &dummy, 1);
275 dummy = (l - 1) & 0xff;
276 SHA1_Update (&context, &dummy, 1);
277 SHA1_Update (&context, buff + 1, l - 1);
278 SHA1_Final (digest, &context);
282 static void skip_bignum (unsigned char *buff, size_t l, size_t j,
283 size_t * toff, size_t n)
288 len = (buff[j] << 8) + buff[j + 1];
289 j += (len + 7) / 8 + 2;
291 while (j <= l && --n > 0);
298 static pgp_key_t pgp_parse_pgp3_key (unsigned char *buff, size_t l)
302 unsigned char digest[SHA_DIGEST_LENGTH];
303 unsigned char scratch[LONG_STRING];
310 p = pgp_new_keyinfo ();
313 for (i = 0; i < 4; i++)
314 gen_time = (gen_time << 8) + buff[j++];
316 p->gen_time = gen_time;
321 p->algorithm = pgp_pkalgbytype (alg);
322 p->flags |= pgp_get_abilities (alg);
325 skip_bignum (buff, l, j, &j, 3);
326 else if (alg == 16 || alg == 20)
327 skip_bignum (buff, l, j, &j, 2);
329 len = (buff[j] << 8) + buff[j + 1];
333 if (alg >= 1 && alg <= 3)
334 skip_bignum (buff, l, j, &j, 2);
335 else if (alg == 17 || alg == 16 || alg == 20)
336 skip_bignum (buff, l, j, &j, 1);
338 pgp_make_pgp3_fingerprint (buff, j, digest);
339 p->fp_len = SHA_DIGEST_LENGTH;
341 for (k = 0; k < 2; k++) {
342 for (id = 0, i = SHA_DIGEST_LENGTH - 8 + k * 4;
343 i < SHA_DIGEST_LENGTH + (k - 1) * 4; i++)
344 id = (id << 8) + digest[i];
346 snprintf ((char *) scratch + k * 8, sizeof (scratch) - k * 8, "%08lX",
350 p->keyid = m_strdup((char *) scratch);
355 static pgp_key_t pgp_parse_keyinfo (unsigned char *buff, size_t l)
363 return pgp_parse_pgp2_key (buff, l);
365 return pgp_parse_pgp3_key (buff, l);
371 static int pgp_parse_pgp2_sig (unsigned char *buff, size_t l,
372 pgp_key_t p, pgp_sig_t * s)
374 unsigned char sigtype;
376 unsigned long signerid1;
377 unsigned long signerid2;
388 for (i = 0; i < 4; i++)
389 sig_gen_time = (sig_gen_time << 8) + buff[j++];
391 signerid1 = signerid2 = 0;
392 for (i = 0; i < 4; i++)
393 signerid1 = (signerid1 << 8) + buff[j++];
395 for (i = 0; i < 4; i++)
396 signerid2 = (signerid2 << 8) + buff[j++];
399 if (sigtype == 0x20 || sigtype == 0x28)
400 p->flags |= KEYFLAG_REVOKED;
403 s->sigtype = sigtype;
411 static int pgp_parse_pgp3_sig (unsigned char *buff, size_t l,
412 pgp_key_t p, pgp_sig_t * s)
414 unsigned char sigtype;
416 unsigned char hashalg;
418 time_t sig_gen_time = -1;
420 long key_validity = -1;
421 unsigned long signerid1 = 0;
422 unsigned long signerid2 = 0;
427 short have_critical_spks = 0;
438 for (ii = 0; ii < 2; ii++) {
442 ml = (buff[j] << 8) + buff[j + 1];
456 skl = (skl - 192) * 256 + buff[j++] + 192;
461 if ((int) ml - (int) skl < 0)
468 switch (skt & 0x7f) {
469 case 2: /* creation time */
474 for (i = 0; i < 4; i++)
475 sig_gen_time = (sig_gen_time << 8) + buff[j++];
479 case 3: /* expiration time */
484 for (i = 0; i < 4; i++)
485 validity = (validity << 8) + buff[j++];
488 case 9: /* key expiration time */
493 for (i = 0; i < 4; i++)
494 key_validity = (key_validity << 8) + buff[j++];
497 case 16: /* issuer key ID */
501 signerid2 = signerid1 = 0;
502 for (i = 0; i < 4; i++)
503 signerid1 = (signerid1 << 8) + buff[j++];
504 for (i = 0; i < 4; i++)
505 signerid2 = (signerid2 << 8) + buff[j++];
509 case 10: /* CMR key */
511 case 4: /* exportable */
514 case 7: /* revocable */
515 case 11: /* Pref. symm. alg. */
516 case 12: /* revocation key */
517 case 20: /* notation data */
518 case 21: /* pref. hash */
519 case 22: /* pref. comp.alg. */
520 case 23: /* key server prefs. */
521 case 24: /* pref. key server */
525 have_critical_spks = 1;
532 if (sigtype == 0x20 || sigtype == 0x28)
533 p->flags |= KEYFLAG_REVOKED;
534 if (key_validity != -1 && time (NULL) > p->gen_time + key_validity)
535 p->flags |= KEYFLAG_EXPIRED;
536 if (have_critical_spks)
537 p->flags |= KEYFLAG_CRITICAL;
540 s->sigtype = sigtype;
551 static int pgp_parse_sig (unsigned char *buff, size_t l,
552 pgp_key_t p, pgp_sig_t * sig)
554 if (!buff || l < 2 || !p)
560 return pgp_parse_pgp2_sig (buff, l, p, sig);
562 return pgp_parse_pgp3_sig (buff, l, p, sig);
568 /* parse one key block, including all subkeys. */
570 static pgp_key_t pgp_parse_keyblock (FILE * fp)
573 unsigned char pt = 0;
574 unsigned char last_pt;
584 pgp_key_t root = NULL;
585 pgp_key_t *last = &root;
587 pgp_uid_t *uid = NULL;
588 pgp_uid_t **addr = NULL;
589 pgp_sig_t **lsig = NULL;
593 while (!err && (buff = pgp_read_packet (fp, &l)) != NULL) {
597 /* check if we have read the complete key block. */
599 if ((pt == PT_SECKEY || pt == PT_PUBKEY) && root) {
610 if (!(*last = p = pgp_parse_keyinfo (buff, l))) {
619 if (pt == PT_SUBKEY || pt == PT_SUBSECKEY) {
620 p->flags |= KEYFLAG_SUBKEY;
623 p->address = pgp_copy_uids (root->address, p);
625 addr = &(*addr)->next;
629 if (pt == PT_SECKEY || pt == PT_SUBSECKEY)
630 p->flags |= KEYFLAG_SECRET;
638 pgp_sig_t *signature = p_new(pgp_sig_t, 1);
641 lsig = &signature->next;
643 pgp_parse_sig (buff, l, p, signature);
650 if (p && (last_pt == PT_SECKEY || last_pt == PT_PUBKEY ||
651 last_pt == PT_SUBKEY || last_pt == PT_SUBSECKEY)) {
652 if (buff[1] & 0x20) {
653 p->flags |= KEYFLAG_DISABLED;
656 else if (last_pt == PT_NAME && uid) {
657 uid->trust = buff[1];
669 chr = p_dupstr(buff + 1, l - 1);
671 *addr = uid = p_new(pgp_uid_t, 1); /* XXX */
678 /* the following tags are generated by
682 if (strstr (chr, "ENCR"))
683 p->flags |= KEYFLAG_PREFER_ENCRYPTION;
684 if (strstr (chr, "SIGN"))
685 p->flags |= KEYFLAG_PREFER_SIGNING;
695 pgp_free_key (&root);
700 static int pgpring_string_matches_hint (const char *s, const char *hints[],
705 if (!hints || !nhints)
708 for (i = 0; i < nhints; i++) {
709 if (str_isstr (s, hints[i]) != NULL)
717 * Go through the key ring file and look for keys with
721 static void pgpring_find_candidates (char *ringfile, const char *hints[],
732 unsigned char *buff = NULL;
733 unsigned char pt = 0;
738 if ((rfp = fopen (ringfile, "r")) == NULL) {
740 size_t error_buf_len;
742 error_buf_len = sizeof ("fopen: ") - 1 + m_strlen(ringfile) + 1;
743 error_buf = p_new(char, error_buf_len);
744 snprintf (error_buf, error_buf_len, "fopen: %s", ringfile);
746 p_delete(&error_buf);
751 FGETPOS (rfp, keypos);
753 while (!err && (buff = pgp_read_packet (rfp, &l)) != NULL) {
759 if ((pt == PT_SECKEY) || (pt == PT_PUBKEY)) {
762 else if (pt == PT_NAME) {
763 char *tmp = p_dupstr(buff + 1, l - 1);
765 /* mutt_decode_utf8_string (tmp, chs); */
767 if (pgpring_string_matches_hint (tmp, hints, nhints)) {
770 FSETPOS (rfp, keypos);
772 /* Not bailing out here would lead us into an endless loop. */
774 if ((p = pgp_parse_keyblock (rfp)) == NULL)
777 pgpring_dump_keyblock (p);
791 static void print_userid (const char *id)
793 for (; id && *id; id++) {
794 if (*id >= ' ' && *id <= 'z' && *id != ':')
797 printf ("\\x%02x", (*id) & 0xff);
801 static void print_fingerprint (pgp_key_t p)
805 printf ("fpr:::::::::");
806 for (i = 0; i < p->fp_len; i++)
807 printf ("%02X", p->fingerprint[i]);
810 } /* print_fingerprint() */
813 static void pgpring_dump_signatures (pgp_sig_t * sig)
815 for (; sig; sig = sig->next) {
816 if (sig->sigtype == 0x10 || sig->sigtype == 0x11 ||
817 sig->sigtype == 0x12 || sig->sigtype == 0x13)
818 printf ("sig::::%08lX%08lX::::::%X:\n",
819 sig->sid1, sig->sid2, sig->sigtype);
820 else if (sig->sigtype == 0x20)
821 printf ("rev::::%08lX%08lX::::::%X:\n",
822 sig->sid1, sig->sid2, sig->sigtype);
827 static char gnupg_trustletter (int t)
840 static void pgpring_dump_keyblock (pgp_key_t p)
847 for (; p; p = p->next) {
850 if (p->flags & KEYFLAG_SECRET) {
851 if (p->flags & KEYFLAG_SUBKEY)
857 if (p->flags & KEYFLAG_SUBKEY)
863 if (p->flags & KEYFLAG_REVOKED)
865 if (p->flags & KEYFLAG_EXPIRED)
867 if (p->flags & KEYFLAG_DISABLED)
870 for (uid = p->address; uid; uid = uid->next, first = 0) {
872 printf ("uid:%c::::::::", gnupg_trustletter (uid->trust));
873 print_userid (uid->addr);
877 if (p->flags & KEYFLAG_SECRET)
880 putchar (gnupg_trustletter (uid->trust));
885 printf (":%d:%d:%s:%04d-%02d-%02d::::", p->keylen, p->numalg,
886 p->keyid, 1900 + tp->tm_year, tp->tm_mon + 1, tp->tm_mday);
888 print_userid (uid->addr);
891 if (pgp_canencrypt (p->numalg))
893 if (pgp_cansign (p->numalg))
895 if (p->flags & KEYFLAG_DISABLED)
899 if (dump_fingerprints)
900 print_fingerprint (p);
903 if (dump_signatures) {
905 pgpring_dump_signatures (p->sigs);
906 pgpring_dump_signatures (uid->sigs);
913 * The mutt_gettext () defined in gettext.c requires iconv,
914 * so we do without charset conversion here.
917 char *mutt_gettext (const char *message)
919 return (char *) message;