]> git.saurik.com Git - apple/securityd.git/blob - src/tokendatabase.h
437158dc86a19441495dcddfa2bce6777b9a2812
[apple/securityd.git] / src / tokendatabase.h
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, 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 // tokendatabase - software database container implementation.
27 //
28 // A TokenDatabase represents access to an external (secure) storage container
29 // of some kind (usually a smartcard token).
30 //
31 #ifndef _H_TOKENDATABASE
32 #define _H_TOKENDATABASE
33
34 #include "database.h"
35 #include "tokenacl.h"
36 #include "session.h"
37 #include "token.h"
38 #include <security_utilities/adornments.h>
39
40 class TokenDatabase;
41 class TokenDbCommon;
42 class TokenKey;
43 class TokenDaemon;
44
45
46 //
47 // The global per-system object for a TokenDatabase (the TokenDbGlobal so to
48 // speak) is the Token object itself (from token.h).
49 //
50
51
52 //
53 // TokenDatabase DbCommons
54 //
55 class TokenDbCommon : public DbCommon, public Adornable {
56 public:
57 TokenDbCommon(Session &ssn, Token &tk, const char *name);
58 ~TokenDbCommon();
59
60 Token &token() const;
61
62 uint32 subservice() const { return token().subservice(); }
63 const std::string &dbName() const;
64
65 Adornable &store();
66 void resetAcls();
67
68 void notify(NotificationEvent event);
69
70 void lockProcessing();
71
72 typedef Token::ResetGeneration ResetGeneration;
73
74 private:
75 std::string mDbName; // name given during open
76 Adornable mAdornments; // Adornable for ACL store
77
78 ResetGeneration mResetLevel; // validity tag
79 };
80
81
82 //
83 // A Database object represents a SC/CSPDL per-process access to a token.
84 //
85 class TokenDatabase : public Database {
86 friend class TokenDbCommon;
87 public:
88 TokenDatabase(uint32 ssid, Process &proc, const char *name, const AccessCredentials *cred);
89 ~TokenDatabase();
90
91 TokenDbCommon &common() const;
92 Token &token() const { return common().token(); }
93 TokenDaemon &tokend();
94 uint32 subservice() const { return common().subservice(); }
95 const char *dbName() const;
96 void dbName(const char *name);
97 bool transient() const;
98
99 SecurityServerAcl &acl(); // it's our Token
100
101 bool isLocked() const;
102
103 void notify(NotificationEvent event) { return common().notify(event); }
104
105 bool validateSecret(const AclSubject *subject, const AccessCredentials *cred);
106
107 const AccessCredentials *openCreds() const { return mOpenCreds; }
108
109 protected:
110 // any Process-referent concept handle we hand out to the client
111 class Handler {
112 public:
113 Handler() : mHandle(0) { }
114 CSSM_HANDLE &tokenHandle() { return mHandle; }
115 CSSM_HANDLE tokenHandle() const { return mHandle; }
116
117 protected:
118 CSSM_HANDLE mHandle;
119 };
120
121 // CSSM-style search handles (returned by findFirst)
122 struct Search : public Database::Search, public Handler {
123 Search(TokenDatabase &db) : Database::Search(db) { }
124 TokenDatabase &database() const { return referent<TokenDatabase>(); }
125 ~Search();
126
127 Search *commit() { database().addReference(*this); return this; }
128 };
129
130 // CSSM-style record handles (returned by findFirst/findNext et al)
131 struct Record : public Database::Record, public Handler, public TokenAcl {
132 Record(TokenDatabase &db) : Database::Record(db) { }
133 TokenDatabase &database() const { return referent<TokenDatabase>(); }
134 ~Record();
135
136 Record *commit() { database().addReference(*this); return this; }
137
138 void validate(AclAuthorization auth, const AccessCredentials *cred)
139 { TokenAcl::validate(auth, cred, &database()); }
140
141 // TokenAcl personality
142 AclKind aclKind() const;
143 Token &token();
144 using Handler::tokenHandle;
145 GenericHandle tokenHandle() const;
146 };
147
148 public:
149 //
150 // Cryptographic service calls
151 //
152 void queryKeySizeInBits(Key &key, CssmKeySize &result);
153 void getOutputSize(const Context &context, Key &key, uint32 inputSize, bool encrypt, uint32 &result);
154
155 // service calls
156 void generateSignature(const Context &context, Key &key, CSSM_ALGORITHMS signOnlyAlgorithm,
157 const CssmData &data, CssmData &signature);
158 void verifySignature(const Context &context, Key &key, CSSM_ALGORITHMS verifyOnlyAlgorithm,
159 const CssmData &data, const CssmData &signature);
160 void generateMac(const Context &context, Key &key,
161 const CssmData &data, CssmData &mac);
162 void verifyMac(const Context &context, Key &key,
163 const CssmData &data, const CssmData &mac);
164
165 void encrypt(const Context &context, Key &key, const CssmData &clear, CssmData &cipher);
166 void decrypt(const Context &context, Key &key, const CssmData &cipher, CssmData &clear);
167
168 void generateKey(const Context &context,
169 const AccessCredentials *cred, const AclEntryPrototype *owner,
170 uint32 usage, uint32 attrs, RefPointer<Key> &newKey);
171 void generateKey(const Context &context,
172 const AccessCredentials *cred, const AclEntryPrototype *owner,
173 uint32 pubUsage, uint32 pubAttrs, uint32 privUsage, uint32 privAttrs,
174 RefPointer<Key> &publicKey, RefPointer<Key> &privateKey);
175 void deriveKey(const Context &context, Key *key,
176 const AccessCredentials *cred, const AclEntryPrototype *owner,
177 CssmData *param, uint32 usage, uint32 attrs, RefPointer<Key> &derivedKey);
178
179 void wrapKey(const Context &context, const AccessCredentials *cred,
180 Key *hWrappingKey, Key &keyToBeWrapped,
181 const CssmData &descriptiveData, CssmKey &wrappedKey);
182 void unwrapKey(const Context &context,
183 const AccessCredentials *cred, const AclEntryPrototype *owner,
184 Key *wrappingKey, Key *publicKey, CSSM_KEYUSE usage, CSSM_KEYATTR_FLAGS attrs,
185 const CssmKey wrappedKey, RefPointer<Key> &unwrappedKey, CssmData &descriptiveData);
186
187 public:
188 //
189 // Data-access calls
190 //
191 void findFirst(const CssmQuery &query,
192 CssmDbRecordAttributeData *inAttributes, mach_msg_type_number_t inAttributesLength,
193 CssmData *data, RefPointer<Key> &key,
194 RefPointer<Database::Search> &search, RefPointer<Database::Record> &record,
195 CssmDbRecordAttributeData * &outAttributes, mach_msg_type_number_t &outAttributesLength);
196 void findNext(Database::Search *search,
197 CssmDbRecordAttributeData *inAttributes, mach_msg_type_number_t inAttributesLength,
198 CssmData *data, RefPointer<Key> &key, RefPointer<Database::Record> &record,
199 CssmDbRecordAttributeData * &outAttributes, mach_msg_type_number_t &outAttributesLength);
200 void findRecordHandle(Database::Record *record,
201 CssmDbRecordAttributeData *inAttributes, mach_msg_type_number_t inAttributesLength,
202 CssmData *data, RefPointer<Key> &key,
203 CssmDbRecordAttributeData * &outAttributes, mach_msg_type_number_t &outAttributesLength);
204 void insertRecord(CSSM_DB_RECORDTYPE recordtype,
205 const CssmDbRecordAttributeData *attributes, mach_msg_type_number_t inAttributesLength,
206 const CssmData &data, RefPointer<Database::Record> &record);
207 void modifyRecord(CSSM_DB_RECORDTYPE recordtype, Record *record,
208 const CssmDbRecordAttributeData *attributes, mach_msg_type_number_t inAttributesLength,
209 const CssmData *data, CSSM_DB_MODIFY_MODE modifyMode);
210 void deleteRecord(Database::Record *record);
211
212 // authenticate to database
213 void authenticate(CSSM_DB_ACCESS_TYPE mode, const AccessCredentials *cred);
214
215 private:
216 // internal utilities
217 RefPointer<Key> makeKey(KeyHandle hKey, const CssmKey *key,
218 const AclEntryPrototype *owner);
219
220 class InputKey {
221 public:
222 InputKey(Key *key) { setup(key); }
223 InputKey(Key &key) { setup(&key); }
224 ~InputKey();
225
226 operator KeyHandle () const { return mKeyHandle; }
227 operator const CssmKey * () const { return mKeyPtr; }
228
229 private:
230 KeyHandle mKeyHandle;
231 CssmKey mKey;
232 CssmKey *mKeyPtr;
233
234 void setup(Key *key);
235 };
236
237 private:
238 AccessCredentials *mOpenCreds; // credentials passed during open
239 };
240
241
242 #endif //_H_TOKENDATABASE