]> git.saurik.com Git - apple/securityd.git/blob - src/kcdatabase.h
securityd-36489.tar.gz
[apple/securityd.git] / src / kcdatabase.h
1 /*
2 * Copyright (c) 2000-2007 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
36 class KeychainDatabase;
37 class KeychainDbCommon;
38 class KeychainKey;
39
40
41 //
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.
45 //
46 class DbIdentifier {
47 public:
48 DbIdentifier(const DLDbIdentifier &id, DbBlob::Signature sig)
49 : mIdent(id), mSig(sig) { }
50
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(); }
56
57 bool operator < (const DbIdentifier &id) const // simple lexicographic
58 {
59 if (mIdent < id.mIdent) return true;
60 if (id.mIdent < mIdent) return false;
61 return mSig < id.mSig;
62 }
63
64 bool operator == (const DbIdentifier &id) const
65 { return mIdent == id.mIdent && mSig == id.mSig; }
66
67 private:
68 DLDbIdentifier mIdent;
69 DbBlob::Signature mSig;
70 };
71
72
73 //
74 // A vestigal system-global database instance
75 // We don't (yet) use it for anything. Perhaps it should carry our ACL...
76 //
77 class KeychainDbGlobal : public PerGlobal {
78 public:
79 KeychainDbGlobal(const DbIdentifier &id);
80 ~KeychainDbGlobal();
81
82 const DbIdentifier &identifier() const { return mIdentifier; }
83
84 private:
85 DbIdentifier mIdentifier; // database external identifier [const]
86 };
87
88
89 //
90 // KeychainDatabase DbCommons
91 //
92 class KeychainDbCommon : public LocalDbCommon,
93 public DatabaseCryptoCore, public MachServer::Timer {
94 public:
95 KeychainDbCommon(Session &ssn, const DbIdentifier &id);
96 ~KeychainDbCommon();
97
98 KeychainDbGlobal &global() const;
99
100 bool unlockDb(DbBlob *blob, void **privateAclBlob = NULL);
101 void lockDb(); // make locked (if currently unlocked)
102 bool isLocked() { return mIsLocked; } // lock status
103 void setUnlocked();
104 void invalidateBlob() { version++; }
105
106 void activity(); // reset lock timeout
107
108 void makeNewSecrets();
109
110 const DbIdentifier &identifier() const {return mIdentifier; }
111 const DLDbIdentifier &dlDbIdent() const { return identifier(); }
112 const char *dbName() const { return dlDbIdent().dbName(); }
113
114 DbBlob *encode(KeychainDatabase &db);
115
116 void notify(NotificationEvent event) { DbCommon::notify(event, identifier()); }
117
118 void sleepProcessing();
119 void lockProcessing();
120
121 public:
122 // debugging
123 IFDUMP(void dumpNode());
124
125 protected:
126 void action(); // timer queue action to lock keychain
127
128 // lifetime management for our Timer personality
129 void select();
130 void unselect();
131
132 public:
133 // all following data locked with object lock
134 uint32 sequence; // change sequence number
135 DBParameters mParams; // database parameters (arbitrated copy)
136
137 uint32 version; // version stamp for change tracking
138
139 private:
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
144 };
145
146
147 //
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
150 // access.
151 //
152 class KeychainDatabase : public LocalDatabase, private virtual SecurityServerAcl {
153 friend class KeychainDbCommon;
154 public:
155 KeychainDatabase(const DLDbIdentifier &id, const DBParameters &params, 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();
162
163 KeychainDbCommon &common() const;
164 const char *dbName() const;
165 bool transient() const;
166
167 KeychainDbGlobal &global() const { return common().global(); }
168
169 public:
170 static const int maxUnlockTryCount = 3;
171
172 public:
173 const DbIdentifier &identifier() const { return common().identifier(); }
174
175 public:
176 // encoding/decoding databases
177 DbBlob *blob();
178
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);
184
185 // lock/unlock processing
186 void lockDb(); // unconditional lock
187 void unlockDb(); // full-feature unlock
188 void unlockDb(const CssmData &passphrase); // unlock with passphrase
189
190 bool decode(); // unlock given established master key
191 bool decode(const CssmData &passphrase); // set master key from PP, try unlock
192
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
197
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; }
203
204 // manage database parameters
205 void setParameters(const DBParameters &params);
206 void getParameters(DBParameters &params);
207
208 // where's my (database) ACL?
209 SecurityServerAcl &acl();
210
211 AclKind aclKind() const;
212 Database *relatedDatabase();
213
214 // ACL state management hooks
215 void instantiateAcl();
216 void changedAcl();
217
218 // miscellaneous utilities
219 static void validateBlob(const DbBlob *blob);
220
221 // debugging
222 IFDUMP(void dumpNode());
223
224 protected:
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);
227
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)
231
232 void establishOldSecrets(const AccessCredentials *creds);
233 void establishNewSecrets(const AccessCredentials *creds, SecurityAgent::Reason reason);
234
235 bool interactiveUnlock();
236
237 CssmClient::Key keyFromCreds(const TypedList &sample, unsigned int requiredLength);
238
239 void encode(); // (re)generate mBlob if needed
240
241 private:
242 // all following data is locked by the common lock
243 bool mValidData; // valid ACL and params (blob decoded)
244
245 uint32 version; // version stamp for blob validity
246 DbBlob *mBlob; // database blob (encoded)
247
248 AccessCredentials *mCred; // local access credentials (always valid)
249
250 RefPointer<KeychainDatabase> mRecodingSource; // keychain synchronization ONLY; should not require accessors
251 };
252
253 #endif //_H_KCDATABASE