SecCertificateCreateWithData is returning nil

Hello, Im trying to make a API call to a server by using a https certificate, but I already get stuck at the server trust stage. I'm using a server certificate with extension .cer and here is my code:

private func shouldTrust(protectionSpace: URLProtectionSpace) -> Bool {
        
        guard let trust = protectionSpace.serverTrust else { return false }
        
        // First try evaluating trust with any custom anchor.  If that succeeds, we're good to go.
        
        var trustResult = SecTrustResultType.invalid
        var err = SecTrustEvaluate(trust, &trustResult)
        guard err == errSecSuccess else { return false }
        if [.proceed, .unspecified].contains(trustResult) { return true }
        
        // If it fails, apply our custom anchor and try again.
        
        let root = Bundle.main.certificateForBankIDPayment(named: "sebtekniskbankinfoserverca")
        err = SecTrustSetAnchorCertificates(trust, [root] as NSArray)
        guard err == errSecSuccess else { return false }
        err = SecTrustEvaluate(trust, &trustResult)
        guard err == errSecSuccess else { return false }
        return [.proceed, .unspecified].contains(trustResult)
    }


extension Bundle {
    
    func certificateForBankIDPayment(named: String) -> SecCertificate {
        
        let cerURL = self.url(forResource: named, withExtension: "cer")!
        let cerData = try! Data(contentsOf: cerURL)
        return SecCertificateCreateWithData(nil, cerData as NSData)!
    }
}


Here is the server certificate in txt form:

-----BEGIN CERTIFICATE-----

MIIFvjCCA6agAwIBAgIITyTh/u1bExowDQYJKoZIhvcNAQENBQAwYjEkMCIGA1UE

CgwbRmluYW5zaWVsbCBJRC1UZWtuaWsgQklEIEFCMRowGAYDVQQLDBFJbmZyYXN0

cnVjdHVyZSBDQTEeMBwGA1UEAwwVQmFua0lEIFNTTCBSb290IENBIHYxMB4XDTEx

MTIwNzEyMzQwN1oXDTM0MTIzMTEyMzQwN1owYjEkMCIGA1UECgwbRmluYW5zaWVs

bCBJRC1UZWtuaWsgQklEIEFCMRowGAYDVQQLDBFJbmZyYXN0cnVjdHVyZSBDQTEe

MBwGA1UEAwwVQmFua0lEIFNTTCBSb290IENBIHYxMIICIjANBgkqhkiG9w0BAQEF

AAOCAg8AMIICCgKCAgEAwVA4snZiSFI3r64LvYu4mOsI42A9aLKEQGq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XWhVr+qYGmFGw1+rwmvDmmSLWNWawYgH4NYxTf8z5hB

iDOdAgilvyiAF8Yl0kCKUB2fAPhRNYlEcN+UP/KL24h/pB+hZ9mvR0tM6nW3HVZa

DrvRz4VihZ8vRi3fYnOAkNE6kZdrrdO7LdBc9yYkfQdTcy0N+Aw7q4TkQ8npomrV

mTKaPhtGhA7VICyRNBVcvyoxr+CY7aRQyHn/C7n/jRsQYxs7uc+msq6jRS4HPK8o

lnF9usWZX6KY+8mweJiTE4uN4ZUUBUtt8WcXXDiK/bxEG2amjPcZ/b4LXwGCJb+a

NWP4+iY6kBKrMANs01pLvtVjUS9RtRrY3cNEOhmKhO0qJSDXhsTcVtpbDr37UTSq

QVw83dReiARPwGdURmmkaheH6z4k6qEUSXuFch0w53UAc+1aBXR1bgyFqMdy7Yxi

b2AYu7wnrHioDWqP6DTkUSUeMB/zqWPM/qx6QNNOcaOcjA==

-----END CERTIFICATE-----


Im pretty sure about the server root certificate is valid because I can add it to the keychain access.


With debug mode, I can pass code line:

let cerURL = self.url(forResource: named, withExtension: "cer")!
let cerData = try! Data(contentsOf: cerURL)


But I will receive the error message "error: Unexpectedly found nil while unwrapping an Optional value" when I trying to pass code line:

        return SecCertificateCreateWithData(nil, cerData as NSData)!

Is there anything I was missing?

Accepted Reply

I'm going to answer my own question, the issue was I did not convert the file format correct. Here is the solution which I found on the internet:
https://stackoverflow.com/a/51742988/11055217
Sometime certificate will not convert to the right format, you need to use OpenSSL command to make the conversion manually.

Replies

I'm going to answer my own question, the issue was I did not convert the file format correct. Here is the solution which I found on the internet:
https://stackoverflow.com/a/51742988/11055217
Sometime certificate will not convert to the right format, you need to use OpenSSL command to make the conversion manually.