This repository has been archived by the owner on Mar 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
AuthenticatorData.php
179 lines (154 loc) · 4.98 KB
/
AuthenticatorData.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
declare(strict_types=1);
namespace Firehed\U2F\WebAuthn;
use BadMethodCallException;
use Firehed\CBOR\Decoder;
/**
* @internal
*
* @phpstan-type AttestedCredentialData array{
* aaguid: string,
* credentialId: string,
* credentialPublicKey: array{
* 1: int,
* 3?: int,
* -1: int,
* -2?: string,
* -3?: string,
* -4?: string,
* }
* }
*/
class AuthenticatorData
{
/** @var bool */
private $isUserPresent;
/** @var bool */
private $isUserVerified;
/** @var string (binary) */
private $rpIdHash;
/** @var int */
private $signCount;
/**
* @var ?AttestedCredentialData Attested Credential Data
*/
private $ACD;
/** @var null RESERVED: WebAuthn Extensions */
private $extensions;
/**
* @see https://w3c.github.io/webauthn/#sec-authenticator-data
* WebAuthn 6.1
*/
public static function parse(string $bytes): AuthenticatorData
{
assert(strlen($bytes) >= 37);
$rpIdHash = substr($bytes, 0, 32);
$flags = ord(substr($bytes, 32, 1));
$UP = ($flags & 0x01) === 0x01; // bit 0
$UV = ($flags & 0x04) === 0x04; // bit 2
$AT = ($flags & 0x40) === 0x40; // bit 6
$ED = ($flags & 0x80) === 0x80; // bit 7
$signCount = unpack('N', substr($bytes, 33, 4))[1];
$authData = new AuthenticatorData();
$authData->isUserPresent = $UP;
$authData->isUserVerified = $UV;
$authData->rpIdHash = $rpIdHash;
$authData->signCount = $signCount;
$restOfBytes = substr($bytes, 37);
$restOfBytesLength = strlen($restOfBytes);
if ($AT) {
assert($restOfBytesLength >= 18);
$aaguid = substr($restOfBytes, 0, 16);
$credentialIdLength = unpack('n', substr($restOfBytes, 16, 2))[1];
assert($restOfBytesLength >= (18 + $credentialIdLength));
$credentialId = substr($restOfBytes, 18, $credentialIdLength);
$rawCredentialPublicKey = substr($restOfBytes, 18 + $credentialIdLength);
$decoder = new Decoder();
$credentialPublicKey = $decoder->decode($rawCredentialPublicKey);
$authData->ACD = [
'aaguid' => $aaguid,
'credentialId' => $credentialId,
'credentialPublicKey' => $credentialPublicKey,
];
// var_dump($decoder->getNumberOfBytesRead());
// cut rest of bytes down based on that ^ ?
}
if ($ED) {
// @codeCoverageIgnoreStart
throw new BadMethodCallException('Not implemented yet');
// @codeCoverageIgnoreEnd
}
return $authData;
}
/** @return ?AttestedCredentialData */
public function getAttestedCredentialData(): ?array
{
return $this->ACD;
}
public function getRpIdHash(): string
{
return $this->rpIdHash;
}
public function getSignCount(): int
{
return $this->signCount;
}
public function isUserPresent(): bool
{
return $this->isUserPresent;
}
/**
* @return array{
* isUserPresent: bool,
* isUserVerified: bool,
* rpIdHash: string,
* signCount: int,
* ACD?: array{
* aaguid: string,
* credentialId: string,
* credentialPublicKey: array{
* kty: int,
* alg: ?int,
* crv: int,
* x: string,
* y: string,
* d: string,
* },
* },
* }
*/
public function __debugInfo(): array
{
$hex = function ($str) {
return '0x' . bin2hex($str);
};
$data = [
'isUserPresent' => $this->isUserPresent,
'isUserVerified' => $this->isUserVerified,
'rpIdHash' => $hex($this->rpIdHash),
'signCount' => $this->signCount,
];
if ($this->ACD) {
// See RFC8152 section 7 (COSE key parameters)
$pk = [
'kty' => $this->ACD['credentialPublicKey'][1], // MUST be 'EC2' (sec 13 tbl 21)
// kid = 2
'alg' => $this->ACD['credentialPublicKey'][3] ?? null,
// key_ops = 4 // must include sign (1)/verify(2) if present, depending on usage
// Base IV = 5
// this would be 'k' if 'kty'===4(Symmetric)
'crv' => $this->ACD['credentialPublicKey'][-1], // (13.1 tbl 22)
'x' => $hex($this->ACD['credentialPublicKey'][-2] ?? ''), // (13.1.1 tbl 23/13.2 tbl 24)
'y' => $hex($this->ACD['credentialPublicKey'][-3] ?? ''), // (13.1.1 tbl 23)
'd' => $hex($this->ACD['credentialPublicKey'][-4] ?? ''), // (13.2 tbl 24)
];
$acd = [
'aaguid' => $hex($this->ACD['aaguid']),
'credentialId' => $hex($this->ACD['credentialId']),
'credentialPublicKey' => $pk,
];
$data['ACD'] = $acd;
}
return $data;
}
}