ios - Swift 中的 CFDictionaryRef 问题

标签 ios swift core-foundation cfdictionary

我正在将一些旧的 Objective-C 代码转换为 Swift,这样我就可以摆脱一些已弃用的方法,但我不断遇到崩溃,到目前为止我似乎无法弄清楚是什么原因造成的。我从 P12 证书获取私钥,这种方法似乎工作正常,直到我到达实际需要从 CFArray 获取字典的部分,即使数组中有值,应用程序仍然崩溃。这是我的代码:

func privateKeyFromCertificate(p12Name: String, withPassword password: String) -> SecKeyRef {
    let resourcePath: String = NSBundle.mainBundle().pathForResource(p12Name, ofType: "p12")!
    let p12Data: NSData = NSData(contentsOfFile: resourcePath)!
    let key : NSString = kSecImportExportPassphrase as NSString
    let options : NSDictionary = [key : password]

    var privateKeyRef: SecKeyRef? = nil


    var items : CFArray?


    let securityError: OSStatus = SecPKCS12Import(p12Data, options, &items)

    let description : CFString = CFCopyDescription(items)
    print(description)

    if securityError == noErr && CFArrayGetCount(items) > 0 {
        let objects : CFDictionaryRef = CFArrayGetValueAtIndex(items, 0) as! CFDictionaryRef
        let kString : NSString = kSecImportItemIdentity as NSString
        let identity : SecIdentityRef = CFDictionaryGetValue(objects, unsafeAddressOf(kString)) as! SecIdentityRef
        let securityError = SecIdentityCopyPrivateKey(identity, &privateKeyRef)
        if securityError != noErr {
            privateKeyRef = nil
        }
    }
    return privateKeyRef!
}

应用程序在第一行的 if 语句中不断崩溃,我尝试从 CFArray 获取 CFDictiionaryRef。我添加了一行来打印 CFArray 的描述作为测试,它确实有值:

<CFArray 0x7fd2d2e8c2f0 [0x10b61f7b0]>{type = mutable-small, count = 1, values = (
0 : <CFBasicHash 0x7fd2d2e8c190 [0x10b61f7b0]>{type = mutable dict, count = 3,entries =>
0 : <CFString 0x10bfdd2c0 [0x10b61f7b0]>{contents = "trust"} = <SecTrustRef: 0x7fd2d2e8ad30>
1 : <CFString 0x10bfdd300 [0x10b61f7b0]>{contents = "identity"} = <SecIdentityRef: 0x7fd2d2e80390>
2 : <CFString 0x10bfdd2e0 [0x10b61f7b0]>{contents = "chain"} = <CFArray 0x7fd2d2d016e0 [0x10b61f7b0]>{type = mutable-small, count = 1, values = (
0 : <cert(0x7fd2d2e8c610) s: Client (IPHONE-WebService) i:Client (IPHONE-WebService)>)}}

最佳答案

我最终改变了一些东西来获取数据,这不是最漂亮的方法,但它对我有用。

func privateKeyFromCertificate() -> SecKeyRef {
    let certName : String = //name of the certificate//
    //get p12 file path
    let resourcePath: String = NSBundle.mainBundle().pathForResource(certName, ofType: "p12")!
    let p12Data: NSData = NSData(contentsOfFile: resourcePath)!
    //create key dictionary for reading p12 file
    let key : NSString = kSecImportExportPassphrase as NSString
    let options : NSDictionary = [key : "password_for_certificate"]
    //create variable for holding security information
    var privateKeyRef: SecKeyRef? = nil

    var items : CFArray?

    let securityError: OSStatus = SecPKCS12Import(p12Data, options, &items)
    //let description : CFString = CFCopyDescription(items)
    //print(description)

    let theArray : CFArray = items!

    if securityError == noErr && CFArrayGetCount(theArray) > 0 {
        let newArray = theArray as [AnyObject] as NSArray
        let dictionary = newArray.objectAtIndex(0)
        let secIdentity = dictionary.valueForKey(kSecImportItemIdentity as String) as! SecIdentityRef
        let securityError = SecIdentityCopyPrivateKey(secIdentity , &privateKeyRef)
        if securityError != noErr {
            privateKeyRef = nil
        }
    }
    return privateKeyRef!
}

关于ios - Swift 中的 CFDictionaryRef 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32704394/

相关文章:

ios - 将多个配置/持久存储快速添加到核心数据

ios - 如何使用 firebase 在 swift 代码上创建搜索字段?

objective-c - 检测正在运行的应用程序是否被沙盒化

c++ - 带有 lambda 自定义删除器的 std::unique_ptr 无法编译

swift - 如何持久化 NSUserDefault?

objective-c - 下载时 CFURL 不提供完整数据

ios - 无法从我的项目中的框架加载图像

ios - 延迟后如何为UILabel文本设置动画?

swift - zlib 的 crc32 函数在 swift 3 中失败无法使用类型 'UnsafePointer<Bytef>' 的参数列表调用类型 '(UnsafeRawPointer)' 的初始化程序

swift - 有没有一种干净的方法来处理 Swift 3 中条件中的赋值表达式?