@@ -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 | } |