ExtendedAttribute.cpp [plain text]
#include "ExtendedAttribute.h"
#include "SecKeychainItemExtendedAttributes.h"
#include "SecKeychainItemPriv.h"
#include "cssmdatetime.h"
#include <security_cdsa_utilities/Schema.h>
using namespace KeychainCore;
ExtendedAttribute::ExtendedAttribute(
CSSM_DB_RECORDTYPE recordType,
const CssmData &itemID,
const CssmData attrName,
const CssmData attrValue) :
ItemImpl((SecItemClass) CSSM_DL_DB_RECORD_EXTENDED_ATTRIBUTE,
reinterpret_cast<SecKeychainAttributeList *>(NULL),
0, NULL),
mRecordType(recordType),
mItemID(Allocator::standard(), itemID.Data, itemID.Length),
mAttrName(Allocator::standard(), attrName.Data, attrName.Length),
mAttrValue(Allocator::standard(), attrValue.Data, attrValue.Length)
{
setupAttrs();
}
ExtendedAttribute::ExtendedAttribute(
const Keychain &keychain,
const PrimaryKey &primaryKey,
const CssmClient::DbUniqueRecord &uniqueId) :
ItemImpl(keychain, primaryKey, uniqueId),
mRecordType(0),
mItemID(Allocator::standard()),
mAttrName(Allocator::standard()),
mAttrValue(Allocator::standard())
{
}
ExtendedAttribute::ExtendedAttribute(
const Keychain &keychain,
const PrimaryKey &primaryKey) :
ItemImpl(keychain, primaryKey),
mRecordType(0),
mItemID(Allocator::standard()),
mAttrName(Allocator::standard()),
mAttrValue(Allocator::standard())
{
}
ExtendedAttribute* ExtendedAttribute::make(const Keychain &keychain, const PrimaryKey &primaryKey, const CssmClient::DbUniqueRecord &uniqueId)
{
ExtendedAttribute* ea = new ExtendedAttribute(keychain, primaryKey, uniqueId);
keychain->addItem(primaryKey, ea);
return ea;
}
ExtendedAttribute* ExtendedAttribute::make(const Keychain &keychain, const PrimaryKey &primaryKey)
{
ExtendedAttribute* ea = new ExtendedAttribute(keychain, primaryKey);
keychain->addItem(primaryKey, ea);
return ea;
}
ExtendedAttribute::ExtendedAttribute(
ExtendedAttribute &extendedAttr) :
ItemImpl(extendedAttr),
mRecordType(extendedAttr.mRecordType),
mItemID(Allocator::standard()),
mAttrName(Allocator::standard()),
mAttrValue(Allocator::standard())
{
mItemID.copy(extendedAttr.mItemID);
mAttrName.copy(extendedAttr.mAttrName);
mAttrValue.copy(extendedAttr.mAttrValue);
setupAttrs();
}
ExtendedAttribute::~ExtendedAttribute() _NOEXCEPT
{
}
PrimaryKey
ExtendedAttribute::add(Keychain &keychain)
{
StLock<Mutex>_(mMutex);
if (mKeychain)
MacOSError::throwMe(errSecDuplicateItem);
SInt64 date;
CSSMDateTimeUtils::GetCurrentMacLongDateTime(date);
CssmDbAttributeInfo attrInfo(kSecModDateItemAttr, CSSM_DB_ATTRIBUTE_FORMAT_TIME_DATE);
setAttribute(attrInfo, date);
Db db(keychain->database());
try
{
mUniqueId = db->insert(CSSM_DL_DB_RECORD_EXTENDED_ATTRIBUTE, mDbAttributes.get(), mData.get());
}
catch (const CssmError &e)
{
if (e.osStatus() != CSSMERR_DL_INVALID_RECORDTYPE)
throw;
db->createRelation(CSSM_DL_DB_RECORD_EXTENDED_ATTRIBUTE,
"CSSM_DL_DB_RECORD_EXTENDED_ATTRIBUTE",
Schema::ExtendedAttributeSchemaAttributeCount,
Schema::ExtendedAttributeSchemaAttributeList,
Schema::ExtendedAttributeSchemaIndexCount,
Schema::ExtendedAttributeSchemaIndexList);
keychain->keychainSchema()->didCreateRelation(
CSSM_DL_DB_RECORD_EXTENDED_ATTRIBUTE,
"CSSM_DL_DB_RECORD_EXTENDED_ATTRIBUTE",
Schema::ExtendedAttributeSchemaAttributeCount,
Schema::ExtendedAttributeSchemaAttributeList,
Schema::ExtendedAttributeSchemaIndexCount,
Schema::ExtendedAttributeSchemaIndexList);
mUniqueId = db->insert(CSSM_DL_DB_RECORD_EXTENDED_ATTRIBUTE, mDbAttributes.get(), mData.get());
}
mPrimaryKey = keychain->makePrimaryKey(CSSM_DL_DB_RECORD_EXTENDED_ATTRIBUTE, mUniqueId);
mKeychain = keychain;
return mPrimaryKey;
}
void ExtendedAttribute::setupAttrs()
{
StLock<Mutex>_(mMutex);
CssmDbAttributeInfo attrInfo1(kExtendedAttrRecordTypeAttr, CSSM_DB_ATTRIBUTE_FORMAT_UINT32);
setAttribute(attrInfo1, (uint32)mRecordType);
CssmData cd = mItemID;
CssmDbAttributeInfo attrInfo2(kExtendedAttrItemIDAttr, CSSM_DB_ATTRIBUTE_FORMAT_BLOB);
setAttribute(attrInfo2, cd);
cd = mAttrName;
CssmDbAttributeInfo attrInfo3(kExtendedAttrAttributeNameAttr, CSSM_DB_ATTRIBUTE_FORMAT_BLOB);
setAttribute(attrInfo3, cd);
cd = mAttrValue;
CssmDbAttributeInfo attrInfo4(kExtendedAttrAttributeValueAttr, CSSM_DB_ATTRIBUTE_FORMAT_BLOB);
setAttribute(attrInfo4, cd);
}