We are pushing a root cert via MDM so that the cert and its child certs are implicitly trusted by the device (all iOS devices). But when we try validating cert for a leaf node cert its failing with
kSecTrustResultRecoverableTrustFailure
Error Domain=SecurityTrustEvaluate Code=5 "Root certificate is not trusted." UserInfo={NSLocalizedDescription=Root certificate is not trusted.}
Steps:
- Enroll into MDM
- Push the Root and Intermediate cert to the client as a credential payload
- Our App installs the leaf certificate in its keychain
- It then validates the leaf cert using the following code
SecPolicyRef policy = SecPolicyCreateBasicX509();
SecTrustRef trust;
SecTrustCreateWithCertificates(leafcert, policy, &trust);
SecTrustResultType trustResult;
SecTrustEvaluate(trust, &trustResult);
BOOL trusted = (trustResult == kSecTrustResultUnspecified || trustResult == kSecTrustResultProceed);
Based on apple docs https://developer.apple.com/documentation/security/1399071-sectrustsetanchorcertificatesonl?language=objc
Alternatively tried:
SecPolicyRef policy = SecPolicyCreateBasicX509();
SecTrustRef trust;
SecTrustCreateWithCertificates(leafcert, policy, &trust);
SecTrustSetAnchorCertificates(trust, (CFArrayRef) [NSArray array]);
SecTrustSetAnchorCertificatesOnly(trust, NO);
SecTrustResultType trustResult;
BOOL trusted = (trustResult == kSecTrustResultUnspecified || trustResult == kSecTrustResultProceed);
In both cases it returns !trusted and the error is:
kSecTrustResultRecoverableTrustFailureError Domain=SecurityTrustEvaluate Code=5 "Root certificate is not trusted." UserInfo={NSLocalizedDescription=Root certificate is not trusted.}