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