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