AbstractConnectionManager.java

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