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)
28 case SILC_ASN1_TAG_CHOICE:
30 case SILC_ASN1_TAG_ANY:
32 case SILC_ASN1_TAG_ANY_PRIMITIVE:
33 return "any primitive";
34 case SILC_ASN1_TAG_SEQUENCE_OF:
36 case SILC_ASN1_TAG_SEQUENCE:
38 case SILC_ASN1_TAG_SET:
40 case SILC_ASN1_TAG_INTEGER:
42 case SILC_ASN1_TAG_SHORT_INTEGER:
43 return "short integer";
44 case SILC_ASN1_TAG_OID:
46 case SILC_ASN1_TAG_BOOLEAN:
48 case SILC_ASN1_TAG_OCTET_STRING:
49 return "octet-string";
50 case SILC_ASN1_TAG_BIT_STRING:
52 case SILC_ASN1_TAG_NULL:
54 case SILC_ASN1_TAG_ENUM:
56 case SILC_ASN1_TAG_UTC_TIME:
58 case SILC_ASN1_TAG_GENERALIZED_TIME:
59 return "generalized-time";
60 case SILC_ASN1_TAG_UTF8_STRING:
62 case SILC_ASN1_TAG_NUMERIC_STRING:
63 return "numeric-string";
64 case SILC_ASN1_TAG_PRINTABLE_STRING:
65 return "printable-string";
66 case SILC_ASN1_TAG_IA5_STRING:
68 case SILC_ASN1_TAG_VISIBLE_STRING:
69 return "visible-string";
70 case SILC_ASN1_TAG_UNIVERSAL_STRING:
71 return "universal-string";
72 case SILC_ASN1_TAG_UNRESTRICTED_STRING:
73 return "unrestricted-string";
74 case SILC_ASN1_TAG_BMP_STRING:
76 case SILC_ASN1_TAG_ODE:
78 case SILC_ASN1_TAG_ETI:
80 case SILC_ASN1_TAG_REAL:
82 case SILC_ASN1_TAG_EMBEDDED:
84 case SILC_ASN1_TAG_ROI:
86 case SILC_ASN1_TAG_TELETEX_STRING:
87 return "teletex-string";
88 case SILC_ASN1_TAG_VIDEOTEX_STRING:
89 return "videotex-string";
90 case SILC_ASN1_TAG_GRAPHIC_STRING:
91 return "graphic-string";
92 case SILC_ASN1_TAG_GENERAL_STRING:
93 return "general-string";
100 int asn1_dump(SilcAsn1 asn1, SilcBuffer src)
102 SilcBool ret = FALSE;
103 SilcBerEncoding renc;
105 const unsigned char *rdata;
106 SilcUInt32 rdata_len, len = 0;
111 SILC_LOG_DEBUG(("Dumping ASN.1"));
112 memset(indent, 0, sizeof(indent));
114 while (silc_buffer_len(src)) {
115 /* Decode the BER block */
116 ret = silc_ber_decode(src, NULL, &renc, &rtag, &rdata,
117 &rdata_len, &rindef, &len);
119 fprintf(stderr, "Error: Cannot parse BER block, malformed ASN.1 data");
123 memset(indent, 32, depth);
125 fprintf(stdout, "%04d: %s[%s] [%d]", depth, indent,
126 asn1_tag_name(rtag), (int)rtag);
128 if (rtag != SILC_ASN1_TAG_SEQUENCE) {
130 fprintf(stdout, " [length %lu]\n", rdata_len);
131 silc_hexdump(rdata, rdata_len, stdout);
133 fprintf(stdout, "\n");
136 fprintf(stdout, "\n");
139 if (rtag == SILC_ASN1_TAG_SEQUENCE && depth < sizeof(indent))
142 if (renc == SILC_BER_ENC_PRIMITIVE)
143 len = len + rdata_len;
148 silc_buffer_pull(src, len);
157 "Usage: asn1dump [OPTIONS] FILE\n"
160 " -h Print this help, then exit\n"
161 " -x HEX dump ASN.1 data\n"
162 " -b Decode Base64 encoding\n"
167 int main(int argc, char **argv)
171 SilcBufferStruct buf;
172 unsigned char *data, *tmp;
181 while ((opt = getopt(argc, argv, "hxb")) != EOF) {
204 data = tmp = silc_file_readfile(argv[i], &data_len, NULL);
206 fprintf(stderr, "Error: Cannot read file '%s': %s", argv[i],
212 data = silc_base64_decode(NULL, data, data_len, &data_len);
214 fprintf(stderr, "Error: Cannot decode Base64 encoding\n");
220 silc_buffer_set(&buf, data, data_len);
222 asn1 = silc_asn1_alloc(NULL);
224 ret = asn1_dump(asn1, &buf);
226 silc_asn1_free(asn1);