]> git.ipfire.org Git - thirdparty/strongswan.git/blob - src/libimcv/plugins/imv_swid/imv_swid_state.c
libimcv: SWIMA SW locator must be file URI
[thirdparty/strongswan.git] / src / libimcv / plugins / imv_swid / imv_swid_state.c
1 /*
2 * Copyright (C) 2013-2017 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 associated with TNCCS connection
72 */
73 imv_session_t *session;
74
75 /**
76 * PA-TNC attribute segmentation contracts associated with TNCCS connection
77 */
78 seg_contract_manager_t *contracts;
79
80 /**
81 * IMV action recommendation
82 */
83 TNC_IMV_Action_Recommendation rec;
84
85 /**
86 * IMV evaluation result
87 */
88 TNC_IMV_Evaluation_Result eval;
89
90 /**
91 * IMV Scanner handshake state
92 */
93 imv_swid_handshake_state_t handshake_state;
94
95 /**
96 * TNC Reason String
97 */
98 imv_reason_string_t *reason_string;
99
100 /**
101 * IETF Remediation Instructions String
102 */
103 imv_remediation_string_t *remediation_string;
104
105 /**
106 * SWID Tag Request ID
107 */
108 uint32_t request_id;
109
110 /**
111 * Number of processed SWID Tag IDs
112 */
113 int tag_id_count;
114
115 /**
116 * Number of processed SWID Tags
117 */
118 int tag_count;
119
120 /**
121 * Number of missing SWID Tags or Tag IDs
122 */
123 uint32_t missing;
124
125 /**
126 * SWID IMC ID
127 */
128 TNC_UInt32 imc_id;
129
130 /**
131 * Top level JSON object
132 */
133 json_object *jobj;
134
135 /**
136 * JSON array containing an inventory of SWID Tag IDs
137 */
138 json_object *jarray;
139
140 };
141
142 METHOD(imv_state_t, get_connection_id, TNC_ConnectionID,
143 private_imv_swid_state_t *this)
144 {
145 return this->connection_id;
146 }
147
148 METHOD(imv_state_t, has_long, bool,
149 private_imv_swid_state_t *this)
150 {
151 return this->has_long;
152 }
153
154 METHOD(imv_state_t, has_excl, bool,
155 private_imv_swid_state_t *this)
156 {
157 return this->has_excl;
158 }
159
160 METHOD(imv_state_t, set_flags, void,
161 private_imv_swid_state_t *this, bool has_long, bool has_excl)
162 {
163 this->has_long = has_long;
164 this->has_excl = has_excl;
165 }
166
167 METHOD(imv_state_t, set_max_msg_len, void,
168 private_imv_swid_state_t *this, uint32_t max_msg_len)
169 {
170 this->max_msg_len = max_msg_len;
171 }
172
173 METHOD(imv_state_t, get_max_msg_len, uint32_t,
174 private_imv_swid_state_t *this)
175 {
176 return this->max_msg_len;
177 }
178
179 METHOD(imv_state_t, set_action_flags, void,
180 private_imv_swid_state_t *this, uint32_t flags)
181 {
182 this->action_flags |= flags;
183 }
184
185 METHOD(imv_state_t, get_action_flags, uint32_t,
186 private_imv_swid_state_t *this)
187 {
188 return this->action_flags;
189 }
190
191 METHOD(imv_state_t, set_session, void,
192 private_imv_swid_state_t *this, imv_session_t *session)
193 {
194 this->session = session;
195 }
196
197 METHOD(imv_state_t, get_session, imv_session_t*,
198 private_imv_swid_state_t *this)
199 {
200 return this->session;
201 }
202
203 METHOD(imv_state_t, get_contracts, seg_contract_manager_t*,
204 private_imv_swid_state_t *this)
205 {
206 return this->contracts;
207 }
208
209 METHOD(imv_state_t, change_state, void,
210 private_imv_swid_state_t *this, TNC_ConnectionState new_state)
211 {
212 this->state = new_state;
213 }
214
215 METHOD(imv_state_t, get_recommendation, void,
216 private_imv_swid_state_t *this, TNC_IMV_Action_Recommendation *rec,
217 TNC_IMV_Evaluation_Result *eval)
218 {
219 *rec = this->rec;
220 *eval = this->eval;
221 }
222
223 METHOD(imv_state_t, set_recommendation, void,
224 private_imv_swid_state_t *this, TNC_IMV_Action_Recommendation rec,
225 TNC_IMV_Evaluation_Result eval)
226 {
227 this->rec = rec;
228 this->eval = eval;
229 }
230
231 METHOD(imv_state_t, update_recommendation, void,
232 private_imv_swid_state_t *this, TNC_IMV_Action_Recommendation rec,
233 TNC_IMV_Evaluation_Result eval)
234 {
235 this->rec = tncif_policy_update_recommendation(this->rec, rec);
236 this->eval = tncif_policy_update_evaluation(this->eval, eval);
237 }
238
239 METHOD(imv_state_t, get_reason_string, bool,
240 private_imv_swid_state_t *this, enumerator_t *language_enumerator,
241 chunk_t *reason_string, char **reason_language)
242 {
243 return FALSE;
244 }
245
246 METHOD(imv_state_t, get_remediation_instructions, bool,
247 private_imv_swid_state_t *this, enumerator_t *language_enumerator,
248 chunk_t *string, char **lang_code, char **uri)
249 {
250 return FALSE;
251 }
252
253 METHOD(imv_state_t, destroy, void,
254 private_imv_swid_state_t *this)
255 {
256 json_object_put(this->jobj);
257 DESTROY_IF(this->session);
258 DESTROY_IF(this->reason_string);
259 DESTROY_IF(this->remediation_string);
260 this->contracts->destroy(this->contracts);
261 free(this);
262 }
263
264 METHOD(imv_swid_state_t, set_handshake_state, void,
265 private_imv_swid_state_t *this, imv_swid_handshake_state_t new_state)
266 {
267 this->handshake_state = new_state;
268 }
269
270 METHOD(imv_swid_state_t, get_handshake_state, imv_swid_handshake_state_t,
271 private_imv_swid_state_t *this)
272 {
273 return this->handshake_state;
274 }
275
276 METHOD(imv_swid_state_t, set_request_id, void,
277 private_imv_swid_state_t *this, uint32_t request_id)
278 {
279 this->request_id = request_id;
280 }
281
282 METHOD(imv_swid_state_t, get_request_id, uint32_t,
283 private_imv_swid_state_t *this)
284 {
285 return this->request_id;
286 }
287
288 METHOD(imv_swid_state_t, set_swid_inventory, void,
289 private_imv_swid_state_t *this, swid_inventory_t *inventory)
290 {
291 chunk_t tag_creator, sw_id;
292 char software_id[BUF_LEN];
293 json_object *jstring;
294 swid_tag_id_t *tag_id;
295 enumerator_t *enumerator;
296
297 enumerator = inventory->create_enumerator(inventory);
298 while (enumerator->enumerate(enumerator, &tag_id))
299 {
300 /* Construct software ID from tag creator and unique software ID */
301 tag_creator = tag_id->get_tag_creator(tag_id);
302 sw_id = tag_id->get_unique_sw_id(tag_id, NULL);
303 snprintf(software_id, BUF_LEN, "%.*s__%.*s",
304 (int)tag_creator.len, tag_creator.ptr,
305 (int)sw_id.len, sw_id.ptr);
306 DBG3(DBG_IMV, " %s", software_id);
307
308 /* Add software ID to JSON array */
309 jstring = json_object_new_string(software_id);
310 json_object_array_add(this->jarray, jstring);
311 }
312 enumerator->destroy(enumerator);
313 }
314
315 METHOD(imv_swid_state_t, get_swid_inventory, json_object*,
316 private_imv_swid_state_t *this)
317 {
318 return this->jobj;
319 }
320
321 METHOD(imv_swid_state_t, set_missing, void,
322 private_imv_swid_state_t *this, uint32_t count)
323 {
324 this->missing = count;
325 }
326
327 METHOD(imv_swid_state_t, get_missing, uint32_t,
328 private_imv_swid_state_t *this)
329 {
330 return this->missing;
331 }
332
333 METHOD(imv_swid_state_t, set_count, void,
334 private_imv_swid_state_t *this, int tag_id_count, int tag_count,
335 TNC_UInt32 imc_id)
336 {
337 this->tag_id_count += tag_id_count;
338 this->tag_count += tag_count;
339 this->imc_id = imc_id;
340 }
341
342 METHOD(imv_swid_state_t, get_count, void,
343 private_imv_swid_state_t *this, int *tag_id_count, int *tag_count)
344 {
345 if (tag_id_count)
346 {
347 *tag_id_count = this->tag_id_count;
348 }
349 if (tag_count)
350 {
351 *tag_count = this->tag_count;
352 }
353 }
354
355 METHOD(imv_swid_state_t, get_imc_id, TNC_UInt32,
356 private_imv_swid_state_t *this)
357 {
358 return this->imc_id;
359 }
360
361 /**
362 * Described in header.
363 */
364 imv_state_t *imv_swid_state_create(TNC_ConnectionID connection_id)
365 {
366 private_imv_swid_state_t *this;
367
368 INIT(this,
369 .public = {
370 .interface = {
371 .get_connection_id = _get_connection_id,
372 .has_long = _has_long,
373 .has_excl = _has_excl,
374 .set_flags = _set_flags,
375 .set_max_msg_len = _set_max_msg_len,
376 .get_max_msg_len = _get_max_msg_len,
377 .set_action_flags = _set_action_flags,
378 .get_action_flags = _get_action_flags,
379 .set_session = _set_session,
380 .get_session= _get_session,
381 .get_contracts = _get_contracts,
382 .change_state = _change_state,
383 .get_recommendation = _get_recommendation,
384 .set_recommendation = _set_recommendation,
385 .update_recommendation = _update_recommendation,
386 .get_reason_string = _get_reason_string,
387 .get_remediation_instructions = _get_remediation_instructions,
388 .destroy = _destroy,
389 },
390 .set_handshake_state = _set_handshake_state,
391 .get_handshake_state = _get_handshake_state,
392 .set_request_id = _set_request_id,
393 .get_request_id = _get_request_id,
394 .set_swid_inventory = _set_swid_inventory,
395 .get_swid_inventory = _get_swid_inventory,
396 .set_missing = _set_missing,
397 .get_missing = _get_missing,
398 .set_count = _set_count,
399 .get_count = _get_count,
400 .get_imc_id = _get_imc_id,
401 },
402 .state = TNC_CONNECTION_STATE_CREATE,
403 .rec = TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
404 .eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW,
405 .connection_id = connection_id,
406 .contracts = seg_contract_manager_create(),
407 .imc_id = TNC_IMCID_ANY,
408 .jobj = json_object_new_object(),
409 .jarray = json_object_new_array(),
410 );
411
412 json_object_object_add(this->jobj, "data", this->jarray);
413
414 return &this->public.interface;
415 }
416
417