decrypt correctly

This commit is contained in:
callebtc
2025-07-20 20:50:01 +02:00
parent 75cc4615c7
commit b275ad7308
31 changed files with 17 additions and 3775 deletions
@@ -259,7 +259,7 @@ class MessageHandler(private val myPeerID: String) {
}
/**
* Handle private message addressed to us
* Handle (decrypted) private message addressed to us
*/
private suspend fun handlePrivateMessage(packet: BitchatPacket, peerID: String) {
try {
@@ -268,18 +268,9 @@ class MessageHandler(private val myPeerID: String) {
Log.w(TAG, "Invalid signature for private message from $peerID")
return
}
// Decrypt message
val decryptedData = delegate?.decryptFromPeer(packet.payload, peerID)
if (decryptedData == null) {
Log.e(TAG, "Failed to decrypt private message from $peerID")
return
}
val unpaddedData = MessagePadding.unpad(decryptedData)
// Parse message
val message = BitchatMessage.fromBinaryPayload(unpaddedData)
val message = BitchatMessage.fromBinaryPayload(packet.payload)
if (message != null) {
// Check for cover traffic (dummy messages)
if (message.content.startsWith("☂DUMMY☂")) {
@@ -288,14 +279,7 @@ class MessageHandler(private val myPeerID: String) {
}
delegate?.updatePeerNickname(peerID, message.sender)
// Replace timestamp with current time (same as iOS)
val messageWithCurrentTime = message.copy(
senderPeerID = peerID,
timestamp = Date() // Use current time instead of original timestamp
)
delegate?.onMessageReceived(messageWithCurrentTime)
delegate?.onMessageReceived(message)
// Send delivery ACK
sendDeliveryAck(message, peerID)
@@ -97,6 +97,11 @@ class SecurityManager(private val encryptionService: EncryptionService, private
// Skip our own handshake messages
if (peerID == myPeerID) return false
if (encryptionService.hasEstablishedSession(peerID)) {
Log.d(TAG, "Handshake already completed with $peerID")
return true
}
if (packet.payload.isEmpty()) {
Log.w(TAG, "Noise handshake packet has empty payload")
@@ -124,8 +129,7 @@ class SecurityManager(private val encryptionService: EncryptionService, private
delegate?.sendHandshakeResponse(peerID, response)
// Notify delegate of handshake completion
delegate?.onKeyExchangeCompleted(peerID, packet.payload, routed.relayAddress)
// delegate?.onKeyExchangeCompleted(peerID, packet.payload, routed.relayAddress)
return true
} else {
// Check if session is now established (handshake complete)
@@ -131,7 +131,7 @@ class NoiseEncryptionService(private val context: Context) {
*/
fun processHandshakeMessage(data: ByteArray, peerID: String): ByteArray? {
return try {
sessionManager.handleIncomingHandshake(peerID, data)
sessionManager.processHandshakeMessage(peerID, data)
} catch (e: Exception) {
Log.e(TAG, "Failed to process handshake from $peerID: ${e.message}")
null
@@ -246,7 +246,7 @@ class NoiseSession(
val handshakeStateLocal = handshakeState ?: throw IllegalStateException("Handshake state is null")
// Let the Noise library validate message sizes and handle the flow
val payloadBuffer = ByteArray(MAX_PAYLOAD_SIZE) // Buffer for any payload data
val payloadBuffer = ByteArray(XX_MESSAGE_2_SIZE + MAX_PAYLOAD_SIZE) // Buffer for any payload data
// Read the incoming message - the Noise library will handle validation
val payloadLength = handshakeStateLocal.readMessage(message, 0, message.size, payloadBuffer, 0)
@@ -270,7 +270,7 @@ class NoiseSession(
HandshakeState.SPLIT -> {
// Handshake complete, split into transport keys
completeHandshake()
Log.d(TAG, "🔒 XX handshake completed with $peerID")
Log.d(TAG, " XX handshake completed with $peerID")
null
}
@@ -329,7 +329,7 @@ class NoiseSession(
messagesReceived = 0
state = NoiseSessionState.Established
Log.d(TAG, "Real XX handshake completed with $peerID - transport keys derived")
Log.d(TAG, "Handshake completed with $peerID as isInitiator: $isInitiator - transport keys derived")
} catch (e: Exception) {
state = NoiseSessionState.Failed(e)
Log.e(TAG, "Failed to complete handshake: ${e.message}")
@@ -79,10 +79,10 @@ class NoiseSessionManager(
}
/**
* SIMPLIFIED: Handle incoming handshake message
* Handle incoming handshake message
*/
fun handleIncomingHandshake(peerID: String, message: ByteArray): ByteArray? {
Log.d(TAG, "handleIncomingHandshake($peerID, ${message.size} bytes)")
fun processHandshakeMessage(peerID: String, message: ByteArray): ByteArray? {
Log.d(TAG, "processHandshakeMessage($peerID, ${message.size} bytes)")
try {
var session = getSession(peerID)