AbstractConnectionManager.java

  1package eu.siacs.conversations.services;
  2
  3import android.content.Context;
  4import android.os.PowerManager;
  5import android.util.Log;
  6import android.util.Pair;
  7
  8import org.bouncycastle.crypto.engines.AESEngine;
  9import org.bouncycastle.crypto.modes.AEADBlockCipher;
 10import org.bouncycastle.crypto.modes.GCMBlockCipher;
 11import org.bouncycastle.crypto.params.AEADParameters;
 12import org.bouncycastle.crypto.params.KeyParameter;
 13
 14import java.io.FileInputStream;
 15import java.io.FileNotFoundException;
 16import java.io.FileOutputStream;
 17import java.io.InputStream;
 18import java.io.OutputStream;
 19import java.security.InvalidAlgorithmParameterException;
 20import java.security.InvalidKeyException;
 21import java.security.NoSuchAlgorithmException;
 22
 23import javax.crypto.Cipher;
 24import javax.crypto.CipherInputStream;
 25import javax.crypto.CipherOutputStream;
 26import javax.crypto.NoSuchPaddingException;
 27import javax.crypto.spec.IvParameterSpec;
 28import javax.crypto.spec.SecretKeySpec;
 29
 30import eu.siacs.conversations.Config;
 31import eu.siacs.conversations.entities.DownloadableFile;
 32
 33public class AbstractConnectionManager {
 34	protected XmppConnectionService mXmppConnectionService;
 35
 36	public AbstractConnectionManager(XmppConnectionService service) {
 37		this.mXmppConnectionService = service;
 38	}
 39
 40	public XmppConnectionService getXmppConnectionService() {
 41		return this.mXmppConnectionService;
 42	}
 43
 44	public long getAutoAcceptFileSize() {
 45		String config = this.mXmppConnectionService.getPreferences().getString(
 46				"auto_accept_file_size", "524288");
 47		try {
 48			return Long.parseLong(config);
 49		} catch (NumberFormatException e) {
 50			return 524288;
 51		}
 52	}
 53
 54	public static Pair<InputStream,Integer> createInputStream(DownloadableFile file, boolean gcm) throws FileNotFoundException {
 55		FileInputStream is;
 56		int size;
 57		is = new FileInputStream(file);
 58		size = (int) file.getSize();
 59		if (file.getKey() == null) {
 60			return new Pair<InputStream,Integer>(is,size);
 61		}
 62		try {
 63			if (gcm) {
 64				AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
 65				cipher.init(true, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv()));
 66				InputStream cis = new org.bouncycastle.crypto.io.CipherInputStream(is, cipher);
 67				return new Pair<>(cis, cipher.getOutputSize(size));
 68			} else {
 69				IvParameterSpec ips = new IvParameterSpec(file.getIv());
 70				Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
 71				cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(file.getKey(), "AES"), ips);
 72				Log.d(Config.LOGTAG, "opening encrypted input stream");
 73				final int s = Config.REPORT_WRONG_FILESIZE_IN_OTR_JINGLE ? size : (size / 16 + 1) * 16;
 74				return new Pair<InputStream,Integer>(new CipherInputStream(is, cipher),s);
 75			}
 76		} catch (InvalidKeyException e) {
 77			return null;
 78		} catch (NoSuchAlgorithmException e) {
 79			return null;
 80		} catch (NoSuchPaddingException e) {
 81			return null;
 82		} catch (InvalidAlgorithmParameterException e) {
 83			return null;
 84		}
 85	}
 86
 87	public static OutputStream createOutputStream(DownloadableFile file, boolean gcm) {
 88		FileOutputStream os;
 89		try {
 90			os = new FileOutputStream(file);
 91			if (file.getKey() == null) {
 92				return os;
 93			}
 94		} catch (FileNotFoundException e) {
 95			return null;
 96		}
 97		try {
 98			if (gcm) {
 99				AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
100				cipher.init(false, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv()));
101				return new org.bouncycastle.crypto.io.CipherOutputStream(os, cipher);
102			} else {
103				IvParameterSpec ips = new IvParameterSpec(file.getIv());
104				Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
105				cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(file.getKey(), "AES"), ips);
106				Log.d(Config.LOGTAG, "opening encrypted output stream");
107				return new CipherOutputStream(os, cipher);
108			}
109		} catch (InvalidKeyException e) {
110			return null;
111		} catch (NoSuchAlgorithmException e) {
112			return null;
113		} catch (NoSuchPaddingException e) {
114			return null;
115		} catch (InvalidAlgorithmParameterException e) {
116			return null;
117		}
118	}
119
120	public PowerManager.WakeLock createWakeLock(String name) {
121		PowerManager powerManager = (PowerManager) mXmppConnectionService.getSystemService(Context.POWER_SERVICE);
122		return powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,name);
123	}
124}