fixed sni and alpn for kitkat
This commit is contained in:
parent
d8c1327658
commit
cf879dd8e8
|
@ -1,15 +1,21 @@
|
||||||
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 org.conscrypt.Conscrypt;
|
import org.conscrypt.Conscrypt;
|
||||||
|
|
||||||
|
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.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;
|
||||||
|
|
||||||
|
@ -35,18 +41,50 @@ public class SSLSocketHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setHostname(final SSLSocket socket, final String hostname) {
|
public static void setHostname(final SSLSocket socket, final String hostname) {
|
||||||
try {
|
if (Conscrypt.isConscrypt(socket)) {
|
||||||
Conscrypt.setHostname(socket, hostname);
|
Conscrypt.setHostname(socket, hostname);
|
||||||
} catch (IllegalArgumentException e) {
|
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||||
|
setHostnameNougat(socket, hostname);
|
||||||
|
} else {
|
||||||
|
setHostnameReflection(socket, hostname);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void setHostnameReflection(final SSLSocket socket, final String hostname) {
|
||||||
|
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);
|
Log.e(Config.LOGTAG, "unable to set SNI name on socket (" + hostname + ")", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setApplicationProtocol(final SSLSocket socket, final String protocol) {
|
@RequiresApi(api = Build.VERSION_CODES.N)
|
||||||
|
private static void setHostnameNougat(final SSLSocket socket, final String hostname) {
|
||||||
|
final SSLParameters parameters = new SSLParameters();
|
||||||
|
parameters.setServerNames(Collections.singletonList(new SNIHostName(hostname)));
|
||||||
|
socket.setSSLParameters(parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void setApplicationProtocolReflection(final SSLSocket socket, final String protocol) {
|
||||||
try {
|
try {
|
||||||
|
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) {
|
||||||
|
Log.e(Config.LOGTAG,"unable to set ALPN on socket",e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setApplicationProtocol(final SSLSocket socket, final String protocol) {
|
||||||
|
if (Conscrypt.isConscrypt(socket)) {
|
||||||
Conscrypt.setApplicationProtocols(socket, new String[]{protocol});
|
Conscrypt.setApplicationProtocols(socket, new String[]{protocol});
|
||||||
} catch (IllegalArgumentException e) {
|
} else {
|
||||||
Log.e(Config.LOGTAG, "unable to set ALPN on socket", e);
|
setApplicationProtocolReflection(socket, protocol);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -48,7 +48,7 @@ public class XmlReader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Tag readTag() throws XmlPullParserException, IOException {
|
public Tag readTag() throws IOException {
|
||||||
try {
|
try {
|
||||||
while (this.is != null && parser.next() != XmlPullParser.END_DOCUMENT) {
|
while (this.is != null && parser.next() != XmlPullParser.END_DOCUMENT) {
|
||||||
if (parser.getEventType() == XmlPullParser.START_TAG) {
|
if (parser.getEventType() == XmlPullParser.START_TAG) {
|
||||||
|
|
Loading…
Reference in a new issue