/*
- * Copyright (c) 2000-2004,2011-2015 Apple Inc. All Rights Reserved.
+ * Copyright (c) 2000-2004,2011-2016 Apple Inc. All Rights Reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
  *
 #include <security_keychain/Item.h>
 #include <security_cdsa_utilities/Schema.h>
 #include <syslog.h>
+#include <os/activity.h>
 
 #include "SecBridge.h"
 
 SecKeychainSearchCreateFromAttributes(CFTypeRef keychainOrArray, SecItemClass itemClass, const SecKeychainAttributeList *attrList, SecKeychainSearchRef *searchRef)
 {
     BEGIN_SECAPI
+    os_activity_t activity = os_activity_create("SecKeychainSearchCreateFromAttributes", OS_ACTIVITY_CURRENT, OS_ACTIVITY_FLAG_IF_NONE_PRESENT);
+    os_activity_scope(activity);
+    os_release(activity);
 
        Required(searchRef);
 
 SecKeychainSearchCreateFromAttributesExtended(CFTypeRef keychainOrArray, SecItemClass itemClass, const SecKeychainAttributeList *attrList, CSSM_DB_CONJUNCTIVE dbConjunctive, CSSM_DB_OPERATOR dbOperator, SecKeychainSearchRef *searchRef)
 {
     BEGIN_SECAPI
+    os_activity_t activity = os_activity_create("SecKeychainSearchCreateFromAttributesExtended", OS_ACTIVITY_CURRENT, OS_ACTIVITY_FLAG_IF_NONE_PRESENT);
+    os_activity_scope(activity);
+    os_release(activity);
 
        Required(searchRef); // Make sure that searchRef is an invalid SearchRef
 
 OSStatus
 SecKeychainSearchCopyNext(SecKeychainSearchRef searchRef, SecKeychainItemRef *itemRef)
 {
-    BEGIN_SECAPI
+       BEGIN_SECAPI
+    os_activity_t activity = os_activity_create("SecKeychainSearchCopyNext", OS_ACTIVITY_CURRENT, OS_ACTIVITY_FLAG_IF_NONE_PRESENT);
+    os_activity_scope(activity);
+    os_release(activity);
 
        RequiredParam(itemRef);
        Item item;
 
        *itemRef=item->handle();
 
-#if SECTRUST_OSX
        bool itemChecked = false;
        do {
                /* see if we should convert outgoing item to a unified SecCertificateRef */
                        }
                        if (!data) {
                                /* zero-length or otherwise bad cert data; skip to next item */
-                               CFRelease(*itemRef);
-                               *itemRef = NULL;
+                               if (*itemRef) {
+                                       CFRelease(*itemRef);
+                                       *itemRef = NULL;
+                               }
                                if (!itemCursor->next(item))
                                        return errSecItemNotFound;
                                *itemRef=item->handle();
                                CFRelease(data);
                        if (tmpRef)
                                CFRelease(tmpRef);
+                       if (NULL == *itemRef) {
+                               /* unable to create unified certificate item; skip to next item */
+                               if (!itemCursor->next(item))
+                                       return errSecItemNotFound;
+                               *itemRef=item->handle();
+                               continue;
+                       }
                        itemChecked = true;
-        }
+               }
                else {
                        itemChecked = true;
                }
        } while (!itemChecked);
-#endif
+
+       if (NULL == *itemRef) {
+               /* never permit a NULL item reference to be returned without an error result */
+               return errSecItemNotFound;
+       }
 
        END_SECAPI
 }