OSStatus DeleteKey()

in macos/macos.c [249:274]


OSStatus DeleteKey(const char* label, const char* tag, unsigned char* hash) {
  CFDataRef cfTag = StringToDataRef(tag);
  CFStringRef cfLabel = CFStringCreateWithCString(
      kCFAllocatorDefault, label, kCFStringEncodingUTF8);

  CFMutableDictionaryRef query =
      CFDictionaryCreateMutable(kCFAllocatorDefault, 0, NULL, NULL);
  CFDictionaryAddValue(query, kSecClass, kSecClassKey);
  CFDictionaryAddValue(query, kSecAttrKeyType, kSecAttrKeyTypeEC);
  CFDictionaryAddValue(query, kSecAttrApplicationTag, cfTag);
  CFDictionaryAddValue(query, kSecAttrLabel, cfLabel);
  CFDictionaryAddValue(query, kSecAttrKeyClass, kSecAttrKeyClassPrivate);

  if (hash) {
    CFDataRef h = CFDataCreateWithBytesNoCopy(
        kCFAllocatorDefault, (UInt8*)hash, 20, kCFAllocatorNull);
    CFDictionaryAddValue(query, kSecAttrApplicationLabel, h);
  }

  OSStatus res;
  do {
    res = SecItemDelete(query);
  } while (res == errSecDuplicateItem);

  return res;
}