fcabc55c3a94e280d796693f172c97e50e45125e
[strongswan.git] / src / libcharon / plugins / tnccs_20 / tnccs_20.c
1 /*
2 * Copyright (C) 2010 Sansar Choinyanbuu
3 * Copyright (C) 2010 Andreas Steffen
4 * HSR Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "tnccs_20.h"
18 #include "tnccs_20_types.h"
19 #include "batch/pb_tnc_batch.h"
20 #include "messages/pb_tnc_message.h"
21 #include "messages/pb_pa_message.h"
22 #include "messages/pb_error_message.h"
23 #include "messages/pb_assessment_result_message.h"
24 #include "messages/pb_access_recommendation_message.h"
25 #include "messages/pb_reason_string_message.h"
26 #include "messages/pb_language_preference_message.h"
27 #include "state_machine/pb_tnc_state_machine.h"
28
29 #include <debug.h>
30 #include <daemon.h>
31 #include <threading/mutex.h>
32 #include <tnc/tncif.h>
33 #include <tnc/tncifimv_names.h>
34 #include <tnc/tnccs/tnccs.h>
35
36 typedef struct private_tnccs_20_t private_tnccs_20_t;
37
38 /**
39 * Private data of a tnccs_20_t object.
40 */
41 struct private_tnccs_20_t {
42
43 /**
44 * Public tls_t interface.
45 */
46 tls_t public;
47
48 /**
49 * TNCC if TRUE, TNCS if FALSE
50 */
51 bool is_server;
52
53 /**
54 * PB-TNC State Machine
55 */
56 pb_tnc_state_machine_t *state_machine;
57
58 /**
59 * Connection ID assigned to this TNCCS connection
60 */
61 TNC_ConnectionID connection_id;
62
63 /**
64 * PB-TNC batch being constructed
65 */
66 pb_tnc_batch_t *batch;
67
68 /**
69 * Mutex locking the batch in construction
70 */
71 mutex_t *mutex;
72
73 /**
74 * Flag set by IMC/IMV RequestHandshakeRetry() function
75 */
76 bool request_handshake_retry;
77
78 /**
79 * Set of IMV recommendations (TNC Server only)
80 */
81 recommendations_t *recs;
82 };
83
84 METHOD(tnccs_t, send_message, void,
85 private_tnccs_20_t* this, TNC_IMCID imc_id, TNC_IMVID imv_id,
86 TNC_BufferReference msg,
87 TNC_UInt32 msg_len,
88 TNC_MessageType msg_type)
89 {
90 TNC_MessageSubtype msg_sub_type;
91 TNC_VendorID msg_vendor_id;
92 pb_tnc_message_t *pb_tnc_msg;
93 pb_tnc_batch_type_t batch_type;
94
95 msg_sub_type = msg_type & TNC_SUBTYPE_ANY;
96 msg_vendor_id = (msg_type >> 8) & TNC_VENDORID_ANY;
97
98 pb_tnc_msg = pb_pa_message_create(msg_vendor_id, msg_sub_type, imc_id, imv_id,
99 chunk_create(msg, msg_len));
100
101 /* adding PA message to SDATA or CDATA batch only */
102 batch_type = this->is_server ? PB_BATCH_SDATA : PB_BATCH_CDATA;
103 this->mutex->lock(this->mutex);
104 if (!this->batch)
105 {
106 this->batch = pb_tnc_batch_create(this->is_server, batch_type);
107 }
108 if (this->batch->get_type(this->batch) == batch_type)
109 {
110 this->batch->add_message(this->batch, pb_tnc_msg);
111 }
112 else
113 {
114 pb_tnc_msg->destroy(pb_tnc_msg);
115 }
116 this->mutex->unlock(this->mutex);
117 }
118
119 static status_t handle_messages(private_tnccs_20_t *this, pb_tnc_batch_t *batch)
120 {
121 enumerator_t *enumerator;
122 pb_tnc_message_t *msg;
123
124 enumerator = batch->create_msg_enumerator(batch);
125 while (enumerator->enumerate(enumerator, &msg))
126 {
127 switch (msg->get_type(msg))
128 {
129 case PB_MSG_EXPERIMENTAL:
130 /* for experiments */
131 break;
132 case PB_MSG_PA:
133 {
134 pb_pa_message_t *pa_msg;
135 TNC_MessageType msg_type;
136 u_int32_t vendor_id, subtype;
137 chunk_t msg_body;
138
139 pa_msg = (pb_pa_message_t*)msg;
140 vendor_id = pa_msg->get_vendor_id(pa_msg, &subtype);
141 msg_type = (vendor_id << 8) | (subtype & 0xff);
142 msg_body = pa_msg->get_body(pa_msg);
143
144 DBG2(DBG_TNC, "handling message type 0x%08x", msg_type);
145
146 if (this->is_server)
147 {
148 charon->imvs->receive_message(charon->imvs,
149 this->connection_id, msg_body.ptr, msg_body.len, msg_type);
150 }
151 else
152 {
153 charon->imcs->receive_message(charon->imcs,
154 this->connection_id, msg_body.ptr, msg_body.len,msg_type);
155 }
156 break;
157 }
158 case PB_MSG_ASSESSMENT_RESULT:
159 {
160 pb_assessment_result_message_t *assess_msg;
161 u_int32_t result;
162
163 assess_msg = (pb_assessment_result_message_t*)msg;
164 result = assess_msg->get_assessment_result(assess_msg);
165 DBG1(DBG_TNC, "assessment result is '%N'",
166 evaluation_result_names, result);
167 break;
168 }
169 case PB_MSG_ACCESS_RECOMMENDATION:
170 {
171 pb_access_recommendation_message_t *rec_msg;
172 u_int16_t rec;
173
174 rec_msg = (pb_access_recommendation_message_t*)msg;
175 rec = rec_msg->get_access_recommendation(rec_msg);
176 DBG1(DBG_TNC, "access_recommendation is '%N'",
177 action_recommendation_names, rec);
178 break;
179 }
180 case PB_MSG_REMEDIATION_PARAMETERS:
181 {
182 /* TODO : Remediation parameters message processing */
183 break;
184 }
185 case PB_MSG_ERROR:
186 {
187 pb_error_message_t *err_msg;
188 bool fatal;
189 u_int32_t vendor_id;
190 u_int16_t error_code;
191
192 err_msg = (pb_error_message_t*)msg;
193 fatal = err_msg->get_fatal_flag(err_msg);
194 vendor_id = err_msg->get_vendor_id(err_msg);
195 error_code = err_msg->get_error_code(err_msg);
196
197 if (vendor_id == IETF_VENDOR_ID)
198 {
199 switch (error_code)
200 {
201 case PB_ERROR_INVALID_PARAMETER:
202 case PB_ERROR_UNSUPPORTED_MANDATORY_MESSAGE:
203 DBG1(DBG_TNC, "received %s PB-TNC Error '%N' "
204 "(offset %u bytes)",
205 fatal ? "fatal" : "non-fatal",
206 pb_tnc_error_code_names, error_code,
207 err_msg->get_offset(err_msg));
208 break;
209 case PB_ERROR_VERSION_NOT_SUPPORTED:
210 DBG1(DBG_TNC, "received %s PB-TNC Error '%N' "
211 "caused by bad version 0x%02x",
212 fatal ? "fatal" : "non-fatal",
213 pb_tnc_error_code_names, error_code,
214 err_msg->get_bad_version(err_msg));
215 break;
216 case PB_ERROR_UNEXPECTED_BATCH_TYPE:
217 case PB_ERROR_LOCAL_ERROR:
218 default:
219 DBG1(DBG_TNC, "received %s PB-TNC Error '%N'",
220 fatal ? "fatal" : "non-fatal",
221 pb_tnc_error_code_names, error_code);
222 break;
223 }
224 }
225 else
226 {
227 DBG1(DBG_TNC, "received %s PB-TNC Error (%u) "
228 "with Vendor ID 0x%06x",
229 fatal ? "fatal" : "non-fatal",
230 error_code, vendor_id);
231 }
232 break;
233 }
234 case PB_MSG_LANGUAGE_PREFERENCE:
235 {
236 pb_language_preference_message_t *lang_msg;
237 chunk_t lang;
238
239 lang_msg = (pb_language_preference_message_t*)msg;
240 lang = lang_msg->get_language_preference(lang_msg);
241
242 DBG2(DBG_TNC, "setting language preference '%.*s'",
243 lang.len, lang.ptr);
244 this->recs->set_preferred_language(this->recs, lang);
245 break;
246 }
247 case PB_MSG_REASON_STRING:
248 {
249 pb_reason_string_message_t *reason_msg;
250 chunk_t reason_string, language_code;
251
252 reason_msg = (pb_reason_string_message_t*)msg;
253 reason_string = reason_msg->get_reason_string(reason_msg);
254 language_code = reason_msg->get_language_code(reason_msg);
255 DBG2(DBG_TNC, "reason string: '%.*s", reason_string.len,
256 reason_string.ptr);
257 DBG2(DBG_TNC, "language code: '%.*s", language_code.len,
258 language_code.ptr);
259 break;
260 }
261 default:
262 break;
263 }
264 }
265 enumerator->destroy(enumerator);
266
267 return SUCCESS;
268 }
269
270 METHOD(tls_t, process, status_t,
271 private_tnccs_20_t *this, void *buf, size_t buflen)
272 {
273 chunk_t data;
274 pb_tnc_batch_t *batch;
275 pb_tnc_message_t *msg;
276 enumerator_t *enumerator;
277 status_t status;
278
279 if (this->is_server && !this->connection_id)
280 {
281 this->connection_id = charon->tnccs->create_connection(charon->tnccs,
282 (tnccs_t*)this, _send_message,
283 &this->request_handshake_retry, &this->recs);
284 if (!this->connection_id)
285 {
286 return FAILED;
287 }
288 charon->imvs->notify_connection_change(charon->imvs,
289 this->connection_id, TNC_CONNECTION_STATE_CREATE);
290 }
291
292 data = chunk_create(buf, buflen);
293 DBG1(DBG_TNC, "received TNCCS Batch (%u bytes) for Connection ID %u",
294 data.len, this->connection_id);
295 DBG3(DBG_TNC, "%B", &data);
296 batch = pb_tnc_batch_create_from_data(this->is_server, data);
297 status = batch->process(batch, this->state_machine);
298
299 switch (status)
300 {
301 case SUCCESS:
302 handle_messages(this, batch);
303 if (this->is_server)
304 {
305 charon->imvs->batch_ending(charon->imvs, this->connection_id);
306 }
307 else
308 {
309 charon->imcs->batch_ending(charon->imcs, this->connection_id);
310 }
311 break;
312 case FAILED:
313 this->mutex->lock(this->mutex);
314 if (this->batch)
315 {
316 DBG1(DBG_TNC, "cancelling PB-TNC %N Batch",
317 pb_tnc_batch_type_names, this->batch->get_type(this->batch));
318 this->batch->destroy(this->batch);
319 }
320 this->batch = pb_tnc_batch_create(this->is_server, PB_BATCH_CLOSE);
321 this->mutex->unlock(this->mutex);
322 /* fall through */
323 case VERIFY_ERROR:
324 enumerator = batch->create_error_enumerator(batch);
325 while (enumerator->enumerate(enumerator, &msg))
326 {
327 this->mutex->lock(this->mutex);
328 this->batch->add_message(this->batch, msg->get_ref(msg));
329 this->mutex->unlock(this->mutex);
330 }
331 enumerator->destroy(enumerator);
332 default:
333 break;
334 }
335 batch->destroy(batch);
336
337 return NEED_MORE;
338 }
339
340 METHOD(tls_t, build, status_t,
341 private_tnccs_20_t *this, void *buf, size_t *buflen, size_t *msglen)
342 {
343 if (!this->is_server && !this->connection_id)
344 {
345 pb_tnc_message_t *msg;
346 char *pref_lang;
347
348 this->connection_id = charon->tnccs->create_connection(charon->tnccs,
349 (tnccs_t*)this, _send_message,
350 &this->request_handshake_retry, NULL);
351 if (!this->connection_id)
352 {
353 return FAILED;
354 }
355
356 /* Create PB-TNC Language Preference Message */
357 pref_lang = charon->imcs->get_preferred_language(charon->imcs);
358 msg = pb_language_preference_message_create(chunk_create(pref_lang,
359 strlen(pref_lang)));
360 this->mutex->lock(this->mutex);
361 this->batch = pb_tnc_batch_create(this->is_server, PB_BATCH_CDATA);
362 this->batch->add_message(this->batch, msg);
363 this->mutex->unlock(this->mutex);
364
365 charon->imcs->notify_connection_change(charon->imcs,
366 this->connection_id, TNC_CONNECTION_STATE_CREATE);
367 charon->imcs->notify_connection_change(charon->imcs,
368 this->connection_id, TNC_CONNECTION_STATE_HANDSHAKE);
369 charon->imcs->begin_handshake(charon->imcs, this->connection_id);
370 }
371
372 if (this->batch)
373 {
374 pb_tnc_batch_type_t batch_type;
375 status_t status;
376 chunk_t data;
377
378 this->mutex->lock(this->mutex);
379 batch_type = this->batch->get_type(this->batch);
380
381 if (this->state_machine->send_batch(this->state_machine, batch_type))
382 {
383 this->batch->build(this->batch);
384 data = this->batch->get_encoding(this->batch);
385 DBG1(DBG_TNC, "sending PB-TNC %N Batch (%d bytes) for Connection ID %u",
386 pb_tnc_batch_type_names, batch_type, data.len,
387 this->connection_id);
388 DBG3(DBG_TNC, "%B", &data);
389
390 *msglen = data.len;
391 *buflen = data.len;
392 memcpy(buf, data.ptr, data.len);
393 status = ALREADY_DONE;
394 }
395 else
396 {
397 DBG1(DBG_TNC, "cancelling unexpected PB-TNC Batch Type: %N",
398 pb_tnc_batch_type_names, batch_type);
399 status = INVALID_STATE;
400 }
401
402 this->batch->destroy(this->batch);
403 this->batch = NULL;
404 this->mutex->unlock(this->mutex);
405
406 return status;
407 }
408 else
409 {
410 DBG1(DBG_TNC, "no TNCCS Batch to send");
411 return INVALID_STATE;
412 }
413 }
414
415 METHOD(tls_t, is_server, bool,
416 private_tnccs_20_t *this)
417 {
418 return this->is_server;
419 }
420
421 METHOD(tls_t, get_purpose, tls_purpose_t,
422 private_tnccs_20_t *this)
423 {
424 return TLS_PURPOSE_EAP_TNC;
425 }
426
427 METHOD(tls_t, is_complete, bool,
428 private_tnccs_20_t *this)
429 {
430 TNC_IMV_Action_Recommendation rec;
431 TNC_IMV_Evaluation_Result eval;
432
433 if (this->recs && this->recs->have_recommendation(this->recs, &rec, &eval))
434 {
435 DBG2(DBG_TNC, "Final recommendation '%N' and evaluation '%N'",
436 action_recommendation_names, rec, evaluation_result_names, eval);
437
438 return charon->imvs->enforce_recommendation(charon->imvs, rec);
439 }
440 else
441 {
442 return FALSE;
443 }
444 }
445
446 METHOD(tls_t, get_eap_msk, chunk_t,
447 private_tnccs_20_t *this)
448 {
449 return chunk_empty;
450 }
451
452 METHOD(tls_t, destroy, void,
453 private_tnccs_20_t *this)
454 {
455 charon->tnccs->remove_connection(charon->tnccs, this->connection_id);
456 this->state_machine->destroy(this->state_machine);
457 this->mutex->destroy(this->mutex);
458 DESTROY_IF(this->batch);
459 free(this);
460 }
461
462 /**
463 * See header
464 */
465 tls_t *tnccs_20_create(bool is_server)
466 {
467 private_tnccs_20_t *this;
468
469 INIT(this,
470 .public = {
471 .process = _process,
472 .build = _build,
473 .is_server = _is_server,
474 .get_purpose = _get_purpose,
475 .is_complete = _is_complete,
476 .get_eap_msk = _get_eap_msk,
477 .destroy = _destroy,
478 },
479 .is_server = is_server,
480 .state_machine = pb_tnc_state_machine_create(is_server),
481 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
482 );
483
484 return &this->public;
485 }