2 * Copyright (C) 2011-2012 Sansar Choinyambuu, Andreas Steffen
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_state.h"
20 #include <imv/imv_lang_string.h>
21 #include "imv/imv_reason_string.h"
23 #include <collections/linked_list.h>
24 #include <utils/debug.h>
26 typedef struct private_imv_attestation_state_t private_imv_attestation_state_t
;
27 typedef struct file_meas_request_t file_meas_request_t
;
28 typedef struct func_comp_t func_comp_t
;
31 * Private data of an imv_attestation_state_t object.
33 struct private_imv_attestation_state_t
{
36 * Public members of imv_attestation_state_t
38 imv_attestation_state_t
public;
43 TNC_ConnectionID connection_id
;
46 * TNCCS connection state
48 TNC_ConnectionState state
;
51 * Does the TNCCS connection support long message types?
56 * Does the TNCCS connection support exclusive delivery?
61 * Maximum PA-TNC message size for this TNCCS connection
63 u_int32_t max_msg_len
;
66 * IMV Attestation handshake state
68 imv_attestation_handshake_state_t handshake_state
;
71 * IMV action recommendation
73 TNC_IMV_Action_Recommendation rec
;
76 * IMV evaluation result
78 TNC_IMV_Evaluation_Result eval
;
81 * File Measurement Request counter
83 u_int16_t file_meas_request_counter
;
86 * List of PTS File/Directory Measurement requests
88 linked_list_t
*file_meas_requests
;
91 * List of Functional Components
93 linked_list_t
*components
;
101 * Measurement error flags
103 u_int32_t measurement_error
;
108 imv_reason_string_t
*reason_string
;
113 * PTS File/Directory Measurement request entry
115 struct file_meas_request_t
{
122 * PTS Functional Component entry
125 pts_component_t
*comp
;
130 * Frees a func_comp_t object
132 static void free_func_comp(func_comp_t
*this)
134 this->comp
->destroy(this->comp
);
139 * Supported languages
141 static char* languages
[] = { "en", "de", "mn" };
144 * Table of reason strings
146 static imv_lang_string_t reason_file_meas_fail
[] = {
147 { "en", "Incorrect file measurement" },
148 { "de", "Falsche Dateimessung" },
149 { "mn", "Буруу байгаа файл" },
153 static imv_lang_string_t reason_file_meas_pend
[] = {
154 { "en", "Pending file measurement" },
155 { "de", "Ausstehende Dateimessung" },
156 { "mn", "Xүлээгдэж байгаа файл" },
160 static imv_lang_string_t reason_comp_evid_fail
[] = {
161 { "en", "Incorrect component evidence" },
162 { "de", "Falsche Komponenten-Evidenz" },
163 { "mn", "Буруу компонент хэмжилт" },
167 static imv_lang_string_t reason_comp_evid_pend
[] = {
168 { "en", "Pending component evidence" },
169 { "de", "Ausstehende Komponenten-Evidenz" },
170 { "mn", "Xүлээгдэж компонент хэмжилт" },
174 static imv_lang_string_t reason_tpm_quote_fail
[] = {
175 { "en", "Invalid TPM Quote signature received" },
176 { "de", "Falsche TPM Quote Signature erhalten" },
177 { "mn", "Буруу TPM Quote гарын үсэг" },
181 METHOD(imv_state_t
, get_connection_id
, TNC_ConnectionID
,
182 private_imv_attestation_state_t
*this)
184 return this->connection_id
;
187 METHOD(imv_state_t
, has_long
, bool,
188 private_imv_attestation_state_t
*this)
190 return this->has_long
;
193 METHOD(imv_state_t
, has_excl
, bool,
194 private_imv_attestation_state_t
*this)
196 return this->has_excl
;
199 METHOD(imv_state_t
, set_flags
, void,
200 private_imv_attestation_state_t
*this, bool has_long
, bool has_excl
)
202 this->has_long
= has_long
;
203 this->has_excl
= has_excl
;
206 METHOD(imv_state_t
, set_max_msg_len
, void,
207 private_imv_attestation_state_t
*this, u_int32_t max_msg_len
)
209 this->max_msg_len
= max_msg_len
;
212 METHOD(imv_state_t
, get_max_msg_len
, u_int32_t
,
213 private_imv_attestation_state_t
*this)
215 return this->max_msg_len
;
218 METHOD(imv_state_t
, change_state
, void,
219 private_imv_attestation_state_t
*this, TNC_ConnectionState new_state
)
221 this->state
= new_state
;
224 METHOD(imv_state_t
, get_recommendation
, void,
225 private_imv_attestation_state_t
*this, TNC_IMV_Action_Recommendation
*rec
,
226 TNC_IMV_Evaluation_Result
*eval
)
232 METHOD(imv_state_t
, set_recommendation
, void,
233 private_imv_attestation_state_t
*this, TNC_IMV_Action_Recommendation rec
,
234 TNC_IMV_Evaluation_Result eval
)
240 METHOD(imv_state_t
, get_reason_string
, bool,
241 private_imv_attestation_state_t
*this, enumerator_t
*language_enumerator
,
242 chunk_t
*reason_string
, char **reason_language
)
244 *reason_language
= imv_lang_string_select_lang(language_enumerator
,
245 languages
, countof(languages
));
247 /* Instantiate a TNC Reason String object */
248 DESTROY_IF(this->reason_string
);
249 this->reason_string
= imv_reason_string_create(*reason_language
);
251 if (this->measurement_error
& IMV_ATTESTATION_ERROR_FILE_MEAS_FAIL
)
253 this->reason_string
->add_reason(this->reason_string
,
254 reason_file_meas_fail
);
256 if (this->measurement_error
& IMV_ATTESTATION_ERROR_FILE_MEAS_PEND
)
258 this->reason_string
->add_reason(this->reason_string
,
259 reason_file_meas_pend
);
261 if (this->measurement_error
& IMV_ATTESTATION_ERROR_COMP_EVID_FAIL
)
263 this->reason_string
->add_reason(this->reason_string
,
264 reason_comp_evid_fail
);
266 if (this->measurement_error
& IMV_ATTESTATION_ERROR_COMP_EVID_PEND
)
268 this->reason_string
->add_reason(this->reason_string
,
269 reason_comp_evid_pend
);
271 if (this->measurement_error
& IMV_ATTESTATION_ERROR_TPM_QUOTE_FAIL
)
273 this->reason_string
->add_reason(this->reason_string
,
274 reason_tpm_quote_fail
);
276 *reason_string
= this->reason_string
->get_encoding(this->reason_string
);
281 METHOD(imv_state_t
, get_remediation_instructions
, bool,
282 private_imv_attestation_state_t
*this, enumerator_t
*language_enumerator
,
283 chunk_t
*string
, char **lang_code
, char **uri
)
288 METHOD(imv_state_t
, destroy
, void,
289 private_imv_attestation_state_t
*this)
291 DESTROY_IF(this->reason_string
);
292 this->file_meas_requests
->destroy_function(this->file_meas_requests
, free
);
293 this->components
->destroy_function(this->components
, (void *)free_func_comp
);
294 this->pts
->destroy(this->pts
);
298 METHOD(imv_attestation_state_t
, get_handshake_state
,
299 imv_attestation_handshake_state_t
, private_imv_attestation_state_t
*this)
301 return this->handshake_state
;
304 METHOD(imv_attestation_state_t
, set_handshake_state
, void,
305 private_imv_attestation_state_t
*this,
306 imv_attestation_handshake_state_t new_state
)
308 this->handshake_state
= new_state
;
311 METHOD(imv_attestation_state_t
, get_pts
, pts_t
*,
312 private_imv_attestation_state_t
*this)
317 METHOD(imv_attestation_state_t
, add_file_meas_request
, u_int16_t
,
318 private_imv_attestation_state_t
*this, int file_id
, bool is_dir
)
320 file_meas_request_t
*request
;
322 request
= malloc_thing(file_meas_request_t
);
323 request
->id
= ++this->file_meas_request_counter
;
324 request
->file_id
= file_id
;
325 request
->is_dir
= is_dir
;
326 this->file_meas_requests
->insert_last(this->file_meas_requests
, request
);
328 return this->file_meas_request_counter
;
331 METHOD(imv_attestation_state_t
, check_off_file_meas_request
, bool,
332 private_imv_attestation_state_t
*this, u_int16_t id
, int *file_id
,
335 enumerator_t
*enumerator
;
336 file_meas_request_t
*request
;
339 enumerator
= this->file_meas_requests
->create_enumerator(this->file_meas_requests
);
340 while (enumerator
->enumerate(enumerator
, &request
))
342 if (request
->id
== id
)
345 *file_id
= request
->file_id
;
346 *is_dir
= request
->is_dir
;
347 this->file_meas_requests
->remove_at(this->file_meas_requests
, enumerator
);
352 enumerator
->destroy(enumerator
);
356 METHOD(imv_attestation_state_t
, get_file_meas_request_count
, int,
357 private_imv_attestation_state_t
*this)
359 return this->file_meas_requests
->get_count(this->file_meas_requests
);
362 METHOD(imv_attestation_state_t
, create_component
, pts_component_t
*,
363 private_imv_attestation_state_t
*this, pts_comp_func_name_t
*name
,
364 u_int32_t depth
, pts_database_t
*pts_db
)
366 enumerator_t
*enumerator
;
367 func_comp_t
*entry
, *new_entry
;
368 pts_component_t
*component
;
371 enumerator
= this->components
->create_enumerator(this->components
);
372 while (enumerator
->enumerate(enumerator
, &entry
))
374 if (name
->equals(name
, entry
->comp
->get_comp_func_name(entry
->comp
)))
380 enumerator
->destroy(enumerator
);
384 if (name
->get_qualifier(name
) == entry
->qualifier
)
386 /* duplicate entry */
389 new_entry
= malloc_thing(func_comp_t
);
390 new_entry
->qualifier
= name
->get_qualifier(name
);
391 new_entry
->comp
= entry
->comp
->get_ref(entry
->comp
);
392 this->components
->insert_last(this->components
, new_entry
);
397 component
= pts_components
->create(pts_components
, name
, depth
, pts_db
);
400 /* unsupported component */
403 new_entry
= malloc_thing(func_comp_t
);
404 new_entry
->qualifier
= name
->get_qualifier(name
);
405 new_entry
->comp
= component
;
406 this->components
->insert_last(this->components
, new_entry
);
411 METHOD(imv_attestation_state_t
, get_component
, pts_component_t
*,
412 private_imv_attestation_state_t
*this, pts_comp_func_name_t
*name
)
414 enumerator_t
*enumerator
;
416 pts_component_t
*found
= NULL
;
418 enumerator
= this->components
->create_enumerator(this->components
);
419 while (enumerator
->enumerate(enumerator
, &entry
))
421 if (name
->equals(name
, entry
->comp
->get_comp_func_name(entry
->comp
)) &&
422 name
->get_qualifier(name
) == entry
->qualifier
)
428 enumerator
->destroy(enumerator
);
432 METHOD(imv_attestation_state_t
, get_measurement_error
, u_int32_t
,
433 private_imv_attestation_state_t
*this)
435 return this->measurement_error
;
438 METHOD(imv_attestation_state_t
, set_measurement_error
, void,
439 private_imv_attestation_state_t
*this, u_int32_t error
)
441 this->measurement_error
|= error
;
444 METHOD(imv_attestation_state_t
, finalize_components
, void,
445 private_imv_attestation_state_t
*this)
449 while (this->components
->remove_last(this->components
,
450 (void**)&entry
) == SUCCESS
)
452 if (!entry
->comp
->finalize(entry
->comp
, entry
->qualifier
))
454 set_measurement_error(this, IMV_ATTESTATION_ERROR_COMP_EVID_PEND
);
456 free_func_comp(entry
);
460 METHOD(imv_attestation_state_t
, components_finalized
, bool,
461 private_imv_attestation_state_t
*this)
463 return this->components
->get_count(this->components
) == 0;
467 * Described in header.
469 imv_state_t
*imv_attestation_state_create(TNC_ConnectionID connection_id
)
471 private_imv_attestation_state_t
*this;
476 .get_connection_id
= _get_connection_id
,
477 .has_long
= _has_long
,
478 .has_excl
= _has_excl
,
479 .set_flags
= _set_flags
,
480 .set_max_msg_len
= _set_max_msg_len
,
481 .get_max_msg_len
= _get_max_msg_len
,
482 .change_state
= _change_state
,
483 .get_recommendation
= _get_recommendation
,
484 .set_recommendation
= _set_recommendation
,
485 .get_reason_string
= _get_reason_string
,
486 .get_remediation_instructions
= _get_remediation_instructions
,
489 .get_handshake_state
= _get_handshake_state
,
490 .set_handshake_state
= _set_handshake_state
,
492 .add_file_meas_request
= _add_file_meas_request
,
493 .check_off_file_meas_request
= _check_off_file_meas_request
,
494 .get_file_meas_request_count
= _get_file_meas_request_count
,
495 .create_component
= _create_component
,
496 .get_component
= _get_component
,
497 .finalize_components
= _finalize_components
,
498 .components_finalized
= _components_finalized
,
499 .get_measurement_error
= _get_measurement_error
,
500 .set_measurement_error
= _set_measurement_error
,
502 .connection_id
= connection_id
,
503 .state
= TNC_CONNECTION_STATE_CREATE
,
504 .handshake_state
= IMV_ATTESTATION_STATE_INIT
,
505 .rec
= TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION
,
506 .eval
= TNC_IMV_EVALUATION_RESULT_DONT_KNOW
,
507 .file_meas_requests
= linked_list_create(),
508 .components
= linked_list_create(),
509 .pts
= pts_create(FALSE
),
512 return &this->public.interface
;