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.util.Log;
9import android.util.Pair;
10
11import org.bouncycastle.crypto.engines.AESEngine;
12import org.bouncycastle.crypto.modes.AEADBlockCipher;
13import org.bouncycastle.crypto.modes.GCMBlockCipher;
14import org.bouncycastle.crypto.params.AEADParameters;
15import org.bouncycastle.crypto.params.KeyParameter;
16
17import java.io.FileInputStream;
18import java.io.FileNotFoundException;
19import java.io.FileOutputStream;
20import java.io.InputStream;
21import java.io.OutputStream;
22import java.security.InvalidAlgorithmParameterException;
23import java.security.InvalidKeyException;
24import java.security.NoSuchAlgorithmException;
25
26import javax.crypto.Cipher;
27import javax.crypto.CipherInputStream;
28import javax.crypto.CipherOutputStream;
29import javax.crypto.NoSuchPaddingException;
30import javax.crypto.spec.IvParameterSpec;
31import javax.crypto.spec.SecretKeySpec;
32
33import eu.siacs.conversations.Config;
34import eu.siacs.conversations.entities.DownloadableFile;
35
36public class AbstractConnectionManager {
37 protected XmppConnectionService mXmppConnectionService;
38
39 public AbstractConnectionManager(XmppConnectionService service) {
40 this.mXmppConnectionService = service;
41 }
42
43 public XmppConnectionService getXmppConnectionService() {
44 return this.mXmppConnectionService;
45 }
46
47 public long getAutoAcceptFileSize() {
48 String config = this.mXmppConnectionService.getPreferences().getString(
49 "auto_accept_file_size", "524288");
50 try {
51 return Long.parseLong(config);
52 } catch (NumberFormatException e) {
53 return 524288;
54 }
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 PowerManager.WakeLock createWakeLock(String name) {
140 PowerManager powerManager = (PowerManager) mXmppConnectionService.getSystemService(Context.POWER_SERVICE);
141 return powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,name);
142 }
143}