Swift SSL error with self signed certificate


Marcus Leon

This code tries but fails to access the SSL URL available in the browser:

let path = "https://localhost:8443/greeting"
let request = NSMutableURLRequest(URL: NSURL(string: path)!)
let session = NSURLSession.sharedSession()

let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
    let json:JSON = JSON(data: data!)
    if let c = json["content"].string {
        print(c)
    }
})
task.resume()

Error failed:

optional(Error Domain=NSURLErrorDomain Code=-1200 "An SSL error occurred and a secure connection to the server could not be established." UserInfo={NSURLErrorFailingURLPeerTrustErrorKey=,

What does it take to allow the app to accept this certificate?

The certificate in question is self-signed. Read some solutions on SO but no success.

Running Xcode 7.2

Marcus Leon

@Ashish Kakkad was found. This works:

class Blah: NSURLSessionDelegate {

    func rest() {
        let path = "https://localhost:8443/greeting"
        let request = NSMutableURLRequest(URL: NSURL(string: path)!)
        let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
        let session = NSURLSession(configuration: configuration, delegate: self, delegateQueue:NSOperationQueue.mainQueue())
        let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
            let json:JSON = JSON(data: data!)
            if let c = json["content"].string {
                print(c)
            }
        })
        task.resume()
    }

    func URLSession(session: NSURLSession, task: NSURLSessionTask, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {
        completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!))
    }
}

Include the following in your Info.plist file:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>localhost</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>
</dict>

Related


Swift SSL error with self signed certificate

Marcus Leon This code tries but fails to access the SSL URL available in the browser: let path = "https://localhost:8443/greeting" let request = NSMutableURLRequest(URL: NSURL(string: path)!) let session = NSURLSession.sharedSession() let task = session.dataT

Swift SSL error with self signed certificate

Marcus Leon This code tries but fails to access the SSL URL available in the browser: let path = "https://localhost:8443/greeting" let request = NSMutableURLRequest(URL: NSURL(string: path)!) let session = NSURLSession.sharedSession() let task = session.dataT

Swift SSL error with self signed certificate

Marcus Leon This code tries but fails to access the SSL URL available in the browser: let path = "https://localhost:8443/greeting" let request = NSMutableURLRequest(URL: NSURL(string: path)!) let session = NSURLSession.sharedSession() let task = session.dataT

Swift SSL error with self signed certificate

Marcus Leon This code tries but fails to access the SSL URL available in the browser: let path = "https://localhost:8443/greeting" let request = NSMutableURLRequest(URL: NSURL(string: path)!) let session = NSURLSession.sharedSession() let task = session.dataT

Swift SSL error with self signed certificate

Marcus Leon This code tries but fails to access the SSL URL available in the browser: let path = "https://localhost:8443/greeting" let request = NSMutableURLRequest(URL: NSURL(string: path)!) let session = NSURLSession.sharedSession() let task = session.dataT

Swift SSL error with self signed certificate

Marcus Leon This code tries but fails to access the SSL URL available in the browser: let path = "https://localhost:8443/greeting" let request = NSMutableURLRequest(URL: NSURL(string: path)!) let session = NSURLSession.sharedSession() let task = session.dataT

Swift SSL error with self signed certificate

Marcus Leon This code tries but fails to access the SSL URL available in the browser: let path = "https://localhost:8443/greeting" let request = NSMutableURLRequest(URL: NSURL(string: path)!) let session = NSURLSession.sharedSession() let task = session.dataT

ssl self signed certificate error - localhost

Rafał Developer I'm using this class and it's fun, but I have some questions. http://www.asp.net/web-api/overview/security/individual-accounts-in-web-api I get a certificate error, but I know why this problem occurs. The certificate should be connected to the

ssl self signed certificate error - localhost

Rafał Developer I'm using this class and it's fun, but I have some questions. http://www.asp.net/web-api/overview/security/individual-accounts-in-web-api I get a certificate error, but I know why this problem occurs. The certificate should be connected to the

ssl self signed certificate error - localhost

Rafał Developer I'm using this class and it's fun, but I have some questions. http://www.asp.net/web-api/overview/security/individual-accounts-in-web-api I get a certificate error, but I know why this problem occurs. The certificate should be connected to the

ssl self signed certificate error - localhost

Rafał Developer I'm using this class and it's fun, but I have some questions. http://www.asp.net/web-api/overview/security/individual-accounts-in-web-api I get a certificate error, but I know why this problem occurs. The certificate should be connected to the

ssl self signed certificate error - localhost

Rafał Developer I'm using this class and it's fun, but I have some questions. http://www.asp.net/web-api/overview/security/individual-accounts-in-web-api I get a certificate error, but I know why this problem occurs. The certificate should be connected to the

SSL - Self Signed Certificate with Wildcard

Niek Vandael I created a certificate with CN: *.hostname Chome and IE complain about this certificate when I navigate to dev.hostname or tst.hostname: Error in Chrome: NET::ERR_CERT_COMMON_NAME_INVALID Yes, I have added the certificate to Trusted Root CAs. Sho

SSL - Self Signed Certificate with Wildcard

Niek Vandael I created a certificate with CN: *.hostname Chome and IE complain about this certificate when I navigate to dev.hostname or tst.hostname: Error in Chrome: NET::ERR_CERT_COMMON_NAME_INVALID Yes, I have added the certificate to Trusted Root CAs. Sho

Intellij Maven repository self signed certificate, ssl error

Benno In intellij (15.0.4) under Settings -> Maven -> Repositories, I got an error when I clicked update on the company repository. org.apache.maven.wagon.TransferFailedException: sun.security.validator.ValidatorException: PKIX path build failed: sun.security.

SSL handshake error with self-signed certificate in Flutter

Armen Kh。 I'm trying to connect to a server using a self-signed certificate, but I'm getting an error: E/flutter(3781): HandshakeException: Handshake error in client (OS Error: E/flutter(3781): CERTIFICATE_VERIFY_FAILED: Hostname mismatch (ssl_cert. c:345)) co

Intellij Maven repository self signed certificate, ssl error

Benno In intellij (15.0.4) under Settings -> Maven -> Repositories, I got an error when I clicked update on the company repository. org.apache.maven.wagon.TransferFailedException: sun.security.validator.ValidatorException: PKIX path build failed: sun.security.

SSL handshake error with self-signed certificate in Flutter

Armen Kh。 I'm trying to connect to a server using a self-signed certificate, but I'm getting an error: E/flutter(3781): HandshakeException: Handshake error in client (OS Error: E/flutter(3781): CERTIFICATE_VERIFY_FAILED: Hostname mismatch (ssl_cert. c:345)) co

SSL handshake error with self-signed certificate in Flutter

Armen Kh。 I'm trying to connect to a server using a self-signed certificate, but I'm getting an error: E/flutter(3781): HandshakeException: Handshake error in client (OS Error: E/flutter(3781): CERTIFICATE_VERIFY_FAILED: Hostname mismatch (ssl_cert. c:345)) co

Intellij Maven repository self signed certificate, ssl error

Benno In intellij (15.0.4) under Settings -> Maven -> Repositories, I got an error when I clicked update on the company repository. org.apache.maven.wagon.TransferFailedException: sun.security.validator.ValidatorException: PKIX path build failed: sun.security.

Spring WebFlux Netty SSL with self signed certificate error

Peter I'm trying to access a Spring Boot application running on Netty via https in localhost using a self signed certificate on the server side. Mine application.propertieslooks like this: server.ssl.enabled=true server.ssl.key-store-type=JKS server.ssl.key-st

SSL handshake error with self-signed certificate in Flutter

Armen Kh。 I'm trying to connect to a server using a self-signed certificate, but I'm getting an error: E/flutter(3781): HandshakeException: Handshake error in client (OS Error: E/flutter(3781): CERTIFICATE_VERIFY_FAILED: Hostname mismatch (ssl_cert. c:345)) co

Intellij Maven repository self signed certificate, ssl error

Benno In intellij (15.0.4) under Settings -> Maven -> Repositories, I got an error when I clicked update on the company repository. org.apache.maven.wagon.TransferFailedException: sun.security.validator.ValidatorException: PKIX path build failed: sun.security.

Intellij Maven repository self signed certificate, ssl error

Benno In intellij (15.0.4) under Settings -> Maven -> Repositories, I got an error when I clicked update on the company repository. org.apache.maven.wagon.TransferFailedException: sun.security.validator.ValidatorException: PKIX path build failed: sun.security.

SSL handshake error with self-signed certificate in Flutter

Armen Kh。 I'm trying to connect to a server using a self-signed certificate, but I'm getting an error: E/flutter(3781): HandshakeException: Handshake error in client (OS Error: E/flutter(3781): CERTIFICATE_VERIFY_FAILED: Hostname mismatch (ssl_cert. c:345)) co

Spring WebFlux Netty SSL with self signed certificate error

Peter I'm trying to access a Spring Boot application running on Netty via https in localhost using a self signed certificate on the server side. Mine application.propertieslooks like this: server.ssl.enabled=true server.ssl.key-store-type=JKS server.ssl.key-st