5bd17c3afe5cf60331f12ac40c19eb73a61cc46a
[strongswan.git] / src / libimcv / ietf / ietf_attr_string_version.c
1 /*
2 * Copyright (C) 2012-2014 Andreas Steffen
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 "ietf_attr_string_version.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/debug.h>
22
23 typedef struct private_ietf_attr_string_version_t private_ietf_attr_string_version_t;
24
25 /**
26 * PA-TNC String Version type (see section 4.2.4 of RFC 5792)
27 *
28 * 1 2 3
29 * 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
30 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
31 * | Version Len | Product Version Number (Variable Length) |
32 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33 * | Build Num Len | Internal Build Number (Variable Length) |
34 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 * | Config. Len | Configuration Version Number (Variable Length)|
36 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 */
38
39 #define STRING_VERSION_MIN_SIZE 3
40
41 /**
42 * Private data of an ietf_attr_string_version_t object.
43 */
44 struct private_ietf_attr_string_version_t {
45
46 /**
47 * Public members of ietf_attr_string_version_t
48 */
49 ietf_attr_string_version_t public;
50
51 /**
52 * Vendor-specific attribute type
53 */
54 pen_type_t type;
55
56 /**
57 * Length of attribute value
58 */
59 size_t length;
60
61 /**
62 * Attribute value or segment
63 */
64 chunk_t value;
65
66 /**
67 * Noskip flag
68 */
69 bool noskip_flag;
70
71 /**
72 * Product Version Number
73 */
74 chunk_t version;
75
76 /**
77 * Internal Build Number
78 */
79 chunk_t build;
80
81 /**
82 * Configuration Version Number
83 */
84 chunk_t config;
85
86 /**
87 * Reference count
88 */
89 refcount_t ref;
90 };
91
92 METHOD(pa_tnc_attr_t, get_type, pen_type_t,
93 private_ietf_attr_string_version_t *this)
94 {
95 return this->type;
96 }
97
98 METHOD(pa_tnc_attr_t, get_value, chunk_t,
99 private_ietf_attr_string_version_t *this)
100 {
101 return this->value;
102 }
103
104 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
105 private_ietf_attr_string_version_t *this)
106 {
107 return this->noskip_flag;
108 }
109
110 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
111 private_ietf_attr_string_version_t *this, bool noskip)
112 {
113 this->noskip_flag = noskip;
114 }
115
116 METHOD(pa_tnc_attr_t, build, void,
117 private_ietf_attr_string_version_t *this)
118 {
119 bio_writer_t *writer;
120
121 if (this->value.ptr)
122 {
123 return;
124 }
125
126 writer = bio_writer_create(STRING_VERSION_MIN_SIZE);
127 writer->write_data8(writer, this->version);
128 writer->write_data8(writer, this->build);
129 writer->write_data8(writer, this->config);
130
131 this->value = writer->extract_buf(writer);
132 this->length = this->value.len;
133 writer->destroy(writer);
134 }
135
136 METHOD(pa_tnc_attr_t, process, status_t,
137 private_ietf_attr_string_version_t *this, u_int32_t *offset)
138 {
139 bio_reader_t *reader;
140 status_t status = FAILED;
141 chunk_t version, build, config;
142 u_char *pos;
143
144 *offset = 0;
145
146 if (this->value.len < this->length)
147 {
148 return NEED_MORE;
149 }
150 if (this->value.len < STRING_VERSION_MIN_SIZE)
151 {
152 DBG1(DBG_TNC, "insufficient data for IETF string version");
153 return FAILED;
154 }
155 reader = bio_reader_create(this->value);
156
157 if (!reader->read_data8(reader, &version))
158 {
159 DBG1(DBG_TNC, "insufficient data for IETF product version number");
160 goto end;
161
162 }
163 pos = memchr(version.ptr, '\0', version.len);
164 if (pos)
165 {
166 DBG1(DBG_TNC, "nul termination in IETF product version number");
167 *offset += 1 + (pos - version.ptr);
168 goto end;
169 }
170 *offset += 1 + version.len;
171
172 if (!reader->read_data8(reader, &build))
173 {
174 DBG1(DBG_TNC, "insufficient data for IETF internal build number");
175 goto end;
176
177 }
178 pos = memchr(build.ptr, '\0', build.len);
179 if (pos)
180 {
181 DBG1(DBG_TNC, "nul termination in IETF internal build number");
182 *offset += 1 + (pos - build.ptr);
183 goto end;
184 }
185 *offset += 1 + build.len;
186
187 if (!reader->read_data8(reader, &config))
188 {
189 DBG1(DBG_TNC, "insufficient data for IETF configuration version number");
190 goto end;
191
192 }
193 pos = memchr(config.ptr, '\0', config.len);
194 if (pos)
195 {
196 DBG1(DBG_TNC, "nul termination in IETF configuration version number");
197 *offset += 1 + (pos - config.ptr);
198 goto end;
199 }
200
201 this->version = chunk_clone(version);
202 this->build = chunk_clone(build);
203 this->config = chunk_clone(config);
204 status = SUCCESS;
205
206 end:
207 reader->destroy(reader);
208 return status;
209 }
210
211 METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*,
212 private_ietf_attr_string_version_t *this)
213 {
214 ref_get(&this->ref);
215 return &this->public.pa_tnc_attribute;
216 }
217
218 METHOD(pa_tnc_attr_t, destroy, void,
219 private_ietf_attr_string_version_t *this)
220 {
221 if (ref_put(&this->ref))
222 {
223 free(this->version.ptr);
224 free(this->build.ptr);
225 free(this->config.ptr);
226 free(this->value.ptr);
227 free(this);
228 }
229 }
230
231 METHOD(ietf_attr_string_version_t, get_version, chunk_t,
232 private_ietf_attr_string_version_t *this, chunk_t *build, chunk_t *config)
233 {
234 if (build)
235 {
236 *build = this->build;
237 }
238 if (config)
239 {
240 *config = this->config;
241 }
242 return this->version;
243 }
244
245 /**
246 * Described in header.
247 */
248 pa_tnc_attr_t *ietf_attr_string_version_create(chunk_t version, chunk_t build,
249 chunk_t config)
250 {
251 private_ietf_attr_string_version_t *this;
252
253 /* limit version numbers to 255 octets */
254 version.len = min(255, version.len);
255 build.len = min(255, build.len);
256 config.len = min(255, config.len);
257
258 INIT(this,
259 .public = {
260 .pa_tnc_attribute = {
261 .get_type = _get_type,
262 .get_value = _get_value,
263 .get_noskip_flag = _get_noskip_flag,
264 .set_noskip_flag = _set_noskip_flag,
265 .build = _build,
266 .process = _process,
267 .get_ref = _get_ref,
268 .destroy = _destroy,
269 },
270 .get_version = _get_version,
271 },
272 .type = { PEN_IETF, IETF_ATTR_STRING_VERSION },
273 .version = chunk_clone(version),
274 .build = chunk_clone(build),
275 .config = chunk_clone(config),
276 .ref = 1,
277 );
278
279 return &this->public.pa_tnc_attribute;
280 }
281
282 /**
283 * Described in header.
284 */
285 pa_tnc_attr_t *ietf_attr_string_version_create_from_data(size_t length,
286 chunk_t data)
287 {
288 private_ietf_attr_string_version_t *this;
289
290 INIT(this,
291 .public = {
292 .pa_tnc_attribute = {
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 .get_ref = _get_ref,
300 .destroy = _destroy,
301 },
302 .get_version = _get_version,
303 },
304 .type = { PEN_IETF, IETF_ATTR_STRING_VERSION },
305 .length = length,
306 .value = chunk_clone(data),
307 .ref = 1,
308 );
309
310 return &this->public.pa_tnc_attribute;
311 }
312