packet-print.c revision 1.42
1/*-
2 * Copyright (c) 2009 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Alistair Crooks (agc@NetBSD.org)
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29/*
30 * Copyright (c) 2005-2008 Nominet UK (www.nic.uk)
31 * All rights reserved.
32 * Contributors: Ben Laurie, Rachel Willmer. The Contributors have asserted
33 * their moral rights under the UK Copyright Design and Patents Act 1988 to
34 * be recorded as the authors of this copyright work.
35 *
36 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
37 * use this file except in compliance with the License.
38 *
39 * You may obtain a copy of the License at
40 *     http://www.apache.org/licenses/LICENSE-2.0
41 *
42 * Unless required by applicable law or agreed to in writing, software
43 * distributed under the License is distributed on an "AS IS" BASIS,
44 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
45 *
46 * See the License for the specific language governing permissions and
47 * limitations under the License.
48 */
49
50/*
51 * ! \file \brief Standard API print functions
52 */
53#include "config.h"
54
55#ifdef HAVE_SYS_CDEFS_H
56#include <sys/cdefs.h>
57#endif
58
59#if defined(__NetBSD__)
60__COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc. All rights reserved.");
61__RCSID("$NetBSD: packet-print.c,v 1.42 2012/02/22 06:29:40 agc Exp $");
62#endif
63
64#include <string.h>
65#include <stdio.h>
66
67#ifdef HAVE_UNISTD_H
68#include <unistd.h>
69#endif
70
71#include "crypto.h"
72#include "keyring.h"
73#include "packet-show.h"
74#include "signature.h"
75#include "readerwriter.h"
76#include "netpgpdefs.h"
77#include "netpgpsdk.h"
78#include "packet.h"
79#include "netpgpdigest.h"
80#include "mj.h"
81
82/* static functions */
83
84static void
85print_indent(int indent)
86{
87	int             i;
88
89	for (i = 0; i < indent; i++) {
90		printf("  ");
91	}
92}
93
94static void
95print_name(int indent, const char *name)
96{
97	print_indent(indent);
98	if (name) {
99		printf("%s: ", name);
100	}
101}
102
103static void
104print_hexdump(int indent, const char *name, const uint8_t *data, unsigned len)
105{
106	print_name(indent, name);
107	hexdump(stdout, NULL, data, len);
108}
109
110static void
111hexdump_data(int indent, const char *name, const uint8_t *data, unsigned len)
112{
113	print_name(indent, name);
114	hexdump(stdout, NULL, data, len);
115}
116
117static void
118print_uint(int indent, const char *name, unsigned val)
119{
120	print_name(indent, name);
121	printf("%u\n", val);
122}
123
124static void
125showtime(const char *name, time_t t)
126{
127	printf("%s=%" PRItime "d (%.24s)", name, (long long) t, ctime(&t));
128}
129
130static void
131print_time(int indent, const char *name, time_t t)
132{
133	print_indent(indent);
134	printf("%s: ", name);
135	showtime("time", t);
136	printf("\n");
137}
138
139static void
140print_string_and_value(int indent, const char *name, const char *str, uint8_t value)
141{
142	print_name(indent, name);
143	printf("%s (0x%x)\n", str, value);
144}
145
146static void
147print_tagname(int indent, const char *str)
148{
149	print_indent(indent);
150	printf("%s packet\n", str);
151}
152
153static void
154print_data(int indent, const char *name, const pgp_data_t *data)
155{
156	print_hexdump(indent, name, data->contents, (unsigned)data->len);
157}
158
159static void
160print_bn(int indent, const char *name, const BIGNUM *bn)
161{
162	print_indent(indent);
163	printf("%s=", name);
164	if (bn) {
165		BN_print_fp(stdout, bn);
166		putchar('\n');
167	} else {
168		puts("(unset)");
169	}
170}
171
172static void
173print_packet_hex(const pgp_subpacket_t *pkt)
174{
175	hexdump(stdout, "packet contents:", pkt->raw, pkt->length);
176}
177
178static void
179print_escaped(const uint8_t *data, size_t length)
180{
181	while (length-- > 0) {
182		if ((*data >= 0x20 && *data < 0x7f && *data != '%') ||
183		    *data == '\n') {
184			putchar(*data);
185		} else {
186			printf("%%%02x", *data);
187		}
188		++data;
189	}
190}
191
192static void
193print_string(int indent, const char *name, const char *str)
194{
195	print_name(indent, name);
196	print_escaped((const uint8_t *) str, strlen(str));
197	putchar('\n');
198}
199
200static void
201print_utf8_string(int indent, const char *name, const uint8_t *str)
202{
203	/* \todo Do this better for non-English character sets */
204	print_string(indent, name, (const char *) str);
205}
206
207static void
208print_duration(int indent, const char *name, time_t t)
209{
210	int             mins, hours, days, years;
211
212	print_indent(indent);
213	printf("%s: ", name);
214	printf("duration %" PRItime "d seconds", (long long) t);
215
216	mins = (int)(t / 60);
217	hours = mins / 60;
218	days = hours / 24;
219	years = days / 365;
220
221	printf(" (approx. ");
222	if (years) {
223		printf("%d %s", years, years == 1 ? "year" : "years");
224	} else if (days) {
225		printf("%d %s", days, days == 1 ? "day" : "days");
226	} else if (hours) {
227		printf("%d %s", hours, hours == 1 ? "hour" : "hours");
228	}
229	printf(")\n");
230}
231
232static void
233print_boolean(int indent, const char *name, uint8_t boolval)
234{
235	print_name(indent, name);
236	printf("%s\n", (boolval) ? "Yes" : "No");
237}
238
239static void
240print_text_breakdown(int indent, pgp_text_t *text)
241{
242	const char     *prefix = ".. ";
243	unsigned        i;
244
245	/* these were recognised */
246	for (i = 0; i < text->known.used; i++) {
247		print_indent(indent);
248		printf("%s", prefix);
249		printf("%s\n", text->known.strings[i]);
250	}
251	/*
252	 * these were not recognised. the strings will contain the hex value
253	 * of the unrecognised value in string format - see
254	 * process_octet_str()
255	 */
256	if (text->unknown.used) {
257		printf("\n");
258		print_indent(indent);
259		printf("Not Recognised: ");
260	}
261	for (i = 0; i < text->unknown.used; i++) {
262		print_indent(indent);
263		printf("%s", prefix);
264		printf("%s\n", text->unknown.strings[i]);
265	}
266}
267
268static void
269print_headers(const pgp_headers_t *h)
270{
271	unsigned        i;
272
273	for (i = 0; i < h->headerc; ++i) {
274		printf("%s=%s\n", h->headers[i].key, h->headers[i].value);
275	}
276}
277
278static void
279print_block(int indent, const char *name, const uint8_t *str, size_t length)
280{
281	int             o = (int)length;
282
283	print_indent(indent);
284	printf(">>>>> %s >>>>>\n", name);
285
286	print_indent(indent);
287	for (; length > 0; --length) {
288		if (*str >= 0x20 && *str < 0x7f && *str != '%') {
289			putchar(*str);
290		} else if (*str == '\n') {
291			putchar(*str);
292			print_indent(indent);
293		} else {
294			printf("%%%02x", *str);
295		}
296		++str;
297	}
298	if (o && str[-1] != '\n') {
299		putchar('\n');
300		print_indent(indent);
301		fputs("[no newline]", stdout);
302	} else {
303		print_indent(indent);
304	}
305	printf("<<<<< %s <<<<<\n", name);
306}
307
308/* return the number of bits in the public key */
309static int
310numkeybits(const pgp_pubkey_t *pubkey)
311{
312	switch(pubkey->alg) {
313	case PGP_PKA_RSA:
314	case PGP_PKA_RSA_ENCRYPT_ONLY:
315	case PGP_PKA_RSA_SIGN_ONLY:
316		return BN_num_bytes(pubkey->key.rsa.n) * 8;
317	case PGP_PKA_DSA:
318		switch(BN_num_bytes(pubkey->key.dsa.q)) {
319		case 20:
320			return 1024;
321		case 28:
322			return 2048;
323		case 32:
324			return 3072;
325		default:
326			return 0;
327		}
328	case PGP_PKA_ELGAMAL:
329		return BN_num_bytes(pubkey->key.elgamal.y) * 8;
330	default:
331		return -1;
332	}
333}
334
335/* return the hexdump as a string */
336static char *
337strhexdump(char *dest, const uint8_t *src, size_t length, const char *sep)
338{
339	unsigned i;
340	int	n;
341
342	for (n = 0, i = 0 ; i < length ; i += 2) {
343		n += snprintf(&dest[n], 3, "%02x", *src++);
344		n += snprintf(&dest[n], 10, "%02x%s", *src++, sep);
345	}
346	return dest;
347}
348
349/* return the time as a string */
350static char *
351ptimestr(char *dest, size_t size, time_t t)
352{
353	struct tm      *tm;
354
355	tm = gmtime(&t);
356	(void) snprintf(dest, size, "%04d-%02d-%02d",
357		tm->tm_year + 1900,
358		tm->tm_mon + 1,
359		tm->tm_mday);
360	return dest;
361}
362
363/* print the sub key binding signature info */
364static int
365psubkeybinding(char *buf, size_t size, const pgp_key_t *key, const char *expired)
366{
367	char	keyid[512];
368	char	t[32];
369
370	return snprintf(buf, size, "encryption %d/%s %s %s %s\n",
371		numkeybits(&key->enckey),
372		pgp_show_pka(key->enckey.alg),
373		strhexdump(keyid, key->encid, PGP_KEY_ID_SIZE, ""),
374		ptimestr(t, sizeof(t), key->enckey.birthtime),
375		expired);
376}
377
378static int
379isrevoked(const pgp_key_t *key, unsigned uid)
380{
381	unsigned	r;
382
383	for (r = 0 ; r < key->revokec ; r++) {
384		if (key->revokes[r].uid == uid) {
385			return r;
386		}
387	}
388	return -1;
389}
390
391#ifndef KB
392#define KB(x)	((x) * 1024)
393#endif
394
395/* print into a string (malloc'ed) the pubkeydata */
396int
397pgp_sprint_keydata(pgp_io_t *io, const pgp_keyring_t *keyring,
398		const pgp_key_t *key, char **buf, const char *header,
399		const pgp_pubkey_t *pubkey, const int psigs)
400{
401	const pgp_key_t	*trustkey;
402	unsigned	 	 from;
403	unsigned		 i;
404	unsigned		 j;
405	time_t			 now;
406	char			 uidbuf[KB(128)];
407	char			 keyid[PGP_KEY_ID_SIZE * 3];
408	char			 fp[(PGP_FINGERPRINT_SIZE * 3) + 1];
409	char			 expired[128];
410	char			 t[32];
411	int			 cc;
412	int			 n;
413	int			 r;
414
415	if (key == NULL || key->revoked) {
416		return -1;
417	}
418	now = time(NULL);
419	if (pubkey->duration > 0) {
420		cc = snprintf(expired, sizeof(expired),
421			(pubkey->birthtime + pubkey->duration < now) ?
422			"[EXPIRED " : "[EXPIRES ");
423		ptimestr(&expired[cc], sizeof(expired) - cc,
424			pubkey->birthtime + pubkey->duration);
425		cc += 10;
426		cc += snprintf(&expired[cc], sizeof(expired) - cc, "]");
427	} else {
428		expired[0] = 0x0;
429	}
430	for (i = 0, n = 0; i < key->uidc; i++) {
431		if ((r = isrevoked(key, i)) >= 0 &&
432		    key->revokes[r].code == PGP_REVOCATION_COMPROMISED) {
433			continue;
434		}
435		n += snprintf(&uidbuf[n], sizeof(uidbuf) - n, "uid%s%s%s\n",
436				(psigs) ? "    " : "              ",
437				key->uids[i],
438				(isrevoked(key, i) >= 0) ? " [REVOKED]" : "");
439		for (j = 0 ; j < key->subsigc ; j++) {
440			if (psigs) {
441				if (key->subsigs[j].uid != i) {
442					continue;
443				}
444			} else {
445				if (!(key->subsigs[j].sig.info.version == 4 &&
446					key->subsigs[j].sig.info.type == PGP_SIG_SUBKEY &&
447					i == key->uidc - 1)) {
448						continue;
449				}
450			}
451			from = 0;
452			trustkey = pgp_getkeybyid(io, keyring, key->subsigs[j].sig.info.signer_id, &from, NULL);
453			if (key->subsigs[j].sig.info.version == 4 &&
454					key->subsigs[j].sig.info.type == PGP_SIG_SUBKEY) {
455				psubkeybinding(&uidbuf[n], sizeof(uidbuf) - n, key, expired);
456			} else {
457				n += snprintf(&uidbuf[n], sizeof(uidbuf) - n,
458					"sig        %s  %s  %s\n",
459					strhexdump(keyid, key->subsigs[j].sig.info.signer_id, PGP_KEY_ID_SIZE, ""),
460					ptimestr(t, sizeof(t), key->subsigs[j].sig.info.birthtime),
461					(trustkey) ? (char *)trustkey->uids[trustkey->uid0] : "[unknown]");
462			}
463		}
464	}
465	return pgp_asprintf(buf, "%s %d/%s %s %s %s\nKey fingerprint: %s\n%s",
466		header,
467		numkeybits(pubkey),
468		pgp_show_pka(pubkey->alg),
469		strhexdump(keyid, key->sigid, PGP_KEY_ID_SIZE, ""),
470		ptimestr(t, sizeof(t), pubkey->birthtime),
471		expired,
472		strhexdump(fp, key->sigfingerprint.fingerprint, key->sigfingerprint.length, " "),
473		uidbuf);
474}
475
476/* return the key info as a JSON encoded string */
477int
478pgp_sprint_mj(pgp_io_t *io, const pgp_keyring_t *keyring,
479		const pgp_key_t *key, mj_t *keyjson, const char *header,
480		const pgp_pubkey_t *pubkey, const int psigs)
481{
482	const pgp_key_t	*trustkey;
483	unsigned	 	 from;
484	unsigned		 i;
485	unsigned		 j;
486	mj_t			 sub_obj;
487	char			 keyid[PGP_KEY_ID_SIZE * 3];
488	char			 fp[(PGP_FINGERPRINT_SIZE * 3) + 1];
489	int			 r;
490
491	if (key == NULL || key->revoked) {
492		return -1;
493	}
494	(void) memset(keyjson, 0x0, sizeof(*keyjson));
495	mj_create(keyjson, "object");
496	mj_append_field(keyjson, "header", "string", header, -1);
497	mj_append_field(keyjson, "key bits", "integer", (int64_t) numkeybits(pubkey));
498	mj_append_field(keyjson, "pka", "string", pgp_show_pka(pubkey->alg), -1);
499	mj_append_field(keyjson, "key id", "string", strhexdump(keyid, key->sigid, PGP_KEY_ID_SIZE, ""), -1);
500	mj_append_field(keyjson, "fingerprint", "string",
501		strhexdump(fp, key->sigfingerprint.fingerprint, key->sigfingerprint.length, " "), -1);
502	mj_append_field(keyjson, "birthtime", "integer", pubkey->birthtime);
503	mj_append_field(keyjson, "duration", "integer", pubkey->duration);
504	for (i = 0; i < key->uidc; i++) {
505		if ((r = isrevoked(key, i)) >= 0 &&
506		    key->revokes[r].code == PGP_REVOCATION_COMPROMISED) {
507			continue;
508		}
509		(void) memset(&sub_obj, 0x0, sizeof(sub_obj));
510		mj_create(&sub_obj, "array");
511		mj_append(&sub_obj, "string", key->uids[i], -1);
512		mj_append(&sub_obj, "string", (r >= 0) ? "[REVOKED]" : "", -1);
513		mj_append_field(keyjson, "uid", "array", &sub_obj);
514		mj_delete(&sub_obj);
515		for (j = 0 ; j < key->subsigc ; j++) {
516			if (psigs) {
517				if (key->subsigs[j].uid != i) {
518					continue;
519				}
520			} else {
521				if (!(key->subsigs[j].sig.info.version == 4 &&
522					key->subsigs[j].sig.info.type == PGP_SIG_SUBKEY &&
523					i == key->uidc - 1)) {
524						continue;
525				}
526			}
527			(void) memset(&sub_obj, 0x0, sizeof(sub_obj));
528			mj_create(&sub_obj, "array");
529			if (key->subsigs[j].sig.info.version == 4 &&
530					key->subsigs[j].sig.info.type == PGP_SIG_SUBKEY) {
531				mj_append(&sub_obj, "integer", (int64_t)numkeybits(&key->enckey));
532				mj_append(&sub_obj, "string",
533					(const char *)pgp_show_pka(key->enckey.alg), -1);
534				mj_append(&sub_obj, "string",
535					strhexdump(keyid, key->encid, PGP_KEY_ID_SIZE, ""), -1);
536				mj_append(&sub_obj, "integer", (int64_t)key->enckey.birthtime);
537				mj_append_field(keyjson, "encryption", "array", &sub_obj);
538				mj_delete(&sub_obj);
539			} else {
540				mj_append(&sub_obj, "string",
541					strhexdump(keyid, key->subsigs[j].sig.info.signer_id, PGP_KEY_ID_SIZE, ""), -1);
542				mj_append(&sub_obj, "integer",
543					(int64_t)(key->subsigs[j].sig.info.birthtime));
544				from = 0;
545				trustkey = pgp_getkeybyid(io, keyring, key->subsigs[j].sig.info.signer_id, &from, NULL);
546				mj_append(&sub_obj, "string",
547					(trustkey) ? (char *)trustkey->uids[trustkey->uid0] : "[unknown]", -1);
548				mj_append_field(keyjson, "sig", "array", &sub_obj);
549				mj_delete(&sub_obj);
550			}
551		}
552	}
553	if (pgp_get_debug_level(__FILE__)) {
554		char	*buf;
555
556		mj_asprint(&buf, keyjson, 1);
557		(void) fprintf(stderr, "pgp_sprint_mj: '%s'\n", buf);
558		free(buf);
559	}
560	return 1;
561}
562
563int
564pgp_hkp_sprint_keydata(pgp_io_t *io, const pgp_keyring_t *keyring,
565		const pgp_key_t *key, char **buf,
566		const pgp_pubkey_t *pubkey, const int psigs)
567{
568	const pgp_key_t	*trustkey;
569	unsigned	 	 from;
570	unsigned	 	 i;
571	unsigned	 	 j;
572	char			 keyid[PGP_KEY_ID_SIZE * 3];
573	char		 	 uidbuf[KB(128)];
574	char		 	 fp[(PGP_FINGERPRINT_SIZE * 3) + 1];
575	int		 	 n;
576
577	if (key->revoked) {
578		return -1;
579	}
580	for (i = 0, n = 0; i < key->uidc; i++) {
581		n += snprintf(&uidbuf[n], sizeof(uidbuf) - n,
582			"uid:%lld:%lld:%s\n",
583			(long long)pubkey->birthtime,
584			(long long)pubkey->duration,
585			key->uids[i]);
586		for (j = 0 ; j < key->subsigc ; j++) {
587			if (psigs) {
588				if (key->subsigs[j].uid != i) {
589					continue;
590				}
591			} else {
592				if (!(key->subsigs[j].sig.info.version == 4 &&
593					key->subsigs[j].sig.info.type == PGP_SIG_SUBKEY &&
594					i == key->uidc - 1)) {
595						continue;
596				}
597			}
598			from = 0;
599			trustkey = pgp_getkeybyid(io, keyring, key->subsigs[j].sig.info.signer_id, &from, NULL);
600			if (key->subsigs[j].sig.info.version == 4 &&
601					key->subsigs[j].sig.info.type == PGP_SIG_SUBKEY) {
602				n += snprintf(&uidbuf[n], sizeof(uidbuf) - n, "sub:%d:%d:%s:%lld:%lld\n",
603					numkeybits(pubkey),
604					key->subsigs[j].sig.info.key_alg,
605					strhexdump(keyid, key->subsigs[j].sig.info.signer_id, PGP_KEY_ID_SIZE, ""),
606					(long long)(key->subsigs[j].sig.info.birthtime),
607					(long long)pubkey->duration);
608			} else {
609				n += snprintf(&uidbuf[n], sizeof(uidbuf) - n,
610					"sig:%s:%lld:%s\n",
611					strhexdump(keyid, key->subsigs[j].sig.info.signer_id, PGP_KEY_ID_SIZE, ""),
612					(long long)key->subsigs[j].sig.info.birthtime,
613					(trustkey) ? (char *)trustkey->uids[trustkey->uid0] : "");
614			}
615		}
616	}
617	return pgp_asprintf(buf, "pub:%s:%d:%d:%lld:%lld\n%s",
618		strhexdump(fp, key->sigfingerprint.fingerprint, PGP_FINGERPRINT_SIZE, ""),
619		pubkey->alg,
620		numkeybits(pubkey),
621		(long long)pubkey->birthtime,
622		(long long)pubkey->duration,
623		uidbuf);
624}
625
626/* print the key data for a pub or sec key */
627void
628pgp_print_keydata(pgp_io_t *io, const pgp_keyring_t *keyring,
629		const pgp_key_t *key, const char *header,
630		const pgp_pubkey_t *pubkey, const int psigs)
631{
632	char	*cp;
633
634	if (pgp_sprint_keydata(io, keyring, key, &cp, header, pubkey, psigs) >= 0) {
635		(void) fprintf(io->res, "%s", cp);
636		free(cp);
637	}
638}
639
640/**
641\ingroup Core_Print
642\param pubkey
643*/
644void
645pgp_print_pubkey(const pgp_pubkey_t *pubkey)
646{
647	printf("------- PUBLIC KEY ------\n");
648	print_uint(0, "Version", (unsigned)pubkey->version);
649	print_time(0, "Creation Time", pubkey->birthtime);
650	if (pubkey->version == PGP_V3) {
651		print_uint(0, "Days Valid", pubkey->days_valid);
652	}
653	print_string_and_value(0, "Algorithm", pgp_show_pka(pubkey->alg),
654			       pubkey->alg);
655	switch (pubkey->alg) {
656	case PGP_PKA_DSA:
657		print_bn(0, "p", pubkey->key.dsa.p);
658		print_bn(0, "q", pubkey->key.dsa.q);
659		print_bn(0, "g", pubkey->key.dsa.g);
660		print_bn(0, "y", pubkey->key.dsa.y);
661		break;
662
663	case PGP_PKA_RSA:
664	case PGP_PKA_RSA_ENCRYPT_ONLY:
665	case PGP_PKA_RSA_SIGN_ONLY:
666		print_bn(0, "n", pubkey->key.rsa.n);
667		print_bn(0, "e", pubkey->key.rsa.e);
668		break;
669
670	case PGP_PKA_ELGAMAL:
671	case PGP_PKA_ELGAMAL_ENCRYPT_OR_SIGN:
672		print_bn(0, "p", pubkey->key.elgamal.p);
673		print_bn(0, "g", pubkey->key.elgamal.g);
674		print_bn(0, "y", pubkey->key.elgamal.y);
675		break;
676
677	default:
678		(void) fprintf(stderr,
679			"pgp_print_pubkey: Unusual algorithm\n");
680	}
681
682	printf("------- end of PUBLIC KEY ------\n");
683}
684
685int
686pgp_sprint_pubkey(const pgp_key_t *key, char *out, size_t outsize)
687{
688	char	fp[(PGP_FINGERPRINT_SIZE * 3) + 1];
689	int	cc;
690
691	cc = snprintf(out, outsize, "key=%s\nname=%s\ncreation=%lld\nexpiry=%lld\nversion=%d\nalg=%d\n",
692		strhexdump(fp, key->sigfingerprint.fingerprint, PGP_FINGERPRINT_SIZE, ""),
693		key->uids[key->uid0],
694		(long long)key->key.pubkey.birthtime,
695		(long long)key->key.pubkey.days_valid,
696		key->key.pubkey.version,
697		key->key.pubkey.alg);
698	switch (key->key.pubkey.alg) {
699	case PGP_PKA_DSA:
700		cc += snprintf(&out[cc], outsize - cc,
701			"p=%s\nq=%s\ng=%s\ny=%s\n",
702			BN_bn2hex(key->key.pubkey.key.dsa.p),
703			BN_bn2hex(key->key.pubkey.key.dsa.q),
704			BN_bn2hex(key->key.pubkey.key.dsa.g),
705			BN_bn2hex(key->key.pubkey.key.dsa.y));
706		break;
707	case PGP_PKA_RSA:
708	case PGP_PKA_RSA_ENCRYPT_ONLY:
709	case PGP_PKA_RSA_SIGN_ONLY:
710		cc += snprintf(&out[cc], outsize - cc,
711			"n=%s\ne=%s\n",
712			BN_bn2hex(key->key.pubkey.key.rsa.n),
713			BN_bn2hex(key->key.pubkey.key.rsa.e));
714		break;
715	case PGP_PKA_ELGAMAL:
716	case PGP_PKA_ELGAMAL_ENCRYPT_OR_SIGN:
717		cc += snprintf(&out[cc], outsize - cc,
718			"p=%s\ng=%s\ny=%s\n",
719			BN_bn2hex(key->key.pubkey.key.elgamal.p),
720			BN_bn2hex(key->key.pubkey.key.elgamal.g),
721			BN_bn2hex(key->key.pubkey.key.elgamal.y));
722		break;
723	default:
724		(void) fprintf(stderr,
725			"pgp_print_pubkey: Unusual algorithm\n");
726	}
727	return cc;
728}
729
730/**
731\ingroup Core_Print
732\param type
733\param seckey
734*/
735static void
736print_seckey_verbose(const pgp_content_enum type,
737				const pgp_seckey_t *seckey)
738{
739	printf("------- SECRET KEY or ENCRYPTED SECRET KEY ------\n");
740	print_tagname(0, (type == PGP_PTAG_CT_SECRET_KEY) ?
741			"SECRET_KEY" :
742			"ENCRYPTED_SECRET_KEY");
743	/* pgp_print_pubkey(key); */
744	printf("S2K Usage: %d\n", seckey->s2k_usage);
745	if (seckey->s2k_usage != PGP_S2KU_NONE) {
746		printf("S2K Specifier: %d\n", seckey->s2k_specifier);
747		printf("Symmetric algorithm: %d (%s)\n", seckey->alg,
748		       pgp_show_symm_alg(seckey->alg));
749		printf("Hash algorithm: %d (%s)\n", seckey->hash_alg,
750		       pgp_show_hash_alg((uint8_t)seckey->hash_alg));
751		if (seckey->s2k_specifier != PGP_S2KS_SIMPLE) {
752			print_hexdump(0, "Salt", seckey->salt,
753					(unsigned)sizeof(seckey->salt));
754		}
755		if (seckey->s2k_specifier == PGP_S2KS_ITERATED_AND_SALTED) {
756			printf("Octet count: %u\n", seckey->octetc);
757		}
758		print_hexdump(0, "IV", seckey->iv, pgp_block_size(seckey->alg));
759	}
760	/* no more set if encrypted */
761	if (type == PGP_PTAG_CT_ENCRYPTED_SECRET_KEY) {
762		return;
763	}
764	switch (seckey->pubkey.alg) {
765	case PGP_PKA_RSA:
766		print_bn(0, "d", seckey->key.rsa.d);
767		print_bn(0, "p", seckey->key.rsa.p);
768		print_bn(0, "q", seckey->key.rsa.q);
769		print_bn(0, "u", seckey->key.rsa.u);
770		break;
771
772	case PGP_PKA_DSA:
773		print_bn(0, "x", seckey->key.dsa.x);
774		break;
775
776	default:
777		(void) fprintf(stderr,
778			"print_seckey_verbose: unusual algorithm\n");
779	}
780	if (seckey->s2k_usage == PGP_S2KU_ENCRYPTED_AND_HASHED) {
781		print_hexdump(0, "Checkhash", seckey->checkhash,
782				PGP_CHECKHASH_SIZE);
783	} else {
784		printf("Checksum: %04x\n", seckey->checksum);
785	}
786	printf("------- end of SECRET KEY or ENCRYPTED SECRET KEY ------\n");
787}
788
789
790/**
791\ingroup Core_Print
792\param tag
793\param key
794*/
795static void
796print_pk_sesskey(pgp_content_enum tag,
797			 const pgp_pk_sesskey_t * key)
798{
799	print_tagname(0, (tag == PGP_PTAG_CT_PK_SESSION_KEY) ?
800		"PUBLIC KEY SESSION KEY" :
801		"ENCRYPTED PUBLIC KEY SESSION KEY");
802	printf("Version: %d\n", key->version);
803	print_hexdump(0, "Key ID", key->key_id, (unsigned)sizeof(key->key_id));
804	printf("Algorithm: %d (%s)\n", key->alg,
805	       pgp_show_pka(key->alg));
806	switch (key->alg) {
807	case PGP_PKA_RSA:
808		print_bn(0, "encrypted_m", key->params.rsa.encrypted_m);
809		break;
810
811	case PGP_PKA_ELGAMAL:
812		print_bn(0, "g_to_k", key->params.elgamal.g_to_k);
813		print_bn(0, "encrypted_m", key->params.elgamal.encrypted_m);
814		break;
815
816	default:
817		(void) fprintf(stderr,
818			"print_pk_sesskey: unusual algorithm\n");
819	}
820	if (tag == PGP_PTAG_CT_PK_SESSION_KEY) {
821		printf("Symmetric algorithm: %d (%s)\n", key->symm_alg,
822		       pgp_show_symm_alg(key->symm_alg));
823		print_hexdump(0, "Key", key->key, pgp_key_size(key->symm_alg));
824		printf("Checksum: %04x\n", key->checksum);
825	}
826}
827
828static void
829start_subpacket(int *indent, int type)
830{
831	*indent += 1;
832	print_indent(*indent);
833	printf("-- %s (type 0x%02x)\n",
834	       pgp_show_ss_type((pgp_content_enum)type),
835	       type - PGP_PTAG_SIG_SUBPKT_BASE);
836}
837
838static void
839end_subpacket(int *indent)
840{
841	*indent -= 1;
842}
843
844/**
845\ingroup Core_Print
846\param contents
847*/
848int
849pgp_print_packet(pgp_printstate_t *print, const pgp_packet_t *pkt)
850{
851	const pgp_contents_t	*content = &pkt->u;
852	pgp_text_t		*text;
853	const char		*str;
854
855	if (print->unarmoured && pkt->tag != PGP_PTAG_CT_UNARMOURED_TEXT) {
856		print->unarmoured = 0;
857		puts("UNARMOURED TEXT ends");
858	}
859	if (pkt->tag == PGP_PARSER_PTAG) {
860		printf("=> PGP_PARSER_PTAG: %s\n",
861			pgp_show_packet_tag((pgp_content_enum)content->ptag.type));
862	} else {
863		printf("=> %s\n", pgp_show_packet_tag(pkt->tag));
864	}
865
866	switch (pkt->tag) {
867	case PGP_PARSER_ERROR:
868		printf("parse error: %s\n", content->error);
869		break;
870
871	case PGP_PARSER_ERRCODE:
872		printf("parse error: %s\n",
873		       pgp_errcode(content->errcode.errcode));
874		break;
875
876	case PGP_PARSER_PACKET_END:
877		print_packet_hex(&content->packet);
878		break;
879
880	case PGP_PARSER_PTAG:
881		if (content->ptag.type == PGP_PTAG_CT_PUBLIC_KEY) {
882			print->indent = 0;
883			printf("\n*** NEXT KEY ***\n");
884		}
885		printf("\n");
886		print_indent(print->indent);
887		printf("==== ptag new_format=%u type=%u length_type=%d"
888		       " length=0x%x (%u) position=0x%x (%u)\n",
889		       content->ptag.new_format,
890		       content->ptag.type, content->ptag.length_type,
891		       content->ptag.length, content->ptag.length,
892		       content->ptag.position, content->ptag.position);
893		print_tagname(print->indent, pgp_show_packet_tag((pgp_content_enum)content->ptag.type));
894		break;
895
896	case PGP_PTAG_CT_SE_DATA_HEADER:
897		print_tagname(print->indent, "SYMMETRIC ENCRYPTED DATA");
898		break;
899
900	case PGP_PTAG_CT_SE_IP_DATA_HEADER:
901		print_tagname(print->indent,
902			"SYMMETRIC ENCRYPTED INTEGRITY PROTECTED DATA HEADER");
903		printf("Version: %d\n", content->se_ip_data_header);
904		break;
905
906	case PGP_PTAG_CT_SE_IP_DATA_BODY:
907		print_tagname(print->indent,
908			"SYMMETRIC ENCRYPTED INTEGRITY PROTECTED DATA BODY");
909		hexdump(stdout, "data", content->se_data_body.data,
910			content->se_data_body.length);
911		break;
912
913	case PGP_PTAG_CT_PUBLIC_KEY:
914	case PGP_PTAG_CT_PUBLIC_SUBKEY:
915		print_tagname(print->indent, (pkt->tag == PGP_PTAG_CT_PUBLIC_KEY) ?
916			"PUBLIC KEY" :
917			"PUBLIC SUBKEY");
918		pgp_print_pubkey(&content->pubkey);
919		break;
920
921	case PGP_PTAG_CT_TRUST:
922		print_tagname(print->indent, "TRUST");
923		print_data(print->indent, "Trust", &content->trust);
924		break;
925
926	case PGP_PTAG_CT_USER_ID:
927		print_tagname(print->indent, "USER ID");
928		print_utf8_string(print->indent, "userid", content->userid);
929		break;
930
931	case PGP_PTAG_CT_SIGNATURE:
932		print_tagname(print->indent, "SIGNATURE");
933		print_indent(print->indent);
934		print_uint(print->indent, "Signature Version",
935				   (unsigned)content->sig.info.version);
936		if (content->sig.info.birthtime_set) {
937			print_time(print->indent, "Signature Creation Time",
938				   content->sig.info.birthtime);
939		}
940		if (content->sig.info.duration_set) {
941			print_uint(print->indent, "Signature Duration",
942				   (unsigned)content->sig.info.duration);
943		}
944
945		print_string_and_value(print->indent, "Signature Type",
946			    pgp_show_sig_type(content->sig.info.type),
947				       content->sig.info.type);
948
949		if (content->sig.info.signer_id_set) {
950			hexdump_data(print->indent, "Signer ID",
951					   content->sig.info.signer_id,
952				  (unsigned)sizeof(content->sig.info.signer_id));
953		}
954
955		print_string_and_value(print->indent, "Public Key Algorithm",
956			pgp_show_pka(content->sig.info.key_alg),
957				     content->sig.info.key_alg);
958		print_string_and_value(print->indent, "Hash Algorithm",
959			pgp_show_hash_alg((uint8_t)
960				content->sig.info.hash_alg),
961			(uint8_t)content->sig.info.hash_alg);
962		print_uint(print->indent, "Hashed data len",
963			(unsigned)content->sig.info.v4_hashlen);
964		print_indent(print->indent);
965		hexdump_data(print->indent, "hash2", &content->sig.hash2[0], 2);
966		switch (content->sig.info.key_alg) {
967		case PGP_PKA_RSA:
968		case PGP_PKA_RSA_SIGN_ONLY:
969			print_bn(print->indent, "sig", content->sig.info.sig.rsa.sig);
970			break;
971
972		case PGP_PKA_DSA:
973			print_bn(print->indent, "r", content->sig.info.sig.dsa.r);
974			print_bn(print->indent, "s", content->sig.info.sig.dsa.s);
975			break;
976
977		case PGP_PKA_ELGAMAL_ENCRYPT_OR_SIGN:
978			print_bn(print->indent, "r", content->sig.info.sig.elgamal.r);
979			print_bn(print->indent, "s", content->sig.info.sig.elgamal.s);
980			break;
981
982		default:
983			(void) fprintf(stderr,
984				"pgp_print_packet: Unusual algorithm\n");
985			return 0;
986		}
987
988		if (content->sig.hash)
989			printf("data hash is set\n");
990
991		break;
992
993	case PGP_PTAG_CT_COMPRESSED:
994		print_tagname(print->indent, "COMPRESSED");
995		print_uint(print->indent, "Compressed Data Type",
996			(unsigned)content->compressed);
997		break;
998
999	case PGP_PTAG_CT_1_PASS_SIG:
1000		print_tagname(print->indent, "ONE PASS SIGNATURE");
1001
1002		print_uint(print->indent, "Version", (unsigned)content->one_pass_sig.version);
1003		print_string_and_value(print->indent, "Signature Type",
1004		    pgp_show_sig_type(content->one_pass_sig.sig_type),
1005				       content->one_pass_sig.sig_type);
1006		print_string_and_value(print->indent, "Hash Algorithm",
1007			pgp_show_hash_alg((uint8_t)content->one_pass_sig.hash_alg),
1008			(uint8_t)content->one_pass_sig.hash_alg);
1009		print_string_and_value(print->indent, "Public Key Algorithm",
1010			pgp_show_pka(content->one_pass_sig.key_alg),
1011			content->one_pass_sig.key_alg);
1012		hexdump_data(print->indent, "Signer ID",
1013				   content->one_pass_sig.keyid,
1014				   (unsigned)sizeof(content->one_pass_sig.keyid));
1015		print_uint(print->indent, "Nested", content->one_pass_sig.nested);
1016		break;
1017
1018	case PGP_PTAG_CT_USER_ATTR:
1019		print_tagname(print->indent, "USER ATTRIBUTE");
1020		print_hexdump(print->indent, "User Attribute",
1021			      content->userattr.contents,
1022			      (unsigned)content->userattr.len);
1023		break;
1024
1025	case PGP_PTAG_RAW_SS:
1026		if (pkt->critical) {
1027			(void) fprintf(stderr, "contents are critical\n");
1028			return 0;
1029		}
1030		start_subpacket(&print->indent, pkt->tag);
1031		print_uint(print->indent, "Raw Signature Subpacket: tag",
1032			(unsigned)(content->ss_raw.tag -
1033		   	(unsigned)PGP_PTAG_SIG_SUBPKT_BASE));
1034		print_hexdump(print->indent, "Raw Data",
1035			      content->ss_raw.raw,
1036			      (unsigned)content->ss_raw.length);
1037		break;
1038
1039	case PGP_PTAG_SS_CREATION_TIME:
1040		start_subpacket(&print->indent, pkt->tag);
1041		print_time(print->indent, "Signature Creation Time", content->ss_time);
1042		end_subpacket(&print->indent);
1043		break;
1044
1045	case PGP_PTAG_SS_EXPIRATION_TIME:
1046		start_subpacket(&print->indent, pkt->tag);
1047		print_duration(print->indent, "Signature Expiration Time",
1048			content->ss_time);
1049		end_subpacket(&print->indent);
1050		break;
1051
1052	case PGP_PTAG_SS_KEY_EXPIRY:
1053		start_subpacket(&print->indent, pkt->tag);
1054		print_duration(print->indent, "Key Expiration Time", content->ss_time);
1055		end_subpacket(&print->indent);
1056		break;
1057
1058	case PGP_PTAG_SS_TRUST:
1059		start_subpacket(&print->indent, pkt->tag);
1060		print_string(print->indent, "Trust Signature", "");
1061		print_uint(print->indent, "Level", (unsigned)content->ss_trust.level);
1062		print_uint(print->indent, "Amount", (unsigned)content->ss_trust.amount);
1063		end_subpacket(&print->indent);
1064		break;
1065
1066	case PGP_PTAG_SS_REVOCABLE:
1067		start_subpacket(&print->indent, pkt->tag);
1068		print_boolean(print->indent, "Revocable", content->ss_revocable);
1069		end_subpacket(&print->indent);
1070		break;
1071
1072	case PGP_PTAG_SS_REVOCATION_KEY:
1073		start_subpacket(&print->indent, pkt->tag);
1074		/* not yet tested */
1075		printf("  revocation key: class=0x%x",
1076		       content->ss_revocation_key.class);
1077		if (content->ss_revocation_key.class & 0x40) {
1078			printf(" (sensitive)");
1079		}
1080		printf(", algid=0x%x", content->ss_revocation_key.algid);
1081		hexdump(stdout, "fingerprint", content->ss_revocation_key.fingerprint,
1082				PGP_FINGERPRINT_SIZE);
1083		end_subpacket(&print->indent);
1084		break;
1085
1086	case PGP_PTAG_SS_ISSUER_KEY_ID:
1087		start_subpacket(&print->indent, pkt->tag);
1088		print_hexdump(print->indent, "Issuer Key Id",
1089			      content->ss_issuer, (unsigned)sizeof(content->ss_issuer));
1090		end_subpacket(&print->indent);
1091		break;
1092
1093	case PGP_PTAG_SS_PREFERRED_SKA:
1094		start_subpacket(&print->indent, pkt->tag);
1095		print_data(print->indent, "Preferred Symmetric Algorithms",
1096			   &content->ss_skapref);
1097		text = pgp_showall_ss_skapref(&content->ss_skapref);
1098		print_text_breakdown(print->indent, text);
1099		pgp_text_free(text);
1100
1101		end_subpacket(&print->indent);
1102		break;
1103
1104	case PGP_PTAG_SS_PRIMARY_USER_ID:
1105		start_subpacket(&print->indent, pkt->tag);
1106		print_boolean(print->indent, "Primary User ID",
1107			      content->ss_primary_userid);
1108		end_subpacket(&print->indent);
1109		break;
1110
1111	case PGP_PTAG_SS_PREFERRED_HASH:
1112		start_subpacket(&print->indent, pkt->tag);
1113		print_data(print->indent, "Preferred Hash Algorithms",
1114			   &content->ss_hashpref);
1115		text = pgp_showall_ss_hashpref(&content->ss_hashpref);
1116		print_text_breakdown(print->indent, text);
1117		pgp_text_free(text);
1118		end_subpacket(&print->indent);
1119		break;
1120
1121	case PGP_PTAG_SS_PREF_COMPRESS:
1122		start_subpacket(&print->indent, pkt->tag);
1123		print_data(print->indent, "Preferred Compression Algorithms",
1124			   &content->ss_zpref);
1125		text = pgp_showall_ss_zpref(&content->ss_zpref);
1126		print_text_breakdown(print->indent, text);
1127		pgp_text_free(text);
1128		end_subpacket(&print->indent);
1129		break;
1130
1131	case PGP_PTAG_SS_KEY_FLAGS:
1132		start_subpacket(&print->indent, pkt->tag);
1133		print_data(print->indent, "Key Flags", &content->ss_key_flags);
1134
1135		text = pgp_showall_ss_key_flags(&content->ss_key_flags);
1136		print_text_breakdown(print->indent, text);
1137		pgp_text_free(text);
1138
1139		end_subpacket(&print->indent);
1140		break;
1141
1142	case PGP_PTAG_SS_KEYSERV_PREFS:
1143		start_subpacket(&print->indent, pkt->tag);
1144		print_data(print->indent, "Key Server Preferences",
1145			   &content->ss_key_server_prefs);
1146		text = pgp_show_keyserv_prefs(&content->ss_key_server_prefs);
1147		print_text_breakdown(print->indent, text);
1148		pgp_text_free(text);
1149
1150		end_subpacket(&print->indent);
1151		break;
1152
1153	case PGP_PTAG_SS_FEATURES:
1154		start_subpacket(&print->indent, pkt->tag);
1155		print_data(print->indent, "Features", &content->ss_features);
1156		text = pgp_showall_ss_features(content->ss_features);
1157		print_text_breakdown(print->indent, text);
1158		pgp_text_free(text);
1159
1160		end_subpacket(&print->indent);
1161		break;
1162
1163	case PGP_PTAG_SS_NOTATION_DATA:
1164		start_subpacket(&print->indent, pkt->tag);
1165		print_indent(print->indent);
1166		printf("Notation Data:\n");
1167
1168		print->indent++;
1169		print_data(print->indent, "Flags", &content->ss_notation.flags);
1170		text = pgp_showall_notation(content->ss_notation);
1171		print_text_breakdown(print->indent, text);
1172		pgp_text_free(text);
1173
1174		print_data(print->indent, "Name", &content->ss_notation.name);
1175
1176		print_data(print->indent, "Value", &content->ss_notation.value);
1177
1178		print->indent--;
1179		end_subpacket(&print->indent);
1180		break;
1181
1182	case PGP_PTAG_SS_REGEXP:
1183		start_subpacket(&print->indent, pkt->tag);
1184		print_hexdump(print->indent, "Regular Expression",
1185			      (uint8_t *) content->ss_regexp,
1186			      (unsigned)strlen(content->ss_regexp));
1187		print_string(print->indent, NULL, content->ss_regexp);
1188		end_subpacket(&print->indent);
1189		break;
1190
1191	case PGP_PTAG_SS_POLICY_URI:
1192		start_subpacket(&print->indent, pkt->tag);
1193		print_string(print->indent, "Policy URL", content->ss_policy);
1194		end_subpacket(&print->indent);
1195		break;
1196
1197	case PGP_PTAG_SS_SIGNERS_USER_ID:
1198		start_subpacket(&print->indent, pkt->tag);
1199		print_utf8_string(print->indent, "Signer's User ID", content->ss_signer);
1200		end_subpacket(&print->indent);
1201		break;
1202
1203	case PGP_PTAG_SS_PREF_KEYSERV:
1204		start_subpacket(&print->indent, pkt->tag);
1205		print_string(print->indent, "Preferred Key Server", content->ss_keyserv);
1206		end_subpacket(&print->indent);
1207		break;
1208
1209	case PGP_PTAG_SS_EMBEDDED_SIGNATURE:
1210		start_subpacket(&print->indent, pkt->tag);
1211		end_subpacket(&print->indent);/* \todo print out contents? */
1212		break;
1213
1214	case PGP_PTAG_SS_USERDEFINED00:
1215	case PGP_PTAG_SS_USERDEFINED01:
1216	case PGP_PTAG_SS_USERDEFINED02:
1217	case PGP_PTAG_SS_USERDEFINED03:
1218	case PGP_PTAG_SS_USERDEFINED04:
1219	case PGP_PTAG_SS_USERDEFINED05:
1220	case PGP_PTAG_SS_USERDEFINED06:
1221	case PGP_PTAG_SS_USERDEFINED07:
1222	case PGP_PTAG_SS_USERDEFINED08:
1223	case PGP_PTAG_SS_USERDEFINED09:
1224	case PGP_PTAG_SS_USERDEFINED10:
1225		start_subpacket(&print->indent, pkt->tag);
1226		print_hexdump(print->indent, "Internal or user-defined",
1227			      content->ss_userdef.contents,
1228			      (unsigned)content->ss_userdef.len);
1229		end_subpacket(&print->indent);
1230		break;
1231
1232	case PGP_PTAG_SS_RESERVED:
1233		start_subpacket(&print->indent, pkt->tag);
1234		print_hexdump(print->indent, "Reserved",
1235			      content->ss_userdef.contents,
1236			      (unsigned)content->ss_userdef.len);
1237		end_subpacket(&print->indent);
1238		break;
1239
1240	case PGP_PTAG_SS_REVOCATION_REASON:
1241		start_subpacket(&print->indent, pkt->tag);
1242		print_hexdump(print->indent, "Revocation Reason",
1243			      &content->ss_revocation.code,
1244			      1);
1245		str = pgp_show_ss_rr_code(content->ss_revocation.code);
1246		print_string(print->indent, NULL, str);
1247		end_subpacket(&print->indent);
1248		break;
1249
1250	case PGP_PTAG_CT_LITDATA_HEADER:
1251		print_tagname(print->indent, "LITERAL DATA HEADER");
1252		printf("  literal data header format=%c filename='%s'\n",
1253		       content->litdata_header.format,
1254		       content->litdata_header.filename);
1255		showtime("    modification time",
1256			 content->litdata_header.mtime);
1257		printf("\n");
1258		break;
1259
1260	case PGP_PTAG_CT_LITDATA_BODY:
1261		print_tagname(print->indent, "LITERAL DATA BODY");
1262		printf("  literal data body length=%u\n",
1263		       content->litdata_body.length);
1264		printf("    data=");
1265		print_escaped(content->litdata_body.data,
1266			      content->litdata_body.length);
1267		printf("\n");
1268		break;
1269
1270	case PGP_PTAG_CT_SIGNATURE_HEADER:
1271		print_tagname(print->indent, "SIGNATURE");
1272		print_indent(print->indent);
1273		print_uint(print->indent, "Signature Version",
1274				   (unsigned)content->sig.info.version);
1275		if (content->sig.info.birthtime_set) {
1276			print_time(print->indent, "Signature Creation Time",
1277				content->sig.info.birthtime);
1278		}
1279		if (content->sig.info.duration_set) {
1280			print_uint(print->indent, "Signature Duration",
1281				   (unsigned)content->sig.info.duration);
1282		}
1283		print_string_and_value(print->indent, "Signature Type",
1284			    pgp_show_sig_type(content->sig.info.type),
1285				       content->sig.info.type);
1286		if (content->sig.info.signer_id_set) {
1287			hexdump_data(print->indent, "Signer ID",
1288				content->sig.info.signer_id,
1289				(unsigned)sizeof(content->sig.info.signer_id));
1290		}
1291		print_string_and_value(print->indent, "Public Key Algorithm",
1292			pgp_show_pka(content->sig.info.key_alg),
1293				     content->sig.info.key_alg);
1294		print_string_and_value(print->indent, "Hash Algorithm",
1295			pgp_show_hash_alg((uint8_t)content->sig.info.hash_alg),
1296			(uint8_t)content->sig.info.hash_alg);
1297		print_uint(print->indent, "Hashed data len",
1298			(unsigned)content->sig.info.v4_hashlen);
1299
1300		break;
1301
1302	case PGP_PTAG_CT_SIGNATURE_FOOTER:
1303		print_indent(print->indent);
1304		hexdump_data(print->indent, "hash2", &content->sig.hash2[0], 2);
1305
1306		switch (content->sig.info.key_alg) {
1307		case PGP_PKA_RSA:
1308			print_bn(print->indent, "sig", content->sig.info.sig.rsa.sig);
1309			break;
1310
1311		case PGP_PKA_DSA:
1312			print_bn(print->indent, "r", content->sig.info.sig.dsa.r);
1313			print_bn(print->indent, "s", content->sig.info.sig.dsa.s);
1314			break;
1315
1316		case PGP_PKA_ELGAMAL_ENCRYPT_OR_SIGN:
1317			print_bn(print->indent, "r", content->sig.info.sig.elgamal.r);
1318			print_bn(print->indent, "s", content->sig.info.sig.elgamal.s);
1319			break;
1320
1321		case PGP_PKA_PRIVATE00:
1322		case PGP_PKA_PRIVATE01:
1323		case PGP_PKA_PRIVATE02:
1324		case PGP_PKA_PRIVATE03:
1325		case PGP_PKA_PRIVATE04:
1326		case PGP_PKA_PRIVATE05:
1327		case PGP_PKA_PRIVATE06:
1328		case PGP_PKA_PRIVATE07:
1329		case PGP_PKA_PRIVATE08:
1330		case PGP_PKA_PRIVATE09:
1331		case PGP_PKA_PRIVATE10:
1332			print_data(print->indent, "Private/Experimental",
1333			   &content->sig.info.sig.unknown);
1334			break;
1335
1336		default:
1337			(void) fprintf(stderr,
1338				"pgp_print_packet: Unusual key algorithm\n");
1339			return 0;
1340		}
1341		break;
1342
1343	case PGP_GET_PASSPHRASE:
1344		print_tagname(print->indent, "PGP_GET_PASSPHRASE");
1345		break;
1346
1347	case PGP_PTAG_CT_SECRET_KEY:
1348		print_tagname(print->indent, "PGP_PTAG_CT_SECRET_KEY");
1349		print_seckey_verbose(pkt->tag, &content->seckey);
1350		break;
1351
1352	case PGP_PTAG_CT_ENCRYPTED_SECRET_KEY:
1353		print_tagname(print->indent, "PGP_PTAG_CT_ENCRYPTED_SECRET_KEY");
1354		print_seckey_verbose(pkt->tag, &content->seckey);
1355		break;
1356
1357	case PGP_PTAG_CT_ARMOUR_HEADER:
1358		print_tagname(print->indent, "ARMOUR HEADER");
1359		print_string(print->indent, "type", content->armour_header.type);
1360		break;
1361
1362	case PGP_PTAG_CT_SIGNED_CLEARTEXT_HEADER:
1363		print_tagname(print->indent, "SIGNED CLEARTEXT HEADER");
1364		print_headers(&content->cleartext_head);
1365		break;
1366
1367	case PGP_PTAG_CT_SIGNED_CLEARTEXT_BODY:
1368		print_tagname(print->indent, "SIGNED CLEARTEXT BODY");
1369		print_block(print->indent, "signed cleartext", content->cleartext_body.data,
1370			    content->cleartext_body.length);
1371		break;
1372
1373	case PGP_PTAG_CT_SIGNED_CLEARTEXT_TRAILER:
1374		print_tagname(print->indent, "SIGNED CLEARTEXT TRAILER");
1375		printf("hash algorithm: %d\n",
1376		       content->cleartext_trailer->alg);
1377		printf("\n");
1378		break;
1379
1380	case PGP_PTAG_CT_UNARMOURED_TEXT:
1381		if (!print->unarmoured) {
1382			print_tagname(print->indent, "UNARMOURED TEXT");
1383			print->unarmoured = 1;
1384		}
1385		putchar('[');
1386		print_escaped(content->unarmoured_text.data,
1387			      content->unarmoured_text.length);
1388		putchar(']');
1389		break;
1390
1391	case PGP_PTAG_CT_ARMOUR_TRAILER:
1392		print_tagname(print->indent, "ARMOUR TRAILER");
1393		print_string(print->indent, "type", content->armour_header.type);
1394		break;
1395
1396	case PGP_PTAG_CT_PK_SESSION_KEY:
1397	case PGP_PTAG_CT_ENCRYPTED_PK_SESSION_KEY:
1398		print_pk_sesskey(pkt->tag, &content->pk_sesskey);
1399		break;
1400
1401	case PGP_GET_SECKEY:
1402		print_pk_sesskey(PGP_PTAG_CT_ENCRYPTED_PK_SESSION_KEY,
1403				    content->get_seckey.pk_sesskey);
1404		break;
1405
1406	default:
1407		print_tagname(print->indent, "UNKNOWN PACKET TYPE");
1408		fprintf(stderr, "pgp_print_packet: unknown tag=%d (0x%x)\n",
1409			pkt->tag, pkt->tag);
1410		return 0;
1411	}
1412	return 1;
1413}
1414
1415static pgp_cb_ret_t
1416cb_list_packets(const pgp_packet_t *pkt, pgp_cbdata_t *cbinfo)
1417{
1418	pgp_print_packet(&cbinfo->printstate, pkt);
1419	return PGP_RELEASE_MEMORY;
1420}
1421
1422/**
1423\ingroup Core_Print
1424\param filename
1425\param armour
1426\param keyring
1427\param cb_get_passphrase
1428*/
1429int
1430pgp_list_packets(pgp_io_t *io,
1431			char *filename,
1432			unsigned armour,
1433			pgp_keyring_t *secring,
1434			pgp_keyring_t *pubring,
1435			void *passfp,
1436			pgp_cbfunc_t *cb_get_passphrase)
1437{
1438	pgp_stream_t	*stream = NULL;
1439	const unsigned	 accumulate = 1;
1440	const int	 printerrors = 1;
1441	int		 fd;
1442
1443	fd = pgp_setup_file_read(io, &stream, filename, NULL, cb_list_packets,
1444				accumulate);
1445	pgp_parse_options(stream, PGP_PTAG_SS_ALL, PGP_PARSE_PARSED);
1446	stream->cryptinfo.secring = secring;
1447	stream->cryptinfo.pubring = pubring;
1448	stream->cbinfo.passfp = passfp;
1449	stream->cryptinfo.getpassphrase = cb_get_passphrase;
1450	if (armour) {
1451		pgp_reader_push_dearmour(stream);
1452	}
1453	pgp_parse(stream, printerrors);
1454	pgp_teardown_file_read(stream, fd);
1455	return 1;
1456}
1457