onionwire/android/README.md

203 lines
8.8 KiB
Markdown
Raw Normal View History

# OnionWire for Android
Two artifacts out of one Gradle build:
| Module | Output | What it is |
|---|---|---|
| `:sdk` | `sdk-release.aar` | The reusable library. UniFFI Kotlin bindings + `libonionwire_sdk.so`. Any Android app can depend on this. |
| `:app` | `app-release.apk` / `app-debug.apk` | A Jetpack Compose + Material 3 messenger that depends on `:sdk` and nothing else. |
The Linux TUI in the repository root is a **separate product** with a separate
Cargo workspace. Nothing here turns it into a WebView, and nothing here imports
its TUI code. Same wire protocol: [docs/PROTOCOL.md](../docs/PROTOCOL.md).
## Requirements
* JDK 17+ (built and tested on JDK 21)
* Android SDK with **platform 36** and **build-tools 36.0.0**
* NDK **28.2.13676358** (NDK r28c) — also what Rust's Android link step uses
* Rust with the `aarch64-linux-android` target and `cargo-ndk`
* `ANDROID_HOME` set, or `sdk.dir` in `android/local.properties` (gitignored)
One-time setup:
```bash
sdkmanager --install "platform-tools" "platforms;android-36" \
"build-tools;36.0.0" "ndk;28.2.13676358"
rustup target add aarch64-linux-android x86_64-linux-android
cargo install cargo-ndk
```
## Build
```bash
cd android
./gradlew :sdk:assembleRelease :app:assembleDebug # or :app:assembleRelease
```
The Rust cross-compile and the UniFFI Kotlin bindings are wired into the Gradle
tasks — there is no manual codegen step. `:sdk:preBuild` depends on them.
Emulator / x86_64 slice:
```bash
./gradlew :app:assembleDebug -Ponionwire.abis=arm64-v8a,x86_64
```
Or from the repo root, with checksums and an optional publish step:
```bash
scripts/build-android-local.sh # -> dist/onionwire-<v>-android-arm64-v8a.apk
PUBLISH_TAG=v0.1.0 scripts/build-android-local.sh
```
## Decisions you should know about
### `minSdk = 26` (Android 8.0)
The floor is not NDK-imposed — Rust's `aarch64-linux-android` std and NDK 28
both happily target API 21. We take 26 deliberately:
* it is the first API level where adaptive icons and notification channels are
unconditional, so there is one icon path instead of two;
* it keeps a single `arm64-v8a` APK inside Play's currently-supported range
without carrying legacy ART/JIT workarounds for pre-O devices;
* the crypto stack (`ring`, `rustls`, `argon2`, `chacha20poly1305`) is
exercised on 4.4+ kernels here, which is what we actually test.
**Devices cut:** Android 7.1 and older. If you need those, the change is
`-Ponionwire.api=24` **and** a real device test — do not bump it silently.
### 16 KB page size
The NDK r28 link step emits `max-page-size=16384` for 64-bit Android, which is
what Play requires for new apps. Verify with:
```bash
readelf -l android/sdk/build/rustJniLibs/arm64-v8a/libonionwire_sdk.so | grep LOAD
```
### Permissions
`INTERNET` only, declared at first launch. No camera (invites are pasted, not
scanned), no contacts, no storage permission — the data directory is
`context.filesDir`, which is already app-private. There is **no**
`FOREGROUND_SERVICE` and no keep-alive notification in v1: the node lives only
while the process does, and the UI says so. Adding one is a product decision
(battery, Play's FGS type declarations), not a silent fix.
### Data at rest
`context.filesDir/onionwire/` (mode 0700), holding `onionwire.db` and the Arti
state dir. `android:allowBackup="false"` plus explicit `data_extraction_rules`
exclusions, because an auto-backup of `onionwire.db` would hand the wrapped
message key *and* the plaintext identity keys to Google Drive. Chat bodies are
encrypted at rest; identity keys are not — that is the locked v1 posture.
feat(android): wrap the store passphrase in the Android Keystore Typing the passphrase on every launch was the v1 posture. It stays the fallback, but it is no longer required: on first unlock the passphrase can be remembered (checkbox, on by default) and is stored as an AES-256-GCM blob wrapped by a key that never leaves the Android Keystore. - crypto/StoreSecret.kt: the OWSEAL1 envelope (magic + IV + ciphertext + GCM tag) and the wipe helpers. No android.* imports, so the code that runs on a device is the code the unit tests exercise, with a JVM AES key standing in for the Keystore key. - crypto/KeystorePassphraseCipher.kt: AES-256-GCM key under alias `onionwire.store`, StrongBox requested first (API 28+) with a graceful fallback on ProviderException (StrongBoxUnavailableException is one), and an honest KeyInfo-backed report of where the key actually lives. setUserAuthenticationRequired(false) stays the default on purpose. - crypto/StoredPassphraseStore.kt: ciphertext only, at filesDir/secrets/store-passphrase.bin, written tmp+rename. A failed decrypt reports Unavailable and does NOT delete the blob. - WireViewModel: unlock(passphrase, remember), unlockStored(), forgetStoredPassphrase(); the secret CharArray is zeroed in `finally`; the Keystore probe runs off the main thread at ViewModel construction. - UnlockScreen: one-tap when a stored blob exists, the typed passphrase is dropped from Compose state as soon as it is handed over and is never put in saved instance state; "Forget stored passphrase" deletes blob and key. - androidTest: the device-only half (AndroidKeyStore, StrongBox, KeyInfo), which cannot run in the unit-test JVM. Verified: ./gradlew :app:assembleDebug and :app:testDebugUnitTest green, 25 tests / 0 failures (11 new StoreSecretTest, 9 new StoredPassphraseStoreTest).
2026-09-12 13:01:25 -04:00
### The store passphrase at rest
Typing the passphrase on every launch is no longer required. On first unlock
the passphrase can be remembered (checkbox, on by default), and it is then
stored as an AES-256-GCM blob:
* the wrapping key is generated inside the **Android Keystore** under alias
`onionwire.store`, AES-256-GCM, `setUserAuthenticationRequired(false)` —
deliberately **not** biometric-gated, because a fingerprint prompt would turn
"the node opens or tells you why it did not" into "the node is unusable";
* **StrongBox is requested first** (`setIsStrongBoxBacked(true)`, guarded by
`FEATURE_STRONGBOX_KEYSTORE` on API 28+) and falls back to an ordinary
TEE-backed or software Keystore key when the chip refuses AES-256
(`StrongBoxUnavailableException is a ProviderException`). The fallback is
reported, not hidden: the lock screen prints what `KeyInfo` actually said,
and a software key is labelled **NOT hardware-backed**;
* only ciphertext + IV reach disk, at
`filesDir/secrets/store-passphrase.bin` — an `OWSEAL1` envelope of
magic + IV + ciphertext + GCM tag. No second copy, no `SharedPreferences`,
no plaintext in logs, and the typed `CharArray` is zeroed as soon as the
unlock call returns. The text field is a plain `remember`, so the typed
passphrase is never written into saved instance state;
* **Keystore invalidation is a supported path, not a crash.** A factory reset,
an app-data wipe, or a key the platform invalidates leaves a blob that cannot
authenticate: `StoredPassphraseStore.load()` returns `Unavailable`, the lock
screen explains it, and the user types the passphrase again. A failed read
does **not** delete the blob — a transient Keystore failure must not destroy
the only copy;
* `forget` deletes the blob **and** the Keystore key.
The one plaintext copy this cannot erase is the immutable `String` the UniFFI
binding takes (`openWire(home, passphrase: String)`). A follow-up SDK change
should accept the key (or a `ByteArray`) so the passphrase never becomes a
`String` at all.
`app/src/test/.../crypto/` covers the envelope and the file handling on the JVM
(the real `StoreSecret` code, with a JVM AES key standing in). The Keystore
provider only exists on a device:
```bash
./gradlew :app:connectedDebugAndroidTest # prints the KeyInfo/hardware report
```
### Signing
Release APKs are signed with a **repo-owned keystore**, not the debug key.
* CI reads `ANDROID_KEYSTORE_BASE64`, `ANDROID_KEYSTORE_PASSWORD`,
`ANDROID_KEY_ALIAS`, and `ANDROID_KEY_PASSWORD` from Forgejo repo secrets
and writes them to `~/.android/onionwire-release.env` for the build.
* Locally, `scripts/build-android-local.sh` sources that same env file if
present (`~/.android/onionwire-release.jks` + `.env`, mode 600). Local and
CI APKs then verify as the same cert and can upgrade in place.
* If the env file is missing, Gradle **warns** and signs with the debug
keystore so a laptop build still installs. CI sets
`ONIONWIRE_REQUIRE_RELEASE_KEY=true` and **fails closed** instead — a
per-machine debug key would make every CI APK uninstallable over the last.
* Never commit a keystore, password, or `android/local.properties`. This is
not a Play upload key; nothing here should be uploaded to Play.
### Release builds are not minified
`isMinifyEnabled = false` for release. Keeps for the UniFFI/JNI surface already
exist in `sdk/consumer-rules.pro`, but enabling R8 without a mapping-file upload
path and an on-device test of the JNI surface is how you ship an
`UnsatisfiedLinkError` nobody can read. Turn it on together with crash
deobfuscation, not before.
## CI
`.forgejo/workflows/android-release.yml` runs on `docker-amd64` (T480 dind).
The job image is `node:20-bullseye` (EOL) — **do not apt-get there**. The
toolchain runs in a sibling `debian:bookworm` container
(stdin-tar + `docker cp`, same pattern as the Rust CI job).
Any push to `main` that touches Android **sources or build config**
(`android/app/**`, `android/sdk/**`, Gradle files — not `android/README.md`)
workflow) builds `onionwire-<version>-android-arm64-v8a.apk` and
`onionwire-sdk-<version>.aar`, verifies them, and attaches them to the Forgejo
release for that tag. Version is the latest `v*.*.*` tag plus patch, or
**v0.1.0** when there are no tags. `versionName` matches the tag;
`versionCode` is `major*10000 + minor*100 + patch` (so 0.1.0 → 100).
`workflow_dispatch` with `dry_run=true` builds and checksums without creating
a tag or publishing. `scripts/build-android-local.sh` remains the offline
fallback; it does not create tags.
## Layout
```
android/
settings.gradle.kts
build.gradle.kts # plugin aliases only
gradle/libs.versions.toml # version catalog
sdk/ # -> AAR
build.gradle.kts # cargo-ndk + uniffi-bindgen wiring
consumer-rules.pro
src/main/AndroidManifest.xml
app/ # -> APK
build.gradle.kts
src/main/java/com/siriusdevops/onionwire/
MainActivity.kt # NavHost
WireViewModel.kt # owns the single Wire node
feat(android): wrap the store passphrase in the Android Keystore Typing the passphrase on every launch was the v1 posture. It stays the fallback, but it is no longer required: on first unlock the passphrase can be remembered (checkbox, on by default) and is stored as an AES-256-GCM blob wrapped by a key that never leaves the Android Keystore. - crypto/StoreSecret.kt: the OWSEAL1 envelope (magic + IV + ciphertext + GCM tag) and the wipe helpers. No android.* imports, so the code that runs on a device is the code the unit tests exercise, with a JVM AES key standing in for the Keystore key. - crypto/KeystorePassphraseCipher.kt: AES-256-GCM key under alias `onionwire.store`, StrongBox requested first (API 28+) with a graceful fallback on ProviderException (StrongBoxUnavailableException is one), and an honest KeyInfo-backed report of where the key actually lives. setUserAuthenticationRequired(false) stays the default on purpose. - crypto/StoredPassphraseStore.kt: ciphertext only, at filesDir/secrets/store-passphrase.bin, written tmp+rename. A failed decrypt reports Unavailable and does NOT delete the blob. - WireViewModel: unlock(passphrase, remember), unlockStored(), forgetStoredPassphrase(); the secret CharArray is zeroed in `finally`; the Keystore probe runs off the main thread at ViewModel construction. - UnlockScreen: one-tap when a stored blob exists, the typed passphrase is dropped from Compose state as soon as it is handed over and is never put in saved instance state; "Forget stored passphrase" deletes blob and key. - androidTest: the device-only half (AndroidKeyStore, StrongBox, KeyInfo), which cannot run in the unit-test JVM. Verified: ./gradlew :app:assembleDebug and :app:testDebugUnitTest green, 25 tests / 0 failures (11 new StoreSecretTest, 9 new StoredPassphraseStoreTest).
2026-09-12 13:01:25 -04:00
crypto/ # Keystore-wrapped store passphrase
ui/ # Compose screens (M3)
feat(android): wrap the store passphrase in the Android Keystore Typing the passphrase on every launch was the v1 posture. It stays the fallback, but it is no longer required: on first unlock the passphrase can be remembered (checkbox, on by default) and is stored as an AES-256-GCM blob wrapped by a key that never leaves the Android Keystore. - crypto/StoreSecret.kt: the OWSEAL1 envelope (magic + IV + ciphertext + GCM tag) and the wipe helpers. No android.* imports, so the code that runs on a device is the code the unit tests exercise, with a JVM AES key standing in for the Keystore key. - crypto/KeystorePassphraseCipher.kt: AES-256-GCM key under alias `onionwire.store`, StrongBox requested first (API 28+) with a graceful fallback on ProviderException (StrongBoxUnavailableException is one), and an honest KeyInfo-backed report of where the key actually lives. setUserAuthenticationRequired(false) stays the default on purpose. - crypto/StoredPassphraseStore.kt: ciphertext only, at filesDir/secrets/store-passphrase.bin, written tmp+rename. A failed decrypt reports Unavailable and does NOT delete the blob. - WireViewModel: unlock(passphrase, remember), unlockStored(), forgetStoredPassphrase(); the secret CharArray is zeroed in `finally`; the Keystore probe runs off the main thread at ViewModel construction. - UnlockScreen: one-tap when a stored blob exists, the typed passphrase is dropped from Compose state as soon as it is handed over and is never put in saved instance state; "Forget stored passphrase" deletes blob and key. - androidTest: the device-only half (AndroidKeyStore, StrongBox, KeyInfo), which cannot run in the unit-test JVM. Verified: ./gradlew :app:assembleDebug and :app:testDebugUnitTest green, 25 tests / 0 failures (11 new StoreSecretTest, 9 new StoredPassphraseStoreTest).
2026-09-12 13:01:25 -04:00
src/test/ # JVM tests (envelope, store, invite flow)
src/androidTest/ # device-only: AndroidKeyStore
```