Skip to content

Commit 13a82b0

Browse files
committed
More detailed logging in case of TLS hostname mismatch
1 parent c9c45b9 commit 13a82b0

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

proxybase/src/main/java/com/dajudge/proxybase/HttpClientHostnameCheck.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919

2020
import org.apache.hc.client5.http.ssl.HttpsSupport;
2121

22-
import javax.net.ssl.SSLPeerUnverifiedException;
2322
import javax.net.ssl.SSLSession;
2423
import javax.net.ssl.SSLSessionContext;
2524
import java.security.Principal;
2625
import java.security.cert.Certificate;
2726
import java.security.cert.CertificateException;
2827
import java.security.cert.X509Certificate;
2928

29+
import static java.lang.String.format;
30+
3031
class HttpClientHostnameCheck implements HostnameCheck {
3132
public static final javax.net.ssl.HostnameVerifier VERIFIER = HttpsSupport.getDefaultHostnameVerifier();
3233
private final String hostname;
@@ -37,9 +38,15 @@ class HttpClientHostnameCheck implements HostnameCheck {
3738

3839
@Override
3940
public void verify(final X509Certificate cert) throws CertificateException {
40-
if (!VERIFIER.verify(hostname, new DummySslSession(cert))) {
41-
throw new CertificateException("Certificate does not match hostname: " + hostname);
42-
}
41+
if (VERIFIER.verify(hostname, new DummySslSession(cert))) {
42+
return;
43+
}
44+
throw new CertificateException(format(
45+
"Certificate does not match hostname '%s': DN=%s, SANs=%s ",
46+
hostname,
47+
cert.getSubjectDN(),
48+
cert.getSubjectAlternativeNames()
49+
));
4350
}
4451

4552
private static class DummySslSession implements SSLSession {
@@ -100,7 +107,7 @@ public String[] getValueNames() {
100107
}
101108

102109
@Override
103-
public Certificate[] getPeerCertificates() throws SSLPeerUnverifiedException {
110+
public Certificate[] getPeerCertificates() {
104111
return new Certificate[]{certificate};
105112
}
106113

@@ -116,7 +123,7 @@ public javax.security.cert.X509Certificate[] getPeerCertificateChain() {
116123
}
117124

118125
@Override
119-
public Principal getPeerPrincipal() throws SSLPeerUnverifiedException {
126+
public Principal getPeerPrincipal() {
120127
return null;
121128
}
122129

0 commit comments

Comments
 (0)