shortened some type names and enforced encoding rules
[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 if (this->value.len < PTS_REQ_FILE_MEAS_SIZE)
157 {
158 DBG1(DBG_TNC, "insufficient data for Request File Measurement");
159 *offset = 0;
160 return FAILED;
161 }
162 reader = bio_reader_create(this->value);
163
164 reader->read_uint8(reader, &flags);
165 if((flags >> 7) & 1) this->directory_flag = true;
166
167 reader->read_uint8(reader, &reserved);
168 reader->read_uint16(reader, &this->request_id);
169 reader->read_uint32(reader, &this->delimiter);
170 file_path_len = reader->remaining(reader);
171 reader->read_data(reader, file_path_len, &this->path);
172
173 reader->destroy(reader);
174 return SUCCESS;
175 }
176
177 METHOD(pa_tnc_attr_t, destroy, void,
178 private_tcg_pts_attr_req_file_meas_t *this)
179 {
180 free(this->value.ptr);
181 free(this->path.ptr);
182 free(this);
183 }
184
185 METHOD(tcg_pts_attr_req_file_meas_t, get_directory_flag, bool,
186 private_tcg_pts_attr_req_file_meas_t *this)
187 {
188 return this->directory_flag;
189 }
190
191 METHOD(tcg_pts_attr_req_file_meas_t, set_directory_flag, void,
192 private_tcg_pts_attr_req_file_meas_t *this, bool directory_flag)
193 {
194 this->directory_flag = directory_flag;
195 }
196
197 METHOD(tcg_pts_attr_req_file_meas_t, get_request_id, u_int16_t,
198 private_tcg_pts_attr_req_file_meas_t *this)
199 {
200 return this->request_id;
201 }
202
203 METHOD(tcg_pts_attr_req_file_meas_t, set_request_id, void,
204 private_tcg_pts_attr_req_file_meas_t *this, u_int16_t request_id)
205 {
206 this->request_id = request_id;
207 }
208
209 METHOD(tcg_pts_attr_req_file_meas_t, get_delimiter, u_int32_t,
210 private_tcg_pts_attr_req_file_meas_t *this)
211 {
212 return this->delimiter;
213 }
214
215 METHOD(tcg_pts_attr_req_file_meas_t, set_delimiter, void,
216 private_tcg_pts_attr_req_file_meas_t *this, u_int32_t delimiter)
217 {
218 this->delimiter = delimiter;
219 }
220
221 METHOD(tcg_pts_attr_req_file_meas_t, get_file_path, chunk_t,
222 private_tcg_pts_attr_req_file_meas_t *this)
223 {
224 return this->path;
225 }
226
227 METHOD(tcg_pts_attr_req_file_meas_t, set_file_path, void,
228 private_tcg_pts_attr_req_file_meas_t *this, chunk_t path)
229 {
230 this->path = path;
231 }
232
233 /**
234 * Described in header.
235 */
236 pa_tnc_attr_t *tcg_pts_attr_req_file_meas_create(
237 bool directory_flag,
238 u_int16_t request_id,
239 u_int32_t delimiter,
240 chunk_t path)
241 {
242 private_tcg_pts_attr_req_file_meas_t *this;
243
244 INIT(this,
245 .public = {
246 .pa_tnc_attribute = {
247 .get_vendor_id = _get_vendor_id,
248 .get_type = _get_type,
249 .get_value = _get_value,
250 .get_noskip_flag = _get_noskip_flag,
251 .set_noskip_flag = _set_noskip_flag,
252 .build = _build,
253 .process = _process,
254 .destroy = _destroy,
255 },
256 .get_directory_flag= _get_directory_flag,
257 .set_directory_flag= _set_directory_flag,
258 .get_request_id = _get_request_id,
259 .set_request_id = _set_request_id,
260 .get_delimiter = _get_delimiter,
261 .set_delimiter = _set_delimiter,
262 .get_file_path = _get_file_path,
263 .set_file_path = _set_file_path,
264 },
265 .vendor_id = PEN_TCG,
266 .type = TCG_PTS_REQ_FILE_MEAS,
267 .directory_flag = directory_flag,
268 .request_id = request_id,
269 .delimiter = delimiter,
270 .path = path,
271 );
272
273 return &this->public.pa_tnc_attribute;
274 }
275
276
277 /**
278 * Described in header.
279 */
280 pa_tnc_attr_t *tcg_pts_attr_req_file_meas_create_from_data(chunk_t data)
281 {
282 private_tcg_pts_attr_req_file_meas_t *this;
283
284 INIT(this,
285 .public = {
286 .pa_tnc_attribute = {
287 .get_vendor_id = _get_vendor_id,
288 .get_type = _get_type,
289 .get_value = _get_value,
290 .get_noskip_flag = _get_noskip_flag,
291 .set_noskip_flag = _set_noskip_flag,
292 .build = _build,
293 .process = _process,
294 .destroy = _destroy,
295 },
296 .get_directory_flag= _get_directory_flag,
297 .set_directory_flag= _set_directory_flag,
298 .get_request_id = _get_request_id,
299 .set_request_id = _set_request_id,
300 .get_delimiter = _get_delimiter,
301 .set_delimiter = _set_delimiter,
302 .get_file_path = _get_file_path,
303 .set_file_path = _set_file_path,
304 },
305 .vendor_id = PEN_TCG,
306 .type = TCG_PTS_REQ_FILE_MEAS,
307 .value = chunk_clone(data),
308 );
309
310 return &this->public.pa_tnc_attribute;
311 }