refactoring of change_state()
[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_command.h>
22
23 #include <tncif_names.h>
24
25 #include <pen/pen.h>
26 #include <debug.h>
27
28 /* IMC definitions */
29
30 static const char imc_name[] = "Test";
31
32 #define IMC_VENDOR_ID PEN_ITA
33 #define IMC_SUBTYPE 0x01
34
35 static imc_agent_t *imc_test;
36
37 /**
38 * see section 3.7.1 of TCG TNC IF-IMC Specification 1.2
39 */
40 TNC_Result TNC_IMC_Initialize(TNC_IMCID imc_id,
41 TNC_Version min_version,
42 TNC_Version max_version,
43 TNC_Version *actual_version)
44 {
45 if (imc_test)
46 {
47 DBG1(DBG_IMC, "IMC \"%s\" has already been initialized", imc_name);
48 return TNC_RESULT_ALREADY_INITIALIZED;
49 }
50 imc_test = imc_agent_create(imc_name, IMC_VENDOR_ID, IMC_SUBTYPE,
51 imc_id, actual_version);
52 if (!imc_test)
53 {
54 return TNC_RESULT_FATAL;
55 }
56 if (min_version > TNC_IFIMC_VERSION_1 || max_version < TNC_IFIMC_VERSION_1)
57 {
58 DBG1(DBG_IMC, "no common IF-IMC version");
59 return TNC_RESULT_NO_COMMON_VERSION;
60 }
61 return TNC_RESULT_SUCCESS;
62 }
63
64 /**
65 * see section 3.7.2 of TCG TNC IF-IMC Specification 1.2
66 */
67 TNC_Result TNC_IMC_NotifyConnectionChange(TNC_IMCID imc_id,
68 TNC_ConnectionID connection_id,
69 TNC_ConnectionState new_state)
70 {
71 imc_state_t *state;
72 imc_test_state_t *test_state;
73 TNC_Result result;
74 char *command;
75 bool retry;
76
77 if (!imc_test)
78 {
79 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
80 return TNC_RESULT_NOT_INITIALIZED;
81 }
82 switch (new_state)
83 {
84 case TNC_CONNECTION_STATE_CREATE:
85 command = lib->settings->get_str(lib->settings,
86 "libimcv.plugins.imc-test.command", "none");
87 retry = lib->settings->get_bool(lib->settings,
88 "libimcv.plugins.imc-test.retry", FALSE);
89 state = imc_test_state_create(connection_id, command, retry);
90 return imc_test->create_state(imc_test, state);
91
92 case TNC_CONNECTION_STATE_HANDSHAKE:
93 /* get updated IMC state */
94 result = imc_test->change_state(imc_test, connection_id,
95 new_state, &state);
96 if (result != TNC_RESULT_SUCCESS)
97 {
98 return TNC_RESULT_FATAL;
99 }
100 test_state = (imc_test_state_t*)state;
101
102 /* is it the first handshake or a retry ? */
103 if (!test_state->is_first_handshake(test_state))
104 {
105 command = lib->settings->get_str(lib->settings,
106 "libimcv.plugins.imc-test.retry_command",
107 test_state->get_command(test_state));
108 test_state->set_command(test_state, command);
109 }
110 return TNC_RESULT_SUCCESS;
111
112 case TNC_CONNECTION_STATE_DELETE:
113 return imc_test->delete_state(imc_test, connection_id);
114
115 case TNC_CONNECTION_STATE_ACCESS_ISOLATED:
116 case TNC_CONNECTION_STATE_ACCESS_NONE:
117 /* get updated IMC state */
118 result = imc_test->change_state(imc_test, connection_id,
119 new_state, &state);
120 if (result != TNC_RESULT_SUCCESS)
121 {
122 return TNC_RESULT_FATAL;
123 }
124 test_state = (imc_test_state_t*)state;
125
126 /* do a handshake retry? */
127 if (test_state->do_handshake_retry(test_state))
128 {
129 return imc_test->request_handshake_retry(imc_id, connection_id,
130 TNC_RETRY_REASON_IMC_REMEDIATION_COMPLETE);
131 }
132 return TNC_RESULT_SUCCESS;
133
134 default:
135 return imc_test->change_state(imc_test, connection_id,
136 new_state, NULL);
137 }
138 }
139
140 static TNC_Result send_message(TNC_ConnectionID connection_id)
141 {
142 pa_tnc_msg_t *msg;
143 pa_tnc_attr_t *attr;
144 imc_state_t *state;
145 imc_test_state_t *test_state;
146 TNC_Result result;
147
148 if (!imc_test->get_state(imc_test, connection_id, &state))
149 {
150 return TNC_RESULT_FATAL;
151 }
152 test_state = (imc_test_state_t*)state;
153 attr = ita_attr_command_create(test_state->get_command(test_state));
154 attr->set_noskip_flag(attr, TRUE);
155 msg = pa_tnc_msg_create();
156 msg->add_attribute(msg, attr);
157 msg->build(msg);
158 result = imc_test->send_message(imc_test, connection_id,
159 msg->get_encoding(msg));
160 msg->destroy(msg);
161
162 return result;
163 }
164
165 /**
166 * see section 3.7.3 of TCG TNC IF-IMC Specification 1.2
167 */
168 TNC_Result TNC_IMC_BeginHandshake(TNC_IMCID imc_id,
169 TNC_ConnectionID connection_id)
170 {
171 if (!imc_test)
172 {
173 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
174 return TNC_RESULT_NOT_INITIALIZED;
175 }
176 return send_message(connection_id);
177 }
178
179 /**
180 * see section 3.7.4 of TCG TNC IF-IMC Specification 1.2
181 */
182 TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
183 TNC_ConnectionID connection_id,
184 TNC_BufferReference msg,
185 TNC_UInt32 msg_len,
186 TNC_MessageType msg_type)
187 {
188 pa_tnc_msg_t *pa_tnc_msg;
189 pa_tnc_attr_t *attr;
190 enumerator_t *enumerator;
191 TNC_Result result;
192 bool fatal_error = FALSE;
193
194 if (!imc_test)
195 {
196 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
197 return TNC_RESULT_NOT_INITIALIZED;
198 }
199
200 /* parse received PA-TNC message and automatically handle any errors */
201 result = imc_test->receive_message(imc_test, connection_id,
202 chunk_create(msg, msg_len), msg_type,
203 &pa_tnc_msg);
204
205 /* no parsed PA-TNC attributes available if an error occurred */
206 if (!pa_tnc_msg)
207 {
208 return result;
209 }
210
211 /* analyze PA-TNC attributes */
212 enumerator = pa_tnc_msg->create_attribute_enumerator(pa_tnc_msg);
213 while (enumerator->enumerate(enumerator, &attr))
214 {
215 if (attr->get_vendor_id(attr) == PEN_IETF &&
216 attr->get_type(attr) == IETF_ATTR_PA_TNC_ERROR)
217 {
218 ietf_attr_pa_tnc_error_t *error_attr;
219 pa_tnc_error_code_t error_code;
220 chunk_t msg_info, attr_info;
221
222 error_attr = (ietf_attr_pa_tnc_error_t*)attr;
223 error_code = error_attr->get_error_code(error_attr);
224 msg_info = error_attr->get_msg_info(error_attr);
225
226 DBG1(DBG_IMC, "received PA-TNC error '%N' concerning message %#B",
227 pa_tnc_error_code_names, error_code, &msg_info);
228 switch (error_code)
229 {
230 case PA_ERROR_ATTR_TYPE_NOT_SUPPORTED:
231 attr_info = error_attr->get_attr_info(error_attr);
232 DBG1(DBG_IMC, " unsupported attribute %#B", &attr_info);
233 break;
234 default:
235 break;
236 }
237 fatal_error = TRUE;
238 }
239 else if (attr->get_vendor_id(attr) == PEN_ITA &&
240 attr->get_type(attr) == ITA_ATTR_COMMAND)
241 {
242 ita_attr_command_t *ita_attr;
243 char *command;
244
245 ita_attr = (ita_attr_command_t*)attr;
246 command = ita_attr->get_command(ita_attr);
247 }
248 }
249 enumerator->destroy(enumerator);
250 pa_tnc_msg->destroy(pa_tnc_msg);
251
252 /* if no error occurred then always return the same response */
253 return fatal_error ? TNC_RESULT_FATAL : send_message(connection_id);
254 }
255
256 /**
257 * see section 3.7.5 of TCG TNC IF-IMC Specification 1.2
258 */
259 TNC_Result TNC_IMC_BatchEnding(TNC_IMCID imc_id,
260 TNC_ConnectionID connection_id)
261 {
262 if (!imc_test)
263 {
264 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
265 return TNC_RESULT_NOT_INITIALIZED;
266 }
267 return TNC_RESULT_SUCCESS;
268 }
269
270 /**
271 * see section 3.7.6 of TCG TNC IF-IMC Specification 1.2
272 */
273 TNC_Result TNC_IMC_Terminate(TNC_IMCID imc_id)
274 {
275 if (!imc_test)
276 {
277 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
278 return TNC_RESULT_NOT_INITIALIZED;
279 }
280 imc_test->destroy(imc_test);
281 imc_test = NULL;
282
283 return TNC_RESULT_SUCCESS;
284 }
285
286 /**
287 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.2
288 */
289 TNC_Result TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id,
290 TNC_TNCC_BindFunctionPointer bind_function)
291 {
292 if (!imc_test)
293 {
294 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
295 return TNC_RESULT_NOT_INITIALIZED;
296 }
297 return imc_test->bind_functions(imc_test, bind_function);
298 }