Skip to content

Commit

Permalink
refactor(plugins): replace usage or resty.openssl.hmac with resty.ope…
Browse files Browse the repository at this point in the history
…nssl.mac (#12276)

Replace all usage of resty.openssl.hmac (which binds HMAC_* low level APIs) with resty.openssl.mac in Kong.


KAG-3445
  • Loading branch information
Water-Melon authored Jan 3, 2024
1 parent e22ac21 commit 5175e10
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
8 changes: 4 additions & 4 deletions kong/plugins/hmac-auth/access.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local constants = require "kong.constants"
local openssl_hmac = require "resty.openssl.hmac"
local openssl_mac = require "resty.openssl.mac"


local sha256_base64 = require("kong.tools.sha256").sha256_base64
Expand Down Expand Up @@ -37,13 +37,13 @@ local hmac = {
return hmac_sha1(secret, data)
end,
["hmac-sha256"] = function(secret, data)
return openssl_hmac.new(secret, "sha256"):final(data)
return openssl_mac.new(secret, "HMAC", nil, "sha256"):final(data)
end,
["hmac-sha384"] = function(secret, data)
return openssl_hmac.new(secret, "sha384"):final(data)
return openssl_mac.new(secret, "HMAC", nil, "sha384"):final(data)
end,
["hmac-sha512"] = function(secret, data)
return openssl_hmac.new(secret, "sha512"):final(data)
return openssl_mac.new(secret, "HMAC", nil, "sha512"):final(data)
end,
}

Expand Down
8 changes: 4 additions & 4 deletions kong/plugins/jwt/jwt_parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local json = require "cjson"
local b64 = require "ngx.base64"
local buffer = require "string.buffer"
local openssl_digest = require "resty.openssl.digest"
local openssl_hmac = require "resty.openssl.hmac"
local openssl_mac = require "resty.openssl.mac"
local openssl_pkey = require "resty.openssl.pkey"


Expand All @@ -33,9 +33,9 @@ local decode_base64url = b64.decode_base64url

--- Supported algorithms for signing tokens.
local alg_sign = {
HS256 = function(data, key) return openssl_hmac.new(key, "sha256"):final(data) end,
HS384 = function(data, key) return openssl_hmac.new(key, "sha384"):final(data) end,
HS512 = function(data, key) return openssl_hmac.new(key, "sha512"):final(data) end,
HS256 = function(data, key) return openssl_mac.new(key, "HMAC", nil, "sha256"):final(data) end,
HS384 = function(data, key) return openssl_mac.new(key, "HMAC", nil, "sha384"):final(data) end,
HS512 = function(data, key) return openssl_mac.new(key, "HMAC", nil, "sha512"):final(data) end,
RS256 = function(data, key)
local digest = openssl_digest.new("sha256")
assert(digest:update(data))
Expand Down
16 changes: 8 additions & 8 deletions spec/03-plugins/19-hmac-auth/03-access_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local cjson = require "cjson"
local openssl_hmac = require "resty.openssl.hmac"
local openssl_mac = require "resty.openssl.mac"
local helpers = require "spec.helpers"
local utils = require "kong.tools.utils"
local resty_sha256 = require "resty.sha256"
Expand All @@ -8,7 +8,7 @@ local fmt = string.format


local hmac_sha1_binary = function(secret, data)
return openssl_hmac.new(secret, "sha1"):final(data)
return openssl_mac.new(secret, "HMAC", nil, "sha1"):final(data)
end


Expand Down Expand Up @@ -816,7 +816,7 @@ for _, strategy in helpers.each_strategy() do
it("should not pass with GET with wrong algorithm", function()
local date = os.date("!%a, %d %b %Y %H:%M:%S GMT")
local encodedSignature = ngx.encode_base64(
openssl_hmac.new("secret", "sha256"):final("date: " .. date .. "\n"
openssl_mac.new("secret", "HMAC", nil, "sha256"):final("date: " .. date .. "\n"
.. "content-md5: md5" .. "\nGET /request HTTP/1.1"))
local hmacAuth = [[hmac username="bob",algorithm="hmac-sha",]]
.. [[ headers="date content-md5 request-line",signature="]]
Expand All @@ -839,7 +839,7 @@ for _, strategy in helpers.each_strategy() do
it("should pass the right headers to the upstream server", function()
local date = os.date("!%a, %d %b %Y %H:%M:%S GMT")
local encodedSignature = ngx.encode_base64(
openssl_hmac.new("secret", "sha256"):final("date: " .. date .. "\n"
openssl_mac.new("secret", "HMAC", nil, "sha256"):final("date: " .. date .. "\n"
.. "content-md5: md5" .. "\nGET /request HTTP/1.1"))
local hmacAuth = [[hmac username="bob",algorithm="hmac-sha256",]]
.. [[ headers="date content-md5 request-line",signature="]]
Expand Down Expand Up @@ -1592,7 +1592,7 @@ for _, strategy in helpers.each_strategy() do
it("should pass with GET with hmac-sha384", function()
local date = os.date("!%a, %d %b %Y %H:%M:%S GMT")
local encodedSignature = ngx.encode_base64(
openssl_hmac.new("secret", "sha384"):final("date: " .. date .. "\n"
openssl_mac.new("secret", "HMAC", nil, "sha384"):final("date: " .. date .. "\n"
.. "content-md5: md5" .. "\nGET /request HTTP/1.1"))
local hmacAuth = [[hmac username="bob", algorithm="hmac-sha384", ]]
.. [[headers="date content-md5 request-line", signature="]]
Expand All @@ -1614,7 +1614,7 @@ for _, strategy in helpers.each_strategy() do
it("should pass with GET with hmac-sha512", function()
local date = os.date("!%a, %d %b %Y %H:%M:%S GMT")
local encodedSignature = ngx.encode_base64(
openssl_hmac.new("secret", "sha512"):final("date: " .. date .. "\n"
openssl_mac.new("secret", "HMAC", nil, "sha512"):final("date: " .. date .. "\n"
.. "content-md5: md5" .. "\nGET /request HTTP/1.1"))
local hmacAuth = [[hmac username="bob", algorithm="hmac-sha512", ]]
.. [[headers="date content-md5 request-line", signature="]]
Expand All @@ -1636,7 +1636,7 @@ for _, strategy in helpers.each_strategy() do
it("should not pass with hmac-sha512", function()
local date = os.date("!%a, %d %b %Y %H:%M:%S GMT")
local encodedSignature = ngx.encode_base64(
openssl_hmac.new("secret", "sha512"):final("date: " .. date .. "\n"
openssl_mac.new("secret", "HMAC", nil, "sha512"):final("date: " .. date .. "\n"
.. "content-md5: md5" .. "\nGET /request HTTP/1.1"))
local hmacAuth = [[hmac username="bob", algorithm="hmac-sha512", ]]
.. [[headers="date content-md5 request-line", signature="]]
Expand Down Expand Up @@ -1673,7 +1673,7 @@ for _, strategy in helpers.each_strategy() do
it("should pass with hmac-sha1", function()
local date = os.date("!%a, %d %b %Y %H:%M:%S GMT")
local encodedSignature = ngx.encode_base64(
openssl_hmac.new("secret", "sha1"):final("date: " .. date .. "\n"
openssl_mac.new("secret", "HMAC", nil, "sha1"):final("date: " .. date .. "\n"
.. "content-md5: md5" .. "\nGET /request HTTP/1.1"))
local hmacAuth = [[hmac username="bob", algorithm="hmac-sha1", ]]
.. [[headers="date content-md5 request-line", signature="]]
Expand Down
4 changes: 2 additions & 2 deletions spec/03-plugins/19-hmac-auth/04-invalidations_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local helpers = require "spec.helpers"
local cjson = require "cjson"
local openssl_hmac = require "resty.openssl.hmac"
local openssl_mac = require "resty.openssl.mac"

for _, strategy in helpers.each_strategy() do
describe("Plugin: hmac-auth (invalidations) [#" .. strategy .. "]", function()
Expand Down Expand Up @@ -62,7 +62,7 @@ for _, strategy in helpers.each_strategy() do
end)

local function hmac_sha1_binary(secret, data)
return openssl_hmac.new(secret, "sha1"):final(data)
return openssl_mac.new(secret, "HMAC", nil, "sha1"):final(data)
end

local function get_authorization(username)
Expand Down

0 comments on commit 5175e10

Please sign in to comment.