From 75040136ae5ca6108335975430b411f8a560d0ba Mon Sep 17 00:00:00 2001 From: Tad Fisher Date: Sun, 9 Oct 2022 16:10:42 -0700 Subject: Add decryption callback to CryptoHandler --- app/src/main/java/app/passwordstore/Application.kt | 11 +++++++---- .../passwordstore/data/crypto/CryptoRepository.kt | 10 +++++++++- .../injection/crypto/CryptoHandlerModule.kt | 20 ++++++++++++++++++-- 3 files changed, 34 insertions(+), 7 deletions(-) (limited to 'app/src') diff --git a/app/src/main/java/app/passwordstore/Application.kt b/app/src/main/java/app/passwordstore/Application.kt index fb4c0f63..9ec32f15 100644 --- a/app/src/main/java/app/passwordstore/Application.kt +++ b/app/src/main/java/app/passwordstore/Application.kt @@ -12,6 +12,7 @@ import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES +import app.passwordstore.crypto.HWSecurityManager import app.passwordstore.injection.context.FilesDirPath import app.passwordstore.injection.prefs.SettingsPreferences import app.passwordstore.util.extensions.getString @@ -43,14 +44,15 @@ class Application : android.app.Application(), SharedPreferences.OnSharedPrefere @Inject lateinit var proxyUtils: ProxyUtils @Inject lateinit var gitSettings: GitSettings @Inject lateinit var features: Features + @Inject lateinit var deviceManager: HWSecurityManager override fun onCreate() { super.onCreate() instance = this - if ( - BuildConfig.ENABLE_DEBUG_FEATURES || - prefs.getBoolean(PreferenceKeys.ENABLE_DEBUG_LOGGING, false) - ) { + + val enableLogging = BuildConfig.ENABLE_DEBUG_FEATURES || + prefs.getBoolean(PreferenceKeys.ENABLE_DEBUG_LOGGING, false) + if (enableLogging) { LogcatLogger.install(AndroidLogcatLogger(DEBUG)) setVmPolicy() } @@ -60,6 +62,7 @@ class Application : android.app.Application(), SharedPreferences.OnSharedPrefere runMigrations(filesDirPath, prefs, gitSettings) proxyUtils.setDefaultProxy() DynamicColors.applyToActivitiesIfAvailable(this) + deviceManager.init(enableLogging) Sentry.configureScope { scope -> val user = User() user.data = diff --git a/app/src/main/java/app/passwordstore/data/crypto/CryptoRepository.kt b/app/src/main/java/app/passwordstore/data/crypto/CryptoRepository.kt index b673e94c..9f8dfbd5 100644 --- a/app/src/main/java/app/passwordstore/data/crypto/CryptoRepository.kt +++ b/app/src/main/java/app/passwordstore/data/crypto/CryptoRepository.kt @@ -6,16 +6,19 @@ package app.passwordstore.data.crypto import app.passwordstore.crypto.GpgIdentifier +import app.passwordstore.crypto.HWSecurityDeviceHandler import app.passwordstore.crypto.PGPKeyManager import app.passwordstore.crypto.PGPainlessCryptoHandler import app.passwordstore.crypto.errors.CryptoHandlerException import com.github.michaelbull.result.Result import com.github.michaelbull.result.getAll +import com.github.michaelbull.result.getOrThrow import com.github.michaelbull.result.unwrap import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream import javax.inject.Inject import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext class CryptoRepository @@ -23,6 +26,7 @@ class CryptoRepository constructor( private val pgpKeyManager: PGPKeyManager, private val pgpCryptoHandler: PGPainlessCryptoHandler, + private val deviceHandler: HWSecurityDeviceHandler ) { suspend fun decrypt( @@ -43,7 +47,11 @@ constructor( out: ByteArrayOutputStream, ): Result { val keys = pgpKeyManager.getAllKeys().unwrap() - return pgpCryptoHandler.decrypt(keys, password, message, out) + return pgpCryptoHandler.decrypt(keys, password, message, out) { encryptedSessionKey -> + runBlocking { + deviceHandler.decryptSessionKey(encryptedSessionKey).getOrThrow() + } + } } private suspend fun encryptPgp( diff --git a/app/src/main/java/app/passwordstore/injection/crypto/CryptoHandlerModule.kt b/app/src/main/java/app/passwordstore/injection/crypto/CryptoHandlerModule.kt index 5a863d8d..6eca052f 100644 --- a/app/src/main/java/app/passwordstore/injection/crypto/CryptoHandlerModule.kt +++ b/app/src/main/java/app/passwordstore/injection/crypto/CryptoHandlerModule.kt @@ -5,14 +5,30 @@ package app.passwordstore.injection.crypto +import android.app.Activity +import androidx.fragment.app.FragmentActivity +import app.passwordstore.crypto.HWSecurityDeviceHandler +import app.passwordstore.crypto.HWSecurityManager import app.passwordstore.crypto.PGPainlessCryptoHandler import dagger.Module import dagger.Provides import dagger.hilt.InstallIn -import dagger.hilt.components.SingletonComponent +import dagger.hilt.android.components.ActivityComponent +import dagger.hilt.android.scopes.ActivityScoped @Module -@InstallIn(SingletonComponent::class) +@InstallIn(ActivityComponent::class) object CryptoHandlerModule { + + @Provides + @ActivityScoped + fun provideDeviceHandler( + activity: Activity, + deviceManager: HWSecurityManager + ): HWSecurityDeviceHandler = HWSecurityDeviceHandler( + deviceManager = deviceManager, + fragmentManager = (activity as FragmentActivity).supportFragmentManager + ) + @Provides fun providePgpCryptoHandler() = PGPainlessCryptoHandler() } -- cgit v1.2.3