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 String mime = URLConnection.guessContentTypeFromName(path);
60 if (mime != null) {
61 return mime;
62 } else if (mime == null && path.endsWith(".webp")) {
63 return "image/webp";
64 } else {
65 return "";
66 }
67 }
68
69 public void setExpectedSize(long size) {
70 this.expectedSize = size;
71 }
72
73 public String getSha1Sum() {
74 return this.sha1sum;
75 }
76
77 public void setSha1Sum(String sum) {
78 this.sha1sum = sum;
79 }
80
81 public void setKey(byte[] key) {
82 if (key.length == 48) {
83 byte[] secretKey = new byte[32];
84 byte[] iv = new byte[16];
85 System.arraycopy(key, 0, iv, 0, 16);
86 System.arraycopy(key, 16, secretKey, 0, 32);
87 this.aeskey = new SecretKeySpec(secretKey, "AES");
88 this.iv = iv;
89 } else if (key.length >= 32) {
90 byte[] secretKey = new byte[32];
91 System.arraycopy(key, 0, secretKey, 0, 32);
92 this.aeskey = new SecretKeySpec(secretKey, "AES");
93 } else if (key.length >= 16) {
94 byte[] secretKey = new byte[16];
95 System.arraycopy(key, 0, secretKey, 0, 16);
96 this.aeskey = new SecretKeySpec(secretKey, "AES");
97 }
98 }
99
100 public Key getKey() {
101 return this.aeskey;
102 }
103
104 public InputStream createInputStream() {
105 if (this.getKey() == null) {
106 try {
107 return new FileInputStream(this);
108 } catch (FileNotFoundException e) {
109 return null;
110 }
111 } else {
112 try {
113 IvParameterSpec ips = new IvParameterSpec(iv);
114 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
115 cipher.init(Cipher.ENCRYPT_MODE, this.getKey(), ips);
116 Log.d(Config.LOGTAG, "opening encrypted input stream");
117 return new CipherInputStream(new FileInputStream(this), cipher);
118 } catch (NoSuchAlgorithmException e) {
119 Log.d(Config.LOGTAG, "no such algo: " + e.getMessage());
120 return null;
121 } catch (NoSuchPaddingException e) {
122 Log.d(Config.LOGTAG, "no such padding: " + e.getMessage());
123 return null;
124 } catch (InvalidKeyException e) {
125 Log.d(Config.LOGTAG, "invalid key: " + e.getMessage());
126 return null;
127 } catch (InvalidAlgorithmParameterException e) {
128 Log.d(Config.LOGTAG, "invavid iv:" + e.getMessage());
129 return null;
130 } catch (FileNotFoundException e) {
131 return null;
132 }
133 }
134 }
135
136 public OutputStream createOutputStream() {
137 if (this.getKey() == null) {
138 try {
139 return new FileOutputStream(this);
140 } catch (FileNotFoundException e) {
141 return null;
142 }
143 } else {
144 try {
145 IvParameterSpec ips = new IvParameterSpec(this.iv);
146 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
147 cipher.init(Cipher.DECRYPT_MODE, this.getKey(), ips);
148 Log.d(Config.LOGTAG, "opening encrypted output stream");
149 return new CipherOutputStream(new FileOutputStream(this),
150 cipher);
151 } catch (NoSuchAlgorithmException e) {
152 Log.d(Config.LOGTAG, "no such algo: " + e.getMessage());
153 return null;
154 } catch (NoSuchPaddingException e) {
155 Log.d(Config.LOGTAG, "no such padding: " + e.getMessage());
156 return null;
157 } catch (InvalidKeyException e) {
158 Log.d(Config.LOGTAG, "invalid key: " + e.getMessage());
159 return null;
160 } catch (InvalidAlgorithmParameterException e) {
161 Log.d(Config.LOGTAG, "invavid iv:" + e.getMessage());
162 return null;
163 } catch (FileNotFoundException e) {
164 return null;
165 }
166 }
167 }
168}