2 * Copyright (c) 2000-2008,2013 Apple 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 // tokendatabase - software database container implementation.
28 // A TokenDatabase represents access to an external (secure) storage container
29 // of some kind (usually a smartcard token).
31 #ifndef _H_TOKENDATABASE
32 #define _H_TOKENDATABASE
38 #include <security_utilities/adornments.h>
47 // The global per-system object for a TokenDatabase (the TokenDbGlobal so to
48 // speak) is the Token object itself (from token.h).
53 // TokenDatabase DbCommons
55 class TokenDbCommon
: public DbCommon
, public Adornable
{
57 TokenDbCommon(Session
&ssn
, Token
&tk
, const char *name
);
62 uint32
subservice() const { return token().subservice(); }
63 std::string
dbName() const;
65 // TokenDbCommons have no real version, return base
66 uint32
dbVersion() { return CommonBlob::version_MacOS_10_0
; }
71 void notify(NotificationEvent event
);
73 void lockProcessing();
75 typedef Token::ResetGeneration ResetGeneration
;
78 std::string mDbName
; // name given during open
79 bool mHasAclState
; // Adornment is carrying active ACL state
84 // A Database object represents a SC/CSPDL per-process access to a token.
86 class TokenDatabase
: public Database
{
87 friend class TokenDbCommon
;
89 TokenDatabase(uint32 ssid
, Process
&proc
, const char *name
, const AccessCredentials
*cred
);
92 TokenDbCommon
&common() const;
93 Token
&token() const { return common().token(); }
94 TokenDaemon
&tokend();
95 uint32
subservice() const { return common().subservice(); }
96 const char *dbName() const;
97 void dbName(const char *name
);
98 bool transient() const;
100 SecurityServerAcl
&acl(); // it's our Token
101 void getAcl(const char *tag
, uint32
&count
, AclEntryInfo
*&acls
); // post-processing
104 bool pinState(uint32 pin
, int *count
= NULL
);
106 void notify(NotificationEvent event
) { return common().notify(event
); }
108 bool validateSecret(const AclSubject
*subject
, const AccessCredentials
*cred
);
110 const AccessCredentials
*openCreds() const { return mOpenCreds
; }
113 // any Process-referent concept handle we hand out to the client
116 Handler() : mHandle(0) { }
117 GenericHandle
&tokenHandle() { return mHandle
; }
118 GenericHandle
tokenHandle() const { return mHandle
; }
121 GenericHandle mHandle
;
124 // CSSM-style search handles (returned by findFirst)
125 struct Search
: public Database::Search
, public Handler
{
126 Search(TokenDatabase
&db
) : Database::Search(db
) { }
127 TokenDatabase
&database() const { return referent
<TokenDatabase
>(); }
130 Search
*commit() { database().addReference(*this); return this; }
133 // CSSM-style record handles (returned by findFirst/findNext et al)
134 struct Record
: public Database::Record
, public Handler
, public TokenAcl
{
135 Record(TokenDatabase
&db
) : Database::Record(db
) { }
136 TokenDatabase
&database() const { return referent
<TokenDatabase
>(); }
139 Record
*commit() { database().addReference(*this); return this; }
141 void validate(AclAuthorization auth
, const AccessCredentials
*cred
, Database
*relatedDatabase
= NULL
)
142 { TokenAcl::validate(auth
, cred
, relatedDatabase
? relatedDatabase
: &database()); }
144 // TokenAcl personality
145 AclKind
aclKind() const;
147 using Handler::tokenHandle
;
148 GenericHandle
tokenHandle() const;
153 // Cryptographic service calls
155 void queryKeySizeInBits(Key
&key
, CssmKeySize
&result
);
156 void getOutputSize(const Context
&context
, Key
&key
, uint32 inputSize
, bool encrypt
, uint32
&result
);
159 void generateSignature(const Context
&context
, Key
&key
, CSSM_ALGORITHMS signOnlyAlgorithm
,
160 const CssmData
&data
, CssmData
&signature
);
161 void verifySignature(const Context
&context
, Key
&key
, CSSM_ALGORITHMS verifyOnlyAlgorithm
,
162 const CssmData
&data
, const CssmData
&signature
);
163 void generateMac(const Context
&context
, Key
&key
,
164 const CssmData
&data
, CssmData
&mac
);
165 void verifyMac(const Context
&context
, Key
&key
,
166 const CssmData
&data
, const CssmData
&mac
);
168 void encrypt(const Context
&context
, Key
&key
, const CssmData
&clear
, CssmData
&cipher
);
169 void decrypt(const Context
&context
, Key
&key
, const CssmData
&cipher
, CssmData
&clear
);
171 void generateKey(const Context
&context
,
172 const AccessCredentials
*cred
, const AclEntryPrototype
*owner
,
173 uint32 usage
, uint32 attrs
, RefPointer
<Key
> &newKey
);
174 void generateKey(const Context
&context
,
175 const AccessCredentials
*cred
, const AclEntryPrototype
*owner
,
176 uint32 pubUsage
, uint32 pubAttrs
, uint32 privUsage
, uint32 privAttrs
,
177 RefPointer
<Key
> &publicKey
, RefPointer
<Key
> &privateKey
);
178 void deriveKey(const Context
&context
, Key
*key
,
179 const AccessCredentials
*cred
, const AclEntryPrototype
*owner
,
180 CssmData
*param
, uint32 usage
, uint32 attrs
, RefPointer
<Key
> &derivedKey
);
182 void wrapKey(const Context
&context
, const AccessCredentials
*cred
,
183 Key
*hWrappingKey
, Key
&keyToBeWrapped
,
184 const CssmData
&descriptiveData
, CssmKey
&wrappedKey
);
185 void unwrapKey(const Context
&context
,
186 const AccessCredentials
*cred
, const AclEntryPrototype
*owner
,
187 Key
*wrappingKey
, Key
*publicKey
, CSSM_KEYUSE usage
, CSSM_KEYATTR_FLAGS attrs
,
188 const CssmKey wrappedKey
, RefPointer
<Key
> &unwrappedKey
, CssmData
&descriptiveData
);
194 void findFirst(const CssmQuery
&query
,
195 CssmDbRecordAttributeData
*inAttributes
, mach_msg_type_number_t inAttributesLength
,
196 CssmData
*data
, RefPointer
<Key
> &key
,
197 RefPointer
<Database::Search
> &search
, RefPointer
<Database::Record
> &record
,
198 CssmDbRecordAttributeData
* &outAttributes
, mach_msg_type_number_t
&outAttributesLength
);
199 void findNext(Database::Search
*search
,
200 CssmDbRecordAttributeData
*inAttributes
, mach_msg_type_number_t inAttributesLength
,
201 CssmData
*data
, RefPointer
<Key
> &key
, RefPointer
<Database::Record
> &record
,
202 CssmDbRecordAttributeData
* &outAttributes
, mach_msg_type_number_t
&outAttributesLength
);
203 void findRecordHandle(Database::Record
*record
,
204 CssmDbRecordAttributeData
*inAttributes
, mach_msg_type_number_t inAttributesLength
,
205 CssmData
*data
, RefPointer
<Key
> &key
,
206 CssmDbRecordAttributeData
* &outAttributes
, mach_msg_type_number_t
&outAttributesLength
);
207 void tokenInsertRecord(CSSM_DB_RECORDTYPE recordtype
,
208 const CssmDbRecordAttributeData
*attributes
, mach_msg_type_number_t inAttributesLength
,
209 const CssmData
&data
, RefPointer
<Database::Record
> &record
);
210 void modifyRecord(CSSM_DB_RECORDTYPE recordtype
, Database::Record
*record
,
211 const CssmDbRecordAttributeData
*attributes
, mach_msg_type_number_t inAttributesLength
,
212 const CssmData
*data
, CSSM_DB_MODIFY_MODE modifyMode
);
213 void deleteRecord(Database::Record
*record
);
215 // authenticate to database
216 void authenticate(CSSM_DB_ACCESS_TYPE mode
, const AccessCredentials
*cred
);
219 // internal utilities
220 RefPointer
<Key
> makeKey(KeyHandle hKey
, const CssmKey
*key
,
221 uint32 moreAttributes
, const AclEntryPrototype
*owner
);
225 InputKey(Key
*key
) { setup(key
); }
226 InputKey(Key
&key
) { setup(&key
); }
229 operator KeyHandle () const { return mKeyHandle
; }
230 operator const CssmKey
* () const { return mKeyPtr
; }
233 KeyHandle mKeyHandle
;
237 void setup(Key
*key
);
241 AccessCredentials
*mOpenCreds
; // credentials passed during open
242 mutable std::string mDbName
; // stored name for method dbName() which need to call c_str on object outside function scope
246 #endif //_H_TOKENDATABASE