Skip to content

Commit 65bc967

Browse files
committed
Harden findDistrustedCertificate() input handling
- check every certificate in a multi-certificate PEM string - search the END marker after BEGIN - skip chain entries that are not PEM strings or X.509 certificates instead of warning - cap DER lengths at 3 bytes to avoid int overflow on 32-bit PHP - reject non-SEQUENCE structures and high-tag-number tags while walking the DER
1 parent 496c03b commit 65bc967

2 files changed

Lines changed: 58 additions & 18 deletions

File tree

‎src/CaBundle.php‎

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -226,39 +226,60 @@ public static function isOpensslParseSafe()
226226
public static function findDistrustedCertificate(array $chain)
227227
{
228228
foreach ($chain as $cert) {
229-
if (!is_string($cert)) {
229+
if (self::isX509Certificate($cert)) {
230230
if (!function_exists('openssl_x509_export') || !openssl_x509_export($cert, $pem)) {
231231
continue;
232232
}
233233
$cert = $pem;
234+
} elseif (!is_string($cert)) {
235+
continue;
234236
}
235237

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];
238+
foreach (self::pemToDer($cert) as $der) {
239+
$spki = self::extractSpki($der);
240+
if (null !== $spki && isset(self::$distrustedSpkiSha256[$hash = hash('sha256', $spki)])) {
241+
return self::$distrustedSpkiSha256[$hash];
242+
}
240243
}
241244
}
242245

243246
return null;
244247
}
245248

246249
/**
247-
* @param string $pem
248-
* @return string|null
250+
* Checks for an OpenSSLCertificate (PHP 8+) or OpenSSL X.509 resource (PHP < 8)
251+
*
252+
* @param mixed $cert
253+
* @return bool
254+
*/
255+
private static function isX509Certificate($cert)
256+
{
257+
return $cert instanceof \OpenSSLCertificate || (is_resource($cert) && get_resource_type($cert) === 'OpenSSL X.509');
258+
}
259+
260+
/**
261+
* @param string $pem one or more PEM encoded certificates
262+
* @return list<string>
249263
*/
250264
private static function pemToDer($pem)
251265
{
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-----');
266+
$ders = array();
267+
$offset = 0;
268+
while (false !== ($start = strpos($pem, '-----BEGIN CERTIFICATE-----', $offset))) {
269+
$start += strlen('-----BEGIN CERTIFICATE-----');
270+
$end = strpos($pem, '-----END CERTIFICATE-----', $start);
271+
if (false === $end) {
272+
break;
273+
}
274+
$offset = $end + strlen('-----END CERTIFICATE-----');
258275

259-
$der = base64_decode(substr($pem, $start, $end - $start), true);
276+
$der = base64_decode(substr($pem, $start, $end - $start), true);
277+
if (false !== $der) {
278+
$ders[] = $der;
279+
}
280+
}
260281

261-
return false === $der ? null : $der;
282+
return $ders;
262283
}
263284

264285
/**
@@ -274,7 +295,7 @@ private static function extractSpki($der)
274295
$offset = 0;
275296
// enter Certificate and tbsCertificate
276297
for ($i = 0; $i < 2; $i++) {
277-
if (null === self::readDerHeader($der, $offset)) {
298+
if (!self::isSequenceAt($der, $offset) || null === self::readDerHeader($der, $offset)) {
278299
return null;
279300
}
280301
}
@@ -289,6 +310,9 @@ private static function extractSpki($der)
289310
}
290311

291312
$start = $offset;
313+
if (!self::isSequenceAt($der, $offset)) {
314+
return null;
315+
}
292316
$length = self::readDerHeader($der, $offset);
293317
if (null === $length) {
294318
return null;
@@ -297,6 +321,16 @@ private static function extractSpki($der)
297321
return substr($der, $start, $offset - $start + $length);
298322
}
299323

324+
/**
325+
* @param string $der
326+
* @param int $offset
327+
* @return bool
328+
*/
329+
private static function isSequenceAt($der, $offset)
330+
{
331+
return isset($der[$offset]) && ord($der[$offset]) === 0x30;
332+
}
333+
300334
/**
301335
* Reads the tag and length at $offset, moving $offset to the start of the contents
302336
*
@@ -307,15 +341,17 @@ private static function extractSpki($der)
307341
*/
308342
private static function readDerHeader($der, &$offset)
309343
{
310-
if (!isset($der[$offset + 1])) {
344+
// high tag numbers (multi-byte tags) do not occur in the fields we walk
345+
if (!isset($der[$offset + 1]) || (ord($der[$offset]) & 0x1F) === 0x1F) {
311346
return null;
312347
}
313348

314349
$length = ord($der[$offset + 1]);
315350
$offset += 2;
316351
if (($length & 0x80) !== 0) {
352+
// max 3 length bytes (16MB) so the length cannot overflow on 32-bit PHP
317353
$bytes = $length & 0x7F;
318-
if ($bytes === 0 || $bytes > 4 || !isset($der[$offset + $bytes - 1])) {
354+
if ($bytes === 0 || $bytes > 3 || !isset($der[$offset + $bytes - 1])) {
319355
return null;
320356
}
321357
$length = 0;

‎tests/CaBundleTest.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ public function testFindDistrustedCertificate(): void
2121
{
2222
$ev = (string) file_get_contents(__DIR__.'/Fixtures/distrusted/em-ev-tls-ca-g3b.pem');
2323
$ov = (string) file_get_contents(__DIR__.'/Fixtures/distrusted/em-ov-tls-ca-g3b.pem');
24+
preg_match('{-----BEGIN CERTIFICATE-----.+?-----END CERTIFICATE-----}s', (string) file_get_contents(CaBundle::getBundledCaBundlePath()), $trusted);
2425

2526
$this->assertSame('EM EV TLS CA - G3B', CaBundle::findDistrustedCertificate(array($ev)));
2627
$this->assertSame('EM OV TLS CA - G3B', CaBundle::findDistrustedCertificate(array('garbage', $ov, $ev)));
2728
$this->assertSame('EM EV TLS CA - G3B', CaBundle::findDistrustedCertificate(array(openssl_x509_read($ev))));
29+
$this->assertSame('EM OV TLS CA - G3B', CaBundle::findDistrustedCertificate(array($trusted[0] . "\n" . $ov)));
30+
$this->assertSame('EM OV TLS CA - G3B', CaBundle::findDistrustedCertificate(array("x\n-----END CERTIFICATE-----\n" . $ov)));
31+
$this->assertSame('EM OV TLS CA - G3B', CaBundle::findDistrustedCertificate(array(123, null, array(), new \stdClass(), $ov)));
2832
}
2933

3034
public function testFindDistrustedCertificateIgnoresTrustedCertificates(): void

0 commit comments

Comments
 (0)