fa8a79c977d56002aa6094ad424ec9615473bade
[strongswan.git] / src / libpts / plugins / imv_swid / imv_swid_state.c
1 /*
2 * Copyright (C) 2013-2014 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "imv_swid_state.h"
17
18 #include <imv/imv_lang_string.h>
19 #include <imv/imv_reason_string.h>
20 #include <imv/imv_remediation_string.h>
21 #include <swid/swid_tag_id.h>
22
23 #include <tncif_policy.h>
24
25 #include <utils/lexparser.h>
26 #include <utils/debug.h>
27
28 typedef struct private_imv_swid_state_t private_imv_swid_state_t;
29
30 /**
31 * Private data of an imv_swid_state_t object.
32 */
33 struct private_imv_swid_state_t {
34
35 /**
36 * Public members of imv_swid_state_t
37 */
38 imv_swid_state_t public;
39
40 /**
41 * TNCCS connection ID
42 */
43 TNC_ConnectionID connection_id;
44
45 /**
46 * TNCCS connection state
47 */
48 TNC_ConnectionState state;
49
50 /**
51 * Does the TNCCS connection support long message types?
52 */
53 bool has_long;
54
55 /**
56 * Does the TNCCS connection support exclusive delivery?
57 */
58 bool has_excl;
59
60 /**
61 * Maximum PA-TNC message size for this TNCCS connection
62 */
63 uint32_t max_msg_len;
64
65 /**
66 * Flags set for completed actions
67 */
68 uint32_t action_flags;
69
70 /**
71 * IMV database session associatied with TNCCS connection
72 */
73 imv_session_t *session;
74
75 /**
76 * IMV action recommendation
77 */
78 TNC_IMV_Action_Recommendation rec;
79
80 /**
81 * IMV evaluation result
82 */
83 TNC_IMV_Evaluation_Result eval;
84
85 /**
86 * IMV Scanner handshake state
87 */
88 imv_swid_handshake_state_t handshake_state;
89
90 /**
91 * TNC Reason String
92 */
93 imv_reason_string_t *reason_string;
94
95 /**
96 * IETF Remediation Instructions String
97 */
98 imv_remediation_string_t *remediation_string;
99
100 /**
101 * SWID Tag Request ID
102 */
103 uint32_t request_id;
104
105 /**
106 * Number of processed SWID Tag IDs
107 */
108 int tag_id_count;
109
110 /**
111 * Number of processed SWID Tags
112 */
113 int tag_count;
114
115 /**
116 * JSON array containing an inventory of SWID Tag IDs
117 */
118 json_object *jarray;
119
120 /**
121 * Angel count
122 */
123 int angel_count;
124
125 };
126
127 METHOD(imv_state_t, get_connection_id, TNC_ConnectionID,
128 private_imv_swid_state_t *this)
129 {
130 return this->connection_id;
131 }
132
133 METHOD(imv_state_t, has_long, bool,
134 private_imv_swid_state_t *this)
135 {
136 return this->has_long;
137 }
138
139 METHOD(imv_state_t, has_excl, bool,
140 private_imv_swid_state_t *this)
141 {
142 return this->has_excl;
143 }
144
145 METHOD(imv_state_t, set_flags, void,
146 private_imv_swid_state_t *this, bool has_long, bool has_excl)
147 {
148 this->has_long = has_long;
149 this->has_excl = has_excl;
150 }
151
152 METHOD(imv_state_t, set_max_msg_len, void,
153 private_imv_swid_state_t *this, uint32_t max_msg_len)
154 {
155 this->max_msg_len = max_msg_len;
156 }
157
158 METHOD(imv_state_t, get_max_msg_len, uint32_t,
159 private_imv_swid_state_t *this)
160 {
161 return this->max_msg_len;
162 }
163
164 METHOD(imv_state_t, set_action_flags, void,
165 private_imv_swid_state_t *this, uint32_t flags)
166 {
167 this->action_flags |= flags;
168 }
169
170 METHOD(imv_state_t, get_action_flags, uint32_t,
171 private_imv_swid_state_t *this)
172 {
173 return this->action_flags;
174 }
175
176 METHOD(imv_state_t, set_session, void,
177 private_imv_swid_state_t *this, imv_session_t *session)
178 {
179 this->session = session;
180 }
181
182 METHOD(imv_state_t, get_session, imv_session_t*,
183 private_imv_swid_state_t *this)
184 {
185 return this->session;
186 }
187
188 METHOD(imv_state_t, change_state, void,
189 private_imv_swid_state_t *this, TNC_ConnectionState new_state)
190 {
191 this->state = new_state;
192 }
193
194 METHOD(imv_state_t, get_recommendation, void,
195 private_imv_swid_state_t *this, TNC_IMV_Action_Recommendation *rec,
196 TNC_IMV_Evaluation_Result *eval)
197 {
198 *rec = this->rec;
199 *eval = this->eval;
200 }
201
202 METHOD(imv_state_t, set_recommendation, void,
203 private_imv_swid_state_t *this, TNC_IMV_Action_Recommendation rec,
204 TNC_IMV_Evaluation_Result eval)
205 {
206 this->rec = rec;
207 this->eval = eval;
208 }
209
210 METHOD(imv_state_t, update_recommendation, void,
211 private_imv_swid_state_t *this, TNC_IMV_Action_Recommendation rec,
212 TNC_IMV_Evaluation_Result eval)
213 {
214 this->rec = tncif_policy_update_recommendation(this->rec, rec);
215 this->eval = tncif_policy_update_evaluation(this->eval, eval);
216 }
217
218 METHOD(imv_state_t, get_reason_string, bool,
219 private_imv_swid_state_t *this, enumerator_t *language_enumerator,
220 chunk_t *reason_string, char **reason_language)
221 {
222 return FALSE;
223 }
224
225 METHOD(imv_state_t, get_remediation_instructions, bool,
226 private_imv_swid_state_t *this, enumerator_t *language_enumerator,
227 chunk_t *string, char **lang_code, char **uri)
228 {
229 return FALSE;
230 }
231
232 METHOD(imv_state_t, destroy, void,
233 private_imv_swid_state_t *this)
234 {
235 json_object_put(this->jarray);
236 DESTROY_IF(this->session);
237 DESTROY_IF(this->reason_string);
238 DESTROY_IF(this->remediation_string);
239 free(this);
240 }
241
242 METHOD(imv_swid_state_t, set_handshake_state, void,
243 private_imv_swid_state_t *this, imv_swid_handshake_state_t new_state)
244 {
245 this->handshake_state = new_state;
246 }
247
248 METHOD(imv_swid_state_t, get_handshake_state, imv_swid_handshake_state_t,
249 private_imv_swid_state_t *this)
250 {
251 return this->handshake_state;
252 }
253
254 METHOD(imv_swid_state_t, set_request_id, void,
255 private_imv_swid_state_t *this, uint32_t request_id)
256 {
257 this->request_id = request_id;
258 }
259
260 METHOD(imv_swid_state_t, get_request_id, uint32_t,
261 private_imv_swid_state_t *this)
262 {
263 return this->request_id;
264 }
265
266 METHOD(imv_swid_state_t, set_swid_inventory, void,
267 private_imv_swid_state_t *this, swid_inventory_t *inventory)
268 {
269 chunk_t tag_creator, unique_sw_id;
270 char software_id[256];
271 json_object *jstring;
272 swid_tag_id_t *tag_id;
273 enumerator_t *enumerator;
274
275 enumerator = inventory->create_enumerator(inventory);
276 while (enumerator->enumerate(enumerator, &tag_id))
277 {
278 /* Construct software ID from tag creator and unique software ID */
279 tag_creator = tag_id->get_tag_creator(tag_id);
280 unique_sw_id = tag_id->get_unique_sw_id(tag_id, NULL);
281 snprintf(software_id, 256, "%.*s_%.*s",
282 tag_creator.len, tag_creator.ptr,
283 unique_sw_id.len, unique_sw_id.ptr);
284 DBG3(DBG_IMV, " %s", software_id);
285
286 /* Add software ID to JSON array */
287 jstring = json_object_new_string(software_id);
288 json_object_array_add(this->jarray, jstring);
289 }
290 enumerator->destroy(enumerator);
291 }
292
293 METHOD(imv_swid_state_t, get_swid_inventory, json_object*,
294 private_imv_swid_state_t *this)
295 {
296 return this->jarray;
297 }
298
299 METHOD(imv_swid_state_t, set_count, void,
300 private_imv_swid_state_t *this, int tag_id_count, int tag_count)
301 {
302 this->tag_id_count += tag_id_count;
303 this->tag_count += tag_count;
304 }
305
306 METHOD(imv_swid_state_t, get_count, void,
307 private_imv_swid_state_t *this, int *tag_id_count, int *tag_count)
308 {
309 if (tag_id_count)
310 {
311 *tag_id_count = this->tag_id_count;
312 }
313 if (tag_count)
314 {
315 *tag_count = this->tag_count;
316 }
317 }
318
319 METHOD(imv_swid_state_t, set_angel_count, void,
320 private_imv_swid_state_t *this, bool start)
321 {
322 this->angel_count += start ? 1 : -1;
323 }
324
325 METHOD(imv_swid_state_t, get_angel_count, int,
326 private_imv_swid_state_t *this)
327 {
328 return this->angel_count;
329 }
330
331 /**
332 * Described in header.
333 */
334 imv_state_t *imv_swid_state_create(TNC_ConnectionID connection_id)
335 {
336 private_imv_swid_state_t *this;
337
338 INIT(this,
339 .public = {
340 .interface = {
341 .get_connection_id = _get_connection_id,
342 .has_long = _has_long,
343 .has_excl = _has_excl,
344 .set_flags = _set_flags,
345 .set_max_msg_len = _set_max_msg_len,
346 .get_max_msg_len = _get_max_msg_len,
347 .set_action_flags = _set_action_flags,
348 .get_action_flags = _get_action_flags,
349 .set_session = _set_session,
350 .get_session= _get_session,
351 .change_state = _change_state,
352 .get_recommendation = _get_recommendation,
353 .set_recommendation = _set_recommendation,
354 .update_recommendation = _update_recommendation,
355 .get_reason_string = _get_reason_string,
356 .get_remediation_instructions = _get_remediation_instructions,
357 .destroy = _destroy,
358 },
359 .set_handshake_state = _set_handshake_state,
360 .get_handshake_state = _get_handshake_state,
361 .set_request_id = _set_request_id,
362 .get_request_id = _get_request_id,
363 .set_swid_inventory = _set_swid_inventory,
364 .get_swid_inventory = _get_swid_inventory,
365 .set_count = _set_count,
366 .get_count = _get_count,
367 .set_angel_count = _set_angel_count,
368 .get_angel_count = _get_angel_count,
369 },
370 .state = TNC_CONNECTION_STATE_CREATE,
371 .rec = TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
372 .eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW,
373 .connection_id = connection_id,
374 .jarray = json_object_new_array(),
375 );
376
377 return &this->public.interface;
378 }
379
380