2 * Copyright (c) 2000-2004,2011-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
31 #include "Certificate.h"
33 #include "ExtendedAttribute.h"
36 #include <security_cdsa_utilities/Schema.h>
37 #include "KCEventNotifier.h"
38 #include "KCExceptions.h"
39 #include "cssmdatetime.h"
40 #include <security_cdsa_client/keychainacl.h>
41 #include <security_utilities/osxcode.h>
42 #include <security_utilities/trackingallocator.h>
43 #include <Security/SecKeychainItemPriv.h>
44 #include <Security/cssmapple.h>
45 #include <CommonCrypto/CommonDigest.h>
46 #include <utilities/der_plist.h>
48 #include <security_utilities/CSPDLTransaction.h>
49 #include <SecBasePriv.h>
51 #define SENDACCESSNOTIFICATIONS 1
53 //%%% schema indexes should be defined in Schema.h
54 #define _kSecAppleSharePasswordItemClass 'ashp'
55 #define APPLEDB_CSSM_PRINTNAME_ATTRIBUTE 1 /* schema index for label attribute of keys or certificates */
56 #define APPLEDB_GENERIC_PRINTNAME_ATTRIBUTE 7 /* schema index for label attribute of password items */
57 #define IS_PASSWORD_ITEM_CLASS(X) ( (X) == kSecInternetPasswordItemClass || \
58 (X) == kSecGenericPasswordItemClass || \
59 (X) == _kSecAppleSharePasswordItemClass ) ? 1 : 0
61 using namespace KeychainCore
;
62 using namespace CSSMDateTimeUtils
;
68 // NewItemImpl constructor
69 ItemImpl::ItemImpl(SecItemClass itemClass
, OSType itemCreator
, UInt32 length
, const void* data
, bool dontDoAttributes
)
70 : mDbAttributes(new DbAttributes()),
72 secd_PersistentRef(NULL
),
75 mMutex(Mutex::recursive
)
78 mData
= new CssmDataContainer(data
, length
);
80 mDbAttributes
->recordType(Schema::recordTypeFor(itemClass
));
83 mDbAttributes
->add(Schema::attributeInfo(kSecCreatorItemAttr
), itemCreator
);
86 ItemImpl::ItemImpl(SecItemClass itemClass
, SecKeychainAttributeList
*attrList
, UInt32 length
, const void* data
)
87 : mDbAttributes(new DbAttributes()),
89 secd_PersistentRef(NULL
),
92 mMutex(Mutex::recursive
)
95 mData
= new CssmDataContainer(data
, length
);
98 mDbAttributes
->recordType(Schema::recordTypeFor(itemClass
));
102 for(UInt32 i
=0; i
< attrList
->count
; i
++)
104 mDbAttributes
->add(Schema::attributeInfo(attrList
->attr
[i
].tag
), CssmData(attrList
->attr
[i
].data
, attrList
->attr
[i
].length
));
109 // DbItemImpl constructor
110 ItemImpl::ItemImpl(const Keychain
&keychain
, const PrimaryKey
&primaryKey
, const DbUniqueRecord
&uniqueId
)
111 : mUniqueId(uniqueId
), mKeychain(keychain
), mPrimaryKey(primaryKey
),
112 secd_PersistentRef(NULL
), mDoNotEncrypt(false), mInCache(false),
113 mMutex(Mutex::recursive
)
117 // PrimaryKey ItemImpl constructor
118 ItemImpl::ItemImpl(const Keychain
&keychain
, const PrimaryKey
&primaryKey
)
119 : mKeychain(keychain
), mPrimaryKey(primaryKey
), secd_PersistentRef(NULL
), mDoNotEncrypt(false),
121 mMutex(Mutex::recursive
)
125 ItemImpl
* ItemImpl::make(const Keychain
&keychain
, const PrimaryKey
&primaryKey
, const CssmClient::DbUniqueRecord
&uniqueId
)
127 ItemImpl
* ii
= new ItemImpl(keychain
, primaryKey
, uniqueId
);
128 keychain
->addItem(primaryKey
, ii
);
134 ItemImpl
* ItemImpl::make(const Keychain
&keychain
, const PrimaryKey
&primaryKey
)
136 ItemImpl
* ii
= new ItemImpl(keychain
, primaryKey
);
137 keychain
->addItem(primaryKey
, ii
);
143 // Constructor used when copying an item to a keychain.
145 ItemImpl::ItemImpl(ItemImpl
&item
) :
146 mData(item
.modifiedData() ? NULL
: new CssmDataContainer()),
147 mDbAttributes(new DbAttributes()),
149 secd_PersistentRef(NULL
),
150 mDoNotEncrypt(false),
152 mMutex(Mutex::recursive
)
154 mDbAttributes
->recordType(item
.recordType());
156 if (item
.mKeychain
) {
157 // get the entire source item from its keychain. This requires figuring
158 // out the schema for the item based on its record type.
159 fillDbAttributesFromSchema(*mDbAttributes
, item
.recordType());
161 item
.getContent(mDbAttributes
.get(), mData
.get());
164 // @@@ We don't deal with modified attributes.
166 if (item
.modifiedData())
167 // the copied data comes from the source item
168 mData
= new CssmDataContainer(item
.modifiedData()->Data
,
169 item
.modifiedData()->Length
);
172 ItemImpl::~ItemImpl()
174 if (secd_PersistentRef
) {
175 CFRelease(secd_PersistentRef
);
182 ItemImpl::getMutexForObject()
186 return mKeychain
->getKeychainMutex();
194 ItemImpl::aboutToDestruct()
196 if(mKeychain
.get()) {
197 mKeychain
->forceRemoveFromCache(this);
204 ItemImpl::didModify()
206 StLock
<Mutex
>_(mMutex
);
208 mDbAttributes
.reset(NULL
);
212 ItemImpl::defaultAttributeValue(const CSSM_DB_ATTRIBUTE_INFO
&info
)
214 static const uint32 zeroInt
= 0;
215 static const double zeroDouble
= 0.0;
216 static const char timeBytes
[] = "20010101000000Z";
218 static const CSSM_DATA defaultFourBytes
= { 4, (uint8
*) &zeroInt
};
219 static const CSSM_DATA defaultEightBytes
= { 8, (uint8
*) &zeroDouble
};
220 static const CSSM_DATA defaultTime
= { 16, (uint8
*) timeBytes
};
221 static const CSSM_DATA defaultZeroBytes
= { 0, NULL
};
223 switch (info
.AttributeFormat
)
225 case CSSM_DB_ATTRIBUTE_FORMAT_SINT32
:
226 case CSSM_DB_ATTRIBUTE_FORMAT_UINT32
:
227 return defaultFourBytes
;
229 case CSSM_DB_ATTRIBUTE_FORMAT_REAL
:
230 return defaultEightBytes
;
232 case CSSM_DB_ATTRIBUTE_FORMAT_TIME_DATE
:
236 return defaultZeroBytes
;
240 void ItemImpl::fillDbAttributesFromSchema(DbAttributes
& dbAttributes
, CSSM_DB_RECORDTYPE recordType
, Keychain keychain
) {
241 // If we weren't passed a keychain, use our own.
242 keychain
= !!keychain
? keychain
: mKeychain
;
244 // Without a keychain, there's no one to ask.
249 SecKeychainAttributeInfo
* infos
;
250 keychain
->getAttributeInfoForItemID(recordType
, &infos
);
252 secdebugfunc("integrity", "filling %u attributes for type %u", (unsigned int)infos
->count
, recordType
);
254 for (uint32 i
= 0; i
< infos
->count
; i
++) {
255 CSSM_DB_ATTRIBUTE_INFO info
;
256 memset(&info
, 0, sizeof(info
));
258 info
.AttributeNameFormat
= CSSM_DB_ATTRIBUTE_NAME_AS_INTEGER
;
259 info
.Label
.AttributeID
= infos
->tag
[i
];
260 info
.AttributeFormat
= infos
->format
[i
];
262 dbAttributes
.add(info
);
265 keychain
->freeAttributeInfo(infos
);
268 DbAttributes
* ItemImpl::getCurrentAttributes() {
269 DbAttributes
* dbAttributes
;
270 secdebugfunc("integrity", "getting current attributes...");
272 if(mUniqueId
.get()) {
273 // If we have a unique id, there's an item in the database backing us. Ask for its attributes.
274 dbAttributes
= new DbAttributes(mUniqueId
->database(), 1);
275 fillDbAttributesFromSchema(*dbAttributes
, recordType());
276 mUniqueId
->get(dbAttributes
, NULL
);
278 // and fold in any updates.
279 if(mDbAttributes
.get()) {
280 secdebugfunc("integrity", "adding %d attributes from mDbAttributes", mDbAttributes
->size());
281 dbAttributes
->updateWithDbAttributes(&(*mDbAttributes
.get()));
283 } else if (mDbAttributes
.get()) {
284 // We don't have a backing item, so all our attributes are in mDbAttributes. Copy them.
285 secdebugfunc("integrity", "no unique id, using %d attributes from mDbAttributes", mDbAttributes
->size());
286 dbAttributes
= new DbAttributes();
287 dbAttributes
->updateWithDbAttributes(&(*mDbAttributes
.get()));
289 // No attributes at all. We should maybe throw here, but let's not.
290 secdebugfunc("integrity", "no attributes at all");
291 dbAttributes
= new DbAttributes();
293 dbAttributes
->recordType(recordType());
294 // TODO: We don't set semanticInformation. Issue?
300 void ItemImpl::encodeAttributes(CssmOwnedData
&attributeBlob
) {
301 // Sometimes we don't have our attributes. Find them.
302 auto_ptr
<DbAttributes
> dbAttributes(getCurrentAttributes());
303 encodeAttributesFromDictionary(attributeBlob
, dbAttributes
.get());
307 void ItemImpl::encodeAttributesFromDictionary(CssmOwnedData
&attributeBlob
, DbAttributes
* dbAttributes
) {
308 // Create a CFDictionary from dbAttributes and call der_encode_dictionary on it
309 CFRef
<CFMutableDictionaryRef
> attributes
;
310 attributes
.take(CFDictionaryCreateMutable(NULL
, dbAttributes
->size(), &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
));
312 secdebugfunc("integrity", "looking at %d attributes", dbAttributes
->size());
313 // TODO: include record type and semantic information?
315 for(int i
= 0; i
< dbAttributes
->size(); i
++) {
316 CssmDbAttributeData
& data
= dbAttributes
->attributes()[i
];
317 CssmDbAttributeInfo
& datainfo
= data
.info();
319 // Sometimes we need to normalize the info. Calling Schema::attributeInfo is the best way to do that.
320 // There's no avoiding the try-catch structure here, since only some of the names are in Schema::attributeInfo,
321 // but it will only indicate that by throwing.
322 CssmDbAttributeInfo
& actualInfo
= datainfo
;
324 if(datainfo
.nameFormat() == CSSM_DB_ATTRIBUTE_NAME_AS_INTEGER
&& Schema::haveAttributeInfo(datainfo
.intName())) {
325 actualInfo
= Schema::attributeInfo(datainfo
.intName());
328 actualInfo
= datainfo
;
331 // Pull the label/name out of this data
332 CFRef
<CFDataRef
> label
= NULL
;
334 switch(actualInfo
.nameFormat()) {
335 case CSSM_DB_ATTRIBUTE_NAME_AS_STRING
: {
336 const char* stringname
= actualInfo
.stringName();
337 label
.take(CFDataCreate(NULL
, reinterpret_cast<const UInt8
*>(stringname
), strlen(stringname
)));
340 case CSSM_DB_ATTRIBUTE_NAME_AS_OID
: {
341 const CssmOid
& oidname
= actualInfo
.oidName();
342 label
.take(CFDataCreate(NULL
, reinterpret_cast<const UInt8
*>(oidname
.data()), oidname
.length()));
345 case CSSM_DB_ATTRIBUTE_NAME_AS_INTEGER
: {
346 uint32 iname
= actualInfo
.intName();
347 label
.take(CFDataCreate(NULL
, reinterpret_cast<const UInt8
*>(&(iname
)), sizeof(uint32
)));
352 if(data
.size() == 0) {
353 // This attribute doesn't have a value, and so shouldn't be included in the digest.
357 // Do not include the Creation or Modification date attributes in the hash.
358 // Use this complicated method of checking so we'll catch string and integer names.
359 SecKeychainAttrType cdat
= kSecCreationDateItemAttr
;
360 SecKeychainAttrType cmod
= kSecModDateItemAttr
;
361 if((CFDataGetLength(label
) == sizeof(SecKeychainAttrType
)) &&
362 ((memcmp(CFDataGetBytePtr(label
), &cdat
, sizeof(SecKeychainAttrType
)) == 0) ||
363 (memcmp(CFDataGetBytePtr(label
), &cmod
, sizeof(SecKeychainAttrType
)) == 0))) {
367 // Collect the raw data for each value of this CssmDbAttributeData
368 CFRef
<CFMutableArrayRef
> attributeDataContainer
;
369 attributeDataContainer
.take(CFArrayCreateMutable(NULL
, data
.size(), &kCFTypeArrayCallBacks
));
371 for(int j
= 0; j
< data
.size(); j
++) {
372 CssmData
& entry
= data
.values()[j
];
374 CFRef
<CFDataRef
> datadata
= NULL
;
375 switch(actualInfo
.format()) {
376 case CSSM_DB_ATTRIBUTE_FORMAT_BLOB
:
377 case CSSM_DB_ATTRIBUTE_FORMAT_STRING
:
378 case CSSM_DB_ATTRIBUTE_FORMAT_TIME_DATE
:
379 datadata
.take(CFDataCreate(NULL
, reinterpret_cast<const UInt8
*>(data
.values()[j
].data()), data
.values()[j
].length()));
382 case CSSM_DB_ATTRIBUTE_FORMAT_UINT32
: {
383 uint32 x
= entry
.length() == 1 ? *reinterpret_cast<uint8
*>(entry
.Data
) :
384 entry
.length() == 2 ? *reinterpret_cast<uint16
*>(entry
.Data
) :
385 entry
.length() == 4 ? *reinterpret_cast<uint32
*>(entry
.Data
) : 0;
386 datadata
.take(CFDataCreate(NULL
, reinterpret_cast<const UInt8
*>(&x
), sizeof(x
)));
390 case CSSM_DB_ATTRIBUTE_FORMAT_SINT32
: {
391 sint32 x
= entry
.length() == 1 ? *reinterpret_cast<sint8
*>(entry
.Data
) :
392 entry
.length() == 2 ? *reinterpret_cast<sint16
*>(entry
.Data
) :
393 entry
.length() == 4 ? *reinterpret_cast<sint32
*>(entry
.Data
) : 0;
394 datadata
.take(CFDataCreate(NULL
, reinterpret_cast<const UInt8
*>(&x
), sizeof(x
)));
397 // CSSM_DB_ATTRIBUTE_FORMAT_BIG_NUM is unimplemented here but
398 // has some canonicalization requirements, see DbValue.cpp
404 CFArrayAppendValue(attributeDataContainer
, datadata
);
406 CFDictionaryAddValue(attributes
, label
, attributeDataContainer
);
409 // Now that we have a CFDictionary containing a bunch of CFDatas, turn that
413 CFRef
<CFDataRef
> derBlob
;
414 derBlob
.take(CFPropertyListCreateDERData(NULL
, attributes
, &error
));
416 // TODO: How do we check error here?
422 attributeBlob
.length(CFDataGetLength(derBlob
));
423 attributeBlob
.copy(CFDataGetBytePtr(derBlob
), CFDataGetLength(derBlob
));
426 void ItemImpl::computeDigest(CssmOwnedData
&sha2
) {
427 auto_ptr
<DbAttributes
> dbAttributes(getCurrentAttributes());
428 ItemImpl::computeDigestFromDictionary(sha2
, dbAttributes
.get());
431 void ItemImpl::computeDigestFromDictionary(CssmOwnedData
&sha2
, DbAttributes
* dbAttributes
) {
433 CssmAutoData
attributeBlob(Allocator::standard());
434 encodeAttributesFromDictionary(attributeBlob
, dbAttributes
);
436 sha2
.length(CC_SHA256_DIGEST_LENGTH
);
437 CC_SHA256(attributeBlob
.get().data(), static_cast<CC_LONG
>(attributeBlob
.get().length()), sha2
);
438 secdebugfunc("integrity", "finished: %s", sha2
.get().toHex().c_str());
439 } catch (MacOSError mose
) {
440 secdebugfunc("integrity", "MacOSError: %d", (int)mose
.osStatus());
442 secdebugfunc("integrity", "unknown exception");
446 void ItemImpl::addIntegrity(Access
&access
, bool force
) {
447 secdebugfunc("integrity", "called");
449 if(!force
&& (!mKeychain
|| !mKeychain
->hasIntegrityProtection())) {
450 secdebugfunc("integrity", "skipping integrity add due to keychain version\n");
455 CssmAutoData
digest(Allocator::standard());
456 computeDigest(digest
);
458 // First, check if this already has an integrity tag
460 access
.findSpecificAclsForRight(CSSM_ACL_AUTHORIZATION_INTEGRITY
, acls
);
462 if(acls
.size() >= 1) {
463 // Use the existing ACL
465 secdebugfunc("integrity", "previous integrity acl exists; setting integrity");
466 acl
->setIntegrity(digest
.get());
468 // Delete all extra ACLs
469 for(int i
= 1; i
< acls
.size(); i
++) {
470 secdebugfunc("integrity", "extra integrity acls exist; removing %d",i
);
473 } else if(acls
.size() == 0) {
475 secdebugfunc("integrity", "no previous integrity acl exists; making a new one");
476 acl
= new ACL(digest
.get());
481 void ItemImpl::setIntegrity(bool force
) {
482 if(!force
&& (!mKeychain
|| !mKeychain
->hasIntegrityProtection())) {
483 secdebugfunc("integrity", "skipping integrity set due to keychain version");
487 // For Items, only passwords should have integrity
488 if(!(recordType() == CSSM_DL_DB_RECORD_GENERIC_PASSWORD
|| recordType() == CSSM_DL_DB_RECORD_INTERNET_PASSWORD
)) {
492 // If we're not on an SSDb, we shouldn't have integrity
493 Db
db(mKeychain
->database());
494 if (!useSecureStorage(db
)) {
498 setIntegrity(*group(), force
);
501 void ItemImpl::setIntegrity(AclBearer
&bearer
, bool force
) {
502 if(!force
&& (!mKeychain
|| !mKeychain
->hasIntegrityProtection())) {
503 secdebugfunc("integrity", "skipping integrity acl set due to keychain version");
507 SecPointer
<Access
> access
= new Access(bearer
);
509 access
->removeAclsForRight(CSSM_ACL_AUTHORIZATION_PARTITION_ID
);
510 addIntegrity(*access
, force
);
511 access
->setAccess(bearer
, true);
514 bool ItemImpl::checkIntegrity() {
515 // Note: subclasses are responsible for checking themselves.
517 // If we don't have a keychain yet, we don't have any group. Return true?
518 if(!isPersistent()) {
519 secdebugfunc("integrity", "no keychain, integrity is valid?");
523 if(!mKeychain
|| !mKeychain
->hasIntegrityProtection()) {
524 secdebugfunc("integrity", "skipping integrity check due to keychain version");
528 // Collect our SSGroup, if it exists.
530 SSGroup ssGroup
= group();
532 return checkIntegrity(*ssGroup
);
535 // If we don't have an SSGroup, we can't be invalid. return true.
539 bool ItemImpl::checkIntegrity(AclBearer
& aclBearer
) {
540 if(!mKeychain
|| !mKeychain
->hasIntegrityProtection()) {
541 secdebugfunc("integrity", "skipping integrity check due to keychain version");
545 auto_ptr
<DbAttributes
> dbAttributes(getCurrentAttributes());
546 return checkIntegrityFromDictionary(aclBearer
, dbAttributes
.get());
549 bool ItemImpl::checkIntegrityFromDictionary(AclBearer
& aclBearer
, DbAttributes
* dbAttributes
) {
551 AutoAclEntryInfoList aclInfos
;
552 aclBearer
.getAcl(aclInfos
, CSSM_APPLE_ACL_TAG_INTEGRITY
);
554 // We should only expect there to be one integrity tag. If there's not,
555 // take the first one and ignore the rest. We should probably attempt delete
558 AclEntryInfo
&info
= aclInfos
.at(0);
559 auto_ptr
<ACL
> acl(new ACL(info
, Allocator::standard()));
561 for(int i
= 1; i
< aclInfos
.count(); i
++) {
562 secdebugfunc("integrity", "*** DUPLICATE INTEGRITY ACL, something has gone wrong");
565 CssmAutoData
digest(Allocator::standard());
566 computeDigestFromDictionary(digest
, dbAttributes
);
567 if (acl
->integrity() == digest
.get()) {
571 catch (CssmError cssme
) {
572 const char* errStr
= cssmErrorString(cssme
.error
);
573 secdebugfunc("integrity", "caught CssmError: %d %s", (int) cssme
.error
, errStr
);
575 if(cssme
.error
== CSSMERR_CSP_ACL_ENTRY_TAG_NOT_FOUND
) {
576 // TODO: No entry, run migrator?
579 if(cssme
.error
== CSSMERR_CSP_INVALID_ACL_SUBJECT_VALUE
) {
580 // something went horribly wrong with fetching acl.
582 secdebugfunc("integrity", "INVALID ITEM (too many integrity acls)");
585 if(cssme
.error
== CSSMERR_CSP_VERIFY_FAILED
) {
586 secdebugfunc("integrity", "MAC verification failed; something has gone very wrong");
592 secdebugfunc("integrity", "***** INVALID ITEM");
596 PrimaryKey
ItemImpl::addWithCopyInfo (Keychain
&keychain
, bool isCopy
)
598 StLock
<Mutex
>_(mMutex
);
599 // If we already have a Keychain we can't be added.
601 MacOSError::throwMe(errSecDuplicateItem
);
603 // If we don't have any attributes we can't be added.
604 // (this might occur if attempting to add the item twice, since our attributes
605 // and data are set to NULL at the end of this function.)
606 if (!mDbAttributes
.get())
607 MacOSError::throwMe(errSecDuplicateItem
);
609 CSSM_DB_RECORDTYPE recordType
= mDbAttributes
->recordType();
611 // update the creation and update dates on the new item
614 KeychainSchema schema
= keychain
->keychainSchema();
616 GetCurrentMacLongDateTime(date
);
617 if (schema
->hasAttribute(recordType
, kSecCreationDateItemAttr
))
619 setAttribute(schema
->attributeInfoFor(recordType
, kSecCreationDateItemAttr
), date
);
622 if (schema
->hasAttribute(recordType
, kSecModDateItemAttr
))
624 setAttribute(schema
->attributeInfoFor(recordType
, kSecModDateItemAttr
), date
);
628 // If the label (PrintName) attribute isn't specified, set a default label.
629 if (!mDoNotEncrypt
&& !mDbAttributes
->find(Schema::attributeInfo(kSecLabelItemAttr
)))
631 // if doNotEncrypt was set all of the attributes are wrapped in the data blob. Don't calculate here.
632 CssmDbAttributeData
*label
= NULL
;
635 case CSSM_DL_DB_RECORD_GENERIC_PASSWORD
:
636 label
= mDbAttributes
->find(Schema::attributeInfo(kSecServiceItemAttr
));
639 case CSSM_DL_DB_RECORD_APPLESHARE_PASSWORD
:
640 case CSSM_DL_DB_RECORD_INTERNET_PASSWORD
:
641 label
= mDbAttributes
->find(Schema::attributeInfo(kSecServerItemAttr
));
642 // if AppleShare server name wasn't specified, try the server address
643 if (!label
) label
= mDbAttributes
->find(Schema::attributeInfo(kSecAddressItemAttr
));
649 // if all else fails, use the account name.
651 label
= mDbAttributes
->find(Schema::attributeInfo(kSecAccountItemAttr
));
653 if (label
&& label
->size())
654 setAttribute (Schema::attributeInfo(kSecLabelItemAttr
), label
->at
<CssmData
>(0));
657 // get the attributes that are part of the primary key
658 const CssmAutoDbRecordAttributeInfo
&primaryKeyInfos
=
659 keychain
->primaryKeyInfosFor(recordType
);
661 // make sure each primary key element has a value in the item, otherwise
662 // the database will complain. we make a set of the provided attribute infos
663 // to avoid O(N^2) behavior.
665 DbAttributes
*attributes
= mDbAttributes
.get();
666 typedef set
<CssmDbAttributeInfo
> InfoSet
;
671 // make a set of all the attributes in the key
672 for (uint32 i
= 0; i
< attributes
->size(); i
++)
673 infoSet
.insert(attributes
->at(i
).Info
);
675 for (uint32 i
= 0; i
< primaryKeyInfos
.size(); i
++) { // check to make sure all required attributes are in the key
676 InfoSet::const_iterator it
= infoSet
.find(primaryKeyInfos
.at(i
));
678 if (it
== infoSet
.end()) { // not in the key? add the default
679 // we need to add a default value to the item attributes
680 attributes
->add(primaryKeyInfos
.at(i
), defaultAttributeValue(primaryKeyInfos
.at(i
)));
686 mKeychain
= keychain
;
688 Db
db(keychain
->database());
691 mUniqueId
= db
->insertWithoutEncryption (recordType
, NULL
, mData
.get());
693 else if (useSecureStorage(db
))
695 updateSSGroup(db
, recordType
, mData
.get(), keychain
, mAccess
);
696 mAccess
= NULL
; // use them and lose them - TODO: should this only be unset if there's no error in saveToNewSSGroup? Unclear.
700 // add the item to the (regular) db
701 mUniqueId
= db
->insert(recordType
, mDbAttributes
.get(), mData
.get());
704 mPrimaryKey
= keychain
->makePrimaryKey(recordType
, mUniqueId
);
710 // Forget our data and attributes.
712 mDbAttributes
.reset(NULL
);
720 ItemImpl::add (Keychain
&keychain
)
722 return addWithCopyInfo (keychain
, false);
728 ItemImpl::copyTo(const Keychain
&keychain
, Access
*newAccess
)
730 StLock
<Mutex
>_(mMutex
);
733 item
->setAccess(newAccess
);
736 /* Attempt to copy the access from the current item to the newly created one. */
737 SSGroup myGroup
= group();
740 SecPointer
<Access
> access
= new Access(*myGroup
);
741 item
->setAccess(access
);
745 keychain
->addCopy(item
);
752 StLock
<Mutex
>_(mMutex
);
754 MacOSError::throwMe(errSecNoSuchKeychain
);
756 // Don't update if nothing changed.
760 CSSM_DB_RECORDTYPE aRecordType
= recordType();
761 KeychainSchema schema
= mKeychain
->keychainSchema();
763 // Update the modification date on the item if there is a mod date attribute.
764 if (schema
->hasAttribute(aRecordType
, kSecModDateItemAttr
))
767 GetCurrentMacLongDateTime(date
);
768 setAttribute(schema
->attributeInfoFor(aRecordType
, kSecModDateItemAttr
), date
);
771 // Make sure that we have mUniqueId
773 Db
db(mUniqueId
->database());
776 CSSM_DB_RECORD_ATTRIBUTE_DATA attrData
;
777 memset (&attrData
, 0, sizeof (attrData
));
778 attrData
.DataRecordType
= aRecordType
;
780 mUniqueId
->modifyWithoutEncryption(aRecordType
,
783 CSSM_DB_MODIFY_ATTRIBUTE_REPLACE
);
785 else if (useSecureStorage(db
))
787 // Pass mData to updateSSGroup. If we have any data to change (and it's
788 // therefore non-null), it'll save to a new SSGroup; otherwise, it will
789 // update the old ssgroup. This prevents a RAA on attribute update, while
790 // still protecting new data from being decrypted by old SSGroups with
791 // outdated attributes.
792 updateSSGroup(db
, recordType(), mData
.get());
796 mUniqueId
->modify(aRecordType
,
799 CSSM_DB_MODIFY_ATTRIBUTE_REPLACE
);
804 PrimaryKey oldPK
= mPrimaryKey
;
805 mPrimaryKey
= mKeychain
->makePrimaryKey(aRecordType
, mUniqueId
);
807 // Forget our data and attributes.
809 mDbAttributes
.reset(NULL
);
811 // Let the Keychain update what it needs to.
812 mKeychain
->didUpdate(this, oldPK
, mPrimaryKey
);
817 ItemImpl::updateSSGroup(Db
& db
, CSSM_DB_RECORDTYPE recordType
, CssmDataContainer
* newdata
, Keychain keychain
, SecPointer
<Access
> access
)
819 // hhs replaced with the new aclFactory class
820 AclFactory aclFactory
;
821 const AccessCredentials
*nullCred
= aclFactory
.nullCred();
823 secdebugfunc("integrity", "called");
825 bool haveOldUniqueId
= !!mUniqueId
.get();
826 SSDbUniqueRecord
ssUniqueId(NULL
);
827 SSGroup
ssGroup(NULL
);
828 if(haveOldUniqueId
) {
829 ssUniqueId
= SSDbUniqueRecord(dynamic_cast<SSDbUniqueRecordImpl
*>(&(*mUniqueId
)));
830 if (ssUniqueId
.get() == NULL
) {
831 CssmError::throwMe(CSSMERR_CSSM_INVALID_POINTER
);
833 ssGroup
= ssUniqueId
->group();
836 // If we have new data OR no old unique id, save to a new group
837 bool saveToNewSSGroup
= (!!newdata
) || (!haveOldUniqueId
);
839 // If there aren't any attributes, make up some blank ones.
840 if (!mDbAttributes
.get())
842 secdebugfunc("integrity", "making new dbattributes");
843 mDbAttributes
.reset(new DbAttributes());
844 mDbAttributes
->recordType(mPrimaryKey
->recordType());
847 // Add the item to the secure storage db
848 SSDbImpl
* impl
= dynamic_cast<SSDbImpl
*>(&(*db
));
851 CssmError::throwMe(CSSMERR_CSSM_INVALID_POINTER
);
856 TrackingAllocator
allocator(Allocator::standard());
858 if ((!access
) && (haveOldUniqueId
)) {
859 // Copy the ACL from the old group.
860 secdebugfunc("integrity", "copying old ACL");
861 access
= new Access(*(ssGroup
));
863 // We can't copy these over to the new item; they're going to be reset.
864 // Remove them before securityd complains.
865 access
->removeAclsForRight(CSSM_ACL_AUTHORIZATION_PARTITION_ID
);
866 access
->removeAclsForRight(CSSM_ACL_AUTHORIZATION_INTEGRITY
);
867 } else if (!access
) {
868 secdebugfunc("integrity", "setting up new ACL");
869 // create default access controls for the new item
870 CssmDbAttributeData
*data
= mDbAttributes
->find(Schema::attributeInfo(kSecLabelItemAttr
));
871 string printName
= data
? CssmData::overlay(data
->Value
[0]).toString() : "keychain item";
872 access
= new Access(printName
);
874 // special case for "iTools" password - allow anyone to decrypt the item
875 if (recordType
== CSSM_DL_DB_RECORD_GENERIC_PASSWORD
)
877 CssmDbAttributeData
*data
= mDbAttributes
->find(Schema::attributeInfo(kSecServiceItemAttr
));
878 if (data
&& data
->Value
[0].Length
== 6 && !memcmp("iTools", data
->Value
[0].Data
, 6))
880 typedef vector
<SecPointer
<ACL
> > AclSet
;
882 access
->findAclsForRight(CSSM_ACL_AUTHORIZATION_DECRYPT
, acls
);
883 for (AclSet::const_iterator it
= acls
.begin(); it
!= acls
.end(); it
++)
884 (*it
)->form(ACL::allowAllForm
);
888 secdebugfunc("integrity", "passed an Access, use it");
889 // Access is non-null. Do nothing.
892 // If we have an old group and an old mUniqueId, then we're in the middle of an update.
893 // mDbAttributes contains the newest attributes, but not the old ones. Find
894 // them, merge them, and shove them all back into mDbAttributes. This lets
895 // us re-apply them all to the new item.
896 if(haveOldUniqueId
) {
897 mDbAttributes
.reset(getCurrentAttributes());
900 // Create a CSPDL transaction. Note that this does things when it goes out of scope.
901 CSPDLTransaction
transaction(db
);
904 ResourceControlContext prototype
;
905 maker
.initialOwner(prototype
, nullCred
);
907 if(saveToNewSSGroup
) {
908 secdebugfunc("integrity", "saving to a new SSGroup");
910 // If we're updating an item, it has an old group and possibly an
911 // old mUniqueId. Delete these from the database, so we can insert
913 if(haveOldUniqueId
) {
914 secdebugfunc("integrity", "deleting old mUniqueId");
915 mUniqueId
->deleteRecord();
918 secdebugfunc("integrity", "no old mUniqueId");
921 // Create a new SSGroup with temporary access controls
922 SSGroup
newSSGroup(ssDb
, &prototype
);
923 const AccessCredentials
* cred
= maker
.cred();
926 doChange(keychain
, recordType
, ^{
927 mUniqueId
= ssDb
->insert(recordType
, mDbAttributes
.get(),
928 newdata
, newSSGroup
, cred
);
931 // now finalize the access controls on the group
932 addIntegrity(*access
);
933 access
->setAccess(*newSSGroup
, maker
);
935 // We have to reset this after we add the integrity, since it needs the attributes
936 mDbAttributes
.reset(NULL
);
938 transaction
.success();
940 catch (CssmError cssme
) {
941 const char* errStr
= cssmErrorString(cssme
.error
);
942 secdebugfunc("integrity", "caught CssmError during add: %d %s", (int) cssme
.error
, errStr
);
943 newSSGroup
->deleteKey(nullCred
);
946 catch (MacOSError mose
) {
947 secdebugfunc("integrity", "caught MacOSError during add: %d", (int) mose
.osStatus());
948 newSSGroup
->deleteKey(nullCred
);
953 secdebugfunc("integrity", "caught unknown exception during add");
954 // Delete the new SSGroup that we just created
955 newSSGroup
->deleteKey(nullCred
);
959 // Modify the old SSGroup
960 secdebugfunc("integrity", "modifying the existing SSGroup");
963 doChange(keychain
, recordType
, ^{
965 const AccessCredentials
*autoPrompt
= globals().itemCredentials();
966 ssUniqueId
->modify(recordType
,
969 CSSM_DB_MODIFY_ATTRIBUTE_REPLACE
,
973 // Update the integrity on the SSGroup
974 setIntegrity(*ssGroup
);
976 // We have to reset this after we add the integrity, since it needs the attributes
977 mDbAttributes
.reset(NULL
);
979 transaction
.success();
981 catch (CssmError cssme
) {
982 const char* errStr
= cssmErrorString(cssme
.error
);
983 secdebugfunc("integrity", "caught CssmError during modify: %d %s", (int) cssme
.error
, errStr
);
986 catch (MacOSError mose
) {
987 secdebugfunc("integrity", "caught MacOSError during modify: %d", (int) mose
.osStatus());
992 secdebugfunc("integrity", "caught unknown exception during modify");
1000 ItemImpl::doChange(Keychain keychain
, CSSM_DB_RECORDTYPE recordType
, void (^tryChange
) ())
1002 // Insert the record using the newly created group.
1005 } catch (CssmError cssme
) {
1006 // If there's a "duplicate" of this item, it might be an item with corrupt/invalid attributes
1007 // Try to extract the item and check its attributes, then try again if necessary
1008 auto_ptr
<CssmClient::DbAttributes
> primaryKeyAttrs
;
1009 if(cssme
.error
== CSSMERR_DL_INVALID_UNIQUE_INDEX_DATA
) {
1010 secdebugfunc("integrity", "possible duplicate, trying to delete invalid items");
1012 Keychain kc
= (keychain
? keychain
: mKeychain
);
1014 secdebugfunc("integrity", "no valid keychain");
1017 // Only check for corrupt items if the keychain supports them
1018 if((!kc
) || !kc
->hasIntegrityProtection()) {
1019 secdebugfunc("integrity", "skipping integrity check for corrupt items due to keychain support");
1022 primaryKeyAttrs
.reset(getCurrentAttributes());
1023 PrimaryKey pk
= kc
->makePrimaryKey(recordType
, primaryKeyAttrs
.get());
1025 bool tryAgain
= false;
1027 // Because things are lazy, maybe our keychain has a version
1028 // of this item with different attributes. Ask it!
1029 ItemImpl
* maybeItem
= kc
->_lookupItem(pk
);
1031 if(!maybeItem
->checkIntegrity()) {
1032 Item
item(maybeItem
);
1033 kc
->deleteItem(item
);
1037 // Our keychain doesn't know about any item with this primary key, so maybe
1038 // we have a corrupt item in the database. Let's check.
1040 secdebugfunc("integrity", "making a cursor from primary key");
1041 CssmClient::DbCursor cursor
= pk
->createCursor(kc
);
1042 DbUniqueRecord uniqueId
;
1044 StLock
<Mutex
> _mutexLocker(*kc
->getKeychainMutex());
1046 // The item on-disk might have more or different attributes than we do, since we're
1047 // only searching via primary key. Fetch all of its attributes.
1048 auto_ptr
<DbAttributes
>dbDupAttributes (new DbAttributes(kc
->database(), 1));
1049 fillDbAttributesFromSchema(*dbDupAttributes
, recordType
, kc
);
1051 // Occasionally this cursor won't return the item attributes (for an unknown reason).
1052 // However, we know the attributes any item with this primary key should have, so use those instead.
1053 while (cursor
->next(dbDupAttributes
.get(), NULL
, uniqueId
)) {
1054 secdebugfunc("integrity", "got an item...");
1056 SSGroup group
= safer_cast
<SSDbUniqueRecordImpl
&>(*uniqueId
).group();
1057 if(!ItemImpl::checkIntegrityFromDictionary(*group
, dbDupAttributes
.get())) {
1058 secdebugfunc("integrity", "item is invalid! deleting...");
1059 uniqueId
->deleteRecord();
1066 secdebugfunc("integrity", "trying again...");
1069 // We didn't find an invalid item, the duplicate item exception is real
1070 secdebugfunc("integrity", "duplicate item exception is real; throwing it on");
1081 ItemImpl::getClass(SecKeychainAttribute
&attr
, UInt32
*actualLength
)
1083 StLock
<Mutex
>_(mMutex
);
1085 *actualLength
= sizeof(SecItemClass
);
1087 if (attr
.length
< sizeof(SecItemClass
))
1088 MacOSError::throwMe(errSecBufferTooSmall
);
1090 SecItemClass aClass
= Schema::itemClassFor(recordType());
1091 memcpy(attr
.data
, &aClass
, sizeof(SecItemClass
));
1095 ItemImpl::setAttribute(SecKeychainAttribute
& attr
)
1097 StLock
<Mutex
>_(mMutex
);
1098 setAttribute(Schema::attributeInfo(attr
.tag
), CssmData(attr
.data
, attr
.length
));
1102 ItemImpl::recordType()
1104 StLock
<Mutex
>_(mMutex
);
1105 if (mDbAttributes
.get())
1106 return mDbAttributes
->recordType();
1108 return mPrimaryKey
->recordType();
1111 const DbAttributes
*
1112 ItemImpl::modifiedAttributes()
1114 StLock
<Mutex
>_(mMutex
);
1115 return mDbAttributes
.get();
1119 ItemImpl::modifiedData()
1121 StLock
<Mutex
>_(mMutex
);
1126 ItemImpl::setData(UInt32 length
,const void *data
)
1128 StLock
<Mutex
>_(mMutex
);
1129 mData
= new CssmDataContainer(data
, length
);
1133 ItemImpl::setAccess(Access
*newAccess
)
1135 StLock
<Mutex
>_(mMutex
);
1136 mAccess
= newAccess
;
1139 CssmClient::DbUniqueRecord
1140 ItemImpl::dbUniqueRecord()
1142 StLock
<Mutex
>_(mMutex
);
1143 if (!isPersistent()) // is there no database attached?
1145 MacOSError::throwMe(errSecNotAvailable
);
1150 DbCursor
cursor(mPrimaryKey
->createCursor(mKeychain
));
1151 if (!cursor
->next(NULL
, NULL
, mUniqueId
))
1152 MacOSError::throwMe(errSecInvalidItemRef
);
1159 ItemImpl::primaryKey()
1165 ItemImpl::isPersistent()
1171 ItemImpl::isModified()
1173 StLock
<Mutex
>_(mMutex
);
1174 return mData
.get() || mDbAttributes
.get();
1178 ItemImpl::keychain()
1184 ItemImpl::operator < (const ItemImpl
&other
)
1186 if (mData
&& *mData
)
1189 return this < &other
;
1192 return mPrimaryKey
< other
.mPrimaryKey
;
1196 ItemImpl::setAttribute(const CssmDbAttributeInfo
&info
, const CssmPolyData
&data
)
1198 StLock
<Mutex
>_(mMutex
);
1199 if (!mDbAttributes
.get())
1201 mDbAttributes
.reset(new DbAttributes());
1202 mDbAttributes
->recordType(mPrimaryKey
->recordType());
1205 size_t length
= data
.Length
;
1206 const void *buf
= reinterpret_cast<const void *>(data
.Data
);
1207 uint8 timeString
[16];
1209 // XXX This code is duplicated in KCCursorImpl::KCCursorImpl()
1210 // Convert a 4 or 8 byte TIME_DATE to a CSSM_DB_ATTRIBUTE_FORMAT_TIME_DATE
1211 // style attribute value.
1212 if (info
.format() == CSSM_DB_ATTRIBUTE_FORMAT_TIME_DATE
)
1214 if (length
== sizeof(UInt32
))
1216 MacSecondsToTimeString(*reinterpret_cast<const UInt32
*>(buf
), 16, &timeString
);
1220 else if (length
== sizeof(SInt64
))
1222 MacLongDateTimeToTimeString(*reinterpret_cast<const SInt64
*>(buf
), 16, &timeString
);
1228 mDbAttributes
->add(info
, CssmData(const_cast<void*>(buf
), length
));
1232 ItemImpl::modifyContent(const SecKeychainAttributeList
*attrList
, UInt32 dataLength
, const void *inData
)
1234 StLock
<Mutex
>_(mMutex
);
1235 if (!mDbAttributes
.get())
1237 mDbAttributes
.reset(new DbAttributes());
1238 mDbAttributes
->recordType(mPrimaryKey
->recordType());
1241 if(attrList
) // optional
1243 for(UInt32 ix
=0; ix
< attrList
->count
; ix
++)
1245 SecKeychainAttrType attrTag
= attrList
->attr
[ix
].tag
;
1247 if (attrTag
== APPLEDB_CSSM_PRINTNAME_ATTRIBUTE
)
1249 // must remap a caller-supplied kSecKeyPrintName attribute tag for key items, since it isn't in the schema
1250 // (note that this will ultimately match kGenericPrintName in Schema.cpp)
1251 attrTag
= kSecLabelItemAttr
;
1254 mDbAttributes
->add(Schema::attributeInfo(attrTag
), CssmData(attrList
->attr
[ix
].data
, attrList
->attr
[ix
].length
));
1260 mData
= new CssmDataContainer(inData
, dataLength
);
1267 ItemImpl::getContent(SecItemClass
*itemClass
, SecKeychainAttributeList
*attrList
, UInt32
*length
, void **outData
)
1269 StLock
<Mutex
>_(mMutex
);
1270 // If the data hasn't been set we can't return it.
1271 if (!mKeychain
&& outData
)
1273 CssmData
*data
= mData
.get();
1275 MacOSError::throwMe(errSecDataNotAvailable
);
1277 // TODO: need to check and make sure attrs are valid and handle error condition
1281 *itemClass
= Schema::itemClassFor(recordType());
1283 bool getDataFromDatabase
= mKeychain
&& mPrimaryKey
;
1284 if (getDataFromDatabase
) // are we attached to a database?
1288 // get the number of attributes requested by the caller
1289 UInt32 attrCount
= attrList
? attrList
->count
: 0;
1291 // make a DBAttributes structure and populate it
1292 DbAttributes
dbAttributes(mUniqueId
->database(), attrCount
);
1293 for (UInt32 ix
= 0; ix
< attrCount
; ++ix
)
1295 dbAttributes
.add(Schema::attributeInfo(attrList
->attr
[ix
].tag
));
1298 // request the data from the database (since we are a reference "item" and the data is really stored there)
1299 CssmDataContainer itemData
;
1300 getContent(&dbAttributes
, outData
? &itemData
: NULL
);
1302 // retrieve the data from result
1303 for (UInt32 ix
= 0; ix
< attrCount
; ++ix
)
1305 if (dbAttributes
.at(ix
).NumberOfValues
> 0)
1307 attrList
->attr
[ix
].data
= dbAttributes
.at(ix
).Value
[0].Data
;
1308 attrList
->attr
[ix
].length
= (UInt32
)dbAttributes
.at(ix
).Value
[0].Length
;
1310 // We don't want the data released, it is up the client
1311 dbAttributes
.at(ix
).Value
[0].Data
= NULL
;
1312 dbAttributes
.at(ix
).Value
[0].Length
= 0;
1316 attrList
->attr
[ix
].data
= NULL
;
1317 attrList
->attr
[ix
].length
= 0;
1324 *outData
=itemData
.data();
1325 itemData
.Data
= NULL
;
1328 *length
=(UInt32
)itemData
.length();
1329 itemData
.Length
= 0;
1334 getLocalContent(attrList
, length
, outData
);
1337 // Inform anyone interested that we are doing this
1338 #if SENDACCESSNOTIFICATIONS
1341 secdebug("kcnotify", "ItemImpl::getContent(%p, %p, %p, %p) retrieved content",
1342 itemClass
, attrList
, length
, outData
);
1344 KCEventNotifier::PostKeychainEvent(kSecDataAccessEvent
, mKeychain
, this);
1350 ItemImpl::freeContent(SecKeychainAttributeList
*attrList
, void *data
)
1352 Allocator
&allocator
= Allocator::standard(); // @@@ This might not match the one used originally
1354 allocator
.free(data
);
1356 UInt32 attrCount
= attrList
? attrList
->count
: 0;
1357 for (UInt32 ix
= 0; ix
< attrCount
; ++ix
)
1359 allocator
.free(attrList
->attr
[ix
].data
);
1360 attrList
->attr
[ix
].data
= NULL
;
1365 ItemImpl::modifyAttributesAndData(const SecKeychainAttributeList
*attrList
, UInt32 dataLength
, const void *inData
)
1367 StLock
<Mutex
>_(mMutex
);
1369 MacOSError::throwMe(errSecNoSuchKeychain
);
1373 if (!mDbAttributes
.get())
1375 mDbAttributes
.reset(new DbAttributes());
1376 mDbAttributes
->recordType(mPrimaryKey
->recordType());
1379 CSSM_DB_RECORDTYPE recordType
= mDbAttributes
->recordType();
1380 UInt32 attrCount
= attrList
? attrList
->count
: 0;
1381 for (UInt32 ix
= 0; ix
< attrCount
; ix
++)
1383 SecKeychainAttrType attrTag
= attrList
->attr
[ix
].tag
;
1385 if (attrTag
== kSecLabelItemAttr
)
1387 // must remap a caller-supplied label attribute tag for password items, since it isn't in the schema
1388 // (note that this will ultimately match kGenericPrintName in Schema.cpp)
1389 if (IS_PASSWORD_ITEM_CLASS( Schema::itemClassFor(recordType
) ))
1390 attrTag
= APPLEDB_GENERIC_PRINTNAME_ATTRIBUTE
;
1393 CssmDbAttributeInfo info
=mKeychain
->attributeInfoFor(recordType
, attrTag
);
1395 if (attrList
->attr
[ix
].length
|| info
.AttributeFormat
==CSSM_DB_ATTRIBUTE_FORMAT_STRING
|| info
.AttributeFormat
==CSSM_DB_ATTRIBUTE_FORMAT_BLOB
1396 || info
.AttributeFormat
==CSSM_DB_ATTRIBUTE_FORMAT_STRING
|| info
.AttributeFormat
==CSSM_DB_ATTRIBUTE_FORMAT_BIG_NUM
1397 || info
.AttributeFormat
==CSSM_DB_ATTRIBUTE_FORMAT_MULTI_UINT32
)
1398 mDbAttributes
->add(info
, CssmData(attrList
->attr
[ix
].data
, attrList
->attr
[ix
].length
));
1400 mDbAttributes
->add(info
);
1406 mData
= new CssmDataContainer(inData
, dataLength
);
1413 ItemImpl::getAttributesAndData(SecKeychainAttributeInfo
*info
, SecItemClass
*itemClass
,
1414 SecKeychainAttributeList
**attrList
, UInt32
*length
, void **outData
)
1416 StLock
<Mutex
>_(mMutex
);
1417 // If the data hasn't been set we can't return it.
1418 if (!mKeychain
&& outData
)
1420 CssmData
*data
= mData
.get();
1422 MacOSError::throwMe(errSecDataNotAvailable
);
1424 // TODO: need to check and make sure attrs are valid and handle error condition
1426 SecItemClass myItemClass
= Schema::itemClassFor(recordType());
1428 *itemClass
= myItemClass
;
1430 // @@@ This call won't work for floating items (like certificates).
1433 UInt32 attrCount
= info
? info
->count
: 0;
1434 DbAttributes
dbAttributes(mUniqueId
->database(), attrCount
);
1435 for (UInt32 ix
= 0; ix
< attrCount
; ix
++)
1437 CssmDbAttributeData
&record
= dbAttributes
.add();
1438 record
.Info
.AttributeNameFormat
=CSSM_DB_ATTRIBUTE_NAME_AS_INTEGER
;
1439 record
.Info
.Label
.AttributeID
=info
->tag
[ix
];
1441 if (record
.Info
.Label
.AttributeID
== kSecLabelItemAttr
)
1443 // must remap a caller-supplied label attribute tag for password items, since it isn't in the schema
1444 if (IS_PASSWORD_ITEM_CLASS( myItemClass
))
1445 record
.Info
.Label
.AttributeID
= APPLEDB_GENERIC_PRINTNAME_ATTRIBUTE
;
1449 CssmDataContainer itemData
;
1450 getContent(&dbAttributes
, outData
? &itemData
: NULL
);
1452 if (info
&& attrList
)
1454 SecKeychainAttributeList
*theList
=reinterpret_cast<SecKeychainAttributeList
*>(malloc(sizeof(SecKeychainAttributeList
)));
1455 SecKeychainAttribute
*attr
=reinterpret_cast<SecKeychainAttribute
*>(malloc(sizeof(SecKeychainAttribute
)*attrCount
));
1456 theList
->count
=attrCount
;
1459 for (UInt32 ix
= 0; ix
< attrCount
; ++ix
)
1461 attr
[ix
].tag
=info
->tag
[ix
];
1463 if (dbAttributes
.at(ix
).NumberOfValues
> 0)
1465 attr
[ix
].data
= dbAttributes
.at(ix
).Value
[0].Data
;
1466 attr
[ix
].length
= (UInt32
)dbAttributes
.at(ix
).Value
[0].Length
;
1468 // We don't want the data released, it is up the client
1469 dbAttributes
.at(ix
).Value
[0].Data
= NULL
;
1470 dbAttributes
.at(ix
).Value
[0].Length
= 0;
1474 attr
[ix
].data
= NULL
;
1475 attr
[ix
].length
= 0;
1483 *outData
=itemData
.data();
1486 if (length
) *length
=(UInt32
)itemData
.length();
1489 #if SENDACCESSNOTIFICATIONS
1490 secdebug("kcnotify", "ItemImpl::getAttributesAndData(%p, %p, %p, %p, %p) retrieved data",
1491 info
, itemClass
, attrList
, length
, outData
);
1493 KCEventNotifier::PostKeychainEvent(kSecDataAccessEvent
, mKeychain
, this);
1500 ItemImpl::freeAttributesAndData(SecKeychainAttributeList
*attrList
, void *data
)
1502 Allocator
&allocator
= Allocator::standard(); // @@@ This might not match the one used originally
1505 allocator
.free(data
);
1509 for (UInt32 ix
= 0; ix
< attrList
->count
; ++ix
)
1511 allocator
.free(attrList
->attr
[ix
].data
);
1513 free(attrList
->attr
);
1519 ItemImpl::getAttribute(SecKeychainAttribute
& attr
, UInt32
*actualLength
)
1521 StLock
<Mutex
>_(mMutex
);
1522 if (attr
.tag
== kSecClassItemAttr
)
1523 return getClass(attr
, actualLength
);
1525 if (mDbAttributes
.get())
1527 CssmDbAttributeData
*data
= mDbAttributes
->find(Schema::attributeInfo(attr
.tag
));
1530 getAttributeFrom(data
, attr
, actualLength
);
1536 MacOSError::throwMe(errSecNoSuchAttr
);
1539 DbAttributes
dbAttributes(mUniqueId
->database(), 1);
1540 dbAttributes
.add(Schema::attributeInfo(attr
.tag
));
1541 mUniqueId
->get(&dbAttributes
, NULL
);
1542 getAttributeFrom(&dbAttributes
.at(0), attr
, actualLength
);
1546 ItemImpl::getAttributeFrom(CssmDbAttributeData
*data
, SecKeychainAttribute
&attr
, UInt32
*actualLength
)
1548 StLock
<Mutex
>_(mMutex
);
1549 static const uint32 zero
= 0;
1551 const void *buf
= NULL
;
1553 // Temporary storage for buf.
1563 else if (data
->size() < 1) // Attribute has no values.
1565 if (data
->format() == CSSM_DB_ATTRIBUTE_FORMAT_SINT32
1566 || data
->format() == CSSM_DB_ATTRIBUTE_FORMAT_UINT32
)
1568 length
= sizeof(zero
);
1571 else if (CSSM_DB_ATTRIBUTE_FORMAT_TIME_DATE
)
1572 length
= 0; // Should we throw here?
1573 else // All other formats
1576 else // Get the first value
1578 length
= (UInt32
)data
->Value
[0].Length
;
1579 buf
= data
->Value
[0].Data
;
1581 if (data
->format() == CSSM_DB_ATTRIBUTE_FORMAT_SINT32
)
1583 if (attr
.length
== sizeof(sint8
))
1585 length
= attr
.length
;
1586 svalue8
= sint8(*reinterpret_cast<const sint32
*>(buf
));
1589 else if (attr
.length
== sizeof(sint16
))
1591 length
= attr
.length
;
1592 svalue16
= sint16(*reinterpret_cast<const sint32
*>(buf
));
1596 else if (data
->format() == CSSM_DB_ATTRIBUTE_FORMAT_UINT32
)
1598 if (attr
.length
== sizeof(uint8
))
1600 length
= attr
.length
;
1601 uvalue8
= uint8(*reinterpret_cast<const uint32
*>(buf
));
1604 else if (attr
.length
== sizeof(uint16
))
1606 length
= attr
.length
;
1607 uvalue16
= uint16(*reinterpret_cast<const uint32
*>(buf
));
1611 else if (data
->format() == CSSM_DB_ATTRIBUTE_FORMAT_TIME_DATE
)
1613 if (attr
.length
== sizeof(uint32
))
1615 TimeStringToMacSeconds(data
->Value
[0], macSeconds
);
1617 length
= attr
.length
;
1619 else if (attr
.length
== sizeof(sint64
))
1621 TimeStringToMacLongDateTime(data
->Value
[0], macLDT
);
1623 length
= attr
.length
;
1629 *actualLength
= length
;
1633 if (attr
.length
< length
)
1634 MacOSError::throwMe(errSecBufferTooSmall
);
1636 memcpy(attr
.data
, buf
, length
);
1641 ItemImpl::getData(CssmDataContainer
& outData
)
1643 StLock
<Mutex
>_(mMutex
);
1646 CssmData
*data
= mData
.get();
1647 // If the data hasn't been set we can't return it.
1649 MacOSError::throwMe(errSecDataNotAvailable
);
1655 getContent(NULL
, &outData
);
1657 #if SENDACCESSNOTIFICATIONS
1658 secdebug("kcnotify", "ItemImpl::getData retrieved data");
1660 //%%%<might> be done elsewhere, but here is good for now
1661 KCEventNotifier::PostKeychainEvent(kSecDataAccessEvent
, mKeychain
, this);
1668 StLock
<Mutex
>_(mMutex
);
1672 Db
db(mKeychain
->database());
1673 if (useSecureStorage(db
))
1675 group
= safer_cast
<SSDbUniqueRecordImpl
&>(*mUniqueId
).group();
1682 void ItemImpl::getLocalContent(SecKeychainAttributeList
*attributeList
, UInt32
*outLength
, void **outData
)
1684 StLock
<Mutex
>_(mMutex
);
1686 Allocator
&allocator
= Allocator::standard(); // @@@ This might not match the one used originally
1689 CssmData
*data
= mData
.get();
1691 MacOSError::throwMe(errSecDataNotAvailable
);
1693 // Copy the data out of our internal cached copy.
1694 UInt32 length
= (UInt32
)data
->Length
;
1695 *outData
= allocator
.malloc(length
);
1696 memcpy(*outData
, data
->Data
, length
);
1698 *outLength
= length
;
1703 if (!mDbAttributes
.get())
1704 MacOSError::throwMe(errSecDataNotAvailable
);
1706 // Pull attributes out of a "floating" item, i.e. one that isn't attached to a database
1707 for (UInt32 ix
= 0; ix
< attributeList
->count
; ++ix
)
1709 SecKeychainAttribute
&attribute
= attributeList
->attr
[ix
];
1710 CssmDbAttributeData
*data
= mDbAttributes
->find(Schema::attributeInfo(attribute
.tag
));
1711 if (data
&& data
->NumberOfValues
> 0)
1713 // Copy the data out of our internal cached copy.
1714 UInt32 length
= (UInt32
)data
->Value
[0].Length
;
1715 attribute
.data
= allocator
.malloc(length
);
1716 memcpy(attribute
.data
, data
->Value
[0].Data
, length
);
1717 attribute
.length
= length
;
1721 attribute
.length
= 0;
1722 attribute
.data
= NULL
;
1729 ItemImpl::getContent(DbAttributes
*dbAttributes
, CssmDataContainer
*itemData
)
1731 StLock
<Mutex
>_(mMutex
);
1732 // Make sure mUniqueId is set.
1736 Db
db(mUniqueId
->database());
1739 mUniqueId
->getWithoutEncryption (dbAttributes
, itemData
);
1742 if (useSecureStorage(db
))
1744 SSDbUniqueRecordImpl
* impl
= dynamic_cast<SSDbUniqueRecordImpl
*>(&(*mUniqueId
));
1747 CssmError::throwMe(CSSMERR_CSSM_INVALID_POINTER
);
1750 SSDbUniqueRecord
ssUniqueId(impl
);
1751 const AccessCredentials
*autoPrompt
= globals().itemCredentials();
1752 ssUniqueId
->get(dbAttributes
, itemData
, autoPrompt
);
1757 mUniqueId
->get(dbAttributes
, itemData
);
1761 ItemImpl::useSecureStorage(const Db
&db
)
1763 StLock
<Mutex
>_(mMutex
);
1764 switch (recordType())
1766 case CSSM_DL_DB_RECORD_GENERIC_PASSWORD
:
1767 case CSSM_DL_DB_RECORD_INTERNET_PASSWORD
:
1768 case CSSM_DL_DB_RECORD_APPLESHARE_PASSWORD
:
1769 if (db
->dl()->subserviceMask() & CSSM_SERVICE_CSP
)
1778 void ItemImpl::willRead()
1782 Item
ItemImpl::makeFromPersistentReference(const CFDataRef persistentRef
, bool *isIdentityRef
)
1784 CssmData
dictData((void*)::CFDataGetBytePtr(persistentRef
), ::CFDataGetLength(persistentRef
));
1785 NameValueDictionary
dict(dictData
);
1788 Item item
= (ItemImpl
*) NULL
;
1790 if (isIdentityRef
) {
1791 *isIdentityRef
= (dict
.FindByName(IDENTITY_KEY
) != 0) ? true : false;
1794 // make sure we have a database identifier
1795 if (dict
.FindByName(SSUID_KEY
) != 0)
1797 DLDbIdentifier dlDbIdentifier
= NameValueDictionary::MakeDLDbIdentifierFromNameValueDictionary(dict
);
1798 DLDbIdentifier
newDlDbIdentifier(dlDbIdentifier
.ssuid(),
1799 DLDbListCFPref::ExpandTildesInPath(dlDbIdentifier
.dbName()).c_str(),
1800 dlDbIdentifier
.dbLocation());
1802 keychain
= globals().storageManager
.keychain(newDlDbIdentifier
);
1804 const NameValuePair
* aDictItem
= dict
.FindByName(ITEM_KEY
);
1805 if (aDictItem
&& keychain
)
1807 PrimaryKey
primaryKey(aDictItem
->Value());
1808 item
= keychain
->item(primaryKey
);
1811 KCThrowIf_( !item
, errSecItemNotFound
);
1815 void ItemImpl::copyPersistentReference(CFDataRef
&outDataRef
, bool isSecIdentityRef
)
1817 if (secd_PersistentRef
) {
1818 outDataRef
= secd_PersistentRef
;
1821 StLock
<Mutex
>_(mMutex
);
1822 // item must be in a keychain and have a primary key to be persistent
1823 if (!mKeychain
|| !mPrimaryKey
) {
1824 MacOSError::throwMe(errSecItemNotFound
);
1826 DLDbIdentifier dlDbIdentifier
= mKeychain
->dlDbIdentifier();
1827 DLDbIdentifier
newDlDbIdentifier(dlDbIdentifier
.ssuid(),
1828 DLDbListCFPref::AbbreviatedPath(mKeychain
->name()).c_str(),
1829 dlDbIdentifier
.dbLocation());
1830 NameValueDictionary dict
;
1831 NameValueDictionary::MakeNameValueDictionaryFromDLDbIdentifier(newDlDbIdentifier
, dict
);
1833 CssmData
* pKey
= mPrimaryKey
;
1834 dict
.Insert (new NameValuePair(ITEM_KEY
, *pKey
));
1836 if (isSecIdentityRef
) {
1837 uint32_t value
= -1;
1838 CssmData
valueData((void*)&value
, sizeof(value
));
1839 dict
.Insert (new NameValuePair(IDENTITY_KEY
, valueData
));
1842 // flatten the NameValueDictionary
1844 dict
.Export(dictData
);
1845 outDataRef
= ::CFDataCreate(kCFAllocatorDefault
, dictData
.Data
, dictData
.Length
);
1846 free (dictData
.Data
);
1849 void ItemImpl::copyRecordIdentifier(CSSM_DATA
&data
)
1851 StLock
<Mutex
>_(mMutex
);
1852 CssmClient::DbUniqueRecord uniqueRecord
= dbUniqueRecord ();
1853 uniqueRecord
->getRecordIdentifier(data
);
1857 * Obtain blob used to bind a keychain item to an Extended Attribute record.
1858 * We just use the PrimaryKey blob as the default. Note that for standard Items,
1859 * this can cause the loss of extended attribute bindings if a Primary Key
1860 * attribute changes.
1862 const CssmData
&ItemImpl::itemID()
1864 StLock
<Mutex
>_(mMutex
);
1865 if(mPrimaryKey
->length() == 0) {
1866 /* not in a keychain; we don't have a primary key */
1867 MacOSError::throwMe(errSecNoSuchAttr
);
1869 return *mPrimaryKey
;
1872 bool ItemImpl::equal(SecCFObject
&other
)
1874 // First check to see if both items have a primary key and
1875 // if the primary key is the same. If so then these
1876 // items must be equal
1877 ItemImpl
& other_item
= (ItemImpl
&)other
;
1878 if (mPrimaryKey
!= NULL
&& mPrimaryKey
== other_item
.mPrimaryKey
)
1883 // The primary keys do not match so do a CFHash of the
1884 // data of the item and compare those for equality
1885 CFHashCode this_hash
= hash();
1886 CFHashCode other_hash
= other
.hash();
1887 return (this_hash
== other_hash
);
1890 CFHashCode
ItemImpl::hash()
1892 CFHashCode result
= SecCFObject::hash();
1894 StLock
<Mutex
>_(mMutex
);
1895 RefPointer
<CssmDataContainer
> data_to_hash
;
1897 // Use the item data for the hash
1898 if (mData
&& *mData
)
1900 data_to_hash
= mData
;
1903 // If there is no primary key AND not data ????
1904 // just return the 'old' hash value which is the
1906 if (NULL
!= data_to_hash
.get())
1908 CFDataRef temp_data
= NULL
;
1909 unsigned char digest
[CC_SHA256_DIGEST_LENGTH
];
1911 if (data_to_hash
->length() < 80)
1913 // If it is less than 80 bytes then CFData can be used
1914 temp_data
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
,
1915 (const UInt8
*)data_to_hash
->data(), data_to_hash
->length(), kCFAllocatorNull
);
1918 // CFData truncates its hash value to 80 bytes. ????
1919 // In order to do the 'right thing' a SHA 256 hash will be used to
1920 // include all of the data
1923 memset(digest
, 0, CC_SHA256_DIGEST_LENGTH
);
1925 CC_SHA256((const void *)data_to_hash
->data(), (CC_LONG
)data_to_hash
->length(), digest
);
1927 temp_data
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
,
1928 (const UInt8
*)digest
, CC_SHA256_DIGEST_LENGTH
, kCFAllocatorNull
);
1931 if (NULL
!= temp_data
)
1933 result
= CFHash(temp_data
);
1934 CFRelease(temp_data
);
1943 void ItemImpl::postItemEvent(SecKeychainEvent theEvent
)
1945 mKeychain
->postEvent(theEvent
, this);
1951 // Item -- This class is here to magically create the right subclass of ItemImpl
1952 // when constructing new items.
1958 Item::Item(ItemImpl
*impl
) : SecPointer
<ItemImpl
>(impl
)
1962 Item::Item(SecItemClass itemClass
, OSType itemCreator
, UInt32 length
, const void* data
, bool inhibitCheck
)
1966 if (itemClass
== CSSM_DL_DB_RECORD_X509_CERTIFICATE
1967 || itemClass
== CSSM_DL_DB_RECORD_PUBLIC_KEY
1968 || itemClass
== CSSM_DL_DB_RECORD_PRIVATE_KEY
1969 || itemClass
== CSSM_DL_DB_RECORD_SYMMETRIC_KEY
)
1970 MacOSError::throwMe(errSecNoSuchClass
); /* @@@ errSecInvalidClass */
1973 *this = new ItemImpl(itemClass
, itemCreator
, length
, data
, inhibitCheck
);
1976 Item::Item(SecItemClass itemClass
, SecKeychainAttributeList
*attrList
, UInt32 length
, const void* data
)
1978 *this = new ItemImpl(itemClass
, attrList
, length
, data
);
1981 Item::Item(const Keychain
&keychain
, const PrimaryKey
&primaryKey
, const CssmClient::DbUniqueRecord
&uniqueId
)
1982 : SecPointer
<ItemImpl
>(
1983 primaryKey
->recordType() == CSSM_DL_DB_RECORD_X509_CERTIFICATE
1984 ? Certificate::make(keychain
, primaryKey
, uniqueId
)
1985 : (primaryKey
->recordType() == CSSM_DL_DB_RECORD_PUBLIC_KEY
1986 || primaryKey
->recordType() == CSSM_DL_DB_RECORD_PRIVATE_KEY
1987 || primaryKey
->recordType() == CSSM_DL_DB_RECORD_SYMMETRIC_KEY
)
1988 ? KeyItem::make(keychain
, primaryKey
, uniqueId
)
1989 : primaryKey
->recordType() == CSSM_DL_DB_RECORD_EXTENDED_ATTRIBUTE
1990 ? ExtendedAttribute::make(keychain
, primaryKey
, uniqueId
)
1991 : ItemImpl::make(keychain
, primaryKey
, uniqueId
))
1995 Item::Item(const Keychain
&keychain
, const PrimaryKey
&primaryKey
)
1996 : SecPointer
<ItemImpl
>(
1997 primaryKey
->recordType() == CSSM_DL_DB_RECORD_X509_CERTIFICATE
1998 ? Certificate::make(keychain
, primaryKey
)
1999 : (primaryKey
->recordType() == CSSM_DL_DB_RECORD_PUBLIC_KEY
2000 || primaryKey
->recordType() == CSSM_DL_DB_RECORD_PRIVATE_KEY
2001 || primaryKey
->recordType() == CSSM_DL_DB_RECORD_SYMMETRIC_KEY
)
2002 ? KeyItem::make(keychain
, primaryKey
)
2003 : primaryKey
->recordType() == CSSM_DL_DB_RECORD_EXTENDED_ATTRIBUTE
2004 ? ExtendedAttribute::make(keychain
, primaryKey
)
2005 : ItemImpl::make(keychain
, primaryKey
))
2009 Item::Item(ItemImpl
&item
)
2010 : SecPointer
<ItemImpl
>(
2011 item
.recordType() == CSSM_DL_DB_RECORD_X509_CERTIFICATE
2012 ? new Certificate(safer_cast
<Certificate
&>(item
))
2013 : (item
.recordType() == CSSM_DL_DB_RECORD_PUBLIC_KEY
2014 || item
.recordType() == CSSM_DL_DB_RECORD_PRIVATE_KEY
2015 || item
.recordType() == CSSM_DL_DB_RECORD_SYMMETRIC_KEY
)
2016 ? new KeyItem(safer_cast
<KeyItem
&>(item
))
2017 : item
.recordType() == CSSM_DL_DB_RECORD_EXTENDED_ATTRIBUTE
2018 ? new ExtendedAttribute(safer_cast
<ExtendedAttribute
&>(item
))
2019 : new ItemImpl(item
))
2023 CFIndex
KeychainCore::GetItemRetainCount(Item
& item
)
2025 return CFGetRetainCount(item
->handle(false));
2028 void ItemImpl::setPersistentRef(CFDataRef ref
)
2030 if (secd_PersistentRef
) {
2031 CFRelease(secd_PersistentRef
);
2033 secd_PersistentRef
= ref
;
2037 CFDataRef
ItemImpl::getPersistentRef()
2039 return secd_PersistentRef
;
2044 bool ItemImpl::mayDelete()
2046 ObjectImpl
* uniqueIDImpl
= mUniqueId
.get();
2048 if (uniqueIDImpl
!= NULL
)
2050 bool result
= mUniqueId
->isIdle();