transform aesgcm:// links back to https:// before connecting through Tor

Daniel Gultsch created

fixes #2444

Change summary

src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java | 17 
src/main/java/eu/siacs/conversations/utils/CryptoHelper.java          | 14 
2 files changed, 25 insertions(+), 6 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java 🔗

@@ -70,9 +70,9 @@ public class HttpDownloadConnection implements Transferable {
 		this.message.setTransferable(this);
 		try {
 			if (message.hasFileOnRemoteHost()) {
-				mUrl = message.getFileParams().url;
+				mUrl = CryptoHelper.toHttpsUrl(message.getFileParams().url);
 			} else {
-				mUrl = new URL(message.getBody());
+				mUrl = CryptoHelper.toHttpsUrl(new URL(message.getBody()));
 			}
 			String[] parts = mUrl.getPath().toLowerCase().split("\\.");
 			String lastPart = parts.length >= 1 ? parts[parts.length - 1] : null;
@@ -91,8 +91,8 @@ public class HttpDownloadConnection implements Transferable {
 			}
 			message.setRelativeFilePath(message.getUuid() + "." + extension);
 			this.file = mXmppConnectionService.getFileBackend().getFile(message, false);
-			String reference = mUrl.getRef();
-			if (reference != null && reference.length() == 96) {
+			final String reference = mUrl.getRef();
+			if (reference != null && reference.matches("([A-Fa-f0-9]{2}){48}")) {
 				this.file.setKeyAndIv(CryptoHelper.hexToBytes(reference));
 			}
 
@@ -332,7 +332,14 @@ public class HttpDownloadConnection implements Transferable {
 
 		private void updateImageBounds() {
 			message.setType(Message.TYPE_FILE);
-			mXmppConnectionService.getFileBackend().updateFileParams(message, mUrl);
+			final URL url;
+			final String ref = mUrl.getRef();
+			if (ref != null && ref.matches("([A-Fa-f0-9]{2}){48}")) {
+				url = CryptoHelper.toAesGcmUrl(mUrl);
+			} else {
+				url = mUrl;
+			}
+			mXmppConnectionService.getFileBackend().updateFileParams(message, url);
 			mXmppConnectionService.updateMessage(message);
 		}
 

src/main/java/eu/siacs/conversations/utils/CryptoHelper.java 🔗

@@ -29,6 +29,7 @@ import eu.siacs.conversations.Config;
 import eu.siacs.conversations.R;
 import eu.siacs.conversations.entities.Account;
 import eu.siacs.conversations.entities.Message;
+import eu.siacs.conversations.http.AesGcmURLStreamHandler;
 import eu.siacs.conversations.xmpp.jid.InvalidJidException;
 import eu.siacs.conversations.xmpp.jid.Jid;
 
@@ -238,7 +239,18 @@ public final class CryptoHelper {
 			return url;
 		}
 		try {
-			return new URL("aesgcm"+url.toString().substring(url.getProtocol().length()));
+			return new URL(AesGcmURLStreamHandler.PROTOCOL_NAME+url.toString().substring(url.getProtocol().length()));
+		} catch (MalformedURLException e) {
+			return url;
+		}
+	}
+
+	public static URL toHttpsUrl(URL url) {
+		if (!url.getProtocol().equalsIgnoreCase(AesGcmURLStreamHandler.PROTOCOL_NAME)) {
+			return url;
+		}
+		try {
+			return new URL("https"+url.toString().substring(url.getProtocol().length()));
 		} catch (MalformedURLException e) {
 			return url;
 		}