]>
git.saurik.com Git - apple/security.git/blob - securityd/src/kcdatabase.h
4013de84e1abf59fcfd6ef4072f918fa05e1a951
2 * Copyright (c) 2000-2008,2012-2013 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"
35 #include <securityd_client/ss_types.h>
36 #include "agentclient.h"
38 class KeychainDatabase
;
39 class KeychainDbCommon
;
44 // We identify KeychainDatabases uniquely by a combination of
45 // a DLDbIdentifier and a database (blob) identifier. Equivalence
46 // by DbIdentifier is the criterion for parent-side merging.
50 DbIdentifier(const DLDbIdentifier
&id
, DbBlob::Signature sig
)
51 : mIdent(id
), mSig(sig
) { }
53 const DLDbIdentifier
&dlDbIdentifier() const { return mIdent
; }
54 const DbBlob::Signature
&signature() const { return mSig
; }
55 operator const DLDbIdentifier
&() const { return dlDbIdentifier(); }
56 operator const DbBlob::Signature
&() const { return signature(); }
57 const char *dbName() const { return mIdent
.dbName(); }
59 bool operator < (const DbIdentifier
&id
) const // simple lexicographic
61 if (mIdent
< id
.mIdent
) return true;
62 if (id
.mIdent
< mIdent
) return false;
63 return mSig
< id
.mSig
;
66 bool operator == (const DbIdentifier
&id
) const
67 { return mIdent
== id
.mIdent
&& mSig
== id
.mSig
; }
70 DLDbIdentifier mIdent
;
71 DbBlob::Signature mSig
;
76 // A vestigal system-global database instance
77 // We don't (yet) use it for anything. Perhaps it should carry our ACL...
79 class KeychainDbGlobal
: public PerGlobal
{
81 KeychainDbGlobal(const DbIdentifier
&id
);
84 const DbIdentifier
&identifier() const { return mIdentifier
; }
87 DbIdentifier mIdentifier
; // database external identifier [const]
92 // KeychainDatabase DbCommons
94 class KeychainDbCommon
: public LocalDbCommon
,
95 public DatabaseCryptoCore
, public MachServer::Timer
{
97 KeychainDbCommon(Session
&ssn
, const DbIdentifier
&id
);
100 KeychainDbGlobal
&global() const;
102 bool unlockDb(DbBlob
*blob
, void **privateAclBlob
= NULL
);
103 void lockDb(); // make locked (if currently unlocked)
104 bool isLocked() { return mIsLocked
; } // lock status
106 void invalidateBlob() { version
++; }
108 void activity(); // reset lock timeout
110 void makeNewSecrets();
112 const DbIdentifier
&identifier() const {return mIdentifier
; }
113 const DLDbIdentifier
&dlDbIdent() const { return identifier(); }
114 const char *dbName() const { return dlDbIdent().dbName(); }
115 uint32
dbVersion() { return DatabaseCryptoCore::mBlobVersion
; }
116 bool isLoginKeychain() const { return mLoginKeychain
; }
118 DbBlob
*encode(KeychainDatabase
&db
);
120 void notify(NotificationEvent event
) { DbCommon::notify(event
, identifier()); }
122 void sleepProcessing();
123 void lockProcessing();
125 bool belongsToSystem() const;
126 bool isDefaultSystemKeychain() const;
130 IFDUMP(void dumpNode());
133 void action(); // timer queue action to lock keychain
135 // lifetime management for our Timer personality
140 // all following data locked with object lock
141 uint32 sequence
; // change sequence number
142 DBParameters mParams
; // database parameters (arbitrated copy)
144 uint32 version
; // version stamp for change tracking
147 DbIdentifier mIdentifier
; // database external identifier [const]
148 // all following data protected by object lock
149 bool mIsLocked
; // logically locked
150 bool mValidParams
; // mParams has been set
156 // A Database object represents an Apple CSP/DL open database (DL/DB) object.
157 // It maintains its protected semantic state (including keys) and provides controlled
160 class KeychainDatabase
: public LocalDatabase
, private virtual SecurityServerAcl
{
161 friend class KeychainDbCommon
;
163 KeychainDatabase(const DLDbIdentifier
&id
, const DBParameters
¶ms
, Process
&proc
,
164 const AccessCredentials
*cred
, const AclEntryPrototype
*owner
);
165 KeychainDatabase(const DLDbIdentifier
&id
, const DbBlob
*blob
, Process
&proc
,
166 const AccessCredentials
*cred
);
168 // keychain synchronization recode to a specfic blob:
169 KeychainDatabase(KeychainDatabase
&src
, Process
&proc
, DbHandle dbToClone
);
171 // Copy another database, but with new secrets
172 KeychainDatabase(KeychainDatabase
&src
, Process
&proc
);
173 virtual ~KeychainDatabase();
175 KeychainDbCommon
&common() const;
176 const char *dbName() const;
177 bool transient() const;
179 KeychainDbGlobal
&global() const { return common().global(); }
182 static const int maxUnlockTryCount
= 3;
185 const DbIdentifier
&identifier() const { return common().identifier(); }
188 // encoding/decoding databases
191 void authenticate(CSSM_DB_ACCESS_TYPE mode
, const AccessCredentials
*cred
);
192 bool checkCredentials(const AccessCredentials
* creds
);
193 void changePassphrase(const AccessCredentials
*cred
);
194 RefPointer
<Key
> extractMasterKey(Database
&db
, const AccessCredentials
*cred
,
195 const AclEntryPrototype
*owner
, uint32 usage
, uint32 attrs
);
196 void commitSecretsForSync(KeychainDatabase
&cloneDb
);
198 // lock/unlock processing
199 void lockDb(); // unconditional lock
200 void unlockDb(); // full-feature unlock
201 void unlockDb(const CssmData
&passphrase
); // unlock with passphrase
203 void stashDbCheck(); // check AppleKeyStore for master key
204 void stashDb(); // stash master key in AppleKeyStore
206 bool decode(); // unlock given established master key
207 bool decode(const CssmData
&passphrase
); // set master key from PP, try unlock
209 bool validatePassphrase(const CssmData
&passphrase
) const; // nonthrowing validation
210 bool isLocked() { return common().isLocked(); } // lock status
211 void notify(NotificationEvent event
) { return common().notify(event
); }
212 void activity() const { common().activity(); } // reset timeout clock
214 // encoding/decoding keys
215 void decodeKey(KeyBlob
*blob
, CssmKey
&key
, void * &pubAcl
, void * &privAcl
);
216 KeyBlob
*encodeKey(const CssmKey
&key
, const CssmData
&pubAcl
, const CssmData
&privAcl
);
217 KeyBlob
*recodeKey(KeychainKey
&oldKey
);
218 bool validBlob() const { return mBlob
&& version
== common().version
; }
220 // manage database parameters
221 void setParameters(const DBParameters
¶ms
);
222 void getParameters(DBParameters
¶ms
);
224 // where's my (database) ACL?
225 SecurityServerAcl
&acl();
227 AclKind
aclKind() const;
228 Database
*relatedDatabase();
230 // ACL state management hooks
231 void instantiateAcl();
234 // miscellaneous utilities
235 static void validateBlob(const DbBlob
*blob
);
238 IFDUMP(void dumpNode());
241 RefPointer
<Key
> makeKey(const CssmKey
&newKey
, uint32 moreAttributes
, const AclEntryPrototype
*owner
);
242 RefPointer
<Key
> makeKey(Database
&db
, const CssmKey
&newKey
, uint32 moreAttributes
, const AclEntryPrototype
*owner
);
244 void makeUnlocked(); // interior version of unlock()
245 void makeUnlocked(const AccessCredentials
*cred
); // like () with explicit cred
246 void makeUnlocked(const CssmData
&passphrase
); // interior version of unlock(CssmData)
248 void establishOldSecrets(const AccessCredentials
*creds
);
249 bool establishNewSecrets(const AccessCredentials
*creds
, SecurityAgent::Reason reason
);
251 bool interactiveUnlock();
253 CssmClient::Key
keyFromCreds(const TypedList
&sample
, unsigned int requiredLength
);
255 void encode(); // (re)generate mBlob if needed
258 // all following data is locked by the common lock
259 bool mValidData
; // valid ACL and params (blob decoded)
260 CssmAutoData mSecret
;
263 uint32 version
; // version stamp for blob validity
264 DbBlob
*mBlob
; // database blob (encoded)
266 AccessCredentials
*mCred
; // local access credentials (always valid)
268 RefPointer
<KeychainDatabase
> mRecodingSource
; // keychain synchronization ONLY; should not require accessors
271 #endif //_H_KCDATABASE