@@ -9,13 +9,13 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | trait PrimitiveType |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * |
|
| 14 | - * @see \ASN1\Element::isConstructed() |
|
| 15 | - * @return boolean |
|
| 16 | - */ |
|
| 17 | - public function isConstructed(): bool |
|
| 18 | - { |
|
| 19 | - return false; |
|
| 20 | - } |
|
| 12 | + /** |
|
| 13 | + * |
|
| 14 | + * @see \ASN1\Element::isConstructed() |
|
| 15 | + * @return boolean |
|
| 16 | + */ |
|
| 17 | + public function isConstructed(): bool |
|
| 18 | + { |
|
| 19 | + return false; |
|
| 20 | + } |
|
| 21 | 21 | } |
@@ -12,54 +12,54 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class Set extends Structure |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * Constructor |
|
| 17 | - * |
|
| 18 | - * @param Element[] $elements Any number of elements |
|
| 19 | - */ |
|
| 20 | - public function __construct(Element ...$elements) |
|
| 21 | - { |
|
| 22 | - $this->_typeTag = self::TYPE_SET; |
|
| 23 | - parent::__construct(...$elements); |
|
| 24 | - } |
|
| 15 | + /** |
|
| 16 | + * Constructor |
|
| 17 | + * |
|
| 18 | + * @param Element[] $elements Any number of elements |
|
| 19 | + */ |
|
| 20 | + public function __construct(Element ...$elements) |
|
| 21 | + { |
|
| 22 | + $this->_typeTag = self::TYPE_SET; |
|
| 23 | + parent::__construct(...$elements); |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Sort by canonical ascending order. |
|
| 28 | - * Used for DER encoding of SET type. |
|
| 29 | - * |
|
| 30 | - * @return self |
|
| 31 | - */ |
|
| 32 | - public function sortedSet(): self |
|
| 33 | - { |
|
| 34 | - $obj = clone $this; |
|
| 35 | - usort($obj->_elements, |
|
| 36 | - function (Element $a, Element $b) { |
|
| 37 | - if ($a->typeClass() != $b->typeClass()) { |
|
| 38 | - return $a->typeClass() < $b->typeClass() ? -1 : 1; |
|
| 39 | - } |
|
| 40 | - if ($a->tag() == $b->tag()) { |
|
| 41 | - return 0; |
|
| 42 | - } |
|
| 43 | - return $a->tag() < $b->tag() ? -1 : 1; |
|
| 44 | - }); |
|
| 45 | - return $obj; |
|
| 46 | - } |
|
| 26 | + /** |
|
| 27 | + * Sort by canonical ascending order. |
|
| 28 | + * Used for DER encoding of SET type. |
|
| 29 | + * |
|
| 30 | + * @return self |
|
| 31 | + */ |
|
| 32 | + public function sortedSet(): self |
|
| 33 | + { |
|
| 34 | + $obj = clone $this; |
|
| 35 | + usort($obj->_elements, |
|
| 36 | + function (Element $a, Element $b) { |
|
| 37 | + if ($a->typeClass() != $b->typeClass()) { |
|
| 38 | + return $a->typeClass() < $b->typeClass() ? -1 : 1; |
|
| 39 | + } |
|
| 40 | + if ($a->tag() == $b->tag()) { |
|
| 41 | + return 0; |
|
| 42 | + } |
|
| 43 | + return $a->tag() < $b->tag() ? -1 : 1; |
|
| 44 | + }); |
|
| 45 | + return $obj; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Sort by encoding ascending order. |
|
| 50 | - * Used for DER encoding of SET OF type. |
|
| 51 | - * |
|
| 52 | - * @return self |
|
| 53 | - */ |
|
| 54 | - public function sortedSetOf(): self |
|
| 55 | - { |
|
| 56 | - $obj = clone $this; |
|
| 57 | - usort($obj->_elements, |
|
| 58 | - function (Element $a, Element $b) { |
|
| 59 | - $a_der = $a->toDER(); |
|
| 60 | - $b_der = $b->toDER(); |
|
| 61 | - return strcmp($a_der, $b_der); |
|
| 62 | - }); |
|
| 63 | - return $obj; |
|
| 64 | - } |
|
| 48 | + /** |
|
| 49 | + * Sort by encoding ascending order. |
|
| 50 | + * Used for DER encoding of SET OF type. |
|
| 51 | + * |
|
| 52 | + * @return self |
|
| 53 | + */ |
|
| 54 | + public function sortedSetOf(): self |
|
| 55 | + { |
|
| 56 | + $obj = clone $this; |
|
| 57 | + usort($obj->_elements, |
|
| 58 | + function (Element $a, Element $b) { |
|
| 59 | + $a_der = $a->toDER(); |
|
| 60 | + $b_der = $b->toDER(); |
|
| 61 | + return strcmp($a_der, $b_der); |
|
| 62 | + }); |
|
| 63 | + return $obj; |
|
| 64 | + } |
|
| 65 | 65 | } |
@@ -17,41 +17,41 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class NullType extends Element |
| 19 | 19 | { |
| 20 | - use UniversalClass; |
|
| 21 | - use PrimitiveType; |
|
| 20 | + use UniversalClass; |
|
| 21 | + use PrimitiveType; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Constructor. |
|
| 25 | - */ |
|
| 26 | - public function __construct() |
|
| 27 | - { |
|
| 28 | - $this->_typeTag = self::TYPE_NULL; |
|
| 29 | - } |
|
| 23 | + /** |
|
| 24 | + * Constructor. |
|
| 25 | + */ |
|
| 26 | + public function __construct() |
|
| 27 | + { |
|
| 28 | + $this->_typeTag = self::TYPE_NULL; |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * |
|
| 33 | - * {@inheritdoc} |
|
| 34 | - */ |
|
| 35 | - protected function _encodedContentDER(): string |
|
| 36 | - { |
|
| 37 | - return ""; |
|
| 38 | - } |
|
| 31 | + /** |
|
| 32 | + * |
|
| 33 | + * {@inheritdoc} |
|
| 34 | + */ |
|
| 35 | + protected function _encodedContentDER(): string |
|
| 36 | + { |
|
| 37 | + return ""; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * |
|
| 42 | - * {@inheritdoc} |
|
| 43 | - * @return self |
|
| 44 | - */ |
|
| 45 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 46 | - int &$offset): ElementBase |
|
| 47 | - { |
|
| 48 | - $idx = $offset; |
|
| 49 | - if (!$identifier->isPrimitive()) { |
|
| 50 | - throw new DecodeException("Null value must be primitive."); |
|
| 51 | - } |
|
| 52 | - // null type has always zero length |
|
| 53 | - Length::expectFromDER($data, $idx, 0); |
|
| 54 | - $offset = $idx; |
|
| 55 | - return new self(); |
|
| 56 | - } |
|
| 40 | + /** |
|
| 41 | + * |
|
| 42 | + * {@inheritdoc} |
|
| 43 | + * @return self |
|
| 44 | + */ |
|
| 45 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 46 | + int &$offset): ElementBase |
|
| 47 | + { |
|
| 48 | + $idx = $offset; |
|
| 49 | + if (!$identifier->isPrimitive()) { |
|
| 50 | + throw new DecodeException("Null value must be primitive."); |
|
| 51 | + } |
|
| 52 | + // null type has always zero length |
|
| 53 | + Length::expectFromDER($data, $idx, 0); |
|
| 54 | + $offset = $idx; |
|
| 55 | + return new self(); |
|
| 56 | + } |
|
| 57 | 57 | } |
@@ -17,64 +17,64 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class Boolean extends Element |
| 19 | 19 | { |
| 20 | - use UniversalClass; |
|
| 21 | - use PrimitiveType; |
|
| 20 | + use UniversalClass; |
|
| 21 | + use PrimitiveType; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Value. |
|
| 25 | - * |
|
| 26 | - * @var bool |
|
| 27 | - */ |
|
| 28 | - private $_bool; |
|
| 23 | + /** |
|
| 24 | + * Value. |
|
| 25 | + * |
|
| 26 | + * @var bool |
|
| 27 | + */ |
|
| 28 | + private $_bool; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Constructor. |
|
| 32 | - * |
|
| 33 | - * @param bool $bool |
|
| 34 | - */ |
|
| 35 | - public function __construct(bool $bool) |
|
| 36 | - { |
|
| 37 | - $this->_typeTag = self::TYPE_BOOLEAN; |
|
| 38 | - $this->_bool = $bool; |
|
| 39 | - } |
|
| 30 | + /** |
|
| 31 | + * Constructor. |
|
| 32 | + * |
|
| 33 | + * @param bool $bool |
|
| 34 | + */ |
|
| 35 | + public function __construct(bool $bool) |
|
| 36 | + { |
|
| 37 | + $this->_typeTag = self::TYPE_BOOLEAN; |
|
| 38 | + $this->_bool = $bool; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Get the value. |
|
| 43 | - * |
|
| 44 | - * @return bool |
|
| 45 | - */ |
|
| 46 | - public function value(): bool |
|
| 47 | - { |
|
| 48 | - return $this->_bool; |
|
| 49 | - } |
|
| 41 | + /** |
|
| 42 | + * Get the value. |
|
| 43 | + * |
|
| 44 | + * @return bool |
|
| 45 | + */ |
|
| 46 | + public function value(): bool |
|
| 47 | + { |
|
| 48 | + return $this->_bool; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * |
|
| 53 | - * {@inheritdoc} |
|
| 54 | - */ |
|
| 55 | - protected function _encodedContentDER(): string |
|
| 56 | - { |
|
| 57 | - return $this->_bool ? chr(0xff) : chr(0); |
|
| 58 | - } |
|
| 51 | + /** |
|
| 52 | + * |
|
| 53 | + * {@inheritdoc} |
|
| 54 | + */ |
|
| 55 | + protected function _encodedContentDER(): string |
|
| 56 | + { |
|
| 57 | + return $this->_bool ? chr(0xff) : chr(0); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * |
|
| 62 | - * {@inheritdoc} |
|
| 63 | - * @return self |
|
| 64 | - */ |
|
| 65 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 66 | - int &$offset): ElementBase |
|
| 67 | - { |
|
| 68 | - $idx = $offset; |
|
| 69 | - Length::expectFromDER($data, $idx, 1); |
|
| 70 | - $byte = ord($data[$idx++]); |
|
| 71 | - if ($byte !== 0) { |
|
| 72 | - if ($byte != 0xff) { |
|
| 73 | - throw new DecodeException( |
|
| 74 | - "DER encoded boolean true must have all bits set to 1."); |
|
| 75 | - } |
|
| 76 | - } |
|
| 77 | - $offset = $idx; |
|
| 78 | - return new self($byte !== 0); |
|
| 79 | - } |
|
| 60 | + /** |
|
| 61 | + * |
|
| 62 | + * {@inheritdoc} |
|
| 63 | + * @return self |
|
| 64 | + */ |
|
| 65 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 66 | + int &$offset): ElementBase |
|
| 67 | + { |
|
| 68 | + $idx = $offset; |
|
| 69 | + Length::expectFromDER($data, $idx, 1); |
|
| 70 | + $byte = ord($data[$idx++]); |
|
| 71 | + if ($byte !== 0) { |
|
| 72 | + if ($byte != 0xff) { |
|
| 73 | + throw new DecodeException( |
|
| 74 | + "DER encoded boolean true must have all bits set to 1."); |
|
| 75 | + } |
|
| 76 | + } |
|
| 77 | + $offset = $idx; |
|
| 78 | + return new self($byte !== 0); |
|
| 79 | + } |
|
| 80 | 80 | } |
@@ -11,48 +11,48 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | abstract class StringType extends Element |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * String value. |
|
| 16 | - * |
|
| 17 | - * @var string $_string |
|
| 18 | - */ |
|
| 19 | - protected $_string; |
|
| 14 | + /** |
|
| 15 | + * String value. |
|
| 16 | + * |
|
| 17 | + * @var string $_string |
|
| 18 | + */ |
|
| 19 | + protected $_string; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Constructor. |
|
| 23 | - * |
|
| 24 | - * @param string $string |
|
| 25 | - * @throws \InvalidArgumentException |
|
| 26 | - */ |
|
| 27 | - public function __construct(string $string) |
|
| 28 | - { |
|
| 29 | - if (!$this->_validateString($string)) { |
|
| 30 | - throw new \InvalidArgumentException( |
|
| 31 | - sprintf("Not a valid %s string.", |
|
| 32 | - self::tagToName($this->_typeTag))); |
|
| 33 | - } |
|
| 34 | - $this->_string = $string; |
|
| 35 | - } |
|
| 21 | + /** |
|
| 22 | + * Constructor. |
|
| 23 | + * |
|
| 24 | + * @param string $string |
|
| 25 | + * @throws \InvalidArgumentException |
|
| 26 | + */ |
|
| 27 | + public function __construct(string $string) |
|
| 28 | + { |
|
| 29 | + if (!$this->_validateString($string)) { |
|
| 30 | + throw new \InvalidArgumentException( |
|
| 31 | + sprintf("Not a valid %s string.", |
|
| 32 | + self::tagToName($this->_typeTag))); |
|
| 33 | + } |
|
| 34 | + $this->_string = $string; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Get the string value. |
|
| 39 | - * |
|
| 40 | - * @return string |
|
| 41 | - */ |
|
| 42 | - public function string(): string |
|
| 43 | - { |
|
| 44 | - return $this->_string; |
|
| 45 | - } |
|
| 37 | + /** |
|
| 38 | + * Get the string value. |
|
| 39 | + * |
|
| 40 | + * @return string |
|
| 41 | + */ |
|
| 42 | + public function string(): string |
|
| 43 | + { |
|
| 44 | + return $this->_string; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Check whether string is valid for the concrete type. |
|
| 49 | - * |
|
| 50 | - * @param string $string |
|
| 51 | - * @return bool |
|
| 52 | - */ |
|
| 53 | - protected function _validateString(string $string): bool |
|
| 54 | - { |
|
| 55 | - // Override in derived classes |
|
| 56 | - return true; |
|
| 57 | - } |
|
| 47 | + /** |
|
| 48 | + * Check whether string is valid for the concrete type. |
|
| 49 | + * |
|
| 50 | + * @param string $string |
|
| 51 | + * @return bool |
|
| 52 | + */ |
|
| 53 | + protected function _validateString(string $string): bool |
|
| 54 | + { |
|
| 55 | + // Override in derived classes |
|
| 56 | + return true; |
|
| 57 | + } |
|
| 58 | 58 | } |
@@ -14,41 +14,41 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | abstract class PrimitiveString extends StringType |
| 16 | 16 | { |
| 17 | - use PrimitiveType; |
|
| 17 | + use PrimitiveType; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * |
|
| 21 | - * @see \ASN1\Element::_encodedContentDER() |
|
| 22 | - * @return string |
|
| 23 | - */ |
|
| 24 | - protected function _encodedContentDER(): string |
|
| 25 | - { |
|
| 26 | - return $this->_string; |
|
| 27 | - } |
|
| 19 | + /** |
|
| 20 | + * |
|
| 21 | + * @see \ASN1\Element::_encodedContentDER() |
|
| 22 | + * @return string |
|
| 23 | + */ |
|
| 24 | + protected function _encodedContentDER(): string |
|
| 25 | + { |
|
| 26 | + return $this->_string; |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * |
|
| 31 | - * {@inheritdoc} |
|
| 32 | - * @see \ASN1\Element::_decodeFromDER() |
|
| 33 | - * @return self |
|
| 34 | - */ |
|
| 35 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 36 | - int &$offset): ElementBase |
|
| 37 | - { |
|
| 38 | - $idx = $offset; |
|
| 39 | - if (!$identifier->isPrimitive()) { |
|
| 40 | - throw new DecodeException("DER encoded string must be primitive."); |
|
| 41 | - } |
|
| 42 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 43 | - $str = $length ? substr($data, $idx, $length) : ""; |
|
| 44 | - // substr should never return false, since length is |
|
| 45 | - // checked by Length::expectFromDER. |
|
| 46 | - assert(is_string($str), "substr"); |
|
| 47 | - $offset = $idx + $length; |
|
| 48 | - try { |
|
| 49 | - return new static($str); |
|
| 50 | - } catch (\InvalidArgumentException $e) { |
|
| 51 | - throw new DecodeException($e->getMessage(), 0, $e); |
|
| 52 | - } |
|
| 53 | - } |
|
| 29 | + /** |
|
| 30 | + * |
|
| 31 | + * {@inheritdoc} |
|
| 32 | + * @see \ASN1\Element::_decodeFromDER() |
|
| 33 | + * @return self |
|
| 34 | + */ |
|
| 35 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 36 | + int &$offset): ElementBase |
|
| 37 | + { |
|
| 38 | + $idx = $offset; |
|
| 39 | + if (!$identifier->isPrimitive()) { |
|
| 40 | + throw new DecodeException("DER encoded string must be primitive."); |
|
| 41 | + } |
|
| 42 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 43 | + $str = $length ? substr($data, $idx, $length) : ""; |
|
| 44 | + // substr should never return false, since length is |
|
| 45 | + // checked by Length::expectFromDER. |
|
| 46 | + assert(is_string($str), "substr"); |
|
| 47 | + $offset = $idx + $length; |
|
| 48 | + try { |
|
| 49 | + return new static($str); |
|
| 50 | + } catch (\InvalidArgumentException $e) { |
|
| 51 | + throw new DecodeException($e->getMessage(), 0, $e); |
|
| 52 | + } |
|
| 53 | + } |
|
| 54 | 54 | } |
@@ -17,271 +17,271 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class Real extends Element |
| 19 | 19 | { |
| 20 | - use UniversalClass; |
|
| 21 | - use PrimitiveType; |
|
| 20 | + use UniversalClass; |
|
| 21 | + use PrimitiveType; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Regex pattern to parse NR3 form number conforming to DER. |
|
| 25 | - * |
|
| 26 | - * @var string |
|
| 27 | - */ |
|
| 28 | - const NR3_REGEX = '/^(-?)(\d+)?\.E([+\-]?\d+)$/'; |
|
| 23 | + /** |
|
| 24 | + * Regex pattern to parse NR3 form number conforming to DER. |
|
| 25 | + * |
|
| 26 | + * @var string |
|
| 27 | + */ |
|
| 28 | + const NR3_REGEX = '/^(-?)(\d+)?\.E([+\-]?\d+)$/'; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Regex pattern to parse PHP exponent number format. |
|
| 32 | - * |
|
| 33 | - * @link http://php.net/manual/en/language.types.float.php |
|
| 34 | - * @var string |
|
| 35 | - */ |
|
| 36 | - const PHP_EXPONENT_DNUM = '/^'. /* @formatter:off */ |
|
| 37 | - '([+\-]?'. // sign |
|
| 38 | - '(?:'. |
|
| 39 | - '\d+'. // LNUM |
|
| 40 | - '|'. |
|
| 41 | - '(?:\d*\.\d+|\d+\.\d*)'. // DNUM |
|
| 42 | - '))[eE]'. |
|
| 43 | - '([+\-]?\d+)'. // exponent |
|
| 44 | - '$/'; /* @formatter:on */ |
|
| 30 | + /** |
|
| 31 | + * Regex pattern to parse PHP exponent number format. |
|
| 32 | + * |
|
| 33 | + * @link http://php.net/manual/en/language.types.float.php |
|
| 34 | + * @var string |
|
| 35 | + */ |
|
| 36 | + const PHP_EXPONENT_DNUM = '/^'. /* @formatter:off */ |
|
| 37 | + '([+\-]?'. // sign |
|
| 38 | + '(?:'. |
|
| 39 | + '\d+'. // LNUM |
|
| 40 | + '|'. |
|
| 41 | + '(?:\d*\.\d+|\d+\.\d*)'. // DNUM |
|
| 42 | + '))[eE]'. |
|
| 43 | + '([+\-]?\d+)'. // exponent |
|
| 44 | + '$/'; /* @formatter:on */ |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Number zero represented in NR3 form. |
|
| 48 | - * |
|
| 49 | - * @var string |
|
| 50 | - */ |
|
| 51 | - const NR3_ZERO = ".E+0"; |
|
| 46 | + /** |
|
| 47 | + * Number zero represented in NR3 form. |
|
| 48 | + * |
|
| 49 | + * @var string |
|
| 50 | + */ |
|
| 51 | + const NR3_ZERO = ".E+0"; |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Number in NR3 form. |
|
| 55 | - * |
|
| 56 | - * @var string |
|
| 57 | - */ |
|
| 58 | - private $_number; |
|
| 53 | + /** |
|
| 54 | + * Number in NR3 form. |
|
| 55 | + * |
|
| 56 | + * @var string |
|
| 57 | + */ |
|
| 58 | + private $_number; |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Constructor. |
|
| 62 | - * |
|
| 63 | - * @param string $number Number in NR3 form. |
|
| 64 | - */ |
|
| 65 | - public function __construct(string $number) |
|
| 66 | - { |
|
| 67 | - $this->_typeTag = self::TYPE_REAL; |
|
| 68 | - if (!self::_validateNumber($number)) { |
|
| 69 | - throw new \InvalidArgumentException( |
|
| 70 | - "'$number' is not a valid NR3 form real."); |
|
| 71 | - } |
|
| 72 | - $this->_number = $number; |
|
| 73 | - } |
|
| 60 | + /** |
|
| 61 | + * Constructor. |
|
| 62 | + * |
|
| 63 | + * @param string $number Number in NR3 form. |
|
| 64 | + */ |
|
| 65 | + public function __construct(string $number) |
|
| 66 | + { |
|
| 67 | + $this->_typeTag = self::TYPE_REAL; |
|
| 68 | + if (!self::_validateNumber($number)) { |
|
| 69 | + throw new \InvalidArgumentException( |
|
| 70 | + "'$number' is not a valid NR3 form real."); |
|
| 71 | + } |
|
| 72 | + $this->_number = $number; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * Initialize from float. |
|
| 77 | - * |
|
| 78 | - * @param float $number |
|
| 79 | - * @return self |
|
| 80 | - */ |
|
| 81 | - public static function fromFloat(float $number): self |
|
| 82 | - { |
|
| 83 | - return new self(self::_decimalToNR3(strval($number))); |
|
| 84 | - } |
|
| 75 | + /** |
|
| 76 | + * Initialize from float. |
|
| 77 | + * |
|
| 78 | + * @param float $number |
|
| 79 | + * @return self |
|
| 80 | + */ |
|
| 81 | + public static function fromFloat(float $number): self |
|
| 82 | + { |
|
| 83 | + return new self(self::_decimalToNR3(strval($number))); |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * Get number as a float. |
|
| 88 | - * |
|
| 89 | - * @return float |
|
| 90 | - */ |
|
| 91 | - public function float(): float |
|
| 92 | - { |
|
| 93 | - return self::_nr3ToDecimal($this->_number); |
|
| 94 | - } |
|
| 86 | + /** |
|
| 87 | + * Get number as a float. |
|
| 88 | + * |
|
| 89 | + * @return float |
|
| 90 | + */ |
|
| 91 | + public function float(): float |
|
| 92 | + { |
|
| 93 | + return self::_nr3ToDecimal($this->_number); |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - /** |
|
| 97 | - * |
|
| 98 | - * {@inheritdoc} |
|
| 99 | - */ |
|
| 100 | - protected function _encodedContentDER(): string |
|
| 101 | - { |
|
| 102 | - /* if the real value is the value zero, there shall be no contents |
|
| 96 | + /** |
|
| 97 | + * |
|
| 98 | + * {@inheritdoc} |
|
| 99 | + */ |
|
| 100 | + protected function _encodedContentDER(): string |
|
| 101 | + { |
|
| 102 | + /* if the real value is the value zero, there shall be no contents |
|
| 103 | 103 | octets in the encoding. (X.690 07-2002, section 8.5.2) */ |
| 104 | - if (self::NR3_ZERO == $this->_number) { |
|
| 105 | - return ""; |
|
| 106 | - } |
|
| 107 | - // encode in NR3 decimal encoding |
|
| 108 | - $data = chr(0x03) . $this->_number; |
|
| 109 | - return $data; |
|
| 110 | - } |
|
| 104 | + if (self::NR3_ZERO == $this->_number) { |
|
| 105 | + return ""; |
|
| 106 | + } |
|
| 107 | + // encode in NR3 decimal encoding |
|
| 108 | + $data = chr(0x03) . $this->_number; |
|
| 109 | + return $data; |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * |
|
| 114 | - * {@inheritdoc} |
|
| 115 | - * @return self |
|
| 116 | - */ |
|
| 117 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 118 | - int &$offset): ElementBase |
|
| 119 | - { |
|
| 120 | - $idx = $offset; |
|
| 121 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 122 | - // if length is zero, value is zero (spec 8.5.2) |
|
| 123 | - if (!$length) { |
|
| 124 | - $obj = new self(self::NR3_ZERO); |
|
| 125 | - } else { |
|
| 126 | - $bytes = substr($data, $idx, $length); |
|
| 127 | - $byte = ord($bytes[0]); |
|
| 128 | - if (0x80 & $byte) { // bit 8 = 1 |
|
| 129 | - $obj = self::_decodeBinaryEncoding($bytes); |
|
| 130 | - } else if ($byte >> 6 == 0x00) { // bit 8 = 0, bit 7 = 0 |
|
| 131 | - $obj = self::_decodeDecimalEncoding($bytes); |
|
| 132 | - } else { // bit 8 = 0, bit 7 = 1 |
|
| 133 | - $obj = self::_decodeSpecialRealValue($bytes); |
|
| 134 | - } |
|
| 135 | - } |
|
| 136 | - $offset = $idx + $length; |
|
| 137 | - return $obj; |
|
| 138 | - } |
|
| 112 | + /** |
|
| 113 | + * |
|
| 114 | + * {@inheritdoc} |
|
| 115 | + * @return self |
|
| 116 | + */ |
|
| 117 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 118 | + int &$offset): ElementBase |
|
| 119 | + { |
|
| 120 | + $idx = $offset; |
|
| 121 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 122 | + // if length is zero, value is zero (spec 8.5.2) |
|
| 123 | + if (!$length) { |
|
| 124 | + $obj = new self(self::NR3_ZERO); |
|
| 125 | + } else { |
|
| 126 | + $bytes = substr($data, $idx, $length); |
|
| 127 | + $byte = ord($bytes[0]); |
|
| 128 | + if (0x80 & $byte) { // bit 8 = 1 |
|
| 129 | + $obj = self::_decodeBinaryEncoding($bytes); |
|
| 130 | + } else if ($byte >> 6 == 0x00) { // bit 8 = 0, bit 7 = 0 |
|
| 131 | + $obj = self::_decodeDecimalEncoding($bytes); |
|
| 132 | + } else { // bit 8 = 0, bit 7 = 1 |
|
| 133 | + $obj = self::_decodeSpecialRealValue($bytes); |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | + $offset = $idx + $length; |
|
| 137 | + return $obj; |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | - /** |
|
| 141 | - * |
|
| 142 | - * @todo Implement |
|
| 143 | - * @param string $data |
|
| 144 | - */ |
|
| 145 | - protected static function _decodeBinaryEncoding(string $data) |
|
| 146 | - { |
|
| 147 | - throw new \RuntimeException( |
|
| 148 | - "Binary encoding of REAL is not implemented."); |
|
| 149 | - } |
|
| 140 | + /** |
|
| 141 | + * |
|
| 142 | + * @todo Implement |
|
| 143 | + * @param string $data |
|
| 144 | + */ |
|
| 145 | + protected static function _decodeBinaryEncoding(string $data) |
|
| 146 | + { |
|
| 147 | + throw new \RuntimeException( |
|
| 148 | + "Binary encoding of REAL is not implemented."); |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - /** |
|
| 152 | - * |
|
| 153 | - * @param string $data |
|
| 154 | - * @throws \RuntimeException |
|
| 155 | - * @return \ASN1\Type\Primitive\Real |
|
| 156 | - */ |
|
| 157 | - protected static function _decodeDecimalEncoding(string $data): Real |
|
| 158 | - { |
|
| 159 | - $nr = ord($data[0]) & 0x03; |
|
| 160 | - if ($nr != 0x03) { |
|
| 161 | - throw new \RuntimeException("Only NR3 form supported."); |
|
| 162 | - } |
|
| 163 | - $str = substr($data, 1); |
|
| 164 | - return new self($str); |
|
| 165 | - } |
|
| 151 | + /** |
|
| 152 | + * |
|
| 153 | + * @param string $data |
|
| 154 | + * @throws \RuntimeException |
|
| 155 | + * @return \ASN1\Type\Primitive\Real |
|
| 156 | + */ |
|
| 157 | + protected static function _decodeDecimalEncoding(string $data): Real |
|
| 158 | + { |
|
| 159 | + $nr = ord($data[0]) & 0x03; |
|
| 160 | + if ($nr != 0x03) { |
|
| 161 | + throw new \RuntimeException("Only NR3 form supported."); |
|
| 162 | + } |
|
| 163 | + $str = substr($data, 1); |
|
| 164 | + return new self($str); |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * |
|
| 169 | - * @todo Implement |
|
| 170 | - * @param string $data |
|
| 171 | - */ |
|
| 172 | - protected static function _decodeSpecialRealValue(string $data) |
|
| 173 | - { |
|
| 174 | - if (strlen($data) != 1) { |
|
| 175 | - throw new DecodeException( |
|
| 176 | - "SpecialRealValue must have one content octet."); |
|
| 177 | - } |
|
| 178 | - $byte = ord($data[0]); |
|
| 179 | - if ($byte == 0x40) { // positive infinity |
|
| 180 | - throw new \RuntimeException("PLUS-INFINITY not supported."); |
|
| 181 | - } else if ($byte == 0x41) { // negative infinity |
|
| 182 | - throw new \RuntimeException("MINUS-INFINITY not supported."); |
|
| 183 | - } else { |
|
| 184 | - throw new DecodeException("Invalid SpecialRealValue encoding."); |
|
| 185 | - } |
|
| 186 | - } |
|
| 167 | + /** |
|
| 168 | + * |
|
| 169 | + * @todo Implement |
|
| 170 | + * @param string $data |
|
| 171 | + */ |
|
| 172 | + protected static function _decodeSpecialRealValue(string $data) |
|
| 173 | + { |
|
| 174 | + if (strlen($data) != 1) { |
|
| 175 | + throw new DecodeException( |
|
| 176 | + "SpecialRealValue must have one content octet."); |
|
| 177 | + } |
|
| 178 | + $byte = ord($data[0]); |
|
| 179 | + if ($byte == 0x40) { // positive infinity |
|
| 180 | + throw new \RuntimeException("PLUS-INFINITY not supported."); |
|
| 181 | + } else if ($byte == 0x41) { // negative infinity |
|
| 182 | + throw new \RuntimeException("MINUS-INFINITY not supported."); |
|
| 183 | + } else { |
|
| 184 | + throw new DecodeException("Invalid SpecialRealValue encoding."); |
|
| 185 | + } |
|
| 186 | + } |
|
| 187 | 187 | |
| 188 | - /** |
|
| 189 | - * Convert decimal number string to NR3 form. |
|
| 190 | - * |
|
| 191 | - * @param string $str |
|
| 192 | - * @return string |
|
| 193 | - */ |
|
| 194 | - private static function _decimalToNR3(string $str): string |
|
| 195 | - { |
|
| 196 | - // if number is in exponent form |
|
| 197 | - if (preg_match(self::PHP_EXPONENT_DNUM, $str, $match)) { |
|
| 198 | - $parts = explode(".", $match[1]); |
|
| 199 | - $m = ltrim($parts[0], "0"); |
|
| 200 | - $e = intval($match[2]); |
|
| 201 | - // if mantissa had decimals |
|
| 202 | - if (count($parts) == 2) { |
|
| 203 | - $d = rtrim($parts[1], "0"); |
|
| 204 | - $e -= strlen($d); |
|
| 205 | - $m .= $d; |
|
| 206 | - } |
|
| 207 | - } else { |
|
| 208 | - // explode from decimal |
|
| 209 | - $parts = explode(".", $str); |
|
| 210 | - $m = ltrim($parts[0], "0"); |
|
| 211 | - // if number had decimals |
|
| 212 | - if (count($parts) == 2) { |
|
| 213 | - // exponent is negative number of the decimals |
|
| 214 | - $e = -strlen($parts[1]); |
|
| 215 | - // append decimals to the mantissa |
|
| 216 | - $m .= $parts[1]; |
|
| 217 | - } else { |
|
| 218 | - $e = 0; |
|
| 219 | - } |
|
| 220 | - // shift trailing zeroes from the mantissa to the exponent |
|
| 221 | - while (substr($m, -1) === "0") { |
|
| 222 | - $e++; |
|
| 223 | - $m = substr($m, 0, -1); |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - /* if exponent is zero, it must be prefixed with a "+" sign |
|
| 188 | + /** |
|
| 189 | + * Convert decimal number string to NR3 form. |
|
| 190 | + * |
|
| 191 | + * @param string $str |
|
| 192 | + * @return string |
|
| 193 | + */ |
|
| 194 | + private static function _decimalToNR3(string $str): string |
|
| 195 | + { |
|
| 196 | + // if number is in exponent form |
|
| 197 | + if (preg_match(self::PHP_EXPONENT_DNUM, $str, $match)) { |
|
| 198 | + $parts = explode(".", $match[1]); |
|
| 199 | + $m = ltrim($parts[0], "0"); |
|
| 200 | + $e = intval($match[2]); |
|
| 201 | + // if mantissa had decimals |
|
| 202 | + if (count($parts) == 2) { |
|
| 203 | + $d = rtrim($parts[1], "0"); |
|
| 204 | + $e -= strlen($d); |
|
| 205 | + $m .= $d; |
|
| 206 | + } |
|
| 207 | + } else { |
|
| 208 | + // explode from decimal |
|
| 209 | + $parts = explode(".", $str); |
|
| 210 | + $m = ltrim($parts[0], "0"); |
|
| 211 | + // if number had decimals |
|
| 212 | + if (count($parts) == 2) { |
|
| 213 | + // exponent is negative number of the decimals |
|
| 214 | + $e = -strlen($parts[1]); |
|
| 215 | + // append decimals to the mantissa |
|
| 216 | + $m .= $parts[1]; |
|
| 217 | + } else { |
|
| 218 | + $e = 0; |
|
| 219 | + } |
|
| 220 | + // shift trailing zeroes from the mantissa to the exponent |
|
| 221 | + while (substr($m, -1) === "0") { |
|
| 222 | + $e++; |
|
| 223 | + $m = substr($m, 0, -1); |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + /* if exponent is zero, it must be prefixed with a "+" sign |
|
| 227 | 227 | (X.690 07-2002, section 11.3.2.6) */ |
| 228 | - if (0 == $e) { |
|
| 229 | - $es = "+"; |
|
| 230 | - } else { |
|
| 231 | - $es = $e < 0 ? "-" : ""; |
|
| 232 | - } |
|
| 233 | - return sprintf("%s.E%s%d", $m, $es, abs($e)); |
|
| 234 | - } |
|
| 228 | + if (0 == $e) { |
|
| 229 | + $es = "+"; |
|
| 230 | + } else { |
|
| 231 | + $es = $e < 0 ? "-" : ""; |
|
| 232 | + } |
|
| 233 | + return sprintf("%s.E%s%d", $m, $es, abs($e)); |
|
| 234 | + } |
|
| 235 | 235 | |
| 236 | - /** |
|
| 237 | - * Convert NR3 form number to decimal. |
|
| 238 | - * |
|
| 239 | - * @param string $str |
|
| 240 | - * @throws \UnexpectedValueException |
|
| 241 | - * @return float |
|
| 242 | - */ |
|
| 243 | - private static function _nr3ToDecimal(string $str): float |
|
| 244 | - { |
|
| 245 | - if (!preg_match(self::NR3_REGEX, $str, $match)) { |
|
| 246 | - throw new \UnexpectedValueException( |
|
| 247 | - "'$str' is not a valid NR3 form real."); |
|
| 248 | - } |
|
| 249 | - $m = $match[2]; |
|
| 250 | - // if number started with minus sign |
|
| 251 | - $inv = $match[1] == "-"; |
|
| 252 | - $e = intval($match[3]); |
|
| 253 | - // positive exponent |
|
| 254 | - if ($e > 0) { |
|
| 255 | - // pad with trailing zeroes |
|
| 256 | - $num = $m . str_repeat("0", $e); |
|
| 257 | - } else if ($e < 0) { |
|
| 258 | - // pad with leading zeroes |
|
| 259 | - if (strlen($m) < abs($e)) { |
|
| 260 | - $m = str_repeat("0", abs($e) - strlen($m)) . $m; |
|
| 261 | - } |
|
| 262 | - // insert decimal point |
|
| 263 | - $num = substr($m, 0, $e) . "." . substr($m, $e); |
|
| 264 | - } else { |
|
| 265 | - $num = empty($m) ? "0" : $m; |
|
| 266 | - } |
|
| 267 | - // if number is negative |
|
| 268 | - if ($inv) { |
|
| 269 | - $num = "-$num"; |
|
| 270 | - } |
|
| 271 | - return floatval($num); |
|
| 272 | - } |
|
| 236 | + /** |
|
| 237 | + * Convert NR3 form number to decimal. |
|
| 238 | + * |
|
| 239 | + * @param string $str |
|
| 240 | + * @throws \UnexpectedValueException |
|
| 241 | + * @return float |
|
| 242 | + */ |
|
| 243 | + private static function _nr3ToDecimal(string $str): float |
|
| 244 | + { |
|
| 245 | + if (!preg_match(self::NR3_REGEX, $str, $match)) { |
|
| 246 | + throw new \UnexpectedValueException( |
|
| 247 | + "'$str' is not a valid NR3 form real."); |
|
| 248 | + } |
|
| 249 | + $m = $match[2]; |
|
| 250 | + // if number started with minus sign |
|
| 251 | + $inv = $match[1] == "-"; |
|
| 252 | + $e = intval($match[3]); |
|
| 253 | + // positive exponent |
|
| 254 | + if ($e > 0) { |
|
| 255 | + // pad with trailing zeroes |
|
| 256 | + $num = $m . str_repeat("0", $e); |
|
| 257 | + } else if ($e < 0) { |
|
| 258 | + // pad with leading zeroes |
|
| 259 | + if (strlen($m) < abs($e)) { |
|
| 260 | + $m = str_repeat("0", abs($e) - strlen($m)) . $m; |
|
| 261 | + } |
|
| 262 | + // insert decimal point |
|
| 263 | + $num = substr($m, 0, $e) . "." . substr($m, $e); |
|
| 264 | + } else { |
|
| 265 | + $num = empty($m) ? "0" : $m; |
|
| 266 | + } |
|
| 267 | + // if number is negative |
|
| 268 | + if ($inv) { |
|
| 269 | + $num = "-$num"; |
|
| 270 | + } |
|
| 271 | + return floatval($num); |
|
| 272 | + } |
|
| 273 | 273 | |
| 274 | - /** |
|
| 275 | - * Test that number is valid for this context. |
|
| 276 | - * |
|
| 277 | - * @param mixed $num |
|
| 278 | - * @return boolean |
|
| 279 | - */ |
|
| 280 | - private static function _validateNumber($num): bool |
|
| 281 | - { |
|
| 282 | - if (!preg_match(self::NR3_REGEX, $num)) { |
|
| 283 | - return false; |
|
| 284 | - } |
|
| 285 | - return true; |
|
| 286 | - } |
|
| 274 | + /** |
|
| 275 | + * Test that number is valid for this context. |
|
| 276 | + * |
|
| 277 | + * @param mixed $num |
|
| 278 | + * @return boolean |
|
| 279 | + */ |
|
| 280 | + private static function _validateNumber($num): bool |
|
| 281 | + { |
|
| 282 | + if (!preg_match(self::NR3_REGEX, $num)) { |
|
| 283 | + return false; |
|
| 284 | + } |
|
| 285 | + return true; |
|
| 286 | + } |
|
| 287 | 287 | } |
@@ -13,38 +13,38 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class RelativeOID extends ObjectIdentifier |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Constructor. |
|
| 18 | - * |
|
| 19 | - * @param string $oid OID in dotted format |
|
| 20 | - */ |
|
| 21 | - public function __construct(string $oid) |
|
| 22 | - { |
|
| 23 | - $this->_oid = $oid; |
|
| 24 | - $this->_typeTag = self::TYPE_RELATIVE_OID; |
|
| 25 | - } |
|
| 16 | + /** |
|
| 17 | + * Constructor. |
|
| 18 | + * |
|
| 19 | + * @param string $oid OID in dotted format |
|
| 20 | + */ |
|
| 21 | + public function __construct(string $oid) |
|
| 22 | + { |
|
| 23 | + $this->_oid = $oid; |
|
| 24 | + $this->_typeTag = self::TYPE_RELATIVE_OID; |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * |
|
| 29 | - * {@inheritdoc} |
|
| 30 | - */ |
|
| 31 | - protected function _encodedContentDER(): string |
|
| 32 | - { |
|
| 33 | - return self::_encodeSubIDs(...self::_explodeDottedOID($this->_oid)); |
|
| 34 | - } |
|
| 27 | + /** |
|
| 28 | + * |
|
| 29 | + * {@inheritdoc} |
|
| 30 | + */ |
|
| 31 | + protected function _encodedContentDER(): string |
|
| 32 | + { |
|
| 33 | + return self::_encodeSubIDs(...self::_explodeDottedOID($this->_oid)); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * |
|
| 38 | - * {@inheritdoc} |
|
| 39 | - * @return self |
|
| 40 | - */ |
|
| 41 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 42 | - int &$offset): ElementBase |
|
| 43 | - { |
|
| 44 | - $idx = $offset; |
|
| 45 | - $len = Length::expectFromDER($data, $idx)->intLength(); |
|
| 46 | - $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
| 47 | - $offset = $idx + $len; |
|
| 48 | - return new self(self::_implodeSubIDs(...$subids)); |
|
| 49 | - } |
|
| 36 | + /** |
|
| 37 | + * |
|
| 38 | + * {@inheritdoc} |
|
| 39 | + * @return self |
|
| 40 | + */ |
|
| 41 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 42 | + int &$offset): ElementBase |
|
| 43 | + { |
|
| 44 | + $idx = $offset; |
|
| 45 | + $len = Length::expectFromDER($data, $idx)->intLength(); |
|
| 46 | + $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
| 47 | + $offset = $idx + $len; |
|
| 48 | + return new self(self::_implodeSubIDs(...$subids)); |
|
| 49 | + } |
|
| 50 | 50 | } |
@@ -17,112 +17,112 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class GeneralizedTime extends TimeType |
| 19 | 19 | { |
| 20 | - use UniversalClass; |
|
| 21 | - use PrimitiveType; |
|
| 20 | + use UniversalClass; |
|
| 21 | + use PrimitiveType; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Regular expression to parse date. |
|
| 25 | - * |
|
| 26 | - * DER restricts format to UTC timezone (Z suffix). |
|
| 27 | - * |
|
| 28 | - * @var string |
|
| 29 | - */ |
|
| 30 | - const REGEX = /* @formatter:off */ '#^' . |
|
| 31 | - '(\d\d\d\d)' . /* YYYY */ |
|
| 32 | - '(\d\d)' . /* MM */ |
|
| 33 | - '(\d\d)' . /* DD */ |
|
| 34 | - '(\d\d)' . /* hh */ |
|
| 35 | - '(\d\d)' . /* mm */ |
|
| 36 | - '(\d\d)' . /* ss */ |
|
| 37 | - '(?:\.(\d+))?' . /* frac */ |
|
| 38 | - 'Z' . /* TZ */ |
|
| 39 | - '$#' /* @formatter:on */; |
|
| 23 | + /** |
|
| 24 | + * Regular expression to parse date. |
|
| 25 | + * |
|
| 26 | + * DER restricts format to UTC timezone (Z suffix). |
|
| 27 | + * |
|
| 28 | + * @var string |
|
| 29 | + */ |
|
| 30 | + const REGEX = /* @formatter:off */ '#^' . |
|
| 31 | + '(\d\d\d\d)' . /* YYYY */ |
|
| 32 | + '(\d\d)' . /* MM */ |
|
| 33 | + '(\d\d)' . /* DD */ |
|
| 34 | + '(\d\d)' . /* hh */ |
|
| 35 | + '(\d\d)' . /* mm */ |
|
| 36 | + '(\d\d)' . /* ss */ |
|
| 37 | + '(?:\.(\d+))?' . /* frac */ |
|
| 38 | + 'Z' . /* TZ */ |
|
| 39 | + '$#' /* @formatter:on */; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Cached formatted date. |
|
| 43 | - * |
|
| 44 | - * @var string|null |
|
| 45 | - */ |
|
| 46 | - private $_formatted; |
|
| 41 | + /** |
|
| 42 | + * Cached formatted date. |
|
| 43 | + * |
|
| 44 | + * @var string|null |
|
| 45 | + */ |
|
| 46 | + private $_formatted; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Constructor. |
|
| 50 | - * |
|
| 51 | - * @param \DateTimeImmutable $dt |
|
| 52 | - */ |
|
| 53 | - public function __construct(\DateTimeImmutable $dt) |
|
| 54 | - { |
|
| 55 | - $this->_typeTag = self::TYPE_GENERALIZED_TIME; |
|
| 56 | - parent::__construct($dt); |
|
| 57 | - } |
|
| 48 | + /** |
|
| 49 | + * Constructor. |
|
| 50 | + * |
|
| 51 | + * @param \DateTimeImmutable $dt |
|
| 52 | + */ |
|
| 53 | + public function __construct(\DateTimeImmutable $dt) |
|
| 54 | + { |
|
| 55 | + $this->_typeTag = self::TYPE_GENERALIZED_TIME; |
|
| 56 | + parent::__construct($dt); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Clear cached variables on clone. |
|
| 61 | - */ |
|
| 62 | - public function __clone() |
|
| 63 | - { |
|
| 64 | - $this->_formatted = null; |
|
| 65 | - } |
|
| 59 | + /** |
|
| 60 | + * Clear cached variables on clone. |
|
| 61 | + */ |
|
| 62 | + public function __clone() |
|
| 63 | + { |
|
| 64 | + $this->_formatted = null; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * |
|
| 69 | - * {@inheritdoc} |
|
| 70 | - */ |
|
| 71 | - protected function _encodedContentDER(): string |
|
| 72 | - { |
|
| 73 | - if (!isset($this->_formatted)) { |
|
| 74 | - $dt = $this->_dateTime->setTimezone( |
|
| 75 | - self::_createTimeZone(self::TZ_UTC)); |
|
| 76 | - $this->_formatted = $dt->format("YmdHis"); |
|
| 77 | - // if fractions were used |
|
| 78 | - $frac = $dt->format("u"); |
|
| 79 | - if ($frac != 0) { |
|
| 80 | - $frac = rtrim($frac, "0"); |
|
| 81 | - $this->_formatted .= ".$frac"; |
|
| 82 | - } |
|
| 83 | - // timezone |
|
| 84 | - $this->_formatted .= "Z"; |
|
| 85 | - } |
|
| 86 | - return $this->_formatted; |
|
| 87 | - } |
|
| 67 | + /** |
|
| 68 | + * |
|
| 69 | + * {@inheritdoc} |
|
| 70 | + */ |
|
| 71 | + protected function _encodedContentDER(): string |
|
| 72 | + { |
|
| 73 | + if (!isset($this->_formatted)) { |
|
| 74 | + $dt = $this->_dateTime->setTimezone( |
|
| 75 | + self::_createTimeZone(self::TZ_UTC)); |
|
| 76 | + $this->_formatted = $dt->format("YmdHis"); |
|
| 77 | + // if fractions were used |
|
| 78 | + $frac = $dt->format("u"); |
|
| 79 | + if ($frac != 0) { |
|
| 80 | + $frac = rtrim($frac, "0"); |
|
| 81 | + $this->_formatted .= ".$frac"; |
|
| 82 | + } |
|
| 83 | + // timezone |
|
| 84 | + $this->_formatted .= "Z"; |
|
| 85 | + } |
|
| 86 | + return $this->_formatted; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * |
|
| 91 | - * {@inheritdoc} |
|
| 92 | - * @return self |
|
| 93 | - */ |
|
| 94 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 95 | - int &$offset): ElementBase |
|
| 96 | - { |
|
| 97 | - $idx = $offset; |
|
| 98 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 99 | - $str = substr($data, $idx, $length); |
|
| 100 | - $idx += $length; |
|
| 101 | - if (!preg_match(self::REGEX, $str, $match)) { |
|
| 102 | - throw new DecodeException("Invalid GeneralizedTime format."); |
|
| 103 | - } |
|
| 104 | - list(, $year, $month, $day, $hour, $minute, $second) = $match; |
|
| 105 | - if (isset($match[7])) { |
|
| 106 | - $frac = $match[7]; |
|
| 107 | - // DER restricts trailing zeroes in fractional seconds component |
|
| 108 | - if ('0' === $frac[strlen($frac) - 1]) { |
|
| 109 | - throw new DecodeException( |
|
| 110 | - "Fractional seconds must omit trailing zeroes."); |
|
| 111 | - } |
|
| 112 | - $frac = (int) $frac; |
|
| 113 | - } else { |
|
| 114 | - $frac = 0; |
|
| 115 | - } |
|
| 116 | - $time = $year . $month . $day . $hour . $minute . $second . "." . $frac . |
|
| 117 | - self::TZ_UTC; |
|
| 118 | - $dt = \DateTimeImmutable::createFromFormat("!YmdHis.uT", $time, |
|
| 119 | - self::_createTimeZone(self::TZ_UTC)); |
|
| 120 | - if (!$dt) { |
|
| 121 | - throw new DecodeException( |
|
| 122 | - "Failed to decode GeneralizedTime: " . |
|
| 123 | - self::_getLastDateTimeImmutableErrorsStr()); |
|
| 124 | - } |
|
| 125 | - $offset = $idx; |
|
| 126 | - return new self($dt); |
|
| 127 | - } |
|
| 89 | + /** |
|
| 90 | + * |
|
| 91 | + * {@inheritdoc} |
|
| 92 | + * @return self |
|
| 93 | + */ |
|
| 94 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 95 | + int &$offset): ElementBase |
|
| 96 | + { |
|
| 97 | + $idx = $offset; |
|
| 98 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
| 99 | + $str = substr($data, $idx, $length); |
|
| 100 | + $idx += $length; |
|
| 101 | + if (!preg_match(self::REGEX, $str, $match)) { |
|
| 102 | + throw new DecodeException("Invalid GeneralizedTime format."); |
|
| 103 | + } |
|
| 104 | + list(, $year, $month, $day, $hour, $minute, $second) = $match; |
|
| 105 | + if (isset($match[7])) { |
|
| 106 | + $frac = $match[7]; |
|
| 107 | + // DER restricts trailing zeroes in fractional seconds component |
|
| 108 | + if ('0' === $frac[strlen($frac) - 1]) { |
|
| 109 | + throw new DecodeException( |
|
| 110 | + "Fractional seconds must omit trailing zeroes."); |
|
| 111 | + } |
|
| 112 | + $frac = (int) $frac; |
|
| 113 | + } else { |
|
| 114 | + $frac = 0; |
|
| 115 | + } |
|
| 116 | + $time = $year . $month . $day . $hour . $minute . $second . "." . $frac . |
|
| 117 | + self::TZ_UTC; |
|
| 118 | + $dt = \DateTimeImmutable::createFromFormat("!YmdHis.uT", $time, |
|
| 119 | + self::_createTimeZone(self::TZ_UTC)); |
|
| 120 | + if (!$dt) { |
|
| 121 | + throw new DecodeException( |
|
| 122 | + "Failed to decode GeneralizedTime: " . |
|
| 123 | + self::_getLastDateTimeImmutableErrorsStr()); |
|
| 124 | + } |
|
| 125 | + $offset = $idx; |
|
| 126 | + return new self($dt); |
|
| 127 | + } |
|
| 128 | 128 | } |