VaultStore.kt

5.0 KB · raw

1package com.peachpasswords.vault23import android.content.Context4import dagger.hilt.android.qualifiers.ApplicationContext5import kotlinx.serialization.Serializable6import kotlinx.serialization.decodeFromString7import kotlinx.serialization.encodeToString8import kotlinx.serialization.json.Json9import javax.inject.Inject10import javax.inject.Singleton1112/**13 * One encrypted slot at rest. [blob] is base64 of VaultCrypto's14 * `nonce || ciphertext || tag` output; [salt] (base64) drives Argon2id.15 * Mirrors the browser's StoredVault, minus the recovery-escrow fields which16 * the Android port wires in a later phase.17 */18@Serializable19data class StoredVault(20    val version: Int = 1,21    val salt: String,22    val blob: String,23)2425/** A soft-deleted entry, exposed to the sync layer for last-write-wins merges. */26@Serializable27data class Tombstone(val id: String, val deletedAt: Long)2829/**30 * The decrypted slot-A envelope: the entry list, the duress flag, and the31 * stable sync root key.32 *33 * [rootKeyB64] is the vault's stable 32-byte sync master key (base64, no34 * padding). Unlike the Argon2id password key it never rotates on a password35 * change, so paired devices keep deriving the same vault_id, Noise static key,36 * and per-entry envelope keys across independent password rotations (browser37 * SEV-2-B). Empty on legacy payloads written before sync landed — the38 * repository lazily mints and persists one on first unlock.39 */40@Serializable41data class VaultPayload(42    val entries: List<VaultEntry> = emptyList(),43    val duressConfigured: Boolean = false,44    val rootKeyB64: String = "",45)4647/** Pure JSON codec for the slot-A plaintext envelope — usable without Android. */48object VaultCodec {49    private val json = Json { ignoreUnknownKeys = true; encodeDefaults = true }50    fun encode(payload: VaultPayload): ByteArray = json.encodeToString(payload).toByteArray(Charsets.UTF_8)51    fun decode(bytes: ByteArray): VaultPayload = json.decodeFromString(String(bytes, Charsets.UTF_8))52    fun encodeSettings(s: VaultSettings): String = json.encodeToString(s)53    fun decodeSettings(s: String): VaultSettings? = runCatching { json.decodeFromString<VaultSettings>(s) }.getOrNull()54    fun encodeSlot(s: StoredVault): String = json.encodeToString(s)55    fun decodeSlot(s: String): StoredVault? = runCatching { json.decodeFromString<StoredVault>(s) }.getOrNull()56}5758/**59 * Persistence for the two vault slots, the monotonic version counter, and the60 * settings blob. Abstracted behind an interface so the repository can be61 * exercised against an in-memory fake in plain JUnit.62 */63interface VaultStorage {64    fun loadSlotA(): StoredVault?65    fun hasVault(): Boolean66    fun writeSlotA(slot: StoredVault): Boolean67    fun writeSlotB(blobBase64: String): Boolean68    fun version(): Long69    fun stampLocalMutation(): Long70    fun loadSettings(): VaultSettings71    fun writeSettings(settings: VaultSettings): Boolean72    fun clearVault()73}7475/**76 * SharedPreferences-backed [VaultStorage]. The slot blobs are already AES-GCM77 * sealed under the Argon2id key, so plaintext prefs are fine — only the salt is78 * in the clear, exactly as in storage.local on the extension. The Pro license79 * lives in a SEPARATE prefs file (LicenseRepository), so wiping this store can80 * never touch the license — destroyVault()'s core invariant.81 */82@Singleton83class VaultStore @Inject constructor(84    @ApplicationContext context: Context,85) : VaultStorage {86    private val prefs = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)8788    override fun loadSlotA(): StoredVault? = prefs.getString(KEY_SLOT_A, null)?.let(VaultCodec::decodeSlot)8990    override fun hasVault(): Boolean = prefs.contains(KEY_SLOT_A)9192    override fun writeSlotA(slot: StoredVault): Boolean =93        prefs.edit().putString(KEY_SLOT_A, VaultCodec.encodeSlot(slot)).commit()9495    override fun writeSlotB(blobBase64: String): Boolean =96        prefs.edit().putString(KEY_SLOT_B, blobBase64).commit()9798    override fun version(): Long = prefs.getString(KEY_VERSION, null)?.toLongOrNull() ?: 0L99100    override fun stampLocalMutation(): Long {101        val next = maxOf(System.currentTimeMillis(), version() + 1L)102        prefs.edit().putString(KEY_VERSION, next.toString()).commit()103        return next104    }105106    override fun loadSettings(): VaultSettings =107        prefs.getString(KEY_SETTINGS, null)?.let(VaultCodec::decodeSettings) ?: VaultSettings()108109    override fun writeSettings(settings: VaultSettings): Boolean =110        prefs.edit().putString(KEY_SETTINGS, VaultCodec.encodeSettings(settings)).commit()111112    override fun clearVault() {113        prefs.edit()114            .remove(KEY_SLOT_A).remove(KEY_SLOT_B).remove(KEY_VERSION).remove(KEY_SETTINGS)115            .commit()116    }117118    companion object {119        private const val PREFS = "peach_vault_store"120        private const val KEY_SLOT_A = "peach_vault_a"121        private const val KEY_SLOT_B = "peach_vault_b"122        private const val KEY_VERSION = "peach_last_modified_at"123        private const val KEY_SETTINGS = "peach_settings"124    }125}