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; |
15
|
|
|
|
16
|
|
|
use Assert\Assertion; |
17
|
|
|
|
18
|
|
|
class CertificateChainChecker |
19
|
|
|
{ |
20
|
|
|
public static function check(array $x5c): string |
21
|
|
|
{ |
22
|
|
|
Assertion::notEmpty($x5c, 'The attestation statement value "x5c" must be a list with at least one certificate.'); |
23
|
|
|
reset($x5c); |
24
|
|
|
$currentCert = self::getX509Certificate(current($x5c)); |
25
|
|
|
if (1 === \count($x5c)) { |
26
|
|
|
return $currentCert; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$firstCert = $currentCert; |
30
|
|
|
$currentCertParsed = openssl_x509_parse($currentCert); |
31
|
|
|
$purpose = X509_PURPOSE_ANY; |
32
|
|
|
$file = \Safe\tmpfile(); |
33
|
|
|
$path = stream_get_meta_data($file)['uri']; |
34
|
|
|
while ($nextCert = next($x5c)) { |
35
|
|
|
$currentCertIssuer = \Safe\json_encode($currentCertParsed['issuer']); |
36
|
|
|
|
37
|
|
|
$nextCertAsPem = self::getX509Certificate($nextCert); |
38
|
|
|
$nextCertParsed = openssl_x509_parse($nextCertAsPem); |
39
|
|
|
$nextCertAsPemSubject = \Safe\json_encode($nextCertParsed['subject']); |
40
|
|
|
|
41
|
|
|
Assertion::eq($currentCertIssuer, $nextCertAsPemSubject, 'Invalid certificate chain.'); |
42
|
|
|
|
43
|
|
|
\Safe\rewind($file); |
44
|
|
|
\Safe\fwrite($file, $nextCertAsPem); |
45
|
|
|
$result = openssl_x509_checkpurpose($currentCert, $purpose, [$path]); |
46
|
|
|
Assertion::eq(1, $result, 'Invalid certificate chain.'); |
47
|
|
|
|
48
|
|
|
Assertion::keyExists($nextCertParsed, 'extensions', 'Invalid certificate chain.'); |
49
|
|
|
Assertion::keyExists($nextCertParsed['extensions'], 'basicConstraints', 'Invalid certificate chain.'); |
50
|
|
|
Assertion::startsWith($nextCertParsed['extensions']['basicConstraints'], 'CA:TRUE', 'Invalid certificate chain.'); |
51
|
|
|
$purpose = X509_PURPOSE_CRL_SIGN; |
52
|
|
|
$currentCert = $nextCertAsPem; |
53
|
|
|
$currentCertParsed = $nextCertParsed; |
54
|
|
|
} |
55
|
|
|
\Safe\fclose($file); |
56
|
|
|
|
57
|
|
|
//We check the last certificate is a root certificate |
58
|
|
|
$currentCertIssuer = \Safe\json_encode($currentCertParsed['issuer']); |
59
|
|
|
$currentCertSubject = \Safe\json_encode($currentCertParsed['subject']); |
60
|
|
|
Assertion::eq($currentCertIssuer, $currentCertSubject, 'Invalid certificate chain.'); |
61
|
|
|
|
62
|
|
|
// We check that the last certificate is a CA certificate |
63
|
|
|
Assertion::keyExists($currentCertParsed, 'extensions', 'Invalid certificate chain.'); |
64
|
|
|
Assertion::keyExists($currentCertParsed['extensions'], 'basicConstraints', 'Invalid certificate chain.'); |
65
|
|
|
Assertion::eq($currentCertParsed['extensions']['basicConstraints'], 'CA:TRUE', 'Invalid certificate chain.'); |
66
|
|
|
|
67
|
|
|
return $firstCert; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private static function getX509Certificate(string $publicKey): string |
71
|
|
|
{ |
72
|
|
|
$derCertificate = self::unusedBytesFix($publicKey); |
73
|
|
|
$pemCert = '-----BEGIN CERTIFICATE-----'.PHP_EOL; |
74
|
|
|
$pemCert .= chunk_split(base64_encode($derCertificate), 64, PHP_EOL); |
75
|
|
|
$pemCert .= '-----END CERTIFICATE-----'.PHP_EOL; |
76
|
|
|
|
77
|
|
|
return $pemCert; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private static function unusedBytesFix(string $derCertificate): string |
81
|
|
|
{ |
82
|
|
|
$certificateHash = hash('sha256', $derCertificate); |
83
|
|
|
if (\in_array($certificateHash, self::getCertificateHashes(), true)) { |
84
|
|
|
$derCertificate[mb_strlen($derCertificate, '8bit') - 257] = "\0"; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $derCertificate; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string[] |
92
|
|
|
*/ |
93
|
|
|
private static function getCertificateHashes(): array |
94
|
|
|
{ |
95
|
|
|
return [ |
96
|
|
|
'349bca1031f8c82c4ceca38b9cebf1a69df9fb3b94eed99eb3fb9aa3822d26e8', |
97
|
|
|
'dd574527df608e47ae45fbba75a2afdd5c20fd94a02419381813cd55a2a3398f', |
98
|
|
|
'1d8764f0f7cd1352df6150045c8f638e517270e8b5dda1c63ade9c2280240cae', |
99
|
|
|
'd0edc9a91a1677435a953390865d208c55b3183c6759c9b5a7ff494c322558eb', |
100
|
|
|
'6073c436dcd064a48127ddbf6032ac1a66fd59a0c24434f070d4e564c124c897', |
101
|
|
|
'ca993121846c464d666096d35f13bf44c1b05af205f9b4a1e00cf6cc10c5e511', |
102
|
|
|
]; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|