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\AttestationStatement; |
15
|
|
|
|
16
|
|
|
use CBOR\MapObject; |
17
|
|
|
use U2FAuthentication\Fido2\AuthenticatorData; |
18
|
|
|
|
19
|
|
|
final class FidoU2FAttestationStatementSupport implements AttestationStatementSupport |
20
|
|
|
{ |
21
|
|
|
private const CERTIFICATES_HASHES = [ |
22
|
|
|
'349bca1031f8c82c4ceca38b9cebf1a69df9fb3b94eed99eb3fb9aa3822d26e8', |
23
|
|
|
'dd574527df608e47ae45fbba75a2afdd5c20fd94a02419381813cd55a2a3398f', |
24
|
|
|
'1d8764f0f7cd1352df6150045c8f638e517270e8b5dda1c63ade9c2280240cae', |
25
|
|
|
'd0edc9a91a1677435a953390865d208c55b3183c6759c9b5a7ff494c322558eb', |
26
|
|
|
'6073c436dcd064a48127ddbf6032ac1a66fd59a0c24434f070d4e564c124c897', |
27
|
|
|
'ca993121846c464d666096d35f13bf44c1b05af205f9b4a1e00cf6cc10c5e511', |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string[] |
32
|
|
|
*/ |
33
|
|
|
private $attestationCertificates = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string[] $attestationCertificates |
37
|
|
|
*/ |
38
|
|
|
public function __construct(array $attestationCertificates = []) |
39
|
|
|
{ |
40
|
|
|
$this->attestationCertificates = $attestationCertificates; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function name(): string |
44
|
|
|
{ |
45
|
|
|
return 'fido-u2f'; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function isValid(string $clientDataJSONHash, AttestationStatement $attestationStatement, AuthenticatorData $authenticatorData): bool |
49
|
|
|
{ |
50
|
|
|
foreach (['sig', 'x5c'] as $key) { |
51
|
|
|
if (!$attestationStatement->has($key)) { |
52
|
|
|
throw new \InvalidArgumentException(\Safe\sprintf('The attestation statement value "%s" is missing.', $key)); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
$certificates = $attestationStatement->get('x5c'); |
56
|
|
|
if (!\is_array($certificates) || 1 !== \count($certificates)) { |
57
|
|
|
throw new \InvalidArgumentException('The attestation statement value "x5c" must be a list with one certificate.'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
reset($certificates); |
61
|
|
|
$certificate = $this->getCertificateAsPem(current($certificates)); |
62
|
|
|
|
63
|
|
|
if (!empty($this->attestationCertificates) && 1 !== openssl_x509_checkpurpose($certificate, X509_PURPOSE_ANY, $this->attestationCertificates)) { |
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$dataToVerify = "\0"; |
68
|
|
|
$dataToVerify .= $authenticatorData->getRpIdHash(); |
69
|
|
|
$dataToVerify .= $clientDataJSONHash; |
70
|
|
|
$dataToVerify .= $authenticatorData->getAttestedCredentialData()->getCredentialId(); |
71
|
|
|
$dataToVerify .= $this->extractPublicKey($authenticatorData->getAttestedCredentialData()->getCredentialPublicKey()); |
72
|
|
|
|
73
|
|
|
return 1 === openssl_verify($dataToVerify, $attestationStatement->get('sig'), $certificate, OPENSSL_ALGO_SHA256); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function getCertificateAsPem(string $publicKey): string |
77
|
|
|
{ |
78
|
|
|
$derCertificate = $this->unusedBytesFix($publicKey); |
79
|
|
|
|
80
|
|
|
$pemCert = '-----BEGIN CERTIFICATE-----'.PHP_EOL; |
81
|
|
|
$pemCert .= chunk_split(base64_encode($derCertificate), 64, PHP_EOL); |
82
|
|
|
$pemCert .= '-----END CERTIFICATE-----'.PHP_EOL; |
83
|
|
|
|
84
|
|
|
return $pemCert; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function extractPublicKey(?MapObject $publicKey): string |
88
|
|
|
{ |
89
|
|
|
if (!$publicKey instanceof MapObject) { |
90
|
|
|
throw new \InvalidArgumentException('The public key of the attestation statement is not valid.'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$publicKey = $publicKey->getNormalizedData(); |
94
|
|
|
if (!array_key_exists(-2, $publicKey) || !\is_string($publicKey[-2]) || 32 !== mb_strlen($publicKey[-2], '8bit')) { |
95
|
|
|
throw new \InvalidArgumentException('The public key of the attestation statement is not valid.'); |
96
|
|
|
} |
97
|
|
|
if (!array_key_exists(-3, $publicKey) || !\is_string($publicKey[-3]) || 32 !== mb_strlen($publicKey[-3], '8bit')) { |
98
|
|
|
throw new \InvalidArgumentException('The public key of the attestation statement is not valid.'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return "\x04".$publicKey[-2].$publicKey[-3]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private function unusedBytesFix(string $derCertificate): string |
105
|
|
|
{ |
106
|
|
|
$certificateHash = hash('sha256', $derCertificate); |
107
|
|
|
if (\in_array($certificateHash, self::CERTIFICATES_HASHES, true)) { |
108
|
|
|
$derCertificate[mb_strlen($derCertificate, '8bit') - 257] = "\0"; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $derCertificate; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|