Handle less generic exceptions in catch clauses

This commit is contained in:
Julian Raufelder 2021-07-06 12:49:25 +02:00
parent d82bcaa3e8
commit 210d0fa7f4
No known key found for this signature in database
GPG Key ID: 17EE71F6634E381D
9 changed files with 50 additions and 45 deletions

View File

@ -116,11 +116,10 @@ class VaultConfig private constructor(builder: VaultConfigBuilder) {
.shorteningThreshold(parser.body[JSON_KEY_SHORTENING_THRESHOLD] as Int) .shorteningThreshold(parser.body[JSON_KEY_SHORTENING_THRESHOLD] as Int)
VaultConfig(vaultConfigBuilder) VaultConfig(vaultConfigBuilder)
} catch (e: Exception) { } catch (e: JwtException) {
when (e) { when (e) {
is MissingClaimException, is IncorrectClaimException -> throw VaultVersionMismatchException("Vault config not for version " + unverifiedVaultConfig.vaultFormat) is MissingClaimException, is IncorrectClaimException -> throw VaultVersionMismatchException("Vault config not for version " + unverifiedVaultConfig.vaultFormat)
is SignatureException -> throw VaultKeyInvalidException() is SignatureException -> throw VaultKeyInvalidException()
is JwtException -> throw VaultConfigLoadException("Failed to verify vault config", e)
else -> throw VaultConfigLoadException(e) else -> throw VaultConfigLoadException(e)
} }
} }

View File

@ -110,9 +110,9 @@ internal class OnedriveCloudContentRepository(private val cloud: OnedriveCloud,
} }
@Throws(BackendException::class) @Throws(BackendException::class)
override fun read(file: OnedriveFile, tmpEncryptedFile: File?, data: OutputStream, progressAware: ProgressAware<DownloadState>) { override fun read(file: OnedriveFile, encryptedTmpFile: File?, data: OutputStream, progressAware: ProgressAware<DownloadState>) {
try { try {
oneDriveImpl.read(file, tmpEncryptedFile, data, progressAware) oneDriveImpl.read(file, encryptedTmpFile, data, progressAware)
} catch (e: IOException) { } catch (e: IOException) {
when { when {
ExceptionUtil.contains(e, NoSuchCloudFileException::class.java) -> { ExceptionUtil.contains(e, NoSuchCloudFileException::class.java) -> {

View File

@ -144,7 +144,7 @@ public class UpdateCheckRepositoryImpl implements UpdateCheckRepository {
} }
} }
return new String(Hex.encodeHex(digest.digest())); return new String(Hex.encodeHex(digest.digest()));
} catch (Exception e) { } catch (NoSuchAlgorithmException | IOException e) {
throw new GeneralUpdateErrorException(e); throw new GeneralUpdateErrorException(e);
} }
} }
@ -272,7 +272,7 @@ public class UpdateCheckRepositoryImpl implements UpdateCheckRepository {
urlApk = jws.get("url", String.class); urlApk = jws.get("url", String.class);
apkSha256 = jws.get("apk_sha_256", String.class); apkSha256 = jws.get("apk_sha_256", String.class);
urlReleaseNote = jws.get("release_notes", String.class); urlReleaseNote = jws.get("release_notes", String.class);
} catch (Exception e) { } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw new GeneralUpdateErrorException("Failed to parse latest version", e); throw new GeneralUpdateErrorException("Failed to parse latest version", e);
} }
} }

View File

@ -7,6 +7,7 @@ import org.cryptomator.presentation.R
import org.cryptomator.presentation.model.S3CloudModel import org.cryptomator.presentation.model.S3CloudModel
import org.cryptomator.presentation.presenter.S3AddOrChangePresenter import org.cryptomator.presentation.presenter.S3AddOrChangePresenter
import org.cryptomator.util.crypto.CredentialCryptor import org.cryptomator.util.crypto.CredentialCryptor
import org.cryptomator.util.crypto.FatalCryptoException
import javax.inject.Inject import javax.inject.Inject
import kotlinx.android.synthetic.main.fragment_setup_s3.accessKeyEditText import kotlinx.android.synthetic.main.fragment_setup_s3.accessKeyEditText
import kotlinx.android.synthetic.main.fragment_setup_s3.bucketEditText import kotlinx.android.synthetic.main.fragment_setup_s3.bucketEditText
@ -58,7 +59,7 @@ class S3AddOrChangeFragment : BaseFragment() {
CredentialCryptor // CredentialCryptor //
.getInstance(activity?.applicationContext) // .getInstance(activity?.applicationContext) //
.decrypt(text) .decrypt(text)
} catch (e: RuntimeException) { } catch (e: FatalCryptoException) {
Timber.tag("S3AddOrChangeFragment").e(e, "Unable to decrypt password, clearing it") Timber.tag("S3AddOrChangeFragment").e(e, "Unable to decrypt password, clearing it")
"" ""
} }

View File

@ -7,6 +7,7 @@ import org.cryptomator.presentation.R
import org.cryptomator.presentation.model.WebDavCloudModel import org.cryptomator.presentation.model.WebDavCloudModel
import org.cryptomator.presentation.presenter.WebDavAddOrChangePresenter import org.cryptomator.presentation.presenter.WebDavAddOrChangePresenter
import org.cryptomator.util.crypto.CredentialCryptor import org.cryptomator.util.crypto.CredentialCryptor
import org.cryptomator.util.crypto.FatalCryptoException
import javax.inject.Inject import javax.inject.Inject
import kotlinx.android.synthetic.main.fragment_setup_webdav.createCloudButton import kotlinx.android.synthetic.main.fragment_setup_webdav.createCloudButton
import kotlinx.android.synthetic.main.fragment_setup_webdav.passwordEditText import kotlinx.android.synthetic.main.fragment_setup_webdav.passwordEditText
@ -55,7 +56,7 @@ class WebDavAddOrChangeFragment : BaseFragment() {
CredentialCryptor // CredentialCryptor //
.getInstance(activity?.applicationContext) // .getInstance(activity?.applicationContext) //
.decrypt(password) .decrypt(password)
} catch (e: RuntimeException) { } catch (e: FatalCryptoException) {
Timber.tag("WebdavAddOrCangeFragmnt").e(e, "Unable to decrypt password, clearing it") Timber.tag("WebdavAddOrCangeFragmnt").e(e, "Unable to decrypt password, clearing it")
"" ""
} }

View File

@ -208,24 +208,20 @@ class FileUtil @Inject constructor(private val context: Context, private val mim
*/ */
private fun tryRecoverAutoUploadFilesStoreDueToFileObfuscation(file: File): AutoUploadFilesStore { private fun tryRecoverAutoUploadFilesStoreDueToFileObfuscation(file: File): AutoUploadFilesStore {
Timber.tag("FileUtil").i("Try to recover AutoUploadFilesStore using class c or a") Timber.tag("FileUtil").i("Try to recover AutoUploadFilesStore using class c or a")
try { ObjectInputStream(FileInputStream(file)).use { objectInputStream ->
ObjectInputStream(FileInputStream(file)).use { objectInputStream -> val uploadPaths = when (val obj = objectInputStream.readObject()) {
val uploadPaths = when (val obj = objectInputStream.readObject()) { is org.cryptomator.presentation.e.c -> obj.mE() // version 1.5.10
is org.cryptomator.presentation.e.c -> obj.mE() // version 1.5.10 is org.cryptomator.presentation.i.a -> obj.b() // version 1.5.11-beta1
is org.cryptomator.presentation.i.a -> obj.b() // version 1.5.11-beta1 else -> null
else -> null }
} when {
when { uploadPaths != null -> {
uploadPaths != null -> { Timber.tag("FileUtil").i("Nailed it! Successfully recovered AutoUploadFilesStore!")
Timber.tag("FileUtil").i("Nailed it! Successfully recovered AutoUploadFilesStore!") file.delete()
file.delete() return AutoUploadFilesStore(uploadPaths)
return AutoUploadFilesStore(uploadPaths) }
} else -> throw FatalBackendException("Failed to recover AutoUploadFilesStore")
else -> throw FatalBackendException("Failed to recover AutoUploadFilesStore")
}
} }
} catch (e: Exception) {
throw FatalBackendException("Failed to recover AutoUploadFilesStore", e)
} }
} }

View File

@ -3,6 +3,8 @@ package org.cryptomator.util.crypto;
import java.security.InvalidAlgorithmParameterException; import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.IvParameterSpec;
@ -22,20 +24,14 @@ class CipherImpl implements Cipher {
private static byte[] mergeIvAndEncryptedData(byte[] encrypted, byte[] iv) { private static byte[] mergeIvAndEncryptedData(byte[] encrypted, byte[] iv) {
byte[] mergedIvAndEncrypted = new byte[encrypted.length + iv.length]; byte[] mergedIvAndEncrypted = new byte[encrypted.length + iv.length];
arraycopy( // arraycopy(iv, 0, mergedIvAndEncrypted, 0, IV_LENGTH);
iv, 0, // arraycopy(encrypted, 0, mergedIvAndEncrypted, IV_LENGTH, encrypted.length);
mergedIvAndEncrypted, 0, IV_LENGTH);
arraycopy( //
encrypted, 0, //
mergedIvAndEncrypted, IV_LENGTH, encrypted.length);
return mergedIvAndEncrypted; return mergedIvAndEncrypted;
} }
static byte[] getBytes(byte[] encryptedBytesWithIv) { static byte[] getBytes(byte[] encryptedBytesWithIv) {
byte[] bytes = new byte[encryptedBytesWithIv.length - IV_LENGTH]; byte[] bytes = new byte[encryptedBytesWithIv.length - IV_LENGTH];
arraycopy( // arraycopy(encryptedBytesWithIv, IV_LENGTH, bytes, 0, bytes.length);
encryptedBytesWithIv, IV_LENGTH, //
bytes, 0, bytes.length);
return bytes; return bytes;
} }
@ -45,8 +41,8 @@ class CipherImpl implements Cipher {
cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, key); cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, key);
byte[] encrypted = cipher.doFinal(data); byte[] encrypted = cipher.doFinal(data);
return mergeIvAndEncryptedData(encrypted, cipher.getIV()); return mergeIvAndEncryptedData(encrypted, cipher.getIV());
} catch (Exception e) { } catch (InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) {
throw new RuntimeException(e); throw new FatalCryptoException(e);
} }
} }
@ -58,8 +54,8 @@ class CipherImpl implements Cipher {
IvParameterSpec ivspec = new IvParameterSpec(iv); IvParameterSpec ivspec = new IvParameterSpec(iv);
cipher.init(javax.crypto.Cipher.DECRYPT_MODE, key, ivspec); cipher.init(javax.crypto.Cipher.DECRYPT_MODE, key, ivspec);
return cipher.doFinal(bytes); return cipher.doFinal(bytes);
} catch (Exception e) { } catch (InvalidKeyException | BadPaddingException | IllegalBlockSizeException | InvalidAlgorithmParameterException e) {
throw new RuntimeException(e); throw new FatalCryptoException(e);
} }
} }
@ -79,9 +75,7 @@ class CipherImpl implements Cipher {
private byte[] getIv(byte[] encryptedBytesWithIv) { private byte[] getIv(byte[] encryptedBytesWithIv) {
byte[] iv = new byte[IV_LENGTH]; byte[] iv = new byte[IV_LENGTH];
arraycopy( // arraycopy(encryptedBytesWithIv, 0, iv, 0, IV_LENGTH);
encryptedBytesWithIv, 0, //
iv, 0, IV_LENGTH);
return iv; return iv;
} }

View File

@ -6,8 +6,12 @@ import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyProperties; import android.security.keystore.KeyProperties;
import java.security.KeyStore; import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.UnrecoverableKeyException; import java.security.UnrecoverableKeyException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
class CryptoOperationsImpl implements CryptoOperations { class CryptoOperationsImpl implements CryptoOperations {
@ -22,8 +26,8 @@ class CryptoOperationsImpl implements CryptoOperations {
return new CipherImpl(cipher, key); return new CipherImpl(cipher, key);
} catch (UnrecoverableKeyException e) { } catch (UnrecoverableKeyException e) {
throw new UnrecoverableStorageKeyException(e); throw new UnrecoverableStorageKeyException(e);
} catch (Exception e) { } catch (NoSuchPaddingException | NoSuchAlgorithmException | KeyStoreException e) {
throw new RuntimeException(e); throw new FatalCryptoException(e);
} }
} }
@ -32,8 +36,8 @@ class CryptoOperationsImpl implements CryptoOperations {
final javax.crypto.KeyGenerator generator; final javax.crypto.KeyGenerator generator;
try { try {
generator = javax.crypto.KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, KeyStoreBuilder.DEFAULT_KEYSTORE_NAME); generator = javax.crypto.KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, KeyStoreBuilder.DEFAULT_KEYSTORE_NAME);
} catch (Exception e) { } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
throw new RuntimeException(e); throw new FatalCryptoException(e);
} }
return requireUserAuthentication -> { return requireUserAuthentication -> {
KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec // KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec //

View File

@ -0,0 +1,10 @@
package org.cryptomator.util.crypto;
import java.security.GeneralSecurityException;
public class FatalCryptoException extends RuntimeException {
public FatalCryptoException(GeneralSecurityException e) {
super(e);
}
}