implement a preferred language enumerator
[strongswan.git] / src / libpts / plugins / imv_attestation / imv_attestation_state.c
1 /*
2 * Copyright (C) 2011-2012 Sansar Choinyambuu, Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
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>.
9 *
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
13 * for more details.
14 */
15
16 #include "imv_attestation_state.h"
17
18 #include <libpts.h>
19
20 #include <utils/lexparser.h>
21 #include <collections/linked_list.h>
22 #include <utils/debug.h>
23
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;
27
28 /**
29 * Private data of an imv_attestation_state_t object.
30 */
31 struct private_imv_attestation_state_t {
32
33 /**
34 * Public members of imv_attestation_state_t
35 */
36 imv_attestation_state_t public;
37
38 /**
39 * TNCCS connection ID
40 */
41 TNC_ConnectionID connection_id;
42
43 /**
44 * TNCCS connection state
45 */
46 TNC_ConnectionState state;
47
48 /**
49 * Does the TNCCS connection support long message types?
50 */
51 bool has_long;
52
53 /**
54 * Does the TNCCS connection support exclusive delivery?
55 */
56 bool has_excl;
57
58 /**
59 * Maximum PA-TNC message size for this TNCCS connection
60 */
61 u_int32_t max_msg_len;
62
63 /**
64 * IMV Attestation handshake state
65 */
66 imv_attestation_handshake_state_t handshake_state;
67
68 /**
69 * IMV action recommendation
70 */
71 TNC_IMV_Action_Recommendation rec;
72
73 /**
74 * IMV evaluation result
75 */
76 TNC_IMV_Evaluation_Result eval;
77
78 /**
79 * File Measurement Request counter
80 */
81 u_int16_t file_meas_request_counter;
82
83 /**
84 * List of PTS File/Directory Measurement requests
85 */
86 linked_list_t *file_meas_requests;
87
88 /**
89 * List of Functional Components
90 */
91 linked_list_t *components;
92
93 /**
94 * PTS object
95 */
96 pts_t *pts;
97
98 /**
99 * Measurement error
100 */
101 bool measurement_error;
102
103 };
104
105 /**
106 * PTS File/Directory Measurement request entry
107 */
108 struct file_meas_request_t {
109 u_int16_t id;
110 int file_id;
111 bool is_dir;
112 };
113
114 /**
115 * PTS Functional Component entry
116 */
117 struct func_comp_t {
118 pts_component_t *comp;
119 u_int8_t qualifier;
120 };
121
122 /**
123 * Frees a func_comp_t object
124 */
125 static void free_func_comp(func_comp_t *this)
126 {
127 this->comp->destroy(this->comp);
128 free(this);
129 }
130
131 typedef struct entry_t entry_t;
132
133 /**
134 * Define an internal reason string entry
135 */
136 struct entry_t {
137 char *lang;
138 char *string;
139 };
140
141 /**
142 * Table of multi-lingual reason string entries
143 */
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" },
151 };
152
153 METHOD(imv_state_t, get_connection_id, TNC_ConnectionID,
154 private_imv_attestation_state_t *this)
155 {
156 return this->connection_id;
157 }
158
159 METHOD(imv_state_t, has_long, bool,
160 private_imv_attestation_state_t *this)
161 {
162 return this->has_long;
163 }
164
165 METHOD(imv_state_t, has_excl, bool,
166 private_imv_attestation_state_t *this)
167 {
168 return this->has_excl;
169 }
170
171 METHOD(imv_state_t, set_flags, void,
172 private_imv_attestation_state_t *this, bool has_long, bool has_excl)
173 {
174 this->has_long = has_long;
175 this->has_excl = has_excl;
176 }
177
178 METHOD(imv_state_t, set_max_msg_len, void,
179 private_imv_attestation_state_t *this, u_int32_t max_msg_len)
180 {
181 this->max_msg_len = max_msg_len;
182 }
183
184 METHOD(imv_state_t, get_max_msg_len, u_int32_t,
185 private_imv_attestation_state_t *this)
186 {
187 return this->max_msg_len;
188 }
189
190 METHOD(imv_state_t, change_state, void,
191 private_imv_attestation_state_t *this, TNC_ConnectionState new_state)
192 {
193 this->state = new_state;
194 }
195
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)
199 {
200 *rec = this->rec;
201 *eval = this->eval;
202 }
203
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)
207 {
208 this->rec = rec;
209 this->eval = eval;
210 }
211
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)
215 {
216 bool match = FALSE;
217 char *lang;
218 int i;
219
220 /* set the default language */
221 *reason_language = reasons[0].lang;
222 *reason_string = reasons[0].string;
223
224 while (language_enumerator->enumerate(language_enumerator, &lang))
225 {
226 for (i = 0 ; i < countof(reasons); i++)
227 {
228 if (streq(lang, reasons[i].lang))
229 {
230 match = TRUE;
231 *reason_language = reasons[i].lang;
232 *reason_string = reasons[i].string;
233 break;
234 }
235 }
236 if (match)
237 {
238 break;
239 }
240 }
241
242 return TRUE;
243 }
244
245 METHOD(imv_state_t, destroy, void,
246 private_imv_attestation_state_t *this)
247 {
248 this->file_meas_requests->destroy_function(this->file_meas_requests, free);
249 this->components->destroy_function(this->components, (void *)free_func_comp);
250 this->pts->destroy(this->pts);
251 free(this);
252 }
253
254 METHOD(imv_attestation_state_t, get_handshake_state,
255 imv_attestation_handshake_state_t, private_imv_attestation_state_t *this)
256 {
257 return this->handshake_state;
258 }
259
260 METHOD(imv_attestation_state_t, set_handshake_state, void,
261 private_imv_attestation_state_t *this,
262 imv_attestation_handshake_state_t new_state)
263 {
264 this->handshake_state = new_state;
265 }
266
267 METHOD(imv_attestation_state_t, get_pts, pts_t*,
268 private_imv_attestation_state_t *this)
269 {
270 return this->pts;
271 }
272
273 METHOD(imv_attestation_state_t, add_file_meas_request, u_int16_t,
274 private_imv_attestation_state_t *this, int file_id, bool is_dir)
275 {
276 file_meas_request_t *request;
277
278 request = malloc_thing(file_meas_request_t);
279 request->id = ++this->file_meas_request_counter;
280 request->file_id = file_id;
281 request->is_dir = is_dir;
282 this->file_meas_requests->insert_last(this->file_meas_requests, request);
283
284 return this->file_meas_request_counter;
285 }
286
287 METHOD(imv_attestation_state_t, check_off_file_meas_request, bool,
288 private_imv_attestation_state_t *this, u_int16_t id, int *file_id,
289 bool* is_dir)
290 {
291 enumerator_t *enumerator;
292 file_meas_request_t *request;
293 bool found = FALSE;
294
295 enumerator = this->file_meas_requests->create_enumerator(this->file_meas_requests);
296 while (enumerator->enumerate(enumerator, &request))
297 {
298 if (request->id == id)
299 {
300 found = TRUE;
301 *file_id = request->file_id;
302 *is_dir = request->is_dir;
303 this->file_meas_requests->remove_at(this->file_meas_requests, enumerator);
304 free(request);
305 break;
306 }
307 }
308 enumerator->destroy(enumerator);
309 return found;
310 }
311
312 METHOD(imv_attestation_state_t, get_file_meas_request_count, int,
313 private_imv_attestation_state_t *this)
314 {
315 return this->file_meas_requests->get_count(this->file_meas_requests);
316 }
317
318 METHOD(imv_attestation_state_t, create_component, pts_component_t*,
319 private_imv_attestation_state_t *this, pts_comp_func_name_t *name,
320 u_int32_t depth, pts_database_t *pts_db)
321 {
322 enumerator_t *enumerator;
323 func_comp_t *entry, *new_entry;
324 pts_component_t *component;
325 bool found = FALSE;
326
327 enumerator = this->components->create_enumerator(this->components);
328 while (enumerator->enumerate(enumerator, &entry))
329 {
330 if (name->equals(name, entry->comp->get_comp_func_name(entry->comp)))
331 {
332 found = TRUE;
333 break;
334 }
335 }
336 enumerator->destroy(enumerator);
337
338 if (found)
339 {
340 if (name->get_qualifier(name) == entry->qualifier)
341 {
342 /* duplicate entry */
343 return NULL;
344 }
345 new_entry = malloc_thing(func_comp_t);
346 new_entry->qualifier = name->get_qualifier(name);
347 new_entry->comp = entry->comp->get_ref(entry->comp);
348 this->components->insert_last(this->components, new_entry);
349 return entry->comp;
350 }
351 else
352 {
353 component = pts_components->create(pts_components, name, depth, pts_db);
354 if (!component)
355 {
356 /* unsupported component */
357 return NULL;
358 }
359 new_entry = malloc_thing(func_comp_t);
360 new_entry->qualifier = name->get_qualifier(name);
361 new_entry->comp = component;
362 this->components->insert_last(this->components, new_entry);
363 return component;
364 }
365 }
366
367 METHOD(imv_attestation_state_t, get_component, pts_component_t*,
368 private_imv_attestation_state_t *this, pts_comp_func_name_t *name)
369 {
370 enumerator_t *enumerator;
371 func_comp_t *entry;
372 pts_component_t *found = NULL;
373
374 enumerator = this->components->create_enumerator(this->components);
375 while (enumerator->enumerate(enumerator, &entry))
376 {
377 if (name->equals(name, entry->comp->get_comp_func_name(entry->comp)) &&
378 name->get_qualifier(name) == entry->qualifier)
379 {
380 found = entry->comp;
381 break;
382 }
383 }
384 enumerator->destroy(enumerator);
385 return found;
386 }
387
388 METHOD(imv_attestation_state_t, get_measurement_error, bool,
389 private_imv_attestation_state_t *this)
390 {
391 return this->measurement_error;
392 }
393
394 METHOD(imv_attestation_state_t, set_measurement_error, void,
395 private_imv_attestation_state_t *this)
396 {
397 this->measurement_error = TRUE;
398 }
399
400 METHOD(imv_attestation_state_t, finalize_components, void,
401 private_imv_attestation_state_t *this)
402 {
403 func_comp_t *entry;
404
405 while (this->components->remove_last(this->components,
406 (void**)&entry) == SUCCESS)
407 {
408 if (!entry->comp->finalize(entry->comp, entry->qualifier))
409 {
410 _set_measurement_error(this);
411 }
412 free_func_comp(entry);
413 }
414 }
415
416 METHOD(imv_attestation_state_t, components_finalized, bool,
417 private_imv_attestation_state_t *this)
418 {
419 return this->components->get_count(this->components) == 0;
420 }
421
422 /**
423 * Described in header.
424 */
425 imv_state_t *imv_attestation_state_create(TNC_ConnectionID connection_id)
426 {
427 private_imv_attestation_state_t *this;
428
429 INIT(this,
430 .public = {
431 .interface = {
432 .get_connection_id = _get_connection_id,
433 .has_long = _has_long,
434 .has_excl = _has_excl,
435 .set_flags = _set_flags,
436 .set_max_msg_len = _set_max_msg_len,
437 .get_max_msg_len = _get_max_msg_len,
438 .change_state = _change_state,
439 .get_recommendation = _get_recommendation,
440 .set_recommendation = _set_recommendation,
441 .get_reason_string = _get_reason_string,
442 .destroy = _destroy,
443 },
444 .get_handshake_state = _get_handshake_state,
445 .set_handshake_state = _set_handshake_state,
446 .get_pts = _get_pts,
447 .add_file_meas_request = _add_file_meas_request,
448 .check_off_file_meas_request = _check_off_file_meas_request,
449 .get_file_meas_request_count = _get_file_meas_request_count,
450 .create_component = _create_component,
451 .get_component = _get_component,
452 .finalize_components = _finalize_components,
453 .components_finalized = _components_finalized,
454 .get_measurement_error = _get_measurement_error,
455 .set_measurement_error = _set_measurement_error,
456 },
457 .connection_id = connection_id,
458 .state = TNC_CONNECTION_STATE_CREATE,
459 .handshake_state = IMV_ATTESTATION_STATE_INIT,
460 .rec = TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
461 .eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW,
462 .file_meas_requests = linked_list_create(),
463 .components = linked_list_create(),
464 .pts = pts_create(FALSE),
465 );
466
467 return &this->public.interface;
468 }