|
| 1 | +package io.sentry.transport; |
| 2 | + |
| 3 | +import java.net.HttpURLConnection; |
| 4 | +import java.security.MessageDigest; |
| 5 | +import java.security.NoSuchAlgorithmException; |
| 6 | +import java.security.cert.Certificate; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.HashSet; |
| 9 | +import java.util.Set; |
| 10 | +import javax.net.ssl.HttpsURLConnection; |
| 11 | +import javax.net.ssl.SSLPeerUnverifiedException; |
| 12 | +import org.jetbrains.annotations.ApiStatus; |
| 13 | +import org.jetbrains.annotations.NotNull; |
| 14 | +import org.jetbrains.annotations.Nullable; |
| 15 | + |
| 16 | +/** |
| 17 | + * Rejects servers whose certificate chain contains a CA certificate that is known to be compromised |
| 18 | + * but still trusted by platform trust stores. |
| 19 | + * |
| 20 | + * <p>Browsers block such certificates on their own and require Certificate Transparency, but {@link |
| 21 | + * HttpsURLConnection} does neither. Android only updates its CA store with OS updates (or Mainline |
| 22 | + * on Android 14+) and the JVM only with JDK updates, so a distrust by the root programs doesn't |
| 23 | + * reach every device. |
| 24 | + */ |
| 25 | +@ApiStatus.Internal |
| 26 | +public final class DistrustedCertificates { |
| 27 | + |
| 28 | + /** |
| 29 | + * SHA-256 hashes of the SubjectPublicKeyInfo of the eight eMudhra intermediates whose private |
| 30 | + * keys can be recovered from their certificates (CVE-2026-111943). Each key is in two |
| 31 | + * certificates: one issued by eMudhra and a cross-sign by Sectigo's AAA Certificate Services |
| 32 | + * root, which is trusted on every Android version. Matching the key covers both. |
| 33 | + * |
| 34 | + * @see <a |
| 35 | + * href="https://groups.google.com/a/mozilla.org/g/dev-security-policy/c/0R3XLJL6FuE">Compromised |
| 36 | + * eMudhra Intermediate Certificates</a> |
| 37 | + */ |
| 38 | + private static final Set<String> SPKI_SHA256_HASHES = |
| 39 | + new HashSet<>( |
| 40 | + Arrays.asList( |
| 41 | + "43bffa19d6ef651451f6841e019931e96f6f2eaeff28ab4b6f1ce4576de2717b", |
| 42 | + "49dd353dc2b919a3770645117d0e60c16115e7d1bc259eefe734b00ea434fe5f", |
| 43 | + "5c0660a2b164910d8ff589c18e14552e9e0ec237dcefe2cb736d41a4811f81a0", |
| 44 | + "63ecefee431d49523bc1136c4e72d0206a477086caf606cd5cf025686c109cf8", |
| 45 | + "92c063d67e8f14db71d288e11266ffdaf69cbf115f7bab14f3bc2f68d25e0e30", |
| 46 | + "e0254c7fcdc265e2f0f8a428dabb75ab4ae039b4872d62f5ee57f0877d5a8a41", |
| 47 | + "ebed6475c2c2d80fcb199f3fe8dd6e6b3ce60b77eb15d4e42c3dec0c6f5b5105", |
| 48 | + "ec4ffa1ad0ac88081314b7aaffef6a91f97cd57291bb5ba686c5415f5eb4f6b6")); |
| 49 | + |
| 50 | + private DistrustedCertificates() {} |
| 51 | + |
| 52 | + /** |
| 53 | + * Disconnects and throws if the server presented a distrusted certificate. |
| 54 | + * |
| 55 | + * <p>Call it right after {@link HttpURLConnection#connect()}: the TLS handshake is done by then, |
| 56 | + * but no part of the request has been written yet. A {@link javax.net.ssl.HostnameVerifier} can't |
| 57 | + * do this check, because the JVM's {@link HttpsURLConnection} only calls it when its own hostname |
| 58 | + * check fails. |
| 59 | + */ |
| 60 | + public static void verify(final @NotNull HttpURLConnection connection) |
| 61 | + throws SSLPeerUnverifiedException { |
| 62 | + if (!(connection instanceof HttpsURLConnection)) { |
| 63 | + return; |
| 64 | + } |
| 65 | + final @Nullable Certificate[] certificates = |
| 66 | + ((HttpsURLConnection) connection).getServerCertificates(); |
| 67 | + if (certificates == null) { |
| 68 | + return; |
| 69 | + } |
| 70 | + for (final @NotNull Certificate certificate : certificates) { |
| 71 | + if (SPKI_SHA256_HASHES.contains(spkiSha256(certificate))) { |
| 72 | + connection.disconnect(); |
| 73 | + throw new SSLPeerUnverifiedException( |
| 74 | + "The server's certificate chain contains a distrusted certificate."); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private static @NotNull String spkiSha256(final @NotNull Certificate certificate) { |
| 80 | + final byte[] hash; |
| 81 | + try { |
| 82 | + hash = MessageDigest.getInstance("SHA-256").digest(certificate.getPublicKey().getEncoded()); |
| 83 | + } catch (NoSuchAlgorithmException e) { |
| 84 | + // Every Java and Android platform is required to support SHA-256. |
| 85 | + throw new AssertionError(e); |
| 86 | + } |
| 87 | + final StringBuilder hex = new StringBuilder(hash.length * 2); |
| 88 | + for (final byte b : hash) { |
| 89 | + hex.append(Character.forDigit((b >> 4) & 0xf, 16)).append(Character.forDigit(b & 0xf, 16)); |
| 90 | + } |
| 91 | + return hex.toString(); |
| 92 | + } |
| 93 | +} |
0 commit comments