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