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