2 * Copyright (C) 2011 Sansar Choinyambuu
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"
18 #include <utils/lexparser.h>
19 #include <utils/linked_list.h>
22 typedef struct private_imv_attestation_state_t private_imv_attestation_state_t
;
23 typedef struct file_meas_request_t file_meas_request_t
;
26 * PTS File/Directory Measurement request entry
28 struct file_meas_request_t
{
35 * Private data of an imv_attestation_state_t object.
37 struct private_imv_attestation_state_t
{
40 * Public members of imv_attestation_state_t
42 imv_attestation_state_t
public;
47 TNC_ConnectionID connection_id
;
50 * TNCCS connection state
52 TNC_ConnectionState state
;
55 * IMV Attestation handshake state
57 imv_attestation_handshake_state_t handshake_state
;
60 * IMV action recommendation
62 TNC_IMV_Action_Recommendation rec
;
65 * IMV evaluation result
67 TNC_IMV_Evaluation_Result eval
;
70 * File Measurement Request counter
72 u_int16_t file_meas_request_counter
;
75 * List of PTS File/Directory Measurement requests
77 linked_list_t
*file_meas_requests
;
80 * List of Functional Components
82 linked_list_t
*components
;
92 bool measurement_error
;
96 typedef struct entry_t entry_t
;
99 * Define an internal reason string entry
107 * Table of multi-lingual reason string entries
109 static entry_t reasons
[] = {
110 { "en", "IMV Attestation: Incorrect/pending file measurement/component"
111 " evidence or invalid TPM Quote signature received" },
112 { "mn", "IMV Attestation: Буруу/хүлээгдэж байгаа файл/компонент хэмжилт "
113 "эсвэл буруу TPM Quote гарын үсэг" },
114 { "de", "IMV Attestation: Falsche/Fehlende Dateimessung/Komponenten Beweis "
115 "oder ungültige TPM Quote Unterschrift ist erhalten" },
118 METHOD(imv_state_t
, get_connection_id
, TNC_ConnectionID
,
119 private_imv_attestation_state_t
*this)
121 return this->connection_id
;
124 METHOD(imv_state_t
, change_state
, void,
125 private_imv_attestation_state_t
*this, TNC_ConnectionState new_state
)
127 this->state
= new_state
;
130 METHOD(imv_state_t
, get_recommendation
, void,
131 private_imv_attestation_state_t
*this, TNC_IMV_Action_Recommendation
*rec
,
132 TNC_IMV_Evaluation_Result
*eval
)
138 METHOD(imv_state_t
, set_recommendation
, void,
139 private_imv_attestation_state_t
*this, TNC_IMV_Action_Recommendation rec
,
140 TNC_IMV_Evaluation_Result eval
)
146 METHOD(imv_state_t
, get_reason_string
, bool,
147 private_imv_attestation_state_t
*this, chunk_t preferred_language
,
148 chunk_t
*reason_string
, chunk_t
*reason_language
)
150 chunk_t pref_lang
, lang
;
154 while (eat_whitespace(&preferred_language
))
156 if (!extract_token(&pref_lang
, ',', &preferred_language
))
158 /* last entry in a comma-separated list or single entry */
159 pref_lang
= preferred_language
;
162 /* eat trailing whitespace */
163 pos
= pref_lang
.ptr
+ pref_lang
.len
- 1;
164 while (pref_lang
.len
&& *pos
-- == ' ')
169 for (i
= 0 ; i
< countof(reasons
); i
++)
171 lang
= chunk_create(reasons
[i
].lang
, strlen(reasons
[i
].lang
));
172 if (chunk_equals(lang
, pref_lang
))
174 *reason_language
= lang
;
175 *reason_string
= chunk_create(reasons
[i
].string
,
176 strlen(reasons
[i
].string
));
182 /* no preferred language match found - use the default language */
183 *reason_string
= chunk_create(reasons
[0].string
,
184 strlen(reasons
[0].string
));
185 *reason_language
= chunk_create(reasons
[0].lang
,
186 strlen(reasons
[0].lang
));
190 METHOD(imv_state_t
, destroy
, void,
191 private_imv_attestation_state_t
*this)
193 this->file_meas_requests
->destroy_function(this->file_meas_requests
, free
);
194 this->components
->destroy_offset(this->components
,
195 offsetof(pts_component_t
, destroy
));
196 this->pts
->destroy(this->pts
);
200 METHOD(imv_attestation_state_t
, get_handshake_state
,
201 imv_attestation_handshake_state_t
, private_imv_attestation_state_t
*this)
203 return this->handshake_state
;
206 METHOD(imv_attestation_state_t
, set_handshake_state
, void,
207 private_imv_attestation_state_t
*this,
208 imv_attestation_handshake_state_t new_state
)
210 this->handshake_state
= new_state
;
213 METHOD(imv_attestation_state_t
, get_pts
, pts_t
*,
214 private_imv_attestation_state_t
*this)
219 METHOD(imv_attestation_state_t
, add_file_meas_request
, u_int16_t
,
220 private_imv_attestation_state_t
*this, int file_id
, bool is_dir
)
222 file_meas_request_t
*request
;
224 request
= malloc_thing(file_meas_request_t
);
225 request
->id
= ++this->file_meas_request_counter
;
226 request
->file_id
= file_id
;
227 request
->is_dir
= is_dir
;
228 this->file_meas_requests
->insert_last(this->file_meas_requests
, request
);
230 return this->file_meas_request_counter
;
233 METHOD(imv_attestation_state_t
, check_off_file_meas_request
, bool,
234 private_imv_attestation_state_t
*this, u_int16_t id
, int *file_id
,
237 enumerator_t
*enumerator
;
238 file_meas_request_t
*request
;
241 enumerator
= this->file_meas_requests
->create_enumerator(this->file_meas_requests
);
242 while (enumerator
->enumerate(enumerator
, &request
))
244 if (request
->id
== id
)
247 *file_id
= request
->file_id
;
248 *is_dir
= request
->is_dir
;
249 this->file_meas_requests
->remove_at(this->file_meas_requests
, enumerator
);
254 enumerator
->destroy(enumerator
);
258 METHOD(imv_attestation_state_t
, get_file_meas_request_count
, int,
259 private_imv_attestation_state_t
*this)
261 return this->file_meas_requests
->get_count(this->file_meas_requests
);
264 METHOD(imv_attestation_state_t
, add_component
, void,
265 private_imv_attestation_state_t
*this, pts_component_t
*entry
)
267 this->components
->insert_last(this->components
, entry
);
270 METHOD(imv_attestation_state_t
, check_off_component
, pts_component_t
*,
271 private_imv_attestation_state_t
*this, pts_comp_func_name_t
*name
)
273 enumerator_t
*enumerator
;
274 pts_component_t
*entry
, *found
= NULL
;
276 enumerator
= this->components
->create_enumerator(this->components
);
277 while (enumerator
->enumerate(enumerator
, &entry
))
279 if (name
->equals(name
, entry
->get_comp_func_name(entry
)))
282 this->components
->remove_at(this->components
, enumerator
);
286 enumerator
->destroy(enumerator
);
290 METHOD(imv_attestation_state_t
, get_component_count
, int,
291 private_imv_attestation_state_t
*this)
293 return this->components
->get_count(this->components
);
296 METHOD(imv_attestation_state_t
, get_measurement_error
, bool,
297 private_imv_attestation_state_t
*this)
299 return this->measurement_error
;
302 METHOD(imv_attestation_state_t
, set_measurement_error
, void,
303 private_imv_attestation_state_t
*this)
305 this->measurement_error
= TRUE
;
309 * Described in header.
311 imv_state_t
*imv_attestation_state_create(TNC_ConnectionID connection_id
)
313 private_imv_attestation_state_t
*this;
319 .get_connection_id
= _get_connection_id
,
320 .change_state
= _change_state
,
321 .get_recommendation
= _get_recommendation
,
322 .set_recommendation
= _set_recommendation
,
323 .get_reason_string
= _get_reason_string
,
326 .get_handshake_state
= _get_handshake_state
,
327 .set_handshake_state
= _set_handshake_state
,
329 .add_file_meas_request
= _add_file_meas_request
,
330 .check_off_file_meas_request
= _check_off_file_meas_request
,
331 .get_file_meas_request_count
= _get_file_meas_request_count
,
332 .add_component
= _add_component
,
333 .check_off_component
= _check_off_component
,
334 .get_component_count
= _get_component_count
,
335 .get_measurement_error
= _get_measurement_error
,
336 .set_measurement_error
= _set_measurement_error
,
338 .connection_id
= connection_id
,
339 .state
= TNC_CONNECTION_STATE_CREATE
,
340 .handshake_state
= IMV_ATTESTATION_STATE_INIT
,
341 .rec
= TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION
,
342 .eval
= TNC_IMV_EVALUATION_RESULT_DONT_KNOW
,
343 .file_meas_requests
= linked_list_create(),
344 .components
= linked_list_create(),
345 .pts
= pts_create(FALSE
),
348 platform_info
= lib
->settings
->get_str(lib
->settings
,
349 "libimcv.plugins.imv-attestation.platform_info", NULL
);
352 this->pts
->set_platform_info(this->pts
, platform_info
);
355 return &this->public.interface
;