Moved log message for unexpected ASN.1 objects to level 2.
[strongswan.git] / src / libstrongswan / asn1 / asn1_parser.c
1 /*
2 * Copyright (C) 2006 Martin Will
3 * Copyright (C) 2000-2008 Andreas Steffen
4 *
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <time.h>
21
22 #include <debug.h>
23
24 #include "asn1.h"
25 #include "asn1_parser.h"
26
27 #define ASN1_MAX_LEVEL 10
28
29 typedef struct private_asn1_parser_t private_asn1_parser_t;
30
31 /**
32 * Private data of an asn1_cxt_t object.
33 */
34 struct private_asn1_parser_t {
35 /**
36 * Public interface.
37 */
38 asn1_parser_t public;
39
40 /**
41 * Syntax definition of ASN.1 object
42 */
43 asn1Object_t const *objects;
44
45 /**
46 * Current syntax definition line
47 */
48 int line;
49
50 /**
51 * Current stat of the parsing operation
52 */
53 bool success;
54
55 /**
56 * Declare object data as private - use debug level 4 to log it
57 */
58 bool private;
59
60 /**
61 * Top-most type is implicit - ignore it
62 */
63 bool implicit;
64
65 /**
66 * Top-most parsing level - defaults to 0
67 */
68 u_int level0;
69
70 /**
71 * Jump back address for loops for each level
72 */
73 int loopAddr[ASN1_MAX_LEVEL + 1];
74
75 /**
76 * Current parsing pointer for each level
77 */
78 chunk_t blobs[ASN1_MAX_LEVEL + 2];
79 };
80
81 METHOD(asn1_parser_t, iterate, bool,
82 private_asn1_parser_t *this, int *objectID, chunk_t *object)
83 {
84 chunk_t *blob, *blob1;
85 u_char *start_ptr;
86 u_int level;
87 asn1Object_t obj;
88
89 *object = chunk_empty;
90
91 /* Advance to the next object syntax definition line */
92 obj = this->objects[++(this->line)];
93
94 /* Terminate if the end of the object syntax definition has been reached */
95 if (obj.flags & ASN1_EXIT)
96 {
97 return FALSE;
98 }
99
100 if (obj.flags & ASN1_END) /* end of loop or option found */
101 {
102 if (this->loopAddr[obj.level] && this->blobs[obj.level+1].len > 0)
103 {
104 this->line = this->loopAddr[obj.level]; /* another iteration */
105 obj = this->objects[this->line];
106 }
107 else
108 {
109 this->loopAddr[obj.level] = 0; /* exit loop or option*/
110 goto end;
111 }
112 }
113
114 level = this->level0 + obj.level;
115 blob = this->blobs + obj.level;
116 blob1 = blob + 1;
117 start_ptr = blob->ptr;
118
119 /* handle ASN.1 defaults values */
120 if ((obj.flags & ASN1_DEF) && (blob->len == 0 || *start_ptr != obj.type) )
121 {
122 /* field is missing */
123 DBG2(DBG_ASN, "L%d - %s:", level, obj.name);
124 if (obj.type & ASN1_CONSTRUCTED)
125 {
126 this->line++ ; /* skip context-specific tag */
127 }
128 goto end;
129 }
130
131 /* handle ASN.1 options */
132
133 if ((obj.flags & ASN1_OPT)
134 && (blob->len == 0 || *start_ptr != obj.type))
135 {
136 /* advance to end of missing option field */
137 do
138 {
139 this->line++;
140 }
141 while (!((this->objects[this->line].flags & ASN1_END) &&
142 (this->objects[this->line].level == obj.level)));
143 goto end;
144 }
145
146 /* an ASN.1 object must possess at least a tag and length field */
147
148 if (blob->len < 2)
149 {
150 DBG1(DBG_ASN, "L%d - %s: ASN.1 object smaller than 2 octets",
151 level, obj.name);
152 this->success = FALSE;
153 goto end;
154 }
155
156 blob1->len = asn1_length(blob);
157
158 if (blob1->len == ASN1_INVALID_LENGTH)
159 {
160 DBG1(DBG_ASN, "L%d - %s: length of ASN.1 object invalid or too large",
161 level, obj.name);
162 this->success = FALSE;
163 }
164
165 blob1->ptr = blob->ptr;
166 blob->ptr += blob1->len;
167 blob->len -= blob1->len;
168
169 /* return raw ASN.1 object without prior type checking */
170
171 if (obj.flags & ASN1_RAW)
172 {
173 DBG2(DBG_ASN, "L%d - %s:", level, obj.name);
174 object->ptr = start_ptr;
175 object->len = (size_t)(blob->ptr - start_ptr);
176 goto end;
177 }
178
179 if (*start_ptr != obj.type && !(this->implicit && this->line == 0))
180 {
181 DBG2(DBG_ASN, "L%d - %s: ASN1 tag 0x%02x expected, but is 0x%02x",
182 level, obj.name, obj.type, *start_ptr);
183 DBG3(DBG_ASN, "%b", start_ptr, (u_int)(blob->ptr - start_ptr));
184 this->success = FALSE;
185 goto end;
186 }
187
188 DBG2(DBG_ASN, "L%d - %s:", level, obj.name);
189
190 /* In case of "SEQUENCE OF" or "SET OF" start a loop */
191 if (obj.flags & ASN1_LOOP)
192 {
193 if (blob1->len > 0)
194 {
195 /* at least one item, start the loop */
196 this->loopAddr[obj.level] = this->line + 1;
197 }
198 else
199 {
200 /* no items, advance directly to end of loop */
201 do
202 {
203 this->line++;
204 }
205 while (!((this->objects[this->line].flags & ASN1_END) &&
206 (this->objects[this->line].level == obj.level)));
207 goto end;
208 }
209 }
210
211 if (obj.flags & ASN1_OBJ)
212 {
213 object->ptr = start_ptr;
214 object->len = (size_t)(blob->ptr - start_ptr);
215 if (this->private)
216 {
217 DBG4(DBG_ASN, "%B", object);
218 }
219 else
220 {
221 DBG3(DBG_ASN, "%B", object);
222 }
223 }
224 else if (obj.flags & ASN1_BODY)
225 {
226 *object = *blob1;
227 asn1_debug_simple_object(*object, obj.type, this->private);
228 }
229
230 end:
231 *objectID = this->line;
232 return this->success;
233 }
234
235 METHOD(asn1_parser_t, get_level, u_int,
236 private_asn1_parser_t *this)
237 {
238 return this->level0 + this->objects[this->line].level;
239 }
240
241 METHOD(asn1_parser_t, set_top_level, void,
242 private_asn1_parser_t *this, u_int level0)
243 {
244 this->level0 = level0;
245 }
246
247 METHOD(asn1_parser_t, set_flags, void,
248 private_asn1_parser_t *this, bool implicit, bool private)
249 {
250 this->implicit = implicit;
251 this->private = private;
252 }
253
254 METHOD(asn1_parser_t, success, bool,
255 private_asn1_parser_t *this)
256 {
257 return this->success;
258 }
259
260 METHOD(asn1_parser_t, destroy, void,
261 private_asn1_parser_t *this)
262 {
263 free(this);
264 }
265
266 /**
267 * Defined in header.
268 */
269 asn1_parser_t* asn1_parser_create(asn1Object_t const *objects, chunk_t blob)
270 {
271 private_asn1_parser_t *this;
272
273 INIT(this,
274 .public = {
275 .iterate = _iterate,
276 .get_level = _get_level,
277 .set_top_level = _set_top_level,
278 .set_flags = _set_flags,
279 .success = _success,
280 .destroy = _destroy,
281 },
282 .objects = objects,
283 .blobs[0] = blob,
284 .line = -1,
285 .success = TRUE,
286 );
287
288 return &this->public;
289 }