Skip to content

Commit f12cbfc

Browse files
authored
fix(pkey) use group bits instead of ECDSA_sig to get parameter size in ECDSA signature (#102)
1 parent 5c29a93 commit f12cbfc

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
lines changed

lib/resty/openssl/auxiliary/ecdsa.lua

+15-13
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ local C = ffi.C
77
require "resty.openssl.include.ecdsa"
88
local bn_lib = require "resty.openssl.bn"
99
local format_error = require("resty.openssl.err").format_error
10-
local floor = math.floor
10+
local ceil = math.ceil
1111

1212
local _M = {}
1313

@@ -26,20 +26,25 @@ SEQUENCE {
2626
The binary form is typically 64 bytes.
2727
]]
2828

29-
local function sig_size(ec_key)
30-
local sz = C.ECDSA_size(ec_key)
31-
if sz <= 8 then
32-
error("failed to get ECDSA signature size", 2)
29+
local function group_size(ec_key)
30+
local group = C.EC_KEY_get0_group(ec_key)
31+
if group == nil then
32+
assert("failed to get EC group", 2)
3333
end
34-
-- 2 bytes for ASN.1 DER header, 2 bytes for length, 2 bytes for each 2 integers, left 2 integers so /2
35-
return (sz - 8) / 2
34+
35+
local sz = C.EC_GROUP_order_bits(group)
36+
if sz <= 0 then
37+
assert("failed to get EC group order bits", 2)
38+
end
39+
40+
return ceil(sz / 8)
3641
end
3742

3843
_M.sig_der2raw = function(der, ec_key)
3944
if ec_key == nil then
4045
error("ec_key is required", 2)
4146
end
42-
local psize = sig_size(ec_key)
47+
local psize = group_size(ec_key)
4348

4449
local buf = ffi.new("const unsigned char*", der)
4550
local buf_ptr = ffi.new("const unsigned char*[1]", buf)
@@ -82,11 +87,8 @@ _M.sig_raw2der = function(bin, ec_key)
8287
if ec_key == nil then
8388
error("ec_key is required", 2)
8489
end
85-
-- p521 private key x point is 65 bytes and y point is 66 bytes
86-
-- 65+66+8 = 139
87-
-- division by two results in a decimal and hence messes with
88-
-- signature length calculation
89-
local psize = floor(sig_size(ec_key))
90+
91+
local psize = group_size(ec_key)
9092

9193
if #bin ~= psize * 2 then
9294
return nil, "invalid signature length, expect " .. (psize * 2) .. " but got " .. #bin

lib/resty/openssl/include/ecdsa.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ ffi.cdef [[
1212
int i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **pp);
1313
ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **sig, const unsigned char **pp, long len);
1414

15-
int ECDSA_size(const EC_KEY *eckey);
15+
int EC_GROUP_order_bits(const EC_GROUP *group);
1616
]]

t/openssl/pkey.t

+7-10
Original file line numberDiff line numberDiff line change
@@ -1332,8 +1332,7 @@ nilpkey:sign: ecdsa.sig_raw2der: invalid signature length, expect 64 but got \\d
13321332
--- config
13331333
location =/t {
13341334
content_by_lua_block {
1335-
if not require("resty.openssl.version").OPENSSL_11_OR_LATER or
1336-
require("resty.openssl.version").OPENSSL_3X then
1335+
if not require("resty.openssl.version").OPENSSL_11_OR_LATER then
13371336
ngx.say("132\n96\ntrue\ntrue")
13381337
ngx.exit(0)
13391338
end
@@ -1347,20 +1346,18 @@ nilpkey:sign: ecdsa.sig_raw2der: invalid signature length, expect 64 but got \\d
13471346
type = "EC",
13481347
curve = "secp384r1",
13491348
}))
1350-
local digest_512 = myassert(require("resty.openssl.digest").new("SHA512"))
1351-
local digest_384 = myassert(require("resty.openssl.digest").new("SHA384"))
1349+
local digest = myassert(require("resty.openssl.digest").new("SHA256"))
13521350
1353-
myassert(digest_512:update("🕶️", "+1s"))
1354-
myassert(digest_384:update("🕶️", "+1s"))
1351+
myassert(digest:update("🕶️", "+1s"))
13551352
1356-
local s_512 = myassert(p_521:sign(digest_512, nil, nil, opts))
1353+
local s_512 = myassert(p_521:sign(digest, nil, nil, opts))
13571354
ngx.say(#s_512)
1358-
local s_384 = myassert(p_384:sign(digest_384, nil, nil, opts))
1355+
local s_384 = myassert(p_384:sign(digest, nil, nil, opts))
13591356
ngx.say(#s_384)
13601357
1361-
local v_512 = myassert(p_521:verify(s_512, digest_512, nil, nil, opts))
1358+
local v_512 = myassert(p_521:verify(s_512, digest, nil, nil, opts))
13621359
ngx.say(v_512)
1363-
local v_384 = myassert(p_384:verify(s_384, digest_384, nil, nil, opts))
1360+
local v_384 = myassert(p_384:verify(s_384, digest, nil, nil, opts))
13641361
ngx.say(v_384)
13651362
}
13661363
}

0 commit comments

Comments
 (0)