Hi all,
I'm working on a macOS project with C interface. And I'm trying to import my private key to a SecKeychainRef, but I always got error code -50. Would you have any advice or suggestion in it? Thanks in advance.
Here is my code:
// Get default keychain
SecKeychainRef import_keychain = NULL;
OSStatus keychain_status = SecKeychainCopyDefault(&import_keychain);
// Create key from ECC key data in X963 format
CFMutableDictionaryRef parameters = CFDictionaryCreateMutable(default_alloc, 0, NULL, NULL);
CFDictionarySetValue(parameters, kSecAttrKeyType, kSecAttrKeyTypeECSECPrimeRandom);
CFDictionarySetValue(parameters, kSecAttrKeyClass, kSecAttrKeyClassPrivate);
CFDictionarySetValue(parameters, kSecAttrApplicationLabel, cfLabel);
SecKeyRef private_key= SecKeyCreateWithData(hard_code_key_ref, parameters, &key_error);
// Add seckey to key chain
CFMutableDictionaryRef secItemParams = CFDictionaryCreateMutable(default_alloc, 0, NULL, NULL);
CFDictionarySetValue(secItemParams, kSecClass, kSecClassKey);
CFDictionarySetValue(secItemParams, kSecValueRef, privateKey);
CFDictionarySetValue(secItemParams, kSecUseKeychain, import_keychain);
OSStatus key_status = SecItemAdd(secItemParams, NULL);
I also tried to test "SecItemAdd" with password value, but it also failed with -25308. I'm not sure if it is related or not. Here is the test code:
CFStringRef server = CFStringCreateWithCString(default_alloc, "example.com", kCFStringEncodingUTF8);
CFStringRef username = CFStringCreateWithCString(default_alloc, "username", kCFStringEncodingUTF8);
CFStringRef password = CFStringCreateWithCString(default_alloc, "password", kCFStringEncodingUTF8);
CFMutableDictionaryRef secItemParams = CFDictionaryCreateMutable(default_alloc, 0, NULL, NULL);
CFDictionarySetValue(secItemParams, kSecClass, kSecClassInternetPassword);
CFDictionarySetValue(secItemParams, kSecValueData, password);
CFDictionarySetValue(secItemParams, kSecAttrAccount, username);
CFDictionarySetValue(secItemParams, kSecAttrServer, server);
CFDictionarySetValue(secItemParams, kSecUseKeychain, import_keychain);
CFDictionarySetValue(secItemParams, kSecAttrAccessible, kSecAttrAccessibleAlways);
OSStatus key_status = SecItemAdd(secItemParams, NULL);
The above code failed with "OSStatus -25308 : User interaction is not allowed." Any advice is welcomed. Thank you!