2 * Copyright (C) 2011 Sansar Choinyambuu
3 * HSR Hochschule fuer Technik Rapperswil
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>.
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
16 #include "imv_attestation_state.h"
18 #include <utils/lexparser.h>
19 #include <utils/linked_list.h>
22 typedef struct private_imv_attestation_state_t private_imv_attestation_state_t
;
23 typedef struct request_t request_t
;
26 * PTS File/Directory Measurement request entry
35 * Private data of an imv_attestation_state_t object.
37 struct private_imv_attestation_state_t
{
40 * Public members of imv_attestation_state_t
42 imv_attestation_state_t
public;
47 TNC_ConnectionID connection_id
;
50 * TNCCS connection state
52 TNC_ConnectionState state
;
55 * IMV Attestation handshake state
57 imv_attestation_handshake_state_t handshake_state
;
60 * IMV action recommendation
62 TNC_IMV_Action_Recommendation rec
;
65 * IMV evaluation result
67 TNC_IMV_Evaluation_Result eval
;
72 u_int16_t request_counter
;
75 * List of PTS File/Directory Measurement requests
77 linked_list_t
*requests
;
86 typedef struct entry_t entry_t
;
89 * Define an internal reason string entry
97 * Table of multi-lingual reason string entries
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" },
105 METHOD(imv_state_t
, get_connection_id
, TNC_ConnectionID
,
106 private_imv_attestation_state_t
*this)
108 return this->connection_id
;
111 METHOD(imv_state_t
, change_state
, void,
112 private_imv_attestation_state_t
*this, TNC_ConnectionState new_state
)
114 this->state
= new_state
;
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
)
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
)
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
)
137 chunk_t pref_lang
, lang
;
141 while (eat_whitespace(&preferred_language
))
143 if (!extract_token(&pref_lang
, ',', &preferred_language
))
145 /* last entry in a comma-separated list or single entry */
146 pref_lang
= preferred_language
;
149 /* eat trailing whitespace */
150 pos
= pref_lang
.ptr
+ pref_lang
.len
- 1;
151 while (pref_lang
.len
&& *pos
-- == ' ')
156 for (i
= 0 ; i
< countof(reasons
); i
++)
158 lang
= chunk_create(reasons
[i
].lang
, strlen(reasons
[i
].lang
));
159 if (chunk_equals(lang
, pref_lang
))
161 *reason_language
= lang
;
162 *reason_string
= chunk_create(reasons
[i
].string
,
163 strlen(reasons
[i
].string
));
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
));
177 METHOD(imv_state_t
, destroy
, void,
178 private_imv_attestation_state_t
*this)
180 this->requests
->destroy_function(this->requests
, free
);
181 this->pts
->destroy(this->pts
);
185 METHOD(imv_attestation_state_t
, get_handshake_state
, imv_attestation_handshake_state_t
,
186 private_imv_attestation_state_t
*this)
188 return this->handshake_state
;
191 METHOD(imv_attestation_state_t
, set_handshake_state
, void,
192 private_imv_attestation_state_t
*this, imv_attestation_handshake_state_t new_state
)
194 this->handshake_state
= new_state
;
197 METHOD(imv_attestation_state_t
, get_pts
, pts_t
*,
198 private_imv_attestation_state_t
*this)
203 METHOD(imv_attestation_state_t
, add_request
, u_int16_t
,
204 private_imv_attestation_state_t
*this, int file_id
, bool is_dir
)
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
);
214 return this->request_counter
;
217 METHOD(imv_attestation_state_t
, check_off_request
, bool,
218 private_imv_attestation_state_t
*this, u_int16_t id
, int *file_id
,
221 enumerator_t
*enumerator
;
225 enumerator
= this->requests
->create_enumerator(this->requests
);
226 while (enumerator
->enumerate(enumerator
, &request
))
228 if (request
->id
== id
)
231 *file_id
= request
->file_id
;
232 *is_dir
= request
->is_dir
;
233 this->requests
->remove_at(this->requests
, enumerator
);
238 enumerator
->destroy(enumerator
);
242 METHOD(imv_attestation_state_t
, get_request_count
, int,
243 private_imv_attestation_state_t
*this)
245 return this->requests
->get_count(this->requests
);
249 * Described in header.
251 imv_state_t
*imv_attestation_state_create(TNC_ConnectionID connection_id
)
253 private_imv_attestation_state_t
*this;
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
,
266 .get_handshake_state
= _get_handshake_state
,
267 .set_handshake_state
= _set_handshake_state
,
269 .add_request
= _add_request
,
270 .check_off_request
= _check_off_request
,
271 .get_request_count
= _get_request_count
,
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
),
282 platform_info
= lib
->settings
->get_str(lib
->settings
,
283 "libimcv.plugins.imv-attestation.platform_info", NULL
);
286 this->pts
->set_platform_info(this->pts
, platform_info
);
289 return &this->public.interface
;