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>
46 short Umask; /* dirty hack because we need Umask in lib.c but don't want globals.h there */
52 #include "pgppacket.h"
54 #define MD5_DIGEST_LENGTH 16
57 #define FGETPOS(fp,pos) fgetpos((fp),&(pos))
58 #define FSETPOS(fp,pos) fsetpos((fp),&(pos))
60 #define FGETPOS(fp,pos) pos=ftello((fp));
61 #define FSETPOS(fp,pos) fseeko((fp),(pos),SEEK_SET)
65 static short dump_signatures = 0;
66 static short dump_fingerprints = 0;
69 static void pgpring_find_candidates (char *ringfile, const char *hints[],
71 static void pgpring_dump_keyblock (pgp_key_t p);
73 int main (int argc, char *const argv[])
80 const char *_kring = NULL;
81 char *env_pgppath, *env_home;
83 char pgppath[_POSIX_PATH_MAX];
84 char kring[_POSIX_PATH_MAX];
86 while ((c = getopt (argc, argv, "f25sk:S")) != EOF) {
96 dump_fingerprints = 1;
122 "usage: %s [-k <key ring> | [-2 | -5] [ -s] [-S] [-f]] [hints]\n",
130 strfcpy (kring, _kring, sizeof (kring));
132 if ((env_pgppath = getenv ("PGPPATH")))
133 strfcpy (pgppath, env_pgppath, sizeof (pgppath));
134 else if ((env_home = getenv ("HOME")))
135 snprintf (pgppath, sizeof (pgppath), "%s/.pgp", env_home);
137 fprintf (stderr, "%s: Can't determine your PGPPATH.\n", argv[0]);
142 snprintf (kring, sizeof (kring), "%s/secring.%s", pgppath,
143 version == 2 ? "pgp" : "skr");
145 snprintf (kring, sizeof (kring), "%s/pubring.%s", pgppath,
146 version == 2 ? "pgp" : "pkr");
149 pgpring_find_candidates (kring, (const char **) argv + optind,
156 /* The actual key ring parser */
158 static void pgp_make_pgp2_fingerprint (unsigned char *buff,
159 unsigned char *digest)
163 unsigned int size = 0;
168 size = (buff[0] << 8) + buff[1];
169 size = ((size + 7) / 8);
172 MD5Update (&context, buff, size);
175 size = (buff[0] << 8) + buff[1];
176 size = ((size + 7) / 8);
179 MD5Update (&context, buff, size);
181 MD5Final (digest, &context);
183 } /* pgp_make_pgp2_fingerprint() */
185 static pgp_key_t pgp_parse_pgp2_key (unsigned char *buff, size_t l)
189 unsigned char digest[MD5_DIGEST_LENGTH];
193 unsigned short exp_days = 0;
196 unsigned char scratch[LONG_STRING];
201 p = pgp_new_keyinfo ();
203 for (i = 0, j = 2; i < 4; i++)
204 gen_time = (gen_time << 8) + buff[j++];
206 p->gen_time = gen_time;
208 for (i = 0; i < 2; i++)
209 exp_days = (exp_days << 8) + buff[j++];
211 if (exp_days && time (NULL) > gen_time + exp_days * 24 * 3600)
212 p->flags |= KEYFLAG_EXPIRED;
217 p->algorithm = pgp_pkalgbytype (alg);
218 p->flags |= pgp_get_abilities (alg);
220 if (dump_fingerprints) {
221 /* j now points to the key material, which we need for the fingerprint */
222 p->fp_len = MD5_DIGEST_LENGTH;
223 pgp_make_pgp2_fingerprint (&buff[j], digest);
224 memcpy (p->fingerprint, digest, MD5_DIGEST_LENGTH);
226 else /* just to be usre */
227 memset (p->fingerprint, 0, MD5_DIGEST_LENGTH);
230 for (i = 0; i < 2; i++)
231 expl = (expl << 8) + buff[j++];
235 expl = (expl + 7) / 8;
242 for (k = 0; k < 2; k++) {
243 for (id = 0, i = 0; i < 4; i++)
244 id = (id << 8) + buff[j++];
246 snprintf ((char *) scratch + k * 8, sizeof (scratch) - k * 8,
250 p->keyid = str_dup ((char *) scratch);
260 static void pgp_make_pgp3_fingerprint (unsigned char *buff, size_t l,
261 unsigned char *digest)
266 SHA1_Init (&context);
268 dummy = buff[0] & 0x3f;
270 if (dummy == PT_SUBSECKEY || dummy == PT_SUBKEY || dummy == PT_SECKEY)
273 dummy = (dummy << 2) | 0x81;
274 SHA1_Update (&context, &dummy, 1);
275 dummy = ((l - 1) >> 8) & 0xff;
276 SHA1_Update (&context, &dummy, 1);
277 dummy = (l - 1) & 0xff;
278 SHA1_Update (&context, &dummy, 1);
279 SHA1_Update (&context, buff + 1, l - 1);
280 SHA1_Final (digest, &context);
284 static void skip_bignum (unsigned char *buff, size_t l, size_t j,
285 size_t * toff, size_t n)
290 len = (buff[j] << 8) + buff[j + 1];
291 j += (len + 7) / 8 + 2;
293 while (j <= l && --n > 0);
300 static pgp_key_t pgp_parse_pgp3_key (unsigned char *buff, size_t l)
304 unsigned char digest[SHA_DIGEST_LENGTH];
305 unsigned char scratch[LONG_STRING];
312 p = pgp_new_keyinfo ();
315 for (i = 0; i < 4; i++)
316 gen_time = (gen_time << 8) + buff[j++];
318 p->gen_time = gen_time;
323 p->algorithm = pgp_pkalgbytype (alg);
324 p->flags |= pgp_get_abilities (alg);
327 skip_bignum (buff, l, j, &j, 3);
328 else if (alg == 16 || alg == 20)
329 skip_bignum (buff, l, j, &j, 2);
331 len = (buff[j] << 8) + buff[j + 1];
335 if (alg >= 1 && alg <= 3)
336 skip_bignum (buff, l, j, &j, 2);
337 else if (alg == 17 || alg == 16 || alg == 20)
338 skip_bignum (buff, l, j, &j, 1);
340 pgp_make_pgp3_fingerprint (buff, j, digest);
341 p->fp_len = SHA_DIGEST_LENGTH;
343 for (k = 0; k < 2; k++) {
344 for (id = 0, i = SHA_DIGEST_LENGTH - 8 + k * 4;
345 i < SHA_DIGEST_LENGTH + (k - 1) * 4; i++)
346 id = (id << 8) + digest[i];
348 snprintf ((char *) scratch + k * 8, sizeof (scratch) - k * 8, "%08lX",
352 p->keyid = str_dup ((char *) scratch);
357 static pgp_key_t pgp_parse_keyinfo (unsigned char *buff, size_t l)
365 return pgp_parse_pgp2_key (buff, l);
367 return pgp_parse_pgp3_key (buff, l);
373 static int pgp_parse_pgp2_sig (unsigned char *buff, size_t l,
374 pgp_key_t p, pgp_sig_t * s)
376 unsigned char sigtype;
378 unsigned long signerid1;
379 unsigned long signerid2;
390 for (i = 0; i < 4; i++)
391 sig_gen_time = (sig_gen_time << 8) + buff[j++];
393 signerid1 = signerid2 = 0;
394 for (i = 0; i < 4; i++)
395 signerid1 = (signerid1 << 8) + buff[j++];
397 for (i = 0; i < 4; i++)
398 signerid2 = (signerid2 << 8) + buff[j++];
401 if (sigtype == 0x20 || sigtype == 0x28)
402 p->flags |= KEYFLAG_REVOKED;
405 s->sigtype = sigtype;
413 static int pgp_parse_pgp3_sig (unsigned char *buff, size_t l,
414 pgp_key_t p, pgp_sig_t * s)
416 unsigned char sigtype;
418 unsigned char hashalg;
420 time_t sig_gen_time = -1;
422 long key_validity = -1;
423 unsigned long signerid1 = 0;
424 unsigned long signerid2 = 0;
429 short have_critical_spks = 0;
440 for (ii = 0; ii < 2; ii++) {
444 ml = (buff[j] << 8) + buff[j + 1];
458 skl = (skl - 192) * 256 + buff[j++] + 192;
463 if ((int) ml - (int) skl < 0)
470 switch (skt & 0x7f) {
471 case 2: /* creation time */
476 for (i = 0; i < 4; i++)
477 sig_gen_time = (sig_gen_time << 8) + buff[j++];
481 case 3: /* expiration time */
486 for (i = 0; i < 4; i++)
487 validity = (validity << 8) + buff[j++];
490 case 9: /* key expiration time */
495 for (i = 0; i < 4; i++)
496 key_validity = (key_validity << 8) + buff[j++];
499 case 16: /* issuer key ID */
503 signerid2 = signerid1 = 0;
504 for (i = 0; i < 4; i++)
505 signerid1 = (signerid1 << 8) + buff[j++];
506 for (i = 0; i < 4; i++)
507 signerid2 = (signerid2 << 8) + buff[j++];
511 case 10: /* CMR key */
513 case 4: /* exportable */
516 case 7: /* revocable */
517 case 11: /* Pref. symm. alg. */
518 case 12: /* revocation key */
519 case 20: /* notation data */
520 case 21: /* pref. hash */
521 case 22: /* pref. comp.alg. */
522 case 23: /* key server prefs. */
523 case 24: /* pref. key server */
527 have_critical_spks = 1;
534 if (sigtype == 0x20 || sigtype == 0x28)
535 p->flags |= KEYFLAG_REVOKED;
536 if (key_validity != -1 && time (NULL) > p->gen_time + key_validity)
537 p->flags |= KEYFLAG_EXPIRED;
538 if (have_critical_spks)
539 p->flags |= KEYFLAG_CRITICAL;
542 s->sigtype = sigtype;
553 static int pgp_parse_sig (unsigned char *buff, size_t l,
554 pgp_key_t p, pgp_sig_t * sig)
556 if (!buff || l < 2 || !p)
562 return pgp_parse_pgp2_sig (buff, l, p, sig);
564 return pgp_parse_pgp3_sig (buff, l, p, sig);
570 /* parse one key block, including all subkeys. */
572 static pgp_key_t pgp_parse_keyblock (FILE * fp)
575 unsigned char pt = 0;
576 unsigned char last_pt;
586 pgp_key_t root = NULL;
587 pgp_key_t *last = &root;
589 pgp_uid_t *uid = NULL;
590 pgp_uid_t **addr = NULL;
591 pgp_sig_t **lsig = NULL;
595 while (!err && (buff = pgp_read_packet (fp, &l)) != NULL) {
599 /* check if we have read the complete key block. */
601 if ((pt == PT_SECKEY || pt == PT_PUBKEY) && root) {
612 if (!(*last = p = pgp_parse_keyinfo (buff, l))) {
621 if (pt == PT_SUBKEY || pt == PT_SUBSECKEY) {
622 p->flags |= KEYFLAG_SUBKEY;
625 p->address = pgp_copy_uids (root->address, p);
627 addr = &(*addr)->next;
631 if (pt == PT_SECKEY || pt == PT_SUBSECKEY)
632 p->flags |= KEYFLAG_SECRET;
640 pgp_sig_t *signature = p_new(pgp_sig_t, 1);
643 lsig = &signature->next;
645 pgp_parse_sig (buff, l, p, signature);
652 if (p && (last_pt == PT_SECKEY || last_pt == PT_PUBKEY ||
653 last_pt == PT_SUBKEY || last_pt == PT_SUBSECKEY)) {
654 if (buff[1] & 0x20) {
655 p->flags |= KEYFLAG_DISABLED;
658 else if (last_pt == PT_NAME && uid) {
659 uid->trust = buff[1];
671 chr = p_dupstr(buff + 1, l - 1);
673 *addr = uid = p_new(pgp_uid_t, 1); /* XXX */
680 /* the following tags are generated by
684 if (strstr (chr, "ENCR"))
685 p->flags |= KEYFLAG_PREFER_ENCRYPTION;
686 if (strstr (chr, "SIGN"))
687 p->flags |= KEYFLAG_PREFER_SIGNING;
697 pgp_free_key (&root);
702 static int pgpring_string_matches_hint (const char *s, const char *hints[],
707 if (!hints || !nhints)
710 for (i = 0; i < nhints; i++) {
711 if (str_isstr (s, hints[i]) != NULL)
719 * Go through the key ring file and look for keys with
723 static void pgpring_find_candidates (char *ringfile, const char *hints[],
734 unsigned char *buff = NULL;
735 unsigned char pt = 0;
740 if ((rfp = fopen (ringfile, "r")) == NULL) {
742 size_t error_buf_len;
744 error_buf_len = sizeof ("fopen: ") - 1 + str_len (ringfile) + 1;
745 error_buf = p_new(char, error_buf_len);
746 snprintf (error_buf, error_buf_len, "fopen: %s", ringfile);
748 p_delete(&error_buf);
753 FGETPOS (rfp, keypos);
755 while (!err && (buff = pgp_read_packet (rfp, &l)) != NULL) {
761 if ((pt == PT_SECKEY) || (pt == PT_PUBKEY)) {
764 else if (pt == PT_NAME) {
765 char *tmp = p_dupstr(buff + 1, l - 1);
767 /* mutt_decode_utf8_string (tmp, chs); */
769 if (pgpring_string_matches_hint (tmp, hints, nhints)) {
772 FSETPOS (rfp, keypos);
774 /* Not bailing out here would lead us into an endless loop. */
776 if ((p = pgp_parse_keyblock (rfp)) == NULL)
779 pgpring_dump_keyblock (p);
793 static void print_userid (const char *id)
795 for (; id && *id; id++) {
796 if (*id >= ' ' && *id <= 'z' && *id != ':')
799 printf ("\\x%02x", (*id) & 0xff);
803 static void print_fingerprint (pgp_key_t p)
807 printf ("fpr:::::::::");
808 for (i = 0; i < p->fp_len; i++)
809 printf ("%02X", p->fingerprint[i]);
812 } /* print_fingerprint() */
815 static void pgpring_dump_signatures (pgp_sig_t * sig)
817 for (; sig; sig = sig->next) {
818 if (sig->sigtype == 0x10 || sig->sigtype == 0x11 ||
819 sig->sigtype == 0x12 || sig->sigtype == 0x13)
820 printf ("sig::::%08lX%08lX::::::%X:\n",
821 sig->sid1, sig->sid2, sig->sigtype);
822 else if (sig->sigtype == 0x20)
823 printf ("rev::::%08lX%08lX::::::%X:\n",
824 sig->sid1, sig->sid2, sig->sigtype);
829 static char gnupg_trustletter (int t)
842 static void pgpring_dump_keyblock (pgp_key_t p)
849 for (; p; p = p->next) {
852 if (p->flags & KEYFLAG_SECRET) {
853 if (p->flags & KEYFLAG_SUBKEY)
859 if (p->flags & KEYFLAG_SUBKEY)
865 if (p->flags & KEYFLAG_REVOKED)
867 if (p->flags & KEYFLAG_EXPIRED)
869 if (p->flags & KEYFLAG_DISABLED)
872 for (uid = p->address; uid; uid = uid->next, first = 0) {
874 printf ("uid:%c::::::::", gnupg_trustletter (uid->trust));
875 print_userid (uid->addr);
879 if (p->flags & KEYFLAG_SECRET)
882 putchar (gnupg_trustletter (uid->trust));
887 printf (":%d:%d:%s:%04d-%02d-%02d::::", p->keylen, p->numalg,
888 p->keyid, 1900 + tp->tm_year, tp->tm_mon + 1, tp->tm_mday);
890 print_userid (uid->addr);
893 if (pgp_canencrypt (p->numalg))
895 if (pgp_cansign (p->numalg))
897 if (p->flags & KEYFLAG_DISABLED)
901 if (dump_fingerprints)
902 print_fingerprint (p);
905 if (dump_signatures) {
907 pgpring_dump_signatures (p->sigs);
908 pgpring_dump_signatures (uid->sigs);
915 * The mutt_gettext () defined in gettext.c requires iconv,
916 * so we do without charset conversion here.
919 char *mutt_gettext (const char *message)
921 return (char *) message;