Attestation IMC/IMV pair uses TCG_PTS subtype
[strongswan.git] / src / libimcv / plugins / imc_attestation / imc_attestation.c
1 /*
2 * Copyright (C) 2011 Sansar Choinyambuu
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
18 #include <imc/imc_agent.h>
19 #include <pa_tnc/pa_tnc_msg.h>
20 #include <ietf/ietf_attr.h>
21 #include <ietf/ietf_attr_pa_tnc_error.h>
22 #include <tcg/tcg_attr.h>
23
24 #include <tncif_pa_subtypes.h>
25
26 #include <pen/pen.h>
27 #include <debug.h>
28
29 /* IMC definitions */
30
31 static const char imc_name[] = "Attestation";
32
33 #define IMC_VENDOR_ID PEN_TCG
34 #define IMC_SUBTYPE PA_SUBTYPE_TCG_PTS
35
36 static imc_agent_t *imc_attestation;
37
38 /**
39 * see section 3.7.1 of TCG TNC IF-IMC Specification 1.2
40 */
41 TNC_Result TNC_IMC_Initialize(TNC_IMCID imc_id,
42 TNC_Version min_version,
43 TNC_Version max_version,
44 TNC_Version *actual_version)
45 {
46 if (imc_attestation)
47 {
48 DBG1(DBG_IMC, "IMC \"%s\" has already been initialized", imc_name);
49 return TNC_RESULT_ALREADY_INITIALIZED;
50 }
51 imc_attestation = imc_agent_create(imc_name, IMC_VENDOR_ID, IMC_SUBTYPE,
52 imc_id, actual_version);
53 if (!imc_attestation)
54 {
55 return TNC_RESULT_FATAL;
56 }
57 if (min_version > TNC_IFIMC_VERSION_1 || max_version < TNC_IFIMC_VERSION_1)
58 {
59 DBG1(DBG_IMC, "no common IF-IMC version");
60 return TNC_RESULT_NO_COMMON_VERSION;
61 }
62 return TNC_RESULT_SUCCESS;
63 }
64
65 /**
66 * see section 3.7.2 of TCG TNC IF-IMC Specification 1.2
67 */
68 TNC_Result TNC_IMC_NotifyConnectionChange(TNC_IMCID imc_id,
69 TNC_ConnectionID connection_id,
70 TNC_ConnectionState new_state)
71 {
72 imc_state_t *state;
73 imc_attestation_state_t *test_state;
74
75 if (!imc_attestation)
76 {
77 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
78 return TNC_RESULT_NOT_INITIALIZED;
79 }
80 switch (new_state)
81 {
82 case TNC_CONNECTION_STATE_CREATE:
83 state = imc_attestation_state_create(connection_id);
84 return imc_attestation->create_state(imc_attestation, state);
85 case TNC_CONNECTION_STATE_DELETE:
86 return imc_attestation->delete_state(imc_attestation, connection_id);
87 case TNC_CONNECTION_STATE_ACCESS_ISOLATED:
88 case TNC_CONNECTION_STATE_ACCESS_NONE:
89 default:
90 return imc_attestation->change_state(imc_attestation, connection_id, new_state);
91 }
92 }
93
94 static TNC_Result send_message(TNC_ConnectionID connection_id)
95 {
96 pa_tnc_msg_t *msg;
97 pa_tnc_attr_t *attr;
98 imc_state_t *state;
99 imc_attestation_state_t *test_state;
100 TNC_Result result;
101
102 if (!imc_attestation->get_state(imc_attestation, connection_id, &state))
103 {
104 return TNC_RESULT_FATAL;
105 }
106 test_state = (imc_attestation_state_t*)state;
107 msg = pa_tnc_msg_create();
108 /**
109 * add TCG PTS attributes
110 */
111 msg->build(msg);
112 result = imc_attestation->send_message(imc_attestation, connection_id,
113 msg->get_encoding(msg));
114 msg->destroy(msg);
115
116 return result;
117 }
118
119 /**
120 * see section 3.7.3 of TCG TNC IF-IMC Specification 1.2
121 */
122 TNC_Result TNC_IMC_BeginHandshake(TNC_IMCID imc_id,
123 TNC_ConnectionID connection_id)
124 {
125 if (!imc_attestation)
126 {
127 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
128 return TNC_RESULT_NOT_INITIALIZED;
129 }
130 return send_message(connection_id);
131 }
132
133 /**
134 * see section 3.7.4 of TCG TNC IF-IMC Specification 1.2
135 */
136 TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
137 TNC_ConnectionID connection_id,
138 TNC_BufferReference msg,
139 TNC_UInt32 msg_len,
140 TNC_MessageType msg_type)
141 {
142 pa_tnc_msg_t *pa_tnc_msg;
143 pa_tnc_attr_t *attr;
144 enumerator_t *enumerator;
145 TNC_Result result;
146 bool fatal_error = FALSE;
147
148 if (!imc_attestation)
149 {
150 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
151 return TNC_RESULT_NOT_INITIALIZED;
152 }
153
154 /* parse received PA-TNC message and automatically handle any errors */
155 result = imc_attestation->receive_message(imc_attestation, connection_id,
156 chunk_create(msg, msg_len), msg_type,
157 &pa_tnc_msg);
158
159 /* no parsed PA-TNC attributes available if an error occurred */
160 if (!pa_tnc_msg)
161 {
162 return result;
163 }
164
165 /* analyze PA-TNC attributes */
166 enumerator = pa_tnc_msg->create_attribute_enumerator(pa_tnc_msg);
167 while (enumerator->enumerate(enumerator, &attr))
168 {
169 if (attr->get_vendor_id(attr) == PEN_IETF &&
170 attr->get_type(attr) == IETF_ATTR_PA_TNC_ERROR)
171 {
172 ietf_attr_pa_tnc_error_t *error_attr;
173 pa_tnc_error_code_t error_code;
174 chunk_t msg_info, attr_info;
175
176 error_attr = (ietf_attr_pa_tnc_error_t*)attr;
177 error_code = error_attr->get_error_code(error_attr);
178 msg_info = error_attr->get_msg_info(error_attr);
179
180 DBG1(DBG_IMC, "received PA-TNC error '%N' concerning message %#B",
181 pa_tnc_error_code_names, error_code, &msg_info);
182 switch (error_code)
183 {
184 case PA_ERROR_ATTR_TYPE_NOT_SUPPORTED:
185 attr_info = error_attr->get_attr_info(error_attr);
186 DBG1(DBG_IMC, " unsupported attribute %#B", &attr_info);
187 break;
188 default:
189 break;
190 }
191 fatal_error = TRUE;
192 }
193 else if (attr->get_vendor_id(attr) == PEN_TCG)
194 {
195 /**
196 * Handle TCG PTS attributes
197 */
198 }
199 }
200 enumerator->destroy(enumerator);
201 pa_tnc_msg->destroy(pa_tnc_msg);
202
203 /* if no error occurred then always return the same response */
204 return fatal_error ? TNC_RESULT_FATAL : send_message(connection_id);
205 }
206
207 /**
208 * see section 3.7.5 of TCG TNC IF-IMC Specification 1.2
209 */
210 TNC_Result TNC_IMC_BatchEnding(TNC_IMCID imc_id,
211 TNC_ConnectionID connection_id)
212 {
213 if (!imc_attestation)
214 {
215 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
216 return TNC_RESULT_NOT_INITIALIZED;
217 }
218 return TNC_RESULT_SUCCESS;
219 }
220
221 /**
222 * see section 3.7.6 of TCG TNC IF-IMC Specification 1.2
223 */
224 TNC_Result TNC_IMC_Terminate(TNC_IMCID imc_id)
225 {
226 if (!imc_attestation)
227 {
228 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
229 return TNC_RESULT_NOT_INITIALIZED;
230 }
231 imc_attestation->destroy(imc_attestation);
232 imc_attestation = NULL;
233
234 return TNC_RESULT_SUCCESS;
235 }
236
237 /**
238 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.2
239 */
240 TNC_Result TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id,
241 TNC_TNCC_BindFunctionPointer bind_function)
242 {
243 if (!imc_attestation)
244 {
245 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
246 return TNC_RESULT_NOT_INITIALIZED;
247 }
248 return imc_attestation->bind_functions(imc_attestation, bind_function);
249 }