0

Here is the curl command that works:

curl -d {"Key1":"value1"} -k -vvvv --request POST --header "Content-Type: application/json" --key KEY.pem --cacert CRT.pem --cert KEY.pem "URL"

How do I translate this to Alamofire request command? I get the authentication failure errors:

Connection 1: default TLS Trust evaluation failed(-9807)
2020-04-09 01:51:46.604692-0600 CertificatePinningExample[7192:1891639] Connection 1: TLS Trust encountered error 3:-9807
2020-04-09 01:51:46.604879-0600 CertificatePinningExample[7192:1891639] Connection 1: encountered error(3:-9807)
2020-04-09 01:51:46.606672-0600 CertificatePinningExample[7192:1891639] Connection 1: unable to determine interface type without an established connection
2020-04-09 01:51:46.650936-0600 CertificatePinningExample[7192:1891639] Task <9E539D4B-9694-426E-B382-6350044743B0>.<1> HTTP load failed, 0/0 bytes (error code: -1202 [3:-9807])
2020-04-09 01:51:46.662507-0600 CertificatePinningExample[7192:1891652] Task <9E539D4B-9694-426E-B382-6350044743B0>.<1> finished with error [-1202] Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “BLAH” which could put your confidential information at risk." UserInfo={NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, NSErrorPeerCertificateChainKey=(

// Alamofire code to fire up the request


override func viewDidLoad() {
        super.viewDidLoad()

        let evaluators = [
          "SERVER_IP":
            PinnedCertificatesTrustEvaluator(certificates:
              getCertificates()
              )
        ]

        let session = Session(
          serverTrustManager: ServerTrustManager(evaluators: evaluators)
        )



        let url = URL(string: "URL_TO_HIT")!
        //enableCertificatePinning()

        var dict: NSDictionary = ["SessionId":""]
        var data: Data = Data.init()
        do {
            data = try JSONSerialization.data(withJSONObject: dict, options: [])
        } catch{
            data = "".data(using: .utf8)!
        }

        //let data = try JSONSerialization.data(withJSONObject: dict, options: [])

        let request = AF.request(url, method: .post)
        // 2
        request.responseJSON { (data) in
          print(data)
        }
    }

    private func getCertificates() -> [SecCertificate] {
        let url = Bundle.main.url(forResource: "ExampleCert", withExtension: "der")!
        let localCertificate = try! Data(contentsOf: url) as CFData
        guard let certificate = SecCertificateCreateWithData(nil, localCertificate)
            else { return [] }

        return [certificate]
   }


My concern is: Alamofire is asking for only one cert vs I am using three cert flags in curl. How do I translate Alamofire request to the same as curl?

  • Check that your website satisfies Apple TLS requirements https://support.apple.com/en-us/HT210176 – Denis Kreshikhin Apr 09 '20 at 08:30
  • What is your Alamofire request code? You know you can ask Alamofire to print as a `curl` command and compare that you are doing everything alright since you have a working curl command already? (see https://stackoverflow.com/questions/53637437/alamofire-with-d/53637821#53637821 to print it) – Larme Apr 09 '20 at 09:06
  • thanks. Looking into printing as the curl command. Here is the Alamofire request that I am sending. Any comments on how to translate the above curl command to an equivalent Alamofire request would be greatly useful! thanks for taking the time to comment. – curiousAboutSwift Apr 09 '20 at 09:40
  • Please see the edited post for the Alamofire request plus cert validation pieces. – curiousAboutSwift Apr 09 '20 at 09:57

0 Answers0