CFDictionaryRef issues in Swift

痴心易碎 提交于 2019-12-04 17:09:54

I ended up changing some things around to get to the data, it's not the prettiest approach but it worked for me.

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!
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!