b19a048fedb19b916d45cad1b93cb5d04dabaa01
[strongswan.git] / src / libtnccs / plugins / tnccs_11 / tnccs_11.c
1 /*
2 * Copyright (C) 2010-2013 Andreas Steffen
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 "tnccs_11.h"
17 #include "batch/tnccs_batch.h"
18 #include "messages/tnccs_msg.h"
19 #include "messages/imc_imv_msg.h"
20 #include "messages/tnccs_error_msg.h"
21 #include "messages/tnccs_preferred_language_msg.h"
22 #include "messages/tnccs_reason_strings_msg.h"
23 #include "messages/tnccs_recommendation_msg.h"
24
25 #include <tncif_names.h>
26 #include <tncif_pa_subtypes.h>
27
28 #include <tnc/tnc.h>
29 #include <tnc/imc/imc_manager.h>
30 #include <tnc/imv/imv_manager.h>
31 #include <tnc/tnccs/tnccs.h>
32 #include <tnc/tnccs/tnccs_manager.h>
33
34 #include <utils/debug.h>
35 #include <threading/mutex.h>
36
37 typedef struct private_tnccs_11_t private_tnccs_11_t;
38
39 /**
40 * Private data of a tnccs_11_t object.
41 */
42 struct private_tnccs_11_t {
43
44 /**
45 * Public tnccs_t interface.
46 */
47 tnccs_t public;
48
49 /**
50 * TNCC if TRUE, TNCS if FALSE
51 */
52 bool is_server;
53
54 /**
55 * Server identity
56 */
57 identification_t *server;
58
59 /**
60 * Client identity
61 */
62 identification_t *peer;
63
64 /**
65 * Underlying TNC IF-T transport protocol
66 */
67 tnc_ift_type_t transport;
68
69 /**
70 * Type of TNC client authentication
71 */
72 u_int32_t auth_type;
73
74 /**
75 * Connection ID assigned to this TNCCS connection
76 */
77 TNC_ConnectionID connection_id;
78
79 /**
80 * Last TNCCS batch ID
81 */
82 int batch_id;
83
84 /**
85 * TNCCS batch being constructed
86 */
87 tnccs_batch_t *batch;
88
89 /**
90 * Maximum PA-TNC message size
91 */
92 size_t max_msg_len;
93
94 /**
95 * Mutex locking the batch in construction
96 */
97 mutex_t *mutex;
98
99 /**
100 * Flag set while processing
101 */
102 bool fatal_error;
103
104 /**
105 * Flag set by TNCCS-Recommendation message
106 */
107 bool delete_state;
108
109 /**
110 * SendMessage() by IMC/IMV only allowed if flag is set
111 */
112 bool send_msg;
113
114 /**
115 * Flag set by IMC/IMV RequestHandshakeRetry() function
116 */
117 bool request_handshake_retry;
118
119 /**
120 * Set of IMV recommendations (TNC Server only)
121 */
122 recommendations_t *recs;
123
124 };
125
126 METHOD(tnccs_t, send_msg, TNC_Result,
127 private_tnccs_11_t* this, TNC_IMCID imc_id, TNC_IMVID imv_id,
128 TNC_UInt32 msg_flags,
129 TNC_BufferReference msg,
130 TNC_UInt32 msg_len,
131 TNC_VendorID msg_vid,
132 TNC_MessageSubtype msg_subtype)
133 {
134 tnccs_msg_t *tnccs_msg;
135 TNC_MessageType msg_type;
136 enum_name_t *pa_subtype_names;
137
138 if (!this->send_msg)
139 {
140 DBG1(DBG_TNC, "%s %u not allowed to call SendMessage()",
141 this->is_server ? "IMV" : "IMC",
142 this->is_server ? imv_id : imc_id);
143 return TNC_RESULT_ILLEGAL_OPERATION;
144 }
145 if (msg_vid > TNC_VENDORID_ANY || msg_subtype > TNC_SUBTYPE_ANY)
146 {
147 return TNC_RESULT_NO_LONG_MESSAGE_TYPES;
148 }
149 msg_type = (msg_vid << 8) | msg_subtype;
150
151 pa_subtype_names = get_pa_subtype_names(msg_vid);
152 if (pa_subtype_names)
153 {
154 DBG2(DBG_TNC, "creating IMC-IMV message type '%N/%N' 0x%06x/0x%02x",
155 pen_names, msg_vid, pa_subtype_names, msg_subtype,
156 msg_vid, msg_subtype);
157 }
158 else
159 {
160 DBG2(DBG_TNC, "creating IMC-IMV message type '%N' 0x%06x/0x%02x",
161 pen_names, msg_vid, msg_vid, msg_subtype);
162 }
163 tnccs_msg = imc_imv_msg_create(msg_type, chunk_create(msg, msg_len));
164
165 /* adding an IMC-IMV Message to TNCCS batch */
166 this->mutex->lock(this->mutex);
167 if (!this->batch)
168 {
169 this->batch = tnccs_batch_create(this->is_server, ++this->batch_id);
170 }
171 this->batch->add_msg(this->batch, tnccs_msg);
172 this->mutex->unlock(this->mutex);
173 return TNC_RESULT_SUCCESS;
174 }
175
176 /**
177 * Handle a single TNCCS message according to its type
178 */
179 static void handle_message(private_tnccs_11_t *this, tnccs_msg_t *msg)
180 {
181 switch (msg->get_type(msg))
182 {
183 case IMC_IMV_MSG:
184 {
185 imc_imv_msg_t *imc_imv_msg;
186 TNC_MessageType msg_type;
187 chunk_t msg_body;
188 u_int32_t msg_vid, msg_subtype;
189 enum_name_t *pa_subtype_names;
190
191 imc_imv_msg = (imc_imv_msg_t*)msg;
192 msg_type = imc_imv_msg->get_msg_type(imc_imv_msg);
193 msg_body = imc_imv_msg->get_msg_body(imc_imv_msg);
194 msg_vid = (msg_type >> 8) & TNC_VENDORID_ANY;
195 msg_subtype = msg_type & TNC_SUBTYPE_ANY;
196
197 pa_subtype_names = get_pa_subtype_names(msg_vid);
198 if (pa_subtype_names)
199 {
200 DBG2(DBG_TNC, "handling IMC-IMV message type '%N/%N' 0x%06x/0x%02x",
201 pen_names, msg_vid, pa_subtype_names, msg_subtype,
202 msg_vid, msg_subtype);
203 }
204 else
205 {
206 DBG2(DBG_TNC, "handling IMC-IMV message type '%N' 0x%06x/0x%02x",
207 pen_names, msg_vid, msg_vid, msg_subtype);
208 }
209
210 this->send_msg = TRUE;
211 if (this->is_server)
212 {
213 tnc->imvs->receive_message(tnc->imvs, this->connection_id,
214 FALSE, msg_body.ptr, msg_body.len,
215 msg_vid, msg_subtype, 0, TNC_IMVID_ANY);
216 }
217 else
218 {
219 tnc->imcs->receive_message(tnc->imcs, this->connection_id,
220 FALSE, msg_body.ptr, msg_body.len,
221 msg_vid, msg_subtype, 0, TNC_IMCID_ANY);
222 }
223 this->send_msg = FALSE;
224 break;
225 }
226 case TNCCS_MSG_RECOMMENDATION:
227 {
228 tnccs_recommendation_msg_t *rec_msg;
229 TNC_IMV_Action_Recommendation rec;
230 TNC_ConnectionState state = TNC_CONNECTION_STATE_ACCESS_NONE;
231
232 rec_msg = (tnccs_recommendation_msg_t*)msg;
233 rec = rec_msg->get_recommendation(rec_msg);
234 if (this->is_server)
235 {
236 DBG1(DBG_TNC, "ignoring NCCS-Recommendation message from "
237 " TNC client");
238 break;
239 }
240 DBG1(DBG_TNC, "TNC recommendation is '%N'",
241 TNC_IMV_Action_Recommendation_names, rec);
242 switch (rec)
243 {
244 case TNC_IMV_ACTION_RECOMMENDATION_ALLOW:
245 state = TNC_CONNECTION_STATE_ACCESS_ALLOWED;
246 break;
247 case TNC_IMV_ACTION_RECOMMENDATION_ISOLATE:
248 state = TNC_CONNECTION_STATE_ACCESS_ISOLATED;
249 break;
250 case TNC_IMV_ACTION_RECOMMENDATION_NO_ACCESS:
251 default:
252 state = TNC_CONNECTION_STATE_ACCESS_NONE;
253 }
254 tnc->imcs->notify_connection_change(tnc->imcs, this->connection_id,
255 state);
256 this->delete_state = TRUE;
257 break;
258 }
259 case TNCCS_MSG_ERROR:
260 {
261 tnccs_error_msg_t *err_msg;
262 tnccs_error_type_t error_type;
263 char *error_msg;
264
265 err_msg = (tnccs_error_msg_t*)msg;
266 error_msg = err_msg->get_message(err_msg, &error_type);
267 DBG1(DBG_TNC, "received '%N' TNCCS-Error: %s",
268 tnccs_error_type_names, error_type, error_msg);
269
270 /* we assume that all errors are fatal */
271 this->fatal_error = TRUE;
272 break;
273 }
274 case TNCCS_MSG_PREFERRED_LANGUAGE:
275 {
276 tnccs_preferred_language_msg_t *lang_msg;
277 char *lang;
278
279 lang_msg = (tnccs_preferred_language_msg_t*)msg;
280 lang = lang_msg->get_preferred_language(lang_msg);
281
282 DBG2(DBG_TNC, "setting preferred language to '%s'", lang);
283 this->recs->set_preferred_language(this->recs,
284 chunk_create(lang, strlen(lang)));
285 break;
286 }
287 case TNCCS_MSG_REASON_STRINGS:
288 {
289 tnccs_reason_strings_msg_t *reason_msg;
290 chunk_t reason_string, reason_lang;
291
292 reason_msg = (tnccs_reason_strings_msg_t*)msg;
293 reason_string = reason_msg->get_reason(reason_msg, &reason_lang);
294 DBG2(DBG_TNC, "reason string is '%.*s'", (int)reason_string.len,
295 reason_string.ptr);
296 DBG2(DBG_TNC, "language code is '%.*s'", (int)reason_lang.len,
297 reason_lang.ptr);
298 break;
299 }
300 default:
301 break;
302 }
303 }
304
305 METHOD(tls_t, process, status_t,
306 private_tnccs_11_t *this, void *buf, size_t buflen)
307 {
308 chunk_t data;
309 tnccs_batch_t *batch;
310 tnccs_msg_t *msg;
311 enumerator_t *enumerator;
312 status_t status;
313
314 if (this->is_server && !this->connection_id)
315 {
316 this->connection_id = tnc->tnccs->create_connection(tnc->tnccs,
317 TNCCS_1_1, (tnccs_t*)this, _send_msg,
318 &this->request_handshake_retry,
319 this->max_msg_len, &this->recs);
320 if (!this->connection_id)
321 {
322 return FAILED;
323 }
324 tnc->imvs->notify_connection_change(tnc->imvs, this->connection_id,
325 TNC_CONNECTION_STATE_CREATE);
326 tnc->imvs->notify_connection_change(tnc->imvs, this->connection_id,
327 TNC_CONNECTION_STATE_HANDSHAKE);
328 }
329
330 data = chunk_create(buf, buflen);
331 DBG1(DBG_TNC, "received TNCCS Batch (%u bytes) for Connection ID %u",
332 data.len, this->connection_id);
333 DBG3(DBG_TNC, "%.*s", (int)data.len, data.ptr);
334 batch = tnccs_batch_create_from_data(this->is_server, ++this->batch_id, data);
335 status = batch->process(batch);
336
337 if (status == FAILED)
338 {
339 this->fatal_error = TRUE;
340 this->mutex->lock(this->mutex);
341 if (this->batch)
342 {
343 DBG1(DBG_TNC, "cancelling TNCCS batch");
344 this->batch->destroy(this->batch);
345 this->batch_id--;
346 }
347 this->batch = tnccs_batch_create(this->is_server, ++this->batch_id);
348
349 /* add error messages to outbound batch */
350 enumerator = batch->create_error_enumerator(batch);
351 while (enumerator->enumerate(enumerator, &msg))
352 {
353 this->batch->add_msg(this->batch, msg->get_ref(msg));
354 }
355 enumerator->destroy(enumerator);
356 this->mutex->unlock(this->mutex);
357 }
358 else
359 {
360 enumerator = batch->create_msg_enumerator(batch);
361 while (enumerator->enumerate(enumerator, &msg))
362 {
363 handle_message(this, msg);
364 }
365 enumerator->destroy(enumerator);
366
367 /* received any TNCCS-Error messages */
368 if (this->fatal_error)
369 {
370 DBG1(DBG_TNC, "a fatal TNCCS-Error occurred, terminating connection");
371 batch->destroy(batch);
372 return FAILED;
373 }
374
375 this->send_msg = TRUE;
376 if (this->is_server)
377 {
378 tnc->imvs->batch_ending(tnc->imvs, this->connection_id);
379 }
380 else
381 {
382 tnc->imcs->batch_ending(tnc->imcs, this->connection_id);
383 }
384 this->send_msg = FALSE;
385 }
386 batch->destroy(batch);
387
388 return NEED_MORE;
389 }
390
391 /**
392 * Add a recommendation message if a final recommendation is available
393 */
394 static void check_and_build_recommendation(private_tnccs_11_t *this)
395 {
396 TNC_IMV_Action_Recommendation rec;
397 TNC_IMV_Evaluation_Result eval;
398 TNC_IMVID id;
399 chunk_t reason, language;
400 enumerator_t *enumerator;
401 tnccs_msg_t *msg;
402
403 if (!this->recs->have_recommendation(this->recs, &rec, &eval))
404 {
405 tnc->imvs->solicit_recommendation(tnc->imvs, this->connection_id);
406 }
407 if (this->recs->have_recommendation(this->recs, &rec, &eval))
408 {
409 if (!this->batch)
410 {
411 this->batch = tnccs_batch_create(this->is_server, ++this->batch_id);
412 }
413
414 msg = tnccs_recommendation_msg_create(rec);
415 this->batch->add_msg(this->batch, msg);
416
417 /* currently we just send the first Reason String */
418 enumerator = this->recs->create_reason_enumerator(this->recs);
419 if (enumerator->enumerate(enumerator, &id, &reason, &language))
420 {
421 msg = tnccs_reason_strings_msg_create(reason, language);
422 this->batch->add_msg(this->batch, msg);
423 }
424 enumerator->destroy(enumerator);
425
426 /* we have reache the final state */
427 this->delete_state = TRUE;
428 }
429 }
430
431 METHOD(tls_t, build, status_t,
432 private_tnccs_11_t *this, void *buf, size_t *buflen, size_t *msglen)
433 {
434 status_t status;
435
436 /* Initialize the connection */
437 if (!this->is_server && !this->connection_id)
438 {
439 tnccs_msg_t *msg;
440 char *pref_lang;
441
442 this->connection_id = tnc->tnccs->create_connection(tnc->tnccs,
443 TNCCS_1_1, (tnccs_t*)this, _send_msg,
444 &this->request_handshake_retry,
445 this->max_msg_len, NULL);
446 if (!this->connection_id)
447 {
448 return FAILED;
449 }
450
451 /* Create TNCCS-PreferredLanguage message */
452 pref_lang = tnc->imcs->get_preferred_language(tnc->imcs);
453 msg = tnccs_preferred_language_msg_create(pref_lang);
454 this->mutex->lock(this->mutex);
455 this->batch = tnccs_batch_create(this->is_server, ++this->batch_id);
456 this->batch->add_msg(this->batch, msg);
457 this->mutex->unlock(this->mutex);
458
459 tnc->imcs->notify_connection_change(tnc->imcs, this->connection_id,
460 TNC_CONNECTION_STATE_CREATE);
461 tnc->imcs->notify_connection_change(tnc->imcs, this->connection_id,
462 TNC_CONNECTION_STATE_HANDSHAKE);
463 this->send_msg = TRUE;
464 tnc->imcs->begin_handshake(tnc->imcs, this->connection_id);
465 this->send_msg = FALSE;
466 }
467
468 /* Do not allow any asynchronous IMCs or IMVs to add additional messages */
469 this->mutex->lock(this->mutex);
470
471 if (this->recs && !this->delete_state &&
472 (!this->batch || this->fatal_error))
473 {
474 check_and_build_recommendation(this);
475 }
476
477 if (this->batch)
478 {
479 chunk_t data;
480
481 this->batch->build(this->batch);
482 data = this->batch->get_encoding(this->batch);
483 DBG1(DBG_TNC, "sending TNCCS Batch (%d bytes) for Connection ID %u",
484 data.len, this->connection_id);
485 DBG3(DBG_TNC, "%.*s", (int)data.len, data.ptr);
486 *msglen = 0;
487
488 if (data.len > *buflen)
489 {
490 DBG1(DBG_TNC, "fragmentation of TNCCS batch not supported yet");
491 }
492 else
493 {
494 *buflen = data.len;
495 }
496 memcpy(buf, data.ptr, *buflen);
497 this->batch->destroy(this->batch);
498 this->batch = NULL;
499 status = ALREADY_DONE;
500 }
501 else
502 {
503 DBG1(DBG_TNC, "no TNCCS Batch to send");
504 status = INVALID_STATE;
505 }
506 this->mutex->unlock(this->mutex);
507
508 return status;
509 }
510
511 METHOD(tls_t, is_server, bool,
512 private_tnccs_11_t *this)
513 {
514 return this->is_server;
515 }
516
517 METHOD(tls_t, get_server_id, identification_t*,
518 private_tnccs_11_t *this)
519 {
520 return this->server;
521 }
522
523 METHOD(tls_t, get_peer_id, identification_t*,
524 private_tnccs_11_t *this)
525 {
526 return this->peer;
527 }
528
529 METHOD(tls_t, get_purpose, tls_purpose_t,
530 private_tnccs_11_t *this)
531 {
532 return TLS_PURPOSE_EAP_TNC;
533 }
534
535 METHOD(tls_t, is_complete, bool,
536 private_tnccs_11_t *this)
537 {
538 TNC_IMV_Action_Recommendation rec;
539 TNC_IMV_Evaluation_Result eval;
540
541 if (this->recs && this->recs->have_recommendation(this->recs, &rec, &eval))
542 {
543 return tnc->imvs->enforce_recommendation(tnc->imvs, rec, eval);
544 }
545 else
546 {
547 return FALSE;
548 }
549 }
550
551 METHOD(tls_t, get_eap_msk, chunk_t,
552 private_tnccs_11_t *this)
553 {
554 return chunk_empty;
555 }
556
557 METHOD(tls_t, destroy, void,
558 private_tnccs_11_t *this)
559 {
560 tnc->tnccs->remove_connection(tnc->tnccs, this->connection_id,
561 this->is_server);
562 this->server->destroy(this->server);
563 this->peer->destroy(this->peer);
564 this->mutex->destroy(this->mutex);
565 DESTROY_IF(this->batch);
566 free(this);
567 }
568
569 METHOD(tnccs_t, get_transport, tnc_ift_type_t,
570 private_tnccs_11_t *this)
571 {
572 return this->transport;
573 }
574
575 METHOD(tnccs_t, set_transport, void,
576 private_tnccs_11_t *this, tnc_ift_type_t transport)
577 {
578 this->transport = transport;
579 }
580
581 METHOD(tnccs_t, get_auth_type, u_int32_t,
582 private_tnccs_11_t *this)
583 {
584 return this->auth_type;
585 }
586
587 METHOD(tnccs_t, set_auth_type, void,
588 private_tnccs_11_t *this, u_int32_t auth_type)
589 {
590 this->auth_type = auth_type;
591 }
592
593 /**
594 * See header
595 */
596 tnccs_t* tnccs_11_create(bool is_server,
597 identification_t *server,
598 identification_t *peer,
599 tnc_ift_type_t transport)
600 {
601 private_tnccs_11_t *this;
602
603 INIT(this,
604 .public = {
605 .tls = {
606 .process = _process,
607 .build = _build,
608 .is_server = _is_server,
609 .get_server_id = _get_server_id,
610 .get_peer_id = _get_peer_id,
611 .get_purpose = _get_purpose,
612 .is_complete = _is_complete,
613 .get_eap_msk = _get_eap_msk,
614 .destroy = _destroy,
615 },
616 .get_transport = _get_transport,
617 .set_transport = _set_transport,
618 .get_auth_type = _get_auth_type,
619 .set_auth_type = _set_auth_type,
620 },
621 .is_server = is_server,
622 .server = server->clone(server),
623 .peer = peer->clone(peer),
624 .transport = transport,
625 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
626 .max_msg_len = lib->settings->get_int(lib->settings,
627 "libtnccs.plugins.tnccs-11.max_message_size", 45000),
628 );
629
630 return &this->public;
631 }