summaryrefslogtreecommitdiff
path: root/src/libstrongswan/plugins/x509/ietf_attr_list.c
blob: 17f6949b271f529867652d72dc35ad5e635be186 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/* 
 * Copyright (C) 2007 Andreas Steffen, Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include <string.h>
#include <stdio.h>

#include <debug.h>
#include <library.h>

#include <asn1/oid.h>
#include <asn1/asn1.h>
#include <asn1/asn1_parser.h>
#include <utils/lexparser.h>

#include "ietf_attr_list.h"

/**
 * Private definition of ietfAttribute kinds
 */
typedef enum {
	IETF_ATTRIBUTE_OCTETS =	0,
	IETF_ATTRIBUTE_OID =	1,
	IETF_ATTRIBUTE_STRING =	2
} ietfAttribute_t;

typedef struct ietfAttr_t ietfAttr_t;

/**
 * Private definition of an ietfAttribute
 */
struct ietfAttr_t {
	/**
	 * IETF attribute kind
	 */
	ietfAttribute_t kind;

	/**
	 * IETF attribute valuse
	 */
	chunk_t value;

	/**
	 * Compares two ietfAttributes
	 *	
	 * return -1 if this is earlier in the alphabet than other
	 * return  0 if this equals other
	 * return +1 if this is later in the alphabet than other
	 *
	 * @param this		calling object
	 * @param other		other object
	 */
	int (*compare) (const ietfAttr_t *this ,const ietfAttr_t *other);

	/**
	 * Destroys the ietfAttr_t object.
	 * 
	 * @param this			ietfAttr_t to destroy
	 */
	void (*destroy) (ietfAttr_t *this);
};

/**
 * Implements ietfAttr_t.compare.
 */
static int ietfAttr_compare(const ietfAttr_t *this ,const ietfAttr_t *other)
{
	int cmp_len, len, cmp_value;

	/* OID attributes are appended after STRING and OCTETS attributes */
	if (this->kind != IETF_ATTRIBUTE_OID && other->kind == IETF_ATTRIBUTE_OID)
	{
		return -1;
	}
	if (this->kind == IETF_ATTRIBUTE_OID && other->kind != IETF_ATTRIBUTE_OID)
	{
		return 1;
	}
	
    cmp_len = this->value.len - other->value.len;
    len = (cmp_len < 0)? this->value.len : other->value.len;
    cmp_value = memcmp(this->value.ptr, other->value.ptr, len);

    return (cmp_value == 0)? cmp_len : cmp_value;
}

/**
 * Implements ietfAttr_t.destroy.
 */
static void ietfAttr_destroy(ietfAttr_t *this)
{
	free(this->value.ptr);
	free(this);
}

/**
 * Creates an ietfAttr_t object.
 */
static ietfAttr_t *ietfAttr_create(ietfAttribute_t kind, chunk_t value)
{
	ietfAttr_t *this = malloc_thing(ietfAttr_t);

	/* initialize */
	this->kind = kind;
	this->value = chunk_clone(value);

	/* function */
	this->compare = ietfAttr_compare;
	this->destroy = ietfAttr_destroy;

	return this;
}

/**
 * Adds an ietfAttr_t object to a sorted linked list
 */
static void ietfAttr_add(linked_list_t *list, ietfAttr_t *attr)
{
	iterator_t *iterator = list->create_iterator(list, TRUE);
	ietfAttr_t *current_attr;
	bool found = FALSE;

	while (iterator->iterate(iterator, (void **)&current_attr))
	{
		int cmp = attr->compare(attr, current_attr);

		if (cmp > 0)
		{
			 continue;
		}
		if (cmp == 0)
		{
			attr->destroy(attr);
		}
		else
		{
			iterator->insert_before(iterator, attr);
		}
		found = TRUE;
		break;
	}
	iterator->destroy(iterator);
	if (!found)
	{
		list->insert_last(list, attr);
	}
}

/*
 * Described in header.
 */
bool ietfAttr_list_equals(linked_list_t *list_a, linked_list_t *list_b)
{
	 bool result = TRUE;

	/* lists must have the same number of attributes */
	if (list_a->get_count(list_a) != list_b->get_count(list_b))
	{
		return FALSE;
	}
	/* empty lists - no attributes */
	if (list_a->get_count(list_a) == 0)
	{
		return TRUE;
	}

	/* compare two alphabetically-sorted lists */
	{
		iterator_t *iterator_a = list_a->create_iterator(list_a, TRUE);
		iterator_t *iterator_b = list_b->create_iterator(list_b, TRUE);
		ietfAttr_t *attr_a, *attr_b;

		while (iterator_a->iterate(iterator_a, (void **)&attr_a) &&
			   iterator_b->iterate(iterator_b, (void **)&attr_b))
		{
			if (attr_a->compare(attr_a, attr_b) != 0)
			{
				/* we have a mismatch */
				result = FALSE;
				break;
			}
  		}
		iterator_a->destroy(iterator_a);
		iterator_b->destroy(iterator_b);
	}
	return result;
}

/*
 * Described in header.
 */
void ietfAttr_list_list(linked_list_t *list, FILE *out)
{
	iterator_t *iterator = list->create_iterator(list, TRUE);
	ietfAttr_t *attr;
	bool first = TRUE;

	while (iterator->iterate(iterator, (void **)&attr))
	{
		if (first)
		{
			first = FALSE;
		}
		else
		{
			fprintf(out, ", ");
		}

		switch (attr->kind)
		{
			case IETF_ATTRIBUTE_OCTETS:
			case IETF_ATTRIBUTE_STRING:
				fprintf(out, "%.*s", (int)attr->value.len, attr->value.ptr);
				break;
			case IETF_ATTRIBUTE_OID:
				{
					int oid = asn1_known_oid(attr->value);

					if (oid == OID_UNKNOWN)
					{
						fprintf(out, "0x#B", &attr->value);
					}
					else
					{
						fprintf(out, "%s", oid_names[oid]);
					}
				}
	    		break;
			default:
	    		break;
  		}
	}
	iterator->destroy(iterator);
}

/*
 * Described in header.
 */
void ietfAttr_list_create_from_string(char *msg, linked_list_t *list)
{
	chunk_t line = { msg, strlen(msg) };

	while (eat_whitespace(&line))
	{
		chunk_t group;

		/* extract the next comma-separated group attribute */
		if (!extract_token(&group, ',', &line))
		{
			group = line;
			line.len = 0;
		}

		/* remove any trailing spaces */
		while (group.len > 0 && *(group.ptr + group.len - 1) == ' ')
		{
			group.len--;
		}

		/* add the group attribute to the list */
		if (group.len > 0)
		{
			ietfAttr_t *attr = ietfAttr_create(IETF_ATTRIBUTE_STRING, group);
		
			ietfAttr_add(list, attr);
		}
	}
}

/**
 * ASN.1 definition of ietfAttrSyntax
 */
static const asn1Object_t ietfAttrSyntaxObjects[] =
{
	{ 0, "ietfAttrSyntax",		ASN1_SEQUENCE,		ASN1_NONE }, /*  0 */
	{ 1,   "policyAuthority",	ASN1_CONTEXT_C_0,	ASN1_OPT |
													ASN1_BODY }, /*  1 */
	{ 1,   "end opt",			ASN1_EOC,			ASN1_END  }, /*  2 */
	{ 1,   "values",			ASN1_SEQUENCE,		ASN1_LOOP }, /*  3 */
	{ 2,     "octets",			ASN1_OCTET_STRING,	ASN1_OPT |
													ASN1_BODY }, /*  4 */
	{ 2,     "end choice",		ASN1_EOC,			ASN1_END  }, /*  5 */
	{ 2,     "oid",				ASN1_OID,			ASN1_OPT |
													ASN1_BODY }, /*  6 */
	{ 2,     "end choice",		ASN1_EOC,			ASN1_END  }, /*  7 */
	{ 2,     "string",			ASN1_UTF8STRING,	ASN1_OPT |
													ASN1_BODY }, /*  8 */
	{ 2,     "end choice",		ASN1_EOC,			ASN1_END  }, /*  9 */
	{ 1,   "end loop",			ASN1_EOC,			ASN1_END  }, /* 10 */
	{ 0, "exit",				ASN1_EOC,			ASN1_EXIT }
};
#define IETF_ATTR_OCTETS	 4
#define IETF_ATTR_OID		 6
#define IETF_ATTR_STRING	 8

/*
 * Described in header.
 */
void ietfAttr_list_create_from_chunk(chunk_t chunk, linked_list_t *list, int level0)
{
	asn1_parser_t *parser;
	chunk_t object;
	int objectID;

	parser = asn1_parser_create(ietfAttrSyntaxObjects, chunk);
	parser->set_top_level(parser, level0);

	while (parser->iterate(parser, &objectID, &object))
	{
		switch (objectID)
		{
			case IETF_ATTR_OCTETS:
			case IETF_ATTR_OID:
			case IETF_ATTR_STRING:
				{
					ietfAttribute_t kind = (objectID - IETF_ATTR_OCTETS) / 2;
					ietfAttr_t *attr   = ietfAttr_create(kind, object);
					ietfAttr_add(list, attr);
				}
				break;
			default:
				break;
		}
	}
	parser->destroy(parser);
}

/*
 * Described in header.
 */
chunk_t ietfAttr_list_encode(linked_list_t *list)
{
	chunk_t ietfAttributes;
	size_t size = 0;
	u_char *pos;
	iterator_t *iterator = list->create_iterator(list, TRUE);
	ietfAttr_t *attr;

	/* precalculate the total size of all values */
	while (iterator->iterate(iterator, (void **)&attr))
	{
		size_t len = attr->value.len;

		size += 1 + (len > 0) + (len >= 128) + (len >= 256) + (len >= 65536) + len;
	}
	iterator->destroy(iterator);

	pos = asn1_build_object(&ietfAttributes, ASN1_SEQUENCE, size);

	iterator = list->create_iterator(list, TRUE);
	while (iterator->iterate(iterator, (void **)&attr))
	{
		chunk_t ietfAttribute;
		asn1_t type = ASN1_NULL;

		switch (attr->kind)
		{
			case IETF_ATTRIBUTE_OCTETS:
				type = ASN1_OCTET_STRING;
				break;
			case IETF_ATTRIBUTE_STRING:
				type = ASN1_UTF8STRING;
				break;
			case IETF_ATTRIBUTE_OID:
				type = ASN1_OID;
				break;
		}
		ietfAttribute = asn1_simple_object(type, attr->value);

		/* copy ietfAttribute into ietfAttributes chunk */
		memcpy(pos, ietfAttribute.ptr, ietfAttribute.len); 
		pos += ietfAttribute.len;
		free(ietfAttribute.ptr);
	}
	iterator->destroy(iterator);

	return asn1_wrap(ASN1_SEQUENCE, "m", ietfAttributes);
}

/*
 * Described in header.
 */
void ietfAttr_list_destroy(linked_list_t *list)
{
	list->destroy_offset(list, offsetof(ietfAttr_t, destroy));
}