Example #1
0
 public static function fromBinary(&$binaryData, &$offsetIndex = 0)
 {
     self::parseIdentifier($binaryData[$offsetIndex], Identifier::OCTETSTRING, $offsetIndex++);
     $contentLength = self::parseContentLength($binaryData, $offsetIndex, 1);
     $value = substr($binaryData, $offsetIndex, $contentLength);
     $offsetIndex += $contentLength;
     $parsedObject = new self(bin2hex($value));
     $parsedObject->setContentLength($contentLength);
     return $parsedObject;
 }
Example #2
0
 public static function fromBinary(&$binaryData, &$offsetIndex = 0)
 {
     self::parseIdentifier($binaryData[$offsetIndex], Identifier::NULL, $offsetIndex++);
     $contentLength = self::parseContentLength($binaryData, $offsetIndex);
     if ($contentLength != 0) {
         throw new ParserException("An ASN.1 Null should not have a length other than zero. Extracted length was {$contentLength}", $offsetIndex);
     }
     $parsedObject = new self();
     $parsedObject->setContentLength(0);
     return $parsedObject;
 }
Example #3
0
 public static function fromBinary(&$binaryData, &$offsetIndex = 0)
 {
     self::parseIdentifier($binaryData[$offsetIndex], Identifier::OBJECT_IDENTIFIER, $offsetIndex++);
     $contentLength = self::parseContentLength($binaryData, $offsetIndex, 1);
     $firstOctet = ord($binaryData[$offsetIndex++]);
     $oidString = floor($firstOctet / 40) . '.' . $firstOctet % 40;
     $oidString .= '.' . self::parseOid($binaryData, $offsetIndex, $contentLength - 1);
     $parsedObject = new self($oidString);
     $parsedObject->setContentLength($contentLength);
     return $parsedObject;
 }
Example #4
0
 public static function fromBinary(&$binaryData, &$offsetIndex = 0)
 {
     self::parseIdentifier($binaryData[$offsetIndex], Identifier::BITSTRING, $offsetIndex++);
     $contentLength = self::parseContentLength($binaryData, $offsetIndex, 2);
     $nrOfUnusedBits = ord($binaryData[$offsetIndex]);
     $value = substr($binaryData, $offsetIndex + 1, $contentLength - 1);
     $offsetIndex += $contentLength;
     $parsedObject = new self(bin2hex($value), $nrOfUnusedBits);
     $parsedObject->setContentLength($contentLength);
     return $parsedObject;
 }
 public static function fromBinary(&$binaryData, &$offsetIndex = 0)
 {
     self::parseIdentifier($binaryData[$offsetIndex], Identifier::RELATIVE_OID, $offsetIndex++);
     $contentLength = self::parseContentLength($binaryData, $offsetIndex, 1);
     try {
         $oidString = self::parseOid($binaryData, $offsetIndex, $contentLength);
     } catch (ParserException $e) {
         throw new ParserException('Malformed ASN.1 Relative Object Identifier', $e->getOffset());
     }
     $parsedObject = new self($oidString);
     $parsedObject->setContentLength($contentLength);
     return $parsedObject;
 }
Example #6
0
 public static function fromBinary(&$binaryData, &$offsetIndex = 0)
 {
     self::parseIdentifier($binaryData[$offsetIndex], Identifier::BOOLEAN, $offsetIndex++);
     $contentLength = self::parseContentLength($binaryData, $offsetIndex);
     if ($contentLength != 1) {
         throw new ParserException("An ASN.1 Boolean should not have a length other than one. Extracted length was {$contentLength}", $offsetIndex);
     }
     $value = ord($binaryData[$offsetIndex++]);
     $booleanValue = $value == 0xff ? true : false;
     $parsedObject = new self($booleanValue);
     $parsedObject->setContentLength($contentLength);
     return $parsedObject;
 }
Example #7
0
 public static function fromBinary(&$binaryData, &$offsetIndex = 0)
 {
     self::parseIdentifier($binaryData[$offsetIndex], Identifier::UTC_TIME, $offsetIndex++);
     $contentLength = self::parseContentLength($binaryData, $offsetIndex, 11);
     $format = 'ymdGi';
     $dateTimeString = substr($binaryData, $offsetIndex, 10);
     $offsetIndex += 10;
     // extract optional seconds part
     if ($binaryData[$offsetIndex] != 'Z' && $binaryData[$offsetIndex] != '+' && $binaryData[$offsetIndex] != '-') {
         $dateTimeString .= substr($binaryData, $offsetIndex, 2);
         $offsetIndex += 2;
         $format .= 's';
     }
     $dateTime = \DateTime::createFromFormat($format, $dateTimeString, new \DateTimeZone('UTC'));
     // extract time zone settings
     if ($binaryData[$offsetIndex] == '+' || $binaryData[$offsetIndex] == '-') {
         $dateTime = static::extractTimeZoneData($binaryData, $offsetIndex, $dateTime);
     } elseif ($binaryData[$offsetIndex++] != 'Z') {
         throw new ParserException('Invalid UTC String', $offsetIndex);
     }
     $parsedObject = new self($dateTime);
     $parsedObject->setContentLength($contentLength);
     return $parsedObject;
 }
Example #8
0
 public static function fromBinary(&$binaryData, &$offsetIndex = 0)
 {
     self::parseIdentifier($binaryData[$offsetIndex], 0xa0, $offsetIndex++);
     $contentLength = self::parseContentLength($binaryData, $offsetIndex);
     $octetsToRead = $contentLength;
     $parsedObject = new self();
     while ($octetsToRead > 0) {
         $initialOffset = $offsetIndex;
         // used to calculate how much bits have been read
         self::parseIdentifier($binaryData[$offsetIndex], Identifier::SEQUENCE, $offsetIndex++);
         self::parseContentLength($binaryData, $offsetIndex);
         $objectIdentifier = ObjectIdentifier::fromBinary($binaryData, $offsetIndex);
         $oidString = $objectIdentifier->getContent();
         if ($oidString == OID::PKCS9_EXTENSION_REQUEST) {
             $attribute = CertificateExtensions::fromBinary($binaryData, $offsetIndex);
         } else {
             $attribute = Object::fromBinary($binaryData, $offsetIndex);
         }
         $parsedObject->addAttribute($objectIdentifier, $attribute);
         $octetsToRead -= $offsetIndex - $initialOffset;
     }
     $parsedObject->setContentLength($contentLength);
     return $parsedObject;
 }
Example #9
0
 public static function fromBinary(&$binaryData, &$offsetIndex = 0)
 {
     $identifier = self::parseBinaryIdentifier($binaryData, $offsetIndex);
     $firstIdentifierOctet = ord($identifier);
     assert(Identifier::isContextSpecificClass($firstIdentifierOctet), 'identifier octet should indicate context specific class');
     assert(Identifier::isConstructed($firstIdentifierOctet), 'identifier octet should indicate constructed object');
     $tag = Identifier::getTagNumber($identifier);
     $totalContentLength = self::parseContentLength($binaryData, $offsetIndex);
     $remainingContentLength = $totalContentLength;
     $offsetIndexOfDecoratedObject = $offsetIndex;
     $decoratedObjects = [];
     while ($remainingContentLength > 0) {
         $nextObject = Object::fromBinary($binaryData, $offsetIndex);
         $remainingContentLength -= $nextObject->getObjectLength();
         $decoratedObjects[] = $nextObject;
     }
     if ($remainingContentLength != 0) {
         throw new ParserException("Context-Specific explicitly tagged object [{$tag}] starting at offset {$offsetIndexOfDecoratedObject} specifies a length of {$totalContentLength} octets but {$remainingContentLength} remain after parsing the content", $offsetIndexOfDecoratedObject);
     }
     $parsedObject = new self($tag, ...$decoratedObjects);
     $parsedObject->setContentLength($totalContentLength);
     return $parsedObject;
 }
 public static function fromBinary(&$binaryData, &$offsetIndex = 0)
 {
     $identifier = self::parseBinaryIdentifier($binaryData, $offsetIndex);
     $firstIdentifierOctet = ord($identifier);
     assert(Identifier::isContextSpecificClass($firstIdentifierOctet));
     assert(Identifier::isConstructed($firstIdentifierOctet));
     $tag = Identifier::getTagNumber($identifier);
     $contentLength = self::parseContentLength($binaryData, $offsetIndex);
     $offsetIndexOfDecoratedObject = $offsetIndex;
     $decoratedObject = Object::fromBinary($binaryData, $offsetIndex);
     $decoratedObjectLength = $decoratedObject->getObjectLength();
     if ($decoratedObjectLength != $contentLength) {
         throw new ParserException("Context-Specific explicitly tagged object [{$tag}] starting at offset {$offsetIndexOfDecoratedObject} is " . ($decoratedObjectLength < $contentLength ? 'shorter than required' : 'longer than allowed') . ' in the outer tag', $offsetIndexOfDecoratedObject);
     }
     $parsedObject = new self($tag, $decoratedObject);
     $parsedObject->setContentLength($contentLength);
     return $parsedObject;
 }
Example #11
0
 public static function fromBinary(&$binaryData, &$offsetIndex = 0)
 {
     self::parseIdentifier($binaryData[$offsetIndex], Identifier::GENERALIZED_TIME, $offsetIndex++);
     $lengthOfMinimumTimeString = 14;
     // YYYYMMDDHHmmSS
     $contentLength = self::parseContentLength($binaryData, $offsetIndex, $lengthOfMinimumTimeString);
     $maximumBytesToRead = $contentLength;
     $format = 'YmdGis';
     $content = substr($binaryData, $offsetIndex, $contentLength);
     $dateTimeString = substr($content, 0, $lengthOfMinimumTimeString);
     $offsetIndex += $lengthOfMinimumTimeString;
     $maximumBytesToRead -= $lengthOfMinimumTimeString;
     if ($contentLength == $lengthOfMinimumTimeString) {
         $localTimeZone = new \DateTimeZone(date_default_timezone_get());
         $dateTime = \DateTime::createFromFormat($format, $dateTimeString, $localTimeZone);
     } else {
         if ($binaryData[$offsetIndex] == '.') {
             $maximumBytesToRead--;
             // account for the '.'
             $nrOfFractionalSecondElements = 1;
             // account for the '.'
             while ($maximumBytesToRead > 0 && $binaryData[$offsetIndex + $nrOfFractionalSecondElements] != '+' && $binaryData[$offsetIndex + $nrOfFractionalSecondElements] != '-' && $binaryData[$offsetIndex + $nrOfFractionalSecondElements] != 'Z') {
                 $nrOfFractionalSecondElements++;
                 $maximumBytesToRead--;
             }
             $dateTimeString .= substr($binaryData, $offsetIndex, $nrOfFractionalSecondElements);
             $offsetIndex += $nrOfFractionalSecondElements;
             $format .= '.u';
         }
         $dateTime = \DateTime::createFromFormat($format, $dateTimeString, new \DateTimeZone('UTC'));
         if ($maximumBytesToRead > 0) {
             if ($binaryData[$offsetIndex] == '+' || $binaryData[$offsetIndex] == '-') {
                 $dateTime = static::extractTimeZoneData($binaryData, $offsetIndex, $dateTime);
             } elseif ($binaryData[$offsetIndex++] != 'Z') {
                 throw new ParserException('Invalid ISO 8601 Time String', $offsetIndex);
             }
         }
     }
     $parsedObject = new self($dateTime);
     $parsedObject->setContentLength($contentLength);
     return $parsedObject;
 }