summaryrefslogtreecommitdiff
path: root/src/libtls/tls_socket.c
blob: 75b714e308469f3b9d3032b0a01a2428761bbaee (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
 * Copyright (C) 2010 Martin Willi
 * Copyright (C) 2010 revosec AG
 *
 * 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 "tls_socket.h"

#include <unistd.h>
#include <errno.h>

#include <utils/debug.h>
#include <threading/thread.h>

/**
 * Buffer size for plain side I/O
 */
#define PLAIN_BUF_SIZE 4096

/**
 * Buffer size for encrypted side I/O
 */
#define CRYPTO_BUF_SIZE 4096

typedef struct private_tls_socket_t private_tls_socket_t;
typedef struct private_tls_application_t private_tls_application_t;

struct private_tls_application_t {

	/**
	 * Implements tls_application layer.
	 */
	tls_application_t application;

	/**
	 * Chunk of data to send
	 */
	chunk_t out;

	/**
	 * Chunk of data received
	 */
	chunk_t in;
};

/**
 * Private data of an tls_socket_t object.
 */
struct private_tls_socket_t {

	/**
	 * Public tls_socket_t interface.
	 */
	tls_socket_t public;

	/**
	 * TLS application implementation
	 */
	private_tls_application_t app;

	/**
	 * TLS stack
	 */
	tls_t *tls;

	/**
	 * Underlying OS socket
	 */
	int fd;
};

METHOD(tls_application_t, process, status_t,
	private_tls_application_t *this, bio_reader_t *reader)
{
	chunk_t data;

	if (!reader->read_data(reader, reader->remaining(reader), &data))
	{
		return FAILED;
	}
	this->in = chunk_cat("mc", this->in, data);
	return NEED_MORE;
}

METHOD(tls_application_t, build, status_t,
	private_tls_application_t *this, bio_writer_t *writer)
{
	if (this->out.len)
	{
		writer->write_data(writer, this->out);
		this->out = chunk_empty;
		return NEED_MORE;
	}
	return INVALID_STATE;
}

/**
 * TLS data exchange loop
 */
static bool exchange(private_tls_socket_t *this, bool wr)
{
	char buf[CRYPTO_BUF_SIZE], *pos;
	ssize_t len, out;
	int round = 0;

	for (round = 0; TRUE; round++)
	{
		while (TRUE)
		{
			len = sizeof(buf);
			switch (this->tls->build(this->tls, buf, &len, NULL))
			{
				case NEED_MORE:
				case ALREADY_DONE:
					pos = buf;
					while (len)
					{
						out = write(this->fd, pos, len);
						if (out == -1)
						{
							DBG1(DBG_TLS, "TLS crypto write error: %s",
								 strerror(errno));
							return FALSE;
						}
						len -= out;
						pos += out;
					}
					continue;
				case INVALID_STATE:
					break;
				default:
					return FALSE;
			}
			break;
		}
		if (wr)
		{
			if (this->app.out.len == 0)
			{	/* all data written */
				return TRUE;
			}
		}
		else
		{
			if (this->app.in.len)
			{	/* some data received */
				return TRUE;
			}
			if (round > 0)
			{	/* did some handshaking, return empty chunk to not block */
				return TRUE;
			}
		}
		len = read(this->fd, buf, sizeof(buf));
		if (len <= 0)
		{
			return FALSE;
		}
		if (this->tls->process(this->tls, buf, len) != NEED_MORE)
		{
			return FALSE;
		}
	}
}

METHOD(tls_socket_t, read_, bool,
	private_tls_socket_t *this, chunk_t *buf)
{
	if (exchange(this, FALSE))
	{
		*buf = this->app.in;
		this->app.in = chunk_empty;
		return TRUE;
	}
	return FALSE;
}

METHOD(tls_socket_t, write_, bool,
	private_tls_socket_t *this, chunk_t buf)
{
	this->app.out = buf;
	if (exchange(this, TRUE))
	{
		return TRUE;
	}
	return FALSE;
}

METHOD(tls_socket_t, splice, bool,
	private_tls_socket_t *this, int rfd, int wfd)
{
	char buf[PLAIN_BUF_SIZE], *pos;
	fd_set set;
	chunk_t data;
	ssize_t len;
	bool old;

	while (TRUE)
	{
		FD_ZERO(&set);
		FD_SET(rfd, &set);
		FD_SET(this->fd, &set);

		old = thread_cancelability(TRUE);
		len = select(max(rfd, this->fd) + 1, &set, NULL, NULL, NULL);
		thread_cancelability(old);
		if (len == -1)
		{
			DBG1(DBG_TLS, "TLS select error: %s", strerror(errno));
			return FALSE;
		}
		if (FD_ISSET(this->fd, &set))
		{
			if (!read_(this, &data))
			{
				DBG2(DBG_TLS, "TLS read error/disconnect");
				return TRUE;
			}
			pos = data.ptr;
			while (data.len)
			{
				len = write(wfd, pos, data.len);
				if (len == -1)
				{
					free(data.ptr);
					DBG1(DBG_TLS, "TLS plain write error: %s", strerror(errno));
					return FALSE;
				}
				data.len -= len;
				pos += len;
			}
			free(data.ptr);
		}
		if (FD_ISSET(rfd, &set))
		{
			len = read(rfd, buf, sizeof(buf));
			if (len > 0)
			{
				if (!write_(this, chunk_create(buf, len)))
				{
					DBG1(DBG_TLS, "TLS write error");
					return FALSE;
				}
			}
			else
			{
				if (len < 0)
				{
					DBG1(DBG_TLS, "TLS plain read error: %s", strerror(errno));
					return FALSE;
				}
				return TRUE;
			}
		}
	}
}

METHOD(tls_socket_t, get_fd, int,
	private_tls_socket_t *this)
{
	return this->fd;
}

METHOD(tls_socket_t, destroy, void,
	private_tls_socket_t *this)
{
	this->tls->destroy(this->tls);
	free(this->app.in.ptr);
	free(this);
}

/**
 * See header
 */
tls_socket_t *tls_socket_create(bool is_server, identification_t *server,
							identification_t *peer, int fd, tls_cache_t *cache)
{
	private_tls_socket_t *this;

	INIT(this,
		.public = {
			.read = _read_,
			.write = _write_,
			.splice = _splice,
			.get_fd = _get_fd,
			.destroy = _destroy,
		},
		.app = {
			.application = {
				.build = _build,
				.process = _process,
				.destroy = (void*)nop,
			},
		},
		.fd = fd,
	);

	this->tls = tls_create(is_server, server, peer, TLS_PURPOSE_GENERIC,
						   &this->app.application, cache);
	if (!this->tls)
	{
		free(this);
		return NULL;
	}

	return &this->public;
}