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