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