filters: migrate from luacrypto to luaossl

luaossl has no upstream anymore and doesn't support OpenSSL 1.1,
whereas luaossl is quite active.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2019-01-03 02:11:14 +01:00
parent e23f63461f
commit 7d87cd3a21
5 changed files with 83 additions and 44 deletions

View File

@ -3,15 +3,24 @@
-- prefix in filters. It is much faster than the corresponding python script. -- prefix in filters. It is much faster than the corresponding python script.
-- --
-- Requirements: -- Requirements:
-- luacrypto >= 0.3 -- luaossl
-- <http://mkottman.github.io/luacrypto/> -- <http://25thandclement.com/~william/projects/luaossl.html>
-- --
local crypto = require("crypto") local digest = require("openssl.digest")
function md5_hex(input)
local b = digest.new("md5"):final(input)
local x = ""
for i = 1, #b do
x = x .. string.format("%.2x", string.byte(b, i))
end
return x
end
function filter_open(email, page) function filter_open(email, page)
buffer = "" buffer = ""
md5 = crypto.digest("md5", email:sub(2, -2):lower()) md5 = md5_hex(email:sub(2, -2):lower())
end end
function filter_close() function filter_close()

View File

@ -3,15 +3,24 @@
-- prefix in filters. -- prefix in filters.
-- --
-- Requirements: -- Requirements:
-- luacrypto >= 0.3 -- luaossl
-- <http://mkottman.github.io/luacrypto/> -- <http://25thandclement.com/~william/projects/luaossl.html>
-- --
local crypto = require("crypto") local digest = require("openssl.digest")
function md5_hex(input)
local b = digest.new("md5"):final(input)
local x = ""
for i = 1, #b do
x = x .. string.format("%.2x", string.byte(b, i))
end
return x
end
function filter_open(email, page) function filter_open(email, page)
buffer = "" buffer = ""
md5 = crypto.digest("md5", email:sub(2, -2):lower()) md5 = md5_hex(email:sub(2, -2):lower())
end end
function filter_close() function filter_close()

View File

@ -1,15 +1,15 @@
-- This script may be used with the auth-filter. -- This script may be used with the auth-filter.
-- --
-- Requirements: -- Requirements:
-- luacrypto >= 0.3 -- luaossl
-- <http://mkottman.github.io/luacrypto/> -- <http://25thandclement.com/~william/projects/luaossl.html>
-- luaposix -- luaposix
-- <https://github.com/luaposix/luaposix> -- <https://github.com/luaposix/luaposix>
-- --
local sysstat = require("posix.sys.stat") local sysstat = require("posix.sys.stat")
local unistd = require("posix.unistd") local unistd = require("posix.unistd")
local crypto = require("crypto") local rand = require("openssl.rand")
local hmac = require("openssl.hmac")
-- This file should contain a series of lines in the form of: -- This file should contain a series of lines in the form of:
-- username1:hash1 -- username1:hash1
@ -225,6 +225,13 @@ function get_cookie(cookies, name)
return url_decode(string.match(cookies, ";" .. name .. "=(.-);")) return url_decode(string.match(cookies, ";" .. name .. "=(.-);"))
end end
function tohex(b)
local x = ""
for i = 1, #b do
x = x .. string.format("%.2x", string.byte(b, i))
end
return x
end
-- --
-- --
@ -242,12 +249,12 @@ function get_secret()
local secret_file = io.open(secret_filename, "r") local secret_file = io.open(secret_filename, "r")
if secret_file == nil then if secret_file == nil then
local old_umask = sysstat.umask(63) local old_umask = sysstat.umask(63)
local temporary_filename = secret_filename .. ".tmp." .. crypto.hex(crypto.rand.bytes(16)) local temporary_filename = secret_filename .. ".tmp." .. tohex(rand.bytes(16))
local temporary_file = io.open(temporary_filename, "w") local temporary_file = io.open(temporary_filename, "w")
if temporary_file == nil then if temporary_file == nil then
os.exit(177) os.exit(177)
end end
temporary_file:write(crypto.hex(crypto.rand.bytes(32))) temporary_file:write(tohex(rand.bytes(32)))
temporary_file:close() temporary_file:close()
unistd.link(temporary_filename, secret_filename) -- Intentionally fails in the case that another process is doing the same. unistd.link(temporary_filename, secret_filename) -- Intentionally fails in the case that another process is doing the same.
unistd.unlink(temporary_filename) unistd.unlink(temporary_filename)
@ -272,7 +279,7 @@ function validate_value(expected_field, cookie)
local field = "" local field = ""
local expiration = 0 local expiration = 0
local salt = "" local salt = ""
local hmac = "" local chmac = ""
if cookie == nil or cookie:len() < 3 or cookie:sub(1, 1) == "|" then if cookie == nil or cookie:len() < 3 or cookie:sub(1, 1) == "|" then
return nil return nil
@ -291,19 +298,19 @@ function validate_value(expected_field, cookie)
elseif i == 3 then elseif i == 3 then
salt = component salt = component
elseif i == 4 then elseif i == 4 then
hmac = component chmac = component
else else
break break
end end
i = i + 1 i = i + 1
end end
if hmac == nil or hmac:len() == 0 then if chmac == nil or chmac:len() == 0 then
return nil return nil
end end
-- Lua hashes strings, so these comparisons are time invariant. -- Lua hashes strings, so these comparisons are time invariant.
if hmac ~= crypto.hmac.digest("sha256", field .. "|" .. value .. "|" .. tostring(expiration) .. "|" .. salt, get_secret()) then if chmac ~= tohex(hmac.new(get_secret(), "sha256"):final(field .. "|" .. value .. "|" .. tostring(expiration) .. "|" .. salt)) then
return nil return nil
end end
@ -324,11 +331,11 @@ function secure_value(field, value, expiration)
end end
local authstr = "" local authstr = ""
local salt = crypto.hex(crypto.rand.bytes(16)) local salt = tohex(rand.bytes(16))
value = url_encode(value) value = url_encode(value)
field = url_encode(field) field = url_encode(field)
authstr = field .. "|" .. value .. "|" .. tostring(expiration) .. "|" .. salt authstr = field .. "|" .. value .. "|" .. tostring(expiration) .. "|" .. salt
authstr = authstr .. "|" .. crypto.hmac.digest("sha256", authstr, get_secret()) authstr = authstr .. "|" .. tohex(hmac.new(get_secret(), "sha256"):final(authstr))
return authstr return authstr
end end

View File

@ -1,8 +1,8 @@
-- This script may be used with the auth-filter. Be sure to configure it as you wish. -- This script may be used with the auth-filter. Be sure to configure it as you wish.
-- --
-- Requirements: -- Requirements:
-- luacrypto >= 0.3 -- luaossl
-- <http://mkottman.github.io/luacrypto/> -- <http://25thandclement.com/~william/projects/luaossl.html>
-- lualdap >= 1.2 -- lualdap >= 1.2
-- <https://git.zx2c4.com/lualdap/about/> -- <https://git.zx2c4.com/lualdap/about/>
-- luaposix -- luaposix
@ -10,9 +10,9 @@
-- --
local sysstat = require("posix.sys.stat") local sysstat = require("posix.sys.stat")
local unistd = require("posix.unistd") local unistd = require("posix.unistd")
local crypto = require("crypto")
local lualdap = require("lualdap") local lualdap = require("lualdap")
local rand = require("openssl.rand")
local hmac = require("openssl.hmac")
-- --
-- --
@ -226,6 +226,13 @@ function get_cookie(cookies, name)
return string.match(cookies, ";" .. name .. "=(.-);") return string.match(cookies, ";" .. name .. "=(.-);")
end end
function tohex(b)
local x = ""
for i = 1, #b do
x = x .. string.format("%.2x", string.byte(b, i))
end
return x
end
-- --
-- --
@ -243,12 +250,12 @@ function get_secret()
local secret_file = io.open(secret_filename, "r") local secret_file = io.open(secret_filename, "r")
if secret_file == nil then if secret_file == nil then
local old_umask = sysstat.umask(63) local old_umask = sysstat.umask(63)
local temporary_filename = secret_filename .. ".tmp." .. crypto.hex(crypto.rand.bytes(16)) local temporary_filename = secret_filename .. ".tmp." .. tohex(rand.bytes(16))
local temporary_file = io.open(temporary_filename, "w") local temporary_file = io.open(temporary_filename, "w")
if temporary_file == nil then if temporary_file == nil then
os.exit(177) os.exit(177)
end end
temporary_file:write(crypto.hex(crypto.rand.bytes(32))) temporary_file:write(tohex(rand.bytes(32)))
temporary_file:close() temporary_file:close()
unistd.link(temporary_filename, secret_filename) -- Intentionally fails in the case that another process is doing the same. unistd.link(temporary_filename, secret_filename) -- Intentionally fails in the case that another process is doing the same.
unistd.unlink(temporary_filename) unistd.unlink(temporary_filename)
@ -273,7 +280,7 @@ function validate_value(expected_field, cookie)
local field = "" local field = ""
local expiration = 0 local expiration = 0
local salt = "" local salt = ""
local hmac = "" local chmac = ""
if cookie == nil or cookie:len() < 3 or cookie:sub(1, 1) == "|" then if cookie == nil or cookie:len() < 3 or cookie:sub(1, 1) == "|" then
return nil return nil
@ -292,19 +299,19 @@ function validate_value(expected_field, cookie)
elseif i == 3 then elseif i == 3 then
salt = component salt = component
elseif i == 4 then elseif i == 4 then
hmac = component chmac = component
else else
break break
end end
i = i + 1 i = i + 1
end end
if hmac == nil or hmac:len() == 0 then if chmac == nil or chmac:len() == 0 then
return nil return nil
end end
-- Lua hashes strings, so these comparisons are time invariant. -- Lua hashes strings, so these comparisons are time invariant.
if hmac ~= crypto.hmac.digest("sha256", field .. "|" .. value .. "|" .. tostring(expiration) .. "|" .. salt, get_secret()) then if chmac ~= tohex(hmac.new(get_secret(), "sha256"):final(field .. "|" .. value .. "|" .. tostring(expiration) .. "|" .. salt)) then
return nil return nil
end end
@ -325,11 +332,11 @@ function secure_value(field, value, expiration)
end end
local authstr = "" local authstr = ""
local salt = crypto.hex(crypto.rand.bytes(16)) local salt = tohex(rand.bytes(16))
value = url_encode(value) value = url_encode(value)
field = url_encode(field) field = url_encode(field)
authstr = field .. "|" .. value .. "|" .. tostring(expiration) .. "|" .. salt authstr = field .. "|" .. value .. "|" .. tostring(expiration) .. "|" .. salt
authstr = authstr .. "|" .. crypto.hmac.digest("sha256", authstr, get_secret()) authstr = authstr .. "|" .. tohex(hmac.new(get_secret(), "sha256"):final(authstr))
return authstr return authstr
end end

View File

@ -1,15 +1,15 @@
-- This script may be used with the auth-filter. Be sure to configure it as you wish. -- This script may be used with the auth-filter. Be sure to configure it as you wish.
-- --
-- Requirements: -- Requirements:
-- luacrypto >= 0.3 -- luaossl
-- <http://mkottman.github.io/luacrypto/> -- <http://25thandclement.com/~william/projects/luaossl.html>
-- luaposix -- luaposix
-- <https://github.com/luaposix/luaposix> -- <https://github.com/luaposix/luaposix>
-- --
local sysstat = require("posix.sys.stat") local sysstat = require("posix.sys.stat")
local unistd = require("posix.unistd") local unistd = require("posix.unistd")
local crypto = require("crypto") local rand = require("openssl.rand")
local hmac = require("openssl.hmac")
-- --
-- --
@ -180,6 +180,13 @@ function get_cookie(cookies, name)
return url_decode(string.match(cookies, ";" .. name .. "=(.-);")) return url_decode(string.match(cookies, ";" .. name .. "=(.-);"))
end end
function tohex(b)
local x = ""
for i = 1, #b do
x = x .. string.format("%.2x", string.byte(b, i))
end
return x
end
-- --
-- --
@ -197,12 +204,12 @@ function get_secret()
local secret_file = io.open(secret_filename, "r") local secret_file = io.open(secret_filename, "r")
if secret_file == nil then if secret_file == nil then
local old_umask = sysstat.umask(63) local old_umask = sysstat.umask(63)
local temporary_filename = secret_filename .. ".tmp." .. crypto.hex(crypto.rand.bytes(16)) local temporary_filename = secret_filename .. ".tmp." .. tohex(rand.bytes(16))
local temporary_file = io.open(temporary_filename, "w") local temporary_file = io.open(temporary_filename, "w")
if temporary_file == nil then if temporary_file == nil then
os.exit(177) os.exit(177)
end end
temporary_file:write(crypto.hex(crypto.rand.bytes(32))) temporary_file:write(tohex(rand.bytes(32)))
temporary_file:close() temporary_file:close()
unistd.link(temporary_filename, secret_filename) -- Intentionally fails in the case that another process is doing the same. unistd.link(temporary_filename, secret_filename) -- Intentionally fails in the case that another process is doing the same.
unistd.unlink(temporary_filename) unistd.unlink(temporary_filename)
@ -227,7 +234,7 @@ function validate_value(expected_field, cookie)
local field = "" local field = ""
local expiration = 0 local expiration = 0
local salt = "" local salt = ""
local hmac = "" local chmac = ""
if cookie == nil or cookie:len() < 3 or cookie:sub(1, 1) == "|" then if cookie == nil or cookie:len() < 3 or cookie:sub(1, 1) == "|" then
return nil return nil
@ -246,19 +253,19 @@ function validate_value(expected_field, cookie)
elseif i == 3 then elseif i == 3 then
salt = component salt = component
elseif i == 4 then elseif i == 4 then
hmac = component chmac = component
else else
break break
end end
i = i + 1 i = i + 1
end end
if hmac == nil or hmac:len() == 0 then if chmac == nil or chmac:len() == 0 then
return nil return nil
end end
-- Lua hashes strings, so these comparisons are time invariant. -- Lua hashes strings, so these comparisons are time invariant.
if hmac ~= crypto.hmac.digest("sha256", field .. "|" .. value .. "|" .. tostring(expiration) .. "|" .. salt, get_secret()) then if chmac ~= tohex(hmac.new(get_secret(), "sha256"):final(field .. "|" .. value .. "|" .. tostring(expiration) .. "|" .. salt)) then
return nil return nil
end end
@ -279,11 +286,11 @@ function secure_value(field, value, expiration)
end end
local authstr = "" local authstr = ""
local salt = crypto.hex(crypto.rand.bytes(16)) local salt = tohex(rand.bytes(16))
value = url_encode(value) value = url_encode(value)
field = url_encode(field) field = url_encode(field)
authstr = field .. "|" .. value .. "|" .. tostring(expiration) .. "|" .. salt authstr = field .. "|" .. value .. "|" .. tostring(expiration) .. "|" .. salt
authstr = authstr .. "|" .. crypto.hmac.digest("sha256", authstr, get_secret()) authstr = authstr .. "|" .. tohex(hmac.new(get_secret(), "sha256"):final(authstr))
return authstr return authstr
end end