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) {
 55		FileInputStream is;
 56		int size;
 57		try {
 58			is = new FileInputStream(file);
 59			size = (int) file.getSize();
 60			if (file.getKey() == null) {
 61				return new Pair<InputStream,Integer>(is,size);
 62			}
 63		} catch (FileNotFoundException e) {
 64			return null;
 65		}
 66		try {
 67			if (gcm) {
 68				AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
 69				cipher.init(true, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv()));
 70				InputStream cis = new org.bouncycastle.crypto.io.CipherInputStream(is, cipher);
 71				return new Pair<>(cis, cipher.getOutputSize(size));
 72			} else {
 73				IvParameterSpec ips = new IvParameterSpec(file.getIv());
 74				Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
 75				cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(file.getKey(), "AES"), ips);
 76				Log.d(Config.LOGTAG, "opening encrypted input stream");
 77				final int s = Config.REPORT_WRONG_FILESIZE_IN_OTR_JINGLE ? size : (size / 16 + 1) * 16;
 78				return new Pair<InputStream,Integer>(new CipherInputStream(is, cipher),s);
 79			}
 80		} catch (InvalidKeyException e) {
 81			return null;
 82		} catch (NoSuchAlgorithmException e) {
 83			return null;
 84		} catch (NoSuchPaddingException e) {
 85			return null;
 86		} catch (InvalidAlgorithmParameterException e) {
 87			return null;
 88		}
 89	}
 90
 91	public static OutputStream createOutputStream(DownloadableFile file, boolean gcm) {
 92		FileOutputStream os;
 93		try {
 94			os = new FileOutputStream(file);
 95			if (file.getKey() == null) {
 96				return os;
 97			}
 98		} catch (FileNotFoundException e) {
 99			return null;
100		}
101		try {
102			if (gcm) {
103				AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
104				cipher.init(false, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv()));
105				return new org.bouncycastle.crypto.io.CipherOutputStream(os, cipher);
106			} else {
107				IvParameterSpec ips = new IvParameterSpec(file.getIv());
108				Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
109				cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(file.getKey(), "AES"), ips);
110				Log.d(Config.LOGTAG, "opening encrypted output stream");
111				return new CipherOutputStream(os, cipher);
112			}
113		} catch (InvalidKeyException e) {
114			return null;
115		} catch (NoSuchAlgorithmException e) {
116			return null;
117		} catch (NoSuchPaddingException e) {
118			return null;
119		} catch (InvalidAlgorithmParameterException e) {
120			return null;
121		}
122	}
123
124	public PowerManager.WakeLock createWakeLock(String name) {
125		PowerManager powerManager = (PowerManager) mXmppConnectionService.getSystemService(Context.POWER_SERVICE);
126		return powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,name);
127	}
128}