feat(android): Share to Shade — a video from the gallery is sealed on the phone and uploaded

ShadeUpload turns a shared content:// URI into meta and an upload job;
KeystoreSecretFile keeps the owner seed under a Keystore key. The demo app
takes SEND/SEND_MULTIPLE of video, images, audio and documents, uploads in a
foreground service with progress, and its done notification plays the file
or shares the link; the player gets a Share button. The live cross-check
also uploads a content:// share under Robolectric.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-10-19 00:28:36 +02:00
parent b53dcd7813
commit 0a706cdb47
16 changed files with 733 additions and 11 deletions

View File

@@ -0,0 +1,68 @@
package no.zyon.shade.keystore
import java.io.File
import java.io.IOException
import java.security.GeneralSecurityException
import java.security.SecureRandom
/**
* A small secret (a signing seed, a token) kept in a file, encrypted under a
* hardware-backed Android Keystore key that never leaves the secure element.
* For secrets the app uses without asking the user each time — a media owner
* key that signs every upload piece — so the key is not biometric-gated;
* use [KeystoreMasterKey] with `requireBiometric` for those that should be.
*
* File: `"SKS1" || nonce(12) || AES-256-GCM(secret)`, with the alias as AAD
* so a file cannot be swapped in under another alias. A backup restored to
* another phone carries the file but not the key: [read] then throws
* [SecretLostException] rather than handing back garbage.
*/
class KeystoreSecretFile(private val file: File, private val alias: String) {
private val key = KeystoreMasterKey(alias, requireBiometric = false)
class SecretLostException(message: String, cause: Throwable? = null) : IOException(message, cause)
/** The secret, or null if none was written. */
fun read(): ByteArray? {
if (!file.exists()) return null
val b = file.readBytes()
if (b.size < HEADER + 16 || !b.copyOfRange(0, 4).contentEquals(MAGIC)) throw SecretLostException("${file.name} is not a Keystore secret")
if (!key.exists()) throw SecretLostException("the Keystore key for ${file.name} is gone (restored from a backup, or the app's keys were cleared)")
return try {
val c = key.cipherForDecrypt(b.copyOfRange(4, HEADER))
c.updateAAD(alias.toByteArray(Charsets.UTF_8))
c.doFinal(b, HEADER, b.size - HEADER)
} catch (e: GeneralSecurityException) {
throw SecretLostException("${file.name} does not open under its Keystore key", e)
}
}
fun write(secret: ByteArray) {
val c = key.cipherForEncrypt()
c.updateAAD(alias.toByteArray(Charsets.UTF_8))
val ct = c.doFinal(secret)
val nonce = c.iv
check(nonce.size == 12) { "unexpected GCM nonce size ${nonce.size}" }
file.parentFile?.mkdirs()
val tmp = File(file.path + ".tmp")
tmp.writeBytes(MAGIC + nonce + ct)
if (!tmp.renameTo(file)) throw IOException("cannot write ${file.name}")
}
/** The secret, made with `size` random bytes on first use. */
fun getOrCreate(size: Int): ByteArray = read() ?: ByteArray(size).also {
SecureRandom().nextBytes(it)
write(it)
}
/** Remove the file and its key: the secret is gone for good. */
fun destroy() {
file.delete()
key.deleteKey()
}
private companion object {
val MAGIC = "SKS1".toByteArray(Charsets.US_ASCII)
const val HEADER = 4 + 12
}
}