mirror of
https://github.com/permissionlesstech/bitchat-android.git
synced 2026-07-25 02:45:20 +00:00
Sign announcements (#267)
* wip * announcements WIP * works * restore mainnet
This commit is contained in:
@@ -181,6 +181,14 @@ class BluetoothMeshService(private val context: Context) {
|
||||
return delegate?.getNickname()
|
||||
}
|
||||
|
||||
override fun getPeerInfo(peerID: String): PeerInfo? {
|
||||
return peerManager.getPeerInfo(peerID)
|
||||
}
|
||||
|
||||
override fun updatePeerInfo(peerID: String, nickname: String, noisePublicKey: ByteArray, signingPublicKey: ByteArray, isVerified: Boolean): Boolean {
|
||||
return peerManager.updatePeerInfo(peerID, nickname, noisePublicKey, signingPublicKey, isVerified)
|
||||
}
|
||||
|
||||
// Packet operations
|
||||
override fun sendPacket(packet: BitchatPacket) {
|
||||
connectionManager.broadcastPacket(RoutedPacket(packet))
|
||||
@@ -575,8 +583,15 @@ class BluetoothMeshService(private val context: Context) {
|
||||
return@launch
|
||||
}
|
||||
|
||||
// Get the signing public key for the announcement
|
||||
val signingKey = encryptionService.getSigningPublicKey()
|
||||
if (signingKey == null) {
|
||||
Log.e(TAG, "No signing public key available for announcement")
|
||||
return@launch
|
||||
}
|
||||
|
||||
// Create iOS-compatible IdentityAnnouncement with TLV encoding
|
||||
val announcement = IdentityAnnouncement(nickname, staticKey)
|
||||
val announcement = IdentityAnnouncement(nickname, staticKey, signingKey)
|
||||
val tlvPayload = announcement.encode()
|
||||
if (tlvPayload == null) {
|
||||
Log.e(TAG, "Failed to encode announcement as TLV")
|
||||
@@ -590,8 +605,13 @@ class BluetoothMeshService(private val context: Context) {
|
||||
payload = tlvPayload
|
||||
)
|
||||
|
||||
connectionManager.broadcastPacket(RoutedPacket(announcePacket))
|
||||
Log.d(TAG, "Sent iOS-compatible TLV announce (${tlvPayload.size} bytes)")
|
||||
// Sign the packet using our signing key (exactly like iOS)
|
||||
val signedPacket = encryptionService.signData(announcePacket.toBinaryDataForSigning()!!)?.let { signature ->
|
||||
announcePacket.copy(signature = signature)
|
||||
} ?: announcePacket
|
||||
|
||||
connectionManager.broadcastPacket(RoutedPacket(signedPacket))
|
||||
Log.d(TAG, "Sent iOS-compatible signed TLV announce (${tlvPayload.size} bytes)")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -610,8 +630,15 @@ class BluetoothMeshService(private val context: Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Get the signing public key for the announcement
|
||||
val signingKey = encryptionService.getSigningPublicKey()
|
||||
if (signingKey == null) {
|
||||
Log.e(TAG, "No signing public key available for peer announcement")
|
||||
return
|
||||
}
|
||||
|
||||
// Create iOS-compatible IdentityAnnouncement with TLV encoding
|
||||
val announcement = IdentityAnnouncement(nickname, staticKey)
|
||||
val announcement = IdentityAnnouncement(nickname, staticKey, signingKey)
|
||||
val tlvPayload = announcement.encode()
|
||||
if (tlvPayload == null) {
|
||||
Log.e(TAG, "Failed to encode peer announcement as TLV")
|
||||
@@ -625,9 +652,14 @@ class BluetoothMeshService(private val context: Context) {
|
||||
payload = tlvPayload
|
||||
)
|
||||
|
||||
connectionManager.broadcastPacket(RoutedPacket(packet))
|
||||
// Sign the packet using our signing key (exactly like iOS)
|
||||
val signedPacket = encryptionService.signData(packet.toBinaryDataForSigning()!!)?.let { signature ->
|
||||
packet.copy(signature = signature)
|
||||
} ?: packet
|
||||
|
||||
connectionManager.broadcastPacket(RoutedPacket(signedPacket))
|
||||
peerManager.markPeerAsAnnouncedTo(peerID)
|
||||
Log.d(TAG, "Sent iOS-compatible TLV peer announce to $peerID (${tlvPayload.size} bytes)")
|
||||
Log.d(TAG, "Sent iOS-compatible signed TLV peer announce to $peerID (${tlvPayload.size} bytes)")
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -683,6 +715,26 @@ class BluetoothMeshService(private val context: Context) {
|
||||
fun getPeerFingerprint(peerID: String): String? {
|
||||
return peerManager.getFingerprintForPeer(peerID)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get peer info for verification purposes
|
||||
*/
|
||||
fun getPeerInfo(peerID: String): PeerInfo? {
|
||||
return peerManager.getPeerInfo(peerID)
|
||||
}
|
||||
|
||||
/**
|
||||
* Update peer information with verification data
|
||||
*/
|
||||
fun updatePeerInfo(
|
||||
peerID: String,
|
||||
nickname: String,
|
||||
noisePublicKey: ByteArray,
|
||||
signingPublicKey: ByteArray,
|
||||
isVerified: Boolean
|
||||
): Boolean {
|
||||
return peerManager.updatePeerInfo(peerID, nickname, noisePublicKey, signingPublicKey, isVerified)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get our identity fingerprint
|
||||
|
||||
@@ -164,7 +164,7 @@ class MessageHandler(private val myPeerID: String) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle announce message with TLV decoding support - exactly like iOS
|
||||
* Handle announce message with TLV decoding and signature verification - exactly like iOS
|
||||
*/
|
||||
suspend fun handleAnnounce(routed: RoutedPacket): Boolean {
|
||||
val packet = routed.packet
|
||||
@@ -179,26 +179,59 @@ class MessageHandler(private val myPeerID: String) {
|
||||
return false
|
||||
}
|
||||
|
||||
// Successfully decoded TLV format exactly like iOS
|
||||
Log.d(TAG, "Received iOS-compatible announce from $peerID: nickname=${announcement.nickname}, " +
|
||||
"publicKey=${announcement.publicKey.joinToString("") { "%02x".format(it) }.take(16)}...")
|
||||
|
||||
// Extract nickname and public key from TLV data
|
||||
val nickname = announcement.nickname
|
||||
val publicKey = announcement.publicKey
|
||||
|
||||
// Notify delegate to handle peer management with nickname
|
||||
val isFirstAnnounce = delegate?.addOrUpdatePeer(peerID, nickname) ?: false
|
||||
// Verify packet signature using the announced signing public key
|
||||
var verified = false
|
||||
if (packet.signature != null) {
|
||||
// Verify that the packet was signed by the signing private key corresponding to the announced signing public key
|
||||
verified = delegate?.verifyEd25519Signature(packet.signature!!, packet.toBinaryDataForSigning()!!, announcement.signingPublicKey) ?: false
|
||||
if (!verified) {
|
||||
Log.w(TAG, "⚠️ Signature verification for announce failed ${peerID.take(8)}")
|
||||
}
|
||||
}
|
||||
|
||||
// Update peer ID binding with public key for identity management
|
||||
// Check for existing peer with different noise public key
|
||||
// If existing peer has a different noise public key, do not consider this verified
|
||||
val existingPeer = delegate?.getPeerInfo(peerID)
|
||||
|
||||
if (existingPeer != null && existingPeer.noisePublicKey != null && !existingPeer.noisePublicKey!!.contentEquals(announcement.noisePublicKey)) {
|
||||
Log.w(TAG, "⚠️ Announce key mismatch for ${peerID.take(8)}... — keeping unverified")
|
||||
verified = false
|
||||
}
|
||||
|
||||
// Require verified announce; ignore otherwise (no backward compatibility)
|
||||
if (!verified) {
|
||||
Log.w(TAG, "❌ Ignoring unverified announce from ${peerID.take(8)}...")
|
||||
return false
|
||||
}
|
||||
|
||||
// Successfully decoded TLV format exactly like iOS
|
||||
Log.d(TAG, "✅ Verified announce from $peerID: nickname=${announcement.nickname}, " +
|
||||
"noisePublicKey=${announcement.noisePublicKey.joinToString("") { "%02x".format(it) }.take(16)}..., " +
|
||||
"signingPublicKey=${announcement.signingPublicKey.joinToString("") { "%02x".format(it) }.take(16)}...")
|
||||
|
||||
// Extract nickname and public keys from TLV data
|
||||
val nickname = announcement.nickname
|
||||
val noisePublicKey = announcement.noisePublicKey
|
||||
val signingPublicKey = announcement.signingPublicKey
|
||||
|
||||
// Update peer info with verification status through new method
|
||||
val isFirstAnnounce = delegate?.updatePeerInfo(
|
||||
peerID = peerID,
|
||||
nickname = nickname,
|
||||
noisePublicKey = noisePublicKey,
|
||||
signingPublicKey = signingPublicKey,
|
||||
isVerified = true
|
||||
) ?: false
|
||||
|
||||
// Update peer ID binding with noise public key for identity management
|
||||
delegate?.updatePeerIDBinding(
|
||||
newPeerID = peerID,
|
||||
nickname = nickname,
|
||||
publicKey = publicKey,
|
||||
publicKey = noisePublicKey,
|
||||
previousPeerID = null
|
||||
)
|
||||
|
||||
Log.d(TAG, "Processed iOS-compatible TLV announce: stored public key for $peerID")
|
||||
Log.d(TAG, "✅ Processed verified TLV announce: stored identity for $peerID")
|
||||
return isFirstAnnounce
|
||||
}
|
||||
|
||||
@@ -282,11 +315,19 @@ class MessageHandler(private val myPeerID: String) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle broadcast message
|
||||
* Handle broadcast message with verification enforcement
|
||||
*/
|
||||
private suspend fun handleBroadcastMessage(routed: RoutedPacket) {
|
||||
val packet = routed.packet
|
||||
val peerID = routed.peerID ?: "unknown"
|
||||
|
||||
// Enforce: only accept public messages from verified peers we know
|
||||
val peerInfo = delegate?.getPeerInfo(peerID)
|
||||
if (peerInfo == null || !peerInfo.isVerifiedNickname) {
|
||||
Log.w(TAG, "🚫 Dropping public message from unverified or unknown peer ${peerID.take(8)}...")
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
// Parse message
|
||||
val message = BitchatMessage(
|
||||
@@ -398,6 +439,8 @@ interface MessageHandlerDelegate {
|
||||
fun getPeerNickname(peerID: String): String?
|
||||
fun getNetworkSize(): Int
|
||||
fun getMyNickname(): String?
|
||||
fun getPeerInfo(peerID: String): PeerInfo?
|
||||
fun updatePeerInfo(peerID: String, nickname: String, noisePublicKey: ByteArray, signingPublicKey: ByteArray, isVerified: Boolean): Boolean
|
||||
|
||||
// Packet operations
|
||||
fun sendPacket(packet: BitchatPacket)
|
||||
|
||||
@@ -5,11 +5,60 @@ import kotlinx.coroutines.*
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.concurrent.CopyOnWriteArrayList
|
||||
|
||||
/**
|
||||
* Peer information structure with verification status
|
||||
* Compatible with iOS PeerInfo structure
|
||||
*/
|
||||
data class PeerInfo(
|
||||
val id: String,
|
||||
var nickname: String,
|
||||
var isConnected: Boolean,
|
||||
var noisePublicKey: ByteArray?,
|
||||
var signingPublicKey: ByteArray?, // NEW: Ed25519 public key for verification
|
||||
var isVerifiedNickname: Boolean, // NEW: Verification status flag
|
||||
var lastSeen: Long // Using Long instead of Date for simplicity
|
||||
) {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (javaClass != other?.javaClass) return false
|
||||
|
||||
other as PeerInfo
|
||||
|
||||
if (id != other.id) return false
|
||||
if (nickname != other.nickname) return false
|
||||
if (isConnected != other.isConnected) return false
|
||||
if (noisePublicKey != null) {
|
||||
if (other.noisePublicKey == null) return false
|
||||
if (!noisePublicKey.contentEquals(other.noisePublicKey)) return false
|
||||
} else if (other.noisePublicKey != null) return false
|
||||
if (signingPublicKey != null) {
|
||||
if (other.signingPublicKey == null) return false
|
||||
if (!signingPublicKey.contentEquals(other.signingPublicKey)) return false
|
||||
} else if (other.signingPublicKey != null) return false
|
||||
if (isVerifiedNickname != other.isVerifiedNickname) return false
|
||||
if (lastSeen != other.lastSeen) return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = id.hashCode()
|
||||
result = 31 * result + nickname.hashCode()
|
||||
result = 31 * result + isConnected.hashCode()
|
||||
result = 31 * result + (noisePublicKey?.contentHashCode() ?: 0)
|
||||
result = 31 * result + (signingPublicKey?.contentHashCode() ?: 0)
|
||||
result = 31 * result + isVerifiedNickname.hashCode()
|
||||
result = 31 * result + lastSeen.hashCode()
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Manages active peers, nicknames, RSSI tracking, and peer fingerprints
|
||||
* Extracted from BluetoothMeshService for better separation of concerns
|
||||
*
|
||||
* Now includes centralized peer fingerprint management via PeerFingerprintManager singleton
|
||||
* and support for signed announcement verification
|
||||
*/
|
||||
class PeerManager {
|
||||
|
||||
@@ -19,13 +68,18 @@ class PeerManager {
|
||||
private const val CLEANUP_INTERVAL = 60000L // 1 minute
|
||||
}
|
||||
|
||||
// Peer tracking data
|
||||
private val peerNicknames = ConcurrentHashMap<String, String>()
|
||||
private val activePeers = ConcurrentHashMap<String, Long>() // peerID -> lastSeen timestamp
|
||||
// Peer tracking data - enhanced with verification status
|
||||
private val peers = ConcurrentHashMap<String, PeerInfo>() // peerID -> PeerInfo
|
||||
private val peerRSSI = ConcurrentHashMap<String, Int>()
|
||||
private val announcedPeers = CopyOnWriteArrayList<String>()
|
||||
private val announcedToPeers = CopyOnWriteArrayList<String>()
|
||||
|
||||
// Legacy support for existing code
|
||||
@Deprecated("Use PeerInfo structure instead")
|
||||
private val peerNicknames = ConcurrentHashMap<String, String>()
|
||||
@Deprecated("Use PeerInfo structure instead")
|
||||
private val activePeers = ConcurrentHashMap<String, Long>() // peerID -> lastSeen timestamp
|
||||
|
||||
// Centralized fingerprint management
|
||||
private val fingerprintManager = PeerFingerprintManager.getInstance()
|
||||
|
||||
@@ -38,13 +92,90 @@ class PeerManager {
|
||||
init {
|
||||
startPeriodicCleanup()
|
||||
}
|
||||
|
||||
|
||||
// MARK: - New PeerInfo-based methods
|
||||
|
||||
/**
|
||||
* Update peer information with verification data
|
||||
* Similar to iOS updatePeer method
|
||||
*/
|
||||
fun updatePeerInfo(
|
||||
peerID: String,
|
||||
nickname: String,
|
||||
noisePublicKey: ByteArray,
|
||||
signingPublicKey: ByteArray,
|
||||
isVerified: Boolean
|
||||
): Boolean {
|
||||
if (peerID == "unknown") return false
|
||||
|
||||
val now = System.currentTimeMillis()
|
||||
val existingPeer = peers[peerID]
|
||||
val isNewPeer = existingPeer == null
|
||||
|
||||
// Update or create peer info
|
||||
val peerInfo = PeerInfo(
|
||||
id = peerID,
|
||||
nickname = nickname,
|
||||
isConnected = true,
|
||||
noisePublicKey = noisePublicKey,
|
||||
signingPublicKey = signingPublicKey,
|
||||
isVerifiedNickname = isVerified,
|
||||
lastSeen = now
|
||||
)
|
||||
|
||||
peers[peerID] = peerInfo
|
||||
|
||||
// Update legacy structures for compatibility
|
||||
peerNicknames[peerID] = nickname
|
||||
activePeers[peerID] = now
|
||||
|
||||
if (isNewPeer && isVerified) {
|
||||
announcedPeers.add(peerID)
|
||||
notifyPeerListUpdate()
|
||||
Log.d(TAG, "🆕 New verified peer: $nickname ($peerID)")
|
||||
return true
|
||||
} else if (isVerified) {
|
||||
Log.d(TAG, "🔄 Updated verified peer: $nickname ($peerID)")
|
||||
} else {
|
||||
Log.d(TAG, "⚠️ Unverified peer announcement from: $nickname ($peerID)")
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Get peer info
|
||||
*/
|
||||
fun getPeerInfo(peerID: String): PeerInfo? {
|
||||
return peers[peerID]
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if peer is verified
|
||||
*/
|
||||
fun isPeerVerified(peerID: String): Boolean {
|
||||
return peers[peerID]?.isVerifiedNickname == true
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all verified peers
|
||||
*/
|
||||
fun getVerifiedPeers(): Map<String, PeerInfo> {
|
||||
return peers.filterValues { it.isVerifiedNickname }
|
||||
}
|
||||
|
||||
// MARK: - Legacy Methods (maintained for compatibility)
|
||||
|
||||
/**
|
||||
* Update peer last seen timestamp
|
||||
*/
|
||||
fun updatePeerLastSeen(peerID: String) {
|
||||
if (peerID != "unknown") {
|
||||
activePeers[peerID] = System.currentTimeMillis()
|
||||
// Also update PeerInfo if it exists
|
||||
peers[peerID]?.let { info ->
|
||||
peers[peerID] = info.copy(lastSeen = System.currentTimeMillis())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user