refactoring of the ASN.1 parser
[strongswan.git] / src / libstrongswan / plugins / x509 / ietf_attr_list.c
1 /*
2 * Copyright (C) 2007 Andreas Steffen, Hochschule fuer Technik Rapperswil
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 */
14
15 #include <string.h>
16 #include <stdio.h>
17
18 #include <debug.h>
19 #include <library.h>
20
21 #include <asn1/oid.h>
22 #include <asn1/asn1.h>
23 #include <asn1/asn1_parser.h>
24 #include <utils/lexparser.h>
25
26 #include "ietf_attr_list.h"
27
28 /**
29 * Private definition of ietfAttribute kinds
30 */
31 typedef enum {
32 IETF_ATTRIBUTE_OCTETS = 0,
33 IETF_ATTRIBUTE_OID = 1,
34 IETF_ATTRIBUTE_STRING = 2
35 } ietfAttribute_t;
36
37 typedef struct ietfAttr_t ietfAttr_t;
38
39 /**
40 * Private definition of an ietfAttribute
41 */
42 struct ietfAttr_t {
43 /**
44 * IETF attribute kind
45 */
46 ietfAttribute_t kind;
47
48 /**
49 * IETF attribute valuse
50 */
51 chunk_t value;
52
53 /**
54 * Compares two ietfAttributes
55 *
56 * return -1 if this is earlier in the alphabet than other
57 * return 0 if this equals other
58 * return +1 if this is later in the alphabet than other
59 *
60 * @param this calling object
61 * @param other other object
62 */
63 int (*compare) (const ietfAttr_t *this ,const ietfAttr_t *other);
64
65 /**
66 * Destroys the ietfAttr_t object.
67 *
68 * @param this ietfAttr_t to destroy
69 */
70 void (*destroy) (ietfAttr_t *this);
71 };
72
73 /**
74 * Implements ietfAttr_t.compare.
75 */
76 static int ietfAttr_compare(const ietfAttr_t *this ,const ietfAttr_t *other)
77 {
78 int cmp_len, len, cmp_value;
79
80 /* OID attributes are appended after STRING and OCTETS attributes */
81 if (this->kind != IETF_ATTRIBUTE_OID && other->kind == IETF_ATTRIBUTE_OID)
82 {
83 return -1;
84 }
85 if (this->kind == IETF_ATTRIBUTE_OID && other->kind != IETF_ATTRIBUTE_OID)
86 {
87 return 1;
88 }
89
90 cmp_len = this->value.len - other->value.len;
91 len = (cmp_len < 0)? this->value.len : other->value.len;
92 cmp_value = memcmp(this->value.ptr, other->value.ptr, len);
93
94 return (cmp_value == 0)? cmp_len : cmp_value;
95 }
96
97 /**
98 * Implements ietfAttr_t.destroy.
99 */
100 static void ietfAttr_destroy(ietfAttr_t *this)
101 {
102 free(this->value.ptr);
103 free(this);
104 }
105
106 /**
107 * Creates an ietfAttr_t object.
108 */
109 static ietfAttr_t *ietfAttr_create(ietfAttribute_t kind, chunk_t value)
110 {
111 ietfAttr_t *this = malloc_thing(ietfAttr_t);
112
113 /* initialize */
114 this->kind = kind;
115 this->value = chunk_clone(value);
116
117 /* function */
118 this->compare = ietfAttr_compare;
119 this->destroy = ietfAttr_destroy;
120
121 return this;
122 }
123
124 /**
125 * Adds an ietfAttr_t object to a sorted linked list
126 */
127 static void ietfAttr_add(linked_list_t *list, ietfAttr_t *attr)
128 {
129 iterator_t *iterator = list->create_iterator(list, TRUE);
130 ietfAttr_t *current_attr;
131 bool found = FALSE;
132
133 while (iterator->iterate(iterator, (void **)&current_attr))
134 {
135 int cmp = attr->compare(attr, current_attr);
136
137 if (cmp > 0)
138 {
139 continue;
140 }
141 if (cmp == 0)
142 {
143 attr->destroy(attr);
144 }
145 else
146 {
147 iterator->insert_before(iterator, attr);
148 }
149 found = TRUE;
150 break;
151 }
152 iterator->destroy(iterator);
153 if (!found)
154 {
155 list->insert_last(list, attr);
156 }
157 }
158
159 /*
160 * Described in header.
161 */
162 bool ietfAttr_list_equals(linked_list_t *list_a, linked_list_t *list_b)
163 {
164 bool result = TRUE;
165
166 /* lists must have the same number of attributes */
167 if (list_a->get_count(list_a) != list_b->get_count(list_b))
168 {
169 return FALSE;
170 }
171 /* empty lists - no attributes */
172 if (list_a->get_count(list_a) == 0)
173 {
174 return TRUE;
175 }
176
177 /* compare two alphabetically-sorted lists */
178 {
179 iterator_t *iterator_a = list_a->create_iterator(list_a, TRUE);
180 iterator_t *iterator_b = list_b->create_iterator(list_b, TRUE);
181 ietfAttr_t *attr_a, *attr_b;
182
183 while (iterator_a->iterate(iterator_a, (void **)&attr_a) &&
184 iterator_b->iterate(iterator_b, (void **)&attr_b))
185 {
186 if (attr_a->compare(attr_a, attr_b) != 0)
187 {
188 /* we have a mismatch */
189 result = FALSE;
190 break;
191 }
192 }
193 iterator_a->destroy(iterator_a);
194 iterator_b->destroy(iterator_b);
195 }
196 return result;
197 }
198
199 /*
200 * Described in header.
201 */
202 void ietfAttr_list_list(linked_list_t *list, FILE *out)
203 {
204 iterator_t *iterator = list->create_iterator(list, TRUE);
205 ietfAttr_t *attr;
206 bool first = TRUE;
207
208 while (iterator->iterate(iterator, (void **)&attr))
209 {
210 if (first)
211 {
212 first = FALSE;
213 }
214 else
215 {
216 fprintf(out, ", ");
217 }
218
219 switch (attr->kind)
220 {
221 case IETF_ATTRIBUTE_OCTETS:
222 case IETF_ATTRIBUTE_STRING:
223 fprintf(out, "%.*s", (int)attr->value.len, attr->value.ptr);
224 break;
225 case IETF_ATTRIBUTE_OID:
226 {
227 int oid = asn1_known_oid(attr->value);
228
229 if (oid == OID_UNKNOWN)
230 {
231 fprintf(out, "0x#B", &attr->value);
232 }
233 else
234 {
235 fprintf(out, "%s", oid_names[oid]);
236 }
237 }
238 break;
239 default:
240 break;
241 }
242 }
243 iterator->destroy(iterator);
244 }
245
246 /*
247 * Described in header.
248 */
249 void ietfAttr_list_create_from_string(char *msg, linked_list_t *list)
250 {
251 chunk_t line = { msg, strlen(msg) };
252
253 while (eat_whitespace(&line))
254 {
255 chunk_t group;
256
257 /* extract the next comma-separated group attribute */
258 if (!extract_token(&group, ',', &line))
259 {
260 group = line;
261 line.len = 0;
262 }
263
264 /* remove any trailing spaces */
265 while (group.len > 0 && *(group.ptr + group.len - 1) == ' ')
266 {
267 group.len--;
268 }
269
270 /* add the group attribute to the list */
271 if (group.len > 0)
272 {
273 ietfAttr_t *attr = ietfAttr_create(IETF_ATTRIBUTE_STRING, group);
274
275 ietfAttr_add(list, attr);
276 }
277 }
278 }
279
280 /**
281 * ASN.1 definition of ietfAttrSyntax
282 */
283 static const asn1Object_t ietfAttrSyntaxObjects[] =
284 {
285 { 0, "ietfAttrSyntax", ASN1_SEQUENCE, ASN1_NONE }, /* 0 */
286 { 1, "policyAuthority", ASN1_CONTEXT_C_0, ASN1_OPT |
287 ASN1_BODY }, /* 1 */
288 { 1, "end opt", ASN1_EOC, ASN1_END }, /* 2 */
289 { 1, "values", ASN1_SEQUENCE, ASN1_LOOP }, /* 3 */
290 { 2, "octets", ASN1_OCTET_STRING, ASN1_OPT |
291 ASN1_BODY }, /* 4 */
292 { 2, "end choice", ASN1_EOC, ASN1_END }, /* 5 */
293 { 2, "oid", ASN1_OID, ASN1_OPT |
294 ASN1_BODY }, /* 6 */
295 { 2, "end choice", ASN1_EOC, ASN1_END }, /* 7 */
296 { 2, "string", ASN1_UTF8STRING, ASN1_OPT |
297 ASN1_BODY }, /* 8 */
298 { 2, "end choice", ASN1_EOC, ASN1_END }, /* 9 */
299 { 1, "end loop", ASN1_EOC, ASN1_END } /* 10 */
300 };
301 #define IETF_ATTR_OCTETS 4
302 #define IETF_ATTR_OID 6
303 #define IETF_ATTR_STRING 8
304 #define IETF_ATTR_ROOF 11
305
306 /*
307 * Described in header.
308 */
309 void ietfAttr_list_create_from_chunk(chunk_t chunk, linked_list_t *list, int level0)
310 {
311 asn1_parser_t *parser;
312 chunk_t object;
313 int objectID;
314
315 parser = asn1_parser_create(ietfAttrSyntaxObjects, IETF_ATTR_ROOF, chunk);
316 parser->set_top_level(parser, level0);
317
318 while (parser->iterate(parser, &objectID, &object))
319 {
320 switch (objectID)
321 {
322 case IETF_ATTR_OCTETS:
323 case IETF_ATTR_OID:
324 case IETF_ATTR_STRING:
325 {
326 ietfAttribute_t kind = (objectID - IETF_ATTR_OCTETS) / 2;
327 ietfAttr_t *attr = ietfAttr_create(kind, object);
328 ietfAttr_add(list, attr);
329 }
330 break;
331 default:
332 break;
333 }
334 }
335 parser->destroy(parser);
336 }
337
338 /*
339 * Described in header.
340 */
341 chunk_t ietfAttr_list_encode(linked_list_t *list)
342 {
343 chunk_t ietfAttributes;
344 size_t size = 0;
345 u_char *pos;
346 iterator_t *iterator = list->create_iterator(list, TRUE);
347 ietfAttr_t *attr;
348
349 /* precalculate the total size of all values */
350 while (iterator->iterate(iterator, (void **)&attr))
351 {
352 size_t len = attr->value.len;
353
354 size += 1 + (len > 0) + (len >= 128) + (len >= 256) + (len >= 65536) + len;
355 }
356 iterator->destroy(iterator);
357
358 pos = asn1_build_object(&ietfAttributes, ASN1_SEQUENCE, size);
359
360 iterator = list->create_iterator(list, TRUE);
361 while (iterator->iterate(iterator, (void **)&attr))
362 {
363 chunk_t ietfAttribute;
364 asn1_t type = ASN1_NULL;
365
366 switch (attr->kind)
367 {
368 case IETF_ATTRIBUTE_OCTETS:
369 type = ASN1_OCTET_STRING;
370 break;
371 case IETF_ATTRIBUTE_STRING:
372 type = ASN1_UTF8STRING;
373 break;
374 case IETF_ATTRIBUTE_OID:
375 type = ASN1_OID;
376 break;
377 }
378 ietfAttribute = asn1_simple_object(type, attr->value);
379
380 /* copy ietfAttribute into ietfAttributes chunk */
381 memcpy(pos, ietfAttribute.ptr, ietfAttribute.len);
382 pos += ietfAttribute.len;
383 free(ietfAttribute.ptr);
384 }
385 iterator->destroy(iterator);
386
387 return asn1_wrap(ASN1_SEQUENCE, "m", ietfAttributes);
388 }
389
390 /*
391 * Described in header.
392 */
393 void ietfAttr_list_destroy(linked_list_t *list)
394 {
395 list->destroy_offset(list, offsetof(ietfAttr_t, destroy));
396 }