refactored TNC framework
[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 <tnc/tnc.h>
19 #include <tnc/tnccs/tnccs_manager.h>
20 #include <tls_eap.h>
21 #include <debug.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 10
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, get_identifier, u_int8_t,
96 private_eap_tnc_t *this)
97 {
98 return this->tls_eap->get_identifier(this->tls_eap);
99 }
100
101 METHOD(eap_method_t, set_identifier, void,
102 private_eap_tnc_t *this, u_int8_t identifier)
103 {
104 this->tls_eap->set_identifier(this->tls_eap, identifier);
105 }
106
107 METHOD(eap_method_t, is_mutual, bool,
108 private_eap_tnc_t *this)
109 {
110 return FALSE;
111 }
112
113 METHOD(eap_method_t, destroy, void,
114 private_eap_tnc_t *this)
115 {
116 this->tls_eap->destroy(this->tls_eap);
117 free(this);
118 }
119
120 /**
121 * Generic private constructor
122 */
123 static eap_tnc_t *eap_tnc_create(identification_t *server,
124 identification_t *peer, bool is_server)
125 {
126 private_eap_tnc_t *this;
127 size_t frag_size;
128 int max_msg_count;
129 bool include_length;
130 char* protocol;
131 tnccs_type_t type;
132 tnccs_t *tnccs;
133
134 INIT(this,
135 .public = {
136 .eap_method = {
137 .initiate = _initiate,
138 .process = _process,
139 .get_type = _get_type,
140 .is_mutual = _is_mutual,
141 .get_msk = _get_msk,
142 .get_identifier = _get_identifier,
143 .set_identifier = _set_identifier,
144 .destroy = _destroy,
145 },
146 },
147 );
148
149 frag_size = lib->settings->get_int(lib->settings,
150 "charon.plugins.eap-tnc.fragment_size", MAX_FRAGMENT_LEN);
151 max_msg_count = lib->settings->get_int(lib->settings,
152 "charon.plugins.eap-tnc.max_message_count", MAX_MESSAGE_COUNT);
153 include_length = lib->settings->get_bool(lib->settings,
154 "charon.plugins.eap-tnc.include_length", TRUE);
155 protocol = lib->settings->get_str(lib->settings,
156 "charon.plugins.eap-tnc.protocol", "tnccs-1.1");
157 if (strcaseeq(protocol, "tnccs-2.0"))
158 {
159 type = TNCCS_2_0;
160 }
161 else if (strcaseeq(protocol, "tnccs-1.1"))
162 {
163 type = TNCCS_1_1;
164 }
165 else if (strcaseeq(protocol, "tnccs-dynamic") && is_server)
166 {
167 type = TNCCS_DYNAMIC;
168 }
169 else
170 {
171 DBG1(DBG_TNC, "TNCCS protocol '%s' not supported", protocol);
172 free(this);
173 return NULL;
174 }
175 tnccs = tnc->tnccs->create_instance(tnc->tnccs, type, is_server);
176 this->tls_eap = tls_eap_create(EAP_TNC, (tls_t*)tnccs, frag_size,
177 max_msg_count, include_length);
178 if (!this->tls_eap)
179 {
180 free(this);
181 return NULL;
182 }
183 return &this->public;
184 }
185
186 eap_tnc_t *eap_tnc_create_server(identification_t *server,
187 identification_t *peer)
188 {
189 return eap_tnc_create(server, peer, TRUE);
190 }
191
192 eap_tnc_t *eap_tnc_create_peer(identification_t *server,
193 identification_t *peer)
194 {
195 return eap_tnc_create(server, peer, FALSE);
196 }