Skip to content

Commit c15cfea

Browse files
spacewanderagentzh
authored andcommitted
tests: added old OpenSSL clients to test our SSL server features using new OpenSSL versions.
Here we make sure the nginx process is not blocked by pipe reading operations until the `openssl s_client` completes the SSL handshake. Also, the `openssl s_client` command is always protected by the `timeout` command-line utility to avoid hanging forever. We would no longer need such hacks once we have the nonblocking ngx.pipe Lua API. Signed-off-by: Yichun Zhang (agentzh) <[email protected]>
1 parent aa217ad commit c15cfea

File tree

3 files changed

+216
-1
lines changed

3 files changed

+216
-1
lines changed

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ env:
4040
- TEST_NGINX_SLEEP=0.005
4141
- TEST_NGINX_RANDOMIZE=1
4242
- LUACHECK_VER=0.21.1
43+
- OLD_OPENSSL_VER=0.9.8o
4344
matrix:
4445
- NGINX_VERSION=1.13.6 OPENSSL_VER=1.0.2n OPENSSL_PATCH_VER=1.0.2h
4546
- NGINX_VERSION=1.13.6 OPENSSL_VER=1.1.0g OPENSSL_PATCH_VER=1.1.0d
@@ -54,6 +55,7 @@ before_install:
5455
install:
5556
- if [ ! -d download-cache ]; then mkdir download-cache; fi
5657
- if [ ! -f download-cache/openssl-$OPENSSL_VER.tar.gz ]; then wget -O download-cache/openssl-$OPENSSL_VER.tar.gz https://www.openssl.org/source/openssl-$OPENSSL_VER.tar.gz; fi
58+
- if [ ! -f download-cache/old-openssl-$OLD_OPENSSL_VER.tar.gz ]; then wget -O download-cache/old-openssl-$OLD_OPENSSL_VER.tar.gz https://www.openssl.org/source/openssl-$OLD_OPENSSL_VER.tar.gz; fi
5759
- if [ ! -f download-cache/pcre-$PCRE_VER.tar.gz ]; then wget -P download-cache http://ftp.cs.stanford.edu/pub/exim/pcre/pcre-$PCRE_VER.tar.gz; fi
5860
- wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz
5961
- git clone https://github.com/openresty/openresty.git ../openresty
@@ -88,6 +90,13 @@ script:
8890
- make -j$JOBS > build.log 2>&1 || (cat build.log && exit 1)
8991
- sudo PATH=$PATH make install > build.log 2>&1 || (cat build.log && exit 1)
9092
- cd ..
93+
- tar zxf download-cache/old-openssl-$OLD_OPENSSL_VER.tar.gz
94+
- cd openssl-$OLD_OPENSSL_VER/
95+
- ./config no-asm > build.log 2>&1 || (cat build.log && exit 1)
96+
- make -j$JOBS > build.log 2>&1 || (cat build.log && exit 1)
97+
- export PATH=$PWD/apps:$PATH
98+
- openssl version
99+
- cd ..
91100
- export PATH=$PWD/work/nginx/sbin:$PWD/openresty-devel-utils:$PATH
92101
- export LD_PRELOAD=$PWD/mockeagain/mockeagain.so
93102
- export LD_LIBRARY_PATH=$PWD/mockeagain:$LD_LIBRARY_PATH

t/ssl-session-fetch.t

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,25 @@ use File::Basename;
77
#worker_connections(10140);
88
#workers(1);
99
#log_level('warn');
10+
#master_on();
1011

1112
repeat_each(2);
1213

13-
plan tests => repeat_each() * (blocks() * 6);
14+
plan tests => repeat_each() * (blocks() * 6 + 1);
1415

1516
our $CWD = cwd();
1617

1718
no_long_string();
1819
#no_diff();
1920

21+
env_to_nginx("PATH=" . $ENV{'PATH'});
2022
$ENV{TEST_NGINX_LUA_PACKAGE_PATH} = "$::CWD/lib/?.lua;;";
2123
$ENV{TEST_NGINX_HTML_DIR} ||= html_dir();
2224

2325
$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;
2426
$ENV{TEST_NGINX_RESOLVER} ||= '8.8.8.8';
2527
$ENV{TEST_NGINX_CERT_DIR} ||= dirname(realpath(abs_path(__FILE__)));
28+
$ENV{TEST_NGINX_SERVER_SSL_PORT} ||= 4443;
2629

2730
run_tests();
2831

@@ -398,3 +401,109 @@ $/s,
398401
[alert]
399402
[emerg]
400403
[error]
404+
405+
406+
407+
=== TEST 5: yield during doing handshake with client which uses low version OpenSSL
408+
--- no_check_leak
409+
--- http_config
410+
lua_shared_dict done 16k;
411+
lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH/?.lua;;";
412+
ssl_session_store_by_lua_block {
413+
local ssl = require "ngx.ssl.session"
414+
415+
local sid = ssl.get_session_id()
416+
print("session id: ", sid)
417+
418+
ngx.shared.done:set("handshake", true)
419+
}
420+
421+
ssl_session_fetch_by_lua_block {
422+
local ssl = require "ngx.ssl.session"
423+
424+
ngx.sleep(0.01) -- yield
425+
426+
local sid = ssl.get_session_id()
427+
print("session id: ", sid)
428+
local sess = "==garbage data=="
429+
local ok, err = ssl.set_serialized_session(sess)
430+
if not ok or err then
431+
print("failed to resume session: ", err)
432+
end
433+
434+
-- ngx.shared.done:set("handshake", true)
435+
}
436+
437+
server {
438+
listen $TEST_NGINX_SERVER_SSL_PORT ssl;
439+
server_name test.com;
440+
ssl_session_tickets off;
441+
ssl_certificate $TEST_NGINX_CERT_DIR/cert/test.crt;
442+
ssl_certificate_key $TEST_NGINX_CERT_DIR/cert/test.key;
443+
}
444+
--- config
445+
lua_ssl_trusted_certificate $TEST_NGINX_CERT_DIR/cert/test.crt;
446+
447+
location /t {
448+
set $sess_file $TEST_NGINX_HTML_DIR/sess;
449+
set $addr 127.0.0.1:$TEST_NGINX_SERVER_SSL_PORT;
450+
content_by_lua_block {
451+
ngx.shared.done:delete("handshake")
452+
local addr = ngx.var.addr;
453+
local sess = ngx.var.sess_file
454+
local f, err
455+
if not package.loaded.session then
456+
f, err = io.popen("echo 'Q' | timeout 3s openssl s_client -connect " .. addr .. " -sess_out " .. sess)
457+
package.loaded.session = true
458+
else
459+
f, err = io.popen("echo 'Q' | timeout 3s openssl s_client -connect " .. addr .. " -sess_in " .. sess)
460+
end
461+
462+
if not f then
463+
ngx.say(err)
464+
return
465+
end
466+
467+
local step = 0.001
468+
while step < 1 do
469+
ngx.sleep(step)
470+
step = step * 2
471+
472+
if ngx.shared.done:get("handshake") then
473+
local out = f:read('*a')
474+
ngx.log(ngx.INFO, out)
475+
ngx.say("ok")
476+
f:close()
477+
return
478+
end
479+
end
480+
481+
ngx.log(ngx.ERR, "openssl client handshake timeout")
482+
}
483+
}
484+
485+
--- request
486+
GET /t
487+
--- response_body
488+
ok
489+
--- error_log eval
490+
qr/content_by_lua\(nginx\.conf:\d+\):\d+: CONNECTED/
491+
--- grep_error_log eval
492+
qr/failed to resume session: failed to de-serialize session|ssl_session_(fetch|store)_by_lua_block:\d+: session id: [a-fA-F\d]+/s
493+
--- grep_error_log_out eval
494+
[
495+
qr/^ssl_session_store_by_lua_block:\d+: session id: [a-fA-F\d]+$/s,
496+
qr/^ssl_session_fetch_by_lua_block:\d+: session id: [a-fA-F\d]+
497+
failed to resume session: failed to de-serialize session
498+
ssl_session_store_by_lua_block:\d+: session id: [a-fA-F\d]+
499+
$/s,
500+
qr/ssl_session_fetch_by_lua_block:\d+: session id: [a-fA-F\d]+
501+
failed to resume session: failed to de-serialize session
502+
ssl_session_store_by_lua_block:\d+: session id: [a-fA-F\d]+
503+
$/s,
504+
]
505+
506+
--- no_error_log
507+
[alert]
508+
[emerg]
509+
[error]

t/ssl.t

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ our $CWD = cwd();
1616
no_long_string();
1717
#no_diff();
1818

19+
env_to_nginx("PATH=" . $ENV{'PATH'});
1920
$ENV{TEST_NGINX_LUA_PACKAGE_PATH} = "$::CWD/lib/?.lua;;";
2021
$ENV{TEST_NGINX_HTML_DIR} ||= html_dir();
22+
$ENV{TEST_NGINX_SERVER_SSL_PORT} ||= 4443;
2123

2224
run_tests();
2325

@@ -2168,3 +2170,98 @@ client ip: 127.0.0.1
21682170
[error]
21692171
[alert]
21702172
[emerg]
2173+
2174+
2175+
2176+
=== TEST 21: yield during doing handshake with client which uses low version OpenSSL
2177+
--- no_check_leak
2178+
--- http_config
2179+
lua_shared_dict done 16k;
2180+
lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH/?.lua;;";
2181+
server {
2182+
listen $TEST_NGINX_SERVER_SSL_PORT ssl;
2183+
server_name test.com;
2184+
ssl_session_tickets off;
2185+
ssl_certificate ../../cert/test2.crt;
2186+
ssl_certificate_key ../../cert/test2.key;
2187+
2188+
ssl_certificate_by_lua_block {
2189+
local ssl = require "ngx.ssl"
2190+
2191+
ssl.clear_certs()
2192+
2193+
local f = assert(io.open("t/cert/test.crt.der"))
2194+
local cert_data = f:read("*a")
2195+
f:close()
2196+
2197+
ngx.sleep(0.01) -- yield
2198+
2199+
local ok, err = ssl.set_der_cert(cert_data)
2200+
if not ok then
2201+
ngx.log(ngx.ERR, "failed to set DER cert: ", err)
2202+
return
2203+
end
2204+
2205+
local f = assert(io.open("t/cert/test.key.der"))
2206+
local pkey_data = f:read("*a")
2207+
f:close()
2208+
2209+
local ok, err = ssl.set_der_priv_key(pkey_data)
2210+
if not ok then
2211+
ngx.log(ngx.ERR, "failed to set DER cert: ", err)
2212+
return
2213+
end
2214+
2215+
ngx.shared.done:set("handshake", true)
2216+
}
2217+
2218+
location /foo {
2219+
content_by_lua_block {
2220+
ngx.exit(201)
2221+
}
2222+
}
2223+
}
2224+
--- config
2225+
lua_ssl_trusted_certificate ../../cert/test.crt;
2226+
2227+
location /t {
2228+
content_by_lua_block {
2229+
ngx.shared.done:delete("handshake")
2230+
local addr = ngx.var.addr;
2231+
local f, err = io.popen("echo 'Q' | timeout 3s openssl s_client -connect 127.0.0.1:$TEST_NGINX_SERVER_SSL_PORT")
2232+
if not f then
2233+
ngx.say(err)
2234+
return
2235+
end
2236+
2237+
local step = 0.001
2238+
while step < 1 do
2239+
ngx.sleep(step)
2240+
step = step * 2
2241+
2242+
if ngx.shared.done:get("handshake") then
2243+
local out = f:read('*a')
2244+
ngx.log(ngx.INFO, out)
2245+
ngx.say("ok")
2246+
f:close()
2247+
return
2248+
end
2249+
end
2250+
2251+
ngx.log(ngx.ERR, "openssl client handshake timeout")
2252+
}
2253+
}
2254+
2255+
--- request
2256+
GET /t
2257+
--- response_body
2258+
ok
2259+
--- error_log eval
2260+
[
2261+
qr/content_by_lua\(nginx\.conf:\d+\):\d+: CONNECTED/,
2262+
'subject=/C=US/ST=California/L=San Francisco/O=OpenResty/OU=OpenResty/CN=test.com/[email protected]',
2263+
]
2264+
2265+
--- no_error_log
2266+
[error]
2267+
[alert]

0 commit comments

Comments
 (0)