From 0676688c04e6c00ab1472bb48c6d77a1a2b8dbeb Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Sun, 20 Aug 2017 14:59:32 +0200 Subject: [PATCH] Consider file of hash when deciding to accept file --- .../de/vanitasvitae/sync_client/Master.java | 13 +++++++++- .../de/vanitasvitae/sync_client/Slave.java | 26 ++++++++++++++----- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/main/java/de/vanitasvitae/sync_client/Master.java b/src/main/java/de/vanitasvitae/sync_client/Master.java index 5fdada6..315c0ce 100644 --- a/src/main/java/de/vanitasvitae/sync_client/Master.java +++ b/src/main/java/de/vanitasvitae/sync_client/Master.java @@ -31,8 +31,10 @@ import javax.crypto.NoSuchPaddingException; import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.util.Async; +import org.jivesoftware.smackx.hashes.HashManager; import org.jivesoftware.smackx.jet.JetManager; import org.jivesoftware.smackx.jingle_filetransfer.JingleFileTransferManager; +import org.jivesoftware.smackx.jingle_filetransfer.component.JingleFile; import org.jivesoftware.smackx.omemo.OmemoManager; import org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException; @@ -201,8 +203,17 @@ public class Master extends Client { throw new AssertionError("Illegal path! " + fileName); } + JingleFile metadata = null; + try { - jetm.sendEncryptedFile(file, fileName, remote, OmemoManager.getInstanceFor(connection)); + metadata = JingleFile.fromFile(file, null, null, HashManager.ALGORITHM.SHA_256); + metadata.setName(fileName); + } catch (NoSuchAlgorithmException | IOException e) { + System.out.println("Could not calculate hash: " + e); + } + + try { + jetm.sendEncryptedFile(file, metadata, remote, OmemoManager.getInstanceFor(connection)); } catch (Exception e) { System.out.println("Could not send file: " + e); } diff --git a/src/main/java/de/vanitasvitae/sync_client/Slave.java b/src/main/java/de/vanitasvitae/sync_client/Slave.java index e826a8d..fe9fd47 100644 --- a/src/main/java/de/vanitasvitae/sync_client/Slave.java +++ b/src/main/java/de/vanitasvitae/sync_client/Slave.java @@ -7,14 +7,16 @@ import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; -import java.util.logging.Logger; +import java.util.Arrays; import javax.crypto.BadPaddingException; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.XMPPException; +import org.jivesoftware.smackx.hashes.element.HashElement; import org.jivesoftware.smackx.jingle_filetransfer.JingleFileTransferManager; +import org.jivesoftware.smackx.jingle_filetransfer.component.JingleFile; import org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException; import org.jxmpp.stringprep.XmppStringprepException; @@ -30,12 +32,24 @@ public class Slave extends Client { JingleFileTransferManager jingleFileTransferManager = JingleFileTransferManager.getInstanceFor(connection); jingleFileTransferManager.addIncomingFileOfferListener(offer -> { try { - File target = new File(root.toFile(), offer.getFile().getName()); - if (target.exists() && target.length() == offer.getFile().getSize()) { - System.out.println("We already seem to have file " + target.getAbsolutePath()+ ". Cancel."); - offer.cancel(connection); - return; + File target = new File(root.toFile(), offer.getMetadata().getName()); + if (target.exists() && target.length() == offer.getMetadata().getSize()) { + + if (offer.getMetadata().getHashElement() != null) { + HashElement offerHash = offer.getMetadata().getHashElement(); + try { + HashElement ourHash = JingleFile.calculateHash(target, offerHash.getAlgorithm()); + if (Arrays.equals(ourHash.getHash(), offerHash.getHash())) { + System.out.println("We already seem to have file " + target.getAbsolutePath()+ ". Cancel."); + offer.cancel(connection); + return; + } + } catch (NoSuchAlgorithmException e) { + System.out.println("Unsupported hash algo: " + e); + } + } } + target.getParentFile().mkdirs(); offer.accept(connection, target); } catch (InterruptedException | XMPPException.XMPPErrorException | SmackException.NoResponseException | SmackException.NotConnectedException | IOException e) {