简体   繁体   English

SecKeyIsAlgorithmSupported 处的 EXC_BAD_ACCESS 代码 = 257

[英]EXC_BAD_ACCESS code=257 at SecKeyIsAlgorithmSupported

I have a KeyChain class where I sign a string.我有一个 KeyChain class 我在其中签署了一个字符串。 I got Thread 8: EXC_BAD_ACCESS (code=257, address=0x3fd574bc6a7ef9db) error at SecKeyIsAlgorithmSupported function.我在 SecKeyIsAlgorithmSupported function 处遇到Thread 8: EXC_BAD_ACCESS (code=257, address=0x3fd574bc6a7ef9db) SecKeyIsAlgorithmSupported I could not figure out why this error pops up.我无法弄清楚为什么会弹出这个错误。 When I use the getquery variable which is commented it all works fine except on iPhone 13 pro max devices.当我使用被注释的getquery变量时,除了在 iPhone 13 pro max 设备上之外,它都可以正常工作。 So I wanted to try different queries hoping that can work on all devices.所以我想尝试不同的查询,希望可以在所有设备上运行。 But in that case SecKeyIsAlgorithmSupported function crashes giving this error EXC_BAD_ACCESS .但在这种情况下,SecKeyIsAlgorithmSupported function 崩溃,给出此错误EXC_BAD_ACCESS Here is the function I use.这是我使用的 function。

func signString(clearString:String) -> Bool {
        /*let getquery: [String: Any] = [kSecClass as String: kSecClassKey,
                                       kSecAttrApplicationTag as String: serviceName,
                                       kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom,
                                       kSecReturnRef as String: true]*/
        
        let getquery: [String: Any] = [kSecClass as String: kSecClassGenericPassword,
                                       kSecAttrService as String: serviceName,
                                       kSecReturnAttributes as String: kCFBooleanTrue!,
                                       kSecMatchLimit as String: kSecMatchLimitAll]
        
        var item: CFTypeRef?
        let status = SecItemCopyMatching(getquery as CFDictionary, &item)
        print("status = ",status)

        if (status != errSecSuccess) {
            print("No key found")
            return false
        }
        else {
            let key = item as! SecKey
            self.privateKey = key
            
            let data = clearString.data(using: .utf8)! as CFData
            let algorithm: SecKeyAlgorithm = .ecdsaSignatureMessageX962SHA256
            
            if (self.privateKey != nil) {
                guard SecKeyIsAlgorithmSupported(self.privateKey!, .sign, algorithm) else {
                    print("Algorithm Not Supported")
                    return false
                }
                
                var error: Unmanaged<CFError>?
                guard let signature = SecKeyCreateSignature(self.privateKey!,algorithm, data, &error) as Data? else {
                    print("signature error")
                    return false
                }
                
                self.signedString = signature.base64EncodedString()
                return true
            }
            else {
                print("Private Key is null")
                return false
            }
        }
    }

I wish there would be a way to avoid this crash.我希望有办法避免这种崩溃。 I searched about it but I could not find a way to fix that.我搜索了一下,但找不到解决方法。 Any help will be appreciated.任何帮助将不胜感激。 Thanks in advance.提前致谢。

Your get query states kSecMatchLimitAll , which will result in a CFArray object as a result.您的 get 查询状态kSecMatchLimitAll ,结果将导致CFArray object 。 You can easily fix that by changing it to kSecMatchLimitOne , or you can loop the list, by casting it to an array.您可以通过将其更改为kSecMatchLimitOne轻松解决此问题,或者您可以循环列表,将其转换为数组。

let keys = item as! [SecKey]
for key in keys {
    SecKeyIsAlgorithmSupported(key, .sign, . ecdsaSignatureMessageX962SHA256)
}

Do note that not all generic items, or likely none, are valid SecKey objects.请注意,并非所有通用项目,或者可能没有,都是有效的SecKey对象。 It appears you're using ECC keys, which can be stored using the kSecClass: kSecClassKey attribute.看来您正在使用 ECC 密钥,可以使用kSecClass: kSecClassKey属性进行存储。 I would highly recommend storing it as what it is, instead of storing it as a generic password ( kSecClassGenericPassword ) as you're doing right now我强烈建议将其按原样存储,而不是像现在这样将其存储为通用密码 ( kSecClassGenericPassword )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM