Implemented PTS attributes Request File Metadata, Unix-Style File Metadata
[strongswan.git] / src / libpts / 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 #include <debug.h>
23
24 typedef struct private_tcg_pts_attr_file_meas_t private_tcg_pts_attr_file_meas_t;
25
26 /**
27 * File Measurement
28 * see section 3.19.2 of PTS Protocol: Binding to TNC IF-M Specification
29 *
30 * 1 2 3
31 * 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
32 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33 * | Number of Files included |
34 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 * | Number of Files included |
36 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 * | Request ID | Measurement Length |
38 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 * | Measurement #1 (Variable Length) |
40 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 * | Filename Length | Filename (Variable Length) ~
42 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 * ~ Filename (Variable Length) ~
44 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
45 * | Measurement #2 (Variable Length) |
46 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47 * | Filename Length | Filename (Variable Length) ~
48 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49 * ~ Filename (Variable Length) ~
50 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 * ...........................
52 */
53
54 #define PTS_FILE_MEAS_SIZE 12
55
56 /**
57 * Private data of an tcg_pts_attr_file_meas_t object.
58 */
59 struct private_tcg_pts_attr_file_meas_t {
60
61 /**
62 * Public members of tcg_pts_attr_file_meas_t
63 */
64 tcg_pts_attr_file_meas_t public;
65
66 /**
67 * Attribute vendor ID
68 */
69 pen_t vendor_id;
70
71 /**
72 * Attribute type
73 */
74 u_int32_t type;
75
76 /**
77 * Attribute value
78 */
79 chunk_t value;
80
81 /**
82 * Noskip flag
83 */
84 bool noskip_flag;
85
86 /**
87 * PTS File Measurements
88 */
89 pts_file_meas_t *measurements;
90
91 };
92
93 METHOD(pa_tnc_attr_t, get_vendor_id, pen_t,
94 private_tcg_pts_attr_file_meas_t *this)
95 {
96 return this->vendor_id;
97 }
98
99 METHOD(pa_tnc_attr_t, get_type, u_int32_t,
100 private_tcg_pts_attr_file_meas_t *this)
101 {
102 return this->type;
103 }
104
105 METHOD(pa_tnc_attr_t, get_value, chunk_t,
106 private_tcg_pts_attr_file_meas_t *this)
107 {
108 return this->value;
109 }
110
111 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
112 private_tcg_pts_attr_file_meas_t *this)
113 {
114 return this->noskip_flag;
115 }
116
117 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
118 private_tcg_pts_attr_file_meas_t *this, bool noskip)
119 {
120 this->noskip_flag = noskip;
121 }
122
123 METHOD(pa_tnc_attr_t, build, void,
124 private_tcg_pts_attr_file_meas_t *this)
125 {
126 bio_writer_t *writer;
127 enumerator_t *enumerator;
128 u_int64_t number_of_files;
129 u_int16_t request_id;
130 char *filename;
131 chunk_t measurement;
132 bool first = TRUE;
133
134 number_of_files = this->measurements->get_file_count(this->measurements);
135 request_id = this->measurements->get_request_id(this->measurements);
136 writer = bio_writer_create(PTS_FILE_MEAS_SIZE);
137
138 /* Write the 64 bit integer as two 32 bit parts */
139 writer->write_uint32(writer, number_of_files >> 32);
140 writer->write_uint32(writer, number_of_files & 0xffffffff);
141 writer->write_uint16(writer, request_id);
142
143 enumerator = this->measurements->create_enumerator(this->measurements);
144 while (enumerator->enumerate(enumerator, &filename, &measurement))
145 {
146 if (first)
147 {
148 writer->write_uint16(writer, measurement.len);
149 first = FALSE;
150 }
151 writer->write_data (writer, measurement);
152 writer->write_uint16(writer, strlen(filename));
153 writer->write_data (writer, chunk_create(filename, strlen(filename)));
154 }
155 enumerator->destroy(enumerator);
156
157 if (first)
158 {
159 /* no attached measurements */
160 writer->write_uint16(writer, 0);
161 }
162
163 this->value = chunk_clone(writer->get_buf(writer));
164 writer->destroy(writer);
165 }
166
167 METHOD(pa_tnc_attr_t, process, status_t,
168 private_tcg_pts_attr_file_meas_t *this, u_int32_t *offset)
169 {
170 bio_reader_t *reader;
171 int count;
172 u_int32_t number_of_files;
173 u_int16_t request_id, meas_len, filename_len;
174 size_t len;
175 chunk_t measurement, filename;
176 char buf[BUF_LEN];
177 status_t status = FAILED;
178
179 if (this->value.len < PTS_FILE_MEAS_SIZE)
180 {
181 DBG1(DBG_TNC, "insufficient data for PTS file measurement header");
182 *offset = 0;
183 return FAILED;
184 }
185 reader = bio_reader_create(this->value);
186
187 reader->read_uint32(reader, &number_of_files);
188 count = (sizeof(count) > 4) ? number_of_files << 32 : 0;
189 reader->read_uint32(reader, &number_of_files);
190 count += number_of_files;
191 reader->read_uint16(reader, &request_id);
192 reader->read_uint16(reader, &meas_len);
193
194 this->measurements = pts_file_meas_create(request_id);
195
196 while (count--)
197 {
198 if (!reader->read_data(reader, meas_len, &measurement))
199 {
200 DBG1(DBG_TNC, "insufficient data for PTS file measurement");
201 goto end;
202 }
203 if (!reader->read_uint16(reader, &filename_len))
204 {
205 DBG1(DBG_TNC, "insufficient data for filename length");
206 goto end;
207 }
208 if (!reader->read_data(reader, filename_len, &filename))
209 {
210 DBG1(DBG_TNC, "insufficient data for filename");
211 goto end;
212 }
213
214 len = min(filename.len, BUF_LEN-1);
215 memcpy(buf, filename.ptr, len);
216 buf[len] = '\0';
217 this->measurements->add(this->measurements, buf, measurement);
218 }
219 status = SUCCESS;
220
221 end:
222 reader->destroy(reader);
223 return status;
224 }
225
226 METHOD(pa_tnc_attr_t, destroy, void,
227 private_tcg_pts_attr_file_meas_t *this)
228 {
229 this->measurements->destroy(this->measurements);
230 free(this->value.ptr);
231 free(this);
232 }
233
234 METHOD(tcg_pts_attr_file_meas_t, get_measurements, pts_file_meas_t*,
235 private_tcg_pts_attr_file_meas_t *this)
236 {
237 return this->measurements;
238 }
239
240 /**
241 * Described in header.
242 */
243 pa_tnc_attr_t *tcg_pts_attr_file_meas_create(pts_file_meas_t *measurements)
244 {
245 private_tcg_pts_attr_file_meas_t *this;
246
247 INIT(this,
248 .public = {
249 .pa_tnc_attribute = {
250 .get_vendor_id = _get_vendor_id,
251 .get_type = _get_type,
252 .get_value = _get_value,
253 .get_noskip_flag = _get_noskip_flag,
254 .set_noskip_flag = _set_noskip_flag,
255 .build = _build,
256 .process = _process,
257 .destroy = _destroy,
258 },
259 .get_measurements = _get_measurements,
260 },
261 .vendor_id = PEN_TCG,
262 .type = TCG_PTS_FILE_MEAS,
263 .measurements = measurements,
264 );
265
266 return &this->public.pa_tnc_attribute;
267 }
268
269
270 /**
271 * Described in header.
272 */
273 pa_tnc_attr_t *tcg_pts_attr_file_meas_create_from_data(chunk_t data)
274 {
275 private_tcg_pts_attr_file_meas_t *this;
276
277 INIT(this,
278 .public = {
279 .pa_tnc_attribute = {
280 .get_vendor_id = _get_vendor_id,
281 .get_type = _get_type,
282 .get_value = _get_value,
283 .get_noskip_flag = _get_noskip_flag,
284 .set_noskip_flag = _set_noskip_flag,
285 .build = _build,
286 .process = _process,
287 .destroy = _destroy,
288 },
289 .get_measurements = _get_measurements,
290 },
291 .vendor_id = PEN_TCG,
292 .type = TCG_PTS_FILE_MEAS,
293 .value = chunk_clone(data),
294 );
295
296 return &this->public.pa_tnc_attribute;
297 }