AbstractConnectionManager.java

  1package eu.siacs.conversations.services;
  2
  3import android.Manifest;
  4import android.content.Context;
  5import android.content.pm.PackageManager;
  6import android.os.Build;
  7import android.os.PowerManager;
  8import android.os.SystemClock;
  9import android.util.Log;
 10import android.util.Pair;
 11
 12import org.bouncycastle.crypto.engines.AESEngine;
 13import org.bouncycastle.crypto.modes.AEADBlockCipher;
 14import org.bouncycastle.crypto.modes.GCMBlockCipher;
 15import org.bouncycastle.crypto.params.AEADParameters;
 16import org.bouncycastle.crypto.params.KeyParameter;
 17
 18import java.io.FileInputStream;
 19import java.io.FileNotFoundException;
 20import java.io.FileOutputStream;
 21import java.io.InputStream;
 22import java.io.OutputStream;
 23import java.security.InvalidAlgorithmParameterException;
 24import java.security.InvalidKeyException;
 25import java.security.NoSuchAlgorithmException;
 26import java.util.concurrent.atomic.AtomicLong;
 27
 28import javax.crypto.Cipher;
 29import javax.crypto.CipherInputStream;
 30import javax.crypto.CipherOutputStream;
 31import javax.crypto.NoSuchPaddingException;
 32import javax.crypto.spec.IvParameterSpec;
 33import javax.crypto.spec.SecretKeySpec;
 34
 35import eu.siacs.conversations.Config;
 36import eu.siacs.conversations.R;
 37import eu.siacs.conversations.entities.DownloadableFile;
 38
 39public class AbstractConnectionManager {
 40	protected XmppConnectionService mXmppConnectionService;
 41
 42	private static final int UI_REFRESH_THRESHOLD = 250;
 43	private static final AtomicLong LAST_UI_UPDATE_CALL = new AtomicLong(0);
 44
 45	public AbstractConnectionManager(XmppConnectionService service) {
 46		this.mXmppConnectionService = service;
 47	}
 48
 49	public XmppConnectionService getXmppConnectionService() {
 50		return this.mXmppConnectionService;
 51	}
 52
 53	public long getAutoAcceptFileSize() {
 54		return this.mXmppConnectionService.getLongPreference("auto_accept_file_size",R.integer.auto_accept_filesize);
 55	}
 56
 57	public boolean hasStoragePermission() {
 58		if (!Config.ONLY_INTERNAL_STORAGE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
 59			return mXmppConnectionService.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
 60		} else {
 61			return true;
 62		}
 63	}
 64
 65	public static Pair<InputStream,Integer> createInputStream(DownloadableFile file, boolean gcm) throws FileNotFoundException {
 66		FileInputStream is;
 67		int size;
 68		is = new FileInputStream(file);
 69		size = (int) file.getSize();
 70		if (file.getKey() == null) {
 71			return new Pair<InputStream,Integer>(is,size);
 72		}
 73		try {
 74			if (gcm) {
 75				AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
 76				cipher.init(true, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv()));
 77				InputStream cis = new org.bouncycastle.crypto.io.CipherInputStream(is, cipher);
 78				return new Pair<>(cis, cipher.getOutputSize(size));
 79			} else {
 80				IvParameterSpec ips = new IvParameterSpec(file.getIv());
 81				Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
 82				cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(file.getKey(), "AES"), ips);
 83				Log.d(Config.LOGTAG, "opening encrypted input stream");
 84				return new Pair<InputStream,Integer>(new CipherInputStream(is, cipher),(size / 16 + 1) * 16);
 85			}
 86		} catch (InvalidKeyException e) {
 87			return null;
 88		} catch (NoSuchAlgorithmException e) {
 89			return null;
 90		} catch (NoSuchPaddingException e) {
 91			return null;
 92		} catch (InvalidAlgorithmParameterException e) {
 93			return null;
 94		}
 95	}
 96
 97	public static OutputStream createAppendedOutputStream(DownloadableFile file) {
 98		return createOutputStream(file, false, true);
 99	}
100
101	public static OutputStream createOutputStream(DownloadableFile file, boolean gcm) {
102		return createOutputStream(file, gcm, false);
103	}
104
105	private static OutputStream createOutputStream(DownloadableFile file, boolean gcm, boolean append) {
106		FileOutputStream os;
107		try {
108			os = new FileOutputStream(file, append);
109			if (file.getKey() == null) {
110				return os;
111			}
112		} catch (FileNotFoundException e) {
113			return null;
114		}
115		try {
116			if (gcm) {
117				AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
118				cipher.init(false, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv()));
119				return new org.bouncycastle.crypto.io.CipherOutputStream(os, cipher);
120			} else {
121				IvParameterSpec ips = new IvParameterSpec(file.getIv());
122				Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
123				cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(file.getKey(), "AES"), ips);
124				Log.d(Config.LOGTAG, "opening encrypted output stream");
125				return new CipherOutputStream(os, cipher);
126			}
127		} catch (InvalidKeyException e) {
128			return null;
129		} catch (NoSuchAlgorithmException e) {
130			return null;
131		} catch (NoSuchPaddingException e) {
132			return null;
133		} catch (InvalidAlgorithmParameterException e) {
134			return null;
135		}
136	}
137
138	public void updateConversationUi(boolean force) {
139		synchronized (LAST_UI_UPDATE_CALL) {
140			if (force || SystemClock.elapsedRealtime() - LAST_UI_UPDATE_CALL.get() >= UI_REFRESH_THRESHOLD) {
141				LAST_UI_UPDATE_CALL.set(SystemClock.elapsedRealtime());
142				mXmppConnectionService.updateConversationUi();
143			}
144		}
145	}
146
147	public PowerManager.WakeLock createWakeLock(String name) {
148		PowerManager powerManager = (PowerManager) mXmppConnectionService.getSystemService(Context.POWER_SERVICE);
149		return powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,name);
150	}
151}