5afc6e196aa0fda0f75aa20223ff82b4c45cc55d
[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 if (this->is_server && !this->connection_id)
67 {
68 this->connection_id = charon->tnccs->create_connection(charon->tnccs,
69 (tnccs_t*)this, _send_message);
70 charon->imvs->notify_connection_change(charon->imvs,
71 this->connection_id, TNC_CONNECTION_STATE_CREATE);
72 }
73 DBG1(DBG_TNC, "received TNCCS Batch (%u bytes) for Connection ID %u",
74 buflen, this->connection_id);
75 DBG3(DBG_TNC, "%.*s", buflen, buf);
76
77 return NEED_MORE;
78 }
79
80 METHOD(tls_t, build, status_t,
81 private_tnccs_20_t *this, void *buf, size_t *buflen, size_t *msglen)
82 {
83 char *msg = this->is_server ? "|tncs->tncc 2.0|" : "|tncc->tncs 2.0|";
84 size_t len;
85
86 this->batch = chunk_clone(chunk_create(msg, strlen(msg)));
87
88 if (!this->is_server && !this->connection_id)
89 {
90 this->connection_id = charon->tnccs->create_connection(charon->tnccs,
91 (tnccs_t*)this, _send_message);
92 charon->imcs->notify_connection_change(charon->imcs,
93 this->connection_id, TNC_CONNECTION_STATE_CREATE);
94 charon->imcs->notify_connection_change(charon->imcs,
95 this->connection_id, TNC_CONNECTION_STATE_HANDSHAKE);
96 charon->imcs->begin_handshake(charon->imcs, this->connection_id);
97 }
98
99 len = this->batch.len;
100 *msglen = len;
101 *buflen = len;
102 memcpy(buf, this->batch.ptr, len);
103
104 DBG1(DBG_TNC, "sending TNCCS Batch (%d bytes) for Connection ID %u",
105 len, this->connection_id);
106 DBG3(DBG_TNC, "%.*s", len, buf);
107 chunk_free(&this->batch);
108
109 return ALREADY_DONE;
110 }
111
112 METHOD(tls_t, is_server, bool,
113 private_tnccs_20_t *this)
114 {
115 return this->is_server;
116 }
117
118 METHOD(tls_t, get_purpose, tls_purpose_t,
119 private_tnccs_20_t *this)
120 {
121 return TLS_PURPOSE_EAP_TNC;
122 }
123
124 METHOD(tls_t, is_complete, bool,
125 private_tnccs_20_t *this)
126 {
127 return FALSE;
128 }
129
130 METHOD(tls_t, get_eap_msk, chunk_t,
131 private_tnccs_20_t *this)
132 {
133 return chunk_empty;
134 }
135
136 METHOD(tls_t, destroy, void,
137 private_tnccs_20_t *this)
138 {
139 charon->tnccs->remove_connection(charon->tnccs, this->connection_id);
140 free(this->batch.ptr);
141 free(this);
142 }
143
144 /**
145 * See header
146 */
147 tls_t *tnccs_20_create(bool is_server)
148 {
149 private_tnccs_20_t *this;
150
151 INIT(this,
152 .public = {
153 .process = _process,
154 .build = _build,
155 .is_server = _is_server,
156 .get_purpose = _get_purpose,
157 .is_complete = _is_complete,
158 .get_eap_msk = _get_eap_msk,
159 .destroy = _destroy,
160 },
161 .is_server = is_server,
162 );
163
164 return &this->public;
165 }