SecKeychainSearch.cpp [plain text]
#include <Security/SecKeychainSearch.h>
#include <Security/SecKeychainSearchPriv.h>
#include <Security/SecCertificatePriv.h>
#include <security_keychain/KCCursor.h>
#include <security_keychain/Certificate.h>
#include <security_keychain/Item.h>
#include <security_cdsa_utilities/Schema.h>
#include <syslog.h>
#include "SecBridge.h"
CFTypeID
SecKeychainSearchGetTypeID(void)
{
BEGIN_SECAPI
return gTypes().KCCursorImpl.typeID;
END_SECAPI1(_kCFRuntimeNotATypeID)
}
OSStatus
SecKeychainSearchCreateFromAttributes(CFTypeRef keychainOrArray, SecItemClass itemClass, const SecKeychainAttributeList *attrList, SecKeychainSearchRef *searchRef)
{
BEGIN_SECAPI
Required(searchRef);
StorageManager::KeychainList keychains;
globals().storageManager.optionalSearchList(keychainOrArray, keychains);
KCCursor cursor(keychains, itemClass, attrList);
*searchRef = cursor->handle();
END_SECAPI
}
OSStatus
SecKeychainSearchCreateFromAttributesExtended(CFTypeRef keychainOrArray, SecItemClass itemClass, const SecKeychainAttributeList *attrList, CSSM_DB_CONJUNCTIVE dbConjunctive, CSSM_DB_OPERATOR dbOperator, SecKeychainSearchRef *searchRef)
{
BEGIN_SECAPI
Required(searchRef);
StorageManager::KeychainList keychains;
globals().storageManager.optionalSearchList(keychainOrArray, keychains);
KCCursor cursor(keychains, itemClass, attrList, dbConjunctive, dbOperator);
*searchRef = cursor->handle();
END_SECAPI
}
OSStatus
SecKeychainSearchCopyNext(SecKeychainSearchRef searchRef, SecKeychainItemRef *itemRef)
{
BEGIN_SECAPI
RequiredParam(itemRef);
Item item;
KCCursorImpl *itemCursor = KCCursorImpl::required(searchRef);
if (!itemCursor->next(item))
return errSecItemNotFound;
*itemRef=item->handle();
bool itemChecked = false;
do {
SecItemClass tmpItemClass = Schema::itemClassFor(item->recordType());
if (tmpItemClass == kSecCertificateItemClass) {
SecPointer<Certificate> certificate(static_cast<Certificate *>(&*item));
CssmData certData = certificate->data();
CFDataRef data = NULL;
if (certData.Data && certData.Length) {
data = CFDataCreate(NULL, certData.Data, certData.Length);
}
if (!data) {
if (*itemRef) {
CFRelease(*itemRef);
*itemRef = NULL;
}
if (!itemCursor->next(item))
return errSecItemNotFound;
*itemRef=item->handle();
continue;
}
SecKeychainItemRef tmpRef = *itemRef;
*itemRef = (SecKeychainItemRef) SecCertificateCreateWithKeychainItem(NULL, data, tmpRef);
if (data)
CFRelease(data);
if (tmpRef)
CFRelease(tmpRef);
if (NULL == *itemRef) {
if (!itemCursor->next(item))
return errSecItemNotFound;
*itemRef=item->handle();
continue;
}
itemChecked = true;
}
else {
itemChecked = true;
}
} while (!itemChecked);
if (NULL == *itemRef) {
return errSecItemNotFound;
}
END_SECAPI
}