mirror of
https://github.com/kalarina-bit/Matewave.git
synced 2026-11-22 08:06:09 +00:00
Sign presence announcements with their signing time
Wi-Fi presence announcements now carry the time they were signed, and a known friend's announcement is accepted only if it is at most 15 minutes old (or ahead). A recorded announcement can no longer be replayed to show someone as nearby. Each device signs its announcement anew every 5 minutes. Older versions sign without the time: their announcements are hidden for known friends, and they in turn hide announcements from this version.
This commit is contained in:
@@ -29,6 +29,8 @@ data class LanPeer(
|
|||||||
val fingerprint: String = "",
|
val fingerprint: String = "",
|
||||||
|
|
||||||
val signature: String = "",
|
val signature: String = "",
|
||||||
|
/** When the announcement was signed, in epoch seconds; 0 if the peer sent none. */
|
||||||
|
val signedAtSeconds: Long = 0,
|
||||||
/** Raw NSD service name, used to remove the peer when the service is lost. */
|
/** Raw NSD service name, used to remove the peer when the service is lost. */
|
||||||
val serviceName: String = "",
|
val serviceName: String = "",
|
||||||
/** Tag of the LAN game the peer plays, empty when not in one. */
|
/** Tag of the LAN game the peer plays, empty when not in one. */
|
||||||
@@ -59,8 +61,22 @@ class LanTransport(
|
|||||||
|
|
||||||
const val RESOLVE_RETRY_MS = 400L
|
const val RESOLVE_RETRY_MS = 400L
|
||||||
|
|
||||||
fun presencePayload(playerId: String, name: String, elo: Int): String =
|
/**
|
||||||
"presence|$playerId|$name|$elo"
|
* Signed presence announcement. The signing time is part of it, so a recorded
|
||||||
|
* announcement stops being accepted after [PRESENCE_MAX_AGE_MS]; older versions, which
|
||||||
|
* signed without it, are no longer accepted at all.
|
||||||
|
*/
|
||||||
|
fun presencePayload(playerId: String, name: String, elo: Int, signedAtSeconds: Long): String =
|
||||||
|
"presence2|$playerId|$name|$elo|$signedAtSeconds"
|
||||||
|
|
||||||
|
/** How old (or how far ahead, for a clock set wrong) a signed announcement may be. */
|
||||||
|
const val PRESENCE_MAX_AGE_MS = 15 * 60_000L
|
||||||
|
|
||||||
|
/** The announcement is signed anew this often, well within [PRESENCE_MAX_AGE_MS]. */
|
||||||
|
const val PRESENCE_RESIGN_MS = 5 * 60_000L
|
||||||
|
|
||||||
|
fun isFreshPresence(signedAtSeconds: Long, nowMs: Long): Boolean =
|
||||||
|
signedAtSeconds > 0 && kotlin.math.abs(nowMs - signedAtSeconds * 1000L) <= PRESENCE_MAX_AGE_MS
|
||||||
}
|
}
|
||||||
|
|
||||||
override val type = TransportType.LAN
|
override val type = TransportType.LAN
|
||||||
@@ -92,6 +108,9 @@ class LanTransport(
|
|||||||
|
|
||||||
private var registeredServiceName: String? = null
|
private var registeredServiceName: String? = null
|
||||||
|
|
||||||
|
// Re-signs the announcement before it gets too old to be accepted.
|
||||||
|
private var resignJob: kotlinx.coroutines.Job? = null
|
||||||
|
|
||||||
private val ownServiceName: String by lazy {
|
private val ownServiceName: String by lazy {
|
||||||
val fingerprint = cc.skysparkle.matewave.security.DeviceKeys.publicKeyFingerprint(context)
|
val fingerprint = cc.skysparkle.matewave.security.DeviceKeys.publicKeyFingerprint(context)
|
||||||
?: cc.skysparkle.matewave.data.DeviceIdentity.deviceId(context).take(12)
|
?: cc.skysparkle.matewave.data.DeviceIdentity.deviceId(context).take(12)
|
||||||
@@ -212,13 +231,15 @@ class LanTransport(
|
|||||||
val profile = cc.skysparkle.matewave.data.ProfileStore.get()
|
val profile = cc.skysparkle.matewave.data.ProfileStore.get()
|
||||||
runCatching {
|
runCatching {
|
||||||
val shownName = profile.name.take(40)
|
val shownName = profile.name.take(40)
|
||||||
|
val signedAt = System.currentTimeMillis() / 1000L
|
||||||
setAttribute("id", profile.userId)
|
setAttribute("id", profile.userId)
|
||||||
setAttribute("name", shownName)
|
setAttribute("name", shownName)
|
||||||
setAttribute("elo", profile.elo.toString())
|
setAttribute("elo", profile.elo.toString())
|
||||||
setAttribute("fp", cc.skysparkle.matewave.security.DeviceKeys.publicKeyFingerprint(context) ?: "")
|
setAttribute("fp", cc.skysparkle.matewave.security.DeviceKeys.publicKeyFingerprint(context) ?: "")
|
||||||
|
setAttribute("ts", signedAt.toString())
|
||||||
|
|
||||||
cc.skysparkle.matewave.security.DeviceKeys.sign(
|
cc.skysparkle.matewave.security.DeviceKeys.sign(
|
||||||
context, presencePayload(profile.userId, shownName, profile.elo)
|
context, presencePayload(profile.userId, shownName, profile.elo, signedAt)
|
||||||
)?.let { setAttribute("sig", it) }
|
)?.let { setAttribute("sig", it) }
|
||||||
cc.skysparkle.matewave.spectate.SpectatorAdvert.attributes().forEach { (key, value) -> setAttribute(key, value) }
|
cc.skysparkle.matewave.spectate.SpectatorAdvert.attributes().forEach { (key, value) -> setAttribute(key, value) }
|
||||||
}
|
}
|
||||||
@@ -241,9 +262,15 @@ class LanTransport(
|
|||||||
nsd.registerService(info, NsdManager.PROTOCOL_DNS_SD, listener)
|
nsd.registerService(info, NsdManager.PROTOCOL_DNS_SD, listener)
|
||||||
} catch (_: Exception) {
|
} catch (_: Exception) {
|
||||||
}
|
}
|
||||||
|
resignJob = scope.launch {
|
||||||
|
kotlinx.coroutines.delay(PRESENCE_RESIGN_MS)
|
||||||
|
republishProfile()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun unregisterService() {
|
private fun unregisterService() {
|
||||||
|
resignJob?.cancel()
|
||||||
|
resignJob = null
|
||||||
val nsd = nsdManager ?: return
|
val nsd = nsdManager ?: return
|
||||||
registrationListener?.let {
|
registrationListener?.let {
|
||||||
try { nsd.unregisterService(it) } catch (_: Exception) {}
|
try { nsd.unregisterService(it) } catch (_: Exception) {}
|
||||||
@@ -363,6 +390,7 @@ class LanTransport(
|
|||||||
|
|
||||||
fingerprint = attr("fp").ifEmpty { fingerprintFromServiceName(info.serviceName ?: "") ?: "" },
|
fingerprint = attr("fp").ifEmpty { fingerprintFromServiceName(info.serviceName ?: "") ?: "" },
|
||||||
signature = attr("sig"),
|
signature = attr("sig"),
|
||||||
|
signedAtSeconds = attr("ts").toLongOrNull() ?: 0,
|
||||||
serviceName = info.serviceName ?: "",
|
serviceName = info.serviceName ?: "",
|
||||||
gameTag = game.gameTag,
|
gameTag = game.gameTag,
|
||||||
spectatePort = game.spectatePort
|
spectatePort = game.spectatePort
|
||||||
|
|||||||
@@ -141,13 +141,19 @@ class LocalPresence(
|
|||||||
|
|
||||||
val knownKey = knownPeers.publicKeyOf(id)
|
val knownKey = knownPeers.publicKeyOf(id)
|
||||||
if (knownKey != null) {
|
if (knownKey != null) {
|
||||||
val authentic = peer.signature.isNotEmpty() && DeviceKeys.verify(
|
// A known friend must send a fresh announcement signed with their key: an old one
|
||||||
knownKey,
|
// recorded and replayed by someone else is not accepted.
|
||||||
cc.skysparkle.matewave.network.LanTransport.presencePayload(peer.playerId, peer.displayName, peer.elo),
|
val authentic = peer.signature.isNotEmpty() &&
|
||||||
peer.signature
|
cc.skysparkle.matewave.network.LanTransport.isFreshPresence(peer.signedAtSeconds, lastResultAt) &&
|
||||||
)
|
DeviceKeys.verify(
|
||||||
|
knownKey,
|
||||||
|
cc.skysparkle.matewave.network.LanTransport.presencePayload(
|
||||||
|
peer.playerId, peer.displayName, peer.elo, peer.signedAtSeconds
|
||||||
|
),
|
||||||
|
peer.signature
|
||||||
|
)
|
||||||
if (!authentic) {
|
if (!authentic) {
|
||||||
NetworkLog.log("PRESENCE", "Announcement for $id failed signature check, hidden")
|
NetworkLog.log("PRESENCE", "Announcement for $id is stale or failed signature check, hidden")
|
||||||
return@forEach
|
return@forEach
|
||||||
}
|
}
|
||||||
FriendsStore.updateProfile(
|
FriendsStore.updateProfile(
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package cc.skysparkle.matewave
|
||||||
|
|
||||||
|
import cc.skysparkle.matewave.network.LanTransport
|
||||||
|
import org.junit.Assert.assertFalse
|
||||||
|
import org.junit.Assert.assertNotEquals
|
||||||
|
import org.junit.Assert.assertTrue
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class PresenceSignatureTest {
|
||||||
|
private val now = 1_800_000_000_000L
|
||||||
|
private val nowSeconds = now / 1000
|
||||||
|
|
||||||
|
@Test fun recentAnnouncementIsFresh() =
|
||||||
|
assertTrue(LanTransport.isFreshPresence(nowSeconds - 4 * 60, now))
|
||||||
|
|
||||||
|
@Test fun slightlyFastClockIsTolerated() =
|
||||||
|
assertTrue(LanTransport.isFreshPresence(nowSeconds + 5 * 60, now))
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun oldMissingOrFarFutureAnnouncementsAreRejected() {
|
||||||
|
assertFalse(LanTransport.isFreshPresence(nowSeconds - 16 * 60, now))
|
||||||
|
assertFalse(LanTransport.isFreshPresence(nowSeconds + 16 * 60, now))
|
||||||
|
assertFalse(LanTransport.isFreshPresence(0, now))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun signingTimeIsPartOfTheSignedText() = assertNotEquals(
|
||||||
|
LanTransport.presencePayload("u1", "Vladimir", 1350, nowSeconds),
|
||||||
|
LanTransport.presencePayload("u1", "Vladimir", 1350, nowSeconds - 60)
|
||||||
|
)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user