28ebd00691ddf389ce39962a5ba2cfec10fd86c9
[strongswan.git] / src / libimcv / plugins / imv_attestation / imv_attestation_agent.c
1 /*
2 * Copyright (C) 2011-2012 Sansar Choinyambuu
3 * Copyright (C) 2011-2015 Andreas Steffen
4 * HSR Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #define _GNU_SOURCE /* for stdndup() */
18 #include <string.h>
19
20 #include "imv_attestation_agent.h"
21 #include "imv_attestation_state.h"
22 #include "imv_attestation_process.h"
23 #include "imv_attestation_build.h"
24
25 #include <imcv.h>
26 #include <imv/imv_agent.h>
27 #include <imv/imv_msg.h>
28 #include <imv/imv_session.h>
29 #include <imv/imv_os_info.h>
30 #include <generic/generic_attr_string.h>
31 #include <ietf/ietf_attr.h>
32 #include <ietf/ietf_attr_attr_request.h>
33 #include <ietf/ietf_attr_pa_tnc_error.h>
34 #include <ietf/ietf_attr_product_info.h>
35 #include <ietf/ietf_attr_string_version.h>
36 #include <ita/ita_attr.h>
37 #include <tcg/tcg_attr.h>
38 #include <tcg/pts/tcg_pts_attr_meas_algo.h>
39 #include <tcg/pts/tcg_pts_attr_proto_caps.h>
40 #include <tcg/pts/tcg_pts_attr_req_file_meas.h>
41 #include <tcg/pts/tcg_pts_attr_req_file_meta.h>
42 #include "tcg/seg/tcg_seg_attr_max_size.h"
43 #include "tcg/seg/tcg_seg_attr_seg_env.h"
44 #include <pts/pts.h>
45 #include <pts/pts_database.h>
46 #include <pts/pts_creds.h>
47 #include <pts/components/ita/ita_comp_func_name.h>
48
49 #include <tncif_pa_subtypes.h>
50
51 #include <pen/pen.h>
52 #include <utils/debug.h>
53 #include <credentials/credential_manager.h>
54 #include <collections/linked_list.h>
55
56 #define FILE_MEAS_MAX_ATTR_SIZE 100000000
57
58 typedef struct private_imv_attestation_agent_t private_imv_attestation_agent_t;
59
60 /* Subscribed PA-TNC message subtypes */
61 static pen_type_t msg_types[] = {
62 { PEN_TCG, PA_SUBTYPE_TCG_PTS },
63 { PEN_IETF, PA_SUBTYPE_IETF_OPERATING_SYSTEM }
64 };
65
66 /**
67 * Private data of an imv_attestation_agent_t object.
68 */
69 struct private_imv_attestation_agent_t {
70
71 /**
72 * Public members of imv_attestation_agent_t
73 */
74 imv_agent_if_t public;
75
76 /**
77 * IMV agent responsible for generic functions
78 */
79 imv_agent_t *agent;
80
81 /**
82 * Supported PTS measurement algorithms
83 */
84 pts_meas_algorithms_t supported_algorithms;
85
86 /**
87 * Supported PTS Diffie Hellman Groups
88 */
89 pts_dh_group_t supported_dh_groups;
90
91 /**
92 * PTS file measurement database
93 */
94 pts_database_t *pts_db;
95
96 /**
97 * PTS credentials
98 */
99 pts_creds_t *pts_creds;
100
101 /**
102 * PTS credential manager
103 */
104 credential_manager_t *pts_credmgr;
105
106 };
107
108 METHOD(imv_agent_if_t, bind_functions, TNC_Result,
109 private_imv_attestation_agent_t *this, TNC_TNCS_BindFunctionPointer bind_function)
110 {
111 return this->agent->bind_functions(this->agent, bind_function);
112 }
113
114 METHOD(imv_agent_if_t, notify_connection_change, TNC_Result,
115 private_imv_attestation_agent_t *this, TNC_ConnectionID id,
116 TNC_ConnectionState new_state)
117 {
118 TNC_IMV_Action_Recommendation rec;
119 imv_state_t *state;
120 imv_session_t *session;
121
122 switch (new_state)
123 {
124 case TNC_CONNECTION_STATE_CREATE:
125 state = imv_attestation_state_create(id);
126 return this->agent->create_state(this->agent, state);
127 case TNC_CONNECTION_STATE_DELETE:
128 return this->agent->delete_state(this->agent, id);
129 case TNC_CONNECTION_STATE_ACCESS_ALLOWED:
130 case TNC_CONNECTION_STATE_ACCESS_ISOLATED:
131 case TNC_CONNECTION_STATE_ACCESS_NONE:
132 if (this->agent->get_state(this->agent, id, &state) && imcv_db)
133 {
134 session = state->get_session(state);
135
136 if (session->get_policy_started(session))
137 {
138 switch (new_state)
139 {
140 case TNC_CONNECTION_STATE_ACCESS_ALLOWED:
141 rec = TNC_IMV_ACTION_RECOMMENDATION_ALLOW;
142 break;
143 case TNC_CONNECTION_STATE_ACCESS_ISOLATED:
144 rec = TNC_IMV_ACTION_RECOMMENDATION_ISOLATE;
145 break;
146 case TNC_CONNECTION_STATE_ACCESS_NONE:
147 default:
148 rec = TNC_IMV_ACTION_RECOMMENDATION_NO_ACCESS;
149 }
150 imcv_db->add_recommendation(imcv_db, session, rec);
151 if (!imcv_db->policy_script(imcv_db, session, FALSE))
152 {
153 DBG1(DBG_IMV, "error in policy script stop");
154 }
155 }
156 }
157 /* fall through to default state */
158 default:
159 return this->agent->change_state(this->agent, id, new_state, NULL);
160 }
161 }
162
163 /**
164 * Process a received message
165 */
166 static TNC_Result receive_msg(private_imv_attestation_agent_t *this,
167 imv_state_t *state, imv_msg_t *in_msg)
168 {
169 imv_msg_t *out_msg;
170 imv_session_t *session;
171 imv_os_info_t *os_info;
172 enumerator_t *enumerator;
173 pa_tnc_attr_t *attr;
174 pen_type_t type;
175 TNC_Result result;
176 chunk_t os_name, os_version;
177 bool fatal_error = FALSE;
178
179 /* generate an outgoing PA-TNC message - we might need it */
180 out_msg = imv_msg_create_as_reply(in_msg);
181 out_msg->set_msg_type(out_msg, msg_types[0]);
182
183 /* parse received PA-TNC message and handle local and remote errors */
184 result = in_msg->receive(in_msg, out_msg, &fatal_error);
185 if (result != TNC_RESULT_SUCCESS)
186 {
187 out_msg->destroy(out_msg);
188 return result;
189 }
190
191 session = state->get_session(state);
192 os_info = session->get_os_info(session);
193
194 /* analyze PA-TNC attributes */
195 enumerator = in_msg->create_attribute_enumerator(in_msg);
196 while (enumerator->enumerate(enumerator, &attr))
197 {
198 type = attr->get_type(attr);
199
200 if (type.vendor_id == PEN_IETF)
201 {
202 switch (type.type)
203 {
204 case IETF_ATTR_PA_TNC_ERROR:
205 {
206 ietf_attr_pa_tnc_error_t *error_attr;
207 pen_type_t error_code;
208 chunk_t msg_info;
209
210 error_attr = (ietf_attr_pa_tnc_error_t*)attr;
211 error_code = error_attr->get_error_code(error_attr);
212
213 if (error_code.vendor_id == PEN_TCG)
214 {
215 msg_info = error_attr->get_msg_info(error_attr);
216
217 DBG1(DBG_IMV, "received TCG-PTS error '%N'",
218 pts_error_code_names, error_code.type);
219 DBG1(DBG_IMV, "error information: %B", &msg_info);
220 fatal_error = TRUE;
221 }
222 break;
223 }
224 case IETF_ATTR_PRODUCT_INFORMATION:
225 {
226 ietf_attr_product_info_t *attr_cast;
227 pen_t vendor_id;
228
229 state->set_action_flags(state,
230 IMV_ATTESTATION_ATTR_PRODUCT_INFO);
231 attr_cast = (ietf_attr_product_info_t*)attr;
232 os_name = attr_cast->get_info(attr_cast, &vendor_id, NULL);
233 os_info->set_name(os_info, os_name);
234
235 if (vendor_id != PEN_IETF)
236 {
237 DBG1(DBG_IMV, "operating system name is '%.*s' "
238 "from vendor %N", os_name.len, os_name.ptr,
239 pen_names, vendor_id);
240 }
241 else
242 {
243 DBG1(DBG_IMV, "operating system name is '%.*s'",
244 os_name.len, os_name.ptr);
245 }
246 break;
247
248 break;
249 }
250 case IETF_ATTR_STRING_VERSION:
251 {
252 ietf_attr_string_version_t *attr_cast;
253
254 state->set_action_flags(state,
255 IMV_ATTESTATION_ATTR_STRING_VERSION);
256 attr_cast = (ietf_attr_string_version_t*)attr;
257 os_version = attr_cast->get_version(attr_cast, NULL, NULL);
258 os_info->set_version(os_info, os_version);
259
260 if (os_version.len)
261 {
262 DBG1(DBG_IMV, "operating system version is '%.*s'",
263 os_version.len, os_version.ptr);
264 }
265 break;
266 }
267 default:
268 break;
269 }
270 }
271 else if (type.vendor_id == PEN_ITA)
272 {
273 switch (type.type)
274 {
275 case ITA_ATTR_DEVICE_ID:
276 {
277 chunk_t value;
278
279 state->set_action_flags(state,
280 IMV_ATTESTATION_ATTR_DEVICE_ID);
281
282 value = attr->get_value(attr);
283 DBG1(DBG_IMV, "device ID is %.*s", value.len, value.ptr);
284 session->set_device_id(session, value);
285 break;
286 }
287 default:
288 break;
289 }
290 }
291 else if (type.vendor_id == PEN_TCG)
292 {
293 if (!imv_attestation_process(attr, out_msg, state,
294 this->supported_algorithms, this->supported_dh_groups,
295 this->pts_db, this->pts_credmgr))
296 {
297 result = TNC_RESULT_FATAL;
298 break;
299 }
300 }
301 }
302 enumerator->destroy(enumerator);
303
304 if (fatal_error || result != TNC_RESULT_SUCCESS)
305 {
306 state->set_recommendation(state,
307 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
308 TNC_IMV_EVALUATION_RESULT_ERROR);
309 result = out_msg->send_assessment(out_msg);
310 if (result == TNC_RESULT_SUCCESS)
311 {
312 result = this->agent->provide_recommendation(this->agent, state);
313 }
314 }
315 else
316 {
317 /* send PA-TNC message with the EXCL flag set */
318 result = out_msg->send(out_msg, TRUE);
319 }
320 out_msg->destroy(out_msg);
321
322 return result;
323 }
324
325 METHOD(imv_agent_if_t, receive_message, TNC_Result,
326 private_imv_attestation_agent_t *this, TNC_ConnectionID id,
327 TNC_MessageType msg_type, chunk_t msg)
328 {
329 imv_state_t *state;
330 imv_msg_t *in_msg;
331 TNC_Result result;
332
333 if (!this->agent->get_state(this->agent, id, &state))
334 {
335 return TNC_RESULT_FATAL;
336 }
337 in_msg = imv_msg_create_from_data(this->agent, state, id, msg_type, msg);
338 result = receive_msg(this, state, in_msg);
339 in_msg->destroy(in_msg);
340
341 return result;
342 }
343
344 METHOD(imv_agent_if_t, receive_message_long, TNC_Result,
345 private_imv_attestation_agent_t *this, TNC_ConnectionID id,
346 TNC_UInt32 src_imc_id, TNC_UInt32 dst_imv_id,
347 TNC_VendorID msg_vid, TNC_MessageSubtype msg_subtype, chunk_t msg)
348 {
349 imv_state_t *state;
350 imv_msg_t *in_msg;
351 TNC_Result result;
352
353 if (!this->agent->get_state(this->agent, id, &state))
354 {
355 return TNC_RESULT_FATAL;
356 }
357 in_msg = imv_msg_create_from_long_data(this->agent, state, id,
358 src_imc_id, dst_imv_id, msg_vid, msg_subtype, msg);
359 result = receive_msg(this, state, in_msg);
360 in_msg->destroy(in_msg);
361
362 return result;
363 }
364
365 /**
366 * Build an IETF Attribute Request attribute for missing attributes
367 */
368 static pa_tnc_attr_t* build_attr_request(uint32_t received)
369 {
370 pa_tnc_attr_t *attr;
371 ietf_attr_attr_request_t *attr_cast;
372
373 attr = ietf_attr_attr_request_create(PEN_RESERVED, 0);
374 attr_cast = (ietf_attr_attr_request_t*)attr;
375
376 if (!(received & IMV_ATTESTATION_ATTR_PRODUCT_INFO) ||
377 !(received & IMV_ATTESTATION_ATTR_STRING_VERSION))
378 {
379 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_PRODUCT_INFORMATION);
380 attr_cast->add(attr_cast, PEN_IETF, IETF_ATTR_STRING_VERSION);
381 }
382 if (!(received & IMV_ATTESTATION_ATTR_DEVICE_ID))
383 {
384 attr_cast->add(attr_cast, PEN_ITA, ITA_ATTR_DEVICE_ID);
385 }
386
387 return attr;
388 }
389
390 METHOD(imv_agent_if_t, batch_ending, TNC_Result,
391 private_imv_attestation_agent_t *this, TNC_ConnectionID id)
392 {
393 imv_msg_t *out_msg;
394 imv_state_t *state;
395 imv_session_t *session;
396 imv_attestation_state_t *attestation_state;
397 imv_attestation_handshake_state_t handshake_state;
398 imv_workitem_t *workitem;
399 TNC_IMV_Action_Recommendation rec;
400 TNC_IMV_Evaluation_Result eval;
401 TNC_IMVID imv_id;
402 TNC_Result result = TNC_RESULT_SUCCESS;
403 pts_t *pts;
404 int pid;
405 uint32_t actions;
406 enumerator_t *enumerator;
407
408 if (!this->agent->get_state(this->agent, id, &state))
409 {
410 return TNC_RESULT_FATAL;
411 }
412 attestation_state = (imv_attestation_state_t*)state;
413 pts = attestation_state->get_pts(attestation_state);
414 handshake_state = attestation_state->get_handshake_state(attestation_state);
415 actions = state->get_action_flags(state);
416 session = state->get_session(state);
417 imv_id = this->agent->get_id(this->agent);
418
419 /* exit if a recommendation has already been provided */
420 if (actions & IMV_ATTESTATION_REC)
421 {
422 return TNC_RESULT_SUCCESS;
423 }
424
425 /* send an IETF attribute request if no platform info was received */
426 if (!(actions & IMV_ATTESTATION_ATTR_REQ))
427 {
428 if ((actions & IMV_ATTESTATION_ATTR_MUST) != IMV_ATTESTATION_ATTR_MUST)
429 {
430 imv_msg_t *os_msg;
431
432 /* create attribute request for missing mandatory attributes */
433 os_msg = imv_msg_create(this->agent, state, id, imv_id,
434 TNC_IMCID_ANY, msg_types[1]);
435 os_msg->add_attribute(os_msg, build_attr_request(actions));
436 result = os_msg->send(os_msg, FALSE);
437 os_msg->destroy(os_msg);
438
439 if (result != TNC_RESULT_SUCCESS)
440 {
441 return result;
442 }
443 }
444 state->set_action_flags(state, IMV_ATTESTATION_ATTR_REQ);
445 }
446
447 if (!session->get_policy_started(session) &&
448 (actions & IMV_ATTESTATION_ATTR_PRODUCT_INFO) &&
449 (actions & IMV_ATTESTATION_ATTR_STRING_VERSION) &&
450 (actions & IMV_ATTESTATION_ATTR_DEVICE_ID))
451 {
452 if (imcv_db)
453 {
454 /* start the policy script */
455 if (!imcv_db->policy_script(imcv_db, session, TRUE))
456 {
457 DBG1(DBG_IMV, "error in policy script start");
458 }
459 }
460 else
461 {
462 DBG2(DBG_IMV, "no workitems available - no evaluation possible");
463 state->set_recommendation(state,
464 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
465 TNC_IMV_EVALUATION_RESULT_DONT_KNOW);
466 session->set_policy_started(session, TRUE);
467 }
468 }
469
470 if (handshake_state == IMV_ATTESTATION_STATE_INIT)
471 {
472 size_t max_attr_size = FILE_MEAS_MAX_ATTR_SIZE;
473 size_t max_seg_size;
474 seg_contract_t *contract;
475 seg_contract_manager_t *contracts;
476 pa_tnc_attr_t *attr;
477 pts_proto_caps_flag_t flags;
478 char buf[BUF_LEN];
479
480 out_msg = imv_msg_create(this->agent, state, id, imv_id, TNC_IMCID_ANY,
481 msg_types[0]);
482
483 /* Determine maximum PA-TNC attribute segment size */
484 max_seg_size = state->get_max_msg_len(state)
485 - PA_TNC_HEADER_SIZE
486 - PA_TNC_ATTR_HEADER_SIZE
487 - TCG_SEG_ATTR_SEG_ENV_HEADER;
488
489 /* Announce support of PA-TNC segmentation to IMC */
490 contract = seg_contract_create(msg_types[0], max_attr_size,
491 max_seg_size, TRUE, imv_id, FALSE);
492 contract->get_info_string(contract, buf, BUF_LEN, TRUE);
493 DBG2(DBG_IMV, "%s", buf);
494 contracts = state->get_contracts(state);
495 contracts->add_contract(contracts, contract);
496 attr = tcg_seg_attr_max_size_create(max_attr_size, max_seg_size, TRUE);
497 out_msg->add_attribute(out_msg, attr);
498
499 /* Send Request Protocol Capabilities attribute */
500 flags = pts->get_proto_caps(pts);
501 attr = tcg_pts_attr_proto_caps_create(flags, TRUE);
502 attr->set_noskip_flag(attr, TRUE);
503 out_msg->add_attribute(out_msg, attr);
504
505 /* Send Measurement Algorithms attribute */
506 attr = tcg_pts_attr_meas_algo_create(this->supported_algorithms, FALSE);
507 attr->set_noskip_flag(attr, TRUE);
508 out_msg->add_attribute(out_msg, attr);
509
510 attestation_state->set_handshake_state(attestation_state,
511 IMV_ATTESTATION_STATE_DISCOVERY);
512
513 /* send these initial PTS attributes and exit */
514 result = out_msg->send(out_msg, FALSE);
515 out_msg->destroy(out_msg);
516
517 return result;
518 }
519
520 /* exit if we are not ready yet for PTS measurements */
521 if (!(actions & IMV_ATTESTATION_ALGO))
522 {
523 return TNC_RESULT_SUCCESS;
524 }
525
526 session->get_session_id(session, &pid, NULL);
527 pts->set_platform_id(pts, pid);
528
529 /* create an empty out message - we might need it */
530 out_msg = imv_msg_create(this->agent, state, id, imv_id, TNC_IMCID_ANY,
531 msg_types[0]);
532
533 /* establish the PTS measurements to be taken */
534 if (!(actions & IMV_ATTESTATION_FILE_MEAS))
535 {
536 bool is_dir, no_workitems = TRUE;
537 uint32_t delimiter = SOLIDUS_UTF;
538 uint16_t request_id;
539 pa_tnc_attr_t *attr;
540 char *pathname;
541
542 attestation_state->set_handshake_state(attestation_state,
543 IMV_ATTESTATION_STATE_END);
544
545 enumerator = session->create_workitem_enumerator(session);
546 if (enumerator)
547 {
548 while (enumerator->enumerate(enumerator, &workitem))
549 {
550 if (workitem->get_imv_id(workitem) != TNC_IMVID_ANY)
551 {
552 continue;
553 }
554
555 switch (workitem->get_type(workitem))
556 {
557 case IMV_WORKITEM_FILE_REF_MEAS:
558 case IMV_WORKITEM_FILE_MEAS:
559 case IMV_WORKITEM_FILE_META:
560 is_dir = FALSE;
561 break;
562 case IMV_WORKITEM_DIR_REF_MEAS:
563 case IMV_WORKITEM_DIR_MEAS:
564 case IMV_WORKITEM_DIR_META:
565 is_dir = TRUE;
566 break;
567 case IMV_WORKITEM_TPM_ATTEST:
568 {
569 pts_component_t *comp;
570 pts_comp_func_name_t *comp_name;
571 bool no_d_flag, no_t_flag;
572 char result_str[BUF_LEN];
573
574 workitem->set_imv_id(workitem, imv_id);
575 no_workitems = FALSE;
576 no_d_flag = !(pts->get_proto_caps(pts) & PTS_PROTO_CAPS_D);
577 no_t_flag = !(pts->get_proto_caps(pts) & PTS_PROTO_CAPS_T);
578 if (no_d_flag || no_t_flag)
579 {
580 snprintf(result_str, BUF_LEN, "%s%s%s",
581 (no_t_flag) ? "no TPM available" : "",
582 (no_t_flag && no_d_flag) ? ", " : "",
583 (no_d_flag) ? "no DH nonce negotiation" : "");
584 eval = TNC_IMV_EVALUATION_RESULT_ERROR;
585 session->remove_workitem(session, enumerator);
586 rec = workitem->set_result(workitem, result_str, eval);
587 state->update_recommendation(state, rec, eval);
588 imcv_db->finalize_workitem(imcv_db, workitem);
589 workitem->destroy(workitem);
590 continue;
591 }
592
593 /* do TPM BIOS measurements */
594 if (strchr(workitem->get_arg_str(workitem), 'B'))
595 {
596 comp_name = pts_comp_func_name_create(PEN_ITA,
597 PTS_ITA_COMP_FUNC_NAME_IMA,
598 PTS_ITA_QUALIFIER_FLAG_KERNEL |
599 PTS_ITA_QUALIFIER_TYPE_TRUSTED);
600 comp = attestation_state->create_component(
601 attestation_state, comp_name,
602 0, this->pts_db);
603 if (!comp)
604 {
605 comp_name->log(comp_name, "unregistered ");
606 comp_name->destroy(comp_name);
607 }
608 }
609
610 /* do TPM IMA measurements */
611 if (strchr(workitem->get_arg_str(workitem), 'I'))
612 {
613 comp_name = pts_comp_func_name_create(PEN_ITA,
614 PTS_ITA_COMP_FUNC_NAME_IMA,
615 PTS_ITA_QUALIFIER_FLAG_KERNEL |
616 PTS_ITA_QUALIFIER_TYPE_OS);
617 comp = attestation_state->create_component(
618 attestation_state, comp_name,
619 0, this->pts_db);
620 if (!comp)
621 {
622 comp_name->log(comp_name, "unregistered ");
623 comp_name->destroy(comp_name);
624 }
625 }
626
627 /* do TPM TRUSTED BOOT measurements */
628 if (strchr(workitem->get_arg_str(workitem), 'T'))
629 {
630 comp_name = pts_comp_func_name_create(PEN_ITA,
631 PTS_ITA_COMP_FUNC_NAME_TBOOT,
632 PTS_ITA_QUALIFIER_FLAG_KERNEL |
633 PTS_ITA_QUALIFIER_TYPE_TRUSTED);
634 comp = attestation_state->create_component(
635 attestation_state, comp_name,
636 0, this->pts_db);
637 if (!comp)
638 {
639 comp_name->log(comp_name, "unregistered ");
640 comp_name->destroy(comp_name);
641 }
642 }
643 attestation_state->set_handshake_state(attestation_state,
644 IMV_ATTESTATION_STATE_NONCE_REQ);
645 continue;
646 }
647 default:
648 continue;
649 }
650
651 /* initiate file and directory measurements */
652 pathname = this->pts_db->get_pathname(this->pts_db, is_dir,
653 workitem->get_arg_int(workitem));
654 if (!pathname)
655 {
656 continue;
657 }
658 workitem->set_imv_id(workitem, imv_id);
659 no_workitems = FALSE;
660
661 if (workitem->get_type(workitem) == IMV_WORKITEM_FILE_META)
662 {
663 TNC_IMV_Action_Recommendation rec;
664 TNC_IMV_Evaluation_Result eval;
665 char result_str[BUF_LEN];
666
667 DBG2(DBG_IMV, "IMV %d requests metadata for %s '%s'",
668 imv_id, is_dir ? "directory" : "file", pathname);
669
670 /* currently just fire and forget metadata requests */
671 attr = tcg_pts_attr_req_file_meta_create(is_dir,
672 delimiter, pathname);
673 snprintf(result_str, BUF_LEN, "%s metadata requested",
674 is_dir ? "directory" : "file");
675 eval = TNC_IMV_EVALUATION_RESULT_COMPLIANT;
676 session->remove_workitem(session, enumerator);
677 rec = workitem->set_result(workitem, result_str, eval);
678 state->update_recommendation(state, rec, eval);
679 imcv_db->finalize_workitem(imcv_db, workitem);
680 workitem->destroy(workitem);
681 }
682 else
683 {
684 /* use lower 16 bits of the workitem ID as request ID */
685 request_id = workitem->get_id(workitem) & 0xffff;
686
687 DBG2(DBG_IMV, "IMV %d requests measurement %d for %s '%s'",
688 imv_id, request_id, is_dir ? "directory" : "file",
689 pathname);
690 attr = tcg_pts_attr_req_file_meas_create(is_dir, request_id,
691 delimiter, pathname);
692 }
693 free(pathname);
694 attr->set_noskip_flag(attr, TRUE);
695 out_msg->add_attribute(out_msg, attr);
696 }
697 enumerator->destroy(enumerator);
698
699 /* sent all file and directory measurement and metadata requests */
700 state->set_action_flags(state, IMV_ATTESTATION_FILE_MEAS);
701
702 if (no_workitems)
703 {
704 DBG2(DBG_IMV, "IMV %d has no workitems - "
705 "no evaluation requested", imv_id);
706 state->set_recommendation(state,
707 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
708 TNC_IMV_EVALUATION_RESULT_DONT_KNOW);
709 }
710 }
711 }
712
713 /* check the IMV state for the next PA-TNC attributes to send */
714 enumerator = session->create_workitem_enumerator(session);
715 while (enumerator->enumerate(enumerator, &workitem))
716 {
717 if (workitem->get_type(workitem) == IMV_WORKITEM_TPM_ATTEST)
718 {
719 if (!imv_attestation_build(out_msg, state,
720 this->supported_dh_groups, this->pts_db))
721 {
722 imv_reason_string_t *reason_string;
723 chunk_t result;
724 char *result_str;
725
726 reason_string = imv_reason_string_create("en", ", ");
727 attestation_state->add_comp_evid_reasons(attestation_state,
728 reason_string);
729 result = reason_string->get_encoding(reason_string);
730 result_str = strndup(result.ptr, result.len);
731 reason_string->destroy(reason_string);
732
733 eval = TNC_IMV_EVALUATION_RESULT_ERROR;
734 session->remove_workitem(session, enumerator);
735 rec = workitem->set_result(workitem, result_str, eval);
736 state->update_recommendation(state, rec, eval);
737 imcv_db->finalize_workitem(imcv_db, workitem);
738 }
739 break;
740 }
741 }
742 enumerator->destroy(enumerator);
743
744 /* finalized all workitems? */
745 if (session->get_policy_started(session) &&
746 session->get_workitem_count(session, imv_id) == 0 &&
747 attestation_state->get_handshake_state(attestation_state) ==
748 IMV_ATTESTATION_STATE_END)
749 {
750 result = out_msg->send_assessment(out_msg);
751 out_msg->destroy(out_msg);
752 state->set_action_flags(state, IMV_ATTESTATION_REC);
753
754 if (result != TNC_RESULT_SUCCESS)
755 {
756 return result;
757 }
758 return this->agent->provide_recommendation(this->agent, state);
759 }
760
761 /* send non-empty PA-TNC message with excl flag not set */
762 if (out_msg->get_attribute_count(out_msg))
763 {
764 result = out_msg->send(out_msg, FALSE);
765 }
766 out_msg->destroy(out_msg);
767
768 return result;
769 }
770
771 METHOD(imv_agent_if_t, solicit_recommendation, TNC_Result,
772 private_imv_attestation_agent_t *this, TNC_ConnectionID id)
773 {
774 TNC_IMVID imv_id;
775 imv_state_t *state;
776 imv_attestation_state_t *attestation_state;
777 imv_session_t *session;
778
779 if (!this->agent->get_state(this->agent, id, &state))
780 {
781 return TNC_RESULT_FATAL;
782 }
783 attestation_state = (imv_attestation_state_t*)state;
784 session = state->get_session(state);
785 imv_id = this->agent->get_id(this->agent);
786
787 if (imcv_db)
788 {
789 TNC_IMV_Evaluation_Result eval;
790 TNC_IMV_Action_Recommendation rec;
791 imv_workitem_t *workitem;
792 enumerator_t *enumerator;
793 int pending_file_meas = 0;
794 char *result_str;
795 chunk_t result_buf;
796 bio_writer_t *result;
797
798 enumerator = session->create_workitem_enumerator(session);
799 if (enumerator)
800 {
801 while (enumerator->enumerate(enumerator, &workitem))
802 {
803 if (workitem->get_imv_id(workitem) != imv_id)
804 {
805 continue;
806 }
807 result = bio_writer_create(128);
808
809 switch (workitem->get_type(workitem))
810 {
811 case IMV_WORKITEM_FILE_REF_MEAS:
812 case IMV_WORKITEM_FILE_MEAS:
813 case IMV_WORKITEM_DIR_REF_MEAS:
814 case IMV_WORKITEM_DIR_MEAS:
815 result_str = "pending file measurements";
816 pending_file_meas++;
817 break;
818 case IMV_WORKITEM_TPM_ATTEST:
819 attestation_state->finalize_components(attestation_state,
820 result);
821 result->write_data(result,
822 chunk_from_str("; pending component evidence"));
823 result->write_uint8(result, '\0');
824 result_buf = result->get_buf(result);
825 result_str = result_buf.ptr;
826 break;
827 default:
828 result->destroy(result);
829 continue;
830 }
831 session->remove_workitem(session, enumerator);
832 eval = TNC_IMV_EVALUATION_RESULT_ERROR;
833 rec = workitem->set_result(workitem, result_str, eval);
834 state->update_recommendation(state, rec, eval);
835 imcv_db->finalize_workitem(imcv_db, workitem);
836 workitem->destroy(workitem);
837 result->destroy(result);
838 }
839 enumerator->destroy(enumerator);
840
841 if (pending_file_meas)
842 {
843 DBG1(DBG_IMV, "failure due to %d pending file measurements",
844 pending_file_meas);
845 attestation_state->set_measurement_error(attestation_state,
846 IMV_ATTESTATION_ERROR_FILE_MEAS_PEND);
847 }
848 }
849 }
850 return this->agent->provide_recommendation(this->agent, state);
851 }
852
853 METHOD(imv_agent_if_t, destroy, void,
854 private_imv_attestation_agent_t *this)
855 {
856 if (this->pts_creds)
857 {
858 this->pts_credmgr->remove_set(this->pts_credmgr,
859 this->pts_creds->get_set(this->pts_creds));
860 this->pts_creds->destroy(this->pts_creds);
861 }
862 DESTROY_IF(this->pts_db);
863 DESTROY_IF(this->pts_credmgr);
864 DESTROY_IF(this->agent);
865 free(this);
866 }
867
868 /**
869 * Described in header.
870 */
871 imv_agent_if_t *imv_attestation_agent_create(const char *name, TNC_IMVID id,
872 TNC_Version *actual_version)
873 {
874 private_imv_attestation_agent_t *this;
875 imv_agent_t *agent;
876 char *hash_alg, *dh_group, *cadir;
877 bool mandatory_dh_groups;
878
879 agent = imv_agent_create(name, msg_types, countof(msg_types), id,
880 actual_version);
881 if (!agent)
882 {
883 return NULL;
884 }
885
886 hash_alg = lib->settings->get_str(lib->settings,
887 "%s.plugins.imv-attestation.hash_algorithm", "sha256", lib->ns);
888 dh_group = lib->settings->get_str(lib->settings,
889 "%s.plugins.imv-attestation.dh_group", "ecp256", lib->ns);
890 mandatory_dh_groups = lib->settings->get_bool(lib->settings,
891 "%s.plugins.imv-attestation.mandatory_dh_groups", TRUE, lib->ns);
892 cadir = lib->settings->get_str(lib->settings,
893 "%s.plugins.imv-attestation.cadir", NULL, lib->ns);
894
895 INIT(this,
896 .public = {
897 .bind_functions = _bind_functions,
898 .notify_connection_change = _notify_connection_change,
899 .receive_message = _receive_message,
900 .receive_message_long = _receive_message_long,
901 .batch_ending = _batch_ending,
902 .solicit_recommendation = _solicit_recommendation,
903 .destroy = _destroy,
904 },
905 .agent = agent,
906 .supported_algorithms = PTS_MEAS_ALGO_NONE,
907 .supported_dh_groups = PTS_DH_GROUP_NONE,
908 .pts_credmgr = credential_manager_create(),
909 .pts_creds = pts_creds_create(cadir),
910 .pts_db = pts_database_create(imcv_db),
911 );
912
913 if (!pts_meas_algo_probe(&this->supported_algorithms) ||
914 !pts_dh_group_probe(&this->supported_dh_groups, mandatory_dh_groups) ||
915 !pts_meas_algo_update(hash_alg, &this->supported_algorithms) ||
916 !pts_dh_group_update(dh_group, &this->supported_dh_groups))
917 {
918 destroy(this);
919 return NULL;
920 }
921
922 if (this->pts_creds)
923 {
924 this->pts_credmgr->add_set(this->pts_credmgr,
925 this->pts_creds->get_set(this->pts_creds));
926 }
927
928 return &this->public;
929 }