Beispiel #1
0
 /**
  * @depends testFromBinary
  */
 public function testFromBinaryWithOffset()
 {
     $originalObject1 = new UTF8String('Hello ');
     $originalObject2 = new UTF8String(' World');
     $binaryData = $originalObject1->getBinary();
     $binaryData .= $originalObject2->getBinary();
     $offset = 0;
     $parsedObject = UTF8String::fromBinary($binaryData, $offset);
     $this->assertEquals($originalObject1, $parsedObject);
     $this->assertEquals(8, $offset);
     $parsedObject = UTF8String::fromBinary($binaryData, $offset);
     $this->assertEquals($originalObject2, $parsedObject);
     $this->assertEquals(16, $offset);
 }
Beispiel #2
0
 public static function createObject($tagClass, $tagName, $isConstructed, $value, $lengthForm, $children = [])
 {
     $identifier = self::createIdentifier($tagClass, $tagName, $isConstructed);
     if ($identifier->tagClass === Identifier::CLASS_UNIVERSAL && $identifier->isConstructed === false) {
         //для простых элементов вызываем конструктор
         switch ($identifier->getTagNumber()) {
             case Identifier::BITSTRING:
                 $value = Universal\BitString::encodeValue($value);
                 break;
             case Identifier::BOOLEAN:
                 $value = Universal\Boolean::encodeValue($value);
                 break;
             case Identifier::ENUMERATED:
                 $value = Universal\Enumerated::encodeValue($value);
                 break;
             case Identifier::INTEGER:
                 $value = Universal\Integer::encodeValue($value);
                 break;
             case Identifier::NULL:
                 $value = Universal\NullObject::encodeValue($value);
                 break;
             case Identifier::OBJECT_IDENTIFIER:
                 $value = Universal\ObjectIdentifier::encodeValue($value);
                 break;
             case Identifier::OCTETSTRING:
                 $value = Universal\OctetString::encodeValue($value);
                 break;
             case Identifier::UTC_TIME:
                 $value = Universal\UTCTime::encodeValue($value);
                 break;
             case Identifier::GENERALIZED_TIME:
                 $value = Universal\GeneralizedTime::encodeValue($value);
                 break;
             case Identifier::IA5_STRING:
                 $value = Universal\IA5String::encodeValue($value);
                 break;
             case Identifier::PRINTABLE_STRING:
                 $value = Universal\PrintableString::encodeValue($value);
                 break;
             case Identifier::NUMERIC_STRING:
                 $value = Universal\NumericString::encodeValue($value);
                 break;
             case Identifier::UTF8_STRING:
                 $value = Universal\UTF8String::encodeValue($value);
                 break;
             case Identifier::UNIVERSAL_STRING:
                 $value = Universal\UniversalString::encodeValue($value);
                 break;
             case Identifier::CHARACTER_STRING:
                 $value = Universal\CharacterString::encodeValue($value);
                 break;
             case Identifier::GENERAL_STRING:
                 $value = Universal\GeneralString::encodeValue($value);
                 break;
             case Identifier::VISIBLE_STRING:
                 $value = Universal\VisibleString::encodeValue($value);
                 break;
             case Identifier::GRAPHIC_STRING:
                 $value = Universal\GraphicString::encodeValue($value);
                 break;
             case Identifier::BMP_STRING:
                 $value = Universal\BMPString::encodeValue($value);
                 break;
             case Identifier::T61_STRING:
                 $value = Universal\T61String::encodeValue($value);
                 break;
             case Identifier::OBJECT_DESCRIPTOR:
                 $value = Universal\ObjectDescriptor::encodeValue($value);
                 break;
             default:
                 // At this point the identifier may be >1 byte.
                 if ($identifier->isConstructed) {
                     $value = UnknownConstructedObject::encodeValue($value);
                 } else {
                     $value = UnknownObject::encodeValue($value);
                 }
         }
     }
     if ($children) {
         $contentOctets = '';
         foreach ($children as $child) {
             $contentOctets .= $child->getBinary();
         }
     } else {
         $contentOctets = $value;
     }
     $contentLength = self::createContentLength($contentOctets, $lengthForm);
     $content = new Content($contentOctets);
     if ($identifier->tagClass === Identifier::CLASS_UNIVERSAL) {
         //для простых элементов вызываем конструктор
         switch ($identifier->getTagNumber()) {
             case Identifier::BITSTRING:
                 return new Universal\BitString($identifier, $contentLength, $content, $children);
             case Identifier::BOOLEAN:
                 return new Universal\Boolean($identifier, $contentLength, $content, $children);
             case Identifier::ENUMERATED:
                 return new Universal\Enumerated($identifier, $contentLength, $content, $children);
             case Identifier::INTEGER:
                 return new Universal\Integer($identifier, $contentLength, $content, $children);
             case Identifier::NULL:
                 return new Universal\NullObject($identifier, $contentLength, $content, $children);
             case Identifier::OBJECT_IDENTIFIER:
                 return new Universal\ObjectIdentifier($identifier, $contentLength, $content, $children);
             case Identifier::RELATIVE_OID:
                 return new Universal\RelativeObjectIdentifier($identifier, $contentLength, $content, $children);
             case Identifier::OCTETSTRING:
                 return new Universal\OctetString($identifier, $contentLength, $content, $children);
             case Identifier::SEQUENCE:
                 return new Universal\Sequence($identifier, $contentLength, $content, $children);
             case Identifier::SET:
                 return new Universal\Set($identifier, $contentLength, $content, $children);
             case Identifier::UTC_TIME:
                 return new Universal\UTCTime($identifier, $contentLength, $content, $children);
             case Identifier::GENERALIZED_TIME:
                 return new Universal\GeneralizedTime($identifier, $contentLength, $content, $children);
             case Identifier::IA5_STRING:
                 return new Universal\IA5String($identifier, $contentLength, $content, $children);
             case Identifier::PRINTABLE_STRING:
                 return new Universal\PrintableString($identifier, $contentLength, $content, $children);
             case Identifier::NUMERIC_STRING:
                 return new Universal\NumericString($identifier, $contentLength, $content, $children);
             case Identifier::UTF8_STRING:
                 return new Universal\UTF8String($identifier, $contentLength, $content, $children);
             case Identifier::UNIVERSAL_STRING:
                 return new Universal\UniversalString($identifier, $contentLength, $content, $children);
             case Identifier::CHARACTER_STRING:
                 return new Universal\CharacterString($identifier, $contentLength, $content, $children);
             case Identifier::GENERAL_STRING:
                 return new Universal\GeneralString($identifier, $contentLength, $content, $children);
             case Identifier::VISIBLE_STRING:
                 return new Universal\VisibleString($identifier, $contentLength, $content, $children);
             case Identifier::GRAPHIC_STRING:
                 return new Universal\GraphicString($identifier, $contentLength, $content, $children);
             case Identifier::BMP_STRING:
                 return new Universal\BMPString($identifier, $contentLength, $content, $children);
             case Identifier::T61_STRING:
                 return new Universal\T61String($identifier, $contentLength, $content, $children);
             case Identifier::OBJECT_DESCRIPTOR:
                 return new Universal\ObjectDescriptor($identifier, $contentLength, $content, $children);
             default:
                 // At this point the identifier may be >1 byte.
                 if ($identifier->isConstructed) {
                     return new UnknownConstructedObject($identifier, $contentLength, $content, $children);
                 } else {
                     return new UnknownObject($identifier, $contentLength, $content, $children);
                 }
         }
     }
     if ($identifier->tagClass === Identifier::CLASS_CONTEXT_SPECIFIC) {
         if ($identifier->isConstructed) {
             return new ExplicitlyTaggedObject($identifier, $contentLength, $content, $children);
         } else {
             return new ImplicitlyTaggedObject($identifier, $contentLength, $content, $children);
         }
     }
 }
Beispiel #3
0
 /**
  * @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);
             }
     }
 }