Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 1m14s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 1m12s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 1m7s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 1m19s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 1m32s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 1m22s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 1m32s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 1m35s
CI / Build-Artefakte aktuell (pull_request) Failing after 1m7s
- Laravel 13 mit Filament 5 (Admin), Inertia 3 + Vue 3 + Vuetify 4 (Material 3) - Anmeldung über Fortify (Login, Passwort vergessen/ändern), keine Selbstregistrierung - Profil mit Profilbild (WebP in festen Größen), Personenseiten, Header mit Avatar-Menü - Material-3-Farbschema aus der Primärfarbe, hell/dunkel/System pro Person - Basis-Branding (Portalname, Logo, Primärfarbe) im Admin unter „Erscheinungsbild“ - Benutzerverwaltung im Admin, Zugang vorläufig über is_admin - vendor/ (ohne Dev-Pakete) und Frontend-Build mitversioniert, Dev-Pakete in vendor-dev/ - PHPUnit-Tests gegen MySQL/MariaDB, Forgejo-CI-Matrix, Docker-Entwicklungsumgebung - VERSION, CHANGELOG.md und CONTRIBUTING.md mit Release-Ablauf Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
123 lines
4.3 KiB
PHP
123 lines
4.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Webauthn;
|
|
|
|
use CBOR\ByteStringObject;
|
|
use CBOR\Decoder;
|
|
use CBOR\ListObject;
|
|
use CBOR\MapObject;
|
|
use CBOR\NegativeIntegerObject;
|
|
use CBOR\TextStringObject;
|
|
use CBOR\UnsignedIntegerObject;
|
|
use function chr;
|
|
use function ord;
|
|
use Symfony\Component\Uid\Uuid;
|
|
use Webauthn\AuthenticationExtensions\AuthenticationExtensionLoader;
|
|
use Webauthn\Exception\InvalidDataException;
|
|
|
|
final readonly class AuthenticatorDataLoader
|
|
{
|
|
private Decoder $decoder;
|
|
|
|
private function __construct()
|
|
{
|
|
$this->decoder = Decoder::create();
|
|
}
|
|
|
|
public static function create(): self
|
|
{
|
|
return new self();
|
|
}
|
|
|
|
public function load(string $authData): AuthenticatorData
|
|
{
|
|
$authData = $this->fixIncorrectEdDSAKey($authData);
|
|
$authDataStream = new StringStream($authData);
|
|
$rp_id_hash = $authDataStream->read(32);
|
|
$flags = $authDataStream->read(1);
|
|
$signCountRaw = $authDataStream->read(4);
|
|
/** @var array{1: int} $signCount */
|
|
$signCount = unpack('N', $signCountRaw);
|
|
|
|
$attestedCredentialData = null;
|
|
if (0 !== (ord($flags) & AuthenticatorData::FLAG_AT)) {
|
|
$aaguid = Uuid::fromBinary($authDataStream->read(16));
|
|
$credentialLengthRaw = $authDataStream->read(2);
|
|
/** @var array{1: int} $credentialLength */
|
|
$credentialLength = unpack('n', $credentialLengthRaw);
|
|
$credentialId = $authDataStream->read($credentialLength[1]);
|
|
$credentialPublicKey = $this->decoder->decode($authDataStream);
|
|
$credentialPublicKey instanceof MapObject || throw InvalidDataException::create(
|
|
$authData,
|
|
'The data does not contain a valid credential public key.'
|
|
);
|
|
$attestedCredentialData = AttestedCredentialData::create(
|
|
$aaguid,
|
|
$credentialId,
|
|
(string) $credentialPublicKey
|
|
);
|
|
}
|
|
|
|
$extension = null;
|
|
if (0 !== (ord($flags) & AuthenticatorData::FLAG_ED)) {
|
|
$extension = $this->decoder->decode($authDataStream);
|
|
$extension = AuthenticationExtensionLoader::load($extension);
|
|
}
|
|
$authDataStream->isEOF() || throw InvalidDataException::create(
|
|
$authData,
|
|
'Invalid authentication data. Presence of extra bytes.'
|
|
);
|
|
$authDataStream->close();
|
|
|
|
return AuthenticatorData::create(
|
|
$authData,
|
|
$rp_id_hash,
|
|
$flags,
|
|
$signCount[1],
|
|
$attestedCredentialData,
|
|
$extension
|
|
);
|
|
}
|
|
|
|
private function fixIncorrectEdDSAKey(string $data): string
|
|
{
|
|
/** @var string $needle */
|
|
$needle = hex2bin('a301634f4b500327206745643235353139');
|
|
/** @var string $correct */
|
|
$correct = hex2bin('a401634f4b500327206745643235353139');
|
|
$position = strpos($data, $needle);
|
|
if ($position === false) {
|
|
return $data;
|
|
}
|
|
|
|
$begin = substr($data, 0, $position);
|
|
$end = substr($data, $position);
|
|
$end = str_replace($needle, $correct, $end);
|
|
$cbor = new StringStream($end);
|
|
$badKey = $this->decoder->decode($cbor);
|
|
|
|
($badKey instanceof MapObject && $cbor->isEOF()) || throw InvalidDataException::create(
|
|
$end,
|
|
'Invalid authentication data. Presence of extra bytes.'
|
|
);
|
|
$badX = $badKey->get(-2);
|
|
$badX instanceof ListObject || throw InvalidDataException::create($end, 'Invalid authentication data.');
|
|
/** @var list<string> $normalizedBadX */
|
|
$normalizedBadX = $badX->normalize();
|
|
$keyBytes = array_reduce(
|
|
$normalizedBadX,
|
|
static fn (string $carry, string $item): string => $carry . chr((int) $item),
|
|
''
|
|
);
|
|
$correctX = ByteStringObject::create($keyBytes);
|
|
$correctKey = MapObject::create()
|
|
->add(UnsignedIntegerObject::create(1), TextStringObject::create('OKP'))
|
|
->add(UnsignedIntegerObject::create(3), NegativeIntegerObject::create(-8))
|
|
->add(NegativeIntegerObject::create(-1), TextStringObject::create('Ed25519'))
|
|
->add(NegativeIntegerObject::create(-2), $correctX);
|
|
|
|
return $begin . $correctKey;
|
|
}
|
|
}
|