b9adbc0a167a5be6d5141ab91d94614ab939dee5
[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, get_remediation_instructions, bool,
246 private_imv_attestation_state_t *this, enumerator_t *language_enumerator,
247 char **string, char **lang_code)
248 {
249 return FALSE;
250 }
251
252 METHOD(imv_state_t, destroy, void,
253 private_imv_attestation_state_t *this)
254 {
255 this->file_meas_requests->destroy_function(this->file_meas_requests, free);
256 this->components->destroy_function(this->components, (void *)free_func_comp);
257 this->pts->destroy(this->pts);
258 free(this);
259 }
260
261 METHOD(imv_attestation_state_t, get_handshake_state,
262 imv_attestation_handshake_state_t, private_imv_attestation_state_t *this)
263 {
264 return this->handshake_state;
265 }
266
267 METHOD(imv_attestation_state_t, set_handshake_state, void,
268 private_imv_attestation_state_t *this,
269 imv_attestation_handshake_state_t new_state)
270 {
271 this->handshake_state = new_state;
272 }
273
274 METHOD(imv_attestation_state_t, get_pts, pts_t*,
275 private_imv_attestation_state_t *this)
276 {
277 return this->pts;
278 }
279
280 METHOD(imv_attestation_state_t, add_file_meas_request, u_int16_t,
281 private_imv_attestation_state_t *this, int file_id, bool is_dir)
282 {
283 file_meas_request_t *request;
284
285 request = malloc_thing(file_meas_request_t);
286 request->id = ++this->file_meas_request_counter;
287 request->file_id = file_id;
288 request->is_dir = is_dir;
289 this->file_meas_requests->insert_last(this->file_meas_requests, request);
290
291 return this->file_meas_request_counter;
292 }
293
294 METHOD(imv_attestation_state_t, check_off_file_meas_request, bool,
295 private_imv_attestation_state_t *this, u_int16_t id, int *file_id,
296 bool* is_dir)
297 {
298 enumerator_t *enumerator;
299 file_meas_request_t *request;
300 bool found = FALSE;
301
302 enumerator = this->file_meas_requests->create_enumerator(this->file_meas_requests);
303 while (enumerator->enumerate(enumerator, &request))
304 {
305 if (request->id == id)
306 {
307 found = TRUE;
308 *file_id = request->file_id;
309 *is_dir = request->is_dir;
310 this->file_meas_requests->remove_at(this->file_meas_requests, enumerator);
311 free(request);
312 break;
313 }
314 }
315 enumerator->destroy(enumerator);
316 return found;
317 }
318
319 METHOD(imv_attestation_state_t, get_file_meas_request_count, int,
320 private_imv_attestation_state_t *this)
321 {
322 return this->file_meas_requests->get_count(this->file_meas_requests);
323 }
324
325 METHOD(imv_attestation_state_t, create_component, pts_component_t*,
326 private_imv_attestation_state_t *this, pts_comp_func_name_t *name,
327 u_int32_t depth, pts_database_t *pts_db)
328 {
329 enumerator_t *enumerator;
330 func_comp_t *entry, *new_entry;
331 pts_component_t *component;
332 bool found = FALSE;
333
334 enumerator = this->components->create_enumerator(this->components);
335 while (enumerator->enumerate(enumerator, &entry))
336 {
337 if (name->equals(name, entry->comp->get_comp_func_name(entry->comp)))
338 {
339 found = TRUE;
340 break;
341 }
342 }
343 enumerator->destroy(enumerator);
344
345 if (found)
346 {
347 if (name->get_qualifier(name) == entry->qualifier)
348 {
349 /* duplicate entry */
350 return NULL;
351 }
352 new_entry = malloc_thing(func_comp_t);
353 new_entry->qualifier = name->get_qualifier(name);
354 new_entry->comp = entry->comp->get_ref(entry->comp);
355 this->components->insert_last(this->components, new_entry);
356 return entry->comp;
357 }
358 else
359 {
360 component = pts_components->create(pts_components, name, depth, pts_db);
361 if (!component)
362 {
363 /* unsupported component */
364 return NULL;
365 }
366 new_entry = malloc_thing(func_comp_t);
367 new_entry->qualifier = name->get_qualifier(name);
368 new_entry->comp = component;
369 this->components->insert_last(this->components, new_entry);
370 return component;
371 }
372 }
373
374 METHOD(imv_attestation_state_t, get_component, pts_component_t*,
375 private_imv_attestation_state_t *this, pts_comp_func_name_t *name)
376 {
377 enumerator_t *enumerator;
378 func_comp_t *entry;
379 pts_component_t *found = NULL;
380
381 enumerator = this->components->create_enumerator(this->components);
382 while (enumerator->enumerate(enumerator, &entry))
383 {
384 if (name->equals(name, entry->comp->get_comp_func_name(entry->comp)) &&
385 name->get_qualifier(name) == entry->qualifier)
386 {
387 found = entry->comp;
388 break;
389 }
390 }
391 enumerator->destroy(enumerator);
392 return found;
393 }
394
395 METHOD(imv_attestation_state_t, get_measurement_error, bool,
396 private_imv_attestation_state_t *this)
397 {
398 return this->measurement_error;
399 }
400
401 METHOD(imv_attestation_state_t, set_measurement_error, void,
402 private_imv_attestation_state_t *this)
403 {
404 this->measurement_error = TRUE;
405 }
406
407 METHOD(imv_attestation_state_t, finalize_components, void,
408 private_imv_attestation_state_t *this)
409 {
410 func_comp_t *entry;
411
412 while (this->components->remove_last(this->components,
413 (void**)&entry) == SUCCESS)
414 {
415 if (!entry->comp->finalize(entry->comp, entry->qualifier))
416 {
417 _set_measurement_error(this);
418 }
419 free_func_comp(entry);
420 }
421 }
422
423 METHOD(imv_attestation_state_t, components_finalized, bool,
424 private_imv_attestation_state_t *this)
425 {
426 return this->components->get_count(this->components) == 0;
427 }
428
429 /**
430 * Described in header.
431 */
432 imv_state_t *imv_attestation_state_create(TNC_ConnectionID connection_id)
433 {
434 private_imv_attestation_state_t *this;
435
436 INIT(this,
437 .public = {
438 .interface = {
439 .get_connection_id = _get_connection_id,
440 .has_long = _has_long,
441 .has_excl = _has_excl,
442 .set_flags = _set_flags,
443 .set_max_msg_len = _set_max_msg_len,
444 .get_max_msg_len = _get_max_msg_len,
445 .change_state = _change_state,
446 .get_recommendation = _get_recommendation,
447 .set_recommendation = _set_recommendation,
448 .get_reason_string = _get_reason_string,
449 .get_remediation_instructions = _get_remediation_instructions,
450 .destroy = _destroy,
451 },
452 .get_handshake_state = _get_handshake_state,
453 .set_handshake_state = _set_handshake_state,
454 .get_pts = _get_pts,
455 .add_file_meas_request = _add_file_meas_request,
456 .check_off_file_meas_request = _check_off_file_meas_request,
457 .get_file_meas_request_count = _get_file_meas_request_count,
458 .create_component = _create_component,
459 .get_component = _get_component,
460 .finalize_components = _finalize_components,
461 .components_finalized = _components_finalized,
462 .get_measurement_error = _get_measurement_error,
463 .set_measurement_error = _set_measurement_error,
464 },
465 .connection_id = connection_id,
466 .state = TNC_CONNECTION_STATE_CREATE,
467 .handshake_state = IMV_ATTESTATION_STATE_INIT,
468 .rec = TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
469 .eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW,
470 .file_meas_requests = linked_list_create(),
471 .components = linked_list_create(),
472 .pts = pts_create(FALSE),
473 );
474
475 return &this->public.interface;
476 }