Skip to content

Commit d35fd16

Browse files
committed
Add support for HostnameVerifier
1 parent fe258fe commit d35fd16

3 files changed

Lines changed: 55 additions & 0 deletions

File tree

smack-core/src/main/java/org/jivesoftware/smack/ConnectionConfiguration.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.jxmpp.util.XmppStringUtils;
2525

2626
import javax.net.SocketFactory;
27+
import javax.net.ssl.HostnameVerifier;
2728
import javax.net.ssl.SSLContext;
2829
import javax.security.auth.callback.CallbackHandler;
2930

@@ -91,6 +92,8 @@ public class ConnectionConfiguration implements Cloneable {
9192
*/
9293
private String[] enabledSSLCiphers;
9394

95+
private HostnameVerifier hostnameVerifier;
96+
9497
/**
9598
* Permanent store for the Roster, needed for roster versioning
9699
*/
@@ -358,6 +361,29 @@ public String[] getEnabledSSLCiphers() {
358361
return enabledSSLCiphers;
359362
}
360363

364+
/**
365+
* Set the HostnameVerifier used to verify the hostname of SSLSockets used by XMPP connections
366+
* created with this ConnectionConfiguration.
367+
*
368+
* @param verifier
369+
*/
370+
public void setHostnameVerifier(HostnameVerifier verifier) {
371+
hostnameVerifier = verifier;
372+
}
373+
374+
/**
375+
* Returns the configured HostnameVerifier of this ConnectionConfiguration or the Smack default
376+
* HostnameVerifier configured with
377+
* {@link SmackConfiguration#setDefaultHostnameVerifier(HostnameVerifier)}.
378+
*
379+
* @return a configured HostnameVerifier or <code>null</code>
380+
*/
381+
public HostnameVerifier getHostnameVerifier() {
382+
if (hostnameVerifier != null)
383+
return hostnameVerifier;
384+
return SmackConfiguration.getDefaultHostnameVerifier();
385+
}
386+
361387
/**
362388
* Returns true if the connection is going to use stream compression. Stream compression
363389
* will be requested after TLS was established (if TLS was enabled) and only if the server

smack-core/src/main/java/org/jivesoftware/smack/SmackConfiguration.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import java.util.logging.Level;
3232
import java.util.logging.Logger;
3333

34+
import javax.net.ssl.HostnameVerifier;
35+
3436
import org.jivesoftware.smack.compression.Java7ZlibInputOutputStream;
3537
import org.jivesoftware.smack.compression.XMPPInputOutputStream;
3638
import org.jivesoftware.smack.initializer.SmackInitializer;
@@ -178,6 +180,8 @@ public final class SmackConfiguration {
178180
*/
179181
private static ParsingExceptionCallback defaultCallback = new ExceptionThrowingCallback();
180182

183+
private static HostnameVerifier defaultHostnameVerififer;
184+
181185
/**
182186
* Returns the Smack version information, eg "1.3.0".
183187
*
@@ -319,6 +323,25 @@ public static List<XMPPInputOutputStream> getCompresionHandlers() {
319323
return res;
320324
}
321325

326+
/**
327+
* Set the default HostnameVerifier that will be used by XMPP connections to verify the hostname
328+
* of a TLS certificate. XMPP connections are able to overwrite this settings by supplying a
329+
* HostnameVerifier in their ConnecitonConfiguration with
330+
* {@link ConnectionConfiguration#setHostnameVerifier(HostnameVerifier)}.
331+
*/
332+
public static void setDefaultHostnameVerifier(HostnameVerifier verifier) {
333+
defaultHostnameVerififer = verifier;
334+
}
335+
336+
/**
337+
* Get the default HostnameVerifier
338+
*
339+
* @return the default HostnameVerifier or <code>null</code> if none was set
340+
*/
341+
static HostnameVerifier getDefaultHostnameVerifier() {
342+
return defaultHostnameVerififer;
343+
}
344+
322345
public static void processConfigFile(InputStream cfgFileStream,
323346
Collection<Exception> exceptions) throws Exception {
324347
processConfigFile(cfgFileStream, exceptions, SmackConfiguration.class.getClassLoader());

smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.xmlpull.v1.XmlPullParserException;
4747
import org.xmlpull.v1.XmlPullParserFactory;
4848

49+
import javax.net.ssl.HostnameVerifier;
4950
import javax.net.ssl.KeyManager;
5051
import javax.net.ssl.KeyManagerFactory;
5152
import javax.net.ssl.SSLContext;
@@ -674,6 +675,11 @@ else if(config.getKeystoreType().equals("Apple")) {
674675
// Proceed to do the handshake
675676
sslSocket.startHandshake();
676677

678+
final HostnameVerifier verifier = getConfiguration().getHostnameVerifier();
679+
if (verifier != null && !verifier.verify(getServiceName(), sslSocket.getSession())) {
680+
throw new CertificateException("Hostname verification of certificate failed. Certificate does not authenticate " + getServiceName());
681+
}
682+
677683
//if (((SSLSocket) socket).getWantClientAuth()) {
678684
// System.err.println("XMPPConnection wants client auth");
679685
//}

0 commit comments

Comments
 (0)