Debian Codegraph @

lib/3rdParty/composer/web-auth/webauthn-lib/src/AttestationStatement/AppleAttestationStatementSupport.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 CBOR\Decoder;
9 use CBOR\Normalizable;
10 use Cose\Key\Ec2Key;
11 use Cose\Key\Key;
12 use Cose\Key\RsaKey;
13 use function count;
14 use Psr\EventDispatcher\EventDispatcherInterface;
15 use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence;
16 use SpomkyLabs\Pki\ASN1\Type\Primitive\OctetString;
17 use SpomkyLabs\Pki\ASN1\Type\Tagged\ExplicitTagging;
18 use SpomkyLabs\Pki\CryptoEncoding\PEM;
19 use SpomkyLabs\Pki\X509\Certificate\Certificate;
20 use SpomkyLabs\Pki\X509\Certificate\Extension\UnknownExtension;
21 use Webauthn\AuthenticatorData;
22 use Webauthn\Event\AttestationStatementLoaded;
23 use Webauthn\Event\CanDispatchEvents;
24 use Webauthn\Event\NullEventDispatcher;
25 use Webauthn\Exception\AttestationStatementLoadingException;
26 use Webauthn\Exception\AttestationStatementVerificationException;
27 use Webauthn\Exception\InvalidAttestationStatementException;
28 use Webauthn\MetadataService\CertificateChain\CertificateToolbox;
29 use Webauthn\StringStream;
30 use Webauthn\TrustPath\CertificateTrustPath;
31
32 final class AppleAttestationStatementSupport implements AttestationStatementSupport, CanDispatchEvents
33 {
34 private const OID_APPLE = '1.2.840.113635.100.8.2';
35
36 private readonly Decoder $decoder;
37
38 private EventDispatcherInterface $dispatcher;
39
40 public function __construct()
41 {
42 $this->decoder = Decoder::create();
43 $this->dispatcher = new NullEventDispatcher();
44 }
45
46 public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void
47 {
48 $this->dispatcher = $eventDispatcher;
49 }
50
51 public static function create(): self
52 {
53 return new self();
54 }
55
56 public function name(): string
57 {
58 return 'apple';
59 }
60
61 /**
62 * @param array<string, mixed> $attestation
63 */
64 public function load(array $attestation): AttestationStatement
65 {
66 array_key_exists('attStmt', $attestation) || throw AttestationStatementLoadingException::create(
67 $attestation,
68 'Invalid attestation object'
69 );
70 /** @var array<string, mixed> $attStmt */
71 $attStmt = $attestation['attStmt'];
72 array_key_exists('x5c', $attStmt) || throw AttestationStatementLoadingException::create(
73 $attestation,
74 'The attestation statement value "x5c" is missing.'
75 );
76 /** @var array<string> $certificates */
77 $certificates = $attStmt['x5c'];
78 (is_countable($certificates) ? count(
79 $certificates
80 ) : 0) > 0 || throw AttestationStatementLoadingException::create(
81 $attestation,
82 'The attestation statement value "x5c" must be a list with at least one certificate.'
83 );
84 $certificates = CertificateToolbox::convertAllDERToPEM($certificates);
85
86 /** @var string $fmt */
87 $fmt = $attestation['fmt'];
88 $attestationStatement = AttestationStatement::createAnonymizationCA(
89 $fmt,
90 $attStmt,
91 CertificateTrustPath::create($certificates)
92 );
93 $this->dispatcher->dispatch(AttestationStatementLoaded::create($attestationStatement));
94
95 return $attestationStatement;
96 }
97
98 public function isValid(
99 string $clientDataJSONHash,
100 AttestationStatement $attestationStatement,
101 AuthenticatorData $authenticatorData
102 ): bool {
103 $trustPath = $attestationStatement->trustPath;
104 $trustPath instanceof CertificateTrustPath || throw InvalidAttestationStatementException::create(
105 $attestationStatement,
106 'Invalid trust path'
107 );
108
109 $certificates = $trustPath->certificates;
110
111 //Decode leaf attestation certificate
112 $leaf = $certificates[0];
113
114 $this->checkCertificateAndGetPublicKey($leaf, $clientDataJSONHash, $authenticatorData);
115
116 return true;
117 }
118
119 /**
120 * @see https://www.w3.org/TR/webauthn-3/#sctn-apple-anonymous-attestation
121 */
122 private function checkCertificateAndGetPublicKey(
123 string $certificate,
124 string $clientDataHash,
125 AuthenticatorData $authenticatorData
126 ): void {
127 //Check that authData publicKey matches the public key in the attestation certificate
128 $attestedCredentialData = $authenticatorData->attestedCredentialData;
129 $attestedCredentialData !== null || throw AttestationStatementVerificationException::create(
130 'No attested credential data found'
131 );
132 $publicKeyData = $attestedCredentialData->credentialPublicKey;
133 $publicKeyData !== null || throw AttestationStatementVerificationException::create(
134 'No attested public key found'
135 );
136 $publicDataStream = new StringStream($publicKeyData);
137 $coseKey = $this->decoder->decode($publicDataStream);
138 $coseKey instanceof Normalizable || throw AttestationStatementVerificationException::create(
139 'Invalid attested public key found'
140 );
141 $publicDataStream->isEOF() || throw AttestationStatementVerificationException::create(
142 'Invalid public key data. Presence of extra bytes.'
143 );
144 $publicDataStream->close();
145 /** @var array<int, mixed> $coseKeyData */
146 $coseKeyData = $coseKey->normalize();
147 $publicKey = Key::createFromData($coseKeyData);
148
149 ($publicKey instanceof Ec2Key) || ($publicKey instanceof RsaKey) || throw AttestationStatementVerificationException::create(
150 'Unsupported key type'
151 );
152
153 /*---------------------------*/
154 $cert = Certificate::fromPEM(PEM::fromString($certificate));
155
156 //We check the attested key corresponds to the key in the certificate
157 PEM::fromString($publicKey->asPEM())->string() === $cert->tbsCertificate()
158 ->subjectPublicKeyInfo()
159 ->toPEM()
160 ->string() || throw AttestationStatementVerificationException::create('Invalid key');
161
162 $extensions = $cert->tbsCertificate()
163 ->extensions();
164
165 //Find Apple Extension with OID "1.2.840.113635.100.8.2" in certificate extensions
166 $extensions->has(self::OID_APPLE) || throw AttestationStatementVerificationException::create(
167 'The certificate extension "' . self::OID_APPLE . '" is missing'
168 );
169 /** @var UnknownExtension $appleExtension */
170 $appleExtension = $extensions->get(self::OID_APPLE);
171 $extensionSequence = Sequence::fromDER($appleExtension->extensionValue());
172 $extensionSequence->has(0) || throw AttestationStatementVerificationException::create(
173 'The certificate extension "' . self::OID_APPLE . '" is message'
174 );
175 $firstExtension = $extensionSequence->at(0);
176 $firstExtension->isTagged() || throw AttestationStatementVerificationException::create(
177 'The certificate extension "' . self::OID_APPLE . '" is invalid'
178 );
179 $taggedExtension = $firstExtension->asTagged()
180 ->asElement();
181 $taggedExtension instanceof ExplicitTagging || throw AttestationStatementVerificationException::create(
182 'The certificate extension "' . self::OID_APPLE . '" is invalid'
183 );
184 $explicitExtension = $taggedExtension->explicit()
185 ->asElement();
186 $explicitExtension instanceof OctetString || throw AttestationStatementVerificationException::create(
187 'The certificate extension "' . self::OID_APPLE . '" is invalid'
188 );
189 $extensionData = $explicitExtension->string();
190
191 $nonceToHash = $authenticatorData->authData . $clientDataHash;
192 $nonce = hash('sha256', $nonceToHash, true);
193
194 hash_equals($nonce, $extensionData) || throw AttestationStatementVerificationException::create(
195 'The client data hash is not valid'
196 );
197 }
198 }