Skip to content

Commit

Permalink
revert to optional secure enclave usage, but instead only trigger bas…
Browse files Browse the repository at this point in the history
…ed on simulator or not
  • Loading branch information
simonmcl committed Oct 24, 2023
1 parent 43196d6 commit 0006374
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
5 changes: 0 additions & 5 deletions Sources/KukaiCoreSwift/Models/CurrentDevice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ public enum BiometricType {
/// Enum used to get details about the current device's capabilities
public enum CurrentDevice {

/// Does the current device have a secure enclave
public static var hasSecureEnclave: Bool {
return !isSimulator && biometricTypeSupported() != .none
}

/// Is the current device a simulator
public static var isSimulator: Bool {
return TARGET_OS_SIMULATOR == 1
Expand Down
33 changes: 25 additions & 8 deletions Sources/KukaiCoreSwift/Services/WalletCacheService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,8 @@ extension WalletCacheService {
fileprivate func createKeys() throws -> (public: SecKey, private: SecKey?) {
var error: Unmanaged<CFError>?

let privateKeyAccessControl: SecAccessControlCreateFlags = [.privateKeyUsage]
// If not simulator, use secure encalve
let privateKeyAccessControl: SecAccessControlCreateFlags = !CurrentDevice.isSimulator ? [.privateKeyUsage] : []
guard let privateKeyAccess = SecAccessControlCreateWithFlags(kCFAllocatorDefault, kSecAttrAccessibleWhenUnlockedThisDeviceOnly, privateKeyAccessControl, &error) else {
if let err = error {
os_log(.error, log: .walletCache, "createKeys - createWithFlags returned error")
Expand All @@ -741,20 +742,28 @@ extension WalletCacheService {
let context = LAContext()
context.interactionNotAllowed = false

let privateKeyAttributes: [String: Any] = [
var privateKeyAttributes: [String: Any] = [
kSecAttrApplicationTag as String: WalletCacheService.applicationKey,
kSecAttrIsPermanent as String: true,
kSecUseAuthenticationContext as String: context,
kSecAttrAccessControl as String: privateKeyAccessControl
kSecAttrAccessControl as String: privateKeyAccess
]

let commonKeyAttributes: [String: Any] = [
kSecAttrTokenID as String: kSecAttrTokenIDSecureEnclave,
var commonKeyAttributes: [String: Any] = [
kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom,
kSecAttrKeySizeInBits as String: 256,
kSecPrivateKeyAttrs as String: privateKeyAttributes
]

// If not simulator, use secure encalve
if !CurrentDevice.isSimulator {
os_log(.default, log: .keychain, "createKeys - Using secure enclave")
commonKeyAttributes[kSecAttrTokenID as String] = kSecAttrTokenIDSecureEnclave
commonKeyAttributes[kSecPrivateKeyAttrs as String] = privateKeyAttributes
privateKeyAttributes[kSecAttrAccessControl as String] = privateKeyAccessControl
} else {
os_log(.default, log: .keychain, "createKeys - unable to use secure enclave")
}

guard let privateKey = SecKeyCreateRandomKey(commonKeyAttributes as CFDictionary, &error) else {
if let err = error {
os_log(.default, log: .keychain, "createKeys - createRandom returned error")
Expand Down Expand Up @@ -819,10 +828,18 @@ extension WalletCacheService {
kSecClass as String: kSecClassKey,
kSecAttrApplicationTag as String: WalletCacheService.applicationKey,
kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom,
kSecReturnRef as String: true,
kSecAttrTokenID as String: kSecAttrTokenIDSecureEnclave
kSecReturnRef as String: true
]

// If not simulator, use secure encalve
if !CurrentDevice.isSimulator {
os_log(.default, log: .walletCache, "loadKey - Using secure enclave")
query[kSecAttrTokenID as String] = kSecAttrTokenIDSecureEnclave

} else {
os_log(.default, log: .walletCache, "loadKey - unable to use secure enclave")
}

var key: CFTypeRef?
if SecItemCopyMatching(query as CFDictionary, &key) == errSecSuccess {
os_log(.default, log: .walletCache, "loadKey - returning key")
Expand Down
1 change: 0 additions & 1 deletion Tests/KukaiCoreSwiftTests/Models/CurrentDeviceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class CurrentDeviceTests: XCTestCase {
}

func testCurrentDevice() {
XCTAssert(CurrentDevice.hasSecureEnclave == false)
XCTAssert(CurrentDevice.isSimulator == true)
}

Expand Down

0 comments on commit 0006374

Please sign in to comment.