File

util/paseto.lua @ 13652:a08065207ef0

net.server_epoll: Call :shutdown() on TLS sockets when supported Comment from Matthew: This fixes a potential issue where the Prosody process gets blocked on sockets waiting for them to close. Unlike non-TLS sockets, closing a TLS socket sends layer 7 data, and this can cause problems for sockets which are in the process of being cleaned up. This depends on LuaSec changes which are not yet upstream. From Martijn's original email: So first my analysis of luasec. in ssl.c the socket is put into blocking mode right before calling SSL_shutdown() inside meth_destroy(). My best guess to why this is is because meth_destroy is linked to the __close and __gc methods, which can't exactly be called multiple times and luasec does want to make sure that a tls session is shutdown as clean as possible. I can't say I disagree with this reasoning and don't want to change this behaviour. My solution to this without changing the current behaviour is to introduce a shutdown() method. I am aware that this overlaps in a conflicting way with tcp's shutdown method, but it stays close to the OpenSSL name. This method calls SSL_shutdown() in the current (non)blocking mode of the underlying socket and returns a boolean whether or not the shutdown is completed (matching SSL_shutdown()'s 0 or 1 return values), and returns the familiar ssl_ioerror() strings on error with a false for completion. This error can then be used to determine if we have wantread/wantwrite to finalize things. Once meth_shutdown() has been called once a shutdown flag will be set, which indicates to meth_destroy() that the SSL_shutdown() has been handled by the application and it shouldn't be needed to set the socket to blocking mode. I've left the SSL_shutdown() call in the LSEC_STATE_CONNECTED to prevent TOCTOU if the application reaches a timeout for the shutdown code, which might allow SSL_shutdown() to clean up anyway at the last possible moment. Another thing I've changed to luasec is the call to socket_setblocking() right before calling close(2) in socket_destroy() in usocket.c. According to the latest POSIX[0]: Note that the requirement for close() on a socket to block for up to the current linger interval is not conditional on the O_NONBLOCK setting. Which I read to mean that removing O_NONBLOCK on the socket before close doesn't impact the behaviour and only causes noise in system call tracers. I didn't touch the windows bits of this, since I don't do windows. For the prosody side of things I've made the TLS shutdown bits resemble interface:onwritable(), and put it under a combined guard of self._tls and self.conn.shutdown. The self._tls bit is there to prevent getting stuck on this condition, and self.conn.shutdown is there to prevent the code being called by instances where the patched luasec isn't deployed. The destroy() method can be called from various places and is read by me as the "we give up" error path. To accommodate for these unexpected entrypoints I've added a single call to self.conn:shutdown() to prevent the socket being put into blocking mode. I have no expectations that there is any other use here. Same as previous, the self.conn.shutdown check is there to make sure it's not called on unpatched luasec deployments and self._tls is there to make sure we don't call shutdown() on tcp sockets. I wouldn't recommend logging of the conn:shutdown() error inside close(), since a lot of clients simply close the connection before SSL_shutdown() is done.
author Martijn van Duren <martijn@openbsd.org>
date Thu, 06 Feb 2025 15:04:38 +0000
parent 12975:d10957394a3c
line wrap: on
line source

local crypto = require "prosody.util.crypto";
local json = require "prosody.util.json";
local hashes = require "prosody.util.hashes";
local base64_encode = require "prosody.util.encodings".base64.encode;
local base64_decode = require "prosody.util.encodings".base64.decode;
local secure_equals = require "prosody.util.hashes".equals;
local bit = require "prosody.util.bitcompat";
local hex = require "prosody.util.hex";
local rand = require "prosody.util.random";
local s_pack = require "prosody.util.struct".pack;

local s_gsub = string.gsub;

local v4_public = {};

local b64url_rep = { ["+"] = "-", ["/"] = "_", ["="] = "", ["-"] = "+", ["_"] = "/" };
local function b64url(data)
	return (s_gsub(base64_encode(data), "[+/=]", b64url_rep));
end

local valid_tails = {
	nil; -- Always invalid
	"^.[AQgw]$"; -- b??????00
	"^..[AQgwEUk0IYo4Mcs8]$"; -- b????0000
}

local function unb64url(data)
	local rem = #data%4;
	if data:sub(-1,-1) == "=" or rem == 1 or (rem > 1 and not data:sub(-rem):match(valid_tails[rem])) then
		return nil;
	end
	return base64_decode(s_gsub(data, "[-_]", b64url_rep).."==");
end

local function le64(n)
	return s_pack("<I8", bit.band(n, 0x7F));
end

local function pae(parts)
	if type(parts) ~= "table" then
		error("bad argument #1 to 'pae' (table expected, got "..type(parts)..")");
	end
	local o = { le64(#parts) };
	for _, part in ipairs(parts) do
		table.insert(o, le64(#part)..part);
	end
	return table.concat(o);
end

function v4_public.sign(m, sk, f, i)
	if type(m) ~= "table" then
		return nil, "PASETO payloads must be a table";
	end
	m = json.encode(m);
	local h = "v4.public.";
	local m2 = pae({ h, m, f or "", i or "" });
	local sig = crypto.ed25519_sign(sk, m2);
	if not f or f == "" then
		return h..b64url(m..sig);
	else
		return h..b64url(m..sig).."."..b64url(f);
	end
end

function v4_public.verify(tok, pk, expected_f, i)
	local h, sm, f = tok:match("^(v4%.public%.)([^%.]+)%.?(.*)$");
	if not h then
		return nil, "invalid-token-format";
	end
	f = f and unb64url(f) or nil;
	if expected_f then
		if not f or not secure_equals(expected_f, f) then
			return nil, "invalid-footer";
		end
	end
	local raw_sm = unb64url(sm);
	if not raw_sm or #raw_sm <= 64 then
		return nil, "invalid-token-format";
	end
	local s, m = raw_sm:sub(-64), raw_sm:sub(1, -65);
	local m2 = pae({ h, m, f or "", i or "" });
	local ok = crypto.ed25519_verify(pk, m2, s);
	if not ok then
		return nil, "invalid-token";
	end
	local payload, err = json.decode(m);
	if err ~= nil or type(payload) ~= "table" then
		return nil, "json-decode-error";
	end
	return payload;
end

v4_public.import_private_key = crypto.import_private_pem;
v4_public.import_public_key = crypto.import_public_pem;
function v4_public.new_keypair()
	return crypto.generate_ed25519_keypair();
end

function v4_public.init(private_key_pem, public_key_pem, options)
	local sign, verify = v4_public.sign, v4_public.verify;
	local public_key = public_key_pem and v4_public.import_public_key(public_key_pem);
	local private_key = private_key_pem and v4_public.import_private_key(private_key_pem);
	local default_footer = options and options.default_footer;
	local default_assertion = options and options.default_implicit_assertion;
	return private_key and function (token, token_footer, token_assertion)
		return sign(token, private_key, token_footer or default_footer, token_assertion or default_assertion);
	end, public_key and function (token, expected_footer, token_assertion)
		return verify(token, public_key, expected_footer or default_footer, token_assertion or default_assertion);
	end;
end

function v4_public.new_signer(private_key_pem, options)
	return (v4_public.init(private_key_pem, nil, options));
end

function v4_public.new_verifier(public_key_pem, options)
	return (select(2, v4_public.init(nil, public_key_pem, options)));
end

local v3_local = { _key_mt = {} };

local function v3_local_derive_keys(k, n)
	local tmp = hashes.hkdf_hmac_sha384(48, k, nil, "paseto-encryption-key"..n);
	local Ek = tmp:sub(1, 32);
	local n2 = tmp:sub(33);
	local Ak = hashes.hkdf_hmac_sha384(48, k, nil, "paseto-auth-key-for-aead"..n);
	return Ek, Ak, n2;
end

function v3_local.encrypt(m, k, f, i)
	assert(#k == 32)
	if type(m) ~= "table" then
		return nil, "PASETO payloads must be a table";
	end
	m = json.encode(m);
	local h = "v3.local.";
	local n = rand.bytes(32);
	local Ek, Ak, n2 = v3_local_derive_keys(k, n);

	local c = crypto.aes_256_ctr_encrypt(Ek, n2, m);
	local m2 = pae({ h, n, c, f or "", i or "" });
	local t = hashes.hmac_sha384(Ak, m2);

	if not f or f == "" then
		return h..b64url(n..c..t);
	else
		return h..b64url(n..c..t).."."..b64url(f);
	end
end

function v3_local.decrypt(tok, k, expected_f, i)
	assert(#k == 32)

	local h, sm, f = tok:match("^(v3%.local%.)([^%.]+)%.?(.*)$");
	if not h then
		return nil, "invalid-token-format";
	end
	f = f and unb64url(f) or nil;
	if expected_f then
		if not f or not secure_equals(expected_f, f) then
			return nil, "invalid-footer";
		end
	end
	local m = unb64url(sm);
	if not m or #m <= 80 then
		return nil, "invalid-token-format";
	end
	local n, c, t = m:sub(1, 32), m:sub(33, -49), m:sub(-48);
	local Ek, Ak, n2 = v3_local_derive_keys(k, n);
	local preAuth = pae({ h, n, c, f or "", i or "" });
	local t2 = hashes.hmac_sha384(Ak, preAuth);
	if not secure_equals(t, t2) then
		return nil, "invalid-token";
	end
	local m2 = crypto.aes_256_ctr_decrypt(Ek, n2, c);
	if not m2 then
		return nil, "invalid-token";
	end

	local payload, err = json.decode(m2);
	if err ~= nil or type(payload) ~= "table" then
		return nil, "json-decode-error";
	end
	return payload;
end

function v3_local.new_key()
	return "secret-token:paseto.v3.local:"..hex.encode(rand.bytes(32));
end

function v3_local.init(key, options)
	local encoded_key = key:match("^secret%-token:paseto%.v3%.local:(%x+)$");
	if not encoded_key or #encoded_key ~= 64 then
		return error("invalid key for v3.local");
	end
	local raw_key = hex.decode(encoded_key);
	local default_footer = options and options.default_footer;
	local default_assertion = options and options.default_implicit_assertion;
	return function (token, token_footer, token_assertion)
		return v3_local.encrypt(token, raw_key, token_footer or default_footer, token_assertion or default_assertion);
	end, function (token, token_footer, token_assertion)
		return v3_local.decrypt(token, raw_key, token_footer or default_footer, token_assertion or default_assertion);
	end;
end

function v3_local.new_signer(key, options)
	return (v3_local.init(key, options));
end

function v3_local.new_verifier(key, options)
	return (select(2, v3_local.init(key, options)));
end

return {
	pae = pae;
	v3_local = v3_local;
	v4_public = v4_public;
};