Debian Codegraph @

lib/3rdParty/composer/web-auth/webauthn-lib/src/CeremonyStep/CheckSignature.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Webauthn\CeremonyStep;
6
7 use CBOR\Decoder;
8 use CBOR\Normalizable;
9 use Cose\Algorithm\Manager;
10 use Cose\Algorithm\Signature\ECDSA\ES256;
11 use Cose\Algorithm\Signature\RSA\RS256;
12 use Cose\Algorithm\Signature\Signature;
13 use Cose\Key\Key;
14 use function is_array;
15 use function trigger_deprecation;
16 use Webauthn\AuthenticatorAssertionResponse;
17 use Webauthn\AuthenticatorAttestationResponse;
18 use Webauthn\CredentialRecord;
19 use Webauthn\Exception\AuthenticatorResponseVerificationException;
20 use Webauthn\PublicKeyCredentialCreationOptions;
21 use Webauthn\PublicKeyCredentialRequestOptions;
22 use Webauthn\PublicKeyCredentialSource;
23 use Webauthn\StringStream;
24 use Webauthn\U2FPublicKey;
25 use Webauthn\Util\CoseSignatureFixer;
26
27 final readonly class CheckSignature implements CeremonyStep
28 {
29 private Manager $algorithmManager;
30
31 public function __construct(null|Manager $algorithmManager = null)
32 {
33 $this->algorithmManager = $algorithmManager ?? Manager::create()->add(ES256::create(), RS256::create());
34 }
35
36 public function process(
37 CredentialRecord $credentialRecord,
38 AuthenticatorAssertionResponse|AuthenticatorAttestationResponse $authenticatorResponse,
39 PublicKeyCredentialRequestOptions|PublicKeyCredentialCreationOptions $publicKeyCredentialOptions,
40 ?string $userHandle,
41 string $host
42 ): void {
43 if ($credentialRecord instanceof PublicKeyCredentialSource) {
44 trigger_deprecation(
45 'web-auth/webauthn-lib',
46 '5.3',
47 'Passing a PublicKeyCredentialSource to "%s::process()" is deprecated, pass a CredentialRecord instead.',
48 self::class
49 );
50 }
51 if (! $authenticatorResponse instanceof AuthenticatorAssertionResponse) {
52 return;
53 }
54 $credentialPublicKey = $credentialRecord->getAttestedCredentialData()
55 ->credentialPublicKey;
56 $credentialPublicKey !== null || throw AuthenticatorResponseVerificationException::create(
57 'No public key available.'
58 );
59 $coseKey = $this->getCoseKey($credentialPublicKey);
60
61 $getClientDataJSONHash = hash('sha256', $authenticatorResponse->clientDataJSON->rawData, true);
62 $dataToVerify = $authenticatorResponse->authenticatorData->authData . $getClientDataJSONHash;
63 $signature = $authenticatorResponse->signature;
64 $algorithm = $this->algorithmManager->get($coseKey->alg());
65 $algorithm instanceof Signature || throw AuthenticatorResponseVerificationException::create(
66 'Invalid algorithm identifier. Should refer to a signature algorithm'
67 );
68 $signature = CoseSignatureFixer::fix($signature, $algorithm);
69 $algorithm->verify(
70 $dataToVerify,
71 $coseKey,
72 $signature
73 ) || throw AuthenticatorResponseVerificationException::create('Invalid signature.');
74 }
75
76 private function getCoseKey(string $credentialPublicKey): Key
77 {
78 $isU2F = U2FPublicKey::isU2FKey($credentialPublicKey);
79 if ($isU2F === true) {
80 $credentialPublicKey = U2FPublicKey::convertToCoseKey($credentialPublicKey);
81 }
82 $stream = new StringStream($credentialPublicKey);
83 $credentialPublicKeyStream = Decoder::create()->decode($stream);
84 $stream->isEOF() || throw AuthenticatorResponseVerificationException::create(
85 'Invalid key. Presence of extra bytes.'
86 );
87 $stream->close();
88 $credentialPublicKeyStream instanceof Normalizable || throw AuthenticatorResponseVerificationException::create(
89 'Invalid attestation object. Unexpected object.'
90 );
91 $normalizedData = $credentialPublicKeyStream->normalize();
92 is_array($normalizedData) || throw AuthenticatorResponseVerificationException::create(
93 'Invalid attestation object. Unexpected object.'
94 );
95 /** @var array<int|string, mixed> $normalizedData */
96
97 return Key::create($normalizedData);
98 }
99 }