aboutsummaryrefslogtreecommitdiffstats
path: root/src/libcharon/plugins/tnccs_11/tnccs_11.c
blob: 18e06d4af1b742ab919fb284ea3fa4a2fe56986d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
 * Copyright (C) 2010 Andreas Steffen
 * HSR Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "tnccs_11.h"

#include <libtnctncc.h>
#include <libtnctncs.h>

#include <debug.h>

static chunk_t output;

/**
 * Define callback functions called by the libtnc library
 */
TNC_Result TNC_TNCC_SendBatch(libtnc_tncc_connection* conn, 
							  const char* messageBuffer, size_t messageLength)
{
	chunk_free(&output);
	output = chunk_alloc(messageLength);
	memcpy(output.ptr, messageBuffer, messageLength);

	return TNC_RESULT_SUCCESS;
}

TNC_Result TNC_TNCS_SendBatch(libtnc_tncs_connection* conn, 
							  const char* messageBuffer, size_t messageLength)
{
	chunk_free(&output);
	output = chunk_alloc(messageLength);
	memcpy(output.ptr, messageBuffer, messageLength);

	return TNC_RESULT_SUCCESS;
}

typedef struct private_tnccs_11_t private_tnccs_11_t;

/**
 * Private data of a tnccs_11_t object.
 */
struct private_tnccs_11_t {

	/**
	 * Public tls_t interface.
	 */
	tls_t public;

	/**
	 * TNCC if TRUE, TNCS if FALSE
	 */
	bool is_server;

	/**
	 * TNCC Connection to IMCs
	 */
	libtnc_tncc_connection* tncc_connection;

	/**
	 * TNCS Connection to IMVs
	 */
	libtnc_tncs_connection* tncs_connection;
};

METHOD(tls_t, process, status_t,
	private_tnccs_11_t *this, void *buf, size_t buflen)
{
	if (this->is_server && !this->tncs_connection)
	{
		this->tncs_connection = libtnc_tncs_CreateConnection(NULL);
		if (!this->tncs_connection)
		{
			DBG1(DBG_IKE, "TNCS CreateConnection failed");
			return FAILED;
		}
		DBG1(DBG_IKE, "assigned TNCS Connection ID: %d",
			 this->tncs_connection->connectionID);
		if (libtnc_tncs_BeginSession(this->tncs_connection) != TNC_RESULT_SUCCESS)
		{
			DBG1(DBG_IKE, "TNCS BeginSession failed");
			return FAILED;
		}
	}

	DBG1(DBG_IKE, "received TNCCS Batch with %u bytes:", buflen);
	DBG1(DBG_IKE, "%.*s", buflen, buf);

	if (this->is_server)
	{
		if (libtnc_tncs_ReceiveBatch(this->tncs_connection, buf, buflen) !=
			TNC_RESULT_SUCCESS)
		{
			DBG1(DBG_IKE, "TNCS ReceiveBatch failed");
			return FAILED;
		}
	}
	else
	{
		if (libtnc_tncc_ReceiveBatch(this->tncc_connection, buf, buflen) !=
			TNC_RESULT_SUCCESS)
		{
			DBG1(DBG_IKE, "TNCC ReceiveBatch failed");
			return FAILED;
		}
	}
	return NEED_MORE;
}

METHOD(tls_t, build, status_t,
	private_tnccs_11_t *this, void *buf, size_t *buflen, size_t *msglen)
{
	size_t len;

	if (!this->is_server && !this->tncc_connection)
	{
		this->tncc_connection = libtnc_tncc_CreateConnection(NULL);
		if (!this->tncc_connection)
		{
			DBG1(DBG_IKE, "TNCC CreateConnection failed");
			return FAILED;
		}
		DBG1(DBG_IKE, "assigned TNCC Connection ID: %d",
			 this->tncc_connection->connectionID);
		if (libtnc_tncc_BeginSession(this->tncc_connection) != TNC_RESULT_SUCCESS)
		{
			DBG1(DBG_IKE, "TNCC BeginSession failed");
			return FAILED;
		}
	}
		
	len = *buflen;
	len = min(len, output.len);
	*buflen = len;
	if (msglen)
	{
		*msglen = output.len;
	}

	if (output.len)
	{
		DBG1(DBG_IKE, "sending TNCCS Batch with %d bytes:", output.len);
		DBG1(DBG_IKE, "%.*s", output.len, output.ptr);
		memcpy(buf, output.ptr, len);
		chunk_free(&output);
		return ALREADY_DONE;
	}
	else
	{
		return INVALID_STATE;
	}
}

METHOD(tls_t, is_server, bool,
	private_tnccs_11_t *this)
{
	return this->is_server;
}

METHOD(tls_t, get_purpose, tls_purpose_t,
	private_tnccs_11_t *this)
{
	return TLS_PURPOSE_EAP_TNC;
}

METHOD(tls_t, is_complete, bool,
	private_tnccs_11_t *this)
{
	TNC_IMV_Action_Recommendation* rec = NULL;
	TNC_IMV_Evaluation_Result* eval = NULL;
	
	if (libtnc_tncs_HaveRecommendation(this->tncs_connection, rec, eval) ==
		TNC_RESULT_SUCCESS)
	{
		DBG1(DBG_IKE, "have recommendation");
		return TRUE;
	}
	else
	{
		DBG1(DBG_IKE, "no recommendation");
		return FALSE;
	}
}

METHOD(tls_t, get_eap_msk, chunk_t,
	private_tnccs_11_t *this)
{
	return chunk_empty;
}

METHOD(tls_t, destroy, void,
	private_tnccs_11_t *this)
{
	if (this->is_server)
	{
		if (this->tncs_connection)
		{
			/* libtnc_tncs_DeleteConnection(this->tncs_connection); */
		}
	}
	else
	{
		if (this->tncc_connection)
		{
			libtnc_tncc_DeleteConnection(this->tncc_connection);
		}
		libtnc_tncc_Terminate();
	}
	free(this);
}

/**
 * See header
 */
tls_t *tnccs_11_create(bool is_server)
{
	char *tnc_config, *pref_lang;
	private_tnccs_11_t *this;

	INIT(this,
		.public = {
			.process = _process,
			.build = _build,
			.is_server = _is_server,
			.get_purpose = _get_purpose,
			.is_complete = _is_complete,
			.get_eap_msk = _get_eap_msk,
			.destroy = _destroy,
		},
		.is_server = is_server,
	);

	return &this->public;
}