5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2007 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
22 SilcBool hexdump = FALSE;
23 SilcBool dec_base64 = FALSE;
25 const char *asn1_tag_name(SilcAsn1Tag tag)
30 case SILC_ASN1_TAG_OPTS:
32 case SILC_ASN1_TAG_CHOICE:
34 case SILC_ASN1_TAG_ANY:
36 case SILC_ASN1_TAG_ANY_PRIMITIVE:
37 return "any primitive";
38 case SILC_ASN1_TAG_SEQUENCE_OF:
40 case SILC_ASN1_TAG_SEQUENCE:
42 case SILC_ASN1_TAG_SET:
44 case SILC_ASN1_TAG_INTEGER:
46 case SILC_ASN1_TAG_SHORT_INTEGER:
47 return "short integer";
48 case SILC_ASN1_TAG_OID:
50 case SILC_ASN1_TAG_BOOLEAN:
52 case SILC_ASN1_TAG_OCTET_STRING:
53 return "octet-string";
54 case SILC_ASN1_TAG_BIT_STRING:
56 case SILC_ASN1_TAG_NULL:
58 case SILC_ASN1_TAG_ENUM:
60 case SILC_ASN1_TAG_UTC_TIME:
62 case SILC_ASN1_TAG_GENERALIZED_TIME:
63 return "generalized-time";
64 case SILC_ASN1_TAG_UTF8_STRING:
66 case SILC_ASN1_TAG_NUMERIC_STRING:
67 return "numeric-string";
68 case SILC_ASN1_TAG_PRINTABLE_STRING:
69 return "printable-string";
70 case SILC_ASN1_TAG_IA5_STRING:
72 case SILC_ASN1_TAG_VISIBLE_STRING:
73 return "visible-string";
74 case SILC_ASN1_TAG_UNIVERSAL_STRING:
75 return "universal-string";
76 case SILC_ASN1_TAG_UNRESTRICTED_STRING:
77 return "unrestricted-string";
78 case SILC_ASN1_TAG_BMP_STRING:
80 case SILC_ASN1_TAG_ODE:
82 case SILC_ASN1_TAG_ETI:
84 case SILC_ASN1_TAG_REAL:
86 case SILC_ASN1_TAG_EMBEDDED:
88 case SILC_ASN1_TAG_ROI:
90 case SILC_ASN1_TAG_TELETEX_STRING:
91 return "teletex-string";
92 case SILC_ASN1_TAG_VIDEOTEX_STRING:
93 return "videotex-string";
94 case SILC_ASN1_TAG_GRAPHIC_STRING:
95 return "graphic-string";
96 case SILC_ASN1_TAG_GENERAL_STRING:
97 return "general-string";
104 int asn1_dump(SilcAsn1 asn1, SilcBuffer src)
106 SilcBool ret = FALSE;
107 SilcBerEncoding renc;
109 const unsigned char *rdata;
110 SilcUInt32 rdata_len, len = 0;
115 SILC_LOG_DEBUG(("Dumping ASN.1"));
116 memset(indent, 0, sizeof(indent));
118 while (silc_buffer_len(src)) {
119 /* Decode the BER block */
120 ret = silc_ber_decode(src, NULL, &renc, &rtag, &rdata,
121 &rdata_len, &rindef, &len);
123 fprintf(stderr, "Error: Cannot parse BER block, malformed ASN.1 data");
127 memset(indent, 32, depth);
129 fprintf(stdout, "%04d: %s[%s] [%d]", depth, indent,
130 asn1_tag_name(rtag), (int)rtag);
132 if (rtag != SILC_ASN1_TAG_SEQUENCE) {
134 fprintf(stdout, " [length %lu]\n", rdata_len);
135 silc_hexdump(rdata, rdata_len, stdout);
137 fprintf(stdout, "\n");
140 fprintf(stdout, "\n");
143 if (rtag == SILC_ASN1_TAG_SEQUENCE && depth < sizeof(indent))
146 if (renc == SILC_BER_ENC_PRIMITIVE)
147 len = len + rdata_len;
152 silc_buffer_pull(src, len);
161 "Usage: asn1dump [OPTIONS] FILE\n"
164 " -h Print this help, then exit\n"
165 " -x HEX dump ASN.1 data\n"
166 " -b Decode Base64 encoding\n"
171 int main(int argc, char **argv)
175 SilcBufferStruct buf;
176 unsigned char *data, *tmp;
185 while ((opt = getopt(argc, argv, "hxb")) != EOF) {
208 data = tmp = silc_file_readfile(argv[i], &data_len, NULL);
210 fprintf(stderr, "Error: Cannot read file '%s': %s", argv[i],
216 data = silc_base64_decode(NULL, data, data_len, &data_len);
218 fprintf(stderr, "Error: Cannot decode Base64 encoding\n");
224 silc_buffer_set(&buf, data, data_len);
226 asn1 = silc_asn1_alloc(NULL);
228 ret = asn1_dump(asn1, &buf);
230 silc_asn1_free(asn1);