]>
git.saurik.com Git - apple/security.git/blob - SecurityServer/xdatabase.h
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
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
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.
20 // database - database session management
25 #include "securityserver.h"
28 #include <Security/utilities.h>
29 #include <Security/handleobject.h>
30 #include <Security/cssmdb.h>
31 #include <Security/machserver.h>
40 using MachPlusPlus::MachServer
;
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
48 class Database
: public HandleObject
, public SecurityServerAcl
{
49 class Common
; friend class Common
;
51 Database(const DLDbIdentifier
&id
, const DBParameters
¶ms
, Process
&proc
,
52 const AccessCredentials
*cred
, const AclEntryPrototype
*owner
);
57 static const int maxUnlockTryCount
= 3;
60 typedef DbBlob::Signature Signature
;
64 DbIdentifier(const DLDbIdentifier
&id
, Signature sig
)
65 : mIdent(id
), mSig(sig
) { }
67 operator const DLDbIdentifier
&() const { return mIdent
; }
68 operator const Signature
&() const { return mSig
; }
70 bool operator < (const DbIdentifier
&id
) const // simple lexicographic
72 if (mIdent
< id
.mIdent
) return true;
73 if (id
.mIdent
< mIdent
) return false;
74 return mSig
< id
.mSig
;
78 DLDbIdentifier mIdent
;
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.
90 class Common
: public DatabaseCryptoCore
, public MachServer::Timer
, public Mutex
{
92 Common(const DbIdentifier
&id
);
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
102 const DbIdentifier
&identifier() const {return mIdentifier
; }
103 const DLDbIdentifier
&dlDbIdent() const { return identifier(); }
104 const char *dbName() const { return dlDbIdent().dbName(); }
106 DbBlob
*encode(Database
&db
);
107 void setupKeys(const AccessCredentials
*cred
);
110 void action(); // timer queue action to lock keychain
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)
118 CssmAutoData passphrase
; // passphrase if available, or NULL data
120 uint32 useCount
; // database sessions we belong to
121 uint32 version
; // version stamp for change tracking
124 bool mIsLocked
; // database is LOGICALLY locked
127 const DbIdentifier
&identifier() const { return common
->identifier(); }
128 const char *dbName() const { return common
->dbName(); }
131 // encoding/decoding databases
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
);
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
145 void activity() const { common
->activity(); } // reset timeout clock
146 static void lockAllDatabases(bool forSleep
= false); // lock them all
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
);
152 bool validBlob() const { return mBlob
&& version
== common
->version
; }
154 // manage database parameters
155 void setParameters(const DBParameters
¶ms
);
156 void getParameters(DBParameters
¶ms
);
158 // ACL state management hooks
159 void instantiateAcl();
160 void noticeAclChange();
161 const Database
*relatedDatabase() const; // "self", for SecurityServerAcl's sake
164 IFDUMP(void debugDump(const char *msg
));
167 void makeUnlocked(); // interior version of unlock()
168 void makeUnlocked(const CssmData
&passphrase
); // interior version of unlock(CssmData)
169 static void discard(Common
*common
);
172 Common
*common
; // shared features of all instances of this database [const]
174 // all following data is locked by the common lock
175 bool mValidData
; // valid ACL and params (blob decoded)
177 uint32 version
; // version stamp for blob validity
178 DbBlob
*mBlob
; // database blob (encoded)
180 AccessCredentials
*mCred
; // local access credentials (always valid)
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