use countof()
[strongswan.git] / src / libimcv / plugins / imv_test / imv_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 "imv_test_state.h"
17
18 #include <imv/imv_agent.h>
19 #include <imv/imv_msg.h>
20 #include <ietf/ietf_attr.h>
21 #include <ietf/ietf_attr_pa_tnc_error.h>
22 #include <ita/ita_attr.h>
23 #include <ita/ita_attr_command.h>
24 #include <ita/ita_attr_dummy.h>
25
26 #include <tncif_names.h>
27 #include <tncif_pa_subtypes.h>
28
29 #include <pen/pen.h>
30 #include <utils/debug.h>
31
32 /* IMV definitions */
33
34 static const char imv_name[] = "Test";
35
36 static pen_type_t msg_types[] = {
37 { PEN_ITA, PA_SUBTYPE_ITA_TEST }
38 };
39
40 static imv_agent_t *imv_test;
41
42 /**
43 * see section 3.8.1 of TCG TNC IF-IMV Specification 1.3
44 */
45 TNC_Result TNC_IMV_Initialize(TNC_IMVID imv_id,
46 TNC_Version min_version,
47 TNC_Version max_version,
48 TNC_Version *actual_version)
49 {
50 if (imv_test)
51 {
52 DBG1(DBG_IMV, "IMV \"%s\" has already been initialized", imv_name);
53 return TNC_RESULT_ALREADY_INITIALIZED;
54 }
55 imv_test = imv_agent_create(imv_name, msg_types, countof(msg_types),
56 imv_id, actual_version);
57 if (!imv_test)
58 {
59 return TNC_RESULT_FATAL;
60 }
61 if (min_version > TNC_IFIMV_VERSION_1 || max_version < TNC_IFIMV_VERSION_1)
62 {
63 DBG1(DBG_IMV, "no common IF-IMV version");
64 return TNC_RESULT_NO_COMMON_VERSION;
65 }
66 return TNC_RESULT_SUCCESS;
67 }
68
69 /**
70 * see section 3.8.2 of TCG TNC IF-IMV Specification 1.3
71 */
72 TNC_Result TNC_IMV_NotifyConnectionChange(TNC_IMVID imv_id,
73 TNC_ConnectionID connection_id,
74 TNC_ConnectionState new_state)
75 {
76 imv_state_t *state;
77
78 if (!imv_test)
79 {
80 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
81 return TNC_RESULT_NOT_INITIALIZED;
82 }
83 switch (new_state)
84 {
85 case TNC_CONNECTION_STATE_CREATE:
86 state = imv_test_state_create(connection_id);
87 return imv_test->create_state(imv_test, state);
88 case TNC_CONNECTION_STATE_DELETE:
89 return imv_test->delete_state(imv_test, connection_id);
90 default:
91 return imv_test->change_state(imv_test, connection_id,
92 new_state, NULL);
93 }
94 }
95
96 static TNC_Result receive_message(imv_state_t *state, imv_msg_t *in_msg)
97 {
98 imv_msg_t *out_msg;
99 imv_test_state_t *test_state;
100 enumerator_t *enumerator;
101 pa_tnc_attr_t *attr;
102 pen_type_t attr_type;
103 TNC_Result result;
104 int rounds;
105 bool fatal_error = FALSE, received_command = FALSE, retry = FALSE;
106
107 /* parse received PA-TNC message and handle local and remote errors */
108 result = in_msg->receive(in_msg, &fatal_error);
109 if (result != TNC_RESULT_SUCCESS)
110 {
111 return result;
112 }
113
114 /* add any new IMC and set its number of rounds */
115 rounds = lib->settings->get_int(lib->settings,
116 "libimcv.plugins.imv-test.rounds", 0);
117 test_state = (imv_test_state_t*)state;
118 test_state->add_imc(test_state, in_msg->get_src_id(in_msg), rounds);
119
120 /* analyze PA-TNC attributes */
121 enumerator = in_msg->create_attribute_enumerator(in_msg);
122 while (enumerator->enumerate(enumerator, &attr))
123 {
124 attr_type = attr->get_type(attr);
125
126 if (attr_type.vendor_id != PEN_ITA)
127 {
128 continue;
129 }
130 if (attr_type.type == ITA_ATTR_COMMAND)
131 {
132 ita_attr_command_t *ita_attr;
133 char *command;
134
135 received_command = TRUE;
136 ita_attr = (ita_attr_command_t*)attr;
137 command = ita_attr->get_command(ita_attr);
138
139 if (streq(command, "allow"))
140 {
141 state->set_recommendation(state,
142 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
143 TNC_IMV_EVALUATION_RESULT_COMPLIANT);
144 }
145 else if (streq(command, "isolate"))
146 {
147 state->set_recommendation(state,
148 TNC_IMV_ACTION_RECOMMENDATION_ISOLATE,
149 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MINOR);
150 }
151 else if (streq(command, "block") || streq(command, "none"))
152 {
153 state->set_recommendation(state,
154 TNC_IMV_ACTION_RECOMMENDATION_NO_ACCESS,
155 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MAJOR);
156 }
157 else if (streq(command, "retry"))
158 {
159 retry = TRUE;
160 }
161 else
162 {
163 DBG1(DBG_IMV, "unsupported ITA Command '%s'", command);
164 state->set_recommendation(state,
165 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
166 TNC_IMV_EVALUATION_RESULT_ERROR);
167 }
168 }
169 else if (attr_type.type == ITA_ATTR_DUMMY)
170 {
171 ita_attr_dummy_t *ita_attr;
172
173 ita_attr = (ita_attr_dummy_t*)attr;
174 DBG1(DBG_IMV, "received dummy attribute value (%d bytes)",
175 ita_attr->get_size(ita_attr));
176 }
177 }
178 enumerator->destroy(enumerator);
179
180 if (fatal_error)
181 {
182 state->set_recommendation(state,
183 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
184 TNC_IMV_EVALUATION_RESULT_ERROR);
185 out_msg = imv_msg_create_as_reply(in_msg);
186 result = out_msg->send_assessment(out_msg);
187 out_msg->destroy(out_msg);
188 if (result != TNC_RESULT_SUCCESS)
189 {
190 return result;
191 }
192 return imv_test->provide_recommendation(imv_test, state);
193 }
194
195 /* request a handshake retry ? */
196 if (retry)
197 {
198 test_state->set_rounds(test_state, rounds);
199 return imv_test->request_handshake_retry(imv_test->get_id(imv_test),
200 state->get_connection_id(state),
201 TNC_RETRY_REASON_IMV_SERIOUS_EVENT);
202 }
203
204 /* repeat the measurement ? */
205 if (test_state->another_round(test_state, in_msg->get_src_id(in_msg)))
206 {
207 out_msg = imv_msg_create_as_reply(in_msg);
208 attr = ita_attr_command_create("repeat");
209 out_msg->add_attribute(out_msg, attr);
210
211 /* send PA-TNC message with excl flag set */
212 result = out_msg->send(out_msg, TRUE);
213 out_msg->destroy(out_msg);
214
215 return result;
216 }
217
218 if (received_command)
219 {
220 out_msg = imv_msg_create_as_reply(in_msg);
221 result = out_msg->send_assessment(out_msg);
222 out_msg->destroy(out_msg);
223 if (result != TNC_RESULT_SUCCESS)
224 {
225 return result;
226 }
227 return imv_test->provide_recommendation(imv_test, state);
228 }
229 else
230 {
231 return TNC_RESULT_SUCCESS;
232 }
233 }
234
235 /**
236 * see section 3.8.4 of TCG TNC IF-IMV Specification 1.3
237 */
238 TNC_Result TNC_IMV_ReceiveMessage(TNC_IMVID imv_id,
239 TNC_ConnectionID connection_id,
240 TNC_BufferReference msg,
241 TNC_UInt32 msg_len,
242 TNC_MessageType msg_type)
243 {
244 imv_state_t *state;
245 imv_msg_t *in_msg;
246 TNC_Result result;
247
248 if (!imv_test)
249 {
250 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
251 return TNC_RESULT_NOT_INITIALIZED;
252 }
253 if (!imv_test->get_state(imv_test, connection_id, &state))
254 {
255 return TNC_RESULT_FATAL;
256 }
257 in_msg = imv_msg_create_from_data(imv_test, state, connection_id, msg_type,
258 chunk_create(msg, msg_len));
259 result = receive_message(state, in_msg);
260 in_msg->destroy(in_msg);
261
262 return result;
263 }
264
265 /**
266 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
267 */
268 TNC_Result TNC_IMV_ReceiveMessageLong(TNC_IMVID imv_id,
269 TNC_ConnectionID connection_id,
270 TNC_UInt32 msg_flags,
271 TNC_BufferReference msg,
272 TNC_UInt32 msg_len,
273 TNC_VendorID msg_vid,
274 TNC_MessageSubtype msg_subtype,
275 TNC_UInt32 src_imc_id,
276 TNC_UInt32 dst_imv_id)
277 {
278 imv_state_t *state;
279 imv_msg_t *in_msg;
280 TNC_Result result;
281
282 if (!imv_test)
283 {
284 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
285 return TNC_RESULT_NOT_INITIALIZED;
286 }
287 if (!imv_test->get_state(imv_test, connection_id, &state))
288 {
289 return TNC_RESULT_FATAL;
290 }
291 in_msg = imv_msg_create_from_long_data(imv_test, state, connection_id,
292 src_imc_id, dst_imv_id, msg_vid, msg_subtype,
293 chunk_create(msg, msg_len));
294 result =receive_message(state, in_msg);
295 in_msg->destroy(in_msg);
296
297 return result;
298 }
299
300 /**
301 * see section 3.8.7 of TCG TNC IF-IMV Specification 1.3
302 */
303 TNC_Result TNC_IMV_SolicitRecommendation(TNC_IMVID imv_id,
304 TNC_ConnectionID connection_id)
305 {
306 imv_state_t *state;
307
308 if (!imv_test)
309 {
310 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
311 return TNC_RESULT_NOT_INITIALIZED;
312 }
313 if (!imv_test->get_state(imv_test, connection_id, &state))
314 {
315 return TNC_RESULT_FATAL;
316 }
317 return imv_test->provide_recommendation(imv_test, state);
318 }
319
320 /**
321 * see section 3.8.8 of TCG TNC IF-IMV Specification 1.3
322 */
323 TNC_Result TNC_IMV_BatchEnding(TNC_IMVID imv_id,
324 TNC_ConnectionID connection_id)
325 {
326 if (!imv_test)
327 {
328 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
329 return TNC_RESULT_NOT_INITIALIZED;
330 }
331 return TNC_RESULT_SUCCESS;
332 }
333
334 /**
335 * see section 3.8.9 of TCG TNC IF-IMV Specification 1.3
336 */
337 TNC_Result TNC_IMV_Terminate(TNC_IMVID imv_id)
338 {
339 if (!imv_test)
340 {
341 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
342 return TNC_RESULT_NOT_INITIALIZED;
343 }
344 imv_test->destroy(imv_test);
345 imv_test = NULL;
346
347 return TNC_RESULT_SUCCESS;
348 }
349
350 /**
351 * see section 4.2.8.1 of TCG TNC IF-IMV Specification 1.3
352 */
353 TNC_Result TNC_IMV_ProvideBindFunction(TNC_IMVID imv_id,
354 TNC_TNCS_BindFunctionPointer bind_function)
355 {
356 if (!imv_test)
357 {
358 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
359 return TNC_RESULT_NOT_INITIALIZED;
360 }
361 return imv_test->bind_functions(imv_test, bind_function);
362 }