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  CRYPTKIT_CSP_ENABLE 
  37 class CryptKitFactory
; 
  43 class RSA_DSA_Factory
; 
  46 /* one per attach/detach */ 
  47 class AppleCSPSession 
: public CSPFullPluginSession 
{ 
  51                 CSSM_MODULE_HANDLE      handle
, 
  53                 const CSSM_VERSION      
&Version
, 
  55                 CSSM_SERVICE_TYPE       SubServiceType
, 
  56                 CSSM_ATTACH_FLAGS       AttachFlags
, 
  57                 const CSSM_UPCALLS      
&upcalls
); 
  61         CSPContext 
*contextCreate( 
  62                 CSSM_CC_HANDLE          handle
,  
  63                 const Context           
&context
); 
  66                 const Context           
&context
,  
  69         // Functions declared in CSPFullPluginSession which we override. 
  71         // Free a key. If this is a reference key 
  72         // we generated, remove it from refKeyMap.  
  73         void FreeKey(const AccessCredentials 
*AccessCred
, 
  77         void UnwrapKey(CSSM_CC_HANDLE CCHandle
, 
  78         const Context 
&Context
, 
  79         const CssmKey 
*PublicKey
, 
  80         const CssmKey 
&WrappedKey
, 
  83         const CssmData 
*KeyLabel
, 
  84         const CSSM_RESOURCE_CONTROL_CONTEXT 
*CredAndAclEntry
, 
  85         CssmKey 
&UnwrappedKey
, 
  86         CssmData 
&DescriptiveData
, 
  87         CSSM_PRIVILEGE Privilege
); 
  88         void WrapKey(CSSM_CC_HANDLE CCHandle
, 
  89         const Context 
&Context
, 
  90         const AccessCredentials 
&AccessCred
, 
  92         const CssmData 
*DescriptiveData
, 
  94         CSSM_PRIVILEGE Privilege
); 
  95         void DeriveKey(CSSM_CC_HANDLE CCHandle
, 
  96                 const Context 
&Context
, 
 100                 const CssmData 
*KeyLabel
, 
 101                 const CSSM_RESOURCE_CONTROL_CONTEXT 
*CredAndAclEntry
, 
 102                 CssmKey 
&DerivedKey
); 
 103         void PassThrough(CSSM_CC_HANDLE CCHandle
, 
 104                 const Context 
&Context
, 
 105                 uint32 PassThroughId
, 
 108         void getKeySize(const CssmKey 
&key
,  
 109                 CSSM_KEY_SIZE 
&size
); 
 111         // add a BinaryKey to our refKeyMap. Sets up cssmKey 
 117         // Given a CssmKey in reference form, obtain the associated 
 119         BinaryKey 
&lookupRefKey( 
 120                 const CssmKey           
&cssmKey
); 
 122         // CSP's RNG. This redirects to Yarrow. 
 123         void                                    getRandomBytes(size_t length
, uint8 
*cp
); 
 124         void                                    addEntropy(size_t length
, const uint8 
*cp
);   
 126         Allocator                       
&normAlloc()  { return normAllocator
; }  
 127     Allocator                   
&privAlloc()  { return privAllocator
; } 
 129         #ifdef  CRYPTKIT_CSP_ENABLE 
 130         CryptKitFactory                 
&cryptKitFactory
; 
 132         MiscAlgFactory                  
&miscAlgFactory
; 
 133         #ifdef  ASC_CSP_ENABLE 
 134         AscAlgFactory                   
&ascAlgFactory
; 
 136         RSA_DSA_Factory                 
&rsaDsaAlgFactory
; 
 137         DH_Factory                              
&dhAlgFactory
; 
 140         // storage of binary keys (which apps know as reference keys) 
 141         typedef std::map
<KeyRef
, const BinaryKey 
*> keyMap
; 
 144     Allocator                   
&normAllocator
;  
 145     Allocator                   
&privAllocator
;  
 147         BinaryKey                               
*lookupKeyRef(KeyRef keyRef
); 
 148         void                                    DeriveKey_PBKDF2( 
 149                                                                 const Context 
&Context
, 
 150                                                                 const CssmData 
&Param
, 
 153         void                                    DeriveKey_PKCS5_V1_5( 
 154                                                                 const Context 
&context
, 
 155                                                                 CSSM_ALGORITHMS algId
, 
 156                                                                 const CssmData 
&Param
, 
 159         void                                    DeriveKey_OpenSSH1( 
 160                                                                 const Context 
&context
, 
 161                                                                 CSSM_ALGORITHMS algId
, 
 162                                                                 const CssmData 
&Param
, 
 165         /* CMS wrap/unwrap, called out from standard wrap/unwrap */ 
 167                 CSSM_CC_HANDLE CCHandle
, 
 168                 const Context 
&Context
, 
 169                 const AccessCredentials 
&AccessCred
, 
 170                 const CssmKey 
&UnwrappedKey
, 
 172                 bool allocdRawBlob
,                     // callee has to free rawBlob 
 173                 const CssmData 
*DescriptiveData
, 
 175                 CSSM_PRIVILEGE Privilege
); 
 178                 CSSM_CC_HANDLE CCHandle
, 
 179                 const Context 
&Context
, 
 180                 const CssmKey 
&WrappedKey
, 
 181                 const CSSM_RESOURCE_CONTROL_CONTEXT 
*CredAndAclEntry
, 
 182                 CssmKey 
&UnwrappedKey
, 
 183                 CssmData 
&DescriptiveData
, 
 184                 CSSM_PRIVILEGE Privilege
, 
 185                 cspKeyStorage keyStorage
); 
 187         /* OpenSSHv1 wrap/unwrap, called out from standard wrap/unwrap */ 
 188         void WrapKeyOpenSSH1( 
 189                 CSSM_CC_HANDLE CCHandle
, 
 190                 const Context 
&Context
, 
 191                 const AccessCredentials 
&AccessCred
, 
 192                 BinaryKey 
&unwrappedBinKey
, 
 194                 bool allocdRawBlob
,                     // callee has to free rawBlob 
 195                 const CssmData 
*DescriptiveData
, 
 197                 CSSM_PRIVILEGE Privilege
); 
 199         void UnwrapKeyOpenSSH1( 
 200                 CSSM_CC_HANDLE CCHandle
, 
 201                 const Context 
&Context
, 
 202                 const CssmKey 
&WrappedKey
, 
 203                 const CSSM_RESOURCE_CONTROL_CONTEXT 
*CredAndAclEntry
, 
 204                 CssmKey 
&UnwrappedKey
, 
 205                 CssmData 
&DescriptiveData
, 
 206                 CSSM_PRIVILEGE Privilege
, 
 207                 cspKeyStorage keyStorage
); 
 210          * Used for generating crypto contexts at this level.  
 211          * Analogous to AlgorithmFactory.setup(). 
 214                 CSPFullPluginSession::CSPContext 
* &cspCtx
,  
 215                 const Context 
&context
); 
 218          * Find a CSPKeyInfoProvider subclass for the specified key. 
 220         CSPKeyInfoProvider 
*infoProvider( 
 223         void pkcs8InferKeyHeader( 
 226         void opensslInferKeyHeader( 
 229 };      /* AppleCSPSession */ 
 232 #endif //_APPLE_CSP_SESSION_H_