9e373b1e1626cbe5beff6a597e29829538f9e8c4
[strongswan.git] / src / libimcv / plugins / imv_attestation / imv_attestation_process.c
1 /*
2 * Copyright (C) 2011 Sansar Choinyambuu
3 * HSR Hochschule fuer Technik Rapperswil
4 *
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>.
9 *
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
13 * for more details.
14 */
15
16 #include "imv_attestation_process.h"
17
18 #include <ietf/ietf_attr_pa_tnc_error.h>
19
20 #include <pts/pts.h>
21
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>
31
32 #include <debug.h>
33 #include <crypto/hashers/hasher.h>
34
35 bool imv_attestation_process(pa_tnc_attr_t *attr, linked_list_t *attr_list,
36 imv_attestation_state_t *attestation_state,
37 pts_meas_algorithms_t supported_algorithms,
38 pts_dh_group_t supported_dh_groups,
39 pts_database_t *pts_db,
40 credential_manager_t *pts_credmgr)
41 {
42 chunk_t attr_info;
43 pts_t *pts;
44
45 pts = attestation_state->get_pts(attestation_state);
46
47 switch (attr->get_type(attr))
48 {
49 case TCG_PTS_PROTO_CAPS:
50 {
51 tcg_pts_attr_proto_caps_t *attr_cast;
52 pts_proto_caps_flag_t flags;
53
54 attr_cast = (tcg_pts_attr_proto_caps_t*)attr;
55 flags = attr_cast->get_flags(attr_cast);
56 pts->set_proto_caps(pts, flags);
57 break;
58 }
59 case TCG_PTS_MEAS_ALGO_SELECTION:
60 {
61 tcg_pts_attr_meas_algo_t *attr_cast;
62 pts_meas_algorithms_t selected_algorithm;
63
64 attr_cast = (tcg_pts_attr_meas_algo_t*)attr;
65 selected_algorithm = attr_cast->get_algorithms(attr_cast);
66 if (!(selected_algorithm & supported_algorithms))
67 {
68 DBG1(DBG_IMV, "PTS-IMC selected unsupported"
69 " measurement algorithm");
70 return FALSE;
71 }
72 pts->set_meas_algorithm(pts, selected_algorithm);
73 break;
74 }
75 case TCG_PTS_DH_NONCE_PARAMS_RESP:
76 {
77 tcg_pts_attr_dh_nonce_params_resp_t *attr_cast;
78 int nonce_len, min_nonce_len;
79 pts_dh_group_t dh_group;
80 pts_meas_algorithms_t offered_algorithms, selected_algorithm;
81 chunk_t responder_value, responder_nonce;
82
83 attr_cast = (tcg_pts_attr_dh_nonce_params_resp_t*)attr;
84 responder_nonce = attr_cast->get_responder_nonce(attr_cast);
85
86 /* check compliance of responder nonce length */
87 min_nonce_len = lib->settings->get_int(lib->settings,
88 "libimcv.plugins.imv-attestation.min_nonce_len", 0);
89 nonce_len = responder_nonce.len;
90 if (nonce_len < PTS_MIN_NONCE_LEN ||
91 (min_nonce_len > 0 && nonce_len < min_nonce_len))
92 {
93 attr = pts_dh_nonce_error_create(
94 max(PTS_MIN_NONCE_LEN, min_nonce_len),
95 PTS_MAX_NONCE_LEN);
96 attr_list->insert_last(attr_list, attr);
97 break;
98 }
99
100 dh_group = attr_cast->get_dh_group(attr_cast);
101 if (!(dh_group & supported_dh_groups))
102 {
103 DBG1(DBG_IMV, "PTS-IMC selected unsupported DH group");
104 return FALSE;
105 }
106
107 offered_algorithms = attr_cast->get_hash_algo_set(attr_cast);
108 selected_algorithm = pts_meas_algo_select(supported_algorithms,
109 offered_algorithms);
110 if (selected_algorithm == PTS_MEAS_ALGO_NONE)
111 {
112 attr = pts_hash_alg_error_create(supported_algorithms);
113 attr_list->insert_last(attr_list, attr);
114 break;
115 }
116 pts->set_dh_hash_algorithm(pts, selected_algorithm);
117
118 if (!pts->create_dh_nonce(pts, dh_group, nonce_len))
119 {
120 return FALSE;
121 }
122
123 responder_value = attr_cast->get_responder_value(attr_cast);
124 pts->set_peer_public_value(pts, responder_value,
125 responder_nonce);
126
127 /* Calculate secret assessment value */
128 if (!pts->calculate_secret(pts))
129 {
130 return FALSE;
131 }
132 break;
133 }
134 case TCG_PTS_TPM_VERSION_INFO:
135 {
136 tcg_pts_attr_tpm_version_info_t *attr_cast;
137 chunk_t tpm_version_info;
138
139 attr_cast = (tcg_pts_attr_tpm_version_info_t*)attr;
140 tpm_version_info = attr_cast->get_tpm_version_info(attr_cast);
141 pts->set_tpm_version_info(pts, tpm_version_info);
142 break;
143 }
144 case TCG_PTS_AIK:
145 {
146 tcg_pts_attr_aik_t *attr_cast;
147 certificate_t *aik, *issuer;
148 enumerator_t *e;
149 bool trusted = FALSE;
150
151 attr_cast = (tcg_pts_attr_aik_t*)attr;
152 aik = attr_cast->get_aik(attr_cast);
153 if (!aik)
154 {
155 DBG1(DBG_IMV, "AIK unavailable");
156 return FALSE;
157 }
158 if (aik->get_type(aik) == CERT_X509)
159 {
160 DBG1(DBG_IMV, "verifying AIK certificate");
161 e = pts_credmgr->create_trusted_enumerator(pts_credmgr,
162 KEY_ANY, aik->get_issuer(aik), FALSE);
163 while (e->enumerate(e, &issuer))
164 {
165 if (aik->issued_by(aik, issuer))
166 {
167 trusted = TRUE;
168 break;
169 }
170 }
171 e->destroy(e);
172 DBG1(DBG_IMV, "AIK certificate is %strusted",
173 trusted ? "" : "not ");
174 }
175 pts->set_aik(pts, aik);
176 break;
177 }
178 case TCG_PTS_SIMPLE_COMP_EVID:
179 {
180 tcg_pts_attr_simple_comp_evid_t *attr_cast;
181 bool pcr_info_inclided;
182 pts_attr_simple_comp_evid_flag_t flags;
183 u_int32_t depth, comp_vendor_id, extended_pcr;
184 u_int8_t family, measurement_type;
185 pts_qualifier_t qualifier;
186 pts_funct_comp_name_t name;
187 pts_meas_algorithms_t hash_algorithm;
188 pts_pcr_transform_t transformation;
189 chunk_t measurement_time, policy_uri;
190 chunk_t pcr_before, pcr_after, measurement;
191
192 attr_cast = (tcg_pts_attr_simple_comp_evid_t*)attr;
193 attr_info = attr->get_value(attr);
194
195 pcr_info_inclided = attr_cast->is_pcr_info_included(attr_cast);
196 flags = attr_cast->get_flags(attr_cast);
197 depth = attr_cast->get_sub_component_depth(attr_cast);
198 /* TODO: Implement check of components with its sub-components */
199 if (depth != 0)
200 {
201 DBG1(DBG_IMV, "Current version of Attestation IMV does not"
202 " support sub component measurement deeper than zero");
203 }
204 comp_vendor_id = attr_cast->get_spec_comp_funct_name_vendor_id(
205 attr_cast);
206 if (comp_vendor_id != PEN_TCG)
207 {
208 DBG1(DBG_IMV, "Current version of Attestation IMV supports"
209 "only functional component namings by TCG ");
210 break;
211 }
212 family = attr_cast->get_family(attr_cast);
213 if (family)
214 {
215 attr = ietf_attr_pa_tnc_error_create(PEN_TCG,
216 TCG_PTS_INVALID_NAME_FAM, attr_info);
217 attr_list->insert_last(attr_list, attr);
218 break;
219 }
220 qualifier = attr_cast->get_qualifier(attr_cast);
221
222 /* Check if Unknown or Wildcard was set for qualifier */
223 if (qualifier.kernel && qualifier.sub_component &&
224 (qualifier.type & PTS_FUNC_COMP_TYPE_ALL))
225 {
226 DBG1(DBG_IMV, "Wildcard was set for the qualifier "
227 "of functional component");
228 return FALSE;
229 }
230 else if (!qualifier.kernel && !qualifier.sub_component &&
231 (qualifier.type & PTS_FUNC_COMP_TYPE_UNKNOWN))
232 {
233 DBG1(DBG_IMV, "Unknown feature was set for the qualifier "
234 "of functional component");
235 return FALSE;
236 }
237 else
238 {
239 /* TODO: Implement what todo with received qualifier */
240 }
241
242 name = attr_cast->get_comp_funct_name(attr_cast);
243 measurement_type = attr_cast->get_measurement_type(attr_cast);
244 hash_algorithm = attr_cast->get_hash_algorithm(attr_cast);
245 transformation = attr_cast->get_pcr_trans(attr_cast);
246 measurement_time = attr_cast->get_measurement_time(attr_cast);
247
248 /* Call getters of optional fields when corresponding flag is set */
249 if (pcr_info_inclided)
250 {
251 pcr_entry_t *entry;
252
253 extended_pcr = attr_cast->get_extended_pcr(attr_cast);
254 pcr_before = attr_cast->get_pcr_before_value(attr_cast);
255 pcr_after = attr_cast->get_pcr_after_value(attr_cast);
256 measurement = attr_cast->get_comp_measurement(attr_cast);
257
258 DBG4(DBG_IMV,"PCR: %d was extended with %B",
259 extended_pcr, &measurement);
260 DBG4(DBG_IMV,"PCR: %d before value: %B",
261 extended_pcr, &pcr_before);
262 DBG4(DBG_IMV,"PCR: %d after value: %B",
263 extended_pcr, &pcr_after);
264
265 entry = malloc_thing(pcr_entry_t);
266 entry->pcr_number = extended_pcr;
267 strncpy(entry->pcr_value, pcr_after.ptr, PCR_LEN);
268 pts->add_pcr_entry(pts, entry);
269
270 }
271 if (flags != PTS_SIMPLE_COMP_EVID_FLAG_NO_VALID)
272 {
273 policy_uri = attr_cast->get_policy_uri(attr_cast);
274 DBG1(DBG_IMV, "This version of Attestation IMV can not handle"
275 " Verification Policies");
276 }
277
278 break;
279 }
280 case TCG_PTS_SIMPLE_EVID_FINAL:
281 {
282 tcg_pts_attr_simple_evid_final_t *attr_cast;
283 pts_simple_evid_final_flag_t flags;
284 chunk_t pcr_comp;
285 chunk_t tpm_quote_sign;
286 chunk_t evid_sign;
287 bool evid_signature_included;
288
289 /** TODO: Ignoring Composite Hash Algorithm field
290 * No flag defined which indicates the precense of it
291 */
292 attr_cast = (tcg_pts_attr_simple_evid_final_t*)attr;
293 evid_signature_included = attr_cast->is_evid_sign_included(attr_cast);
294 flags = attr_cast->get_flags(attr_cast);
295
296 if ((flags == PTS_SIMPLE_EVID_FINAL_FLAG_TPM_QUOTE_INFO2) ||
297 (flags == PTS_SIMPLE_EVID_FINAL_FLAG_TPM_QUOTE_INFO2_CAP_VER))
298 {
299 DBG1(DBG_IMV, "This version of Attestation IMV can not handle"
300 " TPM Quote Info2 structure");
301 break;
302 }
303 if (flags == PTS_SIMPLE_EVID_FINAL_FLAG_TPM_QUOTE_INFO)
304 {
305 chunk_t pcr_composite, quote_info, quote_digest;
306 hasher_t *hasher;
307 pcr_comp = attr_cast->get_pcr_comp(attr_cast);
308 tpm_quote_sign = attr_cast->get_tpm_quote_sign(attr_cast);
309
310 /* Construct PCR Composite and TPM Quote Info structures*/
311 if (!pts->get_quote_info(pts, &pcr_composite, &quote_info))
312 {
313 DBG1(DBG_IMV, "unable to contruct TPM Quote Info");
314 return FALSE;
315 }
316
317 /* Check calculated PCR composite structure matches with received */
318 if (pcr_comp.ptr && !chunk_equals(pcr_comp, pcr_composite))
319 {
320 DBG1(DBG_IMV, "received PCR Compsosite didn't match"
321 " with constructed");
322 chunk_clear(&pcr_composite);
323 chunk_clear(&quote_info);
324 return FALSE;
325 }
326 DBG2(DBG_IMV, "received PCR Composite matches with constructed");
327 chunk_clear(&pcr_composite);
328
329 /* SHA1(TPM Quote Info) expected from IMC */
330 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
331 hasher->allocate_hash(hasher, quote_info, &quote_digest);
332 hasher->destroy(hasher);
333 chunk_clear(&quote_info);
334
335 if (tpm_quote_sign.ptr &&
336 !pts->verify_quote_signature(pts, quote_digest,
337 tpm_quote_sign))
338 {
339 chunk_clear(&quote_digest);
340 return FALSE;
341 }
342
343 DBG2(DBG_IMV, "signature verification succeeded for "
344 "TPM Quote Info");
345 chunk_clear(&quote_digest);
346 }
347
348 if (evid_signature_included)
349 {
350 /** TODO: What to do with Evidence Signature */
351 evid_sign = attr_cast->get_evid_sign(attr_cast);
352 DBG1(DBG_IMV, "This version of Attestation IMV can not handle"
353 " Optional Evidence Signature field");
354 }
355
356 break;
357 }
358 case TCG_PTS_FILE_MEAS:
359 {
360 tcg_pts_attr_file_meas_t *attr_cast;
361 u_int16_t request_id;
362 int file_count, file_id;
363 pts_meas_algorithms_t algo;
364 pts_file_meas_t *measurements;
365 char *platform_info;
366 enumerator_t *e_hash;
367 bool is_dir;
368
369 platform_info = pts->get_platform_info(pts);
370 if (!pts_db || !platform_info)
371 {
372 break;
373 }
374
375 attr_cast = (tcg_pts_attr_file_meas_t*)attr;
376 measurements = attr_cast->get_measurements(attr_cast);
377 algo = pts->get_meas_algorithm(pts);
378 request_id = measurements->get_request_id(measurements);
379 file_count = measurements->get_file_count(measurements);
380
381 DBG1(DBG_IMV, "measurement request %d returned %d file%s:",
382 request_id, file_count, (file_count == 1) ? "":"s");
383
384 if (!attestation_state->check_off_request(attestation_state,
385 request_id, &file_id, &is_dir))
386 {
387 DBG1(DBG_IMV, " no entry found for this request");
388 break;
389 }
390
391 /* check hashes from database against measurements */
392 e_hash = pts_db->create_hash_enumerator(pts_db,
393 platform_info, algo, file_id, is_dir);
394 if (!measurements->verify(measurements, e_hash, is_dir))
395 {
396 attestation_state->set_measurement_error(attestation_state);
397 }
398 e_hash->destroy(e_hash);
399 break;
400 }
401 case TCG_PTS_UNIX_FILE_META:
402 {
403 tcg_pts_attr_file_meta_t *attr_cast;
404 int file_count;
405 pts_file_meta_t *metadata;
406 pts_file_metadata_t *entry;
407 enumerator_t *e;
408 bool utc = FALSE;
409
410 attr_cast = (tcg_pts_attr_file_meta_t*)attr;
411 metadata = attr_cast->get_metadata(attr_cast);
412 file_count = metadata->get_file_count(metadata);
413
414 DBG1(DBG_IMV, "metadata request returned %d file%s:",
415 file_count, (file_count == 1) ? "":"s");
416
417 e = metadata->create_enumerator(metadata);
418 while (e->enumerate(e, &entry))
419 {
420 DBG1(DBG_IMV, " '%s' (%d bytes) owner %d, group %d, type %d",
421 entry->filename,
422 entry->filesize,
423 entry->owner,
424 entry->group,
425 entry->type);
426 DBG1(DBG_IMV, " created %T, modified %T, accessed %T",
427 &entry->created, utc,
428 &entry->modified, utc,
429 &entry->accessed, utc);
430 }
431 e->destroy(e);
432 break;
433 }
434
435 /* TODO: Not implemented yet */
436 case TCG_PTS_INTEG_MEAS_LOG:
437 /* Attributes using XML */
438 case TCG_PTS_TEMPL_REF_MANI_SET_META:
439 case TCG_PTS_VERIFICATION_RESULT:
440 case TCG_PTS_INTEG_REPORT:
441 /* On Windows only*/
442 case TCG_PTS_WIN_FILE_META:
443 case TCG_PTS_REGISTRY_VALUE:
444 /* Received on IMC side only*/
445 case TCG_PTS_REQ_PROTO_CAPS:
446 case TCG_PTS_DH_NONCE_PARAMS_REQ:
447 case TCG_PTS_DH_NONCE_FINISH:
448 case TCG_PTS_MEAS_ALGO:
449 case TCG_PTS_GET_TPM_VERSION_INFO:
450 case TCG_PTS_REQ_TEMPL_REF_MANI_SET_META:
451 case TCG_PTS_UPDATE_TEMPL_REF_MANI:
452 case TCG_PTS_GET_AIK:
453 case TCG_PTS_REQ_FUNCT_COMP_EVID:
454 case TCG_PTS_GEN_ATTEST_EVID:
455 case TCG_PTS_REQ_FILE_META:
456 case TCG_PTS_REQ_FILE_MEAS:
457 case TCG_PTS_REQ_INTEG_MEAS_LOG:
458 default:
459 DBG1(DBG_IMV, "received unsupported attribute '%N'",
460 tcg_attr_names, attr->get_type(attr));
461 break;
462 }
463 return TRUE;
464 }
465