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