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.
45 short Umask; /* dirty hack because we need Umask in lib.c but don't want globals.h there */
51 #include "pgppacket.h"
53 #define MD5_DIGEST_LENGTH 16
56 #define FGETPOS(fp,pos) fgetpos((fp),&(pos))
57 #define FSETPOS(fp,pos) fsetpos((fp),&(pos))
59 #define FGETPOS(fp,pos) pos=ftell((fp));
60 #define FSETPOS(fp,pos) fseek((fp),(pos),SEEK_SET)
64 static short dump_signatures = 0;
65 static short dump_fingerprints = 0;
68 static void pgpring_find_candidates (char *ringfile, const char *hints[],
70 static void pgpring_dump_keyblock (pgp_key_t p);
72 int main (int argc, char *const argv[])
79 const char *_kring = NULL;
80 char *env_pgppath, *env_home;
82 char pgppath[_POSIX_PATH_MAX];
83 char kring[_POSIX_PATH_MAX];
85 while ((c = getopt (argc, argv, "f25sk:S")) != EOF) {
95 dump_fingerprints = 1;
121 "usage: %s [-k <key ring> | [-2 | -5] [ -s] [-S] [-f]] [hints]\n",
129 strfcpy (kring, _kring, sizeof (kring));
131 if ((env_pgppath = getenv ("PGPPATH")))
132 strfcpy (pgppath, env_pgppath, sizeof (pgppath));
133 else if ((env_home = getenv ("HOME")))
134 snprintf (pgppath, sizeof (pgppath), "%s/.pgp", env_home);
136 fprintf (stderr, "%s: Can't determine your PGPPATH.\n", argv[0]);
141 snprintf (kring, sizeof (kring), "%s/secring.%s", pgppath,
142 version == 2 ? "pgp" : "skr");
144 snprintf (kring, sizeof (kring), "%s/pubring.%s", pgppath,
145 version == 2 ? "pgp" : "pkr");
148 pgpring_find_candidates (kring, (const char **) argv + optind,
155 /* The actual key ring parser */
157 static void pgp_make_pgp2_fingerprint (unsigned char *buff,
158 unsigned char *digest)
162 unsigned int size = 0;
167 size = (buff[0] << 8) + buff[1];
168 size = ((size + 7) / 8);
171 MD5Update (&context, buff, size);
174 size = (buff[0] << 8) + buff[1];
175 size = ((size + 7) / 8);
178 MD5Update (&context, buff, size);
180 MD5Final (digest, &context);
182 } /* pgp_make_pgp2_fingerprint() */
184 static pgp_key_t pgp_parse_pgp2_key (unsigned char *buff, size_t l)
188 unsigned char digest[MD5_DIGEST_LENGTH];
192 unsigned short exp_days = 0;
195 unsigned char scratch[LONG_STRING];
200 p = pgp_new_keyinfo ();
202 for (i = 0, j = 2; i < 4; i++)
203 gen_time = (gen_time << 8) + buff[j++];
205 p->gen_time = gen_time;
207 for (i = 0; i < 2; i++)
208 exp_days = (exp_days << 8) + buff[j++];
210 if (exp_days && time (NULL) > gen_time + exp_days * 24 * 3600)
211 p->flags |= KEYFLAG_EXPIRED;
216 p->algorithm = pgp_pkalgbytype (alg);
217 p->flags |= pgp_get_abilities (alg);
219 if (dump_fingerprints) {
220 /* j now points to the key material, which we need for the fingerprint */
221 p->fp_len = MD5_DIGEST_LENGTH;
222 pgp_make_pgp2_fingerprint (&buff[j], digest);
223 memcpy (p->fingerprint, digest, MD5_DIGEST_LENGTH);
225 else /* just to be usre */
226 memset (p->fingerprint, 0, MD5_DIGEST_LENGTH);
229 for (i = 0; i < 2; i++)
230 expl = (expl << 8) + buff[j++];
234 expl = (expl + 7) / 8;
241 for (k = 0; k < 2; k++) {
242 for (id = 0, i = 0; i < 4; i++)
243 id = (id << 8) + buff[j++];
245 snprintf ((char *) scratch + k * 8, sizeof (scratch) - k * 8,
249 p->keyid = str_dup ((char *) scratch);
259 static void pgp_make_pgp3_fingerprint (unsigned char *buff, size_t l,
260 unsigned char *digest)
265 SHA1_Init (&context);
267 dummy = buff[0] & 0x3f;
269 if (dummy == PT_SUBSECKEY || dummy == PT_SUBKEY || dummy == PT_SECKEY)
272 dummy = (dummy << 2) | 0x81;
273 SHA1_Update (&context, &dummy, 1);
274 dummy = ((l - 1) >> 8) & 0xff;
275 SHA1_Update (&context, &dummy, 1);
276 dummy = (l - 1) & 0xff;
277 SHA1_Update (&context, &dummy, 1);
278 SHA1_Update (&context, buff + 1, l - 1);
279 SHA1_Final (digest, &context);
283 static void skip_bignum (unsigned char *buff, size_t l, size_t j,
284 size_t * toff, size_t n)
289 len = (buff[j] << 8) + buff[j + 1];
290 j += (len + 7) / 8 + 2;
292 while (j <= l && --n > 0);
299 static pgp_key_t pgp_parse_pgp3_key (unsigned char *buff, size_t l)
303 unsigned char digest[SHA_DIGEST_LENGTH];
304 unsigned char scratch[LONG_STRING];
311 p = pgp_new_keyinfo ();
314 for (i = 0; i < 4; i++)
315 gen_time = (gen_time << 8) + buff[j++];
317 p->gen_time = gen_time;
322 p->algorithm = pgp_pkalgbytype (alg);
323 p->flags |= pgp_get_abilities (alg);
326 skip_bignum (buff, l, j, &j, 3);
327 else if (alg == 16 || alg == 20)
328 skip_bignum (buff, l, j, &j, 2);
330 len = (buff[j] << 8) + buff[j + 1];
334 if (alg >= 1 && alg <= 3)
335 skip_bignum (buff, l, j, &j, 2);
336 else if (alg == 17 || alg == 16 || alg == 20)
337 skip_bignum (buff, l, j, &j, 1);
339 pgp_make_pgp3_fingerprint (buff, j, digest);
340 p->fp_len = SHA_DIGEST_LENGTH;
342 for (k = 0; k < 2; k++) {
343 for (id = 0, i = SHA_DIGEST_LENGTH - 8 + k * 4;
344 i < SHA_DIGEST_LENGTH + (k - 1) * 4; i++)
345 id = (id << 8) + digest[i];
347 snprintf ((char *) scratch + k * 8, sizeof (scratch) - k * 8, "%08lX",
351 p->keyid = str_dup ((char *) scratch);
356 static pgp_key_t pgp_parse_keyinfo (unsigned char *buff, size_t l)
364 return pgp_parse_pgp2_key (buff, l);
366 return pgp_parse_pgp3_key (buff, l);
372 static int pgp_parse_pgp2_sig (unsigned char *buff, size_t l,
373 pgp_key_t p, pgp_sig_t * s)
375 unsigned char sigtype;
377 unsigned long signerid1;
378 unsigned long signerid2;
389 for (i = 0; i < 4; i++)
390 sig_gen_time = (sig_gen_time << 8) + buff[j++];
392 signerid1 = signerid2 = 0;
393 for (i = 0; i < 4; i++)
394 signerid1 = (signerid1 << 8) + buff[j++];
396 for (i = 0; i < 4; i++)
397 signerid2 = (signerid2 << 8) + buff[j++];
400 if (sigtype == 0x20 || sigtype == 0x28)
401 p->flags |= KEYFLAG_REVOKED;
404 s->sigtype = sigtype;
412 static int pgp_parse_pgp3_sig (unsigned char *buff, size_t l,
413 pgp_key_t p, pgp_sig_t * s)
415 unsigned char sigtype;
417 unsigned char hashalg;
419 time_t sig_gen_time = -1;
421 long key_validity = -1;
422 unsigned long signerid1 = 0;
423 unsigned long signerid2 = 0;
428 short have_critical_spks = 0;
439 for (ii = 0; ii < 2; ii++) {
443 ml = (buff[j] << 8) + buff[j + 1];
457 skl = (skl - 192) * 256 + buff[j++] + 192;
462 if ((int) ml - (int) skl < 0)
469 switch (skt & 0x7f) {
470 case 2: /* creation time */
475 for (i = 0; i < 4; i++)
476 sig_gen_time = (sig_gen_time << 8) + buff[j++];
480 case 3: /* expiration time */
485 for (i = 0; i < 4; i++)
486 validity = (validity << 8) + buff[j++];
489 case 9: /* key expiration time */
494 for (i = 0; i < 4; i++)
495 key_validity = (key_validity << 8) + buff[j++];
498 case 16: /* issuer key ID */
502 signerid2 = signerid1 = 0;
503 for (i = 0; i < 4; i++)
504 signerid1 = (signerid1 << 8) + buff[j++];
505 for (i = 0; i < 4; i++)
506 signerid2 = (signerid2 << 8) + buff[j++];
510 case 10: /* CMR key */
512 case 4: /* exportable */
515 case 7: /* revocable */
516 case 11: /* Pref. symm. alg. */
517 case 12: /* revocation key */
518 case 20: /* notation data */
519 case 21: /* pref. hash */
520 case 22: /* pref. comp.alg. */
521 case 23: /* key server prefs. */
522 case 24: /* pref. key server */
526 have_critical_spks = 1;
533 if (sigtype == 0x20 || sigtype == 0x28)
534 p->flags |= KEYFLAG_REVOKED;
535 if (key_validity != -1 && time (NULL) > p->gen_time + key_validity)
536 p->flags |= KEYFLAG_EXPIRED;
537 if (have_critical_spks)
538 p->flags |= KEYFLAG_CRITICAL;
541 s->sigtype = sigtype;
552 static int pgp_parse_sig (unsigned char *buff, size_t l,
553 pgp_key_t p, pgp_sig_t * sig)
555 if (!buff || l < 2 || !p)
561 return pgp_parse_pgp2_sig (buff, l, p, sig);
563 return pgp_parse_pgp3_sig (buff, l, p, sig);
569 /* parse one key block, including all subkeys. */
571 static pgp_key_t pgp_parse_keyblock (FILE * fp)
574 unsigned char pt = 0;
575 unsigned char last_pt;
585 pgp_key_t root = NULL;
586 pgp_key_t *last = &root;
588 pgp_uid_t *uid = NULL;
589 pgp_uid_t **addr = NULL;
590 pgp_sig_t **lsig = NULL;
594 while (!err && (buff = pgp_read_packet (fp, &l)) != NULL) {
598 /* check if we have read the complete key block. */
600 if ((pt == PT_SECKEY || pt == PT_PUBKEY) && root) {
611 if (!(*last = p = pgp_parse_keyinfo (buff, l))) {
620 if (pt == PT_SUBKEY || pt == PT_SUBSECKEY) {
621 p->flags |= KEYFLAG_SUBKEY;
624 p->address = pgp_copy_uids (root->address, p);
626 addr = &(*addr)->next;
630 if (pt == PT_SECKEY || pt == PT_SUBSECKEY)
631 p->flags |= KEYFLAG_SECRET;
639 pgp_sig_t *signature = mem_calloc (sizeof (pgp_sig_t), 1);
642 lsig = &signature->next;
644 pgp_parse_sig (buff, l, p, signature);
651 if (p && (last_pt == PT_SECKEY || last_pt == PT_PUBKEY ||
652 last_pt == PT_SUBKEY || last_pt == PT_SUBSECKEY)) {
653 if (buff[1] & 0x20) {
654 p->flags |= KEYFLAG_DISABLED;
657 else if (last_pt == PT_NAME && uid) {
658 uid->trust = buff[1];
670 chr = mem_malloc (l);
671 memcpy (chr, buff + 1, l - 1);
675 *addr = uid = mem_calloc (1, sizeof (pgp_uid_t)); /* XXX */
682 /* the following tags are generated by
686 if (strstr (chr, "ENCR"))
687 p->flags |= KEYFLAG_PREFER_ENCRYPTION;
688 if (strstr (chr, "SIGN"))
689 p->flags |= KEYFLAG_PREFER_SIGNING;
699 pgp_free_key (&root);
704 static int pgpring_string_matches_hint (const char *s, const char *hints[],
709 if (!hints || !nhints)
712 for (i = 0; i < nhints; i++) {
713 if (str_isstr (s, hints[i]) != NULL)
721 * Go through the key ring file and look for keys with
725 static void pgpring_find_candidates (char *ringfile, const char *hints[],
736 unsigned char *buff = NULL;
737 unsigned char pt = 0;
742 if ((rfp = fopen (ringfile, "r")) == NULL) {
744 size_t error_buf_len;
746 error_buf_len = sizeof ("fopen: ") - 1 + str_len (ringfile) + 1;
747 error_buf = mem_malloc (error_buf_len);
748 snprintf (error_buf, error_buf_len, "fopen: %s", ringfile);
750 mem_free (&error_buf);
755 FGETPOS (rfp, keypos);
757 while (!err && (buff = pgp_read_packet (rfp, &l)) != NULL) {
763 if ((pt == PT_SECKEY) || (pt == PT_PUBKEY)) {
766 else if (pt == PT_NAME) {
767 char *tmp = mem_malloc (l);
769 memcpy (tmp, buff + 1, l - 1);
772 /* mutt_decode_utf8_string (tmp, chs); */
774 if (pgpring_string_matches_hint (tmp, hints, nhints)) {
777 FSETPOS (rfp, keypos);
779 /* Not bailing out here would lead us into an endless loop. */
781 if ((p = pgp_parse_keyblock (rfp)) == NULL)
784 pgpring_dump_keyblock (p);
798 static void print_userid (const char *id)
800 for (; id && *id; id++) {
801 if (*id >= ' ' && *id <= 'z' && *id != ':')
804 printf ("\\x%02x", (*id) & 0xff);
808 static void print_fingerprint (pgp_key_t p)
812 printf ("fpr:::::::::");
813 for (i = 0; i < p->fp_len; i++)
814 printf ("%02X", p->fingerprint[i]);
817 } /* print_fingerprint() */
820 static void pgpring_dump_signatures (pgp_sig_t * sig)
822 for (; sig; sig = sig->next) {
823 if (sig->sigtype == 0x10 || sig->sigtype == 0x11 ||
824 sig->sigtype == 0x12 || sig->sigtype == 0x13)
825 printf ("sig::::%08lX%08lX::::::%X:\n",
826 sig->sid1, sig->sid2, sig->sigtype);
827 else if (sig->sigtype == 0x20)
828 printf ("rev::::%08lX%08lX::::::%X:\n",
829 sig->sid1, sig->sid2, sig->sigtype);
834 static char gnupg_trustletter (int t)
847 static void pgpring_dump_keyblock (pgp_key_t p)
854 for (; p; p = p->next) {
857 if (p->flags & KEYFLAG_SECRET) {
858 if (p->flags & KEYFLAG_SUBKEY)
864 if (p->flags & KEYFLAG_SUBKEY)
870 if (p->flags & KEYFLAG_REVOKED)
872 if (p->flags & KEYFLAG_EXPIRED)
874 if (p->flags & KEYFLAG_DISABLED)
877 for (uid = p->address; uid; uid = uid->next, first = 0) {
879 printf ("uid:%c::::::::", gnupg_trustletter (uid->trust));
880 print_userid (uid->addr);
884 if (p->flags & KEYFLAG_SECRET)
887 putchar (gnupg_trustletter (uid->trust));
892 printf (":%d:%d:%s:%04d-%02d-%02d::::", p->keylen, p->numalg,
893 p->keyid, 1900 + tp->tm_year, tp->tm_mon + 1, tp->tm_mday);
895 print_userid (uid->addr);
898 if (pgp_canencrypt (p->numalg))
900 if (pgp_cansign (p->numalg))
902 if (p->flags & KEYFLAG_DISABLED)
906 if (dump_fingerprints)
907 print_fingerprint (p);
910 if (dump_signatures) {
912 pgpring_dump_signatures (p->sigs);
913 pgpring_dump_signatures (uid->sigs);
920 * The mutt_gettext () defined in gettext.c requires iconv,
921 * so we do without charset conversion here.
924 char *mutt_gettext (const char *message)
926 return (char *) message;