@@ -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
0 commit comments