Skip to content

Commit

Permalink
Fix calling .dump(force=True) on a core.Sequence() with no field info
Browse files Browse the repository at this point in the history
For instance, when parsing a CMS structure, we may not know the various
OIDs and the Sequence values they imply. Before this change, this could
result in the unknown Sequence values being encoded as an empty value.
  • Loading branch information
wbond committed Feb 12, 2022
1 parent 6cffd3d commit 5a24aed
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions asn1crypto/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4113,6 +4113,10 @@ def dump(self, force=False):
if self._header is not None and self._header[-1:] == b'\x80':
force = True

# We can't force encoding if we don't have a spec
if force and self._fields == [] and self.__class__ is Sequence:
force = False

if force:
self._set_contents(force=force)

Expand Down
12 changes: 12 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,18 @@ def test_dump_set(self):
st = SetTest({'two': 2, 'one': 1})
self.assertEqual(b'1\x06\x81\x01\x01\x82\x01\x02', st.dump())

def test_force_dump_unknown_sequence(self):
seq = Seq({
'id': '1.2.3',
'value': 1
})
der = seq.dump(force=True)
# Ensure we don't erase the contents of a sequence we don't know
# the fields for when force re-encoding
unknown_seq = core.Sequence.load(der)
unknown_der = unknown_seq.dump(force=True)
self.assertEqual(der, unknown_der)

def test_dump_set_of(self):
st = SetOfTest([3, 2, 1])
self.assertEqual(b'1\x09\x02\x01\x01\x02\x01\x02\x02\x01\x03', st.dump())
Expand Down

0 comments on commit 5a24aed

Please sign in to comment.