752a66ef5b7696340e8bd88139b36f44dfe05b75
[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 <ita/ita_attr_command.h>
20
21 #include <pen/pen.h>
22
23 #include <debug.h>
24
25 /* IMC definitions */
26
27 static const char imc_name[] = "Test";
28
29 #define IMC_VENDOR_ID PEN_ITA
30 #define IMC_SUBTYPE 0x01
31
32 static imc_agent_t *imc_test;
33
34 /**
35 * see section 3.7.1 of TCG TNC IF-IMC Specification 1.2
36 */
37 TNC_Result TNC_IMC_Initialize(TNC_IMCID imc_id,
38 TNC_Version min_version,
39 TNC_Version max_version,
40 TNC_Version *actual_version)
41 {
42 if (imc_test)
43 {
44 DBG1(DBG_IMC, "IMC \"%s\" has already been initialized", imc_name);
45 return TNC_RESULT_ALREADY_INITIALIZED;
46 }
47 if (min_version > TNC_IFIMC_VERSION_1 || max_version < TNC_IFIMC_VERSION_1)
48 {
49 DBG1(DBG_IMC, "no common IF-IMC version");
50 return TNC_RESULT_NO_COMMON_VERSION;
51 }
52 imc_test = imc_agent_create(imc_name, IMC_VENDOR_ID, IMC_SUBTYPE,
53 imc_id, actual_version);
54 return TNC_RESULT_SUCCESS;
55 }
56
57 /**
58 * see section 3.7.2 of TCG TNC IF-IMC Specification 1.2
59 */
60 TNC_Result TNC_IMC_NotifyConnectionChange(TNC_IMCID imc_id,
61 TNC_ConnectionID connection_id,
62 TNC_ConnectionState new_state)
63 {
64 imc_state_t *state;
65
66 if (!imc_test)
67 {
68 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
69 return TNC_RESULT_NOT_INITIALIZED;
70 }
71 switch (new_state)
72 {
73 case TNC_CONNECTION_STATE_CREATE:
74 state = imc_test_state_create(connection_id);
75 return imc_test->create_state(imc_test, state);
76 case TNC_CONNECTION_STATE_DELETE:
77 return imc_test->delete_state(imc_test, connection_id);
78 default:
79 return imc_test->change_state(imc_test, connection_id, new_state);
80 }
81 }
82
83 static TNC_Result send_message(TNC_ConnectionID connection_id)
84 {
85 pa_tnc_msg_t *msg;
86 pa_tnc_attr_t *attr;
87 char *command;
88 TNC_Result result;
89
90 command = lib->settings->get_str(lib->settings, "imc-test.command", "none");
91 attr = ita_attr_command_create(command);
92 attr->set_noskip_flag(attr, TRUE);
93 msg = pa_tnc_msg_create();
94 msg->add_attribute(msg, attr);
95 msg->build(msg);
96 result = imc_test->send_message(imc_test, connection_id,
97 msg->get_encoding(msg));
98 msg->destroy(msg);
99
100 return result;
101 }
102
103 /**
104 * see section 3.7.3 of TCG TNC IF-IMC Specification 1.2
105 */
106 TNC_Result TNC_IMC_BeginHandshake(TNC_IMCID imc_id,
107 TNC_ConnectionID connection_id)
108 {
109 if (!imc_test)
110 {
111 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
112 return TNC_RESULT_NOT_INITIALIZED;
113 }
114 return send_message(connection_id);
115 }
116
117 /**
118 * see section 3.7.4 of TCG TNC IF-IMC Specification 1.2
119 */
120 TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
121 TNC_ConnectionID connection_id,
122 TNC_BufferReference msg,
123 TNC_UInt32 msg_len,
124 TNC_MessageType msg_type)
125 {
126 pa_tnc_msg_t *pa_tnc_msg;
127 status_t status;
128
129 if (!imc_test)
130 {
131 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
132 return TNC_RESULT_NOT_INITIALIZED;
133 }
134
135 /* process received message */
136 DBG2(DBG_IMC, "IMC %u \"%s\" received message type 0x%08x for Connection ID %u",
137 imc_id, imc_name, msg_type, connection_id);
138 pa_tnc_msg = pa_tnc_msg_create_from_data(chunk_create(msg, msg_len));
139 status = pa_tnc_msg->process(pa_tnc_msg);
140 pa_tnc_msg->destroy(pa_tnc_msg);
141 if (status != SUCCESS)
142 {
143 return TNC_RESULT_FATAL;
144 }
145
146 /* always return the same response */
147 return send_message(connection_id);
148 }
149
150 /**
151 * see section 3.7.5 of TCG TNC IF-IMC Specification 1.2
152 */
153 TNC_Result TNC_IMC_BatchEnding(TNC_IMCID imc_id,
154 TNC_ConnectionID connection_id)
155 {
156 if (!imc_test)
157 {
158 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
159 return TNC_RESULT_NOT_INITIALIZED;
160 }
161 return TNC_RESULT_SUCCESS;
162 }
163
164 /**
165 * see section 3.7.6 of TCG TNC IF-IMC Specification 1.2
166 */
167 TNC_Result TNC_IMC_Terminate(TNC_IMCID imc_id)
168 {
169 if (!imc_test)
170 {
171 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
172 return TNC_RESULT_NOT_INITIALIZED;
173 }
174 imc_test->destroy(imc_test);
175 imc_test = NULL;
176
177 return TNC_RESULT_SUCCESS;
178 }
179
180 /**
181 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.2
182 */
183 TNC_Result TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id,
184 TNC_TNCC_BindFunctionPointer bind_function)
185 {
186 if (!imc_test)
187 {
188 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
189 return TNC_RESULT_NOT_INITIALIZED;
190 }
191 return imc_test->bind_functions(imc_test, bind_function);
192 }