More logging to fix protobuf wrong error

This commit is contained in:
Leon Böttger committed 2025-05-05 20:59:37 +02:00
1 parent 088a3b5389
commit 58e488c0ad
5 files changed
+93 -19

No files matched your search

+3
View File
@@ -113,6 +113,9 @@ struct LargeAppIconView<Content: View>: View {
print("Clicked \(taps) times")
if taps == 5 {
NearbyConnectionManager.shared.checkSignature = false
sendLoggingString()
print("Copied log to clipboard")
+54 -15
View File
@@ -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..<nextIndex], radix: 16) {
data.append(byte)
} else {
break // or handle invalid hex gracefully
}
index = nextIndex
}
return data
}
}
+6 -2
View File
@@ -233,8 +233,12 @@ class NearbyConnection{
internal func decryptAndProcessReceivedSecureMessage(_ smsg:Securemessage_SecureMessage) throws{
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") }
if NearbyConnectionManager.shared.checkSignature {
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)") }
}
let headerAndBody=try Securemessage_HeaderAndBody(serializedBytes: smsg.headerAndBody)
var decryptedData=Data(count: headerAndBody.body.count)
@@ -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?
+22 -2
View File
@@ -201,6 +201,12 @@
EB2F9BFA2DA04B67004010A2 /* Helpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Helpers.swift; sourceTree = "<group>"; };
EB36435F2D73A29F009CDFF8 /* AudioManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioManager.swift; sourceTree = "<group>"; };
EB3643612D73A2CA009CDFF8 /* timeIsNow.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = timeIsNow.mp3; sourceTree = "<group>"; };
EB3B73F32DC8FF3A00369951 /* device_to_device_messages.proto */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.protobuf; path = device_to_device_messages.proto; sourceTree = "<group>"; };
EB3B73F42DC8FF3A00369951 /* offline_wire_formats.proto */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.protobuf; path = offline_wire_formats.proto; sourceTree = "<group>"; };
EB3B73F52DC8FF3A00369951 /* securegcm.proto */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.protobuf; path = securegcm.proto; sourceTree = "<group>"; };
EB3B73F62DC8FF3A00369951 /* securemessage.proto */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.protobuf; path = securemessage.proto; sourceTree = "<group>"; };
EB3B73F72DC8FF3A00369951 /* ukey.proto */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.protobuf; path = ukey.proto; sourceTree = "<group>"; };
EB3B73F82DC8FF3A00369951 /* wire_format.proto */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.protobuf; path = wire_format.proto; sourceTree = "<group>"; };
EB6F62D62D91F4AC00534EC2 /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; };
EB7CD1442D6FB0C100CB0CB5 /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = "<group>"; };
EB7F540F2D78E6DD00989564 /* Sidebar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Sidebar.swift; sourceTree = "<group>"; };
@@ -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 = "<group>";
};
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 = "<group>";
};
/* 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";