]> git.saurik.com Git - apple/security.git/blob - securityd/src/kcdatabase.h
Security-57337.50.23.tar.gz
[apple/security.git] / securityd / src / kcdatabase.h
1 /*
2 * Copyright (c) 2000-2008,2012-2013 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 //
26 // kcdatabase - software database container implementation.
27 //
28 // A KeychainDatabase is a software storage container,
29 // implemented in cooperation by the AppleCSLDP CDSA plugin and this daemon.
30 //
31 #ifndef _H_KCDATABASE
32 #define _H_KCDATABASE
33
34 #include "localdatabase.h"
35 #include <securityd_client/ss_types.h>
36 #include "agentclient.h"
37
38 class KeychainDatabase;
39 class KeychainDbCommon;
40 class KeychainKey;
41
42
43 //
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.
47 //
48 class DbIdentifier {
49 public:
50 DbIdentifier(const DLDbIdentifier &id, DbBlob::Signature sig)
51 : mIdent(id), mSig(sig) { }
52
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(); }
58
59 bool operator < (const DbIdentifier &id) const // simple lexicographic
60 {
61 if (mIdent < id.mIdent) return true;
62 if (id.mIdent < mIdent) return false;
63 return mSig < id.mSig;
64 }
65
66 bool operator == (const DbIdentifier &id) const
67 { return mIdent == id.mIdent && mSig == id.mSig; }
68
69 private:
70 DLDbIdentifier mIdent;
71 DbBlob::Signature mSig;
72 };
73
74
75 //
76 // A vestigal system-global database instance
77 // We don't (yet) use it for anything. Perhaps it should carry our ACL...
78 //
79 class KeychainDbGlobal : public PerGlobal {
80 public:
81 KeychainDbGlobal(const DbIdentifier &id);
82 ~KeychainDbGlobal();
83
84 const DbIdentifier &identifier() const { return mIdentifier; }
85
86 private:
87 DbIdentifier mIdentifier; // database external identifier [const]
88 };
89
90
91 //
92 // KeychainDatabase DbCommons
93 //
94 class KeychainDbCommon : public LocalDbCommon,
95 public DatabaseCryptoCore, public MachServer::Timer {
96 public:
97 KeychainDbCommon(Session &ssn, const DbIdentifier &id);
98 ~KeychainDbCommon();
99
100 KeychainDbGlobal &global() const;
101
102 bool unlockDb(DbBlob *blob, void **privateAclBlob = NULL);
103 void lockDb(); // make locked (if currently unlocked)
104 bool isLocked() { return mIsLocked; } // lock status
105 void setUnlocked();
106 void invalidateBlob() { version++; }
107
108 void activity(); // reset lock timeout
109
110 void makeNewSecrets();
111
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; }
117
118 DbBlob *encode(KeychainDatabase &db);
119
120 void notify(NotificationEvent event) { DbCommon::notify(event, identifier()); }
121
122 void sleepProcessing();
123 void lockProcessing();
124
125 bool belongsToSystem() const;
126 bool isDefaultSystemKeychain() const;
127
128 public:
129 // debugging
130 IFDUMP(void dumpNode());
131
132 protected:
133 void action(); // timer queue action to lock keychain
134
135 // lifetime management for our Timer personality
136 void select();
137 void unselect();
138
139 public:
140 // all following data locked with object lock
141 uint32 sequence; // change sequence number
142 DBParameters mParams; // database parameters (arbitrated copy)
143
144 uint32 version; // version stamp for change tracking
145
146 private:
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
151 bool mLoginKeychain;
152 };
153
154
155 //
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
158 // access.
159 //
160 class KeychainDatabase : public LocalDatabase, private virtual SecurityServerAcl {
161 friend class KeychainDbCommon;
162 public:
163 KeychainDatabase(const DLDbIdentifier &id, const DBParameters &params, Process &proc,
164 const AccessCredentials *cred, const AclEntryPrototype *owner);
165 KeychainDatabase(const DLDbIdentifier &id, const DbBlob *blob, Process &proc,
166 const AccessCredentials *cred);
167
168 // keychain synchronization recode to a specfic blob:
169 KeychainDatabase(KeychainDatabase &src, Process &proc, DbHandle dbToClone);
170
171 // Copy another database, but with new secrets
172 KeychainDatabase(KeychainDatabase &src, Process &proc);
173 virtual ~KeychainDatabase();
174
175 KeychainDbCommon &common() const;
176 const char *dbName() const;
177 bool transient() const;
178
179 KeychainDbGlobal &global() const { return common().global(); }
180
181 public:
182 static const int maxUnlockTryCount = 3;
183
184 public:
185 const DbIdentifier &identifier() const { return common().identifier(); }
186
187 public:
188 // encoding/decoding databases
189 DbBlob *blob();
190
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);
197
198 // lock/unlock processing
199 void lockDb(); // unconditional lock
200 void unlockDb(); // full-feature unlock
201 void unlockDb(const CssmData &passphrase); // unlock with passphrase
202
203 void stashDbCheck(); // check AppleKeyStore for master key
204 void stashDb(); // stash master key in AppleKeyStore
205
206 bool decode(); // unlock given established master key
207 bool decode(const CssmData &passphrase); // set master key from PP, try unlock
208
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
213
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; }
219
220 // manage database parameters
221 void setParameters(const DBParameters &params);
222 void getParameters(DBParameters &params);
223
224 // where's my (database) ACL?
225 SecurityServerAcl &acl();
226
227 AclKind aclKind() const;
228 Database *relatedDatabase();
229
230 // ACL state management hooks
231 void instantiateAcl();
232 void changedAcl();
233
234 // miscellaneous utilities
235 static void validateBlob(const DbBlob *blob);
236
237 // debugging
238 IFDUMP(void dumpNode());
239
240 protected:
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);
243
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)
247
248 void establishOldSecrets(const AccessCredentials *creds);
249 bool establishNewSecrets(const AccessCredentials *creds, SecurityAgent::Reason reason);
250
251 bool interactiveUnlock();
252
253 CssmClient::Key keyFromCreds(const TypedList &sample, unsigned int requiredLength);
254
255 void encode(); // (re)generate mBlob if needed
256
257 private:
258 // all following data is locked by the common lock
259 bool mValidData; // valid ACL and params (blob decoded)
260 CssmAutoData mSecret;
261 bool mSaveSecret;
262
263 uint32 version; // version stamp for blob validity
264 DbBlob *mBlob; // database blob (encoded)
265
266 AccessCredentials *mCred; // local access credentials (always valid)
267
268 RefPointer<KeychainDatabase> mRecodingSource; // keychain synchronization ONLY; should not require accessors
269 };
270
271 #endif //_H_KCDATABASE