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 Assert\Assertion; |
17
|
|
|
use U2FAuthentication\CertificateToolbox; |
18
|
|
|
use U2FAuthentication\Fido2\AuthenticatorData; |
19
|
|
|
|
20
|
|
|
final class PackedAttestationStatementSupport implements AttestationStatementSupport |
21
|
|
|
{ |
22
|
|
|
public function name(): string |
23
|
|
|
{ |
24
|
|
|
return 'packed'; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function isValid(string $clientDataJSONHash, AttestationStatement $attestationStatement, AuthenticatorData $authenticatorData): bool |
28
|
|
|
{ |
29
|
|
|
Assertion::true($attestationStatement->has('sig'), 'The attestation statement value "sig" is missing.'); |
30
|
|
|
|
31
|
|
|
switch (true) { |
32
|
|
|
case $attestationStatement->has('x5c'): |
33
|
|
|
return $this->processWithCertificate($clientDataJSONHash, $attestationStatement, $authenticatorData); |
34
|
|
|
case $attestationStatement->has('ecdaaKeyId'): |
35
|
|
|
return $this->processWithECDAA(); |
36
|
|
|
default: |
37
|
|
|
return $this->processWithSelfAttestation(); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
private function processWithCertificate(string $clientDataJSONHash, AttestationStatement $attestationStatement, AuthenticatorData $authenticatorData): bool |
42
|
|
|
{ |
43
|
|
|
$certificates = $attestationStatement->get('x5c'); |
44
|
|
|
Assertion::isArray($certificates, 'The attestation statement value "x5c" must be a list with at least one certificate.'); |
45
|
|
|
|
46
|
|
|
//Check certificate CA chain and returns the Attestation Certificate |
47
|
|
|
$attestnCert = CertificateToolbox::checkChain($certificates); |
48
|
|
|
|
49
|
|
|
$signedData = $authenticatorData->getAuthData().$clientDataJSONHash; |
50
|
|
|
$result = openssl_verify($signedData, $attestationStatement->get('sig'), $attestnCert, OPENSSL_ALGO_SHA256); |
51
|
|
|
if (1 !== $result) { |
52
|
|
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->checkCertificate($attestnCert, $authenticatorData); |
56
|
|
|
|
57
|
|
|
return true; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function checkCertificate(string $attestnCert, AuthenticatorData $authenticatorData): void |
61
|
|
|
{ |
62
|
|
|
$parsed = openssl_x509_parse($attestnCert); |
63
|
|
|
|
64
|
|
|
//Check version |
65
|
|
|
Assertion::false(!isset($parsed['version']) || 2 !== $parsed['version'], 'Invalid certificate version'); |
66
|
|
|
|
67
|
|
|
//Check subject field |
68
|
|
|
Assertion::false(!isset($parsed['name']) || false === mb_strpos($parsed['name'], '/OU=Authenticator Attestation'), 'Invalid certificate name. The Subject Organization Unit must be "Authenticator Attestation"'); |
69
|
|
|
|
70
|
|
|
//Check extensions |
71
|
|
|
Assertion::false(!isset($parsed['extensions']) || !\is_array($parsed['extensions']), 'Certificate extensions are missing'); |
72
|
|
|
|
73
|
|
|
//Check certificate is not a CA cert |
74
|
|
|
Assertion::false(!isset($parsed['extensions']['basicConstraints']) || 'CA:FALSE' !== $parsed['extensions']['basicConstraints'], 'The Basic Constraints extension must have the CA component set to false'); |
75
|
|
|
|
76
|
|
|
// id-fido-gen-ce-aaguid OID check |
77
|
|
|
Assertion::false(\in_array('1.3.6.1.4.1.45724.1.1.4', $parsed['extensions'], true) && !hash_equals($authenticatorData->getAttestedCredentialData()->getAaguid(), $parsed['extensions']['1.3.6.1.4.1.45724.1.1.4']), 'The value of the "aaguid" does not match with the certificate'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function processWithECDAA(): bool |
81
|
|
|
{ |
82
|
|
|
throw new \RuntimeException('ECDAA not supported'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function processWithSelfAttestation(): bool |
86
|
|
|
{ |
87
|
|
|
throw new \RuntimeException('Self attestation not supported'); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|