moved attestation plugins to libpts in order to resolve circular reference with libimcv
[strongswan.git] / src / libpts / 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 #include <inttypes.h>
36
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)
43 {
44 pts_t *pts;
45
46 pts = attestation_state->get_pts(attestation_state);
47
48 switch (attr->get_type(attr))
49 {
50 case TCG_PTS_PROTO_CAPS:
51 {
52 tcg_pts_attr_proto_caps_t *attr_cast;
53 pts_proto_caps_flag_t flags;
54
55 attr_cast = (tcg_pts_attr_proto_caps_t*)attr;
56 flags = attr_cast->get_flags(attr_cast);
57 pts->set_proto_caps(pts, flags);
58 break;
59 }
60 case TCG_PTS_MEAS_ALGO_SELECTION:
61 {
62 tcg_pts_attr_meas_algo_t *attr_cast;
63 pts_meas_algorithms_t selected_algorithm;
64
65 attr_cast = (tcg_pts_attr_meas_algo_t*)attr;
66 selected_algorithm = attr_cast->get_algorithms(attr_cast);
67 if (!(selected_algorithm & supported_algorithms))
68 {
69 DBG1(DBG_IMV, "PTS-IMC selected unsupported"
70 " measurement algorithm");
71 return FALSE;
72 }
73 pts->set_meas_algorithm(pts, selected_algorithm);
74 break;
75 }
76 case TCG_PTS_DH_NONCE_PARAMS_RESP:
77 {
78 tcg_pts_attr_dh_nonce_params_resp_t *attr_cast;
79 int nonce_len, min_nonce_len;
80 pts_dh_group_t dh_group;
81 pts_meas_algorithms_t offered_algorithms, selected_algorithm;
82 chunk_t responder_value, responder_nonce;
83
84 attr_cast = (tcg_pts_attr_dh_nonce_params_resp_t*)attr;
85 responder_nonce = attr_cast->get_responder_nonce(attr_cast);
86
87 /* check compliance of responder nonce length */
88 min_nonce_len = lib->settings->get_int(lib->settings,
89 "libimcv.plugins.imv-attestation.min_nonce_len", 0);
90 nonce_len = responder_nonce.len;
91 if (nonce_len < PTS_MIN_NONCE_LEN ||
92 (min_nonce_len > 0 && nonce_len < min_nonce_len))
93 {
94 attr = pts_dh_nonce_error_create(
95 max(PTS_MIN_NONCE_LEN, min_nonce_len),
96 PTS_MAX_NONCE_LEN);
97 attr_list->insert_last(attr_list, attr);
98 break;
99 }
100
101 dh_group = attr_cast->get_dh_group(attr_cast);
102 if (!(dh_group & supported_dh_groups))
103 {
104 DBG1(DBG_IMV, "PTS-IMC selected unsupported DH group");
105 return FALSE;
106 }
107
108 offered_algorithms = attr_cast->get_hash_algo_set(attr_cast);
109 selected_algorithm = pts_meas_algo_select(supported_algorithms,
110 offered_algorithms);
111 if (selected_algorithm == PTS_MEAS_ALGO_NONE)
112 {
113 attr = pts_hash_alg_error_create(supported_algorithms);
114 attr_list->insert_last(attr_list, attr);
115 break;
116 }
117 pts->set_dh_hash_algorithm(pts, selected_algorithm);
118
119 if (!pts->create_dh_nonce(pts, dh_group, nonce_len))
120 {
121 return FALSE;
122 }
123
124 responder_value = attr_cast->get_responder_value(attr_cast);
125 pts->set_peer_public_value(pts, responder_value,
126 responder_nonce);
127
128 /* Calculate secret assessment value */
129 if (!pts->calculate_secret(pts))
130 {
131 return FALSE;
132 }
133 break;
134 }
135 case TCG_PTS_TPM_VERSION_INFO:
136 {
137 tcg_pts_attr_tpm_version_info_t *attr_cast;
138 chunk_t tpm_version_info;
139
140 attr_cast = (tcg_pts_attr_tpm_version_info_t*)attr;
141 tpm_version_info = attr_cast->get_tpm_version_info(attr_cast);
142 pts->set_tpm_version_info(pts, tpm_version_info);
143 break;
144 }
145 case TCG_PTS_AIK:
146 {
147 tcg_pts_attr_aik_t *attr_cast;
148 certificate_t *aik, *issuer;
149 enumerator_t *e;
150 bool trusted = FALSE;
151
152 attr_cast = (tcg_pts_attr_aik_t*)attr;
153 aik = attr_cast->get_aik(attr_cast);
154 if (!aik)
155 {
156 DBG1(DBG_IMV, "AIK unavailable");
157 return FALSE;
158 }
159 if (aik->get_type(aik) == CERT_X509)
160 {
161 DBG1(DBG_IMV, "verifying AIK certificate");
162 e = pts_credmgr->create_trusted_enumerator(pts_credmgr,
163 KEY_ANY, aik->get_issuer(aik), FALSE);
164 while (e->enumerate(e, &issuer))
165 {
166 if (aik->issued_by(aik, issuer))
167 {
168 trusted = TRUE;
169 break;
170 }
171 }
172 e->destroy(e);
173 DBG1(DBG_IMV, "AIK certificate is %strusted",
174 trusted ? "" : "not ");
175 }
176 pts->set_aik(pts, aik);
177 break;
178 }
179 case TCG_PTS_FILE_MEAS:
180 {
181 tcg_pts_attr_file_meas_t *attr_cast;
182 u_int16_t request_id;
183 int file_count, file_id;
184 pts_meas_algorithms_t algo;
185 pts_file_meas_t *measurements;
186 char *platform_info;
187 enumerator_t *e_hash;
188 bool is_dir;
189
190 platform_info = pts->get_platform_info(pts);
191 if (!pts_db || !platform_info)
192 {
193 DBG1(DBG_IMV, "%s%s%s not available",
194 (pts_db) ? "" : "pts database",
195 (!pts_db && !platform_info) ? "and" : "",
196 (platform_info) ? "" : "platform info");
197 break;
198 }
199
200 attr_cast = (tcg_pts_attr_file_meas_t*)attr;
201 measurements = attr_cast->get_measurements(attr_cast);
202 algo = pts->get_meas_algorithm(pts);
203 request_id = measurements->get_request_id(measurements);
204 file_count = measurements->get_file_count(measurements);
205
206 DBG1(DBG_IMV, "measurement request %d returned %d file%s:",
207 request_id, file_count, (file_count == 1) ? "":"s");
208
209 if (!attestation_state->check_off_file_meas_request(attestation_state,
210 request_id, &file_id, &is_dir))
211 {
212 DBG1(DBG_IMV, " no entry found for file measurement request %d",
213 request_id);
214 break;
215 }
216
217 /* check hashes from database against measurements */
218 e_hash = pts_db->create_file_hash_enumerator(pts_db,
219 platform_info, algo, file_id, is_dir);
220 if (!measurements->verify(measurements, e_hash, is_dir))
221 {
222 attestation_state->set_measurement_error(attestation_state);
223 }
224 e_hash->destroy(e_hash);
225 break;
226 }
227 case TCG_PTS_UNIX_FILE_META:
228 {
229 tcg_pts_attr_file_meta_t *attr_cast;
230 int file_count;
231 pts_file_meta_t *metadata;
232 pts_file_metadata_t *entry;
233 time_t created, modified, accessed;
234 bool utc = FALSE;
235 enumerator_t *e;
236
237 attr_cast = (tcg_pts_attr_file_meta_t*)attr;
238 metadata = attr_cast->get_metadata(attr_cast);
239 file_count = metadata->get_file_count(metadata);
240
241 DBG1(DBG_IMV, "metadata request returned %d file%s:",
242 file_count, (file_count == 1) ? "":"s");
243
244 e = metadata->create_enumerator(metadata);
245 while (e->enumerate(e, &entry))
246 {
247 DBG1(DBG_IMV, " '%s' (%"PRIu64" bytes)"
248 " owner %"PRIu64", group %"PRIu64", type %N",
249 entry->filename, entry->filesize, entry->owner,
250 entry->group, pts_file_type_names, entry->type);
251
252 created = entry->created;
253 modified = entry->modified;
254 accessed = entry->accessed;
255
256 DBG1(DBG_IMV, " created %T, modified %T, accessed %T",
257 &created, utc, &modified, utc, &accessed, utc);
258 }
259 e->destroy(e);
260 break;
261 }
262 case TCG_PTS_SIMPLE_COMP_EVID:
263 {
264 tcg_pts_attr_simple_comp_evid_t *attr_cast;
265 pts_comp_func_name_t *name;
266 pts_comp_evidence_t *evidence;
267 pts_component_t *comp;
268 u_int32_t depth;
269 status_t status;
270
271 attr_cast = (tcg_pts_attr_simple_comp_evid_t*)attr;
272 evidence = attr_cast->get_comp_evidence(attr_cast);
273 name = evidence->get_comp_func_name(evidence, &depth);
274
275 comp = attestation_state->check_off_component(attestation_state, name);
276 if (!comp)
277 {
278 DBG1(DBG_IMV, " no entry found for component evidence request");
279 break;
280 }
281 status = comp->verify(comp, pts, pts_db, evidence);
282
283 switch (status)
284 {
285 default:
286 case FAILED:
287 attestation_state->set_measurement_error(attestation_state);
288 /* fall through to next case */
289 case SUCCESS:
290 comp->destroy(comp);
291 break;
292 case NEED_MORE:
293 /* re-enter component into list */
294 attestation_state->add_component(attestation_state, comp);
295 }
296 break;
297 }
298 case TCG_PTS_SIMPLE_EVID_FINAL:
299 {
300 tcg_pts_attr_simple_evid_final_t *attr_cast;
301 u_int8_t flags;
302 pts_meas_algorithms_t comp_hash_algorithm;
303 chunk_t pcr_comp, tpm_quote_sig, evid_sig;
304 chunk_t pcr_composite, quote_info;
305 bool use_quote2, use_ver_info;
306
307 attr_cast = (tcg_pts_attr_simple_evid_final_t*)attr;
308 flags = attr_cast->get_quote_info(attr_cast, &comp_hash_algorithm,
309 &pcr_comp, &tpm_quote_sig);
310
311 if (flags != PTS_SIMPLE_EVID_FINAL_NO)
312 {
313 use_quote2 = (flags == PTS_SIMPLE_EVID_FINAL_QUOTE_INFO2 ||
314 flags == PTS_SIMPLE_EVID_FINAL_QUOTE_INFO2_CAP_VER);
315 use_ver_info = (flags == PTS_SIMPLE_EVID_FINAL_QUOTE_INFO2_CAP_VER);
316
317 /* Construct PCR Composite and TPM Quote Info structures */
318 if (!pts->get_quote_info(pts, use_quote2, use_ver_info,
319 comp_hash_algorithm, &pcr_composite, &quote_info))
320 {
321 DBG1(DBG_IMV, "unable to construct TPM Quote Info");
322 return FALSE;
323 }
324
325 if (!chunk_equals(pcr_comp, pcr_composite))
326 {
327 DBG1(DBG_IMV, "received PCR Composite does not match "
328 "constructed one");
329 free(pcr_composite.ptr);
330 free(quote_info.ptr);
331 return FALSE;
332 }
333 DBG2(DBG_IMV, "received PCR Composite matches constructed one");
334 free(pcr_composite.ptr);
335
336 if (!pts->verify_quote_signature(pts, quote_info, tpm_quote_sig))
337 {
338 free(quote_info.ptr);
339 return FALSE;
340 }
341 DBG2(DBG_IMV, "TPM Quote Info signature verification successful");
342 free(quote_info.ptr);
343 }
344
345 if (attr_cast->get_evid_sig(attr_cast, &evid_sig))
346 {
347 /** TODO: What to do with Evidence Signature */
348 DBG1(DBG_IMV, "This version of the Attestation IMV can not "
349 "handle Evidence Signatures");
350 }
351 break;
352 }
353
354 /* TODO: Not implemented yet */
355 case TCG_PTS_INTEG_MEAS_LOG:
356 /* Attributes using XML */
357 case TCG_PTS_TEMPL_REF_MANI_SET_META:
358 case TCG_PTS_VERIFICATION_RESULT:
359 case TCG_PTS_INTEG_REPORT:
360 /* On Windows only*/
361 case TCG_PTS_WIN_FILE_META:
362 case TCG_PTS_REGISTRY_VALUE:
363 /* Received on IMC side only*/
364 case TCG_PTS_REQ_PROTO_CAPS:
365 case TCG_PTS_DH_NONCE_PARAMS_REQ:
366 case TCG_PTS_DH_NONCE_FINISH:
367 case TCG_PTS_MEAS_ALGO:
368 case TCG_PTS_GET_TPM_VERSION_INFO:
369 case TCG_PTS_REQ_TEMPL_REF_MANI_SET_META:
370 case TCG_PTS_UPDATE_TEMPL_REF_MANI:
371 case TCG_PTS_GET_AIK:
372 case TCG_PTS_REQ_FUNC_COMP_EVID:
373 case TCG_PTS_GEN_ATTEST_EVID:
374 case TCG_PTS_REQ_FILE_META:
375 case TCG_PTS_REQ_FILE_MEAS:
376 case TCG_PTS_REQ_INTEG_MEAS_LOG:
377 default:
378 DBG1(DBG_IMV, "received unsupported attribute '%N'",
379 tcg_attr_names, attr->get_type(attr));
380 break;
381 }
382 return TRUE;
383 }
384