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