2016-01-12 15:33:15 +00:00
|
|
|
package eu.siacs.conversations.utils;
|
|
|
|
|
2018-09-27 08:00:58 +00:00
|
|
|
import android.os.Build;
|
|
|
|
import android.support.annotation.RequiresApi;
|
2018-09-21 14:33:07 +00:00
|
|
|
import android.util.Log;
|
2016-02-03 17:17:16 +00:00
|
|
|
|
2018-09-23 09:20:23 +00:00
|
|
|
import org.conscrypt.Conscrypt;
|
|
|
|
|
2018-09-27 08:00:58 +00:00
|
|
|
import java.lang.reflect.Method;
|
2016-01-12 15:33:15 +00:00
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collection;
|
2018-09-27 08:00:58 +00:00
|
|
|
import java.util.Collections;
|
2016-01-12 15:33:15 +00:00
|
|
|
import java.util.LinkedList;
|
|
|
|
|
2018-09-27 08:00:58 +00:00
|
|
|
import javax.net.ssl.SNIHostName;
|
2016-02-03 17:17:16 +00:00
|
|
|
import javax.net.ssl.SSLContext;
|
2018-09-27 08:00:58 +00:00
|
|
|
import javax.net.ssl.SSLParameters;
|
2018-09-21 14:33:07 +00:00
|
|
|
import javax.net.ssl.SSLSession;
|
2016-01-12 15:33:15 +00:00
|
|
|
import javax.net.ssl.SSLSocket;
|
|
|
|
|
2018-09-21 14:33:07 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
|
|
|
import eu.siacs.conversations.entities.Account;
|
|
|
|
|
2016-01-12 15:33:15 +00:00
|
|
|
public class SSLSocketHelper {
|
|
|
|
|
2018-09-21 14:33:07 +00:00
|
|
|
public static void setSecurity(final SSLSocket sslSocket) {
|
|
|
|
final String[] supportProtocols;
|
|
|
|
final Collection<String> supportedProtocols = new LinkedList<>(
|
|
|
|
Arrays.asList(sslSocket.getSupportedProtocols()));
|
|
|
|
supportedProtocols.remove("SSLv3");
|
2020-12-11 10:05:08 +00:00
|
|
|
supportProtocols = supportedProtocols.toArray(new String[0]);
|
2018-09-21 14:33:07 +00:00
|
|
|
|
|
|
|
sslSocket.setEnabledProtocols(supportProtocols);
|
2016-01-12 15:33:15 +00:00
|
|
|
|
2018-09-21 14:33:07 +00:00
|
|
|
final String[] cipherSuites = CryptoHelper.getOrderedCipherSuites(
|
|
|
|
sslSocket.getSupportedCipherSuites());
|
|
|
|
if (cipherSuites.length > 0) {
|
|
|
|
sslSocket.setEnabledCipherSuites(cipherSuites);
|
|
|
|
}
|
|
|
|
}
|
2016-01-12 15:33:15 +00:00
|
|
|
|
2018-09-23 09:20:23 +00:00
|
|
|
public static void setHostname(final SSLSocket socket, final String hostname) {
|
2018-09-27 08:00:58 +00:00
|
|
|
if (Conscrypt.isConscrypt(socket)) {
|
2018-09-23 09:20:23 +00:00
|
|
|
Conscrypt.setHostname(socket, hostname);
|
2018-09-27 08:00:58 +00:00
|
|
|
} 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) {
|
2018-09-23 09:20:23 +00:00
|
|
|
Log.e(Config.LOGTAG, "unable to set SNI name on socket (" + hostname + ")", e);
|
2018-09-21 14:33:07 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-12 15:33:15 +00:00
|
|
|
|
2018-09-27 08:00:58 +00:00
|
|
|
@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) {
|
2018-09-21 14:33:07 +00:00
|
|
|
try {
|
2018-09-27 08:00:58 +00:00
|
|
|
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)) {
|
2018-09-23 09:20:23 +00:00
|
|
|
Conscrypt.setApplicationProtocols(socket, new String[]{protocol});
|
2018-09-27 08:00:58 +00:00
|
|
|
} else {
|
|
|
|
setApplicationProtocolReflection(socket, protocol);
|
2018-09-21 14:33:07 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-12 15:33:15 +00:00
|
|
|
|
2018-09-21 14:33:07 +00:00
|
|
|
public static SSLContext getSSLContext() throws NoSuchAlgorithmException {
|
2018-10-06 17:33:38 +00:00
|
|
|
try {
|
|
|
|
return SSLContext.getInstance("TLSv1.3");
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
return SSLContext.getInstance("TLSv1.2");
|
|
|
|
}
|
2018-09-21 14:33:07 +00:00
|
|
|
}
|
2016-02-03 17:17:16 +00:00
|
|
|
|
2018-09-21 14:33:07 +00:00
|
|
|
public static void log(Account account, SSLSocket socket) {
|
|
|
|
SSLSession session = socket.getSession();
|
2018-09-23 09:20:23 +00:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": protocol=" + session.getProtocol() + " cipher=" + session.getCipherSuite());
|
2018-09-21 14:33:07 +00:00
|
|
|
}
|
2016-01-12 15:33:15 +00:00
|
|
|
}
|