Skip to content

Commit cf1840a

Browse files
bungleleafo
authored andcommitted
pgmoon.crypto x509 digest to fallback to available lib
1 parent bac90d5 commit cf1840a

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

pgmoon/crypto.lua

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,31 @@ else
7878
return error("Either luaossl or resty.openssl is required to generate random bytes")
7979
end
8080
end
81+
local x509_digest
82+
if pcall(function()
83+
return require("openssl.x509")
84+
end) then
85+
local x509 = require("openssl.x509")
86+
x509_digest = function(pem, hash_type)
87+
return x509.new(pem, "PEM"):digest(hash_type, "s")
88+
end
89+
elseif pcall(function()
90+
return require("resty.openssl.x509")
91+
end) then
92+
local x509 = require("resty.openssl.x509")
93+
x509_digest = function(pem, hash_type)
94+
return x509.new(pem, "PEM"):digest(hash_type)
95+
end
96+
else
97+
x509_digest = function()
98+
return error("Either luaossl or resty.openssl is required to calculate x509 digest")
99+
end
100+
end
81101
return {
82102
md5 = md5,
83103
hmac_sha256 = hmac_sha256,
84104
digest_sha256 = digest_sha256,
85105
kdf_derive_sha256 = kdf_derive_sha256,
86-
random_bytes = random_bytes
106+
random_bytes = random_bytes,
107+
x509_digest = x509_digest
87108
}

pgmoon/crypto.moon

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,14 @@ else
5757
-> error "Either luaossl or resty.openssl is required to generate random bytes"
5858

5959

60-
{ :md5, :hmac_sha256, :digest_sha256, :kdf_derive_sha256, :random_bytes }
60+
x509_digest = if pcall -> require "openssl.x509"
61+
x509 = require "openssl.x509"
62+
(pem, hash_type) -> x509.new(pem, "PEM")\digest(hash_type, "s")
63+
elseif pcall -> require "resty.openssl.x509"
64+
x509 = require "resty.openssl.x509"
65+
(pem, hash_type) -> x509.new(pem, "PEM")\digest(hash_type)
66+
else
67+
-> error "Either luaossl or resty.openssl is required to calculate x509 digest"
68+
69+
70+
{ :md5, :hmac_sha256, :digest_sha256, :kdf_derive_sha256, :random_bytes, :x509_digest }

pgmoon/init.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,11 @@ do
306306
end,
307307
scram_sha_256_auth = function(self, msg)
308308
assert(self.config.password, "missing password, required for connect")
309-
local random_bytes
310-
random_bytes = require("pgmoon.crypto").random_bytes
309+
local random_bytes, x509_digest
310+
do
311+
local _obj_0 = require("pgmoon.crypto")
312+
random_bytes, x509_digest = _obj_0.random_bytes, _obj_0.x509_digest
313+
end
311314
local rand_bytes = assert(random_bytes(18))
312315
local encode_base64
313316
encode_base64 = require("pgmoon.util").encode_base64
@@ -358,8 +361,7 @@ do
358361
if signature:match("^md5") or signature:match("^sha1") then
359362
signature = "sha256"
360363
end
361-
local openssl_x509 = require("openssl.x509").new(pem, "PEM")
362-
cbind_data = assert(openssl_x509:digest(signature, "s"))
364+
cbind_data = assert(x509_digest(pem, signature))
363365
end
364366
end
365367
cbind_input = gs2_header .. cbind_data

pgmoon/init.moon

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ class Postgres
298298
scram_sha_256_auth: (msg) =>
299299
assert @config.password, "missing password, required for connect"
300300

301-
import random_bytes from require "pgmoon.crypto"
301+
import random_bytes, x509_digest from require "pgmoon.crypto"
302302

303303
-- '18' is the number set by postgres on the server side
304304
rand_bytes = assert random_bytes 18
@@ -356,8 +356,7 @@ class Postgres
356356
if signature\match("^md5") or signature\match("^sha1")
357357
signature = "sha256"
358358

359-
openssl_x509 = require("openssl.x509").new(pem, "PEM")
360-
assert openssl_x509\digest(signature, "s")
359+
assert x509_digest(pem, signature)
361360

362361
cbind_input = gs2_header .. cbind_data
363362

0 commit comments

Comments
 (0)