134641d46aa18fc4f8d0e7f3d7f7c01043203e1a
[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 /**
120 * Handle a single PB-TNC message according to its type
121 */
122 static void handle_message(private_tnccs_20_t *this, pb_tnc_message_t *msg)
123 {
124 switch (msg->get_type(msg))
125 {
126 case PB_MSG_EXPERIMENTAL:
127 /* for experiments */
128 break;
129 case PB_MSG_PA:
130 {
131 pb_pa_message_t *pa_msg;
132 TNC_MessageType msg_type;
133 u_int32_t vendor_id, subtype;
134 chunk_t msg_body;
135
136 pa_msg = (pb_pa_message_t*)msg;
137 vendor_id = pa_msg->get_vendor_id(pa_msg, &subtype);
138 msg_type = (vendor_id << 8) | (subtype & 0xff);
139 msg_body = pa_msg->get_body(pa_msg);
140
141 DBG2(DBG_TNC, "handling message type 0x%08x", msg_type);
142
143 if (this->is_server)
144 {
145 charon->imvs->receive_message(charon->imvs,
146 this->connection_id, msg_body.ptr, msg_body.len, msg_type);
147 }
148 else
149 {
150 charon->imcs->receive_message(charon->imcs,
151 this->connection_id, msg_body.ptr, msg_body.len,msg_type);
152 }
153 break;
154 }
155 case PB_MSG_ASSESSMENT_RESULT:
156 {
157 pb_assessment_result_message_t *assess_msg;
158 u_int32_t result;
159
160 assess_msg = (pb_assessment_result_message_t*)msg;
161 result = assess_msg->get_assessment_result(assess_msg);
162 DBG1(DBG_TNC, "assessment result is '%N'",
163 evaluation_result_names, result);
164 break;
165 }
166 case PB_MSG_ACCESS_RECOMMENDATION:
167 {
168 pb_access_recommendation_message_t *rec_msg;
169 u_int16_t rec;
170
171 rec_msg = (pb_access_recommendation_message_t*)msg;
172 rec = rec_msg->get_access_recommendation(rec_msg);
173 DBG1(DBG_TNC, "access recommendation is '%N'",
174 action_recommendation_names, rec);
175 break;
176 }
177 case PB_MSG_REMEDIATION_PARAMETERS:
178 {
179 /* TODO : Remediation parameters message processing */
180 break;
181 }
182 case PB_MSG_ERROR:
183 {
184 pb_error_message_t *err_msg;
185 bool fatal;
186 u_int32_t vendor_id;
187 u_int16_t error_code;
188
189 err_msg = (pb_error_message_t*)msg;
190 fatal = err_msg->get_fatal_flag(err_msg);
191 vendor_id = err_msg->get_vendor_id(err_msg);
192 error_code = err_msg->get_error_code(err_msg);
193
194 if (vendor_id == IETF_VENDOR_ID)
195 {
196 switch (error_code)
197 {
198 case PB_ERROR_INVALID_PARAMETER:
199 case PB_ERROR_UNSUPPORTED_MANDATORY_MESSAGE:
200 DBG1(DBG_TNC, "received %s PB-TNC Error '%N' "
201 "(offset %u bytes)",
202 fatal ? "fatal" : "non-fatal",
203 pb_tnc_error_code_names, error_code,
204 err_msg->get_offset(err_msg));
205 break;
206 case PB_ERROR_VERSION_NOT_SUPPORTED:
207 DBG1(DBG_TNC, "received %s PB-TNC Error '%N' "
208 "caused by bad version 0x%02x",
209 fatal ? "fatal" : "non-fatal",
210 pb_tnc_error_code_names, error_code,
211 err_msg->get_bad_version(err_msg));
212 break;
213 case PB_ERROR_UNEXPECTED_BATCH_TYPE:
214 case PB_ERROR_LOCAL_ERROR:
215 default:
216 DBG1(DBG_TNC, "received %s PB-TNC Error '%N'",
217 fatal ? "fatal" : "non-fatal",
218 pb_tnc_error_code_names, error_code);
219 break;
220 }
221 }
222 else
223 {
224 DBG1(DBG_TNC, "received %s PB-TNC Error (%u) "
225 "with Vendor ID 0x%06x",
226 fatal ? "fatal" : "non-fatal",
227 error_code, vendor_id);
228 }
229 break;
230 }
231 case PB_MSG_LANGUAGE_PREFERENCE:
232 {
233 pb_language_preference_message_t *lang_msg;
234 chunk_t lang;
235
236 lang_msg = (pb_language_preference_message_t*)msg;
237 lang = lang_msg->get_language_preference(lang_msg);
238
239 DBG2(DBG_TNC, "setting language preference to '%.*s'",
240 lang.len, lang.ptr);
241 this->recs->set_preferred_language(this->recs, lang);
242 break;
243 }
244 case PB_MSG_REASON_STRING:
245 {
246 pb_reason_string_message_t *reason_msg;
247 chunk_t reason_string, language_code;
248
249 reason_msg = (pb_reason_string_message_t*)msg;
250 reason_string = reason_msg->get_reason_string(reason_msg);
251 language_code = reason_msg->get_language_code(reason_msg);
252 DBG2(DBG_TNC, "reason string is '%.*s", reason_string.len,
253 reason_string.ptr);
254 DBG2(DBG_TNC, "language code is '%.*s", language_code.len,
255 language_code.ptr);
256 break;
257 }
258 default:
259 break;
260 }
261 }
262
263 /**
264 * Build a CRETRY or SRETRY Batch
265 */
266 static void build_retry_batch(private_tnccs_20_t *this)
267 {
268 if (this->batch)
269 {
270 DBG1(DBG_TNC, "cancelling PB-TNC %N Batch",
271 pb_tnc_batch_type_names, this->batch->get_type(this->batch));
272 this->batch->destroy(this->batch);
273 }
274 this->batch = pb_tnc_batch_create(this->is_server,
275 this->is_server ? PB_BATCH_SRETRY : PB_BATCH_CRETRY);
276 }
277
278 METHOD(tls_t, process, status_t,
279 private_tnccs_20_t *this, void *buf, size_t buflen)
280 {
281 chunk_t data;
282 pb_tnc_batch_t *batch;
283 pb_tnc_message_t *msg;
284 enumerator_t *enumerator;
285 status_t status;
286
287 if (this->is_server && !this->connection_id)
288 {
289 this->connection_id = charon->tnccs->create_connection(charon->tnccs,
290 (tnccs_t*)this, _send_message,
291 &this->request_handshake_retry, &this->recs);
292 if (!this->connection_id)
293 {
294 return FAILED;
295 }
296 charon->imvs->notify_connection_change(charon->imvs,
297 this->connection_id, TNC_CONNECTION_STATE_CREATE);
298 }
299
300 data = chunk_create(buf, buflen);
301 DBG1(DBG_TNC, "received TNCCS Batch (%u bytes) for Connection ID %u",
302 data.len, this->connection_id);
303 DBG3(DBG_TNC, "%B", &data);
304 batch = pb_tnc_batch_create_from_data(this->is_server, data);
305 status = batch->process(batch, this->state_machine);
306
307 if (status != FAILED)
308 {
309 enumerator_t *enumerator;
310 pb_tnc_message_t *msg;
311 pb_tnc_batch_type_t batch_type;
312 bool empty = TRUE;
313
314 batch_type = batch->get_type(batch);
315
316 if (batch_type == PB_BATCH_CRETRY)
317 {
318 /* Send an SRETRY Batch in response */
319 this->mutex->lock(this->mutex);
320 build_retry_batch(this);
321 this->mutex->unlock(this->mutex);
322 }
323 else if (batch_type == PB_BATCH_SRETRY)
324 {
325 /* Restart the measurements */
326 charon->imcs->notify_connection_change(charon->imcs,
327 this->connection_id, TNC_CONNECTION_STATE_HANDSHAKE);
328 charon->imcs->begin_handshake(charon->imcs, this->connection_id);
329 }
330
331 enumerator = batch->create_msg_enumerator(batch);
332 while (enumerator->enumerate(enumerator, &msg))
333 {
334 handle_message(this, msg);
335 empty = FALSE;
336 }
337 enumerator->destroy(enumerator);
338
339 /* received an empty CLOSE Batch from PB-TNC Client */
340 if (this->is_server && batch_type == PB_BATCH_CLOSE && empty)
341 {
342 batch->destroy(batch);
343 return SUCCESS;
344 }
345
346 if (this->is_server)
347 {
348 charon->imvs->batch_ending(charon->imvs, this->connection_id);
349 }
350 else
351 {
352 charon->imcs->batch_ending(charon->imcs, this->connection_id);
353 }
354 }
355
356 switch (status)
357 {
358 case FAILED:
359 this->mutex->lock(this->mutex);
360 if (this->batch)
361 {
362 DBG1(DBG_TNC, "cancelling PB-TNC %N Batch",
363 pb_tnc_batch_type_names, this->batch->get_type(this->batch));
364 this->batch->destroy(this->batch);
365 }
366 this->batch = pb_tnc_batch_create(this->is_server, PB_BATCH_CLOSE);
367 this->mutex->unlock(this->mutex);
368 /* fall through to add error messages to outbound batch */
369 case VERIFY_ERROR:
370 enumerator = batch->create_error_enumerator(batch);
371 while (enumerator->enumerate(enumerator, &msg))
372 {
373 this->mutex->lock(this->mutex);
374 this->batch->add_message(this->batch, msg->get_ref(msg));
375 this->mutex->unlock(this->mutex);
376 }
377 enumerator->destroy(enumerator);
378 break;
379 case SUCCESS:
380 default:
381 break;
382 }
383 batch->destroy(batch);
384
385 return NEED_MORE;
386 }
387
388 /**
389 * Build a RESULT Batch if a final recommendation is available
390 */
391 static void check_and_build_recommendation(private_tnccs_20_t *this)
392 {
393 TNC_IMV_Action_Recommendation rec;
394 TNC_IMV_Evaluation_Result eval;
395 TNC_IMVID id;
396 chunk_t reason, language;
397 enumerator_t *enumerator;
398 pb_tnc_message_t *msg;
399
400 if (!this->recs->have_recommendation(this->recs, &rec, &eval))
401 {
402 charon->imvs->solicit_recommendation(charon->imvs, this->connection_id);
403 }
404 if (this->recs->have_recommendation(this->recs, &rec, &eval))
405 {
406 this->batch = pb_tnc_batch_create(this->is_server, PB_BATCH_RESULT);
407
408 msg = pb_assessment_result_message_create(eval);
409 this->batch->add_message(this->batch, msg);
410
411 msg = pb_access_recommendation_message_create(rec);
412 this->batch->add_message(this->batch, msg);
413
414 enumerator = this->recs->create_reason_enumerator(this->recs);
415 while (enumerator->enumerate(enumerator, &id, &reason, &language))
416 {
417 msg = pb_reason_string_message_create(reason, language);
418 this->batch->add_message(this->batch, msg);
419 }
420 enumerator->destroy(enumerator);
421 }
422 }
423
424 METHOD(tls_t, build, status_t,
425 private_tnccs_20_t *this, void *buf, size_t *buflen, size_t *msglen)
426 {
427 status_t status;
428
429 /* Initialize the connection */
430 if (!this->is_server && !this->connection_id)
431 {
432 pb_tnc_message_t *msg;
433 char *pref_lang;
434
435 this->connection_id = charon->tnccs->create_connection(charon->tnccs,
436 (tnccs_t*)this, _send_message,
437 &this->request_handshake_retry, NULL);
438 if (!this->connection_id)
439 {
440 return FAILED;
441 }
442
443 /* Create PB-TNC Language Preference Message */
444 pref_lang = charon->imcs->get_preferred_language(charon->imcs);
445 msg = pb_language_preference_message_create(chunk_create(pref_lang,
446 strlen(pref_lang)));
447 this->mutex->lock(this->mutex);
448 this->batch = pb_tnc_batch_create(this->is_server, PB_BATCH_CDATA);
449 this->batch->add_message(this->batch, msg);
450 this->mutex->unlock(this->mutex);
451
452 charon->imcs->notify_connection_change(charon->imcs,
453 this->connection_id, TNC_CONNECTION_STATE_CREATE);
454 charon->imcs->notify_connection_change(charon->imcs,
455 this->connection_id, TNC_CONNECTION_STATE_HANDSHAKE);
456 charon->imcs->begin_handshake(charon->imcs, this->connection_id);
457 }
458
459 /* Do not allow any asynchronous IMCs or IMVs to add additional messages */
460 this->mutex->lock(this->mutex);
461
462 if (this->request_handshake_retry)
463 {
464 build_retry_batch(this);
465
466 /* Reset the flag for the next handshake retry request */
467 this->request_handshake_retry = FALSE;
468 }
469
470 if (!this->batch)
471 {
472 pb_tnc_state_t state;
473
474 state = this->state_machine->get_state(this->state_machine);
475 if (this->is_server)
476 {
477 if (state == PB_STATE_SERVER_WORKING)
478 {
479 check_and_build_recommendation(this);
480 }
481 }
482 else
483 {
484 /**
485 * if the DECIDED state has been reached and no CRETRY is under way
486 * or if a CLOSE batch with error messages has been received,
487 * reply with an empty CLOSE batch.
488 */
489 if (state == PB_STATE_DECIDED || state == PB_STATE_END)
490 {
491 this->batch = pb_tnc_batch_create(this->is_server, PB_BATCH_CLOSE);
492 }
493 }
494 }
495
496 if (this->batch)
497 {
498 pb_tnc_batch_type_t batch_type;
499 chunk_t data;
500
501 batch_type = this->batch->get_type(this->batch);
502
503 if (this->state_machine->send_batch(this->state_machine, batch_type))
504 {
505 this->batch->build(this->batch);
506 data = this->batch->get_encoding(this->batch);
507 DBG1(DBG_TNC, "sending PB-TNC %N Batch (%d bytes) for Connection ID %u",
508 pb_tnc_batch_type_names, batch_type, data.len,
509 this->connection_id);
510 DBG3(DBG_TNC, "%B", &data);
511
512 *msglen = data.len;
513 *buflen = data.len;
514 memcpy(buf, data.ptr, data.len);
515 status = ALREADY_DONE;
516 }
517 else
518 {
519 DBG1(DBG_TNC, "cancelling unexpected PB-TNC Batch Type: %N",
520 pb_tnc_batch_type_names, batch_type);
521 status = INVALID_STATE;
522 }
523
524 this->batch->destroy(this->batch);
525 this->batch = NULL;
526 }
527 else
528 {
529 DBG1(DBG_TNC, "no TNCCS Batch to send");
530 status = INVALID_STATE;
531 }
532 this->mutex->unlock(this->mutex);
533
534 return status;
535 }
536
537 METHOD(tls_t, is_server, bool,
538 private_tnccs_20_t *this)
539 {
540 return this->is_server;
541 }
542
543 METHOD(tls_t, get_purpose, tls_purpose_t,
544 private_tnccs_20_t *this)
545 {
546 return TLS_PURPOSE_EAP_TNC;
547 }
548
549 METHOD(tls_t, is_complete, bool,
550 private_tnccs_20_t *this)
551 {
552 TNC_IMV_Action_Recommendation rec;
553 TNC_IMV_Evaluation_Result eval;
554
555 if (this->recs && this->recs->have_recommendation(this->recs, &rec, &eval))
556 {
557 DBG2(DBG_TNC, "Final recommendation is '%N' and evaluation is '%N'",
558 action_recommendation_names, rec, evaluation_result_names, eval);
559
560 return charon->imvs->enforce_recommendation(charon->imvs, rec);
561 }
562 else
563 {
564 return FALSE;
565 }
566 }
567
568 METHOD(tls_t, get_eap_msk, chunk_t,
569 private_tnccs_20_t *this)
570 {
571 return chunk_empty;
572 }
573
574 METHOD(tls_t, destroy, void,
575 private_tnccs_20_t *this)
576 {
577 charon->tnccs->remove_connection(charon->tnccs, this->connection_id);
578 this->state_machine->destroy(this->state_machine);
579 this->mutex->destroy(this->mutex);
580 DESTROY_IF(this->batch);
581 free(this);
582 }
583
584 /**
585 * See header
586 */
587 tls_t *tnccs_20_create(bool is_server)
588 {
589 private_tnccs_20_t *this;
590
591 INIT(this,
592 .public = {
593 .process = _process,
594 .build = _build,
595 .is_server = _is_server,
596 .get_purpose = _get_purpose,
597 .is_complete = _is_complete,
598 .get_eap_msk = _get_eap_msk,
599 .destroy = _destroy,
600 },
601 .is_server = is_server,
602 .state_machine = pb_tnc_state_machine_create(is_server),
603 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
604 );
605
606 return &this->public;
607 }