Skip to content

Dynamic OCSP cache invalidation #13609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions rootfs/etc/nginx/lua/certificate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ local function fetch_and_cache_ocsp_response(uid, der_cert)
return
end

local ok
ok, err = ocsp.validate_ocsp_response(ocsp_response, der_cert)
local ok, error_or_next_update
ok, error_or_next_update = ocsp.validate_ocsp_response(ocsp_response, der_cert)
if not ok then
-- We are doing the same thing as vanilla Nginx here - if response status is not "good"
-- we do not use it - no stapling.
Expand All @@ -173,14 +173,33 @@ local function fetch_and_cache_ocsp_response(uid, der_cert)
-- the OCSP responder. Imagine OCSP responder is having an intermittent issue
-- and we keep sending request. It might make things worse for the responder.

ngx.log(ngx.NOTICE, "OCSP response validation failed: ", err)
ngx.log(ngx.NOTICE, "OCSP response validation failed: ", error_or_next_update)
return
end

-- Normally this should be (nextUpdate - thisUpdate), but Lua API does not expose
-- those attributes.
local expiry = 3600 * 24 * 3
local success, forcible
local success, forcible, expiry

-- nextUpdate field is finally accessible via Lua API through validate_ocsp_response(), therefore we can calculate cache invalidation precisely
if ok then
local now = ngx.now()
local grace_period = 300

-- handle nextUpdate field from OCSP
if error_or_next_update ~= nil then
expiry = error_or_next_update - now - grace_period
ngx.log(ngx.NOTICE, "OCSP Response cache was provided by OCSP server and is valid for: ", expiry)

if expiry <= 0 then
ngx.log(ngx.WARN, "OCSP next_update is in the past, setting ocsp_response_cache to 0")
expiry = 0
end
else
-- fallback to original logic if OCSP server did not provide nextUpdate field
expiry = 3600 * 24 * 3
ngx.log(ngx.NOTICE, "OCSP did not provide nextUpdate therefore it is set to fixed value of 3 days")
end
end

success, err, forcible = ocsp_response_cache:set(uid, ocsp_response, expiry)
if not success then
ngx.log(ngx.ERR, "failed to cache OCSP response: ", err)
Expand Down Expand Up @@ -273,4 +292,4 @@ function _M.call()
end
end

return _M
return _M