From 58e488c0ad0b5095337b85cf1bd3828a7ba0ce13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20B=C3=B6ttger?= Date: Mon, 5 May 2025 20:59:37 +0200 Subject: [PATCH] More logging to fix protobuf wrong error --- NearDrop/TutorialView.swift | 3 + NearbyShare/InboundNearbyConnection.swift | 69 ++++++++++++++++++----- NearbyShare/NearbyConnection.swift | 8 ++- NearbyShare/NearbyConnectionManager.swift | 8 +++ QuickDrop.xcodeproj/project.pbxproj | 24 +++++++- 5 files changed, 93 insertions(+), 19 deletions(-) diff --git a/NearDrop/TutorialView.swift b/NearDrop/TutorialView.swift index bcff9f9..b221b08 100644 --- a/NearDrop/TutorialView.swift +++ b/NearDrop/TutorialView.swift @@ -113,6 +113,9 @@ struct LargeAppIconView: View { print("Clicked \(taps) times") if taps == 5 { + + NearbyConnectionManager.shared.checkSignature = false + sendLoggingString() print("Copied log to clipboard") diff --git a/NearbyShare/InboundNearbyConnection.swift b/NearbyShare/InboundNearbyConnection.swift index 6e061a6..9aafd62 100644 --- a/NearbyShare/InboundNearbyConnection.swift +++ b/NearbyShare/InboundNearbyConnection.swift @@ -52,7 +52,7 @@ class InboundNearbyConnection: NearbyConnection{ log("Received frame in state \(currentState)...") - do{ + do { switch currentState { case .initial: let frame=try Location_Nearby_Connections_OfflineFrame(serializedBytes: frameData) @@ -68,26 +68,47 @@ class InboundNearbyConnection: NearbyConnection{ let frame=try Location_Nearby_Connections_OfflineFrame(serializedBytes: frameData) try processConnectionResponseFrame(frame) default: - let smsg=try Securemessage_SecureMessage(serializedBytes: frameData) + + var smsg: Securemessage_SecureMessage + + do { + smsg = try Securemessage_SecureMessage(serializedBytes: frameData) + } + catch { + log("Error deserializing secure message. Trying again....") + + // last 32 bytes = HMAC key + // change 2 bytes before to 0x12 0x20 for Protobuf to succeed + + if frameData.count < 34 { + throw NearbyError.protocolError("Frame too short") + } + + var newData = frameData + let count = newData.count + newData[count - 34] = 0x12 + newData[count - 33] = 0x20 + + smsg = try Securemessage_SecureMessage(serializedBytes: newData) + + log("Secure message deserialized successfully after fixing Protobuf message") + } try decryptAndProcessReceivedSecureMessage(smsg) } - }catch{ + } catch { lastError = error log("Deserialization error: \(error) in state \(currentState). Payload: \(frameData.hex)") - let lastState = currentState - log("Waiting for 5 seconds to check if connection is still alive") + //log("Public Key: \(publicKey?.pem ?? "nil")") + //log("Private Key: \(privateKey?.pem ?? "nil")") + //log("Ukey Client Init Msg: \(ukeyClientInitMsgData?.hex ?? "nil")") + //log("Ukey Server Init Msg: \(ukeyServerInitMsgData?.hex ?? "nil")") + log("S1: \(decryptKey?.map { String(format: "%02x", $0) }.joined() ?? "nil")") + //log("Encrypt Key: \(encryptKey?.map { String(format: "%02x", $0) }.joined() ?? "nil")") + log("Received HMAC/Signature Key: \(recvHmacKey?.withUnsafeBytes { Data(Array($0)) }.map { String(format: "%02x", $0) }.joined() ?? "nil")") + log("Sent HMAC/Signature Key: \(sendHmacKey?.withUnsafeBytes { Data(Array($0)) }.map { String(format: "%02x", $0) }.joined() ?? "nil")") - DispatchQueue.main.asyncAfter(deadline: .now() + 5) { - - if self.currentState == lastState { - log("Still in state \(self.currentState), last state was \(lastState)") - self.protocolError() - } - else { - log("State changed to \(self.currentState), not closing connection") - } - } + self.protocolError() } } @@ -575,3 +596,21 @@ extension Data { return map { String(format: "%02hhx", $0) }.joined() } } + +extension String { + var dataFromHex: Data { + var data = Data(capacity: count / 2) + var index = startIndex + while index < endIndex { + let nextIndex = self.index(index, offsetBy: 2) + if nextIndex <= endIndex, + let byte = UInt8(self[index...authenticationCode(for: smsg.headerAndBody, using: recvHmacKey!)) - guard hmac==smsg.signature else { throw NearbyError.protocolError("hmac!=signature") } + + if NearbyConnectionManager.shared.checkSignature { + let hmac = Data(HMAC.authenticationCode(for: smsg.headerAndBody, using: recvHmacKey!)) + guard hmac == smsg.signature else { throw NearbyError.protocolError("hmac!=signature: Expected \(hmac), got \(smsg.signature)") } + } + let headerAndBody=try Securemessage_HeaderAndBody(serializedBytes: smsg.headerAndBody) var decryptedData=Data(count: headerAndBody.body.count) diff --git a/NearbyShare/NearbyConnectionManager.swift b/NearbyShare/NearbyConnectionManager.swift index fc012e8..93e2300 100644 --- a/NearbyShare/NearbyConnectionManager.swift +++ b/NearbyShare/NearbyConnectionManager.swift @@ -150,6 +150,14 @@ public protocol ShareExtensionDelegate:AnyObject{ public class NearbyConnectionManager : NSObject, NetServiceDelegate, InboundNearbyConnectionDelegate, OutboundNearbyConnectionDelegate{ + public var checkSignature = true { + didSet { + if !checkSignature { + log("WARNING: Signature checking is disabled. Resume with caution.") + } + } + } + private var tcpListener:NWListener; public let endpointID:[UInt8]=generateEndpointID() private var mdnsService:NetService? diff --git a/QuickDrop.xcodeproj/project.pbxproj b/QuickDrop.xcodeproj/project.pbxproj index 1a69ada..56bb06a 100644 --- a/QuickDrop.xcodeproj/project.pbxproj +++ b/QuickDrop.xcodeproj/project.pbxproj @@ -201,6 +201,12 @@ EB2F9BFA2DA04B67004010A2 /* Helpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Helpers.swift; sourceTree = ""; }; EB36435F2D73A29F009CDFF8 /* AudioManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioManager.swift; sourceTree = ""; }; EB3643612D73A2CA009CDFF8 /* timeIsNow.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = timeIsNow.mp3; sourceTree = ""; }; + EB3B73F32DC8FF3A00369951 /* device_to_device_messages.proto */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.protobuf; path = device_to_device_messages.proto; sourceTree = ""; }; + EB3B73F42DC8FF3A00369951 /* offline_wire_formats.proto */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.protobuf; path = offline_wire_formats.proto; sourceTree = ""; }; + EB3B73F52DC8FF3A00369951 /* securegcm.proto */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.protobuf; path = securegcm.proto; sourceTree = ""; }; + EB3B73F62DC8FF3A00369951 /* securemessage.proto */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.protobuf; path = securemessage.proto; sourceTree = ""; }; + EB3B73F72DC8FF3A00369951 /* ukey.proto */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.protobuf; path = ukey.proto; sourceTree = ""; }; + EB3B73F82DC8FF3A00369951 /* wire_format.proto */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.protobuf; path = wire_format.proto; sourceTree = ""; }; EB6F62D62D91F4AC00534EC2 /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = ""; }; EB7CD1442D6FB0C100CB0CB5 /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = ""; }; EB7F540F2D78E6DD00989564 /* Sidebar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Sidebar.swift; sourceTree = ""; }; @@ -341,6 +347,7 @@ isa = PBXGroup; children = ( EB2B41E32DC63AC80070FC39 /* SleepManager.swift */, + EB3B73F92DC8FF3A00369951 /* ProtobufSource */, 69DA9A2729E18CB500A442DA /* Protobuf */, 69DA9A1E29E0C0B300A442DA /* NearbyConnectionManager.swift */, 69DA9A2229E17F0400A442DA /* InboundNearbyConnection.swift */, @@ -352,6 +359,19 @@ path = NearbyShare; sourceTree = ""; }; + EB3B73F92DC8FF3A00369951 /* ProtobufSource */ = { + isa = PBXGroup; + children = ( + EB3B73F32DC8FF3A00369951 /* device_to_device_messages.proto */, + EB3B73F42DC8FF3A00369951 /* offline_wire_formats.proto */, + EB3B73F52DC8FF3A00369951 /* securegcm.proto */, + EB3B73F62DC8FF3A00369951 /* securemessage.proto */, + EB3B73F72DC8FF3A00369951 /* ukey.proto */, + EB3B73F82DC8FF3A00369951 /* wire_format.proto */, + ); + path = ProtobufSource; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXHeadersBuildPhase section */ @@ -789,7 +809,7 @@ MTL_FAST_MATH = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = macosx; - SHARED_BUILD = 31; + SHARED_BUILD = 32; SHARED_VERSION = 1.7.3; SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; @@ -848,7 +868,7 @@ MTL_ENABLE_DEBUG_INFO = NO; MTL_FAST_MATH = YES; SDKROOT = macosx; - SHARED_BUILD = 31; + SHARED_BUILD = 32; SHARED_VERSION = 1.7.3; SWIFT_COMPILATION_MODE = wholemodule; SWIFT_OPTIMIZATION_LEVEL = "-O";