Skip to content

Commit

Permalink
fix asn1 parse
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaozg committed Apr 5, 2024
1 parent cc995f8 commit 645fc6c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
15 changes: 3 additions & 12 deletions src/asn1.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,9 @@ static int openssl_get_object(lua_State*L)
int class = 0;
int ret;

if (start > l)
{
lua_pushnil(L);
openssl_pushargerror(L, 2, "out of range");
return 2;
}
if (start>stop)
{
lua_pushnil(L);
openssl_pushargerror(L, 3, "before of start");
return 2;
}
luaL_argcheck(L, start > 0 && start < l, 2, "start out of length of asn1 string");
luaL_argcheck(L, stop > start, 3, "stop must be greater than start");
luaL_argcheck(L, stop <= l, 3, "stop out of length of asn1 string");

p = (const unsigned char *)asn1s + start - 1;
ret = ASN1_get_object(&p, &len, &tag, &class, stop - start + 1);
Expand Down
24 changes: 23 additions & 1 deletion test/1.asn1.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local openssl = require 'openssl'
local lu = require 'luaunit'
local helper = require'helper'

local asn1 = openssl.asn1
local first = true
Expand Down Expand Up @@ -301,3 +300,26 @@ function TestType:testBasic()
assert(o:info())
assert(o:asn1string())
end

function TestType:testAll()
-- FIXME: need more code
local skip = {
[asn1.OBJECT] = true,
[asn1.SEQUENCE] = true,
[asn1.SET] = true,
[asn1.BMPSTRING] = true,
[asn1.UNIVERSALSTRING] = true
}
for i = asn1.BOOLEAN, asn1.BMPSTRING do
if not skip[i] then

local s = assert(asn1.new_string("octet", i))
local o = assert(asn1.new_type(s))
local d = assert(assert(o:i2d()))
assert(asn1.d2i_asn1type(d) == o)

assert(o:info())
end
end

end
21 changes: 15 additions & 6 deletions test/2.asn1.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ local function asn1parse(s, off, last, indent)

if first then
print(string.format('%sTAG=%s CLS=%s START=%s STOP=%s, %s',
string.rep(tab, indent), asn1.tostring(tag, 'tag'),
asn1.tostring(cls, 'class'), start, stop,
string.rep(tab, indent),
asn1.tostring(tag, 'tag'),
asn1.tostring(cls, 'class'),
start, stop,
cons and "CONS" or "PRIM"))
assert(asn1.tostring(tag, 'tag') == asn1.tostring(tag))
end
if cons then
table.insert(d, asn1.put_object(tag, cls, stop - start + 1, true))
Expand All @@ -48,11 +51,17 @@ end
TestAsn1_2 = {}
function TestAsn1_2.testParse()

d = {}
assert(#ss > 0)
-- fire error
asn1parse(ss, 8, 8)
asn1parse(ss, #ss+1)
asn1parse(ss, #ss+1, #ss-1)
lu.assertErrorMsgEquals(
"2.asn1.lua:24: bad argument #2 to 'get_object' (start out of length of asn1 string)",
asn1parse, ss, 0)
lu.assertErrorMsgEquals(
"2.asn1.lua:24: bad argument #2 to 'get_object' (start out of length of asn1 string)",
asn1parse, ss, #ss)
lu.assertErrorMsgEquals(
"2.asn1.lua:24: bad argument #3 to 'get_object' (stop out of length of asn1 string)",
asn1parse, ss, 1, #ss+1)

d = {}
asn1parse(ss)
Expand Down

0 comments on commit 645fc6c

Please sign in to comment.