mirror of
https://github.com/moparisthebest/Conversations
synced 2024-11-28 03:32:15 -05:00
fixes for otr file transfer
This commit is contained in:
parent
1cf055d2fd
commit
49b7c7779d
@ -5,9 +5,9 @@ import java.nio.charset.Charset;
|
|||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
import eu.siacs.conversations.entities.Account;
|
import eu.siacs.conversations.entities.Account;
|
||||||
|
|
||||||
import android.util.Base64;
|
import android.util.Base64;
|
||||||
|
|
||||||
public class CryptoHelper {
|
public class CryptoHelper {
|
||||||
@ -28,7 +28,11 @@ public class CryptoHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] hexToBytes(String hexString) {
|
public static byte[] hexToBytes(String hexString) {
|
||||||
return new BigInteger(hexString, 16).toByteArray();
|
byte[] array = new BigInteger(hexString, 16).toByteArray();
|
||||||
|
if (array[0] == 0) {
|
||||||
|
array = Arrays.copyOfRange(array, 1, array.length);
|
||||||
|
}
|
||||||
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String saslPlain(String username, String password) {
|
public static String saslPlain(String username, String password) {
|
||||||
|
@ -25,7 +25,11 @@ public class JingleFile extends File {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public long getExpectedSize() {
|
public long getExpectedSize() {
|
||||||
return this.expectedSize;
|
if (this.aeskey!=null) {
|
||||||
|
return (this.expectedSize/16 + 1) * 16;
|
||||||
|
} else {
|
||||||
|
return this.expectedSize;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setExpectedSize(long size) {
|
public void setExpectedSize(long size) {
|
||||||
@ -41,18 +45,18 @@ public class JingleFile extends File {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setKey(byte[] key) {
|
public void setKey(byte[] key) {
|
||||||
Log.d("xmppService","using aes key "+CryptoHelper.bytesToHex(key));
|
|
||||||
if (key.length>=32) {
|
if (key.length>=32) {
|
||||||
byte[] secretKey = new byte[32];
|
byte[] secretKey = new byte[32];
|
||||||
System.arraycopy(key, 0, secretKey, 0, 32);
|
System.arraycopy(key, 0, secretKey, 0, 32);
|
||||||
this.aeskey = new SecretKeySpec(key, "AES");
|
this.aeskey = new SecretKeySpec(secretKey, "AES");
|
||||||
} else if (key.length>=16) {
|
} else if (key.length>=16) {
|
||||||
byte[] secretKey = new byte[15];
|
byte[] secretKey = new byte[16];
|
||||||
System.arraycopy(key, 0, secretKey, 0, 16);
|
System.arraycopy(key, 0, secretKey, 0, 16);
|
||||||
this.aeskey = new SecretKeySpec(key, "AES");
|
this.aeskey = new SecretKeySpec(secretKey, "AES");
|
||||||
} else {
|
} else {
|
||||||
Log.d("xmppService","weird key");
|
Log.d("xmppService","weird key");
|
||||||
}
|
}
|
||||||
|
Log.d("xmppService","using aes key "+CryptoHelper.bytesToHex(this.aeskey.getEncoded()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Key getKey() {
|
public Key getKey() {
|
||||||
|
@ -6,6 +6,7 @@ import java.io.FileOutputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
import java.io.ObjectInputStream.GetField;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
@ -97,17 +98,16 @@ public class JingleSocks5Transport extends JingleTransport {
|
|||||||
digest.reset();
|
digest.reset();
|
||||||
fileInputStream = getInputStream(file);
|
fileInputStream = getInputStream(file);
|
||||||
int count;
|
int count;
|
||||||
long txbytes = 0;
|
long txBytes = 0;
|
||||||
byte[] buffer = new byte[8192];
|
byte[] buffer = new byte[8192];
|
||||||
while ((count = fileInputStream.read(buffer)) != -1) {
|
while ((count = fileInputStream.read(buffer)) > 0) {
|
||||||
txbytes += count;
|
txBytes += count;
|
||||||
outputStream.write(buffer, 0, count);
|
outputStream.write(buffer, 0, count);
|
||||||
digest.update(buffer, 0, count);
|
digest.update(buffer, 0, count);
|
||||||
Log.d("xmppService","tx bytes: "+txbytes);
|
|
||||||
}
|
}
|
||||||
|
Log.d("xmppService","txBytes="+txBytes);
|
||||||
outputStream.flush();
|
outputStream.flush();
|
||||||
file.setSha1Sum(CryptoHelper.bytesToHex(digest.digest()));
|
file.setSha1Sum(CryptoHelper.bytesToHex(digest.digest()));
|
||||||
//outputStream.close();
|
|
||||||
if (callback!=null) {
|
if (callback!=null) {
|
||||||
callback.onFileTransmitted(file);
|
callback.onFileTransmitted(file);
|
||||||
}
|
}
|
||||||
@ -115,7 +115,8 @@ public class JingleSocks5Transport extends JingleTransport {
|
|||||||
// TODO Auto-generated catch block
|
// TODO Auto-generated catch block
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.d("xmppService","io exception: "+e.getMessage());
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
} catch (NoSuchAlgorithmException e) {
|
} catch (NoSuchAlgorithmException e) {
|
||||||
// TODO Auto-generated catch block
|
// TODO Auto-generated catch block
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -149,26 +150,32 @@ public class JingleSocks5Transport extends JingleTransport {
|
|||||||
long remainingSize = file.getExpectedSize();
|
long remainingSize = file.getExpectedSize();
|
||||||
byte[] buffer = new byte[8192];
|
byte[] buffer = new byte[8192];
|
||||||
int count = buffer.length;
|
int count = buffer.length;
|
||||||
//while(remainingSize > 0) {
|
long rxBytes = 0;
|
||||||
while((count = inputStream.read(buffer)) > 0) {
|
while(remainingSize > 0) {
|
||||||
Log.d("xmppService","remaining size: "+remainingSize+" reading "+count+" bytes");
|
|
||||||
count = inputStream.read(buffer);
|
count = inputStream.read(buffer);
|
||||||
if (count!=-1) {
|
if (count==-1) {
|
||||||
|
Log.d("xmppService","read end");
|
||||||
|
} else {
|
||||||
|
rxBytes += count;
|
||||||
fileOutputStream.write(buffer, 0, count);
|
fileOutputStream.write(buffer, 0, count);
|
||||||
digest.update(buffer, 0, count);
|
digest.update(buffer, 0, count);
|
||||||
|
remainingSize-=count;
|
||||||
}
|
}
|
||||||
remainingSize-=count;
|
|
||||||
}
|
}
|
||||||
|
Log.d("xmppService","rx bytes="+rxBytes);
|
||||||
fileOutputStream.flush();
|
fileOutputStream.flush();
|
||||||
fileOutputStream.close();
|
fileOutputStream.close();
|
||||||
file.setSha1Sum(CryptoHelper.bytesToHex(digest.digest()));
|
file.setSha1Sum(CryptoHelper.bytesToHex(digest.digest()));
|
||||||
callback.onFileTransmitted(file);
|
callback.onFileTransmitted(file);
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
Log.d("xmppService","file not found exception");
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.d("xmppService","io exception: "+e.getMessage());
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
} catch (NoSuchAlgorithmException e) {
|
} catch (NoSuchAlgorithmException e) {
|
||||||
Log.d("xmppService","no such algo"+e.getMessage());
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
|
@ -5,6 +5,7 @@ import java.io.FileNotFoundException;
|
|||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
import java.security.InvalidAlgorithmParameterException;
|
||||||
import java.security.InvalidKeyException;
|
import java.security.InvalidKeyException;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
|
||||||
@ -12,6 +13,7 @@ import javax.crypto.Cipher;
|
|||||||
import javax.crypto.CipherOutputStream;
|
import javax.crypto.CipherOutputStream;
|
||||||
import javax.crypto.CipherInputStream;
|
import javax.crypto.CipherInputStream;
|
||||||
import javax.crypto.NoSuchPaddingException;
|
import javax.crypto.NoSuchPaddingException;
|
||||||
|
import javax.crypto.spec.IvParameterSpec;
|
||||||
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
@ -19,14 +21,16 @@ public abstract class JingleTransport {
|
|||||||
public abstract void connect(final OnTransportConnected callback);
|
public abstract void connect(final OnTransportConnected callback);
|
||||||
public abstract void receive(final JingleFile file, final OnFileTransmitted callback);
|
public abstract void receive(final JingleFile file, final OnFileTransmitted callback);
|
||||||
public abstract void send(final JingleFile file, final OnFileTransmitted callback);
|
public abstract void send(final JingleFile file, final OnFileTransmitted callback);
|
||||||
|
private byte[] iv = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0xf};
|
||||||
|
|
||||||
protected InputStream getInputStream(JingleFile file) throws FileNotFoundException {
|
protected InputStream getInputStream(JingleFile file) throws FileNotFoundException {
|
||||||
if (file.getKey() == null) {
|
if (file.getKey() == null) {
|
||||||
return new FileInputStream(file);
|
return new FileInputStream(file);
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
|
IvParameterSpec ips = new IvParameterSpec(iv);
|
||||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
||||||
cipher.init(Cipher.ENCRYPT_MODE, file.getKey());
|
cipher.init(Cipher.ENCRYPT_MODE, file.getKey(),ips);
|
||||||
Log.d("xmppService","opening encrypted input stream");
|
Log.d("xmppService","opening encrypted input stream");
|
||||||
return new CipherInputStream(new FileInputStream(file), cipher);
|
return new CipherInputStream(new FileInputStream(file), cipher);
|
||||||
} catch (NoSuchAlgorithmException e) {
|
} catch (NoSuchAlgorithmException e) {
|
||||||
@ -38,6 +42,9 @@ public abstract class JingleTransport {
|
|||||||
} catch (InvalidKeyException e) {
|
} catch (InvalidKeyException e) {
|
||||||
Log.d("xmppService","invalid key: "+e.getMessage());
|
Log.d("xmppService","invalid key: "+e.getMessage());
|
||||||
return null;
|
return null;
|
||||||
|
} catch (InvalidAlgorithmParameterException e) {
|
||||||
|
Log.d("xmppService","invavid iv:"+e.getMessage());
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -47,8 +54,9 @@ public abstract class JingleTransport {
|
|||||||
return new FileOutputStream(file);
|
return new FileOutputStream(file);
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
|
IvParameterSpec ips = new IvParameterSpec(iv);
|
||||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
||||||
cipher.init(Cipher.DECRYPT_MODE, file.getKey());
|
cipher.init(Cipher.DECRYPT_MODE, file.getKey(),ips);
|
||||||
Log.d("xmppService","opening encrypted output stream");
|
Log.d("xmppService","opening encrypted output stream");
|
||||||
return new CipherOutputStream(new FileOutputStream(file), cipher);
|
return new CipherOutputStream(new FileOutputStream(file), cipher);
|
||||||
} catch (NoSuchAlgorithmException e) {
|
} catch (NoSuchAlgorithmException e) {
|
||||||
@ -60,6 +68,9 @@ public abstract class JingleTransport {
|
|||||||
} catch (InvalidKeyException e) {
|
} catch (InvalidKeyException e) {
|
||||||
Log.d("xmppService","invalid key: "+e.getMessage());
|
Log.d("xmppService","invalid key: "+e.getMessage());
|
||||||
return null;
|
return null;
|
||||||
|
} catch (InvalidAlgorithmParameterException e) {
|
||||||
|
Log.d("xmppService","invavid iv:"+e.getMessage());
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user