Updated REST API
[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 * Top level JSON object
117 */
118 json_object *jobj;
119
120 /**
121 * JSON array containing an inventory of SWID Tag IDs
122 */
123 json_object *jarray;
124
125 /**
126 * Angel count
127 */
128 int angel_count;
129
130 };
131
132 METHOD(imv_state_t, get_connection_id, TNC_ConnectionID,
133 private_imv_swid_state_t *this)
134 {
135 return this->connection_id;
136 }
137
138 METHOD(imv_state_t, has_long, bool,
139 private_imv_swid_state_t *this)
140 {
141 return this->has_long;
142 }
143
144 METHOD(imv_state_t, has_excl, bool,
145 private_imv_swid_state_t *this)
146 {
147 return this->has_excl;
148 }
149
150 METHOD(imv_state_t, set_flags, void,
151 private_imv_swid_state_t *this, bool has_long, bool has_excl)
152 {
153 this->has_long = has_long;
154 this->has_excl = has_excl;
155 }
156
157 METHOD(imv_state_t, set_max_msg_len, void,
158 private_imv_swid_state_t *this, uint32_t max_msg_len)
159 {
160 this->max_msg_len = max_msg_len;
161 }
162
163 METHOD(imv_state_t, get_max_msg_len, uint32_t,
164 private_imv_swid_state_t *this)
165 {
166 return this->max_msg_len;
167 }
168
169 METHOD(imv_state_t, set_action_flags, void,
170 private_imv_swid_state_t *this, uint32_t flags)
171 {
172 this->action_flags |= flags;
173 }
174
175 METHOD(imv_state_t, get_action_flags, uint32_t,
176 private_imv_swid_state_t *this)
177 {
178 return this->action_flags;
179 }
180
181 METHOD(imv_state_t, set_session, void,
182 private_imv_swid_state_t *this, imv_session_t *session)
183 {
184 this->session = session;
185 }
186
187 METHOD(imv_state_t, get_session, imv_session_t*,
188 private_imv_swid_state_t *this)
189 {
190 return this->session;
191 }
192
193 METHOD(imv_state_t, change_state, void,
194 private_imv_swid_state_t *this, TNC_ConnectionState new_state)
195 {
196 this->state = new_state;
197 }
198
199 METHOD(imv_state_t, get_recommendation, void,
200 private_imv_swid_state_t *this, TNC_IMV_Action_Recommendation *rec,
201 TNC_IMV_Evaluation_Result *eval)
202 {
203 *rec = this->rec;
204 *eval = this->eval;
205 }
206
207 METHOD(imv_state_t, set_recommendation, void,
208 private_imv_swid_state_t *this, TNC_IMV_Action_Recommendation rec,
209 TNC_IMV_Evaluation_Result eval)
210 {
211 this->rec = rec;
212 this->eval = eval;
213 }
214
215 METHOD(imv_state_t, update_recommendation, void,
216 private_imv_swid_state_t *this, TNC_IMV_Action_Recommendation rec,
217 TNC_IMV_Evaluation_Result eval)
218 {
219 this->rec = tncif_policy_update_recommendation(this->rec, rec);
220 this->eval = tncif_policy_update_evaluation(this->eval, eval);
221 }
222
223 METHOD(imv_state_t, get_reason_string, bool,
224 private_imv_swid_state_t *this, enumerator_t *language_enumerator,
225 chunk_t *reason_string, char **reason_language)
226 {
227 return FALSE;
228 }
229
230 METHOD(imv_state_t, get_remediation_instructions, bool,
231 private_imv_swid_state_t *this, enumerator_t *language_enumerator,
232 chunk_t *string, char **lang_code, char **uri)
233 {
234 return FALSE;
235 }
236
237 METHOD(imv_state_t, destroy, void,
238 private_imv_swid_state_t *this)
239 {
240 json_object_put(this->jobj);
241 DESTROY_IF(this->session);
242 DESTROY_IF(this->reason_string);
243 DESTROY_IF(this->remediation_string);
244 free(this);
245 }
246
247 METHOD(imv_swid_state_t, set_handshake_state, void,
248 private_imv_swid_state_t *this, imv_swid_handshake_state_t new_state)
249 {
250 this->handshake_state = new_state;
251 }
252
253 METHOD(imv_swid_state_t, get_handshake_state, imv_swid_handshake_state_t,
254 private_imv_swid_state_t *this)
255 {
256 return this->handshake_state;
257 }
258
259 METHOD(imv_swid_state_t, set_request_id, void,
260 private_imv_swid_state_t *this, uint32_t request_id)
261 {
262 this->request_id = request_id;
263 }
264
265 METHOD(imv_swid_state_t, get_request_id, uint32_t,
266 private_imv_swid_state_t *this)
267 {
268 return this->request_id;
269 }
270
271 METHOD(imv_swid_state_t, set_swid_inventory, void,
272 private_imv_swid_state_t *this, swid_inventory_t *inventory)
273 {
274 chunk_t tag_creator, unique_sw_id;
275 char software_id[256];
276 json_object *jstring;
277 swid_tag_id_t *tag_id;
278 enumerator_t *enumerator;
279
280 enumerator = inventory->create_enumerator(inventory);
281 while (enumerator->enumerate(enumerator, &tag_id))
282 {
283 /* Construct software ID from tag creator and unique software ID */
284 tag_creator = tag_id->get_tag_creator(tag_id);
285 unique_sw_id = tag_id->get_unique_sw_id(tag_id, NULL);
286 snprintf(software_id, 256, "%.*s_%.*s",
287 tag_creator.len, tag_creator.ptr,
288 unique_sw_id.len, unique_sw_id.ptr);
289 DBG3(DBG_IMV, " %s", software_id);
290
291 /* Add software ID to JSON array */
292 jstring = json_object_new_string(software_id);
293 json_object_array_add(this->jarray, jstring);
294 }
295 enumerator->destroy(enumerator);
296 }
297
298 METHOD(imv_swid_state_t, get_swid_inventory, json_object*,
299 private_imv_swid_state_t *this)
300 {
301 return this->jobj;
302 }
303
304 METHOD(imv_swid_state_t, set_count, void,
305 private_imv_swid_state_t *this, int tag_id_count, int tag_count)
306 {
307 this->tag_id_count += tag_id_count;
308 this->tag_count += tag_count;
309 }
310
311 METHOD(imv_swid_state_t, get_count, void,
312 private_imv_swid_state_t *this, int *tag_id_count, int *tag_count)
313 {
314 if (tag_id_count)
315 {
316 *tag_id_count = this->tag_id_count;
317 }
318 if (tag_count)
319 {
320 *tag_count = this->tag_count;
321 }
322 }
323
324 METHOD(imv_swid_state_t, set_angel_count, void,
325 private_imv_swid_state_t *this, bool start)
326 {
327 this->angel_count += start ? 1 : -1;
328 }
329
330 METHOD(imv_swid_state_t, get_angel_count, int,
331 private_imv_swid_state_t *this)
332 {
333 return this->angel_count;
334 }
335
336 /**
337 * Described in header.
338 */
339 imv_state_t *imv_swid_state_create(TNC_ConnectionID connection_id)
340 {
341 private_imv_swid_state_t *this;
342
343 INIT(this,
344 .public = {
345 .interface = {
346 .get_connection_id = _get_connection_id,
347 .has_long = _has_long,
348 .has_excl = _has_excl,
349 .set_flags = _set_flags,
350 .set_max_msg_len = _set_max_msg_len,
351 .get_max_msg_len = _get_max_msg_len,
352 .set_action_flags = _set_action_flags,
353 .get_action_flags = _get_action_flags,
354 .set_session = _set_session,
355 .get_session= _get_session,
356 .change_state = _change_state,
357 .get_recommendation = _get_recommendation,
358 .set_recommendation = _set_recommendation,
359 .update_recommendation = _update_recommendation,
360 .get_reason_string = _get_reason_string,
361 .get_remediation_instructions = _get_remediation_instructions,
362 .destroy = _destroy,
363 },
364 .set_handshake_state = _set_handshake_state,
365 .get_handshake_state = _get_handshake_state,
366 .set_request_id = _set_request_id,
367 .get_request_id = _get_request_id,
368 .set_swid_inventory = _set_swid_inventory,
369 .get_swid_inventory = _get_swid_inventory,
370 .set_count = _set_count,
371 .get_count = _get_count,
372 .set_angel_count = _set_angel_count,
373 .get_angel_count = _get_angel_count,
374 },
375 .state = TNC_CONNECTION_STATE_CREATE,
376 .rec = TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
377 .eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW,
378 .connection_id = connection_id,
379 .jobj = json_object_new_object(),
380 .jarray = json_object_new_array(),
381 );
382
383 json_object_object_add(this->jobj, "data", this->jarray);
384
385 return &this->public.interface;
386 }
387
388