explicitly activate use of TNC headers
[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 #include <debug.h>
20
21 #define USE_TNC
22
23 #include <daemon.h>
24
25 typedef struct private_eap_tnc_t private_eap_tnc_t;
26
27 /**
28 * Private data of an eap_tnc_t object.
29 */
30 struct private_eap_tnc_t {
31
32 /**
33 * Public authenticator_t interface.
34 */
35 eap_tnc_t public;
36
37 /**
38 * TLS stack, wrapped by EAP helper
39 */
40 tls_eap_t *tls_eap;
41 };
42
43
44 /** Maximum number of EAP-TNC messages/fragments allowed */
45 #define MAX_MESSAGE_COUNT 10
46 /** Default size of a EAP-TNC fragment */
47 #define MAX_FRAGMENT_LEN 50000
48
49 METHOD(eap_method_t, initiate, status_t,
50 private_eap_tnc_t *this, eap_payload_t **out)
51 {
52 chunk_t data;
53
54 if (this->tls_eap->initiate(this->tls_eap, &data) == NEED_MORE)
55 {
56 *out = eap_payload_create_data(data);
57 free(data.ptr);
58 return NEED_MORE;
59 }
60 return FAILED;
61 }
62
63 METHOD(eap_method_t, process, status_t,
64 private_eap_tnc_t *this, eap_payload_t *in, eap_payload_t **out)
65 {
66 status_t status;
67 chunk_t data;
68
69 data = in->get_data(in);
70 status = this->tls_eap->process(this->tls_eap, data, &data);
71 if (status == NEED_MORE)
72 {
73 *out = eap_payload_create_data(data);
74 free(data.ptr);
75 }
76 return status;
77 }
78
79 METHOD(eap_method_t, get_type, eap_type_t,
80 private_eap_tnc_t *this, u_int32_t *vendor)
81 {
82 *vendor = 0;
83 return EAP_TNC;
84 }
85
86 METHOD(eap_method_t, get_msk, status_t,
87 private_eap_tnc_t *this, chunk_t *msk)
88 {
89 *msk = this->tls_eap->get_msk(this->tls_eap);
90 if (msk->len)
91 {
92 return SUCCESS;
93 }
94 return FAILED;
95 }
96
97 METHOD(eap_method_t, get_identifier, u_int8_t,
98 private_eap_tnc_t *this)
99 {
100 return this->tls_eap->get_identifier(this->tls_eap);
101 }
102
103 METHOD(eap_method_t, set_identifier, void,
104 private_eap_tnc_t *this, u_int8_t identifier)
105 {
106 this->tls_eap->set_identifier(this->tls_eap, identifier);
107 }
108
109 METHOD(eap_method_t, is_mutual, bool,
110 private_eap_tnc_t *this)
111 {
112 return FALSE;
113 }
114
115 METHOD(eap_method_t, destroy, void,
116 private_eap_tnc_t *this)
117 {
118 this->tls_eap->destroy(this->tls_eap);
119 free(this);
120 }
121
122 /**
123 * Generic private constructor
124 */
125 static eap_tnc_t *eap_tnc_create(identification_t *server,
126 identification_t *peer, bool is_server)
127 {
128 private_eap_tnc_t *this;
129 size_t frag_size;
130 int max_msg_count;
131 bool include_length;
132 char* protocol;
133 tnccs_type_t type;
134 tnccs_t *tnccs;
135
136 INIT(this,
137 .public = {
138 .eap_method = {
139 .initiate = _initiate,
140 .process = _process,
141 .get_type = _get_type,
142 .is_mutual = _is_mutual,
143 .get_msk = _get_msk,
144 .get_identifier = _get_identifier,
145 .set_identifier = _set_identifier,
146 .destroy = _destroy,
147 },
148 },
149 );
150
151 frag_size = lib->settings->get_int(lib->settings,
152 "charon.plugins.eap-tnc.fragment_size", MAX_FRAGMENT_LEN);
153 max_msg_count = lib->settings->get_int(lib->settings,
154 "charon.plugins.eap-tnc.max_message_count", MAX_MESSAGE_COUNT);
155 include_length = lib->settings->get_bool(lib->settings,
156 "charon.plugins.eap-tnc.include_length", TRUE);
157 protocol = lib->settings->get_str(lib->settings,
158 "charon.plugins.eap-tnc.protocol", "tnccs-1.1");
159 if (strcaseeq(protocol, "tnccs-2.0"))
160 {
161 type = TNCCS_2_0;
162 }
163 else if (strcaseeq(protocol, "tnccs-1.1"))
164 {
165 type = TNCCS_1_1;
166 }
167 else if (strcaseeq(protocol, "tnccs-dynamic") && is_server)
168 {
169 type = TNCCS_DYNAMIC;
170 }
171 else
172 {
173 DBG1(DBG_TNC, "TNCCS protocol '%s' not supported", protocol);
174 free(this);
175 return NULL;
176 }
177 tnccs = charon->tnccs->create_instance(charon->tnccs, type, is_server);
178 this->tls_eap = tls_eap_create(EAP_TNC, (tls_t*)tnccs, frag_size,
179 max_msg_count, include_length);
180 if (!this->tls_eap)
181 {
182 free(this);
183 return NULL;
184 }
185 return &this->public;
186 }
187
188 eap_tnc_t *eap_tnc_create_server(identification_t *server,
189 identification_t *peer)
190 {
191 return eap_tnc_create(server, peer, TRUE);
192 }
193
194 eap_tnc_t *eap_tnc_create_peer(identification_t *server,
195 identification_t *peer)
196 {
197 return eap_tnc_create(server, peer, FALSE);
198 }