3269bceede67afc650cc7d510a1e74d0b973f25d
[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
137 writer = bio_writer_create(PTS_FILE_MEAS_SIZE);
138 writer->write_uint64(writer, number_of_files);
139 writer->write_uint16(writer, request_id);
140
141 enumerator = this->measurements->create_enumerator(this->measurements);
142 while (enumerator->enumerate(enumerator, &filename, &measurement))
143 {
144 if (first)
145 {
146 writer->write_uint16(writer, measurement.len);
147 first = FALSE;
148 }
149 writer->write_data (writer, measurement);
150 writer->write_uint16(writer, strlen(filename));
151 writer->write_data (writer, chunk_create(filename, strlen(filename)));
152 }
153 enumerator->destroy(enumerator);
154
155 if (first)
156 {
157 /* no attached measurements */
158 writer->write_uint16(writer, 0);
159 }
160
161 this->value = chunk_clone(writer->get_buf(writer));
162 writer->destroy(writer);
163 }
164
165 METHOD(pa_tnc_attr_t, process, status_t,
166 private_tcg_pts_attr_file_meas_t *this, u_int32_t *offset)
167 {
168 bio_reader_t *reader;
169 u_int64_t number_of_files;
170 u_int16_t request_id, meas_len, filename_len;
171 size_t len;
172 chunk_t measurement, filename;
173 char buf[BUF_LEN];
174 status_t status = FAILED;
175
176 if (this->value.len < PTS_FILE_MEAS_SIZE)
177 {
178 DBG1(DBG_TNC, "insufficient data for PTS file measurement header");
179 *offset = 0;
180 return FAILED;
181 }
182
183 reader = bio_reader_create(this->value);
184 reader->read_uint64(reader, &number_of_files);
185 reader->read_uint16(reader, &request_id);
186 reader->read_uint16(reader, &meas_len);
187
188 this->measurements = pts_file_meas_create(request_id);
189
190 while (number_of_files--)
191 {
192 if (!reader->read_data(reader, meas_len, &measurement))
193 {
194 DBG1(DBG_TNC, "insufficient data for PTS file measurement");
195 goto end;
196 }
197 if (!reader->read_uint16(reader, &filename_len))
198 {
199 DBG1(DBG_TNC, "insufficient data for filename length");
200 goto end;
201 }
202 if (!reader->read_data(reader, filename_len, &filename))
203 {
204 DBG1(DBG_TNC, "insufficient data for filename");
205 goto end;
206 }
207
208 len = min(filename.len, BUF_LEN-1);
209 memcpy(buf, filename.ptr, len);
210 buf[len] = '\0';
211 this->measurements->add(this->measurements, buf, measurement);
212 }
213 status = SUCCESS;
214
215 end:
216 reader->destroy(reader);
217 return status;
218 }
219
220 METHOD(pa_tnc_attr_t, destroy, void,
221 private_tcg_pts_attr_file_meas_t *this)
222 {
223 this->measurements->destroy(this->measurements);
224 free(this->value.ptr);
225 free(this);
226 }
227
228 METHOD(tcg_pts_attr_file_meas_t, get_measurements, pts_file_meas_t*,
229 private_tcg_pts_attr_file_meas_t *this)
230 {
231 return this->measurements;
232 }
233
234 /**
235 * Described in header.
236 */
237 pa_tnc_attr_t *tcg_pts_attr_file_meas_create(pts_file_meas_t *measurements)
238 {
239 private_tcg_pts_attr_file_meas_t *this;
240
241 INIT(this,
242 .public = {
243 .pa_tnc_attribute = {
244 .get_vendor_id = _get_vendor_id,
245 .get_type = _get_type,
246 .get_value = _get_value,
247 .get_noskip_flag = _get_noskip_flag,
248 .set_noskip_flag = _set_noskip_flag,
249 .build = _build,
250 .process = _process,
251 .destroy = _destroy,
252 },
253 .get_measurements = _get_measurements,
254 },
255 .vendor_id = PEN_TCG,
256 .type = TCG_PTS_FILE_MEAS,
257 .measurements = measurements,
258 );
259
260 return &this->public.pa_tnc_attribute;
261 }
262
263
264 /**
265 * Described in header.
266 */
267 pa_tnc_attr_t *tcg_pts_attr_file_meas_create_from_data(chunk_t data)
268 {
269 private_tcg_pts_attr_file_meas_t *this;
270
271 INIT(this,
272 .public = {
273 .pa_tnc_attribute = {
274 .get_vendor_id = _get_vendor_id,
275 .get_type = _get_type,
276 .get_value = _get_value,
277 .get_noskip_flag = _get_noskip_flag,
278 .set_noskip_flag = _set_noskip_flag,
279 .build = _build,
280 .process = _process,
281 .destroy = _destroy,
282 },
283 .get_measurements = _get_measurements,
284 },
285 .vendor_id = PEN_TCG,
286 .type = TCG_PTS_FILE_MEAS,
287 .value = chunk_clone(data),
288 );
289
290 return &this->public.pa_tnc_attribute;
291 }