Skip to content

Commit 787a0c6

Browse files
committed
add back SNI using HttpsURLConnection.getDefaultHostnameVerifier().verify()
1 parent 18655c9 commit 787a0c6

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

tinodesdk/src/main/java/co/tinode/tinodesdk/Connection.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
import java.util.Map;
1414
import java.util.concurrent.TimeUnit;
1515

16+
import javax.net.ssl.HttpsURLConnection;
17+
import javax.net.ssl.SSLHandshakeException;
18+
import javax.net.ssl.SSLSession;
19+
import javax.net.ssl.SSLSocket;
20+
1621
/**
1722
* A thinly wrapped websocket connection.
1823
*/
@@ -108,8 +113,19 @@ private void connectSocket(final boolean reconnect) {
108113
} else {
109114
connectBlocking(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
110115
}
116+
117+
if ("wss".equals(uri.getScheme())) {
118+
// SNI: Verify server host name.
119+
SSLSession sess = ((SSLSocket) getSocket()).getSession();
120+
String hostName = uri.getHost();
121+
if (!HttpsURLConnection.getDefaultHostnameVerifier().verify(hostName, sess)) {
122+
close();
123+
throw new SSLHandshakeException("SNI verification failed. Expected: '" + uri.getHost() +
124+
"', actual: '" + sess.getPeerPrincipal() + "'");
125+
}
126+
}
111127
} catch (Exception ex) {
112-
Log.d(TAG, "socketConnectionRunnable exception!", ex);
128+
Log.i(TAG, "WS connection failed", ex);
113129
if (mListener != null) {
114130
mListener.onError(Connection.this, ex);
115131
}

0 commit comments

Comments
 (0)