@@ -11,11 +11,15 @@ local ffi_cast = ffi.cast
1111local string_format = string.format
1212local type = type
1313
14- local function require_openssl_libs ()
15- local chain = require (" resty.openssl.x509.chain" )
16- local x509 = require (" resty.openssl.x509" )
17- local pkey = require (" resty.openssl.pkey" )
18- return { chain , x509 , pkey }
14+ local lib_chain , lib_x509 , lib_pkey
15+ local openssl_available , res = xpcall (function ()
16+ lib_chain = require (" resty.openssl.x509.chain" )
17+ lib_x509 = require (" resty.openssl.x509" )
18+ lib_pkey = require (" resty.openssl.pkey" )
19+ end , debug.traceback )
20+
21+ if not openssl_available then
22+ ngx_log (ngx_WARN , " failed to load module `resty.openssl.*`, mTLS isn't supported without lua-resty-openssl :\n " , res )
1923end
2024
2125--[[
@@ -175,7 +179,7 @@ local function connect(self, options)
175179 end
176180
177181 local cert_hash
178- -- fallback to non-mTLS when any error
182+ -- fallback to non-mTLS if it's not an error due to the caller
179183 repeat
180184 if not ssl or not ssl_client_cert or not ssl_client_priv_key then
181185 break
@@ -185,53 +189,37 @@ local function connect(self, options)
185189 local key_type = type (ssl_client_priv_key )
186190
187191 if cert_type ~= " cdata" then
188- ngx_log (ngx_WARN , " bad ssl_client_cert: cdata expected, got " , cert_type )
189- break
192+ return nil , string_format (" bad ssl_client_cert: cdata expected, got %s" , cert_type )
190193 end
191194
192195 if key_type ~= " cdata" then
193- ngx_log (ngx_WARN , " bad ssl_client_priv_key: cdata expected, got " , key_type )
194- break
196+ return nil , string_format (" bad ssl_client_priv_key: cdata expected, got %s" , key_type )
195197 end
196198
197- local status , res = xpcall (require_openssl_libs , debug.traceback )
198-
199- if not status then
200- if type (res ) == " string" and ngx_re_find (res , " module 'resty\\ .openssl\\ ..+' not found" ) then
201- ngx_log (ngx_WARN , " can't use mTLS without module `lua-resty-openssl`, falling back to non-mTLS:\n "
202- , res )
203-
204- else
205- ngx_log (ngx_WARN , " failed to load module `resty.openssl.*`, falling back to non-mTLS:\n " , res )
206- end
207-
199+ if not openssl_available then
200+ ngx_log (ngx_WARN , " module `resty.openssl.*` not available, falling back to non-mTLS:\n " )
208201 break
209202 end
210203
211- local chain = res [1 ]
212- local x509 = res [2 ]
213- local pkey = res [3 ]
214-
215204 -- convert from `void*` to `OPENSSL_STACK*`
216- local cert_chain , err = chain .dup (ffi_cast (" OPENSSL_STACK*" , ssl_client_cert ))
205+ local cert_chain , err = lib_chain .dup (ffi_cast (" OPENSSL_STACK*" , ssl_client_cert ))
217206 if not cert_chain then
218207 ngx_log (ngx_WARN , " failed to dup the ssl_client_cert, falling back to non-mTLS: " , err )
219208 break
220209 end
221210
222211 if # cert_chain < 1 then
223- ngx_log (ngx_WARN , " no cert in ssl_client_cert, falling back to non-mTLS: " , err )
224- break
212+ return nil , " no cert in ssl_client_cert"
225213 end
226214
227- local cert , err = x509 .dup (cert_chain [1 ].ctx )
215+ local cert , err = lib_x509 .dup (cert_chain [1 ].ctx )
228216 if not cert then
229217 ngx_log (ngx_WARN , " failed to dup the x509, falling back to non-mTLS: " , err )
230218 break
231219 end
232220
233221 -- convert from `void*` to `EVP_PKEY*`
234- local key , err = pkey .new (ffi_cast (" EVP_PKEY*" , ssl_client_priv_key ))
222+ local key , err = lib_pkey .new (ffi_cast (" EVP_PKEY*" , ssl_client_priv_key ))
235223 if not key then
236224 ngx_log (ngx_WARN , " failed to new the pkey, falling back to non-mTLS: " , err )
237225 break
@@ -242,8 +230,7 @@ local function connect(self, options)
242230 -- check the private key in order to make sure the caller is indeed the holder of the cert
243231 ok , err = cert :check_private_key (key )
244232 if not ok then
245- ngx_log (ngx_WARN , " the private key doesn't match the cert, falling back to non-mTLS: " , err )
246- break
233+ return nil , string_format (" the private key doesn't match the cert: %s" , err )
247234 end
248235
249236 cert_hash , err = cert :digest (" sha256" )
0 commit comments