DownloadableFile.java

  1package eu.siacs.conversations.entities;
  2
  3import android.util.Log;
  4
  5import java.io.File;
  6import java.io.FileInputStream;
  7import java.io.FileNotFoundException;
  8import java.io.FileOutputStream;
  9import java.io.InputStream;
 10import java.io.OutputStream;
 11import java.security.InvalidAlgorithmParameterException;
 12import java.security.InvalidKeyException;
 13import java.security.Key;
 14import java.security.NoSuchAlgorithmException;
 15
 16import javax.crypto.Cipher;
 17import javax.crypto.CipherInputStream;
 18import javax.crypto.CipherOutputStream;
 19import javax.crypto.NoSuchPaddingException;
 20import javax.crypto.spec.IvParameterSpec;
 21import javax.crypto.spec.SecretKeySpec;
 22
 23import eu.siacs.conversations.Config;
 24import eu.siacs.conversations.utils.MimeUtils;
 25
 26public class DownloadableFile extends File {
 27
 28	private static final long serialVersionUID = 2247012619505115863L;
 29
 30	private long expectedSize = 0;
 31	private String sha1sum;
 32	private Key aeskey;
 33	private String mime;
 34
 35	private byte[] iv = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
 36			0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0xf };
 37
 38	public DownloadableFile(String path) {
 39		super(path);
 40	}
 41
 42	public long getSize() {
 43		return super.length();
 44	}
 45
 46	public long getExpectedSize() {
 47		if (this.aeskey != null) {
 48			if (this.expectedSize == 0) {
 49				return 0;
 50			} else {
 51				return (this.expectedSize / 16 + 1) * 16;
 52			}
 53		} else {
 54			return this.expectedSize;
 55		}
 56	}
 57
 58	public String getMimeType() {
 59		String path = this.getAbsolutePath();
 60		int start = path.lastIndexOf('.') + 1;
 61		if (start < path.length()) {
 62			String mime = MimeUtils.guessMimeTypeFromExtension(path.substring(start));
 63			return mime == null ? "" : mime;
 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}