moved tnc_imv plugin to libtnccs thanks to recommendation callback function
[strongswan.git] / src / libtnccs / plugins / tnccs_dynamic / tnccs_dynamic.c
1 /*
2 * Copyright (C) 2011-2013 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 "tnccs_dynamic.h"
17
18 #include <tnc/tnc.h>
19
20 #include <utils/debug.h>
21
22 typedef struct private_tnccs_dynamic_t private_tnccs_dynamic_t;
23
24 /**
25 * Private data of a tnccs_dynamic_t object.
26 */
27 struct private_tnccs_dynamic_t {
28
29 /**
30 * Public tnccs_t interface.
31 */
32 tnccs_t public;
33
34 /**
35 * Server identity
36 */
37 identification_t *server;
38
39 /**
40 * Client identity
41 */
42 identification_t *peer;
43
44 /**
45 * Detected TNC IF-TNCCS stack
46 */
47 tls_t *tls;
48
49 /**
50 * Underlying TNC IF-T transport protocol
51 */
52 tnc_ift_type_t transport;
53
54 /**
55 * Type of TNC client authentication
56 */
57 u_int32_t auth_type;
58
59 /**
60 * Callback function to communicate recommendation (TNC Server only)
61 */
62 tnccs_cb_t callback;
63
64 };
65
66 /**
67 * Determine the version of the IF-TNCCS protocol used by analyzing the first
68 * byte of the TNCCS batch received from a TNC Client according to the rules
69 * defined by section 3.5 "Interoperability with older IF-TNCCS versions" of
70 * the TCG TNC IF-TNCCS TLV Bindings Version 2.0 standard.
71 */
72 tnccs_type_t determine_tnccs_protocol(char version)
73 {
74 switch (version)
75 {
76 case '\t':
77 case '\n':
78 case '\r':
79 case ' ':
80 case '<':
81 return TNCCS_1_1;
82 case 0x00:
83 return TNCCS_SOH;
84 case 0x02:
85 return TNCCS_2_0;
86 default:
87 return TNCCS_UNKNOWN;
88 }
89 }
90
91 METHOD(tls_t, process, status_t,
92 private_tnccs_dynamic_t *this, void *buf, size_t buflen)
93 {
94 tnccs_type_t type;
95 tnccs_t *tnccs;
96
97 if (!this->tls)
98 {
99 if (buflen == 0)
100 {
101 return FAILED;
102 }
103 type = determine_tnccs_protocol(*(char*)buf);
104 DBG1(DBG_TNC, "%N protocol detected dynamically",
105 tnccs_type_names, type);
106 tnccs = tnc->tnccs->create_instance(tnc->tnccs, type, TRUE,
107 this->server, this->peer, this->transport,
108 this->callback);
109 if (!tnccs)
110 {
111 DBG1(DBG_TNC, "N% protocol not supported", tnccs_type_names, type);
112 return FAILED;
113 }
114 tnccs->set_auth_type(tnccs, this->auth_type);
115 this->tls = &tnccs->tls;
116 }
117 return this->tls->process(this->tls, buf, buflen);
118 }
119
120 METHOD(tls_t, build, status_t,
121 private_tnccs_dynamic_t *this, void *buf, size_t *buflen, size_t *msglen)
122 {
123 return this->tls->build(this->tls, buf, buflen, msglen);
124 }
125
126 METHOD(tls_t, is_server, bool,
127 private_tnccs_dynamic_t *this)
128 {
129 return TRUE;
130 }
131
132 METHOD(tls_t, get_server_id, identification_t*,
133 private_tnccs_dynamic_t *this)
134 {
135 return this->server;
136 }
137
138 METHOD(tls_t, get_peer_id, identification_t*,
139 private_tnccs_dynamic_t *this)
140 {
141 return this->peer;
142 }
143
144 METHOD(tls_t, get_purpose, tls_purpose_t,
145 private_tnccs_dynamic_t *this)
146 {
147 return TLS_PURPOSE_EAP_TNC;
148 }
149
150 METHOD(tls_t, is_complete, bool,
151 private_tnccs_dynamic_t *this)
152 {
153 return this->tls ? this->tls->is_complete(this->tls) : FALSE;
154 }
155
156 METHOD(tls_t, get_eap_msk, chunk_t,
157 private_tnccs_dynamic_t *this)
158 {
159 return chunk_empty;
160 }
161
162 METHOD(tls_t, destroy, void,
163 private_tnccs_dynamic_t *this)
164 {
165 DESTROY_IF(this->tls);
166 this->server->destroy(this->server);
167 this->peer->destroy(this->peer);
168 free(this);
169 }
170
171 METHOD(tnccs_t, get_transport, tnc_ift_type_t,
172 private_tnccs_dynamic_t *this)
173 {
174 return this->transport;
175 }
176
177 METHOD(tnccs_t, set_transport, void,
178 private_tnccs_dynamic_t *this, tnc_ift_type_t transport)
179 {
180 this->transport = transport;
181 }
182
183 METHOD(tnccs_t, get_auth_type, u_int32_t,
184 private_tnccs_dynamic_t *this)
185 {
186 return this->auth_type;
187 }
188
189 METHOD(tnccs_t, set_auth_type, void,
190 private_tnccs_dynamic_t *this, u_int32_t auth_type)
191 {
192 this->auth_type = auth_type;
193 }
194
195 /**
196 * See header
197 */
198 tnccs_t* tnccs_dynamic_create(bool is_server,
199 identification_t *server, identification_t *peer,
200 tnc_ift_type_t transport, tnccs_cb_t cb)
201 {
202 private_tnccs_dynamic_t *this;
203
204 INIT(this,
205 .public = {
206 .tls = {
207 .process = _process,
208 .build = _build,
209 .is_server = _is_server,
210 .get_server_id = _get_server_id,
211 .get_peer_id = _get_peer_id,
212 .get_purpose = _get_purpose,
213 .is_complete = _is_complete,
214 .get_eap_msk = _get_eap_msk,
215 .destroy = _destroy,
216 },
217 .get_transport = _get_transport,
218 .set_transport = _set_transport,
219 .get_auth_type = _get_auth_type,
220 .set_auth_type = _set_auth_type,
221 },
222 .server = server->clone(server),
223 .peer = peer->clone(peer),
224 .transport = transport,
225 .callback = cb,
226 );
227
228 return &this->public;
229 }