Added missing noskip_flag setter/getter to some pa_tnc_attr_t constructors
[strongswan.git] / src / libimcv / ietf / ietf_attr_remediation_instr.c
1 /*
2 * Copyright (C) 2012 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_remediation_instr.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_remediation_instr_t private_ietf_attr_remediation_instr_t;
24
25 /**
26 * PA-TNC Remediation Instructions type (see section 4.2.10 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 * | Reserved | Remediation Parameters Vendor ID |
32 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33 * | Remediation Parameters Type |
34 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 * | Remediation Parameters (Variable Length) |
36 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 */
38
39 #define REMEDIATION_INSTR_MIN_SIZE 8
40 #define REMEDIATION_INSTR_RESERVED 0x00
41
42 /**
43 * IETF Remediation Parameters URI type (see section 4.2.10.1 of RFC 5792)
44 *
45 * 1 2 3
46 * 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
47 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 * | Remediation URI (Variable Length) |
49 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
50 */
51
52 /**
53 * IETF Remediation Parameters String type (see section 4.2.10.2 of RFC 5792)
54 *
55 * 1 2 3
56 * 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
57 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58 * | Remediation String Length |
59 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60 * | Remediation String (Variable Length) |
61 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
62 * | Lang Code Len | Remediation String Lang Code (Variable Len) |
63 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64 */
65
66 /**
67 * Private data of an ietf_attr_remediation_instr_t object.
68 */
69 struct private_ietf_attr_remediation_instr_t {
70
71 /**
72 * Public members of ietf_attr_remediation_instr_t
73 */
74 ietf_attr_remediation_instr_t public;
75
76 /**
77 * Vendor-specific attribute type
78 */
79 pen_type_t type;
80
81 /**
82 * Attribute value
83 */
84 chunk_t value;
85
86 /**
87 * Noskip flag
88 */
89 bool noskip_flag;
90
91 /**
92 * Remediation Parameters Type
93 */
94 pen_type_t parameters_type;
95
96 /**
97 * Remediation Parameters
98 */
99 chunk_t parameters;
100
101 /**
102 * Remediation String
103 */
104 chunk_t string;
105
106 /**
107 * Remediation Language Code
108 */
109 chunk_t lang_code;
110
111 /**
112 * Reference count
113 */
114 refcount_t ref;
115 };
116
117 METHOD(pa_tnc_attr_t, get_type, pen_type_t,
118 private_ietf_attr_remediation_instr_t *this)
119 {
120 return this->type;
121 }
122
123 METHOD(pa_tnc_attr_t, get_value, chunk_t,
124 private_ietf_attr_remediation_instr_t *this)
125 {
126 return this->value;
127 }
128
129 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
130 private_ietf_attr_remediation_instr_t *this)
131 {
132 return this->noskip_flag;
133 }
134
135 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
136 private_ietf_attr_remediation_instr_t *this, bool noskip)
137 {
138 this->noskip_flag = noskip;
139 }
140
141 METHOD(pa_tnc_attr_t, build, void,
142 private_ietf_attr_remediation_instr_t *this)
143 {
144 bio_writer_t *writer;
145
146 if (this->value.ptr)
147 {
148 return;
149 }
150
151 writer = bio_writer_create(REMEDIATION_INSTR_MIN_SIZE);
152 writer->write_uint8 (writer, REMEDIATION_INSTR_RESERVED);
153 writer->write_uint24(writer, this->parameters_type.vendor_id);
154 writer->write_uint32(writer, this->parameters_type.type);
155 writer->write_data (writer, this->parameters);
156
157 this->value = chunk_clone(writer->get_buf(writer));
158 writer->destroy(writer);
159 }
160
161 METHOD(pa_tnc_attr_t, process, status_t,
162 private_ietf_attr_remediation_instr_t *this, u_int32_t *offset)
163 {
164 bio_reader_t *reader;
165 u_int8_t reserved;
166 status_t status = SUCCESS;
167 u_char *pos;
168
169 *offset = 0;
170
171 if (this->value.len < REMEDIATION_INSTR_MIN_SIZE)
172 {
173 DBG1(DBG_TNC, "insufficient data for IETF remediation instructions");
174 return FAILED;
175 }
176 reader = bio_reader_create(this->value);
177 reader->read_uint8 (reader, &reserved);
178 reader->read_uint24(reader, &this->parameters_type.vendor_id);
179 reader->read_uint32(reader, &this->parameters_type.type);
180 reader->read_data (reader, reader->remaining(reader), &this->parameters);
181
182 this->parameters = chunk_clone(this->parameters);
183 reader->destroy(reader);
184
185 if (this->parameters_type.vendor_id == PEN_IETF &&
186 this->parameters_type.type == IETF_REMEDIATION_PARAMETERS_STRING)
187 {
188 reader = bio_reader_create(this->parameters);
189 status = FAILED;
190 *offset = 8;
191
192 if (!reader->read_data32(reader, &this->string))
193 {
194 DBG1(DBG_TNC, "insufficient data for IETF remediation string");
195 goto end;
196 }
197 pos = memchr(this->string.ptr, '\0', this->string.len);
198 if (pos)
199 {
200 DBG1(DBG_TNC, "nul termination in IETF remediation string");
201 *offset += 1 + (pos - this->string.ptr);
202 goto end;
203 }
204 *offset += 4 + this->string.len;
205
206 if (!reader->read_data8(reader, &this->lang_code))
207 {
208 DBG1(DBG_TNC, "insufficient data for IETF remediation lang code");
209 goto end;
210 }
211 status = SUCCESS;
212
213 end:
214 reader->destroy(reader);
215 }
216 return status;
217 }
218
219 METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*,
220 private_ietf_attr_remediation_instr_t *this)
221 {
222 ref_get(&this->ref);
223 return &this->public.pa_tnc_attribute;
224 }
225
226 METHOD(pa_tnc_attr_t, destroy, void,
227 private_ietf_attr_remediation_instr_t *this)
228 {
229 if (ref_put(&this->ref))
230 {
231 free(this->parameters.ptr);
232 free(this->value.ptr);
233 free(this);
234 }
235 }
236
237 METHOD(ietf_attr_remediation_instr_t, get_parameters_type, pen_type_t,
238 private_ietf_attr_remediation_instr_t *this)
239 {
240 return this->parameters_type;
241 }
242
243 METHOD(ietf_attr_remediation_instr_t, get_parameters, chunk_t,
244 private_ietf_attr_remediation_instr_t *this)
245 {
246 return this->parameters;
247 }
248
249 METHOD(ietf_attr_remediation_instr_t, get_uri, chunk_t,
250 private_ietf_attr_remediation_instr_t *this)
251 {
252 return this->parameters;
253 }
254
255 METHOD(ietf_attr_remediation_instr_t, get_string, chunk_t,
256 private_ietf_attr_remediation_instr_t *this, chunk_t *lang_code)
257 {
258 if (lang_code)
259 {
260 *lang_code = this->lang_code;
261 }
262 return this->string;
263 }
264
265 /**
266 * Described in header.
267 */
268 pa_tnc_attr_t *ietf_attr_remediation_instr_create(pen_type_t parameters_type,
269 chunk_t parameters)
270 {
271 private_ietf_attr_remediation_instr_t *this;
272
273 INIT(this,
274 .public = {
275 .pa_tnc_attribute = {
276 .get_type = _get_type,
277 .get_value = _get_value,
278 .get_noskip_flag = _get_noskip_flag,
279 .set_noskip_flag = _set_noskip_flag,
280 .build = _build,
281 .process = _process,
282 .get_ref = _get_ref,
283 .destroy = _destroy,
284 },
285 .get_parameters_type = _get_parameters_type,
286 .get_parameters = _get_parameters,
287 .get_uri = _get_uri,
288 .get_string = _get_string,
289 },
290 .type = { PEN_IETF, IETF_ATTR_REMEDIATION_INSTRUCTIONS },
291 .parameters_type = parameters_type,
292 .parameters = chunk_clone(parameters),
293 .ref = 1,
294 );
295
296 return &this->public.pa_tnc_attribute;
297 }
298
299 /**
300 * Described in header.
301 */
302 pa_tnc_attr_t *ietf_attr_remediation_instr_create_from_uri(chunk_t uri)
303 {
304 pen_type_t type = { PEN_IETF, IETF_REMEDIATION_PARAMETERS_URI };
305
306 return ietf_attr_remediation_instr_create(type, uri);
307 }
308
309 /**
310 * Described in header.
311 */
312 pa_tnc_attr_t *ietf_attr_remediation_instr_create_from_string(chunk_t string,
313 chunk_t lang_code)
314 {
315 pa_tnc_attr_t *attr;
316 bio_writer_t *writer;
317 pen_type_t type = { PEN_IETF, IETF_REMEDIATION_PARAMETERS_STRING };
318
319 /* limit language code to 255 octets */
320 lang_code.len = min(255, lang_code.len);
321
322 writer = bio_writer_create(4 + string.len + 1 + lang_code.len);
323 writer->write_data32(writer, string);
324 writer->write_data8 (writer, lang_code);
325
326 attr = ietf_attr_remediation_instr_create(type, writer->get_buf(writer));
327 writer->destroy(writer);
328
329 return attr;
330 }
331
332 /**
333 * Described in header.
334 */
335 pa_tnc_attr_t *ietf_attr_remediation_instr_create_from_data(chunk_t data)
336 {
337 private_ietf_attr_remediation_instr_t *this;
338
339 INIT(this,
340 .public = {
341 .pa_tnc_attribute = {
342 .get_type = _get_type,
343 .get_value = _get_value,
344 .get_noskip_flag = _get_noskip_flag,
345 .set_noskip_flag = _set_noskip_flag,
346 .build = _build,
347 .process = _process,
348 .get_ref = _get_ref,
349 .destroy = _destroy,
350 },
351 .get_parameters_type = _get_parameters_type,
352 .get_parameters = _get_parameters,
353 .get_uri = _get_uri,
354 .get_string = _get_string,
355 },
356 .type = { PEN_IETF, IETF_ATTR_REMEDIATION_INSTRUCTIONS },
357 .value = chunk_clone(data),
358 .ref = 1,
359 );
360
361 return &this->public.pa_tnc_attribute;
362 }
363