Fix encryption key exchange and add persistent identity for favorites

- Implement three-tier key system: ephemeral encryption, ephemeral signing, persistent identity
- Fix signature verification by properly separating KeyAgreement and Signing keys
- Add persistent identity key for favorites that survives app restarts
- Fix Data subscript crashes by converting to byte arrays
- Add proper error handling for key exchange
- Private messages remain fully encrypted with ephemeral keys
This commit is contained in:
jack
2025-07-04 01:43:16 +02:00
parent 34e51b3d9a
commit fe986ed397
12 changed files with 679 additions and 929 deletions
+1 -49
View File
@@ -185,7 +185,6 @@ extension BitchatMessage {
// - Recipient nickname length + data
// - Sender peer ID length + data
// - Mentions array
// - Voice note duration (8 bytes, double) + data length (4 bytes) + data
var flags: UInt8 = 0
if isRelay { flags |= 0x01 }
@@ -194,7 +193,6 @@ extension BitchatMessage {
if recipientNickname != nil { flags |= 0x08 }
if senderPeerID != nil { flags |= 0x10 }
if mentions != nil && !mentions!.isEmpty { flags |= 0x20 }
if voiceNoteData != nil { flags |= 0x40 }
data.append(flags)
@@ -261,24 +259,6 @@ extension BitchatMessage {
}
}
// Voice note data
if let voiceData = voiceNoteData, let duration = voiceNoteDuration {
// Duration as 8 bytes (double)
let durationBits = duration.bitPattern
for i in (0..<8).reversed() {
data.append(UInt8((durationBits >> (i * 8)) & 0xFF))
}
// Voice data length as 4 bytes (UInt32)
let voiceLength = UInt32(min(voiceData.count, Int(UInt32.max)))
for i in (0..<4).reversed() {
data.append(UInt8((voiceLength >> (i * 8)) & 0xFF))
}
// Voice data
data.append(voiceData.prefix(Int(voiceLength)))
}
return data
}
@@ -304,7 +284,6 @@ extension BitchatMessage {
let hasRecipientNickname = (flags & 0x08) != 0
let hasSenderPeerID = (flags & 0x10) != 0
let hasMentions = (flags & 0x20) != 0
let hasVoiceNote = (flags & 0x40) != 0
// Timestamp
guard offset + 8 <= dataCopy.count else {
@@ -402,31 +381,6 @@ extension BitchatMessage {
}
}
// Voice note data
var voiceNoteData: Data?
var voiceNoteDuration: TimeInterval?
if hasVoiceNote && offset + 12 <= dataCopy.count {
// Duration (8 bytes)
let durationData = dataCopy[offset..<offset+8]
let durationBits = durationData.reduce(0) { result, byte in
(result << 8) | UInt64(byte)
}
voiceNoteDuration = TimeInterval(bitPattern: durationBits)
offset += 8
// Voice data length (4 bytes)
let lengthData = dataCopy[offset..<offset+4]
let voiceLength = lengthData.reduce(0) { result, byte in
(result << 8) | UInt32(byte)
}
offset += 4
// Voice data
if offset + Int(voiceLength) <= dataCopy.count {
voiceNoteData = dataCopy[offset..<offset+Int(voiceLength)]
}
}
let message = BitchatMessage(
sender: sender,
content: content,
@@ -436,9 +390,7 @@ extension BitchatMessage {
isPrivate: isPrivate,
recipientNickname: recipientNickname,
senderPeerID: senderPeerID,
mentions: mentions,
voiceNoteData: voiceNoteData,
voiceNoteDuration: voiceNoteDuration
mentions: mentions
)
return message
}