Skip to content

Commit 022932f

Browse files
committed
Add CaBundle::findDistrustedCertificate() to detect compromised intermediates in a presented chain
1 parent e096683 commit 022932f

4 files changed

Lines changed: 189 additions & 0 deletions

File tree

‎src/CaBundle.php‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ class CaBundle
2424
private static $caPath;
2525
/** @var array<string, bool> */
2626
private static $caFileValidity = array();
27+
/**
28+
* SHA-256 of the DER encoded SubjectPublicKeyInfo of distrusted (e.g. compromised) intermediates
29+
*
30+
* Matching on the key also catches cross-signed copies of the same intermediate.
31+
*
32+
* @var array<string, string>
33+
*/
34+
private static $distrustedSpkiSha256 = array(
35+
// eMudhra intermediates with compromised keys, revoked 2026-10-18 (cross-signed by Sectigo's AAA Certificate Services)
36+
'e0254c7fcdc265e2f0f8a428dabb75ab4ae039b4872d62f5ee57f0877d5a8a41' => 'EM EV TLS CA - G3B',
37+
'43bffa19d6ef651451f6841e019931e96f6f2eaeff28ab4b6f1ce4576de2717b' => 'EM OV TLS CA - G3B',
38+
'ebed6475c2c2d80fcb199f3fe8dd6e6b3ce60b77eb15d4e42c3dec0c6f5b5105' => 'EM DV TLS CA - G3B',
39+
'5c0660a2b164910d8ff589c18e14552e9e0ec237dcefe2cb736d41a4811f81a0' => 'EM EV TLS CA - G3C',
40+
'63ecefee431d49523bc1136c4e72d0206a477086caf606cd5cf025686c109cf8' => 'EM OV TLS CA - G3C',
41+
'49dd353dc2b919a3770645117d0e60c16115e7d1bc259eefe734b00ea434fe5f' => 'EM DV TLS CA - G3C',
42+
'ec4ffa1ad0ac88081314b7aaffef6a91f97cd57291bb5ba686c5415f5eb4f6b6' => 'EM OV CS CA - G3B',
43+
'92c063d67e8f14db71d288e11266ffdaf69cbf115f7bab14f3bc2f68d25e0e30' => 'EM OV CS CA - G3C',
44+
);
2745

2846
/**
2947
* Returns the system CA bundle path, or a path to the bundled one
@@ -197,6 +215,116 @@ public static function isOpensslParseSafe()
197215
return true;
198216
}
199217

218+
/**
219+
* Looks for distrusted intermediates in a certificate chain presented by a server
220+
*
221+
* The system CA store may still trust the roots above them, so they need to be rejected by the client.
222+
*
223+
* @param array<mixed> $chain PEM strings, or X.509 certificates as captured by the peer_certificate_chain stream context option
224+
* @return string|null name of the first distrusted certificate found in the chain
225+
*/
226+
public static function findDistrustedCertificate(array $chain)
227+
{
228+
foreach ($chain as $cert) {
229+
if (!is_string($cert)) {
230+
if (!function_exists('openssl_x509_export') || !openssl_x509_export($cert, $pem)) {
231+
continue;
232+
}
233+
$cert = $pem;
234+
}
235+
236+
$der = self::pemToDer($cert);
237+
$spki = null !== $der ? self::extractSpki($der) : null;
238+
if (null !== $spki && isset(self::$distrustedSpkiSha256[$hash = hash('sha256', $spki)])) {
239+
return self::$distrustedSpkiSha256[$hash];
240+
}
241+
}
242+
243+
return null;
244+
}
245+
246+
/**
247+
* @param string $pem
248+
* @return string|null
249+
*/
250+
private static function pemToDer($pem)
251+
{
252+
$start = strpos($pem, '-----BEGIN CERTIFICATE-----');
253+
$end = strpos($pem, '-----END CERTIFICATE-----');
254+
if (false === $start || false === $end) {
255+
return null;
256+
}
257+
$start += strlen('-----BEGIN CERTIFICATE-----');
258+
259+
$der = base64_decode(substr($pem, $start, $end - $start), true);
260+
261+
return false === $der ? null : $der;
262+
}
263+
264+
/**
265+
* Walks the DER structure by hand as parsing the key with ext-openssl costs ~0.3ms per certificate
266+
*
267+
* Certificate ::= SEQUENCE { tbsCertificate SEQUENCE { [0] version OPTIONAL, serialNumber, signature, issuer, validity, subject, subjectPublicKeyInfo, ... }, ... }
268+
*
269+
* @param string $der
270+
* @return string|null
271+
*/
272+
private static function extractSpki($der)
273+
{
274+
$offset = 0;
275+
// enter Certificate and tbsCertificate
276+
if (null === self::readDerHeader($der, $offset) || null === self::readDerHeader($der, $offset)) {
277+
return null;
278+
}
279+
280+
$fieldsToSkip = isset($der[$offset]) && ord($der[$offset]) === 0xA0 ? 6 : 5;
281+
for ($i = 0; $i < $fieldsToSkip; $i++) {
282+
$length = self::readDerHeader($der, $offset);
283+
if (null === $length) {
284+
return null;
285+
}
286+
$offset += $length;
287+
}
288+
289+
$start = $offset;
290+
$length = self::readDerHeader($der, $offset);
291+
if (null === $length) {
292+
return null;
293+
}
294+
295+
return substr($der, $start, $offset - $start + $length);
296+
}
297+
298+
/**
299+
* Reads the tag and length at $offset, moving $offset to the start of the contents
300+
*
301+
* @param string $der
302+
* @param int $offset
303+
* @return int|null length of the contents
304+
* @phpstan-impure
305+
*/
306+
private static function readDerHeader($der, &$offset)
307+
{
308+
if (!isset($der[$offset + 1])) {
309+
return null;
310+
}
311+
312+
$length = ord($der[$offset + 1]);
313+
$offset += 2;
314+
if (($length & 0x80) !== 0) {
315+
$bytes = $length & 0x7F;
316+
if ($bytes === 0 || $bytes > 4 || !isset($der[$offset + $bytes - 1])) {
317+
return null;
318+
}
319+
$length = 0;
320+
for ($i = 0; $i < $bytes; $i++) {
321+
$length = ($length << 8) | ord($der[$offset++]);
322+
}
323+
}
324+
325+
return $offset + $length <= strlen($der) ? $length : null;
326+
}
327+
200328
/**
201329
* Resets the static caches
202330
* @return void

‎tests/CaBundleTest.php‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,25 @@ public function testCaPath(): void
1717
$this->assertFileExists($caPath);
1818
}
1919

20+
public function testFindDistrustedCertificate(): void
21+
{
22+
$ev = (string) file_get_contents(__DIR__.'/Fixtures/distrusted/em-ev-tls-ca-g3b.pem');
23+
$ov = (string) file_get_contents(__DIR__.'/Fixtures/distrusted/em-ov-tls-ca-g3b.pem');
24+
25+
$this->assertSame('EM EV TLS CA - G3B', CaBundle::findDistrustedCertificate(array($ev)));
26+
$this->assertSame('EM OV TLS CA - G3B', CaBundle::findDistrustedCertificate(array('garbage', $ov, $ev)));
27+
$this->assertSame('EM EV TLS CA - G3B', CaBundle::findDistrustedCertificate(array(openssl_x509_read($ev))));
28+
}
29+
30+
public function testFindDistrustedCertificateIgnoresTrustedCertificates(): void
31+
{
32+
preg_match_all('{-----BEGIN CERTIFICATE-----.+?-----END CERTIFICATE-----}s', (string) file_get_contents(CaBundle::getBundledCaBundlePath()), $match);
33+
34+
$this->assertGreaterThan(100, count($match[0]));
35+
$this->assertNull(CaBundle::findDistrustedCertificate($match[0]));
36+
$this->assertNull(CaBundle::findDistrustedCertificate(array()));
37+
}
38+
2039
public function testCaPathNotNull(): void
2140
{
2241
$caBundle = new CaBundle();
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIDaDCCAu2gAwIBAgIQAMfEHv6S2CXwXq6EGkqCwTAKBggqhkjOPQQDAzBWMQsw
3+
CQYDVQQGEwJJTjElMCMGA1UECgwcZU11ZGhyYSBUZWNobm9sb2dpZXMgTGltaXRl
4+
ZDEgMB4GA1UEAwwXZW1TaWduIFJvb3QgVExTIENBIC0gRzMwHhcNMjUwNDMwMTYw
5+
MzMxWhcNNDAwNDI4MTgzMDAwWjA/MQswCQYDVQQGEwJOTDETMBEGA1UECgwKZU11
6+
ZGhyYSBCVjEbMBkGA1UEAwwSRU0gRVYgVExTIENBIC0gRzNCMHYwEAYHKoZIzj0C
7+
AQYFK4EEACIDYgAEJduoWmTTl9oLEFLp6lRuSHvv6pydnWigxerc/u91ppu11vwB
8+
9Cv7ym60C81kwHZ+Nv/QXZeoE8WqDGyVMOec9fCGZGjxOAZCCqXIUYQLHiUlQOeQ
9+
5LZUw4Uu1krx6GV9o4IBlTCCAZEwHwYDVR0jBBgwFoAU4TqrIXzx401UrtRyt/2L
10+
WUIgkFQwHQYDVR0OBBYEFD7cINmJRpiHe4c/2IFHslwV2EcGMB0GA1UdJQQWMBQG
11+
CCsGAQUFBwMBBggrBgEFBQcDAjBcBgNVHSAEVTBTMDkGCysGAQQBg44hAQABMCow
12+
KAYIKwYBBQUHAgEWHGh0dHA6Ly9yZXBvc2l0b3J5LmVtc2lnbi5jb20wDQYLKwYB
13+
BAGDjiEBAngwBwYFZ4EMAQEwdQYIKwYBBQUHAQEEaTBnMCQGCCsGAQUFBzABhhho
14+
dHRwOi8vb2NzcC1iLmVtc2lnbi5jb20wPwYIKwYBBQUHMAKGM2h0dHA6Ly9yZXBv
15+
c2l0b3J5LmVtc2lnbi5jb20vY2VydHMvRU1FVlRMU0NBRzNCLnA3YzA3BgNVHR8E
16+
MDAuMCygKqAohiZodHRwOi8vY3JsLmVtc2lnbi5jb20vP3Jvb3R0bHNjYWczLmNy
17+
bDAOBgNVHQ8BAf8EBAMCAQYwEgYDVR0TAQH/BAgwBgEB/wIBADAKBggqhkjOPQQD
18+
AwNpADBmAjEA9Jq999oaOUKhFwopw33iCmt0OHIn3gjT43GfuOHR/YckXdwoNdDA
19+
lIEhz0NgRO3/AjEAvY/a3OMvktIZLYjn+WLKmp0QhrIMV+e6EpS7j4GVeOKouNp6
20+
MN/P+ByGeiRMghzB
21+
-----END CERTIFICATE-----
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIDZjCCAu2gAwIBAgIPbK2jZ/scEACIEg6iAtVoMAoGCCqGSM49BAMDMFYxCzAJ
3+
BgNVBAYTAklOMSUwIwYDVQQKDBxlTXVkaHJhIFRlY2hub2xvZ2llcyBMaW1pdGVk
4+
MSAwHgYDVQQDDBdlbVNpZ24gUm9vdCBUTFMgQ0EgLSBHMzAeFw0yNTA0MzAxNjAw
5+
MTJaFw00MDA0MjgxODMwMDBaMD8xCzAJBgNVBAYTAk5MMRMwEQYDVQQKDAplTXVk
6+
aHJhIEJWMRswGQYDVQQDDBJFTSBPViBUTFMgQ0EgLSBHM0IwdjAQBgcqhkjOPQIB
7+
BgUrgQQAIgNiAARziXqg6zz9yosSQC/68KvE4SErr43ud4rJ7i1tuD+sKAxeBWOK
8+
56Ks1xwyKo42HN659pxDArndjXPRzGyqsswK7k4hbOl53VG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