f2aa4ec823617c559e01836a2cfd86844ca6349e
[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 int aik_id;
164 enumerator_t *e;
165 bool trusted = FALSE, trusted_chain = FALSE;
166
167 attr_cast = (tcg_pts_attr_aik_t*)attr;
168 aik = attr_cast->get_aik(attr_cast);
169 if (!aik)
170 {
171 DBG1(DBG_IMV, "AIK unavailable");
172 attestation_state->set_measurement_error(attestation_state,
173 IMV_ATTESTATION_ERROR_NO_TRUSTED_AIK);
174 break;
175 }
176
177 /* check trust into public key as stored in the database */
178 public = aik->get_public_key(aik);
179 public->get_fingerprint(public, KEYID_PUBKEY_INFO_SHA1, &keyid);
180 DBG1(DBG_IMV, "verifying AIK with keyid %#B", &keyid);
181 keyid_hex = chunk_to_hex(keyid, NULL, FALSE);
182 if (session->get_device_id(session, &device_id) &&
183 chunk_equals(keyid_hex, device_id))
184 {
185 trusted = session->get_device_trust(session);
186 }
187 else
188 {
189 DBG1(DBG_IMV, "device ID unknown or different from AIK keyid");
190 }
191 DBG1(DBG_IMV, "AIK public key is %strusted", trusted ? "" : "not ");
192 public->destroy(public);
193 chunk_free(&keyid_hex);
194
195 if (aik->get_type(aik) == CERT_X509)
196 {
197
198 e = pts_credmgr->create_trusted_enumerator(pts_credmgr,
199 KEY_ANY, aik->get_issuer(aik), FALSE);
200 while (e->enumerate(e, &issuer))
201 {
202 if (aik->issued_by(aik, issuer, NULL))
203 {
204 trusted_chain = TRUE;
205 break;
206 }
207 }
208 e->destroy(e);
209 DBG1(DBG_IMV, "AIK certificate is %strusted",
210 trusted_chain ? "" : "not ");
211 if (!trusted || !trusted_chain)
212 {
213 attestation_state->set_measurement_error(attestation_state,
214 IMV_ATTESTATION_ERROR_NO_TRUSTED_AIK);
215 break;
216 }
217 }
218 session->get_session_id(session, NULL, &aik_id);
219 pts->set_aik(pts, aik, aik_id);
220 break;
221 }
222 case TCG_PTS_FILE_MEAS:
223 {
224 TNC_IMV_Evaluation_Result eval;
225 TNC_IMV_Action_Recommendation rec;
226 tcg_pts_attr_file_meas_t *attr_cast;
227 uint16_t request_id;
228 int arg_int, file_count;
229 pts_meas_algorithms_t algo;
230 pts_file_meas_t *measurements;
231 imv_workitem_t *workitem, *found = NULL;
232 imv_workitem_type_t type;
233 char result_str[BUF_LEN];
234 bool is_dir, correct;
235 enumerator_t *enumerator;
236
237 eval = TNC_IMV_EVALUATION_RESULT_COMPLIANT;
238 algo = pts->get_meas_algorithm(pts);
239 attr_cast = (tcg_pts_attr_file_meas_t*)attr;
240 measurements = attr_cast->get_measurements(attr_cast);
241 request_id = measurements->get_request_id(measurements);
242 file_count = measurements->get_file_count(measurements);
243
244 DBG1(DBG_IMV, "measurement request %d returned %d file%s:",
245 request_id, file_count, (file_count == 1) ? "":"s");
246
247 if (request_id)
248 {
249 enumerator = session->create_workitem_enumerator(session);
250 while (enumerator->enumerate(enumerator, &workitem))
251 {
252 /* request ID consist of lower 16 bits of workitem ID */
253 if ((workitem->get_id(workitem) & 0xffff) == request_id)
254 {
255 found = workitem;
256 break;
257 }
258 }
259
260 if (!found)
261 {
262 DBG1(DBG_IMV, " no entry found for file measurement "
263 "request %d", request_id);
264 enumerator->destroy(enumerator);
265 break;
266 }
267 type = found->get_type(found);
268 arg_int = found->get_arg_int(found);
269
270 switch (type)
271 {
272 default:
273 case IMV_WORKITEM_FILE_REF_MEAS:
274 case IMV_WORKITEM_FILE_MEAS:
275 is_dir = FALSE;
276 break;
277 case IMV_WORKITEM_DIR_REF_MEAS:
278 case IMV_WORKITEM_DIR_MEAS:
279 is_dir = TRUE;
280 }
281
282 switch (type)
283 {
284 case IMV_WORKITEM_FILE_MEAS:
285 case IMV_WORKITEM_DIR_MEAS:
286 {
287 enumerator_t *e;
288
289 /* check hashes from database against measurements */
290 e = pts_db->create_file_hash_enumerator(pts_db,
291 pts->get_platform_id(pts),
292 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 pts->get_platform_id(pts), algo, measurement,
325 filename, 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,
349 pts->get_platform_id(pts), algo);
350 }
351 break;
352 }
353 case TCG_PTS_UNIX_FILE_META:
354 {
355 tcg_pts_attr_file_meta_t *attr_cast;
356 int file_count;
357 pts_file_meta_t *metadata;
358 pts_file_metadata_t *entry;
359 time_t created, modified, accessed;
360 bool utc = FALSE;
361 enumerator_t *e;
362
363 attr_cast = (tcg_pts_attr_file_meta_t*)attr;
364 metadata = attr_cast->get_metadata(attr_cast);
365 file_count = metadata->get_file_count(metadata);
366
367 DBG1(DBG_IMV, "metadata request returned %d file%s:",
368 file_count, (file_count == 1) ? "":"s");
369
370 e = metadata->create_enumerator(metadata);
371 while (e->enumerate(e, &entry))
372 {
373 DBG1(DBG_IMV, " '%s' (%"PRIu64" bytes)"
374 " owner %"PRIu64", group %"PRIu64", type %N",
375 entry->filename, entry->filesize, entry->owner,
376 entry->group, pts_file_type_names, entry->type);
377
378 created = entry->created;
379 modified = entry->modified;
380 accessed = entry->accessed;
381
382 DBG1(DBG_IMV, " created %T, modified %T, accessed %T",
383 &created, utc, &modified, utc, &accessed, utc);
384 }
385 e->destroy(e);
386 break;
387 }
388 case TCG_PTS_SIMPLE_COMP_EVID:
389 {
390 tcg_pts_attr_simple_comp_evid_t *attr_cast;
391 pts_comp_func_name_t *name;
392 pts_comp_evidence_t *evidence;
393 pts_component_t *comp;
394 uint32_t depth;
395 status_t status;
396
397 attr_cast = (tcg_pts_attr_simple_comp_evid_t*)attr;
398 evidence = attr_cast->get_comp_evidence(attr_cast);
399 name = evidence->get_comp_func_name(evidence, &depth);
400
401 comp = attestation_state->get_component(attestation_state, name);
402 if (!comp)
403 {
404 DBG1(DBG_IMV, " no entry found for component evidence request");
405 break;
406 }
407 status = comp->verify(comp, name->get_qualifier(name), pts, evidence);
408 if (status == VERIFY_ERROR || status == FAILED)
409 {
410 attestation_state->set_measurement_error(attestation_state,
411 IMV_ATTESTATION_ERROR_COMP_EVID_FAIL);
412 name->log(name, " measurement mismatch for ");
413 }
414 break;
415 }
416 case TCG_PTS_SIMPLE_EVID_FINAL:
417 {
418 tcg_pts_attr_simple_evid_final_t *attr_cast;
419 uint8_t flags;
420 pts_meas_algorithms_t comp_hash_algorithm;
421 chunk_t pcr_comp, tpm_quote_sig, evid_sig;
422 chunk_t pcr_composite, quote_info, result_buf;
423 imv_workitem_t *workitem;
424 imv_reason_string_t *reason_string;
425 enumerator_t *enumerator;
426 bool use_quote2, use_ver_info;
427 bio_writer_t *result;
428
429 attr_cast = (tcg_pts_attr_simple_evid_final_t*)attr;
430 flags = attr_cast->get_quote_info(attr_cast, &comp_hash_algorithm,
431 &pcr_comp, &tpm_quote_sig);
432
433 if (flags != PTS_SIMPLE_EVID_FINAL_NO)
434 {
435 use_quote2 = (flags == PTS_SIMPLE_EVID_FINAL_QUOTE_INFO2 ||
436 flags == PTS_SIMPLE_EVID_FINAL_QUOTE_INFO2_CAP_VER);
437 use_ver_info = (flags == PTS_SIMPLE_EVID_FINAL_QUOTE_INFO2_CAP_VER);
438
439 /* Construct PCR Composite and TPM Quote Info structures */
440 if (!pts->get_quote_info(pts, use_quote2, use_ver_info,
441 comp_hash_algorithm, &pcr_composite, &quote_info))
442 {
443 DBG1(DBG_IMV, "unable to construct TPM Quote Info");
444 return FALSE;
445 }
446
447 if (!chunk_equals(pcr_comp, pcr_composite))
448 {
449 DBG1(DBG_IMV, "received PCR Composite does not match "
450 "constructed one");
451 attestation_state->set_measurement_error(attestation_state,
452 IMV_ATTESTATION_ERROR_TPM_QUOTE_FAIL);
453 goto quote_error;
454 }
455 DBG2(DBG_IMV, "received PCR Composite matches constructed one");
456
457 if (!pts->verify_quote_signature(pts, quote_info, tpm_quote_sig))
458 {
459 attestation_state->set_measurement_error(attestation_state,
460 IMV_ATTESTATION_ERROR_TPM_QUOTE_FAIL);
461 goto quote_error;
462 }
463 DBG2(DBG_IMV, "TPM Quote Info signature verification successful");
464
465 quote_error:
466 free(pcr_composite.ptr);
467 free(quote_info.ptr);
468
469 /**
470 * Finalize any pending measurement registrations and check
471 * if all expected component measurements were received
472 */
473 result = bio_writer_create(128);
474 attestation_state->finalize_components(attestation_state,
475 result);
476
477 enumerator = session->create_workitem_enumerator(session);
478 while (enumerator->enumerate(enumerator, &workitem))
479 {
480 if (workitem->get_type(workitem) == IMV_WORKITEM_TPM_ATTEST)
481 {
482 TNC_IMV_Action_Recommendation rec;
483 TNC_IMV_Evaluation_Result eval;
484 uint32_t error;
485
486 error = attestation_state->get_measurement_error(
487 attestation_state);
488 if (error & (IMV_ATTESTATION_ERROR_COMP_EVID_FAIL |
489 IMV_ATTESTATION_ERROR_COMP_EVID_PEND |
490 IMV_ATTESTATION_ERROR_TPM_QUOTE_FAIL))
491 {
492 reason_string = imv_reason_string_create("en", ", ");
493 attestation_state->add_comp_evid_reasons(
494 attestation_state, reason_string);
495 result->write_data(result, chunk_from_str("; "));
496 result->write_data(result,
497 reason_string->get_encoding(reason_string));
498 reason_string->destroy(reason_string);
499 eval = TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MINOR;
500 }
501 else
502 {
503 eval = TNC_IMV_EVALUATION_RESULT_COMPLIANT;
504 }
505 session->remove_workitem(session, enumerator);
506
507 result->write_uint8(result, '\0');
508 result_buf = result->get_buf(result);
509 rec = workitem->set_result(workitem, result_buf.ptr,
510 eval);
511 state->update_recommendation(state, rec, eval);
512 imcv_db->finalize_workitem(imcv_db, workitem);
513 workitem->destroy(workitem);
514 attestation_state->set_handshake_state(attestation_state,
515 IMV_ATTESTATION_STATE_END);
516 break;
517 }
518 }
519 enumerator->destroy(enumerator);
520 result->destroy(result);
521 }
522
523 if (attr_cast->get_evid_sig(attr_cast, &evid_sig))
524 {
525 /** TODO: What to do with Evidence Signature */
526 DBG1(DBG_IMV, "this version of the Attestation IMV can not "
527 "handle Evidence Signatures");
528 }
529 break;
530 }
531
532 /* TODO: Not implemented yet */
533 case TCG_PTS_INTEG_MEAS_LOG:
534 /* Attributes using XML */
535 case TCG_PTS_TEMPL_REF_MANI_SET_META:
536 case TCG_PTS_VERIFICATION_RESULT:
537 case TCG_PTS_INTEG_REPORT:
538 /* On Windows only*/
539 case TCG_PTS_WIN_FILE_META:
540 case TCG_PTS_REGISTRY_VALUE:
541 /* Received on IMC side only*/
542 case TCG_PTS_REQ_PROTO_CAPS:
543 case TCG_PTS_DH_NONCE_PARAMS_REQ:
544 case TCG_PTS_DH_NONCE_FINISH:
545 case TCG_PTS_MEAS_ALGO:
546 case TCG_PTS_GET_TPM_VERSION_INFO:
547 case TCG_PTS_REQ_TEMPL_REF_MANI_SET_META:
548 case TCG_PTS_UPDATE_TEMPL_REF_MANI:
549 case TCG_PTS_GET_AIK:
550 case TCG_PTS_REQ_FUNC_COMP_EVID:
551 case TCG_PTS_GEN_ATTEST_EVID:
552 case TCG_PTS_REQ_FILE_META:
553 case TCG_PTS_REQ_FILE_MEAS:
554 case TCG_PTS_REQ_INTEG_MEAS_LOG:
555 default:
556 DBG1(DBG_IMV, "received unsupported attribute '%N'",
557 tcg_attr_names, attr->get_type(attr));
558 break;
559 }
560 return TRUE;
561 }
562