e579063014a32f876b3c97ead8b60813a27c06e7
[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 reader->read_data (reader, this->meas_len, &entry->measurement);
198 reader->read_uint16 (reader, &entry->file_name_len);
199 reader->read_data(reader, entry->file_name_len, &entry->file_name);
200 entry->file_name = chunk_clone(entry->file_name);
201 this->measurements->insert_last(this->measurements, entry);
202 }
203
204 reader->destroy(reader);
205 return SUCCESS;
206 }
207
208 METHOD(pa_tnc_attr_t, destroy, void,
209 private_tcg_pts_attr_file_meas_t *this)
210 {
211 free(this->value.ptr);
212 this->measurements->destroy_function(this->measurements, free);
213 free(this);
214 }
215
216 METHOD(tcg_pts_attr_file_meas_t, get_number_of_files, u_int64_t,
217 private_tcg_pts_attr_file_meas_t *this)
218 {
219 return this->number_of_files;
220 }
221
222 METHOD(tcg_pts_attr_file_meas_t, set_number_of_files, void,
223 private_tcg_pts_attr_file_meas_t *this, u_int64_t number_of_files)
224 {
225 this->number_of_files = number_of_files;
226 }
227
228 METHOD(tcg_pts_attr_file_meas_t, get_request_id, u_int16_t,
229 private_tcg_pts_attr_file_meas_t *this)
230 {
231 return this->request_id;
232 }
233
234 METHOD(tcg_pts_attr_file_meas_t, set_request_id, void,
235 private_tcg_pts_attr_file_meas_t *this, u_int16_t request_id)
236 {
237 this->request_id = request_id;
238 }
239
240 METHOD(tcg_pts_attr_file_meas_t, get_meas_len, u_int16_t,
241 private_tcg_pts_attr_file_meas_t *this)
242 {
243 return this->meas_len;
244 }
245
246 METHOD(tcg_pts_attr_file_meas_t, set_meas_len, void,
247 private_tcg_pts_attr_file_meas_t *this, u_int16_t meas_len)
248 {
249 this->meas_len = meas_len;
250 }
251
252 METHOD(tcg_pts_attr_file_meas_t, add_file_meas, void,
253 private_tcg_pts_attr_file_meas_t *this, chunk_t measurement,
254 chunk_t file_name)
255 {
256 file_meas_entry_t *entry;
257
258 entry = malloc_thing(file_meas_entry_t);
259 entry->measurement = measurement;
260 entry->file_name_len = file_name.len;
261 entry->file_name = file_name;
262 this->measurements->insert_last(this->measurements, entry);
263 }
264
265 /**
266 * Enumerate file measurement entries
267 */
268 static bool measurement_filter(void *null, file_meas_entry_t **entry, chunk_t *measurement,
269 void *i2, u_int16_t *file_name_len,
270 void *i3, chunk_t *file_name)
271 {
272 *measurement = (*entry)->measurement;
273 *file_name_len = (*entry)->file_name_len;
274 *file_name = (*entry)->file_name;
275 return TRUE;
276 }
277
278 METHOD(tcg_pts_attr_file_meas_t, create_file_meas_enumerator, enumerator_t*,
279 private_tcg_pts_attr_file_meas_t *this)
280 {
281 return enumerator_create_filter(this->measurements->create_enumerator(this->measurements),
282 (void*)measurement_filter, NULL, NULL);
283 }
284
285 /**
286 * Described in header.
287 */
288 pa_tnc_attr_t *tcg_pts_attr_file_meas_create(
289 u_int64_t number_of_files,
290 u_int16_t request_id,
291 u_int16_t meas_len)
292 {
293 private_tcg_pts_attr_file_meas_t *this;
294
295 INIT(this,
296 .public = {
297 .pa_tnc_attribute = {
298 .get_vendor_id = _get_vendor_id,
299 .get_type = _get_type,
300 .get_value = _get_value,
301 .get_noskip_flag = _get_noskip_flag,
302 .set_noskip_flag = _set_noskip_flag,
303 .build = _build,
304 .process = _process,
305 .destroy = _destroy,
306 },
307 .get_number_of_files= _get_number_of_files,
308 .set_number_of_files= _set_number_of_files,
309 .get_request_id = _get_request_id,
310 .set_request_id = _set_request_id,
311 .get_meas_len = _get_meas_len,
312 .set_meas_len = _set_meas_len,
313 .add_file_meas = _add_file_meas,
314 .create_file_meas_enumerator = _create_file_meas_enumerator,
315 },
316 .vendor_id = PEN_TCG,
317 .type = TCG_PTS_FILE_MEAS,
318 .number_of_files = number_of_files,
319 .request_id = request_id,
320 .meas_len = meas_len,
321 .measurements = linked_list_create(),
322 );
323
324 return &this->public.pa_tnc_attribute;
325 }
326
327
328 /**
329 * Described in header.
330 */
331 pa_tnc_attr_t *tcg_pts_attr_file_meas_create_from_data(chunk_t data)
332 {
333 private_tcg_pts_attr_file_meas_t *this;
334
335 INIT(this,
336 .public = {
337 .pa_tnc_attribute = {
338 .get_vendor_id = _get_vendor_id,
339 .get_type = _get_type,
340 .get_value = _get_value,
341 .get_noskip_flag = _get_noskip_flag,
342 .set_noskip_flag = _set_noskip_flag,
343 .build = _build,
344 .process = _process,
345 .destroy = _destroy,
346 },
347 .get_number_of_files= _get_number_of_files,
348 .set_number_of_files= _set_number_of_files,
349 .get_request_id = _get_request_id,
350 .set_request_id = _set_request_id,
351 .get_meas_len = _get_meas_len,
352 .set_meas_len = _set_meas_len,
353 .add_file_meas = _add_file_meas,
354 .create_file_meas_enumerator = _create_file_meas_enumerator,
355 },
356 .vendor_id = PEN_TCG,
357 .type = TCG_PTS_FILE_MEAS,
358 .value = chunk_clone(data),
359 .measurements = linked_list_create(),
360 );
361
362 return &this->public.pa_tnc_attribute;
363 }