diff options
author | Christian Schneppe <christian@pix-art.de> | 2018-10-04 13:22:05 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2018-10-04 13:22:05 +0200 |
commit | 431b6204cd2ed291b81943b46ea9c2851cb2ec1a (patch) | |
tree | a5e34ccd6889b1b3de2b3fd635601f432b5ec6ae /src/main/java/de/pixart/messenger/services/AbstractConnectionManager.java | |
parent | 219d5f113bf809b2e5929256d4a9812a4aa21f8c (diff) |
refactored file encryption to give access to inner stream
Conscrypt on some plattforms doesn’t like when we close the CipherInputStream. Therefor we refactor the api to give us access to the inner stream so we can close that independently.
Diffstat (limited to '')
-rw-r--r-- | src/main/java/de/pixart/messenger/services/AbstractConnectionManager.java | 27 |
1 files changed, 12 insertions, 15 deletions
diff --git a/src/main/java/de/pixart/messenger/services/AbstractConnectionManager.java b/src/main/java/de/pixart/messenger/services/AbstractConnectionManager.java index 65711ef42..a65a990c8 100644 --- a/src/main/java/de/pixart/messenger/services/AbstractConnectionManager.java +++ b/src/main/java/de/pixart/messenger/services/AbstractConnectionManager.java @@ -3,18 +3,21 @@ package de.pixart.messenger.services; import android.content.Context; import android.os.PowerManager; import android.os.SystemClock; -import android.util.Pair; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; import java.util.concurrent.atomic.AtomicLong; import javax.crypto.Cipher; import javax.crypto.CipherInputStream; import javax.crypto.CipherOutputStream; +import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; @@ -34,32 +37,26 @@ public class AbstractConnectionManager { this.mXmppConnectionService = service; } - public static Pair<InputStream, Integer> createInputStream(DownloadableFile file, boolean gcm) throws FileNotFoundException { - FileInputStream is; - int size; - is = new FileInputStream(file); - size = (int) file.getSize(); - if (file.getKey() == null) { - return new Pair<>(is, size); - } - try { + public static InputStream upgrade(DownloadableFile file, InputStream is, boolean gcm) throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, NoSuchProviderException { + if (file.getKey() != null && file.getIv() != null) { if (gcm) { Cipher cipher = Compatibility.twentyTwo() ? Cipher.getInstance(CIPHERMODE) : Cipher.getInstance(CIPHERMODE, PROVIDER); SecretKeySpec keySpec = new SecretKeySpec(file.getKey(), KEYTYPE); IvParameterSpec ivSpec = new IvParameterSpec(file.getIv()); cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); - return new Pair<>(new CipherInputStream(is, cipher), cipher.getOutputSize(size)); + return new CipherInputStream(is, cipher); } else { IvParameterSpec ips = new IvParameterSpec(file.getIv()); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(file.getKey(), KEYTYPE), ips); - return new Pair<>(new CipherInputStream(is, cipher), (size / 16 + 1) * 16); + return new CipherInputStream(is, cipher); } - } catch (Exception e) { - throw new AssertionError(e); + } else { + return is; } } + public static OutputStream createAppendedOutputStream(DownloadableFile file) { return createOutputStream(file, false, true); } |