-// This only works for symmetric keys; key pairs cannot ever generate a duplicate (due to setting kSecKeyLabel to the hash of the public key)
-static void makeCustomDuplicateKey(const char* name, SecKeychainRef kc, CFStringRef label) {
- CFMutableDictionaryRef query;
-
- query = makeAddKeyDictionary(kc, kSecAttrKeyClassSymmetric, label);
- CFErrorRef error = NULL;
- SecKeyRef item = SecKeyGenerateSymmetric(query, &error);
- is(CFErrorGetCode(error), errSecDuplicateItem, "%s: SecKeyGenerateSymmetric (duplicate) errored: %ld", name, error ? CFErrorGetCode(error) : -1);
-
- CFReleaseNull(query);
-}
-#define makeCustomDuplicateKeyTests 1
-
-static void makeDuplicateKey(const char* name, SecKeychainRef kc) {
- makeCustomDuplicateKey(name, kc, CFSTR("test_key"));
-}
-#define makeDuplicateKeyTests makeCustomDuplicateKeyTests
-
-static SecKeyRef makeCustomFreeKey(const char* name, SecKeychainRef kc, CFStringRef label) {
- SecKeyRef symkey;
-
- ok_status(SecKeyGenerate(
- NULL,
- CSSM_ALGID_AES, 128,
- 0, /* contextHandle */
- CSSM_KEYUSE_ENCRYPT | CSSM_KEYUSE_DECRYPT,
- CSSM_KEYATTR_EXTRACTABLE,
- NULL, /* initialAccess */
- &symkey), "%s: SecKeyGenerate", name);;
-
- CFMutableDictionaryRef query = makeAddKeyDictionary(kc, kSecAttrKeyClassSymmetric, label);
-
- CFMutableArrayRef itemList = (CFMutableArrayRef) CFArrayCreateMutable(kCFAllocatorDefault, 1, &kCFTypeArrayCallBacks);
- CFArrayAppendValue((CFMutableArrayRef)itemList, symkey);
-
- CFDictionarySetValue(query, kSecUseItemList, itemList);
-
- CFTypeRef result = NULL;
- ok_status(SecItemAdd(query, &result), "%s: SecItemAdd", name);
- ok(result != NULL, "%s: SecItemAdd returned a result", name);
- CFReleaseNull(symkey);
- return (SecKeyRef) result;
-}
-#define makeCustomFreeKeyTests 3
-
-static SecKeyRef makeFreeKey(const char* name, SecKeychainRef kc) {
- return makeCustomFreeKey(name, kc, CFSTR("test_free_key"));
-}
-#define makeFreeKeyTests makeCustomFreeKeyTests
-
-static SecKeyRef makeCustomDuplicateFreeKey(const char* name, SecKeychainRef kc, CFStringRef label) {
- SecKeyRef symkey;
-
- ok_status(SecKeyGenerate(
- NULL,
- CSSM_ALGID_AES, 128,
- 0, /* contextHandle */
- CSSM_KEYUSE_ENCRYPT | CSSM_KEYUSE_DECRYPT,
- CSSM_KEYATTR_EXTRACTABLE,
- NULL, /* initialAccess */
- &symkey), "%s: SecKeyGenerate", name);;
-
- CFMutableDictionaryRef query = makeAddKeyDictionary(kc, kSecAttrKeyClassSymmetric, label);
-
- CFMutableArrayRef itemList = (CFMutableArrayRef) CFArrayCreateMutable(kCFAllocatorDefault, 1, &kCFTypeArrayCallBacks);
- CFArrayAppendValue((CFMutableArrayRef)itemList, symkey);
-
- CFDictionarySetValue(query, kSecUseItemList, itemList);
-
- CFTypeRef result = NULL;
- is(SecItemAdd(query, &result), errSecDuplicateItem, "%s: SecItemAdd (duplicate)", name);
- CFReleaseNull(symkey);
- return (SecKeyRef) result;
-}
-#define makeCustomDuplicateFreeKeyTests 2
-
-static SecKeyRef makeDuplicateFreeKey(const char* name, SecKeychainRef kc) {
- return makeCustomFreeKey(name, kc, CFSTR("test_free_key"));
-}
-#define makeDuplicateFreeKeyTests makeCustomDuplicateFreeKeyTests
-
-