I'm trying to develop a quick command line tool that will install a root CA into a MacOS System keychain. This is a chunk of the code that does the job.
SecCertificateRef certificate = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)certificateData);
SecKeychainRef keychain = nil;
OSStatus status = SecKeychainCopyDomainDefault(kSecPreferencesDomainSystem, &keychain);
if(status != errSecSuccess)
{
// log the error
return;
}
status = SecKeychainUnlock(keychain, 0, NULL, TRUE);
if(status != errSecSuccess)
{
// log the error
return;
}
status = SecTrustSettingsSetTrustSettings(certificate, kSecTrustSettingsDomainSystem, NULL);
NSDictionary * addCertificateQuery = @{(id)kSecValueRef: (__bridge id)certificate,
(id)kSecClass: (id)kSecClassCertificate,
(id)kSecAttrLabel: CERT_ATTRIBUTE_LABEL,
(id)kSecUseKeychain: (__bridge id)keychain,
(id)kSecReturnRef: @YES,
};
status = SecItemAdd((__bridge CFDictionaryRef)addCertificateQuery, NULL);
When using the XCode debugger as root, the certificate is correctly added to System Keychain, but when debugging as user (user has Admin privileges) and after getting prompted for a username and password of a user with Admin privileges for some reason SecItemAdd returns error -61: errSecWrPerm (No writing permissions for user).
I've been trying to find some documentation that explains why this happens but still haven't got a clue. So my question is: is there a particular reason why a user with admin privileges cannot add a certificate to System? Is writing on System only reserved to root?
I've also tried changing /Library/Keychains/System.keychain permissions but had the same result.