package com.peachpasswords.sync import kotlinx.serialization.builtins.serializer import kotlinx.serialization.json.Json import org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters /** 0x03 adds relay routing to each introduced peer edge. */ const val DEVICE_MANIFEST_VERSION = 0x03 private const val DEVICE_MANIFEST_FRESHNESS_MS = 5 * 60_000L /** * Upper bounds on what verifyDeviceManifest will accept. A pathological peer * could otherwise craft a manifest that consumes unbounded memory or skips * sanity checks (e.g. `issuedAt = Long.MIN_VALUE` bypasses `abs(...) > window` * because `abs(Long.MIN_VALUE)` is still negative). These caps match the * realistic limits of the pairing protocol: a handful of paired devices, a * bounded revocation list, and string fields long enough for any honest use. */ private const val MAX_MANIFEST_DEVICES = 256 private const val MAX_MANIFEST_REVOCATIONS = 500 private const val MAX_MANIFEST_STRING = 256 internal const val NO_KEY_SENTINEL = "0000000000000000000000000000000000000000000000000000000000000000" interface PairedDeviceRegistry { fun getAll(): List fun get(id: String): PairedDevice? fun getByPubkey(pubkeyHex: String): PairedDevice? = getAll().firstOrNull { it.longTermPublicKeyHex?.let { key -> CryptoCompare.hex(key, pubkeyHex) } == true } fun upsert(device: PairedDevice) fun remove(deviceId: String) {} fun listRevocations(): List = emptyList() /** Returns the ids that were not already revoked. */ fun mergeRevocations(incoming: List): List = emptyList() /** * True when either the id or the long-term key is under a terminal * revocation. `deviceId` is a self-asserted string in a manifest, so * keying terminality on it alone is bypassed by re-vouching a revoked * device's key under a fresh id. The key is what a revoked device still * holds, so it is what terminality has to follow. `0`×64 is the "no key was * ever stored" sentinel (see PairedDeviceStore.revoke) and matches nothing. */ fun isRevoked(deviceId: String, longTermPublicKeyHex: String? = null): Boolean { val key = longTermPublicKeyHex?.lowercase()?.takeIf { it != NO_KEY_SENTINEL } return listRevocations().any { it.deviceId == deviceId || (key != null && it.longTermPublicKeyHex.lowercase() == key) } } /** Only a direct, SAS-confirmed re-pair may lift a terminal revocation. */ fun clearRevocation(deviceId: String) {} } fun buildSignedDeviceManifest( issuerDeviceId: String, devices: List, signingPrivateKey: Ed25519PrivateKeyParameters, nowMs: Long = System.currentTimeMillis(), revoked: List = emptyList(), ): SignedDeviceManifest { require(issuerDeviceId.isNotBlank()) { "issuerDeviceId is required" } val body = DeviceManifestBody( manifestVersion = DEVICE_MANIFEST_VERSION, issuerDeviceId = issuerDeviceId, issuedAt = nowMs, // Normalise valid entries but keep invalid ones too — verifyDeviceManifest // is the gate that detects malformed input (M-09: silent drop would let // a caller believe they sent a valid manifest). devices = devices.map { normaliseManifestEntry(it) ?: it }, revoked = revoked.map(::normaliseRevocation), ) val signature = signEd25519(signingPrivateKey, canonicalDeviceManifestBody(body).toByteArray(Charsets.UTF_8)) return SignedDeviceManifest(body, pubkeyToHex(signature)) } fun verifyDeviceManifest( manifest: SignedDeviceManifest, issuerSigningPublicKey: ByteArray, nowMs: Long = System.currentTimeMillis(), freshnessMs: Long = DEVICE_MANIFEST_FRESHNESS_MS, ): DeviceManifestBody? { val body = manifest.body if (body.manifestVersion != DEVICE_MANIFEST_VERSION) return null // Two-sided bounds — avoids `abs(...) > window`, which overflows for // `Long.MIN_VALUE` and lets a peer-presented timestamp bypass the // freshness gate. Reject negative `issuedAt` outright so the bounds // arithmetic can't be steered into a wrap-around by the manifest. if (body.issuedAt < 0L) return null if (body.issuedAt < nowMs - freshnessMs || body.issuedAt > nowMs + freshnessMs) return null if (body.issuerDeviceId.isBlank() || body.issuerDeviceId.length > MAX_MANIFEST_STRING) return null if (body.devices.size > MAX_MANIFEST_DEVICES) return null if (body.revoked.size > MAX_MANIFEST_REVOCATIONS) return null if (!isHex(manifest.signatureHex, 128)) return null if (!body.devices.all { normaliseManifestEntry(it) != null }) return null if (!body.revoked.all { isValidRevocation(it) }) return null val signature = runCatching { hexToBytes(manifest.signatureHex) }.getOrNull() ?: return null val canonical = canonicalDeviceManifestBody(body).toByteArray(Charsets.UTF_8) if (!verifyEd25519(issuerSigningPublicKey, canonical, signature)) return null return body } fun canonicalDeviceManifestBody(body: DeviceManifestBody): String = canonicalise( mapOf( "devices" to body.devices.map { manifestEntryMap(it) }, "issuedAt" to body.issuedAt, "issuerDeviceId" to body.issuerDeviceId, "manifestVersion" to body.manifestVersion, "revoked" to body.revoked.map { revocationMap(it) }, ), ) fun PairedDevice.toManifestEntry(): ManifestDeviceEntry? { val id = deviceId.takeIf { it.isNotBlank() } ?: return null val name = deviceName?.takeIf { it.isNotBlank() } ?: return null val type = deviceType?.takeIf { it in setOf("extension", "android", "ios") } ?: return null val longTerm = longTermPublicKeyHex?.lowercase()?.takeIf { isHex(it, 64) } ?: return null val signing = signingPublicKeyHex?.lowercase()?.takeIf { isHex(it, 64) } ?: return null return ManifestDeviceEntry( deviceId = id, deviceName = name, deviceType = type, longTermPublicKeyHex = longTerm, signingPublicKeyHex = signing, wsPort = wsPort, relayUrl = relayUrl, pairedAt = pairedAt, ) } private fun normaliseManifestEntry(entry: ManifestDeviceEntry): ManifestDeviceEntry? { if (entry.deviceId.isBlank() || entry.deviceName.isBlank()) return null // Bound string fields — a peer could otherwise send multi-megabyte strings // that pass the format checks and bloat the canonical signature input. if (entry.deviceId.length > MAX_MANIFEST_STRING || entry.deviceName.length > MAX_MANIFEST_STRING) return null if (entry.deviceType !in setOf("extension", "android", "ios")) return null if (!isHex(entry.longTermPublicKeyHex, 64) || !isHex(entry.signingPublicKeyHex, 64)) return null if (entry.wsPort != null && entry.wsPort !in 1..65_535) return null if (entry.pairedAt < 0L) return null if ( entry.relayUrl != null && ( entry.relayUrl.length > MAX_MANIFEST_STRING || runCatching { Rendezvous.normalizeRelayUrl(entry.relayUrl) }.isFailure ) ) return null return entry.copy( longTermPublicKeyHex = entry.longTermPublicKeyHex.lowercase(), signingPublicKeyHex = entry.signingPublicKeyHex.lowercase(), ) } private fun normaliseRevocation(r: DeviceRevocation): DeviceRevocation = r.copy(longTermPublicKeyHex = r.longTermPublicKeyHex.lowercase()) private fun isValidRevocation(r: DeviceRevocation): Boolean = r.deviceId.isNotBlank() && r.deviceId.length <= MAX_MANIFEST_STRING && isHex(r.longTermPublicKeyHex.lowercase(), 64) && r.revokedByDeviceId.isNotBlank() && r.revokedByDeviceId.length <= MAX_MANIFEST_STRING && r.revokedAt >= 0L private fun revocationMap(r: DeviceRevocation): Map = linkedMapOf( "deviceId" to r.deviceId, "longTermPublicKeyHex" to r.longTermPublicKeyHex.lowercase(), "revokedAt" to r.revokedAt, "revokedByDeviceId" to r.revokedByDeviceId, ) private fun manifestEntryMap(entry: ManifestDeviceEntry): Map { val normalised = normaliseManifestEntry(entry) ?: entry val map = linkedMapOf( "deviceId" to normalised.deviceId, "deviceName" to normalised.deviceName, "deviceType" to normalised.deviceType, "longTermPublicKeyHex" to normalised.longTermPublicKeyHex.lowercase(), "pairedAt" to normalised.pairedAt, "signingPublicKeyHex" to normalised.signingPublicKeyHex.lowercase(), ) normalised.wsPort?.let { map["wsPort"] = it } normalised.relayUrl?.let { map["relayUrl"] = it } return map } private fun canonicalise(value: Any?): String = when (value) { null -> "null" is Number -> { val d = value.toDouble() require(d.isFinite()) { "non-finite number" } if (d % 1.0 == 0.0) value.toLong().toString() else value.toString() } is String -> Json.encodeToString(String.serializer(), value) is Boolean -> if (value) "true" else "false" is List<*> -> value.joinToString(prefix = "[", postfix = "]", separator = ",") { canonicalise(it) } is Map<*, *> -> value.entries .map { (k, v) -> k.toString() to v } .sortedBy { it.first } .joinToString(prefix = "{", postfix = "}", separator = ",") { (k, v) -> Json.encodeToString(String.serializer(), k) + ":" + canonicalise(v) } else -> error("cannot canonicalise ${value::class.simpleName}") } private fun isHex(value: String, length: Int): Boolean = value.length == length && value.all { it in '0'..'9' || it in 'a'..'f' }