moved TNCCS layer out of eap_tnc plugin
[strongswan.git] / src / libcharon / plugins / eap_tnc / eap_tnc.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 "eap_tnc.h"
17
18 #include <tls_eap.h>
19
20 #include <daemon.h>
21 #include <library.h>
22
23 typedef struct private_eap_tnc_t private_eap_tnc_t;
24
25 /**
26 * Private data of an eap_tnc_t object.
27 */
28 struct private_eap_tnc_t {
29
30 /**
31 * Public authenticator_t interface.
32 */
33 eap_tnc_t public;
34
35 /**
36 * TLS stack, wrapped by EAP helper
37 */
38 tls_eap_t *tls_eap;
39 };
40
41
42 /** Maximum number of EAP-TNC messages/fragments allowed */
43 #define MAX_MESSAGE_COUNT 2
44 /** Default size of a EAP-TNC fragment */
45 #define MAX_FRAGMENT_LEN 50000
46
47 METHOD(eap_method_t, initiate, status_t,
48 private_eap_tnc_t *this, eap_payload_t **out)
49 {
50 chunk_t data;
51
52 if (this->tls_eap->initiate(this->tls_eap, &data) == NEED_MORE)
53 {
54 *out = eap_payload_create_data(data);
55 free(data.ptr);
56 return NEED_MORE;
57 }
58 return FAILED;
59 }
60
61 METHOD(eap_method_t, process, status_t,
62 private_eap_tnc_t *this, eap_payload_t *in, eap_payload_t **out)
63 {
64 status_t status;
65 chunk_t data;
66
67 data = in->get_data(in);
68 status = this->tls_eap->process(this->tls_eap, data, &data);
69 if (status == NEED_MORE)
70 {
71 *out = eap_payload_create_data(data);
72 free(data.ptr);
73 }
74 return status;
75 }
76
77 METHOD(eap_method_t, get_type, eap_type_t,
78 private_eap_tnc_t *this, u_int32_t *vendor)
79 {
80 *vendor = 0;
81 return EAP_TNC;
82 }
83
84 METHOD(eap_method_t, get_msk, status_t,
85 private_eap_tnc_t *this, chunk_t *msk)
86 {
87 *msk = this->tls_eap->get_msk(this->tls_eap);
88 if (msk->len)
89 {
90 return SUCCESS;
91 }
92 return FAILED;
93 }
94
95 METHOD(eap_method_t, is_mutual, bool,
96 private_eap_tnc_t *this)
97 {
98 return FALSE;
99 }
100
101 METHOD(eap_method_t, destroy, void,
102 private_eap_tnc_t *this)
103 {
104 this->tls_eap->destroy(this->tls_eap);
105 free(this);
106 }
107
108 /**
109 * Generic private constructor
110 */
111 static eap_tnc_t *eap_tnc_create(identification_t *server,
112 identification_t *peer, bool is_server)
113 {
114 private_eap_tnc_t *this;
115 size_t frag_size;
116 int max_msg_count;
117 tnccs_t *tnccs;
118
119 INIT(this,
120 .public = {
121 .eap_method = {
122 .initiate = _initiate,
123 .process = _process,
124 .get_type = _get_type,
125 .is_mutual = _is_mutual,
126 .get_msk = _get_msk,
127 .destroy = _destroy,
128 },
129 },
130 );
131
132 frag_size = lib->settings->get_int(lib->settings,
133 "charon.plugins.eap-tnc.fragment_size", MAX_FRAGMENT_LEN);
134 max_msg_count = lib->settings->get_int(lib->settings,
135 "charon.plugins.eap-tnc.max_message_count", MAX_MESSAGE_COUNT);
136 tnccs = charon->tnccs->create_instance(charon->tnccs, TNCCS_1_1, is_server);
137 this->tls_eap = tls_eap_create(EAP_TNC, (tls_t*)tnccs, frag_size, max_msg_count);
138 if (!this->tls_eap)
139 {
140 free(this);
141 return NULL;
142 }
143 return &this->public;
144 }
145
146 eap_tnc_t *eap_tnc_create_server(identification_t *server,
147 identification_t *peer)
148 {
149 return eap_tnc_create(server, peer, TRUE);
150 }
151
152 eap_tnc_t *eap_tnc_create_peer(identification_t *server,
153 identification_t *peer)
154 {
155 return eap_tnc_create(server, peer, FALSE);
156 }