-
Notifications
You must be signed in to change notification settings - Fork 13
/
ssl.c
418 lines (323 loc) · 9.76 KB
/
ssl.c
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/* SPDX-License-Identifier: MIT */
/*
* Author: Jianhui Zhao <[email protected]>
*/
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <glob.h>
#include "ssl/ssl.h"
#include "bufio.h"
enum {
ECO_SSL_OVERTIME = 1 << 0
};
struct eco_ssl_context {
struct ssl_context *ctx;
struct eco_context *eco;
bool is_server;
};
struct eco_ssl_session {
struct eco_ssl_context *ctx;
struct ssl *ssl;
bool insecure;
lua_State *L;
struct ev_timer tmr;
struct ev_io io;
uint8_t flags;
struct {
size_t len;
size_t sent;
const void *data;
} snd;
};
#define ECO_SSL_CTX_MT "eco{ssl-ctx}"
#define ECO_SSL_MT "eco{ssl}"
static int eco_ssl_context_free(lua_State *L)
{
struct eco_ssl_context *ctx = luaL_checkudata(L, 1, ECO_SSL_CTX_MT);
if (!ctx->ctx)
return 0;
ssl_context_free(ctx->ctx);
ctx->ctx = NULL;
return 0;
}
static int eco_ssl_context_gc(lua_State *L)
{
return eco_ssl_context_free(L);
}
static void ev_timer_cb(struct ev_loop *loop, ev_timer *w, int revents)
{
struct eco_ssl_session *s = container_of(w, struct eco_ssl_session, tmr);
ev_io_stop(loop, &s->io);
s->flags |= ECO_SSL_OVERTIME;
eco_resume(s->ctx->eco->L, s->L, 0);
}
static void ev_io_cb(struct ev_loop *loop, ev_io *w, int revents)
{
struct eco_ssl_session *s = container_of(w, struct eco_ssl_session, io);
ev_io_stop(loop, w);
ev_timer_stop(loop, &s->tmr);
eco_resume(s->ctx->eco->L, s->L, 0);
}
static int lua_ssl_free(lua_State *L)
{
struct eco_ssl_session *s = luaL_checkudata(L, 1, ECO_SSL_MT);
if (!s->ssl)
return 0;
ssl_session_free(s->ssl);
s->ssl = NULL;
return 0;
}
static int lua_ssl_pointer(lua_State *L)
{
struct eco_ssl_session *s = luaL_checkudata(L, 1, ECO_SSL_MT);
lua_pushlightuserdata(L, s);
return 1;
}
static int lua_ssl_set_server_name(lua_State *L)
{
struct eco_ssl_session *s = luaL_checkudata(L, 1, ECO_SSL_MT);
const char *name = luaL_checkstring(L, 2);
ssl_set_server_name(s->ssl, name);
return 0;
}
static void on_ssl_verify_error(int error, const char *str, void *arg)
{
*(const char **)arg = str;
}
static int lua_ssl_handshakek(lua_State *L, int status, lua_KContext ctx)
{
struct eco_ssl_session *s = (struct eco_ssl_session *)ctx;
const char *verify_error = NULL;
char err_buf[128];
int ret;
s->L = NULL;
if (s->flags & ECO_SSL_OVERTIME) {
lua_pushnil(L);
lua_pushliteral(L, "timeout");
return 2;
}
if (s->ctx->is_server)
ret = ssl_accept(s->ssl, on_ssl_verify_error, &verify_error);
else
ret = ssl_connect(s->ssl, on_ssl_verify_error, &verify_error);
if (ret < 0) {
if (ret == SSL_ERROR) {
lua_pushnil(L);
lua_pushstring(L, ssl_last_error_string(s->ssl, err_buf, sizeof(err_buf)));
return 2;
}
s->L = L;
ev_timer_set(&s->tmr, 5.0, 0);
ev_timer_start(s->ctx->eco->loop, &s->tmr);
ev_io_modify(&s->io, ret == SSL_WANT_READ ? EV_READ : EV_WRITE);
ev_io_start(s->ctx->eco->loop, &s->io);
return lua_yieldk(L, 0, ctx, lua_ssl_handshakek);
}
if (verify_error && !s->insecure) {
lua_pushnil(L);
lua_pushfstring(L, "SSL certificate verify fail: %s", verify_error);
return 2;
}
lua_pushboolean(L, true);
return 1;
}
static int lua_ssl_handshake(lua_State *L)
{
struct eco_ssl_session *s = luaL_checkudata(L, 1, ECO_SSL_MT);
return lua_ssl_handshakek(L, 0, (lua_KContext)s);
}
static int lua_sendk(lua_State *L, int status, lua_KContext ctx)
{
struct eco_ssl_session *s = (struct eco_ssl_session *)ctx;
const void *data = s->snd.data;
size_t sent = s->snd.sent;
size_t len = s->snd.len;
char err_buf[128];
int ret = 0;
s->L = NULL;
if (sent == len) {
lua_pushinteger(L, sent);
return 1;
}
ret = ssl_write(s->ssl, data, len - sent);
if (unlikely(ret < 0)) {
if (ret == SSL_ERROR) {
lua_pushnil(L);
lua_pushstring(L, ssl_last_error_string(s->ssl, err_buf, sizeof(err_buf)));
return 2;
}
goto again;
}
s->snd.sent += ret;
s->snd.data += ret;
again:
s->L = L;
ev_io_modify(&s->io, ret == SSL_WANT_READ ? EV_READ : EV_WRITE);
ev_io_start(s->ctx->eco->loop, &s->io);
return lua_yieldk(L, 0, ctx, lua_sendk);
}
static int lua_send(lua_State *L)
{
struct eco_ssl_session *s = luaL_checkudata(L, 1, ECO_SSL_MT);
if (!s->ssl) {
lua_pushnil(L);
lua_pushliteral(L, "closed");
return 2;
}
if (s->L) {
lua_pushnil(L);
lua_pushliteral(L, "busy");
return 2;
}
s->snd.data = luaL_checklstring(L, 2, &s->snd.len);
s->snd.sent = 0;
return lua_sendk(L, 0, (lua_KContext)s);
}
static int bufio_fill_ssl(struct eco_bufio *b, lua_State *L, lua_KContext ctx, lua_KFunction k)
{
struct eco_ssl_session *s = (struct eco_ssl_session *)b->ctx;
static char err_buf[128];
ssize_t ret;
buffer_slide(b);
if (buffer_room(b) == 0) {
b->error = "buffer is full";
return -1;
}
ret = ssl_read(s->ssl, b->data + b->w, buffer_room(b));
if (unlikely(ret < 0)) {
if (ret == SSL_ERROR) {
b->error = ssl_last_error_string(s->ssl, err_buf, sizeof(err_buf));
return -1;
}
b->L = L;
if (b->timeout > 0) {
ev_timer_set(&b->tmr, b->timeout, 0);
ev_timer_start(b->eco->loop, &b->tmr);
}
ev_io_modify(&b->io, ret == SSL_WANT_READ ? EV_READ : EV_WRITE);
ev_io_start(b->eco->loop, &b->io);
return lua_yieldk(L, 0, ctx, k);
}
if (ret == 0) {
b->flags.eof = 1;
b->error = "closed";
return -1;
}
b->w += ret;
return ret;
}
static int lua_ssl_session_new(lua_State *L)
{
struct eco_ssl_context *ctx = luaL_checkudata(L, 1, ECO_SSL_CTX_MT);
int fd = luaL_checkinteger(L, 2);
bool insecure = lua_toboolean(L, 3);
struct eco_ssl_session *s;
s = lua_newuserdata(L, sizeof(struct eco_ssl_session));
memset(s, 0, sizeof(struct eco_ssl_session));
lua_pushvalue(L, lua_upvalueindex(1));
lua_setmetatable(L, -2);
memset(s, 0, sizeof(struct eco_ssl_session));
s->ssl = ssl_session_new(ctx->ctx, fd);
s->insecure = insecure;
s->ctx = ctx;
ev_timer_init(&s->tmr, ev_timer_cb, 0.0, 0);
ev_io_init(&s->io, ev_io_cb, fd, 0);
return 1;
}
static int eco_ssl_load_ca_cert_file(lua_State *L)
{
struct eco_ssl_context *ctx = luaL_checkudata(L, 1, ECO_SSL_CTX_MT);
const char *file = luaL_checkstring(L, 2);
lua_pushboolean(L, !!!ssl_load_ca_cert_file(ctx->ctx, file));
return 1;
}
static int eco_ssl_load_cert_file(lua_State *L)
{
struct eco_ssl_context *ctx = luaL_checkudata(L, 1, ECO_SSL_CTX_MT);
const char *file = luaL_checkstring(L, 2);
lua_pushboolean(L, !!!ssl_load_cert_file(ctx->ctx, file));
return 1;
}
static int eco_ssl_load_key_file(lua_State *L)
{
struct eco_ssl_context *ctx = luaL_checkudata(L, 1, ECO_SSL_CTX_MT);
const char *file = luaL_checkstring(L, 2);
lua_pushboolean(L, !!!ssl_load_key_file(ctx->ctx, file));
return 1;
}
static int eco_ssl_set_ciphers(lua_State *L)
{
struct eco_ssl_context *ctx = luaL_checkudata(L, 1, ECO_SSL_CTX_MT);
const char *ciphers = luaL_checkstring(L, 2);
lua_pushboolean(L, !!!ssl_set_ciphers(ctx->ctx, ciphers));
return 1;
}
static int eco_ssl_set_require_validation(lua_State *L)
{
struct eco_ssl_context *ctx = luaL_checkudata(L, 1, ECO_SSL_CTX_MT);
bool require = lua_toboolean(L, 2);
ssl_set_require_validation(ctx->ctx, require);
return 0;
}
static void load_default_ca_cert(struct ssl_context *ctx)
{
glob_t gl;
size_t i;
glob("/etc/ssl/certs/*.crt", 0, NULL, &gl);
for (i = 0; i < gl.gl_pathc; i++)
ssl_load_ca_cert_file(ctx, gl.gl_pathv[i]);
globfree(&gl);
}
static int lua_ssl_context_new(lua_State *L)
{
bool is_server = lua_toboolean(L, 1);
struct eco_ssl_context *ctx;
ctx = lua_newuserdata(L, sizeof(struct eco_ssl_context));
memset(ctx, 0, sizeof(struct eco_ssl_context));
lua_pushvalue(L, lua_upvalueindex(1));
lua_setmetatable(L, -2);
ctx->eco = eco_get_context(L);
ctx->ctx = ssl_context_new(is_server);
ctx->is_server = is_server;
load_default_ca_cert(ctx->ctx);
return 1;
}
static const struct luaL_Reg ssl_ctx_methods[] = {
{"__gc", eco_ssl_context_gc},
{"free", eco_ssl_context_free},
{"load_ca_cert_file", eco_ssl_load_ca_cert_file},
{"load_cert_file", eco_ssl_load_cert_file},
{"load_key_file", eco_ssl_load_key_file},
{"set_ciphers", eco_ssl_set_ciphers},
{"require_validation", eco_ssl_set_require_validation},
{NULL, NULL}
};
static const struct luaL_Reg ssl_methods[] = {
{"__gc", lua_ssl_free},
{"free", lua_ssl_free},
{"pointer", lua_ssl_pointer},
{"set_server_name", lua_ssl_set_server_name},
{"handshake", lua_ssl_handshake},
{"send", lua_send},
{"write", lua_send},
{NULL, NULL}
};
int luaopen_eco_core_ssl(lua_State *L)
{
lua_newtable(L);
lua_add_constant(L, "OK", SSL_OK);
lua_add_constant(L, "ERROR", SSL_ERROR);
lua_add_constant(L, "WANT_READ", SSL_WANT_READ);
lua_add_constant(L, "WANT_WRITE", SSL_WANT_WRITE);
lua_add_constant(L, "INSECURE", SSL_INSECURE);
lua_pushlightuserdata(L, bufio_fill_ssl);
lua_setfield(L, -2, "bufio_fill");
eco_new_metatable(L, ECO_SSL_CTX_MT, ssl_ctx_methods);
eco_new_metatable(L, ECO_SSL_MT, ssl_methods);
lua_pushcclosure(L, lua_ssl_session_new, 1);
lua_setfield(L, -2, "new");
lua_pushcclosure(L, lua_ssl_context_new, 1);
lua_setfield(L, -2, "context");
return 1;
}