Skip to content

StreamVault is a TV-first free IPTV player for Android TV built with Kotlin, Jetpack Compose, Room, Hilt, and Media3.

fix: handle credential encryption fallback to software key on devices with broken hardware-backed Keystore

David Nashash037318c30784
diff --git a/data/src/main/java/com/streamvault/data/repository/ProviderRepositoryImpl.kt b/data/src/main/java/com/streamvault/data/repository/ProviderRepositoryImpl.kt
index ec477fd2..b1b9e053 100644
--- a/data/src/main/java/com/streamvault/data/repository/ProviderRepositoryImpl.kt
+++ b/data/src/main/java/com/streamvault/data/repository/ProviderRepositoryImpl.kt
@@ -502,7 +502,7 @@ class ProviderRepositoryImpl @Inject constructor(
channelLogoSourcePolicy: ChannelLogoSourcePolicy = ChannelLogoSourcePolicy.SUPPLIER_PREFERRED,
onProgress: ((String) -> Unit)? = null,
id: Long? = null
- ): Result<Provider> {
+ ): Result<Provider> = try {
val normalizedServerUrl = ProviderInputSanitizer.normalizeUrl(serverUrl)
val normalizedUsername = ProviderInputSanitizer.normalizeUsername(username)
val normalizedName = ProviderInputSanitizer.normalizeProviderName(name)
@@ -567,7 +567,7 @@ class ProviderRepositoryImpl @Inject constructor(
is CapabilityResolution.Restricted -> return Result.error(resolution.reason)
is CapabilityResolution.Unsupported -> return Result.error(resolution.reason)
}
- return when (val authResult = provider.authenticate()) {
+ when (val authResult = provider.authenticate()) {
is Result.Success -> {
onProgress?.invoke("Profile accepted; catalog validated")
val onboardingTarget = if (existingProvider != null) {
@@ -634,6 +634,8 @@ class ProviderRepositoryImpl @Inject constructor(
is Result.Error -> Result.error(authResult.message, authResult.exception)
is Result.Loading -> Result.error("Unexpected loading state")
}
+ } catch (e: Exception) {
+ Result.error("Failed to add Xtream provider: ${e.message}", e)
}
internal suspend fun validateM3u(
diff --git a/data/src/main/java/com/streamvault/data/security/CredentialCrypto.kt b/data/src/main/java/com/streamvault/data/security/CredentialCrypto.kt
index 29c8b638..ad336798 100644
--- a/data/src/main/java/com/streamvault/data/security/CredentialCrypto.kt
+++ b/data/src/main/java/com/streamvault/data/security/CredentialCrypto.kt
@@ -1,13 +1,17 @@
package com.streamvault.data.security
+import android.content.Context
import android.security.keystore.KeyGenParameterSpec
import android.security.keystore.KeyProperties
import android.util.Log
+import dagger.hilt.android.qualifiers.ApplicationContext
+import java.io.File
import java.security.KeyStore
import javax.crypto.Cipher
import javax.crypto.KeyGenerator
import javax.crypto.SecretKey
import javax.crypto.spec.GCMParameterSpec
+import javax.crypto.spec.SecretKeySpec
import javax.inject.Inject
import javax.inject.Singleton
@@ -26,7 +30,9 @@ interface CredentialCrypto {
* Values are persisted as: enc:v1:<base64(iv + ciphertext)>
*/
@Singleton
-class AndroidKeystoreCredentialCrypto @Inject constructor() : CredentialCrypto {
+class AndroidKeystoreCredentialCrypto @Inject constructor(
+ @ApplicationContext private val context: Context
+) : CredentialCrypto {
private val TAG = "CredentialCrypto"
private val KEYSTORE_TYPE = "AndroidKeyStore"
private val KEY_ALIAS = "streamvault_credentials"
@@ -34,6 +40,9 @@ class AndroidKeystoreCredentialCrypto @Inject constructor() : CredentialCrypto {
private val IV_SIZE_BYTES = 12
private val AUTH_TAG_BITS = 128
private val PREFIX = "enc:v1:"
+ private val SOFTWARE_KEY_FILE_NAME = "streamvault_credential_software.key"
+ private val SOFTWARE_KEY_ALGORITHM = "AES"
+ private val SOFTWARE_KEY_SIZE_BITS = 256
private val secretKeyProvider = CachingSecretKeyProvider(::loadOrCreateSecretKey)
override fun encryptIfNeeded(value: String): String {
@@ -80,6 +89,18 @@ class AndroidKeystoreCredentialCrypto @Inject constructor() : CredentialCrypto {
}
private fun loadOrCreateSecretKey(): SecretKey {
+ return try {
+ loadOrCreateHardwareSecretKey()
+ } catch (e: Exception) {
+ // Some devices (notably budget Android TV boxes) ship a broken Keymaster/TEE whose
+ // hardware-backed Keystore cannot generate keys. Fall back to a software key so
+ // credential persistence keeps working instead of crashing on those devices.
+ Log.w(TAG, "Hardware-backed Keystore unavailable; falling back to a software key.", e)
+ loadOrCreateSoftwareSecretKey()
+ }
+ }
+
+ private fun loadOrCreateHardwareSecretKey(): SecretKey {
val keyStore = KeyStore.getInstance(KEYSTORE_TYPE).apply { load(null) }
val existing = keyStore.getKey(KEY_ALIAS, null) as? SecretKey
if (existing != null) return existing
@@ -98,4 +119,23 @@ class AndroidKeystoreCredentialCrypto @Inject constructor() : CredentialCrypto {
keyGenerator.init(spec)
return keyGenerator.generateKey()
}
+
+ /**
+ * Best-effort software AES key persisted in app-private storage. This is only reached when the
+ * hardware-backed Keystore is unusable, where a hardware-protected key is impossible by
+ * definition. The key material lives in the app sandbox (invisible to other apps) and keeps
+ * credentials encrypted at rest rather than storing them as plaintext.
+ */
+ private fun loadOrCreateSoftwareSecretKey(): SecretKey {
+ val keyFile = File(context.filesDir, SOFTWARE_KEY_FILE_NAME)
+ if (keyFile.exists()) {
+ return SecretKeySpec(keyFile.readBytes(), SOFTWARE_KEY_ALGORITHM)
+ }
+
+ val keyGenerator = KeyGenerator.getInstance(SOFTWARE_KEY_ALGORITHM)
+ keyGenerator.init(SOFTWARE_KEY_SIZE_BITS)
+ val key = keyGenerator.generateKey()
+ keyFile.writeBytes(key.encoded)
+ return key
+ }
}
diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index aa4bf9fb..e87daa73 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -39,6 +39,7 @@ All notable product changes are recorded in this document.
- Fixed Stalker channel logos returned as bare or portal-relative paths by resolving them against the portal logo directory, including channels imported before the fix.
- Fixed Xtream series showing an empty episode list after re-entering: a hydrated series whose local episodes went missing is now re-fetched, and re-hydration no longer replaces a larger cached episode set with a smaller, partially re-scraped provider response.
- Fixed adaptive M3U metadata being dropped between playlist parsing, import staging, catalog synchronization, and channel or movie playback reconstruction.
+- Fixed Xtream Codes and Xtream M3U URL setup crashing on devices with a broken hardware-backed Keystore (such as some budget Android TV boxes): credential encryption now falls back to a software key instead of aborting when the device cannot generate a hardware key.
### Changed