8f6f11e5009181e8de17a758658dc69371880472
[strongswan.git] / src / libcharon / plugins / tnccs_20 / tnccs_20.c
1 /*
2 * Copyright (C) 2010 Sansar Choinyanbuu
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_20.h"
17
18 #include <debug.h>
19 #include <daemon.h>
20 #include <tnc/tncif.h>
21 #include <tnc/tnccs/tnccs.h>
22
23 typedef struct private_tnccs_20_t private_tnccs_20_t;
24
25 /**
26 * Private data of a tnccs_20_t object.
27 */
28 struct private_tnccs_20_t {
29
30 /**
31 * Public tls_t interface.
32 */
33 tls_t public;
34
35 /**
36 * TNCC if TRUE, TNCS if FALSE
37 */
38 bool is_server;
39
40 /**
41 * Connection ID assigned to this TNCCS connection
42 */
43 TNC_ConnectionID connection_id;
44
45 /**
46 * Batch being constructed
47 */
48 chunk_t batch;
49 };
50
51 METHOD(tnccs_t, send_message, void,
52 private_tnccs_20_t* this, TNC_BufferReference message,
53 TNC_UInt32 message_len,
54 TNC_MessageType message_type)
55 {
56 chunk_t msg = { message, message_len },
57 batch = this->batch;
58
59 DBG1(DBG_TNC, "TNCCS 2.0 send message");
60 this->batch = chunk_cat("mc", batch, msg);
61 }
62
63 METHOD(tls_t, process, status_t,
64 private_tnccs_20_t *this, void *buf, size_t buflen)
65 {
66 char *pos;
67 size_t len;
68
69 if (this->is_server && !this->connection_id)
70 {
71 this->connection_id = charon->tnccs->create_connection(charon->tnccs,
72 (tnccs_t*)this, _send_message);
73 charon->imvs->notify_connection_change(charon->imvs,
74 this->connection_id, TNC_CONNECTION_STATE_CREATE);
75 }
76 DBG1(DBG_TNC, "received TNCCS Batch (%u bytes) for Connection ID %u",
77 buflen, this->connection_id);
78 DBG3(DBG_TNC, "%.*s", buflen, buf);
79 pos = strchr(buf, '|');
80 if (pos)
81 {
82 pos++;
83 len = buflen - ((char*)buf - pos);
84 }
85 else
86 {
87 pos = buf;
88 len = buflen;
89 }
90 if (this->is_server)
91 {
92 charon->imvs->receive_message(charon->imvs, this->connection_id,
93 pos, len, 0x0080ab31);
94 }
95 else
96 {
97 charon->imcs->receive_message(charon->imcs, this->connection_id,
98 pos, len, 0x0080ab31);
99 }
100 return NEED_MORE;
101 }
102
103 METHOD(tls_t, build, status_t,
104 private_tnccs_20_t *this, void *buf, size_t *buflen, size_t *msglen)
105 {
106 char *msg = this->is_server ? "tncs->tncc 2.0|" : "tncc->tncs 2.0|";
107 size_t len;
108
109 this->batch = chunk_clone(chunk_create(msg, strlen(msg)));
110
111 if (!this->is_server && !this->connection_id)
112 {
113 this->connection_id = charon->tnccs->create_connection(charon->tnccs,
114 (tnccs_t*)this, _send_message);
115 charon->imcs->notify_connection_change(charon->imcs,
116 this->connection_id, TNC_CONNECTION_STATE_CREATE);
117 charon->imcs->notify_connection_change(charon->imcs,
118 this->connection_id, TNC_CONNECTION_STATE_HANDSHAKE);
119 charon->imcs->begin_handshake(charon->imcs, this->connection_id);
120 }
121
122 len = this->batch.len;
123 *msglen = len;
124 *buflen = len;
125 memcpy(buf, this->batch.ptr, len);
126
127 DBG1(DBG_TNC, "sending TNCCS Batch (%d bytes) for Connection ID %u",
128 len, this->connection_id);
129 DBG3(DBG_TNC, "%.*s", len, buf);
130 chunk_free(&this->batch);
131
132 return ALREADY_DONE;
133 }
134
135 METHOD(tls_t, is_server, bool,
136 private_tnccs_20_t *this)
137 {
138 return this->is_server;
139 }
140
141 METHOD(tls_t, get_purpose, tls_purpose_t,
142 private_tnccs_20_t *this)
143 {
144 return TLS_PURPOSE_EAP_TNC;
145 }
146
147 METHOD(tls_t, is_complete, bool,
148 private_tnccs_20_t *this)
149 {
150 return FALSE;
151 }
152
153 METHOD(tls_t, get_eap_msk, chunk_t,
154 private_tnccs_20_t *this)
155 {
156 return chunk_empty;
157 }
158
159 METHOD(tls_t, destroy, void,
160 private_tnccs_20_t *this)
161 {
162 charon->tnccs->remove_connection(charon->tnccs, this->connection_id);
163 free(this->batch.ptr);
164 free(this);
165 }
166
167 /**
168 * See header
169 */
170 tls_t *tnccs_20_create(bool is_server)
171 {
172 private_tnccs_20_t *this;
173
174 INIT(this,
175 .public = {
176 .process = _process,
177 .build = _build,
178 .is_server = _is_server,
179 .get_purpose = _get_purpose,
180 .is_complete = _is_complete,
181 .get_eap_msk = _get_eap_msk,
182 .destroy = _destroy,
183 },
184 .is_server = is_server,
185 );
186
187 return &this->public;
188 }