/**
  * @dataProvider getTags
  * @depends testGetBinary
  */
 public function testFromBinary($originalTag)
 {
     $originalStringObject = new PrintableString('test');
     $originalObject = new ExplicitlyTaggedObject($originalTag, $originalStringObject);
     $binaryData = $originalObject->getBinary();
     $parsedObject = ExplicitlyTaggedObject::fromBinary($binaryData);
     $this->assertEquals($originalObject, $parsedObject);
 }
示例#2
0
文件: Object.php 项目: afk11/phpasn1
 /**
  * @param string $binaryData
  * @param int $offsetIndex
  *
  * @throws ParserException
  *
  * @return \FG\ASN1\Object
  */
 public static function fromBinary(&$binaryData, &$offsetIndex = 0)
 {
     if (strlen($binaryData) <= $offsetIndex) {
         throw new ParserException('Can not parse binary from data: Offset index larger than input size', $offsetIndex);
     }
     $identifierOctet = ord($binaryData[$offsetIndex]);
     if (Identifier::isContextSpecificClass($identifierOctet) && Identifier::isConstructed($identifierOctet)) {
         return ExplicitlyTaggedObject::fromBinary($binaryData, $offsetIndex);
     }
     switch ($identifierOctet) {
         case Identifier::BITSTRING:
             return BitString::fromBinary($binaryData, $offsetIndex);
         case Identifier::BOOLEAN:
             return Boolean::fromBinary($binaryData, $offsetIndex);
         case Identifier::ENUMERATED:
             return Enumerated::fromBinary($binaryData, $offsetIndex);
         case Identifier::INTEGER:
             return Integer::fromBinary($binaryData, $offsetIndex);
         case Identifier::NULL:
             return NullObject::fromBinary($binaryData, $offsetIndex);
         case Identifier::OBJECT_IDENTIFIER:
             return ObjectIdentifier::fromBinary($binaryData, $offsetIndex);
         case Identifier::RELATIVE_OID:
             return RelativeObjectIdentifier::fromBinary($binaryData, $offsetIndex);
         case Identifier::OCTETSTRING:
             return OctetString::fromBinary($binaryData, $offsetIndex);
         case Identifier::SEQUENCE:
             return Sequence::fromBinary($binaryData, $offsetIndex);
         case Identifier::SET:
             return Set::fromBinary($binaryData, $offsetIndex);
         case Identifier::UTC_TIME:
             return UTCTime::fromBinary($binaryData, $offsetIndex);
         case Identifier::GENERALIZED_TIME:
             return GeneralizedTime::fromBinary($binaryData, $offsetIndex);
         case Identifier::IA5_STRING:
             return IA5String::fromBinary($binaryData, $offsetIndex);
         case Identifier::PRINTABLE_STRING:
             return PrintableString::fromBinary($binaryData, $offsetIndex);
         case Identifier::NUMERIC_STRING:
             return NumericString::fromBinary($binaryData, $offsetIndex);
         case Identifier::UTF8_STRING:
             return UTF8String::fromBinary($binaryData, $offsetIndex);
         case Identifier::UNIVERSAL_STRING:
             return UniversalString::fromBinary($binaryData, $offsetIndex);
         case Identifier::CHARACTER_STRING:
             return CharacterString::fromBinary($binaryData, $offsetIndex);
         case Identifier::GENERAL_STRING:
             return GeneralString::fromBinary($binaryData, $offsetIndex);
         case Identifier::VISIBLE_STRING:
             return VisibleString::fromBinary($binaryData, $offsetIndex);
         case Identifier::GRAPHIC_STRING:
             return GraphicString::fromBinary($binaryData, $offsetIndex);
         case Identifier::BMP_STRING:
             return BMPString::fromBinary($binaryData, $offsetIndex);
         case Identifier::T61_STRING:
             return T61String::fromBinary($binaryData, $offsetIndex);
         case Identifier::OBJECT_DESCRIPTOR:
             return ObjectDescriptor::fromBinary($binaryData, $offsetIndex);
         default:
             // At this point the identifier may be >1 byte.
             if (Identifier::isConstructed($identifierOctet)) {
                 return new UnknownConstructedObject($binaryData, $offsetIndex);
             } else {
                 $identifier = self::parseBinaryIdentifier($binaryData, $offsetIndex);
                 $lengthOfUnknownObject = self::parseContentLength($binaryData, $offsetIndex);
                 $offsetIndex += $lengthOfUnknownObject;
                 return new UnknownObject($identifier, $lengthOfUnknownObject);
             }
     }
 }
 /**
  * @expectedException \FG\ASN1\Exception\ParserException
  * @expectedExceptionMessage ASN.1 Parser Exception at offset 2: Context-Specific explicitly tagged object [1] starting at offset 2 is longer than allowed in the outer tag
  * @depends testFromBinary
  */
 public function testFromBinaryWithInvalidOuterLengthThrowsException2()
 {
     $data = hex2bin('a102040101');
     //                  ^- this is wrong. correct would be "3"
     ExplicitlyTaggedObject::fromBinary($data);
 }
示例#4
0
 /**
  * For the real parsing tests look in the test cases of each single ASn object.
  */
 public function testFromBinary()
 {
     /* @var BitString $parsedObject */
     $binaryData = chr(Identifier::BITSTRING);
     $binaryData .= chr(0x3);
     $binaryData .= chr(0x5);
     $binaryData .= chr(0xff);
     $binaryData .= chr(0xa0);
     $expectedObject = new BitString(0xffa0, 5);
     $parsedObject = Object::fromBinary($binaryData);
     $this->assertTrue($parsedObject instanceof BitString);
     $this->assertEquals($expectedObject->getContent(), $parsedObject->getContent());
     $this->assertEquals($expectedObject->getNumberOfUnusedBits(), $parsedObject->getNumberOfUnusedBits());
     /* @var OctetString $parsedObject */
     $binaryData = chr(Identifier::OCTETSTRING);
     $binaryData .= chr(0x2);
     $binaryData .= chr(0xff);
     $binaryData .= chr(0xa0);
     $expectedObject = new OctetString(0xffa0);
     $parsedObject = Object::fromBinary($binaryData);
     $this->assertTrue($parsedObject instanceof OctetString);
     $this->assertEquals($expectedObject->getContent(), $parsedObject->getContent());
     /* @var \FG\ASN1\Universal\Boolean $parsedObject */
     $binaryData = chr(Identifier::BOOLEAN);
     $binaryData .= chr(0x1);
     $binaryData .= chr(0xff);
     $expectedObject = new Boolean(true);
     $parsedObject = Object::fromBinary($binaryData);
     $this->assertTrue($parsedObject instanceof Boolean);
     $this->assertEquals($expectedObject->getContent(), $parsedObject->getContent());
     /* @var Enumerated $parsedObject */
     $binaryData = chr(Identifier::ENUMERATED);
     $binaryData .= chr(0x1);
     $binaryData .= chr(0x3);
     $expectedObject = new Enumerated(3);
     $parsedObject = Object::fromBinary($binaryData);
     $this->assertTrue($parsedObject instanceof Enumerated);
     $this->assertEquals($expectedObject->getContent(), $parsedObject->getContent());
     /* @var IA5String $parsedObject */
     $string = 'Hello Foo World!!!11EinsEins!1';
     $binaryData = chr(Identifier::IA5_STRING);
     $binaryData .= chr(strlen($string));
     $binaryData .= $string;
     $expectedObject = new IA5String($string);
     $parsedObject = Object::fromBinary($binaryData);
     $this->assertTrue($parsedObject instanceof IA5String);
     $this->assertEquals($expectedObject->getContent(), $parsedObject->getContent());
     /* @var \FG\ASN1\Universal\Integer $parsedObject */
     $binaryData = chr(Identifier::INTEGER);
     $binaryData .= chr(0x1);
     $binaryData .= chr(123);
     $expectedObject = new Integer(123);
     $parsedObject = Object::fromBinary($binaryData);
     $this->assertTrue($parsedObject instanceof Integer);
     $this->assertEquals($expectedObject->getContent(), $parsedObject->getContent());
     /* @var \FG\ASN1\Universal\NullObject $parsedObject */
     $binaryData = chr(Identifier::NULL);
     $binaryData .= chr(0x0);
     $expectedObject = new NullObject();
     $parsedObject = Object::fromBinary($binaryData);
     $this->assertTrue($parsedObject instanceof NullObject);
     $this->assertEquals($expectedObject->getContent(), $parsedObject->getContent());
     /* @var ObjectIdentifier $parsedObject */
     $binaryData = chr(Identifier::OBJECT_IDENTIFIER);
     $binaryData .= chr(0x2);
     $binaryData .= chr(1 * 40 + 2);
     $binaryData .= chr(3);
     $expectedObject = new ObjectIdentifier('1.2.3');
     $parsedObject = Object::fromBinary($binaryData);
     $this->assertTrue($parsedObject instanceof ObjectIdentifier);
     $this->assertEquals($expectedObject->getContent(), $parsedObject->getContent());
     /* @var PrintableString $parsedObject */
     $string = 'This is a test string. #?!%&""';
     $binaryData = chr(Identifier::PRINTABLE_STRING);
     $binaryData .= chr(strlen($string));
     $binaryData .= $string;
     $expectedObject = new PrintableString($string);
     $parsedObject = Object::fromBinary($binaryData);
     $this->assertTrue($parsedObject instanceof PrintableString);
     $this->assertEquals($expectedObject->getContent(), $parsedObject->getContent());
     /* @var GeneralizedTime $parsedObject */
     $binaryData = chr(Identifier::GENERALIZED_TIME);
     $binaryData .= chr(15);
     $binaryData .= '20120923202316Z';
     $expectedObject = new GeneralizedTime('2012-09-23 20:23:16', 'UTC');
     $parsedObject = Object::fromBinary($binaryData);
     $this->assertTrue($parsedObject instanceof GeneralizedTime);
     $this->assertEquals($expectedObject->getContent(), $parsedObject->getContent());
     /* @var Sequence $parsedObject */
     $binaryData = chr(Identifier::SEQUENCE);
     $binaryData .= chr(0x6);
     $binaryData .= chr(Identifier::BOOLEAN);
     $binaryData .= chr(0x1);
     $binaryData .= chr(0x0);
     $binaryData .= chr(Identifier::INTEGER);
     $binaryData .= chr(0x1);
     $binaryData .= chr(0x3);
     $expectedChild1 = new Boolean(false);
     $expectedChild2 = new Integer(0x3);
     $parsedObject = Object::fromBinary($binaryData);
     $this->assertTrue($parsedObject instanceof Sequence);
     $this->assertEquals(2, $parsedObject->getNumberOfChildren());
     $children = $parsedObject->getChildren();
     $child1 = $children[0];
     $child2 = $children[1];
     $this->assertEquals($expectedChild1->getContent(), $child1->getContent());
     $this->assertEquals($expectedChild2->getContent(), $child2->getContent());
     /* @var ExplicitlyTaggedObject $parsedObject */
     $taggedObject = new ExplicitlyTaggedObject(0x1, new PrintableString('Hello tagged world'));
     $binaryData = $taggedObject->getBinary();
     $parsedObject = Object::fromBinary($binaryData);
     $this->assertTrue($parsedObject instanceof ExplicitlyTaggedObject);
     // An unknown constructed object containing 2 integer children,
     // first 3 bytes are the identifier.
     $binaryData = "?�" . chr(Identifier::INTEGER) . "B" . chr(Identifier::INTEGER) . "i";
     $offsetIndex = 0;
     $parsedObject = OBject::fromBinary($binaryData, $offsetIndex);
     $this->assertTrue($parsedObject instanceof UnknownConstructedObject);
     $this->assertEquals(substr($binaryData, 0, 3), $parsedObject->getIdentifier());
     $this->assertCount(2, $parsedObject->getContent());
     $this->assertEquals(strlen($binaryData), $offsetIndex);
     $this->assertEquals(10, $parsedObject->getObjectLength());
     // First 3 bytes are the identifier
     $binaryData = "��";
     $offsetIndex = 0;
     $parsedObject = Object::fromBinary($binaryData, $offsetIndex);
     $this->assertTrue($parsedObject instanceof UnknownObject);
     $this->assertEquals(substr($binaryData, 0, 3), $parsedObject->getIdentifier());
     $this->assertEquals('Unparsable Object (1 bytes)', $parsedObject->getContent());
     $this->assertEquals(strlen($binaryData), $offsetIndex);
     $this->assertEquals(5, $parsedObject->getObjectLength());
 }
 public function testFromBinaryWithMultipleObjects()
 {
     $object1 = new Boolean(true);
     $object2 = new Integer(42);
     $identifier = 0xa0;
     $length = $object1->getObjectLength() + $object2->getObjectLength();
     $data = chr($identifier) . chr($length) . $object1->getBinary() . $object2->getBinary();
     $object = ExplicitlyTaggedObject::fromBinary($data);
     $this->assertEquals(2 + $length, $object->getObjectLength());
     $this->assertEquals([$object1, $object2], $object->getContent());
     $this->assertEquals($data, $object->getBinary());
     $this->assertEquals('2 context specific objects with tag [0]', $object->__toString());
 }