Revert alloc_str changes
[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 <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 char* protocol;
130 tnccs_type_t type;
131 tnccs_t *tnccs;
132
133 INIT(this,
134 .public = {
135 .eap_method = {
136 .initiate = _initiate,
137 .process = _process,
138 .get_type = _get_type,
139 .is_mutual = _is_mutual,
140 .get_msk = _get_msk,
141 .get_identifier = _get_identifier,
142 .set_identifier = _set_identifier,
143 .destroy = _destroy,
144 },
145 },
146 );
147
148 frag_size = lib->settings->get_int(lib->settings,
149 "charon.plugins.eap-tnc.fragment_size", MAX_FRAGMENT_LEN);
150 max_msg_count = lib->settings->get_int(lib->settings,
151 "charon.plugins.eap-tnc.max_message_count", MAX_MESSAGE_COUNT);
152 protocol = lib->settings->get_str(lib->settings,
153 "charon.plugins.eap-tnc.protocol", "tnccs-1.1");
154 if (strcaseeq(protocol, "tnccs-2.0"))
155 {
156 type = TNCCS_2_0;
157 }
158 else if (strcaseeq(protocol, "tnccs-1.1"))
159 {
160 type = TNCCS_1_1;
161 }
162 else if (strcaseeq(protocol, "tnccs-dynamic") && is_server)
163 {
164 type = TNCCS_DYNAMIC;
165 }
166 else
167 {
168 DBG1(DBG_TNC, "TNCCS protocol '%s' not supported", protocol);
169 free(this);
170 return NULL;
171 }
172 tnccs = charon->tnccs->create_instance(charon->tnccs, type, is_server);
173 this->tls_eap = tls_eap_create(EAP_TNC, (tls_t*)tnccs, frag_size, max_msg_count);
174 if (!this->tls_eap)
175 {
176 free(this);
177 return NULL;
178 }
179 return &this->public;
180 }
181
182 eap_tnc_t *eap_tnc_create_server(identification_t *server,
183 identification_t *peer)
184 {
185 return eap_tnc_create(server, peer, TRUE);
186 }
187
188 eap_tnc_t *eap_tnc_create_peer(identification_t *server,
189 identification_t *peer)
190 {
191 return eap_tnc_create(server, peer, FALSE);
192 }