|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* The MIT License (MIT) |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) 2014-2018 Spomky-Labs |
|
9
|
|
|
* |
|
10
|
|
|
* This software may be modified and distributed under the terms |
|
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace U2FAuthentication\Fido2; |
|
15
|
|
|
|
|
16
|
|
|
use U2FAuthentication\Fido2\AttestationStatement\AttestationStatementSupportManager; |
|
17
|
|
|
|
|
18
|
|
|
class PublicKeyCredentialCreationValidator |
|
19
|
|
|
{ |
|
20
|
|
|
private $attestationStatementSupportManager; |
|
21
|
|
|
private $credentialIdRepository; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(AttestationStatementSupportManager $attestationStatementSupportManager, CredentialIdRepository $credentialIdRepository) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->attestationStatementSupportManager = $attestationStatementSupportManager; |
|
26
|
|
|
$this->credentialIdRepository = $credentialIdRepository; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @see https://www.w3.org/TR/webauthn/#registering-a-new-credential |
|
31
|
|
|
*/ |
|
32
|
|
|
public function isValid(PublicKeyCredential $publicKeyCredential, PublicKeyCredentialCreationOptions $publicKeyCredentialCreationOptions, ?string $rpId = null): bool |
|
33
|
|
|
{ |
|
34
|
|
|
/** @see 7.1.2 */ |
|
35
|
|
|
$C = $publicKeyCredential->getResponse()->getClientDataJSON(); |
|
36
|
|
|
|
|
37
|
|
|
/** @see 7.1.3 */ |
|
38
|
|
|
if ('webauthn.create' !== $C->getType()) { |
|
39
|
|
|
return false; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** @see 7.1.4 */ |
|
43
|
|
|
if (hash_equals($publicKeyCredentialCreationOptions->getChallenge(), $C->getChallenge())) { |
|
44
|
|
|
return false; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** @see 7.1.5 */ |
|
48
|
|
|
if ($rpId === null && $publicKeyCredentialCreationOptions->getRp()->getId() === null) { |
|
49
|
|
|
return false; |
|
50
|
|
|
} |
|
51
|
|
|
$rpId = $rpId ?? $publicKeyCredentialCreationOptions->getRp()->getId(); |
|
52
|
|
|
if ($C->getOrigin() !== $rpId) { |
|
53
|
|
|
return false; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** @see 7.1.6 */ |
|
57
|
|
|
if ($C->getTokenBinding()) { |
|
58
|
|
|
throw new \InvalidArgumentException('Not supported'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** @see 7.1.7 */ |
|
62
|
|
|
$getClientDataJSONHash = hash('sha256', $publicKeyCredential->getResponse()->getClientDataJSON()->getRawData()); |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
/** @see 7.1.8 */ |
|
65
|
|
|
$attestationObject = $publicKeyCredential->getResponse()->getAttestationObject(); |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
/** @see 7.1.9 */ |
|
68
|
|
|
$rpIdHash = hash('sha256', $rpId); |
|
69
|
|
|
if (hash_equals($rpIdHash, $attestationObject->getAuthData()->getRpIdHash())) { |
|
70
|
|
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** @see 7.1.10 */ |
|
74
|
|
|
if (!$attestationObject->getAuthData()->isUserPresent()) { |
|
75
|
|
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** @see 7.1.11 */ |
|
79
|
|
|
if ($publicKeyCredentialCreationOptions->getAuthenticatorSelection()->getUserVerification() === AuthenticatorSelectionCriteria::USER_VERIFICATION_REQUIREMENT_REQUIRED && !$attestationObject->getAuthData()->isUserVerified()) { |
|
80
|
|
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** @see 7.1.12 */ |
|
84
|
|
|
if ($publicKeyCredentialCreationOptions->getExtensions()->count() !== 0) { |
|
85
|
|
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** @see 7.1.13 */ |
|
89
|
|
|
$fmt = $attestationObject->getAttStmt()->getFmt(); |
|
90
|
|
|
if (!$this->attestationStatementSupportManager->has($fmt)) { |
|
91
|
|
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** @see 7.1.14 */ |
|
95
|
|
|
$attestationStatementSupport = $this->attestationStatementSupportManager->get($fmt); |
|
96
|
|
|
if (!$attestationStatementSupport->isValid($attestationObject->getAttStmt(), $attestationObject->getAuthData(), $C)) { |
|
97
|
|
|
return false; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** @see 7.1.15 */ |
|
101
|
|
|
/** @see 7.1.16 */ |
|
102
|
|
|
/** @see 7.1.17 */ |
|
103
|
|
|
$credentialId = $attestationObject->getAuthData()->getAttestedCredentialData()->getCredentialId(); |
|
104
|
|
|
if ($this->credentialIdRepository->hasCredentialId($credentialId)) { |
|
105
|
|
|
return false; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** @see 7.1.18 */ |
|
109
|
|
|
/** @see 7.1.19 */ |
|
110
|
|
|
|
|
111
|
|
|
return true; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|