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.
22 Contains: The keychain class
24 Copyright: 2000 by Apple Computer, Inc., all rights reserved.
32 #include <Security/dlclient.h>
33 #include <Security/refcount.h>
34 #include <Security/utilities.h>
35 #include <Security/DLDBListCFPref.h>
36 #include <Security/Refs.h>
37 #include <Security/SecKeychainAPI.h>
38 #include <Security/SecKeychainAPIPriv.h>
44 namespace KeychainCore
55 class KeychainSchemaImpl
: public ReferencedObject
58 KeychainSchemaImpl(const CssmClient::Db
&db
);
59 ~KeychainSchemaImpl();
61 CSSM_DB_ATTRIBUTE_FORMAT
attributeFormatFor(CSSM_DB_RECORDTYPE recordType
, uint32 attributeId
) const;
62 const CssmAutoDbRecordAttributeInfo
&primaryKeyInfosFor(CSSM_DB_RECORDTYPE recordType
);
64 bool operator <(const KeychainSchemaImpl
&other
) const;
65 bool operator ==(const KeychainSchemaImpl
&other
) const;
67 void getAttributeInfoForRecordType(CSSM_DB_RECORDTYPE recordType
, SecKeychainAttributeInfo
**Info
);
68 CssmDbAttributeInfo
attributeInfoForTag(UInt32 tag
);
71 typedef map
<CSSM_DB_RECORDTYPE
, CssmAutoDbRecordAttributeInfo
*> PrimaryKeyInfoMap
;
72 PrimaryKeyInfoMap mPrimaryKeyInfoMap
;
74 typedef map
<uint32
, CSSM_DB_ATTRIBUTE_FORMAT
> RelationInfoMap
;
75 typedef map
<CSSM_DB_RECORDTYPE
, RelationInfoMap
> DatabaseInfoMap
;
76 DatabaseInfoMap mDatabaseInfoMap
;
82 class KeychainSchema
: public RefPointer
<KeychainSchemaImpl
>
86 KeychainSchema(KeychainSchemaImpl
*impl
) : RefPointer
<KeychainSchemaImpl
>(impl
) {}
87 KeychainSchema(const CssmClient::Db
&db
) : RefPointer
<KeychainSchemaImpl
>(new KeychainSchemaImpl(db
)) {}
89 bool operator <(const KeychainSchema
&other
) const
90 { return ptr
&& other
.ptr
? *ptr
< *other
.ptr
: ptr
< other
.ptr
; }
91 bool operator ==(const KeychainSchema
&other
) const
92 { return ptr
&& other
.ptr
? *ptr
== *other
.ptr
: ptr
== other
.ptr
; }
95 typedef KeychainSchemaImpl Impl
;
99 class KeychainImpl
: public ReferencedObject
102 friend class Keychain
;
104 KeychainImpl(const CssmClient::Db
&db
);
107 // Methods called by ItemImpl;
108 friend class ItemImpl
;
110 void didUpdate(ItemImpl
*inItemImpl
, PrimaryKey
&oldPK
,
114 virtual ~KeychainImpl();
117 void add(Item
&item
); // item must not be persistant. Item will change.
118 void deleteItem(Item
&item
); // item must be persistant.
121 void create(UInt32 passwordLength
, const void *inPassword
);
122 void create(ConstStringPtr inPassword
);
124 void create(const ResourceControlContext
*rcc
);
125 void open(); // There is no close since the client lib deals with that itself. might throw
127 // Locking and unlocking a keychain.
130 void unlock(const CssmData
&password
);
131 void unlock(ConstStringPtr password
); // @@@ This has a length limit, we should remove it.
133 void getSettings(uint32
&outIdleTimeOut
, bool &outLockOnSleep
);
134 void setSettings(uint32 inIdleTimeOut
, bool inLockOnSleep
);
136 // Passing in NULL for either oldPassword or newPassword will cause them to be prompted for.
137 // To specify a zero length password in either case the oldPasswordLength or newPasswordLength
138 // value must be 0 and the oldPassword or newPassword must not be NULL.
139 void changePassphrase(UInt32 oldPasswordLength
, const void *oldPassword
,
140 UInt32 newPasswordLength
, const void *newPassword
);
141 void changePassphrase(ConstStringPtr oldPassword
, ConstStringPtr newPassword
);
143 void authenticate(const CSSM_ACCESS_CREDENTIALS
*cred
); // Does not do an unlock.
145 const char *name() const { return mDb
->name(); }
146 UInt32
status() const;
148 bool isActive() const;
150 KCCursor
createCursor(const SecKeychainAttributeList
*attrList
);
151 KCCursor
createCursor(SecItemClass itemClass
, const SecKeychainAttributeList
*attrList
);
152 CssmClient::Db
database() { return mDb
; }
153 DLDbIdentifier
dLDbIdentifier() const { return mDb
->dlDbIdentifier(); }
155 PrimaryKey
makePrimaryKey(CSSM_DB_RECORDTYPE recordType
, CssmClient::DbUniqueRecord
&uniqueId
);
156 void gatherPrimaryKeyAttributes(CssmClient::DbAttributes
& primaryKeyAttrs
);
158 const CssmAutoDbRecordAttributeInfo
&primaryKeyInfosFor(CSSM_DB_RECORDTYPE recordType
);
160 Item
item(const PrimaryKey
& primaryKey
);
161 Item
item(CSSM_DB_RECORDTYPE recordType
, CssmClient::DbUniqueRecord
&uniqueId
);
163 CssmDbAttributeInfo
attributeInfoForTag(UInt32 tag
);
164 void getAttributeInfoForItemID(CSSM_DB_RECORDTYPE itemID
, SecKeychainAttributeInfo
**Info
);
165 static void freeAttributeInfo(SecKeychainAttributeInfo
*Info
);
168 KeychainSchema
keychainSchema();
169 void addItem(const PrimaryKey
&primaryKey
, ItemImpl
*dbItemImpl
);
170 void removeItem(const PrimaryKey
&primaryKey
, const ItemImpl
*inItemImpl
);
173 Mutex mDbItemMapLock
;
174 typedef map
<PrimaryKey
, ItemImpl
*> DbItemMap
;
175 DbItemMap mDbItemMap
;
177 KeychainSchema mKeychainSchema
;
181 class Keychain
: public RefPointer
<KeychainImpl
>
185 Keychain(KeychainImpl
*impl
) : RefPointer
<KeychainImpl
>(impl
) {}
187 static Keychain
optional(SecKeychainRef handle
);
190 friend class StorageManager
;
191 Keychain(const CssmClient::Db
&db
)
192 : RefPointer
<KeychainImpl
>(new KeychainImpl(db
)) {}
194 typedef KeychainImpl Impl
;
198 typedef Ref
<Keychain
, KeychainImpl
, SecKeychainRef
, errSecInvalidKeychain
> KeychainRef
;
200 } // end namespace KeychainCore
202 } // end namespace Security
204 #endif /* _H_KEYCHAINS_ */