DownloadableFile.java

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