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