8028caafaf9e13186ac2dccf8873e3d83e7eb3df
[strongswan.git] / src / libimcv / tcg / tcg_pts_attr_req_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_req_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 <debug.h>
22
23 typedef struct private_tcg_pts_attr_req_file_meas_t private_tcg_pts_attr_req_file_meas_t;
24
25 /**
26 * Request File Measurement
27 * see section 3.19.1 of PTS Protocol: Binding to TNC IF-M Specification
28 *
29 * 1 2 3
30 * 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
31 *
32 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33 * | Flags | Reserved | Request ID |
34 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 * | Delimiter |
36 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 * ~ Fully Qualified File Path Name (Variable Length) ~
38 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 */
40
41 #define PTS_REQ_FILE_MEAS_SIZE 8
42 #define PTS_REQ_FILE_MEAS_RESERVED 0x00
43
44 /**
45 * Private data of an tcg_pts_attr_req_file_meas_t object.
46 */
47 struct private_tcg_pts_attr_req_file_meas_t {
48
49 /**
50 * Public members of tcg_pts_attr_req_file_meas_t
51 */
52 tcg_pts_attr_req_file_meas_t public;
53
54 /**
55 * Attribute vendor ID
56 */
57 pen_t vendor_id;
58
59 /**
60 * Attribute type
61 */
62 u_int32_t type;
63
64 /**
65 * Attribute value
66 */
67 chunk_t value;
68
69 /**
70 * Noskip flag
71 */
72 bool noskip_flag;
73
74 /**
75 * Directory Contents flag
76 */
77 bool directory_flag;
78
79 /**
80 * Request ID
81 */
82 u_int16_t request_id;
83
84 /**
85 * UTF8 Encoding of Delimiter Character
86 */
87 u_int32_t delimiter;
88
89 /**
90 * Fully Qualified File Path Name
91 */
92 chunk_t path;
93
94 };
95
96 METHOD(pa_tnc_attr_t, get_vendor_id, pen_t,
97 private_tcg_pts_attr_req_file_meas_t *this)
98 {
99 return this->vendor_id;
100 }
101
102 METHOD(pa_tnc_attr_t, get_type, u_int32_t,
103 private_tcg_pts_attr_req_file_meas_t *this)
104 {
105 return this->type;
106 }
107
108 METHOD(pa_tnc_attr_t, get_value, chunk_t,
109 private_tcg_pts_attr_req_file_meas_t *this)
110 {
111 return this->value;
112 }
113
114 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
115 private_tcg_pts_attr_req_file_meas_t *this)
116 {
117 return this->noskip_flag;
118 }
119
120 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
121 private_tcg_pts_attr_req_file_meas_t *this, bool noskip)
122 {
123 this->noskip_flag = noskip;
124 }
125
126 METHOD(pa_tnc_attr_t, build, void,
127 private_tcg_pts_attr_req_file_meas_t *this)
128 {
129 bio_writer_t *writer;
130 u_int8_t flags = 0;
131
132 writer = bio_writer_create(PTS_REQ_FILE_MEAS_SIZE);
133
134 if (this->directory_flag)
135 {
136 flags += 128;
137 }
138 writer->write_uint8(writer, flags);
139 writer->write_uint8(writer, PTS_REQ_FILE_MEAS_RESERVED);
140 writer->write_uint16(writer, this->request_id);
141 writer->write_uint32(writer, this->delimiter);
142 writer->write_data (writer, this->path);
143
144 this->value = chunk_clone(writer->get_buf(writer));
145 writer->destroy(writer);
146 }
147
148 METHOD(pa_tnc_attr_t, process, status_t,
149 private_tcg_pts_attr_req_file_meas_t *this, u_int32_t *offset)
150 {
151 bio_reader_t *reader;
152 u_int8_t flags;
153 u_int8_t reserved;
154 u_int32_t file_path_len;
155
156 char *temp;
157
158 if (this->value.len < PTS_REQ_FILE_MEAS_SIZE)
159 {
160 DBG1(DBG_TNC, "insufficient data for Request File Measurement");
161 *offset = 0;
162 return FAILED;
163 }
164 reader = bio_reader_create(this->value);
165
166 reader->read_uint8(reader, &flags);
167 if((flags >> 7) & 1) this->directory_flag = true;
168
169 reader->read_uint8(reader, &reserved);
170 reader->read_uint16(reader, &this->request_id);
171 reader->read_uint32(reader, &this->delimiter);
172 file_path_len = reader->remaining(reader);
173
174 reader->read_data(reader, file_path_len, &this->path);
175 this->path = chunk_clone(this->path);
176
177 DBG1(DBG_TNC, " ****** Request ID: %d File path len: %d Path: %s", this->request_id, file_path_len, this->path.ptr);
178 reader->destroy(reader);
179 return SUCCESS;
180 }
181
182 METHOD(pa_tnc_attr_t, destroy, void,
183 private_tcg_pts_attr_req_file_meas_t *this)
184 {
185 free(this->value.ptr);
186 free(this->path.ptr);
187 free(this);
188 }
189
190 METHOD(tcg_pts_attr_req_file_meas_t, get_directory_flag, bool,
191 private_tcg_pts_attr_req_file_meas_t *this)
192 {
193 return this->directory_flag;
194 }
195
196 METHOD(tcg_pts_attr_req_file_meas_t, set_directory_flag, void,
197 private_tcg_pts_attr_req_file_meas_t *this, bool directory_flag)
198 {
199 this->directory_flag = directory_flag;
200 }
201
202 METHOD(tcg_pts_attr_req_file_meas_t, get_request_id, u_int16_t,
203 private_tcg_pts_attr_req_file_meas_t *this)
204 {
205 return this->request_id;
206 }
207
208 METHOD(tcg_pts_attr_req_file_meas_t, set_request_id, void,
209 private_tcg_pts_attr_req_file_meas_t *this, u_int16_t request_id)
210 {
211 this->request_id = request_id;
212 }
213
214 METHOD(tcg_pts_attr_req_file_meas_t, get_delimiter, u_int32_t,
215 private_tcg_pts_attr_req_file_meas_t *this)
216 {
217 return this->delimiter;
218 }
219
220 METHOD(tcg_pts_attr_req_file_meas_t, set_delimiter, void,
221 private_tcg_pts_attr_req_file_meas_t *this, u_int32_t delimiter)
222 {
223 this->delimiter = delimiter;
224 }
225
226 METHOD(tcg_pts_attr_req_file_meas_t, get_file_path, chunk_t,
227 private_tcg_pts_attr_req_file_meas_t *this)
228 {
229 return this->path;
230 }
231
232 METHOD(tcg_pts_attr_req_file_meas_t, set_file_path, void,
233 private_tcg_pts_attr_req_file_meas_t *this, chunk_t path)
234 {
235 this->path = path;
236 }
237
238 /**
239 * Described in header.
240 */
241 pa_tnc_attr_t *tcg_pts_attr_req_file_meas_create(
242 bool directory_flag,
243 u_int16_t request_id,
244 u_int32_t delimiter,
245 chunk_t path)
246 {
247 private_tcg_pts_attr_req_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_directory_flag= _get_directory_flag,
262 .set_directory_flag= _set_directory_flag,
263 .get_request_id = _get_request_id,
264 .set_request_id = _set_request_id,
265 .get_delimiter = _get_delimiter,
266 .set_delimiter = _set_delimiter,
267 .get_file_path = _get_file_path,
268 .set_file_path = _set_file_path,
269 },
270 .vendor_id = PEN_TCG,
271 .type = TCG_PTS_REQ_FILE_MEAS,
272 .directory_flag = directory_flag,
273 .request_id = request_id,
274 .delimiter = delimiter,
275 .path = path,
276 );
277
278 return &this->public.pa_tnc_attribute;
279 }
280
281
282 /**
283 * Described in header.
284 */
285 pa_tnc_attr_t *tcg_pts_attr_req_file_meas_create_from_data(chunk_t data)
286 {
287 private_tcg_pts_attr_req_file_meas_t *this;
288
289 INIT(this,
290 .public = {
291 .pa_tnc_attribute = {
292 .get_vendor_id = _get_vendor_id,
293 .get_type = _get_type,
294 .get_value = _get_value,
295 .get_noskip_flag = _get_noskip_flag,
296 .set_noskip_flag = _set_noskip_flag,
297 .build = _build,
298 .process = _process,
299 .destroy = _destroy,
300 },
301 .get_directory_flag= _get_directory_flag,
302 .set_directory_flag= _set_directory_flag,
303 .get_request_id = _get_request_id,
304 .set_request_id = _set_request_id,
305 .get_delimiter = _get_delimiter,
306 .set_delimiter = _set_delimiter,
307 .get_file_path = _get_file_path,
308 .set_file_path = _set_file_path,
309 },
310 .vendor_id = PEN_TCG,
311 .type = TCG_PTS_REQ_FILE_MEAS,
312 .value = chunk_clone(data),
313 );
314
315 return &this->public.pa_tnc_attribute;
316 }