Debian Codegraph @

lib/3rdParty/composer/web-auth/webauthn-lib/src/AttestationStatement/CompoundAttestationStatementSupport.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Webauthn\AttestationStatement;
6
7 use function array_key_exists;
8 use function count;
9 use InvalidArgumentException;
10 use function is_array;
11 use Psr\EventDispatcher\EventDispatcherInterface;
12 use function sprintf;
13 use Throwable;
14 use Webauthn\AuthenticatorData;
15 use Webauthn\Event\AttestationStatementLoaded;
16 use Webauthn\Event\CanDispatchEvents;
17 use Webauthn\Event\NullEventDispatcher;
18 use Webauthn\Exception\AttestationStatementLoadingException;
19 use Webauthn\Exception\AttestationStatementVerificationException;
20 use Webauthn\Exception\InvalidDataException;
21 use Webauthn\TrustPath\EmptyTrustPath;
22
23 /**
24 * Compound Attestation Statement Support
25 *
26 * Supports the "compound" attestation format as defined in WebAuthn Level 3
27 * @see https://w3c.github.io/webauthn/#sctn-compound-attestation
28 *
29 * The compound format allows multiple attestation statements of different formats
30 * to be included together. This is useful for hybrid authenticators or scenarios
31 * where multiple attestation proofs are needed.
32 */
33 final class CompoundAttestationStatementSupport implements AttestationStatementSupport, CanDispatchEvents, AttestationStatementSupportManagerAwareInterface
34 {
35 use AttestationStatementSupportManagerAwareTrait;
36
37 private const ATTESTATION_TYPE_TRUST_ORDER = [
38 AttestationStatement::TYPE_ATTCA,
39 AttestationStatement::TYPE_ANONCA,
40 AttestationStatement::TYPE_BASIC,
41 AttestationStatement::TYPE_SELF,
42 AttestationStatement::TYPE_NONE,
43 ];
44
45 private EventDispatcherInterface $dispatcher;
46
47 private ?float $ratio = 1.0;
48
49 private ?int $minimum = null;
50
51 public function __construct()
52 {
53 $this->dispatcher = new NullEventDispatcher();
54 $this->attestationStatementSupportManager = AttestationStatementSupportManager::create();
55 }
56
57 public function setMinimum(int $minimum): void
58 {
59 $this->checkRules(null, $minimum);
60 $this->minimum = $minimum;
61 $this->ratio = null;
62 }
63
64 public function setRatio(float $ratio): void
65 {
66 $this->checkRules($ratio, null);
67 $this->ratio = $ratio;
68 $this->minimum = null;
69 }
70
71 public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void
72 {
73 $this->dispatcher = $eventDispatcher;
74 }
75
76 public static function create(): self
77 {
78 return new self();
79 }
80
81 public function name(): string
82 {
83 return 'compound';
84 }
85
86 /**
87 * @param array<string, mixed> $attestation
88 */
89 public function load(array $attestation): AttestationStatement
90 {
91 array_key_exists('attStmt', $attestation) || throw AttestationStatementLoadingException::create($attestation);
92 is_array($attestation['attStmt']) || throw AttestationStatementLoadingException::create($attestation);
93
94 /** @var array<string, mixed> $attStmt */
95 $attStmt = $attestation['attStmt'];
96 $loadedAttestations = [];
97 foreach ($attStmt as $index => $nestedAttestationObject) {
98 is_array($nestedAttestationObject) || throw AttestationStatementLoadingException::create(
99 $attestation,
100 sprintf('Attestation at index %s must be an array.', (string) $index)
101 );
102
103 array_key_exists('fmt', $nestedAttestationObject) || throw InvalidDataException::create(
104 $nestedAttestationObject,
105 'Invalid attestation object'
106 );
107 /** @var string $fmt */
108 $fmt = $nestedAttestationObject['fmt'];
109 $fmt !== 'compound' || throw InvalidDataException::create(
110 $nestedAttestationObject,
111 'Compound attestation object in a compound attestation is not allowed.'
112 );
113 array_key_exists('attStmt', $nestedAttestationObject) || throw InvalidDataException::create(
114 $nestedAttestationObject,
115 'Invalid attestation object'
116 );
117 $this->attestationStatementSupportManager->has($fmt) || throw AttestationStatementLoadingException::create(
118 $attestation,
119 sprintf('Unsupported attestation format "%s" at index %s.', $fmt, (string) $index)
120 );
121
122 /** @var array<string, mixed> $nestedAttestationObject */
123 $attestationStatementSupport = $this->attestationStatementSupportManager->get($fmt);
124 $loadedAttestations[] = $attestationStatementSupport->load($nestedAttestationObject);
125 }
126
127 /** @var string $compoundFmt */
128 $compoundFmt = $attestation['fmt'];
129 $attestationType = $this->deriveAttestationType($loadedAttestations);
130 // Create a compound attestation statement with compound trust path
131 $attestationStatement = AttestationStatement::create(
132 $compoundFmt,
133 $loadedAttestations,
134 $attestationType,
135 EmptyTrustPath::create()
136 );
137
138 $this->dispatcher->dispatch(AttestationStatementLoaded::create($attestationStatement));
139
140 return $attestationStatement;
141 }
142
143 public function isValid(
144 string $clientDataJSONHash,
145 AttestationStatement $attestationStatement,
146 AuthenticatorData $authenticatorData
147 ): bool {
148 count($attestationStatement->attStmt) > 1 || throw AttestationStatementVerificationException::create(
149 'Compound attestation must contain at least two attestations.'
150 );
151
152 // Verify all nested attestations
153 $countValid = 0;
154 $total = count($attestationStatement->attStmt);
155
156 foreach ($attestationStatement->attStmt as $nestedAttestationObject) {
157 /** @var AttestationStatement $nestedAttestationObject */
158 $attestationStatementSupport = $this->attestationStatementSupportManager->get(
159 $nestedAttestationObject->fmt
160 );
161
162 try {
163 $isValid = $attestationStatementSupport->isValid(
164 $clientDataJSONHash,
165 $nestedAttestationObject,
166 $authenticatorData
167 );
168
169 if ($isValid) {
170 $countValid++;
171 }
172 } catch (Throwable) {
173 //Nothing to do
174 }
175 }
176
177 if ($this->minimum !== null) {
178 return $countValid >= $this->minimum;
179 }
180 return $countValid / $total >= $this->ratio;
181 }
182
183 /**
184 * Derives the compound attestation type from the nested attestation types
185 * by selecting the weakest (least trusted) type among them.
186 *
187 * @param array<AttestationStatement> $loadedAttestations
188 */
189 private function deriveAttestationType(array $loadedAttestations): string
190 {
191 $weakest = 0;
192 foreach ($loadedAttestations as $stmt) {
193 $index = array_search($stmt->type, self::ATTESTATION_TYPE_TRUST_ORDER, true);
194 if ($index === false) {
195 return AttestationStatement::TYPE_NONE;
196 }
197 if ($index > $weakest) {
198 $weakest = $index;
199 }
200 }
201
202 return self::ATTESTATION_TYPE_TRUST_ORDER[$weakest];
203 }
204
205 private function checkRules(?float $ratio, ?int $minimum): void
206 {
207 if ($ratio !== null && ($ratio <= 0 || $ratio > 1)) {
208 throw new InvalidArgumentException('The ratio must be greater than 0 and less than or equal to 1.');
209 }
210
211 if ($minimum !== null && $minimum <= 1) {
212 throw new InvalidArgumentException('The minimum must be greater than 1.');
213 }
214
215 if ($ratio !== null && $minimum !== null) {
216 throw new InvalidArgumentException('You cannot define both ratio and minimum at the same time.');
217 }
218
219 if ($ratio === null && $minimum === null) {
220 throw new InvalidArgumentException('You must define either ratio or minimum.');
221 }
222 }
223 }