d4fc6a6f71f51aa422357fadd73638aa46975890
[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
61 /**
62 * Determine the version of the IF-TNCCS protocol used by analyzing the first
63 * byte of the TNCCS batch received from a TNC Client according to the rules
64 * defined by section 3.5 "Interoperability with older IF-TNCCS versions" of
65 * the TCG TNC IF-TNCCS TLV Bindings Version 2.0 standard.
66 */
67 tnccs_type_t determine_tnccs_protocol(char version)
68 {
69 switch (version)
70 {
71 case '\t':
72 case '\n':
73 case '\r':
74 case ' ':
75 case '<':
76 return TNCCS_1_1;
77 case 0x00:
78 return TNCCS_SOH;
79 case 0x02:
80 return TNCCS_2_0;
81 default:
82 return TNCCS_UNKNOWN;
83 }
84 }
85
86 METHOD(tls_t, process, status_t,
87 private_tnccs_dynamic_t *this, void *buf, size_t buflen)
88 {
89 tnccs_type_t type;
90 tnccs_t *tnccs;
91
92 if (!this->tls)
93 {
94 if (buflen == 0)
95 {
96 return FAILED;
97 }
98 type = determine_tnccs_protocol(*(char*)buf);
99 DBG1(DBG_TNC, "%N protocol detected dynamically",
100 tnccs_type_names, type);
101 tnccs = tnc->tnccs->create_instance(tnc->tnccs, type, TRUE,
102 this->server, this->peer, this->transport);
103 if (!tnccs)
104 {
105 DBG1(DBG_TNC, "N% protocol not supported", tnccs_type_names, type);
106 return FAILED;
107 }
108 tnccs->set_auth_type(tnccs, this->auth_type);
109 this->tls = &tnccs->tls;
110 }
111 return this->tls->process(this->tls, buf, buflen);
112 }
113
114 METHOD(tls_t, build, status_t,
115 private_tnccs_dynamic_t *this, void *buf, size_t *buflen, size_t *msglen)
116 {
117 return this->tls->build(this->tls, buf, buflen, msglen);
118 }
119
120 METHOD(tls_t, is_server, bool,
121 private_tnccs_dynamic_t *this)
122 {
123 return TRUE;
124 }
125
126 METHOD(tls_t, get_server_id, identification_t*,
127 private_tnccs_dynamic_t *this)
128 {
129 return this->server;
130 }
131
132 METHOD(tls_t, get_peer_id, identification_t*,
133 private_tnccs_dynamic_t *this)
134 {
135 return this->peer;
136 }
137
138 METHOD(tls_t, get_purpose, tls_purpose_t,
139 private_tnccs_dynamic_t *this)
140 {
141 return TLS_PURPOSE_EAP_TNC;
142 }
143
144 METHOD(tls_t, is_complete, bool,
145 private_tnccs_dynamic_t *this)
146 {
147 return this->tls ? this->tls->is_complete(this->tls) : FALSE;
148 }
149
150 METHOD(tls_t, get_eap_msk, chunk_t,
151 private_tnccs_dynamic_t *this)
152 {
153 return chunk_empty;
154 }
155
156 METHOD(tls_t, destroy, void,
157 private_tnccs_dynamic_t *this)
158 {
159 DESTROY_IF(this->tls);
160 this->server->destroy(this->server);
161 this->peer->destroy(this->peer);
162 free(this);
163 }
164
165 METHOD(tnccs_t, get_transport, tnc_ift_type_t,
166 private_tnccs_dynamic_t *this)
167 {
168 return this->transport;
169 }
170
171 METHOD(tnccs_t, set_transport, void,
172 private_tnccs_dynamic_t *this, tnc_ift_type_t transport)
173 {
174 this->transport = transport;
175 }
176
177 METHOD(tnccs_t, get_auth_type, u_int32_t,
178 private_tnccs_dynamic_t *this)
179 {
180 return this->auth_type;
181 }
182
183 METHOD(tnccs_t, set_auth_type, void,
184 private_tnccs_dynamic_t *this, u_int32_t auth_type)
185 {
186 this->auth_type = auth_type;
187 }
188
189 /**
190 * See header
191 */
192 tnccs_t* tnccs_dynamic_create(bool is_server,
193 identification_t *server,
194 identification_t *peer,
195 tnc_ift_type_t transport)
196 {
197 private_tnccs_dynamic_t *this;
198
199 INIT(this,
200 .public = {
201 .tls = {
202 .process = _process,
203 .build = _build,
204 .is_server = _is_server,
205 .get_server_id = _get_server_id,
206 .get_peer_id = _get_peer_id,
207 .get_purpose = _get_purpose,
208 .is_complete = _is_complete,
209 .get_eap_msk = _get_eap_msk,
210 .destroy = _destroy,
211 },
212 .get_transport = _get_transport,
213 .set_transport = _set_transport,
214 .get_auth_type = _get_auth_type,
215 .set_auth_type = _set_auth_type,
216 },
217 .server = server->clone(server),
218 .peer = peer->clone(peer),
219 .transport = transport,
220 );
221
222 return &this->public;
223 }