| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\X509\CertificationPath; |
| 6 |
|
| 7 |
use ArrayIterator; |
| 8 |
use function count; |
| 9 |
use Countable; |
| 10 |
use IteratorAggregate; |
| 11 |
use LogicException; |
| 12 |
use SpomkyLabs\Pki\CryptoBridge\Crypto; |
| 13 |
use SpomkyLabs\Pki\X509\Certificate\Certificate; |
| 14 |
use SpomkyLabs\Pki\X509\Certificate\CertificateBundle; |
| 15 |
use SpomkyLabs\Pki\X509\Certificate\CertificateChain; |
| 16 |
use SpomkyLabs\Pki\X509\CertificationPath\PathBuilding\CertificationPathBuilder; |
| 17 |
use SpomkyLabs\Pki\X509\CertificationPath\PathValidation\PathValidationConfig; |
| 18 |
use SpomkyLabs\Pki\X509\CertificationPath\PathValidation\PathValidationResult; |
| 19 |
use SpomkyLabs\Pki\X509\CertificationPath\PathValidation\PathValidator; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
final class CertificationPath implements Countable, IteratorAggregate |
| 30 |
{ |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
private readonly array $certificates; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
private function __construct(Certificate ...$certificates) |
| 43 |
{ |
| 44 |
$this->certificates = $certificates; |
| 45 |
} |
| 46 |
|
| 47 |
public static function create(Certificate ...$certificates): self |
| 48 |
{ |
| 49 |
return new self(...$certificates); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public static function fromCertificateChain(CertificateChain $chain): self |
| 56 |
{ |
| 57 |
return self::create(...array_reverse($chain->certificates(), false)); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public static function toTarget( |
| 68 |
Certificate $target, |
| 69 |
CertificateBundle $trust_anchors, |
| 70 |
?CertificateBundle $intermediate = null |
| 71 |
): self { |
| 72 |
return CertificationPathBuilder::create($trust_anchors)->shortestPathToTarget($target, $intermediate); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public static function fromTrustAnchorToTarget( |
| 84 |
Certificate $trust_anchor, |
| 85 |
Certificate $target, |
| 86 |
?CertificateBundle $intermediate = null |
| 87 |
): self { |
| 88 |
return self::toTarget($target, CertificateBundle::create($trust_anchor), $intermediate); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public function certificates(): array |
| 97 |
{ |
| 98 |
return $this->certificates; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public function trustAnchorCertificate(): Certificate |
| 105 |
{ |
| 106 |
if (count($this->certificates) === 0) { |
| 107 |
throw new LogicException('No certificates.'); |
| 108 |
} |
| 109 |
return $this->certificates[0]; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public function endEntityCertificate(): Certificate |
| 116 |
{ |
| 117 |
if (count($this->certificates) === 0) { |
| 118 |
throw new LogicException('No certificates.'); |
| 119 |
} |
| 120 |
return $this->certificates[count($this->certificates) - 1]; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public function certificateChain(): CertificateChain |
| 127 |
{ |
| 128 |
return CertificateChain::create(...array_reverse($this->certificates, false)); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public function startsWith(Certificate ...$certs): bool |
| 137 |
{ |
| 138 |
$n = count($certs); |
| 139 |
if ($n > count($this->certificates)) { |
| 140 |
return false; |
| 141 |
} |
| 142 |
for ($i = 0; $i < $n; ++$i) { |
| 143 |
if (! $certs[$i]->equals($this->certificates[$i])) { |
| 144 |
return false; |
| 145 |
} |
| 146 |
} |
| 147 |
return true; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public function validate(PathValidationConfig $config, ?Crypto $crypto = null): PathValidationResult |
| 156 |
{ |
| 157 |
$crypto ??= Crypto::getDefault(); |
| 158 |
return PathValidator::create($crypto, $config, ...$this->certificates)->validate(); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public function count(): int |
| 165 |
{ |
| 166 |
return count($this->certificates); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public function getIterator(): ArrayIterator |
| 175 |
{ |
| 176 |
return new ArrayIterator($this->certificates); |
| 177 |
} |
| 178 |
} |