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