Better error message for failed signature check, throw error on canceled file transfer

This commit is contained in:
Leon Böttger committed 2025-07-28 14:10:16 +02:00
1 parent f18649b959
commit ddd9a32023
4 files changed
+227 -7

No files matched your search

@@ -101,6 +101,8 @@ class InboundNearbyConnection: NearbyConnection {
override func processTransferSetupFrame(_ frame: Sharing_Nearby_Frame) throws {
if frame.hasV1 && frame.v1.hasType, case .cancel = frame.v1.type {
self.lastError = NearbyError.canceled(reason: .userCanceled)
self.cancelled = true
log("[InboundNearbyConnection] Transfer canceled")
try sendDisconnectionAndDisconnect()
return
+10 -3
View File
@@ -26,6 +26,7 @@ class NearbyConnection {
var encryptionDone: Bool = false
var lastError: Error?
var bytesTransferred: Int64 = 0
var cancelled: Bool = false
private var payloadBuffers: [Int64: NSMutableData] = [:]
private var connectionClosed: Bool = false
@@ -299,7 +300,7 @@ class NearbyConnection {
guard smsg.hasSignature, smsg.hasHeaderAndBody else { throw NearbyError.requiredFieldMissing("secureMessage.signature|headerAndBody") }
let hmac = Data(HMAC<SHA256>.authenticationCode(for: smsg.headerAndBody, using: recvHmacKey!))
guard hmac == smsg.signature else { throw NearbyError.protocolError("hmac!=signature: Expected \(hmac), got \(smsg.signature)") }
guard hmac == smsg.signature else { throw NearbyError.protocolError("Error.Signature".localized()) }
let headerAndBody = try Securemessage_HeaderAndBody(serializedBytes: smsg.headerAndBody)
var decryptedData = Data(count: headerAndBody.body.count)
@@ -338,8 +339,14 @@ class NearbyConnection {
guard header.hasType, header.hasID else { throw NearbyError.requiredFieldMissing("payloadHeader.type|id") }
guard payloadTransfer.hasPayloadChunk, chunk.hasOffset, chunk.hasFlags else {
log("[NearbyConnection] Payload transfer chunk is missing offset or flags, likely canceled.")
throw NearbyError.canceled(reason: .userCanceled)
if payloadTransfer.controlMessage.event == .payloadCanceled {
log("[NearbyConnection] Cancel control frame received.")
throw NearbyError.canceled(reason: .userCanceled)
}
log("[NearbyConnection] Payload transfer chunk is missing offset or flags. Frame is \(payloadTransfer.debugDescription)")
throw NearbyError.requiredFieldMissing("payloadChunk.offset|flags")
}
if case .bytes = header.type {
@@ -24,7 +24,6 @@ class OutboundNearbyConnection: NearbyConnection {
private var currentTransfer: OutgoingFileTransfer?
private var totalBytesToSend: Int64 = 0
private var totalBytesSent: Int64 = 0
private var cancelled: Bool = false
private var textPayloadID: Int64 = 0
public var delegate: OutboundNearbyConnectionDelegate?
@@ -136,10 +135,11 @@ class OutboundNearbyConnection: NearbyConnection {
override func processTransferSetupFrame(_ frame: Sharing_Nearby_Frame) throws {
if frame.hasV1 && frame.v1.hasType, case .cancel = frame.v1.type {
self.cancelled = true
self.lastError = NearbyError.canceled(reason: .userCanceled)
log("[OutboundNearbyConnection] Transfer canceled")
try sendDisconnectionAndDisconnect()
delegate?.outboundConnection(connection: self, failedWithError: NearbyError.canceled(reason: .userCanceled))
delegate?.outboundConnection(connection: self, failedWithError: self.lastError!)
return
}