Skip to content

Commit 176f6fc

Browse files
w7849516230jpwsutton
authored andcommitted
add setSSLHostnameVerifier method for fixing issue 38 (#303)
Bug:#38 Signed-off-by: shawn <w7849516230@gmail.com>
1 parent 039a3d2 commit 176f6fc

3 files changed

Lines changed: 38 additions & 0 deletions

File tree

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/MqttAsyncClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ else if ((factory instanceof SSLSocketFactory) == false) {
507507
// Create the network module...
508508
netModule = new SSLNetworkModule((SSLSocketFactory) factory, host, port, clientId);
509509
((SSLNetworkModule)netModule).setSSLhandshakeTimeout(options.getConnectionTimeout());
510+
((SSLNetworkModule)netModule).setSSLHostnameVerifier(options.getSSLHostnameVerifier());
510511
// Ciphers suites need to be set, if they are available
511512
if (factoryFactory != null) {
512513
String[] enabledCiphers = factoryFactory.getEnabledCipherSuites(null);

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/MqttConnectOptions.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Properties;
2121

2222
import javax.net.SocketFactory;
23+
import javax.net.ssl.HostnameVerifier;
2324

2425
import org.eclipse.paho.client.mqttv3.util.Debug;
2526

@@ -73,6 +74,7 @@ public class MqttConnectOptions {
7374
private char[] password;
7475
private SocketFactory socketFactory;
7576
private Properties sslClientProps = null;
77+
private HostnameVerifier sslHostnameVerifier = null;
7678
private boolean cleanSession = CLEAN_SESSION_DEFAULT;
7779
private int connectionTimeout = CONNECTION_TIMEOUT_DEFAULT;
7880
private String[] serverURIs = null;
@@ -408,6 +410,26 @@ public void setSSLProperties(Properties props) {
408410
this.sslClientProps = props;
409411
}
410412

413+
/**
414+
* Returns the HostnameVerifier for the SSL connection.
415+
* @return the HostnameVerifier for the SSL connection
416+
*/
417+
public HostnameVerifier getSSLHostnameVerifier() {
418+
return sslHostnameVerifier;
419+
}
420+
421+
/**
422+
* Sets the HostnameVerifier for the SSL connection. Note that it will be
423+
* used after handshake on a connection and you should do actions by
424+
* yourserlf when hostname is verified error.
425+
* <p>
426+
* There is no default HostnameVerifier
427+
* </p>
428+
*/
429+
public void setSSLHostnameVerifier(HostnameVerifier hostnameVerifier) {
430+
this.sslHostnameVerifier = hostnameVerifier;
431+
}
432+
411433
/**
412434
* Returns whether the client and server should remember state for the client across reconnects.
413435
* @return the clean session flag

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/internal/SSLNetworkModule.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import java.io.IOException;
1919

20+
import javax.net.ssl.HostnameVerifier;
21+
import javax.net.ssl.SSLSession;
2022
import javax.net.ssl.SSLSocket;
2123
import javax.net.ssl.SSLSocketFactory;
2224

@@ -33,6 +35,7 @@ public class SSLNetworkModule extends TCPNetworkModule {
3335

3436
private String[] enabledCiphers;
3537
private int handshakeTimeoutSecs;
38+
private HostnameVerifier hostnameVerifier;
3639

3740
private String host;
3841
private int port;
@@ -88,13 +91,25 @@ public void setSSLhandshakeTimeout(int timeout) {
8891
this.handshakeTimeoutSecs = timeout;
8992
}
9093

94+
public HostnameVerifier getSSLHostnameVerifier() {
95+
return hostnameVerifier;
96+
}
97+
98+
public void setSSLHostnameVerifier(HostnameVerifier hostnameVerifier) {
99+
this.hostnameVerifier = hostnameVerifier;
100+
}
101+
91102
public void start() throws IOException, MqttException {
92103
super.start();
93104
setEnabledCiphers(enabledCiphers);
94105
int soTimeout = socket.getSoTimeout();
95106
// RTC 765: Set a timeout to avoid the SSL handshake being blocked indefinitely
96107
socket.setSoTimeout(this.handshakeTimeoutSecs*1000);
97108
((SSLSocket)socket).startHandshake();
109+
if (hostnameVerifier != null) {
110+
SSLSession session = ((SSLSocket)socket).getSession();
111+
hostnameVerifier.verify(host, session);
112+
}
98113
// reset timeout to default value
99114
socket.setSoTimeout(soTimeout);
100115
}

0 commit comments

Comments
 (0)