1package eu.siacs.conversations.xmpp.jingle;
2
3import java.io.FileInputStream;
4import java.io.FileNotFoundException;
5import java.io.FileOutputStream;
6import java.io.InputStream;
7import java.io.OutputStream;
8import java.security.InvalidAlgorithmParameterException;
9import java.security.InvalidKeyException;
10import java.security.NoSuchAlgorithmException;
11
12import javax.crypto.Cipher;
13import javax.crypto.CipherOutputStream;
14import javax.crypto.CipherInputStream;
15import javax.crypto.NoSuchPaddingException;
16import javax.crypto.spec.IvParameterSpec;
17
18import eu.siacs.conversations.Config;
19
20import android.util.Log;
21
22public abstract class JingleTransport {
23 public abstract void connect(final OnTransportConnected callback);
24
25 public abstract void receive(final JingleFile file,
26 final OnFileTransmissionStatusChanged callback);
27
28 public abstract void send(final JingleFile file,
29 final OnFileTransmissionStatusChanged callback);
30
31 private byte[] iv = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
32 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0xf };
33
34 protected InputStream getInputStream(JingleFile file)
35 throws FileNotFoundException {
36 if (file.getKey() == null) {
37 return new FileInputStream(file);
38 } else {
39 try {
40 IvParameterSpec ips = new IvParameterSpec(iv);
41 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
42 cipher.init(Cipher.ENCRYPT_MODE, file.getKey(), ips);
43 Log.d(Config.LOGTAG, "opening encrypted input stream");
44 return new CipherInputStream(new FileInputStream(file), cipher);
45 } catch (NoSuchAlgorithmException e) {
46 Log.d(Config.LOGTAG, "no such algo: " + e.getMessage());
47 return null;
48 } catch (NoSuchPaddingException e) {
49 Log.d(Config.LOGTAG, "no such padding: " + e.getMessage());
50 return null;
51 } catch (InvalidKeyException e) {
52 Log.d(Config.LOGTAG, "invalid key: " + e.getMessage());
53 return null;
54 } catch (InvalidAlgorithmParameterException e) {
55 Log.d(Config.LOGTAG, "invavid iv:" + e.getMessage());
56 return null;
57 }
58 }
59 }
60
61 protected OutputStream getOutputStream(JingleFile file)
62 throws FileNotFoundException {
63 if (file.getKey() == null) {
64 return new FileOutputStream(file);
65 } else {
66 try {
67 IvParameterSpec ips = new IvParameterSpec(iv);
68 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
69 cipher.init(Cipher.DECRYPT_MODE, file.getKey(), ips);
70 Log.d(Config.LOGTAG, "opening encrypted output stream");
71 return new CipherOutputStream(new FileOutputStream(file),
72 cipher);
73 } catch (NoSuchAlgorithmException e) {
74 Log.d(Config.LOGTAG, "no such algo: " + e.getMessage());
75 return null;
76 } catch (NoSuchPaddingException e) {
77 Log.d(Config.LOGTAG, "no such padding: " + e.getMessage());
78 return null;
79 } catch (InvalidKeyException e) {
80 Log.d(Config.LOGTAG, "invalid key: " + e.getMessage());
81 return null;
82 } catch (InvalidAlgorithmParameterException e) {
83 Log.d(Config.LOGTAG, "invavid iv:" + e.getMessage());
84 return null;
85 }
86 }
87 }
88}