Skip to content

Commit

Permalink
test removal of secure enclave check is ok for simulators
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmcl committed Oct 24, 2023
1 parent be83bd9 commit 43196d6
Showing 1 changed file with 8 additions and 23 deletions.
31 changes: 8 additions & 23 deletions Sources/KukaiCoreSwift/Services/WalletCacheService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,6 @@ extension WalletCacheService {
}



/// Else create the real keys
do {
if let key = try loadKey() {
Expand Down Expand Up @@ -727,7 +726,7 @@ extension WalletCacheService {
fileprivate func createKeys() throws -> (public: SecKey, private: SecKey?) {
var error: Unmanaged<CFError>?

let privateKeyAccessControl: SecAccessControlCreateFlags = CurrentDevice.hasSecureEnclave ? [.privateKeyUsage] : []
let privateKeyAccessControl: SecAccessControlCreateFlags = [.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 @@ -742,27 +741,20 @@ extension WalletCacheService {
let context = LAContext()
context.interactionNotAllowed = false

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

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

if CurrentDevice.hasSecureEnclave {
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 @@ -827,17 +819,10 @@ extension WalletCacheService {
kSecClass as String: kSecClassKey,
kSecAttrApplicationTag as String: WalletCacheService.applicationKey,
kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom,
kSecReturnRef as String: true
kSecReturnRef as String: true,
kSecAttrTokenID as String: kSecAttrTokenIDSecureEnclave
]

if CurrentDevice.hasSecureEnclave {
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

0 comments on commit 43196d6

Please sign in to comment.