JingleTransport.java

 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 android.util.Log;
19
20public abstract class JingleTransport {
21	public abstract void connect(final OnTransportConnected callback);
22	public abstract void receive(final JingleFile file, final OnFileTransmissionStatusChanged callback);
23	public abstract void send(final JingleFile file, final OnFileTransmissionStatusChanged callback);
24	private byte[] iv = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0xf};
25	
26	protected InputStream getInputStream(JingleFile file) throws FileNotFoundException {
27		if (file.getKey() == null) {
28			return new FileInputStream(file);
29		} else {
30			try {
31				IvParameterSpec ips = new IvParameterSpec(iv);
32				Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
33				cipher.init(Cipher.ENCRYPT_MODE, file.getKey(),ips);
34				Log.d("xmppService","opening encrypted input stream");
35				return new CipherInputStream(new FileInputStream(file), cipher);
36			} catch (NoSuchAlgorithmException e) {
37				Log.d("xmppService","no such algo: "+e.getMessage());
38				return null;
39			} catch (NoSuchPaddingException e) {
40				Log.d("xmppService","no such padding: "+e.getMessage());
41				return null;
42			} catch (InvalidKeyException e) {
43				Log.d("xmppService","invalid key: "+e.getMessage());
44				return null;
45			} catch (InvalidAlgorithmParameterException e) {
46				Log.d("xmppService","invavid iv:"+e.getMessage());
47				return null;
48			}
49		}
50	}
51	
52	protected OutputStream getOutputStream(JingleFile file) throws FileNotFoundException {
53		if (file.getKey() == null) {
54			return new FileOutputStream(file);
55		} else {
56			try {
57				IvParameterSpec ips = new IvParameterSpec(iv);
58				Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
59				cipher.init(Cipher.DECRYPT_MODE, file.getKey(),ips);
60				Log.d("xmppService","opening encrypted output stream");
61				return new CipherOutputStream(new FileOutputStream(file), cipher);
62			} catch (NoSuchAlgorithmException e) {
63				Log.d("xmppService","no such algo: "+e.getMessage());
64				return null;
65			} catch (NoSuchPaddingException e) {
66				Log.d("xmppService","no such padding: "+e.getMessage());
67				return null;
68			} catch (InvalidKeyException e) {
69				Log.d("xmppService","invalid key: "+e.getMessage());
70				return null;
71			} catch (InvalidAlgorithmParameterException e) {
72				Log.d("xmppService","invavid iv:"+e.getMessage());
73				return null;
74			}
75		}
76	}
77}