2 * Copyright (C) 2011 Sansar Choinyambuu
3 * HSR Hochschule fuer Technik Rapperswil
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>.
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
16 #include "imv_attestation_process.h"
18 #include <ietf/ietf_attr_pa_tnc_error.h>
22 #include <tcg/tcg_pts_attr_aik.h>
23 #include <tcg/tcg_pts_attr_dh_nonce_params_resp.h>
24 #include <tcg/tcg_pts_attr_file_meas.h>
25 #include <tcg/tcg_pts_attr_meas_algo.h>
26 #include <tcg/tcg_pts_attr_proto_caps.h>
27 #include <tcg/tcg_pts_attr_simple_comp_evid.h>
28 #include <tcg/tcg_pts_attr_simple_evid_final.h>
29 #include <tcg/tcg_pts_attr_tpm_version_info.h>
30 #include <tcg/tcg_pts_attr_unix_file_meta.h>
33 #include <crypto/hashers/hasher.h>
37 bool imv_attestation_process(pa_tnc_attr_t
*attr
, linked_list_t
*attr_list
,
38 imv_attestation_state_t
*attestation_state
,
39 pts_meas_algorithms_t supported_algorithms
,
40 pts_dh_group_t supported_dh_groups
,
41 pts_database_t
*pts_db
,
42 credential_manager_t
*pts_credmgr
)
47 pts
= attestation_state
->get_pts(attestation_state
);
49 switch (attr
->get_type(attr
))
51 case TCG_PTS_PROTO_CAPS
:
53 tcg_pts_attr_proto_caps_t
*attr_cast
;
54 pts_proto_caps_flag_t flags
;
56 attr_cast
= (tcg_pts_attr_proto_caps_t
*)attr
;
57 flags
= attr_cast
->get_flags(attr_cast
);
58 pts
->set_proto_caps(pts
, flags
);
61 case TCG_PTS_MEAS_ALGO_SELECTION
:
63 tcg_pts_attr_meas_algo_t
*attr_cast
;
64 pts_meas_algorithms_t selected_algorithm
;
66 attr_cast
= (tcg_pts_attr_meas_algo_t
*)attr
;
67 selected_algorithm
= attr_cast
->get_algorithms(attr_cast
);
68 if (!(selected_algorithm
& supported_algorithms
))
70 DBG1(DBG_IMV
, "PTS-IMC selected unsupported"
71 " measurement algorithm");
74 pts
->set_meas_algorithm(pts
, selected_algorithm
);
77 case TCG_PTS_DH_NONCE_PARAMS_RESP
:
79 tcg_pts_attr_dh_nonce_params_resp_t
*attr_cast
;
80 int nonce_len
, min_nonce_len
;
81 pts_dh_group_t dh_group
;
82 pts_meas_algorithms_t offered_algorithms
, selected_algorithm
;
83 chunk_t responder_value
, responder_nonce
;
85 attr_cast
= (tcg_pts_attr_dh_nonce_params_resp_t
*)attr
;
86 responder_nonce
= attr_cast
->get_responder_nonce(attr_cast
);
88 /* check compliance of responder nonce length */
89 min_nonce_len
= lib
->settings
->get_int(lib
->settings
,
90 "libimcv.plugins.imv-attestation.min_nonce_len", 0);
91 nonce_len
= responder_nonce
.len
;
92 if (nonce_len
< PTS_MIN_NONCE_LEN
||
93 (min_nonce_len
> 0 && nonce_len
< min_nonce_len
))
95 attr
= pts_dh_nonce_error_create(
96 max(PTS_MIN_NONCE_LEN
, min_nonce_len
),
98 attr_list
->insert_last(attr_list
, attr
);
102 dh_group
= attr_cast
->get_dh_group(attr_cast
);
103 if (!(dh_group
& supported_dh_groups
))
105 DBG1(DBG_IMV
, "PTS-IMC selected unsupported DH group");
109 offered_algorithms
= attr_cast
->get_hash_algo_set(attr_cast
);
110 selected_algorithm
= pts_meas_algo_select(supported_algorithms
,
112 if (selected_algorithm
== PTS_MEAS_ALGO_NONE
)
114 attr
= pts_hash_alg_error_create(supported_algorithms
);
115 attr_list
->insert_last(attr_list
, attr
);
118 pts
->set_dh_hash_algorithm(pts
, selected_algorithm
);
120 if (!pts
->create_dh_nonce(pts
, dh_group
, nonce_len
))
125 responder_value
= attr_cast
->get_responder_value(attr_cast
);
126 pts
->set_peer_public_value(pts
, responder_value
,
129 /* Calculate secret assessment value */
130 if (!pts
->calculate_secret(pts
))
136 case TCG_PTS_TPM_VERSION_INFO
:
138 tcg_pts_attr_tpm_version_info_t
*attr_cast
;
139 chunk_t tpm_version_info
;
141 attr_cast
= (tcg_pts_attr_tpm_version_info_t
*)attr
;
142 tpm_version_info
= attr_cast
->get_tpm_version_info(attr_cast
);
143 pts
->set_tpm_version_info(pts
, tpm_version_info
);
148 tcg_pts_attr_aik_t
*attr_cast
;
149 certificate_t
*aik
, *issuer
;
151 bool trusted
= FALSE
;
153 attr_cast
= (tcg_pts_attr_aik_t
*)attr
;
154 aik
= attr_cast
->get_aik(attr_cast
);
157 DBG1(DBG_IMV
, "AIK unavailable");
160 if (aik
->get_type(aik
) == CERT_X509
)
162 DBG1(DBG_IMV
, "verifying AIK certificate");
163 e
= pts_credmgr
->create_trusted_enumerator(pts_credmgr
,
164 KEY_ANY
, aik
->get_issuer(aik
), FALSE
);
165 while (e
->enumerate(e
, &issuer
))
167 if (aik
->issued_by(aik
, issuer
))
174 DBG1(DBG_IMV
, "AIK certificate is %strusted",
175 trusted ?
"" : "not ");
177 pts
->set_aik(pts
, aik
);
180 case TCG_PTS_SIMPLE_COMP_EVID
:
182 tcg_pts_attr_simple_comp_evid_t
*attr_cast
;
183 bool pcr_info_inclided
;
184 pts_attr_simple_comp_evid_flag_t flags
;
185 u_int32_t depth
, comp_vendor_id
, extended_pcr
;
186 u_int8_t family
, measurement_type
;
187 pts_qualifier_t qualifier
;
188 pts_funct_comp_name_t name
;
189 pts_meas_algorithms_t hash_algorithm
;
190 pts_pcr_transform_t transformation
;
191 chunk_t measurement_time
, policy_uri
;
192 chunk_t pcr_before
, pcr_after
, measurement
;
194 attr_cast
= (tcg_pts_attr_simple_comp_evid_t
*)attr
;
195 attr_info
= attr
->get_value(attr
);
197 pcr_info_inclided
= attr_cast
->is_pcr_info_included(attr_cast
);
198 flags
= attr_cast
->get_flags(attr_cast
);
199 depth
= attr_cast
->get_sub_component_depth(attr_cast
);
200 /* TODO: Implement check of components with its sub-components */
203 DBG1(DBG_IMV
, "Current version of Attestation IMV does not"
204 " support sub component measurement deeper than zero");
206 comp_vendor_id
= attr_cast
->get_spec_comp_funct_name_vendor_id(
208 if (comp_vendor_id
!= PEN_TCG
)
210 DBG1(DBG_IMV
, "Current version of Attestation IMV supports"
211 "only functional component namings by TCG ");
214 family
= attr_cast
->get_family(attr_cast
);
217 attr
= ietf_attr_pa_tnc_error_create(PEN_TCG
,
218 TCG_PTS_INVALID_NAME_FAM
, attr_info
);
219 attr_list
->insert_last(attr_list
, attr
);
222 qualifier
= attr_cast
->get_qualifier(attr_cast
);
224 /* Check if Unknown or Wildcard was set for qualifier */
225 if (qualifier
.kernel
&& qualifier
.sub_component
&&
226 (qualifier
.type
& PTS_FUNC_COMP_TYPE_ALL
))
228 DBG1(DBG_IMV
, "Wildcard was set for the qualifier "
229 "of functional component");
232 else if (!qualifier
.kernel
&& !qualifier
.sub_component
&&
233 (qualifier
.type
& PTS_FUNC_COMP_TYPE_UNKNOWN
))
235 DBG1(DBG_IMV
, "Unknown feature was set for the qualifier "
236 "of functional component");
241 /* TODO: Implement what todo with received qualifier */
244 name
= attr_cast
->get_comp_funct_name(attr_cast
);
245 measurement_type
= attr_cast
->get_measurement_type(attr_cast
);
246 hash_algorithm
= attr_cast
->get_hash_algorithm(attr_cast
);
247 transformation
= attr_cast
->get_pcr_trans(attr_cast
);
248 measurement_time
= attr_cast
->get_measurement_time(attr_cast
);
250 /* Call getters of optional fields when corresponding flag is set */
251 if (pcr_info_inclided
)
255 extended_pcr
= attr_cast
->get_extended_pcr(attr_cast
);
256 pcr_before
= attr_cast
->get_pcr_before_value(attr_cast
);
257 pcr_after
= attr_cast
->get_pcr_after_value(attr_cast
);
258 measurement
= attr_cast
->get_comp_measurement(attr_cast
);
260 DBG4(DBG_IMV
,"PCR: %d was extended with %B",
261 extended_pcr
, &measurement
);
262 DBG4(DBG_IMV
,"PCR: %d before value: %B",
263 extended_pcr
, &pcr_before
);
264 DBG4(DBG_IMV
,"PCR: %d after value: %B",
265 extended_pcr
, &pcr_after
);
267 entry
= malloc_thing(pcr_entry_t
);
268 entry
->pcr_number
= extended_pcr
;
269 strncpy(entry
->pcr_value
, pcr_after
.ptr
, PCR_LEN
);
270 pts
->add_pcr_entry(pts
, entry
);
273 if (flags
!= PTS_SIMPLE_COMP_EVID_FLAG_NO_VALID
)
275 policy_uri
= attr_cast
->get_policy_uri(attr_cast
);
276 DBG1(DBG_IMV
, "This version of Attestation IMV can not handle"
277 " Verification Policies");
282 case TCG_PTS_SIMPLE_EVID_FINAL
:
284 tcg_pts_attr_simple_evid_final_t
*attr_cast
;
285 pts_simple_evid_final_flag_t flags
;
287 chunk_t tpm_quote_sign
;
289 bool evid_signature_included
;
291 /** TODO: Ignoring Composite Hash Algorithm field
292 * No flag defined which indicates the precense of it
294 attr_cast
= (tcg_pts_attr_simple_evid_final_t
*)attr
;
295 evid_signature_included
= attr_cast
->is_evid_sign_included(attr_cast
);
296 flags
= attr_cast
->get_flags(attr_cast
);
298 if ((flags
== PTS_SIMPLE_EVID_FINAL_FLAG_TPM_QUOTE_INFO2
) ||
299 (flags
== PTS_SIMPLE_EVID_FINAL_FLAG_TPM_QUOTE_INFO2_CAP_VER
))
301 DBG1(DBG_IMV
, "This version of Attestation IMV can not handle"
302 " TPM Quote Info2 structure");
305 if (flags
== PTS_SIMPLE_EVID_FINAL_FLAG_TPM_QUOTE_INFO
)
307 chunk_t pcr_composite
, quote_info
, quote_digest
;
309 pcr_comp
= attr_cast
->get_pcr_comp(attr_cast
);
310 tpm_quote_sign
= attr_cast
->get_tpm_quote_sign(attr_cast
);
312 /* Construct PCR Composite and TPM Quote Info structures*/
313 if (!pts
->get_quote_info(pts
, &pcr_composite
, "e_info
))
315 DBG1(DBG_IMV
, "unable to contruct TPM Quote Info");
319 /* Check calculated PCR composite structure matches with received */
320 if (pcr_comp
.ptr
&& !chunk_equals(pcr_comp
, pcr_composite
))
322 DBG1(DBG_IMV
, "received PCR Compsosite didn't match"
323 " with constructed");
324 chunk_clear(&pcr_composite
);
325 chunk_clear("e_info
);
328 DBG2(DBG_IMV
, "received PCR Composite matches with constructed");
329 chunk_clear(&pcr_composite
);
331 /* SHA1(TPM Quote Info) expected from IMC */
332 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, HASH_SHA1
);
333 hasher
->allocate_hash(hasher
, quote_info
, "e_digest
);
334 hasher
->destroy(hasher
);
335 chunk_clear("e_info
);
337 if (tpm_quote_sign
.ptr
&&
338 !pts
->verify_quote_signature(pts
, quote_digest
,
341 chunk_clear("e_digest
);
345 DBG2(DBG_IMV
, "signature verification succeeded for "
347 chunk_clear("e_digest
);
350 if (evid_signature_included
)
352 /** TODO: What to do with Evidence Signature */
353 evid_sign
= attr_cast
->get_evid_sign(attr_cast
);
354 DBG1(DBG_IMV
, "This version of Attestation IMV can not handle"
355 " Optional Evidence Signature field");
360 case TCG_PTS_FILE_MEAS
:
362 tcg_pts_attr_file_meas_t
*attr_cast
;
363 u_int16_t request_id
;
364 int file_count
, file_id
;
365 pts_meas_algorithms_t algo
;
366 pts_file_meas_t
*measurements
;
368 enumerator_t
*e_hash
;
371 platform_info
= pts
->get_platform_info(pts
);
372 if (!pts_db
|| !platform_info
)
377 attr_cast
= (tcg_pts_attr_file_meas_t
*)attr
;
378 measurements
= attr_cast
->get_measurements(attr_cast
);
379 algo
= pts
->get_meas_algorithm(pts
);
380 request_id
= measurements
->get_request_id(measurements
);
381 file_count
= measurements
->get_file_count(measurements
);
383 DBG1(DBG_IMV
, "measurement request %d returned %d file%s:",
384 request_id
, file_count
, (file_count
== 1) ?
"":"s");
386 if (!attestation_state
->check_off_request(attestation_state
,
387 request_id
, &file_id
, &is_dir
))
389 DBG1(DBG_IMV
, " no entry found for this request");
393 /* check hashes from database against measurements */
394 e_hash
= pts_db
->create_hash_enumerator(pts_db
,
395 platform_info
, algo
, file_id
, is_dir
);
396 if (!measurements
->verify(measurements
, e_hash
, is_dir
))
398 attestation_state
->set_measurement_error(attestation_state
);
400 e_hash
->destroy(e_hash
);
403 case TCG_PTS_UNIX_FILE_META
:
405 tcg_pts_attr_file_meta_t
*attr_cast
;
407 pts_file_meta_t
*metadata
;
408 pts_file_metadata_t
*entry
;
409 time_t created
, modified
, accessed
;
413 attr_cast
= (tcg_pts_attr_file_meta_t
*)attr
;
414 metadata
= attr_cast
->get_metadata(attr_cast
);
415 file_count
= metadata
->get_file_count(metadata
);
417 DBG1(DBG_IMV
, "metadata request returned %d file%s:",
418 file_count
, (file_count
== 1) ?
"":"s");
420 e
= metadata
->create_enumerator(metadata
);
421 while (e
->enumerate(e
, &entry
))
423 DBG1(DBG_IMV
, " '%s' (%"PRIu64
" bytes)"
424 " owner %"PRIu64
", group %"PRIu64
", type %d",
425 entry
->filename
, entry
->filesize
, entry
->owner
,
426 entry
->group
, entry
->type
);
428 created
= entry
->created
;
429 modified
= entry
->modified
;
430 accessed
= entry
->accessed
;
432 DBG1(DBG_IMV
, " created %T, modified %T, accessed %T",
433 &created
, utc
, &modified
, utc
, &accessed
, utc
);
439 /* TODO: Not implemented yet */
440 case TCG_PTS_INTEG_MEAS_LOG
:
441 /* Attributes using XML */
442 case TCG_PTS_TEMPL_REF_MANI_SET_META
:
443 case TCG_PTS_VERIFICATION_RESULT
:
444 case TCG_PTS_INTEG_REPORT
:
446 case TCG_PTS_WIN_FILE_META
:
447 case TCG_PTS_REGISTRY_VALUE
:
448 /* Received on IMC side only*/
449 case TCG_PTS_REQ_PROTO_CAPS
:
450 case TCG_PTS_DH_NONCE_PARAMS_REQ
:
451 case TCG_PTS_DH_NONCE_FINISH
:
452 case TCG_PTS_MEAS_ALGO
:
453 case TCG_PTS_GET_TPM_VERSION_INFO
:
454 case TCG_PTS_REQ_TEMPL_REF_MANI_SET_META
:
455 case TCG_PTS_UPDATE_TEMPL_REF_MANI
:
456 case TCG_PTS_GET_AIK
:
457 case TCG_PTS_REQ_FUNCT_COMP_EVID
:
458 case TCG_PTS_GEN_ATTEST_EVID
:
459 case TCG_PTS_REQ_FILE_META
:
460 case TCG_PTS_REQ_FILE_MEAS
:
461 case TCG_PTS_REQ_INTEG_MEAS_LOG
:
463 DBG1(DBG_IMV
, "received unsupported attribute '%N'",
464 tcg_attr_names
, attr
->get_type(attr
));