Skip to content

Commit f66d3f8

Browse files
authored
feat: support pass cert and priv key content (#79)
1 parent 9a5b4c2 commit f66d3f8

File tree

5 files changed

+450
-51
lines changed

5 files changed

+450
-51
lines changed

Diff for: .github/workflows/ci.yml

+8-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,17 @@ on:
88

99
jobs:
1010
build:
11+
strategy:
12+
matrix:
13+
op_version:
14+
- "1.21.4.1"
15+
- "1.21.4.2"
16+
1117
runs-on: "ubuntu-20.04"
18+
1219
env:
20+
OPENRESTY_VERSION: ${{ matrix.op_version }}
1321
OPENRESTY_PREFIX: "/usr/local/openresty"
14-
OPENRESTY_VERSION: "1.21.4.2"
1522

1623
steps:
1724
- name: Check out code

Diff for: patch/1.21.4.1/lua-resty-core-tlshandshake.patch

+46-25
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ index 3caabe2..6361a23 100644
1616
$(INSTALL) lib/ngx/ssl/*.lua $(DESTDIR)$(LUA_LIB_DIR)/ngx/ssl/
1717

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

2525

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

3030
diff --git lib/resty/core/socket/tcp.lua lib/resty/core/socket/tcp.lua
3131
new file mode 100644
32-
index 0000000..89454ad
32+
index 0000000..b6e009c
3333
--- /dev/null
3434
+++ lib/resty/core/socket/tcp.lua
35-
@@ -0,0 +1,284 @@
35+
@@ -0,0 +1,305 @@
3636
+-- Copyright (C) by OpenResty Inc.
3737
+
3838
+
@@ -178,44 +178,65 @@ index 0000000..89454ad
178178
+
179179
+ local client_cert, client_pkey
180180
+
181-
+ local client_cert_path = options.client_cert_path
182-
+ local client_pkey_path = options.client_priv_key_path
183-
+ if client_cert_path then
184-
+ if not client_pkey_path then
181+
+ if options.client_cert_path or options.client_cert then
182+
+ if options.client_cert_path and options.client_cert then
183+
+ error("client client_cert_path and client_cert both setting ", 2)
184+
+ end
185+
+
186+
+ if not options.client_priv_key_path and not options.client_priv_key then
185187
+ error("client certificate supplied without corresponding " ..
186-
+ "private key", 2)
188+
+ "private key", 2)
187189
+ end
188190
+
189-
+ if type(client_cert_path) ~= "string" then
190-
+ error("bad client_cert option type", 2)
191+
+ if options.client_priv_key_path and options.client_priv_key then
192+
+ error("client certificate private key supplied with " ..
193+
+ "client_priv_key and client_priv_key_path", 2)
191194
+ end
192195
+
193-
+ if type(client_pkey_path) ~= "string" then
194-
+ error("bad client_priv_key option type", 2)
196+
+ if options.client_cert then
197+
+ if type(options.client_cert) ~= "string" then
198+
+ error("bad client_cert option type", 2)
199+
+ end
200+
+ else
201+
+ if type(options.client_cert_path) ~= "string" then
202+
+ error("bad client_cert option type", 2)
203+
+ end
204+
+
205+
+ local txt, err = read_file(options.client_cert_path)
206+
+ if not txt then
207+
+ return nil, err
208+
+ end
209+
+
210+
+ options.client_cert = txt
195211
+ end
196212
+
197-
+ local txt, err = read_file(client_cert_path)
198-
+ if not txt then
199-
+ return nil, err
213+
+ if options.client_priv_key then
214+
+ if type(options.client_priv_key) ~= "string" then
215+
+ error("bad client_priv_key option type", 2)
216+
+ end
217+
+ else
218+
+ if type(options.client_priv_key_path) ~= "string" then
219+
+ error("bad client_priv_key_path option type", 2)
220+
+ end
221+
+
222+
+ local txt, err = read_file(options.client_priv_key_path)
223+
+ if not txt then
224+
+ return nil, err
225+
+ end
226+
+
227+
+ options.client_priv_key = txt
200228
+ end
201229
+
202-
+ local cert, err = ssl.parse_pem_cert(txt)
230+
+ local cert, err = ssl.parse_pem_cert(options.client_cert)
203231
+ if not cert then
204232
+ return nil, err
205233
+ end
206-
+
207234
+ client_cert = cert
208235
+
209-
+ local txt, err = read_file(client_pkey_path)
210-
+ if not txt then
211-
+ return nil, err
212-
+ end
213-
+
214-
+ local pkey, err = ssl.parse_pem_priv_key(txt)
236+
+ local pkey, err = ssl.parse_pem_priv_key(options.client_priv_key)
215237
+ if not pkey then
216238
+ return nil, err
217239
+ end
218-
+
219240
+ client_pkey = pkey
220241
+ end
221242
+

Diff for: patch/1.21.4/lua-resty-core-tlshandshake.patch

+46-25
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ index 3caabe2..6361a23 100644
1616
$(INSTALL) lib/ngx/ssl/*.lua $(DESTDIR)$(LUA_LIB_DIR)/ngx/ssl/
1717

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

2525

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

3030
diff --git lib/resty/core/socket/tcp.lua lib/resty/core/socket/tcp.lua
3131
new file mode 100644
32-
index 0000000..89454ad
32+
index 0000000..f66f51f
3333
--- /dev/null
3434
+++ lib/resty/core/socket/tcp.lua
35-
@@ -0,0 +1,284 @@
35+
@@ -0,0 +1,305 @@
3636
+-- Copyright (C) by OpenResty Inc.
3737
+
3838
+
@@ -178,44 +178,65 @@ index 0000000..89454ad
178178
+
179179
+ local client_cert, client_pkey
180180
+
181-
+ local client_cert_path = options.client_cert_path
182-
+ local client_pkey_path = options.client_priv_key_path
183-
+ if client_cert_path then
184-
+ if not client_pkey_path then
181+
+ if options.client_cert_path or options.client_cert then
182+
+ if options.client_cert_path and options.client_cert then
183+
+ error("client client_cert_path and client_cert both setting ", 2)
184+
+ end
185+
+
186+
+ if not options.client_priv_key_path and not options.client_priv_key then
185187
+ error("client certificate supplied without corresponding " ..
186-
+ "private key", 2)
188+
+ "private key", 2)
187189
+ end
188190
+
189-
+ if type(client_cert_path) ~= "string" then
190-
+ error("bad client_cert option type", 2)
191+
+ if options.client_priv_key_path and options.client_priv_key then
192+
+ error("client certificate private key supplied with " ..
193+
+ "client_priv_key and client_priv_key_path", 2)
191194
+ end
192195
+
193-
+ if type(client_pkey_path) ~= "string" then
194-
+ error("bad client_priv_key option type", 2)
196+
+ if options.client_cert then
197+
+ if type(options.client_cert) ~= "string" then
198+
+ error("bad client_cert option type", 2)
199+
+ end
200+
+ else
201+
+ if type(options.client_cert_path) ~= "string" then
202+
+ error("bad client_cert option type", 2)
203+
+ end
204+
+
205+
+ local txt, err = read_file(options.client_cert_path)
206+
+ if not txt then
207+
+ return nil, err
208+
+ end
209+
+
210+
+ options.client_cert = txt
195211
+ end
196212
+
197-
+ local txt, err = read_file(client_cert_path)
198-
+ if not txt then
199-
+ return nil, err
213+
+ if options.client_priv_key then
214+
+ if type(options.client_priv_key) ~= "string" then
215+
+ error("bad client_priv_key option type", 2)
216+
+ end
217+
+ else
218+
+ if type(options.client_priv_key_path) ~= "string" then
219+
+ error("bad client_priv_key_path option type", 2)
220+
+ end
221+
+
222+
+ local txt, err = read_file(options.client_priv_key_path)
223+
+ if not txt then
224+
+ return nil, err
225+
+ end
226+
+
227+
+ options.client_priv_key = txt
200228
+ end
201229
+
202-
+ local cert, err = ssl.parse_pem_cert(txt)
230+
+ local cert, err = ssl.parse_pem_cert(options.client_cert)
203231
+ if not cert then
204232
+ return nil, err
205233
+ end
206-
+
207234
+ client_cert = cert
208235
+
209-
+ local txt, err = read_file(client_pkey_path)
210-
+ if not txt then
211-
+ return nil, err
212-
+ end
213-
+
214-
+ local pkey, err = ssl.parse_pem_priv_key(txt)
236+
+ local pkey, err = ssl.parse_pem_priv_key(options.client_priv_key)
215237
+ if not pkey then
216238
+ return nil, err
217239
+ end
218-
+
219240
+ client_pkey = pkey
220241
+ end
221242
+

Diff for: t/cosocket_mtls.t

+71
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,74 @@ __DATA__
5151
closed
5252
--- error_log
5353
[error]
54+
55+
56+
57+
=== TEST 2: avoid using stale openssl error code with cert content
58+
--- config
59+
location /t {
60+
access_by_lua_block {
61+
local sock = ngx.socket.tcp()
62+
sock:connect("unix:$TEST_NGINX_HTML_DIR/nginx.sock")
63+
for i = 1, 2 do
64+
local ok, err = sock:tlshandshake({
65+
verify = true,
66+
client_cert = [[-----BEGIN CERTIFICATE-----
67+
MIIDOjCCAiICAwD6zzANBgkqhkiG9w0BAQsFADBnMQswCQYDVQQGEwJjbjESMBAG
68+
A1UECAwJR3VhbmdEb25nMQ8wDQYDVQQHDAZaaHVIYWkxDTALBgNVBAoMBGFwaTcx
69+
DDAKBgNVBAsMA29wczEWMBQGA1UEAwwNY2EuYXBpc2l4LmRldjAeFw0yMDA2MjAx
70+
MzE1MDBaFw0zMDA3MDgxMzE1MDBaMF0xCzAJBgNVBAYTAmNuMRIwEAYDVQQIDAlH
71+
dWFuZ0RvbmcxDTALBgNVBAoMBGFwaTcxDzANBgNVBAcMBlpodUhhaTEaMBgGA1UE
72+
AwwRY2xpZW50LmFwaXNpeC5kZXYwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK
73+
AoIBAQCfKI8uiEH/ifZikSnRa3/E2B4ohVWRwjo/IxyDEWomgR4tLk1pSJhP/4SC
74+
LWuMQTFWTbSqt1IFYy4ZbVSHHyGoNPmJGrHRJCGE+sgpfzn0GjV4lXQPJD0k6GR1
75+
CX2Mo1TWdFqSJ/Hc5AQwcQFnPfoLAwsBy4yqrlmf96ZAUytl/7Zkjf4P7mJkJHtM
76+
/WgSR0pGhjZTAGRf5DJWoO51ki3i3JI+15mOhmnnCpnksnGVPfl92q92Hz/4v3iq
77+
E+UThPYRpcGbnddzMvPaCXiavg8B/u2LVbn4l0adamqQGepOAjD/1xraOVP2W22W
78+
0PztDXJ4rLe+capNS4oGuSUfkIENAgMBAAEwDQYJKoZIhvcNAQELBQADggEBAHKn
79+
HxUhuk/nL2Sg5UB84OoJe5XPgNBvVMKN0c/NAPKVIPninvUcG/mHeKexPzE0s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P0VNHVqv+ZO3bktfvlwBru5ZJ7Cf
98+
URLsSc0CgYEAyz7FzV7cZYgjfUFD67MIS1HtVk7SX0UiYCsrGy8zA19tkhe3XVpc
99+
CZSwkjzjdEk0zEwiNAtawrDlR1m2kverbhhCHqXUOHwEpujMBjeJCNUVEh3OABr8
100+
vf2WJ6D1IRh8FA5CYLZP7aZ41fcxAnvIPAEThemLQL3C4H5H5NG2WFsCgYEAyHhP
101+
onpS/Eo/OXKYFLR/mvjizRVSomz1lVVL+GWMUYQsmgsPyBJgyAOX3Pqt9catgxhM
102+
DbEr7EWTxth3YeVzamiJPNVK0HvCax9gQ0KkOmtbrfN54zBHOJ+ieYhsieZLMgjx
103+
iu7Ieo6LDGV39HkvekzutZpypiCpKlMaFlCFiLcCgYEAmAgRsEj4Nh665VPvuZzH
104+
ZIgZMAlwBgHR7/v6l7AbybcVYEXLTNJtrGEEH6/aOL8V9ogwwZuIvb/TEidCkfcf
105+
zg/pTcGf2My0MiJLk47xO6EgzNdso9mMG5ZYPraBBsuo7NupvWxCp7NyCiOJDqGH
106+
K5NmhjInjzsjTghIQRq5+qcCgYEAxnm/NjjvslL8F69p/I3cDJ2/RpaG0sMXvbrO
107+
VWaMryQyWGz9OfNgGIbeMu2Jj90dar6ChcfUmb8lGOi2AZl/VGmc/jqaMKFnElHl
108+
J5JyMFicUzPMiG8DBH+gB71W4Iy+BBKwugHBQP2hkytewQ++PtKuP+RjADEz6vCN
109+
0mv0WS8CgYBnbMRP8wIOLJPRMw/iL9BdMf606X4xbmNn9HWVp2mH9D3D51kDFvls
110+
7y2vEaYkFv3XoYgVN9ZHDUbM/YTUozKjcAcvz0syLQb8wRwKeo+XSmo09+360r18
111+
zRugoE7bPl39WdGWaW3td0qf1r9z3sE2iWUTJPRQ3DYpsLOYIgyKmw==
112+
-----END RSA PRIVATE KEY-----]],
113+
})
114+
if not ok then
115+
ngx.say(err)
116+
end
117+
end
118+
}
119+
}
120+
--- response_body
121+
20: unable to get local issuer certificate
122+
closed
123+
--- error_log
124+
[error]

0 commit comments

Comments
 (0)