@@ -9,76 +9,76 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | trait DateTimeHelper |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Create DateTime object from time string and timezone. |
|
| 14 | - * |
|
| 15 | - * @param null|string $time Time string, default to 'now' |
|
| 16 | - * @param null|string $tz Timezone, default if omitted |
|
| 17 | - * |
|
| 18 | - * @throws \RuntimeException |
|
| 19 | - * |
|
| 20 | - * @return \DateTimeImmutable |
|
| 21 | - */ |
|
| 22 | - private static function _createDateTime( |
|
| 23 | - ?string $time = null, ?string $tz = null): \DateTimeImmutable |
|
| 24 | - { |
|
| 25 | - if (!isset($time)) { |
|
| 26 | - $time = 'now'; |
|
| 27 | - } |
|
| 28 | - if (!isset($tz)) { |
|
| 29 | - $tz = date_default_timezone_get(); |
|
| 30 | - } |
|
| 31 | - try { |
|
| 32 | - $dt = new \DateTimeImmutable($time, self::_createTimeZone($tz)); |
|
| 33 | - return self::_roundDownFractionalSeconds($dt); |
|
| 34 | - } catch (\Exception $e) { |
|
| 35 | - throw new \RuntimeException( |
|
| 36 | - 'Failed to create DateTime: ' . |
|
| 37 | - self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
| 38 | - } |
|
| 39 | - } |
|
| 12 | + /** |
|
| 13 | + * Create DateTime object from time string and timezone. |
|
| 14 | + * |
|
| 15 | + * @param null|string $time Time string, default to 'now' |
|
| 16 | + * @param null|string $tz Timezone, default if omitted |
|
| 17 | + * |
|
| 18 | + * @throws \RuntimeException |
|
| 19 | + * |
|
| 20 | + * @return \DateTimeImmutable |
|
| 21 | + */ |
|
| 22 | + private static function _createDateTime( |
|
| 23 | + ?string $time = null, ?string $tz = null): \DateTimeImmutable |
|
| 24 | + { |
|
| 25 | + if (!isset($time)) { |
|
| 26 | + $time = 'now'; |
|
| 27 | + } |
|
| 28 | + if (!isset($tz)) { |
|
| 29 | + $tz = date_default_timezone_get(); |
|
| 30 | + } |
|
| 31 | + try { |
|
| 32 | + $dt = new \DateTimeImmutable($time, self::_createTimeZone($tz)); |
|
| 33 | + return self::_roundDownFractionalSeconds($dt); |
|
| 34 | + } catch (\Exception $e) { |
|
| 35 | + throw new \RuntimeException( |
|
| 36 | + 'Failed to create DateTime: ' . |
|
| 37 | + self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
| 38 | + } |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Rounds a \DateTimeImmutable value such that fractional |
|
| 43 | - * seconds are removed. |
|
| 44 | - * |
|
| 45 | - * @param \DateTimeImmutable $dt |
|
| 46 | - * |
|
| 47 | - * @return \DateTimeImmutable |
|
| 48 | - */ |
|
| 49 | - private static function _roundDownFractionalSeconds( |
|
| 50 | - \DateTimeImmutable $dt): \DateTimeImmutable |
|
| 51 | - { |
|
| 52 | - return \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', |
|
| 53 | - $dt->format('Y-m-d H:i:s'), $dt->getTimezone()); |
|
| 54 | - } |
|
| 41 | + /** |
|
| 42 | + * Rounds a \DateTimeImmutable value such that fractional |
|
| 43 | + * seconds are removed. |
|
| 44 | + * |
|
| 45 | + * @param \DateTimeImmutable $dt |
|
| 46 | + * |
|
| 47 | + * @return \DateTimeImmutable |
|
| 48 | + */ |
|
| 49 | + private static function _roundDownFractionalSeconds( |
|
| 50 | + \DateTimeImmutable $dt): \DateTimeImmutable |
|
| 51 | + { |
|
| 52 | + return \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', |
|
| 53 | + $dt->format('Y-m-d H:i:s'), $dt->getTimezone()); |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Create DateTimeZone object from string. |
|
| 58 | - * |
|
| 59 | - * @param string $tz |
|
| 60 | - * |
|
| 61 | - * @throws \UnexpectedValueException |
|
| 62 | - * |
|
| 63 | - * @return \DateTimeZone |
|
| 64 | - */ |
|
| 65 | - private static function _createTimeZone(string $tz): \DateTimeZone |
|
| 66 | - { |
|
| 67 | - try { |
|
| 68 | - return new \DateTimeZone($tz); |
|
| 69 | - } catch (\Exception $e) { |
|
| 70 | - throw new \UnexpectedValueException('Invalid timezone.', 0, $e); |
|
| 71 | - } |
|
| 72 | - } |
|
| 56 | + /** |
|
| 57 | + * Create DateTimeZone object from string. |
|
| 58 | + * |
|
| 59 | + * @param string $tz |
|
| 60 | + * |
|
| 61 | + * @throws \UnexpectedValueException |
|
| 62 | + * |
|
| 63 | + * @return \DateTimeZone |
|
| 64 | + */ |
|
| 65 | + private static function _createTimeZone(string $tz): \DateTimeZone |
|
| 66 | + { |
|
| 67 | + try { |
|
| 68 | + return new \DateTimeZone($tz); |
|
| 69 | + } catch (\Exception $e) { |
|
| 70 | + throw new \UnexpectedValueException('Invalid timezone.', 0, $e); |
|
| 71 | + } |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * Get last error caused by DateTimeImmutable. |
|
| 76 | - * |
|
| 77 | - * @return string |
|
| 78 | - */ |
|
| 79 | - private static function _getLastDateTimeImmutableErrorsStr(): string |
|
| 80 | - { |
|
| 81 | - $errors = \DateTimeImmutable::getLastErrors()['errors']; |
|
| 82 | - return implode(', ', $errors); |
|
| 83 | - } |
|
| 74 | + /** |
|
| 75 | + * Get last error caused by DateTimeImmutable. |
|
| 76 | + * |
|
| 77 | + * @return string |
|
| 78 | + */ |
|
| 79 | + private static function _getLastDateTimeImmutableErrorsStr(): string |
|
| 80 | + { |
|
| 81 | + $errors = \DateTimeImmutable::getLastErrors()['errors']; |
|
| 82 | + return implode(', ', $errors); |
|
| 83 | + } |
|
| 84 | 84 | } |
@@ -14,145 +14,145 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | trait AttributeContainer |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * Array of attributes. |
|
| 19 | - * |
|
| 20 | - * @var Attribute[] |
|
| 21 | - */ |
|
| 22 | - protected $_attributes; |
|
| 17 | + /** |
|
| 18 | + * Array of attributes. |
|
| 19 | + * |
|
| 20 | + * @var Attribute[] |
|
| 21 | + */ |
|
| 22 | + protected $_attributes; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Check whether attribute is present. |
|
| 26 | - * |
|
| 27 | - * @param string $name OID or attribute name |
|
| 28 | - * |
|
| 29 | - * @return bool |
|
| 30 | - */ |
|
| 31 | - public function has(string $name): bool |
|
| 32 | - { |
|
| 33 | - return null !== $this->_findFirst($name); |
|
| 34 | - } |
|
| 24 | + /** |
|
| 25 | + * Check whether attribute is present. |
|
| 26 | + * |
|
| 27 | + * @param string $name OID or attribute name |
|
| 28 | + * |
|
| 29 | + * @return bool |
|
| 30 | + */ |
|
| 31 | + public function has(string $name): bool |
|
| 32 | + { |
|
| 33 | + return null !== $this->_findFirst($name); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Get first attribute by OID or attribute name. |
|
| 38 | - * |
|
| 39 | - * @param string $name OID or attribute name |
|
| 40 | - * |
|
| 41 | - * @throws \OutOfBoundsException |
|
| 42 | - * |
|
| 43 | - * @return Attribute |
|
| 44 | - */ |
|
| 45 | - public function firstOf(string $name): Attribute |
|
| 46 | - { |
|
| 47 | - $attr = $this->_findFirst($name); |
|
| 48 | - if (!$attr) { |
|
| 49 | - throw new \UnexpectedValueException("No {$name} attribute."); |
|
| 50 | - } |
|
| 51 | - return $attr; |
|
| 52 | - } |
|
| 36 | + /** |
|
| 37 | + * Get first attribute by OID or attribute name. |
|
| 38 | + * |
|
| 39 | + * @param string $name OID or attribute name |
|
| 40 | + * |
|
| 41 | + * @throws \OutOfBoundsException |
|
| 42 | + * |
|
| 43 | + * @return Attribute |
|
| 44 | + */ |
|
| 45 | + public function firstOf(string $name): Attribute |
|
| 46 | + { |
|
| 47 | + $attr = $this->_findFirst($name); |
|
| 48 | + if (!$attr) { |
|
| 49 | + throw new \UnexpectedValueException("No {$name} attribute."); |
|
| 50 | + } |
|
| 51 | + return $attr; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Get all attributes of given name. |
|
| 56 | - * |
|
| 57 | - * @param string $name OID or attribute name |
|
| 58 | - * |
|
| 59 | - * @return Attribute[] |
|
| 60 | - */ |
|
| 61 | - public function allOf(string $name): array |
|
| 62 | - { |
|
| 63 | - $oid = AttributeType::attrNameToOID($name); |
|
| 64 | - $attrs = array_filter($this->_attributes, |
|
| 65 | - function (Attribute $attr) use ($oid) { |
|
| 66 | - return $attr->oid() === $oid; |
|
| 67 | - }); |
|
| 68 | - return array_values($attrs); |
|
| 69 | - } |
|
| 54 | + /** |
|
| 55 | + * Get all attributes of given name. |
|
| 56 | + * |
|
| 57 | + * @param string $name OID or attribute name |
|
| 58 | + * |
|
| 59 | + * @return Attribute[] |
|
| 60 | + */ |
|
| 61 | + public function allOf(string $name): array |
|
| 62 | + { |
|
| 63 | + $oid = AttributeType::attrNameToOID($name); |
|
| 64 | + $attrs = array_filter($this->_attributes, |
|
| 65 | + function (Attribute $attr) use ($oid) { |
|
| 66 | + return $attr->oid() === $oid; |
|
| 67 | + }); |
|
| 68 | + return array_values($attrs); |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * Get all attributes. |
|
| 73 | - * |
|
| 74 | - * @return Attribute[] |
|
| 75 | - */ |
|
| 76 | - public function all(): array |
|
| 77 | - { |
|
| 78 | - return $this->_attributes; |
|
| 79 | - } |
|
| 71 | + /** |
|
| 72 | + * Get all attributes. |
|
| 73 | + * |
|
| 74 | + * @return Attribute[] |
|
| 75 | + */ |
|
| 76 | + public function all(): array |
|
| 77 | + { |
|
| 78 | + return $this->_attributes; |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * Get self with additional attributes added. |
|
| 83 | - * |
|
| 84 | - * @param Attribute ...$attribs |
|
| 85 | - * |
|
| 86 | - * @return self |
|
| 87 | - */ |
|
| 88 | - public function withAdditional(Attribute ...$attribs): self |
|
| 89 | - { |
|
| 90 | - $obj = clone $this; |
|
| 91 | - foreach ($attribs as $attr) { |
|
| 92 | - $obj->_attributes[] = $attr; |
|
| 93 | - } |
|
| 94 | - return $obj; |
|
| 95 | - } |
|
| 81 | + /** |
|
| 82 | + * Get self with additional attributes added. |
|
| 83 | + * |
|
| 84 | + * @param Attribute ...$attribs |
|
| 85 | + * |
|
| 86 | + * @return self |
|
| 87 | + */ |
|
| 88 | + public function withAdditional(Attribute ...$attribs): self |
|
| 89 | + { |
|
| 90 | + $obj = clone $this; |
|
| 91 | + foreach ($attribs as $attr) { |
|
| 92 | + $obj->_attributes[] = $attr; |
|
| 93 | + } |
|
| 94 | + return $obj; |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * Get self with single unique attribute added. |
|
| 99 | - * |
|
| 100 | - * All previous attributes of the same type are removed. |
|
| 101 | - * |
|
| 102 | - * @param Attribute $attr |
|
| 103 | - * |
|
| 104 | - * @return self |
|
| 105 | - */ |
|
| 106 | - public function withUnique(Attribute $attr): self |
|
| 107 | - { |
|
| 108 | - $obj = clone $this; |
|
| 109 | - $obj->_attributes = array_filter($obj->_attributes, |
|
| 110 | - function (Attribute $a) use ($attr) { |
|
| 111 | - return $a->oid() !== $attr->oid(); |
|
| 112 | - }); |
|
| 113 | - $obj->_attributes[] = $attr; |
|
| 114 | - return $obj; |
|
| 115 | - } |
|
| 97 | + /** |
|
| 98 | + * Get self with single unique attribute added. |
|
| 99 | + * |
|
| 100 | + * All previous attributes of the same type are removed. |
|
| 101 | + * |
|
| 102 | + * @param Attribute $attr |
|
| 103 | + * |
|
| 104 | + * @return self |
|
| 105 | + */ |
|
| 106 | + public function withUnique(Attribute $attr): self |
|
| 107 | + { |
|
| 108 | + $obj = clone $this; |
|
| 109 | + $obj->_attributes = array_filter($obj->_attributes, |
|
| 110 | + function (Attribute $a) use ($attr) { |
|
| 111 | + return $a->oid() !== $attr->oid(); |
|
| 112 | + }); |
|
| 113 | + $obj->_attributes[] = $attr; |
|
| 114 | + return $obj; |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - /** |
|
| 118 | - * Get number of attributes. |
|
| 119 | - * |
|
| 120 | - * @see \Countable::count() |
|
| 121 | - * |
|
| 122 | - * @return int |
|
| 123 | - */ |
|
| 124 | - public function count(): int |
|
| 125 | - { |
|
| 126 | - return count($this->_attributes); |
|
| 127 | - } |
|
| 117 | + /** |
|
| 118 | + * Get number of attributes. |
|
| 119 | + * |
|
| 120 | + * @see \Countable::count() |
|
| 121 | + * |
|
| 122 | + * @return int |
|
| 123 | + */ |
|
| 124 | + public function count(): int |
|
| 125 | + { |
|
| 126 | + return count($this->_attributes); |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | - /** |
|
| 130 | - * Get iterator for attributes. |
|
| 131 | - * |
|
| 132 | - * @see \IteratorAggregate::getIterator() |
|
| 133 | - * |
|
| 134 | - * @return \ArrayIterator |
|
| 135 | - */ |
|
| 136 | - public function getIterator(): \ArrayIterator |
|
| 137 | - { |
|
| 138 | - return new \ArrayIterator($this->_attributes); |
|
| 139 | - } |
|
| 129 | + /** |
|
| 130 | + * Get iterator for attributes. |
|
| 131 | + * |
|
| 132 | + * @see \IteratorAggregate::getIterator() |
|
| 133 | + * |
|
| 134 | + * @return \ArrayIterator |
|
| 135 | + */ |
|
| 136 | + public function getIterator(): \ArrayIterator |
|
| 137 | + { |
|
| 138 | + return new \ArrayIterator($this->_attributes); |
|
| 139 | + } |
|
| 140 | 140 | |
| 141 | - /** |
|
| 142 | - * Find first attribute of given name or OID. |
|
| 143 | - * |
|
| 144 | - * @param string $name |
|
| 145 | - * |
|
| 146 | - * @return null|Attribute |
|
| 147 | - */ |
|
| 148 | - protected function _findFirst(string $name): ?Attribute |
|
| 149 | - { |
|
| 150 | - $oid = AttributeType::attrNameToOID($name); |
|
| 151 | - foreach ($this->_attributes as $attr) { |
|
| 152 | - if ($attr->oid() === $oid) { |
|
| 153 | - return $attr; |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - return null; |
|
| 157 | - } |
|
| 141 | + /** |
|
| 142 | + * Find first attribute of given name or OID. |
|
| 143 | + * |
|
| 144 | + * @param string $name |
|
| 145 | + * |
|
| 146 | + * @return null|Attribute |
|
| 147 | + */ |
|
| 148 | + protected function _findFirst(string $name): ?Attribute |
|
| 149 | + { |
|
| 150 | + $oid = AttributeType::attrNameToOID($name); |
|
| 151 | + foreach ($this->_attributes as $attr) { |
|
| 152 | + if ($attr->oid() === $oid) { |
|
| 153 | + return $attr; |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + return null; |
|
| 157 | + } |
|
| 158 | 158 | } |
@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | |
| 19 | 19 | // load EC private key from PEM |
| 20 | 20 | $private_key_info = PrivateKeyInfo::fromPEM( |
| 21 | - PEM::fromFile(dirname(__DIR__) . '/test/assets/ec/private_key.pem')); |
|
| 21 | + PEM::fromFile(dirname(__DIR__) . '/test/assets/ec/private_key.pem')); |
|
| 22 | 22 | // extract public key from private key |
| 23 | 23 | $public_key_info = $private_key_info->publicKeyInfo(); |
| 24 | 24 | // DN of the subject |
@@ -27,6 +27,6 @@ discard block |
||
| 27 | 27 | $cri = new CertificationRequestInfo($subject, $public_key_info); |
| 28 | 28 | // sign certificate request with private key |
| 29 | 29 | $algo = SignatureAlgorithmIdentifierFactory::algoForAsymmetricCrypto( |
| 30 | - $private_key_info->algorithmIdentifier(), new SHA256AlgorithmIdentifier()); |
|
| 30 | + $private_key_info->algorithmIdentifier(), new SHA256AlgorithmIdentifier()); |
|
| 31 | 31 | $csr = $cri->sign($algo, $private_key_info); |
| 32 | 32 | echo $csr; |
@@ -38,106 +38,106 @@ discard block |
||
| 38 | 38 | |
| 39 | 39 | // CA private key |
| 40 | 40 | openssl_pkey_export( |
| 41 | - openssl_pkey_new( |
|
| 42 | - ['private_key_type' => OPENSSL_KEYTYPE_RSA, |
|
| 43 | - 'private_key_bits' => 2048, ]), $pkey); |
|
| 41 | + openssl_pkey_new( |
|
| 42 | + ['private_key_type' => OPENSSL_KEYTYPE_RSA, |
|
| 43 | + 'private_key_bits' => 2048, ]), $pkey); |
|
| 44 | 44 | $ca_private_key = PrivateKeyInfo::fromPEM(PEM::fromString($pkey)); |
| 45 | 45 | // Issuer private key |
| 46 | 46 | openssl_pkey_export( |
| 47 | - openssl_pkey_new( |
|
| 48 | - ['private_key_type' => OPENSSL_KEYTYPE_RSA, |
|
| 49 | - 'private_key_bits' => 2048, ]), $pkey); |
|
| 47 | + openssl_pkey_new( |
|
| 48 | + ['private_key_type' => OPENSSL_KEYTYPE_RSA, |
|
| 49 | + 'private_key_bits' => 2048, ]), $pkey); |
|
| 50 | 50 | $issuer_private_key = PrivateKeyInfo::fromPEM(PEM::fromString($pkey)); |
| 51 | 51 | // Holder private key |
| 52 | 52 | openssl_pkey_export( |
| 53 | - openssl_pkey_new( |
|
| 54 | - ['private_key_type' => OPENSSL_KEYTYPE_RSA, |
|
| 55 | - 'private_key_bits' => 2048, ]), $pkey); |
|
| 53 | + openssl_pkey_new( |
|
| 54 | + ['private_key_type' => OPENSSL_KEYTYPE_RSA, |
|
| 55 | + 'private_key_bits' => 2048, ]), $pkey); |
|
| 56 | 56 | $holder_private_key = PrivateKeyInfo::fromPEM(PEM::fromString($pkey)); |
| 57 | 57 | |
| 58 | 58 | // create trust anchor certificate (self signed) |
| 59 | 59 | $tbs_cert = new TBSCertificate( |
| 60 | - Name::fromString('cn=CA'), |
|
| 61 | - $ca_private_key->publicKeyInfo(), |
|
| 62 | - Name::fromString('cn=CA'), |
|
| 63 | - Validity::fromStrings('now', 'now + 1 year')); |
|
| 60 | + Name::fromString('cn=CA'), |
|
| 61 | + $ca_private_key->publicKeyInfo(), |
|
| 62 | + Name::fromString('cn=CA'), |
|
| 63 | + Validity::fromStrings('now', 'now + 1 year')); |
|
| 64 | 64 | $tbs_cert = $tbs_cert->withRandomSerialNumber() |
| 65 | - ->withAdditionalExtensions( |
|
| 66 | - new BasicConstraintsExtension(true, true), |
|
| 67 | - new SubjectKeyIdentifierExtension(false, |
|
| 68 | - $ca_private_key->publicKeyInfo()->keyIdentifier()), |
|
| 69 | - new KeyUsageExtension(true, |
|
| 70 | - KeyUsageExtension::DIGITAL_SIGNATURE | |
|
| 71 | - KeyUsageExtension::KEY_CERT_SIGN)); |
|
| 65 | + ->withAdditionalExtensions( |
|
| 66 | + new BasicConstraintsExtension(true, true), |
|
| 67 | + new SubjectKeyIdentifierExtension(false, |
|
| 68 | + $ca_private_key->publicKeyInfo()->keyIdentifier()), |
|
| 69 | + new KeyUsageExtension(true, |
|
| 70 | + KeyUsageExtension::DIGITAL_SIGNATURE | |
|
| 71 | + KeyUsageExtension::KEY_CERT_SIGN)); |
|
| 72 | 72 | $algo = SignatureAlgorithmIdentifierFactory::algoForAsymmetricCrypto( |
| 73 | - $ca_private_key->algorithmIdentifier(), |
|
| 74 | - new SHA256AlgorithmIdentifier()); |
|
| 73 | + $ca_private_key->algorithmIdentifier(), |
|
| 74 | + new SHA256AlgorithmIdentifier()); |
|
| 75 | 75 | $ca_cert = $tbs_cert->sign($algo, $ca_private_key); |
| 76 | 76 | |
| 77 | 77 | // create AC issuer certificate |
| 78 | 78 | $tbs_cert = new TBSCertificate( |
| 79 | - Name::fromString('cn=Issuer'), |
|
| 80 | - $issuer_private_key->publicKeyInfo(), |
|
| 81 | - new Name(), |
|
| 82 | - Validity::fromStrings('now', 'now + 6 months')); |
|
| 79 | + Name::fromString('cn=Issuer'), |
|
| 80 | + $issuer_private_key->publicKeyInfo(), |
|
| 81 | + new Name(), |
|
| 82 | + Validity::fromStrings('now', 'now + 6 months')); |
|
| 83 | 83 | $tbs_cert = $tbs_cert->withIssuerCertificate($ca_cert) |
| 84 | - ->withRandomSerialNumber() |
|
| 85 | - ->withAdditionalExtensions( |
|
| 86 | - // issuer must not be a CA |
|
| 87 | - new BasicConstraintsExtension(true, false), |
|
| 88 | - new KeyUsageExtension(true, |
|
| 89 | - KeyUsageExtension::DIGITAL_SIGNATURE | |
|
| 90 | - KeyUsageExtension::KEY_ENCIPHERMENT)); |
|
| 84 | + ->withRandomSerialNumber() |
|
| 85 | + ->withAdditionalExtensions( |
|
| 86 | + // issuer must not be a CA |
|
| 87 | + new BasicConstraintsExtension(true, false), |
|
| 88 | + new KeyUsageExtension(true, |
|
| 89 | + KeyUsageExtension::DIGITAL_SIGNATURE | |
|
| 90 | + KeyUsageExtension::KEY_ENCIPHERMENT)); |
|
| 91 | 91 | $algo = SignatureAlgorithmIdentifierFactory::algoForAsymmetricCrypto( |
| 92 | - $ca_private_key->algorithmIdentifier(), |
|
| 93 | - new SHA256AlgorithmIdentifier()); |
|
| 92 | + $ca_private_key->algorithmIdentifier(), |
|
| 93 | + new SHA256AlgorithmIdentifier()); |
|
| 94 | 94 | $issuer_cert = $tbs_cert->sign($algo, $ca_private_key); |
| 95 | 95 | |
| 96 | 96 | // create AC holder certificate |
| 97 | 97 | $tbs_cert = new TBSCertificate( |
| 98 | - Name::fromString('cn=Holder, gn=John, sn=Doe'), |
|
| 99 | - $holder_private_key->publicKeyInfo(), |
|
| 100 | - new Name(), |
|
| 101 | - Validity::fromStrings('now', 'now + 6 months')); |
|
| 98 | + Name::fromString('cn=Holder, gn=John, sn=Doe'), |
|
| 99 | + $holder_private_key->publicKeyInfo(), |
|
| 100 | + new Name(), |
|
| 101 | + Validity::fromStrings('now', 'now + 6 months')); |
|
| 102 | 102 | $tbs_cert = $tbs_cert->withIssuerCertificate($ca_cert) |
| 103 | - ->withRandomSerialNumber() |
|
| 104 | - ->withAdditionalExtensions( |
|
| 105 | - new BasicConstraintsExtension(true, false), |
|
| 106 | - new KeyUsageExtension(true, |
|
| 107 | - KeyUsageExtension::DIGITAL_SIGNATURE | |
|
| 108 | - KeyUsageExtension::KEY_ENCIPHERMENT)); |
|
| 103 | + ->withRandomSerialNumber() |
|
| 104 | + ->withAdditionalExtensions( |
|
| 105 | + new BasicConstraintsExtension(true, false), |
|
| 106 | + new KeyUsageExtension(true, |
|
| 107 | + KeyUsageExtension::DIGITAL_SIGNATURE | |
|
| 108 | + KeyUsageExtension::KEY_ENCIPHERMENT)); |
|
| 109 | 109 | $algo = SignatureAlgorithmIdentifierFactory::algoForAsymmetricCrypto( |
| 110 | - $ca_private_key->algorithmIdentifier(), |
|
| 111 | - new SHA256AlgorithmIdentifier()); |
|
| 110 | + $ca_private_key->algorithmIdentifier(), |
|
| 111 | + new SHA256AlgorithmIdentifier()); |
|
| 112 | 112 | $holder_cert = $tbs_cert->sign($algo, $ca_private_key); |
| 113 | 113 | |
| 114 | 114 | // named authority that grants the attributes |
| 115 | 115 | $authority = new GeneralNames( |
| 116 | - new UniformResourceIdentifier('uri:trusted_authority')); |
|
| 116 | + new UniformResourceIdentifier('uri:trusted_authority')); |
|
| 117 | 117 | // role attribute |
| 118 | 118 | $attribs = new Attributes( |
| 119 | - Attribute::fromAttributeValues( |
|
| 120 | - RoleAttributeValue::fromString('role-name', $authority))); |
|
| 119 | + Attribute::fromAttributeValues( |
|
| 120 | + RoleAttributeValue::fromString('role-name', $authority))); |
|
| 121 | 121 | $aci = new AttributeCertificateInfo( |
| 122 | - // holder is identified by the holder's public key certificate |
|
| 123 | - new Holder(IssuerSerial::fromPKC($holder_cert)), |
|
| 124 | - AttCertIssuer::fromPKC($issuer_cert), |
|
| 125 | - AttCertValidityPeriod::fromStrings('now - 1 hour', 'now + 3 months'), |
|
| 126 | - $attribs); |
|
| 122 | + // holder is identified by the holder's public key certificate |
|
| 123 | + new Holder(IssuerSerial::fromPKC($holder_cert)), |
|
| 124 | + AttCertIssuer::fromPKC($issuer_cert), |
|
| 125 | + AttCertValidityPeriod::fromStrings('now - 1 hour', 'now + 3 months'), |
|
| 126 | + $attribs); |
|
| 127 | 127 | $aci = $aci->withRandomSerialNumber() |
| 128 | - ->withAdditionalExtensions( |
|
| 129 | - // named target identifier |
|
| 130 | - TargetInformationExtension::fromTargets( |
|
| 131 | - new TargetName( |
|
| 132 | - new UniformResourceIdentifier('uri:target_identifier'))), |
|
| 133 | - // key identifier of the AC issuer |
|
| 134 | - new AuthorityKeyIdentifierExtension(false, |
|
| 135 | - $issuer_cert->tbsCertificate() |
|
| 136 | - ->subjectPublicKeyInfo() |
|
| 137 | - ->keyIdentifier())); |
|
| 128 | + ->withAdditionalExtensions( |
|
| 129 | + // named target identifier |
|
| 130 | + TargetInformationExtension::fromTargets( |
|
| 131 | + new TargetName( |
|
| 132 | + new UniformResourceIdentifier('uri:target_identifier'))), |
|
| 133 | + // key identifier of the AC issuer |
|
| 134 | + new AuthorityKeyIdentifierExtension(false, |
|
| 135 | + $issuer_cert->tbsCertificate() |
|
| 136 | + ->subjectPublicKeyInfo() |
|
| 137 | + ->keyIdentifier())); |
|
| 138 | 138 | $algo = SignatureAlgorithmIdentifierFactory::algoForAsymmetricCrypto( |
| 139 | - $issuer_private_key->algorithmIdentifier(), |
|
| 140 | - new SHA256AlgorithmIdentifier()); |
|
| 139 | + $issuer_private_key->algorithmIdentifier(), |
|
| 140 | + new SHA256AlgorithmIdentifier()); |
|
| 141 | 141 | $ac = $aci->sign($algo, $issuer_private_key); |
| 142 | 142 | |
| 143 | 143 | // validate AC |
@@ -149,7 +149,7 @@ discard block |
||
| 149 | 149 | $validator_config = $validator_config->withTargets($target); |
| 150 | 150 | $validator = new ACValidator($ac, $validator_config); |
| 151 | 151 | if ($validator->validate()) { |
| 152 | - fprintf(STDERR, "AC validation succeeded.\n"); |
|
| 152 | + fprintf(STDERR, "AC validation succeeded.\n"); |
|
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | fprintf(STDERR, "Root certificate:\n"); |
@@ -27,27 +27,27 @@ |
||
| 27 | 27 | $csr = CertificationRequest::fromPEM(PEM::fromFile($argv[2])); |
| 28 | 28 | // verify CSR |
| 29 | 29 | if (!$csr->verify()) { |
| 30 | - echo "Failed to verify certification request signature.\n"; |
|
| 31 | - exit(1); |
|
| 30 | + echo "Failed to verify certification request signature.\n"; |
|
| 31 | + exit(1); |
|
| 32 | 32 | } |
| 33 | 33 | // load CA's private key from PEM |
| 34 | 34 | $private_key_info = PrivateKeyInfo::fromPEM( |
| 35 | - PEM::fromFile(dirname(__DIR__) . '/test/assets/rsa/private_key.pem')); |
|
| 35 | + PEM::fromFile(dirname(__DIR__) . '/test/assets/rsa/private_key.pem')); |
|
| 36 | 36 | // initialize certificate from CSR and issuer's certificate |
| 37 | 37 | $tbs_cert = TBSCertificate::fromCSR($csr)->withIssuerCertificate($issuer_cert); |
| 38 | 38 | // set random serial number |
| 39 | 39 | $tbs_cert = $tbs_cert->withRandomSerialNumber(); |
| 40 | 40 | // set validity period |
| 41 | 41 | $tbs_cert = $tbs_cert->withValidity( |
| 42 | - Validity::fromStrings('now', 'now + 3 months')); |
|
| 42 | + Validity::fromStrings('now', 'now + 3 months')); |
|
| 43 | 43 | // add extensions |
| 44 | 44 | $tbs_cert = $tbs_cert->withAdditionalExtensions( |
| 45 | - new KeyUsageExtension(true, |
|
| 46 | - KeyUsageExtension::DIGITAL_SIGNATURE | |
|
| 47 | - KeyUsageExtension::KEY_ENCIPHERMENT), |
|
| 48 | - new BasicConstraintsExtension(true, false)); |
|
| 45 | + new KeyUsageExtension(true, |
|
| 46 | + KeyUsageExtension::DIGITAL_SIGNATURE | |
|
| 47 | + KeyUsageExtension::KEY_ENCIPHERMENT), |
|
| 48 | + new BasicConstraintsExtension(true, false)); |
|
| 49 | 49 | // sign certificate with issuer's private key |
| 50 | 50 | $algo = SignatureAlgorithmIdentifierFactory::algoForAsymmetricCrypto( |
| 51 | - $private_key_info->algorithmIdentifier(), new SHA512AlgorithmIdentifier()); |
|
| 51 | + $private_key_info->algorithmIdentifier(), new SHA512AlgorithmIdentifier()); |
|
| 52 | 52 | $cert = $tbs_cert->sign($algo, $private_key_info); |
| 53 | 53 | echo $cert; |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | |
| 23 | 23 | // load RSA private key from PEM |
| 24 | 24 | $private_key_info = PrivateKeyInfo::fromPEM( |
| 25 | - PEM::fromFile(dirname(__DIR__) . '/test/assets/rsa/private_key.pem')); |
|
| 25 | + PEM::fromFile(dirname(__DIR__) . '/test/assets/rsa/private_key.pem')); |
|
| 26 | 26 | // extract public key from private key |
| 27 | 27 | $public_key_info = $private_key_info->publicKeyInfo(); |
| 28 | 28 | // DN of the certification authority |
@@ -32,12 +32,12 @@ discard block |
||
| 32 | 32 | // create "to be signed" certificate object with extensions |
| 33 | 33 | $tbs_cert = new TBSCertificate($name, $public_key_info, $name, $validity); |
| 34 | 34 | $tbs_cert = $tbs_cert->withRandomSerialNumber()->withAdditionalExtensions( |
| 35 | - new BasicConstraintsExtension(true, true), |
|
| 36 | - new SubjectKeyIdentifierExtension(false, $public_key_info->keyIdentifier()), |
|
| 37 | - new KeyUsageExtension(true, |
|
| 38 | - KeyUsageExtension::DIGITAL_SIGNATURE | KeyUsageExtension::KEY_CERT_SIGN)); |
|
| 35 | + new BasicConstraintsExtension(true, true), |
|
| 36 | + new SubjectKeyIdentifierExtension(false, $public_key_info->keyIdentifier()), |
|
| 37 | + new KeyUsageExtension(true, |
|
| 38 | + KeyUsageExtension::DIGITAL_SIGNATURE | KeyUsageExtension::KEY_CERT_SIGN)); |
|
| 39 | 39 | // sign certificate with private key |
| 40 | 40 | $algo = SignatureAlgorithmIdentifierFactory::algoForAsymmetricCrypto( |
| 41 | - $private_key_info->algorithmIdentifier(), new SHA256AlgorithmIdentifier()); |
|
| 41 | + $private_key_info->algorithmIdentifier(), new SHA256AlgorithmIdentifier()); |
|
| 42 | 42 | $cert = $tbs_cert->sign($algo, $private_key_info); |
| 43 | 43 | echo $cert; |
@@ -20,185 +20,185 @@ |
||
| 20 | 20 | */ |
| 21 | 21 | class AuthorityKeyIdentifierExtension extends Extension |
| 22 | 22 | { |
| 23 | - /** |
|
| 24 | - * Key identifier. |
|
| 25 | - * |
|
| 26 | - * @var null|string |
|
| 27 | - */ |
|
| 28 | - protected $_keyIdentifier; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Issuer name. |
|
| 32 | - * |
|
| 33 | - * @var null|GeneralNames |
|
| 34 | - */ |
|
| 35 | - protected $_authorityCertIssuer; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Issuer serial number as a base 10 integer. |
|
| 39 | - * |
|
| 40 | - * @var null|string |
|
| 41 | - */ |
|
| 42 | - protected $_authorityCertSerialNumber; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Constructor. |
|
| 46 | - * |
|
| 47 | - * @param bool $critical Conforming CA's must mark as non-critical (false) |
|
| 48 | - * @param null|string $keyIdentifier Key identifier |
|
| 49 | - * @param null|GeneralNames $issuer Issuer name |
|
| 50 | - * @param null|int|string $serial Issuer serial number as a base 10 integer |
|
| 51 | - */ |
|
| 52 | - public function __construct(bool $critical, ?string $keyIdentifier, |
|
| 53 | - ?GeneralNames $issuer = null, $serial = null) |
|
| 54 | - { |
|
| 55 | - parent::__construct(self::OID_AUTHORITY_KEY_IDENTIFIER, $critical); |
|
| 56 | - $this->_keyIdentifier = $keyIdentifier; |
|
| 57 | - $this->_authorityCertIssuer = $issuer; |
|
| 58 | - $this->_authorityCertSerialNumber = isset($serial) ? strval($serial) : null; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Create from public key info. |
|
| 63 | - * |
|
| 64 | - * @param PublicKeyInfo $pki |
|
| 65 | - * |
|
| 66 | - * @return AuthorityKeyIdentifierExtension |
|
| 67 | - */ |
|
| 68 | - public static function fromPublicKeyInfo(PublicKeyInfo $pki): self |
|
| 69 | - { |
|
| 70 | - return new self(false, $pki->keyIdentifier()); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Whether key identifier is present. |
|
| 75 | - * |
|
| 76 | - * @return bool |
|
| 77 | - */ |
|
| 78 | - public function hasKeyIdentifier(): bool |
|
| 79 | - { |
|
| 80 | - return isset($this->_keyIdentifier); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Get key identifier. |
|
| 85 | - * |
|
| 86 | - * @throws \LogicException If not set |
|
| 87 | - * |
|
| 88 | - * @return string |
|
| 89 | - */ |
|
| 90 | - public function keyIdentifier(): string |
|
| 91 | - { |
|
| 92 | - if (!$this->hasKeyIdentifier()) { |
|
| 93 | - throw new \LogicException('keyIdentifier not set.'); |
|
| 94 | - } |
|
| 95 | - return $this->_keyIdentifier; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Whether issuer is present. |
|
| 100 | - * |
|
| 101 | - * @return bool |
|
| 102 | - */ |
|
| 103 | - public function hasIssuer(): bool |
|
| 104 | - { |
|
| 105 | - return isset($this->_authorityCertIssuer); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Get issuer. |
|
| 110 | - * |
|
| 111 | - * @throws \LogicException If not set |
|
| 112 | - * |
|
| 113 | - * @return GeneralNames |
|
| 114 | - */ |
|
| 115 | - public function issuer(): GeneralNames |
|
| 116 | - { |
|
| 117 | - if (!$this->hasIssuer()) { |
|
| 118 | - throw new \LogicException('authorityCertIssuer not set.'); |
|
| 119 | - } |
|
| 120 | - return $this->_authorityCertIssuer; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Whether serial is present. |
|
| 125 | - * |
|
| 126 | - * @return bool |
|
| 127 | - */ |
|
| 128 | - public function hasSerial(): bool |
|
| 129 | - { |
|
| 130 | - return isset($this->_authorityCertSerialNumber); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Get serial number. |
|
| 135 | - * |
|
| 136 | - * @throws \LogicException If not set |
|
| 137 | - * |
|
| 138 | - * @return string Base 10 integer string |
|
| 139 | - */ |
|
| 140 | - public function serial(): string |
|
| 141 | - { |
|
| 142 | - if (!$this->hasSerial()) { |
|
| 143 | - throw new \LogicException('authorityCertSerialNumber not set.'); |
|
| 144 | - } |
|
| 145 | - return $this->_authorityCertSerialNumber; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * {@inheritdoc} |
|
| 150 | - */ |
|
| 151 | - protected static function _fromDER(string $data, bool $critical): Extension |
|
| 152 | - { |
|
| 153 | - $seq = UnspecifiedType::fromDER($data)->asSequence(); |
|
| 154 | - $keyIdentifier = null; |
|
| 155 | - $issuer = null; |
|
| 156 | - $serial = null; |
|
| 157 | - if ($seq->hasTagged(0)) { |
|
| 158 | - $keyIdentifier = $seq->getTagged(0) |
|
| 159 | - ->asImplicit(Element::TYPE_OCTET_STRING) |
|
| 160 | - ->asOctetString()->string(); |
|
| 161 | - } |
|
| 162 | - if ($seq->hasTagged(1) || $seq->hasTagged(2)) { |
|
| 163 | - if (!$seq->hasTagged(1) || !$seq->hasTagged(2)) { |
|
| 164 | - throw new \UnexpectedValueException( |
|
| 165 | - 'AuthorityKeyIdentifier must have both' . |
|
| 166 | - ' authorityCertIssuer and authorityCertSerialNumber' . |
|
| 167 | - ' present or both absent.'); |
|
| 168 | - } |
|
| 169 | - $issuer = GeneralNames::fromASN1($seq->getTagged(1) |
|
| 170 | - ->asImplicit(Element::TYPE_SEQUENCE)->asSequence()); |
|
| 171 | - $serial = $seq->getTagged(2)->asImplicit(Element::TYPE_INTEGER) |
|
| 172 | - ->asInteger()->number(); |
|
| 173 | - } |
|
| 174 | - return new self($critical, $keyIdentifier, $issuer, $serial); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * {@inheritdoc} |
|
| 179 | - */ |
|
| 180 | - protected function _valueASN1(): Element |
|
| 181 | - { |
|
| 182 | - $elements = []; |
|
| 183 | - if (isset($this->_keyIdentifier)) { |
|
| 184 | - $elements[] = new ImplicitlyTaggedType(0, |
|
| 185 | - new OctetString($this->_keyIdentifier)); |
|
| 186 | - } |
|
| 187 | - // if either issuer or serial is set, both must be set |
|
| 188 | - if (isset($this->_authorityCertIssuer) || |
|
| 189 | - isset($this->_authorityCertSerialNumber)) { |
|
| 190 | - if (!isset($this->_authorityCertIssuer, |
|
| 191 | - $this->_authorityCertSerialNumber)) { |
|
| 192 | - throw new \LogicException( |
|
| 193 | - 'AuthorityKeyIdentifier must have both' . |
|
| 194 | - ' authorityCertIssuer and authorityCertSerialNumber' . |
|
| 195 | - ' present or both absent.'); |
|
| 196 | - } |
|
| 197 | - $elements[] = new ImplicitlyTaggedType(1, |
|
| 198 | - $this->_authorityCertIssuer->toASN1()); |
|
| 199 | - $elements[] = new ImplicitlyTaggedType(2, |
|
| 200 | - new Integer($this->_authorityCertSerialNumber)); |
|
| 201 | - } |
|
| 202 | - return new Sequence(...$elements); |
|
| 203 | - } |
|
| 23 | + /** |
|
| 24 | + * Key identifier. |
|
| 25 | + * |
|
| 26 | + * @var null|string |
|
| 27 | + */ |
|
| 28 | + protected $_keyIdentifier; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Issuer name. |
|
| 32 | + * |
|
| 33 | + * @var null|GeneralNames |
|
| 34 | + */ |
|
| 35 | + protected $_authorityCertIssuer; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Issuer serial number as a base 10 integer. |
|
| 39 | + * |
|
| 40 | + * @var null|string |
|
| 41 | + */ |
|
| 42 | + protected $_authorityCertSerialNumber; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Constructor. |
|
| 46 | + * |
|
| 47 | + * @param bool $critical Conforming CA's must mark as non-critical (false) |
|
| 48 | + * @param null|string $keyIdentifier Key identifier |
|
| 49 | + * @param null|GeneralNames $issuer Issuer name |
|
| 50 | + * @param null|int|string $serial Issuer serial number as a base 10 integer |
|
| 51 | + */ |
|
| 52 | + public function __construct(bool $critical, ?string $keyIdentifier, |
|
| 53 | + ?GeneralNames $issuer = null, $serial = null) |
|
| 54 | + { |
|
| 55 | + parent::__construct(self::OID_AUTHORITY_KEY_IDENTIFIER, $critical); |
|
| 56 | + $this->_keyIdentifier = $keyIdentifier; |
|
| 57 | + $this->_authorityCertIssuer = $issuer; |
|
| 58 | + $this->_authorityCertSerialNumber = isset($serial) ? strval($serial) : null; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Create from public key info. |
|
| 63 | + * |
|
| 64 | + * @param PublicKeyInfo $pki |
|
| 65 | + * |
|
| 66 | + * @return AuthorityKeyIdentifierExtension |
|
| 67 | + */ |
|
| 68 | + public static function fromPublicKeyInfo(PublicKeyInfo $pki): self |
|
| 69 | + { |
|
| 70 | + return new self(false, $pki->keyIdentifier()); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Whether key identifier is present. |
|
| 75 | + * |
|
| 76 | + * @return bool |
|
| 77 | + */ |
|
| 78 | + public function hasKeyIdentifier(): bool |
|
| 79 | + { |
|
| 80 | + return isset($this->_keyIdentifier); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Get key identifier. |
|
| 85 | + * |
|
| 86 | + * @throws \LogicException If not set |
|
| 87 | + * |
|
| 88 | + * @return string |
|
| 89 | + */ |
|
| 90 | + public function keyIdentifier(): string |
|
| 91 | + { |
|
| 92 | + if (!$this->hasKeyIdentifier()) { |
|
| 93 | + throw new \LogicException('keyIdentifier not set.'); |
|
| 94 | + } |
|
| 95 | + return $this->_keyIdentifier; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Whether issuer is present. |
|
| 100 | + * |
|
| 101 | + * @return bool |
|
| 102 | + */ |
|
| 103 | + public function hasIssuer(): bool |
|
| 104 | + { |
|
| 105 | + return isset($this->_authorityCertIssuer); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Get issuer. |
|
| 110 | + * |
|
| 111 | + * @throws \LogicException If not set |
|
| 112 | + * |
|
| 113 | + * @return GeneralNames |
|
| 114 | + */ |
|
| 115 | + public function issuer(): GeneralNames |
|
| 116 | + { |
|
| 117 | + if (!$this->hasIssuer()) { |
|
| 118 | + throw new \LogicException('authorityCertIssuer not set.'); |
|
| 119 | + } |
|
| 120 | + return $this->_authorityCertIssuer; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Whether serial is present. |
|
| 125 | + * |
|
| 126 | + * @return bool |
|
| 127 | + */ |
|
| 128 | + public function hasSerial(): bool |
|
| 129 | + { |
|
| 130 | + return isset($this->_authorityCertSerialNumber); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Get serial number. |
|
| 135 | + * |
|
| 136 | + * @throws \LogicException If not set |
|
| 137 | + * |
|
| 138 | + * @return string Base 10 integer string |
|
| 139 | + */ |
|
| 140 | + public function serial(): string |
|
| 141 | + { |
|
| 142 | + if (!$this->hasSerial()) { |
|
| 143 | + throw new \LogicException('authorityCertSerialNumber not set.'); |
|
| 144 | + } |
|
| 145 | + return $this->_authorityCertSerialNumber; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * {@inheritdoc} |
|
| 150 | + */ |
|
| 151 | + protected static function _fromDER(string $data, bool $critical): Extension |
|
| 152 | + { |
|
| 153 | + $seq = UnspecifiedType::fromDER($data)->asSequence(); |
|
| 154 | + $keyIdentifier = null; |
|
| 155 | + $issuer = null; |
|
| 156 | + $serial = null; |
|
| 157 | + if ($seq->hasTagged(0)) { |
|
| 158 | + $keyIdentifier = $seq->getTagged(0) |
|
| 159 | + ->asImplicit(Element::TYPE_OCTET_STRING) |
|
| 160 | + ->asOctetString()->string(); |
|
| 161 | + } |
|
| 162 | + if ($seq->hasTagged(1) || $seq->hasTagged(2)) { |
|
| 163 | + if (!$seq->hasTagged(1) || !$seq->hasTagged(2)) { |
|
| 164 | + throw new \UnexpectedValueException( |
|
| 165 | + 'AuthorityKeyIdentifier must have both' . |
|
| 166 | + ' authorityCertIssuer and authorityCertSerialNumber' . |
|
| 167 | + ' present or both absent.'); |
|
| 168 | + } |
|
| 169 | + $issuer = GeneralNames::fromASN1($seq->getTagged(1) |
|
| 170 | + ->asImplicit(Element::TYPE_SEQUENCE)->asSequence()); |
|
| 171 | + $serial = $seq->getTagged(2)->asImplicit(Element::TYPE_INTEGER) |
|
| 172 | + ->asInteger()->number(); |
|
| 173 | + } |
|
| 174 | + return new self($critical, $keyIdentifier, $issuer, $serial); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * {@inheritdoc} |
|
| 179 | + */ |
|
| 180 | + protected function _valueASN1(): Element |
|
| 181 | + { |
|
| 182 | + $elements = []; |
|
| 183 | + if (isset($this->_keyIdentifier)) { |
|
| 184 | + $elements[] = new ImplicitlyTaggedType(0, |
|
| 185 | + new OctetString($this->_keyIdentifier)); |
|
| 186 | + } |
|
| 187 | + // if either issuer or serial is set, both must be set |
|
| 188 | + if (isset($this->_authorityCertIssuer) || |
|
| 189 | + isset($this->_authorityCertSerialNumber)) { |
|
| 190 | + if (!isset($this->_authorityCertIssuer, |
|
| 191 | + $this->_authorityCertSerialNumber)) { |
|
| 192 | + throw new \LogicException( |
|
| 193 | + 'AuthorityKeyIdentifier must have both' . |
|
| 194 | + ' authorityCertIssuer and authorityCertSerialNumber' . |
|
| 195 | + ' present or both absent.'); |
|
| 196 | + } |
|
| 197 | + $elements[] = new ImplicitlyTaggedType(1, |
|
| 198 | + $this->_authorityCertIssuer->toASN1()); |
|
| 199 | + $elements[] = new ImplicitlyTaggedType(2, |
|
| 200 | + new Integer($this->_authorityCertSerialNumber)); |
|
| 201 | + } |
|
| 202 | + return new Sequence(...$elements); |
|
| 203 | + } |
|
| 204 | 204 | } |
@@ -24,178 +24,178 @@ |
||
| 24 | 24 | */ |
| 25 | 25 | class CertificationPath implements \Countable, \IteratorAggregate |
| 26 | 26 | { |
| 27 | - /** |
|
| 28 | - * Certification path. |
|
| 29 | - * |
|
| 30 | - * @var Certificate[] |
|
| 31 | - */ |
|
| 32 | - protected $_certificates; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Constructor. |
|
| 36 | - * |
|
| 37 | - * @param Certificate ...$certificates Certificates from the trust anchor |
|
| 38 | - * to the target end-entity certificate |
|
| 39 | - */ |
|
| 40 | - public function __construct(Certificate ...$certificates) |
|
| 41 | - { |
|
| 42 | - $this->_certificates = $certificates; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Initialize from a certificate chain. |
|
| 47 | - * |
|
| 48 | - * @param CertificateChain $chain |
|
| 49 | - * |
|
| 50 | - * @return self |
|
| 51 | - */ |
|
| 52 | - public static function fromCertificateChain(CertificateChain $chain): self |
|
| 53 | - { |
|
| 54 | - return new self(...array_reverse($chain->certificates(), false)); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Build certification path to given target. |
|
| 59 | - * |
|
| 60 | - * @param Certificate $target Target end-entity certificate |
|
| 61 | - * @param CertificateBundle $trust_anchors List of trust anchors |
|
| 62 | - * @param null|CertificateBundle $intermediate Optional intermediate certificates |
|
| 63 | - * |
|
| 64 | - * @return self |
|
| 65 | - */ |
|
| 66 | - public static function toTarget(Certificate $target, |
|
| 67 | - CertificateBundle $trust_anchors, ?CertificateBundle $intermediate = null): self |
|
| 68 | - { |
|
| 69 | - $builder = new CertificationPathBuilder($trust_anchors); |
|
| 70 | - return $builder->shortestPathToTarget($target, $intermediate); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Build certification path from given trust anchor to target certificate, |
|
| 75 | - * using intermediate certificates from given bundle. |
|
| 76 | - * |
|
| 77 | - * @param Certificate $trust_anchor Trust anchor certificate |
|
| 78 | - * @param Certificate $target Target end-entity certificate |
|
| 79 | - * @param null|CertificateBundle $intermediate Optional intermediate certificates |
|
| 80 | - * |
|
| 81 | - * @return self |
|
| 82 | - */ |
|
| 83 | - public static function fromTrustAnchorToTarget(Certificate $trust_anchor, |
|
| 84 | - Certificate $target, ?CertificateBundle $intermediate = null): self |
|
| 85 | - { |
|
| 86 | - return self::toTarget($target, new CertificateBundle($trust_anchor), |
|
| 87 | - $intermediate); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Get certificates. |
|
| 92 | - * |
|
| 93 | - * @return Certificate[] |
|
| 94 | - */ |
|
| 95 | - public function certificates(): array |
|
| 96 | - { |
|
| 97 | - return $this->_certificates; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Get the trust anchor certificate from the path. |
|
| 102 | - * |
|
| 103 | - * @throws \LogicException If path is empty |
|
| 104 | - * |
|
| 105 | - * @return Certificate |
|
| 106 | - */ |
|
| 107 | - public function trustAnchorCertificate(): Certificate |
|
| 108 | - { |
|
| 109 | - if (!count($this->_certificates)) { |
|
| 110 | - throw new \LogicException('No certificates.'); |
|
| 111 | - } |
|
| 112 | - return $this->_certificates[0]; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Get the end-entity certificate from the path. |
|
| 117 | - * |
|
| 118 | - * @throws \LogicException If path is empty |
|
| 119 | - * |
|
| 120 | - * @return Certificate |
|
| 121 | - */ |
|
| 122 | - public function endEntityCertificate(): Certificate |
|
| 123 | - { |
|
| 124 | - if (!count($this->_certificates)) { |
|
| 125 | - throw new \LogicException('No certificates.'); |
|
| 126 | - } |
|
| 127 | - return $this->_certificates[count($this->_certificates) - 1]; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * Get certification path as a certificate chain. |
|
| 132 | - * |
|
| 133 | - * @return CertificateChain |
|
| 134 | - */ |
|
| 135 | - public function certificateChain(): CertificateChain |
|
| 136 | - { |
|
| 137 | - return new CertificateChain(...array_reverse($this->_certificates, false)); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Check whether certification path starts with one ore more given |
|
| 142 | - * certificates in parameter order. |
|
| 143 | - * |
|
| 144 | - * @param Certificate ...$certs Certificates |
|
| 145 | - * |
|
| 146 | - * @return bool |
|
| 147 | - */ |
|
| 148 | - public function startsWith(Certificate ...$certs): bool |
|
| 149 | - { |
|
| 150 | - $n = count($certs); |
|
| 151 | - if ($n > count($this->_certificates)) { |
|
| 152 | - return false; |
|
| 153 | - } |
|
| 154 | - for ($i = 0; $i < $n; ++$i) { |
|
| 155 | - if (!$certs[$i]->equals($this->_certificates[$i])) { |
|
| 156 | - return false; |
|
| 157 | - } |
|
| 158 | - } |
|
| 159 | - return true; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Validate certification path. |
|
| 164 | - * |
|
| 165 | - * @param PathValidationConfig $config |
|
| 166 | - * @param null|Crypto $crypto Crypto engine, use default if not set |
|
| 167 | - * |
|
| 168 | - * @throws Exception\PathValidationException |
|
| 169 | - * |
|
| 170 | - * @return PathValidationResult |
|
| 171 | - */ |
|
| 172 | - public function validate(PathValidationConfig $config, |
|
| 173 | - ?Crypto $crypto = null): PathValidationResult |
|
| 174 | - { |
|
| 175 | - $crypto = $crypto ?? Crypto::getDefault(); |
|
| 176 | - $validator = new PathValidator($crypto, $config, ...$this->_certificates); |
|
| 177 | - return $validator->validate(); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * @see \Countable::count() |
|
| 182 | - * |
|
| 183 | - * @return int |
|
| 184 | - */ |
|
| 185 | - public function count(): int |
|
| 186 | - { |
|
| 187 | - return count($this->_certificates); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Get iterator for certificates. |
|
| 192 | - * |
|
| 193 | - * @see \IteratorAggregate::getIterator() |
|
| 194 | - * |
|
| 195 | - * @return \ArrayIterator |
|
| 196 | - */ |
|
| 197 | - public function getIterator(): \ArrayIterator |
|
| 198 | - { |
|
| 199 | - return new \ArrayIterator($this->_certificates); |
|
| 200 | - } |
|
| 27 | + /** |
|
| 28 | + * Certification path. |
|
| 29 | + * |
|
| 30 | + * @var Certificate[] |
|
| 31 | + */ |
|
| 32 | + protected $_certificates; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Constructor. |
|
| 36 | + * |
|
| 37 | + * @param Certificate ...$certificates Certificates from the trust anchor |
|
| 38 | + * to the target end-entity certificate |
|
| 39 | + */ |
|
| 40 | + public function __construct(Certificate ...$certificates) |
|
| 41 | + { |
|
| 42 | + $this->_certificates = $certificates; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Initialize from a certificate chain. |
|
| 47 | + * |
|
| 48 | + * @param CertificateChain $chain |
|
| 49 | + * |
|
| 50 | + * @return self |
|
| 51 | + */ |
|
| 52 | + public static function fromCertificateChain(CertificateChain $chain): self |
|
| 53 | + { |
|
| 54 | + return new self(...array_reverse($chain->certificates(), false)); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Build certification path to given target. |
|
| 59 | + * |
|
| 60 | + * @param Certificate $target Target end-entity certificate |
|
| 61 | + * @param CertificateBundle $trust_anchors List of trust anchors |
|
| 62 | + * @param null|CertificateBundle $intermediate Optional intermediate certificates |
|
| 63 | + * |
|
| 64 | + * @return self |
|
| 65 | + */ |
|
| 66 | + public static function toTarget(Certificate $target, |
|
| 67 | + CertificateBundle $trust_anchors, ?CertificateBundle $intermediate = null): self |
|
| 68 | + { |
|
| 69 | + $builder = new CertificationPathBuilder($trust_anchors); |
|
| 70 | + return $builder->shortestPathToTarget($target, $intermediate); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Build certification path from given trust anchor to target certificate, |
|
| 75 | + * using intermediate certificates from given bundle. |
|
| 76 | + * |
|
| 77 | + * @param Certificate $trust_anchor Trust anchor certificate |
|
| 78 | + * @param Certificate $target Target end-entity certificate |
|
| 79 | + * @param null|CertificateBundle $intermediate Optional intermediate certificates |
|
| 80 | + * |
|
| 81 | + * @return self |
|
| 82 | + */ |
|
| 83 | + public static function fromTrustAnchorToTarget(Certificate $trust_anchor, |
|
| 84 | + Certificate $target, ?CertificateBundle $intermediate = null): self |
|
| 85 | + { |
|
| 86 | + return self::toTarget($target, new CertificateBundle($trust_anchor), |
|
| 87 | + $intermediate); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Get certificates. |
|
| 92 | + * |
|
| 93 | + * @return Certificate[] |
|
| 94 | + */ |
|
| 95 | + public function certificates(): array |
|
| 96 | + { |
|
| 97 | + return $this->_certificates; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Get the trust anchor certificate from the path. |
|
| 102 | + * |
|
| 103 | + * @throws \LogicException If path is empty |
|
| 104 | + * |
|
| 105 | + * @return Certificate |
|
| 106 | + */ |
|
| 107 | + public function trustAnchorCertificate(): Certificate |
|
| 108 | + { |
|
| 109 | + if (!count($this->_certificates)) { |
|
| 110 | + throw new \LogicException('No certificates.'); |
|
| 111 | + } |
|
| 112 | + return $this->_certificates[0]; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Get the end-entity certificate from the path. |
|
| 117 | + * |
|
| 118 | + * @throws \LogicException If path is empty |
|
| 119 | + * |
|
| 120 | + * @return Certificate |
|
| 121 | + */ |
|
| 122 | + public function endEntityCertificate(): Certificate |
|
| 123 | + { |
|
| 124 | + if (!count($this->_certificates)) { |
|
| 125 | + throw new \LogicException('No certificates.'); |
|
| 126 | + } |
|
| 127 | + return $this->_certificates[count($this->_certificates) - 1]; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * Get certification path as a certificate chain. |
|
| 132 | + * |
|
| 133 | + * @return CertificateChain |
|
| 134 | + */ |
|
| 135 | + public function certificateChain(): CertificateChain |
|
| 136 | + { |
|
| 137 | + return new CertificateChain(...array_reverse($this->_certificates, false)); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Check whether certification path starts with one ore more given |
|
| 142 | + * certificates in parameter order. |
|
| 143 | + * |
|
| 144 | + * @param Certificate ...$certs Certificates |
|
| 145 | + * |
|
| 146 | + * @return bool |
|
| 147 | + */ |
|
| 148 | + public function startsWith(Certificate ...$certs): bool |
|
| 149 | + { |
|
| 150 | + $n = count($certs); |
|
| 151 | + if ($n > count($this->_certificates)) { |
|
| 152 | + return false; |
|
| 153 | + } |
|
| 154 | + for ($i = 0; $i < $n; ++$i) { |
|
| 155 | + if (!$certs[$i]->equals($this->_certificates[$i])) { |
|
| 156 | + return false; |
|
| 157 | + } |
|
| 158 | + } |
|
| 159 | + return true; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Validate certification path. |
|
| 164 | + * |
|
| 165 | + * @param PathValidationConfig $config |
|
| 166 | + * @param null|Crypto $crypto Crypto engine, use default if not set |
|
| 167 | + * |
|
| 168 | + * @throws Exception\PathValidationException |
|
| 169 | + * |
|
| 170 | + * @return PathValidationResult |
|
| 171 | + */ |
|
| 172 | + public function validate(PathValidationConfig $config, |
|
| 173 | + ?Crypto $crypto = null): PathValidationResult |
|
| 174 | + { |
|
| 175 | + $crypto = $crypto ?? Crypto::getDefault(); |
|
| 176 | + $validator = new PathValidator($crypto, $config, ...$this->_certificates); |
|
| 177 | + return $validator->validate(); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * @see \Countable::count() |
|
| 182 | + * |
|
| 183 | + * @return int |
|
| 184 | + */ |
|
| 185 | + public function count(): int |
|
| 186 | + { |
|
| 187 | + return count($this->_certificates); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Get iterator for certificates. |
|
| 192 | + * |
|
| 193 | + * @see \IteratorAggregate::getIterator() |
|
| 194 | + * |
|
| 195 | + * @return \ArrayIterator |
|
| 196 | + */ |
|
| 197 | + public function getIterator(): \ArrayIterator |
|
| 198 | + { |
|
| 199 | + return new \ArrayIterator($this->_certificates); |
|
| 200 | + } |
|
| 201 | 201 | } |
@@ -19,110 +19,110 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | abstract class IPAddress extends GeneralName |
| 21 | 21 | { |
| 22 | - /** |
|
| 23 | - * IP address. |
|
| 24 | - * |
|
| 25 | - * @var string |
|
| 26 | - */ |
|
| 27 | - protected $_ip; |
|
| 22 | + /** |
|
| 23 | + * IP address. |
|
| 24 | + * |
|
| 25 | + * @var string |
|
| 26 | + */ |
|
| 27 | + protected $_ip; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Subnet mask. |
|
| 31 | - * |
|
| 32 | - * @var null|string |
|
| 33 | - */ |
|
| 34 | - protected $_mask; |
|
| 29 | + /** |
|
| 30 | + * Subnet mask. |
|
| 31 | + * |
|
| 32 | + * @var null|string |
|
| 33 | + */ |
|
| 34 | + protected $_mask; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Constructor. |
|
| 38 | - * |
|
| 39 | - * @param string $ip |
|
| 40 | - * @param null|string $mask |
|
| 41 | - */ |
|
| 42 | - public function __construct(string $ip, ?string $mask = null) |
|
| 43 | - { |
|
| 44 | - $this->_tag = self::TAG_IP_ADDRESS; |
|
| 45 | - $this->_ip = $ip; |
|
| 46 | - $this->_mask = $mask; |
|
| 47 | - } |
|
| 36 | + /** |
|
| 37 | + * Constructor. |
|
| 38 | + * |
|
| 39 | + * @param string $ip |
|
| 40 | + * @param null|string $mask |
|
| 41 | + */ |
|
| 42 | + public function __construct(string $ip, ?string $mask = null) |
|
| 43 | + { |
|
| 44 | + $this->_tag = self::TAG_IP_ADDRESS; |
|
| 45 | + $this->_ip = $ip; |
|
| 46 | + $this->_mask = $mask; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * {@inheritdoc} |
|
| 51 | - * |
|
| 52 | - * @return self |
|
| 53 | - */ |
|
| 54 | - public static function fromChosenASN1(UnspecifiedType $el): GeneralName |
|
| 55 | - { |
|
| 56 | - $octets = $el->asOctetString()->string(); |
|
| 57 | - switch (strlen($octets)) { |
|
| 58 | - case 4: |
|
| 59 | - case 8: |
|
| 60 | - return IPv4Address::fromOctets($octets); |
|
| 61 | - case 16: |
|
| 62 | - case 32: |
|
| 63 | - return IPv6Address::fromOctets($octets); |
|
| 64 | - default: |
|
| 65 | - throw new \UnexpectedValueException( |
|
| 66 | - 'Invalid octet length for IP address.'); |
|
| 67 | - } |
|
| 68 | - } |
|
| 49 | + /** |
|
| 50 | + * {@inheritdoc} |
|
| 51 | + * |
|
| 52 | + * @return self |
|
| 53 | + */ |
|
| 54 | + public static function fromChosenASN1(UnspecifiedType $el): GeneralName |
|
| 55 | + { |
|
| 56 | + $octets = $el->asOctetString()->string(); |
|
| 57 | + switch (strlen($octets)) { |
|
| 58 | + case 4: |
|
| 59 | + case 8: |
|
| 60 | + return IPv4Address::fromOctets($octets); |
|
| 61 | + case 16: |
|
| 62 | + case 32: |
|
| 63 | + return IPv6Address::fromOctets($octets); |
|
| 64 | + default: |
|
| 65 | + throw new \UnexpectedValueException( |
|
| 66 | + 'Invalid octet length for IP address.'); |
|
| 67 | + } |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * {@inheritdoc} |
|
| 72 | - */ |
|
| 73 | - public function string(): string |
|
| 74 | - { |
|
| 75 | - return $this->_ip . (isset($this->_mask) ? '/' . $this->_mask : ''); |
|
| 76 | - } |
|
| 70 | + /** |
|
| 71 | + * {@inheritdoc} |
|
| 72 | + */ |
|
| 73 | + public function string(): string |
|
| 74 | + { |
|
| 75 | + return $this->_ip . (isset($this->_mask) ? '/' . $this->_mask : ''); |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Get IP address as a string. |
|
| 80 | - * |
|
| 81 | - * @return string |
|
| 82 | - */ |
|
| 83 | - public function address(): string |
|
| 84 | - { |
|
| 85 | - return $this->_ip; |
|
| 86 | - } |
|
| 78 | + /** |
|
| 79 | + * Get IP address as a string. |
|
| 80 | + * |
|
| 81 | + * @return string |
|
| 82 | + */ |
|
| 83 | + public function address(): string |
|
| 84 | + { |
|
| 85 | + return $this->_ip; |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * Check whether mask is present. |
|
| 90 | - * |
|
| 91 | - * @return bool |
|
| 92 | - */ |
|
| 93 | - public function hasMask(): bool |
|
| 94 | - { |
|
| 95 | - return isset($this->_mask); |
|
| 96 | - } |
|
| 88 | + /** |
|
| 89 | + * Check whether mask is present. |
|
| 90 | + * |
|
| 91 | + * @return bool |
|
| 92 | + */ |
|
| 93 | + public function hasMask(): bool |
|
| 94 | + { |
|
| 95 | + return isset($this->_mask); |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - /** |
|
| 99 | - * Get subnet mask as a string. |
|
| 100 | - * |
|
| 101 | - * @throws \LogicException If not set |
|
| 102 | - * |
|
| 103 | - * @return string |
|
| 104 | - */ |
|
| 105 | - public function mask(): string |
|
| 106 | - { |
|
| 107 | - if (!$this->hasMask()) { |
|
| 108 | - throw new \LogicException('mask is not set.'); |
|
| 109 | - } |
|
| 110 | - return $this->_mask; |
|
| 111 | - } |
|
| 98 | + /** |
|
| 99 | + * Get subnet mask as a string. |
|
| 100 | + * |
|
| 101 | + * @throws \LogicException If not set |
|
| 102 | + * |
|
| 103 | + * @return string |
|
| 104 | + */ |
|
| 105 | + public function mask(): string |
|
| 106 | + { |
|
| 107 | + if (!$this->hasMask()) { |
|
| 108 | + throw new \LogicException('mask is not set.'); |
|
| 109 | + } |
|
| 110 | + return $this->_mask; |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - /** |
|
| 114 | - * Get octet representation of the IP address. |
|
| 115 | - * |
|
| 116 | - * @return string |
|
| 117 | - */ |
|
| 118 | - abstract protected function _octets(): string; |
|
| 113 | + /** |
|
| 114 | + * Get octet representation of the IP address. |
|
| 115 | + * |
|
| 116 | + * @return string |
|
| 117 | + */ |
|
| 118 | + abstract protected function _octets(): string; |
|
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * {@inheritdoc} |
|
| 122 | - */ |
|
| 123 | - protected function _choiceASN1(): TaggedType |
|
| 124 | - { |
|
| 125 | - return new ImplicitlyTaggedType($this->_tag, |
|
| 126 | - new OctetString($this->_octets())); |
|
| 127 | - } |
|
| 120 | + /** |
|
| 121 | + * {@inheritdoc} |
|
| 122 | + */ |
|
| 123 | + protected function _choiceASN1(): TaggedType |
|
| 124 | + { |
|
| 125 | + return new ImplicitlyTaggedType($this->_tag, |
|
| 126 | + new OctetString($this->_octets())); |
|
| 127 | + } |
|
| 128 | 128 | } |