2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
26 // database - abstract database management
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.
35 #include "structure.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>
53 using MachPlusPlus::MachServer
;
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.
63 class DbCommon
: public PerSession
{
65 DbCommon(Session
&ssn
);
67 Session
&session() const;
69 virtual void sleepProcessing(); // generic action on system sleep
70 virtual void lockProcessing(); // generic action on "lock" requests
75 // A Database object represents an Apple CSP/DL open database (DL/DB) object.
76 // It maintains its protected semantic state (including keys) and provides controlled
79 class Database
: public PerProcess
, public AclSource
{
80 static const NotificationEvent lockedEvent
= kNotificationEventLocked
;
81 static const NotificationEvent unlockedEvent
= kNotificationEventUnlocked
;
82 static const NotificationEvent passphraseChangedEvent
= kNotificationEventPassphraseChanged
;
85 Database(Process
&proc
);
88 Process
& process() const;
90 virtual bool transient() const = 0; // is transient store (reboot clears)
94 // A common class for objects that "belong" to a Database and
95 // don't have parents up the global stack. These will die along with
96 // the Database when it goes.
98 class Subsidiary
: public PerProcess
{
100 Subsidiary(Database
&db
) { referent(db
); }
101 Database
&database() const { return referent
<Database
>(); }
102 Process
&process() const { return database().process(); }
106 // Cryptographic service calls.
107 // These must be supported by any type of database.
109 virtual void releaseKey(Key
&key
);
110 virtual void queryKeySizeInBits(Key
&key
, CssmKeySize
&result
) = 0;
111 virtual void getOutputSize(const Context
&context
, Key
&key
,
112 uint32 inputSize
, bool encrypt
, uint32
&result
) = 0;
114 virtual void generateSignature(const Context
&context
, Key
&key
,
115 CSSM_ALGORITHMS signOnlyAlgorithm
, const CssmData
&data
, CssmData
&signature
) = 0;
116 virtual void verifySignature(const Context
&context
, Key
&key
,
117 CSSM_ALGORITHMS verifyOnlyAlgorithm
, const CssmData
&data
, const CssmData
&signature
) = 0;
118 virtual void generateMac(const Context
&context
, Key
&key
,
119 const CssmData
&data
, CssmData
&mac
) = 0;
120 virtual void verifyMac(const Context
&context
, Key
&key
,
121 const CssmData
&data
, const CssmData
&mac
) = 0;
123 virtual void encrypt(const Context
&context
, Key
&key
, const CssmData
&clear
, CssmData
&cipher
) = 0;
124 virtual void decrypt(const Context
&context
, Key
&key
, const CssmData
&cipher
, CssmData
&clear
) = 0;
126 virtual void generateKey(const Context
&context
,
127 const AccessCredentials
*cred
, const AclEntryPrototype
*owner
,
128 uint32 usage
, uint32 attrs
, RefPointer
<Key
> &newKey
) = 0;
129 virtual void generateKey(const Context
&context
,
130 const AccessCredentials
*cred
, const AclEntryPrototype
*owner
,
131 uint32 pubUsage
, uint32 pubAttrs
, uint32 privUsage
, uint32 privAttrs
,
132 RefPointer
<Key
> &publicKey
, RefPointer
<Key
> &privateKey
) = 0;
134 virtual void wrapKey(const Context
&context
, const AccessCredentials
*cred
,
135 Key
*wrappingKey
, Key
&keyToBeWrapped
,
136 const CssmData
&descriptiveData
, CssmKey
&wrappedKey
) = 0;
137 virtual void unwrapKey(const Context
&context
,
138 const AccessCredentials
*cred
, const AclEntryPrototype
*owner
,
139 Key
*wrappingKey
, Key
*publicKey
, CSSM_KEYUSE usage
, CSSM_KEYATTR_FLAGS attrs
,
140 const CssmKey wrappedKey
, RefPointer
<Key
> &unwrappedKey
, CssmData
&descriptiveData
) = 0;
141 virtual void deriveKey(const Context
&context
, Key
*key
,
142 const AccessCredentials
*cred
, const AclEntryPrototype
*owner
,
143 CssmData
*param
, uint32 usage
, uint32 attrs
, RefPointer
<Key
> &derivedKey
) = 0;
145 virtual void authenticate(CSSM_DB_ACCESS_TYPE mode
, const AccessCredentials
*cred
);
146 virtual SecurityServerAcl
&acl();
148 virtual bool isLocked() const;
151 class Search
: public Subsidiary
{
153 Search(Database
&db
) : Subsidiary(db
) { }
156 class Record
: public Subsidiary
{
158 Record(Database
&db
) : Subsidiary(db
) { }
161 virtual void findFirst(const CssmQuery
&query
,
162 CssmDbRecordAttributeData
*inAttributes
, mach_msg_type_number_t inAttributesLength
,
163 CssmData
*data
, RefPointer
<Key
> &key
, RefPointer
<Search
> &search
,
164 RefPointer
<Record
> &record
,
165 CssmDbRecordAttributeData
* &outAttributes
, mach_msg_type_number_t
&outAttributesLength
);
166 virtual void findNext(Search
*search
,
167 CssmDbRecordAttributeData
*inAttributes
, mach_msg_type_number_t inAttributesLength
,
168 CssmData
*data
, RefPointer
<Key
> &key
, RefPointer
<Record
> &record
,
169 CssmDbRecordAttributeData
* &outAttributes
, mach_msg_type_number_t
&outAttributesLength
);
170 virtual void findRecordHandle(Record
*record
,
171 CssmDbRecordAttributeData
*inAttributes
, mach_msg_type_number_t inAttributesLength
,
172 CssmData
*data
, RefPointer
<Key
> &key
,
173 CssmDbRecordAttributeData
* &outAttributes
, mach_msg_type_number_t
&outAttributesLength
);
175 virtual void insertRecord(CSSM_DB_RECORDTYPE recordtype
,
176 const CssmDbRecordAttributeData
*attributes
, mach_msg_type_number_t inAttributesLength
,
177 const CssmData
&data
, RecordHandle
&record
);
178 virtual void modifyRecord(CSSM_DB_RECORDTYPE recordtype
, Record
*record
,
179 const CssmDbRecordAttributeData
*attributes
, mach_msg_type_number_t inAttributesLength
,
180 const CssmData
*data
, CSSM_DB_MODIFY_MODE modifyMode
);
181 virtual void deleteRecord(Database::Record
*record
);
183 virtual void releaseSearch(Search
&search
);
184 virtual void releaseRecord(Record
&record
);
187 // SecurityServerAcl personality
188 AclKind
aclKind() const;
189 GenericHandle
aclHandle() const;
190 Database
*relatedDatabase();
193 // support ACL remote secret validation (default is no support)
194 virtual bool validateSecret(const AclSubject
*subject
, const AccessCredentials
*cred
);
197 static const int maxUnlockTryCount
= 3;
200 DbCommon
& common() const { return parent
<DbCommon
>(); }
201 virtual const char *dbName() const = 0;
202 virtual void dbName(const char *name
);
207 // This class implements a "system keychain unlock record" store
209 class SystemKeychainKey
{
211 SystemKeychainKey(const char *path
);
212 ~SystemKeychainKey();
214 bool matches(const DbBlob::Signature
&signature
);
215 CssmKey
&key() { return mKey
; }
218 std::string mPath
; // path to file
219 CssmKey mKey
; // proper CssmKey with data in mBlob
221 bool mValid
; // mBlob was validly read from mPath
222 UnlockBlob mBlob
; // contents of mPath as last read
224 Time::Absolute mCachedDate
; // modify date of file when last read
225 Time::Absolute mUpdateThreshold
; // cutoff threshold for checking again
227 static const int checkDelay
= 1; // seconds minimum delay between update checks