Posts

Post marked as solved
3 Replies
870 Views
Hi! I'm having issue with decrypting Data when the app is in background and iPhone gets Locked by the user. The app works as expected when it's in background and the iPhone is NOT Locked. I'm getting the following error: Unmanaged<CFErrorRef>(_value: Error Domain=NSOSStatusErrorDomain Code=-25308 "setoken: unable to compute shared secret" UserInfo={NSLocalizedDescription=setoken: unable to compute shared secret, AKSError=-536870174}) Here is the code I use to decrypt:      let decryptedData = SecKeyCreateDecryptedData(       try privateSecKey(),       .eciesEncryptionStandardVariableIVX963SHA256AESGCM,       data as CFData,       &error) as Data? and encryption code is:      let encryptedData = SecKeyCreateEncryptedData(       key,       .eciesEncryptionCofactorVariableIVX963SHA256AESGCM,       data as CFData,       &error) as Data? Query private key for descryption:      let query: [String: Any] = [       kSecClass as String: kSecClassKey,       kSecAttrApplicationTag as String: tag,       kSecAttrKeyType as String: kSecAttrKeyTypeEC,       kSecReturnRef as String: true     ]     var item: CFTypeRef?     let status = SecItemCopyMatching(query as CFDictionary, &item) and I create key using      let access = try SecAccessControlCreateWithFlags(       kCFAllocatorDefault,       // Since the app is using the key in the app background status (for example during       // BLE communication), we need a less strict access level.       kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly,       .privateKeyUsage,       nil).unwrap()     let tag = try Constants.privateKeyName.data(using: .utf8).unwrap()     let attributes: [String: Any] = [       kSecAttrKeyType as String: kSecAttrKeyTypeEC,       kSecAttrKeySizeInBits as String: 256,       kSecAttrTokenID as String: kSecAttrTokenIDSecureEnclave,       kSecPrivateKeyAttrs as String: [         kSecAttrIsPermanent as String: true,         kSecAttrApplicationTag as String: tag,         kSecAttrAccessControl as String: access       ]     ]     var error: Unmanaged<CFError>?     guard let privateKey = SecKeyCreateRandomKey(attributes as CFDictionary, &error) else {       let err = try error.unwrap()       throw err.takeRetainedValue() as Error     } The code is compiled as expected and runs normally in foreground and background when iPhone is NOT Locked as mentioned. Based on my research, the issue could be due to kSecAttrAccessControl, but I clearly set it to kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly and the iPhone I test with is Unlocked before I run the test case. It looks as OS bug to me, but I might be missing something here. The test device is iPhone XS MAX. I would appreciate any help. Thanks!
Posted Last updated
.