use base IMC ID if src IMC ID is not supported
[strongswan.git] / src / libpts / plugins / imc_attestation / imc_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 "imc_attestation_state.h"
17 #include "imc_attestation_process.h"
18
19 #include <imc/imc_agent.h>
20 #include <pa_tnc/pa_tnc_msg.h>
21 #include <ietf/ietf_attr.h>
22 #include <ietf/ietf_attr_pa_tnc_error.h>
23 #include <ietf/ietf_attr_product_info.h>
24 #include <ietf/ietf_attr_assess_result.h>
25
26 #include <libpts.h>
27
28 #include <pts/pts_error.h>
29
30 #include <tcg/tcg_pts_attr_proto_caps.h>
31 #include <tcg/tcg_pts_attr_meas_algo.h>
32
33 #include <tncif_pa_subtypes.h>
34
35 #include <pen/pen.h>
36 #include <debug.h>
37 #include <utils/linked_list.h>
38
39 /* IMC definitions */
40
41 static const char imc_name[] = "Attestation";
42
43 #define IMC_VENDOR_ID PEN_TCG
44 #define IMC_SUBTYPE PA_SUBTYPE_TCG_PTS
45
46 static imc_agent_t *imc_attestation;
47
48 /**
49 * Supported PTS measurement algorithms
50 */
51 static pts_meas_algorithms_t supported_algorithms = PTS_MEAS_ALGO_NONE;
52
53 /**
54 * Supported PTS Diffie Hellman Groups
55 */
56 static pts_dh_group_t supported_dh_groups = PTS_DH_GROUP_NONE;
57
58 /**
59 * see section 3.8.1 of TCG TNC IF-IMC Specification 1.3
60 */
61 TNC_Result TNC_IMC_Initialize(TNC_IMCID imc_id,
62 TNC_Version min_version,
63 TNC_Version max_version,
64 TNC_Version *actual_version)
65 {
66 if (imc_attestation)
67 {
68 DBG1(DBG_IMC, "IMC \"%s\" has already been initialized", imc_name);
69 return TNC_RESULT_ALREADY_INITIALIZED;
70 }
71 if (!pts_meas_algo_probe(&supported_algorithms) ||
72 !pts_dh_group_probe(&supported_dh_groups))
73 {
74 return TNC_RESULT_FATAL;
75 }
76 imc_attestation = imc_agent_create(imc_name, IMC_VENDOR_ID, IMC_SUBTYPE,
77 imc_id, actual_version);
78 if (!imc_attestation)
79 {
80 return TNC_RESULT_FATAL;
81 }
82
83 libpts_init();
84
85 if (min_version > TNC_IFIMC_VERSION_1 || max_version < TNC_IFIMC_VERSION_1)
86 {
87 DBG1(DBG_IMC, "no common IF-IMC version");
88 return TNC_RESULT_NO_COMMON_VERSION;
89 }
90 return TNC_RESULT_SUCCESS;
91 }
92
93 /**
94 * see section 3.8.2 of TCG TNC IF-IMC Specification 1.3
95 */
96 TNC_Result TNC_IMC_NotifyConnectionChange(TNC_IMCID imc_id,
97 TNC_ConnectionID connection_id,
98 TNC_ConnectionState new_state)
99 {
100 imc_state_t *state;
101
102 if (!imc_attestation)
103 {
104 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
105 return TNC_RESULT_NOT_INITIALIZED;
106 }
107 switch (new_state)
108 {
109 case TNC_CONNECTION_STATE_CREATE:
110 state = imc_attestation_state_create(connection_id);
111 return imc_attestation->create_state(imc_attestation, state);
112 case TNC_CONNECTION_STATE_HANDSHAKE:
113 if (imc_attestation->change_state(imc_attestation, connection_id,
114 new_state, &state) != TNC_RESULT_SUCCESS)
115 {
116 return TNC_RESULT_FATAL;
117 }
118 state->set_result(state, imc_id,
119 TNC_IMV_EVALUATION_RESULT_DONT_KNOW);
120 return TNC_RESULT_SUCCESS;
121 case TNC_CONNECTION_STATE_DELETE:
122 return imc_attestation->delete_state(imc_attestation, connection_id);
123 case TNC_CONNECTION_STATE_ACCESS_ISOLATED:
124 case TNC_CONNECTION_STATE_ACCESS_NONE:
125 default:
126 return imc_attestation->change_state(imc_attestation, connection_id,
127 new_state, NULL);
128 }
129 }
130
131
132 /**
133 * see section 3.8.3 of TCG TNC IF-IMC Specification 1.3
134 */
135 TNC_Result TNC_IMC_BeginHandshake(TNC_IMCID imc_id,
136 TNC_ConnectionID connection_id)
137 {
138 imc_state_t *state;
139 imc_attestation_state_t *attestation_state;
140 pts_t *pts;
141 char *platform_info;
142 TNC_Result result = TNC_RESULT_SUCCESS;
143
144 if (!imc_attestation)
145 {
146 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
147 return TNC_RESULT_NOT_INITIALIZED;
148 }
149
150 /* get current IMC state */
151 if (!imc_attestation->get_state(imc_attestation, connection_id, &state))
152 {
153 return TNC_RESULT_FATAL;
154 }
155 attestation_state = (imc_attestation_state_t*)state;
156 pts = attestation_state->get_pts(attestation_state);
157
158 platform_info = pts->get_platform_info(pts);
159 if (platform_info)
160 {
161 linked_list_t *attr_list;
162 pa_tnc_attr_t *attr;
163
164 attr_list = linked_list_create();
165 attr = ietf_attr_product_info_create(0, 0, platform_info);
166 attr_list->insert_last(attr_list, attr);
167 result = imc_attestation->send_message(imc_attestation, connection_id,
168 FALSE, 0, TNC_IMVID_ANY, attr_list);
169 attr_list->destroy(attr_list);
170 }
171
172 return result;
173 }
174
175 static TNC_Result receive_message(TNC_IMCID imc_id,
176 TNC_ConnectionID connection_id,
177 TNC_UInt32 msg_flags,
178 chunk_t msg,
179 TNC_VendorID msg_vid,
180 TNC_MessageSubtype msg_subtype,
181 TNC_UInt32 src_imv_id,
182 TNC_UInt32 dst_imc_id)
183 {
184 pa_tnc_msg_t *pa_tnc_msg;
185 pa_tnc_attr_t *attr;
186 pen_type_t type;
187 linked_list_t *attr_list;
188 imc_state_t *state;
189 imc_attestation_state_t *attestation_state;
190 enumerator_t *enumerator;
191 TNC_Result result;
192 TNC_UInt32 target_imc_id;
193
194 if (!imc_attestation)
195 {
196 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
197 return TNC_RESULT_NOT_INITIALIZED;
198 }
199
200 /* get current IMC state */
201 if (!imc_attestation->get_state(imc_attestation, connection_id, &state))
202 {
203 return TNC_RESULT_FATAL;
204 }
205 attestation_state = (imc_attestation_state_t*)state;
206
207 /* parse received PA-TNC message and automatically handle any errors */
208 result = imc_attestation->receive_message(imc_attestation, state, msg,
209 msg_vid, msg_subtype, src_imv_id, dst_imc_id, &pa_tnc_msg);
210
211 /* no parsed PA-TNC attributes available if an error occurred */
212 if (!pa_tnc_msg)
213 {
214 return result;
215 }
216 target_imc_id = (dst_imc_id == TNC_IMCID_ANY) ? imc_id : dst_imc_id;
217
218 /* preprocess any IETF standard error attributes */
219 result = pa_tnc_msg->process_ietf_std_errors(pa_tnc_msg) ?
220 TNC_RESULT_FATAL : TNC_RESULT_SUCCESS;
221
222 attr_list = linked_list_create();
223
224 /* analyze PA-TNC attributes */
225 enumerator = pa_tnc_msg->create_attribute_enumerator(pa_tnc_msg);
226 while (enumerator->enumerate(enumerator, &attr))
227 {
228 type = attr->get_type(attr);
229
230 if (type.vendor_id == PEN_IETF)
231 {
232 if (type.type == IETF_ATTR_PA_TNC_ERROR)
233 {
234 ietf_attr_pa_tnc_error_t *error_attr;
235 pen_type_t error_code;
236 chunk_t msg_info;
237
238 error_attr = (ietf_attr_pa_tnc_error_t*)attr;
239 error_code = error_attr->get_error_code(error_attr);
240
241 if (error_code.vendor_id == PEN_TCG)
242 {
243 msg_info = error_attr->get_msg_info(error_attr);
244
245 DBG1(DBG_IMC, "received TCG-PTS error '%N'",
246 pts_error_code_names, error_code.type);
247 DBG1(DBG_IMC, "error information: %B", &msg_info);
248
249 result = TNC_RESULT_FATAL;
250 }
251 }
252 else if (type.type == IETF_ATTR_ASSESSMENT_RESULT)
253 {
254 ietf_attr_assess_result_t *ietf_attr;
255
256 ietf_attr = (ietf_attr_assess_result_t*)attr;
257 state->set_result(state, target_imc_id,
258 ietf_attr->get_result(ietf_attr));
259 }
260 }
261 else if (type.vendor_id == PEN_TCG)
262 {
263 if (!imc_attestation_process(attr, attr_list, attestation_state,
264 supported_algorithms, supported_dh_groups))
265 {
266 result = TNC_RESULT_FATAL;
267 break;
268 }
269 }
270 }
271 enumerator->destroy(enumerator);
272 pa_tnc_msg->destroy(pa_tnc_msg);
273
274 if (result == TNC_RESULT_SUCCESS && attr_list->get_count(attr_list))
275 {
276 result = imc_attestation->send_message(imc_attestation, connection_id,
277 FALSE, 0, TNC_IMVID_ANY, attr_list);
278 }
279 attr_list->destroy(attr_list);
280
281 return result;
282 }
283
284 /**
285 * see section 3.8.4 of TCG TNC IF-IMC Specification 1.3
286 */
287 TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
288 TNC_ConnectionID connection_id,
289 TNC_BufferReference msg,
290 TNC_UInt32 msg_len,
291 TNC_MessageType msg_type)
292 {
293 TNC_VendorID msg_vid;
294 TNC_MessageSubtype msg_subtype;
295
296 msg_vid = msg_type >> 8;
297 msg_subtype = msg_type & TNC_SUBTYPE_ANY;
298
299 return receive_message(imc_id, connection_id, 0, chunk_create(msg, msg_len),
300 msg_vid, msg_subtype, 0, TNC_IMCID_ANY);
301 }
302
303 /**
304 * see section 3.8.6 of TCG TNC IF-IMV Specification 1.3
305 */
306 TNC_Result TNC_IMC_ReceiveMessageLong(TNC_IMCID imc_id,
307 TNC_ConnectionID connection_id,
308 TNC_UInt32 msg_flags,
309 TNC_BufferReference msg,
310 TNC_UInt32 msg_len,
311 TNC_VendorID msg_vid,
312 TNC_MessageSubtype msg_subtype,
313 TNC_UInt32 src_imv_id,
314 TNC_UInt32 dst_imc_id)
315 {
316 return receive_message(imc_id, connection_id, msg_flags,
317 chunk_create(msg, msg_len), msg_vid, msg_subtype,
318 src_imv_id, dst_imc_id);
319 }
320
321 /**
322 * see section 3.8.7 of TCG TNC IF-IMC Specification 1.3
323 */
324 TNC_Result TNC_IMC_BatchEnding(TNC_IMCID imc_id,
325 TNC_ConnectionID connection_id)
326 {
327 if (!imc_attestation)
328 {
329 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
330 return TNC_RESULT_NOT_INITIALIZED;
331 }
332 return TNC_RESULT_SUCCESS;
333 }
334
335 /**
336 * see section 3.8.8 of TCG TNC IF-IMC Specification 1.3
337 */
338 TNC_Result TNC_IMC_Terminate(TNC_IMCID imc_id)
339 {
340 if (!imc_attestation)
341 {
342 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
343 return TNC_RESULT_NOT_INITIALIZED;
344 }
345
346 libpts_deinit();
347
348 imc_attestation->destroy(imc_attestation);
349 imc_attestation = NULL;
350
351 return TNC_RESULT_SUCCESS;
352 }
353
354 /**
355 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.3
356 */
357 TNC_Result TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id,
358 TNC_TNCC_BindFunctionPointer bind_function)
359 {
360 if (!imc_attestation)
361 {
362 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
363 return TNC_RESULT_NOT_INITIALIZED;
364 }
365 return imc_attestation->bind_functions(imc_attestation, bind_function);
366 }