pass omemo decrypt up to higher layers to count as download error. decrypt all encrypted files

This commit is contained in:
Daniel Gultsch 2020-01-19 10:01:43 +01:00
parent 581eb5556f
commit c502503994
1 changed files with 462 additions and 465 deletions

View File

@ -31,22 +31,20 @@ import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.utils.CryptoHelper; import eu.siacs.conversations.utils.CryptoHelper;
import eu.siacs.conversations.utils.FileWriterException; import eu.siacs.conversations.utils.FileWriterException;
import eu.siacs.conversations.utils.WakeLockHelper; import eu.siacs.conversations.utils.WakeLockHelper;
import eu.siacs.conversations.xmpp.OnIqPacketReceived;
import eu.siacs.conversations.xmpp.stanzas.IqPacket; import eu.siacs.conversations.xmpp.stanzas.IqPacket;
import rocks.xmpp.addr.Jid; import rocks.xmpp.addr.Jid;
public class HttpDownloadConnection implements Transferable { public class HttpDownloadConnection implements Transferable {
private final Message message;
private final boolean mUseTor;
private HttpConnectionManager mHttpConnectionManager; private HttpConnectionManager mHttpConnectionManager;
private XmppConnectionService mXmppConnectionService; private XmppConnectionService mXmppConnectionService;
private URL mUrl; private URL mUrl;
private final Message message;
private DownloadableFile file; private DownloadableFile file;
private int mStatus = Transferable.STATUS_UNKNOWN; private int mStatus = Transferable.STATUS_UNKNOWN;
private boolean acceptedAutomatically = false; private boolean acceptedAutomatically = false;
private int mProgress = 0; private int mProgress = 0;
private final boolean mUseTor;
private boolean canceled = false; private boolean canceled = false;
private Method method = Method.HTTP_UPLOAD; private Method method = Method.HTTP_UPLOAD;
@ -105,16 +103,15 @@ public class HttpDownloadConnection implements Transferable {
ext = extension.main; ext = extension.main;
} }
message.setRelativeFilePath(message.getUuid() + (ext != null ? ("." + ext) : "")); message.setRelativeFilePath(message.getUuid() + (ext != null ? ("." + ext) : ""));
if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) { final String reference = mUrl.getRef();
if (reference != null && AesGcmURLStreamHandler.IV_KEY.matcher(reference).matches()) {
this.file = new DownloadableFile(mXmppConnectionService.getCacheDir().getAbsolutePath() + "/" + message.getUuid()); this.file = new DownloadableFile(mXmppConnectionService.getCacheDir().getAbsolutePath() + "/" + message.getUuid());
this.file.setKeyAndIv(CryptoHelper.hexToBytes(reference));
Log.d(Config.LOGTAG, "create temporary OMEMO encrypted file: " + this.file.getAbsolutePath() + "(" + message.getMimeType() + ")"); Log.d(Config.LOGTAG, "create temporary OMEMO encrypted file: " + this.file.getAbsolutePath() + "(" + message.getMimeType() + ")");
} else { } else {
this.file = mXmppConnectionService.getFileBackend().getFile(message, false); this.file = mXmppConnectionService.getFileBackend().getFile(message, false);
} }
final String reference = mUrl.getRef();
if (reference != null && AesGcmURLStreamHandler.IV_KEY.matcher(reference).matches()) {
this.file.setKeyAndIv(CryptoHelper.hexToBytes(reference));
}
if (this.message.getEncryption() == Message.ENCRYPTION_AXOLOTL && this.file.getKey() == null) { if (this.message.getEncryption() == Message.ENCRYPTION_AXOLOTL && this.file.getKey() == null) {
this.message.setEncryption(Message.ENCRYPTION_NONE); this.message.setEncryption(Message.ENCRYPTION_NONE);
@ -151,15 +148,17 @@ public class HttpDownloadConnection implements Transferable {
mHttpConnectionManager.updateConversationUi(true); mHttpConnectionManager.updateConversationUi(true);
} }
private void decryptOmemoFile() { private void decryptFile() throws IOException {
final DownloadableFile outputFile = mXmppConnectionService.getFileBackend().getFile(message, true); final DownloadableFile outputFile = mXmppConnectionService.getFileBackend().getFile(message, true);
if (outputFile.getParentFile().mkdirs()) { if (outputFile.getParentFile().mkdirs()) {
Log.d(Config.LOGTAG, "created parent directories for " + outputFile.getAbsolutePath()); Log.d(Config.LOGTAG, "created parent directories for " + outputFile.getAbsolutePath());
} }
try { if (!outputFile.createNewFile()) {
outputFile.createNewFile(); Log.w(Config.LOGTAG, "unable to create output file " + outputFile.getAbsolutePath());
}
final InputStream is = new FileInputStream(this.file); final InputStream is = new FileInputStream(this.file);
outputFile.setKey(this.file.getKey()); outputFile.setKey(this.file.getKey());
@ -175,11 +174,9 @@ public class HttpDownloadConnection implements Transferable {
Log.w(Config.LOGTAG, "unable to delete temporary OMEMO encrypted file " + file.getAbsolutePath()); Log.w(Config.LOGTAG, "unable to delete temporary OMEMO encrypted file " + file.getAbsolutePath());
} }
message.setRelativeFilePath(outputFile.getPath()); //TODO can this be skipped?
} catch (IOException e) { //message.setRelativeFilePath(outputFile.getPath());
message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
mXmppConnectionService.updateMessage(message);
}
} }
private void finish() { private void finish() {
@ -199,9 +196,9 @@ public class HttpDownloadConnection implements Transferable {
}); });
} }
private void decryptIfNeeded() { private void decryptIfNeeded() throws IOException {
if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) { if (file.getKey() != null && file.getIv() != null) {
decryptOmemoFile(); decryptFile();
} }
} }