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>
47 short Umask; /* dirty hack because we need Umask in lib.c but don't want globals.h there */
53 #include "pgppacket.h"
55 #define MD5_DIGEST_LENGTH 16
58 #define FGETPOS(fp,pos) fgetpos((fp),&(pos))
59 #define FSETPOS(fp,pos) fsetpos((fp),&(pos))
61 #define FGETPOS(fp,pos) pos=ftello((fp));
62 #define FSETPOS(fp,pos) fseeko((fp),(pos),SEEK_SET)
66 static short dump_signatures = 0;
67 static short dump_fingerprints = 0;
70 static void pgpring_find_candidates (char *ringfile, const char *hints[],
72 static void pgpring_dump_keyblock (pgp_key_t p);
74 int main (int argc, char *const argv[])
81 const char *_kring = NULL;
82 char *env_pgppath, *env_home;
84 char pgppath[_POSIX_PATH_MAX];
85 char kring[_POSIX_PATH_MAX];
87 while ((c = getopt (argc, argv, "f25sk:S")) != EOF) {
97 dump_fingerprints = 1;
123 "usage: %s [-k <key ring> | [-2 | -5] [ -s] [-S] [-f]] [hints]\n",
131 strfcpy (kring, _kring, sizeof (kring));
133 if ((env_pgppath = getenv ("PGPPATH")))
134 strfcpy (pgppath, env_pgppath, sizeof (pgppath));
135 else if ((env_home = getenv ("HOME")))
136 snprintf (pgppath, sizeof (pgppath), "%s/.pgp", env_home);
138 fprintf (stderr, "%s: Can't determine your PGPPATH.\n", argv[0]);
143 snprintf (kring, sizeof (kring), "%s/secring.%s", pgppath,
144 version == 2 ? "pgp" : "skr");
146 snprintf (kring, sizeof (kring), "%s/pubring.%s", pgppath,
147 version == 2 ? "pgp" : "pkr");
150 pgpring_find_candidates (kring, (const char **) argv + optind,
157 /* The actual key ring parser */
159 static void pgp_make_pgp2_fingerprint (unsigned char *buff,
160 unsigned char *digest)
164 unsigned int size = 0;
169 size = (buff[0] << 8) + buff[1];
170 size = ((size + 7) / 8);
173 MD5Update (&context, buff, size);
176 size = (buff[0] << 8) + buff[1];
177 size = ((size + 7) / 8);
180 MD5Update (&context, buff, size);
182 MD5Final (digest, &context);
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) {
222 /* j now points to the key material, which we need for the fingerprint */
223 p->fp_len = MD5_DIGEST_LENGTH;
224 pgp_make_pgp2_fingerprint (&buff[j], digest);
225 memcpy (p->fingerprint, digest, MD5_DIGEST_LENGTH);
227 else /* just to be usre */
228 memset (p->fingerprint, 0, MD5_DIGEST_LENGTH);
231 for (i = 0; i < 2; i++)
232 expl = (expl << 8) + buff[j++];
236 expl = (expl + 7) / 8;
243 for (k = 0; k < 2; k++) {
244 for (id = 0, i = 0; i < 4; i++)
245 id = (id << 8) + buff[j++];
247 snprintf ((char *) scratch + k * 8, sizeof (scratch) - k * 8,
251 p->keyid = str_dup ((char *) scratch);
261 static void pgp_make_pgp3_fingerprint (unsigned char *buff, size_t l,
262 unsigned char *digest)
267 SHA1_Init (&context);
269 dummy = buff[0] & 0x3f;
271 if (dummy == PT_SUBSECKEY || dummy == PT_SUBKEY || dummy == PT_SECKEY)
274 dummy = (dummy << 2) | 0x81;
275 SHA1_Update (&context, &dummy, 1);
276 dummy = ((l - 1) >> 8) & 0xff;
277 SHA1_Update (&context, &dummy, 1);
278 dummy = (l - 1) & 0xff;
279 SHA1_Update (&context, &dummy, 1);
280 SHA1_Update (&context, buff + 1, l - 1);
281 SHA1_Final (digest, &context);
285 static void skip_bignum (unsigned char *buff, size_t l, size_t j,
286 size_t * toff, size_t n)
291 len = (buff[j] << 8) + buff[j + 1];
292 j += (len + 7) / 8 + 2;
294 while (j <= l && --n > 0);
301 static pgp_key_t pgp_parse_pgp3_key (unsigned char *buff, size_t l)
305 unsigned char digest[SHA_DIGEST_LENGTH];
306 unsigned char scratch[LONG_STRING];
313 p = pgp_new_keyinfo ();
316 for (i = 0; i < 4; i++)
317 gen_time = (gen_time << 8) + buff[j++];
319 p->gen_time = gen_time;
324 p->algorithm = pgp_pkalgbytype (alg);
325 p->flags |= pgp_get_abilities (alg);
328 skip_bignum (buff, l, j, &j, 3);
329 else if (alg == 16 || alg == 20)
330 skip_bignum (buff, l, j, &j, 2);
332 len = (buff[j] << 8) + buff[j + 1];
336 if (alg >= 1 && alg <= 3)
337 skip_bignum (buff, l, j, &j, 2);
338 else if (alg == 17 || alg == 16 || alg == 20)
339 skip_bignum (buff, l, j, &j, 1);
341 pgp_make_pgp3_fingerprint (buff, j, digest);
342 p->fp_len = SHA_DIGEST_LENGTH;
344 for (k = 0; k < 2; k++) {
345 for (id = 0, i = SHA_DIGEST_LENGTH - 8 + k * 4;
346 i < SHA_DIGEST_LENGTH + (k - 1) * 4; i++)
347 id = (id << 8) + digest[i];
349 snprintf ((char *) scratch + k * 8, sizeof (scratch) - k * 8, "%08lX",
353 p->keyid = str_dup ((char *) scratch);
358 static pgp_key_t pgp_parse_keyinfo (unsigned char *buff, size_t l)
366 return pgp_parse_pgp2_key (buff, l);
368 return pgp_parse_pgp3_key (buff, l);
374 static int pgp_parse_pgp2_sig (unsigned char *buff, size_t l,
375 pgp_key_t p, pgp_sig_t * s)
377 unsigned char sigtype;
379 unsigned long signerid1;
380 unsigned long signerid2;
391 for (i = 0; i < 4; i++)
392 sig_gen_time = (sig_gen_time << 8) + buff[j++];
394 signerid1 = signerid2 = 0;
395 for (i = 0; i < 4; i++)
396 signerid1 = (signerid1 << 8) + buff[j++];
398 for (i = 0; i < 4; i++)
399 signerid2 = (signerid2 << 8) + buff[j++];
402 if (sigtype == 0x20 || sigtype == 0x28)
403 p->flags |= KEYFLAG_REVOKED;
406 s->sigtype = sigtype;
414 static int pgp_parse_pgp3_sig (unsigned char *buff, size_t l,
415 pgp_key_t p, pgp_sig_t * s)
417 unsigned char sigtype;
419 unsigned char hashalg;
421 time_t sig_gen_time = -1;
423 long key_validity = -1;
424 unsigned long signerid1 = 0;
425 unsigned long signerid2 = 0;
430 short have_critical_spks = 0;
441 for (ii = 0; ii < 2; ii++) {
445 ml = (buff[j] << 8) + buff[j + 1];
459 skl = (skl - 192) * 256 + buff[j++] + 192;
464 if ((int) ml - (int) skl < 0)
471 switch (skt & 0x7f) {
472 case 2: /* creation time */
477 for (i = 0; i < 4; i++)
478 sig_gen_time = (sig_gen_time << 8) + buff[j++];
482 case 3: /* expiration time */
487 for (i = 0; i < 4; i++)
488 validity = (validity << 8) + buff[j++];
491 case 9: /* key expiration time */
496 for (i = 0; i < 4; i++)
497 key_validity = (key_validity << 8) + buff[j++];
500 case 16: /* issuer key ID */
504 signerid2 = signerid1 = 0;
505 for (i = 0; i < 4; i++)
506 signerid1 = (signerid1 << 8) + buff[j++];
507 for (i = 0; i < 4; i++)
508 signerid2 = (signerid2 << 8) + buff[j++];
512 case 10: /* CMR key */
514 case 4: /* exportable */
517 case 7: /* revocable */
518 case 11: /* Pref. symm. alg. */
519 case 12: /* revocation key */
520 case 20: /* notation data */
521 case 21: /* pref. hash */
522 case 22: /* pref. comp.alg. */
523 case 23: /* key server prefs. */
524 case 24: /* pref. key server */
528 have_critical_spks = 1;
535 if (sigtype == 0x20 || sigtype == 0x28)
536 p->flags |= KEYFLAG_REVOKED;
537 if (key_validity != -1 && time (NULL) > p->gen_time + key_validity)
538 p->flags |= KEYFLAG_EXPIRED;
539 if (have_critical_spks)
540 p->flags |= KEYFLAG_CRITICAL;
543 s->sigtype = sigtype;
554 static int pgp_parse_sig (unsigned char *buff, size_t l,
555 pgp_key_t p, pgp_sig_t * sig)
557 if (!buff || l < 2 || !p)
563 return pgp_parse_pgp2_sig (buff, l, p, sig);
565 return pgp_parse_pgp3_sig (buff, l, p, sig);
571 /* parse one key block, including all subkeys. */
573 static pgp_key_t pgp_parse_keyblock (FILE * fp)
576 unsigned char pt = 0;
577 unsigned char last_pt;
587 pgp_key_t root = NULL;
588 pgp_key_t *last = &root;
590 pgp_uid_t *uid = NULL;
591 pgp_uid_t **addr = NULL;
592 pgp_sig_t **lsig = NULL;
596 while (!err && (buff = pgp_read_packet (fp, &l)) != NULL) {
600 /* check if we have read the complete key block. */
602 if ((pt == PT_SECKEY || pt == PT_PUBKEY) && root) {
613 if (!(*last = p = pgp_parse_keyinfo (buff, l))) {
622 if (pt == PT_SUBKEY || pt == PT_SUBSECKEY) {
623 p->flags |= KEYFLAG_SUBKEY;
626 p->address = pgp_copy_uids (root->address, p);
628 addr = &(*addr)->next;
632 if (pt == PT_SECKEY || pt == PT_SUBSECKEY)
633 p->flags |= KEYFLAG_SECRET;
641 pgp_sig_t *signature = mem_calloc (sizeof (pgp_sig_t), 1);
644 lsig = &signature->next;
646 pgp_parse_sig (buff, l, p, signature);
653 if (p && (last_pt == PT_SECKEY || last_pt == PT_PUBKEY ||
654 last_pt == PT_SUBKEY || last_pt == PT_SUBSECKEY)) {
655 if (buff[1] & 0x20) {
656 p->flags |= KEYFLAG_DISABLED;
659 else if (last_pt == PT_NAME && uid) {
660 uid->trust = buff[1];
672 chr = p_dupstr(buff + 1, l - 1);
674 *addr = uid = mem_calloc (1, sizeof (pgp_uid_t)); /* XXX */
681 /* the following tags are generated by
685 if (strstr (chr, "ENCR"))
686 p->flags |= KEYFLAG_PREFER_ENCRYPTION;
687 if (strstr (chr, "SIGN"))
688 p->flags |= KEYFLAG_PREFER_SIGNING;
698 pgp_free_key (&root);
703 static int pgpring_string_matches_hint (const char *s, const char *hints[],
708 if (!hints || !nhints)
711 for (i = 0; i < nhints; i++) {
712 if (str_isstr (s, hints[i]) != NULL)
720 * Go through the key ring file and look for keys with
724 static void pgpring_find_candidates (char *ringfile, const char *hints[],
735 unsigned char *buff = NULL;
736 unsigned char pt = 0;
741 if ((rfp = fopen (ringfile, "r")) == NULL) {
743 size_t error_buf_len;
745 error_buf_len = sizeof ("fopen: ") - 1 + str_len (ringfile) + 1;
746 error_buf = p_new(char, error_buf_len);
747 snprintf (error_buf, error_buf_len, "fopen: %s", ringfile);
749 p_delete(&error_buf);
754 FGETPOS (rfp, keypos);
756 while (!err && (buff = pgp_read_packet (rfp, &l)) != NULL) {
762 if ((pt == PT_SECKEY) || (pt == PT_PUBKEY)) {
765 else if (pt == PT_NAME) {
766 char *tmp = p_dupstr(buff + 1, l - 1);
768 /* mutt_decode_utf8_string (tmp, chs); */
770 if (pgpring_string_matches_hint (tmp, hints, nhints)) {
773 FSETPOS (rfp, keypos);
775 /* Not bailing out here would lead us into an endless loop. */
777 if ((p = pgp_parse_keyblock (rfp)) == NULL)
780 pgpring_dump_keyblock (p);
794 static void print_userid (const char *id)
796 for (; id && *id; id++) {
797 if (*id >= ' ' && *id <= 'z' && *id != ':')
800 printf ("\\x%02x", (*id) & 0xff);
804 static void print_fingerprint (pgp_key_t p)
808 printf ("fpr:::::::::");
809 for (i = 0; i < p->fp_len; i++)
810 printf ("%02X", p->fingerprint[i]);
813 } /* print_fingerprint() */
816 static void pgpring_dump_signatures (pgp_sig_t * sig)
818 for (; sig; sig = sig->next) {
819 if (sig->sigtype == 0x10 || sig->sigtype == 0x11 ||
820 sig->sigtype == 0x12 || sig->sigtype == 0x13)
821 printf ("sig::::%08lX%08lX::::::%X:\n",
822 sig->sid1, sig->sid2, sig->sigtype);
823 else if (sig->sigtype == 0x20)
824 printf ("rev::::%08lX%08lX::::::%X:\n",
825 sig->sid1, sig->sid2, sig->sigtype);
830 static char gnupg_trustletter (int t)
843 static void pgpring_dump_keyblock (pgp_key_t p)
850 for (; p; p = p->next) {
853 if (p->flags & KEYFLAG_SECRET) {
854 if (p->flags & KEYFLAG_SUBKEY)
860 if (p->flags & KEYFLAG_SUBKEY)
866 if (p->flags & KEYFLAG_REVOKED)
868 if (p->flags & KEYFLAG_EXPIRED)
870 if (p->flags & KEYFLAG_DISABLED)
873 for (uid = p->address; uid; uid = uid->next, first = 0) {
875 printf ("uid:%c::::::::", gnupg_trustletter (uid->trust));
876 print_userid (uid->addr);
880 if (p->flags & KEYFLAG_SECRET)
883 putchar (gnupg_trustletter (uid->trust));
888 printf (":%d:%d:%s:%04d-%02d-%02d::::", p->keylen, p->numalg,
889 p->keyid, 1900 + tp->tm_year, tp->tm_mon + 1, tp->tm_mday);
891 print_userid (uid->addr);
894 if (pgp_canencrypt (p->numalg))
896 if (pgp_cansign (p->numalg))
898 if (p->flags & KEYFLAG_DISABLED)
902 if (dump_fingerprints)
903 print_fingerprint (p);
906 if (dump_signatures) {
908 pgpring_dump_signatures (p->sigs);
909 pgpring_dump_signatures (uid->sigs);
916 * The mutt_gettext () defined in gettext.c requires iconv,
917 * so we do without charset conversion here.
920 char *mutt_gettext (const char *message)
922 return (char *) message;