AbstractConnectionManager.java

  1package eu.siacs.conversations.services;
  2
  3import android.content.Context;
  4import android.os.PowerManager;
  5import android.os.SystemClock;
  6import android.util.Log;
  7
  8import org.bouncycastle.crypto.engines.AESEngine;
  9import org.bouncycastle.crypto.io.CipherInputStream;
 10import org.bouncycastle.crypto.io.CipherOutputStream;
 11import org.bouncycastle.crypto.modes.AEADBlockCipher;
 12import org.bouncycastle.crypto.modes.GCMBlockCipher;
 13import org.bouncycastle.crypto.params.AEADParameters;
 14import org.bouncycastle.crypto.params.KeyParameter;
 15
 16import java.io.FileNotFoundException;
 17import java.io.FileOutputStream;
 18import java.io.InputStream;
 19import java.io.OutputStream;
 20import java.security.InvalidAlgorithmParameterException;
 21import java.security.InvalidKeyException;
 22import java.security.NoSuchAlgorithmException;
 23import java.security.NoSuchProviderException;
 24import java.util.concurrent.atomic.AtomicLong;
 25
 26import javax.crypto.NoSuchPaddingException;
 27
 28import eu.siacs.conversations.Config;
 29import eu.siacs.conversations.R;
 30import eu.siacs.conversations.entities.DownloadableFile;
 31import eu.siacs.conversations.utils.Compatibility;
 32
 33import static eu.siacs.conversations.entities.Transferable.VALID_CRYPTO_EXTENSIONS;
 34
 35public class AbstractConnectionManager {
 36
 37    private static final int UI_REFRESH_THRESHOLD = 250;
 38    private static final AtomicLong LAST_UI_UPDATE_CALL = new AtomicLong(0);
 39    protected XmppConnectionService mXmppConnectionService;
 40
 41    public AbstractConnectionManager(XmppConnectionService service) {
 42        this.mXmppConnectionService = service;
 43    }
 44
 45    public static InputStream upgrade(DownloadableFile file, InputStream is) throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, NoSuchProviderException {
 46        if (file.getKey() != null && file.getIv() != null) {
 47            AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
 48            cipher.init(true, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv()));
 49            return new CipherInputStream(is, cipher);
 50        } else {
 51            return is;
 52        }
 53    }
 54
 55    public static OutputStream createOutputStream(DownloadableFile file, boolean append, boolean decrypt) {
 56        FileOutputStream os;
 57        try {
 58            os = new FileOutputStream(file, append);
 59            if (file.getKey() == null || !decrypt) {
 60                return os;
 61            }
 62        } catch (FileNotFoundException e) {
 63            Log.d(Config.LOGTAG, "unable to create output stream", e);
 64            return null;
 65        }
 66        try {
 67            AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
 68            cipher.init(false, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv()));
 69            return new CipherOutputStream(os, cipher);
 70        } catch (Exception e) {
 71            Log.d(Config.LOGTAG, "unable to create cipher output stream", e);
 72            return null;
 73        }
 74    }
 75
 76    public XmppConnectionService getXmppConnectionService() {
 77        return this.mXmppConnectionService;
 78    }
 79
 80    public long getAutoAcceptFileSize() {
 81        return this.mXmppConnectionService.getLongPreference("auto_accept_file_size", R.integer.auto_accept_filesize);
 82    }
 83
 84    public boolean hasStoragePermission() {
 85        return Compatibility.hasStoragePermission(mXmppConnectionService);
 86    }
 87
 88    public void updateConversationUi(boolean force) {
 89        synchronized (LAST_UI_UPDATE_CALL) {
 90            if (force || SystemClock.elapsedRealtime() - LAST_UI_UPDATE_CALL.get() >= UI_REFRESH_THRESHOLD) {
 91                LAST_UI_UPDATE_CALL.set(SystemClock.elapsedRealtime());
 92                mXmppConnectionService.updateConversationUi();
 93            }
 94        }
 95    }
 96
 97    public PowerManager.WakeLock createWakeLock(String name) {
 98        PowerManager powerManager = (PowerManager) mXmppConnectionService.getSystemService(Context.POWER_SERVICE);
 99        return powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, name);
100    }
101
102    public static class Extension {
103        public final String main;
104        public final String secondary;
105
106        private Extension(String main, String secondary) {
107            this.main = main;
108            this.secondary = secondary;
109        }
110
111        public String getExtension() {
112            if (VALID_CRYPTO_EXTENSIONS.contains(main)) {
113                return secondary;
114            } else {
115                return main;
116            }
117        }
118
119        public static Extension of(String path) {
120            final int pos = path.lastIndexOf('/');
121            final String filename = path.substring(pos + 1).toLowerCase();
122            final String[] parts = filename.split("\\.");
123            final String main = parts.length >= 2 ? parts[parts.length - 1] : null;
124            final String secondary = parts.length >= 3 ? parts[parts.length - 2] : null;
125            return new Extension(main, secondary);
126        }
127    }
128}