implemented IF-IMC/IMV 1.3 attributes
[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 msg_vid, msg_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 msg_vid = (msg_type >> 8) & TNC_VENDORID_ANY;
170 msg_subtype = msg_type & TNC_SUBTYPE_ANY;
171
172 pa_subtype_names = get_pa_subtype_names(msg_vid);
173 if (pa_subtype_names)
174 {
175 DBG2(DBG_TNC, "handling IMC-IMV message type '%N/%N' 0x%06x/0x%02x",
176 pen_names, msg_vid, pa_subtype_names, msg_subtype,
177 msg_vid, msg_subtype);
178 }
179 else
180 {
181 DBG2(DBG_TNC, "handling IMC-IMV message type '%N' 0x%06x/0x%02x",
182 pen_names, msg_vid, msg_vid, msg_subtype);
183 }
184
185 this->send_msg = TRUE;
186 if (this->is_server)
187 {
188 tnc->imvs->receive_message(tnc->imvs, this->connection_id,
189 FALSE, msg_body.ptr, msg_body.len,
190 msg_vid, msg_subtype, 0, TNC_IMVID_ANY);
191 }
192 else
193 {
194 tnc->imcs->receive_message(tnc->imcs, this->connection_id,
195 FALSE, msg_body.ptr, msg_body.len,
196 msg_vid, msg_subtype, 0, TNC_IMCID_ANY);
197 }
198 this->send_msg = FALSE;
199 break;
200 }
201 case TNCCS_MSG_RECOMMENDATION:
202 {
203 tnccs_recommendation_msg_t *rec_msg;
204 TNC_IMV_Action_Recommendation rec;
205 TNC_ConnectionState state = TNC_CONNECTION_STATE_ACCESS_NONE;
206
207 rec_msg = (tnccs_recommendation_msg_t*)msg;
208 rec = rec_msg->get_recommendation(rec_msg);
209 if (this->is_server)
210 {
211 DBG1(DBG_TNC, "ignoring NCCS-Recommendation message from "
212 " TNC client");
213 break;
214 }
215 DBG1(DBG_TNC, "TNC recommendation is '%N'",
216 TNC_IMV_Action_Recommendation_names, rec);
217 switch (rec)
218 {
219 case TNC_IMV_ACTION_RECOMMENDATION_ALLOW:
220 state = TNC_CONNECTION_STATE_ACCESS_ALLOWED;
221 break;
222 case TNC_IMV_ACTION_RECOMMENDATION_ISOLATE:
223 state = TNC_CONNECTION_STATE_ACCESS_ISOLATED;
224 break;
225 case TNC_IMV_ACTION_RECOMMENDATION_NO_ACCESS:
226 default:
227 state = TNC_CONNECTION_STATE_ACCESS_NONE;
228 }
229 tnc->imcs->notify_connection_change(tnc->imcs, this->connection_id,
230 state);
231 this->delete_state = TRUE;
232 break;
233 }
234 case TNCCS_MSG_ERROR:
235 {
236 tnccs_error_msg_t *err_msg;
237 tnccs_error_type_t error_type;
238 char *error_msg;
239
240 err_msg = (tnccs_error_msg_t*)msg;
241 error_msg = err_msg->get_message(err_msg, &error_type);
242 DBG1(DBG_TNC, "received '%N' TNCCS-Error: %s",
243 tnccs_error_type_names, error_type, error_msg);
244
245 /* we assume that all errors are fatal */
246 this->fatal_error = TRUE;
247 break;
248 }
249 case TNCCS_MSG_PREFERRED_LANGUAGE:
250 {
251 tnccs_preferred_language_msg_t *lang_msg;
252 char *lang;
253
254 lang_msg = (tnccs_preferred_language_msg_t*)msg;
255 lang = lang_msg->get_preferred_language(lang_msg);
256
257 DBG2(DBG_TNC, "setting preferred language to '%s'", lang);
258 this->recs->set_preferred_language(this->recs,
259 chunk_create(lang, strlen(lang)));
260 break;
261 }
262 case TNCCS_MSG_REASON_STRINGS:
263 {
264 tnccs_reason_strings_msg_t *reason_msg;
265 chunk_t reason_string, reason_lang;
266
267 reason_msg = (tnccs_reason_strings_msg_t*)msg;
268 reason_string = reason_msg->get_reason(reason_msg, &reason_lang);
269 DBG2(DBG_TNC, "reason string is '%.*s'", reason_string.len,
270 reason_string.ptr);
271 DBG2(DBG_TNC, "reason language is '%.*s'", reason_lang.len,
272 reason_lang.ptr);
273 break;
274 }
275 default:
276 break;
277 }
278 }
279
280 METHOD(tls_t, process, status_t,
281 private_tnccs_11_t *this, void *buf, size_t buflen)
282 {
283 chunk_t data;
284 tnccs_batch_t *batch;
285 tnccs_msg_t *msg;
286 enumerator_t *enumerator;
287 status_t status;
288
289 if (this->is_server && !this->connection_id)
290 {
291 this->connection_id = tnc->tnccs->create_connection(tnc->tnccs,
292 TNCCS_1_1, (tnccs_t*)this, _send_msg,
293 &this->request_handshake_retry, &this->recs);
294 if (!this->connection_id)
295 {
296 return FAILED;
297 }
298 tnc->imvs->notify_connection_change(tnc->imvs, this->connection_id,
299 TNC_CONNECTION_STATE_CREATE);
300 tnc->imvs->notify_connection_change(tnc->imvs, this->connection_id,
301 TNC_CONNECTION_STATE_HANDSHAKE);
302 }
303
304 data = chunk_create(buf, buflen);
305 DBG1(DBG_TNC, "received TNCCS Batch (%u bytes) for Connection ID %u",
306 data.len, this->connection_id);
307 DBG3(DBG_TNC, "%.*s", data.len, data.ptr);
308 batch = tnccs_batch_create_from_data(this->is_server, ++this->batch_id, data);
309 status = batch->process(batch);
310
311 if (status == FAILED)
312 {
313 this->fatal_error = TRUE;
314 this->mutex->lock(this->mutex);
315 if (this->batch)
316 {
317 DBG1(DBG_TNC, "cancelling TNCCS batch");
318 this->batch->destroy(this->batch);
319 this->batch_id--;
320 }
321 this->batch = tnccs_batch_create(this->is_server, ++this->batch_id);
322
323 /* add error messages to outbound batch */
324 enumerator = batch->create_error_enumerator(batch);
325 while (enumerator->enumerate(enumerator, &msg))
326 {
327 this->batch->add_msg(this->batch, msg->get_ref(msg));
328 }
329 enumerator->destroy(enumerator);
330 this->mutex->unlock(this->mutex);
331 }
332 else
333 {
334 enumerator = batch->create_msg_enumerator(batch);
335 while (enumerator->enumerate(enumerator, &msg))
336 {
337 handle_message(this, msg);
338 }
339 enumerator->destroy(enumerator);
340
341 /* received any TNCCS-Error messages */
342 if (this->fatal_error)
343 {
344 DBG1(DBG_TNC, "a fatal TNCCS-Error occurred, terminating connection");
345 batch->destroy(batch);
346 return FAILED;
347 }
348
349 this->send_msg = TRUE;
350 if (this->is_server)
351 {
352 tnc->imvs->batch_ending(tnc->imvs, this->connection_id);
353 }
354 else
355 {
356 tnc->imcs->batch_ending(tnc->imcs, this->connection_id);
357 }
358 this->send_msg = FALSE;
359 }
360 batch->destroy(batch);
361
362 return NEED_MORE;
363 }
364
365 /**
366 * Add a recommendation message if a final recommendation is available
367 */
368 static void check_and_build_recommendation(private_tnccs_11_t *this)
369 {
370 TNC_IMV_Action_Recommendation rec;
371 TNC_IMV_Evaluation_Result eval;
372 TNC_IMVID id;
373 chunk_t reason, language;
374 enumerator_t *enumerator;
375 tnccs_msg_t *msg;
376
377 if (!this->recs->have_recommendation(this->recs, &rec, &eval))
378 {
379 tnc->imvs->solicit_recommendation(tnc->imvs, this->connection_id);
380 }
381 if (this->recs->have_recommendation(this->recs, &rec, &eval))
382 {
383 if (!this->batch)
384 {
385 this->batch = tnccs_batch_create(this->is_server, ++this->batch_id);
386 }
387
388 msg = tnccs_recommendation_msg_create(rec);
389 this->batch->add_msg(this->batch, msg);
390
391 /* currently we just send the first Reason String */
392 enumerator = this->recs->create_reason_enumerator(this->recs);
393 if (enumerator->enumerate(enumerator, &id, &reason, &language))
394 {
395 msg = tnccs_reason_strings_msg_create(reason, language);
396 this->batch->add_msg(this->batch, msg);
397 }
398 enumerator->destroy(enumerator);
399 this->recs->clear_reasons(this->recs);
400
401 /* we have reache the final state */
402 this->delete_state = TRUE;
403 }
404 }
405
406 METHOD(tls_t, build, status_t,
407 private_tnccs_11_t *this, void *buf, size_t *buflen, size_t *msglen)
408 {
409 status_t status;
410
411 /* Initialize the connection */
412 if (!this->is_server && !this->connection_id)
413 {
414 tnccs_msg_t *msg;
415 char *pref_lang;
416
417 this->connection_id = tnc->tnccs->create_connection(tnc->tnccs,
418 TNCCS_1_1, (tnccs_t*)this, _send_msg,
419 &this->request_handshake_retry, NULL);
420 if (!this->connection_id)
421 {
422 return FAILED;
423 }
424
425 /* Create TNCCS-PreferredLanguage message */
426 pref_lang = tnc->imcs->get_preferred_language(tnc->imcs);
427 msg = tnccs_preferred_language_msg_create(pref_lang);
428 this->mutex->lock(this->mutex);
429 this->batch = tnccs_batch_create(this->is_server, ++this->batch_id);
430 this->batch->add_msg(this->batch, msg);
431 this->mutex->unlock(this->mutex);
432
433 tnc->imcs->notify_connection_change(tnc->imcs, this->connection_id,
434 TNC_CONNECTION_STATE_CREATE);
435 tnc->imcs->notify_connection_change(tnc->imcs, this->connection_id,
436 TNC_CONNECTION_STATE_HANDSHAKE);
437 this->send_msg = TRUE;
438 tnc->imcs->begin_handshake(tnc->imcs, this->connection_id);
439 this->send_msg = FALSE;
440 }
441
442 /* Do not allow any asynchronous IMCs or IMVs to add additional messages */
443 this->mutex->lock(this->mutex);
444
445 if (this->recs && !this->delete_state &&
446 (!this->batch || this->fatal_error))
447 {
448 check_and_build_recommendation(this);
449 }
450
451 if (this->batch)
452 {
453 chunk_t data;
454
455 this->batch->build(this->batch);
456 data = this->batch->get_encoding(this->batch);
457 DBG1(DBG_TNC, "sending TNCCS Batch (%d bytes) for Connection ID %u",
458 data.len, this->connection_id);
459 DBG3(DBG_TNC, "%.*s", data.len, data.ptr);
460 *msglen = data.len;
461
462 if (data.len > *buflen)
463 {
464 DBG1(DBG_TNC, "fragmentation of TNCCS batch not supported yet");
465 }
466 else
467 {
468 *buflen = data.len;
469 }
470 memcpy(buf, data.ptr, *buflen);
471 this->batch->destroy(this->batch);
472 this->batch = NULL;
473 status = ALREADY_DONE;
474 }
475 else
476 {
477 DBG1(DBG_TNC, "no TNCCS Batch to send");
478 status = INVALID_STATE;
479 }
480 this->mutex->unlock(this->mutex);
481
482 return status;
483 }
484
485 METHOD(tls_t, is_server, bool,
486 private_tnccs_11_t *this)
487 {
488 return this->is_server;
489 }
490
491 METHOD(tls_t, get_purpose, tls_purpose_t,
492 private_tnccs_11_t *this)
493 {
494 return TLS_PURPOSE_EAP_TNC;
495 }
496
497 METHOD(tls_t, is_complete, bool,
498 private_tnccs_11_t *this)
499 {
500 TNC_IMV_Action_Recommendation rec;
501 TNC_IMV_Evaluation_Result eval;
502
503 if (this->recs && this->recs->have_recommendation(this->recs, &rec, &eval))
504 {
505 return tnc->imvs->enforce_recommendation(tnc->imvs, rec, eval);
506 }
507 else
508 {
509 return FALSE;
510 }
511 }
512
513 METHOD(tls_t, get_eap_msk, chunk_t,
514 private_tnccs_11_t *this)
515 {
516 return chunk_empty;
517 }
518
519 METHOD(tls_t, destroy, void,
520 private_tnccs_11_t *this)
521 {
522 tnc->tnccs->remove_connection(tnc->tnccs, this->connection_id,
523 this->is_server);
524 this->mutex->destroy(this->mutex);
525 DESTROY_IF(this->batch);
526 free(this);
527 }
528
529 /**
530 * See header
531 */
532 tls_t *tnccs_11_create(bool is_server)
533 {
534 private_tnccs_11_t *this;
535
536 INIT(this,
537 .public = {
538 .process = _process,
539 .build = _build,
540 .is_server = _is_server,
541 .get_purpose = _get_purpose,
542 .is_complete = _is_complete,
543 .get_eap_msk = _get_eap_msk,
544 .destroy = _destroy,
545 },
546 .is_server = is_server,
547 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
548 );
549
550 return &this->public;
551 }