feat(phase-1/m3.3): desktop at-rest encryption — key file, container wiring, security menu

Milestone M3 task 3 (DESKTOP_PHASE_PLAN §5). Three pieces:

1. DesktopKeyProvider — the JVM FieldKeyProvider. The plan's keyring
   condition is FAILed honestly: no keyring library is in the catalog
   and the candidates are unverified on this stack, so the sanctioned
   fallback applies: the 32-byte field key lives in `field.key` inside
   the library dir, written via temp+atomic-move with 0600 POSIX perms
   (File.setReadable/setWritable fallback for non-POSIX fs). It speaks
   the phone's recovery format natively through EncryptedKeyCodec
   (importRecoveryKey / exportRecoveryKey — INCKEY1, PBKDF2-200k,
   AES-GCM, wrong passphrase throws before anything is written).
   Key retention on disable: the sweep decrypts first, then flags
   clear, but the key file stays (Android's deleteKey-on-disable is
   Keystore-scoped; keeping it lets users re-enable and re-export).
   Stated, not silent.

2. DesktopContainer now mirrors AppContainer fully: settings -> cipher
   auto-install from snapshot (isEncryptionActive AND key on disk)
   BEFORE the repository -> NoteCryptoService -> EncryptionVerifier ->
   checkNow at startup. shutdown() cancels appScope.

3. UI: SecurityActions menu (theme cycle + encrypt/decrypt toggle +
   recovery-key import/export, incl. Android's overwrite-warning gate)
   reusing the backup menu's pickFile/PassphraseDialog (now internal,
   parameterized title/extensions). theme_mode drives Material3 via
   colorScheme (M3 MaterialTheme has no darkTheme param). dynamic_color
   / use_biometric / block_screenshots are NOT shown — no JVM
   equivalent, hide-don't-fake. Window geometry persists to its own
   incido_window.preferences_pb (desktop-only state; position left to
   KWin).

Verification (scripted legs in Main.kt, --crypto-self-test / --crypto-reopen):
  leg A: probe note -> key+cipher+sweep -> verifier trial-decrypt
    PASS (verified=1 failed=0) -> transparent repo read -> WRONG KEY
    REJECTED via AEAD tag -> INCKEY1 envelope 84 bytes, wrong-pw
    rejected, key byte-match -> LEG-A PASS.
  leg B (separate process = real relaunch): settings survived, cipher
    auto-installed, sweep all-ok, decrypt sweep reverses it -> PASS.
  :desktopApp:run --self-test regression green; assembleDebug green.
  ls confirms field.key is -rw------- (0600).
One environmental note: a leg-A run earlier in the session wedged in
XWayland window teardown (never reached exitApplication); the rerun
auto-exited cleanly — same one-off as the M3.2 startup wedge.
This commit is contained in:
Incido Developer committed 2026-10-06 07:26:17 +13:00
1 parent 40e2b9ff52
commit 000f4095cc
6 files changed
+709 -30

No files matched your search

@@ -83,7 +83,7 @@ fun BackupActions(
onClick = {
menuOpen = false
scope.launch {
val file = pickFile(load = true) ?: return@launch
val file = pickFile(load = true, title = "Import backup") ?: return@launch
startImport(file, ::say) { stage -> importStage = stage }
}
},
@@ -93,7 +93,7 @@ fun BackupActions(
onClick = {
menuOpen = false
scope.launch {
val file = pickFile(load = false, suggested = suggestedExportName()) ?: return@launch
val file = pickFile(load = false, title = "Export backup", suggested = suggestedExportName()) ?: return@launch
runCatching {
withContext(Dispatchers.IO) {
BackupBridge.exportPlain(repository, FileOutputStream(file), DESKTOP_APP_VERSION)
@@ -174,7 +174,7 @@ fun BackupActions(
onConfirm = { pass ->
exportStage = null
scope.launch {
val file = pickFile(load = false, suggested = suggestedExportName()) ?: return@launch
val file = pickFile(load = false, title = "Export backup", suggested = suggestedExportName()) ?: return@launch
runCatching {
withContext(Dispatchers.IO) {
BackupBridge.exportEncrypted(
@@ -269,7 +269,7 @@ private fun ConfirmImportDialog(
}
@Composable
private fun PassphraseDialog(
internal fun PassphraseDialog(
blurb: String,
onDismiss: () -> Unit,
onConfirm: (String) -> Unit,
@@ -304,18 +304,27 @@ private fun suggestedExportName(): String =
/**
* Blocking AWT/GTK file picker. Safe off the EDT (AWT marshals internally);
* callers run it on the IO dispatcher so the composition stays responsive.
*
* M3: shared with SecurityActions (recovery-key import/export) — the
* dialog title and the accepted extensions are now the caller's choice;
* the backup callers keep their previous behaviour exactly.
*/
private suspend fun pickFile(load: Boolean, suggested: String? = null): File? =
internal suspend fun pickFile(
load: Boolean,
title: String,
suggested: String? = null,
extensions: List<String> = listOf("json", "incido", "incido.enc"),
): File? =
withContext(Dispatchers.IO) {
val dialog = FileDialog(
Frame.getFrames().firstOrNull(),
if (load) "Import backup" else "Export backup",
title,
if (load) FileDialog.LOAD else FileDialog.SAVE,
).apply {
directory = System.getProperty("user.home") + File.separator
suggested?.let { file = it }
filenameFilter = FilenameFilter { dir, name ->
dir.isDirectory || name.endsWith(".json") || name.endsWith(".incido") || name.endsWith(".incido.enc")
dir.isDirectory || extensions.any { name.endsWith(".$it") }
}
isVisible = true
}