| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Webauthn\MetadataService\CertificateChain; |
| 6 |
|
| 7 |
use function count; |
| 8 |
use function in_array; |
| 9 |
use function parse_url; |
| 10 |
use const PHP_EOL; |
| 11 |
use const PHP_URL_SCHEME; |
| 12 |
use Psr\Clock\ClockInterface; |
| 13 |
use Psr\EventDispatcher\EventDispatcherInterface; |
| 14 |
use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType; |
| 15 |
use SpomkyLabs\Pki\CryptoEncoding\PEM; |
| 16 |
use SpomkyLabs\Pki\X509\Certificate\Certificate; |
| 17 |
use SpomkyLabs\Pki\X509\CertificationPath\CertificationPath; |
| 18 |
use SpomkyLabs\Pki\X509\CertificationPath\PathValidation\PathValidationConfig; |
| 19 |
use function sprintf; |
| 20 |
use Symfony\Component\Clock\NativeClock; |
| 21 |
use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 22 |
use Throwable; |
| 23 |
use Webauthn\Event\BeforeCertificateChainValidation; |
| 24 |
use Webauthn\Event\CanDispatchEvents; |
| 25 |
use Webauthn\Event\CertificateChainValidationFailed; |
| 26 |
use Webauthn\Event\CertificateChainValidationSucceeded; |
| 27 |
use Webauthn\Event\NullEventDispatcher; |
| 28 |
use Webauthn\Exception\CertificateChainException; |
| 29 |
use Webauthn\Exception\CertificateRevocationListException; |
| 30 |
use Webauthn\Exception\InvalidCertificateException; |
| 31 |
|
| 32 |
final class PhpCertificateChainValidator implements CertificateChainValidator, CanDispatchEvents |
| 33 |
{ |
| 34 |
private const MAX_VALIDATION_LENGTH = 5; |
| 35 |
|
| 36 |
private readonly ClockInterface $clock; |
| 37 |
|
| 38 |
private EventDispatcherInterface $dispatcher; |
| 39 |
|
| 40 |
public function __construct( |
| 41 |
private readonly HttpClientInterface $client, |
| 42 |
null|ClockInterface $clock = null, |
| 43 |
private readonly bool $allowFailures = true |
| 44 |
) { |
| 45 |
$this->clock = $clock ?? new NativeClock(); |
| 46 |
$this->dispatcher = new NullEventDispatcher(); |
| 47 |
} |
| 48 |
|
| 49 |
public static function create( |
| 50 |
HttpClientInterface $client, |
| 51 |
null|ClockInterface $clock = null, |
| 52 |
bool $allowFailures = true |
| 53 |
): self { |
| 54 |
return new self($client, $clock, $allowFailures); |
| 55 |
} |
| 56 |
|
| 57 |
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void |
| 58 |
{ |
| 59 |
$this->dispatcher = $eventDispatcher; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public function check(array $untrustedCertificates, array $trustedCertificates): void |
| 67 |
{ |
| 68 |
foreach ($trustedCertificates as $trustedCertificate) { |
| 69 |
$this->dispatcher->dispatch( |
| 70 |
BeforeCertificateChainValidation::create($untrustedCertificates, $trustedCertificate) |
| 71 |
); |
| 72 |
try { |
| 73 |
if ($this->validateChain($untrustedCertificates, $trustedCertificate)) { |
| 74 |
$this->dispatcher->dispatch( |
| 75 |
CertificateChainValidationSucceeded::create($untrustedCertificates, $trustedCertificate) |
| 76 |
); |
| 77 |
return; |
| 78 |
} |
| 79 |
} catch (Throwable $exception) { |
| 80 |
$this->dispatcher->dispatch( |
| 81 |
CertificateChainValidationFailed::create($untrustedCertificates, $trustedCertificate) |
| 82 |
); |
| 83 |
throw $exception; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
throw CertificateChainException::create($untrustedCertificates, $trustedCertificates); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
private function validateChain(array $untrustedCertificates, string $trustedCertificate): bool |
| 94 |
{ |
| 95 |
$untrustedCertificateObjects = array_map( |
| 96 |
static fn (string $cert): Certificate => Certificate::fromPEM(PEM::fromString($cert)), |
| 97 |
array_reverse($untrustedCertificates) |
| 98 |
); |
| 99 |
$trustedCertificateObject = Certificate::fromPEM(PEM::fromString($trustedCertificate)); |
| 100 |
|
| 101 |
|
| 102 |
if (count( |
| 103 |
$untrustedCertificateObjects |
| 104 |
) === 1 && $untrustedCertificateObjects[0]->toPEM()->string() === $trustedCertificateObject->toPEM()->string()) { |
| 105 |
return true; |
| 106 |
} |
| 107 |
$uniqueCertificates = array_map( |
| 108 |
static fn (Certificate $cert): string => $cert->toPEM() |
| 109 |
->string(), |
| 110 |
[...$untrustedCertificateObjects, $trustedCertificateObject] |
| 111 |
); |
| 112 |
count(array_unique($uniqueCertificates)) === count( |
| 113 |
$uniqueCertificates |
| 114 |
) || throw CertificateChainException::create( |
| 115 |
$untrustedCertificates, |
| 116 |
[$trustedCertificate], |
| 117 |
'Invalid certificate chain with duplicated certificates.' |
| 118 |
); |
| 119 |
|
| 120 |
if (! $this->validateCertificates($trustedCertificateObject, ...$untrustedCertificateObjects)) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
$certificates = [$trustedCertificateObject, ...$untrustedCertificateObjects]; |
| 125 |
$numCerts = count($certificates); |
| 126 |
for ($i = 1; $i < $numCerts; $i++) { |
| 127 |
if ($this->isRevoked($certificates[$i])) { |
| 128 |
throw CertificateChainException::create( |
| 129 |
$untrustedCertificates, |
| 130 |
[$trustedCertificate], |
| 131 |
'Unable to validate the certificate chain. Revoked certificate found.' |
| 132 |
); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
return true; |
| 137 |
} |
| 138 |
|
| 139 |
private function isRevoked(Certificate $subject): bool |
| 140 |
{ |
| 141 |
try { |
| 142 |
$csn = $subject->tbsCertificate() |
| 143 |
->serialNumber(); |
| 144 |
} catch (Throwable $e) { |
| 145 |
throw InvalidCertificateException::create( |
| 146 |
$subject->toPEM() |
| 147 |
->string(), |
| 148 |
sprintf('Failed to parse certificate: %s', $e->getMessage()), |
| 149 |
$e |
| 150 |
); |
| 151 |
} |
| 152 |
|
| 153 |
try { |
| 154 |
$urls = $this->getCrlUrlList($subject); |
| 155 |
} catch (Throwable $e) { |
| 156 |
if ($this->allowFailures) { |
| 157 |
return false; |
| 158 |
} |
| 159 |
throw InvalidCertificateException::create( |
| 160 |
$subject->toPEM() |
| 161 |
->string(), |
| 162 |
'Failed to get CRL distribution points: ' . $e->getMessage(), |
| 163 |
$e |
| 164 |
); |
| 165 |
} |
| 166 |
|
| 167 |
foreach ($urls as $url) { |
| 168 |
try { |
| 169 |
$revokedCertificates = $this->retrieveRevokedSerialNumbers($url); |
| 170 |
|
| 171 |
if (in_array($csn, $revokedCertificates, true)) { |
| 172 |
return true; |
| 173 |
} |
| 174 |
} catch (Throwable $e) { |
| 175 |
if ($this->allowFailures) { |
| 176 |
return false; |
| 177 |
} |
| 178 |
throw CertificateRevocationListException::create($url, sprintf( |
| 179 |
'Failed to retrieve the CRL:' . PHP_EOL . '%s', |
| 180 |
$e->getMessage() |
| 181 |
), $e); |
| 182 |
} |
| 183 |
} |
| 184 |
return false; |
| 185 |
} |
| 186 |
|
| 187 |
private function validateCertificates(Certificate $trustAnchor, Certificate ...$certificates): bool |
| 188 |
{ |
| 189 |
try { |
| 190 |
$config = PathValidationConfig::create($this->clock->now(), self::MAX_VALIDATION_LENGTH)->withTrustAnchor( |
| 191 |
$trustAnchor |
| 192 |
); |
| 193 |
CertificationPath::create(...$certificates)->validate($config); |
| 194 |
|
| 195 |
return true; |
| 196 |
} catch (Throwable) { |
| 197 |
return false; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
private function retrieveRevokedSerialNumbers(string $url): array |
| 205 |
{ |
| 206 |
try { |
| 207 |
$crlData = $this->client->request('GET', $url) |
| 208 |
->getContent(); |
| 209 |
$crl = UnspecifiedType::fromDER($crlData)->asSequence(); |
| 210 |
count($crl) === 3 || throw CertificateRevocationListException::create($url); |
| 211 |
$tbsCertList = $crl->at(0) |
| 212 |
->asSequence(); |
| 213 |
count($tbsCertList) >= 6 || throw CertificateRevocationListException::create($url); |
| 214 |
$list = $tbsCertList->at(5) |
| 215 |
->asSequence(); |
| 216 |
|
| 217 |
return array_map(static function (UnspecifiedType $r) use ($url): string { |
| 218 |
$sequence = $r->asSequence(); |
| 219 |
count($sequence) >= 1 || throw CertificateRevocationListException::create($url); |
| 220 |
return $sequence->at(0) |
| 221 |
->asInteger() |
| 222 |
->number(); |
| 223 |
}, $list->elements()); |
| 224 |
} catch (Throwable $e) { |
| 225 |
throw CertificateRevocationListException::create($url, 'Failed to download the CRL', $e); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
private function getCrlUrlList(Certificate $subject): array |
| 233 |
{ |
| 234 |
try { |
| 235 |
$urls = []; |
| 236 |
|
| 237 |
$extensions = $subject->tbsCertificate() |
| 238 |
->extensions(); |
| 239 |
if ($extensions->hasCRLDistributionPoints()) { |
| 240 |
$crlDists = $extensions->crlDistributionPoints(); |
| 241 |
foreach ($crlDists->distributionPoints() as $dist) { |
| 242 |
$url = $dist->fullName() |
| 243 |
->names() |
| 244 |
->firstURI(); |
| 245 |
$scheme = parse_url($url, PHP_URL_SCHEME); |
| 246 |
if (! in_array($scheme, ['http', 'https'], true)) { |
| 247 |
continue; |
| 248 |
} |
| 249 |
$urls[] = $url; |
| 250 |
} |
| 251 |
} |
| 252 |
return $urls; |
| 253 |
} catch (Throwable $e) { |
| 254 |
throw InvalidCertificateException::create( |
| 255 |
$subject->toPEM() |
| 256 |
->string(), |
| 257 |
'Failed to get CRL distribution points from certificate: ' . $e->getMessage(), |
| 258 |
$e |
| 259 |
); |
| 260 |
} |
| 261 |
} |
| 262 |
} |