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.InvalidKeyException;
 9import java.security.NoSuchAlgorithmException;
10
11import javax.crypto.Cipher;
12import javax.crypto.CipherOutputStream;
13import javax.crypto.CipherInputStream;
14import javax.crypto.NoSuchPaddingException;
15
16import android.util.Log;
17
18public abstract class JingleTransport {
19	public abstract void connect(final OnTransportConnected callback);
20	public abstract void receive(final JingleFile file, final OnFileTransmitted callback);
21	public abstract void send(final JingleFile file, final OnFileTransmitted callback);
22	
23	protected InputStream getInputStream(JingleFile file) throws FileNotFoundException {
24		if (file.getKey() == null) {
25			return new FileInputStream(file);
26		} else {
27			try {
28				Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
29				cipher.init(Cipher.ENCRYPT_MODE, file.getKey());
30				Log.d("xmppService","opening encrypted input stream");
31				return new CipherInputStream(new FileInputStream(file), cipher);
32			} catch (NoSuchAlgorithmException e) {
33				Log.d("xmppService","no such algo: "+e.getMessage());
34				return null;
35			} catch (NoSuchPaddingException e) {
36				Log.d("xmppService","no such padding: "+e.getMessage());
37				return null;
38			} catch (InvalidKeyException e) {
39				Log.d("xmppService","invalid key: "+e.getMessage());
40				return null;
41			}
42		}
43	}
44	
45	protected OutputStream getOutputStream(JingleFile file) throws FileNotFoundException {
46		if (file.getKey() == null) {
47			return new FileOutputStream(file);
48		} else {
49			try {
50				Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
51				cipher.init(Cipher.DECRYPT_MODE, file.getKey());
52				Log.d("xmppService","opening encrypted output stream");
53				return new CipherOutputStream(new FileOutputStream(file), cipher);
54			} catch (NoSuchAlgorithmException e) {
55				Log.d("xmppService","no such algo: "+e.getMessage());
56				return null;
57			} catch (NoSuchPaddingException e) {
58				Log.d("xmppService","no such padding: "+e.getMessage());
59				return null;
60			} catch (InvalidKeyException e) {
61				Log.d("xmppService","invalid key: "+e.getMessage());
62				return null;
63			}
64		}
65	}
66}