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.Pair;
10
11import java.io.FileInputStream;
12import java.io.FileNotFoundException;
13import java.io.FileOutputStream;
14import java.io.InputStream;
15import java.io.OutputStream;
16import java.util.concurrent.atomic.AtomicLong;
17
18import javax.crypto.Cipher;
19import javax.crypto.CipherInputStream;
20import javax.crypto.CipherOutputStream;
21import javax.crypto.spec.IvParameterSpec;
22import javax.crypto.spec.SecretKeySpec;
23
24import eu.siacs.conversations.Config;
25import eu.siacs.conversations.R;
26import eu.siacs.conversations.entities.DownloadableFile;
27import eu.siacs.conversations.utils.Compatibility;
28
29public class AbstractConnectionManager {
30
31 private static final String KEYTYPE = "AES";
32 private static final String CIPHERMODE = "AES/GCM/NoPadding";
33 private static final String PROVIDER = "BC";
34 private static final int UI_REFRESH_THRESHOLD = 250;
35 private static final AtomicLong LAST_UI_UPDATE_CALL = new AtomicLong(0);
36 protected XmppConnectionService mXmppConnectionService;
37
38 public AbstractConnectionManager(XmppConnectionService service) {
39 this.mXmppConnectionService = service;
40 }
41
42 public static Pair<InputStream, Integer> createInputStream(DownloadableFile file, boolean gcm) throws FileNotFoundException {
43 FileInputStream is;
44 int size;
45 is = new FileInputStream(file);
46 size = (int) file.getSize();
47 if (file.getKey() == null) {
48 return new Pair<>(is, size);
49 }
50 try {
51 if (gcm) {
52 Cipher cipher = Compatibility.twentyTwo() ? Cipher.getInstance(CIPHERMODE) : Cipher.getInstance(CIPHERMODE, PROVIDER);
53 SecretKeySpec keySpec = new SecretKeySpec(file.getKey(), KEYTYPE);
54 IvParameterSpec ivSpec = new IvParameterSpec(file.getIv());
55 cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
56 return new Pair<>(new CipherInputStream(is, cipher), cipher.getOutputSize(size));
57 } else {
58 IvParameterSpec ips = new IvParameterSpec(file.getIv());
59 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
60 cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(file.getKey(), KEYTYPE), ips);
61 return new Pair<>(new CipherInputStream(is, cipher), (size / 16 + 1) * 16);
62 }
63 } catch (Exception e) {
64 throw new AssertionError(e);
65 }
66 }
67
68 public static OutputStream createAppendedOutputStream(DownloadableFile file) {
69 return createOutputStream(file, false, true);
70 }
71
72 public static OutputStream createOutputStream(DownloadableFile file, boolean gcm) {
73 return createOutputStream(file, gcm, false);
74 }
75
76 private static OutputStream createOutputStream(DownloadableFile file, boolean gcm, boolean append) {
77 FileOutputStream os;
78 try {
79 os = new FileOutputStream(file, append);
80 if (file.getKey() == null) {
81 return os;
82 }
83 } catch (FileNotFoundException e) {
84 return null;
85 }
86 try {
87 if (gcm) {
88 Cipher cipher = Compatibility.twentyTwo() ? Cipher.getInstance(CIPHERMODE) : Cipher.getInstance(CIPHERMODE, PROVIDER);
89 SecretKeySpec keySpec = new SecretKeySpec(file.getKey(), KEYTYPE);
90 IvParameterSpec ivSpec = new IvParameterSpec(file.getIv());
91 cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
92 return new CipherOutputStream(os, cipher);
93 } else {
94 IvParameterSpec ips = new IvParameterSpec(file.getIv());
95 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
96 cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(file.getKey(), KEYTYPE), ips);
97 return new CipherOutputStream(os, cipher);
98 }
99 } catch (Exception e) {
100 throw new AssertionError(e);
101 }
102 }
103
104 public XmppConnectionService getXmppConnectionService() {
105 return this.mXmppConnectionService;
106 }
107
108 public long getAutoAcceptFileSize() {
109 return this.mXmppConnectionService.getLongPreference("auto_accept_file_size", R.integer.auto_accept_filesize);
110 }
111
112 public boolean hasStoragePermission() {
113 if (!Config.ONLY_INTERNAL_STORAGE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
114 return mXmppConnectionService.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
115 } else {
116 return true;
117 }
118 }
119
120 public void updateConversationUi(boolean force) {
121 synchronized (LAST_UI_UPDATE_CALL) {
122 if (force || SystemClock.elapsedRealtime() - LAST_UI_UPDATE_CALL.get() >= UI_REFRESH_THRESHOLD) {
123 LAST_UI_UPDATE_CALL.set(SystemClock.elapsedRealtime());
124 mXmppConnectionService.updateConversationUi();
125 }
126 }
127 }
128
129 public PowerManager.WakeLock createWakeLock(String name) {
130 PowerManager powerManager = (PowerManager) mXmppConnectionService.getSystemService(Context.POWER_SERVICE);
131 return powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, name);
132 }
133}