Skip to content
This repository has been archived by the owner on Apr 23, 2023. It is now read-only.

Commit

Permalink
Merge branch 'bug/empty-tagged-object'
Browse files Browse the repository at this point in the history
  • Loading branch information
fgrosse committed Sep 29, 2015
2 parents 1e6560d + 7eb91f7 commit 8ffc4c4
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 17 deletions.
4 changes: 2 additions & 2 deletions examples/Issue14.php
Expand Up @@ -106,7 +106,7 @@ function printObject(Object $object, $depth = 0)
$certExtensions = $certInfoFields[7];

// check if this is really the certificate extensions sequence
/* @var Object $certExtensions */
/* @var \FG\ASN1\Object $certExtensions */
$certExtensionsType = $certExtensions->getType();
assert(Identifier::isContextSpecificClass($certExtensionsType));
assert(Identifier::getTagNumber($certExtensions->getType()) == 3);
Expand All @@ -116,7 +116,7 @@ function printObject(Object $object, $depth = 0)
assert($certExtensions->getType() == Identifier::SEQUENCE);

// now check all extensions and search for the SAN
/** @var Object $extensionSequence */
/** @var \FG\ASN1\Object $extensionSequence */
foreach ($certExtensions as $extensionSequence) {
assert($extensionSequence->getType() == Identifier::SEQUENCE);
assert($extensionSequence->getNumberofChildren() >= 2);
Expand Down
2 changes: 1 addition & 1 deletion lib/ASN1/Composite/RelativeDistinguishedName.php
Expand Up @@ -29,7 +29,7 @@ public function __construct($objIdentifierString, Object $value)

public function getContent()
{
/** @var Object $firstObject */
/** @var \FG\ASN1\Object $firstObject */
$firstObject = $this->children[0];
return $firstObject->__toString();
}
Expand Down
8 changes: 4 additions & 4 deletions lib/ASN1/Construct.php
Expand Up @@ -17,12 +17,12 @@

abstract class Construct extends Object implements Countable, ArrayAccess, Iterator, Parsable
{
/** @var Object[] */
/** @var \FG\ASN1\Object[] */
protected $children;
private $iteratorPosition;

/**
* @param Object[] $children the variadic type hint is commented due to https://github.com/facebook/hhvm/issues/4858
* @param \FG\ASN1\Object[] $children the variadic type hint is commented due to https://github.com/facebook/hhvm/issues/4858
*/
public function __construct(/* HH_FIXME[4858]: variadic + strict */ ...$children)
{
Expand Down Expand Up @@ -130,15 +130,15 @@ public function getNumberOfChildren()
}

/**
* @return Object[]
* @return \FG\ASN1\Object[]
*/
public function getChildren()
{
return $this->children;
}

/**
* @return Object
* @return \FG\ASN1\Object
*/
public function getFirstChild()
{
Expand Down
28 changes: 22 additions & 6 deletions lib/ASN1/ExplicitlyTaggedObject.php
Expand Up @@ -36,22 +36,30 @@ class ExplicitlyTaggedObject extends Object

/**
* @param int $tag
* @param \FG\ASN1\Object $object
* @param \FG\ASN1\Object|null $object
*/
public function __construct($tag, Object $object)
public function __construct($tag, Object $object = null)
{
$this->tag = $tag;
$this->decoratedObject = $object;
}

protected function calculateContentLength()
{
return $this->decoratedObject->getObjectLength();
if (isset($this->decoratedObject)) {
return $this->decoratedObject->getObjectLength();
} else {
return 0;
}
}

protected function getEncodedValue()
{
return $this->decoratedObject->getBinary();
if (isset($this->decoratedObject)) {
return $this->decoratedObject->getBinary();
} else {
return '';
}
}

public function getContent()
Expand All @@ -61,8 +69,12 @@ public function getContent()

public function __toString()
{
$decoratedType = Identifier::getShortName($this->decoratedObject->getType());
return "Context specific $decoratedType with tag [{$this->tag}]";
if (isset($this->decoratedObject)) {
$decoratedType = Identifier::getShortName($this->decoratedObject->getType());
return "Context specific $decoratedType with tag [{$this->tag}]";
} else {
return "Context specific empty object with tag [{$this->tag}]";
}
}

public function getType()
Expand Down Expand Up @@ -91,6 +103,10 @@ public static function fromBinary(&$binaryData, &$offsetIndex = 0)
$tag = Identifier::getTagNumber($identifier);

$contentLength = self::parseContentLength($binaryData, $offsetIndex);
if ($contentLength == 0) {
return new self($tag);
}

$offsetIndexOfDecoratedObject = $offsetIndex;
$decoratedObject = Object::fromBinary($binaryData, $offsetIndex);
$decoratedObjectLength = $decoratedObject->getObjectLength();
Expand Down
4 changes: 2 additions & 2 deletions lib/ASN1/TemplateParser.php
Expand Up @@ -19,7 +19,7 @@ class TemplateParser
/**
* @param string $data
* @param array $template
* @return Object|Sequence
* @return \FG\ASN1\Object|Sequence
* @throws ParserException if there was an issue parsing
*/
public function parseBase64($data, array $template)
Expand All @@ -31,7 +31,7 @@ public function parseBase64($data, array $template)
/**
* @param string $binary
* @param array $template
* @return Object|Sequence
* @return \FG\ASN1\Object|Sequence
* @throws ParserException if there was an issue parsing
*/
public function parseBinary($binary, array $template)
Expand Down
2 changes: 1 addition & 1 deletion lib/X509/CertificateExtensions.php
Expand Up @@ -73,7 +73,7 @@ public static function fromBinary(&$binaryData, &$offsetIndex = 0)
if (count($children) < 2) {
throw new ParserException('Could not parse Certificate Extensions: Needs at least two child elements per extension sequence (object identifier and octet string)', $tmpOffset);
}
/** @var Object $objectIdentifier */
/** @var \FG\ASN1\Object $objectIdentifier */
$objectIdentifier = $children[0];

/** @var OctetString $octetString */
Expand Down
2 changes: 1 addition & 1 deletion lib/X509/SAN/SubjectAlternativeNames.php
Expand Up @@ -77,7 +77,7 @@ public static function fromBinary(&$binaryData, &$offsetIndex = 0)
}

$parsedObject = new self();
/** @var Object $object */
/** @var \FG\ASN1\Object $object */
foreach ($sequence as $object) {
if ($object->getType() == DNSName::IDENTIFIER) {
$domainName = DNSName::fromBinary($binaryData, $offsetOfSequence);
Expand Down
10 changes: 10 additions & 0 deletions tests/ASN1/ExplicitlyTaggedObjectTest.php
Expand Up @@ -111,4 +111,14 @@ public function testFromBinaryWithInvalidOuterLengthThrowsException2()
// ^- this is wrong. correct would be "3"
ExplicitlyTaggedObject::fromBinary($data);
}

public function testFromBinaryWithZeroContent()
{
$data = hex2bin('A000');
$object = ExplicitlyTaggedObject::fromBinary($data);
$this->assertEquals(2, $object->getObjectLength());
$this->assertNull($object->getContent());
$this->assertEquals('Context specific empty object with tag [0]', $object->__toString());
$this->assertEquals($data, $object->getBinary());
}
}

0 comments on commit 8ffc4c4

Please sign in to comment.