Separated IMV session management from IMV policy database
[strongswan.git] / src / libpts / plugins / imv_attestation / imv_attestation_process.c
1 /*
2 * Copyright (C) 2011-2012 Sansar Choinyambuu
3 * Copyright (C) 2011-2014 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_process.h"
21
22 #include <imcv.h>
23 #include <ietf/ietf_attr_pa_tnc_error.h>
24
25 #include <pts/pts.h>
26
27 #include <tcg/pts/tcg_pts_attr_aik.h>
28 #include <tcg/pts/tcg_pts_attr_dh_nonce_params_resp.h>
29 #include <tcg/pts/tcg_pts_attr_file_meas.h>
30 #include <tcg/pts/tcg_pts_attr_meas_algo.h>
31 #include <tcg/pts/tcg_pts_attr_proto_caps.h>
32 #include <tcg/pts/tcg_pts_attr_simple_comp_evid.h>
33 #include <tcg/pts/tcg_pts_attr_simple_evid_final.h>
34 #include <tcg/pts/tcg_pts_attr_tpm_version_info.h>
35 #include <tcg/pts/tcg_pts_attr_unix_file_meta.h>
36
37 #include <utils/debug.h>
38 #include <crypto/hashers/hasher.h>
39
40 #include <inttypes.h>
41
42 bool imv_attestation_process(pa_tnc_attr_t *attr, imv_msg_t *out_msg,
43 imv_state_t *state,
44 pts_meas_algorithms_t supported_algorithms,
45 pts_dh_group_t supported_dh_groups,
46 pts_database_t *pts_db,
47 credential_manager_t *pts_credmgr)
48 {
49 imv_session_t *session;
50 imv_attestation_state_t *attestation_state;
51 pen_type_t attr_type;
52 pts_t *pts;
53
54 session = state->get_session(state);
55 attestation_state = (imv_attestation_state_t*)state;
56 pts = attestation_state->get_pts(attestation_state);
57 attr_type = attr->get_type(attr);
58
59 switch (attr_type.type)
60 {
61 case TCG_PTS_PROTO_CAPS:
62 {
63 tcg_pts_attr_proto_caps_t *attr_cast;
64 pts_proto_caps_flag_t flags;
65
66 attr_cast = (tcg_pts_attr_proto_caps_t*)attr;
67 flags = attr_cast->get_flags(attr_cast);
68 pts->set_proto_caps(pts, flags);
69 break;
70 }
71 case TCG_PTS_MEAS_ALGO_SELECTION:
72 {
73 tcg_pts_attr_meas_algo_t *attr_cast;
74 pts_meas_algorithms_t selected_algorithm;
75
76 attr_cast = (tcg_pts_attr_meas_algo_t*)attr;
77 selected_algorithm = attr_cast->get_algorithms(attr_cast);
78 if (!(selected_algorithm & supported_algorithms))
79 {
80 DBG1(DBG_IMV, "PTS-IMC selected unsupported"
81 " measurement algorithm");
82 return FALSE;
83 }
84 pts->set_meas_algorithm(pts, selected_algorithm);
85 state->set_action_flags(state, IMV_ATTESTATION_ALGO);
86 break;
87 }
88 case TCG_PTS_DH_NONCE_PARAMS_RESP:
89 {
90 tcg_pts_attr_dh_nonce_params_resp_t *attr_cast;
91 int nonce_len, min_nonce_len;
92 pts_dh_group_t dh_group;
93 pts_meas_algorithms_t offered_algorithms, selected_algorithm;
94 chunk_t responder_value, responder_nonce;
95
96 attr_cast = (tcg_pts_attr_dh_nonce_params_resp_t*)attr;
97 responder_nonce = attr_cast->get_responder_nonce(attr_cast);
98
99 /* check compliance of responder nonce length */
100 min_nonce_len = lib->settings->get_int(lib->settings,
101 "%s.plugins.imv-attestation.min_nonce_len", 0, lib->ns);
102 nonce_len = responder_nonce.len;
103 if (nonce_len < PTS_MIN_NONCE_LEN ||
104 (min_nonce_len > 0 && nonce_len < min_nonce_len))
105 {
106 attr = pts_dh_nonce_error_create(
107 max(PTS_MIN_NONCE_LEN, min_nonce_len),
108 PTS_MAX_NONCE_LEN);
109 out_msg->add_attribute(out_msg, attr);
110 break;
111 }
112
113 dh_group = attr_cast->get_dh_group(attr_cast);
114 if (!(dh_group & supported_dh_groups))
115 {
116 DBG1(DBG_IMV, "PTS-IMC selected unsupported DH group");
117 return FALSE;
118 }
119
120 offered_algorithms = attr_cast->get_hash_algo_set(attr_cast);
121 selected_algorithm = pts_meas_algo_select(supported_algorithms,
122 offered_algorithms);
123 if (selected_algorithm == PTS_MEAS_ALGO_NONE)
124 {
125 attr = pts_hash_alg_error_create(supported_algorithms);
126 out_msg->add_attribute(out_msg, attr);
127 break;
128 }
129 pts->set_dh_hash_algorithm(pts, selected_algorithm);
130
131 if (!pts->create_dh_nonce(pts, dh_group, nonce_len))
132 {
133 return FALSE;
134 }
135
136 responder_value = attr_cast->get_responder_value(attr_cast);
137 pts->set_peer_public_value(pts, responder_value,
138 responder_nonce);
139
140 /* Calculate secret assessment value */
141 if (!pts->calculate_secret(pts))
142 {
143 return FALSE;
144 }
145 break;
146 }
147 case TCG_PTS_TPM_VERSION_INFO:
148 {
149 tcg_pts_attr_tpm_version_info_t *attr_cast;
150 chunk_t tpm_version_info;
151
152 attr_cast = (tcg_pts_attr_tpm_version_info_t*)attr;
153 tpm_version_info = attr_cast->get_tpm_version_info(attr_cast);
154 pts->set_tpm_version_info(pts, tpm_version_info);
155 break;
156 }
157 case TCG_PTS_AIK:
158 {
159 tcg_pts_attr_aik_t *attr_cast;
160 certificate_t *aik, *issuer;
161 public_key_t *public;
162 chunk_t keyid, keyid_hex, device_id;
163 enumerator_t *e;
164 bool trusted = FALSE, trusted_chain = FALSE;
165
166 attr_cast = (tcg_pts_attr_aik_t*)attr;
167 aik = attr_cast->get_aik(attr_cast);
168 if (!aik)
169 {
170 DBG1(DBG_IMV, "AIK unavailable");
171 attestation_state->set_measurement_error(attestation_state,
172 IMV_ATTESTATION_ERROR_NO_TRUSTED_AIK);
173 break;
174 }
175
176 /* check trust into public key as stored in the database */
177 public = aik->get_public_key(aik);
178 public->get_fingerprint(public, KEYID_PUBKEY_INFO_SHA1, &keyid);
179 DBG1(DBG_IMV, "verifying AIK with keyid %#B", &keyid);
180 keyid_hex = chunk_to_hex(keyid, NULL, FALSE);
181 if (session->get_device_id(session, &device_id) &&
182 chunk_equals(keyid_hex, device_id))
183 {
184 trusted = session->get_device_trust(session);
185 }
186 else
187 {
188 DBG1(DBG_IMV, "device ID unknown or different from AIK keyid");
189 }
190 DBG1(DBG_IMV, "AIK public key is %strusted", trusted ? "" : "not ");
191 public->destroy(public);
192 chunk_free(&keyid_hex);
193
194 if (aik->get_type(aik) == CERT_X509)
195 {
196
197 e = pts_credmgr->create_trusted_enumerator(pts_credmgr,
198 KEY_ANY, aik->get_issuer(aik), FALSE);
199 while (e->enumerate(e, &issuer))
200 {
201 if (aik->issued_by(aik, issuer, NULL))
202 {
203 trusted_chain = TRUE;
204 break;
205 }
206 }
207 e->destroy(e);
208 DBG1(DBG_IMV, "AIK certificate is %strusted",
209 trusted_chain ? "" : "not ");
210 if (!trusted || !trusted_chain)
211 {
212 attestation_state->set_measurement_error(attestation_state,
213 IMV_ATTESTATION_ERROR_NO_TRUSTED_AIK);
214 break;
215 }
216 }
217 pts->set_aik(pts, aik);
218 break;
219 }
220 case TCG_PTS_FILE_MEAS:
221 {
222 TNC_IMV_Evaluation_Result eval;
223 TNC_IMV_Action_Recommendation rec;
224 tcg_pts_attr_file_meas_t *attr_cast;
225 u_int16_t request_id;
226 int arg_int, file_count;
227 pts_meas_algorithms_t algo;
228 pts_file_meas_t *measurements;
229 imv_session_t *session;
230 imv_workitem_t *workitem, *found = NULL;
231 imv_workitem_type_t type;
232 char result_str[BUF_LEN], *platform_info;
233 bool is_dir, correct;
234 enumerator_t *enumerator;
235
236 eval = TNC_IMV_EVALUATION_RESULT_COMPLIANT;
237 session = state->get_session(state);
238 algo = pts->get_meas_algorithm(pts);
239 platform_info = pts->get_platform_info(pts);
240 attr_cast = (tcg_pts_attr_file_meas_t*)attr;
241 measurements = attr_cast->get_measurements(attr_cast);
242 request_id = measurements->get_request_id(measurements);
243 file_count = measurements->get_file_count(measurements);
244
245 DBG1(DBG_IMV, "measurement request %d returned %d file%s:",
246 request_id, file_count, (file_count == 1) ? "":"s");
247
248 if (request_id)
249 {
250 enumerator = session->create_workitem_enumerator(session);
251 while (enumerator->enumerate(enumerator, &workitem))
252 {
253 /* request ID consist of lower 16 bits of workitem ID */
254 if ((workitem->get_id(workitem) & 0xffff) == request_id)
255 {
256 found = workitem;
257 break;
258 }
259 }
260
261 if (!found)
262 {
263 DBG1(DBG_IMV, " no entry found for file measurement "
264 "request %d", request_id);
265 enumerator->destroy(enumerator);
266 break;
267 }
268 type = found->get_type(found);
269 arg_int = found->get_arg_int(found);
270
271 switch (type)
272 {
273 default:
274 case IMV_WORKITEM_FILE_REF_MEAS:
275 case IMV_WORKITEM_FILE_MEAS:
276 is_dir = FALSE;
277 break;
278 case IMV_WORKITEM_DIR_REF_MEAS:
279 case IMV_WORKITEM_DIR_MEAS:
280 is_dir = TRUE;
281 }
282
283 switch (type)
284 {
285 case IMV_WORKITEM_FILE_MEAS:
286 case IMV_WORKITEM_DIR_MEAS:
287 {
288 enumerator_t *e;
289
290 /* check hashes from database against measurements */
291 e = pts_db->create_file_hash_enumerator(pts_db,
292 platform_info, algo, is_dir, arg_int);
293 if (!e)
294 {
295 eval = TNC_IMV_EVALUATION_RESULT_ERROR;
296 break;
297 }
298 correct = measurements->verify(measurements, e, is_dir);
299 if (!correct)
300 {
301 attestation_state->set_measurement_error(
302 attestation_state,
303 IMV_ATTESTATION_ERROR_FILE_MEAS_FAIL);
304 eval = TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MINOR;
305 }
306 e->destroy(e);
307
308 snprintf(result_str, BUF_LEN, "%s measurement%s correct",
309 is_dir ? "directory" : "file",
310 correct ? "" : " not");
311 break;
312 }
313 case IMV_WORKITEM_FILE_REF_MEAS:
314 case IMV_WORKITEM_DIR_REF_MEAS:
315 {
316 enumerator_t *e;
317 char *filename;
318 chunk_t measurement;
319
320 e = measurements->create_enumerator(measurements);
321 while (e->enumerate(e, &filename, &measurement))
322 {
323 if (pts_db->add_file_measurement(pts_db,
324 platform_info, algo, measurement, filename,
325 is_dir, arg_int) != SUCCESS)
326 {
327 eval = TNC_IMV_EVALUATION_RESULT_ERROR;
328 }
329 }
330 e->destroy(e);
331 snprintf(result_str, BUF_LEN, "%s reference measurement "
332 "successful", is_dir ? "directory" : "file");
333 break;
334 }
335 default:
336 break;
337 }
338
339 session->remove_workitem(session, enumerator);
340 enumerator->destroy(enumerator);
341 rec = found->set_result(found, result_str, eval);
342 state->update_recommendation(state, rec, eval);
343 imcv_db->finalize_workitem(imcv_db, found);
344 found->destroy(found);
345 }
346 else
347 {
348 measurements->check(measurements, pts_db, platform_info, algo);
349 }
350 break;
351 }
352 case TCG_PTS_UNIX_FILE_META:
353 {
354 tcg_pts_attr_file_meta_t *attr_cast;
355 int file_count;
356 pts_file_meta_t *metadata;
357 pts_file_metadata_t *entry;
358 time_t created, modified, accessed;
359 bool utc = FALSE;
360 enumerator_t *e;
361
362 attr_cast = (tcg_pts_attr_file_meta_t*)attr;
363 metadata = attr_cast->get_metadata(attr_cast);
364 file_count = metadata->get_file_count(metadata);
365
366 DBG1(DBG_IMV, "metadata request returned %d file%s:",
367 file_count, (file_count == 1) ? "":"s");
368
369 e = metadata->create_enumerator(metadata);
370 while (e->enumerate(e, &entry))
371 {
372 DBG1(DBG_IMV, " '%s' (%"PRIu64" bytes)"
373 " owner %"PRIu64", group %"PRIu64", type %N",
374 entry->filename, entry->filesize, entry->owner,
375 entry->group, pts_file_type_names, entry->type);
376
377 created = entry->created;
378 modified = entry->modified;
379 accessed = entry->accessed;
380
381 DBG1(DBG_IMV, " created %T, modified %T, accessed %T",
382 &created, utc, &modified, utc, &accessed, utc);
383 }
384 e->destroy(e);
385 break;
386 }
387 case TCG_PTS_SIMPLE_COMP_EVID:
388 {
389 tcg_pts_attr_simple_comp_evid_t *attr_cast;
390 pts_comp_func_name_t *name;
391 pts_comp_evidence_t *evidence;
392 pts_component_t *comp;
393 u_int32_t depth;
394 status_t status;
395
396 attr_cast = (tcg_pts_attr_simple_comp_evid_t*)attr;
397 evidence = attr_cast->get_comp_evidence(attr_cast);
398 name = evidence->get_comp_func_name(evidence, &depth);
399
400 comp = attestation_state->get_component(attestation_state, name);
401 if (!comp)
402 {
403 DBG1(DBG_IMV, " no entry found for component evidence request");
404 break;
405 }
406 status = comp->verify(comp, name->get_qualifier(name), pts, evidence);
407 if (status == VERIFY_ERROR || status == FAILED)
408 {
409 attestation_state->set_measurement_error(attestation_state,
410 IMV_ATTESTATION_ERROR_COMP_EVID_FAIL);
411 name->log(name, " measurement mismatch for ");
412 }
413 break;
414 }
415 case TCG_PTS_SIMPLE_EVID_FINAL:
416 {
417 tcg_pts_attr_simple_evid_final_t *attr_cast;
418 u_int8_t flags;
419 pts_meas_algorithms_t comp_hash_algorithm;
420 chunk_t pcr_comp, tpm_quote_sig, evid_sig;
421 chunk_t pcr_composite, quote_info;
422 imv_session_t *session;
423 imv_workitem_t *workitem;
424 enumerator_t *enumerator;
425 bool use_quote2, use_ver_info;
426
427 attr_cast = (tcg_pts_attr_simple_evid_final_t*)attr;
428 flags = attr_cast->get_quote_info(attr_cast, &comp_hash_algorithm,
429 &pcr_comp, &tpm_quote_sig);
430
431 if (flags != PTS_SIMPLE_EVID_FINAL_NO)
432 {
433 use_quote2 = (flags == PTS_SIMPLE_EVID_FINAL_QUOTE_INFO2 ||
434 flags == PTS_SIMPLE_EVID_FINAL_QUOTE_INFO2_CAP_VER);
435 use_ver_info = (flags == PTS_SIMPLE_EVID_FINAL_QUOTE_INFO2_CAP_VER);
436
437 /* Construct PCR Composite and TPM Quote Info structures */
438 if (!pts->get_quote_info(pts, use_quote2, use_ver_info,
439 comp_hash_algorithm, &pcr_composite, &quote_info))
440 {
441 DBG1(DBG_IMV, "unable to construct TPM Quote Info");
442 return FALSE;
443 }
444
445 if (!chunk_equals(pcr_comp, pcr_composite))
446 {
447 DBG1(DBG_IMV, "received PCR Composite does not match "
448 "constructed one");
449 attestation_state->set_measurement_error(attestation_state,
450 IMV_ATTESTATION_ERROR_TPM_QUOTE_FAIL);
451 goto quote_error;
452 }
453 DBG2(DBG_IMV, "received PCR Composite matches constructed one");
454
455 if (!pts->verify_quote_signature(pts, quote_info, tpm_quote_sig))
456 {
457 attestation_state->set_measurement_error(attestation_state,
458 IMV_ATTESTATION_ERROR_TPM_QUOTE_FAIL);
459 goto quote_error;
460 }
461 DBG2(DBG_IMV, "TPM Quote Info signature verification successful");
462
463 quote_error:
464 free(pcr_composite.ptr);
465 free(quote_info.ptr);
466
467 /**
468 * Finalize any pending measurement registrations and check
469 * if all expected component measurements were received
470 */
471 attestation_state->finalize_components(attestation_state);
472
473 session = state->get_session(state);
474 enumerator = session->create_workitem_enumerator(session);
475 while (enumerator->enumerate(enumerator, &workitem))
476 {
477 if (workitem->get_type(workitem) == IMV_WORKITEM_TPM_ATTEST)
478 {
479 TNC_IMV_Action_Recommendation rec;
480 TNC_IMV_Evaluation_Result eval;
481 char *result_str;
482 u_int32_t error;
483
484 error = attestation_state->get_measurement_error(
485 attestation_state);
486 if (error & (IMV_ATTESTATION_ERROR_COMP_EVID_FAIL |
487 IMV_ATTESTATION_ERROR_COMP_EVID_PEND |
488 IMV_ATTESTATION_ERROR_TPM_QUOTE_FAIL))
489 {
490 imv_reason_string_t *reason_string;
491 chunk_t result;
492
493 reason_string = imv_reason_string_create("en", ", ");
494 attestation_state->add_comp_evid_reasons(
495 attestation_state, reason_string);
496 result = reason_string->get_encoding(reason_string);
497 result_str = strndup(result.ptr, result.len);
498 reason_string->destroy(reason_string);
499 eval = TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MINOR;
500 }
501 else
502 {
503 result_str = strdup("attestation successful");
504 eval = TNC_IMV_EVALUATION_RESULT_COMPLIANT;
505 }
506 session->remove_workitem(session, enumerator);
507 rec = workitem->set_result(workitem, result_str, eval);
508 state->update_recommendation(state, rec, eval);
509 imcv_db->finalize_workitem(imcv_db, workitem);
510 workitem->destroy(workitem);
511 free(result_str);
512 attestation_state->set_handshake_state(attestation_state,
513 IMV_ATTESTATION_STATE_END);
514 break;
515 }
516 }
517 enumerator->destroy(enumerator);
518 }
519
520 if (attr_cast->get_evid_sig(attr_cast, &evid_sig))
521 {
522 /** TODO: What to do with Evidence Signature */
523 DBG1(DBG_IMV, "this version of the Attestation IMV can not "
524 "handle Evidence Signatures");
525 }
526 break;
527 }
528
529 /* TODO: Not implemented yet */
530 case TCG_PTS_INTEG_MEAS_LOG:
531 /* Attributes using XML */
532 case TCG_PTS_TEMPL_REF_MANI_SET_META:
533 case TCG_PTS_VERIFICATION_RESULT:
534 case TCG_PTS_INTEG_REPORT:
535 /* On Windows only*/
536 case TCG_PTS_WIN_FILE_META:
537 case TCG_PTS_REGISTRY_VALUE:
538 /* Received on IMC side only*/
539 case TCG_PTS_REQ_PROTO_CAPS:
540 case TCG_PTS_DH_NONCE_PARAMS_REQ:
541 case TCG_PTS_DH_NONCE_FINISH:
542 case TCG_PTS_MEAS_ALGO:
543 case TCG_PTS_GET_TPM_VERSION_INFO:
544 case TCG_PTS_REQ_TEMPL_REF_MANI_SET_META:
545 case TCG_PTS_UPDATE_TEMPL_REF_MANI:
546 case TCG_PTS_GET_AIK:
547 case TCG_PTS_REQ_FUNC_COMP_EVID:
548 case TCG_PTS_GEN_ATTEST_EVID:
549 case TCG_PTS_REQ_FILE_META:
550 case TCG_PTS_REQ_FILE_MEAS:
551 case TCG_PTS_REQ_INTEG_MEAS_LOG:
552 default:
553 DBG1(DBG_IMV, "received unsupported attribute '%N'",
554 tcg_attr_names, attr->get_type(attr));
555 break;
556 }
557 return TRUE;
558 }
559