From ee9c92f555cf48b8cfca37f2c7d971287c597d62 Mon Sep 17 00:00:00 2001 From: Ampli-fier <22896574+Ampli-fier@users.noreply.github.com> Date: Thu, 18 Apr 2019 22:32:18 +0200 Subject: [PATCH] Revert "remove unused glide modues" This reverts commit dcfe4c3e5b2e75f76beb303a5a3059435d79e131. --- .../cache/EncryptedBitmapCacheDecoder.java | 54 ++++++++++ .../cache/EncryptedBitmapResourceEncoder.java | 65 ++++++++++++ .../glide/cache/EncryptedCacheEncoder.java | 52 ++++++++++ .../securesms/glide/cache/EncryptedCoder.java | 98 +++++++++++++++++++ .../glide/cache/EncryptedGifCacheDecoder.java | 51 ++++++++++ .../EncryptedGifDrawableResourceEncoder.java | 45 +++++++++ .../securesms/mms/SignalGlideModule.java | 27 +++-- 7 files changed, 385 insertions(+), 7 deletions(-) create mode 100644 src/org/thoughtcrime/securesms/glide/cache/EncryptedBitmapCacheDecoder.java create mode 100644 src/org/thoughtcrime/securesms/glide/cache/EncryptedBitmapResourceEncoder.java create mode 100644 src/org/thoughtcrime/securesms/glide/cache/EncryptedCacheEncoder.java create mode 100644 src/org/thoughtcrime/securesms/glide/cache/EncryptedCoder.java create mode 100644 src/org/thoughtcrime/securesms/glide/cache/EncryptedGifCacheDecoder.java create mode 100644 src/org/thoughtcrime/securesms/glide/cache/EncryptedGifDrawableResourceEncoder.java diff --git a/src/org/thoughtcrime/securesms/glide/cache/EncryptedBitmapCacheDecoder.java b/src/org/thoughtcrime/securesms/glide/cache/EncryptedBitmapCacheDecoder.java new file mode 100644 index 000000000..362bb7a5a --- /dev/null +++ b/src/org/thoughtcrime/securesms/glide/cache/EncryptedBitmapCacheDecoder.java @@ -0,0 +1,54 @@ +package org.thoughtcrime.securesms.glide.cache; + + +import android.graphics.Bitmap; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.util.Log; + +import com.bumptech.glide.load.Options; +import com.bumptech.glide.load.ResourceDecoder; +import com.bumptech.glide.load.engine.Resource; +import com.bumptech.glide.load.resource.bitmap.StreamBitmapDecoder; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; + +public class EncryptedBitmapCacheDecoder extends EncryptedCoder implements ResourceDecoder { + + private static final String TAG = EncryptedBitmapCacheDecoder.class.getSimpleName(); + + private final StreamBitmapDecoder streamBitmapDecoder; + private final byte[] secret; + + public EncryptedBitmapCacheDecoder(@NonNull byte[] secret, @NonNull StreamBitmapDecoder streamBitmapDecoder) { + this.secret = secret; + this.streamBitmapDecoder = streamBitmapDecoder; + } + + @Override + public boolean handles(@NonNull File source, @NonNull Options options) + throws IOException + { + Log.w(TAG, "Checking item for encrypted Bitmap cache decoder: " + source.toString()); + + try (InputStream inputStream = createEncryptedInputStream(secret, source)) { + return streamBitmapDecoder.handles(inputStream, options); + } catch (IOException e) { + Log.w(TAG, e); + return false; + } + } + + @Nullable + @Override + public Resource decode(@NonNull File source, int width, int height, @NonNull Options options) + throws IOException + { + Log.w(TAG, "Encrypted Bitmap cache decoder running: " + source.toString()); + try (InputStream inputStream = createEncryptedInputStream(secret, source)) { + return streamBitmapDecoder.decode(inputStream, width, height, options); + } + } +} diff --git a/src/org/thoughtcrime/securesms/glide/cache/EncryptedBitmapResourceEncoder.java b/src/org/thoughtcrime/securesms/glide/cache/EncryptedBitmapResourceEncoder.java new file mode 100644 index 000000000..104bd4e55 --- /dev/null +++ b/src/org/thoughtcrime/securesms/glide/cache/EncryptedBitmapResourceEncoder.java @@ -0,0 +1,65 @@ +package org.thoughtcrime.securesms.glide.cache; + + +import android.graphics.Bitmap; +import android.support.annotation.NonNull; +import android.util.Log; + +import com.bumptech.glide.load.EncodeStrategy; +import com.bumptech.glide.load.Options; +import com.bumptech.glide.load.ResourceEncoder; +import com.bumptech.glide.load.engine.Resource; +import com.bumptech.glide.load.resource.bitmap.BitmapEncoder; + +import java.io.File; +import java.io.IOException; +import java.io.OutputStream; + +public class EncryptedBitmapResourceEncoder extends EncryptedCoder implements ResourceEncoder { + + private static final String TAG = EncryptedBitmapResourceEncoder.class.getSimpleName(); + + private final byte[] secret; + + public EncryptedBitmapResourceEncoder(@NonNull byte[] secret) { + this.secret = secret; + } + + @Override + public EncodeStrategy getEncodeStrategy(@NonNull Options options) { + return EncodeStrategy.TRANSFORMED; + } + + @SuppressWarnings("EmptyCatchBlock") + @Override + public boolean encode(@NonNull Resource data, @NonNull File file, @NonNull Options options) { + Log.w(TAG, "Encrypted resource encoder running: " + file.toString()); + + Bitmap bitmap = data.get(); + Bitmap.CompressFormat format = getFormat(bitmap, options); + int quality = options.get(BitmapEncoder.COMPRESSION_QUALITY); + + try (OutputStream os = createEncryptedOutputStream(secret, file)) { + bitmap.compress(format, quality, os); + os.close(); + return true; + } catch (IOException e) { + Log.w(TAG, e); + return false; + } + } + + private Bitmap.CompressFormat getFormat(Bitmap bitmap, Options options) { + Bitmap.CompressFormat format = options.get(BitmapEncoder.COMPRESSION_FORMAT); + + if (format != null) { + return format; + } else if (bitmap.hasAlpha()) { + return Bitmap.CompressFormat.PNG; + } else { + return Bitmap.CompressFormat.JPEG; + } + } + + +} diff --git a/src/org/thoughtcrime/securesms/glide/cache/EncryptedCacheEncoder.java b/src/org/thoughtcrime/securesms/glide/cache/EncryptedCacheEncoder.java new file mode 100644 index 000000000..b98f71ac7 --- /dev/null +++ b/src/org/thoughtcrime/securesms/glide/cache/EncryptedCacheEncoder.java @@ -0,0 +1,52 @@ +package org.thoughtcrime.securesms.glide.cache; + + +import android.support.annotation.NonNull; +import android.util.Log; + +import com.bumptech.glide.load.Encoder; +import com.bumptech.glide.load.Options; +import com.bumptech.glide.load.engine.bitmap_recycle.ArrayPool; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +public class EncryptedCacheEncoder extends EncryptedCoder implements Encoder { + + private static final String TAG = EncryptedCacheEncoder.class.getSimpleName(); + + private final byte[] secret; + private final ArrayPool byteArrayPool; + + public EncryptedCacheEncoder(@NonNull byte[] secret, @NonNull ArrayPool byteArrayPool) { + this.secret = secret; + this.byteArrayPool = byteArrayPool; + } + + @SuppressWarnings("EmptyCatchBlock") + @Override + public boolean encode(@NonNull InputStream data, @NonNull File file, @NonNull Options options) { + Log.w(TAG, "Encrypted cache encoder running: " + file.toString()); + + byte[] buffer = byteArrayPool.get(ArrayPool.STANDARD_BUFFER_SIZE_BYTES, byte[].class); + + try (OutputStream outputStream = createEncryptedOutputStream(secret, file)) { + int read; + + while ((read = data.read(buffer)) != -1) { + outputStream.write(buffer, 0, read); + } + + return true; + } catch (IOException e) { + Log.w(TAG, e); + return false; + } finally { + byteArrayPool.put(buffer); + } + } + + +} diff --git a/src/org/thoughtcrime/securesms/glide/cache/EncryptedCoder.java b/src/org/thoughtcrime/securesms/glide/cache/EncryptedCoder.java new file mode 100644 index 000000000..ed7f7d622 --- /dev/null +++ b/src/org/thoughtcrime/securesms/glide/cache/EncryptedCoder.java @@ -0,0 +1,98 @@ +package org.thoughtcrime.securesms.glide.cache; + + +import android.support.annotation.NonNull; + +import org.thoughtcrime.securesms.util.Util; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +import javax.crypto.Cipher; +import javax.crypto.CipherInputStream; +import javax.crypto.CipherOutputStream; +import javax.crypto.Mac; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.SecretKeySpec; + +class EncryptedCoder { + + private static byte[] MAGIC_BYTES = {(byte)0x91, (byte)0x5e, (byte)0x6d, (byte)0xb4, + (byte)0x09, (byte)0xa6, (byte)0x68, (byte)0xbe, + (byte)0xe5, (byte)0xb1, (byte)0x1b, (byte)0xd7, + (byte)0x29, (byte)0xe5, (byte)0x04, (byte)0xcc}; + + OutputStream createEncryptedOutputStream(@NonNull byte[] masterKey, @NonNull File file) + throws IOException + { + try { + byte[] random = Util.getSecretBytes(32); + Mac mac = Mac.getInstance("HmacSHA256"); + mac.init(new SecretKeySpec(masterKey, "HmacSHA256")); + + FileOutputStream fileOutputStream = new FileOutputStream(file); + byte[] iv = new byte[16]; + byte[] key = mac.doFinal(random); + + Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding"); + cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv)); + + fileOutputStream.write(MAGIC_BYTES); + fileOutputStream.write(random); + + CipherOutputStream outputStream = new CipherOutputStream(fileOutputStream, cipher); + outputStream.write(MAGIC_BYTES); + + return outputStream; + } catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException | InvalidAlgorithmParameterException e) { + throw new AssertionError(e); + } + } + + InputStream createEncryptedInputStream(@NonNull byte[] masterKey, @NonNull File file) throws IOException { + try { + Mac mac = Mac.getInstance("HmacSHA256"); + mac.init(new SecretKeySpec(masterKey, "HmacSHA256")); + + FileInputStream fileInputStream = new FileInputStream(file); + byte[] theirMagic = new byte[MAGIC_BYTES.length]; + byte[] theirRandom = new byte[32]; + byte[] theirEncryptedMagic = new byte[MAGIC_BYTES.length]; + + Util.readFully(fileInputStream, theirMagic); + Util.readFully(fileInputStream, theirRandom); + + if (!MessageDigest.isEqual(theirMagic, MAGIC_BYTES)) { + throw new IOException("Not an encrypted cache file!"); + } + + byte[] iv = new byte[16]; + byte[] key = mac.doFinal(theirRandom); + + Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding"); + cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv)); + + CipherInputStream inputStream = new CipherInputStream(fileInputStream, cipher); + Util.readFully(inputStream, theirEncryptedMagic); + + if (!MessageDigest.isEqual(theirEncryptedMagic, MAGIC_BYTES)) { + throw new IOException("Key change on encrypted cache file!"); + } + + return inputStream; + } catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException | InvalidAlgorithmParameterException e) { + throw new AssertionError(e); + } + } + + +} diff --git a/src/org/thoughtcrime/securesms/glide/cache/EncryptedGifCacheDecoder.java b/src/org/thoughtcrime/securesms/glide/cache/EncryptedGifCacheDecoder.java new file mode 100644 index 000000000..03ea73817 --- /dev/null +++ b/src/org/thoughtcrime/securesms/glide/cache/EncryptedGifCacheDecoder.java @@ -0,0 +1,51 @@ +package org.thoughtcrime.securesms.glide.cache; + + +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.util.Log; + +import com.bumptech.glide.load.Options; +import com.bumptech.glide.load.ResourceDecoder; +import com.bumptech.glide.load.engine.Resource; +import com.bumptech.glide.load.resource.gif.GifDrawable; +import com.bumptech.glide.load.resource.gif.StreamGifDecoder; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; + +public class EncryptedGifCacheDecoder extends EncryptedCoder implements ResourceDecoder { + + private static final String TAG = EncryptedGifCacheDecoder.class.getSimpleName(); + + private final byte[] secret; + private final StreamGifDecoder gifDecoder; + + public EncryptedGifCacheDecoder(@NonNull byte[] secret, @NonNull StreamGifDecoder gifDecoder) { + this.secret = secret; + this.gifDecoder = gifDecoder; + } + + @Override + public boolean handles(@NonNull File source, @NonNull Options options) { + Log.w(TAG, "Checking item for encrypted GIF cache decoder: " + source.toString()); + + try (InputStream inputStream = createEncryptedInputStream(secret, source)) { + return gifDecoder.handles(inputStream, options); + } catch (IOException e) { + Log.w(TAG, e); + return false; + } + } + + @Nullable + @Override + public Resource decode(@NonNull File source, int width, int height, @NonNull Options options) throws IOException { + Log.w(TAG, "Encrypted GIF cache decoder running..."); + try (InputStream inputStream = createEncryptedInputStream(secret, source)) { + return gifDecoder.decode(inputStream, width, height, options); + } + } + +} diff --git a/src/org/thoughtcrime/securesms/glide/cache/EncryptedGifDrawableResourceEncoder.java b/src/org/thoughtcrime/securesms/glide/cache/EncryptedGifDrawableResourceEncoder.java new file mode 100644 index 000000000..5a72de5a5 --- /dev/null +++ b/src/org/thoughtcrime/securesms/glide/cache/EncryptedGifDrawableResourceEncoder.java @@ -0,0 +1,45 @@ +package org.thoughtcrime.securesms.glide.cache; + + +import android.support.annotation.NonNull; +import android.util.Log; + +import com.bumptech.glide.load.EncodeStrategy; +import com.bumptech.glide.load.Options; +import com.bumptech.glide.load.ResourceEncoder; +import com.bumptech.glide.load.engine.Resource; +import com.bumptech.glide.load.resource.gif.GifDrawable; +import com.bumptech.glide.util.ByteBufferUtil; + +import java.io.File; +import java.io.IOException; +import java.io.OutputStream; + +public class EncryptedGifDrawableResourceEncoder extends EncryptedCoder implements ResourceEncoder { + + private static final String TAG = EncryptedGifDrawableResourceEncoder.class.getSimpleName(); + + private final byte[] secret; + + public EncryptedGifDrawableResourceEncoder(@NonNull byte[] secret) { + this.secret = secret; + } + + @Override + public EncodeStrategy getEncodeStrategy(@NonNull Options options) { + return EncodeStrategy.TRANSFORMED; + } + + @Override + public boolean encode(@NonNull Resource data, @NonNull File file, @NonNull Options options) { + GifDrawable drawable = data.get(); + + try (OutputStream outputStream = createEncryptedOutputStream(secret, file)) { + ByteBufferUtil.toStream(drawable.getBuffer(), outputStream); + return true; + } catch (IOException e) { + Log.w(TAG, e); + return false; + } + } +} diff --git a/src/org/thoughtcrime/securesms/mms/SignalGlideModule.java b/src/org/thoughtcrime/securesms/mms/SignalGlideModule.java index 948589d78..3b5c5c7e3 100644 --- a/src/org/thoughtcrime/securesms/mms/SignalGlideModule.java +++ b/src/org/thoughtcrime/securesms/mms/SignalGlideModule.java @@ -9,11 +9,24 @@ import com.bumptech.glide.Glide; import com.bumptech.glide.GlideBuilder; import com.bumptech.glide.Registry; import com.bumptech.glide.annotation.GlideModule; +import com.bumptech.glide.load.model.GlideUrl; import com.bumptech.glide.load.model.UnitModelLoader; +import com.bumptech.glide.load.resource.bitmap.Downsampler; +import com.bumptech.glide.load.resource.bitmap.StreamBitmapDecoder; +import com.bumptech.glide.load.resource.gif.ByteBufferGifDecoder; +import com.bumptech.glide.load.resource.gif.GifDrawable; +import com.bumptech.glide.load.resource.gif.StreamGifDecoder; import com.bumptech.glide.module.AppGlideModule; import org.thoughtcrime.securesms.contacts.avatars.ContactPhoto; +import org.thoughtcrime.securesms.crypto.AttachmentSecret; +import org.thoughtcrime.securesms.crypto.AttachmentSecretProvider; import org.thoughtcrime.securesms.glide.ContactPhotoLoader; +import org.thoughtcrime.securesms.glide.cache.EncryptedBitmapCacheDecoder; +import org.thoughtcrime.securesms.glide.cache.EncryptedBitmapResourceEncoder; +import org.thoughtcrime.securesms.glide.cache.EncryptedCacheEncoder; +import org.thoughtcrime.securesms.glide.cache.EncryptedGifCacheDecoder; +import org.thoughtcrime.securesms.glide.cache.EncryptedGifDrawableResourceEncoder; import org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader.DecryptableUri; import java.io.File; @@ -35,16 +48,16 @@ public class SignalGlideModule extends AppGlideModule { @Override public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) { - //AttachmentSecret attachmentSecret = AttachmentSecretProvider.getInstance(context).getOrCreateAttachmentSecret(); - //byte[] secret = attachmentSecret.getModernKey(); + AttachmentSecret attachmentSecret = AttachmentSecretProvider.getInstance(context).getOrCreateAttachmentSecret(); + byte[] secret = attachmentSecret.getModernKey(); registry.prepend(File.class, File.class, UnitModelLoader.Factory.getInstance()); - //registry.prepend(InputStream.class, new EncryptedCacheEncoder(secret, glide.getArrayPool())); - //registry.prepend(File.class, Bitmap.class, new EncryptedBitmapCacheDecoder(secret, new StreamBitmapDecoder(new Downsampler(registry.getImageHeaderParsers(), context.getResources().getDisplayMetrics(), glide.getBitmapPool(), glide.getArrayPool()), glide.getArrayPool()))); - //registry.prepend(File.class, GifDrawable.class, new EncryptedGifCacheDecoder(secret, new StreamGifDecoder(registry.getImageHeaderParsers(), new ByteBufferGifDecoder(context, registry.getImageHeaderParsers(), glide.getBitmapPool(), glide.getArrayPool()), glide.getArrayPool()))); + registry.prepend(InputStream.class, new EncryptedCacheEncoder(secret, glide.getArrayPool())); + registry.prepend(File.class, Bitmap.class, new EncryptedBitmapCacheDecoder(secret, new StreamBitmapDecoder(new Downsampler(registry.getImageHeaderParsers(), context.getResources().getDisplayMetrics(), glide.getBitmapPool(), glide.getArrayPool()), glide.getArrayPool()))); + registry.prepend(File.class, GifDrawable.class, new EncryptedGifCacheDecoder(secret, new StreamGifDecoder(registry.getImageHeaderParsers(), new ByteBufferGifDecoder(context, registry.getImageHeaderParsers(), glide.getBitmapPool(), glide.getArrayPool()), glide.getArrayPool()))); - //registry.prepend(Bitmap.class, new EncryptedBitmapResourceEncoder(secret)); - //registry.prepend(GifDrawable.class, new EncryptedGifDrawableResourceEncoder(secret)); + registry.prepend(Bitmap.class, new EncryptedBitmapResourceEncoder(secret)); + registry.prepend(GifDrawable.class, new EncryptedGifDrawableResourceEncoder(secret)); registry.append(ContactPhoto.class, InputStream.class, new ContactPhotoLoader.Factory(context)); registry.append(DecryptableUri.class, InputStream.class, new DecryptableStreamUriLoader.Factory(context));