title: Client certificate authentication in swift2
body: <p>I want to authenticate client certificate in my app. I am getting the following errors in <code>didReceiveChallenge</code> function. I found this solution in this <a href="https://stackoverflow.com/questions/30174052/swift-ios-client-certificate-authentication">link</a>.</p>
  
  <p><a href="https://i.stack.imgur.com/AaGpy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AaGpy.png" alt="enter image description here"></a></p>
  
  <p>My <code>didReceiveChallenge</code> func code:</p>
  
  <pre><code>   func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -&gt; Void) {
  
      if challenge.protectionSpace.authenticationMethod == (NSURLAuthenticationMethodServerTrust) {
  
  
      let serverTrust:SecTrustRef = challenge.protectionSpace.serverTrust!
      let certificate: SecCertificateRef = SecTrustGetCertificateAtIndex(serverTrust, 0)!
      let remoteCertificateData = CFBridgingRetain(SecCertificateCopyData(certificate))!
      let cerPath: String = NSBundle.mainBundle().pathForResource("example.com", ofType: "cer")!
      let localCertificateData = NSData(contentsOfFile:cerPath)!
  
  
      if (remoteCertificateData.isEqualToData(localCertificateData) == true) {
          let credential:NSURLCredential = NSURLCredential(forTrust: serverTrust)
          challenge.sender?.useCredential(credential, forAuthenticationChallenge: challenge)
          completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!))
  
      } else {
  
          completionHandler(NSURLSessionAuthChallengeDisposition.CancelAuthenticationChallenge, nil)
      }
      }
  
      else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodClientCertificate
      {
  
          let path: String = NSBundle.mainBundle().pathForResource("client", ofType: "p12")!
          let PKCS12Data = NSData(contentsOfFile:path)!
  
          var p12items : Unmanaged&lt;CFArray&gt;?
  
          let index: CFIndex = 1
          let password: CFString = "password"
  
          var values = [unsafeAddressOf(password)]
          var keys = [unsafeAddressOf(kSecImportExportPassphrase)]
  
          var keyCallbacks = kCFTypeDictionaryKeyCallBacks
          var valueCallbacks = kCFTypeDictionaryValueCallBacks
  
          let length: CFIndex = PKCS12Data.length
          let p12CfData: CFData = CFDataCreate(kCFAllocatorDefault, UnsafePointer&lt;UInt8&gt;(PKCS12Data.bytes), length)
  
          let options = CFDictionaryCreate(kCFAllocatorDefault, &amp;keys, &amp;values, index, &amp;keyCallbacks, &amp;valueCallbacks)
          let result = SecPKCS12Import(p12CfData, options, p12items)
  
          if result == noErr {
  
              let idIndex: CFIndex = 0
              var items = p12items?.takeRetainedValue()
              var identityDict = CFArrayGetValueAtIndex(items!, idIndex)
  
              var keyAddress = unsafeAddressOf(kSecImportItemIdentity)
              var identityApp: SecIdentityRef = CFDictionaryGetValue(identityDict, keyAddress)
              var certRef : Unmanaged&lt;SecCertificateRef&gt;?
              SecIdentityCopyCertificate(identityApp, certRef)
  
              var cert: SecCertificateRef = certRef!.takeRetainedValue()
              var certArray = [unsafeAddressOf(cert)]
              var arrayCallback = kCFTypeArrayCallBacks
              var myCerts: CFArrayRef = CFArrayCreate(kCFAllocatorDefault, &amp;certArray, index, &amp;arrayCallback);
  
  
          let urlCredential:NSURLCredential = NSURLCredential(
              identity: identityApp,
              certificates: myCerts as [AnyObject],
              persistence: NSURLCredentialPersistence.Permanent)
  
          challenge.sender!.useCredential(urlCredential ,forAuthenticationChallenge:challenge)
  
      }
  
  
  }
  </code></pre>
  
  <p>Any example for client authentication in NSURLSession will be helpful.
  Thanks in advance.</p>