diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/TLSUtils.java b/smack-core/src/main/java/org/jivesoftware/smack/util/TLSUtils.java index e85de4973..822543d1d 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/TLSUtils.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/TLSUtils.java @@ -25,7 +25,9 @@ import java.util.Arrays; import java.util.HashSet; import java.util.Set; +import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSocket; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; @@ -78,15 +80,17 @@ public class TLSUtils { } /** - * Accept all SSL/TLS certificates. + * Accept all TLS certificates. *

- * Warning Use with care. This method make the Connection use - * {@link AcceptAllTrustManager}. Only use this method if you understand the implications. + * Warning: Use with care. This method make the Connection use {@link AcceptAllTrustManager} and essentially + * invalidates all security guarantees provided by TLS. Only use this method if you understand the + * implications. *

* - * @param builder + * @param builder a connection configuration builder. * @throws NoSuchAlgorithmException * @throws KeyManagementException + * @return the given builder. */ public static > B acceptAllCertificates(B builder) throws NoSuchAlgorithmException, KeyManagementException { SSLContext context = SSLContext.getInstance(TLS); @@ -95,6 +99,30 @@ public class TLSUtils { return builder; } + private static final HostnameVerifier DOES_NOT_VERIFY_VERIFIER = new HostnameVerifier() { + @Override + public boolean verify(String hostname, SSLSession session) { + // This verifier doesn't verify the hostname, it always returns true. + return true; + } + }; + + /** + * Disable the hostname verification of TLS certificates. + *

+ * Warning: Use with care. This disables hostname verification of TLS certificates and essentially + * invalidates all security guarantees provided by TLS. Only use this method if you understand the + * implications. + *

+ * + * @param builder a connection configuration builder. + * @return the given builder. + */ + public static > B disableHostnameVerificationForTlsCertificicates(B builder) { + builder.setHostnameVerifier(DOES_NOT_VERIFY_VERIFIER); + return builder; + } + public static void setEnabledProtocolsAndCiphers(final SSLSocket sslSocket, String[] enabledProtocols, String[] enabledCiphers) throws SecurityNotPossibleException {