findfamily: embed outbound pair ID on send, expose claimed ID on receive
- Commit
0a23ecd552b05051c2da8bcd6f8ecbd2237b1122- Author
- vayun-mathur <[email protected]>
- When
- 2026-10-13 21:44 UTC (1 month ago)
- Parents
84740e5
Diff
findfamily/src/main/java/com/vayunmathur/findfamily/util/Networking.kt
diff --git a/findfamily/src/main/java/com/vayunmathur/findfamily/util/Networking.kt b/findfamily/src/main/java/com/vayunmathur/findfamily/util/Networking.ktindex 9cb6cc96a0..6290a4bb84 100644--- a/findfamily/src/main/java/com/vayunmathur/findfamily/util/Networking.kt+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/util/Networking.kt@@ -322,7 +322,7 @@ object Networking { @Synchronized fun startLive( scope: CoroutineScope,- onLocations: suspend (List<LocationValue>) -> Unit,+ onLocations: suspend (List<InboundFix>) -> Unit, onUwb: suspend (List<UwbEnvelope>) -> Unit, ) { if (liveJob?.isActive == true) return@@ -428,7 +428,8 @@ object Networking { return false } return try {- val ok = sendLivePublish(user.id, "location", sealLocation(location, bundle))+ val pairId = outboundPairIdFor(user)+ val ok = sendLivePublish(user.id, "location", sealLocation(location, bundle, pairId)) Log.dev(TAG, "publishLocation PQC to ${user.id.toULong()} (${user.name}) ok=$ok") ok } catch (e: Exception) {@@ -443,7 +444,9 @@ object Networking { suspend fun publishLocation(location: LocationValue, link: TemporaryLink): Boolean { return try { val bundle = Base64.decode(link.pqcPublicKey)- val ok = sendLivePublish(link.id, "location", sealLocation(location, bundle))+ // Anonymous link viewers prove nothing about identity and expect no auth:+ // pair ID stays null so nothing link-specific is pinned or leaked.+ val ok = sendLivePublish(link.id, "location", sealLocation(location, bundle, pairId = null)) Log.dev(TAG, "publishLocation PQC to temp link ${link.id} ok=$ok") ok } catch (e: Exception) {@@ -456,12 +459,15 @@ object Networking { * Publish to a peer identified only by id and cached public bundle. This is the * pre-unlock path: it takes the same wire format as the [User] overload but reads * nothing from Room, which is unreadable before the passcode is entered.+ *+ * [pairId] is the stored outbound secret for this peer, mirrored into+ * device-protected storage at seed time — the pre-unlock path cannot read Room. */ // Broad catch is deliberate: relay-socket boundary, same as [wsGetKey]. @Suppress("TooGenericExceptionCaught")- suspend fun publishLocation(location: LocationValue, targetId: Long, bundleB64: String): Boolean {+ suspend fun publishLocation(location: LocationValue, targetId: Long, bundleB64: String, pairId: String? = null): Boolean { return try {- val ok = sendLivePublish(targetId, "location", sealLocation(location, Base64.decode(bundleB64)))+ val ok = sendLivePublish(targetId, "location", sealLocation(location, Base64.decode(bundleB64), pairId)) Log.dev(TAG, "publishLocation PQC to ${targetId.toULong()} (direct boot) ok=$ok") ok } catch (e: Exception) {@@ -508,12 +514,29 @@ object Networking { // Encryption helpers (post-quantum only) // ---------------------------------------------------------------- - private fun sealLocation(location: LocationValue, bundle: ByteArray): ByteArray {- val str = json.encodeToString(location.toCompatible(senderPlatform = PLATFORM))+ private fun sealLocation(location: LocationValue, bundle: ByteArray, pairId: String?): ByteArray {+ val str = json.encodeToString(location.toCompatible(senderPlatform = PLATFORM, pairId = pairId)) // PQC hybrid: ML-KEM encapsulate → AES-256-GCM, layout [4B encapLen][encap][aes]. return Pqc.encryptTo(bundle, str.encodeToByteArray()) } + /**+ * Our outbound pair-ID secret for [user] (GHSA-x94x-v6f7-wwpp): the hidden value every+ * packet to this peer carries inside the ciphertext.+ *+ * Created once and never rotated: the receiver pins whatever it first sees, so rotating+ * would look exactly like a forgery and get dropped. Uses the conditional+ * `setOutboundPairIdIfAbsent` write so two concurrent heartbeats cannot mint different+ * values — the loser re-reads the winner.+ */+ private suspend fun outboundPairIdFor(user: User): String {+ user.outboundPairId?.let { return it }+ val fresh = com.vayunmathur.findfamily.domain.PairAuth.generatePairId()+ runCatching { repository.setOutboundPairIdIfAbsent(user.id, fresh) }+ return runCatching { repository.getUser(user.id)?.outboundPairId }+ .getOrNull() ?: fresh+ }+ /** * Generates a fresh share-link key. The link is ML-KEM only and derived from a * 32-byte seed, which is the whole secret handed to the recipient in the URL
findfamily/src/main/java/com/vayunmathur/findfamily/util/NetworkingLive.kt
diff --git a/findfamily/src/main/java/com/vayunmathur/findfamily/util/NetworkingLive.kt b/findfamily/src/main/java/com/vayunmathur/findfamily/util/NetworkingLive.ktindex 0dfcc7a8c2..f1c7f13539 100644--- a/findfamily/src/main/java/com/vayunmathur/findfamily/util/NetworkingLive.kt+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/util/NetworkingLive.kt@@ -45,7 +45,7 @@ internal suspend fun WsSession.sendSubscribe() { internal suspend fun WsSession.superviseConnection( connScope: CoroutineScope, outerScope: CoroutineScope,- onLocations: suspend (List<LocationValue>) -> Unit,+ onLocations: suspend (List<InboundFix>) -> Unit, onUwb: suspend (List<UwbEnvelope>) -> Unit, ) { val lastInboundMs = AtomicLong(System.currentTimeMillis())@@ -92,7 +92,7 @@ internal suspend fun WsSession.superviseConnection( /** Parse one server frame and dispatch: MSG → decrypt+deliver; KEYRESP → complete the lookup. */ internal suspend fun dispatchLiveFrame( buf: ByteArray,- onLocations: suspend (List<LocationValue>) -> Unit,+ onLocations: suspend (List<InboundFix>) -> Unit, onUwb: suspend (List<UwbEnvelope>) -> Unit, ) { val op = buf.firstOrNull() ?: return@@ -108,7 +108,7 @@ internal suspend fun dispatchLiveFrame( /** MSG frame: [op][flags][ciphertext] — location or UWB envelope after PQC decrypt. */ private suspend fun handleLiveMsg( buf: ByteArray,- onLocations: suspend (List<LocationValue>) -> Unit,+ onLocations: suspend (List<InboundFix>) -> Unit, onUwb: suspend (List<UwbEnvelope>) -> Unit, ) { if (buf.size < Networking.MSG_FRAME_HEADER_LEN) return@@ -117,9 +117,9 @@ private suspend fun handleLiveMsg( if (!isUwb) { val decoded = runCatching { decryptLocationPqcBytes(raw) } .onFailure { Log.status(Networking.TAG, "live location decrypt fail", it) }.getOrNull() ?: return- val (loc, platform) = decoded+ val (loc, platform, pairId) = decoded if (platform != null) runCatching { Networking.repository.setPlatform(loc.userid, platform) }- runCatching { onLocations(listOf(loc)) }+ runCatching { onLocations(listOf(loc.withPairId(pairId))) } } else { val env = runCatching { val plain = Networking.pqcIdentity.decrypt(raw)@@ -173,10 +173,27 @@ private fun handleReportGetResp(buf: ByteArray) { Networking.pendingReportGets.poll()?.complete(out) } -private fun decryptLocationPqcBytes(raw: ByteArray): Pair<LocationValue, String?> {+/**+ * A decrypted inbound fix plus its envelope metadata (never persisted as-is).+ *+ * [pairId] is the sender's claimed outbound secret (GHSA-x94x-v6f7-wwpp). It rides+ * beside the fix rather than inside [LocationValue] because Room must never store an+ * unauthenticated claim — the inbound pipeline checks it against the pinned+ * `User.inboundPairId` first and drops the fix on mismatch (see+ * `processIncomingLocations`).+ *+ * Public (not internal): [Networking.startLive] is public API, so its parameter type+ * must be visible at the call site in [LocationTrackingService].+ */+data class InboundFix(val location: LocationValue, val pairId: String?)++/** Attach the claimed pair ID to a freshly-decrypted fix for the inbound pipeline. */+internal fun LocationValue.withPairId(pairId: String?): InboundFix = InboundFix(this, pairId)++private fun decryptLocationPqcBytes(raw: ByteArray): Triple<LocationValue, String?, String?> { val plainBytes = Networking.pqcIdentity.decrypt(raw) val compat = Networking.json.decodeFromString<LocationValueCompatible>(plainBytes.decodeToString())- return compat.toLocationValue() to compat.senderPlatform+ return Triple(compat.toLocationValue(), compat.senderPlatform, compat.pairId) } /** Reads a big-endian u16 at [off]. */