5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2003 - 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.
24 /* Allocate ASN.1 context. */
26 SilcAsn1 silc_asn1_alloc(SilcStack stack)
31 stack1 = silc_stack_alloc(0, stack);
35 /* Allocate itself from the stack */
36 asn1 = silc_scalloc(stack1, 1, sizeof(*asn1));
38 silc_stack_free(stack1);
42 asn1->orig_stack = stack;
43 asn1->stack1 = stack1;
44 asn1->stack2 = silc_stack_alloc(0, stack);
46 silc_stack_free(stack1);
55 /* Free ASN.1 context */
57 void silc_asn1_free(SilcAsn1 asn1)
59 silc_asn1_uninit(asn1);
62 /* Init pre-allocated ASN.1 context */
64 SilcBool silc_asn1_init(SilcAsn1 asn1, SilcStack stack)
66 asn1->orig_stack = stack;
68 asn1->stack1 = silc_stack_alloc(0, stack);
72 asn1->stack2 = silc_stack_alloc(0, stack);
74 silc_stack_free(asn1->stack1);
83 /* Uninit ASN.1 context */
85 void silc_asn1_uninit(SilcAsn1 asn1)
87 SilcStack stack1 = asn1->stack1;
90 stack1 = asn1->stack2;
91 asn1->stack2 = asn1->stack1;
94 silc_stack_free(asn1->stack2);
95 silc_stack_free(stack1);
98 #if defined(SILC_DEBUG)
99 /* Returns string representation of a tag */
101 const char *silc_asn1_tag_name(SilcAsn1Tag tag)
106 case SILC_ASN1_TAG_OPTS:
108 case SILC_ASN1_TAG_CHOICE:
110 case SILC_ASN1_TAG_ANY:
112 case SILC_ASN1_TAG_ANY_PRIMITIVE:
113 return "any primitive";
114 case SILC_ASN1_TAG_SEQUENCE_OF:
115 return "sequence of";
116 case SILC_ASN1_TAG_SEQUENCE:
118 case SILC_ASN1_TAG_SET:
120 case SILC_ASN1_TAG_INTEGER:
122 case SILC_ASN1_TAG_SHORT_INTEGER:
123 return "short integer";
124 case SILC_ASN1_TAG_OID:
126 case SILC_ASN1_TAG_BOOLEAN:
128 case SILC_ASN1_TAG_OCTET_STRING:
129 return "octet-string";
130 case SILC_ASN1_TAG_BIT_STRING:
132 case SILC_ASN1_TAG_NULL:
134 case SILC_ASN1_TAG_ENUM:
136 case SILC_ASN1_TAG_UTC_TIME:
138 case SILC_ASN1_TAG_GENERALIZED_TIME:
139 return "generalized-time";
140 case SILC_ASN1_TAG_UTF8_STRING:
141 return "utf8-string";
142 case SILC_ASN1_TAG_NUMERIC_STRING:
143 return "numeric-string";
144 case SILC_ASN1_TAG_PRINTABLE_STRING:
145 return "printable-string";
146 case SILC_ASN1_TAG_IA5_STRING:
148 case SILC_ASN1_TAG_VISIBLE_STRING:
149 return "visible-string";
150 case SILC_ASN1_TAG_UNIVERSAL_STRING:
151 return "universal-string";
152 case SILC_ASN1_TAG_UNRESTRICTED_STRING:
153 return "unrestricted-string";
154 case SILC_ASN1_TAG_BMP_STRING:
156 case SILC_ASN1_TAG_ODE:
158 case SILC_ASN1_TAG_ETI:
160 case SILC_ASN1_TAG_REAL:
162 case SILC_ASN1_TAG_EMBEDDED:
164 case SILC_ASN1_TAG_ROI:
166 case SILC_ASN1_TAG_TELETEX_STRING:
167 return "teletex-string";
168 case SILC_ASN1_TAG_VIDEOTEX_STRING:
169 return "videotex-string";
170 case SILC_ASN1_TAG_GRAPHIC_STRING:
171 return "graphic-string";
172 case SILC_ASN1_TAG_GENERAL_STRING:
173 return "general-string";
179 #endif /* SILC_DEBUG */
181 #ifdef SILC_DIST_TOOLKIT
183 /* Dumps the ASN.1 data block into standard output (stdout). */
185 SilcBool silc_asn1_dump(SilcAsn1 asn1, SilcBuffer src)
187 SilcBool ret = FALSE;
188 SilcBerEncoding renc;
190 const unsigned char *rdata;
191 SilcUInt32 rdata_len, len = 0;
194 SILC_LOG_DEBUG(("Dumping ASN.1"));
196 while (silc_buffer_len(src)) {
197 /* Decode the BER block */
198 ret = silc_ber_decode(src, NULL, &renc, &rtag, &rdata,
199 &rdata_len, &rindef, &len);
201 SILC_LOG_DEBUG(("Error parsing BER block, malformed ASN.1 data"));
205 fprintf(stdout, "Type %s [%d]\n",
206 silc_asn1_tag_name(rtag), (int)rtag);
208 if (renc == SILC_BER_ENC_PRIMITIVE)
209 len = len + rdata_len;
214 silc_buffer_pull(src, len);
219 #endif /* SILC_DEBUG */
220 #endif /* SILC_DIST_TOOLKIT */