aboutsummaryrefslogtreecommitdiffstats
path: root/g13/g13tuple.c
diff options
context:
space:
mode:
authorWerner Koch <[email protected]>2016-02-11 12:57:35 +0000
committerWerner Koch <[email protected]>2016-02-13 16:06:40 +0000
commit13f745b50dc7031755faadb2d3476a6b6aafc739 (patch)
tree6f08695b1afdde483f0d73907228aa32a07b18c0 /g13/g13tuple.c
parentg13: Define 3 new tags. (diff)
downloadgnupg-13f745b50dc7031755faadb2d3476a6b6aafc739.tar.gz
gnupg-13f745b50dc7031755faadb2d3476a6b6aafc739.zip
g13: Improve dump_keyblob.
* g13/g13tuple.c: Include keyblob.h. (find_tuple_uint): Factor code out to ... (convert_uint): new. (all_printable): New. * g13/mount.c (dump_keyblob: Move and rename to ... * g13/g13tuple.c (dump_tupledesc): here. Revamp and pretyy print uint values. Signed-off-by: Werner Koch <[email protected]>
Diffstat (limited to 'g13/g13tuple.c')
-rw-r--r--g13/g13tuple.c96
1 files changed, 85 insertions, 11 deletions
diff --git a/g13/g13tuple.c b/g13/g13tuple.c
index b90d81769..b3187fbfe 100644
--- a/g13/g13tuple.c
+++ b/g13/g13tuple.c
@@ -27,6 +27,7 @@
#include "g13.h"
#include "g13tuple.h"
+#include "keyblob.h" /* Required for dump_tupledesc. */
/* Definition of the tuple descriptor object. */
@@ -183,21 +184,14 @@ find_tuple (tupledesc_t tupledesc, unsigned int tag, size_t *r_length)
}
-/* Similar to find-tuple but expects an unsigned int value and stores
- * that at R_VALUE. If the tag was not found GPG_ERR_NOT_FOUND is
- * returned and 0 stored at R_VALUE. If the value cannot be converted
- * to an unsigned integer GPG_ERR_ERANGE is returned. */
-gpg_error_t
-find_tuple_uint (tupledesc_t tupledesc, unsigned int tag,
- unsigned long long *r_value)
+/* Helper for find_tuple_uint and others. */
+static gpg_error_t
+convert_uint (const unsigned char *s, size_t n, unsigned long long *r_value)
{
- const unsigned char *s;
- size_t n;
unsigned long long value = 0;
*r_value = 0;
- s = find_tuple (tupledesc, tag, &n);
if (!s)
return gpg_error (GPG_ERR_NOT_FOUND);
if (!n || (*s & 0x80)) /* No bytes or negative. */
@@ -214,12 +208,27 @@ find_tuple_uint (tupledesc_t tupledesc, unsigned int tag,
value <<= 8;
value |= *s;
}
-
*r_value = value;
return 0;
}
+/* Similar to find-tuple but expects an unsigned int value and stores
+ * that at R_VALUE. If the tag was not found GPG_ERR_NOT_FOUND is
+ * returned and 0 stored at R_VALUE. If the value cannot be converted
+ * to an unsigned integer GPG_ERR_ERANGE is returned. */
+gpg_error_t
+find_tuple_uint (tupledesc_t tupledesc, unsigned int tag,
+ unsigned long long *r_value)
+{
+ const unsigned char *s;
+ size_t n;
+
+ s = find_tuple (tupledesc, tag, &n);
+ return convert_uint (s, n, r_value);
+}
+
+
const void *
next_tuple (tupledesc_t tupledesc, unsigned int *r_tag, size_t *r_length)
{
@@ -252,3 +261,68 @@ next_tuple (tupledesc_t tupledesc, unsigned int *r_tag, size_t *r_length)
return NULL;
}
+
+
+/* Return true if BUF has only printable characters. */
+static int
+all_printable (const void *buf, size_t buflen)
+{
+ const unsigned char *s;
+
+ for (s=buf ; buflen; s++, buflen--)
+ if (*s < 32 && *s > 126)
+ return 0;
+ return 1;
+}
+
+
+/* Print information about TUPLES to the log stream. */
+void
+dump_tupledesc (tupledesc_t tuples)
+{
+ size_t n;
+ unsigned int tag;
+ const void *value;
+ unsigned long long uint;
+
+ log_info ("keyblob dump:\n");
+ tag = KEYBLOB_TAG_BLOBVERSION;
+ value = find_tuple (tuples, tag, &n);
+ while (value)
+ {
+ log_info (" tag: %-5u len: %-2u value: ", tag, (unsigned int)n);
+ if (!n)
+ log_printf ("[none]\n");
+ else
+ {
+ switch (tag)
+ {
+ case KEYBLOB_TAG_ENCKEY:
+ case KEYBLOB_TAG_MACKEY:
+ log_printf ("[confidential]\n");
+ break;
+
+ case KEYBLOB_TAG_ALGOSTR:
+ if (n < 100 && all_printable (value, n))
+ log_printf ("%.*s\n", (int)n, (const char*)value);
+ else
+ log_printhex ("", value, n);
+ break;
+
+ case KEYBLOB_TAG_CONT_NSEC:
+ case KEYBLOB_TAG_ENC_NSEC:
+ case KEYBLOB_TAG_ENC_OFF:
+ if (!convert_uint (value, n, &uint))
+ log_printf ("%llu\n", uint);
+ else
+ log_printhex ("", value, n);
+ break;
+
+ default:
+ log_printhex ("", value, n);
+ break;
+ }
+ }
+ value = next_tuple (tuples, &tag, &n);
+ }
+}