Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / apple / Shared / KeychainSecureStore.swift

KeychainSecureStore.swift68 lines · 3.5 KiBRaw
1// EverySync Apple shells — Keychain-backed `SecureStore` notes.
2//
3// The KMP `SecureStore` interface (`platform/Platform.kt`: `getBytes /
4// `setBytes(onlyIfAbsent)` / `delete` / `getString` / `setString` / `getLong` /
5// `setLong`) is implemented in KOTLIN (`IOS_ACTUALS_NOTES.md`), not here.
6// This file documents the exact Keychain contract the Kotlin actual must
7// implement so all three apps + extensions agree on one layout.
8//
9// DO NOT re-implement `SecureStore` in Swift. Swift calls the XCFramework's
10// `SecureStore` (backed by Keychain per this contract); vault AES keys never
11// appear in Swift values, logs, or share links.
12//
13// Keychain layout contract (all entries `kSecClassGenericPassword`):
14// - Service: `com.vayunmathur.everysync.securestore`
15// - Account: the `SecureStore` key verbatim (e.g. `personalKemPriv`,
16// `relay_cursor:todos:<vault>:todos:<vault>`, `personalDeviceId`).
17// - Value: `kSecValueData` = raw bytes (strings UTF-8, longs big-endian Int64).
18// - Access group: `$(AppIdentifierPrefix)com.vayunmathur.everysync.shared`
19// so the main apps + Credential Provider + Share extension share one store.
20// - Accessibility: `kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly`
21// (no iCloud sync — relay keys never leave the device except via
22// PQC-sealed inbox invites).
23//
24// Semantics mirror `MemorySecureStore` + the Android KeyStore actual:
25// - `get*` returns nil when absent (absence is NOT an error).
26// - `set*(onlyIfAbsent: true)` is atomic: check-then-add must hold the
27// Keychain entry (Kotlin serializes with a mutex, same as `MemorySecureStore`).
28// - `delete` removes bytes+strings+longs rows for the name (single Keychain
29// item per name here, so one `SecItemDelete`).
30// - `getLong` decodes 8-byte big-endian; malformed data returns nil (caller
31// treats as absent, same as the relay-cursor default 0).
32//
33// Key-hygiene invariant (verbatim): vault keys never user-visible. Share links
34// carry ids only (`RelayLinks.shareLinkFor`); keys move exclusively via
35// PQC-sealed inbox invites (`inbox:<deviceId>`). `RelayCursorStore` keys
36// (`relay_cursor:<type>:<vault>:<channel>`) live here, NOT in the IPC tables.
37
38import Foundation
39import Security
40
41/// Compile-time documentation of the Keychain layout. No instances: the live
42/// implementation is the Kotlin `SecureStore` actual in the XCFramework.
43public enum KeychainSecureStoreContract {
44 public static let service = "com.vayunmathur.everysync.securestore"
45 public static let accessGroup = "com.vayunmathur.everysync.shared"
46
47 /// Keys the transport bootstrap writes (see `SharedRelayTransport.init`):
48 /// PQC identity (`personalKemPub/Priv`, `personalDsaPub/Priv`) + stable
49 /// device id (`personalDeviceId`). `onlyIfAbsent` on first write wins
50 /// concurrent races — re-read after write, exactly like
51 /// `PqcIdentityHandle.loadOrCreate`.
52 public static let identityKeys = [
53 "personalDeviceId",
54 "personalKemPub", "personalKemPriv",
55 "personalDsaPub", "personalDsaPriv",
56 ]
57
58 /// Longs are 8-byte big-endian (matches `RelayCursorStore` via `setLong`).
59 public static func encodeLong(_ value: Int64) -> Data {
60 var bigEndian = value.bigEndian
61 return Data(bytes: &bigEndian, count: MemoryLayout<Int64>.size)
62 }
63
64 public static func decodeLong(_ data: Data) -> Int64? {
65 guard data.count == MemoryLayout<Int64>.size else { return nil }
66 return data.withUnsafeBytes { $0.load(as: Int64.self).bigEndian }
67 }
68}