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 <utils/lexparser.h>
24 #include <collections/linked_list.h>
25 #include <utils/debug.h>
27 typedef struct private_imv_attestation_state_t private_imv_attestation_state_t
;
28 typedef struct file_meas_request_t file_meas_request_t
;
29 typedef struct func_comp_t func_comp_t
;
32 * Private data of an imv_attestation_state_t object.
34 struct private_imv_attestation_state_t
{
37 * Public members of imv_attestation_state_t
39 imv_attestation_state_t
public;
44 TNC_ConnectionID connection_id
;
47 * TNCCS connection state
49 TNC_ConnectionState state
;
52 * Does the TNCCS connection support long message types?
57 * Does the TNCCS connection support exclusive delivery?
62 * Maximum PA-TNC message size for this TNCCS connection
64 u_int32_t max_msg_len
;
67 * IMV Attestation handshake state
69 imv_attestation_handshake_state_t handshake_state
;
72 * IMV action recommendation
74 TNC_IMV_Action_Recommendation rec
;
77 * IMV evaluation result
79 TNC_IMV_Evaluation_Result eval
;
82 * File Measurement Request counter
84 u_int16_t file_meas_request_counter
;
87 * List of PTS File/Directory Measurement requests
89 linked_list_t
*file_meas_requests
;
92 * List of Functional Components
94 linked_list_t
*components
;
104 bool measurement_error
;
109 imv_reason_string_t
*reason_string
;
114 * PTS File/Directory Measurement request entry
116 struct file_meas_request_t
{
123 * PTS Functional Component entry
126 pts_component_t
*comp
;
131 * Frees a func_comp_t object
133 static void free_func_comp(func_comp_t
*this)
135 this->comp
->destroy(this->comp
);
140 * Supported languages
142 static char* languages
[] = { "en", "mn", "de" };
145 * Table of reason strings
147 static imv_lang_string_t reasons
[] = {
148 { "en", "IMV Attestation: Incorrect/pending file measurement/component"
149 " evidence or invalid TPM Quote signature received" },
150 { "mn", "IMV Attestation: Буруу/хүлээгдэж байгаа файл/компонент хэмжилт "
151 "эсвэл буруу TPM Quote гарын үсэг" },
152 { "de", "IMV Attestation: Falsche/Fehlende Dateimessung/Komponenten Beweis "
153 "oder ungültige TPM Quote Unterschrift ist erhalten" },
157 METHOD(imv_state_t
, get_connection_id
, TNC_ConnectionID
,
158 private_imv_attestation_state_t
*this)
160 return this->connection_id
;
163 METHOD(imv_state_t
, has_long
, bool,
164 private_imv_attestation_state_t
*this)
166 return this->has_long
;
169 METHOD(imv_state_t
, has_excl
, bool,
170 private_imv_attestation_state_t
*this)
172 return this->has_excl
;
175 METHOD(imv_state_t
, set_flags
, void,
176 private_imv_attestation_state_t
*this, bool has_long
, bool has_excl
)
178 this->has_long
= has_long
;
179 this->has_excl
= has_excl
;
182 METHOD(imv_state_t
, set_max_msg_len
, void,
183 private_imv_attestation_state_t
*this, u_int32_t max_msg_len
)
185 this->max_msg_len
= max_msg_len
;
188 METHOD(imv_state_t
, get_max_msg_len
, u_int32_t
,
189 private_imv_attestation_state_t
*this)
191 return this->max_msg_len
;
194 METHOD(imv_state_t
, change_state
, void,
195 private_imv_attestation_state_t
*this, TNC_ConnectionState new_state
)
197 this->state
= new_state
;
200 METHOD(imv_state_t
, get_recommendation
, void,
201 private_imv_attestation_state_t
*this, TNC_IMV_Action_Recommendation
*rec
,
202 TNC_IMV_Evaluation_Result
*eval
)
208 METHOD(imv_state_t
, set_recommendation
, void,
209 private_imv_attestation_state_t
*this, TNC_IMV_Action_Recommendation rec
,
210 TNC_IMV_Evaluation_Result eval
)
216 METHOD(imv_state_t
, get_reason_string
, bool,
217 private_imv_attestation_state_t
*this, enumerator_t
*language_enumerator
,
218 chunk_t
*reason_string
, char **reason_language
)
220 *reason_language
= imv_lang_string_select_lang(language_enumerator
,
221 languages
, countof(languages
));
223 /* Instantiate a TNC Reason String object */
224 DESTROY_IF(this->reason_string
);
225 this->reason_string
= imv_reason_string_create(*reason_language
);
226 this->reason_string
->add_reason(this->reason_string
, reasons
);
227 *reason_string
= this->reason_string
->get_encoding(this->reason_string
);
232 METHOD(imv_state_t
, get_remediation_instructions
, bool,
233 private_imv_attestation_state_t
*this, enumerator_t
*language_enumerator
,
234 chunk_t
*string
, char **lang_code
, char **uri
)
239 METHOD(imv_state_t
, destroy
, void,
240 private_imv_attestation_state_t
*this)
242 DESTROY_IF(this->reason_string
);
243 this->file_meas_requests
->destroy_function(this->file_meas_requests
, free
);
244 this->components
->destroy_function(this->components
, (void *)free_func_comp
);
245 this->pts
->destroy(this->pts
);
249 METHOD(imv_attestation_state_t
, get_handshake_state
,
250 imv_attestation_handshake_state_t
, private_imv_attestation_state_t
*this)
252 return this->handshake_state
;
255 METHOD(imv_attestation_state_t
, set_handshake_state
, void,
256 private_imv_attestation_state_t
*this,
257 imv_attestation_handshake_state_t new_state
)
259 this->handshake_state
= new_state
;
262 METHOD(imv_attestation_state_t
, get_pts
, pts_t
*,
263 private_imv_attestation_state_t
*this)
268 METHOD(imv_attestation_state_t
, add_file_meas_request
, u_int16_t
,
269 private_imv_attestation_state_t
*this, int file_id
, bool is_dir
)
271 file_meas_request_t
*request
;
273 request
= malloc_thing(file_meas_request_t
);
274 request
->id
= ++this->file_meas_request_counter
;
275 request
->file_id
= file_id
;
276 request
->is_dir
= is_dir
;
277 this->file_meas_requests
->insert_last(this->file_meas_requests
, request
);
279 return this->file_meas_request_counter
;
282 METHOD(imv_attestation_state_t
, check_off_file_meas_request
, bool,
283 private_imv_attestation_state_t
*this, u_int16_t id
, int *file_id
,
286 enumerator_t
*enumerator
;
287 file_meas_request_t
*request
;
290 enumerator
= this->file_meas_requests
->create_enumerator(this->file_meas_requests
);
291 while (enumerator
->enumerate(enumerator
, &request
))
293 if (request
->id
== id
)
296 *file_id
= request
->file_id
;
297 *is_dir
= request
->is_dir
;
298 this->file_meas_requests
->remove_at(this->file_meas_requests
, enumerator
);
303 enumerator
->destroy(enumerator
);
307 METHOD(imv_attestation_state_t
, get_file_meas_request_count
, int,
308 private_imv_attestation_state_t
*this)
310 return this->file_meas_requests
->get_count(this->file_meas_requests
);
313 METHOD(imv_attestation_state_t
, create_component
, pts_component_t
*,
314 private_imv_attestation_state_t
*this, pts_comp_func_name_t
*name
,
315 u_int32_t depth
, pts_database_t
*pts_db
)
317 enumerator_t
*enumerator
;
318 func_comp_t
*entry
, *new_entry
;
319 pts_component_t
*component
;
322 enumerator
= this->components
->create_enumerator(this->components
);
323 while (enumerator
->enumerate(enumerator
, &entry
))
325 if (name
->equals(name
, entry
->comp
->get_comp_func_name(entry
->comp
)))
331 enumerator
->destroy(enumerator
);
335 if (name
->get_qualifier(name
) == entry
->qualifier
)
337 /* duplicate entry */
340 new_entry
= malloc_thing(func_comp_t
);
341 new_entry
->qualifier
= name
->get_qualifier(name
);
342 new_entry
->comp
= entry
->comp
->get_ref(entry
->comp
);
343 this->components
->insert_last(this->components
, new_entry
);
348 component
= pts_components
->create(pts_components
, name
, depth
, pts_db
);
351 /* unsupported component */
354 new_entry
= malloc_thing(func_comp_t
);
355 new_entry
->qualifier
= name
->get_qualifier(name
);
356 new_entry
->comp
= component
;
357 this->components
->insert_last(this->components
, new_entry
);
362 METHOD(imv_attestation_state_t
, get_component
, pts_component_t
*,
363 private_imv_attestation_state_t
*this, pts_comp_func_name_t
*name
)
365 enumerator_t
*enumerator
;
367 pts_component_t
*found
= NULL
;
369 enumerator
= this->components
->create_enumerator(this->components
);
370 while (enumerator
->enumerate(enumerator
, &entry
))
372 if (name
->equals(name
, entry
->comp
->get_comp_func_name(entry
->comp
)) &&
373 name
->get_qualifier(name
) == entry
->qualifier
)
379 enumerator
->destroy(enumerator
);
383 METHOD(imv_attestation_state_t
, get_measurement_error
, bool,
384 private_imv_attestation_state_t
*this)
386 return this->measurement_error
;
389 METHOD(imv_attestation_state_t
, set_measurement_error
, void,
390 private_imv_attestation_state_t
*this)
392 this->measurement_error
= TRUE
;
395 METHOD(imv_attestation_state_t
, finalize_components
, void,
396 private_imv_attestation_state_t
*this)
400 while (this->components
->remove_last(this->components
,
401 (void**)&entry
) == SUCCESS
)
403 if (!entry
->comp
->finalize(entry
->comp
, entry
->qualifier
))
405 _set_measurement_error(this);
407 free_func_comp(entry
);
411 METHOD(imv_attestation_state_t
, components_finalized
, bool,
412 private_imv_attestation_state_t
*this)
414 return this->components
->get_count(this->components
) == 0;
418 * Described in header.
420 imv_state_t
*imv_attestation_state_create(TNC_ConnectionID connection_id
)
422 private_imv_attestation_state_t
*this;
427 .get_connection_id
= _get_connection_id
,
428 .has_long
= _has_long
,
429 .has_excl
= _has_excl
,
430 .set_flags
= _set_flags
,
431 .set_max_msg_len
= _set_max_msg_len
,
432 .get_max_msg_len
= _get_max_msg_len
,
433 .change_state
= _change_state
,
434 .get_recommendation
= _get_recommendation
,
435 .set_recommendation
= _set_recommendation
,
436 .get_reason_string
= _get_reason_string
,
437 .get_remediation_instructions
= _get_remediation_instructions
,
440 .get_handshake_state
= _get_handshake_state
,
441 .set_handshake_state
= _set_handshake_state
,
443 .add_file_meas_request
= _add_file_meas_request
,
444 .check_off_file_meas_request
= _check_off_file_meas_request
,
445 .get_file_meas_request_count
= _get_file_meas_request_count
,
446 .create_component
= _create_component
,
447 .get_component
= _get_component
,
448 .finalize_components
= _finalize_components
,
449 .components_finalized
= _components_finalized
,
450 .get_measurement_error
= _get_measurement_error
,
451 .set_measurement_error
= _set_measurement_error
,
453 .connection_id
= connection_id
,
454 .state
= TNC_CONNECTION_STATE_CREATE
,
455 .handshake_state
= IMV_ATTESTATION_STATE_INIT
,
456 .rec
= TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION
,
457 .eval
= TNC_IMV_EVALUATION_RESULT_DONT_KNOW
,
458 .file_meas_requests
= linked_list_create(),
459 .components
= linked_list_create(),
460 .pts
= pts_create(FALSE
),
463 return &this->public.interface
;