2 * Copyright (C) 2011 Andreas Steffen, HSR Hochschule fuer Technik Rapperswil
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>.
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
15 #include "imc_test_state.h"
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.h>
22 #include <ita/ita_attr_command.h>
24 #include <tncif_names.h>
25 #include <tncif_pa_subtypes.h>
32 static const char imc_name
[] = "Test";
34 #define IMC_VENDOR_ID PEN_ITA
35 #define IMC_SUBTYPE PA_SUBTYPE_ITA_TEST
37 static imc_agent_t
*imc_test
;
40 * see section 3.7.1 of TCG TNC IF-IMC Specification 1.2
42 TNC_Result
TNC_IMC_Initialize(TNC_IMCID imc_id
,
43 TNC_Version min_version
,
44 TNC_Version max_version
,
45 TNC_Version
*actual_version
)
49 DBG1(DBG_IMC
, "IMC \"%s\" has already been initialized", imc_name
);
50 return TNC_RESULT_ALREADY_INITIALIZED
;
52 imc_test
= imc_agent_create(imc_name
, IMC_VENDOR_ID
, IMC_SUBTYPE
,
53 imc_id
, actual_version
);
56 return TNC_RESULT_FATAL
;
58 if (min_version
> TNC_IFIMC_VERSION_1
|| max_version
< TNC_IFIMC_VERSION_1
)
60 DBG1(DBG_IMC
, "no common IF-IMC version");
61 return TNC_RESULT_NO_COMMON_VERSION
;
63 return TNC_RESULT_SUCCESS
;
67 * see section 3.7.2 of TCG TNC IF-IMC Specification 1.2
69 TNC_Result
TNC_IMC_NotifyConnectionChange(TNC_IMCID imc_id
,
70 TNC_ConnectionID connection_id
,
71 TNC_ConnectionState new_state
)
74 imc_test_state_t
*test_state
;
81 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
82 return TNC_RESULT_NOT_INITIALIZED
;
86 case TNC_CONNECTION_STATE_CREATE
:
87 command
= lib
->settings
->get_str(lib
->settings
,
88 "libimcv.plugins.imc-test.command", "none");
89 retry
= lib
->settings
->get_bool(lib
->settings
,
90 "libimcv.plugins.imc-test.retry", FALSE
);
91 state
= imc_test_state_create(connection_id
, command
, retry
);
92 return imc_test
->create_state(imc_test
, state
);
94 case TNC_CONNECTION_STATE_HANDSHAKE
:
95 /* get updated IMC state */
96 result
= imc_test
->change_state(imc_test
, connection_id
,
98 if (result
!= TNC_RESULT_SUCCESS
)
100 return TNC_RESULT_FATAL
;
102 test_state
= (imc_test_state_t
*)state
;
104 /* is it the first handshake or a retry ? */
105 if (!test_state
->is_first_handshake(test_state
))
107 command
= lib
->settings
->get_str(lib
->settings
,
108 "libimcv.plugins.imc-test.retry_command",
109 test_state
->get_command(test_state
));
110 test_state
->set_command(test_state
, command
);
112 return TNC_RESULT_SUCCESS
;
114 case TNC_CONNECTION_STATE_DELETE
:
115 return imc_test
->delete_state(imc_test
, connection_id
);
117 case TNC_CONNECTION_STATE_ACCESS_ISOLATED
:
118 case TNC_CONNECTION_STATE_ACCESS_NONE
:
119 /* get updated IMC state */
120 result
= imc_test
->change_state(imc_test
, connection_id
,
122 if (result
!= TNC_RESULT_SUCCESS
)
124 return TNC_RESULT_FATAL
;
126 test_state
= (imc_test_state_t
*)state
;
128 /* do a handshake retry? */
129 if (test_state
->do_handshake_retry(test_state
))
131 return imc_test
->request_handshake_retry(imc_id
, connection_id
,
132 TNC_RETRY_REASON_IMC_REMEDIATION_COMPLETE
);
134 return TNC_RESULT_SUCCESS
;
137 return imc_test
->change_state(imc_test
, connection_id
,
142 static TNC_Result
send_message(TNC_ConnectionID connection_id
)
147 imc_test_state_t
*test_state
;
150 if (!imc_test
->get_state(imc_test
, connection_id
, &state
))
152 return TNC_RESULT_FATAL
;
154 test_state
= (imc_test_state_t
*)state
;
155 attr
= ita_attr_command_create(test_state
->get_command(test_state
));
156 attr
->set_noskip_flag(attr
, TRUE
);
157 msg
= pa_tnc_msg_create();
158 msg
->add_attribute(msg
, attr
);
160 result
= imc_test
->send_message(imc_test
, connection_id
,
161 msg
->get_encoding(msg
));
168 * see section 3.7.3 of TCG TNC IF-IMC Specification 1.2
170 TNC_Result
TNC_IMC_BeginHandshake(TNC_IMCID imc_id
,
171 TNC_ConnectionID connection_id
)
175 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
176 return TNC_RESULT_NOT_INITIALIZED
;
178 return send_message(connection_id
);
182 * see section 3.7.4 of TCG TNC IF-IMC Specification 1.2
184 TNC_Result
TNC_IMC_ReceiveMessage(TNC_IMCID imc_id
,
185 TNC_ConnectionID connection_id
,
186 TNC_BufferReference msg
,
188 TNC_MessageType msg_type
)
190 pa_tnc_msg_t
*pa_tnc_msg
;
192 enumerator_t
*enumerator
;
194 bool fatal_error
= FALSE
;
198 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
199 return TNC_RESULT_NOT_INITIALIZED
;
202 /* parse received PA-TNC message and automatically handle any errors */
203 result
= imc_test
->receive_message(imc_test
, connection_id
,
204 chunk_create(msg
, msg_len
), msg_type
,
207 /* no parsed PA-TNC attributes available if an error occurred */
213 /* analyze PA-TNC attributes */
214 enumerator
= pa_tnc_msg
->create_attribute_enumerator(pa_tnc_msg
);
215 while (enumerator
->enumerate(enumerator
, &attr
))
217 if (attr
->get_vendor_id(attr
) == PEN_IETF
&&
218 attr
->get_type(attr
) == IETF_ATTR_PA_TNC_ERROR
)
220 ietf_attr_pa_tnc_error_t
*error_attr
;
221 pa_tnc_error_code_t error_code
;
222 chunk_t msg_info
, attr_info
;
225 error_attr
= (ietf_attr_pa_tnc_error_t
*)attr
;
226 error_code
= error_attr
->get_error_code(error_attr
);
227 msg_info
= error_attr
->get_msg_info(error_attr
);
229 DBG1(DBG_IMC
, "received PA-TNC error '%N' concerning message %#B",
230 pa_tnc_error_code_names
, error_code
, &msg_info
);
233 case PA_ERROR_INVALID_PARAMETER
:
234 offset
= error_attr
->get_offset(error_attr
);
235 DBG1(DBG_IMC
, " occurred at offset of %u bytes", offset
);
237 case PA_ERROR_ATTR_TYPE_NOT_SUPPORTED
:
238 attr_info
= error_attr
->get_attr_info(error_attr
);
239 DBG1(DBG_IMC
, " unsupported attribute %#B", &attr_info
);
246 else if (attr
->get_vendor_id(attr
) == PEN_ITA
&&
247 attr
->get_type(attr
) == ITA_ATTR_COMMAND
)
249 ita_attr_command_t
*ita_attr
;
252 ita_attr
= (ita_attr_command_t
*)attr
;
253 command
= ita_attr
->get_command(ita_attr
);
256 enumerator
->destroy(enumerator
);
257 pa_tnc_msg
->destroy(pa_tnc_msg
);
259 /* if no error occurred then always return the same response */
260 return fatal_error ? TNC_RESULT_FATAL
: send_message(connection_id
);
264 * see section 3.7.5 of TCG TNC IF-IMC Specification 1.2
266 TNC_Result
TNC_IMC_BatchEnding(TNC_IMCID imc_id
,
267 TNC_ConnectionID connection_id
)
271 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
272 return TNC_RESULT_NOT_INITIALIZED
;
274 return TNC_RESULT_SUCCESS
;
278 * see section 3.7.6 of TCG TNC IF-IMC Specification 1.2
280 TNC_Result
TNC_IMC_Terminate(TNC_IMCID imc_id
)
284 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
285 return TNC_RESULT_NOT_INITIALIZED
;
287 imc_test
->destroy(imc_test
);
290 return TNC_RESULT_SUCCESS
;
294 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.2
296 TNC_Result
TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id
,
297 TNC_TNCC_BindFunctionPointer bind_function
)
301 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
302 return TNC_RESULT_NOT_INITIALIZED
;
304 return imc_test
->bind_functions(imc_test
, bind_function
);