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