OSStatus UpdateKeyLabel()

in macos/macos.c [336:373]


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

  CFMutableDictionaryRef query = CFDictionaryCreateMutable(
      NULL,
      0,
      &kCFTypeDictionaryKeyCallBacks,
      &kCFTypeDictionaryValueCallBacks);
  CFDictionaryAddValue(query, kSecClass, kSecClassKey);
  CFDictionaryAddValue(query, kSecAttrApplicationTag, cfTag);
  CFDictionaryAddValue(query, kSecAttrLabel, cfLabel);
  CFDictionaryAddValue(query, kSecMatchLimit, kSecMatchLimitOne);

  CFMutableDictionaryRef toUpdate = CFDictionaryCreateMutable(
      kCFAllocatorDefault,
      0,
      &kCFTypeDictionaryKeyCallBacks,
      &kCFTypeDictionaryValueCallBacks);

  CFDictionaryAddValue(toUpdate, kSecAttrLabel, cfNewLabel);

  OSStatus status = SecItemUpdate(query, toUpdate);
  CFRelease((CFTypeRef)query);
  CFRelease((CFTypeRef)toUpdate);
  CFRelease((CFTypeRef)cfTag);
  CFRelease((CFTypeRef)cfLabel);
  CFRelease((CFTypeRef)cfNewLabel);

  return status;
}