Skip to content

Commit

Permalink
fixed crash on exp tagged Sequence component encoding (#79)
Browse files Browse the repository at this point in the history
Also EOO encoder call replaced with a constant outcome
  • Loading branch information
etingof authored Sep 20, 2017
1 parent aaf2ad1 commit 8b62fa2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
Revision 0.3.6, released XX-09-2017
-----------------------------------

- End-of-octets encoding optimized at ASN.1 encoders
- The __getitem__/__setitem__ behavior of Set/Sequence and SetOf/SequenceOf
objects aligned with the canonical Mapping and Sequence protocols in part
- Fixed crash in ASN.1 encoder when encoding an explicitly tagged
component of a Sequence

Revision 0.3.5, released 16-09-2017
-----------------------------------
Expand Down
18 changes: 15 additions & 3 deletions pyasn1/codec/ber/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
class AbstractItemEncoder(object):
supportIndefLenMode = 1

# An outcome of otherwise legit call `encodeFun(eoo.endOfOctets)`
eooIntegerSubstrate = (0, 0)
eooOctetsSubstrate = ints2octs(eooIntegerSubstrate)

# noinspection PyMethodMayBeStatic
def encodeTag(self, singleTag, isConstructed):
tagClass, tagFormat, tagId = singleTag
Expand Down Expand Up @@ -85,11 +89,18 @@ def encode(self, value, encodeFun, **options):

if isOctets:
substrate = ints2octs(header) + substrate

if not defModeOverride:
substrate += self.eooOctetsSubstrate

else:
substrate = ints2octs(header + substrate)
substrate = header + substrate

if not defModeOverride:
substrate += encodeFun(eoo.endOfOctets, defMode=defModeOverride)
if not defModeOverride:
substrate += self.eooIntegerSubstrate

if not isOctets:
substrate = ints2octs(substrate)

return substrate

Expand Down Expand Up @@ -506,6 +517,7 @@ def __call__(self, value, **options):

if logger:
logger('codec %s built %s octets of substrate: %s\nencoder completed' % (concreteEncoder, len(substrate), debug.hexdump(substrate)))

return substrate

#: Turns ASN.1 object into BER octet stream.
Expand Down
20 changes: 20 additions & 0 deletions tests/codec/ber/test_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,26 @@ def testIndefMode(self):
) == ints2octs((101, 128, 48, 128, 2, 1, 12, 0, 0, 0, 0))


class ExpTaggedSequenceComponentEncoderTestCase(BaseTestCase):
def setUp(self):
BaseTestCase.setUp(self)
self.s = univ.Sequence(
componentType=namedtype.NamedTypes(
namedtype.NamedType('number', univ.Boolean().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0))),
)
)

self.s[0] = True

def testDefMode(self):
assert encoder.encode(self.s) == ints2octs((48, 5, 160, 3, 1, 1, 1))

def testIndefMode(self):
assert encoder.encode(
self.s, defMode=False
) == ints2octs((48, 128, 160, 3, 1, 1, 1, 0, 0, 0, 0))


class SetEncoderTestCase(BaseTestCase):
def setUp(self):
BaseTestCase.setUp(self)
Expand Down

0 comments on commit 8b62fa2

Please sign in to comment.