| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\X509\CertificationPath\PathBuilding; |
| 6 |
|
| 7 |
use function count; |
| 8 |
use SpomkyLabs\Pki\X509\Certificate\Certificate; |
| 9 |
use SpomkyLabs\Pki\X509\Certificate\CertificateBundle; |
| 10 |
use SpomkyLabs\Pki\X509\CertificationPath\CertificationPath; |
| 11 |
use SpomkyLabs\Pki\X509\CertificationPath\Exception\PathBuildingException; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
final class CertificationPathBuilder |
| 19 |
{ |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
private function __construct( |
| 24 |
private readonly CertificateBundle $trustList |
| 25 |
) { |
| 26 |
} |
| 27 |
|
| 28 |
public static function create(CertificateBundle $trustList): self |
| 29 |
{ |
| 30 |
return new self($trustList); |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public function allPathsToTarget(Certificate $target, ?CertificateBundle $intermediate = null): array |
| 42 |
{ |
| 43 |
$paths = $this->resolvePathsToTarget($target, $intermediate); |
| 44 |
|
| 45 |
return array_map(static fn ($certs) => CertificationPath::create(...$certs), $paths); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public function shortestPathToTarget( |
| 55 |
Certificate $target, |
| 56 |
?CertificateBundle $intermediate = null |
| 57 |
): CertificationPath { |
| 58 |
$paths = $this->allPathsToTarget($target, $intermediate); |
| 59 |
if (count($paths) === 0) { |
| 60 |
throw new PathBuildingException('No certification paths.'); |
| 61 |
} |
| 62 |
usort($paths, fn ($a, $b) => count($a) < count($b) ? -1 : 1); |
| 63 |
return reset($paths); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
private function findIssuers(Certificate $target, CertificateBundle $bundle): array |
| 75 |
{ |
| 76 |
$issuers = []; |
| 77 |
$issuer_name = $target->tbsCertificate() |
| 78 |
->issuer(); |
| 79 |
$extensions = $target->tbsCertificate() |
| 80 |
->extensions(); |
| 81 |
|
| 82 |
if ($extensions->hasAuthorityKeyIdentifier()) { |
| 83 |
$ext = $extensions->authorityKeyIdentifier(); |
| 84 |
if ($ext->hasKeyIdentifier()) { |
| 85 |
foreach ($bundle->allBySubjectKeyIdentifier($ext->keyIdentifier()) as $issuer) { |
| 86 |
|
| 87 |
if ($issuer->tbsCertificate()->subject()->equals($issuer_name)) { |
| 88 |
$issuers[] = $issuer; |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
return $issuers; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
private function resolvePathsToTarget(Certificate $target, ?CertificateBundle $intermediate = null): array |
| 106 |
{ |
| 107 |
|
| 108 |
$paths = []; |
| 109 |
|
| 110 |
foreach ($this->findIssuers($target, $this->trustList) as $issuer) { |
| 111 |
|
| 112 |
|
| 113 |
if ($target->equals($issuer)) { |
| 114 |
$paths[] = [$target]; |
| 115 |
} else { |
| 116 |
$paths[] = [$issuer, $target]; |
| 117 |
} |
| 118 |
} |
| 119 |
if (isset($intermediate)) { |
| 120 |
|
| 121 |
foreach ($this->findIssuers($target, $intermediate) as $issuer) { |
| 122 |
|
| 123 |
if ($issuer->isSelfIssued()) { |
| 124 |
continue; |
| 125 |
} |
| 126 |
|
| 127 |
$subpaths = $this->resolvePathsToTarget($issuer, $intermediate); |
| 128 |
foreach ($subpaths as $path) { |
| 129 |
$paths[] = array_merge($path, [$target]); |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
return $paths; |
| 135 |
} |
| 136 |
} |