initialize libstrongswan in dynamic stand-alone libimcv-based libraries
[strongswan.git] / src / libimcv / plugins / imv_test / imv_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 "imv_test_state.h"
16
17 #include <imv/imv_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 /* IMV definitions */
26
27 static const char imv_name[] = "Test";
28
29 #define IMV_VENDOR_ID PEN_ITA
30 #define IMV_SUBTYPE 0x01
31
32 static imv_agent_t *imv_test;
33
34 /**
35 * see section 3.7.1 of TCG TNC IF-IMV Specification 1.2
36 */
37 TNC_Result TNC_IMV_Initialize(TNC_IMVID imv_id,
38 TNC_Version min_version,
39 TNC_Version max_version,
40 TNC_Version *actual_version)
41 {
42 if (imv_test)
43 {
44 DBG1(DBG_IMV, "IMV \"%s\" has already been initialized", imv_name);
45 return TNC_RESULT_ALREADY_INITIALIZED;
46 }
47 imv_test = imv_agent_create(imv_name, IMV_VENDOR_ID, IMV_SUBTYPE,
48 imv_id, actual_version);
49 if (!imv_test)
50 {
51 return TNC_RESULT_FATAL;
52 }
53 if (min_version > TNC_IFIMV_VERSION_1 || max_version < TNC_IFIMV_VERSION_1)
54 {
55 DBG1(DBG_IMV, "no common IF-IMV version");
56 return TNC_RESULT_NO_COMMON_VERSION;
57 }
58 return TNC_RESULT_SUCCESS;
59 }
60
61 /**
62 * see section 3.7.2 of TCG TNC IF-IMV Specification 1.2
63 */
64 TNC_Result TNC_IMV_NotifyConnectionChange(TNC_IMVID imv_id,
65 TNC_ConnectionID connection_id,
66 TNC_ConnectionState new_state)
67 {
68 imv_state_t *state;
69 int rounds;
70
71 if (!imv_test)
72 {
73 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
74 return TNC_RESULT_NOT_INITIALIZED;
75 }
76 switch (new_state)
77 {
78 case TNC_CONNECTION_STATE_CREATE:
79 rounds = lib->settings->get_int(lib->settings, "imv-test.rounds", 0);
80 state = imv_test_state_create(connection_id, rounds);
81 return imv_test->create_state(imv_test, state);
82 case TNC_CONNECTION_STATE_DELETE:
83 return imv_test->delete_state(imv_test, connection_id);
84 default:
85 return imv_test->change_state(imv_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 TNC_Result result;
94
95 attr = ita_attr_command_create("repeat");
96 msg = pa_tnc_msg_create();
97 msg->add_attribute(msg, attr);
98 msg->build(msg);
99 result = imv_test->send_message(imv_test, connection_id,
100 msg->get_encoding(msg));
101 msg->destroy(msg);
102
103 return result;
104 }
105
106 /**
107 * see section 3.7.3 of TCG TNC IF-IMV Specification 1.2
108 */
109 TNC_Result TNC_IMV_ReceiveMessage(TNC_IMVID imv_id,
110 TNC_ConnectionID connection_id,
111 TNC_BufferReference msg,
112 TNC_UInt32 msg_len,
113 TNC_MessageType msg_type)
114 {
115 pa_tnc_msg_t *pa_tnc_msg;
116 pa_tnc_attr_t *attr;
117 imv_state_t *state;
118 imv_test_state_t *imv_test_state;
119 TNC_Result result = TNC_RESULT_SUCCESS;
120 enumerator_t *enumerator;
121
122 if (!imv_test)
123 {
124 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
125 return TNC_RESULT_NOT_INITIALIZED;
126 }
127
128 /* process received message */
129 DBG2(DBG_IMV, "IMV %u \"%s\" received message type 0x%08x for Connection ID %u",
130 imv_id, imv_name, msg_type, connection_id);
131 pa_tnc_msg = pa_tnc_msg_create_from_data(chunk_create(msg, msg_len));
132
133 if (pa_tnc_msg->process(pa_tnc_msg) != SUCCESS)
134 {
135 pa_tnc_msg->destroy(pa_tnc_msg);
136 return TNC_RESULT_FATAL;
137 }
138
139 /* get current IMV state */
140 if (!imv_test->get_state(imv_test, connection_id, &state))
141 {
142 pa_tnc_msg->destroy(pa_tnc_msg);
143 return TNC_RESULT_FATAL;
144 }
145
146 enumerator = pa_tnc_msg->create_attribute_enumerator(pa_tnc_msg);
147 while (enumerator->enumerate(enumerator, &attr))
148 {
149 if (attr->get_vendor_id(attr) == PEN_ITA &&
150 attr->get_type(attr) == ITA_ATTR_COMMAND)
151 {
152 ita_attr_command_t *ita_attr;
153 char *command;
154
155 ita_attr = (ita_attr_command_t*)attr;
156 command = ita_attr->get_command(ita_attr);
157
158 if (streq(command, "allow"))
159 {
160 state->set_recommendation(state,
161 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
162 TNC_IMV_EVALUATION_RESULT_COMPLIANT);
163 }
164 else if (streq(command, "isolate"))
165 {
166 state->set_recommendation(state,
167 TNC_IMV_ACTION_RECOMMENDATION_ISOLATE,
168 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MINOR);
169 }
170 else if (streq(command, "none"))
171 {
172 state->set_recommendation(state,
173 TNC_IMV_ACTION_RECOMMENDATION_NO_ACCESS,
174 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MAJOR);
175 }
176 else
177 {
178 result = TNC_RESULT_FATAL;
179 }
180 break;
181 }
182 }
183 enumerator->destroy(enumerator);
184 pa_tnc_msg->destroy(pa_tnc_msg);
185
186 if (result != TNC_RESULT_SUCCESS)
187 {
188 return result;
189 }
190
191 /* repeat the measurement ? */
192 imv_test_state = (imv_test_state_t*)state;
193 if (imv_test_state->another_round(imv_test_state))
194 {
195 return send_message(connection_id);
196 }
197
198 return imv_test->provide_recommendation(imv_test, connection_id);
199 }
200
201 /**
202 * see section 3.7.4 of TCG TNC IF-IMV Specification 1.2
203 */
204 TNC_Result TNC_IMV_SolicitRecommendation(TNC_IMVID imv_id,
205 TNC_ConnectionID connection_id)
206 {
207 if (!imv_test)
208 {
209 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
210 return TNC_RESULT_NOT_INITIALIZED;
211 }
212 return imv_test->provide_recommendation(imv_test, connection_id);
213 }
214
215 /**
216 * see section 3.7.5 of TCG TNC IF-IMV Specification 1.2
217 */
218 TNC_Result TNC_IMV_BatchEnding(TNC_IMVID imv_id,
219 TNC_ConnectionID connection_id)
220 {
221 if (!imv_test)
222 {
223 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
224 return TNC_RESULT_NOT_INITIALIZED;
225 }
226 return TNC_RESULT_SUCCESS;
227 }
228
229 /**
230 * see section 3.7.6 of TCG TNC IF-IMV Specification 1.2
231 */
232 TNC_Result TNC_IMV_Terminate(TNC_IMVID imv_id)
233 {
234 if (!imv_test)
235 {
236 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
237 return TNC_RESULT_NOT_INITIALIZED;
238 }
239 imv_test->destroy(imv_test);
240 imv_test = NULL;
241
242 return TNC_RESULT_SUCCESS;
243 }
244
245 /**
246 * see section 4.2.8.1 of TCG TNC IF-IMV Specification 1.2
247 */
248 TNC_Result TNC_IMV_ProvideBindFunction(TNC_IMVID imv_id,
249 TNC_TNCS_BindFunctionPointer bind_function)
250 {
251 if (!imv_test)
252 {
253 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
254 return TNC_RESULT_NOT_INITIALIZED;
255 }
256 return imv_test->bind_functions(imv_test, bind_function);
257 }