/
AttestationObjectTest.php
134 lines (118 loc) · 4.95 KB
/
AttestationObjectTest.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
<?php
namespace MadWizard\WebAuthn\Tests\Attestation;
use MadWizard\WebAuthn\Attestation\AttestationObject;
use MadWizard\WebAuthn\Exception\WebAuthnException;
use MadWizard\WebAuthn\Format\ByteBuffer;
use MadWizard\WebAuthn\Json\JsonConverter;
use MadWizard\WebAuthn\Tests\Helper\FixtureHelper;
use MadWizard\WebAuthn\Tests\Helper\HexData;
use PHPUnit\Framework\TestCase;
use function bin2hex;
class AttestationObjectTest extends TestCase
{
public function testInvalidEmpty()
{
$this->expectException(WebAuthnException::class);
AttestationObject::parse(new ByteBuffer(''));
}
public function testInvalidType()
{
$this->expectException(WebAuthnException::class);
$this->expectExceptionMessageMatches('~expecting.+Cbor map~i');
AttestationObject::parse(ByteBuffer::fromHex('10'));
}
public function testFormatType()
{
$buf = HexData::buf(
'A3 # map(3)
63 # text(3)
666D74 # "fmt"
10 # unsigned(16)
67 # text(7)
61747453746D74 # "attStmt"
A0 # map(0)
68 # text(8)
6175746844617461 # "authData"
41 # bytes(1)
AA # "\xAA"
'
);
$this->expectException(WebAuthnException::class);
$this->expectExceptionMessageMatches('~expecting.+fmt.+string~i');
AttestationObject::parse($buf);
}
public function testStatementType()
{
$buf = HexData::buf(
'A3 # map(3)
63 # text(3)
666D74 # "fmt"
68 # text(8)
6669646F2D753266 # "fido-u2f"
67 # text(7)
61747453746D74 # "attStmt"
10 # unsigned(16)
68 # text(8)
6175746844617461 # "authData"
41 # bytes(1)
AA # "\xAA"
'
);
$this->expectException(WebAuthnException::class);
$this->expectExceptionMessageMatches('~Expecting key "attStmt".+CborMap~i');
AttestationObject::parse($buf);
}
public function testAuthDataType()
{
$buf = HexData::buf(
'A3 # map(3)
63 # text(3)
666D74 # "fmt"
68 # text(8)
6669646F2D753266 # "fido-u2f"
67 # text(7)
61747453746D74 # "attStmt"
A0 # map(0)
68 # text(8)
6175746844617461 # "authData"
10 # unsigned(16)
'
);
$this->expectException(WebAuthnException::class);
$this->expectExceptionMessageMatches('~expecting.+authData.+byte~i');
AttestationObject::parse($buf);
}
public function testU2f()
{
$json = FixtureHelper::getJsonFixture('fido2-helpers/attestation.json');
$message = $json['challengeResponseAttestationU2fMsgB64Url'];
$message['type'] = 'public-key';
$cred = JsonConverter::decodeAttestation($message);
self::assertSame('Bo-VjHOkJZy8DjnCJnIc0Oxt9QAz5upMdSJxNbd-GyAo6MNIvPBb9YsUlE0ZJaaWXtWH5FQyPS6bT_e698IirQ', $cred->getId());
self::assertSame(
'{"challenge":"Vu8uDqnkwOjd83KLj6Scn2BgFNLFbGR7Kq_XJJwQnnatztUR7XIBL7K8uMPCIaQmKw1MCVQ5aazNJFk7NakgqA",' .
'"clientExtensions":{},"hashAlgorithm":"SHA-256","origin":"https://localhost:8443","type":"webauthn.create"}',
$cred->getResponse()->getClientDataJson()
);
$response = $cred->getResponse()->asAttestationResponse();
$buffer = $response->getAttestationObject();
$decoded = AttestationObject::parse($buffer);
self::assertSame('fido-u2f', $decoded->getFormat());
$authData = HexData::bin(
'49960de5880e8c687434170f6476605b8fe4aeb9a28632c7995cf3ba831d9763
41
00000000
0000000000000000000000000000000
00040
068f958c73a4259cbc0e39c226721cd0ec6df50033e6ea4c75227135b77e1b2
028e8c348bcf05bf58b14944d1925a6965ed587e454323d2e9b4ff7baf7c222
ada50102032620012158203573d008787e6c37ac7543edaa47bbf6e79b64786
6d6b34102083c37e642460422582018d3531aee69d8c514c9d6951e6b3c9af6
dec0494fda9ec58f4f09cf68f21993
'
);
self::assertSame(bin2hex($authData), $decoded->getAuthenticatorData()->getHex());
$statement = $decoded->getStatement();
self::assertTrue($statement->has('x5c'));
}
}