mirror of
https://github.com/leonboe1/QuickDrop.git
synced 2026-11-21 05:46:03 +00:00
Add Trusted Devices Authentication
This commit is contained in:
1 parent
78df7fe227
commit
48164b8095
14 files changed
+621
-70
No files matched your search
@@ -26,6 +26,8 @@ class InboundNearbyConnection: NearbyConnection {
|
||||
private var cipherCommitment: Data?
|
||||
private var textPayloadID: Int64 = 0
|
||||
private var bytesToBeTransferred: Int64 = 0
|
||||
private var isAuthenticated = false
|
||||
private var peerCertificate: Sharing_Nearby_PublicCertificate? = nil
|
||||
var isPlainTextTransfer = false
|
||||
|
||||
var wasUserRejected = false
|
||||
@@ -371,9 +373,11 @@ class InboundNearbyConnection: NearbyConnection {
|
||||
pairedEncryption.v1 = Sharing_Nearby_V1Frame()
|
||||
pairedEncryption.v1.type = .pairedKeyEncryption
|
||||
pairedEncryption.v1.pairedKeyEncryption = Sharing_Nearby_PairedKeyEncryptionFrame()
|
||||
// Presumably used for all the phone number stuff that no one needs anyway
|
||||
|
||||
// For inbound connections, we do not use provide secretIDHash and signedData
|
||||
pairedEncryption.v1.pairedKeyEncryption.secretIDHash = Data.randomData(length: 6)
|
||||
pairedEncryption.v1.pairedKeyEncryption.signedData = Data.randomData(length: 72)
|
||||
|
||||
try sendTransferSetupFrame(pairedEncryption)
|
||||
currentState = .sentConnectionResponse
|
||||
} else {
|
||||
@@ -381,16 +385,77 @@ class InboundNearbyConnection: NearbyConnection {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private func processPairedKeyEncryptionFrame(_ frame: Sharing_Nearby_Frame) throws {
|
||||
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()
|
||||
pairedResult.v1.type = .pairedKeyResult
|
||||
pairedResult.v1.pairedKeyResult = Sharing_Nearby_PairedKeyResultFrame()
|
||||
pairedResult.v1.pairedKeyResult.status = .unable
|
||||
try sendTransferSetupFrame(pairedResult)
|
||||
let pkeFrame = frame.v1.pairedKeyEncryption
|
||||
|
||||
log("[InboundNearbyConnection \(self.id)] Processing paired key encryption frame with secretIDHash: \(pkeFrame.secretIDHash.hex)")
|
||||
|
||||
log("Signed data: " + pkeFrame.signedData.hex)
|
||||
|
||||
// Check if we can accept the connection automatically
|
||||
if !pkeFrame.secretIDHash.isEmpty,
|
||||
let trustedCertData = TrustStore.shared.findTrustedKey(for: pkeFrame.secretIDHash),
|
||||
let trustedCert = try? Sharing_Nearby_PublicCertificate(serializedBytes: trustedCertData),
|
||||
let peerGenericKey = try? Securemessage_GenericPublicKey(serializedBytes: trustedCert.publicKey),
|
||||
let authKeyData = self.authKey?.data() {
|
||||
|
||||
log("[InboundNearbyConnection \(self.id)] Found trusted certificate for secretIDHash: \(pkeFrame.secretIDHash.hex)")
|
||||
|
||||
let domain = Domain.instance(curve: .EC256r1)
|
||||
let ecKey = peerGenericKey.ecP256PublicKey
|
||||
let point = Point(BInt(magnitude: [UInt8](ecKey.x)), BInt(magnitude: [UInt8](ecKey.y)))
|
||||
|
||||
guard let peerPublicKey = try? ECPublicKey(domain: domain, w: point) else {
|
||||
log("[InboundNearbyConnection \(self.id)] Paired key auth failed: Invalid peer public key.")
|
||||
try automaticAuthFailed()
|
||||
return
|
||||
}
|
||||
|
||||
guard pkeFrame.signedData.count == 72 else {
|
||||
log("[InboundNearbyConnection \(self.id)] Paired key auth failed: Invalid signature length.")
|
||||
try automaticAuthFailed()
|
||||
return
|
||||
}
|
||||
|
||||
// Strip header
|
||||
let signedData = pkeFrame.signedData.subdata(in: 8..<72)
|
||||
|
||||
let r = Array(signedData.prefix(32))
|
||||
let s = Array(signedData.suffix(32))
|
||||
|
||||
let signature = ECSignature(domain: domain, r: r, s: s)
|
||||
|
||||
if peerPublicKey.verify(signature: signature, msg: authKeyData) {
|
||||
log("[InboundNearbyConnection \(self.id)] Paired key authentication successful.")
|
||||
try sendPairedKeyResult(status: .success)
|
||||
isAuthenticated = true
|
||||
} else {
|
||||
log("[InboundNearbyConnection \(self.id)] Paired key auth failed: Signature verification failed.")
|
||||
try automaticAuthFailed()
|
||||
}
|
||||
} else {
|
||||
try automaticAuthFailed()
|
||||
}
|
||||
|
||||
func automaticAuthFailed() throws {
|
||||
// Store certificate of peer to let user device later to trust it
|
||||
if let peerCertificate = frame.v1.certificateInfo.publicCertificate.first {
|
||||
log("[InboundNearbyConnection \(self.id)] Storing peer certificate for potential later use")
|
||||
self.peerCertificate = peerCertificate
|
||||
}
|
||||
|
||||
try sendPairedKeyResult(status: .unable)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private func sendPairedKeyResult(status: Sharing_Nearby_PairedKeyResultFrame.Status) throws {
|
||||
var resultFrame = Sharing_Nearby_Frame()
|
||||
resultFrame.version = .v1
|
||||
resultFrame.v1.type = .pairedKeyResult
|
||||
resultFrame.v1.pairedKeyResult.status = status
|
||||
try sendTransferSetupFrame(resultFrame)
|
||||
currentState = .sentPairedKeyResult
|
||||
}
|
||||
|
||||
@@ -436,10 +501,8 @@ class InboundNearbyConnection: NearbyConnection {
|
||||
filesToBeReceived[file.payloadID] = info
|
||||
bytesToBeTransferred += file.size
|
||||
}
|
||||
let metadata = TransferMetadata(files: filesToBeReceived.map { $0.value.meta }, id: id, pinCode: pinCode)
|
||||
DispatchQueue.main.async {
|
||||
self.delegate?.obtainUserConsent(transfer: metadata, device: self.remoteDeviceInfo!, connection: self)
|
||||
}
|
||||
let metadata = TransferMetadata(files: filesToBeReceived.map { $0.value.meta }, id: id, pinCode: pinCode, allowsToBeAddedAsTrustedDevice: self.peerCertificate != nil)
|
||||
checkIfCanProceed(metadata: metadata)
|
||||
}
|
||||
else if let textMetadata = frame.v1.introduction.textMetadata.first {
|
||||
|
||||
@@ -447,16 +510,14 @@ class InboundNearbyConnection: NearbyConnection {
|
||||
|
||||
let isClipboardText = textMetadata.type == .text
|
||||
|
||||
let metadata = TransferMetadata(files: [], id: id, pinCode: pinCode, textDescription: textMetadata.textTitle, transferType: isClipboardText ? .text : .url)
|
||||
let metadata = TransferMetadata(files: [], id: id, pinCode: pinCode, textDescription: textMetadata.textTitle, transferType: isClipboardText ? .text : .url, allowsToBeAddedAsTrustedDevice: self.peerCertificate != nil)
|
||||
textPayloadID = textMetadata.payloadID
|
||||
|
||||
if isClipboardText {
|
||||
isPlainTextTransfer = true
|
||||
}
|
||||
|
||||
DispatchQueue.main.async {
|
||||
self.delegate?.obtainUserConsent(transfer: metadata, device: self.remoteDeviceInfo!, connection: self)
|
||||
}
|
||||
checkIfCanProceed(metadata: metadata)
|
||||
}
|
||||
else {
|
||||
rejectDueToUnsupportedFileType(frame)
|
||||
@@ -464,6 +525,16 @@ class InboundNearbyConnection: NearbyConnection {
|
||||
} else {
|
||||
rejectDueToUnsupportedFileType(frame)
|
||||
}
|
||||
|
||||
|
||||
func checkIfCanProceed(metadata: TransferMetadata) {
|
||||
|
||||
let acceptAutomatically = isAuthenticated || UserDefaults.standard.bool(forKey: UserDefaultsKeys.automaticallyAcceptFiles.rawValue)
|
||||
|
||||
DispatchQueue.main.async {
|
||||
self.delegate?.obtainUserConsent(transfer: metadata, device: self.remoteDeviceInfo!, connection: self, acceptAutomatically: acceptAutomatically)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -476,7 +547,12 @@ class InboundNearbyConnection: NearbyConnection {
|
||||
}
|
||||
|
||||
|
||||
func submitUserConsent(accepted: Bool, storeInTemp: Bool = false) {
|
||||
func submitUserConsent(accepted: Bool, trustDevice: Bool, storeInTemp: Bool = false) {
|
||||
|
||||
if trustDevice, let peerCertificate = peerCertificate {
|
||||
TrustStore.shared.addTrusted(certificate: peerCertificate)
|
||||
}
|
||||
|
||||
DispatchQueue.global(qos: .utility).async {
|
||||
if accepted {
|
||||
self.acceptTransfer(storeInTemp: storeInTemp)
|
||||
|
||||
@@ -154,8 +154,8 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
|
||||
}
|
||||
|
||||
|
||||
func obtainUserConsent(transfer: TransferMetadata, device: RemoteDeviceInfo, connection _: InboundNearbyConnection) {
|
||||
mainAppDelegate?.obtainUserConsent(transfer: transfer, device: device)
|
||||
func obtainUserConsent(transfer: TransferMetadata, device: RemoteDeviceInfo, connection _: InboundNearbyConnection, acceptAutomatically: Bool) {
|
||||
mainAppDelegate?.obtainUserConsent(transfer: transfer, device: device, acceptAutomatically: acceptAutomatically)
|
||||
}
|
||||
|
||||
|
||||
@@ -173,11 +173,11 @@ public class NearbyConnectionManager: NSObject, NetServiceDelegate, InboundNearb
|
||||
}
|
||||
|
||||
|
||||
public func submitUserConsent(transferID: String, accept: Bool, storeInTemp: Bool = false) {
|
||||
public func submitUserConsent(transferID: String, accept: Bool, trustDevice: Bool, storeInTemp: Bool = false) {
|
||||
guard let conn = incomingConnections[transferID] else { return }
|
||||
|
||||
log("[NearbyConnectionManager] Submitting user consent for transfer \(transferID), accepted: \(accept), store in temp: \(storeInTemp)")
|
||||
conn.submitUserConsent(accepted: accept, storeInTemp: storeInTemp)
|
||||
conn.submitUserConsent(accepted: accept, trustDevice: trustDevice, storeInTemp: storeInTemp)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -271,11 +271,40 @@ class OutboundNearbyConnection: NearbyConnection {
|
||||
|
||||
var pairedEncryption = Sharing_Nearby_Frame()
|
||||
pairedEncryption.version = .v1
|
||||
pairedEncryption.v1 = Sharing_Nearby_V1Frame()
|
||||
pairedEncryption.v1.type = .pairedKeyEncryption
|
||||
pairedEncryption.v1.pairedKeyEncryption = Sharing_Nearby_PairedKeyEncryptionFrame()
|
||||
pairedEncryption.v1.pairedKeyEncryption.secretIDHash = Data.randomData(length: 6)
|
||||
pairedEncryption.v1.pairedKeyEncryption.signedData = Data.randomData(length: 72)
|
||||
|
||||
// add public key
|
||||
var cert = Sharing_Nearby_PublicCertificate()
|
||||
if let signingPrivateKey = IdentityManager.shared.getPrivateKey(),
|
||||
let publicKey = IdentityManager.shared.getPublicKey()?.toGenericPublicKey(),
|
||||
let publicKeyData = IdentityManager.shared.getPublicKey()?.toGenericPublicKeyData(),
|
||||
let publicKeyId = publicKey.id(),
|
||||
let authKeyData = self.authKey?.data() {
|
||||
|
||||
log("[OutboundNearbyConnection \(self.id)] Using private key for signing")
|
||||
|
||||
cert.secretID = publicKeyId
|
||||
cert.publicKey = publicKeyData
|
||||
|
||||
let signatureTuple = signingPrivateKey.sign(msg: authKeyData)
|
||||
|
||||
let rBytes = signatureTuple.r
|
||||
let sBytes = signatureTuple.s
|
||||
|
||||
// Make sure r and s are 32 bytes each by padding with leading zeros if necessary
|
||||
let paddedR = Data(repeating: 0, count: 32 - rBytes.count) + rBytes
|
||||
let paddedS = Data(repeating: 0, count: 32 - sBytes.count) + sBytes
|
||||
|
||||
// Signature: Header (8 bytes) + r (32 bytes) + s (32 bytes)
|
||||
let signatureData = Data(repeating: 0, count: 8) + paddedR + paddedS
|
||||
|
||||
pairedEncryption.v1.certificateInfo.publicCertificate.append(cert)
|
||||
pairedEncryption.v1.pairedKeyEncryption.signedData = signatureData
|
||||
pairedEncryption.v1.pairedKeyEncryption.secretIDHash = cert.secretID
|
||||
|
||||
pairedEncryption.v1.pairedKeyResult.status = .success
|
||||
}
|
||||
|
||||
|
||||
if let qrKey = qrCodePrivateKey, let authKey = authKey {
|
||||
let signature = qrKey.sign(msg: authKey.data())
|
||||
@@ -298,6 +327,7 @@ class OutboundNearbyConnection: NearbyConnection {
|
||||
pairedResult.v1.type = .pairedKeyResult
|
||||
pairedResult.v1.pairedKeyResult = Sharing_Nearby_PairedKeyResultFrame()
|
||||
pairedResult.v1.pairedKeyResult.status = .unable
|
||||
|
||||
try sendTransferSetupFrame(pairedResult)
|
||||
currentState = .sentPairedKeyResult
|
||||
}
|
||||
@@ -375,8 +405,7 @@ class OutboundNearbyConnection: NearbyConnection {
|
||||
|
||||
if let textToSend = textToSend {
|
||||
try sendText(text: textToSend)
|
||||
}
|
||||
if hasURL() {
|
||||
} else if hasURL() {
|
||||
try sendURL()
|
||||
} else {
|
||||
try sendNextFileChunk()
|
||||
|
||||
Reference in new issue
Block a user