Device identity and key management
The device identity and key management service gives the app a stable local device identity and a protected end-to-end encryption key pair. It keeps the public key available for local and cloud registration while keeping private key use inside the Android keystore, then replaces old keys without losing the ability to decrypt in-flight messages. See Security and encryption for the wider encryption model and Local HTTP API for the local service contract.
Device identity
The local device identity identifies this app installation to clients of the local HTTP service. The service creates it when the local server starts if no identity is stored, then reuses the stored value for device responses and message-related device checks.
The identity is separate from the end-to-end encryption key. The local server keeps the identity and its access settings in local settings, while the device service manages the encryption key pair and its versions.
References: app/…/LocalServerSettings.kt• L7-16 app/…/LocalServerService.kt• L19-35
Device settings
The device settings control whether automatic key rotation is active and how often the service should replace the current key. The app exposes rotation as disabled or as a 30-, 60-, or 90-day interval; the setting is read again for each due check, so a changed value controls later work without changing stored key records.
The device settings screen also provides two identity-related actions. An engineer or user can request an immediate key rotation and can copy the fingerprint of the current public key for out-of-band verification.
References: app/…/DeviceSettings.kt• L7-31 app/…/device_preferences.xml• L4-24 app/…/arrays.xml• L56-67 app/…/DeviceSettingsFragment.kt• L42-111
Key records
Each device key has a version, a Base64 public key, creation time, and optional retirement time. Room stores these records in the device_keys table and treats the newest non-retired record as the current key. The record is the durable link between a key version used in encrypted data and the private key entry that the Android keystore serves.
The record does not contain private key material. A version remains addressable after retirement so old encrypted messages can still select the correct private key during the retention period.
| Record state | Meaning | Private-key use |
|---|---|---|
| Active | Current public key for new encryption and registration | Available for current operations |
| Retired | Replaced key kept for old ciphertext | Available until the retention cutoff |
| Purged | Retired record and keystore entry removed | No longer available |
References: app/…/DeviceKey.kt• L8-29 app/…/DeviceKeysDao.kt• L8-31 app/…/AppDatabase.kt• L28-42
Keystore-backed keys
The key store creates RSA-2048 key pairs in AndroidKeyStore across supported Android API levels. The public key leaves the keystore as Base64 data for storage and registration, but the private key stays in AndroidKeyStore and is loaded only when decryption needs a given version.
The device service caches loaded private keys for the process. Rotation, retirement, and cleanup clear the affected cache entries so an old private key is not served after its record is no longer valid.
References: app/…/KeyStore.kt• L17-54 app/…/DeviceService.kt• L30-45 app/…/RSAEncryptionProvider.kt• L62-85
Key fingerprints
The key fingerprint is a short value for checking that a public key was received from the expected device. The service decodes the active public key and hashes its X.509 SubjectPublicKeyInfo bytes with SHA-256, then displays the result as uppercase hexadecimal in 16 groups separated by colons.
The settings screen reads the current fingerprint when it resumes and copies the same value to the clipboard when selected. If no active key exists or the stored public key cannot be decoded, the screen shows that the fingerprint is not available.
References: app/…/Fingerprint.kt• L5-24 app/…/DeviceService.kt• L218-231 app/…/DeviceSettingsFragment.kt• L77-103
Key rotation
Key rotation replaces the active public key while preserving old private keys long enough to decrypt messages already encrypted with them. A single rotation lock makes creation, retirement, cleanup, and private-key loading agree on which key versions can be served.
When rotation starts, the service creates the next version and persists its public record before retiring older active records. It then clears stale cached private keys and performs best-effort cleanup. If record persistence fails, the newly created keystore entry is removed to avoid an untracked key.
Automatic rotation is checked by a daily background worker when the interval is enabled. The same worker also purges expired retired keys, even when rotation is disabled or the current key is not yet due. Manual rotation uses the same service path and can be started from the device settings screen.
flowchart TD
A[Rotation setting or manual action] --> B[Device service checks current key]
B -->|No key| C[Create baseline key]
B -->|Due or manual| D[Generate next key version]
D --> E[Store public key record]
E --> F[Retire older active records]
F --> G[Emit rotation event]
G --> H[Publish new public key and version]
C --> I[Keep key active]
H --> J[Retain old keys for decryption]
J --> K[Delete expired keystore entries and records]
References: app/…/DeviceService.kt• L47-186 app/…/DeviceService.kt• L233-295 app/…/KeyRotationWorker.kt• L18-63 app/…/DeviceSettingsFragment.kt• L60-74
Rotation events
The device service emits DeviceKeyRotatedEvent only when it replaces an existing key. The gateway listens for this event and starts registration work so the server receives the new public key and version. Initial key creation does not emit the event because registration and device-update flows already include the key data.
The event therefore connects local key state to the remote device record without making encryption wait for a network request. If gateway use is disabled, the listener ignores the event and no remote update is started.
References: app/…/DeviceKeyRotatedEvent.kt• L3-8 app/…/DeviceService.kt• L158-169 app/…/EventsReceiver.kt• L118-126 app/…/GatewayService.kt• L180-207
Local identity API
The local HTTP service exposes the device as a one-item list through both /device and /devices. The route requires the devices:list scope and returns the local device ID together with device details, active SIM data, and the current public key and key version when a key is available.
The route calls ensureKey before building the response. A client can therefore use the device response as the local view of the public encryption identity, while private key material remains inaccessible through this API.
sequenceDiagram
participant C as Local client
participant H as Local HTTP service
participant D as Device service
participant K as Android keystore
C->>H: Authenticated GET /device or /devices
H->>H: Check devices:list scope
H->>D: Ensure active key
D->>K: Create or load key pair as needed
D-->>H: Public key and version
H-->>C: Device identity and public device data
References: app/…/WebService.kt• L147-158 app/…/DeviceRoutes.kt• L17-51 app/…/Device.kt• L5-30 app/…/AuthScopes.kt• L11-17
Change checklist
When these files or sections are changed, remember to consider these:
- When changing key record fields or retirement rules, keep the Room schema, key-version lookup, keystore alias mapping, and cleanup order aligned.
- When changing rotation timing or its setting values, keep the settings key, UI entry values, background schedule, and due-check behavior aligned.
- When changing the public key or version wire fields, update both local device responses and cloud registration or device-update requests.
- When changing the rotation event, preserve the gateway listener that publishes the new public key before old key material is purged.
- When changing local device routes or scopes, keep
/device,/devices, and thedevices:listauthorization contract consistent.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A new key record cannot be stored after keystore generation | The new key would otherwise be orphaned and no durable version would point to it | The service attempts to delete the new keystore entry and reports the original failure |
| A retired key is needed after its retention cutoff | Decryption fails because the version is no longer available | Retired keys remain for seven days and are selected by the version in the encrypted payload during that period |
| Keystore deletion fails during cleanup | The old key record remains and cleanup can retry instead of losing state | The service keeps the Room row and logs a warning |
| A key rotates while the gateway is disabled | The remote server does not receive the new public key at that time | The rotation event is ignored until a later registration or update path runs |
| The stored public key is missing or cannot be decoded | The local device response or fingerprint cannot include a usable key value | ensureKey creates a key when possible, and fingerprint display reports unavailable when decoding fails |
Key files
| File | Why to read it |
|---|---|
| app/…/DeviceService.kt | Main lifecycle for key creation, rotation, retention, fingerprint lookup, and private-key access |
| app/…/KeyStore.kt | Boundary that keeps private keys in AndroidKeyStore and exposes public key data |
| app/…/DeviceKey.kt | Durable shape and state of a device key record |
| app/…/DeviceKeysDao.kt | Queries that define current, retired, and expired key behavior |
| app/…/KeyRotationWorker.kt | Daily scheduling and retry behavior for rotation and cleanup |
| app/…/EventsReceiver.kt | Gateway response to a completed key rotation |
| app/…/DeviceRoutes.kt | Local API response that exposes device identity and the active public key |
| app/…/DeviceSettingsFragment.kt | User-facing manual rotation and fingerprint actions |