debug cosmetics
[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
28 #include <debug.h>
29 #include <daemon.h>
30 #include <threading/mutex.h>
31 #include <tnc/tncif.h>
32 #include <tnc/tncifimv_names.h>
33 #include <tnc/tnccs/tnccs.h>
34
35 typedef struct private_tnccs_20_t private_tnccs_20_t;
36
37 /**
38 * Private data of a tnccs_20_t object.
39 */
40 struct private_tnccs_20_t {
41
42 /**
43 * Public tls_t interface.
44 */
45 tls_t public;
46
47 /**
48 * TNCC if TRUE, TNCS if FALSE
49 */
50 bool is_server;
51
52 /**
53 * PB-TNC State Machine
54 */
55 pb_tnc_state_t state;
56
57 /**
58 * Connection ID assigned to this TNCCS connection
59 */
60 TNC_ConnectionID connection_id;
61
62 /**
63 * PB-TNC batch being constructed
64 */
65 pb_tnc_batch_t *batch;
66
67 /**
68 * Mutex locking the batch in construction
69 */
70 mutex_t *mutex;
71
72 /**
73 * Flag set by IMC/IMV RequestHandshakeRetry() function
74 */
75 bool request_handshake_retry;
76
77 /**
78 * Set of IMV recommendations (TNC Server only)
79 */
80 recommendations_t *recs;
81 };
82
83 METHOD(tnccs_t, send_message, void,
84 private_tnccs_20_t* this, TNC_IMCID imc_id, TNC_IMVID imv_id,
85 TNC_BufferReference msg,
86 TNC_UInt32 msg_len,
87 TNC_MessageType msg_type)
88 {
89 TNC_MessageSubtype msg_sub_type;
90 TNC_VendorID msg_vendor_id;
91 pb_tnc_message_t *pb_tnc_msg;
92 pb_tnc_batch_type_t batch_type;
93
94 msg_sub_type = msg_type & TNC_SUBTYPE_ANY;
95 msg_vendor_id = (msg_type >> 8) & TNC_VENDORID_ANY;
96
97 pb_tnc_msg = pb_pa_message_create(msg_vendor_id, msg_sub_type, imc_id, imv_id,
98 chunk_create(msg, msg_len));
99
100 /* adding PA message to SDATA or CDATA batch only */
101 batch_type = this->is_server ? PB_BATCH_SDATA : PB_BATCH_CDATA;
102 this->mutex->lock(this->mutex);
103 if (!this->batch)
104 {
105 this->batch = pb_tnc_batch_create(this->is_server, batch_type);
106 }
107 if (this->batch->get_type(this->batch) == batch_type)
108 {
109 this->batch->add_message(this->batch, pb_tnc_msg);
110 }
111 else
112 {
113 pb_tnc_msg->destroy(pb_tnc_msg);
114 }
115 this->mutex->unlock(this->mutex);
116 }
117
118 static status_t handle_messages(private_tnccs_20_t *this, pb_tnc_batch_t *batch)
119 {
120 enumerator_t *enumerator;
121 pb_tnc_message_t *msg;
122
123 enumerator = batch->create_msg_enumerator(batch);
124 while (enumerator->enumerate(enumerator, &msg))
125 {
126 switch (msg->get_type(msg))
127 {
128 case PB_MSG_EXPERIMENTAL:
129 /* for experiments */
130 break;
131 case PB_MSG_PA:
132 {
133 pb_pa_message_t *pa_msg;
134 TNC_MessageType msg_type;
135 u_int32_t vendor_id, subtype;
136 chunk_t msg_body;
137
138 pa_msg = (pb_pa_message_t*)msg;
139 vendor_id = pa_msg->get_vendor_id(pa_msg, &subtype);
140 msg_type = (vendor_id << 8) | (subtype & 0xff);
141 msg_body = pa_msg->get_body(pa_msg);
142
143 DBG2(DBG_TNC, "handling message type 0x%08x", msg_type);
144
145 if (this->is_server)
146 {
147 charon->imvs->receive_message(charon->imvs,
148 this->connection_id, msg_body.ptr, msg_body.len, msg_type);
149 }
150 else
151 {
152 charon->imcs->receive_message(charon->imcs,
153 this->connection_id, msg_body.ptr, msg_body.len,msg_type);
154 }
155 break;
156 }
157 case PB_MSG_ASSESSMENT_RESULT:
158 {
159 pb_assessment_result_message_t *assess_msg;
160 u_int32_t result;
161
162 assess_msg = (pb_assessment_result_message_t*)msg;
163 result = assess_msg->get_assessment_result(assess_msg);
164 DBG1(DBG_TNC, "assessment result is '%N'",
165 evaluation_result_names, result);
166 break;
167 }
168 case PB_MSG_ACCESS_RECOMMENDATION:
169 {
170 pb_access_recommendation_message_t *rec_msg;
171 u_int16_t rec;
172
173 rec_msg = (pb_access_recommendation_message_t*)msg;
174 rec = rec_msg->get_access_recommendation(rec_msg);
175 DBG1(DBG_TNC, "access_recommendation is '%N'",
176 action_recommendation_names, rec);
177 break;
178 }
179 case PB_MSG_REMEDIATION_PARAMETERS:
180 {
181 /* TODO : Remediation parameters message processing */
182 break;
183 }
184 case PB_MSG_ERROR:
185 {
186 pb_error_message_t *err_msg;
187 bool fatal;
188 u_int32_t vendor_id;
189 u_int16_t error_code;
190
191 err_msg = (pb_error_message_t*)msg;
192 fatal = err_msg->get_fatal_flag(err_msg);
193 vendor_id = err_msg->get_vendor_id(err_msg);
194 error_code = err_msg->get_error_code(err_msg);
195
196 if (vendor_id == IETF_VENDOR_ID)
197 {
198 switch (error_code)
199 {
200 case PB_ERROR_INVALID_PARAMETER:
201 case PB_ERROR_UNSUPPORTED_MANDATORY_MESSAGE:
202 DBG1(DBG_TNC, "received %s PB-TNC Error '%N' "
203 "(offset %u bytes)",
204 fatal ? "fatal" : "non-fatal",
205 pb_tnc_error_code_names, error_code,
206 err_msg->get_offset(err_msg));
207 break;
208 case PB_ERROR_VERSION_NOT_SUPPORTED:
209 DBG1(DBG_TNC, "received %s PB-TNC Error '%N' "
210 "caused by bad version 0x%02x",
211 fatal ? "fatal" : "non-fatal",
212 pb_tnc_error_code_names, error_code,
213 err_msg->get_bad_version(err_msg));
214 break;
215 case PB_ERROR_UNEXPECTED_BATCH_TYPE:
216 case PB_ERROR_LOCAL_ERROR:
217 default:
218 DBG1(DBG_TNC, "received %s PB-TNC Error '%N'",
219 fatal ? "fatal" : "non-fatal",
220 pb_tnc_error_code_names, error_code);
221 break;
222 }
223 }
224 else
225 {
226 DBG1(DBG_TNC, "received %s PB-TNC Error (%u) "
227 "with Vendor ID 0x%06x",
228 fatal ? "fatal" : "non-fatal",
229 error_code, vendor_id);
230 }
231 break;
232 }
233 case PB_MSG_LANGUAGE_PREFERENCE:
234 {
235 pb_language_preference_message_t *lang_msg;
236 chunk_t lang;
237
238 lang_msg = (pb_language_preference_message_t*)msg;
239 lang = lang_msg->get_language_preference(lang_msg);
240
241 DBG2(DBG_TNC, "setting language preference '%.*s'",
242 lang.len, lang.ptr);
243 this->recs->set_preferred_language(this->recs, lang);
244 break;
245 }
246 case PB_MSG_REASON_STRING:
247 {
248 pb_reason_string_message_t *reason_msg;
249 chunk_t reason_string, language_code;
250
251 reason_msg = (pb_reason_string_message_t*)msg;
252 reason_string = reason_msg->get_reason_string(reason_msg);
253 language_code = reason_msg->get_language_code(reason_msg);
254 DBG2(DBG_TNC, "reason string: '%.*s", reason_string.len,
255 reason_string.ptr);
256 DBG2(DBG_TNC, "language code: '%.*s", language_code.len,
257 language_code.ptr);
258 break;
259 }
260 default:
261 break;
262 }
263 }
264 enumerator->destroy(enumerator);
265
266 return SUCCESS;
267 }
268
269 METHOD(tls_t, process, status_t,
270 private_tnccs_20_t *this, void *buf, size_t buflen)
271 {
272 chunk_t data;
273 pb_tnc_batch_t *batch;
274 pb_tnc_message_t *msg;
275 pb_tnc_state_t old_state = this->state;
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);
298
299 if (this->state != old_state)
300 {
301 DBG2(DBG_TNC, "PB-TNC state transition from '%N' to '%N'",
302 pb_tnc_state_names, old_state, pb_tnc_state_names, this->state);
303 }
304
305 switch (status)
306 {
307 case SUCCESS:
308 handle_messages(this, batch);
309 if (this->is_server)
310 {
311 charon->imvs->batch_ending(charon->imvs, this->connection_id);
312 }
313 else
314 {
315 charon->imcs->batch_ending(charon->imcs, this->connection_id);
316 }
317 break;
318 case FAILED:
319 this->mutex->lock(this->mutex);
320 if (this->batch)
321 {
322 DBG1(DBG_TNC, "cancelling PB-TNC %N Batch",
323 pb_tnc_batch_type_names, this->batch->get_type(this->batch));
324 this->batch->destroy(this->batch);
325 }
326 this->batch = pb_tnc_batch_create(this->is_server, PB_BATCH_CLOSE);
327 this->mutex->unlock(this->mutex);
328 /* fall through */
329 case VERIFY_ERROR:
330 enumerator = batch->create_error_enumerator(batch);
331 while (enumerator->enumerate(enumerator, &msg))
332 {
333 this->mutex->lock(this->mutex);
334 this->batch->add_message(this->batch, msg->get_ref(msg));
335 this->mutex->unlock(this->mutex);
336 }
337 enumerator->destroy(enumerator);
338 default:
339 break;
340 }
341 batch->destroy(batch);
342
343 return NEED_MORE;
344 }
345
346 /*
347 * Implements the PB-TNC state machine in send direction
348 */
349 static bool state_transition_upon_send(pb_tnc_state_t *state,
350 pb_tnc_batch_type_t batch_type)
351 {
352 switch (*state)
353 {
354 case PB_STATE_INIT:
355 if (batch_type == PB_BATCH_CDATA)
356 {
357 *state = PB_STATE_SERVER_WORKING;
358 break;
359 }
360 if (batch_type == PB_BATCH_SDATA)
361 {
362 *state = PB_STATE_CLIENT_WORKING;
363 break;
364 }
365 if (batch_type == PB_BATCH_CLOSE)
366 {
367 *state = PB_STATE_END;
368 break;
369 }
370 return FALSE;
371 case PB_STATE_SERVER_WORKING:
372 if (batch_type == PB_BATCH_SDATA)
373 {
374 *state = PB_STATE_CLIENT_WORKING;
375 break;
376 }
377 if (batch_type == PB_BATCH_RESULT)
378 {
379 *state = PB_STATE_DECIDED;
380 break;
381 }
382 if (batch_type == PB_BATCH_CRETRY ||
383 batch_type == PB_BATCH_SRETRY)
384 {
385 break;
386 }
387 if (batch_type == PB_BATCH_CLOSE)
388 {
389 *state = PB_STATE_END;
390 break;
391 }
392 return FALSE;
393 case PB_STATE_CLIENT_WORKING:
394 if (batch_type == PB_BATCH_CDATA)
395 {
396 *state = PB_STATE_SERVER_WORKING;
397 break;
398 }
399 if (batch_type == PB_BATCH_CRETRY)
400 {
401 break;
402 }
403 if (batch_type == PB_BATCH_CLOSE)
404 {
405 *state = PB_STATE_END;
406 break;
407 }
408 return FALSE;
409 case PB_STATE_DECIDED:
410 if (batch_type == PB_BATCH_CRETRY ||
411 batch_type == PB_BATCH_SRETRY)
412 {
413 *state = PB_STATE_SERVER_WORKING;
414 break;
415 }
416 if (batch_type == PB_BATCH_CLOSE)
417 {
418 *state = PB_STATE_END;
419 break;
420 }
421 return FALSE;
422 case PB_STATE_END:
423 if (batch_type == PB_BATCH_CLOSE)
424 {
425 break;
426 }
427 return FALSE;
428 }
429 return TRUE;
430 }
431
432 METHOD(tls_t, build, status_t,
433 private_tnccs_20_t *this, void *buf, size_t *buflen, size_t *msglen)
434 {
435 if (!this->is_server && !this->connection_id)
436 {
437 pb_tnc_message_t *msg;
438 char *pref_lang;
439
440 this->connection_id = charon->tnccs->create_connection(charon->tnccs,
441 (tnccs_t*)this, _send_message,
442 &this->request_handshake_retry, NULL);
443 if (!this->connection_id)
444 {
445 return FAILED;
446 }
447
448 /* Create PB-TNC Language Preference Message */
449 pref_lang = charon->imcs->get_preferred_language(charon->imcs);
450 msg = pb_language_preference_message_create(chunk_create(pref_lang,
451 strlen(pref_lang)));
452 this->mutex->lock(this->mutex);
453 this->batch = pb_tnc_batch_create(this->is_server, PB_BATCH_CDATA);
454 this->batch->add_message(this->batch, msg);
455 this->mutex->unlock(this->mutex);
456
457 charon->imcs->notify_connection_change(charon->imcs,
458 this->connection_id, TNC_CONNECTION_STATE_CREATE);
459 charon->imcs->notify_connection_change(charon->imcs,
460 this->connection_id, TNC_CONNECTION_STATE_HANDSHAKE);
461 charon->imcs->begin_handshake(charon->imcs, this->connection_id);
462 }
463
464 if (this->batch)
465 {
466 pb_tnc_batch_type_t batch_type;
467 pb_tnc_state_t old_state;
468 status_t status;
469 chunk_t data;
470
471 batch_type = this->batch->get_type(this->batch);
472 old_state = this->state;
473 this->mutex->lock(this->mutex);
474
475 if (state_transition_upon_send(&this->state, batch_type))
476 {
477 this->batch->build(this->batch);
478 data = this->batch->get_encoding(this->batch);
479 DBG1(DBG_TNC, "sending PB-TNC %N Batch (%d bytes) for Connection ID %u",
480 pb_tnc_batch_type_names, batch_type, data.len,
481 this->connection_id);
482 DBG3(DBG_TNC, "%B", &data);
483
484 *msglen = data.len;
485 *buflen = data.len;
486 memcpy(buf, data.ptr, data.len);
487 status = ALREADY_DONE;
488 }
489 else
490 {
491 DBG1(DBG_TNC, "cancelling unexpected PB-TNC Batch Type: %N",
492 pb_tnc_batch_type_names, batch_type);
493 status = INVALID_STATE;
494 }
495
496 this->batch->destroy(this->batch);
497 this->batch = NULL;
498 this->mutex->unlock(this->mutex);
499
500 if (this->state != old_state)
501 {
502 DBG2(DBG_TNC, "PB-TNC state transition from '%N' to '%N'",
503 pb_tnc_state_names, old_state, pb_tnc_state_names, this->state);
504 }
505 return status;
506 }
507 else
508 {
509 DBG1(DBG_TNC, "no TNCCS Batch to send");
510 return INVALID_STATE;
511 }
512 }
513
514 METHOD(tls_t, is_server, bool,
515 private_tnccs_20_t *this)
516 {
517 return this->is_server;
518 }
519
520 METHOD(tls_t, get_purpose, tls_purpose_t,
521 private_tnccs_20_t *this)
522 {
523 return TLS_PURPOSE_EAP_TNC;
524 }
525
526 METHOD(tls_t, is_complete, bool,
527 private_tnccs_20_t *this)
528 {
529 TNC_IMV_Action_Recommendation rec;
530 TNC_IMV_Evaluation_Result eval;
531
532 if (this->recs && this->recs->have_recommendation(this->recs, &rec, &eval))
533 {
534 DBG2(DBG_TNC, "Final recommendation '%N' and evaluation '%N'",
535 action_recommendation_names, rec, evaluation_result_names, eval);
536
537 return charon->imvs->enforce_recommendation(charon->imvs, rec);
538 }
539 else
540 {
541 return FALSE;
542 }
543 }
544
545 METHOD(tls_t, get_eap_msk, chunk_t,
546 private_tnccs_20_t *this)
547 {
548 return chunk_empty;
549 }
550
551 METHOD(tls_t, destroy, void,
552 private_tnccs_20_t *this)
553 {
554 charon->tnccs->remove_connection(charon->tnccs, this->connection_id);
555 this->mutex->destroy(this->mutex);
556 DESTROY_IF(this->batch);
557 free(this);
558 }
559
560 /**
561 * See header
562 */
563 tls_t *tnccs_20_create(bool is_server)
564 {
565 private_tnccs_20_t *this;
566
567 INIT(this,
568 .public = {
569 .process = _process,
570 .build = _build,
571 .is_server = _is_server,
572 .get_purpose = _get_purpose,
573 .is_complete = _is_complete,
574 .get_eap_msk = _get_eap_msk,
575 .destroy = _destroy,
576 },
577 .is_server = is_server,
578 .state = PB_STATE_INIT,
579 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
580 );
581
582 return &this->public;
583 }