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