2 * Copyright (c) 2000-2001,2011-2014 Apple Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 // dlclient - client interface to CSSM DLs and their operations
22 #include <security_cdsa_client/dlclient.h>
23 #include <security_cdsa_client/aclclient.h>
24 #include <Security/cssmapple.h>
25 #include <Security/cssmapplePriv.h>
26 #include <Security/SecBase.h>
27 #include <security_cdsa_utilities/Schema.h>
29 using namespace CssmClient
;
31 #pragma clang diagnostic push
32 #pragma clang diagnostic ignored "-Wunused-const-variable"
33 // blob type for blobs created by these classes -- done so that we can change the formats later
34 const uint32 kBlobType
= 0x1;
35 #pragma clang diagnostic pop
43 DbCursorMaker::~DbCursorMaker()
46 DbUniqueRecordMaker::~DbUniqueRecordMaker()
51 // Manage DL attachments
53 DLImpl::DLImpl(const Guid
&guid
) : AttachmentImpl(guid
, CSSM_SERVICE_DL
)
57 DLImpl::DLImpl(const Module
&module) : AttachmentImpl(module, CSSM_SERVICE_DL
)
66 DLImpl::getDbNames(char **)
68 CssmError::throwMe(CSSMERR_DL_FUNCTION_NOT_IMPLEMENTED
);
72 DLImpl::freeNameList(char **)
74 CssmError::throwMe(CSSMERR_DL_FUNCTION_NOT_IMPLEMENTED
);
78 DLImpl::newDb(const char *inDbName
, const CSSM_NET_ADDRESS
*inDbLocation
)
80 return new DbImpl(DL(this), inDbName
, inDbLocation
);
87 DbImpl::DbImpl(const DL
&dl
, const char *inDbName
, const CSSM_NET_ADDRESS
*inDbLocation
)
88 : ObjectImpl(dl
), mDbName(inDbName
, inDbLocation
),
89 mUseNameFromHandle(!inDbName
), mNameFromHandle(NULL
),
90 mAccessRequest(CSSM_DB_ACCESS_READ
), mAccessCredentials(NULL
),
91 mDefaultCredentials(NULL
), mOpenParameters(NULL
), mDbInfo(NULL
),
92 mResourceControlContext(NULL
)
101 allocator().free(mNameFromHandle
);
111 StLock
<Mutex
> _(mActivateMutex
);
114 assert(mDbInfo
== nil
);
115 mHandle
.DLHandle
= dl()->handle();
116 check(CSSM_DL_DbOpen(mHandle
.DLHandle
, mDbName
.canonicalName(), dbLocation(),
117 mAccessRequest
, mAccessCredentials
,
118 mOpenParameters
, &mHandle
.DBHandle
));
124 if (!mAccessCredentials
&& mDefaultCredentials
)
125 if (const AccessCredentials
*creds
= mDefaultCredentials
->makeCredentials())
126 CSSM_DL_Authenticate(handle(), mAccessRequest
, creds
); // ignore error
130 DbImpl::createWithBlob(CssmData
&blob
)
133 CssmError::throwMe(CSSMERR_DL_DATASTORE_ALREADY_EXISTS
);
135 if (mDbInfo
== nil
) {
136 // handle a missing (null) mDbInfo as an all-zero one
137 static const CSSM_DBINFO nullDbInfo
= { };
138 mDbInfo
= &nullDbInfo
;
141 mHandle
.DLHandle
= dl()->handle();
143 // create a parameter block for our call to the passthrough
144 CSSM_APPLE_CSPDL_DB_CREATE_WITH_BLOB_PARAMETERS params
;
146 params
.dbName
= mDbName
.canonicalName ();
147 params
.dbLocation
= dbLocation ();
148 params
.dbInfo
= mDbInfo
;
149 params
.accessRequest
= mAccessRequest
;
150 params
.credAndAclEntry
= NULL
;
151 params
.openParameters
= mOpenParameters
;
154 check(CSSM_DL_PassThrough (mHandle
, CSSM_APPLECSPDL_DB_CREATE_WITH_BLOB
, ¶ms
, (void**) &mHandle
.DBHandle
));
160 StLock
<Mutex
> _(mActivateMutex
);
162 CssmError::throwMe(CSSMERR_DL_DATASTORE_ALREADY_EXISTS
);
164 if (mDbInfo
== nil
) {
165 // handle a missing (null) mDbInfo as an all-zero one
166 static const CSSM_DBINFO nullDbInfo
= { };
167 mDbInfo
= &nullDbInfo
;
169 mHandle
.DLHandle
= dl()->handle();
171 if (!mResourceControlContext
&& mAccessCredentials
) {
172 AclFactory::AnyResourceContext
ctx(mAccessCredentials
);
173 check(CSSM_DL_DbCreate(mHandle
.DLHandle
, mDbName
.canonicalName(), dbLocation(),
174 mDbInfo
, mAccessRequest
, &ctx
,
175 mOpenParameters
, &mHandle
.DBHandle
));
177 check(CSSM_DL_DbCreate(mHandle
.DLHandle
, mDbName
.canonicalName(), dbLocation(),
178 mDbInfo
, mAccessRequest
, mResourceControlContext
,
179 mOpenParameters
, &mHandle
.DBHandle
));
187 StLock
<Mutex
> _(mActivateMutex
);
190 check(CSSM_DL_DbClose (mHandle
));
198 StLock
<Mutex
> _(mActivateMutex
);
211 StLock
<Mutex
> _(mActivateMutex
);
222 // Deactivate so the db gets closed if it was open.
224 // This call does not require the receiver to be active.
225 check(CSSM_DL_DbDelete(dl()->handle(), mDbName
.canonicalName(), dbLocation(),
226 mAccessCredentials
));
230 DbImpl::rename(const char *newName
)
232 // Deactivate so the db gets closed if it was open.
234 if (::rename(mDbName
.canonicalName(), newName
))
235 UnixError::throwMe(errno
);
237 // Change our DbName to reflect this rename.
238 mDbName
= DbName(newName
, dbLocation());
242 DbImpl::authenticate(CSSM_DB_ACCESS_TYPE inAccessRequest
,
243 const CSSM_ACCESS_CREDENTIALS
*inAccessCredentials
)
247 // XXX Could do the same for create but this would require sticking
248 // inAccessCredentials into mResourceControlContext.
251 // We were not yet active. Just do an open.
252 accessRequest(inAccessRequest
);
253 accessCredentials(inAccessCredentials
);
259 check(CSSM_DL_Authenticate(handle(), inAccessRequest
, inAccessCredentials
));
263 DbImpl::name(char *&outDbName
)
265 check(CSSM_DL_GetDbNameFromHandle(handle(), &outDbName
));
271 if (mUseNameFromHandle
)
274 || !CSSM_DL_GetDbNameFromHandle(handle(), &mNameFromHandle
))
276 return mNameFromHandle
;
279 // We failed to get the name from the handle so use the passed
281 mUseNameFromHandle
= false;
284 return mDbName
.canonicalName();
288 DbImpl::createRelation(CSSM_DB_RECORDTYPE inRelationID
,
289 const char *inRelationName
,
290 uint32 inNumberOfAttributes
,
291 const CSSM_DB_SCHEMA_ATTRIBUTE_INFO
*pAttributeInfo
,
292 uint32 inNumberOfIndexes
,
293 const CSSM_DB_SCHEMA_INDEX_INFO
*pIndexInfo
)
295 check(CSSM_DL_CreateRelation(handle(), inRelationID
, inRelationName
,
296 inNumberOfAttributes
, pAttributeInfo
,
297 inNumberOfIndexes
, pIndexInfo
));
301 DbImpl::destroyRelation(CSSM_DB_RECORDTYPE inRelationID
)
303 check(CSSM_DL_DestroyRelation(handle(), inRelationID
));
307 DbImpl::insert(CSSM_DB_RECORDTYPE recordType
, const CSSM_DB_RECORD_ATTRIBUTE_DATA
*attributes
,
308 const CSSM_DATA
*data
)
310 DbUniqueRecord
uniqueId(Db(this));
311 check(CSSM_DL_DataInsert(handle(), recordType
,
314 // Activate uniqueId so CSSM_DL_FreeUniqueRecord() gets called when it goes out of scope.
315 uniqueId
->activate();
321 DbImpl::insertWithoutEncryption(CSSM_DB_RECORDTYPE recordType
, const CSSM_DB_RECORD_ATTRIBUTE_DATA
*attributes
,
324 DbUniqueRecord
uniqueId(Db(this));
326 // fill out the parameters
327 CSSM_APPLECSPDL_DB_INSERT_WITHOUT_ENCRYPTION_PARAMETERS params
;
328 params
.recordType
= recordType
;
329 params
.attributes
= const_cast<CSSM_DB_RECORD_ATTRIBUTE_DATA
*>(attributes
);
332 // for clarity, call the overloaded operator to produce a unique record pointer
333 CSSM_DB_UNIQUE_RECORD_PTR
*uniquePtr
= uniqueId
;
336 passThrough (CSSM_APPLECSPDL_DB_INSERT_WITHOUT_ENCRYPTION
, ¶ms
, (void**) uniquePtr
);
338 // Activate uniqueId so CSSM_DL_FreeUniqueRecord() gets called when it goes out of scope.
339 uniqueId
->activate();
345 // Generic Passthrough interface
347 void DbImpl::passThrough(uint32 passThroughId
, const void *in
, void **out
)
349 check(CSSM_DL_PassThrough(handle(), passThroughId
, in
, out
));
354 // Passthrough functions (only implemented by AppleCSPDL).
359 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_LOCK
, NULL
, NULL
));
365 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_UNLOCK
, NULL
, NULL
));
369 DbImpl::unlock(const CSSM_DATA
&password
)
371 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_UNLOCK
, &password
, NULL
));
377 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_STASH
, NULL
, NULL
));
383 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_STASH_CHECK
, NULL
, NULL
));
387 DbImpl::getSettings(uint32
&outIdleTimeout
, bool &outLockOnSleep
)
389 CSSM_APPLECSPDL_DB_SETTINGS_PARAMETERS_PTR settings
;
390 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_GET_SETTINGS
,
391 NULL
, reinterpret_cast<void **>(&settings
)));
392 outIdleTimeout
= settings
->idleTimeout
;
393 outLockOnSleep
= settings
->lockOnSleep
;
394 allocator().free(settings
);
398 DbImpl::setSettings(uint32 inIdleTimeout
, bool inLockOnSleep
)
400 CSSM_APPLECSPDL_DB_SETTINGS_PARAMETERS settings
;
401 settings
.idleTimeout
= inIdleTimeout
;
402 settings
.lockOnSleep
= inLockOnSleep
;
403 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_SET_SETTINGS
, &settings
, NULL
));
409 CSSM_APPLECSPDL_DB_IS_LOCKED_PARAMETERS_PTR params
;
410 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_IS_LOCKED
,
411 NULL
, reinterpret_cast<void **>(¶ms
)));
412 bool isLocked
= params
->isLocked
;
413 allocator().free(params
);
418 DbImpl::changePassphrase(const CSSM_ACCESS_CREDENTIALS
*cred
)
420 CSSM_APPLECSPDL_DB_CHANGE_PASSWORD_PARAMETERS params
;
421 params
.accessCredentials
= const_cast<CSSM_ACCESS_CREDENTIALS
*>(cred
);
422 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_CHANGE_PASSWORD
, ¶ms
, NULL
));
425 void DbImpl::recode(const CSSM_DATA
&data
, const CSSM_DATA
&extraData
)
427 // setup parameters for the recode call
428 CSSM_APPLECSPDL_RECODE_PARAMETERS params
;
429 params
.dbBlob
= data
;
430 params
.extraData
= extraData
;
433 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_CSP_RECODE
, ¶ms
, NULL
));
436 void DbImpl::copyBlob (CssmData
&data
)
439 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_COPY_BLOB
, NULL
, (void**) (CSSM_DATA
*) &data
));
442 void DbImpl::setBatchMode(Boolean mode
, Boolean rollback
)
445 // We need the DL_DB_Handle of the underyling DL in order to use CSSM_APPLEFILEDL_TOGGLE_AUTOCOMMIT
448 CSSM_DL_DB_HANDLE dldbHandleOfUnderlyingDL
;
449 result
= CSSM_DL_PassThrough(handle(),
450 CSSM_APPLECSPDL_DB_GET_HANDLE
,
452 (void **)&dldbHandleOfUnderlyingDL
);
454 // Now, toggle the autocommit...
456 if ( result
== errSecSuccess
)
458 CSSM_BOOL modeToUse
= !mode
;
461 result
= (OSStatus
)CSSM_DL_PassThrough(dldbHandleOfUnderlyingDL
,
462 CSSM_APPLEFILEDL_ROLLBACK
, NULL
, NULL
);
465 result
= CSSM_DL_PassThrough(dldbHandleOfUnderlyingDL
,
466 CSSM_APPLEFILEDL_TOGGLE_AUTOCOMMIT
,
467 (void *)((size_t) modeToUse
),
469 if (!rollback
&& modeToUse
)
470 result
= CSSM_DL_PassThrough(dldbHandleOfUnderlyingDL
,
471 CSSM_APPLEFILEDL_COMMIT
,
477 uint32
DbImpl::dbBlobVersion() {
478 uint32 dbBlobVersion
= 0;
479 uint32
* dbBlobVersionPtr
= &dbBlobVersion
;
481 // We only have a blob version if we're an apple CSPDL
482 if(dl()->guid() == gGuidAppleCSPDL
) {
483 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_GET_BLOB_VERSION
, NULL
, (void**) &dbBlobVersionPtr
));
485 secnotice("integrity", "Non-Apple CSPDL keychains don't have keychain versions");
487 return dbBlobVersion
;
490 uint32
DbImpl::recodeDbToVersion(uint32 version
) {
492 uint32
* newDbVersionPtr
= &newDbVersion
;
493 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_RECODE_TO_BLOB_VERSION
, &version
, (void**) &newDbVersionPtr
));
497 void DbImpl::recodeFinished() {
498 check(CSSM_DL_PassThrough(handle(), CSSM_APPLECSPDL_DB_RECODE_FINISHED
, NULL
, NULL
));
501 void DbImpl::takeFileLock() {
502 passThrough(CSSM_APPLECSPDL_DB_TAKE_FILE_LOCK
, NULL
, NULL
);
505 void DbImpl::releaseFileLock(bool success
) {
506 passThrough(CSSM_APPLECSPDL_DB_RELEASE_FILE_LOCK
, &success
, NULL
);
509 void DbImpl::makeBackup() {
510 passThrough(CSSM_APPLECSPDL_DB_MAKE_BACKUP
, NULL
, NULL
);
513 void DbImpl::makeCopy(const char* path
) {
514 passThrough(CSSM_APPLECSPDL_DB_MAKE_COPY
, path
, NULL
);
517 void DbImpl::deleteFile() {
518 passThrough(CSSM_APPLECSPDL_DB_DELETE_FILE
, NULL
, NULL
);
521 void DbImpl::transferTo(const DLDbIdentifier
& dldbidentifier
) {
522 if (dldbidentifier
.ssuid().subserviceType() & CSSM_SERVICE_CSP
) {
523 // if we're an Apple CSPDL, do the fancy transfer:
524 // clone the file, clone the db, remove the original file
525 string oldPath
= name();
527 CSSM_DB_HANDLE dbhandle
;
528 passThrough(CSSM_APPLECSPDL_DB_CLONE
, &dldbidentifier
, &dbhandle
);
530 mDbName
= dldbidentifier
.dbName();
531 mHandle
.DBHandle
= dbhandle
;
533 unlink(oldPath
.c_str());
535 // Don't cache this name
536 if (mNameFromHandle
) {
537 allocator().free(mNameFromHandle
);
538 mNameFromHandle
= NULL
;
541 // if we're not an Apple CSPDL, just call rename
542 this->rename(dldbidentifier
.dbName());
547 // cloneTo only makes sense if you're on an Apple CSPDL
548 Db
DbImpl::cloneTo(const DLDbIdentifier
& dldbidentifier
) {
549 CSSM_DB_HANDLE dbhandle
;
550 passThrough(CSSM_APPLECSPDL_DB_CLONE
, &dldbidentifier
, &dbhandle
);
552 // This is the only reasonable way to make a SSDbImpl at this layer.
553 CssmClient::Db
db(dl(), dldbidentifier
.dbName(), dldbidentifier
.dbLocation());
554 db
->mHandle
.DBHandle
= dbhandle
;
563 DbImpl::newDbCursor(const CSSM_QUERY
&query
, Allocator
&allocator
)
565 return new DbDbCursorImpl(Db(this), query
, allocator
);
569 DbImpl::newDbCursor(uint32 capacity
, Allocator
&allocator
)
571 return new DbDbCursorImpl(Db(this), capacity
, allocator
);
576 // Db adapters for AclBearer
578 void DbImpl::getAcl(AutoAclEntryInfoList
&aclInfos
, const char *selectionTag
) const
580 aclInfos
.allocator(allocator());
581 check(CSSM_DL_GetDbAcl(const_cast<DbImpl
*>(this)->handle(),
582 reinterpret_cast<const CSSM_STRING
*>(selectionTag
), aclInfos
, aclInfos
));
585 void DbImpl::changeAcl(const CSSM_ACL_EDIT
&aclEdit
,
586 const CSSM_ACCESS_CREDENTIALS
*accessCred
)
588 check(CSSM_DL_ChangeDbAcl(handle(), AccessCredentials::needed(accessCred
), &aclEdit
));
591 void DbImpl::getOwner(AutoAclOwnerPrototype
&owner
) const
593 owner
.allocator(allocator());
594 check(CSSM_DL_GetDbOwner(const_cast<DbImpl
*>(this)->handle(), owner
));
597 void DbImpl::changeOwner(const CSSM_ACL_OWNER_PROTOTYPE
&newOwner
,
598 const CSSM_ACCESS_CREDENTIALS
*accessCred
)
600 check(CSSM_DL_ChangeDbOwner(handle(),
601 AccessCredentials::needed(accessCred
), &newOwner
));
604 void DbImpl::defaultCredentials(DefaultCredentialsMaker
*maker
)
606 mDefaultCredentials
= maker
;
611 // Abstract DefaultCredentialsMakers
613 DbImpl::DefaultCredentialsMaker::~DefaultCredentialsMaker()
618 // Db adapters for DLAccess
620 CSSM_HANDLE
Db::dlGetFirst(const CSSM_QUERY
&query
, CSSM_DB_RECORD_ATTRIBUTE_DATA
&attributes
,
621 CSSM_DATA
*data
, CSSM_DB_UNIQUE_RECORD
*&id
)
624 switch (CSSM_RETURN rc
= CSSM_DL_DataGetFirst(handle(), &query
, &result
, &attributes
, data
, &id
)) {
627 case CSSMERR_DL_ENDOFDATA
:
628 return CSSM_INVALID_HANDLE
;
630 CssmError::throwMe(rc
);
634 bool Db::dlGetNext(CSSM_HANDLE query
, CSSM_DB_RECORD_ATTRIBUTE_DATA
&attributes
,
635 CSSM_DATA
*data
, CSSM_DB_UNIQUE_RECORD
*&id
)
637 CSSM_RETURN rc
= CSSM_DL_DataGetNext(handle(), query
, &attributes
, data
, &id
);
641 case CSSMERR_DL_ENDOFDATA
:
644 CssmError::throwMe(rc
);
648 void Db::dlAbortQuery(CSSM_HANDLE query
)
650 CssmError::check(CSSM_DL_DataAbortQuery(handle(), query
));
653 void Db::dlFreeUniqueId(CSSM_DB_UNIQUE_RECORD
*id
)
655 CssmError::check(CSSM_DL_FreeUniqueRecord(handle(), id
));
658 void Db::dlDeleteRecord(CSSM_DB_UNIQUE_RECORD
*id
)
660 CssmError::check(CSSM_DL_DataDelete(handle(), id
));
663 Allocator
&Db::allocator()
665 return Object::allocator();
670 // DbUniqueRecordMaker
673 DbImpl::newDbUniqueRecord()
675 return new DbUniqueRecordImpl(Db(this));
683 DbImpl::dlDbIdentifier()
685 // Always use the same dbName and dbLocation that were passed in during
687 return DLDbIdentifier(dl()->subserviceUid(), mDbName
.canonicalName(), dbLocation());
694 DbDbCursorImpl::DbDbCursorImpl(const Db
&db
, const CSSM_QUERY
&query
, Allocator
&allocator
)
695 : DbCursorImpl(db
, query
, allocator
), mResultsHandle(CSSM_INVALID_HANDLE
)
699 DbDbCursorImpl::DbDbCursorImpl(const Db
&db
, uint32 capacity
, Allocator
&allocator
)
700 : DbCursorImpl(db
, capacity
, allocator
), mResultsHandle(CSSM_INVALID_HANDLE
)
704 DbDbCursorImpl::~DbDbCursorImpl()
714 DbDbCursorImpl::next(DbAttributes
*attributes
, ::CssmDataContainer
*data
, DbUniqueRecord
&uniqueId
)
717 attributes
->deleteValues();
724 DbUniqueRecord
unique(db
);
727 // ask the CSP/DL if the requested record type exists
728 CSSM_BOOL boolResult
;
729 CSSM_DL_PassThrough(db
->handle(), CSSM_APPLECSPDL_DB_RELATION_EXISTS
, &RecordType
, (void**) &boolResult
);
740 result
= CSSM_DL_DataGetFirst(db
->handle(),
747 StLock
<Mutex
> _(mActivateMutex
);
748 if (result
== CSSM_OK
)
750 else if (data
!= NULL
)
755 result
= CSSM_DL_DataGetNext(db
->handle(),
761 if (result
!= CSSM_OK
&& data
!= NULL
)
767 if (result
!= CSSM_OK
&& attributes
!= NULL
)
769 attributes
->invalidate();
772 if (result
== CSSMERR_DL_ENDOFDATA
)
774 StLock
<Mutex
> _(mActivateMutex
);
781 // Activate uniqueId so CSSM_DL_FreeUniqueRecord() gets called when it goes out of scope.
788 DbDbCursorImpl::activate()
793 DbDbCursorImpl::deactivate()
795 StLock
<Mutex
> _(mActivateMutex
);
799 check(CSSM_DL_DataAbortQuery(database()->handle(), mResultsHandle
));
807 DbCursorImpl::DbCursorImpl(const Object
&parent
, const CSSM_QUERY
&query
, Allocator
&allocator
) :
808 ObjectImpl(parent
), CssmAutoQuery(query
, allocator
)
812 DbCursorImpl::DbCursorImpl(const Object
&parent
, uint32 capacity
, Allocator
&allocator
) :
813 ObjectImpl(parent
), CssmAutoQuery(capacity
, allocator
)
818 DbCursorImpl::allocator() const
820 return ObjectImpl::allocator();
824 DbCursorImpl::allocator(Allocator
&alloc
)
826 ObjectImpl::allocator(alloc
);
833 DbUniqueRecordImpl::DbUniqueRecordImpl(const Db
&db
) : ObjectImpl(db
), mDestroyID (false)
837 DbUniqueRecordImpl::~DbUniqueRecordImpl()
843 allocator ().free (mUniqueId
);
852 DbUniqueRecordImpl::deleteRecord()
854 check(CSSM_DL_DataDelete(database()->handle(), mUniqueId
));
858 DbUniqueRecordImpl::modify(CSSM_DB_RECORDTYPE recordType
,
859 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*attributes
,
860 const CSSM_DATA
*data
,
861 CSSM_DB_MODIFY_MODE modifyMode
)
863 check(CSSM_DL_DataModify(database()->handle(), recordType
, mUniqueId
,
869 DbUniqueRecordImpl::modifyWithoutEncryption(CSSM_DB_RECORDTYPE recordType
,
870 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*attributes
,
871 const CSSM_DATA
*data
,
872 CSSM_DB_MODIFY_MODE modifyMode
)
874 // fill out the parameters
875 CSSM_APPLECSPDL_DB_MODIFY_WITHOUT_ENCRYPTION_PARAMETERS params
;
876 params
.recordType
= recordType
;
877 params
.uniqueID
= mUniqueId
;
878 params
.attributes
= const_cast<CSSM_DB_RECORD_ATTRIBUTE_DATA
*>(attributes
);
879 params
.data
= (CSSM_DATA
*) data
;
880 params
.modifyMode
= modifyMode
;
883 check(CSSM_DL_PassThrough(database()->handle(),
884 CSSM_APPLECSPDL_DB_MODIFY_WITHOUT_ENCRYPTION
,
890 DbUniqueRecordImpl::get(DbAttributes
*attributes
,
891 ::CssmDataContainer
*data
)
894 attributes
->deleteValues();
899 // @@@ Fix the allocators for attributes and data.
901 result
= CSSM_DL_DataGetFromUniqueRecordId(database()->handle(), mUniqueId
,
905 if (result
!= CSSM_OK
)
908 attributes
->invalidate();
909 if (data
!= NULL
) // the data returned is no longer valid
919 DbUniqueRecordImpl::getWithoutEncryption(DbAttributes
*attributes
,
920 ::CssmDataContainer
*data
)
923 attributes
->deleteValues();
928 // @@@ Fix the allocators for attributes and data.
931 // make the parameter block
932 CSSM_APPLECSPDL_DB_GET_WITHOUT_ENCRYPTION_PARAMETERS params
;
933 params
.uniqueID
= mUniqueId
;
934 params
.attributes
= attributes
;
937 ::CssmDataContainer recordData
;
938 result
= CSSM_DL_PassThrough(database()->handle(), CSSM_APPLECSPDL_DB_GET_WITHOUT_ENCRYPTION
, ¶ms
,
944 DbUniqueRecordImpl::activate()
946 StLock
<Mutex
> _(mActivateMutex
);
951 DbUniqueRecordImpl::deactivate()
953 StLock
<Mutex
> _(mActivateMutex
);
957 check(CSSM_DL_FreeUniqueRecord(database()->handle(), mUniqueId
));
962 DbUniqueRecordImpl::getRecordIdentifier(CSSM_DATA
&data
)
964 check(CSSM_DL_PassThrough(database()->handle(), CSSM_APPLECSPDL_DB_GET_RECORD_IDENTIFIER
,
965 mUniqueId
, (void**) &data
));
968 void DbUniqueRecordImpl::setUniqueRecordPtr(CSSM_DB_UNIQUE_RECORD_PTR uniquePtr
)
971 mUniqueId
= (CSSM_DB_UNIQUE_RECORD_PTR
) allocator ().malloc (sizeof (CSSM_DB_UNIQUE_RECORD
));
972 *mUniqueId
= *uniquePtr
;
979 DbAttributes::DbAttributes()
980 : CssmAutoDbRecordAttributeData(0, Allocator::standard(), Allocator::standard())
984 DbAttributes::DbAttributes(const Db
&db
, uint32 capacity
, Allocator
&allocator
)
985 : CssmAutoDbRecordAttributeData(capacity
, db
->allocator(), allocator
)
989 void DbAttributes::updateWithDbAttributes(DbAttributes
* newValues
) {
995 newValues
->canonicalize();
997 updateWith(newValues
);
1001 DbAttributes::canonicalize() {
1002 for(int i
= 0; i
< size(); i
++) {
1003 CssmDbAttributeData
& data
= attributes()[i
];
1004 CssmDbAttributeInfo
& datainfo
= data
.info();
1006 // Calling Schema::attributeInfo is the best way to canonicalize.
1007 // There's no way around the try-catch structure, since it throws if it
1008 // can't find something.
1011 if(datainfo
.nameFormat() == CSSM_DB_ATTRIBUTE_NAME_AS_INTEGER
) {
1012 data
.info() = Security::KeychainCore::Schema::attributeInfo(datainfo
.intName());
1015 // Don't worry about it