Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how can I connecting my server as HTTPS with using self-signed Certificate file #977

Closed
dickwu opened this issue Dec 24, 2015 · 4 comments
Closed
Assignees

Comments

@dickwu
Copy link

dickwu commented Dec 24, 2015

I setup up a web server on localhost with self-sign Certificate file, but I can't POST data to the Server over HTTPS.

let serverTrustPolicies: [String: ServerTrustPolicy] = [
"localhost": .PinCertificates(
certificates: ServerTrustPolicy.certificatesInBundle(),
validateCertificateChain: true,
validateHost: true
),
"insecure.expired-apis.com": .DisableEvaluation
]

    let manager = Manager(
        serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
    )

manager.request(.POST, "https://localhost/index.php", parameters: nil).responseString { (response) -> Void in
print(response.result.value)
}

print result is nil

@ntnmrndn
Copy link
Contributor

validateCertificateChain: true, => Change it to false.

You should try to ask this kind of questions on stack overflow (as instructed to in the readme).
Buy the way, you should also get a real certificate. Especially since you have a free option with https://letsencrypt.org

@dickwu
Copy link
Author

dickwu commented Dec 25, 2015

I override NSURLSessionDelegate like this below: (validateCertificateChain: true, => Change it to false. is not useful)
func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {
if let localCertificateData = self.localCertData {//NSData
if let serverTrust = challenge.protectionSpace.serverTrust,
certificate = SecTrustGetCertificateAtIndex(serverTrust, 0),
remoteCertificateData: NSData = SecCertificateCopyData(certificate) {
if localCertificateData.isEqualToData(remoteCertificateData) {
let credential = NSURLCredential(forTrust: serverTrust)
challenge.sender?.useCredential(credential, forAuthenticationChallenge: challenge)
completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, credential)
} else {
challenge.sender?.cancelAuthenticationChallenge(challenge)
completionHandler(NSURLSessionAuthChallengeDisposition.CancelAuthenticationChallenge, nil)
self.sSLValidateErrorCallBack?()//a block
}
} else {
NSLog("Get RemoteCertificateData or LocalCertificateData error!")
}
} else {
completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, nil)
}
}

@davepatterson
Copy link

Did you ever figure this out. I had a similar issue and it was caused by my not declaring manager globally. The following declaration of manager fixed it for me:

class AppTests: XCTestCase {
    let manager: Alamofire.Manager = {
    let serverTrustPolicies: [String: ServerTrustPolicy] = [
    "yourdomain.com": .DisableEvaluation
]

let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.HTTPAdditionalHeaders = Alamofire.Manager.defaultHTTPHeaders

return Alamofire.Manager(
    configuration: configuration,
    serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
)
}()

Also, @dickwu, for future references, I recommend that you to prepend each line of your code with four spaces so that it is easier to read. Doing so is how I achieved the code display that you see above.

@cnoon
Copy link
Member

cnoon commented Jan 3, 2016

Hi @dickwu,

My guess at this point is that you're getting bit by ATS. Here's a few tips:

  • Check out our documentation in the README. This will most likely fix up your issue.
  • Dig through all the info in Self-Signed Certificate not accepted #876. There's a TON of great info in that issue about ATS and how to wield it.
  • Finally, you should stop using self-signed certs. Check out Let's Encrypt like @ntnmrndn mentioned. 👍🏼

If you still cannot figure it out, I'd suggest opening a question on Stack Overflow.

Best of luck! 🍻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants