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->stack1 = stack1;
43 asn1->stack2 = silc_stack_alloc(0, stack);
45 silc_stack_free(stack1);
54 /* Free ASN.1 context */
56 void silc_asn1_free(SilcAsn1 asn1)
58 silc_asn1_uninit(asn1);
61 /* Init pre-allocated ASN.1 context */
63 SilcBool silc_asn1_init(SilcAsn1 asn1, SilcStack stack)
65 asn1->stack1 = silc_stack_alloc(0, stack);
69 asn1->stack2 = silc_stack_alloc(0, stack);
71 silc_stack_free(asn1->stack1);
80 /* Uninit ASN.1 context */
82 void silc_asn1_uninit(SilcAsn1 asn1)
84 silc_stack_free(asn1->stack2);
85 silc_stack_free(asn1->stack1);
88 #if defined(SILC_DEBUG)
89 /* Returns string representation of a tag */
91 const char *silc_asn1_tag_name(SilcAsn1Tag tag)
96 case SILC_ASN1_TAG_OPTS:
98 case SILC_ASN1_TAG_CHOICE:
100 case SILC_ASN1_TAG_ANY:
102 case SILC_ASN1_TAG_ANY_PRIMITIVE:
103 return "any primitive";
104 case SILC_ASN1_TAG_SEQUENCE_OF:
105 return "sequence of";
106 case SILC_ASN1_TAG_SEQUENCE:
108 case SILC_ASN1_TAG_SET:
110 case SILC_ASN1_TAG_INTEGER:
112 case SILC_ASN1_TAG_SHORT_INTEGER:
113 return "short integer";
114 case SILC_ASN1_TAG_OID:
116 case SILC_ASN1_TAG_BOOLEAN:
118 case SILC_ASN1_TAG_OCTET_STRING:
119 return "octet-string";
120 case SILC_ASN1_TAG_BIT_STRING:
122 case SILC_ASN1_TAG_NULL:
124 case SILC_ASN1_TAG_ENUM:
126 case SILC_ASN1_TAG_UTC_TIME:
128 case SILC_ASN1_TAG_GENERALIZED_TIME:
129 return "generalized-time";
130 case SILC_ASN1_TAG_UTF8_STRING:
131 return "utf8-string";
132 case SILC_ASN1_TAG_NUMERIC_STRING:
133 return "numeric-string";
134 case SILC_ASN1_TAG_PRINTABLE_STRING:
135 return "printable-string";
136 case SILC_ASN1_TAG_IA5_STRING:
138 case SILC_ASN1_TAG_VISIBLE_STRING:
139 return "visible-string";
140 case SILC_ASN1_TAG_UNIVERSAL_STRING:
141 return "universal-string";
142 case SILC_ASN1_TAG_UNRESTRICTED_STRING:
143 return "unrestricted-string";
144 case SILC_ASN1_TAG_BMP_STRING:
146 case SILC_ASN1_TAG_ODE:
148 case SILC_ASN1_TAG_ETI:
150 case SILC_ASN1_TAG_REAL:
152 case SILC_ASN1_TAG_EMBEDDED:
154 case SILC_ASN1_TAG_ROI:
156 case SILC_ASN1_TAG_TELETEX_STRING:
157 return "teletex-string";
158 case SILC_ASN1_TAG_VIDEOTEX_STRING:
159 return "videotex-string";
160 case SILC_ASN1_TAG_GRAPHIC_STRING:
161 return "graphic-string";
162 case SILC_ASN1_TAG_GENERAL_STRING:
163 return "general-string";
169 #endif /* SILC_DEBUG */
171 #ifdef SILC_DIST_TOOLKIT
173 /* Dumps the ASN.1 data block into standard output (stdout). */
175 SilcBool silc_asn1_dump(SilcAsn1 asn1, SilcBuffer src)
177 SilcBool ret = FALSE;
178 SilcBerEncoding renc;
180 const unsigned char *rdata;
181 SilcUInt32 rdata_len, len = 0;
184 SILC_LOG_DEBUG(("Dumping ASN.1"));
186 while (silc_buffer_len(src)) {
187 /* Decode the BER block */
188 ret = silc_ber_decode(src, NULL, &renc, &rtag, &rdata,
189 &rdata_len, &rindef, &len);
191 SILC_LOG_DEBUG(("Error parsing BER block, malformed ASN.1 data"));
195 fprintf(stdout, "Type %s [%d]\n",
196 silc_asn1_tag_name(rtag), rtag);
198 if (renc == SILC_BER_ENC_PRIMITIVE)
199 len = len + rdata_len;
204 silc_buffer_pull(src, len);
209 #endif /* SILC_DEBUG */
210 #endif /* SILC_DIST_TOOLKIT */