Use python-based swidGenerator to generated SWID tags
[strongswan.git] / src / libimcv / ietf / ietf_attr_installed_packages.c
1 /*
2 * Copyright (C) 2012-2014 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "ietf_attr_installed_packages.h"
17
18 #include <string.h>
19
20 #include <pa_tnc/pa_tnc_msg.h>
21 #include <bio/bio_writer.h>
22 #include <bio/bio_reader.h>
23 #include <collections/linked_list.h>
24 #include <utils/debug.h>
25
26
27 typedef struct private_ietf_attr_installed_packages_t private_ietf_attr_installed_packages_t;
28 typedef struct package_entry_t package_entry_t;
29
30 /**
31 * PA-TNC Installed Packages Type (see section 4.2.7 of RFC 5792)
32 *
33 * 1 2 3
34 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
35 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 * | Reserved | Package Count |
37 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 * | Pkg Name Len | Package Name (Variable Length) |
39 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 * | Version Len | Package Version Number (Variable Length) |
41 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 */
43
44 /**
45 * Private data of an ietf_attr_installed_packages_t object.
46 */
47 struct private_ietf_attr_installed_packages_t {
48
49 /**
50 * Public members of ietf_attr_installed_packages_t
51 */
52 ietf_attr_installed_packages_t public;
53
54 /**
55 * Vendor-specific attribute type
56 */
57 pen_type_t type;
58
59 /**
60 * Attribute value
61 */
62 chunk_t value;
63
64 /**
65 * Noskip flag
66 */
67 bool noskip_flag;
68
69 /**
70 * List of Installed Package entries
71 */
72 linked_list_t *packages;
73
74 /**
75 * Reference count
76 */
77 refcount_t ref;
78 };
79
80 /**
81 * Package entry
82 */
83 struct package_entry_t {
84 chunk_t name;
85 chunk_t version;
86 };
87
88 /**
89 * Free a package entry
90 */
91 static void free_package_entry(package_entry_t *entry)
92 {
93 free(entry->name.ptr);
94 free(entry->version.ptr);
95 free(entry);
96 }
97
98 METHOD(pa_tnc_attr_t, get_type, pen_type_t,
99 private_ietf_attr_installed_packages_t *this)
100 {
101 return this->type;
102 }
103
104 METHOD(pa_tnc_attr_t, get_value, chunk_t,
105 private_ietf_attr_installed_packages_t *this)
106 {
107 return this->value;
108 }
109
110 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
111 private_ietf_attr_installed_packages_t *this)
112 {
113 return this->noskip_flag;
114 }
115
116 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
117 private_ietf_attr_installed_packages_t *this, bool noskip)
118 {
119 this->noskip_flag = noskip;
120 }
121
122 METHOD(pa_tnc_attr_t, build, void,
123 private_ietf_attr_installed_packages_t *this)
124 {
125 bio_writer_t *writer;
126 enumerator_t *enumerator;
127 package_entry_t *entry;
128
129 if (this->value.ptr)
130 {
131 return;
132 }
133 writer = bio_writer_create(IETF_INSTALLED_PACKAGES_MIN_SIZE);
134 writer->write_uint16(writer, 0x0000);
135 writer->write_uint16(writer, this->packages->get_count(this->packages));
136
137 enumerator = this->packages->create_enumerator(this->packages);
138 while (enumerator->enumerate(enumerator, &entry))
139 {
140 writer->write_data8(writer, entry->name);
141 writer->write_data8(writer, entry->version);
142 }
143 enumerator->destroy(enumerator);
144
145 this->value = writer->extract_buf(writer);
146 writer->destroy(writer);
147 }
148
149 METHOD(pa_tnc_attr_t, process, status_t,
150 private_ietf_attr_installed_packages_t *this, u_int32_t *offset)
151 {
152 bio_reader_t *reader;
153 package_entry_t *entry;
154 status_t status = FAILED;
155 chunk_t name, version;
156 u_int16_t reserved, count;
157 u_char *pos;
158
159 *offset = 0;
160
161 if (this->value.len < IETF_INSTALLED_PACKAGES_MIN_SIZE)
162 {
163 DBG1(DBG_TNC, "insufficient data for IETF installed packages");
164 return FAILED;
165 }
166 reader = bio_reader_create(this->value);
167 reader->read_uint16(reader, &reserved);
168 reader->read_uint16(reader, &count);
169 *offset = IETF_INSTALLED_PACKAGES_MIN_SIZE;
170
171 while (reader->remaining(reader))
172 {
173 if (!reader->read_data8(reader, &name))
174 {
175 DBG1(DBG_TNC, "insufficient data for IETF installed package name");
176 goto end;
177 }
178 pos = memchr(name.ptr, '\0', name.len);
179 if (pos)
180 {
181 DBG1(DBG_TNC, "nul termination in IETF installed package name");
182 *offset += 1 + (pos - name.ptr);
183 goto end;
184 }
185 *offset += 1 + name.len;
186
187 if (!reader->read_data8(reader, &version))
188 {
189 DBG1(DBG_TNC, "insufficient data for IETF installed package version");
190 goto end;
191 }
192 pos = memchr(version.ptr, '\0', version.len);
193 if (pos)
194 {
195 DBG1(DBG_TNC, "nul termination in IETF installed package version");
196 *offset += 1 + (pos - version.ptr);
197 goto end;
198 }
199 *offset += 1 + version.len;
200
201 entry = malloc_thing(package_entry_t);
202 entry->name = chunk_clone(name);
203 entry->version = chunk_clone(version);
204 this->packages->insert_last(this->packages, entry);
205 }
206
207 if (count != this->packages->get_count(this->packages))
208 {
209 DBG1(DBG_TNC, "IETF installed package count unequal to "
210 "number of included packages");
211 goto end;
212 }
213 status = SUCCESS;
214
215 end:
216 reader->destroy(reader);
217 return status;
218 }
219
220 METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*,
221 private_ietf_attr_installed_packages_t *this)
222 {
223 ref_get(&this->ref);
224 return &this->public.pa_tnc_attribute;
225 }
226
227 METHOD(pa_tnc_attr_t, destroy, void,
228 private_ietf_attr_installed_packages_t *this)
229 {
230 if (ref_put(&this->ref))
231 {
232 this->packages->destroy_function(this->packages, (void*)free_package_entry);
233 free(this->value.ptr);
234 free(this);
235 }
236 }
237
238 METHOD(ietf_attr_installed_packages_t, add, void,
239 private_ietf_attr_installed_packages_t *this, chunk_t name, chunk_t version)
240 {
241 package_entry_t *entry;
242
243 /* restrict package name and package version number fields to 255 octets */
244 name.len = min(255, name.len);
245 version.len = min(255, version.len);
246
247 entry = malloc_thing(package_entry_t);
248 entry->name = chunk_clone(name);
249 entry->version = chunk_clone(version);
250 this->packages->insert_last(this->packages, entry);
251 }
252
253 /**
254 * Enumerate package filter entries
255 */
256 static bool package_filter(void *null, package_entry_t **entry, chunk_t *name,
257 void *i2, chunk_t *version)
258 {
259 *name = (*entry)->name;
260 *version = (*entry)->version;
261 return TRUE;
262 }
263
264 METHOD(ietf_attr_installed_packages_t, create_enumerator, enumerator_t*,
265 private_ietf_attr_installed_packages_t *this)
266 {
267 return enumerator_create_filter(
268 this->packages->create_enumerator(this->packages),
269 (void*)package_filter, NULL, NULL);
270 }
271
272 /**
273 * Described in header.
274 */
275 pa_tnc_attr_t *ietf_attr_installed_packages_create(void)
276 {
277 private_ietf_attr_installed_packages_t *this;
278
279 INIT(this,
280 .public = {
281 .pa_tnc_attribute = {
282 .get_type = _get_type,
283 .get_value = _get_value,
284 .get_noskip_flag = _get_noskip_flag,
285 .set_noskip_flag = _set_noskip_flag,
286 .build = _build,
287 .process = _process,
288 .get_ref = _get_ref,
289 .destroy = _destroy,
290 },
291 .add = _add,
292 .create_enumerator = _create_enumerator,
293 },
294 .type = { PEN_IETF, IETF_ATTR_INSTALLED_PACKAGES },
295 .packages = linked_list_create(),
296 .ref = 1,
297 );
298
299 return &this->public.pa_tnc_attribute;
300 }
301
302 /**
303 * Described in header.
304 */
305 pa_tnc_attr_t *ietf_attr_installed_packages_create_from_data(chunk_t data)
306 {
307 private_ietf_attr_installed_packages_t *this;
308
309 INIT(this,
310 .public = {
311 .pa_tnc_attribute = {
312 .get_type = _get_type,
313 .get_value = _get_value,
314 .get_noskip_flag = _get_noskip_flag,
315 .set_noskip_flag = _set_noskip_flag,
316 .build = _build,
317 .process = _process,
318 .get_ref = _get_ref,
319 .destroy = _destroy,
320 },
321 .add = _add,
322 .create_enumerator = _create_enumerator,
323 },
324 .type = {PEN_IETF, IETF_ATTR_INSTALLED_PACKAGES },
325 .value = chunk_clone(data),
326 .packages = linked_list_create(),
327 .ref = 1,
328 );
329
330 return &this->public.pa_tnc_attribute;
331 }
332
333