iOS 12 Swift NWConnection TLS Certificate

Viewed 426

I am struggling trying to manually add a trusted certificate manually. I have saved the cert (using Wireshark) but I'm not sure how to tell the NWConnection to trust it.

So far I have ...

    let cert = FileManager().contents(atPath: "path/to/cert.cert")
    let certArray = [cert]
    let policy = SecPolicyCreateSSL(false, nil)
    var optionalTrust: SecTrust?
    let trust = SecTrustCreateWithCertificates(certArray as AnyObject,
                                               policy,
                                               &optionalTrust)
    //Not really sure how to get from "trust" to something I could pass to NWConnection?




    let my_host = NWEndpoint.hostPort(host: "192.168.0.1", port: 4444)
    let connection = NWConnection(to: my_host, using: .tls)

    connection.stateUpdateHandler = { (newState) in
        switch(newState) {
        case .ready:
            print("ready")
        // Handle connection established
        case .waiting(let error):
            print("waiting: ", error)
        // Handle connection waiting for network
        case .failed(let error):
            print("failed", error)
        // Handle fatal connection error
        default:
            print("default")
        }
    }
    let myQ = DispatchQueue.init(label: "connQ")
    connection.start(queue: myQ)
0 Answers
Related