This repository has been archived by the owner on Feb 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsslsocket.lua
executable file
·551 lines (448 loc) · 15 KB
/
sslsocket.lua
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#!/usr/bin/env tarantool
local TIMEOUT_INFINITY = 500 * 365 * 86400
local LIMIT_INFINITY = 2147483647
local log = require('log')
local ffi = require('ffi')
local socket = require('socket')
local buffer = require('buffer')
local clock = require('clock')
local errno = require('errno')
ffi.cdef[[
typedef struct SSL_METHOD {} SSL_METHOD;
typedef struct SSL_CTX {} SSL_CTX;
typedef struct SSL {} SSL;
const SSL_METHOD *TLS_method(void);
const SSL_METHOD *TLS_server_method(void);
const SSL_METHOD *TLS_client_method(void);
const SSL_METHOD *TLSv1_method(void);
const SSL_METHOD *TLSv1_server_method(void);
const SSL_METHOD *TLSv1_client_method(void);
const SSL_METHOD *TLSv1_1_method(void);
const SSL_METHOD *TLSv1_1_server_method(void);
const SSL_METHOD *TLSv1_1_client_method(void);
const SSL_METHOD *TLSv1_2_method(void);
const SSL_METHOD *TLSv1_2_server_method(void);
const SSL_METHOD *TLSv1_2_client_method(void);
const SSL_METHOD *DTLS_method(void);
const SSL_METHOD *DTLS_server_method(void);
const SSL_METHOD *DTLS_client_method(void);
const SSL_METHOD *DTLSv1_method(void);
const SSL_METHOD *DTLSv1_server_method(void);
const SSL_METHOD *DTLSv1_client_method(void);
const SSL_METHOD *DTLSv1_2_method(void);
const SSL_METHOD *DTLSv1_2_server_method(void);
const SSL_METHOD *DTLSv1_2_client_method(void);
SSL_CTX *SSL_CTX_new(const SSL_METHOD *method);
int SSL_CTX_up_ref(SSL_CTX *ctx);
void SSL_CTX_free(SSL_CTX *);
int SSL_shutdown(SSL *ssl);
int SSL_CTX_use_certificate_file(SSL_CTX *ctx, const char *file, int type);
int SSL_CTX_use_PrivateKey_file(SSL_CTX *ctx, const char *file, int type);
SSL *SSL_new(SSL_CTX *ctx);
void SSL_free(SSL *ssl);
int SSL_set_fd(SSL *s, int fd);
void SSL_set_connect_state(SSL *s);
void SSL_set_accept_state(SSL *s);
int SSL_write(SSL *ssl, const void *buf, int num);
int SSL_read(SSL *ssl, void *buf, int num);
int SSL_pending(const SSL *ssl);
int SSL_has_pending(const SSL *s);
void ERR_clear_error(void);
char *ERR_error_string(unsigned long e, char *buf);
unsigned long ERR_peek_last_error(void);
int SSL_get_error(const SSL *s, int ret_code);
typedef socklen_t uint32;
int getsockopt(int sockfd, int level, int optname, void *optval,
socklen_t *optlen);
int setsockopt(int sockfd, int level, int optname,
const void *optval, socklen_t optlen);
void *memmem(const void *haystack, size_t haystacklen,
const void *needle, size_t needlelen);
]]
pcall(
function()
ffi.cdef([[
int SSL_library_init(void);
void SSL_load_error_strings(void);
const SSL_METHOD *SSLv23_method(void);
const SSL_METHOD *SSLv23_server_method(void);
const SSL_METHOD *SSLv23_client_method(void);
const SSL_METHOD *SSLv3_method(void);
const SSL_METHOD *SSLv3_server_method(void);
const SSL_METHOD *SSLv3_client_method(void);
]])
ffi.C.SSL_library_init()
ffi.C.SSL_load_error_strings()
end)
local methods = {
tlsv1 = ffi.C.TLSv1_method(),
tlsv11 = ffi.C.TLSv1_1_method(),
tlsv12 = ffi.C.TLSv1_2_method(),
}
local function slice_wait(timeout, starttime)
if timeout == nil then
return nil
end
return timeout - (clock.time() - starttime)
end
local X509_FILETYPE_PEM =1
local X509_FILETYPE_ASN1 =2
local X509_FILETYPE_DEFAULT =3
local function ctx(method)
method = method or methods['tlsv1']
ffi.C.ERR_clear_error()
local newctx =
ffi.gc(ffi.C.SSL_CTX_new(method), ffi.C.SSL_CTX_free)
return newctx
end
local function ctx_use_private_key_file(ctx, pem_file)
if ffi.C.SSL_CTX_use_PrivateKey_file(ctx, pem_file, X509_FILETYPE_PEM) ~= 1 then
return false
end
return true
end
local function ctx_use_certificate_file(ctx, pem_file)
if ffi.C.SSL_CTX_use_certificate_file(ctx, pem_file, X509_FILETYPE_PEM) ~= 1 then
return false
end
return true
end
local default_ctx = ctx(methods.sslv23)
local SSL_ERROR_NONE =0
local SSL_ERROR_SSL =1
local SSL_ERROR_WANT_READ =2
local SSL_ERROR_WANT_WRITE =3
local SSL_ERROR_WANT_X509_LOOKUP =4
local SSL_ERROR_SYSCALL =5 -- look at error stack/return value/errno
local SSL_ERROR_ZERO_RETURN =6
local SSL_ERROR_WANT_CONNECT =7
local SSL_ERROR_WANT_ACCEPT =8
local sslsocket = {
__newindex = function(table, key, value)
error("Attempt to modify read-only sslsocket properties "..key)
end,
}
sslsocket.__index = sslsocket
local WAIT_FOR_READ =1
local WAIT_FOR_WRITE =2
function sslsocket.write(self, data, timeout)
local start = clock.time()
local size = #data
local s = ffi.cast('const char *', data)
local mode = WAIT_FOR_WRITE
while true do
local rc = nil
if mode == WAIT_FOR_READ then
rc = self.sock:readable(slice_wait(timeout, start))
elseif mode == WAIT_FOR_WRITE then
rc = self.sock:writable(slice_wait(timeout, start))
else
assert(false)
end
if not rc then
self.sock._errno = errno.ETIMEDOUT
return nil, 'Timeout exceeded'
end
ffi.C.ERR_clear_error()
local num = ffi.C.SSL_write(self.ssl, s, size);
if num <= 0 then
local ssl_error = ffi.C.SSL_get_error(self.ssl, num);
if ssl_error == SSL_ERROR_WANT_WRITE then
mode = WAIT_FOR_WRITE
elseif ssl_error == SSL_ERROR_WANT_READ then
mode = WAIT_FOR_READ
elseif ssl_error == SSL_ERROR_SYSCALL then
return nil, self.sock:error()
elseif ssl_error == SSL_ERROR_ZERO_RETURN then
return 0
else
local error_string = ffi.string(ffi.C.ERR_error_string(ssl_error, nil))
return nil, error_string
end
else
return num
end
end
end
function sslsocket.shutdown(self, timeout)
local start = clock.time()
ffi.C.ERR_clear_error()
local rc = ffi.C.SSL_shutdown(self.ssl) -- ignore result
while rc < 0 do
local mode
local ssl_error = ffi.C.SSL_get_error(self.ssl, rc);
if ssl_error == SSL_ERROR_WANT_WRITE then
mode = WAIT_FOR_WRITE
elseif ssl_error == SSL_ERROR_WANT_READ then
mode = WAIT_FOR_READ
elseif ssl_error == SSL_ERROR_SYSCALL then
return nil, self.sock:error()
elseif ssl_error == SSL_ERROR_ZERO_RETURN then
return 0
else
local error_string = ffi.string(ffi.C.ERR_error_string(ssl_error, nil))
return nil, error_string
end
local waited = nil
if mode == WAIT_FOR_READ then
waited = self.sock:readable(slice_wait(timeout, start))
elseif mode == WAIT_FOR_WRITE then
waited = self.sock:writable(slice_wait(timeout, start))
else
assert(false)
end
if not waited then
self.sock._errno = errno.ETIMEDOUT
return nil, 'Timeout exceeded'
end
rc = ffi.C.SSL_shutdown(self.ssl) -- ignore result
end
-- TODO Is this possible case for async socket?
if rc == 0 then
ffi.C.SSL_shutdown(self.ssl)
end
self.sock:shutdown(socket.SHUT_RDWR)
return true
end
function sslsocket.close(self)
return self.sock:close()
end
function sslsocket.error(self)
local error_string =
ffi.string(ffi.C.ERR_error_string(ffi.C.ERR_peek_last_error(), nil))
return self.sock:error() or error_string
end
function sslsocket.errno(self)
return self.sock:errno() or ffi.C.ERR_peek_last_error()
end
function sslsocket.setsockopt(self, level, name, value)
return self.sock:setsockopt(level, name, value)
end
function sslsocket.getsockopt(self, level, name)
return self.sock:getsockopt(level, name)
end
function sslsocket.name(self)
return self.sock:name()
end
function sslsocket.peer(self)
return self.sock:peer()
end
function sslsocket.fd(self)
return self.sock:fd()
end
function sslsocket.nonblock(self, nb)
return self.sock:nonblock(nb)
end
local function sysread(self, charptr, size, timeout)
local start = clock.time()
local mode = rawget(self, 'first_state') or WAIT_FOR_READ
rawset(self, 'first_state', nil)
while true do
local rc = nil
if mode == WAIT_FOR_READ then
if ffi.C.SSL_pending(self.ssl) > 0 then
rc = true
else
rc = self.sock:readable(slice_wait(timeout, start))
end
elseif mode == WAIT_FOR_WRITE then
rc = self.sock:writable(slice_wait(timeout, start))
else
assert(false)
end
if not rc then
self.sock._errno = errno.ETIMEDOUT
return nil, 'Timeout exceeded'
end
ffi.C.ERR_clear_error()
local num = ffi.C.SSL_read(self.ssl, charptr, size);
if num <= 0 then
local ssl_error = ffi.C.SSL_get_error(self.ssl, num);
if ssl_error == SSL_ERROR_WANT_WRITE then
mode = WAIT_FOR_WRITE
elseif ssl_error == SSL_ERROR_WANT_READ then
mode = WAIT_FOR_READ
elseif ssl_error == SSL_ERROR_SYSCALL then
return nil, self.sock:error()
elseif ssl_error == SSL_ERROR_ZERO_RETURN then
return 0
else
local error_string = ffi.string(ffi.C.ERR_error_string(ssl_error, nil))
return nil, error_string
end
else
return num
end
end
end
local function read(self, limit, timeout, check, ...)
assert(limit >= 0)
local start = clock.time()
limit = math.min(limit, LIMIT_INFINITY)
local rbuf = self.rbuf
if rbuf == nil then
rbuf = buffer.ibuf()
rawset(self, 'rbuf', rbuf)
end
local len = check(self, limit, ...)
if len ~= nil then
local data = ffi.string(rbuf.rpos, len)
rbuf.rpos = rbuf.rpos + len
return data
end
while true do
assert(rbuf:size() < limit)
local to_read = math.min(limit - rbuf:size(), buffer.READAHEAD)
local data = rbuf:reserve(to_read)
assert(rbuf:unused() >= to_read)
local res, err = sysread(self, data, rbuf:unused(), slice_wait(timeout, start))
if res == 0 then -- eof
local len = rbuf:size()
local data = ffi.string(rbuf.rpos, len)
rbuf.rpos = rbuf.rpos + len
return data
elseif res ~= nil then
rbuf.wpos = rbuf.wpos + res
local len = check(self, limit, ...)
if len ~= nil then
local data = ffi.string(rbuf.rpos, len)
rbuf.rpos = rbuf.rpos + len
return data
end
else
return nil, err
end
end
-- return nil
end
local function check_limit(self, limit)
if self.rbuf:size() >= limit then
return limit
end
return nil
end
local function check_delimiter(self, limit, eols)
if limit == 0 then
return 0
end
local rbuf = self.rbuf
if rbuf:size() == 0 then
return nil
end
local shortest
for i, eol in ipairs(eols) do
local data = ffi.C.memmem(rbuf.rpos, rbuf:size(), eol, #eol)
if data ~= nil then
local len = ffi.cast('char *', data) - rbuf.rpos + #eol
if shortest == nil or shortest > len then
shortest = len
end
end
end
if shortest ~= nil and shortest <= limit then
return shortest
elseif limit <= rbuf:size() then
return limit
end
return nil
end
function sslsocket.read(self, opts, timeout)
timeout = timeout or TIMEOUT_INFINITY
if type(opts) == 'number' then
return read(self, opts, timeout, check_limit)
elseif type(opts) == 'string' then
return read(self, LIMIT_INFINITY, timeout, check_delimiter, { opts })
elseif type(opts) == 'table' then
local chunk = opts.chunk or opts.size or LIMIT_INFINITY
local delimiter = opts.delimiter or opts.line
if delimiter == nil then
return read(self, chunk, timeout, check_limit)
elseif type(delimiter) == 'string' then
return read(self, chunk, timeout, check_delimiter, { delimiter })
elseif type(delimiter) == 'table' then
return read(self, chunk, timeout, check_delimiter, delimiter)
end
end
error('Usage: s:read(delimiter|chunk|{delimiter = x, chunk = x}, timeout)')
end
local function tcp_connect(host, port, timeout, sslctx)
sslctx = sslctx or default_ctx
ffi.C.ERR_clear_error()
local ssl = ffi.gc(ffi.C.SSL_new(sslctx),
ffi.C.SSL_free)
if ssl == nil then
return nil, 'SSL_new failed'
end
local sock = socket.tcp_connect(host, port, timeout)
if not sock then
return nil, 'tcp connect failed'
end
sock:nonblock(true)
ffi.C.ERR_clear_error()
local rc = ffi.C.SSL_set_fd(ssl, sock:fd())
if rc == 0 then
sock:close()
return nil, 'SSL_set_fd failed'
end
ffi.C.ERR_clear_error()
ffi.C.SSL_set_connect_state(ssl);
local self = setmetatable({}, sslsocket)
rawset(self, 'sock', sock)
rawset(self, 'ctx', sslctx)
rawset(self, 'ssl', ssl)
rawset(self, 'first_state', WAIT_FOR_WRITE)
return self
end
local function wrap_accepted_socket(sock, sslctx)
sslctx = sslctx or default_ctx
ffi.C.ERR_clear_error()
local ssl = ffi.gc(ffi.C.SSL_new(sslctx),
ffi.C.SSL_free)
if ssl == nil then
sock:close()
return nil, 'SSL_new failed'
end
sock:nonblock(true)
ffi.C.ERR_clear_error()
local rc = ffi.C.SSL_set_fd(ssl, sock:fd())
if rc == 0 then
sock:close()
return nil, 'SSL_set_fd failed'
end
ffi.C.ERR_clear_error()
ffi.C.SSL_set_accept_state(ssl);
local self = setmetatable({}, sslsocket)
rawset(self, 'sock', sock)
rawset(self, 'ctx', sslctx)
rawset(self, 'ssl', ssl)
return self
end
local function tcp_server(host, port, handler_function, timeout, sslctx)
sslctx = sslctx or default_ctx
local wrapper = function (sock, from)
local self, err = wrap_accepted_socket(sock, sslctx)
if not self then
log.info('sslsocket.tcp_server error: %s ', err)
else
handler_function(self, from)
end
end
return socket.tcp_server(host, port, wrapper, timeout)
end
local function accept(server, sslctx)
local sock = server:accept()
if sock == nil then
return nil
end
return wrap_accepted_socket(sock, sslctx)
end
return {
methods = methods,
ctx = ctx,
ctx_use_private_key_file = ctx_use_private_key_file,
ctx_use_certificate_file = ctx_use_certificate_file,
tcp_connect = tcp_connect,
tcp_server = tcp_server,
wrap_accepted_socket = wrap_accepted_socket,
accept = accept,
}