Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion luajwtjitsi-1.3-7.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ description = {

dependencies = {
"lua >= 5.1",
"luacrypto >= 0.3.2-1",
"luaossl >= 20190731-0",
"lua-cjson >= 2.1.0",
"lbase64 >= 20120807-3"
}
Expand Down
29 changes: 21 additions & 8 deletions luajwtjitsi.lua
Original file line number Diff line number Diff line change
@@ -1,29 +1,42 @@
local cjson = require 'cjson'
local base64 = require 'base64'
local crypto = require 'crypto'
local openssl = require 'openssl'
local pkey = require 'openssl.pkey'
local digest = require 'openssl.digest'
local hmac = require 'openssl.hmac'

local function signRS (data, key, algo)
local privkey = crypto.pkey.from_pem(key, true)
local privkey = pkey.new(key, "PEM")
if privkey == nil then
return nil, 'Not a private PEM key'
else
return crypto.sign(algo, data, privkey)
local digest_o = digest.new(algo)
local digest_result = digest_o:final(digest_o, data)
return privkey:sign(digest_result)
end
end

local function verifyRS (data, signature, key, algo)
local pubkey = crypto.pkey.from_pem(key)
local pubkey = pkey.new(key, "PEM")
if pubkey == nil then
return nil, 'Not a public PEM key'
else
return crypto.verify(algo, data, signature, pubkey)
local digest_o = digest.new(algo)
local digest_result = digest_o:final(data)
return pubkey:verify(digest_result, signature)
end
end

local function hmacDigest(algo, data, key)
local digest_o = hmac.new(key, algo)
return digest_o:final(data)

end

local alg_sign = {
['HS256'] = function(data, key) return crypto.hmac.digest('sha256', data, key, true) end,
['HS384'] = function(data, key) return crypto.hmac.digest('sha384', data, key, true) end,
['HS512'] = function(data, key) return crypto.hmac.digest('sha512', data, key, true) end,
['HS256'] = function(data, key) return hmacDigest('sha256', data, key) end,
['HS384'] = function(data, key) return hmacDigest('sha384', data, key) end,
['HS512'] = function(data, key) return hmacDigest('sha512', data, key) end,
['RS256'] = function(data, key) return signRS(data, key, 'sha256') end,
['RS384'] = function(data, key) return signRS(data, key, 'sha384') end,
['RS512'] = function(data, key) return signRS(data, key, 'sha512') end
Expand Down