wip, use noise encryption

This commit is contained in:
callebtc
2025-07-19 21:31:02 +02:00
parent c419e97968
commit b5d062a0a9
6 changed files with 303 additions and 264 deletions
@@ -122,44 +122,19 @@ class BluetoothMeshService(private val context: Context) {
}
}
override fun processNoiseHandshake(peerID: String, payload: ByteArray, isInitiation: Boolean): Boolean {
return try {
Log.d(TAG, "Processing Noise handshake from $peerID, payload size: ${payload.size} bytes")
// Use proper Noise protocol implementation
val response = noiseEncryptionService.processHandshakeMessage(payload, peerID)
if (response != null) {
// Send response back
val responsePacket = BitchatPacket(
version = 1u,
type = MessageType.NOISE_HANDSHAKE_RESP.value,
senderID = hexStringToByteArray(myPeerID),
recipientID = hexStringToByteArray(peerID),
timestamp = System.currentTimeMillis().toULong(),
payload = response,
ttl = 1u
)
connectionManager.broadcastPacket(RoutedPacket(responsePacket))
Log.d(TAG, "Sent Noise handshake response to $peerID")
}
// Check if session is now established
if (noiseEncryptionService.hasEstablishedSession(peerID)) {
Log.d(TAG, "Noise session established with $peerID")
// Get the peer's public key for fingerprinting
val peerPublicKey = noiseEncryptionService.getPeerPublicKeyData(peerID)
if (peerPublicKey != null) {
// Notify delegate about successful handshake
delegate?.registerPeerPublicKey(peerID, peerPublicKey)
}
}
true
} catch (e: Exception) {
Log.e(TAG, "Failed to process Noise handshake from $peerID: ${e.message}")
false
}
override fun sendHandshakeResponse(peerID: String, response: ByteArray) {
// Send Noise handshake response
val responsePacket = BitchatPacket(
version = 1u,
type = MessageType.NOISE_HANDSHAKE_RESP.value,
senderID = hexStringToByteArray(myPeerID),
recipientID = hexStringToByteArray(peerID),
timestamp = System.currentTimeMillis().toULong(),
payload = response,
ttl = 1u
)
connectionManager.broadcastPacket(RoutedPacket(responsePacket))
Log.d(TAG, "Sent Noise handshake response to $peerID (${response.size} bytes)")
}
}
@@ -634,6 +609,41 @@ class BluetoothMeshService(private val context: Context) {
*/
fun getPeerRSSI(): Map<String, Int> = peerManager.getAllPeerRSSI()
/**
* Check if we have an established Noise session with a peer
*/
fun hasEstablishedSession(peerID: String): Boolean {
return encryptionService.hasEstablishedSession(peerID)
}
/**
* Get peer fingerprint for identity management
*/
fun getPeerFingerprint(peerID: String): String? {
return encryptionService.getPeerFingerprint(peerID)
}
/**
* Get our identity fingerprint
*/
fun getIdentityFingerprint(): String {
return encryptionService.getIdentityFingerprint()
}
/**
* Check if encryption icon should be shown for a peer
*/
fun shouldShowEncryptionIcon(peerID: String): Boolean {
return encryptionService.shouldShowEncryptionIcon(peerID)
}
/**
* Get all peers with established encrypted sessions
*/
fun getEncryptedPeers(): List<String> {
return encryptionService.getEstablishedPeers()
}
/**
* Get device address for a specific peer ID
*/
@@ -197,11 +197,14 @@ class MessageHandler(private val myPeerID: String) {
if (peerID == myPeerID) return
val recipientID = packet.recipientID?.takeIf { !it.contentEquals(delegate?.getBroadcastRecipient()) }
var recipientIDString = ""
if (recipientID != null) {
recipientIDString = String(recipientID).replace("\u0000", "")
}
if (recipientID == null) {
// BROADCAST MESSAGE
handleBroadcastMessage(routed)
} else if (String(recipientID).replace("\u0000", "") == myPeerID) {
} else if (recipientIDString == myPeerID) {
// PRIVATE MESSAGE FOR US
handlePrivateMessage(packet, peerID)
} else if (packet.ttl > 0u) {
@@ -114,19 +114,29 @@ class SecurityManager(private val encryptionService: EncryptionService, private
processedKeyExchanges.add(exchangeKey)
try {
// Process the Noise handshake through the delegate (NoiseEncryptionService)
val success = delegate?.processNoiseHandshake(peerID, packet.payload, step == 1) ?: false
// Process the Noise handshake through the updated EncryptionService
val response = encryptionService.processHandshakeMessage(packet.payload, peerID)
if (success) {
Log.d(TAG, "Successfully processed Noise handshake step $step from $peerID")
if (response != null) {
Log.d(TAG, "Successfully processed Noise handshake step $step from $peerID, sending response")
// Notify delegate
// Send handshake response through delegate
delegate?.sendHandshakeResponse(peerID, response)
// Notify delegate of handshake completion
delegate?.onKeyExchangeCompleted(peerID, packet.payload, routed.relayAddress)
return true
} else {
Log.w(TAG, "Failed to process Noise handshake from $peerID")
return false
// Check if session is now established (handshake complete)
if (encryptionService.hasEstablishedSession(peerID)) {
Log.d(TAG, "✅ Noise handshake completed with $peerID")
delegate?.onKeyExchangeCompleted(peerID, packet.payload, routed.relayAddress)
return true
} else {
Log.w(TAG, "Failed to process Noise handshake from $peerID")
return false
}
}
} catch (e: Exception) {
@@ -258,9 +268,7 @@ class SecurityManager(private val encryptionService: EncryptionService, private
* Check if we have encryption keys for a peer
*/
fun hasKeysForPeer(peerID: String): Boolean {
// This would need to be implemented in EncryptionService
// For now, we'll assume we have keys if we processed a key exchange
return processedKeyExchanges.any { it.startsWith("$peerID-") }
return encryptionService.hasEstablishedSession(peerID)
}
/**
@@ -369,5 +377,5 @@ class SecurityManager(private val encryptionService: EncryptionService, private
*/
interface SecurityManagerDelegate {
fun onKeyExchangeCompleted(peerID: String, peerPublicKeyData: ByteArray, receivedAddress: String?)
fun processNoiseHandshake(peerID: String, payload: ByteArray, isInitiation: Boolean): Boolean
fun sendHandshakeResponse(peerID: String, response: ByteArray)
}