Use python-based swidGenerator to generated SWID tags
[strongswan.git] / src / libpts / plugins / imv_swid / imv_swid_agent.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_agent.h"
17 #include "imv_swid_state.h"
18
19 #include "libpts.h"
20 #include "swid/swid_error.h"
21 #include "swid/swid_inventory.h"
22 #include "tcg/swid/tcg_swid_attr_req.h"
23 #include "tcg/swid/tcg_swid_attr_tag_inv.h"
24 #include "tcg/swid/tcg_swid_attr_tag_id_inv.h"
25
26 #include <imcv.h>
27 #include <ietf/ietf_attr_pa_tnc_error.h>
28 #include <imv/imv_agent.h>
29 #include <imv/imv_msg.h>
30 #include <ita/ita_attr.h>
31 #include <ita/ita_attr_angel.h>
32
33 #include <tncif_names.h>
34 #include <tncif_pa_subtypes.h>
35
36 #include <pen/pen.h>
37 #include <utils/debug.h>
38 #include <bio/bio_reader.h>
39
40 typedef struct private_imv_swid_agent_t private_imv_swid_agent_t;
41
42 /* Subscribed PA-TNC message subtypes */
43 static pen_type_t msg_types[] = {
44 { PEN_TCG, PA_SUBTYPE_TCG_SWID }
45 };
46
47 /**
48 * Private data of an imv_swid_agent_t object.
49 */
50 struct private_imv_swid_agent_t {
51
52 /**
53 * Public members of imv_swid_agent_t
54 */
55 imv_agent_if_t public;
56
57 /**
58 * IMV agent responsible for generic functions
59 */
60 imv_agent_t *agent;
61
62 };
63
64 METHOD(imv_agent_if_t, bind_functions, TNC_Result,
65 private_imv_swid_agent_t *this, TNC_TNCS_BindFunctionPointer bind_function)
66 {
67 return this->agent->bind_functions(this->agent, bind_function);
68 }
69
70 METHOD(imv_agent_if_t, notify_connection_change, TNC_Result,
71 private_imv_swid_agent_t *this, TNC_ConnectionID id,
72 TNC_ConnectionState new_state)
73 {
74 imv_state_t *state;
75
76 switch (new_state)
77 {
78 case TNC_CONNECTION_STATE_CREATE:
79 state = imv_swid_state_create(id);
80 return this->agent->create_state(this->agent, state);
81 case TNC_CONNECTION_STATE_DELETE:
82 return this->agent->delete_state(this->agent, id);
83 default:
84 return this->agent->change_state(this->agent, id, new_state, NULL);
85 }
86 }
87
88 /**
89 * Process a received message
90 */
91 static TNC_Result receive_msg(private_imv_swid_agent_t *this,
92 imv_state_t *state, imv_msg_t *in_msg)
93 {
94 imv_swid_state_t *swid_state;
95 imv_msg_t *out_msg;
96 imv_session_t *session;
97 enumerator_t *enumerator;
98 pa_tnc_attr_t *attr;
99 TNC_Result result;
100 bool fatal_error = FALSE;
101
102 /* parse received PA-TNC message and handle local and remote errors */
103 result = in_msg->receive(in_msg, &fatal_error);
104 if (result != TNC_RESULT_SUCCESS)
105 {
106 return result;
107 }
108
109 swid_state = (imv_swid_state_t*)state;
110 session = state->get_session(state);
111
112 /* analyze PA-TNC attributes */
113 enumerator = in_msg->create_attribute_enumerator(in_msg);
114 while (enumerator->enumerate(enumerator, &attr))
115 {
116 TNC_IMV_Evaluation_Result eval;
117 TNC_IMV_Action_Recommendation rec;
118 pen_type_t type;
119 uint32_t request_id, last_eid, eid_epoch;
120 swid_inventory_t *inventory;
121 int tag_count;
122 char result_str[BUF_LEN], *tag_item;
123 imv_workitem_t *workitem, *found = NULL;
124 enumerator_t *et, *ew;
125
126 type = attr->get_type(attr);
127
128 if (type.vendor_id == PEN_IETF && type.type == IETF_ATTR_PA_TNC_ERROR)
129 {
130 ietf_attr_pa_tnc_error_t *error_attr;
131 pen_type_t error_code;
132 chunk_t msg_info, description;
133 bio_reader_t *reader;
134 uint32_t request_id = 0, max_attr_size;
135 bool success;
136
137 error_attr = (ietf_attr_pa_tnc_error_t*)attr;
138 error_code = error_attr->get_error_code(error_attr);
139
140 if (error_code.vendor_id == PEN_TCG)
141 {
142 fatal_error = TRUE;
143 msg_info = error_attr->get_msg_info(error_attr);
144 reader = bio_reader_create(msg_info);
145 success = reader->read_uint32(reader, &request_id);
146
147 DBG1(DBG_IMV, "received TCG error '%N' for request %d",
148 swid_error_code_names, error_code.type, request_id);
149 if (!success)
150 {
151 reader->destroy(reader);
152 continue;
153 }
154 if (error_code.type == TCG_SWID_RESPONSE_TOO_LARGE)
155 {
156 if (!reader->read_uint32(reader, &max_attr_size))
157 {
158 reader->destroy(reader);
159 continue;
160 }
161 DBG1(DBG_IMV, " maximum PA-TNC attribute size is %u bytes",
162 max_attr_size);
163 }
164 description = reader->peek(reader);
165 if (description.len)
166 {
167 DBG1(DBG_IMV, " description: %.*s", description.len,
168 description.ptr);
169 }
170 reader->destroy(reader);
171 }
172 }
173 else if (type.vendor_id == PEN_ITA)
174 {
175 switch (type.type)
176 {
177 case ITA_ATTR_START_ANGEL:
178 swid_state->set_angel_count(swid_state, TRUE);
179 break;
180 case ITA_ATTR_STOP_ANGEL:
181 swid_state->set_angel_count(swid_state, FALSE);
182 break;
183 default:
184 break;
185 }
186 }
187 else if (type.vendor_id != PEN_TCG)
188 {
189 continue;
190 }
191
192 switch (type.type)
193 {
194 case TCG_SWID_TAG_ID_INVENTORY:
195 {
196 tcg_swid_attr_tag_id_inv_t *attr_cast;
197 swid_tag_id_t *tag_id;
198 chunk_t tag_creator, unique_sw_id;
199
200 attr_cast = (tcg_swid_attr_tag_id_inv_t*)attr;
201 request_id = attr_cast->get_request_id(attr_cast);
202 last_eid = attr_cast->get_last_eid(attr_cast, &eid_epoch);
203 inventory = attr_cast->get_inventory(attr_cast);
204 tag_item = "tag ID";
205 DBG2(DBG_IMV, "received SWID %s inventory for request %d "
206 "at eid %d of epoch 0x%08x", tag_item,
207 request_id, last_eid, eid_epoch);
208
209 et = inventory->create_enumerator(inventory);
210 while (et->enumerate(et, &tag_id))
211 {
212 tag_creator = tag_id->get_tag_creator(tag_id);
213 unique_sw_id = tag_id->get_unique_sw_id(tag_id, NULL);
214 DBG3(DBG_IMV, " %.*s_%.*s",
215 tag_creator.len, tag_creator.ptr,
216 unique_sw_id.len, unique_sw_id.ptr);
217 }
218 et->destroy(et);
219
220 if (request_id == 0)
221 {
222 /* TODO handle subscribed messages */
223 break;
224 }
225 break;
226 }
227 case TCG_SWID_TAG_INVENTORY:
228 {
229 tcg_swid_attr_tag_inv_t *attr_cast;
230 swid_tag_t *tag;
231 chunk_t tag_encoding;
232
233 attr_cast = (tcg_swid_attr_tag_inv_t*)attr;
234 request_id = attr_cast->get_request_id(attr_cast);
235 last_eid = attr_cast->get_last_eid(attr_cast, &eid_epoch);
236 inventory = attr_cast->get_inventory(attr_cast);
237 tag_item = "tag";
238 DBG2(DBG_IMV, "received SWID %s inventory for request %d "
239 "at eid %d of epoch 0x%08x", tag_item,
240 request_id, last_eid, eid_epoch);
241
242 et = inventory->create_enumerator(inventory);
243 while (et->enumerate(et, &tag))
244 {
245 tag_encoding = tag->get_encoding(tag);
246 DBG3(DBG_IMV, "%.*s", tag_encoding.len, tag_encoding.ptr);
247 }
248 et->destroy(et);
249
250 if (request_id == 0)
251 {
252 /* TODO handle subscribed messages */
253 break;
254 }
255 break;
256 }
257 default:
258 continue;
259 }
260 tag_count = inventory->get_count(inventory);
261 swid_state->set_count(swid_state, tag_count);
262
263 ew = session->create_workitem_enumerator(session);
264 while (ew->enumerate(ew, &workitem))
265 {
266 if (workitem->get_id(workitem) == request_id)
267 {
268 found = workitem;
269 break;
270 }
271 }
272 if (!found)
273 {
274 DBG1(DBG_IMV, "no workitem found for SWID %s inventory "
275 "with request ID %d", tag_item, request_id);
276 ew->destroy(ew);
277 continue;
278 }
279
280 if (!swid_state->get_angel_count(swid_state))
281 {
282 swid_state->get_count(swid_state, &tag_count);
283 snprintf(result_str, BUF_LEN, "received inventory of %d SWID %s%s",
284 tag_count, tag_item, (tag_count == 1) ? "" : "s");
285 session->remove_workitem(session, ew);
286 ew->destroy(ew);
287
288 eval = TNC_IMV_EVALUATION_RESULT_COMPLIANT;
289 rec = found->set_result(found, result_str, eval);
290 state->update_recommendation(state, rec, eval);
291 imcv_db->finalize_workitem(imcv_db, found);
292 found->destroy(found);
293 }
294 }
295 enumerator->destroy(enumerator);
296
297 if (fatal_error)
298 {
299 state->set_recommendation(state,
300 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
301 TNC_IMV_EVALUATION_RESULT_ERROR);
302 out_msg = imv_msg_create_as_reply(in_msg);
303 result = out_msg->send_assessment(out_msg);
304 out_msg->destroy(out_msg);
305 if (result != TNC_RESULT_SUCCESS)
306 {
307 return result;
308 }
309 return this->agent->provide_recommendation(this->agent, state);
310 }
311
312 return TNC_RESULT_SUCCESS;
313 }
314
315 METHOD(imv_agent_if_t, receive_message, TNC_Result,
316 private_imv_swid_agent_t *this, TNC_ConnectionID id,
317 TNC_MessageType msg_type, chunk_t msg)
318 {
319 imv_state_t *state;
320 imv_msg_t *in_msg;
321 TNC_Result result;
322
323 if (!this->agent->get_state(this->agent, id, &state))
324 {
325 return TNC_RESULT_FATAL;
326 }
327 in_msg = imv_msg_create_from_data(this->agent, state, id, msg_type, msg);
328 result = receive_msg(this, state, in_msg);
329 in_msg->destroy(in_msg);
330
331 return result;
332 }
333
334 METHOD(imv_agent_if_t, receive_message_long, TNC_Result,
335 private_imv_swid_agent_t *this, TNC_ConnectionID id,
336 TNC_UInt32 src_imc_id, TNC_UInt32 dst_imv_id,
337 TNC_VendorID msg_vid, TNC_MessageSubtype msg_subtype, chunk_t msg)
338 {
339 imv_state_t *state;
340 imv_msg_t *in_msg;
341 TNC_Result result;
342
343 if (!this->agent->get_state(this->agent, id, &state))
344 {
345 return TNC_RESULT_FATAL;
346 }
347 in_msg = imv_msg_create_from_long_data(this->agent, state, id,
348 src_imc_id, dst_imv_id, msg_vid, msg_subtype, msg);
349 result = receive_msg(this, state, in_msg);
350 in_msg->destroy(in_msg);
351
352 return result;
353
354 }
355
356 METHOD(imv_agent_if_t, batch_ending, TNC_Result,
357 private_imv_swid_agent_t *this, TNC_ConnectionID id)
358 {
359 imv_msg_t *out_msg;
360 imv_state_t *state;
361 imv_session_t *session;
362 imv_workitem_t *workitem;
363 imv_swid_state_t *swid_state;
364 imv_swid_handshake_state_t handshake_state;
365 pa_tnc_attr_t *attr;
366 TNC_IMVID imv_id;
367 TNC_Result result = TNC_RESULT_SUCCESS;
368 bool no_workitems = TRUE;
369 uint32_t request_id;
370 uint8_t flags;
371 enumerator_t *enumerator;
372
373 if (!this->agent->get_state(this->agent, id, &state))
374 {
375 return TNC_RESULT_FATAL;
376 }
377 swid_state = (imv_swid_state_t*)state;
378 handshake_state = swid_state->get_handshake_state(swid_state);
379 session = state->get_session(state);
380 imv_id = this->agent->get_id(this->agent);
381
382 if (handshake_state == IMV_SWID_STATE_END)
383 {
384 return TNC_RESULT_SUCCESS;
385 }
386
387 /* create an empty out message - we might need it */
388 out_msg = imv_msg_create(this->agent, state, id, imv_id, TNC_IMCID_ANY,
389 msg_types[0]);
390
391 if (!imcv_db)
392 {
393 DBG2(DBG_IMV, "no workitems available - no evaluation possible");
394 state->set_recommendation(state,
395 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
396 TNC_IMV_EVALUATION_RESULT_DONT_KNOW);
397 result = out_msg->send_assessment(out_msg);
398 out_msg->destroy(out_msg);
399 swid_state->set_handshake_state(swid_state, IMV_SWID_STATE_END);
400
401 if (result != TNC_RESULT_SUCCESS)
402 {
403 return result;
404 }
405 return this->agent->provide_recommendation(this->agent, state);
406 }
407
408 if (handshake_state == IMV_SWID_STATE_INIT &&
409 session->get_policy_started(session))
410 {
411 enumerator = session->create_workitem_enumerator(session);
412 if (enumerator)
413 {
414 while (enumerator->enumerate(enumerator, &workitem))
415 {
416 if (workitem->get_imv_id(workitem) != TNC_IMVID_ANY ||
417 workitem->get_type(workitem) != IMV_WORKITEM_SWID_TAGS)
418 {
419 continue;
420 }
421
422 flags = TCG_SWID_ATTR_REQ_FLAG_NONE;
423 if (strchr(workitem->get_arg_str(workitem), 'R'))
424 {
425 flags |= TCG_SWID_ATTR_REQ_FLAG_R;
426 }
427 if (strchr(workitem->get_arg_str(workitem), 'S'))
428 {
429 flags |= TCG_SWID_ATTR_REQ_FLAG_S;
430 }
431 if (strchr(workitem->get_arg_str(workitem), 'C'))
432 {
433 flags |= TCG_SWID_ATTR_REQ_FLAG_C;
434 }
435 request_id = workitem->get_id(workitem);
436
437 attr = tcg_swid_attr_req_create(flags, request_id, 0);
438 out_msg->add_attribute(out_msg, attr);
439 workitem->set_imv_id(workitem, imv_id);
440 no_workitems = FALSE;
441 DBG2(DBG_IMV, "IMV %d issues SWID request %d",
442 imv_id, request_id);
443 }
444 enumerator->destroy(enumerator);
445
446 if (no_workitems)
447 {
448 DBG2(DBG_IMV, "IMV %d has no workitems - "
449 "no evaluation requested", imv_id);
450 state->set_recommendation(state,
451 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
452 TNC_IMV_EVALUATION_RESULT_DONT_KNOW);
453 }
454 handshake_state = IMV_SWID_STATE_WORKITEMS;
455 swid_state->set_handshake_state(swid_state, handshake_state);
456 }
457 }
458
459 /* finalized all workitems ? */
460 if (handshake_state == IMV_SWID_STATE_WORKITEMS &&
461 session->get_workitem_count(session, imv_id) == 0)
462 {
463 result = out_msg->send_assessment(out_msg);
464 out_msg->destroy(out_msg);
465 swid_state->set_handshake_state(swid_state, IMV_SWID_STATE_END);
466
467 if (result != TNC_RESULT_SUCCESS)
468 {
469 return result;
470 }
471 return this->agent->provide_recommendation(this->agent, state);
472 }
473
474 /* send non-empty PA-TNC message with excl flag not set */
475 if (out_msg->get_attribute_count(out_msg))
476 {
477 result = out_msg->send(out_msg, FALSE);
478 }
479 out_msg->destroy(out_msg);
480
481 return result;
482 }
483
484 METHOD(imv_agent_if_t, solicit_recommendation, TNC_Result,
485 private_imv_swid_agent_t *this, TNC_ConnectionID id)
486 {
487 imv_state_t *state;
488
489 if (!this->agent->get_state(this->agent, id, &state))
490 {
491 return TNC_RESULT_FATAL;
492 }
493 return this->agent->provide_recommendation(this->agent, state);
494 }
495
496 METHOD(imv_agent_if_t, destroy, void,
497 private_imv_swid_agent_t *this)
498 {
499 this->agent->destroy(this->agent);
500 free(this);
501 libpts_deinit();
502 }
503
504 /**
505 * Described in header.
506 */
507 imv_agent_if_t *imv_swid_agent_create(const char *name, TNC_IMVID id,
508 TNC_Version *actual_version)
509 {
510 private_imv_swid_agent_t *this;
511 imv_agent_t *agent;
512
513 agent = imv_agent_create(name, msg_types, countof(msg_types), id,
514 actual_version);
515 if (!agent)
516 {
517 return NULL;
518 }
519
520 INIT(this,
521 .public = {
522 .bind_functions = _bind_functions,
523 .notify_connection_change = _notify_connection_change,
524 .receive_message = _receive_message,
525 .receive_message_long = _receive_message_long,
526 .batch_ending = _batch_ending,
527 .solicit_recommendation = _solicit_recommendation,
528 .destroy = _destroy,
529 },
530 .agent = agent,
531 );
532
533 libpts_init();
534
535 return &this->public;
536 }
537