How to check if a Certificate is installed and trusted on iOS

2.3k views Asked by At

I've an app which prompts the user to download and install a Configuration Profile. The profile contains a Root CA embedded inside it. I want to check if the Configuration Profile is installed on the device, after it got downloaded.

After going through the Apple Developer Forums, I realised that one way to do this is to check if the Certificate embedded in the profile is installed and trusted by the user. If it is, it would implicitly mean (with exceptions) that the Configuration profile was installed by the user.

I went through this link where the OP had similar requirement but apparently it is not able to detect if the certificate is already installed.

Does anybody have experience doing this?

1

There are 1 answers

2
iUrii On

You cant use SecTrustEvaluateAsyncWithError to recognise whether the certificate is installed(trusted) on not e.g.:

// Load cert
guard let filePath = Bundle.main.path(forResource: "your_cert", ofType: "crt"),
      let data = try? Data(contentsOf: URL(fileURLWithPath: filePath)),
      let certificate = SecCertificateCreateWithData(nil, data as CFData)
else {
    return
}

// Check
var secTrust: SecTrust?
if SecTrustCreateWithCertificates(certificate, SecPolicyCreateBasicX509(), &secTrust) == errSecSuccess, let trust = secTrust {
    SecTrustEvaluateAsyncWithError(trust, .main) { trust, result, error in
        print("Cert is", result ? "installed" : "not installed")
    }
}