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<>(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 return new Pair<>(new CipherInputStream(is, cipher),(size / 16 + 1) * 16);
85 }
86 } catch (Exception e) {
87 throw new AssertionError(e);
88 }
89 }
90
91 public static OutputStream createAppendedOutputStream(DownloadableFile file) {
92 return createOutputStream(file, false, true);
93 }
94
95 public static OutputStream createOutputStream(DownloadableFile file, boolean gcm) {
96 return createOutputStream(file, gcm, false);
97 }
98
99 private static OutputStream createOutputStream(DownloadableFile file, boolean gcm, boolean append) {
100 FileOutputStream os;
101 try {
102 os = new FileOutputStream(file, append);
103 if (file.getKey() == null) {
104 return os;
105 }
106 } catch (FileNotFoundException e) {
107 return null;
108 }
109 try {
110 if (gcm) {
111 AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine());
112 cipher.init(false, new AEADParameters(new KeyParameter(file.getKey()), 128, file.getIv()));
113 return new org.bouncycastle.crypto.io.CipherOutputStream(os, cipher);
114 } else {
115 IvParameterSpec ips = new IvParameterSpec(file.getIv());
116 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
117 cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(file.getKey(), "AES"), ips);
118 Log.d(Config.LOGTAG, "opening encrypted output stream");
119 return new CipherOutputStream(os, cipher);
120 }
121 } catch (InvalidKeyException e) {
122 return null;
123 } catch (NoSuchAlgorithmException e) {
124 return null;
125 } catch (NoSuchPaddingException e) {
126 return null;
127 } catch (InvalidAlgorithmParameterException e) {
128 return null;
129 }
130 }
131
132 public void updateConversationUi(boolean force) {
133 synchronized (LAST_UI_UPDATE_CALL) {
134 if (force || SystemClock.elapsedRealtime() - LAST_UI_UPDATE_CALL.get() >= UI_REFRESH_THRESHOLD) {
135 LAST_UI_UPDATE_CALL.set(SystemClock.elapsedRealtime());
136 mXmppConnectionService.updateConversationUi();
137 }
138 }
139 }
140
141 public PowerManager.WakeLock createWakeLock(String name) {
142 PowerManager powerManager = (PowerManager) mXmppConnectionService.getSystemService(Context.POWER_SERVICE);
143 return powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,name);
144 }
145}