Skip to content

Commit

Permalink
Add descriptions to requiredFieldMissing and be more lenient
Browse files Browse the repository at this point in the history
fixes #156, fixes #159
  • Loading branch information
grishka committed May 22, 2024
1 parent 08e5faf commit 10d63f1
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 27 deletions.
8 changes: 4 additions & 4 deletions NearDrop.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@
CODE_SIGN_ENTITLEMENTS = NearDrop/NearDrop.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 6;
CURRENT_PROJECT_VERSION = 7;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.utilities";
INFOPLIST_KEY_LSUIElement = YES;
Expand All @@ -835,7 +835,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 11.0;
MARKETING_VERSION = 2.0.2;
MARKETING_VERSION = 2.0.3;
PRODUCT_BUNDLE_IDENTIFIER = me.grishka.NearDrop;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand All @@ -855,7 +855,7 @@
CODE_SIGN_ENTITLEMENTS = NearDrop/NearDrop.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 6;
CURRENT_PROJECT_VERSION = 7;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.utilities";
INFOPLIST_KEY_LSUIElement = YES;
Expand All @@ -867,7 +867,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 11.0;
MARKETING_VERSION = 2.0.2;
MARKETING_VERSION = 2.0.3;
PRODUCT_BUNDLE_IDENTIFIER = me.grishka.NearDrop;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"originHash" : "5bbb4a57a61db67d6ce8adc203fbc456a84ce246fff6c01807617c5de6a47adc",
"pins" : [
{
"identity" : "asn1",
Expand Down Expand Up @@ -38,5 +37,5 @@
}
}
],
"version" : 3
"version" : 2
}
16 changes: 8 additions & 8 deletions NearbyShare/InboundNearbyConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class InboundNearbyConnection: NearbyConnection{
}

private func processConnectionRequestFrame(_ frame:Location_Nearby_Connections_OfflineFrame) throws{
guard frame.hasV1 && frame.v1.hasConnectionRequest && frame.v1.connectionRequest.hasEndpointInfo else { throw NearbyError.requiredFieldMissing }
guard frame.hasV1 && frame.v1.hasConnectionRequest && frame.v1.connectionRequest.hasEndpointInfo else { throw NearbyError.requiredFieldMissing("connectionRequest.endpointInfo") }
guard case .connectionRequest = frame.v1.type else { throw NearbyError.protocolError("Unexpected frame type \(frame.v1.type)") }
let endpointInfo=frame.v1.connectionRequest.endpointInfo
guard endpointInfo.count>17 else { throw NearbyError.protocolError("Endpoint info too short") }
Expand All @@ -142,7 +142,7 @@ class InboundNearbyConnection: NearbyConnection{
}

private func processUkey2ClientInit(_ msg:Securegcm_Ukey2Message) throws{
guard msg.hasMessageType, msg.hasMessageData else { throw NearbyError.requiredFieldMissing }
guard msg.hasMessageType, msg.hasMessageData else { throw NearbyError.requiredFieldMissing("clientInit ukey2message.type|data") }
guard case .clientInit = msg.messageType else{
sendUkey2Alert(type: .badMessageType)
throw NearbyError.ukey2
Expand Down Expand Up @@ -206,15 +206,15 @@ class InboundNearbyConnection: NearbyConnection{
}

private func processUkey2ClientFinish(_ msg:Securegcm_Ukey2Message, raw:Data) throws{
guard msg.hasMessageType, msg.hasMessageData else { throw NearbyError.requiredFieldMissing }
guard msg.hasMessageType, msg.hasMessageData else { throw NearbyError.requiredFieldMissing("clientFinish ukey2message.type|data") }
guard case .clientFinish = msg.messageType else { throw NearbyError.ukey2 }

var sha=SHA512()
sha.update(data: raw)
guard cipherCommitment==Data(sha.finalize()) else { throw NearbyError.ukey2 }

let clientFinish=try Securegcm_Ukey2ClientFinished(serializedData: msg.messageData)
guard clientFinish.hasPublicKey else {throw NearbyError.requiredFieldMissing }
guard clientFinish.hasPublicKey else {throw NearbyError.requiredFieldMissing("ukey2clientFinish.publicKey") }
let clientKey=try Securemessage_GenericPublicKey(serializedData: clientFinish.publicKey)

try finalizeKeyExchange(peerKey: clientKey)
Expand All @@ -223,7 +223,7 @@ class InboundNearbyConnection: NearbyConnection{
}

private func processConnectionResponseFrame(_ frame:Location_Nearby_Connections_OfflineFrame) throws{
guard frame.hasV1, frame.v1.hasType else { throw NearbyError.requiredFieldMissing }
guard frame.hasV1, frame.v1.hasType else { throw NearbyError.requiredFieldMissing("offlineFrame.v1.type") }
if case .connectionResponse = frame.v1.type {
var resp=Location_Nearby_Connections_OfflineFrame()
resp.version = .v1
Expand Down Expand Up @@ -254,7 +254,7 @@ class InboundNearbyConnection: NearbyConnection{
}

private func processPairedKeyEncryptionFrame(_ frame:Sharing_Nearby_Frame) throws{
guard frame.hasV1, frame.v1.hasPairedKeyEncryption else { throw NearbyError.requiredFieldMissing }
guard frame.hasV1, frame.v1.hasPairedKeyEncryption else { throw NearbyError.requiredFieldMissing("shareNearbyFrame.v1.pairedKeyEncryption") }
var pairedResult=Sharing_Nearby_Frame()
pairedResult.version = .v1
pairedResult.v1=Sharing_Nearby_V1Frame()
Expand All @@ -266,12 +266,12 @@ class InboundNearbyConnection: NearbyConnection{
}

private func processPairedKeyResultFrame(_ frame:Sharing_Nearby_Frame) throws{
guard frame.hasV1, frame.v1.hasPairedKeyResult else { throw NearbyError.requiredFieldMissing }
guard frame.hasV1, frame.v1.hasPairedKeyResult else { throw NearbyError.requiredFieldMissing("shareNearbyFrame.v1.pairedKeyResult") }
currentState = .receivedPairedKeyResult
}

private func processIntroductionFrame(_ frame:Sharing_Nearby_Frame) throws{
guard frame.hasV1, frame.v1.hasIntroduction else { throw NearbyError.requiredFieldMissing }
guard frame.hasV1, frame.v1.hasIntroduction else { throw NearbyError.requiredFieldMissing("shareNearbyFrame.v1.introduction") }
currentState = .waitingForUserConsent
if frame.v1.introduction.fileMetadata.count>0 && frame.v1.introduction.textMetadata.isEmpty{
let downloadsDirectory=(try FileManager.default.url(for: .downloadsDirectory, in: .userDomainMask, appropriateFor: nil, create: true)).resolvingSymlinksInPath()
Expand Down
17 changes: 8 additions & 9 deletions NearbyShare/NearbyConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ class NearbyConnection{
}

internal func decryptAndProcessReceivedSecureMessage(_ smsg:Securemessage_SecureMessage) throws{
guard smsg.hasSignature, smsg.hasHeaderAndBody else { throw NearbyError.requiredFieldMissing }
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") }
let headerAndBody=try Securemessage_HeaderAndBody(serializedData: smsg.headerAndBody)
Expand All @@ -254,19 +254,18 @@ class NearbyConnection{
})
decryptedData=decryptedData.prefix(decryptedLength)
let d2dMsg=try Securegcm_DeviceToDeviceMessage(serializedData: decryptedData)
guard d2dMsg.hasMessage, d2dMsg.hasSequenceNumber else { throw NearbyError.requiredFieldMissing }
guard d2dMsg.hasMessage, d2dMsg.hasSequenceNumber else { throw NearbyError.requiredFieldMissing("d2dMessage.message|sequenceNumber") }
clientSeq+=1
guard d2dMsg.sequenceNumber==clientSeq else { throw NearbyError.protocolError("Wrong sequence number. Expected \(clientSeq), got \(d2dMsg.sequenceNumber)") }
let offlineFrame=try Location_Nearby_Connections_OfflineFrame(serializedData: d2dMsg.message)
guard offlineFrame.hasV1, offlineFrame.v1.hasType else { throw NearbyError.requiredFieldMissing }

if case .payloadTransfer = offlineFrame.v1.type {
guard offlineFrame.v1.hasPayloadTransfer else { throw NearbyError.requiredFieldMissing }
if offlineFrame.hasV1 && offlineFrame.v1.hasType, case .payloadTransfer = offlineFrame.v1.type {
guard offlineFrame.v1.hasPayloadTransfer else { throw NearbyError.requiredFieldMissing("offlineFrame.v1.payloadTransfer") }
let payloadTransfer=offlineFrame.v1.payloadTransfer
let header=payloadTransfer.payloadHeader;
let chunk=payloadTransfer.payloadChunk;
guard header.hasType, header.hasID else { throw NearbyError.requiredFieldMissing }
guard payloadTransfer.hasPayloadChunk, chunk.hasOffset, chunk.hasFlags else { throw NearbyError.requiredFieldMissing }
guard header.hasType, header.hasID else { throw NearbyError.requiredFieldMissing("payloadHeader.type|id") }
guard payloadTransfer.hasPayloadChunk, chunk.hasOffset, chunk.hasFlags else { throw NearbyError.requiredFieldMissing("payloadTransfer.payloadChunk|offset|flags") }
if case .bytes = header.type{
let payloadID=header.id
if header.totalSize>InboundNearbyConnection.SANE_FRAME_LENGTH{
Expand Down Expand Up @@ -294,7 +293,7 @@ class NearbyConnection{
}else if case .file = header.type{
try processFileChunk(frame: payloadTransfer)
}
}else if case .keepAlive = offlineFrame.v1.type{
}else if offlineFrame.hasV1 && offlineFrame.v1.hasType, case .keepAlive = offlineFrame.v1.type{
#if DEBUG
print("Sent keep-alive")
#endif
Expand All @@ -321,7 +320,7 @@ class NearbyConnection{
}

internal func finalizeKeyExchange(peerKey:Securemessage_GenericPublicKey) throws{
guard peerKey.hasEcP256PublicKey else { throw NearbyError.requiredFieldMissing }
guard peerKey.hasEcP256PublicKey else { throw NearbyError.requiredFieldMissing("peerKey.ecP256PublicKey") }

let domain=Domain.instance(curve: .EC256r1)
var clientX=peerKey.ecP256PublicKey.x
Expand Down
2 changes: 1 addition & 1 deletion NearbyShare/NearbyConnectionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public struct RemoteDeviceInfo{

public enum NearbyError:Error{
case protocolError(_ message:String)
case requiredFieldMissing
case requiredFieldMissing(_ message:String)
case ukey2
case inputOutput(cause:Errno)
case canceled(reason:CancellationReason)
Expand Down
6 changes: 3 additions & 3 deletions NearbyShare/OutboundNearbyConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ class OutboundNearbyConnection:NearbyConnection{
}

private func processPairedKeyEncryption(frame:Sharing_Nearby_Frame) throws{
guard frame.hasV1, frame.v1.hasPairedKeyEncryption else { throw NearbyError.requiredFieldMissing }
guard frame.hasV1, frame.v1.hasPairedKeyEncryption else { throw NearbyError.requiredFieldMissing("sharingNearbyFrame.v1.pairedKeyEncryption") }
var pairedResult=Sharing_Nearby_Frame()
pairedResult.version = .v1
pairedResult.v1=Sharing_Nearby_V1Frame()
Expand All @@ -255,7 +255,7 @@ class OutboundNearbyConnection:NearbyConnection{
}

private func processPairedKeyResult(frame:Sharing_Nearby_Frame) throws{
guard frame.hasV1, frame.v1.hasPairedKeyResult else { throw NearbyError.requiredFieldMissing }
guard frame.hasV1, frame.v1.hasPairedKeyResult else { throw NearbyError.requiredFieldMissing("sharingNearbyFrame.v1.pairedKeyResult") }

var introduction=Sharing_Nearby_Frame()
introduction.version = .v1
Expand Down Expand Up @@ -308,7 +308,7 @@ class OutboundNearbyConnection:NearbyConnection{
}

private func processConsent(frame:Sharing_Nearby_Frame) throws{
guard frame.version == .v1, frame.v1.type == .response else {throw NearbyError.requiredFieldMissing}
guard frame.version == .v1, frame.v1.type == .response else {throw NearbyError.requiredFieldMissing("sharingNearbyFrame.v1.type==response")}
switch frame.v1.connectionResponse.status{
case .accept:
currentState = .sendingFiles
Expand Down

0 comments on commit 10d63f1

Please sign in to comment.