Wait for the arrival of the TCPG_PTS_DH_NONCE_PARAMS_RESP
[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 state->set_action_flags(state, IMV_ATTESTATION_DH_NONCE);
146 break;
147 }
148 case TCG_PTS_TPM_VERSION_INFO:
149 {
150 tcg_pts_attr_tpm_version_info_t *attr_cast;
151 chunk_t tpm_version_info;
152
153 attr_cast = (tcg_pts_attr_tpm_version_info_t*)attr;
154 tpm_version_info = attr_cast->get_tpm_version_info(attr_cast);
155 pts->set_tpm_version_info(pts, tpm_version_info);
156 break;
157 }
158 case TCG_PTS_AIK:
159 {
160 tcg_pts_attr_aik_t *attr_cast;
161 certificate_t *aik, *issuer;
162 public_key_t *public;
163 chunk_t keyid, keyid_hex, device_id;
164 int aik_id;
165 enumerator_t *e;
166 bool trusted = FALSE, trusted_chain = FALSE;
167
168 attr_cast = (tcg_pts_attr_aik_t*)attr;
169 aik = attr_cast->get_aik(attr_cast);
170 if (!aik)
171 {
172 DBG1(DBG_IMV, "AIK unavailable");
173 attestation_state->set_measurement_error(attestation_state,
174 IMV_ATTESTATION_ERROR_NO_TRUSTED_AIK);
175 break;
176 }
177
178 /* check trust into public key as stored in the database */
179 public = aik->get_public_key(aik);
180 public->get_fingerprint(public, KEYID_PUBKEY_INFO_SHA1, &keyid);
181 DBG1(DBG_IMV, "verifying AIK with keyid %#B", &keyid);
182 keyid_hex = chunk_to_hex(keyid, NULL, FALSE);
183 if (session->get_device_id(session, &device_id) &&
184 chunk_equals(keyid_hex, device_id))
185 {
186 trusted = session->get_device_trust(session);
187 }
188 else
189 {
190 DBG1(DBG_IMV, "device ID unknown or different from AIK keyid");
191 }
192 DBG1(DBG_IMV, "AIK public key is %strusted", trusted ? "" : "not ");
193 public->destroy(public);
194 chunk_free(&keyid_hex);
195
196 if (aik->get_type(aik) == CERT_X509)
197 {
198
199 e = pts_credmgr->create_trusted_enumerator(pts_credmgr,
200 KEY_ANY, aik->get_issuer(aik), FALSE);
201 while (e->enumerate(e, &issuer))
202 {
203 if (aik->issued_by(aik, issuer, NULL))
204 {
205 trusted_chain = TRUE;
206 break;
207 }
208 }
209 e->destroy(e);
210 DBG1(DBG_IMV, "AIK certificate is %strusted",
211 trusted_chain ? "" : "not ");
212 if (!trusted || !trusted_chain)
213 {
214 attestation_state->set_measurement_error(attestation_state,
215 IMV_ATTESTATION_ERROR_NO_TRUSTED_AIK);
216 break;
217 }
218 }
219 session->get_session_id(session, NULL, &aik_id);
220 pts->set_aik(pts, aik, aik_id);
221 break;
222 }
223 case TCG_PTS_FILE_MEAS:
224 {
225 TNC_IMV_Evaluation_Result eval;
226 TNC_IMV_Action_Recommendation rec;
227 tcg_pts_attr_file_meas_t *attr_cast;
228 uint16_t request_id;
229 int arg_int, file_count;
230 pts_meas_algorithms_t algo;
231 pts_file_meas_t *measurements;
232 imv_workitem_t *workitem, *found = NULL;
233 imv_workitem_type_t type;
234 char result_str[BUF_LEN];
235 bool is_dir, correct;
236 enumerator_t *enumerator;
237
238 eval = TNC_IMV_EVALUATION_RESULT_COMPLIANT;
239 algo = pts->get_meas_algorithm(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 pts->get_platform_id(pts),
293 algo, is_dir, arg_int);
294 if (!e)
295 {
296 eval = TNC_IMV_EVALUATION_RESULT_ERROR;
297 break;
298 }
299 correct = measurements->verify(measurements, e, is_dir);
300 if (!correct)
301 {
302 attestation_state->set_measurement_error(
303 attestation_state,
304 IMV_ATTESTATION_ERROR_FILE_MEAS_FAIL);
305 eval = TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MINOR;
306 }
307 e->destroy(e);
308
309 snprintf(result_str, BUF_LEN, "%s measurement%s correct",
310 is_dir ? "directory" : "file",
311 correct ? "" : " not");
312 break;
313 }
314 case IMV_WORKITEM_FILE_REF_MEAS:
315 case IMV_WORKITEM_DIR_REF_MEAS:
316 {
317 enumerator_t *e;
318 char *filename;
319 chunk_t measurement;
320
321 e = measurements->create_enumerator(measurements);
322 while (e->enumerate(e, &filename, &measurement))
323 {
324 if (pts_db->add_file_measurement(pts_db,
325 pts->get_platform_id(pts), algo, measurement,
326 filename, is_dir, arg_int) != SUCCESS)
327 {
328 eval = TNC_IMV_EVALUATION_RESULT_ERROR;
329 }
330 }
331 e->destroy(e);
332 snprintf(result_str, BUF_LEN, "%s reference measurement "
333 "successful", is_dir ? "directory" : "file");
334 break;
335 }
336 default:
337 break;
338 }
339
340 session->remove_workitem(session, enumerator);
341 enumerator->destroy(enumerator);
342 rec = found->set_result(found, result_str, eval);
343 state->update_recommendation(state, rec, eval);
344 imcv_db->finalize_workitem(imcv_db, found);
345 found->destroy(found);
346 }
347 else
348 {
349 measurements->check(measurements, pts_db,
350 pts->get_platform_id(pts), algo);
351 }
352 break;
353 }
354 case TCG_PTS_UNIX_FILE_META:
355 {
356 tcg_pts_attr_file_meta_t *attr_cast;
357 int file_count;
358 pts_file_meta_t *metadata;
359 pts_file_metadata_t *entry;
360 time_t created, modified, accessed;
361 bool utc = FALSE;
362 enumerator_t *e;
363
364 attr_cast = (tcg_pts_attr_file_meta_t*)attr;
365 metadata = attr_cast->get_metadata(attr_cast);
366 file_count = metadata->get_file_count(metadata);
367
368 DBG1(DBG_IMV, "metadata request returned %d file%s:",
369 file_count, (file_count == 1) ? "":"s");
370
371 e = metadata->create_enumerator(metadata);
372 while (e->enumerate(e, &entry))
373 {
374 DBG1(DBG_IMV, " '%s' (%"PRIu64" bytes)"
375 " owner %"PRIu64", group %"PRIu64", type %N",
376 entry->filename, entry->filesize, entry->owner,
377 entry->group, pts_file_type_names, entry->type);
378
379 created = entry->created;
380 modified = entry->modified;
381 accessed = entry->accessed;
382
383 DBG1(DBG_IMV, " created %T, modified %T, accessed %T",
384 &created, utc, &modified, utc, &accessed, utc);
385 }
386 e->destroy(e);
387 break;
388 }
389 case TCG_PTS_SIMPLE_COMP_EVID:
390 {
391 tcg_pts_attr_simple_comp_evid_t *attr_cast;
392 pts_comp_func_name_t *name;
393 pts_comp_evidence_t *evidence;
394 pts_component_t *comp;
395 uint32_t depth;
396 status_t status;
397
398 attr_cast = (tcg_pts_attr_simple_comp_evid_t*)attr;
399 evidence = attr_cast->get_comp_evidence(attr_cast);
400 name = evidence->get_comp_func_name(evidence, &depth);
401
402 comp = attestation_state->get_component(attestation_state, name);
403 if (!comp)
404 {
405 DBG1(DBG_IMV, " no entry found for component evidence request");
406 break;
407 }
408 status = comp->verify(comp, name->get_qualifier(name), pts, evidence);
409 if (status == VERIFY_ERROR || status == FAILED)
410 {
411 attestation_state->set_measurement_error(attestation_state,
412 IMV_ATTESTATION_ERROR_COMP_EVID_FAIL);
413 name->log(name, " measurement mismatch for ");
414 }
415 break;
416 }
417 case TCG_PTS_SIMPLE_EVID_FINAL:
418 {
419 tcg_pts_attr_simple_evid_final_t *attr_cast;
420 uint8_t flags;
421 pts_meas_algorithms_t comp_hash_algorithm;
422 chunk_t pcr_comp, tpm_quote_sig, evid_sig;
423 chunk_t pcr_composite, quote_info, result_buf;
424 imv_workitem_t *workitem;
425 imv_reason_string_t *reason_string;
426 enumerator_t *enumerator;
427 bool use_quote2, use_ver_info;
428 bio_writer_t *result;
429
430 attr_cast = (tcg_pts_attr_simple_evid_final_t*)attr;
431 flags = attr_cast->get_quote_info(attr_cast, &comp_hash_algorithm,
432 &pcr_comp, &tpm_quote_sig);
433
434 if (flags != PTS_SIMPLE_EVID_FINAL_NO)
435 {
436 use_quote2 = (flags == PTS_SIMPLE_EVID_FINAL_QUOTE_INFO2 ||
437 flags == PTS_SIMPLE_EVID_FINAL_QUOTE_INFO2_CAP_VER);
438 use_ver_info = (flags == PTS_SIMPLE_EVID_FINAL_QUOTE_INFO2_CAP_VER);
439
440 /* Construct PCR Composite and TPM Quote Info structures */
441 if (!pts->get_quote_info(pts, use_quote2, use_ver_info,
442 comp_hash_algorithm, &pcr_composite, &quote_info))
443 {
444 DBG1(DBG_IMV, "unable to construct TPM Quote Info");
445 return FALSE;
446 }
447
448 if (!chunk_equals(pcr_comp, pcr_composite))
449 {
450 DBG1(DBG_IMV, "received PCR Composite does not match "
451 "constructed one");
452 attestation_state->set_measurement_error(attestation_state,
453 IMV_ATTESTATION_ERROR_TPM_QUOTE_FAIL);
454 goto quote_error;
455 }
456 DBG2(DBG_IMV, "received PCR Composite matches constructed one");
457
458 if (!pts->verify_quote_signature(pts, quote_info, tpm_quote_sig))
459 {
460 attestation_state->set_measurement_error(attestation_state,
461 IMV_ATTESTATION_ERROR_TPM_QUOTE_FAIL);
462 goto quote_error;
463 }
464 DBG2(DBG_IMV, "TPM Quote Info signature verification successful");
465
466 quote_error:
467 free(pcr_composite.ptr);
468 free(quote_info.ptr);
469
470 /**
471 * Finalize any pending measurement registrations and check
472 * if all expected component measurements were received
473 */
474 result = bio_writer_create(128);
475 attestation_state->finalize_components(attestation_state,
476 result);
477
478 enumerator = session->create_workitem_enumerator(session);
479 while (enumerator->enumerate(enumerator, &workitem))
480 {
481 if (workitem->get_type(workitem) == IMV_WORKITEM_TPM_ATTEST)
482 {
483 TNC_IMV_Action_Recommendation rec;
484 TNC_IMV_Evaluation_Result eval;
485 uint32_t error;
486
487 error = attestation_state->get_measurement_error(
488 attestation_state);
489 if (error & (IMV_ATTESTATION_ERROR_COMP_EVID_FAIL |
490 IMV_ATTESTATION_ERROR_COMP_EVID_PEND |
491 IMV_ATTESTATION_ERROR_TPM_QUOTE_FAIL))
492 {
493 reason_string = imv_reason_string_create("en", ", ");
494 attestation_state->add_comp_evid_reasons(
495 attestation_state, reason_string);
496 result->write_data(result, chunk_from_str("; "));
497 result->write_data(result,
498 reason_string->get_encoding(reason_string));
499 reason_string->destroy(reason_string);
500 eval = TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MINOR;
501 }
502 else
503 {
504 eval = TNC_IMV_EVALUATION_RESULT_COMPLIANT;
505 }
506 session->remove_workitem(session, enumerator);
507
508 result->write_uint8(result, '\0');
509 result_buf = result->get_buf(result);
510 rec = workitem->set_result(workitem, result_buf.ptr,
511 eval);
512 state->update_recommendation(state, rec, eval);
513 imcv_db->finalize_workitem(imcv_db, workitem);
514 workitem->destroy(workitem);
515 attestation_state->set_handshake_state(attestation_state,
516 IMV_ATTESTATION_STATE_END);
517 break;
518 }
519 }
520 enumerator->destroy(enumerator);
521 result->destroy(result);
522 }
523
524 if (attr_cast->get_evid_sig(attr_cast, &evid_sig))
525 {
526 /** TODO: What to do with Evidence Signature */
527 DBG1(DBG_IMV, "this version of the Attestation IMV can not "
528 "handle Evidence Signatures");
529 }
530 break;
531 }
532
533 /* TODO: Not implemented yet */
534 case TCG_PTS_INTEG_MEAS_LOG:
535 /* Attributes using XML */
536 case TCG_PTS_TEMPL_REF_MANI_SET_META:
537 case TCG_PTS_VERIFICATION_RESULT:
538 case TCG_PTS_INTEG_REPORT:
539 /* On Windows only*/
540 case TCG_PTS_WIN_FILE_META:
541 case TCG_PTS_REGISTRY_VALUE:
542 /* Received on IMC side only*/
543 case TCG_PTS_REQ_PROTO_CAPS:
544 case TCG_PTS_DH_NONCE_PARAMS_REQ:
545 case TCG_PTS_DH_NONCE_FINISH:
546 case TCG_PTS_MEAS_ALGO:
547 case TCG_PTS_GET_TPM_VERSION_INFO:
548 case TCG_PTS_REQ_TEMPL_REF_MANI_SET_META:
549 case TCG_PTS_UPDATE_TEMPL_REF_MANI:
550 case TCG_PTS_GET_AIK:
551 case TCG_PTS_REQ_FUNC_COMP_EVID:
552 case TCG_PTS_GEN_ATTEST_EVID:
553 case TCG_PTS_REQ_FILE_META:
554 case TCG_PTS_REQ_FILE_MEAS:
555 case TCG_PTS_REQ_INTEG_MEAS_LOG:
556 default:
557 DBG1(DBG_IMV, "received unsupported attribute '%N'",
558 tcg_attr_names, attr->get_type(attr));
559 break;
560 }
561 return TRUE;
562 }
563