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