2 * Copyright (c) 2000-2004 Apple Computer, 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@
25 // Keychains.h - The Keychain class
27 #ifndef _SECURITY_KEYCHAINS_H_
28 #define _SECURITY_KEYCHAINS_H_
30 #include <security_cdsa_client/cspclient.h>
31 #include <security_cdsa_client/dlclient.h>
32 #include <security_utilities/refcount.h>
33 #include <security_utilities/seccfobject.h>
34 #include <Security/SecKeychain.h>
35 #include <Security/SecKeychainItem.h>
37 #include "SecCFTypes.h"
38 #include "defaultcreds.h"
45 namespace KeychainCore
53 class KeychainSchemaImpl
: public RefCount
55 NOCOPY(KeychainSchemaImpl
)
57 friend class KeychainSchema
;
59 KeychainSchemaImpl(const CssmClient::Db
&db
);
61 virtual ~KeychainSchemaImpl();
63 CSSM_DB_ATTRIBUTE_FORMAT
attributeFormatFor(CSSM_DB_RECORDTYPE recordType
, uint32 attributeId
) const;
64 const CssmAutoDbRecordAttributeInfo
&primaryKeyInfosFor(CSSM_DB_RECORDTYPE recordType
) const;
66 bool operator <(const KeychainSchemaImpl
&other
) const;
67 bool operator ==(const KeychainSchemaImpl
&other
) const;
69 void getAttributeInfoForRecordType(CSSM_DB_RECORDTYPE recordType
, SecKeychainAttributeInfo
**Info
) const;
70 CssmDbAttributeInfo
attributeInfoFor(CSSM_DB_RECORDTYPE recordType
, uint32 attributeId
) const;
71 bool hasAttribute(CSSM_DB_RECORDTYPE recordType
, uint32 attributeId
) const;
72 bool hasRecordType(CSSM_DB_RECORDTYPE recordType
) const;
74 void didCreateRelation(CSSM_DB_RECORDTYPE inRelationID
,
75 const char *inRelationName
,
76 uint32 inNumberOfAttributes
,
77 const CSSM_DB_SCHEMA_ATTRIBUTE_INFO
*pAttributeInfo
,
78 uint32 inNumberOfIndexes
,
79 const CSSM_DB_SCHEMA_INDEX_INFO
*pIndexInfo
);
82 typedef map
<CSSM_DB_RECORDTYPE
, CssmAutoDbRecordAttributeInfo
*> PrimaryKeyInfoMap
;
83 PrimaryKeyInfoMap mPrimaryKeyInfoMap
;
85 typedef map
<uint32
, CSSM_DB_ATTRIBUTE_FORMAT
> RelationInfoMap
;
86 typedef map
<CSSM_DB_RECORDTYPE
, RelationInfoMap
> DatabaseInfoMap
;
87 DatabaseInfoMap mDatabaseInfoMap
;
91 const RelationInfoMap
&relationInfoMapFor(CSSM_DB_RECORDTYPE recordType
) const;
95 class KeychainSchema
: public RefPointer
<KeychainSchemaImpl
>
99 KeychainSchema(KeychainSchemaImpl
*impl
) : RefPointer
<KeychainSchemaImpl
>(impl
) {}
100 KeychainSchema(const CssmClient::Db
&db
) : RefPointer
<KeychainSchemaImpl
>(new KeychainSchemaImpl(db
)) {}
102 bool operator <(const KeychainSchema
&other
) const
103 { return ptr
&& other
.ptr
? *ptr
< *other
.ptr
: ptr
< other
.ptr
; }
104 bool operator ==(const KeychainSchema
&other
) const
105 { return ptr
&& other
.ptr
? *ptr
== *other
.ptr
: ptr
== other
.ptr
; }
108 typedef KeychainSchemaImpl Impl
;
114 class KeychainImpl
: public SecCFObject
, private CssmClient::Db::DefaultCredentialsMaker
118 SECCFFUNCTIONS(KeychainImpl
, SecKeychainRef
, errSecInvalidKeychain
, gTypes().KeychainImpl
)
120 friend class Keychain
;
121 friend class ItemImpl
;
123 KeychainImpl(const CssmClient::Db
&db
);
126 // Methods called by ItemImpl;
127 void didUpdate(const Item
&inItem
, PrimaryKey
&oldPK
,
129 void completeAdd(Item
&item
, PrimaryKey
&key
);
131 void markBlobForDotMacSyncUpdate(CssmData
&data
);
134 virtual ~KeychainImpl();
136 Mutex
* getKeychainMutex();
137 Mutex
* getMutexForObject();
138 void aboutToDestruct();
140 bool operator ==(const KeychainImpl
&) const;
143 void add(Item
&item
);
144 void addCopy(Item
&item
);
145 void deleteItem(Item
&item
); // item must be persistant.
148 void create(UInt32 passwordLength
, const void *inPassword
);
149 void createWithBlob(CssmData
&blob
);
150 void create(ConstStringPtr inPassword
);
152 void create(const ResourceControlContext
*rcc
);
155 // Locking and unlocking a keychain.
158 void unlock(const CssmData
&password
);
159 void unlock(ConstStringPtr password
); // @@@ This has a length limit, we should remove it.
161 void getSettings(uint32
&outIdleTimeOut
, bool &outLockOnSleep
);
162 void setSettings(uint32 inIdleTimeOut
, bool inLockOnSleep
);
164 // Passing in NULL for either oldPassword or newPassword will cause them to be prompted for.
165 // To specify a zero length password in either case the oldPasswordLength or newPasswordLength
166 // value must be 0 and the oldPassword or newPassword must not be NULL.
167 void changePassphrase(UInt32 oldPasswordLength
, const void *oldPassword
,
168 UInt32 newPasswordLength
, const void *newPassword
);
169 void changePassphrase(ConstStringPtr oldPassword
, ConstStringPtr newPassword
);
171 void authenticate(const CSSM_ACCESS_CREDENTIALS
*cred
); // Does not do an unlock.
173 const char *name() const { return mDb
->name(); }
174 UInt32
status() const;
176 bool isActive() const;
178 KCCursor
createCursor(const SecKeychainAttributeList
*attrList
);
179 KCCursor
createCursor(SecItemClass itemClass
, const SecKeychainAttributeList
*attrList
);
180 CssmClient::Db
database() { return mDb
; }
181 DLDbIdentifier
dlDbIdentifier() const { return mDb
->dlDbIdentifier(); }
183 CssmClient::CSP
csp();
185 PrimaryKey
makePrimaryKey(CSSM_DB_RECORDTYPE recordType
, CssmClient::DbUniqueRecord
&uniqueId
);
186 void gatherPrimaryKeyAttributes(CssmClient::DbAttributes
& primaryKeyAttrs
);
188 const CssmAutoDbRecordAttributeInfo
&primaryKeyInfosFor(CSSM_DB_RECORDTYPE recordType
);
190 Item
item(const PrimaryKey
& primaryKey
);
191 Item
item(CSSM_DB_RECORDTYPE recordType
, CssmClient::DbUniqueRecord
&uniqueId
);
193 CssmDbAttributeInfo
attributeInfoFor(CSSM_DB_RECORDTYPE recordType
, UInt32 tag
);
194 void getAttributeInfoForItemID(CSSM_DB_RECORDTYPE itemID
, SecKeychainAttributeInfo
**Info
);
195 static void freeAttributeInfo(SecKeychainAttributeInfo
*Info
);
196 KeychainSchema
keychainSchema();
198 void didDeleteItem(ItemImpl
*inItemImpl
);
200 void recode(const CssmData
&data
, const CssmData
&extraData
);
201 void copyBlob(CssmData
&dbBlob
);
203 void setBatchMode(Boolean mode
, Boolean rollBack
);
205 // yield default open() credentials for this keychain (as of now)
206 const AccessCredentials
*defaultCredentials();
208 // Only call these functions while holding globals().apiLock.
209 bool inCache() const throw() { return mInCache
; }
210 void inCache(bool inCache
) throw() { mInCache
= inCache
; }
212 void postEvent(SecKeychainEvent kcEvent
, ItemImpl
* item
);
214 void addItem(const PrimaryKey
&primaryKey
, ItemImpl
*dbItemImpl
);
217 void removeItem(const PrimaryKey
&primaryKey
, ItemImpl
*inItemImpl
);
218 ItemImpl
*_lookupItem(const PrimaryKey
&primaryKey
);
220 const AccessCredentials
*makeCredentials();
222 typedef map
<PrimaryKey
, __weak ItemImpl
*> DbItemMap
;
223 // Weak reference map of all items we know about that have a primaryKey
224 DbItemMap mDbItemMap
;
225 // True iff we are in the cache of keychains in StorageManager
230 KeychainSchema mKeychainSchema
;
232 // Data for auto-unlock credentials
233 DefaultCredentials mCustomUnlockCreds
;
235 EventBuffer
*mEventBuffer
;
240 CFIndex
GetKeychainRetainCount(Keychain
& kc
);
242 class Keychain
: public SecPointer
<KeychainImpl
>
246 Keychain(KeychainImpl
*impl
) : SecPointer
<KeychainImpl
>(impl
) {}
248 static Keychain
optional(SecKeychainRef handle
);
251 friend class StorageManager
;
252 Keychain(const CssmClient::Db
&db
)
253 : SecPointer
<KeychainImpl
>(new KeychainImpl(db
)) {}
255 typedef KeychainImpl Impl
;
259 } // end namespace KeychainCore
261 } // end namespace Security
263 #endif // !_SECURITY_KEYCHAINS_H_