shortened some type names and enforced encoding rules
[strongswan.git] / src / libimcv / tcg / tcg_pts_attr_req_funct_comp_evid.c
1 /*
2 * Copyright (C) 2011 Sansar Choinyambuu
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_funct_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 <debug.h>
22
23 typedef struct private_tcg_pts_attr_req_funct_comp_evid_t private_tcg_pts_attr_req_funct_comp_evid_t;
24
25 /**
26 * Request Functional Component Evidence
27 * see section 3.14.1 of PTS Protocol: Binding to TNC IF-M Specification
28 *
29 * 1 2 3
30 * 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
31 *
32 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33 * | Flags | Sub-component Depth |
34 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 * | Component Functional Name |
36 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 *
38 */
39
40 /**
41 * Component Functional Name Structure (see section 5.1 of PTS Protocol: Binding to TNC IF-M Specification)
42 *
43 * 1 2 3
44 * 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
45 *
46 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 * | Component Functional Name Vendor ID |Fam| Qualifier |
48 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 * | Component Functional Name |
50 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 *
52 */
53
54 /**
55 * Qualifier for Functional Component
56 * see section 5.2 of PTS Protocol: Binding to TNC IF-M Specification
57 *
58 *
59 * 0 1 2 3 4 5
60 * +-+-+-+-+-+-+
61 * |K|S| Type |
62 * +-+-+-+-+-+-+
63 */
64
65 #define PTS_REQ_FUNCT_COMP_EVID_SIZE 12
66 #define PTS_REQ_FUNCT_COMP_FAM_BIN_ENUM 0x00
67
68 /**
69 * Private data of an tcg_pts_attr_req_funct_comp_evid_t object.
70 */
71 struct private_tcg_pts_attr_req_funct_comp_evid_t {
72
73 /**
74 * Public members of tcg_pts_attr_req_funct_comp_evid_t
75 */
76 tcg_pts_attr_req_funct_comp_evid_t public;
77
78 /**
79 * Attribute vendor ID
80 */
81 pen_t vendor_id;
82
83 /**
84 * Attribute type
85 */
86 u_int32_t type;
87
88 /**
89 * Attribute value
90 */
91 chunk_t value;
92
93 /**
94 * Noskip flag
95 */
96 bool noskip_flag;
97
98 /**
99 * Set of flags for Request Functional Component
100 */
101 pts_attr_req_funct_comp_evid_flag_t flags;
102
103 /**
104 * Sub-component Depth
105 */
106 u_int32_t depth;
107
108 /**
109 * Component Functional Name Vendor ID
110 */
111 u_int32_t comp_vendor_id;
112
113 /**
114 * Functional Name Encoding Family
115 */
116 u_int8_t family;
117
118 /**
119 * Functional Name Category Qualifier
120 */
121 tcg_pts_qualifier_t qualifier;
122
123 /**
124 * Component Functional Name
125 */
126 pts_attr_req_funct_comp_name_bin_enum_t name;
127 };
128
129 METHOD(pa_tnc_attr_t, get_vendor_id, pen_t,
130 private_tcg_pts_attr_req_funct_comp_evid_t *this)
131 {
132 return this->vendor_id;
133 }
134
135 METHOD(pa_tnc_attr_t, get_type, u_int32_t,
136 private_tcg_pts_attr_req_funct_comp_evid_t *this)
137 {
138 return this->type;
139 }
140
141 METHOD(pa_tnc_attr_t, get_value, chunk_t,
142 private_tcg_pts_attr_req_funct_comp_evid_t *this)
143 {
144 return this->value;
145 }
146
147 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
148 private_tcg_pts_attr_req_funct_comp_evid_t *this)
149 {
150 return this->noskip_flag;
151 }
152
153 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
154 private_tcg_pts_attr_req_funct_comp_evid_t *this, bool noskip)
155 {
156 this->noskip_flag = noskip;
157 }
158
159 METHOD(pa_tnc_attr_t, build, void,
160 private_tcg_pts_attr_req_funct_comp_evid_t *this)
161 {
162 bio_writer_t *writer;
163 u_int8_t flags = 0;
164 u_int8_t qualifier = 0;
165
166 writer = bio_writer_create(PTS_REQ_FUNCT_COMP_EVID_SIZE);
167
168 /* Determine the flags to set*/
169 if (this->flags & PTS_REQ_FUNC_COMP_FLAG_PCR)
170 {
171 flags += 128;
172 }
173 if (this->flags & PTS_REQ_FUNC_COMP_FLAG_CURR)
174 {
175 flags += 64;
176 }
177 if (this->flags & PTS_REQ_FUNC_COMP_FLAG_VER)
178 {
179 flags += 32;
180 }
181 if (this->flags & PTS_REQ_FUNC_COMP_FLAG_TTC)
182 {
183 flags += 16;
184 }
185 writer->write_uint8(writer, flags);
186
187 writer->write_uint24 (writer, this->depth);
188 writer->write_uint24 (writer, this->comp_vendor_id);
189
190 if(this->family != PTS_REQ_FUNCT_COMP_FAM_BIN_ENUM)
191 {
192 DBG1(DBG_TNC, "Functional Name Encoding Family is not set to 00");
193 }
194
195 qualifier += this->qualifier.type;
196 if (this->qualifier.kernel)
197 {
198 qualifier += 16;
199 }
200 if (this->qualifier.sub_component)
201 {
202 qualifier += 32;
203 }
204 writer->write_uint8 (writer, qualifier);
205 writer->write_uint32 (writer, this->name);
206
207 this->value = chunk_clone(writer->get_buf(writer));
208 writer->destroy(writer);
209 }
210
211 METHOD(pa_tnc_attr_t, process, status_t,
212 private_tcg_pts_attr_req_funct_comp_evid_t *this, u_int32_t *offset)
213 {
214 bio_reader_t *reader;
215 u_int8_t flags;
216 u_int8_t fam_and_qualifier;
217
218 if (this->value.len < PTS_REQ_FUNCT_COMP_EVID_SIZE)
219 {
220 DBG1(DBG_TNC, "insufficient data for Request Functional Component Evidence");
221 *offset = 0;
222 return FAILED;
223 }
224 reader = bio_reader_create(this->value);
225
226 reader->read_uint8(reader, &flags);
227 if ((flags >> 4) & 1)
228 {
229 this->flags |= PTS_REQ_FUNC_COMP_FLAG_PCR;
230 }
231 if ((flags >> 5) & 1)
232 {
233 this->flags |= PTS_REQ_FUNC_COMP_FLAG_CURR;
234 }
235 if ((flags >> 6) & 1)
236 {
237 this->flags |= PTS_REQ_FUNC_COMP_FLAG_VER;
238 }
239 if ((flags >> 7) & 1)
240 {
241 this->flags |= PTS_REQ_FUNC_COMP_FLAG_TTC;
242 }
243
244 reader->read_uint24(reader, &this->depth);
245 reader->read_uint24(reader, &this->comp_vendor_id);
246 reader->read_uint8(reader, &fam_and_qualifier);
247
248 if (((fam_and_qualifier >> 6) & 1) )
249 {
250 this->family += 1;
251 }
252 if (((fam_and_qualifier >> 7) & 1) )
253 {
254 this->family += 2;
255 }
256
257 /* TODO: Generate an IF-M error attribute indicating */
258 /* TCG_PTS_INVALID_NAME_FAM */
259 //if(&this->comp_vendor_id==PEN_TCG && this->family != PTS_REQ_FUNCT_COMP_FAM_BIN_ENUM)
260 //{
261 // DBG1(DBG_TNC, "Functional Name Encoding Family is not set to 00");
262 //}
263
264 if (((fam_and_qualifier >> 5) & 1) )
265 {
266 this->qualifier.kernel = true;
267 }
268 if (((fam_and_qualifier >> 4) & 1) )
269 {
270 this->qualifier.sub_component = true;
271 }
272 this->qualifier.type = ( fam_and_qualifier & 0xF );
273 /* TODO: Check the type is defined in pts_attr_req_funct_comp_type_t */
274
275 reader->read_uint32(reader, &this->name);
276 /* TODO: Check the name is defined in pts_attr_req_funct_comp_name_bin_enum_t */
277
278 reader->destroy(reader);
279 return SUCCESS;
280 }
281
282 METHOD(pa_tnc_attr_t, destroy, void,
283 private_tcg_pts_attr_req_funct_comp_evid_t *this)
284 {
285 free(this->value.ptr);
286 free(this);
287 }
288
289 METHOD(tcg_pts_attr_req_funct_comp_evid_t, get_flags, pts_attr_req_funct_comp_evid_flag_t,
290 private_tcg_pts_attr_req_funct_comp_evid_t *this)
291 {
292 return this->flags;
293 }
294
295 METHOD(tcg_pts_attr_req_funct_comp_evid_t, set_flags, void,
296 private_tcg_pts_attr_req_funct_comp_evid_t *this, pts_attr_req_funct_comp_evid_flag_t flags)
297 {
298 this->flags = flags;
299 }
300
301 METHOD(tcg_pts_attr_req_funct_comp_evid_t, get_sub_component_depth, u_int32_t,
302 private_tcg_pts_attr_req_funct_comp_evid_t *this)
303 {
304 return this->depth;
305 }
306
307 METHOD(tcg_pts_attr_req_funct_comp_evid_t, get_comp_funct_name_vendor_id, u_int32_t,
308 private_tcg_pts_attr_req_funct_comp_evid_t *this)
309 {
310 return this->comp_vendor_id;
311 }
312
313 METHOD(tcg_pts_attr_req_funct_comp_evid_t, get_family, u_int8_t,
314 private_tcg_pts_attr_req_funct_comp_evid_t *this)
315 {
316 return this->family;
317 }
318
319 METHOD(tcg_pts_attr_req_funct_comp_evid_t, get_qualifier, tcg_pts_qualifier_t,
320 private_tcg_pts_attr_req_funct_comp_evid_t *this)
321 {
322 return this->qualifier;
323 }
324
325 METHOD(tcg_pts_attr_req_funct_comp_evid_t, set_qualifier, void,
326 private_tcg_pts_attr_req_funct_comp_evid_t *this,
327 tcg_pts_qualifier_t qualifier)
328 {
329 this->qualifier = qualifier;
330 }
331
332 METHOD(tcg_pts_attr_req_funct_comp_evid_t, get_comp_funct_name, pts_attr_req_funct_comp_name_bin_enum_t,
333 private_tcg_pts_attr_req_funct_comp_evid_t *this)
334 {
335 return this->name;
336 }
337
338 METHOD(tcg_pts_attr_req_funct_comp_evid_t, set_comp_funct_name, void,
339 private_tcg_pts_attr_req_funct_comp_evid_t *this, pts_attr_req_funct_comp_name_bin_enum_t name)
340 {
341 this->name = name;
342 }
343
344 /**
345 * Described in header.
346 */
347 pa_tnc_attr_t *tcg_pts_attr_req_funct_comp_evid_create(
348 pts_attr_req_funct_comp_evid_flag_t flags,
349 u_int32_t depth,
350 u_int32_t vendor_id,
351 tcg_pts_qualifier_t qualifier,
352 pts_attr_req_funct_comp_name_bin_enum_t name)
353 {
354 private_tcg_pts_attr_req_funct_comp_evid_t *this;
355
356 INIT(this,
357 .public = {
358 .pa_tnc_attribute = {
359 .get_vendor_id = _get_vendor_id,
360 .get_type = _get_type,
361 .get_value = _get_value,
362 .get_noskip_flag = _get_noskip_flag,
363 .set_noskip_flag = _set_noskip_flag,
364 .build = _build,
365 .process = _process,
366 .destroy = _destroy,
367 },
368 .get_flags= _get_flags,
369 .set_flags= _set_flags,
370 .get_sub_component_depth = _get_sub_component_depth,
371 .get_comp_funct_name_vendor_id = _get_comp_funct_name_vendor_id,
372 .get_family = _get_family,
373 .get_qualifier = _get_qualifier,
374 .set_qualifier = _set_qualifier,
375 .get_comp_funct_name = _get_comp_funct_name,
376 .set_comp_funct_name = _set_comp_funct_name,
377 },
378 .vendor_id = PEN_TCG,
379 .type = TCG_PTS_REQ_FUNCT_COMP_EVID,
380 .flags = flags,
381 .depth = depth,
382 .comp_vendor_id = vendor_id,
383 .family = PTS_REQ_FUNCT_COMP_FAM_BIN_ENUM,
384 .qualifier = qualifier,
385 .name = name,
386 );
387
388 return &this->public.pa_tnc_attribute;
389 }
390
391
392 /**
393 * Described in header.
394 */
395 pa_tnc_attr_t *tcg_pts_attr_req_funct_comp_evid_create_from_data(chunk_t data)
396 {
397 private_tcg_pts_attr_req_funct_comp_evid_t *this;
398
399 INIT(this,
400 .public = {
401 .pa_tnc_attribute = {
402 .get_vendor_id = _get_vendor_id,
403 .get_type = _get_type,
404 .get_value = _get_value,
405 .get_noskip_flag = _get_noskip_flag,
406 .set_noskip_flag = _set_noskip_flag,
407 .build = _build,
408 .process = _process,
409 .destroy = _destroy,
410 },
411 .get_flags= _get_flags,
412 .set_flags= _set_flags,
413 .get_sub_component_depth = _get_sub_component_depth,
414 .get_comp_funct_name_vendor_id = _get_comp_funct_name_vendor_id,
415 .get_family = _get_family,
416 .get_qualifier = _get_qualifier,
417 .set_qualifier = _set_qualifier,
418 .get_comp_funct_name = _get_comp_funct_name,
419 .set_comp_funct_name = _set_comp_funct_name,
420 },
421 .vendor_id = PEN_TCG,
422 .type = TCG_PTS_REQ_FUNCT_COMP_EVID,
423 .value = chunk_clone(data),
424 );
425
426 return &this->public.pa_tnc_attribute;
427 }