]> git.saurik.com Git - apple/security.git/blob - SecurityServer/xdatabase.h
Security-29.tar.gz
[apple/security.git] / SecurityServer / xdatabase.h
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 //
20 // database - database session management
21 //
22 #ifndef _H_DATABASE
23 #define _H_DATABASE
24
25 #include "securityserver.h"
26 #include "acls.h"
27 #include "dbcrypto.h"
28 #include <Security/utilities.h>
29 #include <Security/handleobject.h>
30 #include <Security/cssmdb.h>
31 #include <Security/machserver.h>
32 #include <time.h>
33 #include <string>
34 #include <map>
35
36
37 class Key;
38 class Connection;
39 class Process;
40 using MachPlusPlus::MachServer;
41
42
43 //
44 // A Database object represents an Apple CSP/DL open database (DL/DB) object.
45 // It maintains its protected semantic state (including keys) and provides controlled
46 // access.
47 //
48 class Database : public HandleObject, public SecurityServerAcl {
49 class Common; friend class Common;
50 public:
51 Database(const DLDbIdentifier &id, const DBParameters &params, Process &proc,
52 const AccessCredentials *cred, const AclEntryPrototype *owner);
53 virtual ~Database();
54
55 Process &process;
56
57 static const int maxUnlockTryCount = 3;
58
59 public:
60 typedef DbBlob::Signature Signature;
61
62 class DbIdentifier {
63 public:
64 DbIdentifier(const DLDbIdentifier &id, Signature sig)
65 : mIdent(id), mSig(sig) { }
66
67 operator const DLDbIdentifier &() const { return mIdent; }
68 operator const Signature &() const { return mSig; }
69
70 bool operator < (const DbIdentifier &id) const // simple lexicographic
71 {
72 if (mIdent < id.mIdent) return true;
73 if (id.mIdent < mIdent) return false;
74 return mSig < id.mSig;
75 }
76
77 private:
78 DLDbIdentifier mIdent;
79 Signature mSig;
80 };
81
82 public:
83 //
84 // A Database::Common is the "common core" of all Database objects that
85 // represent the same client database (on disk, presumably).
86 // NOTE: Common obeys exterior locking protocol: the caller (always Database)
87 // must lock it before operating on its non-const members. In practice,
88 // most Database methods lock down their Common first thing.
89 //
90 class Common : public DatabaseCryptoCore, public MachServer::Timer, public Mutex {
91 public:
92 Common(const DbIdentifier &id);
93 ~Common();
94
95 bool unlock(DbBlob *blob, const CssmData &passphrase,
96 void **privateAclBlob = NULL);
97 bool unlock(const CssmData &passphrase);
98 void lock(bool holdingCommonLock = false, bool forSleep = false); // versatile lock primitive
99 bool isLocked() const { return mIsLocked; } // lock status
100 void activity(); // reset lock timeout
101
102 const DbIdentifier &identifier() const {return mIdentifier; }
103 const DLDbIdentifier &dlDbIdent() const { return identifier(); }
104 const char *dbName() const { return dlDbIdent().dbName(); }
105
106 DbBlob *encode(Database &db);
107 void setupKeys(const AccessCredentials *cred);
108
109 protected:
110 void action(); // timer queue action to lock keychain
111
112 public:
113 DbIdentifier mIdentifier; // database external identifier [const]
114 // all following data locked with object lock
115 uint32 sequence; // change sequence number
116 DBParameters mParams; // database parameters (arbitrated copy)
117
118 CssmAutoData passphrase; // passphrase if available, or NULL data
119
120 uint32 useCount; // database sessions we belong to
121 uint32 version; // version stamp for change tracking
122
123 private:
124 bool mIsLocked; // database is LOGICALLY locked
125 };
126
127 const DbIdentifier &identifier() const { return common->identifier(); }
128 const char *dbName() const { return common->dbName(); }
129
130 public:
131 // encoding/decoding databases
132 DbBlob *encode();
133 Database(const DLDbIdentifier &id, const DbBlob *blob, Process &proc,
134 const AccessCredentials *cred);
135 void authenticate(const AccessCredentials *cred);
136 void changePassphrase(const AccessCredentials *cred);
137
138 // lock/unlock processing
139 void lock(); // unconditional lock
140 void unlock(); // full-feature unlock
141 void unlock(const CssmData &passphrase); // unlock with passphrase
142 bool decode(const CssmData &passphrase); // try unlock/don't fail
143 bool isLocked() const { return common->isLocked(); } // lock status
144
145 void activity() const { common->activity(); } // reset timeout clock
146 static void lockAllDatabases(bool forSleep = false); // lock them all
147
148 // encoding/decoding keys
149 void decodeKey(KeyBlob *blob, CssmKey &key, void * &pubAcl, void * &privAcl);
150 KeyBlob *encodeKey(const CssmKey &key, const CssmData &pubAcl, const CssmData &privAcl);
151
152 bool validBlob() const { return mBlob && version == common->version; }
153
154 // manage database parameters
155 void setParameters(const DBParameters &params);
156 void getParameters(DBParameters &params);
157
158 // ACL state management hooks
159 void instantiateAcl();
160 void noticeAclChange();
161 const Database *relatedDatabase() const; // "self", for SecurityServerAcl's sake
162
163 // debugging
164 IFDUMP(void debugDump(const char *msg));
165
166 protected:
167 void makeUnlocked(); // interior version of unlock()
168 void makeUnlocked(const CssmData &passphrase); // interior version of unlock(CssmData)
169 static void discard(Common *common);
170
171 private:
172 Common *common; // shared features of all instances of this database [const]
173
174 // all following data is locked by the common lock
175 bool mValidData; // valid ACL and params (blob decoded)
176
177 uint32 version; // version stamp for blob validity
178 DbBlob *mBlob; // database blob (encoded)
179
180 AccessCredentials *mCred; // local access credentials (always valid)
181
182 private:
183 // @@@ Arguably, this should be a member of the Server or Session.
184 // @@@ If we do this, encapsulate it as a DatabaseMap object of sorts.
185 static Mutex commonLock; // lock for commons map (only)
186 typedef map<DbIdentifier, Common *> CommonMap;
187 static CommonMap commons; // map of extant database objects
188 };
189
190
191 #endif //_H_DATABASE