From 074a30618fd6776453bfce5e06b05724aa99d5d4 Mon Sep 17 00:00:00 2001 From: Love Hornquist Astrand Date: Mon, 25 Apr 2011 11:33:58 -0700 Subject: [PATCH] Try to parse inner structure of an octet string (limited to CONS SEQ right now) --- lib/asn1/asn1_print.c | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/lib/asn1/asn1_print.c b/lib/asn1/asn1_print.c index 279e969b0..917b8cd5f 100644 --- a/lib/asn1/asn1_print.c +++ b/lib/asn1/asn1_print.c @@ -41,9 +41,8 @@ #include #include -RCSID("$Id$"); - static int indent_flag = 1; +static int inner_flag = 0; static unsigned long indefinite_form_loop; static unsigned long indefinite_form_loop_max = 10000; @@ -167,17 +166,39 @@ loop (unsigned char *buf, size_t len, int indent) } case UT_OctetString : { heim_octet_string str; - int i; - unsigned char *uc; + size_t i; ret = der_get_octet_string (buf, length, &str, NULL); if (ret) errx (1, "der_get_octet_string: %s", error_message (ret)); printf ("(length %lu), ", (unsigned long)length); - uc = (unsigned char *)str.data; - for (i = 0; i < min(16,length); ++i) - printf ("%02x", uc[i]); - printf ("\n"); + + if (inner_flag) { + Der_class class; + Der_type type; + unsigned int tag; + + ret = der_get_tag(str.data, str.length, + &class, &type, &tag, &sz); + if (ret || sz > str.length || + type != CONS || tag != UT_Sequence) + goto just_an_octet_string; + + printf("{\n"); + loop (str.data, str.length, indent + 2); + for (i = 0; i < indent; ++i) + printf (" "); + printf ("}\n"); + + } else { + unsigned char *uc; + + just_an_octet_string: + uc = (unsigned char *)str.data; + for (i = 0; i < min(16,length); ++i) + printf ("%02x", uc[i]); + printf ("\n"); + } free (str.data); break; } @@ -295,6 +316,7 @@ static int version_flag; static int help_flag; struct getargs args[] = { { "indent", 0, arg_negative_flag, &indent_flag }, + { "inner", 0, arg_flag, &inner_flag, "try to parse inner structures of OCTET STRING" }, { "version", 0, arg_flag, &version_flag }, { "help", 0, arg_flag, &help_flag } }; -- 2.11.4.GIT