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 // ssclient - SecurityServer client interface library
26 #include <Security/cssm.h>
27 #include <Security/utilities.h>
28 #include <Security/cssmalloc.h>
29 #include <Security/cssmacl.h>
30 #include <Security/context.h>
31 #include <Security/globalizer.h>
32 #include <Security/mach++.h>
33 #include <Security/cssmdb.h>
34 #include <Security/osxsigning.h>
35 #include <Security/Authorization.h>
36 #include <Security/AuthSession.h>
42 using MachPlusPlus::Port
;
43 using MachPlusPlus::ReceivePort
;
46 namespace SecurityServer
52 typedef CSSM_HANDLE KeyHandle
;
53 typedef CSSM_HANDLE DbHandle
;
55 static const CSSM_HANDLE noDb
= 0;
56 static const CSSM_HANDLE noKey
= 0;
62 struct AuthorizationBlob
{
65 bool operator < (const AuthorizationBlob
&other
) const
66 { return memcmp(data
, other
.data
, sizeof(data
)) < 0; }
68 bool operator == (const AuthorizationBlob
&other
) const
69 { return memcmp(data
, other
.data
, sizeof(data
)) == 0; }
71 size_t hash() const { //@@@ revisit this hash
72 return data
[0] ^ data
[1] << 3;
76 enum AclKind
{ dbAcl
, keyAcl
, loginAcl
};
80 // Database parameter structure
84 uint32 idleTimeout
; // seconds idle timout lock
85 uint8 lockOnSleep
; // lock keychain when system sleeps
90 // A client connection (session)
95 ClientSession(CssmAllocator
&standard
, CssmAllocator
&returning
);
96 virtual ~ClientSession();
98 CssmAllocator
&internalAllocator
;
99 CssmAllocator
&returnAllocator
;
102 typedef CSSM_DB_ACCESS_TYPE DBAccessType
;
110 DbHandle
createDb(const DLDbIdentifier
&dbId
,
111 const AccessCredentials
*cred
, const AclEntryInput
*owner
,
112 const DBParameters
¶ms
);
113 DbHandle
decodeDb(const DLDbIdentifier
&dbId
,
114 const AccessCredentials
*cred
, const CssmData
&blob
);
115 void encodeDb(DbHandle db
, CssmData
&blob
, CssmAllocator
&alloc
);
116 void encodeDb(DbHandle db
, CssmData
&blob
) { return encodeDb(db
, blob
, returnAllocator
); }
117 void releaseDb(DbHandle db
);
118 void authenticateDb(DbHandle db
, DBAccessType type
, const AccessCredentials
*cred
);
119 void setDbParameters(DbHandle db
, const DBParameters
¶ms
);
120 void getDbParameters(DbHandle db
, DBParameters
¶ms
);
121 void changePassphrase(DbHandle db
, const AccessCredentials
*cred
);
122 void lock(DbHandle db
);
123 void unlock(DbHandle db
);
124 void unlock(DbHandle db
, const CssmData
&passPhrase
);
125 bool isLocked(DbHandle db
);
128 void encodeKey(KeyHandle key
, CssmData
&blob
, KeyUID
*uid
, CssmAllocator
&alloc
);
129 void encodeKey(KeyHandle key
, CssmData
&blob
, KeyUID
*uid
= NULL
)
130 { return encodeKey(key
, blob
, uid
, returnAllocator
); }
131 KeyHandle
decodeKey(DbHandle db
, const CssmData
&blob
, CssmKey::Header
&header
);
132 void releaseKey(KeyHandle key
);
135 // key wrapping and unwrapping
136 void wrapKey(const Context
&context
, KeyHandle key
, KeyHandle keyToBeWrapped
,
137 const AccessCredentials
*cred
,
138 const CssmData
*descriptiveData
, CssmWrappedKey
&wrappedKey
, CssmAllocator
&alloc
);
139 void wrapKey(const Context
&context
, KeyHandle key
, KeyHandle keyToBeWrapped
,
140 const AccessCredentials
*cred
,
141 const CssmData
*descriptiveData
, CssmWrappedKey
&wrappedKey
)
142 { return wrapKey(context
, key
, keyToBeWrapped
, cred
,
143 descriptiveData
, wrappedKey
, returnAllocator
); }
145 void unwrapKey(DbHandle db
, const Context
&context
, KeyHandle key
, KeyHandle publicKey
,
146 const CssmWrappedKey
&wrappedKey
, uint32 keyUsage
, uint32 keyAttr
,
147 const AccessCredentials
*cred
, const AclEntryInput
*owner
,
148 CssmData
&data
, KeyHandle
&newKey
, CssmKey::Header
&newKeyHeader
, CssmAllocator
&alloc
);
149 void unwrapKey(DbHandle db
, const Context
&context
, KeyHandle key
, KeyHandle publicKey
,
150 const CssmWrappedKey
&wrappedKey
, uint32 keyUsage
, uint32 keyAttr
,
151 const AccessCredentials
*cred
, const AclEntryInput
*owner
, CssmData
&data
,
152 KeyHandle
&newKey
, CssmKey::Header
&newKeyHeader
)
153 { return unwrapKey(db
, context
, key
, publicKey
, wrappedKey
, keyUsage
, keyAttr
,
154 cred
, owner
, data
, newKey
, newKeyHeader
, returnAllocator
); }
156 // key generation and derivation
157 void generateKey(DbHandle db
, const Context
&context
, uint32 keyUsage
, uint32 keyAttr
,
158 const AccessCredentials
*cred
, const AclEntryInput
*owner
,
159 KeyHandle
&newKey
, CssmKey::Header
&newHeader
);
160 void generateKey(DbHandle db
, const Context
&context
,
161 uint32 pubKeyUsage
, uint32 pubKeyAttr
,
162 uint32 privKeyUsage
, uint32 privKeyAttr
,
163 const AccessCredentials
*cred
, const AclEntryInput
*owner
,
164 KeyHandle
&pubKey
, CssmKey::Header
&pubHeader
,
165 KeyHandle
&privKey
, CssmKey::Header
&privHeader
);
166 void deriveKey(DbHandle db
, KeyHandle
&newKey
, CssmKey::Header
&newHeader
);
167 //void generateAlgorithmParameters(); // not implemented
169 void generateRandom(CssmData
&data
);
172 void encrypt(const Context
&context
, KeyHandle key
,
173 const CssmData
&in
, CssmData
&out
, CssmAllocator
&alloc
);
174 void encrypt(const Context
&context
, KeyHandle key
, const CssmData
&in
, CssmData
&out
)
175 { return encrypt(context
, key
, in
, out
, returnAllocator
); }
176 void decrypt(const Context
&context
, KeyHandle key
,
177 const CssmData
&in
, CssmData
&out
, CssmAllocator
&alloc
);
178 void decrypt(const Context
&context
, KeyHandle key
, const CssmData
&in
, CssmData
&out
)
179 { return decrypt(context
, key
, in
, out
, returnAllocator
); }
182 void generateSignature(const Context
&context
, KeyHandle key
,
183 const CssmData
&data
, CssmData
&signature
, CssmAllocator
&alloc
);
184 void generateSignature(const Context
&context
, KeyHandle key
,
185 const CssmData
&data
, CssmData
&signature
)
186 { return generateSignature(context
, key
, data
, signature
, returnAllocator
); }
187 void verifySignature(const Context
&context
, KeyHandle key
,
188 const CssmData
&data
, const CssmData
&signature
);
191 void generateMac(const Context
&context
, KeyHandle key
,
192 const CssmData
&data
, CssmData
&mac
, CssmAllocator
&alloc
);
193 void generateMac(const Context
&context
, KeyHandle key
,
194 const CssmData
&data
, CssmData
&mac
)
195 { return generateMac(context
, key
, data
, mac
, returnAllocator
); }
196 void verifyMac(const Context
&context
, KeyHandle key
,
197 const CssmData
&data
, const CssmData
&mac
);
198 uint32
queryKeySizeInBits(KeyHandle key
);
200 // key ACL management
201 void getKeyAcl(KeyHandle key
, const char *tag
,
202 uint32
&count
, AclEntryInfo
* &info
, CssmAllocator
&alloc
);
203 void getKeyAcl(KeyHandle key
, const char *tag
,
204 uint32
&count
, AclEntryInfo
* &info
)
205 { return getKeyAcl(key
, tag
, count
, info
, returnAllocator
); }
206 void changeKeyAcl(KeyHandle key
, const AccessCredentials
&cred
, const AclEdit
&edit
);
207 void getKeyOwner(KeyHandle key
, AclOwnerPrototype
&owner
, CssmAllocator
&alloc
);
208 void getKeyOwner(KeyHandle key
, AclOwnerPrototype
&owner
)
209 { return getKeyOwner(key
, owner
, returnAllocator
); }
210 void changeKeyOwner(KeyHandle key
, const AccessCredentials
&cred
,
211 const AclOwnerPrototype
&edit
);
213 // database ACL management
214 void getDbAcl(DbHandle db
, const char *tag
,
215 uint32
&count
, AclEntryInfo
* &info
, CssmAllocator
&alloc
);
216 void getDbAcl(DbHandle db
, const char *tag
,
217 uint32
&count
, AclEntryInfo
* &info
)
218 { return getDbAcl(db
, tag
, count
, info
, returnAllocator
); }
219 void changeDbAcl(DbHandle db
, const AccessCredentials
&cred
, const AclEdit
&edit
);
220 void getDbOwner(DbHandle db
, AclOwnerPrototype
&owner
, CssmAllocator
&alloc
);
221 void getDbOwner(DbHandle db
, AclOwnerPrototype
&owner
)
222 { return getDbOwner(db
, owner
, returnAllocator
); }
223 void changeDbOwner(DbHandle db
, const AccessCredentials
&cred
,
224 const AclOwnerPrototype
&edit
);
227 // Authorization API support
228 void authCreate(const AuthorizationItemSet
*rights
, const AuthorizationItemSet
*environment
,
229 AuthorizationFlags flags
,AuthorizationBlob
&result
);
230 void authRelease(const AuthorizationBlob
&auth
, AuthorizationFlags flags
);
231 void authCopyRights(const AuthorizationBlob
&auth
,
232 const AuthorizationItemSet
*rights
, const AuthorizationItemSet
*environment
,
233 AuthorizationFlags flags
, AuthorizationItemSet
**result
);
234 void authCopyInfo(const AuthorizationBlob
&auth
, const char *tag
, AuthorizationItemSet
* &info
);
235 void authExternalize(const AuthorizationBlob
&auth
, AuthorizationExternalForm
&extForm
);
236 void authInternalize(const AuthorizationExternalForm
&extForm
, AuthorizationBlob
&auth
);
239 // Session API support
240 void getSessionInfo(SecuritySessionId
&sessionId
, SessionAttributeBits
&attrs
);
241 void setupSession(SessionCreationFlags flags
, SessionAttributeBits attrs
);
244 void getAcl(AclKind kind
, KeyHandle key
, const char *tag
,
245 uint32
&count
, AclEntryInfo
* &info
, CssmAllocator
&alloc
);
246 void changeAcl(AclKind kind
, KeyHandle key
,
247 const AccessCredentials
&cred
, const AclEdit
&edit
);
248 void getOwner(AclKind kind
, KeyHandle key
, AclOwnerPrototype
&owner
, CssmAllocator
&alloc
);
249 void changeOwner(AclKind kind
, KeyHandle key
, const AccessCredentials
&cred
,
250 const AclOwnerPrototype
&edit
);
254 Thread() : replyPort(mig_get_reply_port()), registered(false) { }
255 operator bool() const { return registered
; }
257 Port replyPort
; // cached mig_get_reply_port
258 bool registered
; // has been registered with SecurityServer
264 CodeSigning::OSXCode
*myself
;
265 ThreadNexus
<Thread
> thread
;
268 static ModuleNexus
<Global
> mGlobal
;
272 } // end namespace SecurityServer
274 } // end namespace Security