2014-04-20 20:34:27 +00:00
|
|
|
package eu.siacs.conversations.xmpp.jingle;
|
|
|
|
|
2014-06-20 15:30:19 +00:00
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2014-06-20 17:28:47 +00:00
|
|
|
import java.security.InvalidAlgorithmParameterException;
|
2014-06-20 15:30:19 +00:00
|
|
|
import java.security.InvalidKeyException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
|
|
|
import javax.crypto.Cipher;
|
|
|
|
import javax.crypto.CipherOutputStream;
|
|
|
|
import javax.crypto.CipherInputStream;
|
|
|
|
import javax.crypto.NoSuchPaddingException;
|
2014-06-20 17:28:47 +00:00
|
|
|
import javax.crypto.spec.IvParameterSpec;
|
2014-06-20 15:30:19 +00:00
|
|
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
2014-04-20 20:34:27 +00:00
|
|
|
public abstract class JingleTransport {
|
2014-04-22 11:11:53 +00:00
|
|
|
public abstract void connect(final OnTransportConnected callback);
|
2014-04-20 20:34:27 +00:00
|
|
|
public abstract void receive(final JingleFile file, final OnFileTransmitted callback);
|
|
|
|
public abstract void send(final JingleFile file, final OnFileTransmitted callback);
|
2014-06-20 17:28:47 +00:00
|
|
|
private byte[] iv = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0xf};
|
2014-06-20 15:30:19 +00:00
|
|
|
|
|
|
|
protected InputStream getInputStream(JingleFile file) throws FileNotFoundException {
|
|
|
|
if (file.getKey() == null) {
|
|
|
|
return new FileInputStream(file);
|
|
|
|
} else {
|
|
|
|
try {
|
2014-06-20 17:28:47 +00:00
|
|
|
IvParameterSpec ips = new IvParameterSpec(iv);
|
2014-06-20 15:30:19 +00:00
|
|
|
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
2014-06-20 17:28:47 +00:00
|
|
|
cipher.init(Cipher.ENCRYPT_MODE, file.getKey(),ips);
|
2014-06-20 15:30:19 +00:00
|
|
|
Log.d("xmppService","opening encrypted input stream");
|
|
|
|
return new CipherInputStream(new FileInputStream(file), cipher);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
Log.d("xmppService","no such algo: "+e.getMessage());
|
|
|
|
return null;
|
|
|
|
} catch (NoSuchPaddingException e) {
|
|
|
|
Log.d("xmppService","no such padding: "+e.getMessage());
|
|
|
|
return null;
|
|
|
|
} catch (InvalidKeyException e) {
|
|
|
|
Log.d("xmppService","invalid key: "+e.getMessage());
|
|
|
|
return null;
|
2014-06-20 17:28:47 +00:00
|
|
|
} catch (InvalidAlgorithmParameterException e) {
|
|
|
|
Log.d("xmppService","invavid iv:"+e.getMessage());
|
|
|
|
return null;
|
2014-06-20 15:30:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected OutputStream getOutputStream(JingleFile file) throws FileNotFoundException {
|
|
|
|
if (file.getKey() == null) {
|
|
|
|
return new FileOutputStream(file);
|
|
|
|
} else {
|
|
|
|
try {
|
2014-06-20 17:28:47 +00:00
|
|
|
IvParameterSpec ips = new IvParameterSpec(iv);
|
2014-06-20 15:30:19 +00:00
|
|
|
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
2014-06-20 17:28:47 +00:00
|
|
|
cipher.init(Cipher.DECRYPT_MODE, file.getKey(),ips);
|
2014-06-20 15:30:19 +00:00
|
|
|
Log.d("xmppService","opening encrypted output stream");
|
|
|
|
return new CipherOutputStream(new FileOutputStream(file), cipher);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
Log.d("xmppService","no such algo: "+e.getMessage());
|
|
|
|
return null;
|
|
|
|
} catch (NoSuchPaddingException e) {
|
|
|
|
Log.d("xmppService","no such padding: "+e.getMessage());
|
|
|
|
return null;
|
|
|
|
} catch (InvalidKeyException e) {
|
|
|
|
Log.d("xmppService","invalid key: "+e.getMessage());
|
|
|
|
return null;
|
2014-06-20 17:28:47 +00:00
|
|
|
} catch (InvalidAlgorithmParameterException e) {
|
|
|
|
Log.d("xmppService","invavid iv:"+e.getMessage());
|
|
|
|
return null;
|
2014-06-20 15:30:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-20 20:34:27 +00:00
|
|
|
}
|