moved attribute processing to imv_attestation_process
[strongswan.git] / src / libimcv / 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 request_t request_t;
24
25 /**
26 * PTS File/Directory Measurement request entry
27 */
28 struct 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 * Request counter
71 */
72 u_int16_t request_counter;
73
74 /**
75 * List of PTS File/Directory Measurement requests
76 */
77 linked_list_t *requests;
78
79 /**
80 * PTS object
81 */
82 pts_t *pts;
83
84 /**
85 * File Measurement error
86 */
87 bool measurement_error;
88
89 };
90
91 typedef struct entry_t entry_t;
92
93 /**
94 * Define an internal reason string entry
95 */
96 struct entry_t {
97 char *lang;
98 char *string;
99 };
100
101 /**
102 * Table of multi-lingual reason string entries
103 */
104 static entry_t reasons[] = {
105 { "en", "IMC Attestation Measurement/s of requested file didn't match" },
106 { "mn", "IMC Attestation Шалгахаар тохируулсан файлуудын хэмжилтүүд таарсангүй" },
107 { "de", "IMC Attestation Messung/en von angefordeten Datein stimmt nicht überein" },
108 };
109
110 METHOD(imv_state_t, get_connection_id, TNC_ConnectionID,
111 private_imv_attestation_state_t *this)
112 {
113 return this->connection_id;
114 }
115
116 METHOD(imv_state_t, change_state, void,
117 private_imv_attestation_state_t *this, TNC_ConnectionState new_state)
118 {
119 this->state = new_state;
120 }
121
122 METHOD(imv_state_t, get_recommendation, void,
123 private_imv_attestation_state_t *this, TNC_IMV_Action_Recommendation *rec,
124 TNC_IMV_Evaluation_Result *eval)
125 {
126 *rec = this->rec;
127 *eval = this->eval;
128 }
129
130 METHOD(imv_state_t, set_recommendation, void,
131 private_imv_attestation_state_t *this, TNC_IMV_Action_Recommendation rec,
132 TNC_IMV_Evaluation_Result eval)
133 {
134 this->rec = rec;
135 this->eval = eval;
136 }
137
138 METHOD(imv_state_t, get_reason_string, bool,
139 private_imv_attestation_state_t *this, chunk_t preferred_language,
140 chunk_t *reason_string, chunk_t *reason_language)
141 {
142 chunk_t pref_lang, lang;
143 u_char *pos;
144 int i;
145
146 while (eat_whitespace(&preferred_language))
147 {
148 if (!extract_token(&pref_lang, ',', &preferred_language))
149 {
150 /* last entry in a comma-separated list or single entry */
151 pref_lang = preferred_language;
152 }
153
154 /* eat trailing whitespace */
155 pos = pref_lang.ptr + pref_lang.len - 1;
156 while (pref_lang.len && *pos-- == ' ')
157 {
158 pref_lang.len--;
159 }
160
161 for (i = 0 ; i < countof(reasons); i++)
162 {
163 lang = chunk_create(reasons[i].lang, strlen(reasons[i].lang));
164 if (chunk_equals(lang, pref_lang))
165 {
166 *reason_language = lang;
167 *reason_string = chunk_create(reasons[i].string,
168 strlen(reasons[i].string));
169 return TRUE;
170 }
171 }
172 }
173
174 /* no preferred language match found - use the default language */
175 *reason_string = chunk_create(reasons[0].string,
176 strlen(reasons[0].string));
177 *reason_language = chunk_create(reasons[0].lang,
178 strlen(reasons[0].lang));
179 return TRUE;
180 }
181
182 METHOD(imv_state_t, destroy, void,
183 private_imv_attestation_state_t *this)
184 {
185 this->requests->destroy_function(this->requests, free);
186 this->pts->destroy(this->pts);
187 free(this);
188 }
189
190 METHOD(imv_attestation_state_t, get_handshake_state, imv_attestation_handshake_state_t,
191 private_imv_attestation_state_t *this)
192 {
193 return this->handshake_state;
194 }
195
196 METHOD(imv_attestation_state_t, set_handshake_state, void,
197 private_imv_attestation_state_t *this, imv_attestation_handshake_state_t new_state)
198 {
199 this->handshake_state = new_state;
200 }
201
202 METHOD(imv_attestation_state_t, get_pts, pts_t*,
203 private_imv_attestation_state_t *this)
204 {
205 return this->pts;
206 }
207
208 METHOD(imv_attestation_state_t, add_request, u_int16_t,
209 private_imv_attestation_state_t *this, int file_id, bool is_dir)
210 {
211 request_t *request;
212
213 request = malloc_thing(request_t);
214 request->id = ++this->request_counter;
215 request->file_id = file_id;
216 request->is_dir = is_dir;
217 this->requests->insert_last(this->requests, request);
218
219 return this->request_counter;
220 }
221
222 METHOD(imv_attestation_state_t, check_off_request, bool,
223 private_imv_attestation_state_t *this, u_int16_t id, int *file_id,
224 bool* is_dir)
225 {
226 enumerator_t *enumerator;
227 request_t *request;
228 bool found = FALSE;
229
230 enumerator = this->requests->create_enumerator(this->requests);
231 while (enumerator->enumerate(enumerator, &request))
232 {
233 if (request->id == id)
234 {
235 found = TRUE;
236 *file_id = request->file_id;
237 *is_dir = request->is_dir;
238 this->requests->remove_at(this->requests, enumerator);
239 free(request);
240 break;
241 }
242 }
243 enumerator->destroy(enumerator);
244 return found;
245 }
246
247 METHOD(imv_attestation_state_t, get_request_count, int,
248 private_imv_attestation_state_t *this)
249 {
250 return this->requests->get_count(this->requests);
251 }
252
253 METHOD(imv_attestation_state_t, get_measurement_error, bool,
254 private_imv_attestation_state_t *this)
255 {
256 return this->measurement_error;
257 }
258
259 METHOD(imv_attestation_state_t, set_measurement_error, void,
260 private_imv_attestation_state_t *this)
261 {
262 this->measurement_error = TRUE;
263 }
264
265 /**
266 * Described in header.
267 */
268 imv_state_t *imv_attestation_state_create(TNC_ConnectionID connection_id)
269 {
270 private_imv_attestation_state_t *this;
271 char *platform_info;
272
273 INIT(this,
274 .public = {
275 .interface = {
276 .get_connection_id = _get_connection_id,
277 .change_state = _change_state,
278 .get_recommendation = _get_recommendation,
279 .set_recommendation = _set_recommendation,
280 .get_reason_string = _get_reason_string,
281 .destroy = _destroy,
282 },
283 .get_handshake_state = _get_handshake_state,
284 .set_handshake_state = _set_handshake_state,
285 .get_pts = _get_pts,
286 .add_request = _add_request,
287 .check_off_request = _check_off_request,
288 .get_request_count = _get_request_count,
289 .get_measurement_error = _get_measurement_error,
290 .set_measurement_error = _set_measurement_error,
291 },
292 .connection_id = connection_id,
293 .state = TNC_CONNECTION_STATE_CREATE,
294 .handshake_state = IMV_ATTESTATION_STATE_INIT,
295 .rec = TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
296 .eval = TNC_IMV_EVALUATION_RESULT_DONT_KNOW,
297 .requests = linked_list_create(),
298 .pts = pts_create(FALSE),
299 );
300
301 platform_info = lib->settings->get_str(lib->settings,
302 "libimcv.plugins.imv-attestation.platform_info", NULL);
303 if (platform_info)
304 {
305 this->pts->set_platform_info(this->pts, platform_info);
306 }
307
308 return &this->public.interface;
309 }