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.entities.DownloadableFile;
 37
 38public class AbstractConnectionManager {
 39	protected XmppConnectionService mXmppConnectionService;
 40
 41	private static final int UI_REFRESH_THRESHOLD = 250;
 42	private static final AtomicLong LAST_UI_UPDATE_CALL = new AtomicLong(0);
 43
 44	public AbstractConnectionManager(XmppConnectionService service) {
 45		this.mXmppConnectionService = service;
 46	}
 47
 48	public XmppConnectionService getXmppConnectionService() {
 49		return this.mXmppConnectionService;
 50	}
 51
 52	public long getAutoAcceptFileSize() {
 53		String config = this.mXmppConnectionService.getPreferences().getString(
 54				"auto_accept_file_size", "524288");
 55		try {
 56			return Long.parseLong(config);
 57		} catch (NumberFormatException e) {
 58			return 524288;
 59		}
 60	}
 61
 62	public boolean hasStoragePermission() {
 63		if (!Config.ONLY_INTERNAL_STORAGE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
 64			return mXmppConnectionService.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
 65		} else {
 66			return true;
 67		}
 68	}
 69
 70	public static Pair<InputStream,Integer> createInputStream(DownloadableFile file, boolean gcm) throws FileNotFoundException {
 71		FileInputStream is;
 72		int size;
 73		is = new FileInputStream(file);
 74		size = (int) file.getSize();
 75		if (file.getKey() == null) {
 76			return new Pair<InputStream,Integer>(is,size);
 77		}
 78		try {
 79			if (gcm) {
 80				AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
 81				cipher.init(true, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv()));
 82				InputStream cis = new org.bouncycastle.crypto.io.CipherInputStream(is, cipher);
 83				return new Pair<>(cis, cipher.getOutputSize(size));
 84			} else {
 85				IvParameterSpec ips = new IvParameterSpec(file.getIv());
 86				Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
 87				cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(file.getKey(), "AES"), ips);
 88				Log.d(Config.LOGTAG, "opening encrypted input stream");
 89				final int s = Config.REPORT_WRONG_FILESIZE_IN_OTR_JINGLE ? size : (size / 16 + 1) * 16;
 90				return new Pair<InputStream,Integer>(new CipherInputStream(is, cipher),s);
 91			}
 92		} catch (InvalidKeyException e) {
 93			return null;
 94		} catch (NoSuchAlgorithmException e) {
 95			return null;
 96		} catch (NoSuchPaddingException e) {
 97			return null;
 98		} catch (InvalidAlgorithmParameterException e) {
 99			return null;
100		}
101	}
102
103	public static OutputStream createAppendedOutputStream(DownloadableFile file) {
104		return createOutputStream(file, false, true);
105	}
106
107	public static OutputStream createOutputStream(DownloadableFile file, boolean gcm) {
108		return createOutputStream(file, gcm, false);
109	}
110
111	private static OutputStream createOutputStream(DownloadableFile file, boolean gcm, boolean append) {
112		FileOutputStream os;
113		try {
114			os = new FileOutputStream(file, append);
115			if (file.getKey() == null) {
116				return os;
117			}
118		} catch (FileNotFoundException e) {
119			return null;
120		}
121		try {
122			if (gcm) {
123				AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
124				cipher.init(false, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv()));
125				return new org.bouncycastle.crypto.io.CipherOutputStream(os, cipher);
126			} else {
127				IvParameterSpec ips = new IvParameterSpec(file.getIv());
128				Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
129				cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(file.getKey(), "AES"), ips);
130				Log.d(Config.LOGTAG, "opening encrypted output stream");
131				return new CipherOutputStream(os, cipher);
132			}
133		} catch (InvalidKeyException e) {
134			return null;
135		} catch (NoSuchAlgorithmException e) {
136			return null;
137		} catch (NoSuchPaddingException e) {
138			return null;
139		} catch (InvalidAlgorithmParameterException e) {
140			return null;
141		}
142	}
143
144	public void updateConversationUi(boolean force) {
145		synchronized (LAST_UI_UPDATE_CALL) {
146			if (force || SystemClock.elapsedRealtime() - LAST_UI_UPDATE_CALL.get() >= UI_REFRESH_THRESHOLD) {
147				LAST_UI_UPDATE_CALL.set(SystemClock.elapsedRealtime());
148				mXmppConnectionService.updateConversationUi();
149			}
150		}
151	}
152
153	public PowerManager.WakeLock createWakeLock(String name) {
154		PowerManager powerManager = (PowerManager) mXmppConnectionService.getSystemService(Context.POWER_SERVICE);
155		return powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,name);
156	}
157}