Не удаётся запустить эхо-сервер из примера
a1div0 opened this issue · comments
Проблема - не работает
Добрый день!
Целый день бьюсь - не могу понять что не так. Не работает.
Сценарий 1
Запускаю код из примера echo_server.lua
. Как есть, без изменений. Сертификат беру оттуда же. Запускаю на настоящем сервере. Проверку осуществляю командой:
openssl s_client -connect mydns.me:443 -state -nbio 2>&1 | grep "^SSL"
На 30й строке
log.info('getaddrinfo: %s', yaml.encode(client:getaddrinfo()))
падает с ошибкой:
ssl-test.lua:30: attempt to call method 'getaddrinfo' (a nil value)
Сценарий 2
Комментирую эту строку (30). Ошибка возникает на следующей
local buf, err = client:read(10)
дальше которой я продвинуться не смог. В err
лежит вот это:
error:00000001:lib(0):func(0):reason(1)
Вывод клиента вот такой:
openssl s_client -connect vpoint.me:443 -state -nbio 2>&1 | grep "^SSL"
SSL_connect:before SSL initialization
SSL_connect:SSLv3/TLS write client hello
SSL_connect:error in SSLv3/TLS write client hello
SSL_connect:error in SSLv3/TLS write client hello
SSL handshake has read 0 bytes and written 301 bytes
Вывод примера соответственно вот такой:
started
entering the event loop
client accepted ---
host: 212.74.201.232
family: AF_INET
port: 59489
...
client error error:00000001:lib(0):func(0):reason(1)
Пробовал разные сертификаты, в том числе свой, выданный на доменное имя, пробовал стучаться с браузера по-разному. Каждый раз результат один и тот же: error:00000001:lib(0):func(0):reason(1)
В проекте используется какой-то код на C (нужно уйти вглубь SSL_read
) и у меня нет понимания как отследить причину ошибки.
int SSL_read(SSL *ssl, void *buf, int num); // оно ломается здесь
Помогите пожалуйста разобраться!
Is there anybody here?
The first one looks as a mistake, it works for me after the following change:
diff --git a/example/echo_server.lua b/example/echo_server.lua
index 854534c..254882b 100755
--- a/example/echo_server.lua
+++ b/example/echo_server.lua
@@ -26,7 +26,6 @@ sslsocket.tcp_server(
'0.0.0.0', 8443,
function(client, from)
log.info('client accepted %s', yaml.encode(from))
- log.info('getaddrinfo: %s', yaml.encode(client:getaddrinfo()))
local buf, err = client:read(10)
if buf == nil then
log.info('client error %s', err)
Regarding the second: I don't know. I can suggest the following:
- Try to ask the author (@filonenko-mikhail).
- Pack some simple run-and-see reproducer and ping me again.
Сценарий 2
можно попробовать настроить метод шифрования — поставить повыше tlsv1
почему-то оно не умеет апгрейдить протокол само на лету
Спасибо большое! Это сработало!
local ctx = sslsocket.ctx(sslsocket.methods.tlsv12)
Can't reproduce the problem, so I'll just leave my findings here.
Show more debug info
diff --git a/sslsocket.lua b/sslsocket.lua
index 293a0c8..2c34341 100755
--- a/sslsocket.lua
+++ b/sslsocket.lua
@@ -72,6 +72,9 @@ ffi.cdef[[
int SSL_get_error(const SSL *s, int ret_code);
+ FILE *stderr;
+ void ERR_print_errors_fp(FILE *fp);
+
typedef socklen_t uint32;
int getsockopt(int sockfd, int level, int optname, void *optval,
socklen_t *optlen);
@@ -338,6 +349,7 @@ local function sysread(self, charptr, size, timeout)
return 0
else
local error_string = ffi.string(ffi.C.ERR_error_string(ssl_error, nil))
+ ffi.C.ERR_print_errors_fp(ffi.C.stderr)
return nil, error_string
end
else
Set OpenSSL default TLS versions by default
Maybe we should implement something like this, but since I can't reproduce the problem, I don't know for sure.
diff --git a/example/echo_server.lua b/example/echo_server.lua
index 854534c..b3cd690 100755
--- a/example/echo_server.lua
+++ b/example/echo_server.lua
@@ -9,7 +9,7 @@ local function script_path()
local str = debug.getinfo(2, "S").source:sub(2)
return str:match("(.*/)")
end
-local ctx = sslsocket.ctx(sslsocket.methods.tlsv1)
+local ctx = sslsocket.ctx()
local rc = sslsocket.ctx_use_private_key_file(ctx, script_path() .. 'certificate.pem')
if rc == false then
log.info('Certificate is invalid')
diff --git a/sslsocket.lua b/sslsocket.lua
index 293a0c8..7d72846 100755
--- a/sslsocket.lua
+++ b/sslsocket.lua
@@ -105,6 +105,14 @@ end)
local methods = {
+ -- It is OpenSSL default set of acceptable TLS methods.
+ --
+ -- For OpenSSL 1.1.1 and OpenSSL 3.0 it means SSLv3, TLSv1,
+ -- TLSv1.1, TLSv1.2 and TLSv1.3.
+ --
+ -- https://www.openssl.org/docs/man3.0/man3/SSL_CTX_new.html#TLS_method-TLS_server_method-TLS_client_method
+ default = ffi.C.TLS_method(),
+
tlsv1 = ffi.C.TLSv1_method(),
tlsv11 = ffi.C.TLSv1_1_method(),
tlsv12 = ffi.C.TLSv1_2_method(),
@@ -124,7 +132,7 @@ local X509_FILETYPE_ASN1 =2
local X509_FILETYPE_DEFAULT =3
local function ctx(method)
- method = method or methods['tlsv1']
+ method = method or methods['default']
ffi.C.ERR_clear_error()
local newctx =
@@ -147,7 +155,7 @@ local function ctx_use_certificate_file(ctx, pem_file)
return true
end
-local default_ctx = ctx(methods.sslv23)
+local default_ctx = ctx()
local SSL_ERROR_NONE =0
local SSL_ERROR_SSL =1
diff --git a/test/basic_ssl_test.lua b/test/basic_ssl_test.lua
index 3ed0592..0988090 100644
--- a/test/basic_ssl_test.lua
+++ b/test/basic_ssl_test.lua
@@ -7,7 +7,7 @@ local g = t.group('sslsocket')
local sslsocket = require('sslsocket')
g.test_simple_echo = function()
- local ctx = sslsocket.ctx(sslsocket.methods.tlsv1)
+ local ctx = sslsocket.ctx()
local rc = sslsocket.ctx_use_private_key_file(ctx, 'test/cert.pem')
t.assert(rc == true, 'Private key is invalid')
rc = sslsocket.ctx_use_certificate_file(ctx, 'test/cert.pem')
@@ -24,7 +24,7 @@ g.test_simple_echo = function()
t.assert(server ~= nil, "Something wrong when tcp server creation")
- local clientctx = sslsocket.ctx(sslsocket.methods.tlsv1)
+ local clientctx = sslsocket.ctx()
local socket, err = sslsocket.tcp_connect('0.0.0.0', 8443, 10, clientctx)
t.assert(err == nil, 'Error create client socket')
local data = socket:read(5, 1)
CA certs
The example/
and test/
directories do not contain CA certificates, but, it seems, they should. At least if I want to test it with curl
like so:
$ curl -v --insecure -D - --cacert <...> https://127.0.0.1:8443
--insecure
still needed, because curl
complains about self-signed certificate.