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