SecKeychainItem.cpp [plain text]
#include <Security/SecKeychainItem.h>
#include <Security/SecKeychainItemPriv.h>
#include <security_keychain/Keychains.h>
#include <security_keychain/KeyItem.h>
#include <security_keychain/Item.h>
#include <security_keychain/KCCursor.h> // @@@ Remove this when SecKeychainItemFindFirst moves to SecKeychainSearch
#include <securityd_client/dictionary.h>
#include <security_cdsa_utilities/Schema.h>
#include <Security/cssmapplePriv.h>
#include "SecBridge.h"
#include "KCExceptions.h"
#include "Access.h"
#include "SecKeychainItemExtendedAttributes.h"
RefPointer<AclBearer> aclBearer(CFTypeRef itemRef)
{
CFTypeID id = CFGetTypeID(itemRef);
if (id == gTypes().ItemImpl.typeID) {
if (SSGroup group = ItemImpl::required(SecKeychainItemRef(itemRef))->group())
return &*group;
} else if (id == gTypes().KeyItem.typeID) {
if (CssmClient::Key key = KeyItem::required(SecKeyRef(itemRef))->key())
return &*key;
} else if (id == gTypes().KeychainImpl.typeID) {
}
MacOSError::throwMe(errSecNoAccessForItem);
}
CFTypeID
SecKeychainItemGetTypeID(void)
{
BEGIN_SECAPI
return gTypes().ItemImpl.typeID;
END_SECAPI1(_kCFRuntimeNotATypeID)
}
OSStatus
SecKeychainItemCreateFromContent(SecItemClass itemClass, SecKeychainAttributeList *attrList,
UInt32 length, const void *data, SecKeychainRef keychainRef,
SecAccessRef initialAccess, SecKeychainItemRef *itemRef)
{
BEGIN_SECAPI
KCThrowParamErrIf_(length!=0 && data==NULL);
Item item(itemClass, attrList, length, data);
if (initialAccess)
item->setAccess(Access::required(initialAccess));
Keychain keychain = nil;
try
{
keychain = Keychain::optional(keychainRef);
if ( !keychain->exists() )
{
MacOSError::throwMe(errSecNoSuchKeychain); }
}
catch(...)
{
keychain = globals().storageManager.defaultKeychainUI(item);
}
keychain->add(item);
if (itemRef)
*itemRef = item->handle();
END_SECAPI
}
OSStatus
SecKeychainItemModifyContent(SecKeychainItemRef itemRef, const SecKeychainAttributeList *attrList, UInt32 length, const void *data)
{
BEGIN_SECAPI
Item item = ItemImpl::required(itemRef);
item->modifyContent(attrList, length, data);
END_SECAPI
}
OSStatus
SecKeychainItemCopyContent(SecKeychainItemRef itemRef, SecItemClass *itemClass, SecKeychainAttributeList *attrList, UInt32 *length, void **outData)
{
BEGIN_SECAPI
Item item = ItemImpl::required(itemRef);
item->getContent(itemClass, attrList, length, outData);
END_SECAPI
}
OSStatus
SecKeychainItemFreeContent(SecKeychainAttributeList *attrList, void *data)
{
BEGIN_SECAPI
ItemImpl::freeContent(attrList, data);
END_SECAPI
}
OSStatus
SecKeychainItemModifyAttributesAndData(SecKeychainItemRef itemRef, const SecKeychainAttributeList *attrList, UInt32 length, const void *data)
{
BEGIN_SECAPI
Item item = ItemImpl::required(itemRef);
item->modifyAttributesAndData(attrList, length, data);
END_SECAPI
}
OSStatus
SecKeychainItemCopyAttributesAndData(SecKeychainItemRef itemRef, SecKeychainAttributeInfo *info, SecItemClass *itemClass, SecKeychainAttributeList **attrList, UInt32 *length, void **outData)
{
BEGIN_SECAPI
Item item = ItemImpl::required(itemRef);
item->getAttributesAndData(info, itemClass, attrList, length, outData);
END_SECAPI
}
OSStatus
SecKeychainItemFreeAttributesAndData(SecKeychainAttributeList *attrList, void *data)
{
BEGIN_SECAPI
ItemImpl::freeAttributesAndData(attrList, data);
END_SECAPI
}
OSStatus
SecKeychainItemDelete(SecKeychainItemRef itemRef)
{
BEGIN_SECAPI
Item item = ItemImpl::required( itemRef );
Keychain keychain = item->keychain();
KCThrowIf_( !keychain, errSecInvalidItemRef );
OSStatus ortn;
CFArrayRef attrNames = NULL;
ortn = SecKeychainItemCopyAllExtendedAttributes(itemRef, &attrNames, NULL);
if(ortn == noErr) {
CFIndex numAttrs = CFArrayGetCount(attrNames);
for(CFIndex dex=0; dex<numAttrs; dex++) {
CFStringRef attrName = (CFStringRef)CFArrayGetValueAtIndex(attrNames, dex);
SecKeychainItemSetExtendedAttribute(itemRef, attrName, NULL);
}
}
keychain->deleteItem( item );
END_SECAPI
}
OSStatus
SecKeychainItemCopyKeychain(SecKeychainItemRef itemRef, SecKeychainRef* keychainRef)
{
BEGIN_SECAPI
Keychain kc = ItemImpl::required(itemRef)->keychain ();
if (kc == NULL)
{
MacOSError::throwMe(errSecNoSuchKeychain);
}
Required(keychainRef) = kc->handle();
END_SECAPI
}
OSStatus
SecKeychainItemCreateCopy(SecKeychainItemRef itemRef, SecKeychainRef destKeychainRef,
SecAccessRef initialAccess, SecKeychainItemRef *itemCopy)
{
BEGIN_SECAPI
Item copy = ItemImpl::required(itemRef)->copyTo(Keychain::optional(destKeychainRef), Access::optional(initialAccess));
if (itemCopy)
*itemCopy = copy->handle();
END_SECAPI
}
OSStatus
SecKeychainItemGetUniqueRecordID(SecKeychainItemRef itemRef, const CSSM_DB_UNIQUE_RECORD **uniqueRecordID)
{
BEGIN_SECAPI
Required(uniqueRecordID) = ItemImpl::required(itemRef)->dbUniqueRecord();
END_SECAPI
}
OSStatus
SecKeychainItemGetDLDBHandle(SecKeychainItemRef itemRef, CSSM_DL_DB_HANDLE* dldbHandle)
{
BEGIN_SECAPI
*dldbHandle = ItemImpl::required(itemRef)->keychain()->database()->handle();
END_SECAPI
}
OSStatus SecAccessCreateFromObject(CFTypeRef sourceRef,
SecAccessRef *accessRef)
{
BEGIN_SECAPI
Required(accessRef); SecPointer<Access> access = new Access(*aclBearer(sourceRef));
*accessRef = access->handle();
END_SECAPI
}
OSStatus SecAccessModifyObject(SecAccessRef accessRef, CFTypeRef sourceRef)
{
BEGIN_SECAPI
Access::required(accessRef)->setAccess(*aclBearer(sourceRef), true);
END_SECAPI
}
OSStatus
SecKeychainItemCopyAccess(SecKeychainItemRef itemRef, SecAccessRef* accessRef)
{
BEGIN_SECAPI
Required(accessRef); SecPointer<Access> access = new Access(*aclBearer(reinterpret_cast<CFTypeRef>(itemRef)));
*accessRef = access->handle();
END_SECAPI
}
OSStatus
SecKeychainItemSetAccess(SecKeychainItemRef itemRef, SecAccessRef accessRef)
{
BEGIN_SECAPI
Access::required(accessRef)->setAccess(*aclBearer(reinterpret_cast<CFTypeRef>(itemRef)), true);
ItemImpl::required(itemRef)->postItemEvent (kSecUpdateEvent);
END_SECAPI
}
OSStatus SecKeychainItemSetData(SecKeychainItemRef itemRef, UInt32 length, const void* data)
{
BEGIN_SECAPI
ItemImpl::required(itemRef)->setData(length, data);
END_SECAPI
}
OSStatus SecKeychainItemGetData(SecKeychainItemRef itemRef, UInt32 maxLength, void* data, UInt32* actualLength)
{
BEGIN_SECAPI
if (!((data && maxLength) || actualLength))
MacOSError::throwMe(paramErr);
CssmDataContainer aData;
ItemImpl::required(itemRef)->getData(aData);
if (actualLength)
*actualLength = aData.length();
if (data)
{
if (aData.length() > maxLength)
MacOSError::throwMe(errKCBufferTooSmall);
memcpy(data, aData.data(), aData.length());
}
END_SECAPI
}
OSStatus SecKeychainItemUpdate(SecKeychainItemRef itemRef)
{
BEGIN_SECAPI
ItemImpl::required(itemRef)->update();
END_SECAPI
}
OSStatus SecKeychainItemAddNoUI(SecKeychainRef keychainRef, SecKeychainItemRef itemRef)
{
BEGIN_SECAPI
Item item = ItemImpl::required(itemRef);
Keychain::optional(keychainRef)->add(item);
END_SECAPI
}
OSStatus SecKeychainItemAdd(SecKeychainItemRef itemRef)
{
BEGIN_SECAPI
Item item = ItemImpl::required(itemRef);
Keychain defaultKeychain = globals().storageManager.defaultKeychainUI(item);
defaultKeychain->add(item);
END_SECAPI
}
OSStatus SecKeychainItemCreateNew(SecItemClass itemClass, OSType itemCreator, UInt32 length, const void* data, SecKeychainItemRef* itemRef)
{
BEGIN_SECAPI
RequiredParam(itemRef) = Item(itemClass, itemCreator, length, data, false)->handle();
END_SECAPI
}
OSStatus SecKeychainItemGetAttribute(SecKeychainItemRef itemRef, SecKeychainAttribute* attribute, UInt32* actualLength)
{
BEGIN_SECAPI
ItemImpl::required(itemRef)->getAttribute(RequiredParam(attribute), actualLength);
END_SECAPI
}
OSStatus SecKeychainItemSetAttribute(SecKeychainItemRef itemRef, SecKeychainAttribute* attribute)
{
BEGIN_SECAPI
ItemImpl::required(itemRef)->setAttribute(RequiredParam(attribute));
END_SECAPI
}
OSStatus SecKeychainItemFindFirst(SecKeychainRef keychainRef, const SecKeychainAttributeList *attrList, SecKeychainSearchRef *searchRef, SecKeychainItemRef *itemRef)
{
BEGIN_SECAPI
KCCursor cursor;
if (keychainRef)
cursor = KeychainImpl::required(keychainRef)->createCursor(attrList);
else
cursor = globals().storageManager.createCursor(attrList);
Item item;
if (!cursor->next(item))
return errKCItemNotFound;
*itemRef=item->handle();
if (searchRef)
*searchRef=cursor->handle();
END_SECAPI
}
OSStatus SecKeychainItemCreatePersistentReference(SecKeychainItemRef itemRef, CFDataRef *persistentItemRef)
{
BEGIN_SECAPI
KCThrowParamErrIf_(!itemRef || !persistentItemRef);
Item item = ItemImpl::required(itemRef);
item->copyPersistentReference(*persistentItemRef);
END_SECAPI
}
OSStatus SecKeychainItemCopyFromPersistentReference(CFDataRef persistentItemRef, SecKeychainItemRef *itemRef)
{
BEGIN_SECAPI
KCThrowParamErrIf_(!persistentItemRef || !itemRef);
CssmData dictData((void*)::CFDataGetBytePtr(persistentItemRef), ::CFDataGetLength(persistentItemRef));
NameValueDictionary dict(dictData);
Keychain keychain;
Item item;
if (dict.FindByName(SSUID_KEY) != 0)
{
DLDbIdentifier dlDbIdentifier = NameValueDictionary::MakeDLDbIdentifierFromNameValueDictionary(dict);
DLDbIdentifier newDlDbIdentifier(dlDbIdentifier.ssuid(),
DLDbListCFPref::ExpandTildesInPath(dlDbIdentifier.dbName()).c_str(),
dlDbIdentifier.dbLocation());
keychain = globals().storageManager.keychain(newDlDbIdentifier);
const NameValuePair* aDictItem = dict.FindByName(ITEM_KEY);
if (aDictItem && keychain)
{
PrimaryKey primaryKey(aDictItem->Value());
item = keychain->item(primaryKey);
}
}
KCThrowIf_( !item, errSecItemNotFound );
*itemRef = item->handle();
END_SECAPI
}
OSStatus SecKeychainItemCopyRecordIdentifier(SecKeychainItemRef itemRef, CFDataRef *recordIdentifier)
{
BEGIN_SECAPI
CSSM_DATA data;
RequiredParam (recordIdentifier);
Item item = ItemImpl::required(itemRef);
item->copyRecordIdentifier (data);
*recordIdentifier = ::CFDataCreate(kCFAllocatorDefault, (UInt8*) data.Data, data.Length);
free (data.Data);
END_SECAPI
}
OSStatus
SecKeychainItemCopyFromRecordIdentifier(SecKeychainRef keychainRef,
SecKeychainItemRef *itemRef,
CFDataRef recordIdentifier)
{
BEGIN_SECAPI
RequiredParam (keychainRef);
Keychain keychain = KeychainImpl::optional (keychainRef);
RequiredParam (itemRef);
RequiredParam (recordIdentifier);
Db db(keychain->database());
CSSM_DL_DB_HANDLE dbHandle = db.handle ();
CSSM_DB_UNIQUE_RECORD uniqueRecord;
memset (&uniqueRecord, 0, sizeof (uniqueRecord));
uniqueRecord.RecordIdentifier.Data = (uint8*) CFDataGetBytePtr (recordIdentifier);
uniqueRecord.RecordIdentifier.Length = CFDataGetLength (recordIdentifier);
CSSM_DB_UNIQUE_RECORD_PTR outputUniqueRecordPtr;
CSSM_RETURN result;
result = CSSM_DL_PassThrough (dbHandle, CSSM_APPLECSPDL_DB_CONVERT_RECORD_IDENTIFIER, &uniqueRecord, (void**) &outputUniqueRecordPtr);
KCThrowIf_(result != 0, errSecItemNotFound);
CSSM_DB_RECORD_ATTRIBUTE_DATA attributeData;
memset (&attributeData, 0, sizeof (attributeData));
result = CSSM_DL_DataGetFromUniqueRecordId (dbHandle, outputUniqueRecordPtr, &attributeData, NULL);
KCThrowIf_(result != 0, errSecItemNotFound);
CSSM_DB_RECORDTYPE recordType = attributeData.DataRecordType;
DbUniqueRecord unique(db);
CSSM_DB_UNIQUE_RECORD_PTR *uniquePtr = unique;
*uniquePtr = outputUniqueRecordPtr;
unique->activate ();
Item item = keychain->item (recordType, unique);
if (itemRef)
{
*itemRef = item->handle();
}
END_SECAPI
}
OSStatus SecKeychainItemCreateFromEncryptedContent(SecItemClass itemClass,
UInt32 length, const void *data, SecKeychainRef keychainRef,
SecAccessRef initialAccess, SecKeychainItemRef *itemRef, CFDataRef *localID)
{
BEGIN_SECAPI
KCThrowParamErrIf_(length!=0 && data==NULL);
RequiredParam (localID);
RequiredParam (keychainRef);
Item item(itemClass, (uint32) 0, length, data, true);
if (initialAccess)
item->setAccess(Access::required(initialAccess));
Keychain keychain = Keychain::optional(keychainRef);
if (!keychain->exists())
{
MacOSError::throwMe(errSecNoSuchKeychain); }
item->doNotEncrypt ();
try
{
keychain->add(item);
}
catch (const CommonError &err)
{
if (err.osStatus () == errSecNoSuchClass)
{
if (itemClass == CSSM_DL_DB_RECORD_X509_CERTIFICATE)
{
Db db(keychain->database());
db->createRelation(CSSM_DL_DB_RECORD_X509_CERTIFICATE,
"CSSM_DL_DB_RECORD_X509_CERTIFICATE",
Schema::X509CertificateSchemaAttributeCount,
Schema::X509CertificateSchemaAttributeList,
Schema::X509CertificateSchemaIndexCount,
Schema::X509CertificateSchemaIndexList);
keychain->keychainSchema()->didCreateRelation(
CSSM_DL_DB_RECORD_X509_CERTIFICATE,
"CSSM_DL_DB_RECORD_X509_CERTIFICATE",
Schema::X509CertificateSchemaAttributeCount,
Schema::X509CertificateSchemaAttributeList,
Schema::X509CertificateSchemaIndexCount,
Schema::X509CertificateSchemaIndexList);
keychain->add(item);
}
}
else
{
throw;
}
}
if (itemRef)
*itemRef = item->handle();
CSSM_DATA recordID;
item->copyRecordIdentifier (recordID);
*localID = CFDataCreate(kCFAllocatorDefault, (UInt8*) recordID.Data, recordID.Length);
free (recordID.Data);
END_SECAPI
}
OSStatus SecKeychainItemCopyAttributesAndEncryptedData(SecKeychainItemRef itemRef, SecKeychainAttributeInfo *info,
SecItemClass *itemClass, SecKeychainAttributeList **attrList,
UInt32 *length, void **outData)
{
BEGIN_SECAPI
Item item = ItemImpl::required(itemRef);
item->doNotEncrypt ();
item->getAttributesAndData(info, itemClass, attrList, length, outData);
END_SECAPI
}
OSStatus SecKeychainItemModifyEncryptedData(SecKeychainItemRef itemRef, UInt32 length, const void *data)
{
BEGIN_SECAPI
Item item = ItemImpl::required(itemRef);
item->doNotEncrypt ();
item->modifyAttributesAndData(NULL, length, data);
END_SECAPI
}