Skip to content

Commit f5ed7c8

Browse files
lziestagentzh
authored andcommitted
doc: added documentation for ticket key rotation logic.
Signed-off-by: Yichun Zhang (agentzh) <agentzh@gmail.com>
1 parent afe76b5 commit f5ed7c8

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,47 @@ Memcached-compatible servers automatically every hour.
8888
1. Uses shm cache for the keys so that only one worker needs to query the Memcached or
8989
Memcached-compatible servers. The shm cache can be disabled though.
9090

91+
NOTE: ticket key should be protected by some key encryption key. The ticket key should
92+
be decrypted before being used. However we leave this to the user to handle.
93+
94+
Methods
95+
=======
96+
97+
This section documents the methods for the `ngx.ssl.session.ticket.key_rotation` Lua module.
98+
99+
init
100+
----
101+
`syntax: module.init(opts)`
102+
103+
Initialize the settings of this module.
104+
105+
start_update_timer
106+
------------------
107+
`syntax: module.start_update_timer()`
108+
109+
Starts a recurring timer that periodically populates and rotates the ticket key list.
110+
When invoked, it does three things:
111+
112+
1. Look up a ticket key for current time slot and insert
113+
it to the beginning of the ticket key list.
114+
2. Look up a ticket key for next time slot and replace the
115+
the last element of the key list with it.
116+
3. Start a new timer for next check.
117+
118+
Note we use rounded down timestamp based indexing in the shared
119+
memcached to store/fetch ticket key.
120+
121+
For example, using a time slot of 1000 second, we would round
122+
the timestamp down to the nearest 1000: 1001 -> 1000, 1987 -> 1000,
123+
and 2001 -> 2000. In practice we usually use 1 hour as slot size.
124+
You can check out the Lua function `ticket_key_index` for implementation details.
125+
126+
Timers across hosts are only loosely synchronized, there are cases that
127+
host A is waken up by its timer and host B is not.
128+
host A would start to use new key while session B is yet to load
129+
it. The problem is solved by preloading the key for the next slot,
130+
as described by item 2 above.
131+
91132
Installation
92133
============
93134

lualib/ngx/ssl/session/ticket/key_rotation.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ local function ticket_key_index(now, offset)
113113
end
114114

115115

116+
-- get ticket key from shared dict. Ticket key should be protected
117+
-- by some key encryption key. The ticket key would be decrypted
118+
-- before being used. However here we leave it to users to finish
119+
-- it.
116120
local function shdict_get_and_decrypt(ctx, idx)
117121
local res, stale = meta_shdict_get(ctx, idx)
118122

@@ -129,6 +133,9 @@ local function shdict_get_and_decrypt(ctx, idx)
129133
end
130134

131135
local key = res
136+
-- TODO: Decrypt res into 48-byte key
137+
-- Ideally we should protect the ticket key with some encryption.
138+
-- key = decrypt(key, key_encryption_key)
132139

133140
if #key ~= 48 then
134141
return fail("malformed key: #key ", #key)
@@ -138,6 +145,10 @@ local function shdict_get_and_decrypt(ctx, idx)
138145
end
139146

140147

148+
-- get ticket key from memcached. Ticket key should be protected
149+
-- by some key encryption key. The ticket key would be decrypted
150+
-- before being used. However here we leave it to users to finish
151+
-- it.
141152
local function memc_get_and_decrypt(ctx, idx, offset)
142153
if DEBUG then
143154
dlog(ctx, "ticket key index: ", idx, " time slot offset: ", offset)
@@ -155,6 +166,9 @@ local function memc_get_and_decrypt(ctx, idx, offset)
155166
end
156167

157168
local key = res
169+
-- TODO: Decrypt res into 48-byte key
170+
-- Ideally we should protect the ticket key with some encryption.
171+
-- key = decrypt(key, key_encryption_key)
158172

159173
if #key ~= 48 then
160174
return fail("malformed key: #key ", #key)

0 commit comments

Comments
 (0)