fixed regression that didn’t enable SNI
This commit is contained in:
parent
6637d7056e
commit
b759cf902d
|
@ -1,14 +1,20 @@
|
||||||
package eu.siacs.conversations.utils;
|
package eu.siacs.conversations.utils;
|
||||||
|
|
||||||
|
import android.os.Build;
|
||||||
|
import android.support.annotation.RequiresApi;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
import javax.net.ssl.SNIHostName;
|
||||||
|
import javax.net.ssl.SNIServerName;
|
||||||
import javax.net.ssl.SSLContext;
|
import javax.net.ssl.SSLContext;
|
||||||
|
import javax.net.ssl.SSLParameters;
|
||||||
import javax.net.ssl.SSLSession;
|
import javax.net.ssl.SSLSession;
|
||||||
import javax.net.ssl.SSLSocket;
|
import javax.net.ssl.SSLSocket;
|
||||||
import javax.net.ssl.SSLSocketFactory;
|
import javax.net.ssl.SSLSocketFactory;
|
||||||
|
@ -34,19 +40,27 @@ public class SSLSocketHelper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setSNIHost(final SSLSocketFactory factory, final SSLSocket socket, final String hostname) {
|
public static void setSNIHost(final SSLSocket socket, final String hostname) {
|
||||||
if (factory instanceof android.net.SSLCertificateSocketFactory) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||||
((android.net.SSLCertificateSocketFactory) factory).setHostname(socket, hostname);
|
setSNIHostNougat(socket, hostname);
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
socket.getClass().getMethod("setHostname", String.class).invoke(socket, hostname);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
Log.e(Config.LOGTAG, "unable to set SNI name on socket (" + hostname + ")", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setAlpnProtocol(final SSLSocketFactory factory, final SSLSocket socket, final String protocol) {
|
@RequiresApi(api = Build.VERSION_CODES.N)
|
||||||
|
private static void setSNIHostNougat(final SSLSocket socket, final String hostname) {
|
||||||
|
final SSLParameters parameters = new SSLParameters();
|
||||||
|
parameters.setServerNames(Collections.singletonList(new SNIHostName(hostname)));
|
||||||
|
socket.setSSLParameters(parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setAlpnProtocol(final SSLSocket socket, final String protocol) {
|
||||||
try {
|
try {
|
||||||
if (factory instanceof android.net.SSLCertificateSocketFactory && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
|
|
||||||
// can't call directly because of @hide?
|
|
||||||
//((android.net.SSLCertificateSocketFactory)factory).setAlpnProtocols(new byte[][]{protocol.getBytes("UTF-8")});
|
|
||||||
android.net.SSLCertificateSocketFactory.class.getMethod("setAlpnProtocols", byte[][].class).invoke(socket, new Object[]{new byte[][]{protocol.getBytes("UTF-8")}});
|
|
||||||
} else {
|
|
||||||
final Method method = socket.getClass().getMethod("setAlpnProtocols", byte[].class);
|
final Method method = socket.getClass().getMethod("setAlpnProtocols", byte[].class);
|
||||||
// the concatenation of 8-bit, length prefixed protocol names, just one in our case...
|
// the concatenation of 8-bit, length prefixed protocol names, just one in our case...
|
||||||
// http://tools.ietf.org/html/draft-agl-tls-nextprotoneg-04#page-4
|
// http://tools.ietf.org/html/draft-agl-tls-nextprotoneg-04#page-4
|
||||||
|
@ -55,9 +69,8 @@ public class SSLSocketHelper {
|
||||||
lengthPrefixedProtocols[0] = (byte) protocol.length(); // cannot be over 255 anyhow
|
lengthPrefixedProtocols[0] = (byte) protocol.length(); // cannot be over 255 anyhow
|
||||||
System.arraycopy(protocolUTF8Bytes, 0, lengthPrefixedProtocols, 1, protocolUTF8Bytes.length);
|
System.arraycopy(protocolUTF8Bytes, 0, lengthPrefixedProtocols, 1, protocolUTF8Bytes.length);
|
||||||
method.invoke(socket, new Object[]{lengthPrefixedProtocols});
|
method.invoke(socket, new Object[]{lengthPrefixedProtocols});
|
||||||
}
|
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
// ignore any error, we just can't set the alpn protocol...
|
Log.e(Config.LOGTAG,"unable to set ALPN on socket",e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -377,8 +377,8 @@ public class XmppConnection implements Runnable {
|
||||||
}
|
}
|
||||||
|
|
||||||
SSLSocketHelper.setSecurity((SSLSocket) localSocket);
|
SSLSocketHelper.setSecurity((SSLSocket) localSocket);
|
||||||
SSLSocketHelper.setSNIHost(tlsFactoryVerifier.factory, (SSLSocket) localSocket, account.getServer());
|
SSLSocketHelper.setSNIHost((SSLSocket) localSocket, account.getServer());
|
||||||
SSLSocketHelper.setAlpnProtocol(tlsFactoryVerifier.factory, (SSLSocket) localSocket, "xmpp-client");
|
SSLSocketHelper.setAlpnProtocol((SSLSocket) localSocket, "xmpp-client");
|
||||||
|
|
||||||
localSocket.connect(addr, Config.SOCKET_TIMEOUT * 1000);
|
localSocket.connect(addr, Config.SOCKET_TIMEOUT * 1000);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue