made httpconnection accept aes encrypted files

This commit is contained in:
iNPUTmice 2014-10-22 13:06:46 +02:00
parent 45bdadd915
commit 62b0fc3fda
5 changed files with 33 additions and 16 deletions

View File

@ -19,7 +19,6 @@ import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
import eu.siacs.conversations.utils.CryptoHelper;
import android.util.Log; import android.util.Log;
public class DownloadableFile extends File { public class DownloadableFile extends File {
@ -43,7 +42,11 @@ public class DownloadableFile extends File {
public long getExpectedSize() { public long getExpectedSize() {
if (this.aeskey != null) { if (this.aeskey != null) {
return (this.expectedSize / 16 + 1) * 16; if (this.expectedSize == 0) {
return 0;
} else {
return (this.expectedSize / 16 + 1) * 16;
}
} else { } else {
return this.expectedSize; return this.expectedSize;
} }
@ -62,7 +65,14 @@ public class DownloadableFile extends File {
} }
public void setKey(byte[] key) { public void setKey(byte[] key) {
if (key.length >= 32) { if (key.length == 48) {
byte[] secretKey = new byte[32];
byte[] iv = new byte[16];
System.arraycopy(key, 0, iv, 0, 16);
System.arraycopy(key, 16, secretKey, 0, 32);
this.aeskey = new SecretKeySpec(secretKey, "AES");
this.iv = iv;
} else 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(secretKey, "AES"); this.aeskey = new SecretKeySpec(secretKey, "AES");
@ -70,12 +80,7 @@ public class DownloadableFile extends File {
byte[] secretKey = new byte[16]; byte[] secretKey = new byte[16];
System.arraycopy(key, 0, secretKey, 0, 16); System.arraycopy(key, 0, secretKey, 0, 16);
this.aeskey = new SecretKeySpec(secretKey, "AES"); this.aeskey = new SecretKeySpec(secretKey, "AES");
} else {
Log.d(Config.LOGTAG, "weird key");
} }
Log.d(Config.LOGTAG,
"using aes key "
+ CryptoHelper.bytesToHex(this.aeskey.getEncoded()));
} }
public Key getKey() { public Key getKey() {
@ -123,7 +128,7 @@ public class DownloadableFile extends File {
} }
} else { } else {
try { try {
IvParameterSpec ips = new IvParameterSpec(iv); IvParameterSpec ips = new IvParameterSpec(this.iv);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, this.getKey(), ips); cipher.init(Cipher.DECRYPT_MODE, this.getKey(), ips);
Log.d(Config.LOGTAG, "opening encrypted output stream"); Log.d(Config.LOGTAG, "opening encrypted output stream");

View File

@ -403,8 +403,9 @@ public class Message extends AbstractEntity {
extensionParts[extensionParts.length - 1])) { extensionParts[extensionParts.length - 1])) {
return true; return true;
} else if (extensionParts.length == 3 } else if (extensionParts.length == 3
&& Arrays.asList(Downloadable.VALID_CRYPTO_EXTENSIONS) && Arrays
.contains(extensionParts.length - 1) .asList(Downloadable.VALID_CRYPTO_EXTENSIONS)
.contains(extensionParts[extensionParts.length - 1])
&& Arrays.asList(Downloadable.VALID_EXTENSIONS).contains( && Arrays.asList(Downloadable.VALID_EXTENSIONS).contains(
extensionParts[extensionParts.length - 2])) { extensionParts[extensionParts.length - 2])) {
return true; return true;

View File

@ -27,6 +27,7 @@ import eu.siacs.conversations.entities.Downloadable;
import eu.siacs.conversations.entities.DownloadableFile; import eu.siacs.conversations.entities.DownloadableFile;
import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.utils.CryptoHelper;
public class HttpConnection implements Downloadable { public class HttpConnection implements Downloadable {
@ -64,6 +65,14 @@ public class HttpConnection implements Downloadable {
mUrl = new URL(message.getBody()); mUrl = new URL(message.getBody());
this.file = mXmppConnectionService.getFileBackend().getFile( this.file = mXmppConnectionService.getFileBackend().getFile(
message, false); message, false);
String reference = mUrl.getRef();
if (reference != null && reference.length() == 96) {
this.file.setKey(CryptoHelper.hexToBytes(reference));
}
if (this.message.getEncryption() == Message.ENCRYPTION_OTR
&& this.file.getKey() == null) {
this.message.setEncryption(Message.ENCRYPTION_NONE);
}
checkFileSize(false); checkFileSize(false);
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
this.cancel(); this.cancel();

View File

@ -5,7 +5,6 @@ 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;
@ -28,9 +27,11 @@ public class CryptoHelper {
} }
public static byte[] hexToBytes(String hexString) { public static byte[] hexToBytes(String hexString) {
byte[] array = new BigInteger(hexString, 16).toByteArray(); int len = hexString.length();
if (array[0] == 0) { byte[] array = new byte[len / 2];
array = Arrays.copyOfRange(array, 1, array.length); for (int i = 0; i < len; i += 2) {
array[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character
.digit(hexString.charAt(i + 1), 16));
} }
return array; return array;
} }

View File

@ -136,7 +136,8 @@ public class JingleConnectionManager extends AbstractConnectionManager {
} }
if (sid != null) { if (sid != null) {
for (JingleConnection connection : connections) { for (JingleConnection connection : connections) {
if (connection.getAccount() == account && connection.hasTransportId(sid)) { if (connection.getAccount() == account
&& connection.hasTransportId(sid)) {
JingleTransport transport = connection.getTransport(); JingleTransport transport = connection.getTransport();
if (transport instanceof JingleInbandTransport) { if (transport instanceof JingleInbandTransport) {
JingleInbandTransport inbandTransport = (JingleInbandTransport) transport; JingleInbandTransport inbandTransport = (JingleInbandTransport) transport;