From d9cd4641f2bc3678cc232bf3358b0f40b04d9dee Mon Sep 17 00:00:00 2001 From: mdoucleff Date: Mon, 14 Aug 2017 10:35:37 -0700 Subject: [PATCH] Add flag to CachedContentIndex to disable encryption. This allows the encryption feature to be disabled gracefully: encrypted index files may be read, but plaintext will be written. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=165196508 --- .../upstream/cache/CachedContentIndex.java | 21 +++++++++++++++---- .../upstream/cache/SimpleCache.java | 16 +++++++++++++- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CachedContentIndex.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CachedContentIndex.java index 10bc298579..e1c2c13865 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CachedContentIndex.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CachedContentIndex.java @@ -64,6 +64,7 @@ import javax.crypto.spec.SecretKeySpec; private final AtomicFile atomicFile; private final Cipher cipher; private final SecretKeySpec secretKeySpec; + private final boolean encrypt; private boolean changed; private ReusableBufferedOutputStream bufferedOutputStream; @@ -80,10 +81,21 @@ import javax.crypto.spec.SecretKeySpec; * Creates a CachedContentIndex which works on the index file in the given cacheDir. * * @param cacheDir Directory where the index file is kept. - * @param secretKey If not null, cache keys will be stored encrypted on filesystem using AES/CBC. - * The key must be 16 bytes long. + * @param secretKey 16 byte AES key for reading and writing the cache index. */ public CachedContentIndex(File cacheDir, byte[] secretKey) { + this(cacheDir, secretKey, secretKey != null); + } + + /** + * Creates a CachedContentIndex which works on the index file in the given cacheDir. + * + * @param cacheDir Directory where the index file is kept. + * @param secretKey 16 byte AES key for reading, and optionally writing, the cache index. + * @param encrypt When false, a plaintext index will be written. + */ + public CachedContentIndex(File cacheDir, byte[] secretKey, boolean encrypt) { + this.encrypt = encrypt; if (secretKey != null) { Assertions.checkArgument(secretKey.length == 16); try { @@ -288,10 +300,11 @@ import javax.crypto.spec.SecretKeySpec; output = new DataOutputStream(bufferedOutputStream); output.writeInt(VERSION); - int flags = cipher != null ? FLAG_ENCRYPTED_INDEX : 0; + boolean writeEncrypted = encrypt && cipher != null; + int flags = writeEncrypted ? FLAG_ENCRYPTED_INDEX : 0; output.writeInt(flags); - if (cipher != null) { + if (writeEncrypted) { byte[] initializationVector = new byte[16]; new Random().nextBytes(initializationVector); output.write(initializationVector); diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCache.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCache.java index 62bd2783b8..bb1ac83698 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCache.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCache.java @@ -60,10 +60,24 @@ public final class SimpleCache implements Cache { * The key must be 16 bytes long. */ public SimpleCache(File cacheDir, CacheEvictor evictor, byte[] secretKey) { + this(cacheDir, evictor, secretKey, secretKey != null); + } + + /** + * Constructs the cache. The cache will delete any unrecognized files from the directory. Hence + * the directory cannot be used to store other files. + * + * @param cacheDir A dedicated cache directory. + * @param evictor The evictor to be used. + * @param secretKey If not null, cache keys will be stored encrypted on filesystem using AES/CBC. + * The key must be 16 bytes long. + * @param encrypt When false, a plaintext index will be written. + */ + public SimpleCache(File cacheDir, CacheEvictor evictor, byte[] secretKey, boolean encrypt) { this.cacheDir = cacheDir; this.evictor = evictor; this.lockedSpans = new HashMap<>(); - this.index = new CachedContentIndex(cacheDir, secretKey); + this.index = new CachedContentIndex(cacheDir, secretKey, encrypt); this.listeners = new HashMap<>(); // Start cache initialization. final ConditionVariable conditionVariable = new ConditionVariable();