2014-10-22 16:38:44 +00:00
|
|
|
package eu.siacs.conversations.services;
|
|
|
|
|
2015-08-10 17:48:36 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.os.PowerManager;
|
2017-01-12 15:02:09 +00:00
|
|
|
import android.os.SystemClock;
|
2018-10-08 08:31:41 +00:00
|
|
|
import android.util.Log;
|
2015-07-31 23:19:16 +00:00
|
|
|
|
2020-03-09 18:12:30 +00:00
|
|
|
import org.bouncycastle.crypto.engines.AESEngine;
|
|
|
|
import org.bouncycastle.crypto.io.CipherInputStream;
|
|
|
|
import org.bouncycastle.crypto.io.CipherOutputStream;
|
|
|
|
import org.bouncycastle.crypto.modes.AEADBlockCipher;
|
|
|
|
import org.bouncycastle.crypto.modes.GCMBlockCipher;
|
|
|
|
import org.bouncycastle.crypto.params.AEADParameters;
|
|
|
|
import org.bouncycastle.crypto.params.KeyParameter;
|
|
|
|
|
2015-07-31 23:19:16 +00:00
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2018-10-03 16:14:41 +00:00
|
|
|
import java.security.InvalidAlgorithmParameterException;
|
|
|
|
import java.security.InvalidKeyException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.security.NoSuchProviderException;
|
2017-01-12 15:02:09 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicLong;
|
2015-07-31 23:19:16 +00:00
|
|
|
|
2018-10-03 16:14:41 +00:00
|
|
|
import javax.crypto.NoSuchPaddingException;
|
2015-07-31 23:19:16 +00:00
|
|
|
|
|
|
|
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;
|
2018-09-17 15:47:51 +00:00
|
|
|
import eu.siacs.conversations.utils.Compatibility;
|
2015-07-31 23:19:16 +00:00
|
|
|
|
2020-12-06 18:22:36 +00:00
|
|
|
import static eu.siacs.conversations.entities.Transferable.VALID_CRYPTO_EXTENSIONS;
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
public class AbstractConnectionManager {
|
2018-09-17 15:47:51 +00:00
|
|
|
|
|
|
|
private static final int UI_REFRESH_THRESHOLD = 250;
|
|
|
|
private static final AtomicLong LAST_UI_UPDATE_CALL = new AtomicLong(0);
|
|
|
|
protected XmppConnectionService mXmppConnectionService;
|
|
|
|
|
|
|
|
public AbstractConnectionManager(XmppConnectionService service) {
|
|
|
|
this.mXmppConnectionService = service;
|
|
|
|
}
|
|
|
|
|
2018-10-03 16:14:41 +00:00
|
|
|
public static InputStream upgrade(DownloadableFile file, InputStream is) throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, NoSuchProviderException {
|
|
|
|
if (file.getKey() != null && file.getIv() != null) {
|
2020-03-09 18:12:30 +00:00
|
|
|
AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
|
|
|
|
cipher.init(true, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv()));
|
2018-10-03 16:14:41 +00:00
|
|
|
return new CipherInputStream(is, cipher);
|
|
|
|
} else {
|
|
|
|
return is;
|
2018-09-17 15:47:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 18:07:31 +00:00
|
|
|
public static OutputStream createOutputStream(DownloadableFile file, boolean append, boolean decrypt) {
|
2018-09-17 15:47:51 +00:00
|
|
|
FileOutputStream os;
|
|
|
|
try {
|
|
|
|
os = new FileOutputStream(file, append);
|
2019-12-20 18:07:31 +00:00
|
|
|
if (file.getKey() == null || !decrypt) {
|
2018-09-17 15:47:51 +00:00
|
|
|
return os;
|
|
|
|
}
|
|
|
|
} catch (FileNotFoundException e) {
|
2020-03-09 18:12:30 +00:00
|
|
|
Log.d(Config.LOGTAG, "unable to create output stream", e);
|
2018-09-17 15:47:51 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
try {
|
2020-03-09 18:12:30 +00:00
|
|
|
AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
|
|
|
|
cipher.init(false, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv()));
|
2018-09-30 11:48:11 +00:00
|
|
|
return new CipherOutputStream(os, cipher);
|
2018-09-17 15:47:51 +00:00
|
|
|
} catch (Exception e) {
|
2020-03-09 18:12:30 +00:00
|
|
|
Log.d(Config.LOGTAG, "unable to create cipher output stream", e);
|
2018-09-30 11:48:11 +00:00
|
|
|
return null;
|
2018-09-17 15:47:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2018-09-17 19:24:25 +00:00
|
|
|
return Compatibility.hasStoragePermission(mXmppConnectionService);
|
2018-09-17 15:47:51 +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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 08:24:41 +00:00
|
|
|
public PowerManager.WakeLock createWakeLock(final Thread thread) {
|
|
|
|
return createWakeLock("conversations:" + thread.getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
public PowerManager.WakeLock createWakeLock(final String name) {
|
|
|
|
final PowerManager powerManager = (PowerManager) mXmppConnectionService.getSystemService(Context.POWER_SERVICE);
|
2018-09-17 15:47:51 +00:00
|
|
|
return powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, name);
|
|
|
|
}
|
2019-08-16 12:09:42 +00:00
|
|
|
|
|
|
|
public static class Extension {
|
|
|
|
public final String main;
|
|
|
|
public final String secondary;
|
|
|
|
|
|
|
|
private Extension(String main, String secondary) {
|
|
|
|
this.main = main;
|
|
|
|
this.secondary = secondary;
|
|
|
|
}
|
|
|
|
|
2020-12-06 18:22:36 +00:00
|
|
|
public String getExtension() {
|
|
|
|
if (VALID_CRYPTO_EXTENSIONS.contains(main)) {
|
|
|
|
return secondary;
|
|
|
|
} else {
|
|
|
|
return main;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-16 12:09:42 +00:00
|
|
|
public static Extension of(String path) {
|
|
|
|
final int pos = path.lastIndexOf('/');
|
|
|
|
final String filename = path.substring(pos + 1).toLowerCase();
|
|
|
|
final String[] parts = filename.split("\\.");
|
|
|
|
final String main = parts.length >= 2 ? parts[parts.length - 1] : null;
|
|
|
|
final String secondary = parts.length >= 3 ? parts[parts.length - 2] : null;
|
|
|
|
return new Extension(main, secondary);
|
|
|
|
}
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|