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