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