2 * Copyright (c) 2000-2001,2011,2014 Apple 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 // AppleCSPSession.h - top-level session class
22 #ifndef _APPLE_CSP_SESSION_H_
23 #define _APPLE_CSP_SESSION_H_
25 #include <security_cdsa_plugin/cssmplugin.h>
26 #include <security_cdsa_plugin/pluginsession.h>
27 #include <security_cdsa_plugin/CSPsession.h>
28 #include <security_utilities/threading.h>
29 #include "BinaryKey.h"
30 #include "AppleCSPUtils.h"
32 class CSPKeyInfoProvider
;
34 /* avoid unnecessary includes.... */
36 #ifdef BSAFE_CSP_ENABLE
39 #ifdef CRYPTKIT_CSP_ENABLE
40 class CryptKitFactory
;
46 class RSA_DSA_Factory
;
49 /* one per attach/detach */
50 class AppleCSPSession
: public CSPFullPluginSession
{
54 CSSM_MODULE_HANDLE handle
,
56 const CSSM_VERSION
&Version
,
58 CSSM_SERVICE_TYPE SubServiceType
,
59 CSSM_ATTACH_FLAGS AttachFlags
,
60 const CSSM_UPCALLS
&upcalls
);
64 CSPContext
*contextCreate(
65 CSSM_CC_HANDLE handle
,
66 const Context
&context
);
69 const Context
&context
,
72 // Functions declared in CSPFullPluginSession which we override.
74 // Free a key. If this is a reference key
75 // we generated, remove it from refKeyMap.
76 void FreeKey(const AccessCredentials
*AccessCred
,
80 void UnwrapKey(CSSM_CC_HANDLE CCHandle
,
81 const Context
&Context
,
82 const CssmKey
*PublicKey
,
83 const CssmKey
&WrappedKey
,
86 const CssmData
*KeyLabel
,
87 const CSSM_RESOURCE_CONTROL_CONTEXT
*CredAndAclEntry
,
88 CssmKey
&UnwrappedKey
,
89 CssmData
&DescriptiveData
,
90 CSSM_PRIVILEGE Privilege
);
91 void WrapKey(CSSM_CC_HANDLE CCHandle
,
92 const Context
&Context
,
93 const AccessCredentials
&AccessCred
,
95 const CssmData
*DescriptiveData
,
97 CSSM_PRIVILEGE Privilege
);
98 void DeriveKey(CSSM_CC_HANDLE CCHandle
,
99 const Context
&Context
,
103 const CssmData
*KeyLabel
,
104 const CSSM_RESOURCE_CONTROL_CONTEXT
*CredAndAclEntry
,
105 CssmKey
&DerivedKey
);
106 void PassThrough(CSSM_CC_HANDLE CCHandle
,
107 const Context
&Context
,
108 uint32 PassThroughId
,
111 void getKeySize(const CssmKey
&key
,
112 CSSM_KEY_SIZE
&size
);
114 // add a BinaryKey to our refKeyMap. Sets up cssmKey
120 // Given a CssmKey in reference form, obtain the associated
122 BinaryKey
&lookupRefKey(
123 const CssmKey
&cssmKey
);
125 // CSP's RNG. This redirects to Yarrow.
126 void getRandomBytes(size_t length
, uint8
*cp
);
127 void addEntropy(size_t length
, const uint8
*cp
);
129 Allocator
&normAlloc() { return normAllocator
; }
130 Allocator
&privAlloc() { return privAllocator
; }
132 #ifdef BSAFE_CSP_ENABLE
133 BSafeFactory
&bSafe4Factory
;
135 #ifdef CRYPTKIT_CSP_ENABLE
136 CryptKitFactory
&cryptKitFactory
;
138 MiscAlgFactory
&miscAlgFactory
;
139 #ifdef ASC_CSP_ENABLE
140 AscAlgFactory
&ascAlgFactory
;
142 RSA_DSA_Factory
&rsaDsaAlgFactory
;
143 DH_Factory
&dhAlgFactory
;
146 // storage of binary keys (which apps know as reference keys)
147 typedef std::map
<KeyRef
, const BinaryKey
*> keyMap
;
150 Allocator
&normAllocator
;
151 Allocator
&privAllocator
;
153 BinaryKey
*lookupKeyRef(KeyRef keyRef
);
154 void DeriveKey_PBKDF2(
155 const Context
&Context
,
156 const CssmData
&Param
,
159 void DeriveKey_PKCS5_V1_5(
160 const Context
&context
,
161 CSSM_ALGORITHMS algId
,
162 const CssmData
&Param
,
165 void DeriveKey_OpenSSH1(
166 const Context
&context
,
167 CSSM_ALGORITHMS algId
,
168 const CssmData
&Param
,
171 /* CMS wrap/unwrap, called out from standard wrap/unwrap */
173 CSSM_CC_HANDLE CCHandle
,
174 const Context
&Context
,
175 const AccessCredentials
&AccessCred
,
176 const CssmKey
&UnwrappedKey
,
178 bool allocdRawBlob
, // callee has to free rawBlob
179 const CssmData
*DescriptiveData
,
181 CSSM_PRIVILEGE Privilege
);
184 CSSM_CC_HANDLE CCHandle
,
185 const Context
&Context
,
186 const CssmKey
&WrappedKey
,
187 const CSSM_RESOURCE_CONTROL_CONTEXT
*CredAndAclEntry
,
188 CssmKey
&UnwrappedKey
,
189 CssmData
&DescriptiveData
,
190 CSSM_PRIVILEGE Privilege
,
191 cspKeyStorage keyStorage
);
193 /* OpenSSHv1 wrap/unwrap, called out from standard wrap/unwrap */
194 void WrapKeyOpenSSH1(
195 CSSM_CC_HANDLE CCHandle
,
196 const Context
&Context
,
197 const AccessCredentials
&AccessCred
,
198 BinaryKey
&unwrappedBinKey
,
200 bool allocdRawBlob
, // callee has to free rawBlob
201 const CssmData
*DescriptiveData
,
203 CSSM_PRIVILEGE Privilege
);
205 void UnwrapKeyOpenSSH1(
206 CSSM_CC_HANDLE CCHandle
,
207 const Context
&Context
,
208 const CssmKey
&WrappedKey
,
209 const CSSM_RESOURCE_CONTROL_CONTEXT
*CredAndAclEntry
,
210 CssmKey
&UnwrappedKey
,
211 CssmData
&DescriptiveData
,
212 CSSM_PRIVILEGE Privilege
,
213 cspKeyStorage keyStorage
);
216 * Used for generating crypto contexts at this level.
217 * Analogous to AlgorithmFactory.setup().
220 CSPFullPluginSession::CSPContext
* &cspCtx
,
221 const Context
&context
);
224 * Find a CSPKeyInfoProvider subclass for the specified key.
226 CSPKeyInfoProvider
*infoProvider(
229 void pkcs8InferKeyHeader(
232 void opensslInferKeyHeader(
235 }; /* AppleCSPSession */
238 #endif //_APPLE_CSP_SESSION_H_