xref: /netbsd-src/crypto/external/bsd/netpgp/dist/src/lib/packet-show.c (revision da9817918ec7e88db2912a2882967c7570a83f47)
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 /** \file
51  *
52  * Creates printable text strings from packet contents
53  *
54  */
55 #include "config.h"
56 
57 #ifdef HAVE_SYS_CDEFS_H
58 #include <sys/cdefs.h>
59 #endif
60 
61 #if defined(__NetBSD__)
62 __COPYRIGHT("@(#) Copyright (c) 2009 The NetBSD Foundation, Inc. All rights reserved.");
63 __RCSID("$NetBSD: packet-show.c,v 1.12 2009/06/11 01:12:42 agc Exp $");
64 #endif
65 
66 #include <stdlib.h>
67 #include <string.h>
68 
69 #include "packet-show.h"
70 
71 #include "netpgpdefs.h"
72 
73 
74 /*
75  * Arrays of value->text maps
76  */
77 
78 static __ops_map_t packet_tag_map[] =
79 {
80 	{OPS_PTAG_CT_RESERVED, "Reserved"},
81 	{OPS_PTAG_CT_PK_SESSION_KEY, "Public-Key Encrypted Session Key"},
82 	{OPS_PTAG_CT_SIGNATURE, "Signature"},
83 	{OPS_PTAG_CT_SK_SESSION_KEY, "Symmetric-Key Encrypted Session Key"},
84 	{OPS_PTAG_CT_1_PASS_SIG, "One-Pass Signature"},
85 	{OPS_PTAG_CT_SECRET_KEY, "Secret Key"},
86 	{OPS_PTAG_CT_PUBLIC_KEY, "Public Key"},
87 	{OPS_PTAG_CT_SECRET_SUBKEY, "Secret Subkey"},
88 	{OPS_PTAG_CT_COMPRESSED, "Compressed Data"},
89 	{OPS_PTAG_CT_SE_DATA, "Symmetrically Encrypted Data"},
90 	{OPS_PTAG_CT_MARKER, "Marker"},
91 	{OPS_PTAG_CT_LITDATA, "Literal Data"},
92 	{OPS_PTAG_CT_TRUST, "Trust"},
93 	{OPS_PTAG_CT_USER_ID, "User ID"},
94 	{OPS_PTAG_CT_PUBLIC_SUBKEY, "Public Subkey"},
95 	{OPS_PTAG_CT_RESERVED2, "reserved2"},
96 	{OPS_PTAG_CT_RESERVED3, "reserved3"},
97 	{OPS_PTAG_CT_USER_ATTR, "User Attribute"},
98 	{OPS_PTAG_CT_SE_IP_DATA,
99 		"Symmetric Encrypted and Integrity Protected Data"},
100 	{OPS_PTAG_CT_MDC, "Modification Detection Code"},
101 	{OPS_PARSER_PTAG, "OPS_PARSER_PTAG"},
102 	{OPS_PTAG_RAW_SS, "OPS_PTAG_RAW_SS"},
103 	{OPS_PTAG_SS_ALL, "OPS_PTAG_SS_ALL"},
104 	{OPS_PARSER_PACKET_END, "OPS_PARSER_PACKET_END"},
105 	{OPS_PTAG_SIG_SUBPKT_BASE, "OPS_PTAG_SIG_SUBPKT_BASE"},
106 	{OPS_PTAG_SS_CREATION_TIME, "SS: Signature Creation Time"},
107 	{OPS_PTAG_SS_EXPIRATION_TIME, "SS: Signature Expiration Time"},
108 	{OPS_PTAG_SS_EXPORT_CERT, "SS: Exportable Certification"},
109 	{OPS_PTAG_SS_TRUST, "SS: Trust Signature"},
110 	{OPS_PTAG_SS_REGEXP, "SS: Regular Expression"},
111 	{OPS_PTAG_SS_REVOCABLE, "SS: Revocable"},
112 	{OPS_PTAG_SS_KEY_EXPIRY, "SS: Key Expiration Time"},
113 	{OPS_PTAG_SS_RESERVED, "SS: Reserved"},
114 	{OPS_PTAG_SS_PREFERRED_SKA, "SS: Preferred Secret Key Algorithm"},
115 	{OPS_PTAG_SS_REVOCATION_KEY, "SS: Revocation Key"},
116 	{OPS_PTAG_SS_ISSUER_KEY_ID, "SS: Issuer Key Id"},
117 	{OPS_PTAG_SS_NOTATION_DATA, "SS: Notation Data"},
118 	{OPS_PTAG_SS_PREFERRED_HASH, "SS: Preferred Hash Algorithm"},
119 	{OPS_PTAG_SS_PREF_COMPRESS, "SS: Preferred Compression Algorithm"},
120 	{OPS_PTAG_SS_KEYSERV_PREFS, "SS: Key Server Preferences"},
121 	{OPS_PTAG_SS_PREF_KEYSERV, "SS: Preferred Key Server"},
122 	{OPS_PTAG_SS_PRIMARY_USER_ID, "SS: Primary User ID"},
123 	{OPS_PTAG_SS_POLICY_URI, "SS: Policy URI"},
124 	{OPS_PTAG_SS_KEY_FLAGS, "SS: Key Flags"},
125 	{OPS_PTAG_SS_SIGNERS_USER_ID, "SS: Signer's User ID"},
126 	{OPS_PTAG_SS_REVOCATION_REASON, "SS: Reason for Revocation"},
127 	{OPS_PTAG_SS_FEATURES, "SS: Features"},
128 	{OPS_PTAG_SS_SIGNATURE_TARGET, "SS: Signature Target"},
129 	{OPS_PTAG_SS_EMBEDDED_SIGNATURE, "SS: Embedded Signature"},
130 
131 	{OPS_PTAG_CT_LITDATA_HEADER, "CT: Literal Data Header"},
132 	{OPS_PTAG_CT_LITDATA_BODY, "CT: Literal Data Body"},
133 	{OPS_PTAG_CT_SIGNATURE_HEADER, "CT: Signature Header"},
134 	{OPS_PTAG_CT_SIGNATURE_FOOTER, "CT: Signature Footer"},
135 	{OPS_PTAG_CT_ARMOUR_HEADER, "CT: Armour Header"},
136 	{OPS_PTAG_CT_ARMOUR_TRAILER, "CT: Armour Trailer"},
137 	{OPS_PTAG_CT_SIGNED_CLEARTEXT_HEADER, "CT: Signed Cleartext Header"},
138 	{OPS_PTAG_CT_SIGNED_CLEARTEXT_BODY, "CT: Signed Cleartext Body"},
139 	{OPS_PTAG_CT_SIGNED_CLEARTEXT_TRAILER, "CT: Signed Cleartext Trailer"},
140 	{OPS_PTAG_CT_UNARMOURED_TEXT, "CT: Unarmoured Text"},
141 	{OPS_PTAG_CT_ENCRYPTED_SECRET_KEY, "CT: Encrypted Secret Key"},
142 	{OPS_PTAG_CT_SE_DATA_HEADER, "CT: Sym Encrypted Data Header"},
143 	{OPS_PTAG_CT_SE_DATA_BODY, "CT: Sym Encrypted Data Body"},
144 	{OPS_PTAG_CT_SE_IP_DATA_HEADER, "CT: Sym Encrypted IP Data Header"},
145 	{OPS_PTAG_CT_SE_IP_DATA_BODY, "CT: Sym Encrypted IP Data Body"},
146 	{OPS_PTAG_CT_ENCRYPTED_PK_SESSION_KEY, "CT: Encrypted PK Session Key"},
147 	{OPS_GET_PASSPHRASE, "CMD: Get Secret Key Passphrase"},
148 	{OPS_GET_SECKEY, "CMD: Get Secret Key"},
149 	{OPS_PARSER_ERROR, "OPS_PARSER_ERROR"},
150 	{OPS_PARSER_ERRCODE, "OPS_PARSER_ERRCODE"},
151 
152 	{0x00, NULL},		/* this is the end-of-array marker */
153 };
154 typedef __ops_map_t packet_tag_map_t;
155 
156 static __ops_map_t ss_type_map[] =
157 {
158 	{OPS_PTAG_SS_CREATION_TIME, "Signature Creation Time"},
159 	{OPS_PTAG_SS_EXPIRATION_TIME, "Signature Expiration Time"},
160 	{OPS_PTAG_SS_TRUST, "Trust Signature"},
161 	{OPS_PTAG_SS_REGEXP, "Regular Expression"},
162 	{OPS_PTAG_SS_REVOCABLE, "Revocable"},
163 	{OPS_PTAG_SS_KEY_EXPIRY, "Key Expiration Time"},
164 	{OPS_PTAG_SS_PREFERRED_SKA, "Preferred Symmetric Algorithms"},
165 	{OPS_PTAG_SS_REVOCATION_KEY, "Revocation Key"},
166 	{OPS_PTAG_SS_ISSUER_KEY_ID, "Issuer key ID"},
167 	{OPS_PTAG_SS_NOTATION_DATA, "Notation Data"},
168 	{OPS_PTAG_SS_PREFERRED_HASH, "Preferred Hash Algorithms"},
169 	{OPS_PTAG_SS_PREF_COMPRESS, "Preferred Compression Algorithms"},
170 	{OPS_PTAG_SS_KEYSERV_PREFS, "Key Server Preferences"},
171 	{OPS_PTAG_SS_PREF_KEYSERV, "Preferred Key Server"},
172 	{OPS_PTAG_SS_PRIMARY_USER_ID, "Primary User ID"},
173 	{OPS_PTAG_SS_POLICY_URI, "Policy URI"},
174 	{OPS_PTAG_SS_KEY_FLAGS, "Key Flags"},
175 	{OPS_PTAG_SS_REVOCATION_REASON, "Reason for Revocation"},
176 	{OPS_PTAG_SS_FEATURES, "Features"},
177 	{0x00, NULL},		/* this is the end-of-array marker */
178 };
179 typedef __ops_map_t ss_type_map_t;
180 
181 
182 static __ops_map_t ss_rr_code_map[] =
183 {
184 	{0x00, "No reason specified"},
185 	{0x01, "Key is superseded"},
186 	{0x02, "Key material has been compromised"},
187 	{0x03, "Key is retired and no longer used"},
188 	{0x20, "User ID information is no longer valid"},
189 	{0x00, NULL},		/* this is the end-of-array marker */
190 };
191 typedef __ops_map_t ss_rr_code_map_t;
192 
193 static __ops_map_t sig_type_map[] =
194 {
195 	{OPS_SIG_BINARY, "Signature of a binary document"},
196 	{OPS_SIG_TEXT, "Signature of a canonical text document"},
197 	{OPS_SIG_STANDALONE, "Standalone signature"},
198 	{OPS_CERT_GENERIC, "Generic certification of a User ID and Public Key packet"},
199 	{OPS_CERT_PERSONA, "Personal certification of a User ID and Public Key packet"},
200 	{OPS_CERT_CASUAL, "Casual certification of a User ID and Public Key packet"},
201 	{OPS_CERT_POSITIVE, "Positive certification of a User ID and Public Key packet"},
202 	{OPS_SIG_SUBKEY, "Subkey Binding Signature"},
203 	{OPS_SIG_PRIMARY, "Primary Key Binding Signature"},
204 	{OPS_SIG_DIRECT, "Signature directly on a key"},
205 	{OPS_SIG_REV_KEY, "Key revocation signature"},
206 	{OPS_SIG_REV_SUBKEY, "Subkey revocation signature"},
207 	{OPS_SIG_REV_CERT, "Certification revocation signature"},
208 	{OPS_SIG_TIMESTAMP, "Timestamp signature"},
209 	{OPS_SIG_3RD_PARTY, "Third-Party Confirmation signature"},
210 	{0x00, NULL},		/* this is the end-of-array marker */
211 };
212 typedef __ops_map_t sig_type_map_t;
213 
214 static __ops_map_t pubkey_alg_map[] =
215 {
216 	{OPS_PKA_RSA, "RSA (Encrypt or Sign)"},
217 	{OPS_PKA_RSA_ENCRYPT_ONLY, "RSA Encrypt-Only"},
218 	{OPS_PKA_RSA_SIGN_ONLY, "RSA Sign-Only"},
219 	{OPS_PKA_ELGAMAL, "Elgamal (Encrypt-Only)"},
220 	{OPS_PKA_DSA, "DSA"},
221 	{OPS_PKA_RESERVED_ELLIPTIC_CURVE, "Reserved for Elliptic Curve"},
222 	{OPS_PKA_RESERVED_ECDSA, "Reserved for ECDSA"},
223 	{OPS_PKA_ELGAMAL_ENCRYPT_OR_SIGN, "Reserved (formerly Elgamal Encrypt or Sign"},
224 	{OPS_PKA_RESERVED_DH, "Reserved for Diffie-Hellman (X9.42)"},
225 	{OPS_PKA_PRIVATE00, "Private/Experimental"},
226 	{OPS_PKA_PRIVATE01, "Private/Experimental"},
227 	{OPS_PKA_PRIVATE02, "Private/Experimental"},
228 	{OPS_PKA_PRIVATE03, "Private/Experimental"},
229 	{OPS_PKA_PRIVATE04, "Private/Experimental"},
230 	{OPS_PKA_PRIVATE05, "Private/Experimental"},
231 	{OPS_PKA_PRIVATE06, "Private/Experimental"},
232 	{OPS_PKA_PRIVATE07, "Private/Experimental"},
233 	{OPS_PKA_PRIVATE08, "Private/Experimental"},
234 	{OPS_PKA_PRIVATE09, "Private/Experimental"},
235 	{OPS_PKA_PRIVATE10, "Private/Experimental"},
236 	{0x00, NULL},		/* this is the end-of-array marker */
237 };
238 typedef __ops_map_t pubkey_alg_map_t;
239 
240 static __ops_map_t symm_alg_map[] =
241 {
242 	{OPS_SA_PLAINTEXT, "Plaintext or unencrypted data"},
243 	{OPS_SA_IDEA, "IDEA"},
244 	{OPS_SA_TRIPLEDES, "TripleDES"},
245 	{OPS_SA_CAST5, "CAST5"},
246 	{OPS_SA_BLOWFISH, "Blowfish"},
247 	{OPS_SA_AES_128, "AES (128-bit key)"},
248 	{OPS_SA_AES_192, "AES (192-bit key)"},
249 	{OPS_SA_AES_256, "AES (256-bit key)"},
250 	{OPS_SA_TWOFISH, "Twofish(256-bit key)"},
251 	{0x00, NULL},		/* this is the end-of-array marker */
252 };
253 
254 static __ops_map_t hash_alg_map[] =
255 {
256 	{OPS_HASH_MD5, "MD5"},
257 	{OPS_HASH_SHA1, "SHA1"},
258 	{OPS_HASH_RIPEMD, "RIPEMD160"},
259 	{OPS_HASH_SHA256, "SHA256"},
260 	{OPS_HASH_SHA384, "SHA384"},
261 	{OPS_HASH_SHA512, "SHA512"},
262 	{OPS_HASH_SHA224, "SHA224"},
263 	{0x00, NULL},		/* this is the end-of-array marker */
264 };
265 
266 static __ops_map_t compression_alg_map[] =
267 {
268 	{OPS_C_NONE, "Uncompressed"},
269 	{OPS_C_ZIP, "ZIP(RFC1951)"},
270 	{OPS_C_ZLIB, "ZLIB(RFC1950)"},
271 	{OPS_C_BZIP2, "Bzip2(BZ2)"},
272 	{0x00, NULL},		/* this is the end-of-array marker */
273 };
274 
275 static __ops_bit_map_t ss_notation_map_byte0[] =
276 {
277 	{0x80, "Human-readable"},
278 	{0x00, NULL},
279 };
280 
281 static __ops_bit_map_t *ss_notation_map[] =
282 {
283 	ss_notation_map_byte0,
284 };
285 
286 static __ops_bit_map_t ss_feature_map_byte0[] =
287 {
288 	{0x01, "Modification Detection"},
289 	{0x00, NULL},
290 };
291 
292 static __ops_bit_map_t *ss_feature_map[] =
293 {
294 	ss_feature_map_byte0,
295 };
296 
297 static __ops_bit_map_t ss_key_flags_map[] =
298 {
299 	{0x01, "May be used to certify other keys"},
300 	{0x02, "May be used to sign data"},
301 	{0x04, "May be used to encrypt communications"},
302 	{0x08, "May be used to encrypt storage"},
303 	{0x10, "Private component may have been split by a secret-sharing mechanism"},
304 	{0x80, "Private component may be in possession of more than one person"},
305 	{0x00, NULL},
306 };
307 
308 static __ops_bit_map_t ss_key_server_prefs_map[] =
309 {
310 	{0x80, "Key holder requests that this key only be modified or updated by the key holder or an administrator of the key server"},
311 	{0x00, NULL},
312 };
313 
314 /*
315  * Private functions
316  */
317 
318 static void
319 list_init(__ops_list_t *list)
320 {
321 	list->size = 0;
322 	list->used = 0;
323 	list->strings = NULL;
324 }
325 
326 static void
327 list_free_strings(__ops_list_t *list)
328 {
329 	unsigned        i;
330 
331 	for (i = 0; i < list->used; i++) {
332 		free(list->strings[i]);
333 		list->strings[i] = NULL;
334 	}
335 }
336 
337 static void
338 list_free(__ops_list_t *list)
339 {
340 	if (list->strings)
341 		free(list->strings);
342 	list_init(list);
343 }
344 
345 static unsigned int
346 list_resize(__ops_list_t *list)
347 {
348 	/*
349 	 * We only resize in one direction - upwards. Algorithm used : double
350 	 * the current size then add 1
351 	 */
352 
353 	int             newsize = 0;
354 
355 	newsize = list->size * 2 + 1;
356 	list->strings = realloc(list->strings, newsize * sizeof(char *));
357 	if (list->strings) {
358 		list->size = newsize;
359 		return 1;
360 	} else {
361 		/* xxx - realloc failed. error message? - rachel */
362 		return 0;
363 	}
364 }
365 
366 static unsigned int
367 add_str(__ops_list_t *list, const char *str)
368 {
369 	if (list->size == list->used)
370 		if (!list_resize(list))
371 			return 0;
372 
373 	list->strings[list->used] = __UNCONST(str);
374 	list->used++;
375 	return 1;
376 }
377 
378 /* find a bitfield in a map - serial search */
379 static const char *
380 find_bitfield(__ops_bit_map_t *map, unsigned char octet)
381 {
382 	__ops_bit_map_t  *row;
383 
384 	for (row = map; row->string != NULL && row->mask != octet ; row++) {
385 	}
386 	return (row->string) ? row->string : "Unknown";
387 }
388 
389 /* ! generic function to initialise __ops_text_t structure */
390 void
391 __ops_text_init(__ops_text_t *text)
392 {
393 	list_init(&text->known);
394 	list_init(&text->unknown);
395 }
396 
397 /**
398  * \ingroup Core_Print
399  *
400  * __ops_text_free() frees the memory used by an __ops_text_t structure
401  *
402  * \param text Pointer to a previously allocated structure. This structure and its contents will be freed.
403  */
404 void
405 __ops_text_free(__ops_text_t *text)
406 {
407 	/* Strings in "known" array will be constants, so don't free them */
408 	list_free(&text->known);
409 
410 	/*
411 	 * Strings in "unknown" array will be dynamically allocated, so do
412 	 * free them
413 	 */
414 	list_free_strings(&text->unknown);
415 	list_free(&text->unknown);
416 
417 	(void) free(text);
418 }
419 
420 /* XXX: should this (and many others) be unsigned? */
421 /* ! generic function which adds text derived from single octet map to text */
422 static unsigned
423 add_str_from_octet_map(__ops_text_t *map, char *str, unsigned char octet)
424 {
425 	if (str && !add_str(&map->known, str)) {
426 		/*
427 		 * value recognised, but there was a problem adding it to the
428 		 * list
429 		 */
430 		/* XXX - should print out error msg here, Ben? - rachel */
431 		return 0;
432 	} else if (!str) {
433 		/*
434 		 * value not recognised and there was a problem adding it to
435 		 * the unknown list
436 		 */
437 		unsigned        len = 2 + 2 + 1;	/* 2 for "0x", 2 for
438 							 * single octet in hex
439 							 * format, 1 for NUL */
440 		str = calloc(1, len);
441 		(void) snprintf(str, len, "0x%x", octet);
442 		if (!add_str(&map->unknown, str)) {
443 			return 0;
444 		}
445 	}
446 	return 1;
447 }
448 
449 /* ! generic function which adds text derived from single bit map to text */
450 static unsigned int
451 add_bitmap_entry(__ops_text_t *map, const char *str, unsigned char bit)
452 {
453 	const char     *fmt_unknown = "Unknown bit(0x%x)";
454 
455 	if (str && !add_str(&map->known, str)) {
456 		/*
457 		 * value recognised, but there was a problem adding it to the
458 		 * list
459 		 */
460 		/* XXX - should print out error msg here, Ben? - rachel */
461 		return 0;
462 	} else if (!str) {
463 		/*
464 		 * value not recognised and there was a problem adding it to
465 		 * the unknown list
466 		 * 2 chars of the string are the format definition, this will
467 		 * be replaced in the output by 2 chars of hex, so the length
468 		 * will be correct
469 		 */
470 		unsigned        len = strlen(fmt_unknown) + 1;
471 
472 		str = calloc(1, len);
473 		(void) snprintf(__UNCONST(str), len, fmt_unknown, bit);
474 		if (!add_str(&map->unknown, str)) {
475 			return 0;
476 		}
477 	}
478 	return 1;
479 }
480 
481 /**
482  * Produce a structure containing human-readable textstrings
483  * representing the recognised and unrecognised contents
484  * of this byte array. text_fn() will be called on each octet in turn.
485  * Each octet will generate one string representing the whole byte.
486  *
487  */
488 
489 static __ops_text_t *
490 text_from_bytemapped_octets(__ops_data_t *data,
491 			    const char *(*text_fn)(unsigned char octet))
492 {
493 
494 	__ops_text_t     *text = NULL;
495 	const char     *str;
496 	unsigned        i;
497 
498 	/*
499 	 * ! allocate and initialise __ops_text_t structure to store derived
500 	 * strings
501 	 */
502 	text = calloc(1, sizeof(__ops_text_t));
503 	if (!text) {
504 		return NULL;
505 	}
506 
507 	__ops_text_init(text);
508 
509 	/* ! for each octet in field ... */
510 	for (i = 0; i < data->len; i++) {
511 		/* ! derive string from octet */
512 		str = (*text_fn) (data->contents[i]);
513 
514 		/* ! and add to text */
515 		if (!add_str_from_octet_map(text, strdup(str),
516 						data->contents[i])) {
517 			__ops_text_free(text);
518 			return NULL;
519 		}
520 	}
521 	/*
522 	 * ! All values have been added to either the known or the unknown
523 	 * list
524 	 */
525 	return text;
526 }
527 
528 /**
529  * Produce a structure containing human-readable textstrings
530  * representing the recognised and unrecognised contents
531  * of this byte array, derived from each bit of each octet.
532  *
533  */
534 static __ops_text_t *
535 showall_octets_bits(__ops_data_t *data, __ops_bit_map_t **map,
536 		    size_t nmap)
537 {
538 	unsigned char	 mask, bit;
539 	__ops_text_t	*text = NULL;
540 	const char	*str;
541 	unsigned         i;
542 	int              j = 0;
543 
544 	/*
545 	 * ! allocate and initialise __ops_text_t structure to store derived
546 	 * strings
547 	 */
548 	text = calloc(1, sizeof(__ops_text_t));
549 	if (!text) {
550 		return NULL;
551 	}
552 
553 	__ops_text_init(text);
554 
555 	/* ! for each octet in field ... */
556 	for (i = 0; i < data->len; i++) {
557 		/* ! for each bit in octet ... */
558 		mask = 0x80;
559 		for (j = 0; j < 8; j++, mask = (unsigned)mask >> 1) {
560 			bit = data->contents[i] & mask;
561 			if (bit) {
562 				str = (i >= nmap) ? "Unknown" :
563 					find_bitfield(map[i], bit);
564 				if (!add_bitmap_entry(text, str, bit)) {
565 					__ops_text_free(text);
566 					return NULL;
567 				}
568 			}
569 		}
570 	}
571 	return text;
572 }
573 
574 /*
575  * Public Functions
576  */
577 
578 /**
579  * \ingroup Core_Print
580  * returns description of the Packet Tag
581  * \param packet_tag
582  * \return string or "Unknown"
583 */
584 const char     *
585 __ops_show_packet_tag(__ops_packet_tag_t packet_tag)
586 {
587 	const char     *ret;
588 
589 	ret = __ops_str_from_map(packet_tag, packet_tag_map);
590 	if (!ret) {
591 		ret = "Unknown Tag";
592 	}
593 	return ret;
594 }
595 
596 /**
597  * \ingroup Core_Print
598  *
599  * returns description of the Signature Sub-Packet type
600  * \param ss_type Signature Sub-Packet type
601  * \return string or "Unknown"
602  */
603 const char     *
604 __ops_show_ss_type(__ops_ss_type_t ss_type)
605 {
606 	return __ops_str_from_map(ss_type, ss_type_map);
607 }
608 
609 /**
610  * \ingroup Core_Print
611  *
612  * returns description of the Revocation Reason code
613  * \param ss_rr_code Revocation Reason code
614  * \return string or "Unknown"
615  */
616 const char     *
617 __ops_show_ss_rr_code(__ops_ss_rr_code_t ss_rr_code)
618 {
619 	return __ops_str_from_map(ss_rr_code, ss_rr_code_map);
620 }
621 
622 /**
623  * \ingroup Core_Print
624  *
625  * returns description of the given Signature type
626  * \param sig_type Signature type
627  * \return string or "Unknown"
628  */
629 const char     *
630 __ops_show_sig_type(__ops_sig_type_t sig_type)
631 {
632 	return __ops_str_from_map(sig_type, sig_type_map);
633 }
634 
635 /**
636  * \ingroup Core_Print
637  *
638  * returns description of the given Public Key Algorithm
639  * \param pka Public Key Algorithm type
640  * \return string or "Unknown"
641  */
642 const char     *
643 __ops_show_pka(__ops_pubkey_alg_t pka)
644 {
645 	return __ops_str_from_map(pka, pubkey_alg_map);
646 }
647 
648 /**
649  * \ingroup Core_Print
650  * returns description of the Preferred Compression
651  * \param octet Preferred Compression
652  * \return string or "Unknown"
653 */
654 const char     *
655 __ops_show_ss_zpref(unsigned char octet)
656 {
657 	return __ops_str_from_map(octet, compression_alg_map);
658 }
659 
660 /**
661  * \ingroup Core_Print
662  *
663  * returns set of descriptions of the given Preferred Compression Algorithms
664  * \param ss_zpref Array of Preferred Compression Algorithms
665  * \return NULL if cannot allocate memory or other error
666  * \return pointer to structure, if no error
667  */
668 __ops_text_t     *
669 __ops_showall_ss_zpref(__ops_ss_zpref_t ss_zpref)
670 {
671 	return text_from_bytemapped_octets(&ss_zpref.data,
672 					&__ops_show_ss_zpref);
673 }
674 
675 
676 /**
677  * \ingroup Core_Print
678  *
679  * returns description of the Hash Algorithm type
680  * \param hash Hash Algorithm type
681  * \return string or "Unknown"
682  */
683 const char     *
684 __ops_show_hash_alg(unsigned char hash)
685 {
686 	return __ops_str_from_map(hash, hash_alg_map);
687 }
688 
689 /**
690  * \ingroup Core_Print
691  *
692  * returns set of descriptions of the given Preferred Hash Algorithms
693  * \param ss_hashpref Array of Preferred Hash Algorithms
694  * \return NULL if cannot allocate memory or other error
695  * \return pointer to structure, if no error
696  */
697 __ops_text_t     *
698 __ops_showall_ss_hashpref(__ops_ss_hashpref_t ss_hashpref)
699 {
700 	return text_from_bytemapped_octets(&ss_hashpref.data,
701 					   &__ops_show_hash_alg);
702 }
703 
704 const char     *
705 __ops_show_symm_alg(unsigned char hash)
706 {
707 	return __ops_str_from_map(hash, symm_alg_map);
708 }
709 
710 /**
711  * \ingroup Core_Print
712  * returns description of the given Preferred Symmetric Key Algorithm
713  * \param octet
714  * \return string or "Unknown"
715 */
716 const char     *
717 __ops_show_ss_skapref(unsigned char octet)
718 {
719 	return __ops_str_from_map(octet, symm_alg_map);
720 }
721 
722 /**
723  * \ingroup Core_Print
724  *
725  * returns set of descriptions of the given Preferred Symmetric Key Algorithms
726  * \param ss_skapref Array of Preferred Symmetric Key Algorithms
727  * \return NULL if cannot allocate memory or other error
728  * \return pointer to structure, if no error
729  */
730 __ops_text_t     *
731 __ops_showall_ss_skapref(__ops_ss_skapref_t ss_skapref)
732 {
733 	return text_from_bytemapped_octets(&ss_skapref.data,
734 					   &__ops_show_ss_skapref);
735 }
736 
737 /**
738  * \ingroup Core_Print
739  * returns description of one SS Feature
740  * \param octet
741  * \return string or "Unknown"
742 */
743 static const char *
744 __ops_show_ss_feature(unsigned char octet, unsigned offset)
745 {
746 	if (offset >= OPS_ARRAY_SIZE(ss_feature_map)) {
747 		return "Unknown";
748 	}
749 	return find_bitfield(ss_feature_map[offset], octet);
750 }
751 
752 /**
753  * \ingroup Core_Print
754  *
755  * returns set of descriptions of the given SS Features
756  * \param ss_features Signature Sub-Packet Features
757  * \return NULL if cannot allocate memory or other error
758  * \return pointer to structure, if no error
759  */
760 /* XXX: shouldn't this use show_all_octets_bits? */
761 __ops_text_t     *
762 __ops_showall_ss_features(__ops_ss_features_t ss_features)
763 {
764 	unsigned char	 mask, bit;
765 	__ops_text_t	*text = NULL;
766 	const char	*str;
767 	unsigned	 i;
768 	int		 j = 0;
769 
770 	text = calloc(1, sizeof(__ops_text_t));
771 	if (!text) {
772 		return NULL;
773 	}
774 
775 	__ops_text_init(text);
776 
777 	for (i = 0; i < ss_features.data.len; i++) {
778 		mask = 0x80;
779 		for (j = 0; j < 8; j++, mask = (unsigned)mask >> 1) {
780 			bit = ss_features.data.contents[i] & mask;
781 			if (bit) {
782 				str = __ops_show_ss_feature(bit, i);
783 				if (!add_bitmap_entry(text, str, bit)) {
784 					__ops_text_free(text);
785 					return NULL;
786 				}
787 			}
788 		}
789 	}
790 	return text;
791 }
792 
793 /**
794  * \ingroup Core_Print
795  * returns description of SS Key Flag
796  * \param octet
797  * \param map
798  * \return
799 */
800 const char     *
801 __ops_show_ss_key_flag(unsigned char octet, __ops_bit_map_t *map)
802 {
803 	return find_bitfield(map, octet);
804 }
805 
806 /**
807  * \ingroup Core_Print
808  *
809  * returns set of descriptions of the given Preferred Key Flags
810  * \param ss_key_flags Array of Key Flags
811  * \return NULL if cannot allocate memory or other error
812  * \return pointer to structure, if no error
813  */
814 __ops_text_t     *
815 __ops_showall_ss_key_flags(__ops_ss_key_flags_t ss_key_flags)
816 {
817 	unsigned char    mask, bit;
818 	__ops_text_t	*text = NULL;
819 	const char	*str;
820 	int              i = 0;
821 
822 	text = calloc(1, sizeof(__ops_text_t));
823 	if (!text) {
824 		return NULL;
825 	}
826 
827 	__ops_text_init(text);
828 
829 	/* xxx - TBD: extend to handle multiple octets of bits - rachel */
830 	for (i = 0, mask = 0x80; i < 8; i++, mask = (unsigned)mask >> 1) {
831 		bit = ss_key_flags.data.contents[0] & mask;
832 		if (bit) {
833 			str = __ops_show_ss_key_flag(bit, &ss_key_flags_map[0]);
834 			if (!add_bitmap_entry(text, strdup(str), bit)) {
835 				__ops_text_free(text);
836 				return NULL;
837 			}
838 		}
839 	}
840 	/*
841 	 * xxx - must add error text if more than one octet. Only one
842 	 * currently specified -- rachel
843 	 */
844 	return text;
845 }
846 
847 /**
848  * \ingroup Core_Print
849  *
850  * returns description of one given Key Server Preference
851  *
852  * \param prefs Byte containing bitfield of preferences
853  * \param map
854  * \return string or "Unknown"
855  */
856 const char     *
857 __ops_show_keyserv_pref(unsigned char prefs, __ops_bit_map_t *map)
858 {
859 	return find_bitfield(map, prefs);
860 }
861 
862 /**
863  * \ingroup Core_Print
864  * returns set of descriptions of given Key Server Preferences
865  * \param ss_key_server_prefs
866  * \return NULL if cannot allocate memory or other error
867  * \return pointer to structure, if no error
868  *
869 */
870 __ops_text_t     *
871 __ops_show_keyserv_prefs(__ops_ss_key_server_prefs_t prefs)
872 {
873 	unsigned char	 mask, bit;
874 	__ops_text_t	*text = NULL;
875 	const char	*str;
876 	int              i = 0;
877 
878 	text = calloc(1, sizeof(__ops_text_t));
879 	if (!text) {
880 		return NULL;
881 	}
882 
883 	__ops_text_init(text);
884 
885 	/* xxx - TBD: extend to handle multiple octets of bits - rachel */
886 
887 	for (i = 0, mask = 0x80; i < 8; i++, mask = (unsigned)mask >> 1) {
888 		bit = prefs.data.contents[0] & mask;
889 		if (bit) {
890 			str = __ops_show_keyserv_pref(bit,
891 						ss_key_server_prefs_map);
892 			if (!add_bitmap_entry(text, strdup(str), bit)) {
893 				__ops_text_free(text);
894 				return NULL;
895 			}
896 		}
897 	}
898 	/*
899 	 * xxx - must add error text if more than one octet. Only one
900 	 * currently specified -- rachel
901 	 */
902 	return text;
903 }
904 
905 /**
906  * \ingroup Core_Print
907  *
908  * returns set of descriptions of the given SS Notation Data Flags
909  * \param ss_notation Signature Sub-Packet Notation Data
910  * \return NULL if cannot allocate memory or other error
911  * \return pointer to structure, if no error
912  */
913 __ops_text_t     *
914 __ops_showall_notation(__ops_ss_notation_t ss_notation)
915 {
916 	return showall_octets_bits(&ss_notation.flags,
917 				ss_notation_map,
918 				OPS_ARRAY_SIZE(ss_notation_map));
919 }
920