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