finished refactoring of file measurements
[strongswan.git] / src / libimcv / plugins / imc_attestation / imc_attestation.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 "imc_attestation_state.h"
17
18 #include <imc/imc_agent.h>
19 #include <pa_tnc/pa_tnc_msg.h>
20 #include <ietf/ietf_attr.h>
21 #include <ietf/ietf_attr_pa_tnc_error.h>
22
23 #include <tcg/tcg_pts_attr_proto_caps.h>
24 #include <tcg/tcg_pts_attr_meas_algo.h>
25 #include <tcg/tcg_pts_attr_get_tpm_version_info.h>
26 #include <tcg/tcg_pts_attr_tpm_version_info.h>
27 #include <tcg/tcg_pts_attr_get_aik.h>
28 #include <tcg/tcg_pts_attr_aik.h>
29 #include <tcg/tcg_pts_attr_req_funct_comp_evid.h>
30 #include <tcg/tcg_pts_attr_gen_attest_evid.h>
31 #include <tcg/tcg_pts_attr_simple_comp_evid.h>
32 #include <tcg/tcg_pts_attr_simple_evid_final.h>
33 #include <tcg/tcg_pts_attr_req_file_meas.h>
34 #include <tcg/tcg_pts_attr_file_meas.h>
35
36 #include <tncif_pa_subtypes.h>
37
38 #include <pen/pen.h>
39 #include <debug.h>
40 #include <utils/linked_list.h>
41 #include <crypto/hashers/hasher.h>
42
43 /* IMC definitions */
44
45 static const char imc_name[] = "Attestation";
46
47 #define IMC_VENDOR_ID PEN_TCG
48 #define IMC_SUBTYPE PA_SUBTYPE_TCG_PTS
49
50 static imc_agent_t *imc_attestation;
51
52 /**
53 * Supported PTS measurement algorithms
54 */
55 static pts_meas_algorithms_t supported_algorithms = 0;
56
57 /**
58 * see section 3.7.1 of TCG TNC IF-IMC Specification 1.2
59 */
60 TNC_Result TNC_IMC_Initialize(TNC_IMCID imc_id,
61 TNC_Version min_version,
62 TNC_Version max_version,
63 TNC_Version *actual_version)
64 {
65 if (imc_attestation)
66 {
67 DBG1(DBG_IMC, "IMC \"%s\" has already been initialized", imc_name);
68 return TNC_RESULT_ALREADY_INITIALIZED;
69 }
70 imc_attestation = imc_agent_create(imc_name, IMC_VENDOR_ID, IMC_SUBTYPE,
71 imc_id, actual_version);
72 if (!imc_attestation || !pts_meas_probe_algorithms(&supported_algorithms))
73 {
74 return TNC_RESULT_FATAL;
75 }
76 if (min_version > TNC_IFIMC_VERSION_1 || max_version < TNC_IFIMC_VERSION_1)
77 {
78 DBG1(DBG_IMC, "no common IF-IMC version");
79 return TNC_RESULT_NO_COMMON_VERSION;
80 }
81 return TNC_RESULT_SUCCESS;
82 }
83
84 /**
85 * see section 3.7.2 of TCG TNC IF-IMC Specification 1.2
86 */
87 TNC_Result TNC_IMC_NotifyConnectionChange(TNC_IMCID imc_id,
88 TNC_ConnectionID connection_id,
89 TNC_ConnectionState new_state)
90 {
91 imc_state_t *state;
92 /* TODO: Not used so far */
93 //imc_attestation_state_t *attestation_state;
94
95 if (!imc_attestation)
96 {
97 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
98 return TNC_RESULT_NOT_INITIALIZED;
99 }
100 switch (new_state)
101 {
102 case TNC_CONNECTION_STATE_CREATE:
103 state = imc_attestation_state_create(connection_id);
104 return imc_attestation->create_state(imc_attestation, state);
105 case TNC_CONNECTION_STATE_DELETE:
106 return imc_attestation->delete_state(imc_attestation, connection_id);
107 case TNC_CONNECTION_STATE_HANDSHAKE:
108 case TNC_CONNECTION_STATE_ACCESS_ISOLATED:
109 case TNC_CONNECTION_STATE_ACCESS_NONE:
110 default:
111 return imc_attestation->change_state(imc_attestation, connection_id,
112 new_state, NULL);
113 }
114 }
115
116
117 /**
118 * see section 3.7.3 of TCG TNC IF-IMC Specification 1.2
119 */
120 TNC_Result TNC_IMC_BeginHandshake(TNC_IMCID imc_id,
121 TNC_ConnectionID connection_id)
122 {
123 if (!imc_attestation)
124 {
125 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
126 return TNC_RESULT_NOT_INITIALIZED;
127 }
128 return TNC_RESULT_SUCCESS;
129 }
130
131 /**
132 * see section 3.7.4 of TCG TNC IF-IMC Specification 1.2
133 */
134 TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
135 TNC_ConnectionID connection_id,
136 TNC_BufferReference msg,
137 TNC_UInt32 msg_len,
138 TNC_MessageType msg_type)
139 {
140 pa_tnc_msg_t *pa_tnc_msg;
141 pa_tnc_attr_t *attr;
142 linked_list_t *attr_list;
143 imc_state_t *state;
144 imc_attestation_state_t *attestation_state;
145 enumerator_t *enumerator;
146 pts_t *pts;
147 TNC_Result result;
148 bool fatal_error = FALSE;
149
150 if (!imc_attestation)
151 {
152 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
153 return TNC_RESULT_NOT_INITIALIZED;
154 }
155
156 /* get current IMC state */
157 if (!imc_attestation->get_state(imc_attestation, connection_id, &state))
158 {
159 return TNC_RESULT_FATAL;
160 }
161 attestation_state = (imc_attestation_state_t*)state;
162 pts = attestation_state->get_pts(attestation_state);
163
164 /* parse received PA-TNC message and automatically handle any errors */
165 result = imc_attestation->receive_message(imc_attestation, connection_id,
166 chunk_create(msg, msg_len), msg_type,
167 &pa_tnc_msg);
168
169 /* no parsed PA-TNC attributes available if an error occurred */
170 if (!pa_tnc_msg)
171 {
172 return result;
173 }
174
175 attr_list = linked_list_create();
176
177 /* analyze PA-TNC attributes */
178 enumerator = pa_tnc_msg->create_attribute_enumerator(pa_tnc_msg);
179 while (enumerator->enumerate(enumerator, &attr))
180 {
181 if (attr->get_vendor_id(attr) == PEN_IETF &&
182 attr->get_type(attr) == IETF_ATTR_PA_TNC_ERROR)
183 {
184 ietf_attr_pa_tnc_error_t *error_attr;
185 pa_tnc_error_code_t error_code;
186 chunk_t msg_info, attr_info;
187 u_int32_t offset;
188
189 error_attr = (ietf_attr_pa_tnc_error_t*)attr;
190 error_code = error_attr->get_error_code(error_attr);
191 msg_info = error_attr->get_msg_info(error_attr);
192
193 DBG1(DBG_IMC, "received PA-TNC error '%N' concerning message %#B",
194 pa_tnc_error_code_names, error_code, &msg_info);
195 switch (error_code)
196 {
197 case PA_ERROR_INVALID_PARAMETER:
198 offset = error_attr->get_offset(error_attr);
199 DBG1(DBG_IMC, " occurred at offset of %u bytes", offset);
200 break;
201 case PA_ERROR_ATTR_TYPE_NOT_SUPPORTED:
202 attr_info = error_attr->get_attr_info(error_attr);
203 DBG1(DBG_IMC, " unsupported attribute %#B", &attr_info);
204 break;
205 default:
206 break;
207 }
208 fatal_error = TRUE;
209 }
210 else if (attr->get_vendor_id(attr) == PEN_TCG)
211 {
212 switch (attr->get_type(attr))
213 {
214 case TCG_PTS_REQ_PROTO_CAPS:
215 {
216 tcg_pts_attr_proto_caps_t *attr_cast;
217 pts_proto_caps_flag_t imc_flags, imv_flags;
218
219 attr_cast = (tcg_pts_attr_proto_caps_t*)attr;
220 imv_flags = attr_cast->get_flags(attr_cast);
221 imc_flags = pts->get_proto_caps(pts);
222 pts->set_proto_caps(pts, imc_flags & imv_flags);
223
224 /* Send PTS Protocol Capabilities attribute */
225 attr = tcg_pts_attr_proto_caps_create(imc_flags & imv_flags,
226 FALSE);
227 attr_list->insert_last(attr_list, attr);
228 break;
229 }
230 case TCG_PTS_MEAS_ALGO:
231 {
232 tcg_pts_attr_meas_algo_t *attr_cast;
233 pts_meas_algorithms_t selected_algorithm;
234
235 attr_cast = (tcg_pts_attr_meas_algo_t*)attr;
236 selected_algorithm = attr_cast->get_algorithms(attr_cast);
237
238 if ((supported_algorithms & PTS_MEAS_ALGO_SHA384) &&
239 (selected_algorithm & PTS_MEAS_ALGO_SHA384))
240 {
241 pts->set_meas_algorithm(pts, PTS_MEAS_ALGO_SHA384);
242 }
243 else if ((supported_algorithms & PTS_MEAS_ALGO_SHA256) &&
244 (selected_algorithm & PTS_MEAS_ALGO_SHA256))
245 {
246 pts->set_meas_algorithm(pts, PTS_MEAS_ALGO_SHA256);
247 }
248
249 else if ((supported_algorithms & PTS_MEAS_ALGO_SHA1) &&
250 (selected_algorithm & PTS_MEAS_ALGO_SHA1))
251 {
252 pts->set_meas_algorithm(pts, PTS_MEAS_ALGO_SHA1);
253 }
254 else
255 {
256 /* TODO send a TCG_PTS_H_ALG_NOT_SUPPORTED error */
257 }
258
259 /* Send Measurement Algorithm Selection attribute */
260 selected_algorithm = pts->get_meas_algorithm(pts);
261 attr = tcg_pts_attr_meas_algo_create(selected_algorithm,
262 TRUE);
263 attr_list->insert_last(attr_list, attr);
264 break;
265 }
266
267 case TCG_PTS_GET_TPM_VERSION_INFO:
268 {
269 chunk_t tpm_version_info;
270
271 if (!pts->get_tpm_version_info(pts, &tpm_version_info))
272 {
273 /* TODO return TCG_PTS_TPM_VERS_NOT_SUPPORTED error attribute */
274 break;
275 }
276
277 /* Send TPM Version Info attribute */
278 attr = tcg_pts_attr_tpm_version_info_create(tpm_version_info);
279 attr_list->insert_last(attr_list, attr);
280 break;
281 }
282
283 case TCG_PTS_GET_AIK:
284 {
285 chunk_t aik;
286 bool is_naked_key;
287
288 if (!pts->get_aik(pts, &aik, &is_naked_key))
289 {
290 DBG1(DBG_IMC,"Obtaining AIK Certificate failed");
291 break;
292 }
293
294 /* Send AIK attribute */
295 attr = tcg_pts_attr_aik_create(is_naked_key, aik);
296 attr_list->insert_last(attr_list, attr);
297 break;
298 }
299
300 /* PTS-based Attestation Evidence */
301 case TCG_PTS_REQ_FUNCT_COMP_EVID:
302 break;
303 case TCG_PTS_GEN_ATTEST_EVID:
304 break;
305 case TCG_PTS_REQ_FILE_MEAS:
306 {
307 tcg_pts_attr_req_file_meas_t *attr_cast;
308 char *pathname;
309 u_int16_t request_id;
310 bool is_directory;
311 pts_file_meas_t *measurements;
312
313 attr_cast = (tcg_pts_attr_req_file_meas_t*)attr;
314 is_directory = attr_cast->get_directory_flag(attr_cast);
315 request_id = attr_cast->get_request_id(attr_cast);
316 pathname = attr_cast->get_pathname(attr_cast);
317
318 /* Do PTS File Measurements and send them to PTS-IMV */
319 DBG2(DBG_IMC, "measurement request %d for %s '%s'",
320 request_id, is_directory ? "directory" : "file",
321 pathname);
322 measurements = pts->do_measurements(pts, request_id,
323 pathname, is_directory);
324 if (!measurements)
325 {
326 /* TODO handle error codes from measurements */
327 return TNC_RESULT_FATAL;
328 }
329 attr = tcg_pts_attr_file_meas_create(measurements);
330 attr->set_noskip_flag(attr, TRUE);
331 attr_list->insert_last(attr_list, attr);
332 break;
333 }
334 /* TODO: Not implemented yet */
335 case TCG_PTS_DH_NONCE_PARAMS_REQ:
336 case TCG_PTS_DH_NONCE_FINISH:
337 case TCG_PTS_REQ_FILE_META:
338 case TCG_PTS_REQ_INTEG_MEAS_LOG:
339 /* Attributes using XML */
340 case TCG_PTS_REQ_TEMPL_REF_MANI_SET_META:
341 case TCG_PTS_UPDATE_TEMPL_REF_MANI:
342 /* On Windows only*/
343 case TCG_PTS_REQ_REGISTRY_VALUE:
344 /* Received on IMV side only*/
345 case TCG_PTS_PROTO_CAPS:
346 case TCG_PTS_DH_NONCE_PARAMS_RESP:
347 case TCG_PTS_MEAS_ALGO_SELECTION:
348 case TCG_PTS_TPM_VERSION_INFO:
349 case TCG_PTS_TEMPL_REF_MANI_SET_META:
350 case TCG_PTS_AIK:
351 case TCG_PTS_SIMPLE_COMP_EVID:
352 case TCG_PTS_SIMPLE_EVID_FINAL:
353 case TCG_PTS_VERIFICATION_RESULT:
354 case TCG_PTS_INTEG_REPORT:
355 case TCG_PTS_UNIX_FILE_META:
356 case TCG_PTS_FILE_MEAS:
357 case TCG_PTS_INTEG_MEAS_LOG:
358 default:
359 DBG1(DBG_IMC, "received unsupported attribute '%N'",
360 tcg_attr_names, attr->get_type(attr));
361 break;
362 }
363
364
365 }
366 }
367 enumerator->destroy(enumerator);
368 pa_tnc_msg->destroy(pa_tnc_msg);
369
370 result = TNC_RESULT_SUCCESS;
371
372 if (attr_list->get_count(attr_list))
373 {
374 pa_tnc_msg = pa_tnc_msg_create();
375
376 enumerator = attr_list->create_enumerator(attr_list);
377 while (enumerator->enumerate(enumerator, &attr))
378 {
379 pa_tnc_msg->add_attribute(pa_tnc_msg, attr);
380 }
381 enumerator->destroy(enumerator);
382
383 pa_tnc_msg->build(pa_tnc_msg);
384 result = imc_attestation->send_message(imc_attestation, connection_id,
385 pa_tnc_msg->get_encoding(pa_tnc_msg));
386
387 attr_list->destroy(attr_list);
388 pa_tnc_msg->destroy(pa_tnc_msg);
389 }
390
391 return result;
392 }
393
394 /**
395 * see section 3.7.5 of TCG TNC IF-IMC Specification 1.2
396 */
397 TNC_Result TNC_IMC_BatchEnding(TNC_IMCID imc_id,
398 TNC_ConnectionID connection_id)
399 {
400 if (!imc_attestation)
401 {
402 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
403 return TNC_RESULT_NOT_INITIALIZED;
404 }
405 return TNC_RESULT_SUCCESS;
406 }
407
408 /**
409 * see section 3.7.6 of TCG TNC IF-IMC Specification 1.2
410 */
411 TNC_Result TNC_IMC_Terminate(TNC_IMCID imc_id)
412 {
413 if (!imc_attestation)
414 {
415 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
416 return TNC_RESULT_NOT_INITIALIZED;
417 }
418 imc_attestation->destroy(imc_attestation);
419 imc_attestation = NULL;
420
421 return TNC_RESULT_SUCCESS;
422 }
423
424 /**
425 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.2
426 */
427 TNC_Result TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id,
428 TNC_TNCC_BindFunctionPointer bind_function)
429 {
430 if (!imc_attestation)
431 {
432 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
433 return TNC_RESULT_NOT_INITIALIZED;
434 }
435 return imc_attestation->bind_functions(imc_attestation, bind_function);
436 }