1package eu.siacs.conversations;
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.utils.CryptoHelper;
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 return (this.expectedSize / 16 + 1) * 16;
46 } else {
47 return this.expectedSize;
48 }
49 }
50
51 public void setExpectedSize(long size) {
52 this.expectedSize = size;
53 }
54
55 public String getSha1Sum() {
56 return this.sha1sum;
57 }
58
59 public void setSha1Sum(String sum) {
60 this.sha1sum = sum;
61 }
62
63 public void setKey(byte[] key) {
64 if (key.length >= 32) {
65 byte[] secretKey = new byte[32];
66 System.arraycopy(key, 0, secretKey, 0, 32);
67 this.aeskey = new SecretKeySpec(secretKey, "AES");
68 } else if (key.length >= 16) {
69 byte[] secretKey = new byte[16];
70 System.arraycopy(key, 0, secretKey, 0, 16);
71 this.aeskey = new SecretKeySpec(secretKey, "AES");
72 } else {
73 Log.d(Config.LOGTAG, "weird key");
74 }
75 Log.d(Config.LOGTAG,
76 "using aes key "
77 + CryptoHelper.bytesToHex(this.aeskey.getEncoded()));
78 }
79
80 public Key getKey() {
81 return this.aeskey;
82 }
83
84 public InputStream createInputStream() {
85 if (this.getKey() == null) {
86 try {
87 return new FileInputStream(this);
88 } catch (FileNotFoundException e) {
89 return null;
90 }
91 } else {
92 try {
93 IvParameterSpec ips = new IvParameterSpec(iv);
94 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
95 cipher.init(Cipher.ENCRYPT_MODE, this.getKey(), ips);
96 Log.d(Config.LOGTAG, "opening encrypted input stream");
97 return new CipherInputStream(new FileInputStream(this), cipher);
98 } catch (NoSuchAlgorithmException e) {
99 Log.d(Config.LOGTAG, "no such algo: " + e.getMessage());
100 return null;
101 } catch (NoSuchPaddingException e) {
102 Log.d(Config.LOGTAG, "no such padding: " + e.getMessage());
103 return null;
104 } catch (InvalidKeyException e) {
105 Log.d(Config.LOGTAG, "invalid key: " + e.getMessage());
106 return null;
107 } catch (InvalidAlgorithmParameterException e) {
108 Log.d(Config.LOGTAG, "invavid iv:" + e.getMessage());
109 return null;
110 } catch (FileNotFoundException e) {
111 return null;
112 }
113 }
114 }
115
116 public OutputStream createOutputStream() {
117 if (this.getKey() == null) {
118 try {
119 return new FileOutputStream(this);
120 } catch (FileNotFoundException e) {
121 return null;
122 }
123 } else {
124 try {
125 IvParameterSpec ips = new IvParameterSpec(iv);
126 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
127 cipher.init(Cipher.DECRYPT_MODE, this.getKey(), ips);
128 Log.d(Config.LOGTAG, "opening encrypted output stream");
129 return new CipherOutputStream(new FileOutputStream(this),
130 cipher);
131 } catch (NoSuchAlgorithmException e) {
132 Log.d(Config.LOGTAG, "no such algo: " + e.getMessage());
133 return null;
134 } catch (NoSuchPaddingException e) {
135 Log.d(Config.LOGTAG, "no such padding: " + e.getMessage());
136 return null;
137 } catch (InvalidKeyException e) {
138 Log.d(Config.LOGTAG, "invalid key: " + e.getMessage());
139 return null;
140 } catch (InvalidAlgorithmParameterException e) {
141 Log.d(Config.LOGTAG, "invavid iv:" + e.getMessage());
142 return null;
143 } catch (FileNotFoundException e) {
144 return null;
145 }
146 }
147 }
148}