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