Skip to content

Commit

Permalink
fix(ssl): clear the old chain before adding new chain (#94)
Browse files Browse the repository at this point in the history
https://konghq.atlassian.net/browse/FTI-6275

* test(ssl): add set_upstream_client_cert_and_key test
  • Loading branch information
catbro666 authored Oct 15, 2024
1 parent 7da8c7d commit e2b4d03
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/ssl/ngx_lua_kong_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,13 @@ ngx_lua_kong_ssl_set_upstream_ssl(ngx_lua_kong_ssl_ctx_t *ctx, ngx_connection_t
goto failed;
}

/* clear the old chain */
if (SSL_clear_chain_certs(sc) == 0) {
ngx_ssl_error(NGX_LOG_ALERT, c->log, 0,
"SSL_clear_chain_certs() failed");
goto failed;
}

/* read rest of the chain */

for (i = 1; i < sk_X509_num(chain); i++) {
Expand Down
80 changes: 79 additions & 1 deletion t/002-upstream-tls.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use Cwd qw(cwd);

repeat_each(2);

plan tests => repeat_each() * (blocks() * 6);
plan tests => repeat_each() * (blocks() * 6 + 1);

my $pwd = cwd();

Expand Down Expand Up @@ -810,3 +810,81 @@ X509_check_host(): match
[error]
[crit]
[alert]
=== TEST 14: set_upstream_client_cert_and_key should clear the old chain
--- http_config
lua_package_path "../lua-resty-core/lib/?.lua;lualib/?.lua;;";
server {
listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl;
server_name example.com;
ssl_certificate ../../cert/example.com.crt;
ssl_certificate_key ../../cert/example.com.key;
ssl_client_certificate ../../cert/ca.crt;
ssl_verify_client on;
server_tokens off;
location /foo {
default_type 'text/plain';
more_clear_headers Date;
echo 'it works!';
}
}
upstream foo {
server unix:$TEST_NGINX_HTML_DIR/nginx.sock;
balancer_by_lua_block {
collectgarbage() -- to make leak check mode pass
local tls = require("resty.kong.tls")
local ssl = require("ngx.ssl")
local f = assert(io.open("t/cert/client_leaf.crt"))
local cert_data = f:read("*a")
f:close()
local chain = assert(ssl.parse_pem_cert(cert_data))
f = assert(io.open("t/cert/client_example.com.key"))
local key_data = f:read("*a")
f:close()
local key = assert(ssl.parse_pem_priv_key(key_data))
local ok, err = tls.set_upstream_cert_and_key(chain, key)
if not ok then
ngx.say("set_upstream_cert_and_key failed: ", err)
end
}
}
--- config
server_tokens off;
location /t {
proxy_ssl_trusted_certificate ../../cert/ca.crt;
proxy_ssl_verify on;
proxy_ssl_name example.com;
proxy_ssl_certificate ../../cert/client_example.com.crt;
proxy_ssl_certificate_key ../../cert/client_example.com.key;
proxy_ssl_session_reuse off;
proxy_pass https://foo/foo;
}
--- request
GET /t
--- response_body_like
.+400 The SSL certificate error.+
--- error_log
verify:0, error:21, depth:0, subject:"/C=US/ST=California/O=Kong Testing/CN=foo@example.com", issuer:"/C=US/ST=California/O=Kong Testing/CN=Kong Testing Intermidiate CA"
client SSL certificate verify error: (21:unable to verify the first certificate) while reading client request headers
--- error_code: 400
--- no_error_log
[error]
[crit]
[alert]
30 changes: 30 additions & 0 deletions t/cert/client_leaf.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-----BEGIN CERTIFICATE-----
MIIFIjCCAwqgAwIBAgICIAEwDQYJKoZIhvcNAQELBQAwYDELMAkGA1UEBhMCVVMx
EzARBgNVBAgMCkNhbGlmb3JuaWExFTATBgNVBAoMDEtvbmcgVGVzdGluZzElMCMG
A1UEAwwcS29uZyBUZXN0aW5nIEludGVybWlkaWF0ZSBDQTAeFw0xOTA1MDIyMDAz
MTFaFw0yOTA0MjgyMDAzMTFaMFMxCzAJBgNVBAYTAlVTMRMwEQYDVQQIDApDYWxp
Zm9ybmlhMRUwEwYDVQQKDAxLb25nIFRlc3RpbmcxGDAWBgNVBAMMD2Zvb0BleGFt
cGxlLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAJldMxsZHDxA
RpbSXdIFZiTf8D0dYgsPnsmx5tVjA/zrVBSVBP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