title: How to get private key from p12 in X509 certificate
body: <p>I need to integrate my iOS app to consume a Web service requires a client certificate. I am using the below code to extract the private key from p12 in X509 certificate.
  I found this code but not working, I translated it to swift 3.</p>
  
  <pre><code>func privateKeyFromCertificate() -&gt; SecKeyRef {
  
      let certName : String = //name of the certificate//
  
      let resourcePath: String = NSBundle.mainBundle().pathForResource(certName, ofType: "p12")!
  
      let p12Data: NSData = NSData(contentsOfFile: resourcePath)!
  
      let key : NSString = kSecImportExportPassphrase as NSString
      let options : NSDictionary = [key : "password_for_certificate"]
  
      var privateKeyRef: SecKeyRef? = nil
  
      var items : CFArray?
  
      let securityError: OSStatus = SecPKCS12Import(p12Data, options, &amp;items)
  
      //let description : CFString = CFCopyDescription(items)
      //print(description)
  
      let theArray : CFArray = items!
  
      if securityError == noErr &amp;&amp; CFArrayGetCount(theArray) &gt; 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 , &amp;privateKeyRef)
          if securityError != noErr {
              privateKeyRef = nil
          }
      }
      return privateKeyRef!
  }
  </code></pre>
  
  <p>The error encounter at :
  let secIdentity = dictionary.valueForKey(kSecImportItemIdentity as String) as! SecIdentityRef</p>
  
  <p><strong>Below Updated version has the same problem at the same line:</strong></p>
  
  <pre><code>   func privateKeyFromCertificate() -&gt; SecKey {       
          let certName : String = "certficateName"
          let resourcePath: String = Bundle.main.path(forResource: certName, ofType: "p12")!
          let p12Data: NSData = NSData(contentsOfFile: resourcePath)!
          let key : NSString = kSecImportExportPassphrase as NSString
          let options : NSDictionary = [key : "password_for_certificate"]
          var privateKeyRef: SecKey? = nil
          var items : CFArray?
          let securityError: OSStatus = SecPKCS12Import(p12Data, options, &amp;items)
          //let description : CFString = CFCopyDescription(items)
          //print(description)
          let theArray : CFArray = items!
          if securityError == noErr &amp;&amp; CFArrayGetCount(theArray) &gt; 0 {
              let newArray = theArray as [AnyObject] as NSArray
              let dictionary = newArray.object(at: 0)
  //- encounter error here:
              let secIdentity = (dictionary as AnyObject).value(kSecImportItemIdentity as String) as! SecIdentity
  
              let securityError = SecIdentityCopyPrivateKey(secIdentity , &amp;privateKeyRef)
              if securityError != noErr {
                  privateKeyRef = nil
              }
          }
          return privateKeyRef!
      }
  </code></pre>
  
  <p>I get the following error:</p>
  
  <p>let secIdentity = (dictionary as AnyObject).value(kSecImportItemIdentity as String) as! SecIdentity</p>
  
  <p>Cannot invoke 'Value' with any argument list of Type '(String)'</p>
  
  <p>Your help is greatly appreciated.</p>
  
  <p>Thanks</p>