introduced sending of standard IETF Assessment Result PA-TNC attribute by IMVs
[strongswan.git] / src / libpts / plugins / imv_attestation / imv_attestation.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 #include "imv_attestation_process.h"
18 #include "imv_attestation_build.h"
19
20 #include <imv/imv_agent.h>
21 #include <pa_tnc/pa_tnc_msg.h>
22 #include <ietf/ietf_attr.h>
23 #include <ietf/ietf_attr_pa_tnc_error.h>
24 #include <ietf/ietf_attr_product_info.h>
25
26 #include <libpts.h>
27
28 #include <pts/pts.h>
29 #include <pts/pts_database.h>
30 #include <pts/pts_creds.h>
31
32 #include <tcg/tcg_attr.h>
33
34 #include <tncif_pa_subtypes.h>
35
36 #include <pen/pen.h>
37 #include <debug.h>
38 #include <credentials/credential_manager.h>
39 #include <utils/linked_list.h>
40
41 /* IMV definitions */
42
43 static const char imv_name[] = "Attestation";
44
45 #define IMV_VENDOR_ID PEN_TCG
46 #define IMV_SUBTYPE PA_SUBTYPE_TCG_PTS
47
48 static imv_agent_t *imv_attestation;
49
50 /**
51 * Supported PTS measurement algorithms
52 */
53 static pts_meas_algorithms_t supported_algorithms = PTS_MEAS_ALGO_NONE;
54
55 /**
56 * Supported PTS Diffie Hellman Groups
57 */
58 static pts_dh_group_t supported_dh_groups = PTS_DH_GROUP_NONE;
59
60 /**
61 * PTS file measurement database
62 */
63 static pts_database_t *pts_db;
64
65 /**
66 * PTS credentials
67 */
68 static pts_creds_t *pts_creds;
69
70 /**
71 * PTS credential manager
72 */
73 static credential_manager_t *pts_credmgr;
74
75 /**
76 * see section 3.8.1 of TCG TNC IF-IMV Specification 1.3
77 */
78 TNC_Result TNC_IMV_Initialize(TNC_IMVID imv_id,
79 TNC_Version min_version,
80 TNC_Version max_version,
81 TNC_Version *actual_version)
82 {
83 char *hash_alg, *dh_group, *uri, *cadir;
84
85 if (imv_attestation)
86 {
87 DBG1(DBG_IMV, "IMV \"%s\" has already been initialized", imv_name);
88 return TNC_RESULT_ALREADY_INITIALIZED;
89 }
90 if (!pts_meas_algo_probe(&supported_algorithms) ||
91 !pts_dh_group_probe(&supported_dh_groups))
92 {
93 return TNC_RESULT_FATAL;
94 }
95 imv_attestation = imv_agent_create(imv_name, IMV_VENDOR_ID, IMV_SUBTYPE,
96 imv_id, actual_version);
97 if (!imv_attestation)
98 {
99 return TNC_RESULT_FATAL;
100 }
101
102 libpts_init();
103
104 if (min_version > TNC_IFIMV_VERSION_1 || max_version < TNC_IFIMV_VERSION_1)
105 {
106 DBG1(DBG_IMV, "no common IF-IMV version");
107 return TNC_RESULT_NO_COMMON_VERSION;
108 }
109
110 hash_alg = lib->settings->get_str(lib->settings,
111 "libimcv.plugins.imv-attestation.hash_algorithm", "sha256");
112 dh_group = lib->settings->get_str(lib->settings,
113 "libimcv.plugins.imv-attestation.dh_group", "ecp256");
114
115 if (!pts_meas_algo_update(hash_alg, &supported_algorithms) ||
116 !pts_dh_group_update(dh_group, &supported_dh_groups))
117 {
118 return TNC_RESULT_FATAL;
119 }
120
121 /* create a PTS credential manager */
122 pts_credmgr = credential_manager_create();
123
124 /* create PTS credential set */
125 cadir = lib->settings->get_str(lib->settings,
126 "libimcv.plugins.imv-attestation.cadir", NULL);
127 pts_creds = pts_creds_create(cadir);
128 if (pts_creds)
129 {
130 pts_credmgr->add_set(pts_credmgr, pts_creds->get_set(pts_creds));
131 }
132
133 /* attach file measurement database */
134 uri = lib->settings->get_str(lib->settings,
135 "libimcv.plugins.imv-attestation.database", NULL);
136 pts_db = pts_database_create(uri);
137
138 return TNC_RESULT_SUCCESS;
139 }
140
141 /**
142 * see section 3.8.2 of TCG TNC IF-IMV Specification 1.3
143 */
144 TNC_Result TNC_IMV_NotifyConnectionChange(TNC_IMVID imv_id,
145 TNC_ConnectionID connection_id,
146 TNC_ConnectionState new_state)
147 {
148 imv_state_t *state;
149
150 if (!imv_attestation)
151 {
152 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
153 return TNC_RESULT_NOT_INITIALIZED;
154 }
155 switch (new_state)
156 {
157 case TNC_CONNECTION_STATE_CREATE:
158 state = imv_attestation_state_create(connection_id);
159 return imv_attestation->create_state(imv_attestation, state);
160 case TNC_CONNECTION_STATE_DELETE:
161 return imv_attestation->delete_state(imv_attestation, connection_id);
162 case TNC_CONNECTION_STATE_HANDSHAKE:
163 default:
164 return imv_attestation->change_state(imv_attestation, connection_id,
165 new_state, NULL);
166 }
167 }
168
169 static TNC_Result send_message(TNC_ConnectionID connection_id)
170 {
171 linked_list_t *attr_list;
172 imv_state_t *state;
173 imv_attestation_state_t *attestation_state;
174 TNC_Result result;
175
176 if (!imv_attestation->get_state(imv_attestation, connection_id, &state))
177 {
178 return TNC_RESULT_FATAL;
179 }
180 attestation_state = (imv_attestation_state_t*)state;
181 attr_list = linked_list_create();
182
183 if (imv_attestation_build(attr_list, attestation_state, supported_algorithms,
184 supported_dh_groups, pts_db))
185 {
186 if (attr_list->get_count(attr_list))
187 {
188 result = imv_attestation->send_message(imv_attestation,
189 connection_id, FALSE, 0, TNC_IMCID_ANY, attr_list);
190 }
191 else
192 {
193 result = TNC_RESULT_SUCCESS;
194 }
195 attr_list->destroy(attr_list);
196 }
197 else
198 {
199 attr_list->destroy_offset(attr_list, offsetof(pa_tnc_attr_t, destroy));
200 result = TNC_RESULT_FATAL;
201 }
202
203 return result;
204 }
205
206 static TNC_Result receive_message(TNC_IMVID imv_id,
207 TNC_ConnectionID connection_id,
208 TNC_UInt32 msg_flags,
209 chunk_t msg,
210 TNC_VendorID msg_vid,
211 TNC_MessageSubtype msg_subtype,
212 TNC_UInt32 src_imc_id,
213 TNC_UInt32 dst_imv_id)
214 {
215 pa_tnc_msg_t *pa_tnc_msg;
216 pa_tnc_attr_t *attr;
217 pen_type_t type;
218 linked_list_t *attr_list;
219 imv_state_t *state;
220 imv_attestation_state_t *attestation_state;
221 pts_t *pts;
222 enumerator_t *enumerator;
223 TNC_Result result;
224
225 if (!imv_attestation)
226 {
227 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
228 return TNC_RESULT_NOT_INITIALIZED;
229 }
230
231 /* get current IMV state */
232 if (!imv_attestation->get_state(imv_attestation, connection_id, &state))
233 {
234 return TNC_RESULT_FATAL;
235 }
236 attestation_state = (imv_attestation_state_t*)state;
237 pts = attestation_state->get_pts(attestation_state);
238
239 /* parse received PA-TNC message and automatically handle any errors */
240 result = imv_attestation->receive_message(imv_attestation, state, msg,
241 msg_vid, msg_subtype, src_imc_id, dst_imv_id, &pa_tnc_msg);
242
243 /* no parsed PA-TNC attributes available if an error occurred */
244 if (!pa_tnc_msg)
245 {
246 return result;
247 }
248
249 /* preprocess any IETF standard error attributes */
250 result = pa_tnc_msg->process_ietf_std_errors(pa_tnc_msg) ?
251 TNC_RESULT_FATAL : TNC_RESULT_SUCCESS;
252
253 attr_list = linked_list_create();
254
255 /* analyze PA-TNC attributes */
256 enumerator = pa_tnc_msg->create_attribute_enumerator(pa_tnc_msg);
257 while (enumerator->enumerate(enumerator, &attr))
258 {
259 type = attr->get_type(attr);
260
261 if (type.vendor_id == PEN_IETF)
262 {
263 if (type.type == IETF_ATTR_PA_TNC_ERROR)
264 {
265 ietf_attr_pa_tnc_error_t *error_attr;
266 pen_type_t error_code;
267 chunk_t msg_info;
268
269 error_attr = (ietf_attr_pa_tnc_error_t*)attr;
270 error_code = error_attr->get_error_code(error_attr);
271
272 if (error_code.vendor_id == PEN_TCG)
273 {
274 msg_info = error_attr->get_msg_info(error_attr);
275
276 DBG1(DBG_IMV, "received TCG-PTS error '%N'",
277 pts_error_code_names, error_code.type);
278 DBG1(DBG_IMV, "error information: %B", &msg_info);
279
280 result = TNC_RESULT_FATAL;
281 }
282 }
283 else if (type.type == IETF_ATTR_PRODUCT_INFORMATION)
284 {
285 ietf_attr_product_info_t *attr_cast;
286 char *platform_info;
287
288 attr_cast = (ietf_attr_product_info_t*)attr;
289 platform_info = attr_cast->get_info(attr_cast, NULL, NULL);
290 pts->set_platform_info(pts, platform_info);
291 }
292 }
293 else if (type.vendor_id == PEN_TCG)
294 {
295 if (!imv_attestation_process(attr, attr_list, attestation_state,
296 supported_algorithms,supported_dh_groups, pts_db, pts_credmgr))
297 {
298 result = TNC_RESULT_FATAL;
299 break;
300 }
301 }
302 }
303 enumerator->destroy(enumerator);
304 pa_tnc_msg->destroy(pa_tnc_msg);
305
306 if (result != TNC_RESULT_SUCCESS)
307 {
308 attr_list->destroy_offset(attr_list, offsetof(pa_tnc_attr_t, destroy));
309 state->set_recommendation(state,
310 TNC_IMV_ACTION_RECOMMENDATION_ISOLATE,
311 TNC_IMV_EVALUATION_RESULT_ERROR);
312 return imv_attestation->provide_recommendation(imv_attestation,
313 connection_id, src_imc_id);
314 }
315
316 if (attr_list->get_count(attr_list))
317 {
318 result = imv_attestation->send_message(imv_attestation, connection_id,
319 FALSE, 0, TNC_IMCID_ANY, attr_list);
320 attr_list->destroy(attr_list);
321 return result;
322 }
323 attr_list->destroy(attr_list);
324
325 /* check the IMV state for the next PA-TNC attributes to send */
326 result = send_message(connection_id);
327 if (result != TNC_RESULT_SUCCESS)
328 {
329 state->set_recommendation(state,
330 TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
331 TNC_IMV_EVALUATION_RESULT_ERROR);
332 return imv_attestation->provide_recommendation(imv_attestation,
333 connection_id, src_imc_id);
334 }
335
336 if (attestation_state->get_handshake_state(attestation_state) ==
337 IMV_ATTESTATION_STATE_END)
338 {
339 if (attestation_state->get_file_meas_request_count(attestation_state))
340 {
341 DBG1(DBG_IMV, "failure due to %d pending file measurements",
342 attestation_state->get_file_meas_request_count(attestation_state));
343 attestation_state->set_measurement_error(attestation_state);
344 }
345 if (attestation_state->get_measurement_error(attestation_state))
346 {
347 state->set_recommendation(state,
348 TNC_IMV_ACTION_RECOMMENDATION_ISOLATE,
349 TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MAJOR);
350 }
351 else
352 {
353 state->set_recommendation(state,
354 TNC_IMV_ACTION_RECOMMENDATION_ALLOW,
355 TNC_IMV_EVALUATION_RESULT_COMPLIANT);
356 }
357 return imv_attestation->provide_recommendation(imv_attestation,
358 connection_id, src_imc_id);
359 }
360
361 return result;
362 }
363
364 /**
365 * see section 3.8.4 of TCG TNC IF-IMV Specification 1.3
366 */
367 TNC_Result TNC_IMV_ReceiveMessage(TNC_IMVID imv_id,
368 TNC_ConnectionID connection_id,
369 TNC_BufferReference msg,
370 TNC_UInt32 msg_len,
371 TNC_MessageType msg_type)
372 {
373 TNC_VendorID msg_vid;
374 TNC_MessageSubtype msg_subtype;
375
376 msg_vid = msg_type >> 8;
377 msg_subtype = msg_type & TNC_SUBTYPE_ANY;
378
379 return receive_message(imv_id, connection_id, 0, chunk_create(msg, msg_len),
380 msg_vid, msg_subtype, 0, TNC_IMVID_ANY);
381 }
382
383 /**
384 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
385 */
386 TNC_Result TNC_IMV_ReceiveMessageLong(TNC_IMVID imv_id,
387 TNC_ConnectionID connection_id,
388 TNC_UInt32 msg_flags,
389 TNC_BufferReference msg,
390 TNC_UInt32 msg_len,
391 TNC_VendorID msg_vid,
392 TNC_MessageSubtype msg_subtype,
393 TNC_UInt32 src_imc_id,
394 TNC_UInt32 dst_imv_id)
395 {
396 return receive_message(imv_id, connection_id, msg_flags,
397 chunk_create(msg, msg_len), msg_vid, msg_subtype,
398 src_imc_id, dst_imv_id);
399 }
400
401 /**
402 * see section 3.8.7 of TCG TNC IF-IMV Specification 1.3
403 */
404 TNC_Result TNC_IMV_SolicitRecommendation(TNC_IMVID imv_id,
405 TNC_ConnectionID connection_id)
406 {
407 if (!imv_attestation)
408 {
409 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
410 return TNC_RESULT_NOT_INITIALIZED;
411 }
412 return imv_attestation->provide_recommendation(imv_attestation,
413 connection_id, TNC_IMCID_ANY);
414 }
415
416 /**
417 * see section 3.8.8 of TCG TNC IF-IMV Specification 1.3
418 */
419 TNC_Result TNC_IMV_BatchEnding(TNC_IMVID imv_id,
420 TNC_ConnectionID connection_id)
421 {
422 imv_state_t *state;
423 imv_attestation_state_t *attestation_state;
424
425 if (!imv_attestation)
426 {
427 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
428 return TNC_RESULT_NOT_INITIALIZED;
429 }
430 /* get current IMV state */
431 if (!imv_attestation->get_state(imv_attestation, connection_id, &state))
432 {
433 return TNC_RESULT_FATAL;
434 }
435 attestation_state = (imv_attestation_state_t*)state;
436
437 /* Check if IMV has to initiate the PA-TNC exchange */
438 if (attestation_state->get_handshake_state(attestation_state) ==
439 IMV_ATTESTATION_STATE_INIT)
440 {
441 return send_message(connection_id);
442 }
443 return TNC_RESULT_SUCCESS;
444 }
445
446 /**
447 * see section 3.8.9 of TCG TNC IF-IMV Specification 1.3
448 */
449 TNC_Result TNC_IMV_Terminate(TNC_IMVID imv_id)
450 {
451 if (!imv_attestation)
452 {
453 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
454 return TNC_RESULT_NOT_INITIALIZED;
455 }
456 if (pts_creds)
457 {
458 pts_credmgr->remove_set(pts_credmgr, pts_creds->get_set(pts_creds));
459 pts_creds->destroy(pts_creds);
460 }
461 DESTROY_IF(pts_db);
462 DESTROY_IF(pts_credmgr);
463
464 libpts_deinit();
465
466 imv_attestation->destroy(imv_attestation);
467 imv_attestation = NULL;
468
469 return TNC_RESULT_SUCCESS;
470 }
471
472 /**
473 * see section 4.2.8.1 of TCG TNC IF-IMV Specification 1.3
474 */
475 TNC_Result TNC_IMV_ProvideBindFunction(TNC_IMVID imv_id,
476 TNC_TNCS_BindFunctionPointer bind_function)
477 {
478 if (!imv_attestation)
479 {
480 DBG1(DBG_IMV, "IMV \"%s\" has not been initialized", imv_name);
481 return TNC_RESULT_NOT_INITIALIZED;
482 }
483 return imv_attestation->bind_functions(imv_attestation, bind_function);
484 }