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