1919
2020import org .apache .hc .client5 .http .ssl .HttpsSupport ;
2121
22- import javax .net .ssl .SSLPeerUnverifiedException ;
2322import javax .net .ssl .SSLSession ;
2423import javax .net .ssl .SSLSessionContext ;
2524import java .security .Principal ;
2625import java .security .cert .Certificate ;
2726import java .security .cert .CertificateException ;
2827import java .security .cert .X509Certificate ;
2928
29+ import static java .lang .String .format ;
30+
3031class 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