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