use javax api instead of BC for file decryption

This commit is contained in:
Daniel Gultsch 2018-09-17 17:47:51 +02:00
parent 67e7d2cf9e
commit 09eca8478e

View file

@ -6,62 +6,39 @@ import android.content.pm.PackageManager;
import android.os.Build; import android.os.Build;
import android.os.PowerManager; import android.os.PowerManager;
import android.os.SystemClock; import android.os.SystemClock;
import android.util.Log;
import android.util.Pair; import android.util.Pair;
import org.bouncycastle.crypto.engines.AESEngine;
import org.bouncycastle.crypto.modes.AEADBlockCipher;
import org.bouncycastle.crypto.modes.GCMBlockCipher;
import org.bouncycastle.crypto.params.AEADParameters;
import org.bouncycastle.crypto.params.KeyParameter;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import javax.crypto.CipherInputStream; import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream; import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.DownloadableFile; import eu.siacs.conversations.entities.DownloadableFile;
import eu.siacs.conversations.utils.Compatibility;
public class AbstractConnectionManager { public class AbstractConnectionManager {
protected XmppConnectionService mXmppConnectionService;
private static final String KEYTYPE = "AES";
private static final String CIPHERMODE = "AES/GCM/NoPadding";
private static final String PROVIDER = "BC";
private static final int UI_REFRESH_THRESHOLD = 250; private static final int UI_REFRESH_THRESHOLD = 250;
private static final AtomicLong LAST_UI_UPDATE_CALL = new AtomicLong(0); private static final AtomicLong LAST_UI_UPDATE_CALL = new AtomicLong(0);
protected XmppConnectionService mXmppConnectionService;
public AbstractConnectionManager(XmppConnectionService service) { public AbstractConnectionManager(XmppConnectionService service) {
this.mXmppConnectionService = service; this.mXmppConnectionService = service;
} }
public XmppConnectionService getXmppConnectionService() {
return this.mXmppConnectionService;
}
public long getAutoAcceptFileSize() {
return this.mXmppConnectionService.getLongPreference("auto_accept_file_size",R.integer.auto_accept_filesize);
}
public boolean hasStoragePermission() {
if (!Config.ONLY_INTERNAL_STORAGE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return mXmppConnectionService.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
} else {
return true;
}
}
public static Pair<InputStream, Integer> createInputStream(DownloadableFile file, boolean gcm) throws FileNotFoundException { public static Pair<InputStream, Integer> createInputStream(DownloadableFile file, boolean gcm) throws FileNotFoundException {
FileInputStream is; FileInputStream is;
int size; int size;
@ -72,15 +49,15 @@ public class AbstractConnectionManager {
} }
try { try {
if (gcm) { if (gcm) {
AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine()); Cipher cipher = Compatibility.twentyTwo() ? Cipher.getInstance(CIPHERMODE) : Cipher.getInstance(CIPHERMODE, PROVIDER);
cipher.init(true, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv())); SecretKeySpec keySpec = new SecretKeySpec(file.getKey(), KEYTYPE);
InputStream cis = new org.bouncycastle.crypto.io.CipherInputStream(is, cipher); IvParameterSpec ivSpec = new IvParameterSpec(file.getIv());
return new Pair<>(cis, cipher.getOutputSize(size)); cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
return new Pair<>(new CipherInputStream(is, cipher), cipher.getOutputSize(size));
} else { } else {
IvParameterSpec ips = new IvParameterSpec(file.getIv()); IvParameterSpec ips = new IvParameterSpec(file.getIv());
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(file.getKey(), "AES"), ips); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(file.getKey(), KEYTYPE), ips);
Log.d(Config.LOGTAG, "opening encrypted input stream");
return new Pair<>(new CipherInputStream(is, cipher), (size / 16 + 1) * 16); return new Pair<>(new CipherInputStream(is, cipher), (size / 16 + 1) * 16);
} }
} catch (Exception e) { } catch (Exception e) {
@ -108,24 +85,35 @@ public class AbstractConnectionManager {
} }
try { try {
if (gcm) { if (gcm) {
AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine()); Cipher cipher = Compatibility.twentyTwo() ? Cipher.getInstance(CIPHERMODE) : Cipher.getInstance(CIPHERMODE, PROVIDER);
cipher.init(false, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv())); SecretKeySpec keySpec = new SecretKeySpec(file.getKey(), KEYTYPE);
return new org.bouncycastle.crypto.io.CipherOutputStream(os, cipher); IvParameterSpec ivSpec = new IvParameterSpec(file.getIv());
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
return new CipherOutputStream(os, cipher);
} else { } else {
IvParameterSpec ips = new IvParameterSpec(file.getIv()); IvParameterSpec ips = new IvParameterSpec(file.getIv());
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(file.getKey(), "AES"), ips); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(file.getKey(), KEYTYPE), ips);
Log.d(Config.LOGTAG, "opening encrypted output stream");
return new CipherOutputStream(os, cipher); return new CipherOutputStream(os, cipher);
} }
} catch (InvalidKeyException e) { } catch (Exception e) {
return null; throw new AssertionError(e);
} catch (NoSuchAlgorithmException e) { }
return null; }
} catch (NoSuchPaddingException e) {
return null; public XmppConnectionService getXmppConnectionService() {
} catch (InvalidAlgorithmParameterException e) { return this.mXmppConnectionService;
return null; }
public long getAutoAcceptFileSize() {
return this.mXmppConnectionService.getLongPreference("auto_accept_file_size", R.integer.auto_accept_filesize);
}
public boolean hasStoragePermission() {
if (!Config.ONLY_INTERNAL_STORAGE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return mXmppConnectionService.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
} else {
return true;
} }
} }