diff --git a/NearbyShare/InboundNearbyConnection.swift b/NearbyShare/InboundNearbyConnection.swift index 058d9f3..dd3c832 100644 --- a/NearbyShare/InboundNearbyConnection.swift +++ b/NearbyShare/InboundNearbyConnection.swift @@ -136,6 +136,9 @@ class InboundNearbyConnection: NearbyConnection { try fileInfo.fileHandle?.write(contentsOf: frame.payloadChunk.body) filesToBeReceived[id]!.bytesTransferred += Int64(frame.payloadChunk.body.count) fileInfo.progress?.completedUnitCount = filesToBeReceived[id]!.bytesTransferred + + // only for logging + self.bytesTransferred += Int64(frame.payloadChunk.body.count) } catch { log("[InboundNearbyConnection] Error occurred during writing file: \(error.localizedDescription)") @@ -224,6 +227,7 @@ class InboundNearbyConnection: NearbyConnection { guard msg.hasMessageType, msg.hasMessageData else { throw NearbyError.requiredFieldMissing("clientInit ukey2message.type|data") } guard case .clientInit = msg.messageType else { sendUkey2Alert(type: .badMessageType) + log("[InboundNearbyConnection] Unsupported message type: \(msg.messageType)") throw NearbyError.ukey2 } let clientInit: Securegcm_Ukey2ClientInit @@ -231,14 +235,17 @@ class InboundNearbyConnection: NearbyConnection { clientInit = try Securegcm_Ukey2ClientInit(serializedBytes: msg.messageData) } catch { sendUkey2Alert(type: .badMessageData) + log("[InboundNearbyConnection] Failed to parse clientInit: \(error)") throw NearbyError.ukey2 } guard clientInit.version == 1 else { sendUkey2Alert(type: .badVersion) + log("[InboundNearbyConnection] Unsupported clientInit version: \(clientInit.version)") throw NearbyError.ukey2 } guard clientInit.random.count == 32 else { sendUkey2Alert(type: .badRandom) + log("[InboundNearbyConnection] Unsupported clientInit random: \(clientInit.random.count)") throw NearbyError.ukey2 } var found = false @@ -251,10 +258,12 @@ class InboundNearbyConnection: NearbyConnection { } guard found else { sendUkey2Alert(type: .badHandshakeCipher) + log("[InboundNearbyConnection] Unsupported clientInit handshakeCipher: \(clientInit.cipherCommitments)") throw NearbyError.ukey2 } guard clientInit.nextProtocol == "AES_256_CBC-HMAC_SHA256" else { sendUkey2Alert(type: .badNextProtocol) + log("[InboundNearbyConnection] Unsupported clientInit nextProtocol: \(clientInit.nextProtocol)") throw NearbyError.ukey2 } @@ -287,11 +296,17 @@ class InboundNearbyConnection: NearbyConnection { private func processUkey2ClientFinish(_ msg: Securegcm_Ukey2Message, raw: Data) throws { guard msg.hasMessageType, msg.hasMessageData else { throw NearbyError.requiredFieldMissing("clientFinish ukey2message.type|data") } - guard case .clientFinish = msg.messageType else { throw NearbyError.ukey2 } + guard case .clientFinish = msg.messageType else { + log("[InboundNearbyConnection] Unexpected message type \(msg.messageType)") + throw NearbyError.ukey2 + } var sha = SHA512() sha.update(data: raw) - guard cipherCommitment == Data(sha.finalize()) else { throw NearbyError.ukey2 } + guard cipherCommitment == Data(sha.finalize()) else { + log("[InboundNearbyConnection] Invalid cipherCommitment in clientFinish") + throw NearbyError.ukey2 + } let clientFinish = try Securegcm_Ukey2ClientFinished(serializedBytes: msg.messageData) guard clientFinish.hasPublicKey else { throw NearbyError.requiredFieldMissing("ukey2clientFinish.publicKey") } diff --git a/NearbyShare/NearbyConnection.swift b/NearbyShare/NearbyConnection.swift index acd0682..3eeb9d6 100644 --- a/NearbyShare/NearbyConnection.swift +++ b/NearbyShare/NearbyConnection.swift @@ -25,6 +25,7 @@ class NearbyConnection { var remoteDeviceInfo: RemoteDeviceInfo? var encryptionDone: Bool = false var lastError: Error? + var bytesTransferred: Int64 = 0 private var payloadBuffers: [Int64: NSMutableData] = [:] private var connectionClosed: Bool = false @@ -382,7 +383,7 @@ class NearbyConnection { } else if offlineFrame.hasV1, offlineFrame.v1.hasType, case .keepAlive = offlineFrame.v1.type { - log("[NearbyConnection] Sent keep-alive") + log("[NearbyConnection] Sent keep-alive, \(self.bytesTransferred) bytes sent") sendKeepAlive(ack: true) } else { diff --git a/NearbyShare/OutboundNearbyConnection.swift b/NearbyShare/OutboundNearbyConnection.swift index 17b1857..7c83375 100644 --- a/NearbyShare/OutboundNearbyConnection.swift +++ b/NearbyShare/OutboundNearbyConnection.swift @@ -222,19 +222,23 @@ class OutboundNearbyConnection: NearbyConnection { ukeyServerInitMsgData = raw guard frame.messageType == .serverInit else { sendUkey2Alert(type: .badMessageType) + log("[OutboundNearbyConnection] Invalid message type: \(frame.messageType)") throw NearbyError.ukey2 } let serverInit = try Securegcm_Ukey2ServerInit(serializedBytes: frame.messageData) guard serverInit.version == 1 else { sendUkey2Alert(type: .badVersion) + log("[OutboundNearbyConnection] Invalid version: \(serverInit.version)") throw NearbyError.ukey2 } guard serverInit.random.count == 32 else { sendUkey2Alert(type: .badRandom) + log("[OutboundNearbyConnection] Invalid random: \(serverInit.random.count)") throw NearbyError.ukey2 } guard serverInit.handshakeCipher == .p256Sha512 else { sendUkey2Alert(type: .badHandshakeCipher) + log("[OutboundNearbyConnection] Invalid handshake cipher: \(serverInit.handshakeCipher)") throw NearbyError.ukey2 } @@ -447,6 +451,10 @@ class OutboundNearbyConnection: NearbyConnection { } }) totalBytesSent += Int64(fileBuffer.count) + + // only for logging + self.bytesTransferred += Int64(fileBuffer.count) + startInactivityTimer() delegate?.outboundConnection(connection: self, transferProgress: Double(totalBytesSent) / Double(totalBytesToSend))