Implemented PTS attributes Request File Metadata, Unix-Style File Metadata
[strongswan.git] / src / libpts / tcg / tcg_pts_attr_req_file_meta.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_meta.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_meta_t private_tcg_pts_attr_req_file_meta_t;
24
25 /**
26 * Request File Metadata
27 * see section 3.17.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 * | Flags | Delimiter | Reserved |
33 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 * ~ Fully Qualified File Pathname (Variable Length) ~
35 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 */
37
38 #define PTS_REQ_FILE_META_SIZE 4
39 #define PTS_REQ_FILE_META_RESERVED 0x00
40 #define PTS_REQ_FILE_META_NO_FLAGS 0x00
41
42 #define DIRECTORY_CONTENTS_FLAG (1<<7)
43
44 /**
45 * Private data of an tcg_pts_attr_req_file_meta_t object.
46 */
47 struct private_tcg_pts_attr_req_file_meta_t {
48
49 /**
50 * Public members of tcg_pts_attr_req_file_meta_t
51 */
52 tcg_pts_attr_req_file_meta_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 * UTF8 Encoding of Delimiter Character
81 */
82 u_int8_t delimiter;
83
84 /**
85 * Fully Qualified File Pathname
86 */
87 char *pathname;
88
89 };
90
91 METHOD(pa_tnc_attr_t, get_vendor_id, pen_t,
92 private_tcg_pts_attr_req_file_meta_t *this)
93 {
94 return this->vendor_id;
95 }
96
97 METHOD(pa_tnc_attr_t, get_type, u_int32_t,
98 private_tcg_pts_attr_req_file_meta_t *this)
99 {
100 return this->type;
101 }
102
103 METHOD(pa_tnc_attr_t, get_value, chunk_t,
104 private_tcg_pts_attr_req_file_meta_t *this)
105 {
106 return this->value;
107 }
108
109 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
110 private_tcg_pts_attr_req_file_meta_t *this)
111 {
112 return this->noskip_flag;
113 }
114
115 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
116 private_tcg_pts_attr_req_file_meta_t *this, bool noskip)
117 {
118 this->noskip_flag = noskip;
119 }
120
121 METHOD(pa_tnc_attr_t, build, void,
122 private_tcg_pts_attr_req_file_meta_t *this)
123 {
124 u_int8_t flags = PTS_REQ_FILE_META_NO_FLAGS;
125 chunk_t pathname;
126 bio_writer_t *writer;
127
128 if (this->directory_flag)
129 {
130 flags |= DIRECTORY_CONTENTS_FLAG;
131 }
132 pathname = chunk_create(this->pathname, strlen(this->pathname));
133
134 writer = bio_writer_create(PTS_REQ_FILE_META_SIZE);
135 writer->write_uint8 (writer, flags);
136 writer->write_uint8 (writer, this->delimiter);
137 writer->write_uint16(writer, PTS_REQ_FILE_META_RESERVED);
138
139 writer->write_data (writer, pathname);
140 this->value = chunk_clone(writer->get_buf(writer));
141 writer->destroy(writer);
142 }
143
144 METHOD(pa_tnc_attr_t, process, status_t,
145 private_tcg_pts_attr_req_file_meta_t *this, u_int32_t *offset)
146 {
147 bio_reader_t *reader;
148 u_int8_t flags;
149 u_int16_t reserved;
150 chunk_t pathname;
151
152 if (this->value.len < PTS_REQ_FILE_META_SIZE)
153 {
154 DBG1(DBG_TNC, "insufficient data for Request File Metadata");
155 *offset = 0;
156 return FAILED;
157 }
158
159 reader = bio_reader_create(this->value);
160 reader->read_uint8 (reader, &flags);
161 reader->read_uint8 (reader, &this->delimiter);
162 reader->read_uint16(reader, &reserved);
163
164 reader->read_data (reader, reader->remaining(reader), &pathname);
165
166 this->directory_flag = (flags & DIRECTORY_CONTENTS_FLAG) !=
167 PTS_REQ_FILE_META_NO_FLAGS;
168
169 this->pathname = malloc(pathname.len + 1);
170 memcpy(this->pathname, pathname.ptr, pathname.len);
171 this->pathname[pathname.len] = '\0';
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_meta_t *this)
179 {
180 free(this->pathname);
181 free(this->value.ptr);
182 free(this);
183 }
184
185 METHOD(tcg_pts_attr_req_file_meta_t, get_directory_flag, bool,
186 private_tcg_pts_attr_req_file_meta_t *this)
187 {
188 return this->directory_flag;
189 }
190
191 METHOD(tcg_pts_attr_req_file_meta_t, get_delimiter, u_int32_t,
192 private_tcg_pts_attr_req_file_meta_t *this)
193 {
194 return this->delimiter;
195 }
196
197 METHOD(tcg_pts_attr_req_file_meta_t, get_pathname, char*,
198 private_tcg_pts_attr_req_file_meta_t *this)
199 {
200 return this->pathname;
201 }
202
203 /**
204 * Described in header.
205 */
206 pa_tnc_attr_t *tcg_pts_attr_req_file_meas_create(bool directory_flag,
207 u_int8_t delimiter,
208 char *pathname)
209 {
210 private_tcg_pts_attr_req_file_meta_t *this;
211
212 INIT(this,
213 .public = {
214 .pa_tnc_attribute = {
215 .get_vendor_id = _get_vendor_id,
216 .get_type = _get_type,
217 .get_value = _get_value,
218 .get_noskip_flag = _get_noskip_flag,
219 .set_noskip_flag = _set_noskip_flag,
220 .build = _build,
221 .process = _process,
222 .destroy = _destroy,
223 },
224 .get_directory_flag = _get_directory_flag,
225 .get_delimiter = _get_delimiter,
226 .get_pathname = _get_pathname,
227 },
228 .vendor_id = PEN_TCG,
229 .type = TCG_PTS_REQ_FILE_META,
230 .directory_flag = directory_flag,
231 .delimiter = delimiter,
232 .pathname = strdup(pathname),
233 );
234
235 return &this->public.pa_tnc_attribute;
236 }
237
238
239 /**
240 * Described in header.
241 */
242 pa_tnc_attr_t *tcg_pts_attr_req_file_meas_create_from_data(chunk_t data)
243 {
244 private_tcg_pts_attr_req_file_meta_t *this;
245
246 INIT(this,
247 .public = {
248 .pa_tnc_attribute = {
249 .get_vendor_id = _get_vendor_id,
250 .get_type = _get_type,
251 .get_value = _get_value,
252 .get_noskip_flag = _get_noskip_flag,
253 .set_noskip_flag = _set_noskip_flag,
254 .build = _build,
255 .process = _process,
256 .destroy = _destroy,
257 },
258 .get_directory_flag = _get_directory_flag,
259 .get_delimiter = _get_delimiter,
260 .get_pathname = _get_pathname,
261 },
262 .vendor_id = PEN_TCG,
263 .type = TCG_PTS_REQ_FILE_META,
264 .value = chunk_clone(data),
265 );
266
267 return &this->public.pa_tnc_attribute;
268 }