Deleted filter for measurement enumerator
[strongswan.git] / src / libimcv / tcg / tcg_pts_attr_file_meas.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_file_meas.h"
17
18 #include <pa_tnc/pa_tnc_msg.h>
19 #include <bio/bio_writer.h>
20 #include <bio/bio_reader.h>
21 #include <utils/linked_list.h>
22 /* For pow function */
23 #include <math.h>
24 #include <debug.h>
25
26 typedef struct private_tcg_pts_attr_file_meas_t private_tcg_pts_attr_file_meas_t;
27
28 /**
29 * File Measurement
30 * see section 3.19.2 of PTS Protocol: Binding to TNC IF-M Specification
31 *
32 * 1 2 3
33 * 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
34 *
35 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 * | Number of Files included |
37 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 * | Number of Files included |
39 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 * | Request ID | Measurement Length |
41 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 * | Measurement #1 (Variable Length) |
43 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 * | Filename Length | Filename (Variable Length) ~
45 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 * ~ Filename (Variable Length) ~
47 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 * | Measurement #2 (Variable Length) |
49 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 * | Filename Length | Filename (Variable Length) ~
51 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 * ~ Filename (Variable Length) ~
53 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 * ...........................
55 */
56
57 #define PTS_FILE_MEAS_SIZE 12
58
59 /**
60 * Private data of an tcg_pts_attr_file_meas_t object.
61 */
62 struct private_tcg_pts_attr_file_meas_t {
63
64 /**
65 * Public members of tcg_pts_attr_file_meas_t
66 */
67 tcg_pts_attr_file_meas_t public;
68
69 /**
70 * Attribute vendor ID
71 */
72 pen_t vendor_id;
73
74 /**
75 * Attribute type
76 */
77 u_int32_t type;
78
79 /**
80 * Attribute value
81 */
82 chunk_t value;
83
84 /**
85 * Noskip flag
86 */
87 bool noskip_flag;
88
89 /**
90 * Number of files included
91 */
92 u_int64_t number_of_files;
93
94 /**
95 * Request ID
96 */
97 u_int16_t request_id;
98
99 /**
100 * Measurement Length
101 */
102 u_int16_t meas_len;
103
104 /**
105 * List of File Measurement entries
106 */
107 linked_list_t *measurements;
108
109 };
110
111 METHOD(pa_tnc_attr_t, get_vendor_id, pen_t,
112 private_tcg_pts_attr_file_meas_t *this)
113 {
114 return this->vendor_id;
115 }
116
117 METHOD(pa_tnc_attr_t, get_type, u_int32_t,
118 private_tcg_pts_attr_file_meas_t *this)
119 {
120 return this->type;
121 }
122
123 METHOD(pa_tnc_attr_t, get_value, chunk_t,
124 private_tcg_pts_attr_file_meas_t *this)
125 {
126 return this->value;
127 }
128
129 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
130 private_tcg_pts_attr_file_meas_t *this)
131 {
132 return this->noskip_flag;
133 }
134
135 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
136 private_tcg_pts_attr_file_meas_t *this, bool noskip)
137 {
138 this->noskip_flag = noskip;
139 }
140
141 METHOD(pa_tnc_attr_t, build, void,
142 private_tcg_pts_attr_file_meas_t *this)
143 {
144 bio_writer_t *writer;
145 enumerator_t *enumerator;
146 file_meas_entry_t *entry;
147
148 writer = bio_writer_create(PTS_FILE_MEAS_SIZE);
149
150 /* Write the 64 bit integer as 2 parts, first 32 bit and second */
151 writer->write_uint32 (writer, (this->number_of_files >> 32));
152 writer->write_uint32 (writer, (this->number_of_files & (int)(pow(2,32) - 1)));
153 writer->write_uint16(writer, this->request_id);
154 writer->write_uint16(writer, this->meas_len);
155
156 enumerator = this->measurements->create_enumerator(this->measurements);
157 while (enumerator->enumerate(enumerator, &entry))
158 {
159 writer->write_data (writer, entry->measurement);
160 writer->write_uint16 (writer, entry->file_name_len);
161 writer->write_data(writer, entry->file_name);
162 }
163 enumerator->destroy(enumerator);
164
165 this->value = chunk_clone(writer->get_buf(writer));
166 writer->destroy(writer);
167 }
168
169 METHOD(pa_tnc_attr_t, process, status_t,
170 private_tcg_pts_attr_file_meas_t *this, u_int32_t *offset)
171 {
172 bio_reader_t *reader;
173 u_int32_t number_of_files;
174 u_int64_t number_of_files_64;
175 file_meas_entry_t *entry;
176
177 if (this->value.len < PTS_FILE_MEAS_SIZE)
178 {
179 DBG1(DBG_TNC, "insufficient data for File Measurement");
180 *offset = 0;
181 return FAILED;
182 }
183 reader = bio_reader_create(this->value);
184
185 reader->read_uint32(reader, &number_of_files);
186 number_of_files_64 = number_of_files;
187 this->number_of_files = (number_of_files_64 << 32);
188 reader->read_uint32(reader, &number_of_files);
189 this->number_of_files += number_of_files;
190
191 reader->read_uint16(reader, &this->request_id);
192 reader->read_uint16(reader, &this->meas_len);
193
194 while (reader->remaining(reader))
195 {
196 entry = malloc_thing(file_meas_entry_t);
197
198 reader->read_data (reader, this->meas_len, &entry->measurement);
199 entry->measurement = chunk_clone(entry->measurement);
200 reader->read_uint16 (reader, &entry->file_name_len);
201 reader->read_data(reader, entry->file_name_len, &entry->file_name);
202 entry->file_name = chunk_clone(entry->file_name);
203
204 this->measurements->insert_last(this->measurements, entry);
205 }
206
207 reader->destroy(reader);
208 return SUCCESS;
209 }
210
211 METHOD(pa_tnc_attr_t, destroy, void,
212 private_tcg_pts_attr_file_meas_t *this)
213 {
214 free(this->value.ptr);
215 this->measurements->destroy_function(this->measurements, free);
216 free(this);
217 }
218
219 METHOD(tcg_pts_attr_file_meas_t, get_number_of_files, u_int64_t,
220 private_tcg_pts_attr_file_meas_t *this)
221 {
222 return this->number_of_files;
223 }
224
225 METHOD(tcg_pts_attr_file_meas_t, set_number_of_files, void,
226 private_tcg_pts_attr_file_meas_t *this, u_int64_t number_of_files)
227 {
228 this->number_of_files = number_of_files;
229 }
230
231 METHOD(tcg_pts_attr_file_meas_t, get_request_id, u_int16_t,
232 private_tcg_pts_attr_file_meas_t *this)
233 {
234 return this->request_id;
235 }
236
237 METHOD(tcg_pts_attr_file_meas_t, set_request_id, void,
238 private_tcg_pts_attr_file_meas_t *this, u_int16_t request_id)
239 {
240 this->request_id = request_id;
241 }
242
243 METHOD(tcg_pts_attr_file_meas_t, get_meas_len, u_int16_t,
244 private_tcg_pts_attr_file_meas_t *this)
245 {
246 return this->meas_len;
247 }
248
249 METHOD(tcg_pts_attr_file_meas_t, set_meas_len, void,
250 private_tcg_pts_attr_file_meas_t *this, u_int16_t meas_len)
251 {
252 this->meas_len = meas_len;
253 }
254
255 METHOD(tcg_pts_attr_file_meas_t, add_file_meas, void,
256 private_tcg_pts_attr_file_meas_t *this, chunk_t measurement,
257 chunk_t file_name)
258 {
259 file_meas_entry_t *entry;
260
261 entry = malloc_thing(file_meas_entry_t);
262 entry->measurement = measurement;
263 entry->file_name_len = file_name.len;
264 entry->file_name = file_name;
265 this->measurements->insert_last(this->measurements, entry);
266 }
267
268 METHOD(tcg_pts_attr_file_meas_t, create_file_meas_enumerator, enumerator_t*,
269 private_tcg_pts_attr_file_meas_t *this)
270 {
271 return this->measurements->create_enumerator(this->measurements);
272 }
273
274 /**
275 * Described in header.
276 */
277 pa_tnc_attr_t *tcg_pts_attr_file_meas_create(
278 u_int64_t number_of_files,
279 u_int16_t request_id,
280 u_int16_t meas_len)
281 {
282 private_tcg_pts_attr_file_meas_t *this;
283
284 INIT(this,
285 .public = {
286 .pa_tnc_attribute = {
287 .get_vendor_id = _get_vendor_id,
288 .get_type = _get_type,
289 .get_value = _get_value,
290 .get_noskip_flag = _get_noskip_flag,
291 .set_noskip_flag = _set_noskip_flag,
292 .build = _build,
293 .process = _process,
294 .destroy = _destroy,
295 },
296 .get_number_of_files= _get_number_of_files,
297 .set_number_of_files= _set_number_of_files,
298 .get_request_id = _get_request_id,
299 .set_request_id = _set_request_id,
300 .get_meas_len = _get_meas_len,
301 .set_meas_len = _set_meas_len,
302 .add_file_meas = _add_file_meas,
303 .create_file_meas_enumerator = _create_file_meas_enumerator,
304 },
305 .vendor_id = PEN_TCG,
306 .type = TCG_PTS_FILE_MEAS,
307 .number_of_files = number_of_files,
308 .request_id = request_id,
309 .meas_len = meas_len,
310 .measurements = linked_list_create(),
311 );
312
313 return &this->public.pa_tnc_attribute;
314 }
315
316
317 /**
318 * Described in header.
319 */
320 pa_tnc_attr_t *tcg_pts_attr_file_meas_create_from_data(chunk_t data)
321 {
322 private_tcg_pts_attr_file_meas_t *this;
323
324 INIT(this,
325 .public = {
326 .pa_tnc_attribute = {
327 .get_vendor_id = _get_vendor_id,
328 .get_type = _get_type,
329 .get_value = _get_value,
330 .get_noskip_flag = _get_noskip_flag,
331 .set_noskip_flag = _set_noskip_flag,
332 .build = _build,
333 .process = _process,
334 .destroy = _destroy,
335 },
336 .get_number_of_files= _get_number_of_files,
337 .set_number_of_files= _set_number_of_files,
338 .get_request_id = _get_request_id,
339 .set_request_id = _set_request_id,
340 .get_meas_len = _get_meas_len,
341 .set_meas_len = _set_meas_len,
342 .add_file_meas = _add_file_meas,
343 .create_file_meas_enumerator = _create_file_meas_enumerator,
344 },
345 .vendor_id = PEN_TCG,
346 .type = TCG_PTS_FILE_MEAS,
347 .value = chunk_clone(data),
348 .measurements = linked_list_create(),
349 );
350
351 return &this->public.pa_tnc_attribute;
352 }