Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Webauthn\CeremonyStep;
6
7 use function is_string;
8 use function trigger_deprecation;
9 use Webauthn\AuthenticationExtensions\AuthenticationExtensions;
10 use Webauthn\AuthenticatorAssertionResponse;
11 use Webauthn\AuthenticatorAttestationResponse;
12 use Webauthn\CredentialRecord;
13 use Webauthn\Exception\AuthenticatorResponseVerificationException;
14 use Webauthn\PublicKeyCredentialCreationOptions;
15 use Webauthn\PublicKeyCredentialRequestOptions;
16 use Webauthn\PublicKeyCredentialSource;
17 use Webauthn\U2FPublicKey;
18
19 final class CheckRelyingPartyIdIdHash implements CeremonyStep
20 {
21 public function process(
22 CredentialRecord $credentialRecord,
23 AuthenticatorAssertionResponse|AuthenticatorAttestationResponse $authenticatorResponse,
24 PublicKeyCredentialRequestOptions|PublicKeyCredentialCreationOptions $publicKeyCredentialOptions,
25 ?string $userHandle,
26 string $host
27 ): void {
28 if ($credentialRecord instanceof PublicKeyCredentialSource) {
29 trigger_deprecation(
30 'web-auth/webauthn-lib',
31 '5.3',
32 'Passing a PublicKeyCredentialSource to "%s::process()" is deprecated, pass a CredentialRecord instead.',
33 self::class
34 );
35 }
36 $authData = $authenticatorResponse instanceof AuthenticatorAssertionResponse ? $authenticatorResponse->authenticatorData : $authenticatorResponse->attestationObject->authData;
37 $C = $authenticatorResponse->clientDataJSON;
38 $attestedCredentialData = $credentialRecord->getAttestedCredentialData();
39 $credentialPublicKey = $attestedCredentialData->credentialPublicKey;
40 $credentialPublicKey !== null || throw AuthenticatorResponseVerificationException::create(
41 'No public key available.'
42 );
43 $isU2F = U2FPublicKey::isU2FKey($credentialPublicKey);
44 $rpId = $publicKeyCredentialOptions->rpId ?? $publicKeyCredentialOptions->rp->id ?? $host;
45 $facetId = $this->getFacetId($rpId, $publicKeyCredentialOptions->extensions, $authData ->extensions);
46 $rpIdHash = hash('sha256', $isU2F ? $C->origin : $facetId, true);
47 hash_equals(
48 $rpIdHash,
49 $authData
50 ->rpIdHash
51 ) || throw AuthenticatorResponseVerificationException::create('rpId hash mismatch.');
52 }
53
54 private function getFacetId(
55 string $rpId,
56 AuthenticationExtensions $AuthenticationExtensions,
57 null|AuthenticationExtensions $authenticationExtensionsClientOutputs
58 ): string {
59 if ($authenticationExtensionsClientOutputs === null || ! $AuthenticationExtensions->has(
60 'appid'
61 ) || ! $authenticationExtensionsClientOutputs->has('appid')) {
62 return $rpId;
63 }
64 $appId = $AuthenticationExtensions->get('appid')
65 ->value;
66 $wasUsed = $authenticationExtensionsClientOutputs->get('appid')
67 ->value;
68 if (! is_string($appId) || $wasUsed !== true) {
69 return $rpId;
70 }
71 return $appId;
72 }
73 }