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 <utils/lexparser.h>
21 #include <collections/linked_list.h>
22 #include <utils/debug.h>
24 typedef struct private_imv_attestation_state_t private_imv_attestation_state_t
;
25 typedef struct file_meas_request_t file_meas_request_t
;
26 typedef struct func_comp_t func_comp_t
;
29 * Private data of an imv_attestation_state_t object.
31 struct private_imv_attestation_state_t
{
34 * Public members of imv_attestation_state_t
36 imv_attestation_state_t
public;
41 TNC_ConnectionID connection_id
;
44 * TNCCS connection state
46 TNC_ConnectionState state
;
49 * Does the TNCCS connection support long message types?
54 * Does the TNCCS connection support exclusive delivery?
59 * Maximum PA-TNC message size for this TNCCS connection
61 u_int32_t max_msg_len
;
64 * IMV Attestation handshake state
66 imv_attestation_handshake_state_t handshake_state
;
69 * IMV action recommendation
71 TNC_IMV_Action_Recommendation rec
;
74 * IMV evaluation result
76 TNC_IMV_Evaluation_Result eval
;
79 * File Measurement Request counter
81 u_int16_t file_meas_request_counter
;
84 * List of PTS File/Directory Measurement requests
86 linked_list_t
*file_meas_requests
;
89 * List of Functional Components
91 linked_list_t
*components
;
101 bool measurement_error
;
106 * PTS File/Directory Measurement request entry
108 struct file_meas_request_t
{
115 * PTS Functional Component entry
118 pts_component_t
*comp
;
123 * Frees a func_comp_t object
125 static void free_func_comp(func_comp_t
*this)
127 this->comp
->destroy(this->comp
);
131 typedef struct entry_t entry_t
;
134 * Define an internal reason string entry
142 * Table of multi-lingual reason string entries
144 static entry_t reasons
[] = {
145 { "en", "IMV Attestation: Incorrect/pending file measurement/component"
146 " evidence or invalid TPM Quote signature received" },
147 { "mn", "IMV Attestation: Буруу/хүлээгдэж байгаа файл/компонент хэмжилт "
148 "эсвэл буруу TPM Quote гарын үсэг" },
149 { "de", "IMV Attestation: Falsche/Fehlende Dateimessung/Komponenten Beweis "
150 "oder ungültige TPM Quote Unterschrift ist erhalten" },
153 METHOD(imv_state_t
, get_connection_id
, TNC_ConnectionID
,
154 private_imv_attestation_state_t
*this)
156 return this->connection_id
;
159 METHOD(imv_state_t
, has_long
, bool,
160 private_imv_attestation_state_t
*this)
162 return this->has_long
;
165 METHOD(imv_state_t
, has_excl
, bool,
166 private_imv_attestation_state_t
*this)
168 return this->has_excl
;
171 METHOD(imv_state_t
, set_flags
, void,
172 private_imv_attestation_state_t
*this, bool has_long
, bool has_excl
)
174 this->has_long
= has_long
;
175 this->has_excl
= has_excl
;
178 METHOD(imv_state_t
, set_max_msg_len
, void,
179 private_imv_attestation_state_t
*this, u_int32_t max_msg_len
)
181 this->max_msg_len
= max_msg_len
;
184 METHOD(imv_state_t
, get_max_msg_len
, u_int32_t
,
185 private_imv_attestation_state_t
*this)
187 return this->max_msg_len
;
190 METHOD(imv_state_t
, change_state
, void,
191 private_imv_attestation_state_t
*this, TNC_ConnectionState new_state
)
193 this->state
= new_state
;
196 METHOD(imv_state_t
, get_recommendation
, void,
197 private_imv_attestation_state_t
*this, TNC_IMV_Action_Recommendation
*rec
,
198 TNC_IMV_Evaluation_Result
*eval
)
204 METHOD(imv_state_t
, set_recommendation
, void,
205 private_imv_attestation_state_t
*this, TNC_IMV_Action_Recommendation rec
,
206 TNC_IMV_Evaluation_Result eval
)
212 METHOD(imv_state_t
, get_reason_string
, bool,
213 private_imv_attestation_state_t
*this, enumerator_t
*language_enumerator
,
214 char **reason_string
, char **reason_language
)
220 /* set the default language */
221 *reason_language
= reasons
[0].lang
;
222 *reason_string
= reasons
[0].string
;
224 while (language_enumerator
->enumerate(language_enumerator
, &lang
))
226 for (i
= 0 ; i
< countof(reasons
); i
++)
228 if (streq(lang
, reasons
[i
].lang
))
231 *reason_language
= reasons
[i
].lang
;
232 *reason_string
= reasons
[i
].string
;
245 METHOD(imv_state_t
, get_remediation_instructions
, bool,
246 private_imv_attestation_state_t
*this, enumerator_t
*language_enumerator
,
247 char **string
, char **lang_code
)
252 METHOD(imv_state_t
, destroy
, void,
253 private_imv_attestation_state_t
*this)
255 this->file_meas_requests
->destroy_function(this->file_meas_requests
, free
);
256 this->components
->destroy_function(this->components
, (void *)free_func_comp
);
257 this->pts
->destroy(this->pts
);
261 METHOD(imv_attestation_state_t
, get_handshake_state
,
262 imv_attestation_handshake_state_t
, private_imv_attestation_state_t
*this)
264 return this->handshake_state
;
267 METHOD(imv_attestation_state_t
, set_handshake_state
, void,
268 private_imv_attestation_state_t
*this,
269 imv_attestation_handshake_state_t new_state
)
271 this->handshake_state
= new_state
;
274 METHOD(imv_attestation_state_t
, get_pts
, pts_t
*,
275 private_imv_attestation_state_t
*this)
280 METHOD(imv_attestation_state_t
, add_file_meas_request
, u_int16_t
,
281 private_imv_attestation_state_t
*this, int file_id
, bool is_dir
)
283 file_meas_request_t
*request
;
285 request
= malloc_thing(file_meas_request_t
);
286 request
->id
= ++this->file_meas_request_counter
;
287 request
->file_id
= file_id
;
288 request
->is_dir
= is_dir
;
289 this->file_meas_requests
->insert_last(this->file_meas_requests
, request
);
291 return this->file_meas_request_counter
;
294 METHOD(imv_attestation_state_t
, check_off_file_meas_request
, bool,
295 private_imv_attestation_state_t
*this, u_int16_t id
, int *file_id
,
298 enumerator_t
*enumerator
;
299 file_meas_request_t
*request
;
302 enumerator
= this->file_meas_requests
->create_enumerator(this->file_meas_requests
);
303 while (enumerator
->enumerate(enumerator
, &request
))
305 if (request
->id
== id
)
308 *file_id
= request
->file_id
;
309 *is_dir
= request
->is_dir
;
310 this->file_meas_requests
->remove_at(this->file_meas_requests
, enumerator
);
315 enumerator
->destroy(enumerator
);
319 METHOD(imv_attestation_state_t
, get_file_meas_request_count
, int,
320 private_imv_attestation_state_t
*this)
322 return this->file_meas_requests
->get_count(this->file_meas_requests
);
325 METHOD(imv_attestation_state_t
, create_component
, pts_component_t
*,
326 private_imv_attestation_state_t
*this, pts_comp_func_name_t
*name
,
327 u_int32_t depth
, pts_database_t
*pts_db
)
329 enumerator_t
*enumerator
;
330 func_comp_t
*entry
, *new_entry
;
331 pts_component_t
*component
;
334 enumerator
= this->components
->create_enumerator(this->components
);
335 while (enumerator
->enumerate(enumerator
, &entry
))
337 if (name
->equals(name
, entry
->comp
->get_comp_func_name(entry
->comp
)))
343 enumerator
->destroy(enumerator
);
347 if (name
->get_qualifier(name
) == entry
->qualifier
)
349 /* duplicate entry */
352 new_entry
= malloc_thing(func_comp_t
);
353 new_entry
->qualifier
= name
->get_qualifier(name
);
354 new_entry
->comp
= entry
->comp
->get_ref(entry
->comp
);
355 this->components
->insert_last(this->components
, new_entry
);
360 component
= pts_components
->create(pts_components
, name
, depth
, pts_db
);
363 /* unsupported component */
366 new_entry
= malloc_thing(func_comp_t
);
367 new_entry
->qualifier
= name
->get_qualifier(name
);
368 new_entry
->comp
= component
;
369 this->components
->insert_last(this->components
, new_entry
);
374 METHOD(imv_attestation_state_t
, get_component
, pts_component_t
*,
375 private_imv_attestation_state_t
*this, pts_comp_func_name_t
*name
)
377 enumerator_t
*enumerator
;
379 pts_component_t
*found
= NULL
;
381 enumerator
= this->components
->create_enumerator(this->components
);
382 while (enumerator
->enumerate(enumerator
, &entry
))
384 if (name
->equals(name
, entry
->comp
->get_comp_func_name(entry
->comp
)) &&
385 name
->get_qualifier(name
) == entry
->qualifier
)
391 enumerator
->destroy(enumerator
);
395 METHOD(imv_attestation_state_t
, get_measurement_error
, bool,
396 private_imv_attestation_state_t
*this)
398 return this->measurement_error
;
401 METHOD(imv_attestation_state_t
, set_measurement_error
, void,
402 private_imv_attestation_state_t
*this)
404 this->measurement_error
= TRUE
;
407 METHOD(imv_attestation_state_t
, finalize_components
, void,
408 private_imv_attestation_state_t
*this)
412 while (this->components
->remove_last(this->components
,
413 (void**)&entry
) == SUCCESS
)
415 if (!entry
->comp
->finalize(entry
->comp
, entry
->qualifier
))
417 _set_measurement_error(this);
419 free_func_comp(entry
);
423 METHOD(imv_attestation_state_t
, components_finalized
, bool,
424 private_imv_attestation_state_t
*this)
426 return this->components
->get_count(this->components
) == 0;
430 * Described in header.
432 imv_state_t
*imv_attestation_state_create(TNC_ConnectionID connection_id
)
434 private_imv_attestation_state_t
*this;
439 .get_connection_id
= _get_connection_id
,
440 .has_long
= _has_long
,
441 .has_excl
= _has_excl
,
442 .set_flags
= _set_flags
,
443 .set_max_msg_len
= _set_max_msg_len
,
444 .get_max_msg_len
= _get_max_msg_len
,
445 .change_state
= _change_state
,
446 .get_recommendation
= _get_recommendation
,
447 .set_recommendation
= _set_recommendation
,
448 .get_reason_string
= _get_reason_string
,
449 .get_remediation_instructions
= _get_remediation_instructions
,
452 .get_handshake_state
= _get_handshake_state
,
453 .set_handshake_state
= _set_handshake_state
,
455 .add_file_meas_request
= _add_file_meas_request
,
456 .check_off_file_meas_request
= _check_off_file_meas_request
,
457 .get_file_meas_request_count
= _get_file_meas_request_count
,
458 .create_component
= _create_component
,
459 .get_component
= _get_component
,
460 .finalize_components
= _finalize_components
,
461 .components_finalized
= _components_finalized
,
462 .get_measurement_error
= _get_measurement_error
,
463 .set_measurement_error
= _set_measurement_error
,
465 .connection_id
= connection_id
,
466 .state
= TNC_CONNECTION_STATE_CREATE
,
467 .handshake_state
= IMV_ATTESTATION_STATE_INIT
,
468 .rec
= TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION
,
469 .eval
= TNC_IMV_EVALUATION_RESULT_DONT_KNOW
,
470 .file_meas_requests
= linked_list_create(),
471 .components
= linked_list_create(),
472 .pts
= pts_create(FALSE
),
475 return &this->public.interface
;