]>
git.saurik.com Git - apple/securityd.git/blob - src/kcdatabase.h
2 * Copyright (c) 2000-2007 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@
26 // kcdatabase - software database container implementation.
28 // A KeychainDatabase is a software storage container,
29 // implemented in cooperation by the AppleCSLDP CDSA plugin and this daemon.
34 #include "localdatabase.h"
36 class KeychainDatabase
;
37 class KeychainDbCommon
;
42 // We identify KeychainDatabases uniquely by a combination of
43 // a DLDbIdentifier and a database (blob) identifier. Equivalence
44 // by DbIdentifier is the criterion for parent-side merging.
48 DbIdentifier(const DLDbIdentifier
&id
, DbBlob::Signature sig
)
49 : mIdent(id
), mSig(sig
) { }
51 const DLDbIdentifier
&dlDbIdentifier() const { return mIdent
; }
52 const DbBlob::Signature
&signature() const { return mSig
; }
53 operator const DLDbIdentifier
&() const { return dlDbIdentifier(); }
54 operator const DbBlob::Signature
&() const { return signature(); }
55 const char *dbName() const { return mIdent
.dbName(); }
57 bool operator < (const DbIdentifier
&id
) const // simple lexicographic
59 if (mIdent
< id
.mIdent
) return true;
60 if (id
.mIdent
< mIdent
) return false;
61 return mSig
< id
.mSig
;
64 bool operator == (const DbIdentifier
&id
) const
65 { return mIdent
== id
.mIdent
&& mSig
== id
.mSig
; }
68 DLDbIdentifier mIdent
;
69 DbBlob::Signature mSig
;
74 // A vestigal system-global database instance
75 // We don't (yet) use it for anything. Perhaps it should carry our ACL...
77 class KeychainDbGlobal
: public PerGlobal
{
79 KeychainDbGlobal(const DbIdentifier
&id
);
82 const DbIdentifier
&identifier() const { return mIdentifier
; }
85 DbIdentifier mIdentifier
; // database external identifier [const]
90 // KeychainDatabase DbCommons
92 class KeychainDbCommon
: public LocalDbCommon
,
93 public DatabaseCryptoCore
, public MachServer::Timer
{
95 KeychainDbCommon(Session
&ssn
, const DbIdentifier
&id
);
98 KeychainDbGlobal
&global() const;
100 bool unlockDb(DbBlob
*blob
, void **privateAclBlob
= NULL
);
101 void lockDb(); // make locked (if currently unlocked)
102 bool isLocked() { return mIsLocked
; } // lock status
104 void invalidateBlob() { version
++; }
106 void activity(); // reset lock timeout
108 void makeNewSecrets();
110 const DbIdentifier
&identifier() const {return mIdentifier
; }
111 const DLDbIdentifier
&dlDbIdent() const { return identifier(); }
112 const char *dbName() const { return dlDbIdent().dbName(); }
114 DbBlob
*encode(KeychainDatabase
&db
);
116 void notify(NotificationEvent event
) { DbCommon::notify(event
, identifier()); }
118 void sleepProcessing();
119 void lockProcessing();
123 IFDUMP(void dumpNode());
126 void action(); // timer queue action to lock keychain
128 // lifetime management for our Timer personality
133 // all following data locked with object lock
134 uint32 sequence
; // change sequence number
135 DBParameters mParams
; // database parameters (arbitrated copy)
137 uint32 version
; // version stamp for change tracking
140 DbIdentifier mIdentifier
; // database external identifier [const]
141 // all following data protected by object lock
142 bool mIsLocked
; // logically locked
143 bool mValidParams
; // mParams has been set
148 // A Database object represents an Apple CSP/DL open database (DL/DB) object.
149 // It maintains its protected semantic state (including keys) and provides controlled
152 class KeychainDatabase
: public LocalDatabase
, private virtual SecurityServerAcl
{
153 friend class KeychainDbCommon
;
155 KeychainDatabase(const DLDbIdentifier
&id
, const DBParameters
¶ms
, Process
&proc
,
156 const AccessCredentials
*cred
, const AclEntryPrototype
*owner
);
157 KeychainDatabase(const DLDbIdentifier
&id
, const DbBlob
*blob
, Process
&proc
,
158 const AccessCredentials
*cred
);
159 // keychain synchronization
160 KeychainDatabase(KeychainDatabase
&src
, Process
&proc
, const DbBlob
*secretsBlob
, const CssmData
&agentData
);
161 virtual ~KeychainDatabase();
163 KeychainDbCommon
&common() const;
164 const char *dbName() const;
165 bool transient() const;
167 KeychainDbGlobal
&global() const { return common().global(); }
170 static const int maxUnlockTryCount
= 3;
173 const DbIdentifier
&identifier() const { return common().identifier(); }
176 // encoding/decoding databases
179 void authenticate(CSSM_DB_ACCESS_TYPE mode
, const AccessCredentials
*cred
);
180 void changePassphrase(const AccessCredentials
*cred
);
181 RefPointer
<Key
> extractMasterKey(Database
&db
, const AccessCredentials
*cred
,
182 const AclEntryPrototype
*owner
, uint32 usage
, uint32 attrs
);
183 void commitSecretsForSync(KeychainDatabase
&cloneDb
);
185 // lock/unlock processing
186 void lockDb(); // unconditional lock
187 void unlockDb(); // full-feature unlock
188 void unlockDb(const CssmData
&passphrase
); // unlock with passphrase
190 bool decode(); // unlock given established master key
191 bool decode(const CssmData
&passphrase
); // set master key from PP, try unlock
193 bool validatePassphrase(const CssmData
&passphrase
) const; // nonthrowing validation
194 bool isLocked() { return common().isLocked(); } // lock status
195 void notify(NotificationEvent event
) { return common().notify(event
); }
196 void activity() const { common().activity(); } // reset timeout clock
198 // encoding/decoding keys
199 void decodeKey(KeyBlob
*blob
, CssmKey
&key
, void * &pubAcl
, void * &privAcl
);
200 KeyBlob
*encodeKey(const CssmKey
&key
, const CssmData
&pubAcl
, const CssmData
&privAcl
);
201 KeyBlob
*recodeKey(KeychainKey
&oldKey
);
202 bool validBlob() const { return mBlob
&& version
== common().version
; }
204 // manage database parameters
205 void setParameters(const DBParameters
¶ms
);
206 void getParameters(DBParameters
¶ms
);
208 // where's my (database) ACL?
209 SecurityServerAcl
&acl();
211 AclKind
aclKind() const;
212 Database
*relatedDatabase();
214 // ACL state management hooks
215 void instantiateAcl();
218 // miscellaneous utilities
219 static void validateBlob(const DbBlob
*blob
);
222 IFDUMP(void dumpNode());
225 RefPointer
<Key
> makeKey(const CssmKey
&newKey
, uint32 moreAttributes
, const AclEntryPrototype
*owner
);
226 RefPointer
<Key
> makeKey(Database
&db
, const CssmKey
&newKey
, uint32 moreAttributes
, const AclEntryPrototype
*owner
);
228 void makeUnlocked(); // interior version of unlock()
229 void makeUnlocked(const AccessCredentials
*cred
); // like () with explicit cred
230 void makeUnlocked(const CssmData
&passphrase
); // interior version of unlock(CssmData)
232 void establishOldSecrets(const AccessCredentials
*creds
);
233 void establishNewSecrets(const AccessCredentials
*creds
, SecurityAgent::Reason reason
);
235 bool interactiveUnlock();
237 CssmClient::Key
keyFromCreds(const TypedList
&sample
, unsigned int requiredLength
);
239 void encode(); // (re)generate mBlob if needed
242 // all following data is locked by the common lock
243 bool mValidData
; // valid ACL and params (blob decoded)
245 uint32 version
; // version stamp for blob validity
246 DbBlob
*mBlob
; // database blob (encoded)
248 AccessCredentials
*mCred
; // local access credentials (always valid)
250 RefPointer
<KeychainDatabase
> mRecodingSource
; // keychain synchronization ONLY; should not require accessors
253 #endif //_H_KCDATABASE