mirror of
https://github.com/leonboe1/QuickDrop.git
synced 2026-11-22 14:26:01 +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
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// AppGroup.swift
|
||||
// QuickDrop
|
||||
//
|
||||
// Created by Leon Böttger on 23.08.25.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
/// Contains information about the app group used to share data between main app and widget
|
||||
struct AppGroup {
|
||||
|
||||
/// Identifier of the app group
|
||||
static let appGroupName = "group.com.leonboettger.neardrop"
|
||||
static let appGroupUD = UserDefaults(suiteName: AppGroup.appGroupName)!
|
||||
|
||||
/// Returns the shared App Group directory
|
||||
static var appGroupDirectory: URL? {
|
||||
return FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroupName)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
//
|
||||
// IdentityManager.swift
|
||||
// QuickDrop
|
||||
//
|
||||
// Created by Leon Böttger on 22.08.25.
|
||||
//
|
||||
|
||||
|
||||
import Foundation
|
||||
import SwiftECC
|
||||
import Security
|
||||
import CryptoKit
|
||||
|
||||
class IdentityManager {
|
||||
|
||||
static let shared = IdentityManager()
|
||||
|
||||
private let signingKeyTag = "com.leonboettger.quickdrop.identity.signingkey"
|
||||
private var signingPrivateKey: ECPrivateKey?
|
||||
private var signingPublicKey: ECPublicKey?
|
||||
|
||||
private init() {
|
||||
|
||||
if let (privateKey, publicKey) = loadKeys() {
|
||||
|
||||
log("[IdentityManager] Loaded existing private key.")
|
||||
|
||||
self.signingPrivateKey = privateKey
|
||||
self.signingPublicKey = publicKey
|
||||
}
|
||||
else {
|
||||
log("[IdentityManager] No existing private key found, generating a new one.")
|
||||
let (newPrivate, newPublic) = generateAndStoreKeyPair()
|
||||
self.signingPrivateKey = newPrivate
|
||||
self.signingPublicKey = newPublic
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func getPrivateKey() -> ECPrivateKey? {
|
||||
return signingPrivateKey
|
||||
}
|
||||
|
||||
|
||||
func getPublicKey() -> ECPublicKey? {
|
||||
return signingPublicKey
|
||||
}
|
||||
|
||||
|
||||
private func generateAndStoreKeyPair() -> (ECPrivateKey, ECPublicKey) {
|
||||
let domain = Domain.instance(curve: .EC256r1)
|
||||
let (pubKey, privKey) = domain.makeKeyPair()
|
||||
|
||||
AppGroup.appGroupUD.set(Data(privKey.s.asSignedBytes()), forKey: signingKeyTag)
|
||||
|
||||
return (privKey, pubKey)
|
||||
}
|
||||
|
||||
|
||||
private func loadKeys() -> (ECPrivateKey, ECPublicKey)? {
|
||||
let keyData = AppGroup.appGroupUD.data(forKey: signingKeyTag)
|
||||
|
||||
guard let keyData = keyData else { return nil }
|
||||
|
||||
let domain = Domain.instance(curve: .EC256r1)
|
||||
guard let privateKey = try? ECPrivateKey(domain: domain, s: .init(signed: Array(keyData))) else {
|
||||
log("[IdentityManager] Failed to reconstruct private key from stored data.")
|
||||
return nil
|
||||
}
|
||||
|
||||
guard let pubPoint = try? domain.multiplyPoint(domain.g, privateKey.s) else {
|
||||
log("[IdentityManager] Failed to derive public key from private key.")
|
||||
return nil
|
||||
}
|
||||
guard let publicKey = try? ECPublicKey(domain: domain, w: pubPoint) else {
|
||||
log("[IdentityManager] Failed to reconstruct public key.")
|
||||
return nil
|
||||
}
|
||||
|
||||
return (privateKey, publicKey)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension ECPublicKey {
|
||||
|
||||
func toGenericPublicKey() -> Securemessage_GenericPublicKey {
|
||||
var genericKey = Securemessage_GenericPublicKey()
|
||||
genericKey.type = .ecP256
|
||||
genericKey.ecP256PublicKey = Securemessage_EcP256PublicKey()
|
||||
genericKey.ecP256PublicKey.x = Data(self.w.x.asSignedBytes())
|
||||
genericKey.ecP256PublicKey.y = Data(self.w.y.asSignedBytes())
|
||||
|
||||
return genericKey
|
||||
}
|
||||
|
||||
|
||||
func toGenericPublicKeyData() -> Data? {
|
||||
let genericKey = self.toGenericPublicKey()
|
||||
return try? genericKey.serializedData()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension Securemessage_GenericPublicKey {
|
||||
|
||||
func id() -> Data? {
|
||||
var hasher = SHA256()
|
||||
|
||||
guard let data = try? self.serializedData() else {
|
||||
return nil
|
||||
}
|
||||
|
||||
hasher.update(data: data)
|
||||
let hash = hasher.finalize()
|
||||
|
||||
return Data(hash).prefix(6)
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,7 @@ final class LogManager {
|
||||
/// Private initializer
|
||||
private init() {
|
||||
// Use the App Group container
|
||||
let directory = getAppGroupDirectory()
|
||||
let directory = AppGroup.appGroupDirectory
|
||||
|
||||
// Create file name from date
|
||||
let formatter = DateFormatter()
|
||||
@@ -89,7 +89,7 @@ final class LogManager {
|
||||
func deleteLogs() {
|
||||
log("[LUI] Deleting all logs...")
|
||||
|
||||
guard let directory = getAppGroupDirectory() else { return }
|
||||
guard let directory = AppGroup.appGroupDirectory else { return }
|
||||
let fileManager = FileManager.default
|
||||
|
||||
guard let allFiles = try? fileManager.contentsOfDirectory(at: directory, includingPropertiesForKeys: nil) else {
|
||||
@@ -124,10 +124,3 @@ public func log(_ text: String) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Returns the shared App Group directory
|
||||
func getAppGroupDirectory() -> URL? {
|
||||
let groupIdentifier = "group.com.leonboettger.neardrop"
|
||||
return FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: groupIdentifier)
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// TrustStore.swift
|
||||
// QuickDrop
|
||||
//
|
||||
// Created by Leon Böttger on 22.08.25.
|
||||
//
|
||||
|
||||
|
||||
import Foundation
|
||||
|
||||
class TrustStore {
|
||||
|
||||
static let shared = TrustStore()
|
||||
private let trustedKeysKey = "com.leonboettger.quickdrop.identity.trustedKeys"
|
||||
|
||||
|
||||
private func getTrustedKeys() -> [String: Data] {
|
||||
return AppGroup.appGroupUD.dictionary(forKey: trustedKeysKey) as? [String: Data] ?? [:]
|
||||
}
|
||||
|
||||
|
||||
func findTrustedKey(for secretIdHash: Data) -> Data? {
|
||||
return getTrustedKeys()[secretIdHash.hex]
|
||||
}
|
||||
|
||||
|
||||
func addTrusted(certificate: Sharing_Nearby_PublicCertificate) {
|
||||
guard let publicKeyData = try? certificate.serializedData() else { return }
|
||||
var keys = getTrustedKeys()
|
||||
keys[certificate.secretID.hex] = publicKeyData
|
||||
AppGroup.appGroupUD.set(keys, forKey: trustedKeysKey)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user