mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 01:25:20 +00:00
Fix crash: Add thread safety to EncryptionService
The crash was caused by concurrent access to CryptoKit signing operations from multiple threads. Added thread-safe access using a concurrent queue with barriers for write operations and sync reads. This fixes the EXC_BAD_ACCESS crash seen in TestFlight build.
This commit is contained in:
@@ -482,6 +482,7 @@
|
||||
DEVELOPMENT_TEAM = L3N5LHJD5Y;
|
||||
ENABLE_PREVIEWS = YES;
|
||||
INFOPLIST_FILE = bitchat/Info.plist;
|
||||
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.social-networking";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
@@ -491,9 +492,12 @@
|
||||
PRODUCT_BUNDLE_IDENTIFIER = chat.bitchat;
|
||||
PRODUCT_NAME = bitchat;
|
||||
SDKROOT = iphoneos;
|
||||
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = YES;
|
||||
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator";
|
||||
SUPPORTS_MACCATALYST = NO;
|
||||
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
|
||||
SUPPORTS_XR_DESIGNED_FOR_IPHONE_IPAD = NO;
|
||||
SWIFT_VERSION = 5.0;
|
||||
TARGETED_DEVICE_FAMILY = "1,2";
|
||||
TARGETED_DEVICE_FAMILY = 1;
|
||||
};
|
||||
name = Debug;
|
||||
};
|
||||
@@ -528,6 +532,7 @@
|
||||
DEVELOPMENT_TEAM = L3N5LHJD5Y;
|
||||
ENABLE_PREVIEWS = YES;
|
||||
INFOPLIST_FILE = bitchat/Info.plist;
|
||||
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.social-networking";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
@@ -537,9 +542,12 @@
|
||||
PRODUCT_BUNDLE_IDENTIFIER = chat.bitchat;
|
||||
PRODUCT_NAME = bitchat;
|
||||
SDKROOT = iphoneos;
|
||||
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = YES;
|
||||
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator";
|
||||
SUPPORTS_MACCATALYST = NO;
|
||||
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
|
||||
SUPPORTS_XR_DESIGNED_FOR_IPHONE_IPAD = NO;
|
||||
SWIFT_VERSION = 5.0;
|
||||
TARGETED_DEVICE_FAMILY = "1,2";
|
||||
TARGETED_DEVICE_FAMILY = 1;
|
||||
};
|
||||
name = Release;
|
||||
};
|
||||
|
||||
@@ -28,6 +28,9 @@ class EncryptionService {
|
||||
private let identityKey: Curve25519.Signing.PrivateKey
|
||||
public let identityPublicKey: Curve25519.Signing.PublicKey
|
||||
|
||||
// Thread safety
|
||||
private let cryptoQueue = DispatchQueue(label: "chat.bitchat.crypto", attributes: .concurrent)
|
||||
|
||||
init() {
|
||||
// Generate ephemeral key pairs for this session
|
||||
self.privateKey = Curve25519.KeyAgreement.PrivateKey()
|
||||
@@ -59,6 +62,7 @@ class EncryptionService {
|
||||
|
||||
// Add peer's combined public keys
|
||||
func addPeerPublicKey(_ peerID: String, publicKeyData: Data) throws {
|
||||
try cryptoQueue.sync(flags: .barrier) {
|
||||
// Convert to array for safe access
|
||||
let keyBytes = [UInt8](publicKeyData)
|
||||
|
||||
@@ -95,11 +99,14 @@ class EncryptionService {
|
||||
sharedSecrets[peerID] = symmetricKey
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get peer's persistent identity key for favorites
|
||||
func getPeerIdentityKey(_ peerID: String) -> Data? {
|
||||
return cryptoQueue.sync {
|
||||
return peerIdentityKeys[peerID]?.rawRepresentation
|
||||
}
|
||||
}
|
||||
|
||||
// Clear persistent identity (for panic mode)
|
||||
func clearPersistentIdentity() {
|
||||
@@ -108,31 +115,42 @@ class EncryptionService {
|
||||
}
|
||||
|
||||
func encrypt(_ data: Data, for peerID: String) throws -> Data {
|
||||
guard let symmetricKey = sharedSecrets[peerID] else {
|
||||
let symmetricKey = try cryptoQueue.sync {
|
||||
guard let key = sharedSecrets[peerID] else {
|
||||
throw EncryptionError.noSharedSecret
|
||||
}
|
||||
return key
|
||||
}
|
||||
|
||||
let sealedBox = try AES.GCM.seal(data, using: symmetricKey)
|
||||
return sealedBox.combined ?? Data()
|
||||
}
|
||||
|
||||
func decrypt(_ data: Data, from peerID: String) throws -> Data {
|
||||
guard let symmetricKey = sharedSecrets[peerID] else {
|
||||
let symmetricKey = try cryptoQueue.sync {
|
||||
guard let key = sharedSecrets[peerID] else {
|
||||
throw EncryptionError.noSharedSecret
|
||||
}
|
||||
return key
|
||||
}
|
||||
|
||||
let sealedBox = try AES.GCM.SealedBox(combined: data)
|
||||
return try AES.GCM.open(sealedBox, using: symmetricKey)
|
||||
}
|
||||
|
||||
func sign(_ data: Data) throws -> Data {
|
||||
return try signingPrivateKey.signature(for: data)
|
||||
// Create a local copy of the key to avoid concurrent access
|
||||
let key = signingPrivateKey
|
||||
return try key.signature(for: data)
|
||||
}
|
||||
|
||||
func verify(_ signature: Data, for data: Data, from peerID: String) throws -> Bool {
|
||||
guard let verifyingKey = peerSigningKeys[peerID] else {
|
||||
let verifyingKey = try cryptoQueue.sync {
|
||||
guard let key = peerSigningKeys[peerID] else {
|
||||
throw EncryptionError.noSharedSecret
|
||||
}
|
||||
return key
|
||||
}
|
||||
|
||||
return verifyingKey.isValidSignature(signature, for: data)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user