diff --git a/lib/ASN1/ASNObject.php b/lib/ASN1/ASNObject.php index d338ad9..de54fb2 100644 --- a/lib/ASN1/ASNObject.php +++ b/lib/ASN1/ASNObject.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1; @@ -37,21 +38,22 @@ use FG\ASN1\Universal\ObjectDescriptor; use FG\Utility\BigInteger; use LogicException; +use Stringable; /** * Class ASNObject is the base class for all concrete ASN.1 objects. */ -abstract class ASNObject implements Parsable +abstract class ASNObject implements Parsable, Stringable { - private $contentLength; - private $nrOfLengthOctets; + private ?int $contentLength = null; + private ?int $nrOfLengthOctets = null; /** * Must return the number of octets of the content part. * * @return int */ - abstract protected function calculateContentLength(); + abstract protected function calculateContentLength(): int; /** * Encode the object using DER encoding. @@ -60,15 +62,15 @@ abstract protected function calculateContentLength(); * * @return string the binary representation of an objects value */ - abstract protected function getEncodedValue(); + abstract protected function getEncodedValue(): ?string; /** * Return the content of this object in a non encoded form. - * This can be used to print the value in human readable form. + * This can be used to print the value in human-readable form. * * @return mixed */ - abstract public function getContent(); + abstract public function getContent(): mixed; /** * Return the object type octet. @@ -78,7 +80,7 @@ abstract public function getContent(); * * @return int */ - abstract public function getType(); + abstract public function getType(): int; /** * Returns all identifier octets. If an inheriting class models a tag with @@ -89,7 +91,7 @@ abstract public function getType(); * * @return string Identifier as a set of octets */ - public function getIdentifier() + public function getIdentifier(): string { $firstOctet = $this->getType(); @@ -105,7 +107,7 @@ public function getIdentifier() * * @return string the full binary representation of the complete object */ - public function getBinary() + public function getBinary(): string { $result = $this->getIdentifier(); $result .= $this->createLengthPart(); @@ -114,7 +116,7 @@ public function getBinary() return $result; } - private function createLengthPart() + private function createLengthPart(): string { $contentLength = $this->getContentLength(); $nrOfLengthOctets = $this->getNumberOfLengthOctets($contentLength); @@ -132,9 +134,9 @@ private function createLengthPart() } } - protected function getNumberOfLengthOctets($contentLength = null) + protected function getNumberOfLengthOctets(int $contentLength = null): int { - if (!isset($this->nrOfLengthOctets)) { + if ($this->nrOfLengthOctets === null) { if ($contentLength == null) { $contentLength = $this->getContentLength(); } @@ -151,16 +153,16 @@ protected function getNumberOfLengthOctets($contentLength = null) return $this->nrOfLengthOctets; } - protected function getContentLength() + protected function getContentLength(): int { - if (!isset($this->contentLength)) { + if ($this->contentLength === null) { $this->contentLength = $this->calculateContentLength(); } return $this->contentLength; } - protected function setContentLength($newContentLength) + protected function setContentLength(int $newContentLength): void { $this->contentLength = $newContentLength; $this->getNumberOfLengthOctets($newContentLength); @@ -169,7 +171,7 @@ protected function setContentLength($newContentLength) /** * Returns the length of the whole object (including the identifier and length octets). */ - public function getObjectLength() + public function getObjectLength(): int { $nrOfIdentifierOctets = strlen($this->getIdentifier()); $contentLength = $this->getContentLength(); @@ -178,7 +180,7 @@ public function getObjectLength() return $nrOfIdentifierOctets + $nrOfLengthOctets + $contentLength; } - public function __toString() + public function __toString(): string { return $this->getContent(); } @@ -188,7 +190,7 @@ public function __toString() * * @see Identifier::getName() */ - public function getTypeName() + public function getTypeName(): string { return Identifier::getName($this->getType()); } @@ -201,7 +203,7 @@ public function getTypeName() * * @return \FG\ASN1\ASNObject */ - public static function fromBinary(&$binaryData, &$offsetIndex = 0) + public static function fromBinary(string &$binaryData, ?int &$offsetIndex = 0): static { if (strlen($binaryData) <= $offsetIndex) { throw new ParserException('Can not parse binary from data: Offset index larger than input size', $offsetIndex); diff --git a/lib/ASN1/AbstractString.php b/lib/ASN1/AbstractString.php index 7e0d7dd..09f6cde 100644 --- a/lib/ASN1/AbstractString.php +++ b/lib/ASN1/AbstractString.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1; @@ -14,81 +15,76 @@ abstract class AbstractString extends ASNObject implements Parsable { - /** @var string */ - protected $value; - private $checkStringForIllegalChars = true; - private $allowedCharacters = []; + private bool $checkStringForIllegalChars = true; + private array $allowedCharacters = []; /** * The abstract base class for ASN.1 classes which represent some string of character. - * - * @param string $string */ - public function __construct($string) + public function __construct(protected string $value) { - $this->value = $string; } - public function getContent() + public function getContent(): string { return $this->value; } - protected function allowCharacter($character) + protected function allowCharacter(string $character): void { $this->allowedCharacters[] = $character; } - protected function allowCharacters(...$characters) + protected function allowCharacters(string ...$characters): void { foreach ($characters as $character) { $this->allowedCharacters[] = $character; } } - protected function allowNumbers() + protected function allowNumbers(): void { foreach (range('0', '9') as $char) { $this->allowedCharacters[] = (string) $char; } } - protected function allowAllLetters() + protected function allowAllLetters(): void { $this->allowSmallLetters(); $this->allowCapitalLetters(); } - protected function allowSmallLetters() + protected function allowSmallLetters(): void { foreach (range('a', 'z') as $char) { $this->allowedCharacters[] = $char; } } - protected function allowCapitalLetters() + protected function allowCapitalLetters(): void { foreach (range('A', 'Z') as $char) { $this->allowedCharacters[] = $char; } } - protected function allowSpaces() + protected function allowSpaces(): void { $this->allowedCharacters[] = ' '; } - protected function allowAll() + protected function allowAll(): void { $this->checkStringForIllegalChars = false; } - protected function calculateContentLength() + protected function calculateContentLength(): int { return strlen($this->value); } - protected function getEncodedValue() + protected function getEncodedValue(): ?string { if ($this->checkStringForIllegalChars) { $this->checkString(); @@ -97,18 +93,18 @@ protected function getEncodedValue() return $this->value; } - protected function checkString() + protected function checkString(): void { $stringLength = $this->getContentLength(); for ($i = 0; $i < $stringLength; $i++) { - if (in_array($this->value[$i], $this->allowedCharacters) == false) { + if (in_array($this->value[$i], $this->allowedCharacters) === false) { $typeName = Identifier::getName($this->getType()); throw new Exception("Could not create a {$typeName} from the character sequence '{$this->value}'."); } } } - public static function fromBinary(&$binaryData, &$offsetIndex = 0) + public static function fromBinary(string &$binaryData, ?int &$offsetIndex = 0): static { $parsedObject = new static(''); diff --git a/lib/ASN1/AbstractTime.php b/lib/ASN1/AbstractTime.php index f67ef26..5e1dc9c 100644 --- a/lib/ASN1/AbstractTime.php +++ b/lib/ASN1/AbstractTime.php @@ -7,20 +7,21 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1; use DateInterval; use DateTime; +use DateTimeInterface; use DateTimeZone; use Exception; abstract class AbstractTime extends ASNObject { - /** @var DateTime */ - protected $value; + protected DateTimeInterface $value; - public function __construct($dateTime = null, $dateTimeZone = 'UTC') + public function __construct(null|string|\DateTimeInterface $dateTime = null, $dateTimeZone = 'UTC') { if ($dateTime == null || is_string($dateTime)) { if (is_null($dateTime)) { @@ -28,7 +29,7 @@ public function __construct($dateTime = null, $dateTimeZone = 'UTC') } $timeZone = new DateTimeZone($dateTimeZone); $dateTimeObject = new DateTime($dateTime, $timeZone); - if ($dateTimeObject == false) { + if ($dateTimeObject === false) { $errorMessage = $this->getLastDateTimeErrors(); $className = Identifier::getName($this->getType()); throw new Exception(sprintf("Could not create %s from date time string '%s': %s", $className, $dateTime, $errorMessage)); @@ -41,12 +42,12 @@ public function __construct($dateTime = null, $dateTimeZone = 'UTC') $this->value = $dateTime; } - public function getContent() + public function getContent(): \DateTimeInterface { return $this->value; } - protected function getLastDateTimeErrors() + protected function getLastDateTimeErrors(): string { $messages = ''; $lastErrors = DateTime::getLastErrors() ?: ['errors' => []]; @@ -57,12 +58,12 @@ protected function getLastDateTimeErrors() return substr($messages, 0, -2); } - public function __toString() + public function __toString(): string { return $this->value->format("Y-m-d\tH:i:s"); } - protected static function extractTimeZoneData(&$binaryData, &$offsetIndex, DateTime $dateTime) + protected static function extractTimeZoneData(&$binaryData, &$offsetIndex, DateTime $dateTime): \DateTimeInterface { $sign = $binaryData[$offsetIndex++]; $timeOffsetHours = intval(substr($binaryData, $offsetIndex, 2)); diff --git a/lib/ASN1/Base128.php b/lib/ASN1/Base128.php index 119ee7b..d5874bd 100644 --- a/lib/ASN1/Base128.php +++ b/lib/ASN1/Base128.php @@ -1,4 +1,5 @@ modulus(0x80)->toInteger()); @@ -30,13 +26,9 @@ public static function encode($value) } /** - * @param string $octets - * * @throws InvalidArgumentException if the given octets represent a malformed base-128 value or the decoded value would exceed the the maximum integer length - * - * @return int */ - public static function decode($octets) + public static function decode(string $octets): string { $bitsPerOctet = 7; $value = BigInteger::create(0); diff --git a/lib/ASN1/Composite/AttributeTypeAndValue.php b/lib/ASN1/Composite/AttributeTypeAndValue.php index 3f4027c..be8b378 100644 --- a/lib/ASN1/Composite/AttributeTypeAndValue.php +++ b/lib/ASN1/Composite/AttributeTypeAndValue.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1\Composite; @@ -16,11 +17,7 @@ class AttributeTypeAndValue extends Sequence { - /** - * @param ObjectIdentifier|string $objIdentifier - * @param \FG\ASN1\ASNObject $value - */ - public function __construct($objIdentifier, ASNObject $value) + public function __construct(ObjectIdentifier|string $objIdentifier, ASNObject $value) { if ($objIdentifier instanceof ObjectIdentifier == false) { $objIdentifier = new ObjectIdentifier($objIdentifier); @@ -28,7 +25,7 @@ public function __construct($objIdentifier, ASNObject $value) parent::__construct($objIdentifier, $value); } - public function __toString() + public function __toString(): string { return $this->children[0].': '.$this->children[1]; } diff --git a/lib/ASN1/Composite/RDNString.php b/lib/ASN1/Composite/RDNString.php index e95e7ac..8bee3aa 100644 --- a/lib/ASN1/Composite/RDNString.php +++ b/lib/ASN1/Composite/RDNString.php @@ -7,20 +7,19 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1\Composite; +use FG\ASN1\ASNObject; +use FG\ASN1\Universal\ObjectIdentifier; use FG\ASN1\Universal\PrintableString; use FG\ASN1\Universal\IA5String; use FG\ASN1\Universal\UTF8String; class RDNString extends RelativeDistinguishedName { - /** - * @param string|\FG\ASN1\Universal\ObjectIdentifier $objectIdentifierString - * @param string|\FG\ASN1\ASNObject $value - */ - public function __construct($objectIdentifierString, $value) + public function __construct(string|ObjectIdentifier $objectIdentifierString, string|ASNObject $value) { if (PrintableString::isValid($value)) { $value = new PrintableString($value); diff --git a/lib/ASN1/Composite/RelativeDistinguishedName.php b/lib/ASN1/Composite/RelativeDistinguishedName.php index 4185f41..e0ba462 100644 --- a/lib/ASN1/Composite/RelativeDistinguishedName.php +++ b/lib/ASN1/Composite/RelativeDistinguishedName.php @@ -7,27 +7,25 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1\Composite; use FG\ASN1\Exception\NotImplementedException; use FG\ASN1\ASNObject; +use FG\ASN1\Universal\ObjectIdentifier; use FG\ASN1\Universal\Set; class RelativeDistinguishedName extends Set { - /** - * @param string|\FG\ASN1\Universal\ObjectIdentifier $objIdentifierString - * @param \FG\ASN1\ASNObject $value - */ - public function __construct($objIdentifierString, ASNObject $value) + public function __construct(string|ObjectIdentifier $objIdentifierString, ASNObject $value) { // TODO: This does only support one element in the RelativeDistinguishedName Set but it it is defined as follows: // RelativeDistinguishedName ::= SET SIZE (1..MAX) OF AttributeTypeAndValue parent::__construct(new AttributeTypeAndValue($objIdentifierString, $value)); } - public function getContent() + public function getContent(): string { /** @var \FG\ASN1\ASNObject $firstObject */ $firstObject = $this->children[0]; @@ -43,7 +41,7 @@ public function getContent() * @param int $offsetIndex * @throws NotImplementedException */ - public static function fromBinary(&$binaryData, &$offsetIndex = 0) + public static function fromBinary(string &$binaryData, ?int &$offsetIndex = 0): static { throw new NotImplementedException(); } diff --git a/lib/ASN1/Construct.php b/lib/ASN1/Construct.php index cedf422..2452909 100644 --- a/lib/ASN1/Construct.php +++ b/lib/ASN1/Construct.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1; @@ -18,68 +19,59 @@ abstract class Construct extends ASNObject implements Countable, ArrayAccess, Iterator, Parsable { - /** @var \FG\ASN1\ASNObject[] */ - protected $children; - private $iteratorPosition; + /** @var ASNObject[] */ + protected array $children; + private int $iteratorPosition = 0; /** - * @param \FG\ASN1\ASNObject[] $children the variadic type hint is commented due to https://github.com/facebook/hhvm/issues/4858 + * @param ASNObject[] $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) + public function __construct(ASNObject ...$children) { $this->children = $children; - $this->iteratorPosition = 0; } - public function getContent() + public function getContent(): mixed { return $this->children; } - #[\ReturnTypeWillChange] - public function rewind() + public function rewind(): void { $this->iteratorPosition = 0; } - #[\ReturnTypeWillChange] - public function current() + public function current(): ASNObject { return $this->children[$this->iteratorPosition]; } - #[\ReturnTypeWillChange] - public function key() + public function key(): int { return $this->iteratorPosition; } - #[\ReturnTypeWillChange] - public function next() + public function next(): void { $this->iteratorPosition++; } - #[\ReturnTypeWillChange] - public function valid() + public function valid(): bool { return isset($this->children[$this->iteratorPosition]); } - #[\ReturnTypeWillChange] - public function offsetExists($offset) + public function offsetExists($offset): bool { return array_key_exists($offset, $this->children); } - #[\ReturnTypeWillChange] - public function offsetGet($offset) + public function offsetGet($offset): ASNObject { return $this->children[$offset]; } - #[\ReturnTypeWillChange] - public function offsetSet($offset, $value) + public function offsetSet($offset, $value): void { if ($offset === null) { $offset = count($this->children); @@ -88,13 +80,12 @@ public function offsetSet($offset, $value) $this->children[$offset] = $value; } - #[\ReturnTypeWillChange] - public function offsetUnset($offset) + public function offsetUnset($offset): void { unset($this->children[$offset]); } - protected function calculateContentLength() + protected function calculateContentLength(): int { $length = 0; foreach ($this->children as $component) { @@ -104,7 +95,7 @@ protected function calculateContentLength() return $length; } - protected function getEncodedValue() + protected function getEncodedValue(): ?string { $result = ''; foreach ($this->children as $component) { @@ -114,19 +105,19 @@ protected function getEncodedValue() return $result; } - public function addChild(ASNObject $child) + public function addChild(ASNObject $child): void { $this->children[] = $child; } - public function addChildren(array $children) + public function addChildren(array $children): void { foreach ($children as $child) { $this->addChild($child); } } - public function __toString() + public function __toString(): string { $nrOfChildren = $this->getNumberOfChildren(); $childString = $nrOfChildren == 1 ? 'child' : 'children'; @@ -134,23 +125,23 @@ public function __toString() return "[{$nrOfChildren} {$childString}]"; } - public function getNumberOfChildren() + public function getNumberOfChildren(): int { return count($this->children); } /** - * @return \FG\ASN1\ASNObject[] + * @return ASNObject[] */ - public function getChildren() + public function getChildren(): array { return $this->children; } /** - * @return \FG\ASN1\ASNObject + * @return ASNObject */ - public function getFirstChild() + public function getFirstChild(): ASNObject { return $this->children[0]; } @@ -163,8 +154,7 @@ public function getFirstChild() * * @return Construct|static */ - #[\ReturnTypeWillChange] - public static function fromBinary(&$binaryData, &$offsetIndex = 0) + public static function fromBinary(string &$binaryData, ?int &$offsetIndex = 0): static { $parsedObject = new static(); self::parseIdentifier($binaryData[$offsetIndex], $parsedObject->getType(), $offsetIndex++); @@ -189,13 +179,12 @@ public static function fromBinary(&$binaryData, &$offsetIndex = 0) return $parsedObject; } - #[\ReturnTypeWillChange] - public function count($mode = COUNT_NORMAL) + public function count($mode = COUNT_NORMAL): int { return count($this->children, $mode); } - public function getIterator() + public function getIterator(): \ArrayIterator { return new ArrayIterator($this->children); } diff --git a/lib/ASN1/Exception/NotImplementedException.php b/lib/ASN1/Exception/NotImplementedException.php index c9f8e82..2992a53 100644 --- a/lib/ASN1/Exception/NotImplementedException.php +++ b/lib/ASN1/Exception/NotImplementedException.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1\Exception; diff --git a/lib/ASN1/Exception/ParserException.php b/lib/ASN1/Exception/ParserException.php index 4bda4e8..07a4916 100644 --- a/lib/ASN1/Exception/ParserException.php +++ b/lib/ASN1/Exception/ParserException.php @@ -7,22 +7,18 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1\Exception; class ParserException extends \Exception { - private $errorMessage; - private $offset; - - public function __construct($errorMessage, $offset) + public function __construct(private string $errorMessage, private int $offset) { - $this->errorMessage = $errorMessage; - $this->offset = $offset; parent::__construct("ASN.1 Parser Exception at offset {$this->offset}: {$this->errorMessage}"); } - public function getOffset() + public function getOffset(): int { return $this->offset; } diff --git a/lib/ASN1/ExplicitlyTaggedObject.php b/lib/ASN1/ExplicitlyTaggedObject.php index b947a95..2f42a7d 100644 --- a/lib/ASN1/ExplicitlyTaggedObject.php +++ b/lib/ASN1/ExplicitlyTaggedObject.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1; @@ -31,21 +32,15 @@ */ class ExplicitlyTaggedObject extends ASNObject { - /** @var \FG\ASN1\ASNObject[] */ - private $decoratedObjects; - private $tag; - - /** - * @param int $tag - * @param \FG\ASN1\ASNObject $objects,... - */ - public function __construct($tag, /* HH_FIXME[4858]: variadic + strict */ ...$objects) + /** @var ASNObject[] */ + private array $decoratedObjects; + + public function __construct(private string|int $tag, ASNObject ...$objects) { - $this->tag = $tag; $this->decoratedObjects = $objects; } - protected function calculateContentLength() + protected function calculateContentLength(): int { $length = 0; foreach ($this->decoratedObjects as $object) { @@ -55,7 +50,7 @@ protected function calculateContentLength() return $length; } - protected function getEncodedValue() + protected function getEncodedValue(): ?string { $encoded = ''; foreach ($this->decoratedObjects as $object) { @@ -65,12 +60,12 @@ protected function getEncodedValue() return $encoded; } - public function getContent() + public function getContent(): array { return $this->decoratedObjects; } - public function __toString() + public function __toString(): string { switch ($length = count($this->decoratedObjects)) { case 0: @@ -83,24 +78,24 @@ public function __toString() } } - public function getType() + public function getType(): int { return ord($this->getIdentifier()); } - public function getIdentifier() + public function getIdentifier(): string { $identifier = Identifier::create(Identifier::CLASS_CONTEXT_SPECIFIC, true, $this->tag); return is_int($identifier) ? chr($identifier) : $identifier; } - public function getTag() + public function getTag(): int { return $this->tag; } - public static function fromBinary(&$binaryData, &$offsetIndex = 0) + public static function fromBinary(string &$binaryData, ?int &$offsetIndex = 0): static { $identifier = self::parseBinaryIdentifier($binaryData, $offsetIndex); $firstIdentifierOctet = ord($identifier); diff --git a/lib/ASN1/Identifier.php b/lib/ASN1/Identifier.php index b21caa3..869a440 100644 --- a/lib/ASN1/Identifier.php +++ b/lib/ASN1/Identifier.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1; @@ -71,15 +72,9 @@ class Identifier * Creates an identifier. Short form identifiers are returned as integers * for BC, long form identifiers will be returned as a string of octets. * - * @param int $class - * @param bool $isConstructed - * @param int $tagNumber - * * @throws Exception if the given arguments are invalid - * - * @return int|string */ - public static function create($class, $isConstructed, $tagNumber) + public static function create(int $class, bool $isConstructed, int|string $tagNumber): int|string { if (!is_numeric($class) || $class < self::CLASS_UNIVERSAL || $class > self::CLASS_PRIVATE) { throw new Exception(sprintf('Invalid class %d given', $class)); @@ -104,12 +99,12 @@ public static function create($class, $isConstructed, $tagNumber) return chr($firstOctet).Base128::encode($tagNumber); } - public static function isConstructed($identifierOctet) + public static function isConstructed(int $identifierOctet): bool { return ($identifierOctet & self::IS_CONSTRUCTED) === self::IS_CONSTRUCTED; } - public static function isLongForm($identifierOctet) + public static function isLongForm(int $identifierOctet): bool { return ($identifierOctet & self::LONG_FORM) === self::LONG_FORM; } @@ -120,12 +115,8 @@ public static function isLongForm($identifierOctet) * Example: ASN.1 Octet String * * @see Identifier::getShortName() - * - * @param int|string $identifier - * - * @return string */ - public static function getName($identifier) + public static function getName(int|string $identifier): string { $identifierOctet = self::makeNumeric($identifier); @@ -147,12 +138,8 @@ public static function getName($identifier) * * @see Identifier::getName() * @see Identifier::getClassDescription() - * - * @param int|string $identifier - * - * @return string */ - public static function getShortName($identifier) + public static function getShortName(int|string $identifier): string { $identifierOctet = self::makeNumeric($identifier); @@ -243,12 +230,8 @@ public static function getShortName($identifier) * Example: * Constructed context-specific * Primitive universal - * - * @param int|string $identifier - * - * @return string */ - public static function getClassDescription($identifier) + public static function getClassDescription(int|string $identifier): string { $identifierOctet = self::makeNumeric($identifier); @@ -280,12 +263,7 @@ public static function getClassDescription($identifier) return $classDescription; } - /** - * @param int|string $identifier - * - * @return int - */ - public static function getTagNumber($identifier) + public static function getTagNumber(string|int $identifier): string|int { $firstOctet = self::makeNumeric($identifier); $tagNumber = $firstOctet & self::LONG_FORM; @@ -300,35 +278,35 @@ public static function getTagNumber($identifier) return Base128::decode(substr($identifier, 1)); } - public static function isUniversalClass($identifier) + public static function isUniversalClass(string|int $identifier): bool { $identifier = self::makeNumeric($identifier); return $identifier >> 6 == self::CLASS_UNIVERSAL; } - public static function isApplicationClass($identifier) + public static function isApplicationClass(string|int $identifier): bool { $identifier = self::makeNumeric($identifier); return $identifier >> 6 == self::CLASS_APPLICATION; } - public static function isContextSpecificClass($identifier) + public static function isContextSpecificClass(string|int $identifier): bool { $identifier = self::makeNumeric($identifier); return $identifier >> 6 == self::CLASS_CONTEXT_SPECIFIC; } - public static function isPrivateClass($identifier) + public static function isPrivateClass(string|int $identifier): bool { $identifier = self::makeNumeric($identifier); return $identifier >> 6 == self::CLASS_PRIVATE; } - private static function makeNumeric($identifierOctet) + private static function makeNumeric(int|string $identifierOctet): int { if (!is_numeric($identifierOctet)) { return ord($identifierOctet); diff --git a/lib/ASN1/OID.php b/lib/ASN1/OID.php index bfaeae1..2624b27 100644 --- a/lib/ASN1/OID.php +++ b/lib/ASN1/OID.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1; @@ -69,14 +70,9 @@ class OID * query http://oid-info.com for the right name. * This behavior can be suppressed by setting the second method parameter to false. * - * @param string $oidString - * @param bool $loadFromWeb - * * @see self::loadFromWeb($oidString) - * - * @return string */ - public static function getName($oidString, $loadFromWeb = true) + public static function getName(string $oidString, bool $loadFromWeb = true): string { $oids = [ '1.2' => 'ISO Member Body', @@ -998,7 +994,7 @@ public static function getName($oidString, $loadFromWeb = true) } } - public static function loadFromWeb($oidString) + public static function loadFromWeb(string $oidString): string { $ch = curl_init("http://oid-info.com/get/{$oidString}"); diff --git a/lib/ASN1/Parsable.php b/lib/ASN1/Parsable.php index fa66b55..27fd4f5 100644 --- a/lib/ASN1/Parsable.php +++ b/lib/ASN1/Parsable.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1; @@ -28,5 +29,5 @@ interface Parsable * * @return static */ - public static function fromBinary(&$binaryData, &$offsetIndex = null); + public static function fromBinary(string &$binaryData, ?int &$offsetIndex = 0): static; } diff --git a/lib/ASN1/TemplateParser.php b/lib/ASN1/TemplateParser.php index 90a40b0..a1f6658 100644 --- a/lib/ASN1/TemplateParser.php +++ b/lib/ASN1/TemplateParser.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1; @@ -17,24 +18,18 @@ class TemplateParser { /** - * @param string $data - * @param array $template - * @return \FG\ASN1\ASNObject|Sequence * @throws ParserException if there was an issue parsing */ - public function parseBase64($data, array $template) + public function parseBase64(string $data, array $template): ASNObject|Sequence { // TODO test with invalid data return $this->parseBinary(base64_decode($data), $template); } /** - * @param string $binary - * @param array $template - * @return \FG\ASN1\ASNObject|Sequence * @throws ParserException if there was an issue parsing */ - public function parseBinary($binary, array $template) + public function parseBinary(string $binary, array $template): ASNObject|Sequence { $parsedObject = ASNObject::fromBinary($binary); @@ -45,7 +40,7 @@ public function parseBinary($binary, array $template) return $parsedObject; } - private function validate(ASNObject $object, $key, $value) + private function validate(ASNObject $object, $key, $value): void { if (is_array($value)) { $this->assertTypeId($key, $object); @@ -60,7 +55,7 @@ private function validate(ASNObject $object, $key, $value) } } - private function assertTypeId($expectedTypeId, ASNObject $object) + private function assertTypeId(int $expectedTypeId, ASNObject $object): void { $actualType = $object->getType(); if ($expectedTypeId != $actualType) { diff --git a/lib/ASN1/Universal/BMPString.php b/lib/ASN1/Universal/BMPString.php index 83ec6a9..0d327a0 100644 --- a/lib/ASN1/Universal/BMPString.php +++ b/lib/ASN1/Universal/BMPString.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1\Universal; @@ -25,16 +26,14 @@ class BMPString extends AbstractString * characters outside the Basic Multilingual Plane) of ISO/IEC 10646-1. * * TODO The encodable characters of this type are not yet checked. - * - * @param string $string */ - public function __construct($string) + public function __construct(string $string) { - $this->value = $string; + parent::__construct($string); $this->allowAll(); } - public function getType() + public function getType(): int { return Identifier::BMP_STRING; } diff --git a/lib/ASN1/Universal/BitString.php b/lib/ASN1/Universal/BitString.php index 226695c..825110d 100644 --- a/lib/ASN1/Universal/BitString.php +++ b/lib/ASN1/Universal/BitString.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1\Universal; @@ -17,8 +18,6 @@ class BitString extends OctetString implements Parsable { - private $nrOfUnusedBits; - /** * Creates a new ASN.1 BitString object. * @@ -27,29 +26,27 @@ class BitString extends OctetString implements Parsable * * @throws Exception if the second parameter is no positive numeric value */ - public function __construct($value, $nrOfUnusedBits = 0) + public function __construct(string|int $value, private int $nrOfUnusedBits = 0) { parent::__construct($value); if (!is_numeric($nrOfUnusedBits) || $nrOfUnusedBits < 0) { throw new Exception('BitString: second parameter needs to be a positive number (or zero)!'); } - - $this->nrOfUnusedBits = $nrOfUnusedBits; } - public function getType() + public function getType(): int { return Identifier::BITSTRING; } - protected function calculateContentLength() + protected function calculateContentLength(): int { // add one to the length for the first octet which encodes the number of unused bits in the last octet return parent::calculateContentLength() + 1; } - protected function getEncodedValue() + protected function getEncodedValue(): ?string { // the first octet determines the number of unused bits $nrOfUnusedBitsOctet = chr($this->nrOfUnusedBits); @@ -58,12 +55,12 @@ protected function getEncodedValue() return $nrOfUnusedBitsOctet.$actualContent; } - public function getNumberOfUnusedBits() + public function getNumberOfUnusedBits(): int { return $this->nrOfUnusedBits; } - public static function fromBinary(&$binaryData, &$offsetIndex = 0) + public static function fromBinary(string &$binaryData, ?int &$offsetIndex = 0): static { self::parseIdentifier($binaryData[$offsetIndex], Identifier::BITSTRING, $offsetIndex++); $contentLength = self::parseContentLength($binaryData, $offsetIndex, 2); diff --git a/lib/ASN1/Universal/Boolean.php b/lib/ASN1/Universal/Boolean.php index b73c99f..2491a8f 100644 --- a/lib/ASN1/Universal/Boolean.php +++ b/lib/ASN1/Universal/Boolean.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1\Universal; @@ -17,45 +18,39 @@ class Boolean extends ASNObject implements Parsable { - private $value; - - /** - * @param bool $value - */ - public function __construct($value) + public function __construct(private bool $value) { - $this->value = $value; } - public function getType() + public function getType(): int { return Identifier::BOOLEAN; } - protected function calculateContentLength() + protected function calculateContentLength(): int { return 1; } - protected function getEncodedValue() + protected function getEncodedValue(): string { - if ($this->value == false) { + if ($this->value === false) { return chr(0x00); } else { return chr(0xFF); } } - public function getContent() + public function getContent(): string { - if ($this->value == true) { + if ($this->value === true) { return 'TRUE'; } else { return 'FALSE'; } } - public static function fromBinary(&$binaryData, &$offsetIndex = 0) + public static function fromBinary(string &$binaryData, ?int &$offsetIndex = 0): static { self::parseIdentifier($binaryData[$offsetIndex], Identifier::BOOLEAN, $offsetIndex++); $contentLength = self::parseContentLength($binaryData, $offsetIndex); diff --git a/lib/ASN1/Universal/CharacterString.php b/lib/ASN1/Universal/CharacterString.php index bfc170d..f67ddc1 100644 --- a/lib/ASN1/Universal/CharacterString.php +++ b/lib/ASN1/Universal/CharacterString.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1\Universal; @@ -15,13 +16,13 @@ class CharacterString extends AbstractString { - public function __construct($string) + public function __construct(string $string) { - $this->value = $string; + parent::__construct($string); $this->allowAll(); } - public function getType() + public function getType(): int { return Identifier::CHARACTER_STRING; } diff --git a/lib/ASN1/Universal/Enumerated.php b/lib/ASN1/Universal/Enumerated.php index 06d04a3..d7310cb 100644 --- a/lib/ASN1/Universal/Enumerated.php +++ b/lib/ASN1/Universal/Enumerated.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1\Universal; @@ -14,7 +15,7 @@ class Enumerated extends Integer { - public function getType() + public function getType(): int { return Identifier::ENUMERATED; } diff --git a/lib/ASN1/Universal/GeneralString.php b/lib/ASN1/Universal/GeneralString.php index fb0346f..60fe0b0 100644 --- a/lib/ASN1/Universal/GeneralString.php +++ b/lib/ASN1/Universal/GeneralString.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1\Universal; @@ -18,16 +19,14 @@ class GeneralString extends AbstractString /** * Creates a new ASN.1 GeneralString. * TODO The encodable characters of this type are not yet checked. - * - * @param string $string */ - public function __construct($string) + public function __construct(string $string) { - $this->value = $string; + parent::__construct($string); $this->allowAll(); } - public function getType() + public function getType(): int { return Identifier::GENERAL_STRING; } diff --git a/lib/ASN1/Universal/GeneralizedTime.php b/lib/ASN1/Universal/GeneralizedTime.php index ca92209..e0332fc 100644 --- a/lib/ASN1/Universal/GeneralizedTime.php +++ b/lib/ASN1/Universal/GeneralizedTime.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1\Universal; @@ -40,12 +41,12 @@ public function __construct($dateTime = null, $dateTimeZone = 'UTC') } } - public function getType() + public function getType(): int { return Identifier::GENERALIZED_TIME; } - protected function calculateContentLength() + protected function calculateContentLength(): int { $contentSize = 15; // YYYYMMDDHHmmSSZ @@ -56,12 +57,12 @@ protected function calculateContentLength() return $contentSize; } - public function containsFractionalSecondsElement() + public function containsFractionalSecondsElement(): bool { return intval($this->microseconds) > 0; } - protected function getEncodedValue() + protected function getEncodedValue(): ?string { $encodedContent = $this->value->format('YmdHis'); if ($this->containsFractionalSecondsElement()) { @@ -71,7 +72,7 @@ protected function getEncodedValue() return $encodedContent.'Z'; } - public function __toString() + public function __toString(): string { if ($this->containsFractionalSecondsElement()) { return $this->value->format("Y-m-d\tH:i:s.uP"); @@ -80,7 +81,7 @@ public function __toString() } } - public static function fromBinary(&$binaryData, &$offsetIndex = 0) + public static function fromBinary(string &$binaryData, ?int &$offsetIndex = 0): static { self::parseIdentifier($binaryData[$offsetIndex], Identifier::GENERALIZED_TIME, $offsetIndex++); $lengthOfMinimumTimeString = 14; // YYYYMMDDHHmmSS diff --git a/lib/ASN1/Universal/GraphicString.php b/lib/ASN1/Universal/GraphicString.php index 4a01d67..bff6405 100644 --- a/lib/ASN1/Universal/GraphicString.php +++ b/lib/ASN1/Universal/GraphicString.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1\Universal; @@ -18,16 +19,14 @@ class GraphicString extends AbstractString /** * Creates a new ASN.1 Graphic String. * TODO The encodable characters of this type are not yet checked. - * - * @param string $string */ - public function __construct($string) + public function __construct(string $string) { - $this->value = $string; + parent::__construct($string); $this->allowAll(); } - public function getType() + public function getType(): int { return Identifier::GRAPHIC_STRING; } diff --git a/lib/ASN1/Universal/IA5String.php b/lib/ASN1/Universal/IA5String.php index 33a8067..7d8982b 100644 --- a/lib/ASN1/Universal/IA5String.php +++ b/lib/ASN1/Universal/IA5String.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1\Universal; @@ -28,7 +29,7 @@ public function __construct($string) } } - public function getType() + public function getType(): int { return Identifier::IA5_STRING; } diff --git a/lib/ASN1/Universal/Integer.php b/lib/ASN1/Universal/Integer.php index fe3806b..93006a2 100644 --- a/lib/ASN1/Universal/Integer.php +++ b/lib/ASN1/Universal/Integer.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1\Universal; @@ -19,38 +20,32 @@ class Integer extends ASNObject implements Parsable { - /** @var int */ - private $value; - /** - * @param int $value - * * @throws Exception if the value is not numeric */ - public function __construct($value) + public function __construct(private string|int $value) { if (is_numeric($value) == false) { throw new Exception("Invalid VALUE [{$value}] for ASN1_INTEGER"); } - $this->value = $value; } - public function getType() + public function getType(): int { return Identifier::INTEGER; } - public function getContent() + public function getContent(): string|int { return $this->value; } - protected function calculateContentLength() + protected function calculateContentLength(): int { return strlen($this->getEncodedValue()); } - protected function getEncodedValue() + protected function getEncodedValue(): ?string { $value = BigInteger::create($this->value, 10); $negative = $value->compare(0) < 0; @@ -92,7 +87,7 @@ protected function getEncodedValue() return $r; } - private static function ensureMinimalEncoding($binaryData, $offsetIndex) + private static function ensureMinimalEncoding($binaryData, $offsetIndex): void { // All the first nine bits cannot equal 0 or 1, which would // be non-minimal encoding for positive and negative integers respectively @@ -102,7 +97,7 @@ private static function ensureMinimalEncoding($binaryData, $offsetIndex) } } - public static function fromBinary(&$binaryData, &$offsetIndex = 0) + public static function fromBinary(string &$binaryData, ?int &$offsetIndex = 0): static { $parsedObject = new static(0); self::parseIdentifier($binaryData[$offsetIndex], $parsedObject->getType(), $offsetIndex++); diff --git a/lib/ASN1/Universal/NullObject.php b/lib/ASN1/Universal/NullObject.php index b5293e4..ec25c5b 100644 --- a/lib/ASN1/Universal/NullObject.php +++ b/lib/ASN1/Universal/NullObject.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1\Universal; @@ -17,27 +18,27 @@ class NullObject extends ASNObject implements Parsable { - public function getType() + public function getType(): int { return Identifier::NULL; } - protected function calculateContentLength() + protected function calculateContentLength(): int { return 0; } - protected function getEncodedValue() + protected function getEncodedValue(): ?string { return null; } - public function getContent() + public function getContent(): string { return 'NULL'; } - public static function fromBinary(&$binaryData, &$offsetIndex = 0) + public static function fromBinary(string &$binaryData, ?int &$offsetIndex = 0): static { self::parseIdentifier($binaryData[$offsetIndex], Identifier::NULL, $offsetIndex++); $contentLength = self::parseContentLength($binaryData, $offsetIndex); diff --git a/lib/ASN1/Universal/NumericString.php b/lib/ASN1/Universal/NumericString.php index 13fb7c3..8a7b50b 100644 --- a/lib/ASN1/Universal/NumericString.php +++ b/lib/ASN1/Universal/NumericString.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1\Universal; @@ -21,17 +22,15 @@ class NumericString extends AbstractString * The following characters are permitted: * Digits 0,1, ... 9 * SPACE (space) - * - * @param string $string */ - public function __construct($string) + public function __construct(string $string) { - $this->value = $string; + parent::__construct($string); $this->allowNumbers(); $this->allowSpaces(); } - public function getType() + public function getType(): int { return Identifier::NUMERIC_STRING; } diff --git a/lib/ASN1/Universal/ObjectDescriptor.php b/lib/ASN1/Universal/ObjectDescriptor.php index 1c5d349..f2ab925 100644 --- a/lib/ASN1/Universal/ObjectDescriptor.php +++ b/lib/ASN1/Universal/ObjectDescriptor.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1\Universal; @@ -14,12 +15,12 @@ class ObjectDescriptor extends GraphicString { - public function __construct($objectDescription) + public function __construct(string $objectDescription) { parent::__construct($objectDescription); } - public function getType() + public function getType(): int { return Identifier::OBJECT_DESCRIPTOR; } diff --git a/lib/ASN1/Universal/ObjectIdentifier.php b/lib/ASN1/Universal/ObjectIdentifier.php index 150ce9c..9b635cc 100644 --- a/lib/ASN1/Universal/ObjectIdentifier.php +++ b/lib/ASN1/Universal/ObjectIdentifier.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1\Universal; @@ -20,8 +21,8 @@ class ObjectIdentifier extends ASNObject implements Parsable { - protected $subIdentifiers; - protected $value; + protected array $subIdentifiers; + protected string $value; public function __construct($value) { @@ -46,17 +47,17 @@ public function __construct($value) $this->value = $value; } - public function getContent() + public function getContent(): string { return $this->value; } - public function getType() + public function getType(): int { return Identifier::OBJECT_IDENTIFIER; } - protected function calculateContentLength() + protected function calculateContentLength(): int { $length = 0; foreach ($this->subIdentifiers as $subIdentifier) { @@ -69,7 +70,7 @@ protected function calculateContentLength() return $length; } - protected function getEncodedValue() + protected function getEncodedValue(): ?string { $encodedValue = ''; foreach ($this->subIdentifiers as $subIdentifier) { @@ -79,12 +80,12 @@ protected function getEncodedValue() return $encodedValue; } - public function __toString() + public function __toString(): string { return OID::getName($this->value); } - public static function fromBinary(&$binaryData, &$offsetIndex = 0) + public static function fromBinary(string &$binaryData, ?int &$offsetIndex = 0): static { self::parseIdentifier($binaryData[$offsetIndex], Identifier::OBJECT_IDENTIFIER, $offsetIndex++); $contentLength = self::parseContentLength($binaryData, $offsetIndex, 1); @@ -104,15 +105,9 @@ public static function fromBinary(&$binaryData, &$offsetIndex = 0) * differently. This way relative object identifiers can also be parsed * using this. * - * @param $binaryData - * @param $offsetIndex - * @param $octetsToRead - * * @throws ParserException - * - * @return string */ - protected static function parseOid(&$binaryData, &$offsetIndex, $octetsToRead) + protected static function parseOid(string &$binaryData, int &$offsetIndex, int $octetsToRead): string { $oid = ''; diff --git a/lib/ASN1/Universal/OctetString.php b/lib/ASN1/Universal/OctetString.php index e12e722..40594d3 100644 --- a/lib/ASN1/Universal/OctetString.php +++ b/lib/ASN1/Universal/OctetString.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1\Universal; @@ -17,7 +18,7 @@ class OctetString extends ASNObject implements Parsable { - protected $value; + protected ?string $value = null; public function __construct($value) { @@ -40,12 +41,12 @@ public function __construct($value) $this->value = $value; } - public function getType() + public function getType(): int { return Identifier::OCTETSTRING; } - protected function calculateContentLength() + protected function calculateContentLength(): int { if (is_null($this->value)) { return 0; @@ -53,7 +54,7 @@ protected function calculateContentLength() return strlen($this->value) / 2; } - protected function getEncodedValue() + protected function getEncodedValue(): ?string { $value = $this->value; if (is_null($value)) { @@ -72,7 +73,7 @@ protected function getEncodedValue() return $result; } - public function getContent() + public function getContent(): string { if (is_null($this->value)) { return ''; @@ -80,12 +81,12 @@ public function getContent() return strtoupper($this->value); } - public function getBinaryContent() + public function getBinaryContent(): string { return $this->getEncodedValue(); } - public static function fromBinary(&$binaryData, &$offsetIndex = 0) + public static function fromBinary(string &$binaryData, ?int &$offsetIndex = 0): static { self::parseIdentifier($binaryData[$offsetIndex], Identifier::OCTETSTRING, $offsetIndex++); $contentLength = self::parseContentLength($binaryData, $offsetIndex); diff --git a/lib/ASN1/Universal/PrintableString.php b/lib/ASN1/Universal/PrintableString.php index fe6d4bc..a941073 100644 --- a/lib/ASN1/Universal/PrintableString.php +++ b/lib/ASN1/Universal/PrintableString.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1\Universal; @@ -34,19 +35,17 @@ class PrintableString extends AbstractString * COLON : * EQUALS SIGN = * QUESTION MARK ? - * - * @param string $string */ - public function __construct($string) + public function __construct(string $string) { - $this->value = $string; + parent::__construct($string); $this->allowNumbers(); $this->allowAllLetters(); $this->allowSpaces(); $this->allowCharacters("'", '(', ')', '+', '-', '.', ',', '/', ':', '=', '?'); } - public function getType() + public function getType(): int { return Identifier::PRINTABLE_STRING; } diff --git a/lib/ASN1/Universal/RelativeObjectIdentifier.php b/lib/ASN1/Universal/RelativeObjectIdentifier.php index 2aa9643..8185074 100644 --- a/lib/ASN1/Universal/RelativeObjectIdentifier.php +++ b/lib/ASN1/Universal/RelativeObjectIdentifier.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1\Universal; @@ -33,12 +34,12 @@ public function __construct($subIdentifiers) } } - public function getType() + public function getType():int { return Identifier::RELATIVE_OID; } - public static function fromBinary(&$binaryData, &$offsetIndex = 0) + public static function fromBinary(string &$binaryData, ?int &$offsetIndex = 0): static { self::parseIdentifier($binaryData[$offsetIndex], Identifier::RELATIVE_OID, $offsetIndex++); $contentLength = self::parseContentLength($binaryData, $offsetIndex, 1); diff --git a/lib/ASN1/Universal/Sequence.php b/lib/ASN1/Universal/Sequence.php index 0397cf1..c4ad3b4 100644 --- a/lib/ASN1/Universal/Sequence.php +++ b/lib/ASN1/Universal/Sequence.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1\Universal; @@ -16,7 +17,7 @@ class Sequence extends Construct implements Parsable { - public function getType() + public function getType(): int { return Identifier::SEQUENCE; } diff --git a/lib/ASN1/Universal/Set.php b/lib/ASN1/Universal/Set.php index 6e6d346..6d7d332 100644 --- a/lib/ASN1/Universal/Set.php +++ b/lib/ASN1/Universal/Set.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1\Universal; @@ -14,7 +15,7 @@ class Set extends Sequence { - public function getType() + public function getType(): int { return Identifier::SET; } diff --git a/lib/ASN1/Universal/T61String.php b/lib/ASN1/Universal/T61String.php index 5641864..588de8c 100644 --- a/lib/ASN1/Universal/T61String.php +++ b/lib/ASN1/Universal/T61String.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1\Universal; @@ -20,16 +21,14 @@ class T61String extends AbstractString * TODO The encodable characters of this type are not yet checked. * * @see http://en.wikipedia.org/wiki/ITU_T.61 - * - * @param string $string */ - public function __construct($string) + public function __construct(string $string) { - $this->value = $string; + parent::__construct($string); $this->allowAll(); } - public function getType() + public function getType(): int { return Identifier::T61_STRING; } diff --git a/lib/ASN1/Universal/UTCTime.php b/lib/ASN1/Universal/UTCTime.php index c4d303c..397316a 100644 --- a/lib/ASN1/Universal/UTCTime.php +++ b/lib/ASN1/Universal/UTCTime.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1\Universal; @@ -26,22 +27,22 @@ */ class UTCTime extends AbstractTime implements Parsable { - public function getType() + public function getType(): int { return Identifier::UTC_TIME; } - protected function calculateContentLength() + protected function calculateContentLength(): int { return 13; // Content is a string o the following format: YYMMDDhhmmssZ (13 octets) } - protected function getEncodedValue() + protected function getEncodedValue(): ?string { return $this->value->format('ymdHis').'Z'; } - public static function fromBinary(&$binaryData, &$offsetIndex = 0) + public static function fromBinary(string &$binaryData, ?int &$offsetIndex = 0): static { self::parseIdentifier($binaryData[$offsetIndex], Identifier::UTC_TIME, $offsetIndex++); $contentLength = self::parseContentLength($binaryData, $offsetIndex, 11); diff --git a/lib/ASN1/Universal/UTF8String.php b/lib/ASN1/Universal/UTF8String.php index cba568d..b787ba6 100644 --- a/lib/ASN1/Universal/UTF8String.php +++ b/lib/ASN1/Universal/UTF8String.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1\Universal; @@ -18,16 +19,14 @@ class UTF8String extends AbstractString /** * Creates a new ASN.1 Universal String. * TODO The encodable characters of this type are not yet checked. - * - * @param string $string */ - public function __construct($string) + public function __construct(string $string) { - $this->value = $string; + parent::__construct($string); $this->allowAll(); } - public function getType() + public function getType(): int { return Identifier::UTF8_STRING; } diff --git a/lib/ASN1/Universal/UniversalString.php b/lib/ASN1/Universal/UniversalString.php index 0c3fe1d..10f6ae6 100644 --- a/lib/ASN1/Universal/UniversalString.php +++ b/lib/ASN1/Universal/UniversalString.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1\Universal; @@ -20,16 +21,14 @@ class UniversalString extends AbstractString * TODO The encodable characters of this type are not yet checked. * * @see http://en.wikipedia.org/wiki/Universal_Character_Set - * - * @param string $string */ - public function __construct($string) + public function __construct(string $string) { - $this->value = $string; + parent::__construct($string); $this->allowAll(); } - public function getType() + public function getType(): int { return Identifier::UNIVERSAL_STRING; } diff --git a/lib/ASN1/Universal/VisibleString.php b/lib/ASN1/Universal/VisibleString.php index d9326d3..38fa4ad 100644 --- a/lib/ASN1/Universal/VisibleString.php +++ b/lib/ASN1/Universal/VisibleString.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1\Universal; @@ -18,16 +19,14 @@ class VisibleString extends AbstractString /** * Creates a new ASN.1 Visible String. * TODO The encodable characters of this type are not yet checked. - * - * @param string $string */ - public function __construct($string) + public function __construct(string $string) { - $this->value = $string; + parent::__construct($string); $this->allowAll(); } - public function getType() + public function getType(): int { return Identifier::VISIBLE_STRING; } diff --git a/lib/ASN1/UnknownConstructedObject.php b/lib/ASN1/UnknownConstructedObject.php index b19a07a..9535d1c 100644 --- a/lib/ASN1/UnknownConstructedObject.php +++ b/lib/ASN1/UnknownConstructedObject.php @@ -7,13 +7,14 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1; class UnknownConstructedObject extends Construct { - private $identifier; - private $contentLength; + private string $identifier; + private int $contentLength; /** * @param string $binaryData @@ -21,7 +22,7 @@ class UnknownConstructedObject extends Construct * * @throws \FG\ASN1\Exception\ParserException */ - public function __construct($binaryData, &$offsetIndex) + public function __construct(string $binaryData, int &$offsetIndex) { $this->identifier = self::parseBinaryIdentifier($binaryData, $offsetIndex); $this->contentLength = self::parseContentLength($binaryData, $offsetIndex); @@ -37,22 +38,22 @@ public function __construct($binaryData, &$offsetIndex) parent::__construct(...$children); } - public function getType() + public function getType(): int { return ord($this->identifier); } - public function getIdentifier() + public function getIdentifier(): string { return $this->identifier; } - protected function calculateContentLength() + protected function calculateContentLength(): int { return $this->contentLength; } - protected function getEncodedValue() + protected function getEncodedValue(): ?string { return ''; } diff --git a/lib/ASN1/UnknownObject.php b/lib/ASN1/UnknownObject.php index 4ac536a..2ebde31 100644 --- a/lib/ASN1/UnknownObject.php +++ b/lib/ASN1/UnknownObject.php @@ -7,21 +7,19 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\ASN1; class UnknownObject extends ASNObject { - /** @var string */ - private $value; - - private $identifier; + private string $value; + private string $identifier; /** * @param string|int $identifier Either the first identifier octet as int or all identifier bytes as a string - * @param int $contentLength */ - public function __construct($identifier, $contentLength) + public function __construct(string|int $identifier, int $contentLength) { if (is_int($identifier)) { $identifier = chr($identifier); @@ -32,27 +30,27 @@ public function __construct($identifier, $contentLength) $this->setContentLength($contentLength); } - public function getContent() + public function getContent(): string { return $this->value; } - public function getType() + public function getType(): int { return ord($this->identifier[0]); } - public function getIdentifier() + public function getIdentifier(): string { return $this->identifier; } - protected function calculateContentLength() + protected function calculateContentLength(): int { return $this->getContentLength(); } - protected function getEncodedValue() + protected function getEncodedValue(): ?string { return ''; } diff --git a/lib/Utility/BigInteger.php b/lib/Utility/BigInteger.php index 866162c..dafa70b 100644 --- a/lib/Utility/BigInteger.php +++ b/lib/Utility/BigInteger.php @@ -5,6 +5,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\Utility; @@ -22,34 +23,26 @@ abstract class BigInteger { /** * Force a preference on the underlying big number implementation, useful for testing. - * @var string|null */ - private static $_prefer; + private static string|int|null $_prefer = null; - public static function setPrefer($prefer = null) + public static function setPrefer(string|int $prefer = null) { self::$_prefer = $prefer; } /** * Create a BigInteger instance based off the base 10 string or an integer. - * @param string|int $val - * @return BigInteger * @throws \InvalidArgumentException */ - public static function create($val) + public static function create(string|int $val): BigInteger { if (self::$_prefer) { - switch (self::$_prefer) { - case 'gmp': - $ret = new BigIntegerGmp(); - break; - case 'bcmath': - $ret = new BigIntegerBcmath(); - break; - default: - throw new \UnexpectedValueException('Unknown number implementation: ' . self::$_prefer); - } + $ret = match (self::$_prefer) { + 'gmp' => new BigIntegerGmp(), + 'bcmath' => new BigIntegerBcmath(), + default => throw new \UnexpectedValueException('Unknown number implementation: ' . self::$_prefer), + }; } else { // autodetect @@ -91,105 +84,80 @@ protected function __construct() /** * Subclasses must provide clone functionality. - * @return BigInteger */ - abstract public function __clone(); + abstract public function __clone(): void; /** * Assign the instance value from base 10 string. - * @param string $str */ - abstract protected function _fromString($str); + abstract protected function _fromString(string $str): void; /** * Assign the instance value from an integer type. - * @param int $integer */ - abstract protected function _fromInteger($integer); + abstract protected function _fromInteger(int $integer): void; /** * Must provide string implementation that returns base 10 number. - * @return string */ - abstract public function __toString(); + abstract public function __toString(): string; /* INFORMATIONAL FUNCTIONS */ /** * Return integer, if possible. Throws an exception if the number can not be represented as a native integer. - * @return int * @throws \OverflowException */ - abstract public function toInteger(); + abstract public function toInteger(): int; /** * Is represented integer negative? - * @return bool */ - abstract public function isNegative(); + abstract public function isNegative(): bool; /** * Compare the integer with $number, returns a negative integer if $this is less than number, returns 0 if $this is * equal to number and returns a positive integer if $this is greater than number. - * @param BigInteger|string|int $number - * @return int */ - abstract public function compare($number); - - /* MODIFY */ + abstract public function compare(BigInteger|string|int $number): int; /** * Add another integer $b and returns the result. - * @param BigInteger|string|int $b - * @return BigInteger */ - abstract public function add($b); + abstract public function add(BigInteger|string|int $b): BigInteger; /** * Subtract $b from $this and returns the result. - * @param BigInteger|string|int $b - * @return BigInteger */ - abstract public function subtract($b); + abstract public function subtract(BigInteger|string|int $b): BigInteger; /** * Multiply value. - * @param BigInteger|string|int $b - * @return BigInteger */ - abstract public function multiply($b); + abstract public function multiply(BigInteger|string|int $b): BigInteger; /** * The value $this modulus $b. - * @param BigInteger|string|int $b - * @return BigInteger */ - abstract public function modulus($b); + abstract public function modulus(BigInteger|string|int $b): BigInteger; /** * Raise $this to the power of $b and returns the result. - * @param BigInteger|string|int $b - * @return BigInteger */ - abstract public function toPower($b); + abstract public function toPower(BigInteger|string|int $b): BigInteger; /** * Shift the value to the right by a set number of bits and returns the result. - * @param int $bits - * @return BigInteger */ - abstract public function shiftRight($bits = 8); + abstract public function shiftRight(int $bits = 8): BigInteger; /** * Shift the value to the left by a set number of bits and returns the result. - * @param int $bits - * @return BigInteger */ - abstract public function shiftLeft($bits = 8); + abstract public function shiftLeft(int $bits = 8): BigInteger; /** * Returns the absolute value. - * @return BigInteger */ - abstract public function absoluteValue(); + abstract public function absoluteValue(): BigInteger; } diff --git a/lib/Utility/BigIntegerBcmath.php b/lib/Utility/BigIntegerBcmath.php index 25ad891..866a6cc 100644 --- a/lib/Utility/BigIntegerBcmath.php +++ b/lib/Utility/BigIntegerBcmath.php @@ -5,6 +5,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\Utility; @@ -16,29 +17,28 @@ */ class BigIntegerBcmath extends BigInteger { - protected $_str; + protected string $_str; - public function __clone() + public function __clone(): void { - // nothing needed to copy } - protected function _fromString($str) + protected function _fromString(string $str): void { - $this->_str = (string)$str; + $this->_str = $str; } - protected function _fromInteger($integer) + protected function _fromInteger(int $integer): void { $this->_str = (string)$integer; } - public function __toString() + public function __toString(): string { return $this->_str; } - public function toInteger() + public function toInteger(): int { if ($this->compare(PHP_INT_MAX) > 0 || $this->compare(PHP_INT_MIN) < 0) { throw new \OverflowException(sprintf('Can not represent %s as integer.', $this->_str)); @@ -46,46 +46,47 @@ public function toInteger() return (int)$this->_str; } - public function isNegative() + public function isNegative(): bool { return bccomp($this->_str, '0', 0) < 0; } - protected function _unwrap($number) + protected function _unwrap(BigInteger|string|int $number): string { if ($number instanceof self) { return $number->_str; } - return $number; + + return (string)$number; } - public function compare($number) + public function compare(BigInteger|string|int $number): int { return bccomp($this->_str, $this->_unwrap($number), 0); } - public function add($b) + public function add(BigInteger|string|int $b): BigInteger { $ret = new self(); $ret->_str = bcadd($this->_str, $this->_unwrap($b), 0); return $ret; } - public function subtract($b) + public function subtract(BigInteger|string|int $b): BigInteger { $ret = new self(); $ret->_str = bcsub($this->_str, $this->_unwrap($b), 0); return $ret; } - public function multiply($b) + public function multiply(BigInteger|string|int $b): BigInteger { $ret = new self(); $ret->_str = bcmul($this->_str, $this->_unwrap($b), 0); return $ret; } - public function modulus($b) + public function modulus(BigInteger|string|int $b): BigInteger { $ret = new self(); if ($this->isNegative()) { @@ -99,27 +100,28 @@ public function modulus($b) return $ret; } - public function toPower($b) + public function toPower(BigInteger|string|int $b): BigInteger { $ret = new self(); $ret->_str = bcpow($this->_str, $this->_unwrap($b), 0); return $ret; } - public function shiftRight($bits = 8) + public function shiftRight(int $bits = 8): BigInteger { $ret = new self(); - $ret->_str = bcdiv($this->_str, bcpow('2', $bits)); + $ret->_str = bcdiv($this->_str, bcpow('2', (string)$bits)); return $ret; } - public function shiftLeft($bits = 8) { + public function shiftLeft(int $bits = 8): BigInteger + { $ret = new self(); - $ret->_str = bcmul($this->_str, bcpow('2', $bits)); + $ret->_str = bcmul($this->_str, bcpow('2', (string)$bits)); return $ret; } - public function absoluteValue() + public function absoluteValue(): BigInteger { $ret = new self(); if (-1 === bccomp($this->_str, '0', 0)) { diff --git a/lib/Utility/BigIntegerGmp.php b/lib/Utility/BigIntegerGmp.php index 0791226..9304269 100644 --- a/lib/Utility/BigIntegerGmp.php +++ b/lib/Utility/BigIntegerGmp.php @@ -5,9 +5,12 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\Utility; +use GMP; + /** * Class BigIntegerGmp * Integer representation of big numbers using the GMP extension to perform operations. @@ -16,33 +19,29 @@ */ class BigIntegerGmp extends BigInteger { - /** - * Resource handle. - * @var \GMP - */ - protected $_rh; + protected GMP $_rh; - public function __clone() + public function __clone(): void { $this->_rh = gmp_add($this->_rh, 0); } - protected function _fromString($str) + protected function _fromString($str): void { $this->_rh = gmp_init($str, 10); } - protected function _fromInteger($integer) + protected function _fromInteger($integer): void { $this->_rh = gmp_init($integer, 10); } - public function __toString() + public function __toString(): string { return gmp_strval($this->_rh, 10); } - public function toInteger() + public function toInteger(): int { if ($this->compare(PHP_INT_MAX) > 0 || $this->compare(PHP_INT_MIN) < 0) { throw new \OverflowException(sprintf('Can not represent %s as integer.', $this)); @@ -50,12 +49,12 @@ public function toInteger() return gmp_intval($this->_rh); } - public function isNegative() + public function isNegative(): bool { return gmp_sign($this->_rh) === -1; } - protected function _unwrap($number) + protected function _unwrap(BigInteger|string|int $number): BigInteger|GMP|string|int { if ($number instanceof self) { return $number->_rh; @@ -63,40 +62,40 @@ protected function _unwrap($number) return $number; } - public function compare($number) + public function compare(BigInteger|string|int $number): int { return gmp_cmp($this->_rh, $this->_unwrap($number)); } - public function add($b) + public function add(BigInteger|string|int $b): BigInteger { $ret = new self(); $ret->_rh = gmp_add($this->_rh, $this->_unwrap($b)); return $ret; } - public function subtract($b) + public function subtract(BigInteger|string|int $b): BigInteger { $ret = new self(); $ret->_rh = gmp_sub($this->_rh, $this->_unwrap($b)); return $ret; } - public function multiply($b) + public function multiply(BigInteger|string|int $b): BigInteger { $ret = new self(); $ret->_rh = gmp_mul($this->_rh, $this->_unwrap($b)); return $ret; } - public function modulus($b) + public function modulus(BigInteger|string|int $b): BigInteger { $ret = new self(); $ret->_rh = gmp_mod($this->_rh, $this->_unwrap($b)); return $ret; } - public function toPower($b) + public function toPower(BigInteger|string|int $b): BigInteger { if ($b instanceof self) { // gmp_pow accepts just an integer @@ -110,21 +109,21 @@ public function toPower($b) return $ret; } - public function shiftRight($bits=8) + public function shiftRight(int $bits = 8): BigInteger { $ret = new self(); $ret->_rh = gmp_div($this->_rh, gmp_pow(2, $bits)); return $ret; } - public function shiftLeft($bits=8) + public function shiftLeft(int $bits = 8): BigInteger { $ret = new self(); $ret->_rh = gmp_mul($this->_rh, gmp_pow(2, $bits)); return $ret; } - public function absoluteValue() + public function absoluteValue(): BigInteger { $ret = new self(); $ret->_rh = gmp_abs($this->_rh); diff --git a/lib/X509/AlgorithmIdentifier.php b/lib/X509/AlgorithmIdentifier.php index a06b56f..fdc77d7 100644 --- a/lib/X509/AlgorithmIdentifier.php +++ b/lib/X509/AlgorithmIdentifier.php @@ -7,15 +7,17 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\X509; use FG\ASN1\Universal\NullObject; use FG\ASN1\Composite\AttributeTypeAndValue; +use FG\ASN1\Universal\ObjectIdentifier; class AlgorithmIdentifier extends AttributeTypeAndValue { - public function __construct($objectIdentifierString) + public function __construct(ObjectIdentifier|string $objectIdentifierString) { parent::__construct($objectIdentifierString, new NullObject()); } diff --git a/lib/X509/CSR/Attributes.php b/lib/X509/CSR/Attributes.php index 5a965e2..5a13525 100644 --- a/lib/X509/CSR/Attributes.php +++ b/lib/X509/CSR/Attributes.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\X509\CSR; @@ -22,12 +23,12 @@ class Attributes extends Construct implements Parsable { - public function getType() + public function getType(): int { return 0xA0; } - public function addAttribute($objectIdentifier, Set $attribute) + public function addAttribute($objectIdentifier, Set $attribute): void { if (is_string($objectIdentifier)) { $objectIdentifier = new ObjectIdentifier($objectIdentifier); @@ -37,7 +38,7 @@ public function addAttribute($objectIdentifier, Set $attribute) $this->addChild($attributeSequence); } - public static function fromBinary(&$binaryData, &$offsetIndex = 0) + public static function fromBinary(string &$binaryData, ?int &$offsetIndex = 0): static { self::parseIdentifier($binaryData[$offsetIndex], 0xA0, $offsetIndex++); $contentLength = self::parseContentLength($binaryData, $offsetIndex); diff --git a/lib/X509/CSR/CSR.php b/lib/X509/CSR/CSR.php index 8f2a319..abc5078 100644 --- a/lib/X509/CSR/CSR.php +++ b/lib/X509/CSR/CSR.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\X509\CSR; @@ -21,28 +22,20 @@ class CSR extends Sequence { const CSR_VERSION_NR = 0; - - protected $subject; - protected $publicKey; - protected $signature; - protected $signatureAlgorithm; - - protected $startSequence; - - /** - * @param string $commonName - * @param string $email - * @param string $organization - * @param string $locality - * @param string $state - * @param string $country - * @param string $organizationalUnit - * @param string $publicKey - * @param string $signature - * @param string $signatureAlgorithm - */ - public function __construct($commonName, $email, $organization, $locality, $state, $country, $organizationalUnit, $publicKey, $signature = null, $signatureAlgorithm = OID::SHA1_WITH_RSA_SIGNATURE) - { + private CertificateSubject $subject; + + public function __construct( + string $commonName, + string $email, + string $organization, + string $locality, + string $state, + string $country, + string $organizationalUnit, + private string $publicKey, + private ?string $signature = null, + private string $signatureAlgorithm = OID::SHA1_WITH_RSA_SIGNATURE + ) { $this->subject = new CertificateSubject( $commonName, $email, @@ -52,16 +45,15 @@ public function __construct($commonName, $email, $organization, $locality, $stat $country, $organizationalUnit ); - $this->publicKey = $publicKey; - $this->signature = $signature; - $this->signatureAlgorithm = $signatureAlgorithm; - if (isset($signature)) { + if ($signature !== null) { $this->createCSRSequence(); } + + parent::__construct(); } - protected function createCSRSequence() + protected function createCSRSequence(): void { $versionNr = new Integer(self::CSR_VERSION_NR); $publicKey = new PublicKey($this->publicKey); @@ -78,7 +70,7 @@ protected function createCSRSequence() $this->addChild($signature); } - public function getSignatureSubject() + public function getSignatureSubject(): string { $versionNr = new Integer(self::CSR_VERSION_NR); $publicKey = new PublicKey($this->publicKey); @@ -87,7 +79,7 @@ public function getSignatureSubject() return $certRequestInfo->getBinary(); } - public function setSignature($signature, $signatureAlgorithm = OID::SHA1_WITH_RSA_SIGNATURE) + public function setSignature($signature, $signatureAlgorithm = OID::SHA1_WITH_RSA_SIGNATURE): void { $this->signature = $signature; $this->signatureAlgorithm = $signatureAlgorithm; @@ -95,7 +87,7 @@ public function setSignature($signature, $signatureAlgorithm = OID::SHA1_WITH_RS $this->createCSRSequence(); } - public function __toString() + public function __toString(): string { $tmp = base64_encode($this->getBinary()); @@ -112,47 +104,47 @@ public function __toString() return $result; } - public function getVersion() + public function getVersion(): int { return self::CSR_VERSION_NR; } - public function getOrganizationName() + public function getOrganizationName(): string { return $this->subject->getOrganization(); } - public function getLocalName() + public function getLocalName(): string { return $this->subject->getLocality(); } - public function getState() + public function getState(): string { return $this->subject->getState(); } - public function getCountry() + public function getCountry(): string { return $this->subject->getCountry(); } - public function getOrganizationalUnit() + public function getOrganizationalUnit(): string { return $this->subject->getOrganizationalUnit(); } - public function getPublicKey() + public function getPublicKey(): string { return $this->publicKey; } - public function getSignature() + public function getSignature(): string { return $this->signature; } - public function getSignatureAlgorithm() + public function getSignatureAlgorithm(): string { return $this->signatureAlgorithm; } diff --git a/lib/X509/CertificateExtensions.php b/lib/X509/CertificateExtensions.php index 6ed1c6a..2a1220f 100644 --- a/lib/X509/CertificateExtensions.php +++ b/lib/X509/CertificateExtensions.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\X509; @@ -23,8 +24,8 @@ class CertificateExtensions extends Set implements Parsable { - private $innerSequence; - private $extensions = []; + private Sequence $innerSequence; + private array $extensions = []; public function __construct() { @@ -32,12 +33,12 @@ public function __construct() parent::__construct($this->innerSequence); } - public function addSubjectAlternativeNames(SubjectAlternativeNames $sans) + public function addSubjectAlternativeNames(SubjectAlternativeNames $sans): void { $this->addExtension(OID::CERT_EXT_SUBJECT_ALT_NAME, $sans); } - private function addExtension($oidString, ASNObject $extension) + private function addExtension(string $oidString, ASNObject $extension): void { $sequence = new Sequence(); $sequence->addChild(new ObjectIdentifier($oidString)); @@ -47,12 +48,12 @@ private function addExtension($oidString, ASNObject $extension) $this->extensions[] = $extension; } - public function getContent() + public function getContent(): array { return $this->extensions; } - public static function fromBinary(&$binaryData, &$offsetIndex = 0) + public static function fromBinary(string &$binaryData, ?int &$offsetIndex = 0): static { self::parseIdentifier($binaryData[$offsetIndex], Identifier::SET, $offsetIndex++); self::parseContentLength($binaryData, $offsetIndex); diff --git a/lib/X509/CertificateSubject.php b/lib/X509/CertificateSubject.php index 0a04d57..958902b 100644 --- a/lib/X509/CertificateSubject.php +++ b/lib/X509/CertificateSubject.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\X509; @@ -19,25 +20,15 @@ class CertificateSubject extends Sequence implements Parsable { - private $commonName; - private $email; - private $organization; - private $locality; - private $state; - private $country; - private $organizationalUnit; - - /** - * @param string $commonName - * @param string $email - * @param string $organization - * @param string $locality - * @param string $state - * @param string $country - * @param string $organizationalUnit - */ - public function __construct($commonName, $email, $organization, $locality, $state, $country, $organizationalUnit) - { + public function __construct( + private string $commonName, + private string $email, + private string $organization, + private string $locality, + private string $state, + private string $country, + private string $organizationalUnit + ) { parent::__construct( new RDNString(OID::COUNTRY_NAME, $country), new RDNString(OID::STATE_OR_PROVINCE_NAME, $state), @@ -47,52 +38,44 @@ public function __construct($commonName, $email, $organization, $locality, $stat new RDNString(OID::COMMON_NAME, $commonName), new RDNString(OID::PKCS9_EMAIL, $email) ); - - $this->commonName = $commonName; - $this->email = $email; - $this->organization = $organization; - $this->locality = $locality; - $this->state = $state; - $this->country = $country; - $this->organizationalUnit = $organizationalUnit; } - public function getCommonName() + public function getCommonName(): string { return $this->commonName; } - public function getEmail() + public function getEmail(): string { return $this->email; } - public function getOrganization() + public function getOrganization(): string { return $this->organization; } - public function getLocality() + public function getLocality(): string { return $this->locality; } - public function getState() + public function getState(): string { return $this->state; } - public function getCountry() + public function getCountry(): string { return $this->country; } - public function getOrganizationalUnit() + public function getOrganizationalUnit(): string { return $this->organizationalUnit; } - public static function fromBinary(&$binaryData, &$offsetIndex = 0) + public static function fromBinary(string &$binaryData, ?int &$offsetIndex = 0): static { self::parseIdentifier($binaryData[$offsetIndex], Identifier::SEQUENCE, $offsetIndex++); $contentLength = self::parseContentLength($binaryData, $offsetIndex); diff --git a/lib/X509/PrivateKey.php b/lib/X509/PrivateKey.php index d57ad86..76d852a 100644 --- a/lib/X509/PrivateKey.php +++ b/lib/X509/PrivateKey.php @@ -7,9 +7,11 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\X509; +use FG\ASN1\ASNObject; use FG\ASN1\OID; use FG\ASN1\Universal\NullObject; use FG\ASN1\Universal\Sequence; @@ -18,11 +20,7 @@ class PrivateKey extends Sequence { - /** - * @param string $hexKey - * @param \FG\ASN1\ASNObject|string $algorithmIdentifierString - */ - public function __construct($hexKey, $algorithmIdentifierString = OID::RSA_ENCRYPTION) + public function __construct(string $hexKey, ASNObject|string $algorithmIdentifierString = OID::RSA_ENCRYPTION) { parent::__construct( new Sequence( diff --git a/lib/X509/PublicKey.php b/lib/X509/PublicKey.php index ab8b451..eb420d8 100644 --- a/lib/X509/PublicKey.php +++ b/lib/X509/PublicKey.php @@ -7,9 +7,11 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\X509; +use FG\ASN1\ASNObject; use FG\ASN1\OID; use FG\ASN1\Universal\NullObject; use FG\ASN1\Universal\Sequence; @@ -18,11 +20,7 @@ class PublicKey extends Sequence { - /** - * @param string $hexKey - * @param \FG\ASN1\ASNObject|string $algorithmIdentifierString - */ - public function __construct($hexKey, $algorithmIdentifierString = OID::RSA_ENCRYPTION) + public function __construct(string $hexKey, ASNObject|string $algorithmIdentifierString = OID::RSA_ENCRYPTION) { parent::__construct( new Sequence( diff --git a/lib/X509/SAN/DNSName.php b/lib/X509/SAN/DNSName.php index 502738b..f39f66f 100644 --- a/lib/X509/SAN/DNSName.php +++ b/lib/X509/SAN/DNSName.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\X509\SAN; @@ -16,12 +17,12 @@ class DNSName extends GeneralString { const IDENTIFIER = 0x82; // not sure yet why this is the identifier used in SAN extensions - public function __construct($dnsNameString) + public function __construct(string $dnsNameString) { parent::__construct($dnsNameString); } - public function getType() + public function getType(): int { return self::IDENTIFIER; } diff --git a/lib/X509/SAN/IPAddress.php b/lib/X509/SAN/IPAddress.php index f55be95..82ccca1 100644 --- a/lib/X509/SAN/IPAddress.php +++ b/lib/X509/SAN/IPAddress.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\X509\SAN; @@ -18,41 +19,37 @@ class IPAddress extends ASNObject implements Parsable { const IDENTIFIER = 0x87; // not sure yet why this is the identifier used in SAN extensions - /** @var string */ - private $value; - - public function __construct($ipAddressString) + public function __construct(private string $value) { - $this->value = $ipAddressString; } - public function getType() + public function getType(): int { return self::IDENTIFIER; } - public function getContent() + public function getContent(): string { return $this->value; } - protected function calculateContentLength() + protected function calculateContentLength(): int { return 4; } - protected function getEncodedValue() + protected function getEncodedValue(): string { $ipParts = explode('.', $this->value); - $binary = chr($ipParts[0]); - $binary .= chr($ipParts[1]); - $binary .= chr($ipParts[2]); - $binary .= chr($ipParts[3]); + $binary = chr((int)$ipParts[0]); + $binary .= chr((int)$ipParts[1]); + $binary .= chr((int)$ipParts[2]); + $binary .= chr((int)$ipParts[3]); return $binary; } - public static function fromBinary(&$binaryData, &$offsetIndex = 0) + public static function fromBinary(string &$binaryData, ?int &$offsetIndex = 0): static { self::parseIdentifier($binaryData[$offsetIndex], self::IDENTIFIER, $offsetIndex++); $contentLength = self::parseContentLength($binaryData, $offsetIndex); diff --git a/lib/X509/SAN/SubjectAlternativeNames.php b/lib/X509/SAN/SubjectAlternativeNames.php index 271ddde..b02bd96 100644 --- a/lib/X509/SAN/SubjectAlternativeNames.php +++ b/lib/X509/SAN/SubjectAlternativeNames.php @@ -7,6 +7,7 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +declare(strict_types=1); namespace FG\X509\SAN; @@ -22,44 +23,44 @@ */ class SubjectAlternativeNames extends ASNObject implements Parsable { - private $alternativeNamesSequence; + private Sequence $alternativeNamesSequence; public function __construct() { $this->alternativeNamesSequence = new Sequence(); } - protected function calculateContentLength() + protected function calculateContentLength(): int { return $this->alternativeNamesSequence->getObjectLength(); } - public function getType() + public function getType(): int { return Identifier::OCTETSTRING; } - public function addDomainName(DNSName $domainName) + public function addDomainName(DNSName $domainName): void { $this->alternativeNamesSequence->addChild($domainName); } - public function addIP(IPAddress $ip) + public function addIP(IPAddress $ip): void { $this->alternativeNamesSequence->addChild($ip); } - public function getContent() + public function getContent(): array { return $this->alternativeNamesSequence->getContent(); } - protected function getEncodedValue() + protected function getEncodedValue(): string { return $this->alternativeNamesSequence->getBinary(); } - public static function fromBinary(&$binaryData, &$offsetIndex = 0) + public static function fromBinary(string &$binaryData, ?int &$offsetIndex = 0): static { self::parseIdentifier($binaryData[$offsetIndex], Identifier::OCTETSTRING, $offsetIndex++); $contentLength = self::parseContentLength($binaryData, $offsetIndex);