]> git.saurik.com Git - apple/securityd.git/blob - src/database.h
securityd-36489.tar.gz
[apple/securityd.git] / src / database.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 // database - abstract database management
27 //
28 // This file defines database objects that represent different
29 // way to implement "database with cryptographic operations on its contents".
30 // The objects here are abstract and need to be implemented to be useful.
31 //
32 #ifndef _H_DATABASE
33 #define _H_DATABASE
34
35 #include "structure.h"
36 #include "acls.h"
37 #include "dbcrypto.h"
38 #include "notifications.h"
39 #include <security_utilities/utilities.h>
40 #include <security_cdsa_utilities/handleobject.h>
41 #include <security_cdsa_utilities/cssmdb.h>
42 #include <security_utilities/machserver.h>
43 #include <security_agent_client/agentclient.h>
44 #include <security_utilities/timeflow.h>
45 #include <string>
46 #include <map>
47
48
49 class Key;
50 class Connection;
51 class Process;
52 class Session;
53 using MachPlusPlus::MachServer;
54
55
56 //
57 // A Database::DbCommon is the "common core" of all Database objects that
58 // represent the same client database (on disk, presumably).
59 // NOTE: DbCommon obeys exterior locking protocol: the caller (always Database)
60 // must lock it before operating on its non-const members. In practice,
61 // most Database methods lock down their DbCommon first thing.
62 //
63 class DbCommon : public PerSession {
64 public:
65 DbCommon(Session &ssn);
66
67 Session &session() const;
68
69 virtual void sleepProcessing(); // generic action on system sleep
70 virtual void lockProcessing(); // generic action on "lock" requests
71
72 protected:
73 void notify(NotificationEvent event, const DLDbIdentifier &ident);
74 };
75
76
77 //
78 // A Database object represents an Apple CSP/DL open database (DL/DB) object.
79 // It maintains its protected semantic state (including keys) and provides controlled
80 // access.
81 //
82 class Database : public PerProcess, public AclSource {
83 static const NotificationEvent lockedEvent = kNotificationEventLocked;
84 static const NotificationEvent unlockedEvent = kNotificationEventUnlocked;
85 static const NotificationEvent passphraseChangedEvent = kNotificationEventPassphraseChanged;
86
87 protected:
88 Database(Process &proc);
89
90 public:
91 Process& process() const;
92
93 virtual bool transient() const = 0; // is transient store (reboot clears)
94
95 public:
96 //
97 // A common class for objects that "belong" to a Database and
98 // don't have parents up the global stack. These will die along with
99 // the Database when it goes.
100 //
101 class Subsidiary : public PerProcess {
102 public:
103 Subsidiary(Database &db) { referent(db); }
104 Database &database() const { return referent<Database>(); }
105 Process &process() const { return database().process(); }
106 };
107
108 //
109 // Cryptographic service calls.
110 // These must be supported by any type of database.
111 //
112 virtual void releaseKey(Key &key);
113 virtual void queryKeySizeInBits(Key &key, CssmKeySize &result) = 0;
114 virtual void getOutputSize(const Context &context, Key &key,
115 uint32 inputSize, bool encrypt, uint32 &result) = 0;
116
117 virtual void generateSignature(const Context &context, Key &key,
118 CSSM_ALGORITHMS signOnlyAlgorithm, const CssmData &data, CssmData &signature) = 0;
119 virtual void verifySignature(const Context &context, Key &key,
120 CSSM_ALGORITHMS verifyOnlyAlgorithm, const CssmData &data, const CssmData &signature) = 0;
121 virtual void generateMac(const Context &context, Key &key,
122 const CssmData &data, CssmData &mac) = 0;
123 virtual void verifyMac(const Context &context, Key &key,
124 const CssmData &data, const CssmData &mac) = 0;
125
126 virtual void encrypt(const Context &context, Key &key, const CssmData &clear, CssmData &cipher) = 0;
127 virtual void decrypt(const Context &context, Key &key, const CssmData &cipher, CssmData &clear) = 0;
128
129 virtual void generateKey(const Context &context,
130 const AccessCredentials *cred, const AclEntryPrototype *owner,
131 uint32 usage, uint32 attrs, RefPointer<Key> &newKey) = 0;
132 virtual void generateKey(const Context &context,
133 const AccessCredentials *cred, const AclEntryPrototype *owner,
134 uint32 pubUsage, uint32 pubAttrs, uint32 privUsage, uint32 privAttrs,
135 RefPointer<Key> &publicKey, RefPointer<Key> &privateKey) = 0;
136
137 virtual void wrapKey(const Context &context, const AccessCredentials *cred,
138 Key *wrappingKey, Key &keyToBeWrapped,
139 const CssmData &descriptiveData, CssmKey &wrappedKey) = 0;
140 virtual void unwrapKey(const Context &context,
141 const AccessCredentials *cred, const AclEntryPrototype *owner,
142 Key *wrappingKey, Key *publicKey, CSSM_KEYUSE usage, CSSM_KEYATTR_FLAGS attrs,
143 const CssmKey wrappedKey, RefPointer<Key> &unwrappedKey, CssmData &descriptiveData) = 0;
144 virtual void deriveKey(const Context &context, Key *key,
145 const AccessCredentials *cred, const AclEntryPrototype *owner,
146 CssmData *param, uint32 usage, uint32 attrs, RefPointer<Key> &derivedKey) = 0;
147
148 virtual void authenticate(CSSM_DB_ACCESS_TYPE mode, const AccessCredentials *cred);
149 virtual SecurityServerAcl &acl();
150
151 virtual bool isLocked();
152
153 public:
154 class Search : public Subsidiary {
155 public:
156 Search(Database &db) : Subsidiary(db) { }
157 };
158
159 class Record : public Subsidiary {
160 public:
161 Record(Database &db) : Subsidiary(db) { }
162 };
163
164 virtual void findFirst(const CssmQuery &query,
165 CssmDbRecordAttributeData *inAttributes, mach_msg_type_number_t inAttributesLength,
166 CssmData *data, RefPointer<Key> &key, RefPointer<Search> &search,
167 RefPointer<Record> &record,
168 CssmDbRecordAttributeData * &outAttributes, mach_msg_type_number_t &outAttributesLength);
169 virtual void findNext(Search *search,
170 CssmDbRecordAttributeData *inAttributes, mach_msg_type_number_t inAttributesLength,
171 CssmData *data, RefPointer<Key> &key, RefPointer<Record> &record,
172 CssmDbRecordAttributeData * &outAttributes, mach_msg_type_number_t &outAttributesLength);
173 virtual void findRecordHandle(Record *record,
174 CssmDbRecordAttributeData *inAttributes, mach_msg_type_number_t inAttributesLength,
175 CssmData *data, RefPointer<Key> &key,
176 CssmDbRecordAttributeData * &outAttributes, mach_msg_type_number_t &outAttributesLength);
177
178 virtual void insertRecord(CSSM_DB_RECORDTYPE recordtype,
179 const CssmDbRecordAttributeData *attributes, mach_msg_type_number_t inAttributesLength,
180 const CssmData &data, RecordHandle &record);
181 virtual void modifyRecord(CSSM_DB_RECORDTYPE recordtype, Record *record,
182 const CssmDbRecordAttributeData *attributes, mach_msg_type_number_t inAttributesLength,
183 const CssmData *data, CSSM_DB_MODIFY_MODE modifyMode);
184 virtual void deleteRecord(Database::Record *record);
185
186 virtual void releaseSearch(Search &search);
187 virtual void releaseRecord(Record &record);
188
189 public:
190 // SecurityServerAcl personality
191 AclKind aclKind() const;
192 GenericHandle aclHandle() const;
193 Database *relatedDatabase();
194
195 public:
196 // support ACL remote secret validation (default is no support)
197 virtual bool validateSecret(const AclSubject *subject, const AccessCredentials *cred);
198
199 public:
200 static const int maxUnlockTryCount = 3;
201
202 public:
203 DbCommon& common() const { return parent<DbCommon>(); }
204 virtual const char *dbName() const = 0;
205 virtual void dbName(const char *name);
206 };
207
208
209 //
210 // This class implements a "system keychain unlock record" store
211 //
212 class SystemKeychainKey {
213 public:
214 SystemKeychainKey(const char *path);
215 ~SystemKeychainKey();
216
217 bool matches(const DbBlob::Signature &signature);
218 CssmKey &key() { return mKey; }
219
220 private:
221 std::string mPath; // path to file
222 CssmKey mKey; // proper CssmKey with data in mBlob
223
224 bool mValid; // mBlob was validly read from mPath
225 UnlockBlob mBlob; // contents of mPath as last read
226
227 Time::Absolute mCachedDate; // modify date of file when last read
228 Time::Absolute mUpdateThreshold; // cutoff threshold for checking again
229
230 static const int checkDelay = 1; // seconds minimum delay between update checks
231
232 bool update();
233 };
234
235 #endif //_H_DATABASE