introduced sending of standard IETF Assessment Result PA-TNC attribute by IMVs
[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 bool fatal_error = FALSE;
288
289 if (!imc_test)
290 {
291 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
292 return TNC_RESULT_NOT_INITIALIZED;
293 }
294
295 /* get current IMC state */
296 if (!imc_test->get_state(imc_test, connection_id, &state))
297 {
298 return TNC_RESULT_FATAL;
299 }
300
301 /* parse received PA-TNC message and automatically handle any errors */
302 result = imc_test->receive_message(imc_test, state, msg, msg_vid,
303 msg_subtype, src_imv_id, dst_imc_id, &pa_tnc_msg);
304
305 /* no parsed PA-TNC attributes available if an error occurred */
306 if (!pa_tnc_msg)
307 {
308 return result;
309 }
310
311 /* preprocess any IETF standard error attributes */
312 fatal_error = pa_tnc_msg->process_ietf_std_errors(pa_tnc_msg);
313
314 /* analyze PA-TNC attributes */
315 enumerator = pa_tnc_msg->create_attribute_enumerator(pa_tnc_msg);
316 while (enumerator->enumerate(enumerator, &attr))
317 {
318 attr_type = attr->get_type(attr);
319
320 if (attr_type.vendor_id == PEN_IETF)
321 {
322 ietf_attr_assess_result_t *ietf_attr;
323
324 ietf_attr = (ietf_attr_assess_result_t*)attr;
325 state->set_result(state, dst_imc_id,
326 ietf_attr->get_result(ietf_attr));
327 }
328 else if (attr_type.vendor_id == PEN_ITA)
329 {
330 if (attr_type.type == ITA_ATTR_COMMAND)
331 {
332 ita_attr_command_t *ita_attr;
333
334 ita_attr = (ita_attr_command_t*)attr;
335 DBG1(DBG_IMC, "received command '%s'",
336 ita_attr->get_command(ita_attr));
337 }
338 else if (attr_type.type == ITA_ATTR_DUMMY)
339 {
340 ita_attr_dummy_t *ita_attr;
341
342 ita_attr = (ita_attr_dummy_t*)attr;
343 DBG1(DBG_IMC, "received dummy attribute value (%d bytes)",
344 ita_attr->get_size(ita_attr));
345 }
346 }
347 }
348 enumerator->destroy(enumerator);
349 pa_tnc_msg->destroy(pa_tnc_msg);
350
351 if (fatal_error)
352 {
353 return TNC_RESULT_FATAL;
354 }
355
356 /* if no assessment result is known then repeat the measurement */
357 return state->get_result(state, dst_imc_id, NULL) ?
358 TNC_RESULT_SUCCESS : send_message(state, dst_imc_id, src_imv_id);
359 }
360
361 /**
362 * see section 3.8.4 of TCG TNC IF-IMC Specification 1.3
363 */
364 TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
365 TNC_ConnectionID connection_id,
366 TNC_BufferReference msg,
367 TNC_UInt32 msg_len,
368 TNC_MessageType msg_type)
369 {
370 TNC_VendorID msg_vid;
371 TNC_MessageSubtype msg_subtype;
372
373 msg_vid = msg_type >> 8;
374 msg_subtype = msg_type & TNC_SUBTYPE_ANY;
375
376 return receive_message(imc_id, connection_id, 0, chunk_create(msg, msg_len),
377 msg_vid, msg_subtype, 0, TNC_IMCID_ANY);
378 }
379
380 /**
381 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
382 */
383 TNC_Result TNC_IMC_ReceiveMessageLong(TNC_IMCID imc_id,
384 TNC_ConnectionID connection_id,
385 TNC_UInt32 msg_flags,
386 TNC_BufferReference msg,
387 TNC_UInt32 msg_len,
388 TNC_VendorID msg_vid,
389 TNC_MessageSubtype msg_subtype,
390 TNC_UInt32 src_imv_id,
391 TNC_UInt32 dst_imc_id)
392 {
393 return receive_message(imc_id, connection_id, msg_flags,
394 chunk_create(msg, msg_len), msg_vid, msg_subtype,
395 src_imv_id, dst_imc_id);
396 }
397
398 /**
399 * see section 3.8.7 of TCG TNC IF-IMC Specification 1.3
400 */
401 TNC_Result TNC_IMC_BatchEnding(TNC_IMCID imc_id,
402 TNC_ConnectionID connection_id)
403 {
404 if (!imc_test)
405 {
406 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
407 return TNC_RESULT_NOT_INITIALIZED;
408 }
409 return TNC_RESULT_SUCCESS;
410 }
411
412 /**
413 * see section 3.8.8 of TCG TNC IF-IMC Specification 1.3
414 */
415 TNC_Result TNC_IMC_Terminate(TNC_IMCID imc_id)
416 {
417 if (!imc_test)
418 {
419 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
420 return TNC_RESULT_NOT_INITIALIZED;
421 }
422 imc_test->destroy(imc_test);
423 imc_test = NULL;
424
425 return TNC_RESULT_SUCCESS;
426 }
427
428 /**
429 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.3
430 */
431 TNC_Result TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id,
432 TNC_TNCC_BindFunctionPointer bind_function)
433 {
434 if (!imc_test)
435 {
436 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
437 return TNC_RESULT_NOT_INITIALIZED;
438 }
439 return imc_test->bind_functions(imc_test, bind_function);
440 }