| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Webauthn\Denormalizer; |
| 6 |
|
| 7 |
use function assert; |
| 8 |
use CBOR\ByteStringObject; |
| 9 |
use CBOR\Decoder; |
| 10 |
use CBOR\ListObject; |
| 11 |
use CBOR\MapObject; |
| 12 |
use CBOR\NegativeIntegerObject; |
| 13 |
use CBOR\TextStringObject; |
| 14 |
use CBOR\UnsignedIntegerObject; |
| 15 |
use function chr; |
| 16 |
use function is_string; |
| 17 |
use function ord; |
| 18 |
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; |
| 19 |
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait; |
| 20 |
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
| 21 |
use Symfony\Component\Uid\Uuid; |
| 22 |
use Webauthn\AttestedCredentialData; |
| 23 |
use Webauthn\AuthenticationExtensions\AuthenticationExtensionLoader; |
| 24 |
use Webauthn\AuthenticationExtensions\AuthenticationExtensions; |
| 25 |
use Webauthn\AuthenticatorData; |
| 26 |
use Webauthn\Exception\InvalidDataException; |
| 27 |
use Webauthn\StringStream; |
| 28 |
|
| 29 |
final class AuthenticatorDataDenormalizer implements DenormalizerInterface, DenormalizerAwareInterface |
| 30 |
{ |
| 31 |
use DenormalizerAwareTrait; |
| 32 |
|
| 33 |
private readonly Decoder $decoder; |
| 34 |
|
| 35 |
public function __construct() |
| 36 |
{ |
| 37 |
$this->decoder = Decoder::create(); |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed |
| 44 |
{ |
| 45 |
assert(is_string($data)); |
| 46 |
$authData = $this->fixIncorrectEdDSAKey($data); |
| 47 |
$authDataStream = new StringStream($authData); |
| 48 |
$rp_id_hash = $authDataStream->read(32); |
| 49 |
$flags = $authDataStream->read(1); |
| 50 |
$signCount = $authDataStream->read(4); |
| 51 |
|
| 52 |
$signCount = unpack('N', $signCount); |
| 53 |
|
| 54 |
$attestedCredentialData = null; |
| 55 |
if (0 !== (ord($flags) & AuthenticatorData::FLAG_AT)) { |
| 56 |
$aaguid = Uuid::fromBinary($authDataStream->read(16)); |
| 57 |
$credentialLength = $authDataStream->read(2); |
| 58 |
|
| 59 |
$credentialLength = unpack('n', $credentialLength); |
| 60 |
$credentialId = $authDataStream->read($credentialLength[1]); |
| 61 |
$credentialPublicKey = $this->decoder->decode($authDataStream); |
| 62 |
$credentialPublicKey instanceof MapObject || throw InvalidDataException::create( |
| 63 |
$authData, |
| 64 |
'The data does not contain a valid credential public key.' |
| 65 |
); |
| 66 |
$attestedCredentialData = AttestedCredentialData::create( |
| 67 |
$aaguid, |
| 68 |
$credentialId, |
| 69 |
(string) $credentialPublicKey, |
| 70 |
); |
| 71 |
} |
| 72 |
$extension = null; |
| 73 |
if (0 !== (ord($flags) & AuthenticatorData::FLAG_ED)) { |
| 74 |
$extension = $this->decoder->decode($authDataStream); |
| 75 |
$extension = AuthenticationExtensionLoader::load($extension); |
| 76 |
} |
| 77 |
$authDataStream->isEOF() || throw InvalidDataException::create( |
| 78 |
$authData, |
| 79 |
'Invalid authentication data. Presence of extra bytes.' |
| 80 |
); |
| 81 |
$authDataStream->close(); |
| 82 |
|
| 83 |
return AuthenticatorData::create( |
| 84 |
$authData, |
| 85 |
$rp_id_hash, |
| 86 |
$flags, |
| 87 |
$signCount[1], |
| 88 |
$attestedCredentialData, |
| 89 |
$extension === null ? null : $this->denormalizer->denormalize( |
| 90 |
$extension, |
| 91 |
AuthenticationExtensions::class, |
| 92 |
$format, |
| 93 |
$context |
| 94 |
), |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
public function supportsDenormalization( |
| 99 |
mixed $data, |
| 100 |
string $type, |
| 101 |
?string $format = null, |
| 102 |
array $context = [] |
| 103 |
): bool { |
| 104 |
return $type === AuthenticatorData::class; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public function getSupportedTypes(?string $format): array |
| 111 |
{ |
| 112 |
return [ |
| 113 |
AuthenticatorData::class => true, |
| 114 |
]; |
| 115 |
} |
| 116 |
|
| 117 |
private function fixIncorrectEdDSAKey(string $data): string |
| 118 |
{ |
| 119 |
|
| 120 |
$needle = hex2bin('a301634f4b500327206745643235353139'); |
| 121 |
|
| 122 |
$correct = hex2bin('a401634f4b500327206745643235353139'); |
| 123 |
$position = strpos($data, $needle); |
| 124 |
if ($position === false) { |
| 125 |
return $data; |
| 126 |
} |
| 127 |
|
| 128 |
$begin = substr($data, 0, $position); |
| 129 |
$end = substr($data, $position); |
| 130 |
$end = str_replace($needle, $correct, $end); |
| 131 |
$cbor = new StringStream($end); |
| 132 |
$badKey = $this->decoder->decode($cbor); |
| 133 |
|
| 134 |
($badKey instanceof MapObject && $cbor->isEOF()) || throw InvalidDataException::create( |
| 135 |
$end, |
| 136 |
'Invalid authentication data. Presence of extra bytes.' |
| 137 |
); |
| 138 |
$badX = $badKey->get(-2); |
| 139 |
$badX instanceof ListObject || throw InvalidDataException::create($end, 'Invalid authentication data.'); |
| 140 |
|
| 141 |
$normalizedBadX = $badX->normalize(); |
| 142 |
$keyBytes = array_reduce( |
| 143 |
$normalizedBadX, |
| 144 |
static fn (string $carry, string $item): string => $carry . chr((int) $item), |
| 145 |
'' |
| 146 |
); |
| 147 |
$correctX = ByteStringObject::create($keyBytes); |
| 148 |
$correctKey = MapObject::create() |
| 149 |
->add(UnsignedIntegerObject::create(1), TextStringObject::create('OKP')) |
| 150 |
->add(UnsignedIntegerObject::create(3), NegativeIntegerObject::create(-8)) |
| 151 |
->add(NegativeIntegerObject::create(-1), TextStringObject::create('Ed25519')) |
| 152 |
->add(NegativeIntegerObject::create(-2), $correctX); |
| 153 |
|
| 154 |
return $begin . $correctKey; |
| 155 |
} |
| 156 |
} |