5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2003 - 2005 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.
24 /* Allocate ASN.1 context. */
26 SilcAsn1 silc_asn1_alloc(void)
28 SilcAsn1 asn1 = silc_calloc(1, sizeof(*asn1));
32 if (!silc_asn1_init(asn1))
38 /* Free ASN.1 context */
40 void silc_asn1_free(SilcAsn1 asn1)
42 silc_asn1_uninit(asn1);
46 /* Init pre-allocated ASN.1 context */
48 SilcBool silc_asn1_init(SilcAsn1 asn1)
50 asn1->stack1 = silc_stack_alloc(768);
54 asn1->stack2 = silc_stack_alloc(768);
56 silc_stack_free(asn1->stack2);
65 /* Uninit ASN.1 context */
67 void silc_asn1_uninit(SilcAsn1 asn1)
70 silc_stack_stats(asn1->stack1);
71 silc_stack_stats(asn1->stack2);
73 silc_stack_free(asn1->stack1);
74 silc_stack_free(asn1->stack2);
77 #ifdef SILC_DIST_INPLACE
78 /* Returns string representation of a tag */
80 const char *silc_asn1_tag_name(SilcAsn1Tag tag)
85 case SILC_ASN1_TAG_OPTS:
87 case SILC_ASN1_TAG_CHOICE:
89 case SILC_ASN1_TAG_ANY:
91 case SILC_ASN1_TAG_ANY_PRIMITIVE:
92 return "any primitive";
93 case SILC_ASN1_TAG_SEQUENCE_OF:
95 case SILC_ASN1_TAG_SEQUENCE:
97 case SILC_ASN1_TAG_SET:
99 case SILC_ASN1_TAG_INTEGER:
101 case SILC_ASN1_TAG_OID:
103 case SILC_ASN1_TAG_BOOLEAN:
105 case SILC_ASN1_TAG_OCTET_STRING:
106 return "octet-string";
107 case SILC_ASN1_TAG_BIT_STRING:
109 case SILC_ASN1_TAG_NULL:
111 case SILC_ASN1_TAG_ENUM:
113 case SILC_ASN1_TAG_UTC_TIME:
115 case SILC_ASN1_TAG_GENERALIZED_TIME:
116 return "generalized-time";
117 case SILC_ASN1_TAG_UTF8_STRING:
118 return "utf8-string";
119 case SILC_ASN1_TAG_NUMERIC_STRING:
120 return "numeric-string";
121 case SILC_ASN1_TAG_PRINTABLE_STRING:
122 return "printable-string";
123 case SILC_ASN1_TAG_IA5_STRING:
125 case SILC_ASN1_TAG_VISIBLE_STRING:
126 return "visible-string";
127 case SILC_ASN1_TAG_UNIVERSAL_STRING:
128 return "universal-string";
129 case SILC_ASN1_TAG_UNRESTRICTED_STRING:
130 return "unrestricted-string";
131 case SILC_ASN1_TAG_BMP_STRING:
133 case SILC_ASN1_TAG_ODE:
135 case SILC_ASN1_TAG_ETI:
137 case SILC_ASN1_TAG_REAL:
139 case SILC_ASN1_TAG_EMBEDDED:
141 case SILC_ASN1_TAG_ROI:
143 case SILC_ASN1_TAG_TELETEX_STRING:
144 return "teletex-string";
145 case SILC_ASN1_TAG_VIDEOTEX_STRING:
146 return "videotex-string";
147 case SILC_ASN1_TAG_GRAPHIC_STRING:
148 return "graphic-string";
149 case SILC_ASN1_TAG_GENERAL_STRING:
150 return "general-string";
157 /* Dumps the ASN.1 data block into standard output (stdout). */
159 SilcBool silc_asn1_dump(SilcAsn1 asn1, SilcBuffer src)
161 SilcBool ret = FALSE;
162 SilcBerEncoding renc;
164 const unsigned char *rdata;
165 SilcUInt32 rdata_len, len = 0;
168 SILC_LOG_DEBUG(("Dumping ASN.1"));
170 while (silc_buffer_len(src)) {
171 /* Decode the BER block */
172 ret = silc_ber_decode(src, NULL, &renc, (SilcUInt32 *)&rtag, &rdata,
173 &rdata_len, &rindef, &len);
175 SILC_LOG_DEBUG(("Error parsing BER block, malformed ASN.1 data"));
179 fprintf(stdout, "Type %s [%d]\n",
180 silc_asn1_tag_name(rtag), rtag);
182 if (renc == SILC_BER_ENC_PRIMITIVE)
183 len = len + rdata_len;
188 silc_buffer_pull(src, len);
194 #endif /* SILC_DIST_INPLACE */