refactored PTS measurement algorithms
[strongswan.git] / src / libimcv / plugins / imv_attestation / imv_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 "imv_attestation_state.h"
17
18 #include <imv/imv_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 #include <tcg/tcg_attr.h>
23
24 #include <tcg/tcg_pts_attr_req_proto_caps.h>
25 #include <tcg/tcg_pts_attr_meas_algo.h>
26 #include <tcg/tcg_pts_attr_get_tpm_version_info.h>
27 #include <tcg/tcg_pts_attr_get_aik.h>
28 #include <tcg/tcg_pts_attr_req_funct_comp_evid.h>
29 #include <tcg/tcg_pts_attr_gen_attest_evid.h>
30 #include <tcg/tcg_pts_attr_req_file_meas.h>
31
32 #include <tncif_pa_subtypes.h>
33
34 #include <pen/pen.h>
35 #include <debug.h>
36 #include <utils/linked_list.h>
37
38 #include <trousers/tss.h>
39 #include <trousers/trousers.h>
40
41 /* IMV definitions */
42
43 static const char imv_name[] = "Attestation";
44
45 #define IMV_VENDOR_ID PEN_TCG
46 #define IMV_SUBTYPE PA_SUBTYPE_TCG_PTS
47
48 /**
49 * UTF-8 encoding of the character used to delimiter the filename
50 */
51 #define SOLIDUS_UTF 0x002F
52 #define REVERSE_SOLIDUS_UTF 0x005C
53
54 static imv_agent_t *imv_attestation;
55
56 /**
57 * List of files and directories to measure
58 */
59 static linked_list_t *file_list, *directory_list;
60
61 /**
62 * Monotonic increasing number for Request File Measurement attribute
63 */
64 static u_int16_t request_id_counter = 0;
65
66 /**
67 * Struct to hold file or directory name with the request ID for Request File Measurement attribute
68 */
69 typedef struct measurement_req_entry_t measurement_req_entry_t;
70
71 struct measurement_req_entry_t {
72 char *path;
73 u_int16_t request_id;
74 };
75
76 /**
77 * see section 3.7.1 of TCG TNC IF-IMV Specification 1.2
78 */
79 TNC_Result TNC_IMV_Initialize(TNC_IMVID imv_id,
80 TNC_Version min_version,
81 TNC_Version max_version,
82 TNC_Version *actual_version)
83 {
84 if (imv_attestation)
85 {
86 DBG1(DBG_IMV, "IMV \"%s\" has already been initialized", imv_name);
87 return TNC_RESULT_ALREADY_INITIALIZED;
88 }
89 imv_attestation = imv_agent_create(imv_name, IMV_VENDOR_ID, IMV_SUBTYPE,
90 imv_id, actual_version);
91 if (!imv_attestation)
92 {
93 return TNC_RESULT_FATAL;
94 }
95 if (min_version > TNC_IFIMV_VERSION_1 || max_version < TNC_IFIMV_VERSION_1)
96 {
97 DBG1(DBG_IMV, "no common IF-IMV version");
98 return TNC_RESULT_NO_COMMON_VERSION;
99 }
100 return TNC_RESULT_SUCCESS;
101 }
102
103 /**
104 * see section 3.7.2 of TCG TNC IF-IMV Specification 1.2
105 */
106 TNC_Result TNC_IMV_NotifyConnectionChange(TNC_IMVID imv_id,
107 TNC_ConnectionID connection_id,
108 TNC_ConnectionState new_state)
109 {
110 imv_state_t *state;
111 imv_attestation_state_t *attestation_state;
112 enumerator_t *enumerator;
113 char *files;
114 char *directories;
115 measurement_req_entry_t *entry;
116 char *token;
117
118 if (!imv_attestation)
119 {
120 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
121 return TNC_RESULT_NOT_INITIALIZED;
122 }
123 switch (new_state)
124 {
125 case TNC_CONNECTION_STATE_CREATE:
126 state = imv_attestation_state_create(connection_id);
127 return imv_attestation->create_state(imv_attestation, state);
128 case TNC_CONNECTION_STATE_DELETE:
129 return imv_attestation->delete_state(imv_attestation, connection_id);
130 case TNC_CONNECTION_STATE_HANDSHAKE:
131 if (!imv_attestation->get_state(imv_attestation, connection_id, &state))
132 {
133 return TNC_RESULT_FATAL;
134 }
135 state->change_state(state, new_state);
136 attestation_state = (imv_attestation_state_t*)state;
137
138 /** Get the files to measure for
139 * PTS Request File Measurement attribute
140 */
141
142 file_list = linked_list_create();
143 directory_list = linked_list_create();
144
145 files = lib->settings->get_str(lib->settings,
146 "libimcv.plugins.imc-attestation.files", "none");
147 enumerator = enumerator_create_token(files, " ", " ");
148 while (enumerator->enumerate(enumerator, &token))
149 {
150 entry = malloc_thing(measurement_req_entry_t);
151 token = strdup(token);
152 entry->path = token;
153 entry->request_id = request_id_counter;
154 file_list->insert_last(file_list, entry);
155 free(token);
156 request_id_counter ++;
157 }
158
159 /** Get the directories to measure for
160 * PTS Request File Measurement attribute
161 */
162
163 directories = lib->settings->get_str(lib->settings,
164 "libimcv.plugins.imc-attestation.directories", "none");
165 enumerator = enumerator_create_token(directories, " ", " ");
166 while (enumerator->enumerate(enumerator, &token))
167 {
168 entry = malloc_thing(measurement_req_entry_t);
169 token = strdup(token);
170 entry->path = token;
171 entry->request_id = request_id_counter;
172 directory_list->insert_last(directory_list, entry);
173 free(token);
174 request_id_counter ++;
175 }
176 enumerator->destroy(enumerator);
177 return TNC_RESULT_SUCCESS;
178 default:
179 return imv_attestation->change_state(imv_attestation, connection_id,
180 new_state, NULL);
181 }
182 }
183
184 static TNC_Result send_message(TNC_ConnectionID connection_id)
185 {
186 pa_tnc_msg_t *msg;
187 pa_tnc_attr_t *attr;
188 TNC_Result result;
189 imv_state_t *state;
190 imv_attestation_state_t *attestation_state;
191 imv_attestation_handshake_state_t handshake_state;
192
193 if (!imv_attestation->get_state(imv_attestation, connection_id, &state))
194 {
195 return TNC_RESULT_FATAL;
196 }
197
198 attestation_state = (imv_attestation_state_t*)state;
199 handshake_state = attestation_state->get_handshake_state(attestation_state);
200
201 /* Switch on the attribute type IMV has received */
202 switch (handshake_state)
203 {
204 case IMV_ATTESTATION_STATE_INIT:
205 {
206 pts_proto_caps_flag_t flags;
207
208 /* Send Request Protocol Capabilities attribute */
209 flags = PTS_PROTO_CAPS_T | PTS_PROTO_CAPS_VER |
210 PTS_PROTO_CAPS_CURRENT;
211 attr = tcg_pts_attr_req_proto_caps_create(flags);
212 break;
213 }
214 case IMV_ATTESTATION_STATE_PROTO_CAP:
215 {
216 pts_meas_algorithms_t algorithms;
217
218 /* Send Measurement Algorithms attribute */
219 algorithms = PTS_MEAS_ALGO_SHA1 | PTS_MEAS_ALGO_SHA256 |
220 PTS_MEAS_ALGO_SHA384;
221 attr = tcg_pts_attr_meas_algo_create(algorithms, FALSE);
222 break;
223 }
224 case IMV_ATTESTATION_STATE_MEAS_ALGO:
225 {
226 /* Send Get TPM Version Information attribute */
227 attr = tcg_pts_attr_get_tpm_version_info_create();
228 break;
229 }
230 case IMV_ATTESTATION_STATE_TPM_INFO:
231 {
232 /* Send Get AIK attribute */
233 /* TODO: Uncomment when the retrieving of AIK on IMC side is implemented */
234 //attr = tcg_pts_attr_get_aik_create();
235 //break;
236 }
237 case IMV_ATTESTATION_STATE_AIK:
238 {
239 /* Send Request File Measurement attribute */
240 enumerator_t *enumerator;
241 measurement_req_entry_t *entry;
242 char *path;
243 u_int16_t request_id;
244 u_int32_t delimiter = SOLIDUS_UTF;
245
246 msg = pa_tnc_msg_create();
247
248 /**
249 * Add files to measure to PTS Request File Measurement attribute
250 */
251 enumerator = enumerator_create_single(file_list, NULL);
252 while (enumerator->enumerate(enumerator, &entry))
253 {
254 attr = tcg_pts_attr_req_file_meas_create(false,
255 entry->request_id, delimiter,
256 chunk_create(entry->path, strlen(entry->path)));
257 attr->set_noskip_flag(attr, TRUE);
258 msg->add_attribute(msg, attr);
259 }
260 /** Add directories to measure to PTS Request File Measurement attribute
261 */
262 enumerator = enumerator_create_single(directory_list, NULL);
263 while (enumerator->enumerate(enumerator, &entry))
264 {
265 attr = tcg_pts_attr_req_file_meas_create(true,
266 entry->request_id, delimiter,
267 chunk_create(entry->path, strlen(entry->path)));
268 attr->set_noskip_flag(attr, TRUE);
269 msg->add_attribute(msg, attr);
270 }
271 enumerator->destroy(enumerator);
272 goto end;
273 }
274 case IMV_ATTESTATION_STATE_SIMPLE_COMP_EVID:
275 case IMV_ATTESTATION_STATE_SIMPLE_EVID_FINAL:
276 case IMV_ATTESTATION_STATE_FILE_METADATA:
277 case IMV_ATTESTATION_STATE_FILE_MEAS:
278 case IMV_ATTESTATION_STATE_IML:
279 DBG1(DBG_IMV, "Attestation IMV has nothing to send: \"%s\"", handshake_state);
280 return TNC_RESULT_FATAL;
281 default:
282 DBG1(DBG_IMV, "Attestation IMV is in unknown state: \"%s\"", handshake_state);
283 return TNC_RESULT_FATAL;
284 }
285
286 attr->set_noskip_flag(attr, TRUE);
287 msg = pa_tnc_msg_create();
288 msg->add_attribute(msg, attr);
289
290 end:
291 msg->build(msg);
292 result = imv_attestation->send_message(imv_attestation, connection_id,
293 msg->get_encoding(msg));
294 msg->destroy(msg);
295
296 return result;
297 }
298
299 /**
300 * see section 3.7.3 of TCG TNC IF-IMV Specification 1.2
301 */
302 TNC_Result TNC_IMV_ReceiveMessage(TNC_IMVID imv_id,
303 TNC_ConnectionID connection_id,
304 TNC_BufferReference msg,
305 TNC_UInt32 msg_len,
306 TNC_MessageType msg_type)
307 {
308 pa_tnc_msg_t *pa_tnc_msg;
309 pa_tnc_attr_t *attr;
310 imv_state_t *state;
311 imv_attestation_state_t *imv_attestation_state;
312 enumerator_t *enumerator;
313 TNC_Result result;
314 bool fatal_error = FALSE;
315
316 if (!imv_attestation)
317 {
318 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
319 return TNC_RESULT_NOT_INITIALIZED;
320 }
321
322 /* get current IMV state */
323 if (!imv_attestation->get_state(imv_attestation, connection_id, &state))
324 {
325 return TNC_RESULT_FATAL;
326 }
327
328 /* parse received PA-TNC message and automatically handle any errors */
329 result = imv_attestation->receive_message(imv_attestation, connection_id,
330 chunk_create(msg, msg_len), msg_type,
331 &pa_tnc_msg);
332
333 /* no parsed PA-TNC attributes available if an error occurred */
334 if (!pa_tnc_msg)
335 {
336 return result;
337 }
338
339 /* analyze PA-TNC attributes */
340 enumerator = pa_tnc_msg->create_attribute_enumerator(pa_tnc_msg);
341 while (enumerator->enumerate(enumerator, &attr))
342 {
343 if (attr->get_vendor_id(attr) == PEN_IETF &&
344 attr->get_type(attr) == IETF_ATTR_PA_TNC_ERROR)
345 {
346 ietf_attr_pa_tnc_error_t *error_attr;
347 pa_tnc_error_code_t error_code;
348 chunk_t msg_info, attr_info;
349 u_int32_t offset;
350
351 error_attr = (ietf_attr_pa_tnc_error_t*)attr;
352 error_code = error_attr->get_error_code(error_attr);
353 msg_info = error_attr->get_msg_info(error_attr);
354
355 DBG1(DBG_IMV, "received PA-TNC error '%N' concerning message %#B",
356 pa_tnc_error_code_names, error_code, &msg_info);
357 switch (error_code)
358 {
359 case PA_ERROR_INVALID_PARAMETER:
360 offset = error_attr->get_offset(error_attr);
361 DBG1(DBG_IMV, " occurred at offset of %u bytes", offset);
362 break;
363 case PA_ERROR_ATTR_TYPE_NOT_SUPPORTED:
364 attr_info = error_attr->get_attr_info(error_attr);
365 DBG1(DBG_IMV, " unsupported attribute %#B", &attr_info);
366 break;
367 default:
368 break;
369 }
370 fatal_error = TRUE;
371 }
372 else if (attr->get_vendor_id(attr) == PEN_TCG)
373 {
374 /**
375 * Handle TCG PTS attributes
376 */
377 switch(attr->get_type(attr))
378 {
379 case TCG_PTS_PROTO_CAPS:
380 break;
381 case TCG_PTS_MEAS_ALGO_SELECTION:
382 break;
383 case TCG_PTS_TPM_VERSION_INFO:
384 break;
385 case TCG_PTS_AIK:
386 break;
387
388 /* PTS-based Attestation Evidence */
389 case TCG_PTS_SIMPLE_COMP_EVID:
390 break;
391 case TCG_PTS_SIMPLE_EVID_FINAL:
392 break;
393 case TCG_PTS_FILE_MEAS:
394 break;
395
396 /* TODO: Not implemented yet */
397 case TCG_PTS_DH_NONCE_PARAMS_RESP:
398 case TCG_PTS_UNIX_FILE_META:
399 case TCG_PTS_INTEG_MEAS_LOG:
400 /* Attributes using XML */
401 case TCG_PTS_TEMPL_REF_MANI_SET_META:
402 case TCG_PTS_VERIFICATION_RESULT:
403 case TCG_PTS_INTEG_REPORT:
404 /* On Windows only*/
405 case TCG_PTS_WIN_FILE_META:
406 case TCG_PTS_REGISTRY_VALUE:
407 /* Received on IMC side only*/
408 case TCG_PTS_REQ_PROTO_CAPS:
409 case TCG_PTS_DH_NONCE_PARAMS_REQ:
410 case TCG_PTS_DH_NONCE_FINISH:
411 case TCG_PTS_MEAS_ALGO:
412 case TCG_PTS_GET_TPM_VERSION_INFO:
413 case TCG_PTS_REQ_TEMPL_REF_MANI_SET_META:
414 case TCG_PTS_UPDATE_TEMPL_REF_MANI:
415 case TCG_PTS_GET_AIK:
416 case TCG_PTS_REQ_FUNCT_COMP_EVID:
417 case TCG_PTS_GEN_ATTEST_EVID:
418 case TCG_PTS_REQ_FILE_META:
419 case TCG_PTS_REQ_FILE_MEAS:
420 case TCG_PTS_REQ_INTEG_MEAS_LOG:
421 default:
422 DBG1(DBG_IMV, "received unsupported attribute '%N'",
423 tcg_attr_names, attr->get_type(attr));
424 break;
425 }
426 }
427 }
428 enumerator->destroy(enumerator);
429 pa_tnc_msg->destroy(pa_tnc_msg);
430
431 if (fatal_error)
432 {
433 state->set_recommendation(state,
434 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
435 TNC_IMV_EVALUATION_RESULT_ERROR);
436 return imv_attestation->provide_recommendation(imv_attestation, connection_id);
437 }
438
439 return imv_attestation->provide_recommendation(imv_attestation, connection_id);
440 }
441
442 /**
443 * see section 3.7.4 of TCG TNC IF-IMV Specification 1.2
444 */
445 TNC_Result TNC_IMV_SolicitRecommendation(TNC_IMVID imv_id,
446 TNC_ConnectionID connection_id)
447 {
448 if (!imv_attestation)
449 {
450 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
451 return TNC_RESULT_NOT_INITIALIZED;
452 }
453 return imv_attestation->provide_recommendation(imv_attestation, connection_id);
454 }
455
456 /**
457 * see section 3.7.5 of TCG TNC IF-IMV Specification 1.2
458 */
459 TNC_Result TNC_IMV_BatchEnding(TNC_IMVID imv_id,
460 TNC_ConnectionID connection_id)
461 {
462 if (!imv_attestation)
463 {
464 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
465 return TNC_RESULT_NOT_INITIALIZED;
466 }
467 return TNC_RESULT_SUCCESS;
468 }
469
470 /**
471 * see section 3.7.6 of TCG TNC IF-IMV Specification 1.2
472 */
473 TNC_Result TNC_IMV_Terminate(TNC_IMVID imv_id)
474 {
475 if (!imv_attestation)
476 {
477 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
478 return TNC_RESULT_NOT_INITIALIZED;
479 }
480 imv_attestation->destroy(imv_attestation);
481 imv_attestation = NULL;
482
483 return TNC_RESULT_SUCCESS;
484 }
485
486 /**
487 * see section 4.2.8.1 of TCG TNC IF-IMV Specification 1.2
488 */
489 TNC_Result TNC_IMV_ProvideBindFunction(TNC_IMVID imv_id,
490 TNC_TNCS_BindFunctionPointer bind_function)
491 {
492 if (!imv_attestation)
493 {
494 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
495 return TNC_RESULT_NOT_INITIALIZED;
496 }
497 return imv_attestation->bind_functions(imv_attestation, bind_function);
498 }