make jingle implementation send file hash when using ft5

This commit is contained in:
Daniel Gultsch 2017-05-03 11:03:04 +02:00
parent ef78721f94
commit f47cf7ae67
5 changed files with 93 additions and 42 deletions

View File

@ -9,7 +9,7 @@ public class DownloadableFile extends File {
private static final long serialVersionUID = 2247012619505115863L; private static final long serialVersionUID = 2247012619505115863L;
private long expectedSize = 0; private long expectedSize = 0;
private String sha1sum; private byte[] sha1sum;
private byte[] aeskey; private byte[] aeskey;
private byte[] iv = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, private byte[] iv = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
@ -42,11 +42,11 @@ public class DownloadableFile extends File {
this.expectedSize = size; this.expectedSize = size;
} }
public String getSha1Sum() { public byte[] getSha1Sum() {
return this.sha1sum; return this.sha1sum;
} }
public void setSha1Sum(String sum) { public void setSha1Sum(byte[] sum) {
this.sha1sum = sum; this.sha1sum = sum;
} }

View File

@ -1,5 +1,6 @@
package eu.siacs.conversations.xmpp.jingle; package eu.siacs.conversations.xmpp.jingle;
import android.util.Base64;
import android.util.Log; import android.util.Log;
import android.util.Pair; import android.util.Pair;
@ -7,6 +8,7 @@ import java.io.FileNotFoundException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -29,6 +31,7 @@ import eu.siacs.conversations.parser.IqParser;
import eu.siacs.conversations.persistance.FileBackend; import eu.siacs.conversations.persistance.FileBackend;
import eu.siacs.conversations.services.AbstractConnectionManager; import eu.siacs.conversations.services.AbstractConnectionManager;
import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.utils.CryptoHelper;
import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.OnIqPacketReceived; import eu.siacs.conversations.xmpp.OnIqPacketReceived;
import eu.siacs.conversations.xmpp.jid.Jid; import eu.siacs.conversations.xmpp.jid.Jid;
@ -92,12 +95,24 @@ public class JingleConnection implements Transferable {
} }
} }
}; };
private byte[] expectedHash = new byte[0];
final OnFileTransmissionStatusChanged onFileTransmissionSatusChanged = new OnFileTransmissionStatusChanged() { private boolean responding() {
return responder.equals(account.getJid());
}
private boolean initiating() {
return initiator.equals(account.getJid());
}
final OnFileTransmissionStatusChanged onFileTransmissionStatusChanged = new OnFileTransmissionStatusChanged() {
@Override @Override
public void onFileTransmitted(DownloadableFile file) { public void onFileTransmitted(DownloadableFile file) {
if (responder.equals(account.getJid())) { if (responding()) {
if (expectedHash.length > 0 && !Arrays.equals(expectedHash,file.getSha1Sum())) {
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": hashes did not match");
}
sendSuccess(); sendSuccess();
mXmppConnectionService.getFileBackend().updateFileParams(message); mXmppConnectionService.getFileBackend().updateFileParams(message);
mXmppConnectionService.databaseBackend.createMessage(message); mXmppConnectionService.databaseBackend.createMessage(message);
@ -111,6 +126,9 @@ public class JingleConnection implements Transferable {
} }
} }
} else { } else {
if (ftVersion == Content.Version.FT_5) { //older Conversations will break when receiving a session-info
sendHash();
}
if (message.getEncryption() == Message.ENCRYPTION_PGP) { if (message.getEncryption() == Message.ENCRYPTION_PGP) {
account.getPgpDecryptionService().decrypt(message, false); account.getPgpDecryptionService().decrypt(message, false);
} }
@ -118,7 +136,7 @@ public class JingleConnection implements Transferable {
file.delete(); file.delete();
} }
} }
Log.d(Config.LOGTAG,"successfully transmitted file:" + file.getAbsolutePath()+" ("+file.getSha1Sum()+")"); Log.d(Config.LOGTAG,"successfully transmitted file:" + file.getAbsolutePath()+" ("+ CryptoHelper.bytesToHex(file.getSha1Sum())+")");
if (message.getEncryption() != Message.ENCRYPTION_PGP) { if (message.getEncryption() != Message.ENCRYPTION_PGP) {
mXmppConnectionService.getFileBackend().updateMediaScanner(file); mXmppConnectionService.getFileBackend().updateMediaScanner(file);
} }
@ -145,9 +163,9 @@ public class JingleConnection implements Transferable {
public void success() { public void success() {
if (initiator.equals(account.getJid())) { if (initiator.equals(account.getJid())) {
Log.d(Config.LOGTAG, "we were initiating. sending file"); Log.d(Config.LOGTAG, "we were initiating. sending file");
transport.send(file, onFileTransmissionSatusChanged); transport.send(file, onFileTransmissionStatusChanged);
} else { } else {
transport.receive(file, onFileTransmissionSatusChanged); transport.receive(file, onFileTransmissionStatusChanged);
Log.d(Config.LOGTAG, "we were responding. receiving file"); Log.d(Config.LOGTAG, "we were responding. receiving file");
} }
} }
@ -193,6 +211,18 @@ public class JingleConnection implements Transferable {
} }
} else if (packet.isAction("session-accept")) { } else if (packet.isAction("session-accept")) {
returnResult = receiveAccept(packet); returnResult = receiveAccept(packet);
} else if (packet.isAction("session-info")) {
returnResult = true;
Element checksum = packet.getChecksum();
Element file = checksum == null ? null : checksum.findChild("file");
Element hash = file == null ? null : file.findChild("hash","urn:xmpp:hashes:2");
if (hash != null && "sha-1".equalsIgnoreCase(hash.getAttribute("algo"))) {
try {
this.expectedHash = Base64.decode(hash.getContent(), Base64.DEFAULT);
} catch (Exception e) {
this.expectedHash = new byte[0];
}
}
} else if (packet.isAction("transport-info")) { } else if (packet.isAction("transport-info")) {
returnResult = receiveTransportInfo(packet); returnResult = receiveTransportInfo(packet);
} else if (packet.isAction("transport-replace")) { } else if (packet.isAction("transport-replace")) {
@ -499,6 +529,12 @@ public class JingleConnection implements Transferable {
} }
} }
private void sendHash() {
JinglePacket packet = this.bootstrapPacket("session-info");
packet.addChecksum(file.getSha1Sum(),ftVersion.getNamespace());
this.sendJinglePacket(packet);
}
private List<Element> getCandidatesAsElements() { private List<Element> getCandidatesAsElements() {
List<Element> elements = new ArrayList<>(); List<Element> elements = new ArrayList<>();
for (JingleCandidate c : this.candidates) { for (JingleCandidate c : this.candidates) {
@ -653,7 +689,7 @@ public class JingleConnection implements Transferable {
if (connection == null) { if (connection == null) {
Log.d(Config.LOGTAG, "could not find suitable candidate"); Log.d(Config.LOGTAG, "could not find suitable candidate");
this.disconnectSocks5Connections(); this.disconnectSocks5Connections();
if (this.initiator.equals(account.getJid())) { if (initiating()) {
this.sendFallbackToIbb(); this.sendFallbackToIbb();
} }
} else { } else {
@ -697,12 +733,12 @@ public class JingleConnection implements Transferable {
+ " was a proxy. waiting for other party to activate"); + " was a proxy. waiting for other party to activate");
} }
} else { } else {
if (initiator.equals(account.getJid())) { if (initiating()) {
Log.d(Config.LOGTAG, "we were initiating. sending file"); Log.d(Config.LOGTAG, "we were initiating. sending file");
connection.send(file, onFileTransmissionSatusChanged); connection.send(file, onFileTransmissionStatusChanged);
} else { } else {
Log.d(Config.LOGTAG, "we were responding. receiving file"); Log.d(Config.LOGTAG, "we were responding. receiving file");
connection.receive(file, onFileTransmissionSatusChanged); connection.receive(file, onFileTransmissionStatusChanged);
} }
} }
} }
@ -727,7 +763,7 @@ public class JingleConnection implements Transferable {
} else if (connection.getCandidate().getPriority() == currentConnection } else if (connection.getCandidate().getPriority() == currentConnection
.getCandidate().getPriority()) { .getCandidate().getPriority()) {
// Log.d(Config.LOGTAG,"found two candidates with same priority"); // Log.d(Config.LOGTAG,"found two candidates with same priority");
if (initiator.equals(account.getJid())) { if (initiating()) {
if (currentConnection.getCandidate().isOurs()) { if (currentConnection.getCandidate().isOurs()) {
connection = currentConnection; connection = currentConnection;
} }
@ -777,7 +813,7 @@ public class JingleConnection implements Transferable {
@Override @Override
public void established() { public void established() {
JingleConnection.this.transport.send(file, onFileTransmissionSatusChanged); JingleConnection.this.transport.send(file, onFileTransmissionStatusChanged);
} }
}; };
@ -801,7 +837,7 @@ public class JingleConnection implements Transferable {
answer.setContent(content); answer.setContent(content);
if (initiator.equals(account.getJid())) { if (initiating()) {
this.sendJinglePacket(answer, new OnIqPacketReceived() { this.sendJinglePacket(answer, new OnIqPacketReceived() {
@Override @Override
public void onIqPacketReceived(Account account, IqPacket packet) { public void onIqPacketReceived(Account account, IqPacket packet) {
@ -812,7 +848,7 @@ public class JingleConnection implements Transferable {
} }
}); });
} else { } else {
this.transport.receive(file, onFileTransmissionSatusChanged); this.transport.receive(file, onFileTransmissionStatusChanged);
this.sendJinglePacket(answer); this.sendJinglePacket(answer);
} }
return true; return true;
@ -831,10 +867,10 @@ public class JingleConnection implements Transferable {
this.transport = new JingleInbandTransport(this, this.transportId, this.ibbBlockSize); this.transport = new JingleInbandTransport(this, this.transportId, this.ibbBlockSize);
//might be receive instead if we are not initiating //might be receive instead if we are not initiating
if (initiator.equals(account.getJid())) { if (initiating()) {
this.transport.connect(onIbbTransportConnected); this.transport.connect(onIbbTransportConnected);
} else { } else {
this.transport.receive(file,onFileTransmissionSatusChanged); this.transport.receive(file, onFileTransmissionStatusChanged);
} }
return true; return true;
} else { } else {
@ -843,14 +879,18 @@ public class JingleConnection implements Transferable {
} }
private void receiveSuccess() { private void receiveSuccess() {
this.mJingleStatus = JINGLE_STATUS_FINISHED; if (initiating()) {
this.mXmppConnectionService.markMessage(this.message,Message.STATUS_SEND_RECEIVED); this.mJingleStatus = JINGLE_STATUS_FINISHED;
this.disconnectSocks5Connections(); this.mXmppConnectionService.markMessage(this.message, Message.STATUS_SEND_RECEIVED);
if (this.transport != null && this.transport instanceof JingleInbandTransport) { this.disconnectSocks5Connections();
this.transport.disconnect(); if (this.transport != null && this.transport instanceof JingleInbandTransport) {
this.transport.disconnect();
}
this.message.setTransferable(null);
this.mJingleConnectionManager.finishConnection(this);
} else {
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": received session-terminate/success while responding");
} }
this.message.setTransferable(null);
this.mJingleConnectionManager.finishConnection(this);
} }
public void cancel() { public void cancel() {
@ -860,7 +900,7 @@ public class JingleConnection implements Transferable {
} }
this.sendCancel(); this.sendCancel();
this.mJingleConnectionManager.finishConnection(this); this.mJingleConnectionManager.finishConnection(this);
if (this.responder.equals(account.getJid())) { if (responding()) {
this.message.setTransferable(new TransferablePlaceholder(Transferable.STATUS_FAILED)); this.message.setTransferable(new TransferablePlaceholder(Transferable.STATUS_FAILED));
if (this.file!=null) { if (this.file!=null) {
file.delete(); file.delete();
@ -886,7 +926,7 @@ public class JingleConnection implements Transferable {
FileBackend.close(mFileInputStream); FileBackend.close(mFileInputStream);
FileBackend.close(mFileOutputStream); FileBackend.close(mFileOutputStream);
if (this.message != null) { if (this.message != null) {
if (this.responder.equals(account.getJid())) { if (responding()) {
this.message.setTransferable(new TransferablePlaceholder(Transferable.STATUS_FAILED)); this.message.setTransferable(new TransferablePlaceholder(Transferable.STATUS_FAILED));
if (this.file!=null) { if (this.file!=null) {
file.delete(); file.delete();
@ -992,14 +1032,6 @@ public class JingleConnection implements Transferable {
this.sendJinglePacket(packet); this.sendJinglePacket(packet);
} }
public Jid getInitiator() {
return this.initiator;
}
public Jid getResponder() {
return this.responder;
}
public int getJingleStatus() { public int getJingleStatus() {
return this.mJingleStatus; return this.mJingleStatus;
} }
@ -1051,9 +1083,9 @@ public class JingleConnection implements Transferable {
} }
interface OnProxyActivated { interface OnProxyActivated {
public void success(); void success();
public void failed(); void failed();
} }
public boolean hasTransportId(String sid) { public boolean hasTransportId(String sid) {

View File

@ -166,7 +166,7 @@ public class JingleInbandTransport extends JingleTransport {
int count = fileInputStream.read(buffer); int count = fileInputStream.read(buffer);
if (count == -1) { if (count == -1) {
sendClose(); sendClose();
file.setSha1Sum(CryptoHelper.bytesToHex(digest.digest())); file.setSha1Sum(digest.digest());
this.onFileTransmissionStatusChanged.onFileTransmitted(file); this.onFileTransmissionStatusChanged.onFileTransmitted(file);
fileInputStream.close(); fileInputStream.close();
return; return;
@ -193,7 +193,7 @@ public class JingleInbandTransport extends JingleTransport {
connection.updateProgress((int) ((((double) (this.fileSize - this.remainingSize)) / this.fileSize) * 100)); connection.updateProgress((int) ((((double) (this.fileSize - this.remainingSize)) / this.fileSize) * 100));
} else { } else {
sendClose(); sendClose();
file.setSha1Sum(CryptoHelper.bytesToHex(digest.digest())); file.setSha1Sum(digest.digest());
this.onFileTransmissionStatusChanged.onFileTransmitted(file); this.onFileTransmissionStatusChanged.onFileTransmitted(file);
fileInputStream.close(); fileInputStream.close();
} }
@ -214,7 +214,7 @@ public class JingleInbandTransport extends JingleTransport {
this.fileOutputStream.write(buffer); this.fileOutputStream.write(buffer);
this.digest.update(buffer); this.digest.update(buffer);
if (this.remainingSize <= 0) { if (this.remainingSize <= 0) {
file.setSha1Sum(CryptoHelper.bytesToHex(digest.digest())); file.setSha1Sum(digest.digest());
fileOutputStream.flush(); fileOutputStream.flush();
fileOutputStream.close(); fileOutputStream.close();
this.onFileTransmissionStatusChanged.onFileTransmitted(file); this.onFileTransmissionStatusChanged.onFileTransmitted(file);

View File

@ -112,7 +112,7 @@ public class JingleSocks5Transport extends JingleTransport {
connection.updateProgress((int) ((((double) transmitted) / size) * 100)); connection.updateProgress((int) ((((double) transmitted) / size) * 100));
} }
outputStream.flush(); outputStream.flush();
file.setSha1Sum(CryptoHelper.bytesToHex(digest.digest())); file.setSha1Sum(digest.digest());
if (callback != null) { if (callback != null) {
callback.onFileTransmitted(file); callback.onFileTransmitted(file);
} }
@ -168,7 +168,7 @@ public class JingleSocks5Transport extends JingleTransport {
} }
fileOutputStream.flush(); fileOutputStream.flush();
fileOutputStream.close(); fileOutputStream.close();
file.setSha1Sum(CryptoHelper.bytesToHex(digest.digest())); file.setSha1Sum(digest.digest());
callback.onFileTransmitted(file); callback.onFileTransmitted(file);
} catch (Exception e) { } catch (Exception e) {
Log.d(Config.LOGTAG, connection.getAccount().getJid().toBareJid() + ": "+e.getMessage()); Log.d(Config.LOGTAG, connection.getAccount().getJid().toBareJid() + ": "+e.getMessage());

View File

@ -1,5 +1,7 @@
package eu.siacs.conversations.xmpp.jingle.stanzas; package eu.siacs.conversations.xmpp.jingle.stanzas;
import android.util.Base64;
import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.jid.Jid; import eu.siacs.conversations.xmpp.jid.Jid;
import eu.siacs.conversations.xmpp.stanzas.IqPacket; import eu.siacs.conversations.xmpp.stanzas.IqPacket;
@ -7,6 +9,7 @@ import eu.siacs.conversations.xmpp.stanzas.IqPacket;
public class JinglePacket extends IqPacket { public class JinglePacket extends IqPacket {
Content content = null; Content content = null;
Reason reason = null; Reason reason = null;
Element checksum = null;
Element jingle = new Element("jingle"); Element jingle = new Element("jingle");
@Override @Override
@ -24,6 +27,7 @@ public class JinglePacket extends IqPacket {
this.reason.setChildren(reasonElement.getChildren()); this.reason.setChildren(reasonElement.getChildren());
this.reason.setAttributes(reasonElement.getAttributes()); this.reason.setAttributes(reasonElement.getAttributes());
} }
this.checksum = child.findChild("checksum");
this.jingle.setAttributes(child.getAttributes()); this.jingle.setAttributes(child.getAttributes());
} }
return child; return child;
@ -50,6 +54,10 @@ public class JinglePacket extends IqPacket {
return this.reason; return this.reason;
} }
public Element getChecksum() {
return this.checksum;
}
private void build() { private void build() {
this.children.clear(); this.children.clear();
this.jingle.clearChildren(); this.jingle.clearChildren();
@ -60,6 +68,9 @@ public class JinglePacket extends IqPacket {
if (this.reason != null) { if (this.reason != null) {
jingle.addChild(this.reason); jingle.addChild(this.reason);
} }
if (this.checksum != null) {
jingle.addChild(checksum);
}
this.children.add(jingle); this.children.add(jingle);
this.setAttribute("type", "set"); this.setAttribute("type", "set");
} }
@ -93,4 +104,12 @@ public class JinglePacket extends IqPacket {
public boolean isAction(String action) { public boolean isAction(String action) {
return action.equalsIgnoreCase(this.getAction()); return action.equalsIgnoreCase(this.getAction());
} }
public void addChecksum(byte[] sha1Sum, String namespace) {
this.checksum = new Element("checksum",namespace);
checksum.setAttribute("creator","initiator");
checksum.setAttribute("name","a-file-offer");
Element hash = checksum.addChild("file").addChild("hash","urn:xmpp:hashes:2");
hash.setAttribute("algo","sha-1").setContent(Base64.encodeToString(sha1Sum,Base64.NO_WRAP));
}
} }