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