Implemet mailstones 0-1
This commit is contained in:
@@ -9,16 +9,44 @@ import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.ee2e.app.crypto.DeviceIdentity
|
||||
import com.ee2e.app.crypto.DeviceIdentityKeyStore
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
private val identityExecutor = Executors.newSingleThreadExecutor()
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContent { EE2EHome() }
|
||||
setContent { EE2EHome(isLoadingIdentity = true) }
|
||||
|
||||
identityExecutor.execute {
|
||||
val result = runCatching { DeviceIdentityKeyStore().getOrCreate() }
|
||||
runOnUiThread {
|
||||
if (!isFinishing && !isDestroyed) {
|
||||
setContent {
|
||||
EE2EHome(
|
||||
identity = result.getOrNull(),
|
||||
identityUnavailable = result.isFailure,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
identityExecutor.shutdown()
|
||||
super.onDestroy()
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun EE2EHome() {
|
||||
private fun EE2EHome(
|
||||
identity: DeviceIdentity? = null,
|
||||
isLoadingIdentity: Boolean = false,
|
||||
identityUnavailable: Boolean = false,
|
||||
) {
|
||||
MaterialTheme {
|
||||
Surface(modifier = Modifier.fillMaxSize()) {
|
||||
Column(
|
||||
@@ -31,7 +59,19 @@ private fun EE2EHome() {
|
||||
Text("Development scaffold · v0.1.0")
|
||||
Spacer(Modifier.height(24.dp))
|
||||
Text("Secure calling is not implemented yet.")
|
||||
Text("Next: device identity and cryptographic proof of concept.")
|
||||
Spacer(Modifier.height(24.dp))
|
||||
when {
|
||||
isLoadingIdentity -> Text("Preparing local device identity…")
|
||||
identity != null -> {
|
||||
Text("Local P-256 signing key is stored in Android Keystore.")
|
||||
Spacer(Modifier.height(8.dp))
|
||||
Text("SHA-256 fingerprint")
|
||||
Text(identity.fingerprint)
|
||||
Spacer(Modifier.height(8.dp))
|
||||
Text("Development only: this key is not the selected PQXDH identity protocol.")
|
||||
}
|
||||
identityUnavailable -> Text("Could not provision local device identity.")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.ee2e.app.crypto
|
||||
|
||||
import android.security.keystore.KeyGenParameterSpec
|
||||
import android.security.keystore.KeyProperties
|
||||
import java.security.KeyPair
|
||||
import java.security.KeyPairGenerator
|
||||
import java.security.KeyStore
|
||||
import java.security.MessageDigest
|
||||
import java.security.PrivateKey
|
||||
import java.security.Signature
|
||||
import java.security.spec.ECGenParameterSpec
|
||||
import java.util.Base64
|
||||
|
||||
/**
|
||||
* Local device identity storage scaffold.
|
||||
*
|
||||
* This classical P-256 key is not the selected PQXDH identity protocol and does
|
||||
* not by itself authenticate a peer. The private key is kept in Android Keystore.
|
||||
*/
|
||||
class DeviceIdentityKeyStore {
|
||||
@Synchronized
|
||||
fun getOrCreate(): DeviceIdentity {
|
||||
val keyStore = KeyStore.getInstance(ANDROID_KEY_STORE).apply { load(null) }
|
||||
val keyPair = if (keyStore.containsAlias(ALIAS)) {
|
||||
val privateKey = keyStore.getKey(ALIAS, null) as? PrivateKey
|
||||
?: error("Stored device identity private key is unavailable")
|
||||
val publicKey = keyStore.getCertificate(ALIAS)?.publicKey
|
||||
?: error("Stored device identity certificate is unavailable")
|
||||
require(privateKey.algorithm == "EC" && publicKey.algorithm == "EC") {
|
||||
"Stored device identity key has an unsupported algorithm"
|
||||
}
|
||||
KeyPair(publicKey, privateKey)
|
||||
} else {
|
||||
KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_EC, ANDROID_KEY_STORE).run {
|
||||
initialize(
|
||||
KeyGenParameterSpec.Builder(
|
||||
ALIAS,
|
||||
KeyProperties.PURPOSE_SIGN or KeyProperties.PURPOSE_VERIFY,
|
||||
)
|
||||
.setAlgorithmParameterSpec(ECGenParameterSpec(CURVE))
|
||||
.setDigests(KeyProperties.DIGEST_SHA256)
|
||||
.build(),
|
||||
)
|
||||
generateKeyPair()
|
||||
}
|
||||
}
|
||||
|
||||
val encodedPublicKey = keyPair.public.encoded
|
||||
val fingerprint = MessageDigest.getInstance("SHA-256")
|
||||
.digest(encodedPublicKey)
|
||||
.joinToString("") { byte -> "%02X".format(byte.toInt() and 0xFF) }
|
||||
.chunked(2)
|
||||
.joinToString(":")
|
||||
|
||||
return DeviceIdentity(
|
||||
publicKeyBase64 = Base64.getEncoder().encodeToString(encodedPublicKey),
|
||||
fingerprint = fingerprint,
|
||||
)
|
||||
}
|
||||
|
||||
fun sign(payload: ByteArray): ByteArray {
|
||||
val keyStore = KeyStore.getInstance(ANDROID_KEY_STORE).apply { load(null) }
|
||||
val privateKey = keyStore.getKey(ALIAS, null) as? PrivateKey
|
||||
?: error("Device identity key has not been provisioned")
|
||||
return Signature.getInstance(SIGNATURE_ALGORITHM).run {
|
||||
initSign(privateKey)
|
||||
update(payload)
|
||||
sign()
|
||||
}
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val ALIAS = "ee2e.device.identity.p256.v1"
|
||||
const val ANDROID_KEY_STORE = "AndroidKeyStore"
|
||||
const val CURVE = "secp256r1"
|
||||
const val SIGNATURE_ALGORITHM = "SHA256withECDSA"
|
||||
}
|
||||
}
|
||||
|
||||
data class DeviceIdentity(
|
||||
val publicKeyBase64: String,
|
||||
val fingerprint: String,
|
||||
)
|
||||
Reference in New Issue
Block a user