]> git.saurik.com Git - apple/security.git/blobdiff - OSX/libsecurity_keychain/lib/SecItem.cpp
Security-59754.80.3.tar.gz
[apple/security.git] / OSX / libsecurity_keychain / lib / SecItem.cpp
index 40122c76dddad75a9019241f804cb01cf347275d..e5b078ad01aa9b189fd1ccc28b3dba9e469d732f 100644 (file)
@@ -41,6 +41,8 @@
 #include "TrustSettingsSchema.h"
 #include <Security/SecTrustPriv.h>
 #include "utilities/array_size.h"
+#include "utilities/SecCFWrappers.h"
+#include "LegacyAPICounts.h"
 
 #include <AssertMacros.h>
 #include <syslog.h>
@@ -70,8 +72,6 @@ OSStatus SecItemAdd_ios(CFDictionaryRef attributes, CFTypeRef *result);
 OSStatus SecItemCopyMatching_ios(CFDictionaryRef query, CFTypeRef *result);
 OSStatus SecItemUpdate_ios(CFDictionaryRef query, CFDictionaryRef attributesToUpdate);
 OSStatus SecItemDelete_ios(CFDictionaryRef query);
-OSStatus SecItemUpdateTokenItems_ios(CFTypeRef tokenID, CFArrayRef tokenItemsAttributes);
-
 
 OSStatus SecItemValidateAppleApplicationGroupAccess(CFStringRef group);
 CFDictionaryRef SecItemCopyTranslatedAttributes(CFDictionaryRef inOSXDict, CFTypeRef itemClass,
@@ -539,6 +539,12 @@ _ConvertNewFormatToOldFormat(
        SecKeychainAttributeList* &attrList
        )
 {
+    // make storage to extract the dictionary items
+    CFIndex itemsInDictionary = CFDictionaryGetCount(dictionaryRef);
+    if (itemsInDictionary > 10000) {
+        return errSecParam;
+    }
+
        // get the keychain attributes array from the data item
        // here's the problem.  On the one hand, we have a dictionary that is purported to contain
        // attributes for our type.  On the other hand, the dictionary may contain items we don't support,
@@ -547,8 +553,6 @@ _ConvertNewFormatToOldFormat(
        // setup the return
        attrList = (SecKeychainAttributeList*) calloc(1, sizeof(SecKeychainAttributeList));
 
-       // make storage to extract the dictionary items
-       CFIndex itemsInDictionary = CFDictionaryGetCount(dictionaryRef);
        std::vector<CFTypeRef> keys(itemsInDictionary);
        std::vector<CFTypeRef> values(itemsInDictionary);
 
@@ -592,7 +596,7 @@ _ConvertNewFormatToOldFormat(
     if(count == 0) {
         attrList->attr = NULL;
     } else {
-        attrList->attr = (SecKeychainAttribute*) malloc(sizeof(SecKeychainAttribute) * count);
+        attrList->attr = (SecKeychainAttribute*) calloc(count, sizeof(SecKeychainAttribute));
 
         // fill out the array
         int resultPointer = 0;
@@ -2362,7 +2366,7 @@ _ReplaceKeychainItem(
 
        // make attribute list for new item (the data is still owned by attrList)
        newAttrList.count = attrList->count;
-       newAttrList.attr = (SecKeychainAttribute *) malloc(sizeof(SecKeychainAttribute) * attrList->count);
+       newAttrList.attr = (SecKeychainAttribute *) calloc(attrList->count, sizeof(SecKeychainAttribute));
        int i, newCount;
        for (i=0, newCount=0; i < attrList->count; i++) {
                if (attrList->attr[i].length > 0) {
@@ -3822,6 +3826,8 @@ AddItemResults(SecKeychainItemRef item,
        //
        // Note that we allocate *items if needed.
 
+       CFTypeRef localResult = NULL;
+
        if (!item || !itemParams || !result)
                return errSecParam;
 
@@ -3850,7 +3856,8 @@ AddItemResults(SecKeychainItemRef item,
                        CFArrayAppendValue(itemArray, itemRef);
                }
                else {
-                       *result = CFRetain((CFTypeRef)itemRef);
+                       CFReleaseNull(localResult);
+                       localResult = CFRetain((CFTypeRef)itemRef);
                }
        }
 
@@ -3869,7 +3876,8 @@ AddItemResults(SecKeychainItemRef item,
                                CFArrayAppendValue(itemArray, persistentRef);
                        }
                        else {
-                               *result = CFRetain(persistentRef);
+                               CFReleaseNull(localResult);
+                               localResult = CFRetain(persistentRef);
                        }
                        CFRelease(persistentRef);
                }
@@ -3893,7 +3901,8 @@ AddItemResults(SecKeychainItemRef item,
                                        CFArrayAppendValue(itemArray, dataRef);
                                }
                                else {
-                                       *result = CFRetain(dataRef);
+                                       CFReleaseNull(localResult);
+                                       localResult = CFRetain(dataRef);
                                }
                                CFRelease(dataRef);
                                status = errSecSuccess;
@@ -3915,7 +3924,8 @@ AddItemResults(SecKeychainItemRef item,
                                        CFArrayAppendValue(itemArray, dataRef);
                                }
                                else {
-                                       *result = CFRetain(dataRef);
+                                       CFReleaseNull(localResult);
+                                       localResult = CFRetain(dataRef);
                                }
                                CFRelease(dataRef);
                                (void) SecKeychainItemFreeContent(NULL, data);
@@ -3944,7 +3954,8 @@ AddItemResults(SecKeychainItemRef item,
                                CFArrayAppendValue(itemArray, attrsDict);
                        }
                        else {
-                               *result = CFRetain(attrsDict);
+                               CFReleaseNull(localResult);
+                               localResult = CFRetain(attrsDict);
                        }
                        CFRelease(attrsDict);
                }
@@ -3957,14 +3968,22 @@ AddItemResults(SecKeychainItemRef item,
                if (itemArray) {
                        CFArrayAppendValue(itemArray, itemDict);
                        CFRelease(itemDict);
-                       *result = itemArray;
+                       CFReleaseNull(localResult);
+                       localResult = itemArray;
                }
                else {
-                       *result = itemDict;
+                       CFReleaseNull(localResult);
+                       localResult = itemDict;
                }
        }
        else if (itemArray) {
-               *result = itemArray;
+               CFReleaseNull(localResult);
+               localResult = itemArray;
+       }
+
+       if (localResult) {
+               *result = localResult;
+               localResult = NULL;
        }
 
        return status;
@@ -4305,7 +4324,7 @@ SecItemCreateFromAttributeDictionary_osx(CFDictionaryRef refAttributes) {
                CFTypeRef v;
 
     Item item = Item(item_class, &attrs, 0, "");
-               v = CFDictionaryGetValue(refAttributes, kSecValuePersistentRef);
+               v = CFCast(CFData, CFDictionaryGetValue(refAttributes, kSecValuePersistentRef));
                if (v) {
                        item->setPersistentRef((CFDataRef)v);
                }
@@ -5077,14 +5096,6 @@ SecItemDelete(CFDictionaryRef query)
        return status;
 }
 
-OSStatus
-SecItemUpdateTokenItems(CFTypeRef tokenID, CFArrayRef tokenItemsAttributes)
-{
-       OSStatus status = SecItemUpdateTokenItems_ios(tokenID, tokenItemsAttributes);
-       secitemlog(LOG_NOTICE, "SecItemUpdateTokenItems_ios result: %d", status);
-       return status;
-}
-
 OSStatus
 SecItemCopyMatching_osx(
        CFDictionaryRef query,
@@ -5095,6 +5106,8 @@ SecItemCopyMatching_osx(
        else
                *result = NULL;
 
+    setCountLegacyAPIEnabledForThread(false);
+
        CFAllocatorRef allocator = CFGetAllocator(query);
        CFIndex matchCount = 0;
        CFMutableArrayRef itemArray = NULL;
@@ -5139,20 +5152,9 @@ error_exit:
        }
        _FreeSecItemParams(itemParams);
 
-       return status;
-}
+    setCountLegacyAPIEnabledForThread(true);
 
-OSStatus
-SecItemCopyDisplayNames(
-       CFArrayRef items,
-       CFArrayRef *displayNames)
-{
-    BEGIN_SECAPI
-       Required(items);
-       Required(displayNames);
-    //%%%TBI
-    return errSecUnimplemented;
-    END_SECAPI
+       return status;
 }
 
 OSStatus
@@ -5165,6 +5167,8 @@ SecItemAdd_osx(
        else if (result)
                *result = NULL;
 
+    setCountLegacyAPIEnabledForThread(false);
+
        CFAllocatorRef allocator = CFGetAllocator(attributes);
        CFMutableArrayRef itemArray = NULL;
        SecKeychainItemRef item = NULL;
@@ -5298,6 +5302,7 @@ error_exit:
                *result = NULL;
        }
        _FreeSecItemParams(itemParams);
+    setCountLegacyAPIEnabledForThread(true);
 
        return status;
 }
@@ -5325,6 +5330,8 @@ SecItemUpdate_osx(
                CFRelease(results);
        }
 
+    setCountLegacyAPIEnabledForThread(false);
+
        OSStatus result = errSecSuccess;
        CFIndex ix, count = CFArrayGetCount(items);
        for (ix=0; ix < count; ix++) {
@@ -5335,6 +5342,8 @@ SecItemUpdate_osx(
                }
        }
 
+    setCountLegacyAPIEnabledForThread(true);
+
        if (items) {
                CFRelease(items);
        }
@@ -5363,6 +5372,8 @@ SecItemDelete_osx(
                CFRelease(results);
        }
 
+    setCountLegacyAPIEnabledForThread(false);
+
        OSStatus result = errSecSuccess;
        CFIndex ix, count = CFArrayGetCount(items);
        for (ix=0; ix < count; ix++) {
@@ -5378,6 +5389,8 @@ SecItemDelete_osx(
                }
        }
 
+    setCountLegacyAPIEnabledForThread(true);
+
        if (items)
                CFRelease(items);