Consider file of hash when deciding to accept file

This commit is contained in:
vanitasvitae 2017-08-20 14:59:32 +02:00
parent 646f617bc1
commit 0676688c04
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 32 additions and 7 deletions

View file

@ -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);
}

View file

@ -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) {