aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/utils
diff options
context:
space:
mode:
authorChristian Schneppe <christian@pix-art.de>2018-10-01 11:01:40 +0200
committerChristian Schneppe <christian@pix-art.de>2018-10-01 11:01:40 +0200
commit45a436a079d5d381cd89a02de62fa7f459eaf66a (patch)
treee2314a0b531aa52ad81b084a0c599292ce5ec532 /src/main/java/de/pixart/messenger/utils
parentc0b51141a76b23f05e809d133fdf627b3b4c09bb (diff)
fixed regression that didn’t enable SNI
Diffstat (limited to 'src/main/java/de/pixart/messenger/utils')
-rw-r--r--src/main/java/de/pixart/messenger/utils/SSLSocketHelper.java51
1 files changed, 31 insertions, 20 deletions
diff --git a/src/main/java/de/pixart/messenger/utils/SSLSocketHelper.java b/src/main/java/de/pixart/messenger/utils/SSLSocketHelper.java
index f0d1c00ec..1e2b58d27 100644
--- a/src/main/java/de/pixart/messenger/utils/SSLSocketHelper.java
+++ b/src/main/java/de/pixart/messenger/utils/SSLSocketHelper.java
@@ -1,17 +1,21 @@
package de.pixart.messenger.utils;
+import android.os.Build;
+import android.support.annotation.RequiresApi;
import android.util.Log;
import java.lang.reflect.Method;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Collection;
+import java.util.Collections;
import java.util.LinkedList;
+import javax.net.ssl.SNIHostName;
import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
-import javax.net.ssl.SSLSocketFactory;
import de.pixart.messenger.Config;
import de.pixart.messenger.entities.Account;
@@ -34,30 +38,37 @@ public class SSLSocketHelper {
}
}
- public static void setSNIHost(final SSLSocketFactory factory, final SSLSocket socket, final String hostname) {
- if (factory instanceof android.net.SSLCertificateSocketFactory) {
- ((android.net.SSLCertificateSocketFactory) factory).setHostname(socket, hostname);
+ public static void setSNIHost(final SSLSocket socket, final String hostname) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+ 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 {
- 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);
- // 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
- final byte[] protocolUTF8Bytes = protocol.getBytes("UTF-8");
- final byte[] lengthPrefixedProtocols = new byte[protocolUTF8Bytes.length + 1];
- lengthPrefixedProtocols[0] = (byte) protocol.length(); // cannot be over 255 anyhow
- System.arraycopy(protocolUTF8Bytes, 0, lengthPrefixedProtocols, 1, protocolUTF8Bytes.length);
- method.invoke(socket, new Object[]{lengthPrefixedProtocols});
- }
+ final Method method = socket.getClass().getMethod("setAlpnProtocols", byte[].class);
+ // 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
+ final byte[] protocolUTF8Bytes = protocol.getBytes("UTF-8");
+ final byte[] lengthPrefixedProtocols = new byte[protocolUTF8Bytes.length + 1];
+ lengthPrefixedProtocols[0] = (byte) protocol.length(); // cannot be over 255 anyhow
+ System.arraycopy(protocolUTF8Bytes, 0, lengthPrefixedProtocols, 1, protocolUTF8Bytes.length);
+ method.invoke(socket, new Object[]{lengthPrefixedProtocols});
} 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);
}
}