refactor but untested

This commit is contained in:
callebtc
2025-07-21 23:35:23 +02:00
parent cdac892042
commit a6469a3d73
2 changed files with 96 additions and 63 deletions
@@ -472,7 +472,6 @@ class BluetoothMeshService(private val context: Context) {
fun sendPrivateMessage(content: String, recipientPeerID: String, recipientNickname: String, messageID: String? = null) { fun sendPrivateMessage(content: String, recipientPeerID: String, recipientNickname: String, messageID: String? = null) {
if (content.isEmpty() || recipientPeerID.isEmpty() || recipientNickname.isEmpty()) return if (content.isEmpty() || recipientPeerID.isEmpty() || recipientNickname.isEmpty()) return
serviceScope.launch {
val nickname = delegate?.getNickname() ?: myPeerID val nickname = delegate?.getNickname() ?: myPeerID
val message = BitchatMessage( val message = BitchatMessage(
@@ -488,47 +487,39 @@ class BluetoothMeshService(private val context: Context) {
message.toBinaryPayload()?.let { messageData -> message.toBinaryPayload()?.let { messageData ->
try { try {
// Pad and encrypt // Pad the message data
val blockSize = MessagePadding.optimalBlockSize(messageData.size) val blockSize = MessagePadding.optimalBlockSize(messageData.size)
val paddedData = MessagePadding.pad(messageData, blockSize) val paddedData = MessagePadding.pad(messageData, blockSize)
val encryptedPayload = securityManager.encryptForPeer(paddedData, recipientPeerID)
if (encryptedPayload != null) { // Create inner packet with the padded message data
// Sign val innerPacket = BitchatPacket(
// val signature = securityManager.signPacket(encryptedPayload) type = MessageType.MESSAGE.value,
val packet = BitchatPacket(
type = MessageType.NOISE_ENCRYPTED.value,
senderID = hexStringToByteArray(myPeerID), senderID = hexStringToByteArray(myPeerID),
recipientID = hexStringToByteArray(recipientPeerID), recipientID = hexStringToByteArray(recipientPeerID),
timestamp = System.currentTimeMillis().toULong(), timestamp = System.currentTimeMillis().toULong(),
payload = encryptedPayload, payload = paddedData,
signature = null, signature = null,
ttl = MAX_TTL ttl = MAX_TTL
) )
// Cache for offline favorites // Cache for offline favorites
if (storeForwardManager.shouldCacheForPeer(recipientPeerID)) { if (storeForwardManager.shouldCacheForPeer(recipientPeerID)) {
storeForwardManager.cacheMessage(packet, messageID ?: message.id) storeForwardManager.cacheMessage(innerPacket, messageID ?: message.id)
} }
// Send with delay // Use the new encrypt and broadcast function
delay(Random.nextLong(50, 500)) encryptAndBroadcastNoisePacket(innerPacket, recipientPeerID)
connectionManager.broadcastPacket(RoutedPacket(packet))
}
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "Failed to send private message: ${e.message}") Log.e(TAG, "Failed to send private message: ${e.message}")
} }
} }
} }
}
/** /**
* Send delivery acknowledgment for a received private message * Send delivery acknowledgment for a received private message
*/ */
fun sendDeliveryAck(message: BitchatMessage, senderPeerID: String) { fun sendDeliveryAck(message: BitchatMessage, senderPeerID: String) {
serviceScope.launch {
val nickname = delegate?.getNickname() ?: myPeerID val nickname = delegate?.getNickname() ?: myPeerID
val ack = DeliveryAck( val ack = DeliveryAck(
originalMessageID = message.id, originalMessageID = message.id,
@@ -538,26 +529,68 @@ class BluetoothMeshService(private val context: Context) {
) )
try { try {
val ackData = ack.encode() ?: return@launch val ackData = ack.encode() ?: return
val encryptedPayload = securityManager.encryptForPeer(ackData, senderPeerID)
if (encryptedPayload != null) { // Create inner packet with the delivery ACK data
val packet = BitchatPacket( val innerPacket = BitchatPacket(
type = MessageType.DELIVERY_ACK.value, type = MessageType.DELIVERY_ACK.value,
senderID = hexStringToByteArray(myPeerID), senderID = hexStringToByteArray(myPeerID),
recipientID = hexStringToByteArray(senderPeerID), recipientID = hexStringToByteArray(senderPeerID),
timestamp = System.currentTimeMillis().toULong(), timestamp = System.currentTimeMillis().toULong(),
payload = encryptedPayload, payload = ackData,
signature = null, signature = null,
ttl = 3u ttl = 3u
) )
connectionManager.broadcastPacket(RoutedPacket(packet)) // Use the new encrypt and broadcast function
} encryptAndBroadcastNoisePacket(innerPacket, senderPeerID)
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "Failed to send delivery ACK: ${e.message}") Log.e(TAG, "Failed to send delivery ACK: ${e.message}")
} }
} }
/**
* Encrypt a BitchatPacket and broadcast it as a NOISE_ENCRYPTED message
* This is the correct protocol implementation - encrypt the entire packet, not just the payload
*/
private fun encryptAndBroadcastNoisePacket(innerPacket: BitchatPacket, recipientPeerID: String) {
serviceScope.launch {
try {
// Serialize the inner packet to binary data
val innerPacketData = innerPacket.toBinaryData()
if (innerPacketData == null) {
Log.e(TAG, "Failed to serialize inner packet for encryption")
return@launch
}
// Encrypt the serialized packet using Noise encryption
val encryptedPayload = securityManager.encryptForPeer(innerPacketData, recipientPeerID)
if (encryptedPayload != null) {
// Create the outer NOISE_ENCRYPTED packet
val outerPacket = BitchatPacket(
type = MessageType.NOISE_ENCRYPTED.value,
senderID = hexStringToByteArray(myPeerID),
recipientID = hexStringToByteArray(recipientPeerID),
timestamp = System.currentTimeMillis().toULong(),
payload = encryptedPayload,
signature = null,
ttl = MAX_TTL
)
// Broadcast the encrypted packet
connectionManager.broadcastPacket(RoutedPacket(outerPacket))
Log.d(TAG, "Encrypted and sent packet type ${innerPacket.type} to $recipientPeerID (${encryptedPayload.size} bytes encrypted)")
} else {
Log.w(TAG, "Failed to encrypt packet for $recipientPeerID - no session available")
}
} catch (e: Exception) {
Log.e(TAG, "Failed to encrypt and broadcast Noise packet to $recipientPeerID: ${e.message}")
}
}
} }
/** /**
@@ -39,7 +39,7 @@ class PacketProcessor(private val myPeerID: String) {
Log.d(TAG, "🎭 Created packet actor for peer: $peerID") Log.d(TAG, "🎭 Created packet actor for peer: $peerID")
try { try {
for (packet in channel) { for (packet in channel) {
Log.d(TAG, "Processing packet type ${packet.packet.type} from $peerID (serialized)") Log.d(TAG, "📦 Processing packet type ${packet.packet.type} from $peerID (serialized)")
handleReceivedPacket(packet) handleReceivedPacket(packet)
Log.d(TAG, "Completed packet type ${packet.packet.type} from $peerID") Log.d(TAG, "Completed packet type ${packet.packet.type} from $peerID")
} }