summaryrefslogtreecommitdiffhomepage
path: root/libs/nixio/src/tls-socket.c
blob: 693a2a551281ce24dc92ed0952032b2012bbd743 (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
/*
 * nixio - Linux I/O library for lua
 *
 *   Copyright (C) 2009 Steven Barth <steven@midlink.org>
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#include "nixio-tls.h"
#include <string.h>
#include <stdlib.h>

static int nixio__tls_sock_perror(lua_State *L, SSL *sock, int code) {
	lua_pushnil(L);
	lua_pushinteger(L, SSL_get_error(sock, code));
	return 2;
}

static int nixio__tls_sock_pstatus(lua_State *L, SSL *sock, int code) {
	if (code == 1) {
		lua_pushboolean(L, 1);
		return 1;
	} else {
		return nixio__tls_sock_perror(L, sock, code);
	}
}

static SSL* nixio__checktlssock(lua_State *L) {
	if (lua_istable(L, 1)) {
		lua_getfield(L, 1, "connection");
		lua_replace(L, 1);
	}
	nixio_tls_sock *sock = luaL_checkudata(L, 1, NIXIO_TLS_SOCK_META);
	luaL_argcheck(L, sock->socket, 1, "invalid context");
	return sock->socket;
}

static int nixio_tls_sock_recv(lua_State *L) {
	SSL *sock = nixio__checktlssock(L);
	int req = luaL_checkinteger(L, 2);

	luaL_argcheck(L, req >= 0, 2, "out of range");

	/* We limit the readsize to NIXIO_BUFFERSIZE */
	req = (req > NIXIO_BUFFERSIZE) ? NIXIO_BUFFERSIZE : req;

#ifndef WITH_AXTLS

	char buffer[NIXIO_BUFFERSIZE];
	int readc = SSL_read(sock, buffer, req);

	if (readc < 0) {
		return nixio__tls_sock_pstatus(L, sock, readc);
	} else {
		lua_pushlstring(L, buffer, readc);
		return 1;
	}

#else

	if (!req) {
		lua_pushliteral(L, "");
		return 1;
	}

	nixio_tls_sock *t = lua_touserdata(L, 1);

	/* AXTLS doesn't handle buffering for us, so we have to hack around*/
	if (req < t->pbufsiz) {
		lua_pushlstring(L, t->pbufpos, req);
		t->pbufpos += req;
		t->pbufsiz -= req;
		return 1;
	} else {
		uint8_t *axbuf;
		size_t axread;

		/* while handshake pending */
		while ((axread = ssl_read(sock, &axbuf)) == SSL_OK);

		if (t->pbufsiz) {
			lua_pushlstring(L, t->pbufpos, t->pbufsiz);
		}

		if (axread < 0) {
			/* There is an error */
			free(t->pbuffer);
			t->pbuffer = t->pbufpos = NULL;

			if (axread != SSL_ERROR_CONN_LOST) {
				t->pbufsiz = 0;
				return nixio__tls_sock_perror(L, sock, axread);
			} else {
				if (!t->pbufsiz) {
					lua_pushliteral(L, "");
				} else {
					t->pbufsiz = 0;
				}
			}
		} else {
			int stillwant = req - t->pbufsiz;
			if (stillwant < axread) {
				/* we got more data than we need */
				lua_pushlstring(L, (char *)axbuf, stillwant);
				if(t->pbufsiz) {
					lua_concat(L, 2);
				}

				/* remaining data goes into the buffer */
				t->pbufpos = t->pbuffer;
				t->pbufsiz = axread - stillwant;
				t->pbuffer = realloc(t->pbuffer, t->pbufsiz);
				if (!t->pbuffer) {
					free(t->pbufpos);
					t->pbufpos = NULL;
					t->pbufsiz = 0;
					return luaL_error(L, "out of memory");
				}

				t->pbufpos = t->pbuffer;
				memcpy(t->pbufpos, axbuf + stillwant, t->pbufsiz);
			} else {
				lua_pushlstring(L, (char *)axbuf, axread);
				if(t->pbufsiz) {
					lua_concat(L, 2);
				}

				/* free buffer */
				free(t->pbuffer);
				t->pbuffer = t->pbufpos = NULL;
				t->pbufsiz = 0;
			}
		}
		return 1;
	}

#endif /* WITH_AXTLS */

}

static int nixio_tls_sock_send(lua_State *L) {
	SSL *sock = nixio__checktlssock(L);
	size_t len;
	ssize_t sent;
	const char *data = luaL_checklstring(L, 2, &len);
	sent = SSL_write(sock, data, len);
	if (sent > 0) {
		lua_pushinteger(L, sent);
		return 1;
	} else {
		return nixio__tls_sock_pstatus(L, sock, len);
	}
}

static int nixio_tls_sock_accept(lua_State *L) {
	SSL *sock = nixio__checktlssock(L);
	return nixio__tls_sock_pstatus(L, sock, SSL_accept(sock));
}

static int nixio_tls_sock_connect(lua_State *L) {
	SSL *sock = nixio__checktlssock(L);
	return nixio__tls_sock_pstatus(L, sock, SSL_connect(sock));
}

static int nixio_tls_sock_shutdown(lua_State *L) {
	SSL *sock = nixio__checktlssock(L);
	return nixio__tls_sock_pstatus(L, sock, SSL_shutdown(sock));
}

static int nixio_tls_sock__gc(lua_State *L) {
	nixio_tls_sock *sock = luaL_checkudata(L, 1, NIXIO_TLS_SOCK_META);
	if (sock->socket) {
		SSL_free(sock->socket);
		sock->socket = NULL;
#ifdef WITH_AXTLS
		free(sock->pbuffer);
#endif
	}
	return 0;
}

static int nixio_tls_sock__tostring(lua_State *L) {
	SSL *sock = nixio__checktlssock(L);
	lua_pushfstring(L, "nixio TLS connection: %p", sock);
	return 1;
}


/* ctx function table */
static const luaL_reg M[] = {
	{"recv", 		nixio_tls_sock_recv},
	{"send", 		nixio_tls_sock_send},
	{"read", 		nixio_tls_sock_recv},
	{"write", 		nixio_tls_sock_send},
	{"accept",	 	nixio_tls_sock_accept},
	{"connect", 	nixio_tls_sock_connect},
	{"shutdown", 	nixio_tls_sock_shutdown},
	{"__gc",		nixio_tls_sock__gc},
	{"__tostring",	nixio_tls_sock__tostring},
	{NULL,			NULL}
};


void nixio_open_tls_socket(lua_State *L) {
	/* create socket metatable */
	luaL_newmetatable(L, NIXIO_TLS_SOCK_META);
	luaL_register(L, NULL, M);
	lua_pushvalue(L, -1);
	lua_setfield(L, -2, "__index");
	lua_setfield(L, -2, "meta_tls_socket");
}