Search code examples
swiftsecurityface-id

Secure Enclave, key generation failure


I am new to iOS development, and recently I was trying to build an application, which will create a key inside the secure element, and after - I will sing something with it. While developing I've encountered an issue: the key generation fails if there is a flag .biometryAny or .biometryCurrentSet

The authentication itself is triggered, but the function still throws a mistake.

My setup - Xcode iPhone15 simulator, FaceID enrolled and the animation of it is working.

I've tried deleting the flag, while keeping the manual authorisation, and this approach works, but I still would like have maximum security.

THIS WORKS:

import UIKit
import Security
import LocalAuthentication
import CommonCrypto

func sha256(data: Data) -> Data {
    var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
    data.withUnsafeBytes {
        _ = CC_SHA256($0.baseAddress, CC_LONG(data.count), &hash)
    }
    return Data(hash)
}



class ViewController: UIViewController {
    
    let inputTextField = UITextField()
    let encryptButton = UIButton()
    let outputLabel = UILabel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    func setupUI() {
        // Input TextField setup
        inputTextField.placeholder = "Enter a random number"
        inputTextField.borderStyle = .roundedRect
        inputTextField.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(inputTextField)
        
        // Encrypt Button setup
        encryptButton.setTitle("Encrypt", for: .normal)
        encryptButton.backgroundColor = .blue
        encryptButton.translatesAutoresizingMaskIntoConstraints = false
        encryptButton.addTarget(self, action: #selector(encryptAction), for: .touchUpInside)
        view.addSubview(encryptButton)
        
        // Output Label setup
        outputLabel.text = "Encrypted output will appear here"
        outputLabel.numberOfLines = 0
        outputLabel.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(outputLabel)
        
        NSLayoutConstraint.activate([
            inputTextField.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            inputTextField.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20),
            inputTextField.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.8),
            
            encryptButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            encryptButton.topAnchor.constraint(equalTo: inputTextField.bottomAnchor, constant: 20),
            
            outputLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            outputLabel.topAnchor.constraint(equalTo: encryptButton.bottomAnchor, constant: 20),
            outputLabel.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.8)
        ])
    }
    
    func authenticateUser(completion: @escaping (Bool, Error?) -> Void) {
        let context = LAContext()
        var error: NSError?
        
        if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
            let reason = "Biometric authentication is needed to access your secure data."
            context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, authenticationError in
                DispatchQueue.main.async {
                    completion(success, authenticationError)
                }
            }
        } else {
            // Biometry is not available or not enrolled.
            DispatchQueue.main.async {
                completion(false, error)
            }
        }
    }
    
    @objc func encryptAction() {
        authenticateUser { [weak self] (success, error) in
            guard success else {
                self?.outputLabel.text = "Authentication failed: \(error?.localizedDescription ?? "Unknown error")"
                return
            }
            
            guard let randomNumber = self?.inputTextField.text, !randomNumber.isEmpty,
                  let dataToSign = randomNumber.data(using: .utf8),
                  let privateKey = self?.generatePrivateKey() else {
                self?.outputLabel.text = "Error: Could not generate private key."
                return
            }
            
            if let signature = self?.signData(privateKey: privateKey, data: dataToSign) {
                self?.outputLabel.text = "Signature: \(signature.base64EncodedString())"
            } else {
                self?.outputLabel.text = "Error: Could not sign data."
            }
        }
    }
    
    func generatePrivateKey() -> SecKey? {
        // 1. Create Keys Access Control
        guard let accessControl =
            SecAccessControlCreateWithFlags(
                nil,
                kSecAttrAccessibleWhenUnlockedThisDeviceOnly,
                [.privateKeyUsage],
                nil)
        else {
            fatalError("cannot set access control")
        }
        
        
        // 2. Create Key Attributes
        guard let tag = "com.example.keys.mykey".data(using: .utf8) else {
            fatalError("cannot set tag")
        }
        let attributes: [String: Any] = [
             kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom,
             kSecAttrKeySizeInBits as String: 256,
             kSecAttrTokenID as String: kSecAttrTokenIDSecureEnclave,
             kSecPrivateKeyAttrs as String: [
                 kSecAttrIsPermanent as String: true,
                 kSecAttrApplicationTag as String: tag,
                 kSecAttrAccessControl as String: accessControl
             ]
         ]
         
        
        // 3. Generate Key Pairs
        var error: Unmanaged<CFError>?
        guard let privateKey = SecKeyCreateRandomKey(attributes as CFDictionary, &error) else {
            if let error = error?.takeRetainedValue() {
                        print("Error creating a key: \(error)")
                    }
            return nil
        }

        return privateKey
    }
    
    func signData(privateKey: SecKey, data: Data) -> Data? {
        let digest = sha256(data: data)

        var error: Unmanaged<CFError>?
        guard let signature = SecKeyCreateSignature(privateKey,
                                                    .ecdsaSignatureMessageX962SHA256,
                                                    digest as CFData,
                                                    &error) as Data? else {
            print(error!.takeRetainedValue() as Error)
            return nil
        }
        
        return signature
    }

}

THIS DOESN'T

  guard let accessControl =
            SecAccessControlCreateWithFlags(
                nil,
                kSecAttrAccessibleWhenUnlockedThisDeviceOnly,
                [.privateKeyUsage, .biometryCurrentSet],
                nil)
        else {

info.something file is updated and there is a privacy FaceID field included.

the error is triggered at this part:

        var error: Unmanaged<CFError>?
        guard let privateKey = SecKeyCreateRandomKey(attributes as CFDictionary, &error) else {
            if let error = error?.takeRetainedValue() {
                        print("Error creating a key: \(error)")
                    }
            return nil
        }

The error itself:

Error creating a key: Error Domain=NSOSStatusErrorDomain Code=-25293 "Key generation failed, error -25293" UserInfo={numberOfErrorsDeep=0, NSDescription=Key generation failed, error -25293}


Solution

  • I found an issue. The problem was due to the limitations of the simulator. As soon as I run the app on my mobile phone, everything started to work

    PS: The code above will produce double FaceID check :)

    That's the correct version in the end:

    func generatePrivateKey() -> SecKey? {
        // 1. Create Keys Access Control
        guard let accessControl =
            SecAccessControlCreateWithFlags(
                nil,
                kSecAttrAccessibleWhenUnlockedThisDeviceOnly,
                [.privateKeyUsage, .biometryCurrentSet],
                nil)
        else {
            fatalError("cannot set access control")
        }