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 <utils/linked_list.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, chunk_t preferred_language
,
214 chunk_t
*reason_string
, chunk_t
*reason_language
)
216 chunk_t pref_lang
, lang
;
220 while (eat_whitespace(&preferred_language
))
222 if (!extract_token(&pref_lang
, ',', &preferred_language
))
224 /* last entry in a comma-separated list or single entry */
225 pref_lang
= preferred_language
;
228 /* eat trailing whitespace */
229 pos
= pref_lang
.ptr
+ pref_lang
.len
- 1;
230 while (pref_lang
.len
&& *pos
-- == ' ')
235 for (i
= 0 ; i
< countof(reasons
); i
++)
237 lang
= chunk_create(reasons
[i
].lang
, strlen(reasons
[i
].lang
));
238 if (chunk_equals(lang
, pref_lang
))
240 *reason_language
= lang
;
241 *reason_string
= chunk_create(reasons
[i
].string
,
242 strlen(reasons
[i
].string
));
248 /* no preferred language match found - use the default language */
249 *reason_string
= chunk_create(reasons
[0].string
,
250 strlen(reasons
[0].string
));
251 *reason_language
= chunk_create(reasons
[0].lang
,
252 strlen(reasons
[0].lang
));
256 METHOD(imv_state_t
, destroy
, void,
257 private_imv_attestation_state_t
*this)
259 this->file_meas_requests
->destroy_function(this->file_meas_requests
, free
);
260 this->components
->destroy_function(this->components
, (void *)free_func_comp
);
261 this->pts
->destroy(this->pts
);
265 METHOD(imv_attestation_state_t
, get_handshake_state
,
266 imv_attestation_handshake_state_t
, private_imv_attestation_state_t
*this)
268 return this->handshake_state
;
271 METHOD(imv_attestation_state_t
, set_handshake_state
, void,
272 private_imv_attestation_state_t
*this,
273 imv_attestation_handshake_state_t new_state
)
275 this->handshake_state
= new_state
;
278 METHOD(imv_attestation_state_t
, get_pts
, pts_t
*,
279 private_imv_attestation_state_t
*this)
284 METHOD(imv_attestation_state_t
, add_file_meas_request
, u_int16_t
,
285 private_imv_attestation_state_t
*this, int file_id
, bool is_dir
)
287 file_meas_request_t
*request
;
289 request
= malloc_thing(file_meas_request_t
);
290 request
->id
= ++this->file_meas_request_counter
;
291 request
->file_id
= file_id
;
292 request
->is_dir
= is_dir
;
293 this->file_meas_requests
->insert_last(this->file_meas_requests
, request
);
295 return this->file_meas_request_counter
;
298 METHOD(imv_attestation_state_t
, check_off_file_meas_request
, bool,
299 private_imv_attestation_state_t
*this, u_int16_t id
, int *file_id
,
302 enumerator_t
*enumerator
;
303 file_meas_request_t
*request
;
306 enumerator
= this->file_meas_requests
->create_enumerator(this->file_meas_requests
);
307 while (enumerator
->enumerate(enumerator
, &request
))
309 if (request
->id
== id
)
312 *file_id
= request
->file_id
;
313 *is_dir
= request
->is_dir
;
314 this->file_meas_requests
->remove_at(this->file_meas_requests
, enumerator
);
319 enumerator
->destroy(enumerator
);
323 METHOD(imv_attestation_state_t
, get_file_meas_request_count
, int,
324 private_imv_attestation_state_t
*this)
326 return this->file_meas_requests
->get_count(this->file_meas_requests
);
329 METHOD(imv_attestation_state_t
, create_component
, pts_component_t
*,
330 private_imv_attestation_state_t
*this, pts_comp_func_name_t
*name
,
331 u_int32_t depth
, pts_database_t
*pts_db
)
333 enumerator_t
*enumerator
;
334 func_comp_t
*entry
, *new_entry
;
335 pts_component_t
*component
;
338 enumerator
= this->components
->create_enumerator(this->components
);
339 while (enumerator
->enumerate(enumerator
, &entry
))
341 if (name
->equals(name
, entry
->comp
->get_comp_func_name(entry
->comp
)))
347 enumerator
->destroy(enumerator
);
351 if (name
->get_qualifier(name
) == entry
->qualifier
)
353 /* duplicate entry */
356 new_entry
= malloc_thing(func_comp_t
);
357 new_entry
->qualifier
= name
->get_qualifier(name
);
358 new_entry
->comp
= entry
->comp
->get_ref(entry
->comp
);
359 this->components
->insert_last(this->components
, new_entry
);
364 component
= pts_components
->create(pts_components
, name
, depth
, pts_db
);
367 /* unsupported component */
370 new_entry
= malloc_thing(func_comp_t
);
371 new_entry
->qualifier
= name
->get_qualifier(name
);
372 new_entry
->comp
= component
;
373 this->components
->insert_last(this->components
, new_entry
);
378 METHOD(imv_attestation_state_t
, get_component
, pts_component_t
*,
379 private_imv_attestation_state_t
*this, pts_comp_func_name_t
*name
)
381 enumerator_t
*enumerator
;
383 pts_component_t
*found
= NULL
;
385 enumerator
= this->components
->create_enumerator(this->components
);
386 while (enumerator
->enumerate(enumerator
, &entry
))
388 if (name
->equals(name
, entry
->comp
->get_comp_func_name(entry
->comp
)) &&
389 name
->get_qualifier(name
) == entry
->qualifier
)
395 enumerator
->destroy(enumerator
);
399 METHOD(imv_attestation_state_t
, get_measurement_error
, bool,
400 private_imv_attestation_state_t
*this)
402 return this->measurement_error
;
405 METHOD(imv_attestation_state_t
, set_measurement_error
, void,
406 private_imv_attestation_state_t
*this)
408 this->measurement_error
= TRUE
;
411 METHOD(imv_attestation_state_t
, finalize_components
, void,
412 private_imv_attestation_state_t
*this)
416 while (this->components
->remove_last(this->components
,
417 (void**)&entry
) == SUCCESS
)
419 if (!entry
->comp
->finalize(entry
->comp
, entry
->qualifier
))
421 _set_measurement_error(this);
423 free_func_comp(entry
);
427 METHOD(imv_attestation_state_t
, components_finalized
, bool,
428 private_imv_attestation_state_t
*this)
430 return this->components
->get_count(this->components
) == 0;
434 * Described in header.
436 imv_state_t
*imv_attestation_state_create(TNC_ConnectionID connection_id
)
438 private_imv_attestation_state_t
*this;
444 .get_connection_id
= _get_connection_id
,
445 .has_long
= _has_long
,
446 .has_excl
= _has_excl
,
447 .set_flags
= _set_flags
,
448 .set_max_msg_len
= _set_max_msg_len
,
449 .get_max_msg_len
= _get_max_msg_len
,
450 .change_state
= _change_state
,
451 .get_recommendation
= _get_recommendation
,
452 .set_recommendation
= _set_recommendation
,
453 .get_reason_string
= _get_reason_string
,
456 .get_handshake_state
= _get_handshake_state
,
457 .set_handshake_state
= _set_handshake_state
,
459 .add_file_meas_request
= _add_file_meas_request
,
460 .check_off_file_meas_request
= _check_off_file_meas_request
,
461 .get_file_meas_request_count
= _get_file_meas_request_count
,
462 .create_component
= _create_component
,
463 .get_component
= _get_component
,
464 .finalize_components
= _finalize_components
,
465 .components_finalized
= _components_finalized
,
466 .get_measurement_error
= _get_measurement_error
,
467 .set_measurement_error
= _set_measurement_error
,
469 .connection_id
= connection_id
,
470 .state
= TNC_CONNECTION_STATE_CREATE
,
471 .handshake_state
= IMV_ATTESTATION_STATE_INIT
,
472 .rec
= TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION
,
473 .eval
= TNC_IMV_EVALUATION_RESULT_DONT_KNOW
,
474 .file_meas_requests
= linked_list_create(),
475 .components
= linked_list_create(),
476 .pts
= pts_create(FALSE
),
479 platform_info
= lib
->settings
->get_str(lib
->settings
,
480 "libimcv.plugins.imv-attestation.platform_info", NULL
);
483 this->pts
->set_platform_info(this->pts
, platform_info
);
486 return &this->public.interface
;