aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/eu/siacs/conversations/entities/DownloadableFile.java
blob: fdda6d0e086afa144e03ed02a0e39a8b56ed8754 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package eu.siacs.conversations.entities;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLConnection;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import eu.siacs.conversations.Config;
import eu.siacs.conversations.utils.MimeUtils;

import de.thedevstack.conversationsplus.utils.Logging;

public class DownloadableFile extends File {

	private static final long serialVersionUID = 2247012619505115863L;

	private long expectedSize = 0;
	private String sha1sum;
	private Key aeskey;
	private String mime;

	private byte[] iv = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
			0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0xf };

	public DownloadableFile(String path) {
		super(path);
	}

	public long getSize() {
		return super.length();
	}

	public long getExpectedSize() {
		if (this.aeskey != null) {
			if (this.expectedSize == 0) {
				return 0;
			} else {
				return (this.expectedSize / 16 + 1) * 16;
			}
		} else {
			return this.expectedSize;
		}
	}

	public String getMimeType() {
		String path = this.getAbsolutePath();
		int start = path.lastIndexOf('.') + 1;
		if (start < path.length()) {
			String mime = MimeUtils.guessMimeTypeFromExtension(path.substring(start));
			return mime == null ? "" : mime;
		} else {
			return "";
		}
	}

	public void setExpectedSize(long size) {
		this.expectedSize = size;
	}

	public String getSha1Sum() {
		return this.sha1sum;
	}

	public void setSha1Sum(String sum) {
		this.sha1sum = sum;
	}

	public void setKey(byte[] key) {
		if (key.length == 48) {
			byte[] secretKey = new byte[32];
			byte[] iv = new byte[16];
			System.arraycopy(key, 0, iv, 0, 16);
			System.arraycopy(key, 16, secretKey, 0, 32);
			this.aeskey = new SecretKeySpec(secretKey, "AES");
			this.iv = iv;
		} else if (key.length >= 32) {
			byte[] secretKey = new byte[32];
			System.arraycopy(key, 0, secretKey, 0, 32);
			this.aeskey = new SecretKeySpec(secretKey, "AES");
		} else if (key.length >= 16) {
			byte[] secretKey = new byte[16];
			System.arraycopy(key, 0, secretKey, 0, 16);
			this.aeskey = new SecretKeySpec(secretKey, "AES");
		}
	}

	public Key getKey() {
		return this.aeskey;
	}

	public InputStream createInputStream() {
		if (this.getKey() == null) {
			try {
				return new FileInputStream(this);
			} catch (FileNotFoundException e) {
				return null;
			}
		} else {
			try {
				IvParameterSpec ips = new IvParameterSpec(iv);
				Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
				cipher.init(Cipher.ENCRYPT_MODE, this.getKey(), ips);
				Logging.d(Config.LOGTAG, "opening encrypted input stream");
				return new CipherInputStream(new FileInputStream(this), cipher);
			} catch (NoSuchAlgorithmException e) {
				Logging.d(Config.LOGTAG, "no such algo: " + e.getMessage());
				return null;
			} catch (NoSuchPaddingException e) {
				Logging.d(Config.LOGTAG, "no such padding: " + e.getMessage());
				return null;
			} catch (InvalidKeyException e) {
				Logging.d(Config.LOGTAG, "invalid key: " + e.getMessage());
				return null;
			} catch (InvalidAlgorithmParameterException e) {
				Logging.d(Config.LOGTAG, "invavid iv:" + e.getMessage());
				return null;
			} catch (FileNotFoundException e) {
				return null;
			}
		}
	}

	public OutputStream createOutputStream() {
		if (this.getKey() == null) {
			try {
				return new FileOutputStream(this);
			} catch (FileNotFoundException e) {
				return null;
			}
		} else {
			try {
				IvParameterSpec ips = new IvParameterSpec(this.iv);
				Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
				cipher.init(Cipher.DECRYPT_MODE, this.getKey(), ips);
				Logging.d(Config.LOGTAG, "opening encrypted output stream");
				return new CipherOutputStream(new FileOutputStream(this),
						cipher);
			} catch (NoSuchAlgorithmException e) {
				Logging.d(Config.LOGTAG, "no such algo: " + e.getMessage());
				return null;
			} catch (NoSuchPaddingException e) {
				Logging.d(Config.LOGTAG, "no such padding: " + e.getMessage());
				return null;
			} catch (InvalidKeyException e) {
				Logging.d(Config.LOGTAG, "invalid key: " + e.getMessage());
				return null;
			} catch (InvalidAlgorithmParameterException e) {
				Logging.d(Config.LOGTAG, "invavid iv:" + e.getMessage());
				return null;
			} catch (FileNotFoundException e) {
				return null;
			}
		}
	}
}