aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwbond <will@wbond.net>2022-02-12 11:21:07 -0500
committerwbond <will@wbond.net>2022-02-12 11:21:07 -0500
commit5a24aed1b99e7f51ba38183aebbcce86e6eb4d23 (patch)
tree2230eeea14e584b3a6d51a8761ff587598cd980f
parent6cffd3dc5d9300cfa2ba4b9a0566c11b66c1b436 (diff)
downloadasn1crypto-5a24aed1b99e7f51ba38183aebbcce86e6eb4d23.tar.gz
Fix calling .dump(force=True) on a core.Sequence() with no field info
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.
-rw-r--r--asn1crypto/core.py4
-rw-r--r--tests/test_core.py12
2 files changed, 16 insertions, 0 deletions
diff --git a/asn1crypto/core.py b/asn1crypto/core.py
index 7133367..364c6b5 100644
--- a/asn1crypto/core.py
+++ b/asn1crypto/core.py
@@ -4113,6 +4113,10 @@ class Sequence(Asn1Value):
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)
diff --git a/tests/test_core.py b/tests/test_core.py
index 9821c63..7ac9196 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -978,6 +978,18 @@ class CoreTests(unittest.TestCase):
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())