2016-01-12 15:33:15 +00:00
|
|
|
package eu.siacs.conversations.utils;
|
|
|
|
|
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;
|
|
|
|
|
2016-01-12 15:33:15 +00:00
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
|
2016-02-03 17:17:16 +00:00
|
|
|
import javax.net.ssl.SSLContext;
|
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");
|
|
|
|
supportProtocols = supportedProtocols.toArray(new String[supportedProtocols.size()]);
|
|
|
|
|
|
|
|
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) {
|
|
|
|
try {
|
|
|
|
Conscrypt.setHostname(socket, hostname);
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
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-23 09:20:23 +00:00
|
|
|
public static void setApplicationProtocol(final SSLSocket socket, final String protocol) {
|
2018-09-21 14:33:07 +00:00
|
|
|
try {
|
2018-09-23 09:20:23 +00:00
|
|
|
Conscrypt.setApplicationProtocols(socket, new String[]{protocol});
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
Log.e(Config.LOGTAG, "unable to set ALPN on socket", e);
|
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 {
|
|
|
|
return SSLContext.getInstance("TLSv1.3");
|
|
|
|
}
|
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
|
|
|
}
|