2 * Copyright (c) 2000-2001 Apple Computer, 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 // SSDLSession.h - DL session for security server CSP/DL.
22 #include "SSDLSession.h"
24 #include "CSPDLPlugin.h"
26 #include <security_cdsa_utilities/cssmbridge.h>
27 #include <Security/cssmapplePriv.h>
29 using namespace CssmClient
;
30 using namespace SecurityServer
;
34 // SSDLSession -- Security Server DL session
36 SSDLSession::SSDLSession(CSSM_MODULE_HANDLE handle
,
38 const CSSM_VERSION
&version
,
40 CSSM_SERVICE_TYPE subserviceType
,
41 CSSM_ATTACH_FLAGS attachFlags
,
42 const CSSM_UPCALLS
&upcalls
,
43 DatabaseManager
&databaseManager
,
44 SSCSPDLSession
&ssCSPDLSession
)
45 : DLPluginSession(handle
, plug
, version
, subserviceId
, subserviceType
,
46 attachFlags
, upcalls
, databaseManager
),
47 mSSCSPDLSession(ssCSPDLSession
),
48 mDL(Module(gGuidAppleFileDL
, Cssm::standard())),
49 mClientSession(Allocator::standard(), static_cast<PluginSession
&>(*this))
51 mClientSession
.registerForAclEdits(SSCSPDLSession::didChangeKeyAclCallback
, &mSSCSPDLSession
);
52 // @@@ mDL.allocator(*static_cast<DatabaseSession *>(this));
53 mDL
->allocator(allocator());
54 mDL
->version(version
);
55 mDL
->subserviceId(subserviceId
);
56 mDL
->flags(attachFlags
);
57 // fprintf(stderr, "%p: Created %p\n", pthread_self(), this);
60 SSDLSession::~SSDLSession()
62 // @@@ What about a catch?
63 StLock
<Mutex
> _1(mSSUniqueRecordLock
);
64 mSSUniqueRecordMap
.clear();
66 StLock
<Mutex
> _2(mDbHandleLock
);
67 DbHandleMap::iterator end
= mDbHandleMap
.end();
68 for (DbHandleMap::iterator it
= mDbHandleMap
.begin(); it
!= end
; ++it
)
77 SSDLSession::GetDbNames(CSSM_NAME_LIST_PTR
&outNameList
)
80 CSSM_DL_GetDbNames(mDL
->handle(), &outNameList
);
85 SSDLSession::FreeNameList(CSSM_NAME_LIST
&inNameList
)
88 CSSM_DL_FreeNameList(mDL
->handle(), &inNameList
);
93 SSDLSession::DbDelete(const char *inDbName
,
94 const CSSM_NET_ADDRESS
*inDbLocation
,
95 const AccessCredentials
*inAccessCred
)
97 SSDatabase
db(mClientSession
, mDL
, inDbName
, inDbLocation
);
98 db
->accessCredentials(inAccessCred
);
102 // DbContext creation and destruction.
104 SSDLSession::DbCreate(const char *inDbName
,
105 const CSSM_NET_ADDRESS
*inDbLocation
,
106 const CSSM_DBINFO
&inDBInfo
,
107 CSSM_DB_ACCESS_TYPE inAccessRequest
,
108 const CSSM_RESOURCE_CONTROL_CONTEXT
*inCredAndAclEntry
,
109 const void *inOpenParameters
,
110 CSSM_DB_HANDLE
&outDbHandle
)
112 SSDatabase
db(mClientSession
, mDL
, inDbName
, inDbLocation
);
113 db
->dbInfo(&inDBInfo
);
114 db
->accessRequest(inAccessRequest
);
115 db
->resourceControlContext(inCredAndAclEntry
);
116 db
->openParameters(inOpenParameters
);
117 db
->create(DLDbIdentifier(CssmSubserviceUid(plugin
.myGuid(), &version(), subserviceId(),
118 CSSM_SERVICE_DL
| CSSM_SERVICE_CSP
),
119 inDbName
, inDbLocation
));
121 outDbHandle
= makeDbHandle(db
);
122 // fprintf(stderr, "%p %p was created for %s in session %p\n", pthread_self(), (void*) outDbHandle, inDbName, this);
126 SSDLSession::CreateWithBlob(const char *DbName
,
127 const CSSM_NET_ADDRESS
*DbLocation
,
128 const CSSM_DBINFO
&DBInfo
,
129 CSSM_DB_ACCESS_TYPE AccessRequest
,
130 const void *OpenParameters
,
131 const CSSM_DATA
&blob
,
132 CSSM_DB_HANDLE
&DbHandle
)
134 SSDatabase
db(mClientSession
, mDL
, DbName
, DbLocation
);
136 db
->accessRequest(AccessRequest
);
137 db
->resourceControlContext(NULL
);
138 db
->openParameters(OpenParameters
);
139 db
->createWithBlob(DLDbIdentifier(CssmSubserviceUid(plugin
.myGuid(), &version(), subserviceId(),
140 CSSM_SERVICE_DL
| CSSM_SERVICE_CSP
),
144 DbHandle
= makeDbHandle(db
);
145 // fprintf(stderr, "%p %p was created with a blob in session %p\n", pthread_self(), (void*) DbHandle, this);
149 SSDLSession::DbOpen(const char *inDbName
,
150 const CSSM_NET_ADDRESS
*inDbLocation
,
151 CSSM_DB_ACCESS_TYPE inAccessRequest
,
152 const AccessCredentials
*inAccessCred
,
153 const void *inOpenParameters
,
154 CSSM_DB_HANDLE
&outDbHandle
)
156 SSDatabase
db(mClientSession
, mDL
, inDbName
, inDbLocation
);
157 db
->accessRequest(inAccessRequest
);
158 db
->accessCredentials(inAccessCred
);
159 db
->openParameters(inOpenParameters
);
160 db
->open(DLDbIdentifier(CssmSubserviceUid(plugin
.myGuid(), &version(), subserviceId(),
161 CSSM_SERVICE_DL
| CSSM_SERVICE_CSP
),
162 inDbName
, inDbLocation
));
163 outDbHandle
= makeDbHandle(db
);
164 // fprintf(stderr, "%p %p was opened for %s in session %p\n", pthread_self(), (void*) outDbHandle, inDbName, this);
167 // Operations using DbContext instances.
169 SSDLSession::DbClose(CSSM_DB_HANDLE inDbHandle
)
171 killDbHandle(inDbHandle
)->close();
175 SSDLSession::CreateRelation(CSSM_DB_HANDLE inDbHandle
,
176 CSSM_DB_RECORDTYPE inRelationID
,
177 const char *inRelationName
,
178 uint32 inNumberOfAttributes
,
179 const CSSM_DB_SCHEMA_ATTRIBUTE_INFO
*inAttributeInfo
,
180 uint32 inNumberOfIndexes
,
181 const CSSM_DB_SCHEMA_INDEX_INFO
&inIndexInfo
)
183 SSDatabase db
= findDbHandle(inDbHandle
);
184 // @@@ Fix inAttributeInfo and inIndexInfo arguments (might be NULL if NumberOf = 0)
185 db
->createRelation(inRelationID
, inRelationName
,
186 inNumberOfAttributes
, inAttributeInfo
,
187 inNumberOfIndexes
, &inIndexInfo
);
191 SSDLSession::DestroyRelation(CSSM_DB_HANDLE inDbHandle
,
192 CSSM_DB_RECORDTYPE inRelationID
)
194 // @@@ Check credentials.
195 SSDatabase db
= findDbHandle(inDbHandle
);
196 db
->destroyRelation(inRelationID
);
200 SSDLSession::Authenticate(CSSM_DB_HANDLE inDbHandle
,
201 CSSM_DB_ACCESS_TYPE inAccessRequest
,
202 const AccessCredentials
&inAccessCred
)
204 SSDatabase db
= findDbHandle(inDbHandle
);
205 db
->authenticate(inAccessRequest
, &inAccessCred
);
210 SSDLSession::GetDbAcl(CSSM_DB_HANDLE inDbHandle
,
211 const CSSM_STRING
*inSelectionTag
,
212 uint32
&outNumberOfAclInfos
,
213 CSSM_ACL_ENTRY_INFO_PTR
&outAclInfos
)
215 SSDatabase db
= findDbHandle(inDbHandle
);
216 mClientSession
.getDbAcl(db
->dbHandle(),
217 inSelectionTag
? *inSelectionTag
: NULL
,
218 outNumberOfAclInfos
, AclEntryInfo::overlayVar(outAclInfos
), allocator());
222 SSDLSession::ChangeDbAcl(CSSM_DB_HANDLE inDbHandle
,
223 const AccessCredentials
&inAccessCred
,
224 const CSSM_ACL_EDIT
&inAclEdit
)
226 SSDatabase db
= findDbHandle(inDbHandle
);
227 mClientSession
.changeDbAcl(db
->dbHandle(), inAccessCred
, AclEdit::overlay(inAclEdit
));
231 SSDLSession::GetDbOwner(CSSM_DB_HANDLE inDbHandle
,
232 CSSM_ACL_OWNER_PROTOTYPE
&outOwner
)
234 SSDatabase db
= findDbHandle(inDbHandle
);
235 mClientSession
.getDbOwner(db
->dbHandle(),
236 AclOwnerPrototype::overlay(outOwner
), allocator());
240 SSDLSession::ChangeDbOwner(CSSM_DB_HANDLE inDbHandle
,
241 const AccessCredentials
&inAccessCred
,
242 const CSSM_ACL_OWNER_PROTOTYPE
&inNewOwner
)
244 SSDatabase db
= findDbHandle(inDbHandle
);
245 mClientSession
.changeDbOwner(db
->dbHandle(), inAccessCred
,
246 AclOwnerPrototype::overlay(inNewOwner
));
250 SSDLSession::GetDbNameFromHandle(CSSM_DB_HANDLE inDbHandle
,
253 SSDatabase db
= findDbHandle(inDbHandle
);
254 // @@@ Fix this functions signature.
255 db
->name(*outDbName
);
259 SSDLSession::DataInsert(CSSM_DB_HANDLE inDbHandle
,
260 CSSM_DB_RECORDTYPE inRecordType
,
261 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*inAttributes
,
262 const CssmData
*inData
,
263 CSSM_DB_UNIQUE_RECORD_PTR
&outUniqueId
)
265 SSDatabase db
= findDbHandle(inDbHandle
);
266 // @@@ Fix client lib.
267 SSUniqueRecord uniqueId
= db
->insert(inRecordType
, inAttributes
, inData
, true); // @@@ Fix me
268 outUniqueId
= makeSSUniqueRecord(uniqueId
);
269 // @@@ If this is a key do the right thing.
273 SSDLSession::DataDelete(CSSM_DB_HANDLE inDbHandle
,
274 const CSSM_DB_UNIQUE_RECORD
&inUniqueRecordIdentifier
)
276 SSDatabase db
= findDbHandle(inDbHandle
);
277 SSUniqueRecord uniqueId
= findSSUniqueRecord(inUniqueRecordIdentifier
);
278 uniqueId
->deleteRecord();
279 // @@@ If this is a key do the right thing.
284 SSDLSession::DataModify(CSSM_DB_HANDLE inDbHandle
,
285 CSSM_DB_RECORDTYPE inRecordType
,
286 CSSM_DB_UNIQUE_RECORD
&inoutUniqueRecordIdentifier
,
287 const CSSM_DB_RECORD_ATTRIBUTE_DATA
*inAttributesToBeModified
,
288 const CssmData
*inDataToBeModified
,
289 CSSM_DB_MODIFY_MODE inModifyMode
)
291 SSDatabase db
= findDbHandle(inDbHandle
);
292 SSUniqueRecord uniqueId
= findSSUniqueRecord(inoutUniqueRecordIdentifier
);
293 uniqueId
->modify(inRecordType
, inAttributesToBeModified
, inDataToBeModified
, inModifyMode
);
294 // @@@ If this is a key do the right thing.
298 SSDLSession::DataGetFirst(CSSM_DB_HANDLE inDbHandle
,
299 const CssmQuery
*inQuery
,
300 CSSM_DB_RECORD_ATTRIBUTE_DATA_PTR inoutAttributes
,
302 CSSM_DB_UNIQUE_RECORD_PTR
&outUniqueRecord
)
304 SSDatabase db
= findDbHandle(inDbHandle
);
305 CSSM_HANDLE resultsHandle
= CSSM_INVALID_HANDLE
;
306 SSUniqueRecord
uniqueId(db
);
308 // Setup so we always retrive the attributes even if the client
309 // doesn't want them so we can figure out if we just retrived a key.
310 CSSM_DB_RECORD_ATTRIBUTE_DATA attributes
;
311 CSSM_DB_RECORD_ATTRIBUTE_DATA_PTR pAttributes
;
313 pAttributes
= inoutAttributes
;
316 pAttributes
= &attributes
;
317 memset(pAttributes
, 0, sizeof(attributes
));
320 // Retrive the record.
321 CSSM_RETURN result
= CSSM_DL_DataGetFirst(db
->handle(), inQuery
, &resultsHandle
,
322 pAttributes
, inoutData
, uniqueId
);
325 if (result
== CSSMERR_DL_ENDOFDATA
)
326 return CSSM_INVALID_HANDLE
;
328 CssmError::throwMe(result
);
331 uniqueId
->activate();
333 // If we the client didn't ask for data then it doesn't matter
334 // if this record is a key or not, just return it.
337 if (pAttributes
->DataRecordType
== CSSM_DL_DB_RECORD_PUBLIC_KEY
338 || pAttributes
->DataRecordType
== CSSM_DL_DB_RECORD_PRIVATE_KEY
339 || pAttributes
->DataRecordType
== CSSM_DL_DB_RECORD_SYMMETRIC_KEY
)
341 // This record is a key, do the right thing (tm).
342 // Allocate storage for the key.
343 CssmKey
*outKey
= DatabaseSession::alloc
<CssmKey
>();
344 new SSKey(*this, *outKey
, db
, uniqueId
, pAttributes
->DataRecordType
, *inoutData
);
346 // Free the data we retrived (keyblob)
347 allocator().free(inoutData
->Data
);
349 // Set the length and data on the data we return to the client
350 inoutData
->Length
= sizeof(*outKey
);
351 inoutData
->Data
= reinterpret_cast<uint8
*>(outKey
);
355 outUniqueRecord
= makeSSUniqueRecord(uniqueId
);
356 return resultsHandle
;
360 SSDLSession::DataGetNext(CSSM_DB_HANDLE inDbHandle
,
361 CSSM_HANDLE inResultsHandle
,
362 CSSM_DB_RECORD_ATTRIBUTE_DATA_PTR inoutAttributes
,
364 CSSM_DB_UNIQUE_RECORD_PTR
&outUniqueRecord
)
366 // @@@ If this is a key do the right thing.
367 SSDatabase db
= findDbHandle(inDbHandle
);
368 SSUniqueRecord
uniqueId(db
);
370 // Setup so we always retrive the attributes even if the client
371 // doesn't want them so we can figure out if we just retrived a key.
372 CSSM_DB_RECORD_ATTRIBUTE_DATA attributes
;
373 CSSM_DB_RECORD_ATTRIBUTE_DATA_PTR pAttributes
;
375 pAttributes
= inoutAttributes
;
378 pAttributes
= &attributes
;
379 memset(pAttributes
, 0, sizeof(attributes
));
382 CSSM_RETURN result
= CSSM_DL_DataGetNext(db
->handle(), inResultsHandle
,
383 inoutAttributes
, inoutData
, uniqueId
);
386 if (result
== CSSMERR_DL_ENDOFDATA
)
389 CssmError::throwMe(result
);
392 uniqueId
->activate();
394 // If we the client didn't ask for data then it doesn't matter
395 // if this record is a key or not, just return it.
398 if (pAttributes
->DataRecordType
== CSSM_DL_DB_RECORD_PUBLIC_KEY
399 || pAttributes
->DataRecordType
== CSSM_DL_DB_RECORD_PRIVATE_KEY
400 || pAttributes
->DataRecordType
== CSSM_DL_DB_RECORD_SYMMETRIC_KEY
)
402 // This record is a key, do the right thing (tm).
403 // Allocate storage for the key.
404 CssmKey
*outKey
= DatabaseSession::alloc
<CssmKey
>();
405 new SSKey(*this, *outKey
, db
, uniqueId
, pAttributes
->DataRecordType
, *inoutData
);
407 // Free the data we retrived (keyblob)
408 allocator().free(inoutData
->Data
);
410 // Set the length and data on the data we return to the client
411 inoutData
->Length
= sizeof(*outKey
);
412 inoutData
->Data
= reinterpret_cast<uint8
*>(outKey
);
416 outUniqueRecord
= makeSSUniqueRecord(uniqueId
);
422 SSDLSession::DataAbortQuery(CSSM_DB_HANDLE inDbHandle
,
423 CSSM_HANDLE inResultsHandle
)
425 // @@@ If this is a key do the right thing.
426 SSDatabase db
= findDbHandle(inDbHandle
);
427 CSSM_RETURN result
= CSSM_DL_DataAbortQuery(db
->handle(), inResultsHandle
);
429 CssmError::throwMe(result
);
433 SSDLSession::DataGetFromUniqueRecordId(CSSM_DB_HANDLE inDbHandle
,
434 const CSSM_DB_UNIQUE_RECORD
&inUniqueRecord
,
435 CSSM_DB_RECORD_ATTRIBUTE_DATA_PTR inoutAttributes
,
438 SSDatabase db
= findDbHandle(inDbHandle
);
439 const SSUniqueRecord uniqueId
= findSSUniqueRecord(inUniqueRecord
);
441 // Setup so we always retrive the attributes even if the client
442 // doesn't want them so we can figure out if we just retrived a key.
443 CSSM_DB_RECORD_ATTRIBUTE_DATA attributes
;
444 CSSM_DB_RECORD_ATTRIBUTE_DATA_PTR pAttributes
;
446 pAttributes
= inoutAttributes
;
449 pAttributes
= &attributes
;
450 memset(pAttributes
, 0, sizeof(attributes
));
453 CSSM_RETURN result
= CSSM_DL_DataGetFromUniqueRecordId(db
->handle(),
454 uniqueId
, pAttributes
, inoutData
);
456 CssmError::throwMe(result
);
460 if (pAttributes
->DataRecordType
== CSSM_DL_DB_RECORD_PUBLIC_KEY
461 || pAttributes
->DataRecordType
== CSSM_DL_DB_RECORD_PRIVATE_KEY
462 || pAttributes
->DataRecordType
== CSSM_DL_DB_RECORD_SYMMETRIC_KEY
)
464 // This record is a key, do the right thing (tm).
465 // Allocate storage for the key.
466 CssmKey
*outKey
= DatabaseSession::alloc
<CssmKey
>();
467 new SSKey(*this, *outKey
, db
, uniqueId
, pAttributes
->DataRecordType
, *inoutData
);
469 // Free the data we retrived (keyblob)
470 allocator().free(inoutData
->Data
);
472 // Set the length and data on the data we return to the client
473 inoutData
->Length
= sizeof(*outKey
);
474 inoutData
->Data
= reinterpret_cast<uint8
*>(outKey
);
480 SSDLSession::FreeUniqueRecord(CSSM_DB_HANDLE inDbHandle
,
481 CSSM_DB_UNIQUE_RECORD
&inUniqueRecordIdentifier
)
483 killSSUniqueRecord(inUniqueRecordIdentifier
);
486 static const uint32 kGenericAttributeNames
[] =
488 'cdat', 'mdat', 'desc', 'icmt', 'crtr', 'type', 'scrp', 7, 8, 'invi', 'nega', 'cusi', 'prot', 'acct', 'svce',
492 const uint32 kNumGenericAttributes
= sizeof (kGenericAttributeNames
) / sizeof (uint32
);
494 static const uint32 kApplesharePasswordNames
[] =
496 'cdat', 'mdat', 'desc', 'icmt', 'crtr', 'type', 'scrp', 7, 8, 'invi', 'nega', 'cusi', 'prot', 'acct', 'vlme',
497 'srvr', 'ptcl', 'addr', 'ssig'
500 const uint32 kNumApplesharePasswordAttributes
= sizeof (kApplesharePasswordNames
) / sizeof (uint32
);
502 static const uint32 kInternetPasswordNames
[] =
504 'cdat', 'mdat', 'desc', 'icmt', 'crtr', 'type', 'scrp', 7, 8, 'invi', 'nega', 'cusi', 'prot', 'acct', 'sdmn',
505 'srvr', 'ptcl', 'atyp', 'port', 'path'
508 const uint32 kNumInternetPasswordAttributes
= sizeof (kInternetPasswordNames
) / sizeof (uint32
);
510 const uint32 kKeyAttributeNames
[] =
512 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26
515 const uint32 kNumKeyAttributes
= sizeof (kKeyAttributeNames
) / sizeof (uint32
);
517 const uint32 kCertificateAttributeNames
[] =
519 'ctyp', 'cenc', 'labl', 'alis', 'subj', 'issu', 'snbr', 'skid', 'hpky'
522 const uint32 kNumCertificateAttributes
= sizeof (kCertificateAttributeNames
) / sizeof (uint32
);
524 const unsigned kSymmetricKeyLabel
= 6; // record id for the symmetric key
525 const unsigned kLabelSize
= 20;
526 const unsigned kNumSymmetricAttributes
= 27; // number of attributes to request
528 static void appendUInt32ToData (const uint32 value
, CssmDataContainer
&data
)
530 data
.append (CssmPolyData (uint32 (htonl (value
))));
533 static inline uint32
GetUInt32AtFinger (uint8
*&finger
)
535 uint32 a
= ((finger
[0] << 24) | (finger
[1] << 16) | (finger
[2] << 8) | finger
[3]);
536 finger
+= sizeof (uint32
);
541 SSDLSession::unwrapAttributesAndData (uint32
&numAttributes
,
542 CSSM_DB_ATTRIBUTE_DATA_PTR
&attributes
,
546 // get the number of attributes
547 uint8
* finger
= input
.Data
;
548 numAttributes
= GetUInt32AtFinger (finger
);
550 // compute the end of the data for sanity checking later
551 uint8
* maximum
= input
.Data
+ input
.Length
;
553 // make the attribute array
554 attributes
= (CSSM_DB_ATTRIBUTE_DATA
*) allocator ().malloc (numAttributes
* sizeof (CSSM_DB_ATTRIBUTE_DATA
));
556 // for each attribute, retrieve the name format, name, type, and number of values
558 for (i
= 0; i
< numAttributes
; ++i
)
560 attributes
[i
].Info
.AttributeNameFormat
= GetUInt32AtFinger (finger
);
561 attributes
[i
].Info
.Label
.AttributeID
= GetUInt32AtFinger (finger
);
562 attributes
[i
].Info
.AttributeFormat
= GetUInt32AtFinger (finger
);
563 attributes
[i
].NumberOfValues
= GetUInt32AtFinger (finger
);
565 // for each value, get the length and data
566 attributes
[i
].Value
= (CSSM_DATA
*) allocator ().malloc (sizeof (CSSM_DATA
) * attributes
[i
].NumberOfValues
);
568 for (j
= 0; j
< attributes
[i
].NumberOfValues
; ++j
)
570 attributes
[i
].Value
[j
].Length
= GetUInt32AtFinger (finger
);
571 if (attributes
[i
].Value
[j
].Length
!= 0)
573 // sanity check what we are about to do
574 if (finger
> maximum
|| finger
+ attributes
[i
].Value
[j
].Length
> maximum
)
576 CssmError::throwMe (CSSM_ERRCODE_INVALID_POINTER
);
579 attributes
[i
].Value
[j
].Data
= (uint8
*) allocator ().malloc (attributes
[i
].Value
[j
].Length
);
581 switch (attributes
[i
].Info
.AttributeFormat
)
585 memmove (attributes
[i
].Value
[j
].Data
, finger
, attributes
[i
].Value
[j
].Length
);
586 finger
+= attributes
[i
].Value
[j
].Length
;
590 case CSSM_DB_ATTRIBUTE_FORMAT_SINT32
:
591 case CSSM_DB_ATTRIBUTE_FORMAT_UINT32
:
593 *(uint32
*) attributes
[i
].Value
[j
].Data
= GetUInt32AtFinger (finger
);
597 case CSSM_DB_ATTRIBUTE_FORMAT_MULTI_UINT32
:
599 uint32
* d
= (uint32
*) attributes
[i
].Value
[j
].Data
;
600 uint32 numValues
= attributes
[i
].Value
[j
].Length
/ sizeof (UInt32
);
603 *d
++ = GetUInt32AtFinger (finger
);
611 attributes
[i
].Value
[j
].Data
= NULL
;
617 data
.Length
= GetUInt32AtFinger (finger
);
618 if (data
.Length
!= 0)
620 // sanity check the pointer
621 if (finger
+ data
.Length
> maximum
)
623 CssmError::throwMe (CSSM_ERRCODE_INVALID_POINTER
);
626 data
.Data
= (uint8
*) allocator ().malloc (data
.Length
);
627 memmove (data
.Data
, finger
, data
.Length
);
628 finger
+= data
.Length
;
637 SSDLSession::getWrappedAttributesAndData (SSDatabase
&db
,
638 CSSM_DB_RECORDTYPE recordType
,
639 CSSM_DB_UNIQUE_RECORD_PTR recordPtr
,
640 CssmDataContainer
&output
,
643 // figure out which attributes to use
644 const uint32
* attributeNameArray
;
645 uint32 numAttributeNames
;
649 case CSSM_DL_DB_RECORD_GENERIC_PASSWORD
:
651 attributeNameArray
= kGenericAttributeNames
;
652 numAttributeNames
= kNumGenericAttributes
;
656 case CSSM_DL_DB_RECORD_INTERNET_PASSWORD
:
658 attributeNameArray
= kInternetPasswordNames
;
659 numAttributeNames
= kNumInternetPasswordAttributes
;
663 case CSSM_DL_DB_RECORD_APPLESHARE_PASSWORD
:
665 attributeNameArray
= kApplesharePasswordNames
;
666 numAttributeNames
= kNumApplesharePasswordAttributes
;
670 case CSSM_DL_DB_RECORD_X509_CERTIFICATE
:
672 attributeNameArray
= kCertificateAttributeNames
;
673 numAttributeNames
= kNumCertificateAttributes
;
677 case CSSM_DL_DB_RECORD_PUBLIC_KEY
:
678 case CSSM_DL_DB_RECORD_PRIVATE_KEY
:
679 case CSSM_DL_DB_RECORD_SYMMETRIC_KEY
:
681 attributeNameArray
= kKeyAttributeNames
;
682 numAttributeNames
= kNumKeyAttributes
;
688 CssmError::throwMe (CSSMERR_DL_FUNCTION_NOT_IMPLEMENTED
);
692 // make the attribute array
693 size_t arraySize
= numAttributeNames
* sizeof (CSSM_DB_ATTRIBUTE_DATA
);
695 CSSM_DB_ATTRIBUTE_DATA_PTR attributes
=
696 (CSSM_DB_ATTRIBUTE_DATA_PTR
) allocator ().malloc (arraySize
);
698 // initialize the array
699 memset (attributes
, 0, arraySize
);
701 for (i
= 0; i
< numAttributeNames
; ++i
)
703 attributes
[i
].Info
.AttributeNameFormat
= CSSM_DB_ATTRIBUTE_NAME_AS_INTEGER
;
704 attributes
[i
].Info
.Label
.AttributeID
= attributeNameArray
[i
];
707 // make the attribute record
708 CSSM_DB_RECORD_ATTRIBUTE_DATA attrData
;
709 attrData
.DataRecordType
= recordType
;
710 attrData
.SemanticInformation
= 0;
711 attrData
.NumberOfAttributes
= numAttributeNames
;
712 attrData
.AttributeData
= attributes
;
715 CssmDataContainer data
;
716 CSSM_RETURN result
= CSSM_DL_DataGetFromUniqueRecordId (db
->handle (),
722 CssmError::throwMe (result
);
725 // wrap the data -- write the number of attributes
726 appendUInt32ToData (numAttributeNames
, output
);
728 // for each attribute, write the type and number of values
729 for (i
= 0; i
< numAttributeNames
; ++i
)
731 appendUInt32ToData (attributes
[i
].Info
.AttributeNameFormat
, output
);
732 appendUInt32ToData (attributes
[i
].Info
.Label
.AttributeID
, output
);
733 appendUInt32ToData (attributes
[i
].Info
.AttributeFormat
, output
);
734 appendUInt32ToData (attributes
[i
].NumberOfValues
, output
);
736 // for each value, write the name format, name, length and the data
738 for (j
= 0; j
< attributes
[i
].NumberOfValues
; ++j
)
740 appendUInt32ToData (attributes
[i
].Value
[j
].Length
, output
);
741 if (attributes
[i
].Value
[j
].Length
!= 0)
743 switch (attributes
[i
].Info
.AttributeFormat
)
747 output
.append (CssmPolyData (attributes
[i
].Value
[j
]));
751 case CSSM_DB_ATTRIBUTE_FORMAT_SINT32
:
752 case CSSM_DB_ATTRIBUTE_FORMAT_UINT32
:
754 uint32 n
= htonl (*(uint32
*) attributes
[i
].Value
[j
].Data
);
756 d
.Length
= sizeof (uint32
);
757 d
.Data
= (uint8
*) &n
;
758 output
.append (CssmPolyData (d
));
766 // write the length of the data
767 appendUInt32ToData (data
.Length
, output
);
769 // write the data itself
770 if (data
.Length
!= 0)
772 output
.append (CssmPolyData (data
));
776 for (i
= 0; i
< numAttributeNames
; ++i
)
779 for (j
= 0; j
< attributes
[i
].NumberOfValues
; ++j
)
781 allocator ().free (attributes
[i
].Value
[j
].Data
);
784 allocator ().free (attributes
[i
].Value
);
787 allocator ().free (attributes
);
789 // copy out the data if the caller needs it
792 dataBlob
->Data
= data
.Data
;
793 dataBlob
->Length
= data
.Length
;
800 SSDLSession::getUniqueIdForSymmetricKey (SSDatabase
&db
, CSSM_DATA
&label
,
801 CSSM_DB_UNIQUE_RECORD_PTR
&uniqueRecord
)
803 // set up a query to get the key
804 CSSM_SELECTION_PREDICATE predicate
;
805 predicate
.DbOperator
= CSSM_DB_EQUAL
;
806 predicate
.Attribute
.Info
.AttributeNameFormat
= CSSM_DB_ATTRIBUTE_NAME_AS_INTEGER
;
807 predicate
.Attribute
.Info
.Label
.AttributeID
= kSymmetricKeyLabel
;
808 predicate
.Attribute
.Info
.AttributeFormat
= CSSM_DB_ATTRIBUTE_FORMAT_BLOB
;
809 predicate
.Attribute
.NumberOfValues
= 1;
810 // the label of the corresponding key is the first 20 bytes of the blob we returned
811 predicate
.Attribute
.Value
= &label
;
814 query
.RecordType
= CSSM_DL_DB_RECORD_SYMMETRIC_KEY
;
815 query
.Conjunctive
= CSSM_DB_NONE
;
816 query
.NumSelectionPredicates
= 1;
817 query
.SelectionPredicate
= &predicate
;
819 // fill out the record data
820 CSSM_DB_RECORD_ATTRIBUTE_DATA recordAttributeData
;
821 recordAttributeData
.DataRecordType
= CSSM_DL_DB_RECORD_SYMMETRIC_KEY
;
822 recordAttributeData
.SemanticInformation
= 0;
823 recordAttributeData
.NumberOfAttributes
= 0;
824 recordAttributeData
.AttributeData
= NULL
;
828 CSSM_RETURN result
= CSSM_DL_DataGetFirst (db
->handle (), &query
, &handle
, &recordAttributeData
, NULL
,
832 CssmError::throwMe (result
);
836 CSSM_DL_DataAbortQuery (db
->handle (), handle
);
840 SSDLSession::getCorrespondingSymmetricKey (SSDatabase
&db
, CSSM_DATA
&labelData
, CssmDataContainer
&data
)
843 CSSM_DB_UNIQUE_RECORD_PTR uniqueRecord
;
844 getUniqueIdForSymmetricKey (db
, labelData
, uniqueRecord
);
846 // from this. get the wrapped attributes and data
847 getWrappedAttributesAndData (db
, CSSM_DL_DB_RECORD_SYMMETRIC_KEY
, uniqueRecord
, data
, NULL
);
849 // clean up after the query
850 CSSM_DL_FreeUniqueRecord (db
->handle (), uniqueRecord
);
853 void SSDLSession::doGetWithoutEncryption (SSDatabase
&db
, const void *inInputParams
, void **outOutputParams
)
855 CSSM_APPLECSPDL_DB_GET_WITHOUT_ENCRYPTION_PARAMETERS
* params
=
856 (CSSM_APPLECSPDL_DB_GET_WITHOUT_ENCRYPTION_PARAMETERS
*) inInputParams
;
858 SSUniqueRecord uniqueID
= findSSUniqueRecord(*(params
->uniqueID
));
860 CSSM_DATA
*outputData
= (CSSM_DATA
*) outOutputParams
;
861 CssmDataContainer output
;
863 // get the record type and requested attributes from the DL
864 CssmDataContainer data
;
865 CSSM_RETURN result
= CSSM_DL_DataGetFromUniqueRecordId(db
->handle(),
872 CssmError::throwMe(result
);
875 // get the real data and all of the attributes from the DL
876 CssmDataContainer blobData
;
877 getWrappedAttributesAndData (db
, params
->attributes
->DataRecordType
, uniqueID
, data
, &blobData
);
879 // write out the data blob
880 appendUInt32ToData (data
.Length
, output
);
881 output
.append (CssmPolyData (data
));
883 // figure out what we need to do with the key blob
884 CssmDataContainer key
;
885 switch (params
->attributes
->DataRecordType
)
887 case CSSM_DL_DB_RECORD_GENERIC_PASSWORD
:
888 case CSSM_DL_DB_RECORD_INTERNET_PASSWORD
:
889 case CSSM_DL_DB_RECORD_APPLESHARE_PASSWORD
:
891 // the label is the first kLabelSize bytes of the resultant data blob
892 CSSM_DATA label
= {kLabelSize
, blobData
.Data
};
895 getCorrespondingSymmetricKey (db
, label
, key
);
906 // write out the length of the key blob
907 appendUInt32ToData (key
.Length
, output
);
912 output
.append (CssmPolyData (key
));
915 // copy out the results
916 outputData
->Data
= output
.Data
;
918 outputData
->Length
= output
.Length
;
923 SSDLSession::cleanupAttributes (uint32 numAttributes
, CSSM_DB_ATTRIBUTE_DATA_PTR attributes
)
926 for (i
= 0; i
< numAttributes
; ++i
)
929 for (j
= 0; j
< attributes
[i
].NumberOfValues
; ++j
)
931 free (attributes
[i
].Value
[j
].Data
);
934 free (attributes
[i
].Value
);
941 SSDLSession::doModifyWithoutEncryption (SSDatabase
&db
, const void* inInputParams
, void** outOutputParams
)
944 CSSM_APPLECSPDL_DB_MODIFY_WITHOUT_ENCRYPTION_PARAMETERS
* params
=
945 (CSSM_APPLECSPDL_DB_MODIFY_WITHOUT_ENCRYPTION_PARAMETERS
*) inInputParams
;
947 // extract the data for this modify.
948 uint8
* finger
= params
->data
->Data
;
950 data
.Length
= GetUInt32AtFinger (finger
);
952 if (data
.Length
+ sizeof (UInt32
) > params
->data
->Length
)
954 CssmError::throwMe (CSSM_ERRCODE_INVALID_POINTER
);
958 finger
+= data
.Length
;
960 // reconstruct the attributes and data
961 uint32 numAttributes
;
962 CSSM_DB_ATTRIBUTE_DATA_PTR attributes
;
963 CssmDataContainer dataBlob
;
965 unwrapAttributesAndData (numAttributes
, attributes
, dataBlob
, data
);
967 CSSM_DB_RECORD_ATTRIBUTE_DATA attrData
;
968 attrData
.DataRecordType
= params
->attributes
->DataRecordType
;
969 attrData
.SemanticInformation
= 0;
970 attrData
.NumberOfAttributes
= numAttributes
;
971 attrData
.AttributeData
= attributes
;
973 // get the unique ID for this record (from the db's perspective)
974 SSUniqueRecord uniqueID
= findSSUniqueRecord(*(params
->uniqueID
));
975 CSSM_DB_UNIQUE_RECORD
*uniqueIDPtr
= uniqueID
; // for readability. There's cast overloading
978 switch (attrData
.DataRecordType
)
980 case CSSM_DL_DB_RECORD_GENERIC_PASSWORD
:
981 case CSSM_DL_DB_RECORD_INTERNET_PASSWORD
:
982 case CSSM_DL_DB_RECORD_APPLESHARE_PASSWORD
:
984 // read off the data so that we can update the key
985 CssmDataContainer oldData
;
986 result
= CSSM_DL_DataGetFromUniqueRecordId (db
->handle(),
992 CssmError::throwMe (result
);
995 CSSM_DB_MODIFY_MODE modifyMode
= params
->modifyMode
;
997 // parse the key data blob
998 CssmDataContainer keyBlob
;
999 data
.Length
= GetUInt32AtFinger (finger
);
1002 CSSM_DB_RECORD_ATTRIBUTE_DATA
* attrDataPtr
= NULL
;
1003 CSSM_DB_RECORD_ATTRIBUTE_DATA attrData
;
1005 CSSM_DATA labelData
= {kLabelSize
, oldData
.Data
};
1006 CSSM_DB_UNIQUE_RECORD_PTR recordID
;
1007 getUniqueIdForSymmetricKey (db
, labelData
, recordID
);
1009 CSSM_DB_ATTRIBUTE_DATA_PTR keyAttributes
;
1010 uint32 numKeyAttributes
;
1011 unwrapAttributesAndData (numKeyAttributes
, keyAttributes
, keyBlob
, data
);
1013 // make the attribute data
1014 attrData
.DataRecordType
= params
->recordType
;
1015 attrData
.SemanticInformation
= 0;
1016 attrData
.NumberOfAttributes
= numKeyAttributes
;
1017 attrData
.AttributeData
= keyAttributes
;
1019 attrDataPtr
= &attrData
;
1021 result
= CSSM_DL_DataModify (db
->handle(),
1022 CSSM_DL_DB_RECORD_SYMMETRIC_KEY
,
1029 CSSM_DL_FreeUniqueRecord (db
->handle (), recordID
);
1031 cleanupAttributes (numKeyAttributes
, keyAttributes
);
1041 // save off the new data
1042 result
= CSSM_DL_DataModify(db
->handle(),
1047 params
->modifyMode
);
1050 cleanupAttributes (numAttributes
, attributes
);
1054 CssmError::throwMe(result
);
1060 SSDLSession::doInsertWithoutEncryption (SSDatabase
&db
, const void* inInputParams
, void** outOutputParams
)
1064 CSSM_APPLECSPDL_DB_INSERT_WITHOUT_ENCRYPTION_PARAMETERS
* params
=
1065 (CSSM_APPLECSPDL_DB_INSERT_WITHOUT_ENCRYPTION_PARAMETERS
*) inInputParams
;
1067 // extract the data for this insert.
1068 uint8
* finger
= params
->data
.Data
;
1070 data
.Length
= GetUInt32AtFinger (finger
);
1072 finger
+= data
.Length
;
1074 // reconstruct the attributes and data
1075 uint32 numAttributes
;
1076 CSSM_DB_ATTRIBUTE_DATA_PTR attributes
;
1079 unwrapAttributesAndData (numAttributes
, attributes
, dataBlob
, data
);
1081 // make the attribute data
1082 CSSM_DB_RECORD_ATTRIBUTE_DATA attrData
;
1083 attrData
.DataRecordType
= params
->recordType
;
1084 attrData
.SemanticInformation
= 0;
1085 attrData
.NumberOfAttributes
= numAttributes
;
1086 attrData
.AttributeData
= attributes
;
1088 // insert into the database
1089 SSUniqueRecord
uniqueID (db
);
1090 result
= CSSM_DL_DataInsert (db
->handle(), params
->recordType
,
1096 allocator ().free (dataBlob
.Data
);
1097 cleanupAttributes (numAttributes
, attributes
);
1099 // attach into the CSP/DL mechanism
1100 CSSM_DB_UNIQUE_RECORD_PTR newRecord
= makeSSUniqueRecord(uniqueID
);
1101 *(CSSM_DB_UNIQUE_RECORD_PTR
*) outOutputParams
= newRecord
;
1105 CssmError::throwMe(result
);
1108 // Get the key data for this insert
1109 data
.Length
= GetUInt32AtFinger (finger
);
1110 if (data
.Length
!= 0)
1114 // parse the key data blob
1115 unwrapAttributesAndData (numAttributes
, attributes
, dataBlob
, data
);
1117 // make the attribute data
1118 CSSM_DB_RECORD_ATTRIBUTE_DATA attrData
;
1119 attrData
.DataRecordType
= params
->recordType
;
1120 attrData
.SemanticInformation
= 0;
1121 attrData
.NumberOfAttributes
= numAttributes
;
1122 attrData
.AttributeData
= attributes
;
1124 // insert the key data into the symmetric key table
1125 CSSM_DB_UNIQUE_RECORD_PTR uniqueRecord
;
1126 result
= CSSM_DL_DataInsert (db
->handle(), CSSM_DL_DB_RECORD_SYMMETRIC_KEY
, &attrData
, &dataBlob
,
1130 CssmError::throwMe (result
);
1133 // clean up after inserting the key
1134 CSSM_DL_FreeUniqueRecord (db
->handle (), uniqueRecord
);
1135 allocator ().free (dataBlob
.Data
);
1136 cleanupAttributes (numAttributes
, attributes
);
1141 SSDLSession::doConvertRecordIdentifier (SSDatabase
&db
, const void *inInputParams
, void **outOutputParams
)
1143 SSUniqueRecord
uniqueId (db
);
1145 // clone the unique record
1146 CSSM_DB_UNIQUE_RECORD_PTR clone
= (CSSM_DB_UNIQUE_RECORD_PTR
) allocator ().malloc (sizeof (CSSM_DB_UNIQUE_RECORD
));
1147 *clone
= *(CSSM_DB_UNIQUE_RECORD_PTR
) inInputParams
;
1149 // set the value of the unique record
1150 uniqueId
->setUniqueRecordPtr (clone
);
1152 // byte swap the retrieved record pointer to host order
1153 uint32
* idArray
= (uint32
*) clone
->RecordIdentifier
.Data
;
1154 idArray
[0] = ntohl (idArray
[0]);
1155 idArray
[1] = ntohl (idArray
[1]);
1156 idArray
[2] = ntohl (idArray
[2]);
1158 CSSM_DB_UNIQUE_RECORD_PTR newRecord
= makeSSUniqueRecord(uniqueId
);
1159 *(CSSM_DB_UNIQUE_RECORD_PTR
*) outOutputParams
= newRecord
;
1163 SSDLSession::PassThrough(CSSM_DB_HANDLE inDbHandle
,
1164 uint32 inPassThroughId
,
1165 const void *inInputParams
,
1166 void **outOutputParams
)
1168 if (inPassThroughId
== CSSM_APPLECSPDL_DB_CREATE_WITH_BLOB
)
1170 CSSM_APPLE_CSPDL_DB_CREATE_WITH_BLOB_PARAMETERS
* params
= (CSSM_APPLE_CSPDL_DB_CREATE_WITH_BLOB_PARAMETERS
*) inInputParams
;
1171 CreateWithBlob(params
->dbName
, params
->dbLocation
, *params
->dbInfo
, params
->accessRequest
, params
->openParameters
, *params
->blob
,
1172 * (CSSM_DB_HANDLE
*) outOutputParams
);
1176 SSDatabase db
= findDbHandle(inDbHandle
);
1177 switch (inPassThroughId
)
1179 case CSSM_APPLECSPDL_DB_LOCK
:
1182 case CSSM_APPLECSPDL_DB_UNLOCK
:
1184 db
->unlock(*reinterpret_cast<const CSSM_DATA
*>(inInputParams
));
1188 case CSSM_APPLECSPDL_DB_GET_SETTINGS
:
1190 if (!outOutputParams
)
1191 CssmError::throwMe(CSSM_ERRCODE_INVALID_OUTPUT_POINTER
);
1193 CSSM_APPLECSPDL_DB_SETTINGS_PARAMETERS_PTR params
=
1194 DatabaseSession::alloc
<CSSM_APPLECSPDL_DB_SETTINGS_PARAMETERS
>();
1199 db
->getSettings(idleTimeout
, lockOnSleep
);
1200 params
->idleTimeout
= idleTimeout
;
1201 params
->lockOnSleep
= lockOnSleep
;
1205 allocator().free(params
);
1208 *reinterpret_cast<CSSM_APPLECSPDL_DB_SETTINGS_PARAMETERS_PTR
*>(outOutputParams
) = params
;
1211 case CSSM_APPLECSPDL_DB_SET_SETTINGS
:
1214 CssmError::throwMe(CSSM_ERRCODE_INVALID_INPUT_POINTER
);
1216 const CSSM_APPLECSPDL_DB_SETTINGS_PARAMETERS
*params
=
1217 reinterpret_cast<const CSSM_APPLECSPDL_DB_SETTINGS_PARAMETERS
*>(inInputParams
);
1218 db
->setSettings(params
->idleTimeout
, params
->lockOnSleep
);
1221 case CSSM_APPLECSPDL_DB_IS_LOCKED
:
1223 if (!outOutputParams
)
1224 CssmError::throwMe(CSSM_ERRCODE_INVALID_OUTPUT_POINTER
);
1226 CSSM_APPLECSPDL_DB_IS_LOCKED_PARAMETERS_PTR params
=
1227 DatabaseSession::alloc
<CSSM_APPLECSPDL_DB_IS_LOCKED_PARAMETERS
>();
1230 params
->isLocked
= db
->isLocked();
1234 allocator().free(params
);
1237 *reinterpret_cast<CSSM_APPLECSPDL_DB_IS_LOCKED_PARAMETERS_PTR
*>(outOutputParams
) = params
;
1240 case CSSM_APPLECSPDL_DB_CHANGE_PASSWORD
:
1243 CssmError::throwMe(CSSM_ERRCODE_INVALID_INPUT_POINTER
);
1245 const CSSM_APPLECSPDL_DB_CHANGE_PASSWORD_PARAMETERS
*params
=
1246 reinterpret_cast<const CSSM_APPLECSPDL_DB_CHANGE_PASSWORD_PARAMETERS
*>(inInputParams
);
1247 db
->changePassphrase(params
->accessCredentials
);
1250 case CSSM_APPLECSPDL_DB_GET_HANDLE
:
1252 using SecurityServer::DbHandle
;
1253 Required(outOutputParams
, CSSM_ERRCODE_INVALID_OUTPUT_POINTER
);
1254 *reinterpret_cast<CSSM_DL_DB_HANDLE
*>(outOutputParams
) = db
->handle();
1257 case CSSM_APPLECSPDL_CSP_RECODE
:
1260 CssmError::throwMe(CSSM_ERRCODE_INVALID_INPUT_POINTER
);
1262 const CSSM_APPLECSPDL_RECODE_PARAMETERS
*params
=
1263 reinterpret_cast<const CSSM_APPLECSPDL_RECODE_PARAMETERS
*>(inInputParams
);
1265 db
->recode(CssmData::overlay(params
->dbBlob
),
1266 CssmData::overlay(params
->extraData
));
1269 case CSSM_APPLECSPDL_DB_GET_RECORD_IDENTIFIER
:
1271 SSUniqueRecord uniqueID
= findSSUniqueRecord(*(CSSM_DB_UNIQUE_RECORD_PTR
) inInputParams
);
1272 db
->getRecordIdentifier(uniqueID
, *reinterpret_cast<CSSM_DATA
*>(outOutputParams
));
1275 case CSSM_APPLECSPDL_DB_COPY_BLOB
:
1277 // make the output parameters
1278 db
->copyBlob(*reinterpret_cast<CSSM_DATA
*>(outOutputParams
));
1281 case CSSM_APPLECSPDL_DB_INSERT_WITHOUT_ENCRYPTION
:
1283 doInsertWithoutEncryption (db
, inInputParams
, outOutputParams
);
1286 case CSSM_APPLECSPDL_DB_MODIFY_WITHOUT_ENCRYPTION
:
1288 doModifyWithoutEncryption (db
, inInputParams
, outOutputParams
);
1291 case CSSM_APPLECSPDL_DB_GET_WITHOUT_ENCRYPTION
:
1293 doGetWithoutEncryption (db
, inInputParams
, outOutputParams
);
1296 case CSSM_APPLECSPDL_DB_CONVERT_RECORD_IDENTIFIER
:
1298 doConvertRecordIdentifier (db
, inInputParams
, outOutputParams
);
1303 CSSM_RETURN result
= CSSM_DL_PassThrough(db
->handle(), inPassThroughId
, inInputParams
, outOutputParams
);
1305 CssmError::throwMe(result
);
1312 SSDLSession::makeDbHandle(SSDatabase
&inDb
)
1314 StLock
<Mutex
> _(mDbHandleLock
);
1315 CSSM_DB_HANDLE aDbHandle
= inDb
->handle().DBHandle
;
1316 IFDEBUG(bool inserted
=) mDbHandleMap
.insert(DbHandleMap::value_type(aDbHandle
, inDb
)).second
;
1318 // fprintf(stderr, "%p Added %p to %p\n", pthread_self(), (void*) aDbHandle, (void*) this);
1323 SSDLSession::killDbHandle(CSSM_DB_HANDLE inDbHandle
)
1325 StLock
<Mutex
> _(mDbHandleLock
);
1326 DbHandleMap::iterator it
= mDbHandleMap
.find(inDbHandle
);
1327 if (it
== mDbHandleMap
.end())
1329 // fprintf(stderr, "Can't find %p in %p\n", (void*) inDbHandle, this);
1330 CssmError::throwMe(CSSMERR_DL_INVALID_DB_HANDLE
);
1333 SSDatabase db
= it
->second
;
1334 // fprintf(stderr, "%p Removed %p from %p\n", pthread_self(), (void*) it->first, (void*) this);
1335 mDbHandleMap
.erase(it
);
1340 SSDLSession::findDbHandle(CSSM_DB_HANDLE inDbHandle
)
1342 StLock
<Mutex
> _(mDbHandleLock
);
1343 // fprintf(stderr, "%p Looking for %p in %p\n", pthread_self(), (void*) inDbHandle, (void*) this);
1344 DbHandleMap::iterator it
= mDbHandleMap
.find(inDbHandle
);
1345 if (it
== mDbHandleMap
.end())
1347 // fprintf(stderr, "%p Can't find %p in %p\n", pthread_self(), (void*) inDbHandle, this);
1348 DbHandleMap::iterator it
= mDbHandleMap
.begin();
1349 while (it
!= mDbHandleMap
.end())
1351 // fprintf(stderr, "\t%p\n", (void*) it->first);
1355 CssmError::throwMe(CSSMERR_DL_INVALID_DB_HANDLE
);
1361 CSSM_DB_UNIQUE_RECORD_PTR
1362 SSDLSession::makeSSUniqueRecord(SSUniqueRecord
&uniqueId
)
1364 StLock
<Mutex
> _(mSSUniqueRecordLock
);
1365 CSSM_HANDLE ref
= CSSM_HANDLE(static_cast<CSSM_DB_UNIQUE_RECORD
*>(uniqueId
));
1366 IFDEBUG(bool inserted
=) mSSUniqueRecordMap
.insert(SSUniqueRecordMap::value_type(ref
, uniqueId
)).second
;
1368 return createUniqueRecord(ref
);
1372 SSDLSession::killSSUniqueRecord(CSSM_DB_UNIQUE_RECORD
&inUniqueRecord
)
1374 CSSM_HANDLE ref
= parseUniqueRecord(inUniqueRecord
);
1375 StLock
<Mutex
> _(mSSUniqueRecordLock
);
1376 SSUniqueRecordMap::iterator it
= mSSUniqueRecordMap
.find(ref
);
1377 if (it
== mSSUniqueRecordMap
.end())
1378 CssmError::throwMe(CSSMERR_DL_INVALID_RECORD_UID
);
1380 SSUniqueRecord uniqueRecord
= it
->second
;
1381 mSSUniqueRecordMap
.erase(it
);
1382 freeUniqueRecord(inUniqueRecord
);
1383 return uniqueRecord
;
1387 SSDLSession::findSSUniqueRecord(const CSSM_DB_UNIQUE_RECORD
&inUniqueRecord
)
1389 CSSM_HANDLE ref
= parseUniqueRecord(inUniqueRecord
);
1390 StLock
<Mutex
> _(mSSUniqueRecordLock
);
1391 SSUniqueRecordMap::iterator it
= mSSUniqueRecordMap
.find(ref
);
1392 if (it
== mSSUniqueRecordMap
.end())
1393 CssmError::throwMe(CSSMERR_DL_INVALID_RECORD_UID
);
1398 CSSM_DB_UNIQUE_RECORD_PTR
1399 SSDLSession::createUniqueRecord(CSSM_HANDLE ref
)
1401 CSSM_DB_UNIQUE_RECORD
*aUniqueRecord
= DatabaseSession::alloc
<CSSM_DB_UNIQUE_RECORD
>();
1402 memset(aUniqueRecord
, 0, sizeof(CSSM_DB_UNIQUE_RECORD
));
1403 aUniqueRecord
->RecordIdentifier
.Length
= sizeof(CSSM_HANDLE
);
1406 aUniqueRecord
->RecordIdentifier
.Data
= DatabaseSession::alloc
<uint8
>(sizeof(CSSM_HANDLE
));
1407 *reinterpret_cast<CSSM_HANDLE
*>(aUniqueRecord
->RecordIdentifier
.Data
) = ref
;
1411 free(aUniqueRecord
);
1415 return aUniqueRecord
;
1419 SSDLSession::parseUniqueRecord(const CSSM_DB_UNIQUE_RECORD
&inUniqueRecord
)
1421 if (inUniqueRecord
.RecordIdentifier
.Length
!= sizeof(CSSM_HANDLE
))
1422 CssmError::throwMe(CSSMERR_DL_INVALID_RECORD_UID
);
1424 return *reinterpret_cast<CSSM_HANDLE
*>(inUniqueRecord
.RecordIdentifier
.Data
);
1428 SSDLSession::freeUniqueRecord(CSSM_DB_UNIQUE_RECORD
&inUniqueRecord
)
1430 if (inUniqueRecord
.RecordIdentifier
.Length
!= 0
1431 && inUniqueRecord
.RecordIdentifier
.Data
!= NULL
)
1433 inUniqueRecord
.RecordIdentifier
.Length
= 0;
1434 allocator().free(inUniqueRecord
.RecordIdentifier
.Data
);
1436 allocator().free(&inUniqueRecord
);