use conscrypt as security provider to provide tls 1.3 and modern cyphers on old androids

This commit is contained in:
Daniel Gultsch 2018-09-21 16:33:07 +02:00
parent 1985f6bdec
commit 6637d7056e
5 changed files with 61 additions and 59 deletions

View file

@ -52,6 +52,7 @@ dependencies {
implementation 'rocks.xmpp:xmpp-addr:0.8.0'
implementation 'org.osmdroid:osmdroid-android:6.0.1'
implementation 'org.hsluv:hsluv:0.2'
implementation 'org.conscrypt:conscrypt-android:1.3.0'
}
ext {

View file

@ -40,12 +40,14 @@ import android.util.Log;
import android.util.LruCache;
import android.util.Pair;
import org.conscrypt.Conscrypt;
import org.openintents.openpgp.IOpenPgpService2;
import org.openintents.openpgp.util.OpenPgpApi;
import org.openintents.openpgp.util.OpenPgpServiceConnection;
import java.net.URL;
import java.security.SecureRandom;
import java.security.Security;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
@ -955,7 +957,7 @@ public class XmppConnectionService extends Service {
public void onCreate() {
OmemoSetting.load(this);
ExceptionHelper.init(getApplicationContext());
PRNGFixes.apply();
Security.insertProviderAt(Conscrypt.newProvider(), 1);
Resolver.init(this);
this.mRandom = new SecureRandom();
updateMemorizingTrustmanager();

View file

@ -1,6 +1,6 @@
package eu.siacs.conversations.utils;
import android.os.Build;
import android.util.Log;
import java.lang.reflect.Method;
import java.security.NoSuchAlgorithmException;
@ -9,12 +9,16 @@ import java.util.Collection;
import java.util.LinkedList;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.entities.Account;
public class SSLSocketHelper {
public static void setSecurity(final SSLSocket sslSocket) throws NoSuchAlgorithmException {
public static void setSecurity(final SSLSocket sslSocket) {
final String[] supportProtocols;
final Collection<String> supportedProtocols = new LinkedList<>(
Arrays.asList(sslSocket.getSupportedProtocols()));
@ -31,14 +35,8 @@ public class SSLSocketHelper {
}
public static void setSNIHost(final SSLSocketFactory factory, final SSLSocket socket, final String hostname) {
if (factory instanceof android.net.SSLCertificateSocketFactory && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
if (factory instanceof android.net.SSLCertificateSocketFactory) {
((android.net.SSLCertificateSocketFactory) factory).setHostname(socket, hostname);
} else {
try {
socket.getClass().getMethod("setHostname", String.class).invoke(socket, hostname);
} catch (Throwable e) {
// ignore any error, we just can't set the hostname...
}
}
}
@ -64,10 +62,11 @@ public class SSLSocketHelper {
}
public static SSLContext getSSLContext() throws NoSuchAlgorithmException {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
return SSLContext.getInstance("TLSv1.2");
} else {
return SSLContext.getInstance("TLS");
return SSLContext.getInstance("TLSv1.3");
}
public static void log(Account account, SSLSocket socket) {
SSLSession session = socket.getSession();
Log.d(Config.LOGTAG,account.getJid().asBareJid()+": protocol="+session.getProtocol()+" cipher="+session.getCipherSuite());
}
}

View file

@ -16,7 +16,7 @@ public class TLSSocketFactory extends SSLSocketFactory {
private final SSLSocketFactory internalSSLSocketFactory;
public TLSSocketFactory(X509TrustManager[] trustManager, SecureRandom random) throws KeyManagementException, NoSuchAlgorithmException {
SSLContext context = SSLContext.getInstance("TLS");
SSLContext context = SSLSocketHelper.getSSLContext();
context.init(null, trustManager, random);
this.internalSSLSocketFactory = context.getSocketFactory();
}
@ -58,11 +58,7 @@ public class TLSSocketFactory extends SSLSocketFactory {
private static Socket enableTLSOnSocket(Socket socket) {
if(socket != null && (socket instanceof SSLSocket)) {
try {
SSLSocketHelper.setSecurity((SSLSocket) socket);
} catch (NoSuchAlgorithmException e) {
//ignoring
}
}
return socket;
}

View file

@ -455,6 +455,9 @@ public class XmppConnection implements Runnable {
if (Thread.currentThread().isInterrupted()) {
throw new InterruptedException();
}
if (socket instanceof SSLSocket) {
SSLSocketHelper.log(account, (SSLSocket) socket);
}
return tag != null && tag.isStart("stream");
}
@ -852,6 +855,7 @@ public class XmppConnection implements Runnable {
features.encryptionEnabled = true;
final Tag tag = tagReader.readTag();
if (tag != null && tag.isStart("stream")) {
SSLSocketHelper.log(account, sslSocket);
processStream();
} else {
throw new IOException("server didn't restart stream after STARTTLS");