diff --git a/app/src/androidTest/kotlin/io/privkey/keep/CertificatePinHoldsCardTest.kt b/app/src/androidTest/kotlin/io/privkey/keep/CertificatePinHoldsCardTest.kt index e83fe209..82d23e54 100644 --- a/app/src/androidTest/kotlin/io/privkey/keep/CertificatePinHoldsCardTest.kt +++ b/app/src/androidTest/kotlin/io/privkey/keep/CertificatePinHoldsCardTest.kt @@ -5,11 +5,9 @@ import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onAllNodesWithText import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick -import androidx.compose.ui.test.performTextInput import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.platform.app.InstrumentationRegistry import io.privkey.keep.ui.theme.KeepAndroidTheme -import io.privkey.keep.uniffi.CertificatePinHolds import org.junit.Assert.assertEquals import org.junit.Rule import org.junit.Test @@ -18,7 +16,7 @@ import org.junit.runner.RunWith /** * A relay whose pin was removed stays disconnected until the user acts, so the card has to * show it and offer the way back for each one, including the case where the core could not - * read which hosts it holds. + * read the pin records. */ @RunWith(AndroidJUnit4::class) class CertificatePinHoldsCardTest { @@ -31,14 +29,13 @@ class CertificatePinHoldsCardTest { private val context = InstrumentationRegistry.getInstrumentation().targetContext private val trustNext get() = context.getString(R.string.settings_cert_pins_trust_next) - private val trustHost get() = context.getString(R.string.settings_cert_pins_trust_host) private val heldTitle get() = context.getString(R.string.settings_cert_pins_held_title) - private fun show(holds: CertificatePinHolds?, trusted: MutableList) = compose.setContent { + private fun show(held: List?, trusted: MutableList) = compose.setContent { KeepAndroidTheme { CertificatePinsCard( pins = emptyList(), - holds = holds, + held = held, onStagePin = { _, _ -> }, onRetirePin = { _, _ -> }, onTrustNextKey = { trusted += it }, @@ -50,37 +47,28 @@ class CertificatePinHoldsCardTest { @Test fun eachHeldHostOffersToTrustItsNextKey() { val trusted = mutableListOf() - show(CertificatePinHolds(listOf("relay.one.com", "relay.two.com"), false, emptyList()), trusted) + show(listOf("wss://relay.one.com", "wss://relay.two.com"), trusted) compose.onNodeWithText(heldTitle).assertIsDisplayed() - compose.onNodeWithText("relay.two.com").assertIsDisplayed() + compose.onNodeWithText("wss://relay.two.com").assertIsDisplayed() compose.onAllNodesWithText(trustNext)[1].performClick() assertEquals("nothing is trusted before the warning is confirmed", emptyList(), trusted) - compose.onNodeWithText(context.getString(R.string.settings_cert_pins_trust_dialog_text, "relay.two.com")).assertIsDisplayed() + compose.onNodeWithText(context.getString(R.string.settings_cert_pins_trust_dialog_text, "wss://relay.two.com")).assertIsDisplayed() compose.onAllNodesWithText(trustNext).let { it[it.fetchSemanticsNodes().size - 1] }.performClick() - assertEquals(listOf("relay.two.com"), trusted) - assertEquals(0, compose.onAllNodesWithText(trustHost).fetchSemanticsNodes().size) + assertEquals(listOf("wss://relay.two.com"), trusted) } @Test - fun unreadableHoldsOfferToTrustAHostByName() { - val trusted = mutableListOf() - show(CertificatePinHolds(emptyList(), true, emptyList()), trusted) - - compose.onNodeWithText(context.getString(R.string.settings_cert_pins_held_all_text)).assertIsDisplayed() - compose.onNodeWithText(trustHost).performClick() - compose.onNodeWithText(context.getString(R.string.settings_cert_pins_stage_host_label)).performTextInput(" relay.example.com ") - compose.onNodeWithText(trustNext).performClick() - assertEquals(emptyList(), trusted) - compose.onNodeWithText(context.getString(R.string.settings_cert_pins_trust_dialog_text, "relay.example.com")).assertIsDisplayed() - compose.onNodeWithText(trustNext).performClick() - assertEquals(listOf("relay.example.com"), trusted) + fun unreadableHoldsSayTheRecordsCouldNotBeRead() { + show(null, mutableListOf()) + compose.onNodeWithText(context.getString(R.string.settings_cert_pins_held_unreadable)).assertIsDisplayed() + assertEquals(0, compose.onAllNodesWithText(context.getString(R.string.settings_cert_pins_empty)).fetchSemanticsNodes().size) } @Test fun cancellingTheWarningTrustsNothing() { val trusted = mutableListOf() - show(CertificatePinHolds(listOf("relay.one.com"), false, emptyList()), trusted) + show(listOf("wss://relay.one.com"), trusted) compose.onNodeWithText(trustNext).performClick() compose.onNodeWithText(context.getString(R.string.settings_cancel)).performClick() assertEquals(emptyList(), trusted) @@ -90,9 +78,10 @@ class CertificatePinHoldsCardTest { fun theMismatchDialogWarnsBeforeClearingAndTrusting() { var cleared = 0 compose.setContent { - KeepAndroidTheme { PinMismatchDialog("relay.example.com", onClearAndRetry = { cleared++ }, onDismiss = {}) } + KeepAndroidTheme { PinMismatchDialog("relay.example.com", NEW_KEY, onClearAndRetry = { cleared++ }, onDismiss = {}) } } compose.onNodeWithText(context.getString(R.string.main_clear_pin_and_retry_text, "relay.example.com")).assertIsDisplayed() + compose.onNodeWithText(context.getString(R.string.main_pin_mismatch_new_key, NEW_KEY)).assertIsDisplayed() assertEquals(0, cleared) compose.onNodeWithText(context.getString(R.string.main_clear_pin_and_retry)).performClick() assertEquals(1, cleared) @@ -100,7 +89,12 @@ class CertificatePinHoldsCardTest { @Test fun nothingHeldShowsNoSection() { - show(CertificatePinHolds(emptyList(), false, emptyList()), mutableListOf()) + show(emptyList(), mutableListOf()) assertEquals(0, compose.onAllNodesWithText(heldTitle).fetchSemanticsNodes().size) + compose.onNodeWithText(context.getString(R.string.settings_cert_pins_empty)).assertIsDisplayed() + } + + private companion object { + const val NEW_KEY = "3b2f000000000000000000000000000000000000000000000000000000000a1c" } } diff --git a/app/src/androidTest/kotlin/io/privkey/keep/approver/ApproverPinChangeE2ETest.kt b/app/src/androidTest/kotlin/io/privkey/keep/approver/ApproverPinChangeE2ETest.kt index 0c424ae4..9b0f9fd9 100644 --- a/app/src/androidTest/kotlin/io/privkey/keep/approver/ApproverPinChangeE2ETest.kt +++ b/app/src/androidTest/kotlin/io/privkey/keep/approver/ApproverPinChangeE2ETest.kt @@ -28,7 +28,7 @@ import java.security.spec.ECGenParameterSpec * End-to-end: the app's own approver, paired with a gateway that never answers over a * public relay, is started again by the core after a certificate pin change. Once a * change leaves its relay without a pin the relay is held: the restart is refused, and so - * is any start, until the user trusts the relay's next key. Manual-only, since it needs the relay: run it with `-e approverManual 1`. + * is any start, until the user trusts the relay's next key, which resumes the approver. Manual-only, since it needs the relay: run it with `-e approverManual 1`. * The test key needs no biometric, unlike the app's approver key, and is deleted after. */ @RunWith(AndroidJUnit4::class) @@ -51,8 +51,10 @@ class ApproverPinChangeE2ETest { // The run clears every pin, so the device's own are put back after it. val mobile = checkNotNull(app.getKeepMobile()) { "Keep not initialized" } val devicePins = mobile.getCertificatePins() - val heldBefore = mobile.certificatePinHolds().hosts.toSet() val relayHost = checkNotNull(URI(relay).host) + // A run that died before its cleanup leaves the relay held, which no pairing can reach. + // This is a test device's relay, so trusting its next key here is the run's own choice. + mobile.heldRelays(listOf(relay)).forEach { mobile.releaseCertificatePinHold(it) } val secret = ByteArray(32).also(SecureRandom()::nextBytes).joinToString("") { "%02x".format(it) } val uri = "keep-approver:v1?gateway=$GATEWAY&secret=$secret&relay=$relay" @@ -82,25 +84,29 @@ class ApproverPinChangeE2ETest { Thread.sleep(SETTLE_MS) assertFalse("nothing started the approver on an unpinned relay", handler.status().running) - assertTrue("clearing the pin holds the relay", relayHost in mobile.certificatePinHolds().hosts) + assertEquals("clearing the pin holds the relay", listOf(relay), mobile.heldRelays(listOf(relay))) controller.startIfPaired() assertFalse("a start does not learn a key for a held relay", handler.status().running) - assertTrue(app.releaseCertificatePinHold(relayHost)) - controller.startIfPaired() - assertTrue("a start after the user trusts the next key serves again", awaitStatus(handler) { it.running && it.pairing }) + assertEquals(relayHost, app.releaseCertificatePinHold(relay)) + assertTrue("the core resumes the approver the hold stopped", awaitStatus(handler) { it.running && it.pairing }) assertFalse(handler.status().paired) + assertTrue("the resumed start pinned the relay", mobile.getCertificatePins().any { it.hostname == relayHost }) } finally { runCatching { handler.forget() } + // Clearing holds the relay, and only a pin ends that, so the key it showed goes back. + val relayPins = mobile.getCertificatePins().filter { it.hostname == relayHost } mobile.clearCertificatePins() devicePins.forEach { mobile.stageCertificatePin(it.hostname, it.spkiHash) } - (mobile.certificatePinHolds().hosts - heldBefore).forEach { mobile.releaseCertificatePinHold(it) } + if (devicePins.none { it.hostname == relayHost }) { + relayPins.forEach { mobile.stageCertificatePin(it.hostname, it.spkiHash) } + } runCatching { controller.startIfPaired() } runCatching { keyStore().deleteEntry(ALIAS) } } assertFalse(handler.status().running) - assertEquals(devicePins, mobile.getCertificatePins()) - assertEquals(heldBefore, mobile.certificatePinHolds().hosts.toSet()) + assertEquals(devicePins.toSet(), mobile.getCertificatePins().filter { it.hostname != relayHost || it in devicePins }.toSet()) + assertEquals("the relay is not left held", emptyList(), mobile.heldRelays(listOf(relay))) } // The stop lands before the pin call returns, and the restart probes the relay before it diff --git a/app/src/main/kotlin/io/privkey/keep/KeepMobileApp.kt b/app/src/main/kotlin/io/privkey/keep/KeepMobileApp.kt index 5c743082..827b0b3e 100644 --- a/app/src/main/kotlin/io/privkey/keep/KeepMobileApp.kt +++ b/app/src/main/kotlin/io/privkey/keep/KeepMobileApp.kt @@ -52,7 +52,6 @@ import io.privkey.keep.uniffi.ProxyConfigInfo import io.privkey.keep.uniffi.RelayConfigInfo import io.privkey.keep.uniffi.SignPolicyStore import io.privkey.keep.uniffi.SigningAuditLog -import io.privkey.keep.uniffi.CertificatePinHolds import io.privkey.keep.uniffi.SigningRateLimiter import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CoroutineScope @@ -67,6 +66,7 @@ import java.util.UUID import javax.crypto.Cipher private const val TAG = "KeepMobileApp" +private val DEFAULT_NODE_RELAYS = listOf("wss://relay.damus.io", "wss://nos.lol", "wss://relay.primal.net") private const val EVENT_LOG_MAX_AGE_MS = 7L * 24 * 60 * 60 * 1000 class KeepMobileApp : Application() { @@ -519,9 +519,7 @@ class KeepMobileApp : Application() { initMutex.withLock { if (isNodeLive() && mobile.getShareInfo() != null) return - val relays = getActiveRelays().ifEmpty { - listOf("wss://relay.damus.io", "wss://nos.lol", "wss://relay.primal.net") - } + val relays = getActiveRelays().ifEmpty { DEFAULT_NODE_RELAYS } val store = storage withContext(initDispatcher) { if (requestId != null && store != null) { @@ -840,32 +838,38 @@ class KeepMobileApp : Application() { .onFailure { if (BuildConfig.DEBUG) Log.e(TAG, "Failed to clearCertificatePin: ${it::class.simpleName}") } .isSuccess - fun certificatePinHolds(): CertificatePinHolds? = - runCatching { keepMobile?.certificatePinHolds() } - .onFailure { if (BuildConfig.DEBUG) Log.e(TAG, "Failed to read certificatePinHolds: ${it::class.simpleName}") } - .getOrNull() + /** + * The node, bunker and approver relays waiting on the user for a pin, or null if unreadable. + * The candidates are gathered here until the core lists the relays it connects to itself. + * Any source that cannot be read makes the whole list unreadable rather than incomplete. + */ + fun heldRelays(): List? = runCatching { + val mobile = checkNotNull(keepMobile) + val nodeRelays = mobile.getRelayConfig(storage?.getActiveShareKey()).frostRelays.ifEmpty { DEFAULT_NODE_RELAYS } + val bunkerRelays = mobile.getRelayConfig(null).bunkerRelays + val approverRelays = approverController?.handlerStatusRelays().orEmpty() + mobile.heldRelays((nodeRelays + bunkerRelays + approverRelays).distinct()) + }.onFailure { if (BuildConfig.DEBUG) Log.e(TAG, "Failed to read heldRelays: ${it::class.simpleName}") } + .getOrNull() + + /** Pins [spkiHash] as the host's only key, for a mismatch the user chose to accept. Gate it on the user. */ + fun replaceCertificatePins(hostname: String, spkiHash: String): Boolean = + runCatching { checkNotNull(keepMobile).replaceCertificatePins(hostname, spkiHash) } + .onSuccess { onLiveNodeDropped() } + .onFailure { if (BuildConfig.DEBUG) Log.e(TAG, "Failed to replaceCertificatePins: ${it::class.simpleName}") } + .isSuccess /** - * Lets the next connection to [hostname] trust whatever key it shows, so callers gate it - * on the user. That choice is also the start the hold waited for: a bunker serving without - * the relay and a paired approver the hold stopped start again, and the caller reconnects - * the node. + * Lets a probe of [hostname] in the next ten minutes trust whatever key it shows, so callers + * gate it on the user and reconnect right after. Returns the host the core released. The core + * resumes an approver waiting on it, but not the bunker yet, so that restart stays here. */ - fun releaseCertificatePinHold(hostname: String): Boolean { - val released = runCatching { checkNotNull(keepMobile).releaseCertificatePinHold(hostname) } + fun releaseCertificatePinHold(hostname: String): String? = + runCatching { checkNotNull(keepMobile).releaseCertificatePinHold(hostname) } .onFailure { if (BuildConfig.DEBUG) Log.e(TAG, "Failed to releaseCertificatePinHold: ${it::class.simpleName}") } - .isSuccess - if (!released) return false - io.privkey.keep.nip46.BunkerService.restartAfterNodeDrop() - approverController?.let { controller -> - applicationScope.launch(Dispatchers.IO) { - runCatching { controller.startIfPaired() }.onFailure { - if (BuildConfig.DEBUG) Log.e(TAG, "Failed to start approver: ${it::class.simpleName}") - } - } - } - return true - } + .getOrNull() + // The core drops the node when it uses the host; this also restarts the bunker. + ?.also { onLiveNodeDropped() } fun clearAllCertificatePins() { runCatching { keepMobile?.clearCertificatePins() } diff --git a/app/src/main/kotlin/io/privkey/keep/MainActivity.kt b/app/src/main/kotlin/io/privkey/keep/MainActivity.kt index 4578b166..33c56651 100644 --- a/app/src/main/kotlin/io/privkey/keep/MainActivity.kt +++ b/app/src/main/kotlin/io/privkey/keep/MainActivity.kt @@ -23,6 +23,7 @@ import androidx.compose.ui.text.LinkAnnotation import androidx.compose.ui.text.SpanStyle import androidx.compose.ui.text.TextLinkStyles import androidx.compose.ui.text.buildAnnotatedString +import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextDecoration @@ -318,8 +319,8 @@ class MainActivity : FragmentActivity() { }, onReconnectRelays = { app.reconnectRelays() }, onSaveProxyConfig = app::saveProxyConfig, - onClearCertificatePin = app::clearCertificatePin, - onCertificatePinHolds = app::certificatePinHolds, + onReplaceCertificatePins = app::replaceCertificatePins, + onHeldRelays = app::heldRelays, onReleaseCertificatePinHold = app::releaseCertificatePinHold, onClearAllCertificatePins = app::clearAllCertificatePins, onStageCertificatePin = app::stageCertificatePin, @@ -469,9 +470,9 @@ fun MainScreen( onBunkerServiceChanged: (Boolean) -> Unit = {}, onReconnectRelays: () -> Unit = {}, onSaveProxyConfig: (ProxyConfigInfo) -> Unit, - onClearCertificatePin: (String) -> Boolean = { false }, - onCertificatePinHolds: () -> CertificatePinHolds? = { null }, - onReleaseCertificatePinHold: (String) -> Boolean = { false }, + onReplaceCertificatePins: (String, String) -> Boolean = { _, _ -> false }, + onHeldRelays: () -> List? = { null }, + onReleaseCertificatePinHold: (String) -> String? = { null }, onClearAllCertificatePins: () -> Unit = {}, onStageCertificatePin: (String, String) -> Boolean = { _, _ -> false }, onRemoveCertificatePin: (String, String) -> CertPinRemoval? = { _, _ -> null }, @@ -602,7 +603,7 @@ fun MainScreen( val bunkerUrl by BunkerService.bunkerUrl.collectAsState() val bunkerStatus by BunkerService.status.collectAsState() var certificatePins by remember { mutableStateOf(emptyList()) } - var certificatePinHolds by remember { mutableStateOf(null) } + var heldRelays by remember { mutableStateOf?>(emptyList()) } var profileRelays by remember { mutableStateOf(emptyList()) } var showSecuritySettings by remember { mutableStateOf(false) } var showExportLogs by remember { mutableStateOf(false) } @@ -716,14 +717,14 @@ fun MainScreen( suspend fun refreshCertificatePins() { certificatePins = withContext(Dispatchers.IO) { keepMobile.getCertificatePinsCompat() } - certificatePinHolds = withContext(Dispatchers.IO) { onCertificatePinHolds() } + heldRelays = withContext(Dispatchers.IO) { onHeldRelays() } } LaunchedEffect(Unit) { refreshCertificatePins() } - suspend fun afterRelease(hostname: String, released: Boolean) { + suspend fun afterPinTrust(hostname: String, trusted: Boolean) { refreshCertificatePins() - if (released) { + if (trusted) { onReconnectRelays() } else { Toast.makeText( @@ -1630,20 +1631,18 @@ fun MainScreen( if (pinMismatchInfo != null) { PinMismatchDialog( hostname = pinMismatchInfo.hostname, + actualSpkiHash = pinMismatchInfo.actual, onClearAndRetry = { - // Clearing holds the host, so the retry needs the hold released too: trusting - // the key it now shows, right after a possible MITM, is what this asks for, - // so both steps sit behind one fresh auth factor. + // Pins the exact key the dialog showed, so nothing else is learned in between. requireAuthThen( - appContext.getString(R.string.settings_cert_pins_clear_dialog_title), + appContext.getString(R.string.main_trust_new_certificate_title), appContext.getString(R.string.main_clear_pin_and_retry_text, pinMismatchInfo.hostname), - appContext.getString(R.string.settings_cert_pins_clear), + appContext.getString(R.string.main_clear_pin_and_retry), ) { val trusted = withContext(Dispatchers.IO) { - onClearCertificatePin(pinMismatchInfo.hostname) && - onReleaseCertificatePinHold(pinMismatchInfo.hostname) + onReplaceCertificatePins(pinMismatchInfo.hostname, pinMismatchInfo.actual) } - afterRelease(pinMismatchInfo.hostname, trusted) + afterPinTrust(pinMismatchInfo.hostname, trusted) } }, onDismiss = onDismissPinMismatch @@ -1928,7 +1927,7 @@ fun MainScreen( // Null (not running) also covers a stale state left by a stop or error. relayHealth = appLiveState?.relays?.takeIf { isConnected || isConnecting }, certificatePins = certificatePins, - certificatePinHolds = certificatePinHolds, + heldRelays = heldRelays, proxyEnabled = proxyEnabled, proxyPort = proxyPort, autoStartEnabled = autoStartEnabled, @@ -2051,7 +2050,7 @@ fun MainScreen( appContext.getString(R.string.settings_cert_pins_trust_dialog_text, hostname), appContext.getString(R.string.settings_cert_pins_trust_next), ) { - afterRelease(hostname, withContext(Dispatchers.IO) { onReleaseCertificatePinHold(hostname) }) + afterPinTrust(hostname, withContext(Dispatchers.IO) { onReleaseCertificatePinHold(hostname) } != null) } }, onClearAllPins = { @@ -2415,7 +2414,7 @@ private fun SettingsTab( relayConfigUnreadable: Boolean, relayHealth: List?, certificatePins: List, - certificatePinHolds: CertificatePinHolds?, + heldRelays: List?, proxyEnabled: Boolean, proxyPort: Int, autoStartEnabled: Boolean, @@ -2472,7 +2471,7 @@ private fun SettingsTab( relayConfigUnreadable = relayConfigUnreadable, relayHealth = relayHealth, certificatePins = certificatePins, - certificatePinHolds = certificatePinHolds, + heldRelays = heldRelays, proxyEnabled = proxyEnabled, proxyPort = proxyPort, onAddRelay = onAddRelay, @@ -2843,6 +2842,7 @@ private fun DiscardPendingDkgDialog( @Composable internal fun PinMismatchDialog( hostname: String, + actualSpkiHash: String, onClearAndRetry: () -> Unit, onDismiss: () -> Unit ) { @@ -2854,6 +2854,11 @@ internal fun PinMismatchDialog( Column(verticalArrangement = Arrangement.spacedBy(8.dp)) { Text(stringResource(R.string.main_pin_mismatch_text, hostname)) Text(stringResource(R.string.main_clear_pin_and_retry_text, hostname)) + Text( + stringResource(R.string.main_pin_mismatch_new_key, actualSpkiHash), + style = MaterialTheme.typography.bodySmall, + fontFamily = FontFamily.Monospace + ) } }, confirmButton = { @@ -2872,7 +2877,7 @@ private fun ShareSettingsSection( relayConfigUnreadable: Boolean, relayHealth: List?, certificatePins: List, - certificatePinHolds: CertificatePinHolds?, + heldRelays: List?, proxyEnabled: Boolean, proxyPort: Int, onAddRelay: (String) -> Unit, @@ -2902,7 +2907,7 @@ private fun ShareSettingsSection( CertificatePinsCard( pins = certificatePins, - holds = certificatePinHolds, + held = heldRelays, onStagePin = onStagePin, onRetirePin = onRetirePin, onTrustNextKey = onTrustNextKey, diff --git a/app/src/main/kotlin/io/privkey/keep/SecurityCards.kt b/app/src/main/kotlin/io/privkey/keep/SecurityCards.kt index 9a9b09bc..aace85b7 100644 --- a/app/src/main/kotlin/io/privkey/keep/SecurityCards.kt +++ b/app/src/main/kotlin/io/privkey/keep/SecurityCards.kt @@ -1,7 +1,6 @@ package io.privkey.keep import io.privkey.keep.ui.components.KeepCard -import io.privkey.keep.uniffi.CertificatePinHolds import androidx.compose.foundation.BorderStroke import androidx.compose.foundation.layout.* import androidx.compose.material.icons.Icons @@ -230,7 +229,7 @@ internal fun isValidSpkiHash(hash: String): Boolean = @Composable fun CertificatePinsCard( pins: List, - holds: CertificatePinHolds?, + held: List?, onStagePin: (String, String) -> Unit, onRetirePin: (String, String) -> Unit, onTrustNextKey: (String) -> Unit, @@ -238,7 +237,6 @@ fun CertificatePinsCard( ) { var showClearAllDialog by remember { mutableStateOf(false) } var showStageDialog by remember { mutableStateOf(false) } - var showTrustHostDialog by remember { mutableStateOf(false) } var hostToTrust by remember { mutableStateOf(null) } var pinToRetire by remember { mutableStateOf(null) } @@ -256,8 +254,7 @@ fun CertificatePinsCard( } } } - val held = holds?.takeIf { it.hosts.isNotEmpty() || it.everyUnpinnedHost } - if (pins.isEmpty() && held == null) { + if (pins.isEmpty() && held?.isEmpty() == true) { Text( stringResource(R.string.settings_cert_pins_empty), color = MaterialTheme.colorScheme.onSurfaceVariant, @@ -299,22 +296,18 @@ fun CertificatePinsCard( TextButton(onClick = { showStageDialog = true }) { Text(stringResource(R.string.settings_cert_pins_stage)) } - if (held != null) { - HeldHostsSection(held, onTrustNextKey = { hostToTrust = it }, onTrustOtherHost = { showTrustHostDialog = true }) + if (held == null) { + Text( + stringResource(R.string.settings_cert_pins_held_unreadable), + color = MaterialTheme.colorScheme.error, + style = MaterialTheme.typography.bodySmall + ) + } else if (held.isNotEmpty()) { + HeldRelaysSection(held, onTrustNextKey = { hostToTrust = it }) } } } - if (showTrustHostDialog) { - TrustHostDialog( - onDismiss = { showTrustHostDialog = false }, - onTrust = { host -> - hostToTrust = host - showTrustHostDialog = false - } - ) - } - // The one action here that trusts a key on first use, so the warning is shown in the app // whatever the lock: the gate after it may be a biometric prompt or no prompt at all. hostToTrust?.let { host -> @@ -401,21 +394,15 @@ fun CertificatePinsCard( } @Composable -private fun HeldHostsSection( - holds: CertificatePinHolds, - onTrustNextKey: (String) -> Unit, - onTrustOtherHost: () -> Unit -) { +private fun HeldRelaysSection(relays: List, onTrustNextKey: (String) -> Unit) { Spacer(modifier = Modifier.height(8.dp)) Text(stringResource(R.string.settings_cert_pins_held_title), style = MaterialTheme.typography.titleSmall) Text( - stringResource( - if (holds.everyUnpinnedHost) R.string.settings_cert_pins_held_all_text else R.string.settings_cert_pins_held_text - ), + stringResource(R.string.settings_cert_pins_held_text), style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant ) - holds.hosts.forEach { host -> + relays.forEach { relay -> Row( modifier = Modifier .fillMaxWidth() @@ -423,48 +410,12 @@ private fun HeldHostsSection( horizontalArrangement = Arrangement.SpaceBetween, verticalAlignment = Alignment.CenterVertically ) { - Text(host, style = MaterialTheme.typography.bodyMedium, modifier = Modifier.weight(1f)) - TextButton(onClick = { onTrustNextKey(host) }) { + Text(relay, style = MaterialTheme.typography.bodyMedium, modifier = Modifier.weight(1f)) + TextButton(onClick = { onTrustNextKey(relay) }) { Text(stringResource(R.string.settings_cert_pins_trust_next)) } } } - if (holds.everyUnpinnedHost) { - TextButton(onClick = onTrustOtherHost) { - Text(stringResource(R.string.settings_cert_pins_trust_host)) - } - } -} - -@Composable -private fun TrustHostDialog( - onDismiss: () -> Unit, - onTrust: (String) -> Unit -) { - var host by remember { mutableStateOf("") } - val hostTrimmed = host.trim() - AlertDialog( - onDismissRequest = onDismiss, - title = { Text(stringResource(R.string.settings_cert_pins_trust_host)) }, - text = { - OutlinedTextField( - value = host, - onValueChange = { host = it }, - label = { Text(stringResource(R.string.settings_cert_pins_stage_host_label)) }, - singleLine = true - ) - }, - confirmButton = { - TextButton(onClick = { onTrust(hostTrimmed) }, enabled = hostTrimmed.isNotEmpty()) { - Text(stringResource(R.string.settings_cert_pins_trust_next)) - } - }, - dismissButton = { - TextButton(onClick = onDismiss) { - Text(stringResource(R.string.settings_cancel)) - } - } - ) } @Composable diff --git a/app/src/main/kotlin/io/privkey/keep/approver/ApproverController.kt b/app/src/main/kotlin/io/privkey/keep/approver/ApproverController.kt index a3cc373b..86d897e4 100644 --- a/app/src/main/kotlin/io/privkey/keep/approver/ApproverController.kt +++ b/app/src/main/kotlin/io/privkey/keep/approver/ApproverController.kt @@ -48,6 +48,9 @@ class ApproverKeyInvalidatedException : Exception() */ class ApproverController(private val context: Context, mobile: KeepMobile) { private val handler = ApproverHandler(mobile) + + /** The relays the approver serves or waits on, read from the core rather than the last refresh. */ + fun handlerStatusRelays(): List = handler.status().relays private val notifier = ApproverNotifier(context) private val _state = MutableStateFlow(ApproverState()) diff --git a/app/src/main/res/values/strings_main.xml b/app/src/main/res/values/strings_main.xml index 66620d29..d2cddf18 100644 --- a/app/src/main/res/values/strings_main.xml +++ b/app/src/main/res/values/strings_main.xml @@ -124,8 +124,10 @@ Recover or discard your pending signing group share before creating a new group. Certificate Pin Mismatch The certificate for %1$s has changed. This could indicate a security issue or a legitimate certificate rotation. - Clear Pin & Retry - Remove the certificate pin for %1$s and trust the certificate it shows on the next connection? If you do not know why it changed, someone may be intercepting the connection. + Trust New Certificate + Pin the certificate %1$s showed just now in place of its old pin? If you do not know why it changed, someone may be intercepting the connection. + Trust New Certificate? + New key: %1$s Dismiss Database: %1$s MB Clean up expired data diff --git a/app/src/main/res/values/strings_settings.xml b/app/src/main/res/values/strings_settings.xml index 01045c52..882ab90b 100644 --- a/app/src/main/res/values/strings_settings.xml +++ b/app/src/main/res/values/strings_settings.xml @@ -102,10 +102,9 @@ %1$s has no pin now and stays disconnected until you stage one or trust its next key. Stage backup pin Waiting for a new key - These relays had their pin removed. They stay disconnected until you stage a new pin or trust the key they show next. - Your pin records could not be read, so every relay without a pin stays disconnected until you stage a pin for it or trust its next key. + These relays have no pin and stay disconnected until you stage a new pin or trust the key they show next. + Could not check which relays are waiting for a new key. Trust next key - Trust a relay\'s next key Trust Next Key? The next connection to %1$s will trust whatever certificate it shows, including an attacker\'s, and pin it. Only continue if you know why its pin was removed. Could not trust the next key for %1$s diff --git a/keep.version b/keep.version index 93567bbe..7440d88a 100644 --- a/keep.version +++ b/keep.version @@ -1 +1 @@ -44a0b3aa0949cb8fc7c6e2b836707f88130d19f4 +ebbbec3efd0c9e432301e48d8eadf1edf839a2cf