mirror of
https://github.com/kalarina-bit/Matewave.git
synced 2026-11-22 16:16:10 +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 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. */
|
||||
val serviceName: String = "",
|
||||
/** 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
|
||||
|
||||
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
|
||||
@@ -92,6 +108,9 @@ class LanTransport(
|
||||
|
||||
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 {
|
||||
val fingerprint = cc.skysparkle.matewave.security.DeviceKeys.publicKeyFingerprint(context)
|
||||
?: cc.skysparkle.matewave.data.DeviceIdentity.deviceId(context).take(12)
|
||||
@@ -212,13 +231,15 @@ class LanTransport(
|
||||
val profile = cc.skysparkle.matewave.data.ProfileStore.get()
|
||||
runCatching {
|
||||
val shownName = profile.name.take(40)
|
||||
val signedAt = System.currentTimeMillis() / 1000L
|
||||
setAttribute("id", profile.userId)
|
||||
setAttribute("name", shownName)
|
||||
setAttribute("elo", profile.elo.toString())
|
||||
setAttribute("fp", cc.skysparkle.matewave.security.DeviceKeys.publicKeyFingerprint(context) ?: "")
|
||||
setAttribute("ts", signedAt.toString())
|
||||
|
||||
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) }
|
||||
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)
|
||||
} catch (_: Exception) {
|
||||
}
|
||||
resignJob = scope.launch {
|
||||
kotlinx.coroutines.delay(PRESENCE_RESIGN_MS)
|
||||
republishProfile()
|
||||
}
|
||||
}
|
||||
|
||||
private fun unregisterService() {
|
||||
resignJob?.cancel()
|
||||
resignJob = null
|
||||
val nsd = nsdManager ?: return
|
||||
registrationListener?.let {
|
||||
try { nsd.unregisterService(it) } catch (_: Exception) {}
|
||||
@@ -363,6 +390,7 @@ class LanTransport(
|
||||
|
||||
fingerprint = attr("fp").ifEmpty { fingerprintFromServiceName(info.serviceName ?: "") ?: "" },
|
||||
signature = attr("sig"),
|
||||
signedAtSeconds = attr("ts").toLongOrNull() ?: 0,
|
||||
serviceName = info.serviceName ?: "",
|
||||
gameTag = game.gameTag,
|
||||
spectatePort = game.spectatePort
|
||||
|
||||
@@ -141,13 +141,19 @@ class LocalPresence(
|
||||
|
||||
val knownKey = knownPeers.publicKeyOf(id)
|
||||
if (knownKey != null) {
|
||||
val authentic = peer.signature.isNotEmpty() && DeviceKeys.verify(
|
||||
knownKey,
|
||||
cc.skysparkle.matewave.network.LanTransport.presencePayload(peer.playerId, peer.displayName, peer.elo),
|
||||
peer.signature
|
||||
)
|
||||
// A known friend must send a fresh announcement signed with their key: an old one
|
||||
// recorded and replayed by someone else is not accepted.
|
||||
val authentic = peer.signature.isNotEmpty() &&
|
||||
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) {
|
||||
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
|
||||
}
|
||||
FriendsStore.updateProfile(
|
||||
|
||||
Reference in New Issue
Block a user