Skip to content

Commit

Permalink
Merge pull request #8 from bashkarev/fix-eoc
Browse files Browse the repository at this point in the history
getDecoratedObject does not enforce proper use of end-of-content (EOC) markers
  • Loading branch information
adapik authored Mar 22, 2024
2 parents b0ea781 + 3629747 commit aa15fb8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/ASN1/AbstractTaggedObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public function getDecoratedObject($tagNumber, $tagClass = Identifier::CLASS_UNI
$identifierOctets = IdentifierManager::create($tagClass, $isConstructed, $tagNumber);

$binary = $identifierOctets.$this->getContentLength()->getBinary().$this->getContent()->getBinary();
if ($this->eoc) {
$binary .= $this->eoc->getBinary();
}

return ASN1Object::fromBinary($binary);
}
Expand Down
20 changes: 20 additions & 0 deletions tests/ASN1/ExplicitlyTaggedObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,24 @@ public function testFromBinaryWithMultipleObjects()
$this->assertEquals(2+$length, $object->getObjectLength());
$this->assertEquals($data, $object->getBinary());
}

public function testDecorated()
{
$object1 = Boolean::create(true);
$object2 = Integer::create(42);
$identifier = 0xA0;

// SHORT_FORM
$length = $object1->getObjectLength() + $object2->getObjectLength();
$data = chr($identifier) . chr($length) . $object1->getBinary() . $object2->getBinary();
$object = ExplicitlyTaggedObject::fromBinary($data);
$decoratedData = $object->getDecoratedObject(0, Identifier::CLASS_CONTEXT_SPECIFIC, true)->getBinary();
$this->assertEquals($data, $decoratedData);

// INDEFINITE_FORM
$dataEoc = chr($identifier) . chr(128) . $object1->getBinary() . $object2->getBinary() . chr(0) . chr(0);
$object = ExplicitlyTaggedObject::fromBinary($dataEoc);
$decoratedData = $object->getDecoratedObject(0, Identifier::CLASS_CONTEXT_SPECIFIC, true)->getBinary();
$this->assertEquals($dataEoc, $decoratedData);
}
}

0 comments on commit aa15fb8

Please sign in to comment.