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