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_command.h>
23 #include <tncif_names.h>
24 #include <tncif_pa_subtypes.h>
31 static const char imc_name
[] = "Test";
33 #define IMC_VENDOR_ID PEN_ITA
34 #define IMC_SUBTYPE PA_SUBTYPE_ITA_TEST
36 static imc_agent_t
*imc_test
;
39 * see section 3.7.1 of TCG TNC IF-IMC Specification 1.2
41 TNC_Result
TNC_IMC_Initialize(TNC_IMCID imc_id
,
42 TNC_Version min_version
,
43 TNC_Version max_version
,
44 TNC_Version
*actual_version
)
48 DBG1(DBG_IMC
, "IMC \"%s\" has already been initialized", imc_name
);
49 return TNC_RESULT_ALREADY_INITIALIZED
;
51 imc_test
= imc_agent_create(imc_name
, IMC_VENDOR_ID
, IMC_SUBTYPE
,
52 imc_id
, actual_version
);
55 return TNC_RESULT_FATAL
;
57 if (min_version
> TNC_IFIMC_VERSION_1
|| max_version
< TNC_IFIMC_VERSION_1
)
59 DBG1(DBG_IMC
, "no common IF-IMC version");
60 return TNC_RESULT_NO_COMMON_VERSION
;
62 return TNC_RESULT_SUCCESS
;
66 * see section 3.7.2 of TCG TNC IF-IMC Specification 1.2
68 TNC_Result
TNC_IMC_NotifyConnectionChange(TNC_IMCID imc_id
,
69 TNC_ConnectionID connection_id
,
70 TNC_ConnectionState new_state
)
73 imc_test_state_t
*test_state
;
80 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
81 return TNC_RESULT_NOT_INITIALIZED
;
85 case TNC_CONNECTION_STATE_CREATE
:
86 command
= lib
->settings
->get_str(lib
->settings
,
87 "libimcv.plugins.imc-test.command", "none");
88 retry
= lib
->settings
->get_bool(lib
->settings
,
89 "libimcv.plugins.imc-test.retry", FALSE
);
90 state
= imc_test_state_create(connection_id
, command
, retry
);
91 return imc_test
->create_state(imc_test
, state
);
93 case TNC_CONNECTION_STATE_HANDSHAKE
:
94 /* get updated IMC state */
95 result
= imc_test
->change_state(imc_test
, connection_id
,
97 if (result
!= TNC_RESULT_SUCCESS
)
99 return TNC_RESULT_FATAL
;
101 test_state
= (imc_test_state_t
*)state
;
103 /* is it the first handshake or a retry ? */
104 if (!test_state
->is_first_handshake(test_state
))
106 command
= lib
->settings
->get_str(lib
->settings
,
107 "libimcv.plugins.imc-test.retry_command",
108 test_state
->get_command(test_state
));
109 test_state
->set_command(test_state
, command
);
111 return TNC_RESULT_SUCCESS
;
113 case TNC_CONNECTION_STATE_DELETE
:
114 return imc_test
->delete_state(imc_test
, connection_id
);
116 case TNC_CONNECTION_STATE_ACCESS_ISOLATED
:
117 case TNC_CONNECTION_STATE_ACCESS_NONE
:
118 /* get updated IMC state */
119 result
= imc_test
->change_state(imc_test
, connection_id
,
121 if (result
!= TNC_RESULT_SUCCESS
)
123 return TNC_RESULT_FATAL
;
125 test_state
= (imc_test_state_t
*)state
;
127 /* do a handshake retry? */
128 if (test_state
->do_handshake_retry(test_state
))
130 return imc_test
->request_handshake_retry(imc_id
, connection_id
,
131 TNC_RETRY_REASON_IMC_REMEDIATION_COMPLETE
);
133 return TNC_RESULT_SUCCESS
;
136 return imc_test
->change_state(imc_test
, connection_id
,
141 static TNC_Result
send_message(TNC_ConnectionID connection_id
)
146 imc_test_state_t
*test_state
;
149 if (!imc_test
->get_state(imc_test
, connection_id
, &state
))
151 return TNC_RESULT_FATAL
;
153 test_state
= (imc_test_state_t
*)state
;
154 attr
= ita_attr_command_create(test_state
->get_command(test_state
));
155 attr
->set_noskip_flag(attr
, TRUE
);
156 msg
= pa_tnc_msg_create();
157 msg
->add_attribute(msg
, attr
);
159 result
= imc_test
->send_message(imc_test
, connection_id
,
160 msg
->get_encoding(msg
));
167 * see section 3.7.3 of TCG TNC IF-IMC Specification 1.2
169 TNC_Result
TNC_IMC_BeginHandshake(TNC_IMCID imc_id
,
170 TNC_ConnectionID connection_id
)
174 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
175 return TNC_RESULT_NOT_INITIALIZED
;
177 return send_message(connection_id
);
181 * see section 3.7.4 of TCG TNC IF-IMC Specification 1.2
183 TNC_Result
TNC_IMC_ReceiveMessage(TNC_IMCID imc_id
,
184 TNC_ConnectionID connection_id
,
185 TNC_BufferReference msg
,
187 TNC_MessageType msg_type
)
189 pa_tnc_msg_t
*pa_tnc_msg
;
191 enumerator_t
*enumerator
;
193 bool fatal_error
= FALSE
;
197 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
198 return TNC_RESULT_NOT_INITIALIZED
;
201 /* parse received PA-TNC message and automatically handle any errors */
202 result
= imc_test
->receive_message(imc_test
, connection_id
,
203 chunk_create(msg
, msg_len
), msg_type
,
206 /* no parsed PA-TNC attributes available if an error occurred */
212 /* analyze PA-TNC attributes */
213 enumerator
= pa_tnc_msg
->create_attribute_enumerator(pa_tnc_msg
);
214 while (enumerator
->enumerate(enumerator
, &attr
))
216 if (attr
->get_vendor_id(attr
) == PEN_IETF
&&
217 attr
->get_type(attr
) == IETF_ATTR_PA_TNC_ERROR
)
219 ietf_attr_pa_tnc_error_t
*error_attr
;
220 pa_tnc_error_code_t error_code
;
221 chunk_t msg_info
, attr_info
;
224 error_attr
= (ietf_attr_pa_tnc_error_t
*)attr
;
225 error_code
= error_attr
->get_error_code(error_attr
);
226 msg_info
= error_attr
->get_msg_info(error_attr
);
228 DBG1(DBG_IMC
, "received PA-TNC error '%N' concerning message %#B",
229 pa_tnc_error_code_names
, error_code
, &msg_info
);
232 case PA_ERROR_INVALID_PARAMETER
:
233 offset
= error_attr
->get_offset(error_attr
);
234 DBG1(DBG_IMC
, " occurred at offset of %u bytes", offset
);
236 case PA_ERROR_ATTR_TYPE_NOT_SUPPORTED
:
237 attr_info
= error_attr
->get_attr_info(error_attr
);
238 DBG1(DBG_IMC
, " unsupported attribute %#B", &attr_info
);
245 else if (attr
->get_vendor_id(attr
) == PEN_ITA
&&
246 attr
->get_type(attr
) == ITA_ATTR_COMMAND
)
248 ita_attr_command_t
*ita_attr
;
251 ita_attr
= (ita_attr_command_t
*)attr
;
252 command
= ita_attr
->get_command(ita_attr
);
255 enumerator
->destroy(enumerator
);
256 pa_tnc_msg
->destroy(pa_tnc_msg
);
258 /* if no error occurred then always return the same response */
259 return fatal_error ? TNC_RESULT_FATAL
: send_message(connection_id
);
263 * see section 3.7.5 of TCG TNC IF-IMC Specification 1.2
265 TNC_Result
TNC_IMC_BatchEnding(TNC_IMCID imc_id
,
266 TNC_ConnectionID connection_id
)
270 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
271 return TNC_RESULT_NOT_INITIALIZED
;
273 return TNC_RESULT_SUCCESS
;
277 * see section 3.7.6 of TCG TNC IF-IMC Specification 1.2
279 TNC_Result
TNC_IMC_Terminate(TNC_IMCID imc_id
)
283 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
284 return TNC_RESULT_NOT_INITIALIZED
;
286 imc_test
->destroy(imc_test
);
289 return TNC_RESULT_SUCCESS
;
293 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.2
295 TNC_Result
TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id
,
296 TNC_TNCC_BindFunctionPointer bind_function
)
300 DBG1(DBG_IMC
, "IMC \"%s\" has not been initialized", imc_name
);
301 return TNC_RESULT_NOT_INITIALIZED
;
303 return imc_test
->bind_functions(imc_test
, bind_function
);