automatically registering component measurements
[strongswan.git] / src / libpts / plugins / imv_attestation / imv_attestation_state.c
1 /*
2 * Copyright (C) 2011 Sansar Choinyambuu
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 <utils/lexparser.h>
19 #include <utils/linked_list.h>
20 #include <debug.h>
21
22 typedef struct private_imv_attestation_state_t private_imv_attestation_state_t;
23 typedef struct file_meas_request_t file_meas_request_t;
24
25 /**
26 * PTS File/Directory Measurement request entry
27 */
28 struct file_meas_request_t {
29 u_int16_t id;
30 int file_id;
31 bool is_dir;
32 };
33
34 /**
35 * Private data of an imv_attestation_state_t object.
36 */
37 struct private_imv_attestation_state_t {
38
39 /**
40 * Public members of imv_attestation_state_t
41 */
42 imv_attestation_state_t public;
43
44 /**
45 * TNCCS connection ID
46 */
47 TNC_ConnectionID connection_id;
48
49 /**
50 * TNCCS connection state
51 */
52 TNC_ConnectionState state;
53
54 /**
55 * IMV Attestation handshake state
56 */
57 imv_attestation_handshake_state_t handshake_state;
58
59 /**
60 * IMV action recommendation
61 */
62 TNC_IMV_Action_Recommendation rec;
63
64 /**
65 * IMV evaluation result
66 */
67 TNC_IMV_Evaluation_Result eval;
68
69 /**
70 * File Measurement Request counter
71 */
72 u_int16_t file_meas_request_counter;
73
74 /**
75 * List of PTS File/Directory Measurement requests
76 */
77 linked_list_t *file_meas_requests;
78
79 /**
80 * List of Functional Components
81 */
82 linked_list_t *components;
83
84 /**
85 * PTS object
86 */
87 pts_t *pts;
88
89 /**
90 * Measurement error
91 */
92 bool measurement_error;
93
94 };
95
96 typedef struct entry_t entry_t;
97
98 /**
99 * Define an internal reason string entry
100 */
101 struct entry_t {
102 char *lang;
103 char *string;
104 };
105
106 /**
107 * Table of multi-lingual reason string entries
108 */
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" },
116 };
117
118 METHOD(imv_state_t, get_connection_id, TNC_ConnectionID,
119 private_imv_attestation_state_t *this)
120 {
121 return this->connection_id;
122 }
123
124 METHOD(imv_state_t, change_state, void,
125 private_imv_attestation_state_t *this, TNC_ConnectionState new_state)
126 {
127 this->state = new_state;
128 }
129
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)
133 {
134 *rec = this->rec;
135 *eval = this->eval;
136 }
137
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)
141 {
142 this->rec = rec;
143 this->eval = eval;
144 }
145
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)
149 {
150 chunk_t pref_lang, lang;
151 u_char *pos;
152 int i;
153
154 while (eat_whitespace(&preferred_language))
155 {
156 if (!extract_token(&pref_lang, ',', &preferred_language))
157 {
158 /* last entry in a comma-separated list or single entry */
159 pref_lang = preferred_language;
160 }
161
162 /* eat trailing whitespace */
163 pos = pref_lang.ptr + pref_lang.len - 1;
164 while (pref_lang.len && *pos-- == ' ')
165 {
166 pref_lang.len--;
167 }
168
169 for (i = 0 ; i < countof(reasons); i++)
170 {
171 lang = chunk_create(reasons[i].lang, strlen(reasons[i].lang));
172 if (chunk_equals(lang, pref_lang))
173 {
174 *reason_language = lang;
175 *reason_string = chunk_create(reasons[i].string,
176 strlen(reasons[i].string));
177 return TRUE;
178 }
179 }
180 }
181
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));
187 return TRUE;
188 }
189
190 METHOD(imv_state_t, destroy, void,
191 private_imv_attestation_state_t *this)
192 {
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);
197 free(this);
198 }
199
200 METHOD(imv_attestation_state_t, get_handshake_state,
201 imv_attestation_handshake_state_t, private_imv_attestation_state_t *this)
202 {
203 return this->handshake_state;
204 }
205
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)
209 {
210 this->handshake_state = new_state;
211 }
212
213 METHOD(imv_attestation_state_t, get_pts, pts_t*,
214 private_imv_attestation_state_t *this)
215 {
216 return this->pts;
217 }
218
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)
221 {
222 file_meas_request_t *request;
223
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);
229
230 return this->file_meas_request_counter;
231 }
232
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,
235 bool* is_dir)
236 {
237 enumerator_t *enumerator;
238 file_meas_request_t *request;
239 bool found = FALSE;
240
241 enumerator = this->file_meas_requests->create_enumerator(this->file_meas_requests);
242 while (enumerator->enumerate(enumerator, &request))
243 {
244 if (request->id == id)
245 {
246 found = TRUE;
247 *file_id = request->file_id;
248 *is_dir = request->is_dir;
249 this->file_meas_requests->remove_at(this->file_meas_requests, enumerator);
250 free(request);
251 break;
252 }
253 }
254 enumerator->destroy(enumerator);
255 return found;
256 }
257
258 METHOD(imv_attestation_state_t, get_file_meas_request_count, int,
259 private_imv_attestation_state_t *this)
260 {
261 return this->file_meas_requests->get_count(this->file_meas_requests);
262 }
263
264 METHOD(imv_attestation_state_t, add_component, void,
265 private_imv_attestation_state_t *this, pts_component_t *entry)
266 {
267 this->components->insert_last(this->components, entry);
268 }
269
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)
272 {
273 enumerator_t *enumerator;
274 pts_component_t *entry, *found = NULL;
275
276 enumerator = this->components->create_enumerator(this->components);
277 while (enumerator->enumerate(enumerator, &entry))
278 {
279 if (name->equals(name, entry->get_comp_func_name(entry)))
280 {
281 found = entry;
282 this->components->remove_at(this->components, enumerator);
283 break;
284 }
285 }
286 enumerator->destroy(enumerator);
287 return found;
288 }
289
290 METHOD(imv_attestation_state_t, check_off_registrations, void,
291 private_imv_attestation_state_t *this)
292 {
293 enumerator_t *enumerator;
294 pts_component_t *entry;
295
296 enumerator = this->components->create_enumerator(this->components);
297 while (enumerator->enumerate(enumerator, &entry))
298 {
299 if (entry->check_off_registrations(entry))
300 {
301 this->components->remove_at(this->components, enumerator);
302 entry->destroy(entry);
303 }
304 }
305 enumerator->destroy(enumerator);
306 }
307
308 METHOD(imv_attestation_state_t, get_component_count, int,
309 private_imv_attestation_state_t *this)
310 {
311 return this->components->get_count(this->components);
312 }
313
314 METHOD(imv_attestation_state_t, get_measurement_error, bool,
315 private_imv_attestation_state_t *this)
316 {
317 return this->measurement_error;
318 }
319
320 METHOD(imv_attestation_state_t, set_measurement_error, void,
321 private_imv_attestation_state_t *this)
322 {
323 this->measurement_error = TRUE;
324 }
325
326 /**
327 * Described in header.
328 */
329 imv_state_t *imv_attestation_state_create(TNC_ConnectionID connection_id)
330 {
331 private_imv_attestation_state_t *this;
332 char *platform_info;
333
334 INIT(this,
335 .public = {
336 .interface = {
337 .get_connection_id = _get_connection_id,
338 .change_state = _change_state,
339 .get_recommendation = _get_recommendation,
340 .set_recommendation = _set_recommendation,
341 .get_reason_string = _get_reason_string,
342 .destroy = _destroy,
343 },
344 .get_handshake_state = _get_handshake_state,
345 .set_handshake_state = _set_handshake_state,
346 .get_pts = _get_pts,
347 .add_file_meas_request = _add_file_meas_request,
348 .check_off_file_meas_request = _check_off_file_meas_request,
349 .get_file_meas_request_count = _get_file_meas_request_count,
350 .add_component = _add_component,
351 .check_off_component = _check_off_component,
352 .check_off_registrations = _check_off_registrations,
353 .get_component_count = _get_component_count,
354 .get_measurement_error = _get_measurement_error,
355 .set_measurement_error = _set_measurement_error,
356 },
357 .connection_id = connection_id,
358 .state = TNC_CONNECTION_STATE_CREATE,
359 .handshake_state = IMV_ATTESTATION_STATE_INIT,
360 .rec = TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
361 .eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW,
362 .file_meas_requests = linked_list_create(),
363 .components = linked_list_create(),
364 .pts = pts_create(FALSE),
365 );
366
367 platform_info = lib->settings->get_str(lib->settings,
368 "libimcv.plugins.imv-attestation.platform_info", NULL);
369 if (platform_info)
370 {
371 this->pts->set_platform_info(this->pts, platform_info);
372 }
373
374 return &this->public.interface;
375 }