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 final int s = Config.REPORT_WRONG_FILESIZE_IN_OTR_JINGLE ? size : (size / 16 + 1) * 16;
76 return new Pair<InputStream,Integer>(new CipherInputStream(is, cipher),s);
77 }
78 } catch (InvalidKeyException e) {
79 return null;
80 } catch (NoSuchAlgorithmException e) {
81 return null;
82 } catch (NoSuchPaddingException e) {
83 return null;
84 } catch (InvalidAlgorithmParameterException e) {
85 return null;
86 }
87 }
88
89 public static OutputStream createOutputStream(DownloadableFile file, boolean gcm) {
90 FileOutputStream os;
91 try {
92 os = new FileOutputStream(file);
93 if (file.getKey() == null) {
94 return os;
95 }
96 } catch (FileNotFoundException e) {
97 return null;
98 }
99 try {
100 if (gcm) {
101 AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
102 cipher.init(false, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv()));
103 return new org.bouncycastle.crypto.io.CipherOutputStream(os, cipher);
104 } else {
105 IvParameterSpec ips = new IvParameterSpec(file.getIv());
106 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
107 cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(file.getKey(), "AES"), ips);
108 Log.d(Config.LOGTAG, "opening encrypted output stream");
109 return new CipherOutputStream(os, cipher);
110 }
111 } catch (InvalidKeyException e) {
112 return null;
113 } catch (NoSuchAlgorithmException e) {
114 return null;
115 } catch (NoSuchPaddingException e) {
116 return null;
117 } catch (InvalidAlgorithmParameterException e) {
118 return null;
119 }
120 }
121}