Skip to content

Commit

Permalink
feat: support pass cert and priv key content (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sn0rt authored Aug 28, 2023
1 parent 9a5b4c2 commit f66d3f8
Show file tree
Hide file tree
Showing 5 changed files with 450 additions and 51 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ on:

jobs:
build:
strategy:
matrix:
op_version:
- "1.21.4.1"
- "1.21.4.2"

runs-on: "ubuntu-20.04"

env:
OPENRESTY_VERSION: ${{ matrix.op_version }}
OPENRESTY_PREFIX: "/usr/local/openresty"
OPENRESTY_VERSION: "1.21.4.2"

steps:
- name: Check out code
Expand Down
71 changes: 46 additions & 25 deletions patch/1.21.4.1/lua-resty-core-tlshandshake.patch
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ index 3caabe2..6361a23 100644
$(INSTALL) lib/ngx/ssl/*.lua $(DESTDIR)$(LUA_LIB_DIR)/ngx/ssl/

diff --git lib/resty/core.lua lib/resty/core.lua
index 5472230..7d3ab16 100644
index e92084c..fd823ce 100644
--- lib/resty/core.lua
+++ lib/resty/core.lua
@@ -23,6 +23,7 @@ if subsystem == 'http' then
@@ -25,6 +25,7 @@ if subsystem == 'http' then
end


Expand All @@ -29,10 +29,10 @@ index 5472230..7d3ab16 100644

diff --git lib/resty/core/socket/tcp.lua lib/resty/core/socket/tcp.lua
new file mode 100644
index 0000000..89454ad
index 0000000..b6e009c
--- /dev/null
+++ lib/resty/core/socket/tcp.lua
@@ -0,0 +1,284 @@
@@ -0,0 +1,305 @@
+-- Copyright (C) by OpenResty Inc.
+
+
Expand Down Expand Up @@ -178,44 +178,65 @@ index 0000000..89454ad
+
+ local client_cert, client_pkey
+
+ local client_cert_path = options.client_cert_path
+ local client_pkey_path = options.client_priv_key_path
+ if client_cert_path then
+ if not client_pkey_path then
+ if options.client_cert_path or options.client_cert then
+ if options.client_cert_path and options.client_cert then
+ error("client client_cert_path and client_cert both setting ", 2)
+ end
+
+ if not options.client_priv_key_path and not options.client_priv_key then
+ error("client certificate supplied without corresponding " ..
+ "private key", 2)
+ "private key", 2)
+ end
+
+ if type(client_cert_path) ~= "string" then
+ error("bad client_cert option type", 2)
+ if options.client_priv_key_path and options.client_priv_key then
+ error("client certificate private key supplied with " ..
+ "client_priv_key and client_priv_key_path", 2)
+ end
+
+ if type(client_pkey_path) ~= "string" then
+ error("bad client_priv_key option type", 2)
+ if options.client_cert then
+ if type(options.client_cert) ~= "string" then
+ error("bad client_cert option type", 2)
+ end
+ else
+ if type(options.client_cert_path) ~= "string" then
+ error("bad client_cert option type", 2)
+ end
+
+ local txt, err = read_file(options.client_cert_path)
+ if not txt then
+ return nil, err
+ end
+
+ options.client_cert = txt
+ end
+
+ local txt, err = read_file(client_cert_path)
+ if not txt then
+ return nil, err
+ if options.client_priv_key then
+ if type(options.client_priv_key) ~= "string" then
+ error("bad client_priv_key option type", 2)
+ end
+ else
+ if type(options.client_priv_key_path) ~= "string" then
+ error("bad client_priv_key_path option type", 2)
+ end
+
+ local txt, err = read_file(options.client_priv_key_path)
+ if not txt then
+ return nil, err
+ end
+
+ options.client_priv_key = txt
+ end
+
+ local cert, err = ssl.parse_pem_cert(txt)
+ local cert, err = ssl.parse_pem_cert(options.client_cert)
+ if not cert then
+ return nil, err
+ end
+
+ client_cert = cert
+
+ local txt, err = read_file(client_pkey_path)
+ if not txt then
+ return nil, err
+ end
+
+ local pkey, err = ssl.parse_pem_priv_key(txt)
+ local pkey, err = ssl.parse_pem_priv_key(options.client_priv_key)
+ if not pkey then
+ return nil, err
+ end
+
+ client_pkey = pkey
+ end
+
Expand Down
71 changes: 46 additions & 25 deletions patch/1.21.4/lua-resty-core-tlshandshake.patch
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ index 3caabe2..6361a23 100644
$(INSTALL) lib/ngx/ssl/*.lua $(DESTDIR)$(LUA_LIB_DIR)/ngx/ssl/

diff --git lib/resty/core.lua lib/resty/core.lua
index 5472230..7d3ab16 100644
index e92084c..fd823ce 100644
--- lib/resty/core.lua
+++ lib/resty/core.lua
@@ -23,6 +23,7 @@ if subsystem == 'http' then
@@ -25,6 +25,7 @@ if subsystem == 'http' then
end


Expand All @@ -29,10 +29,10 @@ index 5472230..7d3ab16 100644

diff --git lib/resty/core/socket/tcp.lua lib/resty/core/socket/tcp.lua
new file mode 100644
index 0000000..89454ad
index 0000000..f66f51f
--- /dev/null
+++ lib/resty/core/socket/tcp.lua
@@ -0,0 +1,284 @@
@@ -0,0 +1,305 @@
+-- Copyright (C) by OpenResty Inc.
+
+
Expand Down Expand Up @@ -178,44 +178,65 @@ index 0000000..89454ad
+
+ local client_cert, client_pkey
+
+ local client_cert_path = options.client_cert_path
+ local client_pkey_path = options.client_priv_key_path
+ if client_cert_path then
+ if not client_pkey_path then
+ if options.client_cert_path or options.client_cert then
+ if options.client_cert_path and options.client_cert then
+ error("client client_cert_path and client_cert both setting ", 2)
+ end
+
+ if not options.client_priv_key_path and not options.client_priv_key then
+ error("client certificate supplied without corresponding " ..
+ "private key", 2)
+ "private key", 2)
+ end
+
+ if type(client_cert_path) ~= "string" then
+ error("bad client_cert option type", 2)
+ if options.client_priv_key_path and options.client_priv_key then
+ error("client certificate private key supplied with " ..
+ "client_priv_key and client_priv_key_path", 2)
+ end
+
+ if type(client_pkey_path) ~= "string" then
+ error("bad client_priv_key option type", 2)
+ if options.client_cert then
+ if type(options.client_cert) ~= "string" then
+ error("bad client_cert option type", 2)
+ end
+ else
+ if type(options.client_cert_path) ~= "string" then
+ error("bad client_cert option type", 2)
+ end
+
+ local txt, err = read_file(options.client_cert_path)
+ if not txt then
+ return nil, err
+ end
+
+ options.client_cert = txt
+ end
+
+ local txt, err = read_file(client_cert_path)
+ if not txt then
+ return nil, err
+ if options.client_priv_key then
+ if type(options.client_priv_key) ~= "string" then
+ error("bad client_priv_key option type", 2)
+ end
+ else
+ if type(options.client_priv_key_path) ~= "string" then
+ error("bad client_priv_key_path option type", 2)
+ end
+
+ local txt, err = read_file(options.client_priv_key_path)
+ if not txt then
+ return nil, err
+ end
+
+ options.client_priv_key = txt
+ end
+
+ local cert, err = ssl.parse_pem_cert(txt)
+ local cert, err = ssl.parse_pem_cert(options.client_cert)
+ if not cert then
+ return nil, err
+ end
+
+ client_cert = cert
+
+ local txt, err = read_file(client_pkey_path)
+ if not txt then
+ return nil, err
+ end
+
+ local pkey, err = ssl.parse_pem_priv_key(txt)
+ local pkey, err = ssl.parse_pem_priv_key(options.client_priv_key)
+ if not pkey then
+ return nil, err
+ end
+
+ client_pkey = pkey
+ end
+
Expand Down
71 changes: 71 additions & 0 deletions t/cosocket_mtls.t
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,74 @@ __DATA__
closed
--- error_log
[error]



=== TEST 2: avoid using stale openssl error code with cert content
--- config
location /t {
access_by_lua_block {
local sock = ngx.socket.tcp()
sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
for i = 1, 2 do
local ok, err = sock:tlshandshake({
verify = true,
client_cert = [[-----BEGIN CERTIFICATE-----
MIIDOjCCAiICAwD6zzANBgkqhkiG9w0BAQsFADBnMQswCQYDVQQGEwJjbjESMBAG
A1UECAwJR3VhbmdEb25nMQ8wDQYDVQQHDAZaaHVIYWkxDTALBgNVBAoMBGFwaTcx
DDAKBgNVBAsMA29wczEWMBQGA1UEAwwNY2EuYXBpc2l4LmRldjAeFw0yMDA2MjAx
MzE1MDBaFw0zMDA3MDgxMzE1MDBaMF0xCzAJBgNVBAYTAmNuMRIwEAYDVQQIDAlH
dWFuZ0RvbmcxDTALBgNVBAoMBGFwaTcxDzANBgNVBAcMBlpodUhhaTEaMBgGA1UE
AwwRY2xpZW50LmFwaXNpeC5kZXYwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK
AoIBAQCfKI8uiEH/ifZikSnRa3/E2B4ohVWRwjo/IxyDEWomgR4tLk1pSJhP/4SC
LWuMQTFWTbSqt1IFYy4Zb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LJPRMw/iL9BdMf606X4xbmNn9HWVp2mH9D3D51kDFvls
7y2vEaYkFv3XoYgVN9ZHDUbM/YTUozKjcAcvz0syLQb8wRwKeo+XSmo09+360r18
zRugoE7bPl39WdGWaW3td0qf1r9z3sE2iWUTJPRQ3DYpsLOYIgyKmw==
-----END RSA PRIVATE KEY-----]],
})
if not ok then
ngx.say(err)
end
end
}
}
--- response_body
20: unable to get local issuer certificate
closed
--- error_log
[error]
Loading

0 comments on commit f66d3f8

Please sign in to comment.