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