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