@@ -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 /** @var bool|null */
2846 private static $ useOpensslParse ;
2947
@@ -308,6 +326,116 @@ public static function isOpensslParseSafe()
308326 return self ::$ useOpensslParse = false ;
309327 }
310328
329+ /**
330+ * Looks for distrusted intermediates in a certificate chain presented by a server
331+ *
332+ * The system CA store may still trust the roots above them, so they need to be rejected by the client.
333+ *
334+ * @param array<mixed> $chain PEM strings, or X.509 certificates as captured by the peer_certificate_chain stream context option
335+ * @return string|null name of the first distrusted certificate found in the chain
336+ */
337+ public static function findDistrustedCertificate (array $ chain )
338+ {
339+ foreach ($ chain as $ cert ) {
340+ if (!is_string ($ cert )) {
341+ if (!function_exists ('openssl_x509_export ' ) || !openssl_x509_export ($ cert , $ pem )) {
342+ continue ;
343+ }
344+ $ cert = $ pem ;
345+ }
346+
347+ $ der = self ::pemToDer ($ cert );
348+ $ spki = null !== $ der ? self ::extractSpki ($ der ) : null ;
349+ if (null !== $ spki && isset (self ::$ distrustedSpkiSha256 [$ hash = hash ('sha256 ' , $ spki )])) {
350+ return self ::$ distrustedSpkiSha256 [$ hash ];
351+ }
352+ }
353+
354+ return null ;
355+ }
356+
357+ /**
358+ * @param string $pem
359+ * @return string|null
360+ */
361+ private static function pemToDer ($ pem )
362+ {
363+ $ start = strpos ($ pem , '-----BEGIN CERTIFICATE----- ' );
364+ $ end = strpos ($ pem , '-----END CERTIFICATE----- ' );
365+ if (false === $ start || false === $ end ) {
366+ return null ;
367+ }
368+ $ start += strlen ('-----BEGIN CERTIFICATE----- ' );
369+
370+ $ der = base64_decode (substr ($ pem , $ start , $ end - $ start ), true );
371+
372+ return false === $ der ? null : $ der ;
373+ }
374+
375+ /**
376+ * Walks the DER structure by hand as parsing the key with ext-openssl costs ~0.3ms per certificate
377+ *
378+ * Certificate ::= SEQUENCE { tbsCertificate SEQUENCE { [0] version OPTIONAL, serialNumber, signature, issuer, validity, subject, subjectPublicKeyInfo, ... }, ... }
379+ *
380+ * @param string $der
381+ * @return string|null
382+ */
383+ private static function extractSpki ($ der )
384+ {
385+ $ offset = 0 ;
386+ // enter Certificate and tbsCertificate
387+ if (null === self ::readDerHeader ($ der , $ offset ) || null === self ::readDerHeader ($ der , $ offset )) {
388+ return null ;
389+ }
390+
391+ $ fieldsToSkip = isset ($ der [$ offset ]) && ord ($ der [$ offset ]) === 0xA0 ? 6 : 5 ;
392+ for ($ i = 0 ; $ i < $ fieldsToSkip ; $ i ++) {
393+ $ length = self ::readDerHeader ($ der , $ offset );
394+ if (null === $ length ) {
395+ return null ;
396+ }
397+ $ offset += $ length ;
398+ }
399+
400+ $ start = $ offset ;
401+ $ length = self ::readDerHeader ($ der , $ offset );
402+ if (null === $ length ) {
403+ return null ;
404+ }
405+
406+ return substr ($ der , $ start , $ offset - $ start + $ length );
407+ }
408+
409+ /**
410+ * Reads the tag and length at $offset, moving $offset to the start of the contents
411+ *
412+ * @param string $der
413+ * @param int $offset
414+ * @return int|null length of the contents
415+ * @phpstan-impure
416+ */
417+ private static function readDerHeader ($ der , &$ offset )
418+ {
419+ if (!isset ($ der [$ offset + 1 ])) {
420+ return null ;
421+ }
422+
423+ $ length = ord ($ der [$ offset + 1 ]);
424+ $ offset += 2 ;
425+ if (($ length & 0x80 ) !== 0 ) {
426+ $ bytes = $ length & 0x7F ;
427+ if ($ bytes === 0 || $ bytes > 4 || !isset ($ der [$ offset + $ bytes - 1 ])) {
428+ return null ;
429+ }
430+ $ length = 0 ;
431+ for ($ i = 0 ; $ i < $ bytes ; $ i ++) {
432+ $ length = ($ length << 8 ) | ord ($ der [$ offset ++]);
433+ }
434+ }
435+
436+ return $ offset + $ length <= strlen ($ der ) ? $ length : null ;
437+ }
438+
311439 /**
312440 * Resets the static caches
313441 * @return void
0 commit comments