gpt4 book ai didi

ios - 如何将 SecIdentityRef 添加到 iOS 钥匙串(keychain),swift

转载 作者:行者123 更新时间:2023-11-28 06:55:59 25 4
gpt4 key购买 nike

我正在读取一个 p12 文件并获取一个 SecIdentityRef 然后将这个身份添加到钥匙串(keychain),如下所示

let certData: NSData = NSFileManager.defaultManager().contentsAtPath(filePath)!
let passDictionary: NSMutableDictionary = NSMutableDictionary()
passDictionary.setValue("pass", forKey: kSecImportExportPassphrase as String)
print(kSecImportExportPassphrase as String)
var items: CFArray?
let error = SecPKCS12Import(certData, passDictionary, &items)
let unwrappedItems:CFArray = items!
if error == noErr && CFArrayGetCount(items) > 0 {
let certChain = unwrappedItems as [AnyObject] as NSArray
let certificateDict = certChain.objectAtIndex(0)
var privateKeyRef : SecKeyRef? = nil
var certificateRef: SecCertificate? = nil
let secIdentity:SecIdentityRef = certificateDict.valueForKey(kSecImportItemIdentity as String) as! SecIdentityRef
let subject:NSString=SecCertificateCopySubjectSummary(certificateRef!)

let keyChainQuery:NSMutableDictionary = NSMutableDictionary(
objects: [String(kSecClassIdentity),subject,kCFBooleanTrue,String(kSecAttrAccessibleAlwaysThisDeviceOnly),secIdentity],
forKeys: [String(kSecClass),String(kSecAttrLabel), String(kSecAttrCanSign),String(kSecAttrAccessible),String(kSecValueRef)])
let status:OSStatus = SecItemAdd(keyChainQuery as CFDictionaryRef, nil)
}

这似乎工作正常并返回 0 作为状态代码,但是当我尝试从钥匙串(keychain)中读取此项时:

var identity: AnyObject?
let searchQuery: NSMutableDictionary = NSMutableDictionary(objects: [String(kSecClassIdentity), kCFBooleanTrue], forKeys: [String(kSecClass),String(kSecReturnRef)])
let status:OSStatus = SecItemCopyMatching(searchQuery as CFDictionaryRef, &identity)

我收到一个 -25300 错误代码(未找到项目)nil 身份,我使用 iOS 9.1 作为基础 SDK,我做错了什么?

更新:在 8.1、8.4 和 9.1 模拟器以及 9.1 真实设备中测试,代码正常运行。 8.4.1 版本的 iPhone 出现故障,有人知道发生了什么吗?

最佳答案

最终重新安装应用程序并使用 SecItemDelete 清理钥匙串(keychain)解决了问题。

希望这对您有所帮助!

关于ios - 如何将 SecIdentityRef 添加到 iOS 钥匙串(keychain),swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33633122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com