@@ -16,137 +16,137 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class CertificationPathBuilder |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * Trust anchors. |
|
| 21 | - * |
|
| 22 | - * @var CertificateBundle |
|
| 23 | - */ |
|
| 24 | - protected $_trustList; |
|
| 19 | + /** |
|
| 20 | + * Trust anchors. |
|
| 21 | + * |
|
| 22 | + * @var CertificateBundle |
|
| 23 | + */ |
|
| 24 | + protected $_trustList; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Constructor. |
|
| 28 | - * |
|
| 29 | - * @param CertificateBundle $trust_list List of trust anchors |
|
| 30 | - */ |
|
| 31 | - public function __construct(CertificateBundle $trust_list) |
|
| 32 | - { |
|
| 33 | - $this->_trustList = $trust_list; |
|
| 34 | - } |
|
| 26 | + /** |
|
| 27 | + * Constructor. |
|
| 28 | + * |
|
| 29 | + * @param CertificateBundle $trust_list List of trust anchors |
|
| 30 | + */ |
|
| 31 | + public function __construct(CertificateBundle $trust_list) |
|
| 32 | + { |
|
| 33 | + $this->_trustList = $trust_list; |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Get all certification paths to given target certificate from |
|
| 38 | - * any trust anchor. |
|
| 39 | - * |
|
| 40 | - * @param Certificate $target Target certificate |
|
| 41 | - * @param CertificateBundle|null $intermediate Optional intermediate |
|
| 42 | - * certificates |
|
| 43 | - * @return CertificationPath[] |
|
| 44 | - */ |
|
| 45 | - public function allPathsToTarget(Certificate $target, |
|
| 46 | - CertificateBundle $intermediate = null): array |
|
| 47 | - { |
|
| 48 | - $paths = $this->_resolvePathsToTarget($target, $intermediate); |
|
| 49 | - // map paths to CertificationPath objects |
|
| 50 | - return array_map( |
|
| 51 | - function ($certs) { |
|
| 52 | - return new CertificationPath(...$certs); |
|
| 53 | - }, $paths); |
|
| 54 | - } |
|
| 36 | + /** |
|
| 37 | + * Get all certification paths to given target certificate from |
|
| 38 | + * any trust anchor. |
|
| 39 | + * |
|
| 40 | + * @param Certificate $target Target certificate |
|
| 41 | + * @param CertificateBundle|null $intermediate Optional intermediate |
|
| 42 | + * certificates |
|
| 43 | + * @return CertificationPath[] |
|
| 44 | + */ |
|
| 45 | + public function allPathsToTarget(Certificate $target, |
|
| 46 | + CertificateBundle $intermediate = null): array |
|
| 47 | + { |
|
| 48 | + $paths = $this->_resolvePathsToTarget($target, $intermediate); |
|
| 49 | + // map paths to CertificationPath objects |
|
| 50 | + return array_map( |
|
| 51 | + function ($certs) { |
|
| 52 | + return new CertificationPath(...$certs); |
|
| 53 | + }, $paths); |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Resolve all possible certification paths from any trust anchor to |
|
| 58 | - * the target certificate, using optional intermediate certificates. |
|
| 59 | - * |
|
| 60 | - * Helper method for allPathsToTarget to be called recursively. |
|
| 61 | - * |
|
| 62 | - * @todo Implement loop detection |
|
| 63 | - * @param Certificate $target |
|
| 64 | - * @param CertificateBundle $intermediate |
|
| 65 | - * @return array[] Array of arrays containing path certificates |
|
| 66 | - */ |
|
| 67 | - private function _resolvePathsToTarget(Certificate $target, |
|
| 68 | - CertificateBundle $intermediate = null): array |
|
| 69 | - { |
|
| 70 | - // array of possible paths |
|
| 71 | - $paths = array(); |
|
| 72 | - // signed by certificate in the trust list |
|
| 73 | - foreach ($this->_findIssuers($target, $this->_trustList) as $issuer) { |
|
| 74 | - // if target is self-signed, path consists of only |
|
| 75 | - // the target certificate |
|
| 76 | - if ($target->equals($issuer)) { |
|
| 77 | - $paths[] = array($target); |
|
| 78 | - } else { |
|
| 79 | - $paths[] = array($issuer, $target); |
|
| 80 | - } |
|
| 81 | - } |
|
| 82 | - if (isset($intermediate)) { |
|
| 83 | - // signed by intermediate certificate |
|
| 84 | - foreach ($this->_findIssuers($target, $intermediate) as $issuer) { |
|
| 85 | - // intermediate certificate must not be self-signed |
|
| 86 | - if ($issuer->isSelfIssued()) { |
|
| 87 | - continue; |
|
| 88 | - } |
|
| 89 | - // resolve paths to issuer |
|
| 90 | - $subpaths = $this->_resolvePathsToTarget($issuer, $intermediate); |
|
| 91 | - foreach ($subpaths as $path) { |
|
| 92 | - $paths[] = array_merge($path, array($target)); |
|
| 93 | - } |
|
| 94 | - } |
|
| 95 | - } |
|
| 96 | - return $paths; |
|
| 97 | - } |
|
| 56 | + /** |
|
| 57 | + * Resolve all possible certification paths from any trust anchor to |
|
| 58 | + * the target certificate, using optional intermediate certificates. |
|
| 59 | + * |
|
| 60 | + * Helper method for allPathsToTarget to be called recursively. |
|
| 61 | + * |
|
| 62 | + * @todo Implement loop detection |
|
| 63 | + * @param Certificate $target |
|
| 64 | + * @param CertificateBundle $intermediate |
|
| 65 | + * @return array[] Array of arrays containing path certificates |
|
| 66 | + */ |
|
| 67 | + private function _resolvePathsToTarget(Certificate $target, |
|
| 68 | + CertificateBundle $intermediate = null): array |
|
| 69 | + { |
|
| 70 | + // array of possible paths |
|
| 71 | + $paths = array(); |
|
| 72 | + // signed by certificate in the trust list |
|
| 73 | + foreach ($this->_findIssuers($target, $this->_trustList) as $issuer) { |
|
| 74 | + // if target is self-signed, path consists of only |
|
| 75 | + // the target certificate |
|
| 76 | + if ($target->equals($issuer)) { |
|
| 77 | + $paths[] = array($target); |
|
| 78 | + } else { |
|
| 79 | + $paths[] = array($issuer, $target); |
|
| 80 | + } |
|
| 81 | + } |
|
| 82 | + if (isset($intermediate)) { |
|
| 83 | + // signed by intermediate certificate |
|
| 84 | + foreach ($this->_findIssuers($target, $intermediate) as $issuer) { |
|
| 85 | + // intermediate certificate must not be self-signed |
|
| 86 | + if ($issuer->isSelfIssued()) { |
|
| 87 | + continue; |
|
| 88 | + } |
|
| 89 | + // resolve paths to issuer |
|
| 90 | + $subpaths = $this->_resolvePathsToTarget($issuer, $intermediate); |
|
| 91 | + foreach ($subpaths as $path) { |
|
| 92 | + $paths[] = array_merge($path, array($target)); |
|
| 93 | + } |
|
| 94 | + } |
|
| 95 | + } |
|
| 96 | + return $paths; |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * Get shortest path to given target certificate from any trust anchor. |
|
| 101 | - * |
|
| 102 | - * @param Certificate $target Target certificate |
|
| 103 | - * @param CertificateBundle|null $intermediate Optional intermediate |
|
| 104 | - * certificates |
|
| 105 | - * @throws PathBuildingException |
|
| 106 | - * @return CertificationPath |
|
| 107 | - */ |
|
| 108 | - public function shortestPathToTarget(Certificate $target, |
|
| 109 | - CertificateBundle $intermediate = null): CertificationPath |
|
| 110 | - { |
|
| 111 | - $paths = $this->allPathsToTarget($target, $intermediate); |
|
| 112 | - if (!count($paths)) { |
|
| 113 | - throw new PathBuildingException("No certification paths."); |
|
| 114 | - } |
|
| 115 | - usort($paths, |
|
| 116 | - function ($a, $b) { |
|
| 117 | - return count($a) < count($b) ? -1 : 1; |
|
| 118 | - }); |
|
| 119 | - return reset($paths); |
|
| 120 | - } |
|
| 99 | + /** |
|
| 100 | + * Get shortest path to given target certificate from any trust anchor. |
|
| 101 | + * |
|
| 102 | + * @param Certificate $target Target certificate |
|
| 103 | + * @param CertificateBundle|null $intermediate Optional intermediate |
|
| 104 | + * certificates |
|
| 105 | + * @throws PathBuildingException |
|
| 106 | + * @return CertificationPath |
|
| 107 | + */ |
|
| 108 | + public function shortestPathToTarget(Certificate $target, |
|
| 109 | + CertificateBundle $intermediate = null): CertificationPath |
|
| 110 | + { |
|
| 111 | + $paths = $this->allPathsToTarget($target, $intermediate); |
|
| 112 | + if (!count($paths)) { |
|
| 113 | + throw new PathBuildingException("No certification paths."); |
|
| 114 | + } |
|
| 115 | + usort($paths, |
|
| 116 | + function ($a, $b) { |
|
| 117 | + return count($a) < count($b) ? -1 : 1; |
|
| 118 | + }); |
|
| 119 | + return reset($paths); |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | - /** |
|
| 123 | - * Find all issuers of the target certificate from a given bundle. |
|
| 124 | - * |
|
| 125 | - * @param Certificate $target Target certificate |
|
| 126 | - * @param CertificateBundle $bundle Certificates to search |
|
| 127 | - * @return Certificate[] |
|
| 128 | - */ |
|
| 129 | - protected function _findIssuers(Certificate $target, |
|
| 130 | - CertificateBundle $bundle): array |
|
| 131 | - { |
|
| 132 | - $issuers = array(); |
|
| 133 | - $issuer_name = $target->tbsCertificate()->issuer(); |
|
| 134 | - $extensions = $target->tbsCertificate()->extensions(); |
|
| 135 | - // find by authority key identifier |
|
| 136 | - if ($extensions->hasAuthorityKeyIdentifier()) { |
|
| 137 | - $ext = $extensions->authorityKeyIdentifier(); |
|
| 138 | - if ($ext->hasKeyIdentifier()) { |
|
| 139 | - foreach ($bundle->allBySubjectKeyIdentifier( |
|
| 140 | - $ext->keyIdentifier()) as $issuer) { |
|
| 141 | - // check that issuer name matches |
|
| 142 | - if ($issuer->tbsCertificate() |
|
| 143 | - ->subject() |
|
| 144 | - ->equals($issuer_name)) { |
|
| 145 | - $issuers[] = $issuer; |
|
| 146 | - } |
|
| 147 | - } |
|
| 148 | - } |
|
| 149 | - } |
|
| 150 | - return $issuers; |
|
| 151 | - } |
|
| 122 | + /** |
|
| 123 | + * Find all issuers of the target certificate from a given bundle. |
|
| 124 | + * |
|
| 125 | + * @param Certificate $target Target certificate |
|
| 126 | + * @param CertificateBundle $bundle Certificates to search |
|
| 127 | + * @return Certificate[] |
|
| 128 | + */ |
|
| 129 | + protected function _findIssuers(Certificate $target, |
|
| 130 | + CertificateBundle $bundle): array |
|
| 131 | + { |
|
| 132 | + $issuers = array(); |
|
| 133 | + $issuer_name = $target->tbsCertificate()->issuer(); |
|
| 134 | + $extensions = $target->tbsCertificate()->extensions(); |
|
| 135 | + // find by authority key identifier |
|
| 136 | + if ($extensions->hasAuthorityKeyIdentifier()) { |
|
| 137 | + $ext = $extensions->authorityKeyIdentifier(); |
|
| 138 | + if ($ext->hasKeyIdentifier()) { |
|
| 139 | + foreach ($bundle->allBySubjectKeyIdentifier( |
|
| 140 | + $ext->keyIdentifier()) as $issuer) { |
|
| 141 | + // check that issuer name matches |
|
| 142 | + if ($issuer->tbsCertificate() |
|
| 143 | + ->subject() |
|
| 144 | + ->equals($issuer_name)) { |
|
| 145 | + $issuers[] = $issuer; |
|
| 146 | + } |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | + } |
|
| 150 | + return $issuers; |
|
| 151 | + } |
|
| 152 | 152 | } |
@@ -24,204 +24,204 @@ |
||
| 24 | 24 | */ |
| 25 | 25 | class CertificationRequestInfo |
| 26 | 26 | { |
| 27 | - const VERSION_1 = 0; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Version. |
|
| 31 | - * |
|
| 32 | - * @var int |
|
| 33 | - */ |
|
| 34 | - protected $_version; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Subject. |
|
| 38 | - * |
|
| 39 | - * @var Name $_subject |
|
| 40 | - */ |
|
| 41 | - protected $_subject; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Public key info. |
|
| 45 | - * |
|
| 46 | - * @var PublicKeyInfo $_subjectPKInfo |
|
| 47 | - */ |
|
| 48 | - protected $_subjectPKInfo; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Attributes. |
|
| 52 | - * |
|
| 53 | - * @var Attributes|null $_attributes |
|
| 54 | - */ |
|
| 55 | - protected $_attributes; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Constructor. |
|
| 59 | - * |
|
| 60 | - * @param Name $subject Subject |
|
| 61 | - * @param PublicKeyInfo $pkinfo Public key info |
|
| 62 | - */ |
|
| 63 | - public function __construct(Name $subject, PublicKeyInfo $pkinfo) |
|
| 64 | - { |
|
| 65 | - $this->_version = self::VERSION_1; |
|
| 66 | - $this->_subject = $subject; |
|
| 67 | - $this->_subjectPKInfo = $pkinfo; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Initialize from ASN.1. |
|
| 72 | - * |
|
| 73 | - * @param Sequence $seq |
|
| 74 | - * @throws \UnexpectedValueException |
|
| 75 | - * @return self |
|
| 76 | - */ |
|
| 77 | - public static function fromASN1(Sequence $seq): self |
|
| 78 | - { |
|
| 79 | - $version = $seq->at(0) |
|
| 80 | - ->asInteger() |
|
| 81 | - ->intNumber(); |
|
| 82 | - if ($version != self::VERSION_1) { |
|
| 83 | - throw new \UnexpectedValueException( |
|
| 84 | - "Version $version not supported."); |
|
| 85 | - } |
|
| 86 | - $subject = Name::fromASN1($seq->at(1)->asSequence()); |
|
| 87 | - $pkinfo = PublicKeyInfo::fromASN1($seq->at(2)->asSequence()); |
|
| 88 | - $obj = new self($subject, $pkinfo); |
|
| 89 | - if ($seq->hasTagged(0)) { |
|
| 90 | - $obj->_attributes = Attributes::fromASN1( |
|
| 91 | - $seq->getTagged(0) |
|
| 92 | - ->asImplicit(Element::TYPE_SET) |
|
| 93 | - ->asSet()); |
|
| 94 | - } |
|
| 95 | - return $obj; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Get version. |
|
| 100 | - * |
|
| 101 | - * @return int |
|
| 102 | - */ |
|
| 103 | - public function version(): int |
|
| 104 | - { |
|
| 105 | - return $this->_version; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Get self with subject. |
|
| 110 | - * |
|
| 111 | - * @param Name $subject |
|
| 112 | - * @return self |
|
| 113 | - */ |
|
| 114 | - public function withSubject(Name $subject): self |
|
| 115 | - { |
|
| 116 | - $obj = clone $this; |
|
| 117 | - $obj->_subject = $subject; |
|
| 118 | - return $obj; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Get subject. |
|
| 123 | - * |
|
| 124 | - * @return Name |
|
| 125 | - */ |
|
| 126 | - public function subject(): Name |
|
| 127 | - { |
|
| 128 | - return $this->_subject; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Get subject public key info. |
|
| 133 | - * |
|
| 134 | - * @return PublicKeyInfo |
|
| 135 | - */ |
|
| 136 | - public function subjectPKInfo(): PublicKeyInfo |
|
| 137 | - { |
|
| 138 | - return $this->_subjectPKInfo; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * Whether certification request info has attributes. |
|
| 143 | - * |
|
| 144 | - * @return bool |
|
| 145 | - */ |
|
| 146 | - public function hasAttributes(): bool |
|
| 147 | - { |
|
| 148 | - return isset($this->_attributes); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Get attributes. |
|
| 153 | - * |
|
| 154 | - * @throws \LogicException |
|
| 155 | - * @return Attributes |
|
| 156 | - */ |
|
| 157 | - public function attributes(): Attributes |
|
| 158 | - { |
|
| 159 | - if (!$this->hasAttributes()) { |
|
| 160 | - throw new \LogicException("No attributes."); |
|
| 161 | - } |
|
| 162 | - return $this->_attributes; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Get instance of self with attributes. |
|
| 167 | - * |
|
| 168 | - * @param Attributes $attribs |
|
| 169 | - */ |
|
| 170 | - public function withAttributes(Attributes $attribs): self |
|
| 171 | - { |
|
| 172 | - $obj = clone $this; |
|
| 173 | - $obj->_attributes = $attribs; |
|
| 174 | - return $obj; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * Get self with extension request attribute. |
|
| 179 | - * |
|
| 180 | - * @param Extensions $extensions Extensions to request |
|
| 181 | - * @return self |
|
| 182 | - */ |
|
| 183 | - public function withExtensionRequest(Extensions $extensions): self |
|
| 184 | - { |
|
| 185 | - $obj = clone $this; |
|
| 186 | - if (!isset($obj->_attributes)) { |
|
| 187 | - $obj->_attributes = new Attributes(); |
|
| 188 | - } |
|
| 189 | - $obj->_attributes = $obj->_attributes->withUnique( |
|
| 190 | - Attribute::fromAttributeValues( |
|
| 191 | - new ExtensionRequestValue($extensions))); |
|
| 192 | - return $obj; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Generate ASN.1 structure. |
|
| 197 | - * |
|
| 198 | - * @return Sequence |
|
| 199 | - */ |
|
| 200 | - public function toASN1(): Sequence |
|
| 201 | - { |
|
| 202 | - $elements = array(new Integer($this->_version), |
|
| 203 | - $this->_subject->toASN1(), $this->_subjectPKInfo->toASN1()); |
|
| 204 | - if (isset($this->_attributes)) { |
|
| 205 | - $elements[] = new ImplicitlyTaggedType(0, |
|
| 206 | - $this->_attributes->toASN1()); |
|
| 207 | - } |
|
| 208 | - return new Sequence(...$elements); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * Create signed CertificationRequest. |
|
| 213 | - * |
|
| 214 | - * @param SignatureAlgorithmIdentifier $algo Algorithm used for signing |
|
| 215 | - * @param PrivateKeyInfo $privkey_info Private key used for signing |
|
| 216 | - * @param Crypto|null $crypto Crypto engine, use default if not set |
|
| 217 | - * @return CertificationRequest |
|
| 218 | - */ |
|
| 219 | - public function sign(SignatureAlgorithmIdentifier $algo, |
|
| 220 | - PrivateKeyInfo $privkey_info, Crypto $crypto = null): CertificationRequest |
|
| 221 | - { |
|
| 222 | - $crypto = $crypto ?: Crypto::getDefault(); |
|
| 223 | - $data = $this->toASN1()->toDER(); |
|
| 224 | - $signature = $crypto->sign($data, $privkey_info, $algo); |
|
| 225 | - return new CertificationRequest($this, $algo, $signature); |
|
| 226 | - } |
|
| 27 | + const VERSION_1 = 0; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Version. |
|
| 31 | + * |
|
| 32 | + * @var int |
|
| 33 | + */ |
|
| 34 | + protected $_version; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Subject. |
|
| 38 | + * |
|
| 39 | + * @var Name $_subject |
|
| 40 | + */ |
|
| 41 | + protected $_subject; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Public key info. |
|
| 45 | + * |
|
| 46 | + * @var PublicKeyInfo $_subjectPKInfo |
|
| 47 | + */ |
|
| 48 | + protected $_subjectPKInfo; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Attributes. |
|
| 52 | + * |
|
| 53 | + * @var Attributes|null $_attributes |
|
| 54 | + */ |
|
| 55 | + protected $_attributes; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Constructor. |
|
| 59 | + * |
|
| 60 | + * @param Name $subject Subject |
|
| 61 | + * @param PublicKeyInfo $pkinfo Public key info |
|
| 62 | + */ |
|
| 63 | + public function __construct(Name $subject, PublicKeyInfo $pkinfo) |
|
| 64 | + { |
|
| 65 | + $this->_version = self::VERSION_1; |
|
| 66 | + $this->_subject = $subject; |
|
| 67 | + $this->_subjectPKInfo = $pkinfo; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Initialize from ASN.1. |
|
| 72 | + * |
|
| 73 | + * @param Sequence $seq |
|
| 74 | + * @throws \UnexpectedValueException |
|
| 75 | + * @return self |
|
| 76 | + */ |
|
| 77 | + public static function fromASN1(Sequence $seq): self |
|
| 78 | + { |
|
| 79 | + $version = $seq->at(0) |
|
| 80 | + ->asInteger() |
|
| 81 | + ->intNumber(); |
|
| 82 | + if ($version != self::VERSION_1) { |
|
| 83 | + throw new \UnexpectedValueException( |
|
| 84 | + "Version $version not supported."); |
|
| 85 | + } |
|
| 86 | + $subject = Name::fromASN1($seq->at(1)->asSequence()); |
|
| 87 | + $pkinfo = PublicKeyInfo::fromASN1($seq->at(2)->asSequence()); |
|
| 88 | + $obj = new self($subject, $pkinfo); |
|
| 89 | + if ($seq->hasTagged(0)) { |
|
| 90 | + $obj->_attributes = Attributes::fromASN1( |
|
| 91 | + $seq->getTagged(0) |
|
| 92 | + ->asImplicit(Element::TYPE_SET) |
|
| 93 | + ->asSet()); |
|
| 94 | + } |
|
| 95 | + return $obj; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Get version. |
|
| 100 | + * |
|
| 101 | + * @return int |
|
| 102 | + */ |
|
| 103 | + public function version(): int |
|
| 104 | + { |
|
| 105 | + return $this->_version; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Get self with subject. |
|
| 110 | + * |
|
| 111 | + * @param Name $subject |
|
| 112 | + * @return self |
|
| 113 | + */ |
|
| 114 | + public function withSubject(Name $subject): self |
|
| 115 | + { |
|
| 116 | + $obj = clone $this; |
|
| 117 | + $obj->_subject = $subject; |
|
| 118 | + return $obj; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Get subject. |
|
| 123 | + * |
|
| 124 | + * @return Name |
|
| 125 | + */ |
|
| 126 | + public function subject(): Name |
|
| 127 | + { |
|
| 128 | + return $this->_subject; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Get subject public key info. |
|
| 133 | + * |
|
| 134 | + * @return PublicKeyInfo |
|
| 135 | + */ |
|
| 136 | + public function subjectPKInfo(): PublicKeyInfo |
|
| 137 | + { |
|
| 138 | + return $this->_subjectPKInfo; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * Whether certification request info has attributes. |
|
| 143 | + * |
|
| 144 | + * @return bool |
|
| 145 | + */ |
|
| 146 | + public function hasAttributes(): bool |
|
| 147 | + { |
|
| 148 | + return isset($this->_attributes); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Get attributes. |
|
| 153 | + * |
|
| 154 | + * @throws \LogicException |
|
| 155 | + * @return Attributes |
|
| 156 | + */ |
|
| 157 | + public function attributes(): Attributes |
|
| 158 | + { |
|
| 159 | + if (!$this->hasAttributes()) { |
|
| 160 | + throw new \LogicException("No attributes."); |
|
| 161 | + } |
|
| 162 | + return $this->_attributes; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Get instance of self with attributes. |
|
| 167 | + * |
|
| 168 | + * @param Attributes $attribs |
|
| 169 | + */ |
|
| 170 | + public function withAttributes(Attributes $attribs): self |
|
| 171 | + { |
|
| 172 | + $obj = clone $this; |
|
| 173 | + $obj->_attributes = $attribs; |
|
| 174 | + return $obj; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * Get self with extension request attribute. |
|
| 179 | + * |
|
| 180 | + * @param Extensions $extensions Extensions to request |
|
| 181 | + * @return self |
|
| 182 | + */ |
|
| 183 | + public function withExtensionRequest(Extensions $extensions): self |
|
| 184 | + { |
|
| 185 | + $obj = clone $this; |
|
| 186 | + if (!isset($obj->_attributes)) { |
|
| 187 | + $obj->_attributes = new Attributes(); |
|
| 188 | + } |
|
| 189 | + $obj->_attributes = $obj->_attributes->withUnique( |
|
| 190 | + Attribute::fromAttributeValues( |
|
| 191 | + new ExtensionRequestValue($extensions))); |
|
| 192 | + return $obj; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Generate ASN.1 structure. |
|
| 197 | + * |
|
| 198 | + * @return Sequence |
|
| 199 | + */ |
|
| 200 | + public function toASN1(): Sequence |
|
| 201 | + { |
|
| 202 | + $elements = array(new Integer($this->_version), |
|
| 203 | + $this->_subject->toASN1(), $this->_subjectPKInfo->toASN1()); |
|
| 204 | + if (isset($this->_attributes)) { |
|
| 205 | + $elements[] = new ImplicitlyTaggedType(0, |
|
| 206 | + $this->_attributes->toASN1()); |
|
| 207 | + } |
|
| 208 | + return new Sequence(...$elements); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * Create signed CertificationRequest. |
|
| 213 | + * |
|
| 214 | + * @param SignatureAlgorithmIdentifier $algo Algorithm used for signing |
|
| 215 | + * @param PrivateKeyInfo $privkey_info Private key used for signing |
|
| 216 | + * @param Crypto|null $crypto Crypto engine, use default if not set |
|
| 217 | + * @return CertificationRequest |
|
| 218 | + */ |
|
| 219 | + public function sign(SignatureAlgorithmIdentifier $algo, |
|
| 220 | + PrivateKeyInfo $privkey_info, Crypto $crypto = null): CertificationRequest |
|
| 221 | + { |
|
| 222 | + $crypto = $crypto ?: Crypto::getDefault(); |
|
| 223 | + $data = $this->toASN1()->toDER(); |
|
| 224 | + $signature = $crypto->sign($data, $privkey_info, $algo); |
|
| 225 | + return new CertificationRequest($this, $algo, $signature); |
|
| 226 | + } |
|
| 227 | 227 | } |
@@ -17,94 +17,94 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class ExtensionRequestValue extends AttributeValue |
| 19 | 19 | { |
| 20 | - const OID = "1.2.840.113549.1.9.14"; |
|
| 20 | + const OID = "1.2.840.113549.1.9.14"; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Extensions. |
|
| 24 | - * |
|
| 25 | - * @var Extensions $_extensions |
|
| 26 | - */ |
|
| 27 | - protected $_extensions; |
|
| 22 | + /** |
|
| 23 | + * Extensions. |
|
| 24 | + * |
|
| 25 | + * @var Extensions $_extensions |
|
| 26 | + */ |
|
| 27 | + protected $_extensions; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Constructor. |
|
| 31 | - * |
|
| 32 | - * @param Extensions $extensions |
|
| 33 | - */ |
|
| 34 | - public function __construct(Extensions $extensions) |
|
| 35 | - { |
|
| 36 | - $this->_extensions = $extensions; |
|
| 37 | - $this->_oid = self::OID; |
|
| 38 | - } |
|
| 29 | + /** |
|
| 30 | + * Constructor. |
|
| 31 | + * |
|
| 32 | + * @param Extensions $extensions |
|
| 33 | + */ |
|
| 34 | + public function __construct(Extensions $extensions) |
|
| 35 | + { |
|
| 36 | + $this->_extensions = $extensions; |
|
| 37 | + $this->_oid = self::OID; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * |
|
| 42 | - * @see \X501\ASN1\AttributeValue\AttributeValue::fromASN1() |
|
| 43 | - * @param UnspecifiedType $el |
|
| 44 | - * @return self |
|
| 45 | - */ |
|
| 46 | - public static function fromASN1(UnspecifiedType $el): self |
|
| 47 | - { |
|
| 48 | - return new self(Extensions::fromASN1($el->asSequence())); |
|
| 49 | - } |
|
| 40 | + /** |
|
| 41 | + * |
|
| 42 | + * @see \X501\ASN1\AttributeValue\AttributeValue::fromASN1() |
|
| 43 | + * @param UnspecifiedType $el |
|
| 44 | + * @return self |
|
| 45 | + */ |
|
| 46 | + public static function fromASN1(UnspecifiedType $el): self |
|
| 47 | + { |
|
| 48 | + return new self(Extensions::fromASN1($el->asSequence())); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Get requested extensions. |
|
| 53 | - * |
|
| 54 | - * @return Extensions |
|
| 55 | - */ |
|
| 56 | - public function extensions(): Extensions |
|
| 57 | - { |
|
| 58 | - return $this->_extensions; |
|
| 59 | - } |
|
| 51 | + /** |
|
| 52 | + * Get requested extensions. |
|
| 53 | + * |
|
| 54 | + * @return Extensions |
|
| 55 | + */ |
|
| 56 | + public function extensions(): Extensions |
|
| 57 | + { |
|
| 58 | + return $this->_extensions; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * |
|
| 63 | - * @see \X501\ASN1\AttributeValue\AttributeValue::toASN1() |
|
| 64 | - * @return Sequence |
|
| 65 | - */ |
|
| 66 | - public function toASN1(): Sequence |
|
| 67 | - { |
|
| 68 | - return $this->_extensions->toASN1(); |
|
| 69 | - } |
|
| 61 | + /** |
|
| 62 | + * |
|
| 63 | + * @see \X501\ASN1\AttributeValue\AttributeValue::toASN1() |
|
| 64 | + * @return Sequence |
|
| 65 | + */ |
|
| 66 | + public function toASN1(): Sequence |
|
| 67 | + { |
|
| 68 | + return $this->_extensions->toASN1(); |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * |
|
| 73 | - * @see \X501\ASN1\AttributeValue\AttributeValue::stringValue() |
|
| 74 | - * @return string |
|
| 75 | - */ |
|
| 76 | - public function stringValue(): string |
|
| 77 | - { |
|
| 78 | - return "#" . bin2hex($this->toASN1()->toDER()); |
|
| 79 | - } |
|
| 71 | + /** |
|
| 72 | + * |
|
| 73 | + * @see \X501\ASN1\AttributeValue\AttributeValue::stringValue() |
|
| 74 | + * @return string |
|
| 75 | + */ |
|
| 76 | + public function stringValue(): string |
|
| 77 | + { |
|
| 78 | + return "#" . bin2hex($this->toASN1()->toDER()); |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * |
|
| 83 | - * @see \X501\ASN1\AttributeValue\AttributeValue::equalityMatchingRule() |
|
| 84 | - * @return BinaryMatch |
|
| 85 | - */ |
|
| 86 | - public function equalityMatchingRule(): BinaryMatch |
|
| 87 | - { |
|
| 88 | - return new BinaryMatch(); |
|
| 89 | - } |
|
| 81 | + /** |
|
| 82 | + * |
|
| 83 | + * @see \X501\ASN1\AttributeValue\AttributeValue::equalityMatchingRule() |
|
| 84 | + * @return BinaryMatch |
|
| 85 | + */ |
|
| 86 | + public function equalityMatchingRule(): BinaryMatch |
|
| 87 | + { |
|
| 88 | + return new BinaryMatch(); |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * |
|
| 93 | - * @see \X501\ASN1\AttributeValue\AttributeValue::rfc2253String() |
|
| 94 | - * @return string |
|
| 95 | - */ |
|
| 96 | - public function rfc2253String(): string |
|
| 97 | - { |
|
| 98 | - return $this->stringValue(); |
|
| 99 | - } |
|
| 91 | + /** |
|
| 92 | + * |
|
| 93 | + * @see \X501\ASN1\AttributeValue\AttributeValue::rfc2253String() |
|
| 94 | + * @return string |
|
| 95 | + */ |
|
| 96 | + public function rfc2253String(): string |
|
| 97 | + { |
|
| 98 | + return $this->stringValue(); |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * |
|
| 103 | - * @see \X501\ASN1\AttributeValue\AttributeValue::_transcodedString() |
|
| 104 | - * @return string |
|
| 105 | - */ |
|
| 106 | - protected function _transcodedString(): string |
|
| 107 | - { |
|
| 108 | - return $this->stringValue(); |
|
| 109 | - } |
|
| 101 | + /** |
|
| 102 | + * |
|
| 103 | + * @see \X501\ASN1\AttributeValue\AttributeValue::_transcodedString() |
|
| 104 | + * @return string |
|
| 105 | + */ |
|
| 106 | + protected function _transcodedString(): string |
|
| 107 | + { |
|
| 108 | + return $this->stringValue(); |
|
| 109 | + } |
|
| 110 | 110 | } |
@@ -14,136 +14,136 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | trait AttributeContainer |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * Array of attributes. |
|
| 19 | - * |
|
| 20 | - * @var Attribute[] $_attributes |
|
| 21 | - */ |
|
| 22 | - protected $_attributes; |
|
| 17 | + /** |
|
| 18 | + * Array of attributes. |
|
| 19 | + * |
|
| 20 | + * @var Attribute[] $_attributes |
|
| 21 | + */ |
|
| 22 | + protected $_attributes; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Find first attribute of given name or OID. |
|
| 26 | - * |
|
| 27 | - * @param string $name |
|
| 28 | - * @return Attribute|null |
|
| 29 | - */ |
|
| 30 | - protected function _findFirst(string $name) |
|
| 31 | - { |
|
| 32 | - $oid = AttributeType::attrNameToOID($name); |
|
| 33 | - foreach ($this->_attributes as $attr) { |
|
| 34 | - if ($attr->oid() == $oid) { |
|
| 35 | - return $attr; |
|
| 36 | - } |
|
| 37 | - } |
|
| 38 | - return null; |
|
| 39 | - } |
|
| 24 | + /** |
|
| 25 | + * Find first attribute of given name or OID. |
|
| 26 | + * |
|
| 27 | + * @param string $name |
|
| 28 | + * @return Attribute|null |
|
| 29 | + */ |
|
| 30 | + protected function _findFirst(string $name) |
|
| 31 | + { |
|
| 32 | + $oid = AttributeType::attrNameToOID($name); |
|
| 33 | + foreach ($this->_attributes as $attr) { |
|
| 34 | + if ($attr->oid() == $oid) { |
|
| 35 | + return $attr; |
|
| 36 | + } |
|
| 37 | + } |
|
| 38 | + return null; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Check whether attribute is present. |
|
| 43 | - * |
|
| 44 | - * @param string $name OID or attribute name |
|
| 45 | - * @return boolean |
|
| 46 | - */ |
|
| 47 | - public function has(string $name): bool |
|
| 48 | - { |
|
| 49 | - return null !== $this->_findFirst($name); |
|
| 50 | - } |
|
| 41 | + /** |
|
| 42 | + * Check whether attribute is present. |
|
| 43 | + * |
|
| 44 | + * @param string $name OID or attribute name |
|
| 45 | + * @return boolean |
|
| 46 | + */ |
|
| 47 | + public function has(string $name): bool |
|
| 48 | + { |
|
| 49 | + return null !== $this->_findFirst($name); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Get first attribute by OID or attribute name. |
|
| 54 | - * |
|
| 55 | - * @param string $name OID or attribute name |
|
| 56 | - * @throws \OutOfBoundsException |
|
| 57 | - * @return Attribute |
|
| 58 | - */ |
|
| 59 | - public function firstOf(string $name): Attribute |
|
| 60 | - { |
|
| 61 | - $attr = $this->_findFirst($name); |
|
| 62 | - if (!$attr) { |
|
| 63 | - throw new \UnexpectedValueException("No $name attribute."); |
|
| 64 | - } |
|
| 65 | - return $attr; |
|
| 66 | - } |
|
| 52 | + /** |
|
| 53 | + * Get first attribute by OID or attribute name. |
|
| 54 | + * |
|
| 55 | + * @param string $name OID or attribute name |
|
| 56 | + * @throws \OutOfBoundsException |
|
| 57 | + * @return Attribute |
|
| 58 | + */ |
|
| 59 | + public function firstOf(string $name): Attribute |
|
| 60 | + { |
|
| 61 | + $attr = $this->_findFirst($name); |
|
| 62 | + if (!$attr) { |
|
| 63 | + throw new \UnexpectedValueException("No $name attribute."); |
|
| 64 | + } |
|
| 65 | + return $attr; |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * Get all attributes of given name. |
|
| 70 | - * |
|
| 71 | - * @param string $name OID or attribute name |
|
| 72 | - * @return Attribute[] |
|
| 73 | - */ |
|
| 74 | - public function allOf(string $name): array |
|
| 75 | - { |
|
| 76 | - $oid = AttributeType::attrNameToOID($name); |
|
| 77 | - $attrs = array_filter($this->_attributes, |
|
| 78 | - function (Attribute $attr) use ($oid) { |
|
| 79 | - return $attr->oid() == $oid; |
|
| 80 | - }); |
|
| 81 | - return array_values($attrs); |
|
| 82 | - } |
|
| 68 | + /** |
|
| 69 | + * Get all attributes of given name. |
|
| 70 | + * |
|
| 71 | + * @param string $name OID or attribute name |
|
| 72 | + * @return Attribute[] |
|
| 73 | + */ |
|
| 74 | + public function allOf(string $name): array |
|
| 75 | + { |
|
| 76 | + $oid = AttributeType::attrNameToOID($name); |
|
| 77 | + $attrs = array_filter($this->_attributes, |
|
| 78 | + function (Attribute $attr) use ($oid) { |
|
| 79 | + return $attr->oid() == $oid; |
|
| 80 | + }); |
|
| 81 | + return array_values($attrs); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * Get all attributes. |
|
| 86 | - * |
|
| 87 | - * @return Attribute[] |
|
| 88 | - */ |
|
| 89 | - public function all(): array |
|
| 90 | - { |
|
| 91 | - return $this->_attributes; |
|
| 92 | - } |
|
| 84 | + /** |
|
| 85 | + * Get all attributes. |
|
| 86 | + * |
|
| 87 | + * @return Attribute[] |
|
| 88 | + */ |
|
| 89 | + public function all(): array |
|
| 90 | + { |
|
| 91 | + return $this->_attributes; |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * Get self with additional attributes added. |
|
| 96 | - * |
|
| 97 | - * @param Attribute ...$attribs |
|
| 98 | - * @return self |
|
| 99 | - */ |
|
| 100 | - public function withAdditional(Attribute ...$attribs): self |
|
| 101 | - { |
|
| 102 | - $obj = clone $this; |
|
| 103 | - foreach ($attribs as $attr) { |
|
| 104 | - $obj->_attributes[] = $attr; |
|
| 105 | - } |
|
| 106 | - return $obj; |
|
| 107 | - } |
|
| 94 | + /** |
|
| 95 | + * Get self with additional attributes added. |
|
| 96 | + * |
|
| 97 | + * @param Attribute ...$attribs |
|
| 98 | + * @return self |
|
| 99 | + */ |
|
| 100 | + public function withAdditional(Attribute ...$attribs): self |
|
| 101 | + { |
|
| 102 | + $obj = clone $this; |
|
| 103 | + foreach ($attribs as $attr) { |
|
| 104 | + $obj->_attributes[] = $attr; |
|
| 105 | + } |
|
| 106 | + return $obj; |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - /** |
|
| 110 | - * Get self with single unique attribute added. |
|
| 111 | - * |
|
| 112 | - * All previous attributes of the same type are removed. |
|
| 113 | - * |
|
| 114 | - * @param Attribute $attr |
|
| 115 | - * @return self |
|
| 116 | - */ |
|
| 117 | - public function withUnique(Attribute $attr): self |
|
| 118 | - { |
|
| 119 | - $obj = clone $this; |
|
| 120 | - $obj->_attributes = array_filter($obj->_attributes, |
|
| 121 | - function (Attribute $a) use ($attr) { |
|
| 122 | - return $a->oid() != $attr->oid(); |
|
| 123 | - }); |
|
| 124 | - $obj->_attributes[] = $attr; |
|
| 125 | - return $obj; |
|
| 126 | - } |
|
| 109 | + /** |
|
| 110 | + * Get self with single unique attribute added. |
|
| 111 | + * |
|
| 112 | + * All previous attributes of the same type are removed. |
|
| 113 | + * |
|
| 114 | + * @param Attribute $attr |
|
| 115 | + * @return self |
|
| 116 | + */ |
|
| 117 | + public function withUnique(Attribute $attr): self |
|
| 118 | + { |
|
| 119 | + $obj = clone $this; |
|
| 120 | + $obj->_attributes = array_filter($obj->_attributes, |
|
| 121 | + function (Attribute $a) use ($attr) { |
|
| 122 | + return $a->oid() != $attr->oid(); |
|
| 123 | + }); |
|
| 124 | + $obj->_attributes[] = $attr; |
|
| 125 | + return $obj; |
|
| 126 | + } |
|
| 127 | 127 | |
| 128 | - /** |
|
| 129 | - * Get number of attributes. |
|
| 130 | - * |
|
| 131 | - * @see \Countable::count() |
|
| 132 | - * @return int |
|
| 133 | - */ |
|
| 134 | - public function count(): int |
|
| 135 | - { |
|
| 136 | - return count($this->_attributes); |
|
| 137 | - } |
|
| 128 | + /** |
|
| 129 | + * Get number of attributes. |
|
| 130 | + * |
|
| 131 | + * @see \Countable::count() |
|
| 132 | + * @return int |
|
| 133 | + */ |
|
| 134 | + public function count(): int |
|
| 135 | + { |
|
| 136 | + return count($this->_attributes); |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | - /** |
|
| 140 | - * Get iterator for attributes. |
|
| 141 | - * |
|
| 142 | - * @see \IteratorAggregate::getIterator() |
|
| 143 | - * @return \ArrayIterator |
|
| 144 | - */ |
|
| 145 | - public function getIterator(): \ArrayIterator |
|
| 146 | - { |
|
| 147 | - return new \ArrayIterator($this->_attributes); |
|
| 148 | - } |
|
| 139 | + /** |
|
| 140 | + * Get iterator for attributes. |
|
| 141 | + * |
|
| 142 | + * @see \IteratorAggregate::getIterator() |
|
| 143 | + * @return \ArrayIterator |
|
| 144 | + */ |
|
| 145 | + public function getIterator(): \ArrayIterator |
|
| 146 | + { |
|
| 147 | + return new \ArrayIterator($this->_attributes); |
|
| 148 | + } |
|
| 149 | 149 | } |
@@ -9,69 +9,69 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | trait DateTimeHelper |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Create DateTime object from time string and timezone. |
|
| 14 | - * |
|
| 15 | - * @param string|null $time Time string, default to 'now' |
|
| 16 | - * @param string|null $tz Timezone, default if omitted |
|
| 17 | - * @throws \RuntimeException |
|
| 18 | - * @return \DateTimeImmutable |
|
| 19 | - */ |
|
| 20 | - private static function _createDateTime($time = null, $tz = null): \DateTimeImmutable |
|
| 21 | - { |
|
| 22 | - if (!isset($time)) { |
|
| 23 | - $time = 'now'; |
|
| 24 | - } |
|
| 25 | - if (!isset($tz)) { |
|
| 26 | - $tz = date_default_timezone_get(); |
|
| 27 | - } |
|
| 28 | - try { |
|
| 29 | - $dt = new \DateTimeImmutable($time, self::_createTimeZone($tz)); |
|
| 30 | - return self::_roundDownFractionalSeconds($dt); |
|
| 31 | - } catch (\Exception $e) { |
|
| 32 | - throw new \RuntimeException( |
|
| 33 | - "Failed to create DateTime: " . |
|
| 34 | - self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
| 35 | - } |
|
| 36 | - } |
|
| 12 | + /** |
|
| 13 | + * Create DateTime object from time string and timezone. |
|
| 14 | + * |
|
| 15 | + * @param string|null $time Time string, default to 'now' |
|
| 16 | + * @param string|null $tz Timezone, default if omitted |
|
| 17 | + * @throws \RuntimeException |
|
| 18 | + * @return \DateTimeImmutable |
|
| 19 | + */ |
|
| 20 | + private static function _createDateTime($time = null, $tz = null): \DateTimeImmutable |
|
| 21 | + { |
|
| 22 | + if (!isset($time)) { |
|
| 23 | + $time = 'now'; |
|
| 24 | + } |
|
| 25 | + if (!isset($tz)) { |
|
| 26 | + $tz = date_default_timezone_get(); |
|
| 27 | + } |
|
| 28 | + try { |
|
| 29 | + $dt = new \DateTimeImmutable($time, self::_createTimeZone($tz)); |
|
| 30 | + return self::_roundDownFractionalSeconds($dt); |
|
| 31 | + } catch (\Exception $e) { |
|
| 32 | + throw new \RuntimeException( |
|
| 33 | + "Failed to create DateTime: " . |
|
| 34 | + self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
| 35 | + } |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Rounds a \DateTimeImmutable value such that fractional |
|
| 40 | - * seconds are removed. |
|
| 41 | - * |
|
| 42 | - * @param \DateTimeImmutable $dt |
|
| 43 | - * @return \DateTimeImmutable |
|
| 44 | - */ |
|
| 45 | - private static function _roundDownFractionalSeconds(\DateTimeImmutable $dt): \DateTimeImmutable |
|
| 46 | - { |
|
| 47 | - return \DateTimeImmutable::createFromFormat("Y-m-d H:i:s", |
|
| 48 | - $dt->format("Y-m-d H:i:s"), $dt->getTimezone()); |
|
| 49 | - } |
|
| 38 | + /** |
|
| 39 | + * Rounds a \DateTimeImmutable value such that fractional |
|
| 40 | + * seconds are removed. |
|
| 41 | + * |
|
| 42 | + * @param \DateTimeImmutable $dt |
|
| 43 | + * @return \DateTimeImmutable |
|
| 44 | + */ |
|
| 45 | + private static function _roundDownFractionalSeconds(\DateTimeImmutable $dt): \DateTimeImmutable |
|
| 46 | + { |
|
| 47 | + return \DateTimeImmutable::createFromFormat("Y-m-d H:i:s", |
|
| 48 | + $dt->format("Y-m-d H:i:s"), $dt->getTimezone()); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Create DateTimeZone object from string. |
|
| 53 | - * |
|
| 54 | - * @param string $tz |
|
| 55 | - * @throws \UnexpectedValueException |
|
| 56 | - * @return \DateTimeZone |
|
| 57 | - */ |
|
| 58 | - private static function _createTimeZone(string $tz): \DateTimeZone |
|
| 59 | - { |
|
| 60 | - try { |
|
| 61 | - return new \DateTimeZone($tz); |
|
| 62 | - } catch (\Exception $e) { |
|
| 63 | - throw new \UnexpectedValueException("Invalid timezone.", 0, $e); |
|
| 64 | - } |
|
| 65 | - } |
|
| 51 | + /** |
|
| 52 | + * Create DateTimeZone object from string. |
|
| 53 | + * |
|
| 54 | + * @param string $tz |
|
| 55 | + * @throws \UnexpectedValueException |
|
| 56 | + * @return \DateTimeZone |
|
| 57 | + */ |
|
| 58 | + private static function _createTimeZone(string $tz): \DateTimeZone |
|
| 59 | + { |
|
| 60 | + try { |
|
| 61 | + return new \DateTimeZone($tz); |
|
| 62 | + } catch (\Exception $e) { |
|
| 63 | + throw new \UnexpectedValueException("Invalid timezone.", 0, $e); |
|
| 64 | + } |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Get last error caused by DateTimeImmutable. |
|
| 69 | - * |
|
| 70 | - * @return string |
|
| 71 | - */ |
|
| 72 | - private static function _getLastDateTimeImmutableErrorsStr(): string |
|
| 73 | - { |
|
| 74 | - $errors = \DateTimeImmutable::getLastErrors()["errors"]; |
|
| 75 | - return implode(", ", $errors); |
|
| 76 | - } |
|
| 67 | + /** |
|
| 68 | + * Get last error caused by DateTimeImmutable. |
|
| 69 | + * |
|
| 70 | + * @return string |
|
| 71 | + */ |
|
| 72 | + private static function _getLastDateTimeImmutableErrorsStr(): string |
|
| 73 | + { |
|
| 74 | + $errors = \DateTimeImmutable::getLastErrors()["errors"]; |
|
| 75 | + return implode(", ", $errors); |
|
| 76 | + } |
|
| 77 | 77 | } |
@@ -15,147 +15,147 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | abstract class GeneralName |
| 17 | 17 | { |
| 18 | - // GeneralName CHOICE tags |
|
| 19 | - const TAG_OTHER_NAME = 0; |
|
| 20 | - const TAG_RFC822_NAME = 1; |
|
| 21 | - const TAG_DNS_NAME = 2; |
|
| 22 | - const TAG_X400_ADDRESS = 3; |
|
| 23 | - const TAG_DIRECTORY_NAME = 4; |
|
| 24 | - const TAG_EDI_PARTY_NAME = 5; |
|
| 25 | - const TAG_URI = 6; |
|
| 26 | - const TAG_IP_ADDRESS = 7; |
|
| 27 | - const TAG_REGISTERED_ID = 8; |
|
| 18 | + // GeneralName CHOICE tags |
|
| 19 | + const TAG_OTHER_NAME = 0; |
|
| 20 | + const TAG_RFC822_NAME = 1; |
|
| 21 | + const TAG_DNS_NAME = 2; |
|
| 22 | + const TAG_X400_ADDRESS = 3; |
|
| 23 | + const TAG_DIRECTORY_NAME = 4; |
|
| 24 | + const TAG_EDI_PARTY_NAME = 5; |
|
| 25 | + const TAG_URI = 6; |
|
| 26 | + const TAG_IP_ADDRESS = 7; |
|
| 27 | + const TAG_REGISTERED_ID = 8; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Chosen tag. |
|
| 31 | - * |
|
| 32 | - * @var int $_tag |
|
| 33 | - */ |
|
| 34 | - protected $_tag; |
|
| 29 | + /** |
|
| 30 | + * Chosen tag. |
|
| 31 | + * |
|
| 32 | + * @var int $_tag |
|
| 33 | + */ |
|
| 34 | + protected $_tag; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Get string value of the type. |
|
| 38 | - * |
|
| 39 | - * @return string |
|
| 40 | - */ |
|
| 41 | - abstract public function string(): string; |
|
| 36 | + /** |
|
| 37 | + * Get string value of the type. |
|
| 38 | + * |
|
| 39 | + * @return string |
|
| 40 | + */ |
|
| 41 | + abstract public function string(): string; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Get ASN.1 value in GeneralName CHOICE context. |
|
| 45 | - * |
|
| 46 | - * @return TaggedType |
|
| 47 | - */ |
|
| 48 | - abstract protected function _choiceASN1(): TaggedType; |
|
| 43 | + /** |
|
| 44 | + * Get ASN.1 value in GeneralName CHOICE context. |
|
| 45 | + * |
|
| 46 | + * @return TaggedType |
|
| 47 | + */ |
|
| 48 | + abstract protected function _choiceASN1(): TaggedType; |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Initialize concrete object from the chosen ASN.1 element. |
|
| 52 | - * |
|
| 53 | - * @param UnspecifiedType $el |
|
| 54 | - * @return self |
|
| 55 | - */ |
|
| 56 | - public static function fromChosenASN1(UnspecifiedType $el) |
|
| 57 | - { |
|
| 58 | - throw new \BadMethodCallException( |
|
| 59 | - __FUNCTION__ . " must be implemented in the derived class."); |
|
| 60 | - } |
|
| 50 | + /** |
|
| 51 | + * Initialize concrete object from the chosen ASN.1 element. |
|
| 52 | + * |
|
| 53 | + * @param UnspecifiedType $el |
|
| 54 | + * @return self |
|
| 55 | + */ |
|
| 56 | + public static function fromChosenASN1(UnspecifiedType $el) |
|
| 57 | + { |
|
| 58 | + throw new \BadMethodCallException( |
|
| 59 | + __FUNCTION__ . " must be implemented in the derived class."); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Initialize from ASN.1. |
|
| 64 | - * |
|
| 65 | - * @param TaggedType $el |
|
| 66 | - * @throws \UnexpectedValueException |
|
| 67 | - * @return self |
|
| 68 | - */ |
|
| 69 | - public static function fromASN1(TaggedType $el): self |
|
| 70 | - { |
|
| 71 | - switch ($el->tag()) { |
|
| 72 | - // otherName |
|
| 73 | - case self::TAG_OTHER_NAME: |
|
| 74 | - return OtherName::fromChosenASN1( |
|
| 75 | - $el->asImplicit(Element::TYPE_SEQUENCE)); |
|
| 76 | - // rfc822Name |
|
| 77 | - case self::TAG_RFC822_NAME: |
|
| 78 | - return RFC822Name::fromChosenASN1( |
|
| 79 | - $el->asImplicit(Element::TYPE_IA5_STRING)); |
|
| 80 | - // dNSName |
|
| 81 | - case self::TAG_DNS_NAME: |
|
| 82 | - return DNSName::fromChosenASN1( |
|
| 83 | - $el->asImplicit(Element::TYPE_IA5_STRING)); |
|
| 84 | - // x400Address |
|
| 85 | - case self::TAG_X400_ADDRESS: |
|
| 86 | - return X400Address::fromChosenASN1( |
|
| 87 | - $el->asImplicit(Element::TYPE_SEQUENCE)); |
|
| 88 | - // directoryName |
|
| 89 | - case self::TAG_DIRECTORY_NAME: |
|
| 90 | - // because Name is a CHOICE, albeit having only one option, |
|
| 91 | - // explicit tagging must be used |
|
| 92 | - // (see X.680 07/2002 30.6.c) |
|
| 93 | - return DirectoryName::fromChosenASN1($el->asExplicit()); |
|
| 94 | - // ediPartyName |
|
| 95 | - case self::TAG_EDI_PARTY_NAME: |
|
| 96 | - return EDIPartyName::fromChosenASN1( |
|
| 97 | - $el->asImplicit(Element::TYPE_SEQUENCE)); |
|
| 98 | - // uniformResourceIdentifier |
|
| 99 | - case self::TAG_URI: |
|
| 100 | - return UniformResourceIdentifier::fromChosenASN1( |
|
| 101 | - $el->asImplicit(Element::TYPE_IA5_STRING)); |
|
| 102 | - // iPAddress |
|
| 103 | - case self::TAG_IP_ADDRESS: |
|
| 104 | - return IPAddress::fromChosenASN1( |
|
| 105 | - $el->asImplicit(Element::TYPE_OCTET_STRING)); |
|
| 106 | - // registeredID |
|
| 107 | - case self::TAG_REGISTERED_ID: |
|
| 108 | - return RegisteredID::fromChosenASN1( |
|
| 109 | - $el->asImplicit(Element::TYPE_OBJECT_IDENTIFIER)); |
|
| 110 | - } |
|
| 111 | - throw new \UnexpectedValueException( |
|
| 112 | - "GeneralName type " . $el->tag() . " not supported."); |
|
| 113 | - } |
|
| 62 | + /** |
|
| 63 | + * Initialize from ASN.1. |
|
| 64 | + * |
|
| 65 | + * @param TaggedType $el |
|
| 66 | + * @throws \UnexpectedValueException |
|
| 67 | + * @return self |
|
| 68 | + */ |
|
| 69 | + public static function fromASN1(TaggedType $el): self |
|
| 70 | + { |
|
| 71 | + switch ($el->tag()) { |
|
| 72 | + // otherName |
|
| 73 | + case self::TAG_OTHER_NAME: |
|
| 74 | + return OtherName::fromChosenASN1( |
|
| 75 | + $el->asImplicit(Element::TYPE_SEQUENCE)); |
|
| 76 | + // rfc822Name |
|
| 77 | + case self::TAG_RFC822_NAME: |
|
| 78 | + return RFC822Name::fromChosenASN1( |
|
| 79 | + $el->asImplicit(Element::TYPE_IA5_STRING)); |
|
| 80 | + // dNSName |
|
| 81 | + case self::TAG_DNS_NAME: |
|
| 82 | + return DNSName::fromChosenASN1( |
|
| 83 | + $el->asImplicit(Element::TYPE_IA5_STRING)); |
|
| 84 | + // x400Address |
|
| 85 | + case self::TAG_X400_ADDRESS: |
|
| 86 | + return X400Address::fromChosenASN1( |
|
| 87 | + $el->asImplicit(Element::TYPE_SEQUENCE)); |
|
| 88 | + // directoryName |
|
| 89 | + case self::TAG_DIRECTORY_NAME: |
|
| 90 | + // because Name is a CHOICE, albeit having only one option, |
|
| 91 | + // explicit tagging must be used |
|
| 92 | + // (see X.680 07/2002 30.6.c) |
|
| 93 | + return DirectoryName::fromChosenASN1($el->asExplicit()); |
|
| 94 | + // ediPartyName |
|
| 95 | + case self::TAG_EDI_PARTY_NAME: |
|
| 96 | + return EDIPartyName::fromChosenASN1( |
|
| 97 | + $el->asImplicit(Element::TYPE_SEQUENCE)); |
|
| 98 | + // uniformResourceIdentifier |
|
| 99 | + case self::TAG_URI: |
|
| 100 | + return UniformResourceIdentifier::fromChosenASN1( |
|
| 101 | + $el->asImplicit(Element::TYPE_IA5_STRING)); |
|
| 102 | + // iPAddress |
|
| 103 | + case self::TAG_IP_ADDRESS: |
|
| 104 | + return IPAddress::fromChosenASN1( |
|
| 105 | + $el->asImplicit(Element::TYPE_OCTET_STRING)); |
|
| 106 | + // registeredID |
|
| 107 | + case self::TAG_REGISTERED_ID: |
|
| 108 | + return RegisteredID::fromChosenASN1( |
|
| 109 | + $el->asImplicit(Element::TYPE_OBJECT_IDENTIFIER)); |
|
| 110 | + } |
|
| 111 | + throw new \UnexpectedValueException( |
|
| 112 | + "GeneralName type " . $el->tag() . " not supported."); |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - /** |
|
| 116 | - * Get type tag. |
|
| 117 | - * |
|
| 118 | - * @return int |
|
| 119 | - */ |
|
| 120 | - public function tag(): int |
|
| 121 | - { |
|
| 122 | - return $this->_tag; |
|
| 123 | - } |
|
| 115 | + /** |
|
| 116 | + * Get type tag. |
|
| 117 | + * |
|
| 118 | + * @return int |
|
| 119 | + */ |
|
| 120 | + public function tag(): int |
|
| 121 | + { |
|
| 122 | + return $this->_tag; |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - /** |
|
| 126 | - * Generate ASN.1 element. |
|
| 127 | - * |
|
| 128 | - * @return Element |
|
| 129 | - */ |
|
| 130 | - public function toASN1(): Element |
|
| 131 | - { |
|
| 132 | - return $this->_choiceASN1(); |
|
| 133 | - } |
|
| 125 | + /** |
|
| 126 | + * Generate ASN.1 element. |
|
| 127 | + * |
|
| 128 | + * @return Element |
|
| 129 | + */ |
|
| 130 | + public function toASN1(): Element |
|
| 131 | + { |
|
| 132 | + return $this->_choiceASN1(); |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - /** |
|
| 136 | - * Check whether GeneralName is equal to other. |
|
| 137 | - * |
|
| 138 | - * @param GeneralName $other GeneralName to compare to |
|
| 139 | - * @return boolean True if names are equal |
|
| 140 | - */ |
|
| 141 | - public function equals(GeneralName $other): bool |
|
| 142 | - { |
|
| 143 | - if ($this->_tag != $other->_tag) { |
|
| 144 | - return false; |
|
| 145 | - } |
|
| 146 | - if ($this->_choiceASN1()->toDER() != $other->_choiceASN1()->toDER()) { |
|
| 147 | - return false; |
|
| 148 | - } |
|
| 149 | - return true; |
|
| 150 | - } |
|
| 135 | + /** |
|
| 136 | + * Check whether GeneralName is equal to other. |
|
| 137 | + * |
|
| 138 | + * @param GeneralName $other GeneralName to compare to |
|
| 139 | + * @return boolean True if names are equal |
|
| 140 | + */ |
|
| 141 | + public function equals(GeneralName $other): bool |
|
| 142 | + { |
|
| 143 | + if ($this->_tag != $other->_tag) { |
|
| 144 | + return false; |
|
| 145 | + } |
|
| 146 | + if ($this->_choiceASN1()->toDER() != $other->_choiceASN1()->toDER()) { |
|
| 147 | + return false; |
|
| 148 | + } |
|
| 149 | + return true; |
|
| 150 | + } |
|
| 151 | 151 | |
| 152 | - /** |
|
| 153 | - * Get general name as a string. |
|
| 154 | - * |
|
| 155 | - * @return string |
|
| 156 | - */ |
|
| 157 | - public function __toString() |
|
| 158 | - { |
|
| 159 | - return $this->string(); |
|
| 160 | - } |
|
| 152 | + /** |
|
| 153 | + * Get general name as a string. |
|
| 154 | + * |
|
| 155 | + * @return string |
|
| 156 | + */ |
|
| 157 | + public function __toString() |
|
| 158 | + { |
|
| 159 | + return $this->string(); |
|
| 160 | + } |
|
| 161 | 161 | } |
@@ -16,59 +16,59 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class RFC822Name extends GeneralName |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * Email. |
|
| 21 | - * |
|
| 22 | - * @var string $_email |
|
| 23 | - */ |
|
| 24 | - protected $_email; |
|
| 19 | + /** |
|
| 20 | + * Email. |
|
| 21 | + * |
|
| 22 | + * @var string $_email |
|
| 23 | + */ |
|
| 24 | + protected $_email; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Constructor. |
|
| 28 | - * |
|
| 29 | - * @param string $email |
|
| 30 | - */ |
|
| 31 | - public function __construct(string $email) |
|
| 32 | - { |
|
| 33 | - $this->_tag = self::TAG_RFC822_NAME; |
|
| 34 | - $this->_email = $email; |
|
| 35 | - } |
|
| 26 | + /** |
|
| 27 | + * Constructor. |
|
| 28 | + * |
|
| 29 | + * @param string $email |
|
| 30 | + */ |
|
| 31 | + public function __construct(string $email) |
|
| 32 | + { |
|
| 33 | + $this->_tag = self::TAG_RFC822_NAME; |
|
| 34 | + $this->_email = $email; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * |
|
| 39 | - * @param UnspecifiedType $el |
|
| 40 | - * @return self |
|
| 41 | - */ |
|
| 42 | - public static function fromChosenASN1(UnspecifiedType $el): self |
|
| 43 | - { |
|
| 44 | - return new self($el->asIA5String()->string()); |
|
| 45 | - } |
|
| 37 | + /** |
|
| 38 | + * |
|
| 39 | + * @param UnspecifiedType $el |
|
| 40 | + * @return self |
|
| 41 | + */ |
|
| 42 | + public static function fromChosenASN1(UnspecifiedType $el): self |
|
| 43 | + { |
|
| 44 | + return new self($el->asIA5String()->string()); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * |
|
| 49 | - * {@inheritdoc} |
|
| 50 | - */ |
|
| 51 | - public function string(): string |
|
| 52 | - { |
|
| 53 | - return $this->_email; |
|
| 54 | - } |
|
| 47 | + /** |
|
| 48 | + * |
|
| 49 | + * {@inheritdoc} |
|
| 50 | + */ |
|
| 51 | + public function string(): string |
|
| 52 | + { |
|
| 53 | + return $this->_email; |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Get email. |
|
| 58 | - * |
|
| 59 | - * @return string |
|
| 60 | - */ |
|
| 61 | - public function email(): string |
|
| 62 | - { |
|
| 63 | - return $this->_email; |
|
| 64 | - } |
|
| 56 | + /** |
|
| 57 | + * Get email. |
|
| 58 | + * |
|
| 59 | + * @return string |
|
| 60 | + */ |
|
| 61 | + public function email(): string |
|
| 62 | + { |
|
| 63 | + return $this->_email; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * |
|
| 68 | - * {@inheritdoc} |
|
| 69 | - */ |
|
| 70 | - protected function _choiceASN1(): TaggedType |
|
| 71 | - { |
|
| 72 | - return new ImplicitlyTaggedType($this->_tag, new IA5String($this->_email)); |
|
| 73 | - } |
|
| 66 | + /** |
|
| 67 | + * |
|
| 68 | + * {@inheritdoc} |
|
| 69 | + */ |
|
| 70 | + protected function _choiceASN1(): TaggedType |
|
| 71 | + { |
|
| 72 | + return new ImplicitlyTaggedType($this->_tag, new IA5String($this->_email)); |
|
| 73 | + } |
|
| 74 | 74 | } |
@@ -18,47 +18,47 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class EDIPartyName extends GeneralName |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * |
|
| 23 | - * @var \ASN1\Element |
|
| 24 | - */ |
|
| 25 | - protected $_element; |
|
| 21 | + /** |
|
| 22 | + * |
|
| 23 | + * @var \ASN1\Element |
|
| 24 | + */ |
|
| 25 | + protected $_element; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Constructor. |
|
| 29 | - */ |
|
| 30 | - protected function __construct() |
|
| 31 | - { |
|
| 32 | - $this->_tag = self::TAG_EDI_PARTY_NAME; |
|
| 33 | - } |
|
| 27 | + /** |
|
| 28 | + * Constructor. |
|
| 29 | + */ |
|
| 30 | + protected function __construct() |
|
| 31 | + { |
|
| 32 | + $this->_tag = self::TAG_EDI_PARTY_NAME; |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * |
|
| 37 | - * @param UnspecifiedType $el |
|
| 38 | - * @return self |
|
| 39 | - */ |
|
| 40 | - public static function fromChosenASN1(UnspecifiedType $el): self |
|
| 41 | - { |
|
| 42 | - $obj = new self(); |
|
| 43 | - $obj->_element = $el->asSequence(); |
|
| 44 | - return $obj; |
|
| 45 | - } |
|
| 35 | + /** |
|
| 36 | + * |
|
| 37 | + * @param UnspecifiedType $el |
|
| 38 | + * @return self |
|
| 39 | + */ |
|
| 40 | + public static function fromChosenASN1(UnspecifiedType $el): self |
|
| 41 | + { |
|
| 42 | + $obj = new self(); |
|
| 43 | + $obj->_element = $el->asSequence(); |
|
| 44 | + return $obj; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * |
|
| 49 | - * {@inheritdoc} |
|
| 50 | - */ |
|
| 51 | - public function string(): string |
|
| 52 | - { |
|
| 53 | - return bin2hex($this->_element->toDER()); |
|
| 54 | - } |
|
| 47 | + /** |
|
| 48 | + * |
|
| 49 | + * {@inheritdoc} |
|
| 50 | + */ |
|
| 51 | + public function string(): string |
|
| 52 | + { |
|
| 53 | + return bin2hex($this->_element->toDER()); |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * |
|
| 58 | - * {@inheritdoc} |
|
| 59 | - */ |
|
| 60 | - protected function _choiceASN1(): TaggedType |
|
| 61 | - { |
|
| 62 | - return new ImplicitlyTaggedType($this->_tag, $this->_element); |
|
| 63 | - } |
|
| 56 | + /** |
|
| 57 | + * |
|
| 58 | + * {@inheritdoc} |
|
| 59 | + */ |
|
| 60 | + protected function _choiceASN1(): TaggedType |
|
| 61 | + { |
|
| 62 | + return new ImplicitlyTaggedType($this->_tag, $this->_element); |
|
| 63 | + } |
|
| 64 | 64 | } |
@@ -6,42 +6,42 @@ |
||
| 6 | 6 | |
| 7 | 7 | class IPv4Address extends IPAddress |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Initialize from octets. |
|
| 11 | - * |
|
| 12 | - * @param string $octets |
|
| 13 | - * @throws \InvalidArgumentException |
|
| 14 | - * @return self |
|
| 15 | - */ |
|
| 16 | - public static function fromOctets(string $octets): self |
|
| 17 | - { |
|
| 18 | - $mask = null; |
|
| 19 | - $bytes = unpack("C*", $octets); |
|
| 20 | - switch (count($bytes)) { |
|
| 21 | - case 4: |
|
| 22 | - $ip = implode(".", $bytes); |
|
| 23 | - break; |
|
| 24 | - case 8: |
|
| 25 | - $ip = implode(".", array_slice($bytes, 0, 4)); |
|
| 26 | - $mask = implode(".", array_slice($bytes, 4, 4)); |
|
| 27 | - break; |
|
| 28 | - default: |
|
| 29 | - throw new \UnexpectedValueException("Invalid IPv4 octet length."); |
|
| 30 | - } |
|
| 31 | - return new self($ip, $mask); |
|
| 32 | - } |
|
| 9 | + /** |
|
| 10 | + * Initialize from octets. |
|
| 11 | + * |
|
| 12 | + * @param string $octets |
|
| 13 | + * @throws \InvalidArgumentException |
|
| 14 | + * @return self |
|
| 15 | + */ |
|
| 16 | + public static function fromOctets(string $octets): self |
|
| 17 | + { |
|
| 18 | + $mask = null; |
|
| 19 | + $bytes = unpack("C*", $octets); |
|
| 20 | + switch (count($bytes)) { |
|
| 21 | + case 4: |
|
| 22 | + $ip = implode(".", $bytes); |
|
| 23 | + break; |
|
| 24 | + case 8: |
|
| 25 | + $ip = implode(".", array_slice($bytes, 0, 4)); |
|
| 26 | + $mask = implode(".", array_slice($bytes, 4, 4)); |
|
| 27 | + break; |
|
| 28 | + default: |
|
| 29 | + throw new \UnexpectedValueException("Invalid IPv4 octet length."); |
|
| 30 | + } |
|
| 31 | + return new self($ip, $mask); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * |
|
| 36 | - * {@inheritdoc} |
|
| 37 | - */ |
|
| 38 | - protected function _octets(): string |
|
| 39 | - { |
|
| 40 | - $bytes = array_map("intval", explode(".", $this->_ip)); |
|
| 41 | - if (isset($this->_mask)) { |
|
| 42 | - $bytes = array_merge($bytes, |
|
| 43 | - array_map("intval", explode(".", $this->_mask))); |
|
| 44 | - } |
|
| 45 | - return pack("C*", ...$bytes); |
|
| 46 | - } |
|
| 34 | + /** |
|
| 35 | + * |
|
| 36 | + * {@inheritdoc} |
|
| 37 | + */ |
|
| 38 | + protected function _octets(): string |
|
| 39 | + { |
|
| 40 | + $bytes = array_map("intval", explode(".", $this->_ip)); |
|
| 41 | + if (isset($this->_mask)) { |
|
| 42 | + $bytes = array_merge($bytes, |
|
| 43 | + array_map("intval", explode(".", $this->_mask))); |
|
| 44 | + } |
|
| 45 | + return pack("C*", ...$bytes); |
|
| 46 | + } |
|
| 47 | 47 | } |