feat(desktop): M8-3 KEM key provisioning — server-truth SK blob, unlock-hooked (#223) #227

Merged
amiryahaya merged 1 commit from feat/m8-3-kem-provisioning into main 2026-11-18 09:53:31 +01:00
Owner

Implements M8-3 per the merged design (docs/plans/2026-11-18-m8-3-kem-key-provisioning-design.md, PR #226) — first work item of M4 epic #223. Fixes audit #214 M8-3: fresh-install folder creation always failed and shares were one-way.

Core

  • KeyProvisioningService::ensure_provisioned at the end of every successful REAL-tree unlock (decoy unlock excluded — the decoy master key must never provision). Idempotent fast path; 5s timeout so a black-holed network never parks unlock.
  • Source of truth = /me/keys.encrypted_private_keys (SK blob AES-GCM under the account master key); local settings = cache tagged with kem_provisioned_user — account switch drops the stale cache, second device adopts instead of clobbering.
  • Publishes {v:1, ml_kem_pk, kaz_kem_pk} via PATCH /me/keys (never touches KemPublicKey); offline → same-keys retry; corrupt server state refuses to regenerate.
  • Removed dead generate_kem_keypair command.

⚠ TWO APP-WIDE LATENT BUGS FOUND AND FIXED (caught by the new tests)

  1. api_client passed Arc<rustls::ClientConfig> to reqwest 0.12's use_preconfigured_tls (downcasts bare Option<ClientConfig>) — every API client construction failed since 2026-11-12 (#190): the desktop could not reach the API at all. TS tests mock Tauri, so no gate ran this path.
  2. failsulit-crypto cached init in a OnceLock; after any CryptoService drop → cleanup() the native state stayed uninitialized and the cached result blocked re-init — "KAZ-KEM keygen failed: Library not initialized". Fixed in both kaz_kem and kaz_sign (init keyed off native is_initialized under FFI_LOCK).

Review: dual-pass per repo rules — ponytail pass (2 lean fixes: dead decision arm, double fetch) + full correctness review (1 Critical: account-scoped cache — fixed + tested; 2 Important: unlock timeout — fixed, test re-scoping — done via real decapsulate-path test + design §5.1 amendment; comment-mechanism corrections + zeroized SK strings — fixed; minors M3/M4/M5 noted in #222 scope).

Tests: 14 provisioning tests (decision matrix, bundle schema, executor vs in-process HTTP stub: fresh-install repro, same-keys publish retry, second-device adopt-without-publish, account-switch, corrupt-server refusal, offline defer, real folder_keys::decapsulate round trip). Full gates: cargo test 82/82, clippy clean, tsc clean, vitest 1303 green.

Closes the M8-3 portion of #222 (M8-13 receive-envelope unblocks next).

Implements **M8-3** per the merged design (docs/plans/2026-11-18-m8-3-kem-key-provisioning-design.md, PR #226) — first work item of M4 epic #223. Fixes audit #214 M8-3: fresh-install folder creation always failed and shares were one-way. **Core** - `KeyProvisioningService::ensure_provisioned` at the end of every successful REAL-tree unlock (decoy unlock excluded — the decoy master key must never provision). Idempotent fast path; 5s timeout so a black-holed network never parks unlock. - Source of truth = `/me/keys.encrypted_private_keys` (SK blob AES-GCM under the account master key); local settings = cache tagged with `kem_provisioned_user` — account switch drops the stale cache, second device adopts instead of clobbering. - Publishes `{v:1, ml_kem_pk, kaz_kem_pk}` via PATCH /me/keys (never touches `KemPublicKey`); offline → same-keys retry; corrupt server state refuses to regenerate. - Removed dead `generate_kem_keypair` command. **⚠ TWO APP-WIDE LATENT BUGS FOUND AND FIXED (caught by the new tests)** 1. `api_client` passed `Arc<rustls::ClientConfig>` to reqwest 0.12's `use_preconfigured_tls` (downcasts bare `Option<ClientConfig>`) — **every API client construction failed since 2026-11-12 (#190): the desktop could not reach the API at all.** TS tests mock Tauri, so no gate ran this path. 2. `failsulit-crypto` cached init in a `OnceLock`; after any CryptoService drop → cleanup() the native state stayed uninitialized and the cached result blocked re-init — "KAZ-KEM keygen failed: Library not initialized". Fixed in both kaz_kem and kaz_sign (init keyed off native is_initialized under FFI_LOCK). **Review**: dual-pass per repo rules — ponytail pass (2 lean fixes: dead decision arm, double fetch) + full correctness review (1 Critical: account-scoped cache — fixed + tested; 2 Important: unlock timeout — fixed, test re-scoping — done via real decapsulate-path test + design §5.1 amendment; comment-mechanism corrections + zeroized SK strings — fixed; minors M3/M4/M5 noted in #222 scope). **Tests**: 14 provisioning tests (decision matrix, bundle schema, executor vs in-process HTTP stub: fresh-install repro, same-keys publish retry, second-device adopt-without-publish, account-switch, corrupt-server refusal, offline defer, real `folder_keys::decapsulate` round trip). Full gates: cargo test 82/82, clippy clean, tsc clean, vitest 1303 green. Closes the M8-3 portion of #222 (M8-13 receive-envelope unblocks next).
feat(desktop): M8-3 KEM key provisioning — server-truth SK blob, unlock-hooked (#223)
All checks were successful
Security CI / security-scan (pull_request) Successful in 3m19s
Desktop CI / lint-and-test (pull_request) Successful in 13m16s
9d7cb0a496
Implements docs/plans/2026-11-18-m8-3-kem-key-provisioning-design.md:

- KeyProvisioningService::ensure_provisioned runs at the end of every
  successful REAL-tree unlock (session / passphrase-real / biometric —
  decoy unlock is excluded: the decoy master key must never provision).
  Idempotent fast path; offline-tolerant (network failures defer to the
  next unlock, never regenerating); published-bundle corruption surfaces
  instead of regenerating over possibly-wrapped grants.
- Source of truth = /me/keys.encrypted_private_keys (SK blob AES-GCM
  under the account master key); local settings rows are a cache —
  a second device reconstructs instead of clobbering.
- Publishes {v:1, ml_kem_pk, kaz_kem_pk} via PATCH /me/keys; never
  touches KemPublicKey (pairing/recovery channel) or encrypted_master_key.
- CryptoService::encrypt_with_master_key/decrypt_with_master_key added.
- Dead generate_kem_keypair command removed (zero callers, superseded).
- Tests (15): decision matrix, bundle schema, executor behaviors vs a
  canned local HTTP stub — fresh-install repro, publish-retry-same-keys,
  second-device adopt-without-publish, corrupt-server refusal, offline
  defer. Database::open_temp (test-only) + init_schema extraction.

Latent bugs found by the new tests (both app-wide, both fixed here):
- api_client passed Arc<rustls::ClientConfig> to use_preconfigured_tls,
  which reqwest 0.12 downcasts as Option<ClientConfig> — EVERY client
  build failed ('builder error') since #190 (2026-11-12): the desktop
  could not reach the API at all. TS tests mock Tauri, so nothing ran
  this path. Fixed: pass the bare ClientConfig.
- failsulit-crypto cached init in OnceLock; the native context dies
  with its initializing thread, and the cached result blocked re-init —
  'KAZ-KEM keygen failed: Library not initialized' after any cleanup.
  Fixed: init keyed off the native is_initialized state (kem + sign).

Gates: cargo test 81/81, clippy clean (new code), tsc clean, vitest
1303 passed.
Sign in to join this conversation.
No description provided.