Skip to content

Commit

Permalink
Move PKCS12 password check to lifecycle scope
Browse files Browse the repository at this point in the history
  • Loading branch information
kleest committed Nov 14, 2023
1 parent bf12b07 commit 45624b8
Showing 1 changed file with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ import androidx.compose.ui.platform.ComposeView
import androidx.core.content.getSystemService
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import com.google.accompanist.themeadapter.material.MdcTheme
import dagger.hilt.android.AndroidEntryPoint
import io.homeassistant.companion.android.R
import io.homeassistant.companion.android.common.util.DisabledLocationHandler
import io.homeassistant.companion.android.onboarding.OnboardingViewModel
import io.homeassistant.companion.android.onboarding.notifications.NotificationPermissionFragment
import io.homeassistant.companion.android.sensors.LocationSensorManager
import kotlinx.coroutines.launch
import java.io.IOException
import java.security.KeyStore
import io.homeassistant.companion.android.common.R as commonR
Expand Down Expand Up @@ -101,18 +103,20 @@ class MobileAppIntegrationFragment : Fragment() {
}

private fun onCheckTLSCertificatePassword(password: String) {
var ok: Boolean
context?.contentResolver?.openInputStream(viewModel.tlsClientCertificateUri!!)!!.buffered().use {
val keystore = KeyStore.getInstance("PKCS12")
ok = try {
keystore.load(it, password.toCharArray())
true
} catch (e: IOException) {
// we cannot determine if it failed due to wrong password or other reasons, since e.cause is not set to UnrecoverableKeyException
false
lifecycleScope.launch {
var ok: Boolean
context?.contentResolver?.openInputStream(viewModel.tlsClientCertificateUri!!)!!.buffered().use {
val keystore = KeyStore.getInstance("PKCS12")
ok = try {
keystore.load(it, password.toCharArray())
true
} catch (e: IOException) {
// we cannot determine if it failed due to wrong password or other reasons, since e.cause is not set to UnrecoverableKeyException
false
}
}
viewModel.tlsClientCertificatePasswordCorrect = ok
}
viewModel.tlsClientCertificatePasswordCorrect = ok
}

@SuppressLint("Range")
Expand Down

0 comments on commit 45624b8

Please sign in to comment.