]> git.saurik.com Git - apple/securityd.git/blob - src/kcdatabase.h
securityd-55137.1.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 #include <securityd_client/ss_types.h>
36
37 class KeychainDatabase;
38 class KeychainDbCommon;
39 class KeychainKey;
40
41
42 //
43 // We identify KeychainDatabases uniquely by a combination of
44 // a DLDbIdentifier and a database (blob) identifier. Equivalence
45 // by DbIdentifier is the criterion for parent-side merging.
46 //
47 class DbIdentifier {
48 public:
49 DbIdentifier(const DLDbIdentifier &id, DbBlob::Signature sig)
50 : mIdent(id), mSig(sig) { }
51
52 const DLDbIdentifier &dlDbIdentifier() const { return mIdent; }
53 const DbBlob::Signature &signature() const { return mSig; }
54 operator const DLDbIdentifier &() const { return dlDbIdentifier(); }
55 operator const DbBlob::Signature &() const { return signature(); }
56 const char *dbName() const { return mIdent.dbName(); }
57
58 bool operator < (const DbIdentifier &id) const // simple lexicographic
59 {
60 if (mIdent < id.mIdent) return true;
61 if (id.mIdent < mIdent) return false;
62 return mSig < id.mSig;
63 }
64
65 bool operator == (const DbIdentifier &id) const
66 { return mIdent == id.mIdent && mSig == id.mSig; }
67
68 private:
69 DLDbIdentifier mIdent;
70 DbBlob::Signature mSig;
71 };
72
73
74 //
75 // A vestigal system-global database instance
76 // We don't (yet) use it for anything. Perhaps it should carry our ACL...
77 //
78 class KeychainDbGlobal : public PerGlobal {
79 public:
80 KeychainDbGlobal(const DbIdentifier &id);
81 ~KeychainDbGlobal();
82
83 const DbIdentifier &identifier() const { return mIdentifier; }
84
85 private:
86 DbIdentifier mIdentifier; // database external identifier [const]
87 };
88
89
90 //
91 // KeychainDatabase DbCommons
92 //
93 class KeychainDbCommon : public LocalDbCommon,
94 public DatabaseCryptoCore, public MachServer::Timer {
95 public:
96 KeychainDbCommon(Session &ssn, const DbIdentifier &id);
97 ~KeychainDbCommon();
98
99 KeychainDbGlobal &global() const;
100
101 bool unlockDb(DbBlob *blob, void **privateAclBlob = NULL);
102 void lockDb(); // make locked (if currently unlocked)
103 bool isLocked() { return mIsLocked; } // lock status
104 void setUnlocked();
105 void invalidateBlob() { version++; }
106
107 void activity(); // reset lock timeout
108
109 void makeNewSecrets();
110
111 const DbIdentifier &identifier() const {return mIdentifier; }
112 const DLDbIdentifier &dlDbIdent() const { return identifier(); }
113 const char *dbName() const { return dlDbIdent().dbName(); }
114
115 DbBlob *encode(KeychainDatabase &db);
116
117 void notify(NotificationEvent event) { DbCommon::notify(event, identifier()); }
118
119 void sleepProcessing();
120 void lockProcessing();
121
122 bool belongsToSystem() const;
123
124 public:
125 // debugging
126 IFDUMP(void dumpNode());
127
128 protected:
129 void action(); // timer queue action to lock keychain
130
131 // lifetime management for our Timer personality
132 void select();
133 void unselect();
134
135 public:
136 // all following data locked with object lock
137 uint32 sequence; // change sequence number
138 DBParameters mParams; // database parameters (arbitrated copy)
139
140 uint32 version; // version stamp for change tracking
141
142 private:
143 DbIdentifier mIdentifier; // database external identifier [const]
144 // all following data protected by object lock
145 bool mIsLocked; // logically locked
146 bool mValidParams; // mParams has been set
147 };
148
149
150 //
151 // A Database object represents an Apple CSP/DL open database (DL/DB) object.
152 // It maintains its protected semantic state (including keys) and provides controlled
153 // access.
154 //
155 class KeychainDatabase : public LocalDatabase, private virtual SecurityServerAcl {
156 friend class KeychainDbCommon;
157 public:
158 KeychainDatabase(const DLDbIdentifier &id, const DBParameters &params, Process &proc,
159 const AccessCredentials *cred, const AclEntryPrototype *owner);
160 KeychainDatabase(const DLDbIdentifier &id, const DbBlob *blob, Process &proc,
161 const AccessCredentials *cred);
162
163 // keychain synchronization recode to a specfic blob:
164 KeychainDatabase(KeychainDatabase &src, Process &proc, DbHandle dbToClone);
165 virtual ~KeychainDatabase();
166
167 KeychainDbCommon &common() const;
168 const char *dbName() const;
169 bool transient() const;
170
171 KeychainDbGlobal &global() const { return common().global(); }
172
173 public:
174 static const int maxUnlockTryCount = 3;
175
176 public:
177 const DbIdentifier &identifier() const { return common().identifier(); }
178
179 public:
180 // encoding/decoding databases
181 DbBlob *blob();
182
183 void authenticate(CSSM_DB_ACCESS_TYPE mode, const AccessCredentials *cred);
184 void changePassphrase(const AccessCredentials *cred);
185 RefPointer<Key> extractMasterKey(Database &db, const AccessCredentials *cred,
186 const AclEntryPrototype *owner, uint32 usage, uint32 attrs);
187 void commitSecretsForSync(KeychainDatabase &cloneDb);
188
189 // lock/unlock processing
190 void lockDb(); // unconditional lock
191 void unlockDb(); // full-feature unlock
192 void unlockDb(const CssmData &passphrase); // unlock with passphrase
193
194 bool decode(); // unlock given established master key
195 bool decode(const CssmData &passphrase); // set master key from PP, try unlock
196
197 bool validatePassphrase(const CssmData &passphrase) const; // nonthrowing validation
198 bool isLocked() { return common().isLocked(); } // lock status
199 void notify(NotificationEvent event) { return common().notify(event); }
200 void activity() const { common().activity(); } // reset timeout clock
201
202 // encoding/decoding keys
203 void decodeKey(KeyBlob *blob, CssmKey &key, void * &pubAcl, void * &privAcl);
204 KeyBlob *encodeKey(const CssmKey &key, const CssmData &pubAcl, const CssmData &privAcl);
205 KeyBlob *recodeKey(KeychainKey &oldKey);
206 bool validBlob() const { return mBlob && version == common().version; }
207
208 // manage database parameters
209 void setParameters(const DBParameters &params);
210 void getParameters(DBParameters &params);
211
212 // where's my (database) ACL?
213 SecurityServerAcl &acl();
214
215 AclKind aclKind() const;
216 Database *relatedDatabase();
217
218 // ACL state management hooks
219 void instantiateAcl();
220 void changedAcl();
221
222 // miscellaneous utilities
223 static void validateBlob(const DbBlob *blob);
224
225 // debugging
226 IFDUMP(void dumpNode());
227
228 protected:
229 RefPointer<Key> makeKey(const CssmKey &newKey, uint32 moreAttributes, const AclEntryPrototype *owner);
230 RefPointer<Key> makeKey(Database &db, const CssmKey &newKey, uint32 moreAttributes, const AclEntryPrototype *owner);
231
232 void makeUnlocked(); // interior version of unlock()
233 void makeUnlocked(const AccessCredentials *cred); // like () with explicit cred
234 void makeUnlocked(const CssmData &passphrase); // interior version of unlock(CssmData)
235
236 void establishOldSecrets(const AccessCredentials *creds);
237 void establishNewSecrets(const AccessCredentials *creds, SecurityAgent::Reason reason);
238
239 bool interactiveUnlock();
240
241 CssmClient::Key keyFromCreds(const TypedList &sample, unsigned int requiredLength);
242
243 void encode(); // (re)generate mBlob if needed
244
245 private:
246 // all following data is locked by the common lock
247 bool mValidData; // valid ACL and params (blob decoded)
248
249 uint32 version; // version stamp for blob validity
250 DbBlob *mBlob; // database blob (encoded)
251
252 AccessCredentials *mCred; // local access credentials (always valid)
253
254 RefPointer<KeychainDatabase> mRecodingSource; // keychain synchronization ONLY; should not require accessors
255 };
256
257 #endif //_H_KCDATABASE