2014-10-22 16:38:44 +00:00
|
|
|
package eu.siacs.conversations.services;
|
|
|
|
|
2015-12-06 17:23:59 +00:00
|
|
|
import android.Manifest;
|
2015-08-10 17:48:36 +00:00
|
|
|
import android.content.Context;
|
2015-12-06 17:23:59 +00:00
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.os.Build;
|
2015-08-10 17:48:36 +00:00
|
|
|
import android.os.PowerManager;
|
2017-01-12 15:02:09 +00:00
|
|
|
import android.os.SystemClock;
|
2015-07-31 23:19:16 +00:00
|
|
|
import android.util.Log;
|
|
|
|
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.FileNotFoundException;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.security.InvalidAlgorithmParameterException;
|
|
|
|
import java.security.InvalidKeyException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2017-01-12 15:02:09 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicLong;
|
2015-07-31 23:19:16 +00:00
|
|
|
|
|
|
|
import javax.crypto.Cipher;
|
|
|
|
import javax.crypto.CipherInputStream;
|
|
|
|
import javax.crypto.CipherOutputStream;
|
|
|
|
import javax.crypto.NoSuchPaddingException;
|
|
|
|
import javax.crypto.spec.IvParameterSpec;
|
|
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
|
|
|
|
|
|
import eu.siacs.conversations.Config;
|
2017-06-30 19:22:35 +00:00
|
|
|
import eu.siacs.conversations.R;
|
2015-07-31 23:19:16 +00:00
|
|
|
import eu.siacs.conversations.entities.DownloadableFile;
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
public class AbstractConnectionManager {
|
|
|
|
protected XmppConnectionService mXmppConnectionService;
|
|
|
|
|
2017-01-12 15:02:09 +00:00
|
|
|
private static final int UI_REFRESH_THRESHOLD = 250;
|
|
|
|
private static final AtomicLong LAST_UI_UPDATE_CALL = new AtomicLong(0);
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
public AbstractConnectionManager(XmppConnectionService service) {
|
|
|
|
this.mXmppConnectionService = service;
|
|
|
|
}
|
|
|
|
|
|
|
|
public XmppConnectionService getXmppConnectionService() {
|
|
|
|
return this.mXmppConnectionService;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getAutoAcceptFileSize() {
|
2017-07-01 11:41:24 +00:00
|
|
|
return this.mXmppConnectionService.getLongPreference("auto_accept_file_size",R.integer.auto_accept_filesize);
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
2015-07-31 23:19:16 +00:00
|
|
|
|
2015-12-06 17:23:59 +00:00
|
|
|
public boolean hasStoragePermission() {
|
2016-12-02 10:25:14 +00:00
|
|
|
if (!Config.ONLY_INTERNAL_STORAGE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
2015-12-06 17:23:59 +00:00
|
|
|
return mXmppConnectionService.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-11 14:50:00 +00:00
|
|
|
public static Pair<InputStream,Integer> createInputStream(DownloadableFile file, boolean gcm) throws FileNotFoundException {
|
2015-07-31 23:19:16 +00:00
|
|
|
FileInputStream is;
|
|
|
|
int size;
|
2015-08-11 14:50:00 +00:00
|
|
|
is = new FileInputStream(file);
|
|
|
|
size = (int) file.getSize();
|
|
|
|
if (file.getKey() == null) {
|
|
|
|
return new Pair<InputStream,Integer>(is,size);
|
2015-07-31 23:19:16 +00:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
if (gcm) {
|
|
|
|
AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
|
|
|
|
cipher.init(true, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv()));
|
|
|
|
InputStream cis = new org.bouncycastle.crypto.io.CipherInputStream(is, cipher);
|
|
|
|
return new Pair<>(cis, cipher.getOutputSize(size));
|
|
|
|
} else {
|
|
|
|
IvParameterSpec ips = new IvParameterSpec(file.getIv());
|
|
|
|
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
|
|
|
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(file.getKey(), "AES"), ips);
|
|
|
|
Log.d(Config.LOGTAG, "opening encrypted input stream");
|
2015-08-10 10:55:37 +00:00
|
|
|
final int s = Config.REPORT_WRONG_FILESIZE_IN_OTR_JINGLE ? size : (size / 16 + 1) * 16;
|
|
|
|
return new Pair<InputStream,Integer>(new CipherInputStream(is, cipher),s);
|
2015-07-31 23:19:16 +00:00
|
|
|
}
|
|
|
|
} catch (InvalidKeyException e) {
|
|
|
|
return null;
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
return null;
|
|
|
|
} catch (NoSuchPaddingException e) {
|
|
|
|
return null;
|
|
|
|
} catch (InvalidAlgorithmParameterException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-11 19:33:41 +00:00
|
|
|
public static OutputStream createAppendedOutputStream(DownloadableFile file) {
|
|
|
|
return createOutputStream(file, false, true);
|
|
|
|
}
|
|
|
|
|
2015-07-31 23:19:16 +00:00
|
|
|
public static OutputStream createOutputStream(DownloadableFile file, boolean gcm) {
|
2015-12-11 19:33:41 +00:00
|
|
|
return createOutputStream(file, gcm, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static OutputStream createOutputStream(DownloadableFile file, boolean gcm, boolean append) {
|
2015-07-31 23:19:16 +00:00
|
|
|
FileOutputStream os;
|
|
|
|
try {
|
2015-12-11 19:33:41 +00:00
|
|
|
os = new FileOutputStream(file, append);
|
2015-07-31 23:19:16 +00:00
|
|
|
if (file.getKey() == null) {
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
if (gcm) {
|
|
|
|
AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
|
|
|
|
cipher.init(false, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv()));
|
|
|
|
return new org.bouncycastle.crypto.io.CipherOutputStream(os, cipher);
|
|
|
|
} else {
|
|
|
|
IvParameterSpec ips = new IvParameterSpec(file.getIv());
|
|
|
|
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
|
|
|
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(file.getKey(), "AES"), ips);
|
|
|
|
Log.d(Config.LOGTAG, "opening encrypted output stream");
|
|
|
|
return new CipherOutputStream(os, cipher);
|
|
|
|
}
|
|
|
|
} catch (InvalidKeyException e) {
|
|
|
|
return null;
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
return null;
|
|
|
|
} catch (NoSuchPaddingException e) {
|
|
|
|
return null;
|
|
|
|
} catch (InvalidAlgorithmParameterException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2015-08-10 17:48:36 +00:00
|
|
|
|
2017-01-12 15:02:09 +00:00
|
|
|
public void updateConversationUi(boolean force) {
|
|
|
|
synchronized (LAST_UI_UPDATE_CALL) {
|
|
|
|
if (force || SystemClock.elapsedRealtime() - LAST_UI_UPDATE_CALL.get() >= UI_REFRESH_THRESHOLD) {
|
|
|
|
LAST_UI_UPDATE_CALL.set(SystemClock.elapsedRealtime());
|
|
|
|
mXmppConnectionService.updateConversationUi();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-10 17:48:36 +00:00
|
|
|
public PowerManager.WakeLock createWakeLock(String name) {
|
|
|
|
PowerManager powerManager = (PowerManager) mXmppConnectionService.getSystemService(Context.POWER_SERVICE);
|
|
|
|
return powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,name);
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|