0eea4c311a696af8a9d186932effbe36a98816dd
[strongswan.git] / src / libimcv / pa_tnc / pa_tnc_msg.c
1 /*
2 * Copyright (C) 2011 Andreas Steffen
3 *
4 * HSR Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "pa_tnc_msg.h"
18 #include "ietf/ietf_attr_pa_tnc_error.h"
19
20 #include <bio/bio_writer.h>
21 #include <bio/bio_reader.h>
22 #include <utils/linked_list.h>
23 #include <pen/pen.h>
24 #include <debug.h>
25
26
27 typedef struct private_pa_tnc_msg_t private_pa_tnc_msg_t;
28
29 /**
30 * PA-TNC message header
31 *
32 * 1 2 3
33 * 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
34 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 * | Version | Reserved |
36 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 * | Message Identifier |
38 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 */
40
41 #define PA_TNC_HEADER_SIZE 8
42 #define PA_TNC_VERSION 0x01
43 #define PA_TNC_RESERVED 0x000000
44
45 /**
46 * PA-TNC attribute
47 *
48 * 1 2 3
49 * 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
50 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 * | Flags | PA-TNC Attribute Vendor ID |
52 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53 * | PA-TNC Attribute Type |
54 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55 * | PA-TNC Attribute Length |
56 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
57 * | Attribute Value (Variable Length) |
58 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59 */
60
61 #define PA_TNC_ATTR_FLAG_NONE 0x00
62 #define PA_TNC_ATTR_FLAG_NOSKIP (1<<7)
63 #define PA_TNC_ATTR_HEADER_SIZE 12
64
65 /**
66 * Private data of a pa_tnc_msg_t object.
67 *
68 */
69 struct private_pa_tnc_msg_t {
70
71 /**
72 * Public pa_tnc_msg_t interface.
73 */
74 pa_tnc_msg_t public;
75
76 /**
77 * List of PA-TNC attributes
78 */
79 linked_list_t *attributes;
80
81 /**
82 * linked list of PA-TNC error messages
83 */
84 linked_list_t *errors;
85
86 /**
87 * Message identifier
88 */
89 u_int32_t identifier;
90
91 /**
92 * Encoded message
93 */
94 chunk_t encoding;
95 };
96
97 METHOD(pa_tnc_msg_t, get_encoding, chunk_t,
98 private_pa_tnc_msg_t *this)
99 {
100 return this->encoding;
101 }
102
103 METHOD(pa_tnc_msg_t, add_attribute, void,
104 private_pa_tnc_msg_t *this, pa_tnc_attr_t *attr)
105 {
106 this->attributes->insert_last(this->attributes, attr);
107 }
108
109 METHOD(pa_tnc_msg_t, build, void,
110 private_pa_tnc_msg_t *this)
111 {
112 bio_writer_t *writer;
113 enumerator_t *enumerator;
114 pa_tnc_attr_t *attr;
115 pen_t vendor_id;
116 u_int32_t type;
117 u_int8_t flags;
118 chunk_t value;
119 rng_t *rng;
120
121 /* create a random message identifier */
122 rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
123 rng->get_bytes(rng, sizeof(this->identifier), (u_int8_t*)&this->identifier);
124 rng->destroy(rng);
125 DBG2(DBG_TNC, "creating PA-TNC message with ID 0x%08x", this->identifier);
126
127 /* build message header */
128 writer = bio_writer_create(PA_TNC_HEADER_SIZE);
129 writer->write_uint8 (writer, PA_TNC_VERSION);
130 writer->write_uint24(writer, PA_TNC_RESERVED);
131 writer->write_uint32(writer, this->identifier);
132
133 /* build and append encoding of PA-TNC attributes */
134 enumerator = this->attributes->create_enumerator(this->attributes);
135 while (enumerator->enumerate(enumerator, &attr))
136 {
137 attr->build(attr);
138 vendor_id = attr->get_vendor_id(attr);
139 type = attr->get_type(attr);
140 value = attr->get_value(attr);
141 flags = attr->get_noskip_flag(attr) ? PA_TNC_ATTR_FLAG_NOSKIP :
142 PA_TNC_ATTR_FLAG_NONE;
143 DBG2(DBG_TNC, "creating PA-TNC attribute type 0x%06x(%N)/0x%08x",
144 vendor_id, pen_names, vendor_id, type);
145 DBG3(DBG_TNC, "%B", &value);
146
147 writer->write_uint8 (writer, flags);
148 writer->write_uint24(writer, vendor_id);
149 writer->write_uint32(writer, type);
150 writer->write_uint32(writer, PA_TNC_ATTR_HEADER_SIZE + value.len);
151 writer->write_data (writer, value);
152 }
153 enumerator->destroy(enumerator);
154
155 free(this->encoding.ptr);
156 this->encoding = chunk_clone(writer->get_buf(writer));
157 writer->destroy(writer);
158 }
159
160 METHOD(pa_tnc_msg_t, process, status_t,
161 private_pa_tnc_msg_t *this)
162 {
163 bio_reader_t *reader;
164 pa_tnc_attr_t *error;
165 u_int8_t version;
166 u_int32_t reserved;
167
168 /* process message header */
169 if (this->encoding.len < PA_TNC_HEADER_SIZE)
170 {
171 DBG1(DBG_TNC, "%u bytes insufficient to parse PA-TNC message header",
172 this->encoding.len);
173 return FAILED;
174 }
175 reader = bio_reader_create(this->encoding);
176 reader->read_uint8 (reader, &version);
177 reader->read_uint24(reader, &reserved);
178 reader->read_uint32(reader, &this->identifier);
179
180 if (version != PA_TNC_VERSION)
181 {
182 DBG1(DBG_TNC, "PA-TNC version %u not supported", version);
183 error = ietf_attr_pa_tnc_error_create(PEN_IETF,
184 PA_ERROR_VERSION_NOT_SUPPORTED, this->encoding);
185 goto err;
186 }
187 DBG2(DBG_TNC, "processing PA-TNC message with ID 0x%08x", this->identifier);
188
189 /* pre-process PA-TNC attributes */
190 while (reader->remaining(reader) >= PA_TNC_ATTR_HEADER_SIZE)
191 {
192 pen_t vendor_id;
193 u_int8_t flags;
194 u_int32_t type, length;
195 chunk_t value;
196 pa_tnc_attr_t *attr;
197
198 reader->read_uint8 (reader, &flags);
199 reader->read_uint24(reader, &vendor_id);
200 reader->read_uint32(reader, &type);
201 reader->read_uint32(reader, &length);
202 DBG2(DBG_TNC, "processing PA-TNC attribute type 0x%06x(%N)/0x%08x",
203 vendor_id, pen_names, vendor_id, type);
204
205 if (length < PA_TNC_ATTR_HEADER_SIZE)
206 {
207 DBG1(DBG_TNC, "%u bytes too small for PA-TNC attribute length",
208 length);
209 error = ietf_attr_pa_tnc_error_create(PEN_IETF,
210 PA_ERROR_INVALID_PARAMETER, this->encoding);
211 goto err;
212 }
213 length -= PA_TNC_ATTR_HEADER_SIZE;
214
215 if (!reader->read_data(reader, length , &value))
216 {
217 DBG1(DBG_TNC, "insufficient bytes for PA-TNC attribute value");
218 error = ietf_attr_pa_tnc_error_create(PEN_IETF,
219 PA_ERROR_INVALID_PARAMETER, this->encoding);
220 goto err;
221 }
222 DBG3(DBG_TNC, "%B", &value);
223
224 attr = pa_tnc_attr_create_from_data(vendor_id, type, value);
225 if (!attr)
226 {
227 if (flags & PA_TNC_ATTR_FLAG_NOSKIP)
228 {
229 DBG1(DBG_TNC, "unsupported PA-TNC attribute with NOSKIP flag");
230 error = ietf_attr_pa_tnc_error_create(PEN_IETF,
231 PA_ERROR_ATTR_TYPE_NOT_SUPPORTED, this->encoding);
232 goto err;
233 }
234 else
235 {
236 DBG1(DBG_TNC, "skipping unsupported PA-TNC attribute");
237 }
238 }
239
240 if (attr->process(attr) != SUCCESS)
241 {
242 attr->destroy(attr);
243 error = ietf_attr_pa_tnc_error_create(PEN_IETF,
244 PA_ERROR_INVALID_PARAMETER, this->encoding);
245 goto err;
246 }
247 add_attribute(this, attr);
248 }
249
250 if (reader->remaining(reader) == 0)
251 {
252 reader->destroy(reader);
253 return SUCCESS;
254 }
255
256 err:
257 reader->destroy(reader);
258 this->errors->insert_last(this->errors, error);
259 return VERIFY_ERROR;
260 }
261
262 METHOD(pa_tnc_msg_t, create_attribute_enumerator, enumerator_t*,
263 private_pa_tnc_msg_t *this)
264 {
265 return this->attributes->create_enumerator(this->attributes);
266 }
267
268 METHOD(pa_tnc_msg_t, create_error_enumerator, enumerator_t*,
269 private_pa_tnc_msg_t *this)
270 {
271 return this->errors->create_enumerator(this->attributes);
272 }
273
274 METHOD(pa_tnc_msg_t, destroy, void,
275 private_pa_tnc_msg_t *this)
276 {
277 this->attributes->destroy_offset(this->attributes,
278 offsetof(pa_tnc_attr_t, destroy));
279 this->errors->destroy_offset(this->errors,
280 offsetof(pa_tnc_attr_t, destroy));
281 free(this->encoding.ptr);
282 free(this);
283 }
284
285 /**
286 * See header
287 */
288 pa_tnc_msg_t *pa_tnc_msg_create_from_data(chunk_t data)
289 {
290 private_pa_tnc_msg_t *this;
291
292 INIT(this,
293 .public = {
294 .get_encoding = _get_encoding,
295 .add_attribute = _add_attribute,
296 .build = _build,
297 .process = _process,
298 .create_attribute_enumerator = _create_attribute_enumerator,
299 .create_error_enumerator = _create_error_enumerator,
300 .destroy = _destroy,
301 },
302 .encoding = chunk_clone(data),
303 .attributes = linked_list_create(),
304 .errors = linked_list_create(),
305 );
306
307 return &this->public;
308 }
309
310 /**
311 * See header
312 */
313 pa_tnc_msg_t *pa_tnc_msg_create(void)
314 {
315 return pa_tnc_msg_create_from_data(chunk_empty);
316 }
317