]> git.ipfire.org Git - thirdparty/strongswan.git/blob - src/libpts/tcg/tcg_pts_attr_req_func_comp_evid.c
Moved debug.[ch] to utils folder
[thirdparty/strongswan.git] / src / libpts / tcg / tcg_pts_attr_req_func_comp_evid.c
1 /*
2 * Copyright (C) 2011-2012 Sansar Choinyambuu, 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 "tcg_pts_attr_req_func_comp_evid.h"
17
18 #include <pa_tnc/pa_tnc_msg.h>
19 #include <bio/bio_writer.h>
20 #include <bio/bio_reader.h>
21 #include <collections/linked_list.h>
22 #include <utils/debug.h>
23
24 typedef struct private_tcg_pts_attr_req_func_comp_evid_t private_tcg_pts_attr_req_func_comp_evid_t;
25
26 /**
27 * Request Functional Component Evidence
28 * see section 3.14.1 of PTS Protocol: Binding to TNC IF-M Specification
29 *
30 * 1 2 3
31 * 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
32 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33 * | Flags | Sub-component Depth (for Component #1) |
34 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 * | Component Functional Name #1 |
36 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 * | Component Functional Name #1 |
38 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 * | ........ |
40 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 * | Flags | Sub-component Depth (for Component #N) |
42 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 * | Component Functional Name #N |
44 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 * | Component Functional Name #N |
46 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 */
48
49 /**
50 * Component Functional Name Structure
51 * (see section 5.1 of PTS Protocol: Binding to TNC IF-M Specification)
52 *
53 * 1 2 3
54 * 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
55 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56 * | Component Functional Name Vendor ID |Fam| Qualifier |
57 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58 * | Component Functional Name |
59 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60 */
61
62 #define PTS_REQ_FUNC_COMP_EVID_SIZE 12
63 #define PTS_REQ_FUNC_COMP_FAMILY_MASK 0xC0
64
65 /**
66 * Private data of an tcg_pts_attr_req_func_comp_evid_t object.
67 */
68 struct private_tcg_pts_attr_req_func_comp_evid_t {
69
70 /**
71 * Public members of tcg_pts_attr_req_func_comp_evid_t
72 */
73 tcg_pts_attr_req_func_comp_evid_t public;
74
75 /**
76 * Vendor-specific attribute type
77 */
78 pen_type_t type;
79
80 /**
81 * Attribute value
82 */
83 chunk_t value;
84
85 /**
86 * Noskip flag
87 */
88 bool noskip_flag;
89
90 /**
91 * List of Functional Components
92 */
93 linked_list_t *list;
94
95 /**
96 * Reference count
97 */
98 refcount_t ref;
99 };
100
101 typedef struct entry_t entry_t;
102
103 /**
104 * Functional component entry
105 */
106 struct entry_t {
107 u_int8_t flags;
108 u_int32_t depth;
109 pts_comp_func_name_t *name;
110 };
111
112 /**
113 * Enumerate functional component entries
114 */
115 static bool entry_filter(void *null, entry_t **entry, u_int8_t *flags,
116 void *i2, u_int32_t *depth, void *i3,
117 pts_comp_func_name_t **name)
118 {
119 *flags = (*entry)->flags;
120 *depth = (*entry)->depth;
121 *name = (*entry)->name;
122
123 return TRUE;
124 }
125
126 /**
127 * Free an entry_t object
128 */
129 static void free_entry(entry_t *this)
130 {
131 if (this)
132 {
133 this->name->destroy(this->name);
134 free(this);
135 }
136 }
137
138 METHOD(pa_tnc_attr_t, get_type, pen_type_t,
139 private_tcg_pts_attr_req_func_comp_evid_t *this)
140 {
141 return this->type;
142 }
143
144 METHOD(pa_tnc_attr_t, get_value, chunk_t,
145 private_tcg_pts_attr_req_func_comp_evid_t *this)
146 {
147 return this->value;
148 }
149
150 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
151 private_tcg_pts_attr_req_func_comp_evid_t *this)
152 {
153 return this->noskip_flag;
154 }
155
156 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
157 private_tcg_pts_attr_req_func_comp_evid_t *this, bool noskip)
158 {
159 this->noskip_flag = noskip;
160 }
161
162 METHOD(pa_tnc_attr_t, build, void,
163 private_tcg_pts_attr_req_func_comp_evid_t *this)
164 {
165 bio_writer_t *writer;
166 enumerator_t *enumerator;
167 entry_t *entry;
168
169 if (this->value.ptr)
170 {
171 return;
172 }
173 writer = bio_writer_create(PTS_REQ_FUNC_COMP_EVID_SIZE);
174
175 enumerator = this->list->create_enumerator(this->list);
176 while (enumerator->enumerate(enumerator, &entry))
177 {
178 writer->write_uint8 (writer, entry->flags);
179 writer->write_uint24(writer, entry->depth);
180 writer->write_uint24(writer, entry->name->get_vendor_id(entry->name));
181 writer->write_uint8 (writer, entry->name->get_qualifier(entry->name));
182 writer->write_uint32(writer, entry->name->get_name(entry->name));
183 }
184 enumerator->destroy(enumerator);
185
186 this->value = chunk_clone(writer->get_buf(writer));
187 writer->destroy(writer);
188 }
189
190 METHOD(pa_tnc_attr_t, process, status_t,
191 private_tcg_pts_attr_req_func_comp_evid_t *this, u_int32_t *offset)
192 {
193 bio_reader_t *reader;
194 u_int32_t depth, vendor_id, name;
195 u_int8_t flags, fam_and_qualifier, qualifier;
196 status_t status = FAILED;
197 entry_t *entry = NULL;
198
199 if (this->value.len < PTS_REQ_FUNC_COMP_EVID_SIZE)
200 {
201 DBG1(DBG_TNC, "insufficient data for Request Functional "
202 "Component Evidence");
203 *offset = 0;
204 return FAILED;
205 }
206 reader = bio_reader_create(this->value);
207
208 while (reader->remaining(reader))
209 {
210 if (!reader->read_uint8(reader, &flags))
211 {
212 DBG1(DBG_TNC, "insufficient data for PTS Request Functional "
213 "Component Evidence Flags");
214 goto end;
215 }
216 if (!reader->read_uint24(reader, &depth))
217 {
218 DBG1(DBG_TNC, "insufficient data for PTS Request Functional "
219 "Component Evidence Sub Component Depth");
220 goto end;
221 }
222 if (!reader->read_uint24(reader, &vendor_id))
223 {
224 DBG1(DBG_TNC, "insufficient data for PTS Request Functional "
225 "Component Evidence Component Name Vendor ID");
226 goto end;
227 }
228 if (!reader->read_uint8(reader, &fam_and_qualifier))
229 {
230 DBG1(DBG_TNC, "insufficient data for PTS Request Functional "
231 "Component Evidence Family and Qualifier");
232 goto end;
233 }
234 if (fam_and_qualifier & PTS_REQ_FUNC_COMP_FAMILY_MASK)
235 {
236 DBG1(DBG_TNC, "the Functional Name Encoding Family "
237 "is not Binary Enumeration");
238 goto end;
239 }
240 if (!reader->read_uint32(reader, &name))
241 {
242 DBG1(DBG_TNC, "insufficient data for PTS Request Functional "
243 "Component Evidence Component Functional Name");
244 goto end;
245 }
246 qualifier = fam_and_qualifier & ~PTS_REQ_FUNC_COMP_FAMILY_MASK;
247
248 entry = malloc_thing(entry_t);
249 entry->flags = flags;
250 entry->depth = depth;
251 entry->name = pts_comp_func_name_create(vendor_id, name, qualifier);
252
253 this->list->insert_last(this->list, entry);
254 }
255 status = SUCCESS;
256
257 end:
258 reader->destroy(reader);
259 return status;
260 }
261
262 METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*,
263 private_tcg_pts_attr_req_func_comp_evid_t *this)
264 {
265 ref_get(&this->ref);
266 return &this->public.pa_tnc_attribute;
267 }
268
269 METHOD(pa_tnc_attr_t, destroy, void,
270 private_tcg_pts_attr_req_func_comp_evid_t *this)
271 {
272 if (ref_put(&this->ref))
273 {
274 this->list->destroy_function(this->list, (void *)free_entry);
275 free(this->value.ptr);
276 free(this);
277 }
278 }
279
280 METHOD(tcg_pts_attr_req_func_comp_evid_t, add_component, void,
281 private_tcg_pts_attr_req_func_comp_evid_t *this, u_int8_t flags,
282 u_int32_t depth, pts_comp_func_name_t *name)
283 {
284 entry_t *entry;
285
286 entry = malloc_thing(entry_t);
287 entry->flags = flags;
288 entry->depth = depth;
289 entry->name = name;
290 this->list->insert_last(this->list, entry);
291 }
292
293 METHOD(tcg_pts_attr_req_func_comp_evid_t, get_count, int,
294 private_tcg_pts_attr_req_func_comp_evid_t *this)
295 {
296 return this->list->get_count(this->list);
297 }
298
299 METHOD(tcg_pts_attr_req_func_comp_evid_t, create_enumerator, enumerator_t*,
300 private_tcg_pts_attr_req_func_comp_evid_t *this)
301 {
302 return enumerator_create_filter(this->list->create_enumerator(this->list),
303 (void*)entry_filter, NULL, NULL);
304 }
305
306 /**
307 * Described in header.
308 */
309 pa_tnc_attr_t *tcg_pts_attr_req_func_comp_evid_create(void)
310 {
311 private_tcg_pts_attr_req_func_comp_evid_t *this;
312
313 INIT(this,
314 .public = {
315 .pa_tnc_attribute = {
316 .get_type = _get_type,
317 .get_value = _get_value,
318 .get_noskip_flag = _get_noskip_flag,
319 .set_noskip_flag = _set_noskip_flag,
320 .build = _build,
321 .process = _process,
322 .get_ref = _get_ref,
323 .destroy = _destroy,
324 },
325 .add_component = _add_component,
326 .get_count = _get_count,
327 .create_enumerator = _create_enumerator,
328 },
329 .type = { PEN_TCG, TCG_PTS_REQ_FUNC_COMP_EVID },
330 .list = linked_list_create(),
331 .ref = 1,
332 );
333
334 return &this->public.pa_tnc_attribute;
335 }
336
337 /**
338 * Described in header.
339 */
340 pa_tnc_attr_t *tcg_pts_attr_req_func_comp_evid_create_from_data(chunk_t data)
341 {
342 private_tcg_pts_attr_req_func_comp_evid_t *this;
343
344 INIT(this,
345 .public = {
346 .pa_tnc_attribute = {
347 .get_type = _get_type,
348 .get_value = _get_value,
349 .get_noskip_flag = _get_noskip_flag,
350 .set_noskip_flag = _set_noskip_flag,
351 .build = _build,
352 .process = _process,
353 .get_ref = _get_ref,
354 .destroy = _destroy,
355 },
356 .add_component = _add_component,
357 .get_count = _get_count,
358 .create_enumerator = _create_enumerator,
359 },
360 .type = { PEN_TCG, TCG_PTS_REQ_FUNC_COMP_EVID },
361 .list = linked_list_create(),
362 .value = chunk_clone(data),
363 .ref = 1,
364 );
365
366 return &this->public.pa_tnc_attribute;
367 }