|
| 1 | +package io.sentry.transport; |
| 2 | + |
| 3 | +import java.security.cert.Certificate; |
| 4 | +import java.security.cert.X509Certificate; |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.HashSet; |
| 7 | +import java.util.Set; |
| 8 | +import javax.net.ssl.SSLPeerUnverifiedException; |
| 9 | +import javax.net.ssl.SSLSocketFactory; |
| 10 | +import javax.security.auth.x500.X500Principal; |
| 11 | +import org.jetbrains.annotations.ApiStatus; |
| 12 | +import org.jetbrains.annotations.NotNull; |
| 13 | + |
| 14 | +/** |
| 15 | + * Rejects servers whose certificate was issued by a CA that is known to be compromised but still |
| 16 | + * trusted by platform trust stores. |
| 17 | + * |
| 18 | + * <p>Browsers block such certificates on their own and require Certificate Transparency, but {@link |
| 19 | + * javax.net.ssl.HttpsURLConnection} does neither. Android only updates its CA store with OS updates |
| 20 | + * (or Mainline on Android 14+) and the JVM only with JDK updates, so a distrust by the root |
| 21 | + * programs doesn't reach every device. |
| 22 | + */ |
| 23 | +@ApiStatus.Internal |
| 24 | +public final class DistrustedCertificates { |
| 25 | + |
| 26 | + /** |
| 27 | + * The eight eMudhra intermediates whose private keys can be recovered from their certificates |
| 28 | + * (CVE-2026-111943). Each key is in two certificates with the same subject: one issued by eMudhra |
| 29 | + * and a cross-sign by Sectigo's AAA Certificate Services root, which is trusted on every Android |
| 30 | + * version. |
| 31 | + * |
| 32 | + * <p>They are matched by name rather than by key because the server doesn't have to send them: |
| 33 | + * Android caches intermediates from earlier connections and completes a chain that is missing |
| 34 | + * one. The server always sends the leaf, which names its issuer, and since none of these CAs may |
| 35 | + * issue further CAs ({@code pathlen:0}), any certificate they issued is a leaf. |
| 36 | + * |
| 37 | + * @see <a |
| 38 | + * href="https://groups.google.com/a/mozilla.org/g/dev-security-policy/c/0R3XLJL6FuE">Compromised |
| 39 | + * eMudhra Intermediate Certificates</a> |
| 40 | + */ |
| 41 | + private static final Set<X500Principal> DISTRUSTED_CAS = |
| 42 | + new HashSet<>( |
| 43 | + Arrays.asList( |
| 44 | + new X500Principal("CN=EM DV TLS CA - G3B, O=eMudhra BV, C=NL"), |
| 45 | + new X500Principal("CN=EM EV TLS CA - G3B, O=eMudhra BV, C=NL"), |
| 46 | + new X500Principal("CN=EM OV TLS CA - G3B, O=eMudhra BV, C=NL"), |
| 47 | + new X500Principal("CN=EM OV CS CA - G3B, O=eMudhra BV, C=NL"), |
| 48 | + new X500Principal("CN=EM DV TLS CA - G3C, O=eMudhra Inc, C=US"), |
| 49 | + new X500Principal("CN=EM EV TLS CA - G3C, O=eMudhra Inc, C=US"), |
| 50 | + new X500Principal("CN=EM OV TLS CA - G3C, O=eMudhra Inc, C=US"), |
| 51 | + new X500Principal("CN=EM OV CS CA - G3C, O=eMudhra Inc, C=US"))); |
| 52 | + |
| 53 | + private DistrustedCertificates() {} |
| 54 | + |
| 55 | + /** |
| 56 | + * Wraps {@code delegate} so that every TLS handshake of its sockets is checked, including the |
| 57 | + * ones {@link java.net.HttpURLConnection} makes on its own to retry a request on a new |
| 58 | + * connection. |
| 59 | + */ |
| 60 | + public static @NotNull SSLSocketFactory wrap(final @NotNull SSLSocketFactory delegate) { |
| 61 | + return new DistrustedCertificatesSocketFactory(delegate); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @param peerCertificates the certificates the server sent, leaf first |
| 66 | + */ |
| 67 | + static void check(final @NotNull Certificate[] peerCertificates) |
| 68 | + throws SSLPeerUnverifiedException { |
| 69 | + for (final Certificate certificate : peerCertificates) { |
| 70 | + if (certificate instanceof X509Certificate && isDistrusted((X509Certificate) certificate)) { |
| 71 | + throw new SSLPeerUnverifiedException( |
| 72 | + "The server's certificate chain contains a distrusted certificate."); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private static boolean isDistrusted(final @NotNull X509Certificate certificate) { |
| 78 | + return DISTRUSTED_CAS.contains(certificate.getIssuerX500Principal()) |
| 79 | + || DISTRUSTED_CAS.contains(certificate.getSubjectX500Principal()); |
| 80 | + } |
| 81 | +} |
0 commit comments