| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Webauthn\MetadataService\Service; |
| 6 |
|
| 7 |
use function array_key_exists; |
| 8 |
use function is_array; |
| 9 |
use Jose\Component\Core\AlgorithmManager; |
| 10 |
use Jose\Component\KeyManagement\JWKFactory; |
| 11 |
use Jose\Component\Signature\Algorithm\ES256; |
| 12 |
use Jose\Component\Signature\Algorithm\RS256; |
| 13 |
use Jose\Component\Signature\JWSVerifier; |
| 14 |
use Jose\Component\Signature\Serializer\CompactSerializer; |
| 15 |
use LogicException; |
| 16 |
use Psr\EventDispatcher\EventDispatcherInterface; |
| 17 |
use function sprintf; |
| 18 |
use Symfony\Component\Serializer\Encoder\JsonEncoder; |
| 19 |
use Symfony\Component\Serializer\SerializerInterface; |
| 20 |
use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 21 |
use Throwable; |
| 22 |
use Webauthn\AttestationStatement\AttestationStatementSupportManager; |
| 23 |
use Webauthn\Denormalizer\WebauthnSerializerFactory; |
| 24 |
use Webauthn\Event\CanDispatchEvents; |
| 25 |
use Webauthn\Event\MetadataStatementFound; |
| 26 |
use Webauthn\Event\NullEventDispatcher; |
| 27 |
use Webauthn\Exception\MetadataStatementLoadingException; |
| 28 |
use Webauthn\Exception\MissingMetadataStatementException; |
| 29 |
use Webauthn\MetadataService\CertificateChain\CertificateChainValidator; |
| 30 |
use Webauthn\MetadataService\CertificateChain\CertificateToolbox; |
| 31 |
use Webauthn\MetadataService\Statement\MetadataStatement; |
| 32 |
use Webauthn\MetadataService\Statement\StatusReport; |
| 33 |
|
| 34 |
final class FidoAllianceCompliantMetadataService implements MetadataService, CanDispatchEvents |
| 35 |
{ |
| 36 |
private bool $loaded = false; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
private array $statements = []; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
private array $statusReports = []; |
| 47 |
|
| 48 |
private EventDispatcherInterface $dispatcher; |
| 49 |
|
| 50 |
private readonly SerializerInterface $serializer; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function __construct( |
| 56 |
private readonly HttpClientInterface $httpClient, |
| 57 |
private readonly string $uri, |
| 58 |
private readonly array $additionalHeaderParameters = [], |
| 59 |
private readonly ?CertificateChainValidator $certificateChainValidator = null, |
| 60 |
private readonly ?string $rootCertificateUri = null, |
| 61 |
?SerializerInterface $serializer = null, |
| 62 |
) { |
| 63 |
if (! class_exists(CompactSerializer::class)) { |
| 64 |
throw new LogicException( |
| 65 |
'The "web-token/jwt-library" package is required to use this service. Please run "composer require web-token/jwt-library".' |
| 66 |
); |
| 67 |
} |
| 68 |
$this->serializer = $serializer ?? (new WebauthnSerializerFactory( |
| 69 |
AttestationStatementSupportManager::create() |
| 70 |
))->create(); |
| 71 |
$this->dispatcher = new NullEventDispatcher(); |
| 72 |
} |
| 73 |
|
| 74 |
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void |
| 75 |
{ |
| 76 |
$this->dispatcher = $eventDispatcher; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public static function create( |
| 83 |
HttpClientInterface $httpClient, |
| 84 |
string $uri, |
| 85 |
array $additionalHeaderParameters = [], |
| 86 |
?CertificateChainValidator $certificateChainValidator = null, |
| 87 |
?string $rootCertificateUri = null, |
| 88 |
?SerializerInterface $serializer = null, |
| 89 |
): self { |
| 90 |
return new self( |
| 91 |
$httpClient, |
| 92 |
$uri, |
| 93 |
$additionalHeaderParameters, |
| 94 |
$certificateChainValidator, |
| 95 |
$rootCertificateUri, |
| 96 |
$serializer, |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public function list(): iterable |
| 104 |
{ |
| 105 |
$this->loadData(); |
| 106 |
|
| 107 |
yield from array_keys($this->statements); |
| 108 |
} |
| 109 |
|
| 110 |
public function has(string $aaguid): bool |
| 111 |
{ |
| 112 |
$this->loadData(); |
| 113 |
|
| 114 |
return array_key_exists($aaguid, $this->statements); |
| 115 |
} |
| 116 |
|
| 117 |
public function get(string $aaguid): MetadataStatement |
| 118 |
{ |
| 119 |
$this->loadData(); |
| 120 |
array_key_exists($aaguid, $this->statements) || throw MissingMetadataStatementException::create($aaguid); |
| 121 |
$mds = $this->statements[$aaguid]; |
| 122 |
$this->dispatcher->dispatch(MetadataStatementFound::create($mds)); |
| 123 |
|
| 124 |
return $mds; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public function getStatusReports(string $aaguid): iterable |
| 131 |
{ |
| 132 |
$this->loadData(); |
| 133 |
|
| 134 |
return $this->statusReports[$aaguid] ?? []; |
| 135 |
} |
| 136 |
|
| 137 |
private function loadData(): void |
| 138 |
{ |
| 139 |
if ($this->loaded) { |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
$content = $this->fetch($this->uri, $this->additionalHeaderParameters); |
| 144 |
$jwtCertificates = []; |
| 145 |
try { |
| 146 |
$payload = $this->getJwsPayload($content, $jwtCertificates); |
| 147 |
$this->validateCertificates(...$jwtCertificates); |
| 148 |
|
| 149 |
$blob = $this->serializer->deserialize($payload, MetadataBLOBPayload::class, JsonEncoder::FORMAT); |
| 150 |
foreach ($blob->entries as $entry) { |
| 151 |
$mds = $entry->metadataStatement; |
| 152 |
if ($mds !== null && $entry->aaguid !== null) { |
| 153 |
$this->statements[$entry->aaguid] = $mds; |
| 154 |
$this->statusReports[$entry->aaguid] = $entry->statusReports; |
| 155 |
} |
| 156 |
} |
| 157 |
} catch (Throwable) { |
| 158 |
|
| 159 |
} finally { |
| 160 |
$this->loaded = true; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
private function fetch(string $uri, array $headerParameters): string |
| 168 |
{ |
| 169 |
$response = $this->httpClient->request('GET', $uri, [ |
| 170 |
'headers' => $headerParameters, |
| 171 |
]); |
| 172 |
$response->getStatusCode() === 200 || throw MetadataStatementLoadingException::create(sprintf( |
| 173 |
'Unable to contact the server. Response code is %d', |
| 174 |
$response->getStatusCode() |
| 175 |
)); |
| 176 |
|
| 177 |
$content = $response->getContent(); |
| 178 |
$content !== '' || throw MetadataStatementLoadingException::create( |
| 179 |
'Unable to contact the server. The response has no content' |
| 180 |
); |
| 181 |
|
| 182 |
return $content; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
private function getJwsPayload(string $token, array &$rootCertificates): string |
| 189 |
{ |
| 190 |
$jws = (new CompactSerializer())->unserialize($token); |
| 191 |
$jws->countSignatures() === 1 || throw MetadataStatementLoadingException::create( |
| 192 |
'Invalid response from the metadata service. Only one signature shall be present.' |
| 193 |
); |
| 194 |
$signature = $jws->getSignature(0); |
| 195 |
$payload = $jws->getPayload(); |
| 196 |
$payload !== '' || throw MetadataStatementLoadingException::create( |
| 197 |
'Invalid response from the metadata service. The token payload is empty.' |
| 198 |
); |
| 199 |
$header = $signature->getProtectedHeader(); |
| 200 |
array_key_exists('alg', $header) || throw MetadataStatementLoadingException::create( |
| 201 |
'The "alg" parameter is missing.' |
| 202 |
); |
| 203 |
array_key_exists('x5c', $header) || throw MetadataStatementLoadingException::create( |
| 204 |
'The "x5c" parameter is missing.' |
| 205 |
); |
| 206 |
is_array($header['x5c']) || throw MetadataStatementLoadingException::create( |
| 207 |
'The "x5c" parameter should be an array.' |
| 208 |
); |
| 209 |
|
| 210 |
$x5c = $header['x5c']; |
| 211 |
$key = JWKFactory::createFromX5C($x5c); |
| 212 |
$rootCertificates = $x5c; |
| 213 |
|
| 214 |
$verifier = new JWSVerifier(new AlgorithmManager([new ES256(), new RS256()])); |
| 215 |
$isValid = $verifier->verifyWithKey($jws, $key, 0); |
| 216 |
$isValid || throw MetadataStatementLoadingException::create( |
| 217 |
'Invalid response from the metadata service. The token signature is invalid.' |
| 218 |
); |
| 219 |
$payload = $jws->getPayload(); |
| 220 |
$payload !== null || throw MetadataStatementLoadingException::create( |
| 221 |
'Invalid response from the metadata service. The payload is missing.' |
| 222 |
); |
| 223 |
|
| 224 |
return $payload; |
| 225 |
} |
| 226 |
|
| 227 |
private function validateCertificates(string ...$untrustedCertificates): void |
| 228 |
{ |
| 229 |
if ($this->certificateChainValidator === null || $this->rootCertificateUri === null) { |
| 230 |
return; |
| 231 |
} |
| 232 |
$untrustedCertificates = CertificateToolbox::fixPEMStructures($untrustedCertificates); |
| 233 |
$rootCertificate = CertificateToolbox::convertDERToPEM($this->fetch($this->rootCertificateUri, [])); |
| 234 |
$this->certificateChainValidator->check($untrustedCertificates, [$rootCertificate]); |
| 235 |
} |
| 236 |
} |