use base IMC ID if src IMC ID is not supported
[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 #define IMC_VENDOR_ID PEN_ITA
37 #define IMC_SUBTYPE PA_SUBTYPE_ITA_TEST
38
39 static imc_agent_t *imc_test;
40
41 /**
42 * see section 3.8.1 of TCG TNC IF-IMC Specification 1.3
43 */
44 TNC_Result TNC_IMC_Initialize(TNC_IMCID imc_id,
45 TNC_Version min_version,
46 TNC_Version max_version,
47 TNC_Version *actual_version)
48 {
49 if (imc_test)
50 {
51 DBG1(DBG_IMC, "IMC \"%s\" has already been initialized", imc_name);
52 return TNC_RESULT_ALREADY_INITIALIZED;
53 }
54 imc_test = imc_agent_create(imc_name, IMC_VENDOR_ID, IMC_SUBTYPE,
55 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, attr_list);
212 attr_list->destroy(attr_list);
213
214 return result;
215 }
216
217 /**
218 * see section 3.8.3 of TCG TNC IF-IMC Specification 1.3
219 */
220 TNC_Result TNC_IMC_BeginHandshake(TNC_IMCID imc_id,
221 TNC_ConnectionID connection_id)
222 {
223 imc_state_t *state;
224 enumerator_t *enumerator;
225 void *pointer;
226 TNC_UInt32 additional_id;
227 TNC_Result result;
228
229 if (!imc_test)
230 {
231 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
232 return TNC_RESULT_NOT_INITIALIZED;
233 }
234
235 /* get current IMC state */
236 if (!imc_test->get_state(imc_test, connection_id, &state))
237 {
238 return TNC_RESULT_FATAL;
239 }
240
241 /* send PA message for primary IMC ID */
242 result = send_message(state, imc_id, TNC_IMVID_ANY);
243
244 /* Exit if there are no additional IMC IDs */
245 if (!imc_test->count_additional_ids(imc_test))
246 {
247 return result;
248 }
249
250 /* Do we have support for transporting multiple IMC IDs? */
251 if (!state->has_long(state))
252 {
253 DBG1(DBG_IMC, "IMC %u \"%s\" did not detect support for transporting "
254 "multiple IMC IDs", imc_id, imc_name);
255 return result;
256 }
257
258 /* send PA messages for additional IMC IDs */
259 enumerator = imc_test->create_id_enumerator(imc_test);
260 while (result == TNC_RESULT_SUCCESS &&
261 enumerator->enumerate(enumerator, &pointer))
262 {
263 /* interpret pointer as scalar value */
264 additional_id = (TNC_UInt32)pointer;
265 result = send_message(state, additional_id, TNC_IMVID_ANY);
266 }
267 enumerator->destroy(enumerator);
268
269 return result;
270 }
271
272 static TNC_Result receive_message(TNC_IMCID imc_id,
273 TNC_ConnectionID connection_id,
274 TNC_UInt32 msg_flags,
275 chunk_t msg,
276 TNC_VendorID msg_vid,
277 TNC_MessageSubtype msg_subtype,
278 TNC_UInt32 src_imv_id,
279 TNC_UInt32 dst_imc_id)
280 {
281 pa_tnc_msg_t *pa_tnc_msg;
282 pa_tnc_attr_t *attr;
283 pen_type_t attr_type;
284 imc_state_t *state;
285 enumerator_t *enumerator;
286 TNC_Result result;
287 TNC_UInt32 target_imc_id;
288 bool fatal_error = FALSE;
289
290 if (!imc_test)
291 {
292 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
293 return TNC_RESULT_NOT_INITIALIZED;
294 }
295
296 /* get current IMC state */
297 if (!imc_test->get_state(imc_test, connection_id, &state))
298 {
299 return TNC_RESULT_FATAL;
300 }
301
302 /* parse received PA-TNC message and automatically handle any errors */
303 result = imc_test->receive_message(imc_test, state, msg, msg_vid,
304 msg_subtype, src_imv_id, dst_imc_id, &pa_tnc_msg);
305
306 /* no parsed PA-TNC attributes available if an error occurred */
307 if (!pa_tnc_msg)
308 {
309 return result;
310 }
311 target_imc_id = (dst_imc_id == TNC_IMCID_ANY) ? imc_id : dst_imc_id;
312
313 /* preprocess any IETF standard error attributes */
314 fatal_error = pa_tnc_msg->process_ietf_std_errors(pa_tnc_msg);
315
316 /* analyze PA-TNC attributes */
317 enumerator = pa_tnc_msg->create_attribute_enumerator(pa_tnc_msg);
318 while (enumerator->enumerate(enumerator, &attr))
319 {
320 attr_type = attr->get_type(attr);
321
322 if (attr_type.vendor_id == PEN_IETF)
323 {
324 ietf_attr_assess_result_t *ietf_attr;
325
326 ietf_attr = (ietf_attr_assess_result_t*)attr;
327 state->set_result(state, target_imc_id,
328 ietf_attr->get_result(ietf_attr));
329 }
330 else if (attr_type.vendor_id == PEN_ITA)
331 {
332 if (attr_type.type == ITA_ATTR_COMMAND)
333 {
334 ita_attr_command_t *ita_attr;
335
336 ita_attr = (ita_attr_command_t*)attr;
337 DBG1(DBG_IMC, "received command '%s'",
338 ita_attr->get_command(ita_attr));
339 }
340 else if (attr_type.type == ITA_ATTR_DUMMY)
341 {
342 ita_attr_dummy_t *ita_attr;
343
344 ita_attr = (ita_attr_dummy_t*)attr;
345 DBG1(DBG_IMC, "received dummy attribute value (%d bytes)",
346 ita_attr->get_size(ita_attr));
347 }
348 }
349 }
350 enumerator->destroy(enumerator);
351 pa_tnc_msg->destroy(pa_tnc_msg);
352
353 if (fatal_error)
354 {
355 return TNC_RESULT_FATAL;
356 }
357
358 /* if no assessment result is known then repeat the measurement */
359 return state->get_result(state, target_imc_id, NULL) ?
360 TNC_RESULT_SUCCESS : send_message(state, dst_imc_id, src_imv_id);
361 }
362
363 /**
364 * see section 3.8.4 of TCG TNC IF-IMC Specification 1.3
365 */
366 TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
367 TNC_ConnectionID connection_id,
368 TNC_BufferReference msg,
369 TNC_UInt32 msg_len,
370 TNC_MessageType msg_type)
371 {
372 TNC_VendorID msg_vid;
373 TNC_MessageSubtype msg_subtype;
374
375 msg_vid = msg_type >> 8;
376 msg_subtype = msg_type & TNC_SUBTYPE_ANY;
377
378 return receive_message(imc_id, connection_id, 0, chunk_create(msg, msg_len),
379 msg_vid, msg_subtype, 0, TNC_IMCID_ANY);
380 }
381
382 /**
383 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
384 */
385 TNC_Result TNC_IMC_ReceiveMessageLong(TNC_IMCID imc_id,
386 TNC_ConnectionID connection_id,
387 TNC_UInt32 msg_flags,
388 TNC_BufferReference msg,
389 TNC_UInt32 msg_len,
390 TNC_VendorID msg_vid,
391 TNC_MessageSubtype msg_subtype,
392 TNC_UInt32 src_imv_id,
393 TNC_UInt32 dst_imc_id)
394 {
395 return receive_message(imc_id, connection_id, msg_flags,
396 chunk_create(msg, msg_len), msg_vid, msg_subtype,
397 src_imv_id, dst_imc_id);
398 }
399
400 /**
401 * see section 3.8.7 of TCG TNC IF-IMC Specification 1.3
402 */
403 TNC_Result TNC_IMC_BatchEnding(TNC_IMCID imc_id,
404 TNC_ConnectionID connection_id)
405 {
406 if (!imc_test)
407 {
408 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
409 return TNC_RESULT_NOT_INITIALIZED;
410 }
411 return TNC_RESULT_SUCCESS;
412 }
413
414 /**
415 * see section 3.8.8 of TCG TNC IF-IMC Specification 1.3
416 */
417 TNC_Result TNC_IMC_Terminate(TNC_IMCID imc_id)
418 {
419 if (!imc_test)
420 {
421 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
422 return TNC_RESULT_NOT_INITIALIZED;
423 }
424 imc_test->destroy(imc_test);
425 imc_test = NULL;
426
427 return TNC_RESULT_SUCCESS;
428 }
429
430 /**
431 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.3
432 */
433 TNC_Result TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id,
434 TNC_TNCC_BindFunctionPointer bind_function)
435 {
436 if (!imc_test)
437 {
438 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
439 return TNC_RESULT_NOT_INITIALIZED;
440 }
441 return imc_test->bind_functions(imc_test, bind_function);
442 }