title: IOS Request with client certificate authentication
body: <p>Hi guys Im trying to apply this authentication <a href="http://veewee.github.io/blog/authenticating-with-x509-client-certificates/" rel="nofollow">strategy</a> . Basically I create RSA Key pair, then I create CSR(Certificate Signing Request), then I send it to server which will return (X.509)Self-signed client certificate in response. Then I remove ----BEGIN CERTIFICATE---- and ----END CERTIFICATE----, transform it to base64 encoded data with line length 64 characters, and save it to keychain this way </p>
  
  <ol>
  <li>create reference with <code>let certRef: CFTypeRef? = SecCertificateCreateWithData(nil , cert)</code></li>
  <li>save it to keychain with <code>let params: NSDictionary = [kSecClass as String: kSecClassCertificate, kSecValueRef as String: certRef!]
      let status = SecItemAdd(params, nil)</code></li>
  </ol>
  
  <p>certificate is saved, now If I understand it correctly, if I have private key with which CSR was created saved in keychain, I should be able to get <code>SecIdentity</code> which I will use in NSURLSession to make request. So I created method to get this identity this is how it looks like :</p>
  
  <pre><code>     func getIdentityReference() -&gt; SecIdentityRef? {
          let parameters = [
              kSecClass as String: kSecClassIdentity,
              kSecAttrApplicationTag as String: kAsymmetricCryptoManagerApplicationTag,
              kSecReturnRef as String: true
          ]
          var ref: AnyObject?
          let status = SecItemCopyMatching(parameters, &amp;ref)
          if status == errSecSuccess { return ref as! SecIdentityRef? } else { return nil }
      } 
  </code></pre>
  
  <p>everything works fine, Im getting identity so the only thing to do is actual request. For that I need to implement NSURLSessionDelegate's did receive challenge method and here it is</p>
  
  <pre><code>func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -&gt; Void) {
          let method = challenge.protectionSpace.authenticationMethod
          print(method)
  
          if method == NSURLAuthenticationMethodServerTrust {
              let host = challenge.protectionSpace.host
              print(host)
  
              let credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)
              completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, credential)
          }
  
          let identity = AsymmetricCryptoManager.sharedInstance.getIdentityReference()!
  
          var certificate: SecCertificateRef? = nil
          SecIdentityCopyCertificate(identity, &amp;certificate)
  
          var certArray = [certificate!]
          let credential = NSURLCredential(identity: identity, certificates: certArray, persistence: .Permanent)
  
          completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, credential)
      }
  </code></pre>
  
  <p>(I'm just prototyping, that's the reason for implicit unwrapping)</p>
  
  <p>but when I fire this request I'm getting error </p>
  
  <pre><code>Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo={_kCFStreamErrorCodeKey=-9824, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, NSUnderlyingError=0x13cfe7850 {Error Domain=kCFErrorDomainCFNetwork Code=-1200 "(null)" UserInfo={_kCFStreamPropertySSLClientCertificateState=0, _kCFNetworkCFStreamSSLErrorOriginalValue=-9824, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9824}}, NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made., NSErrorFailingURLKey=https://blabla.blabla, NSErrorFailingURLStringKey=https://blabla.blabla, _kCFStreamErrorDomainKey=3}
  </code></pre>
  
  <p>now I really don't know where is the problem, note that on Android everything works well, so server is set up correctly. Any help will be appreciated and it might help few people in the future.
  Thank you</p>