Debian Codegraph @

lib/3rdParty/composer/web-auth/webauthn-lib/src/CeremonyStep/CheckAlgorithm.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\Algorithms;
10 use Cose\Key\Key;
11 use function count;
12 use function in_array;
13 use function is_array;
14 use function sprintf;
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
26 class CheckAlgorithm implements CeremonyStep
27 {
28 public function process(
29 CredentialRecord $credentialRecord,
30 AuthenticatorAssertionResponse|AuthenticatorAttestationResponse $authenticatorResponse,
31 PublicKeyCredentialRequestOptions|PublicKeyCredentialCreationOptions $publicKeyCredentialOptions,
32 ?string $userHandle,
33 string $host
34 ): void {
35 if ($credentialRecord instanceof PublicKeyCredentialSource) {
36 trigger_deprecation(
37 'web-auth/webauthn-lib',
38 '5.3',
39 'Passing a PublicKeyCredentialSource to "%s::process()" is deprecated, pass a CredentialRecord instead.',
40 self::class
41 );
42 }
43 if (! $publicKeyCredentialOptions instanceof PublicKeyCredentialCreationOptions) {
44 return;
45 }
46 $credentialPublicKey = $credentialRecord->getAttestedCredentialData()
47 ->credentialPublicKey;
48 $credentialPublicKey !== null || throw AuthenticatorResponseVerificationException::create(
49 'No public key available.'
50 );
51 $algorithms = array_map(
52 fn ($pubKeyCredParam) => $pubKeyCredParam->alg,
53 $publicKeyCredentialOptions->pubKeyCredParams
54 );
55 if (count($algorithms) === 0) {
56 $algorithms = [Algorithms::COSE_ALGORITHM_ES256, Algorithms::COSE_ALGORITHM_RS256];
57 }
58 $coseKey = $this->getCoseKey($credentialPublicKey);
59 in_array($coseKey->alg(), $algorithms, true) || throw AuthenticatorResponseVerificationException::create(
60 sprintf('Invalid algorithm. Expected one of %s but got %d', implode(', ', $algorithms), $coseKey->alg())
61 );
62 }
63
64 private function getCoseKey(string $credentialPublicKey): Key
65 {
66 $isU2F = U2FPublicKey::isU2FKey($credentialPublicKey);
67 if ($isU2F === true) {
68 $credentialPublicKey = U2FPublicKey::convertToCoseKey($credentialPublicKey);
69 }
70 $stream = new StringStream($credentialPublicKey);
71 $credentialPublicKeyStream = Decoder::create()->decode($stream);
72 $stream->isEOF() || throw AuthenticatorResponseVerificationException::create(
73 'Invalid key. Presence of extra bytes.'
74 );
75 $stream->close();
76 $credentialPublicKeyStream instanceof Normalizable || throw AuthenticatorResponseVerificationException::create(
77 'Invalid attestation object. Unexpected object.'
78 );
79 $normalizedData = $credentialPublicKeyStream->normalize();
80 is_array($normalizedData) || throw AuthenticatorResponseVerificationException::create(
81 'Invalid attestation object. Unexpected object.'
82 );
83 /** @var array<int|string, mixed> $normalizedData */
84
85 return Key::create($normalizedData);
86 }
87 }