e89008f87c0815e32ee84623713ea30f37dcb7d7
[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
72 if (!imc_test)
73 {
74 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
75 return TNC_RESULT_NOT_INITIALIZED;
76 }
77 switch (new_state)
78 {
79 case TNC_CONNECTION_STATE_CREATE:
80 state = imc_test_state_create(connection_id);
81 return imc_test->create_state(imc_test, state);
82 case TNC_CONNECTION_STATE_DELETE:
83 return imc_test->delete_state(imc_test, connection_id);
84 default:
85 return imc_test->change_state(imc_test, connection_id, new_state);
86 }
87 }
88
89 static TNC_Result send_message(TNC_ConnectionID connection_id)
90 {
91 pa_tnc_msg_t *msg;
92 pa_tnc_attr_t *attr;
93 char *command;
94 TNC_Result result;
95
96 command = lib->settings->get_str(lib->settings,
97 "libimcv.plugins.imc-test.command", "none");
98 attr = ita_attr_command_create(command);
99 attr->set_noskip_flag(attr, TRUE);
100 msg = pa_tnc_msg_create();
101 msg->add_attribute(msg, attr);
102 msg->build(msg);
103 result = imc_test->send_message(imc_test, connection_id,
104 msg->get_encoding(msg));
105 msg->destroy(msg);
106
107 return result;
108 }
109
110 /**
111 * see section 3.7.3 of TCG TNC IF-IMC Specification 1.2
112 */
113 TNC_Result TNC_IMC_BeginHandshake(TNC_IMCID imc_id,
114 TNC_ConnectionID connection_id)
115 {
116 if (!imc_test)
117 {
118 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
119 return TNC_RESULT_NOT_INITIALIZED;
120 }
121 return send_message(connection_id);
122 }
123
124 /**
125 * see section 3.7.4 of TCG TNC IF-IMC Specification 1.2
126 */
127 TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
128 TNC_ConnectionID connection_id,
129 TNC_BufferReference msg,
130 TNC_UInt32 msg_len,
131 TNC_MessageType msg_type)
132 {
133 pa_tnc_msg_t *pa_tnc_msg;
134 pa_tnc_attr_t *attr;
135 enumerator_t *enumerator;
136 TNC_Result result;
137 bool fatal_error = FALSE;
138
139 if (!imc_test)
140 {
141 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
142 return TNC_RESULT_NOT_INITIALIZED;
143 }
144
145 /* parse received PA-TNC message and automatically handle any errors */
146 result = imc_test->receive_message(imc_test, connection_id,
147 chunk_create(msg, msg_len), msg_type,
148 &pa_tnc_msg);
149
150 /* no parsed PA-TNC attributes available if an error occurred */
151 if (!pa_tnc_msg)
152 {
153 return result;
154 }
155
156 /* analyze PA-TNC attributes */
157 enumerator = pa_tnc_msg->create_attribute_enumerator(pa_tnc_msg);
158 while (enumerator->enumerate(enumerator, &attr))
159 {
160 if (attr->get_vendor_id(attr) == PEN_IETF &&
161 attr->get_type(attr) == IETF_ATTR_PA_TNC_ERROR)
162 {
163 ietf_attr_pa_tnc_error_t *error_attr;
164 pa_tnc_error_code_t error_code;
165 chunk_t msg_info, attr_info;
166
167 error_attr = (ietf_attr_pa_tnc_error_t*)attr;
168 error_code = error_attr->get_error_code(error_attr);
169 msg_info = error_attr->get_msg_info(error_attr);
170
171 DBG1(DBG_IMC, "received PA-TNC error '%N' concerning message %#B",
172 pa_tnc_error_code_names, error_code, &msg_info);
173 switch (error_code)
174 {
175 case PA_ERROR_ATTR_TYPE_NOT_SUPPORTED:
176 attr_info = error_attr->get_attr_info(error_attr);
177 DBG1(DBG_IMC, " unsupported attribute %#B", &attr_info);
178 break;
179 default:
180 break;
181 }
182 fatal_error = TRUE;
183 }
184 else if (attr->get_vendor_id(attr) == PEN_ITA &&
185 attr->get_type(attr) == ITA_ATTR_COMMAND)
186 {
187 ita_attr_command_t *ita_attr;
188 char *command;
189
190 ita_attr = (ita_attr_command_t*)attr;
191 command = ita_attr->get_command(ita_attr);
192 }
193 }
194 enumerator->destroy(enumerator);
195 pa_tnc_msg->destroy(pa_tnc_msg);
196
197 /* if no error occurred then always return the same response */
198 return fatal_error ? TNC_RESULT_FATAL : send_message(connection_id);
199 }
200
201 /**
202 * see section 3.7.5 of TCG TNC IF-IMC Specification 1.2
203 */
204 TNC_Result TNC_IMC_BatchEnding(TNC_IMCID imc_id,
205 TNC_ConnectionID connection_id)
206 {
207 if (!imc_test)
208 {
209 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
210 return TNC_RESULT_NOT_INITIALIZED;
211 }
212 return TNC_RESULT_SUCCESS;
213 }
214
215 /**
216 * see section 3.7.6 of TCG TNC IF-IMC Specification 1.2
217 */
218 TNC_Result TNC_IMC_Terminate(TNC_IMCID imc_id)
219 {
220 if (!imc_test)
221 {
222 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
223 return TNC_RESULT_NOT_INITIALIZED;
224 }
225 imc_test->destroy(imc_test);
226 imc_test = NULL;
227
228 return TNC_RESULT_SUCCESS;
229 }
230
231 /**
232 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.2
233 */
234 TNC_Result TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id,
235 TNC_TNCC_BindFunctionPointer bind_function)
236 {
237 if (!imc_test)
238 {
239 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
240 return TNC_RESULT_NOT_INITIALIZED;
241 }
242 return imc_test->bind_functions(imc_test, bind_function);
243 }