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