1package eu.siacs.conversations.entities;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileNotFoundException;
6import java.io.FileOutputStream;
7import java.io.InputStream;
8import java.io.OutputStream;
9import java.net.URLConnection;
10import java.security.InvalidAlgorithmParameterException;
11import java.security.InvalidKeyException;
12import java.security.Key;
13import java.security.NoSuchAlgorithmException;
14
15import javax.crypto.Cipher;
16import javax.crypto.CipherInputStream;
17import javax.crypto.CipherOutputStream;
18import javax.crypto.NoSuchPaddingException;
19import javax.crypto.spec.IvParameterSpec;
20import javax.crypto.spec.SecretKeySpec;
21
22import eu.siacs.conversations.Config;
23import android.util.Log;
24
25public class DownloadableFile extends File {
26
27 private static final long serialVersionUID = 2247012619505115863L;
28
29 private long expectedSize = 0;
30 private String sha1sum;
31 private Key aeskey;
32 private String mime;
33
34 private byte[] iv = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
35 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0xf };
36
37 public DownloadableFile(String path) {
38 super(path);
39 }
40
41 public long getSize() {
42 return super.length();
43 }
44
45 public long getExpectedSize() {
46 if (this.aeskey != null) {
47 if (this.expectedSize == 0) {
48 return 0;
49 } else {
50 return (this.expectedSize / 16 + 1) * 16;
51 }
52 } else {
53 return this.expectedSize;
54 }
55 }
56
57 public String getMimeType() {
58 String path = this.getAbsolutePath();
59 try {
60 String mime = URLConnection.guessContentTypeFromName(path.replace("#",""));
61 if (mime != null) {
62 return mime;
63 } else if (mime == null && path.endsWith(".webp")) {
64 return "image/webp";
65 } else {
66 return "";
67 }
68 } catch (final StringIndexOutOfBoundsException e) {
69 return "";
70 }
71 }
72
73 public void setExpectedSize(long size) {
74 this.expectedSize = size;
75 }
76
77 public String getSha1Sum() {
78 return this.sha1sum;
79 }
80
81 public void setSha1Sum(String sum) {
82 this.sha1sum = sum;
83 }
84
85 public void setKey(byte[] key) {
86 if (key.length == 48) {
87 byte[] secretKey = new byte[32];
88 byte[] iv = new byte[16];
89 System.arraycopy(key, 0, iv, 0, 16);
90 System.arraycopy(key, 16, secretKey, 0, 32);
91 this.aeskey = new SecretKeySpec(secretKey, "AES");
92 this.iv = iv;
93 } else if (key.length >= 32) {
94 byte[] secretKey = new byte[32];
95 System.arraycopy(key, 0, secretKey, 0, 32);
96 this.aeskey = new SecretKeySpec(secretKey, "AES");
97 } else if (key.length >= 16) {
98 byte[] secretKey = new byte[16];
99 System.arraycopy(key, 0, secretKey, 0, 16);
100 this.aeskey = new SecretKeySpec(secretKey, "AES");
101 }
102 }
103
104 public Key getKey() {
105 return this.aeskey;
106 }
107
108 public InputStream createInputStream() {
109 if (this.getKey() == null) {
110 try {
111 return new FileInputStream(this);
112 } catch (FileNotFoundException e) {
113 return null;
114 }
115 } else {
116 try {
117 IvParameterSpec ips = new IvParameterSpec(iv);
118 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
119 cipher.init(Cipher.ENCRYPT_MODE, this.getKey(), ips);
120 Log.d(Config.LOGTAG, "opening encrypted input stream");
121 return new CipherInputStream(new FileInputStream(this), cipher);
122 } catch (NoSuchAlgorithmException e) {
123 Log.d(Config.LOGTAG, "no such algo: " + e.getMessage());
124 return null;
125 } catch (NoSuchPaddingException e) {
126 Log.d(Config.LOGTAG, "no such padding: " + e.getMessage());
127 return null;
128 } catch (InvalidKeyException e) {
129 Log.d(Config.LOGTAG, "invalid key: " + e.getMessage());
130 return null;
131 } catch (InvalidAlgorithmParameterException e) {
132 Log.d(Config.LOGTAG, "invavid iv:" + e.getMessage());
133 return null;
134 } catch (FileNotFoundException e) {
135 return null;
136 }
137 }
138 }
139
140 public OutputStream createOutputStream() {
141 if (this.getKey() == null) {
142 try {
143 return new FileOutputStream(this);
144 } catch (FileNotFoundException e) {
145 return null;
146 }
147 } else {
148 try {
149 IvParameterSpec ips = new IvParameterSpec(this.iv);
150 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
151 cipher.init(Cipher.DECRYPT_MODE, this.getKey(), ips);
152 Log.d(Config.LOGTAG, "opening encrypted output stream");
153 return new CipherOutputStream(new FileOutputStream(this),
154 cipher);
155 } catch (NoSuchAlgorithmException e) {
156 Log.d(Config.LOGTAG, "no such algo: " + e.getMessage());
157 return null;
158 } catch (NoSuchPaddingException e) {
159 Log.d(Config.LOGTAG, "no such padding: " + e.getMessage());
160 return null;
161 } catch (InvalidKeyException e) {
162 Log.d(Config.LOGTAG, "invalid key: " + e.getMessage());
163 return null;
164 } catch (InvalidAlgorithmParameterException e) {
165 Log.d(Config.LOGTAG, "invavid iv:" + e.getMessage());
166 return null;
167 } catch (FileNotFoundException e) {
168 return null;
169 }
170 }
171 }
172}