allow registration of multiple message type
[strongswan.git] / src / libimcv / plugins / imc_test / imc_test.c
1 /*
2 * Copyright (C) 2011-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 "imc_test_state.h"
17
18 #include <imc/imc_agent.h>
19 #include <pa_tnc/pa_tnc_msg.h>
20 #include <ietf/ietf_attr.h>
21 #include <ietf/ietf_attr_pa_tnc_error.h>
22 #include <ietf/ietf_attr_assess_result.h>
23 #include <ita/ita_attr.h>
24 #include <ita/ita_attr_command.h>
25 #include <ita/ita_attr_dummy.h>
26
27 #include <tncif_pa_subtypes.h>
28
29 #include <pen/pen.h>
30 #include <debug.h>
31
32 /* IMC definitions */
33
34 static const char imc_name[] = "Test";
35
36 static pen_type_t msg_types[] = {
37 { PEN_ITA, PA_SUBTYPE_ITA_TEST }
38 };
39
40 static imc_agent_t *imc_test;
41
42 /**
43 * see section 3.8.1 of TCG TNC IF-IMC Specification 1.3
44 */
45 TNC_Result TNC_IMC_Initialize(TNC_IMCID imc_id,
46 TNC_Version min_version,
47 TNC_Version max_version,
48 TNC_Version *actual_version)
49 {
50 if (imc_test)
51 {
52 DBG1(DBG_IMC, "IMC \"%s\" has already been initialized", imc_name);
53 return TNC_RESULT_ALREADY_INITIALIZED;
54 }
55 imc_test = imc_agent_create(imc_name, msg_types, 1, imc_id, actual_version);
56 if (!imc_test)
57 {
58 return TNC_RESULT_FATAL;
59 }
60 if (min_version > TNC_IFIMC_VERSION_1 || max_version < TNC_IFIMC_VERSION_1)
61 {
62 DBG1(DBG_IMC, "no common IF-IMC version");
63 return TNC_RESULT_NO_COMMON_VERSION;
64 }
65 return TNC_RESULT_SUCCESS;
66 }
67
68 /**
69 * see section 3.8.2 of TCG TNC IF-IMC Specification 1.3
70 */
71 TNC_Result TNC_IMC_NotifyConnectionChange(TNC_IMCID imc_id,
72 TNC_ConnectionID connection_id,
73 TNC_ConnectionState new_state)
74 {
75 imc_state_t *state;
76 imc_test_state_t *test_state;
77 TNC_Result result;
78 TNC_UInt32 additional_id;
79 char *command;
80 bool retry;
81 void *pointer;
82 enumerator_t *enumerator;
83 int dummy_size, additional_ids;
84
85 if (!imc_test)
86 {
87 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
88 return TNC_RESULT_NOT_INITIALIZED;
89 }
90
91 switch (new_state)
92 {
93 case TNC_CONNECTION_STATE_CREATE:
94 command = lib->settings->get_str(lib->settings,
95 "libimcv.plugins.imc-test.command", "none");
96 dummy_size = lib->settings->get_int(lib->settings,
97 "libimcv.plugins.imc-test.dummy_size", 0);
98 retry = lib->settings->get_bool(lib->settings,
99 "libimcv.plugins.imc-test.retry", FALSE);
100 state = imc_test_state_create(connection_id, command, dummy_size,
101 retry);
102
103 result = imc_test->create_state(imc_test, state);
104 if (result != TNC_RESULT_SUCCESS)
105 {
106 return result;
107 }
108
109 /* Optionally reserve additional IMC IDs */
110 additional_ids = lib->settings->get_int(lib->settings,
111 "libimcv.plugins.imc-test.additional_ids", 0);
112 imc_test->reserve_additional_ids(imc_test, additional_ids -
113 imc_test->count_additional_ids(imc_test));
114
115 return TNC_RESULT_SUCCESS;
116
117 case TNC_CONNECTION_STATE_HANDSHAKE:
118 /* get updated IMC state */
119 result = imc_test->change_state(imc_test, connection_id,
120 new_state, &state);
121 if (result != TNC_RESULT_SUCCESS)
122 {
123 return TNC_RESULT_FATAL;
124 }
125 test_state = (imc_test_state_t*)state;
126
127 /* is it the first handshake or a retry ? */
128 if (!test_state->is_first_handshake(test_state))
129 {
130 command = lib->settings->get_str(lib->settings,
131 "libimcv.plugins.imc-test.retry_command",
132 test_state->get_command(test_state));
133 test_state->set_command(test_state, command);
134 }
135
136 state->set_result(state, imc_id, TNC_IMV_EVALUATION_RESULT_DONT_KNOW);
137
138 /* Exit if there are no additional IMC IDs */
139 if (!imc_test->count_additional_ids(imc_test))
140 {
141 return result;
142 }
143
144 enumerator = imc_test->create_id_enumerator(imc_test);
145 while (enumerator->enumerate(enumerator, &pointer))
146 {
147 /* interpret pointer as scalar value */
148 additional_id = (TNC_UInt32)pointer;
149
150 state->set_result(state, additional_id,
151 TNC_IMV_EVALUATION_RESULT_DONT_KNOW);
152 }
153 enumerator->destroy(enumerator);
154
155 return TNC_RESULT_SUCCESS;
156
157 case TNC_CONNECTION_STATE_DELETE:
158 return imc_test->delete_state(imc_test, connection_id);
159
160 case TNC_CONNECTION_STATE_ACCESS_ISOLATED:
161 case TNC_CONNECTION_STATE_ACCESS_NONE:
162 /* get updated IMC state */
163 result = imc_test->change_state(imc_test, connection_id,
164 new_state, &state);
165 if (result != TNC_RESULT_SUCCESS)
166 {
167 return TNC_RESULT_FATAL;
168 }
169 test_state = (imc_test_state_t*)state;
170
171 /* do a handshake retry? */
172 if (test_state->do_handshake_retry(test_state))
173 {
174 return imc_test->request_handshake_retry(imc_id, connection_id,
175 TNC_RETRY_REASON_IMC_REMEDIATION_COMPLETE);
176 }
177 return TNC_RESULT_SUCCESS;
178
179 default:
180 return imc_test->change_state(imc_test, connection_id,
181 new_state, NULL);
182 }
183 }
184
185 static TNC_Result send_message(imc_state_t *state, TNC_UInt32 src_imc_id,
186 TNC_UInt32 dst_imv_id)
187 {
188 imc_test_state_t *test_state;
189 linked_list_t *attr_list;
190 pa_tnc_attr_t *attr;
191 bool excl;
192 TNC_ConnectionID connection_id;
193 TNC_Result result;
194
195 attr_list = linked_list_create();
196 connection_id = state->get_connection_id(state);
197 test_state = (imc_test_state_t*)state;
198
199 if (test_state->get_dummy_size(test_state))
200 {
201 attr = ita_attr_dummy_create(test_state->get_dummy_size(test_state));
202 attr->set_noskip_flag(attr, TRUE);
203 attr_list->insert_last(attr_list, attr);
204 }
205 attr = ita_attr_command_create(test_state->get_command(test_state));
206 attr->set_noskip_flag(attr, TRUE);
207 attr_list->insert_last(attr_list, attr);
208
209 excl = dst_imv_id != TNC_IMVID_ANY;
210 result = imc_test->send_message(imc_test, connection_id, excl, src_imc_id,
211 dst_imv_id, PEN_ITA, PA_SUBTYPE_ITA_TEST,
212 attr_list);
213 attr_list->destroy(attr_list);
214
215 return result;
216 }
217
218 /**
219 * see section 3.8.3 of TCG TNC IF-IMC Specification 1.3
220 */
221 TNC_Result TNC_IMC_BeginHandshake(TNC_IMCID imc_id,
222 TNC_ConnectionID connection_id)
223 {
224 imc_state_t *state;
225 enumerator_t *enumerator;
226 void *pointer;
227 TNC_UInt32 additional_id;
228 TNC_Result result;
229
230 if (!imc_test)
231 {
232 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
233 return TNC_RESULT_NOT_INITIALIZED;
234 }
235
236 /* get current IMC state */
237 if (!imc_test->get_state(imc_test, connection_id, &state))
238 {
239 return TNC_RESULT_FATAL;
240 }
241
242 /* send PA message for primary IMC ID */
243 result = send_message(state, imc_id, TNC_IMVID_ANY);
244
245 /* Exit if there are no additional IMC IDs */
246 if (!imc_test->count_additional_ids(imc_test))
247 {
248 return result;
249 }
250
251 /* Do we have support for transporting multiple IMC IDs? */
252 if (!state->has_long(state))
253 {
254 DBG1(DBG_IMC, "IMC %u \"%s\" did not detect support for transporting "
255 "multiple IMC IDs", imc_id, imc_name);
256 return result;
257 }
258
259 /* send PA messages for additional IMC IDs */
260 enumerator = imc_test->create_id_enumerator(imc_test);
261 while (result == TNC_RESULT_SUCCESS &&
262 enumerator->enumerate(enumerator, &pointer))
263 {
264 /* interpret pointer as scalar value */
265 additional_id = (TNC_UInt32)pointer;
266 result = send_message(state, additional_id, TNC_IMVID_ANY);
267 }
268 enumerator->destroy(enumerator);
269
270 return result;
271 }
272
273 static TNC_Result receive_message(TNC_IMCID imc_id,
274 TNC_ConnectionID connection_id,
275 TNC_UInt32 msg_flags,
276 chunk_t msg,
277 TNC_VendorID msg_vid,
278 TNC_MessageSubtype msg_subtype,
279 TNC_UInt32 src_imv_id,
280 TNC_UInt32 dst_imc_id)
281 {
282 pa_tnc_msg_t *pa_tnc_msg;
283 pa_tnc_attr_t *attr;
284 pen_type_t attr_type;
285 imc_state_t *state;
286 enumerator_t *enumerator;
287 TNC_Result result;
288 TNC_UInt32 target_imc_id;
289 bool fatal_error = FALSE;
290
291 if (!imc_test)
292 {
293 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
294 return TNC_RESULT_NOT_INITIALIZED;
295 }
296
297 /* get current IMC state */
298 if (!imc_test->get_state(imc_test, connection_id, &state))
299 {
300 return TNC_RESULT_FATAL;
301 }
302
303 /* parse received PA-TNC message and automatically handle any errors */
304 result = imc_test->receive_message(imc_test, state, msg, msg_vid,
305 msg_subtype, src_imv_id, dst_imc_id, &pa_tnc_msg);
306
307 /* no parsed PA-TNC attributes available if an error occurred */
308 if (!pa_tnc_msg)
309 {
310 return result;
311 }
312 target_imc_id = (dst_imc_id == TNC_IMCID_ANY) ? imc_id : dst_imc_id;
313
314 /* preprocess any IETF standard error attributes */
315 fatal_error = pa_tnc_msg->process_ietf_std_errors(pa_tnc_msg);
316
317 /* analyze PA-TNC attributes */
318 enumerator = pa_tnc_msg->create_attribute_enumerator(pa_tnc_msg);
319 while (enumerator->enumerate(enumerator, &attr))
320 {
321 attr_type = attr->get_type(attr);
322
323 if (attr_type.vendor_id == PEN_IETF)
324 {
325 ietf_attr_assess_result_t *ietf_attr;
326
327 ietf_attr = (ietf_attr_assess_result_t*)attr;
328 state->set_result(state, target_imc_id,
329 ietf_attr->get_result(ietf_attr));
330 }
331 else if (attr_type.vendor_id == PEN_ITA)
332 {
333 if (attr_type.type == ITA_ATTR_COMMAND)
334 {
335 ita_attr_command_t *ita_attr;
336
337 ita_attr = (ita_attr_command_t*)attr;
338 DBG1(DBG_IMC, "received command '%s'",
339 ita_attr->get_command(ita_attr));
340 }
341 else if (attr_type.type == ITA_ATTR_DUMMY)
342 {
343 ita_attr_dummy_t *ita_attr;
344
345 ita_attr = (ita_attr_dummy_t*)attr;
346 DBG1(DBG_IMC, "received dummy attribute value (%d bytes)",
347 ita_attr->get_size(ita_attr));
348 }
349 }
350 }
351 enumerator->destroy(enumerator);
352 pa_tnc_msg->destroy(pa_tnc_msg);
353
354 if (fatal_error)
355 {
356 return TNC_RESULT_FATAL;
357 }
358
359 /* if no assessment result is known then repeat the measurement */
360 return state->get_result(state, target_imc_id, NULL) ?
361 TNC_RESULT_SUCCESS : send_message(state, dst_imc_id, src_imv_id);
362 }
363
364 /**
365 * see section 3.8.4 of TCG TNC IF-IMC Specification 1.3
366 */
367 TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
368 TNC_ConnectionID connection_id,
369 TNC_BufferReference msg,
370 TNC_UInt32 msg_len,
371 TNC_MessageType msg_type)
372 {
373 TNC_VendorID msg_vid;
374 TNC_MessageSubtype msg_subtype;
375
376 msg_vid = msg_type >> 8;
377 msg_subtype = msg_type & TNC_SUBTYPE_ANY;
378
379 return receive_message(imc_id, connection_id, 0, chunk_create(msg, msg_len),
380 msg_vid, msg_subtype, 0, TNC_IMCID_ANY);
381 }
382
383 /**
384 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
385 */
386 TNC_Result TNC_IMC_ReceiveMessageLong(TNC_IMCID imc_id,
387 TNC_ConnectionID connection_id,
388 TNC_UInt32 msg_flags,
389 TNC_BufferReference msg,
390 TNC_UInt32 msg_len,
391 TNC_VendorID msg_vid,
392 TNC_MessageSubtype msg_subtype,
393 TNC_UInt32 src_imv_id,
394 TNC_UInt32 dst_imc_id)
395 {
396 return receive_message(imc_id, connection_id, msg_flags,
397 chunk_create(msg, msg_len), msg_vid, msg_subtype,
398 src_imv_id, dst_imc_id);
399 }
400
401 /**
402 * see section 3.8.7 of TCG TNC IF-IMC Specification 1.3
403 */
404 TNC_Result TNC_IMC_BatchEnding(TNC_IMCID imc_id,
405 TNC_ConnectionID connection_id)
406 {
407 if (!imc_test)
408 {
409 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
410 return TNC_RESULT_NOT_INITIALIZED;
411 }
412 return TNC_RESULT_SUCCESS;
413 }
414
415 /**
416 * see section 3.8.8 of TCG TNC IF-IMC Specification 1.3
417 */
418 TNC_Result TNC_IMC_Terminate(TNC_IMCID imc_id)
419 {
420 if (!imc_test)
421 {
422 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
423 return TNC_RESULT_NOT_INITIALIZED;
424 }
425 imc_test->destroy(imc_test);
426 imc_test = NULL;
427
428 return TNC_RESULT_SUCCESS;
429 }
430
431 /**
432 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.3
433 */
434 TNC_Result TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id,
435 TNC_TNCC_BindFunctionPointer bind_function)
436 {
437 if (!imc_test)
438 {
439 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
440 return TNC_RESULT_NOT_INITIALIZED;
441 }
442 return imc_test->bind_functions(imc_test, bind_function);
443 }