2 * Copyright (C) 1997-2003 Thomas Roessler <roessler@does-not-exist.org>
4 * This program is free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later
10 * This program is distributed in the hope that it will be
11 * useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 * PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the Free
18 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * This is a "simple" PGP key ring dumper.
25 * The output format is supposed to be compatible to the one GnuPG
26 * emits and Mutt expects.
28 * Note that the code of this program could be considerably less
29 * complex, but most of it was taken from mutt's second generation
32 * You can actually use this to put together some fairly general
33 * PGP key management applications.
58 #include "pgppacket.h"
60 #define MD5_DIGEST_LENGTH 16
63 #define FGETPOS(fp,pos) fgetpos((fp),&(pos))
64 #define FSETPOS(fp,pos) fsetpos((fp),&(pos))
66 #define FGETPOS(fp,pos) pos=ftello((fp));
67 #define FSETPOS(fp,pos) fseeko((fp),(pos),SEEK_SET)
71 static short dump_signatures = 0;
72 static short dump_fingerprints = 0;
75 static void pgpring_find_candidates (char *ringfile, const char *hints[], int nhints);
76 static void pgpring_dump_keyblock (pgp_key_t p);
78 int main (int argc, char * const argv[])
85 const char *_kring = NULL;
86 char *env_pgppath, *env_home;
88 char pgppath[_POSIX_PATH_MAX];
89 char kring[_POSIX_PATH_MAX];
91 while ((c = getopt (argc, argv, "f25sk:S")) != EOF)
103 dump_fingerprints = 1;
127 fprintf (stderr, "usage: %s [-k <key ring> | [-2 | -5] [ -s] [-S] [-f]] [hints]\n",
135 strfcpy (kring, _kring, sizeof (kring));
138 if ((env_pgppath = getenv ("PGPPATH")))
139 strfcpy (pgppath, env_pgppath, sizeof (pgppath));
140 else if ((env_home = getenv ("HOME")))
141 snprintf (pgppath, sizeof (pgppath), "%s/.pgp", env_home);
144 fprintf (stderr, "%s: Can't determine your PGPPATH.\n", argv[0]);
149 snprintf (kring, sizeof (kring), "%s/secring.%s", pgppath, version == 2 ? "pgp" : "skr");
151 snprintf (kring, sizeof (kring), "%s/pubring.%s", pgppath, version == 2 ? "pgp" : "pkr");
154 pgpring_find_candidates (kring, (const char**) argv + optind, argc - optind);
160 /* The actual key ring parser */
161 static void pgp_make_pgp2_fingerprint (unsigned char *buff,
162 unsigned char *digest)
165 unsigned int size = 0;
169 size = (buff[0] << 8) + buff[1];
170 size = ((size + 7) / 8);
173 md5_process_bytes (buff, size, &ctx);
177 size = (buff[0] << 8) + buff[1];
178 size = ((size + 7) / 8);
181 md5_process_bytes (buff, size, &ctx);
183 md5_finish_ctx (&ctx, digest);
184 } /* pgp_make_pgp2_fingerprint() */
186 static pgp_key_t pgp_parse_pgp2_key (unsigned char *buff, size_t l)
190 unsigned char digest[MD5_DIGEST_LENGTH];
194 unsigned short exp_days = 0;
197 unsigned char scratch[LONG_STRING];
202 p = pgp_new_keyinfo();
204 for (i = 0, j = 2; i < 4; i++)
205 gen_time = (gen_time << 8) + buff[j++];
207 p->gen_time = gen_time;
209 for (i = 0; i < 2; i++)
210 exp_days = (exp_days << 8) + buff[j++];
212 if (exp_days && time (NULL) > gen_time + exp_days * 24 * 3600)
213 p->flags |= KEYFLAG_EXPIRED;
218 p->algorithm = pgp_pkalgbytype (alg);
219 p->flags |= pgp_get_abilities (alg);
221 if (dump_fingerprints)
223 /* j now points to the key material, which we need for the fingerprint */
224 p->fp_len = MD5_DIGEST_LENGTH;
225 pgp_make_pgp2_fingerprint (&buff[j], digest);
226 memcpy (p->fingerprint, digest, MD5_DIGEST_LENGTH);
228 else /* just to be usre */
229 memset (p->fingerprint, 0, MD5_DIGEST_LENGTH);
232 for (i = 0; i < 2; i++)
233 expl = (expl << 8) + buff[j++];
237 expl = (expl + 7) / 8;
244 for (k = 0; k < 2; k++)
246 for (id = 0, i = 0; i < 4; i++)
247 id = (id << 8) + buff[j++];
249 snprintf ((char *) scratch + k * 8, sizeof (scratch) - k * 8,
253 p->keyid = safe_strdup ((char *) scratch);
263 static void pgp_make_pgp3_fingerprint (unsigned char *buff, size_t l,
264 unsigned char *digest)
269 SHA1_Init (&context);
271 dummy = buff[0] & 0x3f;
273 if (dummy == PT_SUBSECKEY || dummy == PT_SUBKEY || dummy == PT_SECKEY)
276 dummy = (dummy << 2) | 0x81;
277 SHA1_Update (&context, &dummy, 1);
278 dummy = ((l - 1) >> 8) & 0xff;
279 SHA1_Update (&context, &dummy, 1);
280 dummy = (l - 1) & 0xff;
281 SHA1_Update (&context, &dummy, 1);
282 SHA1_Update (&context, buff + 1, l - 1);
283 SHA1_Final (digest, &context);
287 static void skip_bignum (unsigned char *buff, size_t l, size_t j,
288 size_t * toff, size_t n)
294 len = (buff[j] << 8) + buff[j + 1];
295 j += (len + 7) / 8 + 2;
297 while (j <= l && --n > 0);
304 static pgp_key_t pgp_parse_pgp3_key (unsigned char *buff, size_t l)
308 unsigned char digest[SHA_DIGEST_LENGTH];
309 unsigned char scratch[LONG_STRING];
316 p = pgp_new_keyinfo ();
319 for (i = 0; i < 4; i++)
320 gen_time = (gen_time << 8) + buff[j++];
322 p->gen_time = gen_time;
327 p->algorithm = pgp_pkalgbytype (alg);
328 p->flags |= pgp_get_abilities (alg);
331 skip_bignum (buff, l, j, &j, 3);
332 else if (alg == 16 || alg == 20)
333 skip_bignum (buff, l, j, &j, 2);
335 len = (buff[j] << 8) + buff[j + 1];
339 if (alg >= 1 && alg <= 3)
340 skip_bignum (buff, l, j, &j, 2);
341 else if (alg == 17 || alg == 16 || alg == 20)
342 skip_bignum (buff, l, j, &j, 1);
344 pgp_make_pgp3_fingerprint (buff, j, digest);
345 p->fp_len = SHA_DIGEST_LENGTH;
347 for (k = 0; k < 2; k++)
349 for (id = 0, i = SHA_DIGEST_LENGTH - 8 + k * 4;
350 i < SHA_DIGEST_LENGTH + (k - 1) * 4; i++)
351 id = (id << 8) + digest[i];
353 snprintf ((char *) scratch + k * 8, sizeof (scratch) - k * 8, "%08lX", id);
356 p->keyid = safe_strdup ((char *) scratch);
361 static pgp_key_t pgp_parse_keyinfo (unsigned char *buff, size_t l)
370 return pgp_parse_pgp2_key (buff, l);
372 return pgp_parse_pgp3_key (buff, l);
378 static int pgp_parse_pgp2_sig (unsigned char *buff, size_t l,
379 pgp_key_t p, pgp_sig_t *s)
381 unsigned char sigtype;
383 unsigned long signerid1;
384 unsigned long signerid2;
395 for (i = 0; i < 4; i++)
396 sig_gen_time = (sig_gen_time << 8) + buff[j++];
398 signerid1 = signerid2 = 0;
399 for (i = 0; i < 4; i++)
400 signerid1 = (signerid1 << 8) + buff[j++];
402 for (i = 0; i < 4; i++)
403 signerid2 = (signerid2 << 8) + buff[j++];
406 if (sigtype == 0x20 || sigtype == 0x28)
407 p->flags |= KEYFLAG_REVOKED;
411 s->sigtype = sigtype;
419 static int pgp_parse_pgp3_sig (unsigned char *buff, size_t l,
420 pgp_key_t p, pgp_sig_t *s)
422 unsigned char sigtype;
424 unsigned char hashalg;
426 time_t sig_gen_time = -1;
428 long key_validity = -1;
429 unsigned long signerid1 = 0;
430 unsigned long signerid2 = 0;
435 short have_critical_spks = 0;
446 for (ii = 0; ii < 2; ii++)
451 ml = (buff[j] << 8) + buff[j + 1];
467 skl = (skl - 192) * 256 + buff[j++] + 192;
472 if ((int) ml - (int) skl < 0)
481 case 2: /* creation time */
486 for (i = 0; i < 4; i++)
487 sig_gen_time = (sig_gen_time << 8) + buff[j++];
491 case 3: /* expiration time */
496 for (i = 0; i < 4; i++)
497 validity = (validity << 8) + buff[j++];
500 case 9: /* key expiration time */
505 for (i = 0; i < 4; i++)
506 key_validity = (key_validity << 8) + buff[j++];
509 case 16: /* issuer key ID */
513 signerid2 = signerid1 = 0;
514 for (i = 0; i < 4; i++)
515 signerid1 = (signerid1 << 8) + buff[j++];
516 for (i = 0; i < 4; i++)
517 signerid2 = (signerid2 << 8) + buff[j++];
521 case 10: /* CMR key */
523 case 4: /* exportable */
526 case 7: /* revocable */
527 case 11: /* Pref. symm. alg. */
528 case 12: /* revocation key */
529 case 20: /* notation data */
530 case 21: /* pref. hash */
531 case 22: /* pref. comp.alg. */
532 case 23: /* key server prefs. */
533 case 24: /* pref. key server */
537 have_critical_spks = 1;
544 if (sigtype == 0x20 || sigtype == 0x28)
545 p->flags |= KEYFLAG_REVOKED;
546 if (key_validity != -1 && time (NULL) > p->gen_time + key_validity)
547 p->flags |= KEYFLAG_EXPIRED;
548 if (have_critical_spks)
549 p->flags |= KEYFLAG_CRITICAL;
553 s->sigtype = sigtype;
564 static int pgp_parse_sig (unsigned char *buff, size_t l,
565 pgp_key_t p, pgp_sig_t *sig)
567 if (!buff || l < 2 || !p)
574 return pgp_parse_pgp2_sig (buff, l, p, sig);
576 return pgp_parse_pgp3_sig (buff, l, p, sig);
582 /* parse one key block, including all subkeys. */
584 static pgp_key_t pgp_parse_keyblock (FILE * fp)
587 unsigned char pt = 0;
588 unsigned char last_pt;
598 pgp_key_t root = NULL;
599 pgp_key_t *last = &root;
601 pgp_uid_t *uid = NULL;
602 pgp_uid_t **addr = NULL;
603 pgp_sig_t **lsig = NULL;
607 while (!err && (buff = pgp_read_packet (fp, &l)) != NULL)
612 /* check if we have read the complete key block. */
614 if ((pt == PT_SECKEY || pt == PT_PUBKEY) && root)
627 if (!(*last = p = pgp_parse_keyinfo (buff, l)))
637 if (pt == PT_SUBKEY || pt == PT_SUBSECKEY)
639 p->flags |= KEYFLAG_SUBKEY;
643 p->address = pgp_copy_uids (root->address, p);
644 while (*addr) addr = &(*addr)->next;
648 if (pt == PT_SECKEY || pt == PT_SUBSECKEY)
649 p->flags |= KEYFLAG_SECRET;
658 pgp_sig_t *signature = safe_calloc (sizeof (pgp_sig_t), 1);
660 lsig = &signature->next;
662 pgp_parse_sig (buff, l, p, signature);
669 if (p && (last_pt == PT_SECKEY || last_pt == PT_PUBKEY ||
670 last_pt == PT_SUBKEY || last_pt == PT_SUBSECKEY))
674 p->flags |= KEYFLAG_DISABLED;
677 else if (last_pt == PT_NAME && uid)
679 uid->trust = buff[1];
691 chr = safe_malloc (l);
692 memcpy (chr, buff + 1, l - 1);
696 *addr = uid = safe_calloc (1, sizeof (pgp_uid_t)); /* XXX */
703 /* the following tags are generated by
707 if (strstr (chr, "ENCR"))
708 p->flags |= KEYFLAG_PREFER_ENCRYPTION;
709 if (strstr (chr, "SIGN"))
710 p->flags |= KEYFLAG_PREFER_SIGNING;
720 pgp_free_key (&root);
725 static int pgpring_string_matches_hint (const char *s, const char *hints[], int nhints)
729 if (!hints || !nhints)
732 for (i = 0; i < nhints; i++)
734 if (mutt_stristr (s, hints[i]) != NULL)
742 * Go through the key ring file and look for keys with
746 static void pgpring_find_candidates (char *ringfile, const char *hints[], int nhints)
755 unsigned char *buff = NULL;
756 unsigned char pt = 0;
761 if ((rfp = fopen (ringfile, "r")) == NULL)
764 size_t error_buf_len;
766 error_buf_len = sizeof ("fopen: ") - 1 + strlen (ringfile) + 1;
767 error_buf = safe_malloc (error_buf_len);
768 snprintf (error_buf, error_buf_len, "fopen: %s", ringfile);
777 while (!err && (buff = pgp_read_packet (rfp, &l)) != NULL)
784 if ((pt == PT_SECKEY) || (pt == PT_PUBKEY))
788 else if (pt == PT_NAME)
790 char *tmp = safe_malloc (l);
792 memcpy (tmp, buff + 1, l - 1);
795 /* mutt_decode_utf8_string (tmp, chs); */
797 if (pgpring_string_matches_hint (tmp, hints, nhints))
801 FSETPOS(rfp, keypos);
803 /* Not bailing out here would lead us into an endless loop. */
805 if ((p = pgp_parse_keyblock (rfp)) == NULL)
808 pgpring_dump_keyblock (p);
822 static void print_userid (const char *id)
824 for (; id && *id; id++)
826 if (*id >= ' ' && *id <= 'z' && *id != ':')
829 printf ("\\x%02x", (*id) & 0xff);
833 static void print_fingerprint (pgp_key_t p)
837 printf ("fpr:::::::::");
838 for (i = 0; i < p->fp_len; i++)
839 printf ("%02X", p->fingerprint[i]);
842 } /* print_fingerprint() */
845 static void pgpring_dump_signatures (pgp_sig_t *sig)
847 for (; sig; sig = sig->next)
849 if (sig->sigtype == 0x10 || sig->sigtype == 0x11 ||
850 sig->sigtype == 0x12 || sig->sigtype == 0x13)
851 printf ("sig::::%08lX%08lX::::::%X:\n",
852 sig->sid1, sig->sid2, sig->sigtype);
853 else if (sig->sigtype == 0x20)
854 printf ("rev::::%08lX%08lX::::::%X:\n",
855 sig->sid1, sig->sid2, sig->sigtype);
860 static char gnupg_trustletter (int t)
871 static void pgpring_dump_keyblock (pgp_key_t p)
878 for (; p; p = p->next)
882 if (p->flags & KEYFLAG_SECRET)
884 if (p->flags & KEYFLAG_SUBKEY)
891 if (p->flags & KEYFLAG_SUBKEY)
897 if (p->flags & KEYFLAG_REVOKED)
899 if (p->flags & KEYFLAG_EXPIRED)
901 if (p->flags & KEYFLAG_DISABLED)
904 for (uid = p->address; uid; uid = uid->next, first = 0)
908 printf ("uid:%c::::::::", gnupg_trustletter (uid->trust));
909 print_userid (uid->addr);
914 if (p->flags & KEYFLAG_SECRET)
917 putchar (gnupg_trustletter (uid->trust));
922 printf (":%d:%d:%s:%04d-%02d-%02d::::", p->keylen, p->numalg, p->keyid,
923 1900 + tp->tm_year, tp->tm_mon + 1, tp->tm_mday);
925 print_userid (uid->addr);
928 if(pgp_canencrypt(p->numalg))
930 if(pgp_cansign(p->numalg))
932 if (p->flags & KEYFLAG_DISABLED)
936 if (dump_fingerprints)
937 print_fingerprint (p);
942 if (first) pgpring_dump_signatures (p->sigs);
943 pgpring_dump_signatures (uid->sigs);
950 * The mutt_gettext () defined in gettext.c requires iconv,
951 * so we do without charset conversion here.
954 char *mutt_gettext (const char *message)
956 return (char *)message;