about summary refs log tree commit diff
path: root/filters/file-authentication.lua
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2019-01-03 02:11:14 +0100
committerJason A. Donenfeld <Jason@zx2c4.com>2019-01-03 02:12:16 +0100
commit7d87cd3a215976a480b3c71b017a191597e5cb44 (patch)
tree70d600e62e9aaacc34993cc169a46f05cbe10f0e /filters/file-authentication.lua
parentui-shared: fix broken sizeof in title setting and rewrite (diff)
downloadcgit-pink-7d87cd3a215976a480b3c71b017a191597e5cb44.tar.gz
cgit-pink-7d87cd3a215976a480b3c71b017a191597e5cb44.zip
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>
Diffstat (limited to 'filters/file-authentication.lua')
-rw-r--r--filters/file-authentication.lua31
1 files changed, 19 insertions, 12 deletions
diff --git a/filters/file-authentication.lua b/filters/file-authentication.lua
index 6ee1e19..0248804 100644
--- a/filters/file-authentication.lua
+++ b/filters/file-authentication.lua
@@ -1,15 +1,15 @@
 -- This script may be used with the auth-filter.
 --
 -- Requirements:
--- 	luacrypto >= 0.3
--- 	<http://mkottman.github.io/luacrypto/>
+-- 	luaossl
+-- 	<http://25thandclement.com/~william/projects/luaossl.html>
 -- 	luaposix
 -- 	<https://github.com/luaposix/luaposix>
 --
 local sysstat = require("posix.sys.stat")
 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:
 --	username1:hash1
@@ -225,6 +225,13 @@ function get_cookie(cookies, name)
 	return url_decode(string.match(cookies, ";" .. name .. "=(.-);"))
 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")
 	if secret_file == nil then
 		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")
 		if temporary_file == nil then
 			os.exit(177)
 		end
-		temporary_file:write(crypto.hex(crypto.rand.bytes(32)))
+		temporary_file:write(tohex(rand.bytes(32)))
 		temporary_file:close()
 		unistd.link(temporary_filename, secret_filename) -- Intentionally fails in the case that another process is doing the same.
 		unistd.unlink(temporary_filename)
@@ -272,7 +279,7 @@ function validate_value(expected_field, cookie)
 	local field = ""
 	local expiration = 0
 	local salt = ""
-	local hmac = ""
+	local chmac = ""
 
 	if cookie == nil or cookie:len() < 3 or cookie:sub(1, 1) == "|" then
 		return nil
@@ -291,19 +298,19 @@ function validate_value(expected_field, cookie)
 		elseif i == 3 then
 			salt = component
 		elseif i == 4 then
-			hmac = component
+			chmac = component
 		else
 			break
 		end
 		i = i + 1
 	end
 
-	if hmac == nil or hmac:len() == 0 then
+	if chmac == nil or chmac:len() == 0 then
 		return nil
 	end
 
 	-- 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
 	end
 
@@ -324,11 +331,11 @@ function secure_value(field, value, expiration)
 	end
 
 	local authstr = ""
-	local salt = crypto.hex(crypto.rand.bytes(16))
+	local salt = tohex(rand.bytes(16))
 	value = url_encode(value)
 	field = url_encode(field)
 	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
 end