Moved debug.[ch] to utils folder
[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, chunk_t preferred_language,
214 chunk_t *reason_string, chunk_t *reason_language)
215 {
216 chunk_t pref_lang, lang;
217 u_char *pos;
218 int i;
219
220 while (eat_whitespace(&preferred_language))
221 {
222 if (!extract_token(&pref_lang, ',', &preferred_language))
223 {
224 /* last entry in a comma-separated list or single entry */
225 pref_lang = preferred_language;
226 }
227
228 /* eat trailing whitespace */
229 pos = pref_lang.ptr + pref_lang.len - 1;
230 while (pref_lang.len && *pos-- == ' ')
231 {
232 pref_lang.len--;
233 }
234
235 for (i = 0 ; i < countof(reasons); i++)
236 {
237 lang = chunk_create(reasons[i].lang, strlen(reasons[i].lang));
238 if (chunk_equals(lang, pref_lang))
239 {
240 *reason_language = lang;
241 *reason_string = chunk_create(reasons[i].string,
242 strlen(reasons[i].string));
243 return TRUE;
244 }
245 }
246 }
247
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));
253 return TRUE;
254 }
255
256 METHOD(imv_state_t, destroy, void,
257 private_imv_attestation_state_t *this)
258 {
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);
262 free(this);
263 }
264
265 METHOD(imv_attestation_state_t, get_handshake_state,
266 imv_attestation_handshake_state_t, private_imv_attestation_state_t *this)
267 {
268 return this->handshake_state;
269 }
270
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)
274 {
275 this->handshake_state = new_state;
276 }
277
278 METHOD(imv_attestation_state_t, get_pts, pts_t*,
279 private_imv_attestation_state_t *this)
280 {
281 return this->pts;
282 }
283
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)
286 {
287 file_meas_request_t *request;
288
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);
294
295 return this->file_meas_request_counter;
296 }
297
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,
300 bool* is_dir)
301 {
302 enumerator_t *enumerator;
303 file_meas_request_t *request;
304 bool found = FALSE;
305
306 enumerator = this->file_meas_requests->create_enumerator(this->file_meas_requests);
307 while (enumerator->enumerate(enumerator, &request))
308 {
309 if (request->id == id)
310 {
311 found = TRUE;
312 *file_id = request->file_id;
313 *is_dir = request->is_dir;
314 this->file_meas_requests->remove_at(this->file_meas_requests, enumerator);
315 free(request);
316 break;
317 }
318 }
319 enumerator->destroy(enumerator);
320 return found;
321 }
322
323 METHOD(imv_attestation_state_t, get_file_meas_request_count, int,
324 private_imv_attestation_state_t *this)
325 {
326 return this->file_meas_requests->get_count(this->file_meas_requests);
327 }
328
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)
332 {
333 enumerator_t *enumerator;
334 func_comp_t *entry, *new_entry;
335 pts_component_t *component;
336 bool found = FALSE;
337
338 enumerator = this->components->create_enumerator(this->components);
339 while (enumerator->enumerate(enumerator, &entry))
340 {
341 if (name->equals(name, entry->comp->get_comp_func_name(entry->comp)))
342 {
343 found = TRUE;
344 break;
345 }
346 }
347 enumerator->destroy(enumerator);
348
349 if (found)
350 {
351 if (name->get_qualifier(name) == entry->qualifier)
352 {
353 /* duplicate entry */
354 return NULL;
355 }
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);
360 return entry->comp;
361 }
362 else
363 {
364 component = pts_components->create(pts_components, name, depth, pts_db);
365 if (!component)
366 {
367 /* unsupported component */
368 return NULL;
369 }
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);
374 return component;
375 }
376 }
377
378 METHOD(imv_attestation_state_t, get_component, pts_component_t*,
379 private_imv_attestation_state_t *this, pts_comp_func_name_t *name)
380 {
381 enumerator_t *enumerator;
382 func_comp_t *entry;
383 pts_component_t *found = NULL;
384
385 enumerator = this->components->create_enumerator(this->components);
386 while (enumerator->enumerate(enumerator, &entry))
387 {
388 if (name->equals(name, entry->comp->get_comp_func_name(entry->comp)) &&
389 name->get_qualifier(name) == entry->qualifier)
390 {
391 found = entry->comp;
392 break;
393 }
394 }
395 enumerator->destroy(enumerator);
396 return found;
397 }
398
399 METHOD(imv_attestation_state_t, get_measurement_error, bool,
400 private_imv_attestation_state_t *this)
401 {
402 return this->measurement_error;
403 }
404
405 METHOD(imv_attestation_state_t, set_measurement_error, void,
406 private_imv_attestation_state_t *this)
407 {
408 this->measurement_error = TRUE;
409 }
410
411 METHOD(imv_attestation_state_t, finalize_components, void,
412 private_imv_attestation_state_t *this)
413 {
414 func_comp_t *entry;
415
416 while (this->components->remove_last(this->components,
417 (void**)&entry) == SUCCESS)
418 {
419 if (!entry->comp->finalize(entry->comp, entry->qualifier))
420 {
421 _set_measurement_error(this);
422 }
423 free_func_comp(entry);
424 }
425 }
426
427 METHOD(imv_attestation_state_t, components_finalized, bool,
428 private_imv_attestation_state_t *this)
429 {
430 return this->components->get_count(this->components) == 0;
431 }
432
433 /**
434 * Described in header.
435 */
436 imv_state_t *imv_attestation_state_create(TNC_ConnectionID connection_id)
437 {
438 private_imv_attestation_state_t *this;
439
440 INIT(this,
441 .public = {
442 .interface = {
443 .get_connection_id = _get_connection_id,
444 .has_long = _has_long,
445 .has_excl = _has_excl,
446 .set_flags = _set_flags,
447 .set_max_msg_len = _set_max_msg_len,
448 .get_max_msg_len = _get_max_msg_len,
449 .change_state = _change_state,
450 .get_recommendation = _get_recommendation,
451 .set_recommendation = _set_recommendation,
452 .get_reason_string = _get_reason_string,
453 .destroy = _destroy,
454 },
455 .get_handshake_state = _get_handshake_state,
456 .set_handshake_state = _set_handshake_state,
457 .get_pts = _get_pts,
458 .add_file_meas_request = _add_file_meas_request,
459 .check_off_file_meas_request = _check_off_file_meas_request,
460 .get_file_meas_request_count = _get_file_meas_request_count,
461 .create_component = _create_component,
462 .get_component = _get_component,
463 .finalize_components = _finalize_components,
464 .components_finalized = _components_finalized,
465 .get_measurement_error = _get_measurement_error,
466 .set_measurement_error = _set_measurement_error,
467 },
468 .connection_id = connection_id,
469 .state = TNC_CONNECTION_STATE_CREATE,
470 .handshake_state = IMV_ATTESTATION_STATE_INIT,
471 .rec = TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
472 .eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW,
473 .file_meas_requests = linked_list_create(),
474 .components = linked_list_create(),
475 .pts = pts_create(FALSE),
476 );
477
478 return &this->public.interface;
479 }